1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2019 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. */
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. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bfd_boolean prep_headers (bfd *);
55 static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
56 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
58 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
59 file_ptr offset, size_t align);
61 /* Swap version information in and out. The version information is
62 currently size independent. If that ever changes, this code will
63 need to move into elfcode.h. */
65 /* Swap in a Verdef structure. */
68 _bfd_elf_swap_verdef_in (bfd *abfd,
69 const Elf_External_Verdef *src,
70 Elf_Internal_Verdef *dst)
72 dst->vd_version = H_GET_16 (abfd, src->vd_version);
73 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
74 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
75 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
76 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
77 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
78 dst->vd_next = H_GET_32 (abfd, src->vd_next);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (bfd *abfd,
85 const Elf_Internal_Verdef *src,
86 Elf_External_Verdef *dst)
88 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
89 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
90 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
91 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
92 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
93 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
94 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
97 /* Swap in a Verdaux structure. */
100 _bfd_elf_swap_verdaux_in (bfd *abfd,
101 const Elf_External_Verdaux *src,
102 Elf_Internal_Verdaux *dst)
104 dst->vda_name = H_GET_32 (abfd, src->vda_name);
105 dst->vda_next = H_GET_32 (abfd, src->vda_next);
108 /* Swap out a Verdaux structure. */
111 _bfd_elf_swap_verdaux_out (bfd *abfd,
112 const Elf_Internal_Verdaux *src,
113 Elf_External_Verdaux *dst)
115 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
116 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (bfd *abfd,
123 const Elf_External_Verneed *src,
124 Elf_Internal_Verneed *dst)
126 dst->vn_version = H_GET_16 (abfd, src->vn_version);
127 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
128 dst->vn_file = H_GET_32 (abfd, src->vn_file);
129 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
130 dst->vn_next = H_GET_32 (abfd, src->vn_next);
133 /* Swap out a Verneed structure. */
136 _bfd_elf_swap_verneed_out (bfd *abfd,
137 const Elf_Internal_Verneed *src,
138 Elf_External_Verneed *dst)
140 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
141 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
142 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
143 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
144 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
147 /* Swap in a Vernaux structure. */
150 _bfd_elf_swap_vernaux_in (bfd *abfd,
151 const Elf_External_Vernaux *src,
152 Elf_Internal_Vernaux *dst)
154 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
155 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
156 dst->vna_other = H_GET_16 (abfd, src->vna_other);
157 dst->vna_name = H_GET_32 (abfd, src->vna_name);
158 dst->vna_next = H_GET_32 (abfd, src->vna_next);
161 /* Swap out a Vernaux structure. */
164 _bfd_elf_swap_vernaux_out (bfd *abfd,
165 const Elf_Internal_Vernaux *src,
166 Elf_External_Vernaux *dst)
168 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
169 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
170 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
171 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
172 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
175 /* Swap in a Versym structure. */
178 _bfd_elf_swap_versym_in (bfd *abfd,
179 const Elf_External_Versym *src,
180 Elf_Internal_Versym *dst)
182 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
185 /* Swap out a Versym structure. */
188 _bfd_elf_swap_versym_out (bfd *abfd,
189 const Elf_Internal_Versym *src,
190 Elf_External_Versym *dst)
192 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
195 /* Standard ELF hash function. Do not change this function; you will
196 cause invalid hash tables to be generated. */
199 bfd_elf_hash (const char *namearg)
201 const unsigned char *name = (const unsigned char *) namearg;
206 while ((ch = *name++) != '\0')
209 if ((g = (h & 0xf0000000)) != 0)
212 /* The ELF ABI says `h &= ~g', but this is equivalent in
213 this case and on some machines one insn instead of two. */
217 return h & 0xffffffff;
220 /* DT_GNU_HASH hash function. Do not change this function; you will
221 cause invalid hash tables to be generated. */
224 bfd_elf_gnu_hash (const char *namearg)
226 const unsigned char *name = (const unsigned char *) namearg;
227 unsigned long h = 5381;
230 while ((ch = *name++) != '\0')
231 h = (h << 5) + h + ch;
232 return h & 0xffffffff;
235 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
236 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
238 bfd_elf_allocate_object (bfd *abfd,
240 enum elf_target_id object_id)
242 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
243 abfd->tdata.any = bfd_zalloc (abfd, object_size);
244 if (abfd->tdata.any == NULL)
247 elf_object_id (abfd) = object_id;
248 if (abfd->direction != read_direction)
250 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
253 elf_tdata (abfd)->o = o;
254 elf_program_header_size (abfd) = (bfd_size_type) -1;
261 bfd_elf_make_object (bfd *abfd)
263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
264 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
269 bfd_elf_mkcorefile (bfd *abfd)
271 /* I think this can be done just like an object file. */
272 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
274 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
275 return elf_tdata (abfd)->core != NULL;
279 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
281 Elf_Internal_Shdr **i_shdrp;
282 bfd_byte *shstrtab = NULL;
284 bfd_size_type shstrtabsize;
286 i_shdrp = elf_elfsections (abfd);
288 || shindex >= elf_numsections (abfd)
289 || i_shdrp[shindex] == 0)
292 shstrtab = i_shdrp[shindex]->contents;
293 if (shstrtab == NULL)
295 /* No cached one, attempt to read, and cache what we read. */
296 offset = i_shdrp[shindex]->sh_offset;
297 shstrtabsize = i_shdrp[shindex]->sh_size;
299 /* Allocate and clear an extra byte at the end, to prevent crashes
300 in case the string table is not terminated. */
301 if (shstrtabsize + 1 <= 1
302 || shstrtabsize > bfd_get_file_size (abfd)
303 || bfd_seek (abfd, offset, SEEK_SET) != 0
304 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
306 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
308 if (bfd_get_error () != bfd_error_system_call)
309 bfd_set_error (bfd_error_file_truncated);
310 bfd_release (abfd, shstrtab);
312 /* Once we've failed to read it, make sure we don't keep
313 trying. Otherwise, we'll keep allocating space for
314 the string table over and over. */
315 i_shdrp[shindex]->sh_size = 0;
318 shstrtab[shstrtabsize] = '\0';
319 i_shdrp[shindex]->contents = shstrtab;
321 return (char *) shstrtab;
325 bfd_elf_string_from_elf_section (bfd *abfd,
326 unsigned int shindex,
327 unsigned int strindex)
329 Elf_Internal_Shdr *hdr;
334 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
337 hdr = elf_elfsections (abfd)[shindex];
339 if (hdr->contents == NULL)
341 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
343 /* PR 17512: file: f057ec89. */
344 /* xgettext:c-format */
345 _bfd_error_handler (_("%pB: attempt to load strings from"
346 " a non-string section (number %d)"),
351 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
356 /* PR 24273: The string section's contents may have already
357 been loaded elsewhere, eg because a corrupt file has the
358 string section index in the ELF header pointing at a group
359 section. So be paranoid, and test that the last byte of
360 the section is zero. */
361 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
365 if (strindex >= hdr->sh_size)
367 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
369 /* xgettext:c-format */
370 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
371 abfd, strindex, (uint64_t) hdr->sh_size,
372 (shindex == shstrndx && strindex == hdr->sh_name
374 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
378 return ((char *) hdr->contents) + strindex;
381 /* Read and convert symbols to internal format.
382 SYMCOUNT specifies the number of symbols to read, starting from
383 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
384 are non-NULL, they are used to store the internal symbols, external
385 symbols, and symbol section index extensions, respectively.
386 Returns a pointer to the internal symbol buffer (malloced if necessary)
387 or NULL if there were no symbols or some kind of problem. */
390 bfd_elf_get_elf_syms (bfd *ibfd,
391 Elf_Internal_Shdr *symtab_hdr,
394 Elf_Internal_Sym *intsym_buf,
396 Elf_External_Sym_Shndx *extshndx_buf)
398 Elf_Internal_Shdr *shndx_hdr;
400 const bfd_byte *esym;
401 Elf_External_Sym_Shndx *alloc_extshndx;
402 Elf_External_Sym_Shndx *shndx;
403 Elf_Internal_Sym *alloc_intsym;
404 Elf_Internal_Sym *isym;
405 Elf_Internal_Sym *isymend;
406 const struct elf_backend_data *bed;
411 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
417 /* Normal syms might have section extension entries. */
419 if (elf_symtab_shndx_list (ibfd) != NULL)
421 elf_section_list * entry;
422 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
424 /* Find an index section that is linked to this symtab section. */
425 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
428 if (entry->hdr.sh_link >= elf_numsections (ibfd))
431 if (sections[entry->hdr.sh_link] == symtab_hdr)
433 shndx_hdr = & entry->hdr;
438 if (shndx_hdr == NULL)
440 if (symtab_hdr == & elf_symtab_hdr (ibfd))
441 /* Not really accurate, but this was how the old code used to work. */
442 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
443 /* Otherwise we do nothing. The assumption is that
444 the index table will not be needed. */
448 /* Read the symbols. */
450 alloc_extshndx = NULL;
452 bed = get_elf_backend_data (ibfd);
453 extsym_size = bed->s->sizeof_sym;
454 amt = (bfd_size_type) symcount * extsym_size;
455 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
456 if (extsym_buf == NULL)
458 alloc_ext = bfd_malloc2 (symcount, extsym_size);
459 extsym_buf = alloc_ext;
461 if (extsym_buf == NULL
462 || bfd_seek (ibfd, pos, SEEK_SET) != 0
463 || bfd_bread (extsym_buf, amt, ibfd) != amt)
469 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
473 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
474 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
475 if (extshndx_buf == NULL)
477 alloc_extshndx = (Elf_External_Sym_Shndx *)
478 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
479 extshndx_buf = alloc_extshndx;
481 if (extshndx_buf == NULL
482 || bfd_seek (ibfd, pos, SEEK_SET) != 0
483 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
490 if (intsym_buf == NULL)
492 alloc_intsym = (Elf_Internal_Sym *)
493 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
494 intsym_buf = alloc_intsym;
495 if (intsym_buf == NULL)
499 /* Convert the symbols to internal form. */
500 isymend = intsym_buf + symcount;
501 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
502 shndx = extshndx_buf;
504 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
505 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
507 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
508 /* xgettext:c-format */
509 _bfd_error_handler (_("%pB symbol number %lu references"
510 " nonexistent SHT_SYMTAB_SHNDX section"),
511 ibfd, (unsigned long) symoffset);
512 if (alloc_intsym != NULL)
519 if (alloc_ext != NULL)
521 if (alloc_extshndx != NULL)
522 free (alloc_extshndx);
527 /* Look up a symbol name. */
529 bfd_elf_sym_name (bfd *abfd,
530 Elf_Internal_Shdr *symtab_hdr,
531 Elf_Internal_Sym *isym,
535 unsigned int iname = isym->st_name;
536 unsigned int shindex = symtab_hdr->sh_link;
538 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
539 /* Check for a bogus st_shndx to avoid crashing. */
540 && isym->st_shndx < elf_numsections (abfd))
542 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
543 shindex = elf_elfheader (abfd)->e_shstrndx;
546 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
549 else if (sym_sec && *name == '\0')
550 name = bfd_section_name (abfd, sym_sec);
555 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
556 sections. The first element is the flags, the rest are section
559 typedef union elf_internal_group {
560 Elf_Internal_Shdr *shdr;
562 } Elf_Internal_Group;
564 /* Return the name of the group signature symbol. Why isn't the
565 signature just a string? */
568 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
570 Elf_Internal_Shdr *hdr;
571 unsigned char esym[sizeof (Elf64_External_Sym)];
572 Elf_External_Sym_Shndx eshndx;
573 Elf_Internal_Sym isym;
575 /* First we need to ensure the symbol table is available. Make sure
576 that it is a symbol table section. */
577 if (ghdr->sh_link >= elf_numsections (abfd))
579 hdr = elf_elfsections (abfd) [ghdr->sh_link];
580 if (hdr->sh_type != SHT_SYMTAB
581 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
584 /* Go read the symbol. */
585 hdr = &elf_tdata (abfd)->symtab_hdr;
586 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
587 &isym, esym, &eshndx) == NULL)
590 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
593 /* Set next_in_group list pointer, and group name for NEWSECT. */
596 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
598 unsigned int num_group = elf_tdata (abfd)->num_group;
600 /* If num_group is zero, read in all SHT_GROUP sections. The count
601 is set to -1 if there are no SHT_GROUP sections. */
604 unsigned int i, shnum;
606 /* First count the number of groups. If we have a SHT_GROUP
607 section with just a flag word (ie. sh_size is 4), ignore it. */
608 shnum = elf_numsections (abfd);
611 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
612 ( (shdr)->sh_type == SHT_GROUP \
613 && (shdr)->sh_size >= minsize \
614 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
615 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
617 for (i = 0; i < shnum; i++)
619 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
621 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
627 num_group = (unsigned) -1;
628 elf_tdata (abfd)->num_group = num_group;
629 elf_tdata (abfd)->group_sect_ptr = NULL;
633 /* We keep a list of elf section headers for group sections,
634 so we can find them quickly. */
637 elf_tdata (abfd)->num_group = num_group;
638 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
639 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
640 if (elf_tdata (abfd)->group_sect_ptr == NULL)
642 memset (elf_tdata (abfd)->group_sect_ptr, 0,
643 num_group * sizeof (Elf_Internal_Shdr *));
646 for (i = 0; i < shnum; i++)
648 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
650 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
653 Elf_Internal_Group *dest;
655 /* Make sure the group section has a BFD section
657 if (!bfd_section_from_shdr (abfd, i))
660 /* Add to list of sections. */
661 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest) >= 4);
666 amt = shdr->sh_size * sizeof (*dest) / 4;
667 shdr->contents = (unsigned char *)
668 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
669 /* PR binutils/4110: Handle corrupt group headers. */
670 if (shdr->contents == NULL)
673 /* xgettext:c-format */
674 (_("%pB: corrupt size field in group section"
675 " header: %#" PRIx64),
676 abfd, (uint64_t) shdr->sh_size);
677 bfd_set_error (bfd_error_bad_value);
682 memset (shdr->contents, 0, amt);
684 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
685 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
689 /* xgettext:c-format */
690 (_("%pB: invalid size field in group section"
691 " header: %#" PRIx64 ""),
692 abfd, (uint64_t) shdr->sh_size);
693 bfd_set_error (bfd_error_bad_value);
695 /* PR 17510: If the group contents are even
696 partially corrupt, do not allow any of the
697 contents to be used. */
698 memset (shdr->contents, 0, amt);
702 /* Translate raw contents, a flag word followed by an
703 array of elf section indices all in target byte order,
704 to the flag word followed by an array of elf section
706 src = shdr->contents + shdr->sh_size;
707 dest = (Elf_Internal_Group *) (shdr->contents + amt);
715 idx = H_GET_32 (abfd, src);
716 if (src == shdr->contents)
719 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
720 shdr->bfd_section->flags
721 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
726 dest->shdr = elf_elfsections (abfd)[idx];
727 /* PR binutils/23199: All sections in a
728 section group should be marked with
729 SHF_GROUP. But some tools generate
730 broken objects without SHF_GROUP. Fix
732 dest->shdr->sh_flags |= SHF_GROUP;
735 || dest->shdr->sh_type == SHT_GROUP)
738 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
746 /* PR 17510: Corrupt binaries might contain invalid groups. */
747 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
749 elf_tdata (abfd)->num_group = num_group;
751 /* If all groups are invalid then fail. */
754 elf_tdata (abfd)->group_sect_ptr = NULL;
755 elf_tdata (abfd)->num_group = num_group = -1;
757 (_("%pB: no valid group sections found"), abfd);
758 bfd_set_error (bfd_error_bad_value);
764 if (num_group != (unsigned) -1)
766 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
769 for (j = 0; j < num_group; j++)
771 /* Begin search from previous found group. */
772 unsigned i = (j + search_offset) % num_group;
774 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
775 Elf_Internal_Group *idx;
781 idx = (Elf_Internal_Group *) shdr->contents;
782 if (idx == NULL || shdr->sh_size < 4)
784 /* See PR 21957 for a reproducer. */
785 /* xgettext:c-format */
786 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
787 abfd, shdr->bfd_section);
788 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
789 bfd_set_error (bfd_error_bad_value);
792 n_elt = shdr->sh_size / 4;
794 /* Look through this group's sections to see if current
795 section is a member. */
797 if ((++idx)->shdr == hdr)
801 /* We are a member of this group. Go looking through
802 other members to see if any others are linked via
804 idx = (Elf_Internal_Group *) shdr->contents;
805 n_elt = shdr->sh_size / 4;
807 if ((++idx)->shdr != NULL
808 && (s = idx->shdr->bfd_section) != NULL
809 && elf_next_in_group (s) != NULL)
813 /* Snarf the group name from other member, and
814 insert current section in circular list. */
815 elf_group_name (newsect) = elf_group_name (s);
816 elf_next_in_group (newsect) = elf_next_in_group (s);
817 elf_next_in_group (s) = newsect;
823 gname = group_signature (abfd, shdr);
826 elf_group_name (newsect) = gname;
828 /* Start a circular list with one element. */
829 elf_next_in_group (newsect) = newsect;
832 /* If the group section has been created, point to the
834 if (shdr->bfd_section != NULL)
835 elf_next_in_group (shdr->bfd_section) = newsect;
837 elf_tdata (abfd)->group_search_offset = i;
844 if (elf_group_name (newsect) == NULL)
846 /* xgettext:c-format */
847 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
855 _bfd_elf_setup_sections (bfd *abfd)
858 unsigned int num_group = elf_tdata (abfd)->num_group;
859 bfd_boolean result = TRUE;
862 /* Process SHF_LINK_ORDER. */
863 for (s = abfd->sections; s != NULL; s = s->next)
865 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
866 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
868 unsigned int elfsec = this_hdr->sh_link;
869 /* FIXME: The old Intel compiler and old strip/objcopy may
870 not set the sh_link or sh_info fields. Hence we could
871 get the situation where elfsec is 0. */
874 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
875 if (bed->link_order_error_handler)
876 bed->link_order_error_handler
877 /* xgettext:c-format */
878 (_("%pB: warning: sh_link not set for section `%pA'"),
883 asection *linksec = NULL;
885 if (elfsec < elf_numsections (abfd))
887 this_hdr = elf_elfsections (abfd)[elfsec];
888 linksec = this_hdr->bfd_section;
892 Some strip/objcopy may leave an incorrect value in
893 sh_link. We don't want to proceed. */
897 /* xgettext:c-format */
898 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
899 s->owner, elfsec, s);
903 elf_linked_to_section (s) = linksec;
906 else if (this_hdr->sh_type == SHT_GROUP
907 && elf_next_in_group (s) == NULL)
910 /* xgettext:c-format */
911 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
912 abfd, elf_section_data (s)->this_idx);
917 /* Process section groups. */
918 if (num_group == (unsigned) -1)
921 for (i = 0; i < num_group; i++)
923 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
924 Elf_Internal_Group *idx;
927 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
928 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
931 /* xgettext:c-format */
932 (_("%pB: section group entry number %u is corrupt"),
938 idx = (Elf_Internal_Group *) shdr->contents;
939 n_elt = shdr->sh_size / 4;
945 if (idx->shdr == NULL)
947 else if (idx->shdr->bfd_section)
948 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
949 else if (idx->shdr->sh_type != SHT_RELA
950 && idx->shdr->sh_type != SHT_REL)
952 /* There are some unknown sections in the group. */
954 /* xgettext:c-format */
955 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
958 bfd_elf_string_from_elf_section (abfd,
959 (elf_elfheader (abfd)
972 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
974 return elf_next_in_group (sec) != NULL;
978 convert_debug_to_zdebug (bfd *abfd, const char *name)
980 unsigned int len = strlen (name);
981 char *new_name = bfd_alloc (abfd, len + 2);
982 if (new_name == NULL)
986 memcpy (new_name + 2, name + 1, len);
991 convert_zdebug_to_debug (bfd *abfd, const char *name)
993 unsigned int len = strlen (name);
994 char *new_name = bfd_alloc (abfd, len);
995 if (new_name == NULL)
998 memcpy (new_name + 1, name + 2, len - 1);
1002 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
1006 int16_t major_version;
1007 int16_t minor_version;
1008 unsigned char slim_object;
1010 /* Flags is a private field that is not defined publicly. */
1014 /* Make a BFD section from an ELF section. We store a pointer to the
1015 BFD section in the bfd_section field of the header. */
1018 _bfd_elf_make_section_from_shdr (bfd *abfd,
1019 Elf_Internal_Shdr *hdr,
1025 const struct elf_backend_data *bed;
1027 if (hdr->bfd_section != NULL)
1030 newsect = bfd_make_section_anyway (abfd, name);
1031 if (newsect == NULL)
1034 hdr->bfd_section = newsect;
1035 elf_section_data (newsect)->this_hdr = *hdr;
1036 elf_section_data (newsect)->this_idx = shindex;
1038 /* Always use the real type/flags. */
1039 elf_section_type (newsect) = hdr->sh_type;
1040 elf_section_flags (newsect) = hdr->sh_flags;
1042 newsect->filepos = hdr->sh_offset;
1044 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1045 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1046 || ! bfd_set_section_alignment (abfd, newsect,
1047 bfd_log2 (hdr->sh_addralign)))
1050 flags = SEC_NO_FLAGS;
1051 if (hdr->sh_type != SHT_NOBITS)
1052 flags |= SEC_HAS_CONTENTS;
1053 if (hdr->sh_type == SHT_GROUP)
1055 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1058 if (hdr->sh_type != SHT_NOBITS)
1061 if ((hdr->sh_flags & SHF_WRITE) == 0)
1062 flags |= SEC_READONLY;
1063 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1065 else if ((flags & SEC_LOAD) != 0)
1067 if ((hdr->sh_flags & SHF_MERGE) != 0)
1070 newsect->entsize = hdr->sh_entsize;
1072 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1073 flags |= SEC_STRINGS;
1074 if (hdr->sh_flags & SHF_GROUP)
1075 if (!setup_group (abfd, hdr, newsect))
1077 if ((hdr->sh_flags & SHF_TLS) != 0)
1078 flags |= SEC_THREAD_LOCAL;
1079 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1080 flags |= SEC_EXCLUDE;
1082 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1084 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1085 but binutils as of 2019-07-23 did not set the EI_OSABI header
1089 case ELFOSABI_FREEBSD:
1090 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1091 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1095 if ((flags & SEC_ALLOC) == 0)
1097 /* The debugging sections appear to be recognized only by name,
1098 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1099 if (name [0] == '.')
1104 p = ".debug", n = 6;
1105 else if (name[1] == 'g' && name[2] == 'n')
1106 p = ".gnu.linkonce.wi.", n = 17;
1107 else if (name[1] == 'g' && name[2] == 'd')
1108 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1109 else if (name[1] == 'l')
1111 else if (name[1] == 's')
1113 else if (name[1] == 'z')
1114 p = ".zdebug", n = 7;
1117 if (p != NULL && strncmp (name, p, n) == 0)
1118 flags |= SEC_DEBUGGING;
1122 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1123 only link a single copy of the section. This is used to support
1124 g++. g++ will emit each template expansion in its own section.
1125 The symbols will be defined as weak, so that multiple definitions
1126 are permitted. The GNU linker extension is to actually discard
1127 all but one of the sections. */
1128 if (CONST_STRNEQ (name, ".gnu.linkonce")
1129 && elf_next_in_group (newsect) == NULL)
1130 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1132 bed = get_elf_backend_data (abfd);
1133 if (bed->elf_backend_section_flags)
1134 if (! bed->elf_backend_section_flags (&flags, hdr))
1137 if (! bfd_set_section_flags (abfd, newsect, flags))
1140 /* We do not parse the PT_NOTE segments as we are interested even in the
1141 separate debug info files which may have the segments offsets corrupted.
1142 PT_NOTEs from the core files are currently not parsed using BFD. */
1143 if (hdr->sh_type == SHT_NOTE)
1147 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1150 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1151 hdr->sh_offset, hdr->sh_addralign);
1155 if ((flags & SEC_ALLOC) != 0)
1157 Elf_Internal_Phdr *phdr;
1158 unsigned int i, nload;
1160 /* Some ELF linkers produce binaries with all the program header
1161 p_paddr fields zero. If we have such a binary with more than
1162 one PT_LOAD header, then leave the section lma equal to vma
1163 so that we don't create sections with overlapping lma. */
1164 phdr = elf_tdata (abfd)->phdr;
1165 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1166 if (phdr->p_paddr != 0)
1168 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1170 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1173 phdr = elf_tdata (abfd)->phdr;
1174 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1176 if (((phdr->p_type == PT_LOAD
1177 && (hdr->sh_flags & SHF_TLS) == 0)
1178 || phdr->p_type == PT_TLS)
1179 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1181 if ((flags & SEC_LOAD) == 0)
1182 newsect->lma = (phdr->p_paddr
1183 + hdr->sh_addr - phdr->p_vaddr);
1185 /* We used to use the same adjustment for SEC_LOAD
1186 sections, but that doesn't work if the segment
1187 is packed with code from multiple VMAs.
1188 Instead we calculate the section LMA based on
1189 the segment LMA. It is assumed that the
1190 segment will contain sections with contiguous
1191 LMAs, even if the VMAs are not. */
1192 newsect->lma = (phdr->p_paddr
1193 + hdr->sh_offset - phdr->p_offset);
1195 /* With contiguous segments, we can't tell from file
1196 offsets whether a section with zero size should
1197 be placed at the end of one segment or the
1198 beginning of the next. Decide based on vaddr. */
1199 if (hdr->sh_addr >= phdr->p_vaddr
1200 && (hdr->sh_addr + hdr->sh_size
1201 <= phdr->p_vaddr + phdr->p_memsz))
1207 /* Compress/decompress DWARF debug sections with names: .debug_* and
1208 .zdebug_*, after the section flags is set. */
1209 if ((flags & SEC_DEBUGGING)
1210 && ((name[1] == 'd' && name[6] == '_')
1211 || (name[1] == 'z' && name[7] == '_')))
1213 enum { nothing, compress, decompress } action = nothing;
1214 int compression_header_size;
1215 bfd_size_type uncompressed_size;
1216 unsigned int uncompressed_align_power;
1217 bfd_boolean compressed
1218 = bfd_is_section_compressed_with_header (abfd, newsect,
1219 &compression_header_size,
1221 &uncompressed_align_power);
1224 /* Compressed section. Check if we should decompress. */
1225 if ((abfd->flags & BFD_DECOMPRESS))
1226 action = decompress;
1229 /* Compress the uncompressed section or convert from/to .zdebug*
1230 section. Check if we should compress. */
1231 if (action == nothing)
1233 if (newsect->size != 0
1234 && (abfd->flags & BFD_COMPRESS)
1235 && compression_header_size >= 0
1236 && uncompressed_size > 0
1238 || ((compression_header_size > 0)
1239 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1245 if (action == compress)
1247 if (!bfd_init_section_compress_status (abfd, newsect))
1250 /* xgettext:c-format */
1251 (_("%pB: unable to initialize compress status for section %s"),
1258 if (!bfd_init_section_decompress_status (abfd, newsect))
1261 /* xgettext:c-format */
1262 (_("%pB: unable to initialize decompress status for section %s"),
1268 if (abfd->is_linker_input)
1271 && (action == decompress
1272 || (action == compress
1273 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1275 /* Convert section name from .zdebug_* to .debug_* so
1276 that linker will consider this section as a debug
1278 char *new_name = convert_zdebug_to_debug (abfd, name);
1279 if (new_name == NULL)
1281 bfd_rename_section (abfd, newsect, new_name);
1285 /* For objdump, don't rename the section. For objcopy, delay
1286 section rename to elf_fake_sections. */
1287 newsect->flags |= SEC_ELF_RENAME;
1290 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1292 const char *lto_section_name = ".gnu.lto_.lto.";
1293 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1295 struct lto_section lsection;
1296 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1297 sizeof (struct lto_section)))
1298 abfd->lto_slim_object = lsection.slim_object;
1304 const char *const bfd_elf_section_type_names[] =
1306 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1307 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1308 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1311 /* ELF relocs are against symbols. If we are producing relocatable
1312 output, and the reloc is against an external symbol, and nothing
1313 has given us any additional addend, the resulting reloc will also
1314 be against the same symbol. In such a case, we don't want to
1315 change anything about the way the reloc is handled, since it will
1316 all be done at final link time. Rather than put special case code
1317 into bfd_perform_relocation, all the reloc types use this howto
1318 function. It just short circuits the reloc if producing
1319 relocatable output against an external symbol. */
1321 bfd_reloc_status_type
1322 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1323 arelent *reloc_entry,
1325 void *data ATTRIBUTE_UNUSED,
1326 asection *input_section,
1328 char **error_message ATTRIBUTE_UNUSED)
1330 if (output_bfd != NULL
1331 && (symbol->flags & BSF_SECTION_SYM) == 0
1332 && (! reloc_entry->howto->partial_inplace
1333 || reloc_entry->addend == 0))
1335 reloc_entry->address += input_section->output_offset;
1336 return bfd_reloc_ok;
1339 return bfd_reloc_continue;
1342 /* Returns TRUE if section A matches section B.
1343 Names, addresses and links may be different, but everything else
1344 should be the same. */
1347 section_match (const Elf_Internal_Shdr * a,
1348 const Elf_Internal_Shdr * b)
1350 if (a->sh_type != b->sh_type
1351 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1352 || a->sh_addralign != b->sh_addralign
1353 || a->sh_entsize != b->sh_entsize)
1355 if (a->sh_type == SHT_SYMTAB
1356 || a->sh_type == SHT_STRTAB)
1358 return a->sh_size == b->sh_size;
1361 /* Find a section in OBFD that has the same characteristics
1362 as IHEADER. Return the index of this section or SHN_UNDEF if
1363 none can be found. Check's section HINT first, as this is likely
1364 to be the correct section. */
1367 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1368 const unsigned int hint)
1370 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1373 BFD_ASSERT (iheader != NULL);
1375 /* See PR 20922 for a reproducer of the NULL test. */
1376 if (hint < elf_numsections (obfd)
1377 && oheaders[hint] != NULL
1378 && section_match (oheaders[hint], iheader))
1381 for (i = 1; i < elf_numsections (obfd); i++)
1383 Elf_Internal_Shdr * oheader = oheaders[i];
1385 if (oheader == NULL)
1387 if (section_match (oheader, iheader))
1388 /* FIXME: Do we care if there is a potential for
1389 multiple matches ? */
1396 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1397 Processor specific section, based upon a matching input section.
1398 Returns TRUE upon success, FALSE otherwise. */
1401 copy_special_section_fields (const bfd *ibfd,
1403 const Elf_Internal_Shdr *iheader,
1404 Elf_Internal_Shdr *oheader,
1405 const unsigned int secnum)
1407 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1408 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1409 bfd_boolean changed = FALSE;
1410 unsigned int sh_link;
1412 if (oheader->sh_type == SHT_NOBITS)
1414 /* This is a feature for objcopy --only-keep-debug:
1415 When a section's type is changed to NOBITS, we preserve
1416 the sh_link and sh_info fields so that they can be
1417 matched up with the original.
1419 Note: Strictly speaking these assignments are wrong.
1420 The sh_link and sh_info fields should point to the
1421 relevent sections in the output BFD, which may not be in
1422 the same location as they were in the input BFD. But
1423 the whole point of this action is to preserve the
1424 original values of the sh_link and sh_info fields, so
1425 that they can be matched up with the section headers in
1426 the original file. So strictly speaking we may be
1427 creating an invalid ELF file, but it is only for a file
1428 that just contains debug info and only for sections
1429 without any contents. */
1430 if (oheader->sh_link == 0)
1431 oheader->sh_link = iheader->sh_link;
1432 if (oheader->sh_info == 0)
1433 oheader->sh_info = iheader->sh_info;
1437 /* Allow the target a chance to decide how these fields should be set. */
1438 if (bed->elf_backend_copy_special_section_fields != NULL
1439 && bed->elf_backend_copy_special_section_fields
1440 (ibfd, obfd, iheader, oheader))
1443 /* We have an iheader which might match oheader, and which has non-zero
1444 sh_info and/or sh_link fields. Attempt to follow those links and find
1445 the section in the output bfd which corresponds to the linked section
1446 in the input bfd. */
1447 if (iheader->sh_link != SHN_UNDEF)
1449 /* See PR 20931 for a reproducer. */
1450 if (iheader->sh_link >= elf_numsections (ibfd))
1453 /* xgettext:c-format */
1454 (_("%pB: invalid sh_link field (%d) in section number %d"),
1455 ibfd, iheader->sh_link, secnum);
1459 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1460 if (sh_link != SHN_UNDEF)
1462 oheader->sh_link = sh_link;
1466 /* FIXME: Should we install iheader->sh_link
1467 if we could not find a match ? */
1469 /* xgettext:c-format */
1470 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1473 if (iheader->sh_info)
1475 /* The sh_info field can hold arbitrary information, but if the
1476 SHF_LINK_INFO flag is set then it should be interpreted as a
1478 if (iheader->sh_flags & SHF_INFO_LINK)
1480 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1482 if (sh_link != SHN_UNDEF)
1483 oheader->sh_flags |= SHF_INFO_LINK;
1486 /* No idea what it means - just copy it. */
1487 sh_link = iheader->sh_info;
1489 if (sh_link != SHN_UNDEF)
1491 oheader->sh_info = sh_link;
1496 /* xgettext:c-format */
1497 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1503 /* Copy the program header and other data from one object module to
1507 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1509 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1510 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1511 const struct elf_backend_data *bed;
1514 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1515 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1518 if (!elf_flags_init (obfd))
1520 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1521 elf_flags_init (obfd) = TRUE;
1524 elf_gp (obfd) = elf_gp (ibfd);
1526 /* Also copy the EI_OSABI field. */
1527 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1528 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1530 /* If set, copy the EI_ABIVERSION field. */
1531 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1532 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1533 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1535 /* Copy object attributes. */
1536 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1538 if (iheaders == NULL || oheaders == NULL)
1541 bed = get_elf_backend_data (obfd);
1543 /* Possibly copy other fields in the section header. */
1544 for (i = 1; i < elf_numsections (obfd); i++)
1547 Elf_Internal_Shdr * oheader = oheaders[i];
1549 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1550 because of a special case need for generating separate debug info
1551 files. See below for more details. */
1553 || (oheader->sh_type != SHT_NOBITS
1554 && oheader->sh_type < SHT_LOOS))
1557 /* Ignore empty sections, and sections whose
1558 fields have already been initialised. */
1559 if (oheader->sh_size == 0
1560 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1563 /* Scan for the matching section in the input bfd.
1564 First we try for a direct mapping between the input and output sections. */
1565 for (j = 1; j < elf_numsections (ibfd); j++)
1567 const Elf_Internal_Shdr * iheader = iheaders[j];
1569 if (iheader == NULL)
1572 if (oheader->bfd_section != NULL
1573 && iheader->bfd_section != NULL
1574 && iheader->bfd_section->output_section != NULL
1575 && iheader->bfd_section->output_section == oheader->bfd_section)
1577 /* We have found a connection from the input section to the
1578 output section. Attempt to copy the header fields. If
1579 this fails then do not try any further sections - there
1580 should only be a one-to-one mapping between input and output. */
1581 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1582 j = elf_numsections (ibfd);
1587 if (j < elf_numsections (ibfd))
1590 /* That failed. So try to deduce the corresponding input section.
1591 Unfortunately we cannot compare names as the output string table
1592 is empty, so instead we check size, address and type. */
1593 for (j = 1; j < elf_numsections (ibfd); j++)
1595 const Elf_Internal_Shdr * iheader = iheaders[j];
1597 if (iheader == NULL)
1600 /* Try matching fields in the input section's header.
1601 Since --only-keep-debug turns all non-debug sections into
1602 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1604 if ((oheader->sh_type == SHT_NOBITS
1605 || iheader->sh_type == oheader->sh_type)
1606 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1607 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1608 && iheader->sh_addralign == oheader->sh_addralign
1609 && iheader->sh_entsize == oheader->sh_entsize
1610 && iheader->sh_size == oheader->sh_size
1611 && iheader->sh_addr == oheader->sh_addr
1612 && (iheader->sh_info != oheader->sh_info
1613 || iheader->sh_link != oheader->sh_link))
1615 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1620 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1622 /* Final attempt. Call the backend copy function
1623 with a NULL input section. */
1624 if (bed->elf_backend_copy_special_section_fields != NULL)
1625 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1633 get_segment_type (unsigned int p_type)
1638 case PT_NULL: pt = "NULL"; break;
1639 case PT_LOAD: pt = "LOAD"; break;
1640 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1641 case PT_INTERP: pt = "INTERP"; break;
1642 case PT_NOTE: pt = "NOTE"; break;
1643 case PT_SHLIB: pt = "SHLIB"; break;
1644 case PT_PHDR: pt = "PHDR"; break;
1645 case PT_TLS: pt = "TLS"; break;
1646 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1647 case PT_GNU_STACK: pt = "STACK"; break;
1648 case PT_GNU_RELRO: pt = "RELRO"; break;
1649 default: pt = NULL; break;
1654 /* Print out the program headers. */
1657 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1659 FILE *f = (FILE *) farg;
1660 Elf_Internal_Phdr *p;
1662 bfd_byte *dynbuf = NULL;
1664 p = elf_tdata (abfd)->phdr;
1669 fprintf (f, _("\nProgram Header:\n"));
1670 c = elf_elfheader (abfd)->e_phnum;
1671 for (i = 0; i < c; i++, p++)
1673 const char *pt = get_segment_type (p->p_type);
1678 sprintf (buf, "0x%lx", p->p_type);
1681 fprintf (f, "%8s off 0x", pt);
1682 bfd_fprintf_vma (abfd, f, p->p_offset);
1683 fprintf (f, " vaddr 0x");
1684 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1685 fprintf (f, " paddr 0x");
1686 bfd_fprintf_vma (abfd, f, p->p_paddr);
1687 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1688 fprintf (f, " filesz 0x");
1689 bfd_fprintf_vma (abfd, f, p->p_filesz);
1690 fprintf (f, " memsz 0x");
1691 bfd_fprintf_vma (abfd, f, p->p_memsz);
1692 fprintf (f, " flags %c%c%c",
1693 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1694 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1695 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1696 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1697 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1702 s = bfd_get_section_by_name (abfd, ".dynamic");
1705 unsigned int elfsec;
1706 unsigned long shlink;
1707 bfd_byte *extdyn, *extdynend;
1709 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1711 fprintf (f, _("\nDynamic Section:\n"));
1713 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1716 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1717 if (elfsec == SHN_BAD)
1719 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1721 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1722 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1725 /* PR 17512: file: 6f427532. */
1726 if (s->size < extdynsize)
1728 extdynend = extdyn + s->size;
1729 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1731 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1733 Elf_Internal_Dyn dyn;
1734 const char *name = "";
1736 bfd_boolean stringp;
1737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1739 (*swap_dyn_in) (abfd, extdyn, &dyn);
1741 if (dyn.d_tag == DT_NULL)
1748 if (bed->elf_backend_get_target_dtag)
1749 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1751 if (!strcmp (name, ""))
1753 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1758 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1759 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1760 case DT_PLTGOT: name = "PLTGOT"; break;
1761 case DT_HASH: name = "HASH"; break;
1762 case DT_STRTAB: name = "STRTAB"; break;
1763 case DT_SYMTAB: name = "SYMTAB"; break;
1764 case DT_RELA: name = "RELA"; break;
1765 case DT_RELASZ: name = "RELASZ"; break;
1766 case DT_RELAENT: name = "RELAENT"; break;
1767 case DT_STRSZ: name = "STRSZ"; break;
1768 case DT_SYMENT: name = "SYMENT"; break;
1769 case DT_INIT: name = "INIT"; break;
1770 case DT_FINI: name = "FINI"; break;
1771 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1772 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1773 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1774 case DT_REL: name = "REL"; break;
1775 case DT_RELSZ: name = "RELSZ"; break;
1776 case DT_RELENT: name = "RELENT"; break;
1777 case DT_PLTREL: name = "PLTREL"; break;
1778 case DT_DEBUG: name = "DEBUG"; break;
1779 case DT_TEXTREL: name = "TEXTREL"; break;
1780 case DT_JMPREL: name = "JMPREL"; break;
1781 case DT_BIND_NOW: name = "BIND_NOW"; break;
1782 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1783 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1784 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1785 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1786 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1787 case DT_FLAGS: name = "FLAGS"; break;
1788 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1789 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1790 case DT_CHECKSUM: name = "CHECKSUM"; break;
1791 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1792 case DT_MOVEENT: name = "MOVEENT"; break;
1793 case DT_MOVESZ: name = "MOVESZ"; break;
1794 case DT_FEATURE: name = "FEATURE"; break;
1795 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1796 case DT_SYMINSZ: name = "SYMINSZ"; break;
1797 case DT_SYMINENT: name = "SYMINENT"; break;
1798 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1799 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1800 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1801 case DT_PLTPAD: name = "PLTPAD"; break;
1802 case DT_MOVETAB: name = "MOVETAB"; break;
1803 case DT_SYMINFO: name = "SYMINFO"; break;
1804 case DT_RELACOUNT: name = "RELACOUNT"; break;
1805 case DT_RELCOUNT: name = "RELCOUNT"; break;
1806 case DT_FLAGS_1: name = "FLAGS_1"; break;
1807 case DT_VERSYM: name = "VERSYM"; break;
1808 case DT_VERDEF: name = "VERDEF"; break;
1809 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1810 case DT_VERNEED: name = "VERNEED"; break;
1811 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1812 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1813 case DT_USED: name = "USED"; break;
1814 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1815 case DT_GNU_HASH: name = "GNU_HASH"; break;
1818 fprintf (f, " %-20s ", name);
1822 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1827 unsigned int tagv = dyn.d_un.d_val;
1829 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1832 fprintf (f, "%s", string);
1841 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1842 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1844 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1848 if (elf_dynverdef (abfd) != 0)
1850 Elf_Internal_Verdef *t;
1852 fprintf (f, _("\nVersion definitions:\n"));
1853 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1855 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1856 t->vd_flags, t->vd_hash,
1857 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1858 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1860 Elf_Internal_Verdaux *a;
1863 for (a = t->vd_auxptr->vda_nextptr;
1867 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1873 if (elf_dynverref (abfd) != 0)
1875 Elf_Internal_Verneed *t;
1877 fprintf (f, _("\nVersion References:\n"));
1878 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1880 Elf_Internal_Vernaux *a;
1882 fprintf (f, _(" required from %s:\n"),
1883 t->vn_filename ? t->vn_filename : "<corrupt>");
1884 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1885 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1886 a->vna_flags, a->vna_other,
1887 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1899 /* Get version string. */
1902 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1903 bfd_boolean *hidden)
1905 const char *version_string = NULL;
1906 if (elf_dynversym (abfd) != 0
1907 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1909 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1911 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1912 vernum &= VERSYM_VERSION;
1915 version_string = "";
1916 else if (vernum == 1
1917 && (vernum > elf_tdata (abfd)->cverdefs
1918 || (elf_tdata (abfd)->verdef[0].vd_flags
1920 version_string = "Base";
1921 else if (vernum <= elf_tdata (abfd)->cverdefs)
1923 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1926 Elf_Internal_Verneed *t;
1928 version_string = _("<corrupt>");
1929 for (t = elf_tdata (abfd)->verref;
1933 Elf_Internal_Vernaux *a;
1935 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1937 if (a->vna_other == vernum)
1939 version_string = a->vna_nodename;
1946 return version_string;
1949 /* Display ELF-specific fields of a symbol. */
1952 bfd_elf_print_symbol (bfd *abfd,
1955 bfd_print_symbol_type how)
1957 FILE *file = (FILE *) filep;
1960 case bfd_print_symbol_name:
1961 fprintf (file, "%s", symbol->name);
1963 case bfd_print_symbol_more:
1964 fprintf (file, "elf ");
1965 bfd_fprintf_vma (abfd, file, symbol->value);
1966 fprintf (file, " %x", symbol->flags);
1968 case bfd_print_symbol_all:
1970 const char *section_name;
1971 const char *name = NULL;
1972 const struct elf_backend_data *bed;
1973 unsigned char st_other;
1975 const char *version_string;
1978 section_name = symbol->section ? symbol->section->name : "(*none*)";
1980 bed = get_elf_backend_data (abfd);
1981 if (bed->elf_backend_print_symbol_all)
1982 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1986 name = symbol->name;
1987 bfd_print_symbol_vandf (abfd, file, symbol);
1990 fprintf (file, " %s\t", section_name);
1991 /* Print the "other" value for a symbol. For common symbols,
1992 we've already printed the size; now print the alignment.
1993 For other symbols, we have no specified alignment, and
1994 we've printed the address; now print the size. */
1995 if (symbol->section && bfd_is_com_section (symbol->section))
1996 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1998 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1999 bfd_fprintf_vma (abfd, file, val);
2001 /* If we have version information, print it. */
2002 version_string = _bfd_elf_get_symbol_version_string (abfd,
2008 fprintf (file, " %-11s", version_string);
2013 fprintf (file, " (%s)", version_string);
2014 for (i = 10 - strlen (version_string); i > 0; --i)
2019 /* If the st_other field is not zero, print it. */
2020 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2025 case STV_INTERNAL: fprintf (file, " .internal"); break;
2026 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2027 case STV_PROTECTED: fprintf (file, " .protected"); break;
2029 /* Some other non-defined flags are also present, so print
2031 fprintf (file, " 0x%02x", (unsigned int) st_other);
2034 fprintf (file, " %s", name);
2040 /* ELF .o/exec file reading */
2042 /* Create a new bfd section from an ELF section header. */
2045 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2047 Elf_Internal_Shdr *hdr;
2048 Elf_Internal_Ehdr *ehdr;
2049 const struct elf_backend_data *bed;
2051 bfd_boolean ret = TRUE;
2052 static bfd_boolean * sections_being_created = NULL;
2053 static bfd * sections_being_created_abfd = NULL;
2054 static unsigned int nesting = 0;
2056 if (shindex >= elf_numsections (abfd))
2061 /* PR17512: A corrupt ELF binary might contain a recursive group of
2062 sections, with each the string indices pointing to the next in the
2063 loop. Detect this here, by refusing to load a section that we are
2064 already in the process of loading. We only trigger this test if
2065 we have nested at least three sections deep as normal ELF binaries
2066 can expect to recurse at least once.
2068 FIXME: It would be better if this array was attached to the bfd,
2069 rather than being held in a static pointer. */
2071 if (sections_being_created_abfd != abfd)
2072 sections_being_created = NULL;
2073 if (sections_being_created == NULL)
2075 sections_being_created = (bfd_boolean *)
2076 bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean));
2077 sections_being_created_abfd = abfd;
2079 if (sections_being_created [shindex])
2082 (_("%pB: warning: loop in section dependencies detected"), abfd);
2085 sections_being_created [shindex] = TRUE;
2088 hdr = elf_elfsections (abfd)[shindex];
2089 ehdr = elf_elfheader (abfd);
2090 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2095 bed = get_elf_backend_data (abfd);
2096 switch (hdr->sh_type)
2099 /* Inactive section. Throw it away. */
2102 case SHT_PROGBITS: /* Normal section with contents. */
2103 case SHT_NOBITS: /* .bss section. */
2104 case SHT_HASH: /* .hash section. */
2105 case SHT_NOTE: /* .note section. */
2106 case SHT_INIT_ARRAY: /* .init_array section. */
2107 case SHT_FINI_ARRAY: /* .fini_array section. */
2108 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2109 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2110 case SHT_GNU_HASH: /* .gnu.hash section. */
2111 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2114 case SHT_DYNAMIC: /* Dynamic linking information. */
2115 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2118 if (hdr->sh_link > elf_numsections (abfd))
2120 /* PR 10478: Accept Solaris binaries with a sh_link
2121 field set to SHN_BEFORE or SHN_AFTER. */
2122 switch (bfd_get_arch (abfd))
2125 case bfd_arch_sparc:
2126 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2127 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2129 /* Otherwise fall through. */
2134 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2136 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2138 Elf_Internal_Shdr *dynsymhdr;
2140 /* The shared libraries distributed with hpux11 have a bogus
2141 sh_link field for the ".dynamic" section. Find the
2142 string table for the ".dynsym" section instead. */
2143 if (elf_dynsymtab (abfd) != 0)
2145 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2146 hdr->sh_link = dynsymhdr->sh_link;
2150 unsigned int i, num_sec;
2152 num_sec = elf_numsections (abfd);
2153 for (i = 1; i < num_sec; i++)
2155 dynsymhdr = elf_elfsections (abfd)[i];
2156 if (dynsymhdr->sh_type == SHT_DYNSYM)
2158 hdr->sh_link = dynsymhdr->sh_link;
2166 case SHT_SYMTAB: /* A symbol table. */
2167 if (elf_onesymtab (abfd) == shindex)
2170 if (hdr->sh_entsize != bed->s->sizeof_sym)
2173 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2175 if (hdr->sh_size != 0)
2177 /* Some assemblers erroneously set sh_info to one with a
2178 zero sh_size. ld sees this as a global symbol count
2179 of (unsigned) -1. Fix it here. */
2184 /* PR 18854: A binary might contain more than one symbol table.
2185 Unusual, but possible. Warn, but continue. */
2186 if (elf_onesymtab (abfd) != 0)
2189 /* xgettext:c-format */
2190 (_("%pB: warning: multiple symbol tables detected"
2191 " - ignoring the table in section %u"),
2195 elf_onesymtab (abfd) = shindex;
2196 elf_symtab_hdr (abfd) = *hdr;
2197 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2198 abfd->flags |= HAS_SYMS;
2200 /* Sometimes a shared object will map in the symbol table. If
2201 SHF_ALLOC is set, and this is a shared object, then we also
2202 treat this section as a BFD section. We can not base the
2203 decision purely on SHF_ALLOC, because that flag is sometimes
2204 set in a relocatable object file, which would confuse the
2206 if ((hdr->sh_flags & SHF_ALLOC) != 0
2207 && (abfd->flags & DYNAMIC) != 0
2208 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2212 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2213 can't read symbols without that section loaded as well. It
2214 is most likely specified by the next section header. */
2216 elf_section_list * entry;
2217 unsigned int i, num_sec;
2219 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2220 if (entry->hdr.sh_link == shindex)
2223 num_sec = elf_numsections (abfd);
2224 for (i = shindex + 1; i < num_sec; i++)
2226 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2228 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2229 && hdr2->sh_link == shindex)
2234 for (i = 1; i < shindex; i++)
2236 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2238 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2239 && hdr2->sh_link == shindex)
2244 ret = bfd_section_from_shdr (abfd, i);
2245 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2249 case SHT_DYNSYM: /* A dynamic symbol table. */
2250 if (elf_dynsymtab (abfd) == shindex)
2253 if (hdr->sh_entsize != bed->s->sizeof_sym)
2256 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2258 if (hdr->sh_size != 0)
2261 /* Some linkers erroneously set sh_info to one with a
2262 zero sh_size. ld sees this as a global symbol count
2263 of (unsigned) -1. Fix it here. */
2268 /* PR 18854: A binary might contain more than one dynamic symbol table.
2269 Unusual, but possible. Warn, but continue. */
2270 if (elf_dynsymtab (abfd) != 0)
2273 /* xgettext:c-format */
2274 (_("%pB: warning: multiple dynamic symbol tables detected"
2275 " - ignoring the table in section %u"),
2279 elf_dynsymtab (abfd) = shindex;
2280 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2281 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2282 abfd->flags |= HAS_SYMS;
2284 /* Besides being a symbol table, we also treat this as a regular
2285 section, so that objcopy can handle it. */
2286 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2289 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2291 elf_section_list * entry;
2293 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2294 if (entry->ndx == shindex)
2297 entry = bfd_alloc (abfd, sizeof (*entry));
2300 entry->ndx = shindex;
2302 entry->next = elf_symtab_shndx_list (abfd);
2303 elf_symtab_shndx_list (abfd) = entry;
2304 elf_elfsections (abfd)[shindex] = & entry->hdr;
2308 case SHT_STRTAB: /* A string table. */
2309 if (hdr->bfd_section != NULL)
2312 if (ehdr->e_shstrndx == shindex)
2314 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2315 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2319 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2322 elf_tdata (abfd)->strtab_hdr = *hdr;
2323 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2327 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2330 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2331 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2332 elf_elfsections (abfd)[shindex] = hdr;
2333 /* We also treat this as a regular section, so that objcopy
2335 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2340 /* If the string table isn't one of the above, then treat it as a
2341 regular section. We need to scan all the headers to be sure,
2342 just in case this strtab section appeared before the above. */
2343 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2345 unsigned int i, num_sec;
2347 num_sec = elf_numsections (abfd);
2348 for (i = 1; i < num_sec; i++)
2350 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2351 if (hdr2->sh_link == shindex)
2353 /* Prevent endless recursion on broken objects. */
2356 if (! bfd_section_from_shdr (abfd, i))
2358 if (elf_onesymtab (abfd) == i)
2360 if (elf_dynsymtab (abfd) == i)
2361 goto dynsymtab_strtab;
2365 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2370 /* *These* do a lot of work -- but build no sections! */
2372 asection *target_sect;
2373 Elf_Internal_Shdr *hdr2, **p_hdr;
2374 unsigned int num_sec = elf_numsections (abfd);
2375 struct bfd_elf_section_data *esdt;
2378 != (bfd_size_type) (hdr->sh_type == SHT_REL
2379 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2382 /* Check for a bogus link to avoid crashing. */
2383 if (hdr->sh_link >= num_sec)
2386 /* xgettext:c-format */
2387 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2388 abfd, hdr->sh_link, name, shindex);
2389 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2394 /* For some incomprehensible reason Oracle distributes
2395 libraries for Solaris in which some of the objects have
2396 bogus sh_link fields. It would be nice if we could just
2397 reject them, but, unfortunately, some people need to use
2398 them. We scan through the section headers; if we find only
2399 one suitable symbol table, we clobber the sh_link to point
2400 to it. I hope this doesn't break anything.
2402 Don't do it on executable nor shared library. */
2403 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2404 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2405 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2411 for (scan = 1; scan < num_sec; scan++)
2413 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2414 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2425 hdr->sh_link = found;
2428 /* Get the symbol table. */
2429 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2430 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2431 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2434 /* If this is an alloc section in an executable or shared
2435 library, or the reloc section does not use the main symbol
2436 table we don't treat it as a reloc section. BFD can't
2437 adequately represent such a section, so at least for now,
2438 we don't try. We just present it as a normal section. We
2439 also can't use it as a reloc section if it points to the
2440 null section, an invalid section, another reloc section, or
2441 its sh_link points to the null section. */
2442 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2443 && (hdr->sh_flags & SHF_ALLOC) != 0)
2444 || hdr->sh_link == SHN_UNDEF
2445 || hdr->sh_link != elf_onesymtab (abfd)
2446 || hdr->sh_info == SHN_UNDEF
2447 || hdr->sh_info >= num_sec
2448 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2449 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2451 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2456 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2459 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2460 if (target_sect == NULL)
2463 esdt = elf_section_data (target_sect);
2464 if (hdr->sh_type == SHT_RELA)
2465 p_hdr = &esdt->rela.hdr;
2467 p_hdr = &esdt->rel.hdr;
2469 /* PR 17512: file: 0b4f81b7.
2470 Also see PR 24456, for a file which deliberately has two reloc
2475 /* xgettext:c-format */
2476 (_("%pB: warning: multiple relocation sections for section %pA \
2477 found - ignoring all but the first"),
2481 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2486 elf_elfsections (abfd)[shindex] = hdr2;
2487 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2488 * bed->s->int_rels_per_ext_rel);
2489 target_sect->flags |= SEC_RELOC;
2490 target_sect->relocation = NULL;
2491 target_sect->rel_filepos = hdr->sh_offset;
2492 /* In the section to which the relocations apply, mark whether
2493 its relocations are of the REL or RELA variety. */
2494 if (hdr->sh_size != 0)
2496 if (hdr->sh_type == SHT_RELA)
2497 target_sect->use_rela_p = 1;
2499 abfd->flags |= HAS_RELOC;
2503 case SHT_GNU_verdef:
2504 elf_dynverdef (abfd) = shindex;
2505 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2506 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2509 case SHT_GNU_versym:
2510 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2513 elf_dynversym (abfd) = shindex;
2514 elf_tdata (abfd)->dynversym_hdr = *hdr;
2515 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2518 case SHT_GNU_verneed:
2519 elf_dynverref (abfd) = shindex;
2520 elf_tdata (abfd)->dynverref_hdr = *hdr;
2521 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2528 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2531 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2537 /* Possibly an attributes section. */
2538 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2539 || hdr->sh_type == bed->obj_attrs_section_type)
2541 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2543 _bfd_elf_parse_attributes (abfd, hdr);
2547 /* Check for any processor-specific section types. */
2548 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2551 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2553 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2554 /* FIXME: How to properly handle allocated section reserved
2555 for applications? */
2557 /* xgettext:c-format */
2558 (_("%pB: unknown type [%#x] section `%s'"),
2559 abfd, hdr->sh_type, name);
2562 /* Allow sections reserved for applications. */
2563 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2568 else if (hdr->sh_type >= SHT_LOPROC
2569 && hdr->sh_type <= SHT_HIPROC)
2570 /* FIXME: We should handle this section. */
2572 /* xgettext:c-format */
2573 (_("%pB: unknown type [%#x] section `%s'"),
2574 abfd, hdr->sh_type, name);
2575 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2577 /* Unrecognised OS-specific sections. */
2578 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2579 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2580 required to correctly process the section and the file should
2581 be rejected with an error message. */
2583 /* xgettext:c-format */
2584 (_("%pB: unknown type [%#x] section `%s'"),
2585 abfd, hdr->sh_type, name);
2588 /* Otherwise it should be processed. */
2589 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2594 /* FIXME: We should handle this section. */
2596 /* xgettext:c-format */
2597 (_("%pB: unknown type [%#x] section `%s'"),
2598 abfd, hdr->sh_type, name);
2606 if (sections_being_created && sections_being_created_abfd == abfd)
2607 sections_being_created [shindex] = FALSE;
2608 if (-- nesting == 0)
2610 sections_being_created = NULL;
2611 sections_being_created_abfd = abfd;
2616 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2619 bfd_sym_from_r_symndx (struct sym_cache *cache,
2621 unsigned long r_symndx)
2623 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2625 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2627 Elf_Internal_Shdr *symtab_hdr;
2628 unsigned char esym[sizeof (Elf64_External_Sym)];
2629 Elf_External_Sym_Shndx eshndx;
2631 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2632 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2633 &cache->sym[ent], esym, &eshndx) == NULL)
2636 if (cache->abfd != abfd)
2638 memset (cache->indx, -1, sizeof (cache->indx));
2641 cache->indx[ent] = r_symndx;
2644 return &cache->sym[ent];
2647 /* Given an ELF section number, retrieve the corresponding BFD
2651 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2653 if (sec_index >= elf_numsections (abfd))
2655 return elf_elfsections (abfd)[sec_index]->bfd_section;
2658 static const struct bfd_elf_special_section special_sections_b[] =
2660 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2661 { NULL, 0, 0, 0, 0 }
2664 static const struct bfd_elf_special_section special_sections_c[] =
2666 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2667 { NULL, 0, 0, 0, 0 }
2670 static const struct bfd_elf_special_section special_sections_d[] =
2672 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2673 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2674 /* There are more DWARF sections than these, but they needn't be added here
2675 unless you have to cope with broken compilers that don't emit section
2676 attributes or you want to help the user writing assembler. */
2677 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2678 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2679 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2680 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2681 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2682 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2683 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2684 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2685 { NULL, 0, 0, 0, 0 }
2688 static const struct bfd_elf_special_section special_sections_f[] =
2690 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2691 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2692 { NULL, 0 , 0, 0, 0 }
2695 static const struct bfd_elf_special_section special_sections_g[] =
2697 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2698 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2699 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2700 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2701 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2702 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2703 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2704 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2705 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2706 { NULL, 0, 0, 0, 0 }
2709 static const struct bfd_elf_special_section special_sections_h[] =
2711 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2712 { NULL, 0, 0, 0, 0 }
2715 static const struct bfd_elf_special_section special_sections_i[] =
2717 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2718 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2719 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2720 { NULL, 0, 0, 0, 0 }
2723 static const struct bfd_elf_special_section special_sections_l[] =
2725 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2726 { NULL, 0, 0, 0, 0 }
2729 static const struct bfd_elf_special_section special_sections_n[] =
2731 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2732 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2733 { NULL, 0, 0, 0, 0 }
2736 static const struct bfd_elf_special_section special_sections_p[] =
2738 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2739 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2740 { NULL, 0, 0, 0, 0 }
2743 static const struct bfd_elf_special_section special_sections_r[] =
2745 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2746 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2747 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2748 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2749 { NULL, 0, 0, 0, 0 }
2752 static const struct bfd_elf_special_section special_sections_s[] =
2754 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2755 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2756 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2757 /* See struct bfd_elf_special_section declaration for the semantics of
2758 this special case where .prefix_length != strlen (.prefix). */
2759 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2760 { NULL, 0, 0, 0, 0 }
2763 static const struct bfd_elf_special_section special_sections_t[] =
2765 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2766 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2767 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2768 { NULL, 0, 0, 0, 0 }
2771 static const struct bfd_elf_special_section special_sections_z[] =
2773 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2774 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2775 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2776 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2777 { NULL, 0, 0, 0, 0 }
2780 static const struct bfd_elf_special_section * const special_sections[] =
2782 special_sections_b, /* 'b' */
2783 special_sections_c, /* 'c' */
2784 special_sections_d, /* 'd' */
2786 special_sections_f, /* 'f' */
2787 special_sections_g, /* 'g' */
2788 special_sections_h, /* 'h' */
2789 special_sections_i, /* 'i' */
2792 special_sections_l, /* 'l' */
2794 special_sections_n, /* 'n' */
2796 special_sections_p, /* 'p' */
2798 special_sections_r, /* 'r' */
2799 special_sections_s, /* 's' */
2800 special_sections_t, /* 't' */
2806 special_sections_z /* 'z' */
2809 const struct bfd_elf_special_section *
2810 _bfd_elf_get_special_section (const char *name,
2811 const struct bfd_elf_special_section *spec,
2817 len = strlen (name);
2819 for (i = 0; spec[i].prefix != NULL; i++)
2822 int prefix_len = spec[i].prefix_length;
2824 if (len < prefix_len)
2826 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2829 suffix_len = spec[i].suffix_length;
2830 if (suffix_len <= 0)
2832 if (name[prefix_len] != 0)
2834 if (suffix_len == 0)
2836 if (name[prefix_len] != '.'
2837 && (suffix_len == -2
2838 || (rela && spec[i].type == SHT_REL)))
2844 if (len < prefix_len + suffix_len)
2846 if (memcmp (name + len - suffix_len,
2847 spec[i].prefix + prefix_len,
2857 const struct bfd_elf_special_section *
2858 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2861 const struct bfd_elf_special_section *spec;
2862 const struct elf_backend_data *bed;
2864 /* See if this is one of the special sections. */
2865 if (sec->name == NULL)
2868 bed = get_elf_backend_data (abfd);
2869 spec = bed->special_sections;
2872 spec = _bfd_elf_get_special_section (sec->name,
2873 bed->special_sections,
2879 if (sec->name[0] != '.')
2882 i = sec->name[1] - 'b';
2883 if (i < 0 || i > 'z' - 'b')
2886 spec = special_sections[i];
2891 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2895 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2897 struct bfd_elf_section_data *sdata;
2898 const struct elf_backend_data *bed;
2899 const struct bfd_elf_special_section *ssect;
2901 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2904 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2908 sec->used_by_bfd = sdata;
2911 /* Indicate whether or not this section should use RELA relocations. */
2912 bed = get_elf_backend_data (abfd);
2913 sec->use_rela_p = bed->default_use_rela_p;
2915 /* When we read a file, we don't need to set ELF section type and
2916 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2917 anyway. We will set ELF section type and flags for all linker
2918 created sections. If user specifies BFD section flags, we will
2919 set ELF section type and flags based on BFD section flags in
2920 elf_fake_sections. Special handling for .init_array/.fini_array
2921 output sections since they may contain .ctors/.dtors input
2922 sections. We don't want _bfd_elf_init_private_section_data to
2923 copy ELF section type from .ctors/.dtors input sections. */
2924 if (abfd->direction != read_direction
2925 || (sec->flags & SEC_LINKER_CREATED) != 0)
2927 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2930 || (sec->flags & SEC_LINKER_CREATED) != 0
2931 || ssect->type == SHT_INIT_ARRAY
2932 || ssect->type == SHT_FINI_ARRAY))
2934 elf_section_type (sec) = ssect->type;
2935 elf_section_flags (sec) = ssect->attr;
2939 return _bfd_generic_new_section_hook (abfd, sec);
2942 /* Create a new bfd section from an ELF program header.
2944 Since program segments have no names, we generate a synthetic name
2945 of the form segment<NUM>, where NUM is generally the index in the
2946 program header table. For segments that are split (see below) we
2947 generate the names segment<NUM>a and segment<NUM>b.
2949 Note that some program segments may have a file size that is different than
2950 (less than) the memory size. All this means is that at execution the
2951 system must allocate the amount of memory specified by the memory size,
2952 but only initialize it with the first "file size" bytes read from the
2953 file. This would occur for example, with program segments consisting
2954 of combined data+bss.
2956 To handle the above situation, this routine generates TWO bfd sections
2957 for the single program segment. The first has the length specified by
2958 the file size of the segment, and the second has the length specified
2959 by the difference between the two sizes. In effect, the segment is split
2960 into its initialized and uninitialized parts.
2965 _bfd_elf_make_section_from_phdr (bfd *abfd,
2966 Elf_Internal_Phdr *hdr,
2968 const char *type_name)
2976 split = ((hdr->p_memsz > 0)
2977 && (hdr->p_filesz > 0)
2978 && (hdr->p_memsz > hdr->p_filesz));
2980 if (hdr->p_filesz > 0)
2982 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2983 len = strlen (namebuf) + 1;
2984 name = (char *) bfd_alloc (abfd, len);
2987 memcpy (name, namebuf, len);
2988 newsect = bfd_make_section (abfd, name);
2989 if (newsect == NULL)
2991 newsect->vma = hdr->p_vaddr;
2992 newsect->lma = hdr->p_paddr;
2993 newsect->size = hdr->p_filesz;
2994 newsect->filepos = hdr->p_offset;
2995 newsect->flags |= SEC_HAS_CONTENTS;
2996 newsect->alignment_power = bfd_log2 (hdr->p_align);
2997 if (hdr->p_type == PT_LOAD)
2999 newsect->flags |= SEC_ALLOC;
3000 newsect->flags |= SEC_LOAD;
3001 if (hdr->p_flags & PF_X)
3003 /* FIXME: all we known is that it has execute PERMISSION,
3005 newsect->flags |= SEC_CODE;
3008 if (!(hdr->p_flags & PF_W))
3010 newsect->flags |= SEC_READONLY;
3014 if (hdr->p_memsz > hdr->p_filesz)
3018 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3019 len = strlen (namebuf) + 1;
3020 name = (char *) bfd_alloc (abfd, len);
3023 memcpy (name, namebuf, len);
3024 newsect = bfd_make_section (abfd, name);
3025 if (newsect == NULL)
3027 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
3028 newsect->lma = hdr->p_paddr + hdr->p_filesz;
3029 newsect->size = hdr->p_memsz - hdr->p_filesz;
3030 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3031 align = newsect->vma & -newsect->vma;
3032 if (align == 0 || align > hdr->p_align)
3033 align = hdr->p_align;
3034 newsect->alignment_power = bfd_log2 (align);
3035 if (hdr->p_type == PT_LOAD)
3037 /* Hack for gdb. Segments that have not been modified do
3038 not have their contents written to a core file, on the
3039 assumption that a debugger can find the contents in the
3040 executable. We flag this case by setting the fake
3041 section size to zero. Note that "real" bss sections will
3042 always have their contents dumped to the core file. */
3043 if (bfd_get_format (abfd) == bfd_core)
3045 newsect->flags |= SEC_ALLOC;
3046 if (hdr->p_flags & PF_X)
3047 newsect->flags |= SEC_CODE;
3049 if (!(hdr->p_flags & PF_W))
3050 newsect->flags |= SEC_READONLY;
3057 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3059 const struct elf_backend_data *bed;
3061 switch (hdr->p_type)
3064 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3067 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3076 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3078 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3084 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3087 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3089 case PT_GNU_EH_FRAME:
3090 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3094 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3097 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3100 /* Check for any processor-specific program segment types. */
3101 bed = get_elf_backend_data (abfd);
3102 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3106 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3110 _bfd_elf_single_rel_hdr (asection *sec)
3112 if (elf_section_data (sec)->rel.hdr)
3114 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3115 return elf_section_data (sec)->rel.hdr;
3118 return elf_section_data (sec)->rela.hdr;
3122 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3123 Elf_Internal_Shdr *rel_hdr,
3124 const char *sec_name,
3125 bfd_boolean use_rela_p)
3127 char *name = (char *) bfd_alloc (abfd,
3128 sizeof ".rela" + strlen (sec_name));
3132 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3134 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3136 if (rel_hdr->sh_name == (unsigned int) -1)
3142 /* Allocate and initialize a section-header for a new reloc section,
3143 containing relocations against ASECT. It is stored in RELDATA. If
3144 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3148 _bfd_elf_init_reloc_shdr (bfd *abfd,
3149 struct bfd_elf_section_reloc_data *reldata,
3150 const char *sec_name,
3151 bfd_boolean use_rela_p,
3152 bfd_boolean delay_st_name_p)
3154 Elf_Internal_Shdr *rel_hdr;
3155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3157 BFD_ASSERT (reldata->hdr == NULL);
3158 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3159 reldata->hdr = rel_hdr;
3161 if (delay_st_name_p)
3162 rel_hdr->sh_name = (unsigned int) -1;
3163 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3166 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3167 rel_hdr->sh_entsize = (use_rela_p
3168 ? bed->s->sizeof_rela
3169 : bed->s->sizeof_rel);
3170 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3171 rel_hdr->sh_flags = 0;
3172 rel_hdr->sh_addr = 0;
3173 rel_hdr->sh_size = 0;
3174 rel_hdr->sh_offset = 0;
3179 /* Return the default section type based on the passed in section flags. */
3182 bfd_elf_get_default_section_type (flagword flags)
3184 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3185 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3187 return SHT_PROGBITS;
3190 struct fake_section_arg
3192 struct bfd_link_info *link_info;
3196 /* Set up an ELF internal section header for a section. */
3199 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3201 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3203 struct bfd_elf_section_data *esd = elf_section_data (asect);
3204 Elf_Internal_Shdr *this_hdr;
3205 unsigned int sh_type;
3206 const char *name = asect->name;
3207 bfd_boolean delay_st_name_p = FALSE;
3211 /* We already failed; just get out of the bfd_map_over_sections
3216 this_hdr = &esd->this_hdr;
3220 /* ld: compress DWARF debug sections with names: .debug_*. */
3221 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3222 && (asect->flags & SEC_DEBUGGING)
3226 /* Set SEC_ELF_COMPRESS to indicate this section should be
3228 asect->flags |= SEC_ELF_COMPRESS;
3230 /* If this section will be compressed, delay adding section
3231 name to section name section after it is compressed in
3232 _bfd_elf_assign_file_positions_for_non_load. */
3233 delay_st_name_p = TRUE;
3236 else if ((asect->flags & SEC_ELF_RENAME))
3238 /* objcopy: rename output DWARF debug section. */
3239 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3241 /* When we decompress or compress with SHF_COMPRESSED,
3242 convert section name from .zdebug_* to .debug_* if
3246 char *new_name = convert_zdebug_to_debug (abfd, name);
3247 if (new_name == NULL)
3255 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3257 /* PR binutils/18087: Compression does not always make a
3258 section smaller. So only rename the section when
3259 compression has actually taken place. If input section
3260 name is .zdebug_*, we should never compress it again. */
3261 char *new_name = convert_debug_to_zdebug (abfd, name);
3262 if (new_name == NULL)
3267 BFD_ASSERT (name[1] != 'z');
3272 if (delay_st_name_p)
3273 this_hdr->sh_name = (unsigned int) -1;
3277 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3279 if (this_hdr->sh_name == (unsigned int) -1)
3286 /* Don't clear sh_flags. Assembler may set additional bits. */
3288 if ((asect->flags & SEC_ALLOC) != 0
3289 || asect->user_set_vma)
3290 this_hdr->sh_addr = asect->vma;
3292 this_hdr->sh_addr = 0;
3294 this_hdr->sh_offset = 0;
3295 this_hdr->sh_size = asect->size;
3296 this_hdr->sh_link = 0;
3297 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3298 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3301 /* xgettext:c-format */
3302 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3303 abfd, asect->alignment_power, asect);
3307 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
3308 /* The sh_entsize and sh_info fields may have been set already by
3309 copy_private_section_data. */
3311 this_hdr->bfd_section = asect;
3312 this_hdr->contents = NULL;
3314 /* If the section type is unspecified, we set it based on
3316 if ((asect->flags & SEC_GROUP) != 0)
3317 sh_type = SHT_GROUP;
3319 sh_type = bfd_elf_get_default_section_type (asect->flags);
3321 if (this_hdr->sh_type == SHT_NULL)
3322 this_hdr->sh_type = sh_type;
3323 else if (this_hdr->sh_type == SHT_NOBITS
3324 && sh_type == SHT_PROGBITS
3325 && (asect->flags & SEC_ALLOC) != 0)
3327 /* Warn if we are changing a NOBITS section to PROGBITS, but
3328 allow the link to proceed. This can happen when users link
3329 non-bss input sections to bss output sections, or emit data
3330 to a bss output section via a linker script. */
3332 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3333 this_hdr->sh_type = sh_type;
3336 switch (this_hdr->sh_type)
3347 case SHT_INIT_ARRAY:
3348 case SHT_FINI_ARRAY:
3349 case SHT_PREINIT_ARRAY:
3350 this_hdr->sh_entsize = bed->s->arch_size / 8;
3354 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3358 this_hdr->sh_entsize = bed->s->sizeof_sym;
3362 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3366 if (get_elf_backend_data (abfd)->may_use_rela_p)
3367 this_hdr->sh_entsize = bed->s->sizeof_rela;
3371 if (get_elf_backend_data (abfd)->may_use_rel_p)
3372 this_hdr->sh_entsize = bed->s->sizeof_rel;
3375 case SHT_GNU_versym:
3376 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3379 case SHT_GNU_verdef:
3380 this_hdr->sh_entsize = 0;
3381 /* objcopy or strip will copy over sh_info, but may not set
3382 cverdefs. The linker will set cverdefs, but sh_info will be
3384 if (this_hdr->sh_info == 0)
3385 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3387 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3388 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3391 case SHT_GNU_verneed:
3392 this_hdr->sh_entsize = 0;
3393 /* objcopy or strip will copy over sh_info, but may not set
3394 cverrefs. The linker will set cverrefs, but sh_info will be
3396 if (this_hdr->sh_info == 0)
3397 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3399 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3400 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3404 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3408 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3412 if ((asect->flags & SEC_ALLOC) != 0)
3413 this_hdr->sh_flags |= SHF_ALLOC;
3414 if ((asect->flags & SEC_READONLY) == 0)
3415 this_hdr->sh_flags |= SHF_WRITE;
3416 if ((asect->flags & SEC_CODE) != 0)
3417 this_hdr->sh_flags |= SHF_EXECINSTR;
3418 if ((asect->flags & SEC_MERGE) != 0)
3420 this_hdr->sh_flags |= SHF_MERGE;
3421 this_hdr->sh_entsize = asect->entsize;
3423 if ((asect->flags & SEC_STRINGS) != 0)
3424 this_hdr->sh_flags |= SHF_STRINGS;
3425 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3426 this_hdr->sh_flags |= SHF_GROUP;
3427 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3429 this_hdr->sh_flags |= SHF_TLS;
3430 if (asect->size == 0
3431 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3433 struct bfd_link_order *o = asect->map_tail.link_order;
3435 this_hdr->sh_size = 0;
3438 this_hdr->sh_size = o->offset + o->size;
3439 if (this_hdr->sh_size != 0)
3440 this_hdr->sh_type = SHT_NOBITS;
3444 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3445 this_hdr->sh_flags |= SHF_EXCLUDE;
3447 /* If the section has relocs, set up a section header for the
3448 SHT_REL[A] section. If two relocation sections are required for
3449 this section, it is up to the processor-specific back-end to
3450 create the other. */
3451 if ((asect->flags & SEC_RELOC) != 0)
3453 /* When doing a relocatable link, create both REL and RELA sections if
3456 /* Do the normal setup if we wouldn't create any sections here. */
3457 && esd->rel.count + esd->rela.count > 0
3458 && (bfd_link_relocatable (arg->link_info)
3459 || arg->link_info->emitrelocations))
3461 if (esd->rel.count && esd->rel.hdr == NULL
3462 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3463 FALSE, delay_st_name_p))
3468 if (esd->rela.count && esd->rela.hdr == NULL
3469 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3470 TRUE, delay_st_name_p))
3476 else if (!_bfd_elf_init_reloc_shdr (abfd,
3478 ? &esd->rela : &esd->rel),
3488 /* Check for processor-specific section types. */
3489 sh_type = this_hdr->sh_type;
3490 if (bed->elf_backend_fake_sections
3491 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3497 if (sh_type == SHT_NOBITS && asect->size != 0)
3499 /* Don't change the header type from NOBITS if we are being
3500 called for objcopy --only-keep-debug. */
3501 this_hdr->sh_type = sh_type;
3505 /* Fill in the contents of a SHT_GROUP section. Called from
3506 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3507 when ELF targets use the generic linker, ld. Called for ld -r
3508 from bfd_elf_final_link. */
3511 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3513 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3514 asection *elt, *first;
3518 /* Ignore linker created group section. See elfNN_ia64_object_p in
3520 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3525 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3527 unsigned long symindx = 0;
3529 /* elf_group_id will have been set up by objcopy and the
3531 if (elf_group_id (sec) != NULL)
3532 symindx = elf_group_id (sec)->udata.i;
3536 /* If called from the assembler, swap_out_syms will have set up
3537 elf_section_syms. */
3538 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3539 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3541 elf_section_data (sec)->this_hdr.sh_info = symindx;
3543 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3545 /* The ELF backend linker sets sh_info to -2 when the group
3546 signature symbol is global, and thus the index can't be
3547 set until all local symbols are output. */
3549 struct bfd_elf_section_data *sec_data;
3550 unsigned long symndx;
3551 unsigned long extsymoff;
3552 struct elf_link_hash_entry *h;
3554 /* The point of this little dance to the first SHF_GROUP section
3555 then back to the SHT_GROUP section is that this gets us to
3556 the SHT_GROUP in the input object. */
3557 igroup = elf_sec_group (elf_next_in_group (sec));
3558 sec_data = elf_section_data (igroup);
3559 symndx = sec_data->this_hdr.sh_info;
3561 if (!elf_bad_symtab (igroup->owner))
3563 Elf_Internal_Shdr *symtab_hdr;
3565 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3566 extsymoff = symtab_hdr->sh_info;
3568 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3569 while (h->root.type == bfd_link_hash_indirect
3570 || h->root.type == bfd_link_hash_warning)
3571 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3573 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3576 /* The contents won't be allocated for "ld -r" or objcopy. */
3578 if (sec->contents == NULL)
3581 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3583 /* Arrange for the section to be written out. */
3584 elf_section_data (sec)->this_hdr.contents = sec->contents;
3585 if (sec->contents == NULL)
3592 loc = sec->contents + sec->size;
3594 /* Get the pointer to the first section in the group that gas
3595 squirreled away here. objcopy arranges for this to be set to the
3596 start of the input section group. */
3597 first = elt = elf_next_in_group (sec);
3599 /* First element is a flag word. Rest of section is elf section
3600 indices for all the sections of the group. Write them backwards
3601 just to keep the group in the same order as given in .section
3602 directives, not that it matters. */
3609 s = s->output_section;
3611 && !bfd_is_abs_section (s))
3613 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3614 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3616 if (elf_sec->rel.hdr != NULL
3618 || (input_elf_sec->rel.hdr != NULL
3619 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3621 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3623 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3625 if (elf_sec->rela.hdr != NULL
3627 || (input_elf_sec->rela.hdr != NULL
3628 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3630 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3632 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3635 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3637 elt = elf_next_in_group (elt);
3643 BFD_ASSERT (loc == sec->contents);
3645 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3648 /* Given NAME, the name of a relocation section stripped of its
3649 .rel/.rela prefix, return the section in ABFD to which the
3650 relocations apply. */
3653 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3655 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3656 section likely apply to .got.plt or .got section. */
3657 if (get_elf_backend_data (abfd)->want_got_plt
3658 && strcmp (name, ".plt") == 0)
3663 sec = bfd_get_section_by_name (abfd, name);
3669 return bfd_get_section_by_name (abfd, name);
3672 /* Return the section to which RELOC_SEC applies. */
3675 elf_get_reloc_section (asection *reloc_sec)
3680 const struct elf_backend_data *bed;
3682 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3683 if (type != SHT_REL && type != SHT_RELA)
3686 /* We look up the section the relocs apply to by name. */
3687 name = reloc_sec->name;
3688 if (strncmp (name, ".rel", 4) != 0)
3691 if (type == SHT_RELA && *name++ != 'a')
3694 abfd = reloc_sec->owner;
3695 bed = get_elf_backend_data (abfd);
3696 return bed->get_reloc_section (abfd, name);
3699 /* Assign all ELF section numbers. The dummy first section is handled here
3700 too. The link/info pointers for the standard section types are filled
3701 in here too, while we're at it. */
3704 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3706 struct elf_obj_tdata *t = elf_tdata (abfd);
3708 unsigned int section_number;
3709 Elf_Internal_Shdr **i_shdrp;
3710 struct bfd_elf_section_data *d;
3711 bfd_boolean need_symtab;
3715 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3717 /* SHT_GROUP sections are in relocatable files only. */
3718 if (link_info == NULL || !link_info->resolve_section_groups)
3720 size_t reloc_count = 0;
3722 /* Put SHT_GROUP sections first. */
3723 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3725 d = elf_section_data (sec);
3727 if (d->this_hdr.sh_type == SHT_GROUP)
3729 if (sec->flags & SEC_LINKER_CREATED)
3731 /* Remove the linker created SHT_GROUP sections. */
3732 bfd_section_list_remove (abfd, sec);
3733 abfd->section_count--;
3736 d->this_idx = section_number++;
3739 /* Count relocations. */
3740 reloc_count += sec->reloc_count;
3743 /* Clear HAS_RELOC if there are no relocations. */
3744 if (reloc_count == 0)
3745 abfd->flags &= ~HAS_RELOC;
3748 for (sec = abfd->sections; sec; sec = sec->next)
3750 d = elf_section_data (sec);
3752 if (d->this_hdr.sh_type != SHT_GROUP)
3753 d->this_idx = section_number++;
3754 if (d->this_hdr.sh_name != (unsigned int) -1)
3755 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3758 d->rel.idx = section_number++;
3759 if (d->rel.hdr->sh_name != (unsigned int) -1)
3760 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3767 d->rela.idx = section_number++;
3768 if (d->rela.hdr->sh_name != (unsigned int) -1)
3769 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3775 need_symtab = (bfd_get_symcount (abfd) > 0
3776 || (link_info == NULL
3777 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3781 elf_onesymtab (abfd) = section_number++;
3782 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3783 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3785 elf_section_list *entry;
3787 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3789 entry = bfd_zalloc (abfd, sizeof (*entry));
3790 entry->ndx = section_number++;
3791 elf_symtab_shndx_list (abfd) = entry;
3793 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3794 ".symtab_shndx", FALSE);
3795 if (entry->hdr.sh_name == (unsigned int) -1)
3798 elf_strtab_sec (abfd) = section_number++;
3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3802 elf_shstrtab_sec (abfd) = section_number++;
3803 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3804 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3806 if (section_number >= SHN_LORESERVE)
3808 /* xgettext:c-format */
3809 _bfd_error_handler (_("%pB: too many sections: %u"),
3810 abfd, section_number);
3814 elf_numsections (abfd) = section_number;
3815 elf_elfheader (abfd)->e_shnum = section_number;
3817 /* Set up the list of section header pointers, in agreement with the
3819 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3820 sizeof (Elf_Internal_Shdr *));
3821 if (i_shdrp == NULL)
3824 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3825 sizeof (Elf_Internal_Shdr));
3826 if (i_shdrp[0] == NULL)
3828 bfd_release (abfd, i_shdrp);
3832 elf_elfsections (abfd) = i_shdrp;
3834 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3837 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3838 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3840 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3841 BFD_ASSERT (entry != NULL);
3842 i_shdrp[entry->ndx] = & entry->hdr;
3843 entry->hdr.sh_link = elf_onesymtab (abfd);
3845 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3846 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3849 for (sec = abfd->sections; sec; sec = sec->next)
3853 d = elf_section_data (sec);
3855 i_shdrp[d->this_idx] = &d->this_hdr;
3856 if (d->rel.idx != 0)
3857 i_shdrp[d->rel.idx] = d->rel.hdr;
3858 if (d->rela.idx != 0)
3859 i_shdrp[d->rela.idx] = d->rela.hdr;
3861 /* Fill in the sh_link and sh_info fields while we're at it. */
3863 /* sh_link of a reloc section is the section index of the symbol
3864 table. sh_info is the section index of the section to which
3865 the relocation entries apply. */
3866 if (d->rel.idx != 0)
3868 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3869 d->rel.hdr->sh_info = d->this_idx;
3870 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3872 if (d->rela.idx != 0)
3874 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3875 d->rela.hdr->sh_info = d->this_idx;
3876 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3879 /* We need to set up sh_link for SHF_LINK_ORDER. */
3880 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3882 s = elf_linked_to_section (sec);
3885 /* elf_linked_to_section points to the input section. */
3886 if (link_info != NULL)
3888 /* Check discarded linkonce section. */
3889 if (discarded_section (s))
3893 /* xgettext:c-format */
3894 (_("%pB: sh_link of section `%pA' points to"
3895 " discarded section `%pA' of `%pB'"),
3896 abfd, d->this_hdr.bfd_section,
3898 /* Point to the kept section if it has the same
3899 size as the discarded one. */
3900 kept = _bfd_elf_check_kept_section (s, link_info);
3903 bfd_set_error (bfd_error_bad_value);
3909 s = s->output_section;
3910 BFD_ASSERT (s != NULL);
3914 /* Handle objcopy. */
3915 if (s->output_section == NULL)
3918 /* xgettext:c-format */
3919 (_("%pB: sh_link of section `%pA' points to"
3920 " removed section `%pA' of `%pB'"),
3921 abfd, d->this_hdr.bfd_section, s, s->owner);
3922 bfd_set_error (bfd_error_bad_value);
3925 s = s->output_section;
3927 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3932 The Intel C compiler generates SHT_IA_64_UNWIND with
3933 SHF_LINK_ORDER. But it doesn't set the sh_link or
3934 sh_info fields. Hence we could get the situation
3936 const struct elf_backend_data *bed
3937 = get_elf_backend_data (abfd);
3938 if (bed->link_order_error_handler)
3939 bed->link_order_error_handler
3940 /* xgettext:c-format */
3941 (_("%pB: warning: sh_link not set for section `%pA'"),
3946 switch (d->this_hdr.sh_type)
3950 /* A reloc section which we are treating as a normal BFD
3951 section. sh_link is the section index of the symbol
3952 table. sh_info is the section index of the section to
3953 which the relocation entries apply. We assume that an
3954 allocated reloc section uses the dynamic symbol table.
3955 FIXME: How can we be sure? */
3956 s = bfd_get_section_by_name (abfd, ".dynsym");
3958 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3960 s = elf_get_reloc_section (sec);
3963 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3964 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3969 /* We assume that a section named .stab*str is a stabs
3970 string section. We look for a section with the same name
3971 but without the trailing ``str'', and set its sh_link
3972 field to point to this section. */
3973 if (CONST_STRNEQ (sec->name, ".stab")
3974 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3979 len = strlen (sec->name);
3980 alc = (char *) bfd_malloc (len - 2);
3983 memcpy (alc, sec->name, len - 3);
3984 alc[len - 3] = '\0';
3985 s = bfd_get_section_by_name (abfd, alc);
3989 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3991 /* This is a .stab section. */
3992 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3993 elf_section_data (s)->this_hdr.sh_entsize
3994 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
4001 case SHT_GNU_verneed:
4002 case SHT_GNU_verdef:
4003 /* sh_link is the section header index of the string table
4004 used for the dynamic entries, or the symbol table, or the
4006 s = bfd_get_section_by_name (abfd, ".dynstr");
4008 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4011 case SHT_GNU_LIBLIST:
4012 /* sh_link is the section header index of the prelink library
4013 list used for the dynamic entries, or the symbol table, or
4014 the version strings. */
4015 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4016 ? ".dynstr" : ".gnu.libstr");
4018 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4023 case SHT_GNU_versym:
4024 /* sh_link is the section header index of the symbol table
4025 this hash table or version table is for. */
4026 s = bfd_get_section_by_name (abfd, ".dynsym");
4028 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4032 d->this_hdr.sh_link = elf_onesymtab (abfd);
4036 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4037 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4038 debug section name from .debug_* to .zdebug_* if needed. */
4044 sym_is_global (bfd *abfd, asymbol *sym)
4046 /* If the backend has a special mapping, use it. */
4047 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4048 if (bed->elf_backend_sym_is_global)
4049 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4051 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4052 || bfd_is_und_section (bfd_get_section (sym))
4053 || bfd_is_com_section (bfd_get_section (sym)));
4056 /* Filter global symbols of ABFD to include in the import library. All
4057 SYMCOUNT symbols of ABFD can be examined from their pointers in
4058 SYMS. Pointers of symbols to keep should be stored contiguously at
4059 the beginning of that array.
4061 Returns the number of symbols to keep. */
4064 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4065 asymbol **syms, long symcount)
4067 long src_count, dst_count = 0;
4069 for (src_count = 0; src_count < symcount; src_count++)
4071 asymbol *sym = syms[src_count];
4072 char *name = (char *) bfd_asymbol_name (sym);
4073 struct bfd_link_hash_entry *h;
4075 if (!sym_is_global (abfd, sym))
4078 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
4081 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4083 if (h->linker_def || h->ldscript_def)
4086 syms[dst_count++] = sym;
4089 syms[dst_count] = NULL;
4094 /* Don't output section symbols for sections that are not going to be
4095 output, that are duplicates or there is no BFD section. */
4098 ignore_section_sym (bfd *abfd, asymbol *sym)
4100 elf_symbol_type *type_ptr;
4105 if ((sym->flags & BSF_SECTION_SYM) == 0)
4108 if (sym->section == NULL)
4111 type_ptr = elf_symbol_from (abfd, sym);
4112 return ((type_ptr != NULL
4113 && type_ptr->internal_elf_sym.st_shndx != 0
4114 && bfd_is_abs_section (sym->section))
4115 || !(sym->section->owner == abfd
4116 || (sym->section->output_section != NULL
4117 && sym->section->output_section->owner == abfd
4118 && sym->section->output_offset == 0)
4119 || bfd_is_abs_section (sym->section)));
4122 /* Map symbol from it's internal number to the external number, moving
4123 all local symbols to be at the head of the list. */
4126 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4128 unsigned int symcount = bfd_get_symcount (abfd);
4129 asymbol **syms = bfd_get_outsymbols (abfd);
4130 asymbol **sect_syms;
4131 unsigned int num_locals = 0;
4132 unsigned int num_globals = 0;
4133 unsigned int num_locals2 = 0;
4134 unsigned int num_globals2 = 0;
4135 unsigned int max_index = 0;
4141 fprintf (stderr, "elf_map_symbols\n");
4145 for (asect = abfd->sections; asect; asect = asect->next)
4147 if (max_index < asect->index)
4148 max_index = asect->index;
4152 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
4153 if (sect_syms == NULL)
4155 elf_section_syms (abfd) = sect_syms;
4156 elf_num_section_syms (abfd) = max_index;
4158 /* Init sect_syms entries for any section symbols we have already
4159 decided to output. */
4160 for (idx = 0; idx < symcount; idx++)
4162 asymbol *sym = syms[idx];
4164 if ((sym->flags & BSF_SECTION_SYM) != 0
4166 && !ignore_section_sym (abfd, sym)
4167 && !bfd_is_abs_section (sym->section))
4169 asection *sec = sym->section;
4171 if (sec->owner != abfd)
4172 sec = sec->output_section;
4174 sect_syms[sec->index] = syms[idx];
4178 /* Classify all of the symbols. */
4179 for (idx = 0; idx < symcount; idx++)
4181 if (sym_is_global (abfd, syms[idx]))
4183 else if (!ignore_section_sym (abfd, syms[idx]))
4187 /* We will be adding a section symbol for each normal BFD section. Most
4188 sections will already have a section symbol in outsymbols, but
4189 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4190 at least in that case. */
4191 for (asect = abfd->sections; asect; asect = asect->next)
4193 if (sect_syms[asect->index] == NULL)
4195 if (!sym_is_global (abfd, asect->symbol))
4202 /* Now sort the symbols so the local symbols are first. */
4203 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
4204 sizeof (asymbol *));
4206 if (new_syms == NULL)
4209 for (idx = 0; idx < symcount; idx++)
4211 asymbol *sym = syms[idx];
4214 if (sym_is_global (abfd, sym))
4215 i = num_locals + num_globals2++;
4216 else if (!ignore_section_sym (abfd, sym))
4221 sym->udata.i = i + 1;
4223 for (asect = abfd->sections; asect; asect = asect->next)
4225 if (sect_syms[asect->index] == NULL)
4227 asymbol *sym = asect->symbol;
4230 sect_syms[asect->index] = sym;
4231 if (!sym_is_global (abfd, sym))
4234 i = num_locals + num_globals2++;
4236 sym->udata.i = i + 1;
4240 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4242 *pnum_locals = num_locals;
4246 /* Align to the maximum file alignment that could be required for any
4247 ELF data structure. */
4249 static inline file_ptr
4250 align_file_position (file_ptr off, int align)
4252 return (off + align - 1) & ~(align - 1);
4255 /* Assign a file position to a section, optionally aligning to the
4256 required section alignment. */
4259 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4263 if (align && i_shdrp->sh_addralign > 1)
4264 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4265 i_shdrp->sh_offset = offset;
4266 if (i_shdrp->bfd_section != NULL)
4267 i_shdrp->bfd_section->filepos = offset;
4268 if (i_shdrp->sh_type != SHT_NOBITS)
4269 offset += i_shdrp->sh_size;
4273 /* Compute the file positions we are going to put the sections at, and
4274 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4275 is not NULL, this is being called by the ELF backend linker. */
4278 _bfd_elf_compute_section_file_positions (bfd *abfd,
4279 struct bfd_link_info *link_info)
4281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4282 struct fake_section_arg fsargs;
4284 struct elf_strtab_hash *strtab = NULL;
4285 Elf_Internal_Shdr *shstrtab_hdr;
4286 bfd_boolean need_symtab;
4288 if (abfd->output_has_begun)
4291 /* Do any elf backend specific processing first. */
4292 if (bed->elf_backend_begin_write_processing)
4293 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4295 if (! prep_headers (abfd))
4298 /* Post process the headers if necessary. */
4299 (*bed->elf_backend_post_process_headers) (abfd, link_info);
4301 fsargs.failed = FALSE;
4302 fsargs.link_info = link_info;
4303 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4307 if (!assign_section_numbers (abfd, link_info))
4310 /* The backend linker builds symbol table information itself. */
4311 need_symtab = (link_info == NULL
4312 && (bfd_get_symcount (abfd) > 0
4313 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4317 /* Non-zero if doing a relocatable link. */
4318 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4320 if (! swap_out_syms (abfd, &strtab, relocatable_p))
4325 if (link_info == NULL)
4327 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4332 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4333 /* sh_name was set in prep_headers. */
4334 shstrtab_hdr->sh_type = SHT_STRTAB;
4335 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4336 shstrtab_hdr->sh_addr = 0;
4337 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4338 shstrtab_hdr->sh_entsize = 0;
4339 shstrtab_hdr->sh_link = 0;
4340 shstrtab_hdr->sh_info = 0;
4341 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4342 shstrtab_hdr->sh_addralign = 1;
4344 if (!assign_file_positions_except_relocs (abfd, link_info))
4350 Elf_Internal_Shdr *hdr;
4352 off = elf_next_file_pos (abfd);
4354 hdr = & elf_symtab_hdr (abfd);
4355 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4357 if (elf_symtab_shndx_list (abfd) != NULL)
4359 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4360 if (hdr->sh_size != 0)
4361 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4362 /* FIXME: What about other symtab_shndx sections in the list ? */
4365 hdr = &elf_tdata (abfd)->strtab_hdr;
4366 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4368 elf_next_file_pos (abfd) = off;
4370 /* Now that we know where the .strtab section goes, write it
4372 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4373 || ! _bfd_elf_strtab_emit (abfd, strtab))
4375 _bfd_elf_strtab_free (strtab);
4378 abfd->output_has_begun = TRUE;
4383 /* Make an initial estimate of the size of the program header. If we
4384 get the number wrong here, we'll redo section placement. */
4386 static bfd_size_type
4387 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4391 const struct elf_backend_data *bed;
4393 /* Assume we will need exactly two PT_LOAD segments: one for text
4394 and one for data. */
4397 s = bfd_get_section_by_name (abfd, ".interp");
4398 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4400 /* If we have a loadable interpreter section, we need a
4401 PT_INTERP segment. In this case, assume we also need a
4402 PT_PHDR segment, although that may not be true for all
4407 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4409 /* We need a PT_DYNAMIC segment. */
4413 if (info != NULL && info->relro)
4415 /* We need a PT_GNU_RELRO segment. */
4419 if (elf_eh_frame_hdr (abfd))
4421 /* We need a PT_GNU_EH_FRAME segment. */
4425 if (elf_stack_flags (abfd))
4427 /* We need a PT_GNU_STACK segment. */
4431 s = bfd_get_section_by_name (abfd,
4432 NOTE_GNU_PROPERTY_SECTION_NAME);
4433 if (s != NULL && s->size != 0)
4435 /* We need a PT_GNU_PROPERTY segment. */
4439 for (s = abfd->sections; s != NULL; s = s->next)
4441 if ((s->flags & SEC_LOAD) != 0
4442 && elf_section_type (s) == SHT_NOTE)
4444 unsigned int alignment_power;
4445 /* We need a PT_NOTE segment. */
4447 /* Try to create just one PT_NOTE segment for all adjacent
4448 loadable SHT_NOTE sections. gABI requires that within a
4449 PT_NOTE segment (and also inside of each SHT_NOTE section)
4450 each note should have the same alignment. So we check
4451 whether the sections are correctly aligned. */
4452 alignment_power = s->alignment_power;
4453 while (s->next != NULL
4454 && s->next->alignment_power == alignment_power
4455 && (s->next->flags & SEC_LOAD) != 0
4456 && elf_section_type (s->next) == SHT_NOTE)
4461 for (s = abfd->sections; s != NULL; s = s->next)
4463 if (s->flags & SEC_THREAD_LOCAL)
4465 /* We need a PT_TLS segment. */
4471 bed = get_elf_backend_data (abfd);
4473 if ((abfd->flags & D_PAGED) != 0
4474 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4476 /* Add a PT_GNU_MBIND segment for each mbind section. */
4477 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4478 for (s = abfd->sections; s != NULL; s = s->next)
4479 if (elf_section_flags (s) & SHF_GNU_MBIND)
4481 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4484 /* xgettext:c-format */
4485 (_("%pB: GNU_MBIND section `%pA' has invalid "
4486 "sh_info field: %d"),
4487 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4490 /* Align mbind section to page size. */
4491 if (s->alignment_power < page_align_power)
4492 s->alignment_power = page_align_power;
4497 /* Let the backend count up any program headers it might need. */
4498 if (bed->elf_backend_additional_program_headers)
4502 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4508 return segs * bed->s->sizeof_phdr;
4511 /* Find the segment that contains the output_section of section. */
4514 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4516 struct elf_segment_map *m;
4517 Elf_Internal_Phdr *p;
4519 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4525 for (i = m->count - 1; i >= 0; i--)
4526 if (m->sections[i] == section)
4533 /* Create a mapping from a set of sections to a program segment. */
4535 static struct elf_segment_map *
4536 make_mapping (bfd *abfd,
4537 asection **sections,
4542 struct elf_segment_map *m;
4547 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4548 amt += (to - from) * sizeof (asection *);
4549 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4553 m->p_type = PT_LOAD;
4554 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4555 m->sections[i - from] = *hdrpp;
4556 m->count = to - from;
4558 if (from == 0 && phdr)
4560 /* Include the headers in the first PT_LOAD segment. */
4561 m->includes_filehdr = 1;
4562 m->includes_phdrs = 1;
4568 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4571 struct elf_segment_map *
4572 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4574 struct elf_segment_map *m;
4576 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4577 sizeof (struct elf_segment_map));
4581 m->p_type = PT_DYNAMIC;
4583 m->sections[0] = dynsec;
4588 /* Possibly add or remove segments from the segment map. */
4591 elf_modify_segment_map (bfd *abfd,
4592 struct bfd_link_info *info,
4593 bfd_boolean remove_empty_load)
4595 struct elf_segment_map **m;
4596 const struct elf_backend_data *bed;
4598 /* The placement algorithm assumes that non allocated sections are
4599 not in PT_LOAD segments. We ensure this here by removing such
4600 sections from the segment map. We also remove excluded
4601 sections. Finally, any PT_LOAD segment without sections is
4603 m = &elf_seg_map (abfd);
4606 unsigned int i, new_count;
4608 for (new_count = 0, i = 0; i < (*m)->count; i++)
4610 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4611 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4612 || (*m)->p_type != PT_LOAD))
4614 (*m)->sections[new_count] = (*m)->sections[i];
4618 (*m)->count = new_count;
4620 if (remove_empty_load
4621 && (*m)->p_type == PT_LOAD
4623 && !(*m)->includes_phdrs)
4629 bed = get_elf_backend_data (abfd);
4630 if (bed->elf_backend_modify_segment_map != NULL)
4632 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4639 #define IS_TBSS(s) \
4640 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4642 /* Set up a mapping from BFD sections to program segments. */
4645 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4648 struct elf_segment_map *m;
4649 asection **sections = NULL;
4650 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4651 bfd_boolean no_user_phdrs;
4653 no_user_phdrs = elf_seg_map (abfd) == NULL;
4656 info->user_phdrs = !no_user_phdrs;
4658 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4662 struct elf_segment_map *mfirst;
4663 struct elf_segment_map **pm;
4666 unsigned int hdr_index;
4667 bfd_vma maxpagesize;
4669 bfd_boolean phdr_in_segment;
4670 bfd_boolean writable;
4671 bfd_boolean executable;
4673 asection *first_tls = NULL;
4674 asection *first_mbind = NULL;
4675 asection *dynsec, *eh_frame_hdr;
4677 bfd_vma addr_mask, wrap_to = 0;
4678 bfd_size_type phdr_size;
4680 /* Select the allocated sections, and sort them. */
4682 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4683 sizeof (asection *));
4684 if (sections == NULL)
4687 /* Calculate top address, avoiding undefined behaviour of shift
4688 left operator when shift count is equal to size of type
4690 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4691 addr_mask = (addr_mask << 1) + 1;
4694 for (s = abfd->sections; s != NULL; s = s->next)
4696 if ((s->flags & SEC_ALLOC) != 0)
4700 /* A wrapping section potentially clashes with header. */
4701 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4702 wrap_to = (s->lma + s->size) & addr_mask;
4705 BFD_ASSERT (i <= bfd_count_sections (abfd));
4708 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4710 phdr_size = elf_program_header_size (abfd);
4711 if (phdr_size == (bfd_size_type) -1)
4712 phdr_size = get_program_header_size (abfd, info);
4713 phdr_size += bed->s->sizeof_ehdr;
4714 maxpagesize = bed->maxpagesize;
4715 if (maxpagesize == 0)
4717 phdr_in_segment = info != NULL && info->load_phdrs;
4719 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4720 >= (phdr_size & (maxpagesize - 1))))
4721 /* For compatibility with old scripts that may not be using
4722 SIZEOF_HEADERS, add headers when it looks like space has
4723 been left for them. */
4724 phdr_in_segment = TRUE;
4726 /* Build the mapping. */
4730 /* If we have a .interp section, then create a PT_PHDR segment for
4731 the program headers and a PT_INTERP segment for the .interp
4733 s = bfd_get_section_by_name (abfd, ".interp");
4734 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4736 amt = sizeof (struct elf_segment_map);
4737 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4741 m->p_type = PT_PHDR;
4743 m->p_flags_valid = 1;
4744 m->includes_phdrs = 1;
4745 phdr_in_segment = TRUE;
4749 amt = sizeof (struct elf_segment_map);
4750 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4754 m->p_type = PT_INTERP;
4762 /* Look through the sections. We put sections in the same program
4763 segment when the start of the second section can be placed within
4764 a few bytes of the end of the first section. */
4770 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4772 && (dynsec->flags & SEC_LOAD) == 0)
4775 if ((abfd->flags & D_PAGED) == 0)
4776 phdr_in_segment = FALSE;
4778 /* Deal with -Ttext or something similar such that the first section
4779 is not adjacent to the program headers. This is an
4780 approximation, since at this point we don't know exactly how many
4781 program headers we will need. */
4782 if (phdr_in_segment && count > 0)
4785 bfd_boolean separate_phdr = FALSE;
4787 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4789 && info->separate_code
4790 && (sections[0]->flags & SEC_CODE) != 0)
4792 /* If data sections should be separate from code and
4793 thus not executable, and the first section is
4794 executable then put the file and program headers in
4795 their own PT_LOAD. */
4796 separate_phdr = TRUE;
4797 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4798 == (sections[0]->lma & addr_mask & -maxpagesize)))
4800 /* The file and program headers are currently on the
4801 same page as the first section. Put them on the
4802 previous page if we can. */
4803 if (phdr_lma >= maxpagesize)
4804 phdr_lma -= maxpagesize;
4806 separate_phdr = FALSE;
4809 if ((sections[0]->lma & addr_mask) < phdr_lma
4810 || (sections[0]->lma & addr_mask) < phdr_size)
4811 /* If file and program headers would be placed at the end
4812 of memory then it's probably better to omit them. */
4813 phdr_in_segment = FALSE;
4814 else if (phdr_lma < wrap_to)
4815 /* If a section wraps around to where we'll be placing
4816 file and program headers, then the headers will be
4818 phdr_in_segment = FALSE;
4819 else if (separate_phdr)
4821 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4824 m->p_paddr = phdr_lma;
4826 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4827 m->p_paddr_valid = 1;
4830 phdr_in_segment = FALSE;
4834 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4837 bfd_boolean new_segment;
4841 /* See if this section and the last one will fit in the same
4844 if (last_hdr == NULL)
4846 /* If we don't have a segment yet, then we don't need a new
4847 one (we build the last one after this loop). */
4848 new_segment = FALSE;
4850 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4852 /* If this section has a different relation between the
4853 virtual address and the load address, then we need a new
4857 else if (hdr->lma < last_hdr->lma + last_size
4858 || last_hdr->lma + last_size < last_hdr->lma)
4860 /* If this section has a load address that makes it overlap
4861 the previous section, then we need a new segment. */
4864 else if ((abfd->flags & D_PAGED) != 0
4865 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4866 == (hdr->lma & -maxpagesize)))
4868 /* If we are demand paged then we can't map two disk
4869 pages onto the same memory page. */
4870 new_segment = FALSE;
4872 /* In the next test we have to be careful when last_hdr->lma is close
4873 to the end of the address space. If the aligned address wraps
4874 around to the start of the address space, then there are no more
4875 pages left in memory and it is OK to assume that the current
4876 section can be included in the current segment. */
4877 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4878 + maxpagesize > last_hdr->lma)
4879 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4880 + maxpagesize <= hdr->lma))
4882 /* If putting this section in this segment would force us to
4883 skip a page in the segment, then we need a new segment. */
4886 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4887 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4889 /* We don't want to put a loaded section after a
4890 nonloaded (ie. bss style) section in the same segment
4891 as that will force the non-loaded section to be loaded.
4892 Consider .tbss sections as loaded for this purpose. */
4895 else if ((abfd->flags & D_PAGED) == 0)
4897 /* If the file is not demand paged, which means that we
4898 don't require the sections to be correctly aligned in the
4899 file, then there is no other reason for a new segment. */
4900 new_segment = FALSE;
4902 else if (info != NULL
4903 && info->separate_code
4904 && executable != ((hdr->flags & SEC_CODE) != 0))
4909 && (hdr->flags & SEC_READONLY) == 0)
4911 /* We don't want to put a writable section in a read only
4917 /* Otherwise, we can use the same segment. */
4918 new_segment = FALSE;
4921 /* Allow interested parties a chance to override our decision. */
4922 if (last_hdr != NULL
4924 && info->callbacks->override_segment_assignment != NULL)
4926 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4932 if ((hdr->flags & SEC_READONLY) == 0)
4934 if ((hdr->flags & SEC_CODE) != 0)
4937 /* .tbss sections effectively have zero size. */
4938 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4942 /* We need a new program segment. We must create a new program
4943 header holding all the sections from hdr_index until hdr. */
4945 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4952 if ((hdr->flags & SEC_READONLY) == 0)
4957 if ((hdr->flags & SEC_CODE) == 0)
4963 /* .tbss sections effectively have zero size. */
4964 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4966 phdr_in_segment = FALSE;
4969 /* Create a final PT_LOAD program segment, but not if it's just
4971 if (last_hdr != NULL
4972 && (i - hdr_index != 1
4973 || !IS_TBSS (last_hdr)))
4975 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4983 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4986 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4993 /* For each batch of consecutive loadable SHT_NOTE sections,
4994 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4995 because if we link together nonloadable .note sections and
4996 loadable .note sections, we will generate two .note sections
4997 in the output file. */
4998 for (s = abfd->sections; s != NULL; s = s->next)
5000 if ((s->flags & SEC_LOAD) != 0
5001 && elf_section_type (s) == SHT_NOTE)
5004 unsigned int alignment_power = s->alignment_power;
5007 for (s2 = s; s2->next != NULL; s2 = s2->next)
5009 if (s2->next->alignment_power == alignment_power
5010 && (s2->next->flags & SEC_LOAD) != 0
5011 && elf_section_type (s2->next) == SHT_NOTE
5012 && align_power (s2->lma + s2->size,
5019 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5020 amt += count * sizeof (asection *);
5021 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5025 m->p_type = PT_NOTE;
5029 m->sections[m->count - count--] = s;
5030 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5033 m->sections[m->count - 1] = s;
5034 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5038 if (s->flags & SEC_THREAD_LOCAL)
5044 if (first_mbind == NULL
5045 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5049 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5052 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5053 amt += tls_count * sizeof (asection *);
5054 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5059 m->count = tls_count;
5060 /* Mandated PF_R. */
5062 m->p_flags_valid = 1;
5064 for (i = 0; i < (unsigned int) tls_count; ++i)
5066 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5069 (_("%pB: TLS sections are not adjacent:"), abfd);
5072 while (i < (unsigned int) tls_count)
5074 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5076 _bfd_error_handler (_(" TLS: %pA"), s);
5080 _bfd_error_handler (_(" non-TLS: %pA"), s);
5083 bfd_set_error (bfd_error_bad_value);
5095 && (abfd->flags & D_PAGED) != 0
5096 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5097 for (s = first_mbind; s != NULL; s = s->next)
5098 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5099 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5101 /* Mandated PF_R. */
5102 unsigned long p_flags = PF_R;
5103 if ((s->flags & SEC_READONLY) == 0)
5105 if ((s->flags & SEC_CODE) != 0)
5108 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5109 m = bfd_zalloc (abfd, amt);
5113 m->p_type = (PT_GNU_MBIND_LO
5114 + elf_section_data (s)->this_hdr.sh_info);
5116 m->p_flags_valid = 1;
5118 m->p_flags = p_flags;
5124 s = bfd_get_section_by_name (abfd,
5125 NOTE_GNU_PROPERTY_SECTION_NAME);
5126 if (s != NULL && s->size != 0)
5128 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5129 m = bfd_zalloc (abfd, amt);
5133 m->p_type = PT_GNU_PROPERTY;
5135 m->p_flags_valid = 1;
5142 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5144 eh_frame_hdr = elf_eh_frame_hdr (abfd);
5145 if (eh_frame_hdr != NULL
5146 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5148 amt = sizeof (struct elf_segment_map);
5149 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5153 m->p_type = PT_GNU_EH_FRAME;
5155 m->sections[0] = eh_frame_hdr->output_section;
5161 if (elf_stack_flags (abfd))
5163 amt = sizeof (struct elf_segment_map);
5164 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5168 m->p_type = PT_GNU_STACK;
5169 m->p_flags = elf_stack_flags (abfd);
5170 m->p_align = bed->stack_align;
5171 m->p_flags_valid = 1;
5172 m->p_align_valid = m->p_align != 0;
5173 if (info->stacksize > 0)
5175 m->p_size = info->stacksize;
5176 m->p_size_valid = 1;
5183 if (info != NULL && info->relro)
5185 for (m = mfirst; m != NULL; m = m->next)
5187 if (m->p_type == PT_LOAD
5189 && m->sections[0]->vma >= info->relro_start
5190 && m->sections[0]->vma < info->relro_end)
5193 while (--i != (unsigned) -1)
5194 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5195 == (SEC_LOAD | SEC_HAS_CONTENTS))
5198 if (i != (unsigned) -1)
5203 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5206 amt = sizeof (struct elf_segment_map);
5207 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5211 m->p_type = PT_GNU_RELRO;
5218 elf_seg_map (abfd) = mfirst;
5221 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5224 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5226 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5231 if (sections != NULL)
5236 /* Sort sections by address. */
5239 elf_sort_sections (const void *arg1, const void *arg2)
5241 const asection *sec1 = *(const asection **) arg1;
5242 const asection *sec2 = *(const asection **) arg2;
5243 bfd_size_type size1, size2;
5245 /* Sort by LMA first, since this is the address used to
5246 place the section into a segment. */
5247 if (sec1->lma < sec2->lma)
5249 else if (sec1->lma > sec2->lma)
5252 /* Then sort by VMA. Normally the LMA and the VMA will be
5253 the same, and this will do nothing. */
5254 if (sec1->vma < sec2->vma)
5256 else if (sec1->vma > sec2->vma)
5259 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5261 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5267 /* If the indices are the same, do not return 0
5268 here, but continue to try the next comparison. */
5269 if (sec1->target_index - sec2->target_index != 0)
5270 return sec1->target_index - sec2->target_index;
5275 else if (TOEND (sec2))
5280 /* Sort by size, to put zero sized sections
5281 before others at the same address. */
5283 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5284 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5291 return sec1->target_index - sec2->target_index;
5294 /* Ian Lance Taylor writes:
5296 We shouldn't be using % with a negative signed number. That's just
5297 not good. We have to make sure either that the number is not
5298 negative, or that the number has an unsigned type. When the types
5299 are all the same size they wind up as unsigned. When file_ptr is a
5300 larger signed type, the arithmetic winds up as signed long long,
5303 What we're trying to say here is something like ``increase OFF by
5304 the least amount that will cause it to be equal to the VMA modulo
5306 /* In other words, something like:
5308 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5309 off_offset = off % bed->maxpagesize;
5310 if (vma_offset < off_offset)
5311 adjustment = vma_offset + bed->maxpagesize - off_offset;
5313 adjustment = vma_offset - off_offset;
5315 which can be collapsed into the expression below. */
5318 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5320 /* PR binutils/16199: Handle an alignment of zero. */
5321 if (maxpagesize == 0)
5323 return ((vma - off) % maxpagesize);
5327 print_segment_map (const struct elf_segment_map *m)
5330 const char *pt = get_segment_type (m->p_type);
5335 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5336 sprintf (buf, "LOPROC+%7.7x",
5337 (unsigned int) (m->p_type - PT_LOPROC));
5338 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5339 sprintf (buf, "LOOS+%7.7x",
5340 (unsigned int) (m->p_type - PT_LOOS));
5342 snprintf (buf, sizeof (buf), "%8.8x",
5343 (unsigned int) m->p_type);
5347 fprintf (stderr, "%s:", pt);
5348 for (j = 0; j < m->count; j++)
5349 fprintf (stderr, " %s", m->sections [j]->name);
5355 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5360 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5362 buf = bfd_zmalloc (len);
5365 ret = bfd_bwrite (buf, len, abfd) == len;
5370 /* Assign file positions to the sections based on the mapping from
5371 sections to segments. This function also sets up some fields in
5375 assign_file_positions_for_load_sections (bfd *abfd,
5376 struct bfd_link_info *link_info)
5378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5379 struct elf_segment_map *m;
5380 Elf_Internal_Phdr *phdrs;
5381 Elf_Internal_Phdr *p;
5383 bfd_size_type maxpagesize;
5384 unsigned int pt_load_count = 0;
5387 bfd_vma header_pad = 0;
5389 if (link_info == NULL
5390 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5394 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5398 header_pad = m->header_size;
5403 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5404 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5408 /* PR binutils/12467. */
5409 elf_elfheader (abfd)->e_phoff = 0;
5410 elf_elfheader (abfd)->e_phentsize = 0;
5413 elf_elfheader (abfd)->e_phnum = alloc;
5415 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5416 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5418 BFD_ASSERT (elf_program_header_size (abfd)
5419 >= alloc * bed->s->sizeof_phdr);
5423 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5427 /* We're writing the size in elf_program_header_size (abfd),
5428 see assign_file_positions_except_relocs, so make sure we have
5429 that amount allocated, with trailing space cleared.
5430 The variable alloc contains the computed need, while
5431 elf_program_header_size (abfd) contains the size used for the
5433 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5434 where the layout is forced to according to a larger size in the
5435 last iterations for the testcase ld-elf/header. */
5436 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
5438 phdrs = (Elf_Internal_Phdr *)
5440 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5441 sizeof (Elf_Internal_Phdr));
5442 elf_tdata (abfd)->phdr = phdrs;
5447 if ((abfd->flags & D_PAGED) != 0)
5448 maxpagesize = bed->maxpagesize;
5450 off = bed->s->sizeof_ehdr;
5451 off += alloc * bed->s->sizeof_phdr;
5452 if (header_pad < (bfd_vma) off)
5458 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
5460 m = m->next, p++, j++)
5464 bfd_boolean no_contents;
5466 /* If elf_segment_map is not from map_sections_to_segments, the
5467 sections may not be correctly ordered. NOTE: sorting should
5468 not be done to the PT_NOTE section of a corefile, which may
5469 contain several pseudo-sections artificially created by bfd.
5470 Sorting these pseudo-sections breaks things badly. */
5472 && !(elf_elfheader (abfd)->e_type == ET_CORE
5473 && m->p_type == PT_NOTE))
5474 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5477 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5478 number of sections with contents contributing to both p_filesz
5479 and p_memsz, followed by a number of sections with no contents
5480 that just contribute to p_memsz. In this loop, OFF tracks next
5481 available file offset for PT_LOAD and PT_NOTE segments. */
5482 p->p_type = m->p_type;
5483 p->p_flags = m->p_flags;
5486 p->p_vaddr = m->p_vaddr_offset;
5488 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
5490 if (m->p_paddr_valid)
5491 p->p_paddr = m->p_paddr;
5492 else if (m->count == 0)
5495 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
5497 if (p->p_type == PT_LOAD
5498 && (abfd->flags & D_PAGED) != 0)
5500 /* p_align in demand paged PT_LOAD segments effectively stores
5501 the maximum page size. When copying an executable with
5502 objcopy, we set m->p_align from the input file. Use this
5503 value for maxpagesize rather than bed->maxpagesize, which
5504 may be different. Note that we use maxpagesize for PT_TLS
5505 segment alignment later in this function, so we are relying
5506 on at least one PT_LOAD segment appearing before a PT_TLS
5508 if (m->p_align_valid)
5509 maxpagesize = m->p_align;
5511 p->p_align = maxpagesize;
5514 else if (m->p_align_valid)
5515 p->p_align = m->p_align;
5516 else if (m->count == 0)
5517 p->p_align = 1 << bed->s->log_file_align;
5521 no_contents = FALSE;
5523 if (p->p_type == PT_LOAD
5526 bfd_size_type align;
5527 unsigned int align_power = 0;
5529 if (m->p_align_valid)
5533 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5535 unsigned int secalign;
5537 secalign = bfd_get_section_alignment (abfd, *secpp);
5538 if (secalign > align_power)
5539 align_power = secalign;
5541 align = (bfd_size_type) 1 << align_power;
5542 if (align < maxpagesize)
5543 align = maxpagesize;
5546 for (i = 0; i < m->count; i++)
5547 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5548 /* If we aren't making room for this section, then
5549 it must be SHT_NOBITS regardless of what we've
5550 set via struct bfd_elf_special_section. */
5551 elf_section_type (m->sections[i]) = SHT_NOBITS;
5553 /* Find out whether this segment contains any loadable
5556 for (i = 0; i < m->count; i++)
5557 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5559 no_contents = FALSE;
5563 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5565 /* Broken hardware and/or kernel require that files do not
5566 map the same page with different permissions on some hppa
5568 if (pt_load_count > 1
5569 && bed->no_page_alias
5570 && (off & (maxpagesize - 1)) != 0
5571 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5572 off_adjust += maxpagesize;
5576 /* We shouldn't need to align the segment on disk since
5577 the segment doesn't need file space, but the gABI
5578 arguably requires the alignment and glibc ld.so
5579 checks it. So to comply with the alignment
5580 requirement but not waste file space, we adjust
5581 p_offset for just this segment. (OFF_ADJUST is
5582 subtracted from OFF later.) This may put p_offset
5583 past the end of file, but that shouldn't matter. */
5588 /* Make sure the .dynamic section is the first section in the
5589 PT_DYNAMIC segment. */
5590 else if (p->p_type == PT_DYNAMIC
5592 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5595 (_("%pB: The first section in the PT_DYNAMIC segment"
5596 " is not the .dynamic section"),
5598 bfd_set_error (bfd_error_bad_value);
5601 /* Set the note section type to SHT_NOTE. */
5602 else if (p->p_type == PT_NOTE)
5603 for (i = 0; i < m->count; i++)
5604 elf_section_type (m->sections[i]) = SHT_NOTE;
5610 if (m->includes_filehdr)
5612 if (!m->p_flags_valid)
5614 p->p_filesz = bed->s->sizeof_ehdr;
5615 p->p_memsz = bed->s->sizeof_ehdr;
5618 if (p->p_vaddr < (bfd_vma) off
5619 || (!m->p_paddr_valid
5620 && p->p_paddr < (bfd_vma) off))
5623 (_("%pB: not enough room for program headers,"
5624 " try linking with -N"),
5626 bfd_set_error (bfd_error_bad_value);
5631 if (!m->p_paddr_valid)
5636 if (m->includes_phdrs)
5638 if (!m->p_flags_valid)
5641 if (!m->includes_filehdr)
5643 p->p_offset = bed->s->sizeof_ehdr;
5647 p->p_vaddr -= off - p->p_offset;
5648 if (!m->p_paddr_valid)
5649 p->p_paddr -= off - p->p_offset;
5653 p->p_filesz += alloc * bed->s->sizeof_phdr;
5654 p->p_memsz += alloc * bed->s->sizeof_phdr;
5657 p->p_filesz += header_pad;
5658 p->p_memsz += header_pad;
5662 if (p->p_type == PT_LOAD
5663 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5665 if (!m->includes_filehdr && !m->includes_phdrs)
5671 adjust = off - (p->p_offset + p->p_filesz);
5673 p->p_filesz += adjust;
5674 p->p_memsz += adjust;
5678 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5679 maps. Set filepos for sections in PT_LOAD segments, and in
5680 core files, for sections in PT_NOTE segments.
5681 assign_file_positions_for_non_load_sections will set filepos
5682 for other sections and update p_filesz for other segments. */
5683 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5686 bfd_size_type align;
5687 Elf_Internal_Shdr *this_hdr;
5690 this_hdr = &elf_section_data (sec)->this_hdr;
5691 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5693 if ((p->p_type == PT_LOAD
5694 || p->p_type == PT_TLS)
5695 && (this_hdr->sh_type != SHT_NOBITS
5696 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5697 && ((this_hdr->sh_flags & SHF_TLS) == 0
5698 || p->p_type == PT_TLS))))
5700 bfd_vma p_start = p->p_paddr;
5701 bfd_vma p_end = p_start + p->p_memsz;
5702 bfd_vma s_start = sec->lma;
5703 bfd_vma adjust = s_start - p_end;
5707 || p_end < p_start))
5710 /* xgettext:c-format */
5711 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5712 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
5716 p->p_memsz += adjust;
5718 if (this_hdr->sh_type != SHT_NOBITS)
5720 if (p->p_filesz + adjust < p->p_memsz)
5722 /* We have a PROGBITS section following NOBITS ones.
5723 Allocate file space for the NOBITS section(s) and
5725 adjust = p->p_memsz - p->p_filesz;
5726 if (!write_zeros (abfd, off, adjust))
5730 p->p_filesz += adjust;
5734 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5736 /* The section at i == 0 is the one that actually contains
5740 this_hdr->sh_offset = sec->filepos = off;
5741 off += this_hdr->sh_size;
5742 p->p_filesz = this_hdr->sh_size;
5748 /* The rest are fake sections that shouldn't be written. */
5757 if (p->p_type == PT_LOAD)
5759 this_hdr->sh_offset = sec->filepos = off;
5760 if (this_hdr->sh_type != SHT_NOBITS)
5761 off += this_hdr->sh_size;
5763 else if (this_hdr->sh_type == SHT_NOBITS
5764 && (this_hdr->sh_flags & SHF_TLS) != 0
5765 && this_hdr->sh_offset == 0)
5767 /* This is a .tbss section that didn't get a PT_LOAD.
5768 (See _bfd_elf_map_sections_to_segments "Create a
5769 final PT_LOAD".) Set sh_offset to the value it
5770 would have if we had created a zero p_filesz and
5771 p_memsz PT_LOAD header for the section. This
5772 also makes the PT_TLS header have the same
5774 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5776 this_hdr->sh_offset = sec->filepos = off + adjust;
5779 if (this_hdr->sh_type != SHT_NOBITS)
5781 p->p_filesz += this_hdr->sh_size;
5782 /* A load section without SHF_ALLOC is something like
5783 a note section in a PT_NOTE segment. These take
5784 file space but are not loaded into memory. */
5785 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5786 p->p_memsz += this_hdr->sh_size;
5788 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5790 if (p->p_type == PT_TLS)
5791 p->p_memsz += this_hdr->sh_size;
5793 /* .tbss is special. It doesn't contribute to p_memsz of
5795 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5796 p->p_memsz += this_hdr->sh_size;
5799 if (align > p->p_align
5800 && !m->p_align_valid
5801 && (p->p_type != PT_LOAD
5802 || (abfd->flags & D_PAGED) == 0))
5806 if (!m->p_flags_valid)
5809 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5811 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5818 /* Check that all sections are in a PT_LOAD segment.
5819 Don't check funky gdb generated core files. */
5820 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5822 bfd_boolean check_vma = TRUE;
5824 for (i = 1; i < m->count; i++)
5825 if (m->sections[i]->vma == m->sections[i - 1]->vma
5826 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5827 ->this_hdr), p) != 0
5828 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5829 ->this_hdr), p) != 0)
5831 /* Looks like we have overlays packed into the segment. */
5836 for (i = 0; i < m->count; i++)
5838 Elf_Internal_Shdr *this_hdr;
5841 sec = m->sections[i];
5842 this_hdr = &(elf_section_data(sec)->this_hdr);
5843 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5844 && !ELF_TBSS_SPECIAL (this_hdr, p))
5847 /* xgettext:c-format */
5848 (_("%pB: section `%pA' can't be allocated in segment %d"),
5850 print_segment_map (m);
5856 elf_next_file_pos (abfd) = off;
5860 /* Determine if a bfd is a debuginfo file. Unfortunately there
5861 is no defined method for detecting such files, so we have to
5862 use heuristics instead. */
5865 is_debuginfo_file (bfd *abfd)
5867 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5870 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
5871 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
5872 Elf_Internal_Shdr **headerp;
5874 for (headerp = start_headers; headerp < end_headers; headerp ++)
5876 Elf_Internal_Shdr *header = * headerp;
5878 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5879 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5880 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
5881 && header->sh_type != SHT_NOBITS
5882 && header->sh_type != SHT_NOTE)
5889 /* Assign file positions for the other sections. */
5892 assign_file_positions_for_non_load_sections (bfd *abfd,
5893 struct bfd_link_info *link_info)
5895 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5896 Elf_Internal_Shdr **i_shdrpp;
5897 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5898 Elf_Internal_Phdr *phdrs;
5899 Elf_Internal_Phdr *p;
5900 struct elf_segment_map *m;
5901 struct elf_segment_map *hdrs_segment;
5902 bfd_vma filehdr_vaddr, filehdr_paddr;
5903 bfd_vma phdrs_vaddr, phdrs_paddr;
5907 i_shdrpp = elf_elfsections (abfd);
5908 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5909 off = elf_next_file_pos (abfd);
5910 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5912 Elf_Internal_Shdr *hdr;
5915 if (hdr->bfd_section != NULL
5916 && (hdr->bfd_section->filepos != 0
5917 || (hdr->sh_type == SHT_NOBITS
5918 && hdr->contents == NULL)))
5919 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5920 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5922 if (hdr->sh_size != 0
5923 /* PR 24717 - debuginfo files are known to be not strictly
5924 compliant with the ELF standard. In particular they often
5925 have .note.gnu.property sections that are outside of any
5926 loadable segment. This is not a problem for such files,
5927 so do not warn about them. */
5928 && ! is_debuginfo_file (abfd))
5930 /* xgettext:c-format */
5931 (_("%pB: warning: allocated section `%s' not in segment"),
5933 (hdr->bfd_section == NULL
5935 : hdr->bfd_section->name));
5936 /* We don't need to page align empty sections. */
5937 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5938 off += vma_page_aligned_bias (hdr->sh_addr, off,
5941 off += vma_page_aligned_bias (hdr->sh_addr, off,
5943 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5946 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5947 && hdr->bfd_section == NULL)
5948 || (hdr->bfd_section != NULL
5949 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5950 /* Compress DWARF debug sections. */
5951 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5952 || (elf_symtab_shndx_list (abfd) != NULL
5953 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5954 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5955 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5956 hdr->sh_offset = -1;
5958 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5961 /* Now that we have set the section file positions, we can set up
5962 the file positions for the non PT_LOAD segments. */
5966 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5968 hdrs_segment = NULL;
5969 phdrs = elf_tdata (abfd)->phdr;
5970 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5973 if (p->p_type != PT_LOAD)
5976 if (m->includes_filehdr)
5978 filehdr_vaddr = p->p_vaddr;
5979 filehdr_paddr = p->p_paddr;
5981 if (m->includes_phdrs)
5983 phdrs_vaddr = p->p_vaddr;
5984 phdrs_paddr = p->p_paddr;
5985 if (m->includes_filehdr)
5988 phdrs_vaddr += bed->s->sizeof_ehdr;
5989 phdrs_paddr += bed->s->sizeof_ehdr;
5994 if (hdrs_segment != NULL && link_info != NULL)
5996 /* There is a segment that contains both the file headers and the
5997 program headers, so provide a symbol __ehdr_start pointing there.
5998 A program can use this to examine itself robustly. */
6000 struct elf_link_hash_entry *hash
6001 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6002 FALSE, FALSE, TRUE);
6003 /* If the symbol was referenced and not defined, define it. */
6005 && (hash->root.type == bfd_link_hash_new
6006 || hash->root.type == bfd_link_hash_undefined
6007 || hash->root.type == bfd_link_hash_undefweak
6008 || hash->root.type == bfd_link_hash_common))
6011 if (hdrs_segment->count != 0)
6012 /* The segment contains sections, so use the first one. */
6013 s = hdrs_segment->sections[0];
6015 /* Use the first (i.e. lowest-addressed) section in any segment. */
6016 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6025 hash->root.u.def.value = filehdr_vaddr - s->vma;
6026 hash->root.u.def.section = s;
6030 hash->root.u.def.value = filehdr_vaddr;
6031 hash->root.u.def.section = bfd_abs_section_ptr;
6034 hash->root.type = bfd_link_hash_defined;
6035 hash->def_regular = 1;
6040 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6042 if (p->p_type == PT_GNU_RELRO)
6047 if (link_info != NULL)
6049 /* During linking the range of the RELRO segment is passed
6050 in link_info. Note that there may be padding between
6051 relro_start and the first RELRO section. */
6052 start = link_info->relro_start;
6053 end = link_info->relro_end;
6055 else if (m->count != 0)
6057 if (!m->p_size_valid)
6059 start = m->sections[0]->vma;
6060 end = start + m->p_size;
6071 struct elf_segment_map *lm;
6072 const Elf_Internal_Phdr *lp;
6075 /* Find a LOAD segment containing a section in the RELRO
6077 for (lm = elf_seg_map (abfd), lp = phdrs;
6079 lm = lm->next, lp++)
6081 if (lp->p_type == PT_LOAD
6083 && (lm->sections[lm->count - 1]->vma
6084 + (!IS_TBSS (lm->sections[lm->count - 1])
6085 ? lm->sections[lm->count - 1]->size
6087 && lm->sections[0]->vma < end)
6093 /* Find the section starting the RELRO segment. */
6094 for (i = 0; i < lm->count; i++)
6096 asection *s = lm->sections[i];
6105 p->p_vaddr = lm->sections[i]->vma;
6106 p->p_paddr = lm->sections[i]->lma;
6107 p->p_offset = lm->sections[i]->filepos;
6108 p->p_memsz = end - p->p_vaddr;
6109 p->p_filesz = p->p_memsz;
6111 /* The RELRO segment typically ends a few bytes
6112 into .got.plt but other layouts are possible.
6113 In cases where the end does not match any
6114 loaded section (for instance is in file
6115 padding), trim p_filesz back to correspond to
6116 the end of loaded section contents. */
6117 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6118 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6120 /* Preserve the alignment and flags if they are
6121 valid. The gold linker generates RW/4 for
6122 the PT_GNU_RELRO section. It is better for
6123 objcopy/strip to honor these attributes
6124 otherwise gdb will choke when using separate
6126 if (!m->p_align_valid)
6128 if (!m->p_flags_valid)
6134 if (link_info != NULL)
6137 memset (p, 0, sizeof *p);
6139 else if (p->p_type == PT_GNU_STACK)
6141 if (m->p_size_valid)
6142 p->p_memsz = m->p_size;
6144 else if (m->count != 0)
6148 if (p->p_type != PT_LOAD
6149 && (p->p_type != PT_NOTE
6150 || bfd_get_format (abfd) != bfd_core))
6152 /* A user specified segment layout may include a PHDR
6153 segment that overlaps with a LOAD segment... */
6154 if (p->p_type == PT_PHDR)
6160 if (m->includes_filehdr || m->includes_phdrs)
6162 /* PR 17512: file: 2195325e. */
6164 (_("%pB: error: non-load segment %d includes file header "
6165 "and/or program header"),
6166 abfd, (int) (p - phdrs));
6171 p->p_offset = m->sections[0]->filepos;
6172 for (i = m->count; i-- != 0;)
6174 asection *sect = m->sections[i];
6175 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6176 if (hdr->sh_type != SHT_NOBITS)
6178 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6185 else if (m->includes_filehdr)
6187 p->p_vaddr = filehdr_vaddr;
6188 if (! m->p_paddr_valid)
6189 p->p_paddr = filehdr_paddr;
6191 else if (m->includes_phdrs)
6193 p->p_vaddr = phdrs_vaddr;
6194 if (! m->p_paddr_valid)
6195 p->p_paddr = phdrs_paddr;
6199 elf_next_file_pos (abfd) = off;
6204 static elf_section_list *
6205 find_section_in_list (unsigned int i, elf_section_list * list)
6207 for (;list != NULL; list = list->next)
6213 /* Work out the file positions of all the sections. This is called by
6214 _bfd_elf_compute_section_file_positions. All the section sizes and
6215 VMAs must be known before this is called.
6217 Reloc sections come in two flavours: Those processed specially as
6218 "side-channel" data attached to a section to which they apply, and
6219 those that bfd doesn't process as relocations. The latter sort are
6220 stored in a normal bfd section by bfd_section_from_shdr. We don't
6221 consider the former sort here, unless they form part of the loadable
6222 image. Reloc sections not assigned here will be handled later by
6223 assign_file_positions_for_relocs.
6225 We also don't set the positions of the .symtab and .strtab here. */
6228 assign_file_positions_except_relocs (bfd *abfd,
6229 struct bfd_link_info *link_info)
6231 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6232 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6235 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6236 && bfd_get_format (abfd) != bfd_core)
6238 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6239 unsigned int num_sec = elf_numsections (abfd);
6240 Elf_Internal_Shdr **hdrpp;
6244 /* Start after the ELF header. */
6245 off = i_ehdrp->e_ehsize;
6247 /* We are not creating an executable, which means that we are
6248 not creating a program header, and that the actual order of
6249 the sections in the file is unimportant. */
6250 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6252 Elf_Internal_Shdr *hdr;
6255 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6256 && hdr->bfd_section == NULL)
6257 || (hdr->bfd_section != NULL
6258 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6259 /* Compress DWARF debug sections. */
6260 || i == elf_onesymtab (abfd)
6261 || (elf_symtab_shndx_list (abfd) != NULL
6262 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6263 || i == elf_strtab_sec (abfd)
6264 || i == elf_shstrtab_sec (abfd))
6266 hdr->sh_offset = -1;
6269 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6272 elf_next_file_pos (abfd) = off;
6278 /* Assign file positions for the loaded sections based on the
6279 assignment of sections to segments. */
6280 if (!assign_file_positions_for_load_sections (abfd, link_info))
6283 /* And for non-load sections. */
6284 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6287 if (bed->elf_backend_modify_program_headers != NULL)
6289 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6293 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6294 if (link_info != NULL && bfd_link_pie (link_info))
6296 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6297 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6298 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6300 /* Find the lowest p_vaddr in PT_LOAD segments. */
6301 bfd_vma p_vaddr = (bfd_vma) -1;
6302 for (; segment < end_segment; segment++)
6303 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6304 p_vaddr = segment->p_vaddr;
6306 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6307 segments is non-zero. */
6309 i_ehdrp->e_type = ET_EXEC;
6312 /* Write out the program headers. */
6313 alloc = elf_elfheader (abfd)->e_phnum;
6317 /* PR ld/20815 - Check that the program header segment, if present, will
6318 be loaded into memory. FIXME: The check below is not sufficient as
6319 really all PT_LOAD segments should be checked before issuing an error
6320 message. Plus the PHDR segment does not have to be the first segment
6321 in the program header table. But this version of the check should
6322 catch all real world use cases.
6324 FIXME: We used to have code here to sort the PT_LOAD segments into
6325 ascending order, as per the ELF spec. But this breaks some programs,
6326 including the Linux kernel. But really either the spec should be
6327 changed or the programs updated. */
6329 && tdata->phdr[0].p_type == PT_PHDR
6330 && (bed->elf_backend_allow_non_load_phdr == NULL
6331 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6333 && tdata->phdr[1].p_type == PT_LOAD
6334 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6335 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6336 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
6338 /* The fix for this error is usually to edit the linker script being
6339 used and set up the program headers manually. Either that or
6340 leave room for the headers at the start of the SECTIONS. */
6341 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6342 " by LOAD segment"),
6347 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
6348 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6356 prep_headers (bfd *abfd)
6358 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6359 struct elf_strtab_hash *shstrtab;
6360 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6362 i_ehdrp = elf_elfheader (abfd);
6364 shstrtab = _bfd_elf_strtab_init ();
6365 if (shstrtab == NULL)
6368 elf_shstrtab (abfd) = shstrtab;
6370 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6371 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6372 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6373 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6375 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6376 i_ehdrp->e_ident[EI_DATA] =
6377 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6378 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6380 if ((abfd->flags & DYNAMIC) != 0)
6381 i_ehdrp->e_type = ET_DYN;
6382 else if ((abfd->flags & EXEC_P) != 0)
6383 i_ehdrp->e_type = ET_EXEC;
6384 else if (bfd_get_format (abfd) == bfd_core)
6385 i_ehdrp->e_type = ET_CORE;
6387 i_ehdrp->e_type = ET_REL;
6389 switch (bfd_get_arch (abfd))
6391 case bfd_arch_unknown:
6392 i_ehdrp->e_machine = EM_NONE;
6395 /* There used to be a long list of cases here, each one setting
6396 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6397 in the corresponding bfd definition. To avoid duplication,
6398 the switch was removed. Machines that need special handling
6399 can generally do it in elf_backend_final_write_processing(),
6400 unless they need the information earlier than the final write.
6401 Such need can generally be supplied by replacing the tests for
6402 e_machine with the conditions used to determine it. */
6404 i_ehdrp->e_machine = bed->elf_machine_code;
6407 i_ehdrp->e_version = bed->s->ev_current;
6408 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6410 /* No program header, for now. */
6411 i_ehdrp->e_phoff = 0;
6412 i_ehdrp->e_phentsize = 0;
6413 i_ehdrp->e_phnum = 0;
6415 /* Each bfd section is section header entry. */
6416 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6417 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6419 /* If we're building an executable, we'll need a program header table. */
6420 if (abfd->flags & EXEC_P)
6421 /* It all happens later. */
6425 i_ehdrp->e_phentsize = 0;
6426 i_ehdrp->e_phoff = 0;
6429 elf_tdata (abfd)->symtab_hdr.sh_name =
6430 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6431 elf_tdata (abfd)->strtab_hdr.sh_name =
6432 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6433 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6434 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6435 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6436 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6437 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6443 /* Assign file positions for all the reloc sections which are not part
6444 of the loadable file image, and the file position of section headers. */
6447 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6450 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6451 Elf_Internal_Shdr *shdrp;
6452 Elf_Internal_Ehdr *i_ehdrp;
6453 const struct elf_backend_data *bed;
6455 off = elf_next_file_pos (abfd);
6457 shdrpp = elf_elfsections (abfd);
6458 end_shdrpp = shdrpp + elf_numsections (abfd);
6459 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6462 if (shdrp->sh_offset == -1)
6464 asection *sec = shdrp->bfd_section;
6465 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6466 || shdrp->sh_type == SHT_RELA);
6468 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6472 const char *name = sec->name;
6473 struct bfd_elf_section_data *d;
6475 /* Compress DWARF debug sections. */
6476 if (!bfd_compress_section (abfd, sec,
6480 if (sec->compress_status == COMPRESS_SECTION_DONE
6481 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6483 /* If section is compressed with zlib-gnu, convert
6484 section name from .debug_* to .zdebug_*. */
6486 = convert_debug_to_zdebug (abfd, name);
6487 if (new_name == NULL)
6491 /* Add section name to section name section. */
6492 if (shdrp->sh_name != (unsigned int) -1)
6495 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6497 d = elf_section_data (sec);
6499 /* Add reloc section name to section name section. */
6501 && !_bfd_elf_set_reloc_sh_name (abfd,
6506 && !_bfd_elf_set_reloc_sh_name (abfd,
6511 /* Update section size and contents. */
6512 shdrp->sh_size = sec->size;
6513 shdrp->contents = sec->contents;
6514 shdrp->bfd_section->contents = NULL;
6516 off = _bfd_elf_assign_file_position_for_section (shdrp,
6523 /* Place section name section after DWARF debug sections have been
6525 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6526 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6527 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6528 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6530 /* Place the section headers. */
6531 i_ehdrp = elf_elfheader (abfd);
6532 bed = get_elf_backend_data (abfd);
6533 off = align_file_position (off, 1 << bed->s->log_file_align);
6534 i_ehdrp->e_shoff = off;
6535 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6536 elf_next_file_pos (abfd) = off;
6542 _bfd_elf_write_object_contents (bfd *abfd)
6544 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6545 Elf_Internal_Shdr **i_shdrp;
6547 unsigned int count, num_sec;
6548 struct elf_obj_tdata *t;
6550 if (! abfd->output_has_begun
6551 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6553 /* Do not rewrite ELF data when the BFD has been opened for update.
6554 abfd->output_has_begun was set to TRUE on opening, so creation of new
6555 sections, and modification of existing section sizes was restricted.
6556 This means the ELF header, program headers and section headers can't have
6558 If the contents of any sections has been modified, then those changes have
6559 already been written to the BFD. */
6560 else if (abfd->direction == both_direction)
6562 BFD_ASSERT (abfd->output_has_begun);
6566 i_shdrp = elf_elfsections (abfd);
6569 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6573 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6576 /* After writing the headers, we need to write the sections too... */
6577 num_sec = elf_numsections (abfd);
6578 for (count = 1; count < num_sec; count++)
6580 i_shdrp[count]->sh_name
6581 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6582 i_shdrp[count]->sh_name);
6583 if (bed->elf_backend_section_processing)
6584 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6586 if (i_shdrp[count]->contents)
6588 bfd_size_type amt = i_shdrp[count]->sh_size;
6590 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6591 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6596 /* Write out the section header names. */
6597 t = elf_tdata (abfd);
6598 if (elf_shstrtab (abfd) != NULL
6599 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6600 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6603 if (!(*bed->elf_backend_final_write_processing) (abfd))
6606 if (!bed->s->write_shdrs_and_ehdr (abfd))
6609 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6610 if (t->o->build_id.after_write_object_contents != NULL)
6611 return (*t->o->build_id.after_write_object_contents) (abfd);
6617 _bfd_elf_write_corefile_contents (bfd *abfd)
6619 /* Hopefully this can be done just like an object file. */
6620 return _bfd_elf_write_object_contents (abfd);
6623 /* Given a section, search the header to find them. */
6626 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6628 const struct elf_backend_data *bed;
6629 unsigned int sec_index;
6631 if (elf_section_data (asect) != NULL
6632 && elf_section_data (asect)->this_idx != 0)
6633 return elf_section_data (asect)->this_idx;
6635 if (bfd_is_abs_section (asect))
6636 sec_index = SHN_ABS;
6637 else if (bfd_is_com_section (asect))
6638 sec_index = SHN_COMMON;
6639 else if (bfd_is_und_section (asect))
6640 sec_index = SHN_UNDEF;
6642 sec_index = SHN_BAD;
6644 bed = get_elf_backend_data (abfd);
6645 if (bed->elf_backend_section_from_bfd_section)
6647 int retval = sec_index;
6649 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6653 if (sec_index == SHN_BAD)
6654 bfd_set_error (bfd_error_nonrepresentable_section);
6659 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6663 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6665 asymbol *asym_ptr = *asym_ptr_ptr;
6667 flagword flags = asym_ptr->flags;
6669 /* When gas creates relocations against local labels, it creates its
6670 own symbol for the section, but does put the symbol into the
6671 symbol chain, so udata is 0. When the linker is generating
6672 relocatable output, this section symbol may be for one of the
6673 input sections rather than the output section. */
6674 if (asym_ptr->udata.i == 0
6675 && (flags & BSF_SECTION_SYM)
6676 && asym_ptr->section)
6681 sec = asym_ptr->section;
6682 if (sec->owner != abfd && sec->output_section != NULL)
6683 sec = sec->output_section;
6684 if (sec->owner == abfd
6685 && (indx = sec->index) < elf_num_section_syms (abfd)
6686 && elf_section_syms (abfd)[indx] != NULL)
6687 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6690 idx = asym_ptr->udata.i;
6694 /* This case can occur when using --strip-symbol on a symbol
6695 which is used in a relocation entry. */
6697 /* xgettext:c-format */
6698 (_("%pB: symbol `%s' required but not present"),
6699 abfd, bfd_asymbol_name (asym_ptr));
6700 bfd_set_error (bfd_error_no_symbols);
6707 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6708 (long) asym_ptr, asym_ptr->name, idx, flags);
6716 /* Rewrite program header information. */
6719 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6721 Elf_Internal_Ehdr *iehdr;
6722 struct elf_segment_map *map;
6723 struct elf_segment_map *map_first;
6724 struct elf_segment_map **pointer_to_map;
6725 Elf_Internal_Phdr *segment;
6728 unsigned int num_segments;
6729 bfd_boolean phdr_included = FALSE;
6730 bfd_boolean p_paddr_valid;
6731 bfd_vma maxpagesize;
6732 struct elf_segment_map *phdr_adjust_seg = NULL;
6733 unsigned int phdr_adjust_num = 0;
6734 const struct elf_backend_data *bed;
6736 bed = get_elf_backend_data (ibfd);
6737 iehdr = elf_elfheader (ibfd);
6740 pointer_to_map = &map_first;
6742 num_segments = elf_elfheader (ibfd)->e_phnum;
6743 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6745 /* Returns the end address of the segment + 1. */
6746 #define SEGMENT_END(segment, start) \
6747 (start + (segment->p_memsz > segment->p_filesz \
6748 ? segment->p_memsz : segment->p_filesz))
6750 #define SECTION_SIZE(section, segment) \
6751 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6752 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6753 ? section->size : 0)
6755 /* Returns TRUE if the given section is contained within
6756 the given segment. VMA addresses are compared. */
6757 #define IS_CONTAINED_BY_VMA(section, segment) \
6758 (section->vma >= segment->p_vaddr \
6759 && (section->vma + SECTION_SIZE (section, segment) \
6760 <= (SEGMENT_END (segment, segment->p_vaddr))))
6762 /* Returns TRUE if the given section is contained within
6763 the given segment. LMA addresses are compared. */
6764 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6765 (section->lma >= base \
6766 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6767 && (section->lma + SECTION_SIZE (section, segment) \
6768 <= SEGMENT_END (segment, base)))
6770 /* Handle PT_NOTE segment. */
6771 #define IS_NOTE(p, s) \
6772 (p->p_type == PT_NOTE \
6773 && elf_section_type (s) == SHT_NOTE \
6774 && (bfd_vma) s->filepos >= p->p_offset \
6775 && ((bfd_vma) s->filepos + s->size \
6776 <= p->p_offset + p->p_filesz))
6778 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6780 #define IS_COREFILE_NOTE(p, s) \
6782 && bfd_get_format (ibfd) == bfd_core \
6786 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6787 linker, which generates a PT_INTERP section with p_vaddr and
6788 p_memsz set to 0. */
6789 #define IS_SOLARIS_PT_INTERP(p, s) \
6791 && p->p_paddr == 0 \
6792 && p->p_memsz == 0 \
6793 && p->p_filesz > 0 \
6794 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6796 && (bfd_vma) s->filepos >= p->p_offset \
6797 && ((bfd_vma) s->filepos + s->size \
6798 <= p->p_offset + p->p_filesz))
6800 /* Decide if the given section should be included in the given segment.
6801 A section will be included if:
6802 1. It is within the address space of the segment -- we use the LMA
6803 if that is set for the segment and the VMA otherwise,
6804 2. It is an allocated section or a NOTE section in a PT_NOTE
6806 3. There is an output section associated with it,
6807 4. The section has not already been allocated to a previous segment.
6808 5. PT_GNU_STACK segments do not include any sections.
6809 6. PT_TLS segment includes only SHF_TLS sections.
6810 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6811 8. PT_DYNAMIC should not contain empty sections at the beginning
6812 (with the possible exception of .dynamic). */
6813 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6814 ((((segment->p_paddr \
6815 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6816 : IS_CONTAINED_BY_VMA (section, segment)) \
6817 && (section->flags & SEC_ALLOC) != 0) \
6818 || IS_NOTE (segment, section)) \
6819 && segment->p_type != PT_GNU_STACK \
6820 && (segment->p_type != PT_TLS \
6821 || (section->flags & SEC_THREAD_LOCAL)) \
6822 && (segment->p_type == PT_LOAD \
6823 || segment->p_type == PT_TLS \
6824 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6825 && (segment->p_type != PT_DYNAMIC \
6826 || SECTION_SIZE (section, segment) > 0 \
6827 || (segment->p_paddr \
6828 ? segment->p_paddr != section->lma \
6829 : segment->p_vaddr != section->vma) \
6830 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6832 && (segment->p_type != PT_LOAD || !section->segment_mark))
6834 /* If the output section of a section in the input segment is NULL,
6835 it is removed from the corresponding output segment. */
6836 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6837 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6838 && section->output_section != NULL)
6840 /* Returns TRUE iff seg1 starts after the end of seg2. */
6841 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6842 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6844 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6845 their VMA address ranges and their LMA address ranges overlap.
6846 It is possible to have overlapping VMA ranges without overlapping LMA
6847 ranges. RedBoot images for example can have both .data and .bss mapped
6848 to the same VMA range, but with the .data section mapped to a different
6850 #define SEGMENT_OVERLAPS(seg1, seg2) \
6851 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6852 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6853 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6854 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6856 /* Initialise the segment mark field. */
6857 for (section = ibfd->sections; section != NULL; section = section->next)
6858 section->segment_mark = FALSE;
6860 /* The Solaris linker creates program headers in which all the
6861 p_paddr fields are zero. When we try to objcopy or strip such a
6862 file, we get confused. Check for this case, and if we find it
6863 don't set the p_paddr_valid fields. */
6864 p_paddr_valid = FALSE;
6865 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6868 if (segment->p_paddr != 0)
6870 p_paddr_valid = TRUE;
6874 /* Scan through the segments specified in the program header
6875 of the input BFD. For this first scan we look for overlaps
6876 in the loadable segments. These can be created by weird
6877 parameters to objcopy. Also, fix some solaris weirdness. */
6878 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6883 Elf_Internal_Phdr *segment2;
6885 if (segment->p_type == PT_INTERP)
6886 for (section = ibfd->sections; section; section = section->next)
6887 if (IS_SOLARIS_PT_INTERP (segment, section))
6889 /* Mininal change so that the normal section to segment
6890 assignment code will work. */
6891 segment->p_vaddr = section->vma;
6895 if (segment->p_type != PT_LOAD)
6897 /* Remove PT_GNU_RELRO segment. */
6898 if (segment->p_type == PT_GNU_RELRO)
6899 segment->p_type = PT_NULL;
6903 /* Determine if this segment overlaps any previous segments. */
6904 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6906 bfd_signed_vma extra_length;
6908 if (segment2->p_type != PT_LOAD
6909 || !SEGMENT_OVERLAPS (segment, segment2))
6912 /* Merge the two segments together. */
6913 if (segment2->p_vaddr < segment->p_vaddr)
6915 /* Extend SEGMENT2 to include SEGMENT and then delete
6917 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6918 - SEGMENT_END (segment2, segment2->p_vaddr));
6920 if (extra_length > 0)
6922 segment2->p_memsz += extra_length;
6923 segment2->p_filesz += extra_length;
6926 segment->p_type = PT_NULL;
6928 /* Since we have deleted P we must restart the outer loop. */
6930 segment = elf_tdata (ibfd)->phdr;
6935 /* Extend SEGMENT to include SEGMENT2 and then delete
6937 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6938 - SEGMENT_END (segment, segment->p_vaddr));
6940 if (extra_length > 0)
6942 segment->p_memsz += extra_length;
6943 segment->p_filesz += extra_length;
6946 segment2->p_type = PT_NULL;
6951 /* The second scan attempts to assign sections to segments. */
6952 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6956 unsigned int section_count;
6957 asection **sections;
6958 asection *output_section;
6960 asection *matching_lma;
6961 asection *suggested_lma;
6964 asection *first_section;
6966 if (segment->p_type == PT_NULL)
6969 first_section = NULL;
6970 /* Compute how many sections might be placed into this segment. */
6971 for (section = ibfd->sections, section_count = 0;
6973 section = section->next)
6975 /* Find the first section in the input segment, which may be
6976 removed from the corresponding output segment. */
6977 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6979 if (first_section == NULL)
6980 first_section = section;
6981 if (section->output_section != NULL)
6986 /* Allocate a segment map big enough to contain
6987 all of the sections we have selected. */
6988 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6989 amt += (bfd_size_type) section_count * sizeof (asection *);
6990 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6994 /* Initialise the fields of the segment map. Default to
6995 using the physical address of the segment in the input BFD. */
6997 map->p_type = segment->p_type;
6998 map->p_flags = segment->p_flags;
6999 map->p_flags_valid = 1;
7001 /* If the first section in the input segment is removed, there is
7002 no need to preserve segment physical address in the corresponding
7004 if (!first_section || first_section->output_section != NULL)
7006 map->p_paddr = segment->p_paddr;
7007 map->p_paddr_valid = p_paddr_valid;
7010 /* Determine if this segment contains the ELF file header
7011 and if it contains the program headers themselves. */
7012 map->includes_filehdr = (segment->p_offset == 0
7013 && segment->p_filesz >= iehdr->e_ehsize);
7014 map->includes_phdrs = 0;
7016 if (!phdr_included || segment->p_type != PT_LOAD)
7018 map->includes_phdrs =
7019 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7020 && (segment->p_offset + segment->p_filesz
7021 >= ((bfd_vma) iehdr->e_phoff
7022 + iehdr->e_phnum * iehdr->e_phentsize)));
7024 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7025 phdr_included = TRUE;
7028 if (section_count == 0)
7030 /* Special segments, such as the PT_PHDR segment, may contain
7031 no sections, but ordinary, loadable segments should contain
7032 something. They are allowed by the ELF spec however, so only
7033 a warning is produced.
7034 There is however the valid use case of embedded systems which
7035 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7036 flash memory with zeros. No warning is shown for that case. */
7037 if (segment->p_type == PT_LOAD
7038 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7039 /* xgettext:c-format */
7041 (_("%pB: warning: empty loadable segment detected"
7042 " at vaddr=%#" PRIx64 ", is this intentional?"),
7043 ibfd, (uint64_t) segment->p_vaddr);
7045 map->p_vaddr_offset = segment->p_vaddr;
7047 *pointer_to_map = map;
7048 pointer_to_map = &map->next;
7053 /* Now scan the sections in the input BFD again and attempt
7054 to add their corresponding output sections to the segment map.
7055 The problem here is how to handle an output section which has
7056 been moved (ie had its LMA changed). There are four possibilities:
7058 1. None of the sections have been moved.
7059 In this case we can continue to use the segment LMA from the
7062 2. All of the sections have been moved by the same amount.
7063 In this case we can change the segment's LMA to match the LMA
7064 of the first section.
7066 3. Some of the sections have been moved, others have not.
7067 In this case those sections which have not been moved can be
7068 placed in the current segment which will have to have its size,
7069 and possibly its LMA changed, and a new segment or segments will
7070 have to be created to contain the other sections.
7072 4. The sections have been moved, but not by the same amount.
7073 In this case we can change the segment's LMA to match the LMA
7074 of the first section and we will have to create a new segment
7075 or segments to contain the other sections.
7077 In order to save time, we allocate an array to hold the section
7078 pointers that we are interested in. As these sections get assigned
7079 to a segment, they are removed from this array. */
7081 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
7082 if (sections == NULL)
7085 /* Step One: Scan for segment vs section LMA conflicts.
7086 Also add the sections to the section array allocated above.
7087 Also add the sections to the current segment. In the common
7088 case, where the sections have not been moved, this means that
7089 we have completely filled the segment, and there is nothing
7092 matching_lma = NULL;
7093 suggested_lma = NULL;
7095 for (section = first_section, j = 0;
7097 section = section->next)
7099 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
7101 output_section = section->output_section;
7103 sections[j++] = section;
7105 /* The Solaris native linker always sets p_paddr to 0.
7106 We try to catch that case here, and set it to the
7107 correct value. Note - some backends require that
7108 p_paddr be left as zero. */
7110 && segment->p_vaddr != 0
7111 && !bed->want_p_paddr_set_to_zero
7113 && output_section->lma != 0
7114 && (align_power (segment->p_vaddr
7115 + (map->includes_filehdr
7116 ? iehdr->e_ehsize : 0)
7117 + (map->includes_phdrs
7118 ? iehdr->e_phnum * iehdr->e_phentsize
7120 output_section->alignment_power)
7121 == output_section->vma))
7122 map->p_paddr = segment->p_vaddr;
7124 /* Match up the physical address of the segment with the
7125 LMA address of the output section. */
7126 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7127 || IS_COREFILE_NOTE (segment, section)
7128 || (bed->want_p_paddr_set_to_zero
7129 && IS_CONTAINED_BY_VMA (output_section, segment)))
7131 if (matching_lma == NULL
7132 || output_section->lma < matching_lma->lma)
7133 matching_lma = output_section;
7135 /* We assume that if the section fits within the segment
7136 then it does not overlap any other section within that
7138 map->sections[isec++] = output_section;
7140 else if (suggested_lma == NULL)
7141 suggested_lma = output_section;
7143 if (j == section_count)
7148 BFD_ASSERT (j == section_count);
7150 /* Step Two: Adjust the physical address of the current segment,
7152 if (isec == section_count)
7154 /* All of the sections fitted within the segment as currently
7155 specified. This is the default case. Add the segment to
7156 the list of built segments and carry on to process the next
7157 program header in the input BFD. */
7158 map->count = section_count;
7159 *pointer_to_map = map;
7160 pointer_to_map = &map->next;
7163 && !bed->want_p_paddr_set_to_zero
7164 && matching_lma->lma != map->p_paddr
7165 && !map->includes_filehdr
7166 && !map->includes_phdrs)
7167 /* There is some padding before the first section in the
7168 segment. So, we must account for that in the output
7170 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
7177 /* Change the current segment's physical address to match
7178 the LMA of the first section that fitted, or if no
7179 section fitted, the first section. */
7180 if (matching_lma == NULL)
7181 matching_lma = suggested_lma;
7183 map->p_paddr = matching_lma->lma;
7185 /* Offset the segment physical address from the lma
7186 to allow for space taken up by elf headers. */
7187 if (map->includes_phdrs)
7189 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7191 /* iehdr->e_phnum is just an estimate of the number
7192 of program headers that we will need. Make a note
7193 here of the number we used and the segment we chose
7194 to hold these headers, so that we can adjust the
7195 offset when we know the correct value. */
7196 phdr_adjust_num = iehdr->e_phnum;
7197 phdr_adjust_seg = map;
7200 if (map->includes_filehdr)
7202 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7203 map->p_paddr -= iehdr->e_ehsize;
7204 /* We've subtracted off the size of headers from the
7205 first section lma, but there may have been some
7206 alignment padding before that section too. Try to
7207 account for that by adjusting the segment lma down to
7208 the same alignment. */
7209 if (segment->p_align != 0 && segment->p_align < align)
7210 align = segment->p_align;
7211 map->p_paddr &= -align;
7215 /* Step Three: Loop over the sections again, this time assigning
7216 those that fit to the current segment and removing them from the
7217 sections array; but making sure not to leave large gaps. Once all
7218 possible sections have been assigned to the current segment it is
7219 added to the list of built segments and if sections still remain
7220 to be assigned, a new segment is constructed before repeating
7226 suggested_lma = NULL;
7228 /* Fill the current segment with sections that fit. */
7229 for (j = 0; j < section_count; j++)
7231 section = sections[j];
7233 if (section == NULL)
7236 output_section = section->output_section;
7238 BFD_ASSERT (output_section != NULL);
7240 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7241 || IS_COREFILE_NOTE (segment, section))
7243 if (map->count == 0)
7245 /* If the first section in a segment does not start at
7246 the beginning of the segment, then something is
7248 if (align_power (map->p_paddr
7249 + (map->includes_filehdr
7250 ? iehdr->e_ehsize : 0)
7251 + (map->includes_phdrs
7252 ? iehdr->e_phnum * iehdr->e_phentsize
7254 output_section->alignment_power)
7255 != output_section->lma)
7262 prev_sec = map->sections[map->count - 1];
7264 /* If the gap between the end of the previous section
7265 and the start of this section is more than
7266 maxpagesize then we need to start a new segment. */
7267 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7269 < BFD_ALIGN (output_section->lma, maxpagesize))
7270 || (prev_sec->lma + prev_sec->size
7271 > output_section->lma))
7273 if (suggested_lma == NULL)
7274 suggested_lma = output_section;
7280 map->sections[map->count++] = output_section;
7283 if (segment->p_type == PT_LOAD)
7284 section->segment_mark = TRUE;
7286 else if (suggested_lma == NULL)
7287 suggested_lma = output_section;
7290 /* PR 23932. A corrupt input file may contain sections that cannot
7291 be assigned to any segment - because for example they have a
7292 negative size - or segments that do not contain any sections. */
7293 if (map->count == 0)
7295 bfd_set_error (bfd_error_bad_value);
7300 /* Add the current segment to the list of built segments. */
7301 *pointer_to_map = map;
7302 pointer_to_map = &map->next;
7304 if (isec < section_count)
7306 /* We still have not allocated all of the sections to
7307 segments. Create a new segment here, initialise it
7308 and carry on looping. */
7309 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7310 amt += (bfd_size_type) section_count * sizeof (asection *);
7311 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7318 /* Initialise the fields of the segment map. Set the physical
7319 physical address to the LMA of the first section that has
7320 not yet been assigned. */
7322 map->p_type = segment->p_type;
7323 map->p_flags = segment->p_flags;
7324 map->p_flags_valid = 1;
7325 map->p_paddr = suggested_lma->lma;
7326 map->p_paddr_valid = p_paddr_valid;
7327 map->includes_filehdr = 0;
7328 map->includes_phdrs = 0;
7331 while (isec < section_count);
7336 elf_seg_map (obfd) = map_first;
7338 /* If we had to estimate the number of program headers that were
7339 going to be needed, then check our estimate now and adjust
7340 the offset if necessary. */
7341 if (phdr_adjust_seg != NULL)
7345 for (count = 0, map = map_first; map != NULL; map = map->next)
7348 if (count > phdr_adjust_num)
7349 phdr_adjust_seg->p_paddr
7350 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7352 for (map = map_first; map != NULL; map = map->next)
7353 if (map->p_type == PT_PHDR)
7356 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7357 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7364 #undef IS_CONTAINED_BY_VMA
7365 #undef IS_CONTAINED_BY_LMA
7367 #undef IS_COREFILE_NOTE
7368 #undef IS_SOLARIS_PT_INTERP
7369 #undef IS_SECTION_IN_INPUT_SEGMENT
7370 #undef INCLUDE_SECTION_IN_SEGMENT
7371 #undef SEGMENT_AFTER_SEGMENT
7372 #undef SEGMENT_OVERLAPS
7376 /* Copy ELF program header information. */
7379 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7381 Elf_Internal_Ehdr *iehdr;
7382 struct elf_segment_map *map;
7383 struct elf_segment_map *map_first;
7384 struct elf_segment_map **pointer_to_map;
7385 Elf_Internal_Phdr *segment;
7387 unsigned int num_segments;
7388 bfd_boolean phdr_included = FALSE;
7389 bfd_boolean p_paddr_valid;
7391 iehdr = elf_elfheader (ibfd);
7394 pointer_to_map = &map_first;
7396 /* If all the segment p_paddr fields are zero, don't set
7397 map->p_paddr_valid. */
7398 p_paddr_valid = FALSE;
7399 num_segments = elf_elfheader (ibfd)->e_phnum;
7400 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7403 if (segment->p_paddr != 0)
7405 p_paddr_valid = TRUE;
7409 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7414 unsigned int section_count;
7416 Elf_Internal_Shdr *this_hdr;
7417 asection *first_section = NULL;
7418 asection *lowest_section;
7419 bfd_boolean no_contents = TRUE;
7421 /* Compute how many sections are in this segment. */
7422 for (section = ibfd->sections, section_count = 0;
7424 section = section->next)
7426 this_hdr = &(elf_section_data(section)->this_hdr);
7427 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7429 if (first_section == NULL)
7430 first_section = section;
7431 if (elf_section_type (section) != SHT_NOBITS)
7432 no_contents = FALSE;
7437 /* Allocate a segment map big enough to contain
7438 all of the sections we have selected. */
7439 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7440 amt += (bfd_size_type) section_count * sizeof (asection *);
7441 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7445 /* Initialize the fields of the output segment map with the
7448 map->p_type = segment->p_type;
7449 map->p_flags = segment->p_flags;
7450 map->p_flags_valid = 1;
7451 map->p_paddr = segment->p_paddr;
7452 map->p_paddr_valid = p_paddr_valid;
7453 map->p_align = segment->p_align;
7454 map->p_align_valid = 1;
7455 map->p_vaddr_offset = 0;
7457 if (map->p_type == PT_GNU_RELRO
7458 || map->p_type == PT_GNU_STACK)
7460 /* The PT_GNU_RELRO segment may contain the first a few
7461 bytes in the .got.plt section even if the whole .got.plt
7462 section isn't in the PT_GNU_RELRO segment. We won't
7463 change the size of the PT_GNU_RELRO segment.
7464 Similarly, PT_GNU_STACK size is significant on uclinux
7466 map->p_size = segment->p_memsz;
7467 map->p_size_valid = 1;
7470 /* Determine if this segment contains the ELF file header
7471 and if it contains the program headers themselves. */
7472 map->includes_filehdr = (segment->p_offset == 0
7473 && segment->p_filesz >= iehdr->e_ehsize);
7475 map->includes_phdrs = 0;
7476 if (! phdr_included || segment->p_type != PT_LOAD)
7478 map->includes_phdrs =
7479 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7480 && (segment->p_offset + segment->p_filesz
7481 >= ((bfd_vma) iehdr->e_phoff
7482 + iehdr->e_phnum * iehdr->e_phentsize)));
7484 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7485 phdr_included = TRUE;
7488 lowest_section = NULL;
7489 if (section_count != 0)
7491 unsigned int isec = 0;
7493 for (section = first_section;
7495 section = section->next)
7497 this_hdr = &(elf_section_data(section)->this_hdr);
7498 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7500 map->sections[isec++] = section->output_section;
7501 if ((section->flags & SEC_ALLOC) != 0)
7505 if (lowest_section == NULL
7506 || section->lma < lowest_section->lma)
7507 lowest_section = section;
7509 /* Section lmas are set up from PT_LOAD header
7510 p_paddr in _bfd_elf_make_section_from_shdr.
7511 If this header has a p_paddr that disagrees
7512 with the section lma, flag the p_paddr as
7514 if ((section->flags & SEC_LOAD) != 0)
7515 seg_off = this_hdr->sh_offset - segment->p_offset;
7517 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7518 if (section->lma - segment->p_paddr != seg_off)
7519 map->p_paddr_valid = FALSE;
7521 if (isec == section_count)
7527 if (map->includes_filehdr && lowest_section != NULL)
7529 /* Try to keep the space used by the headers plus any
7530 padding fixed. If there are sections with file contents
7531 in this segment then the lowest sh_offset is the best
7532 guess. Otherwise the segment only has file contents for
7533 the headers, and p_filesz is the best guess. */
7535 map->header_size = segment->p_filesz;
7537 map->header_size = lowest_section->filepos;
7540 if (section_count == 0)
7541 map->p_vaddr_offset = segment->p_vaddr;
7542 else if (!map->includes_phdrs
7543 && !map->includes_filehdr
7544 && map->p_paddr_valid)
7545 /* Account for padding before the first section. */
7546 map->p_vaddr_offset = (segment->p_paddr
7547 - (lowest_section ? lowest_section->lma : 0));
7549 map->count = section_count;
7550 *pointer_to_map = map;
7551 pointer_to_map = &map->next;
7554 elf_seg_map (obfd) = map_first;
7558 /* Copy private BFD data. This copies or rewrites ELF program header
7562 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7564 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7565 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7568 if (elf_tdata (ibfd)->phdr == NULL)
7571 if (ibfd->xvec == obfd->xvec)
7573 /* Check to see if any sections in the input BFD
7574 covered by ELF program header have changed. */
7575 Elf_Internal_Phdr *segment;
7576 asection *section, *osec;
7577 unsigned int i, num_segments;
7578 Elf_Internal_Shdr *this_hdr;
7579 const struct elf_backend_data *bed;
7581 bed = get_elf_backend_data (ibfd);
7583 /* Regenerate the segment map if p_paddr is set to 0. */
7584 if (bed->want_p_paddr_set_to_zero)
7587 /* Initialize the segment mark field. */
7588 for (section = obfd->sections; section != NULL;
7589 section = section->next)
7590 section->segment_mark = FALSE;
7592 num_segments = elf_elfheader (ibfd)->e_phnum;
7593 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7597 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7598 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7599 which severly confuses things, so always regenerate the segment
7600 map in this case. */
7601 if (segment->p_paddr == 0
7602 && segment->p_memsz == 0
7603 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7606 for (section = ibfd->sections;
7607 section != NULL; section = section->next)
7609 /* We mark the output section so that we know it comes
7610 from the input BFD. */
7611 osec = section->output_section;
7613 osec->segment_mark = TRUE;
7615 /* Check if this section is covered by the segment. */
7616 this_hdr = &(elf_section_data(section)->this_hdr);
7617 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7619 /* FIXME: Check if its output section is changed or
7620 removed. What else do we need to check? */
7622 || section->flags != osec->flags
7623 || section->lma != osec->lma
7624 || section->vma != osec->vma
7625 || section->size != osec->size
7626 || section->rawsize != osec->rawsize
7627 || section->alignment_power != osec->alignment_power)
7633 /* Check to see if any output section do not come from the
7635 for (section = obfd->sections; section != NULL;
7636 section = section->next)
7638 if (!section->segment_mark)
7641 section->segment_mark = FALSE;
7644 return copy_elf_program_header (ibfd, obfd);
7648 if (ibfd->xvec == obfd->xvec)
7650 /* When rewriting program header, set the output maxpagesize to
7651 the maximum alignment of input PT_LOAD segments. */
7652 Elf_Internal_Phdr *segment;
7654 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7655 bfd_vma maxpagesize = 0;
7657 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7660 if (segment->p_type == PT_LOAD
7661 && maxpagesize < segment->p_align)
7663 /* PR 17512: file: f17299af. */
7664 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7665 /* xgettext:c-format */
7666 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7667 PRIx64 " is too large"),
7668 ibfd, (uint64_t) segment->p_align);
7670 maxpagesize = segment->p_align;
7673 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7674 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7677 return rewrite_elf_program_header (ibfd, obfd);
7680 /* Initialize private output section information from input section. */
7683 _bfd_elf_init_private_section_data (bfd *ibfd,
7687 struct bfd_link_info *link_info)
7690 Elf_Internal_Shdr *ihdr, *ohdr;
7691 bfd_boolean final_link = (link_info != NULL
7692 && !bfd_link_relocatable (link_info));
7694 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7695 || obfd->xvec->flavour != bfd_target_elf_flavour)
7698 BFD_ASSERT (elf_section_data (osec) != NULL);
7700 /* For objcopy and relocatable link, don't copy the output ELF
7701 section type from input if the output BFD section flags have been
7702 set to something different. For a final link allow some flags
7703 that the linker clears to differ. */
7704 if (elf_section_type (osec) == SHT_NULL
7705 && (osec->flags == isec->flags
7707 && ((osec->flags ^ isec->flags)
7708 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7709 elf_section_type (osec) = elf_section_type (isec);
7711 /* FIXME: Is this correct for all OS/PROC specific flags? */
7712 elf_section_flags (osec) |= (elf_section_flags (isec)
7713 & (SHF_MASKOS | SHF_MASKPROC));
7715 /* Copy sh_info from input for mbind section. */
7716 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7717 && elf_section_flags (isec) & SHF_GNU_MBIND)
7718 elf_section_data (osec)->this_hdr.sh_info
7719 = elf_section_data (isec)->this_hdr.sh_info;
7721 /* Set things up for objcopy and relocatable link. The output
7722 SHT_GROUP section will have its elf_next_in_group pointing back
7723 to the input group members. Ignore linker created group section.
7724 See elfNN_ia64_object_p in elfxx-ia64.c. */
7725 if ((link_info == NULL
7726 || !link_info->resolve_section_groups)
7727 && (elf_sec_group (isec) == NULL
7728 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7730 if (elf_section_flags (isec) & SHF_GROUP)
7731 elf_section_flags (osec) |= SHF_GROUP;
7732 elf_next_in_group (osec) = elf_next_in_group (isec);
7733 elf_section_data (osec)->group = elf_section_data (isec)->group;
7736 /* If not decompress, preserve SHF_COMPRESSED. */
7737 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7738 elf_section_flags (osec) |= (elf_section_flags (isec)
7741 ihdr = &elf_section_data (isec)->this_hdr;
7743 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7744 don't use the output section of the linked-to section since it
7745 may be NULL at this point. */
7746 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7748 ohdr = &elf_section_data (osec)->this_hdr;
7749 ohdr->sh_flags |= SHF_LINK_ORDER;
7750 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7753 osec->use_rela_p = isec->use_rela_p;
7758 /* Copy private section information. This copies over the entsize
7759 field, and sometimes the info field. */
7762 _bfd_elf_copy_private_section_data (bfd *ibfd,
7767 Elf_Internal_Shdr *ihdr, *ohdr;
7769 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7770 || obfd->xvec->flavour != bfd_target_elf_flavour)
7773 ihdr = &elf_section_data (isec)->this_hdr;
7774 ohdr = &elf_section_data (osec)->this_hdr;
7776 ohdr->sh_entsize = ihdr->sh_entsize;
7778 if (ihdr->sh_type == SHT_SYMTAB
7779 || ihdr->sh_type == SHT_DYNSYM
7780 || ihdr->sh_type == SHT_GNU_verneed
7781 || ihdr->sh_type == SHT_GNU_verdef)
7782 ohdr->sh_info = ihdr->sh_info;
7784 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7788 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7789 necessary if we are removing either the SHT_GROUP section or any of
7790 the group member sections. DISCARDED is the value that a section's
7791 output_section has if the section will be discarded, NULL when this
7792 function is called from objcopy, bfd_abs_section_ptr when called
7796 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7800 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7801 if (elf_section_type (isec) == SHT_GROUP)
7803 asection *first = elf_next_in_group (isec);
7804 asection *s = first;
7805 bfd_size_type removed = 0;
7809 /* If this member section is being output but the
7810 SHT_GROUP section is not, then clear the group info
7811 set up by _bfd_elf_copy_private_section_data. */
7812 if (s->output_section != discarded
7813 && isec->output_section == discarded)
7815 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7816 elf_group_name (s->output_section) = NULL;
7818 /* Conversely, if the member section is not being output
7819 but the SHT_GROUP section is, then adjust its size. */
7820 else if (s->output_section == discarded
7821 && isec->output_section != discarded)
7823 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7825 if (elf_sec->rel.hdr != NULL
7826 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7828 if (elf_sec->rela.hdr != NULL
7829 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7832 s = elf_next_in_group (s);
7838 if (discarded != NULL)
7840 /* If we've been called for ld -r, then we need to
7841 adjust the input section size. */
7842 if (isec->rawsize == 0)
7843 isec->rawsize = isec->size;
7844 isec->size = isec->rawsize - removed;
7845 if (isec->size <= 4)
7848 isec->flags |= SEC_EXCLUDE;
7853 /* Adjust the output section size when called from
7855 isec->output_section->size -= removed;
7856 if (isec->output_section->size <= 4)
7858 isec->output_section->size = 0;
7859 isec->output_section->flags |= SEC_EXCLUDE;
7868 /* Copy private header information. */
7871 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7873 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7874 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7877 /* Copy over private BFD data if it has not already been copied.
7878 This must be done here, rather than in the copy_private_bfd_data
7879 entry point, because the latter is called after the section
7880 contents have been set, which means that the program headers have
7881 already been worked out. */
7882 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7884 if (! copy_private_bfd_data (ibfd, obfd))
7888 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7891 /* Copy private symbol information. If this symbol is in a section
7892 which we did not map into a BFD section, try to map the section
7893 index correctly. We use special macro definitions for the mapped
7894 section indices; these definitions are interpreted by the
7895 swap_out_syms function. */
7897 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7898 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7899 #define MAP_STRTAB (SHN_HIOS + 3)
7900 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7901 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7904 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7909 elf_symbol_type *isym, *osym;
7911 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7912 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7915 isym = elf_symbol_from (ibfd, isymarg);
7916 osym = elf_symbol_from (obfd, osymarg);
7919 && isym->internal_elf_sym.st_shndx != 0
7921 && bfd_is_abs_section (isym->symbol.section))
7925 shndx = isym->internal_elf_sym.st_shndx;
7926 if (shndx == elf_onesymtab (ibfd))
7927 shndx = MAP_ONESYMTAB;
7928 else if (shndx == elf_dynsymtab (ibfd))
7929 shndx = MAP_DYNSYMTAB;
7930 else if (shndx == elf_strtab_sec (ibfd))
7932 else if (shndx == elf_shstrtab_sec (ibfd))
7933 shndx = MAP_SHSTRTAB;
7934 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7935 shndx = MAP_SYM_SHNDX;
7936 osym->internal_elf_sym.st_shndx = shndx;
7942 /* Swap out the symbols. */
7945 swap_out_syms (bfd *abfd,
7946 struct elf_strtab_hash **sttp,
7949 const struct elf_backend_data *bed;
7952 struct elf_strtab_hash *stt;
7953 Elf_Internal_Shdr *symtab_hdr;
7954 Elf_Internal_Shdr *symtab_shndx_hdr;
7955 Elf_Internal_Shdr *symstrtab_hdr;
7956 struct elf_sym_strtab *symstrtab;
7957 bfd_byte *outbound_syms;
7958 bfd_byte *outbound_shndx;
7959 unsigned long outbound_syms_index;
7960 unsigned long outbound_shndx_index;
7962 unsigned int num_locals;
7964 bfd_boolean name_local_sections;
7966 if (!elf_map_symbols (abfd, &num_locals))
7969 /* Dump out the symtabs. */
7970 stt = _bfd_elf_strtab_init ();
7974 bed = get_elf_backend_data (abfd);
7975 symcount = bfd_get_symcount (abfd);
7976 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7977 symtab_hdr->sh_type = SHT_SYMTAB;
7978 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7979 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7980 symtab_hdr->sh_info = num_locals + 1;
7981 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7983 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7984 symstrtab_hdr->sh_type = SHT_STRTAB;
7986 /* Allocate buffer to swap out the .strtab section. */
7987 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
7988 sizeof (*symstrtab));
7989 if (symstrtab == NULL)
7991 _bfd_elf_strtab_free (stt);
7995 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7996 bed->s->sizeof_sym);
7997 if (outbound_syms == NULL)
8000 _bfd_elf_strtab_free (stt);
8004 symtab_hdr->contents = outbound_syms;
8005 outbound_syms_index = 0;
8007 outbound_shndx = NULL;
8008 outbound_shndx_index = 0;
8010 if (elf_symtab_shndx_list (abfd))
8012 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8013 if (symtab_shndx_hdr->sh_name != 0)
8015 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
8016 outbound_shndx = (bfd_byte *)
8017 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
8018 if (outbound_shndx == NULL)
8021 symtab_shndx_hdr->contents = outbound_shndx;
8022 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8023 symtab_shndx_hdr->sh_size = amt;
8024 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8025 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8027 /* FIXME: What about any other headers in the list ? */
8030 /* Now generate the data (for "contents"). */
8032 /* Fill in zeroth symbol and swap it out. */
8033 Elf_Internal_Sym sym;
8039 sym.st_shndx = SHN_UNDEF;
8040 sym.st_target_internal = 0;
8041 symstrtab[0].sym = sym;
8042 symstrtab[0].dest_index = outbound_syms_index;
8043 symstrtab[0].destshndx_index = outbound_shndx_index;
8044 outbound_syms_index++;
8045 if (outbound_shndx != NULL)
8046 outbound_shndx_index++;
8050 = (bed->elf_backend_name_local_section_symbols
8051 && bed->elf_backend_name_local_section_symbols (abfd));
8053 syms = bfd_get_outsymbols (abfd);
8054 for (idx = 0; idx < symcount;)
8056 Elf_Internal_Sym sym;
8057 bfd_vma value = syms[idx]->value;
8058 elf_symbol_type *type_ptr;
8059 flagword flags = syms[idx]->flags;
8062 if (!name_local_sections
8063 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8065 /* Local section symbols have no name. */
8066 sym.st_name = (unsigned long) -1;
8070 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8071 to get the final offset for st_name. */
8073 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8075 if (sym.st_name == (unsigned long) -1)
8079 type_ptr = elf_symbol_from (abfd, syms[idx]);
8081 if ((flags & BSF_SECTION_SYM) == 0
8082 && bfd_is_com_section (syms[idx]->section))
8084 /* ELF common symbols put the alignment into the `value' field,
8085 and the size into the `size' field. This is backwards from
8086 how BFD handles it, so reverse it here. */
8087 sym.st_size = value;
8088 if (type_ptr == NULL
8089 || type_ptr->internal_elf_sym.st_value == 0)
8090 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8092 sym.st_value = type_ptr->internal_elf_sym.st_value;
8093 sym.st_shndx = _bfd_elf_section_from_bfd_section
8094 (abfd, syms[idx]->section);
8098 asection *sec = syms[idx]->section;
8101 if (sec->output_section)
8103 value += sec->output_offset;
8104 sec = sec->output_section;
8107 /* Don't add in the section vma for relocatable output. */
8108 if (! relocatable_p)
8110 sym.st_value = value;
8111 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8113 if (bfd_is_abs_section (sec)
8115 && type_ptr->internal_elf_sym.st_shndx != 0)
8117 /* This symbol is in a real ELF section which we did
8118 not create as a BFD section. Undo the mapping done
8119 by copy_private_symbol_data. */
8120 shndx = type_ptr->internal_elf_sym.st_shndx;
8124 shndx = elf_onesymtab (abfd);
8127 shndx = elf_dynsymtab (abfd);
8130 shndx = elf_strtab_sec (abfd);
8133 shndx = elf_shstrtab_sec (abfd);
8136 if (elf_symtab_shndx_list (abfd))
8137 shndx = elf_symtab_shndx_list (abfd)->ndx;
8146 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8148 if (shndx == SHN_BAD)
8152 /* Writing this would be a hell of a lot easier if
8153 we had some decent documentation on bfd, and
8154 knew what to expect of the library, and what to
8155 demand of applications. For example, it
8156 appears that `objcopy' might not set the
8157 section of a symbol to be a section that is
8158 actually in the output file. */
8159 sec2 = bfd_get_section_by_name (abfd, sec->name);
8161 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8162 if (shndx == SHN_BAD)
8164 /* xgettext:c-format */
8166 (_("unable to find equivalent output section"
8167 " for symbol '%s' from section '%s'"),
8168 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8170 bfd_set_error (bfd_error_invalid_operation);
8176 sym.st_shndx = shndx;
8179 if ((flags & BSF_THREAD_LOCAL) != 0)
8181 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8182 type = STT_GNU_IFUNC;
8183 else if ((flags & BSF_FUNCTION) != 0)
8185 else if ((flags & BSF_OBJECT) != 0)
8187 else if ((flags & BSF_RELC) != 0)
8189 else if ((flags & BSF_SRELC) != 0)
8194 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8197 /* Processor-specific types. */
8198 if (type_ptr != NULL
8199 && bed->elf_backend_get_symbol_type)
8200 type = ((*bed->elf_backend_get_symbol_type)
8201 (&type_ptr->internal_elf_sym, type));
8203 if (flags & BSF_SECTION_SYM)
8205 if (flags & BSF_GLOBAL)
8206 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8208 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8210 else if (bfd_is_com_section (syms[idx]->section))
8212 if (type != STT_TLS)
8214 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8215 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8216 ? STT_COMMON : STT_OBJECT);
8218 type = ((flags & BSF_ELF_COMMON) != 0
8219 ? STT_COMMON : STT_OBJECT);
8221 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8223 else if (bfd_is_und_section (syms[idx]->section))
8224 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8228 else if (flags & BSF_FILE)
8229 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8232 int bind = STB_LOCAL;
8234 if (flags & BSF_LOCAL)
8236 else if (flags & BSF_GNU_UNIQUE)
8237 bind = STB_GNU_UNIQUE;
8238 else if (flags & BSF_WEAK)
8240 else if (flags & BSF_GLOBAL)
8243 sym.st_info = ELF_ST_INFO (bind, type);
8246 if (type_ptr != NULL)
8248 sym.st_other = type_ptr->internal_elf_sym.st_other;
8249 sym.st_target_internal
8250 = type_ptr->internal_elf_sym.st_target_internal;
8255 sym.st_target_internal = 0;
8259 symstrtab[idx].sym = sym;
8260 symstrtab[idx].dest_index = outbound_syms_index;
8261 symstrtab[idx].destshndx_index = outbound_shndx_index;
8263 outbound_syms_index++;
8264 if (outbound_shndx != NULL)
8265 outbound_shndx_index++;
8268 /* Finalize the .strtab section. */
8269 _bfd_elf_strtab_finalize (stt);
8271 /* Swap out the .strtab section. */
8272 for (idx = 0; idx <= symcount; idx++)
8274 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8275 if (elfsym->sym.st_name == (unsigned long) -1)
8276 elfsym->sym.st_name = 0;
8278 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8279 elfsym->sym.st_name);
8280 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8282 + (elfsym->dest_index
8283 * bed->s->sizeof_sym)),
8285 + (elfsym->destshndx_index
8286 * sizeof (Elf_External_Sym_Shndx))));
8291 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8292 symstrtab_hdr->sh_type = SHT_STRTAB;
8293 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8294 symstrtab_hdr->sh_addr = 0;
8295 symstrtab_hdr->sh_entsize = 0;
8296 symstrtab_hdr->sh_link = 0;
8297 symstrtab_hdr->sh_info = 0;
8298 symstrtab_hdr->sh_addralign = 1;
8303 /* Return the number of bytes required to hold the symtab vector.
8305 Note that we base it on the count plus 1, since we will null terminate
8306 the vector allocated based on this size. However, the ELF symbol table
8307 always has a dummy entry as symbol #0, so it ends up even. */
8310 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8312 bfd_size_type symcount;
8314 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8316 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8317 if (symcount >= LONG_MAX / sizeof (asymbol *))
8319 bfd_set_error (bfd_error_file_too_big);
8322 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8324 symtab_size -= sizeof (asymbol *);
8330 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8332 bfd_size_type symcount;
8334 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8336 if (elf_dynsymtab (abfd) == 0)
8338 bfd_set_error (bfd_error_invalid_operation);
8342 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8343 if (symcount >= LONG_MAX / sizeof (asymbol *))
8345 bfd_set_error (bfd_error_file_too_big);
8348 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8350 symtab_size -= sizeof (asymbol *);
8356 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8359 #if SIZEOF_LONG == SIZEOF_INT
8360 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8362 bfd_set_error (bfd_error_file_too_big);
8366 return (asect->reloc_count + 1) * sizeof (arelent *);
8369 /* Canonicalize the relocs. */
8372 _bfd_elf_canonicalize_reloc (bfd *abfd,
8379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8381 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8384 tblptr = section->relocation;
8385 for (i = 0; i < section->reloc_count; i++)
8386 *relptr++ = tblptr++;
8390 return section->reloc_count;
8394 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8396 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8397 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8400 bfd_get_symcount (abfd) = symcount;
8405 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8406 asymbol **allocation)
8408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8409 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8412 bfd_get_dynamic_symcount (abfd) = symcount;
8416 /* Return the size required for the dynamic reloc entries. Any loadable
8417 section that was actually installed in the BFD, and has type SHT_REL
8418 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8419 dynamic reloc section. */
8422 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8424 bfd_size_type count;
8427 if (elf_dynsymtab (abfd) == 0)
8429 bfd_set_error (bfd_error_invalid_operation);
8434 for (s = abfd->sections; s != NULL; s = s->next)
8435 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8436 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8437 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8439 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8440 if (count > LONG_MAX / sizeof (arelent *))
8442 bfd_set_error (bfd_error_file_too_big);
8446 return count * sizeof (arelent *);
8449 /* Canonicalize the dynamic relocation entries. Note that we return the
8450 dynamic relocations as a single block, although they are actually
8451 associated with particular sections; the interface, which was
8452 designed for SunOS style shared libraries, expects that there is only
8453 one set of dynamic relocs. Any loadable section that was actually
8454 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8455 dynamic symbol table, is considered to be a dynamic reloc section. */
8458 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8462 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8466 if (elf_dynsymtab (abfd) == 0)
8468 bfd_set_error (bfd_error_invalid_operation);
8472 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8474 for (s = abfd->sections; s != NULL; s = s->next)
8476 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8477 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8478 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8483 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8485 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8487 for (i = 0; i < count; i++)
8498 /* Read in the version information. */
8501 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8503 bfd_byte *contents = NULL;
8504 unsigned int freeidx = 0;
8506 if (elf_dynverref (abfd) != 0)
8508 Elf_Internal_Shdr *hdr;
8509 Elf_External_Verneed *everneed;
8510 Elf_Internal_Verneed *iverneed;
8512 bfd_byte *contents_end;
8514 hdr = &elf_tdata (abfd)->dynverref_hdr;
8516 if (hdr->sh_info == 0
8517 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8519 error_return_bad_verref:
8521 (_("%pB: .gnu.version_r invalid entry"), abfd);
8522 bfd_set_error (bfd_error_bad_value);
8523 error_return_verref:
8524 elf_tdata (abfd)->verref = NULL;
8525 elf_tdata (abfd)->cverrefs = 0;
8529 ufile_ptr filesize = bfd_get_file_size (abfd);
8530 if (filesize > 0 && filesize < hdr->sh_size)
8532 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8534 bfd_set_error (bfd_error_no_memory);
8536 /* xgettext:c-format */
8537 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8538 abfd, (uint64_t) hdr->sh_size);
8539 goto error_return_verref;
8541 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8542 if (contents == NULL)
8543 goto error_return_verref;
8545 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8546 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8547 goto error_return_verref;
8549 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8550 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8552 if (elf_tdata (abfd)->verref == NULL)
8553 goto error_return_verref;
8555 BFD_ASSERT (sizeof (Elf_External_Verneed)
8556 == sizeof (Elf_External_Vernaux));
8557 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8558 everneed = (Elf_External_Verneed *) contents;
8559 iverneed = elf_tdata (abfd)->verref;
8560 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8562 Elf_External_Vernaux *evernaux;
8563 Elf_Internal_Vernaux *ivernaux;
8566 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8568 iverneed->vn_bfd = abfd;
8570 iverneed->vn_filename =
8571 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8573 if (iverneed->vn_filename == NULL)
8574 goto error_return_bad_verref;
8576 if (iverneed->vn_cnt == 0)
8577 iverneed->vn_auxptr = NULL;
8580 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8581 bfd_alloc2 (abfd, iverneed->vn_cnt,
8582 sizeof (Elf_Internal_Vernaux));
8583 if (iverneed->vn_auxptr == NULL)
8584 goto error_return_verref;
8587 if (iverneed->vn_aux
8588 > (size_t) (contents_end - (bfd_byte *) everneed))
8589 goto error_return_bad_verref;
8591 evernaux = ((Elf_External_Vernaux *)
8592 ((bfd_byte *) everneed + iverneed->vn_aux));
8593 ivernaux = iverneed->vn_auxptr;
8594 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8596 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8598 ivernaux->vna_nodename =
8599 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8600 ivernaux->vna_name);
8601 if (ivernaux->vna_nodename == NULL)
8602 goto error_return_bad_verref;
8604 if (ivernaux->vna_other > freeidx)
8605 freeidx = ivernaux->vna_other;
8607 ivernaux->vna_nextptr = NULL;
8608 if (ivernaux->vna_next == 0)
8610 iverneed->vn_cnt = j + 1;
8613 if (j + 1 < iverneed->vn_cnt)
8614 ivernaux->vna_nextptr = ivernaux + 1;
8616 if (ivernaux->vna_next
8617 > (size_t) (contents_end - (bfd_byte *) evernaux))
8618 goto error_return_bad_verref;
8620 evernaux = ((Elf_External_Vernaux *)
8621 ((bfd_byte *) evernaux + ivernaux->vna_next));
8624 iverneed->vn_nextref = NULL;
8625 if (iverneed->vn_next == 0)
8627 if (i + 1 < hdr->sh_info)
8628 iverneed->vn_nextref = iverneed + 1;
8630 if (iverneed->vn_next
8631 > (size_t) (contents_end - (bfd_byte *) everneed))
8632 goto error_return_bad_verref;
8634 everneed = ((Elf_External_Verneed *)
8635 ((bfd_byte *) everneed + iverneed->vn_next));
8637 elf_tdata (abfd)->cverrefs = i;
8643 if (elf_dynverdef (abfd) != 0)
8645 Elf_Internal_Shdr *hdr;
8646 Elf_External_Verdef *everdef;
8647 Elf_Internal_Verdef *iverdef;
8648 Elf_Internal_Verdef *iverdefarr;
8649 Elf_Internal_Verdef iverdefmem;
8651 unsigned int maxidx;
8652 bfd_byte *contents_end_def, *contents_end_aux;
8654 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8656 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8658 error_return_bad_verdef:
8660 (_("%pB: .gnu.version_d invalid entry"), abfd);
8661 bfd_set_error (bfd_error_bad_value);
8662 error_return_verdef:
8663 elf_tdata (abfd)->verdef = NULL;
8664 elf_tdata (abfd)->cverdefs = 0;
8668 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8669 if (contents == NULL)
8670 goto error_return_verdef;
8671 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8672 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8673 goto error_return_verdef;
8675 BFD_ASSERT (sizeof (Elf_External_Verdef)
8676 >= sizeof (Elf_External_Verdaux));
8677 contents_end_def = contents + hdr->sh_size
8678 - sizeof (Elf_External_Verdef);
8679 contents_end_aux = contents + hdr->sh_size
8680 - sizeof (Elf_External_Verdaux);
8682 /* We know the number of entries in the section but not the maximum
8683 index. Therefore we have to run through all entries and find
8685 everdef = (Elf_External_Verdef *) contents;
8687 for (i = 0; i < hdr->sh_info; ++i)
8689 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8691 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8692 goto error_return_bad_verdef;
8693 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8694 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8696 if (iverdefmem.vd_next == 0)
8699 if (iverdefmem.vd_next
8700 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8701 goto error_return_bad_verdef;
8703 everdef = ((Elf_External_Verdef *)
8704 ((bfd_byte *) everdef + iverdefmem.vd_next));
8707 if (default_imported_symver)
8709 if (freeidx > maxidx)
8715 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8716 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8717 if (elf_tdata (abfd)->verdef == NULL)
8718 goto error_return_verdef;
8720 elf_tdata (abfd)->cverdefs = maxidx;
8722 everdef = (Elf_External_Verdef *) contents;
8723 iverdefarr = elf_tdata (abfd)->verdef;
8724 for (i = 0; i < hdr->sh_info; i++)
8726 Elf_External_Verdaux *everdaux;
8727 Elf_Internal_Verdaux *iverdaux;
8730 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8732 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8733 goto error_return_bad_verdef;
8735 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8736 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8738 iverdef->vd_bfd = abfd;
8740 if (iverdef->vd_cnt == 0)
8741 iverdef->vd_auxptr = NULL;
8744 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8745 bfd_alloc2 (abfd, iverdef->vd_cnt,
8746 sizeof (Elf_Internal_Verdaux));
8747 if (iverdef->vd_auxptr == NULL)
8748 goto error_return_verdef;
8752 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8753 goto error_return_bad_verdef;
8755 everdaux = ((Elf_External_Verdaux *)
8756 ((bfd_byte *) everdef + iverdef->vd_aux));
8757 iverdaux = iverdef->vd_auxptr;
8758 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8760 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8762 iverdaux->vda_nodename =
8763 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8764 iverdaux->vda_name);
8765 if (iverdaux->vda_nodename == NULL)
8766 goto error_return_bad_verdef;
8768 iverdaux->vda_nextptr = NULL;
8769 if (iverdaux->vda_next == 0)
8771 iverdef->vd_cnt = j + 1;
8774 if (j + 1 < iverdef->vd_cnt)
8775 iverdaux->vda_nextptr = iverdaux + 1;
8777 if (iverdaux->vda_next
8778 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8779 goto error_return_bad_verdef;
8781 everdaux = ((Elf_External_Verdaux *)
8782 ((bfd_byte *) everdaux + iverdaux->vda_next));
8785 iverdef->vd_nodename = NULL;
8786 if (iverdef->vd_cnt)
8787 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8789 iverdef->vd_nextdef = NULL;
8790 if (iverdef->vd_next == 0)
8792 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8793 iverdef->vd_nextdef = iverdef + 1;
8795 everdef = ((Elf_External_Verdef *)
8796 ((bfd_byte *) everdef + iverdef->vd_next));
8802 else if (default_imported_symver)
8809 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8810 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8811 if (elf_tdata (abfd)->verdef == NULL)
8814 elf_tdata (abfd)->cverdefs = freeidx;
8817 /* Create a default version based on the soname. */
8818 if (default_imported_symver)
8820 Elf_Internal_Verdef *iverdef;
8821 Elf_Internal_Verdaux *iverdaux;
8823 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8825 iverdef->vd_version = VER_DEF_CURRENT;
8826 iverdef->vd_flags = 0;
8827 iverdef->vd_ndx = freeidx;
8828 iverdef->vd_cnt = 1;
8830 iverdef->vd_bfd = abfd;
8832 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8833 if (iverdef->vd_nodename == NULL)
8834 goto error_return_verdef;
8835 iverdef->vd_nextdef = NULL;
8836 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8837 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8838 if (iverdef->vd_auxptr == NULL)
8839 goto error_return_verdef;
8841 iverdaux = iverdef->vd_auxptr;
8842 iverdaux->vda_nodename = iverdef->vd_nodename;
8848 if (contents != NULL)
8854 _bfd_elf_make_empty_symbol (bfd *abfd)
8856 elf_symbol_type *newsym;
8858 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
8861 newsym->symbol.the_bfd = abfd;
8862 return &newsym->symbol;
8866 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8870 bfd_symbol_info (symbol, ret);
8873 /* Return whether a symbol name implies a local symbol. Most targets
8874 use this function for the is_local_label_name entry point, but some
8878 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8881 /* Normal local symbols start with ``.L''. */
8882 if (name[0] == '.' && name[1] == 'L')
8885 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8886 DWARF debugging symbols starting with ``..''. */
8887 if (name[0] == '.' && name[1] == '.')
8890 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8891 emitting DWARF debugging output. I suspect this is actually a
8892 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8893 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8894 underscore to be emitted on some ELF targets). For ease of use,
8895 we treat such symbols as local. */
8896 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8899 /* Treat assembler generated fake symbols, dollar local labels and
8900 forward-backward labels (aka local labels) as locals.
8901 These labels have the form:
8903 L0^A.* (fake symbols)
8905 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8907 Versions which start with .L will have already been matched above,
8908 so we only need to match the rest. */
8909 if (name[0] == 'L' && ISDIGIT (name[1]))
8911 bfd_boolean ret = FALSE;
8915 for (p = name + 2; (c = *p); p++)
8917 if (c == 1 || c == 2)
8919 if (c == 1 && p == name + 2)
8920 /* A fake symbol. */
8923 /* FIXME: We are being paranoid here and treating symbols like
8924 L0^Bfoo as if there were non-local, on the grounds that the
8925 assembler will never generate them. But can any symbol
8926 containing an ASCII value in the range 1-31 ever be anything
8927 other than some kind of local ? */
8944 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8945 asymbol *symbol ATTRIBUTE_UNUSED)
8952 _bfd_elf_set_arch_mach (bfd *abfd,
8953 enum bfd_architecture arch,
8954 unsigned long machine)
8956 /* If this isn't the right architecture for this backend, and this
8957 isn't the generic backend, fail. */
8958 if (arch != get_elf_backend_data (abfd)->arch
8959 && arch != bfd_arch_unknown
8960 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8963 return bfd_default_set_arch_mach (abfd, arch, machine);
8966 /* Find the nearest line to a particular section and offset,
8967 for error reporting. */
8970 _bfd_elf_find_nearest_line (bfd *abfd,
8974 const char **filename_ptr,
8975 const char **functionname_ptr,
8976 unsigned int *line_ptr,
8977 unsigned int *discriminator_ptr)
8981 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8982 filename_ptr, functionname_ptr,
8983 line_ptr, discriminator_ptr,
8984 dwarf_debug_sections,
8985 &elf_tdata (abfd)->dwarf2_find_line_info)
8986 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8987 filename_ptr, functionname_ptr,
8990 if (!*functionname_ptr)
8991 _bfd_elf_find_function (abfd, symbols, section, offset,
8992 *filename_ptr ? NULL : filename_ptr,
8997 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8998 &found, filename_ptr,
8999 functionname_ptr, line_ptr,
9000 &elf_tdata (abfd)->line_info))
9002 if (found && (*functionname_ptr || *line_ptr))
9005 if (symbols == NULL)
9008 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9009 filename_ptr, functionname_ptr))
9016 /* Find the line for a symbol. */
9019 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9020 const char **filename_ptr, unsigned int *line_ptr)
9022 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9023 filename_ptr, NULL, line_ptr, NULL,
9024 dwarf_debug_sections,
9025 &elf_tdata (abfd)->dwarf2_find_line_info);
9028 /* After a call to bfd_find_nearest_line, successive calls to
9029 bfd_find_inliner_info can be used to get source information about
9030 each level of function inlining that terminated at the address
9031 passed to bfd_find_nearest_line. Currently this is only supported
9032 for DWARF2 with appropriate DWARF3 extensions. */
9035 _bfd_elf_find_inliner_info (bfd *abfd,
9036 const char **filename_ptr,
9037 const char **functionname_ptr,
9038 unsigned int *line_ptr)
9041 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9042 functionname_ptr, line_ptr,
9043 & elf_tdata (abfd)->dwarf2_find_line_info);
9048 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9050 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9051 int ret = bed->s->sizeof_ehdr;
9053 if (!bfd_link_relocatable (info))
9055 bfd_size_type phdr_size = elf_program_header_size (abfd);
9057 if (phdr_size == (bfd_size_type) -1)
9059 struct elf_segment_map *m;
9062 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9063 phdr_size += bed->s->sizeof_phdr;
9066 phdr_size = get_program_header_size (abfd, info);
9069 elf_program_header_size (abfd) = phdr_size;
9077 _bfd_elf_set_section_contents (bfd *abfd,
9079 const void *location,
9081 bfd_size_type count)
9083 Elf_Internal_Shdr *hdr;
9086 if (! abfd->output_has_begun
9087 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9093 hdr = &elf_section_data (section)->this_hdr;
9094 if (hdr->sh_offset == (file_ptr) -1)
9096 /* We must compress this section. Write output to the buffer. */
9097 unsigned char *contents = hdr->contents;
9098 if ((offset + count) > hdr->sh_size
9099 || (section->flags & SEC_ELF_COMPRESS) == 0
9100 || contents == NULL)
9102 memcpy (contents + offset, location, count);
9105 pos = hdr->sh_offset + offset;
9106 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9107 || bfd_bwrite (location, count, abfd) != count)
9114 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9115 arelent *cache_ptr ATTRIBUTE_UNUSED,
9116 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9122 /* Try to convert a non-ELF reloc into an ELF one. */
9125 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9127 /* Check whether we really have an ELF howto. */
9129 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9131 bfd_reloc_code_real_type code;
9132 reloc_howto_type *howto;
9134 /* Alien reloc: Try to determine its type to replace it with an
9135 equivalent ELF reloc. */
9137 if (areloc->howto->pc_relative)
9139 switch (areloc->howto->bitsize)
9142 code = BFD_RELOC_8_PCREL;
9145 code = BFD_RELOC_12_PCREL;
9148 code = BFD_RELOC_16_PCREL;
9151 code = BFD_RELOC_24_PCREL;
9154 code = BFD_RELOC_32_PCREL;
9157 code = BFD_RELOC_64_PCREL;
9163 howto = bfd_reloc_type_lookup (abfd, code);
9165 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9167 if (howto->pcrel_offset)
9168 areloc->addend += areloc->address;
9170 areloc->addend -= areloc->address; /* addend is unsigned!! */
9175 switch (areloc->howto->bitsize)
9181 code = BFD_RELOC_14;
9184 code = BFD_RELOC_16;
9187 code = BFD_RELOC_26;
9190 code = BFD_RELOC_32;
9193 code = BFD_RELOC_64;
9199 howto = bfd_reloc_type_lookup (abfd, code);
9203 areloc->howto = howto;
9211 /* xgettext:c-format */
9212 _bfd_error_handler (_("%pB: %s unsupported"),
9213 abfd, areloc->howto->name);
9214 bfd_set_error (bfd_error_bad_value);
9219 _bfd_elf_close_and_cleanup (bfd *abfd)
9221 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9222 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
9224 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9225 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9226 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9229 return _bfd_generic_close_and_cleanup (abfd);
9232 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9233 in the relocation's offset. Thus we cannot allow any sort of sanity
9234 range-checking to interfere. There is nothing else to do in processing
9237 bfd_reloc_status_type
9238 _bfd_elf_rel_vtable_reloc_fn
9239 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9240 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9241 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9242 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9244 return bfd_reloc_ok;
9247 /* Elf core file support. Much of this only works on native
9248 toolchains, since we rely on knowing the
9249 machine-dependent procfs structure in order to pick
9250 out details about the corefile. */
9252 #ifdef HAVE_SYS_PROCFS_H
9253 /* Needed for new procfs interface on sparc-solaris. */
9254 # define _STRUCTURED_PROC 1
9255 # include <sys/procfs.h>
9258 /* Return a PID that identifies a "thread" for threaded cores, or the
9259 PID of the main process for non-threaded cores. */
9262 elfcore_make_pid (bfd *abfd)
9266 pid = elf_tdata (abfd)->core->lwpid;
9268 pid = elf_tdata (abfd)->core->pid;
9273 /* If there isn't a section called NAME, make one, using
9274 data from SECT. Note, this function will generate a
9275 reference to NAME, so you shouldn't deallocate or
9279 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9283 if (bfd_get_section_by_name (abfd, name) != NULL)
9286 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9290 sect2->size = sect->size;
9291 sect2->filepos = sect->filepos;
9292 sect2->alignment_power = sect->alignment_power;
9296 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9297 actually creates up to two pseudosections:
9298 - For the single-threaded case, a section named NAME, unless
9299 such a section already exists.
9300 - For the multi-threaded case, a section named "NAME/PID", where
9301 PID is elfcore_make_pid (abfd).
9302 Both pseudosections have identical contents. */
9304 _bfd_elfcore_make_pseudosection (bfd *abfd,
9310 char *threaded_name;
9314 /* Build the section name. */
9316 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9317 len = strlen (buf) + 1;
9318 threaded_name = (char *) bfd_alloc (abfd, len);
9319 if (threaded_name == NULL)
9321 memcpy (threaded_name, buf, len);
9323 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9328 sect->filepos = filepos;
9329 sect->alignment_power = 2;
9331 return elfcore_maybe_make_sect (abfd, name, sect);
9335 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9338 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9344 sect->size = note->descsz - offs;
9345 sect->filepos = note->descpos + offs;
9346 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9351 /* prstatus_t exists on:
9353 linux 2.[01] + glibc
9357 #if defined (HAVE_PRSTATUS_T)
9360 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9365 if (note->descsz == sizeof (prstatus_t))
9369 size = sizeof (prstat.pr_reg);
9370 offset = offsetof (prstatus_t, pr_reg);
9371 memcpy (&prstat, note->descdata, sizeof (prstat));
9373 /* Do not overwrite the core signal if it
9374 has already been set by another thread. */
9375 if (elf_tdata (abfd)->core->signal == 0)
9376 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9377 if (elf_tdata (abfd)->core->pid == 0)
9378 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9380 /* pr_who exists on:
9383 pr_who doesn't exist on:
9386 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9387 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9389 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9392 #if defined (HAVE_PRSTATUS32_T)
9393 else if (note->descsz == sizeof (prstatus32_t))
9395 /* 64-bit host, 32-bit corefile */
9396 prstatus32_t prstat;
9398 size = sizeof (prstat.pr_reg);
9399 offset = offsetof (prstatus32_t, pr_reg);
9400 memcpy (&prstat, note->descdata, sizeof (prstat));
9402 /* Do not overwrite the core signal if it
9403 has already been set by another thread. */
9404 if (elf_tdata (abfd)->core->signal == 0)
9405 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9406 if (elf_tdata (abfd)->core->pid == 0)
9407 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9409 /* pr_who exists on:
9412 pr_who doesn't exist on:
9415 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9416 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9418 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9421 #endif /* HAVE_PRSTATUS32_T */
9424 /* Fail - we don't know how to handle any other
9425 note size (ie. data object type). */
9429 /* Make a ".reg/999" section and a ".reg" section. */
9430 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9431 size, note->descpos + offset);
9433 #endif /* defined (HAVE_PRSTATUS_T) */
9435 /* Create a pseudosection containing the exact contents of NOTE. */
9437 elfcore_make_note_pseudosection (bfd *abfd,
9439 Elf_Internal_Note *note)
9441 return _bfd_elfcore_make_pseudosection (abfd, name,
9442 note->descsz, note->descpos);
9445 /* There isn't a consistent prfpregset_t across platforms,
9446 but it doesn't matter, because we don't have to pick this
9447 data structure apart. */
9450 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9452 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9455 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9456 type of NT_PRXFPREG. Just include the whole note's contents
9460 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9462 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9465 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9466 with a note type of NT_X86_XSTATE. Just include the whole note's
9467 contents literally. */
9470 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9472 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9476 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9478 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9482 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9484 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9488 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9490 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9494 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9496 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9500 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9502 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9506 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9508 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9512 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9514 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9518 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9520 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9524 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9526 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9530 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9532 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9536 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9538 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9542 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9544 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9548 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9550 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9554 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9556 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9560 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9562 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9566 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9568 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9572 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9574 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9578 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9580 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9584 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9586 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9590 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9592 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9596 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9598 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9602 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9604 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9608 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9610 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9614 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9616 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9620 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9622 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9626 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9628 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9632 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9634 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9638 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9640 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9644 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9646 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9650 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9652 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9656 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9658 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9662 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9664 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9668 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9670 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9674 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9676 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9679 #if defined (HAVE_PRPSINFO_T)
9680 typedef prpsinfo_t elfcore_psinfo_t;
9681 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9682 typedef prpsinfo32_t elfcore_psinfo32_t;
9686 #if defined (HAVE_PSINFO_T)
9687 typedef psinfo_t elfcore_psinfo_t;
9688 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9689 typedef psinfo32_t elfcore_psinfo32_t;
9693 /* return a malloc'ed copy of a string at START which is at
9694 most MAX bytes long, possibly without a terminating '\0'.
9695 the copy will always have a terminating '\0'. */
9698 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9701 char *end = (char *) memchr (start, '\0', max);
9709 dups = (char *) bfd_alloc (abfd, len + 1);
9713 memcpy (dups, start, len);
9719 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9721 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9723 if (note->descsz == sizeof (elfcore_psinfo_t))
9725 elfcore_psinfo_t psinfo;
9727 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9729 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9730 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9732 elf_tdata (abfd)->core->program
9733 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9734 sizeof (psinfo.pr_fname));
9736 elf_tdata (abfd)->core->command
9737 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9738 sizeof (psinfo.pr_psargs));
9740 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9741 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9743 /* 64-bit host, 32-bit corefile */
9744 elfcore_psinfo32_t psinfo;
9746 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9748 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9749 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9751 elf_tdata (abfd)->core->program
9752 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9753 sizeof (psinfo.pr_fname));
9755 elf_tdata (abfd)->core->command
9756 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9757 sizeof (psinfo.pr_psargs));
9763 /* Fail - we don't know how to handle any other
9764 note size (ie. data object type). */
9768 /* Note that for some reason, a spurious space is tacked
9769 onto the end of the args in some (at least one anyway)
9770 implementations, so strip it off if it exists. */
9773 char *command = elf_tdata (abfd)->core->command;
9774 int n = strlen (command);
9776 if (0 < n && command[n - 1] == ' ')
9777 command[n - 1] = '\0';
9782 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9784 #if defined (HAVE_PSTATUS_T)
9786 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9788 if (note->descsz == sizeof (pstatus_t)
9789 #if defined (HAVE_PXSTATUS_T)
9790 || note->descsz == sizeof (pxstatus_t)
9796 memcpy (&pstat, note->descdata, sizeof (pstat));
9798 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9800 #if defined (HAVE_PSTATUS32_T)
9801 else if (note->descsz == sizeof (pstatus32_t))
9803 /* 64-bit host, 32-bit corefile */
9806 memcpy (&pstat, note->descdata, sizeof (pstat));
9808 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9811 /* Could grab some more details from the "representative"
9812 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9813 NT_LWPSTATUS note, presumably. */
9817 #endif /* defined (HAVE_PSTATUS_T) */
9819 #if defined (HAVE_LWPSTATUS_T)
9821 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9823 lwpstatus_t lwpstat;
9829 if (note->descsz != sizeof (lwpstat)
9830 #if defined (HAVE_LWPXSTATUS_T)
9831 && note->descsz != sizeof (lwpxstatus_t)
9836 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9838 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9839 /* Do not overwrite the core signal if it has already been set by
9841 if (elf_tdata (abfd)->core->signal == 0)
9842 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9844 /* Make a ".reg/999" section. */
9846 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
9847 len = strlen (buf) + 1;
9848 name = bfd_alloc (abfd, len);
9851 memcpy (name, buf, len);
9853 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9857 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9858 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
9859 sect->filepos = note->descpos
9860 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9863 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9864 sect->size = sizeof (lwpstat.pr_reg);
9865 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9868 sect->alignment_power = 2;
9870 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9873 /* Make a ".reg2/999" section */
9875 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9876 len = strlen (buf) + 1;
9877 name = bfd_alloc (abfd, len);
9880 memcpy (name, buf, len);
9882 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9886 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9887 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9888 sect->filepos = note->descpos
9889 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9892 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9893 sect->size = sizeof (lwpstat.pr_fpreg);
9894 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9897 sect->alignment_power = 2;
9899 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9901 #endif /* defined (HAVE_LWPSTATUS_T) */
9904 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9911 int is_active_thread;
9914 if (note->descsz < 728)
9917 if (! CONST_STRNEQ (note->namedata, "win32"))
9920 type = bfd_get_32 (abfd, note->descdata);
9924 case 1 /* NOTE_INFO_PROCESS */:
9925 /* FIXME: need to add ->core->command. */
9926 /* process_info.pid */
9927 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9928 /* process_info.signal */
9929 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9932 case 2 /* NOTE_INFO_THREAD */:
9933 /* Make a ".reg/999" section. */
9934 /* thread_info.tid */
9935 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9937 len = strlen (buf) + 1;
9938 name = (char *) bfd_alloc (abfd, len);
9942 memcpy (name, buf, len);
9944 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9948 /* sizeof (thread_info.thread_context) */
9950 /* offsetof (thread_info.thread_context) */
9951 sect->filepos = note->descpos + 12;
9952 sect->alignment_power = 2;
9954 /* thread_info.is_active_thread */
9955 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9957 if (is_active_thread)
9958 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9962 case 3 /* NOTE_INFO_MODULE */:
9963 /* Make a ".module/xxxxxxxx" section. */
9964 /* module_info.base_address */
9965 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9966 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9968 len = strlen (buf) + 1;
9969 name = (char *) bfd_alloc (abfd, len);
9973 memcpy (name, buf, len);
9975 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9980 sect->size = note->descsz;
9981 sect->filepos = note->descpos;
9982 sect->alignment_power = 2;
9993 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10003 if (bed->elf_backend_grok_prstatus)
10004 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10006 #if defined (HAVE_PRSTATUS_T)
10007 return elfcore_grok_prstatus (abfd, note);
10012 #if defined (HAVE_PSTATUS_T)
10014 return elfcore_grok_pstatus (abfd, note);
10017 #if defined (HAVE_LWPSTATUS_T)
10019 return elfcore_grok_lwpstatus (abfd, note);
10022 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10023 return elfcore_grok_prfpreg (abfd, note);
10025 case NT_WIN32PSTATUS:
10026 return elfcore_grok_win32pstatus (abfd, note);
10028 case NT_PRXFPREG: /* Linux SSE extension */
10029 if (note->namesz == 6
10030 && strcmp (note->namedata, "LINUX") == 0)
10031 return elfcore_grok_prxfpreg (abfd, note);
10035 case NT_X86_XSTATE: /* Linux XSAVE extension */
10036 if (note->namesz == 6
10037 && strcmp (note->namedata, "LINUX") == 0)
10038 return elfcore_grok_xstatereg (abfd, note);
10043 if (note->namesz == 6
10044 && strcmp (note->namedata, "LINUX") == 0)
10045 return elfcore_grok_ppc_vmx (abfd, note);
10050 if (note->namesz == 6
10051 && strcmp (note->namedata, "LINUX") == 0)
10052 return elfcore_grok_ppc_vsx (abfd, note);
10057 if (note->namesz == 6
10058 && strcmp (note->namedata, "LINUX") == 0)
10059 return elfcore_grok_ppc_tar (abfd, note);
10064 if (note->namesz == 6
10065 && strcmp (note->namedata, "LINUX") == 0)
10066 return elfcore_grok_ppc_ppr (abfd, note);
10071 if (note->namesz == 6
10072 && strcmp (note->namedata, "LINUX") == 0)
10073 return elfcore_grok_ppc_dscr (abfd, note);
10078 if (note->namesz == 6
10079 && strcmp (note->namedata, "LINUX") == 0)
10080 return elfcore_grok_ppc_ebb (abfd, note);
10085 if (note->namesz == 6
10086 && strcmp (note->namedata, "LINUX") == 0)
10087 return elfcore_grok_ppc_pmu (abfd, note);
10091 case NT_PPC_TM_CGPR:
10092 if (note->namesz == 6
10093 && strcmp (note->namedata, "LINUX") == 0)
10094 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10098 case NT_PPC_TM_CFPR:
10099 if (note->namesz == 6
10100 && strcmp (note->namedata, "LINUX") == 0)
10101 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10105 case NT_PPC_TM_CVMX:
10106 if (note->namesz == 6
10107 && strcmp (note->namedata, "LINUX") == 0)
10108 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10112 case NT_PPC_TM_CVSX:
10113 if (note->namesz == 6
10114 && strcmp (note->namedata, "LINUX") == 0)
10115 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10119 case NT_PPC_TM_SPR:
10120 if (note->namesz == 6
10121 && strcmp (note->namedata, "LINUX") == 0)
10122 return elfcore_grok_ppc_tm_spr (abfd, note);
10126 case NT_PPC_TM_CTAR:
10127 if (note->namesz == 6
10128 && strcmp (note->namedata, "LINUX") == 0)
10129 return elfcore_grok_ppc_tm_ctar (abfd, note);
10133 case NT_PPC_TM_CPPR:
10134 if (note->namesz == 6
10135 && strcmp (note->namedata, "LINUX") == 0)
10136 return elfcore_grok_ppc_tm_cppr (abfd, note);
10140 case NT_PPC_TM_CDSCR:
10141 if (note->namesz == 6
10142 && strcmp (note->namedata, "LINUX") == 0)
10143 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10147 case NT_S390_HIGH_GPRS:
10148 if (note->namesz == 6
10149 && strcmp (note->namedata, "LINUX") == 0)
10150 return elfcore_grok_s390_high_gprs (abfd, note);
10154 case NT_S390_TIMER:
10155 if (note->namesz == 6
10156 && strcmp (note->namedata, "LINUX") == 0)
10157 return elfcore_grok_s390_timer (abfd, note);
10161 case NT_S390_TODCMP:
10162 if (note->namesz == 6
10163 && strcmp (note->namedata, "LINUX") == 0)
10164 return elfcore_grok_s390_todcmp (abfd, note);
10168 case NT_S390_TODPREG:
10169 if (note->namesz == 6
10170 && strcmp (note->namedata, "LINUX") == 0)
10171 return elfcore_grok_s390_todpreg (abfd, note);
10176 if (note->namesz == 6
10177 && strcmp (note->namedata, "LINUX") == 0)
10178 return elfcore_grok_s390_ctrs (abfd, note);
10182 case NT_S390_PREFIX:
10183 if (note->namesz == 6
10184 && strcmp (note->namedata, "LINUX") == 0)
10185 return elfcore_grok_s390_prefix (abfd, note);
10189 case NT_S390_LAST_BREAK:
10190 if (note->namesz == 6
10191 && strcmp (note->namedata, "LINUX") == 0)
10192 return elfcore_grok_s390_last_break (abfd, note);
10196 case NT_S390_SYSTEM_CALL:
10197 if (note->namesz == 6
10198 && strcmp (note->namedata, "LINUX") == 0)
10199 return elfcore_grok_s390_system_call (abfd, note);
10204 if (note->namesz == 6
10205 && strcmp (note->namedata, "LINUX") == 0)
10206 return elfcore_grok_s390_tdb (abfd, note);
10210 case NT_S390_VXRS_LOW:
10211 if (note->namesz == 6
10212 && strcmp (note->namedata, "LINUX") == 0)
10213 return elfcore_grok_s390_vxrs_low (abfd, note);
10217 case NT_S390_VXRS_HIGH:
10218 if (note->namesz == 6
10219 && strcmp (note->namedata, "LINUX") == 0)
10220 return elfcore_grok_s390_vxrs_high (abfd, note);
10224 case NT_S390_GS_CB:
10225 if (note->namesz == 6
10226 && strcmp (note->namedata, "LINUX") == 0)
10227 return elfcore_grok_s390_gs_cb (abfd, note);
10231 case NT_S390_GS_BC:
10232 if (note->namesz == 6
10233 && strcmp (note->namedata, "LINUX") == 0)
10234 return elfcore_grok_s390_gs_bc (abfd, note);
10239 if (note->namesz == 6
10240 && strcmp (note->namedata, "LINUX") == 0)
10241 return elfcore_grok_arm_vfp (abfd, note);
10246 if (note->namesz == 6
10247 && strcmp (note->namedata, "LINUX") == 0)
10248 return elfcore_grok_aarch_tls (abfd, note);
10252 case NT_ARM_HW_BREAK:
10253 if (note->namesz == 6
10254 && strcmp (note->namedata, "LINUX") == 0)
10255 return elfcore_grok_aarch_hw_break (abfd, note);
10259 case NT_ARM_HW_WATCH:
10260 if (note->namesz == 6
10261 && strcmp (note->namedata, "LINUX") == 0)
10262 return elfcore_grok_aarch_hw_watch (abfd, note);
10267 if (note->namesz == 6
10268 && strcmp (note->namedata, "LINUX") == 0)
10269 return elfcore_grok_aarch_sve (abfd, note);
10273 case NT_ARM_PAC_MASK:
10274 if (note->namesz == 6
10275 && strcmp (note->namedata, "LINUX") == 0)
10276 return elfcore_grok_aarch_pauth (abfd, note);
10282 if (bed->elf_backend_grok_psinfo)
10283 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10285 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10286 return elfcore_grok_psinfo (abfd, note);
10292 return elfcore_make_auxv_note_section (abfd, note, 0);
10295 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10299 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10306 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10308 struct bfd_build_id* build_id;
10310 if (note->descsz == 0)
10313 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10314 if (build_id == NULL)
10317 build_id->size = note->descsz;
10318 memcpy (build_id->data, note->descdata, note->descsz);
10319 abfd->build_id = build_id;
10325 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10327 switch (note->type)
10332 case NT_GNU_PROPERTY_TYPE_0:
10333 return _bfd_elf_parse_gnu_properties (abfd, note);
10335 case NT_GNU_BUILD_ID:
10336 return elfobj_grok_gnu_build_id (abfd, note);
10341 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10343 struct sdt_note *cur =
10344 (struct sdt_note *) bfd_alloc (abfd,
10345 sizeof (struct sdt_note) + note->descsz);
10347 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10348 cur->size = (bfd_size_type) note->descsz;
10349 memcpy (cur->data, note->descdata, note->descsz);
10351 elf_tdata (abfd)->sdt_note_head = cur;
10357 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10359 switch (note->type)
10362 return elfobj_grok_stapsdt_note_1 (abfd, note);
10370 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10374 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10377 if (note->descsz < 108)
10382 if (note->descsz < 120)
10390 /* Check for version 1 in pr_version. */
10391 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10396 /* Skip over pr_psinfosz. */
10397 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10401 offset += 4; /* Padding before pr_psinfosz. */
10405 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10406 elf_tdata (abfd)->core->program
10407 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10410 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10411 elf_tdata (abfd)->core->command
10412 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10415 /* Padding before pr_pid. */
10418 /* The pr_pid field was added in version "1a". */
10419 if (note->descsz < offset + 4)
10422 elf_tdata (abfd)->core->pid
10423 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10429 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10435 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10436 Also compute minimum size of this note. */
10437 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10441 min_size = offset + (4 * 2) + 4 + 4 + 4;
10445 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10446 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10453 if (note->descsz < min_size)
10456 /* Check for version 1 in pr_version. */
10457 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10460 /* Extract size of pr_reg from pr_gregsetsz. */
10461 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10462 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10464 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10469 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10473 /* Skip over pr_osreldate. */
10476 /* Read signal from pr_cursig. */
10477 if (elf_tdata (abfd)->core->signal == 0)
10478 elf_tdata (abfd)->core->signal
10479 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10482 /* Read TID from pr_pid. */
10483 elf_tdata (abfd)->core->lwpid
10484 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10487 /* Padding before pr_reg. */
10488 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10491 /* Make sure that there is enough data remaining in the note. */
10492 if ((note->descsz - offset) < size)
10495 /* Make a ".reg/999" section and a ".reg" section. */
10496 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10497 size, note->descpos + offset);
10501 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10503 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10505 switch (note->type)
10508 if (bed->elf_backend_grok_freebsd_prstatus)
10509 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10511 return elfcore_grok_freebsd_prstatus (abfd, note);
10514 return elfcore_grok_prfpreg (abfd, note);
10517 return elfcore_grok_freebsd_psinfo (abfd, note);
10519 case NT_FREEBSD_THRMISC:
10520 if (note->namesz == 8)
10521 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10525 case NT_FREEBSD_PROCSTAT_PROC:
10526 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10529 case NT_FREEBSD_PROCSTAT_FILES:
10530 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10533 case NT_FREEBSD_PROCSTAT_VMMAP:
10534 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10537 case NT_FREEBSD_PROCSTAT_AUXV:
10538 return elfcore_make_auxv_note_section (abfd, note, 4);
10540 case NT_X86_XSTATE:
10541 if (note->namesz == 8)
10542 return elfcore_grok_xstatereg (abfd, note);
10546 case NT_FREEBSD_PTLWPINFO:
10547 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10551 return elfcore_grok_arm_vfp (abfd, note);
10559 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10563 cp = strchr (note->namedata, '@');
10566 *lwpidp = atoi(cp + 1);
10573 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10575 if (note->descsz <= 0x7c + 31)
10578 /* Signal number at offset 0x08. */
10579 elf_tdata (abfd)->core->signal
10580 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10582 /* Process ID at offset 0x50. */
10583 elf_tdata (abfd)->core->pid
10584 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10586 /* Command name at 0x7c (max 32 bytes, including nul). */
10587 elf_tdata (abfd)->core->command
10588 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10590 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10595 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10599 if (elfcore_netbsd_get_lwpid (note, &lwp))
10600 elf_tdata (abfd)->core->lwpid = lwp;
10602 switch (note->type)
10604 case NT_NETBSDCORE_PROCINFO:
10605 /* NetBSD-specific core "procinfo". Note that we expect to
10606 find this note before any of the others, which is fine,
10607 since the kernel writes this note out first when it
10608 creates a core file. */
10609 return elfcore_grok_netbsd_procinfo (abfd, note);
10610 #ifdef NT_NETBSDCORE_AUXV
10611 case NT_NETBSDCORE_AUXV:
10612 /* NetBSD-specific Elf Auxiliary Vector data. */
10613 return elfcore_make_auxv_note_section (abfd, note, 4);
10619 /* As of March 2017 there are no other machine-independent notes
10620 defined for NetBSD core files. If the note type is less
10621 than the start of the machine-dependent note types, we don't
10624 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10628 switch (bfd_get_arch (abfd))
10630 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10631 PT_GETFPREGS == mach+2. */
10633 case bfd_arch_alpha:
10634 case bfd_arch_sparc:
10635 switch (note->type)
10637 case NT_NETBSDCORE_FIRSTMACH+0:
10638 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10640 case NT_NETBSDCORE_FIRSTMACH+2:
10641 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10647 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10648 There's also old PT___GETREGS40 == mach + 1 for old reg
10649 structure which lacks GBR. */
10652 switch (note->type)
10654 case NT_NETBSDCORE_FIRSTMACH+3:
10655 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10657 case NT_NETBSDCORE_FIRSTMACH+5:
10658 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10664 /* On all other arch's, PT_GETREGS == mach+1 and
10665 PT_GETFPREGS == mach+3. */
10668 switch (note->type)
10670 case NT_NETBSDCORE_FIRSTMACH+1:
10671 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10673 case NT_NETBSDCORE_FIRSTMACH+3:
10674 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10684 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10686 if (note->descsz <= 0x48 + 31)
10689 /* Signal number at offset 0x08. */
10690 elf_tdata (abfd)->core->signal
10691 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10693 /* Process ID at offset 0x20. */
10694 elf_tdata (abfd)->core->pid
10695 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10697 /* Command name at 0x48 (max 32 bytes, including nul). */
10698 elf_tdata (abfd)->core->command
10699 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10705 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10707 if (note->type == NT_OPENBSD_PROCINFO)
10708 return elfcore_grok_openbsd_procinfo (abfd, note);
10710 if (note->type == NT_OPENBSD_REGS)
10711 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10713 if (note->type == NT_OPENBSD_FPREGS)
10714 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10716 if (note->type == NT_OPENBSD_XFPREGS)
10717 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10719 if (note->type == NT_OPENBSD_AUXV)
10720 return elfcore_make_auxv_note_section (abfd, note, 0);
10722 if (note->type == NT_OPENBSD_WCOOKIE)
10724 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10729 sect->size = note->descsz;
10730 sect->filepos = note->descpos;
10731 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10740 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10742 void *ddata = note->descdata;
10749 if (note->descsz < 16)
10752 /* nto_procfs_status 'pid' field is at offset 0. */
10753 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10755 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10756 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10758 /* nto_procfs_status 'flags' field is at offset 8. */
10759 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10761 /* nto_procfs_status 'what' field is at offset 14. */
10762 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10764 elf_tdata (abfd)->core->signal = sig;
10765 elf_tdata (abfd)->core->lwpid = *tid;
10768 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10769 do not come from signals so we make sure we set the current
10770 thread just in case. */
10771 if (flags & 0x00000080)
10772 elf_tdata (abfd)->core->lwpid = *tid;
10774 /* Make a ".qnx_core_status/%d" section. */
10775 sprintf (buf, ".qnx_core_status/%ld", *tid);
10777 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10780 strcpy (name, buf);
10782 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10786 sect->size = note->descsz;
10787 sect->filepos = note->descpos;
10788 sect->alignment_power = 2;
10790 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10794 elfcore_grok_nto_regs (bfd *abfd,
10795 Elf_Internal_Note *note,
10803 /* Make a "(base)/%d" section. */
10804 sprintf (buf, "%s/%ld", base, tid);
10806 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10809 strcpy (name, buf);
10811 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10815 sect->size = note->descsz;
10816 sect->filepos = note->descpos;
10817 sect->alignment_power = 2;
10819 /* This is the current thread. */
10820 if (elf_tdata (abfd)->core->lwpid == tid)
10821 return elfcore_maybe_make_sect (abfd, base, sect);
10826 #define BFD_QNT_CORE_INFO 7
10827 #define BFD_QNT_CORE_STATUS 8
10828 #define BFD_QNT_CORE_GREG 9
10829 #define BFD_QNT_CORE_FPREG 10
10832 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10834 /* Every GREG section has a STATUS section before it. Store the
10835 tid from the previous call to pass down to the next gregs
10837 static long tid = 1;
10839 switch (note->type)
10841 case BFD_QNT_CORE_INFO:
10842 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10843 case BFD_QNT_CORE_STATUS:
10844 return elfcore_grok_nto_status (abfd, note, &tid);
10845 case BFD_QNT_CORE_GREG:
10846 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10847 case BFD_QNT_CORE_FPREG:
10848 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10855 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10861 /* Use note name as section name. */
10862 len = note->namesz;
10863 name = (char *) bfd_alloc (abfd, len);
10866 memcpy (name, note->namedata, len);
10867 name[len - 1] = '\0';
10869 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10873 sect->size = note->descsz;
10874 sect->filepos = note->descpos;
10875 sect->alignment_power = 1;
10880 /* Function: elfcore_write_note
10883 buffer to hold note, and current size of buffer
10887 size of data for note
10889 Writes note to end of buffer. ELF64 notes are written exactly as
10890 for ELF32, despite the current (as of 2006) ELF gabi specifying
10891 that they ought to have 8-byte namesz and descsz field, and have
10892 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10895 Pointer to realloc'd buffer, *BUFSIZ updated. */
10898 elfcore_write_note (bfd *abfd,
10906 Elf_External_Note *xnp;
10913 namesz = strlen (name) + 1;
10915 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
10917 buf = (char *) realloc (buf, *bufsiz + newspace);
10920 dest = buf + *bufsiz;
10921 *bufsiz += newspace;
10922 xnp = (Elf_External_Note *) dest;
10923 H_PUT_32 (abfd, namesz, xnp->namesz);
10924 H_PUT_32 (abfd, size, xnp->descsz);
10925 H_PUT_32 (abfd, type, xnp->type);
10929 memcpy (dest, name, namesz);
10937 memcpy (dest, input, size);
10947 /* gcc-8 warns (*) on all the strncpy calls in this function about
10948 possible string truncation. The "truncation" is not a bug. We
10949 have an external representation of structs with fields that are not
10950 necessarily NULL terminated and corresponding internal
10951 representation fields that are one larger so that they can always
10952 be NULL terminated.
10953 gcc versions between 4.2 and 4.6 do not allow pragma control of
10954 diagnostics inside functions, giving a hard error if you try to use
10955 the finer control available with later versions.
10956 gcc prior to 4.2 warns about diagnostic push and pop.
10957 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10958 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10959 (*) Depending on your system header files! */
10960 #if GCC_VERSION >= 8000
10961 # pragma GCC diagnostic push
10962 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10965 elfcore_write_prpsinfo (bfd *abfd,
10969 const char *psargs)
10971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10973 if (bed->elf_backend_write_core_note != NULL)
10976 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10977 NT_PRPSINFO, fname, psargs);
10982 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10983 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10984 if (bed->s->elfclass == ELFCLASS32)
10986 # if defined (HAVE_PSINFO32_T)
10988 int note_type = NT_PSINFO;
10991 int note_type = NT_PRPSINFO;
10994 memset (&data, 0, sizeof (data));
10995 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10996 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10997 return elfcore_write_note (abfd, buf, bufsiz,
10998 "CORE", note_type, &data, sizeof (data));
11003 # if defined (HAVE_PSINFO_T)
11005 int note_type = NT_PSINFO;
11008 int note_type = NT_PRPSINFO;
11011 memset (&data, 0, sizeof (data));
11012 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11013 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11014 return elfcore_write_note (abfd, buf, bufsiz,
11015 "CORE", note_type, &data, sizeof (data));
11017 #endif /* PSINFO_T or PRPSINFO_T */
11022 #if GCC_VERSION >= 8000
11023 # pragma GCC diagnostic pop
11027 elfcore_write_linux_prpsinfo32
11028 (bfd *abfd, char *buf, int *bufsiz,
11029 const struct elf_internal_linux_prpsinfo *prpsinfo)
11031 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11033 struct elf_external_linux_prpsinfo32_ugid16 data;
11035 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11036 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11037 &data, sizeof (data));
11041 struct elf_external_linux_prpsinfo32_ugid32 data;
11043 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11044 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11045 &data, sizeof (data));
11050 elfcore_write_linux_prpsinfo64
11051 (bfd *abfd, char *buf, int *bufsiz,
11052 const struct elf_internal_linux_prpsinfo *prpsinfo)
11054 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11056 struct elf_external_linux_prpsinfo64_ugid16 data;
11058 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11059 return elfcore_write_note (abfd, buf, bufsiz,
11060 "CORE", NT_PRPSINFO, &data, sizeof (data));
11064 struct elf_external_linux_prpsinfo64_ugid32 data;
11066 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11067 return elfcore_write_note (abfd, buf, bufsiz,
11068 "CORE", NT_PRPSINFO, &data, sizeof (data));
11073 elfcore_write_prstatus (bfd *abfd,
11080 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11082 if (bed->elf_backend_write_core_note != NULL)
11085 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11087 pid, cursig, gregs);
11092 #if defined (HAVE_PRSTATUS_T)
11093 #if defined (HAVE_PRSTATUS32_T)
11094 if (bed->s->elfclass == ELFCLASS32)
11096 prstatus32_t prstat;
11098 memset (&prstat, 0, sizeof (prstat));
11099 prstat.pr_pid = pid;
11100 prstat.pr_cursig = cursig;
11101 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11102 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11103 NT_PRSTATUS, &prstat, sizeof (prstat));
11110 memset (&prstat, 0, sizeof (prstat));
11111 prstat.pr_pid = pid;
11112 prstat.pr_cursig = cursig;
11113 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11114 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11115 NT_PRSTATUS, &prstat, sizeof (prstat));
11117 #endif /* HAVE_PRSTATUS_T */
11123 #if defined (HAVE_LWPSTATUS_T)
11125 elfcore_write_lwpstatus (bfd *abfd,
11132 lwpstatus_t lwpstat;
11133 const char *note_name = "CORE";
11135 memset (&lwpstat, 0, sizeof (lwpstat));
11136 lwpstat.pr_lwpid = pid >> 16;
11137 lwpstat.pr_cursig = cursig;
11138 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11139 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11140 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11141 #if !defined(gregs)
11142 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11143 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11145 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11146 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11149 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11150 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11152 #endif /* HAVE_LWPSTATUS_T */
11154 #if defined (HAVE_PSTATUS_T)
11156 elfcore_write_pstatus (bfd *abfd,
11160 int cursig ATTRIBUTE_UNUSED,
11161 const void *gregs ATTRIBUTE_UNUSED)
11163 const char *note_name = "CORE";
11164 #if defined (HAVE_PSTATUS32_T)
11165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11167 if (bed->s->elfclass == ELFCLASS32)
11171 memset (&pstat, 0, sizeof (pstat));
11172 pstat.pr_pid = pid & 0xffff;
11173 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11174 NT_PSTATUS, &pstat, sizeof (pstat));
11182 memset (&pstat, 0, sizeof (pstat));
11183 pstat.pr_pid = pid & 0xffff;
11184 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11185 NT_PSTATUS, &pstat, sizeof (pstat));
11189 #endif /* HAVE_PSTATUS_T */
11192 elfcore_write_prfpreg (bfd *abfd,
11195 const void *fpregs,
11198 const char *note_name = "CORE";
11199 return elfcore_write_note (abfd, buf, bufsiz,
11200 note_name, NT_FPREGSET, fpregs, size);
11204 elfcore_write_prxfpreg (bfd *abfd,
11207 const void *xfpregs,
11210 char *note_name = "LINUX";
11211 return elfcore_write_note (abfd, buf, bufsiz,
11212 note_name, NT_PRXFPREG, xfpregs, size);
11216 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11217 const void *xfpregs, int size)
11220 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11221 note_name = "FreeBSD";
11223 note_name = "LINUX";
11224 return elfcore_write_note (abfd, buf, bufsiz,
11225 note_name, NT_X86_XSTATE, xfpregs, size);
11229 elfcore_write_ppc_vmx (bfd *abfd,
11232 const void *ppc_vmx,
11235 char *note_name = "LINUX";
11236 return elfcore_write_note (abfd, buf, bufsiz,
11237 note_name, NT_PPC_VMX, ppc_vmx, size);
11241 elfcore_write_ppc_vsx (bfd *abfd,
11244 const void *ppc_vsx,
11247 char *note_name = "LINUX";
11248 return elfcore_write_note (abfd, buf, bufsiz,
11249 note_name, NT_PPC_VSX, ppc_vsx, size);
11253 elfcore_write_ppc_tar (bfd *abfd,
11256 const void *ppc_tar,
11259 char *note_name = "LINUX";
11260 return elfcore_write_note (abfd, buf, bufsiz,
11261 note_name, NT_PPC_TAR, ppc_tar, size);
11265 elfcore_write_ppc_ppr (bfd *abfd,
11268 const void *ppc_ppr,
11271 char *note_name = "LINUX";
11272 return elfcore_write_note (abfd, buf, bufsiz,
11273 note_name, NT_PPC_PPR, ppc_ppr, size);
11277 elfcore_write_ppc_dscr (bfd *abfd,
11280 const void *ppc_dscr,
11283 char *note_name = "LINUX";
11284 return elfcore_write_note (abfd, buf, bufsiz,
11285 note_name, NT_PPC_DSCR, ppc_dscr, size);
11289 elfcore_write_ppc_ebb (bfd *abfd,
11292 const void *ppc_ebb,
11295 char *note_name = "LINUX";
11296 return elfcore_write_note (abfd, buf, bufsiz,
11297 note_name, NT_PPC_EBB, ppc_ebb, size);
11301 elfcore_write_ppc_pmu (bfd *abfd,
11304 const void *ppc_pmu,
11307 char *note_name = "LINUX";
11308 return elfcore_write_note (abfd, buf, bufsiz,
11309 note_name, NT_PPC_PMU, ppc_pmu, size);
11313 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11316 const void *ppc_tm_cgpr,
11319 char *note_name = "LINUX";
11320 return elfcore_write_note (abfd, buf, bufsiz,
11321 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11325 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11328 const void *ppc_tm_cfpr,
11331 char *note_name = "LINUX";
11332 return elfcore_write_note (abfd, buf, bufsiz,
11333 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11337 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11340 const void *ppc_tm_cvmx,
11343 char *note_name = "LINUX";
11344 return elfcore_write_note (abfd, buf, bufsiz,
11345 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11349 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11352 const void *ppc_tm_cvsx,
11355 char *note_name = "LINUX";
11356 return elfcore_write_note (abfd, buf, bufsiz,
11357 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11361 elfcore_write_ppc_tm_spr (bfd *abfd,
11364 const void *ppc_tm_spr,
11367 char *note_name = "LINUX";
11368 return elfcore_write_note (abfd, buf, bufsiz,
11369 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11373 elfcore_write_ppc_tm_ctar (bfd *abfd,
11376 const void *ppc_tm_ctar,
11379 char *note_name = "LINUX";
11380 return elfcore_write_note (abfd, buf, bufsiz,
11381 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11385 elfcore_write_ppc_tm_cppr (bfd *abfd,
11388 const void *ppc_tm_cppr,
11391 char *note_name = "LINUX";
11392 return elfcore_write_note (abfd, buf, bufsiz,
11393 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11397 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11400 const void *ppc_tm_cdscr,
11403 char *note_name = "LINUX";
11404 return elfcore_write_note (abfd, buf, bufsiz,
11405 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11409 elfcore_write_s390_high_gprs (bfd *abfd,
11412 const void *s390_high_gprs,
11415 char *note_name = "LINUX";
11416 return elfcore_write_note (abfd, buf, bufsiz,
11417 note_name, NT_S390_HIGH_GPRS,
11418 s390_high_gprs, size);
11422 elfcore_write_s390_timer (bfd *abfd,
11425 const void *s390_timer,
11428 char *note_name = "LINUX";
11429 return elfcore_write_note (abfd, buf, bufsiz,
11430 note_name, NT_S390_TIMER, s390_timer, size);
11434 elfcore_write_s390_todcmp (bfd *abfd,
11437 const void *s390_todcmp,
11440 char *note_name = "LINUX";
11441 return elfcore_write_note (abfd, buf, bufsiz,
11442 note_name, NT_S390_TODCMP, s390_todcmp, size);
11446 elfcore_write_s390_todpreg (bfd *abfd,
11449 const void *s390_todpreg,
11452 char *note_name = "LINUX";
11453 return elfcore_write_note (abfd, buf, bufsiz,
11454 note_name, NT_S390_TODPREG, s390_todpreg, size);
11458 elfcore_write_s390_ctrs (bfd *abfd,
11461 const void *s390_ctrs,
11464 char *note_name = "LINUX";
11465 return elfcore_write_note (abfd, buf, bufsiz,
11466 note_name, NT_S390_CTRS, s390_ctrs, size);
11470 elfcore_write_s390_prefix (bfd *abfd,
11473 const void *s390_prefix,
11476 char *note_name = "LINUX";
11477 return elfcore_write_note (abfd, buf, bufsiz,
11478 note_name, NT_S390_PREFIX, s390_prefix, size);
11482 elfcore_write_s390_last_break (bfd *abfd,
11485 const void *s390_last_break,
11488 char *note_name = "LINUX";
11489 return elfcore_write_note (abfd, buf, bufsiz,
11490 note_name, NT_S390_LAST_BREAK,
11491 s390_last_break, size);
11495 elfcore_write_s390_system_call (bfd *abfd,
11498 const void *s390_system_call,
11501 char *note_name = "LINUX";
11502 return elfcore_write_note (abfd, buf, bufsiz,
11503 note_name, NT_S390_SYSTEM_CALL,
11504 s390_system_call, size);
11508 elfcore_write_s390_tdb (bfd *abfd,
11511 const void *s390_tdb,
11514 char *note_name = "LINUX";
11515 return elfcore_write_note (abfd, buf, bufsiz,
11516 note_name, NT_S390_TDB, s390_tdb, size);
11520 elfcore_write_s390_vxrs_low (bfd *abfd,
11523 const void *s390_vxrs_low,
11526 char *note_name = "LINUX";
11527 return elfcore_write_note (abfd, buf, bufsiz,
11528 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11532 elfcore_write_s390_vxrs_high (bfd *abfd,
11535 const void *s390_vxrs_high,
11538 char *note_name = "LINUX";
11539 return elfcore_write_note (abfd, buf, bufsiz,
11540 note_name, NT_S390_VXRS_HIGH,
11541 s390_vxrs_high, size);
11545 elfcore_write_s390_gs_cb (bfd *abfd,
11548 const void *s390_gs_cb,
11551 char *note_name = "LINUX";
11552 return elfcore_write_note (abfd, buf, bufsiz,
11553 note_name, NT_S390_GS_CB,
11558 elfcore_write_s390_gs_bc (bfd *abfd,
11561 const void *s390_gs_bc,
11564 char *note_name = "LINUX";
11565 return elfcore_write_note (abfd, buf, bufsiz,
11566 note_name, NT_S390_GS_BC,
11571 elfcore_write_arm_vfp (bfd *abfd,
11574 const void *arm_vfp,
11577 char *note_name = "LINUX";
11578 return elfcore_write_note (abfd, buf, bufsiz,
11579 note_name, NT_ARM_VFP, arm_vfp, size);
11583 elfcore_write_aarch_tls (bfd *abfd,
11586 const void *aarch_tls,
11589 char *note_name = "LINUX";
11590 return elfcore_write_note (abfd, buf, bufsiz,
11591 note_name, NT_ARM_TLS, aarch_tls, size);
11595 elfcore_write_aarch_hw_break (bfd *abfd,
11598 const void *aarch_hw_break,
11601 char *note_name = "LINUX";
11602 return elfcore_write_note (abfd, buf, bufsiz,
11603 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11607 elfcore_write_aarch_hw_watch (bfd *abfd,
11610 const void *aarch_hw_watch,
11613 char *note_name = "LINUX";
11614 return elfcore_write_note (abfd, buf, bufsiz,
11615 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11619 elfcore_write_aarch_sve (bfd *abfd,
11622 const void *aarch_sve,
11625 char *note_name = "LINUX";
11626 return elfcore_write_note (abfd, buf, bufsiz,
11627 note_name, NT_ARM_SVE, aarch_sve, size);
11631 elfcore_write_aarch_pauth (bfd *abfd,
11634 const void *aarch_pauth,
11637 char *note_name = "LINUX";
11638 return elfcore_write_note (abfd, buf, bufsiz,
11639 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11643 elfcore_write_register_note (bfd *abfd,
11646 const char *section,
11650 if (strcmp (section, ".reg2") == 0)
11651 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11652 if (strcmp (section, ".reg-xfp") == 0)
11653 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
11654 if (strcmp (section, ".reg-xstate") == 0)
11655 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
11656 if (strcmp (section, ".reg-ppc-vmx") == 0)
11657 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
11658 if (strcmp (section, ".reg-ppc-vsx") == 0)
11659 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
11660 if (strcmp (section, ".reg-ppc-tar") == 0)
11661 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11662 if (strcmp (section, ".reg-ppc-ppr") == 0)
11663 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11664 if (strcmp (section, ".reg-ppc-dscr") == 0)
11665 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11666 if (strcmp (section, ".reg-ppc-ebb") == 0)
11667 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11668 if (strcmp (section, ".reg-ppc-pmu") == 0)
11669 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11670 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11671 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11672 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11673 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11674 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11675 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11676 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11677 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11678 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11679 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11680 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11681 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11682 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11683 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11684 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11685 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
11686 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11687 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
11688 if (strcmp (section, ".reg-s390-timer") == 0)
11689 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11690 if (strcmp (section, ".reg-s390-todcmp") == 0)
11691 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11692 if (strcmp (section, ".reg-s390-todpreg") == 0)
11693 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11694 if (strcmp (section, ".reg-s390-ctrs") == 0)
11695 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11696 if (strcmp (section, ".reg-s390-prefix") == 0)
11697 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
11698 if (strcmp (section, ".reg-s390-last-break") == 0)
11699 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11700 if (strcmp (section, ".reg-s390-system-call") == 0)
11701 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
11702 if (strcmp (section, ".reg-s390-tdb") == 0)
11703 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
11704 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11705 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11706 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11707 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
11708 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11709 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11710 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11711 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
11712 if (strcmp (section, ".reg-arm-vfp") == 0)
11713 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
11714 if (strcmp (section, ".reg-aarch-tls") == 0)
11715 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11716 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11717 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11718 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11719 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
11720 if (strcmp (section, ".reg-aarch-sve") == 0)
11721 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
11722 if (strcmp (section, ".reg-aarch-pauth") == 0)
11723 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
11728 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11733 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11734 gABI specifies that PT_NOTE alignment should be aligned to 4
11735 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11736 align is less than 4, we use 4 byte alignment. */
11739 if (align != 4 && align != 8)
11743 while (p < buf + size)
11745 Elf_External_Note *xnp = (Elf_External_Note *) p;
11746 Elf_Internal_Note in;
11748 if (offsetof (Elf_External_Note, name) > buf - p + size)
11751 in.type = H_GET_32 (abfd, xnp->type);
11753 in.namesz = H_GET_32 (abfd, xnp->namesz);
11754 in.namedata = xnp->name;
11755 if (in.namesz > buf - in.namedata + size)
11758 in.descsz = H_GET_32 (abfd, xnp->descsz);
11759 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
11760 in.descpos = offset + (in.descdata - buf);
11762 && (in.descdata >= buf + size
11763 || in.descsz > buf - in.descdata + size))
11766 switch (bfd_get_format (abfd))
11773 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11776 const char * string;
11778 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
11782 GROKER_ELEMENT ("", elfcore_grok_note),
11783 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11784 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11785 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11786 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11787 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
11789 #undef GROKER_ELEMENT
11792 for (i = ARRAY_SIZE (grokers); i--;)
11794 if (in.namesz >= grokers[i].len
11795 && strncmp (in.namedata, grokers[i].string,
11796 grokers[i].len) == 0)
11798 if (! grokers[i].func (abfd, & in))
11807 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11809 if (! elfobj_grok_gnu_note (abfd, &in))
11812 else if (in.namesz == sizeof "stapsdt"
11813 && strcmp (in.namedata, "stapsdt") == 0)
11815 if (! elfobj_grok_stapsdt_note (abfd, &in))
11821 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
11828 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11833 if (size == 0 || (size + 1) == 0)
11836 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11839 buf = (char *) bfd_malloc (size + 1);
11843 /* PR 17512: file: ec08f814
11844 0-termintate the buffer so that string searches will not overflow. */
11847 if (bfd_bread (buf, size, abfd) != size
11848 || !elf_parse_notes (abfd, buf, size, offset, align))
11858 /* Providing external access to the ELF program header table. */
11860 /* Return an upper bound on the number of bytes required to store a
11861 copy of ABFD's program header table entries. Return -1 if an error
11862 occurs; bfd_get_error will return an appropriate code. */
11865 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11867 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11869 bfd_set_error (bfd_error_wrong_format);
11873 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11876 /* Copy ABFD's program header table entries to *PHDRS. The entries
11877 will be stored as an array of Elf_Internal_Phdr structures, as
11878 defined in include/elf/internal.h. To find out how large the
11879 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11881 Return the number of program header table entries read, or -1 if an
11882 error occurs; bfd_get_error will return an appropriate code. */
11885 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11889 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11891 bfd_set_error (bfd_error_wrong_format);
11895 num_phdrs = elf_elfheader (abfd)->e_phnum;
11896 if (num_phdrs != 0)
11897 memcpy (phdrs, elf_tdata (abfd)->phdr,
11898 num_phdrs * sizeof (Elf_Internal_Phdr));
11903 enum elf_reloc_type_class
11904 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11905 const asection *rel_sec ATTRIBUTE_UNUSED,
11906 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
11908 return reloc_class_normal;
11911 /* For RELA architectures, return the relocation value for a
11912 relocation against a local symbol. */
11915 _bfd_elf_rela_local_sym (bfd *abfd,
11916 Elf_Internal_Sym *sym,
11918 Elf_Internal_Rela *rel)
11920 asection *sec = *psec;
11921 bfd_vma relocation;
11923 relocation = (sec->output_section->vma
11924 + sec->output_offset
11926 if ((sec->flags & SEC_MERGE)
11927 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
11928 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
11931 _bfd_merged_section_offset (abfd, psec,
11932 elf_section_data (sec)->sec_info,
11933 sym->st_value + rel->r_addend);
11936 /* If we have changed the section, and our original section is
11937 marked with SEC_EXCLUDE, it means that the original
11938 SEC_MERGE section has been completely subsumed in some
11939 other SEC_MERGE section. In this case, we need to leave
11940 some info around for --emit-relocs. */
11941 if ((sec->flags & SEC_EXCLUDE) != 0)
11942 sec->kept_section = *psec;
11945 rel->r_addend -= relocation;
11946 rel->r_addend += sec->output_section->vma + sec->output_offset;
11952 _bfd_elf_rel_local_sym (bfd *abfd,
11953 Elf_Internal_Sym *sym,
11957 asection *sec = *psec;
11959 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
11960 return sym->st_value + addend;
11962 return _bfd_merged_section_offset (abfd, psec,
11963 elf_section_data (sec)->sec_info,
11964 sym->st_value + addend);
11967 /* Adjust an address within a section. Given OFFSET within SEC, return
11968 the new offset within the section, based upon changes made to the
11969 section. Returns -1 if the offset is now invalid.
11970 The offset (in abnd out) is in target sized bytes, however big a
11974 _bfd_elf_section_offset (bfd *abfd,
11975 struct bfd_link_info *info,
11979 switch (sec->sec_info_type)
11981 case SEC_INFO_TYPE_STABS:
11982 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11984 case SEC_INFO_TYPE_EH_FRAME:
11985 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
11988 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11990 /* Reverse the offset. */
11991 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11992 bfd_size_type address_size = bed->s->arch_size / 8;
11994 /* address_size and sec->size are in octets. Convert
11995 to bytes before subtracting the original offset. */
11996 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
12002 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12003 reconstruct an ELF file by reading the segments out of remote memory
12004 based on the ELF file header at EHDR_VMA and the ELF program headers it
12005 points to. If not null, *LOADBASEP is filled in with the difference
12006 between the VMAs from which the segments were read, and the VMAs the
12007 file headers (and hence BFD's idea of each section's VMA) put them at.
12009 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12010 remote memory at target address VMA into the local buffer at MYADDR; it
12011 should return zero on success or an `errno' code on failure. TEMPL must
12012 be a BFD for an ELF target with the word size and byte order found in
12013 the remote memory. */
12016 bfd_elf_bfd_from_remote_memory
12019 bfd_size_type size,
12020 bfd_vma *loadbasep,
12021 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12023 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12024 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12028 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12029 long symcount ATTRIBUTE_UNUSED,
12030 asymbol **syms ATTRIBUTE_UNUSED,
12035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12038 const char *relplt_name;
12039 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12043 Elf_Internal_Shdr *hdr;
12049 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12052 if (dynsymcount <= 0)
12055 if (!bed->plt_sym_val)
12058 relplt_name = bed->relplt_name;
12059 if (relplt_name == NULL)
12060 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12061 relplt = bfd_get_section_by_name (abfd, relplt_name);
12062 if (relplt == NULL)
12065 hdr = &elf_section_data (relplt)->this_hdr;
12066 if (hdr->sh_link != elf_dynsymtab (abfd)
12067 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12070 plt = bfd_get_section_by_name (abfd, ".plt");
12074 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12075 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
12078 count = relplt->size / hdr->sh_entsize;
12079 size = count * sizeof (asymbol);
12080 p = relplt->relocation;
12081 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12083 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12084 if (p->addend != 0)
12087 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12089 size += sizeof ("+0x") - 1 + 8;
12094 s = *ret = (asymbol *) bfd_malloc (size);
12098 names = (char *) (s + count);
12099 p = relplt->relocation;
12101 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12106 addr = bed->plt_sym_val (i, plt, p);
12107 if (addr == (bfd_vma) -1)
12110 *s = **p->sym_ptr_ptr;
12111 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12112 we are defining a symbol, ensure one of them is set. */
12113 if ((s->flags & BSF_LOCAL) == 0)
12114 s->flags |= BSF_GLOBAL;
12115 s->flags |= BSF_SYNTHETIC;
12117 s->value = addr - plt->vma;
12120 len = strlen ((*p->sym_ptr_ptr)->name);
12121 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12123 if (p->addend != 0)
12127 memcpy (names, "+0x", sizeof ("+0x") - 1);
12128 names += sizeof ("+0x") - 1;
12129 bfd_sprintf_vma (abfd, buf, p->addend);
12130 for (a = buf; *a == '0'; ++a)
12133 memcpy (names, a, len);
12136 memcpy (names, "@plt", sizeof ("@plt"));
12137 names += sizeof ("@plt");
12144 /* It is only used by x86-64 so far.
12145 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12146 but current usage would allow all of _bfd_std_section to be zero. */
12147 static const asymbol lcomm_sym
12148 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12149 asection _bfd_elf_large_com_section
12150 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12151 "LARGE_COMMON", 0, SEC_IS_COMMON);
12154 _bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED,
12155 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12160 _bfd_elf_final_write_processing (bfd *abfd)
12162 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
12164 i_ehdrp = elf_elfheader (abfd);
12166 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12167 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
12169 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12170 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12171 STB_GNU_UNIQUE binding. */
12172 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12174 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12175 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12176 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12177 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12179 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12180 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12181 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12182 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12183 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12184 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12185 bfd_set_error (bfd_error_bad_value);
12193 /* Return TRUE for ELF symbol types that represent functions.
12194 This is the default version of this function, which is sufficient for
12195 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12198 _bfd_elf_is_function_type (unsigned int type)
12200 return (type == STT_FUNC
12201 || type == STT_GNU_IFUNC);
12204 /* If the ELF symbol SYM might be a function in SEC, return the
12205 function size and set *CODE_OFF to the function's entry point,
12206 otherwise return zero. */
12209 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12212 bfd_size_type size;
12214 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
12215 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12216 || sym->section != sec)
12219 *code_off = sym->value;
12221 if (!(sym->flags & BSF_SYNTHETIC))
12222 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;