x86: move fetch error handling into a helper function
[binutils-gdb.git] / bfd / elf.c
blobfa7c25ad9dc98dc6322934e3a86735bc24fecef7
1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2023 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
24 SECTION
25 ELF backends
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).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "sysdep.h"
38 #include <limits.h>
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
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. */
63 /* Swap in a Verdef structure. */
65 void
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
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);
79 /* Swap out a Verdef structure. */
81 void
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
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);
95 /* Swap in a Verdaux structure. */
97 void
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
106 /* Swap out a Verdaux structure. */
108 void
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
117 /* Swap in a Verneed structure. */
119 void
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
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);
131 /* Swap out a Verneed structure. */
133 void
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
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);
145 /* Swap in a Vernaux structure. */
147 void
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
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);
159 /* Swap out a Vernaux structure. */
161 void
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
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);
173 /* Swap in a Versym structure. */
175 void
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
183 /* Swap out a Versym structure. */
185 void
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
196 unsigned long
197 bfd_elf_hash (const char *namearg)
199 uint32_t h = 0;
201 for (const unsigned char *name = (const unsigned char *) namearg;
202 *name; name++)
204 h = (h << 4) + *name;
205 h ^= (h >> 24) & 0xf0;
207 return h & 0x0fffffff;
210 /* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
213 unsigned long
214 bfd_elf_gnu_hash (const char *namearg)
216 uint32_t h = 5381;
218 for (const unsigned char *name = (const unsigned char *) namearg;
219 *name; name++)
220 h = (h << 5) + h + *name;
221 return h;
224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
226 bool
227 bfd_elf_allocate_object (bfd *abfd,
228 size_t object_size,
229 enum elf_target_id object_id)
231 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232 abfd->tdata.any = bfd_zalloc (abfd, object_size);
233 if (abfd->tdata.any == NULL)
234 return false;
236 elf_object_id (abfd) = object_id;
237 if (abfd->direction != read_direction)
239 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
240 if (o == NULL)
241 return false;
242 elf_tdata (abfd)->o = o;
243 elf_program_header_size (abfd) = (bfd_size_type) -1;
245 return true;
249 bool
250 bfd_elf_make_object (bfd *abfd)
252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
254 bed->target_id);
257 bool
258 bfd_elf_mkcorefile (bfd *abfd)
260 /* I think this can be done just like an object file. */
261 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
262 return false;
263 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
264 return elf_tdata (abfd)->core != NULL;
267 char *
268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
270 Elf_Internal_Shdr **i_shdrp;
271 bfd_byte *shstrtab = NULL;
272 file_ptr offset;
273 bfd_size_type shstrtabsize;
275 i_shdrp = elf_elfsections (abfd);
276 if (i_shdrp == 0
277 || shindex >= elf_numsections (abfd)
278 || i_shdrp[shindex] == 0)
279 return NULL;
281 shstrtab = i_shdrp[shindex]->contents;
282 if (shstrtab == NULL)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset = i_shdrp[shindex]->sh_offset;
286 shstrtabsize = i_shdrp[shindex]->sh_size;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize + 1 <= 1
291 || bfd_seek (abfd, offset, SEEK_SET) != 0
292 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
293 shstrtabsize)) == NULL)
295 /* Once we've failed to read it, make sure we don't keep
296 trying. Otherwise, we'll keep allocating space for
297 the string table over and over. */
298 i_shdrp[shindex]->sh_size = 0;
300 else
301 shstrtab[shstrtabsize] = '\0';
302 i_shdrp[shindex]->contents = shstrtab;
304 return (char *) shstrtab;
307 char *
308 bfd_elf_string_from_elf_section (bfd *abfd,
309 unsigned int shindex,
310 unsigned int strindex)
312 Elf_Internal_Shdr *hdr;
314 if (strindex == 0)
315 return "";
317 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 return NULL;
320 hdr = elf_elfsections (abfd)[shindex];
322 if (hdr->contents == NULL)
324 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
326 /* PR 17512: file: f057ec89. */
327 /* xgettext:c-format */
328 _bfd_error_handler (_("%pB: attempt to load strings from"
329 " a non-string section (number %d)"),
330 abfd, shindex);
331 return NULL;
334 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335 return NULL;
337 else
339 /* PR 24273: The string section's contents may have already
340 been loaded elsewhere, eg because a corrupt file has the
341 string section index in the ELF header pointing at a group
342 section. So be paranoid, and test that the last byte of
343 the section is zero. */
344 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345 return NULL;
348 if (strindex >= hdr->sh_size)
350 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351 _bfd_error_handler
352 /* xgettext:c-format */
353 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354 abfd, strindex, (uint64_t) hdr->sh_size,
355 (shindex == shstrndx && strindex == hdr->sh_name
356 ? ".shstrtab"
357 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358 return NULL;
361 return ((char *) hdr->contents) + strindex;
364 /* Read and convert symbols to internal format.
365 SYMCOUNT specifies the number of symbols to read, starting from
366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 are non-NULL, they are used to store the internal symbols, external
368 symbols, and symbol section index extensions, respectively.
369 Returns a pointer to the internal symbol buffer (malloced if necessary)
370 or NULL if there were no symbols or some kind of problem. */
372 Elf_Internal_Sym *
373 bfd_elf_get_elf_syms (bfd *ibfd,
374 Elf_Internal_Shdr *symtab_hdr,
375 size_t symcount,
376 size_t symoffset,
377 Elf_Internal_Sym *intsym_buf,
378 void *extsym_buf,
379 Elf_External_Sym_Shndx *extshndx_buf)
381 Elf_Internal_Shdr *shndx_hdr;
382 void *alloc_ext;
383 const bfd_byte *esym;
384 Elf_External_Sym_Shndx *alloc_extshndx;
385 Elf_External_Sym_Shndx *shndx;
386 Elf_Internal_Sym *alloc_intsym;
387 Elf_Internal_Sym *isym;
388 Elf_Internal_Sym *isymend;
389 const struct elf_backend_data *bed;
390 size_t extsym_size;
391 size_t amt;
392 file_ptr pos;
394 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395 abort ();
397 if (symcount == 0)
398 return intsym_buf;
400 /* Normal syms might have section extension entries. */
401 shndx_hdr = NULL;
402 if (elf_symtab_shndx_list (ibfd) != NULL)
404 elf_section_list * entry;
405 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
407 /* Find an index section that is linked to this symtab section. */
408 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
410 /* PR 20063. */
411 if (entry->hdr.sh_link >= elf_numsections (ibfd))
412 continue;
414 if (sections[entry->hdr.sh_link] == symtab_hdr)
416 shndx_hdr = & entry->hdr;
417 break;
421 if (shndx_hdr == NULL)
423 if (symtab_hdr == &elf_symtab_hdr (ibfd))
424 /* Not really accurate, but this was how the old code used
425 to work. */
426 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
427 /* Otherwise we do nothing. The assumption is that
428 the index table will not be needed. */
432 /* Read the symbols. */
433 alloc_ext = NULL;
434 alloc_extshndx = NULL;
435 alloc_intsym = NULL;
436 bed = get_elf_backend_data (ibfd);
437 extsym_size = bed->s->sizeof_sym;
438 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
440 bfd_set_error (bfd_error_file_too_big);
441 intsym_buf = NULL;
442 goto out;
444 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
445 if (extsym_buf == NULL)
447 alloc_ext = bfd_malloc (amt);
448 extsym_buf = alloc_ext;
450 if (extsym_buf == NULL
451 || bfd_seek (ibfd, pos, SEEK_SET) != 0
452 || bfd_bread (extsym_buf, amt, ibfd) != amt)
454 intsym_buf = NULL;
455 goto out;
458 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
459 extshndx_buf = NULL;
460 else
462 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
464 bfd_set_error (bfd_error_file_too_big);
465 intsym_buf = NULL;
466 goto out;
468 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
469 if (extshndx_buf == NULL)
471 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
472 extshndx_buf = alloc_extshndx;
474 if (extshndx_buf == NULL
475 || bfd_seek (ibfd, pos, SEEK_SET) != 0
476 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
478 intsym_buf = NULL;
479 goto out;
483 if (intsym_buf == NULL)
485 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
487 bfd_set_error (bfd_error_file_too_big);
488 goto out;
490 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
491 intsym_buf = alloc_intsym;
492 if (intsym_buf == NULL)
493 goto out;
496 /* Convert the symbols to internal form. */
497 isymend = intsym_buf + symcount;
498 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
499 shndx = extshndx_buf;
500 isym < isymend;
501 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
502 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
504 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
505 /* xgettext:c-format */
506 _bfd_error_handler (_("%pB symbol number %lu references"
507 " nonexistent SHT_SYMTAB_SHNDX section"),
508 ibfd, (unsigned long) symoffset);
509 free (alloc_intsym);
510 intsym_buf = NULL;
511 goto out;
514 out:
515 free (alloc_ext);
516 free (alloc_extshndx);
518 return intsym_buf;
521 /* Look up a symbol name. */
522 const char *
523 bfd_elf_sym_name (bfd *abfd,
524 Elf_Internal_Shdr *symtab_hdr,
525 Elf_Internal_Sym *isym,
526 asection *sym_sec)
528 const char *name;
529 unsigned int iname = isym->st_name;
530 unsigned int shindex = symtab_hdr->sh_link;
532 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
533 /* Check for a bogus st_shndx to avoid crashing. */
534 && isym->st_shndx < elf_numsections (abfd))
536 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
537 shindex = elf_elfheader (abfd)->e_shstrndx;
540 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
541 if (name == NULL)
542 name = "(null)";
543 else if (sym_sec && *name == '\0')
544 name = bfd_section_name (sym_sec);
546 return name;
549 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
550 sections. The first element is the flags, the rest are section
551 pointers. */
553 typedef union elf_internal_group {
554 Elf_Internal_Shdr *shdr;
555 unsigned int flags;
556 } Elf_Internal_Group;
558 /* Return the name of the group signature symbol. Why isn't the
559 signature just a string? */
561 static const char *
562 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
564 Elf_Internal_Shdr *hdr;
565 unsigned char esym[sizeof (Elf64_External_Sym)];
566 Elf_External_Sym_Shndx eshndx;
567 Elf_Internal_Sym isym;
569 /* First we need to ensure the symbol table is available. Make sure
570 that it is a symbol table section. */
571 if (ghdr->sh_link >= elf_numsections (abfd))
572 return NULL;
573 hdr = elf_elfsections (abfd) [ghdr->sh_link];
574 if (hdr->sh_type != SHT_SYMTAB
575 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
576 return NULL;
578 /* Go read the symbol. */
579 hdr = &elf_tdata (abfd)->symtab_hdr;
580 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
581 &isym, esym, &eshndx) == NULL)
582 return NULL;
584 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
587 /* Set next_in_group list pointer, and group name for NEWSECT. */
589 static bool
590 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
592 unsigned int num_group = elf_tdata (abfd)->num_group;
594 /* If num_group is zero, read in all SHT_GROUP sections. The count
595 is set to -1 if there are no SHT_GROUP sections. */
596 if (num_group == 0)
598 unsigned int i, shnum;
600 /* First count the number of groups. If we have a SHT_GROUP
601 section with just a flag word (ie. sh_size is 4), ignore it. */
602 shnum = elf_numsections (abfd);
603 num_group = 0;
605 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
606 ( (shdr)->sh_type == SHT_GROUP \
607 && (shdr)->sh_size >= minsize \
608 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
609 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
611 for (i = 0; i < shnum; i++)
613 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
615 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
616 num_group += 1;
619 if (num_group == 0)
621 num_group = (unsigned) -1;
622 elf_tdata (abfd)->num_group = num_group;
623 elf_tdata (abfd)->group_sect_ptr = NULL;
625 else
627 /* We keep a list of elf section headers for group sections,
628 so we can find them quickly. */
629 size_t amt;
631 elf_tdata (abfd)->num_group = num_group;
632 amt = num_group * sizeof (Elf_Internal_Shdr *);
633 elf_tdata (abfd)->group_sect_ptr
634 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
635 if (elf_tdata (abfd)->group_sect_ptr == NULL)
636 return false;
637 num_group = 0;
639 for (i = 0; i < shnum; i++)
641 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
643 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
645 unsigned char *src;
646 Elf_Internal_Group *dest;
648 /* Make sure the group section has a BFD section
649 attached to it. */
650 if (!bfd_section_from_shdr (abfd, i))
651 return false;
653 /* Add to list of sections. */
654 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
655 num_group += 1;
657 /* Read the raw contents. */
658 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
659 shdr->contents = NULL;
660 if (_bfd_mul_overflow (shdr->sh_size,
661 sizeof (*dest) / 4, &amt)
662 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
663 || !(shdr->contents
664 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
666 _bfd_error_handler
667 /* xgettext:c-format */
668 (_("%pB: invalid size field in group section"
669 " header: %#" PRIx64 ""),
670 abfd, (uint64_t) shdr->sh_size);
671 bfd_set_error (bfd_error_bad_value);
672 -- num_group;
673 continue;
676 /* Translate raw contents, a flag word followed by an
677 array of elf section indices all in target byte order,
678 to the flag word followed by an array of elf section
679 pointers. */
680 src = shdr->contents + shdr->sh_size;
681 dest = (Elf_Internal_Group *) (shdr->contents + amt);
683 while (1)
685 unsigned int idx;
687 src -= 4;
688 --dest;
689 idx = H_GET_32 (abfd, src);
690 if (src == shdr->contents)
692 dest->shdr = NULL;
693 dest->flags = idx;
694 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
695 shdr->bfd_section->flags
696 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
697 break;
699 if (idx < shnum)
701 dest->shdr = elf_elfsections (abfd)[idx];
702 /* PR binutils/23199: All sections in a
703 section group should be marked with
704 SHF_GROUP. But some tools generate
705 broken objects without SHF_GROUP. Fix
706 them up here. */
707 dest->shdr->sh_flags |= SHF_GROUP;
709 if (idx >= shnum
710 || dest->shdr->sh_type == SHT_GROUP)
712 _bfd_error_handler
713 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
714 abfd, i);
715 dest->shdr = NULL;
721 /* PR 17510: Corrupt binaries might contain invalid groups. */
722 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
724 elf_tdata (abfd)->num_group = num_group;
726 /* If all groups are invalid then fail. */
727 if (num_group == 0)
729 elf_tdata (abfd)->group_sect_ptr = NULL;
730 elf_tdata (abfd)->num_group = num_group = -1;
731 _bfd_error_handler
732 (_("%pB: no valid group sections found"), abfd);
733 bfd_set_error (bfd_error_bad_value);
739 if (num_group != (unsigned) -1)
741 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
742 unsigned int j;
744 for (j = 0; j < num_group; j++)
746 /* Begin search from previous found group. */
747 unsigned i = (j + search_offset) % num_group;
749 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
750 Elf_Internal_Group *idx;
751 bfd_size_type n_elt;
753 if (shdr == NULL)
754 continue;
756 idx = (Elf_Internal_Group *) shdr->contents;
757 if (idx == NULL || shdr->sh_size < 4)
759 /* See PR 21957 for a reproducer. */
760 /* xgettext:c-format */
761 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
762 abfd, shdr->bfd_section);
763 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
764 bfd_set_error (bfd_error_bad_value);
765 return false;
767 n_elt = shdr->sh_size / 4;
769 /* Look through this group's sections to see if current
770 section is a member. */
771 while (--n_elt != 0)
772 if ((++idx)->shdr == hdr)
774 asection *s = NULL;
776 /* We are a member of this group. Go looking through
777 other members to see if any others are linked via
778 next_in_group. */
779 idx = (Elf_Internal_Group *) shdr->contents;
780 n_elt = shdr->sh_size / 4;
781 while (--n_elt != 0)
782 if ((++idx)->shdr != NULL
783 && (s = idx->shdr->bfd_section) != NULL
784 && elf_next_in_group (s) != NULL)
785 break;
786 if (n_elt != 0)
788 /* Snarf the group name from other member, and
789 insert current section in circular list. */
790 elf_group_name (newsect) = elf_group_name (s);
791 elf_next_in_group (newsect) = elf_next_in_group (s);
792 elf_next_in_group (s) = newsect;
794 else
796 const char *gname;
798 gname = group_signature (abfd, shdr);
799 if (gname == NULL)
800 return false;
801 elf_group_name (newsect) = gname;
803 /* Start a circular list with one element. */
804 elf_next_in_group (newsect) = newsect;
807 /* If the group section has been created, point to the
808 new member. */
809 if (shdr->bfd_section != NULL)
810 elf_next_in_group (shdr->bfd_section) = newsect;
812 elf_tdata (abfd)->group_search_offset = i;
813 j = num_group - 1;
814 break;
819 if (elf_group_name (newsect) == NULL)
821 /* xgettext:c-format */
822 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
823 abfd, newsect);
824 /* PR 29532: Return true here, even though the group info has not been
825 read. Separate debug info files can have empty group sections, but
826 we do not want this to prevent them from being loaded as otherwise
827 GDB will not be able to use them. */
828 return true;
830 return true;
833 bool
834 _bfd_elf_setup_sections (bfd *abfd)
836 unsigned int i;
837 unsigned int num_group = elf_tdata (abfd)->num_group;
838 bool result = true;
839 asection *s;
841 /* Process SHF_LINK_ORDER. */
842 for (s = abfd->sections; s != NULL; s = s->next)
844 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
845 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
847 unsigned int elfsec = this_hdr->sh_link;
848 /* An sh_link value of 0 is now allowed. It indicates that linked
849 to section has already been discarded, but that the current
850 section has been retained for some other reason. This linking
851 section is still a candidate for later garbage collection
852 however. */
853 if (elfsec == 0)
855 elf_linked_to_section (s) = NULL;
857 else
859 asection *linksec = NULL;
861 if (elfsec < elf_numsections (abfd))
863 this_hdr = elf_elfsections (abfd)[elfsec];
864 linksec = this_hdr->bfd_section;
867 /* PR 1991, 2008:
868 Some strip/objcopy may leave an incorrect value in
869 sh_link. We don't want to proceed. */
870 if (linksec == NULL)
872 _bfd_error_handler
873 /* xgettext:c-format */
874 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
875 s->owner, elfsec, s);
876 result = false;
879 elf_linked_to_section (s) = linksec;
882 else if (this_hdr->sh_type == SHT_GROUP
883 && elf_next_in_group (s) == NULL)
885 _bfd_error_handler
886 /* xgettext:c-format */
887 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
888 abfd, elf_section_data (s)->this_idx);
889 result = false;
893 /* Process section groups. */
894 if (num_group == (unsigned) -1)
895 return result;
897 for (i = 0; i < num_group; i++)
899 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
900 Elf_Internal_Group *idx;
901 unsigned int n_elt;
903 /* PR binutils/18758: Beware of corrupt binaries with invalid
904 group data. */
905 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
907 _bfd_error_handler
908 /* xgettext:c-format */
909 (_("%pB: section group entry number %u is corrupt"),
910 abfd, i);
911 result = false;
912 continue;
915 idx = (Elf_Internal_Group *) shdr->contents;
916 n_elt = shdr->sh_size / 4;
918 while (--n_elt != 0)
920 ++ idx;
922 if (idx->shdr == NULL)
923 continue;
924 else if (idx->shdr->bfd_section)
925 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
926 else if (idx->shdr->sh_type != SHT_RELA
927 && idx->shdr->sh_type != SHT_REL)
929 /* There are some unknown sections in the group. */
930 _bfd_error_handler
931 /* xgettext:c-format */
932 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
933 abfd,
934 idx->shdr->sh_type,
935 bfd_elf_string_from_elf_section (abfd,
936 (elf_elfheader (abfd)
937 ->e_shstrndx),
938 idx->shdr->sh_name),
939 shdr->bfd_section);
940 result = false;
945 return result;
948 bool
949 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
951 return elf_next_in_group (sec) != NULL;
954 const char *
955 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
957 if (elf_sec_group (sec) != NULL)
958 return elf_group_name (sec);
959 return NULL;
962 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
964 struct lto_section
966 int16_t major_version;
967 int16_t minor_version;
968 unsigned char slim_object;
970 /* Flags is a private field that is not defined publicly. */
971 uint16_t flags;
974 /* Make a BFD section from an ELF section. We store a pointer to the
975 BFD section in the bfd_section field of the header. */
977 bool
978 _bfd_elf_make_section_from_shdr (bfd *abfd,
979 Elf_Internal_Shdr *hdr,
980 const char *name,
981 int shindex)
983 asection *newsect;
984 flagword flags;
985 const struct elf_backend_data *bed;
986 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
988 if (hdr->bfd_section != NULL)
989 return true;
991 newsect = bfd_make_section_anyway (abfd, name);
992 if (newsect == NULL)
993 return false;
995 hdr->bfd_section = newsect;
996 elf_section_data (newsect)->this_hdr = *hdr;
997 elf_section_data (newsect)->this_idx = shindex;
999 /* Always use the real type/flags. */
1000 elf_section_type (newsect) = hdr->sh_type;
1001 elf_section_flags (newsect) = hdr->sh_flags;
1003 newsect->filepos = hdr->sh_offset;
1005 flags = SEC_NO_FLAGS;
1006 if (hdr->sh_type != SHT_NOBITS)
1007 flags |= SEC_HAS_CONTENTS;
1008 if (hdr->sh_type == SHT_GROUP)
1009 flags |= SEC_GROUP;
1010 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1012 flags |= SEC_ALLOC;
1013 if (hdr->sh_type != SHT_NOBITS)
1014 flags |= SEC_LOAD;
1016 if ((hdr->sh_flags & SHF_WRITE) == 0)
1017 flags |= SEC_READONLY;
1018 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1019 flags |= SEC_CODE;
1020 else if ((flags & SEC_LOAD) != 0)
1021 flags |= SEC_DATA;
1022 if ((hdr->sh_flags & SHF_MERGE) != 0)
1024 flags |= SEC_MERGE;
1025 newsect->entsize = hdr->sh_entsize;
1027 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1028 flags |= SEC_STRINGS;
1029 if (hdr->sh_flags & SHF_GROUP)
1030 if (!setup_group (abfd, hdr, newsect))
1031 return false;
1032 if ((hdr->sh_flags & SHF_TLS) != 0)
1033 flags |= SEC_THREAD_LOCAL;
1034 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1035 flags |= SEC_EXCLUDE;
1037 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1039 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1040 but binutils as of 2019-07-23 did not set the EI_OSABI header
1041 byte. */
1042 case ELFOSABI_GNU:
1043 case ELFOSABI_FREEBSD:
1044 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1045 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1046 /* Fall through */
1047 case ELFOSABI_NONE:
1048 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1049 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1050 break;
1053 if ((flags & SEC_ALLOC) == 0)
1055 /* The debugging sections appear to be recognized only by name,
1056 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1057 if (name [0] == '.')
1059 if (startswith (name, ".debug")
1060 || startswith (name, ".gnu.debuglto_.debug_")
1061 || startswith (name, ".gnu.linkonce.wi.")
1062 || startswith (name, ".zdebug"))
1063 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1064 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1065 || startswith (name, ".note.gnu"))
1067 flags |= SEC_ELF_OCTETS;
1068 opb = 1;
1070 else if (startswith (name, ".line")
1071 || startswith (name, ".stab")
1072 || strcmp (name, ".gdb_index") == 0)
1073 flags |= SEC_DEBUGGING;
1077 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1078 || !bfd_set_section_size (newsect, hdr->sh_size)
1079 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1080 & -hdr->sh_addralign)))
1081 return false;
1083 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1084 only link a single copy of the section. This is used to support
1085 g++. g++ will emit each template expansion in its own section.
1086 The symbols will be defined as weak, so that multiple definitions
1087 are permitted. The GNU linker extension is to actually discard
1088 all but one of the sections. */
1089 if (startswith (name, ".gnu.linkonce")
1090 && elf_next_in_group (newsect) == NULL)
1091 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1093 if (!bfd_set_section_flags (newsect, flags))
1094 return false;
1096 bed = get_elf_backend_data (abfd);
1097 if (bed->elf_backend_section_flags)
1098 if (!bed->elf_backend_section_flags (hdr))
1099 return false;
1101 /* We do not parse the PT_NOTE segments as we are interested even in the
1102 separate debug info files which may have the segments offsets corrupted.
1103 PT_NOTEs from the core files are currently not parsed using BFD. */
1104 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1106 bfd_byte *contents;
1108 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1109 return false;
1111 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1112 hdr->sh_offset, hdr->sh_addralign);
1113 free (contents);
1116 if ((newsect->flags & SEC_ALLOC) != 0)
1118 Elf_Internal_Phdr *phdr;
1119 unsigned int i, nload;
1121 /* Some ELF linkers produce binaries with all the program header
1122 p_paddr fields zero. If we have such a binary with more than
1123 one PT_LOAD header, then leave the section lma equal to vma
1124 so that we don't create sections with overlapping lma. */
1125 phdr = elf_tdata (abfd)->phdr;
1126 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1127 if (phdr->p_paddr != 0)
1128 break;
1129 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1130 ++nload;
1131 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1132 return true;
1134 phdr = elf_tdata (abfd)->phdr;
1135 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1137 if (((phdr->p_type == PT_LOAD
1138 && (hdr->sh_flags & SHF_TLS) == 0)
1139 || phdr->p_type == PT_TLS)
1140 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1142 if ((newsect->flags & SEC_LOAD) == 0)
1143 newsect->lma = (phdr->p_paddr
1144 + hdr->sh_addr - phdr->p_vaddr) / opb;
1145 else
1146 /* We used to use the same adjustment for SEC_LOAD
1147 sections, but that doesn't work if the segment
1148 is packed with code from multiple VMAs.
1149 Instead we calculate the section LMA based on
1150 the segment LMA. It is assumed that the
1151 segment will contain sections with contiguous
1152 LMAs, even if the VMAs are not. */
1153 newsect->lma = (phdr->p_paddr
1154 + hdr->sh_offset - phdr->p_offset) / opb;
1156 /* With contiguous segments, we can't tell from file
1157 offsets whether a section with zero size should
1158 be placed at the end of one segment or the
1159 beginning of the next. Decide based on vaddr. */
1160 if (hdr->sh_addr >= phdr->p_vaddr
1161 && (hdr->sh_addr + hdr->sh_size
1162 <= phdr->p_vaddr + phdr->p_memsz))
1163 break;
1168 /* Compress/decompress DWARF debug sections with names: .debug_*,
1169 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1170 if ((newsect->flags & SEC_DEBUGGING) != 0
1171 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1172 && (newsect->flags & SEC_ELF_OCTETS) != 0)
1174 enum { nothing, compress, decompress } action = nothing;
1175 int compression_header_size;
1176 bfd_size_type uncompressed_size;
1177 unsigned int uncompressed_align_power;
1178 enum compression_type ch_type = ch_none;
1179 bool compressed
1180 = bfd_is_section_compressed_info (abfd, newsect,
1181 &compression_header_size,
1182 &uncompressed_size,
1183 &uncompressed_align_power,
1184 &ch_type);
1186 /* Should we decompress? */
1187 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1188 action = decompress;
1190 /* Should we compress? Or convert to a different compression? */
1191 else if ((abfd->flags & BFD_COMPRESS) != 0
1192 && newsect->size != 0
1193 && compression_header_size >= 0
1194 && uncompressed_size > 0)
1196 if (!compressed)
1197 action = compress;
1198 else
1200 enum compression_type new_ch_type = ch_none;
1201 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1202 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1203 ? ch_compress_zstd : ch_compress_zlib);
1204 if (new_ch_type != ch_type)
1205 action = compress;
1209 if (action == compress)
1211 if (!bfd_init_section_compress_status (abfd, newsect))
1213 _bfd_error_handler
1214 /* xgettext:c-format */
1215 (_("%pB: unable to compress section %s"), abfd, name);
1216 return false;
1219 else if (action == decompress)
1221 if (!bfd_init_section_decompress_status (abfd, newsect))
1223 _bfd_error_handler
1224 /* xgettext:c-format */
1225 (_("%pB: unable to decompress section %s"), abfd, name);
1226 return false;
1228 #ifndef HAVE_ZSTD
1229 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1231 _bfd_error_handler
1232 /* xgettext:c-format */
1233 (_ ("%pB: section %s is compressed with zstd, but BFD "
1234 "is not built with zstd support"),
1235 abfd, name);
1236 newsect->compress_status = COMPRESS_SECTION_NONE;
1237 return false;
1239 #endif
1240 if (abfd->is_linker_input
1241 && name[1] == 'z')
1243 /* Rename section from .zdebug_* to .debug_* so that ld
1244 scripts will see this section as a debug section. */
1245 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1246 if (new_name == NULL)
1247 return false;
1248 bfd_rename_section (newsect, new_name);
1253 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1254 section. */
1255 if (startswith (name, ".gnu.lto_.lto."))
1257 struct lto_section lsection;
1258 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1259 sizeof (struct lto_section)))
1260 abfd->lto_slim_object = lsection.slim_object;
1263 return true;
1266 const char *const bfd_elf_section_type_names[] =
1268 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1269 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1270 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1273 /* ELF relocs are against symbols. If we are producing relocatable
1274 output, and the reloc is against an external symbol, and nothing
1275 has given us any additional addend, the resulting reloc will also
1276 be against the same symbol. In such a case, we don't want to
1277 change anything about the way the reloc is handled, since it will
1278 all be done at final link time. Rather than put special case code
1279 into bfd_perform_relocation, all the reloc types use this howto
1280 function, or should call this function for relocatable output. */
1282 bfd_reloc_status_type
1283 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1284 arelent *reloc_entry,
1285 asymbol *symbol,
1286 void *data ATTRIBUTE_UNUSED,
1287 asection *input_section,
1288 bfd *output_bfd,
1289 char **error_message ATTRIBUTE_UNUSED)
1291 if (output_bfd != NULL
1292 && (symbol->flags & BSF_SECTION_SYM) == 0
1293 && (! reloc_entry->howto->partial_inplace
1294 || reloc_entry->addend == 0))
1296 reloc_entry->address += input_section->output_offset;
1297 return bfd_reloc_ok;
1300 /* In some cases the relocation should be treated as output section
1301 relative, as when linking ELF DWARF into PE COFF. Many ELF
1302 targets lack section relative relocations and instead use
1303 ordinary absolute relocations for references between DWARF
1304 sections. That is arguably a bug in those targets but it happens
1305 to work for the usual case of linking to non-loaded ELF debug
1306 sections with VMAs forced to zero. PE COFF on the other hand
1307 doesn't allow a section VMA of zero. */
1308 if (output_bfd == NULL
1309 && !reloc_entry->howto->pc_relative
1310 && (symbol->section->flags & SEC_DEBUGGING) != 0
1311 && (input_section->flags & SEC_DEBUGGING) != 0)
1312 reloc_entry->addend -= symbol->section->output_section->vma;
1314 return bfd_reloc_continue;
1317 /* Returns TRUE if section A matches section B.
1318 Names, addresses and links may be different, but everything else
1319 should be the same. */
1321 static bool
1322 section_match (const Elf_Internal_Shdr * a,
1323 const Elf_Internal_Shdr * b)
1325 if (a->sh_type != b->sh_type
1326 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1327 || a->sh_addralign != b->sh_addralign
1328 || a->sh_entsize != b->sh_entsize)
1329 return false;
1330 if (a->sh_type == SHT_SYMTAB
1331 || a->sh_type == SHT_STRTAB)
1332 return true;
1333 return a->sh_size == b->sh_size;
1336 /* Find a section in OBFD that has the same characteristics
1337 as IHEADER. Return the index of this section or SHN_UNDEF if
1338 none can be found. Check's section HINT first, as this is likely
1339 to be the correct section. */
1341 static unsigned int
1342 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1343 const unsigned int hint)
1345 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1346 unsigned int i;
1348 BFD_ASSERT (iheader != NULL);
1350 /* See PR 20922 for a reproducer of the NULL test. */
1351 if (hint < elf_numsections (obfd)
1352 && oheaders[hint] != NULL
1353 && section_match (oheaders[hint], iheader))
1354 return hint;
1356 for (i = 1; i < elf_numsections (obfd); i++)
1358 Elf_Internal_Shdr * oheader = oheaders[i];
1360 if (oheader == NULL)
1361 continue;
1362 if (section_match (oheader, iheader))
1363 /* FIXME: Do we care if there is a potential for
1364 multiple matches ? */
1365 return i;
1368 return SHN_UNDEF;
1371 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1372 Processor specific section, based upon a matching input section.
1373 Returns TRUE upon success, FALSE otherwise. */
1375 static bool
1376 copy_special_section_fields (const bfd *ibfd,
1377 bfd *obfd,
1378 const Elf_Internal_Shdr *iheader,
1379 Elf_Internal_Shdr *oheader,
1380 const unsigned int secnum)
1382 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1383 const Elf_Internal_Shdr **iheaders
1384 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1385 bool changed = false;
1386 unsigned int sh_link;
1388 if (oheader->sh_type == SHT_NOBITS)
1390 /* This is a feature for objcopy --only-keep-debug:
1391 When a section's type is changed to NOBITS, we preserve
1392 the sh_link and sh_info fields so that they can be
1393 matched up with the original.
1395 Note: Strictly speaking these assignments are wrong.
1396 The sh_link and sh_info fields should point to the
1397 relevent sections in the output BFD, which may not be in
1398 the same location as they were in the input BFD. But
1399 the whole point of this action is to preserve the
1400 original values of the sh_link and sh_info fields, so
1401 that they can be matched up with the section headers in
1402 the original file. So strictly speaking we may be
1403 creating an invalid ELF file, but it is only for a file
1404 that just contains debug info and only for sections
1405 without any contents. */
1406 if (oheader->sh_link == 0)
1407 oheader->sh_link = iheader->sh_link;
1408 if (oheader->sh_info == 0)
1409 oheader->sh_info = iheader->sh_info;
1410 return true;
1413 /* Allow the target a chance to decide how these fields should be set. */
1414 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1415 iheader, oheader))
1416 return true;
1418 /* We have an iheader which might match oheader, and which has non-zero
1419 sh_info and/or sh_link fields. Attempt to follow those links and find
1420 the section in the output bfd which corresponds to the linked section
1421 in the input bfd. */
1422 if (iheader->sh_link != SHN_UNDEF)
1424 /* See PR 20931 for a reproducer. */
1425 if (iheader->sh_link >= elf_numsections (ibfd))
1427 _bfd_error_handler
1428 /* xgettext:c-format */
1429 (_("%pB: invalid sh_link field (%d) in section number %d"),
1430 ibfd, iheader->sh_link, secnum);
1431 return false;
1434 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1435 if (sh_link != SHN_UNDEF)
1437 oheader->sh_link = sh_link;
1438 changed = true;
1440 else
1441 /* FIXME: Should we install iheader->sh_link
1442 if we could not find a match ? */
1443 _bfd_error_handler
1444 /* xgettext:c-format */
1445 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1448 if (iheader->sh_info)
1450 /* The sh_info field can hold arbitrary information, but if the
1451 SHF_LINK_INFO flag is set then it should be interpreted as a
1452 section index. */
1453 if (iheader->sh_flags & SHF_INFO_LINK)
1455 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1456 iheader->sh_info);
1457 if (sh_link != SHN_UNDEF)
1458 oheader->sh_flags |= SHF_INFO_LINK;
1460 else
1461 /* No idea what it means - just copy it. */
1462 sh_link = iheader->sh_info;
1464 if (sh_link != SHN_UNDEF)
1466 oheader->sh_info = sh_link;
1467 changed = true;
1469 else
1470 _bfd_error_handler
1471 /* xgettext:c-format */
1472 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1475 return changed;
1478 /* Copy the program header and other data from one object module to
1479 another. */
1481 bool
1482 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1484 const Elf_Internal_Shdr **iheaders
1485 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1486 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1487 const struct elf_backend_data *bed;
1488 unsigned int i;
1490 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1491 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1492 return true;
1494 if (!elf_flags_init (obfd))
1496 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1497 elf_flags_init (obfd) = true;
1500 elf_gp (obfd) = elf_gp (ibfd);
1502 /* Also copy the EI_OSABI field. */
1503 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1504 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1506 /* If set, copy the EI_ABIVERSION field. */
1507 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1508 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1509 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1511 /* Copy object attributes. */
1512 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1514 if (iheaders == NULL || oheaders == NULL)
1515 return true;
1517 bed = get_elf_backend_data (obfd);
1519 /* Possibly copy other fields in the section header. */
1520 for (i = 1; i < elf_numsections (obfd); i++)
1522 unsigned int j;
1523 Elf_Internal_Shdr * oheader = oheaders[i];
1525 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1526 because of a special case need for generating separate debug info
1527 files. See below for more details. */
1528 if (oheader == NULL
1529 || (oheader->sh_type != SHT_NOBITS
1530 && oheader->sh_type < SHT_LOOS))
1531 continue;
1533 /* Ignore empty sections, and sections whose
1534 fields have already been initialised. */
1535 if (oheader->sh_size == 0
1536 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1537 continue;
1539 /* Scan for the matching section in the input bfd.
1540 First we try for a direct mapping between the input and
1541 output sections. */
1542 for (j = 1; j < elf_numsections (ibfd); j++)
1544 const Elf_Internal_Shdr * iheader = iheaders[j];
1546 if (iheader == NULL)
1547 continue;
1549 if (oheader->bfd_section != NULL
1550 && iheader->bfd_section != NULL
1551 && iheader->bfd_section->output_section != NULL
1552 && iheader->bfd_section->output_section == oheader->bfd_section)
1554 /* We have found a connection from the input section to
1555 the output section. Attempt to copy the header fields.
1556 If this fails then do not try any further sections -
1557 there should only be a one-to-one mapping between
1558 input and output. */
1559 if (!copy_special_section_fields (ibfd, obfd,
1560 iheader, oheader, i))
1561 j = elf_numsections (ibfd);
1562 break;
1566 if (j < elf_numsections (ibfd))
1567 continue;
1569 /* That failed. So try to deduce the corresponding input section.
1570 Unfortunately we cannot compare names as the output string table
1571 is empty, so instead we check size, address and type. */
1572 for (j = 1; j < elf_numsections (ibfd); j++)
1574 const Elf_Internal_Shdr * iheader = iheaders[j];
1576 if (iheader == NULL)
1577 continue;
1579 /* Try matching fields in the input section's header.
1580 Since --only-keep-debug turns all non-debug sections into
1581 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1582 input type. */
1583 if ((oheader->sh_type == SHT_NOBITS
1584 || iheader->sh_type == oheader->sh_type)
1585 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1586 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1587 && iheader->sh_addralign == oheader->sh_addralign
1588 && iheader->sh_entsize == oheader->sh_entsize
1589 && iheader->sh_size == oheader->sh_size
1590 && iheader->sh_addr == oheader->sh_addr
1591 && (iheader->sh_info != oheader->sh_info
1592 || iheader->sh_link != oheader->sh_link))
1594 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1595 break;
1599 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1601 /* Final attempt. Call the backend copy function
1602 with a NULL input section. */
1603 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1604 NULL, oheader);
1608 return true;
1611 static const char *
1612 get_segment_type (unsigned int p_type)
1614 const char *pt;
1615 switch (p_type)
1617 case PT_NULL: pt = "NULL"; break;
1618 case PT_LOAD: pt = "LOAD"; break;
1619 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1620 case PT_INTERP: pt = "INTERP"; break;
1621 case PT_NOTE: pt = "NOTE"; break;
1622 case PT_SHLIB: pt = "SHLIB"; break;
1623 case PT_PHDR: pt = "PHDR"; break;
1624 case PT_TLS: pt = "TLS"; break;
1625 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1626 case PT_GNU_STACK: pt = "STACK"; break;
1627 case PT_GNU_RELRO: pt = "RELRO"; break;
1628 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1629 default: pt = NULL; break;
1631 return pt;
1634 /* Print out the program headers. */
1636 bool
1637 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1639 FILE *f = (FILE *) farg;
1640 Elf_Internal_Phdr *p;
1641 asection *s;
1642 bfd_byte *dynbuf = NULL;
1644 p = elf_tdata (abfd)->phdr;
1645 if (p != NULL)
1647 unsigned int i, c;
1649 fprintf (f, _("\nProgram Header:\n"));
1650 c = elf_elfheader (abfd)->e_phnum;
1651 for (i = 0; i < c; i++, p++)
1653 const char *pt = get_segment_type (p->p_type);
1654 char buf[20];
1656 if (pt == NULL)
1658 sprintf (buf, "0x%lx", p->p_type);
1659 pt = buf;
1661 fprintf (f, "%8s off 0x", pt);
1662 bfd_fprintf_vma (abfd, f, p->p_offset);
1663 fprintf (f, " vaddr 0x");
1664 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1665 fprintf (f, " paddr 0x");
1666 bfd_fprintf_vma (abfd, f, p->p_paddr);
1667 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1668 fprintf (f, " filesz 0x");
1669 bfd_fprintf_vma (abfd, f, p->p_filesz);
1670 fprintf (f, " memsz 0x");
1671 bfd_fprintf_vma (abfd, f, p->p_memsz);
1672 fprintf (f, " flags %c%c%c",
1673 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1674 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1675 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1676 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1677 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1678 fprintf (f, "\n");
1682 s = bfd_get_section_by_name (abfd, ".dynamic");
1683 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1685 unsigned int elfsec;
1686 unsigned long shlink;
1687 bfd_byte *extdyn, *extdynend;
1688 size_t extdynsize;
1689 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1691 fprintf (f, _("\nDynamic Section:\n"));
1693 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1694 goto error_return;
1696 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1697 if (elfsec == SHN_BAD)
1698 goto error_return;
1699 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1701 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1702 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1704 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1705 (size_t) (extdynend - extdyn) >= extdynsize;
1706 extdyn += extdynsize)
1708 Elf_Internal_Dyn dyn;
1709 const char *name = "";
1710 char ab[20];
1711 bool stringp;
1712 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1714 (*swap_dyn_in) (abfd, extdyn, &dyn);
1716 if (dyn.d_tag == DT_NULL)
1717 break;
1719 stringp = false;
1720 switch (dyn.d_tag)
1722 default:
1723 if (bed->elf_backend_get_target_dtag)
1724 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1726 if (!strcmp (name, ""))
1728 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1729 name = ab;
1731 break;
1733 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1734 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1735 case DT_PLTGOT: name = "PLTGOT"; break;
1736 case DT_HASH: name = "HASH"; break;
1737 case DT_STRTAB: name = "STRTAB"; break;
1738 case DT_SYMTAB: name = "SYMTAB"; break;
1739 case DT_RELA: name = "RELA"; break;
1740 case DT_RELASZ: name = "RELASZ"; break;
1741 case DT_RELAENT: name = "RELAENT"; break;
1742 case DT_STRSZ: name = "STRSZ"; break;
1743 case DT_SYMENT: name = "SYMENT"; break;
1744 case DT_INIT: name = "INIT"; break;
1745 case DT_FINI: name = "FINI"; break;
1746 case DT_SONAME: name = "SONAME"; stringp = true; break;
1747 case DT_RPATH: name = "RPATH"; stringp = true; break;
1748 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1749 case DT_REL: name = "REL"; break;
1750 case DT_RELSZ: name = "RELSZ"; break;
1751 case DT_RELENT: name = "RELENT"; break;
1752 case DT_RELR: name = "RELR"; break;
1753 case DT_RELRSZ: name = "RELRSZ"; break;
1754 case DT_RELRENT: name = "RELRENT"; break;
1755 case DT_PLTREL: name = "PLTREL"; break;
1756 case DT_DEBUG: name = "DEBUG"; break;
1757 case DT_TEXTREL: name = "TEXTREL"; break;
1758 case DT_JMPREL: name = "JMPREL"; break;
1759 case DT_BIND_NOW: name = "BIND_NOW"; break;
1760 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1761 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1762 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1763 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1764 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1765 case DT_FLAGS: name = "FLAGS"; break;
1766 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1767 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1768 case DT_CHECKSUM: name = "CHECKSUM"; break;
1769 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1770 case DT_MOVEENT: name = "MOVEENT"; break;
1771 case DT_MOVESZ: name = "MOVESZ"; break;
1772 case DT_FEATURE: name = "FEATURE"; break;
1773 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1774 case DT_SYMINSZ: name = "SYMINSZ"; break;
1775 case DT_SYMINENT: name = "SYMINENT"; break;
1776 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1777 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1778 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1779 case DT_PLTPAD: name = "PLTPAD"; break;
1780 case DT_MOVETAB: name = "MOVETAB"; break;
1781 case DT_SYMINFO: name = "SYMINFO"; break;
1782 case DT_RELACOUNT: name = "RELACOUNT"; break;
1783 case DT_RELCOUNT: name = "RELCOUNT"; break;
1784 case DT_FLAGS_1: name = "FLAGS_1"; break;
1785 case DT_VERSYM: name = "VERSYM"; break;
1786 case DT_VERDEF: name = "VERDEF"; break;
1787 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1788 case DT_VERNEED: name = "VERNEED"; break;
1789 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1790 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1791 case DT_USED: name = "USED"; break;
1792 case DT_FILTER: name = "FILTER"; stringp = true; break;
1793 case DT_GNU_HASH: name = "GNU_HASH"; break;
1796 fprintf (f, " %-20s ", name);
1797 if (! stringp)
1799 fprintf (f, "0x");
1800 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1802 else
1804 const char *string;
1805 unsigned int tagv = dyn.d_un.d_val;
1807 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1808 if (string == NULL)
1809 goto error_return;
1810 fprintf (f, "%s", string);
1812 fprintf (f, "\n");
1815 free (dynbuf);
1816 dynbuf = NULL;
1819 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1820 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1822 if (! _bfd_elf_slurp_version_tables (abfd, false))
1823 return false;
1826 if (elf_dynverdef (abfd) != 0)
1828 Elf_Internal_Verdef *t;
1830 fprintf (f, _("\nVersion definitions:\n"));
1831 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1833 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1834 t->vd_flags, t->vd_hash,
1835 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1836 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1838 Elf_Internal_Verdaux *a;
1840 fprintf (f, "\t");
1841 for (a = t->vd_auxptr->vda_nextptr;
1842 a != NULL;
1843 a = a->vda_nextptr)
1844 fprintf (f, "%s ",
1845 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1846 fprintf (f, "\n");
1851 if (elf_dynverref (abfd) != 0)
1853 Elf_Internal_Verneed *t;
1855 fprintf (f, _("\nVersion References:\n"));
1856 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1858 Elf_Internal_Vernaux *a;
1860 fprintf (f, _(" required from %s:\n"),
1861 t->vn_filename ? t->vn_filename : "<corrupt>");
1862 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1863 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1864 a->vna_flags, a->vna_other,
1865 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1869 return true;
1871 error_return:
1872 free (dynbuf);
1873 return false;
1876 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1877 and return symbol version for symbol version itself. */
1879 const char *
1880 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1881 bool base_p,
1882 bool *hidden)
1884 const char *version_string = NULL;
1885 if (elf_dynversym (abfd) != 0
1886 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1888 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1890 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1891 vernum &= VERSYM_VERSION;
1893 if (vernum == 0)
1894 version_string = "";
1895 else if (vernum == 1
1896 && (vernum > elf_tdata (abfd)->cverdefs
1897 || (elf_tdata (abfd)->verdef[0].vd_flags
1898 == VER_FLG_BASE)))
1899 version_string = base_p ? "Base" : "";
1900 else if (vernum <= elf_tdata (abfd)->cverdefs)
1902 const char *nodename
1903 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1904 version_string = "";
1905 if (base_p
1906 || nodename == NULL
1907 || symbol->name == NULL
1908 || strcmp (symbol->name, nodename) != 0)
1909 version_string = nodename;
1911 else
1913 Elf_Internal_Verneed *t;
1915 version_string = _("<corrupt>");
1916 for (t = elf_tdata (abfd)->verref;
1917 t != NULL;
1918 t = t->vn_nextref)
1920 Elf_Internal_Vernaux *a;
1922 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1924 if (a->vna_other == vernum)
1926 *hidden = true;
1927 version_string = a->vna_nodename;
1928 break;
1934 return version_string;
1937 /* Display ELF-specific fields of a symbol. */
1939 void
1940 bfd_elf_print_symbol (bfd *abfd,
1941 void *filep,
1942 asymbol *symbol,
1943 bfd_print_symbol_type how)
1945 FILE *file = (FILE *) filep;
1946 switch (how)
1948 case bfd_print_symbol_name:
1949 fprintf (file, "%s", symbol->name);
1950 break;
1951 case bfd_print_symbol_more:
1952 fprintf (file, "elf ");
1953 bfd_fprintf_vma (abfd, file, symbol->value);
1954 fprintf (file, " %x", symbol->flags);
1955 break;
1956 case bfd_print_symbol_all:
1958 const char *section_name;
1959 const char *name = NULL;
1960 const struct elf_backend_data *bed;
1961 unsigned char st_other;
1962 bfd_vma val;
1963 const char *version_string;
1964 bool hidden;
1966 section_name = symbol->section ? symbol->section->name : "(*none*)";
1968 bed = get_elf_backend_data (abfd);
1969 if (bed->elf_backend_print_symbol_all)
1970 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1972 if (name == NULL)
1974 name = symbol->name;
1975 bfd_print_symbol_vandf (abfd, file, symbol);
1978 fprintf (file, " %s\t", section_name);
1979 /* Print the "other" value for a symbol. For common symbols,
1980 we've already printed the size; now print the alignment.
1981 For other symbols, we have no specified alignment, and
1982 we've printed the address; now print the size. */
1983 if (symbol->section && bfd_is_com_section (symbol->section))
1984 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1985 else
1986 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1987 bfd_fprintf_vma (abfd, file, val);
1989 /* If we have version information, print it. */
1990 version_string = _bfd_elf_get_symbol_version_string (abfd,
1991 symbol,
1992 true,
1993 &hidden);
1994 if (version_string)
1996 if (!hidden)
1997 fprintf (file, " %-11s", version_string);
1998 else
2000 int i;
2002 fprintf (file, " (%s)", version_string);
2003 for (i = 10 - strlen (version_string); i > 0; --i)
2004 putc (' ', file);
2008 /* If the st_other field is not zero, print it. */
2009 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2011 switch (st_other)
2013 case 0: break;
2014 case STV_INTERNAL: fprintf (file, " .internal"); break;
2015 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2016 case STV_PROTECTED: fprintf (file, " .protected"); break;
2017 default:
2018 /* Some other non-defined flags are also present, so print
2019 everything hex. */
2020 fprintf (file, " 0x%02x", (unsigned int) st_other);
2023 fprintf (file, " %s", name);
2025 break;
2029 /* ELF .o/exec file reading */
2031 /* Create a new bfd section from an ELF section header. */
2033 bool
2034 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2036 Elf_Internal_Shdr *hdr;
2037 Elf_Internal_Ehdr *ehdr;
2038 const struct elf_backend_data *bed;
2039 const char *name;
2040 bool ret = true;
2042 if (shindex >= elf_numsections (abfd))
2043 return false;
2045 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2046 sh_link or sh_info. Detect this here, by refusing to load a
2047 section that we are already in the process of loading. */
2048 if (elf_tdata (abfd)->being_created[shindex])
2050 _bfd_error_handler
2051 (_("%pB: warning: loop in section dependencies detected"), abfd);
2052 return false;
2054 elf_tdata (abfd)->being_created[shindex] = true;
2056 hdr = elf_elfsections (abfd)[shindex];
2057 ehdr = elf_elfheader (abfd);
2058 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2059 hdr->sh_name);
2060 if (name == NULL)
2061 goto fail;
2063 bed = get_elf_backend_data (abfd);
2064 switch (hdr->sh_type)
2066 case SHT_NULL:
2067 /* Inactive section. Throw it away. */
2068 goto success;
2070 case SHT_PROGBITS: /* Normal section with contents. */
2071 case SHT_NOBITS: /* .bss section. */
2072 case SHT_HASH: /* .hash section. */
2073 case SHT_NOTE: /* .note section. */
2074 case SHT_INIT_ARRAY: /* .init_array section. */
2075 case SHT_FINI_ARRAY: /* .fini_array section. */
2076 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2077 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2078 case SHT_GNU_HASH: /* .gnu.hash section. */
2079 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2080 goto success;
2082 case SHT_DYNAMIC: /* Dynamic linking information. */
2083 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2084 goto fail;
2086 if (hdr->sh_link > elf_numsections (abfd))
2088 /* PR 10478: Accept Solaris binaries with a sh_link field
2089 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2090 switch (bfd_get_arch (abfd))
2092 case bfd_arch_i386:
2093 case bfd_arch_sparc:
2094 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2095 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2096 break;
2097 /* Otherwise fall through. */
2098 default:
2099 goto fail;
2102 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2103 goto fail;
2104 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2106 Elf_Internal_Shdr *dynsymhdr;
2108 /* The shared libraries distributed with hpux11 have a bogus
2109 sh_link field for the ".dynamic" section. Find the
2110 string table for the ".dynsym" section instead. */
2111 if (elf_dynsymtab (abfd) != 0)
2113 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2114 hdr->sh_link = dynsymhdr->sh_link;
2116 else
2118 unsigned int i, num_sec;
2120 num_sec = elf_numsections (abfd);
2121 for (i = 1; i < num_sec; i++)
2123 dynsymhdr = elf_elfsections (abfd)[i];
2124 if (dynsymhdr->sh_type == SHT_DYNSYM)
2126 hdr->sh_link = dynsymhdr->sh_link;
2127 break;
2132 goto success;
2134 case SHT_SYMTAB: /* A symbol table. */
2135 if (elf_onesymtab (abfd) == shindex)
2136 goto success;
2138 if (hdr->sh_entsize != bed->s->sizeof_sym)
2139 goto fail;
2141 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2143 if (hdr->sh_size != 0)
2144 goto fail;
2145 /* Some assemblers erroneously set sh_info to one with a
2146 zero sh_size. ld sees this as a global symbol count
2147 of (unsigned) -1. Fix it here. */
2148 hdr->sh_info = 0;
2149 goto success;
2152 /* PR 18854: A binary might contain more than one symbol table.
2153 Unusual, but possible. Warn, but continue. */
2154 if (elf_onesymtab (abfd) != 0)
2156 _bfd_error_handler
2157 /* xgettext:c-format */
2158 (_("%pB: warning: multiple symbol tables detected"
2159 " - ignoring the table in section %u"),
2160 abfd, shindex);
2161 goto success;
2163 elf_onesymtab (abfd) = shindex;
2164 elf_symtab_hdr (abfd) = *hdr;
2165 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2166 abfd->flags |= HAS_SYMS;
2168 /* Sometimes a shared object will map in the symbol table. If
2169 SHF_ALLOC is set, and this is a shared object, then we also
2170 treat this section as a BFD section. We can not base the
2171 decision purely on SHF_ALLOC, because that flag is sometimes
2172 set in a relocatable object file, which would confuse the
2173 linker. */
2174 if ((hdr->sh_flags & SHF_ALLOC) != 0
2175 && (abfd->flags & DYNAMIC) != 0
2176 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2177 shindex))
2178 goto fail;
2180 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2181 can't read symbols without that section loaded as well. It
2182 is most likely specified by the next section header. */
2184 elf_section_list * entry;
2185 unsigned int i, num_sec;
2187 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2188 if (entry->hdr.sh_link == shindex)
2189 goto success;
2191 num_sec = elf_numsections (abfd);
2192 for (i = shindex + 1; i < num_sec; i++)
2194 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2196 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2197 && hdr2->sh_link == shindex)
2198 break;
2201 if (i == num_sec)
2202 for (i = 1; i < shindex; i++)
2204 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2206 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2207 && hdr2->sh_link == shindex)
2208 break;
2211 if (i != shindex)
2212 ret = bfd_section_from_shdr (abfd, i);
2213 /* else FIXME: we have failed to find the symbol table.
2214 Should we issue an error? */
2215 goto success;
2218 case SHT_DYNSYM: /* A dynamic symbol table. */
2219 if (elf_dynsymtab (abfd) == shindex)
2220 goto success;
2222 if (hdr->sh_entsize != bed->s->sizeof_sym)
2223 goto fail;
2225 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2227 if (hdr->sh_size != 0)
2228 goto fail;
2230 /* Some linkers erroneously set sh_info to one with a
2231 zero sh_size. ld sees this as a global symbol count
2232 of (unsigned) -1. Fix it here. */
2233 hdr->sh_info = 0;
2234 goto success;
2237 /* PR 18854: A binary might contain more than one dynamic symbol table.
2238 Unusual, but possible. Warn, but continue. */
2239 if (elf_dynsymtab (abfd) != 0)
2241 _bfd_error_handler
2242 /* xgettext:c-format */
2243 (_("%pB: warning: multiple dynamic symbol tables detected"
2244 " - ignoring the table in section %u"),
2245 abfd, shindex);
2246 goto success;
2248 elf_dynsymtab (abfd) = shindex;
2249 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2250 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2251 abfd->flags |= HAS_SYMS;
2253 /* Besides being a symbol table, we also treat this as a regular
2254 section, so that objcopy can handle it. */
2255 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2256 goto success;
2258 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2260 elf_section_list * entry;
2262 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2263 if (entry->ndx == shindex)
2264 goto success;
2266 entry = bfd_alloc (abfd, sizeof (*entry));
2267 if (entry == NULL)
2268 goto fail;
2269 entry->ndx = shindex;
2270 entry->hdr = * hdr;
2271 entry->next = elf_symtab_shndx_list (abfd);
2272 elf_symtab_shndx_list (abfd) = entry;
2273 elf_elfsections (abfd)[shindex] = & entry->hdr;
2274 goto success;
2277 case SHT_STRTAB: /* A string table. */
2278 if (hdr->bfd_section != NULL)
2279 goto success;
2281 if (ehdr->e_shstrndx == shindex)
2283 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2284 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2285 goto success;
2288 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2290 symtab_strtab:
2291 elf_tdata (abfd)->strtab_hdr = *hdr;
2292 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2293 goto success;
2296 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2298 dynsymtab_strtab:
2299 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2300 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2301 elf_elfsections (abfd)[shindex] = hdr;
2302 /* We also treat this as a regular section, so that objcopy
2303 can handle it. */
2304 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2305 shindex);
2306 goto success;
2309 /* If the string table isn't one of the above, then treat it as a
2310 regular section. We need to scan all the headers to be sure,
2311 just in case this strtab section appeared before the above. */
2312 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2314 unsigned int i, num_sec;
2316 num_sec = elf_numsections (abfd);
2317 for (i = 1; i < num_sec; i++)
2319 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2320 if (hdr2->sh_link == shindex)
2322 /* Prevent endless recursion on broken objects. */
2323 if (i == shindex)
2324 goto fail;
2325 if (! bfd_section_from_shdr (abfd, i))
2326 goto fail;
2327 if (elf_onesymtab (abfd) == i)
2328 goto symtab_strtab;
2329 if (elf_dynsymtab (abfd) == i)
2330 goto dynsymtab_strtab;
2334 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2335 goto success;
2337 case SHT_REL:
2338 case SHT_RELA:
2339 case SHT_RELR:
2340 /* *These* do a lot of work -- but build no sections! */
2342 asection *target_sect;
2343 Elf_Internal_Shdr *hdr2, **p_hdr;
2344 unsigned int num_sec = elf_numsections (abfd);
2345 struct bfd_elf_section_data *esdt;
2346 bfd_size_type size;
2348 if (hdr->sh_type == SHT_REL)
2349 size = bed->s->sizeof_rel;
2350 else if (hdr->sh_type == SHT_RELA)
2351 size = bed->s->sizeof_rela;
2352 else
2353 size = bed->s->arch_size / 8;
2354 if (hdr->sh_entsize != size)
2355 goto fail;
2357 /* Check for a bogus link to avoid crashing. */
2358 if (hdr->sh_link >= num_sec)
2360 _bfd_error_handler
2361 /* xgettext:c-format */
2362 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2363 abfd, hdr->sh_link, name, shindex);
2364 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2365 goto success;
2368 /* Get the symbol table. */
2369 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2370 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2371 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2372 goto fail;
2374 /* If this is an alloc section in an executable or shared
2375 library, or the reloc section does not use the main symbol
2376 table we don't treat it as a reloc section. BFD can't
2377 adequately represent such a section, so at least for now,
2378 we don't try. We just present it as a normal section. We
2379 also can't use it as a reloc section if it points to the
2380 null section, an invalid section, another reloc section, or
2381 its sh_link points to the null section. */
2382 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2383 && (hdr->sh_flags & SHF_ALLOC) != 0)
2384 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2385 || hdr->sh_type == SHT_RELR
2386 || hdr->sh_link == SHN_UNDEF
2387 || hdr->sh_link != elf_onesymtab (abfd)
2388 || hdr->sh_info == SHN_UNDEF
2389 || hdr->sh_info >= num_sec
2390 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2391 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2393 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2394 goto success;
2397 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2398 goto fail;
2400 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2401 if (target_sect == NULL)
2402 goto fail;
2404 esdt = elf_section_data (target_sect);
2405 if (hdr->sh_type == SHT_RELA)
2406 p_hdr = &esdt->rela.hdr;
2407 else
2408 p_hdr = &esdt->rel.hdr;
2410 /* PR 17512: file: 0b4f81b7.
2411 Also see PR 24456, for a file which deliberately has two reloc
2412 sections. */
2413 if (*p_hdr != NULL)
2415 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2417 _bfd_error_handler
2418 /* xgettext:c-format */
2419 (_("%pB: warning: secondary relocation section '%s' "
2420 "for section %pA found - ignoring"),
2421 abfd, name, target_sect);
2423 else
2424 esdt->has_secondary_relocs = true;
2425 goto success;
2428 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2429 if (hdr2 == NULL)
2430 goto fail;
2431 *hdr2 = *hdr;
2432 *p_hdr = hdr2;
2433 elf_elfsections (abfd)[shindex] = hdr2;
2434 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2435 * bed->s->int_rels_per_ext_rel);
2436 target_sect->flags |= SEC_RELOC;
2437 target_sect->relocation = NULL;
2438 target_sect->rel_filepos = hdr->sh_offset;
2439 /* In the section to which the relocations apply, mark whether
2440 its relocations are of the REL or RELA variety. */
2441 if (hdr->sh_size != 0)
2443 if (hdr->sh_type == SHT_RELA)
2444 target_sect->use_rela_p = 1;
2446 abfd->flags |= HAS_RELOC;
2447 goto success;
2450 case SHT_GNU_verdef:
2451 if (hdr->sh_info != 0)
2452 elf_dynverdef (abfd) = shindex;
2453 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2454 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2455 goto success;
2457 case SHT_GNU_versym:
2458 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2459 goto fail;
2461 elf_dynversym (abfd) = shindex;
2462 elf_tdata (abfd)->dynversym_hdr = *hdr;
2463 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2464 goto success;
2466 case SHT_GNU_verneed:
2467 if (hdr->sh_info != 0)
2468 elf_dynverref (abfd) = shindex;
2469 elf_tdata (abfd)->dynverref_hdr = *hdr;
2470 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2471 goto success;
2473 case SHT_SHLIB:
2474 goto success;
2476 case SHT_GROUP:
2477 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2478 goto fail;
2480 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2481 goto fail;
2483 goto success;
2485 default:
2486 /* Possibly an attributes section. */
2487 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2488 || hdr->sh_type == bed->obj_attrs_section_type)
2490 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2491 goto fail;
2492 _bfd_elf_parse_attributes (abfd, hdr);
2493 goto success;
2496 /* Check for any processor-specific section types. */
2497 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2498 goto success;
2500 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2502 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2503 /* FIXME: How to properly handle allocated section reserved
2504 for applications? */
2505 _bfd_error_handler
2506 /* xgettext:c-format */
2507 (_("%pB: unknown type [%#x] section `%s'"),
2508 abfd, hdr->sh_type, name);
2509 else
2511 /* Allow sections reserved for applications. */
2512 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2513 goto success;
2516 else if (hdr->sh_type >= SHT_LOPROC
2517 && hdr->sh_type <= SHT_HIPROC)
2518 /* FIXME: We should handle this section. */
2519 _bfd_error_handler
2520 /* xgettext:c-format */
2521 (_("%pB: unknown type [%#x] section `%s'"),
2522 abfd, hdr->sh_type, name);
2523 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2525 /* Unrecognised OS-specific sections. */
2526 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2527 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2528 required to correctly process the section and the file should
2529 be rejected with an error message. */
2530 _bfd_error_handler
2531 /* xgettext:c-format */
2532 (_("%pB: unknown type [%#x] section `%s'"),
2533 abfd, hdr->sh_type, name);
2534 else
2536 /* Otherwise it should be processed. */
2537 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2538 goto success;
2541 else
2542 /* FIXME: We should handle this section. */
2543 _bfd_error_handler
2544 /* xgettext:c-format */
2545 (_("%pB: unknown type [%#x] section `%s'"),
2546 abfd, hdr->sh_type, name);
2548 goto fail;
2551 fail:
2552 ret = false;
2553 success:
2554 elf_tdata (abfd)->being_created[shindex] = false;
2555 return ret;
2558 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2560 Elf_Internal_Sym *
2561 bfd_sym_from_r_symndx (struct sym_cache *cache,
2562 bfd *abfd,
2563 unsigned long r_symndx)
2565 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2567 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2569 Elf_Internal_Shdr *symtab_hdr;
2570 unsigned char esym[sizeof (Elf64_External_Sym)];
2571 Elf_External_Sym_Shndx eshndx;
2573 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2574 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2575 &cache->sym[ent], esym, &eshndx) == NULL)
2576 return NULL;
2578 if (cache->abfd != abfd)
2580 memset (cache->indx, -1, sizeof (cache->indx));
2581 cache->abfd = abfd;
2583 cache->indx[ent] = r_symndx;
2586 return &cache->sym[ent];
2589 /* Given an ELF section number, retrieve the corresponding BFD
2590 section. */
2592 asection *
2593 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2595 if (sec_index >= elf_numsections (abfd))
2596 return NULL;
2597 return elf_elfsections (abfd)[sec_index]->bfd_section;
2600 static const struct bfd_elf_special_section special_sections_b[] =
2602 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2603 { NULL, 0, 0, 0, 0 }
2606 static const struct bfd_elf_special_section special_sections_c[] =
2608 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2609 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2610 { NULL, 0, 0, 0, 0 }
2613 static const struct bfd_elf_special_section special_sections_d[] =
2615 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2616 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2617 /* There are more DWARF sections than these, but they needn't be added here
2618 unless you have to cope with broken compilers that don't emit section
2619 attributes or you want to help the user writing assembler. */
2620 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2621 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2622 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2623 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2624 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2625 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2626 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2627 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2628 { NULL, 0, 0, 0, 0 }
2631 static const struct bfd_elf_special_section special_sections_f[] =
2633 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2634 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2635 { NULL, 0 , 0, 0, 0 }
2638 static const struct bfd_elf_special_section special_sections_g[] =
2640 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2641 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2642 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2643 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2644 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2645 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2646 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2647 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2648 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2649 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2650 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2651 { NULL, 0, 0, 0, 0 }
2654 static const struct bfd_elf_special_section special_sections_h[] =
2656 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2657 { NULL, 0, 0, 0, 0 }
2660 static const struct bfd_elf_special_section special_sections_i[] =
2662 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2663 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2664 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2665 { NULL, 0, 0, 0, 0 }
2668 static const struct bfd_elf_special_section special_sections_l[] =
2670 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2671 { NULL, 0, 0, 0, 0 }
2674 static const struct bfd_elf_special_section special_sections_n[] =
2676 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2677 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2678 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2679 { NULL, 0, 0, 0, 0 }
2682 static const struct bfd_elf_special_section special_sections_p[] =
2684 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2685 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2686 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2687 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2688 { NULL, 0, 0, 0, 0 }
2691 static const struct bfd_elf_special_section special_sections_r[] =
2693 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2694 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2695 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
2696 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2697 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2698 { NULL, 0, 0, 0, 0 }
2701 static const struct bfd_elf_special_section special_sections_s[] =
2703 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2704 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2705 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2706 /* See struct bfd_elf_special_section declaration for the semantics of
2707 this special case where .prefix_length != strlen (.prefix). */
2708 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2709 { NULL, 0, 0, 0, 0 }
2712 static const struct bfd_elf_special_section special_sections_t[] =
2714 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2715 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2716 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2717 { NULL, 0, 0, 0, 0 }
2720 static const struct bfd_elf_special_section special_sections_z[] =
2722 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2723 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2724 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2725 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2726 { NULL, 0, 0, 0, 0 }
2729 static const struct bfd_elf_special_section * const special_sections[] =
2731 special_sections_b, /* 'b' */
2732 special_sections_c, /* 'c' */
2733 special_sections_d, /* 'd' */
2734 NULL, /* 'e' */
2735 special_sections_f, /* 'f' */
2736 special_sections_g, /* 'g' */
2737 special_sections_h, /* 'h' */
2738 special_sections_i, /* 'i' */
2739 NULL, /* 'j' */
2740 NULL, /* 'k' */
2741 special_sections_l, /* 'l' */
2742 NULL, /* 'm' */
2743 special_sections_n, /* 'n' */
2744 NULL, /* 'o' */
2745 special_sections_p, /* 'p' */
2746 NULL, /* 'q' */
2747 special_sections_r, /* 'r' */
2748 special_sections_s, /* 's' */
2749 special_sections_t, /* 't' */
2750 NULL, /* 'u' */
2751 NULL, /* 'v' */
2752 NULL, /* 'w' */
2753 NULL, /* 'x' */
2754 NULL, /* 'y' */
2755 special_sections_z /* 'z' */
2758 const struct bfd_elf_special_section *
2759 _bfd_elf_get_special_section (const char *name,
2760 const struct bfd_elf_special_section *spec,
2761 unsigned int rela)
2763 int i;
2764 int len;
2766 len = strlen (name);
2768 for (i = 0; spec[i].prefix != NULL; i++)
2770 int suffix_len;
2771 int prefix_len = spec[i].prefix_length;
2773 if (len < prefix_len)
2774 continue;
2775 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2776 continue;
2778 suffix_len = spec[i].suffix_length;
2779 if (suffix_len <= 0)
2781 if (name[prefix_len] != 0)
2783 if (suffix_len == 0)
2784 continue;
2785 if (name[prefix_len] != '.'
2786 && (suffix_len == -2
2787 || (rela && spec[i].type == SHT_REL)))
2788 continue;
2791 else
2793 if (len < prefix_len + suffix_len)
2794 continue;
2795 if (memcmp (name + len - suffix_len,
2796 spec[i].prefix + prefix_len,
2797 suffix_len) != 0)
2798 continue;
2800 return &spec[i];
2803 return NULL;
2806 const struct bfd_elf_special_section *
2807 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2809 int i;
2810 const struct bfd_elf_special_section *spec;
2811 const struct elf_backend_data *bed;
2813 /* See if this is one of the special sections. */
2814 if (sec->name == NULL)
2815 return NULL;
2817 bed = get_elf_backend_data (abfd);
2818 spec = bed->special_sections;
2819 if (spec)
2821 spec = _bfd_elf_get_special_section (sec->name,
2822 bed->special_sections,
2823 sec->use_rela_p);
2824 if (spec != NULL)
2825 return spec;
2828 if (sec->name[0] != '.')
2829 return NULL;
2831 i = sec->name[1] - 'b';
2832 if (i < 0 || i > 'z' - 'b')
2833 return NULL;
2835 spec = special_sections[i];
2837 if (spec == NULL)
2838 return NULL;
2840 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2843 bool
2844 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2846 struct bfd_elf_section_data *sdata;
2847 const struct elf_backend_data *bed;
2848 const struct bfd_elf_special_section *ssect;
2850 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2851 if (sdata == NULL)
2853 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2854 sizeof (*sdata));
2855 if (sdata == NULL)
2856 return false;
2857 sec->used_by_bfd = sdata;
2860 /* Indicate whether or not this section should use RELA relocations. */
2861 bed = get_elf_backend_data (abfd);
2862 sec->use_rela_p = bed->default_use_rela_p;
2864 /* Set up ELF section type and flags for newly created sections, if
2865 there is an ABI mandated section. */
2866 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2867 if (ssect != NULL)
2869 elf_section_type (sec) = ssect->type;
2870 elf_section_flags (sec) = ssect->attr;
2873 return _bfd_generic_new_section_hook (abfd, sec);
2876 /* Create a new bfd section from an ELF program header.
2878 Since program segments have no names, we generate a synthetic name
2879 of the form segment<NUM>, where NUM is generally the index in the
2880 program header table. For segments that are split (see below) we
2881 generate the names segment<NUM>a and segment<NUM>b.
2883 Note that some program segments may have a file size that is different than
2884 (less than) the memory size. All this means is that at execution the
2885 system must allocate the amount of memory specified by the memory size,
2886 but only initialize it with the first "file size" bytes read from the
2887 file. This would occur for example, with program segments consisting
2888 of combined data+bss.
2890 To handle the above situation, this routine generates TWO bfd sections
2891 for the single program segment. The first has the length specified by
2892 the file size of the segment, and the second has the length specified
2893 by the difference between the two sizes. In effect, the segment is split
2894 into its initialized and uninitialized parts. */
2896 bool
2897 _bfd_elf_make_section_from_phdr (bfd *abfd,
2898 Elf_Internal_Phdr *hdr,
2899 int hdr_index,
2900 const char *type_name)
2902 asection *newsect;
2903 char *name;
2904 char namebuf[64];
2905 size_t len;
2906 int split;
2907 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2909 split = ((hdr->p_memsz > 0)
2910 && (hdr->p_filesz > 0)
2911 && (hdr->p_memsz > hdr->p_filesz));
2913 if (hdr->p_filesz > 0)
2915 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2916 len = strlen (namebuf) + 1;
2917 name = (char *) bfd_alloc (abfd, len);
2918 if (!name)
2919 return false;
2920 memcpy (name, namebuf, len);
2921 newsect = bfd_make_section (abfd, name);
2922 if (newsect == NULL)
2923 return false;
2924 newsect->vma = hdr->p_vaddr / opb;
2925 newsect->lma = hdr->p_paddr / opb;
2926 newsect->size = hdr->p_filesz;
2927 newsect->filepos = hdr->p_offset;
2928 newsect->flags |= SEC_HAS_CONTENTS;
2929 newsect->alignment_power = bfd_log2 (hdr->p_align);
2930 if (hdr->p_type == PT_LOAD)
2932 newsect->flags |= SEC_ALLOC;
2933 newsect->flags |= SEC_LOAD;
2934 if (hdr->p_flags & PF_X)
2936 /* FIXME: all we known is that it has execute PERMISSION,
2937 may be data. */
2938 newsect->flags |= SEC_CODE;
2941 if (!(hdr->p_flags & PF_W))
2943 newsect->flags |= SEC_READONLY;
2947 if (hdr->p_memsz > hdr->p_filesz)
2949 bfd_vma align;
2951 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2952 len = strlen (namebuf) + 1;
2953 name = (char *) bfd_alloc (abfd, len);
2954 if (!name)
2955 return false;
2956 memcpy (name, namebuf, len);
2957 newsect = bfd_make_section (abfd, name);
2958 if (newsect == NULL)
2959 return false;
2960 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2961 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
2962 newsect->size = hdr->p_memsz - hdr->p_filesz;
2963 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2964 align = newsect->vma & -newsect->vma;
2965 if (align == 0 || align > hdr->p_align)
2966 align = hdr->p_align;
2967 newsect->alignment_power = bfd_log2 (align);
2968 if (hdr->p_type == PT_LOAD)
2970 newsect->flags |= SEC_ALLOC;
2971 if (hdr->p_flags & PF_X)
2972 newsect->flags |= SEC_CODE;
2974 if (!(hdr->p_flags & PF_W))
2975 newsect->flags |= SEC_READONLY;
2978 return true;
2981 static bool
2982 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
2984 /* The return value is ignored. Build-ids are considered optional. */
2985 if (templ->xvec->flavour == bfd_target_elf_flavour)
2986 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
2987 (templ, offset);
2988 return false;
2991 bool
2992 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2994 const struct elf_backend_data *bed;
2996 switch (hdr->p_type)
2998 case PT_NULL:
2999 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3001 case PT_LOAD:
3002 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3003 return false;
3004 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3005 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3006 return true;
3008 case PT_DYNAMIC:
3009 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3011 case PT_INTERP:
3012 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3014 case PT_NOTE:
3015 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3016 return false;
3017 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3018 hdr->p_align))
3019 return false;
3020 return true;
3022 case PT_SHLIB:
3023 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3025 case PT_PHDR:
3026 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3028 case PT_GNU_EH_FRAME:
3029 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3030 "eh_frame_hdr");
3032 case PT_GNU_STACK:
3033 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3035 case PT_GNU_RELRO:
3036 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3038 case PT_GNU_SFRAME:
3039 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3040 "sframe");
3042 default:
3043 /* Check for any processor-specific program segment types. */
3044 bed = get_elf_backend_data (abfd);
3045 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3049 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3050 REL or RELA. */
3052 Elf_Internal_Shdr *
3053 _bfd_elf_single_rel_hdr (asection *sec)
3055 if (elf_section_data (sec)->rel.hdr)
3057 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3058 return elf_section_data (sec)->rel.hdr;
3060 else
3061 return elf_section_data (sec)->rela.hdr;
3064 static bool
3065 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3066 Elf_Internal_Shdr *rel_hdr,
3067 const char *sec_name,
3068 bool use_rela_p)
3070 char *name = (char *) bfd_alloc (abfd,
3071 sizeof ".rela" + strlen (sec_name));
3072 if (name == NULL)
3073 return false;
3075 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3076 rel_hdr->sh_name =
3077 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3078 false);
3079 if (rel_hdr->sh_name == (unsigned int) -1)
3080 return false;
3082 return true;
3085 /* Allocate and initialize a section-header for a new reloc section,
3086 containing relocations against ASECT. It is stored in RELDATA. If
3087 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3088 relocations. */
3090 static bool
3091 _bfd_elf_init_reloc_shdr (bfd *abfd,
3092 struct bfd_elf_section_reloc_data *reldata,
3093 const char *sec_name,
3094 bool use_rela_p,
3095 bool delay_st_name_p)
3097 Elf_Internal_Shdr *rel_hdr;
3098 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3100 BFD_ASSERT (reldata->hdr == NULL);
3101 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3102 reldata->hdr = rel_hdr;
3104 if (delay_st_name_p)
3105 rel_hdr->sh_name = (unsigned int) -1;
3106 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3107 use_rela_p))
3108 return false;
3109 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3110 rel_hdr->sh_entsize = (use_rela_p
3111 ? bed->s->sizeof_rela
3112 : bed->s->sizeof_rel);
3113 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3114 rel_hdr->sh_flags = 0;
3115 rel_hdr->sh_addr = 0;
3116 rel_hdr->sh_size = 0;
3117 rel_hdr->sh_offset = 0;
3119 return true;
3122 /* Return the default section type based on the passed in section flags. */
3125 bfd_elf_get_default_section_type (flagword flags)
3127 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3128 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3129 return SHT_NOBITS;
3130 return SHT_PROGBITS;
3133 struct fake_section_arg
3135 struct bfd_link_info *link_info;
3136 bool failed;
3139 /* Set up an ELF internal section header for a section. */
3141 static void
3142 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3144 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3145 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3146 struct bfd_elf_section_data *esd = elf_section_data (asect);
3147 Elf_Internal_Shdr *this_hdr;
3148 unsigned int sh_type;
3149 const char *name = asect->name;
3150 bool delay_st_name_p = false;
3151 bfd_vma mask;
3153 if (arg->failed)
3155 /* We already failed; just get out of the bfd_map_over_sections
3156 loop. */
3157 return;
3160 this_hdr = &esd->this_hdr;
3162 /* ld: compress DWARF debug sections with names: .debug_*. */
3163 if (arg->link_info
3164 && (abfd->flags & BFD_COMPRESS) != 0
3165 && (asect->flags & SEC_DEBUGGING) != 0
3166 && name[1] == 'd'
3167 && name[6] == '_')
3169 /* If this section will be compressed, delay adding section
3170 name to section name section after it is compressed in
3171 _bfd_elf_assign_file_positions_for_non_load. */
3172 delay_st_name_p = true;
3175 if (delay_st_name_p)
3176 this_hdr->sh_name = (unsigned int) -1;
3177 else
3179 this_hdr->sh_name
3180 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3181 name, false);
3182 if (this_hdr->sh_name == (unsigned int) -1)
3184 arg->failed = true;
3185 return;
3189 /* Don't clear sh_flags. Assembler may set additional bits. */
3191 if ((asect->flags & SEC_ALLOC) != 0
3192 || asect->user_set_vma)
3193 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3194 else
3195 this_hdr->sh_addr = 0;
3197 this_hdr->sh_offset = 0;
3198 this_hdr->sh_size = asect->size;
3199 this_hdr->sh_link = 0;
3200 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3201 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3203 _bfd_error_handler
3204 /* xgettext:c-format */
3205 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3206 abfd, asect->alignment_power, asect);
3207 arg->failed = true;
3208 return;
3210 /* Set sh_addralign to the highest power of two given by alignment
3211 consistent with the section VMA. Linker scripts can force VMA. */
3212 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3213 this_hdr->sh_addralign = mask & -mask;
3214 /* The sh_entsize and sh_info fields may have been set already by
3215 copy_private_section_data. */
3217 this_hdr->bfd_section = asect;
3218 this_hdr->contents = NULL;
3220 /* If the section type is unspecified, we set it based on
3221 asect->flags. */
3222 if (asect->type != 0)
3223 sh_type = asect->type;
3224 else if ((asect->flags & SEC_GROUP) != 0)
3225 sh_type = SHT_GROUP;
3226 else
3227 sh_type = bfd_elf_get_default_section_type (asect->flags);
3229 if (this_hdr->sh_type == SHT_NULL)
3230 this_hdr->sh_type = sh_type;
3231 else if (this_hdr->sh_type == SHT_NOBITS
3232 && sh_type == SHT_PROGBITS
3233 && (asect->flags & SEC_ALLOC) != 0)
3235 /* Warn if we are changing a NOBITS section to PROGBITS, but
3236 allow the link to proceed. This can happen when users link
3237 non-bss input sections to bss output sections, or emit data
3238 to a bss output section via a linker script. */
3239 _bfd_error_handler
3240 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3241 this_hdr->sh_type = sh_type;
3244 switch (this_hdr->sh_type)
3246 default:
3247 break;
3249 case SHT_STRTAB:
3250 case SHT_NOTE:
3251 case SHT_NOBITS:
3252 case SHT_PROGBITS:
3253 break;
3255 case SHT_INIT_ARRAY:
3256 case SHT_FINI_ARRAY:
3257 case SHT_PREINIT_ARRAY:
3258 this_hdr->sh_entsize = bed->s->arch_size / 8;
3259 break;
3261 case SHT_HASH:
3262 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3263 break;
3265 case SHT_DYNSYM:
3266 this_hdr->sh_entsize = bed->s->sizeof_sym;
3267 break;
3269 case SHT_DYNAMIC:
3270 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3271 break;
3273 case SHT_RELA:
3274 if (get_elf_backend_data (abfd)->may_use_rela_p)
3275 this_hdr->sh_entsize = bed->s->sizeof_rela;
3276 break;
3278 case SHT_REL:
3279 if (get_elf_backend_data (abfd)->may_use_rel_p)
3280 this_hdr->sh_entsize = bed->s->sizeof_rel;
3281 break;
3283 case SHT_GNU_versym:
3284 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3285 break;
3287 case SHT_GNU_verdef:
3288 this_hdr->sh_entsize = 0;
3289 /* objcopy or strip will copy over sh_info, but may not set
3290 cverdefs. The linker will set cverdefs, but sh_info will be
3291 zero. */
3292 if (this_hdr->sh_info == 0)
3293 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3294 else
3295 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3296 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3297 break;
3299 case SHT_GNU_verneed:
3300 this_hdr->sh_entsize = 0;
3301 /* objcopy or strip will copy over sh_info, but may not set
3302 cverrefs. The linker will set cverrefs, but sh_info will be
3303 zero. */
3304 if (this_hdr->sh_info == 0)
3305 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3306 else
3307 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3308 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3309 break;
3311 case SHT_GROUP:
3312 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3313 break;
3315 case SHT_GNU_HASH:
3316 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3317 break;
3320 if ((asect->flags & SEC_ALLOC) != 0)
3321 this_hdr->sh_flags |= SHF_ALLOC;
3322 if ((asect->flags & SEC_READONLY) == 0)
3323 this_hdr->sh_flags |= SHF_WRITE;
3324 if ((asect->flags & SEC_CODE) != 0)
3325 this_hdr->sh_flags |= SHF_EXECINSTR;
3326 if ((asect->flags & SEC_MERGE) != 0)
3328 this_hdr->sh_flags |= SHF_MERGE;
3329 this_hdr->sh_entsize = asect->entsize;
3331 if ((asect->flags & SEC_STRINGS) != 0)
3332 this_hdr->sh_flags |= SHF_STRINGS;
3333 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3334 this_hdr->sh_flags |= SHF_GROUP;
3335 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3337 this_hdr->sh_flags |= SHF_TLS;
3338 if (asect->size == 0
3339 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3341 struct bfd_link_order *o = asect->map_tail.link_order;
3343 this_hdr->sh_size = 0;
3344 if (o != NULL)
3346 this_hdr->sh_size = o->offset + o->size;
3347 if (this_hdr->sh_size != 0)
3348 this_hdr->sh_type = SHT_NOBITS;
3352 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3353 this_hdr->sh_flags |= SHF_EXCLUDE;
3355 /* If the section has relocs, set up a section header for the
3356 SHT_REL[A] section. If two relocation sections are required for
3357 this section, it is up to the processor-specific back-end to
3358 create the other. */
3359 if ((asect->flags & SEC_RELOC) != 0)
3361 /* When doing a relocatable link, create both REL and RELA sections if
3362 needed. */
3363 if (arg->link_info
3364 /* Do the normal setup if we wouldn't create any sections here. */
3365 && esd->rel.count + esd->rela.count > 0
3366 && (bfd_link_relocatable (arg->link_info)
3367 || arg->link_info->emitrelocations))
3369 if (esd->rel.count && esd->rel.hdr == NULL
3370 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3371 false, delay_st_name_p))
3373 arg->failed = true;
3374 return;
3376 if (esd->rela.count && esd->rela.hdr == NULL
3377 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3378 true, delay_st_name_p))
3380 arg->failed = true;
3381 return;
3384 else if (!_bfd_elf_init_reloc_shdr (abfd,
3385 (asect->use_rela_p
3386 ? &esd->rela : &esd->rel),
3387 name,
3388 asect->use_rela_p,
3389 delay_st_name_p))
3391 arg->failed = true;
3392 return;
3396 /* Check for processor-specific section types. */
3397 sh_type = this_hdr->sh_type;
3398 if (bed->elf_backend_fake_sections
3399 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3401 arg->failed = true;
3402 return;
3405 if (sh_type == SHT_NOBITS && asect->size != 0)
3407 /* Don't change the header type from NOBITS if we are being
3408 called for objcopy --only-keep-debug. */
3409 this_hdr->sh_type = sh_type;
3413 /* Fill in the contents of a SHT_GROUP section. Called from
3414 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3415 when ELF targets use the generic linker, ld. Called for ld -r
3416 from bfd_elf_final_link. */
3418 void
3419 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3421 bool *failedptr = (bool *) failedptrarg;
3422 asection *elt, *first;
3423 unsigned char *loc;
3424 bool gas;
3426 /* Ignore linker created group section. See elfNN_ia64_object_p in
3427 elfxx-ia64.c. */
3428 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3429 || sec->size == 0
3430 || *failedptr)
3431 return;
3433 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3435 unsigned long symindx = 0;
3437 /* elf_group_id will have been set up by objcopy and the
3438 generic linker. */
3439 if (elf_group_id (sec) != NULL)
3440 symindx = elf_group_id (sec)->udata.i;
3442 if (symindx == 0)
3444 /* If called from the assembler, swap_out_syms will have set up
3445 elf_section_syms.
3446 PR 25699: A corrupt input file could contain bogus group info. */
3447 if (sec->index >= elf_num_section_syms (abfd)
3448 || elf_section_syms (abfd)[sec->index] == NULL)
3450 *failedptr = true;
3451 return;
3453 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3455 elf_section_data (sec)->this_hdr.sh_info = symindx;
3457 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3459 /* The ELF backend linker sets sh_info to -2 when the group
3460 signature symbol is global, and thus the index can't be
3461 set until all local symbols are output. */
3462 asection *igroup;
3463 struct bfd_elf_section_data *sec_data;
3464 unsigned long symndx;
3465 unsigned long extsymoff;
3466 struct elf_link_hash_entry *h;
3468 /* The point of this little dance to the first SHF_GROUP section
3469 then back to the SHT_GROUP section is that this gets us to
3470 the SHT_GROUP in the input object. */
3471 igroup = elf_sec_group (elf_next_in_group (sec));
3472 sec_data = elf_section_data (igroup);
3473 symndx = sec_data->this_hdr.sh_info;
3474 extsymoff = 0;
3475 if (!elf_bad_symtab (igroup->owner))
3477 Elf_Internal_Shdr *symtab_hdr;
3479 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3480 extsymoff = symtab_hdr->sh_info;
3482 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3483 while (h->root.type == bfd_link_hash_indirect
3484 || h->root.type == bfd_link_hash_warning)
3485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3487 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3490 /* The contents won't be allocated for "ld -r" or objcopy. */
3491 gas = true;
3492 if (sec->contents == NULL)
3494 gas = false;
3495 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3497 /* Arrange for the section to be written out. */
3498 elf_section_data (sec)->this_hdr.contents = sec->contents;
3499 if (sec->contents == NULL)
3501 *failedptr = true;
3502 return;
3506 loc = sec->contents + sec->size;
3508 /* Get the pointer to the first section in the group that gas
3509 squirreled away here. objcopy arranges for this to be set to the
3510 start of the input section group. */
3511 first = elt = elf_next_in_group (sec);
3513 /* First element is a flag word. Rest of section is elf section
3514 indices for all the sections of the group. Write them backwards
3515 just to keep the group in the same order as given in .section
3516 directives, not that it matters. */
3517 while (elt != NULL)
3519 asection *s;
3521 s = elt;
3522 if (!gas)
3523 s = s->output_section;
3524 if (s != NULL
3525 && !bfd_is_abs_section (s))
3527 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3528 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3530 if (elf_sec->rel.hdr != NULL
3531 && (gas
3532 || (input_elf_sec->rel.hdr != NULL
3533 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3535 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3536 loc -= 4;
3537 if (loc == sec->contents)
3538 break;
3539 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3541 if (elf_sec->rela.hdr != NULL
3542 && (gas
3543 || (input_elf_sec->rela.hdr != NULL
3544 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3546 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3547 loc -= 4;
3548 if (loc == sec->contents)
3549 break;
3550 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3552 loc -= 4;
3553 if (loc == sec->contents)
3554 break;
3555 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3557 elt = elf_next_in_group (elt);
3558 if (elt == first)
3559 break;
3562 /* We should always get here with loc == sec->contents + 4, but it is
3563 possible to craft bogus SHT_GROUP sections that will cause segfaults
3564 in objcopy without checking loc here and in the loop above. */
3565 if (loc == sec->contents)
3566 BFD_ASSERT (0);
3567 else
3569 loc -= 4;
3570 if (loc != sec->contents)
3572 BFD_ASSERT (0);
3573 memset (sec->contents + 4, 0, loc - sec->contents);
3574 loc = sec->contents;
3578 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3581 /* Given NAME, the name of a relocation section stripped of its
3582 .rel/.rela prefix, return the section in ABFD to which the
3583 relocations apply. */
3585 asection *
3586 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3588 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3589 section likely apply to .got.plt or .got section. */
3590 if (get_elf_backend_data (abfd)->want_got_plt
3591 && strcmp (name, ".plt") == 0)
3593 asection *sec;
3595 name = ".got.plt";
3596 sec = bfd_get_section_by_name (abfd, name);
3597 if (sec != NULL)
3598 return sec;
3599 name = ".got";
3602 return bfd_get_section_by_name (abfd, name);
3605 /* Return the section to which RELOC_SEC applies. */
3607 static asection *
3608 elf_get_reloc_section (asection *reloc_sec)
3610 const char *name;
3611 unsigned int type;
3612 bfd *abfd;
3613 const struct elf_backend_data *bed;
3615 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3616 if (type != SHT_REL && type != SHT_RELA)
3617 return NULL;
3619 /* We look up the section the relocs apply to by name. */
3620 name = reloc_sec->name;
3621 if (!startswith (name, ".rel"))
3622 return NULL;
3623 name += 4;
3624 if (type == SHT_RELA && *name++ != 'a')
3625 return NULL;
3627 abfd = reloc_sec->owner;
3628 bed = get_elf_backend_data (abfd);
3629 return bed->get_reloc_section (abfd, name);
3632 /* Assign all ELF section numbers. The dummy first section is handled here
3633 too. The link/info pointers for the standard section types are filled
3634 in here too, while we're at it. LINK_INFO will be 0 when arriving
3635 here for gas, objcopy, and when using the generic ELF linker. */
3637 static bool
3638 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3640 struct elf_obj_tdata *t = elf_tdata (abfd);
3641 asection *sec;
3642 unsigned int section_number;
3643 Elf_Internal_Shdr **i_shdrp;
3644 struct bfd_elf_section_data *d;
3645 bool need_symtab;
3646 size_t amt;
3648 section_number = 1;
3650 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3652 /* SHT_GROUP sections are in relocatable files only. */
3653 if (link_info == NULL || !link_info->resolve_section_groups)
3655 size_t reloc_count = 0;
3657 /* Put SHT_GROUP sections first. */
3658 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3660 d = elf_section_data (sec);
3662 if (d->this_hdr.sh_type == SHT_GROUP)
3664 if (sec->flags & SEC_LINKER_CREATED)
3666 /* Remove the linker created SHT_GROUP sections. */
3667 bfd_section_list_remove (abfd, sec);
3668 abfd->section_count--;
3670 else
3671 d->this_idx = section_number++;
3674 /* Count relocations. */
3675 reloc_count += sec->reloc_count;
3678 /* Set/clear HAS_RELOC depending on whether there are relocations. */
3679 if (reloc_count == 0)
3680 abfd->flags &= ~HAS_RELOC;
3681 else
3682 abfd->flags |= HAS_RELOC;
3685 for (sec = abfd->sections; sec; sec = sec->next)
3687 d = elf_section_data (sec);
3689 if (d->this_hdr.sh_type != SHT_GROUP)
3690 d->this_idx = section_number++;
3691 if (d->this_hdr.sh_name != (unsigned int) -1)
3692 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3693 if (d->rel.hdr)
3695 d->rel.idx = section_number++;
3696 if (d->rel.hdr->sh_name != (unsigned int) -1)
3697 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3699 else
3700 d->rel.idx = 0;
3702 if (d->rela.hdr)
3704 d->rela.idx = section_number++;
3705 if (d->rela.hdr->sh_name != (unsigned int) -1)
3706 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3708 else
3709 d->rela.idx = 0;
3712 need_symtab = (bfd_get_symcount (abfd) > 0
3713 || (link_info == NULL
3714 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3715 == HAS_RELOC)));
3716 if (need_symtab)
3718 elf_onesymtab (abfd) = section_number++;
3719 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3720 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3722 elf_section_list *entry;
3724 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3726 entry = bfd_zalloc (abfd, sizeof (*entry));
3727 entry->ndx = section_number++;
3728 elf_symtab_shndx_list (abfd) = entry;
3729 entry->hdr.sh_name
3730 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3731 ".symtab_shndx", false);
3732 if (entry->hdr.sh_name == (unsigned int) -1)
3733 return false;
3735 elf_strtab_sec (abfd) = section_number++;
3736 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3739 elf_shstrtab_sec (abfd) = section_number++;
3740 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3741 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3743 if (section_number >= SHN_LORESERVE)
3745 /* xgettext:c-format */
3746 _bfd_error_handler (_("%pB: too many sections: %u"),
3747 abfd, section_number);
3748 return false;
3751 elf_numsections (abfd) = section_number;
3752 elf_elfheader (abfd)->e_shnum = section_number;
3754 /* Set up the list of section header pointers, in agreement with the
3755 indices. */
3756 amt = section_number * sizeof (Elf_Internal_Shdr *);
3757 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3758 if (i_shdrp == NULL)
3759 return false;
3761 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3762 sizeof (Elf_Internal_Shdr));
3763 if (i_shdrp[0] == NULL)
3765 bfd_release (abfd, i_shdrp);
3766 return false;
3769 elf_elfsections (abfd) = i_shdrp;
3771 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3772 if (need_symtab)
3774 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3775 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3777 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3778 BFD_ASSERT (entry != NULL);
3779 i_shdrp[entry->ndx] = & entry->hdr;
3780 entry->hdr.sh_link = elf_onesymtab (abfd);
3782 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3783 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3786 for (sec = abfd->sections; sec; sec = sec->next)
3788 asection *s;
3790 d = elf_section_data (sec);
3792 i_shdrp[d->this_idx] = &d->this_hdr;
3793 if (d->rel.idx != 0)
3794 i_shdrp[d->rel.idx] = d->rel.hdr;
3795 if (d->rela.idx != 0)
3796 i_shdrp[d->rela.idx] = d->rela.hdr;
3798 /* Fill in the sh_link and sh_info fields while we're at it. */
3800 /* sh_link of a reloc section is the section index of the symbol
3801 table. sh_info is the section index of the section to which
3802 the relocation entries apply. */
3803 if (d->rel.idx != 0)
3805 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3806 d->rel.hdr->sh_info = d->this_idx;
3807 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3809 if (d->rela.idx != 0)
3811 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3812 d->rela.hdr->sh_info = d->this_idx;
3813 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3816 /* We need to set up sh_link for SHF_LINK_ORDER. */
3817 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3819 s = elf_linked_to_section (sec);
3820 /* We can now have a NULL linked section pointer.
3821 This happens when the sh_link field is 0, which is done
3822 when a linked to section is discarded but the linking
3823 section has been retained for some reason. */
3824 if (s)
3826 /* Check discarded linkonce section. */
3827 if (discarded_section (s))
3829 asection *kept;
3830 _bfd_error_handler
3831 /* xgettext:c-format */
3832 (_("%pB: sh_link of section `%pA' points to"
3833 " discarded section `%pA' of `%pB'"),
3834 abfd, d->this_hdr.bfd_section, s, s->owner);
3835 /* Point to the kept section if it has the same
3836 size as the discarded one. */
3837 kept = _bfd_elf_check_kept_section (s, link_info);
3838 if (kept == NULL)
3840 bfd_set_error (bfd_error_bad_value);
3841 return false;
3843 s = kept;
3845 /* Handle objcopy. */
3846 else if (s->output_section == NULL)
3848 _bfd_error_handler
3849 /* xgettext:c-format */
3850 (_("%pB: sh_link of section `%pA' points to"
3851 " removed section `%pA' of `%pB'"),
3852 abfd, d->this_hdr.bfd_section, s, s->owner);
3853 bfd_set_error (bfd_error_bad_value);
3854 return false;
3856 s = s->output_section;
3857 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3861 switch (d->this_hdr.sh_type)
3863 case SHT_REL:
3864 case SHT_RELA:
3865 /* sh_link is the section index of the symbol table.
3866 sh_info is the section index of the section to which the
3867 relocation entries apply. */
3868 if (d->this_hdr.sh_link == 0)
3870 /* FIXME maybe: If this is a reloc section which we are
3871 treating as a normal section then we likely should
3872 not be assuming its sh_link is .dynsym or .symtab. */
3873 if ((sec->flags & SEC_ALLOC) != 0)
3875 s = bfd_get_section_by_name (abfd, ".dynsym");
3876 if (s != NULL)
3877 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3879 else
3880 d->this_hdr.sh_link = elf_onesymtab (abfd);
3883 s = elf_get_reloc_section (sec);
3884 if (s != NULL)
3886 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3887 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3889 break;
3891 case SHT_STRTAB:
3892 /* We assume that a section named .stab*str is a stabs
3893 string section. We look for a section with the same name
3894 but without the trailing ``str'', and set its sh_link
3895 field to point to this section. */
3896 if (startswith (sec->name, ".stab")
3897 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3899 size_t len;
3900 char *alc;
3902 len = strlen (sec->name);
3903 alc = (char *) bfd_malloc (len - 2);
3904 if (alc == NULL)
3905 return false;
3906 memcpy (alc, sec->name, len - 3);
3907 alc[len - 3] = '\0';
3908 s = bfd_get_section_by_name (abfd, alc);
3909 free (alc);
3910 if (s != NULL)
3912 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3914 /* This is a .stab section. */
3915 elf_section_data (s)->this_hdr.sh_entsize = 12;
3918 break;
3920 case SHT_DYNAMIC:
3921 case SHT_DYNSYM:
3922 case SHT_GNU_verneed:
3923 case SHT_GNU_verdef:
3924 /* sh_link is the section header index of the string table
3925 used for the dynamic entries, or the symbol table, or the
3926 version strings. */
3927 s = bfd_get_section_by_name (abfd, ".dynstr");
3928 if (s != NULL)
3929 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3930 break;
3932 case SHT_GNU_LIBLIST:
3933 /* sh_link is the section header index of the prelink library
3934 list used for the dynamic entries, or the symbol table, or
3935 the version strings. */
3936 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3937 ? ".dynstr" : ".gnu.libstr"));
3938 if (s != NULL)
3939 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3940 break;
3942 case SHT_HASH:
3943 case SHT_GNU_HASH:
3944 case SHT_GNU_versym:
3945 /* sh_link is the section header index of the symbol table
3946 this hash table or version table is for. */
3947 s = bfd_get_section_by_name (abfd, ".dynsym");
3948 if (s != NULL)
3949 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3950 break;
3952 case SHT_GROUP:
3953 d->this_hdr.sh_link = elf_onesymtab (abfd);
3957 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3958 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3959 debug section name from .debug_* to .zdebug_* if needed. */
3961 return true;
3964 static bool
3965 sym_is_global (bfd *abfd, asymbol *sym)
3967 /* If the backend has a special mapping, use it. */
3968 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3969 if (bed->elf_backend_sym_is_global)
3970 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3972 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3973 || bfd_is_und_section (bfd_asymbol_section (sym))
3974 || bfd_is_com_section (bfd_asymbol_section (sym)));
3977 /* Filter global symbols of ABFD to include in the import library. All
3978 SYMCOUNT symbols of ABFD can be examined from their pointers in
3979 SYMS. Pointers of symbols to keep should be stored contiguously at
3980 the beginning of that array.
3982 Returns the number of symbols to keep. */
3984 unsigned int
3985 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
3986 asymbol **syms, long symcount)
3988 long src_count, dst_count = 0;
3990 for (src_count = 0; src_count < symcount; src_count++)
3992 asymbol *sym = syms[src_count];
3993 char *name = (char *) bfd_asymbol_name (sym);
3994 struct bfd_link_hash_entry *h;
3996 if (!sym_is_global (abfd, sym))
3997 continue;
3999 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4000 if (h == NULL)
4001 continue;
4002 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4003 continue;
4004 if (h->linker_def || h->ldscript_def)
4005 continue;
4007 syms[dst_count++] = sym;
4010 syms[dst_count] = NULL;
4012 return dst_count;
4015 /* Don't output section symbols for sections that are not going to be
4016 output, that are duplicates or there is no BFD section. */
4018 static bool
4019 ignore_section_sym (bfd *abfd, asymbol *sym)
4021 elf_symbol_type *type_ptr;
4023 if (sym == NULL)
4024 return false;
4026 if ((sym->flags & BSF_SECTION_SYM) == 0)
4027 return false;
4029 /* Ignore the section symbol if it isn't used. */
4030 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4031 return true;
4033 if (sym->section == NULL)
4034 return true;
4036 type_ptr = elf_symbol_from (sym);
4037 return ((type_ptr != NULL
4038 && type_ptr->internal_elf_sym.st_shndx != 0
4039 && bfd_is_abs_section (sym->section))
4040 || !(sym->section->owner == abfd
4041 || (sym->section->output_section != NULL
4042 && sym->section->output_section->owner == abfd
4043 && sym->section->output_offset == 0)
4044 || bfd_is_abs_section (sym->section)));
4047 /* Map symbol from it's internal number to the external number, moving
4048 all local symbols to be at the head of the list. */
4050 static bool
4051 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4053 unsigned int symcount = bfd_get_symcount (abfd);
4054 asymbol **syms = bfd_get_outsymbols (abfd);
4055 asymbol **sect_syms;
4056 unsigned int num_locals = 0;
4057 unsigned int num_globals = 0;
4058 unsigned int num_locals2 = 0;
4059 unsigned int num_globals2 = 0;
4060 unsigned int max_index = 0;
4061 unsigned int idx;
4062 asection *asect;
4063 asymbol **new_syms;
4064 size_t amt;
4066 #ifdef DEBUG
4067 fprintf (stderr, "elf_map_symbols\n");
4068 fflush (stderr);
4069 #endif
4071 for (asect = abfd->sections; asect; asect = asect->next)
4073 if (max_index < asect->index)
4074 max_index = asect->index;
4077 max_index++;
4078 amt = max_index * sizeof (asymbol *);
4079 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4080 if (sect_syms == NULL)
4081 return false;
4082 elf_section_syms (abfd) = sect_syms;
4083 elf_num_section_syms (abfd) = max_index;
4085 /* Init sect_syms entries for any section symbols we have already
4086 decided to output. */
4087 for (idx = 0; idx < symcount; idx++)
4089 asymbol *sym = syms[idx];
4091 if ((sym->flags & BSF_SECTION_SYM) != 0
4092 && sym->value == 0
4093 && !ignore_section_sym (abfd, sym)
4094 && !bfd_is_abs_section (sym->section))
4096 asection *sec = sym->section;
4098 if (sec->owner != abfd)
4099 sec = sec->output_section;
4101 sect_syms[sec->index] = syms[idx];
4105 /* Classify all of the symbols. */
4106 for (idx = 0; idx < symcount; idx++)
4108 if (sym_is_global (abfd, syms[idx]))
4109 num_globals++;
4110 else if (!ignore_section_sym (abfd, syms[idx]))
4111 num_locals++;
4114 /* We will be adding a section symbol for each normal BFD section. Most
4115 sections will already have a section symbol in outsymbols, but
4116 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4117 at least in that case. */
4118 for (asect = abfd->sections; asect; asect = asect->next)
4120 asymbol *sym = asect->symbol;
4121 /* Don't include ignored section symbols. */
4122 if (!ignore_section_sym (abfd, sym)
4123 && sect_syms[asect->index] == NULL)
4125 if (!sym_is_global (abfd, asect->symbol))
4126 num_locals++;
4127 else
4128 num_globals++;
4132 /* Now sort the symbols so the local symbols are first. */
4133 amt = (num_locals + num_globals) * sizeof (asymbol *);
4134 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4135 if (new_syms == NULL)
4136 return false;
4138 for (idx = 0; idx < symcount; idx++)
4140 asymbol *sym = syms[idx];
4141 unsigned int i;
4143 if (sym_is_global (abfd, sym))
4144 i = num_locals + num_globals2++;
4145 /* Don't include ignored section symbols. */
4146 else if (!ignore_section_sym (abfd, sym))
4147 i = num_locals2++;
4148 else
4149 continue;
4150 new_syms[i] = sym;
4151 sym->udata.i = i + 1;
4153 for (asect = abfd->sections; asect; asect = asect->next)
4155 asymbol *sym = asect->symbol;
4156 if (!ignore_section_sym (abfd, sym)
4157 && sect_syms[asect->index] == NULL)
4159 unsigned int i;
4161 sect_syms[asect->index] = sym;
4162 if (!sym_is_global (abfd, sym))
4163 i = num_locals2++;
4164 else
4165 i = num_locals + num_globals2++;
4166 new_syms[i] = sym;
4167 sym->udata.i = i + 1;
4171 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4173 *pnum_locals = num_locals;
4174 return true;
4177 /* Align to the maximum file alignment that could be required for any
4178 ELF data structure. */
4180 static inline file_ptr
4181 align_file_position (file_ptr off, int align)
4183 return (off + align - 1) & ~(align - 1);
4186 /* Assign a file position to a section, optionally aligning to the
4187 required section alignment. */
4189 file_ptr
4190 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4191 file_ptr offset,
4192 bool align)
4194 if (align && i_shdrp->sh_addralign > 1)
4195 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4196 i_shdrp->sh_offset = offset;
4197 if (i_shdrp->bfd_section != NULL)
4198 i_shdrp->bfd_section->filepos = offset;
4199 if (i_shdrp->sh_type != SHT_NOBITS)
4200 offset += i_shdrp->sh_size;
4201 return offset;
4204 /* Compute the file positions we are going to put the sections at, and
4205 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4206 is not NULL, this is being called by the ELF backend linker. */
4208 bool
4209 _bfd_elf_compute_section_file_positions (bfd *abfd,
4210 struct bfd_link_info *link_info)
4212 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4213 struct fake_section_arg fsargs;
4214 bool failed;
4215 struct elf_strtab_hash *strtab = NULL;
4216 Elf_Internal_Shdr *shstrtab_hdr;
4217 bool need_symtab;
4219 if (abfd->output_has_begun)
4220 return true;
4222 /* Do any elf backend specific processing first. */
4223 if (bed->elf_backend_begin_write_processing)
4224 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4226 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4227 return false;
4229 fsargs.failed = false;
4230 fsargs.link_info = link_info;
4231 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4232 if (fsargs.failed)
4233 return false;
4235 if (!assign_section_numbers (abfd, link_info))
4236 return false;
4238 /* The backend linker builds symbol table information itself. */
4239 need_symtab = (link_info == NULL
4240 && (bfd_get_symcount (abfd) > 0
4241 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4242 == HAS_RELOC)));
4243 if (need_symtab)
4245 /* Non-zero if doing a relocatable link. */
4246 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4248 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4249 return false;
4252 failed = false;
4253 if (link_info == NULL)
4255 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4256 if (failed)
4257 return false;
4260 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4261 /* sh_name was set in init_file_header. */
4262 shstrtab_hdr->sh_type = SHT_STRTAB;
4263 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4264 shstrtab_hdr->sh_addr = 0;
4265 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4266 shstrtab_hdr->sh_entsize = 0;
4267 shstrtab_hdr->sh_link = 0;
4268 shstrtab_hdr->sh_info = 0;
4269 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4270 shstrtab_hdr->sh_addralign = 1;
4272 if (!assign_file_positions_except_relocs (abfd, link_info))
4273 return false;
4275 if (need_symtab)
4277 file_ptr off;
4278 Elf_Internal_Shdr *hdr;
4280 off = elf_next_file_pos (abfd);
4282 hdr = & elf_symtab_hdr (abfd);
4283 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4285 if (elf_symtab_shndx_list (abfd) != NULL)
4287 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4288 if (hdr->sh_size != 0)
4289 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4290 /* FIXME: What about other symtab_shndx sections in the list ? */
4293 hdr = &elf_tdata (abfd)->strtab_hdr;
4294 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4296 elf_next_file_pos (abfd) = off;
4298 /* Now that we know where the .strtab section goes, write it
4299 out. */
4300 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4301 || ! _bfd_elf_strtab_emit (abfd, strtab))
4302 return false;
4303 _bfd_elf_strtab_free (strtab);
4306 abfd->output_has_begun = true;
4308 return true;
4311 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4312 function effectively returns whether --eh-frame-hdr is given on the
4313 command line. After size_dynamic_sections the result reflects
4314 whether .eh_frame_hdr will actually be output (sizing isn't done
4315 until ldemul_after_allocation). */
4317 static asection *
4318 elf_eh_frame_hdr (const struct bfd_link_info *info)
4320 if (info != NULL && is_elf_hash_table (info->hash))
4321 return elf_hash_table (info)->eh_info.hdr_sec;
4322 return NULL;
4325 /* Make an initial estimate of the size of the program header. If we
4326 get the number wrong here, we'll redo section placement. */
4328 static bfd_size_type
4329 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4331 size_t segs;
4332 asection *s;
4333 const struct elf_backend_data *bed;
4335 /* Assume we will need exactly two PT_LOAD segments: one for text
4336 and one for data. */
4337 segs = 2;
4339 s = bfd_get_section_by_name (abfd, ".interp");
4340 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4342 /* If we have a loadable interpreter section, we need a
4343 PT_INTERP segment. In this case, assume we also need a
4344 PT_PHDR segment, although that may not be true for all
4345 targets. */
4346 segs += 2;
4349 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4351 /* We need a PT_DYNAMIC segment. */
4352 ++segs;
4355 if (info != NULL && info->relro)
4357 /* We need a PT_GNU_RELRO segment. */
4358 ++segs;
4361 if (elf_eh_frame_hdr (info))
4363 /* We need a PT_GNU_EH_FRAME segment. */
4364 ++segs;
4367 if (elf_stack_flags (abfd))
4369 /* We need a PT_GNU_STACK segment. */
4370 ++segs;
4373 if (elf_sframe (abfd))
4375 /* We need a PT_GNU_SFRAME segment. */
4376 ++segs;
4379 s = bfd_get_section_by_name (abfd,
4380 NOTE_GNU_PROPERTY_SECTION_NAME);
4381 if (s != NULL && s->size != 0)
4383 /* We need a PT_GNU_PROPERTY segment. */
4384 ++segs;
4387 for (s = abfd->sections; s != NULL; s = s->next)
4389 if ((s->flags & SEC_LOAD) != 0
4390 && elf_section_type (s) == SHT_NOTE)
4392 unsigned int alignment_power;
4393 /* We need a PT_NOTE segment. */
4394 ++segs;
4395 /* Try to create just one PT_NOTE segment for all adjacent
4396 loadable SHT_NOTE sections. gABI requires that within a
4397 PT_NOTE segment (and also inside of each SHT_NOTE section)
4398 each note should have the same alignment. So we check
4399 whether the sections are correctly aligned. */
4400 alignment_power = s->alignment_power;
4401 while (s->next != NULL
4402 && s->next->alignment_power == alignment_power
4403 && (s->next->flags & SEC_LOAD) != 0
4404 && elf_section_type (s->next) == SHT_NOTE)
4405 s = s->next;
4409 for (s = abfd->sections; s != NULL; s = s->next)
4411 if (s->flags & SEC_THREAD_LOCAL)
4413 /* We need a PT_TLS segment. */
4414 ++segs;
4415 break;
4419 bed = get_elf_backend_data (abfd);
4421 if ((abfd->flags & D_PAGED) != 0
4422 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4424 /* Add a PT_GNU_MBIND segment for each mbind section. */
4425 bfd_vma commonpagesize;
4426 unsigned int page_align_power;
4428 if (info != NULL)
4429 commonpagesize = info->commonpagesize;
4430 else
4431 commonpagesize = bed->commonpagesize;
4432 page_align_power = bfd_log2 (commonpagesize);
4433 for (s = abfd->sections; s != NULL; s = s->next)
4434 if (elf_section_flags (s) & SHF_GNU_MBIND)
4436 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4438 _bfd_error_handler
4439 /* xgettext:c-format */
4440 (_("%pB: GNU_MBIND section `%pA' has invalid "
4441 "sh_info field: %d"),
4442 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4443 continue;
4445 /* Align mbind section to page size. */
4446 if (s->alignment_power < page_align_power)
4447 s->alignment_power = page_align_power;
4448 segs ++;
4452 /* Let the backend count up any program headers it might need. */
4453 if (bed->elf_backend_additional_program_headers)
4455 int a;
4457 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4458 if (a == -1)
4459 abort ();
4460 segs += a;
4463 return segs * bed->s->sizeof_phdr;
4466 /* Find the segment that contains the output_section of section. */
4468 Elf_Internal_Phdr *
4469 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4471 struct elf_segment_map *m;
4472 Elf_Internal_Phdr *p;
4474 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4475 m != NULL;
4476 m = m->next, p++)
4478 int i;
4480 for (i = m->count - 1; i >= 0; i--)
4481 if (m->sections[i] == section)
4482 return p;
4485 return NULL;
4488 /* Create a mapping from a set of sections to a program segment. */
4490 static struct elf_segment_map *
4491 make_mapping (bfd *abfd,
4492 asection **sections,
4493 unsigned int from,
4494 unsigned int to,
4495 bool phdr)
4497 struct elf_segment_map *m;
4498 unsigned int i;
4499 asection **hdrpp;
4500 size_t amt;
4502 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4503 amt += (to - from) * sizeof (asection *);
4504 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4505 if (m == NULL)
4506 return NULL;
4507 m->next = NULL;
4508 m->p_type = PT_LOAD;
4509 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4510 m->sections[i - from] = *hdrpp;
4511 m->count = to - from;
4513 if (from == 0 && phdr)
4515 /* Include the headers in the first PT_LOAD segment. */
4516 m->includes_filehdr = 1;
4517 m->includes_phdrs = 1;
4520 return m;
4523 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4524 on failure. */
4526 struct elf_segment_map *
4527 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4529 struct elf_segment_map *m;
4531 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4532 sizeof (struct elf_segment_map));
4533 if (m == NULL)
4534 return NULL;
4535 m->next = NULL;
4536 m->p_type = PT_DYNAMIC;
4537 m->count = 1;
4538 m->sections[0] = dynsec;
4540 return m;
4543 /* Possibly add or remove segments from the segment map. */
4545 static bool
4546 elf_modify_segment_map (bfd *abfd,
4547 struct bfd_link_info *info,
4548 bool remove_empty_load)
4550 struct elf_segment_map **m;
4551 const struct elf_backend_data *bed;
4553 /* The placement algorithm assumes that non allocated sections are
4554 not in PT_LOAD segments. We ensure this here by removing such
4555 sections from the segment map. We also remove excluded
4556 sections. Finally, any PT_LOAD segment without sections is
4557 removed. */
4558 m = &elf_seg_map (abfd);
4559 while (*m)
4561 unsigned int i, new_count;
4563 for (new_count = 0, i = 0; i < (*m)->count; i++)
4565 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4566 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4567 || (*m)->p_type != PT_LOAD))
4569 (*m)->sections[new_count] = (*m)->sections[i];
4570 new_count++;
4573 (*m)->count = new_count;
4575 if (remove_empty_load
4576 && (*m)->p_type == PT_LOAD
4577 && (*m)->count == 0
4578 && !(*m)->includes_phdrs)
4579 *m = (*m)->next;
4580 else
4581 m = &(*m)->next;
4584 bed = get_elf_backend_data (abfd);
4585 if (bed->elf_backend_modify_segment_map != NULL)
4587 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4588 return false;
4591 return true;
4594 #define IS_TBSS(s) \
4595 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4597 /* Set up a mapping from BFD sections to program segments. Update
4598 NEED_LAYOUT if the section layout is changed. */
4600 bool
4601 _bfd_elf_map_sections_to_segments (bfd *abfd,
4602 struct bfd_link_info *info,
4603 bool *need_layout)
4605 unsigned int count;
4606 struct elf_segment_map *m;
4607 asection **sections = NULL;
4608 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4609 bool no_user_phdrs;
4611 no_user_phdrs = elf_seg_map (abfd) == NULL;
4613 if (info != NULL)
4615 info->user_phdrs = !no_user_phdrs;
4617 /* Size the relative relocations if DT_RELR is enabled. */
4618 if (info->enable_dt_relr
4619 && need_layout != NULL
4620 && bed->size_relative_relocs
4621 && !bed->size_relative_relocs (info, need_layout))
4622 info->callbacks->einfo
4623 (_("%F%P: failed to size relative relocations\n"));
4626 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4628 asection *s;
4629 unsigned int i;
4630 struct elf_segment_map *mfirst;
4631 struct elf_segment_map **pm;
4632 asection *last_hdr;
4633 bfd_vma last_size;
4634 unsigned int hdr_index;
4635 bfd_vma maxpagesize;
4636 asection **hdrpp;
4637 bool phdr_in_segment;
4638 bool writable;
4639 bool executable;
4640 unsigned int tls_count = 0;
4641 asection *first_tls = NULL;
4642 asection *first_mbind = NULL;
4643 asection *dynsec, *eh_frame_hdr;
4644 asection *sframe;
4645 size_t amt;
4646 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4647 bfd_size_type phdr_size; /* Octets/bytes. */
4648 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4650 /* Select the allocated sections, and sort them. */
4652 amt = bfd_count_sections (abfd) * sizeof (asection *);
4653 sections = (asection **) bfd_malloc (amt);
4654 if (sections == NULL)
4655 goto error_return;
4657 /* Calculate top address, avoiding undefined behaviour of shift
4658 left operator when shift count is equal to size of type
4659 being shifted. */
4660 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4661 addr_mask = (addr_mask << 1) + 1;
4663 i = 0;
4664 for (s = abfd->sections; s != NULL; s = s->next)
4666 if ((s->flags & SEC_ALLOC) != 0)
4668 /* target_index is unused until bfd_elf_final_link
4669 starts output of section symbols. Use it to make
4670 qsort stable. */
4671 s->target_index = i;
4672 sections[i] = s;
4673 ++i;
4674 /* A wrapping section potentially clashes with header. */
4675 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4676 wrap_to = (s->lma + s->size / opb) & addr_mask;
4679 BFD_ASSERT (i <= bfd_count_sections (abfd));
4680 count = i;
4682 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4684 phdr_size = elf_program_header_size (abfd);
4685 if (phdr_size == (bfd_size_type) -1)
4686 phdr_size = get_program_header_size (abfd, info);
4687 phdr_size += bed->s->sizeof_ehdr;
4688 /* phdr_size is compared to LMA values which are in bytes. */
4689 phdr_size /= opb;
4690 if (info != NULL)
4691 maxpagesize = info->maxpagesize;
4692 else
4693 maxpagesize = bed->maxpagesize;
4694 if (maxpagesize == 0)
4695 maxpagesize = 1;
4696 phdr_in_segment = info != NULL && info->load_phdrs;
4697 if (count != 0
4698 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4699 >= (phdr_size & (maxpagesize - 1))))
4700 /* For compatibility with old scripts that may not be using
4701 SIZEOF_HEADERS, add headers when it looks like space has
4702 been left for them. */
4703 phdr_in_segment = true;
4705 /* Build the mapping. */
4706 mfirst = NULL;
4707 pm = &mfirst;
4709 /* If we have a .interp section, then create a PT_PHDR segment for
4710 the program headers and a PT_INTERP segment for the .interp
4711 section. */
4712 s = bfd_get_section_by_name (abfd, ".interp");
4713 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4715 amt = sizeof (struct elf_segment_map);
4716 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4717 if (m == NULL)
4718 goto error_return;
4719 m->next = NULL;
4720 m->p_type = PT_PHDR;
4721 m->p_flags = PF_R;
4722 m->p_flags_valid = 1;
4723 m->includes_phdrs = 1;
4724 phdr_in_segment = true;
4725 *pm = m;
4726 pm = &m->next;
4728 amt = sizeof (struct elf_segment_map);
4729 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4730 if (m == NULL)
4731 goto error_return;
4732 m->next = NULL;
4733 m->p_type = PT_INTERP;
4734 m->count = 1;
4735 m->sections[0] = s;
4737 *pm = m;
4738 pm = &m->next;
4741 /* Look through the sections. We put sections in the same program
4742 segment when the start of the second section can be placed within
4743 a few bytes of the end of the first section. */
4744 last_hdr = NULL;
4745 last_size = 0;
4746 hdr_index = 0;
4747 writable = false;
4748 executable = false;
4749 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4750 if (dynsec != NULL
4751 && (dynsec->flags & SEC_LOAD) == 0)
4752 dynsec = NULL;
4754 if ((abfd->flags & D_PAGED) == 0)
4755 phdr_in_segment = false;
4757 /* Deal with -Ttext or something similar such that the first section
4758 is not adjacent to the program headers. This is an
4759 approximation, since at this point we don't know exactly how many
4760 program headers we will need. */
4761 if (phdr_in_segment && count > 0)
4763 bfd_vma phdr_lma; /* Bytes. */
4764 bool separate_phdr = false;
4766 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4767 if (info != NULL
4768 && info->separate_code
4769 && (sections[0]->flags & SEC_CODE) != 0)
4771 /* If data sections should be separate from code and
4772 thus not executable, and the first section is
4773 executable then put the file and program headers in
4774 their own PT_LOAD. */
4775 separate_phdr = true;
4776 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4777 == (sections[0]->lma & addr_mask & -maxpagesize)))
4779 /* The file and program headers are currently on the
4780 same page as the first section. Put them on the
4781 previous page if we can. */
4782 if (phdr_lma >= maxpagesize)
4783 phdr_lma -= maxpagesize;
4784 else
4785 separate_phdr = false;
4788 if ((sections[0]->lma & addr_mask) < phdr_lma
4789 || (sections[0]->lma & addr_mask) < phdr_size)
4790 /* If file and program headers would be placed at the end
4791 of memory then it's probably better to omit them. */
4792 phdr_in_segment = false;
4793 else if (phdr_lma < wrap_to)
4794 /* If a section wraps around to where we'll be placing
4795 file and program headers, then the headers will be
4796 overwritten. */
4797 phdr_in_segment = false;
4798 else if (separate_phdr)
4800 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4801 if (m == NULL)
4802 goto error_return;
4803 m->p_paddr = phdr_lma * opb;
4804 m->p_vaddr_offset
4805 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4806 m->p_paddr_valid = 1;
4807 *pm = m;
4808 pm = &m->next;
4809 phdr_in_segment = false;
4813 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4815 asection *hdr;
4816 bool new_segment;
4818 hdr = *hdrpp;
4820 /* See if this section and the last one will fit in the same
4821 segment. */
4823 if (last_hdr == NULL)
4825 /* If we don't have a segment yet, then we don't need a new
4826 one (we build the last one after this loop). */
4827 new_segment = false;
4829 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4831 /* If this section has a different relation between the
4832 virtual address and the load address, then we need a new
4833 segment. */
4834 new_segment = true;
4836 else if (hdr->lma < last_hdr->lma + last_size
4837 || last_hdr->lma + last_size < last_hdr->lma)
4839 /* If this section has a load address that makes it overlap
4840 the previous section, then we need a new segment. */
4841 new_segment = true;
4843 else if ((abfd->flags & D_PAGED) != 0
4844 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4845 == (hdr->lma & -maxpagesize)))
4847 /* If we are demand paged then we can't map two disk
4848 pages onto the same memory page. */
4849 new_segment = false;
4851 /* In the next test we have to be careful when last_hdr->lma is close
4852 to the end of the address space. If the aligned address wraps
4853 around to the start of the address space, then there are no more
4854 pages left in memory and it is OK to assume that the current
4855 section can be included in the current segment. */
4856 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4857 + maxpagesize > last_hdr->lma)
4858 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4859 + maxpagesize <= hdr->lma))
4861 /* If putting this section in this segment would force us to
4862 skip a page in the segment, then we need a new segment. */
4863 new_segment = true;
4865 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4866 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4868 /* We don't want to put a loaded section after a
4869 nonloaded (ie. bss style) section in the same segment
4870 as that will force the non-loaded section to be loaded.
4871 Consider .tbss sections as loaded for this purpose. */
4872 new_segment = true;
4874 else if ((abfd->flags & D_PAGED) == 0)
4876 /* If the file is not demand paged, which means that we
4877 don't require the sections to be correctly aligned in the
4878 file, then there is no other reason for a new segment. */
4879 new_segment = false;
4881 else if (info != NULL
4882 && info->separate_code
4883 && executable != ((hdr->flags & SEC_CODE) != 0))
4885 new_segment = true;
4887 else if (! writable
4888 && (hdr->flags & SEC_READONLY) == 0)
4890 /* We don't want to put a writable section in a read only
4891 segment. */
4892 new_segment = true;
4894 else
4896 /* Otherwise, we can use the same segment. */
4897 new_segment = false;
4900 /* Allow interested parties a chance to override our decision. */
4901 if (last_hdr != NULL
4902 && info != NULL
4903 && info->callbacks->override_segment_assignment != NULL)
4904 new_segment
4905 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4906 last_hdr,
4907 new_segment);
4909 if (! new_segment)
4911 if ((hdr->flags & SEC_READONLY) == 0)
4912 writable = true;
4913 if ((hdr->flags & SEC_CODE) != 0)
4914 executable = true;
4915 last_hdr = hdr;
4916 /* .tbss sections effectively have zero size. */
4917 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4918 continue;
4921 /* We need a new program segment. We must create a new program
4922 header holding all the sections from hdr_index until hdr. */
4924 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4925 if (m == NULL)
4926 goto error_return;
4928 *pm = m;
4929 pm = &m->next;
4931 if ((hdr->flags & SEC_READONLY) == 0)
4932 writable = true;
4933 else
4934 writable = false;
4936 if ((hdr->flags & SEC_CODE) == 0)
4937 executable = false;
4938 else
4939 executable = true;
4941 last_hdr = hdr;
4942 /* .tbss sections effectively have zero size. */
4943 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4944 hdr_index = i;
4945 phdr_in_segment = false;
4948 /* Create a final PT_LOAD program segment, but not if it's just
4949 for .tbss. */
4950 if (last_hdr != NULL
4951 && (i - hdr_index != 1
4952 || !IS_TBSS (last_hdr)))
4954 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4955 if (m == NULL)
4956 goto error_return;
4958 *pm = m;
4959 pm = &m->next;
4962 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4963 if (dynsec != NULL)
4965 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4966 if (m == NULL)
4967 goto error_return;
4968 *pm = m;
4969 pm = &m->next;
4972 /* For each batch of consecutive loadable SHT_NOTE sections,
4973 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4974 because if we link together nonloadable .note sections and
4975 loadable .note sections, we will generate two .note sections
4976 in the output file. */
4977 for (s = abfd->sections; s != NULL; s = s->next)
4979 if ((s->flags & SEC_LOAD) != 0
4980 && elf_section_type (s) == SHT_NOTE)
4982 asection *s2;
4983 unsigned int alignment_power = s->alignment_power;
4985 count = 1;
4986 for (s2 = s; s2->next != NULL; s2 = s2->next)
4988 if (s2->next->alignment_power == alignment_power
4989 && (s2->next->flags & SEC_LOAD) != 0
4990 && elf_section_type (s2->next) == SHT_NOTE
4991 && align_power (s2->lma + s2->size / opb,
4992 alignment_power)
4993 == s2->next->lma)
4994 count++;
4995 else
4996 break;
4998 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4999 amt += count * sizeof (asection *);
5000 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5001 if (m == NULL)
5002 goto error_return;
5003 m->next = NULL;
5004 m->p_type = PT_NOTE;
5005 m->count = count;
5006 while (count > 1)
5008 m->sections[m->count - count--] = s;
5009 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5010 s = s->next;
5012 m->sections[m->count - 1] = s;
5013 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5014 *pm = m;
5015 pm = &m->next;
5017 if (s->flags & SEC_THREAD_LOCAL)
5019 if (! tls_count)
5020 first_tls = s;
5021 tls_count++;
5023 if (first_mbind == NULL
5024 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5025 first_mbind = s;
5028 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5029 if (tls_count > 0)
5031 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5032 amt += tls_count * sizeof (asection *);
5033 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5034 if (m == NULL)
5035 goto error_return;
5036 m->next = NULL;
5037 m->p_type = PT_TLS;
5038 m->count = tls_count;
5039 /* Mandated PF_R. */
5040 m->p_flags = PF_R;
5041 m->p_flags_valid = 1;
5042 s = first_tls;
5043 for (i = 0; i < tls_count; ++i)
5045 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5047 _bfd_error_handler
5048 (_("%pB: TLS sections are not adjacent:"), abfd);
5049 s = first_tls;
5050 i = 0;
5051 while (i < tls_count)
5053 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5055 _bfd_error_handler (_(" TLS: %pA"), s);
5056 i++;
5058 else
5059 _bfd_error_handler (_(" non-TLS: %pA"), s);
5060 s = s->next;
5062 bfd_set_error (bfd_error_bad_value);
5063 goto error_return;
5065 m->sections[i] = s;
5066 s = s->next;
5069 *pm = m;
5070 pm = &m->next;
5073 if (first_mbind
5074 && (abfd->flags & D_PAGED) != 0
5075 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5076 for (s = first_mbind; s != NULL; s = s->next)
5077 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5078 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5080 /* Mandated PF_R. */
5081 unsigned long p_flags = PF_R;
5082 if ((s->flags & SEC_READONLY) == 0)
5083 p_flags |= PF_W;
5084 if ((s->flags & SEC_CODE) != 0)
5085 p_flags |= PF_X;
5087 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5088 m = bfd_zalloc (abfd, amt);
5089 if (m == NULL)
5090 goto error_return;
5091 m->next = NULL;
5092 m->p_type = (PT_GNU_MBIND_LO
5093 + elf_section_data (s)->this_hdr.sh_info);
5094 m->count = 1;
5095 m->p_flags_valid = 1;
5096 m->sections[0] = s;
5097 m->p_flags = p_flags;
5099 *pm = m;
5100 pm = &m->next;
5103 s = bfd_get_section_by_name (abfd,
5104 NOTE_GNU_PROPERTY_SECTION_NAME);
5105 if (s != NULL && s->size != 0)
5107 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5108 m = bfd_zalloc (abfd, amt);
5109 if (m == NULL)
5110 goto error_return;
5111 m->next = NULL;
5112 m->p_type = PT_GNU_PROPERTY;
5113 m->count = 1;
5114 m->p_flags_valid = 1;
5115 m->sections[0] = s;
5116 m->p_flags = PF_R;
5117 *pm = m;
5118 pm = &m->next;
5121 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5122 segment. */
5123 eh_frame_hdr = elf_eh_frame_hdr (info);
5124 if (eh_frame_hdr != NULL
5125 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5127 amt = sizeof (struct elf_segment_map);
5128 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5129 if (m == NULL)
5130 goto error_return;
5131 m->next = NULL;
5132 m->p_type = PT_GNU_EH_FRAME;
5133 m->count = 1;
5134 m->sections[0] = eh_frame_hdr->output_section;
5136 *pm = m;
5137 pm = &m->next;
5140 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5141 segment. */
5142 sframe = elf_sframe (abfd);
5143 if (sframe != NULL
5144 && (sframe->output_section->flags & SEC_LOAD) != 0
5145 && sframe->size != 0)
5147 amt = sizeof (struct elf_segment_map);
5148 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5149 if (m == NULL)
5150 goto error_return;
5151 m->next = NULL;
5152 m->p_type = PT_GNU_SFRAME;
5153 m->count = 1;
5154 m->sections[0] = sframe->output_section;
5156 *pm = m;
5157 pm = &m->next;
5160 if (elf_stack_flags (abfd))
5162 amt = sizeof (struct elf_segment_map);
5163 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5164 if (m == NULL)
5165 goto error_return;
5166 m->next = NULL;
5167 m->p_type = PT_GNU_STACK;
5168 m->p_flags = elf_stack_flags (abfd);
5169 m->p_align = bed->stack_align;
5170 m->p_flags_valid = 1;
5171 m->p_align_valid = m->p_align != 0;
5172 if (info->stacksize > 0)
5174 m->p_size = info->stacksize;
5175 m->p_size_valid = 1;
5178 *pm = m;
5179 pm = &m->next;
5182 if (info != NULL && info->relro)
5184 for (m = mfirst; m != NULL; m = m->next)
5186 if (m->p_type == PT_LOAD
5187 && m->count != 0
5188 && m->sections[0]->vma >= info->relro_start
5189 && m->sections[0]->vma < info->relro_end)
5191 i = m->count;
5192 while (--i != (unsigned) -1)
5194 if (m->sections[i]->size > 0
5195 && (m->sections[i]->flags & SEC_LOAD) != 0
5196 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5197 break;
5200 if (i != (unsigned) -1)
5201 break;
5205 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5206 if (m != NULL)
5208 amt = sizeof (struct elf_segment_map);
5209 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5210 if (m == NULL)
5211 goto error_return;
5212 m->next = NULL;
5213 m->p_type = PT_GNU_RELRO;
5214 *pm = m;
5215 pm = &m->next;
5219 free (sections);
5220 elf_seg_map (abfd) = mfirst;
5223 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5224 return false;
5226 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5227 ++count;
5228 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5230 return true;
5232 error_return:
5233 free (sections);
5234 return false;
5237 /* Sort sections by address. */
5239 static int
5240 elf_sort_sections (const void *arg1, const void *arg2)
5242 const asection *sec1 = *(const asection **) arg1;
5243 const asection *sec2 = *(const asection **) arg2;
5244 bfd_size_type size1, size2;
5246 /* Sort by LMA first, since this is the address used to
5247 place the section into a segment. */
5248 if (sec1->lma < sec2->lma)
5249 return -1;
5250 else if (sec1->lma > sec2->lma)
5251 return 1;
5253 /* Then sort by VMA. Normally the LMA and the VMA will be
5254 the same, and this will do nothing. */
5255 if (sec1->vma < sec2->vma)
5256 return -1;
5257 else if (sec1->vma > sec2->vma)
5258 return 1;
5260 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5262 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5263 && (x)->size != 0)
5265 if (TOEND (sec1))
5267 if (!TOEND (sec2))
5268 return 1;
5270 else if (TOEND (sec2))
5271 return -1;
5273 #undef TOEND
5275 /* Sort by size, to put zero sized sections
5276 before others at the same address. */
5278 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5279 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5281 if (size1 < size2)
5282 return -1;
5283 if (size1 > size2)
5284 return 1;
5286 return sec1->target_index - sec2->target_index;
5289 /* This qsort comparison functions sorts PT_LOAD segments first and
5290 by p_paddr, for assign_file_positions_for_load_sections. */
5292 static int
5293 elf_sort_segments (const void *arg1, const void *arg2)
5295 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5296 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5298 if (m1->p_type != m2->p_type)
5300 if (m1->p_type == PT_NULL)
5301 return 1;
5302 if (m2->p_type == PT_NULL)
5303 return -1;
5304 return m1->p_type < m2->p_type ? -1 : 1;
5306 if (m1->includes_filehdr != m2->includes_filehdr)
5307 return m1->includes_filehdr ? -1 : 1;
5308 if (m1->no_sort_lma != m2->no_sort_lma)
5309 return m1->no_sort_lma ? -1 : 1;
5310 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5312 bfd_vma lma1, lma2; /* Octets. */
5313 lma1 = 0;
5314 if (m1->p_paddr_valid)
5315 lma1 = m1->p_paddr;
5316 else if (m1->count != 0)
5318 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5319 m1->sections[0]);
5320 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5322 lma2 = 0;
5323 if (m2->p_paddr_valid)
5324 lma2 = m2->p_paddr;
5325 else if (m2->count != 0)
5327 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5328 m2->sections[0]);
5329 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5331 if (lma1 != lma2)
5332 return lma1 < lma2 ? -1 : 1;
5334 if (m1->idx != m2->idx)
5335 return m1->idx < m2->idx ? -1 : 1;
5336 return 0;
5339 /* Ian Lance Taylor writes:
5341 We shouldn't be using % with a negative signed number. That's just
5342 not good. We have to make sure either that the number is not
5343 negative, or that the number has an unsigned type. When the types
5344 are all the same size they wind up as unsigned. When file_ptr is a
5345 larger signed type, the arithmetic winds up as signed long long,
5346 which is wrong.
5348 What we're trying to say here is something like ``increase OFF by
5349 the least amount that will cause it to be equal to the VMA modulo
5350 the page size.'' */
5351 /* In other words, something like:
5353 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5354 off_offset = off % bed->maxpagesize;
5355 if (vma_offset < off_offset)
5356 adjustment = vma_offset + bed->maxpagesize - off_offset;
5357 else
5358 adjustment = vma_offset - off_offset;
5360 which can be collapsed into the expression below. */
5362 static file_ptr
5363 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5365 /* PR binutils/16199: Handle an alignment of zero. */
5366 if (maxpagesize == 0)
5367 maxpagesize = 1;
5368 return ((vma - off) % maxpagesize);
5371 static void
5372 print_segment_map (const struct elf_segment_map *m)
5374 unsigned int j;
5375 const char *pt = get_segment_type (m->p_type);
5376 char buf[32];
5378 if (pt == NULL)
5380 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5381 sprintf (buf, "LOPROC+%7.7x",
5382 (unsigned int) (m->p_type - PT_LOPROC));
5383 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5384 sprintf (buf, "LOOS+%7.7x",
5385 (unsigned int) (m->p_type - PT_LOOS));
5386 else
5387 snprintf (buf, sizeof (buf), "%8.8x",
5388 (unsigned int) m->p_type);
5389 pt = buf;
5391 fflush (stdout);
5392 fprintf (stderr, "%s:", pt);
5393 for (j = 0; j < m->count; j++)
5394 fprintf (stderr, " %s", m->sections [j]->name);
5395 putc ('\n',stderr);
5396 fflush (stderr);
5399 /* Assign file positions to the sections based on the mapping from
5400 sections to segments. This function also sets up some fields in
5401 the file header. */
5403 static bool
5404 assign_file_positions_for_load_sections (bfd *abfd,
5405 struct bfd_link_info *link_info)
5407 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5408 struct elf_segment_map *m;
5409 struct elf_segment_map *phdr_load_seg;
5410 Elf_Internal_Phdr *phdrs;
5411 Elf_Internal_Phdr *p;
5412 file_ptr off; /* Octets. */
5413 bfd_size_type maxpagesize;
5414 unsigned int alloc, actual;
5415 unsigned int i, j;
5416 struct elf_segment_map **sorted_seg_map;
5417 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5419 if (link_info == NULL
5420 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5421 return false;
5423 alloc = 0;
5424 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5425 m->idx = alloc++;
5427 if (alloc)
5429 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5430 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5432 else
5434 /* PR binutils/12467. */
5435 elf_elfheader (abfd)->e_phoff = 0;
5436 elf_elfheader (abfd)->e_phentsize = 0;
5439 elf_elfheader (abfd)->e_phnum = alloc;
5441 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5443 actual = alloc;
5444 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5446 else
5448 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5449 BFD_ASSERT (elf_program_header_size (abfd)
5450 == actual * bed->s->sizeof_phdr);
5451 BFD_ASSERT (actual >= alloc);
5454 if (alloc == 0)
5456 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5457 return true;
5460 /* We're writing the size in elf_program_header_size (abfd),
5461 see assign_file_positions_except_relocs, so make sure we have
5462 that amount allocated, with trailing space cleared.
5463 The variable alloc contains the computed need, while
5464 elf_program_header_size (abfd) contains the size used for the
5465 layout.
5466 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5467 where the layout is forced to according to a larger size in the
5468 last iterations for the testcase ld-elf/header. */
5469 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5470 + alloc * sizeof (*sorted_seg_map)));
5471 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5472 elf_tdata (abfd)->phdr = phdrs;
5473 if (phdrs == NULL)
5474 return false;
5476 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5478 sorted_seg_map[j] = m;
5479 /* If elf_segment_map is not from map_sections_to_segments, the
5480 sections may not be correctly ordered. NOTE: sorting should
5481 not be done to the PT_NOTE section of a corefile, which may
5482 contain several pseudo-sections artificially created by bfd.
5483 Sorting these pseudo-sections breaks things badly. */
5484 if (m->count > 1
5485 && !(elf_elfheader (abfd)->e_type == ET_CORE
5486 && m->p_type == PT_NOTE))
5488 for (i = 0; i < m->count; i++)
5489 m->sections[i]->target_index = i;
5490 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5491 elf_sort_sections);
5494 if (alloc > 1)
5495 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5496 elf_sort_segments);
5498 maxpagesize = 1;
5499 if ((abfd->flags & D_PAGED) != 0)
5501 if (link_info != NULL)
5502 maxpagesize = link_info->maxpagesize;
5503 else
5504 maxpagesize = bed->maxpagesize;
5507 /* Sections must map to file offsets past the ELF file header. */
5508 off = bed->s->sizeof_ehdr;
5509 /* And if one of the PT_LOAD headers doesn't include the program
5510 headers then we'll be mapping program headers in the usual
5511 position after the ELF file header. */
5512 phdr_load_seg = NULL;
5513 for (j = 0; j < alloc; j++)
5515 m = sorted_seg_map[j];
5516 if (m->p_type != PT_LOAD)
5517 break;
5518 if (m->includes_phdrs)
5520 phdr_load_seg = m;
5521 break;
5524 if (phdr_load_seg == NULL)
5525 off += actual * bed->s->sizeof_phdr;
5527 for (j = 0; j < alloc; j++)
5529 asection **secpp;
5530 bfd_vma off_adjust; /* Octets. */
5531 bool no_contents;
5532 bfd_size_type p_align;
5533 bool p_align_p;
5535 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5536 number of sections with contents contributing to both p_filesz
5537 and p_memsz, followed by a number of sections with no contents
5538 that just contribute to p_memsz. In this loop, OFF tracks next
5539 available file offset for PT_LOAD and PT_NOTE segments. */
5540 m = sorted_seg_map[j];
5541 p = phdrs + m->idx;
5542 p->p_type = m->p_type;
5543 p->p_flags = m->p_flags;
5544 p_align = bed->p_align;
5545 p_align_p = false;
5547 if (m->count == 0)
5548 p->p_vaddr = m->p_vaddr_offset * opb;
5549 else
5550 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5552 if (m->p_paddr_valid)
5553 p->p_paddr = m->p_paddr;
5554 else if (m->count == 0)
5555 p->p_paddr = 0;
5556 else
5557 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5559 if (p->p_type == PT_LOAD
5560 && (abfd->flags & D_PAGED) != 0)
5562 /* p_align in demand paged PT_LOAD segments effectively stores
5563 the maximum page size. When copying an executable with
5564 objcopy, we set m->p_align from the input file. Use this
5565 value for maxpagesize rather than bed->maxpagesize, which
5566 may be different. Note that we use maxpagesize for PT_TLS
5567 segment alignment later in this function, so we are relying
5568 on at least one PT_LOAD segment appearing before a PT_TLS
5569 segment. */
5570 if (m->p_align_valid)
5571 maxpagesize = m->p_align;
5572 else if (p_align != 0
5573 && (link_info == NULL
5574 || !link_info->maxpagesize_is_set))
5575 /* Set p_align to the default p_align value while laying
5576 out segments aligning to the maximum page size or the
5577 largest section alignment. The run-time loader can
5578 align segments to the default p_align value or the
5579 maximum page size, depending on system page size. */
5580 p_align_p = true;
5582 p->p_align = maxpagesize;
5584 else if (m->p_align_valid)
5585 p->p_align = m->p_align;
5586 else if (m->count == 0)
5587 p->p_align = 1 << bed->s->log_file_align;
5589 if (m == phdr_load_seg)
5591 if (!m->includes_filehdr)
5592 p->p_offset = off;
5593 off += actual * bed->s->sizeof_phdr;
5596 no_contents = false;
5597 off_adjust = 0;
5598 if (p->p_type == PT_LOAD
5599 && m->count > 0)
5601 bfd_size_type align; /* Bytes. */
5602 unsigned int align_power = 0;
5604 if (m->p_align_valid)
5605 align = p->p_align;
5606 else
5608 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5610 unsigned int secalign;
5612 secalign = bfd_section_alignment (*secpp);
5613 if (secalign > align_power)
5614 align_power = secalign;
5616 align = (bfd_size_type) 1 << align_power;
5617 if (align < maxpagesize)
5619 /* If a section requires alignment higher than the
5620 default p_align value, don't set p_align to the
5621 default p_align value. */
5622 if (align > p_align)
5623 p_align_p = false;
5624 align = maxpagesize;
5626 else
5628 /* If a section requires alignment higher than the
5629 maximum page size, set p_align to the section
5630 alignment. */
5631 p_align_p = true;
5632 p_align = align;
5636 for (i = 0; i < m->count; i++)
5637 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5638 /* If we aren't making room for this section, then
5639 it must be SHT_NOBITS regardless of what we've
5640 set via struct bfd_elf_special_section. */
5641 elf_section_type (m->sections[i]) = SHT_NOBITS;
5643 /* Find out whether this segment contains any loadable
5644 sections. */
5645 no_contents = true;
5646 for (i = 0; i < m->count; i++)
5647 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5649 no_contents = false;
5650 break;
5653 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5655 /* Broken hardware and/or kernel require that files do not
5656 map the same page with different permissions on some hppa
5657 processors. */
5658 if (j != 0
5659 && (abfd->flags & D_PAGED) != 0
5660 && bed->no_page_alias
5661 && (off & (maxpagesize - 1)) != 0
5662 && ((off & -maxpagesize)
5663 == ((off + off_adjust) & -maxpagesize)))
5664 off_adjust += maxpagesize;
5665 off += off_adjust;
5666 if (no_contents)
5668 /* We shouldn't need to align the segment on disk since
5669 the segment doesn't need file space, but the gABI
5670 arguably requires the alignment and glibc ld.so
5671 checks it. So to comply with the alignment
5672 requirement but not waste file space, we adjust
5673 p_offset for just this segment. (OFF_ADJUST is
5674 subtracted from OFF later.) This may put p_offset
5675 past the end of file, but that shouldn't matter. */
5677 else
5678 off_adjust = 0;
5680 /* Make sure the .dynamic section is the first section in the
5681 PT_DYNAMIC segment. */
5682 else if (p->p_type == PT_DYNAMIC
5683 && m->count > 1
5684 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5686 _bfd_error_handler
5687 (_("%pB: The first section in the PT_DYNAMIC segment"
5688 " is not the .dynamic section"),
5689 abfd);
5690 bfd_set_error (bfd_error_bad_value);
5691 return false;
5693 /* Set the note section type to SHT_NOTE. */
5694 else if (p->p_type == PT_NOTE)
5695 for (i = 0; i < m->count; i++)
5696 elf_section_type (m->sections[i]) = SHT_NOTE;
5698 if (m->includes_filehdr)
5700 if (!m->p_flags_valid)
5701 p->p_flags |= PF_R;
5702 p->p_filesz = bed->s->sizeof_ehdr;
5703 p->p_memsz = bed->s->sizeof_ehdr;
5704 if (p->p_type == PT_LOAD)
5706 if (m->count > 0)
5708 if (p->p_vaddr < (bfd_vma) off
5709 || (!m->p_paddr_valid
5710 && p->p_paddr < (bfd_vma) off))
5712 _bfd_error_handler
5713 (_("%pB: not enough room for program headers,"
5714 " try linking with -N"),
5715 abfd);
5716 bfd_set_error (bfd_error_bad_value);
5717 return false;
5719 p->p_vaddr -= off;
5720 if (!m->p_paddr_valid)
5721 p->p_paddr -= off;
5724 else if (sorted_seg_map[0]->includes_filehdr)
5726 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5727 p->p_vaddr = filehdr->p_vaddr;
5728 if (!m->p_paddr_valid)
5729 p->p_paddr = filehdr->p_paddr;
5733 if (m->includes_phdrs)
5735 if (!m->p_flags_valid)
5736 p->p_flags |= PF_R;
5737 p->p_filesz += actual * bed->s->sizeof_phdr;
5738 p->p_memsz += actual * bed->s->sizeof_phdr;
5739 if (!m->includes_filehdr)
5741 if (p->p_type == PT_LOAD)
5743 elf_elfheader (abfd)->e_phoff = p->p_offset;
5744 if (m->count > 0)
5746 p->p_vaddr -= off - p->p_offset;
5747 if (!m->p_paddr_valid)
5748 p->p_paddr -= off - p->p_offset;
5751 else if (phdr_load_seg != NULL)
5753 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5754 bfd_vma phdr_off = 0; /* Octets. */
5755 if (phdr_load_seg->includes_filehdr)
5756 phdr_off = bed->s->sizeof_ehdr;
5757 p->p_vaddr = phdr->p_vaddr + phdr_off;
5758 if (!m->p_paddr_valid)
5759 p->p_paddr = phdr->p_paddr + phdr_off;
5760 p->p_offset = phdr->p_offset + phdr_off;
5762 else
5763 p->p_offset = bed->s->sizeof_ehdr;
5767 if (p->p_type == PT_LOAD
5768 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5770 if (!m->includes_filehdr && !m->includes_phdrs)
5772 p->p_offset = off;
5773 if (no_contents)
5775 /* Put meaningless p_offset for PT_LOAD segments
5776 without file contents somewhere within the first
5777 page, in an attempt to not point past EOF. */
5778 bfd_size_type align = maxpagesize;
5779 if (align < p->p_align)
5780 align = p->p_align;
5781 if (align < 1)
5782 align = 1;
5783 p->p_offset = off % align;
5786 else
5788 file_ptr adjust; /* Octets. */
5790 adjust = off - (p->p_offset + p->p_filesz);
5791 if (!no_contents)
5792 p->p_filesz += adjust;
5793 p->p_memsz += adjust;
5797 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5798 maps. Set filepos for sections in PT_LOAD segments, and in
5799 core files, for sections in PT_NOTE segments.
5800 assign_file_positions_for_non_load_sections will set filepos
5801 for other sections and update p_filesz for other segments. */
5802 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5804 asection *sec;
5805 bfd_size_type align;
5806 Elf_Internal_Shdr *this_hdr;
5808 sec = *secpp;
5809 this_hdr = &elf_section_data (sec)->this_hdr;
5810 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5812 if ((p->p_type == PT_LOAD
5813 || p->p_type == PT_TLS)
5814 && (this_hdr->sh_type != SHT_NOBITS
5815 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5816 && ((this_hdr->sh_flags & SHF_TLS) == 0
5817 || p->p_type == PT_TLS))))
5819 bfd_vma p_start = p->p_paddr; /* Octets. */
5820 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5821 bfd_vma s_start = sec->lma * opb; /* Octets. */
5822 bfd_vma adjust = s_start - p_end; /* Octets. */
5824 if (adjust != 0
5825 && (s_start < p_end
5826 || p_end < p_start))
5828 _bfd_error_handler
5829 /* xgettext:c-format */
5830 (_("%pB: section %pA lma %#" PRIx64
5831 " adjusted to %#" PRIx64),
5832 abfd, sec, (uint64_t) s_start / opb,
5833 (uint64_t) p_end / opb);
5834 adjust = 0;
5835 sec->lma = p_end / opb;
5837 p->p_memsz += adjust;
5839 if (p->p_type == PT_LOAD)
5841 if (this_hdr->sh_type != SHT_NOBITS)
5843 off_adjust = 0;
5844 if (p->p_filesz + adjust < p->p_memsz)
5846 /* We have a PROGBITS section following NOBITS ones.
5847 Allocate file space for the NOBITS section(s).
5848 We don't need to write out the zeros, posix
5849 fseek past the end of data already written
5850 followed by a write at that location is
5851 guaranteed to result in zeros being read
5852 from the gap. */
5853 adjust = p->p_memsz - p->p_filesz;
5856 /* We only adjust sh_offset in SHT_NOBITS sections
5857 as would seem proper for their address when the
5858 section is first in the segment. sh_offset
5859 doesn't really have any significance for
5860 SHT_NOBITS anyway, apart from a notional position
5861 relative to other sections. Historically we
5862 didn't bother with adjusting sh_offset and some
5863 programs depend on it not being adjusted. See
5864 pr12921 and pr25662. */
5865 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5867 off += adjust;
5868 if (this_hdr->sh_type == SHT_NOBITS)
5869 off_adjust += adjust;
5872 if (this_hdr->sh_type != SHT_NOBITS)
5873 p->p_filesz += adjust;
5876 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5878 /* The section at i == 0 is the one that actually contains
5879 everything. */
5880 if (i == 0)
5882 this_hdr->sh_offset = sec->filepos = off;
5883 off += this_hdr->sh_size;
5884 p->p_filesz = this_hdr->sh_size;
5885 p->p_memsz = 0;
5886 p->p_align = 1;
5888 else
5890 /* The rest are fake sections that shouldn't be written. */
5891 sec->filepos = 0;
5892 sec->size = 0;
5893 sec->flags = 0;
5894 continue;
5897 else
5899 if (p->p_type == PT_LOAD)
5901 this_hdr->sh_offset = sec->filepos = off;
5902 if (this_hdr->sh_type != SHT_NOBITS)
5903 off += this_hdr->sh_size;
5905 else if (this_hdr->sh_type == SHT_NOBITS
5906 && (this_hdr->sh_flags & SHF_TLS) != 0
5907 && this_hdr->sh_offset == 0)
5909 /* This is a .tbss section that didn't get a PT_LOAD.
5910 (See _bfd_elf_map_sections_to_segments "Create a
5911 final PT_LOAD".) Set sh_offset to the value it
5912 would have if we had created a zero p_filesz and
5913 p_memsz PT_LOAD header for the section. This
5914 also makes the PT_TLS header have the same
5915 p_offset value. */
5916 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5917 off, align);
5918 this_hdr->sh_offset = sec->filepos = off + adjust;
5921 if (this_hdr->sh_type != SHT_NOBITS)
5923 p->p_filesz += this_hdr->sh_size;
5924 /* A load section without SHF_ALLOC is something like
5925 a note section in a PT_NOTE segment. These take
5926 file space but are not loaded into memory. */
5927 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5928 p->p_memsz += this_hdr->sh_size;
5930 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5932 if (p->p_type == PT_TLS)
5933 p->p_memsz += this_hdr->sh_size;
5935 /* .tbss is special. It doesn't contribute to p_memsz of
5936 normal segments. */
5937 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5938 p->p_memsz += this_hdr->sh_size;
5941 if (align > p->p_align
5942 && !m->p_align_valid
5943 && (p->p_type != PT_LOAD
5944 || (abfd->flags & D_PAGED) == 0))
5945 p->p_align = align;
5948 if (!m->p_flags_valid)
5950 p->p_flags |= PF_R;
5951 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5952 p->p_flags |= PF_X;
5953 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5954 p->p_flags |= PF_W;
5958 off -= off_adjust;
5960 /* PR ld/20815 - Check that the program header segment, if
5961 present, will be loaded into memory. */
5962 if (p->p_type == PT_PHDR
5963 && phdr_load_seg == NULL
5964 && !(bed->elf_backend_allow_non_load_phdr != NULL
5965 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5967 /* The fix for this error is usually to edit the linker script being
5968 used and set up the program headers manually. Either that or
5969 leave room for the headers at the start of the SECTIONS. */
5970 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5971 " by LOAD segment"),
5972 abfd);
5973 if (link_info == NULL)
5974 return false;
5975 /* Arrange for the linker to exit with an error, deleting
5976 the output file unless --noinhibit-exec is given. */
5977 link_info->callbacks->info ("%X");
5980 /* Check that all sections are in a PT_LOAD segment.
5981 Don't check funky gdb generated core files. */
5982 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5984 bool check_vma = true;
5986 for (i = 1; i < m->count; i++)
5987 if (m->sections[i]->vma == m->sections[i - 1]->vma
5988 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5989 ->this_hdr), p) != 0
5990 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5991 ->this_hdr), p) != 0)
5993 /* Looks like we have overlays packed into the segment. */
5994 check_vma = false;
5995 break;
5998 for (i = 0; i < m->count; i++)
6000 Elf_Internal_Shdr *this_hdr;
6001 asection *sec;
6003 sec = m->sections[i];
6004 this_hdr = &(elf_section_data(sec)->this_hdr);
6005 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6006 && !ELF_TBSS_SPECIAL (this_hdr, p))
6008 _bfd_error_handler
6009 /* xgettext:c-format */
6010 (_("%pB: section `%pA' can't be allocated in segment %d"),
6011 abfd, sec, j);
6012 print_segment_map (m);
6016 if (p_align_p)
6017 p->p_align = p_align;
6021 elf_next_file_pos (abfd) = off;
6023 if (link_info != NULL
6024 && phdr_load_seg != NULL
6025 && phdr_load_seg->includes_filehdr)
6027 /* There is a segment that contains both the file headers and the
6028 program headers, so provide a symbol __ehdr_start pointing there.
6029 A program can use this to examine itself robustly. */
6031 struct elf_link_hash_entry *hash
6032 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6033 false, false, true);
6034 /* If the symbol was referenced and not defined, define it. */
6035 if (hash != NULL
6036 && (hash->root.type == bfd_link_hash_new
6037 || hash->root.type == bfd_link_hash_undefined
6038 || hash->root.type == bfd_link_hash_undefweak
6039 || hash->root.type == bfd_link_hash_common))
6041 asection *s = NULL;
6042 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6044 if (phdr_load_seg->count != 0)
6045 /* The segment contains sections, so use the first one. */
6046 s = phdr_load_seg->sections[0];
6047 else
6048 /* Use the first (i.e. lowest-addressed) section in any segment. */
6049 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6050 if (m->p_type == PT_LOAD && m->count != 0)
6052 s = m->sections[0];
6053 break;
6056 if (s != NULL)
6058 hash->root.u.def.value = filehdr_vaddr - s->vma;
6059 hash->root.u.def.section = s;
6061 else
6063 hash->root.u.def.value = filehdr_vaddr;
6064 hash->root.u.def.section = bfd_abs_section_ptr;
6067 hash->root.type = bfd_link_hash_defined;
6068 hash->def_regular = 1;
6069 hash->non_elf = 0;
6073 return true;
6076 /* Determine if a bfd is a debuginfo file. Unfortunately there
6077 is no defined method for detecting such files, so we have to
6078 use heuristics instead. */
6080 bool
6081 is_debuginfo_file (bfd *abfd)
6083 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6084 return false;
6086 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6087 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6088 Elf_Internal_Shdr **headerp;
6090 for (headerp = start_headers; headerp < end_headers; headerp ++)
6092 Elf_Internal_Shdr *header = * headerp;
6094 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6095 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6096 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6097 && header->sh_type != SHT_NOBITS
6098 && header->sh_type != SHT_NOTE)
6099 return false;
6102 return true;
6105 /* Assign file positions for other sections, except for compressed debug
6106 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6108 static bool
6109 assign_file_positions_for_non_load_sections (bfd *abfd,
6110 struct bfd_link_info *link_info)
6112 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6113 Elf_Internal_Shdr **i_shdrpp;
6114 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6115 Elf_Internal_Phdr *phdrs;
6116 Elf_Internal_Phdr *p;
6117 struct elf_segment_map *m;
6118 file_ptr off;
6119 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6120 bfd_vma maxpagesize;
6122 if (link_info != NULL)
6123 maxpagesize = link_info->maxpagesize;
6124 else
6125 maxpagesize = bed->maxpagesize;
6126 i_shdrpp = elf_elfsections (abfd);
6127 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6128 off = elf_next_file_pos (abfd);
6129 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6131 Elf_Internal_Shdr *hdr;
6132 bfd_vma align;
6134 hdr = *hdrpp;
6135 if (hdr->bfd_section != NULL
6136 && (hdr->bfd_section->filepos != 0
6137 || (hdr->sh_type == SHT_NOBITS
6138 && hdr->contents == NULL)))
6139 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6140 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6142 if (hdr->sh_size != 0
6143 /* PR 24717 - debuginfo files are known to be not strictly
6144 compliant with the ELF standard. In particular they often
6145 have .note.gnu.property sections that are outside of any
6146 loadable segment. This is not a problem for such files,
6147 so do not warn about them. */
6148 && ! is_debuginfo_file (abfd))
6149 _bfd_error_handler
6150 /* xgettext:c-format */
6151 (_("%pB: warning: allocated section `%s' not in segment"),
6152 abfd,
6153 (hdr->bfd_section == NULL
6154 ? "*unknown*"
6155 : hdr->bfd_section->name));
6156 /* We don't need to page align empty sections. */
6157 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6158 align = maxpagesize;
6159 else
6160 align = hdr->sh_addralign & -hdr->sh_addralign;
6161 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6162 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6163 false);
6165 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6166 && hdr->bfd_section == NULL)
6167 /* We don't know the offset of these sections yet:
6168 their size has not been decided. */
6169 || (abfd->is_linker_output
6170 && hdr->bfd_section != NULL
6171 && (hdr->sh_name == -1u
6172 || bfd_section_is_ctf (hdr->bfd_section)))
6173 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6174 || (elf_symtab_shndx_list (abfd) != NULL
6175 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6176 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6177 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6178 hdr->sh_offset = -1;
6179 else
6180 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6182 elf_next_file_pos (abfd) = off;
6184 /* Now that we have set the section file positions, we can set up
6185 the file positions for the non PT_LOAD segments. */
6186 phdrs = elf_tdata (abfd)->phdr;
6187 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6189 if (p->p_type == PT_GNU_RELRO)
6191 bfd_vma start, end; /* Bytes. */
6192 bool ok;
6194 if (link_info != NULL)
6196 /* During linking the range of the RELRO segment is passed
6197 in link_info. Note that there may be padding between
6198 relro_start and the first RELRO section. */
6199 start = link_info->relro_start;
6200 end = link_info->relro_end;
6202 else if (m->count != 0)
6204 if (!m->p_size_valid)
6205 abort ();
6206 start = m->sections[0]->vma;
6207 end = start + m->p_size / opb;
6209 else
6211 start = 0;
6212 end = 0;
6215 ok = false;
6216 if (start < end)
6218 struct elf_segment_map *lm;
6219 const Elf_Internal_Phdr *lp;
6220 unsigned int i;
6222 /* Find a LOAD segment containing a section in the RELRO
6223 segment. */
6224 for (lm = elf_seg_map (abfd), lp = phdrs;
6225 lm != NULL;
6226 lm = lm->next, lp++)
6228 if (lp->p_type == PT_LOAD
6229 && lm->count != 0
6230 && (lm->sections[lm->count - 1]->vma
6231 + (!IS_TBSS (lm->sections[lm->count - 1])
6232 ? lm->sections[lm->count - 1]->size / opb
6233 : 0)) > start
6234 && lm->sections[0]->vma < end)
6235 break;
6238 if (lm != NULL)
6240 /* Find the section starting the RELRO segment. */
6241 for (i = 0; i < lm->count; i++)
6243 asection *s = lm->sections[i];
6244 if (s->vma >= start
6245 && s->vma < end
6246 && s->size != 0)
6247 break;
6250 if (i < lm->count)
6252 p->p_vaddr = lm->sections[i]->vma * opb;
6253 p->p_paddr = lm->sections[i]->lma * opb;
6254 p->p_offset = lm->sections[i]->filepos;
6255 p->p_memsz = end * opb - p->p_vaddr;
6256 p->p_filesz = p->p_memsz;
6258 /* The RELRO segment typically ends a few bytes
6259 into .got.plt but other layouts are possible.
6260 In cases where the end does not match any
6261 loaded section (for instance is in file
6262 padding), trim p_filesz back to correspond to
6263 the end of loaded section contents. */
6264 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6265 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6267 /* Preserve the alignment and flags if they are
6268 valid. The gold linker generates RW/4 for
6269 the PT_GNU_RELRO section. It is better for
6270 objcopy/strip to honor these attributes
6271 otherwise gdb will choke when using separate
6272 debug files. */
6273 if (!m->p_align_valid)
6274 p->p_align = 1;
6275 if (!m->p_flags_valid)
6276 p->p_flags = PF_R;
6277 ok = true;
6282 if (!ok)
6284 if (link_info != NULL)
6285 _bfd_error_handler
6286 (_("%pB: warning: unable to allocate any sections"
6287 " to PT_GNU_RELRO segment"),
6288 abfd);
6289 memset (p, 0, sizeof *p);
6292 else if (p->p_type == PT_GNU_STACK)
6294 if (m->p_size_valid)
6295 p->p_memsz = m->p_size;
6297 else if (m->count != 0)
6299 unsigned int i;
6301 if (p->p_type != PT_LOAD
6302 && (p->p_type != PT_NOTE
6303 || bfd_get_format (abfd) != bfd_core))
6305 /* A user specified segment layout may include a PHDR
6306 segment that overlaps with a LOAD segment... */
6307 if (p->p_type == PT_PHDR)
6309 m->count = 0;
6310 continue;
6313 if (m->includes_filehdr || m->includes_phdrs)
6315 /* PR 17512: file: 2195325e. */
6316 _bfd_error_handler
6317 (_("%pB: error: non-load segment %d includes file header "
6318 "and/or program header"),
6319 abfd, (int) (p - phdrs));
6320 return false;
6323 p->p_filesz = 0;
6324 p->p_offset = m->sections[0]->filepos;
6325 for (i = m->count; i-- != 0;)
6327 asection *sect = m->sections[i];
6328 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6329 if (hdr->sh_type != SHT_NOBITS)
6331 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6332 /* NB: p_memsz of the loadable PT_NOTE segment
6333 should be the same as p_filesz. */
6334 if (p->p_type == PT_NOTE
6335 && (hdr->sh_flags & SHF_ALLOC) != 0)
6336 p->p_memsz = p->p_filesz;
6337 break;
6344 return true;
6347 static elf_section_list *
6348 find_section_in_list (unsigned int i, elf_section_list * list)
6350 for (;list != NULL; list = list->next)
6351 if (list->ndx == i)
6352 break;
6353 return list;
6356 /* Work out the file positions of all the sections. This is called by
6357 _bfd_elf_compute_section_file_positions. All the section sizes and
6358 VMAs must be known before this is called.
6360 Reloc sections come in two flavours: Those processed specially as
6361 "side-channel" data attached to a section to which they apply, and
6362 those that bfd doesn't process as relocations. The latter sort are
6363 stored in a normal bfd section by bfd_section_from_shdr. We don't
6364 consider the former sort here, unless they form part of the loadable
6365 image. Reloc sections not assigned here (and compressed debugging
6366 sections and CTF sections which nothing else in the file can rely
6367 upon) will be handled later by assign_file_positions_for_relocs.
6369 We also don't set the positions of the .symtab and .strtab here. */
6371 static bool
6372 assign_file_positions_except_relocs (bfd *abfd,
6373 struct bfd_link_info *link_info)
6375 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6376 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6378 unsigned int alloc;
6380 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6381 && bfd_get_format (abfd) != bfd_core)
6383 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6384 unsigned int num_sec = elf_numsections (abfd);
6385 Elf_Internal_Shdr **hdrpp;
6386 unsigned int i;
6387 file_ptr off;
6389 /* Start after the ELF header. */
6390 off = i_ehdrp->e_ehsize;
6392 /* We are not creating an executable, which means that we are
6393 not creating a program header, and that the actual order of
6394 the sections in the file is unimportant. */
6395 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6397 Elf_Internal_Shdr *hdr;
6399 hdr = *hdrpp;
6400 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6401 && hdr->bfd_section == NULL)
6402 /* Do not assign offsets for these sections yet: we don't know
6403 their sizes. */
6404 || (abfd->is_linker_output
6405 && hdr->bfd_section != NULL
6406 && (hdr->sh_name == -1u
6407 || bfd_section_is_ctf (hdr->bfd_section)))
6408 || i == elf_onesymtab (abfd)
6409 || (elf_symtab_shndx_list (abfd) != NULL
6410 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6411 || i == elf_strtab_sec (abfd)
6412 || i == elf_shstrtab_sec (abfd))
6414 hdr->sh_offset = -1;
6416 else
6417 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6420 elf_next_file_pos (abfd) = off;
6421 elf_program_header_size (abfd) = 0;
6423 else
6425 /* Assign file positions for the loaded sections based on the
6426 assignment of sections to segments. */
6427 if (!assign_file_positions_for_load_sections (abfd, link_info))
6428 return false;
6430 /* And for non-load sections. */
6431 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6432 return false;
6435 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6436 return false;
6438 /* Write out the program headers. */
6439 alloc = i_ehdrp->e_phnum;
6440 if (alloc != 0)
6442 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6444 /* Memory resident segments with non-zero size and RWX
6445 permissions are a security risk, so we generate a warning
6446 here if we are creating any. */
6447 unsigned int i;
6449 for (i = 0; i < alloc; i++)
6451 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6453 if (phdr->p_memsz == 0)
6454 continue;
6456 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
6457 _bfd_error_handler (_("warning: %pB has a TLS segment"
6458 " with execute permission"),
6459 abfd);
6460 else if (phdr->p_type == PT_LOAD
6461 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6462 == (PF_R | PF_W | PF_X)))
6463 _bfd_error_handler (_("warning: %pB has a LOAD segment"
6464 " with RWX permissions"),
6465 abfd);
6469 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6470 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6471 return false;
6474 return true;
6477 bool
6478 _bfd_elf_init_file_header (bfd *abfd,
6479 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6481 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6482 struct elf_strtab_hash *shstrtab;
6483 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6485 i_ehdrp = elf_elfheader (abfd);
6487 shstrtab = _bfd_elf_strtab_init ();
6488 if (shstrtab == NULL)
6489 return false;
6491 elf_shstrtab (abfd) = shstrtab;
6493 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6494 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6495 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6496 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6498 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6499 i_ehdrp->e_ident[EI_DATA] =
6500 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6501 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6503 if ((abfd->flags & DYNAMIC) != 0)
6504 i_ehdrp->e_type = ET_DYN;
6505 else if ((abfd->flags & EXEC_P) != 0)
6506 i_ehdrp->e_type = ET_EXEC;
6507 else if (bfd_get_format (abfd) == bfd_core)
6508 i_ehdrp->e_type = ET_CORE;
6509 else
6510 i_ehdrp->e_type = ET_REL;
6512 switch (bfd_get_arch (abfd))
6514 case bfd_arch_unknown:
6515 i_ehdrp->e_machine = EM_NONE;
6516 break;
6518 /* There used to be a long list of cases here, each one setting
6519 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6520 in the corresponding bfd definition. To avoid duplication,
6521 the switch was removed. Machines that need special handling
6522 can generally do it in elf_backend_final_write_processing(),
6523 unless they need the information earlier than the final write.
6524 Such need can generally be supplied by replacing the tests for
6525 e_machine with the conditions used to determine it. */
6526 default:
6527 i_ehdrp->e_machine = bed->elf_machine_code;
6530 i_ehdrp->e_version = bed->s->ev_current;
6531 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6533 /* No program header, for now. */
6534 i_ehdrp->e_phoff = 0;
6535 i_ehdrp->e_phentsize = 0;
6536 i_ehdrp->e_phnum = 0;
6538 /* Each bfd section is section header entry. */
6539 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6540 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6542 elf_tdata (abfd)->symtab_hdr.sh_name =
6543 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6544 elf_tdata (abfd)->strtab_hdr.sh_name =
6545 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6546 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6547 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6548 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6549 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6550 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6551 return false;
6553 return true;
6556 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6558 FIXME: We used to have code here to sort the PT_LOAD segments into
6559 ascending order, as per the ELF spec. But this breaks some programs,
6560 including the Linux kernel. But really either the spec should be
6561 changed or the programs updated. */
6563 bool
6564 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6566 if (link_info != NULL && bfd_link_pie (link_info))
6568 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6569 unsigned int num_segments = i_ehdrp->e_phnum;
6570 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6571 Elf_Internal_Phdr *segment = tdata->phdr;
6572 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6574 /* Find the lowest p_vaddr in PT_LOAD segments. */
6575 bfd_vma p_vaddr = (bfd_vma) -1;
6576 for (; segment < end_segment; segment++)
6577 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6578 p_vaddr = segment->p_vaddr;
6580 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6581 segments is non-zero. */
6582 if (p_vaddr)
6583 i_ehdrp->e_type = ET_EXEC;
6585 return true;
6588 /* Assign file positions for all the reloc sections which are not part
6589 of the loadable file image, and the file position of section headers. */
6591 static bool
6592 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6594 file_ptr off;
6595 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6596 Elf_Internal_Shdr *shdrp;
6597 Elf_Internal_Ehdr *i_ehdrp;
6598 const struct elf_backend_data *bed;
6600 off = elf_next_file_pos (abfd);
6602 shdrpp = elf_elfsections (abfd);
6603 end_shdrpp = shdrpp + elf_numsections (abfd);
6604 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6606 shdrp = *shdrpp;
6607 if (shdrp->sh_offset == -1)
6609 asection *sec = shdrp->bfd_section;
6610 if (sec == NULL
6611 || shdrp->sh_type == SHT_REL
6612 || shdrp->sh_type == SHT_RELA)
6614 else if (bfd_section_is_ctf (sec))
6616 /* Update section size and contents. */
6617 shdrp->sh_size = sec->size;
6618 shdrp->contents = sec->contents;
6620 else if (shdrp->sh_name == -1u)
6622 const char *name = sec->name;
6623 struct bfd_elf_section_data *d;
6625 /* Compress DWARF debug sections. */
6626 if (!bfd_compress_section (abfd, sec, shdrp->contents))
6627 return false;
6629 if (sec->compress_status == COMPRESS_SECTION_DONE
6630 && (abfd->flags & BFD_COMPRESS_GABI) == 0
6631 && name[1] == 'd')
6633 /* If section is compressed with zlib-gnu, convert
6634 section name from .debug_* to .zdebug_*. */
6635 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
6636 if (new_name == NULL)
6637 return false;
6638 name = new_name;
6640 /* Add section name to section name section. */
6641 shdrp->sh_name
6642 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6643 name, false);
6644 d = elf_section_data (sec);
6646 /* Add reloc section name to section name section. */
6647 if (d->rel.hdr
6648 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
6649 name, false))
6650 return false;
6651 if (d->rela.hdr
6652 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
6653 name, true))
6654 return false;
6656 /* Update section size and contents. */
6657 shdrp->sh_size = sec->size;
6658 shdrp->contents = sec->contents;
6659 sec->contents = NULL;
6662 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6666 /* Place section name section after DWARF debug sections have been
6667 compressed. */
6668 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6669 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6670 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6671 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6673 /* Place the section headers. */
6674 i_ehdrp = elf_elfheader (abfd);
6675 bed = get_elf_backend_data (abfd);
6676 off = align_file_position (off, 1 << bed->s->log_file_align);
6677 i_ehdrp->e_shoff = off;
6678 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6679 elf_next_file_pos (abfd) = off;
6681 return true;
6684 bool
6685 _bfd_elf_write_object_contents (bfd *abfd)
6687 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6688 Elf_Internal_Shdr **i_shdrp;
6689 bool failed;
6690 unsigned int count, num_sec;
6691 struct elf_obj_tdata *t;
6693 if (! abfd->output_has_begun
6694 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6695 return false;
6696 /* Do not rewrite ELF data when the BFD has been opened for update.
6697 abfd->output_has_begun was set to TRUE on opening, so creation of
6698 new sections, and modification of existing section sizes was
6699 restricted. This means the ELF header, program headers and
6700 section headers can't have changed. If the contents of any
6701 sections has been modified, then those changes have already been
6702 written to the BFD. */
6703 else if (abfd->direction == both_direction)
6705 BFD_ASSERT (abfd->output_has_begun);
6706 return true;
6709 i_shdrp = elf_elfsections (abfd);
6711 failed = false;
6712 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6713 if (failed)
6714 return false;
6716 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6717 return false;
6719 /* After writing the headers, we need to write the sections too... */
6720 num_sec = elf_numsections (abfd);
6721 for (count = 1; count < num_sec; count++)
6723 i_shdrp[count]->sh_name
6724 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6725 i_shdrp[count]->sh_name);
6726 if (bed->elf_backend_section_processing)
6727 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6728 return false;
6729 if (i_shdrp[count]->contents)
6731 bfd_size_type amt = i_shdrp[count]->sh_size;
6733 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6734 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6735 return false;
6739 /* Write out the section header names. */
6740 t = elf_tdata (abfd);
6741 if (elf_shstrtab (abfd) != NULL
6742 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6743 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6744 return false;
6746 if (!(*bed->elf_backend_final_write_processing) (abfd))
6747 return false;
6749 if (!bed->s->write_shdrs_and_ehdr (abfd))
6750 return false;
6752 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6753 if (t->o->build_id.after_write_object_contents != NULL
6754 && !(*t->o->build_id.after_write_object_contents) (abfd))
6755 return false;
6756 if (t->o->package_metadata.after_write_object_contents != NULL
6757 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6758 return false;
6760 return true;
6763 bool
6764 _bfd_elf_write_corefile_contents (bfd *abfd)
6766 /* Hopefully this can be done just like an object file. */
6767 return _bfd_elf_write_object_contents (abfd);
6770 /* Given a section, search the header to find them. */
6772 unsigned int
6773 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6775 const struct elf_backend_data *bed;
6776 unsigned int sec_index;
6778 if (elf_section_data (asect) != NULL
6779 && elf_section_data (asect)->this_idx != 0)
6780 return elf_section_data (asect)->this_idx;
6782 if (bfd_is_abs_section (asect))
6783 sec_index = SHN_ABS;
6784 else if (bfd_is_com_section (asect))
6785 sec_index = SHN_COMMON;
6786 else if (bfd_is_und_section (asect))
6787 sec_index = SHN_UNDEF;
6788 else
6789 sec_index = SHN_BAD;
6791 bed = get_elf_backend_data (abfd);
6792 if (bed->elf_backend_section_from_bfd_section)
6794 int retval = sec_index;
6796 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6797 return retval;
6800 if (sec_index == SHN_BAD)
6801 bfd_set_error (bfd_error_nonrepresentable_section);
6803 return sec_index;
6806 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6807 on error. */
6810 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6812 asymbol *asym_ptr = *asym_ptr_ptr;
6813 int idx;
6814 flagword flags = asym_ptr->flags;
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)
6825 asection *sec;
6827 sec = asym_ptr->section;
6828 if (sec->owner != abfd && sec->output_section != NULL)
6829 sec = sec->output_section;
6830 if (sec->owner == abfd
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;
6836 idx = asym_ptr->udata.i;
6838 if (idx == 0)
6840 /* This case can occur when using --strip-symbol on a symbol
6841 which is used in a relocation entry. */
6842 _bfd_error_handler
6843 /* xgettext:c-format */
6844 (_("%pB: symbol `%s' required but not present"),
6845 abfd, bfd_asymbol_name (asym_ptr));
6846 bfd_set_error (bfd_error_no_symbols);
6847 return -1;
6850 #if DEBUG & 4
6852 fprintf (stderr,
6853 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6854 " flags = 0x%.8x\n",
6855 (long) asym_ptr, asym_ptr->name, idx, flags);
6856 fflush (stderr);
6858 #endif
6860 return idx;
6863 static inline bfd_vma
6864 segment_size (Elf_Internal_Phdr *segment)
6866 return (segment->p_memsz > segment->p_filesz
6867 ? segment->p_memsz : segment->p_filesz);
6871 /* Returns the end address of the segment + 1. */
6872 static inline bfd_vma
6873 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6875 return start + segment_size (segment);
6878 static inline bfd_size_type
6879 section_size (asection *section, Elf_Internal_Phdr *segment)
6881 if ((section->flags & SEC_HAS_CONTENTS) != 0
6882 || (section->flags & SEC_THREAD_LOCAL) == 0
6883 || segment->p_type == PT_TLS)
6884 return section->size;
6885 return 0;
6888 /* Returns TRUE if the given section is contained within the given
6889 segment. LMA addresses are compared against PADDR when
6890 USE_VADDR is false, VMA against VADDR when true. */
6891 static bool
6892 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
6893 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
6894 bool use_vaddr)
6896 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
6897 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
6898 bfd_vma octet;
6899 if (_bfd_mul_overflow (addr, opb, &octet))
6900 return false;
6901 /* The third and fourth lines below are testing that the section end
6902 address is within the segment. It's written this way to avoid
6903 overflow. Add seg_addr + section_size to both sides of the
6904 inequality to make it obvious. */
6905 return (octet >= seg_addr
6906 && segment_size (segment) >= section_size (section, segment)
6907 && (octet - seg_addr
6908 <= segment_size (segment) - section_size (section, segment)));
6911 /* Handle PT_NOTE segment. */
6912 static bool
6913 is_note (asection *s, Elf_Internal_Phdr *p)
6915 return (p->p_type == PT_NOTE
6916 && elf_section_type (s) == SHT_NOTE
6917 && (ufile_ptr) s->filepos >= p->p_offset
6918 && p->p_filesz >= s->size
6919 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
6922 /* Rewrite program header information. */
6924 static bool
6925 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
6927 Elf_Internal_Ehdr *iehdr;
6928 struct elf_segment_map *map;
6929 struct elf_segment_map *map_first;
6930 struct elf_segment_map **pointer_to_map;
6931 Elf_Internal_Phdr *segment;
6932 asection *section;
6933 unsigned int i;
6934 unsigned int num_segments;
6935 bool phdr_included = false;
6936 bool p_paddr_valid;
6937 struct elf_segment_map *phdr_adjust_seg = NULL;
6938 unsigned int phdr_adjust_num = 0;
6939 const struct elf_backend_data *bed;
6940 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
6942 bed = get_elf_backend_data (ibfd);
6943 iehdr = elf_elfheader (ibfd);
6945 map_first = NULL;
6946 pointer_to_map = &map_first;
6948 num_segments = elf_elfheader (ibfd)->e_phnum;
6950 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6951 linker, which generates a PT_INTERP section with p_vaddr and
6952 p_memsz set to 0. */
6953 #define IS_SOLARIS_PT_INTERP(p, s) \
6954 (p->p_vaddr == 0 \
6955 && p->p_paddr == 0 \
6956 && p->p_memsz == 0 \
6957 && p->p_filesz > 0 \
6958 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6959 && s->size > 0 \
6960 && (bfd_vma) s->filepos >= p->p_offset \
6961 && ((bfd_vma) s->filepos + s->size \
6962 <= p->p_offset + p->p_filesz))
6964 /* Decide if the given section should be included in the given segment.
6965 A section will be included if:
6966 1. It is within the address space of the segment -- we use the LMA
6967 if that is set for the segment and the VMA otherwise,
6968 2. It is an allocated section or a NOTE section in a PT_NOTE
6969 segment.
6970 3. There is an output section associated with it,
6971 4. The section has not already been allocated to a previous segment.
6972 5. PT_GNU_STACK segments do not include any sections.
6973 6. PT_TLS segment includes only SHF_TLS sections.
6974 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6975 8. PT_DYNAMIC should not contain empty sections at the beginning
6976 (with the possible exception of .dynamic). */
6977 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6978 (((is_contained_by (section, segment, segment->p_paddr, \
6979 segment->p_vaddr, opb, \
6980 bed->want_p_paddr_set_to_zero) \
6981 && (section->flags & SEC_ALLOC) != 0) \
6982 || is_note (section, segment)) \
6983 && segment->p_type != PT_GNU_STACK \
6984 && (segment->p_type != PT_TLS \
6985 || (section->flags & SEC_THREAD_LOCAL)) \
6986 && (segment->p_type == PT_LOAD \
6987 || segment->p_type == PT_TLS \
6988 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6989 && (segment->p_type != PT_DYNAMIC \
6990 || section_size (section, segment) > 0 \
6991 || (segment->p_paddr \
6992 ? segment->p_paddr != section->lma * (opb) \
6993 : segment->p_vaddr != section->vma * (opb)) \
6994 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6995 && (segment->p_type != PT_LOAD || !section->segment_mark))
6997 /* If the output section of a section in the input segment is NULL,
6998 it is removed from the corresponding output segment. */
6999 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7000 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7001 && section->output_section != NULL)
7003 /* Returns TRUE iff seg1 starts after the end of seg2. */
7004 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7005 (seg1->field >= segment_end (seg2, seg2->field))
7007 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7008 their VMA address ranges and their LMA address ranges overlap.
7009 It is possible to have overlapping VMA ranges without overlapping LMA
7010 ranges. RedBoot images for example can have both .data and .bss mapped
7011 to the same VMA range, but with the .data section mapped to a different
7012 LMA. */
7013 #define SEGMENT_OVERLAPS(seg1, seg2) \
7014 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7015 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7016 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7017 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7019 /* Initialise the segment mark field, and discard stupid alignment. */
7020 for (section = ibfd->sections; section != NULL; section = section->next)
7022 asection *o = section->output_section;
7023 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7024 o->alignment_power = 0;
7025 section->segment_mark = false;
7028 /* The Solaris linker creates program headers in which all the
7029 p_paddr fields are zero. When we try to objcopy or strip such a
7030 file, we get confused. Check for this case, and if we find it
7031 don't set the p_paddr_valid fields. */
7032 p_paddr_valid = false;
7033 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7034 i < num_segments;
7035 i++, segment++)
7036 if (segment->p_paddr != 0)
7038 p_paddr_valid = true;
7039 break;
7042 /* Scan through the segments specified in the program header
7043 of the input BFD. For this first scan we look for overlaps
7044 in the loadable segments. These can be created by weird
7045 parameters to objcopy. Also, fix some solaris weirdness. */
7046 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7047 i < num_segments;
7048 i++, segment++)
7050 unsigned int j;
7051 Elf_Internal_Phdr *segment2;
7053 if (segment->p_type == PT_INTERP)
7054 for (section = ibfd->sections; section; section = section->next)
7055 if (IS_SOLARIS_PT_INTERP (segment, section))
7057 /* Mininal change so that the normal section to segment
7058 assignment code will work. */
7059 segment->p_vaddr = section->vma * opb;
7060 break;
7063 if (segment->p_type != PT_LOAD)
7065 /* Remove PT_GNU_RELRO segment. */
7066 if (segment->p_type == PT_GNU_RELRO)
7067 segment->p_type = PT_NULL;
7068 continue;
7071 /* Determine if this segment overlaps any previous segments. */
7072 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7074 bfd_signed_vma extra_length;
7076 if (segment2->p_type != PT_LOAD
7077 || !SEGMENT_OVERLAPS (segment, segment2))
7078 continue;
7080 /* Merge the two segments together. */
7081 if (segment2->p_vaddr < segment->p_vaddr)
7083 /* Extend SEGMENT2 to include SEGMENT and then delete
7084 SEGMENT. */
7085 extra_length = (segment_end (segment, segment->p_vaddr)
7086 - segment_end (segment2, segment2->p_vaddr));
7088 if (extra_length > 0)
7090 segment2->p_memsz += extra_length;
7091 segment2->p_filesz += extra_length;
7094 segment->p_type = PT_NULL;
7096 /* Since we have deleted P we must restart the outer loop. */
7097 i = 0;
7098 segment = elf_tdata (ibfd)->phdr;
7099 break;
7101 else
7103 /* Extend SEGMENT to include SEGMENT2 and then delete
7104 SEGMENT2. */
7105 extra_length = (segment_end (segment2, segment2->p_vaddr)
7106 - segment_end (segment, segment->p_vaddr));
7108 if (extra_length > 0)
7110 segment->p_memsz += extra_length;
7111 segment->p_filesz += extra_length;
7114 segment2->p_type = PT_NULL;
7119 /* The second scan attempts to assign sections to segments. */
7120 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7121 i < num_segments;
7122 i++, segment++)
7124 unsigned int section_count;
7125 asection **sections;
7126 asection *output_section;
7127 unsigned int isec;
7128 asection *matching_lma;
7129 asection *suggested_lma;
7130 unsigned int j;
7131 size_t amt;
7132 asection *first_section;
7134 if (segment->p_type == PT_NULL)
7135 continue;
7137 first_section = NULL;
7138 /* Compute how many sections might be placed into this segment. */
7139 for (section = ibfd->sections, section_count = 0;
7140 section != NULL;
7141 section = section->next)
7143 /* Find the first section in the input segment, which may be
7144 removed from the corresponding output segment. */
7145 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7147 if (first_section == NULL)
7148 first_section = section;
7149 if (section->output_section != NULL)
7150 ++section_count;
7154 /* Allocate a segment map big enough to contain
7155 all of the sections we have selected. */
7156 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7157 amt += section_count * sizeof (asection *);
7158 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7159 if (map == NULL)
7160 return false;
7162 /* Initialise the fields of the segment map. Default to
7163 using the physical address of the segment in the input BFD. */
7164 map->next = NULL;
7165 map->p_type = segment->p_type;
7166 map->p_flags = segment->p_flags;
7167 map->p_flags_valid = 1;
7169 if (map->p_type == PT_LOAD
7170 && (ibfd->flags & D_PAGED) != 0
7171 && maxpagesize > 1
7172 && segment->p_align > 1)
7174 map->p_align = segment->p_align;
7175 if (segment->p_align > maxpagesize)
7176 map->p_align = maxpagesize;
7177 map->p_align_valid = 1;
7180 /* If the first section in the input segment is removed, there is
7181 no need to preserve segment physical address in the corresponding
7182 output segment. */
7183 if (!first_section || first_section->output_section != NULL)
7185 map->p_paddr = segment->p_paddr;
7186 map->p_paddr_valid = p_paddr_valid;
7189 /* Determine if this segment contains the ELF file header
7190 and if it contains the program headers themselves. */
7191 map->includes_filehdr = (segment->p_offset == 0
7192 && segment->p_filesz >= iehdr->e_ehsize);
7193 map->includes_phdrs = 0;
7195 if (!phdr_included || segment->p_type != PT_LOAD)
7197 map->includes_phdrs =
7198 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7199 && (segment->p_offset + segment->p_filesz
7200 >= ((bfd_vma) iehdr->e_phoff
7201 + iehdr->e_phnum * iehdr->e_phentsize)));
7203 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7204 phdr_included = true;
7207 if (section_count == 0)
7209 /* Special segments, such as the PT_PHDR segment, may contain
7210 no sections, but ordinary, loadable segments should contain
7211 something. They are allowed by the ELF spec however, so only
7212 a warning is produced.
7213 There is however the valid use case of embedded systems which
7214 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7215 flash memory with zeros. No warning is shown for that case. */
7216 if (segment->p_type == PT_LOAD
7217 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7218 /* xgettext:c-format */
7219 _bfd_error_handler
7220 (_("%pB: warning: empty loadable segment detected"
7221 " at vaddr=%#" PRIx64 ", is this intentional?"),
7222 ibfd, (uint64_t) segment->p_vaddr);
7224 map->p_vaddr_offset = segment->p_vaddr / opb;
7225 map->count = 0;
7226 *pointer_to_map = map;
7227 pointer_to_map = &map->next;
7229 continue;
7232 /* Now scan the sections in the input BFD again and attempt
7233 to add their corresponding output sections to the segment map.
7234 The problem here is how to handle an output section which has
7235 been moved (ie had its LMA changed). There are four possibilities:
7237 1. None of the sections have been moved.
7238 In this case we can continue to use the segment LMA from the
7239 input BFD.
7241 2. All of the sections have been moved by the same amount.
7242 In this case we can change the segment's LMA to match the LMA
7243 of the first section.
7245 3. Some of the sections have been moved, others have not.
7246 In this case those sections which have not been moved can be
7247 placed in the current segment which will have to have its size,
7248 and possibly its LMA changed, and a new segment or segments will
7249 have to be created to contain the other sections.
7251 4. The sections have been moved, but not by the same amount.
7252 In this case we can change the segment's LMA to match the LMA
7253 of the first section and we will have to create a new segment
7254 or segments to contain the other sections.
7256 In order to save time, we allocate an array to hold the section
7257 pointers that we are interested in. As these sections get assigned
7258 to a segment, they are removed from this array. */
7260 amt = section_count * sizeof (asection *);
7261 sections = (asection **) bfd_malloc (amt);
7262 if (sections == NULL)
7263 return false;
7265 /* Step One: Scan for segment vs section LMA conflicts.
7266 Also add the sections to the section array allocated above.
7267 Also add the sections to the current segment. In the common
7268 case, where the sections have not been moved, this means that
7269 we have completely filled the segment, and there is nothing
7270 more to do. */
7271 isec = 0;
7272 matching_lma = NULL;
7273 suggested_lma = NULL;
7275 for (section = first_section, j = 0;
7276 section != NULL;
7277 section = section->next)
7279 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7281 output_section = section->output_section;
7283 sections[j++] = section;
7285 /* The Solaris native linker always sets p_paddr to 0.
7286 We try to catch that case here, and set it to the
7287 correct value. Note - some backends require that
7288 p_paddr be left as zero. */
7289 if (!p_paddr_valid
7290 && segment->p_vaddr != 0
7291 && !bed->want_p_paddr_set_to_zero
7292 && isec == 0
7293 && output_section->lma != 0
7294 && (align_power (segment->p_vaddr
7295 + (map->includes_filehdr
7296 ? iehdr->e_ehsize : 0)
7297 + (map->includes_phdrs
7298 ? iehdr->e_phnum * iehdr->e_phentsize
7299 : 0),
7300 output_section->alignment_power * opb)
7301 == (output_section->vma * opb)))
7302 map->p_paddr = segment->p_vaddr;
7304 /* Match up the physical address of the segment with the
7305 LMA address of the output section. */
7306 if (is_contained_by (output_section, segment, map->p_paddr,
7307 0, opb, false)
7308 || is_note (section, segment))
7310 if (matching_lma == NULL
7311 || output_section->lma < matching_lma->lma)
7312 matching_lma = output_section;
7314 /* We assume that if the section fits within the segment
7315 then it does not overlap any other section within that
7316 segment. */
7317 map->sections[isec++] = output_section;
7319 else if (suggested_lma == NULL)
7320 suggested_lma = output_section;
7322 if (j == section_count)
7323 break;
7327 BFD_ASSERT (j == section_count);
7329 /* Step Two: Adjust the physical address of the current segment,
7330 if necessary. */
7331 if (isec == section_count)
7333 /* All of the sections fitted within the segment as currently
7334 specified. This is the default case. Add the segment to
7335 the list of built segments and carry on to process the next
7336 program header in the input BFD. */
7337 map->count = section_count;
7338 *pointer_to_map = map;
7339 pointer_to_map = &map->next;
7341 if (p_paddr_valid
7342 && !bed->want_p_paddr_set_to_zero)
7344 bfd_vma hdr_size = 0;
7345 if (map->includes_filehdr)
7346 hdr_size = iehdr->e_ehsize;
7347 if (map->includes_phdrs)
7348 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7350 /* Account for padding before the first section in the
7351 segment. */
7352 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7353 - matching_lma->lma);
7356 free (sections);
7357 continue;
7359 else
7361 /* Change the current segment's physical address to match
7362 the LMA of the first section that fitted, or if no
7363 section fitted, the first section. */
7364 if (matching_lma == NULL)
7365 matching_lma = suggested_lma;
7367 map->p_paddr = matching_lma->lma * opb;
7369 /* Offset the segment physical address from the lma
7370 to allow for space taken up by elf headers. */
7371 if (map->includes_phdrs)
7373 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7375 /* iehdr->e_phnum is just an estimate of the number
7376 of program headers that we will need. Make a note
7377 here of the number we used and the segment we chose
7378 to hold these headers, so that we can adjust the
7379 offset when we know the correct value. */
7380 phdr_adjust_num = iehdr->e_phnum;
7381 phdr_adjust_seg = map;
7384 if (map->includes_filehdr)
7386 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7387 map->p_paddr -= iehdr->e_ehsize;
7388 /* We've subtracted off the size of headers from the
7389 first section lma, but there may have been some
7390 alignment padding before that section too. Try to
7391 account for that by adjusting the segment lma down to
7392 the same alignment. */
7393 if (segment->p_align != 0 && segment->p_align < align)
7394 align = segment->p_align;
7395 map->p_paddr &= -(align * opb);
7399 /* Step Three: Loop over the sections again, this time assigning
7400 those that fit to the current segment and removing them from the
7401 sections array; but making sure not to leave large gaps. Once all
7402 possible sections have been assigned to the current segment it is
7403 added to the list of built segments and if sections still remain
7404 to be assigned, a new segment is constructed before repeating
7405 the loop. */
7406 isec = 0;
7409 map->count = 0;
7410 suggested_lma = NULL;
7412 /* Fill the current segment with sections that fit. */
7413 for (j = 0; j < section_count; j++)
7415 section = sections[j];
7417 if (section == NULL)
7418 continue;
7420 output_section = section->output_section;
7422 BFD_ASSERT (output_section != NULL);
7424 if (is_contained_by (output_section, segment, map->p_paddr,
7425 0, opb, false)
7426 || is_note (section, segment))
7428 if (map->count == 0)
7430 /* If the first section in a segment does not start at
7431 the beginning of the segment, then something is
7432 wrong. */
7433 if (align_power (map->p_paddr
7434 + (map->includes_filehdr
7435 ? iehdr->e_ehsize : 0)
7436 + (map->includes_phdrs
7437 ? iehdr->e_phnum * iehdr->e_phentsize
7438 : 0),
7439 output_section->alignment_power * opb)
7440 != output_section->lma * opb)
7441 goto sorry;
7443 else
7445 asection *prev_sec;
7447 prev_sec = map->sections[map->count - 1];
7449 /* If the gap between the end of the previous section
7450 and the start of this section is more than
7451 maxpagesize then we need to start a new segment. */
7452 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7453 maxpagesize)
7454 < BFD_ALIGN (output_section->lma, maxpagesize))
7455 || (prev_sec->lma + prev_sec->size
7456 > output_section->lma))
7458 if (suggested_lma == NULL)
7459 suggested_lma = output_section;
7461 continue;
7465 map->sections[map->count++] = output_section;
7466 ++isec;
7467 sections[j] = NULL;
7468 if (segment->p_type == PT_LOAD)
7469 section->segment_mark = true;
7471 else if (suggested_lma == NULL)
7472 suggested_lma = output_section;
7475 /* PR 23932. A corrupt input file may contain sections that cannot
7476 be assigned to any segment - because for example they have a
7477 negative size - or segments that do not contain any sections.
7478 But there are also valid reasons why a segment can be empty.
7479 So allow a count of zero. */
7481 /* Add the current segment to the list of built segments. */
7482 *pointer_to_map = map;
7483 pointer_to_map = &map->next;
7485 if (isec < section_count)
7487 /* We still have not allocated all of the sections to
7488 segments. Create a new segment here, initialise it
7489 and carry on looping. */
7490 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7491 amt += section_count * sizeof (asection *);
7492 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7493 if (map == NULL)
7495 free (sections);
7496 return false;
7499 /* Initialise the fields of the segment map. Set the physical
7500 physical address to the LMA of the first section that has
7501 not yet been assigned. */
7502 map->next = NULL;
7503 map->p_type = segment->p_type;
7504 map->p_flags = segment->p_flags;
7505 map->p_flags_valid = 1;
7506 map->p_paddr = suggested_lma->lma * opb;
7507 map->p_paddr_valid = p_paddr_valid;
7508 map->includes_filehdr = 0;
7509 map->includes_phdrs = 0;
7512 continue;
7513 sorry:
7514 bfd_set_error (bfd_error_sorry);
7515 free (sections);
7516 return false;
7518 while (isec < section_count);
7520 free (sections);
7523 elf_seg_map (obfd) = map_first;
7525 /* If we had to estimate the number of program headers that were
7526 going to be needed, then check our estimate now and adjust
7527 the offset if necessary. */
7528 if (phdr_adjust_seg != NULL)
7530 unsigned int count;
7532 for (count = 0, map = map_first; map != NULL; map = map->next)
7533 count++;
7535 if (count > phdr_adjust_num)
7536 phdr_adjust_seg->p_paddr
7537 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7539 for (map = map_first; map != NULL; map = map->next)
7540 if (map->p_type == PT_PHDR)
7542 bfd_vma adjust
7543 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7544 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7545 break;
7549 #undef IS_SOLARIS_PT_INTERP
7550 #undef IS_SECTION_IN_INPUT_SEGMENT
7551 #undef INCLUDE_SECTION_IN_SEGMENT
7552 #undef SEGMENT_AFTER_SEGMENT
7553 #undef SEGMENT_OVERLAPS
7554 return true;
7557 /* Return true if p_align in the ELF program header in ABFD is valid. */
7559 static bool
7560 elf_is_p_align_valid (bfd *abfd)
7562 unsigned int i;
7563 Elf_Internal_Phdr *segment;
7564 unsigned int num_segments;
7565 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7566 bfd_size_type maxpagesize = bed->maxpagesize;
7567 bfd_size_type p_align = bed->p_align;
7569 /* Return true if the default p_align value isn't set or the maximum
7570 page size is the same as the minimum page size. */
7571 if (p_align == 0 || maxpagesize == bed->minpagesize)
7572 return true;
7574 /* When the default p_align value is set, p_align may be set to the
7575 default p_align value while segments are aligned to the maximum
7576 page size. In this case, the input p_align will be ignored and
7577 the maximum page size will be used to align the output segments. */
7578 segment = elf_tdata (abfd)->phdr;
7579 num_segments = elf_elfheader (abfd)->e_phnum;
7580 for (i = 0; i < num_segments; i++, segment++)
7581 if (segment->p_type == PT_LOAD
7582 && (segment->p_align != p_align
7583 || vma_page_aligned_bias (segment->p_vaddr,
7584 segment->p_offset,
7585 maxpagesize) != 0))
7586 return true;
7588 return false;
7591 /* Copy ELF program header information. */
7593 static bool
7594 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7596 Elf_Internal_Ehdr *iehdr;
7597 struct elf_segment_map *map;
7598 struct elf_segment_map *map_first;
7599 struct elf_segment_map **pointer_to_map;
7600 Elf_Internal_Phdr *segment;
7601 unsigned int i;
7602 unsigned int num_segments;
7603 bool phdr_included = false;
7604 bool p_paddr_valid;
7605 bool p_palign_valid;
7606 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7608 iehdr = elf_elfheader (ibfd);
7610 map_first = NULL;
7611 pointer_to_map = &map_first;
7613 /* If all the segment p_paddr fields are zero, don't set
7614 map->p_paddr_valid. */
7615 p_paddr_valid = false;
7616 num_segments = elf_elfheader (ibfd)->e_phnum;
7617 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7618 i < num_segments;
7619 i++, segment++)
7620 if (segment->p_paddr != 0)
7622 p_paddr_valid = true;
7623 break;
7626 p_palign_valid = elf_is_p_align_valid (ibfd);
7628 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7629 i < num_segments;
7630 i++, segment++)
7632 asection *section;
7633 unsigned int section_count;
7634 size_t amt;
7635 Elf_Internal_Shdr *this_hdr;
7636 asection *first_section = NULL;
7637 asection *lowest_section;
7639 /* Compute how many sections are in this segment. */
7640 for (section = ibfd->sections, section_count = 0;
7641 section != NULL;
7642 section = section->next)
7644 this_hdr = &(elf_section_data(section)->this_hdr);
7645 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7647 if (first_section == NULL)
7648 first_section = section;
7649 section_count++;
7653 /* Allocate a segment map big enough to contain
7654 all of the sections we have selected. */
7655 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7656 amt += section_count * sizeof (asection *);
7657 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7658 if (map == NULL)
7659 return false;
7661 /* Initialize the fields of the output segment map with the
7662 input segment. */
7663 map->next = NULL;
7664 map->p_type = segment->p_type;
7665 map->p_flags = segment->p_flags;
7666 map->p_flags_valid = 1;
7667 map->p_paddr = segment->p_paddr;
7668 map->p_paddr_valid = p_paddr_valid;
7669 map->p_align = segment->p_align;
7670 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7671 map->p_align_valid = (map->p_type == PT_GNU_STACK
7672 || p_palign_valid);
7673 map->p_vaddr_offset = 0;
7675 if (map->p_type == PT_GNU_RELRO
7676 || map->p_type == PT_GNU_STACK)
7678 /* The PT_GNU_RELRO segment may contain the first a few
7679 bytes in the .got.plt section even if the whole .got.plt
7680 section isn't in the PT_GNU_RELRO segment. We won't
7681 change the size of the PT_GNU_RELRO segment.
7682 Similarly, PT_GNU_STACK size is significant on uclinux
7683 systems. */
7684 map->p_size = segment->p_memsz;
7685 map->p_size_valid = 1;
7688 /* Determine if this segment contains the ELF file header
7689 and if it contains the program headers themselves. */
7690 map->includes_filehdr = (segment->p_offset == 0
7691 && segment->p_filesz >= iehdr->e_ehsize);
7693 map->includes_phdrs = 0;
7694 if (! phdr_included || segment->p_type != PT_LOAD)
7696 map->includes_phdrs =
7697 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7698 && (segment->p_offset + segment->p_filesz
7699 >= ((bfd_vma) iehdr->e_phoff
7700 + iehdr->e_phnum * iehdr->e_phentsize)));
7702 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7703 phdr_included = true;
7706 lowest_section = NULL;
7707 if (section_count != 0)
7709 unsigned int isec = 0;
7711 for (section = first_section;
7712 section != NULL;
7713 section = section->next)
7715 this_hdr = &(elf_section_data(section)->this_hdr);
7716 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7718 map->sections[isec++] = section->output_section;
7719 if ((section->flags & SEC_ALLOC) != 0)
7721 bfd_vma seg_off;
7723 if (lowest_section == NULL
7724 || section->lma < lowest_section->lma)
7725 lowest_section = section;
7727 /* Section lmas are set up from PT_LOAD header
7728 p_paddr in _bfd_elf_make_section_from_shdr.
7729 If this header has a p_paddr that disagrees
7730 with the section lma, flag the p_paddr as
7731 invalid. */
7732 if ((section->flags & SEC_LOAD) != 0)
7733 seg_off = this_hdr->sh_offset - segment->p_offset;
7734 else
7735 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7736 if (section->lma * opb - segment->p_paddr != seg_off)
7737 map->p_paddr_valid = false;
7739 if (isec == section_count)
7740 break;
7745 if (section_count == 0)
7746 map->p_vaddr_offset = segment->p_vaddr / opb;
7747 else if (map->p_paddr_valid)
7749 /* Account for padding before the first section in the segment. */
7750 bfd_vma hdr_size = 0;
7751 if (map->includes_filehdr)
7752 hdr_size = iehdr->e_ehsize;
7753 if (map->includes_phdrs)
7754 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7756 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7757 - (lowest_section ? lowest_section->lma : 0));
7760 map->count = section_count;
7761 *pointer_to_map = map;
7762 pointer_to_map = &map->next;
7765 elf_seg_map (obfd) = map_first;
7766 return true;
7769 /* Copy private BFD data. This copies or rewrites ELF program header
7770 information. */
7772 static bool
7773 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7775 bfd_vma maxpagesize;
7777 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7778 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7779 return true;
7781 if (elf_tdata (ibfd)->phdr == NULL)
7782 return true;
7784 if (ibfd->xvec == obfd->xvec)
7786 /* Check to see if any sections in the input BFD
7787 covered by ELF program header have changed. */
7788 Elf_Internal_Phdr *segment;
7789 asection *section, *osec;
7790 unsigned int i, num_segments;
7791 Elf_Internal_Shdr *this_hdr;
7792 const struct elf_backend_data *bed;
7794 bed = get_elf_backend_data (ibfd);
7796 /* Regenerate the segment map if p_paddr is set to 0. */
7797 if (bed->want_p_paddr_set_to_zero)
7798 goto rewrite;
7800 /* Initialize the segment mark field. */
7801 for (section = obfd->sections; section != NULL;
7802 section = section->next)
7803 section->segment_mark = false;
7805 num_segments = elf_elfheader (ibfd)->e_phnum;
7806 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7807 i < num_segments;
7808 i++, segment++)
7810 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7811 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7812 which severly confuses things, so always regenerate the segment
7813 map in this case. */
7814 if (segment->p_paddr == 0
7815 && segment->p_memsz == 0
7816 && (segment->p_type == PT_INTERP
7817 || segment->p_type == PT_DYNAMIC))
7818 goto rewrite;
7820 for (section = ibfd->sections;
7821 section != NULL; section = section->next)
7823 /* We mark the output section so that we know it comes
7824 from the input BFD. */
7825 osec = section->output_section;
7826 if (osec)
7827 osec->segment_mark = true;
7829 /* Check if this section is covered by the segment. */
7830 this_hdr = &(elf_section_data(section)->this_hdr);
7831 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7833 /* FIXME: Check if its output section is changed or
7834 removed. What else do we need to check? */
7835 if (osec == NULL
7836 || section->flags != osec->flags
7837 || section->lma != osec->lma
7838 || section->vma != osec->vma
7839 || section->size != osec->size
7840 || section->rawsize != osec->rawsize
7841 || section->alignment_power != osec->alignment_power)
7842 goto rewrite;
7847 /* Check to see if any output section do not come from the
7848 input BFD. */
7849 for (section = obfd->sections; section != NULL;
7850 section = section->next)
7852 if (!section->segment_mark)
7853 goto rewrite;
7854 else
7855 section->segment_mark = false;
7858 return copy_elf_program_header (ibfd, obfd);
7861 rewrite:
7862 maxpagesize = 0;
7863 if (ibfd->xvec == obfd->xvec)
7865 /* When rewriting program header, set the output maxpagesize to
7866 the maximum alignment of input PT_LOAD segments. */
7867 Elf_Internal_Phdr *segment;
7868 unsigned int i;
7869 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7871 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7872 i < num_segments;
7873 i++, segment++)
7874 if (segment->p_type == PT_LOAD
7875 && maxpagesize < segment->p_align)
7877 /* PR 17512: file: f17299af. */
7878 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7879 /* xgettext:c-format */
7880 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7881 PRIx64 " is too large"),
7882 ibfd, (uint64_t) segment->p_align);
7883 else
7884 maxpagesize = segment->p_align;
7887 if (maxpagesize == 0)
7888 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7890 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7893 /* Initialize private output section information from input section. */
7895 bool
7896 _bfd_elf_init_private_section_data (bfd *ibfd,
7897 asection *isec,
7898 bfd *obfd,
7899 asection *osec,
7900 struct bfd_link_info *link_info)
7903 Elf_Internal_Shdr *ihdr, *ohdr;
7904 bool final_link = (link_info != NULL
7905 && !bfd_link_relocatable (link_info));
7907 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7908 || obfd->xvec->flavour != bfd_target_elf_flavour)
7909 return true;
7911 BFD_ASSERT (elf_section_data (osec) != NULL);
7913 /* If this is a known ABI section, ELF section type and flags may
7914 have been set up when OSEC was created. For normal sections we
7915 allow the user to override the type and flags other than
7916 SHF_MASKOS and SHF_MASKPROC. */
7917 if (elf_section_type (osec) == SHT_PROGBITS
7918 || elf_section_type (osec) == SHT_NOTE
7919 || elf_section_type (osec) == SHT_NOBITS)
7920 elf_section_type (osec) = SHT_NULL;
7921 /* For objcopy and relocatable link, copy the ELF section type from
7922 the input file if the BFD section flags are the same. (If they
7923 are different the user may be doing something like
7924 "objcopy --set-section-flags .text=alloc,data".) For a final
7925 link allow some flags that the linker clears to differ. */
7926 if (elf_section_type (osec) == SHT_NULL
7927 && (osec->flags == isec->flags
7928 || (final_link
7929 && ((osec->flags ^ isec->flags)
7930 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7931 elf_section_type (osec) = elf_section_type (isec);
7933 /* FIXME: Is this correct for all OS/PROC specific flags? */
7934 elf_section_flags (osec) = (elf_section_flags (isec)
7935 & (SHF_MASKOS | SHF_MASKPROC));
7937 /* Copy sh_info from input for mbind section. */
7938 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7939 && elf_section_flags (isec) & SHF_GNU_MBIND)
7940 elf_section_data (osec)->this_hdr.sh_info
7941 = elf_section_data (isec)->this_hdr.sh_info;
7943 /* Set things up for objcopy and relocatable link. The output
7944 SHT_GROUP section will have its elf_next_in_group pointing back
7945 to the input group members. Ignore linker created group section.
7946 See elfNN_ia64_object_p in elfxx-ia64.c. */
7947 if ((link_info == NULL
7948 || !link_info->resolve_section_groups)
7949 && (elf_sec_group (isec) == NULL
7950 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7952 if (elf_section_flags (isec) & SHF_GROUP)
7953 elf_section_flags (osec) |= SHF_GROUP;
7954 elf_next_in_group (osec) = elf_next_in_group (isec);
7955 elf_section_data (osec)->group = elf_section_data (isec)->group;
7958 /* If not decompress, preserve SHF_COMPRESSED. */
7959 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7960 elf_section_flags (osec) |= (elf_section_flags (isec)
7961 & SHF_COMPRESSED);
7963 ihdr = &elf_section_data (isec)->this_hdr;
7965 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7966 don't use the output section of the linked-to section since it
7967 may be NULL at this point. */
7968 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7970 ohdr = &elf_section_data (osec)->this_hdr;
7971 ohdr->sh_flags |= SHF_LINK_ORDER;
7972 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7975 osec->use_rela_p = isec->use_rela_p;
7977 return true;
7980 /* Copy private section information. This copies over the entsize
7981 field, and sometimes the info field. */
7983 bool
7984 _bfd_elf_copy_private_section_data (bfd *ibfd,
7985 asection *isec,
7986 bfd *obfd,
7987 asection *osec)
7989 Elf_Internal_Shdr *ihdr, *ohdr;
7991 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7992 || obfd->xvec->flavour != bfd_target_elf_flavour)
7993 return true;
7995 ihdr = &elf_section_data (isec)->this_hdr;
7996 ohdr = &elf_section_data (osec)->this_hdr;
7998 ohdr->sh_entsize = ihdr->sh_entsize;
8000 if (ihdr->sh_type == SHT_SYMTAB
8001 || ihdr->sh_type == SHT_DYNSYM
8002 || ihdr->sh_type == SHT_GNU_verneed
8003 || ihdr->sh_type == SHT_GNU_verdef)
8004 ohdr->sh_info = ihdr->sh_info;
8006 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8007 NULL);
8010 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8011 necessary if we are removing either the SHT_GROUP section or any of
8012 the group member sections. DISCARDED is the value that a section's
8013 output_section has if the section will be discarded, NULL when this
8014 function is called from objcopy, bfd_abs_section_ptr when called
8015 from the linker. */
8017 bool
8018 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8020 asection *isec;
8022 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8023 if (elf_section_type (isec) == SHT_GROUP)
8025 asection *first = elf_next_in_group (isec);
8026 asection *s = first;
8027 bfd_size_type removed = 0;
8029 while (s != NULL)
8031 /* If this member section is being output but the
8032 SHT_GROUP section is not, then clear the group info
8033 set up by _bfd_elf_copy_private_section_data. */
8034 if (s->output_section != discarded
8035 && isec->output_section == discarded)
8037 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8038 elf_group_name (s->output_section) = NULL;
8040 else
8042 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8043 if (s->output_section == discarded
8044 && isec->output_section != discarded)
8046 /* Conversely, if the member section is not being
8047 output but the SHT_GROUP section is, then adjust
8048 its size. */
8049 removed += 4;
8050 if (elf_sec->rel.hdr != NULL
8051 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8052 removed += 4;
8053 if (elf_sec->rela.hdr != NULL
8054 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8055 removed += 4;
8057 else
8059 /* Also adjust for zero-sized relocation member
8060 section. */
8061 if (elf_sec->rel.hdr != NULL
8062 && elf_sec->rel.hdr->sh_size == 0)
8063 removed += 4;
8064 if (elf_sec->rela.hdr != NULL
8065 && elf_sec->rela.hdr->sh_size == 0)
8066 removed += 4;
8069 s = elf_next_in_group (s);
8070 if (s == first)
8071 break;
8073 if (removed != 0)
8075 if (discarded != NULL)
8077 /* If we've been called for ld -r, then we need to
8078 adjust the input section size. */
8079 if (isec->rawsize == 0)
8080 isec->rawsize = isec->size;
8081 isec->size = isec->rawsize - removed;
8082 if (isec->size <= 4)
8084 isec->size = 0;
8085 isec->flags |= SEC_EXCLUDE;
8088 else if (isec->output_section != NULL)
8090 /* Adjust the output section size when called from
8091 objcopy. */
8092 isec->output_section->size -= removed;
8093 if (isec->output_section->size <= 4)
8095 isec->output_section->size = 0;
8096 isec->output_section->flags |= SEC_EXCLUDE;
8102 return true;
8105 /* Copy private header information. */
8107 bool
8108 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8110 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8111 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8112 return true;
8114 /* Copy over private BFD data if it has not already been copied.
8115 This must be done here, rather than in the copy_private_bfd_data
8116 entry point, because the latter is called after the section
8117 contents have been set, which means that the program headers have
8118 already been worked out. */
8119 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8121 if (! copy_private_bfd_data (ibfd, obfd))
8122 return false;
8125 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8128 /* Copy private symbol information. If this symbol is in a section
8129 which we did not map into a BFD section, try to map the section
8130 index correctly. We use special macro definitions for the mapped
8131 section indices; these definitions are interpreted by the
8132 swap_out_syms function. */
8134 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8135 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8136 #define MAP_STRTAB (SHN_HIOS + 3)
8137 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8138 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8140 bool
8141 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8142 asymbol *isymarg,
8143 bfd *obfd,
8144 asymbol *osymarg)
8146 elf_symbol_type *isym, *osym;
8148 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8149 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8150 return true;
8152 isym = elf_symbol_from (isymarg);
8153 osym = elf_symbol_from (osymarg);
8155 if (isym != NULL
8156 && isym->internal_elf_sym.st_shndx != 0
8157 && osym != NULL
8158 && bfd_is_abs_section (isym->symbol.section))
8160 unsigned int shndx;
8162 shndx = isym->internal_elf_sym.st_shndx;
8163 if (shndx == elf_onesymtab (ibfd))
8164 shndx = MAP_ONESYMTAB;
8165 else if (shndx == elf_dynsymtab (ibfd))
8166 shndx = MAP_DYNSYMTAB;
8167 else if (shndx == elf_strtab_sec (ibfd))
8168 shndx = MAP_STRTAB;
8169 else if (shndx == elf_shstrtab_sec (ibfd))
8170 shndx = MAP_SHSTRTAB;
8171 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8172 shndx = MAP_SYM_SHNDX;
8173 osym->internal_elf_sym.st_shndx = shndx;
8176 return true;
8179 /* Swap out the symbols. */
8181 static bool
8182 swap_out_syms (bfd *abfd,
8183 struct elf_strtab_hash **sttp,
8184 int relocatable_p,
8185 struct bfd_link_info *info)
8187 const struct elf_backend_data *bed;
8188 unsigned int symcount;
8189 asymbol **syms;
8190 struct elf_strtab_hash *stt;
8191 Elf_Internal_Shdr *symtab_hdr;
8192 Elf_Internal_Shdr *symtab_shndx_hdr;
8193 Elf_Internal_Shdr *symstrtab_hdr;
8194 struct elf_sym_strtab *symstrtab;
8195 bfd_byte *outbound_syms;
8196 bfd_byte *outbound_shndx;
8197 unsigned long outbound_syms_index;
8198 unsigned int idx;
8199 unsigned int num_locals;
8200 size_t amt;
8201 bool name_local_sections;
8203 if (!elf_map_symbols (abfd, &num_locals))
8204 return false;
8206 /* Dump out the symtabs. */
8207 stt = _bfd_elf_strtab_init ();
8208 if (stt == NULL)
8209 return false;
8211 bed = get_elf_backend_data (abfd);
8212 symcount = bfd_get_symcount (abfd);
8213 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8214 symtab_hdr->sh_type = SHT_SYMTAB;
8215 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8216 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8217 symtab_hdr->sh_info = num_locals + 1;
8218 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8220 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8221 symstrtab_hdr->sh_type = SHT_STRTAB;
8223 /* Allocate buffer to swap out the .strtab section. */
8224 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8225 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8227 bfd_set_error (bfd_error_no_memory);
8228 _bfd_elf_strtab_free (stt);
8229 return false;
8232 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8233 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8235 error_no_mem:
8236 bfd_set_error (bfd_error_no_memory);
8237 error_return:
8238 free (symstrtab);
8239 _bfd_elf_strtab_free (stt);
8240 return false;
8242 symtab_hdr->contents = outbound_syms;
8243 outbound_syms_index = 0;
8245 outbound_shndx = NULL;
8247 if (elf_symtab_shndx_list (abfd))
8249 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8250 if (symtab_shndx_hdr->sh_name != 0)
8252 if (_bfd_mul_overflow (symcount + 1,
8253 sizeof (Elf_External_Sym_Shndx), &amt))
8254 goto error_no_mem;
8255 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8256 if (outbound_shndx == NULL)
8257 goto error_return;
8259 symtab_shndx_hdr->contents = outbound_shndx;
8260 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8261 symtab_shndx_hdr->sh_size = amt;
8262 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8263 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8265 /* FIXME: What about any other headers in the list ? */
8268 /* Now generate the data (for "contents"). */
8270 /* Fill in zeroth symbol and swap it out. */
8271 Elf_Internal_Sym sym;
8272 sym.st_name = 0;
8273 sym.st_value = 0;
8274 sym.st_size = 0;
8275 sym.st_info = 0;
8276 sym.st_other = 0;
8277 sym.st_shndx = SHN_UNDEF;
8278 sym.st_target_internal = 0;
8279 symstrtab[0].sym = sym;
8280 symstrtab[0].dest_index = outbound_syms_index;
8281 outbound_syms_index++;
8284 name_local_sections
8285 = (bed->elf_backend_name_local_section_symbols
8286 && bed->elf_backend_name_local_section_symbols (abfd));
8288 syms = bfd_get_outsymbols (abfd);
8289 for (idx = 0; idx < symcount;)
8291 Elf_Internal_Sym sym;
8292 bfd_vma value = syms[idx]->value;
8293 elf_symbol_type *type_ptr;
8294 flagword flags = syms[idx]->flags;
8295 int type;
8297 if (!name_local_sections
8298 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8300 /* Local section symbols have no name. */
8301 sym.st_name = (unsigned long) -1;
8303 else
8305 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8306 to get the final offset for st_name. */
8307 sym.st_name
8308 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8309 false);
8310 if (sym.st_name == (unsigned long) -1)
8311 goto error_return;
8314 type_ptr = elf_symbol_from (syms[idx]);
8316 if ((flags & BSF_SECTION_SYM) == 0
8317 && bfd_is_com_section (syms[idx]->section))
8319 /* ELF common symbols put the alignment into the `value' field,
8320 and the size into the `size' field. This is backwards from
8321 how BFD handles it, so reverse it here. */
8322 sym.st_size = value;
8323 if (type_ptr == NULL
8324 || type_ptr->internal_elf_sym.st_value == 0)
8325 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8326 else
8327 sym.st_value = type_ptr->internal_elf_sym.st_value;
8328 sym.st_shndx = _bfd_elf_section_from_bfd_section
8329 (abfd, syms[idx]->section);
8331 else
8333 asection *sec = syms[idx]->section;
8334 unsigned int shndx;
8336 if (sec->output_section)
8338 value += sec->output_offset;
8339 sec = sec->output_section;
8342 /* Don't add in the section vma for relocatable output. */
8343 if (! relocatable_p)
8344 value += sec->vma;
8345 sym.st_value = value;
8346 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8348 if (bfd_is_abs_section (sec)
8349 && type_ptr != NULL
8350 && type_ptr->internal_elf_sym.st_shndx != 0)
8352 /* This symbol is in a real ELF section which we did
8353 not create as a BFD section. Undo the mapping done
8354 by copy_private_symbol_data. */
8355 shndx = type_ptr->internal_elf_sym.st_shndx;
8356 switch (shndx)
8358 case MAP_ONESYMTAB:
8359 shndx = elf_onesymtab (abfd);
8360 break;
8361 case MAP_DYNSYMTAB:
8362 shndx = elf_dynsymtab (abfd);
8363 break;
8364 case MAP_STRTAB:
8365 shndx = elf_strtab_sec (abfd);
8366 break;
8367 case MAP_SHSTRTAB:
8368 shndx = elf_shstrtab_sec (abfd);
8369 break;
8370 case MAP_SYM_SHNDX:
8371 if (elf_symtab_shndx_list (abfd))
8372 shndx = elf_symtab_shndx_list (abfd)->ndx;
8373 break;
8374 case SHN_COMMON:
8375 case SHN_ABS:
8376 shndx = SHN_ABS;
8377 break;
8378 default:
8379 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8381 if (bed->symbol_section_index)
8382 shndx = bed->symbol_section_index (abfd, type_ptr);
8383 /* Otherwise just leave the index alone. */
8385 else
8387 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8388 _bfd_error_handler (_("%pB: \
8389 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8390 abfd, shndx);
8391 shndx = SHN_ABS;
8393 break;
8396 else
8398 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8400 if (shndx == SHN_BAD)
8402 asection *sec2;
8404 /* Writing this would be a hell of a lot easier if
8405 we had some decent documentation on bfd, and
8406 knew what to expect of the library, and what to
8407 demand of applications. For example, it
8408 appears that `objcopy' might not set the
8409 section of a symbol to be a section that is
8410 actually in the output file. */
8411 sec2 = bfd_get_section_by_name (abfd, sec->name);
8412 if (sec2 != NULL)
8413 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8414 if (shndx == SHN_BAD)
8416 /* xgettext:c-format */
8417 _bfd_error_handler
8418 (_("unable to find equivalent output section"
8419 " for symbol '%s' from section '%s'"),
8420 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8421 sec->name);
8422 bfd_set_error (bfd_error_invalid_operation);
8423 goto error_return;
8428 sym.st_shndx = shndx;
8431 if ((flags & BSF_THREAD_LOCAL) != 0)
8432 type = STT_TLS;
8433 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8434 type = STT_GNU_IFUNC;
8435 else if ((flags & BSF_FUNCTION) != 0)
8436 type = STT_FUNC;
8437 else if ((flags & BSF_OBJECT) != 0)
8438 type = STT_OBJECT;
8439 else if ((flags & BSF_RELC) != 0)
8440 type = STT_RELC;
8441 else if ((flags & BSF_SRELC) != 0)
8442 type = STT_SRELC;
8443 else
8444 type = STT_NOTYPE;
8446 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8447 type = STT_TLS;
8449 /* Processor-specific types. */
8450 if (type_ptr != NULL
8451 && bed->elf_backend_get_symbol_type)
8452 type = ((*bed->elf_backend_get_symbol_type)
8453 (&type_ptr->internal_elf_sym, type));
8455 if (flags & BSF_SECTION_SYM)
8457 if (flags & BSF_GLOBAL)
8458 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8459 else
8460 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8462 else if (bfd_is_com_section (syms[idx]->section))
8464 if (type != STT_TLS)
8466 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8467 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8468 ? STT_COMMON : STT_OBJECT);
8469 else
8470 type = ((flags & BSF_ELF_COMMON) != 0
8471 ? STT_COMMON : STT_OBJECT);
8473 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8475 else if (bfd_is_und_section (syms[idx]->section))
8476 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8477 ? STB_WEAK
8478 : STB_GLOBAL),
8479 type);
8480 else if (flags & BSF_FILE)
8481 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8482 else
8484 int bind = STB_LOCAL;
8486 if (flags & BSF_LOCAL)
8487 bind = STB_LOCAL;
8488 else if (flags & BSF_GNU_UNIQUE)
8489 bind = STB_GNU_UNIQUE;
8490 else if (flags & BSF_WEAK)
8491 bind = STB_WEAK;
8492 else if (flags & BSF_GLOBAL)
8493 bind = STB_GLOBAL;
8495 sym.st_info = ELF_ST_INFO (bind, type);
8498 if (type_ptr != NULL)
8500 sym.st_other = type_ptr->internal_elf_sym.st_other;
8501 sym.st_target_internal
8502 = type_ptr->internal_elf_sym.st_target_internal;
8504 else
8506 sym.st_other = 0;
8507 sym.st_target_internal = 0;
8510 idx++;
8511 symstrtab[idx].sym = sym;
8512 symstrtab[idx].dest_index = outbound_syms_index;
8514 outbound_syms_index++;
8517 /* Finalize the .strtab section. */
8518 _bfd_elf_strtab_finalize (stt);
8520 /* Swap out the .strtab section. */
8521 for (idx = 0; idx <= symcount; idx++)
8523 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8524 if (elfsym->sym.st_name == (unsigned long) -1)
8525 elfsym->sym.st_name = 0;
8526 else
8527 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8528 elfsym->sym.st_name);
8529 if (info && info->callbacks->ctf_new_symbol)
8530 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8531 &elfsym->sym);
8533 /* Inform the linker of the addition of this symbol. */
8535 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8536 (outbound_syms
8537 + (elfsym->dest_index
8538 * bed->s->sizeof_sym)),
8539 NPTR_ADD (outbound_shndx,
8540 (elfsym->dest_index
8541 * sizeof (Elf_External_Sym_Shndx))));
8543 free (symstrtab);
8545 *sttp = stt;
8546 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8547 symstrtab_hdr->sh_type = SHT_STRTAB;
8548 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8549 symstrtab_hdr->sh_addr = 0;
8550 symstrtab_hdr->sh_entsize = 0;
8551 symstrtab_hdr->sh_link = 0;
8552 symstrtab_hdr->sh_info = 0;
8553 symstrtab_hdr->sh_addralign = 1;
8555 return true;
8558 /* Return the number of bytes required to hold the symtab vector.
8560 Note that we base it on the count plus 1, since we will null terminate
8561 the vector allocated based on this size. However, the ELF symbol table
8562 always has a dummy entry as symbol #0, so it ends up even. */
8564 long
8565 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8567 bfd_size_type symcount;
8568 long symtab_size;
8569 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8571 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8572 if (symcount > LONG_MAX / sizeof (asymbol *))
8574 bfd_set_error (bfd_error_file_too_big);
8575 return -1;
8577 symtab_size = symcount * (sizeof (asymbol *));
8578 if (symcount == 0)
8579 symtab_size = sizeof (asymbol *);
8580 else if (!bfd_write_p (abfd))
8582 ufile_ptr filesize = bfd_get_file_size (abfd);
8584 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8586 bfd_set_error (bfd_error_file_truncated);
8587 return -1;
8591 return symtab_size;
8594 long
8595 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8597 bfd_size_type symcount;
8598 long symtab_size;
8599 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8601 if (elf_dynsymtab (abfd) == 0)
8603 bfd_set_error (bfd_error_invalid_operation);
8604 return -1;
8607 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8608 if (symcount > LONG_MAX / sizeof (asymbol *))
8610 bfd_set_error (bfd_error_file_too_big);
8611 return -1;
8613 symtab_size = symcount * (sizeof (asymbol *));
8614 if (symcount == 0)
8615 symtab_size = sizeof (asymbol *);
8616 else if (!bfd_write_p (abfd))
8618 ufile_ptr filesize = bfd_get_file_size (abfd);
8620 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8622 bfd_set_error (bfd_error_file_truncated);
8623 return -1;
8627 return symtab_size;
8630 long
8631 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8633 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8635 /* Sanity check reloc section size. */
8636 ufile_ptr filesize = bfd_get_file_size (abfd);
8638 if (filesize != 0)
8640 struct bfd_elf_section_data *d = elf_section_data (asect);
8641 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
8642 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
8644 if (rel_size + rela_size > filesize
8645 || rel_size + rela_size < rel_size)
8647 bfd_set_error (bfd_error_file_truncated);
8648 return -1;
8653 #if SIZEOF_LONG == SIZEOF_INT
8654 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8656 bfd_set_error (bfd_error_file_too_big);
8657 return -1;
8659 #endif
8660 return (asect->reloc_count + 1L) * sizeof (arelent *);
8663 /* Canonicalize the relocs. */
8665 long
8666 _bfd_elf_canonicalize_reloc (bfd *abfd,
8667 sec_ptr section,
8668 arelent **relptr,
8669 asymbol **symbols)
8671 arelent *tblptr;
8672 unsigned int i;
8673 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8675 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8676 return -1;
8678 tblptr = section->relocation;
8679 for (i = 0; i < section->reloc_count; i++)
8680 *relptr++ = tblptr++;
8682 *relptr = NULL;
8684 return section->reloc_count;
8687 long
8688 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8690 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8691 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8693 if (symcount >= 0)
8694 abfd->symcount = symcount;
8695 return symcount;
8698 long
8699 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8700 asymbol **allocation)
8702 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8703 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8705 if (symcount >= 0)
8706 abfd->dynsymcount = symcount;
8707 return symcount;
8710 /* Return the size required for the dynamic reloc entries. Any loadable
8711 section that was actually installed in the BFD, and has type SHT_REL
8712 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8713 dynamic reloc section. */
8715 long
8716 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8718 bfd_size_type count, ext_rel_size;
8719 asection *s;
8721 if (elf_dynsymtab (abfd) == 0)
8723 bfd_set_error (bfd_error_invalid_operation);
8724 return -1;
8727 count = 1;
8728 ext_rel_size = 0;
8729 for (s = abfd->sections; s != NULL; s = s->next)
8730 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8731 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8732 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8733 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
8735 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
8736 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
8738 bfd_set_error (bfd_error_file_truncated);
8739 return -1;
8741 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
8742 if (count > LONG_MAX / sizeof (arelent *))
8744 bfd_set_error (bfd_error_file_too_big);
8745 return -1;
8748 if (count > 1 && !bfd_write_p (abfd))
8750 /* Sanity check reloc section sizes. */
8751 ufile_ptr filesize = bfd_get_file_size (abfd);
8752 if (filesize != 0 && ext_rel_size > filesize)
8754 bfd_set_error (bfd_error_file_truncated);
8755 return -1;
8758 return count * sizeof (arelent *);
8761 /* Canonicalize the dynamic relocation entries. Note that we return the
8762 dynamic relocations as a single block, although they are actually
8763 associated with particular sections; the interface, which was
8764 designed for SunOS style shared libraries, expects that there is only
8765 one set of dynamic relocs. Any loadable section that was actually
8766 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8767 dynamic symbol table, is considered to be a dynamic reloc section. */
8769 long
8770 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8771 arelent **storage,
8772 asymbol **syms)
8774 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8775 asection *s;
8776 long ret;
8778 if (elf_dynsymtab (abfd) == 0)
8780 bfd_set_error (bfd_error_invalid_operation);
8781 return -1;
8784 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8785 ret = 0;
8786 for (s = abfd->sections; s != NULL; s = s->next)
8788 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8789 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8790 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
8791 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
8793 arelent *p;
8794 long count, i;
8796 if (! (*slurp_relocs) (abfd, s, syms, true))
8797 return -1;
8798 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
8799 p = s->relocation;
8800 for (i = 0; i < count; i++)
8801 *storage++ = p++;
8802 ret += count;
8806 *storage = NULL;
8808 return ret;
8811 /* Read in the version information. */
8813 bool
8814 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8816 bfd_byte *contents = NULL;
8817 unsigned int freeidx = 0;
8818 size_t amt;
8820 if (elf_dynverref (abfd) != 0)
8822 Elf_Internal_Shdr *hdr;
8823 Elf_External_Verneed *everneed;
8824 Elf_Internal_Verneed *iverneed;
8825 unsigned int i;
8826 bfd_byte *contents_end;
8828 hdr = &elf_tdata (abfd)->dynverref_hdr;
8830 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8832 error_return_bad_verref:
8833 _bfd_error_handler
8834 (_("%pB: .gnu.version_r invalid entry"), abfd);
8835 bfd_set_error (bfd_error_bad_value);
8836 error_return_verref:
8837 elf_tdata (abfd)->verref = NULL;
8838 elf_tdata (abfd)->cverrefs = 0;
8839 goto error_return;
8842 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8843 goto error_return_verref;
8844 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8845 if (contents == NULL)
8846 goto error_return_verref;
8848 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8850 bfd_set_error (bfd_error_file_too_big);
8851 goto error_return_verref;
8853 if (amt == 0)
8854 goto error_return_verref;
8855 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
8856 if (elf_tdata (abfd)->verref == NULL)
8857 goto error_return_verref;
8859 BFD_ASSERT (sizeof (Elf_External_Verneed)
8860 == sizeof (Elf_External_Vernaux));
8861 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8862 everneed = (Elf_External_Verneed *) contents;
8863 iverneed = elf_tdata (abfd)->verref;
8864 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8866 Elf_External_Vernaux *evernaux;
8867 Elf_Internal_Vernaux *ivernaux;
8868 unsigned int j;
8870 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8872 iverneed->vn_bfd = abfd;
8874 iverneed->vn_filename =
8875 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8876 iverneed->vn_file);
8877 if (iverneed->vn_filename == NULL)
8878 goto error_return_bad_verref;
8880 if (iverneed->vn_cnt == 0)
8881 iverneed->vn_auxptr = NULL;
8882 else
8884 if (_bfd_mul_overflow (iverneed->vn_cnt,
8885 sizeof (Elf_Internal_Vernaux), &amt))
8887 bfd_set_error (bfd_error_file_too_big);
8888 goto error_return_verref;
8890 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8891 bfd_alloc (abfd, amt);
8892 if (iverneed->vn_auxptr == NULL)
8893 goto error_return_verref;
8896 if (iverneed->vn_aux
8897 > (size_t) (contents_end - (bfd_byte *) everneed))
8898 goto error_return_bad_verref;
8900 evernaux = ((Elf_External_Vernaux *)
8901 ((bfd_byte *) everneed + iverneed->vn_aux));
8902 ivernaux = iverneed->vn_auxptr;
8903 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8905 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8907 ivernaux->vna_nodename =
8908 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8909 ivernaux->vna_name);
8910 if (ivernaux->vna_nodename == NULL)
8911 goto error_return_bad_verref;
8913 if (ivernaux->vna_other > freeidx)
8914 freeidx = ivernaux->vna_other;
8916 ivernaux->vna_nextptr = NULL;
8917 if (ivernaux->vna_next == 0)
8919 iverneed->vn_cnt = j + 1;
8920 break;
8922 if (j + 1 < iverneed->vn_cnt)
8923 ivernaux->vna_nextptr = ivernaux + 1;
8925 if (ivernaux->vna_next
8926 > (size_t) (contents_end - (bfd_byte *) evernaux))
8927 goto error_return_bad_verref;
8929 evernaux = ((Elf_External_Vernaux *)
8930 ((bfd_byte *) evernaux + ivernaux->vna_next));
8933 iverneed->vn_nextref = NULL;
8934 if (iverneed->vn_next == 0)
8935 break;
8936 if (i + 1 < hdr->sh_info)
8937 iverneed->vn_nextref = iverneed + 1;
8939 if (iverneed->vn_next
8940 > (size_t) (contents_end - (bfd_byte *) everneed))
8941 goto error_return_bad_verref;
8943 everneed = ((Elf_External_Verneed *)
8944 ((bfd_byte *) everneed + iverneed->vn_next));
8946 elf_tdata (abfd)->cverrefs = i;
8948 free (contents);
8949 contents = NULL;
8952 if (elf_dynverdef (abfd) != 0)
8954 Elf_Internal_Shdr *hdr;
8955 Elf_External_Verdef *everdef;
8956 Elf_Internal_Verdef *iverdef;
8957 Elf_Internal_Verdef *iverdefarr;
8958 Elf_Internal_Verdef iverdefmem;
8959 unsigned int i;
8960 unsigned int maxidx;
8961 bfd_byte *contents_end_def, *contents_end_aux;
8963 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8965 if (hdr->sh_size < sizeof (Elf_External_Verdef))
8967 error_return_bad_verdef:
8968 _bfd_error_handler
8969 (_("%pB: .gnu.version_d invalid entry"), abfd);
8970 bfd_set_error (bfd_error_bad_value);
8971 error_return_verdef:
8972 elf_tdata (abfd)->verdef = NULL;
8973 elf_tdata (abfd)->cverdefs = 0;
8974 goto error_return;
8977 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8978 goto error_return_verdef;
8979 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8980 if (contents == NULL)
8981 goto error_return_verdef;
8983 BFD_ASSERT (sizeof (Elf_External_Verdef)
8984 >= sizeof (Elf_External_Verdaux));
8985 contents_end_def = contents + hdr->sh_size
8986 - sizeof (Elf_External_Verdef);
8987 contents_end_aux = contents + hdr->sh_size
8988 - sizeof (Elf_External_Verdaux);
8990 /* We know the number of entries in the section but not the maximum
8991 index. Therefore we have to run through all entries and find
8992 the maximum. */
8993 everdef = (Elf_External_Verdef *) contents;
8994 maxidx = 0;
8995 for (i = 0; i < hdr->sh_info; ++i)
8997 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8999 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9000 goto error_return_bad_verdef;
9001 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9002 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9004 if (iverdefmem.vd_next == 0)
9005 break;
9007 if (iverdefmem.vd_next
9008 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9009 goto error_return_bad_verdef;
9011 everdef = ((Elf_External_Verdef *)
9012 ((bfd_byte *) everdef + iverdefmem.vd_next));
9015 if (default_imported_symver)
9017 if (freeidx > maxidx)
9018 maxidx = ++freeidx;
9019 else
9020 freeidx = ++maxidx;
9022 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9024 bfd_set_error (bfd_error_file_too_big);
9025 goto error_return_verdef;
9028 if (amt == 0)
9029 goto error_return_verdef;
9030 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9031 if (elf_tdata (abfd)->verdef == NULL)
9032 goto error_return_verdef;
9034 elf_tdata (abfd)->cverdefs = maxidx;
9036 everdef = (Elf_External_Verdef *) contents;
9037 iverdefarr = elf_tdata (abfd)->verdef;
9038 for (i = 0; i < hdr->sh_info; i++)
9040 Elf_External_Verdaux *everdaux;
9041 Elf_Internal_Verdaux *iverdaux;
9042 unsigned int j;
9044 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9046 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9047 goto error_return_bad_verdef;
9049 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9050 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9052 iverdef->vd_bfd = abfd;
9054 if (iverdef->vd_cnt == 0)
9055 iverdef->vd_auxptr = NULL;
9056 else
9058 if (_bfd_mul_overflow (iverdef->vd_cnt,
9059 sizeof (Elf_Internal_Verdaux), &amt))
9061 bfd_set_error (bfd_error_file_too_big);
9062 goto error_return_verdef;
9064 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9065 bfd_alloc (abfd, amt);
9066 if (iverdef->vd_auxptr == NULL)
9067 goto error_return_verdef;
9070 if (iverdef->vd_aux
9071 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9072 goto error_return_bad_verdef;
9074 everdaux = ((Elf_External_Verdaux *)
9075 ((bfd_byte *) everdef + iverdef->vd_aux));
9076 iverdaux = iverdef->vd_auxptr;
9077 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9079 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9081 iverdaux->vda_nodename =
9082 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9083 iverdaux->vda_name);
9084 if (iverdaux->vda_nodename == NULL)
9085 goto error_return_bad_verdef;
9087 iverdaux->vda_nextptr = NULL;
9088 if (iverdaux->vda_next == 0)
9090 iverdef->vd_cnt = j + 1;
9091 break;
9093 if (j + 1 < iverdef->vd_cnt)
9094 iverdaux->vda_nextptr = iverdaux + 1;
9096 if (iverdaux->vda_next
9097 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9098 goto error_return_bad_verdef;
9100 everdaux = ((Elf_External_Verdaux *)
9101 ((bfd_byte *) everdaux + iverdaux->vda_next));
9104 iverdef->vd_nodename = NULL;
9105 if (iverdef->vd_cnt)
9106 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9108 iverdef->vd_nextdef = NULL;
9109 if (iverdef->vd_next == 0)
9110 break;
9111 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9112 iverdef->vd_nextdef = iverdef + 1;
9114 everdef = ((Elf_External_Verdef *)
9115 ((bfd_byte *) everdef + iverdef->vd_next));
9118 free (contents);
9119 contents = NULL;
9121 else if (default_imported_symver)
9123 if (freeidx < 3)
9124 freeidx = 3;
9125 else
9126 freeidx++;
9128 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9130 bfd_set_error (bfd_error_file_too_big);
9131 goto error_return;
9133 if (amt == 0)
9134 goto error_return;
9135 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9136 if (elf_tdata (abfd)->verdef == NULL)
9137 goto error_return;
9139 elf_tdata (abfd)->cverdefs = freeidx;
9142 /* Create a default version based on the soname. */
9143 if (default_imported_symver)
9145 Elf_Internal_Verdef *iverdef;
9146 Elf_Internal_Verdaux *iverdaux;
9148 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9150 iverdef->vd_version = VER_DEF_CURRENT;
9151 iverdef->vd_flags = 0;
9152 iverdef->vd_ndx = freeidx;
9153 iverdef->vd_cnt = 1;
9155 iverdef->vd_bfd = abfd;
9157 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9158 if (iverdef->vd_nodename == NULL)
9159 goto error_return_verdef;
9160 iverdef->vd_nextdef = NULL;
9161 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9162 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9163 if (iverdef->vd_auxptr == NULL)
9164 goto error_return_verdef;
9166 iverdaux = iverdef->vd_auxptr;
9167 iverdaux->vda_nodename = iverdef->vd_nodename;
9170 return true;
9172 error_return:
9173 free (contents);
9174 return false;
9177 asymbol *
9178 _bfd_elf_make_empty_symbol (bfd *abfd)
9180 elf_symbol_type *newsym;
9182 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9183 if (!newsym)
9184 return NULL;
9185 newsym->symbol.the_bfd = abfd;
9186 return &newsym->symbol;
9189 void
9190 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9191 asymbol *symbol,
9192 symbol_info *ret)
9194 bfd_symbol_info (symbol, ret);
9197 /* Return whether a symbol name implies a local symbol. Most targets
9198 use this function for the is_local_label_name entry point, but some
9199 override it. */
9201 bool
9202 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9203 const char *name)
9205 /* Normal local symbols start with ``.L''. */
9206 if (name[0] == '.' && name[1] == 'L')
9207 return true;
9209 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9210 DWARF debugging symbols starting with ``..''. */
9211 if (name[0] == '.' && name[1] == '.')
9212 return true;
9214 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9215 emitting DWARF debugging output. I suspect this is actually a
9216 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9217 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9218 underscore to be emitted on some ELF targets). For ease of use,
9219 we treat such symbols as local. */
9220 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9221 return true;
9223 /* Treat assembler generated fake symbols, dollar local labels and
9224 forward-backward labels (aka local labels) as locals.
9225 These labels have the form:
9227 L0^A.* (fake symbols)
9229 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9231 Versions which start with .L will have already been matched above,
9232 so we only need to match the rest. */
9233 if (name[0] == 'L' && ISDIGIT (name[1]))
9235 bool ret = false;
9236 const char * p;
9237 char c;
9239 for (p = name + 2; (c = *p); p++)
9241 if (c == 1 || c == 2)
9243 if (c == 1 && p == name + 2)
9244 /* A fake symbol. */
9245 return true;
9247 /* FIXME: We are being paranoid here and treating symbols like
9248 L0^Bfoo as if there were non-local, on the grounds that the
9249 assembler will never generate them. But can any symbol
9250 containing an ASCII value in the range 1-31 ever be anything
9251 other than some kind of local ? */
9252 ret = true;
9255 if (! ISDIGIT (c))
9257 ret = false;
9258 break;
9261 return ret;
9264 return false;
9267 alent *
9268 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9269 asymbol *symbol ATTRIBUTE_UNUSED)
9271 abort ();
9272 return NULL;
9275 bool
9276 _bfd_elf_set_arch_mach (bfd *abfd,
9277 enum bfd_architecture arch,
9278 unsigned long machine)
9280 /* If this isn't the right architecture for this backend, and this
9281 isn't the generic backend, fail. */
9282 if (arch != get_elf_backend_data (abfd)->arch
9283 && arch != bfd_arch_unknown
9284 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9285 return false;
9287 return bfd_default_set_arch_mach (abfd, arch, machine);
9290 /* Find the nearest line to a particular section and offset,
9291 for error reporting. */
9293 bool
9294 _bfd_elf_find_nearest_line (bfd *abfd,
9295 asymbol **symbols,
9296 asection *section,
9297 bfd_vma offset,
9298 const char **filename_ptr,
9299 const char **functionname_ptr,
9300 unsigned int *line_ptr,
9301 unsigned int *discriminator_ptr)
9303 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9304 offset, filename_ptr,
9305 functionname_ptr, line_ptr,
9306 discriminator_ptr);
9309 /* Find the nearest line to a particular section and offset,
9310 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9311 can be optionally specified. */
9313 bool
9314 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9315 const char *alt_filename,
9316 asymbol **symbols,
9317 asection *section,
9318 bfd_vma offset,
9319 const char **filename_ptr,
9320 const char **functionname_ptr,
9321 unsigned int *line_ptr,
9322 unsigned int *discriminator_ptr)
9324 bool found;
9326 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9327 section, offset, filename_ptr,
9328 functionname_ptr, line_ptr,
9329 discriminator_ptr,
9330 dwarf_debug_sections,
9331 &elf_tdata (abfd)->dwarf2_find_line_info))
9332 return true;
9334 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9335 filename_ptr, functionname_ptr, line_ptr))
9337 if (!*functionname_ptr)
9338 _bfd_elf_find_function (abfd, symbols, section, offset,
9339 *filename_ptr ? NULL : filename_ptr,
9340 functionname_ptr);
9341 return true;
9344 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9345 &found, filename_ptr,
9346 functionname_ptr, line_ptr,
9347 &elf_tdata (abfd)->line_info))
9348 return false;
9349 if (found && (*functionname_ptr || *line_ptr))
9350 return true;
9352 if (symbols == NULL)
9353 return false;
9355 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9356 filename_ptr, functionname_ptr))
9357 return false;
9359 *line_ptr = 0;
9360 return true;
9363 /* Find the line for a symbol. */
9365 bool
9366 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9367 const char **filename_ptr, unsigned int *line_ptr)
9369 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9370 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9371 filename_ptr, NULL, line_ptr, NULL,
9372 dwarf_debug_sections,
9373 &tdata->dwarf2_find_line_info);
9376 /* After a call to bfd_find_nearest_line, successive calls to
9377 bfd_find_inliner_info can be used to get source information about
9378 each level of function inlining that terminated at the address
9379 passed to bfd_find_nearest_line. Currently this is only supported
9380 for DWARF2 with appropriate DWARF3 extensions. */
9382 bool
9383 _bfd_elf_find_inliner_info (bfd *abfd,
9384 const char **filename_ptr,
9385 const char **functionname_ptr,
9386 unsigned int *line_ptr)
9388 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9389 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9390 functionname_ptr, line_ptr,
9391 &tdata->dwarf2_find_line_info);
9395 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9397 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9398 int ret = bed->s->sizeof_ehdr;
9400 if (!bfd_link_relocatable (info))
9402 bfd_size_type phdr_size = elf_program_header_size (abfd);
9404 if (phdr_size == (bfd_size_type) -1)
9406 struct elf_segment_map *m;
9408 phdr_size = 0;
9409 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9410 phdr_size += bed->s->sizeof_phdr;
9412 if (phdr_size == 0)
9413 phdr_size = get_program_header_size (abfd, info);
9416 elf_program_header_size (abfd) = phdr_size;
9417 ret += phdr_size;
9420 return ret;
9423 bool
9424 _bfd_elf_set_section_contents (bfd *abfd,
9425 sec_ptr section,
9426 const void *location,
9427 file_ptr offset,
9428 bfd_size_type count)
9430 Elf_Internal_Shdr *hdr;
9432 if (! abfd->output_has_begun
9433 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9434 return false;
9436 if (!count)
9437 return true;
9439 hdr = &elf_section_data (section)->this_hdr;
9440 if (hdr->sh_offset == (file_ptr) -1)
9442 unsigned char *contents;
9444 if (bfd_section_is_ctf (section))
9445 /* Nothing to do with this section: the contents are generated
9446 later. */
9447 return true;
9449 if ((offset + count) > hdr->sh_size)
9451 _bfd_error_handler
9452 (_("%pB:%pA: error: attempting to write"
9453 " over the end of the section"),
9454 abfd, section);
9456 bfd_set_error (bfd_error_invalid_operation);
9457 return false;
9460 contents = hdr->contents;
9461 if (contents == NULL)
9463 _bfd_error_handler
9464 (_("%pB:%pA: error: attempting to write"
9465 " section into an empty buffer"),
9466 abfd, section);
9468 bfd_set_error (bfd_error_invalid_operation);
9469 return false;
9472 memcpy (contents + offset, location, count);
9473 return true;
9476 return _bfd_generic_set_section_contents (abfd, section,
9477 location, offset, count);
9480 bool
9481 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9482 arelent *cache_ptr ATTRIBUTE_UNUSED,
9483 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9485 abort ();
9486 return false;
9489 /* Try to convert a non-ELF reloc into an ELF one. */
9491 bool
9492 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9494 /* Check whether we really have an ELF howto. */
9496 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9498 bfd_reloc_code_real_type code;
9499 reloc_howto_type *howto;
9501 /* Alien reloc: Try to determine its type to replace it with an
9502 equivalent ELF reloc. */
9504 if (areloc->howto->pc_relative)
9506 switch (areloc->howto->bitsize)
9508 case 8:
9509 code = BFD_RELOC_8_PCREL;
9510 break;
9511 case 12:
9512 code = BFD_RELOC_12_PCREL;
9513 break;
9514 case 16:
9515 code = BFD_RELOC_16_PCREL;
9516 break;
9517 case 24:
9518 code = BFD_RELOC_24_PCREL;
9519 break;
9520 case 32:
9521 code = BFD_RELOC_32_PCREL;
9522 break;
9523 case 64:
9524 code = BFD_RELOC_64_PCREL;
9525 break;
9526 default:
9527 goto fail;
9530 howto = bfd_reloc_type_lookup (abfd, code);
9532 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9534 if (howto->pcrel_offset)
9535 areloc->addend += areloc->address;
9536 else
9537 areloc->addend -= areloc->address; /* addend is unsigned!! */
9540 else
9542 switch (areloc->howto->bitsize)
9544 case 8:
9545 code = BFD_RELOC_8;
9546 break;
9547 case 14:
9548 code = BFD_RELOC_14;
9549 break;
9550 case 16:
9551 code = BFD_RELOC_16;
9552 break;
9553 case 26:
9554 code = BFD_RELOC_26;
9555 break;
9556 case 32:
9557 code = BFD_RELOC_32;
9558 break;
9559 case 64:
9560 code = BFD_RELOC_64;
9561 break;
9562 default:
9563 goto fail;
9566 howto = bfd_reloc_type_lookup (abfd, code);
9569 if (howto)
9570 areloc->howto = howto;
9571 else
9572 goto fail;
9575 return true;
9577 fail:
9578 /* xgettext:c-format */
9579 _bfd_error_handler (_("%pB: %s unsupported"),
9580 abfd, areloc->howto->name);
9581 bfd_set_error (bfd_error_sorry);
9582 return false;
9585 bool
9586 _bfd_elf_close_and_cleanup (bfd *abfd)
9588 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9589 if (tdata != NULL
9590 && (bfd_get_format (abfd) == bfd_object
9591 || bfd_get_format (abfd) == bfd_core))
9593 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9594 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9595 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9596 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
9597 _bfd_stab_cleanup (abfd, &tdata->line_info);
9600 return _bfd_generic_close_and_cleanup (abfd);
9603 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9604 in the relocation's offset. Thus we cannot allow any sort of sanity
9605 range-checking to interfere. There is nothing else to do in processing
9606 this reloc. */
9608 bfd_reloc_status_type
9609 _bfd_elf_rel_vtable_reloc_fn
9610 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9611 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9612 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9613 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9615 return bfd_reloc_ok;
9618 /* Elf core file support. Much of this only works on native
9619 toolchains, since we rely on knowing the
9620 machine-dependent procfs structure in order to pick
9621 out details about the corefile. */
9623 #ifdef HAVE_SYS_PROCFS_H
9624 # include <sys/procfs.h>
9625 #endif
9627 /* Return a PID that identifies a "thread" for threaded cores, or the
9628 PID of the main process for non-threaded cores. */
9630 static int
9631 elfcore_make_pid (bfd *abfd)
9633 int pid;
9635 pid = elf_tdata (abfd)->core->lwpid;
9636 if (pid == 0)
9637 pid = elf_tdata (abfd)->core->pid;
9639 return pid;
9642 /* If there isn't a section called NAME, make one, using data from
9643 SECT. Note, this function will generate a reference to NAME, so
9644 you shouldn't deallocate or overwrite it. */
9646 static bool
9647 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9649 asection *sect2;
9651 if (bfd_get_section_by_name (abfd, name) != NULL)
9652 return true;
9654 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9655 if (sect2 == NULL)
9656 return false;
9658 sect2->size = sect->size;
9659 sect2->filepos = sect->filepos;
9660 sect2->alignment_power = sect->alignment_power;
9661 return true;
9664 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9665 actually creates up to two pseudosections:
9666 - For the single-threaded case, a section named NAME, unless
9667 such a section already exists.
9668 - For the multi-threaded case, a section named "NAME/PID", where
9669 PID is elfcore_make_pid (abfd).
9670 Both pseudosections have identical contents. */
9671 bool
9672 _bfd_elfcore_make_pseudosection (bfd *abfd,
9673 char *name,
9674 size_t size,
9675 ufile_ptr filepos)
9677 char buf[100];
9678 char *threaded_name;
9679 size_t len;
9680 asection *sect;
9682 /* Build the section name. */
9684 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9685 len = strlen (buf) + 1;
9686 threaded_name = (char *) bfd_alloc (abfd, len);
9687 if (threaded_name == NULL)
9688 return false;
9689 memcpy (threaded_name, buf, len);
9691 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9692 SEC_HAS_CONTENTS);
9693 if (sect == NULL)
9694 return false;
9695 sect->size = size;
9696 sect->filepos = filepos;
9697 sect->alignment_power = 2;
9699 return elfcore_maybe_make_sect (abfd, name, sect);
9702 static bool
9703 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9704 size_t offs)
9706 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9707 SEC_HAS_CONTENTS);
9709 if (sect == NULL)
9710 return false;
9712 sect->size = note->descsz - offs;
9713 sect->filepos = note->descpos + offs;
9714 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9716 return true;
9719 /* prstatus_t exists on:
9720 solaris 2.5+
9721 linux 2.[01] + glibc
9722 unixware 4.2
9725 #if defined (HAVE_PRSTATUS_T)
9727 static bool
9728 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9730 size_t size;
9731 int offset;
9733 if (note->descsz == sizeof (prstatus_t))
9735 prstatus_t prstat;
9737 size = sizeof (prstat.pr_reg);
9738 offset = offsetof (prstatus_t, pr_reg);
9739 memcpy (&prstat, note->descdata, sizeof (prstat));
9741 /* Do not overwrite the core signal if it
9742 has already been set by another thread. */
9743 if (elf_tdata (abfd)->core->signal == 0)
9744 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9745 if (elf_tdata (abfd)->core->pid == 0)
9746 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9748 /* pr_who exists on:
9749 solaris 2.5+
9750 unixware 4.2
9751 pr_who doesn't exist on:
9752 linux 2.[01]
9754 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9755 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9756 #else
9757 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9758 #endif
9760 #if defined (HAVE_PRSTATUS32_T)
9761 else if (note->descsz == sizeof (prstatus32_t))
9763 /* 64-bit host, 32-bit corefile */
9764 prstatus32_t prstat;
9766 size = sizeof (prstat.pr_reg);
9767 offset = offsetof (prstatus32_t, pr_reg);
9768 memcpy (&prstat, note->descdata, sizeof (prstat));
9770 /* Do not overwrite the core signal if it
9771 has already been set by another thread. */
9772 if (elf_tdata (abfd)->core->signal == 0)
9773 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9774 if (elf_tdata (abfd)->core->pid == 0)
9775 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9777 /* pr_who exists on:
9778 solaris 2.5+
9779 unixware 4.2
9780 pr_who doesn't exist on:
9781 linux 2.[01]
9783 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9784 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9785 #else
9786 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9787 #endif
9789 #endif /* HAVE_PRSTATUS32_T */
9790 else
9792 /* Fail - we don't know how to handle any other
9793 note size (ie. data object type). */
9794 return true;
9797 /* Make a ".reg/999" section and a ".reg" section. */
9798 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9799 size, note->descpos + offset);
9801 #endif /* defined (HAVE_PRSTATUS_T) */
9803 /* Create a pseudosection containing the exact contents of NOTE. */
9804 static bool
9805 elfcore_make_note_pseudosection (bfd *abfd,
9806 char *name,
9807 Elf_Internal_Note *note)
9809 return _bfd_elfcore_make_pseudosection (abfd, name,
9810 note->descsz, note->descpos);
9813 /* There isn't a consistent prfpregset_t across platforms,
9814 but it doesn't matter, because we don't have to pick this
9815 data structure apart. */
9817 static bool
9818 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9820 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9823 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9824 type of NT_PRXFPREG. Just include the whole note's contents
9825 literally. */
9827 static bool
9828 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9830 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9833 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9834 with a note type of NT_X86_XSTATE. Just include the whole note's
9835 contents literally. */
9837 static bool
9838 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9840 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9843 static bool
9844 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9846 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9849 static bool
9850 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9852 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9855 static bool
9856 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9858 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9861 static bool
9862 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9864 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9867 static bool
9868 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9870 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9873 static bool
9874 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9876 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9879 static bool
9880 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9882 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9885 static bool
9886 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9888 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9891 static bool
9892 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9894 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9897 static bool
9898 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9900 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9903 static bool
9904 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9906 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9909 static bool
9910 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9912 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9915 static bool
9916 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9918 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9921 static bool
9922 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9924 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9927 static bool
9928 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9930 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9933 static bool
9934 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9936 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9939 static bool
9940 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9942 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9945 static bool
9946 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9948 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9951 static bool
9952 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9954 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9957 static bool
9958 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9960 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9963 static bool
9964 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9966 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9969 static bool
9970 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9972 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9975 static bool
9976 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9978 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9981 static bool
9982 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9984 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9987 static bool
9988 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9990 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9993 static bool
9994 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9996 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9999 static bool
10000 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10002 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10005 static bool
10006 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10008 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10011 static bool
10012 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10014 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10017 static bool
10018 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10020 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10023 static bool
10024 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10026 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10029 static bool
10030 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10032 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10035 static bool
10036 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10038 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10041 static bool
10042 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10044 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10047 static bool
10048 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10050 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10051 note);
10054 static bool
10055 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10057 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10060 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10061 successful otherwise, return FALSE. */
10063 static bool
10064 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10066 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10069 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10070 successful otherwise, return FALSE. */
10072 static bool
10073 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10075 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10078 static bool
10079 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10081 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10084 static bool
10085 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10087 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10090 static bool
10091 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10093 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10096 static bool
10097 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10099 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10102 #if defined (HAVE_PRPSINFO_T)
10103 typedef prpsinfo_t elfcore_psinfo_t;
10104 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10105 typedef prpsinfo32_t elfcore_psinfo32_t;
10106 #endif
10107 #endif
10109 #if defined (HAVE_PSINFO_T)
10110 typedef psinfo_t elfcore_psinfo_t;
10111 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10112 typedef psinfo32_t elfcore_psinfo32_t;
10113 #endif
10114 #endif
10116 /* return a malloc'ed copy of a string at START which is at
10117 most MAX bytes long, possibly without a terminating '\0'.
10118 the copy will always have a terminating '\0'. */
10120 char *
10121 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10123 char *dups;
10124 char *end = (char *) memchr (start, '\0', max);
10125 size_t len;
10127 if (end == NULL)
10128 len = max;
10129 else
10130 len = end - start;
10132 dups = (char *) bfd_alloc (abfd, len + 1);
10133 if (dups == NULL)
10134 return NULL;
10136 memcpy (dups, start, len);
10137 dups[len] = '\0';
10139 return dups;
10142 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10143 static bool
10144 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10146 if (note->descsz == sizeof (elfcore_psinfo_t))
10148 elfcore_psinfo_t psinfo;
10150 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10152 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10153 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10154 #endif
10155 elf_tdata (abfd)->core->program
10156 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10157 sizeof (psinfo.pr_fname));
10159 elf_tdata (abfd)->core->command
10160 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10161 sizeof (psinfo.pr_psargs));
10163 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10164 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10166 /* 64-bit host, 32-bit corefile */
10167 elfcore_psinfo32_t psinfo;
10169 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10171 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10172 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10173 #endif
10174 elf_tdata (abfd)->core->program
10175 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10176 sizeof (psinfo.pr_fname));
10178 elf_tdata (abfd)->core->command
10179 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10180 sizeof (psinfo.pr_psargs));
10182 #endif
10184 else
10186 /* Fail - we don't know how to handle any other
10187 note size (ie. data object type). */
10188 return true;
10191 /* Note that for some reason, a spurious space is tacked
10192 onto the end of the args in some (at least one anyway)
10193 implementations, so strip it off if it exists. */
10196 char *command = elf_tdata (abfd)->core->command;
10197 int n = strlen (command);
10199 if (0 < n && command[n - 1] == ' ')
10200 command[n - 1] = '\0';
10203 return true;
10205 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10207 #if defined (HAVE_PSTATUS_T)
10208 static bool
10209 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10211 if (note->descsz == sizeof (pstatus_t)
10212 #if defined (HAVE_PXSTATUS_T)
10213 || note->descsz == sizeof (pxstatus_t)
10214 #endif
10217 pstatus_t pstat;
10219 memcpy (&pstat, note->descdata, sizeof (pstat));
10221 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10223 #if defined (HAVE_PSTATUS32_T)
10224 else if (note->descsz == sizeof (pstatus32_t))
10226 /* 64-bit host, 32-bit corefile */
10227 pstatus32_t pstat;
10229 memcpy (&pstat, note->descdata, sizeof (pstat));
10231 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10233 #endif
10234 /* Could grab some more details from the "representative"
10235 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10236 NT_LWPSTATUS note, presumably. */
10238 return true;
10240 #endif /* defined (HAVE_PSTATUS_T) */
10242 #if defined (HAVE_LWPSTATUS_T)
10243 static bool
10244 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10246 lwpstatus_t lwpstat;
10247 char buf[100];
10248 char *name;
10249 size_t len;
10250 asection *sect;
10252 if (note->descsz != sizeof (lwpstat)
10253 #if defined (HAVE_LWPXSTATUS_T)
10254 && note->descsz != sizeof (lwpxstatus_t)
10255 #endif
10257 return true;
10259 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10261 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10262 /* Do not overwrite the core signal if it has already been set by
10263 another thread. */
10264 if (elf_tdata (abfd)->core->signal == 0)
10265 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10267 /* Make a ".reg/999" section. */
10269 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10270 len = strlen (buf) + 1;
10271 name = bfd_alloc (abfd, len);
10272 if (name == NULL)
10273 return false;
10274 memcpy (name, buf, len);
10276 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10277 if (sect == NULL)
10278 return false;
10280 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10281 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10282 sect->filepos = note->descpos
10283 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10284 #endif
10286 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10287 sect->size = sizeof (lwpstat.pr_reg);
10288 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10289 #endif
10291 sect->alignment_power = 2;
10293 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10294 return false;
10296 /* Make a ".reg2/999" section */
10298 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10299 len = strlen (buf) + 1;
10300 name = bfd_alloc (abfd, len);
10301 if (name == NULL)
10302 return false;
10303 memcpy (name, buf, len);
10305 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10306 if (sect == NULL)
10307 return false;
10309 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10310 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10311 sect->filepos = note->descpos
10312 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10313 #endif
10315 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10316 sect->size = sizeof (lwpstat.pr_fpreg);
10317 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10318 #endif
10320 sect->alignment_power = 2;
10322 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10324 #endif /* defined (HAVE_LWPSTATUS_T) */
10326 /* These constants, and the structure offsets used below, are defined by
10327 Cygwin's core_dump.h */
10328 #define NOTE_INFO_PROCESS 1
10329 #define NOTE_INFO_THREAD 2
10330 #define NOTE_INFO_MODULE 3
10331 #define NOTE_INFO_MODULE64 4
10333 static bool
10334 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10336 char buf[30];
10337 char *name;
10338 size_t len;
10339 unsigned int name_size;
10340 asection *sect;
10341 unsigned int type;
10342 int is_active_thread;
10343 bfd_vma base_addr;
10345 if (note->descsz < 4)
10346 return true;
10348 if (! startswith (note->namedata, "win32"))
10349 return true;
10351 type = bfd_get_32 (abfd, note->descdata);
10353 struct
10355 const char *type_name;
10356 unsigned long min_size;
10357 } size_check[] =
10359 { "NOTE_INFO_PROCESS", 12 },
10360 { "NOTE_INFO_THREAD", 12 },
10361 { "NOTE_INFO_MODULE", 12 },
10362 { "NOTE_INFO_MODULE64", 16 },
10365 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10366 return true;
10368 if (note->descsz < size_check[type - 1].min_size)
10370 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10371 " is too small"),
10372 abfd, size_check[type - 1].type_name, note->descsz);
10373 return true;
10376 switch (type)
10378 case NOTE_INFO_PROCESS:
10379 /* FIXME: need to add ->core->command. */
10380 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10381 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10382 break;
10384 case NOTE_INFO_THREAD:
10385 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10386 structure. */
10387 /* thread_info.tid */
10388 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10390 len = strlen (buf) + 1;
10391 name = (char *) bfd_alloc (abfd, len);
10392 if (name == NULL)
10393 return false;
10395 memcpy (name, buf, len);
10397 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10398 if (sect == NULL)
10399 return false;
10401 /* sizeof (thread_info.thread_context) */
10402 sect->size = note->descsz - 12;
10403 /* offsetof (thread_info.thread_context) */
10404 sect->filepos = note->descpos + 12;
10405 sect->alignment_power = 2;
10407 /* thread_info.is_active_thread */
10408 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10410 if (is_active_thread)
10411 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10412 return false;
10413 break;
10415 case NOTE_INFO_MODULE:
10416 case NOTE_INFO_MODULE64:
10417 /* Make a ".module/xxxxxxxx" section. */
10418 if (type == NOTE_INFO_MODULE)
10420 /* module_info.base_address */
10421 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10422 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10423 /* module_info.module_name_size */
10424 name_size = bfd_get_32 (abfd, note->descdata + 8);
10426 else /* NOTE_INFO_MODULE64 */
10428 /* module_info.base_address */
10429 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10430 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10431 /* module_info.module_name_size */
10432 name_size = bfd_get_32 (abfd, note->descdata + 12);
10435 len = strlen (buf) + 1;
10436 name = (char *) bfd_alloc (abfd, len);
10437 if (name == NULL)
10438 return false;
10440 memcpy (name, buf, len);
10442 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10444 if (sect == NULL)
10445 return false;
10447 if (note->descsz < 12 + name_size)
10449 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10450 " is too small to contain a name of size %u"),
10451 abfd, note->descsz, name_size);
10452 return true;
10455 sect->size = note->descsz;
10456 sect->filepos = note->descpos;
10457 sect->alignment_power = 2;
10458 break;
10460 default:
10461 return true;
10464 return true;
10467 static bool
10468 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10470 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10472 switch (note->type)
10474 default:
10475 return true;
10477 case NT_PRSTATUS:
10478 if (bed->elf_backend_grok_prstatus)
10479 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10480 return true;
10481 #if defined (HAVE_PRSTATUS_T)
10482 return elfcore_grok_prstatus (abfd, note);
10483 #else
10484 return true;
10485 #endif
10487 #if defined (HAVE_PSTATUS_T)
10488 case NT_PSTATUS:
10489 return elfcore_grok_pstatus (abfd, note);
10490 #endif
10492 #if defined (HAVE_LWPSTATUS_T)
10493 case NT_LWPSTATUS:
10494 return elfcore_grok_lwpstatus (abfd, note);
10495 #endif
10497 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10498 return elfcore_grok_prfpreg (abfd, note);
10500 case NT_WIN32PSTATUS:
10501 return elfcore_grok_win32pstatus (abfd, note);
10503 case NT_PRXFPREG: /* Linux SSE extension */
10504 if (note->namesz == 6
10505 && strcmp (note->namedata, "LINUX") == 0)
10506 return elfcore_grok_prxfpreg (abfd, note);
10507 else
10508 return true;
10510 case NT_X86_XSTATE: /* Linux XSAVE extension */
10511 if (note->namesz == 6
10512 && strcmp (note->namedata, "LINUX") == 0)
10513 return elfcore_grok_xstatereg (abfd, note);
10514 else
10515 return true;
10517 case NT_PPC_VMX:
10518 if (note->namesz == 6
10519 && strcmp (note->namedata, "LINUX") == 0)
10520 return elfcore_grok_ppc_vmx (abfd, note);
10521 else
10522 return true;
10524 case NT_PPC_VSX:
10525 if (note->namesz == 6
10526 && strcmp (note->namedata, "LINUX") == 0)
10527 return elfcore_grok_ppc_vsx (abfd, note);
10528 else
10529 return true;
10531 case NT_PPC_TAR:
10532 if (note->namesz == 6
10533 && strcmp (note->namedata, "LINUX") == 0)
10534 return elfcore_grok_ppc_tar (abfd, note);
10535 else
10536 return true;
10538 case NT_PPC_PPR:
10539 if (note->namesz == 6
10540 && strcmp (note->namedata, "LINUX") == 0)
10541 return elfcore_grok_ppc_ppr (abfd, note);
10542 else
10543 return true;
10545 case NT_PPC_DSCR:
10546 if (note->namesz == 6
10547 && strcmp (note->namedata, "LINUX") == 0)
10548 return elfcore_grok_ppc_dscr (abfd, note);
10549 else
10550 return true;
10552 case NT_PPC_EBB:
10553 if (note->namesz == 6
10554 && strcmp (note->namedata, "LINUX") == 0)
10555 return elfcore_grok_ppc_ebb (abfd, note);
10556 else
10557 return true;
10559 case NT_PPC_PMU:
10560 if (note->namesz == 6
10561 && strcmp (note->namedata, "LINUX") == 0)
10562 return elfcore_grok_ppc_pmu (abfd, note);
10563 else
10564 return true;
10566 case NT_PPC_TM_CGPR:
10567 if (note->namesz == 6
10568 && strcmp (note->namedata, "LINUX") == 0)
10569 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10570 else
10571 return true;
10573 case NT_PPC_TM_CFPR:
10574 if (note->namesz == 6
10575 && strcmp (note->namedata, "LINUX") == 0)
10576 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10577 else
10578 return true;
10580 case NT_PPC_TM_CVMX:
10581 if (note->namesz == 6
10582 && strcmp (note->namedata, "LINUX") == 0)
10583 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10584 else
10585 return true;
10587 case NT_PPC_TM_CVSX:
10588 if (note->namesz == 6
10589 && strcmp (note->namedata, "LINUX") == 0)
10590 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10591 else
10592 return true;
10594 case NT_PPC_TM_SPR:
10595 if (note->namesz == 6
10596 && strcmp (note->namedata, "LINUX") == 0)
10597 return elfcore_grok_ppc_tm_spr (abfd, note);
10598 else
10599 return true;
10601 case NT_PPC_TM_CTAR:
10602 if (note->namesz == 6
10603 && strcmp (note->namedata, "LINUX") == 0)
10604 return elfcore_grok_ppc_tm_ctar (abfd, note);
10605 else
10606 return true;
10608 case NT_PPC_TM_CPPR:
10609 if (note->namesz == 6
10610 && strcmp (note->namedata, "LINUX") == 0)
10611 return elfcore_grok_ppc_tm_cppr (abfd, note);
10612 else
10613 return true;
10615 case NT_PPC_TM_CDSCR:
10616 if (note->namesz == 6
10617 && strcmp (note->namedata, "LINUX") == 0)
10618 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10619 else
10620 return true;
10622 case NT_S390_HIGH_GPRS:
10623 if (note->namesz == 6
10624 && strcmp (note->namedata, "LINUX") == 0)
10625 return elfcore_grok_s390_high_gprs (abfd, note);
10626 else
10627 return true;
10629 case NT_S390_TIMER:
10630 if (note->namesz == 6
10631 && strcmp (note->namedata, "LINUX") == 0)
10632 return elfcore_grok_s390_timer (abfd, note);
10633 else
10634 return true;
10636 case NT_S390_TODCMP:
10637 if (note->namesz == 6
10638 && strcmp (note->namedata, "LINUX") == 0)
10639 return elfcore_grok_s390_todcmp (abfd, note);
10640 else
10641 return true;
10643 case NT_S390_TODPREG:
10644 if (note->namesz == 6
10645 && strcmp (note->namedata, "LINUX") == 0)
10646 return elfcore_grok_s390_todpreg (abfd, note);
10647 else
10648 return true;
10650 case NT_S390_CTRS:
10651 if (note->namesz == 6
10652 && strcmp (note->namedata, "LINUX") == 0)
10653 return elfcore_grok_s390_ctrs (abfd, note);
10654 else
10655 return true;
10657 case NT_S390_PREFIX:
10658 if (note->namesz == 6
10659 && strcmp (note->namedata, "LINUX") == 0)
10660 return elfcore_grok_s390_prefix (abfd, note);
10661 else
10662 return true;
10664 case NT_S390_LAST_BREAK:
10665 if (note->namesz == 6
10666 && strcmp (note->namedata, "LINUX") == 0)
10667 return elfcore_grok_s390_last_break (abfd, note);
10668 else
10669 return true;
10671 case NT_S390_SYSTEM_CALL:
10672 if (note->namesz == 6
10673 && strcmp (note->namedata, "LINUX") == 0)
10674 return elfcore_grok_s390_system_call (abfd, note);
10675 else
10676 return true;
10678 case NT_S390_TDB:
10679 if (note->namesz == 6
10680 && strcmp (note->namedata, "LINUX") == 0)
10681 return elfcore_grok_s390_tdb (abfd, note);
10682 else
10683 return true;
10685 case NT_S390_VXRS_LOW:
10686 if (note->namesz == 6
10687 && strcmp (note->namedata, "LINUX") == 0)
10688 return elfcore_grok_s390_vxrs_low (abfd, note);
10689 else
10690 return true;
10692 case NT_S390_VXRS_HIGH:
10693 if (note->namesz == 6
10694 && strcmp (note->namedata, "LINUX") == 0)
10695 return elfcore_grok_s390_vxrs_high (abfd, note);
10696 else
10697 return true;
10699 case NT_S390_GS_CB:
10700 if (note->namesz == 6
10701 && strcmp (note->namedata, "LINUX") == 0)
10702 return elfcore_grok_s390_gs_cb (abfd, note);
10703 else
10704 return true;
10706 case NT_S390_GS_BC:
10707 if (note->namesz == 6
10708 && strcmp (note->namedata, "LINUX") == 0)
10709 return elfcore_grok_s390_gs_bc (abfd, note);
10710 else
10711 return true;
10713 case NT_ARC_V2:
10714 if (note->namesz == 6
10715 && strcmp (note->namedata, "LINUX") == 0)
10716 return elfcore_grok_arc_v2 (abfd, note);
10717 else
10718 return true;
10720 case NT_ARM_VFP:
10721 if (note->namesz == 6
10722 && strcmp (note->namedata, "LINUX") == 0)
10723 return elfcore_grok_arm_vfp (abfd, note);
10724 else
10725 return true;
10727 case NT_ARM_TLS:
10728 if (note->namesz == 6
10729 && strcmp (note->namedata, "LINUX") == 0)
10730 return elfcore_grok_aarch_tls (abfd, note);
10731 else
10732 return true;
10734 case NT_ARM_HW_BREAK:
10735 if (note->namesz == 6
10736 && strcmp (note->namedata, "LINUX") == 0)
10737 return elfcore_grok_aarch_hw_break (abfd, note);
10738 else
10739 return true;
10741 case NT_ARM_HW_WATCH:
10742 if (note->namesz == 6
10743 && strcmp (note->namedata, "LINUX") == 0)
10744 return elfcore_grok_aarch_hw_watch (abfd, note);
10745 else
10746 return true;
10748 case NT_ARM_SVE:
10749 if (note->namesz == 6
10750 && strcmp (note->namedata, "LINUX") == 0)
10751 return elfcore_grok_aarch_sve (abfd, note);
10752 else
10753 return true;
10755 case NT_ARM_PAC_MASK:
10756 if (note->namesz == 6
10757 && strcmp (note->namedata, "LINUX") == 0)
10758 return elfcore_grok_aarch_pauth (abfd, note);
10759 else
10760 return true;
10762 case NT_ARM_TAGGED_ADDR_CTRL:
10763 if (note->namesz == 6
10764 && strcmp (note->namedata, "LINUX") == 0)
10765 return elfcore_grok_aarch_mte (abfd, note);
10766 else
10767 return true;
10769 case NT_GDB_TDESC:
10770 if (note->namesz == 4
10771 && strcmp (note->namedata, "GDB") == 0)
10772 return elfcore_grok_gdb_tdesc (abfd, note);
10773 else
10774 return true;
10776 case NT_RISCV_CSR:
10777 if (note->namesz == 4
10778 && strcmp (note->namedata, "GDB") == 0)
10779 return elfcore_grok_riscv_csr (abfd, note);
10780 else
10781 return true;
10783 case NT_LARCH_CPUCFG:
10784 if (note->namesz == 6
10785 && strcmp (note->namedata, "LINUX") == 0)
10786 return elfcore_grok_loongarch_cpucfg (abfd, note);
10787 else
10788 return true;
10790 case NT_LARCH_LBT:
10791 if (note->namesz == 6
10792 && strcmp (note->namedata, "LINUX") == 0)
10793 return elfcore_grok_loongarch_lbt (abfd, note);
10794 else
10795 return true;
10797 case NT_LARCH_LSX:
10798 if (note->namesz == 6
10799 && strcmp (note->namedata, "LINUX") == 0)
10800 return elfcore_grok_loongarch_lsx (abfd, note);
10801 else
10802 return true;
10804 case NT_LARCH_LASX:
10805 if (note->namesz == 6
10806 && strcmp (note->namedata, "LINUX") == 0)
10807 return elfcore_grok_loongarch_lasx (abfd, note);
10808 else
10809 return true;
10811 case NT_PRPSINFO:
10812 case NT_PSINFO:
10813 if (bed->elf_backend_grok_psinfo)
10814 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10815 return true;
10816 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10817 return elfcore_grok_psinfo (abfd, note);
10818 #else
10819 return true;
10820 #endif
10822 case NT_AUXV:
10823 return elfcore_make_auxv_note_section (abfd, note, 0);
10825 case NT_FILE:
10826 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10827 note);
10829 case NT_SIGINFO:
10830 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10831 note);
10836 static bool
10837 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10839 struct bfd_build_id* build_id;
10841 if (note->descsz == 0)
10842 return false;
10844 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10845 if (build_id == NULL)
10846 return false;
10848 build_id->size = note->descsz;
10849 memcpy (build_id->data, note->descdata, note->descsz);
10850 abfd->build_id = build_id;
10852 return true;
10855 static bool
10856 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10858 switch (note->type)
10860 default:
10861 return true;
10863 case NT_GNU_PROPERTY_TYPE_0:
10864 return _bfd_elf_parse_gnu_properties (abfd, note);
10866 case NT_GNU_BUILD_ID:
10867 return elfobj_grok_gnu_build_id (abfd, note);
10871 static bool
10872 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10874 struct sdt_note *cur =
10875 (struct sdt_note *) bfd_alloc (abfd,
10876 sizeof (struct sdt_note) + note->descsz);
10878 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10879 cur->size = (bfd_size_type) note->descsz;
10880 memcpy (cur->data, note->descdata, note->descsz);
10882 elf_tdata (abfd)->sdt_note_head = cur;
10884 return true;
10887 static bool
10888 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10890 switch (note->type)
10892 case NT_STAPSDT:
10893 return elfobj_grok_stapsdt_note_1 (abfd, note);
10895 default:
10896 return true;
10900 static bool
10901 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10903 size_t offset;
10905 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10907 case ELFCLASS32:
10908 if (note->descsz < 108)
10909 return false;
10910 break;
10912 case ELFCLASS64:
10913 if (note->descsz < 120)
10914 return false;
10915 break;
10917 default:
10918 return false;
10921 /* Check for version 1 in pr_version. */
10922 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10923 return false;
10925 offset = 4;
10927 /* Skip over pr_psinfosz. */
10928 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10929 offset += 4;
10930 else
10932 offset += 4; /* Padding before pr_psinfosz. */
10933 offset += 8;
10936 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10937 elf_tdata (abfd)->core->program
10938 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10939 offset += 17;
10941 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10942 elf_tdata (abfd)->core->command
10943 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10944 offset += 81;
10946 /* Padding before pr_pid. */
10947 offset += 2;
10949 /* The pr_pid field was added in version "1a". */
10950 if (note->descsz < offset + 4)
10951 return true;
10953 elf_tdata (abfd)->core->pid
10954 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10956 return true;
10959 static bool
10960 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10962 size_t offset;
10963 size_t size;
10964 size_t min_size;
10966 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10967 Also compute minimum size of this note. */
10968 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10970 case ELFCLASS32:
10971 offset = 4 + 4;
10972 min_size = offset + (4 * 2) + 4 + 4 + 4;
10973 break;
10975 case ELFCLASS64:
10976 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10977 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10978 break;
10980 default:
10981 return false;
10984 if (note->descsz < min_size)
10985 return false;
10987 /* Check for version 1 in pr_version. */
10988 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10989 return false;
10991 /* Extract size of pr_reg from pr_gregsetsz. */
10992 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10993 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10995 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10996 offset += 4 * 2;
10998 else
11000 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11001 offset += 8 * 2;
11004 /* Skip over pr_osreldate. */
11005 offset += 4;
11007 /* Read signal from pr_cursig. */
11008 if (elf_tdata (abfd)->core->signal == 0)
11009 elf_tdata (abfd)->core->signal
11010 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11011 offset += 4;
11013 /* Read TID from pr_pid. */
11014 elf_tdata (abfd)->core->lwpid
11015 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11016 offset += 4;
11018 /* Padding before pr_reg. */
11019 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11020 offset += 4;
11022 /* Make sure that there is enough data remaining in the note. */
11023 if ((note->descsz - offset) < size)
11024 return false;
11026 /* Make a ".reg/999" section and a ".reg" section. */
11027 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11028 size, note->descpos + offset);
11031 static bool
11032 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11034 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11036 switch (note->type)
11038 case NT_PRSTATUS:
11039 if (bed->elf_backend_grok_freebsd_prstatus)
11040 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11041 return true;
11042 return elfcore_grok_freebsd_prstatus (abfd, note);
11044 case NT_FPREGSET:
11045 return elfcore_grok_prfpreg (abfd, note);
11047 case NT_PRPSINFO:
11048 return elfcore_grok_freebsd_psinfo (abfd, note);
11050 case NT_FREEBSD_THRMISC:
11051 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11053 case NT_FREEBSD_PROCSTAT_PROC:
11054 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11055 note);
11057 case NT_FREEBSD_PROCSTAT_FILES:
11058 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11059 note);
11061 case NT_FREEBSD_PROCSTAT_VMMAP:
11062 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11063 note);
11065 case NT_FREEBSD_PROCSTAT_AUXV:
11066 return elfcore_make_auxv_note_section (abfd, note, 4);
11068 case NT_FREEBSD_X86_SEGBASES:
11069 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11071 case NT_X86_XSTATE:
11072 return elfcore_grok_xstatereg (abfd, note);
11074 case NT_FREEBSD_PTLWPINFO:
11075 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11076 note);
11078 case NT_ARM_TLS:
11079 return elfcore_grok_aarch_tls (abfd, note);
11081 case NT_ARM_VFP:
11082 return elfcore_grok_arm_vfp (abfd, note);
11084 default:
11085 return true;
11089 static bool
11090 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11092 char *cp;
11094 cp = strchr (note->namedata, '@');
11095 if (cp != NULL)
11097 *lwpidp = atoi(cp + 1);
11098 return true;
11100 return false;
11103 static bool
11104 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11106 if (note->descsz <= 0x7c + 31)
11107 return false;
11109 /* Signal number at offset 0x08. */
11110 elf_tdata (abfd)->core->signal
11111 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11113 /* Process ID at offset 0x50. */
11114 elf_tdata (abfd)->core->pid
11115 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11117 /* Command name at 0x7c (max 32 bytes, including nul). */
11118 elf_tdata (abfd)->core->command
11119 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11121 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11122 note);
11125 static bool
11126 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11128 int lwp;
11130 if (elfcore_netbsd_get_lwpid (note, &lwp))
11131 elf_tdata (abfd)->core->lwpid = lwp;
11133 switch (note->type)
11135 case NT_NETBSDCORE_PROCINFO:
11136 /* NetBSD-specific core "procinfo". Note that we expect to
11137 find this note before any of the others, which is fine,
11138 since the kernel writes this note out first when it
11139 creates a core file. */
11140 return elfcore_grok_netbsd_procinfo (abfd, note);
11141 case NT_NETBSDCORE_AUXV:
11142 /* NetBSD-specific Elf Auxiliary Vector data. */
11143 return elfcore_make_auxv_note_section (abfd, note, 4);
11144 case NT_NETBSDCORE_LWPSTATUS:
11145 return elfcore_make_note_pseudosection (abfd,
11146 ".note.netbsdcore.lwpstatus",
11147 note);
11148 default:
11149 break;
11152 /* As of March 2020 there are no other machine-independent notes
11153 defined for NetBSD core files. If the note type is less
11154 than the start of the machine-dependent note types, we don't
11155 understand it. */
11157 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11158 return true;
11161 switch (bfd_get_arch (abfd))
11163 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11164 PT_GETFPREGS == mach+2. */
11166 case bfd_arch_aarch64:
11167 case bfd_arch_alpha:
11168 case bfd_arch_sparc:
11169 switch (note->type)
11171 case NT_NETBSDCORE_FIRSTMACH+0:
11172 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11174 case NT_NETBSDCORE_FIRSTMACH+2:
11175 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11177 default:
11178 return true;
11181 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11182 There's also old PT___GETREGS40 == mach + 1 for old reg
11183 structure which lacks GBR. */
11185 case bfd_arch_sh:
11186 switch (note->type)
11188 case NT_NETBSDCORE_FIRSTMACH+3:
11189 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11191 case NT_NETBSDCORE_FIRSTMACH+5:
11192 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11194 default:
11195 return true;
11198 /* On all other arch's, PT_GETREGS == mach+1 and
11199 PT_GETFPREGS == mach+3. */
11201 default:
11202 switch (note->type)
11204 case NT_NETBSDCORE_FIRSTMACH+1:
11205 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11207 case NT_NETBSDCORE_FIRSTMACH+3:
11208 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11210 default:
11211 return true;
11214 /* NOTREACHED */
11217 static bool
11218 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11220 if (note->descsz <= 0x48 + 31)
11221 return false;
11223 /* Signal number at offset 0x08. */
11224 elf_tdata (abfd)->core->signal
11225 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11227 /* Process ID at offset 0x20. */
11228 elf_tdata (abfd)->core->pid
11229 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11231 /* Command name at 0x48 (max 32 bytes, including nul). */
11232 elf_tdata (abfd)->core->command
11233 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11235 return true;
11238 /* Processes Solaris's process status note.
11239 sig_off ~ offsetof(prstatus_t, pr_cursig)
11240 pid_off ~ offsetof(prstatus_t, pr_pid)
11241 lwpid_off ~ offsetof(prstatus_t, pr_who)
11242 gregset_size ~ sizeof(gregset_t)
11243 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11245 static bool
11246 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11247 int pid_off, int lwpid_off, size_t gregset_size,
11248 size_t gregset_offset)
11250 asection *sect = NULL;
11251 elf_tdata (abfd)->core->signal
11252 = bfd_get_16 (abfd, note->descdata + sig_off);
11253 elf_tdata (abfd)->core->pid
11254 = bfd_get_32 (abfd, note->descdata + pid_off);
11255 elf_tdata (abfd)->core->lwpid
11256 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11258 sect = bfd_get_section_by_name (abfd, ".reg");
11259 if (sect != NULL)
11260 sect->size = gregset_size;
11262 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11263 note->descpos + gregset_offset);
11266 /* Gets program and arguments from a core.
11267 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11268 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11270 static bool
11271 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11272 int prog_off, int comm_off)
11274 elf_tdata (abfd)->core->program
11275 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11276 elf_tdata (abfd)->core->command
11277 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11279 return true;
11282 /* Processes Solaris's LWP status note.
11283 gregset_size ~ sizeof(gregset_t)
11284 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11285 fpregset_size ~ sizeof(fpregset_t)
11286 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11288 static bool
11289 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11290 size_t gregset_size, int gregset_off,
11291 size_t fpregset_size, int fpregset_off)
11293 asection *sect = NULL;
11294 char reg2_section_name[16] = { 0 };
11296 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11297 elf_tdata (abfd)->core->lwpid);
11299 /* offsetof(lwpstatus_t, pr_lwpid) */
11300 elf_tdata (abfd)->core->lwpid
11301 = bfd_get_32 (abfd, note->descdata + 4);
11302 /* offsetof(lwpstatus_t, pr_cursig) */
11303 elf_tdata (abfd)->core->signal
11304 = bfd_get_16 (abfd, note->descdata + 12);
11306 sect = bfd_get_section_by_name (abfd, ".reg");
11307 if (sect != NULL)
11308 sect->size = gregset_size;
11309 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11310 note->descpos + gregset_off))
11311 return false;
11313 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11314 if (sect != NULL)
11316 sect->size = fpregset_size;
11317 sect->filepos = note->descpos + fpregset_off;
11318 sect->alignment_power = 2;
11320 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11321 note->descpos + fpregset_off))
11322 return false;
11324 return true;
11327 static bool
11328 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11330 if (note == NULL)
11331 return false;
11333 /* core files are identified as 32- or 64-bit, SPARC or x86,
11334 by the size of the descsz which matches the sizeof()
11335 the type appropriate for that note type (e.g., prstatus_t for
11336 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11337 on Solaris. The core file bitness may differ from the bitness of
11338 gdb itself, so fixed values are used instead of sizeof().
11339 Appropriate fixed offsets are also used to obtain data from
11340 the note. */
11342 switch ((int) note->type)
11344 case SOLARIS_NT_PRSTATUS:
11345 switch (note->descsz)
11347 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11348 return elfcore_grok_solaris_prstatus(abfd, note,
11349 136, 216, 308, 152, 356);
11350 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11351 return elfcore_grok_solaris_prstatus(abfd, note,
11352 264, 360, 520, 304, 600);
11353 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11354 return elfcore_grok_solaris_prstatus(abfd, note,
11355 136, 216, 308, 76, 356);
11356 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11357 return elfcore_grok_solaris_prstatus(abfd, note,
11358 264, 360, 520, 224, 600);
11359 default:
11360 return true;
11363 case SOLARIS_NT_PSINFO:
11364 case SOLARIS_NT_PRPSINFO:
11365 switch (note->descsz)
11367 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11368 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11369 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11370 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11371 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11372 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11373 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11374 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11375 default:
11376 return true;
11379 case SOLARIS_NT_LWPSTATUS:
11380 switch (note->descsz)
11382 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11383 return elfcore_grok_solaris_lwpstatus(abfd, note,
11384 152, 344, 400, 496);
11385 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11386 return elfcore_grok_solaris_lwpstatus(abfd, note,
11387 304, 544, 544, 848);
11388 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11389 return elfcore_grok_solaris_lwpstatus(abfd, note,
11390 76, 344, 380, 420);
11391 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11392 return elfcore_grok_solaris_lwpstatus(abfd, note,
11393 224, 544, 528, 768);
11394 default:
11395 return true;
11398 case SOLARIS_NT_LWPSINFO:
11399 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11400 if (note->descsz == 128 || note->descsz == 152)
11401 elf_tdata (abfd)->core->lwpid =
11402 bfd_get_32 (abfd, note->descdata + 4);
11403 break;
11405 default:
11406 break;
11409 return true;
11412 /* For name starting with "CORE" this may be either a Solaris
11413 core file or a gdb-generated core file. Do Solaris-specific
11414 processing on selected note types first with
11415 elfcore_grok_solaris_note(), then process the note
11416 in elfcore_grok_note(). */
11418 static bool
11419 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11421 if (!elfcore_grok_solaris_note_impl (abfd, note))
11422 return false;
11424 return elfcore_grok_note (abfd, note);
11427 static bool
11428 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11430 if (note->type == NT_OPENBSD_PROCINFO)
11431 return elfcore_grok_openbsd_procinfo (abfd, note);
11433 if (note->type == NT_OPENBSD_REGS)
11434 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11436 if (note->type == NT_OPENBSD_FPREGS)
11437 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11439 if (note->type == NT_OPENBSD_XFPREGS)
11440 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11442 if (note->type == NT_OPENBSD_AUXV)
11443 return elfcore_make_auxv_note_section (abfd, note, 0);
11445 if (note->type == NT_OPENBSD_WCOOKIE)
11447 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11448 SEC_HAS_CONTENTS);
11450 if (sect == NULL)
11451 return false;
11452 sect->size = note->descsz;
11453 sect->filepos = note->descpos;
11454 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11456 return true;
11459 return true;
11462 static bool
11463 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11465 void *ddata = note->descdata;
11466 char buf[100];
11467 char *name;
11468 asection *sect;
11469 short sig;
11470 unsigned flags;
11472 if (note->descsz < 16)
11473 return false;
11475 /* nto_procfs_status 'pid' field is at offset 0. */
11476 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11478 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11479 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11481 /* nto_procfs_status 'flags' field is at offset 8. */
11482 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11484 /* nto_procfs_status 'what' field is at offset 14. */
11485 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11487 elf_tdata (abfd)->core->signal = sig;
11488 elf_tdata (abfd)->core->lwpid = *tid;
11491 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11492 do not come from signals so we make sure we set the current
11493 thread just in case. */
11494 if (flags & 0x00000080)
11495 elf_tdata (abfd)->core->lwpid = *tid;
11497 /* Make a ".qnx_core_status/%d" section. */
11498 sprintf (buf, ".qnx_core_status/%ld", *tid);
11500 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11501 if (name == NULL)
11502 return false;
11503 strcpy (name, buf);
11505 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11506 if (sect == NULL)
11507 return false;
11509 sect->size = note->descsz;
11510 sect->filepos = note->descpos;
11511 sect->alignment_power = 2;
11513 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11516 static bool
11517 elfcore_grok_nto_regs (bfd *abfd,
11518 Elf_Internal_Note *note,
11519 long tid,
11520 char *base)
11522 char buf[100];
11523 char *name;
11524 asection *sect;
11526 /* Make a "(base)/%d" section. */
11527 sprintf (buf, "%s/%ld", base, tid);
11529 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11530 if (name == NULL)
11531 return false;
11532 strcpy (name, buf);
11534 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11535 if (sect == NULL)
11536 return false;
11538 sect->size = note->descsz;
11539 sect->filepos = note->descpos;
11540 sect->alignment_power = 2;
11542 /* This is the current thread. */
11543 if (elf_tdata (abfd)->core->lwpid == tid)
11544 return elfcore_maybe_make_sect (abfd, base, sect);
11546 return true;
11549 static bool
11550 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11552 /* Every GREG section has a STATUS section before it. Store the
11553 tid from the previous call to pass down to the next gregs
11554 function. */
11555 static long tid = 1;
11557 switch (note->type)
11559 case QNT_CORE_INFO:
11560 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11561 case QNT_CORE_STATUS:
11562 return elfcore_grok_nto_status (abfd, note, &tid);
11563 case QNT_CORE_GREG:
11564 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11565 case QNT_CORE_FPREG:
11566 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11567 default:
11568 return true;
11572 static bool
11573 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11575 char *name;
11576 asection *sect;
11577 size_t len;
11579 /* Use note name as section name. */
11580 len = note->namesz;
11581 name = (char *) bfd_alloc (abfd, len);
11582 if (name == NULL)
11583 return false;
11584 memcpy (name, note->namedata, len);
11585 name[len - 1] = '\0';
11587 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11588 if (sect == NULL)
11589 return false;
11591 sect->size = note->descsz;
11592 sect->filepos = note->descpos;
11593 sect->alignment_power = 1;
11595 return true;
11598 /* Function: elfcore_write_note
11600 Inputs:
11601 buffer to hold note, and current size of buffer
11602 name of note
11603 type of note
11604 data for note
11605 size of data for note
11607 Writes note to end of buffer. ELF64 notes are written exactly as
11608 for ELF32, despite the current (as of 2006) ELF gabi specifying
11609 that they ought to have 8-byte namesz and descsz field, and have
11610 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11612 Return:
11613 Pointer to realloc'd buffer, *BUFSIZ updated. */
11615 char *
11616 elfcore_write_note (bfd *abfd,
11617 char *buf,
11618 int *bufsiz,
11619 const char *name,
11620 int type,
11621 const void *input,
11622 int size)
11624 Elf_External_Note *xnp;
11625 size_t namesz;
11626 size_t newspace;
11627 char *dest;
11629 namesz = 0;
11630 if (name != NULL)
11631 namesz = strlen (name) + 1;
11633 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11635 buf = (char *) realloc (buf, *bufsiz + newspace);
11636 if (buf == NULL)
11637 return buf;
11638 dest = buf + *bufsiz;
11639 *bufsiz += newspace;
11640 xnp = (Elf_External_Note *) dest;
11641 H_PUT_32 (abfd, namesz, xnp->namesz);
11642 H_PUT_32 (abfd, size, xnp->descsz);
11643 H_PUT_32 (abfd, type, xnp->type);
11644 dest = xnp->name;
11645 if (name != NULL)
11647 memcpy (dest, name, namesz);
11648 dest += namesz;
11649 while (namesz & 3)
11651 *dest++ = '\0';
11652 ++namesz;
11655 memcpy (dest, input, size);
11656 dest += size;
11657 while (size & 3)
11659 *dest++ = '\0';
11660 ++size;
11662 return buf;
11665 /* gcc-8 warns (*) on all the strncpy calls in this function about
11666 possible string truncation. The "truncation" is not a bug. We
11667 have an external representation of structs with fields that are not
11668 necessarily NULL terminated and corresponding internal
11669 representation fields that are one larger so that they can always
11670 be NULL terminated.
11671 gcc versions between 4.2 and 4.6 do not allow pragma control of
11672 diagnostics inside functions, giving a hard error if you try to use
11673 the finer control available with later versions.
11674 gcc prior to 4.2 warns about diagnostic push and pop.
11675 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11676 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11677 (*) Depending on your system header files! */
11678 #if GCC_VERSION >= 8000
11679 # pragma GCC diagnostic push
11680 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11681 #endif
11682 char *
11683 elfcore_write_prpsinfo (bfd *abfd,
11684 char *buf,
11685 int *bufsiz,
11686 const char *fname,
11687 const char *psargs)
11689 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11691 if (bed->elf_backend_write_core_note != NULL)
11693 char *ret;
11694 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11695 NT_PRPSINFO, fname, psargs);
11696 if (ret != NULL)
11697 return ret;
11700 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11701 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11702 if (bed->s->elfclass == ELFCLASS32)
11704 # if defined (HAVE_PSINFO32_T)
11705 psinfo32_t data;
11706 int note_type = NT_PSINFO;
11707 # else
11708 prpsinfo32_t data;
11709 int note_type = NT_PRPSINFO;
11710 # endif
11712 memset (&data, 0, sizeof (data));
11713 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11714 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11715 return elfcore_write_note (abfd, buf, bufsiz,
11716 "CORE", note_type, &data, sizeof (data));
11718 else
11719 # endif
11721 # if defined (HAVE_PSINFO_T)
11722 psinfo_t data;
11723 int note_type = NT_PSINFO;
11724 # else
11725 prpsinfo_t data;
11726 int note_type = NT_PRPSINFO;
11727 # endif
11729 memset (&data, 0, sizeof (data));
11730 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11731 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11732 return elfcore_write_note (abfd, buf, bufsiz,
11733 "CORE", note_type, &data, sizeof (data));
11735 #endif /* PSINFO_T or PRPSINFO_T */
11737 free (buf);
11738 return NULL;
11740 #if GCC_VERSION >= 8000
11741 # pragma GCC diagnostic pop
11742 #endif
11744 char *
11745 elfcore_write_linux_prpsinfo32
11746 (bfd *abfd, char *buf, int *bufsiz,
11747 const struct elf_internal_linux_prpsinfo *prpsinfo)
11749 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11751 struct elf_external_linux_prpsinfo32_ugid16 data;
11753 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11754 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11755 &data, sizeof (data));
11757 else
11759 struct elf_external_linux_prpsinfo32_ugid32 data;
11761 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11762 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11763 &data, sizeof (data));
11767 char *
11768 elfcore_write_linux_prpsinfo64
11769 (bfd *abfd, char *buf, int *bufsiz,
11770 const struct elf_internal_linux_prpsinfo *prpsinfo)
11772 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11774 struct elf_external_linux_prpsinfo64_ugid16 data;
11776 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11777 return elfcore_write_note (abfd, buf, bufsiz,
11778 "CORE", NT_PRPSINFO, &data, sizeof (data));
11780 else
11782 struct elf_external_linux_prpsinfo64_ugid32 data;
11784 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11785 return elfcore_write_note (abfd, buf, bufsiz,
11786 "CORE", NT_PRPSINFO, &data, sizeof (data));
11790 char *
11791 elfcore_write_prstatus (bfd *abfd,
11792 char *buf,
11793 int *bufsiz,
11794 long pid,
11795 int cursig,
11796 const void *gregs)
11798 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11800 if (bed->elf_backend_write_core_note != NULL)
11802 char *ret;
11803 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11804 NT_PRSTATUS,
11805 pid, cursig, gregs);
11806 if (ret != NULL)
11807 return ret;
11810 #if defined (HAVE_PRSTATUS_T)
11811 #if defined (HAVE_PRSTATUS32_T)
11812 if (bed->s->elfclass == ELFCLASS32)
11814 prstatus32_t prstat;
11816 memset (&prstat, 0, sizeof (prstat));
11817 prstat.pr_pid = pid;
11818 prstat.pr_cursig = cursig;
11819 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11820 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11821 NT_PRSTATUS, &prstat, sizeof (prstat));
11823 else
11824 #endif
11826 prstatus_t prstat;
11828 memset (&prstat, 0, sizeof (prstat));
11829 prstat.pr_pid = pid;
11830 prstat.pr_cursig = cursig;
11831 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11832 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11833 NT_PRSTATUS, &prstat, sizeof (prstat));
11835 #endif /* HAVE_PRSTATUS_T */
11837 free (buf);
11838 return NULL;
11841 #if defined (HAVE_LWPSTATUS_T)
11842 char *
11843 elfcore_write_lwpstatus (bfd *abfd,
11844 char *buf,
11845 int *bufsiz,
11846 long pid,
11847 int cursig,
11848 const void *gregs)
11850 lwpstatus_t lwpstat;
11851 const char *note_name = "CORE";
11853 memset (&lwpstat, 0, sizeof (lwpstat));
11854 lwpstat.pr_lwpid = pid >> 16;
11855 lwpstat.pr_cursig = cursig;
11856 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11857 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11858 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11859 #if !defined(gregs)
11860 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11861 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11862 #else
11863 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11864 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11865 #endif
11866 #endif
11867 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11868 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11870 #endif /* HAVE_LWPSTATUS_T */
11872 #if defined (HAVE_PSTATUS_T)
11873 char *
11874 elfcore_write_pstatus (bfd *abfd,
11875 char *buf,
11876 int *bufsiz,
11877 long pid,
11878 int cursig ATTRIBUTE_UNUSED,
11879 const void *gregs ATTRIBUTE_UNUSED)
11881 const char *note_name = "CORE";
11882 #if defined (HAVE_PSTATUS32_T)
11883 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11885 if (bed->s->elfclass == ELFCLASS32)
11887 pstatus32_t pstat;
11889 memset (&pstat, 0, sizeof (pstat));
11890 pstat.pr_pid = pid & 0xffff;
11891 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11892 NT_PSTATUS, &pstat, sizeof (pstat));
11893 return buf;
11895 else
11896 #endif
11898 pstatus_t pstat;
11900 memset (&pstat, 0, sizeof (pstat));
11901 pstat.pr_pid = pid & 0xffff;
11902 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11903 NT_PSTATUS, &pstat, sizeof (pstat));
11904 return buf;
11907 #endif /* HAVE_PSTATUS_T */
11909 char *
11910 elfcore_write_prfpreg (bfd *abfd,
11911 char *buf,
11912 int *bufsiz,
11913 const void *fpregs,
11914 int size)
11916 const char *note_name = "CORE";
11917 return elfcore_write_note (abfd, buf, bufsiz,
11918 note_name, NT_FPREGSET, fpregs, size);
11921 char *
11922 elfcore_write_prxfpreg (bfd *abfd,
11923 char *buf,
11924 int *bufsiz,
11925 const void *xfpregs,
11926 int size)
11928 char *note_name = "LINUX";
11929 return elfcore_write_note (abfd, buf, bufsiz,
11930 note_name, NT_PRXFPREG, xfpregs, size);
11933 char *
11934 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11935 const void *xfpregs, int size)
11937 char *note_name;
11938 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11939 note_name = "FreeBSD";
11940 else
11941 note_name = "LINUX";
11942 return elfcore_write_note (abfd, buf, bufsiz,
11943 note_name, NT_X86_XSTATE, xfpregs, size);
11946 char *
11947 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
11948 const void *regs, int size)
11950 char *note_name = "FreeBSD";
11951 return elfcore_write_note (abfd, buf, bufsiz,
11952 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
11955 char *
11956 elfcore_write_ppc_vmx (bfd *abfd,
11957 char *buf,
11958 int *bufsiz,
11959 const void *ppc_vmx,
11960 int size)
11962 char *note_name = "LINUX";
11963 return elfcore_write_note (abfd, buf, bufsiz,
11964 note_name, NT_PPC_VMX, ppc_vmx, size);
11967 char *
11968 elfcore_write_ppc_vsx (bfd *abfd,
11969 char *buf,
11970 int *bufsiz,
11971 const void *ppc_vsx,
11972 int size)
11974 char *note_name = "LINUX";
11975 return elfcore_write_note (abfd, buf, bufsiz,
11976 note_name, NT_PPC_VSX, ppc_vsx, size);
11979 char *
11980 elfcore_write_ppc_tar (bfd *abfd,
11981 char *buf,
11982 int *bufsiz,
11983 const void *ppc_tar,
11984 int size)
11986 char *note_name = "LINUX";
11987 return elfcore_write_note (abfd, buf, bufsiz,
11988 note_name, NT_PPC_TAR, ppc_tar, size);
11991 char *
11992 elfcore_write_ppc_ppr (bfd *abfd,
11993 char *buf,
11994 int *bufsiz,
11995 const void *ppc_ppr,
11996 int size)
11998 char *note_name = "LINUX";
11999 return elfcore_write_note (abfd, buf, bufsiz,
12000 note_name, NT_PPC_PPR, ppc_ppr, size);
12003 char *
12004 elfcore_write_ppc_dscr (bfd *abfd,
12005 char *buf,
12006 int *bufsiz,
12007 const void *ppc_dscr,
12008 int size)
12010 char *note_name = "LINUX";
12011 return elfcore_write_note (abfd, buf, bufsiz,
12012 note_name, NT_PPC_DSCR, ppc_dscr, size);
12015 char *
12016 elfcore_write_ppc_ebb (bfd *abfd,
12017 char *buf,
12018 int *bufsiz,
12019 const void *ppc_ebb,
12020 int size)
12022 char *note_name = "LINUX";
12023 return elfcore_write_note (abfd, buf, bufsiz,
12024 note_name, NT_PPC_EBB, ppc_ebb, size);
12027 char *
12028 elfcore_write_ppc_pmu (bfd *abfd,
12029 char *buf,
12030 int *bufsiz,
12031 const void *ppc_pmu,
12032 int size)
12034 char *note_name = "LINUX";
12035 return elfcore_write_note (abfd, buf, bufsiz,
12036 note_name, NT_PPC_PMU, ppc_pmu, size);
12039 char *
12040 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12041 char *buf,
12042 int *bufsiz,
12043 const void *ppc_tm_cgpr,
12044 int size)
12046 char *note_name = "LINUX";
12047 return elfcore_write_note (abfd, buf, bufsiz,
12048 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12051 char *
12052 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12053 char *buf,
12054 int *bufsiz,
12055 const void *ppc_tm_cfpr,
12056 int size)
12058 char *note_name = "LINUX";
12059 return elfcore_write_note (abfd, buf, bufsiz,
12060 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12063 char *
12064 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12065 char *buf,
12066 int *bufsiz,
12067 const void *ppc_tm_cvmx,
12068 int size)
12070 char *note_name = "LINUX";
12071 return elfcore_write_note (abfd, buf, bufsiz,
12072 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12075 char *
12076 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12077 char *buf,
12078 int *bufsiz,
12079 const void *ppc_tm_cvsx,
12080 int size)
12082 char *note_name = "LINUX";
12083 return elfcore_write_note (abfd, buf, bufsiz,
12084 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12087 char *
12088 elfcore_write_ppc_tm_spr (bfd *abfd,
12089 char *buf,
12090 int *bufsiz,
12091 const void *ppc_tm_spr,
12092 int size)
12094 char *note_name = "LINUX";
12095 return elfcore_write_note (abfd, buf, bufsiz,
12096 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12099 char *
12100 elfcore_write_ppc_tm_ctar (bfd *abfd,
12101 char *buf,
12102 int *bufsiz,
12103 const void *ppc_tm_ctar,
12104 int size)
12106 char *note_name = "LINUX";
12107 return elfcore_write_note (abfd, buf, bufsiz,
12108 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12111 char *
12112 elfcore_write_ppc_tm_cppr (bfd *abfd,
12113 char *buf,
12114 int *bufsiz,
12115 const void *ppc_tm_cppr,
12116 int size)
12118 char *note_name = "LINUX";
12119 return elfcore_write_note (abfd, buf, bufsiz,
12120 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12123 char *
12124 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12125 char *buf,
12126 int *bufsiz,
12127 const void *ppc_tm_cdscr,
12128 int size)
12130 char *note_name = "LINUX";
12131 return elfcore_write_note (abfd, buf, bufsiz,
12132 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12135 static char *
12136 elfcore_write_s390_high_gprs (bfd *abfd,
12137 char *buf,
12138 int *bufsiz,
12139 const void *s390_high_gprs,
12140 int size)
12142 char *note_name = "LINUX";
12143 return elfcore_write_note (abfd, buf, bufsiz,
12144 note_name, NT_S390_HIGH_GPRS,
12145 s390_high_gprs, size);
12148 char *
12149 elfcore_write_s390_timer (bfd *abfd,
12150 char *buf,
12151 int *bufsiz,
12152 const void *s390_timer,
12153 int size)
12155 char *note_name = "LINUX";
12156 return elfcore_write_note (abfd, buf, bufsiz,
12157 note_name, NT_S390_TIMER, s390_timer, size);
12160 char *
12161 elfcore_write_s390_todcmp (bfd *abfd,
12162 char *buf,
12163 int *bufsiz,
12164 const void *s390_todcmp,
12165 int size)
12167 char *note_name = "LINUX";
12168 return elfcore_write_note (abfd, buf, bufsiz,
12169 note_name, NT_S390_TODCMP, s390_todcmp, size);
12172 char *
12173 elfcore_write_s390_todpreg (bfd *abfd,
12174 char *buf,
12175 int *bufsiz,
12176 const void *s390_todpreg,
12177 int size)
12179 char *note_name = "LINUX";
12180 return elfcore_write_note (abfd, buf, bufsiz,
12181 note_name, NT_S390_TODPREG, s390_todpreg, size);
12184 char *
12185 elfcore_write_s390_ctrs (bfd *abfd,
12186 char *buf,
12187 int *bufsiz,
12188 const void *s390_ctrs,
12189 int size)
12191 char *note_name = "LINUX";
12192 return elfcore_write_note (abfd, buf, bufsiz,
12193 note_name, NT_S390_CTRS, s390_ctrs, size);
12196 char *
12197 elfcore_write_s390_prefix (bfd *abfd,
12198 char *buf,
12199 int *bufsiz,
12200 const void *s390_prefix,
12201 int size)
12203 char *note_name = "LINUX";
12204 return elfcore_write_note (abfd, buf, bufsiz,
12205 note_name, NT_S390_PREFIX, s390_prefix, size);
12208 char *
12209 elfcore_write_s390_last_break (bfd *abfd,
12210 char *buf,
12211 int *bufsiz,
12212 const void *s390_last_break,
12213 int size)
12215 char *note_name = "LINUX";
12216 return elfcore_write_note (abfd, buf, bufsiz,
12217 note_name, NT_S390_LAST_BREAK,
12218 s390_last_break, size);
12221 char *
12222 elfcore_write_s390_system_call (bfd *abfd,
12223 char *buf,
12224 int *bufsiz,
12225 const void *s390_system_call,
12226 int size)
12228 char *note_name = "LINUX";
12229 return elfcore_write_note (abfd, buf, bufsiz,
12230 note_name, NT_S390_SYSTEM_CALL,
12231 s390_system_call, size);
12234 char *
12235 elfcore_write_s390_tdb (bfd *abfd,
12236 char *buf,
12237 int *bufsiz,
12238 const void *s390_tdb,
12239 int size)
12241 char *note_name = "LINUX";
12242 return elfcore_write_note (abfd, buf, bufsiz,
12243 note_name, NT_S390_TDB, s390_tdb, size);
12246 char *
12247 elfcore_write_s390_vxrs_low (bfd *abfd,
12248 char *buf,
12249 int *bufsiz,
12250 const void *s390_vxrs_low,
12251 int size)
12253 char *note_name = "LINUX";
12254 return elfcore_write_note (abfd, buf, bufsiz,
12255 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12258 char *
12259 elfcore_write_s390_vxrs_high (bfd *abfd,
12260 char *buf,
12261 int *bufsiz,
12262 const void *s390_vxrs_high,
12263 int size)
12265 char *note_name = "LINUX";
12266 return elfcore_write_note (abfd, buf, bufsiz,
12267 note_name, NT_S390_VXRS_HIGH,
12268 s390_vxrs_high, size);
12271 char *
12272 elfcore_write_s390_gs_cb (bfd *abfd,
12273 char *buf,
12274 int *bufsiz,
12275 const void *s390_gs_cb,
12276 int size)
12278 char *note_name = "LINUX";
12279 return elfcore_write_note (abfd, buf, bufsiz,
12280 note_name, NT_S390_GS_CB,
12281 s390_gs_cb, size);
12284 char *
12285 elfcore_write_s390_gs_bc (bfd *abfd,
12286 char *buf,
12287 int *bufsiz,
12288 const void *s390_gs_bc,
12289 int size)
12291 char *note_name = "LINUX";
12292 return elfcore_write_note (abfd, buf, bufsiz,
12293 note_name, NT_S390_GS_BC,
12294 s390_gs_bc, size);
12297 char *
12298 elfcore_write_arm_vfp (bfd *abfd,
12299 char *buf,
12300 int *bufsiz,
12301 const void *arm_vfp,
12302 int size)
12304 char *note_name = "LINUX";
12305 return elfcore_write_note (abfd, buf, bufsiz,
12306 note_name, NT_ARM_VFP, arm_vfp, size);
12309 char *
12310 elfcore_write_aarch_tls (bfd *abfd,
12311 char *buf,
12312 int *bufsiz,
12313 const void *aarch_tls,
12314 int size)
12316 char *note_name = "LINUX";
12317 return elfcore_write_note (abfd, buf, bufsiz,
12318 note_name, NT_ARM_TLS, aarch_tls, size);
12321 char *
12322 elfcore_write_aarch_hw_break (bfd *abfd,
12323 char *buf,
12324 int *bufsiz,
12325 const void *aarch_hw_break,
12326 int size)
12328 char *note_name = "LINUX";
12329 return elfcore_write_note (abfd, buf, bufsiz,
12330 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12333 char *
12334 elfcore_write_aarch_hw_watch (bfd *abfd,
12335 char *buf,
12336 int *bufsiz,
12337 const void *aarch_hw_watch,
12338 int size)
12340 char *note_name = "LINUX";
12341 return elfcore_write_note (abfd, buf, bufsiz,
12342 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12345 char *
12346 elfcore_write_aarch_sve (bfd *abfd,
12347 char *buf,
12348 int *bufsiz,
12349 const void *aarch_sve,
12350 int size)
12352 char *note_name = "LINUX";
12353 return elfcore_write_note (abfd, buf, bufsiz,
12354 note_name, NT_ARM_SVE, aarch_sve, size);
12357 char *
12358 elfcore_write_aarch_pauth (bfd *abfd,
12359 char *buf,
12360 int *bufsiz,
12361 const void *aarch_pauth,
12362 int size)
12364 char *note_name = "LINUX";
12365 return elfcore_write_note (abfd, buf, bufsiz,
12366 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12369 char *
12370 elfcore_write_aarch_mte (bfd *abfd,
12371 char *buf,
12372 int *bufsiz,
12373 const void *aarch_mte,
12374 int size)
12376 char *note_name = "LINUX";
12377 return elfcore_write_note (abfd, buf, bufsiz,
12378 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12379 aarch_mte,
12380 size);
12383 char *
12384 elfcore_write_arc_v2 (bfd *abfd,
12385 char *buf,
12386 int *bufsiz,
12387 const void *arc_v2,
12388 int size)
12390 char *note_name = "LINUX";
12391 return elfcore_write_note (abfd, buf, bufsiz,
12392 note_name, NT_ARC_V2, arc_v2, size);
12395 char *
12396 elfcore_write_loongarch_cpucfg (bfd *abfd,
12397 char *buf,
12398 int *bufsiz,
12399 const void *loongarch_cpucfg,
12400 int size)
12402 char *note_name = "LINUX";
12403 return elfcore_write_note (abfd, buf, bufsiz,
12404 note_name, NT_LARCH_CPUCFG,
12405 loongarch_cpucfg, size);
12408 char *
12409 elfcore_write_loongarch_lbt (bfd *abfd,
12410 char *buf,
12411 int *bufsiz,
12412 const void *loongarch_lbt,
12413 int size)
12415 char *note_name = "LINUX";
12416 return elfcore_write_note (abfd, buf, bufsiz,
12417 note_name, NT_LARCH_LBT, loongarch_lbt, size);
12420 char *
12421 elfcore_write_loongarch_lsx (bfd *abfd,
12422 char *buf,
12423 int *bufsiz,
12424 const void *loongarch_lsx,
12425 int size)
12427 char *note_name = "LINUX";
12428 return elfcore_write_note (abfd, buf, bufsiz,
12429 note_name, NT_LARCH_LSX, loongarch_lsx, size);
12432 char *
12433 elfcore_write_loongarch_lasx (bfd *abfd,
12434 char *buf,
12435 int *bufsiz,
12436 const void *loongarch_lasx,
12437 int size)
12439 char *note_name = "LINUX";
12440 return elfcore_write_note (abfd, buf, bufsiz,
12441 note_name, NT_LARCH_LASX, loongarch_lasx, size);
12444 /* Write the buffer of csr values in CSRS (length SIZE) into the note
12445 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12446 written into. Return a pointer to the new start of the note buffer, to
12447 replace BUF which may no longer be valid. */
12449 char *
12450 elfcore_write_riscv_csr (bfd *abfd,
12451 char *buf,
12452 int *bufsiz,
12453 const void *csrs,
12454 int size)
12456 const char *note_name = "GDB";
12457 return elfcore_write_note (abfd, buf, bufsiz,
12458 note_name, NT_RISCV_CSR, csrs, size);
12461 /* Write the target description (a string) pointed to by TDESC, length
12462 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12463 note is being written into. Return a pointer to the new start of the
12464 note buffer, to replace BUF which may no longer be valid. */
12466 char *
12467 elfcore_write_gdb_tdesc (bfd *abfd,
12468 char *buf,
12469 int *bufsiz,
12470 const void *tdesc,
12471 int size)
12473 const char *note_name = "GDB";
12474 return elfcore_write_note (abfd, buf, bufsiz,
12475 note_name, NT_GDB_TDESC, tdesc, size);
12478 char *
12479 elfcore_write_register_note (bfd *abfd,
12480 char *buf,
12481 int *bufsiz,
12482 const char *section,
12483 const void *data,
12484 int size)
12486 if (strcmp (section, ".reg2") == 0)
12487 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12488 if (strcmp (section, ".reg-xfp") == 0)
12489 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12490 if (strcmp (section, ".reg-xstate") == 0)
12491 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12492 if (strcmp (section, ".reg-x86-segbases") == 0)
12493 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
12494 if (strcmp (section, ".reg-ppc-vmx") == 0)
12495 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12496 if (strcmp (section, ".reg-ppc-vsx") == 0)
12497 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12498 if (strcmp (section, ".reg-ppc-tar") == 0)
12499 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12500 if (strcmp (section, ".reg-ppc-ppr") == 0)
12501 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12502 if (strcmp (section, ".reg-ppc-dscr") == 0)
12503 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12504 if (strcmp (section, ".reg-ppc-ebb") == 0)
12505 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12506 if (strcmp (section, ".reg-ppc-pmu") == 0)
12507 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12508 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12509 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12510 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12511 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12512 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12513 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12514 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12515 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12516 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12517 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12518 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12519 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12520 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12521 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12522 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12523 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12524 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12525 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12526 if (strcmp (section, ".reg-s390-timer") == 0)
12527 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12528 if (strcmp (section, ".reg-s390-todcmp") == 0)
12529 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12530 if (strcmp (section, ".reg-s390-todpreg") == 0)
12531 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12532 if (strcmp (section, ".reg-s390-ctrs") == 0)
12533 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12534 if (strcmp (section, ".reg-s390-prefix") == 0)
12535 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12536 if (strcmp (section, ".reg-s390-last-break") == 0)
12537 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12538 if (strcmp (section, ".reg-s390-system-call") == 0)
12539 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12540 if (strcmp (section, ".reg-s390-tdb") == 0)
12541 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12542 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12543 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12544 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12545 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12546 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12547 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12548 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12549 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12550 if (strcmp (section, ".reg-arm-vfp") == 0)
12551 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12552 if (strcmp (section, ".reg-aarch-tls") == 0)
12553 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12554 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12555 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12556 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12557 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12558 if (strcmp (section, ".reg-aarch-sve") == 0)
12559 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12560 if (strcmp (section, ".reg-aarch-pauth") == 0)
12561 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12562 if (strcmp (section, ".reg-aarch-mte") == 0)
12563 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12564 if (strcmp (section, ".reg-arc-v2") == 0)
12565 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12566 if (strcmp (section, ".gdb-tdesc") == 0)
12567 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12568 if (strcmp (section, ".reg-riscv-csr") == 0)
12569 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12570 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12571 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12572 if (strcmp (section, ".reg-loongarch-lbt") == 0)
12573 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12574 if (strcmp (section, ".reg-loongarch-lsx") == 0)
12575 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12576 if (strcmp (section, ".reg-loongarch-lasx") == 0)
12577 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
12578 return NULL;
12581 char *
12582 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12583 const void *buf, int bufsiz)
12585 return elfcore_write_note (obfd, note_data, note_size,
12586 "CORE", NT_FILE, buf, bufsiz);
12589 static bool
12590 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12591 size_t align)
12593 char *p;
12595 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12596 gABI specifies that PT_NOTE alignment should be aligned to 4
12597 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12598 align is less than 4, we use 4 byte alignment. */
12599 if (align < 4)
12600 align = 4;
12601 if (align != 4 && align != 8)
12602 return false;
12604 p = buf;
12605 while (p < buf + size)
12607 Elf_External_Note *xnp = (Elf_External_Note *) p;
12608 Elf_Internal_Note in;
12610 if (offsetof (Elf_External_Note, name) > buf - p + size)
12611 return false;
12613 in.type = H_GET_32 (abfd, xnp->type);
12615 in.namesz = H_GET_32 (abfd, xnp->namesz);
12616 in.namedata = xnp->name;
12617 if (in.namesz > buf - in.namedata + size)
12618 return false;
12620 in.descsz = H_GET_32 (abfd, xnp->descsz);
12621 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12622 in.descpos = offset + (in.descdata - buf);
12623 if (in.descsz != 0
12624 && (in.descdata >= buf + size
12625 || in.descsz > buf - in.descdata + size))
12626 return false;
12628 switch (bfd_get_format (abfd))
12630 default:
12631 return true;
12633 case bfd_core:
12635 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12636 struct
12638 const char * string;
12639 size_t len;
12640 bool (*func) (bfd *, Elf_Internal_Note *);
12642 grokers[] =
12644 GROKER_ELEMENT ("", elfcore_grok_note),
12645 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12646 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12647 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
12648 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12649 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12650 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12651 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
12653 #undef GROKER_ELEMENT
12654 int i;
12656 for (i = ARRAY_SIZE (grokers); i--;)
12658 if (in.namesz >= grokers[i].len
12659 && strncmp (in.namedata, grokers[i].string,
12660 grokers[i].len) == 0)
12662 if (! grokers[i].func (abfd, & in))
12663 return false;
12664 break;
12667 break;
12670 case bfd_object:
12671 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12673 if (! elfobj_grok_gnu_note (abfd, &in))
12674 return false;
12676 else if (in.namesz == sizeof "stapsdt"
12677 && strcmp (in.namedata, "stapsdt") == 0)
12679 if (! elfobj_grok_stapsdt_note (abfd, &in))
12680 return false;
12682 break;
12685 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12688 return true;
12691 bool
12692 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12693 size_t align)
12695 char *buf;
12697 if (size == 0 || (size + 1) == 0)
12698 return true;
12700 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12701 return false;
12703 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12704 if (buf == NULL)
12705 return false;
12707 /* PR 17512: file: ec08f814
12708 0-termintate the buffer so that string searches will not overflow. */
12709 buf[size] = 0;
12711 if (!elf_parse_notes (abfd, buf, size, offset, align))
12713 free (buf);
12714 return false;
12717 free (buf);
12718 return true;
12721 /* Providing external access to the ELF program header table. */
12723 /* Return an upper bound on the number of bytes required to store a
12724 copy of ABFD's program header table entries. Return -1 if an error
12725 occurs; bfd_get_error will return an appropriate code. */
12727 long
12728 bfd_get_elf_phdr_upper_bound (bfd *abfd)
12730 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12732 bfd_set_error (bfd_error_wrong_format);
12733 return -1;
12736 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12739 /* Copy ABFD's program header table entries to *PHDRS. The entries
12740 will be stored as an array of Elf_Internal_Phdr structures, as
12741 defined in include/elf/internal.h. To find out how large the
12742 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12744 Return the number of program header table entries read, or -1 if an
12745 error occurs; bfd_get_error will return an appropriate code. */
12748 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12750 int num_phdrs;
12752 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12754 bfd_set_error (bfd_error_wrong_format);
12755 return -1;
12758 num_phdrs = elf_elfheader (abfd)->e_phnum;
12759 if (num_phdrs != 0)
12760 memcpy (phdrs, elf_tdata (abfd)->phdr,
12761 num_phdrs * sizeof (Elf_Internal_Phdr));
12763 return num_phdrs;
12766 enum elf_reloc_type_class
12767 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12768 const asection *rel_sec ATTRIBUTE_UNUSED,
12769 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12771 return reloc_class_normal;
12774 /* For RELA architectures, return the relocation value for a
12775 relocation against a local symbol. */
12777 bfd_vma
12778 _bfd_elf_rela_local_sym (bfd *abfd,
12779 Elf_Internal_Sym *sym,
12780 asection **psec,
12781 Elf_Internal_Rela *rel)
12783 asection *sec = *psec;
12784 bfd_vma relocation;
12786 relocation = (sec->output_section->vma
12787 + sec->output_offset
12788 + sym->st_value);
12789 if ((sec->flags & SEC_MERGE)
12790 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12791 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12793 rel->r_addend =
12794 _bfd_merged_section_offset (abfd, psec,
12795 elf_section_data (sec)->sec_info,
12796 sym->st_value + rel->r_addend);
12797 if (sec != *psec)
12799 /* If we have changed the section, and our original section is
12800 marked with SEC_EXCLUDE, it means that the original
12801 SEC_MERGE section has been completely subsumed in some
12802 other SEC_MERGE section. In this case, we need to leave
12803 some info around for --emit-relocs. */
12804 if ((sec->flags & SEC_EXCLUDE) != 0)
12805 sec->kept_section = *psec;
12806 sec = *psec;
12808 rel->r_addend -= relocation;
12809 rel->r_addend += sec->output_section->vma + sec->output_offset;
12811 return relocation;
12814 bfd_vma
12815 _bfd_elf_rel_local_sym (bfd *abfd,
12816 Elf_Internal_Sym *sym,
12817 asection **psec,
12818 bfd_vma addend)
12820 asection *sec = *psec;
12822 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12823 return sym->st_value + addend;
12825 return _bfd_merged_section_offset (abfd, psec,
12826 elf_section_data (sec)->sec_info,
12827 sym->st_value + addend);
12830 /* Adjust an address within a section. Given OFFSET within SEC, return
12831 the new offset within the section, based upon changes made to the
12832 section. Returns -1 if the offset is now invalid.
12833 The offset (in abnd out) is in target sized bytes, however big a
12834 byte may be. */
12836 bfd_vma
12837 _bfd_elf_section_offset (bfd *abfd,
12838 struct bfd_link_info *info,
12839 asection *sec,
12840 bfd_vma offset)
12842 switch (sec->sec_info_type)
12844 case SEC_INFO_TYPE_STABS:
12845 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12846 offset);
12847 case SEC_INFO_TYPE_EH_FRAME:
12848 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12850 default:
12851 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12853 /* Reverse the offset. */
12854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12855 bfd_size_type address_size = bed->s->arch_size / 8;
12857 /* address_size and sec->size are in octets. Convert
12858 to bytes before subtracting the original offset. */
12859 offset = ((sec->size - address_size)
12860 / bfd_octets_per_byte (abfd, sec) - offset);
12862 return offset;
12866 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12867 reconstruct an ELF file by reading the segments out of remote memory
12868 based on the ELF file header at EHDR_VMA and the ELF program headers it
12869 points to. If not null, *LOADBASEP is filled in with the difference
12870 between the VMAs from which the segments were read, and the VMAs the
12871 file headers (and hence BFD's idea of each section's VMA) put them at.
12873 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12874 remote memory at target address VMA into the local buffer at MYADDR; it
12875 should return zero on success or an `errno' code on failure. TEMPL must
12876 be a BFD for an ELF target with the word size and byte order found in
12877 the remote memory. */
12879 bfd *
12880 bfd_elf_bfd_from_remote_memory
12881 (bfd *templ,
12882 bfd_vma ehdr_vma,
12883 bfd_size_type size,
12884 bfd_vma *loadbasep,
12885 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12887 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12888 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12891 long
12892 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12893 long symcount ATTRIBUTE_UNUSED,
12894 asymbol **syms ATTRIBUTE_UNUSED,
12895 long dynsymcount,
12896 asymbol **dynsyms,
12897 asymbol **ret)
12899 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12900 asection *relplt;
12901 asymbol *s;
12902 const char *relplt_name;
12903 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
12904 arelent *p;
12905 long count, i, n;
12906 size_t size;
12907 Elf_Internal_Shdr *hdr;
12908 char *names;
12909 asection *plt;
12911 *ret = NULL;
12913 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12914 return 0;
12916 if (dynsymcount <= 0)
12917 return 0;
12919 if (!bed->plt_sym_val)
12920 return 0;
12922 relplt_name = bed->relplt_name;
12923 if (relplt_name == NULL)
12924 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12925 relplt = bfd_get_section_by_name (abfd, relplt_name);
12926 if (relplt == NULL)
12927 return 0;
12929 hdr = &elf_section_data (relplt)->this_hdr;
12930 if (hdr->sh_link != elf_dynsymtab (abfd)
12931 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12932 return 0;
12934 plt = bfd_get_section_by_name (abfd, ".plt");
12935 if (plt == NULL)
12936 return 0;
12938 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12939 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
12940 return -1;
12942 count = NUM_SHDR_ENTRIES (hdr);
12943 size = count * sizeof (asymbol);
12944 p = relplt->relocation;
12945 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12947 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12948 if (p->addend != 0)
12950 #ifdef BFD64
12951 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12952 #else
12953 size += sizeof ("+0x") - 1 + 8;
12954 #endif
12958 s = *ret = (asymbol *) bfd_malloc (size);
12959 if (s == NULL)
12960 return -1;
12962 names = (char *) (s + count);
12963 p = relplt->relocation;
12964 n = 0;
12965 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12967 size_t len;
12968 bfd_vma addr;
12970 addr = bed->plt_sym_val (i, plt, p);
12971 if (addr == (bfd_vma) -1)
12972 continue;
12974 *s = **p->sym_ptr_ptr;
12975 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12976 we are defining a symbol, ensure one of them is set. */
12977 if ((s->flags & BSF_LOCAL) == 0)
12978 s->flags |= BSF_GLOBAL;
12979 s->flags |= BSF_SYNTHETIC;
12980 s->section = plt;
12981 s->value = addr - plt->vma;
12982 s->name = names;
12983 s->udata.p = NULL;
12984 len = strlen ((*p->sym_ptr_ptr)->name);
12985 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12986 names += len;
12987 if (p->addend != 0)
12989 char buf[30], *a;
12991 memcpy (names, "+0x", sizeof ("+0x") - 1);
12992 names += sizeof ("+0x") - 1;
12993 bfd_sprintf_vma (abfd, buf, p->addend);
12994 for (a = buf; *a == '0'; ++a)
12996 len = strlen (a);
12997 memcpy (names, a, len);
12998 names += len;
13000 memcpy (names, "@plt", sizeof ("@plt"));
13001 names += sizeof ("@plt");
13002 ++s, ++n;
13005 return n;
13008 /* It is only used by x86-64 so far.
13009 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13010 but current usage would allow all of _bfd_std_section to be zero. */
13011 static const asymbol lcomm_sym
13012 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13013 asection _bfd_elf_large_com_section
13014 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13015 "LARGE_COMMON", 0, SEC_IS_COMMON);
13017 bool
13018 _bfd_elf_final_write_processing (bfd *abfd)
13020 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13022 i_ehdrp = elf_elfheader (abfd);
13024 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13025 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13027 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13028 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13029 or STB_GNU_UNIQUE binding. */
13030 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13032 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13033 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13034 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13035 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13037 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13038 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13039 "and FreeBSD targets"));
13040 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13041 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13042 "only by GNU and FreeBSD targets"));
13043 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13044 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13045 "only by GNU and FreeBSD targets"));
13046 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13047 _bfd_error_handler (_("GNU_RETAIN section is supported "
13048 "only by GNU and FreeBSD targets"));
13049 bfd_set_error (bfd_error_sorry);
13050 return false;
13053 return true;
13057 /* Return TRUE for ELF symbol types that represent functions.
13058 This is the default version of this function, which is sufficient for
13059 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13061 bool
13062 _bfd_elf_is_function_type (unsigned int type)
13064 return (type == STT_FUNC
13065 || type == STT_GNU_IFUNC);
13068 /* If the ELF symbol SYM might be a function in SEC, return the
13069 function size and set *CODE_OFF to the function's entry point,
13070 otherwise return zero. */
13072 bfd_size_type
13073 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13074 bfd_vma *code_off)
13076 bfd_size_type size;
13077 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13079 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13080 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13081 || sym->section != sec)
13082 return 0;
13084 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13086 /* In theory we should check that the symbol's type satisfies
13087 _bfd_elf_is_function_type(), but there are some function-like
13088 symbols which would fail this test. (eg _start). Instead
13089 we check for hidden, local, notype symbols with zero size.
13090 This type of symbol is generated by the annobin plugin for gcc
13091 and clang, and should not be considered to be a function symbol. */
13092 if (size == 0
13093 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13094 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13095 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13096 return 0;
13098 *code_off = sym->value;
13099 /* Do not return 0 for the function's size. */
13100 return size ? size : 1;
13103 /* Set to non-zero to enable some debug messages. */
13104 #define DEBUG_SECONDARY_RELOCS 0
13106 /* An internal-to-the-bfd-library only section type
13107 used to indicate a cached secondary reloc section. */
13108 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13110 /* Create a BFD section to hold a secondary reloc section. */
13112 bool
13113 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13114 Elf_Internal_Shdr *hdr,
13115 const char * name,
13116 unsigned int shindex)
13118 /* We only support RELA secondary relocs. */
13119 if (hdr->sh_type != SHT_RELA)
13120 return false;
13122 #if DEBUG_SECONDARY_RELOCS
13123 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13124 #endif
13125 hdr->sh_type = SHT_SECONDARY_RELOC;
13126 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13129 /* Read in any secondary relocs associated with SEC. */
13131 bool
13132 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13133 asection * sec,
13134 asymbol ** symbols,
13135 bool dynamic)
13137 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13138 asection * relsec;
13139 bool result = true;
13140 bfd_vma (*r_sym) (bfd_vma);
13141 ufile_ptr filesize;
13143 #if BFD_DEFAULT_TARGET_SIZE > 32
13144 if (bfd_arch_bits_per_address (abfd) != 32)
13145 r_sym = elf64_r_sym;
13146 else
13147 #endif
13148 r_sym = elf32_r_sym;
13150 if (!elf_section_data (sec)->has_secondary_relocs)
13151 return true;
13153 /* Discover if there are any secondary reloc sections
13154 associated with SEC. */
13155 filesize = bfd_get_file_size (abfd);
13156 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13158 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13160 if (hdr->sh_type == SHT_SECONDARY_RELOC
13161 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13162 && (hdr->sh_entsize == ebd->s->sizeof_rel
13163 || hdr->sh_entsize == ebd->s->sizeof_rela))
13165 bfd_byte * native_relocs;
13166 bfd_byte * native_reloc;
13167 arelent * internal_relocs;
13168 arelent * internal_reloc;
13169 size_t i;
13170 unsigned int entsize;
13171 unsigned int symcount;
13172 bfd_size_type reloc_count;
13173 size_t amt;
13175 if (ebd->elf_info_to_howto == NULL)
13176 return false;
13178 #if DEBUG_SECONDARY_RELOCS
13179 fprintf (stderr, "read secondary relocs for %s from %s\n",
13180 sec->name, relsec->name);
13181 #endif
13182 entsize = hdr->sh_entsize;
13184 if (filesize != 0
13185 && ((ufile_ptr) hdr->sh_offset > filesize
13186 || hdr->sh_size > filesize - hdr->sh_offset))
13188 bfd_set_error (bfd_error_file_truncated);
13189 result = false;
13190 continue;
13193 native_relocs = bfd_malloc (hdr->sh_size);
13194 if (native_relocs == NULL)
13196 result = false;
13197 continue;
13200 reloc_count = NUM_SHDR_ENTRIES (hdr);
13201 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13203 free (native_relocs);
13204 bfd_set_error (bfd_error_file_too_big);
13205 result = false;
13206 continue;
13209 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13210 if (internal_relocs == NULL)
13212 free (native_relocs);
13213 result = false;
13214 continue;
13217 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13218 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13219 != hdr->sh_size))
13221 free (native_relocs);
13222 /* The internal_relocs will be freed when
13223 the memory for the bfd is released. */
13224 result = false;
13225 continue;
13228 if (dynamic)
13229 symcount = bfd_get_dynamic_symcount (abfd);
13230 else
13231 symcount = bfd_get_symcount (abfd);
13233 for (i = 0, internal_reloc = internal_relocs,
13234 native_reloc = native_relocs;
13235 i < reloc_count;
13236 i++, internal_reloc++, native_reloc += entsize)
13238 bool res;
13239 Elf_Internal_Rela rela;
13241 if (entsize == ebd->s->sizeof_rel)
13242 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13243 else /* entsize == ebd->s->sizeof_rela */
13244 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13246 /* The address of an ELF reloc is section relative for an object
13247 file, and absolute for an executable file or shared library.
13248 The address of a normal BFD reloc is always section relative,
13249 and the address of a dynamic reloc is absolute.. */
13250 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13251 internal_reloc->address = rela.r_offset;
13252 else
13253 internal_reloc->address = rela.r_offset - sec->vma;
13255 if (r_sym (rela.r_info) == STN_UNDEF)
13257 /* FIXME: This and the error case below mean that we
13258 have a symbol on relocs that is not elf_symbol_type. */
13259 internal_reloc->sym_ptr_ptr =
13260 bfd_abs_section_ptr->symbol_ptr_ptr;
13262 else if (r_sym (rela.r_info) > symcount)
13264 _bfd_error_handler
13265 /* xgettext:c-format */
13266 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13267 abfd, sec, i, (long) r_sym (rela.r_info));
13268 bfd_set_error (bfd_error_bad_value);
13269 internal_reloc->sym_ptr_ptr =
13270 bfd_abs_section_ptr->symbol_ptr_ptr;
13271 result = false;
13273 else
13275 asymbol **ps;
13277 ps = symbols + r_sym (rela.r_info) - 1;
13278 internal_reloc->sym_ptr_ptr = ps;
13279 /* Make sure that this symbol is not removed by strip. */
13280 (*ps)->flags |= BSF_KEEP;
13283 internal_reloc->addend = rela.r_addend;
13285 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13286 if (! res || internal_reloc->howto == NULL)
13288 #if DEBUG_SECONDARY_RELOCS
13289 fprintf (stderr,
13290 "there is no howto associated with reloc %lx\n",
13291 rela.r_info);
13292 #endif
13293 result = false;
13297 free (native_relocs);
13298 /* Store the internal relocs. */
13299 elf_section_data (relsec)->sec_info = internal_relocs;
13303 return result;
13306 /* Set the ELF section header fields of an output secondary reloc section. */
13308 bool
13309 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13310 bfd *obfd ATTRIBUTE_UNUSED,
13311 const Elf_Internal_Shdr *isection,
13312 Elf_Internal_Shdr *osection)
13314 asection * isec;
13315 asection * osec;
13316 struct bfd_elf_section_data * esd;
13318 if (isection == NULL)
13319 return false;
13321 if (isection->sh_type != SHT_SECONDARY_RELOC)
13322 return true;
13324 isec = isection->bfd_section;
13325 if (isec == NULL)
13326 return false;
13328 osec = osection->bfd_section;
13329 if (osec == NULL)
13330 return false;
13332 esd = elf_section_data (osec);
13333 BFD_ASSERT (esd->sec_info == NULL);
13334 esd->sec_info = elf_section_data (isec)->sec_info;
13335 osection->sh_type = SHT_RELA;
13336 osection->sh_link = elf_onesymtab (obfd);
13337 if (osection->sh_link == 0)
13339 /* There is no symbol table - we are hosed... */
13340 _bfd_error_handler
13341 /* xgettext:c-format */
13342 (_("%pB(%pA): link section cannot be set"
13343 " because the output file does not have a symbol table"),
13344 obfd, osec);
13345 bfd_set_error (bfd_error_bad_value);
13346 return false;
13349 /* Find the output section that corresponds to the isection's
13350 sh_info link. */
13351 if (isection->sh_info == 0
13352 || isection->sh_info >= elf_numsections (ibfd))
13354 _bfd_error_handler
13355 /* xgettext:c-format */
13356 (_("%pB(%pA): info section index is invalid"),
13357 obfd, osec);
13358 bfd_set_error (bfd_error_bad_value);
13359 return false;
13362 isection = elf_elfsections (ibfd)[isection->sh_info];
13364 if (isection == NULL
13365 || isection->bfd_section == NULL
13366 || isection->bfd_section->output_section == NULL)
13368 _bfd_error_handler
13369 /* xgettext:c-format */
13370 (_("%pB(%pA): info section index cannot be set"
13371 " because the section is not in the output"),
13372 obfd, osec);
13373 bfd_set_error (bfd_error_bad_value);
13374 return false;
13377 esd = elf_section_data (isection->bfd_section->output_section);
13378 BFD_ASSERT (esd != NULL);
13379 osection->sh_info = esd->this_idx;
13380 esd->has_secondary_relocs = true;
13381 #if DEBUG_SECONDARY_RELOCS
13382 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13383 osec->name, osection->sh_link, osection->sh_info);
13384 fprintf (stderr, "mark section %s as having secondary relocs\n",
13385 bfd_section_name (isection->bfd_section->output_section));
13386 #endif
13388 return true;
13391 /* Write out a secondary reloc section.
13393 FIXME: Currently this function can result in a serious performance penalty
13394 for files with secondary relocs and lots of sections. The proper way to
13395 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13396 relocs together and then to have this function just walk that chain. */
13398 bool
13399 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13401 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13402 bfd_vma addr_offset;
13403 asection * relsec;
13404 bfd_vma (*r_info) (bfd_vma, bfd_vma);
13405 bool result = true;
13407 if (sec == NULL)
13408 return false;
13410 #if BFD_DEFAULT_TARGET_SIZE > 32
13411 if (bfd_arch_bits_per_address (abfd) != 32)
13412 r_info = elf64_r_info;
13413 else
13414 #endif
13415 r_info = elf32_r_info;
13417 /* The address of an ELF reloc is section relative for an object
13418 file, and absolute for an executable file or shared library.
13419 The address of a BFD reloc is always section relative. */
13420 addr_offset = 0;
13421 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13422 addr_offset = sec->vma;
13424 /* Discover if there are any secondary reloc sections
13425 associated with SEC. */
13426 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13428 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13429 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13431 if (hdr->sh_type == SHT_RELA
13432 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13434 asymbol * last_sym;
13435 int last_sym_idx;
13436 size_t reloc_count;
13437 size_t idx;
13438 bfd_size_type entsize;
13439 arelent * src_irel;
13440 bfd_byte * dst_rela;
13442 if (hdr->contents != NULL)
13444 _bfd_error_handler
13445 /* xgettext:c-format */
13446 (_("%pB(%pA): error: secondary reloc section processed twice"),
13447 abfd, relsec);
13448 bfd_set_error (bfd_error_bad_value);
13449 result = false;
13450 continue;
13453 entsize = hdr->sh_entsize;
13454 if (entsize == 0)
13456 _bfd_error_handler
13457 /* xgettext:c-format */
13458 (_("%pB(%pA): error: secondary reloc section"
13459 " has zero sized entries"),
13460 abfd, relsec);
13461 bfd_set_error (bfd_error_bad_value);
13462 result = false;
13463 continue;
13465 else if (entsize != ebd->s->sizeof_rel
13466 && entsize != ebd->s->sizeof_rela)
13468 _bfd_error_handler
13469 /* xgettext:c-format */
13470 (_("%pB(%pA): error: secondary reloc section"
13471 " has non-standard sized entries"),
13472 abfd, relsec);
13473 bfd_set_error (bfd_error_bad_value);
13474 result = false;
13475 continue;
13478 reloc_count = hdr->sh_size / entsize;
13479 hdr->sh_size = entsize * reloc_count;
13480 if (reloc_count == 0)
13482 _bfd_error_handler
13483 /* xgettext:c-format */
13484 (_("%pB(%pA): error: secondary reloc section is empty!"),
13485 abfd, relsec);
13486 bfd_set_error (bfd_error_bad_value);
13487 result = false;
13488 continue;
13491 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13492 if (hdr->contents == NULL)
13493 continue;
13495 #if DEBUG_SECONDARY_RELOCS
13496 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13497 reloc_count, sec->name, relsec->name);
13498 #endif
13499 last_sym = NULL;
13500 last_sym_idx = 0;
13501 dst_rela = hdr->contents;
13502 src_irel = (arelent *) esd->sec_info;
13503 if (src_irel == NULL)
13505 _bfd_error_handler
13506 /* xgettext:c-format */
13507 (_("%pB(%pA): error: internal relocs missing"
13508 " for secondary reloc section"),
13509 abfd, relsec);
13510 bfd_set_error (bfd_error_bad_value);
13511 result = false;
13512 continue;
13515 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13517 Elf_Internal_Rela src_rela;
13518 arelent *ptr;
13519 asymbol *sym;
13520 int n;
13522 ptr = src_irel + idx;
13523 if (ptr == NULL)
13525 _bfd_error_handler
13526 /* xgettext:c-format */
13527 (_("%pB(%pA): error: reloc table entry %zu is empty"),
13528 abfd, relsec, idx);
13529 bfd_set_error (bfd_error_bad_value);
13530 result = false;
13531 break;
13534 if (ptr->sym_ptr_ptr == NULL)
13536 /* FIXME: Is this an error ? */
13537 n = 0;
13539 else
13541 sym = *ptr->sym_ptr_ptr;
13543 if (sym == last_sym)
13544 n = last_sym_idx;
13545 else
13547 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13548 if (n < 0)
13550 _bfd_error_handler
13551 /* xgettext:c-format */
13552 (_("%pB(%pA): error: secondary reloc %zu"
13553 " references a missing symbol"),
13554 abfd, relsec, idx);
13555 bfd_set_error (bfd_error_bad_value);
13556 result = false;
13557 n = 0;
13560 last_sym = sym;
13561 last_sym_idx = n;
13564 if (sym->the_bfd != NULL
13565 && sym->the_bfd->xvec != abfd->xvec
13566 && ! _bfd_elf_validate_reloc (abfd, ptr))
13568 _bfd_error_handler
13569 /* xgettext:c-format */
13570 (_("%pB(%pA): error: secondary reloc %zu"
13571 " references a deleted symbol"),
13572 abfd, relsec, idx);
13573 bfd_set_error (bfd_error_bad_value);
13574 result = false;
13575 n = 0;
13579 src_rela.r_offset = ptr->address + addr_offset;
13580 if (ptr->howto == NULL)
13582 _bfd_error_handler
13583 /* xgettext:c-format */
13584 (_("%pB(%pA): error: secondary reloc %zu"
13585 " is of an unknown type"),
13586 abfd, relsec, idx);
13587 bfd_set_error (bfd_error_bad_value);
13588 result = false;
13589 src_rela.r_info = r_info (0, 0);
13591 else
13592 src_rela.r_info = r_info (n, ptr->howto->type);
13593 src_rela.r_addend = ptr->addend;
13595 if (entsize == ebd->s->sizeof_rel)
13596 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13597 else /* entsize == ebd->s->sizeof_rela */
13598 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13603 return result;