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 (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 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
980 if (elf_sec_group (sec) != NULL)
981 return elf_group_name (sec);
986 convert_debug_to_zdebug (bfd *abfd, const char *name)
988 unsigned int len = strlen (name);
989 char *new_name = bfd_alloc (abfd, len + 2);
990 if (new_name == NULL)
994 memcpy (new_name + 2, name + 1, len);
999 convert_zdebug_to_debug (bfd *abfd, const char *name)
1001 unsigned int len = strlen (name);
1002 char *new_name = bfd_alloc (abfd, len);
1003 if (new_name == NULL)
1006 memcpy (new_name + 1, name + 2, len - 1);
1010 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
1014 int16_t major_version;
1015 int16_t minor_version;
1016 unsigned char slim_object;
1018 /* Flags is a private field that is not defined publicly. */
1022 /* Make a BFD section from an ELF section. We store a pointer to the
1023 BFD section in the bfd_section field of the header. */
1026 _bfd_elf_make_section_from_shdr (bfd *abfd,
1027 Elf_Internal_Shdr *hdr,
1033 const struct elf_backend_data *bed;
1035 if (hdr->bfd_section != NULL)
1038 newsect = bfd_make_section_anyway (abfd, name);
1039 if (newsect == NULL)
1042 hdr->bfd_section = newsect;
1043 elf_section_data (newsect)->this_hdr = *hdr;
1044 elf_section_data (newsect)->this_idx = shindex;
1046 /* Always use the real type/flags. */
1047 elf_section_type (newsect) = hdr->sh_type;
1048 elf_section_flags (newsect) = hdr->sh_flags;
1050 newsect->filepos = hdr->sh_offset;
1052 if (!bfd_set_section_vma (newsect, hdr->sh_addr)
1053 || !bfd_set_section_size (newsect, hdr->sh_size)
1054 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
1057 flags = SEC_NO_FLAGS;
1058 if (hdr->sh_type != SHT_NOBITS)
1059 flags |= SEC_HAS_CONTENTS;
1060 if (hdr->sh_type == SHT_GROUP)
1062 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1065 if (hdr->sh_type != SHT_NOBITS)
1068 if ((hdr->sh_flags & SHF_WRITE) == 0)
1069 flags |= SEC_READONLY;
1070 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1072 else if ((flags & SEC_LOAD) != 0)
1074 if ((hdr->sh_flags & SHF_MERGE) != 0)
1077 newsect->entsize = hdr->sh_entsize;
1079 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1080 flags |= SEC_STRINGS;
1081 if (hdr->sh_flags & SHF_GROUP)
1082 if (!setup_group (abfd, hdr, newsect))
1084 if ((hdr->sh_flags & SHF_TLS) != 0)
1085 flags |= SEC_THREAD_LOCAL;
1086 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1087 flags |= SEC_EXCLUDE;
1089 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1091 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1092 but binutils as of 2019-07-23 did not set the EI_OSABI header
1096 case ELFOSABI_FREEBSD:
1097 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1098 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1102 if ((flags & SEC_ALLOC) == 0)
1104 /* The debugging sections appear to be recognized only by name,
1105 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1106 if (name [0] == '.')
1111 p = ".debug", n = 6;
1112 else if (name[1] == 'g' && name[2] == 'n')
1113 p = ".gnu.linkonce.wi.", n = 17;
1114 else if (name[1] == 'g' && name[2] == 'd')
1115 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1116 else if (name[1] == 'l')
1118 else if (name[1] == 's')
1120 else if (name[1] == 'z')
1121 p = ".zdebug", n = 7;
1124 if (p != NULL && strncmp (name, p, n) == 0)
1125 flags |= SEC_DEBUGGING;
1129 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1130 only link a single copy of the section. This is used to support
1131 g++. g++ will emit each template expansion in its own section.
1132 The symbols will be defined as weak, so that multiple definitions
1133 are permitted. The GNU linker extension is to actually discard
1134 all but one of the sections. */
1135 if (CONST_STRNEQ (name, ".gnu.linkonce")
1136 && elf_next_in_group (newsect) == NULL)
1137 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1139 bed = get_elf_backend_data (abfd);
1140 if (bed->elf_backend_section_flags)
1141 if (! bed->elf_backend_section_flags (&flags, hdr))
1144 if (!bfd_set_section_flags (newsect, flags))
1147 /* We do not parse the PT_NOTE segments as we are interested even in the
1148 separate debug info files which may have the segments offsets corrupted.
1149 PT_NOTEs from the core files are currently not parsed using BFD. */
1150 if (hdr->sh_type == SHT_NOTE)
1154 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1157 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1158 hdr->sh_offset, hdr->sh_addralign);
1162 if ((flags & SEC_ALLOC) != 0)
1164 Elf_Internal_Phdr *phdr;
1165 unsigned int i, nload;
1167 /* Some ELF linkers produce binaries with all the program header
1168 p_paddr fields zero. If we have such a binary with more than
1169 one PT_LOAD header, then leave the section lma equal to vma
1170 so that we don't create sections with overlapping lma. */
1171 phdr = elf_tdata (abfd)->phdr;
1172 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1173 if (phdr->p_paddr != 0)
1175 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1177 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1180 phdr = elf_tdata (abfd)->phdr;
1181 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1183 if (((phdr->p_type == PT_LOAD
1184 && (hdr->sh_flags & SHF_TLS) == 0)
1185 || phdr->p_type == PT_TLS)
1186 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1188 if ((flags & SEC_LOAD) == 0)
1189 newsect->lma = (phdr->p_paddr
1190 + hdr->sh_addr - phdr->p_vaddr);
1192 /* We used to use the same adjustment for SEC_LOAD
1193 sections, but that doesn't work if the segment
1194 is packed with code from multiple VMAs.
1195 Instead we calculate the section LMA based on
1196 the segment LMA. It is assumed that the
1197 segment will contain sections with contiguous
1198 LMAs, even if the VMAs are not. */
1199 newsect->lma = (phdr->p_paddr
1200 + hdr->sh_offset - phdr->p_offset);
1202 /* With contiguous segments, we can't tell from file
1203 offsets whether a section with zero size should
1204 be placed at the end of one segment or the
1205 beginning of the next. Decide based on vaddr. */
1206 if (hdr->sh_addr >= phdr->p_vaddr
1207 && (hdr->sh_addr + hdr->sh_size
1208 <= phdr->p_vaddr + phdr->p_memsz))
1214 /* Compress/decompress DWARF debug sections with names: .debug_* and
1215 .zdebug_*, after the section flags is set. */
1216 if ((flags & SEC_DEBUGGING)
1217 && ((name[1] == 'd' && name[6] == '_')
1218 || (name[1] == 'z' && name[7] == '_')))
1220 enum { nothing, compress, decompress } action = nothing;
1221 int compression_header_size;
1222 bfd_size_type uncompressed_size;
1223 unsigned int uncompressed_align_power;
1224 bfd_boolean compressed
1225 = bfd_is_section_compressed_with_header (abfd, newsect,
1226 &compression_header_size,
1228 &uncompressed_align_power);
1231 /* Compressed section. Check if we should decompress. */
1232 if ((abfd->flags & BFD_DECOMPRESS))
1233 action = decompress;
1236 /* Compress the uncompressed section or convert from/to .zdebug*
1237 section. Check if we should compress. */
1238 if (action == nothing)
1240 if (newsect->size != 0
1241 && (abfd->flags & BFD_COMPRESS)
1242 && compression_header_size >= 0
1243 && uncompressed_size > 0
1245 || ((compression_header_size > 0)
1246 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1252 if (action == compress)
1254 if (!bfd_init_section_compress_status (abfd, newsect))
1257 /* xgettext:c-format */
1258 (_("%pB: unable to initialize compress status for section %s"),
1265 if (!bfd_init_section_decompress_status (abfd, newsect))
1268 /* xgettext:c-format */
1269 (_("%pB: unable to initialize decompress status for section %s"),
1275 if (abfd->is_linker_input)
1278 && (action == decompress
1279 || (action == compress
1280 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1282 /* Convert section name from .zdebug_* to .debug_* so
1283 that linker will consider this section as a debug
1285 char *new_name = convert_zdebug_to_debug (abfd, name);
1286 if (new_name == NULL)
1288 bfd_rename_section (newsect, new_name);
1292 /* For objdump, don't rename the section. For objcopy, delay
1293 section rename to elf_fake_sections. */
1294 newsect->flags |= SEC_ELF_RENAME;
1297 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1299 const char *lto_section_name = ".gnu.lto_.lto.";
1300 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1302 struct lto_section lsection;
1303 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1304 sizeof (struct lto_section)))
1305 abfd->lto_slim_object = lsection.slim_object;
1311 const char *const bfd_elf_section_type_names[] =
1313 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1314 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1315 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1318 /* ELF relocs are against symbols. If we are producing relocatable
1319 output, and the reloc is against an external symbol, and nothing
1320 has given us any additional addend, the resulting reloc will also
1321 be against the same symbol. In such a case, we don't want to
1322 change anything about the way the reloc is handled, since it will
1323 all be done at final link time. Rather than put special case code
1324 into bfd_perform_relocation, all the reloc types use this howto
1325 function. It just short circuits the reloc if producing
1326 relocatable output against an external symbol. */
1328 bfd_reloc_status_type
1329 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1330 arelent *reloc_entry,
1332 void *data ATTRIBUTE_UNUSED,
1333 asection *input_section,
1335 char **error_message ATTRIBUTE_UNUSED)
1337 if (output_bfd != NULL
1338 && (symbol->flags & BSF_SECTION_SYM) == 0
1339 && (! reloc_entry->howto->partial_inplace
1340 || reloc_entry->addend == 0))
1342 reloc_entry->address += input_section->output_offset;
1343 return bfd_reloc_ok;
1346 return bfd_reloc_continue;
1349 /* Returns TRUE if section A matches section B.
1350 Names, addresses and links may be different, but everything else
1351 should be the same. */
1354 section_match (const Elf_Internal_Shdr * a,
1355 const Elf_Internal_Shdr * b)
1357 if (a->sh_type != b->sh_type
1358 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1359 || a->sh_addralign != b->sh_addralign
1360 || a->sh_entsize != b->sh_entsize)
1362 if (a->sh_type == SHT_SYMTAB
1363 || a->sh_type == SHT_STRTAB)
1365 return a->sh_size == b->sh_size;
1368 /* Find a section in OBFD that has the same characteristics
1369 as IHEADER. Return the index of this section or SHN_UNDEF if
1370 none can be found. Check's section HINT first, as this is likely
1371 to be the correct section. */
1374 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1375 const unsigned int hint)
1377 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1380 BFD_ASSERT (iheader != NULL);
1382 /* See PR 20922 for a reproducer of the NULL test. */
1383 if (hint < elf_numsections (obfd)
1384 && oheaders[hint] != NULL
1385 && section_match (oheaders[hint], iheader))
1388 for (i = 1; i < elf_numsections (obfd); i++)
1390 Elf_Internal_Shdr * oheader = oheaders[i];
1392 if (oheader == NULL)
1394 if (section_match (oheader, iheader))
1395 /* FIXME: Do we care if there is a potential for
1396 multiple matches ? */
1403 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1404 Processor specific section, based upon a matching input section.
1405 Returns TRUE upon success, FALSE otherwise. */
1408 copy_special_section_fields (const bfd *ibfd,
1410 const Elf_Internal_Shdr *iheader,
1411 Elf_Internal_Shdr *oheader,
1412 const unsigned int secnum)
1414 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1415 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1416 bfd_boolean changed = FALSE;
1417 unsigned int sh_link;
1419 if (oheader->sh_type == SHT_NOBITS)
1421 /* This is a feature for objcopy --only-keep-debug:
1422 When a section's type is changed to NOBITS, we preserve
1423 the sh_link and sh_info fields so that they can be
1424 matched up with the original.
1426 Note: Strictly speaking these assignments are wrong.
1427 The sh_link and sh_info fields should point to the
1428 relevent sections in the output BFD, which may not be in
1429 the same location as they were in the input BFD. But
1430 the whole point of this action is to preserve the
1431 original values of the sh_link and sh_info fields, so
1432 that they can be matched up with the section headers in
1433 the original file. So strictly speaking we may be
1434 creating an invalid ELF file, but it is only for a file
1435 that just contains debug info and only for sections
1436 without any contents. */
1437 if (oheader->sh_link == 0)
1438 oheader->sh_link = iheader->sh_link;
1439 if (oheader->sh_info == 0)
1440 oheader->sh_info = iheader->sh_info;
1444 /* Allow the target a chance to decide how these fields should be set. */
1445 if (bed->elf_backend_copy_special_section_fields != NULL
1446 && bed->elf_backend_copy_special_section_fields
1447 (ibfd, obfd, iheader, oheader))
1450 /* We have an iheader which might match oheader, and which has non-zero
1451 sh_info and/or sh_link fields. Attempt to follow those links and find
1452 the section in the output bfd which corresponds to the linked section
1453 in the input bfd. */
1454 if (iheader->sh_link != SHN_UNDEF)
1456 /* See PR 20931 for a reproducer. */
1457 if (iheader->sh_link >= elf_numsections (ibfd))
1460 /* xgettext:c-format */
1461 (_("%pB: invalid sh_link field (%d) in section number %d"),
1462 ibfd, iheader->sh_link, secnum);
1466 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1467 if (sh_link != SHN_UNDEF)
1469 oheader->sh_link = sh_link;
1473 /* FIXME: Should we install iheader->sh_link
1474 if we could not find a match ? */
1476 /* xgettext:c-format */
1477 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1480 if (iheader->sh_info)
1482 /* The sh_info field can hold arbitrary information, but if the
1483 SHF_LINK_INFO flag is set then it should be interpreted as a
1485 if (iheader->sh_flags & SHF_INFO_LINK)
1487 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1489 if (sh_link != SHN_UNDEF)
1490 oheader->sh_flags |= SHF_INFO_LINK;
1493 /* No idea what it means - just copy it. */
1494 sh_link = iheader->sh_info;
1496 if (sh_link != SHN_UNDEF)
1498 oheader->sh_info = sh_link;
1503 /* xgettext:c-format */
1504 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1510 /* Copy the program header and other data from one object module to
1514 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1516 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1517 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1518 const struct elf_backend_data *bed;
1521 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1522 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1525 if (!elf_flags_init (obfd))
1527 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1528 elf_flags_init (obfd) = TRUE;
1531 elf_gp (obfd) = elf_gp (ibfd);
1533 /* Also copy the EI_OSABI field. */
1534 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1535 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1537 /* If set, copy the EI_ABIVERSION field. */
1538 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1539 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1540 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1542 /* Copy object attributes. */
1543 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1545 if (iheaders == NULL || oheaders == NULL)
1548 bed = get_elf_backend_data (obfd);
1550 /* Possibly copy other fields in the section header. */
1551 for (i = 1; i < elf_numsections (obfd); i++)
1554 Elf_Internal_Shdr * oheader = oheaders[i];
1556 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1557 because of a special case need for generating separate debug info
1558 files. See below for more details. */
1560 || (oheader->sh_type != SHT_NOBITS
1561 && oheader->sh_type < SHT_LOOS))
1564 /* Ignore empty sections, and sections whose
1565 fields have already been initialised. */
1566 if (oheader->sh_size == 0
1567 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1570 /* Scan for the matching section in the input bfd.
1571 First we try for a direct mapping between the input and output sections. */
1572 for (j = 1; j < elf_numsections (ibfd); j++)
1574 const Elf_Internal_Shdr * iheader = iheaders[j];
1576 if (iheader == NULL)
1579 if (oheader->bfd_section != NULL
1580 && iheader->bfd_section != NULL
1581 && iheader->bfd_section->output_section != NULL
1582 && iheader->bfd_section->output_section == oheader->bfd_section)
1584 /* We have found a connection from the input section to the
1585 output section. Attempt to copy the header fields. If
1586 this fails then do not try any further sections - there
1587 should only be a one-to-one mapping between input and output. */
1588 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1589 j = elf_numsections (ibfd);
1594 if (j < elf_numsections (ibfd))
1597 /* That failed. So try to deduce the corresponding input section.
1598 Unfortunately we cannot compare names as the output string table
1599 is empty, so instead we check size, address and type. */
1600 for (j = 1; j < elf_numsections (ibfd); j++)
1602 const Elf_Internal_Shdr * iheader = iheaders[j];
1604 if (iheader == NULL)
1607 /* Try matching fields in the input section's header.
1608 Since --only-keep-debug turns all non-debug sections into
1609 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1611 if ((oheader->sh_type == SHT_NOBITS
1612 || iheader->sh_type == oheader->sh_type)
1613 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1614 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1615 && iheader->sh_addralign == oheader->sh_addralign
1616 && iheader->sh_entsize == oheader->sh_entsize
1617 && iheader->sh_size == oheader->sh_size
1618 && iheader->sh_addr == oheader->sh_addr
1619 && (iheader->sh_info != oheader->sh_info
1620 || iheader->sh_link != oheader->sh_link))
1622 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1627 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1629 /* Final attempt. Call the backend copy function
1630 with a NULL input section. */
1631 if (bed->elf_backend_copy_special_section_fields != NULL)
1632 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1640 get_segment_type (unsigned int p_type)
1645 case PT_NULL: pt = "NULL"; break;
1646 case PT_LOAD: pt = "LOAD"; break;
1647 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1648 case PT_INTERP: pt = "INTERP"; break;
1649 case PT_NOTE: pt = "NOTE"; break;
1650 case PT_SHLIB: pt = "SHLIB"; break;
1651 case PT_PHDR: pt = "PHDR"; break;
1652 case PT_TLS: pt = "TLS"; break;
1653 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1654 case PT_GNU_STACK: pt = "STACK"; break;
1655 case PT_GNU_RELRO: pt = "RELRO"; break;
1656 default: pt = NULL; break;
1661 /* Print out the program headers. */
1664 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1666 FILE *f = (FILE *) farg;
1667 Elf_Internal_Phdr *p;
1669 bfd_byte *dynbuf = NULL;
1671 p = elf_tdata (abfd)->phdr;
1676 fprintf (f, _("\nProgram Header:\n"));
1677 c = elf_elfheader (abfd)->e_phnum;
1678 for (i = 0; i < c; i++, p++)
1680 const char *pt = get_segment_type (p->p_type);
1685 sprintf (buf, "0x%lx", p->p_type);
1688 fprintf (f, "%8s off 0x", pt);
1689 bfd_fprintf_vma (abfd, f, p->p_offset);
1690 fprintf (f, " vaddr 0x");
1691 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1692 fprintf (f, " paddr 0x");
1693 bfd_fprintf_vma (abfd, f, p->p_paddr);
1694 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1695 fprintf (f, " filesz 0x");
1696 bfd_fprintf_vma (abfd, f, p->p_filesz);
1697 fprintf (f, " memsz 0x");
1698 bfd_fprintf_vma (abfd, f, p->p_memsz);
1699 fprintf (f, " flags %c%c%c",
1700 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1701 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1702 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1703 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1704 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1709 s = bfd_get_section_by_name (abfd, ".dynamic");
1712 unsigned int elfsec;
1713 unsigned long shlink;
1714 bfd_byte *extdyn, *extdynend;
1716 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1718 fprintf (f, _("\nDynamic Section:\n"));
1720 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1723 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1724 if (elfsec == SHN_BAD)
1726 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1728 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1729 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1732 /* PR 17512: file: 6f427532. */
1733 if (s->size < extdynsize)
1735 extdynend = extdyn + s->size;
1736 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1738 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1740 Elf_Internal_Dyn dyn;
1741 const char *name = "";
1743 bfd_boolean stringp;
1744 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1746 (*swap_dyn_in) (abfd, extdyn, &dyn);
1748 if (dyn.d_tag == DT_NULL)
1755 if (bed->elf_backend_get_target_dtag)
1756 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1758 if (!strcmp (name, ""))
1760 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1765 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1766 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1767 case DT_PLTGOT: name = "PLTGOT"; break;
1768 case DT_HASH: name = "HASH"; break;
1769 case DT_STRTAB: name = "STRTAB"; break;
1770 case DT_SYMTAB: name = "SYMTAB"; break;
1771 case DT_RELA: name = "RELA"; break;
1772 case DT_RELASZ: name = "RELASZ"; break;
1773 case DT_RELAENT: name = "RELAENT"; break;
1774 case DT_STRSZ: name = "STRSZ"; break;
1775 case DT_SYMENT: name = "SYMENT"; break;
1776 case DT_INIT: name = "INIT"; break;
1777 case DT_FINI: name = "FINI"; break;
1778 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1779 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1780 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1781 case DT_REL: name = "REL"; break;
1782 case DT_RELSZ: name = "RELSZ"; break;
1783 case DT_RELENT: name = "RELENT"; break;
1784 case DT_PLTREL: name = "PLTREL"; break;
1785 case DT_DEBUG: name = "DEBUG"; break;
1786 case DT_TEXTREL: name = "TEXTREL"; break;
1787 case DT_JMPREL: name = "JMPREL"; break;
1788 case DT_BIND_NOW: name = "BIND_NOW"; break;
1789 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1790 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1791 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1792 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1793 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1794 case DT_FLAGS: name = "FLAGS"; break;
1795 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1796 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1797 case DT_CHECKSUM: name = "CHECKSUM"; break;
1798 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1799 case DT_MOVEENT: name = "MOVEENT"; break;
1800 case DT_MOVESZ: name = "MOVESZ"; break;
1801 case DT_FEATURE: name = "FEATURE"; break;
1802 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1803 case DT_SYMINSZ: name = "SYMINSZ"; break;
1804 case DT_SYMINENT: name = "SYMINENT"; break;
1805 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1806 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1807 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1808 case DT_PLTPAD: name = "PLTPAD"; break;
1809 case DT_MOVETAB: name = "MOVETAB"; break;
1810 case DT_SYMINFO: name = "SYMINFO"; break;
1811 case DT_RELACOUNT: name = "RELACOUNT"; break;
1812 case DT_RELCOUNT: name = "RELCOUNT"; break;
1813 case DT_FLAGS_1: name = "FLAGS_1"; break;
1814 case DT_VERSYM: name = "VERSYM"; break;
1815 case DT_VERDEF: name = "VERDEF"; break;
1816 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1817 case DT_VERNEED: name = "VERNEED"; break;
1818 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1819 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1820 case DT_USED: name = "USED"; break;
1821 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1822 case DT_GNU_HASH: name = "GNU_HASH"; break;
1825 fprintf (f, " %-20s ", name);
1829 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1834 unsigned int tagv = dyn.d_un.d_val;
1836 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1839 fprintf (f, "%s", string);
1848 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1849 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1851 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1855 if (elf_dynverdef (abfd) != 0)
1857 Elf_Internal_Verdef *t;
1859 fprintf (f, _("\nVersion definitions:\n"));
1860 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1862 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1863 t->vd_flags, t->vd_hash,
1864 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1865 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1867 Elf_Internal_Verdaux *a;
1870 for (a = t->vd_auxptr->vda_nextptr;
1874 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1880 if (elf_dynverref (abfd) != 0)
1882 Elf_Internal_Verneed *t;
1884 fprintf (f, _("\nVersion References:\n"));
1885 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1887 Elf_Internal_Vernaux *a;
1889 fprintf (f, _(" required from %s:\n"),
1890 t->vn_filename ? t->vn_filename : "<corrupt>");
1891 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1892 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1893 a->vna_flags, a->vna_other,
1894 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1906 /* Get version string. */
1909 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1910 bfd_boolean *hidden)
1912 const char *version_string = NULL;
1913 if (elf_dynversym (abfd) != 0
1914 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1916 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1918 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1919 vernum &= VERSYM_VERSION;
1922 version_string = "";
1923 else if (vernum == 1
1924 && (vernum > elf_tdata (abfd)->cverdefs
1925 || (elf_tdata (abfd)->verdef[0].vd_flags
1927 version_string = "Base";
1928 else if (vernum <= elf_tdata (abfd)->cverdefs)
1930 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1933 Elf_Internal_Verneed *t;
1935 version_string = _("<corrupt>");
1936 for (t = elf_tdata (abfd)->verref;
1940 Elf_Internal_Vernaux *a;
1942 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1944 if (a->vna_other == vernum)
1946 version_string = a->vna_nodename;
1953 return version_string;
1956 /* Display ELF-specific fields of a symbol. */
1959 bfd_elf_print_symbol (bfd *abfd,
1962 bfd_print_symbol_type how)
1964 FILE *file = (FILE *) filep;
1967 case bfd_print_symbol_name:
1968 fprintf (file, "%s", symbol->name);
1970 case bfd_print_symbol_more:
1971 fprintf (file, "elf ");
1972 bfd_fprintf_vma (abfd, file, symbol->value);
1973 fprintf (file, " %x", symbol->flags);
1975 case bfd_print_symbol_all:
1977 const char *section_name;
1978 const char *name = NULL;
1979 const struct elf_backend_data *bed;
1980 unsigned char st_other;
1982 const char *version_string;
1985 section_name = symbol->section ? symbol->section->name : "(*none*)";
1987 bed = get_elf_backend_data (abfd);
1988 if (bed->elf_backend_print_symbol_all)
1989 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1993 name = symbol->name;
1994 bfd_print_symbol_vandf (abfd, file, symbol);
1997 fprintf (file, " %s\t", section_name);
1998 /* Print the "other" value for a symbol. For common symbols,
1999 we've already printed the size; now print the alignment.
2000 For other symbols, we have no specified alignment, and
2001 we've printed the address; now print the size. */
2002 if (symbol->section && bfd_is_com_section (symbol->section))
2003 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2005 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2006 bfd_fprintf_vma (abfd, file, val);
2008 /* If we have version information, print it. */
2009 version_string = _bfd_elf_get_symbol_version_string (abfd,
2015 fprintf (file, " %-11s", version_string);
2020 fprintf (file, " (%s)", version_string);
2021 for (i = 10 - strlen (version_string); i > 0; --i)
2026 /* If the st_other field is not zero, print it. */
2027 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2032 case STV_INTERNAL: fprintf (file, " .internal"); break;
2033 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2034 case STV_PROTECTED: fprintf (file, " .protected"); break;
2036 /* Some other non-defined flags are also present, so print
2038 fprintf (file, " 0x%02x", (unsigned int) st_other);
2041 fprintf (file, " %s", name);
2047 /* ELF .o/exec file reading */
2049 /* Create a new bfd section from an ELF section header. */
2052 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2054 Elf_Internal_Shdr *hdr;
2055 Elf_Internal_Ehdr *ehdr;
2056 const struct elf_backend_data *bed;
2058 bfd_boolean ret = TRUE;
2059 static bfd_boolean * sections_being_created = NULL;
2060 static bfd * sections_being_created_abfd = NULL;
2061 static unsigned int nesting = 0;
2063 if (shindex >= elf_numsections (abfd))
2068 /* PR17512: A corrupt ELF binary might contain a recursive group of
2069 sections, with each the string indices pointing to the next in the
2070 loop. Detect this here, by refusing to load a section that we are
2071 already in the process of loading. We only trigger this test if
2072 we have nested at least three sections deep as normal ELF binaries
2073 can expect to recurse at least once.
2075 FIXME: It would be better if this array was attached to the bfd,
2076 rather than being held in a static pointer. */
2078 if (sections_being_created_abfd != abfd)
2079 sections_being_created = NULL;
2080 if (sections_being_created == NULL)
2082 sections_being_created = (bfd_boolean *)
2083 bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean));
2084 sections_being_created_abfd = abfd;
2086 if (sections_being_created [shindex])
2089 (_("%pB: warning: loop in section dependencies detected"), abfd);
2092 sections_being_created [shindex] = TRUE;
2095 hdr = elf_elfsections (abfd)[shindex];
2096 ehdr = elf_elfheader (abfd);
2097 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2102 bed = get_elf_backend_data (abfd);
2103 switch (hdr->sh_type)
2106 /* Inactive section. Throw it away. */
2109 case SHT_PROGBITS: /* Normal section with contents. */
2110 case SHT_NOBITS: /* .bss section. */
2111 case SHT_HASH: /* .hash section. */
2112 case SHT_NOTE: /* .note section. */
2113 case SHT_INIT_ARRAY: /* .init_array section. */
2114 case SHT_FINI_ARRAY: /* .fini_array section. */
2115 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2116 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2117 case SHT_GNU_HASH: /* .gnu.hash section. */
2118 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2121 case SHT_DYNAMIC: /* Dynamic linking information. */
2122 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2125 if (hdr->sh_link > elf_numsections (abfd))
2127 /* PR 10478: Accept Solaris binaries with a sh_link
2128 field set to SHN_BEFORE or SHN_AFTER. */
2129 switch (bfd_get_arch (abfd))
2132 case bfd_arch_sparc:
2133 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2134 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2136 /* Otherwise fall through. */
2141 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2143 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2145 Elf_Internal_Shdr *dynsymhdr;
2147 /* The shared libraries distributed with hpux11 have a bogus
2148 sh_link field for the ".dynamic" section. Find the
2149 string table for the ".dynsym" section instead. */
2150 if (elf_dynsymtab (abfd) != 0)
2152 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2153 hdr->sh_link = dynsymhdr->sh_link;
2157 unsigned int i, num_sec;
2159 num_sec = elf_numsections (abfd);
2160 for (i = 1; i < num_sec; i++)
2162 dynsymhdr = elf_elfsections (abfd)[i];
2163 if (dynsymhdr->sh_type == SHT_DYNSYM)
2165 hdr->sh_link = dynsymhdr->sh_link;
2173 case SHT_SYMTAB: /* A symbol table. */
2174 if (elf_onesymtab (abfd) == shindex)
2177 if (hdr->sh_entsize != bed->s->sizeof_sym)
2180 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2182 if (hdr->sh_size != 0)
2184 /* Some assemblers erroneously set sh_info to one with a
2185 zero sh_size. ld sees this as a global symbol count
2186 of (unsigned) -1. Fix it here. */
2191 /* PR 18854: A binary might contain more than one symbol table.
2192 Unusual, but possible. Warn, but continue. */
2193 if (elf_onesymtab (abfd) != 0)
2196 /* xgettext:c-format */
2197 (_("%pB: warning: multiple symbol tables detected"
2198 " - ignoring the table in section %u"),
2202 elf_onesymtab (abfd) = shindex;
2203 elf_symtab_hdr (abfd) = *hdr;
2204 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2205 abfd->flags |= HAS_SYMS;
2207 /* Sometimes a shared object will map in the symbol table. If
2208 SHF_ALLOC is set, and this is a shared object, then we also
2209 treat this section as a BFD section. We can not base the
2210 decision purely on SHF_ALLOC, because that flag is sometimes
2211 set in a relocatable object file, which would confuse the
2213 if ((hdr->sh_flags & SHF_ALLOC) != 0
2214 && (abfd->flags & DYNAMIC) != 0
2215 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2219 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2220 can't read symbols without that section loaded as well. It
2221 is most likely specified by the next section header. */
2223 elf_section_list * entry;
2224 unsigned int i, num_sec;
2226 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2227 if (entry->hdr.sh_link == shindex)
2230 num_sec = elf_numsections (abfd);
2231 for (i = shindex + 1; i < num_sec; i++)
2233 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2235 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2236 && hdr2->sh_link == shindex)
2241 for (i = 1; i < shindex; i++)
2243 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2245 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2246 && hdr2->sh_link == shindex)
2251 ret = bfd_section_from_shdr (abfd, i);
2252 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2256 case SHT_DYNSYM: /* A dynamic symbol table. */
2257 if (elf_dynsymtab (abfd) == shindex)
2260 if (hdr->sh_entsize != bed->s->sizeof_sym)
2263 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2265 if (hdr->sh_size != 0)
2268 /* Some linkers erroneously set sh_info to one with a
2269 zero sh_size. ld sees this as a global symbol count
2270 of (unsigned) -1. Fix it here. */
2275 /* PR 18854: A binary might contain more than one dynamic symbol table.
2276 Unusual, but possible. Warn, but continue. */
2277 if (elf_dynsymtab (abfd) != 0)
2280 /* xgettext:c-format */
2281 (_("%pB: warning: multiple dynamic symbol tables detected"
2282 " - ignoring the table in section %u"),
2286 elf_dynsymtab (abfd) = shindex;
2287 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2288 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2289 abfd->flags |= HAS_SYMS;
2291 /* Besides being a symbol table, we also treat this as a regular
2292 section, so that objcopy can handle it. */
2293 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2296 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2298 elf_section_list * entry;
2300 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2301 if (entry->ndx == shindex)
2304 entry = bfd_alloc (abfd, sizeof (*entry));
2307 entry->ndx = shindex;
2309 entry->next = elf_symtab_shndx_list (abfd);
2310 elf_symtab_shndx_list (abfd) = entry;
2311 elf_elfsections (abfd)[shindex] = & entry->hdr;
2315 case SHT_STRTAB: /* A string table. */
2316 if (hdr->bfd_section != NULL)
2319 if (ehdr->e_shstrndx == shindex)
2321 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2322 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2326 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2329 elf_tdata (abfd)->strtab_hdr = *hdr;
2330 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2334 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2337 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2338 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2339 elf_elfsections (abfd)[shindex] = hdr;
2340 /* We also treat this as a regular section, so that objcopy
2342 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2347 /* If the string table isn't one of the above, then treat it as a
2348 regular section. We need to scan all the headers to be sure,
2349 just in case this strtab section appeared before the above. */
2350 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2352 unsigned int i, num_sec;
2354 num_sec = elf_numsections (abfd);
2355 for (i = 1; i < num_sec; i++)
2357 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2358 if (hdr2->sh_link == shindex)
2360 /* Prevent endless recursion on broken objects. */
2363 if (! bfd_section_from_shdr (abfd, i))
2365 if (elf_onesymtab (abfd) == i)
2367 if (elf_dynsymtab (abfd) == i)
2368 goto dynsymtab_strtab;
2372 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2377 /* *These* do a lot of work -- but build no sections! */
2379 asection *target_sect;
2380 Elf_Internal_Shdr *hdr2, **p_hdr;
2381 unsigned int num_sec = elf_numsections (abfd);
2382 struct bfd_elf_section_data *esdt;
2385 != (bfd_size_type) (hdr->sh_type == SHT_REL
2386 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2389 /* Check for a bogus link to avoid crashing. */
2390 if (hdr->sh_link >= num_sec)
2393 /* xgettext:c-format */
2394 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2395 abfd, hdr->sh_link, name, shindex);
2396 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2401 /* For some incomprehensible reason Oracle distributes
2402 libraries for Solaris in which some of the objects have
2403 bogus sh_link fields. It would be nice if we could just
2404 reject them, but, unfortunately, some people need to use
2405 them. We scan through the section headers; if we find only
2406 one suitable symbol table, we clobber the sh_link to point
2407 to it. I hope this doesn't break anything.
2409 Don't do it on executable nor shared library. */
2410 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2411 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2412 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2418 for (scan = 1; scan < num_sec; scan++)
2420 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2421 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2432 hdr->sh_link = found;
2435 /* Get the symbol table. */
2436 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2437 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2438 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2441 /* If this is an alloc section in an executable or shared
2442 library, or the reloc section does not use the main symbol
2443 table we don't treat it as a reloc section. BFD can't
2444 adequately represent such a section, so at least for now,
2445 we don't try. We just present it as a normal section. We
2446 also can't use it as a reloc section if it points to the
2447 null section, an invalid section, another reloc section, or
2448 its sh_link points to the null section. */
2449 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2450 && (hdr->sh_flags & SHF_ALLOC) != 0)
2451 || hdr->sh_link == SHN_UNDEF
2452 || hdr->sh_link != elf_onesymtab (abfd)
2453 || hdr->sh_info == SHN_UNDEF
2454 || hdr->sh_info >= num_sec
2455 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2456 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2458 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2463 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2466 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2467 if (target_sect == NULL)
2470 esdt = elf_section_data (target_sect);
2471 if (hdr->sh_type == SHT_RELA)
2472 p_hdr = &esdt->rela.hdr;
2474 p_hdr = &esdt->rel.hdr;
2476 /* PR 17512: file: 0b4f81b7.
2477 Also see PR 24456, for a file which deliberately has two reloc
2482 /* xgettext:c-format */
2483 (_("%pB: warning: multiple relocation sections for section %pA \
2484 found - ignoring all but the first"),
2488 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2493 elf_elfsections (abfd)[shindex] = hdr2;
2494 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2495 * bed->s->int_rels_per_ext_rel);
2496 target_sect->flags |= SEC_RELOC;
2497 target_sect->relocation = NULL;
2498 target_sect->rel_filepos = hdr->sh_offset;
2499 /* In the section to which the relocations apply, mark whether
2500 its relocations are of the REL or RELA variety. */
2501 if (hdr->sh_size != 0)
2503 if (hdr->sh_type == SHT_RELA)
2504 target_sect->use_rela_p = 1;
2506 abfd->flags |= HAS_RELOC;
2510 case SHT_GNU_verdef:
2511 elf_dynverdef (abfd) = shindex;
2512 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2513 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2516 case SHT_GNU_versym:
2517 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2520 elf_dynversym (abfd) = shindex;
2521 elf_tdata (abfd)->dynversym_hdr = *hdr;
2522 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2525 case SHT_GNU_verneed:
2526 elf_dynverref (abfd) = shindex;
2527 elf_tdata (abfd)->dynverref_hdr = *hdr;
2528 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2535 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2538 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2544 /* Possibly an attributes section. */
2545 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2546 || hdr->sh_type == bed->obj_attrs_section_type)
2548 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2550 _bfd_elf_parse_attributes (abfd, hdr);
2554 /* Check for any processor-specific section types. */
2555 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2558 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2560 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2561 /* FIXME: How to properly handle allocated section reserved
2562 for applications? */
2564 /* xgettext:c-format */
2565 (_("%pB: unknown type [%#x] section `%s'"),
2566 abfd, hdr->sh_type, name);
2569 /* Allow sections reserved for applications. */
2570 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2575 else if (hdr->sh_type >= SHT_LOPROC
2576 && hdr->sh_type <= SHT_HIPROC)
2577 /* FIXME: We should handle this section. */
2579 /* xgettext:c-format */
2580 (_("%pB: unknown type [%#x] section `%s'"),
2581 abfd, hdr->sh_type, name);
2582 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2584 /* Unrecognised OS-specific sections. */
2585 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2586 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2587 required to correctly process the section and the file should
2588 be rejected with an error message. */
2590 /* xgettext:c-format */
2591 (_("%pB: unknown type [%#x] section `%s'"),
2592 abfd, hdr->sh_type, name);
2595 /* Otherwise it should be processed. */
2596 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2601 /* FIXME: We should handle this section. */
2603 /* xgettext:c-format */
2604 (_("%pB: unknown type [%#x] section `%s'"),
2605 abfd, hdr->sh_type, name);
2613 if (sections_being_created && sections_being_created_abfd == abfd)
2614 sections_being_created [shindex] = FALSE;
2615 if (-- nesting == 0)
2617 sections_being_created = NULL;
2618 sections_being_created_abfd = abfd;
2623 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2626 bfd_sym_from_r_symndx (struct sym_cache *cache,
2628 unsigned long r_symndx)
2630 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2632 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2634 Elf_Internal_Shdr *symtab_hdr;
2635 unsigned char esym[sizeof (Elf64_External_Sym)];
2636 Elf_External_Sym_Shndx eshndx;
2638 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2639 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2640 &cache->sym[ent], esym, &eshndx) == NULL)
2643 if (cache->abfd != abfd)
2645 memset (cache->indx, -1, sizeof (cache->indx));
2648 cache->indx[ent] = r_symndx;
2651 return &cache->sym[ent];
2654 /* Given an ELF section number, retrieve the corresponding BFD
2658 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2660 if (sec_index >= elf_numsections (abfd))
2662 return elf_elfsections (abfd)[sec_index]->bfd_section;
2665 static const struct bfd_elf_special_section special_sections_b[] =
2667 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2668 { NULL, 0, 0, 0, 0 }
2671 static const struct bfd_elf_special_section special_sections_c[] =
2673 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2674 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2675 { NULL, 0, 0, 0, 0 }
2678 static const struct bfd_elf_special_section special_sections_d[] =
2680 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2681 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2682 /* There are more DWARF sections than these, but they needn't be added here
2683 unless you have to cope with broken compilers that don't emit section
2684 attributes or you want to help the user writing assembler. */
2685 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2686 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2687 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2688 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2689 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2690 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2691 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2692 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2693 { NULL, 0, 0, 0, 0 }
2696 static const struct bfd_elf_special_section special_sections_f[] =
2698 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2699 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2700 { NULL, 0 , 0, 0, 0 }
2703 static const struct bfd_elf_special_section special_sections_g[] =
2705 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2706 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2707 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2708 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2709 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2710 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2711 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2712 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2713 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2714 { NULL, 0, 0, 0, 0 }
2717 static const struct bfd_elf_special_section special_sections_h[] =
2719 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2720 { NULL, 0, 0, 0, 0 }
2723 static const struct bfd_elf_special_section special_sections_i[] =
2725 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2726 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2727 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2728 { NULL, 0, 0, 0, 0 }
2731 static const struct bfd_elf_special_section special_sections_l[] =
2733 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2734 { NULL, 0, 0, 0, 0 }
2737 static const struct bfd_elf_special_section special_sections_n[] =
2739 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2740 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2741 { NULL, 0, 0, 0, 0 }
2744 static const struct bfd_elf_special_section special_sections_p[] =
2746 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2747 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2748 { NULL, 0, 0, 0, 0 }
2751 static const struct bfd_elf_special_section special_sections_r[] =
2753 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2754 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2755 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2756 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2757 { NULL, 0, 0, 0, 0 }
2760 static const struct bfd_elf_special_section special_sections_s[] =
2762 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2763 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2764 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2765 /* See struct bfd_elf_special_section declaration for the semantics of
2766 this special case where .prefix_length != strlen (.prefix). */
2767 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2768 { NULL, 0, 0, 0, 0 }
2771 static const struct bfd_elf_special_section special_sections_t[] =
2773 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2774 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2775 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2776 { NULL, 0, 0, 0, 0 }
2779 static const struct bfd_elf_special_section special_sections_z[] =
2781 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2782 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2783 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2784 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2785 { NULL, 0, 0, 0, 0 }
2788 static const struct bfd_elf_special_section * const special_sections[] =
2790 special_sections_b, /* 'b' */
2791 special_sections_c, /* 'c' */
2792 special_sections_d, /* 'd' */
2794 special_sections_f, /* 'f' */
2795 special_sections_g, /* 'g' */
2796 special_sections_h, /* 'h' */
2797 special_sections_i, /* 'i' */
2800 special_sections_l, /* 'l' */
2802 special_sections_n, /* 'n' */
2804 special_sections_p, /* 'p' */
2806 special_sections_r, /* 'r' */
2807 special_sections_s, /* 's' */
2808 special_sections_t, /* 't' */
2814 special_sections_z /* 'z' */
2817 const struct bfd_elf_special_section *
2818 _bfd_elf_get_special_section (const char *name,
2819 const struct bfd_elf_special_section *spec,
2825 len = strlen (name);
2827 for (i = 0; spec[i].prefix != NULL; i++)
2830 int prefix_len = spec[i].prefix_length;
2832 if (len < prefix_len)
2834 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2837 suffix_len = spec[i].suffix_length;
2838 if (suffix_len <= 0)
2840 if (name[prefix_len] != 0)
2842 if (suffix_len == 0)
2844 if (name[prefix_len] != '.'
2845 && (suffix_len == -2
2846 || (rela && spec[i].type == SHT_REL)))
2852 if (len < prefix_len + suffix_len)
2854 if (memcmp (name + len - suffix_len,
2855 spec[i].prefix + prefix_len,
2865 const struct bfd_elf_special_section *
2866 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2869 const struct bfd_elf_special_section *spec;
2870 const struct elf_backend_data *bed;
2872 /* See if this is one of the special sections. */
2873 if (sec->name == NULL)
2876 bed = get_elf_backend_data (abfd);
2877 spec = bed->special_sections;
2880 spec = _bfd_elf_get_special_section (sec->name,
2881 bed->special_sections,
2887 if (sec->name[0] != '.')
2890 i = sec->name[1] - 'b';
2891 if (i < 0 || i > 'z' - 'b')
2894 spec = special_sections[i];
2899 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2903 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2905 struct bfd_elf_section_data *sdata;
2906 const struct elf_backend_data *bed;
2907 const struct bfd_elf_special_section *ssect;
2909 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2912 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2916 sec->used_by_bfd = sdata;
2919 /* Indicate whether or not this section should use RELA relocations. */
2920 bed = get_elf_backend_data (abfd);
2921 sec->use_rela_p = bed->default_use_rela_p;
2923 /* When we read a file, we don't need to set ELF section type and
2924 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2925 anyway. We will set ELF section type and flags for all linker
2926 created sections. If user specifies BFD section flags, we will
2927 set ELF section type and flags based on BFD section flags in
2928 elf_fake_sections. Special handling for .init_array/.fini_array
2929 output sections since they may contain .ctors/.dtors input
2930 sections. We don't want _bfd_elf_init_private_section_data to
2931 copy ELF section type from .ctors/.dtors input sections. */
2932 if (abfd->direction != read_direction
2933 || (sec->flags & SEC_LINKER_CREATED) != 0)
2935 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2938 || (sec->flags & SEC_LINKER_CREATED) != 0
2939 || ssect->type == SHT_INIT_ARRAY
2940 || ssect->type == SHT_FINI_ARRAY))
2942 elf_section_type (sec) = ssect->type;
2943 elf_section_flags (sec) = ssect->attr;
2947 return _bfd_generic_new_section_hook (abfd, sec);
2950 /* Create a new bfd section from an ELF program header.
2952 Since program segments have no names, we generate a synthetic name
2953 of the form segment<NUM>, where NUM is generally the index in the
2954 program header table. For segments that are split (see below) we
2955 generate the names segment<NUM>a and segment<NUM>b.
2957 Note that some program segments may have a file size that is different than
2958 (less than) the memory size. All this means is that at execution the
2959 system must allocate the amount of memory specified by the memory size,
2960 but only initialize it with the first "file size" bytes read from the
2961 file. This would occur for example, with program segments consisting
2962 of combined data+bss.
2964 To handle the above situation, this routine generates TWO bfd sections
2965 for the single program segment. The first has the length specified by
2966 the file size of the segment, and the second has the length specified
2967 by the difference between the two sizes. In effect, the segment is split
2968 into its initialized and uninitialized parts.
2973 _bfd_elf_make_section_from_phdr (bfd *abfd,
2974 Elf_Internal_Phdr *hdr,
2976 const char *type_name)
2984 split = ((hdr->p_memsz > 0)
2985 && (hdr->p_filesz > 0)
2986 && (hdr->p_memsz > hdr->p_filesz));
2988 if (hdr->p_filesz > 0)
2990 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2991 len = strlen (namebuf) + 1;
2992 name = (char *) bfd_alloc (abfd, len);
2995 memcpy (name, namebuf, len);
2996 newsect = bfd_make_section (abfd, name);
2997 if (newsect == NULL)
2999 newsect->vma = hdr->p_vaddr;
3000 newsect->lma = hdr->p_paddr;
3001 newsect->size = hdr->p_filesz;
3002 newsect->filepos = hdr->p_offset;
3003 newsect->flags |= SEC_HAS_CONTENTS;
3004 newsect->alignment_power = bfd_log2 (hdr->p_align);
3005 if (hdr->p_type == PT_LOAD)
3007 newsect->flags |= SEC_ALLOC;
3008 newsect->flags |= SEC_LOAD;
3009 if (hdr->p_flags & PF_X)
3011 /* FIXME: all we known is that it has execute PERMISSION,
3013 newsect->flags |= SEC_CODE;
3016 if (!(hdr->p_flags & PF_W))
3018 newsect->flags |= SEC_READONLY;
3022 if (hdr->p_memsz > hdr->p_filesz)
3026 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3027 len = strlen (namebuf) + 1;
3028 name = (char *) bfd_alloc (abfd, len);
3031 memcpy (name, namebuf, len);
3032 newsect = bfd_make_section (abfd, name);
3033 if (newsect == NULL)
3035 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
3036 newsect->lma = hdr->p_paddr + hdr->p_filesz;
3037 newsect->size = hdr->p_memsz - hdr->p_filesz;
3038 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3039 align = newsect->vma & -newsect->vma;
3040 if (align == 0 || align > hdr->p_align)
3041 align = hdr->p_align;
3042 newsect->alignment_power = bfd_log2 (align);
3043 if (hdr->p_type == PT_LOAD)
3045 /* Hack for gdb. Segments that have not been modified do
3046 not have their contents written to a core file, on the
3047 assumption that a debugger can find the contents in the
3048 executable. We flag this case by setting the fake
3049 section size to zero. Note that "real" bss sections will
3050 always have their contents dumped to the core file. */
3051 if (bfd_get_format (abfd) == bfd_core)
3053 newsect->flags |= SEC_ALLOC;
3054 if (hdr->p_flags & PF_X)
3055 newsect->flags |= SEC_CODE;
3057 if (!(hdr->p_flags & PF_W))
3058 newsect->flags |= SEC_READONLY;
3065 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3067 const struct elf_backend_data *bed;
3069 switch (hdr->p_type)
3072 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3075 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
3078 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3081 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3084 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3086 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3092 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3095 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3097 case PT_GNU_EH_FRAME:
3098 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3102 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3105 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3108 /* Check for any processor-specific program segment types. */
3109 bed = get_elf_backend_data (abfd);
3110 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3114 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3118 _bfd_elf_single_rel_hdr (asection *sec)
3120 if (elf_section_data (sec)->rel.hdr)
3122 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3123 return elf_section_data (sec)->rel.hdr;
3126 return elf_section_data (sec)->rela.hdr;
3130 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3131 Elf_Internal_Shdr *rel_hdr,
3132 const char *sec_name,
3133 bfd_boolean use_rela_p)
3135 char *name = (char *) bfd_alloc (abfd,
3136 sizeof ".rela" + strlen (sec_name));
3140 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3142 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3144 if (rel_hdr->sh_name == (unsigned int) -1)
3150 /* Allocate and initialize a section-header for a new reloc section,
3151 containing relocations against ASECT. It is stored in RELDATA. If
3152 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3156 _bfd_elf_init_reloc_shdr (bfd *abfd,
3157 struct bfd_elf_section_reloc_data *reldata,
3158 const char *sec_name,
3159 bfd_boolean use_rela_p,
3160 bfd_boolean delay_st_name_p)
3162 Elf_Internal_Shdr *rel_hdr;
3163 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3165 BFD_ASSERT (reldata->hdr == NULL);
3166 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3167 reldata->hdr = rel_hdr;
3169 if (delay_st_name_p)
3170 rel_hdr->sh_name = (unsigned int) -1;
3171 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3174 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3175 rel_hdr->sh_entsize = (use_rela_p
3176 ? bed->s->sizeof_rela
3177 : bed->s->sizeof_rel);
3178 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3179 rel_hdr->sh_flags = 0;
3180 rel_hdr->sh_addr = 0;
3181 rel_hdr->sh_size = 0;
3182 rel_hdr->sh_offset = 0;
3187 /* Return the default section type based on the passed in section flags. */
3190 bfd_elf_get_default_section_type (flagword flags)
3192 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3193 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3195 return SHT_PROGBITS;
3198 struct fake_section_arg
3200 struct bfd_link_info *link_info;
3204 /* Set up an ELF internal section header for a section. */
3207 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3209 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3210 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3211 struct bfd_elf_section_data *esd = elf_section_data (asect);
3212 Elf_Internal_Shdr *this_hdr;
3213 unsigned int sh_type;
3214 const char *name = asect->name;
3215 bfd_boolean delay_st_name_p = FALSE;
3219 /* We already failed; just get out of the bfd_map_over_sections
3224 this_hdr = &esd->this_hdr;
3228 /* ld: compress DWARF debug sections with names: .debug_*. */
3229 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3230 && (asect->flags & SEC_DEBUGGING)
3234 /* Set SEC_ELF_COMPRESS to indicate this section should be
3236 asect->flags |= SEC_ELF_COMPRESS;
3238 /* If this section will be compressed, delay adding section
3239 name to section name section after it is compressed in
3240 _bfd_elf_assign_file_positions_for_non_load. */
3241 delay_st_name_p = TRUE;
3244 else if ((asect->flags & SEC_ELF_RENAME))
3246 /* objcopy: rename output DWARF debug section. */
3247 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3249 /* When we decompress or compress with SHF_COMPRESSED,
3250 convert section name from .zdebug_* to .debug_* if
3254 char *new_name = convert_zdebug_to_debug (abfd, name);
3255 if (new_name == NULL)
3263 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3265 /* PR binutils/18087: Compression does not always make a
3266 section smaller. So only rename the section when
3267 compression has actually taken place. If input section
3268 name is .zdebug_*, we should never compress it again. */
3269 char *new_name = convert_debug_to_zdebug (abfd, name);
3270 if (new_name == NULL)
3275 BFD_ASSERT (name[1] != 'z');
3280 if (delay_st_name_p)
3281 this_hdr->sh_name = (unsigned int) -1;
3285 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3287 if (this_hdr->sh_name == (unsigned int) -1)
3294 /* Don't clear sh_flags. Assembler may set additional bits. */
3296 if ((asect->flags & SEC_ALLOC) != 0
3297 || asect->user_set_vma)
3298 this_hdr->sh_addr = asect->vma;
3300 this_hdr->sh_addr = 0;
3302 this_hdr->sh_offset = 0;
3303 this_hdr->sh_size = asect->size;
3304 this_hdr->sh_link = 0;
3305 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3306 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3309 /* xgettext:c-format */
3310 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3311 abfd, asect->alignment_power, asect);
3315 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
3316 /* The sh_entsize and sh_info fields may have been set already by
3317 copy_private_section_data. */
3319 this_hdr->bfd_section = asect;
3320 this_hdr->contents = NULL;
3322 /* If the section type is unspecified, we set it based on
3324 if ((asect->flags & SEC_GROUP) != 0)
3325 sh_type = SHT_GROUP;
3327 sh_type = bfd_elf_get_default_section_type (asect->flags);
3329 if (this_hdr->sh_type == SHT_NULL)
3330 this_hdr->sh_type = sh_type;
3331 else if (this_hdr->sh_type == SHT_NOBITS
3332 && sh_type == SHT_PROGBITS
3333 && (asect->flags & SEC_ALLOC) != 0)
3335 /* Warn if we are changing a NOBITS section to PROGBITS, but
3336 allow the link to proceed. This can happen when users link
3337 non-bss input sections to bss output sections, or emit data
3338 to a bss output section via a linker script. */
3340 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3341 this_hdr->sh_type = sh_type;
3344 switch (this_hdr->sh_type)
3355 case SHT_INIT_ARRAY:
3356 case SHT_FINI_ARRAY:
3357 case SHT_PREINIT_ARRAY:
3358 this_hdr->sh_entsize = bed->s->arch_size / 8;
3362 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3366 this_hdr->sh_entsize = bed->s->sizeof_sym;
3370 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3374 if (get_elf_backend_data (abfd)->may_use_rela_p)
3375 this_hdr->sh_entsize = bed->s->sizeof_rela;
3379 if (get_elf_backend_data (abfd)->may_use_rel_p)
3380 this_hdr->sh_entsize = bed->s->sizeof_rel;
3383 case SHT_GNU_versym:
3384 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3387 case SHT_GNU_verdef:
3388 this_hdr->sh_entsize = 0;
3389 /* objcopy or strip will copy over sh_info, but may not set
3390 cverdefs. The linker will set cverdefs, but sh_info will be
3392 if (this_hdr->sh_info == 0)
3393 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3395 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3396 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3399 case SHT_GNU_verneed:
3400 this_hdr->sh_entsize = 0;
3401 /* objcopy or strip will copy over sh_info, but may not set
3402 cverrefs. The linker will set cverrefs, but sh_info will be
3404 if (this_hdr->sh_info == 0)
3405 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3407 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3408 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3412 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3416 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3420 if ((asect->flags & SEC_ALLOC) != 0)
3421 this_hdr->sh_flags |= SHF_ALLOC;
3422 if ((asect->flags & SEC_READONLY) == 0)
3423 this_hdr->sh_flags |= SHF_WRITE;
3424 if ((asect->flags & SEC_CODE) != 0)
3425 this_hdr->sh_flags |= SHF_EXECINSTR;
3426 if ((asect->flags & SEC_MERGE) != 0)
3428 this_hdr->sh_flags |= SHF_MERGE;
3429 this_hdr->sh_entsize = asect->entsize;
3431 if ((asect->flags & SEC_STRINGS) != 0)
3432 this_hdr->sh_flags |= SHF_STRINGS;
3433 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3434 this_hdr->sh_flags |= SHF_GROUP;
3435 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3437 this_hdr->sh_flags |= SHF_TLS;
3438 if (asect->size == 0
3439 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3441 struct bfd_link_order *o = asect->map_tail.link_order;
3443 this_hdr->sh_size = 0;
3446 this_hdr->sh_size = o->offset + o->size;
3447 if (this_hdr->sh_size != 0)
3448 this_hdr->sh_type = SHT_NOBITS;
3452 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3453 this_hdr->sh_flags |= SHF_EXCLUDE;
3455 /* If the section has relocs, set up a section header for the
3456 SHT_REL[A] section. If two relocation sections are required for
3457 this section, it is up to the processor-specific back-end to
3458 create the other. */
3459 if ((asect->flags & SEC_RELOC) != 0)
3461 /* When doing a relocatable link, create both REL and RELA sections if
3464 /* Do the normal setup if we wouldn't create any sections here. */
3465 && esd->rel.count + esd->rela.count > 0
3466 && (bfd_link_relocatable (arg->link_info)
3467 || arg->link_info->emitrelocations))
3469 if (esd->rel.count && esd->rel.hdr == NULL
3470 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3471 FALSE, delay_st_name_p))
3476 if (esd->rela.count && esd->rela.hdr == NULL
3477 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3478 TRUE, delay_st_name_p))
3484 else if (!_bfd_elf_init_reloc_shdr (abfd,
3486 ? &esd->rela : &esd->rel),
3496 /* Check for processor-specific section types. */
3497 sh_type = this_hdr->sh_type;
3498 if (bed->elf_backend_fake_sections
3499 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3505 if (sh_type == SHT_NOBITS && asect->size != 0)
3507 /* Don't change the header type from NOBITS if we are being
3508 called for objcopy --only-keep-debug. */
3509 this_hdr->sh_type = sh_type;
3513 /* Fill in the contents of a SHT_GROUP section. Called from
3514 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3515 when ELF targets use the generic linker, ld. Called for ld -r
3516 from bfd_elf_final_link. */
3519 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3521 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3522 asection *elt, *first;
3526 /* Ignore linker created group section. See elfNN_ia64_object_p in
3528 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3533 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3535 unsigned long symindx = 0;
3537 /* elf_group_id will have been set up by objcopy and the
3539 if (elf_group_id (sec) != NULL)
3540 symindx = elf_group_id (sec)->udata.i;
3544 /* If called from the assembler, swap_out_syms will have set up
3545 elf_section_syms. */
3546 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3547 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3549 elf_section_data (sec)->this_hdr.sh_info = symindx;
3551 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3553 /* The ELF backend linker sets sh_info to -2 when the group
3554 signature symbol is global, and thus the index can't be
3555 set until all local symbols are output. */
3557 struct bfd_elf_section_data *sec_data;
3558 unsigned long symndx;
3559 unsigned long extsymoff;
3560 struct elf_link_hash_entry *h;
3562 /* The point of this little dance to the first SHF_GROUP section
3563 then back to the SHT_GROUP section is that this gets us to
3564 the SHT_GROUP in the input object. */
3565 igroup = elf_sec_group (elf_next_in_group (sec));
3566 sec_data = elf_section_data (igroup);
3567 symndx = sec_data->this_hdr.sh_info;
3569 if (!elf_bad_symtab (igroup->owner))
3571 Elf_Internal_Shdr *symtab_hdr;
3573 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3574 extsymoff = symtab_hdr->sh_info;
3576 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3577 while (h->root.type == bfd_link_hash_indirect
3578 || h->root.type == bfd_link_hash_warning)
3579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3581 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3584 /* The contents won't be allocated for "ld -r" or objcopy. */
3586 if (sec->contents == NULL)
3589 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3591 /* Arrange for the section to be written out. */
3592 elf_section_data (sec)->this_hdr.contents = sec->contents;
3593 if (sec->contents == NULL)
3600 loc = sec->contents + sec->size;
3602 /* Get the pointer to the first section in the group that gas
3603 squirreled away here. objcopy arranges for this to be set to the
3604 start of the input section group. */
3605 first = elt = elf_next_in_group (sec);
3607 /* First element is a flag word. Rest of section is elf section
3608 indices for all the sections of the group. Write them backwards
3609 just to keep the group in the same order as given in .section
3610 directives, not that it matters. */
3617 s = s->output_section;
3619 && !bfd_is_abs_section (s))
3621 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3622 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3624 if (elf_sec->rel.hdr != NULL
3626 || (input_elf_sec->rel.hdr != NULL
3627 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3629 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3631 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3633 if (elf_sec->rela.hdr != NULL
3635 || (input_elf_sec->rela.hdr != NULL
3636 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3638 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3640 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3643 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3645 elt = elf_next_in_group (elt);
3651 BFD_ASSERT (loc == sec->contents);
3653 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3656 /* Given NAME, the name of a relocation section stripped of its
3657 .rel/.rela prefix, return the section in ABFD to which the
3658 relocations apply. */
3661 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3663 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3664 section likely apply to .got.plt or .got section. */
3665 if (get_elf_backend_data (abfd)->want_got_plt
3666 && strcmp (name, ".plt") == 0)
3671 sec = bfd_get_section_by_name (abfd, name);
3677 return bfd_get_section_by_name (abfd, name);
3680 /* Return the section to which RELOC_SEC applies. */
3683 elf_get_reloc_section (asection *reloc_sec)
3688 const struct elf_backend_data *bed;
3690 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3691 if (type != SHT_REL && type != SHT_RELA)
3694 /* We look up the section the relocs apply to by name. */
3695 name = reloc_sec->name;
3696 if (strncmp (name, ".rel", 4) != 0)
3699 if (type == SHT_RELA && *name++ != 'a')
3702 abfd = reloc_sec->owner;
3703 bed = get_elf_backend_data (abfd);
3704 return bed->get_reloc_section (abfd, name);
3707 /* Assign all ELF section numbers. The dummy first section is handled here
3708 too. The link/info pointers for the standard section types are filled
3709 in here too, while we're at it. */
3712 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3714 struct elf_obj_tdata *t = elf_tdata (abfd);
3716 unsigned int section_number;
3717 Elf_Internal_Shdr **i_shdrp;
3718 struct bfd_elf_section_data *d;
3719 bfd_boolean need_symtab;
3723 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3725 /* SHT_GROUP sections are in relocatable files only. */
3726 if (link_info == NULL || !link_info->resolve_section_groups)
3728 size_t reloc_count = 0;
3730 /* Put SHT_GROUP sections first. */
3731 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3733 d = elf_section_data (sec);
3735 if (d->this_hdr.sh_type == SHT_GROUP)
3737 if (sec->flags & SEC_LINKER_CREATED)
3739 /* Remove the linker created SHT_GROUP sections. */
3740 bfd_section_list_remove (abfd, sec);
3741 abfd->section_count--;
3744 d->this_idx = section_number++;
3747 /* Count relocations. */
3748 reloc_count += sec->reloc_count;
3751 /* Clear HAS_RELOC if there are no relocations. */
3752 if (reloc_count == 0)
3753 abfd->flags &= ~HAS_RELOC;
3756 for (sec = abfd->sections; sec; sec = sec->next)
3758 d = elf_section_data (sec);
3760 if (d->this_hdr.sh_type != SHT_GROUP)
3761 d->this_idx = section_number++;
3762 if (d->this_hdr.sh_name != (unsigned int) -1)
3763 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3766 d->rel.idx = section_number++;
3767 if (d->rel.hdr->sh_name != (unsigned int) -1)
3768 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3775 d->rela.idx = section_number++;
3776 if (d->rela.hdr->sh_name != (unsigned int) -1)
3777 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3783 need_symtab = (bfd_get_symcount (abfd) > 0
3784 || (link_info == NULL
3785 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3789 elf_onesymtab (abfd) = section_number++;
3790 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3791 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3793 elf_section_list *entry;
3795 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3797 entry = bfd_zalloc (abfd, sizeof (*entry));
3798 entry->ndx = section_number++;
3799 elf_symtab_shndx_list (abfd) = entry;
3801 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3802 ".symtab_shndx", FALSE);
3803 if (entry->hdr.sh_name == (unsigned int) -1)
3806 elf_strtab_sec (abfd) = section_number++;
3807 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3810 elf_shstrtab_sec (abfd) = section_number++;
3811 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3812 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3814 if (section_number >= SHN_LORESERVE)
3816 /* xgettext:c-format */
3817 _bfd_error_handler (_("%pB: too many sections: %u"),
3818 abfd, section_number);
3822 elf_numsections (abfd) = section_number;
3823 elf_elfheader (abfd)->e_shnum = section_number;
3825 /* Set up the list of section header pointers, in agreement with the
3827 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3828 sizeof (Elf_Internal_Shdr *));
3829 if (i_shdrp == NULL)
3832 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3833 sizeof (Elf_Internal_Shdr));
3834 if (i_shdrp[0] == NULL)
3836 bfd_release (abfd, i_shdrp);
3840 elf_elfsections (abfd) = i_shdrp;
3842 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3845 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3846 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3848 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3849 BFD_ASSERT (entry != NULL);
3850 i_shdrp[entry->ndx] = & entry->hdr;
3851 entry->hdr.sh_link = elf_onesymtab (abfd);
3853 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3854 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3857 for (sec = abfd->sections; sec; sec = sec->next)
3861 d = elf_section_data (sec);
3863 i_shdrp[d->this_idx] = &d->this_hdr;
3864 if (d->rel.idx != 0)
3865 i_shdrp[d->rel.idx] = d->rel.hdr;
3866 if (d->rela.idx != 0)
3867 i_shdrp[d->rela.idx] = d->rela.hdr;
3869 /* Fill in the sh_link and sh_info fields while we're at it. */
3871 /* sh_link of a reloc section is the section index of the symbol
3872 table. sh_info is the section index of the section to which
3873 the relocation entries apply. */
3874 if (d->rel.idx != 0)
3876 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3877 d->rel.hdr->sh_info = d->this_idx;
3878 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3880 if (d->rela.idx != 0)
3882 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3883 d->rela.hdr->sh_info = d->this_idx;
3884 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3887 /* We need to set up sh_link for SHF_LINK_ORDER. */
3888 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3890 s = elf_linked_to_section (sec);
3893 /* elf_linked_to_section points to the input section. */
3894 if (link_info != NULL)
3896 /* Check discarded linkonce section. */
3897 if (discarded_section (s))
3901 /* xgettext:c-format */
3902 (_("%pB: sh_link of section `%pA' points to"
3903 " discarded section `%pA' of `%pB'"),
3904 abfd, d->this_hdr.bfd_section,
3906 /* Point to the kept section if it has the same
3907 size as the discarded one. */
3908 kept = _bfd_elf_check_kept_section (s, link_info);
3911 bfd_set_error (bfd_error_bad_value);
3917 s = s->output_section;
3918 BFD_ASSERT (s != NULL);
3922 /* Handle objcopy. */
3923 if (s->output_section == NULL)
3926 /* xgettext:c-format */
3927 (_("%pB: sh_link of section `%pA' points to"
3928 " removed section `%pA' of `%pB'"),
3929 abfd, d->this_hdr.bfd_section, s, s->owner);
3930 bfd_set_error (bfd_error_bad_value);
3933 s = s->output_section;
3935 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3940 The Intel C compiler generates SHT_IA_64_UNWIND with
3941 SHF_LINK_ORDER. But it doesn't set the sh_link or
3942 sh_info fields. Hence we could get the situation
3944 const struct elf_backend_data *bed
3945 = get_elf_backend_data (abfd);
3946 if (bed->link_order_error_handler)
3947 bed->link_order_error_handler
3948 /* xgettext:c-format */
3949 (_("%pB: warning: sh_link not set for section `%pA'"),
3954 switch (d->this_hdr.sh_type)
3958 /* A reloc section which we are treating as a normal BFD
3959 section. sh_link is the section index of the symbol
3960 table. sh_info is the section index of the section to
3961 which the relocation entries apply. We assume that an
3962 allocated reloc section uses the dynamic symbol table.
3963 FIXME: How can we be sure? */
3964 s = bfd_get_section_by_name (abfd, ".dynsym");
3966 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3968 s = elf_get_reloc_section (sec);
3971 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3972 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3977 /* We assume that a section named .stab*str is a stabs
3978 string section. We look for a section with the same name
3979 but without the trailing ``str'', and set its sh_link
3980 field to point to this section. */
3981 if (CONST_STRNEQ (sec->name, ".stab")
3982 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3987 len = strlen (sec->name);
3988 alc = (char *) bfd_malloc (len - 2);
3991 memcpy (alc, sec->name, len - 3);
3992 alc[len - 3] = '\0';
3993 s = bfd_get_section_by_name (abfd, alc);
3997 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3999 /* This is a .stab section. */
4000 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
4001 elf_section_data (s)->this_hdr.sh_entsize
4002 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
4009 case SHT_GNU_verneed:
4010 case SHT_GNU_verdef:
4011 /* sh_link is the section header index of the string table
4012 used for the dynamic entries, or the symbol table, or the
4014 s = bfd_get_section_by_name (abfd, ".dynstr");
4016 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4019 case SHT_GNU_LIBLIST:
4020 /* sh_link is the section header index of the prelink library
4021 list used for the dynamic entries, or the symbol table, or
4022 the version strings. */
4023 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4024 ? ".dynstr" : ".gnu.libstr");
4026 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4031 case SHT_GNU_versym:
4032 /* sh_link is the section header index of the symbol table
4033 this hash table or version table is for. */
4034 s = bfd_get_section_by_name (abfd, ".dynsym");
4036 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4040 d->this_hdr.sh_link = elf_onesymtab (abfd);
4044 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4045 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4046 debug section name from .debug_* to .zdebug_* if needed. */
4052 sym_is_global (bfd *abfd, asymbol *sym)
4054 /* If the backend has a special mapping, use it. */
4055 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4056 if (bed->elf_backend_sym_is_global)
4057 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4059 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4060 || bfd_is_und_section (bfd_asymbol_section (sym))
4061 || bfd_is_com_section (bfd_asymbol_section (sym)));
4064 /* Filter global symbols of ABFD to include in the import library. All
4065 SYMCOUNT symbols of ABFD can be examined from their pointers in
4066 SYMS. Pointers of symbols to keep should be stored contiguously at
4067 the beginning of that array.
4069 Returns the number of symbols to keep. */
4072 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4073 asymbol **syms, long symcount)
4075 long src_count, dst_count = 0;
4077 for (src_count = 0; src_count < symcount; src_count++)
4079 asymbol *sym = syms[src_count];
4080 char *name = (char *) bfd_asymbol_name (sym);
4081 struct bfd_link_hash_entry *h;
4083 if (!sym_is_global (abfd, sym))
4086 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
4089 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4091 if (h->linker_def || h->ldscript_def)
4094 syms[dst_count++] = sym;
4097 syms[dst_count] = NULL;
4102 /* Don't output section symbols for sections that are not going to be
4103 output, that are duplicates or there is no BFD section. */
4106 ignore_section_sym (bfd *abfd, asymbol *sym)
4108 elf_symbol_type *type_ptr;
4113 if ((sym->flags & BSF_SECTION_SYM) == 0)
4116 if (sym->section == NULL)
4119 type_ptr = elf_symbol_from (abfd, sym);
4120 return ((type_ptr != NULL
4121 && type_ptr->internal_elf_sym.st_shndx != 0
4122 && bfd_is_abs_section (sym->section))
4123 || !(sym->section->owner == abfd
4124 || (sym->section->output_section != NULL
4125 && sym->section->output_section->owner == abfd
4126 && sym->section->output_offset == 0)
4127 || bfd_is_abs_section (sym->section)));
4130 /* Map symbol from it's internal number to the external number, moving
4131 all local symbols to be at the head of the list. */
4134 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4136 unsigned int symcount = bfd_get_symcount (abfd);
4137 asymbol **syms = bfd_get_outsymbols (abfd);
4138 asymbol **sect_syms;
4139 unsigned int num_locals = 0;
4140 unsigned int num_globals = 0;
4141 unsigned int num_locals2 = 0;
4142 unsigned int num_globals2 = 0;
4143 unsigned int max_index = 0;
4149 fprintf (stderr, "elf_map_symbols\n");
4153 for (asect = abfd->sections; asect; asect = asect->next)
4155 if (max_index < asect->index)
4156 max_index = asect->index;
4160 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
4161 if (sect_syms == NULL)
4163 elf_section_syms (abfd) = sect_syms;
4164 elf_num_section_syms (abfd) = max_index;
4166 /* Init sect_syms entries for any section symbols we have already
4167 decided to output. */
4168 for (idx = 0; idx < symcount; idx++)
4170 asymbol *sym = syms[idx];
4172 if ((sym->flags & BSF_SECTION_SYM) != 0
4174 && !ignore_section_sym (abfd, sym)
4175 && !bfd_is_abs_section (sym->section))
4177 asection *sec = sym->section;
4179 if (sec->owner != abfd)
4180 sec = sec->output_section;
4182 sect_syms[sec->index] = syms[idx];
4186 /* Classify all of the symbols. */
4187 for (idx = 0; idx < symcount; idx++)
4189 if (sym_is_global (abfd, syms[idx]))
4191 else if (!ignore_section_sym (abfd, syms[idx]))
4195 /* We will be adding a section symbol for each normal BFD section. Most
4196 sections will already have a section symbol in outsymbols, but
4197 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4198 at least in that case. */
4199 for (asect = abfd->sections; asect; asect = asect->next)
4201 if (sect_syms[asect->index] == NULL)
4203 if (!sym_is_global (abfd, asect->symbol))
4210 /* Now sort the symbols so the local symbols are first. */
4211 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
4212 sizeof (asymbol *));
4214 if (new_syms == NULL)
4217 for (idx = 0; idx < symcount; idx++)
4219 asymbol *sym = syms[idx];
4222 if (sym_is_global (abfd, sym))
4223 i = num_locals + num_globals2++;
4224 else if (!ignore_section_sym (abfd, sym))
4229 sym->udata.i = i + 1;
4231 for (asect = abfd->sections; asect; asect = asect->next)
4233 if (sect_syms[asect->index] == NULL)
4235 asymbol *sym = asect->symbol;
4238 sect_syms[asect->index] = sym;
4239 if (!sym_is_global (abfd, sym))
4242 i = num_locals + num_globals2++;
4244 sym->udata.i = i + 1;
4248 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4250 *pnum_locals = num_locals;
4254 /* Align to the maximum file alignment that could be required for any
4255 ELF data structure. */
4257 static inline file_ptr
4258 align_file_position (file_ptr off, int align)
4260 return (off + align - 1) & ~(align - 1);
4263 /* Assign a file position to a section, optionally aligning to the
4264 required section alignment. */
4267 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4271 if (align && i_shdrp->sh_addralign > 1)
4272 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4273 i_shdrp->sh_offset = offset;
4274 if (i_shdrp->bfd_section != NULL)
4275 i_shdrp->bfd_section->filepos = offset;
4276 if (i_shdrp->sh_type != SHT_NOBITS)
4277 offset += i_shdrp->sh_size;
4281 /* Compute the file positions we are going to put the sections at, and
4282 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4283 is not NULL, this is being called by the ELF backend linker. */
4286 _bfd_elf_compute_section_file_positions (bfd *abfd,
4287 struct bfd_link_info *link_info)
4289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4290 struct fake_section_arg fsargs;
4292 struct elf_strtab_hash *strtab = NULL;
4293 Elf_Internal_Shdr *shstrtab_hdr;
4294 bfd_boolean need_symtab;
4296 if (abfd->output_has_begun)
4299 /* Do any elf backend specific processing first. */
4300 if (bed->elf_backend_begin_write_processing)
4301 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4303 if (! prep_headers (abfd))
4306 /* Post process the headers if necessary. */
4307 (*bed->elf_backend_post_process_headers) (abfd, link_info);
4309 fsargs.failed = FALSE;
4310 fsargs.link_info = link_info;
4311 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4315 if (!assign_section_numbers (abfd, link_info))
4318 /* The backend linker builds symbol table information itself. */
4319 need_symtab = (link_info == NULL
4320 && (bfd_get_symcount (abfd) > 0
4321 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4325 /* Non-zero if doing a relocatable link. */
4326 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4328 if (! swap_out_syms (abfd, &strtab, relocatable_p))
4333 if (link_info == NULL)
4335 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4340 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4341 /* sh_name was set in prep_headers. */
4342 shstrtab_hdr->sh_type = SHT_STRTAB;
4343 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4344 shstrtab_hdr->sh_addr = 0;
4345 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4346 shstrtab_hdr->sh_entsize = 0;
4347 shstrtab_hdr->sh_link = 0;
4348 shstrtab_hdr->sh_info = 0;
4349 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4350 shstrtab_hdr->sh_addralign = 1;
4352 if (!assign_file_positions_except_relocs (abfd, link_info))
4358 Elf_Internal_Shdr *hdr;
4360 off = elf_next_file_pos (abfd);
4362 hdr = & elf_symtab_hdr (abfd);
4363 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4365 if (elf_symtab_shndx_list (abfd) != NULL)
4367 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4368 if (hdr->sh_size != 0)
4369 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4370 /* FIXME: What about other symtab_shndx sections in the list ? */
4373 hdr = &elf_tdata (abfd)->strtab_hdr;
4374 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4376 elf_next_file_pos (abfd) = off;
4378 /* Now that we know where the .strtab section goes, write it
4380 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4381 || ! _bfd_elf_strtab_emit (abfd, strtab))
4383 _bfd_elf_strtab_free (strtab);
4386 abfd->output_has_begun = TRUE;
4391 /* Make an initial estimate of the size of the program header. If we
4392 get the number wrong here, we'll redo section placement. */
4394 static bfd_size_type
4395 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4399 const struct elf_backend_data *bed;
4401 /* Assume we will need exactly two PT_LOAD segments: one for text
4402 and one for data. */
4405 s = bfd_get_section_by_name (abfd, ".interp");
4406 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4408 /* If we have a loadable interpreter section, we need a
4409 PT_INTERP segment. In this case, assume we also need a
4410 PT_PHDR segment, although that may not be true for all
4415 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4417 /* We need a PT_DYNAMIC segment. */
4421 if (info != NULL && info->relro)
4423 /* We need a PT_GNU_RELRO segment. */
4427 if (elf_eh_frame_hdr (abfd))
4429 /* We need a PT_GNU_EH_FRAME segment. */
4433 if (elf_stack_flags (abfd))
4435 /* We need a PT_GNU_STACK segment. */
4439 s = bfd_get_section_by_name (abfd,
4440 NOTE_GNU_PROPERTY_SECTION_NAME);
4441 if (s != NULL && s->size != 0)
4443 /* We need a PT_GNU_PROPERTY segment. */
4447 for (s = abfd->sections; s != NULL; s = s->next)
4449 if ((s->flags & SEC_LOAD) != 0
4450 && elf_section_type (s) == SHT_NOTE)
4452 unsigned int alignment_power;
4453 /* We need a PT_NOTE segment. */
4455 /* Try to create just one PT_NOTE segment for all adjacent
4456 loadable SHT_NOTE sections. gABI requires that within a
4457 PT_NOTE segment (and also inside of each SHT_NOTE section)
4458 each note should have the same alignment. So we check
4459 whether the sections are correctly aligned. */
4460 alignment_power = s->alignment_power;
4461 while (s->next != NULL
4462 && s->next->alignment_power == alignment_power
4463 && (s->next->flags & SEC_LOAD) != 0
4464 && elf_section_type (s->next) == SHT_NOTE)
4469 for (s = abfd->sections; s != NULL; s = s->next)
4471 if (s->flags & SEC_THREAD_LOCAL)
4473 /* We need a PT_TLS segment. */
4479 bed = get_elf_backend_data (abfd);
4481 if ((abfd->flags & D_PAGED) != 0
4482 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4484 /* Add a PT_GNU_MBIND segment for each mbind section. */
4485 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4486 for (s = abfd->sections; s != NULL; s = s->next)
4487 if (elf_section_flags (s) & SHF_GNU_MBIND)
4489 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4492 /* xgettext:c-format */
4493 (_("%pB: GNU_MBIND section `%pA' has invalid "
4494 "sh_info field: %d"),
4495 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4498 /* Align mbind section to page size. */
4499 if (s->alignment_power < page_align_power)
4500 s->alignment_power = page_align_power;
4505 /* Let the backend count up any program headers it might need. */
4506 if (bed->elf_backend_additional_program_headers)
4510 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4516 return segs * bed->s->sizeof_phdr;
4519 /* Find the segment that contains the output_section of section. */
4522 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4524 struct elf_segment_map *m;
4525 Elf_Internal_Phdr *p;
4527 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4533 for (i = m->count - 1; i >= 0; i--)
4534 if (m->sections[i] == section)
4541 /* Create a mapping from a set of sections to a program segment. */
4543 static struct elf_segment_map *
4544 make_mapping (bfd *abfd,
4545 asection **sections,
4550 struct elf_segment_map *m;
4555 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4556 amt += (to - from) * sizeof (asection *);
4557 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4561 m->p_type = PT_LOAD;
4562 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4563 m->sections[i - from] = *hdrpp;
4564 m->count = to - from;
4566 if (from == 0 && phdr)
4568 /* Include the headers in the first PT_LOAD segment. */
4569 m->includes_filehdr = 1;
4570 m->includes_phdrs = 1;
4576 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4579 struct elf_segment_map *
4580 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4582 struct elf_segment_map *m;
4584 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4585 sizeof (struct elf_segment_map));
4589 m->p_type = PT_DYNAMIC;
4591 m->sections[0] = dynsec;
4596 /* Possibly add or remove segments from the segment map. */
4599 elf_modify_segment_map (bfd *abfd,
4600 struct bfd_link_info *info,
4601 bfd_boolean remove_empty_load)
4603 struct elf_segment_map **m;
4604 const struct elf_backend_data *bed;
4606 /* The placement algorithm assumes that non allocated sections are
4607 not in PT_LOAD segments. We ensure this here by removing such
4608 sections from the segment map. We also remove excluded
4609 sections. Finally, any PT_LOAD segment without sections is
4611 m = &elf_seg_map (abfd);
4614 unsigned int i, new_count;
4616 for (new_count = 0, i = 0; i < (*m)->count; i++)
4618 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4619 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4620 || (*m)->p_type != PT_LOAD))
4622 (*m)->sections[new_count] = (*m)->sections[i];
4626 (*m)->count = new_count;
4628 if (remove_empty_load
4629 && (*m)->p_type == PT_LOAD
4631 && !(*m)->includes_phdrs)
4637 bed = get_elf_backend_data (abfd);
4638 if (bed->elf_backend_modify_segment_map != NULL)
4640 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4647 #define IS_TBSS(s) \
4648 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4650 /* Set up a mapping from BFD sections to program segments. */
4653 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4656 struct elf_segment_map *m;
4657 asection **sections = NULL;
4658 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4659 bfd_boolean no_user_phdrs;
4661 no_user_phdrs = elf_seg_map (abfd) == NULL;
4664 info->user_phdrs = !no_user_phdrs;
4666 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4670 struct elf_segment_map *mfirst;
4671 struct elf_segment_map **pm;
4674 unsigned int hdr_index;
4675 bfd_vma maxpagesize;
4677 bfd_boolean phdr_in_segment;
4678 bfd_boolean writable;
4679 bfd_boolean executable;
4681 asection *first_tls = NULL;
4682 asection *first_mbind = NULL;
4683 asection *dynsec, *eh_frame_hdr;
4685 bfd_vma addr_mask, wrap_to = 0;
4686 bfd_size_type phdr_size;
4688 /* Select the allocated sections, and sort them. */
4690 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4691 sizeof (asection *));
4692 if (sections == NULL)
4695 /* Calculate top address, avoiding undefined behaviour of shift
4696 left operator when shift count is equal to size of type
4698 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4699 addr_mask = (addr_mask << 1) + 1;
4702 for (s = abfd->sections; s != NULL; s = s->next)
4704 if ((s->flags & SEC_ALLOC) != 0)
4708 /* A wrapping section potentially clashes with header. */
4709 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4710 wrap_to = (s->lma + s->size) & addr_mask;
4713 BFD_ASSERT (i <= bfd_count_sections (abfd));
4716 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4718 phdr_size = elf_program_header_size (abfd);
4719 if (phdr_size == (bfd_size_type) -1)
4720 phdr_size = get_program_header_size (abfd, info);
4721 phdr_size += bed->s->sizeof_ehdr;
4722 maxpagesize = bed->maxpagesize;
4723 if (maxpagesize == 0)
4725 phdr_in_segment = info != NULL && info->load_phdrs;
4727 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4728 >= (phdr_size & (maxpagesize - 1))))
4729 /* For compatibility with old scripts that may not be using
4730 SIZEOF_HEADERS, add headers when it looks like space has
4731 been left for them. */
4732 phdr_in_segment = TRUE;
4734 /* Build the mapping. */
4738 /* If we have a .interp section, then create a PT_PHDR segment for
4739 the program headers and a PT_INTERP segment for the .interp
4741 s = bfd_get_section_by_name (abfd, ".interp");
4742 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4744 amt = sizeof (struct elf_segment_map);
4745 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4749 m->p_type = PT_PHDR;
4751 m->p_flags_valid = 1;
4752 m->includes_phdrs = 1;
4753 phdr_in_segment = TRUE;
4757 amt = sizeof (struct elf_segment_map);
4758 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4762 m->p_type = PT_INTERP;
4770 /* Look through the sections. We put sections in the same program
4771 segment when the start of the second section can be placed within
4772 a few bytes of the end of the first section. */
4778 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4780 && (dynsec->flags & SEC_LOAD) == 0)
4783 if ((abfd->flags & D_PAGED) == 0)
4784 phdr_in_segment = FALSE;
4786 /* Deal with -Ttext or something similar such that the first section
4787 is not adjacent to the program headers. This is an
4788 approximation, since at this point we don't know exactly how many
4789 program headers we will need. */
4790 if (phdr_in_segment && count > 0)
4793 bfd_boolean separate_phdr = FALSE;
4795 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4797 && info->separate_code
4798 && (sections[0]->flags & SEC_CODE) != 0)
4800 /* If data sections should be separate from code and
4801 thus not executable, and the first section is
4802 executable then put the file and program headers in
4803 their own PT_LOAD. */
4804 separate_phdr = TRUE;
4805 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4806 == (sections[0]->lma & addr_mask & -maxpagesize)))
4808 /* The file and program headers are currently on the
4809 same page as the first section. Put them on the
4810 previous page if we can. */
4811 if (phdr_lma >= maxpagesize)
4812 phdr_lma -= maxpagesize;
4814 separate_phdr = FALSE;
4817 if ((sections[0]->lma & addr_mask) < phdr_lma
4818 || (sections[0]->lma & addr_mask) < phdr_size)
4819 /* If file and program headers would be placed at the end
4820 of memory then it's probably better to omit them. */
4821 phdr_in_segment = FALSE;
4822 else if (phdr_lma < wrap_to)
4823 /* If a section wraps around to where we'll be placing
4824 file and program headers, then the headers will be
4826 phdr_in_segment = FALSE;
4827 else if (separate_phdr)
4829 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4832 m->p_paddr = phdr_lma;
4834 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4835 m->p_paddr_valid = 1;
4838 phdr_in_segment = FALSE;
4842 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4845 bfd_boolean new_segment;
4849 /* See if this section and the last one will fit in the same
4852 if (last_hdr == NULL)
4854 /* If we don't have a segment yet, then we don't need a new
4855 one (we build the last one after this loop). */
4856 new_segment = FALSE;
4858 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4860 /* If this section has a different relation between the
4861 virtual address and the load address, then we need a new
4865 else if (hdr->lma < last_hdr->lma + last_size
4866 || last_hdr->lma + last_size < last_hdr->lma)
4868 /* If this section has a load address that makes it overlap
4869 the previous section, then we need a new segment. */
4872 else if ((abfd->flags & D_PAGED) != 0
4873 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4874 == (hdr->lma & -maxpagesize)))
4876 /* If we are demand paged then we can't map two disk
4877 pages onto the same memory page. */
4878 new_segment = FALSE;
4880 /* In the next test we have to be careful when last_hdr->lma is close
4881 to the end of the address space. If the aligned address wraps
4882 around to the start of the address space, then there are no more
4883 pages left in memory and it is OK to assume that the current
4884 section can be included in the current segment. */
4885 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4886 + maxpagesize > last_hdr->lma)
4887 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4888 + maxpagesize <= hdr->lma))
4890 /* If putting this section in this segment would force us to
4891 skip a page in the segment, then we need a new segment. */
4894 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4895 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4897 /* We don't want to put a loaded section after a
4898 nonloaded (ie. bss style) section in the same segment
4899 as that will force the non-loaded section to be loaded.
4900 Consider .tbss sections as loaded for this purpose. */
4903 else if ((abfd->flags & D_PAGED) == 0)
4905 /* If the file is not demand paged, which means that we
4906 don't require the sections to be correctly aligned in the
4907 file, then there is no other reason for a new segment. */
4908 new_segment = FALSE;
4910 else if (info != NULL
4911 && info->separate_code
4912 && executable != ((hdr->flags & SEC_CODE) != 0))
4917 && (hdr->flags & SEC_READONLY) == 0)
4919 /* We don't want to put a writable section in a read only
4925 /* Otherwise, we can use the same segment. */
4926 new_segment = FALSE;
4929 /* Allow interested parties a chance to override our decision. */
4930 if (last_hdr != NULL
4932 && info->callbacks->override_segment_assignment != NULL)
4934 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4940 if ((hdr->flags & SEC_READONLY) == 0)
4942 if ((hdr->flags & SEC_CODE) != 0)
4945 /* .tbss sections effectively have zero size. */
4946 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4950 /* We need a new program segment. We must create a new program
4951 header holding all the sections from hdr_index until hdr. */
4953 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4960 if ((hdr->flags & SEC_READONLY) == 0)
4965 if ((hdr->flags & SEC_CODE) == 0)
4971 /* .tbss sections effectively have zero size. */
4972 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4974 phdr_in_segment = FALSE;
4977 /* Create a final PT_LOAD program segment, but not if it's just
4979 if (last_hdr != NULL
4980 && (i - hdr_index != 1
4981 || !IS_TBSS (last_hdr)))
4983 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4991 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4994 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5001 /* For each batch of consecutive loadable SHT_NOTE sections,
5002 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5003 because if we link together nonloadable .note sections and
5004 loadable .note sections, we will generate two .note sections
5005 in the output file. */
5006 for (s = abfd->sections; s != NULL; s = s->next)
5008 if ((s->flags & SEC_LOAD) != 0
5009 && elf_section_type (s) == SHT_NOTE)
5012 unsigned int alignment_power = s->alignment_power;
5015 for (s2 = s; s2->next != NULL; s2 = s2->next)
5017 if (s2->next->alignment_power == alignment_power
5018 && (s2->next->flags & SEC_LOAD) != 0
5019 && elf_section_type (s2->next) == SHT_NOTE
5020 && align_power (s2->lma + s2->size,
5027 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5028 amt += count * sizeof (asection *);
5029 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5033 m->p_type = PT_NOTE;
5037 m->sections[m->count - count--] = s;
5038 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5041 m->sections[m->count - 1] = s;
5042 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5046 if (s->flags & SEC_THREAD_LOCAL)
5052 if (first_mbind == NULL
5053 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5057 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5060 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5061 amt += tls_count * sizeof (asection *);
5062 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5067 m->count = tls_count;
5068 /* Mandated PF_R. */
5070 m->p_flags_valid = 1;
5072 for (i = 0; i < (unsigned int) tls_count; ++i)
5074 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5077 (_("%pB: TLS sections are not adjacent:"), abfd);
5080 while (i < (unsigned int) tls_count)
5082 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5084 _bfd_error_handler (_(" TLS: %pA"), s);
5088 _bfd_error_handler (_(" non-TLS: %pA"), s);
5091 bfd_set_error (bfd_error_bad_value);
5103 && (abfd->flags & D_PAGED) != 0
5104 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5105 for (s = first_mbind; s != NULL; s = s->next)
5106 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5107 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5109 /* Mandated PF_R. */
5110 unsigned long p_flags = PF_R;
5111 if ((s->flags & SEC_READONLY) == 0)
5113 if ((s->flags & SEC_CODE) != 0)
5116 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5117 m = bfd_zalloc (abfd, amt);
5121 m->p_type = (PT_GNU_MBIND_LO
5122 + elf_section_data (s)->this_hdr.sh_info);
5124 m->p_flags_valid = 1;
5126 m->p_flags = p_flags;
5132 s = bfd_get_section_by_name (abfd,
5133 NOTE_GNU_PROPERTY_SECTION_NAME);
5134 if (s != NULL && s->size != 0)
5136 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5137 m = bfd_zalloc (abfd, amt);
5141 m->p_type = PT_GNU_PROPERTY;
5143 m->p_flags_valid = 1;
5150 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5152 eh_frame_hdr = elf_eh_frame_hdr (abfd);
5153 if (eh_frame_hdr != NULL
5154 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5156 amt = sizeof (struct elf_segment_map);
5157 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5161 m->p_type = PT_GNU_EH_FRAME;
5163 m->sections[0] = eh_frame_hdr->output_section;
5169 if (elf_stack_flags (abfd))
5171 amt = sizeof (struct elf_segment_map);
5172 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5176 m->p_type = PT_GNU_STACK;
5177 m->p_flags = elf_stack_flags (abfd);
5178 m->p_align = bed->stack_align;
5179 m->p_flags_valid = 1;
5180 m->p_align_valid = m->p_align != 0;
5181 if (info->stacksize > 0)
5183 m->p_size = info->stacksize;
5184 m->p_size_valid = 1;
5191 if (info != NULL && info->relro)
5193 for (m = mfirst; m != NULL; m = m->next)
5195 if (m->p_type == PT_LOAD
5197 && m->sections[0]->vma >= info->relro_start
5198 && m->sections[0]->vma < info->relro_end)
5201 while (--i != (unsigned) -1)
5202 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5203 == (SEC_LOAD | SEC_HAS_CONTENTS))
5206 if (i != (unsigned) -1)
5211 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5214 amt = sizeof (struct elf_segment_map);
5215 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5219 m->p_type = PT_GNU_RELRO;
5226 elf_seg_map (abfd) = mfirst;
5229 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5232 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5234 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5239 if (sections != NULL)
5244 /* Sort sections by address. */
5247 elf_sort_sections (const void *arg1, const void *arg2)
5249 const asection *sec1 = *(const asection **) arg1;
5250 const asection *sec2 = *(const asection **) arg2;
5251 bfd_size_type size1, size2;
5253 /* Sort by LMA first, since this is the address used to
5254 place the section into a segment. */
5255 if (sec1->lma < sec2->lma)
5257 else if (sec1->lma > sec2->lma)
5260 /* Then sort by VMA. Normally the LMA and the VMA will be
5261 the same, and this will do nothing. */
5262 if (sec1->vma < sec2->vma)
5264 else if (sec1->vma > sec2->vma)
5267 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5269 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5275 /* If the indices are the same, do not return 0
5276 here, but continue to try the next comparison. */
5277 if (sec1->target_index - sec2->target_index != 0)
5278 return sec1->target_index - sec2->target_index;
5283 else if (TOEND (sec2))
5288 /* Sort by size, to put zero sized sections
5289 before others at the same address. */
5291 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5292 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5299 return sec1->target_index - sec2->target_index;
5302 /* Ian Lance Taylor writes:
5304 We shouldn't be using % with a negative signed number. That's just
5305 not good. We have to make sure either that the number is not
5306 negative, or that the number has an unsigned type. When the types
5307 are all the same size they wind up as unsigned. When file_ptr is a
5308 larger signed type, the arithmetic winds up as signed long long,
5311 What we're trying to say here is something like ``increase OFF by
5312 the least amount that will cause it to be equal to the VMA modulo
5314 /* In other words, something like:
5316 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5317 off_offset = off % bed->maxpagesize;
5318 if (vma_offset < off_offset)
5319 adjustment = vma_offset + bed->maxpagesize - off_offset;
5321 adjustment = vma_offset - off_offset;
5323 which can be collapsed into the expression below. */
5326 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5328 /* PR binutils/16199: Handle an alignment of zero. */
5329 if (maxpagesize == 0)
5331 return ((vma - off) % maxpagesize);
5335 print_segment_map (const struct elf_segment_map *m)
5338 const char *pt = get_segment_type (m->p_type);
5343 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5344 sprintf (buf, "LOPROC+%7.7x",
5345 (unsigned int) (m->p_type - PT_LOPROC));
5346 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5347 sprintf (buf, "LOOS+%7.7x",
5348 (unsigned int) (m->p_type - PT_LOOS));
5350 snprintf (buf, sizeof (buf), "%8.8x",
5351 (unsigned int) m->p_type);
5355 fprintf (stderr, "%s:", pt);
5356 for (j = 0; j < m->count; j++)
5357 fprintf (stderr, " %s", m->sections [j]->name);
5363 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5368 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5370 buf = bfd_zmalloc (len);
5373 ret = bfd_bwrite (buf, len, abfd) == len;
5378 /* Assign file positions to the sections based on the mapping from
5379 sections to segments. This function also sets up some fields in
5383 assign_file_positions_for_load_sections (bfd *abfd,
5384 struct bfd_link_info *link_info)
5386 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5387 struct elf_segment_map *m;
5388 Elf_Internal_Phdr *phdrs;
5389 Elf_Internal_Phdr *p;
5391 bfd_size_type maxpagesize;
5392 unsigned int pt_load_count = 0;
5395 bfd_vma header_pad = 0;
5397 if (link_info == NULL
5398 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5402 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5406 header_pad = m->header_size;
5411 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5412 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5416 /* PR binutils/12467. */
5417 elf_elfheader (abfd)->e_phoff = 0;
5418 elf_elfheader (abfd)->e_phentsize = 0;
5421 elf_elfheader (abfd)->e_phnum = alloc;
5423 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5424 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5426 BFD_ASSERT (elf_program_header_size (abfd)
5427 >= alloc * bed->s->sizeof_phdr);
5431 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5435 /* We're writing the size in elf_program_header_size (abfd),
5436 see assign_file_positions_except_relocs, so make sure we have
5437 that amount allocated, with trailing space cleared.
5438 The variable alloc contains the computed need, while
5439 elf_program_header_size (abfd) contains the size used for the
5441 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5442 where the layout is forced to according to a larger size in the
5443 last iterations for the testcase ld-elf/header. */
5444 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
5446 phdrs = (Elf_Internal_Phdr *)
5448 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5449 sizeof (Elf_Internal_Phdr));
5450 elf_tdata (abfd)->phdr = phdrs;
5455 if ((abfd->flags & D_PAGED) != 0)
5456 maxpagesize = bed->maxpagesize;
5458 off = bed->s->sizeof_ehdr;
5459 off += alloc * bed->s->sizeof_phdr;
5460 if (header_pad < (bfd_vma) off)
5466 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
5468 m = m->next, p++, j++)
5472 bfd_boolean no_contents;
5474 /* If elf_segment_map is not from map_sections_to_segments, the
5475 sections may not be correctly ordered. NOTE: sorting should
5476 not be done to the PT_NOTE section of a corefile, which may
5477 contain several pseudo-sections artificially created by bfd.
5478 Sorting these pseudo-sections breaks things badly. */
5480 && !(elf_elfheader (abfd)->e_type == ET_CORE
5481 && m->p_type == PT_NOTE))
5482 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5485 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5486 number of sections with contents contributing to both p_filesz
5487 and p_memsz, followed by a number of sections with no contents
5488 that just contribute to p_memsz. In this loop, OFF tracks next
5489 available file offset for PT_LOAD and PT_NOTE segments. */
5490 p->p_type = m->p_type;
5491 p->p_flags = m->p_flags;
5494 p->p_vaddr = m->p_vaddr_offset;
5496 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
5498 if (m->p_paddr_valid)
5499 p->p_paddr = m->p_paddr;
5500 else if (m->count == 0)
5503 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
5505 if (p->p_type == PT_LOAD
5506 && (abfd->flags & D_PAGED) != 0)
5508 /* p_align in demand paged PT_LOAD segments effectively stores
5509 the maximum page size. When copying an executable with
5510 objcopy, we set m->p_align from the input file. Use this
5511 value for maxpagesize rather than bed->maxpagesize, which
5512 may be different. Note that we use maxpagesize for PT_TLS
5513 segment alignment later in this function, so we are relying
5514 on at least one PT_LOAD segment appearing before a PT_TLS
5516 if (m->p_align_valid)
5517 maxpagesize = m->p_align;
5519 p->p_align = maxpagesize;
5522 else if (m->p_align_valid)
5523 p->p_align = m->p_align;
5524 else if (m->count == 0)
5525 p->p_align = 1 << bed->s->log_file_align;
5529 no_contents = FALSE;
5531 if (p->p_type == PT_LOAD
5534 bfd_size_type align;
5535 unsigned int align_power = 0;
5537 if (m->p_align_valid)
5541 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5543 unsigned int secalign;
5545 secalign = bfd_section_alignment (*secpp);
5546 if (secalign > align_power)
5547 align_power = secalign;
5549 align = (bfd_size_type) 1 << align_power;
5550 if (align < maxpagesize)
5551 align = maxpagesize;
5554 for (i = 0; i < m->count; i++)
5555 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5556 /* If we aren't making room for this section, then
5557 it must be SHT_NOBITS regardless of what we've
5558 set via struct bfd_elf_special_section. */
5559 elf_section_type (m->sections[i]) = SHT_NOBITS;
5561 /* Find out whether this segment contains any loadable
5564 for (i = 0; i < m->count; i++)
5565 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5567 no_contents = FALSE;
5571 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5573 /* Broken hardware and/or kernel require that files do not
5574 map the same page with different permissions on some hppa
5576 if (pt_load_count > 1
5577 && bed->no_page_alias
5578 && (off & (maxpagesize - 1)) != 0
5579 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5580 off_adjust += maxpagesize;
5584 /* We shouldn't need to align the segment on disk since
5585 the segment doesn't need file space, but the gABI
5586 arguably requires the alignment and glibc ld.so
5587 checks it. So to comply with the alignment
5588 requirement but not waste file space, we adjust
5589 p_offset for just this segment. (OFF_ADJUST is
5590 subtracted from OFF later.) This may put p_offset
5591 past the end of file, but that shouldn't matter. */
5596 /* Make sure the .dynamic section is the first section in the
5597 PT_DYNAMIC segment. */
5598 else if (p->p_type == PT_DYNAMIC
5600 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5603 (_("%pB: The first section in the PT_DYNAMIC segment"
5604 " is not the .dynamic section"),
5606 bfd_set_error (bfd_error_bad_value);
5609 /* Set the note section type to SHT_NOTE. */
5610 else if (p->p_type == PT_NOTE)
5611 for (i = 0; i < m->count; i++)
5612 elf_section_type (m->sections[i]) = SHT_NOTE;
5618 if (m->includes_filehdr)
5620 if (!m->p_flags_valid)
5622 p->p_filesz = bed->s->sizeof_ehdr;
5623 p->p_memsz = bed->s->sizeof_ehdr;
5626 if (p->p_vaddr < (bfd_vma) off
5627 || (!m->p_paddr_valid
5628 && p->p_paddr < (bfd_vma) off))
5631 (_("%pB: not enough room for program headers,"
5632 " try linking with -N"),
5634 bfd_set_error (bfd_error_bad_value);
5639 if (!m->p_paddr_valid)
5644 if (m->includes_phdrs)
5646 if (!m->p_flags_valid)
5649 if (!m->includes_filehdr)
5651 p->p_offset = bed->s->sizeof_ehdr;
5655 p->p_vaddr -= off - p->p_offset;
5656 if (!m->p_paddr_valid)
5657 p->p_paddr -= off - p->p_offset;
5661 p->p_filesz += alloc * bed->s->sizeof_phdr;
5662 p->p_memsz += alloc * bed->s->sizeof_phdr;
5665 p->p_filesz += header_pad;
5666 p->p_memsz += header_pad;
5670 if (p->p_type == PT_LOAD
5671 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5673 if (!m->includes_filehdr && !m->includes_phdrs)
5679 adjust = off - (p->p_offset + p->p_filesz);
5681 p->p_filesz += adjust;
5682 p->p_memsz += adjust;
5686 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5687 maps. Set filepos for sections in PT_LOAD segments, and in
5688 core files, for sections in PT_NOTE segments.
5689 assign_file_positions_for_non_load_sections will set filepos
5690 for other sections and update p_filesz for other segments. */
5691 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5694 bfd_size_type align;
5695 Elf_Internal_Shdr *this_hdr;
5698 this_hdr = &elf_section_data (sec)->this_hdr;
5699 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5701 if ((p->p_type == PT_LOAD
5702 || p->p_type == PT_TLS)
5703 && (this_hdr->sh_type != SHT_NOBITS
5704 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5705 && ((this_hdr->sh_flags & SHF_TLS) == 0
5706 || p->p_type == PT_TLS))))
5708 bfd_vma p_start = p->p_paddr;
5709 bfd_vma p_end = p_start + p->p_memsz;
5710 bfd_vma s_start = sec->lma;
5711 bfd_vma adjust = s_start - p_end;
5715 || p_end < p_start))
5718 /* xgettext:c-format */
5719 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5720 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
5724 p->p_memsz += adjust;
5726 if (this_hdr->sh_type != SHT_NOBITS)
5728 if (p->p_filesz + adjust < p->p_memsz)
5730 /* We have a PROGBITS section following NOBITS ones.
5731 Allocate file space for the NOBITS section(s) and
5733 adjust = p->p_memsz - p->p_filesz;
5734 if (!write_zeros (abfd, off, adjust))
5738 p->p_filesz += adjust;
5742 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5744 /* The section at i == 0 is the one that actually contains
5748 this_hdr->sh_offset = sec->filepos = off;
5749 off += this_hdr->sh_size;
5750 p->p_filesz = this_hdr->sh_size;
5756 /* The rest are fake sections that shouldn't be written. */
5765 if (p->p_type == PT_LOAD)
5767 this_hdr->sh_offset = sec->filepos = off;
5768 if (this_hdr->sh_type != SHT_NOBITS)
5769 off += this_hdr->sh_size;
5771 else if (this_hdr->sh_type == SHT_NOBITS
5772 && (this_hdr->sh_flags & SHF_TLS) != 0
5773 && this_hdr->sh_offset == 0)
5775 /* This is a .tbss section that didn't get a PT_LOAD.
5776 (See _bfd_elf_map_sections_to_segments "Create a
5777 final PT_LOAD".) Set sh_offset to the value it
5778 would have if we had created a zero p_filesz and
5779 p_memsz PT_LOAD header for the section. This
5780 also makes the PT_TLS header have the same
5782 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5784 this_hdr->sh_offset = sec->filepos = off + adjust;
5787 if (this_hdr->sh_type != SHT_NOBITS)
5789 p->p_filesz += this_hdr->sh_size;
5790 /* A load section without SHF_ALLOC is something like
5791 a note section in a PT_NOTE segment. These take
5792 file space but are not loaded into memory. */
5793 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5794 p->p_memsz += this_hdr->sh_size;
5796 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5798 if (p->p_type == PT_TLS)
5799 p->p_memsz += this_hdr->sh_size;
5801 /* .tbss is special. It doesn't contribute to p_memsz of
5803 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5804 p->p_memsz += this_hdr->sh_size;
5807 if (align > p->p_align
5808 && !m->p_align_valid
5809 && (p->p_type != PT_LOAD
5810 || (abfd->flags & D_PAGED) == 0))
5814 if (!m->p_flags_valid)
5817 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5819 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5826 /* Check that all sections are in a PT_LOAD segment.
5827 Don't check funky gdb generated core files. */
5828 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5830 bfd_boolean check_vma = TRUE;
5832 for (i = 1; i < m->count; i++)
5833 if (m->sections[i]->vma == m->sections[i - 1]->vma
5834 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5835 ->this_hdr), p) != 0
5836 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5837 ->this_hdr), p) != 0)
5839 /* Looks like we have overlays packed into the segment. */
5844 for (i = 0; i < m->count; i++)
5846 Elf_Internal_Shdr *this_hdr;
5849 sec = m->sections[i];
5850 this_hdr = &(elf_section_data(sec)->this_hdr);
5851 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5852 && !ELF_TBSS_SPECIAL (this_hdr, p))
5855 /* xgettext:c-format */
5856 (_("%pB: section `%pA' can't be allocated in segment %d"),
5858 print_segment_map (m);
5864 elf_next_file_pos (abfd) = off;
5868 /* Determine if a bfd is a debuginfo file. Unfortunately there
5869 is no defined method for detecting such files, so we have to
5870 use heuristics instead. */
5873 is_debuginfo_file (bfd *abfd)
5875 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5878 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
5879 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
5880 Elf_Internal_Shdr **headerp;
5882 for (headerp = start_headers; headerp < end_headers; headerp ++)
5884 Elf_Internal_Shdr *header = * headerp;
5886 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5887 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5888 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
5889 && header->sh_type != SHT_NOBITS
5890 && header->sh_type != SHT_NOTE)
5897 /* Assign file positions for the other sections, except for compressed debugging
5898 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
5901 assign_file_positions_for_non_load_sections (bfd *abfd,
5902 struct bfd_link_info *link_info)
5904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5905 Elf_Internal_Shdr **i_shdrpp;
5906 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5907 Elf_Internal_Phdr *phdrs;
5908 Elf_Internal_Phdr *p;
5909 struct elf_segment_map *m;
5910 struct elf_segment_map *hdrs_segment;
5911 bfd_vma filehdr_vaddr, filehdr_paddr;
5912 bfd_vma phdrs_vaddr, phdrs_paddr;
5916 i_shdrpp = elf_elfsections (abfd);
5917 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5918 off = elf_next_file_pos (abfd);
5919 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5921 Elf_Internal_Shdr *hdr;
5924 if (hdr->bfd_section != NULL
5925 && (hdr->bfd_section->filepos != 0
5926 || (hdr->sh_type == SHT_NOBITS
5927 && hdr->contents == NULL)))
5928 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5929 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5931 if (hdr->sh_size != 0
5932 /* PR 24717 - debuginfo files are known to be not strictly
5933 compliant with the ELF standard. In particular they often
5934 have .note.gnu.property sections that are outside of any
5935 loadable segment. This is not a problem for such files,
5936 so do not warn about them. */
5937 && ! is_debuginfo_file (abfd))
5939 /* xgettext:c-format */
5940 (_("%pB: warning: allocated section `%s' not in segment"),
5942 (hdr->bfd_section == NULL
5944 : hdr->bfd_section->name));
5945 /* We don't need to page align empty sections. */
5946 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5947 off += vma_page_aligned_bias (hdr->sh_addr, off,
5950 off += vma_page_aligned_bias (hdr->sh_addr, off,
5952 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5955 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5956 && hdr->bfd_section == NULL)
5957 /* We don't know the offset of these sections yet: their size has
5958 not been decided. */
5959 || (hdr->bfd_section != NULL
5960 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
5961 || (bfd_section_is_ctf (hdr->bfd_section)
5962 && abfd->is_linker_output)))
5963 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5964 || (elf_symtab_shndx_list (abfd) != NULL
5965 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5966 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5967 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5968 hdr->sh_offset = -1;
5970 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5973 /* Now that we have set the section file positions, we can set up
5974 the file positions for the non PT_LOAD segments. */
5978 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5980 hdrs_segment = NULL;
5981 phdrs = elf_tdata (abfd)->phdr;
5982 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5985 if (p->p_type != PT_LOAD)
5988 if (m->includes_filehdr)
5990 filehdr_vaddr = p->p_vaddr;
5991 filehdr_paddr = p->p_paddr;
5993 if (m->includes_phdrs)
5995 phdrs_vaddr = p->p_vaddr;
5996 phdrs_paddr = p->p_paddr;
5997 if (m->includes_filehdr)
6000 phdrs_vaddr += bed->s->sizeof_ehdr;
6001 phdrs_paddr += bed->s->sizeof_ehdr;
6006 if (hdrs_segment != NULL && link_info != NULL)
6008 /* There is a segment that contains both the file headers and the
6009 program headers, so provide a symbol __ehdr_start pointing there.
6010 A program can use this to examine itself robustly. */
6012 struct elf_link_hash_entry *hash
6013 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6014 FALSE, FALSE, TRUE);
6015 /* If the symbol was referenced and not defined, define it. */
6017 && (hash->root.type == bfd_link_hash_new
6018 || hash->root.type == bfd_link_hash_undefined
6019 || hash->root.type == bfd_link_hash_undefweak
6020 || hash->root.type == bfd_link_hash_common))
6023 if (hdrs_segment->count != 0)
6024 /* The segment contains sections, so use the first one. */
6025 s = hdrs_segment->sections[0];
6027 /* Use the first (i.e. lowest-addressed) section in any segment. */
6028 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6037 hash->root.u.def.value = filehdr_vaddr - s->vma;
6038 hash->root.u.def.section = s;
6042 hash->root.u.def.value = filehdr_vaddr;
6043 hash->root.u.def.section = bfd_abs_section_ptr;
6046 hash->root.type = bfd_link_hash_defined;
6047 hash->def_regular = 1;
6052 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6054 if (p->p_type == PT_GNU_RELRO)
6059 if (link_info != NULL)
6061 /* During linking the range of the RELRO segment is passed
6062 in link_info. Note that there may be padding between
6063 relro_start and the first RELRO section. */
6064 start = link_info->relro_start;
6065 end = link_info->relro_end;
6067 else if (m->count != 0)
6069 if (!m->p_size_valid)
6071 start = m->sections[0]->vma;
6072 end = start + m->p_size;
6083 struct elf_segment_map *lm;
6084 const Elf_Internal_Phdr *lp;
6087 /* Find a LOAD segment containing a section in the RELRO
6089 for (lm = elf_seg_map (abfd), lp = phdrs;
6091 lm = lm->next, lp++)
6093 if (lp->p_type == PT_LOAD
6095 && (lm->sections[lm->count - 1]->vma
6096 + (!IS_TBSS (lm->sections[lm->count - 1])
6097 ? lm->sections[lm->count - 1]->size
6099 && lm->sections[0]->vma < end)
6105 /* Find the section starting the RELRO segment. */
6106 for (i = 0; i < lm->count; i++)
6108 asection *s = lm->sections[i];
6117 p->p_vaddr = lm->sections[i]->vma;
6118 p->p_paddr = lm->sections[i]->lma;
6119 p->p_offset = lm->sections[i]->filepos;
6120 p->p_memsz = end - p->p_vaddr;
6121 p->p_filesz = p->p_memsz;
6123 /* The RELRO segment typically ends a few bytes
6124 into .got.plt but other layouts are possible.
6125 In cases where the end does not match any
6126 loaded section (for instance is in file
6127 padding), trim p_filesz back to correspond to
6128 the end of loaded section contents. */
6129 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6130 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6132 /* Preserve the alignment and flags if they are
6133 valid. The gold linker generates RW/4 for
6134 the PT_GNU_RELRO section. It is better for
6135 objcopy/strip to honor these attributes
6136 otherwise gdb will choke when using separate
6138 if (!m->p_align_valid)
6140 if (!m->p_flags_valid)
6146 if (link_info != NULL)
6149 memset (p, 0, sizeof *p);
6151 else if (p->p_type == PT_GNU_STACK)
6153 if (m->p_size_valid)
6154 p->p_memsz = m->p_size;
6156 else if (m->count != 0)
6160 if (p->p_type != PT_LOAD
6161 && (p->p_type != PT_NOTE
6162 || bfd_get_format (abfd) != bfd_core))
6164 /* A user specified segment layout may include a PHDR
6165 segment that overlaps with a LOAD segment... */
6166 if (p->p_type == PT_PHDR)
6172 if (m->includes_filehdr || m->includes_phdrs)
6174 /* PR 17512: file: 2195325e. */
6176 (_("%pB: error: non-load segment %d includes file header "
6177 "and/or program header"),
6178 abfd, (int) (p - phdrs));
6183 p->p_offset = m->sections[0]->filepos;
6184 for (i = m->count; i-- != 0;)
6186 asection *sect = m->sections[i];
6187 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6188 if (hdr->sh_type != SHT_NOBITS)
6190 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6197 else if (m->includes_filehdr)
6199 p->p_vaddr = filehdr_vaddr;
6200 if (! m->p_paddr_valid)
6201 p->p_paddr = filehdr_paddr;
6203 else if (m->includes_phdrs)
6205 p->p_vaddr = phdrs_vaddr;
6206 if (! m->p_paddr_valid)
6207 p->p_paddr = phdrs_paddr;
6211 elf_next_file_pos (abfd) = off;
6216 static elf_section_list *
6217 find_section_in_list (unsigned int i, elf_section_list * list)
6219 for (;list != NULL; list = list->next)
6225 /* Work out the file positions of all the sections. This is called by
6226 _bfd_elf_compute_section_file_positions. All the section sizes and
6227 VMAs must be known before this is called.
6229 Reloc sections come in two flavours: Those processed specially as
6230 "side-channel" data attached to a section to which they apply, and those that
6231 bfd doesn't process as relocations. The latter sort are stored in a normal
6232 bfd section by bfd_section_from_shdr. We don't consider the former sort
6233 here, unless they form part of the loadable image. Reloc sections not
6234 assigned here (and compressed debugging sections and CTF sections which
6235 nothing else in the file can rely upon) will be handled later by
6236 assign_file_positions_for_relocs.
6238 We also don't set the positions of the .symtab and .strtab here. */
6241 assign_file_positions_except_relocs (bfd *abfd,
6242 struct bfd_link_info *link_info)
6244 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6245 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6246 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6248 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6249 && bfd_get_format (abfd) != bfd_core)
6251 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6252 unsigned int num_sec = elf_numsections (abfd);
6253 Elf_Internal_Shdr **hdrpp;
6257 /* Start after the ELF header. */
6258 off = i_ehdrp->e_ehsize;
6260 /* We are not creating an executable, which means that we are
6261 not creating a program header, and that the actual order of
6262 the sections in the file is unimportant. */
6263 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6265 Elf_Internal_Shdr *hdr;
6268 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6269 && hdr->bfd_section == NULL)
6270 /* Do not assign offsets for these sections yet: we don't know
6272 || (hdr->bfd_section != NULL
6273 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6274 || (bfd_section_is_ctf (hdr->bfd_section)
6275 && abfd->is_linker_output)))
6276 || i == elf_onesymtab (abfd)
6277 || (elf_symtab_shndx_list (abfd) != NULL
6278 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6279 || i == elf_strtab_sec (abfd)
6280 || i == elf_shstrtab_sec (abfd))
6282 hdr->sh_offset = -1;
6285 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6288 elf_next_file_pos (abfd) = off;
6294 /* Assign file positions for the loaded sections based on the
6295 assignment of sections to segments. */
6296 if (!assign_file_positions_for_load_sections (abfd, link_info))
6299 /* And for non-load sections. */
6300 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6303 if (bed->elf_backend_modify_program_headers != NULL)
6305 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6309 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6310 if (link_info != NULL && bfd_link_pie (link_info))
6312 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6313 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6314 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6316 /* Find the lowest p_vaddr in PT_LOAD segments. */
6317 bfd_vma p_vaddr = (bfd_vma) -1;
6318 for (; segment < end_segment; segment++)
6319 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6320 p_vaddr = segment->p_vaddr;
6322 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6323 segments is non-zero. */
6325 i_ehdrp->e_type = ET_EXEC;
6328 /* Write out the program headers. */
6329 alloc = elf_elfheader (abfd)->e_phnum;
6333 /* PR ld/20815 - Check that the program header segment, if present, will
6334 be loaded into memory. FIXME: The check below is not sufficient as
6335 really all PT_LOAD segments should be checked before issuing an error
6336 message. Plus the PHDR segment does not have to be the first segment
6337 in the program header table. But this version of the check should
6338 catch all real world use cases.
6340 FIXME: We used to have code here to sort the PT_LOAD segments into
6341 ascending order, as per the ELF spec. But this breaks some programs,
6342 including the Linux kernel. But really either the spec should be
6343 changed or the programs updated. */
6345 && tdata->phdr[0].p_type == PT_PHDR
6346 && (bed->elf_backend_allow_non_load_phdr == NULL
6347 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6349 && tdata->phdr[1].p_type == PT_LOAD
6350 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6351 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6352 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
6354 /* The fix for this error is usually to edit the linker script being
6355 used and set up the program headers manually. Either that or
6356 leave room for the headers at the start of the SECTIONS. */
6357 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6358 " by LOAD segment"),
6363 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
6364 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6372 prep_headers (bfd *abfd)
6374 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6375 struct elf_strtab_hash *shstrtab;
6376 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6378 i_ehdrp = elf_elfheader (abfd);
6380 shstrtab = _bfd_elf_strtab_init ();
6381 if (shstrtab == NULL)
6384 elf_shstrtab (abfd) = shstrtab;
6386 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6387 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6388 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6389 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6391 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6392 i_ehdrp->e_ident[EI_DATA] =
6393 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6394 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6396 if ((abfd->flags & DYNAMIC) != 0)
6397 i_ehdrp->e_type = ET_DYN;
6398 else if ((abfd->flags & EXEC_P) != 0)
6399 i_ehdrp->e_type = ET_EXEC;
6400 else if (bfd_get_format (abfd) == bfd_core)
6401 i_ehdrp->e_type = ET_CORE;
6403 i_ehdrp->e_type = ET_REL;
6405 switch (bfd_get_arch (abfd))
6407 case bfd_arch_unknown:
6408 i_ehdrp->e_machine = EM_NONE;
6411 /* There used to be a long list of cases here, each one setting
6412 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6413 in the corresponding bfd definition. To avoid duplication,
6414 the switch was removed. Machines that need special handling
6415 can generally do it in elf_backend_final_write_processing(),
6416 unless they need the information earlier than the final write.
6417 Such need can generally be supplied by replacing the tests for
6418 e_machine with the conditions used to determine it. */
6420 i_ehdrp->e_machine = bed->elf_machine_code;
6423 i_ehdrp->e_version = bed->s->ev_current;
6424 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6426 /* No program header, for now. */
6427 i_ehdrp->e_phoff = 0;
6428 i_ehdrp->e_phentsize = 0;
6429 i_ehdrp->e_phnum = 0;
6431 /* Each bfd section is section header entry. */
6432 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6433 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6435 /* If we're building an executable, we'll need a program header table. */
6436 if (abfd->flags & EXEC_P)
6437 /* It all happens later. */
6441 i_ehdrp->e_phentsize = 0;
6442 i_ehdrp->e_phoff = 0;
6445 elf_tdata (abfd)->symtab_hdr.sh_name =
6446 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6447 elf_tdata (abfd)->strtab_hdr.sh_name =
6448 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6449 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6450 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6451 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6452 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6453 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6459 /* Assign file positions for all the reloc sections which are not part
6460 of the loadable file image, and the file position of section headers. */
6463 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6466 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6467 Elf_Internal_Shdr *shdrp;
6468 Elf_Internal_Ehdr *i_ehdrp;
6469 const struct elf_backend_data *bed;
6471 off = elf_next_file_pos (abfd);
6473 shdrpp = elf_elfsections (abfd);
6474 end_shdrpp = shdrpp + elf_numsections (abfd);
6475 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6478 if (shdrp->sh_offset == -1)
6480 asection *sec = shdrp->bfd_section;
6481 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6482 || shdrp->sh_type == SHT_RELA);
6483 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
6486 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6488 if (!is_rel && !is_ctf)
6490 const char *name = sec->name;
6491 struct bfd_elf_section_data *d;
6493 /* Compress DWARF debug sections. */
6494 if (!bfd_compress_section (abfd, sec,
6498 if (sec->compress_status == COMPRESS_SECTION_DONE
6499 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6501 /* If section is compressed with zlib-gnu, convert
6502 section name from .debug_* to .zdebug_*. */
6504 = convert_debug_to_zdebug (abfd, name);
6505 if (new_name == NULL)
6509 /* Add section name to section name section. */
6510 if (shdrp->sh_name != (unsigned int) -1)
6513 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6515 d = elf_section_data (sec);
6517 /* Add reloc section name to section name section. */
6519 && !_bfd_elf_set_reloc_sh_name (abfd,
6524 && !_bfd_elf_set_reloc_sh_name (abfd,
6529 /* Update section size and contents. */
6530 shdrp->sh_size = sec->size;
6531 shdrp->contents = sec->contents;
6532 shdrp->bfd_section->contents = NULL;
6536 /* Update section size and contents. */
6537 shdrp->sh_size = sec->size;
6538 shdrp->contents = sec->contents;
6541 off = _bfd_elf_assign_file_position_for_section (shdrp,
6548 /* Place section name section after DWARF debug sections have been
6550 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6551 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6552 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6553 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6555 /* Place the section headers. */
6556 i_ehdrp = elf_elfheader (abfd);
6557 bed = get_elf_backend_data (abfd);
6558 off = align_file_position (off, 1 << bed->s->log_file_align);
6559 i_ehdrp->e_shoff = off;
6560 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6561 elf_next_file_pos (abfd) = off;
6567 _bfd_elf_write_object_contents (bfd *abfd)
6569 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6570 Elf_Internal_Shdr **i_shdrp;
6572 unsigned int count, num_sec;
6573 struct elf_obj_tdata *t;
6575 if (! abfd->output_has_begun
6576 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6578 /* Do not rewrite ELF data when the BFD has been opened for update.
6579 abfd->output_has_begun was set to TRUE on opening, so creation of new
6580 sections, and modification of existing section sizes was restricted.
6581 This means the ELF header, program headers and section headers can't have
6583 If the contents of any sections has been modified, then those changes have
6584 already been written to the BFD. */
6585 else if (abfd->direction == both_direction)
6587 BFD_ASSERT (abfd->output_has_begun);
6591 i_shdrp = elf_elfsections (abfd);
6594 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6598 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6601 /* After writing the headers, we need to write the sections too... */
6602 num_sec = elf_numsections (abfd);
6603 for (count = 1; count < num_sec; count++)
6605 i_shdrp[count]->sh_name
6606 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6607 i_shdrp[count]->sh_name);
6608 if (bed->elf_backend_section_processing)
6609 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6611 if (i_shdrp[count]->contents)
6613 bfd_size_type amt = i_shdrp[count]->sh_size;
6615 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6616 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6621 /* Write out the section header names. */
6622 t = elf_tdata (abfd);
6623 if (elf_shstrtab (abfd) != NULL
6624 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6625 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6628 if (!(*bed->elf_backend_final_write_processing) (abfd))
6631 if (!bed->s->write_shdrs_and_ehdr (abfd))
6634 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6635 if (t->o->build_id.after_write_object_contents != NULL)
6636 return (*t->o->build_id.after_write_object_contents) (abfd);
6642 _bfd_elf_write_corefile_contents (bfd *abfd)
6644 /* Hopefully this can be done just like an object file. */
6645 return _bfd_elf_write_object_contents (abfd);
6648 /* Given a section, search the header to find them. */
6651 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6653 const struct elf_backend_data *bed;
6654 unsigned int sec_index;
6656 if (elf_section_data (asect) != NULL
6657 && elf_section_data (asect)->this_idx != 0)
6658 return elf_section_data (asect)->this_idx;
6660 if (bfd_is_abs_section (asect))
6661 sec_index = SHN_ABS;
6662 else if (bfd_is_com_section (asect))
6663 sec_index = SHN_COMMON;
6664 else if (bfd_is_und_section (asect))
6665 sec_index = SHN_UNDEF;
6667 sec_index = SHN_BAD;
6669 bed = get_elf_backend_data (abfd);
6670 if (bed->elf_backend_section_from_bfd_section)
6672 int retval = sec_index;
6674 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6678 if (sec_index == SHN_BAD)
6679 bfd_set_error (bfd_error_nonrepresentable_section);
6684 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6688 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6690 asymbol *asym_ptr = *asym_ptr_ptr;
6692 flagword flags = asym_ptr->flags;
6694 /* When gas creates relocations against local labels, it creates its
6695 own symbol for the section, but does put the symbol into the
6696 symbol chain, so udata is 0. When the linker is generating
6697 relocatable output, this section symbol may be for one of the
6698 input sections rather than the output section. */
6699 if (asym_ptr->udata.i == 0
6700 && (flags & BSF_SECTION_SYM)
6701 && asym_ptr->section)
6706 sec = asym_ptr->section;
6707 if (sec->owner != abfd && sec->output_section != NULL)
6708 sec = sec->output_section;
6709 if (sec->owner == abfd
6710 && (indx = sec->index) < elf_num_section_syms (abfd)
6711 && elf_section_syms (abfd)[indx] != NULL)
6712 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6715 idx = asym_ptr->udata.i;
6719 /* This case can occur when using --strip-symbol on a symbol
6720 which is used in a relocation entry. */
6722 /* xgettext:c-format */
6723 (_("%pB: symbol `%s' required but not present"),
6724 abfd, bfd_asymbol_name (asym_ptr));
6725 bfd_set_error (bfd_error_no_symbols);
6732 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6733 (long) asym_ptr, asym_ptr->name, idx, flags);
6741 /* Rewrite program header information. */
6744 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6746 Elf_Internal_Ehdr *iehdr;
6747 struct elf_segment_map *map;
6748 struct elf_segment_map *map_first;
6749 struct elf_segment_map **pointer_to_map;
6750 Elf_Internal_Phdr *segment;
6753 unsigned int num_segments;
6754 bfd_boolean phdr_included = FALSE;
6755 bfd_boolean p_paddr_valid;
6756 bfd_vma maxpagesize;
6757 struct elf_segment_map *phdr_adjust_seg = NULL;
6758 unsigned int phdr_adjust_num = 0;
6759 const struct elf_backend_data *bed;
6761 bed = get_elf_backend_data (ibfd);
6762 iehdr = elf_elfheader (ibfd);
6765 pointer_to_map = &map_first;
6767 num_segments = elf_elfheader (ibfd)->e_phnum;
6768 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6770 /* Returns the end address of the segment + 1. */
6771 #define SEGMENT_END(segment, start) \
6772 (start + (segment->p_memsz > segment->p_filesz \
6773 ? segment->p_memsz : segment->p_filesz))
6775 #define SECTION_SIZE(section, segment) \
6776 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6777 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6778 ? section->size : 0)
6780 /* Returns TRUE if the given section is contained within
6781 the given segment. VMA addresses are compared. */
6782 #define IS_CONTAINED_BY_VMA(section, segment) \
6783 (section->vma >= segment->p_vaddr \
6784 && (section->vma + SECTION_SIZE (section, segment) \
6785 <= (SEGMENT_END (segment, segment->p_vaddr))))
6787 /* Returns TRUE if the given section is contained within
6788 the given segment. LMA addresses are compared. */
6789 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6790 (section->lma >= base \
6791 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6792 && (section->lma + SECTION_SIZE (section, segment) \
6793 <= SEGMENT_END (segment, base)))
6795 /* Handle PT_NOTE segment. */
6796 #define IS_NOTE(p, s) \
6797 (p->p_type == PT_NOTE \
6798 && elf_section_type (s) == SHT_NOTE \
6799 && (bfd_vma) s->filepos >= p->p_offset \
6800 && ((bfd_vma) s->filepos + s->size \
6801 <= p->p_offset + p->p_filesz))
6803 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6805 #define IS_COREFILE_NOTE(p, s) \
6807 && bfd_get_format (ibfd) == bfd_core \
6811 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6812 linker, which generates a PT_INTERP section with p_vaddr and
6813 p_memsz set to 0. */
6814 #define IS_SOLARIS_PT_INTERP(p, s) \
6816 && p->p_paddr == 0 \
6817 && p->p_memsz == 0 \
6818 && p->p_filesz > 0 \
6819 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6821 && (bfd_vma) s->filepos >= p->p_offset \
6822 && ((bfd_vma) s->filepos + s->size \
6823 <= p->p_offset + p->p_filesz))
6825 /* Decide if the given section should be included in the given segment.
6826 A section will be included if:
6827 1. It is within the address space of the segment -- we use the LMA
6828 if that is set for the segment and the VMA otherwise,
6829 2. It is an allocated section or a NOTE section in a PT_NOTE
6831 3. There is an output section associated with it,
6832 4. The section has not already been allocated to a previous segment.
6833 5. PT_GNU_STACK segments do not include any sections.
6834 6. PT_TLS segment includes only SHF_TLS sections.
6835 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6836 8. PT_DYNAMIC should not contain empty sections at the beginning
6837 (with the possible exception of .dynamic). */
6838 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6839 ((((segment->p_paddr \
6840 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6841 : IS_CONTAINED_BY_VMA (section, segment)) \
6842 && (section->flags & SEC_ALLOC) != 0) \
6843 || IS_NOTE (segment, section)) \
6844 && segment->p_type != PT_GNU_STACK \
6845 && (segment->p_type != PT_TLS \
6846 || (section->flags & SEC_THREAD_LOCAL)) \
6847 && (segment->p_type == PT_LOAD \
6848 || segment->p_type == PT_TLS \
6849 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6850 && (segment->p_type != PT_DYNAMIC \
6851 || SECTION_SIZE (section, segment) > 0 \
6852 || (segment->p_paddr \
6853 ? segment->p_paddr != section->lma \
6854 : segment->p_vaddr != section->vma) \
6855 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6856 && (segment->p_type != PT_LOAD || !section->segment_mark))
6858 /* If the output section of a section in the input segment is NULL,
6859 it is removed from the corresponding output segment. */
6860 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6861 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6862 && section->output_section != NULL)
6864 /* Returns TRUE iff seg1 starts after the end of seg2. */
6865 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6866 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6868 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6869 their VMA address ranges and their LMA address ranges overlap.
6870 It is possible to have overlapping VMA ranges without overlapping LMA
6871 ranges. RedBoot images for example can have both .data and .bss mapped
6872 to the same VMA range, but with the .data section mapped to a different
6874 #define SEGMENT_OVERLAPS(seg1, seg2) \
6875 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6876 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6877 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6878 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6880 /* Initialise the segment mark field. */
6881 for (section = ibfd->sections; section != NULL; section = section->next)
6882 section->segment_mark = FALSE;
6884 /* The Solaris linker creates program headers in which all the
6885 p_paddr fields are zero. When we try to objcopy or strip such a
6886 file, we get confused. Check for this case, and if we find it
6887 don't set the p_paddr_valid fields. */
6888 p_paddr_valid = FALSE;
6889 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6892 if (segment->p_paddr != 0)
6894 p_paddr_valid = TRUE;
6898 /* Scan through the segments specified in the program header
6899 of the input BFD. For this first scan we look for overlaps
6900 in the loadable segments. These can be created by weird
6901 parameters to objcopy. Also, fix some solaris weirdness. */
6902 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6907 Elf_Internal_Phdr *segment2;
6909 if (segment->p_type == PT_INTERP)
6910 for (section = ibfd->sections; section; section = section->next)
6911 if (IS_SOLARIS_PT_INTERP (segment, section))
6913 /* Mininal change so that the normal section to segment
6914 assignment code will work. */
6915 segment->p_vaddr = section->vma;
6919 if (segment->p_type != PT_LOAD)
6921 /* Remove PT_GNU_RELRO segment. */
6922 if (segment->p_type == PT_GNU_RELRO)
6923 segment->p_type = PT_NULL;
6927 /* Determine if this segment overlaps any previous segments. */
6928 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6930 bfd_signed_vma extra_length;
6932 if (segment2->p_type != PT_LOAD
6933 || !SEGMENT_OVERLAPS (segment, segment2))
6936 /* Merge the two segments together. */
6937 if (segment2->p_vaddr < segment->p_vaddr)
6939 /* Extend SEGMENT2 to include SEGMENT and then delete
6941 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6942 - SEGMENT_END (segment2, segment2->p_vaddr));
6944 if (extra_length > 0)
6946 segment2->p_memsz += extra_length;
6947 segment2->p_filesz += extra_length;
6950 segment->p_type = PT_NULL;
6952 /* Since we have deleted P we must restart the outer loop. */
6954 segment = elf_tdata (ibfd)->phdr;
6959 /* Extend SEGMENT to include SEGMENT2 and then delete
6961 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6962 - SEGMENT_END (segment, segment->p_vaddr));
6964 if (extra_length > 0)
6966 segment->p_memsz += extra_length;
6967 segment->p_filesz += extra_length;
6970 segment2->p_type = PT_NULL;
6975 /* The second scan attempts to assign sections to segments. */
6976 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6980 unsigned int section_count;
6981 asection **sections;
6982 asection *output_section;
6984 asection *matching_lma;
6985 asection *suggested_lma;
6988 asection *first_section;
6990 if (segment->p_type == PT_NULL)
6993 first_section = NULL;
6994 /* Compute how many sections might be placed into this segment. */
6995 for (section = ibfd->sections, section_count = 0;
6997 section = section->next)
6999 /* Find the first section in the input segment, which may be
7000 removed from the corresponding output segment. */
7001 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
7003 if (first_section == NULL)
7004 first_section = section;
7005 if (section->output_section != NULL)
7010 /* Allocate a segment map big enough to contain
7011 all of the sections we have selected. */
7012 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7013 amt += (bfd_size_type) section_count * sizeof (asection *);
7014 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7018 /* Initialise the fields of the segment map. Default to
7019 using the physical address of the segment in the input BFD. */
7021 map->p_type = segment->p_type;
7022 map->p_flags = segment->p_flags;
7023 map->p_flags_valid = 1;
7025 /* If the first section in the input segment is removed, there is
7026 no need to preserve segment physical address in the corresponding
7028 if (!first_section || first_section->output_section != NULL)
7030 map->p_paddr = segment->p_paddr;
7031 map->p_paddr_valid = p_paddr_valid;
7034 /* Determine if this segment contains the ELF file header
7035 and if it contains the program headers themselves. */
7036 map->includes_filehdr = (segment->p_offset == 0
7037 && segment->p_filesz >= iehdr->e_ehsize);
7038 map->includes_phdrs = 0;
7040 if (!phdr_included || segment->p_type != PT_LOAD)
7042 map->includes_phdrs =
7043 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7044 && (segment->p_offset + segment->p_filesz
7045 >= ((bfd_vma) iehdr->e_phoff
7046 + iehdr->e_phnum * iehdr->e_phentsize)));
7048 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7049 phdr_included = TRUE;
7052 if (section_count == 0)
7054 /* Special segments, such as the PT_PHDR segment, may contain
7055 no sections, but ordinary, loadable segments should contain
7056 something. They are allowed by the ELF spec however, so only
7057 a warning is produced.
7058 There is however the valid use case of embedded systems which
7059 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7060 flash memory with zeros. No warning is shown for that case. */
7061 if (segment->p_type == PT_LOAD
7062 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7063 /* xgettext:c-format */
7065 (_("%pB: warning: empty loadable segment detected"
7066 " at vaddr=%#" PRIx64 ", is this intentional?"),
7067 ibfd, (uint64_t) segment->p_vaddr);
7069 map->p_vaddr_offset = segment->p_vaddr;
7071 *pointer_to_map = map;
7072 pointer_to_map = &map->next;
7077 /* Now scan the sections in the input BFD again and attempt
7078 to add their corresponding output sections to the segment map.
7079 The problem here is how to handle an output section which has
7080 been moved (ie had its LMA changed). There are four possibilities:
7082 1. None of the sections have been moved.
7083 In this case we can continue to use the segment LMA from the
7086 2. All of the sections have been moved by the same amount.
7087 In this case we can change the segment's LMA to match the LMA
7088 of the first section.
7090 3. Some of the sections have been moved, others have not.
7091 In this case those sections which have not been moved can be
7092 placed in the current segment which will have to have its size,
7093 and possibly its LMA changed, and a new segment or segments will
7094 have to be created to contain the other sections.
7096 4. The sections have been moved, but not by the same amount.
7097 In this case we can change the segment's LMA to match the LMA
7098 of the first section and we will have to create a new segment
7099 or segments to contain the other sections.
7101 In order to save time, we allocate an array to hold the section
7102 pointers that we are interested in. As these sections get assigned
7103 to a segment, they are removed from this array. */
7105 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
7106 if (sections == NULL)
7109 /* Step One: Scan for segment vs section LMA conflicts.
7110 Also add the sections to the section array allocated above.
7111 Also add the sections to the current segment. In the common
7112 case, where the sections have not been moved, this means that
7113 we have completely filled the segment, and there is nothing
7116 matching_lma = NULL;
7117 suggested_lma = NULL;
7119 for (section = first_section, j = 0;
7121 section = section->next)
7123 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
7125 output_section = section->output_section;
7127 sections[j++] = section;
7129 /* The Solaris native linker always sets p_paddr to 0.
7130 We try to catch that case here, and set it to the
7131 correct value. Note - some backends require that
7132 p_paddr be left as zero. */
7134 && segment->p_vaddr != 0
7135 && !bed->want_p_paddr_set_to_zero
7137 && output_section->lma != 0
7138 && (align_power (segment->p_vaddr
7139 + (map->includes_filehdr
7140 ? iehdr->e_ehsize : 0)
7141 + (map->includes_phdrs
7142 ? iehdr->e_phnum * iehdr->e_phentsize
7144 output_section->alignment_power)
7145 == output_section->vma))
7146 map->p_paddr = segment->p_vaddr;
7148 /* Match up the physical address of the segment with the
7149 LMA address of the output section. */
7150 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7151 || IS_COREFILE_NOTE (segment, section)
7152 || (bed->want_p_paddr_set_to_zero
7153 && IS_CONTAINED_BY_VMA (output_section, segment)))
7155 if (matching_lma == NULL
7156 || output_section->lma < matching_lma->lma)
7157 matching_lma = output_section;
7159 /* We assume that if the section fits within the segment
7160 then it does not overlap any other section within that
7162 map->sections[isec++] = output_section;
7164 else if (suggested_lma == NULL)
7165 suggested_lma = output_section;
7167 if (j == section_count)
7172 BFD_ASSERT (j == section_count);
7174 /* Step Two: Adjust the physical address of the current segment,
7176 if (isec == section_count)
7178 /* All of the sections fitted within the segment as currently
7179 specified. This is the default case. Add the segment to
7180 the list of built segments and carry on to process the next
7181 program header in the input BFD. */
7182 map->count = section_count;
7183 *pointer_to_map = map;
7184 pointer_to_map = &map->next;
7187 && !bed->want_p_paddr_set_to_zero
7188 && matching_lma->lma != map->p_paddr
7189 && !map->includes_filehdr
7190 && !map->includes_phdrs)
7191 /* There is some padding before the first section in the
7192 segment. So, we must account for that in the output
7194 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
7201 /* Change the current segment's physical address to match
7202 the LMA of the first section that fitted, or if no
7203 section fitted, the first section. */
7204 if (matching_lma == NULL)
7205 matching_lma = suggested_lma;
7207 map->p_paddr = matching_lma->lma;
7209 /* Offset the segment physical address from the lma
7210 to allow for space taken up by elf headers. */
7211 if (map->includes_phdrs)
7213 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7215 /* iehdr->e_phnum is just an estimate of the number
7216 of program headers that we will need. Make a note
7217 here of the number we used and the segment we chose
7218 to hold these headers, so that we can adjust the
7219 offset when we know the correct value. */
7220 phdr_adjust_num = iehdr->e_phnum;
7221 phdr_adjust_seg = map;
7224 if (map->includes_filehdr)
7226 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7227 map->p_paddr -= iehdr->e_ehsize;
7228 /* We've subtracted off the size of headers from the
7229 first section lma, but there may have been some
7230 alignment padding before that section too. Try to
7231 account for that by adjusting the segment lma down to
7232 the same alignment. */
7233 if (segment->p_align != 0 && segment->p_align < align)
7234 align = segment->p_align;
7235 map->p_paddr &= -align;
7239 /* Step Three: Loop over the sections again, this time assigning
7240 those that fit to the current segment and removing them from the
7241 sections array; but making sure not to leave large gaps. Once all
7242 possible sections have been assigned to the current segment it is
7243 added to the list of built segments and if sections still remain
7244 to be assigned, a new segment is constructed before repeating
7250 suggested_lma = NULL;
7252 /* Fill the current segment with sections that fit. */
7253 for (j = 0; j < section_count; j++)
7255 section = sections[j];
7257 if (section == NULL)
7260 output_section = section->output_section;
7262 BFD_ASSERT (output_section != NULL);
7264 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7265 || IS_COREFILE_NOTE (segment, section))
7267 if (map->count == 0)
7269 /* If the first section in a segment does not start at
7270 the beginning of the segment, then something is
7272 if (align_power (map->p_paddr
7273 + (map->includes_filehdr
7274 ? iehdr->e_ehsize : 0)
7275 + (map->includes_phdrs
7276 ? iehdr->e_phnum * iehdr->e_phentsize
7278 output_section->alignment_power)
7279 != output_section->lma)
7286 prev_sec = map->sections[map->count - 1];
7288 /* If the gap between the end of the previous section
7289 and the start of this section is more than
7290 maxpagesize then we need to start a new segment. */
7291 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7293 < BFD_ALIGN (output_section->lma, maxpagesize))
7294 || (prev_sec->lma + prev_sec->size
7295 > output_section->lma))
7297 if (suggested_lma == NULL)
7298 suggested_lma = output_section;
7304 map->sections[map->count++] = output_section;
7307 if (segment->p_type == PT_LOAD)
7308 section->segment_mark = TRUE;
7310 else if (suggested_lma == NULL)
7311 suggested_lma = output_section;
7314 /* PR 23932. A corrupt input file may contain sections that cannot
7315 be assigned to any segment - because for example they have a
7316 negative size - or segments that do not contain any sections. */
7317 if (map->count == 0)
7319 bfd_set_error (bfd_error_bad_value);
7324 /* Add the current segment to the list of built segments. */
7325 *pointer_to_map = map;
7326 pointer_to_map = &map->next;
7328 if (isec < section_count)
7330 /* We still have not allocated all of the sections to
7331 segments. Create a new segment here, initialise it
7332 and carry on looping. */
7333 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7334 amt += (bfd_size_type) section_count * sizeof (asection *);
7335 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7342 /* Initialise the fields of the segment map. Set the physical
7343 physical address to the LMA of the first section that has
7344 not yet been assigned. */
7346 map->p_type = segment->p_type;
7347 map->p_flags = segment->p_flags;
7348 map->p_flags_valid = 1;
7349 map->p_paddr = suggested_lma->lma;
7350 map->p_paddr_valid = p_paddr_valid;
7351 map->includes_filehdr = 0;
7352 map->includes_phdrs = 0;
7355 while (isec < section_count);
7360 elf_seg_map (obfd) = map_first;
7362 /* If we had to estimate the number of program headers that were
7363 going to be needed, then check our estimate now and adjust
7364 the offset if necessary. */
7365 if (phdr_adjust_seg != NULL)
7369 for (count = 0, map = map_first; map != NULL; map = map->next)
7372 if (count > phdr_adjust_num)
7373 phdr_adjust_seg->p_paddr
7374 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7376 for (map = map_first; map != NULL; map = map->next)
7377 if (map->p_type == PT_PHDR)
7380 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7381 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7388 #undef IS_CONTAINED_BY_VMA
7389 #undef IS_CONTAINED_BY_LMA
7391 #undef IS_COREFILE_NOTE
7392 #undef IS_SOLARIS_PT_INTERP
7393 #undef IS_SECTION_IN_INPUT_SEGMENT
7394 #undef INCLUDE_SECTION_IN_SEGMENT
7395 #undef SEGMENT_AFTER_SEGMENT
7396 #undef SEGMENT_OVERLAPS
7400 /* Copy ELF program header information. */
7403 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7405 Elf_Internal_Ehdr *iehdr;
7406 struct elf_segment_map *map;
7407 struct elf_segment_map *map_first;
7408 struct elf_segment_map **pointer_to_map;
7409 Elf_Internal_Phdr *segment;
7411 unsigned int num_segments;
7412 bfd_boolean phdr_included = FALSE;
7413 bfd_boolean p_paddr_valid;
7415 iehdr = elf_elfheader (ibfd);
7418 pointer_to_map = &map_first;
7420 /* If all the segment p_paddr fields are zero, don't set
7421 map->p_paddr_valid. */
7422 p_paddr_valid = FALSE;
7423 num_segments = elf_elfheader (ibfd)->e_phnum;
7424 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7427 if (segment->p_paddr != 0)
7429 p_paddr_valid = TRUE;
7433 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7438 unsigned int section_count;
7440 Elf_Internal_Shdr *this_hdr;
7441 asection *first_section = NULL;
7442 asection *lowest_section;
7443 bfd_boolean no_contents = TRUE;
7445 /* Compute how many sections are in this segment. */
7446 for (section = ibfd->sections, section_count = 0;
7448 section = section->next)
7450 this_hdr = &(elf_section_data(section)->this_hdr);
7451 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7453 if (first_section == NULL)
7454 first_section = section;
7455 if (elf_section_type (section) != SHT_NOBITS)
7456 no_contents = FALSE;
7461 /* Allocate a segment map big enough to contain
7462 all of the sections we have selected. */
7463 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7464 amt += (bfd_size_type) section_count * sizeof (asection *);
7465 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7469 /* Initialize the fields of the output segment map with the
7472 map->p_type = segment->p_type;
7473 map->p_flags = segment->p_flags;
7474 map->p_flags_valid = 1;
7475 map->p_paddr = segment->p_paddr;
7476 map->p_paddr_valid = p_paddr_valid;
7477 map->p_align = segment->p_align;
7478 map->p_align_valid = 1;
7479 map->p_vaddr_offset = 0;
7481 if (map->p_type == PT_GNU_RELRO
7482 || map->p_type == PT_GNU_STACK)
7484 /* The PT_GNU_RELRO segment may contain the first a few
7485 bytes in the .got.plt section even if the whole .got.plt
7486 section isn't in the PT_GNU_RELRO segment. We won't
7487 change the size of the PT_GNU_RELRO segment.
7488 Similarly, PT_GNU_STACK size is significant on uclinux
7490 map->p_size = segment->p_memsz;
7491 map->p_size_valid = 1;
7494 /* Determine if this segment contains the ELF file header
7495 and if it contains the program headers themselves. */
7496 map->includes_filehdr = (segment->p_offset == 0
7497 && segment->p_filesz >= iehdr->e_ehsize);
7499 map->includes_phdrs = 0;
7500 if (! phdr_included || segment->p_type != PT_LOAD)
7502 map->includes_phdrs =
7503 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7504 && (segment->p_offset + segment->p_filesz
7505 >= ((bfd_vma) iehdr->e_phoff
7506 + iehdr->e_phnum * iehdr->e_phentsize)));
7508 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7509 phdr_included = TRUE;
7512 lowest_section = NULL;
7513 if (section_count != 0)
7515 unsigned int isec = 0;
7517 for (section = first_section;
7519 section = section->next)
7521 this_hdr = &(elf_section_data(section)->this_hdr);
7522 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7524 map->sections[isec++] = section->output_section;
7525 if ((section->flags & SEC_ALLOC) != 0)
7529 if (lowest_section == NULL
7530 || section->lma < lowest_section->lma)
7531 lowest_section = section;
7533 /* Section lmas are set up from PT_LOAD header
7534 p_paddr in _bfd_elf_make_section_from_shdr.
7535 If this header has a p_paddr that disagrees
7536 with the section lma, flag the p_paddr as
7538 if ((section->flags & SEC_LOAD) != 0)
7539 seg_off = this_hdr->sh_offset - segment->p_offset;
7541 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7542 if (section->lma - segment->p_paddr != seg_off)
7543 map->p_paddr_valid = FALSE;
7545 if (isec == section_count)
7551 if (map->includes_filehdr && lowest_section != NULL)
7553 /* Try to keep the space used by the headers plus any
7554 padding fixed. If there are sections with file contents
7555 in this segment then the lowest sh_offset is the best
7556 guess. Otherwise the segment only has file contents for
7557 the headers, and p_filesz is the best guess. */
7559 map->header_size = segment->p_filesz;
7561 map->header_size = lowest_section->filepos;
7564 if (section_count == 0)
7565 map->p_vaddr_offset = segment->p_vaddr;
7566 else if (!map->includes_phdrs
7567 && !map->includes_filehdr
7568 && map->p_paddr_valid)
7569 /* Account for padding before the first section. */
7570 map->p_vaddr_offset = (segment->p_paddr
7571 - (lowest_section ? lowest_section->lma : 0));
7573 map->count = section_count;
7574 *pointer_to_map = map;
7575 pointer_to_map = &map->next;
7578 elf_seg_map (obfd) = map_first;
7582 /* Copy private BFD data. This copies or rewrites ELF program header
7586 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7588 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7589 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7592 if (elf_tdata (ibfd)->phdr == NULL)
7595 if (ibfd->xvec == obfd->xvec)
7597 /* Check to see if any sections in the input BFD
7598 covered by ELF program header have changed. */
7599 Elf_Internal_Phdr *segment;
7600 asection *section, *osec;
7601 unsigned int i, num_segments;
7602 Elf_Internal_Shdr *this_hdr;
7603 const struct elf_backend_data *bed;
7605 bed = get_elf_backend_data (ibfd);
7607 /* Regenerate the segment map if p_paddr is set to 0. */
7608 if (bed->want_p_paddr_set_to_zero)
7611 /* Initialize the segment mark field. */
7612 for (section = obfd->sections; section != NULL;
7613 section = section->next)
7614 section->segment_mark = FALSE;
7616 num_segments = elf_elfheader (ibfd)->e_phnum;
7617 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7621 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7622 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7623 which severly confuses things, so always regenerate the segment
7624 map in this case. */
7625 if (segment->p_paddr == 0
7626 && segment->p_memsz == 0
7627 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7630 for (section = ibfd->sections;
7631 section != NULL; section = section->next)
7633 /* We mark the output section so that we know it comes
7634 from the input BFD. */
7635 osec = section->output_section;
7637 osec->segment_mark = TRUE;
7639 /* Check if this section is covered by the segment. */
7640 this_hdr = &(elf_section_data(section)->this_hdr);
7641 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7643 /* FIXME: Check if its output section is changed or
7644 removed. What else do we need to check? */
7646 || section->flags != osec->flags
7647 || section->lma != osec->lma
7648 || section->vma != osec->vma
7649 || section->size != osec->size
7650 || section->rawsize != osec->rawsize
7651 || section->alignment_power != osec->alignment_power)
7657 /* Check to see if any output section do not come from the
7659 for (section = obfd->sections; section != NULL;
7660 section = section->next)
7662 if (!section->segment_mark)
7665 section->segment_mark = FALSE;
7668 return copy_elf_program_header (ibfd, obfd);
7672 if (ibfd->xvec == obfd->xvec)
7674 /* When rewriting program header, set the output maxpagesize to
7675 the maximum alignment of input PT_LOAD segments. */
7676 Elf_Internal_Phdr *segment;
7678 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7679 bfd_vma maxpagesize = 0;
7681 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7684 if (segment->p_type == PT_LOAD
7685 && maxpagesize < segment->p_align)
7687 /* PR 17512: file: f17299af. */
7688 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7689 /* xgettext:c-format */
7690 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7691 PRIx64 " is too large"),
7692 ibfd, (uint64_t) segment->p_align);
7694 maxpagesize = segment->p_align;
7697 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7698 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7701 return rewrite_elf_program_header (ibfd, obfd);
7704 /* Initialize private output section information from input section. */
7707 _bfd_elf_init_private_section_data (bfd *ibfd,
7711 struct bfd_link_info *link_info)
7714 Elf_Internal_Shdr *ihdr, *ohdr;
7715 bfd_boolean final_link = (link_info != NULL
7716 && !bfd_link_relocatable (link_info));
7718 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7719 || obfd->xvec->flavour != bfd_target_elf_flavour)
7722 BFD_ASSERT (elf_section_data (osec) != NULL);
7724 /* For objcopy and relocatable link, don't copy the output ELF
7725 section type from input if the output BFD section flags have been
7726 set to something different. For a final link allow some flags
7727 that the linker clears to differ. */
7728 if (elf_section_type (osec) == SHT_NULL
7729 && (osec->flags == isec->flags
7731 && ((osec->flags ^ isec->flags)
7732 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7733 elf_section_type (osec) = elf_section_type (isec);
7735 /* FIXME: Is this correct for all OS/PROC specific flags? */
7736 elf_section_flags (osec) |= (elf_section_flags (isec)
7737 & (SHF_MASKOS | SHF_MASKPROC));
7739 /* Copy sh_info from input for mbind section. */
7740 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7741 && elf_section_flags (isec) & SHF_GNU_MBIND)
7742 elf_section_data (osec)->this_hdr.sh_info
7743 = elf_section_data (isec)->this_hdr.sh_info;
7745 /* Set things up for objcopy and relocatable link. The output
7746 SHT_GROUP section will have its elf_next_in_group pointing back
7747 to the input group members. Ignore linker created group section.
7748 See elfNN_ia64_object_p in elfxx-ia64.c. */
7749 if ((link_info == NULL
7750 || !link_info->resolve_section_groups)
7751 && (elf_sec_group (isec) == NULL
7752 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7754 if (elf_section_flags (isec) & SHF_GROUP)
7755 elf_section_flags (osec) |= SHF_GROUP;
7756 elf_next_in_group (osec) = elf_next_in_group (isec);
7757 elf_section_data (osec)->group = elf_section_data (isec)->group;
7760 /* If not decompress, preserve SHF_COMPRESSED. */
7761 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7762 elf_section_flags (osec) |= (elf_section_flags (isec)
7765 ihdr = &elf_section_data (isec)->this_hdr;
7767 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7768 don't use the output section of the linked-to section since it
7769 may be NULL at this point. */
7770 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7772 ohdr = &elf_section_data (osec)->this_hdr;
7773 ohdr->sh_flags |= SHF_LINK_ORDER;
7774 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7777 osec->use_rela_p = isec->use_rela_p;
7782 /* Copy private section information. This copies over the entsize
7783 field, and sometimes the info field. */
7786 _bfd_elf_copy_private_section_data (bfd *ibfd,
7791 Elf_Internal_Shdr *ihdr, *ohdr;
7793 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7794 || obfd->xvec->flavour != bfd_target_elf_flavour)
7797 ihdr = &elf_section_data (isec)->this_hdr;
7798 ohdr = &elf_section_data (osec)->this_hdr;
7800 ohdr->sh_entsize = ihdr->sh_entsize;
7802 if (ihdr->sh_type == SHT_SYMTAB
7803 || ihdr->sh_type == SHT_DYNSYM
7804 || ihdr->sh_type == SHT_GNU_verneed
7805 || ihdr->sh_type == SHT_GNU_verdef)
7806 ohdr->sh_info = ihdr->sh_info;
7808 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7812 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7813 necessary if we are removing either the SHT_GROUP section or any of
7814 the group member sections. DISCARDED is the value that a section's
7815 output_section has if the section will be discarded, NULL when this
7816 function is called from objcopy, bfd_abs_section_ptr when called
7820 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7824 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7825 if (elf_section_type (isec) == SHT_GROUP)
7827 asection *first = elf_next_in_group (isec);
7828 asection *s = first;
7829 bfd_size_type removed = 0;
7833 /* If this member section is being output but the
7834 SHT_GROUP section is not, then clear the group info
7835 set up by _bfd_elf_copy_private_section_data. */
7836 if (s->output_section != discarded
7837 && isec->output_section == discarded)
7839 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7840 elf_group_name (s->output_section) = NULL;
7842 /* Conversely, if the member section is not being output
7843 but the SHT_GROUP section is, then adjust its size. */
7844 else if (s->output_section == discarded
7845 && isec->output_section != discarded)
7847 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7849 if (elf_sec->rel.hdr != NULL
7850 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7852 if (elf_sec->rela.hdr != NULL
7853 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7856 s = elf_next_in_group (s);
7862 if (discarded != NULL)
7864 /* If we've been called for ld -r, then we need to
7865 adjust the input section size. */
7866 if (isec->rawsize == 0)
7867 isec->rawsize = isec->size;
7868 isec->size = isec->rawsize - removed;
7869 if (isec->size <= 4)
7872 isec->flags |= SEC_EXCLUDE;
7877 /* Adjust the output section size when called from
7879 isec->output_section->size -= removed;
7880 if (isec->output_section->size <= 4)
7882 isec->output_section->size = 0;
7883 isec->output_section->flags |= SEC_EXCLUDE;
7892 /* Copy private header information. */
7895 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7897 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7898 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7901 /* Copy over private BFD data if it has not already been copied.
7902 This must be done here, rather than in the copy_private_bfd_data
7903 entry point, because the latter is called after the section
7904 contents have been set, which means that the program headers have
7905 already been worked out. */
7906 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7908 if (! copy_private_bfd_data (ibfd, obfd))
7912 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7915 /* Copy private symbol information. If this symbol is in a section
7916 which we did not map into a BFD section, try to map the section
7917 index correctly. We use special macro definitions for the mapped
7918 section indices; these definitions are interpreted by the
7919 swap_out_syms function. */
7921 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7922 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7923 #define MAP_STRTAB (SHN_HIOS + 3)
7924 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7925 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7928 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7933 elf_symbol_type *isym, *osym;
7935 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7936 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7939 isym = elf_symbol_from (ibfd, isymarg);
7940 osym = elf_symbol_from (obfd, osymarg);
7943 && isym->internal_elf_sym.st_shndx != 0
7945 && bfd_is_abs_section (isym->symbol.section))
7949 shndx = isym->internal_elf_sym.st_shndx;
7950 if (shndx == elf_onesymtab (ibfd))
7951 shndx = MAP_ONESYMTAB;
7952 else if (shndx == elf_dynsymtab (ibfd))
7953 shndx = MAP_DYNSYMTAB;
7954 else if (shndx == elf_strtab_sec (ibfd))
7956 else if (shndx == elf_shstrtab_sec (ibfd))
7957 shndx = MAP_SHSTRTAB;
7958 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7959 shndx = MAP_SYM_SHNDX;
7960 osym->internal_elf_sym.st_shndx = shndx;
7966 /* Swap out the symbols. */
7969 swap_out_syms (bfd *abfd,
7970 struct elf_strtab_hash **sttp,
7973 const struct elf_backend_data *bed;
7976 struct elf_strtab_hash *stt;
7977 Elf_Internal_Shdr *symtab_hdr;
7978 Elf_Internal_Shdr *symtab_shndx_hdr;
7979 Elf_Internal_Shdr *symstrtab_hdr;
7980 struct elf_sym_strtab *symstrtab;
7981 bfd_byte *outbound_syms;
7982 bfd_byte *outbound_shndx;
7983 unsigned long outbound_syms_index;
7984 unsigned long outbound_shndx_index;
7986 unsigned int num_locals;
7988 bfd_boolean name_local_sections;
7990 if (!elf_map_symbols (abfd, &num_locals))
7993 /* Dump out the symtabs. */
7994 stt = _bfd_elf_strtab_init ();
7998 bed = get_elf_backend_data (abfd);
7999 symcount = bfd_get_symcount (abfd);
8000 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8001 symtab_hdr->sh_type = SHT_SYMTAB;
8002 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8003 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8004 symtab_hdr->sh_info = num_locals + 1;
8005 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8007 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8008 symstrtab_hdr->sh_type = SHT_STRTAB;
8010 /* Allocate buffer to swap out the .strtab section. */
8011 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
8012 sizeof (*symstrtab));
8013 if (symstrtab == NULL)
8015 _bfd_elf_strtab_free (stt);
8019 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
8020 bed->s->sizeof_sym);
8021 if (outbound_syms == NULL)
8024 _bfd_elf_strtab_free (stt);
8028 symtab_hdr->contents = outbound_syms;
8029 outbound_syms_index = 0;
8031 outbound_shndx = NULL;
8032 outbound_shndx_index = 0;
8034 if (elf_symtab_shndx_list (abfd))
8036 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8037 if (symtab_shndx_hdr->sh_name != 0)
8039 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
8040 outbound_shndx = (bfd_byte *)
8041 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
8042 if (outbound_shndx == NULL)
8045 symtab_shndx_hdr->contents = outbound_shndx;
8046 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8047 symtab_shndx_hdr->sh_size = amt;
8048 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8049 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8051 /* FIXME: What about any other headers in the list ? */
8054 /* Now generate the data (for "contents"). */
8056 /* Fill in zeroth symbol and swap it out. */
8057 Elf_Internal_Sym sym;
8063 sym.st_shndx = SHN_UNDEF;
8064 sym.st_target_internal = 0;
8065 symstrtab[0].sym = sym;
8066 symstrtab[0].dest_index = outbound_syms_index;
8067 symstrtab[0].destshndx_index = outbound_shndx_index;
8068 outbound_syms_index++;
8069 if (outbound_shndx != NULL)
8070 outbound_shndx_index++;
8074 = (bed->elf_backend_name_local_section_symbols
8075 && bed->elf_backend_name_local_section_symbols (abfd));
8077 syms = bfd_get_outsymbols (abfd);
8078 for (idx = 0; idx < symcount;)
8080 Elf_Internal_Sym sym;
8081 bfd_vma value = syms[idx]->value;
8082 elf_symbol_type *type_ptr;
8083 flagword flags = syms[idx]->flags;
8086 if (!name_local_sections
8087 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8089 /* Local section symbols have no name. */
8090 sym.st_name = (unsigned long) -1;
8094 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8095 to get the final offset for st_name. */
8097 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8099 if (sym.st_name == (unsigned long) -1)
8103 type_ptr = elf_symbol_from (abfd, syms[idx]);
8105 if ((flags & BSF_SECTION_SYM) == 0
8106 && bfd_is_com_section (syms[idx]->section))
8108 /* ELF common symbols put the alignment into the `value' field,
8109 and the size into the `size' field. This is backwards from
8110 how BFD handles it, so reverse it here. */
8111 sym.st_size = value;
8112 if (type_ptr == NULL
8113 || type_ptr->internal_elf_sym.st_value == 0)
8114 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8116 sym.st_value = type_ptr->internal_elf_sym.st_value;
8117 sym.st_shndx = _bfd_elf_section_from_bfd_section
8118 (abfd, syms[idx]->section);
8122 asection *sec = syms[idx]->section;
8125 if (sec->output_section)
8127 value += sec->output_offset;
8128 sec = sec->output_section;
8131 /* Don't add in the section vma for relocatable output. */
8132 if (! relocatable_p)
8134 sym.st_value = value;
8135 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8137 if (bfd_is_abs_section (sec)
8139 && type_ptr->internal_elf_sym.st_shndx != 0)
8141 /* This symbol is in a real ELF section which we did
8142 not create as a BFD section. Undo the mapping done
8143 by copy_private_symbol_data. */
8144 shndx = type_ptr->internal_elf_sym.st_shndx;
8148 shndx = elf_onesymtab (abfd);
8151 shndx = elf_dynsymtab (abfd);
8154 shndx = elf_strtab_sec (abfd);
8157 shndx = elf_shstrtab_sec (abfd);
8160 if (elf_symtab_shndx_list (abfd))
8161 shndx = elf_symtab_shndx_list (abfd)->ndx;
8170 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8172 if (shndx == SHN_BAD)
8176 /* Writing this would be a hell of a lot easier if
8177 we had some decent documentation on bfd, and
8178 knew what to expect of the library, and what to
8179 demand of applications. For example, it
8180 appears that `objcopy' might not set the
8181 section of a symbol to be a section that is
8182 actually in the output file. */
8183 sec2 = bfd_get_section_by_name (abfd, sec->name);
8185 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8186 if (shndx == SHN_BAD)
8188 /* xgettext:c-format */
8190 (_("unable to find equivalent output section"
8191 " for symbol '%s' from section '%s'"),
8192 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8194 bfd_set_error (bfd_error_invalid_operation);
8200 sym.st_shndx = shndx;
8203 if ((flags & BSF_THREAD_LOCAL) != 0)
8205 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8206 type = STT_GNU_IFUNC;
8207 else if ((flags & BSF_FUNCTION) != 0)
8209 else if ((flags & BSF_OBJECT) != 0)
8211 else if ((flags & BSF_RELC) != 0)
8213 else if ((flags & BSF_SRELC) != 0)
8218 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8221 /* Processor-specific types. */
8222 if (type_ptr != NULL
8223 && bed->elf_backend_get_symbol_type)
8224 type = ((*bed->elf_backend_get_symbol_type)
8225 (&type_ptr->internal_elf_sym, type));
8227 if (flags & BSF_SECTION_SYM)
8229 if (flags & BSF_GLOBAL)
8230 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8232 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8234 else if (bfd_is_com_section (syms[idx]->section))
8236 if (type != STT_TLS)
8238 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8239 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8240 ? STT_COMMON : STT_OBJECT);
8242 type = ((flags & BSF_ELF_COMMON) != 0
8243 ? STT_COMMON : STT_OBJECT);
8245 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8247 else if (bfd_is_und_section (syms[idx]->section))
8248 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8252 else if (flags & BSF_FILE)
8253 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8256 int bind = STB_LOCAL;
8258 if (flags & BSF_LOCAL)
8260 else if (flags & BSF_GNU_UNIQUE)
8261 bind = STB_GNU_UNIQUE;
8262 else if (flags & BSF_WEAK)
8264 else if (flags & BSF_GLOBAL)
8267 sym.st_info = ELF_ST_INFO (bind, type);
8270 if (type_ptr != NULL)
8272 sym.st_other = type_ptr->internal_elf_sym.st_other;
8273 sym.st_target_internal
8274 = type_ptr->internal_elf_sym.st_target_internal;
8279 sym.st_target_internal = 0;
8283 symstrtab[idx].sym = sym;
8284 symstrtab[idx].dest_index = outbound_syms_index;
8285 symstrtab[idx].destshndx_index = outbound_shndx_index;
8287 outbound_syms_index++;
8288 if (outbound_shndx != NULL)
8289 outbound_shndx_index++;
8292 /* Finalize the .strtab section. */
8293 _bfd_elf_strtab_finalize (stt);
8295 /* Swap out the .strtab section. */
8296 for (idx = 0; idx <= symcount; idx++)
8298 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8299 if (elfsym->sym.st_name == (unsigned long) -1)
8300 elfsym->sym.st_name = 0;
8302 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8303 elfsym->sym.st_name);
8304 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8306 + (elfsym->dest_index
8307 * bed->s->sizeof_sym)),
8309 + (elfsym->destshndx_index
8310 * sizeof (Elf_External_Sym_Shndx))));
8315 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8316 symstrtab_hdr->sh_type = SHT_STRTAB;
8317 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8318 symstrtab_hdr->sh_addr = 0;
8319 symstrtab_hdr->sh_entsize = 0;
8320 symstrtab_hdr->sh_link = 0;
8321 symstrtab_hdr->sh_info = 0;
8322 symstrtab_hdr->sh_addralign = 1;
8327 /* Return the number of bytes required to hold the symtab vector.
8329 Note that we base it on the count plus 1, since we will null terminate
8330 the vector allocated based on this size. However, the ELF symbol table
8331 always has a dummy entry as symbol #0, so it ends up even. */
8334 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8336 bfd_size_type symcount;
8338 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8340 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8341 if (symcount >= LONG_MAX / sizeof (asymbol *))
8343 bfd_set_error (bfd_error_file_too_big);
8346 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8348 symtab_size -= sizeof (asymbol *);
8354 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8356 bfd_size_type symcount;
8358 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8360 if (elf_dynsymtab (abfd) == 0)
8362 bfd_set_error (bfd_error_invalid_operation);
8366 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8367 if (symcount >= LONG_MAX / sizeof (asymbol *))
8369 bfd_set_error (bfd_error_file_too_big);
8372 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8374 symtab_size -= sizeof (asymbol *);
8380 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8383 #if SIZEOF_LONG == SIZEOF_INT
8384 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8386 bfd_set_error (bfd_error_file_too_big);
8390 return (asect->reloc_count + 1) * sizeof (arelent *);
8393 /* Canonicalize the relocs. */
8396 _bfd_elf_canonicalize_reloc (bfd *abfd,
8403 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8405 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8408 tblptr = section->relocation;
8409 for (i = 0; i < section->reloc_count; i++)
8410 *relptr++ = tblptr++;
8414 return section->reloc_count;
8418 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8420 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8421 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8424 abfd->symcount = symcount;
8429 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8430 asymbol **allocation)
8432 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8433 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8436 abfd->dynsymcount = symcount;
8440 /* Return the size required for the dynamic reloc entries. Any loadable
8441 section that was actually installed in the BFD, and has type SHT_REL
8442 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8443 dynamic reloc section. */
8446 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8448 bfd_size_type count;
8451 if (elf_dynsymtab (abfd) == 0)
8453 bfd_set_error (bfd_error_invalid_operation);
8458 for (s = abfd->sections; s != NULL; s = s->next)
8459 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8460 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8461 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8463 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8464 if (count > LONG_MAX / sizeof (arelent *))
8466 bfd_set_error (bfd_error_file_too_big);
8470 return count * sizeof (arelent *);
8473 /* Canonicalize the dynamic relocation entries. Note that we return the
8474 dynamic relocations as a single block, although they are actually
8475 associated with particular sections; the interface, which was
8476 designed for SunOS style shared libraries, expects that there is only
8477 one set of dynamic relocs. Any loadable section that was actually
8478 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8479 dynamic symbol table, is considered to be a dynamic reloc section. */
8482 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8486 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8490 if (elf_dynsymtab (abfd) == 0)
8492 bfd_set_error (bfd_error_invalid_operation);
8496 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8498 for (s = abfd->sections; s != NULL; s = s->next)
8500 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8501 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8502 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8507 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8509 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8511 for (i = 0; i < count; i++)
8522 /* Read in the version information. */
8525 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8527 bfd_byte *contents = NULL;
8528 unsigned int freeidx = 0;
8530 if (elf_dynverref (abfd) != 0)
8532 Elf_Internal_Shdr *hdr;
8533 Elf_External_Verneed *everneed;
8534 Elf_Internal_Verneed *iverneed;
8536 bfd_byte *contents_end;
8538 hdr = &elf_tdata (abfd)->dynverref_hdr;
8540 if (hdr->sh_info == 0
8541 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8543 error_return_bad_verref:
8545 (_("%pB: .gnu.version_r invalid entry"), abfd);
8546 bfd_set_error (bfd_error_bad_value);
8547 error_return_verref:
8548 elf_tdata (abfd)->verref = NULL;
8549 elf_tdata (abfd)->cverrefs = 0;
8553 ufile_ptr filesize = bfd_get_file_size (abfd);
8554 if (filesize > 0 && filesize < hdr->sh_size)
8556 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8558 bfd_set_error (bfd_error_no_memory);
8560 /* xgettext:c-format */
8561 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8562 abfd, (uint64_t) hdr->sh_size);
8563 goto error_return_verref;
8565 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8566 if (contents == NULL)
8567 goto error_return_verref;
8569 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8570 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8571 goto error_return_verref;
8573 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8574 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8576 if (elf_tdata (abfd)->verref == NULL)
8577 goto error_return_verref;
8579 BFD_ASSERT (sizeof (Elf_External_Verneed)
8580 == sizeof (Elf_External_Vernaux));
8581 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8582 everneed = (Elf_External_Verneed *) contents;
8583 iverneed = elf_tdata (abfd)->verref;
8584 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8586 Elf_External_Vernaux *evernaux;
8587 Elf_Internal_Vernaux *ivernaux;
8590 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8592 iverneed->vn_bfd = abfd;
8594 iverneed->vn_filename =
8595 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8597 if (iverneed->vn_filename == NULL)
8598 goto error_return_bad_verref;
8600 if (iverneed->vn_cnt == 0)
8601 iverneed->vn_auxptr = NULL;
8604 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8605 bfd_alloc2 (abfd, iverneed->vn_cnt,
8606 sizeof (Elf_Internal_Vernaux));
8607 if (iverneed->vn_auxptr == NULL)
8608 goto error_return_verref;
8611 if (iverneed->vn_aux
8612 > (size_t) (contents_end - (bfd_byte *) everneed))
8613 goto error_return_bad_verref;
8615 evernaux = ((Elf_External_Vernaux *)
8616 ((bfd_byte *) everneed + iverneed->vn_aux));
8617 ivernaux = iverneed->vn_auxptr;
8618 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8620 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8622 ivernaux->vna_nodename =
8623 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8624 ivernaux->vna_name);
8625 if (ivernaux->vna_nodename == NULL)
8626 goto error_return_bad_verref;
8628 if (ivernaux->vna_other > freeidx)
8629 freeidx = ivernaux->vna_other;
8631 ivernaux->vna_nextptr = NULL;
8632 if (ivernaux->vna_next == 0)
8634 iverneed->vn_cnt = j + 1;
8637 if (j + 1 < iverneed->vn_cnt)
8638 ivernaux->vna_nextptr = ivernaux + 1;
8640 if (ivernaux->vna_next
8641 > (size_t) (contents_end - (bfd_byte *) evernaux))
8642 goto error_return_bad_verref;
8644 evernaux = ((Elf_External_Vernaux *)
8645 ((bfd_byte *) evernaux + ivernaux->vna_next));
8648 iverneed->vn_nextref = NULL;
8649 if (iverneed->vn_next == 0)
8651 if (i + 1 < hdr->sh_info)
8652 iverneed->vn_nextref = iverneed + 1;
8654 if (iverneed->vn_next
8655 > (size_t) (contents_end - (bfd_byte *) everneed))
8656 goto error_return_bad_verref;
8658 everneed = ((Elf_External_Verneed *)
8659 ((bfd_byte *) everneed + iverneed->vn_next));
8661 elf_tdata (abfd)->cverrefs = i;
8667 if (elf_dynverdef (abfd) != 0)
8669 Elf_Internal_Shdr *hdr;
8670 Elf_External_Verdef *everdef;
8671 Elf_Internal_Verdef *iverdef;
8672 Elf_Internal_Verdef *iverdefarr;
8673 Elf_Internal_Verdef iverdefmem;
8675 unsigned int maxidx;
8676 bfd_byte *contents_end_def, *contents_end_aux;
8678 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8680 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8682 error_return_bad_verdef:
8684 (_("%pB: .gnu.version_d invalid entry"), abfd);
8685 bfd_set_error (bfd_error_bad_value);
8686 error_return_verdef:
8687 elf_tdata (abfd)->verdef = NULL;
8688 elf_tdata (abfd)->cverdefs = 0;
8692 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8693 if (contents == NULL)
8694 goto error_return_verdef;
8695 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8696 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8697 goto error_return_verdef;
8699 BFD_ASSERT (sizeof (Elf_External_Verdef)
8700 >= sizeof (Elf_External_Verdaux));
8701 contents_end_def = contents + hdr->sh_size
8702 - sizeof (Elf_External_Verdef);
8703 contents_end_aux = contents + hdr->sh_size
8704 - sizeof (Elf_External_Verdaux);
8706 /* We know the number of entries in the section but not the maximum
8707 index. Therefore we have to run through all entries and find
8709 everdef = (Elf_External_Verdef *) contents;
8711 for (i = 0; i < hdr->sh_info; ++i)
8713 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8715 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8716 goto error_return_bad_verdef;
8717 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8718 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8720 if (iverdefmem.vd_next == 0)
8723 if (iverdefmem.vd_next
8724 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8725 goto error_return_bad_verdef;
8727 everdef = ((Elf_External_Verdef *)
8728 ((bfd_byte *) everdef + iverdefmem.vd_next));
8731 if (default_imported_symver)
8733 if (freeidx > maxidx)
8739 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8740 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8741 if (elf_tdata (abfd)->verdef == NULL)
8742 goto error_return_verdef;
8744 elf_tdata (abfd)->cverdefs = maxidx;
8746 everdef = (Elf_External_Verdef *) contents;
8747 iverdefarr = elf_tdata (abfd)->verdef;
8748 for (i = 0; i < hdr->sh_info; i++)
8750 Elf_External_Verdaux *everdaux;
8751 Elf_Internal_Verdaux *iverdaux;
8754 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8756 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8757 goto error_return_bad_verdef;
8759 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8760 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8762 iverdef->vd_bfd = abfd;
8764 if (iverdef->vd_cnt == 0)
8765 iverdef->vd_auxptr = NULL;
8768 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8769 bfd_alloc2 (abfd, iverdef->vd_cnt,
8770 sizeof (Elf_Internal_Verdaux));
8771 if (iverdef->vd_auxptr == NULL)
8772 goto error_return_verdef;
8776 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8777 goto error_return_bad_verdef;
8779 everdaux = ((Elf_External_Verdaux *)
8780 ((bfd_byte *) everdef + iverdef->vd_aux));
8781 iverdaux = iverdef->vd_auxptr;
8782 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8784 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8786 iverdaux->vda_nodename =
8787 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8788 iverdaux->vda_name);
8789 if (iverdaux->vda_nodename == NULL)
8790 goto error_return_bad_verdef;
8792 iverdaux->vda_nextptr = NULL;
8793 if (iverdaux->vda_next == 0)
8795 iverdef->vd_cnt = j + 1;
8798 if (j + 1 < iverdef->vd_cnt)
8799 iverdaux->vda_nextptr = iverdaux + 1;
8801 if (iverdaux->vda_next
8802 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8803 goto error_return_bad_verdef;
8805 everdaux = ((Elf_External_Verdaux *)
8806 ((bfd_byte *) everdaux + iverdaux->vda_next));
8809 iverdef->vd_nodename = NULL;
8810 if (iverdef->vd_cnt)
8811 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8813 iverdef->vd_nextdef = NULL;
8814 if (iverdef->vd_next == 0)
8816 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8817 iverdef->vd_nextdef = iverdef + 1;
8819 everdef = ((Elf_External_Verdef *)
8820 ((bfd_byte *) everdef + iverdef->vd_next));
8826 else if (default_imported_symver)
8833 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8834 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8835 if (elf_tdata (abfd)->verdef == NULL)
8838 elf_tdata (abfd)->cverdefs = freeidx;
8841 /* Create a default version based on the soname. */
8842 if (default_imported_symver)
8844 Elf_Internal_Verdef *iverdef;
8845 Elf_Internal_Verdaux *iverdaux;
8847 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8849 iverdef->vd_version = VER_DEF_CURRENT;
8850 iverdef->vd_flags = 0;
8851 iverdef->vd_ndx = freeidx;
8852 iverdef->vd_cnt = 1;
8854 iverdef->vd_bfd = abfd;
8856 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8857 if (iverdef->vd_nodename == NULL)
8858 goto error_return_verdef;
8859 iverdef->vd_nextdef = NULL;
8860 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8861 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8862 if (iverdef->vd_auxptr == NULL)
8863 goto error_return_verdef;
8865 iverdaux = iverdef->vd_auxptr;
8866 iverdaux->vda_nodename = iverdef->vd_nodename;
8872 if (contents != NULL)
8878 _bfd_elf_make_empty_symbol (bfd *abfd)
8880 elf_symbol_type *newsym;
8882 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
8885 newsym->symbol.the_bfd = abfd;
8886 return &newsym->symbol;
8890 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8894 bfd_symbol_info (symbol, ret);
8897 /* Return whether a symbol name implies a local symbol. Most targets
8898 use this function for the is_local_label_name entry point, but some
8902 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8905 /* Normal local symbols start with ``.L''. */
8906 if (name[0] == '.' && name[1] == 'L')
8909 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8910 DWARF debugging symbols starting with ``..''. */
8911 if (name[0] == '.' && name[1] == '.')
8914 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8915 emitting DWARF debugging output. I suspect this is actually a
8916 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8917 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8918 underscore to be emitted on some ELF targets). For ease of use,
8919 we treat such symbols as local. */
8920 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8923 /* Treat assembler generated fake symbols, dollar local labels and
8924 forward-backward labels (aka local labels) as locals.
8925 These labels have the form:
8927 L0^A.* (fake symbols)
8929 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8931 Versions which start with .L will have already been matched above,
8932 so we only need to match the rest. */
8933 if (name[0] == 'L' && ISDIGIT (name[1]))
8935 bfd_boolean ret = FALSE;
8939 for (p = name + 2; (c = *p); p++)
8941 if (c == 1 || c == 2)
8943 if (c == 1 && p == name + 2)
8944 /* A fake symbol. */
8947 /* FIXME: We are being paranoid here and treating symbols like
8948 L0^Bfoo as if there were non-local, on the grounds that the
8949 assembler will never generate them. But can any symbol
8950 containing an ASCII value in the range 1-31 ever be anything
8951 other than some kind of local ? */
8968 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8969 asymbol *symbol ATTRIBUTE_UNUSED)
8976 _bfd_elf_set_arch_mach (bfd *abfd,
8977 enum bfd_architecture arch,
8978 unsigned long machine)
8980 /* If this isn't the right architecture for this backend, and this
8981 isn't the generic backend, fail. */
8982 if (arch != get_elf_backend_data (abfd)->arch
8983 && arch != bfd_arch_unknown
8984 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8987 return bfd_default_set_arch_mach (abfd, arch, machine);
8990 /* Find the nearest line to a particular section and offset,
8991 for error reporting. */
8994 _bfd_elf_find_nearest_line (bfd *abfd,
8998 const char **filename_ptr,
8999 const char **functionname_ptr,
9000 unsigned int *line_ptr,
9001 unsigned int *discriminator_ptr)
9005 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
9006 filename_ptr, functionname_ptr,
9007 line_ptr, discriminator_ptr,
9008 dwarf_debug_sections,
9009 &elf_tdata (abfd)->dwarf2_find_line_info)
9010 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9011 filename_ptr, functionname_ptr,
9014 if (!*functionname_ptr)
9015 _bfd_elf_find_function (abfd, symbols, section, offset,
9016 *filename_ptr ? NULL : filename_ptr,
9021 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9022 &found, filename_ptr,
9023 functionname_ptr, line_ptr,
9024 &elf_tdata (abfd)->line_info))
9026 if (found && (*functionname_ptr || *line_ptr))
9029 if (symbols == NULL)
9032 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9033 filename_ptr, functionname_ptr))
9040 /* Find the line for a symbol. */
9043 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9044 const char **filename_ptr, unsigned int *line_ptr)
9046 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9047 filename_ptr, NULL, line_ptr, NULL,
9048 dwarf_debug_sections,
9049 &elf_tdata (abfd)->dwarf2_find_line_info);
9052 /* After a call to bfd_find_nearest_line, successive calls to
9053 bfd_find_inliner_info can be used to get source information about
9054 each level of function inlining that terminated at the address
9055 passed to bfd_find_nearest_line. Currently this is only supported
9056 for DWARF2 with appropriate DWARF3 extensions. */
9059 _bfd_elf_find_inliner_info (bfd *abfd,
9060 const char **filename_ptr,
9061 const char **functionname_ptr,
9062 unsigned int *line_ptr)
9065 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9066 functionname_ptr, line_ptr,
9067 & elf_tdata (abfd)->dwarf2_find_line_info);
9072 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9074 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9075 int ret = bed->s->sizeof_ehdr;
9077 if (!bfd_link_relocatable (info))
9079 bfd_size_type phdr_size = elf_program_header_size (abfd);
9081 if (phdr_size == (bfd_size_type) -1)
9083 struct elf_segment_map *m;
9086 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9087 phdr_size += bed->s->sizeof_phdr;
9090 phdr_size = get_program_header_size (abfd, info);
9093 elf_program_header_size (abfd) = phdr_size;
9101 _bfd_elf_set_section_contents (bfd *abfd,
9103 const void *location,
9105 bfd_size_type count)
9107 Elf_Internal_Shdr *hdr;
9110 if (! abfd->output_has_begun
9111 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9117 hdr = &elf_section_data (section)->this_hdr;
9118 if (hdr->sh_offset == (file_ptr) -1)
9120 if (bfd_section_is_ctf (section))
9121 /* Nothing to do with this section: the contents are generated
9125 /* We must compress this section. Write output to the buffer. */
9126 unsigned char *contents = hdr->contents;
9127 if ((offset + count) > hdr->sh_size
9128 || (section->flags & SEC_ELF_COMPRESS) == 0
9129 || contents == NULL)
9131 memcpy (contents + offset, location, count);
9134 pos = hdr->sh_offset + offset;
9135 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9136 || bfd_bwrite (location, count, abfd) != count)
9143 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9144 arelent *cache_ptr ATTRIBUTE_UNUSED,
9145 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9151 /* Try to convert a non-ELF reloc into an ELF one. */
9154 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9156 /* Check whether we really have an ELF howto. */
9158 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9160 bfd_reloc_code_real_type code;
9161 reloc_howto_type *howto;
9163 /* Alien reloc: Try to determine its type to replace it with an
9164 equivalent ELF reloc. */
9166 if (areloc->howto->pc_relative)
9168 switch (areloc->howto->bitsize)
9171 code = BFD_RELOC_8_PCREL;
9174 code = BFD_RELOC_12_PCREL;
9177 code = BFD_RELOC_16_PCREL;
9180 code = BFD_RELOC_24_PCREL;
9183 code = BFD_RELOC_32_PCREL;
9186 code = BFD_RELOC_64_PCREL;
9192 howto = bfd_reloc_type_lookup (abfd, code);
9194 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9196 if (howto->pcrel_offset)
9197 areloc->addend += areloc->address;
9199 areloc->addend -= areloc->address; /* addend is unsigned!! */
9204 switch (areloc->howto->bitsize)
9210 code = BFD_RELOC_14;
9213 code = BFD_RELOC_16;
9216 code = BFD_RELOC_26;
9219 code = BFD_RELOC_32;
9222 code = BFD_RELOC_64;
9228 howto = bfd_reloc_type_lookup (abfd, code);
9232 areloc->howto = howto;
9240 /* xgettext:c-format */
9241 _bfd_error_handler (_("%pB: %s unsupported"),
9242 abfd, areloc->howto->name);
9243 bfd_set_error (bfd_error_bad_value);
9248 _bfd_elf_close_and_cleanup (bfd *abfd)
9250 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9251 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
9253 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9254 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9255 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9258 return _bfd_generic_close_and_cleanup (abfd);
9261 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9262 in the relocation's offset. Thus we cannot allow any sort of sanity
9263 range-checking to interfere. There is nothing else to do in processing
9266 bfd_reloc_status_type
9267 _bfd_elf_rel_vtable_reloc_fn
9268 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9269 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9270 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9271 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9273 return bfd_reloc_ok;
9276 /* Elf core file support. Much of this only works on native
9277 toolchains, since we rely on knowing the
9278 machine-dependent procfs structure in order to pick
9279 out details about the corefile. */
9281 #ifdef HAVE_SYS_PROCFS_H
9282 /* Needed for new procfs interface on sparc-solaris. */
9283 # define _STRUCTURED_PROC 1
9284 # include <sys/procfs.h>
9287 /* Return a PID that identifies a "thread" for threaded cores, or the
9288 PID of the main process for non-threaded cores. */
9291 elfcore_make_pid (bfd *abfd)
9295 pid = elf_tdata (abfd)->core->lwpid;
9297 pid = elf_tdata (abfd)->core->pid;
9302 /* If there isn't a section called NAME, make one, using
9303 data from SECT. Note, this function will generate a
9304 reference to NAME, so you shouldn't deallocate or
9308 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9312 if (bfd_get_section_by_name (abfd, name) != NULL)
9315 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9319 sect2->size = sect->size;
9320 sect2->filepos = sect->filepos;
9321 sect2->alignment_power = sect->alignment_power;
9325 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9326 actually creates up to two pseudosections:
9327 - For the single-threaded case, a section named NAME, unless
9328 such a section already exists.
9329 - For the multi-threaded case, a section named "NAME/PID", where
9330 PID is elfcore_make_pid (abfd).
9331 Both pseudosections have identical contents. */
9333 _bfd_elfcore_make_pseudosection (bfd *abfd,
9339 char *threaded_name;
9343 /* Build the section name. */
9345 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9346 len = strlen (buf) + 1;
9347 threaded_name = (char *) bfd_alloc (abfd, len);
9348 if (threaded_name == NULL)
9350 memcpy (threaded_name, buf, len);
9352 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9357 sect->filepos = filepos;
9358 sect->alignment_power = 2;
9360 return elfcore_maybe_make_sect (abfd, name, sect);
9364 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9367 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9373 sect->size = note->descsz - offs;
9374 sect->filepos = note->descpos + offs;
9375 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9380 /* prstatus_t exists on:
9382 linux 2.[01] + glibc
9386 #if defined (HAVE_PRSTATUS_T)
9389 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9394 if (note->descsz == sizeof (prstatus_t))
9398 size = sizeof (prstat.pr_reg);
9399 offset = offsetof (prstatus_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_PRSTATUS_T_PR_WHO)
9416 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9418 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9421 #if defined (HAVE_PRSTATUS32_T)
9422 else if (note->descsz == sizeof (prstatus32_t))
9424 /* 64-bit host, 32-bit corefile */
9425 prstatus32_t prstat;
9427 size = sizeof (prstat.pr_reg);
9428 offset = offsetof (prstatus32_t, pr_reg);
9429 memcpy (&prstat, note->descdata, sizeof (prstat));
9431 /* Do not overwrite the core signal if it
9432 has already been set by another thread. */
9433 if (elf_tdata (abfd)->core->signal == 0)
9434 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9435 if (elf_tdata (abfd)->core->pid == 0)
9436 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9438 /* pr_who exists on:
9441 pr_who doesn't exist on:
9444 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9445 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9447 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9450 #endif /* HAVE_PRSTATUS32_T */
9453 /* Fail - we don't know how to handle any other
9454 note size (ie. data object type). */
9458 /* Make a ".reg/999" section and a ".reg" section. */
9459 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9460 size, note->descpos + offset);
9462 #endif /* defined (HAVE_PRSTATUS_T) */
9464 /* Create a pseudosection containing the exact contents of NOTE. */
9466 elfcore_make_note_pseudosection (bfd *abfd,
9468 Elf_Internal_Note *note)
9470 return _bfd_elfcore_make_pseudosection (abfd, name,
9471 note->descsz, note->descpos);
9474 /* There isn't a consistent prfpregset_t across platforms,
9475 but it doesn't matter, because we don't have to pick this
9476 data structure apart. */
9479 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9481 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9484 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9485 type of NT_PRXFPREG. Just include the whole note's contents
9489 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9491 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9494 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9495 with a note type of NT_X86_XSTATE. Just include the whole note's
9496 contents literally. */
9499 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9501 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9505 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9507 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9511 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9513 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9517 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9519 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9523 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9525 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9529 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9531 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9535 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9537 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9541 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9543 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9547 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9549 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9553 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9555 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9559 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9561 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9565 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9567 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9571 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9573 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9577 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9579 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9583 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9585 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9589 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9591 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9595 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9597 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9601 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9603 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9607 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9609 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9613 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9615 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9619 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9621 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9625 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9627 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9631 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9633 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9637 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9639 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9643 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9645 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9649 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9651 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9655 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9657 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9661 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9663 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9667 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9669 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9673 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9675 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9679 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9681 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9685 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9687 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9691 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9693 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9697 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9699 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9703 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9705 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9708 #if defined (HAVE_PRPSINFO_T)
9709 typedef prpsinfo_t elfcore_psinfo_t;
9710 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9711 typedef prpsinfo32_t elfcore_psinfo32_t;
9715 #if defined (HAVE_PSINFO_T)
9716 typedef psinfo_t elfcore_psinfo_t;
9717 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9718 typedef psinfo32_t elfcore_psinfo32_t;
9722 /* return a malloc'ed copy of a string at START which is at
9723 most MAX bytes long, possibly without a terminating '\0'.
9724 the copy will always have a terminating '\0'. */
9727 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9730 char *end = (char *) memchr (start, '\0', max);
9738 dups = (char *) bfd_alloc (abfd, len + 1);
9742 memcpy (dups, start, len);
9748 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9750 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9752 if (note->descsz == sizeof (elfcore_psinfo_t))
9754 elfcore_psinfo_t psinfo;
9756 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9758 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9759 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9761 elf_tdata (abfd)->core->program
9762 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9763 sizeof (psinfo.pr_fname));
9765 elf_tdata (abfd)->core->command
9766 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9767 sizeof (psinfo.pr_psargs));
9769 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9770 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9772 /* 64-bit host, 32-bit corefile */
9773 elfcore_psinfo32_t psinfo;
9775 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9777 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9778 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9780 elf_tdata (abfd)->core->program
9781 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9782 sizeof (psinfo.pr_fname));
9784 elf_tdata (abfd)->core->command
9785 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9786 sizeof (psinfo.pr_psargs));
9792 /* Fail - we don't know how to handle any other
9793 note size (ie. data object type). */
9797 /* Note that for some reason, a spurious space is tacked
9798 onto the end of the args in some (at least one anyway)
9799 implementations, so strip it off if it exists. */
9802 char *command = elf_tdata (abfd)->core->command;
9803 int n = strlen (command);
9805 if (0 < n && command[n - 1] == ' ')
9806 command[n - 1] = '\0';
9811 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9813 #if defined (HAVE_PSTATUS_T)
9815 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9817 if (note->descsz == sizeof (pstatus_t)
9818 #if defined (HAVE_PXSTATUS_T)
9819 || note->descsz == sizeof (pxstatus_t)
9825 memcpy (&pstat, note->descdata, sizeof (pstat));
9827 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9829 #if defined (HAVE_PSTATUS32_T)
9830 else if (note->descsz == sizeof (pstatus32_t))
9832 /* 64-bit host, 32-bit corefile */
9835 memcpy (&pstat, note->descdata, sizeof (pstat));
9837 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9840 /* Could grab some more details from the "representative"
9841 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9842 NT_LWPSTATUS note, presumably. */
9846 #endif /* defined (HAVE_PSTATUS_T) */
9848 #if defined (HAVE_LWPSTATUS_T)
9850 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9852 lwpstatus_t lwpstat;
9858 if (note->descsz != sizeof (lwpstat)
9859 #if defined (HAVE_LWPXSTATUS_T)
9860 && note->descsz != sizeof (lwpxstatus_t)
9865 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9867 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9868 /* Do not overwrite the core signal if it has already been set by
9870 if (elf_tdata (abfd)->core->signal == 0)
9871 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9873 /* Make a ".reg/999" section. */
9875 sprintf (buf, ".reg/%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.gregs);
9888 sect->filepos = note->descpos
9889 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9892 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9893 sect->size = sizeof (lwpstat.pr_reg);
9894 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9897 sect->alignment_power = 2;
9899 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9902 /* Make a ".reg2/999" section */
9904 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9905 len = strlen (buf) + 1;
9906 name = bfd_alloc (abfd, len);
9909 memcpy (name, buf, len);
9911 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9915 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9916 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9917 sect->filepos = note->descpos
9918 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9921 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9922 sect->size = sizeof (lwpstat.pr_fpreg);
9923 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9926 sect->alignment_power = 2;
9928 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9930 #endif /* defined (HAVE_LWPSTATUS_T) */
9933 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9940 int is_active_thread;
9943 if (note->descsz < 728)
9946 if (! CONST_STRNEQ (note->namedata, "win32"))
9949 type = bfd_get_32 (abfd, note->descdata);
9953 case 1 /* NOTE_INFO_PROCESS */:
9954 /* FIXME: need to add ->core->command. */
9955 /* process_info.pid */
9956 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9957 /* process_info.signal */
9958 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9961 case 2 /* NOTE_INFO_THREAD */:
9962 /* Make a ".reg/999" section. */
9963 /* thread_info.tid */
9964 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9966 len = strlen (buf) + 1;
9967 name = (char *) bfd_alloc (abfd, len);
9971 memcpy (name, buf, len);
9973 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9977 /* sizeof (thread_info.thread_context) */
9979 /* offsetof (thread_info.thread_context) */
9980 sect->filepos = note->descpos + 12;
9981 sect->alignment_power = 2;
9983 /* thread_info.is_active_thread */
9984 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9986 if (is_active_thread)
9987 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9991 case 3 /* NOTE_INFO_MODULE */:
9992 /* Make a ".module/xxxxxxxx" section. */
9993 /* module_info.base_address */
9994 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9995 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9997 len = strlen (buf) + 1;
9998 name = (char *) bfd_alloc (abfd, len);
10002 memcpy (name, buf, len);
10004 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10009 sect->size = note->descsz;
10010 sect->filepos = note->descpos;
10011 sect->alignment_power = 2;
10022 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10024 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10026 switch (note->type)
10032 if (bed->elf_backend_grok_prstatus)
10033 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10035 #if defined (HAVE_PRSTATUS_T)
10036 return elfcore_grok_prstatus (abfd, note);
10041 #if defined (HAVE_PSTATUS_T)
10043 return elfcore_grok_pstatus (abfd, note);
10046 #if defined (HAVE_LWPSTATUS_T)
10048 return elfcore_grok_lwpstatus (abfd, note);
10051 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10052 return elfcore_grok_prfpreg (abfd, note);
10054 case NT_WIN32PSTATUS:
10055 return elfcore_grok_win32pstatus (abfd, note);
10057 case NT_PRXFPREG: /* Linux SSE extension */
10058 if (note->namesz == 6
10059 && strcmp (note->namedata, "LINUX") == 0)
10060 return elfcore_grok_prxfpreg (abfd, note);
10064 case NT_X86_XSTATE: /* Linux XSAVE extension */
10065 if (note->namesz == 6
10066 && strcmp (note->namedata, "LINUX") == 0)
10067 return elfcore_grok_xstatereg (abfd, note);
10072 if (note->namesz == 6
10073 && strcmp (note->namedata, "LINUX") == 0)
10074 return elfcore_grok_ppc_vmx (abfd, note);
10079 if (note->namesz == 6
10080 && strcmp (note->namedata, "LINUX") == 0)
10081 return elfcore_grok_ppc_vsx (abfd, note);
10086 if (note->namesz == 6
10087 && strcmp (note->namedata, "LINUX") == 0)
10088 return elfcore_grok_ppc_tar (abfd, note);
10093 if (note->namesz == 6
10094 && strcmp (note->namedata, "LINUX") == 0)
10095 return elfcore_grok_ppc_ppr (abfd, note);
10100 if (note->namesz == 6
10101 && strcmp (note->namedata, "LINUX") == 0)
10102 return elfcore_grok_ppc_dscr (abfd, note);
10107 if (note->namesz == 6
10108 && strcmp (note->namedata, "LINUX") == 0)
10109 return elfcore_grok_ppc_ebb (abfd, note);
10114 if (note->namesz == 6
10115 && strcmp (note->namedata, "LINUX") == 0)
10116 return elfcore_grok_ppc_pmu (abfd, note);
10120 case NT_PPC_TM_CGPR:
10121 if (note->namesz == 6
10122 && strcmp (note->namedata, "LINUX") == 0)
10123 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10127 case NT_PPC_TM_CFPR:
10128 if (note->namesz == 6
10129 && strcmp (note->namedata, "LINUX") == 0)
10130 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10134 case NT_PPC_TM_CVMX:
10135 if (note->namesz == 6
10136 && strcmp (note->namedata, "LINUX") == 0)
10137 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10141 case NT_PPC_TM_CVSX:
10142 if (note->namesz == 6
10143 && strcmp (note->namedata, "LINUX") == 0)
10144 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10148 case NT_PPC_TM_SPR:
10149 if (note->namesz == 6
10150 && strcmp (note->namedata, "LINUX") == 0)
10151 return elfcore_grok_ppc_tm_spr (abfd, note);
10155 case NT_PPC_TM_CTAR:
10156 if (note->namesz == 6
10157 && strcmp (note->namedata, "LINUX") == 0)
10158 return elfcore_grok_ppc_tm_ctar (abfd, note);
10162 case NT_PPC_TM_CPPR:
10163 if (note->namesz == 6
10164 && strcmp (note->namedata, "LINUX") == 0)
10165 return elfcore_grok_ppc_tm_cppr (abfd, note);
10169 case NT_PPC_TM_CDSCR:
10170 if (note->namesz == 6
10171 && strcmp (note->namedata, "LINUX") == 0)
10172 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10176 case NT_S390_HIGH_GPRS:
10177 if (note->namesz == 6
10178 && strcmp (note->namedata, "LINUX") == 0)
10179 return elfcore_grok_s390_high_gprs (abfd, note);
10183 case NT_S390_TIMER:
10184 if (note->namesz == 6
10185 && strcmp (note->namedata, "LINUX") == 0)
10186 return elfcore_grok_s390_timer (abfd, note);
10190 case NT_S390_TODCMP:
10191 if (note->namesz == 6
10192 && strcmp (note->namedata, "LINUX") == 0)
10193 return elfcore_grok_s390_todcmp (abfd, note);
10197 case NT_S390_TODPREG:
10198 if (note->namesz == 6
10199 && strcmp (note->namedata, "LINUX") == 0)
10200 return elfcore_grok_s390_todpreg (abfd, note);
10205 if (note->namesz == 6
10206 && strcmp (note->namedata, "LINUX") == 0)
10207 return elfcore_grok_s390_ctrs (abfd, note);
10211 case NT_S390_PREFIX:
10212 if (note->namesz == 6
10213 && strcmp (note->namedata, "LINUX") == 0)
10214 return elfcore_grok_s390_prefix (abfd, note);
10218 case NT_S390_LAST_BREAK:
10219 if (note->namesz == 6
10220 && strcmp (note->namedata, "LINUX") == 0)
10221 return elfcore_grok_s390_last_break (abfd, note);
10225 case NT_S390_SYSTEM_CALL:
10226 if (note->namesz == 6
10227 && strcmp (note->namedata, "LINUX") == 0)
10228 return elfcore_grok_s390_system_call (abfd, note);
10233 if (note->namesz == 6
10234 && strcmp (note->namedata, "LINUX") == 0)
10235 return elfcore_grok_s390_tdb (abfd, note);
10239 case NT_S390_VXRS_LOW:
10240 if (note->namesz == 6
10241 && strcmp (note->namedata, "LINUX") == 0)
10242 return elfcore_grok_s390_vxrs_low (abfd, note);
10246 case NT_S390_VXRS_HIGH:
10247 if (note->namesz == 6
10248 && strcmp (note->namedata, "LINUX") == 0)
10249 return elfcore_grok_s390_vxrs_high (abfd, note);
10253 case NT_S390_GS_CB:
10254 if (note->namesz == 6
10255 && strcmp (note->namedata, "LINUX") == 0)
10256 return elfcore_grok_s390_gs_cb (abfd, note);
10260 case NT_S390_GS_BC:
10261 if (note->namesz == 6
10262 && strcmp (note->namedata, "LINUX") == 0)
10263 return elfcore_grok_s390_gs_bc (abfd, note);
10268 if (note->namesz == 6
10269 && strcmp (note->namedata, "LINUX") == 0)
10270 return elfcore_grok_arm_vfp (abfd, note);
10275 if (note->namesz == 6
10276 && strcmp (note->namedata, "LINUX") == 0)
10277 return elfcore_grok_aarch_tls (abfd, note);
10281 case NT_ARM_HW_BREAK:
10282 if (note->namesz == 6
10283 && strcmp (note->namedata, "LINUX") == 0)
10284 return elfcore_grok_aarch_hw_break (abfd, note);
10288 case NT_ARM_HW_WATCH:
10289 if (note->namesz == 6
10290 && strcmp (note->namedata, "LINUX") == 0)
10291 return elfcore_grok_aarch_hw_watch (abfd, note);
10296 if (note->namesz == 6
10297 && strcmp (note->namedata, "LINUX") == 0)
10298 return elfcore_grok_aarch_sve (abfd, note);
10302 case NT_ARM_PAC_MASK:
10303 if (note->namesz == 6
10304 && strcmp (note->namedata, "LINUX") == 0)
10305 return elfcore_grok_aarch_pauth (abfd, note);
10311 if (bed->elf_backend_grok_psinfo)
10312 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10314 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10315 return elfcore_grok_psinfo (abfd, note);
10321 return elfcore_make_auxv_note_section (abfd, note, 0);
10324 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10328 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10335 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10337 struct bfd_build_id* build_id;
10339 if (note->descsz == 0)
10342 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10343 if (build_id == NULL)
10346 build_id->size = note->descsz;
10347 memcpy (build_id->data, note->descdata, note->descsz);
10348 abfd->build_id = build_id;
10354 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10356 switch (note->type)
10361 case NT_GNU_PROPERTY_TYPE_0:
10362 return _bfd_elf_parse_gnu_properties (abfd, note);
10364 case NT_GNU_BUILD_ID:
10365 return elfobj_grok_gnu_build_id (abfd, note);
10370 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10372 struct sdt_note *cur =
10373 (struct sdt_note *) bfd_alloc (abfd,
10374 sizeof (struct sdt_note) + note->descsz);
10376 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10377 cur->size = (bfd_size_type) note->descsz;
10378 memcpy (cur->data, note->descdata, note->descsz);
10380 elf_tdata (abfd)->sdt_note_head = cur;
10386 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10388 switch (note->type)
10391 return elfobj_grok_stapsdt_note_1 (abfd, note);
10399 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10403 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10406 if (note->descsz < 108)
10411 if (note->descsz < 120)
10419 /* Check for version 1 in pr_version. */
10420 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10425 /* Skip over pr_psinfosz. */
10426 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10430 offset += 4; /* Padding before pr_psinfosz. */
10434 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10435 elf_tdata (abfd)->core->program
10436 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10439 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10440 elf_tdata (abfd)->core->command
10441 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10444 /* Padding before pr_pid. */
10447 /* The pr_pid field was added in version "1a". */
10448 if (note->descsz < offset + 4)
10451 elf_tdata (abfd)->core->pid
10452 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10458 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10464 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10465 Also compute minimum size of this note. */
10466 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10470 min_size = offset + (4 * 2) + 4 + 4 + 4;
10474 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10475 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10482 if (note->descsz < min_size)
10485 /* Check for version 1 in pr_version. */
10486 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10489 /* Extract size of pr_reg from pr_gregsetsz. */
10490 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10491 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10493 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10498 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10502 /* Skip over pr_osreldate. */
10505 /* Read signal from pr_cursig. */
10506 if (elf_tdata (abfd)->core->signal == 0)
10507 elf_tdata (abfd)->core->signal
10508 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10511 /* Read TID from pr_pid. */
10512 elf_tdata (abfd)->core->lwpid
10513 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10516 /* Padding before pr_reg. */
10517 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10520 /* Make sure that there is enough data remaining in the note. */
10521 if ((note->descsz - offset) < size)
10524 /* Make a ".reg/999" section and a ".reg" section. */
10525 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10526 size, note->descpos + offset);
10530 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10532 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10534 switch (note->type)
10537 if (bed->elf_backend_grok_freebsd_prstatus)
10538 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10540 return elfcore_grok_freebsd_prstatus (abfd, note);
10543 return elfcore_grok_prfpreg (abfd, note);
10546 return elfcore_grok_freebsd_psinfo (abfd, note);
10548 case NT_FREEBSD_THRMISC:
10549 if (note->namesz == 8)
10550 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10554 case NT_FREEBSD_PROCSTAT_PROC:
10555 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10558 case NT_FREEBSD_PROCSTAT_FILES:
10559 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10562 case NT_FREEBSD_PROCSTAT_VMMAP:
10563 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10566 case NT_FREEBSD_PROCSTAT_AUXV:
10567 return elfcore_make_auxv_note_section (abfd, note, 4);
10569 case NT_X86_XSTATE:
10570 if (note->namesz == 8)
10571 return elfcore_grok_xstatereg (abfd, note);
10575 case NT_FREEBSD_PTLWPINFO:
10576 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10580 return elfcore_grok_arm_vfp (abfd, note);
10588 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10592 cp = strchr (note->namedata, '@');
10595 *lwpidp = atoi(cp + 1);
10602 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10604 if (note->descsz <= 0x7c + 31)
10607 /* Signal number at offset 0x08. */
10608 elf_tdata (abfd)->core->signal
10609 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10611 /* Process ID at offset 0x50. */
10612 elf_tdata (abfd)->core->pid
10613 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10615 /* Command name at 0x7c (max 32 bytes, including nul). */
10616 elf_tdata (abfd)->core->command
10617 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10619 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10624 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10628 if (elfcore_netbsd_get_lwpid (note, &lwp))
10629 elf_tdata (abfd)->core->lwpid = lwp;
10631 switch (note->type)
10633 case NT_NETBSDCORE_PROCINFO:
10634 /* NetBSD-specific core "procinfo". Note that we expect to
10635 find this note before any of the others, which is fine,
10636 since the kernel writes this note out first when it
10637 creates a core file. */
10638 return elfcore_grok_netbsd_procinfo (abfd, note);
10639 #ifdef NT_NETBSDCORE_AUXV
10640 case NT_NETBSDCORE_AUXV:
10641 /* NetBSD-specific Elf Auxiliary Vector data. */
10642 return elfcore_make_auxv_note_section (abfd, note, 4);
10648 /* As of March 2017 there are no other machine-independent notes
10649 defined for NetBSD core files. If the note type is less
10650 than the start of the machine-dependent note types, we don't
10653 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10657 switch (bfd_get_arch (abfd))
10659 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10660 PT_GETFPREGS == mach+2. */
10662 case bfd_arch_alpha:
10663 case bfd_arch_sparc:
10664 switch (note->type)
10666 case NT_NETBSDCORE_FIRSTMACH+0:
10667 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10669 case NT_NETBSDCORE_FIRSTMACH+2:
10670 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10676 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10677 There's also old PT___GETREGS40 == mach + 1 for old reg
10678 structure which lacks GBR. */
10681 switch (note->type)
10683 case NT_NETBSDCORE_FIRSTMACH+3:
10684 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10686 case NT_NETBSDCORE_FIRSTMACH+5:
10687 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10693 /* On all other arch's, PT_GETREGS == mach+1 and
10694 PT_GETFPREGS == mach+3. */
10697 switch (note->type)
10699 case NT_NETBSDCORE_FIRSTMACH+1:
10700 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10702 case NT_NETBSDCORE_FIRSTMACH+3:
10703 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10713 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10715 if (note->descsz <= 0x48 + 31)
10718 /* Signal number at offset 0x08. */
10719 elf_tdata (abfd)->core->signal
10720 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10722 /* Process ID at offset 0x20. */
10723 elf_tdata (abfd)->core->pid
10724 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10726 /* Command name at 0x48 (max 32 bytes, including nul). */
10727 elf_tdata (abfd)->core->command
10728 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10734 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10736 if (note->type == NT_OPENBSD_PROCINFO)
10737 return elfcore_grok_openbsd_procinfo (abfd, note);
10739 if (note->type == NT_OPENBSD_REGS)
10740 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10742 if (note->type == NT_OPENBSD_FPREGS)
10743 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10745 if (note->type == NT_OPENBSD_XFPREGS)
10746 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10748 if (note->type == NT_OPENBSD_AUXV)
10749 return elfcore_make_auxv_note_section (abfd, note, 0);
10751 if (note->type == NT_OPENBSD_WCOOKIE)
10753 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10758 sect->size = note->descsz;
10759 sect->filepos = note->descpos;
10760 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10769 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10771 void *ddata = note->descdata;
10778 if (note->descsz < 16)
10781 /* nto_procfs_status 'pid' field is at offset 0. */
10782 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10784 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10785 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10787 /* nto_procfs_status 'flags' field is at offset 8. */
10788 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10790 /* nto_procfs_status 'what' field is at offset 14. */
10791 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10793 elf_tdata (abfd)->core->signal = sig;
10794 elf_tdata (abfd)->core->lwpid = *tid;
10797 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10798 do not come from signals so we make sure we set the current
10799 thread just in case. */
10800 if (flags & 0x00000080)
10801 elf_tdata (abfd)->core->lwpid = *tid;
10803 /* Make a ".qnx_core_status/%d" section. */
10804 sprintf (buf, ".qnx_core_status/%ld", *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 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10823 elfcore_grok_nto_regs (bfd *abfd,
10824 Elf_Internal_Note *note,
10832 /* Make a "(base)/%d" section. */
10833 sprintf (buf, "%s/%ld", base, tid);
10835 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10838 strcpy (name, buf);
10840 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10844 sect->size = note->descsz;
10845 sect->filepos = note->descpos;
10846 sect->alignment_power = 2;
10848 /* This is the current thread. */
10849 if (elf_tdata (abfd)->core->lwpid == tid)
10850 return elfcore_maybe_make_sect (abfd, base, sect);
10855 #define BFD_QNT_CORE_INFO 7
10856 #define BFD_QNT_CORE_STATUS 8
10857 #define BFD_QNT_CORE_GREG 9
10858 #define BFD_QNT_CORE_FPREG 10
10861 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10863 /* Every GREG section has a STATUS section before it. Store the
10864 tid from the previous call to pass down to the next gregs
10866 static long tid = 1;
10868 switch (note->type)
10870 case BFD_QNT_CORE_INFO:
10871 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10872 case BFD_QNT_CORE_STATUS:
10873 return elfcore_grok_nto_status (abfd, note, &tid);
10874 case BFD_QNT_CORE_GREG:
10875 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10876 case BFD_QNT_CORE_FPREG:
10877 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10884 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10890 /* Use note name as section name. */
10891 len = note->namesz;
10892 name = (char *) bfd_alloc (abfd, len);
10895 memcpy (name, note->namedata, len);
10896 name[len - 1] = '\0';
10898 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10902 sect->size = note->descsz;
10903 sect->filepos = note->descpos;
10904 sect->alignment_power = 1;
10909 /* Function: elfcore_write_note
10912 buffer to hold note, and current size of buffer
10916 size of data for note
10918 Writes note to end of buffer. ELF64 notes are written exactly as
10919 for ELF32, despite the current (as of 2006) ELF gabi specifying
10920 that they ought to have 8-byte namesz and descsz field, and have
10921 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10924 Pointer to realloc'd buffer, *BUFSIZ updated. */
10927 elfcore_write_note (bfd *abfd,
10935 Elf_External_Note *xnp;
10942 namesz = strlen (name) + 1;
10944 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
10946 buf = (char *) realloc (buf, *bufsiz + newspace);
10949 dest = buf + *bufsiz;
10950 *bufsiz += newspace;
10951 xnp = (Elf_External_Note *) dest;
10952 H_PUT_32 (abfd, namesz, xnp->namesz);
10953 H_PUT_32 (abfd, size, xnp->descsz);
10954 H_PUT_32 (abfd, type, xnp->type);
10958 memcpy (dest, name, namesz);
10966 memcpy (dest, input, size);
10976 /* gcc-8 warns (*) on all the strncpy calls in this function about
10977 possible string truncation. The "truncation" is not a bug. We
10978 have an external representation of structs with fields that are not
10979 necessarily NULL terminated and corresponding internal
10980 representation fields that are one larger so that they can always
10981 be NULL terminated.
10982 gcc versions between 4.2 and 4.6 do not allow pragma control of
10983 diagnostics inside functions, giving a hard error if you try to use
10984 the finer control available with later versions.
10985 gcc prior to 4.2 warns about diagnostic push and pop.
10986 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10987 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10988 (*) Depending on your system header files! */
10989 #if GCC_VERSION >= 8000
10990 # pragma GCC diagnostic push
10991 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10994 elfcore_write_prpsinfo (bfd *abfd,
10998 const char *psargs)
11000 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11002 if (bed->elf_backend_write_core_note != NULL)
11005 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11006 NT_PRPSINFO, fname, psargs);
11011 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11012 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11013 if (bed->s->elfclass == ELFCLASS32)
11015 # if defined (HAVE_PSINFO32_T)
11017 int note_type = NT_PSINFO;
11020 int note_type = NT_PRPSINFO;
11023 memset (&data, 0, sizeof (data));
11024 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11025 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11026 return elfcore_write_note (abfd, buf, bufsiz,
11027 "CORE", note_type, &data, sizeof (data));
11032 # if defined (HAVE_PSINFO_T)
11034 int note_type = NT_PSINFO;
11037 int note_type = NT_PRPSINFO;
11040 memset (&data, 0, sizeof (data));
11041 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11042 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11043 return elfcore_write_note (abfd, buf, bufsiz,
11044 "CORE", note_type, &data, sizeof (data));
11046 #endif /* PSINFO_T or PRPSINFO_T */
11051 #if GCC_VERSION >= 8000
11052 # pragma GCC diagnostic pop
11056 elfcore_write_linux_prpsinfo32
11057 (bfd *abfd, char *buf, int *bufsiz,
11058 const struct elf_internal_linux_prpsinfo *prpsinfo)
11060 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11062 struct elf_external_linux_prpsinfo32_ugid16 data;
11064 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11065 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11066 &data, sizeof (data));
11070 struct elf_external_linux_prpsinfo32_ugid32 data;
11072 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11073 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11074 &data, sizeof (data));
11079 elfcore_write_linux_prpsinfo64
11080 (bfd *abfd, char *buf, int *bufsiz,
11081 const struct elf_internal_linux_prpsinfo *prpsinfo)
11083 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11085 struct elf_external_linux_prpsinfo64_ugid16 data;
11087 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11088 return elfcore_write_note (abfd, buf, bufsiz,
11089 "CORE", NT_PRPSINFO, &data, sizeof (data));
11093 struct elf_external_linux_prpsinfo64_ugid32 data;
11095 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11096 return elfcore_write_note (abfd, buf, bufsiz,
11097 "CORE", NT_PRPSINFO, &data, sizeof (data));
11102 elfcore_write_prstatus (bfd *abfd,
11109 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11111 if (bed->elf_backend_write_core_note != NULL)
11114 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11116 pid, cursig, gregs);
11121 #if defined (HAVE_PRSTATUS_T)
11122 #if defined (HAVE_PRSTATUS32_T)
11123 if (bed->s->elfclass == ELFCLASS32)
11125 prstatus32_t prstat;
11127 memset (&prstat, 0, sizeof (prstat));
11128 prstat.pr_pid = pid;
11129 prstat.pr_cursig = cursig;
11130 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11131 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11132 NT_PRSTATUS, &prstat, sizeof (prstat));
11139 memset (&prstat, 0, sizeof (prstat));
11140 prstat.pr_pid = pid;
11141 prstat.pr_cursig = cursig;
11142 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11143 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11144 NT_PRSTATUS, &prstat, sizeof (prstat));
11146 #endif /* HAVE_PRSTATUS_T */
11152 #if defined (HAVE_LWPSTATUS_T)
11154 elfcore_write_lwpstatus (bfd *abfd,
11161 lwpstatus_t lwpstat;
11162 const char *note_name = "CORE";
11164 memset (&lwpstat, 0, sizeof (lwpstat));
11165 lwpstat.pr_lwpid = pid >> 16;
11166 lwpstat.pr_cursig = cursig;
11167 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11168 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11169 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11170 #if !defined(gregs)
11171 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11172 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11174 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11175 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11178 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11179 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11181 #endif /* HAVE_LWPSTATUS_T */
11183 #if defined (HAVE_PSTATUS_T)
11185 elfcore_write_pstatus (bfd *abfd,
11189 int cursig ATTRIBUTE_UNUSED,
11190 const void *gregs ATTRIBUTE_UNUSED)
11192 const char *note_name = "CORE";
11193 #if defined (HAVE_PSTATUS32_T)
11194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11196 if (bed->s->elfclass == ELFCLASS32)
11200 memset (&pstat, 0, sizeof (pstat));
11201 pstat.pr_pid = pid & 0xffff;
11202 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11203 NT_PSTATUS, &pstat, sizeof (pstat));
11211 memset (&pstat, 0, sizeof (pstat));
11212 pstat.pr_pid = pid & 0xffff;
11213 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11214 NT_PSTATUS, &pstat, sizeof (pstat));
11218 #endif /* HAVE_PSTATUS_T */
11221 elfcore_write_prfpreg (bfd *abfd,
11224 const void *fpregs,
11227 const char *note_name = "CORE";
11228 return elfcore_write_note (abfd, buf, bufsiz,
11229 note_name, NT_FPREGSET, fpregs, size);
11233 elfcore_write_prxfpreg (bfd *abfd,
11236 const void *xfpregs,
11239 char *note_name = "LINUX";
11240 return elfcore_write_note (abfd, buf, bufsiz,
11241 note_name, NT_PRXFPREG, xfpregs, size);
11245 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11246 const void *xfpregs, int size)
11249 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11250 note_name = "FreeBSD";
11252 note_name = "LINUX";
11253 return elfcore_write_note (abfd, buf, bufsiz,
11254 note_name, NT_X86_XSTATE, xfpregs, size);
11258 elfcore_write_ppc_vmx (bfd *abfd,
11261 const void *ppc_vmx,
11264 char *note_name = "LINUX";
11265 return elfcore_write_note (abfd, buf, bufsiz,
11266 note_name, NT_PPC_VMX, ppc_vmx, size);
11270 elfcore_write_ppc_vsx (bfd *abfd,
11273 const void *ppc_vsx,
11276 char *note_name = "LINUX";
11277 return elfcore_write_note (abfd, buf, bufsiz,
11278 note_name, NT_PPC_VSX, ppc_vsx, size);
11282 elfcore_write_ppc_tar (bfd *abfd,
11285 const void *ppc_tar,
11288 char *note_name = "LINUX";
11289 return elfcore_write_note (abfd, buf, bufsiz,
11290 note_name, NT_PPC_TAR, ppc_tar, size);
11294 elfcore_write_ppc_ppr (bfd *abfd,
11297 const void *ppc_ppr,
11300 char *note_name = "LINUX";
11301 return elfcore_write_note (abfd, buf, bufsiz,
11302 note_name, NT_PPC_PPR, ppc_ppr, size);
11306 elfcore_write_ppc_dscr (bfd *abfd,
11309 const void *ppc_dscr,
11312 char *note_name = "LINUX";
11313 return elfcore_write_note (abfd, buf, bufsiz,
11314 note_name, NT_PPC_DSCR, ppc_dscr, size);
11318 elfcore_write_ppc_ebb (bfd *abfd,
11321 const void *ppc_ebb,
11324 char *note_name = "LINUX";
11325 return elfcore_write_note (abfd, buf, bufsiz,
11326 note_name, NT_PPC_EBB, ppc_ebb, size);
11330 elfcore_write_ppc_pmu (bfd *abfd,
11333 const void *ppc_pmu,
11336 char *note_name = "LINUX";
11337 return elfcore_write_note (abfd, buf, bufsiz,
11338 note_name, NT_PPC_PMU, ppc_pmu, size);
11342 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11345 const void *ppc_tm_cgpr,
11348 char *note_name = "LINUX";
11349 return elfcore_write_note (abfd, buf, bufsiz,
11350 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11354 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11357 const void *ppc_tm_cfpr,
11360 char *note_name = "LINUX";
11361 return elfcore_write_note (abfd, buf, bufsiz,
11362 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11366 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11369 const void *ppc_tm_cvmx,
11372 char *note_name = "LINUX";
11373 return elfcore_write_note (abfd, buf, bufsiz,
11374 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11378 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11381 const void *ppc_tm_cvsx,
11384 char *note_name = "LINUX";
11385 return elfcore_write_note (abfd, buf, bufsiz,
11386 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11390 elfcore_write_ppc_tm_spr (bfd *abfd,
11393 const void *ppc_tm_spr,
11396 char *note_name = "LINUX";
11397 return elfcore_write_note (abfd, buf, bufsiz,
11398 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11402 elfcore_write_ppc_tm_ctar (bfd *abfd,
11405 const void *ppc_tm_ctar,
11408 char *note_name = "LINUX";
11409 return elfcore_write_note (abfd, buf, bufsiz,
11410 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11414 elfcore_write_ppc_tm_cppr (bfd *abfd,
11417 const void *ppc_tm_cppr,
11420 char *note_name = "LINUX";
11421 return elfcore_write_note (abfd, buf, bufsiz,
11422 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11426 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11429 const void *ppc_tm_cdscr,
11432 char *note_name = "LINUX";
11433 return elfcore_write_note (abfd, buf, bufsiz,
11434 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11438 elfcore_write_s390_high_gprs (bfd *abfd,
11441 const void *s390_high_gprs,
11444 char *note_name = "LINUX";
11445 return elfcore_write_note (abfd, buf, bufsiz,
11446 note_name, NT_S390_HIGH_GPRS,
11447 s390_high_gprs, size);
11451 elfcore_write_s390_timer (bfd *abfd,
11454 const void *s390_timer,
11457 char *note_name = "LINUX";
11458 return elfcore_write_note (abfd, buf, bufsiz,
11459 note_name, NT_S390_TIMER, s390_timer, size);
11463 elfcore_write_s390_todcmp (bfd *abfd,
11466 const void *s390_todcmp,
11469 char *note_name = "LINUX";
11470 return elfcore_write_note (abfd, buf, bufsiz,
11471 note_name, NT_S390_TODCMP, s390_todcmp, size);
11475 elfcore_write_s390_todpreg (bfd *abfd,
11478 const void *s390_todpreg,
11481 char *note_name = "LINUX";
11482 return elfcore_write_note (abfd, buf, bufsiz,
11483 note_name, NT_S390_TODPREG, s390_todpreg, size);
11487 elfcore_write_s390_ctrs (bfd *abfd,
11490 const void *s390_ctrs,
11493 char *note_name = "LINUX";
11494 return elfcore_write_note (abfd, buf, bufsiz,
11495 note_name, NT_S390_CTRS, s390_ctrs, size);
11499 elfcore_write_s390_prefix (bfd *abfd,
11502 const void *s390_prefix,
11505 char *note_name = "LINUX";
11506 return elfcore_write_note (abfd, buf, bufsiz,
11507 note_name, NT_S390_PREFIX, s390_prefix, size);
11511 elfcore_write_s390_last_break (bfd *abfd,
11514 const void *s390_last_break,
11517 char *note_name = "LINUX";
11518 return elfcore_write_note (abfd, buf, bufsiz,
11519 note_name, NT_S390_LAST_BREAK,
11520 s390_last_break, size);
11524 elfcore_write_s390_system_call (bfd *abfd,
11527 const void *s390_system_call,
11530 char *note_name = "LINUX";
11531 return elfcore_write_note (abfd, buf, bufsiz,
11532 note_name, NT_S390_SYSTEM_CALL,
11533 s390_system_call, size);
11537 elfcore_write_s390_tdb (bfd *abfd,
11540 const void *s390_tdb,
11543 char *note_name = "LINUX";
11544 return elfcore_write_note (abfd, buf, bufsiz,
11545 note_name, NT_S390_TDB, s390_tdb, size);
11549 elfcore_write_s390_vxrs_low (bfd *abfd,
11552 const void *s390_vxrs_low,
11555 char *note_name = "LINUX";
11556 return elfcore_write_note (abfd, buf, bufsiz,
11557 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11561 elfcore_write_s390_vxrs_high (bfd *abfd,
11564 const void *s390_vxrs_high,
11567 char *note_name = "LINUX";
11568 return elfcore_write_note (abfd, buf, bufsiz,
11569 note_name, NT_S390_VXRS_HIGH,
11570 s390_vxrs_high, size);
11574 elfcore_write_s390_gs_cb (bfd *abfd,
11577 const void *s390_gs_cb,
11580 char *note_name = "LINUX";
11581 return elfcore_write_note (abfd, buf, bufsiz,
11582 note_name, NT_S390_GS_CB,
11587 elfcore_write_s390_gs_bc (bfd *abfd,
11590 const void *s390_gs_bc,
11593 char *note_name = "LINUX";
11594 return elfcore_write_note (abfd, buf, bufsiz,
11595 note_name, NT_S390_GS_BC,
11600 elfcore_write_arm_vfp (bfd *abfd,
11603 const void *arm_vfp,
11606 char *note_name = "LINUX";
11607 return elfcore_write_note (abfd, buf, bufsiz,
11608 note_name, NT_ARM_VFP, arm_vfp, size);
11612 elfcore_write_aarch_tls (bfd *abfd,
11615 const void *aarch_tls,
11618 char *note_name = "LINUX";
11619 return elfcore_write_note (abfd, buf, bufsiz,
11620 note_name, NT_ARM_TLS, aarch_tls, size);
11624 elfcore_write_aarch_hw_break (bfd *abfd,
11627 const void *aarch_hw_break,
11630 char *note_name = "LINUX";
11631 return elfcore_write_note (abfd, buf, bufsiz,
11632 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11636 elfcore_write_aarch_hw_watch (bfd *abfd,
11639 const void *aarch_hw_watch,
11642 char *note_name = "LINUX";
11643 return elfcore_write_note (abfd, buf, bufsiz,
11644 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11648 elfcore_write_aarch_sve (bfd *abfd,
11651 const void *aarch_sve,
11654 char *note_name = "LINUX";
11655 return elfcore_write_note (abfd, buf, bufsiz,
11656 note_name, NT_ARM_SVE, aarch_sve, size);
11660 elfcore_write_aarch_pauth (bfd *abfd,
11663 const void *aarch_pauth,
11666 char *note_name = "LINUX";
11667 return elfcore_write_note (abfd, buf, bufsiz,
11668 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11672 elfcore_write_register_note (bfd *abfd,
11675 const char *section,
11679 if (strcmp (section, ".reg2") == 0)
11680 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11681 if (strcmp (section, ".reg-xfp") == 0)
11682 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
11683 if (strcmp (section, ".reg-xstate") == 0)
11684 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
11685 if (strcmp (section, ".reg-ppc-vmx") == 0)
11686 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
11687 if (strcmp (section, ".reg-ppc-vsx") == 0)
11688 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
11689 if (strcmp (section, ".reg-ppc-tar") == 0)
11690 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11691 if (strcmp (section, ".reg-ppc-ppr") == 0)
11692 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11693 if (strcmp (section, ".reg-ppc-dscr") == 0)
11694 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11695 if (strcmp (section, ".reg-ppc-ebb") == 0)
11696 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11697 if (strcmp (section, ".reg-ppc-pmu") == 0)
11698 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11699 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11700 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11701 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11702 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11703 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11704 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11705 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11706 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11707 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11708 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11709 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11710 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11711 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11712 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11713 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11714 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
11715 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11716 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
11717 if (strcmp (section, ".reg-s390-timer") == 0)
11718 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11719 if (strcmp (section, ".reg-s390-todcmp") == 0)
11720 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11721 if (strcmp (section, ".reg-s390-todpreg") == 0)
11722 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11723 if (strcmp (section, ".reg-s390-ctrs") == 0)
11724 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11725 if (strcmp (section, ".reg-s390-prefix") == 0)
11726 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
11727 if (strcmp (section, ".reg-s390-last-break") == 0)
11728 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11729 if (strcmp (section, ".reg-s390-system-call") == 0)
11730 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
11731 if (strcmp (section, ".reg-s390-tdb") == 0)
11732 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
11733 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11734 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11735 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11736 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
11737 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11738 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11739 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11740 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
11741 if (strcmp (section, ".reg-arm-vfp") == 0)
11742 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
11743 if (strcmp (section, ".reg-aarch-tls") == 0)
11744 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11745 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11746 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11747 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11748 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
11749 if (strcmp (section, ".reg-aarch-sve") == 0)
11750 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
11751 if (strcmp (section, ".reg-aarch-pauth") == 0)
11752 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
11757 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11762 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11763 gABI specifies that PT_NOTE alignment should be aligned to 4
11764 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11765 align is less than 4, we use 4 byte alignment. */
11768 if (align != 4 && align != 8)
11772 while (p < buf + size)
11774 Elf_External_Note *xnp = (Elf_External_Note *) p;
11775 Elf_Internal_Note in;
11777 if (offsetof (Elf_External_Note, name) > buf - p + size)
11780 in.type = H_GET_32 (abfd, xnp->type);
11782 in.namesz = H_GET_32 (abfd, xnp->namesz);
11783 in.namedata = xnp->name;
11784 if (in.namesz > buf - in.namedata + size)
11787 in.descsz = H_GET_32 (abfd, xnp->descsz);
11788 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
11789 in.descpos = offset + (in.descdata - buf);
11791 && (in.descdata >= buf + size
11792 || in.descsz > buf - in.descdata + size))
11795 switch (bfd_get_format (abfd))
11802 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11805 const char * string;
11807 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
11811 GROKER_ELEMENT ("", elfcore_grok_note),
11812 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11813 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11814 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11815 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11816 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
11818 #undef GROKER_ELEMENT
11821 for (i = ARRAY_SIZE (grokers); i--;)
11823 if (in.namesz >= grokers[i].len
11824 && strncmp (in.namedata, grokers[i].string,
11825 grokers[i].len) == 0)
11827 if (! grokers[i].func (abfd, & in))
11836 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11838 if (! elfobj_grok_gnu_note (abfd, &in))
11841 else if (in.namesz == sizeof "stapsdt"
11842 && strcmp (in.namedata, "stapsdt") == 0)
11844 if (! elfobj_grok_stapsdt_note (abfd, &in))
11850 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
11857 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11862 if (size == 0 || (size + 1) == 0)
11865 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11868 buf = (char *) bfd_malloc (size + 1);
11872 /* PR 17512: file: ec08f814
11873 0-termintate the buffer so that string searches will not overflow. */
11876 if (bfd_bread (buf, size, abfd) != size
11877 || !elf_parse_notes (abfd, buf, size, offset, align))
11887 /* Providing external access to the ELF program header table. */
11889 /* Return an upper bound on the number of bytes required to store a
11890 copy of ABFD's program header table entries. Return -1 if an error
11891 occurs; bfd_get_error will return an appropriate code. */
11894 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11896 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11898 bfd_set_error (bfd_error_wrong_format);
11902 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11905 /* Copy ABFD's program header table entries to *PHDRS. The entries
11906 will be stored as an array of Elf_Internal_Phdr structures, as
11907 defined in include/elf/internal.h. To find out how large the
11908 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11910 Return the number of program header table entries read, or -1 if an
11911 error occurs; bfd_get_error will return an appropriate code. */
11914 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11918 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11920 bfd_set_error (bfd_error_wrong_format);
11924 num_phdrs = elf_elfheader (abfd)->e_phnum;
11925 if (num_phdrs != 0)
11926 memcpy (phdrs, elf_tdata (abfd)->phdr,
11927 num_phdrs * sizeof (Elf_Internal_Phdr));
11932 enum elf_reloc_type_class
11933 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11934 const asection *rel_sec ATTRIBUTE_UNUSED,
11935 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
11937 return reloc_class_normal;
11940 /* For RELA architectures, return the relocation value for a
11941 relocation against a local symbol. */
11944 _bfd_elf_rela_local_sym (bfd *abfd,
11945 Elf_Internal_Sym *sym,
11947 Elf_Internal_Rela *rel)
11949 asection *sec = *psec;
11950 bfd_vma relocation;
11952 relocation = (sec->output_section->vma
11953 + sec->output_offset
11955 if ((sec->flags & SEC_MERGE)
11956 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
11957 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
11960 _bfd_merged_section_offset (abfd, psec,
11961 elf_section_data (sec)->sec_info,
11962 sym->st_value + rel->r_addend);
11965 /* If we have changed the section, and our original section is
11966 marked with SEC_EXCLUDE, it means that the original
11967 SEC_MERGE section has been completely subsumed in some
11968 other SEC_MERGE section. In this case, we need to leave
11969 some info around for --emit-relocs. */
11970 if ((sec->flags & SEC_EXCLUDE) != 0)
11971 sec->kept_section = *psec;
11974 rel->r_addend -= relocation;
11975 rel->r_addend += sec->output_section->vma + sec->output_offset;
11981 _bfd_elf_rel_local_sym (bfd *abfd,
11982 Elf_Internal_Sym *sym,
11986 asection *sec = *psec;
11988 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
11989 return sym->st_value + addend;
11991 return _bfd_merged_section_offset (abfd, psec,
11992 elf_section_data (sec)->sec_info,
11993 sym->st_value + addend);
11996 /* Adjust an address within a section. Given OFFSET within SEC, return
11997 the new offset within the section, based upon changes made to the
11998 section. Returns -1 if the offset is now invalid.
11999 The offset (in abnd out) is in target sized bytes, however big a
12003 _bfd_elf_section_offset (bfd *abfd,
12004 struct bfd_link_info *info,
12008 switch (sec->sec_info_type)
12010 case SEC_INFO_TYPE_STABS:
12011 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12013 case SEC_INFO_TYPE_EH_FRAME:
12014 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12017 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12019 /* Reverse the offset. */
12020 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12021 bfd_size_type address_size = bed->s->arch_size / 8;
12023 /* address_size and sec->size are in octets. Convert
12024 to bytes before subtracting the original offset. */
12025 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
12031 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12032 reconstruct an ELF file by reading the segments out of remote memory
12033 based on the ELF file header at EHDR_VMA and the ELF program headers it
12034 points to. If not null, *LOADBASEP is filled in with the difference
12035 between the VMAs from which the segments were read, and the VMAs the
12036 file headers (and hence BFD's idea of each section's VMA) put them at.
12038 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12039 remote memory at target address VMA into the local buffer at MYADDR; it
12040 should return zero on success or an `errno' code on failure. TEMPL must
12041 be a BFD for an ELF target with the word size and byte order found in
12042 the remote memory. */
12045 bfd_elf_bfd_from_remote_memory
12048 bfd_size_type size,
12049 bfd_vma *loadbasep,
12050 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12052 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12053 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12057 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12058 long symcount ATTRIBUTE_UNUSED,
12059 asymbol **syms ATTRIBUTE_UNUSED,
12064 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12067 const char *relplt_name;
12068 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12072 Elf_Internal_Shdr *hdr;
12078 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12081 if (dynsymcount <= 0)
12084 if (!bed->plt_sym_val)
12087 relplt_name = bed->relplt_name;
12088 if (relplt_name == NULL)
12089 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12090 relplt = bfd_get_section_by_name (abfd, relplt_name);
12091 if (relplt == NULL)
12094 hdr = &elf_section_data (relplt)->this_hdr;
12095 if (hdr->sh_link != elf_dynsymtab (abfd)
12096 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12099 plt = bfd_get_section_by_name (abfd, ".plt");
12103 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12104 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
12107 count = relplt->size / hdr->sh_entsize;
12108 size = count * sizeof (asymbol);
12109 p = relplt->relocation;
12110 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12112 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12113 if (p->addend != 0)
12116 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12118 size += sizeof ("+0x") - 1 + 8;
12123 s = *ret = (asymbol *) bfd_malloc (size);
12127 names = (char *) (s + count);
12128 p = relplt->relocation;
12130 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12135 addr = bed->plt_sym_val (i, plt, p);
12136 if (addr == (bfd_vma) -1)
12139 *s = **p->sym_ptr_ptr;
12140 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12141 we are defining a symbol, ensure one of them is set. */
12142 if ((s->flags & BSF_LOCAL) == 0)
12143 s->flags |= BSF_GLOBAL;
12144 s->flags |= BSF_SYNTHETIC;
12146 s->value = addr - plt->vma;
12149 len = strlen ((*p->sym_ptr_ptr)->name);
12150 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12152 if (p->addend != 0)
12156 memcpy (names, "+0x", sizeof ("+0x") - 1);
12157 names += sizeof ("+0x") - 1;
12158 bfd_sprintf_vma (abfd, buf, p->addend);
12159 for (a = buf; *a == '0'; ++a)
12162 memcpy (names, a, len);
12165 memcpy (names, "@plt", sizeof ("@plt"));
12166 names += sizeof ("@plt");
12173 /* It is only used by x86-64 so far.
12174 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12175 but current usage would allow all of _bfd_std_section to be zero. */
12176 static const asymbol lcomm_sym
12177 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12178 asection _bfd_elf_large_com_section
12179 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12180 "LARGE_COMMON", 0, SEC_IS_COMMON);
12183 _bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED,
12184 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12189 _bfd_elf_final_write_processing (bfd *abfd)
12191 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
12193 i_ehdrp = elf_elfheader (abfd);
12195 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12196 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
12198 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12199 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12200 STB_GNU_UNIQUE binding. */
12201 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12203 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12204 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12205 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12206 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12208 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12209 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12210 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12211 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12212 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12213 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12214 bfd_set_error (bfd_error_bad_value);
12222 /* Return TRUE for ELF symbol types that represent functions.
12223 This is the default version of this function, which is sufficient for
12224 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12227 _bfd_elf_is_function_type (unsigned int type)
12229 return (type == STT_FUNC
12230 || type == STT_GNU_IFUNC);
12233 /* If the ELF symbol SYM might be a function in SEC, return the
12234 function size and set *CODE_OFF to the function's entry point,
12235 otherwise return zero. */
12238 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12241 bfd_size_type size;
12243 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
12244 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12245 || sym->section != sec)
12248 *code_off = sym->value;
12250 if (!(sym->flags & BSF_SYNTHETIC))
12251 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;