]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
gdb/tui: add a missing white space character
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
a2c58332 3 Copyright (C) 1993-2022 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
5e8d7549 10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549 17 You should have received a copy of the GNU General Public License
b34976b6 18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
1b74d094
BW
23/*
24SECTION
252b5132
RH
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
661a3fd4 33 haven't bothered yet. */
252b5132 34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132 37#include "sysdep.h"
3a551c7a 38#include <limits.h>
3db64b00 39#include "bfd.h"
252b5132
RH
40#include "bfdlink.h"
41#include "libbfd.h"
42#define ARCH_SIZE 0
43#include "elf-bfd.h"
e0e8c97f 44#include "libiberty.h"
ff59fc36 45#include "safe-ctype.h"
de64ce13 46#include "elf-linux-core.h"
252b5132 47
8bc7f138
L
48#ifdef CORE_HEADER
49#include CORE_HEADER
50#endif
51
217aa764 52static int elf_sort_sections (const void *, const void *);
0a1b45a2
AM
53static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
50b2bdb7 58
252b5132
RH
59/* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
62
63/* Swap in a Verdef structure. */
64
65void
217aa764
AM
66_bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
252b5132 69{
dc810e39
AM
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
77}
78
79/* Swap out a Verdef structure. */
80
81void
217aa764
AM
82_bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
252b5132 85{
dc810e39
AM
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
93}
94
95/* Swap in a Verdaux structure. */
96
97void
217aa764
AM
98_bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
252b5132 101{
dc810e39
AM
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
104}
105
106/* Swap out a Verdaux structure. */
107
108void
217aa764
AM
109_bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
252b5132 112{
dc810e39
AM
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
115}
116
117/* Swap in a Verneed structure. */
118
119void
217aa764
AM
120_bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
252b5132 123{
dc810e39
AM
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
129}
130
131/* Swap out a Verneed structure. */
132
133void
217aa764
AM
134_bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
252b5132 137{
dc810e39
AM
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
143}
144
145/* Swap in a Vernaux structure. */
146
147void
217aa764
AM
148_bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
252b5132 151{
dc810e39
AM
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
157}
158
159/* Swap out a Vernaux structure. */
160
161void
217aa764
AM
162_bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
252b5132 165{
dc810e39
AM
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
171}
172
173/* Swap in a Versym structure. */
174
175void
217aa764
AM
176_bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
252b5132 179{
dc810e39 180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
181}
182
183/* Swap out a Versym structure. */
184
185void
217aa764
AM
186_bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
252b5132 189{
dc810e39 190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
191}
192
193/* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
3a99b017 195
252b5132 196unsigned long
217aa764 197bfd_elf_hash (const char *namearg)
252b5132 198{
3a99b017 199 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
200 unsigned long h = 0;
201 unsigned long g;
202 int ch;
203
204 while ((ch = *name++) != '\0')
205 {
206 h = (h << 4) + ch;
207 if ((g = (h & 0xf0000000)) != 0)
208 {
209 h ^= g >> 24;
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
212 h ^= g;
213 }
214 }
32dfa85d 215 return h & 0xffffffff;
252b5132
RH
216}
217
fdc90cb4
JJ
218/* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
220
221unsigned long
222bfd_elf_gnu_hash (const char *namearg)
223{
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
226 unsigned char ch;
227
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
231}
232
0c8d6e5c
AM
233/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
0a1b45a2 235bool
0c8d6e5c 236bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 237 size_t object_size,
4dfe6ac6 238 enum elf_target_id object_id)
252b5132 239{
0ffa91dd
NC
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
0a1b45a2 243 return false;
252b5132 244
0ffa91dd 245 elf_object_id (abfd) = object_id;
c0355132
AM
246 if (abfd->direction != read_direction)
247 {
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 if (o == NULL)
0a1b45a2 250 return false;
c0355132
AM
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
253 }
0a1b45a2 254 return true;
252b5132
RH
255}
256
0ffa91dd 257
0a1b45a2 258bool
ae95ffa6 259bfd_elf_make_object (bfd *abfd)
0ffa91dd 260{
ae95ffa6 261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 263 bed->target_id);
0ffa91dd
NC
264}
265
0a1b45a2 266bool
217aa764 267bfd_elf_mkcorefile (bfd *abfd)
252b5132 268{
c044fabd 269 /* I think this can be done just like an object file. */
228e534f 270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
0a1b45a2 271 return false;
228e534f
AM
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
252b5132
RH
274}
275
6d5944fc 276char *
217aa764 277bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
278{
279 Elf_Internal_Shdr **i_shdrp;
f075ee0c 280 bfd_byte *shstrtab = NULL;
dc810e39
AM
281 file_ptr offset;
282 bfd_size_type shstrtabsize;
252b5132
RH
283
284 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
285 if (i_shdrp == 0
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
f075ee0c 288 return NULL;
252b5132 289
f075ee0c 290 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
291 if (shstrtab == NULL)
292 {
c044fabd 293 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
296
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
3471d59d 299 if (shstrtabsize + 1 <= 1
06614111 300 || bfd_seek (abfd, offset, SEEK_SET) != 0
2bb3687b
AM
301 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
302 shstrtabsize)) == NULL)
303 {
3471d59d
CC
304 /* Once we've failed to read it, make sure we don't keep
305 trying. Otherwise, we'll keep allocating space for
306 the string table over and over. */
307 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
308 }
309 else
310 shstrtab[shstrtabsize] = '\0';
217aa764 311 i_shdrp[shindex]->contents = shstrtab;
252b5132 312 }
f075ee0c 313 return (char *) shstrtab;
252b5132
RH
314}
315
316char *
217aa764
AM
317bfd_elf_string_from_elf_section (bfd *abfd,
318 unsigned int shindex,
319 unsigned int strindex)
252b5132
RH
320{
321 Elf_Internal_Shdr *hdr;
322
323 if (strindex == 0)
324 return "";
325
74f2e02b
AM
326 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
327 return NULL;
328
252b5132
RH
329 hdr = elf_elfsections (abfd)[shindex];
330
06614111
NC
331 if (hdr->contents == NULL)
332 {
333 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
334 {
335 /* PR 17512: file: f057ec89. */
695344c0 336 /* xgettext:c-format */
871b3ab2 337 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 338 " a non-string section (number %d)"),
06614111
NC
339 abfd, shindex);
340 return NULL;
341 }
b1fa9dd6 342
06614111
NC
343 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
344 return NULL;
345 }
eed5def8
NC
346 else
347 {
348 /* PR 24273: The string section's contents may have already
349 been loaded elsewhere, eg because a corrupt file has the
350 string section index in the ELF header pointing at a group
351 section. So be paranoid, and test that the last byte of
352 the section is zero. */
353 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
354 return NULL;
355 }
252b5132
RH
356
357 if (strindex >= hdr->sh_size)
358 {
1b3a8575 359 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 360 _bfd_error_handler
695344c0 361 /* xgettext:c-format */
2dcf00ce
AM
362 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
363 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 364 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 365 ? ".shstrtab"
1b3a8575 366 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 367 return NULL;
252b5132
RH
368 }
369
370 return ((char *) hdr->contents) + strindex;
371}
372
6cdc0ccc
AM
373/* Read and convert symbols to internal format.
374 SYMCOUNT specifies the number of symbols to read, starting from
375 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
376 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
377 symbols, and symbol section index extensions, respectively.
378 Returns a pointer to the internal symbol buffer (malloced if necessary)
379 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
380
381Elf_Internal_Sym *
217aa764
AM
382bfd_elf_get_elf_syms (bfd *ibfd,
383 Elf_Internal_Shdr *symtab_hdr,
384 size_t symcount,
385 size_t symoffset,
386 Elf_Internal_Sym *intsym_buf,
387 void *extsym_buf,
388 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
389{
390 Elf_Internal_Shdr *shndx_hdr;
217aa764 391 void *alloc_ext;
df622259 392 const bfd_byte *esym;
6cdc0ccc
AM
393 Elf_External_Sym_Shndx *alloc_extshndx;
394 Elf_External_Sym_Shndx *shndx;
4dd07732 395 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
396 Elf_Internal_Sym *isym;
397 Elf_Internal_Sym *isymend;
9c5bfbb7 398 const struct elf_backend_data *bed;
6cdc0ccc 399 size_t extsym_size;
1f4361a7 400 size_t amt;
6cdc0ccc
AM
401 file_ptr pos;
402
e44a2c9c
AM
403 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
404 abort ();
405
6cdc0ccc
AM
406 if (symcount == 0)
407 return intsym_buf;
408
409 /* Normal syms might have section extension entries. */
410 shndx_hdr = NULL;
6a40cf0c
NC
411 if (elf_symtab_shndx_list (ibfd) != NULL)
412 {
413 elf_section_list * entry;
414 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
415
416 /* Find an index section that is linked to this symtab section. */
417 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
418 {
419 /* PR 20063. */
420 if (entry->hdr.sh_link >= elf_numsections (ibfd))
421 continue;
422
423 if (sections[entry->hdr.sh_link] == symtab_hdr)
424 {
425 shndx_hdr = & entry->hdr;
426 break;
427 };
428 }
6a40cf0c
NC
429
430 if (shndx_hdr == NULL)
431 {
432 if (symtab_hdr == & elf_symtab_hdr (ibfd))
433 /* Not really accurate, but this was how the old code used to work. */
434 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
435 /* Otherwise we do nothing. The assumption is that
436 the index table will not be needed. */
437 }
438 }
6cdc0ccc
AM
439
440 /* Read the symbols. */
441 alloc_ext = NULL;
442 alloc_extshndx = NULL;
4dd07732 443 alloc_intsym = NULL;
6cdc0ccc
AM
444 bed = get_elf_backend_data (ibfd);
445 extsym_size = bed->s->sizeof_sym;
1f4361a7
AM
446 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
447 {
448 bfd_set_error (bfd_error_file_too_big);
449 intsym_buf = NULL;
450 goto out;
451 }
6cdc0ccc
AM
452 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
453 if (extsym_buf == NULL)
454 {
1f4361a7 455 alloc_ext = bfd_malloc (amt);
6cdc0ccc
AM
456 extsym_buf = alloc_ext;
457 }
458 if (extsym_buf == NULL
459 || bfd_seek (ibfd, pos, SEEK_SET) != 0
460 || bfd_bread (extsym_buf, amt, ibfd) != amt)
461 {
462 intsym_buf = NULL;
463 goto out;
464 }
465
466 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
467 extshndx_buf = NULL;
468 else
469 {
1f4361a7
AM
470 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
471 {
472 bfd_set_error (bfd_error_file_too_big);
473 intsym_buf = NULL;
474 goto out;
475 }
6cdc0ccc
AM
476 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477 if (extshndx_buf == NULL)
478 {
1f4361a7 479 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
6cdc0ccc
AM
480 extshndx_buf = alloc_extshndx;
481 }
482 if (extshndx_buf == NULL
483 || bfd_seek (ibfd, pos, SEEK_SET) != 0
484 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
485 {
486 intsym_buf = NULL;
487 goto out;
488 }
489 }
490
491 if (intsym_buf == NULL)
492 {
1f4361a7
AM
493 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
494 {
495 bfd_set_error (bfd_error_file_too_big);
496 goto out;
497 }
498 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
4dd07732 499 intsym_buf = alloc_intsym;
6cdc0ccc
AM
500 if (intsym_buf == NULL)
501 goto out;
502 }
503
504 /* Convert the symbols to internal form. */
505 isymend = intsym_buf + symcount;
a50b1753 506 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 507 shndx = extshndx_buf;
6cdc0ccc
AM
508 isym < isymend;
509 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
510 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
511 {
512 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 513 /* xgettext:c-format */
871b3ab2 514 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 515 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 516 ibfd, (unsigned long) symoffset);
c9594989 517 free (alloc_intsym);
8384fb8f
AM
518 intsym_buf = NULL;
519 goto out;
520 }
6cdc0ccc
AM
521
522 out:
c9594989
AM
523 free (alloc_ext);
524 free (alloc_extshndx);
6cdc0ccc
AM
525
526 return intsym_buf;
527}
528
5cab59f6
AM
529/* Look up a symbol name. */
530const char *
be8dd2ca
AM
531bfd_elf_sym_name (bfd *abfd,
532 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
533 Elf_Internal_Sym *isym,
534 asection *sym_sec)
5cab59f6 535{
26c61ae5 536 const char *name;
5cab59f6 537 unsigned int iname = isym->st_name;
be8dd2ca 538 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 539
138f35cc
JJ
540 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
541 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 542 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
543 {
544 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
545 shindex = elf_elfheader (abfd)->e_shstrndx;
546 }
547
26c61ae5
L
548 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
549 if (name == NULL)
550 name = "(null)";
551 else if (sym_sec && *name == '\0')
fd361982 552 name = bfd_section_name (sym_sec);
26c61ae5
L
553
554 return name;
5cab59f6
AM
555}
556
dbb410c3
AM
557/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
558 sections. The first element is the flags, the rest are section
559 pointers. */
560
561typedef union elf_internal_group {
562 Elf_Internal_Shdr *shdr;
563 unsigned int flags;
564} Elf_Internal_Group;
565
b885599b
AM
566/* Return the name of the group signature symbol. Why isn't the
567 signature just a string? */
568
569static const char *
217aa764 570group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 571{
9dce4196 572 Elf_Internal_Shdr *hdr;
9dce4196
AM
573 unsigned char esym[sizeof (Elf64_External_Sym)];
574 Elf_External_Sym_Shndx eshndx;
575 Elf_Internal_Sym isym;
b885599b 576
13792e9d
L
577 /* First we need to ensure the symbol table is available. Make sure
578 that it is a symbol table section. */
4fbb74a6
AM
579 if (ghdr->sh_link >= elf_numsections (abfd))
580 return NULL;
13792e9d
L
581 hdr = elf_elfsections (abfd) [ghdr->sh_link];
582 if (hdr->sh_type != SHT_SYMTAB
583 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
584 return NULL;
585
9dce4196
AM
586 /* Go read the symbol. */
587 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
588 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
589 &isym, esym, &eshndx) == NULL)
b885599b 590 return NULL;
9dce4196 591
26c61ae5 592 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
593}
594
dbb410c3
AM
595/* Set next_in_group list pointer, and group name for NEWSECT. */
596
0a1b45a2 597static bool
217aa764 598setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
599{
600 unsigned int num_group = elf_tdata (abfd)->num_group;
601
602 /* If num_group is zero, read in all SHT_GROUP sections. The count
603 is set to -1 if there are no SHT_GROUP sections. */
604 if (num_group == 0)
605 {
606 unsigned int i, shnum;
607
608 /* First count the number of groups. If we have a SHT_GROUP
609 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 610 shnum = elf_numsections (abfd);
dbb410c3 611 num_group = 0;
08a40648 612
44534af3 613#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 614 ( (shdr)->sh_type == SHT_GROUP \
44534af3 615 && (shdr)->sh_size >= minsize \
1783205a
NC
616 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
617 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 618
dbb410c3
AM
619 for (i = 0; i < shnum; i++)
620 {
621 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 622
44534af3 623 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
624 num_group += 1;
625 }
626
627 if (num_group == 0)
20dbb49d
L
628 {
629 num_group = (unsigned) -1;
630 elf_tdata (abfd)->num_group = num_group;
ce497010 631 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
632 }
633 else
dbb410c3
AM
634 {
635 /* We keep a list of elf section headers for group sections,
636 so we can find them quickly. */
1f4361a7 637 size_t amt;
d0fb9a8d 638
20dbb49d 639 elf_tdata (abfd)->num_group = num_group;
1f4361a7
AM
640 amt = num_group * sizeof (Elf_Internal_Shdr *);
641 elf_tdata (abfd)->group_sect_ptr
642 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
dbb410c3 643 if (elf_tdata (abfd)->group_sect_ptr == NULL)
0a1b45a2 644 return false;
dbb410c3 645 num_group = 0;
ce497010 646
dbb410c3
AM
647 for (i = 0; i < shnum; i++)
648 {
649 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 650
44534af3 651 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 652 {
973ffd63 653 unsigned char *src;
dbb410c3
AM
654 Elf_Internal_Group *dest;
655
07d6d2b8
AM
656 /* Make sure the group section has a BFD section
657 attached to it. */
658 if (!bfd_section_from_shdr (abfd, i))
0a1b45a2 659 return false;
07d6d2b8 660
dbb410c3
AM
661 /* Add to list of sections. */
662 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
663 num_group += 1;
664
665 /* Read the raw contents. */
1f4361a7
AM
666 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
667 shdr->contents = NULL;
668 if (_bfd_mul_overflow (shdr->sh_size,
669 sizeof (*dest) / 4, &amt)
1f4361a7 670 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
671 || !(shdr->contents
672 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
493a3386
NC
673 {
674 _bfd_error_handler
695344c0 675 /* xgettext:c-format */
871b3ab2 676 (_("%pB: invalid size field in group section"
2dcf00ce
AM
677 " header: %#" PRIx64 ""),
678 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
679 bfd_set_error (bfd_error_bad_value);
680 -- num_group;
493a3386
NC
681 continue;
682 }
708d7d0d 683
dbb410c3
AM
684 /* Translate raw contents, a flag word followed by an
685 array of elf section indices all in target byte order,
686 to the flag word followed by an array of elf section
687 pointers. */
688 src = shdr->contents + shdr->sh_size;
689 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 690
dbb410c3
AM
691 while (1)
692 {
693 unsigned int idx;
694
695 src -= 4;
696 --dest;
697 idx = H_GET_32 (abfd, src);
698 if (src == shdr->contents)
699 {
327301a4 700 dest->shdr = NULL;
dbb410c3 701 dest->flags = idx;
b885599b
AM
702 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
703 shdr->bfd_section->flags
704 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
705 break;
706 }
4bba0fb1 707 if (idx < shnum)
bae363f1
L
708 {
709 dest->shdr = elf_elfsections (abfd)[idx];
710 /* PR binutils/23199: All sections in a
711 section group should be marked with
712 SHF_GROUP. But some tools generate
713 broken objects without SHF_GROUP. Fix
714 them up here. */
715 dest->shdr->sh_flags |= SHF_GROUP;
716 }
4bba0fb1
AM
717 if (idx >= shnum
718 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 719 {
4eca0228 720 _bfd_error_handler
4bba0fb1
AM
721 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
722 abfd, i);
723 dest->shdr = NULL;
dbb410c3 724 }
dbb410c3
AM
725 }
726 }
727 }
493a3386
NC
728
729 /* PR 17510: Corrupt binaries might contain invalid groups. */
730 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
731 {
732 elf_tdata (abfd)->num_group = num_group;
733
734 /* If all groups are invalid then fail. */
735 if (num_group == 0)
736 {
737 elf_tdata (abfd)->group_sect_ptr = NULL;
738 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 739 _bfd_error_handler
871b3ab2 740 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
741 bfd_set_error (bfd_error_bad_value);
742 }
743 }
dbb410c3
AM
744 }
745 }
746
747 if (num_group != (unsigned) -1)
748 {
564e11c9
JW
749 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
750 unsigned int j;
dbb410c3 751
564e11c9 752 for (j = 0; j < num_group; j++)
dbb410c3 753 {
564e11c9
JW
754 /* Begin search from previous found group. */
755 unsigned i = (j + search_offset) % num_group;
756
dbb410c3 757 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 758 Elf_Internal_Group *idx;
0c54f692 759 bfd_size_type n_elt;
ce497010
NC
760
761 if (shdr == NULL)
762 continue;
763
764 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
765 if (idx == NULL || shdr->sh_size < 4)
766 {
767 /* See PR 21957 for a reproducer. */
768 /* xgettext:c-format */
871b3ab2 769 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
770 abfd, shdr->bfd_section);
771 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
772 bfd_set_error (bfd_error_bad_value);
0a1b45a2 773 return false;
0c54f692 774 }
ce497010 775 n_elt = shdr->sh_size / 4;
dbb410c3
AM
776
777 /* Look through this group's sections to see if current
778 section is a member. */
779 while (--n_elt != 0)
780 if ((++idx)->shdr == hdr)
781 {
e0e8c97f 782 asection *s = NULL;
dbb410c3
AM
783
784 /* We are a member of this group. Go looking through
785 other members to see if any others are linked via
786 next_in_group. */
787 idx = (Elf_Internal_Group *) shdr->contents;
788 n_elt = shdr->sh_size / 4;
789 while (--n_elt != 0)
4bba0fb1
AM
790 if ((++idx)->shdr != NULL
791 && (s = idx->shdr->bfd_section) != NULL
945906ff 792 && elf_next_in_group (s) != NULL)
dbb410c3
AM
793 break;
794 if (n_elt != 0)
795 {
dbb410c3
AM
796 /* Snarf the group name from other member, and
797 insert current section in circular list. */
945906ff
AM
798 elf_group_name (newsect) = elf_group_name (s);
799 elf_next_in_group (newsect) = elf_next_in_group (s);
800 elf_next_in_group (s) = newsect;
dbb410c3
AM
801 }
802 else
803 {
dbb410c3
AM
804 const char *gname;
805
b885599b
AM
806 gname = group_signature (abfd, shdr);
807 if (gname == NULL)
0a1b45a2 808 return false;
945906ff 809 elf_group_name (newsect) = gname;
dbb410c3
AM
810
811 /* Start a circular list with one element. */
945906ff 812 elf_next_in_group (newsect) = newsect;
dbb410c3 813 }
b885599b 814
9dce4196
AM
815 /* If the group section has been created, point to the
816 new member. */
dbb410c3 817 if (shdr->bfd_section != NULL)
945906ff 818 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 819
564e11c9
JW
820 elf_tdata (abfd)->group_search_offset = i;
821 j = num_group - 1;
dbb410c3
AM
822 break;
823 }
824 }
825 }
826
945906ff 827 if (elf_group_name (newsect) == NULL)
dbb410c3 828 {
695344c0 829 /* xgettext:c-format */
871b3ab2 830 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 831 abfd, newsect);
0a1b45a2 832 return false;
dbb410c3 833 }
0a1b45a2 834 return true;
dbb410c3
AM
835}
836
0a1b45a2 837bool
dd863624 838_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
839{
840 unsigned int i;
841 unsigned int num_group = elf_tdata (abfd)->num_group;
0a1b45a2 842 bool result = true;
dd863624
L
843 asection *s;
844
845 /* Process SHF_LINK_ORDER. */
846 for (s = abfd->sections; s != NULL; s = s->next)
847 {
848 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
849 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
850 {
851 unsigned int elfsec = this_hdr->sh_link;
b71702f1
NC
852 /* An sh_link value of 0 is now allowed. It indicates that linked
853 to section has already been discarded, but that the current
854 section has been retained for some other reason. This linking
855 section is still a candidate for later garbage collection
856 however. */
dd863624
L
857 if (elfsec == 0)
858 {
b71702f1 859 elf_linked_to_section (s) = NULL;
dd863624
L
860 }
861 else
862 {
91d6fa6a 863 asection *linksec = NULL;
25bbc984 864
4fbb74a6
AM
865 if (elfsec < elf_numsections (abfd))
866 {
867 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 868 linksec = this_hdr->bfd_section;
4fbb74a6 869 }
25bbc984
L
870
871 /* PR 1991, 2008:
872 Some strip/objcopy may leave an incorrect value in
873 sh_link. We don't want to proceed. */
91d6fa6a 874 if (linksec == NULL)
25bbc984 875 {
4eca0228 876 _bfd_error_handler
695344c0 877 /* xgettext:c-format */
871b3ab2 878 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 879 s->owner, elfsec, s);
0a1b45a2 880 result = false;
25bbc984
L
881 }
882
91d6fa6a 883 elf_linked_to_section (s) = linksec;
dd863624
L
884 }
885 }
53720c49
AM
886 else if (this_hdr->sh_type == SHT_GROUP
887 && elf_next_in_group (s) == NULL)
888 {
4eca0228 889 _bfd_error_handler
695344c0 890 /* xgettext:c-format */
871b3ab2 891 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49 892 abfd, elf_section_data (s)->this_idx);
0a1b45a2 893 result = false;
53720c49 894 }
dd863624 895 }
3d7f7666 896
dd863624 897 /* Process section groups. */
3d7f7666
L
898 if (num_group == (unsigned) -1)
899 return result;
900
901 for (i = 0; i < num_group; i++)
902 {
903 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
904 Elf_Internal_Group *idx;
905 unsigned int n_elt;
3d7f7666 906
4b0e8a5f
NC
907 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
908 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
909 {
4eca0228 910 _bfd_error_handler
695344c0 911 /* xgettext:c-format */
871b3ab2 912 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f 913 abfd, i);
0a1b45a2 914 result = false;
4b0e8a5f
NC
915 continue;
916 }
917
918 idx = (Elf_Internal_Group *) shdr->contents;
919 n_elt = shdr->sh_size / 4;
1b786873 920
3d7f7666 921 while (--n_elt != 0)
24d3e51b
NC
922 {
923 ++ idx;
924
925 if (idx->shdr == NULL)
926 continue;
927 else if (idx->shdr->bfd_section)
928 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
929 else if (idx->shdr->sh_type != SHT_RELA
930 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
931 {
932 /* There are some unknown sections in the group. */
933 _bfd_error_handler
934 /* xgettext:c-format */
871b3ab2 935 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
936 abfd,
937 idx->shdr->sh_type,
938 bfd_elf_string_from_elf_section (abfd,
939 (elf_elfheader (abfd)
940 ->e_shstrndx),
941 idx->shdr->sh_name),
942 shdr->bfd_section);
0a1b45a2 943 result = false;
24d3e51b
NC
944 }
945 }
3d7f7666 946 }
24d3e51b 947
3d7f7666
L
948 return result;
949}
950
0a1b45a2 951bool
72adc230
AM
952bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
953{
954 return elf_next_in_group (sec) != NULL;
955}
956
cb7f4b29
AM
957const char *
958bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
959{
960 if (elf_sec_group (sec) != NULL)
961 return elf_group_name (sec);
962 return NULL;
963}
964
f6fe1ccd
L
965static char *
966convert_debug_to_zdebug (bfd *abfd, const char *name)
967{
968 unsigned int len = strlen (name);
969 char *new_name = bfd_alloc (abfd, len + 2);
970 if (new_name == NULL)
971 return NULL;
972 new_name[0] = '.';
973 new_name[1] = 'z';
974 memcpy (new_name + 2, name + 1, len);
975 return new_name;
976}
977
978static char *
979convert_zdebug_to_debug (bfd *abfd, const char *name)
980{
981 unsigned int len = strlen (name);
982 char *new_name = bfd_alloc (abfd, len);
983 if (new_name == NULL)
984 return NULL;
985 new_name[0] = '.';
986 memcpy (new_name + 1, name + 2, len - 1);
987 return new_name;
988}
989
cc5277b1
ML
990/* This a copy of lto_section defined in GCC (lto-streamer.h). */
991
992struct lto_section
993{
994 int16_t major_version;
995 int16_t minor_version;
996 unsigned char slim_object;
997
998 /* Flags is a private field that is not defined publicly. */
999 uint16_t flags;
1000};
1001
252b5132
RH
1002/* Make a BFD section from an ELF section. We store a pointer to the
1003 BFD section in the bfd_section field of the header. */
1004
0a1b45a2 1005bool
217aa764
AM
1006_bfd_elf_make_section_from_shdr (bfd *abfd,
1007 Elf_Internal_Shdr *hdr,
6dc132d9
L
1008 const char *name,
1009 int shindex)
252b5132
RH
1010{
1011 asection *newsect;
1012 flagword flags;
9c5bfbb7 1013 const struct elf_backend_data *bed;
502794d4 1014 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
1015
1016 if (hdr->bfd_section != NULL)
0a1b45a2 1017 return true;
252b5132
RH
1018
1019 newsect = bfd_make_section_anyway (abfd, name);
1020 if (newsect == NULL)
0a1b45a2 1021 return false;
252b5132 1022
1829f4b2
AM
1023 hdr->bfd_section = newsect;
1024 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1025 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1026
2f89ff8d
L
1027 /* Always use the real type/flags. */
1028 elf_section_type (newsect) = hdr->sh_type;
1029 elf_section_flags (newsect) = hdr->sh_flags;
1030
252b5132
RH
1031 newsect->filepos = hdr->sh_offset;
1032
252b5132
RH
1033 flags = SEC_NO_FLAGS;
1034 if (hdr->sh_type != SHT_NOBITS)
1035 flags |= SEC_HAS_CONTENTS;
dbb410c3 1036 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1037 flags |= SEC_GROUP;
252b5132
RH
1038 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1039 {
1040 flags |= SEC_ALLOC;
1041 if (hdr->sh_type != SHT_NOBITS)
1042 flags |= SEC_LOAD;
1043 }
1044 if ((hdr->sh_flags & SHF_WRITE) == 0)
1045 flags |= SEC_READONLY;
1046 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1047 flags |= SEC_CODE;
1048 else if ((flags & SEC_LOAD) != 0)
1049 flags |= SEC_DATA;
f5fa8ca2
JJ
1050 if ((hdr->sh_flags & SHF_MERGE) != 0)
1051 {
1052 flags |= SEC_MERGE;
1053 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1054 }
84865015
NC
1055 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1056 flags |= SEC_STRINGS;
dbb410c3
AM
1057 if (hdr->sh_flags & SHF_GROUP)
1058 if (!setup_group (abfd, hdr, newsect))
0a1b45a2 1059 return false;
13ae64f3
JJ
1060 if ((hdr->sh_flags & SHF_TLS) != 0)
1061 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1062 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1063 flags |= SEC_EXCLUDE;
252b5132 1064
df3a023b
AM
1065 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1066 {
1067 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1068 but binutils as of 2019-07-23 did not set the EI_OSABI header
1069 byte. */
df3a023b
AM
1070 case ELFOSABI_GNU:
1071 case ELFOSABI_FREEBSD:
99fabbc9
JL
1072 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1073 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1074 /* Fall through */
1075 case ELFOSABI_NONE:
df3a023b
AM
1076 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1077 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1078 break;
1079 }
1080
3d2b39cf 1081 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1082 {
3d2b39cf
L
1083 /* The debugging sections appear to be recognized only by name,
1084 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1085 if (name [0] == '.')
1086 {
3f3328b8
ML
1087 if (startswith (name, ".debug")
1088 || startswith (name, ".gnu.debuglto_.debug_")
1089 || startswith (name, ".gnu.linkonce.wi.")
1090 || startswith (name, ".zdebug"))
bb294208 1091 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
3f3328b8
ML
1092 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1093 || startswith (name, ".note.gnu"))
502794d4
CE
1094 {
1095 flags |= SEC_ELF_OCTETS;
1096 opb = 1;
1097 }
3f3328b8
ML
1098 else if (startswith (name, ".line")
1099 || startswith (name, ".stab")
bb294208 1100 || strcmp (name, ".gdb_index") == 0)
3d2b39cf
L
1101 flags |= SEC_DEBUGGING;
1102 }
1103 }
252b5132 1104
502794d4
CE
1105 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1106 || !bfd_set_section_size (newsect, hdr->sh_size)
1f9b1a84
AM
1107 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1108 & -hdr->sh_addralign)))
0a1b45a2 1109 return false;
502794d4 1110
252b5132
RH
1111 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1112 only link a single copy of the section. This is used to support
1113 g++. g++ will emit each template expansion in its own section.
1114 The symbols will be defined as weak, so that multiple definitions
1115 are permitted. The GNU linker extension is to actually discard
1116 all but one of the sections. */
08dedd66 1117 if (startswith (name, ".gnu.linkonce")
b885599b 1118 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1119 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1120
8c803a2d 1121 if (!bfd_set_section_flags (newsect, flags))
0a1b45a2 1122 return false;
8c803a2d 1123
fa152c49
JW
1124 bed = get_elf_backend_data (abfd);
1125 if (bed->elf_backend_section_flags)
8c803a2d 1126 if (!bed->elf_backend_section_flags (hdr))
0a1b45a2 1127 return false;
fa152c49 1128
718175fa
JK
1129 /* We do not parse the PT_NOTE segments as we are interested even in the
1130 separate debug info files which may have the segments offsets corrupted.
1131 PT_NOTEs from the core files are currently not parsed using BFD. */
1132 if (hdr->sh_type == SHT_NOTE)
1133 {
baea7ef1 1134 bfd_byte *contents;
718175fa 1135
baea7ef1 1136 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
0a1b45a2 1137 return false;
718175fa 1138
276da9b3
L
1139 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1140 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1141 free (contents);
1142 }
1143
8c803a2d 1144 if ((newsect->flags & SEC_ALLOC) != 0)
252b5132
RH
1145 {
1146 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1147 unsigned int i, nload;
1148
1149 /* Some ELF linkers produce binaries with all the program header
1150 p_paddr fields zero. If we have such a binary with more than
1151 one PT_LOAD header, then leave the section lma equal to vma
1152 so that we don't create sections with overlapping lma. */
1153 phdr = elf_tdata (abfd)->phdr;
1154 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1155 if (phdr->p_paddr != 0)
1156 break;
1157 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1158 ++nload;
1159 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
0a1b45a2 1160 return true;
252b5132 1161
252b5132
RH
1162 phdr = elf_tdata (abfd)->phdr;
1163 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1164 {
86b2281f
AM
1165 if (((phdr->p_type == PT_LOAD
1166 && (hdr->sh_flags & SHF_TLS) == 0)
1167 || phdr->p_type == PT_TLS)
9a83a553 1168 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1169 {
8c803a2d 1170 if ((newsect->flags & SEC_LOAD) == 0)
88967714 1171 newsect->lma = (phdr->p_paddr
502794d4 1172 + hdr->sh_addr - phdr->p_vaddr) / opb;
88967714
AM
1173 else
1174 /* We used to use the same adjustment for SEC_LOAD
1175 sections, but that doesn't work if the segment
1176 is packed with code from multiple VMAs.
1177 Instead we calculate the section LMA based on
1178 the segment LMA. It is assumed that the
1179 segment will contain sections with contiguous
1180 LMAs, even if the VMAs are not. */
1181 newsect->lma = (phdr->p_paddr
502794d4 1182 + hdr->sh_offset - phdr->p_offset) / opb;
88967714
AM
1183
1184 /* With contiguous segments, we can't tell from file
1185 offsets whether a section with zero size should
1186 be placed at the end of one segment or the
1187 beginning of the next. Decide based on vaddr. */
1188 if (hdr->sh_addr >= phdr->p_vaddr
1189 && (hdr->sh_addr + hdr->sh_size
1190 <= phdr->p_vaddr + phdr->p_memsz))
1191 break;
252b5132
RH
1192 }
1193 }
1194 }
1195
4a114e3e
L
1196 /* Compress/decompress DWARF debug sections with names: .debug_* and
1197 .zdebug_*, after the section flags is set. */
8c803a2d 1198 if ((newsect->flags & SEC_DEBUGGING)
4a114e3e
L
1199 && ((name[1] == 'd' && name[6] == '_')
1200 || (name[1] == 'z' && name[7] == '_')))
1201 {
1202 enum { nothing, compress, decompress } action = nothing;
151411f8 1203 int compression_header_size;
dab394de 1204 bfd_size_type uncompressed_size;
4207142d 1205 unsigned int uncompressed_align_power;
0a1b45a2 1206 bool compressed
151411f8 1207 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1208 &compression_header_size,
4207142d
MW
1209 &uncompressed_size,
1210 &uncompressed_align_power);
151411f8 1211 if (compressed)
4a114e3e
L
1212 {
1213 /* Compressed section. Check if we should decompress. */
1214 if ((abfd->flags & BFD_DECOMPRESS))
1215 action = decompress;
1216 }
151411f8
L
1217
1218 /* Compress the uncompressed section or convert from/to .zdebug*
1219 section. Check if we should compress. */
1220 if (action == nothing)
4a114e3e 1221 {
151411f8
L
1222 if (newsect->size != 0
1223 && (abfd->flags & BFD_COMPRESS)
1224 && compression_header_size >= 0
dab394de 1225 && uncompressed_size > 0
151411f8
L
1226 && (!compressed
1227 || ((compression_header_size > 0)
1228 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1229 action = compress;
151411f8 1230 else
0a1b45a2 1231 return true;
4a114e3e
L
1232 }
1233
151411f8 1234 if (action == compress)
4a114e3e 1235 {
4a114e3e
L
1236 if (!bfd_init_section_compress_status (abfd, newsect))
1237 {
4eca0228 1238 _bfd_error_handler
695344c0 1239 /* xgettext:c-format */
871b3ab2 1240 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e 1241 abfd, name);
0a1b45a2 1242 return false;
4a114e3e 1243 }
151411f8
L
1244 }
1245 else
1246 {
4a114e3e
L
1247 if (!bfd_init_section_decompress_status (abfd, newsect))
1248 {
4eca0228 1249 _bfd_error_handler
695344c0 1250 /* xgettext:c-format */
871b3ab2 1251 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e 1252 abfd, name);
0a1b45a2 1253 return false;
4a114e3e 1254 }
151411f8
L
1255 }
1256
f6fe1ccd 1257 if (abfd->is_linker_input)
151411f8 1258 {
f6fe1ccd
L
1259 if (name[1] == 'z'
1260 && (action == decompress
1261 || (action == compress
1262 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1263 {
f6fe1ccd
L
1264 /* Convert section name from .zdebug_* to .debug_* so
1265 that linker will consider this section as a debug
1266 section. */
1267 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8 1268 if (new_name == NULL)
0a1b45a2 1269 return false;
fd361982 1270 bfd_rename_section (newsect, new_name);
151411f8 1271 }
4a114e3e 1272 }
f6fe1ccd
L
1273 else
1274 /* For objdump, don't rename the section. For objcopy, delay
1275 section rename to elf_fake_sections. */
1276 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1277 }
1278
cc5277b1
ML
1279 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1280 section. */
3f3328b8 1281 if (startswith (name, ".gnu.lto_.lto."))
cc5277b1
ML
1282 {
1283 struct lto_section lsection;
1284 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1285 sizeof (struct lto_section)))
1286 abfd->lto_slim_object = lsection.slim_object;
1287 }
1288
0a1b45a2 1289 return true;
252b5132
RH
1290}
1291
84865015
NC
1292const char *const bfd_elf_section_type_names[] =
1293{
252b5132
RH
1294 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1295 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1296 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1297};
1298
1049f94e 1299/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1300 output, and the reloc is against an external symbol, and nothing
1301 has given us any additional addend, the resulting reloc will also
1302 be against the same symbol. In such a case, we don't want to
1303 change anything about the way the reloc is handled, since it will
1304 all be done at final link time. Rather than put special case code
1305 into bfd_perform_relocation, all the reloc types use this howto
2dfa8341 1306 function, or should call this function for relocatable output. */
252b5132 1307
252b5132 1308bfd_reloc_status_type
217aa764
AM
1309bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1310 arelent *reloc_entry,
1311 asymbol *symbol,
1312 void *data ATTRIBUTE_UNUSED,
1313 asection *input_section,
1314 bfd *output_bfd,
1315 char **error_message ATTRIBUTE_UNUSED)
1316{
1317 if (output_bfd != NULL
252b5132
RH
1318 && (symbol->flags & BSF_SECTION_SYM) == 0
1319 && (! reloc_entry->howto->partial_inplace
1320 || reloc_entry->addend == 0))
1321 {
1322 reloc_entry->address += input_section->output_offset;
1323 return bfd_reloc_ok;
1324 }
1325
2dfa8341
AM
1326 /* In some cases the relocation should be treated as output section
1327 relative, as when linking ELF DWARF into PE COFF. Many ELF
1328 targets lack section relative relocations and instead use
1329 ordinary absolute relocations for references between DWARF
1330 sections. That is arguably a bug in those targets but it happens
1331 to work for the usual case of linking to non-loaded ELF debug
1332 sections with VMAs forced to zero. PE COFF on the other hand
1333 doesn't allow a section VMA of zero. */
1334 if (output_bfd == NULL
1335 && !reloc_entry->howto->pc_relative
1336 && (symbol->section->flags & SEC_DEBUGGING) != 0
1337 && (input_section->flags & SEC_DEBUGGING) != 0)
1338 reloc_entry->addend -= symbol->section->output_section->vma;
1339
252b5132
RH
1340 return bfd_reloc_continue;
1341}
1342\f
84865015
NC
1343/* Returns TRUE if section A matches section B.
1344 Names, addresses and links may be different, but everything else
1345 should be the same. */
1346
0a1b45a2 1347static bool
5522f910
NC
1348section_match (const Elf_Internal_Shdr * a,
1349 const Elf_Internal_Shdr * b)
84865015 1350{
ac85e67c
AM
1351 if (a->sh_type != b->sh_type
1352 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1353 || a->sh_addralign != b->sh_addralign
1354 || a->sh_entsize != b->sh_entsize)
0a1b45a2 1355 return false;
ac85e67c
AM
1356 if (a->sh_type == SHT_SYMTAB
1357 || a->sh_type == SHT_STRTAB)
0a1b45a2 1358 return true;
ac85e67c 1359 return a->sh_size == b->sh_size;
84865015
NC
1360}
1361
1362/* Find a section in OBFD that has the same characteristics
1363 as IHEADER. Return the index of this section or SHN_UNDEF if
1364 none can be found. Check's section HINT first, as this is likely
1365 to be the correct section. */
1366
1367static unsigned int
5cc4ca83
ST
1368find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1369 const unsigned int hint)
84865015
NC
1370{
1371 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1372 unsigned int i;
1373
a55c9876
NC
1374 BFD_ASSERT (iheader != NULL);
1375
1376 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1377 if (hint < elf_numsections (obfd)
1378 && oheaders[hint] != NULL
a55c9876 1379 && section_match (oheaders[hint], iheader))
84865015
NC
1380 return hint;
1381
1382 for (i = 1; i < elf_numsections (obfd); i++)
1383 {
1384 Elf_Internal_Shdr * oheader = oheaders[i];
1385
a55c9876
NC
1386 if (oheader == NULL)
1387 continue;
84865015
NC
1388 if (section_match (oheader, iheader))
1389 /* FIXME: Do we care if there is a potential for
1390 multiple matches ? */
1391 return i;
1392 }
1393
1394 return SHN_UNDEF;
1395}
1396
5522f910
NC
1397/* PR 19938: Attempt to set the ELF section header fields of an OS or
1398 Processor specific section, based upon a matching input section.
1399 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1400
0a1b45a2 1401static bool
5522f910
NC
1402copy_special_section_fields (const bfd *ibfd,
1403 bfd *obfd,
1404 const Elf_Internal_Shdr *iheader,
1405 Elf_Internal_Shdr *oheader,
1406 const unsigned int secnum)
1407{
1408 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1409 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
0a1b45a2 1410 bool changed = false;
5522f910
NC
1411 unsigned int sh_link;
1412
1413 if (oheader->sh_type == SHT_NOBITS)
1414 {
1415 /* This is a feature for objcopy --only-keep-debug:
1416 When a section's type is changed to NOBITS, we preserve
1417 the sh_link and sh_info fields so that they can be
1418 matched up with the original.
1419
1420 Note: Strictly speaking these assignments are wrong.
1421 The sh_link and sh_info fields should point to the
1422 relevent sections in the output BFD, which may not be in
1423 the same location as they were in the input BFD. But
1424 the whole point of this action is to preserve the
1425 original values of the sh_link and sh_info fields, so
1426 that they can be matched up with the section headers in
1427 the original file. So strictly speaking we may be
1428 creating an invalid ELF file, but it is only for a file
1429 that just contains debug info and only for sections
1430 without any contents. */
1431 if (oheader->sh_link == 0)
1432 oheader->sh_link = iheader->sh_link;
1433 if (oheader->sh_info == 0)
1434 oheader->sh_info = iheader->sh_info;
0a1b45a2 1435 return true;
5522f910
NC
1436 }
1437
1438 /* Allow the target a chance to decide how these fields should be set. */
a859124d
AM
1439 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1440 iheader, oheader))
0a1b45a2 1441 return true;
5522f910
NC
1442
1443 /* We have an iheader which might match oheader, and which has non-zero
1444 sh_info and/or sh_link fields. Attempt to follow those links and find
1445 the section in the output bfd which corresponds to the linked section
1446 in the input bfd. */
1447 if (iheader->sh_link != SHN_UNDEF)
1448 {
4f3ca05b
NC
1449 /* See PR 20931 for a reproducer. */
1450 if (iheader->sh_link >= elf_numsections (ibfd))
1451 {
76cfced5 1452 _bfd_error_handler
4f3ca05b 1453 /* xgettext:c-format */
9793eb77 1454 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b 1455 ibfd, iheader->sh_link, secnum);
0a1b45a2 1456 return false;
4f3ca05b
NC
1457 }
1458
5522f910
NC
1459 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1460 if (sh_link != SHN_UNDEF)
1461 {
1462 oheader->sh_link = sh_link;
0a1b45a2 1463 changed = true;
5522f910
NC
1464 }
1465 else
1466 /* FIXME: Should we install iheader->sh_link
1467 if we could not find a match ? */
76cfced5 1468 _bfd_error_handler
695344c0 1469 /* xgettext:c-format */
9793eb77 1470 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1471 }
1472
1473 if (iheader->sh_info)
1474 {
1475 /* The sh_info field can hold arbitrary information, but if the
1476 SHF_LINK_INFO flag is set then it should be interpreted as a
1477 section index. */
1478 if (iheader->sh_flags & SHF_INFO_LINK)
1479 {
1480 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1481 iheader->sh_info);
1482 if (sh_link != SHN_UNDEF)
1483 oheader->sh_flags |= SHF_INFO_LINK;
1484 }
1485 else
1486 /* No idea what it means - just copy it. */
1487 sh_link = iheader->sh_info;
1488
1489 if (sh_link != SHN_UNDEF)
1490 {
1491 oheader->sh_info = sh_link;
0a1b45a2 1492 changed = true;
5522f910
NC
1493 }
1494 else
76cfced5 1495 _bfd_error_handler
695344c0 1496 /* xgettext:c-format */
9793eb77 1497 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1498 }
1499
1500 return changed;
1501}
07d6d2b8 1502
0ac4564e
L
1503/* Copy the program header and other data from one object module to
1504 another. */
252b5132 1505
0a1b45a2 1506bool
217aa764 1507_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1508{
5522f910
NC
1509 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1510 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1511 const struct elf_backend_data *bed;
84865015
NC
1512 unsigned int i;
1513
2d502050 1514 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1515 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 1516 return true;
2d502050 1517
57b828ef
L
1518 if (!elf_flags_init (obfd))
1519 {
1520 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
0a1b45a2 1521 elf_flags_init (obfd) = true;
57b828ef 1522 }
2d502050 1523
0ac4564e 1524 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1525
1526 /* Also copy the EI_OSABI field. */
1527 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1528 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1529
5522f910
NC
1530 /* If set, copy the EI_ABIVERSION field. */
1531 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1532 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1533 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1534
104d59d1
JM
1535 /* Copy object attributes. */
1536 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1537
84865015 1538 if (iheaders == NULL || oheaders == NULL)
0a1b45a2 1539 return true;
63b9bbb7 1540
5522f910
NC
1541 bed = get_elf_backend_data (obfd);
1542
1543 /* Possibly copy other fields in the section header. */
84865015 1544 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1545 {
84865015
NC
1546 unsigned int j;
1547 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1548
5522f910
NC
1549 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1550 because of a special case need for generating separate debug info
1551 files. See below for more details. */
84865015
NC
1552 if (oheader == NULL
1553 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1554 && oheader->sh_type < SHT_LOOS))
1555 continue;
1556
1557 /* Ignore empty sections, and sections whose
1558 fields have already been initialised. */
1559 if (oheader->sh_size == 0
84865015
NC
1560 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1561 continue;
63b9bbb7 1562
84865015 1563 /* Scan for the matching section in the input bfd.
5522f910
NC
1564 First we try for a direct mapping between the input and output sections. */
1565 for (j = 1; j < elf_numsections (ibfd); j++)
1566 {
1567 const Elf_Internal_Shdr * iheader = iheaders[j];
1568
1569 if (iheader == NULL)
1570 continue;
1571
1572 if (oheader->bfd_section != NULL
1573 && iheader->bfd_section != NULL
1574 && iheader->bfd_section->output_section != NULL
1575 && iheader->bfd_section->output_section == oheader->bfd_section)
1576 {
1577 /* We have found a connection from the input section to the
1578 output section. Attempt to copy the header fields. If
1579 this fails then do not try any further sections - there
1580 should only be a one-to-one mapping between input and output. */
1581 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1582 j = elf_numsections (ibfd);
1583 break;
1584 }
1585 }
1586
1587 if (j < elf_numsections (ibfd))
1588 continue;
1589
1590 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1591 Unfortunately we cannot compare names as the output string table
1592 is empty, so instead we check size, address and type. */
1593 for (j = 1; j < elf_numsections (ibfd); j++)
1594 {
5522f910 1595 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1596
5522f910
NC
1597 if (iheader == NULL)
1598 continue;
1599
1600 /* Try matching fields in the input section's header.
1601 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1602 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1603 input type. */
1604 if ((oheader->sh_type == SHT_NOBITS
1605 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1606 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1607 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1608 && iheader->sh_addralign == oheader->sh_addralign
1609 && iheader->sh_entsize == oheader->sh_entsize
1610 && iheader->sh_size == oheader->sh_size
1611 && iheader->sh_addr == oheader->sh_addr
1612 && (iheader->sh_info != oheader->sh_info
1613 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1614 {
5522f910
NC
1615 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1616 break;
63b9bbb7
NC
1617 }
1618 }
5522f910
NC
1619
1620 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1621 {
1622 /* Final attempt. Call the backend copy function
1623 with a NULL input section. */
a859124d
AM
1624 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1625 NULL, oheader);
5522f910 1626 }
63b9bbb7
NC
1627 }
1628
0a1b45a2 1629 return true;
2d502050
L
1630}
1631
cedc298e
L
1632static const char *
1633get_segment_type (unsigned int p_type)
1634{
1635 const char *pt;
1636 switch (p_type)
1637 {
1638 case PT_NULL: pt = "NULL"; break;
1639 case PT_LOAD: pt = "LOAD"; break;
1640 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1641 case PT_INTERP: pt = "INTERP"; break;
1642 case PT_NOTE: pt = "NOTE"; break;
1643 case PT_SHLIB: pt = "SHLIB"; break;
1644 case PT_PHDR: pt = "PHDR"; break;
1645 case PT_TLS: pt = "TLS"; break;
1646 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1647 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1648 case PT_GNU_RELRO: pt = "RELRO"; break;
1649 default: pt = NULL; break;
1650 }
1651 return pt;
1652}
1653
f0b79d91
L
1654/* Print out the program headers. */
1655
0a1b45a2 1656bool
217aa764 1657_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1658{
a50b1753 1659 FILE *f = (FILE *) farg;
252b5132
RH
1660 Elf_Internal_Phdr *p;
1661 asection *s;
1662 bfd_byte *dynbuf = NULL;
1663
1664 p = elf_tdata (abfd)->phdr;
1665 if (p != NULL)
1666 {
1667 unsigned int i, c;
1668
1669 fprintf (f, _("\nProgram Header:\n"));
1670 c = elf_elfheader (abfd)->e_phnum;
1671 for (i = 0; i < c; i++, p++)
1672 {
cedc298e 1673 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1674 char buf[20];
1675
cedc298e 1676 if (pt == NULL)
252b5132 1677 {
cedc298e
L
1678 sprintf (buf, "0x%lx", p->p_type);
1679 pt = buf;
252b5132 1680 }
dc810e39 1681 fprintf (f, "%8s off 0x", pt);
60b89a18 1682 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1683 fprintf (f, " vaddr 0x");
60b89a18 1684 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1685 fprintf (f, " paddr 0x");
60b89a18 1686 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1687 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1688 fprintf (f, " filesz 0x");
60b89a18 1689 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1690 fprintf (f, " memsz 0x");
60b89a18 1691 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1692 fprintf (f, " flags %c%c%c",
1693 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1694 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1695 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1696 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1697 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1698 fprintf (f, "\n");
1699 }
1700 }
1701
1702 s = bfd_get_section_by_name (abfd, ".dynamic");
1703 if (s != NULL)
1704 {
cb33740c 1705 unsigned int elfsec;
dc810e39 1706 unsigned long shlink;
252b5132
RH
1707 bfd_byte *extdyn, *extdynend;
1708 size_t extdynsize;
217aa764 1709 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1710
1711 fprintf (f, _("\nDynamic Section:\n"));
1712
eea6121a 1713 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1714 goto error_return;
1715
1716 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1717 if (elfsec == SHN_BAD)
252b5132 1718 goto error_return;
dc810e39 1719 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1720
1721 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1722 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1723
1724 extdyn = dynbuf;
06614111
NC
1725 /* PR 17512: file: 6f427532. */
1726 if (s->size < extdynsize)
1727 goto error_return;
eea6121a 1728 extdynend = extdyn + s->size;
1036838a 1729 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1730 Fix range check. */
1036838a 1731 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1732 {
1733 Elf_Internal_Dyn dyn;
ad9563d6 1734 const char *name = "";
252b5132 1735 char ab[20];
0a1b45a2 1736 bool stringp;
ad9563d6 1737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1738
217aa764 1739 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1740
1741 if (dyn.d_tag == DT_NULL)
1742 break;
1743
0a1b45a2 1744 stringp = false;
252b5132
RH
1745 switch (dyn.d_tag)
1746 {
1747 default:
ad9563d6
CM
1748 if (bed->elf_backend_get_target_dtag)
1749 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1750
1751 if (!strcmp (name, ""))
1752 {
cd9af601 1753 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1754 name = ab;
1755 }
252b5132
RH
1756 break;
1757
0a1b45a2 1758 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
252b5132
RH
1759 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1760 case DT_PLTGOT: name = "PLTGOT"; break;
1761 case DT_HASH: name = "HASH"; break;
1762 case DT_STRTAB: name = "STRTAB"; break;
1763 case DT_SYMTAB: name = "SYMTAB"; break;
1764 case DT_RELA: name = "RELA"; break;
1765 case DT_RELASZ: name = "RELASZ"; break;
1766 case DT_RELAENT: name = "RELAENT"; break;
1767 case DT_STRSZ: name = "STRSZ"; break;
1768 case DT_SYMENT: name = "SYMENT"; break;
1769 case DT_INIT: name = "INIT"; break;
1770 case DT_FINI: name = "FINI"; break;
0a1b45a2
AM
1771 case DT_SONAME: name = "SONAME"; stringp = true; break;
1772 case DT_RPATH: name = "RPATH"; stringp = true; break;
252b5132
RH
1773 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1774 case DT_REL: name = "REL"; break;
1775 case DT_RELSZ: name = "RELSZ"; break;
1776 case DT_RELENT: name = "RELENT"; break;
dd207c13
FS
1777 case DT_RELR: name = "RELR"; break;
1778 case DT_RELRSZ: name = "RELRSZ"; break;
1779 case DT_RELRENT: name = "RELRENT"; break;
252b5132
RH
1780 case DT_PLTREL: name = "PLTREL"; break;
1781 case DT_DEBUG: name = "DEBUG"; break;
1782 case DT_TEXTREL: name = "TEXTREL"; break;
1783 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1784 case DT_BIND_NOW: name = "BIND_NOW"; break;
1785 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1786 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1787 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1788 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
0a1b45a2 1789 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
94558834
L
1790 case DT_FLAGS: name = "FLAGS"; break;
1791 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1792 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1793 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1794 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1795 case DT_MOVEENT: name = "MOVEENT"; break;
1796 case DT_MOVESZ: name = "MOVESZ"; break;
1797 case DT_FEATURE: name = "FEATURE"; break;
1798 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1799 case DT_SYMINSZ: name = "SYMINSZ"; break;
1800 case DT_SYMINENT: name = "SYMINENT"; break;
0a1b45a2
AM
1801 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1802 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1803 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
94558834
L
1804 case DT_PLTPAD: name = "PLTPAD"; break;
1805 case DT_MOVETAB: name = "MOVETAB"; break;
1806 case DT_SYMINFO: name = "SYMINFO"; break;
1807 case DT_RELACOUNT: name = "RELACOUNT"; break;
1808 case DT_RELCOUNT: name = "RELCOUNT"; break;
1809 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1810 case DT_VERSYM: name = "VERSYM"; break;
1811 case DT_VERDEF: name = "VERDEF"; break;
1812 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1813 case DT_VERNEED: name = "VERNEED"; break;
1814 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
0a1b45a2 1815 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
94558834 1816 case DT_USED: name = "USED"; break;
0a1b45a2 1817 case DT_FILTER: name = "FILTER"; stringp = true; break;
fdc90cb4 1818 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1819 }
1820
ad9563d6 1821 fprintf (f, " %-20s ", name);
252b5132 1822 if (! stringp)
a1f3c56e
AN
1823 {
1824 fprintf (f, "0x");
1825 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1826 }
252b5132
RH
1827 else
1828 {
1829 const char *string;
dc810e39 1830 unsigned int tagv = dyn.d_un.d_val;
252b5132 1831
dc810e39 1832 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1833 if (string == NULL)
1834 goto error_return;
1835 fprintf (f, "%s", string);
1836 }
1837 fprintf (f, "\n");
1838 }
1839
1840 free (dynbuf);
1841 dynbuf = NULL;
1842 }
1843
1844 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1845 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1846 {
0a1b45a2
AM
1847 if (! _bfd_elf_slurp_version_tables (abfd, false))
1848 return false;
252b5132
RH
1849 }
1850
1851 if (elf_dynverdef (abfd) != 0)
1852 {
1853 Elf_Internal_Verdef *t;
1854
1855 fprintf (f, _("\nVersion definitions:\n"));
1856 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1857 {
1858 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1859 t->vd_flags, t->vd_hash,
1860 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1861 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1862 {
1863 Elf_Internal_Verdaux *a;
1864
1865 fprintf (f, "\t");
1866 for (a = t->vd_auxptr->vda_nextptr;
1867 a != NULL;
1868 a = a->vda_nextptr)
d0fb9a8d
JJ
1869 fprintf (f, "%s ",
1870 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1871 fprintf (f, "\n");
1872 }
1873 }
1874 }
1875
1876 if (elf_dynverref (abfd) != 0)
1877 {
1878 Elf_Internal_Verneed *t;
1879
1880 fprintf (f, _("\nVersion References:\n"));
1881 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1882 {
1883 Elf_Internal_Vernaux *a;
1884
d0fb9a8d
JJ
1885 fprintf (f, _(" required from %s:\n"),
1886 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1887 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1888 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1889 a->vna_flags, a->vna_other,
1890 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1891 }
1892 }
1893
0a1b45a2 1894 return true;
252b5132
RH
1895
1896 error_return:
c9594989 1897 free (dynbuf);
0a1b45a2 1898 return false;
252b5132
RH
1899}
1900
7e6e972f
L
1901/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1902 and return symbol version for symbol version itself. */
bb4d2ac2
L
1903
1904const char *
1081065c 1905_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
0a1b45a2
AM
1906 bool base_p,
1907 bool *hidden)
bb4d2ac2
L
1908{
1909 const char *version_string = NULL;
1910 if (elf_dynversym (abfd) != 0
1911 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1912 {
1913 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1914
1915 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1916 vernum &= VERSYM_VERSION;
1917
1918 if (vernum == 0)
1919 version_string = "";
1f6f5dba
L
1920 else if (vernum == 1
1921 && (vernum > elf_tdata (abfd)->cverdefs
1922 || (elf_tdata (abfd)->verdef[0].vd_flags
1923 == VER_FLG_BASE)))
7e6e972f 1924 version_string = base_p ? "Base" : "";
bb4d2ac2 1925 else if (vernum <= elf_tdata (abfd)->cverdefs)
7e6e972f
L
1926 {
1927 const char *nodename
1928 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
8d55d10a
AM
1929 version_string = "";
1930 if (base_p
1931 || nodename == NULL
1932 || symbol->name == NULL
1933 || strcmp (symbol->name, nodename) != 0)
1934 version_string = nodename;
7e6e972f 1935 }
bb4d2ac2
L
1936 else
1937 {
1938 Elf_Internal_Verneed *t;
1939
7a815dd5 1940 version_string = _("<corrupt>");
bb4d2ac2
L
1941 for (t = elf_tdata (abfd)->verref;
1942 t != NULL;
1943 t = t->vn_nextref)
1944 {
1945 Elf_Internal_Vernaux *a;
1946
1947 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1948 {
1949 if (a->vna_other == vernum)
1950 {
f897689c 1951 *hidden = true;
bb4d2ac2
L
1952 version_string = a->vna_nodename;
1953 break;
1954 }
1955 }
1956 }
1957 }
1958 }
1959 return version_string;
1960}
1961
252b5132
RH
1962/* Display ELF-specific fields of a symbol. */
1963
1964void
217aa764
AM
1965bfd_elf_print_symbol (bfd *abfd,
1966 void *filep,
1967 asymbol *symbol,
1968 bfd_print_symbol_type how)
252b5132 1969{
a50b1753 1970 FILE *file = (FILE *) filep;
252b5132
RH
1971 switch (how)
1972 {
1973 case bfd_print_symbol_name:
1974 fprintf (file, "%s", symbol->name);
1975 break;
1976 case bfd_print_symbol_more:
1977 fprintf (file, "elf ");
60b89a18 1978 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1979 fprintf (file, " %x", symbol->flags);
252b5132
RH
1980 break;
1981 case bfd_print_symbol_all:
1982 {
4e8a9624
AM
1983 const char *section_name;
1984 const char *name = NULL;
9c5bfbb7 1985 const struct elf_backend_data *bed;
7a13edea 1986 unsigned char st_other;
dbb410c3 1987 bfd_vma val;
bb4d2ac2 1988 const char *version_string;
0a1b45a2 1989 bool hidden;
c044fabd 1990
252b5132 1991 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1992
1993 bed = get_elf_backend_data (abfd);
1994 if (bed->elf_backend_print_symbol_all)
c044fabd 1995 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1996
1997 if (name == NULL)
1998 {
7ee38065 1999 name = symbol->name;
217aa764 2000 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
2001 }
2002
252b5132
RH
2003 fprintf (file, " %s\t", section_name);
2004 /* Print the "other" value for a symbol. For common symbols,
2005 we've already printed the size; now print the alignment.
2006 For other symbols, we have no specified alignment, and
2007 we've printed the address; now print the size. */
dcf6c779 2008 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
2009 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2010 else
2011 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2012 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
2013
2014 /* If we have version information, print it. */
60bb06bc
L
2015 version_string = _bfd_elf_get_symbol_version_string (abfd,
2016 symbol,
0a1b45a2 2017 true,
60bb06bc 2018 &hidden);
bb4d2ac2 2019 if (version_string)
252b5132 2020 {
bb4d2ac2 2021 if (!hidden)
252b5132
RH
2022 fprintf (file, " %-11s", version_string);
2023 else
2024 {
2025 int i;
2026
2027 fprintf (file, " (%s)", version_string);
2028 for (i = 10 - strlen (version_string); i > 0; --i)
2029 putc (' ', file);
2030 }
2031 }
2032
2033 /* If the st_other field is not zero, print it. */
7a13edea 2034 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2035
7a13edea
NC
2036 switch (st_other)
2037 {
2038 case 0: break;
2039 case STV_INTERNAL: fprintf (file, " .internal"); break;
2040 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2041 case STV_PROTECTED: fprintf (file, " .protected"); break;
2042 default:
2043 /* Some other non-defined flags are also present, so print
2044 everything hex. */
2045 fprintf (file, " 0x%02x", (unsigned int) st_other);
2046 }
252b5132 2047
587ff49e 2048 fprintf (file, " %s", name);
252b5132
RH
2049 }
2050 break;
2051 }
2052}
252b5132
RH
2053\f
2054/* ELF .o/exec file reading */
2055
c044fabd 2056/* Create a new bfd section from an ELF section header. */
252b5132 2057
0a1b45a2 2058bool
217aa764 2059bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2060{
4fbb74a6
AM
2061 Elf_Internal_Shdr *hdr;
2062 Elf_Internal_Ehdr *ehdr;
2063 const struct elf_backend_data *bed;
90937f86 2064 const char *name;
0a1b45a2 2065 bool ret = true;
252b5132 2066
4fbb74a6 2067 if (shindex >= elf_numsections (abfd))
0a1b45a2 2068 return false;
4fbb74a6 2069
a86c6c19
AM
2070 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2071 sh_link or sh_info. Detect this here, by refusing to load a
2072 section that we are already in the process of loading. */
2073 if (elf_tdata (abfd)->being_created[shindex])
bf67003b 2074 {
a86c6c19
AM
2075 _bfd_error_handler
2076 (_("%pB: warning: loop in section dependencies detected"), abfd);
0a1b45a2 2077 return false;
bf67003b 2078 }
0a1b45a2 2079 elf_tdata (abfd)->being_created[shindex] = true;
bf67003b 2080
4fbb74a6
AM
2081 hdr = elf_elfsections (abfd)[shindex];
2082 ehdr = elf_elfheader (abfd);
2083 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2084 hdr->sh_name);
933d961a 2085 if (name == NULL)
bf67003b 2086 goto fail;
252b5132 2087
4fbb74a6 2088 bed = get_elf_backend_data (abfd);
252b5132
RH
2089 switch (hdr->sh_type)
2090 {
2091 case SHT_NULL:
2092 /* Inactive section. Throw it away. */
bf67003b 2093 goto success;
252b5132 2094
bf67003b
NC
2095 case SHT_PROGBITS: /* Normal section with contents. */
2096 case SHT_NOBITS: /* .bss section. */
2097 case SHT_HASH: /* .hash section. */
2098 case SHT_NOTE: /* .note section. */
25e27870
L
2099 case SHT_INIT_ARRAY: /* .init_array section. */
2100 case SHT_FINI_ARRAY: /* .fini_array section. */
2101 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2102 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2103 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2104 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2105 goto success;
252b5132 2106
797fc050 2107 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2108 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2109 goto fail;
2110
cfcac11d
NC
2111 if (hdr->sh_link > elf_numsections (abfd))
2112 {
caa83f8b 2113 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2114 field set to SHN_BEFORE or SHN_AFTER. */
2115 switch (bfd_get_arch (abfd))
2116 {
caa83f8b 2117 case bfd_arch_i386:
cfcac11d
NC
2118 case bfd_arch_sparc:
2119 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2120 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2121 break;
2122 /* Otherwise fall through. */
2123 default:
bf67003b 2124 goto fail;
cfcac11d
NC
2125 }
2126 }
2127 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2128 goto fail;
cfcac11d 2129 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2130 {
2131 Elf_Internal_Shdr *dynsymhdr;
2132
2133 /* The shared libraries distributed with hpux11 have a bogus
2134 sh_link field for the ".dynamic" section. Find the
2135 string table for the ".dynsym" section instead. */
2136 if (elf_dynsymtab (abfd) != 0)
2137 {
2138 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2139 hdr->sh_link = dynsymhdr->sh_link;
2140 }
2141 else
2142 {
2143 unsigned int i, num_sec;
2144
2145 num_sec = elf_numsections (abfd);
2146 for (i = 1; i < num_sec; i++)
2147 {
2148 dynsymhdr = elf_elfsections (abfd)[i];
2149 if (dynsymhdr->sh_type == SHT_DYNSYM)
2150 {
2151 hdr->sh_link = dynsymhdr->sh_link;
2152 break;
2153 }
2154 }
2155 }
2156 }
bf67003b 2157 goto success;
797fc050 2158
bf67003b 2159 case SHT_SYMTAB: /* A symbol table. */
252b5132 2160 if (elf_onesymtab (abfd) == shindex)
bf67003b 2161 goto success;
252b5132 2162
a50b2160 2163 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2164 goto fail;
2165
3337c1e5 2166 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2167 {
2168 if (hdr->sh_size != 0)
bf67003b 2169 goto fail;
eee3b786
AM
2170 /* Some assemblers erroneously set sh_info to one with a
2171 zero sh_size. ld sees this as a global symbol count
2172 of (unsigned) -1. Fix it here. */
2173 hdr->sh_info = 0;
bf67003b 2174 goto success;
eee3b786 2175 }
bf67003b 2176
16ad13ec
NC
2177 /* PR 18854: A binary might contain more than one symbol table.
2178 Unusual, but possible. Warn, but continue. */
2179 if (elf_onesymtab (abfd) != 0)
2180 {
4eca0228 2181 _bfd_error_handler
695344c0 2182 /* xgettext:c-format */
871b3ab2 2183 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2184 " - ignoring the table in section %u"),
16ad13ec
NC
2185 abfd, shindex);
2186 goto success;
2187 }
252b5132 2188 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2189 elf_symtab_hdr (abfd) = *hdr;
2190 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2191 abfd->flags |= HAS_SYMS;
2192
2193 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2194 SHF_ALLOC is set, and this is a shared object, then we also
2195 treat this section as a BFD section. We can not base the
2196 decision purely on SHF_ALLOC, because that flag is sometimes
2197 set in a relocatable object file, which would confuse the
2198 linker. */
252b5132
RH
2199 if ((hdr->sh_flags & SHF_ALLOC) != 0
2200 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2201 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2202 shindex))
bf67003b 2203 goto fail;
252b5132 2204
1b3a8575
AM
2205 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2206 can't read symbols without that section loaded as well. It
2207 is most likely specified by the next section header. */
6a40cf0c
NC
2208 {
2209 elf_section_list * entry;
2210 unsigned int i, num_sec;
1b3a8575 2211
6a40cf0c
NC
2212 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2213 if (entry->hdr.sh_link == shindex)
2214 goto success;
2215
2216 num_sec = elf_numsections (abfd);
2217 for (i = shindex + 1; i < num_sec; i++)
2218 {
2219 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2220
2221 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2222 && hdr2->sh_link == shindex)
2223 break;
2224 }
2225
2226 if (i == num_sec)
2227 for (i = 1; i < shindex; i++)
1b3a8575
AM
2228 {
2229 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2230
1b3a8575
AM
2231 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2232 && hdr2->sh_link == shindex)
2233 break;
2234 }
6a40cf0c
NC
2235
2236 if (i != shindex)
2237 ret = bfd_section_from_shdr (abfd, i);
2238 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2239 goto success;
2240 }
252b5132 2241
bf67003b 2242 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2243 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2244 goto success;
252b5132 2245
a50b2160 2246 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2247 goto fail;
2248
eee3b786
AM
2249 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2250 {
2251 if (hdr->sh_size != 0)
bf67003b
NC
2252 goto fail;
2253
eee3b786
AM
2254 /* Some linkers erroneously set sh_info to one with a
2255 zero sh_size. ld sees this as a global symbol count
2256 of (unsigned) -1. Fix it here. */
2257 hdr->sh_info = 0;
bf67003b 2258 goto success;
eee3b786 2259 }
bf67003b 2260
16ad13ec
NC
2261 /* PR 18854: A binary might contain more than one dynamic symbol table.
2262 Unusual, but possible. Warn, but continue. */
2263 if (elf_dynsymtab (abfd) != 0)
2264 {
4eca0228 2265 _bfd_error_handler
695344c0 2266 /* xgettext:c-format */
871b3ab2 2267 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2268 " - ignoring the table in section %u"),
16ad13ec
NC
2269 abfd, shindex);
2270 goto success;
2271 }
252b5132
RH
2272 elf_dynsymtab (abfd) = shindex;
2273 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2274 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2275 abfd->flags |= HAS_SYMS;
2276
2277 /* Besides being a symbol table, we also treat this as a regular
2278 section, so that objcopy can handle it. */
bf67003b
NC
2279 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2280 goto success;
252b5132 2281
bf67003b 2282 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2283 {
2284 elf_section_list * entry;
9ad5cbcf 2285
6a40cf0c
NC
2286 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2287 if (entry->ndx == shindex)
2288 goto success;
07d6d2b8 2289
7a6e0d89 2290 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2291 if (entry == NULL)
2292 goto fail;
2293 entry->ndx = shindex;
2294 entry->hdr = * hdr;
2295 entry->next = elf_symtab_shndx_list (abfd);
2296 elf_symtab_shndx_list (abfd) = entry;
2297 elf_elfsections (abfd)[shindex] = & entry->hdr;
2298 goto success;
2299 }
9ad5cbcf 2300
bf67003b 2301 case SHT_STRTAB: /* A string table. */
252b5132 2302 if (hdr->bfd_section != NULL)
bf67003b
NC
2303 goto success;
2304
252b5132
RH
2305 if (ehdr->e_shstrndx == shindex)
2306 {
2307 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2308 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2309 goto success;
252b5132 2310 }
bf67003b 2311
1b3a8575
AM
2312 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2313 {
2314 symtab_strtab:
2315 elf_tdata (abfd)->strtab_hdr = *hdr;
2316 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2317 goto success;
1b3a8575 2318 }
bf67003b 2319
1b3a8575
AM
2320 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2321 {
2322 dynsymtab_strtab:
2323 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2324 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2325 elf_elfsections (abfd)[shindex] = hdr;
2326 /* We also treat this as a regular section, so that objcopy
2327 can handle it. */
bf67003b
NC
2328 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2329 shindex);
2330 goto success;
1b3a8575 2331 }
252b5132 2332
1b3a8575
AM
2333 /* If the string table isn't one of the above, then treat it as a
2334 regular section. We need to scan all the headers to be sure,
2335 just in case this strtab section appeared before the above. */
2336 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2337 {
2338 unsigned int i, num_sec;
252b5132 2339
1b3a8575
AM
2340 num_sec = elf_numsections (abfd);
2341 for (i = 1; i < num_sec; i++)
2342 {
2343 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2344 if (hdr2->sh_link == shindex)
2345 {
933d961a
JJ
2346 /* Prevent endless recursion on broken objects. */
2347 if (i == shindex)
bf67003b 2348 goto fail;
1b3a8575 2349 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2350 goto fail;
1b3a8575
AM
2351 if (elf_onesymtab (abfd) == i)
2352 goto symtab_strtab;
2353 if (elf_dynsymtab (abfd) == i)
2354 goto dynsymtab_strtab;
2355 }
2356 }
2357 }
bf67003b
NC
2358 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2359 goto success;
252b5132
RH
2360
2361 case SHT_REL:
2362 case SHT_RELA:
a619b587 2363 case SHT_RELR:
252b5132
RH
2364 /* *These* do a lot of work -- but build no sections! */
2365 {
2366 asection *target_sect;
d4730f92 2367 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2368 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2369 struct bfd_elf_section_data *esdt;
a619b587 2370 bfd_size_type size;
252b5132 2371
a619b587
FS
2372 if (hdr->sh_type == SHT_REL)
2373 size = bed->s->sizeof_rel;
2374 else if (hdr->sh_type == SHT_RELA)
2375 size = bed->s->sizeof_rela;
2376 else
2377 size = bed->s->arch_size / 8;
2378 if (hdr->sh_entsize != size)
bf67003b 2379 goto fail;
a50b2160 2380
03ae5f59 2381 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2382 if (hdr->sh_link >= num_sec)
03ae5f59 2383 {
4eca0228 2384 _bfd_error_handler
695344c0 2385 /* xgettext:c-format */
871b3ab2 2386 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2387 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2388 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2389 shindex);
2390 goto success;
03ae5f59
ILT
2391 }
2392
252b5132 2393 /* Get the symbol table. */
1b3a8575
AM
2394 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2395 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2396 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2397 goto fail;
252b5132 2398
a4bcd733
AM
2399 /* If this is an alloc section in an executable or shared
2400 library, or the reloc section does not use the main symbol
2401 table we don't treat it as a reloc section. BFD can't
2402 adequately represent such a section, so at least for now,
2403 we don't try. We just present it as a normal section. We
2404 also can't use it as a reloc section if it points to the
2405 null section, an invalid section, another reloc section, or
2406 its sh_link points to the null section. */
2407 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2408 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2409 || hdr->sh_link == SHN_UNDEF
a4bcd733 2410 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2411 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2412 || hdr->sh_info >= num_sec
2413 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2414 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2415 {
2416 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2417 shindex);
2418 goto success;
2419 }
252b5132
RH
2420
2421 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2422 goto fail;
2423
252b5132
RH
2424 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2425 if (target_sect == NULL)
bf67003b 2426 goto fail;
252b5132 2427
d4730f92
BS
2428 esdt = elf_section_data (target_sect);
2429 if (hdr->sh_type == SHT_RELA)
2430 p_hdr = &esdt->rela.hdr;
252b5132 2431 else
d4730f92
BS
2432 p_hdr = &esdt->rel.hdr;
2433
a7ba3896
NC
2434 /* PR 17512: file: 0b4f81b7.
2435 Also see PR 24456, for a file which deliberately has two reloc
2436 sections. */
06614111 2437 if (*p_hdr != NULL)
a7ba3896 2438 {
a859124d 2439 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
a8e14f4c
NC
2440 {
2441 _bfd_error_handler
2442 /* xgettext:c-format */
a859124d
AM
2443 (_("%pB: warning: secondary relocation section '%s' "
2444 "for section %pA found - ignoring"),
a8e14f4c
NC
2445 abfd, name, target_sect);
2446 }
956ea65c
MM
2447 else
2448 esdt->has_secondary_relocs = true;
a7ba3896
NC
2449 goto success;
2450 }
a8e14f4c 2451
ef53be89 2452 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2453 if (hdr2 == NULL)
bf67003b 2454 goto fail;
252b5132 2455 *hdr2 = *hdr;
d4730f92 2456 *p_hdr = hdr2;
252b5132 2457 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2458 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2459 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2460 target_sect->flags |= SEC_RELOC;
2461 target_sect->relocation = NULL;
2462 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2463 /* In the section to which the relocations apply, mark whether
2464 its relocations are of the REL or RELA variety. */
72730e0c 2465 if (hdr->sh_size != 0)
d4730f92
BS
2466 {
2467 if (hdr->sh_type == SHT_RELA)
2468 target_sect->use_rela_p = 1;
2469 }
252b5132 2470 abfd->flags |= HAS_RELOC;
bf67003b 2471 goto success;
252b5132 2472 }
252b5132
RH
2473
2474 case SHT_GNU_verdef:
2475 elf_dynverdef (abfd) = shindex;
2476 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2477 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2478 goto success;
252b5132
RH
2479
2480 case SHT_GNU_versym:
a50b2160 2481 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2482 goto fail;
2483
252b5132
RH
2484 elf_dynversym (abfd) = shindex;
2485 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2486 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2487 goto success;
252b5132
RH
2488
2489 case SHT_GNU_verneed:
2490 elf_dynverref (abfd) = shindex;
2491 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2492 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2493 goto success;
252b5132
RH
2494
2495 case SHT_SHLIB:
bf67003b 2496 goto success;
252b5132 2497
dbb410c3 2498 case SHT_GROUP:
44534af3 2499 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2500 goto fail;
2501
6dc132d9 2502 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2503 goto fail;
2504
bf67003b 2505 goto success;
dbb410c3 2506
252b5132 2507 default:
104d59d1
JM
2508 /* Possibly an attributes section. */
2509 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2510 || hdr->sh_type == bed->obj_attrs_section_type)
2511 {
2512 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2513 goto fail;
104d59d1 2514 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2515 goto success;
104d59d1
JM
2516 }
2517
252b5132 2518 /* Check for any processor-specific section types. */
3eb70a79 2519 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2520 goto success;
3eb70a79
L
2521
2522 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2523 {
2524 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2525 /* FIXME: How to properly handle allocated section reserved
2526 for applications? */
4eca0228 2527 _bfd_error_handler
695344c0 2528 /* xgettext:c-format */
871b3ab2 2529 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2530 abfd, hdr->sh_type, name);
3eb70a79 2531 else
bf67003b
NC
2532 {
2533 /* Allow sections reserved for applications. */
2534 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2535 shindex);
2536 goto success;
2537 }
3eb70a79
L
2538 }
2539 else if (hdr->sh_type >= SHT_LOPROC
2540 && hdr->sh_type <= SHT_HIPROC)
2541 /* FIXME: We should handle this section. */
4eca0228 2542 _bfd_error_handler
695344c0 2543 /* xgettext:c-format */
871b3ab2 2544 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2545 abfd, hdr->sh_type, name);
3eb70a79 2546 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2547 {
2548 /* Unrecognised OS-specific sections. */
2549 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2550 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2551 required to correctly process the section and the file should
ff15b240 2552 be rejected with an error message. */
4eca0228 2553 _bfd_error_handler
695344c0 2554 /* xgettext:c-format */
871b3ab2 2555 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2556 abfd, hdr->sh_type, name);
ff15b240 2557 else
bf67003b
NC
2558 {
2559 /* Otherwise it should be processed. */
2560 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2561 goto success;
2562 }
ff15b240 2563 }
3eb70a79
L
2564 else
2565 /* FIXME: We should handle this section. */
4eca0228 2566 _bfd_error_handler
695344c0 2567 /* xgettext:c-format */
871b3ab2 2568 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2569 abfd, hdr->sh_type, name);
3eb70a79 2570
bf67003b 2571 goto fail;
252b5132
RH
2572 }
2573
bf67003b 2574 fail:
0a1b45a2 2575 ret = false;
bf67003b 2576 success:
0a1b45a2 2577 elf_tdata (abfd)->being_created[shindex] = false;
bf67003b 2578 return ret;
252b5132
RH
2579}
2580
87d72d41 2581/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2582
87d72d41
AM
2583Elf_Internal_Sym *
2584bfd_sym_from_r_symndx (struct sym_cache *cache,
2585 bfd *abfd,
2586 unsigned long r_symndx)
ec338859 2587{
ec338859
AM
2588 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2589
a5d1b3b5
AM
2590 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2591 {
2592 Elf_Internal_Shdr *symtab_hdr;
2593 unsigned char esym[sizeof (Elf64_External_Sym)];
2594 Elf_External_Sym_Shndx eshndx;
ec338859 2595
a5d1b3b5
AM
2596 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2597 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2598 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2599 return NULL;
9ad5cbcf 2600
a5d1b3b5
AM
2601 if (cache->abfd != abfd)
2602 {
2603 memset (cache->indx, -1, sizeof (cache->indx));
2604 cache->abfd = abfd;
2605 }
2606 cache->indx[ent] = r_symndx;
ec338859 2607 }
a5d1b3b5 2608
87d72d41 2609 return &cache->sym[ent];
ec338859
AM
2610}
2611
252b5132
RH
2612/* Given an ELF section number, retrieve the corresponding BFD
2613 section. */
2614
2615asection *
91d6fa6a 2616bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2617{
91d6fa6a 2618 if (sec_index >= elf_numsections (abfd))
252b5132 2619 return NULL;
91d6fa6a 2620 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2621}
2622
b35d266b 2623static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2624{
0112cd26 2625 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2626 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2627};
2628
b35d266b 2629static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2630{
0112cd26 2631 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2632 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2633 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2634};
2635
b35d266b 2636static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2637{
07d6d2b8
AM
2638 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2639 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2640 /* There are more DWARF sections than these, but they needn't be added here
2641 unless you have to cope with broken compilers that don't emit section
2642 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2643 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2644 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2645 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2646 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2647 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2648 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2649 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2650 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2651 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2652};
2653
b35d266b 2654static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2655{
07d6d2b8 2656 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2657 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2658 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2659};
2660
b35d266b 2661static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2662{
0112cd26 2663 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56
JL
2664 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2665 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2666 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2667 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2668 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2669 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2670 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2671 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2672 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2673 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2674 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2675};
2676
b35d266b 2677static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2678{
07d6d2b8
AM
2679 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2680 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2681};
2682
b35d266b 2683static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2684{
07d6d2b8 2685 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2686 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2687 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2688 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2689};
2690
b35d266b 2691static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2692{
0112cd26 2693 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2694 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2695};
2696
b35d266b 2697static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2698{
2c6f3e56 2699 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 2700 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2701 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2702 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2703};
2704
b35d266b 2705static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2706{
f5e98b7d 2707 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2c6f3e56 2708 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6f9dbcd4 2709 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2710 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2711 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2712};
2713
b35d266b 2714static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2715{
0112cd26
NC
2716 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2717 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
fc07cf22 2718 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
07d6d2b8
AM
2719 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2720 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2721 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2722};
2723
b35d266b 2724static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2725{
0112cd26
NC
2726 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2727 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2728 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2729 /* See struct bfd_elf_special_section declaration for the semantics of
2730 this special case where .prefix_length != strlen (.prefix). */
2731 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2732 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2733};
2734
b35d266b 2735static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2736{
07d6d2b8
AM
2737 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2738 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2739 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2740 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2741};
2742
1b315056
CS
2743static const struct bfd_elf_special_section special_sections_z[] =
2744{
07d6d2b8
AM
2745 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2746 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2747 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2748 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2749 { NULL, 0, 0, 0, 0 }
1b315056
CS
2750};
2751
e4c93b56 2752static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2753{
7f4d3958 2754 special_sections_b, /* 'b' */
98ece1b3 2755 special_sections_c, /* 'c' */
7f4d3958
L
2756 special_sections_d, /* 'd' */
2757 NULL, /* 'e' */
2758 special_sections_f, /* 'f' */
2759 special_sections_g, /* 'g' */
2760 special_sections_h, /* 'h' */
2761 special_sections_i, /* 'i' */
2762 NULL, /* 'j' */
2763 NULL, /* 'k' */
2764 special_sections_l, /* 'l' */
2765 NULL, /* 'm' */
2766 special_sections_n, /* 'n' */
2767 NULL, /* 'o' */
2768 special_sections_p, /* 'p' */
2769 NULL, /* 'q' */
2770 special_sections_r, /* 'r' */
2771 special_sections_s, /* 's' */
2772 special_sections_t, /* 't' */
1b315056
CS
2773 NULL, /* 'u' */
2774 NULL, /* 'v' */
2775 NULL, /* 'w' */
2776 NULL, /* 'x' */
2777 NULL, /* 'y' */
2778 special_sections_z /* 'z' */
7f4d3958
L
2779};
2780
551b43fd
AM
2781const struct bfd_elf_special_section *
2782_bfd_elf_get_special_section (const char *name,
2783 const struct bfd_elf_special_section *spec,
2784 unsigned int rela)
2f89ff8d
L
2785{
2786 int i;
7f4d3958 2787 int len;
7f4d3958 2788
551b43fd 2789 len = strlen (name);
7f4d3958 2790
551b43fd 2791 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2792 {
2793 int suffix_len;
551b43fd 2794 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2795
2796 if (len < prefix_len)
2797 continue;
551b43fd 2798 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2799 continue;
2800
551b43fd 2801 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2802 if (suffix_len <= 0)
2803 {
2804 if (name[prefix_len] != 0)
2805 {
2806 if (suffix_len == 0)
2807 continue;
2808 if (name[prefix_len] != '.'
2809 && (suffix_len == -2
551b43fd 2810 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2811 continue;
2812 }
2813 }
2814 else
2815 {
2816 if (len < prefix_len + suffix_len)
2817 continue;
2818 if (memcmp (name + len - suffix_len,
551b43fd 2819 spec[i].prefix + prefix_len,
7dcb9820
AM
2820 suffix_len) != 0)
2821 continue;
2822 }
551b43fd 2823 return &spec[i];
7dcb9820 2824 }
2f89ff8d
L
2825
2826 return NULL;
2827}
2828
7dcb9820 2829const struct bfd_elf_special_section *
29ef7005 2830_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2831{
551b43fd
AM
2832 int i;
2833 const struct bfd_elf_special_section *spec;
29ef7005 2834 const struct elf_backend_data *bed;
2f89ff8d
L
2835
2836 /* See if this is one of the special sections. */
551b43fd
AM
2837 if (sec->name == NULL)
2838 return NULL;
2f89ff8d 2839
29ef7005
L
2840 bed = get_elf_backend_data (abfd);
2841 spec = bed->special_sections;
2842 if (spec)
2843 {
2844 spec = _bfd_elf_get_special_section (sec->name,
2845 bed->special_sections,
2846 sec->use_rela_p);
2847 if (spec != NULL)
2848 return spec;
2849 }
2850
551b43fd
AM
2851 if (sec->name[0] != '.')
2852 return NULL;
2f89ff8d 2853
551b43fd 2854 i = sec->name[1] - 'b';
1b315056 2855 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2856 return NULL;
2857
2858 spec = special_sections[i];
2f89ff8d 2859
551b43fd
AM
2860 if (spec == NULL)
2861 return NULL;
2862
2863 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2864}
2865
0a1b45a2 2866bool
217aa764 2867_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2868{
2869 struct bfd_elf_section_data *sdata;
551b43fd 2870 const struct elf_backend_data *bed;
7dcb9820 2871 const struct bfd_elf_special_section *ssect;
252b5132 2872
f0abc2a1
AM
2873 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2874 if (sdata == NULL)
2875 {
a50b1753 2876 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2877 sizeof (*sdata));
f0abc2a1 2878 if (sdata == NULL)
0a1b45a2 2879 return false;
217aa764 2880 sec->used_by_bfd = sdata;
f0abc2a1 2881 }
bf572ba0 2882
551b43fd
AM
2883 /* Indicate whether or not this section should use RELA relocations. */
2884 bed = get_elf_backend_data (abfd);
2885 sec->use_rela_p = bed->default_use_rela_p;
2886
8c803a2d
AM
2887 /* Set up ELF section type and flags for newly created sections, if
2888 there is an ABI mandated section. */
2889 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2890 if (ssect != NULL)
2f89ff8d 2891 {
8c803a2d
AM
2892 elf_section_type (sec) = ssect->type;
2893 elf_section_flags (sec) = ssect->attr;
2f89ff8d
L
2894 }
2895
f592407e 2896 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2897}
2898
2899/* Create a new bfd section from an ELF program header.
2900
2901 Since program segments have no names, we generate a synthetic name
2902 of the form segment<NUM>, where NUM is generally the index in the
2903 program header table. For segments that are split (see below) we
2904 generate the names segment<NUM>a and segment<NUM>b.
2905
2906 Note that some program segments may have a file size that is different than
2907 (less than) the memory size. All this means is that at execution the
2908 system must allocate the amount of memory specified by the memory size,
2909 but only initialize it with the first "file size" bytes read from the
2910 file. This would occur for example, with program segments consisting
2911 of combined data+bss.
2912
2913 To handle the above situation, this routine generates TWO bfd sections
2914 for the single program segment. The first has the length specified by
2915 the file size of the segment, and the second has the length specified
2916 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2917 into its initialized and uninitialized parts.
252b5132
RH
2918
2919 */
2920
0a1b45a2 2921bool
217aa764
AM
2922_bfd_elf_make_section_from_phdr (bfd *abfd,
2923 Elf_Internal_Phdr *hdr,
91d6fa6a 2924 int hdr_index,
a50b1753 2925 const char *type_name)
252b5132
RH
2926{
2927 asection *newsect;
2928 char *name;
2929 char namebuf[64];
d4c88bbb 2930 size_t len;
252b5132 2931 int split;
502794d4 2932 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132
RH
2933
2934 split = ((hdr->p_memsz > 0)
2935 && (hdr->p_filesz > 0)
2936 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2937
2938 if (hdr->p_filesz > 0)
252b5132 2939 {
91d6fa6a 2940 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2941 len = strlen (namebuf) + 1;
a50b1753 2942 name = (char *) bfd_alloc (abfd, len);
d5191d0c 2943 if (!name)
0a1b45a2 2944 return false;
d5191d0c
AM
2945 memcpy (name, namebuf, len);
2946 newsect = bfd_make_section (abfd, name);
2947 if (newsect == NULL)
0a1b45a2 2948 return false;
502794d4
CE
2949 newsect->vma = hdr->p_vaddr / opb;
2950 newsect->lma = hdr->p_paddr / opb;
d5191d0c
AM
2951 newsect->size = hdr->p_filesz;
2952 newsect->filepos = hdr->p_offset;
2953 newsect->flags |= SEC_HAS_CONTENTS;
2954 newsect->alignment_power = bfd_log2 (hdr->p_align);
2955 if (hdr->p_type == PT_LOAD)
252b5132 2956 {
d5191d0c
AM
2957 newsect->flags |= SEC_ALLOC;
2958 newsect->flags |= SEC_LOAD;
2959 if (hdr->p_flags & PF_X)
2960 {
2961 /* FIXME: all we known is that it has execute PERMISSION,
2962 may be data. */
2963 newsect->flags |= SEC_CODE;
2964 }
2965 }
2966 if (!(hdr->p_flags & PF_W))
2967 {
2968 newsect->flags |= SEC_READONLY;
252b5132 2969 }
252b5132
RH
2970 }
2971
d5191d0c 2972 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2973 {
d5191d0c
AM
2974 bfd_vma align;
2975
91d6fa6a 2976 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2977 len = strlen (namebuf) + 1;
a50b1753 2978 name = (char *) bfd_alloc (abfd, len);
252b5132 2979 if (!name)
0a1b45a2 2980 return false;
d4c88bbb 2981 memcpy (name, namebuf, len);
252b5132
RH
2982 newsect = bfd_make_section (abfd, name);
2983 if (newsect == NULL)
0a1b45a2 2984 return false;
502794d4
CE
2985 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2986 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
eea6121a 2987 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2988 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2989 align = newsect->vma & -newsect->vma;
2990 if (align == 0 || align > hdr->p_align)
2991 align = hdr->p_align;
2992 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2993 if (hdr->p_type == PT_LOAD)
2994 {
2995 newsect->flags |= SEC_ALLOC;
2996 if (hdr->p_flags & PF_X)
2997 newsect->flags |= SEC_CODE;
2998 }
2999 if (!(hdr->p_flags & PF_W))
3000 newsect->flags |= SEC_READONLY;
3001 }
3002
0a1b45a2 3003 return true;
252b5132
RH
3004}
3005
0a1b45a2 3006static bool
864619bb
KS
3007_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3008{
3009 /* The return value is ignored. Build-ids are considered optional. */
3010 if (templ->xvec->flavour == bfd_target_elf_flavour)
3011 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3012 (templ, offset);
0a1b45a2 3013 return false;
864619bb
KS
3014}
3015
0a1b45a2 3016bool
91d6fa6a 3017bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3018{
9c5bfbb7 3019 const struct elf_backend_data *bed;
20cfcaae
NC
3020
3021 switch (hdr->p_type)
3022 {
3023 case PT_NULL:
91d6fa6a 3024 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3025
3026 case PT_LOAD:
864619bb 3027 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
0a1b45a2 3028 return false;
864619bb
KS
3029 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3030 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
0a1b45a2 3031 return true;
20cfcaae
NC
3032
3033 case PT_DYNAMIC:
91d6fa6a 3034 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3035
3036 case PT_INTERP:
91d6fa6a 3037 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3038
3039 case PT_NOTE:
91d6fa6a 3040 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
0a1b45a2 3041 return false;
276da9b3
L
3042 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3043 hdr->p_align))
0a1b45a2
AM
3044 return false;
3045 return true;
20cfcaae
NC
3046
3047 case PT_SHLIB:
91d6fa6a 3048 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3049
3050 case PT_PHDR:
91d6fa6a 3051 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3052
811072d8 3053 case PT_GNU_EH_FRAME:
91d6fa6a 3054 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3055 "eh_frame_hdr");
3056
2b05f1b7 3057 case PT_GNU_STACK:
91d6fa6a 3058 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3059
8c37241b 3060 case PT_GNU_RELRO:
91d6fa6a 3061 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3062
20cfcaae 3063 default:
8c1acd09 3064 /* Check for any processor-specific program segment types. */
20cfcaae 3065 bed = get_elf_backend_data (abfd);
91d6fa6a 3066 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3067 }
3068}
3069
d4730f92
BS
3070/* Return the REL_HDR for SEC, assuming there is only a single one, either
3071 REL or RELA. */
3072
3073Elf_Internal_Shdr *
3074_bfd_elf_single_rel_hdr (asection *sec)
3075{
3076 if (elf_section_data (sec)->rel.hdr)
3077 {
3078 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3079 return elf_section_data (sec)->rel.hdr;
3080 }
3081 else
3082 return elf_section_data (sec)->rela.hdr;
3083}
3084
0a1b45a2 3085static bool
3e19fb8f
L
3086_bfd_elf_set_reloc_sh_name (bfd *abfd,
3087 Elf_Internal_Shdr *rel_hdr,
3088 const char *sec_name,
0a1b45a2 3089 bool use_rela_p)
3e19fb8f
L
3090{
3091 char *name = (char *) bfd_alloc (abfd,
3092 sizeof ".rela" + strlen (sec_name));
3093 if (name == NULL)
0a1b45a2 3094 return false;
3e19fb8f
L
3095
3096 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3097 rel_hdr->sh_name =
3098 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
0a1b45a2 3099 false);
3e19fb8f 3100 if (rel_hdr->sh_name == (unsigned int) -1)
0a1b45a2 3101 return false;
3e19fb8f 3102
0a1b45a2 3103 return true;
3e19fb8f
L
3104}
3105
d4730f92
BS
3106/* Allocate and initialize a section-header for a new reloc section,
3107 containing relocations against ASECT. It is stored in RELDATA. If
3108 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3109 relocations. */
23bc299b 3110
0a1b45a2 3111static bool
217aa764 3112_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3113 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3114 const char *sec_name,
0a1b45a2
AM
3115 bool use_rela_p,
3116 bool delay_st_name_p)
23bc299b 3117{
d4730f92 3118 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3119 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3120
d4730f92 3121 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3122 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3123 reldata->hdr = rel_hdr;
23bc299b 3124
3e19fb8f
L
3125 if (delay_st_name_p)
3126 rel_hdr->sh_name = (unsigned int) -1;
3127 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3128 use_rela_p))
0a1b45a2 3129 return false;
23bc299b
MM
3130 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3131 rel_hdr->sh_entsize = (use_rela_p
3132 ? bed->s->sizeof_rela
3133 : bed->s->sizeof_rel);
72de5009 3134 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3135 rel_hdr->sh_flags = 0;
23bc299b
MM
3136 rel_hdr->sh_addr = 0;
3137 rel_hdr->sh_size = 0;
3138 rel_hdr->sh_offset = 0;
3139
0a1b45a2 3140 return true;
23bc299b
MM
3141}
3142
94be91de
JB
3143/* Return the default section type based on the passed in section flags. */
3144
3145int
3146bfd_elf_get_default_section_type (flagword flags)
3147{
0e41bebb 3148 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3149 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3150 return SHT_NOBITS;
3151 return SHT_PROGBITS;
3152}
3153
d4730f92
BS
3154struct fake_section_arg
3155{
3156 struct bfd_link_info *link_info;
0a1b45a2 3157 bool failed;
d4730f92
BS
3158};
3159
252b5132
RH
3160/* Set up an ELF internal section header for a section. */
3161
252b5132 3162static void
d4730f92 3163elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3164{
d4730f92 3165 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3166 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3167 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3168 Elf_Internal_Shdr *this_hdr;
0414f35b 3169 unsigned int sh_type;
0ce398f1 3170 const char *name = asect->name;
0a1b45a2 3171 bool delay_st_name_p = false;
233bf4f8 3172 bfd_vma mask;
252b5132 3173
d4730f92 3174 if (arg->failed)
252b5132
RH
3175 {
3176 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3177 loop. */
252b5132
RH
3178 return;
3179 }
3180
d4730f92 3181 this_hdr = &esd->this_hdr;
252b5132 3182
f6fe1ccd 3183 if (arg->link_info)
0ce398f1 3184 {
f6fe1ccd
L
3185 /* ld: compress DWARF debug sections with names: .debug_*. */
3186 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3187 && (asect->flags & SEC_DEBUGGING)
3188 && name[1] == 'd'
3189 && name[6] == '_')
3190 {
3191 /* Set SEC_ELF_COMPRESS to indicate this section should be
3192 compressed. */
3193 asect->flags |= SEC_ELF_COMPRESS;
dd905818 3194 /* If this section will be compressed, delay adding section
3e19fb8f
L
3195 name to section name section after it is compressed in
3196 _bfd_elf_assign_file_positions_for_non_load. */
0a1b45a2 3197 delay_st_name_p = true;
f6fe1ccd
L
3198 }
3199 }
3200 else if ((asect->flags & SEC_ELF_RENAME))
3201 {
3202 /* objcopy: rename output DWARF debug section. */
3203 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3204 {
3205 /* When we decompress or compress with SHF_COMPRESSED,
3206 convert section name from .zdebug_* to .debug_* if
3207 needed. */
3208 if (name[1] == 'z')
3209 {
3210 char *new_name = convert_zdebug_to_debug (abfd, name);
3211 if (new_name == NULL)
3212 {
0a1b45a2 3213 arg->failed = true;
f6fe1ccd
L
3214 return;
3215 }
3216 name = new_name;
3217 }
3218 }
3219 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3220 {
f6fe1ccd
L
3221 /* PR binutils/18087: Compression does not always make a
3222 section smaller. So only rename the section when
3223 compression has actually taken place. If input section
3224 name is .zdebug_*, we should never compress it again. */
3225 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3226 if (new_name == NULL)
3227 {
0a1b45a2 3228 arg->failed = true;
0ce398f1
L
3229 return;
3230 }
f6fe1ccd
L
3231 BFD_ASSERT (name[1] != 'z');
3232 name = new_name;
0ce398f1
L
3233 }
3234 }
3235
3e19fb8f
L
3236 if (delay_st_name_p)
3237 this_hdr->sh_name = (unsigned int) -1;
3238 else
252b5132 3239 {
3e19fb8f
L
3240 this_hdr->sh_name
3241 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3242 name, false);
3e19fb8f
L
3243 if (this_hdr->sh_name == (unsigned int) -1)
3244 {
0a1b45a2 3245 arg->failed = true;
3e19fb8f
L
3246 return;
3247 }
252b5132
RH
3248 }
3249
a4d8e49b 3250 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3251
3252 if ((asect->flags & SEC_ALLOC) != 0
3253 || asect->user_set_vma)
502794d4 3254 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
252b5132
RH
3255 else
3256 this_hdr->sh_addr = 0;
3257
3258 this_hdr->sh_offset = 0;
eea6121a 3259 this_hdr->sh_size = asect->size;
252b5132 3260 this_hdr->sh_link = 0;
c86934ce
NC
3261 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3262 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3263 {
4eca0228 3264 _bfd_error_handler
695344c0 3265 /* xgettext:c-format */
9793eb77 3266 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3267 abfd, asect->alignment_power, asect);
0a1b45a2 3268 arg->failed = true;
c86934ce
NC
3269 return;
3270 }
233bf4f8
AM
3271 /* Set sh_addralign to the highest power of two given by alignment
3272 consistent with the section VMA. Linker scripts can force VMA. */
3273 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3274 this_hdr->sh_addralign = mask & -mask;
252b5132
RH
3275 /* The sh_entsize and sh_info fields may have been set already by
3276 copy_private_section_data. */
3277
3278 this_hdr->bfd_section = asect;
3279 this_hdr->contents = NULL;
3280
3cddba1e
L
3281 /* If the section type is unspecified, we set it based on
3282 asect->flags. */
98ece1b3
AM
3283 if ((asect->flags & SEC_GROUP) != 0)
3284 sh_type = SHT_GROUP;
98ece1b3 3285 else
94be91de 3286 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3287
3cddba1e 3288 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3289 this_hdr->sh_type = sh_type;
3290 else if (this_hdr->sh_type == SHT_NOBITS
3291 && sh_type == SHT_PROGBITS
3292 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3293 {
98ece1b3
AM
3294 /* Warn if we are changing a NOBITS section to PROGBITS, but
3295 allow the link to proceed. This can happen when users link
3296 non-bss input sections to bss output sections, or emit data
3297 to a bss output section via a linker script. */
4eca0228 3298 _bfd_error_handler
871b3ab2 3299 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3300 this_hdr->sh_type = sh_type;
3cddba1e
L
3301 }
3302
2f89ff8d 3303 switch (this_hdr->sh_type)
252b5132 3304 {
2f89ff8d 3305 default:
2f89ff8d
L
3306 break;
3307
3308 case SHT_STRTAB:
2f89ff8d
L
3309 case SHT_NOTE:
3310 case SHT_NOBITS:
3311 case SHT_PROGBITS:
3312 break;
606851fb
AM
3313
3314 case SHT_INIT_ARRAY:
3315 case SHT_FINI_ARRAY:
3316 case SHT_PREINIT_ARRAY:
3317 this_hdr->sh_entsize = bed->s->arch_size / 8;
3318 break;
2f89ff8d
L
3319
3320 case SHT_HASH:
c7ac6ff8 3321 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3322 break;
5de3bf90 3323
2f89ff8d 3324 case SHT_DYNSYM:
252b5132 3325 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3326 break;
3327
3328 case SHT_DYNAMIC:
252b5132 3329 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3330 break;
3331
3332 case SHT_RELA:
3333 if (get_elf_backend_data (abfd)->may_use_rela_p)
3334 this_hdr->sh_entsize = bed->s->sizeof_rela;
3335 break;
3336
3337 case SHT_REL:
3338 if (get_elf_backend_data (abfd)->may_use_rel_p)
3339 this_hdr->sh_entsize = bed->s->sizeof_rel;
3340 break;
3341
3342 case SHT_GNU_versym:
252b5132 3343 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3344 break;
3345
3346 case SHT_GNU_verdef:
252b5132
RH
3347 this_hdr->sh_entsize = 0;
3348 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3349 cverdefs. The linker will set cverdefs, but sh_info will be
3350 zero. */
252b5132
RH
3351 if (this_hdr->sh_info == 0)
3352 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3353 else
3354 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3355 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3356 break;
3357
3358 case SHT_GNU_verneed:
252b5132
RH
3359 this_hdr->sh_entsize = 0;
3360 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3361 cverrefs. The linker will set cverrefs, but sh_info will be
3362 zero. */
252b5132
RH
3363 if (this_hdr->sh_info == 0)
3364 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3365 else
3366 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3367 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3368 break;
3369
3370 case SHT_GROUP:
1783205a 3371 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3372 break;
fdc90cb4
JJ
3373
3374 case SHT_GNU_HASH:
3375 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3376 break;
dbb410c3 3377 }
252b5132
RH
3378
3379 if ((asect->flags & SEC_ALLOC) != 0)
3380 this_hdr->sh_flags |= SHF_ALLOC;
3381 if ((asect->flags & SEC_READONLY) == 0)
3382 this_hdr->sh_flags |= SHF_WRITE;
3383 if ((asect->flags & SEC_CODE) != 0)
3384 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3385 if ((asect->flags & SEC_MERGE) != 0)
3386 {
3387 this_hdr->sh_flags |= SHF_MERGE;
3388 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3389 }
84865015
NC
3390 if ((asect->flags & SEC_STRINGS) != 0)
3391 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3392 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3393 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3394 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3395 {
3396 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3397 if (asect->size == 0
3398 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3399 {
3a800eb9 3400 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3401
704afa60 3402 this_hdr->sh_size = 0;
3a800eb9
AM
3403 if (o != NULL)
3404 {
704afa60 3405 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3406 if (this_hdr->sh_size != 0)
3407 this_hdr->sh_type = SHT_NOBITS;
3408 }
704afa60
JJ
3409 }
3410 }
18ae9cc1
L
3411 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3412 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3413
d4730f92
BS
3414 /* If the section has relocs, set up a section header for the
3415 SHT_REL[A] section. If two relocation sections are required for
3416 this section, it is up to the processor-specific back-end to
3417 create the other. */
3418 if ((asect->flags & SEC_RELOC) != 0)
3419 {
3420 /* When doing a relocatable link, create both REL and RELA sections if
3421 needed. */
3422 if (arg->link_info
3423 /* Do the normal setup if we wouldn't create any sections here. */
3424 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3425 && (bfd_link_relocatable (arg->link_info)
3426 || arg->link_info->emitrelocations))
d4730f92
BS
3427 {
3428 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3429 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
0a1b45a2 3430 false, delay_st_name_p))
d4730f92 3431 {
0a1b45a2 3432 arg->failed = true;
d4730f92
BS
3433 return;
3434 }
3435 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3436 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
0a1b45a2 3437 true, delay_st_name_p))
d4730f92 3438 {
0a1b45a2 3439 arg->failed = true;
d4730f92
BS
3440 return;
3441 }
3442 }
3443 else if (!_bfd_elf_init_reloc_shdr (abfd,
3444 (asect->use_rela_p
3445 ? &esd->rela : &esd->rel),
f6fe1ccd 3446 name,
3e19fb8f
L
3447 asect->use_rela_p,
3448 delay_st_name_p))
db4677b8 3449 {
0a1b45a2 3450 arg->failed = true;
db4677b8
AM
3451 return;
3452 }
d4730f92
BS
3453 }
3454
252b5132 3455 /* Check for processor-specific section types. */
0414f35b 3456 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3457 if (bed->elf_backend_fake_sections
3458 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8 3459 {
0a1b45a2 3460 arg->failed = true;
db4677b8
AM
3461 return;
3462 }
252b5132 3463
42bb2e33 3464 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3465 {
3466 /* Don't change the header type from NOBITS if we are being
42bb2e33 3467 called for objcopy --only-keep-debug. */
0414f35b
AM
3468 this_hdr->sh_type = sh_type;
3469 }
252b5132
RH
3470}
3471
bcacc0f5
AM
3472/* Fill in the contents of a SHT_GROUP section. Called from
3473 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3474 when ELF targets use the generic linker, ld. Called for ld -r
3475 from bfd_elf_final_link. */
dbb410c3 3476
1126897b 3477void
217aa764 3478bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3479{
0a1b45a2 3480 bool *failedptr = (bool *) failedptrarg;
9dce4196 3481 asection *elt, *first;
dbb410c3 3482 unsigned char *loc;
0a1b45a2 3483 bool gas;
dbb410c3 3484
7e4111ad
L
3485 /* Ignore linker created group section. See elfNN_ia64_object_p in
3486 elfxx-ia64.c. */
ce5aecf8
AM
3487 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3488 || sec->size == 0
dbb410c3
AM
3489 || *failedptr)
3490 return;
3491
bcacc0f5
AM
3492 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3493 {
3494 unsigned long symindx = 0;
3495
3496 /* elf_group_id will have been set up by objcopy and the
3497 generic linker. */
3498 if (elf_group_id (sec) != NULL)
3499 symindx = elf_group_id (sec)->udata.i;
1126897b 3500
bcacc0f5
AM
3501 if (symindx == 0)
3502 {
3503 /* If called from the assembler, swap_out_syms will have set up
6a541707
NC
3504 elf_section_syms.
3505 PR 25699: A corrupt input file could contain bogus group info. */
27e3da31
AM
3506 if (sec->index >= elf_num_section_syms (abfd)
3507 || elf_section_syms (abfd)[sec->index] == NULL)
6a541707 3508 {
0a1b45a2 3509 *failedptr = true;
6a541707
NC
3510 return;
3511 }
bcacc0f5
AM
3512 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3513 }
3514 elf_section_data (sec)->this_hdr.sh_info = symindx;
3515 }
3516 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3517 {
bcacc0f5
AM
3518 /* The ELF backend linker sets sh_info to -2 when the group
3519 signature symbol is global, and thus the index can't be
3520 set until all local symbols are output. */
53720c49
AM
3521 asection *igroup;
3522 struct bfd_elf_section_data *sec_data;
3523 unsigned long symndx;
3524 unsigned long extsymoff;
bcacc0f5
AM
3525 struct elf_link_hash_entry *h;
3526
53720c49
AM
3527 /* The point of this little dance to the first SHF_GROUP section
3528 then back to the SHT_GROUP section is that this gets us to
3529 the SHT_GROUP in the input object. */
3530 igroup = elf_sec_group (elf_next_in_group (sec));
3531 sec_data = elf_section_data (igroup);
3532 symndx = sec_data->this_hdr.sh_info;
3533 extsymoff = 0;
bcacc0f5
AM
3534 if (!elf_bad_symtab (igroup->owner))
3535 {
3536 Elf_Internal_Shdr *symtab_hdr;
3537
3538 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3539 extsymoff = symtab_hdr->sh_info;
3540 }
3541 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3542 while (h->root.type == bfd_link_hash_indirect
3543 || h->root.type == bfd_link_hash_warning)
3544 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3545
3546 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3547 }
dbb410c3 3548
1126897b 3549 /* The contents won't be allocated for "ld -r" or objcopy. */
0a1b45a2 3550 gas = true;
dbb410c3
AM
3551 if (sec->contents == NULL)
3552 {
0a1b45a2 3553 gas = false;
a50b1753 3554 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3555
3556 /* Arrange for the section to be written out. */
3557 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3558 if (sec->contents == NULL)
3559 {
0a1b45a2 3560 *failedptr = true;
dbb410c3
AM
3561 return;
3562 }
3563 }
3564
eea6121a 3565 loc = sec->contents + sec->size;
dbb410c3 3566
9dce4196
AM
3567 /* Get the pointer to the first section in the group that gas
3568 squirreled away here. objcopy arranges for this to be set to the
3569 start of the input section group. */
3570 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3571
3572 /* First element is a flag word. Rest of section is elf section
3573 indices for all the sections of the group. Write them backwards
3574 just to keep the group in the same order as given in .section
3575 directives, not that it matters. */
3576 while (elt != NULL)
3577 {
9dce4196 3578 asection *s;
9dce4196 3579
9dce4196 3580 s = elt;
415f38a6
AM
3581 if (!gas)
3582 s = s->output_section;
3583 if (s != NULL
3584 && !bfd_is_abs_section (s))
01e1a5bc 3585 {
db4677b8 3586 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3587 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3588
3589 if (elf_sec->rel.hdr != NULL
3590 && (gas
3591 || (input_elf_sec->rel.hdr != NULL
3592 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3593 {
28e07a05 3594 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3595 loc -= 4;
3596 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3597 }
28e07a05
AM
3598 if (elf_sec->rela.hdr != NULL
3599 && (gas
3600 || (input_elf_sec->rela.hdr != NULL
3601 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3602 {
28e07a05 3603 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3604 loc -= 4;
3605 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3606 }
01e1a5bc 3607 loc -= 4;
db4677b8 3608 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3609 }
945906ff 3610 elt = elf_next_in_group (elt);
9dce4196
AM
3611 if (elt == first)
3612 break;
dbb410c3
AM
3613 }
3614
7bdf4127
AB
3615 loc -= 4;
3616 BFD_ASSERT (loc == sec->contents);
dbb410c3 3617
9dce4196 3618 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3619}
3620
bce964aa
AM
3621/* Given NAME, the name of a relocation section stripped of its
3622 .rel/.rela prefix, return the section in ABFD to which the
3623 relocations apply. */
bd53a53a
L
3624
3625asection *
bce964aa
AM
3626_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3627{
3628 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3629 section likely apply to .got.plt or .got section. */
3630 if (get_elf_backend_data (abfd)->want_got_plt
3631 && strcmp (name, ".plt") == 0)
3632 {
3633 asection *sec;
3634
3635 name = ".got.plt";
3636 sec = bfd_get_section_by_name (abfd, name);
3637 if (sec != NULL)
3638 return sec;
3639 name = ".got";
3640 }
3641
3642 return bfd_get_section_by_name (abfd, name);
3643}
3644
3645/* Return the section to which RELOC_SEC applies. */
3646
3647static asection *
3648elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3649{
3650 const char *name;
3651 unsigned int type;
3652 bfd *abfd;
bce964aa 3653 const struct elf_backend_data *bed;
bd53a53a
L
3654
3655 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3656 if (type != SHT_REL && type != SHT_RELA)
3657 return NULL;
3658
3659 /* We look up the section the relocs apply to by name. */
3660 name = reloc_sec->name;
3f3328b8 3661 if (!startswith (name, ".rel"))
bce964aa
AM
3662 return NULL;
3663 name += 4;
3664 if (type == SHT_RELA && *name++ != 'a')
3665 return NULL;
bd53a53a 3666
bd53a53a 3667 abfd = reloc_sec->owner;
bce964aa
AM
3668 bed = get_elf_backend_data (abfd);
3669 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3670}
3671
252b5132
RH
3672/* Assign all ELF section numbers. The dummy first section is handled here
3673 too. The link/info pointers for the standard section types are filled
67411cbf
AM
3674 in here too, while we're at it. LINK_INFO will be 0 when arriving
3675 here for objcopy, and when using the generic ELF linker. */
252b5132 3676
0a1b45a2 3677static bool
da9f89d4 3678assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3679{
3680 struct elf_obj_tdata *t = elf_tdata (abfd);
3681 asection *sec;
3e19fb8f 3682 unsigned int section_number;
252b5132 3683 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3684 struct bfd_elf_section_data *d;
0a1b45a2 3685 bool need_symtab;
446f7ed5 3686 size_t amt;
252b5132
RH
3687
3688 section_number = 1;
3689
2b0f7ef9
JJ
3690 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3691
da9f89d4 3692 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3693 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3694 {
ef53be89 3695 size_t reloc_count = 0;
14f2c699 3696
da9f89d4 3697 /* Put SHT_GROUP sections first. */
04dd1667 3698 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3699 {
5daa8fe7 3700 d = elf_section_data (sec);
da9f89d4
L
3701
3702 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3703 {
5daa8fe7 3704 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3705 {
3706 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3707 bfd_section_list_remove (abfd, sec);
da9f89d4 3708 abfd->section_count--;
da9f89d4 3709 }
08a40648 3710 else
4fbb74a6 3711 d->this_idx = section_number++;
da9f89d4 3712 }
14f2c699
L
3713
3714 /* Count relocations. */
3715 reloc_count += sec->reloc_count;
47cc2cf5 3716 }
14f2c699
L
3717
3718 /* Clear HAS_RELOC if there are no relocations. */
3719 if (reloc_count == 0)
3720 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3721 }
3722
3723 for (sec = abfd->sections; sec; sec = sec->next)
3724 {
3725 d = elf_section_data (sec);
3726
3727 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3728 d->this_idx = section_number++;
3e19fb8f
L
3729 if (d->this_hdr.sh_name != (unsigned int) -1)
3730 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3731 if (d->rel.hdr)
2b0f7ef9 3732 {
d4730f92 3733 d->rel.idx = section_number++;
3e19fb8f
L
3734 if (d->rel.hdr->sh_name != (unsigned int) -1)
3735 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3736 }
d4730f92
BS
3737 else
3738 d->rel.idx = 0;
23bc299b 3739
d4730f92 3740 if (d->rela.hdr)
2b0f7ef9 3741 {
d4730f92 3742 d->rela.idx = section_number++;
3e19fb8f
L
3743 if (d->rela.hdr->sh_name != (unsigned int) -1)
3744 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3745 }
23bc299b 3746 else
d4730f92 3747 d->rela.idx = 0;
252b5132
RH
3748 }
3749
3516e984 3750 need_symtab = (bfd_get_symcount (abfd) > 0
b6d1f70c
AM
3751 || (link_info == NULL
3752 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3753 == HAS_RELOC)));
3516e984 3754 if (need_symtab)
252b5132 3755 {
12bd6957 3756 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3757 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3758 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3759 {
7a6e0d89 3760 elf_section_list *entry;
6a40cf0c
NC
3761
3762 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3763
7a6e0d89 3764 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3765 entry->ndx = section_number++;
3766 elf_symtab_shndx_list (abfd) = entry;
3767 entry->hdr.sh_name
9ad5cbcf 3768 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 3769 ".symtab_shndx", false);
6a40cf0c 3770 if (entry->hdr.sh_name == (unsigned int) -1)
0a1b45a2 3771 return false;
9ad5cbcf 3772 }
12bd6957 3773 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3774 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3775 }
3776
dd905818
NC
3777 elf_shstrtab_sec (abfd) = section_number++;
3778 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3779 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3780
1c52a645
L
3781 if (section_number >= SHN_LORESERVE)
3782 {
695344c0 3783 /* xgettext:c-format */
871b3ab2 3784 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645 3785 abfd, section_number);
0a1b45a2 3786 return false;
1c52a645
L
3787 }
3788
9ad5cbcf 3789 elf_numsections (abfd) = section_number;
252b5132
RH
3790 elf_elfheader (abfd)->e_shnum = section_number;
3791
3792 /* Set up the list of section header pointers, in agreement with the
3793 indices. */
446f7ed5
AM
3794 amt = section_number * sizeof (Elf_Internal_Shdr *);
3795 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
252b5132 3796 if (i_shdrp == NULL)
0a1b45a2 3797 return false;
252b5132 3798
a50b1753 3799 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3800 sizeof (Elf_Internal_Shdr));
252b5132
RH
3801 if (i_shdrp[0] == NULL)
3802 {
3803 bfd_release (abfd, i_shdrp);
0a1b45a2 3804 return false;
252b5132 3805 }
252b5132
RH
3806
3807 elf_elfsections (abfd) = i_shdrp;
3808
12bd6957 3809 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3810 if (need_symtab)
252b5132 3811 {
12bd6957 3812 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3813 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3814 {
6a40cf0c
NC
3815 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3816 BFD_ASSERT (entry != NULL);
3817 i_shdrp[entry->ndx] = & entry->hdr;
3818 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3819 }
12bd6957
AM
3820 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3821 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3822 }
38ce5b11 3823
252b5132
RH
3824 for (sec = abfd->sections; sec; sec = sec->next)
3825 {
252b5132 3826 asection *s;
252b5132 3827
91d6fa6a
NC
3828 d = elf_section_data (sec);
3829
252b5132 3830 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3831 if (d->rel.idx != 0)
3832 i_shdrp[d->rel.idx] = d->rel.hdr;
3833 if (d->rela.idx != 0)
3834 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3835
3836 /* Fill in the sh_link and sh_info fields while we're at it. */
3837
3838 /* sh_link of a reloc section is the section index of the symbol
3839 table. sh_info is the section index of the section to which
3840 the relocation entries apply. */
d4730f92 3841 if (d->rel.idx != 0)
252b5132 3842 {
12bd6957 3843 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3844 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3845 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3846 }
d4730f92 3847 if (d->rela.idx != 0)
23bc299b 3848 {
12bd6957 3849 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3850 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3851 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3852 }
252b5132 3853
38ce5b11
L
3854 /* We need to set up sh_link for SHF_LINK_ORDER. */
3855 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3856 {
3857 s = elf_linked_to_section (sec);
b71702f1
NC
3858 /* We can now have a NULL linked section pointer.
3859 This happens when the sh_link field is 0, which is done
3860 when a linked to section is discarded but the linking
3861 section has been retained for some reason. */
38ce5b11 3862 if (s)
38ce5b11 3863 {
67411cbf
AM
3864 /* Check discarded linkonce section. */
3865 if (discarded_section (s))
38ce5b11 3866 {
67411cbf
AM
3867 asection *kept;
3868 _bfd_error_handler
3869 /* xgettext:c-format */
3870 (_("%pB: sh_link of section `%pA' points to"
3871 " discarded section `%pA' of `%pB'"),
3872 abfd, d->this_hdr.bfd_section, s, s->owner);
3873 /* Point to the kept section if it has the same
3874 size as the discarded one. */
3875 kept = _bfd_elf_check_kept_section (s, link_info);
3876 if (kept == NULL)
f2876037 3877 {
f2876037 3878 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3879 return false;
f2876037 3880 }
67411cbf
AM
3881 s = kept;
3882 }
3883 /* Handle objcopy. */
3884 else if (s->output_section == NULL)
3885 {
3886 _bfd_error_handler
3887 /* xgettext:c-format */
3888 (_("%pB: sh_link of section `%pA' points to"
3889 " removed section `%pA' of `%pB'"),
3890 abfd, d->this_hdr.bfd_section, s, s->owner);
3891 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3892 return false;
f2876037 3893 }
67411cbf 3894 s = s->output_section;
ccd2ec6a
L
3895 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3896 }
38ce5b11
L
3897 }
3898
252b5132
RH
3899 switch (d->this_hdr.sh_type)
3900 {
3901 case SHT_REL:
3902 case SHT_RELA:
3903 /* A reloc section which we are treating as a normal BFD
3904 section. sh_link is the section index of the symbol
3905 table. sh_info is the section index of the section to
3906 which the relocation entries apply. We assume that an
b6d1f70c
AM
3907 allocated reloc section uses the dynamic symbol table
3908 if there is one. Otherwise we guess the normal symbol
3909 table. FIXME: How can we be sure? */
3910 if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
3911 {
3912 s = bfd_get_section_by_name (abfd, ".dynsym");
3913 if (s != NULL)
3914 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3915 }
3916 if (d->this_hdr.sh_link == 0)
3917 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132 3918
bce964aa 3919 s = elf_get_reloc_section (sec);
252b5132 3920 if (s != NULL)
9ef5d938
L
3921 {
3922 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3923 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3924 }
252b5132
RH
3925 break;
3926
3927 case SHT_STRTAB:
3928 /* We assume that a section named .stab*str is a stabs
3929 string section. We look for a section with the same name
3930 but without the trailing ``str'', and set its sh_link
3931 field to point to this section. */
08dedd66 3932 if (startswith (sec->name, ".stab")
252b5132
RH
3933 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3934 {
3935 size_t len;
3936 char *alc;
3937
3938 len = strlen (sec->name);
a50b1753 3939 alc = (char *) bfd_malloc (len - 2);
252b5132 3940 if (alc == NULL)
0a1b45a2 3941 return false;
d4c88bbb 3942 memcpy (alc, sec->name, len - 3);
252b5132
RH
3943 alc[len - 3] = '\0';
3944 s = bfd_get_section_by_name (abfd, alc);
3945 free (alc);
3946 if (s != NULL)
3947 {
3948 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3949
3950 /* This is a .stab section. */
34ca5531 3951 elf_section_data (s)->this_hdr.sh_entsize = 12;
252b5132
RH
3952 }
3953 }
3954 break;
3955
3956 case SHT_DYNAMIC:
3957 case SHT_DYNSYM:
3958 case SHT_GNU_verneed:
3959 case SHT_GNU_verdef:
3960 /* sh_link is the section header index of the string table
3961 used for the dynamic entries, or the symbol table, or the
3962 version strings. */
3963 s = bfd_get_section_by_name (abfd, ".dynstr");
3964 if (s != NULL)
3965 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3966 break;
3967
7f1204bb
JJ
3968 case SHT_GNU_LIBLIST:
3969 /* sh_link is the section header index of the prelink library
08a40648
AM
3970 list used for the dynamic entries, or the symbol table, or
3971 the version strings. */
b6d1f70c
AM
3972 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3973 ? ".dynstr" : ".gnu.libstr"));
7f1204bb
JJ
3974 if (s != NULL)
3975 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3976 break;
3977
252b5132 3978 case SHT_HASH:
fdc90cb4 3979 case SHT_GNU_HASH:
252b5132
RH
3980 case SHT_GNU_versym:
3981 /* sh_link is the section header index of the symbol table
3982 this hash table or version table is for. */
3983 s = bfd_get_section_by_name (abfd, ".dynsym");
3984 if (s != NULL)
3985 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3986 break;
dbb410c3
AM
3987
3988 case SHT_GROUP:
12bd6957 3989 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
3990 }
3991 }
3992
3e19fb8f
L
3993 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3994 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3995 debug section name from .debug_* to .zdebug_* if needed. */
3996
0a1b45a2 3997 return true;
252b5132
RH
3998}
3999
0a1b45a2 4000static bool
217aa764 4001sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4002{
4003 /* If the backend has a special mapping, use it. */
9c5bfbb7 4004 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4005 if (bed->elf_backend_sym_is_global)
4006 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4007
e47bf690 4008 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4009 || bfd_is_und_section (bfd_asymbol_section (sym))
4010 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4011}
4012
76359541
TP
4013/* Filter global symbols of ABFD to include in the import library. All
4014 SYMCOUNT symbols of ABFD can be examined from their pointers in
4015 SYMS. Pointers of symbols to keep should be stored contiguously at
4016 the beginning of that array.
4017
4018 Returns the number of symbols to keep. */
4019
4020unsigned int
4021_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4022 asymbol **syms, long symcount)
4023{
4024 long src_count, dst_count = 0;
4025
4026 for (src_count = 0; src_count < symcount; src_count++)
4027 {
4028 asymbol *sym = syms[src_count];
4029 char *name = (char *) bfd_asymbol_name (sym);
4030 struct bfd_link_hash_entry *h;
4031
4032 if (!sym_is_global (abfd, sym))
4033 continue;
4034
0a1b45a2 4035 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
5df1bc57
AM
4036 if (h == NULL)
4037 continue;
76359541
TP
4038 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4039 continue;
76359541
TP
4040 if (h->linker_def || h->ldscript_def)
4041 continue;
4042
4043 syms[dst_count++] = sym;
4044 }
4045
4046 syms[dst_count] = NULL;
4047
4048 return dst_count;
4049}
4050
5372391b 4051/* Don't output section symbols for sections that are not going to be
c6d8cab4 4052 output, that are duplicates or there is no BFD section. */
5372391b 4053
0a1b45a2 4054static bool
5372391b
AM
4055ignore_section_sym (bfd *abfd, asymbol *sym)
4056{
c6d8cab4
L
4057 elf_symbol_type *type_ptr;
4058
db0c309f 4059 if (sym == NULL)
0a1b45a2 4060 return false;
db0c309f 4061
c6d8cab4 4062 if ((sym->flags & BSF_SECTION_SYM) == 0)
0a1b45a2 4063 return false;
c6d8cab4 4064
d1bcae83
L
4065 /* Ignore the section symbol if it isn't used. */
4066 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
0a1b45a2 4067 return true;
d1bcae83 4068
db0c309f 4069 if (sym->section == NULL)
0a1b45a2 4070 return true;
db0c309f 4071
c1229f84 4072 type_ptr = elf_symbol_from (sym);
c6d8cab4
L
4073 return ((type_ptr != NULL
4074 && type_ptr->internal_elf_sym.st_shndx != 0
4075 && bfd_is_abs_section (sym->section))
4076 || !(sym->section->owner == abfd
db0c309f
NC
4077 || (sym->section->output_section != NULL
4078 && sym->section->output_section->owner == abfd
2633a79c
AM
4079 && sym->section->output_offset == 0)
4080 || bfd_is_abs_section (sym->section)));
5372391b
AM
4081}
4082
2633a79c
AM
4083/* Map symbol from it's internal number to the external number, moving
4084 all local symbols to be at the head of the list. */
4085
0a1b45a2 4086static bool
12bd6957 4087elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4088{
dc810e39 4089 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4090 asymbol **syms = bfd_get_outsymbols (abfd);
4091 asymbol **sect_syms;
dc810e39
AM
4092 unsigned int num_locals = 0;
4093 unsigned int num_globals = 0;
4094 unsigned int num_locals2 = 0;
4095 unsigned int num_globals2 = 0;
7292b3ac 4096 unsigned int max_index = 0;
dc810e39 4097 unsigned int idx;
252b5132
RH
4098 asection *asect;
4099 asymbol **new_syms;
446f7ed5 4100 size_t amt;
252b5132
RH
4101
4102#ifdef DEBUG
4103 fprintf (stderr, "elf_map_symbols\n");
4104 fflush (stderr);
4105#endif
4106
252b5132
RH
4107 for (asect = abfd->sections; asect; asect = asect->next)
4108 {
4109 if (max_index < asect->index)
4110 max_index = asect->index;
4111 }
4112
4113 max_index++;
446f7ed5
AM
4114 amt = max_index * sizeof (asymbol *);
4115 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132 4116 if (sect_syms == NULL)
0a1b45a2 4117 return false;
252b5132 4118 elf_section_syms (abfd) = sect_syms;
4e89ac30 4119 elf_num_section_syms (abfd) = max_index;
252b5132 4120
079e9a2f
AM
4121 /* Init sect_syms entries for any section symbols we have already
4122 decided to output. */
252b5132
RH
4123 for (idx = 0; idx < symcount; idx++)
4124 {
dc810e39 4125 asymbol *sym = syms[idx];
c044fabd 4126
252b5132 4127 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4128 && sym->value == 0
2633a79c
AM
4129 && !ignore_section_sym (abfd, sym)
4130 && !bfd_is_abs_section (sym->section))
252b5132 4131 {
5372391b 4132 asection *sec = sym->section;
252b5132 4133
5372391b
AM
4134 if (sec->owner != abfd)
4135 sec = sec->output_section;
252b5132 4136
5372391b 4137 sect_syms[sec->index] = syms[idx];
252b5132
RH
4138 }
4139 }
4140
252b5132
RH
4141 /* Classify all of the symbols. */
4142 for (idx = 0; idx < symcount; idx++)
4143 {
2633a79c 4144 if (sym_is_global (abfd, syms[idx]))
252b5132 4145 num_globals++;
2633a79c
AM
4146 else if (!ignore_section_sym (abfd, syms[idx]))
4147 num_locals++;
252b5132 4148 }
079e9a2f 4149
5372391b 4150 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4151 sections will already have a section symbol in outsymbols, but
4152 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4153 at least in that case. */
252b5132
RH
4154 for (asect = abfd->sections; asect; asect = asect->next)
4155 {
d1bcae83
L
4156 asymbol *sym = asect->symbol;
4157 /* Don't include ignored section symbols. */
4158 if (!ignore_section_sym (abfd, sym)
4159 && sect_syms[asect->index] == NULL)
252b5132 4160 {
079e9a2f 4161 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4162 num_locals++;
4163 else
4164 num_globals++;
252b5132
RH
4165 }
4166 }
4167
4168 /* Now sort the symbols so the local symbols are first. */
446f7ed5
AM
4169 amt = (num_locals + num_globals) * sizeof (asymbol *);
4170 new_syms = (asymbol **) bfd_alloc (abfd, amt);
252b5132 4171 if (new_syms == NULL)
0a1b45a2 4172 return false;
252b5132
RH
4173
4174 for (idx = 0; idx < symcount; idx++)
4175 {
4176 asymbol *sym = syms[idx];
dc810e39 4177 unsigned int i;
252b5132 4178
2633a79c
AM
4179 if (sym_is_global (abfd, sym))
4180 i = num_locals + num_globals2++;
d1bcae83 4181 /* Don't include ignored section symbols. */
2633a79c 4182 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4183 i = num_locals2++;
4184 else
2633a79c 4185 continue;
252b5132
RH
4186 new_syms[i] = sym;
4187 sym->udata.i = i + 1;
4188 }
4189 for (asect = abfd->sections; asect; asect = asect->next)
4190 {
d1bcae83
L
4191 asymbol *sym = asect->symbol;
4192 if (!ignore_section_sym (abfd, sym)
4193 && sect_syms[asect->index] == NULL)
252b5132 4194 {
dc810e39 4195 unsigned int i;
252b5132 4196
079e9a2f 4197 sect_syms[asect->index] = sym;
252b5132
RH
4198 if (!sym_is_global (abfd, sym))
4199 i = num_locals2++;
4200 else
4201 i = num_locals + num_globals2++;
4202 new_syms[i] = sym;
4203 sym->udata.i = i + 1;
4204 }
4205 }
4206
4207 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4208
12bd6957 4209 *pnum_locals = num_locals;
0a1b45a2 4210 return true;
252b5132
RH
4211}
4212
4213/* Align to the maximum file alignment that could be required for any
4214 ELF data structure. */
4215
268b6b39 4216static inline file_ptr
217aa764 4217align_file_position (file_ptr off, int align)
252b5132
RH
4218{
4219 return (off + align - 1) & ~(align - 1);
4220}
4221
4222/* Assign a file position to a section, optionally aligning to the
4223 required section alignment. */
4224
217aa764
AM
4225file_ptr
4226_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4227 file_ptr offset,
0a1b45a2 4228 bool align)
252b5132 4229{
72de5009 4230 if (align && i_shdrp->sh_addralign > 1)
1f9b1a84 4231 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
252b5132
RH
4232 i_shdrp->sh_offset = offset;
4233 if (i_shdrp->bfd_section != NULL)
4234 i_shdrp->bfd_section->filepos = offset;
4235 if (i_shdrp->sh_type != SHT_NOBITS)
4236 offset += i_shdrp->sh_size;
4237 return offset;
4238}
4239
4240/* Compute the file positions we are going to put the sections at, and
4241 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4242 is not NULL, this is being called by the ELF backend linker. */
4243
0a1b45a2 4244bool
217aa764
AM
4245_bfd_elf_compute_section_file_positions (bfd *abfd,
4246 struct bfd_link_info *link_info)
252b5132 4247{
9c5bfbb7 4248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4249 struct fake_section_arg fsargs;
0a1b45a2 4250 bool failed;
ef10c3ac 4251 struct elf_strtab_hash *strtab = NULL;
252b5132 4252 Elf_Internal_Shdr *shstrtab_hdr;
0a1b45a2 4253 bool need_symtab;
252b5132
RH
4254
4255 if (abfd->output_has_begun)
0a1b45a2 4256 return true;
252b5132
RH
4257
4258 /* Do any elf backend specific processing first. */
4259 if (bed->elf_backend_begin_write_processing)
4260 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4261
ed7e9d0b 4262 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
0a1b45a2 4263 return false;
252b5132 4264
0a1b45a2 4265 fsargs.failed = false;
d4730f92
BS
4266 fsargs.link_info = link_info;
4267 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4268 if (fsargs.failed)
0a1b45a2 4269 return false;
252b5132 4270
da9f89d4 4271 if (!assign_section_numbers (abfd, link_info))
0a1b45a2 4272 return false;
252b5132
RH
4273
4274 /* The backend linker builds symbol table information itself. */
3516e984
L
4275 need_symtab = (link_info == NULL
4276 && (bfd_get_symcount (abfd) > 0
4277 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4278 == HAS_RELOC)));
4279 if (need_symtab)
252b5132
RH
4280 {
4281 /* Non-zero if doing a relocatable link. */
4282 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4283
3d16b64e 4284 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
0a1b45a2 4285 return false;
252b5132
RH
4286 }
4287
0a1b45a2 4288 failed = false;
1126897b 4289 if (link_info == NULL)
dbb410c3 4290 {
1126897b 4291 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4292 if (failed)
0a1b45a2 4293 return false;
dbb410c3
AM
4294 }
4295
252b5132 4296 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
ed7e9d0b 4297 /* sh_name was set in init_file_header. */
252b5132 4298 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4299 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4300 shstrtab_hdr->sh_addr = 0;
946748d5 4301 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4302 shstrtab_hdr->sh_entsize = 0;
4303 shstrtab_hdr->sh_link = 0;
4304 shstrtab_hdr->sh_info = 0;
3e19fb8f 4305 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4306 shstrtab_hdr->sh_addralign = 1;
4307
c84fca4d 4308 if (!assign_file_positions_except_relocs (abfd, link_info))
0a1b45a2 4309 return false;
252b5132 4310
3516e984 4311 if (need_symtab)
252b5132
RH
4312 {
4313 file_ptr off;
4314 Elf_Internal_Shdr *hdr;
4315
12bd6957 4316 off = elf_next_file_pos (abfd);
252b5132 4317
6a40cf0c 4318 hdr = & elf_symtab_hdr (abfd);
0a1b45a2 4319 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4320
6a40cf0c
NC
4321 if (elf_symtab_shndx_list (abfd) != NULL)
4322 {
4323 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4324 if (hdr->sh_size != 0)
0a1b45a2 4325 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6a40cf0c
NC
4326 /* FIXME: What about other symtab_shndx sections in the list ? */
4327 }
9ad5cbcf 4328
252b5132 4329 hdr = &elf_tdata (abfd)->strtab_hdr;
0a1b45a2 4330 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4331
12bd6957 4332 elf_next_file_pos (abfd) = off;
252b5132
RH
4333
4334 /* Now that we know where the .strtab section goes, write it
08a40648 4335 out. */
252b5132 4336 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4337 || ! _bfd_elf_strtab_emit (abfd, strtab))
0a1b45a2 4338 return false;
ef10c3ac 4339 _bfd_elf_strtab_free (strtab);
252b5132
RH
4340 }
4341
0a1b45a2 4342 abfd->output_has_begun = true;
252b5132 4343
0a1b45a2 4344 return true;
252b5132
RH
4345}
4346
8df52eee
AM
4347/* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4348 function effectively returns whether --eh-frame-hdr is given on the
4349 command line. After size_dynamic_sections the result reflects
4350 whether .eh_frame_hdr will actually be output (sizing isn't done
4351 until ldemul_after_allocation). */
4352
4353static asection *
4354elf_eh_frame_hdr (const struct bfd_link_info *info)
4355{
4356 if (info != NULL && is_elf_hash_table (info->hash))
4357 return elf_hash_table (info)->eh_info.hdr_sec;
4358 return NULL;
4359}
4360
8ded5a0f
AM
4361/* Make an initial estimate of the size of the program header. If we
4362 get the number wrong here, we'll redo section placement. */
4363
4364static bfd_size_type
4365get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4366{
4367 size_t segs;
4368 asection *s;
2b05f1b7 4369 const struct elf_backend_data *bed;
8ded5a0f
AM
4370
4371 /* Assume we will need exactly two PT_LOAD segments: one for text
4372 and one for data. */
4373 segs = 2;
4374
4375 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4376 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4377 {
4378 /* If we have a loadable interpreter section, we need a
4379 PT_INTERP segment. In this case, assume we also need a
4380 PT_PHDR segment, although that may not be true for all
4381 targets. */
e9a38e0f 4382 segs += 2;
8ded5a0f
AM
4383 }
4384
4385 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4386 {
4387 /* We need a PT_DYNAMIC segment. */
4388 ++segs;
f210dcff 4389 }
08a40648 4390
ceae84aa 4391 if (info != NULL && info->relro)
f210dcff
L
4392 {
4393 /* We need a PT_GNU_RELRO segment. */
4394 ++segs;
8ded5a0f
AM
4395 }
4396
8df52eee 4397 if (elf_eh_frame_hdr (info))
8ded5a0f
AM
4398 {
4399 /* We need a PT_GNU_EH_FRAME segment. */
4400 ++segs;
4401 }
4402
12bd6957 4403 if (elf_stack_flags (abfd))
8ded5a0f 4404 {
2b05f1b7
L
4405 /* We need a PT_GNU_STACK segment. */
4406 ++segs;
4407 }
94b11780 4408
0a59decb
L
4409 s = bfd_get_section_by_name (abfd,
4410 NOTE_GNU_PROPERTY_SECTION_NAME);
4411 if (s != NULL && s->size != 0)
4412 {
4413 /* We need a PT_GNU_PROPERTY segment. */
4414 ++segs;
4415 }
4416
2b05f1b7
L
4417 for (s = abfd->sections; s != NULL; s = s->next)
4418 {
8ded5a0f 4419 if ((s->flags & SEC_LOAD) != 0
23e463ed 4420 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4421 {
23e463ed 4422 unsigned int alignment_power;
8ded5a0f
AM
4423 /* We need a PT_NOTE segment. */
4424 ++segs;
23e463ed
L
4425 /* Try to create just one PT_NOTE segment for all adjacent
4426 loadable SHT_NOTE sections. gABI requires that within a
4427 PT_NOTE segment (and also inside of each SHT_NOTE section)
4428 each note should have the same alignment. So we check
4429 whether the sections are correctly aligned. */
4430 alignment_power = s->alignment_power;
4431 while (s->next != NULL
4432 && s->next->alignment_power == alignment_power
4433 && (s->next->flags & SEC_LOAD) != 0
4434 && elf_section_type (s->next) == SHT_NOTE)
4435 s = s->next;
8ded5a0f
AM
4436 }
4437 }
4438
4439 for (s = abfd->sections; s != NULL; s = s->next)
4440 {
4441 if (s->flags & SEC_THREAD_LOCAL)
4442 {
4443 /* We need a PT_TLS segment. */
4444 ++segs;
4445 break;
4446 }
4447 }
4448
2b05f1b7 4449 bed = get_elf_backend_data (abfd);
a91e1603 4450
df3a023b
AM
4451 if ((abfd->flags & D_PAGED) != 0
4452 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4453 {
4454 /* Add a PT_GNU_MBIND segment for each mbind section. */
c410035d
AM
4455 bfd_vma commonpagesize;
4456 unsigned int page_align_power;
4457
4458 if (info != NULL)
4459 commonpagesize = info->commonpagesize;
4460 else
4461 commonpagesize = bed->commonpagesize;
4462 page_align_power = bfd_log2 (commonpagesize);
df3a023b
AM
4463 for (s = abfd->sections; s != NULL; s = s->next)
4464 if (elf_section_flags (s) & SHF_GNU_MBIND)
4465 {
4466 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4467 {
4468 _bfd_error_handler
4469 /* xgettext:c-format */
4470 (_("%pB: GNU_MBIND section `%pA' has invalid "
4471 "sh_info field: %d"),
4472 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4473 continue;
4474 }
4475 /* Align mbind section to page size. */
4476 if (s->alignment_power < page_align_power)
4477 s->alignment_power = page_align_power;
4478 segs ++;
4479 }
4480 }
4481
4482 /* Let the backend count up any program headers it might need. */
4483 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4484 {
4485 int a;
4486
4487 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4488 if (a == -1)
4489 abort ();
4490 segs += a;
4491 }
4492
4493 return segs * bed->s->sizeof_phdr;
4494}
4495
2ea37f1c
NC
4496/* Find the segment that contains the output_section of section. */
4497
4498Elf_Internal_Phdr *
4499_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4500{
4501 struct elf_segment_map *m;
4502 Elf_Internal_Phdr *p;
4503
12bd6957 4504 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4505 m != NULL;
4506 m = m->next, p++)
4507 {
4508 int i;
4509
4510 for (i = m->count - 1; i >= 0; i--)
4511 if (m->sections[i] == section)
4512 return p;
4513 }
4514
4515 return NULL;
4516}
4517
252b5132
RH
4518/* Create a mapping from a set of sections to a program segment. */
4519
217aa764
AM
4520static struct elf_segment_map *
4521make_mapping (bfd *abfd,
4522 asection **sections,
4523 unsigned int from,
4524 unsigned int to,
0a1b45a2 4525 bool phdr)
252b5132
RH
4526{
4527 struct elf_segment_map *m;
4528 unsigned int i;
4529 asection **hdrpp;
986f0783 4530 size_t amt;
252b5132 4531
00bee008
AM
4532 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4533 amt += (to - from) * sizeof (asection *);
a50b1753 4534 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4535 if (m == NULL)
4536 return NULL;
4537 m->next = NULL;
4538 m->p_type = PT_LOAD;
4539 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4540 m->sections[i - from] = *hdrpp;
4541 m->count = to - from;
4542
4543 if (from == 0 && phdr)
4544 {
4545 /* Include the headers in the first PT_LOAD segment. */
4546 m->includes_filehdr = 1;
4547 m->includes_phdrs = 1;
4548 }
4549
4550 return m;
4551}
4552
229fcec5
MM
4553/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4554 on failure. */
4555
4556struct elf_segment_map *
4557_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4558{
4559 struct elf_segment_map *m;
4560
a50b1753 4561 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4562 sizeof (struct elf_segment_map));
229fcec5
MM
4563 if (m == NULL)
4564 return NULL;
4565 m->next = NULL;
4566 m->p_type = PT_DYNAMIC;
4567 m->count = 1;
4568 m->sections[0] = dynsec;
08a40648 4569
229fcec5
MM
4570 return m;
4571}
4572
8ded5a0f 4573/* Possibly add or remove segments from the segment map. */
252b5132 4574
0a1b45a2 4575static bool
3dea8fca
AM
4576elf_modify_segment_map (bfd *abfd,
4577 struct bfd_link_info *info,
0a1b45a2 4578 bool remove_empty_load)
252b5132 4579{
252e386e 4580 struct elf_segment_map **m;
8ded5a0f 4581 const struct elf_backend_data *bed;
252b5132 4582
8ded5a0f
AM
4583 /* The placement algorithm assumes that non allocated sections are
4584 not in PT_LOAD segments. We ensure this here by removing such
4585 sections from the segment map. We also remove excluded
252e386e
AM
4586 sections. Finally, any PT_LOAD segment without sections is
4587 removed. */
12bd6957 4588 m = &elf_seg_map (abfd);
252e386e 4589 while (*m)
8ded5a0f
AM
4590 {
4591 unsigned int i, new_count;
252b5132 4592
252e386e 4593 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4594 {
252e386e
AM
4595 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4596 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4597 || (*m)->p_type != PT_LOAD))
8ded5a0f 4598 {
252e386e
AM
4599 (*m)->sections[new_count] = (*m)->sections[i];
4600 new_count++;
8ded5a0f
AM
4601 }
4602 }
252e386e 4603 (*m)->count = new_count;
252b5132 4604
1a9ccd70
NC
4605 if (remove_empty_load
4606 && (*m)->p_type == PT_LOAD
4607 && (*m)->count == 0
4608 && !(*m)->includes_phdrs)
252e386e
AM
4609 *m = (*m)->next;
4610 else
4611 m = &(*m)->next;
8ded5a0f 4612 }
252b5132 4613
8ded5a0f
AM
4614 bed = get_elf_backend_data (abfd);
4615 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4616 {
252e386e 4617 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
0a1b45a2 4618 return false;
252b5132 4619 }
252b5132 4620
0a1b45a2 4621 return true;
8ded5a0f 4622}
252b5132 4623
dbc88fc1
AM
4624#define IS_TBSS(s) \
4625 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4626
e2cbf4df
L
4627/* Set up a mapping from BFD sections to program segments. Update
4628 NEED_LAYOUT if the section layout is changed. */
252b5132 4629
0a1b45a2 4630bool
e2cbf4df
L
4631_bfd_elf_map_sections_to_segments (bfd *abfd,
4632 struct bfd_link_info *info,
23cc1de5 4633 bool *need_layout)
8ded5a0f
AM
4634{
4635 unsigned int count;
4636 struct elf_segment_map *m;
4637 asection **sections = NULL;
4638 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 4639 bool no_user_phdrs;
252b5132 4640
12bd6957 4641 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4642
4643 if (info != NULL)
23cc1de5
L
4644 {
4645 info->user_phdrs = !no_user_phdrs;
4646
4647 /* Size the relative relocations if DT_RELR is enabled. */
4648 if (info->enable_dt_relr
4649 && need_layout != NULL
4650 && bed->size_relative_relocs
4651 && !bed->size_relative_relocs (info, need_layout))
4652 info->callbacks->einfo
4653 (_("%F%P: failed to size relative relocations\n"));
4654 }
d324f6d6 4655
3dea8fca 4656 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4657 {
8ded5a0f
AM
4658 asection *s;
4659 unsigned int i;
4660 struct elf_segment_map *mfirst;
4661 struct elf_segment_map **pm;
4662 asection *last_hdr;
4663 bfd_vma last_size;
00bee008 4664 unsigned int hdr_index;
8ded5a0f
AM
4665 bfd_vma maxpagesize;
4666 asection **hdrpp;
0a1b45a2
AM
4667 bool phdr_in_segment;
4668 bool writable;
4669 bool executable;
446f7ed5 4670 unsigned int tls_count = 0;
8ded5a0f 4671 asection *first_tls = NULL;
a91e1603 4672 asection *first_mbind = NULL;
8ded5a0f 4673 asection *dynsec, *eh_frame_hdr;
446f7ed5 4674 size_t amt;
66631823
CE
4675 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4676 bfd_size_type phdr_size; /* Octets/bytes. */
502794d4 4677 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 4678
8ded5a0f 4679 /* Select the allocated sections, and sort them. */
252b5132 4680
446f7ed5
AM
4681 amt = bfd_count_sections (abfd) * sizeof (asection *);
4682 sections = (asection **) bfd_malloc (amt);
8ded5a0f 4683 if (sections == NULL)
252b5132 4684 goto error_return;
252b5132 4685
8d06853e
AM
4686 /* Calculate top address, avoiding undefined behaviour of shift
4687 left operator when shift count is equal to size of type
4688 being shifted. */
4689 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4690 addr_mask = (addr_mask << 1) + 1;
4691
8ded5a0f
AM
4692 i = 0;
4693 for (s = abfd->sections; s != NULL; s = s->next)
4694 {
4695 if ((s->flags & SEC_ALLOC) != 0)
4696 {
48db3297
AM
4697 /* target_index is unused until bfd_elf_final_link
4698 starts output of section symbols. Use it to make
4699 qsort stable. */
4700 s->target_index = i;
8ded5a0f
AM
4701 sections[i] = s;
4702 ++i;
8d06853e 4703 /* A wrapping section potentially clashes with header. */
66631823
CE
4704 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4705 wrap_to = (s->lma + s->size / opb) & addr_mask;
8ded5a0f
AM
4706 }
4707 }
4708 BFD_ASSERT (i <= bfd_count_sections (abfd));
4709 count = i;
252b5132 4710
8ded5a0f 4711 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4712
64029e93
AM
4713 phdr_size = elf_program_header_size (abfd);
4714 if (phdr_size == (bfd_size_type) -1)
4715 phdr_size = get_program_header_size (abfd, info);
4716 phdr_size += bed->s->sizeof_ehdr;
502794d4
CE
4717 /* phdr_size is compared to LMA values which are in bytes. */
4718 phdr_size /= opb;
c410035d
AM
4719 if (info != NULL)
4720 maxpagesize = info->maxpagesize;
4721 else
4722 maxpagesize = bed->maxpagesize;
64029e93
AM
4723 if (maxpagesize == 0)
4724 maxpagesize = 1;
4725 phdr_in_segment = info != NULL && info->load_phdrs;
4726 if (count != 0
4727 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4728 >= (phdr_size & (maxpagesize - 1))))
4729 /* For compatibility with old scripts that may not be using
4730 SIZEOF_HEADERS, add headers when it looks like space has
4731 been left for them. */
0a1b45a2 4732 phdr_in_segment = true;
252b5132 4733
64029e93 4734 /* Build the mapping. */
8ded5a0f
AM
4735 mfirst = NULL;
4736 pm = &mfirst;
252b5132 4737
8ded5a0f
AM
4738 /* If we have a .interp section, then create a PT_PHDR segment for
4739 the program headers and a PT_INTERP segment for the .interp
4740 section. */
4741 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4742 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4743 {
4744 amt = sizeof (struct elf_segment_map);
a50b1753 4745 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4746 if (m == NULL)
4747 goto error_return;
4748 m->next = NULL;
4749 m->p_type = PT_PHDR;
f882209d 4750 m->p_flags = PF_R;
8ded5a0f
AM
4751 m->p_flags_valid = 1;
4752 m->includes_phdrs = 1;
0a1b45a2 4753 phdr_in_segment = true;
8ded5a0f
AM
4754 *pm = m;
4755 pm = &m->next;
252b5132 4756
8ded5a0f 4757 amt = sizeof (struct elf_segment_map);
a50b1753 4758 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4759 if (m == NULL)
4760 goto error_return;
4761 m->next = NULL;
4762 m->p_type = PT_INTERP;
4763 m->count = 1;
4764 m->sections[0] = s;
4765
4766 *pm = m;
4767 pm = &m->next;
252b5132 4768 }
8ded5a0f
AM
4769
4770 /* Look through the sections. We put sections in the same program
4771 segment when the start of the second section can be placed within
4772 a few bytes of the end of the first section. */
4773 last_hdr = NULL;
4774 last_size = 0;
00bee008 4775 hdr_index = 0;
0a1b45a2
AM
4776 writable = false;
4777 executable = false;
8ded5a0f
AM
4778 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4779 if (dynsec != NULL
4780 && (dynsec->flags & SEC_LOAD) == 0)
4781 dynsec = NULL;
4782
64029e93 4783 if ((abfd->flags & D_PAGED) == 0)
0a1b45a2 4784 phdr_in_segment = false;
64029e93 4785
8ded5a0f
AM
4786 /* Deal with -Ttext or something similar such that the first section
4787 is not adjacent to the program headers. This is an
4788 approximation, since at this point we don't know exactly how many
4789 program headers we will need. */
64029e93 4790 if (phdr_in_segment && count > 0)
252b5132 4791 {
66631823 4792 bfd_vma phdr_lma; /* Bytes. */
0a1b45a2 4793 bool separate_phdr = false;
64029e93
AM
4794
4795 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4796 if (info != NULL
4797 && info->separate_code
4798 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4799 {
64029e93
AM
4800 /* If data sections should be separate from code and
4801 thus not executable, and the first section is
4802 executable then put the file and program headers in
4803 their own PT_LOAD. */
0a1b45a2 4804 separate_phdr = true;
64029e93
AM
4805 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4806 == (sections[0]->lma & addr_mask & -maxpagesize)))
4807 {
4808 /* The file and program headers are currently on the
4809 same page as the first section. Put them on the
4810 previous page if we can. */
4811 if (phdr_lma >= maxpagesize)
4812 phdr_lma -= maxpagesize;
4813 else
0a1b45a2 4814 separate_phdr = false;
64029e93
AM
4815 }
4816 }
4817 if ((sections[0]->lma & addr_mask) < phdr_lma
4818 || (sections[0]->lma & addr_mask) < phdr_size)
4819 /* If file and program headers would be placed at the end
4820 of memory then it's probably better to omit them. */
0a1b45a2 4821 phdr_in_segment = false;
64029e93
AM
4822 else if (phdr_lma < wrap_to)
4823 /* If a section wraps around to where we'll be placing
4824 file and program headers, then the headers will be
4825 overwritten. */
0a1b45a2 4826 phdr_in_segment = false;
64029e93
AM
4827 else if (separate_phdr)
4828 {
4829 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4830 if (m == NULL)
4831 goto error_return;
66631823 4832 m->p_paddr = phdr_lma * opb;
64029e93
AM
4833 m->p_vaddr_offset
4834 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4835 m->p_paddr_valid = 1;
4836 *pm = m;
4837 pm = &m->next;
0a1b45a2 4838 phdr_in_segment = false;
1a9ccd70 4839 }
252b5132
RH
4840 }
4841
8ded5a0f 4842 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4843 {
8ded5a0f 4844 asection *hdr;
0a1b45a2 4845 bool new_segment;
8ded5a0f
AM
4846
4847 hdr = *hdrpp;
4848
4849 /* See if this section and the last one will fit in the same
4850 segment. */
4851
4852 if (last_hdr == NULL)
4853 {
4854 /* If we don't have a segment yet, then we don't need a new
4855 one (we build the last one after this loop). */
0a1b45a2 4856 new_segment = false;
8ded5a0f
AM
4857 }
4858 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4859 {
4860 /* If this section has a different relation between the
4861 virtual address and the load address, then we need a new
4862 segment. */
0a1b45a2 4863 new_segment = true;
8ded5a0f 4864 }
b5599592
AM
4865 else if (hdr->lma < last_hdr->lma + last_size
4866 || last_hdr->lma + last_size < last_hdr->lma)
4867 {
4868 /* If this section has a load address that makes it overlap
4869 the previous section, then we need a new segment. */
0a1b45a2 4870 new_segment = true;
b5599592 4871 }
76cb3a89
AM
4872 else if ((abfd->flags & D_PAGED) != 0
4873 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4874 == (hdr->lma & -maxpagesize)))
4875 {
4876 /* If we are demand paged then we can't map two disk
4877 pages onto the same memory page. */
0a1b45a2 4878 new_segment = false;
76cb3a89 4879 }
39948a60
NC
4880 /* In the next test we have to be careful when last_hdr->lma is close
4881 to the end of the address space. If the aligned address wraps
4882 around to the start of the address space, then there are no more
4883 pages left in memory and it is OK to assume that the current
4884 section can be included in the current segment. */
76cb3a89
AM
4885 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4886 + maxpagesize > last_hdr->lma)
4887 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4888 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4889 {
4890 /* If putting this section in this segment would force us to
4891 skip a page in the segment, then we need a new segment. */
0a1b45a2 4892 new_segment = true;
8ded5a0f
AM
4893 }
4894 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4895 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4896 {
e5654c0f
AM
4897 /* We don't want to put a loaded section after a
4898 nonloaded (ie. bss style) section in the same segment
4899 as that will force the non-loaded section to be loaded.
76cb3a89 4900 Consider .tbss sections as loaded for this purpose. */
0a1b45a2 4901 new_segment = true;
8ded5a0f
AM
4902 }
4903 else if ((abfd->flags & D_PAGED) == 0)
4904 {
4905 /* If the file is not demand paged, which means that we
4906 don't require the sections to be correctly aligned in the
4907 file, then there is no other reason for a new segment. */
0a1b45a2 4908 new_segment = false;
8ded5a0f 4909 }
2888249f
L
4910 else if (info != NULL
4911 && info->separate_code
4912 && executable != ((hdr->flags & SEC_CODE) != 0))
4913 {
0a1b45a2 4914 new_segment = true;
2888249f 4915 }
8ded5a0f 4916 else if (! writable
76cb3a89 4917 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4918 {
4919 /* We don't want to put a writable section in a read only
76cb3a89 4920 segment. */
0a1b45a2 4921 new_segment = true;
8ded5a0f
AM
4922 }
4923 else
4924 {
4925 /* Otherwise, we can use the same segment. */
0a1b45a2 4926 new_segment = false;
8ded5a0f
AM
4927 }
4928
2889e75b 4929 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4930 if (last_hdr != NULL
4931 && info != NULL
4932 && info->callbacks->override_segment_assignment != NULL)
4933 new_segment
4934 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4935 last_hdr,
4936 new_segment);
2889e75b 4937
8ded5a0f
AM
4938 if (! new_segment)
4939 {
4940 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4941 writable = true;
2888249f 4942 if ((hdr->flags & SEC_CODE) != 0)
0a1b45a2 4943 executable = true;
8ded5a0f
AM
4944 last_hdr = hdr;
4945 /* .tbss sections effectively have zero size. */
502794d4 4946 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
8ded5a0f
AM
4947 continue;
4948 }
4949
4950 /* We need a new program segment. We must create a new program
00bee008 4951 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4952
00bee008 4953 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4954 if (m == NULL)
4955 goto error_return;
4956
4957 *pm = m;
4958 pm = &m->next;
4959
252b5132 4960 if ((hdr->flags & SEC_READONLY) == 0)
0a1b45a2 4961 writable = true;
8ded5a0f 4962 else
0a1b45a2 4963 writable = false;
8ded5a0f 4964
2888249f 4965 if ((hdr->flags & SEC_CODE) == 0)
0a1b45a2 4966 executable = false;
2888249f 4967 else
0a1b45a2 4968 executable = true;
2888249f 4969
baaff79e
JJ
4970 last_hdr = hdr;
4971 /* .tbss sections effectively have zero size. */
502794d4 4972 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
00bee008 4973 hdr_index = i;
0a1b45a2 4974 phdr_in_segment = false;
252b5132
RH
4975 }
4976
86b2281f
AM
4977 /* Create a final PT_LOAD program segment, but not if it's just
4978 for .tbss. */
4979 if (last_hdr != NULL
00bee008 4980 && (i - hdr_index != 1
dbc88fc1 4981 || !IS_TBSS (last_hdr)))
8ded5a0f 4982 {
00bee008 4983 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4984 if (m == NULL)
4985 goto error_return;
252b5132 4986
8ded5a0f
AM
4987 *pm = m;
4988 pm = &m->next;
4989 }
252b5132 4990
8ded5a0f
AM
4991 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4992 if (dynsec != NULL)
4993 {
4994 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4995 if (m == NULL)
4996 goto error_return;
4997 *pm = m;
4998 pm = &m->next;
4999 }
252b5132 5000
23e463ed 5001 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
5002 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5003 because if we link together nonloadable .note sections and
5004 loadable .note sections, we will generate two .note sections
23e463ed 5005 in the output file. */
8ded5a0f
AM
5006 for (s = abfd->sections; s != NULL; s = s->next)
5007 {
5008 if ((s->flags & SEC_LOAD) != 0
23e463ed 5009 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 5010 {
1c5265b5 5011 asection *s2;
23e463ed 5012 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
5013
5014 count = 1;
23e463ed
L
5015 for (s2 = s; s2->next != NULL; s2 = s2->next)
5016 {
5017 if (s2->next->alignment_power == alignment_power
5018 && (s2->next->flags & SEC_LOAD) != 0
5019 && elf_section_type (s2->next) == SHT_NOTE
66631823 5020 && align_power (s2->lma + s2->size / opb,
23e463ed
L
5021 alignment_power)
5022 == s2->next->lma)
5023 count++;
5024 else
5025 break;
5026 }
00bee008
AM
5027 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5028 amt += count * sizeof (asection *);
a50b1753 5029 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5030 if (m == NULL)
5031 goto error_return;
5032 m->next = NULL;
5033 m->p_type = PT_NOTE;
1c5265b5
JJ
5034 m->count = count;
5035 while (count > 1)
5036 {
5037 m->sections[m->count - count--] = s;
5038 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5039 s = s->next;
5040 }
5041 m->sections[m->count - 1] = s;
5042 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5043 *pm = m;
5044 pm = &m->next;
5045 }
5046 if (s->flags & SEC_THREAD_LOCAL)
5047 {
5048 if (! tls_count)
5049 first_tls = s;
5050 tls_count++;
5051 }
a91e1603
L
5052 if (first_mbind == NULL
5053 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5054 first_mbind = s;
8ded5a0f 5055 }
252b5132 5056
8ded5a0f
AM
5057 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5058 if (tls_count > 0)
5059 {
00bee008
AM
5060 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5061 amt += tls_count * sizeof (asection *);
a50b1753 5062 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5063 if (m == NULL)
5064 goto error_return;
5065 m->next = NULL;
5066 m->p_type = PT_TLS;
5067 m->count = tls_count;
5068 /* Mandated PF_R. */
5069 m->p_flags = PF_R;
5070 m->p_flags_valid = 1;
d923cae0 5071 s = first_tls;
446f7ed5 5072 for (i = 0; i < tls_count; ++i)
8ded5a0f 5073 {
d923cae0
L
5074 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5075 {
5076 _bfd_error_handler
871b3ab2 5077 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5078 s = first_tls;
5079 i = 0;
446f7ed5 5080 while (i < tls_count)
d923cae0
L
5081 {
5082 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5083 {
871b3ab2 5084 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5085 i++;
5086 }
5087 else
871b3ab2 5088 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5089 s = s->next;
5090 }
5091 bfd_set_error (bfd_error_bad_value);
5092 goto error_return;
5093 }
5094 m->sections[i] = s;
5095 s = s->next;
8ded5a0f 5096 }
252b5132 5097
8ded5a0f
AM
5098 *pm = m;
5099 pm = &m->next;
5100 }
252b5132 5101
df3a023b
AM
5102 if (first_mbind
5103 && (abfd->flags & D_PAGED) != 0
5104 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5105 for (s = first_mbind; s != NULL; s = s->next)
5106 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5107 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5108 {
5109 /* Mandated PF_R. */
5110 unsigned long p_flags = PF_R;
5111 if ((s->flags & SEC_READONLY) == 0)
5112 p_flags |= PF_W;
5113 if ((s->flags & SEC_CODE) != 0)
5114 p_flags |= PF_X;
5115
5116 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5117 m = bfd_zalloc (abfd, amt);
5118 if (m == NULL)
5119 goto error_return;
5120 m->next = NULL;
5121 m->p_type = (PT_GNU_MBIND_LO
5122 + elf_section_data (s)->this_hdr.sh_info);
5123 m->count = 1;
5124 m->p_flags_valid = 1;
5125 m->sections[0] = s;
5126 m->p_flags = p_flags;
5127
5128 *pm = m;
5129 pm = &m->next;
5130 }
5131
0a59decb
L
5132 s = bfd_get_section_by_name (abfd,
5133 NOTE_GNU_PROPERTY_SECTION_NAME);
5134 if (s != NULL && s->size != 0)
5135 {
5136 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5137 m = bfd_zalloc (abfd, amt);
5138 if (m == NULL)
5139 goto error_return;
5140 m->next = NULL;
5141 m->p_type = PT_GNU_PROPERTY;
5142 m->count = 1;
5143 m->p_flags_valid = 1;
5144 m->sections[0] = s;
5145 m->p_flags = PF_R;
5146 *pm = m;
5147 pm = &m->next;
5148 }
5149
8ded5a0f
AM
5150 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5151 segment. */
8df52eee 5152 eh_frame_hdr = elf_eh_frame_hdr (info);
8ded5a0f
AM
5153 if (eh_frame_hdr != NULL
5154 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5155 {
dc810e39 5156 amt = sizeof (struct elf_segment_map);
a50b1753 5157 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5158 if (m == NULL)
5159 goto error_return;
5160 m->next = NULL;
8ded5a0f 5161 m->p_type = PT_GNU_EH_FRAME;
252b5132 5162 m->count = 1;
8ded5a0f 5163 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5164
5165 *pm = m;
5166 pm = &m->next;
5167 }
13ae64f3 5168
12bd6957 5169 if (elf_stack_flags (abfd))
13ae64f3 5170 {
8ded5a0f 5171 amt = sizeof (struct elf_segment_map);
a50b1753 5172 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5173 if (m == NULL)
5174 goto error_return;
5175 m->next = NULL;
2b05f1b7 5176 m->p_type = PT_GNU_STACK;
12bd6957 5177 m->p_flags = elf_stack_flags (abfd);
04c3a755 5178 m->p_align = bed->stack_align;
8ded5a0f 5179 m->p_flags_valid = 1;
04c3a755
NS
5180 m->p_align_valid = m->p_align != 0;
5181 if (info->stacksize > 0)
5182 {
5183 m->p_size = info->stacksize;
5184 m->p_size_valid = 1;
5185 }
252b5132 5186
8ded5a0f
AM
5187 *pm = m;
5188 pm = &m->next;
5189 }
65765700 5190
ceae84aa 5191 if (info != NULL && info->relro)
8ded5a0f 5192 {
f210dcff
L
5193 for (m = mfirst; m != NULL; m = m->next)
5194 {
3832a4d8
AM
5195 if (m->p_type == PT_LOAD
5196 && m->count != 0
5197 && m->sections[0]->vma >= info->relro_start
5198 && m->sections[0]->vma < info->relro_end)
f210dcff 5199 {
3832a4d8
AM
5200 i = m->count;
5201 while (--i != (unsigned) -1)
ec2e748a
NC
5202 {
5203 if (m->sections[i]->size > 0
5204 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5205 == (SEC_LOAD | SEC_HAS_CONTENTS))
5206 break;
5207 }
3832a4d8 5208
43a8475c 5209 if (i != (unsigned) -1)
f210dcff
L
5210 break;
5211 }
be01b344 5212 }
f210dcff
L
5213
5214 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5215 if (m != NULL)
5216 {
5217 amt = sizeof (struct elf_segment_map);
a50b1753 5218 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5219 if (m == NULL)
5220 goto error_return;
5221 m->next = NULL;
5222 m->p_type = PT_GNU_RELRO;
f210dcff
L
5223 *pm = m;
5224 pm = &m->next;
5225 }
8ded5a0f 5226 }
9ee5e499 5227
8ded5a0f 5228 free (sections);
12bd6957 5229 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5230 }
5231
3dea8fca 5232 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
0a1b45a2 5233 return false;
8c37241b 5234
12bd6957 5235 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5236 ++count;
12bd6957 5237 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5238
0a1b45a2 5239 return true;
252b5132
RH
5240
5241 error_return:
c9594989 5242 free (sections);
0a1b45a2 5243 return false;
252b5132
RH
5244}
5245
5246/* Sort sections by address. */
5247
5248static int
217aa764 5249elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5250{
5251 const asection *sec1 = *(const asection **) arg1;
5252 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5253 bfd_size_type size1, size2;
252b5132
RH
5254
5255 /* Sort by LMA first, since this is the address used to
5256 place the section into a segment. */
5257 if (sec1->lma < sec2->lma)
5258 return -1;
5259 else if (sec1->lma > sec2->lma)
5260 return 1;
5261
5262 /* Then sort by VMA. Normally the LMA and the VMA will be
5263 the same, and this will do nothing. */
5264 if (sec1->vma < sec2->vma)
5265 return -1;
5266 else if (sec1->vma > sec2->vma)
5267 return 1;
5268
5269 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5270
8d748d1d
AM
5271#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5272 && (x)->size != 0)
252b5132
RH
5273
5274 if (TOEND (sec1))
5275 {
48db3297 5276 if (!TOEND (sec2))
252b5132
RH
5277 return 1;
5278 }
00a7cdc5 5279 else if (TOEND (sec2))
252b5132
RH
5280 return -1;
5281
5282#undef TOEND
5283
00a7cdc5
NC
5284 /* Sort by size, to put zero sized sections
5285 before others at the same address. */
252b5132 5286
eea6121a
AM
5287 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5288 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5289
5290 if (size1 < size2)
252b5132 5291 return -1;
eecdbe52 5292 if (size1 > size2)
252b5132
RH
5293 return 1;
5294
5295 return sec1->target_index - sec2->target_index;
5296}
5297
30fe1832
AM
5298/* This qsort comparison functions sorts PT_LOAD segments first and
5299 by p_paddr, for assign_file_positions_for_load_sections. */
5300
5301static int
5302elf_sort_segments (const void *arg1, const void *arg2)
5303{
5304 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5305 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5306
5307 if (m1->p_type != m2->p_type)
5308 {
5309 if (m1->p_type == PT_NULL)
5310 return 1;
5311 if (m2->p_type == PT_NULL)
5312 return -1;
5313 return m1->p_type < m2->p_type ? -1 : 1;
5314 }
5315 if (m1->includes_filehdr != m2->includes_filehdr)
5316 return m1->includes_filehdr ? -1 : 1;
5317 if (m1->no_sort_lma != m2->no_sort_lma)
5318 return m1->no_sort_lma ? -1 : 1;
5319 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5320 {
4b3ecb3b 5321 bfd_vma lma1, lma2; /* Octets. */
30fe1832
AM
5322 lma1 = 0;
5323 if (m1->p_paddr_valid)
4b3ecb3b 5324 lma1 = m1->p_paddr;
30fe1832 5325 else if (m1->count != 0)
4b3ecb3b
AM
5326 {
5327 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5328 m1->sections[0]);
5329 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5330 }
30fe1832
AM
5331 lma2 = 0;
5332 if (m2->p_paddr_valid)
4b3ecb3b 5333 lma2 = m2->p_paddr;
30fe1832 5334 else if (m2->count != 0)
4b3ecb3b
AM
5335 {
5336 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5337 m2->sections[0]);
5338 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5339 }
30fe1832
AM
5340 if (lma1 != lma2)
5341 return lma1 < lma2 ? -1 : 1;
5342 }
5343 if (m1->idx != m2->idx)
5344 return m1->idx < m2->idx ? -1 : 1;
5345 return 0;
5346}
5347
340b6d91
AC
5348/* Ian Lance Taylor writes:
5349
5350 We shouldn't be using % with a negative signed number. That's just
5351 not good. We have to make sure either that the number is not
5352 negative, or that the number has an unsigned type. When the types
5353 are all the same size they wind up as unsigned. When file_ptr is a
5354 larger signed type, the arithmetic winds up as signed long long,
5355 which is wrong.
5356
5357 What we're trying to say here is something like ``increase OFF by
5358 the least amount that will cause it to be equal to the VMA modulo
5359 the page size.'' */
5360/* In other words, something like:
5361
5362 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5363 off_offset = off % bed->maxpagesize;
5364 if (vma_offset < off_offset)
5365 adjustment = vma_offset + bed->maxpagesize - off_offset;
5366 else
5367 adjustment = vma_offset - off_offset;
08a40648 5368
de194d85 5369 which can be collapsed into the expression below. */
340b6d91
AC
5370
5371static file_ptr
5372vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5373{
dc9155b2
NC
5374 /* PR binutils/16199: Handle an alignment of zero. */
5375 if (maxpagesize == 0)
5376 maxpagesize = 1;
340b6d91
AC
5377 return ((vma - off) % maxpagesize);
5378}
5379
6d33f217
L
5380static void
5381print_segment_map (const struct elf_segment_map *m)
5382{
5383 unsigned int j;
5384 const char *pt = get_segment_type (m->p_type);
5385 char buf[32];
5386
5387 if (pt == NULL)
5388 {
5389 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5390 sprintf (buf, "LOPROC+%7.7x",
5391 (unsigned int) (m->p_type - PT_LOPROC));
5392 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5393 sprintf (buf, "LOOS+%7.7x",
5394 (unsigned int) (m->p_type - PT_LOOS));
5395 else
5396 snprintf (buf, sizeof (buf), "%8.8x",
5397 (unsigned int) m->p_type);
5398 pt = buf;
5399 }
4a97a0e5 5400 fflush (stdout);
6d33f217
L
5401 fprintf (stderr, "%s:", pt);
5402 for (j = 0; j < m->count; j++)
5403 fprintf (stderr, " %s", m->sections [j]->name);
5404 putc ('\n',stderr);
4a97a0e5 5405 fflush (stderr);
6d33f217
L
5406}
5407
0a1b45a2 5408static bool
32812159
AM
5409write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5410{
5411 void *buf;
0a1b45a2 5412 bool ret;
32812159
AM
5413
5414 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
0a1b45a2 5415 return false;
32812159
AM
5416 buf = bfd_zmalloc (len);
5417 if (buf == NULL)
0a1b45a2 5418 return false;
32812159
AM
5419 ret = bfd_bwrite (buf, len, abfd) == len;
5420 free (buf);
5421 return ret;
5422}
5423
252b5132
RH
5424/* Assign file positions to the sections based on the mapping from
5425 sections to segments. This function also sets up some fields in
f3520d2f 5426 the file header. */
252b5132 5427
0a1b45a2 5428static bool
f3520d2f
AM
5429assign_file_positions_for_load_sections (bfd *abfd,
5430 struct bfd_link_info *link_info)
252b5132
RH
5431{
5432 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5433 struct elf_segment_map *m;
30fe1832 5434 struct elf_segment_map *phdr_load_seg;
252b5132 5435 Elf_Internal_Phdr *phdrs;
252b5132 5436 Elf_Internal_Phdr *p;
502794d4 5437 file_ptr off; /* Octets. */
3f570048 5438 bfd_size_type maxpagesize;
74e315db
L
5439 bfd_size_type p_align;
5440 bool p_align_p = false;
30fe1832 5441 unsigned int alloc, actual;
0920dee7 5442 unsigned int i, j;
30fe1832 5443 struct elf_segment_map **sorted_seg_map;
502794d4 5444 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
252b5132 5445
e36284ab 5446 if (link_info == NULL
e2cbf4df 5447 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
0a1b45a2 5448 return false;
252b5132 5449
8ded5a0f 5450 alloc = 0;
12bd6957 5451 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
30fe1832 5452 m->idx = alloc++;
252b5132 5453
82f2dbf7
NC
5454 if (alloc)
5455 {
5456 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5457 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5458 }
5459 else
5460 {
5461 /* PR binutils/12467. */
5462 elf_elfheader (abfd)->e_phoff = 0;
5463 elf_elfheader (abfd)->e_phentsize = 0;
5464 }
d324f6d6 5465
8ded5a0f 5466 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5467
12bd6957 5468 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
30fe1832
AM
5469 {
5470 actual = alloc;
5471 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5472 }
8ded5a0f 5473 else
30fe1832
AM
5474 {
5475 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5476 BFD_ASSERT (elf_program_header_size (abfd)
5477 == actual * bed->s->sizeof_phdr);
5478 BFD_ASSERT (actual >= alloc);
5479 }
252b5132
RH
5480
5481 if (alloc == 0)
f3520d2f 5482 {
12bd6957 5483 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
0a1b45a2 5484 return true;
f3520d2f 5485 }
252b5132 5486
12bd6957 5487 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5488 see assign_file_positions_except_relocs, so make sure we have
5489 that amount allocated, with trailing space cleared.
12bd6957
AM
5490 The variable alloc contains the computed need, while
5491 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5492 layout.
5493 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5494 where the layout is forced to according to a larger size in the
5495 last iterations for the testcase ld-elf/header. */
30fe1832
AM
5496 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5497 + alloc * sizeof (*sorted_seg_map)));
5498 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
f3520d2f 5499 elf_tdata (abfd)->phdr = phdrs;
252b5132 5500 if (phdrs == NULL)
0a1b45a2 5501 return false;
252b5132 5502
30fe1832 5503 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
252b5132 5504 {
30fe1832 5505 sorted_seg_map[j] = m;
252b5132 5506 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5507 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5508 not be done to the PT_NOTE section of a corefile, which may
5509 contain several pseudo-sections artificially created by bfd.
5510 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5511 if (m->count > 1
5512 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5513 && m->p_type == PT_NOTE))
48db3297
AM
5514 {
5515 for (i = 0; i < m->count; i++)
5516 m->sections[i]->target_index = i;
5517 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5518 elf_sort_sections);
5519 }
30fe1832
AM
5520 }
5521 if (alloc > 1)
5522 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5523 elf_sort_segments);
5524
74e315db 5525 p_align = bed->p_align;
30fe1832
AM
5526 maxpagesize = 1;
5527 if ((abfd->flags & D_PAGED) != 0)
c410035d
AM
5528 {
5529 if (link_info != NULL)
5530 maxpagesize = link_info->maxpagesize;
5531 else
5532 maxpagesize = bed->maxpagesize;
5533 }
30fe1832
AM
5534
5535 /* Sections must map to file offsets past the ELF file header. */
5536 off = bed->s->sizeof_ehdr;
5537 /* And if one of the PT_LOAD headers doesn't include the program
5538 headers then we'll be mapping program headers in the usual
5539 position after the ELF file header. */
5540 phdr_load_seg = NULL;
5541 for (j = 0; j < alloc; j++)
5542 {
5543 m = sorted_seg_map[j];
5544 if (m->p_type != PT_LOAD)
5545 break;
5546 if (m->includes_phdrs)
5547 {
5548 phdr_load_seg = m;
5549 break;
5550 }
5551 }
5552 if (phdr_load_seg == NULL)
5553 off += actual * bed->s->sizeof_phdr;
5554
5555 for (j = 0; j < alloc; j++)
5556 {
5557 asection **secpp;
502794d4 5558 bfd_vma off_adjust; /* Octets. */
0a1b45a2 5559 bool no_contents;
252b5132 5560
b301b248
AM
5561 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5562 number of sections with contents contributing to both p_filesz
5563 and p_memsz, followed by a number of sections with no contents
5564 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5565 available file offset for PT_LOAD and PT_NOTE segments. */
30fe1832
AM
5566 m = sorted_seg_map[j];
5567 p = phdrs + m->idx;
252b5132 5568 p->p_type = m->p_type;
28a7f3e7 5569 p->p_flags = m->p_flags;
252b5132 5570
3f570048 5571 if (m->count == 0)
502794d4 5572 p->p_vaddr = m->p_vaddr_offset * opb;
3f570048 5573 else
502794d4 5574 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
3f570048
AM
5575
5576 if (m->p_paddr_valid)
5577 p->p_paddr = m->p_paddr;
5578 else if (m->count == 0)
5579 p->p_paddr = 0;
5580 else
502794d4 5581 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
3f570048
AM
5582
5583 if (p->p_type == PT_LOAD
5584 && (abfd->flags & D_PAGED) != 0)
5585 {
5586 /* p_align in demand paged PT_LOAD segments effectively stores
5587 the maximum page size. When copying an executable with
5588 objcopy, we set m->p_align from the input file. Use this
5589 value for maxpagesize rather than bed->maxpagesize, which
5590 may be different. Note that we use maxpagesize for PT_TLS
5591 segment alignment later in this function, so we are relying
5592 on at least one PT_LOAD segment appearing before a PT_TLS
5593 segment. */
5594 if (m->p_align_valid)
5595 maxpagesize = m->p_align;
74e315db
L
5596 else if (p_align != 0
5597 && (link_info == NULL
5598 || !link_info->maxpagesize_is_set))
5599 /* Set p_align to the default p_align value while laying
5600 out segments aligning to the maximum page size or the
5601 largest section alignment. The run-time loader can
5602 align segments to the default p_align value or the
5603 maximum page size, depending on system page size. */
5604 p_align_p = true;
3f570048
AM
5605
5606 p->p_align = maxpagesize;
5607 }
3271a814
NS
5608 else if (m->p_align_valid)
5609 p->p_align = m->p_align;
e970b90a
DJ
5610 else if (m->count == 0)
5611 p->p_align = 1 << bed->s->log_file_align;
30fe1832
AM
5612
5613 if (m == phdr_load_seg)
5614 {
5615 if (!m->includes_filehdr)
5616 p->p_offset = off;
5617 off += actual * bed->s->sizeof_phdr;
5618 }
3f570048 5619
0a1b45a2 5620 no_contents = false;
bf988460 5621 off_adjust = 0;
252b5132 5622 if (p->p_type == PT_LOAD
b301b248 5623 && m->count > 0)
252b5132 5624 {
66631823 5625 bfd_size_type align; /* Bytes. */
a49e53ed 5626 unsigned int align_power = 0;
b301b248 5627
3271a814
NS
5628 if (m->p_align_valid)
5629 align = p->p_align;
5630 else
252b5132 5631 {
3271a814
NS
5632 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5633 {
5634 unsigned int secalign;
08a40648 5635
fd361982 5636 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5637 if (secalign > align_power)
5638 align_power = secalign;
5639 }
5640 align = (bfd_size_type) 1 << align_power;
5641 if (align < maxpagesize)
74e315db
L
5642 {
5643 /* If a section requires alignment higher than the
5644 default p_align value, don't set p_align to the
5645 default p_align value. */
5646 if (align > p_align)
5647 p_align_p = false;
5648 align = maxpagesize;
5649 }
5650 else
5651 {
5652 /* If a section requires alignment higher than the
5653 maximum page size, set p_align to the section
5654 alignment. */
5655 p_align_p = true;
5656 p_align = align;
5657 }
b301b248 5658 }
252b5132 5659
02bf8d82
AM
5660 for (i = 0; i < m->count; i++)
5661 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5662 /* If we aren't making room for this section, then
5663 it must be SHT_NOBITS regardless of what we've
5664 set via struct bfd_elf_special_section. */
5665 elf_section_type (m->sections[i]) = SHT_NOBITS;
5666
bf988460 5667 /* Find out whether this segment contains any loadable
aea274d3 5668 sections. */
0a1b45a2 5669 no_contents = true;
aea274d3
AM
5670 for (i = 0; i < m->count; i++)
5671 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5672 {
0a1b45a2 5673 no_contents = false;
aea274d3
AM
5674 break;
5675 }
bf988460 5676
66631823 5677 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
a8c75b76
AM
5678
5679 /* Broken hardware and/or kernel require that files do not
5680 map the same page with different permissions on some hppa
5681 processors. */
30fe1832
AM
5682 if (j != 0
5683 && (abfd->flags & D_PAGED) != 0
a8c75b76
AM
5684 && bed->no_page_alias
5685 && (off & (maxpagesize - 1)) != 0
502794d4
CE
5686 && ((off & -maxpagesize)
5687 == ((off + off_adjust) & -maxpagesize)))
a8c75b76 5688 off_adjust += maxpagesize;
bf988460
AM
5689 off += off_adjust;
5690 if (no_contents)
5691 {
5692 /* We shouldn't need to align the segment on disk since
5693 the segment doesn't need file space, but the gABI
5694 arguably requires the alignment and glibc ld.so
5695 checks it. So to comply with the alignment
5696 requirement but not waste file space, we adjust
5697 p_offset for just this segment. (OFF_ADJUST is
5698 subtracted from OFF later.) This may put p_offset
5699 past the end of file, but that shouldn't matter. */
5700 }
5701 else
5702 off_adjust = 0;
252b5132 5703 }
b1a6d0b1
NC
5704 /* Make sure the .dynamic section is the first section in the
5705 PT_DYNAMIC segment. */
5706 else if (p->p_type == PT_DYNAMIC
5707 && m->count > 1
5708 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5709 {
5710 _bfd_error_handler
871b3ab2 5711 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5712 " is not the .dynamic section"),
b301b248 5713 abfd);
b1a6d0b1 5714 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5715 return false;
b1a6d0b1 5716 }
3f001e84
JK
5717 /* Set the note section type to SHT_NOTE. */
5718 else if (p->p_type == PT_NOTE)
5719 for (i = 0; i < m->count; i++)
5720 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5721
252b5132
RH
5722 if (m->includes_filehdr)
5723 {
bf988460 5724 if (!m->p_flags_valid)
252b5132 5725 p->p_flags |= PF_R;
252b5132
RH
5726 p->p_filesz = bed->s->sizeof_ehdr;
5727 p->p_memsz = bed->s->sizeof_ehdr;
30fe1832 5728 if (p->p_type == PT_LOAD)
252b5132 5729 {
30fe1832 5730 if (m->count > 0)
252b5132 5731 {
30fe1832
AM
5732 if (p->p_vaddr < (bfd_vma) off
5733 || (!m->p_paddr_valid
5734 && p->p_paddr < (bfd_vma) off))
5735 {
5736 _bfd_error_handler
5737 (_("%pB: not enough room for program headers,"
5738 " try linking with -N"),
5739 abfd);
5740 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5741 return false;
30fe1832
AM
5742 }
5743 p->p_vaddr -= off;
5744 if (!m->p_paddr_valid)
5745 p->p_paddr -= off;
252b5132 5746 }
30fe1832
AM
5747 }
5748 else if (sorted_seg_map[0]->includes_filehdr)
5749 {
5750 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5751 p->p_vaddr = filehdr->p_vaddr;
bf988460 5752 if (!m->p_paddr_valid)
30fe1832 5753 p->p_paddr = filehdr->p_paddr;
252b5132 5754 }
252b5132
RH
5755 }
5756
5757 if (m->includes_phdrs)
5758 {
bf988460 5759 if (!m->p_flags_valid)
252b5132 5760 p->p_flags |= PF_R;
30fe1832
AM
5761 p->p_filesz += actual * bed->s->sizeof_phdr;
5762 p->p_memsz += actual * bed->s->sizeof_phdr;
f3520d2f 5763 if (!m->includes_filehdr)
252b5132 5764 {
30fe1832 5765 if (p->p_type == PT_LOAD)
252b5132 5766 {
30fe1832
AM
5767 elf_elfheader (abfd)->e_phoff = p->p_offset;
5768 if (m->count > 0)
5769 {
5770 p->p_vaddr -= off - p->p_offset;
5771 if (!m->p_paddr_valid)
5772 p->p_paddr -= off - p->p_offset;
5773 }
5774 }
5775 else if (phdr_load_seg != NULL)
5776 {
5777 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
502794d4 5778 bfd_vma phdr_off = 0; /* Octets. */
30fe1832
AM
5779 if (phdr_load_seg->includes_filehdr)
5780 phdr_off = bed->s->sizeof_ehdr;
5781 p->p_vaddr = phdr->p_vaddr + phdr_off;
bf988460 5782 if (!m->p_paddr_valid)
30fe1832
AM
5783 p->p_paddr = phdr->p_paddr + phdr_off;
5784 p->p_offset = phdr->p_offset + phdr_off;
252b5132 5785 }
30fe1832
AM
5786 else
5787 p->p_offset = bed->s->sizeof_ehdr;
2b0bc088 5788 }
252b5132
RH
5789 }
5790
5791 if (p->p_type == PT_LOAD
5792 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5793 {
bf988460 5794 if (!m->includes_filehdr && !m->includes_phdrs)
0bc3450e
AM
5795 {
5796 p->p_offset = off;
5797 if (no_contents)
67641dd3
AM
5798 {
5799 /* Put meaningless p_offset for PT_LOAD segments
5800 without file contents somewhere within the first
5801 page, in an attempt to not point past EOF. */
5802 bfd_size_type align = maxpagesize;
5803 if (align < p->p_align)
5804 align = p->p_align;
5805 if (align < 1)
5806 align = 1;
5807 p->p_offset = off % align;
5808 }
0bc3450e 5809 }
252b5132
RH
5810 else
5811 {
502794d4 5812 file_ptr adjust; /* Octets. */
252b5132
RH
5813
5814 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5815 if (!no_contents)
5816 p->p_filesz += adjust;
252b5132
RH
5817 p->p_memsz += adjust;
5818 }
5819 }
5820
1ea63fd2
AM
5821 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5822 maps. Set filepos for sections in PT_LOAD segments, and in
5823 core files, for sections in PT_NOTE segments.
5824 assign_file_positions_for_non_load_sections will set filepos
5825 for other sections and update p_filesz for other segments. */
252b5132
RH
5826 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5827 {
5828 asection *sec;
252b5132 5829 bfd_size_type align;
627b32bc 5830 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5831
5832 sec = *secpp;
02bf8d82 5833 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5834 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5835
88967714
AM
5836 if ((p->p_type == PT_LOAD
5837 || p->p_type == PT_TLS)
5838 && (this_hdr->sh_type != SHT_NOBITS
5839 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5840 && ((this_hdr->sh_flags & SHF_TLS) == 0
5841 || p->p_type == PT_TLS))))
252b5132 5842 {
502794d4
CE
5843 bfd_vma p_start = p->p_paddr; /* Octets. */
5844 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5845 bfd_vma s_start = sec->lma * opb; /* Octets. */
5846 bfd_vma adjust = s_start - p_end; /* Octets. */
252b5132 5847
a2d1e028
L
5848 if (adjust != 0
5849 && (s_start < p_end
5850 || p_end < p_start))
252b5132 5851 {
4eca0228 5852 _bfd_error_handler
695344c0 5853 /* xgettext:c-format */
2dcf00ce 5854 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
502794d4
CE
5855 abfd, sec, (uint64_t) s_start / opb,
5856 (uint64_t) p_end / opb);
88967714 5857 adjust = 0;
502794d4 5858 sec->lma = p_end / opb;
1cfb7d1e 5859 }
3ac9b6c9 5860 p->p_memsz += adjust;
1cfb7d1e 5861
d16e3d2e 5862 if (p->p_type == PT_LOAD)
88967714 5863 {
d16e3d2e 5864 if (this_hdr->sh_type != SHT_NOBITS)
32812159 5865 {
d16e3d2e 5866 off_adjust = 0;
30fe1832
AM
5867 if (p->p_filesz + adjust < p->p_memsz)
5868 {
5869 /* We have a PROGBITS section following NOBITS ones.
5870 Allocate file space for the NOBITS section(s) and
5871 zero it. */
5872 adjust = p->p_memsz - p->p_filesz;
5873 if (!write_zeros (abfd, off, adjust))
0a1b45a2 5874 return false;
30fe1832 5875 }
d16e3d2e
AM
5876 }
5877 /* We only adjust sh_offset in SHT_NOBITS sections
5878 as would seem proper for their address when the
5879 section is first in the segment. sh_offset
5880 doesn't really have any significance for
5881 SHT_NOBITS anyway, apart from a notional position
5882 relative to other sections. Historically we
5883 didn't bother with adjusting sh_offset and some
5884 programs depend on it not being adjusted. See
5885 pr12921 and pr25662. */
5886 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5887 {
30fe1832 5888 off += adjust;
d16e3d2e
AM
5889 if (this_hdr->sh_type == SHT_NOBITS)
5890 off_adjust += adjust;
32812159 5891 }
252b5132 5892 }
d16e3d2e
AM
5893 if (this_hdr->sh_type != SHT_NOBITS)
5894 p->p_filesz += adjust;
252b5132
RH
5895 }
5896
5897 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5898 {
b301b248
AM
5899 /* The section at i == 0 is the one that actually contains
5900 everything. */
4a938328
MS
5901 if (i == 0)
5902 {
627b32bc 5903 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5904 off += this_hdr->sh_size;
5905 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5906 p->p_memsz = 0;
5907 p->p_align = 1;
252b5132 5908 }
4a938328 5909 else
252b5132 5910 {
b301b248 5911 /* The rest are fake sections that shouldn't be written. */
252b5132 5912 sec->filepos = 0;
eea6121a 5913 sec->size = 0;
b301b248
AM
5914 sec->flags = 0;
5915 continue;
252b5132 5916 }
252b5132
RH
5917 }
5918 else
5919 {
1e951488 5920 if (p->p_type == PT_LOAD)
b301b248 5921 {
1e951488
AM
5922 this_hdr->sh_offset = sec->filepos = off;
5923 if (this_hdr->sh_type != SHT_NOBITS)
5924 off += this_hdr->sh_size;
5925 }
5926 else if (this_hdr->sh_type == SHT_NOBITS
5927 && (this_hdr->sh_flags & SHF_TLS) != 0
5928 && this_hdr->sh_offset == 0)
5929 {
5930 /* This is a .tbss section that didn't get a PT_LOAD.
5931 (See _bfd_elf_map_sections_to_segments "Create a
5932 final PT_LOAD".) Set sh_offset to the value it
5933 would have if we had created a zero p_filesz and
5934 p_memsz PT_LOAD header for the section. This
5935 also makes the PT_TLS header have the same
5936 p_offset value. */
5937 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5938 off, align);
5939 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5940 }
252b5132 5941
02bf8d82 5942 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5943 {
6a3cd2b4 5944 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5945 /* A load section without SHF_ALLOC is something like
5946 a note section in a PT_NOTE segment. These take
5947 file space but are not loaded into memory. */
5948 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5949 p->p_memsz += this_hdr->sh_size;
b301b248 5950 }
6a3cd2b4 5951 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5952 {
6a3cd2b4
AM
5953 if (p->p_type == PT_TLS)
5954 p->p_memsz += this_hdr->sh_size;
5955
5956 /* .tbss is special. It doesn't contribute to p_memsz of
5957 normal segments. */
5958 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5959 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5960 }
5961
b10a8ae0
L
5962 if (align > p->p_align
5963 && !m->p_align_valid
5964 && (p->p_type != PT_LOAD
5965 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5966 p->p_align = align;
5967 }
5968
bf988460 5969 if (!m->p_flags_valid)
252b5132
RH
5970 {
5971 p->p_flags |= PF_R;
02bf8d82 5972 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5973 p->p_flags |= PF_X;
02bf8d82 5974 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5975 p->p_flags |= PF_W;
5976 }
5977 }
43a8475c 5978
bf988460 5979 off -= off_adjust;
0920dee7 5980
30fe1832
AM
5981 /* PR ld/20815 - Check that the program header segment, if
5982 present, will be loaded into memory. */
5983 if (p->p_type == PT_PHDR
5984 && phdr_load_seg == NULL
5985 && !(bed->elf_backend_allow_non_load_phdr != NULL
5986 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5987 {
5988 /* The fix for this error is usually to edit the linker script being
5989 used and set up the program headers manually. Either that or
5990 leave room for the headers at the start of the SECTIONS. */
5991 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5992 " by LOAD segment"),
5993 abfd);
7b3c2715 5994 if (link_info == NULL)
0a1b45a2 5995 return false;
7b3c2715
AM
5996 /* Arrange for the linker to exit with an error, deleting
5997 the output file unless --noinhibit-exec is given. */
5998 link_info->callbacks->info ("%X");
30fe1832
AM
5999 }
6000
7c928300
AM
6001 /* Check that all sections are in a PT_LOAD segment.
6002 Don't check funky gdb generated core files. */
6003 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553 6004 {
0a1b45a2 6005 bool check_vma = true;
9a83a553
AM
6006
6007 for (i = 1; i < m->count; i++)
6008 if (m->sections[i]->vma == m->sections[i - 1]->vma
6009 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6010 ->this_hdr), p) != 0
6011 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6012 ->this_hdr), p) != 0)
0920dee7 6013 {
9a83a553 6014 /* Looks like we have overlays packed into the segment. */
0a1b45a2 6015 check_vma = false;
9a83a553 6016 break;
0920dee7 6017 }
9a83a553
AM
6018
6019 for (i = 0; i < m->count; i++)
6020 {
6021 Elf_Internal_Shdr *this_hdr;
6022 asection *sec;
6023
6024 sec = m->sections[i];
6025 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
6026 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6027 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 6028 {
4eca0228 6029 _bfd_error_handler
695344c0 6030 /* xgettext:c-format */
871b3ab2 6031 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
6032 abfd, sec, j);
6033 print_segment_map (m);
6034 }
6035 }
74e315db
L
6036
6037 if (p_align_p)
6038 p->p_align = p_align;
9a83a553 6039 }
252b5132
RH
6040 }
6041
12bd6957 6042 elf_next_file_pos (abfd) = off;
30fe1832
AM
6043
6044 if (link_info != NULL
6045 && phdr_load_seg != NULL
6046 && phdr_load_seg->includes_filehdr)
6047 {
6048 /* There is a segment that contains both the file headers and the
6049 program headers, so provide a symbol __ehdr_start pointing there.
6050 A program can use this to examine itself robustly. */
6051
6052 struct elf_link_hash_entry *hash
6053 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
0a1b45a2 6054 false, false, true);
30fe1832
AM
6055 /* If the symbol was referenced and not defined, define it. */
6056 if (hash != NULL
6057 && (hash->root.type == bfd_link_hash_new
6058 || hash->root.type == bfd_link_hash_undefined
6059 || hash->root.type == bfd_link_hash_undefweak
6060 || hash->root.type == bfd_link_hash_common))
6061 {
6062 asection *s = NULL;
66631823 6063 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
30fe1832
AM
6064
6065 if (phdr_load_seg->count != 0)
6066 /* The segment contains sections, so use the first one. */
6067 s = phdr_load_seg->sections[0];
6068 else
6069 /* Use the first (i.e. lowest-addressed) section in any segment. */
6070 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6071 if (m->p_type == PT_LOAD && m->count != 0)
6072 {
6073 s = m->sections[0];
6074 break;
6075 }
6076
6077 if (s != NULL)
6078 {
6079 hash->root.u.def.value = filehdr_vaddr - s->vma;
6080 hash->root.u.def.section = s;
6081 }
6082 else
6083 {
6084 hash->root.u.def.value = filehdr_vaddr;
6085 hash->root.u.def.section = bfd_abs_section_ptr;
6086 }
6087
6088 hash->root.type = bfd_link_hash_defined;
6089 hash->def_regular = 1;
6090 hash->non_elf = 0;
6091 }
6092 }
6093
0a1b45a2 6094 return true;
f3520d2f
AM
6095}
6096
1faa385f
NC
6097/* Determine if a bfd is a debuginfo file. Unfortunately there
6098 is no defined method for detecting such files, so we have to
6099 use heuristics instead. */
6100
0a1b45a2 6101bool
1faa385f
NC
6102is_debuginfo_file (bfd *abfd)
6103{
6104 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
0a1b45a2 6105 return false;
1faa385f
NC
6106
6107 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6108 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6109 Elf_Internal_Shdr **headerp;
6110
6111 for (headerp = start_headers; headerp < end_headers; headerp ++)
6112 {
6113 Elf_Internal_Shdr *header = * headerp;
6114
6115 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6116 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6117 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6118 && header->sh_type != SHT_NOBITS
6119 && header->sh_type != SHT_NOTE)
0a1b45a2 6120 return false;
1faa385f
NC
6121 }
6122
0a1b45a2 6123 return true;
1faa385f
NC
6124}
6125
1ff6de03
NA
6126/* Assign file positions for the other sections, except for compressed debugging
6127 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f 6128
0a1b45a2 6129static bool
f3520d2f
AM
6130assign_file_positions_for_non_load_sections (bfd *abfd,
6131 struct bfd_link_info *link_info)
6132{
6133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6134 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 6135 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
6136 Elf_Internal_Phdr *phdrs;
6137 Elf_Internal_Phdr *p;
6138 struct elf_segment_map *m;
f3520d2f 6139 file_ptr off;
66631823 6140 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c410035d 6141 bfd_vma maxpagesize;
f3520d2f 6142
c410035d
AM
6143 if (link_info != NULL)
6144 maxpagesize = link_info->maxpagesize;
6145 else
6146 maxpagesize = bed->maxpagesize;
5c182d5f 6147 i_shdrpp = elf_elfsections (abfd);
e06efbf1 6148 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 6149 off = elf_next_file_pos (abfd);
e06efbf1 6150 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 6151 {
5c182d5f 6152 Elf_Internal_Shdr *hdr;
1f9b1a84 6153 bfd_vma align;
5c182d5f
AM
6154
6155 hdr = *hdrpp;
6156 if (hdr->bfd_section != NULL
252e386e
AM
6157 && (hdr->bfd_section->filepos != 0
6158 || (hdr->sh_type == SHT_NOBITS
6159 && hdr->contents == NULL)))
627b32bc 6160 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
6161 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6162 {
1faa385f
NC
6163 if (hdr->sh_size != 0
6164 /* PR 24717 - debuginfo files are known to be not strictly
6165 compliant with the ELF standard. In particular they often
6166 have .note.gnu.property sections that are outside of any
6167 loadable segment. This is not a problem for such files,
6168 so do not warn about them. */
6169 && ! is_debuginfo_file (abfd))
4eca0228 6170 _bfd_error_handler
695344c0 6171 /* xgettext:c-format */
871b3ab2 6172 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
6173 abfd,
6174 (hdr->bfd_section == NULL
6175 ? "*unknown*"
6176 : hdr->bfd_section->name));
3ba71138
L
6177 /* We don't need to page align empty sections. */
6178 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
1f9b1a84 6179 align = maxpagesize;
5c182d5f 6180 else
1f9b1a84
AM
6181 align = hdr->sh_addralign & -hdr->sh_addralign;
6182 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
5c182d5f 6183 off = _bfd_elf_assign_file_position_for_section (hdr, off,
0a1b45a2 6184 false);
5c182d5f
AM
6185 }
6186 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6187 && hdr->bfd_section == NULL)
1ff6de03
NA
6188 /* We don't know the offset of these sections yet: their size has
6189 not been decided. */
0ce398f1 6190 || (hdr->bfd_section != NULL
1ff6de03
NA
6191 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6192 || (bfd_section_is_ctf (hdr->bfd_section)
6193 && abfd->is_linker_output)))
12bd6957 6194 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
6195 || (elf_symtab_shndx_list (abfd) != NULL
6196 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6197 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6198 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
6199 hdr->sh_offset = -1;
6200 else
0a1b45a2 6201 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
5c182d5f 6202 }
30fe1832 6203 elf_next_file_pos (abfd) = off;
5c182d5f 6204
252b5132
RH
6205 /* Now that we have set the section file positions, we can set up
6206 the file positions for the non PT_LOAD segments. */
f3520d2f 6207 phdrs = elf_tdata (abfd)->phdr;
12bd6957 6208 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6209 {
129af99f 6210 if (p->p_type == PT_GNU_RELRO)
252b5132 6211 {
66631823 6212 bfd_vma start, end; /* Bytes. */
0a1b45a2 6213 bool ok;
1ea63fd2 6214
129af99f 6215 if (link_info != NULL)
8c37241b 6216 {
129af99f 6217 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6218 in link_info. Note that there may be padding between
6219 relro_start and the first RELRO section. */
6220 start = link_info->relro_start;
6221 end = link_info->relro_end;
6222 }
6223 else if (m->count != 0)
6224 {
6225 if (!m->p_size_valid)
6226 abort ();
6227 start = m->sections[0]->vma;
66631823 6228 end = start + m->p_size / opb;
f2731e0c
AM
6229 }
6230 else
6231 {
6232 start = 0;
6233 end = 0;
6234 }
6235
0a1b45a2 6236 ok = false;
f2731e0c
AM
6237 if (start < end)
6238 {
6239 struct elf_segment_map *lm;
6240 const Elf_Internal_Phdr *lp;
6241 unsigned int i;
6242
6243 /* Find a LOAD segment containing a section in the RELRO
6244 segment. */
12bd6957 6245 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6246 lm != NULL;
6247 lm = lm->next, lp++)
8c37241b
JJ
6248 {
6249 if (lp->p_type == PT_LOAD
3146fac4 6250 && lm->count != 0
dbc88fc1
AM
6251 && (lm->sections[lm->count - 1]->vma
6252 + (!IS_TBSS (lm->sections[lm->count - 1])
66631823 6253 ? lm->sections[lm->count - 1]->size / opb
dbc88fc1 6254 : 0)) > start
f2731e0c 6255 && lm->sections[0]->vma < end)
8c37241b
JJ
6256 break;
6257 }
f2731e0c 6258
01f7e10c 6259 if (lm != NULL)
129af99f 6260 {
01f7e10c
AM
6261 /* Find the section starting the RELRO segment. */
6262 for (i = 0; i < lm->count; i++)
6263 {
6264 asection *s = lm->sections[i];
6265 if (s->vma >= start
6266 && s->vma < end
6267 && s->size != 0)
6268 break;
6269 }
6270
6271 if (i < lm->count)
6272 {
502794d4
CE
6273 p->p_vaddr = lm->sections[i]->vma * opb;
6274 p->p_paddr = lm->sections[i]->lma * opb;
01f7e10c 6275 p->p_offset = lm->sections[i]->filepos;
66631823 6276 p->p_memsz = end * opb - p->p_vaddr;
01f7e10c
AM
6277 p->p_filesz = p->p_memsz;
6278
6279 /* The RELRO segment typically ends a few bytes
6280 into .got.plt but other layouts are possible.
6281 In cases where the end does not match any
6282 loaded section (for instance is in file
6283 padding), trim p_filesz back to correspond to
6284 the end of loaded section contents. */
6285 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6286 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6287
6288 /* Preserve the alignment and flags if they are
6289 valid. The gold linker generates RW/4 for
6290 the PT_GNU_RELRO section. It is better for
6291 objcopy/strip to honor these attributes
6292 otherwise gdb will choke when using separate
6293 debug files. */
6294 if (!m->p_align_valid)
6295 p->p_align = 1;
6296 if (!m->p_flags_valid)
6297 p->p_flags = PF_R;
0a1b45a2 6298 ok = true;
01f7e10c 6299 }
129af99f 6300 }
b84a33b5 6301 }
ee9e412f 6302
01f7e10c 6303 if (!ok)
ee9e412f
NC
6304 {
6305 if (link_info != NULL)
6306 _bfd_error_handler
6307 (_("%pB: warning: unable to allocate any sections to PT_GNU_RELRO segment"),
6308 abfd);
6309 memset (p, 0, sizeof *p);
6310 }
129af99f 6311 }
04c3a755
NS
6312 else if (p->p_type == PT_GNU_STACK)
6313 {
6314 if (m->p_size_valid)
6315 p->p_memsz = m->p_size;
6316 }
129af99f
AS
6317 else if (m->count != 0)
6318 {
e06efbf1 6319 unsigned int i;
1a9ccd70 6320
129af99f
AS
6321 if (p->p_type != PT_LOAD
6322 && (p->p_type != PT_NOTE
6323 || bfd_get_format (abfd) != bfd_core))
6324 {
1a9ccd70
NC
6325 /* A user specified segment layout may include a PHDR
6326 segment that overlaps with a LOAD segment... */
6327 if (p->p_type == PT_PHDR)
6328 {
6329 m->count = 0;
6330 continue;
6331 }
6332
c86934ce
NC
6333 if (m->includes_filehdr || m->includes_phdrs)
6334 {
b1fa9dd6 6335 /* PR 17512: file: 2195325e. */
4eca0228 6336 _bfd_error_handler
871b3ab2 6337 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6338 "and/or program header"),
6339 abfd, (int) (p - phdrs));
0a1b45a2 6340 return false;
c86934ce 6341 }
129af99f 6342
86b2281f 6343 p->p_filesz = 0;
129af99f 6344 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6345 for (i = m->count; i-- != 0;)
6346 {
6347 asection *sect = m->sections[i];
6348 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6349 if (hdr->sh_type != SHT_NOBITS)
6350 {
aebb3d54 6351 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
9917b559
L
6352 /* NB: p_memsz of the loadable PT_NOTE segment
6353 should be the same as p_filesz. */
6354 if (p->p_type == PT_NOTE
6355 && (hdr->sh_flags & SHF_ALLOC) != 0)
6356 p->p_memsz = p->p_filesz;
86b2281f
AM
6357 break;
6358 }
6359 }
129af99f
AS
6360 }
6361 }
252b5132
RH
6362 }
6363
0a1b45a2 6364 return true;
252b5132
RH
6365}
6366
6a40cf0c
NC
6367static elf_section_list *
6368find_section_in_list (unsigned int i, elf_section_list * list)
6369{
6370 for (;list != NULL; list = list->next)
6371 if (list->ndx == i)
6372 break;
6373 return list;
6374}
6375
252b5132
RH
6376/* Work out the file positions of all the sections. This is called by
6377 _bfd_elf_compute_section_file_positions. All the section sizes and
6378 VMAs must be known before this is called.
6379
e0638f70 6380 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6381 "side-channel" data attached to a section to which they apply, and those that
6382 bfd doesn't process as relocations. The latter sort are stored in a normal
6383 bfd section by bfd_section_from_shdr. We don't consider the former sort
6384 here, unless they form part of the loadable image. Reloc sections not
6385 assigned here (and compressed debugging sections and CTF sections which
6386 nothing else in the file can rely upon) will be handled later by
e0638f70 6387 assign_file_positions_for_relocs.
252b5132
RH
6388
6389 We also don't set the positions of the .symtab and .strtab here. */
6390
0a1b45a2 6391static bool
c84fca4d
AO
6392assign_file_positions_except_relocs (bfd *abfd,
6393 struct bfd_link_info *link_info)
252b5132 6394{
5c182d5f
AM
6395 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6396 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6397 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6d6c25c8 6398 unsigned int alloc;
252b5132
RH
6399
6400 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6401 && bfd_get_format (abfd) != bfd_core)
6402 {
5c182d5f
AM
6403 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6404 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6405 Elf_Internal_Shdr **hdrpp;
6406 unsigned int i;
a485e98e 6407 file_ptr off;
252b5132
RH
6408
6409 /* Start after the ELF header. */
6410 off = i_ehdrp->e_ehsize;
6411
6412 /* We are not creating an executable, which means that we are
6413 not creating a program header, and that the actual order of
6414 the sections in the file is unimportant. */
9ad5cbcf 6415 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6416 {
6417 Elf_Internal_Shdr *hdr;
6418
6419 hdr = *hdrpp;
e0638f70
AM
6420 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6421 && hdr->bfd_section == NULL)
1ff6de03
NA
6422 /* Do not assign offsets for these sections yet: we don't know
6423 their sizes. */
0ce398f1 6424 || (hdr->bfd_section != NULL
1ff6de03
NA
6425 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6426 || (bfd_section_is_ctf (hdr->bfd_section)
6427 && abfd->is_linker_output)))
12bd6957 6428 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6429 || (elf_symtab_shndx_list (abfd) != NULL
6430 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6431 || i == elf_strtab_sec (abfd)
6432 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6433 {
6434 hdr->sh_offset = -1;
252b5132 6435 }
9ad5cbcf 6436 else
0a1b45a2 6437 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 6438 }
a485e98e
AM
6439
6440 elf_next_file_pos (abfd) = off;
6d6c25c8 6441 elf_program_header_size (abfd) = 0;
252b5132
RH
6442 }
6443 else
6444 {
252b5132 6445 /* Assign file positions for the loaded sections based on the
08a40648 6446 assignment of sections to segments. */
f3520d2f 6447 if (!assign_file_positions_for_load_sections (abfd, link_info))
0a1b45a2 6448 return false;
f3520d2f
AM
6449
6450 /* And for non-load sections. */
6451 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
0a1b45a2 6452 return false;
6d6c25c8 6453 }
f3520d2f 6454
6d6c25c8 6455 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
0a1b45a2 6456 return false;
1a9ccd70 6457
6d6c25c8
AM
6458 /* Write out the program headers. */
6459 alloc = i_ehdrp->e_phnum;
6460 if (alloc != 0)
6461 {
30fe1832 6462 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
cd584857 6463 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
0a1b45a2 6464 return false;
252b5132
RH
6465 }
6466
0a1b45a2 6467 return true;
252b5132
RH
6468}
6469
0a1b45a2 6470bool
ed7e9d0b
AM
6471_bfd_elf_init_file_header (bfd *abfd,
6472 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 6473{
3d540e93 6474 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6475 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6477
6478 i_ehdrp = elf_elfheader (abfd);
252b5132 6479
2b0f7ef9 6480 shstrtab = _bfd_elf_strtab_init ();
252b5132 6481 if (shstrtab == NULL)
0a1b45a2 6482 return false;
252b5132
RH
6483
6484 elf_shstrtab (abfd) = shstrtab;
6485
6486 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6487 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6488 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6489 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6490
6491 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6492 i_ehdrp->e_ident[EI_DATA] =
6493 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6494 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6495
252b5132
RH
6496 if ((abfd->flags & DYNAMIC) != 0)
6497 i_ehdrp->e_type = ET_DYN;
6498 else if ((abfd->flags & EXEC_P) != 0)
6499 i_ehdrp->e_type = ET_EXEC;
6500 else if (bfd_get_format (abfd) == bfd_core)
6501 i_ehdrp->e_type = ET_CORE;
6502 else
6503 i_ehdrp->e_type = ET_REL;
6504
6505 switch (bfd_get_arch (abfd))
6506 {
6507 case bfd_arch_unknown:
6508 i_ehdrp->e_machine = EM_NONE;
6509 break;
aa4f99bb
AO
6510
6511 /* There used to be a long list of cases here, each one setting
6512 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6513 in the corresponding bfd definition. To avoid duplication,
6514 the switch was removed. Machines that need special handling
6515 can generally do it in elf_backend_final_write_processing(),
6516 unless they need the information earlier than the final write.
6517 Such need can generally be supplied by replacing the tests for
6518 e_machine with the conditions used to determine it. */
252b5132 6519 default:
9c5bfbb7
AM
6520 i_ehdrp->e_machine = bed->elf_machine_code;
6521 }
aa4f99bb 6522
252b5132
RH
6523 i_ehdrp->e_version = bed->s->ev_current;
6524 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6525
c044fabd 6526 /* No program header, for now. */
252b5132
RH
6527 i_ehdrp->e_phoff = 0;
6528 i_ehdrp->e_phentsize = 0;
6529 i_ehdrp->e_phnum = 0;
6530
c044fabd 6531 /* Each bfd section is section header entry. */
252b5132
RH
6532 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6533 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6534
252b5132 6535 elf_tdata (abfd)->symtab_hdr.sh_name =
0a1b45a2 6536 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 6537 elf_tdata (abfd)->strtab_hdr.sh_name =
0a1b45a2 6538 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 6539 elf_tdata (abfd)->shstrtab_hdr.sh_name =
0a1b45a2 6540 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132 6541 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6542 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6543 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
0a1b45a2 6544 return false;
252b5132 6545
0a1b45a2 6546 return true;
252b5132
RH
6547}
6548
6d6c25c8
AM
6549/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6550
6551 FIXME: We used to have code here to sort the PT_LOAD segments into
6552 ascending order, as per the ELF spec. But this breaks some programs,
6553 including the Linux kernel. But really either the spec should be
6554 changed or the programs updated. */
6555
0a1b45a2 6556bool
6d6c25c8
AM
6557_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6558{
6559 if (link_info != NULL && bfd_link_pie (link_info))
6560 {
6561 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6562 unsigned int num_segments = i_ehdrp->e_phnum;
6563 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6564 Elf_Internal_Phdr *segment = tdata->phdr;
6565 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6566
6567 /* Find the lowest p_vaddr in PT_LOAD segments. */
6568 bfd_vma p_vaddr = (bfd_vma) -1;
6569 for (; segment < end_segment; segment++)
6570 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6571 p_vaddr = segment->p_vaddr;
6572
6573 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6574 segments is non-zero. */
6575 if (p_vaddr)
6576 i_ehdrp->e_type = ET_EXEC;
6577 }
0a1b45a2 6578 return true;
6d6c25c8
AM
6579}
6580
252b5132 6581/* Assign file positions for all the reloc sections which are not part
a485e98e 6582 of the loadable file image, and the file position of section headers. */
252b5132 6583
0a1b45a2 6584static bool
0ce398f1 6585_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6586{
6587 file_ptr off;
e06efbf1 6588 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6589 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6590 Elf_Internal_Ehdr *i_ehdrp;
6591 const struct elf_backend_data *bed;
252b5132 6592
12bd6957 6593 off = elf_next_file_pos (abfd);
252b5132 6594
e06efbf1
L
6595 shdrpp = elf_elfsections (abfd);
6596 end_shdrpp = shdrpp + elf_numsections (abfd);
6597 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6598 {
252b5132 6599 shdrp = *shdrpp;
0ce398f1
L
6600 if (shdrp->sh_offset == -1)
6601 {
3e19fb8f 6602 asection *sec = shdrp->bfd_section;
0a1b45a2
AM
6603 bool is_rel = (shdrp->sh_type == SHT_REL
6604 || shdrp->sh_type == SHT_RELA);
6605 bool is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6606 if (is_rel
1ff6de03 6607 || is_ctf
3e19fb8f 6608 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6609 {
1ff6de03 6610 if (!is_rel && !is_ctf)
0ce398f1 6611 {
3e19fb8f
L
6612 const char *name = sec->name;
6613 struct bfd_elf_section_data *d;
6614
0ce398f1 6615 /* Compress DWARF debug sections. */
3e19fb8f 6616 if (!bfd_compress_section (abfd, sec,
0ce398f1 6617 shdrp->contents))
0a1b45a2 6618 return false;
3e19fb8f
L
6619
6620 if (sec->compress_status == COMPRESS_SECTION_DONE
6621 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6622 {
6623 /* If section is compressed with zlib-gnu, convert
6624 section name from .debug_* to .zdebug_*. */
6625 char *new_name
6626 = convert_debug_to_zdebug (abfd, name);
6627 if (new_name == NULL)
0a1b45a2 6628 return false;
3e19fb8f
L
6629 name = new_name;
6630 }
dd905818 6631 /* Add section name to section name section. */
3e19fb8f
L
6632 if (shdrp->sh_name != (unsigned int) -1)
6633 abort ();
6634 shdrp->sh_name
6635 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
0a1b45a2 6636 name, false);
3e19fb8f
L
6637 d = elf_section_data (sec);
6638
dd905818 6639 /* Add reloc section name to section name section. */
3e19fb8f
L
6640 if (d->rel.hdr
6641 && !_bfd_elf_set_reloc_sh_name (abfd,
6642 d->rel.hdr,
0a1b45a2
AM
6643 name, false))
6644 return false;
3e19fb8f
L
6645 if (d->rela.hdr
6646 && !_bfd_elf_set_reloc_sh_name (abfd,
6647 d->rela.hdr,
0a1b45a2
AM
6648 name, true))
6649 return false;
3e19fb8f 6650
0ce398f1 6651 /* Update section size and contents. */
3e19fb8f
L
6652 shdrp->sh_size = sec->size;
6653 shdrp->contents = sec->contents;
0ce398f1
L
6654 shdrp->bfd_section->contents = NULL;
6655 }
1ff6de03
NA
6656 else if (is_ctf)
6657 {
6658 /* Update section size and contents. */
6659 shdrp->sh_size = sec->size;
6660 shdrp->contents = sec->contents;
6661 }
6662
0ce398f1
L
6663 off = _bfd_elf_assign_file_position_for_section (shdrp,
6664 off,
0a1b45a2 6665 true);
0ce398f1
L
6666 }
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
JL
6700 /* Do not rewrite ELF data when the BFD has been opened for update.
6701 abfd->output_has_begun was set to TRUE on opening, so creation of new
6702 sections, and modification of existing section sizes was restricted.
6703 This means the ELF header, program headers and section headers can't have
6704 changed.
6705 If the contents of any sections has been modified, then those changes have
6706 already been written to the BFD. */
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]. */
c0355132
AM
6757 if (t->o->build_id.after_write_object_contents != NULL)
6758 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36 6759
0a1b45a2 6760 return true;
252b5132
RH
6761}
6762
0a1b45a2 6763bool
217aa764 6764_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6765{
c044fabd 6766 /* Hopefully this can be done just like an object file. */
252b5132
RH
6767 return _bfd_elf_write_object_contents (abfd);
6768}
c044fabd
KH
6769
6770/* Given a section, search the header to find them. */
6771
cb33740c 6772unsigned int
198beae2 6773_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6774{
9c5bfbb7 6775 const struct elf_backend_data *bed;
91d6fa6a 6776 unsigned int sec_index;
252b5132 6777
9ad5cbcf
AM
6778 if (elf_section_data (asect) != NULL
6779 && elf_section_data (asect)->this_idx != 0)
6780 return elf_section_data (asect)->this_idx;
6781
6782 if (bfd_is_abs_section (asect))
91d6fa6a 6783 sec_index = SHN_ABS;
af746e92 6784 else if (bfd_is_com_section (asect))
91d6fa6a 6785 sec_index = SHN_COMMON;
af746e92 6786 else if (bfd_is_und_section (asect))
91d6fa6a 6787 sec_index = SHN_UNDEF;
af746e92 6788 else
91d6fa6a 6789 sec_index = SHN_BAD;
252b5132 6790
af746e92 6791 bed = get_elf_backend_data (abfd);
252b5132
RH
6792 if (bed->elf_backend_section_from_bfd_section)
6793 {
91d6fa6a 6794 int retval = sec_index;
9ad5cbcf 6795
af746e92
AM
6796 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6797 return retval;
252b5132
RH
6798 }
6799
91d6fa6a 6800 if (sec_index == SHN_BAD)
af746e92 6801 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6802
91d6fa6a 6803 return sec_index;
252b5132
RH
6804}
6805
6806/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6807 on error. */
6808
6809int
217aa764 6810_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6811{
6812 asymbol *asym_ptr = *asym_ptr_ptr;
6813 int idx;
6814 flagword flags = asym_ptr->flags;
6815
6816 /* When gas creates relocations against local labels, it creates its
6817 own symbol for the section, but does put the symbol into the
6818 symbol chain, so udata is 0. When the linker is generating
6819 relocatable output, this section symbol may be for one of the
6820 input sections rather than the output section. */
6821 if (asym_ptr->udata.i == 0
6822 && (flags & BSF_SECTION_SYM)
6823 && asym_ptr->section)
6824 {
5372391b 6825 asection *sec;
252b5132 6826
5372391b
AM
6827 sec = asym_ptr->section;
6828 if (sec->owner != abfd && sec->output_section != NULL)
6829 sec = sec->output_section;
6830 if (sec->owner == abfd
27e3da31
AM
6831 && sec->index < elf_num_section_syms (abfd)
6832 && elf_section_syms (abfd)[sec->index] != NULL)
6833 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
252b5132
RH
6834 }
6835
6836 idx = asym_ptr->udata.i;
6837
6838 if (idx == 0)
6839 {
6840 /* This case can occur when using --strip-symbol on a symbol
08a40648 6841 which is used in a relocation entry. */
4eca0228 6842 _bfd_error_handler
695344c0 6843 /* xgettext:c-format */
871b3ab2 6844 (_("%pB: symbol `%s' required but not present"),
d003868e 6845 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6846 bfd_set_error (bfd_error_no_symbols);
6847 return -1;
6848 }
6849
6850#if DEBUG & 4
6851 {
6852 fprintf (stderr,
cd9af601
AM
6853 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6854 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6855 fflush (stderr);
6856 }
6857#endif
6858
6859 return idx;
6860}
6861
84d1d650 6862/* Rewrite program header information. */
252b5132 6863
0a1b45a2 6864static bool
c410035d 6865rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
252b5132 6866{
b34976b6
AM
6867 Elf_Internal_Ehdr *iehdr;
6868 struct elf_segment_map *map;
6869 struct elf_segment_map *map_first;
6870 struct elf_segment_map **pointer_to_map;
6871 Elf_Internal_Phdr *segment;
6872 asection *section;
6873 unsigned int i;
6874 unsigned int num_segments;
0a1b45a2
AM
6875 bool phdr_included = false;
6876 bool p_paddr_valid;
b34976b6
AM
6877 struct elf_segment_map *phdr_adjust_seg = NULL;
6878 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6879 const struct elf_backend_data *bed;
502794d4 6880 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
bc67d8a6 6881
caf47ea6 6882 bed = get_elf_backend_data (ibfd);
252b5132
RH
6883 iehdr = elf_elfheader (ibfd);
6884
bc67d8a6 6885 map_first = NULL;
c044fabd 6886 pointer_to_map = &map_first;
252b5132
RH
6887
6888 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6889
6890 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6891#define SEGMENT_END(segment, start) \
6892 (start + (segment->p_memsz > segment->p_filesz \
6893 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6894
eecdbe52
JJ
6895#define SECTION_SIZE(section, segment) \
6896 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6897 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6898 ? section->size : 0)
eecdbe52 6899
b34976b6 6900 /* Returns TRUE if the given section is contained within
bc67d8a6 6901 the given segment. VMA addresses are compared. */
502794d4
CE
6902#define IS_CONTAINED_BY_VMA(section, segment, opb) \
6903 (section->vma * (opb) >= segment->p_vaddr \
6904 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6905 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6906
b34976b6 6907 /* Returns TRUE if the given section is contained within
bc67d8a6 6908 the given segment. LMA addresses are compared. */
502794d4
CE
6909#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6910 (section->lma * (opb) >= base \
6911 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6912 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
aecc8f8a 6913 <= SEGMENT_END (segment, base)))
252b5132 6914
0efc80c8
L
6915 /* Handle PT_NOTE segment. */
6916#define IS_NOTE(p, s) \
aecc8f8a 6917 (p->p_type == PT_NOTE \
0efc80c8 6918 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6919 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6920 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6921 <= p->p_offset + p->p_filesz))
252b5132 6922
0efc80c8
L
6923 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6924 etc. */
6925#define IS_COREFILE_NOTE(p, s) \
6926 (IS_NOTE (p, s) \
6927 && bfd_get_format (ibfd) == bfd_core \
6928 && s->vma == 0 \
6929 && s->lma == 0)
6930
252b5132
RH
6931 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6932 linker, which generates a PT_INTERP section with p_vaddr and
6933 p_memsz set to 0. */
aecc8f8a
AM
6934#define IS_SOLARIS_PT_INTERP(p, s) \
6935 (p->p_vaddr == 0 \
6936 && p->p_paddr == 0 \
6937 && p->p_memsz == 0 \
6938 && p->p_filesz > 0 \
6939 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6940 && s->size > 0 \
aecc8f8a 6941 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6942 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6943 <= p->p_offset + p->p_filesz))
5c440b1e 6944
bc67d8a6
NC
6945 /* Decide if the given section should be included in the given segment.
6946 A section will be included if:
f5ffc919 6947 1. It is within the address space of the segment -- we use the LMA
08a40648 6948 if that is set for the segment and the VMA otherwise,
0efc80c8 6949 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6950 segment.
bc67d8a6 6951 3. There is an output section associated with it,
eecdbe52 6952 4. The section has not already been allocated to a previous segment.
2b05f1b7 6953 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6954 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6955 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6956 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6957 (with the possible exception of .dynamic). */
502794d4 6958#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
2b05f1b7 6959 ((((segment->p_paddr \
502794d4
CE
6960 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6961 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
2b05f1b7 6962 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6963 || IS_NOTE (segment, section)) \
2b05f1b7
L
6964 && segment->p_type != PT_GNU_STACK \
6965 && (segment->p_type != PT_TLS \
6966 || (section->flags & SEC_THREAD_LOCAL)) \
6967 && (segment->p_type == PT_LOAD \
6968 || segment->p_type == PT_TLS \
6969 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6970 && (segment->p_type != PT_DYNAMIC \
6971 || SECTION_SIZE (section, segment) > 0 \
6972 || (segment->p_paddr \
502794d4
CE
6973 ? segment->p_paddr != section->lma * (opb) \
6974 : segment->p_vaddr != section->vma * (opb)) \
fd361982 6975 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6976 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6977
9f17e2a6
L
6978/* If the output section of a section in the input segment is NULL,
6979 it is removed from the corresponding output segment. */
502794d4
CE
6980#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6981 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
9f17e2a6
L
6982 && section->output_section != NULL)
6983
b34976b6 6984 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6985#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6986 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6987
6988 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6989 their VMA address ranges and their LMA address ranges overlap.
6990 It is possible to have overlapping VMA ranges without overlapping LMA
6991 ranges. RedBoot images for example can have both .data and .bss mapped
6992 to the same VMA range, but with the .data section mapped to a different
6993 LMA. */
aecc8f8a 6994#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6995 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6996 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6997 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6998 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6 6999
c82ebeb7 7000 /* Initialise the segment mark field, and discard stupid alignment. */
bc67d8a6 7001 for (section = ibfd->sections; section != NULL; section = section->next)
c82ebeb7
AM
7002 {
7003 asection *o = section->output_section;
7004 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7005 o->alignment_power = 0;
7006 section->segment_mark = false;
7007 }
bc67d8a6 7008
5c44b38e
AM
7009 /* The Solaris linker creates program headers in which all the
7010 p_paddr fields are zero. When we try to objcopy or strip such a
7011 file, we get confused. Check for this case, and if we find it
7012 don't set the p_paddr_valid fields. */
0a1b45a2 7013 p_paddr_valid = false;
5c44b38e
AM
7014 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7015 i < num_segments;
7016 i++, segment++)
7017 if (segment->p_paddr != 0)
7018 {
0a1b45a2 7019 p_paddr_valid = true;
5c44b38e
AM
7020 break;
7021 }
7022
252b5132 7023 /* Scan through the segments specified in the program header
bc67d8a6 7024 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 7025 in the loadable segments. These can be created by weird
aecc8f8a 7026 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
7027 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7028 i < num_segments;
c044fabd 7029 i++, segment++)
252b5132 7030 {
252b5132 7031 unsigned int j;
c044fabd 7032 Elf_Internal_Phdr *segment2;
252b5132 7033
aecc8f8a
AM
7034 if (segment->p_type == PT_INTERP)
7035 for (section = ibfd->sections; section; section = section->next)
7036 if (IS_SOLARIS_PT_INTERP (segment, section))
7037 {
7038 /* Mininal change so that the normal section to segment
4cc11e76 7039 assignment code will work. */
502794d4 7040 segment->p_vaddr = section->vma * opb;
aecc8f8a
AM
7041 break;
7042 }
7043
bc67d8a6 7044 if (segment->p_type != PT_LOAD)
b10a8ae0
L
7045 {
7046 /* Remove PT_GNU_RELRO segment. */
7047 if (segment->p_type == PT_GNU_RELRO)
7048 segment->p_type = PT_NULL;
7049 continue;
7050 }
c044fabd 7051
bc67d8a6 7052 /* Determine if this segment overlaps any previous segments. */
0067a569 7053 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
7054 {
7055 bfd_signed_vma extra_length;
c044fabd 7056
bc67d8a6 7057 if (segment2->p_type != PT_LOAD
0067a569 7058 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 7059 continue;
c044fabd 7060
bc67d8a6
NC
7061 /* Merge the two segments together. */
7062 if (segment2->p_vaddr < segment->p_vaddr)
7063 {
c044fabd 7064 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 7065 SEGMENT. */
0067a569
AM
7066 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7067 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 7068
bc67d8a6
NC
7069 if (extra_length > 0)
7070 {
0067a569 7071 segment2->p_memsz += extra_length;
bc67d8a6
NC
7072 segment2->p_filesz += extra_length;
7073 }
c044fabd 7074
bc67d8a6 7075 segment->p_type = PT_NULL;
c044fabd 7076
bc67d8a6
NC
7077 /* Since we have deleted P we must restart the outer loop. */
7078 i = 0;
7079 segment = elf_tdata (ibfd)->phdr;
7080 break;
7081 }
7082 else
7083 {
c044fabd 7084 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 7085 SEGMENT2. */
0067a569
AM
7086 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7087 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 7088
bc67d8a6
NC
7089 if (extra_length > 0)
7090 {
0067a569 7091 segment->p_memsz += extra_length;
bc67d8a6
NC
7092 segment->p_filesz += extra_length;
7093 }
c044fabd 7094
bc67d8a6
NC
7095 segment2->p_type = PT_NULL;
7096 }
7097 }
7098 }
c044fabd 7099
bc67d8a6
NC
7100 /* The second scan attempts to assign sections to segments. */
7101 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7102 i < num_segments;
0067a569 7103 i++, segment++)
bc67d8a6 7104 {
0067a569
AM
7105 unsigned int section_count;
7106 asection **sections;
7107 asection *output_section;
7108 unsigned int isec;
9933dc52
AM
7109 asection *matching_lma;
7110 asection *suggested_lma;
0067a569 7111 unsigned int j;
446f7ed5 7112 size_t amt;
0067a569 7113 asection *first_section;
bc67d8a6
NC
7114
7115 if (segment->p_type == PT_NULL)
7116 continue;
c044fabd 7117
9f17e2a6 7118 first_section = NULL;
bc67d8a6 7119 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
7120 for (section = ibfd->sections, section_count = 0;
7121 section != NULL;
7122 section = section->next)
9f17e2a6
L
7123 {
7124 /* Find the first section in the input segment, which may be
7125 removed from the corresponding output segment. */
502794d4 7126 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
9f17e2a6
L
7127 {
7128 if (first_section == NULL)
7129 first_section = section;
7130 if (section->output_section != NULL)
7131 ++section_count;
7132 }
7133 }
811072d8 7134
b5f852ea
NC
7135 /* Allocate a segment map big enough to contain
7136 all of the sections we have selected. */
00bee008 7137 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7138 amt += section_count * sizeof (asection *);
a50b1753 7139 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7140 if (map == NULL)
0a1b45a2 7141 return false;
252b5132
RH
7142
7143 /* Initialise the fields of the segment map. Default to
7144 using the physical address of the segment in the input BFD. */
0067a569
AM
7145 map->next = NULL;
7146 map->p_type = segment->p_type;
7147 map->p_flags = segment->p_flags;
bc67d8a6 7148 map->p_flags_valid = 1;
55d55ac7 7149
c410035d
AM
7150 if (map->p_type == PT_LOAD
7151 && (ibfd->flags & D_PAGED) != 0
7152 && maxpagesize > 1
7153 && segment->p_align > 1)
7154 {
7155 map->p_align = segment->p_align;
7156 if (segment->p_align > maxpagesize)
7157 map->p_align = maxpagesize;
7158 map->p_align_valid = 1;
7159 }
7160
9f17e2a6
L
7161 /* If the first section in the input segment is removed, there is
7162 no need to preserve segment physical address in the corresponding
7163 output segment. */
945c025a 7164 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7165 {
7166 map->p_paddr = segment->p_paddr;
5c44b38e 7167 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7168 }
252b5132
RH
7169
7170 /* Determine if this segment contains the ELF file header
7171 and if it contains the program headers themselves. */
bc67d8a6
NC
7172 map->includes_filehdr = (segment->p_offset == 0
7173 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7174 map->includes_phdrs = 0;
252b5132 7175
0067a569 7176 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7177 {
bc67d8a6
NC
7178 map->includes_phdrs =
7179 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7180 && (segment->p_offset + segment->p_filesz
252b5132
RH
7181 >= ((bfd_vma) iehdr->e_phoff
7182 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7183
bc67d8a6 7184 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7185 phdr_included = true;
252b5132
RH
7186 }
7187
bc67d8a6 7188 if (section_count == 0)
252b5132
RH
7189 {
7190 /* Special segments, such as the PT_PHDR segment, may contain
7191 no sections, but ordinary, loadable segments should contain
1ed89aa9 7192 something. They are allowed by the ELF spec however, so only
07d6d2b8 7193 a warning is produced.
f98450c6
NC
7194 There is however the valid use case of embedded systems which
7195 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7196 flash memory with zeros. No warning is shown for that case. */
7197 if (segment->p_type == PT_LOAD
7198 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7199 /* xgettext:c-format */
9793eb77
AM
7200 _bfd_error_handler
7201 (_("%pB: warning: empty loadable segment detected"
7202 " at vaddr=%#" PRIx64 ", is this intentional?"),
7203 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7204
502794d4 7205 map->p_vaddr_offset = segment->p_vaddr / opb;
bc67d8a6 7206 map->count = 0;
c044fabd
KH
7207 *pointer_to_map = map;
7208 pointer_to_map = &map->next;
252b5132
RH
7209
7210 continue;
7211 }
7212
7213 /* Now scan the sections in the input BFD again and attempt
7214 to add their corresponding output sections to the segment map.
7215 The problem here is how to handle an output section which has
7216 been moved (ie had its LMA changed). There are four possibilities:
7217
7218 1. None of the sections have been moved.
7219 In this case we can continue to use the segment LMA from the
7220 input BFD.
7221
7222 2. All of the sections have been moved by the same amount.
7223 In this case we can change the segment's LMA to match the LMA
7224 of the first section.
7225
7226 3. Some of the sections have been moved, others have not.
7227 In this case those sections which have not been moved can be
7228 placed in the current segment which will have to have its size,
7229 and possibly its LMA changed, and a new segment or segments will
7230 have to be created to contain the other sections.
7231
b5f852ea 7232 4. The sections have been moved, but not by the same amount.
252b5132
RH
7233 In this case we can change the segment's LMA to match the LMA
7234 of the first section and we will have to create a new segment
7235 or segments to contain the other sections.
7236
7237 In order to save time, we allocate an array to hold the section
7238 pointers that we are interested in. As these sections get assigned
7239 to a segment, they are removed from this array. */
7240
446f7ed5
AM
7241 amt = section_count * sizeof (asection *);
7242 sections = (asection **) bfd_malloc (amt);
252b5132 7243 if (sections == NULL)
0a1b45a2 7244 return false;
252b5132
RH
7245
7246 /* Step One: Scan for segment vs section LMA conflicts.
7247 Also add the sections to the section array allocated above.
7248 Also add the sections to the current segment. In the common
7249 case, where the sections have not been moved, this means that
7250 we have completely filled the segment, and there is nothing
7251 more to do. */
252b5132 7252 isec = 0;
9933dc52
AM
7253 matching_lma = NULL;
7254 suggested_lma = NULL;
252b5132 7255
461c4b2e 7256 for (section = first_section, j = 0;
bc67d8a6
NC
7257 section != NULL;
7258 section = section->next)
252b5132 7259 {
502794d4 7260 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
c0f7859b 7261 {
bc67d8a6
NC
7262 output_section = section->output_section;
7263
0067a569 7264 sections[j++] = section;
252b5132
RH
7265
7266 /* The Solaris native linker always sets p_paddr to 0.
7267 We try to catch that case here, and set it to the
5e8d7549
NC
7268 correct value. Note - some backends require that
7269 p_paddr be left as zero. */
5c44b38e 7270 if (!p_paddr_valid
4455705d 7271 && segment->p_vaddr != 0
0067a569 7272 && !bed->want_p_paddr_set_to_zero
252b5132 7273 && isec == 0
bc67d8a6 7274 && output_section->lma != 0
9933dc52
AM
7275 && (align_power (segment->p_vaddr
7276 + (map->includes_filehdr
7277 ? iehdr->e_ehsize : 0)
7278 + (map->includes_phdrs
7279 ? iehdr->e_phnum * iehdr->e_phentsize
7280 : 0),
66631823
CE
7281 output_section->alignment_power * opb)
7282 == (output_section->vma * opb)))
bc67d8a6 7283 map->p_paddr = segment->p_vaddr;
252b5132
RH
7284
7285 /* Match up the physical address of the segment with the
7286 LMA address of the output section. */
502794d4
CE
7287 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7288 opb)
5e8d7549 7289 || IS_COREFILE_NOTE (segment, section)
0067a569 7290 || (bed->want_p_paddr_set_to_zero
502794d4 7291 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
252b5132 7292 {
9933dc52
AM
7293 if (matching_lma == NULL
7294 || output_section->lma < matching_lma->lma)
7295 matching_lma = output_section;
252b5132
RH
7296
7297 /* We assume that if the section fits within the segment
bc67d8a6 7298 then it does not overlap any other section within that
252b5132 7299 segment. */
0067a569
AM
7300 map->sections[isec++] = output_section;
7301 }
9933dc52
AM
7302 else if (suggested_lma == NULL)
7303 suggested_lma = output_section;
147d51c2
L
7304
7305 if (j == section_count)
7306 break;
252b5132
RH
7307 }
7308 }
7309
bc67d8a6 7310 BFD_ASSERT (j == section_count);
252b5132
RH
7311
7312 /* Step Two: Adjust the physical address of the current segment,
7313 if necessary. */
bc67d8a6 7314 if (isec == section_count)
252b5132
RH
7315 {
7316 /* All of the sections fitted within the segment as currently
7317 specified. This is the default case. Add the segment to
7318 the list of built segments and carry on to process the next
7319 program header in the input BFD. */
bc67d8a6 7320 map->count = section_count;
c044fabd
KH
7321 *pointer_to_map = map;
7322 pointer_to_map = &map->next;
08a40648 7323
5c44b38e 7324 if (p_paddr_valid
30fe1832
AM
7325 && !bed->want_p_paddr_set_to_zero)
7326 {
7327 bfd_vma hdr_size = 0;
7328 if (map->includes_filehdr)
7329 hdr_size = iehdr->e_ehsize;
7330 if (map->includes_phdrs)
7331 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7332
7333 /* Account for padding before the first section in the
7334 segment. */
502794d4
CE
7335 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7336 - matching_lma->lma);
30fe1832 7337 }
08a40648 7338
252b5132
RH
7339 free (sections);
7340 continue;
7341 }
252b5132
RH
7342 else
7343 {
9933dc52
AM
7344 /* Change the current segment's physical address to match
7345 the LMA of the first section that fitted, or if no
7346 section fitted, the first section. */
7347 if (matching_lma == NULL)
7348 matching_lma = suggested_lma;
7349
66631823 7350 map->p_paddr = matching_lma->lma * opb;
72730e0c 7351
bc67d8a6
NC
7352 /* Offset the segment physical address from the lma
7353 to allow for space taken up by elf headers. */
9933dc52 7354 if (map->includes_phdrs)
010c8431 7355 {
9933dc52
AM
7356 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7357
7358 /* iehdr->e_phnum is just an estimate of the number
7359 of program headers that we will need. Make a note
7360 here of the number we used and the segment we chose
7361 to hold these headers, so that we can adjust the
7362 offset when we know the correct value. */
7363 phdr_adjust_num = iehdr->e_phnum;
7364 phdr_adjust_seg = map;
010c8431 7365 }
252b5132 7366
9933dc52 7367 if (map->includes_filehdr)
bc67d8a6 7368 {
9933dc52
AM
7369 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7370 map->p_paddr -= iehdr->e_ehsize;
7371 /* We've subtracted off the size of headers from the
7372 first section lma, but there may have been some
7373 alignment padding before that section too. Try to
7374 account for that by adjusting the segment lma down to
7375 the same alignment. */
7376 if (segment->p_align != 0 && segment->p_align < align)
7377 align = segment->p_align;
66631823 7378 map->p_paddr &= -(align * opb);
bc67d8a6 7379 }
252b5132
RH
7380 }
7381
7382 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7383 those that fit to the current segment and removing them from the
252b5132
RH
7384 sections array; but making sure not to leave large gaps. Once all
7385 possible sections have been assigned to the current segment it is
7386 added to the list of built segments and if sections still remain
7387 to be assigned, a new segment is constructed before repeating
7388 the loop. */
7389 isec = 0;
7390 do
7391 {
bc67d8a6 7392 map->count = 0;
9933dc52 7393 suggested_lma = NULL;
252b5132
RH
7394
7395 /* Fill the current segment with sections that fit. */
bc67d8a6 7396 for (j = 0; j < section_count; j++)
252b5132 7397 {
bc67d8a6 7398 section = sections[j];
252b5132 7399
bc67d8a6 7400 if (section == NULL)
252b5132
RH
7401 continue;
7402
bc67d8a6 7403 output_section = section->output_section;
252b5132 7404
bc67d8a6 7405 BFD_ASSERT (output_section != NULL);
c044fabd 7406
502794d4
CE
7407 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7408 opb)
bc67d8a6 7409 || IS_COREFILE_NOTE (segment, section))
252b5132 7410 {
bc67d8a6 7411 if (map->count == 0)
252b5132
RH
7412 {
7413 /* If the first section in a segment does not start at
bc67d8a6
NC
7414 the beginning of the segment, then something is
7415 wrong. */
9933dc52
AM
7416 if (align_power (map->p_paddr
7417 + (map->includes_filehdr
7418 ? iehdr->e_ehsize : 0)
7419 + (map->includes_phdrs
7420 ? iehdr->e_phnum * iehdr->e_phentsize
7421 : 0),
66631823
CE
7422 output_section->alignment_power * opb)
7423 != output_section->lma * opb)
9aea1e31 7424 goto sorry;
252b5132
RH
7425 }
7426 else
7427 {
0067a569 7428 asection *prev_sec;
252b5132 7429
bc67d8a6 7430 prev_sec = map->sections[map->count - 1];
252b5132
RH
7431
7432 /* If the gap between the end of the previous section
bc67d8a6
NC
7433 and the start of this section is more than
7434 maxpagesize then we need to start a new segment. */
eea6121a 7435 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7436 maxpagesize)
caf47ea6 7437 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7438 || (prev_sec->lma + prev_sec->size
079e9a2f 7439 > output_section->lma))
252b5132 7440 {
9933dc52
AM
7441 if (suggested_lma == NULL)
7442 suggested_lma = output_section;
252b5132
RH
7443
7444 continue;
7445 }
7446 }
7447
bc67d8a6 7448 map->sections[map->count++] = output_section;
252b5132
RH
7449 ++isec;
7450 sections[j] = NULL;
9933dc52 7451 if (segment->p_type == PT_LOAD)
0a1b45a2 7452 section->segment_mark = true;
0067a569 7453 }
9933dc52
AM
7454 else if (suggested_lma == NULL)
7455 suggested_lma = output_section;
252b5132
RH
7456 }
7457
beab4532
NC
7458 /* PR 23932. A corrupt input file may contain sections that cannot
7459 be assigned to any segment - because for example they have a
9984857c
NC
7460 negative size - or segments that do not contain any sections.
7461 But there are also valid reasons why a segment can be empty.
7462 So allow a count of zero. */
252b5132
RH
7463
7464 /* Add the current segment to the list of built segments. */
c044fabd
KH
7465 *pointer_to_map = map;
7466 pointer_to_map = &map->next;
252b5132 7467
bc67d8a6 7468 if (isec < section_count)
252b5132
RH
7469 {
7470 /* We still have not allocated all of the sections to
7471 segments. Create a new segment here, initialise it
7472 and carry on looping. */
00bee008 7473 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
446f7ed5 7474 amt += section_count * sizeof (asection *);
5964fc3a 7475 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7476 if (map == NULL)
5ed6aba4
NC
7477 {
7478 free (sections);
0a1b45a2 7479 return false;
5ed6aba4 7480 }
252b5132
RH
7481
7482 /* Initialise the fields of the segment map. Set the physical
7483 physical address to the LMA of the first section that has
7484 not yet been assigned. */
0067a569
AM
7485 map->next = NULL;
7486 map->p_type = segment->p_type;
7487 map->p_flags = segment->p_flags;
7488 map->p_flags_valid = 1;
66631823 7489 map->p_paddr = suggested_lma->lma * opb;
5c44b38e 7490 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7491 map->includes_filehdr = 0;
0067a569 7492 map->includes_phdrs = 0;
252b5132 7493 }
9984857c
NC
7494
7495 continue;
7496 sorry:
7497 bfd_set_error (bfd_error_sorry);
7498 free (sections);
0a1b45a2 7499 return false;
252b5132 7500 }
bc67d8a6 7501 while (isec < section_count);
252b5132
RH
7502
7503 free (sections);
7504 }
7505
12bd6957 7506 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7507
7508 /* If we had to estimate the number of program headers that were
9ad5cbcf 7509 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7510 the offset if necessary. */
7511 if (phdr_adjust_seg != NULL)
7512 {
7513 unsigned int count;
c044fabd 7514
bc67d8a6 7515 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7516 count++;
252b5132 7517
bc67d8a6
NC
7518 if (count > phdr_adjust_num)
7519 phdr_adjust_seg->p_paddr
7520 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7521
7522 for (map = map_first; map != NULL; map = map->next)
7523 if (map->p_type == PT_PHDR)
7524 {
7525 bfd_vma adjust
7526 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7527 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7528 break;
7529 }
bc67d8a6 7530 }
c044fabd 7531
bc67d8a6 7532#undef SEGMENT_END
eecdbe52 7533#undef SECTION_SIZE
bc67d8a6
NC
7534#undef IS_CONTAINED_BY_VMA
7535#undef IS_CONTAINED_BY_LMA
0efc80c8 7536#undef IS_NOTE
252b5132 7537#undef IS_COREFILE_NOTE
bc67d8a6 7538#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7539#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7540#undef INCLUDE_SECTION_IN_SEGMENT
7541#undef SEGMENT_AFTER_SEGMENT
7542#undef SEGMENT_OVERLAPS
0a1b45a2 7543 return true;
252b5132
RH
7544}
7545
74e315db
L
7546/* Return true if p_align in the ELF program header in ABFD is valid. */
7547
7548static bool
7549elf_is_p_align_valid (bfd *abfd)
7550{
7551 unsigned int i;
7552 Elf_Internal_Phdr *segment;
7553 unsigned int num_segments;
7554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7555 bfd_size_type maxpagesize = bed->maxpagesize;
7556 bfd_size_type p_align = bed->p_align;
7557
7558 /* Return true if the default p_align value isn't set or the maximum
7559 page size is the same as the minimum page size. */
7560 if (p_align == 0 || maxpagesize == bed->minpagesize)
7561 return true;
7562
7563 /* When the default p_align value is set, p_align may be set to the
7564 default p_align value while segments are aligned to the maximum
7565 page size. In this case, the input p_align will be ignored and
7566 the maximum page size will be used to align the output segments. */
7567 segment = elf_tdata (abfd)->phdr;
7568 num_segments = elf_elfheader (abfd)->e_phnum;
7569 for (i = 0; i < num_segments; i++, segment++)
7570 if (segment->p_type == PT_LOAD
7571 && (segment->p_align != p_align
7572 || vma_page_aligned_bias (segment->p_vaddr,
7573 segment->p_offset,
7574 maxpagesize) != 0))
7575 return true;
7576
7577 return false;
7578}
7579
84d1d650
L
7580/* Copy ELF program header information. */
7581
0a1b45a2 7582static bool
84d1d650
L
7583copy_elf_program_header (bfd *ibfd, bfd *obfd)
7584{
7585 Elf_Internal_Ehdr *iehdr;
7586 struct elf_segment_map *map;
7587 struct elf_segment_map *map_first;
7588 struct elf_segment_map **pointer_to_map;
7589 Elf_Internal_Phdr *segment;
7590 unsigned int i;
7591 unsigned int num_segments;
0a1b45a2
AM
7592 bool phdr_included = false;
7593 bool p_paddr_valid;
74e315db 7594 bool p_palign_valid;
502794d4 7595 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
84d1d650
L
7596
7597 iehdr = elf_elfheader (ibfd);
7598
7599 map_first = NULL;
7600 pointer_to_map = &map_first;
7601
88967714
AM
7602 /* If all the segment p_paddr fields are zero, don't set
7603 map->p_paddr_valid. */
0a1b45a2 7604 p_paddr_valid = false;
84d1d650 7605 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7606 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7607 i < num_segments;
7608 i++, segment++)
7609 if (segment->p_paddr != 0)
7610 {
0a1b45a2 7611 p_paddr_valid = true;
88967714
AM
7612 break;
7613 }
7614
74e315db
L
7615 p_palign_valid = elf_is_p_align_valid (ibfd);
7616
84d1d650
L
7617 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7618 i < num_segments;
7619 i++, segment++)
7620 {
7621 asection *section;
7622 unsigned int section_count;
986f0783 7623 size_t amt;
84d1d650 7624 Elf_Internal_Shdr *this_hdr;
53020534 7625 asection *first_section = NULL;
a76e6f2f 7626 asection *lowest_section;
84d1d650 7627
84d1d650
L
7628 /* Compute how many sections are in this segment. */
7629 for (section = ibfd->sections, section_count = 0;
7630 section != NULL;
7631 section = section->next)
7632 {
7633 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7634 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7635 {
a76e6f2f
AM
7636 if (first_section == NULL)
7637 first_section = section;
3271a814
NS
7638 section_count++;
7639 }
84d1d650
L
7640 }
7641
7642 /* Allocate a segment map big enough to contain
7643 all of the sections we have selected. */
00bee008 7644 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
986f0783 7645 amt += section_count * sizeof (asection *);
a50b1753 7646 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650 7647 if (map == NULL)
0a1b45a2 7648 return false;
84d1d650
L
7649
7650 /* Initialize the fields of the output segment map with the
7651 input segment. */
7652 map->next = NULL;
7653 map->p_type = segment->p_type;
7654 map->p_flags = segment->p_flags;
7655 map->p_flags_valid = 1;
7656 map->p_paddr = segment->p_paddr;
88967714 7657 map->p_paddr_valid = p_paddr_valid;
3f570048 7658 map->p_align = segment->p_align;
74e315db 7659 map->p_align_valid = p_palign_valid;
3271a814 7660 map->p_vaddr_offset = 0;
84d1d650 7661
04c3a755
NS
7662 if (map->p_type == PT_GNU_RELRO
7663 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7664 {
7665 /* The PT_GNU_RELRO segment may contain the first a few
7666 bytes in the .got.plt section even if the whole .got.plt
7667 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7668 change the size of the PT_GNU_RELRO segment.
7669 Similarly, PT_GNU_STACK size is significant on uclinux
7670 systems. */
9433b9b1 7671 map->p_size = segment->p_memsz;
b10a8ae0
L
7672 map->p_size_valid = 1;
7673 }
7674
84d1d650
L
7675 /* Determine if this segment contains the ELF file header
7676 and if it contains the program headers themselves. */
7677 map->includes_filehdr = (segment->p_offset == 0
7678 && segment->p_filesz >= iehdr->e_ehsize);
7679
7680 map->includes_phdrs = 0;
7681 if (! phdr_included || segment->p_type != PT_LOAD)
7682 {
7683 map->includes_phdrs =
7684 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7685 && (segment->p_offset + segment->p_filesz
7686 >= ((bfd_vma) iehdr->e_phoff
7687 + iehdr->e_phnum * iehdr->e_phentsize)));
7688
7689 if (segment->p_type == PT_LOAD && map->includes_phdrs)
0a1b45a2 7690 phdr_included = true;
84d1d650
L
7691 }
7692
bbefd0a9 7693 lowest_section = NULL;
84d1d650
L
7694 if (section_count != 0)
7695 {
7696 unsigned int isec = 0;
7697
53020534 7698 for (section = first_section;
84d1d650
L
7699 section != NULL;
7700 section = section->next)
7701 {
7702 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7703 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7704 {
7705 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7706 if ((section->flags & SEC_ALLOC) != 0)
7707 {
7708 bfd_vma seg_off;
7709
bbefd0a9
AM
7710 if (lowest_section == NULL
7711 || section->lma < lowest_section->lma)
fb8a5684
AM
7712 lowest_section = section;
7713
a76e6f2f
AM
7714 /* Section lmas are set up from PT_LOAD header
7715 p_paddr in _bfd_elf_make_section_from_shdr.
7716 If this header has a p_paddr that disagrees
7717 with the section lma, flag the p_paddr as
7718 invalid. */
7719 if ((section->flags & SEC_LOAD) != 0)
7720 seg_off = this_hdr->sh_offset - segment->p_offset;
7721 else
7722 seg_off = this_hdr->sh_addr - segment->p_vaddr;
502794d4 7723 if (section->lma * opb - segment->p_paddr != seg_off)
0a1b45a2 7724 map->p_paddr_valid = false;
a76e6f2f 7725 }
53020534
L
7726 if (isec == section_count)
7727 break;
7728 }
84d1d650
L
7729 }
7730 }
7731
5d695627 7732 if (section_count == 0)
502794d4 7733 map->p_vaddr_offset = segment->p_vaddr / opb;
30fe1832
AM
7734 else if (map->p_paddr_valid)
7735 {
7736 /* Account for padding before the first section in the segment. */
7737 bfd_vma hdr_size = 0;
7738 if (map->includes_filehdr)
7739 hdr_size = iehdr->e_ehsize;
7740 if (map->includes_phdrs)
7741 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7742
502794d4 7743 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
30fe1832
AM
7744 - (lowest_section ? lowest_section->lma : 0));
7745 }
a76e6f2f 7746
84d1d650
L
7747 map->count = section_count;
7748 *pointer_to_map = map;
7749 pointer_to_map = &map->next;
7750 }
7751
12bd6957 7752 elf_seg_map (obfd) = map_first;
0a1b45a2 7753 return true;
84d1d650
L
7754}
7755
7756/* Copy private BFD data. This copies or rewrites ELF program header
7757 information. */
7758
0a1b45a2 7759static bool
84d1d650
L
7760copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7761{
c410035d
AM
7762 bfd_vma maxpagesize;
7763
84d1d650
L
7764 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7765 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 7766 return true;
84d1d650
L
7767
7768 if (elf_tdata (ibfd)->phdr == NULL)
0a1b45a2 7769 return true;
84d1d650
L
7770
7771 if (ibfd->xvec == obfd->xvec)
7772 {
cb3ff1e5
NC
7773 /* Check to see if any sections in the input BFD
7774 covered by ELF program header have changed. */
d55ce4e2 7775 Elf_Internal_Phdr *segment;
84d1d650
L
7776 asection *section, *osec;
7777 unsigned int i, num_segments;
7778 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7779 const struct elf_backend_data *bed;
7780
7781 bed = get_elf_backend_data (ibfd);
7782
7783 /* Regenerate the segment map if p_paddr is set to 0. */
7784 if (bed->want_p_paddr_set_to_zero)
7785 goto rewrite;
84d1d650
L
7786
7787 /* Initialize the segment mark field. */
7788 for (section = obfd->sections; section != NULL;
7789 section = section->next)
0a1b45a2 7790 section->segment_mark = false;
84d1d650
L
7791
7792 num_segments = elf_elfheader (ibfd)->e_phnum;
7793 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7794 i < num_segments;
7795 i++, segment++)
7796 {
5f6999aa
NC
7797 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7798 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7799 which severly confuses things, so always regenerate the segment
7800 map in this case. */
7801 if (segment->p_paddr == 0
7802 && segment->p_memsz == 0
7803 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7804 goto rewrite;
5f6999aa 7805
84d1d650
L
7806 for (section = ibfd->sections;
7807 section != NULL; section = section->next)
7808 {
7809 /* We mark the output section so that we know it comes
7810 from the input BFD. */
7811 osec = section->output_section;
7812 if (osec)
0a1b45a2 7813 osec->segment_mark = true;
84d1d650
L
7814
7815 /* Check if this section is covered by the segment. */
7816 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7817 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7818 {
7819 /* FIXME: Check if its output section is changed or
7820 removed. What else do we need to check? */
7821 if (osec == NULL
7822 || section->flags != osec->flags
7823 || section->lma != osec->lma
7824 || section->vma != osec->vma
7825 || section->size != osec->size
7826 || section->rawsize != osec->rawsize
7827 || section->alignment_power != osec->alignment_power)
7828 goto rewrite;
7829 }
7830 }
7831 }
7832
cb3ff1e5 7833 /* Check to see if any output section do not come from the
84d1d650
L
7834 input BFD. */
7835 for (section = obfd->sections; section != NULL;
7836 section = section->next)
7837 {
535b785f 7838 if (!section->segment_mark)
84d1d650
L
7839 goto rewrite;
7840 else
0a1b45a2 7841 section->segment_mark = false;
84d1d650
L
7842 }
7843
7844 return copy_elf_program_header (ibfd, obfd);
7845 }
7846
dc1e8a47 7847 rewrite:
c410035d 7848 maxpagesize = 0;
f1d85785
L
7849 if (ibfd->xvec == obfd->xvec)
7850 {
7851 /* When rewriting program header, set the output maxpagesize to
7852 the maximum alignment of input PT_LOAD segments. */
7853 Elf_Internal_Phdr *segment;
7854 unsigned int i;
7855 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
f1d85785
L
7856
7857 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7858 i < num_segments;
7859 i++, segment++)
7860 if (segment->p_type == PT_LOAD
7861 && maxpagesize < segment->p_align)
c86934ce
NC
7862 {
7863 /* PR 17512: file: f17299af. */
7864 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7865 /* xgettext:c-format */
2dcf00ce
AM
7866 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7867 PRIx64 " is too large"),
7868 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7869 else
7870 maxpagesize = segment->p_align;
7871 }
f1d85785 7872 }
c410035d
AM
7873 if (maxpagesize == 0)
7874 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
f1d85785 7875
c410035d 7876 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
84d1d650
L
7877}
7878
ccd2ec6a
L
7879/* Initialize private output section information from input section. */
7880
0a1b45a2 7881bool
ccd2ec6a
L
7882_bfd_elf_init_private_section_data (bfd *ibfd,
7883 asection *isec,
7884 bfd *obfd,
7885 asection *osec,
7886 struct bfd_link_info *link_info)
7887
7888{
7889 Elf_Internal_Shdr *ihdr, *ohdr;
0a1b45a2
AM
7890 bool final_link = (link_info != NULL
7891 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7892
7893 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7894 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7895 return true;
ccd2ec6a 7896
ba85c43e
NC
7897 BFD_ASSERT (elf_section_data (osec) != NULL);
7898
8c803a2d
AM
7899 /* If this is a known ABI section, ELF section type and flags may
7900 have been set up when OSEC was created. For normal sections we
7901 allow the user to override the type and flags other than
7902 SHF_MASKOS and SHF_MASKPROC. */
7903 if (elf_section_type (osec) == SHT_PROGBITS
7904 || elf_section_type (osec) == SHT_NOTE
7905 || elf_section_type (osec) == SHT_NOBITS)
7906 elf_section_type (osec) = SHT_NULL;
7907 /* For objcopy and relocatable link, copy the ELF section type from
7908 the input file if the BFD section flags are the same. (If they
7909 are different the user may be doing something like
7910 "objcopy --set-section-flags .text=alloc,data".) For a final
7911 link allow some flags that the linker clears to differ. */
42bb2e33 7912 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7913 && (osec->flags == isec->flags
7914 || (final_link
7915 && ((osec->flags ^ isec->flags)
0814be7d 7916 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7917 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7918
7919 /* FIXME: Is this correct for all OS/PROC specific flags? */
8c803a2d
AM
7920 elf_section_flags (osec) = (elf_section_flags (isec)
7921 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7922
a91e1603 7923 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7924 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7925 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7926 elf_section_data (osec)->this_hdr.sh_info
7927 = elf_section_data (isec)->this_hdr.sh_info;
7928
ccd2ec6a
L
7929 /* Set things up for objcopy and relocatable link. The output
7930 SHT_GROUP section will have its elf_next_in_group pointing back
7931 to the input group members. Ignore linker created group section.
7932 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7933 if ((link_info == NULL
7934 || !link_info->resolve_section_groups)
7935 && (elf_sec_group (isec) == NULL
7936 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7937 {
7bdf4127
AB
7938 if (elf_section_flags (isec) & SHF_GROUP)
7939 elf_section_flags (osec) |= SHF_GROUP;
7940 elf_next_in_group (osec) = elf_next_in_group (isec);
7941 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7942 }
7943
7bdf4127
AB
7944 /* If not decompress, preserve SHF_COMPRESSED. */
7945 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7946 elf_section_flags (osec) |= (elf_section_flags (isec)
7947 & SHF_COMPRESSED);
7948
ccd2ec6a
L
7949 ihdr = &elf_section_data (isec)->this_hdr;
7950
7951 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7952 don't use the output section of the linked-to section since it
7953 may be NULL at this point. */
7954 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7955 {
7956 ohdr = &elf_section_data (osec)->this_hdr;
7957 ohdr->sh_flags |= SHF_LINK_ORDER;
7958 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7959 }
7960
7961 osec->use_rela_p = isec->use_rela_p;
7962
0a1b45a2 7963 return true;
ccd2ec6a
L
7964}
7965
252b5132
RH
7966/* Copy private section information. This copies over the entsize
7967 field, and sometimes the info field. */
7968
0a1b45a2 7969bool
217aa764
AM
7970_bfd_elf_copy_private_section_data (bfd *ibfd,
7971 asection *isec,
7972 bfd *obfd,
7973 asection *osec)
252b5132
RH
7974{
7975 Elf_Internal_Shdr *ihdr, *ohdr;
7976
7977 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7978 || obfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 7979 return true;
252b5132 7980
252b5132
RH
7981 ihdr = &elf_section_data (isec)->this_hdr;
7982 ohdr = &elf_section_data (osec)->this_hdr;
7983
7984 ohdr->sh_entsize = ihdr->sh_entsize;
7985
7986 if (ihdr->sh_type == SHT_SYMTAB
7987 || ihdr->sh_type == SHT_DYNSYM
7988 || ihdr->sh_type == SHT_GNU_verneed
7989 || ihdr->sh_type == SHT_GNU_verdef)
7990 ohdr->sh_info = ihdr->sh_info;
7991
ccd2ec6a
L
7992 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7993 NULL);
252b5132
RH
7994}
7995
d0bf826b
AM
7996/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7997 necessary if we are removing either the SHT_GROUP section or any of
7998 the group member sections. DISCARDED is the value that a section's
7999 output_section has if the section will be discarded, NULL when this
8000 function is called from objcopy, bfd_abs_section_ptr when called
8001 from the linker. */
80fccad2 8002
0a1b45a2 8003bool
d0bf826b 8004_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 8005{
30288845
AM
8006 asection *isec;
8007
30288845 8008 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 8009 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
8010 {
8011 asection *first = elf_next_in_group (isec);
8012 asection *s = first;
d0bf826b
AM
8013 bfd_size_type removed = 0;
8014
30288845
AM
8015 while (s != NULL)
8016 {
415f38a6
AM
8017 /* If this member section is being output but the
8018 SHT_GROUP section is not, then clear the group info
8019 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
8020 if (s->output_section != discarded
8021 && isec->output_section == discarded)
30288845
AM
8022 {
8023 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8024 elf_group_name (s->output_section) = NULL;
8025 }
3349112e 8026 else
6e5e9d58
AM
8027 {
8028 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3349112e
L
8029 if (s->output_section == discarded
8030 && isec->output_section != discarded)
8031 {
8032 /* Conversely, if the member section is not being
8033 output but the SHT_GROUP section is, then adjust
8034 its size. */
8035 removed += 4;
8036 if (elf_sec->rel.hdr != NULL
8037 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8038 removed += 4;
8039 if (elf_sec->rela.hdr != NULL
8040 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8041 removed += 4;
8042 }
8043 else
8044 {
8045 /* Also adjust for zero-sized relocation member
8046 section. */
8047 if (elf_sec->rel.hdr != NULL
8048 && elf_sec->rel.hdr->sh_size == 0)
8049 removed += 4;
8050 if (elf_sec->rela.hdr != NULL
8051 && elf_sec->rela.hdr->sh_size == 0)
8052 removed += 4;
8053 }
6e5e9d58 8054 }
30288845
AM
8055 s = elf_next_in_group (s);
8056 if (s == first)
8057 break;
8058 }
d0bf826b
AM
8059 if (removed != 0)
8060 {
8061 if (discarded != NULL)
8062 {
8063 /* If we've been called for ld -r, then we need to
6e5e9d58 8064 adjust the input section size. */
d0bf826b
AM
8065 if (isec->rawsize == 0)
8066 isec->rawsize = isec->size;
8067 isec->size = isec->rawsize - removed;
6e5e9d58
AM
8068 if (isec->size <= 4)
8069 {
8070 isec->size = 0;
8071 isec->flags |= SEC_EXCLUDE;
8072 }
d0bf826b 8073 }
182ad375 8074 else if (isec->output_section != NULL)
d0bf826b
AM
8075 {
8076 /* Adjust the output section size when called from
8077 objcopy. */
8078 isec->output_section->size -= removed;
6e5e9d58
AM
8079 if (isec->output_section->size <= 4)
8080 {
8081 isec->output_section->size = 0;
8082 isec->output_section->flags |= SEC_EXCLUDE;
8083 }
d0bf826b
AM
8084 }
8085 }
30288845
AM
8086 }
8087
0a1b45a2 8088 return true;
80fccad2
BW
8089}
8090
d0bf826b
AM
8091/* Copy private header information. */
8092
0a1b45a2 8093bool
d0bf826b
AM
8094_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8095{
8096 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8097 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8098 return true;
d0bf826b
AM
8099
8100 /* Copy over private BFD data if it has not already been copied.
8101 This must be done here, rather than in the copy_private_bfd_data
8102 entry point, because the latter is called after the section
8103 contents have been set, which means that the program headers have
8104 already been worked out. */
12bd6957 8105 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
8106 {
8107 if (! copy_private_bfd_data (ibfd, obfd))
0a1b45a2 8108 return false;
d0bf826b
AM
8109 }
8110
8111 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8112}
8113
252b5132
RH
8114/* Copy private symbol information. If this symbol is in a section
8115 which we did not map into a BFD section, try to map the section
8116 index correctly. We use special macro definitions for the mapped
8117 section indices; these definitions are interpreted by the
8118 swap_out_syms function. */
8119
9ad5cbcf
AM
8120#define MAP_ONESYMTAB (SHN_HIOS + 1)
8121#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8122#define MAP_STRTAB (SHN_HIOS + 3)
8123#define MAP_SHSTRTAB (SHN_HIOS + 4)
8124#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 8125
0a1b45a2 8126bool
217aa764
AM
8127_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8128 asymbol *isymarg,
8129 bfd *obfd,
8130 asymbol *osymarg)
252b5132
RH
8131{
8132 elf_symbol_type *isym, *osym;
8133
8134 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8135 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 8136 return true;
252b5132 8137
c1229f84
AM
8138 isym = elf_symbol_from (isymarg);
8139 osym = elf_symbol_from (osymarg);
252b5132
RH
8140
8141 if (isym != NULL
8424d8f5 8142 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
8143 && osym != NULL
8144 && bfd_is_abs_section (isym->symbol.section))
8145 {
8146 unsigned int shndx;
8147
8148 shndx = isym->internal_elf_sym.st_shndx;
8149 if (shndx == elf_onesymtab (ibfd))
8150 shndx = MAP_ONESYMTAB;
8151 else if (shndx == elf_dynsymtab (ibfd))
8152 shndx = MAP_DYNSYMTAB;
12bd6957 8153 else if (shndx == elf_strtab_sec (ibfd))
252b5132 8154 shndx = MAP_STRTAB;
12bd6957 8155 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 8156 shndx = MAP_SHSTRTAB;
6a40cf0c 8157 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 8158 shndx = MAP_SYM_SHNDX;
252b5132
RH
8159 osym->internal_elf_sym.st_shndx = shndx;
8160 }
8161
0a1b45a2 8162 return true;
252b5132
RH
8163}
8164
8165/* Swap out the symbols. */
8166
0a1b45a2 8167static bool
217aa764 8168swap_out_syms (bfd *abfd,
ef10c3ac 8169 struct elf_strtab_hash **sttp,
3d16b64e
NA
8170 int relocatable_p,
8171 struct bfd_link_info *info)
252b5132 8172{
9c5bfbb7 8173 const struct elf_backend_data *bed;
1f4361a7 8174 unsigned int symcount;
079e9a2f 8175 asymbol **syms;
ef10c3ac 8176 struct elf_strtab_hash *stt;
079e9a2f 8177 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8178 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 8179 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 8180 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
8181 bfd_byte *outbound_syms;
8182 bfd_byte *outbound_shndx;
ef10c3ac 8183 unsigned long outbound_syms_index;
1f4361a7 8184 unsigned int idx;
12bd6957 8185 unsigned int num_locals;
1f4361a7 8186 size_t amt;
0a1b45a2 8187 bool name_local_sections;
252b5132 8188
12bd6957 8189 if (!elf_map_symbols (abfd, &num_locals))
0a1b45a2 8190 return false;
252b5132 8191
c044fabd 8192 /* Dump out the symtabs. */
ef10c3ac 8193 stt = _bfd_elf_strtab_init ();
079e9a2f 8194 if (stt == NULL)
0a1b45a2 8195 return false;
252b5132 8196
079e9a2f
AM
8197 bed = get_elf_backend_data (abfd);
8198 symcount = bfd_get_symcount (abfd);
8199 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8200 symtab_hdr->sh_type = SHT_SYMTAB;
8201 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8202 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8203 symtab_hdr->sh_info = num_locals + 1;
72de5009 8204 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8205
8206 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8207 symstrtab_hdr->sh_type = SHT_STRTAB;
8208
ef10c3ac 8209 /* Allocate buffer to swap out the .strtab section. */
1f4361a7
AM
8210 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8211 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
ef10c3ac 8212 {
1f4361a7 8213 bfd_set_error (bfd_error_no_memory);
ef10c3ac 8214 _bfd_elf_strtab_free (stt);
0a1b45a2 8215 return false;
ef10c3ac
L
8216 }
8217
1f4361a7
AM
8218 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8219 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
5ed6aba4 8220 {
1f4361a7
AM
8221 error_no_mem:
8222 bfd_set_error (bfd_error_no_memory);
8223 error_return:
ef10c3ac 8224 free (symstrtab);
1f4361a7 8225 _bfd_elf_strtab_free (stt);
0a1b45a2 8226 return false;
5ed6aba4 8227 }
217aa764 8228 symtab_hdr->contents = outbound_syms;
ef10c3ac 8229 outbound_syms_index = 0;
252b5132 8230
9ad5cbcf 8231 outbound_shndx = NULL;
6a40cf0c
NC
8232
8233 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8234 {
6a40cf0c
NC
8235 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8236 if (symtab_shndx_hdr->sh_name != 0)
8237 {
1f4361a7
AM
8238 if (_bfd_mul_overflow (symcount + 1,
8239 sizeof (Elf_External_Sym_Shndx), &amt))
8240 goto error_no_mem;
8241 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
6a40cf0c
NC
8242 if (outbound_shndx == NULL)
8243 goto error_return;
5ed6aba4 8244
6a40cf0c
NC
8245 symtab_shndx_hdr->contents = outbound_shndx;
8246 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8247 symtab_shndx_hdr->sh_size = amt;
8248 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8249 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8250 }
8251 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8252 }
8253
589e6347 8254 /* Now generate the data (for "contents"). */
079e9a2f
AM
8255 {
8256 /* Fill in zeroth symbol and swap it out. */
8257 Elf_Internal_Sym sym;
8258 sym.st_name = 0;
8259 sym.st_value = 0;
8260 sym.st_size = 0;
8261 sym.st_info = 0;
8262 sym.st_other = 0;
8263 sym.st_shndx = SHN_UNDEF;
35fc36a8 8264 sym.st_target_internal = 0;
ef10c3ac
L
8265 symstrtab[0].sym = sym;
8266 symstrtab[0].dest_index = outbound_syms_index;
ef10c3ac 8267 outbound_syms_index++;
079e9a2f 8268 }
252b5132 8269
174fd7f9
RS
8270 name_local_sections
8271 = (bed->elf_backend_name_local_section_symbols
8272 && bed->elf_backend_name_local_section_symbols (abfd));
8273
079e9a2f 8274 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8275 for (idx = 0; idx < symcount;)
252b5132 8276 {
252b5132 8277 Elf_Internal_Sym sym;
079e9a2f
AM
8278 bfd_vma value = syms[idx]->value;
8279 elf_symbol_type *type_ptr;
8280 flagword flags = syms[idx]->flags;
8281 int type;
252b5132 8282
174fd7f9
RS
8283 if (!name_local_sections
8284 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8285 {
8286 /* Local section symbols have no name. */
ef10c3ac 8287 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8288 }
8289 else
8290 {
ef10c3ac
L
8291 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8292 to get the final offset for st_name. */
8293 sym.st_name
8294 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
0a1b45a2 8295 false);
079e9a2f 8296 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8297 goto error_return;
079e9a2f 8298 }
252b5132 8299
c1229f84 8300 type_ptr = elf_symbol_from (syms[idx]);
252b5132 8301
079e9a2f
AM
8302 if ((flags & BSF_SECTION_SYM) == 0
8303 && bfd_is_com_section (syms[idx]->section))
8304 {
8305 /* ELF common symbols put the alignment into the `value' field,
8306 and the size into the `size' field. This is backwards from
8307 how BFD handles it, so reverse it here. */
8308 sym.st_size = value;
8309 if (type_ptr == NULL
8310 || type_ptr->internal_elf_sym.st_value == 0)
8311 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8312 else
8313 sym.st_value = type_ptr->internal_elf_sym.st_value;
8314 sym.st_shndx = _bfd_elf_section_from_bfd_section
8315 (abfd, syms[idx]->section);
8316 }
8317 else
8318 {
8319 asection *sec = syms[idx]->section;
cb33740c 8320 unsigned int shndx;
252b5132 8321
079e9a2f
AM
8322 if (sec->output_section)
8323 {
8324 value += sec->output_offset;
8325 sec = sec->output_section;
8326 }
589e6347 8327
079e9a2f
AM
8328 /* Don't add in the section vma for relocatable output. */
8329 if (! relocatable_p)
8330 value += sec->vma;
8331 sym.st_value = value;
8332 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8333
8334 if (bfd_is_abs_section (sec)
8335 && type_ptr != NULL
8336 && type_ptr->internal_elf_sym.st_shndx != 0)
8337 {
8338 /* This symbol is in a real ELF section which we did
8339 not create as a BFD section. Undo the mapping done
8340 by copy_private_symbol_data. */
8341 shndx = type_ptr->internal_elf_sym.st_shndx;
8342 switch (shndx)
8343 {
8344 case MAP_ONESYMTAB:
8345 shndx = elf_onesymtab (abfd);
8346 break;
8347 case MAP_DYNSYMTAB:
8348 shndx = elf_dynsymtab (abfd);
8349 break;
8350 case MAP_STRTAB:
12bd6957 8351 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8352 break;
8353 case MAP_SHSTRTAB:
12bd6957 8354 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8355 break;
9ad5cbcf 8356 case MAP_SYM_SHNDX:
6a40cf0c
NC
8357 if (elf_symtab_shndx_list (abfd))
8358 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8359 break;
00e49dff
NC
8360 case SHN_COMMON:
8361 case SHN_ABS:
15bc576a 8362 shndx = SHN_ABS;
079e9a2f 8363 break;
00e49dff
NC
8364 default:
8365 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8366 {
8367 if (bed->symbol_section_index)
8368 shndx = bed->symbol_section_index (abfd, type_ptr);
8369 /* Otherwise just leave the index alone. */
8370 }
8371 else
8372 {
8373 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8374 _bfd_error_handler (_("%pB: \
8375Unable to handle section index %x in ELF symbol. Using ABS instead."),
8376 abfd, shndx);
8377 shndx = SHN_ABS;
8378 }
8379 break;
079e9a2f
AM
8380 }
8381 }
8382 else
8383 {
8384 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8385
cb33740c 8386 if (shndx == SHN_BAD)
079e9a2f
AM
8387 {
8388 asection *sec2;
8389
8390 /* Writing this would be a hell of a lot easier if
8391 we had some decent documentation on bfd, and
8392 knew what to expect of the library, and what to
8393 demand of applications. For example, it
8394 appears that `objcopy' might not set the
8395 section of a symbol to be a section that is
8396 actually in the output file. */
8397 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8398 if (sec2 != NULL)
8399 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8400 if (shndx == SHN_BAD)
589e6347 8401 {
695344c0 8402 /* xgettext:c-format */
9793eb77
AM
8403 _bfd_error_handler
8404 (_("unable to find equivalent output section"
8405 " for symbol '%s' from section '%s'"),
8406 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8407 sec->name);
811072d8 8408 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8409 goto error_return;
589e6347 8410 }
079e9a2f
AM
8411 }
8412 }
252b5132 8413
079e9a2f
AM
8414 sym.st_shndx = shndx;
8415 }
252b5132 8416
13ae64f3
JJ
8417 if ((flags & BSF_THREAD_LOCAL) != 0)
8418 type = STT_TLS;
d8045f23
NC
8419 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8420 type = STT_GNU_IFUNC;
13ae64f3 8421 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8422 type = STT_FUNC;
8423 else if ((flags & BSF_OBJECT) != 0)
8424 type = STT_OBJECT;
d9352518
DB
8425 else if ((flags & BSF_RELC) != 0)
8426 type = STT_RELC;
8427 else if ((flags & BSF_SRELC) != 0)
8428 type = STT_SRELC;
079e9a2f
AM
8429 else
8430 type = STT_NOTYPE;
252b5132 8431
13ae64f3
JJ
8432 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8433 type = STT_TLS;
8434
589e6347 8435 /* Processor-specific types. */
079e9a2f
AM
8436 if (type_ptr != NULL
8437 && bed->elf_backend_get_symbol_type)
8438 type = ((*bed->elf_backend_get_symbol_type)
8439 (&type_ptr->internal_elf_sym, type));
252b5132 8440
079e9a2f
AM
8441 if (flags & BSF_SECTION_SYM)
8442 {
8443 if (flags & BSF_GLOBAL)
8444 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8445 else
8446 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8447 }
8448 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8449 {
b8871f35
L
8450 if (type != STT_TLS)
8451 {
8452 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8453 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8454 ? STT_COMMON : STT_OBJECT);
8455 else
8456 type = ((flags & BSF_ELF_COMMON) != 0
8457 ? STT_COMMON : STT_OBJECT);
8458 }
8459 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8460 }
079e9a2f
AM
8461 else if (bfd_is_und_section (syms[idx]->section))
8462 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8463 ? STB_WEAK
8464 : STB_GLOBAL),
8465 type);
8466 else if (flags & BSF_FILE)
8467 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8468 else
8469 {
8470 int bind = STB_LOCAL;
252b5132 8471
079e9a2f
AM
8472 if (flags & BSF_LOCAL)
8473 bind = STB_LOCAL;
3e7a7d11
NC
8474 else if (flags & BSF_GNU_UNIQUE)
8475 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8476 else if (flags & BSF_WEAK)
8477 bind = STB_WEAK;
8478 else if (flags & BSF_GLOBAL)
8479 bind = STB_GLOBAL;
252b5132 8480
079e9a2f
AM
8481 sym.st_info = ELF_ST_INFO (bind, type);
8482 }
252b5132 8483
079e9a2f 8484 if (type_ptr != NULL)
35fc36a8
RS
8485 {
8486 sym.st_other = type_ptr->internal_elf_sym.st_other;
8487 sym.st_target_internal
8488 = type_ptr->internal_elf_sym.st_target_internal;
8489 }
079e9a2f 8490 else
35fc36a8
RS
8491 {
8492 sym.st_other = 0;
8493 sym.st_target_internal = 0;
8494 }
252b5132 8495
ef10c3ac
L
8496 idx++;
8497 symstrtab[idx].sym = sym;
8498 symstrtab[idx].dest_index = outbound_syms_index;
ef10c3ac
L
8499
8500 outbound_syms_index++;
ef10c3ac
L
8501 }
8502
8503 /* Finalize the .strtab section. */
8504 _bfd_elf_strtab_finalize (stt);
8505
8506 /* Swap out the .strtab section. */
8507 for (idx = 0; idx <= symcount; idx++)
8508 {
8509 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8510 if (elfsym->sym.st_name == (unsigned long) -1)
8511 elfsym->sym.st_name = 0;
8512 else
8513 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8514 elfsym->sym.st_name);
3d16b64e
NA
8515 if (info && info->callbacks->ctf_new_symbol)
8516 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8517 &elfsym->sym);
8518
8519 /* Inform the linker of the addition of this symbol. */
8520
ef10c3ac
L
8521 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8522 (outbound_syms
8523 + (elfsym->dest_index
8524 * bed->s->sizeof_sym)),
b03b65e2
AM
8525 NPTR_ADD (outbound_shndx,
8526 (elfsym->dest_index
8527 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8528 }
ef10c3ac 8529 free (symstrtab);
252b5132 8530
079e9a2f 8531 *sttp = stt;
ef10c3ac 8532 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8533 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8534 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8535 symstrtab_hdr->sh_addr = 0;
8536 symstrtab_hdr->sh_entsize = 0;
8537 symstrtab_hdr->sh_link = 0;
8538 symstrtab_hdr->sh_info = 0;
8539 symstrtab_hdr->sh_addralign = 1;
252b5132 8540
0a1b45a2 8541 return true;
252b5132
RH
8542}
8543
8544/* Return the number of bytes required to hold the symtab vector.
8545
8546 Note that we base it on the count plus 1, since we will null terminate
8547 the vector allocated based on this size. However, the ELF symbol table
8548 always has a dummy entry as symbol #0, so it ends up even. */
8549
8550long
217aa764 8551_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8552{
3a551c7a 8553 bfd_size_type symcount;
252b5132
RH
8554 long symtab_size;
8555 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8556
8557 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8558 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8559 {
8560 bfd_set_error (bfd_error_file_too_big);
8561 return -1;
8562 }
b5f386d5
AM
8563 symtab_size = symcount * (sizeof (asymbol *));
8564 if (symcount == 0)
8565 symtab_size = sizeof (asymbol *);
8566 else if (!bfd_write_p (abfd))
8567 {
8568 ufile_ptr filesize = bfd_get_file_size (abfd);
8569
8570 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8571 {
8572 bfd_set_error (bfd_error_file_truncated);
8573 return -1;
8574 }
8575 }
252b5132
RH
8576
8577 return symtab_size;
8578}
8579
8580long
217aa764 8581_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8582{
3a551c7a 8583 bfd_size_type symcount;
252b5132
RH
8584 long symtab_size;
8585 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8586
8587 if (elf_dynsymtab (abfd) == 0)
8588 {
8589 bfd_set_error (bfd_error_invalid_operation);
8590 return -1;
8591 }
8592
8593 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b5f386d5 8594 if (symcount > LONG_MAX / sizeof (asymbol *))
3a551c7a
AM
8595 {
8596 bfd_set_error (bfd_error_file_too_big);
8597 return -1;
8598 }
b5f386d5
AM
8599 symtab_size = symcount * (sizeof (asymbol *));
8600 if (symcount == 0)
8601 symtab_size = sizeof (asymbol *);
8602 else if (!bfd_write_p (abfd))
8603 {
8604 ufile_ptr filesize = bfd_get_file_size (abfd);
8605
8606 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8607 {
8608 bfd_set_error (bfd_error_file_truncated);
8609 return -1;
8610 }
8611 }
252b5132
RH
8612
8613 return symtab_size;
8614}
8615
8616long
3c568b8a 8617_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132 8618{
b5f386d5 8619 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
3c568b8a
AM
8620 {
8621 /* Sanity check reloc section size. */
8622 struct bfd_elf_section_data *d = elf_section_data (asect);
8623 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8624 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8625 ufile_ptr filesize = bfd_get_file_size (abfd);
8626
8627 if (filesize != 0 && ext_rel_size > filesize)
8628 {
8629 bfd_set_error (bfd_error_file_truncated);
8630 return -1;
8631 }
8632 }
8633
242a1159 8634#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8635 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8636 {
8637 bfd_set_error (bfd_error_file_too_big);
8638 return -1;
8639 }
242a1159 8640#endif
5cfe19e5 8641 return (asect->reloc_count + 1L) * sizeof (arelent *);
252b5132
RH
8642}
8643
8644/* Canonicalize the relocs. */
8645
8646long
217aa764
AM
8647_bfd_elf_canonicalize_reloc (bfd *abfd,
8648 sec_ptr section,
8649 arelent **relptr,
8650 asymbol **symbols)
252b5132
RH
8651{
8652 arelent *tblptr;
8653 unsigned int i;
9c5bfbb7 8654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8655
0a1b45a2 8656 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
8657 return -1;
8658
8659 tblptr = section->relocation;
8660 for (i = 0; i < section->reloc_count; i++)
8661 *relptr++ = tblptr++;
8662
8663 *relptr = NULL;
8664
8665 return section->reloc_count;
8666}
8667
8668long
6cee3f79 8669_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8670{
9c5bfbb7 8671 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8672 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
252b5132
RH
8673
8674 if (symcount >= 0)
ed48ec2e 8675 abfd->symcount = symcount;
252b5132
RH
8676 return symcount;
8677}
8678
8679long
217aa764
AM
8680_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8681 asymbol **allocation)
252b5132 8682{
9c5bfbb7 8683 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 8684 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
1f70368c
DJ
8685
8686 if (symcount >= 0)
ed48ec2e 8687 abfd->dynsymcount = symcount;
1f70368c 8688 return symcount;
252b5132
RH
8689}
8690
8615f3f2
AM
8691/* Return the size required for the dynamic reloc entries. Any loadable
8692 section that was actually installed in the BFD, and has type SHT_REL
8693 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8694 dynamic reloc section. */
252b5132
RH
8695
8696long
217aa764 8697_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8698{
3c568b8a 8699 bfd_size_type count, ext_rel_size;
252b5132
RH
8700 asection *s;
8701
8702 if (elf_dynsymtab (abfd) == 0)
8703 {
8704 bfd_set_error (bfd_error_invalid_operation);
8705 return -1;
8706 }
8707
3a551c7a 8708 count = 1;
3c568b8a 8709 ext_rel_size = 0;
252b5132 8710 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8711 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8712 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8713 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a 8714 {
3c568b8a
AM
8715 ext_rel_size += s->size;
8716 if (ext_rel_size < s->size)
8717 {
8718 bfd_set_error (bfd_error_file_truncated);
8719 return -1;
8720 }
3a551c7a
AM
8721 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8722 if (count > LONG_MAX / sizeof (arelent *))
8723 {
8724 bfd_set_error (bfd_error_file_too_big);
8725 return -1;
8726 }
8727 }
b5f386d5 8728 if (count > 1 && !bfd_write_p (abfd))
3c568b8a
AM
8729 {
8730 /* Sanity check reloc section sizes. */
8731 ufile_ptr filesize = bfd_get_file_size (abfd);
8732 if (filesize != 0 && ext_rel_size > filesize)
8733 {
8734 bfd_set_error (bfd_error_file_truncated);
8735 return -1;
8736 }
8737 }
3a551c7a 8738 return count * sizeof (arelent *);
252b5132
RH
8739}
8740
8615f3f2
AM
8741/* Canonicalize the dynamic relocation entries. Note that we return the
8742 dynamic relocations as a single block, although they are actually
8743 associated with particular sections; the interface, which was
8744 designed for SunOS style shared libraries, expects that there is only
8745 one set of dynamic relocs. Any loadable section that was actually
8746 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8747 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8748
8749long
217aa764
AM
8750_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8751 arelent **storage,
8752 asymbol **syms)
252b5132 8753{
0a1b45a2 8754 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
252b5132
RH
8755 asection *s;
8756 long ret;
8757
8758 if (elf_dynsymtab (abfd) == 0)
8759 {
8760 bfd_set_error (bfd_error_invalid_operation);
8761 return -1;
8762 }
8763
8764 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8765 ret = 0;
8766 for (s = abfd->sections; s != NULL; s = s->next)
8767 {
266b05cf 8768 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8769 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8770 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8771 {
8772 arelent *p;
8773 long count, i;
8774
0a1b45a2 8775 if (! (*slurp_relocs) (abfd, s, syms, true))
252b5132 8776 return -1;
eea6121a 8777 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8778 p = s->relocation;
8779 for (i = 0; i < count; i++)
8780 *storage++ = p++;
8781 ret += count;
8782 }
8783 }
8784
8785 *storage = NULL;
8786
8787 return ret;
8788}
8789\f
8790/* Read in the version information. */
8791
0a1b45a2
AM
8792bool
8793_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
252b5132
RH
8794{
8795 bfd_byte *contents = NULL;
fc0e6df6 8796 unsigned int freeidx = 0;
1f4361a7 8797 size_t amt;
fc0e6df6
PB
8798
8799 if (elf_dynverref (abfd) != 0)
8800 {
8801 Elf_Internal_Shdr *hdr;
8802 Elf_External_Verneed *everneed;
8803 Elf_Internal_Verneed *iverneed;
8804 unsigned int i;
d0fb9a8d 8805 bfd_byte *contents_end;
fc0e6df6
PB
8806
8807 hdr = &elf_tdata (abfd)->dynverref_hdr;
8808
bd61e135
AM
8809 if (hdr->sh_info == 0
8810 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8811 {
dc1e8a47 8812 error_return_bad_verref:
4eca0228 8813 _bfd_error_handler
871b3ab2 8814 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8815 bfd_set_error (bfd_error_bad_value);
dc1e8a47 8816 error_return_verref:
d0fb9a8d
JJ
8817 elf_tdata (abfd)->verref = NULL;
8818 elf_tdata (abfd)->cverrefs = 0;
8819 goto error_return;
8820 }
601a03ba 8821
2bb3687b
AM
8822 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8823 goto error_return_verref;
8824 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8825 if (contents == NULL)
d0fb9a8d 8826 goto error_return_verref;
fc0e6df6 8827
1f4361a7
AM
8828 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8829 {
8830 bfd_set_error (bfd_error_file_too_big);
8831 goto error_return_verref;
8832 }
8833 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
601a03ba 8834 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8835 goto error_return_verref;
8836
8837 BFD_ASSERT (sizeof (Elf_External_Verneed)
8838 == sizeof (Elf_External_Vernaux));
8839 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8840 everneed = (Elf_External_Verneed *) contents;
8841 iverneed = elf_tdata (abfd)->verref;
8842 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8843 {
8844 Elf_External_Vernaux *evernaux;
8845 Elf_Internal_Vernaux *ivernaux;
8846 unsigned int j;
8847
8848 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8849
8850 iverneed->vn_bfd = abfd;
8851
8852 iverneed->vn_filename =
8853 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8854 iverneed->vn_file);
8855 if (iverneed->vn_filename == NULL)
601a03ba 8856 goto error_return_bad_verref;
fc0e6df6 8857
d0fb9a8d
JJ
8858 if (iverneed->vn_cnt == 0)
8859 iverneed->vn_auxptr = NULL;
8860 else
8861 {
1f4361a7
AM
8862 if (_bfd_mul_overflow (iverneed->vn_cnt,
8863 sizeof (Elf_Internal_Vernaux), &amt))
8864 {
8865 bfd_set_error (bfd_error_file_too_big);
8866 goto error_return_verref;
8867 }
a50b1753 8868 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
1f4361a7 8869 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
8870 if (iverneed->vn_auxptr == NULL)
8871 goto error_return_verref;
8872 }
8873
8874 if (iverneed->vn_aux
8875 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8876 goto error_return_bad_verref;
fc0e6df6
PB
8877
8878 evernaux = ((Elf_External_Vernaux *)
8879 ((bfd_byte *) everneed + iverneed->vn_aux));
8880 ivernaux = iverneed->vn_auxptr;
8881 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8882 {
8883 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8884
8885 ivernaux->vna_nodename =
8886 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8887 ivernaux->vna_name);
8888 if (ivernaux->vna_nodename == NULL)
601a03ba 8889 goto error_return_bad_verref;
fc0e6df6 8890
25ff461f
AM
8891 if (ivernaux->vna_other > freeidx)
8892 freeidx = ivernaux->vna_other;
8893
8894 ivernaux->vna_nextptr = NULL;
8895 if (ivernaux->vna_next == 0)
8896 {
8897 iverneed->vn_cnt = j + 1;
8898 break;
8899 }
fc0e6df6
PB
8900 if (j + 1 < iverneed->vn_cnt)
8901 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8902
d0fb9a8d
JJ
8903 if (ivernaux->vna_next
8904 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8905 goto error_return_bad_verref;
d0fb9a8d 8906
fc0e6df6
PB
8907 evernaux = ((Elf_External_Vernaux *)
8908 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8909 }
8910
25ff461f
AM
8911 iverneed->vn_nextref = NULL;
8912 if (iverneed->vn_next == 0)
8913 break;
fc0e6df6
PB
8914 if (i + 1 < hdr->sh_info)
8915 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8916
d0fb9a8d
JJ
8917 if (iverneed->vn_next
8918 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8919 goto error_return_bad_verref;
d0fb9a8d 8920
fc0e6df6
PB
8921 everneed = ((Elf_External_Verneed *)
8922 ((bfd_byte *) everneed + iverneed->vn_next));
8923 }
25ff461f 8924 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8925
8926 free (contents);
8927 contents = NULL;
8928 }
252b5132
RH
8929
8930 if (elf_dynverdef (abfd) != 0)
8931 {
8932 Elf_Internal_Shdr *hdr;
8933 Elf_External_Verdef *everdef;
8934 Elf_Internal_Verdef *iverdef;
f631889e
UD
8935 Elf_Internal_Verdef *iverdefarr;
8936 Elf_Internal_Verdef iverdefmem;
252b5132 8937 unsigned int i;
062e2358 8938 unsigned int maxidx;
d0fb9a8d 8939 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8940
8941 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8942
601a03ba
AM
8943 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8944 {
8945 error_return_bad_verdef:
4eca0228 8946 _bfd_error_handler
871b3ab2 8947 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8948 bfd_set_error (bfd_error_bad_value);
8949 error_return_verdef:
8950 elf_tdata (abfd)->verdef = NULL;
8951 elf_tdata (abfd)->cverdefs = 0;
8952 goto error_return;
8953 }
8954
2bb3687b 8955 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
601a03ba 8956 goto error_return_verdef;
2bb3687b
AM
8957 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8958 if (contents == NULL)
601a03ba 8959 goto error_return_verdef;
d0fb9a8d
JJ
8960
8961 BFD_ASSERT (sizeof (Elf_External_Verdef)
8962 >= sizeof (Elf_External_Verdaux));
8963 contents_end_def = contents + hdr->sh_size
8964 - sizeof (Elf_External_Verdef);
8965 contents_end_aux = contents + hdr->sh_size
8966 - sizeof (Elf_External_Verdaux);
8967
f631889e
UD
8968 /* We know the number of entries in the section but not the maximum
8969 index. Therefore we have to run through all entries and find
8970 the maximum. */
252b5132 8971 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8972 maxidx = 0;
8973 for (i = 0; i < hdr->sh_info; ++i)
8974 {
8975 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8976
601a03ba
AM
8977 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8978 goto error_return_bad_verdef;
062e2358
AM
8979 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8980 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8981
25ff461f
AM
8982 if (iverdefmem.vd_next == 0)
8983 break;
8984
d0fb9a8d
JJ
8985 if (iverdefmem.vd_next
8986 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8987 goto error_return_bad_verdef;
d0fb9a8d 8988
f631889e
UD
8989 everdef = ((Elf_External_Verdef *)
8990 ((bfd_byte *) everdef + iverdefmem.vd_next));
8991 }
8992
fc0e6df6
PB
8993 if (default_imported_symver)
8994 {
8995 if (freeidx > maxidx)
8996 maxidx = ++freeidx;
8997 else
8998 freeidx = ++maxidx;
8999 }
1f4361a7
AM
9000 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9001 {
9002 bfd_set_error (bfd_error_file_too_big);
9003 goto error_return_verdef;
9004 }
9005 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e 9006 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 9007 goto error_return_verdef;
f631889e
UD
9008
9009 elf_tdata (abfd)->cverdefs = maxidx;
9010
9011 everdef = (Elf_External_Verdef *) contents;
9012 iverdefarr = elf_tdata (abfd)->verdef;
9013 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
9014 {
9015 Elf_External_Verdaux *everdaux;
9016 Elf_Internal_Verdaux *iverdaux;
9017 unsigned int j;
9018
f631889e
UD
9019 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9020
d0fb9a8d 9021 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 9022 goto error_return_bad_verdef;
d0fb9a8d 9023
f631889e 9024 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 9025 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
9026
9027 iverdef->vd_bfd = abfd;
9028
d0fb9a8d
JJ
9029 if (iverdef->vd_cnt == 0)
9030 iverdef->vd_auxptr = NULL;
9031 else
9032 {
1f4361a7
AM
9033 if (_bfd_mul_overflow (iverdef->vd_cnt,
9034 sizeof (Elf_Internal_Verdaux), &amt))
9035 {
9036 bfd_set_error (bfd_error_file_too_big);
9037 goto error_return_verdef;
9038 }
a50b1753 9039 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
1f4361a7 9040 bfd_alloc (abfd, amt);
d0fb9a8d
JJ
9041 if (iverdef->vd_auxptr == NULL)
9042 goto error_return_verdef;
9043 }
9044
9045 if (iverdef->vd_aux
9046 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 9047 goto error_return_bad_verdef;
252b5132
RH
9048
9049 everdaux = ((Elf_External_Verdaux *)
9050 ((bfd_byte *) everdef + iverdef->vd_aux));
9051 iverdaux = iverdef->vd_auxptr;
9052 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9053 {
9054 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9055
9056 iverdaux->vda_nodename =
9057 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9058 iverdaux->vda_name);
9059 if (iverdaux->vda_nodename == NULL)
601a03ba 9060 goto error_return_bad_verdef;
252b5132 9061
25ff461f
AM
9062 iverdaux->vda_nextptr = NULL;
9063 if (iverdaux->vda_next == 0)
9064 {
9065 iverdef->vd_cnt = j + 1;
9066 break;
9067 }
252b5132
RH
9068 if (j + 1 < iverdef->vd_cnt)
9069 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 9070
d0fb9a8d
JJ
9071 if (iverdaux->vda_next
9072 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 9073 goto error_return_bad_verdef;
d0fb9a8d 9074
252b5132
RH
9075 everdaux = ((Elf_External_Verdaux *)
9076 ((bfd_byte *) everdaux + iverdaux->vda_next));
9077 }
9078
595bce75 9079 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
9080 if (iverdef->vd_cnt)
9081 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 9082
25ff461f
AM
9083 iverdef->vd_nextdef = NULL;
9084 if (iverdef->vd_next == 0)
9085 break;
d0fb9a8d 9086 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 9087 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
9088
9089 everdef = ((Elf_External_Verdef *)
9090 ((bfd_byte *) everdef + iverdef->vd_next));
9091 }
9092
9093 free (contents);
9094 contents = NULL;
9095 }
fc0e6df6 9096 else if (default_imported_symver)
252b5132 9097 {
fc0e6df6
PB
9098 if (freeidx < 3)
9099 freeidx = 3;
9100 else
9101 freeidx++;
252b5132 9102
1f4361a7
AM
9103 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9104 {
9105 bfd_set_error (bfd_error_file_too_big);
9106 goto error_return;
9107 }
9108 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
fc0e6df6 9109 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
9110 goto error_return;
9111
fc0e6df6
PB
9112 elf_tdata (abfd)->cverdefs = freeidx;
9113 }
252b5132 9114
fc0e6df6
PB
9115 /* Create a default version based on the soname. */
9116 if (default_imported_symver)
9117 {
9118 Elf_Internal_Verdef *iverdef;
9119 Elf_Internal_Verdaux *iverdaux;
252b5132 9120
5bb3703f 9121 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 9122
fc0e6df6
PB
9123 iverdef->vd_version = VER_DEF_CURRENT;
9124 iverdef->vd_flags = 0;
9125 iverdef->vd_ndx = freeidx;
9126 iverdef->vd_cnt = 1;
252b5132 9127
fc0e6df6 9128 iverdef->vd_bfd = abfd;
252b5132 9129
fc0e6df6
PB
9130 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9131 if (iverdef->vd_nodename == NULL)
d0fb9a8d 9132 goto error_return_verdef;
fc0e6df6 9133 iverdef->vd_nextdef = NULL;
601a03ba
AM
9134 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9135 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
9136 if (iverdef->vd_auxptr == NULL)
9137 goto error_return_verdef;
252b5132 9138
fc0e6df6
PB
9139 iverdaux = iverdef->vd_auxptr;
9140 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
9141 }
9142
0a1b45a2 9143 return true;
252b5132
RH
9144
9145 error_return:
c9594989 9146 free (contents);
0a1b45a2 9147 return false;
252b5132
RH
9148}
9149\f
9150asymbol *
217aa764 9151_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
9152{
9153 elf_symbol_type *newsym;
9154
7a6e0d89 9155 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
9156 if (!newsym)
9157 return NULL;
201159ec
NC
9158 newsym->symbol.the_bfd = abfd;
9159 return &newsym->symbol;
252b5132
RH
9160}
9161
9162void
217aa764
AM
9163_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9164 asymbol *symbol,
9165 symbol_info *ret)
252b5132
RH
9166{
9167 bfd_symbol_info (symbol, ret);
9168}
9169
9170/* Return whether a symbol name implies a local symbol. Most targets
9171 use this function for the is_local_label_name entry point, but some
9172 override it. */
9173
0a1b45a2 9174bool
217aa764
AM
9175_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9176 const char *name)
252b5132
RH
9177{
9178 /* Normal local symbols start with ``.L''. */
9179 if (name[0] == '.' && name[1] == 'L')
0a1b45a2 9180 return true;
252b5132
RH
9181
9182 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9183 DWARF debugging symbols starting with ``..''. */
9184 if (name[0] == '.' && name[1] == '.')
0a1b45a2 9185 return true;
252b5132
RH
9186
9187 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9188 emitting DWARF debugging output. I suspect this is actually a
9189 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9190 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9191 underscore to be emitted on some ELF targets). For ease of use,
9192 we treat such symbols as local. */
9193 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
0a1b45a2 9194 return true;
252b5132 9195
b1fa9dd6
NC
9196 /* Treat assembler generated fake symbols, dollar local labels and
9197 forward-backward labels (aka local labels) as locals.
9198 These labels have the form:
9199
07d6d2b8 9200 L0^A.* (fake symbols)
b1fa9dd6
NC
9201
9202 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9203
9204 Versions which start with .L will have already been matched above,
9205 so we only need to match the rest. */
9206 if (name[0] == 'L' && ISDIGIT (name[1]))
9207 {
0a1b45a2 9208 bool ret = false;
b1fa9dd6
NC
9209 const char * p;
9210 char c;
9211
9212 for (p = name + 2; (c = *p); p++)
9213 {
9214 if (c == 1 || c == 2)
9215 {
9216 if (c == 1 && p == name + 2)
9217 /* A fake symbol. */
0a1b45a2 9218 return true;
b1fa9dd6
NC
9219
9220 /* FIXME: We are being paranoid here and treating symbols like
9221 L0^Bfoo as if there were non-local, on the grounds that the
9222 assembler will never generate them. But can any symbol
9223 containing an ASCII value in the range 1-31 ever be anything
9224 other than some kind of local ? */
0a1b45a2 9225 ret = true;
b1fa9dd6
NC
9226 }
9227
9228 if (! ISDIGIT (c))
9229 {
0a1b45a2 9230 ret = false;
b1fa9dd6
NC
9231 break;
9232 }
9233 }
9234 return ret;
9235 }
ffa54770 9236
0a1b45a2 9237 return false;
252b5132
RH
9238}
9239
9240alent *
217aa764
AM
9241_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9242 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
9243{
9244 abort ();
9245 return NULL;
9246}
9247
0a1b45a2 9248bool
217aa764
AM
9249_bfd_elf_set_arch_mach (bfd *abfd,
9250 enum bfd_architecture arch,
9251 unsigned long machine)
252b5132
RH
9252{
9253 /* If this isn't the right architecture for this backend, and this
9254 isn't the generic backend, fail. */
9255 if (arch != get_elf_backend_data (abfd)->arch
9256 && arch != bfd_arch_unknown
9257 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
0a1b45a2 9258 return false;
252b5132
RH
9259
9260 return bfd_default_set_arch_mach (abfd, arch, machine);
9261}
9262
d1fad7c6
NC
9263/* Find the nearest line to a particular section and offset,
9264 for error reporting. */
9265
0a1b45a2 9266bool
217aa764 9267_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 9268 asymbol **symbols,
fb167eb2 9269 asection *section,
217aa764
AM
9270 bfd_vma offset,
9271 const char **filename_ptr,
9272 const char **functionname_ptr,
fb167eb2
AM
9273 unsigned int *line_ptr,
9274 unsigned int *discriminator_ptr)
d1fad7c6 9275{
0a1b45a2 9276 bool found;
d1fad7c6 9277
fb167eb2 9278 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9279 filename_ptr, functionname_ptr,
fb167eb2 9280 line_ptr, discriminator_ptr,
9defd221 9281 dwarf_debug_sections,
e7679060 9282 &elf_tdata (abfd)->dwarf2_find_line_info))
0a1b45a2 9283 return true;
e7679060
AM
9284
9285 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9286 filename_ptr, functionname_ptr, line_ptr))
d1fad7c6
NC
9287 {
9288 if (!*functionname_ptr)
e00e8198
AM
9289 _bfd_elf_find_function (abfd, symbols, section, offset,
9290 *filename_ptr ? NULL : filename_ptr,
9291 functionname_ptr);
0a1b45a2 9292 return true;
d1fad7c6
NC
9293 }
9294
9295 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9296 &found, filename_ptr,
9297 functionname_ptr, line_ptr,
9298 &elf_tdata (abfd)->line_info))
0a1b45a2 9299 return false;
dc43ada5 9300 if (found && (*functionname_ptr || *line_ptr))
0a1b45a2 9301 return true;
d1fad7c6
NC
9302
9303 if (symbols == NULL)
0a1b45a2 9304 return false;
d1fad7c6 9305
e00e8198
AM
9306 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9307 filename_ptr, functionname_ptr))
0a1b45a2 9308 return false;
d1fad7c6 9309
252b5132 9310 *line_ptr = 0;
0a1b45a2 9311 return true;
252b5132
RH
9312}
9313
5420f73d
L
9314/* Find the line for a symbol. */
9315
0a1b45a2 9316bool
5420f73d
L
9317_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9318 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9319{
fb167eb2
AM
9320 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9321 filename_ptr, NULL, line_ptr, NULL,
9defd221 9322 dwarf_debug_sections,
fb167eb2 9323 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9324}
9325
4ab527b0
FF
9326/* After a call to bfd_find_nearest_line, successive calls to
9327 bfd_find_inliner_info can be used to get source information about
9328 each level of function inlining that terminated at the address
9329 passed to bfd_find_nearest_line. Currently this is only supported
9330 for DWARF2 with appropriate DWARF3 extensions. */
9331
0a1b45a2 9332bool
4ab527b0
FF
9333_bfd_elf_find_inliner_info (bfd *abfd,
9334 const char **filename_ptr,
9335 const char **functionname_ptr,
9336 unsigned int *line_ptr)
9337{
0a1b45a2 9338 bool found;
4ab527b0
FF
9339 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9340 functionname_ptr, line_ptr,
9341 & elf_tdata (abfd)->dwarf2_find_line_info);
9342 return found;
9343}
9344
252b5132 9345int
a6b96beb 9346_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9347{
8ded5a0f
AM
9348 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9349 int ret = bed->s->sizeof_ehdr;
252b5132 9350
0e1862bb 9351 if (!bfd_link_relocatable (info))
8ded5a0f 9352 {
12bd6957 9353 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9354
62d7a5f6
AM
9355 if (phdr_size == (bfd_size_type) -1)
9356 {
9357 struct elf_segment_map *m;
9358
9359 phdr_size = 0;
12bd6957 9360 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9361 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9362
62d7a5f6
AM
9363 if (phdr_size == 0)
9364 phdr_size = get_program_header_size (abfd, info);
9365 }
8ded5a0f 9366
12bd6957 9367 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9368 ret += phdr_size;
9369 }
9370
252b5132
RH
9371 return ret;
9372}
9373
0a1b45a2 9374bool
217aa764
AM
9375_bfd_elf_set_section_contents (bfd *abfd,
9376 sec_ptr section,
0f867abe 9377 const void *location,
217aa764
AM
9378 file_ptr offset,
9379 bfd_size_type count)
252b5132
RH
9380{
9381 Elf_Internal_Shdr *hdr;
1b6aeedb 9382 file_ptr pos;
252b5132
RH
9383
9384 if (! abfd->output_has_begun
217aa764 9385 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
0a1b45a2 9386 return false;
252b5132 9387
0ce398f1 9388 if (!count)
0a1b45a2 9389 return true;
0ce398f1 9390
252b5132 9391 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9392 if (hdr->sh_offset == (file_ptr) -1)
9393 {
a0dcf297
NC
9394 unsigned char *contents;
9395
1ff6de03
NA
9396 if (bfd_section_is_ctf (section))
9397 /* Nothing to do with this section: the contents are generated
9398 later. */
0a1b45a2 9399 return true;
1ff6de03 9400
a0dcf297
NC
9401 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9402 {
9403 _bfd_error_handler
9404 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9405 abfd, section);
9406 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9407 return false;
a0dcf297
NC
9408 }
9409
9410 if ((offset + count) > hdr->sh_size)
9411 {
9412 _bfd_error_handler
9413 (_("%pB:%pA: error: attempting to write over the end of the section"),
9414 abfd, section);
9415
9416 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9417 return false;
a0dcf297
NC
9418 }
9419
9420 contents = hdr->contents;
9421 if (contents == NULL)
9422 {
9423 _bfd_error_handler
9424 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9425 abfd, section);
9426
9427 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9428 return false;
a0dcf297
NC
9429 }
9430
0ce398f1 9431 memcpy (contents + offset, location, count);
0a1b45a2 9432 return true;
0ce398f1 9433 }
a0dcf297 9434
dc810e39
AM
9435 pos = hdr->sh_offset + offset;
9436 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9437 || bfd_bwrite (location, count, abfd) != count)
0a1b45a2 9438 return false;
252b5132 9439
0a1b45a2 9440 return true;
252b5132
RH
9441}
9442
0a1b45a2 9443bool
217aa764
AM
9444_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9445 arelent *cache_ptr ATTRIBUTE_UNUSED,
9446 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9447{
9448 abort ();
0a1b45a2 9449 return false;
252b5132
RH
9450}
9451
252b5132
RH
9452/* Try to convert a non-ELF reloc into an ELF one. */
9453
0a1b45a2 9454bool
217aa764 9455_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9456{
c044fabd 9457 /* Check whether we really have an ELF howto. */
252b5132
RH
9458
9459 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9460 {
9461 bfd_reloc_code_real_type code;
9462 reloc_howto_type *howto;
9463
9464 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9465 equivalent ELF reloc. */
252b5132
RH
9466
9467 if (areloc->howto->pc_relative)
9468 {
9469 switch (areloc->howto->bitsize)
9470 {
9471 case 8:
9472 code = BFD_RELOC_8_PCREL;
9473 break;
9474 case 12:
9475 code = BFD_RELOC_12_PCREL;
9476 break;
9477 case 16:
9478 code = BFD_RELOC_16_PCREL;
9479 break;
9480 case 24:
9481 code = BFD_RELOC_24_PCREL;
9482 break;
9483 case 32:
9484 code = BFD_RELOC_32_PCREL;
9485 break;
9486 case 64:
9487 code = BFD_RELOC_64_PCREL;
9488 break;
9489 default:
9490 goto fail;
9491 }
9492
9493 howto = bfd_reloc_type_lookup (abfd, code);
9494
94698d01 9495 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
252b5132
RH
9496 {
9497 if (howto->pcrel_offset)
9498 areloc->addend += areloc->address;
9499 else
9500 areloc->addend -= areloc->address; /* addend is unsigned!! */
9501 }
9502 }
9503 else
9504 {
9505 switch (areloc->howto->bitsize)
9506 {
9507 case 8:
9508 code = BFD_RELOC_8;
9509 break;
9510 case 14:
9511 code = BFD_RELOC_14;
9512 break;
9513 case 16:
9514 code = BFD_RELOC_16;
9515 break;
9516 case 26:
9517 code = BFD_RELOC_26;
9518 break;
9519 case 32:
9520 code = BFD_RELOC_32;
9521 break;
9522 case 64:
9523 code = BFD_RELOC_64;
9524 break;
9525 default:
9526 goto fail;
9527 }
9528
9529 howto = bfd_reloc_type_lookup (abfd, code);
9530 }
9531
9532 if (howto)
9533 areloc->howto = howto;
9534 else
9535 goto fail;
9536 }
9537
0a1b45a2 9538 return true;
252b5132
RH
9539
9540 fail:
0aa13fee
AM
9541 /* xgettext:c-format */
9542 _bfd_error_handler (_("%pB: %s unsupported"),
9543 abfd, areloc->howto->name);
9aea1e31 9544 bfd_set_error (bfd_error_sorry);
0a1b45a2 9545 return false;
252b5132
RH
9546}
9547
0a1b45a2 9548bool
217aa764 9549_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9550{
d9071b0c 9551 struct elf_obj_tdata *tdata = elf_tdata (abfd);
0ed18fa1
AM
9552 if (tdata != NULL
9553 && (bfd_get_format (abfd) == bfd_object
9554 || bfd_get_format (abfd) == bfd_core))
252b5132 9555 {
c0355132 9556 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9557 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9558 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9559 }
9560
9561 return _bfd_generic_close_and_cleanup (abfd);
9562}
9563
9564/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9565 in the relocation's offset. Thus we cannot allow any sort of sanity
9566 range-checking to interfere. There is nothing else to do in processing
9567 this reloc. */
9568
9569bfd_reloc_status_type
217aa764
AM
9570_bfd_elf_rel_vtable_reloc_fn
9571 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9572 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9573 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9574 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9575{
9576 return bfd_reloc_ok;
9577}
252b5132
RH
9578\f
9579/* Elf core file support. Much of this only works on native
9580 toolchains, since we rely on knowing the
9581 machine-dependent procfs structure in order to pick
c044fabd 9582 out details about the corefile. */
252b5132
RH
9583
9584#ifdef HAVE_SYS_PROCFS_H
9585# include <sys/procfs.h>
9586#endif
9587
261b8d08
PA
9588/* Return a PID that identifies a "thread" for threaded cores, or the
9589 PID of the main process for non-threaded cores. */
252b5132
RH
9590
9591static int
217aa764 9592elfcore_make_pid (bfd *abfd)
252b5132 9593{
261b8d08
PA
9594 int pid;
9595
228e534f 9596 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9597 if (pid == 0)
228e534f 9598 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9599
9600 return pid;
252b5132
RH
9601}
9602
252b5132
RH
9603/* If there isn't a section called NAME, make one, using
9604 data from SECT. Note, this function will generate a
9605 reference to NAME, so you shouldn't deallocate or
c044fabd 9606 overwrite it. */
252b5132 9607
0a1b45a2 9608static bool
217aa764 9609elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9610{
c044fabd 9611 asection *sect2;
252b5132
RH
9612
9613 if (bfd_get_section_by_name (abfd, name) != NULL)
0a1b45a2 9614 return true;
252b5132 9615
117ed4f8 9616 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9617 if (sect2 == NULL)
0a1b45a2 9618 return false;
252b5132 9619
eea6121a 9620 sect2->size = sect->size;
252b5132 9621 sect2->filepos = sect->filepos;
252b5132 9622 sect2->alignment_power = sect->alignment_power;
0a1b45a2 9623 return true;
252b5132
RH
9624}
9625
bb0082d6
AM
9626/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9627 actually creates up to two pseudosections:
9628 - For the single-threaded case, a section named NAME, unless
9629 such a section already exists.
9630 - For the multi-threaded case, a section named "NAME/PID", where
9631 PID is elfcore_make_pid (abfd).
24d3e51b 9632 Both pseudosections have identical contents. */
0a1b45a2 9633bool
217aa764
AM
9634_bfd_elfcore_make_pseudosection (bfd *abfd,
9635 char *name,
9636 size_t size,
9637 ufile_ptr filepos)
bb0082d6
AM
9638{
9639 char buf[100];
9640 char *threaded_name;
d4c88bbb 9641 size_t len;
bb0082d6
AM
9642 asection *sect;
9643
9644 /* Build the section name. */
9645
9646 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9647 len = strlen (buf) + 1;
a50b1753 9648 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9649 if (threaded_name == NULL)
0a1b45a2 9650 return false;
d4c88bbb 9651 memcpy (threaded_name, buf, len);
bb0082d6 9652
117ed4f8
AM
9653 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9654 SEC_HAS_CONTENTS);
bb0082d6 9655 if (sect == NULL)
0a1b45a2 9656 return false;
eea6121a 9657 sect->size = size;
bb0082d6 9658 sect->filepos = filepos;
bb0082d6
AM
9659 sect->alignment_power = 2;
9660
936e320b 9661 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9662}
9663
0a1b45a2 9664static bool
58e07198
CZ
9665elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9666 size_t offs)
9667{
9668 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9669 SEC_HAS_CONTENTS);
9670
9671 if (sect == NULL)
0a1b45a2 9672 return false;
58e07198
CZ
9673
9674 sect->size = note->descsz - offs;
9675 sect->filepos = note->descpos + offs;
9676 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9677
0a1b45a2 9678 return true;
58e07198
CZ
9679}
9680
252b5132 9681/* prstatus_t exists on:
4a938328 9682 solaris 2.5+
252b5132
RH
9683 linux 2.[01] + glibc
9684 unixware 4.2
9685*/
9686
9687#if defined (HAVE_PRSTATUS_T)
a7b97311 9688
0a1b45a2 9689static bool
217aa764 9690elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9691{
eea6121a 9692 size_t size;
7ee38065 9693 int offset;
252b5132 9694
4a938328
MS
9695 if (note->descsz == sizeof (prstatus_t))
9696 {
9697 prstatus_t prstat;
252b5132 9698
eea6121a 9699 size = sizeof (prstat.pr_reg);
7ee38065 9700 offset = offsetof (prstatus_t, pr_reg);
4a938328 9701 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9702
fa49d224
NC
9703 /* Do not overwrite the core signal if it
9704 has already been set by another thread. */
228e534f
AM
9705 if (elf_tdata (abfd)->core->signal == 0)
9706 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9707 if (elf_tdata (abfd)->core->pid == 0)
9708 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9709
4a938328
MS
9710 /* pr_who exists on:
9711 solaris 2.5+
9712 unixware 4.2
9713 pr_who doesn't exist on:
9714 linux 2.[01]
9715 */
252b5132 9716#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9717 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9718#else
228e534f 9719 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9720#endif
4a938328 9721 }
7ee38065 9722#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9723 else if (note->descsz == sizeof (prstatus32_t))
9724 {
9725 /* 64-bit host, 32-bit corefile */
9726 prstatus32_t prstat;
9727
eea6121a 9728 size = sizeof (prstat.pr_reg);
7ee38065 9729 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9730 memcpy (&prstat, note->descdata, sizeof (prstat));
9731
fa49d224
NC
9732 /* Do not overwrite the core signal if it
9733 has already been set by another thread. */
228e534f
AM
9734 if (elf_tdata (abfd)->core->signal == 0)
9735 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9736 if (elf_tdata (abfd)->core->pid == 0)
9737 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9738
9739 /* pr_who exists on:
9740 solaris 2.5+
9741 unixware 4.2
9742 pr_who doesn't exist on:
9743 linux 2.[01]
9744 */
7ee38065 9745#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9746 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9747#else
228e534f 9748 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9749#endif
9750 }
7ee38065 9751#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9752 else
9753 {
9754 /* Fail - we don't know how to handle any other
9755 note size (ie. data object type). */
0a1b45a2 9756 return true;
4a938328 9757 }
252b5132 9758
bb0082d6 9759 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9760 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9761 size, note->descpos + offset);
252b5132
RH
9762}
9763#endif /* defined (HAVE_PRSTATUS_T) */
9764
bb0082d6 9765/* Create a pseudosection containing the exact contents of NOTE. */
0a1b45a2 9766static bool
217aa764
AM
9767elfcore_make_note_pseudosection (bfd *abfd,
9768 char *name,
9769 Elf_Internal_Note *note)
252b5132 9770{
936e320b
AM
9771 return _bfd_elfcore_make_pseudosection (abfd, name,
9772 note->descsz, note->descpos);
252b5132
RH
9773}
9774
ff08c6bb
JB
9775/* There isn't a consistent prfpregset_t across platforms,
9776 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9777 data structure apart. */
9778
0a1b45a2 9779static bool
217aa764 9780elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9781{
9782 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9783}
9784
ff08c6bb 9785/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9786 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9787 literally. */
c044fabd 9788
0a1b45a2 9789static bool
217aa764 9790elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9791{
9792 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9793}
9794
4339cae0
L
9795/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9796 with a note type of NT_X86_XSTATE. Just include the whole note's
9797 contents literally. */
9798
0a1b45a2 9799static bool
4339cae0
L
9800elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9801{
9802 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9803}
9804
0a1b45a2 9805static bool
97753bd5
AM
9806elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9807{
9808 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9809}
9810
0a1b45a2 9811static bool
89eeb0bc
LM
9812elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9813{
9814 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9815}
97753bd5 9816
0a1b45a2 9817static bool
cb2366c1
EBM
9818elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9819{
9820 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9821}
9822
0a1b45a2 9823static bool
cb2366c1
EBM
9824elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9825{
9826 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9827}
9828
0a1b45a2 9829static bool
cb2366c1
EBM
9830elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9831{
9832 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9833}
9834
0a1b45a2 9835static bool
cb2366c1
EBM
9836elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9837{
9838 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9839}
9840
0a1b45a2 9841static bool
cb2366c1
EBM
9842elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9843{
9844 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9845}
9846
0a1b45a2 9847static bool
cb2366c1
EBM
9848elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9849{
9850 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9851}
9852
0a1b45a2 9853static bool
cb2366c1
EBM
9854elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9855{
9856 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9857}
9858
0a1b45a2 9859static bool
cb2366c1
EBM
9860elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9861{
9862 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9863}
9864
0a1b45a2 9865static bool
cb2366c1
EBM
9866elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9867{
9868 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9869}
9870
0a1b45a2 9871static bool
cb2366c1
EBM
9872elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9873{
9874 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9875}
9876
0a1b45a2 9877static bool
cb2366c1
EBM
9878elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9879{
9880 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9881}
9882
0a1b45a2 9883static bool
cb2366c1
EBM
9884elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9885{
9886 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9887}
9888
0a1b45a2 9889static bool
cb2366c1
EBM
9890elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9891{
9892 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9893}
9894
0a1b45a2 9895static bool
0675e188
UW
9896elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9897{
9898 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9899}
9900
0a1b45a2 9901static bool
d7eeb400
MS
9902elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9903{
9904 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9905}
9906
0a1b45a2 9907static bool
d7eeb400
MS
9908elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9909{
9910 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9911}
9912
0a1b45a2 9913static bool
d7eeb400
MS
9914elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9915{
9916 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9917}
9918
0a1b45a2 9919static bool
d7eeb400
MS
9920elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9921{
9922 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9923}
9924
0a1b45a2 9925static bool
d7eeb400
MS
9926elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9927{
9928 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9929}
9930
0a1b45a2 9931static bool
355b81d9
UW
9932elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9933{
9934 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9935}
9936
0a1b45a2 9937static bool
355b81d9
UW
9938elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9939{
9940 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9941}
9942
0a1b45a2 9943static bool
abb3f6cc
NC
9944elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9945{
9946 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9947}
9948
0a1b45a2 9949static bool
4ef9f41a
AA
9950elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9951{
9952 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9953}
9954
0a1b45a2 9955static bool
4ef9f41a
AA
9956elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9957{
9958 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9959}
9960
0a1b45a2 9961static bool
88ab90e8
AA
9962elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9963{
9964 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9965}
9966
0a1b45a2 9967static bool
88ab90e8
AA
9968elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9969{
9970 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9971}
9972
0a1b45a2 9973static bool
faa9a424
UW
9974elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9975{
9976 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9977}
9978
0a1b45a2 9979static bool
652451f8
YZ
9980elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9981{
9982 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9983}
9984
0a1b45a2 9985static bool
652451f8
YZ
9986elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9987{
9988 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9989}
9990
0a1b45a2 9991static bool
652451f8
YZ
9992elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9993{
9994 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9995}
9996
0a1b45a2 9997static bool
ad1cc4e4
AH
9998elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9999{
10000 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10001}
10002
0a1b45a2 10003static bool
e6c3b5bf
AH
10004elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10005{
10006 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10007}
10008
f0bbe8ba
LM
10009static bool
10010elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10011{
10012 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10013 note);
10014}
10015
0a1b45a2 10016static bool
27456742
AK
10017elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10018{
10019 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10020}
10021
db6092f3
AB
10022/* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10023 successful otherwise, return FALSE. */
10024
0a1b45a2 10025static bool
db6092f3
AB
10026elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10027{
10028 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10029}
10030
b63a5e38
AB
10031/* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10032 successful otherwise, return FALSE. */
10033
0a1b45a2 10034static bool
b63a5e38
AB
10035elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10036{
10037 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10038}
10039
e214f8db 10040static bool
10041elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10042{
10043 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10044}
10045
10046static bool
10047elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10048{
10049 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10050}
10051
10052static bool
10053elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10054{
10055 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10056}
10057
10058static bool
10059elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10060{
10061 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10062}
10063
252b5132 10064#if defined (HAVE_PRPSINFO_T)
4a938328 10065typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 10066#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
10067typedef prpsinfo32_t elfcore_psinfo32_t;
10068#endif
252b5132
RH
10069#endif
10070
10071#if defined (HAVE_PSINFO_T)
4a938328 10072typedef psinfo_t elfcore_psinfo_t;
7ee38065 10073#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
10074typedef psinfo32_t elfcore_psinfo32_t;
10075#endif
252b5132
RH
10076#endif
10077
252b5132
RH
10078/* return a malloc'ed copy of a string at START which is at
10079 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 10080 the copy will always have a terminating '\0'. */
252b5132 10081
936e320b 10082char *
217aa764 10083_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 10084{
dc810e39 10085 char *dups;
a50b1753 10086 char *end = (char *) memchr (start, '\0', max);
dc810e39 10087 size_t len;
252b5132
RH
10088
10089 if (end == NULL)
10090 len = max;
10091 else
10092 len = end - start;
10093
a50b1753 10094 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 10095 if (dups == NULL)
252b5132
RH
10096 return NULL;
10097
dc810e39
AM
10098 memcpy (dups, start, len);
10099 dups[len] = '\0';
252b5132 10100
dc810e39 10101 return dups;
252b5132
RH
10102}
10103
bb0082d6 10104#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
0a1b45a2 10105static bool
217aa764 10106elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 10107{
4a938328
MS
10108 if (note->descsz == sizeof (elfcore_psinfo_t))
10109 {
10110 elfcore_psinfo_t psinfo;
252b5132 10111
7ee38065 10112 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10113
335e41d4 10114#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 10115 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10116#endif
228e534f 10117 elf_tdata (abfd)->core->program
936e320b
AM
10118 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10119 sizeof (psinfo.pr_fname));
252b5132 10120
228e534f 10121 elf_tdata (abfd)->core->command
936e320b
AM
10122 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10123 sizeof (psinfo.pr_psargs));
4a938328 10124 }
7ee38065 10125#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
10126 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10127 {
10128 /* 64-bit host, 32-bit corefile */
10129 elfcore_psinfo32_t psinfo;
10130
7ee38065 10131 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 10132
335e41d4 10133#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 10134 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 10135#endif
228e534f 10136 elf_tdata (abfd)->core->program
936e320b
AM
10137 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10138 sizeof (psinfo.pr_fname));
4a938328 10139
228e534f 10140 elf_tdata (abfd)->core->command
936e320b
AM
10141 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10142 sizeof (psinfo.pr_psargs));
4a938328
MS
10143 }
10144#endif
10145
10146 else
10147 {
10148 /* Fail - we don't know how to handle any other
10149 note size (ie. data object type). */
0a1b45a2 10150 return true;
4a938328 10151 }
252b5132
RH
10152
10153 /* Note that for some reason, a spurious space is tacked
10154 onto the end of the args in some (at least one anyway)
c044fabd 10155 implementations, so strip it off if it exists. */
252b5132
RH
10156
10157 {
228e534f 10158 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
10159 int n = strlen (command);
10160
10161 if (0 < n && command[n - 1] == ' ')
10162 command[n - 1] = '\0';
10163 }
10164
0a1b45a2 10165 return true;
252b5132
RH
10166}
10167#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10168
252b5132 10169#if defined (HAVE_PSTATUS_T)
0a1b45a2 10170static bool
217aa764 10171elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 10172{
f572a39d
AM
10173 if (note->descsz == sizeof (pstatus_t)
10174#if defined (HAVE_PXSTATUS_T)
10175 || note->descsz == sizeof (pxstatus_t)
10176#endif
10177 )
4a938328
MS
10178 {
10179 pstatus_t pstat;
252b5132 10180
4a938328 10181 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10182
228e534f 10183 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 10184 }
7ee38065 10185#if defined (HAVE_PSTATUS32_T)
4a938328
MS
10186 else if (note->descsz == sizeof (pstatus32_t))
10187 {
10188 /* 64-bit host, 32-bit corefile */
10189 pstatus32_t pstat;
252b5132 10190
4a938328 10191 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 10192
228e534f 10193 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
10194 }
10195#endif
252b5132
RH
10196 /* Could grab some more details from the "representative"
10197 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 10198 NT_LWPSTATUS note, presumably. */
252b5132 10199
0a1b45a2 10200 return true;
252b5132
RH
10201}
10202#endif /* defined (HAVE_PSTATUS_T) */
10203
252b5132 10204#if defined (HAVE_LWPSTATUS_T)
0a1b45a2 10205static bool
217aa764 10206elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
10207{
10208 lwpstatus_t lwpstat;
10209 char buf[100];
c044fabd 10210 char *name;
d4c88bbb 10211 size_t len;
c044fabd 10212 asection *sect;
252b5132 10213
f572a39d
AM
10214 if (note->descsz != sizeof (lwpstat)
10215#if defined (HAVE_LWPXSTATUS_T)
10216 && note->descsz != sizeof (lwpxstatus_t)
10217#endif
10218 )
0a1b45a2 10219 return true;
252b5132
RH
10220
10221 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10222
228e534f 10223 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
10224 /* Do not overwrite the core signal if it has already been set by
10225 another thread. */
228e534f
AM
10226 if (elf_tdata (abfd)->core->signal == 0)
10227 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 10228
c044fabd 10229 /* Make a ".reg/999" section. */
252b5132
RH
10230
10231 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 10232 len = strlen (buf) + 1;
217aa764 10233 name = bfd_alloc (abfd, len);
252b5132 10234 if (name == NULL)
0a1b45a2 10235 return false;
d4c88bbb 10236 memcpy (name, buf, len);
252b5132 10237
117ed4f8 10238 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10239 if (sect == NULL)
0a1b45a2 10240 return false;
252b5132
RH
10241
10242#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10243 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
10244 sect->filepos = note->descpos
10245 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10246#endif
10247
10248#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 10249 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
10250 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10251#endif
10252
252b5132
RH
10253 sect->alignment_power = 2;
10254
10255 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10256 return false;
252b5132
RH
10257
10258 /* Make a ".reg2/999" section */
10259
10260 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 10261 len = strlen (buf) + 1;
217aa764 10262 name = bfd_alloc (abfd, len);
252b5132 10263 if (name == NULL)
0a1b45a2 10264 return false;
d4c88bbb 10265 memcpy (name, buf, len);
252b5132 10266
117ed4f8 10267 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 10268 if (sect == NULL)
0a1b45a2 10269 return false;
252b5132
RH
10270
10271#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 10272 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
10273 sect->filepos = note->descpos
10274 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10275#endif
10276
10277#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 10278 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
10279 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10280#endif
10281
252b5132
RH
10282 sect->alignment_power = 2;
10283
936e320b 10284 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
10285}
10286#endif /* defined (HAVE_LWPSTATUS_T) */
10287
8fbac78b
JT
10288/* These constants, and the structure offsets used below, are defined by
10289 Cygwin's core_dump.h */
10290#define NOTE_INFO_PROCESS 1
10291#define NOTE_INFO_THREAD 2
10292#define NOTE_INFO_MODULE 3
d61f3d03 10293#define NOTE_INFO_MODULE64 4
8fbac78b 10294
0a1b45a2 10295static bool
217aa764 10296elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
10297{
10298 char buf[30];
c044fabd 10299 char *name;
d4c88bbb 10300 size_t len;
3cdad084 10301 unsigned int name_size;
c044fabd 10302 asection *sect;
2fef9373 10303 unsigned int type;
4a6636fb
PA
10304 int is_active_thread;
10305 bfd_vma base_addr;
16e9c715 10306
04ec0fa2 10307 if (note->descsz < 4)
0a1b45a2 10308 return true;
16e9c715 10309
08dedd66 10310 if (! startswith (note->namedata, "win32"))
0a1b45a2 10311 return true;
4a6636fb
PA
10312
10313 type = bfd_get_32 (abfd, note->descdata);
c044fabd 10314
7e0d77ef
NC
10315 struct
10316 {
404ec933
JT
10317 const char *type_name;
10318 unsigned long min_size;
10319 } size_check[] =
10320 {
10321 { "NOTE_INFO_PROCESS", 12 },
10322 { "NOTE_INFO_THREAD", 12 },
10323 { "NOTE_INFO_MODULE", 12 },
10324 { "NOTE_INFO_MODULE64", 16 },
10325 };
10326
7e0d77ef 10327 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
0a1b45a2 10328 return true;
404ec933
JT
10329
10330 if (note->descsz < size_check[type - 1].min_size)
10331 {
10332 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10333 abfd, size_check[type - 1].type_name, note->descsz);
0a1b45a2 10334 return true;
404ec933
JT
10335 }
10336
4a6636fb 10337 switch (type)
16e9c715 10338 {
8fbac78b 10339 case NOTE_INFO_PROCESS:
228e534f 10340 /* FIXME: need to add ->core->command. */
ff2084b9 10341 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
ff2084b9 10342 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
c044fabd 10343 break;
16e9c715 10344
8fbac78b 10345 case NOTE_INFO_THREAD:
ff2084b9
JT
10346 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10347 structure. */
4a6636fb 10348 /* thread_info.tid */
ff2084b9 10349 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
c044fabd 10350
d4c88bbb 10351 len = strlen (buf) + 1;
a50b1753 10352 name = (char *) bfd_alloc (abfd, len);
16e9c715 10353 if (name == NULL)
0a1b45a2 10354 return false;
c044fabd 10355
d4c88bbb 10356 memcpy (name, buf, len);
16e9c715 10357
117ed4f8 10358 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 10359 if (sect == NULL)
0a1b45a2 10360 return false;
c044fabd 10361
4a6636fb 10362 /* sizeof (thread_info.thread_context) */
03c29a6f 10363 sect->size = note->descsz - 12;
4a6636fb
PA
10364 /* offsetof (thread_info.thread_context) */
10365 sect->filepos = note->descpos + 12;
16e9c715
NC
10366 sect->alignment_power = 2;
10367
4a6636fb
PA
10368 /* thread_info.is_active_thread */
10369 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10370
10371 if (is_active_thread)
16e9c715 10372 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
0a1b45a2 10373 return false;
16e9c715
NC
10374 break;
10375
8fbac78b 10376 case NOTE_INFO_MODULE:
d61f3d03 10377 case NOTE_INFO_MODULE64:
16e9c715 10378 /* Make a ".module/xxxxxxxx" section. */
d61f3d03
JT
10379 if (type == NOTE_INFO_MODULE)
10380 {
d61f3d03
JT
10381 /* module_info.base_address */
10382 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10383 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10384 /* module_info.module_name_size */
10385 name_size = bfd_get_32 (abfd, note->descdata + 8);
10386 }
10387 else /* NOTE_INFO_MODULE64 */
10388 {
d61f3d03
JT
10389 /* module_info.base_address */
10390 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10391 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10392 /* module_info.module_name_size */
10393 name_size = bfd_get_32 (abfd, note->descdata + 12);
10394 }
c044fabd 10395
d4c88bbb 10396 len = strlen (buf) + 1;
a50b1753 10397 name = (char *) bfd_alloc (abfd, len);
16e9c715 10398 if (name == NULL)
0a1b45a2 10399 return false;
c044fabd 10400
d4c88bbb 10401 memcpy (name, buf, len);
252b5132 10402
117ed4f8 10403 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10404
16e9c715 10405 if (sect == NULL)
0a1b45a2 10406 return false;
c044fabd 10407
04ec0fa2 10408 if (note->descsz < 12 + name_size)
404ec933 10409 {
3cdad084 10410 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
404ec933 10411 abfd, note->descsz, name_size);
0a1b45a2 10412 return true;
404ec933 10413 }
04ec0fa2 10414
eea6121a 10415 sect->size = note->descsz;
16e9c715 10416 sect->filepos = note->descpos;
16e9c715
NC
10417 sect->alignment_power = 2;
10418 break;
10419
10420 default:
0a1b45a2 10421 return true;
16e9c715
NC
10422 }
10423
0a1b45a2 10424 return true;
16e9c715 10425}
252b5132 10426
0a1b45a2 10427static bool
217aa764 10428elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10429{
9c5bfbb7 10430 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10431
252b5132
RH
10432 switch (note->type)
10433 {
10434 default:
0a1b45a2 10435 return true;
252b5132 10436
252b5132 10437 case NT_PRSTATUS:
bb0082d6
AM
10438 if (bed->elf_backend_grok_prstatus)
10439 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
0a1b45a2 10440 return true;
bb0082d6 10441#if defined (HAVE_PRSTATUS_T)
252b5132 10442 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10443#else
0a1b45a2 10444 return true;
252b5132
RH
10445#endif
10446
10447#if defined (HAVE_PSTATUS_T)
10448 case NT_PSTATUS:
10449 return elfcore_grok_pstatus (abfd, note);
10450#endif
10451
10452#if defined (HAVE_LWPSTATUS_T)
10453 case NT_LWPSTATUS:
10454 return elfcore_grok_lwpstatus (abfd, note);
10455#endif
10456
10457 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10458 return elfcore_grok_prfpreg (abfd, note);
10459
c044fabd 10460 case NT_WIN32PSTATUS:
16e9c715 10461 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10462
c044fabd 10463 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10464 if (note->namesz == 6
10465 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10466 return elfcore_grok_prxfpreg (abfd, note);
10467 else
0a1b45a2 10468 return true;
ff08c6bb 10469
4339cae0
L
10470 case NT_X86_XSTATE: /* Linux XSAVE extension */
10471 if (note->namesz == 6
10472 && strcmp (note->namedata, "LINUX") == 0)
10473 return elfcore_grok_xstatereg (abfd, note);
10474 else
0a1b45a2 10475 return true;
4339cae0 10476
97753bd5
AM
10477 case NT_PPC_VMX:
10478 if (note->namesz == 6
10479 && strcmp (note->namedata, "LINUX") == 0)
10480 return elfcore_grok_ppc_vmx (abfd, note);
10481 else
0a1b45a2 10482 return true;
97753bd5 10483
89eeb0bc
LM
10484 case NT_PPC_VSX:
10485 if (note->namesz == 6
07d6d2b8
AM
10486 && strcmp (note->namedata, "LINUX") == 0)
10487 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10488 else
0a1b45a2 10489 return true;
89eeb0bc 10490
cb2366c1
EBM
10491 case NT_PPC_TAR:
10492 if (note->namesz == 6
4b24dd1a
AM
10493 && strcmp (note->namedata, "LINUX") == 0)
10494 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10495 else
0a1b45a2 10496 return true;
cb2366c1
EBM
10497
10498 case NT_PPC_PPR:
10499 if (note->namesz == 6
4b24dd1a
AM
10500 && strcmp (note->namedata, "LINUX") == 0)
10501 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10502 else
0a1b45a2 10503 return true;
cb2366c1
EBM
10504
10505 case NT_PPC_DSCR:
10506 if (note->namesz == 6
4b24dd1a
AM
10507 && strcmp (note->namedata, "LINUX") == 0)
10508 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10509 else
0a1b45a2 10510 return true;
cb2366c1
EBM
10511
10512 case NT_PPC_EBB:
10513 if (note->namesz == 6
4b24dd1a
AM
10514 && strcmp (note->namedata, "LINUX") == 0)
10515 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10516 else
0a1b45a2 10517 return true;
cb2366c1
EBM
10518
10519 case NT_PPC_PMU:
10520 if (note->namesz == 6
4b24dd1a
AM
10521 && strcmp (note->namedata, "LINUX") == 0)
10522 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10523 else
0a1b45a2 10524 return true;
cb2366c1
EBM
10525
10526 case NT_PPC_TM_CGPR:
10527 if (note->namesz == 6
4b24dd1a
AM
10528 && strcmp (note->namedata, "LINUX") == 0)
10529 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10530 else
0a1b45a2 10531 return true;
cb2366c1
EBM
10532
10533 case NT_PPC_TM_CFPR:
10534 if (note->namesz == 6
4b24dd1a
AM
10535 && strcmp (note->namedata, "LINUX") == 0)
10536 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10537 else
0a1b45a2 10538 return true;
cb2366c1
EBM
10539
10540 case NT_PPC_TM_CVMX:
10541 if (note->namesz == 6
4b24dd1a
AM
10542 && strcmp (note->namedata, "LINUX") == 0)
10543 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10544 else
0a1b45a2 10545 return true;
cb2366c1
EBM
10546
10547 case NT_PPC_TM_CVSX:
10548 if (note->namesz == 6
4b24dd1a
AM
10549 && strcmp (note->namedata, "LINUX") == 0)
10550 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10551 else
0a1b45a2 10552 return true;
cb2366c1
EBM
10553
10554 case NT_PPC_TM_SPR:
10555 if (note->namesz == 6
4b24dd1a
AM
10556 && strcmp (note->namedata, "LINUX") == 0)
10557 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10558 else
0a1b45a2 10559 return true;
cb2366c1
EBM
10560
10561 case NT_PPC_TM_CTAR:
10562 if (note->namesz == 6
4b24dd1a
AM
10563 && strcmp (note->namedata, "LINUX") == 0)
10564 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10565 else
0a1b45a2 10566 return true;
cb2366c1
EBM
10567
10568 case NT_PPC_TM_CPPR:
10569 if (note->namesz == 6
4b24dd1a
AM
10570 && strcmp (note->namedata, "LINUX") == 0)
10571 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10572 else
0a1b45a2 10573 return true;
cb2366c1
EBM
10574
10575 case NT_PPC_TM_CDSCR:
10576 if (note->namesz == 6
4b24dd1a
AM
10577 && strcmp (note->namedata, "LINUX") == 0)
10578 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10579 else
0a1b45a2 10580 return true;
cb2366c1 10581
0675e188
UW
10582 case NT_S390_HIGH_GPRS:
10583 if (note->namesz == 6
07d6d2b8
AM
10584 && strcmp (note->namedata, "LINUX") == 0)
10585 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10586 else
0a1b45a2 10587 return true;
0675e188 10588
d7eeb400
MS
10589 case NT_S390_TIMER:
10590 if (note->namesz == 6
07d6d2b8
AM
10591 && strcmp (note->namedata, "LINUX") == 0)
10592 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10593 else
0a1b45a2 10594 return true;
d7eeb400
MS
10595
10596 case NT_S390_TODCMP:
10597 if (note->namesz == 6
07d6d2b8
AM
10598 && strcmp (note->namedata, "LINUX") == 0)
10599 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10600 else
0a1b45a2 10601 return true;
d7eeb400
MS
10602
10603 case NT_S390_TODPREG:
10604 if (note->namesz == 6
07d6d2b8
AM
10605 && strcmp (note->namedata, "LINUX") == 0)
10606 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10607 else
0a1b45a2 10608 return true;
d7eeb400
MS
10609
10610 case NT_S390_CTRS:
10611 if (note->namesz == 6
07d6d2b8
AM
10612 && strcmp (note->namedata, "LINUX") == 0)
10613 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10614 else
0a1b45a2 10615 return true;
d7eeb400
MS
10616
10617 case NT_S390_PREFIX:
10618 if (note->namesz == 6
07d6d2b8
AM
10619 && strcmp (note->namedata, "LINUX") == 0)
10620 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10621 else
0a1b45a2 10622 return true;
d7eeb400 10623
355b81d9
UW
10624 case NT_S390_LAST_BREAK:
10625 if (note->namesz == 6
07d6d2b8
AM
10626 && strcmp (note->namedata, "LINUX") == 0)
10627 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10628 else
0a1b45a2 10629 return true;
355b81d9
UW
10630
10631 case NT_S390_SYSTEM_CALL:
10632 if (note->namesz == 6
07d6d2b8
AM
10633 && strcmp (note->namedata, "LINUX") == 0)
10634 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10635 else
0a1b45a2 10636 return true;
355b81d9 10637
abb3f6cc
NC
10638 case NT_S390_TDB:
10639 if (note->namesz == 6
07d6d2b8
AM
10640 && strcmp (note->namedata, "LINUX") == 0)
10641 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10642 else
0a1b45a2 10643 return true;
abb3f6cc 10644
4ef9f41a
AA
10645 case NT_S390_VXRS_LOW:
10646 if (note->namesz == 6
10647 && strcmp (note->namedata, "LINUX") == 0)
10648 return elfcore_grok_s390_vxrs_low (abfd, note);
10649 else
0a1b45a2 10650 return true;
4ef9f41a
AA
10651
10652 case NT_S390_VXRS_HIGH:
10653 if (note->namesz == 6
10654 && strcmp (note->namedata, "LINUX") == 0)
10655 return elfcore_grok_s390_vxrs_high (abfd, note);
10656 else
0a1b45a2 10657 return true;
4ef9f41a 10658
88ab90e8
AA
10659 case NT_S390_GS_CB:
10660 if (note->namesz == 6
10661 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10662 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8 10663 else
0a1b45a2 10664 return true;
88ab90e8
AA
10665
10666 case NT_S390_GS_BC:
10667 if (note->namesz == 6
10668 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10669 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8 10670 else
0a1b45a2 10671 return true;
88ab90e8 10672
27456742
AK
10673 case NT_ARC_V2:
10674 if (note->namesz == 6
10675 && strcmp (note->namedata, "LINUX") == 0)
10676 return elfcore_grok_arc_v2 (abfd, note);
10677 else
0a1b45a2 10678 return true;
27456742 10679
faa9a424
UW
10680 case NT_ARM_VFP:
10681 if (note->namesz == 6
10682 && strcmp (note->namedata, "LINUX") == 0)
10683 return elfcore_grok_arm_vfp (abfd, note);
10684 else
0a1b45a2 10685 return true;
faa9a424 10686
652451f8
YZ
10687 case NT_ARM_TLS:
10688 if (note->namesz == 6
10689 && strcmp (note->namedata, "LINUX") == 0)
10690 return elfcore_grok_aarch_tls (abfd, note);
10691 else
0a1b45a2 10692 return true;
652451f8
YZ
10693
10694 case NT_ARM_HW_BREAK:
10695 if (note->namesz == 6
10696 && strcmp (note->namedata, "LINUX") == 0)
10697 return elfcore_grok_aarch_hw_break (abfd, note);
10698 else
0a1b45a2 10699 return true;
652451f8
YZ
10700
10701 case NT_ARM_HW_WATCH:
10702 if (note->namesz == 6
10703 && strcmp (note->namedata, "LINUX") == 0)
10704 return elfcore_grok_aarch_hw_watch (abfd, note);
10705 else
0a1b45a2 10706 return true;
652451f8 10707
ad1cc4e4
AH
10708 case NT_ARM_SVE:
10709 if (note->namesz == 6
10710 && strcmp (note->namedata, "LINUX") == 0)
10711 return elfcore_grok_aarch_sve (abfd, note);
10712 else
0a1b45a2 10713 return true;
ad1cc4e4 10714
e6c3b5bf
AH
10715 case NT_ARM_PAC_MASK:
10716 if (note->namesz == 6
10717 && strcmp (note->namedata, "LINUX") == 0)
10718 return elfcore_grok_aarch_pauth (abfd, note);
10719 else
0a1b45a2 10720 return true;
e6c3b5bf 10721
f0bbe8ba
LM
10722 case NT_ARM_TAGGED_ADDR_CTRL:
10723 if (note->namesz == 6
10724 && strcmp (note->namedata, "LINUX") == 0)
10725 return elfcore_grok_aarch_mte (abfd, note);
10726 else
10727 return true;
10728
b63a5e38
AB
10729 case NT_GDB_TDESC:
10730 if (note->namesz == 4
10731 && strcmp (note->namedata, "GDB") == 0)
10732 return elfcore_grok_gdb_tdesc (abfd, note);
10733 else
0a1b45a2 10734 return true;
b63a5e38 10735
db6092f3
AB
10736 case NT_RISCV_CSR:
10737 if (note->namesz == 4
10738 && strcmp (note->namedata, "GDB") == 0)
10739 return elfcore_grok_riscv_csr (abfd, note);
10740 else
0a1b45a2 10741 return true;
db6092f3 10742
e214f8db 10743 case NT_LARCH_CPUCFG:
10744 if (note->namesz == 6
10745 && strcmp (note->namedata, "LINUX") == 0)
10746 return elfcore_grok_loongarch_cpucfg (abfd, note);
10747 else
10748 return true;
10749
10750 case NT_LARCH_LBT:
10751 if (note->namesz == 6
10752 && strcmp (note->namedata, "LINUX") == 0)
10753 return elfcore_grok_loongarch_lbt (abfd, note);
10754 else
10755 return true;
10756
10757 case NT_LARCH_LSX:
10758 if (note->namesz == 6
10759 && strcmp (note->namedata, "LINUX") == 0)
10760 return elfcore_grok_loongarch_lsx (abfd, note);
10761 else
10762 return true;
10763
10764 case NT_LARCH_LASX:
10765 if (note->namesz == 6
10766 && strcmp (note->namedata, "LINUX") == 0)
10767 return elfcore_grok_loongarch_lasx (abfd, note);
10768 else
10769 return true;
10770
252b5132
RH
10771 case NT_PRPSINFO:
10772 case NT_PSINFO:
bb0082d6
AM
10773 if (bed->elf_backend_grok_psinfo)
10774 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
0a1b45a2 10775 return true;
bb0082d6 10776#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10777 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10778#else
0a1b45a2 10779 return true;
252b5132 10780#endif
3333a7c3
RM
10781
10782 case NT_AUXV:
58e07198 10783 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10784
451b7c33
TT
10785 case NT_FILE:
10786 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10787 note);
10788
9015683b
TT
10789 case NT_SIGINFO:
10790 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10791 note);
5b2c414d 10792
252b5132
RH
10793 }
10794}
10795
0a1b45a2 10796static bool
718175fa
JK
10797elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10798{
c74f7d1c 10799 struct bfd_build_id* build_id;
30e8ee25
AM
10800
10801 if (note->descsz == 0)
0a1b45a2 10802 return false;
30e8ee25 10803
c74f7d1c
JT
10804 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10805 if (build_id == NULL)
0a1b45a2 10806 return false;
718175fa 10807
c74f7d1c
JT
10808 build_id->size = note->descsz;
10809 memcpy (build_id->data, note->descdata, note->descsz);
10810 abfd->build_id = build_id;
718175fa 10811
0a1b45a2 10812 return true;
718175fa
JK
10813}
10814
0a1b45a2 10815static bool
718175fa
JK
10816elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10817{
10818 switch (note->type)
10819 {
10820 default:
0a1b45a2 10821 return true;
718175fa 10822
46bed679
L
10823 case NT_GNU_PROPERTY_TYPE_0:
10824 return _bfd_elf_parse_gnu_properties (abfd, note);
10825
718175fa
JK
10826 case NT_GNU_BUILD_ID:
10827 return elfobj_grok_gnu_build_id (abfd, note);
10828 }
10829}
10830
0a1b45a2 10831static bool
e21e5835
NC
10832elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10833{
10834 struct sdt_note *cur =
7a6e0d89
AM
10835 (struct sdt_note *) bfd_alloc (abfd,
10836 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10837
10838 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10839 cur->size = (bfd_size_type) note->descsz;
10840 memcpy (cur->data, note->descdata, note->descsz);
10841
10842 elf_tdata (abfd)->sdt_note_head = cur;
10843
0a1b45a2 10844 return true;
e21e5835
NC
10845}
10846
0a1b45a2 10847static bool
e21e5835
NC
10848elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10849{
10850 switch (note->type)
10851 {
10852 case NT_STAPSDT:
10853 return elfobj_grok_stapsdt_note_1 (abfd, note);
10854
10855 default:
0a1b45a2 10856 return true;
e21e5835
NC
10857 }
10858}
10859
0a1b45a2 10860static bool
aa1ed4a9
JB
10861elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10862{
10863 size_t offset;
10864
b5430a3c 10865 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10866 {
b5430a3c 10867 case ELFCLASS32:
0064d223 10868 if (note->descsz < 108)
0a1b45a2 10869 return false;
aa1ed4a9
JB
10870 break;
10871
b5430a3c 10872 case ELFCLASS64:
0064d223 10873 if (note->descsz < 120)
0a1b45a2 10874 return false;
aa1ed4a9
JB
10875 break;
10876
10877 default:
0a1b45a2 10878 return false;
aa1ed4a9
JB
10879 }
10880
0064d223
JB
10881 /* Check for version 1 in pr_version. */
10882 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10883 return false;
80a04378 10884
0064d223
JB
10885 offset = 4;
10886
10887 /* Skip over pr_psinfosz. */
b5430a3c 10888 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10889 offset += 4;
10890 else
10891 {
10892 offset += 4; /* Padding before pr_psinfosz. */
10893 offset += 8;
10894 }
10895
aa1ed4a9
JB
10896 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10897 elf_tdata (abfd)->core->program
10898 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10899 offset += 17;
10900
10901 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10902 elf_tdata (abfd)->core->command
10903 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10904 offset += 81;
10905
10906 /* Padding before pr_pid. */
10907 offset += 2;
10908
10909 /* The pr_pid field was added in version "1a". */
10910 if (note->descsz < offset + 4)
0a1b45a2 10911 return true;
0064d223
JB
10912
10913 elf_tdata (abfd)->core->pid
10914 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9 10915
0a1b45a2 10916 return true;
aa1ed4a9
JB
10917}
10918
0a1b45a2 10919static bool
aa1ed4a9
JB
10920elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10921{
10922 size_t offset;
10923 size_t size;
24d3e51b 10924 size_t min_size;
aa1ed4a9 10925
24d3e51b
NC
10926 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10927 Also compute minimum size of this note. */
b5430a3c 10928 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10929 {
b5430a3c 10930 case ELFCLASS32:
24d3e51b
NC
10931 offset = 4 + 4;
10932 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10933 break;
10934
b5430a3c 10935 case ELFCLASS64:
24d3e51b
NC
10936 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10937 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10938 break;
10939
10940 default:
0a1b45a2 10941 return false;
aa1ed4a9
JB
10942 }
10943
24d3e51b 10944 if (note->descsz < min_size)
0a1b45a2 10945 return false;
24d3e51b
NC
10946
10947 /* Check for version 1 in pr_version. */
10948 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
0a1b45a2 10949 return false;
aa1ed4a9 10950
24d3e51b
NC
10951 /* Extract size of pr_reg from pr_gregsetsz. */
10952 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10953 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10954 {
10955 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10956 offset += 4 * 2;
10957 }
b5430a3c 10958 else
24d3e51b
NC
10959 {
10960 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10961 offset += 8 * 2;
10962 }
aa1ed4a9 10963
24d3e51b 10964 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10965 offset += 4;
10966
24d3e51b 10967 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10968 if (elf_tdata (abfd)->core->signal == 0)
10969 elf_tdata (abfd)->core->signal
10970 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10971 offset += 4;
10972
24d3e51b 10973 /* Read TID from pr_pid. */
aa1ed4a9
JB
10974 elf_tdata (abfd)->core->lwpid
10975 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10976 offset += 4;
10977
24d3e51b 10978 /* Padding before pr_reg. */
b5430a3c 10979 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10980 offset += 4;
10981
24d3e51b
NC
10982 /* Make sure that there is enough data remaining in the note. */
10983 if ((note->descsz - offset) < size)
0a1b45a2 10984 return false;
24d3e51b 10985
aa1ed4a9
JB
10986 /* Make a ".reg/999" section and a ".reg" section. */
10987 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10988 size, note->descpos + offset);
10989}
10990
0a1b45a2 10991static bool
aa1ed4a9
JB
10992elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10993{
544c67cd
JB
10994 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10995
aa1ed4a9
JB
10996 switch (note->type)
10997 {
10998 case NT_PRSTATUS:
544c67cd
JB
10999 if (bed->elf_backend_grok_freebsd_prstatus)
11000 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
0a1b45a2 11001 return true;
aa1ed4a9
JB
11002 return elfcore_grok_freebsd_prstatus (abfd, note);
11003
11004 case NT_FPREGSET:
11005 return elfcore_grok_prfpreg (abfd, note);
11006
11007 case NT_PRPSINFO:
11008 return elfcore_grok_freebsd_psinfo (abfd, note);
11009
11010 case NT_FREEBSD_THRMISC:
11011 if (note->namesz == 8)
11012 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11013 else
0a1b45a2 11014 return true;
aa1ed4a9 11015
ddb2bbcf
JB
11016 case NT_FREEBSD_PROCSTAT_PROC:
11017 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11018 note);
11019
11020 case NT_FREEBSD_PROCSTAT_FILES:
11021 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11022 note);
11023
11024 case NT_FREEBSD_PROCSTAT_VMMAP:
11025 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11026 note);
11027
3350c5f5 11028 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 11029 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 11030
aa1ed4a9
JB
11031 case NT_X86_XSTATE:
11032 if (note->namesz == 8)
11033 return elfcore_grok_xstatereg (abfd, note);
11034 else
0a1b45a2 11035 return true;
aa1ed4a9 11036
e6f3b9c3
JB
11037 case NT_FREEBSD_PTLWPINFO:
11038 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11039 note);
11040
6d5be5d6
JB
11041 case NT_ARM_VFP:
11042 return elfcore_grok_arm_vfp (abfd, note);
11043
aa1ed4a9 11044 default:
0a1b45a2 11045 return true;
aa1ed4a9
JB
11046 }
11047}
11048
0a1b45a2 11049static bool
217aa764 11050elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
11051{
11052 char *cp;
11053
11054 cp = strchr (note->namedata, '@');
11055 if (cp != NULL)
11056 {
d2b64500 11057 *lwpidp = atoi(cp + 1);
0a1b45a2 11058 return true;
50b2bdb7 11059 }
0a1b45a2 11060 return false;
50b2bdb7
AM
11061}
11062
0a1b45a2 11063static bool
217aa764 11064elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 11065{
80a04378 11066 if (note->descsz <= 0x7c + 31)
0a1b45a2 11067 return false;
80a04378 11068
50b2bdb7 11069 /* Signal number at offset 0x08. */
228e534f 11070 elf_tdata (abfd)->core->signal
50b2bdb7
AM
11071 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11072
11073 /* Process ID at offset 0x50. */
228e534f 11074 elf_tdata (abfd)->core->pid
50b2bdb7
AM
11075 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11076
11077 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 11078 elf_tdata (abfd)->core->command
50b2bdb7
AM
11079 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11080
7720ba9f
MK
11081 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11082 note);
50b2bdb7
AM
11083}
11084
0a1b45a2 11085static bool
217aa764 11086elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
11087{
11088 int lwp;
11089
11090 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 11091 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 11092
58e07198 11093 switch (note->type)
50b2bdb7 11094 {
58e07198 11095 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 11096 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
11097 find this note before any of the others, which is fine,
11098 since the kernel writes this note out first when it
11099 creates a core file. */
50b2bdb7 11100 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
11101 case NT_NETBSDCORE_AUXV:
11102 /* NetBSD-specific Elf Auxiliary Vector data. */
11103 return elfcore_make_auxv_note_section (abfd, note, 4);
06d949ec
KR
11104 case NT_NETBSDCORE_LWPSTATUS:
11105 return elfcore_make_note_pseudosection (abfd,
11106 ".note.netbsdcore.lwpstatus",
11107 note);
58e07198
CZ
11108 default:
11109 break;
50b2bdb7
AM
11110 }
11111
06d949ec 11112 /* As of March 2020 there are no other machine-independent notes
b4db1224
JT
11113 defined for NetBSD core files. If the note type is less
11114 than the start of the machine-dependent note types, we don't
11115 understand it. */
47d9a591 11116
b4db1224 11117 if (note->type < NT_NETBSDCORE_FIRSTMACH)
0a1b45a2 11118 return true;
50b2bdb7
AM
11119
11120
11121 switch (bfd_get_arch (abfd))
11122 {
08a40648
AM
11123 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11124 PT_GETFPREGS == mach+2. */
50b2bdb7 11125
015ec493 11126 case bfd_arch_aarch64:
50b2bdb7
AM
11127 case bfd_arch_alpha:
11128 case bfd_arch_sparc:
11129 switch (note->type)
08a40648
AM
11130 {
11131 case NT_NETBSDCORE_FIRSTMACH+0:
11132 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11133
08a40648
AM
11134 case NT_NETBSDCORE_FIRSTMACH+2:
11135 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11136
08a40648 11137 default:
0a1b45a2 11138 return true;
08a40648 11139 }
50b2bdb7 11140
58e07198
CZ
11141 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11142 There's also old PT___GETREGS40 == mach + 1 for old reg
11143 structure which lacks GBR. */
11144
11145 case bfd_arch_sh:
11146 switch (note->type)
11147 {
11148 case NT_NETBSDCORE_FIRSTMACH+3:
11149 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11150
11151 case NT_NETBSDCORE_FIRSTMACH+5:
11152 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11153
11154 default:
0a1b45a2 11155 return true;
58e07198
CZ
11156 }
11157
08a40648
AM
11158 /* On all other arch's, PT_GETREGS == mach+1 and
11159 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
11160
11161 default:
11162 switch (note->type)
08a40648
AM
11163 {
11164 case NT_NETBSDCORE_FIRSTMACH+1:
11165 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 11166
08a40648
AM
11167 case NT_NETBSDCORE_FIRSTMACH+3:
11168 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 11169
08a40648 11170 default:
0a1b45a2 11171 return true;
08a40648 11172 }
50b2bdb7
AM
11173 }
11174 /* NOTREACHED */
11175}
11176
0a1b45a2 11177static bool
67cc5033
MK
11178elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11179{
80a04378 11180 if (note->descsz <= 0x48 + 31)
0a1b45a2 11181 return false;
80a04378 11182
67cc5033 11183 /* Signal number at offset 0x08. */
228e534f 11184 elf_tdata (abfd)->core->signal
67cc5033
MK
11185 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11186
11187 /* Process ID at offset 0x20. */
228e534f 11188 elf_tdata (abfd)->core->pid
67cc5033
MK
11189 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11190
11191 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 11192 elf_tdata (abfd)->core->command
67cc5033
MK
11193 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11194
0a1b45a2 11195 return true;
67cc5033
MK
11196}
11197
6420dd27
LB
11198/* Processes Solaris's process status note.
11199 sig_off ~ offsetof(prstatus_t, pr_cursig)
11200 pid_off ~ offsetof(prstatus_t, pr_pid)
11201 lwpid_off ~ offsetof(prstatus_t, pr_who)
11202 gregset_size ~ sizeof(gregset_t)
11203 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11204
11205static bool
11206elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11207 int pid_off, int lwpid_off, size_t gregset_size,
11208 size_t gregset_offset)
11209{
11210 asection *sect = NULL;
11211 elf_tdata (abfd)->core->signal
11212 = bfd_get_16 (abfd, note->descdata + sig_off);
11213 elf_tdata (abfd)->core->pid
11214 = bfd_get_32 (abfd, note->descdata + pid_off);
11215 elf_tdata (abfd)->core->lwpid
11216 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11217
11218 sect = bfd_get_section_by_name (abfd, ".reg");
11219 if (sect != NULL)
11220 sect->size = gregset_size;
11221
11222 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11223 note->descpos + gregset_offset);
11224}
11225
11226/* Gets program and arguments from a core.
11227 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11228 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11229
11230static bool
11231elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11232 int prog_off, int comm_off)
11233{
11234 elf_tdata (abfd)->core->program
11235 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11236 elf_tdata (abfd)->core->command
11237 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11238
11239 return true;
11240}
11241
11242/* Processes Solaris's LWP status note.
11243 gregset_size ~ sizeof(gregset_t)
11244 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11245 fpregset_size ~ sizeof(fpregset_t)
11246 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11247
11248static bool
11249elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11250 size_t gregset_size, int gregset_off,
11251 size_t fpregset_size, int fpregset_off)
11252{
11253 asection *sect = NULL;
11254 char reg2_section_name[16] = { 0 };
11255
11256 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11257 elf_tdata (abfd)->core->lwpid);
11258
11259 /* offsetof(lwpstatus_t, pr_lwpid) */
11260 elf_tdata (abfd)->core->lwpid
11261 = bfd_get_32 (abfd, note->descdata + 4);
11262 /* offsetof(lwpstatus_t, pr_cursig) */
11263 elf_tdata (abfd)->core->signal
11264 = bfd_get_16 (abfd, note->descdata + 12);
11265
11266 sect = bfd_get_section_by_name (abfd, ".reg");
11267 if (sect != NULL)
11268 sect->size = gregset_size;
11269 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11270 note->descpos + gregset_off))
11271 return false;
11272
11273 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11274 if (sect != NULL)
11275 {
11276 sect->size = fpregset_size;
11277 sect->filepos = note->descpos + fpregset_off;
11278 sect->alignment_power = 2;
11279 }
11280 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11281 note->descpos + fpregset_off))
11282 return false;
11283
11284 return true;
11285}
11286
11287static bool
11288elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11289{
11290 if (note == NULL)
11291 return false;
11292
11293 /* core files are identified as 32- or 64-bit, SPARC or x86,
11294 by the size of the descsz which matches the sizeof()
11295 the type appropriate for that note type (e.g., prstatus_t for
11296 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11297 on Solaris. The core file bitness may differ from the bitness of
11298 gdb itself, so fixed values are used instead of sizeof().
11299 Appropriate fixed offsets are also used to obtain data from
11300 the note. */
11301
11302 switch ((int) note->type)
11303 {
11304 case SOLARIS_NT_PRSTATUS:
11305 switch (note->descsz)
11306 {
11307 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11308 return elfcore_grok_solaris_prstatus(abfd, note,
11309 136, 216, 308, 152, 356);
11310 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11311 return elfcore_grok_solaris_prstatus(abfd, note,
11312 264, 360, 520, 304, 600);
11313 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11314 return elfcore_grok_solaris_prstatus(abfd, note,
11315 136, 216, 308, 76, 356);
11316 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11317 return elfcore_grok_solaris_prstatus(abfd, note,
11318 264, 360, 520, 224, 600);
11319 default:
11320 return true;
11321 }
11322
11323 case SOLARIS_NT_PSINFO:
11324 case SOLARIS_NT_PRPSINFO:
11325 switch (note->descsz)
11326 {
11327 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11328 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11329 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11330 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11331 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11332 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11333 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11334 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11335 default:
11336 return true;
11337 }
11338
11339 case SOLARIS_NT_LWPSTATUS:
11340 switch (note->descsz)
11341 {
11342 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11343 return elfcore_grok_solaris_lwpstatus(abfd, note,
11344 152, 344, 400, 496);
11345 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11346 return elfcore_grok_solaris_lwpstatus(abfd, note,
11347 304, 544, 544, 848);
11348 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11349 return elfcore_grok_solaris_lwpstatus(abfd, note,
11350 76, 344, 380, 420);
11351 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11352 return elfcore_grok_solaris_lwpstatus(abfd, note,
11353 224, 544, 528, 768);
11354 default:
11355 return true;
11356 }
11357
11358 case SOLARIS_NT_LWPSINFO:
11359 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11360 if (note->descsz == 128 || note->descsz == 152)
11361 elf_tdata (abfd)->core->lwpid =
11362 bfd_get_32 (abfd, note->descdata + 4);
11363 break;
11364
11365 default:
11366 break;
11367 }
11368
11369 return true;
11370}
11371
11372/* For name starting with "CORE" this may be either a Solaris
11373 core file or a gdb-generated core file. Do Solaris-specific
11374 processing on selected note types first with
11375 elfcore_grok_solaris_note(), then process the note
11376 in elfcore_grok_note(). */
11377
11378static bool
11379elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11380{
11381 if (!elfcore_grok_solaris_note_impl (abfd, note))
11382 return false;
11383
11384 return elfcore_grok_note (abfd, note);
11385}
11386
0a1b45a2 11387static bool
67cc5033
MK
11388elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11389{
11390 if (note->type == NT_OPENBSD_PROCINFO)
11391 return elfcore_grok_openbsd_procinfo (abfd, note);
11392
11393 if (note->type == NT_OPENBSD_REGS)
11394 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11395
11396 if (note->type == NT_OPENBSD_FPREGS)
11397 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11398
11399 if (note->type == NT_OPENBSD_XFPREGS)
11400 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11401
11402 if (note->type == NT_OPENBSD_AUXV)
58e07198 11403 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
11404
11405 if (note->type == NT_OPENBSD_WCOOKIE)
11406 {
11407 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11408 SEC_HAS_CONTENTS);
11409
11410 if (sect == NULL)
0a1b45a2 11411 return false;
67cc5033
MK
11412 sect->size = note->descsz;
11413 sect->filepos = note->descpos;
11414 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11415
0a1b45a2 11416 return true;
67cc5033
MK
11417 }
11418
0a1b45a2 11419 return true;
67cc5033
MK
11420}
11421
0a1b45a2 11422static bool
d3fd4074 11423elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
11424{
11425 void *ddata = note->descdata;
11426 char buf[100];
11427 char *name;
11428 asection *sect;
f8843e87
AM
11429 short sig;
11430 unsigned flags;
07c6e936 11431
80a04378 11432 if (note->descsz < 16)
0a1b45a2 11433 return false;
80a04378 11434
07c6e936 11435 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 11436 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 11437
f8843e87
AM
11438 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11439 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11440
11441 /* nto_procfs_status 'flags' field is at offset 8. */
11442 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
11443
11444 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
11445 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11446 {
228e534f
AM
11447 elf_tdata (abfd)->core->signal = sig;
11448 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 11449 }
07c6e936 11450
f8843e87
AM
11451 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11452 do not come from signals so we make sure we set the current
11453 thread just in case. */
11454 if (flags & 0x00000080)
228e534f 11455 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
11456
11457 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 11458 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 11459
a50b1753 11460 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11461 if (name == NULL)
0a1b45a2 11462 return false;
07c6e936
NC
11463 strcpy (name, buf);
11464
117ed4f8 11465 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11466 if (sect == NULL)
0a1b45a2 11467 return false;
07c6e936 11468
07d6d2b8
AM
11469 sect->size = note->descsz;
11470 sect->filepos = note->descpos;
07c6e936
NC
11471 sect->alignment_power = 2;
11472
11473 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11474}
11475
0a1b45a2 11476static bool
d69f560c
KW
11477elfcore_grok_nto_regs (bfd *abfd,
11478 Elf_Internal_Note *note,
d3fd4074 11479 long tid,
d69f560c 11480 char *base)
07c6e936
NC
11481{
11482 char buf[100];
11483 char *name;
11484 asection *sect;
11485
d69f560c 11486 /* Make a "(base)/%d" section. */
d3fd4074 11487 sprintf (buf, "%s/%ld", base, tid);
07c6e936 11488
a50b1753 11489 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936 11490 if (name == NULL)
0a1b45a2 11491 return false;
07c6e936
NC
11492 strcpy (name, buf);
11493
117ed4f8 11494 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936 11495 if (sect == NULL)
0a1b45a2 11496 return false;
07c6e936 11497
07d6d2b8
AM
11498 sect->size = note->descsz;
11499 sect->filepos = note->descpos;
07c6e936
NC
11500 sect->alignment_power = 2;
11501
f8843e87 11502 /* This is the current thread. */
228e534f 11503 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 11504 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87 11505
0a1b45a2 11506 return true;
07c6e936
NC
11507}
11508
11509#define BFD_QNT_CORE_INFO 7
11510#define BFD_QNT_CORE_STATUS 8
11511#define BFD_QNT_CORE_GREG 9
11512#define BFD_QNT_CORE_FPREG 10
11513
0a1b45a2 11514static bool
217aa764 11515elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
11516{
11517 /* Every GREG section has a STATUS section before it. Store the
811072d8 11518 tid from the previous call to pass down to the next gregs
07c6e936 11519 function. */
d3fd4074 11520 static long tid = 1;
07c6e936
NC
11521
11522 switch (note->type)
11523 {
d69f560c
KW
11524 case BFD_QNT_CORE_INFO:
11525 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11526 case BFD_QNT_CORE_STATUS:
11527 return elfcore_grok_nto_status (abfd, note, &tid);
11528 case BFD_QNT_CORE_GREG:
11529 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11530 case BFD_QNT_CORE_FPREG:
11531 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11532 default:
0a1b45a2 11533 return true;
07c6e936
NC
11534 }
11535}
11536
0a1b45a2 11537static bool
b15fa79e
AM
11538elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11539{
11540 char *name;
11541 asection *sect;
11542 size_t len;
11543
11544 /* Use note name as section name. */
11545 len = note->namesz;
a50b1753 11546 name = (char *) bfd_alloc (abfd, len);
b15fa79e 11547 if (name == NULL)
0a1b45a2 11548 return false;
b15fa79e
AM
11549 memcpy (name, note->namedata, len);
11550 name[len - 1] = '\0';
11551
11552 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11553 if (sect == NULL)
0a1b45a2 11554 return false;
b15fa79e 11555
07d6d2b8
AM
11556 sect->size = note->descsz;
11557 sect->filepos = note->descpos;
b15fa79e
AM
11558 sect->alignment_power = 1;
11559
0a1b45a2 11560 return true;
b15fa79e
AM
11561}
11562
7c76fa91
MS
11563/* Function: elfcore_write_note
11564
47d9a591 11565 Inputs:
a39f3346 11566 buffer to hold note, and current size of buffer
7c76fa91
MS
11567 name of note
11568 type of note
11569 data for note
11570 size of data for note
11571
a39f3346
AM
11572 Writes note to end of buffer. ELF64 notes are written exactly as
11573 for ELF32, despite the current (as of 2006) ELF gabi specifying
11574 that they ought to have 8-byte namesz and descsz field, and have
11575 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11576
7c76fa91 11577 Return:
a39f3346 11578 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
11579
11580char *
a39f3346 11581elfcore_write_note (bfd *abfd,
217aa764 11582 char *buf,
a39f3346 11583 int *bufsiz,
217aa764 11584 const char *name,
a39f3346 11585 int type,
217aa764 11586 const void *input,
a39f3346 11587 int size)
7c76fa91
MS
11588{
11589 Elf_External_Note *xnp;
d4c88bbb 11590 size_t namesz;
d4c88bbb 11591 size_t newspace;
a39f3346 11592 char *dest;
7c76fa91 11593
d4c88bbb 11594 namesz = 0;
d4c88bbb 11595 if (name != NULL)
a39f3346 11596 namesz = strlen (name) + 1;
d4c88bbb 11597
a39f3346 11598 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 11599
a50b1753 11600 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
11601 if (buf == NULL)
11602 return buf;
a39f3346 11603 dest = buf + *bufsiz;
7c76fa91
MS
11604 *bufsiz += newspace;
11605 xnp = (Elf_External_Note *) dest;
11606 H_PUT_32 (abfd, namesz, xnp->namesz);
11607 H_PUT_32 (abfd, size, xnp->descsz);
11608 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
11609 dest = xnp->name;
11610 if (name != NULL)
11611 {
11612 memcpy (dest, name, namesz);
11613 dest += namesz;
a39f3346 11614 while (namesz & 3)
d4c88bbb
AM
11615 {
11616 *dest++ = '\0';
a39f3346 11617 ++namesz;
d4c88bbb
AM
11618 }
11619 }
11620 memcpy (dest, input, size);
a39f3346
AM
11621 dest += size;
11622 while (size & 3)
11623 {
11624 *dest++ = '\0';
11625 ++size;
11626 }
11627 return buf;
7c76fa91
MS
11628}
11629
602f1657
AM
11630/* gcc-8 warns (*) on all the strncpy calls in this function about
11631 possible string truncation. The "truncation" is not a bug. We
11632 have an external representation of structs with fields that are not
11633 necessarily NULL terminated and corresponding internal
11634 representation fields that are one larger so that they can always
11635 be NULL terminated.
11636 gcc versions between 4.2 and 4.6 do not allow pragma control of
11637 diagnostics inside functions, giving a hard error if you try to use
11638 the finer control available with later versions.
11639 gcc prior to 4.2 warns about diagnostic push and pop.
11640 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11641 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11642 (*) Depending on your system header files! */
d99b4b92 11643#if GCC_VERSION >= 8000
602f1657
AM
11644# pragma GCC diagnostic push
11645# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 11646#endif
7c76fa91 11647char *
217aa764
AM
11648elfcore_write_prpsinfo (bfd *abfd,
11649 char *buf,
11650 int *bufsiz,
11651 const char *fname,
11652 const char *psargs)
7c76fa91 11653{
183e98be
AM
11654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11655
11656 if (bed->elf_backend_write_core_note != NULL)
11657 {
11658 char *ret;
11659 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11660 NT_PRPSINFO, fname, psargs);
11661 if (ret != NULL)
11662 return ret;
11663 }
7c76fa91 11664
1f20dca5 11665#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11666# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11667 if (bed->s->elfclass == ELFCLASS32)
11668 {
602f1657 11669# if defined (HAVE_PSINFO32_T)
183e98be
AM
11670 psinfo32_t data;
11671 int note_type = NT_PSINFO;
602f1657 11672# else
183e98be
AM
11673 prpsinfo32_t data;
11674 int note_type = NT_PRPSINFO;
602f1657 11675# endif
183e98be
AM
11676
11677 memset (&data, 0, sizeof (data));
11678 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11679 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11680 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11681 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11682 }
11683 else
602f1657 11684# endif
183e98be 11685 {
602f1657 11686# if defined (HAVE_PSINFO_T)
183e98be
AM
11687 psinfo_t data;
11688 int note_type = NT_PSINFO;
602f1657 11689# else
183e98be
AM
11690 prpsinfo_t data;
11691 int note_type = NT_PRPSINFO;
602f1657 11692# endif
7c76fa91 11693
183e98be
AM
11694 memset (&data, 0, sizeof (data));
11695 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11696 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11697 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11698 "CORE", note_type, &data, sizeof (data));
183e98be 11699 }
7c76fa91
MS
11700#endif /* PSINFO_T or PRPSINFO_T */
11701
1f20dca5
UW
11702 free (buf);
11703 return NULL;
11704}
d99b4b92 11705#if GCC_VERSION >= 8000
602f1657 11706# pragma GCC diagnostic pop
d99b4b92 11707#endif
1f20dca5 11708
70a38d42
SDJ
11709char *
11710elfcore_write_linux_prpsinfo32
11711 (bfd *abfd, char *buf, int *bufsiz,
11712 const struct elf_internal_linux_prpsinfo *prpsinfo)
11713{
a2f63b2e
MR
11714 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11715 {
11716 struct elf_external_linux_prpsinfo32_ugid16 data;
11717
11718 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11719 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11720 &data, sizeof (data));
11721 }
11722 else
11723 {
11724 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11725
a2f63b2e
MR
11726 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11727 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11728 &data, sizeof (data));
11729 }
70a38d42
SDJ
11730}
11731
11732char *
11733elfcore_write_linux_prpsinfo64
11734 (bfd *abfd, char *buf, int *bufsiz,
11735 const struct elf_internal_linux_prpsinfo *prpsinfo)
11736{
3c9a7b0d
MR
11737 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11738 {
11739 struct elf_external_linux_prpsinfo64_ugid16 data;
11740
11741 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11742 return elfcore_write_note (abfd, buf, bufsiz,
11743 "CORE", NT_PRPSINFO, &data, sizeof (data));
11744 }
11745 else
11746 {
11747 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11748
3c9a7b0d
MR
11749 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11750 return elfcore_write_note (abfd, buf, bufsiz,
11751 "CORE", NT_PRPSINFO, &data, sizeof (data));
11752 }
70a38d42
SDJ
11753}
11754
7c76fa91 11755char *
217aa764
AM
11756elfcore_write_prstatus (bfd *abfd,
11757 char *buf,
11758 int *bufsiz,
11759 long pid,
11760 int cursig,
11761 const void *gregs)
7c76fa91 11762{
183e98be 11763 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11764
183e98be
AM
11765 if (bed->elf_backend_write_core_note != NULL)
11766 {
11767 char *ret;
11768 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11769 NT_PRSTATUS,
11770 pid, cursig, gregs);
11771 if (ret != NULL)
11772 return ret;
11773 }
11774
1f20dca5 11775#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11776#if defined (HAVE_PRSTATUS32_T)
11777 if (bed->s->elfclass == ELFCLASS32)
11778 {
11779 prstatus32_t prstat;
11780
11781 memset (&prstat, 0, sizeof (prstat));
11782 prstat.pr_pid = pid;
11783 prstat.pr_cursig = cursig;
11784 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11785 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11786 NT_PRSTATUS, &prstat, sizeof (prstat));
11787 }
11788 else
11789#endif
11790 {
11791 prstatus_t prstat;
11792
11793 memset (&prstat, 0, sizeof (prstat));
11794 prstat.pr_pid = pid;
11795 prstat.pr_cursig = cursig;
11796 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11797 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11798 NT_PRSTATUS, &prstat, sizeof (prstat));
11799 }
7c76fa91
MS
11800#endif /* HAVE_PRSTATUS_T */
11801
1f20dca5
UW
11802 free (buf);
11803 return NULL;
11804}
11805
51316059
MS
11806#if defined (HAVE_LWPSTATUS_T)
11807char *
217aa764
AM
11808elfcore_write_lwpstatus (bfd *abfd,
11809 char *buf,
11810 int *bufsiz,
11811 long pid,
11812 int cursig,
11813 const void *gregs)
51316059
MS
11814{
11815 lwpstatus_t lwpstat;
183e98be 11816 const char *note_name = "CORE";
51316059
MS
11817
11818 memset (&lwpstat, 0, sizeof (lwpstat));
11819 lwpstat.pr_lwpid = pid >> 16;
11820 lwpstat.pr_cursig = cursig;
11821#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11822 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11823#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11824#if !defined(gregs)
11825 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11826 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11827#else
11828 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11829 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11830#endif
11831#endif
47d9a591 11832 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11833 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11834}
11835#endif /* HAVE_LWPSTATUS_T */
11836
7c76fa91
MS
11837#if defined (HAVE_PSTATUS_T)
11838char *
217aa764
AM
11839elfcore_write_pstatus (bfd *abfd,
11840 char *buf,
11841 int *bufsiz,
11842 long pid,
6c10990d
NC
11843 int cursig ATTRIBUTE_UNUSED,
11844 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11845{
183e98be
AM
11846 const char *note_name = "CORE";
11847#if defined (HAVE_PSTATUS32_T)
11848 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11849
183e98be
AM
11850 if (bed->s->elfclass == ELFCLASS32)
11851 {
11852 pstatus32_t pstat;
11853
11854 memset (&pstat, 0, sizeof (pstat));
11855 pstat.pr_pid = pid & 0xffff;
11856 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11857 NT_PSTATUS, &pstat, sizeof (pstat));
11858 return buf;
11859 }
11860 else
11861#endif
11862 {
11863 pstatus_t pstat;
11864
11865 memset (&pstat, 0, sizeof (pstat));
11866 pstat.pr_pid = pid & 0xffff;
11867 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11868 NT_PSTATUS, &pstat, sizeof (pstat));
11869 return buf;
11870 }
7c76fa91
MS
11871}
11872#endif /* HAVE_PSTATUS_T */
11873
11874char *
217aa764
AM
11875elfcore_write_prfpreg (bfd *abfd,
11876 char *buf,
11877 int *bufsiz,
11878 const void *fpregs,
11879 int size)
7c76fa91 11880{
183e98be 11881 const char *note_name = "CORE";
47d9a591 11882 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11883 note_name, NT_FPREGSET, fpregs, size);
11884}
11885
11886char *
217aa764
AM
11887elfcore_write_prxfpreg (bfd *abfd,
11888 char *buf,
11889 int *bufsiz,
11890 const void *xfpregs,
11891 int size)
7c76fa91
MS
11892{
11893 char *note_name = "LINUX";
47d9a591 11894 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11895 note_name, NT_PRXFPREG, xfpregs, size);
11896}
11897
4339cae0
L
11898char *
11899elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11900 const void *xfpregs, int size)
11901{
97de3545
JB
11902 char *note_name;
11903 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11904 note_name = "FreeBSD";
11905 else
11906 note_name = "LINUX";
4339cae0
L
11907 return elfcore_write_note (abfd, buf, bufsiz,
11908 note_name, NT_X86_XSTATE, xfpregs, size);
11909}
11910
97753bd5
AM
11911char *
11912elfcore_write_ppc_vmx (bfd *abfd,
11913 char *buf,
11914 int *bufsiz,
11915 const void *ppc_vmx,
11916 int size)
11917{
11918 char *note_name = "LINUX";
11919 return elfcore_write_note (abfd, buf, bufsiz,
11920 note_name, NT_PPC_VMX, ppc_vmx, size);
11921}
11922
89eeb0bc
LM
11923char *
11924elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11925 char *buf,
11926 int *bufsiz,
11927 const void *ppc_vsx,
11928 int size)
89eeb0bc
LM
11929{
11930 char *note_name = "LINUX";
11931 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11932 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11933}
11934
cb2366c1
EBM
11935char *
11936elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11937 char *buf,
11938 int *bufsiz,
11939 const void *ppc_tar,
11940 int size)
cb2366c1
EBM
11941{
11942 char *note_name = "LINUX";
11943 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11944 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11945}
11946
11947char *
11948elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11949 char *buf,
11950 int *bufsiz,
11951 const void *ppc_ppr,
11952 int size)
cb2366c1
EBM
11953{
11954 char *note_name = "LINUX";
11955 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11956 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11957}
11958
11959char *
11960elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11961 char *buf,
11962 int *bufsiz,
11963 const void *ppc_dscr,
11964 int size)
cb2366c1
EBM
11965{
11966 char *note_name = "LINUX";
11967 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11968 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11969}
11970
11971char *
11972elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11973 char *buf,
11974 int *bufsiz,
11975 const void *ppc_ebb,
11976 int size)
cb2366c1
EBM
11977{
11978 char *note_name = "LINUX";
11979 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11980 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11981}
11982
11983char *
11984elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11985 char *buf,
11986 int *bufsiz,
11987 const void *ppc_pmu,
11988 int size)
cb2366c1
EBM
11989{
11990 char *note_name = "LINUX";
11991 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11992 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11993}
11994
11995char *
11996elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11997 char *buf,
11998 int *bufsiz,
11999 const void *ppc_tm_cgpr,
12000 int size)
cb2366c1
EBM
12001{
12002 char *note_name = "LINUX";
12003 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12004 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
12005}
12006
12007char *
12008elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
12009 char *buf,
12010 int *bufsiz,
12011 const void *ppc_tm_cfpr,
12012 int size)
cb2366c1
EBM
12013{
12014 char *note_name = "LINUX";
12015 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12016 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
12017}
12018
12019char *
12020elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
12021 char *buf,
12022 int *bufsiz,
12023 const void *ppc_tm_cvmx,
12024 int size)
cb2366c1
EBM
12025{
12026 char *note_name = "LINUX";
12027 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12028 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
12029}
12030
12031char *
12032elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
12033 char *buf,
12034 int *bufsiz,
12035 const void *ppc_tm_cvsx,
12036 int size)
cb2366c1
EBM
12037{
12038 char *note_name = "LINUX";
12039 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12040 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
12041}
12042
12043char *
12044elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
12045 char *buf,
12046 int *bufsiz,
12047 const void *ppc_tm_spr,
12048 int size)
cb2366c1
EBM
12049{
12050 char *note_name = "LINUX";
12051 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12052 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
12053}
12054
12055char *
12056elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
12057 char *buf,
12058 int *bufsiz,
12059 const void *ppc_tm_ctar,
12060 int size)
cb2366c1
EBM
12061{
12062 char *note_name = "LINUX";
12063 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12064 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
12065}
12066
12067char *
12068elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
12069 char *buf,
12070 int *bufsiz,
12071 const void *ppc_tm_cppr,
12072 int size)
cb2366c1
EBM
12073{
12074 char *note_name = "LINUX";
12075 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12076 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
12077}
12078
12079char *
12080elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
12081 char *buf,
12082 int *bufsiz,
12083 const void *ppc_tm_cdscr,
12084 int size)
cb2366c1
EBM
12085{
12086 char *note_name = "LINUX";
12087 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 12088 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
12089}
12090
0675e188
UW
12091static char *
12092elfcore_write_s390_high_gprs (bfd *abfd,
12093 char *buf,
12094 int *bufsiz,
12095 const void *s390_high_gprs,
12096 int size)
12097{
12098 char *note_name = "LINUX";
12099 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12100 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
12101 s390_high_gprs, size);
12102}
12103
d7eeb400
MS
12104char *
12105elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
12106 char *buf,
12107 int *bufsiz,
12108 const void *s390_timer,
12109 int size)
d7eeb400
MS
12110{
12111 char *note_name = "LINUX";
12112 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12113 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
12114}
12115
12116char *
12117elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
12118 char *buf,
12119 int *bufsiz,
12120 const void *s390_todcmp,
12121 int size)
d7eeb400
MS
12122{
12123 char *note_name = "LINUX";
12124 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12125 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
12126}
12127
12128char *
12129elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
12130 char *buf,
12131 int *bufsiz,
12132 const void *s390_todpreg,
12133 int size)
d7eeb400
MS
12134{
12135 char *note_name = "LINUX";
12136 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12137 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
12138}
12139
12140char *
12141elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
12142 char *buf,
12143 int *bufsiz,
12144 const void *s390_ctrs,
12145 int size)
d7eeb400
MS
12146{
12147 char *note_name = "LINUX";
12148 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12149 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
12150}
12151
12152char *
12153elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
12154 char *buf,
12155 int *bufsiz,
12156 const void *s390_prefix,
12157 int size)
d7eeb400
MS
12158{
12159 char *note_name = "LINUX";
12160 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12161 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
12162}
12163
355b81d9
UW
12164char *
12165elfcore_write_s390_last_break (bfd *abfd,
12166 char *buf,
12167 int *bufsiz,
12168 const void *s390_last_break,
12169 int size)
12170{
12171 char *note_name = "LINUX";
12172 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12173 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
12174 s390_last_break, size);
12175}
12176
12177char *
12178elfcore_write_s390_system_call (bfd *abfd,
12179 char *buf,
12180 int *bufsiz,
12181 const void *s390_system_call,
12182 int size)
12183{
12184 char *note_name = "LINUX";
12185 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12186 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
12187 s390_system_call, size);
12188}
12189
abb3f6cc
NC
12190char *
12191elfcore_write_s390_tdb (bfd *abfd,
12192 char *buf,
12193 int *bufsiz,
12194 const void *s390_tdb,
12195 int size)
12196{
12197 char *note_name = "LINUX";
12198 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 12199 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
12200}
12201
4ef9f41a
AA
12202char *
12203elfcore_write_s390_vxrs_low (bfd *abfd,
12204 char *buf,
12205 int *bufsiz,
12206 const void *s390_vxrs_low,
12207 int size)
12208{
12209 char *note_name = "LINUX";
12210 return elfcore_write_note (abfd, buf, bufsiz,
12211 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12212}
12213
12214char *
12215elfcore_write_s390_vxrs_high (bfd *abfd,
12216 char *buf,
12217 int *bufsiz,
12218 const void *s390_vxrs_high,
12219 int size)
12220{
12221 char *note_name = "LINUX";
12222 return elfcore_write_note (abfd, buf, bufsiz,
12223 note_name, NT_S390_VXRS_HIGH,
12224 s390_vxrs_high, size);
12225}
12226
88ab90e8
AA
12227char *
12228elfcore_write_s390_gs_cb (bfd *abfd,
12229 char *buf,
12230 int *bufsiz,
12231 const void *s390_gs_cb,
12232 int size)
12233{
12234 char *note_name = "LINUX";
12235 return elfcore_write_note (abfd, buf, bufsiz,
12236 note_name, NT_S390_GS_CB,
12237 s390_gs_cb, size);
12238}
12239
12240char *
12241elfcore_write_s390_gs_bc (bfd *abfd,
12242 char *buf,
12243 int *bufsiz,
12244 const void *s390_gs_bc,
12245 int size)
12246{
12247 char *note_name = "LINUX";
12248 return elfcore_write_note (abfd, buf, bufsiz,
12249 note_name, NT_S390_GS_BC,
12250 s390_gs_bc, size);
12251}
12252
faa9a424
UW
12253char *
12254elfcore_write_arm_vfp (bfd *abfd,
12255 char *buf,
12256 int *bufsiz,
12257 const void *arm_vfp,
12258 int size)
12259{
12260 char *note_name = "LINUX";
12261 return elfcore_write_note (abfd, buf, bufsiz,
12262 note_name, NT_ARM_VFP, arm_vfp, size);
12263}
12264
652451f8
YZ
12265char *
12266elfcore_write_aarch_tls (bfd *abfd,
12267 char *buf,
12268 int *bufsiz,
12269 const void *aarch_tls,
12270 int size)
12271{
12272 char *note_name = "LINUX";
12273 return elfcore_write_note (abfd, buf, bufsiz,
12274 note_name, NT_ARM_TLS, aarch_tls, size);
12275}
12276
12277char *
12278elfcore_write_aarch_hw_break (bfd *abfd,
12279 char *buf,
12280 int *bufsiz,
12281 const void *aarch_hw_break,
12282 int size)
12283{
12284 char *note_name = "LINUX";
12285 return elfcore_write_note (abfd, buf, bufsiz,
12286 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12287}
12288
12289char *
12290elfcore_write_aarch_hw_watch (bfd *abfd,
12291 char *buf,
12292 int *bufsiz,
12293 const void *aarch_hw_watch,
12294 int size)
12295{
12296 char *note_name = "LINUX";
12297 return elfcore_write_note (abfd, buf, bufsiz,
12298 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12299}
12300
ad1cc4e4
AH
12301char *
12302elfcore_write_aarch_sve (bfd *abfd,
12303 char *buf,
12304 int *bufsiz,
12305 const void *aarch_sve,
12306 int size)
12307{
12308 char *note_name = "LINUX";
12309 return elfcore_write_note (abfd, buf, bufsiz,
12310 note_name, NT_ARM_SVE, aarch_sve, size);
12311}
12312
e6c3b5bf
AH
12313char *
12314elfcore_write_aarch_pauth (bfd *abfd,
12315 char *buf,
12316 int *bufsiz,
12317 const void *aarch_pauth,
12318 int size)
12319{
12320 char *note_name = "LINUX";
12321 return elfcore_write_note (abfd, buf, bufsiz,
12322 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12323}
12324
f0bbe8ba
LM
12325char *
12326elfcore_write_aarch_mte (bfd *abfd,
12327 char *buf,
12328 int *bufsiz,
12329 const void *aarch_mte,
12330 int size)
12331{
12332 char *note_name = "LINUX";
12333 return elfcore_write_note (abfd, buf, bufsiz,
12334 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12335 aarch_mte,
12336 size);
12337}
12338
27456742
AK
12339char *
12340elfcore_write_arc_v2 (bfd *abfd,
12341 char *buf,
12342 int *bufsiz,
12343 const void *arc_v2,
12344 int size)
12345{
12346 char *note_name = "LINUX";
12347 return elfcore_write_note (abfd, buf, bufsiz,
12348 note_name, NT_ARC_V2, arc_v2, size);
12349}
12350
e214f8db 12351char *
12352elfcore_write_loongarch_cpucfg (bfd *abfd,
12353 char *buf,
12354 int *bufsiz,
12355 const void *loongarch_cpucfg,
12356 int size)
12357{
12358 char *note_name = "LINUX";
12359 return elfcore_write_note (abfd, buf, bufsiz,
12360 note_name, NT_LARCH_CPUCFG,
12361 loongarch_cpucfg, size);
12362}
12363
12364char *
12365elfcore_write_loongarch_lbt (bfd *abfd,
12366 char *buf,
12367 int *bufsiz,
12368 const void *loongarch_lbt,
12369 int size)
12370{
12371 char *note_name = "LINUX";
12372 return elfcore_write_note (abfd, buf, bufsiz,
12373 note_name, NT_LARCH_LBT, loongarch_lbt, size);
12374}
12375
12376char *
12377elfcore_write_loongarch_lsx (bfd *abfd,
12378 char *buf,
12379 int *bufsiz,
12380 const void *loongarch_lsx,
12381 int size)
12382{
12383 char *note_name = "LINUX";
12384 return elfcore_write_note (abfd, buf, bufsiz,
12385 note_name, NT_LARCH_LSX, loongarch_lsx, size);
12386}
12387
12388char *
12389elfcore_write_loongarch_lasx (bfd *abfd,
12390 char *buf,
12391 int *bufsiz,
12392 const void *loongarch_lasx,
12393 int size)
12394{
12395 char *note_name = "LINUX";
12396 return elfcore_write_note (abfd, buf, bufsiz,
12397 note_name, NT_LARCH_LASX, loongarch_lasx, size);
12398}
12399
db6092f3
AB
12400/* Write the buffer of csr values in CSRS (length SIZE) into the note
12401 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12402 written into. Return a pointer to the new start of the note buffer, to
12403 replace BUF which may no longer be valid. */
12404
12405char *
12406elfcore_write_riscv_csr (bfd *abfd,
12407 char *buf,
12408 int *bufsiz,
12409 const void *csrs,
12410 int size)
12411{
12412 const char *note_name = "GDB";
12413 return elfcore_write_note (abfd, buf, bufsiz,
12414 note_name, NT_RISCV_CSR, csrs, size);
12415}
12416
b63a5e38
AB
12417/* Write the target description (a string) pointed to by TDESC, length
12418 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12419 note is being written into. Return a pointer to the new start of the
12420 note buffer, to replace BUF which may no longer be valid. */
12421
12422char *
12423elfcore_write_gdb_tdesc (bfd *abfd,
12424 char *buf,
12425 int *bufsiz,
12426 const void *tdesc,
12427 int size)
12428{
12429 const char *note_name = "GDB";
12430 return elfcore_write_note (abfd, buf, bufsiz,
12431 note_name, NT_GDB_TDESC, tdesc, size);
12432}
12433
bb864ac1
CES
12434char *
12435elfcore_write_register_note (bfd *abfd,
12436 char *buf,
12437 int *bufsiz,
12438 const char *section,
12439 const void *data,
12440 int size)
12441{
12442 if (strcmp (section, ".reg2") == 0)
12443 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12444 if (strcmp (section, ".reg-xfp") == 0)
12445 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
12446 if (strcmp (section, ".reg-xstate") == 0)
12447 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12448 if (strcmp (section, ".reg-ppc-vmx") == 0)
12449 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
12450 if (strcmp (section, ".reg-ppc-vsx") == 0)
12451 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
12452 if (strcmp (section, ".reg-ppc-tar") == 0)
12453 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12454 if (strcmp (section, ".reg-ppc-ppr") == 0)
12455 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12456 if (strcmp (section, ".reg-ppc-dscr") == 0)
12457 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12458 if (strcmp (section, ".reg-ppc-ebb") == 0)
12459 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12460 if (strcmp (section, ".reg-ppc-pmu") == 0)
12461 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12462 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12463 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12464 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12465 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12466 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12467 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12468 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12469 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12470 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12471 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12472 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12473 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12474 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12475 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12476 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12477 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
12478 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12479 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
12480 if (strcmp (section, ".reg-s390-timer") == 0)
12481 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12482 if (strcmp (section, ".reg-s390-todcmp") == 0)
12483 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12484 if (strcmp (section, ".reg-s390-todpreg") == 0)
12485 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12486 if (strcmp (section, ".reg-s390-ctrs") == 0)
12487 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12488 if (strcmp (section, ".reg-s390-prefix") == 0)
12489 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
12490 if (strcmp (section, ".reg-s390-last-break") == 0)
12491 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12492 if (strcmp (section, ".reg-s390-system-call") == 0)
12493 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
12494 if (strcmp (section, ".reg-s390-tdb") == 0)
12495 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
12496 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12497 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12498 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12499 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
12500 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12501 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12502 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12503 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
12504 if (strcmp (section, ".reg-arm-vfp") == 0)
12505 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
12506 if (strcmp (section, ".reg-aarch-tls") == 0)
12507 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12508 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12509 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12510 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12511 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
12512 if (strcmp (section, ".reg-aarch-sve") == 0)
12513 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
12514 if (strcmp (section, ".reg-aarch-pauth") == 0)
12515 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
f0bbe8ba
LM
12516 if (strcmp (section, ".reg-aarch-mte") == 0)
12517 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
27456742
AK
12518 if (strcmp (section, ".reg-arc-v2") == 0)
12519 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
b63a5e38
AB
12520 if (strcmp (section, ".gdb-tdesc") == 0)
12521 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
db6092f3
AB
12522 if (strcmp (section, ".reg-riscv-csr") == 0)
12523 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
e214f8db 12524 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12525 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12526 if (strcmp (section, ".reg-loongarch-lbt") == 0)
12527 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12528 if (strcmp (section, ".reg-loongarch-lsx") == 0)
12529 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12530 if (strcmp (section, ".reg-loongarch-lasx") == 0)
12531 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
bb864ac1
CES
12532 return NULL;
12533}
12534
4cb1265b
MS
12535char *
12536elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12537 const void *buf, int bufsiz)
12538{
12539 return elfcore_write_note (obfd, note_data, note_size,
12540 "CORE", NT_FILE, buf, bufsiz);
12541}
12542
0a1b45a2 12543static bool
276da9b3
L
12544elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12545 size_t align)
252b5132 12546{
c044fabd 12547 char *p;
252b5132 12548
276da9b3
L
12549 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12550 gABI specifies that PT_NOTE alignment should be aligned to 4
12551 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12552 align is less than 4, we use 4 byte alignment. */
12553 if (align < 4)
12554 align = 4;
ef135d43 12555 if (align != 4 && align != 8)
0a1b45a2 12556 return false;
276da9b3 12557
252b5132
RH
12558 p = buf;
12559 while (p < buf + size)
12560 {
c044fabd 12561 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
12562 Elf_Internal_Note in;
12563
baea7ef1 12564 if (offsetof (Elf_External_Note, name) > buf - p + size)
0a1b45a2 12565 return false;
baea7ef1 12566
dc810e39 12567 in.type = H_GET_32 (abfd, xnp->type);
252b5132 12568
dc810e39 12569 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 12570 in.namedata = xnp->name;
baea7ef1 12571 if (in.namesz > buf - in.namedata + size)
0a1b45a2 12572 return false;
252b5132 12573
dc810e39 12574 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 12575 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 12576 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
12577 if (in.descsz != 0
12578 && (in.descdata >= buf + size
12579 || in.descsz > buf - in.descdata + size))
0a1b45a2 12580 return false;
252b5132 12581
718175fa 12582 switch (bfd_get_format (abfd))
07d6d2b8 12583 {
718175fa 12584 default:
0a1b45a2 12585 return true;
718175fa
JK
12586
12587 case bfd_core:
f64e188b 12588 {
8acbedd6 12589#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 12590 struct
718175fa 12591 {
f64e188b 12592 const char * string;
8acbedd6 12593 size_t len;
0a1b45a2 12594 bool (*func) (bfd *, Elf_Internal_Note *);
718175fa 12595 }
f64e188b 12596 grokers[] =
b15fa79e 12597 {
8acbedd6 12598 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 12599 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6 12600 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
6420dd27 12601 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
8acbedd6 12602 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
864619bb 12603 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
6420dd27
LB
12604 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12605 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
f64e188b 12606 };
8acbedd6 12607#undef GROKER_ELEMENT
f64e188b
NC
12608 int i;
12609
12610 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
12611 {
12612 if (in.namesz >= grokers[i].len
12613 && strncmp (in.namedata, grokers[i].string,
12614 grokers[i].len) == 0)
12615 {
12616 if (! grokers[i].func (abfd, & in))
0a1b45a2 12617 return false;
8acbedd6
KS
12618 break;
12619 }
12620 }
f64e188b
NC
12621 break;
12622 }
718175fa
JK
12623
12624 case bfd_object:
12625 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12626 {
12627 if (! elfobj_grok_gnu_note (abfd, &in))
0a1b45a2 12628 return false;
718175fa 12629 }
e21e5835
NC
12630 else if (in.namesz == sizeof "stapsdt"
12631 && strcmp (in.namedata, "stapsdt") == 0)
12632 {
12633 if (! elfobj_grok_stapsdt_note (abfd, &in))
0a1b45a2 12634 return false;
e21e5835 12635 }
718175fa 12636 break;
08a40648 12637 }
252b5132 12638
276da9b3 12639 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
12640 }
12641
0a1b45a2 12642 return true;
718175fa
JK
12643}
12644
0a1b45a2 12645bool
276da9b3
L
12646elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12647 size_t align)
718175fa
JK
12648{
12649 char *buf;
12650
957e1fc1 12651 if (size == 0 || (size + 1) == 0)
0a1b45a2 12652 return true;
718175fa
JK
12653
12654 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
0a1b45a2 12655 return false;
718175fa 12656
2bb3687b 12657 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
718175fa 12658 if (buf == NULL)
0a1b45a2 12659 return false;
718175fa 12660
f64e188b
NC
12661 /* PR 17512: file: ec08f814
12662 0-termintate the buffer so that string searches will not overflow. */
12663 buf[size] = 0;
12664
2bb3687b 12665 if (!elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
12666 {
12667 free (buf);
0a1b45a2 12668 return false;
718175fa
JK
12669 }
12670
252b5132 12671 free (buf);
0a1b45a2 12672 return true;
252b5132 12673}
98d8431c
JB
12674\f
12675/* Providing external access to the ELF program header table. */
12676
12677/* Return an upper bound on the number of bytes required to store a
12678 copy of ABFD's program header table entries. Return -1 if an error
12679 occurs; bfd_get_error will return an appropriate code. */
c044fabd 12680
98d8431c 12681long
217aa764 12682bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
12683{
12684 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12685 {
12686 bfd_set_error (bfd_error_wrong_format);
12687 return -1;
12688 }
12689
936e320b 12690 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
12691}
12692
98d8431c
JB
12693/* Copy ABFD's program header table entries to *PHDRS. The entries
12694 will be stored as an array of Elf_Internal_Phdr structures, as
12695 defined in include/elf/internal.h. To find out how large the
12696 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12697
12698 Return the number of program header table entries read, or -1 if an
12699 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 12700
98d8431c 12701int
217aa764 12702bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
12703{
12704 int num_phdrs;
12705
12706 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12707 {
12708 bfd_set_error (bfd_error_wrong_format);
12709 return -1;
12710 }
12711
12712 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
12713 if (num_phdrs != 0)
12714 memcpy (phdrs, elf_tdata (abfd)->phdr,
12715 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
12716
12717 return num_phdrs;
12718}
ae4221d7 12719
db6751f2 12720enum elf_reloc_type_class
7e612e98
AM
12721_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12722 const asection *rel_sec ATTRIBUTE_UNUSED,
12723 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
12724{
12725 return reloc_class_normal;
12726}
f8df10f4 12727
47d9a591 12728/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
12729 relocation against a local symbol. */
12730
12731bfd_vma
217aa764
AM
12732_bfd_elf_rela_local_sym (bfd *abfd,
12733 Elf_Internal_Sym *sym,
8517fae7 12734 asection **psec,
217aa764 12735 Elf_Internal_Rela *rel)
f8df10f4 12736{
8517fae7 12737 asection *sec = *psec;
f8df10f4
JJ
12738 bfd_vma relocation;
12739
6835821b
AM
12740 relocation = (sec->output_section->vma
12741 + sec->output_offset
12742 + sym->st_value);
f8df10f4 12743 if ((sec->flags & SEC_MERGE)
c629eae0 12744 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 12745 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 12746 {
f8df10f4 12747 rel->r_addend =
8517fae7 12748 _bfd_merged_section_offset (abfd, psec,
65765700 12749 elf_section_data (sec)->sec_info,
753731ee
AM
12750 sym->st_value + rel->r_addend);
12751 if (sec != *psec)
12752 {
12753 /* If we have changed the section, and our original section is
12754 marked with SEC_EXCLUDE, it means that the original
12755 SEC_MERGE section has been completely subsumed in some
12756 other SEC_MERGE section. In this case, we need to leave
12757 some info around for --emit-relocs. */
12758 if ((sec->flags & SEC_EXCLUDE) != 0)
12759 sec->kept_section = *psec;
12760 sec = *psec;
12761 }
8517fae7
AM
12762 rel->r_addend -= relocation;
12763 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
12764 }
12765 return relocation;
12766}
c629eae0
JJ
12767
12768bfd_vma
217aa764
AM
12769_bfd_elf_rel_local_sym (bfd *abfd,
12770 Elf_Internal_Sym *sym,
12771 asection **psec,
12772 bfd_vma addend)
47d9a591 12773{
c629eae0
JJ
12774 asection *sec = *psec;
12775
6835821b 12776 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
12777 return sym->st_value + addend;
12778
12779 return _bfd_merged_section_offset (abfd, psec,
65765700 12780 elf_section_data (sec)->sec_info,
753731ee 12781 sym->st_value + addend);
c629eae0
JJ
12782}
12783
37b01f6a
DG
12784/* Adjust an address within a section. Given OFFSET within SEC, return
12785 the new offset within the section, based upon changes made to the
12786 section. Returns -1 if the offset is now invalid.
12787 The offset (in abnd out) is in target sized bytes, however big a
12788 byte may be. */
12789
c629eae0 12790bfd_vma
217aa764 12791_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12792 struct bfd_link_info *info,
217aa764
AM
12793 asection *sec,
12794 bfd_vma offset)
c629eae0 12795{
68bfbfcc 12796 switch (sec->sec_info_type)
65765700 12797 {
dbaa2011 12798 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12799 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12800 offset);
dbaa2011 12801 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12802 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12803
65765700 12804 default:
310fd250
L
12805 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12806 {
37b01f6a 12807 /* Reverse the offset. */
310fd250
L
12808 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12809 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12810
12811 /* address_size and sec->size are in octets. Convert
12812 to bytes before subtracting the original offset. */
61826503 12813 offset = ((sec->size - address_size)
bb294208 12814 / bfd_octets_per_byte (abfd, sec) - offset);
310fd250 12815 }
65765700
JJ
12816 return offset;
12817 }
c629eae0 12818}
3333a7c3
RM
12819\f
12820/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12821 reconstruct an ELF file by reading the segments out of remote memory
12822 based on the ELF file header at EHDR_VMA and the ELF program headers it
12823 points to. If not null, *LOADBASEP is filled in with the difference
12824 between the VMAs from which the segments were read, and the VMAs the
12825 file headers (and hence BFD's idea of each section's VMA) put them at.
12826
12827 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12828 remote memory at target address VMA into the local buffer at MYADDR; it
12829 should return zero on success or an `errno' code on failure. TEMPL must
12830 be a BFD for an ELF target with the word size and byte order found in
12831 the remote memory. */
12832
12833bfd *
217aa764
AM
12834bfd_elf_bfd_from_remote_memory
12835 (bfd *templ,
12836 bfd_vma ehdr_vma,
f0a5d95a 12837 bfd_size_type size,
217aa764 12838 bfd_vma *loadbasep,
fe78531d 12839 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12840{
12841 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12842 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12843}
4c45e5c9
JJ
12844\f
12845long
c9727e01
AM
12846_bfd_elf_get_synthetic_symtab (bfd *abfd,
12847 long symcount ATTRIBUTE_UNUSED,
12848 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12849 long dynsymcount,
c9727e01
AM
12850 asymbol **dynsyms,
12851 asymbol **ret)
4c45e5c9
JJ
12852{
12853 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12854 asection *relplt;
12855 asymbol *s;
12856 const char *relplt_name;
0a1b45a2 12857 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
4c45e5c9
JJ
12858 arelent *p;
12859 long count, i, n;
12860 size_t size;
12861 Elf_Internal_Shdr *hdr;
12862 char *names;
12863 asection *plt;
12864
8615f3f2
AM
12865 *ret = NULL;
12866
90e3cdf2
JJ
12867 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12868 return 0;
12869
8615f3f2
AM
12870 if (dynsymcount <= 0)
12871 return 0;
12872
4c45e5c9
JJ
12873 if (!bed->plt_sym_val)
12874 return 0;
12875
12876 relplt_name = bed->relplt_name;
12877 if (relplt_name == NULL)
d35fd659 12878 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12879 relplt = bfd_get_section_by_name (abfd, relplt_name);
12880 if (relplt == NULL)
12881 return 0;
12882
12883 hdr = &elf_section_data (relplt)->this_hdr;
12884 if (hdr->sh_link != elf_dynsymtab (abfd)
12885 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12886 return 0;
12887
12888 plt = bfd_get_section_by_name (abfd, ".plt");
12889 if (plt == NULL)
12890 return 0;
12891
12892 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 12893 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
4c45e5c9
JJ
12894 return -1;
12895
eea6121a 12896 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12897 size = count * sizeof (asymbol);
12898 p = relplt->relocation;
cb53bf42 12899 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12900 {
12901 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12902 if (p->addend != 0)
12903 {
12904#ifdef BFD64
12905 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12906#else
12907 size += sizeof ("+0x") - 1 + 8;
12908#endif
12909 }
12910 }
4c45e5c9 12911
a50b1753 12912 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12913 if (s == NULL)
12914 return -1;
12915
12916 names = (char *) (s + count);
12917 p = relplt->relocation;
12918 n = 0;
cb53bf42 12919 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12920 {
12921 size_t len;
12922 bfd_vma addr;
12923
12924 addr = bed->plt_sym_val (i, plt, p);
12925 if (addr == (bfd_vma) -1)
12926 continue;
12927
12928 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12929 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12930 we are defining a symbol, ensure one of them is set. */
12931 if ((s->flags & BSF_LOCAL) == 0)
12932 s->flags |= BSF_GLOBAL;
6ba2a415 12933 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12934 s->section = plt;
12935 s->value = addr - plt->vma;
12936 s->name = names;
8f39ba8e 12937 s->udata.p = NULL;
4c45e5c9
JJ
12938 len = strlen ((*p->sym_ptr_ptr)->name);
12939 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12940 names += len;
041de40d
AM
12941 if (p->addend != 0)
12942 {
1d770845 12943 char buf[30], *a;
d324f6d6 12944
041de40d
AM
12945 memcpy (names, "+0x", sizeof ("+0x") - 1);
12946 names += sizeof ("+0x") - 1;
1d770845
L
12947 bfd_sprintf_vma (abfd, buf, p->addend);
12948 for (a = buf; *a == '0'; ++a)
12949 ;
12950 len = strlen (a);
12951 memcpy (names, a, len);
12952 names += len;
041de40d 12953 }
4c45e5c9
JJ
12954 memcpy (names, "@plt", sizeof ("@plt"));
12955 names += sizeof ("@plt");
8f39ba8e 12956 ++s, ++n;
4c45e5c9
JJ
12957 }
12958
12959 return n;
12960}
3d7f7666 12961
821e6ff6
AM
12962/* It is only used by x86-64 so far.
12963 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12964 but current usage would allow all of _bfd_std_section to be zero. */
12965static const asymbol lcomm_sym
12966 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12967asection _bfd_elf_large_com_section
7eacd66b 12968 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12969 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12970
0a1b45a2 12971bool
cc364be6 12972_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12973{
12974 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12975
12976 i_ehdrp = elf_elfheader (abfd);
12977
06f44071
AM
12978 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12979 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12980
df3a023b 12981 /* Set the osabi field to ELFOSABI_GNU if the binary contains
99fabbc9
JL
12982 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
12983 or STB_GNU_UNIQUE binding. */
cc364be6
AM
12984 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12985 {
12986 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12987 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12988 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12989 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12990 {
12991 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
99fabbc9
JL
12992 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
12993 "and FreeBSD targets"));
cc364be6 12994 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
99fabbc9
JL
12995 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
12996 "only by GNU and FreeBSD targets"));
cc364be6 12997 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
99fabbc9
JL
12998 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
12999 "only by GNU and FreeBSD targets"));
13000 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13001 _bfd_error_handler (_("GNU_RETAIN section is supported "
13002 "only by GNU and FreeBSD targets"));
9aea1e31 13003 bfd_set_error (bfd_error_sorry);
0a1b45a2 13004 return false;
cc364be6
AM
13005 }
13006 }
0a1b45a2 13007 return true;
d1036acb 13008}
fcb93ecf
PB
13009
13010
13011/* Return TRUE for ELF symbol types that represent functions.
13012 This is the default version of this function, which is sufficient for
d8045f23 13013 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf 13014
0a1b45a2 13015bool
fcb93ecf
PB
13016_bfd_elf_is_function_type (unsigned int type)
13017{
d8045f23
NC
13018 return (type == STT_FUNC
13019 || type == STT_GNU_IFUNC);
fcb93ecf 13020}
9f296da3 13021
aef36ac1
AM
13022/* If the ELF symbol SYM might be a function in SEC, return the
13023 function size and set *CODE_OFF to the function's entry point,
13024 otherwise return zero. */
9f296da3 13025
aef36ac1
AM
13026bfd_size_type
13027_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13028 bfd_vma *code_off)
9f296da3 13029{
aef36ac1 13030 bfd_size_type size;
24aebc79 13031 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1 13032
ff9e0f5b 13033 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
13034 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13035 || sym->section != sec)
13036 return 0;
ff9e0f5b 13037
24aebc79
NC
13038 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13039
13040 /* In theory we should check that the symbol's type satisfies
13041 _bfd_elf_is_function_type(), but there are some function-like
13042 symbols which would fail this test. (eg _start). Instead
13043 we check for hidden, local, notype symbols with zero size.
13044 This type of symbol is generated by the annobin plugin for gcc
13045 and clang, and should not be considered to be a function symbol. */
13046 if (size == 0
13047 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13048 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13049 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13050 return 0;
13051
ff9e0f5b 13052 *code_off = sym->value;
24aebc79
NC
13053 /* Do not return 0 for the function's size. */
13054 return size ? size : 1;
9f296da3 13055}
a8e14f4c
NC
13056
13057/* Set to non-zero to enable some debug messages. */
13058#define DEBUG_SECONDARY_RELOCS 0
13059
13060/* An internal-to-the-bfd-library only section type
13061 used to indicate a cached secondary reloc section. */
13062#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13063
13064/* Create a BFD section to hold a secondary reloc section. */
13065
0a1b45a2 13066bool
a8e14f4c
NC
13067_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13068 Elf_Internal_Shdr *hdr,
13069 const char * name,
13070 unsigned int shindex)
13071{
13072 /* We only support RELA secondary relocs. */
13073 if (hdr->sh_type != SHT_RELA)
0a1b45a2 13074 return false;
a8e14f4c
NC
13075
13076#if DEBUG_SECONDARY_RELOCS
13077 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13078#endif
13079 hdr->sh_type = SHT_SECONDARY_RELOC;
13080 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13081}
13082
13083/* Read in any secondary relocs associated with SEC. */
13084
0a1b45a2 13085bool
f60742b2
NC
13086_bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13087 asection * sec,
13088 asymbol ** symbols,
0a1b45a2 13089 bool dynamic)
a8e14f4c
NC
13090{
13091 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13092 asection * relsec;
0a1b45a2 13093 bool result = true;
a8e14f4c
NC
13094 bfd_vma (*r_sym) (bfd_vma);
13095
13096#if BFD_DEFAULT_TARGET_SIZE > 32
13097 if (bfd_arch_bits_per_address (abfd) != 32)
13098 r_sym = elf64_r_sym;
13099 else
13100#endif
13101 r_sym = elf32_r_sym;
13102
956ea65c
MM
13103 if (!elf_section_data (sec)->has_secondary_relocs)
13104 return true;
13105
a8e14f4c
NC
13106 /* Discover if there are any secondary reloc sections
13107 associated with SEC. */
13108 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13109 {
13110 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13111
13112 if (hdr->sh_type == SHT_SECONDARY_RELOC
8642dafa
AM
13113 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13114 && (hdr->sh_entsize == ebd->s->sizeof_rel
13115 || hdr->sh_entsize == ebd->s->sizeof_rela))
a8e14f4c
NC
13116 {
13117 bfd_byte * native_relocs;
13118 bfd_byte * native_reloc;
13119 arelent * internal_relocs;
13120 arelent * internal_reloc;
13121 unsigned int i;
13122 unsigned int entsize;
13123 unsigned int symcount;
13124 unsigned int reloc_count;
13125 size_t amt;
13126
13127 if (ebd->elf_info_to_howto == NULL)
0a1b45a2 13128 return false;
a8e14f4c
NC
13129
13130#if DEBUG_SECONDARY_RELOCS
13131 fprintf (stderr, "read secondary relocs for %s from %s\n",
13132 sec->name, relsec->name);
13133#endif
13134 entsize = hdr->sh_entsize;
13135
13136 native_relocs = bfd_malloc (hdr->sh_size);
13137 if (native_relocs == NULL)
13138 {
0a1b45a2 13139 result = false;
a8e14f4c
NC
13140 continue;
13141 }
13142
13143 reloc_count = NUM_SHDR_ENTRIES (hdr);
13144 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13145 {
ecbbbdba 13146 free (native_relocs);
a8e14f4c 13147 bfd_set_error (bfd_error_file_too_big);
0a1b45a2 13148 result = false;
a8e14f4c
NC
13149 continue;
13150 }
13151
13152 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13153 if (internal_relocs == NULL)
13154 {
13155 free (native_relocs);
0a1b45a2 13156 result = false;
a8e14f4c
NC
13157 continue;
13158 }
13159
13160 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13161 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13162 != hdr->sh_size))
13163 {
13164 free (native_relocs);
ecbbbdba
NC
13165 /* The internal_relocs will be freed when
13166 the memory for the bfd is released. */
0a1b45a2 13167 result = false;
a8e14f4c
NC
13168 continue;
13169 }
13170
f60742b2
NC
13171 if (dynamic)
13172 symcount = bfd_get_dynamic_symcount (abfd);
13173 else
13174 symcount = bfd_get_symcount (abfd);
a8e14f4c
NC
13175
13176 for (i = 0, internal_reloc = internal_relocs,
13177 native_reloc = native_relocs;
13178 i < reloc_count;
13179 i++, internal_reloc++, native_reloc += entsize)
13180 {
0a1b45a2 13181 bool res;
a8e14f4c
NC
13182 Elf_Internal_Rela rela;
13183
8ee54925
NC
13184 if (entsize == ebd->s->sizeof_rel)
13185 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13186 else /* entsize == ebd->s->sizeof_rela */
13187 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
a8e14f4c
NC
13188
13189 /* The address of an ELF reloc is section relative for an object
13190 file, and absolute for an executable file or shared library.
13191 The address of a normal BFD reloc is always section relative,
13192 and the address of a dynamic reloc is absolute.. */
13193 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13194 internal_reloc->address = rela.r_offset;
13195 else
13196 internal_reloc->address = rela.r_offset - sec->vma;
13197
13198 if (r_sym (rela.r_info) == STN_UNDEF)
13199 {
13200 /* FIXME: This and the error case below mean that we
13201 have a symbol on relocs that is not elf_symbol_type. */
13202 internal_reloc->sym_ptr_ptr =
13203 bfd_abs_section_ptr->symbol_ptr_ptr;
13204 }
13205 else if (r_sym (rela.r_info) > symcount)
13206 {
13207 _bfd_error_handler
13208 /* xgettext:c-format */
13209 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
13210 abfd, sec, i, (long) r_sym (rela.r_info));
13211 bfd_set_error (bfd_error_bad_value);
13212 internal_reloc->sym_ptr_ptr =
13213 bfd_abs_section_ptr->symbol_ptr_ptr;
0a1b45a2 13214 result = false;
a8e14f4c
NC
13215 }
13216 else
13217 {
13218 asymbol **ps;
13219
13220 ps = symbols + r_sym (rela.r_info) - 1;
a8e14f4c
NC
13221 internal_reloc->sym_ptr_ptr = ps;
13222 /* Make sure that this symbol is not removed by strip. */
13223 (*ps)->flags |= BSF_KEEP;
13224 }
13225
13226 internal_reloc->addend = rela.r_addend;
13227
13228 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13229 if (! res || internal_reloc->howto == NULL)
13230 {
13231#if DEBUG_SECONDARY_RELOCS
13232 fprintf (stderr, "there is no howto associated with reloc %lx\n",
13233 rela.r_info);
13234#endif
0a1b45a2 13235 result = false;
a8e14f4c
NC
13236 }
13237 }
13238
13239 free (native_relocs);
13240 /* Store the internal relocs. */
13241 elf_section_data (relsec)->sec_info = internal_relocs;
13242 }
13243 }
13244
13245 return result;
13246}
13247
13248/* Set the ELF section header fields of an output secondary reloc section. */
13249
0a1b45a2 13250bool
a8e14f4c
NC
13251_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
13252 bfd * obfd ATTRIBUTE_UNUSED,
13253 const Elf_Internal_Shdr * isection,
13254 Elf_Internal_Shdr * osection)
13255{
13256 asection * isec;
13257 asection * osec;
44466e45 13258 struct bfd_elf_section_data * esd;
a8e14f4c
NC
13259
13260 if (isection == NULL)
0a1b45a2 13261 return false;
a8e14f4c
NC
13262
13263 if (isection->sh_type != SHT_SECONDARY_RELOC)
0a1b45a2 13264 return true;
a8e14f4c
NC
13265
13266 isec = isection->bfd_section;
13267 if (isec == NULL)
0a1b45a2 13268 return false;
a8e14f4c
NC
13269
13270 osec = osection->bfd_section;
13271 if (osec == NULL)
0a1b45a2 13272 return false;
a8e14f4c 13273
44466e45
NC
13274 esd = elf_section_data (osec);
13275 BFD_ASSERT (esd->sec_info == NULL);
13276 esd->sec_info = elf_section_data (isec)->sec_info;
a8e14f4c
NC
13277 osection->sh_type = SHT_RELA;
13278 osection->sh_link = elf_onesymtab (obfd);
13279 if (osection->sh_link == 0)
13280 {
13281 /* There is no symbol table - we are hosed... */
13282 _bfd_error_handler
13283 /* xgettext:c-format */
13284 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
13285 obfd, osec);
13286 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13287 return false;
a8e14f4c
NC
13288 }
13289
13290 /* Find the output section that corresponds to the isection's sh_info link. */
327ef784
NC
13291 if (isection->sh_info == 0
13292 || isection->sh_info >= elf_numsections (ibfd))
13293 {
13294 _bfd_error_handler
13295 /* xgettext:c-format */
13296 (_("%pB(%pA): info section index is invalid"),
13297 obfd, osec);
13298 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13299 return false;
327ef784
NC
13300 }
13301
a8e14f4c
NC
13302 isection = elf_elfsections (ibfd)[isection->sh_info];
13303
327ef784
NC
13304 if (isection == NULL
13305 || isection->bfd_section == NULL
13306 || isection->bfd_section->output_section == NULL)
13307 {
13308 _bfd_error_handler
13309 /* xgettext:c-format */
13310 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
13311 obfd, osec);
13312 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13313 return false;
327ef784
NC
13314 }
13315
44466e45
NC
13316 esd = elf_section_data (isection->bfd_section->output_section);
13317 BFD_ASSERT (esd != NULL);
13318 osection->sh_info = esd->this_idx;
0a1b45a2 13319 esd->has_secondary_relocs = true;
a8e14f4c
NC
13320#if DEBUG_SECONDARY_RELOCS
13321 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13322 osec->name, osection->sh_link, osection->sh_info);
44466e45
NC
13323 fprintf (stderr, "mark section %s as having secondary relocs\n",
13324 bfd_section_name (isection->bfd_section->output_section));
a8e14f4c
NC
13325#endif
13326
0a1b45a2 13327 return true;
a8e14f4c
NC
13328}
13329
44466e45
NC
13330/* Write out a secondary reloc section.
13331
13332 FIXME: Currently this function can result in a serious performance penalty
13333 for files with secondary relocs and lots of sections. The proper way to
13334 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13335 relocs together and then to have this function just walk that chain. */
a8e14f4c 13336
0a1b45a2 13337bool
a8e14f4c
NC
13338_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13339{
13340 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13341 bfd_vma addr_offset;
13342 asection * relsec;
13343 bfd_vma (*r_info) (bfd_vma, bfd_vma);
0a1b45a2 13344 bool result = true;
ac4bf06c
NC
13345
13346 if (sec == NULL)
0a1b45a2 13347 return false;
a8e14f4c
NC
13348
13349#if BFD_DEFAULT_TARGET_SIZE > 32
13350 if (bfd_arch_bits_per_address (abfd) != 32)
13351 r_info = elf64_r_info;
13352 else
13353#endif
13354 r_info = elf32_r_info;
13355
a8e14f4c
NC
13356 /* The address of an ELF reloc is section relative for an object
13357 file, and absolute for an executable file or shared library.
13358 The address of a BFD reloc is always section relative. */
13359 addr_offset = 0;
13360 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13361 addr_offset = sec->vma;
13362
13363 /* Discover if there are any secondary reloc sections
13364 associated with SEC. */
13365 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13366 {
13367 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13368 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13369
13370 if (hdr->sh_type == SHT_RELA
13371 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13372 {
13373 asymbol * last_sym;
13374 int last_sym_idx;
13375 unsigned int reloc_count;
13376 unsigned int idx;
8ee54925 13377 unsigned int entsize;
a8e14f4c
NC
13378 arelent * src_irel;
13379 bfd_byte * dst_rela;
13380
ac4bf06c
NC
13381 if (hdr->contents != NULL)
13382 {
13383 _bfd_error_handler
13384 /* xgettext:c-format */
13385 (_("%pB(%pA): error: secondary reloc section processed twice"),
13386 abfd, relsec);
13387 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13388 result = false;
ac4bf06c
NC
13389 continue;
13390 }
a8e14f4c 13391
8ee54925
NC
13392 entsize = hdr->sh_entsize;
13393 if (entsize == 0)
ac267c75
NC
13394 {
13395 _bfd_error_handler
13396 /* xgettext:c-format */
13397 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
13398 abfd, relsec);
13399 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13400 result = false;
ac267c75
NC
13401 continue;
13402 }
8ee54925
NC
13403 else if (entsize != ebd->s->sizeof_rel
13404 && entsize != ebd->s->sizeof_rela)
13405 {
13406 _bfd_error_handler
13407 /* xgettext:c-format */
13408 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
13409 abfd, relsec);
13410 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13411 result = false;
8ee54925
NC
13412 continue;
13413 }
ac267c75 13414
8ee54925 13415 reloc_count = hdr->sh_size / entsize;
ac4bf06c
NC
13416 if (reloc_count <= 0)
13417 {
13418 _bfd_error_handler
13419 /* xgettext:c-format */
13420 (_("%pB(%pA): error: secondary reloc section is empty!"),
13421 abfd, relsec);
13422 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13423 result = false;
ac4bf06c
NC
13424 continue;
13425 }
a8e14f4c
NC
13426
13427 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13428 if (hdr->contents == NULL)
13429 continue;
13430
13431#if DEBUG_SECONDARY_RELOCS
13432 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13433 reloc_count, sec->name, relsec->name);
13434#endif
13435 last_sym = NULL;
13436 last_sym_idx = 0;
13437 dst_rela = hdr->contents;
13438 src_irel = (arelent *) esd->sec_info;
ac4bf06c
NC
13439 if (src_irel == NULL)
13440 {
13441 _bfd_error_handler
13442 /* xgettext:c-format */
13443 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
13444 abfd, relsec);
13445 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13446 result = false;
ac4bf06c
NC
13447 continue;
13448 }
a8e14f4c 13449
8ee54925 13450 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
a8e14f4c
NC
13451 {
13452 Elf_Internal_Rela src_rela;
13453 arelent *ptr;
13454 asymbol *sym;
13455 int n;
13456
13457 ptr = src_irel + idx;
ac4bf06c
NC
13458 if (ptr == NULL)
13459 {
13460 _bfd_error_handler
13461 /* xgettext:c-format */
13462 (_("%pB(%pA): error: reloc table entry %u is empty"),
13463 abfd, relsec, idx);
13464 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13465 result = false;
ac4bf06c
NC
13466 break;
13467 }
a8e14f4c 13468
ac4bf06c
NC
13469 if (ptr->sym_ptr_ptr == NULL)
13470 {
13471 /* FIXME: Is this an error ? */
13472 n = 0;
13473 }
a8e14f4c
NC
13474 else
13475 {
ac4bf06c
NC
13476 sym = *ptr->sym_ptr_ptr;
13477
13478 if (sym == last_sym)
13479 n = last_sym_idx;
13480 else
a8e14f4c 13481 {
ac4bf06c
NC
13482 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13483 if (n < 0)
13484 {
13485 _bfd_error_handler
13486 /* xgettext:c-format */
13487 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
13488 abfd, relsec, idx);
13489 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13490 result = false;
ac4bf06c
NC
13491 n = 0;
13492 }
13493
13494 last_sym = sym;
13495 last_sym_idx = n;
a8e14f4c 13496 }
a8e14f4c 13497
ac4bf06c
NC
13498 if (sym->the_bfd != NULL
13499 && sym->the_bfd->xvec != abfd->xvec
13500 && ! _bfd_elf_validate_reloc (abfd, ptr))
13501 {
13502 _bfd_error_handler
13503 /* xgettext:c-format */
13504 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
13505 abfd, relsec, idx);
13506 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13507 result = false;
ac4bf06c
NC
13508 n = 0;
13509 }
a8e14f4c
NC
13510 }
13511
ac4bf06c 13512 src_rela.r_offset = ptr->address + addr_offset;
a8e14f4c
NC
13513 if (ptr->howto == NULL)
13514 {
ac4bf06c
NC
13515 _bfd_error_handler
13516 /* xgettext:c-format */
13517 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
13518 abfd, relsec, idx);
13519 bfd_set_error (bfd_error_bad_value);
0a1b45a2 13520 result = false;
ac4bf06c 13521 src_rela.r_info = r_info (0, 0);
a8e14f4c 13522 }
ac4bf06c
NC
13523 else
13524 src_rela.r_info = r_info (n, ptr->howto->type);
a8e14f4c 13525 src_rela.r_addend = ptr->addend;
8ee54925
NC
13526
13527 if (entsize == ebd->s->sizeof_rel)
13528 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13529 else /* entsize == ebd->s->sizeof_rela */
13530 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
a8e14f4c
NC
13531 }
13532 }
13533 }
13534
ac4bf06c 13535 return result;
a8e14f4c 13536}