1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2019 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bfd_boolean prep_headers (bfd *);
55 static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
56 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
58 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
59 file_ptr offset, size_t align);
61 /* Swap version information in and out. The version information is
62 currently size independent. If that ever changes, this code will
63 need to move into elfcode.h. */
65 /* Swap in a Verdef structure. */
68 _bfd_elf_swap_verdef_in (bfd *abfd,
69 const Elf_External_Verdef *src,
70 Elf_Internal_Verdef *dst)
72 dst->vd_version = H_GET_16 (abfd, src->vd_version);
73 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
74 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
75 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
76 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
77 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
78 dst->vd_next = H_GET_32 (abfd, src->vd_next);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (bfd *abfd,
85 const Elf_Internal_Verdef *src,
86 Elf_External_Verdef *dst)
88 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
89 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
90 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
91 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
92 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
93 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
94 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
97 /* Swap in a Verdaux structure. */
100 _bfd_elf_swap_verdaux_in (bfd *abfd,
101 const Elf_External_Verdaux *src,
102 Elf_Internal_Verdaux *dst)
104 dst->vda_name = H_GET_32 (abfd, src->vda_name);
105 dst->vda_next = H_GET_32 (abfd, src->vda_next);
108 /* Swap out a Verdaux structure. */
111 _bfd_elf_swap_verdaux_out (bfd *abfd,
112 const Elf_Internal_Verdaux *src,
113 Elf_External_Verdaux *dst)
115 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
116 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (bfd *abfd,
123 const Elf_External_Verneed *src,
124 Elf_Internal_Verneed *dst)
126 dst->vn_version = H_GET_16 (abfd, src->vn_version);
127 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
128 dst->vn_file = H_GET_32 (abfd, src->vn_file);
129 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
130 dst->vn_next = H_GET_32 (abfd, src->vn_next);
133 /* Swap out a Verneed structure. */
136 _bfd_elf_swap_verneed_out (bfd *abfd,
137 const Elf_Internal_Verneed *src,
138 Elf_External_Verneed *dst)
140 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
141 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
142 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
143 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
144 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
147 /* Swap in a Vernaux structure. */
150 _bfd_elf_swap_vernaux_in (bfd *abfd,
151 const Elf_External_Vernaux *src,
152 Elf_Internal_Vernaux *dst)
154 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
155 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
156 dst->vna_other = H_GET_16 (abfd, src->vna_other);
157 dst->vna_name = H_GET_32 (abfd, src->vna_name);
158 dst->vna_next = H_GET_32 (abfd, src->vna_next);
161 /* Swap out a Vernaux structure. */
164 _bfd_elf_swap_vernaux_out (bfd *abfd,
165 const Elf_Internal_Vernaux *src,
166 Elf_External_Vernaux *dst)
168 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
169 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
170 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
171 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
172 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
175 /* Swap in a Versym structure. */
178 _bfd_elf_swap_versym_in (bfd *abfd,
179 const Elf_External_Versym *src,
180 Elf_Internal_Versym *dst)
182 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
185 /* Swap out a Versym structure. */
188 _bfd_elf_swap_versym_out (bfd *abfd,
189 const Elf_Internal_Versym *src,
190 Elf_External_Versym *dst)
192 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
195 /* Standard ELF hash function. Do not change this function; you will
196 cause invalid hash tables to be generated. */
199 bfd_elf_hash (const char *namearg)
201 const unsigned char *name = (const unsigned char *) namearg;
206 while ((ch = *name++) != '\0')
209 if ((g = (h & 0xf0000000)) != 0)
212 /* The ELF ABI says `h &= ~g', but this is equivalent in
213 this case and on some machines one insn instead of two. */
217 return h & 0xffffffff;
220 /* DT_GNU_HASH hash function. Do not change this function; you will
221 cause invalid hash tables to be generated. */
224 bfd_elf_gnu_hash (const char *namearg)
226 const unsigned char *name = (const unsigned char *) namearg;
227 unsigned long h = 5381;
230 while ((ch = *name++) != '\0')
231 h = (h << 5) + h + ch;
232 return h & 0xffffffff;
235 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
236 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
238 bfd_elf_allocate_object (bfd *abfd,
240 enum elf_target_id object_id)
242 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
243 abfd->tdata.any = bfd_zalloc (abfd, object_size);
244 if (abfd->tdata.any == NULL)
247 elf_object_id (abfd) = object_id;
248 if (abfd->direction != read_direction)
250 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
253 elf_tdata (abfd)->o = o;
254 elf_program_header_size (abfd) = (bfd_size_type) -1;
261 bfd_elf_make_object (bfd *abfd)
263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
264 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
269 bfd_elf_mkcorefile (bfd *abfd)
271 /* I think this can be done just like an object file. */
272 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
274 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
275 return elf_tdata (abfd)->core != NULL;
279 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
281 Elf_Internal_Shdr **i_shdrp;
282 bfd_byte *shstrtab = NULL;
284 bfd_size_type shstrtabsize;
286 i_shdrp = elf_elfsections (abfd);
288 || shindex >= elf_numsections (abfd)
289 || i_shdrp[shindex] == 0)
292 shstrtab = i_shdrp[shindex]->contents;
293 if (shstrtab == NULL)
295 /* No cached one, attempt to read, and cache what we read. */
296 offset = i_shdrp[shindex]->sh_offset;
297 shstrtabsize = i_shdrp[shindex]->sh_size;
299 /* Allocate and clear an extra byte at the end, to prevent crashes
300 in case the string table is not terminated. */
301 if (shstrtabsize + 1 <= 1
302 || shstrtabsize > bfd_get_file_size (abfd)
303 || bfd_seek (abfd, offset, SEEK_SET) != 0
304 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
306 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
308 if (bfd_get_error () != bfd_error_system_call)
309 bfd_set_error (bfd_error_file_truncated);
310 bfd_release (abfd, shstrtab);
312 /* Once we've failed to read it, make sure we don't keep
313 trying. Otherwise, we'll keep allocating space for
314 the string table over and over. */
315 i_shdrp[shindex]->sh_size = 0;
318 shstrtab[shstrtabsize] = '\0';
319 i_shdrp[shindex]->contents = shstrtab;
321 return (char *) shstrtab;
325 bfd_elf_string_from_elf_section (bfd *abfd,
326 unsigned int shindex,
327 unsigned int strindex)
329 Elf_Internal_Shdr *hdr;
334 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
337 hdr = elf_elfsections (abfd)[shindex];
339 if (hdr->contents == NULL)
341 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
343 /* PR 17512: file: f057ec89. */
344 /* xgettext:c-format */
345 _bfd_error_handler (_("%pB: attempt to load strings from"
346 " a non-string section (number %d)"),
351 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
356 /* PR 24273: The string section's contents may have already
357 been loaded elsewhere, eg because a corrupt file has the
358 string section index in the ELF header pointing at a group
359 section. So be paranoid, and test that the last byte of
360 the section is zero. */
361 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
365 if (strindex >= hdr->sh_size)
367 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
369 /* xgettext:c-format */
370 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
371 abfd, strindex, (uint64_t) hdr->sh_size,
372 (shindex == shstrndx && strindex == hdr->sh_name
374 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
378 return ((char *) hdr->contents) + strindex;
381 /* Read and convert symbols to internal format.
382 SYMCOUNT specifies the number of symbols to read, starting from
383 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
384 are non-NULL, they are used to store the internal symbols, external
385 symbols, and symbol section index extensions, respectively.
386 Returns a pointer to the internal symbol buffer (malloced if necessary)
387 or NULL if there were no symbols or some kind of problem. */
390 bfd_elf_get_elf_syms (bfd *ibfd,
391 Elf_Internal_Shdr *symtab_hdr,
394 Elf_Internal_Sym *intsym_buf,
396 Elf_External_Sym_Shndx *extshndx_buf)
398 Elf_Internal_Shdr *shndx_hdr;
400 const bfd_byte *esym;
401 Elf_External_Sym_Shndx *alloc_extshndx;
402 Elf_External_Sym_Shndx *shndx;
403 Elf_Internal_Sym *alloc_intsym;
404 Elf_Internal_Sym *isym;
405 Elf_Internal_Sym *isymend;
406 const struct elf_backend_data *bed;
411 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
417 /* Normal syms might have section extension entries. */
419 if (elf_symtab_shndx_list (ibfd) != NULL)
421 elf_section_list * entry;
422 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
424 /* Find an index section that is linked to this symtab section. */
425 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
428 if (entry->hdr.sh_link >= elf_numsections (ibfd))
431 if (sections[entry->hdr.sh_link] == symtab_hdr)
433 shndx_hdr = & entry->hdr;
438 if (shndx_hdr == NULL)
440 if (symtab_hdr == & elf_symtab_hdr (ibfd))
441 /* Not really accurate, but this was how the old code used to work. */
442 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
443 /* Otherwise we do nothing. The assumption is that
444 the index table will not be needed. */
448 /* Read the symbols. */
450 alloc_extshndx = NULL;
452 bed = get_elf_backend_data (ibfd);
453 extsym_size = bed->s->sizeof_sym;
454 amt = (bfd_size_type) symcount * extsym_size;
455 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
456 if (extsym_buf == NULL)
458 alloc_ext = bfd_malloc2 (symcount, extsym_size);
459 extsym_buf = alloc_ext;
461 if (extsym_buf == NULL
462 || bfd_seek (ibfd, pos, SEEK_SET) != 0
463 || bfd_bread (extsym_buf, amt, ibfd) != amt)
469 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
473 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
474 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
475 if (extshndx_buf == NULL)
477 alloc_extshndx = (Elf_External_Sym_Shndx *)
478 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
479 extshndx_buf = alloc_extshndx;
481 if (extshndx_buf == NULL
482 || bfd_seek (ibfd, pos, SEEK_SET) != 0
483 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
490 if (intsym_buf == NULL)
492 alloc_intsym = (Elf_Internal_Sym *)
493 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
494 intsym_buf = alloc_intsym;
495 if (intsym_buf == NULL)
499 /* Convert the symbols to internal form. */
500 isymend = intsym_buf + symcount;
501 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
502 shndx = extshndx_buf;
504 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
505 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
507 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
508 /* xgettext:c-format */
509 _bfd_error_handler (_("%pB symbol number %lu references"
510 " nonexistent SHT_SYMTAB_SHNDX section"),
511 ibfd, (unsigned long) symoffset);
512 if (alloc_intsym != NULL)
519 if (alloc_ext != NULL)
521 if (alloc_extshndx != NULL)
522 free (alloc_extshndx);
527 /* Look up a symbol name. */
529 bfd_elf_sym_name (bfd *abfd,
530 Elf_Internal_Shdr *symtab_hdr,
531 Elf_Internal_Sym *isym,
535 unsigned int iname = isym->st_name;
536 unsigned int shindex = symtab_hdr->sh_link;
538 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
539 /* Check for a bogus st_shndx to avoid crashing. */
540 && isym->st_shndx < elf_numsections (abfd))
542 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
543 shindex = elf_elfheader (abfd)->e_shstrndx;
546 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
549 else if (sym_sec && *name == '\0')
550 name = bfd_section_name (abfd, sym_sec);
555 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
556 sections. The first element is the flags, the rest are section
559 typedef union elf_internal_group {
560 Elf_Internal_Shdr *shdr;
562 } Elf_Internal_Group;
564 /* Return the name of the group signature symbol. Why isn't the
565 signature just a string? */
568 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
570 Elf_Internal_Shdr *hdr;
571 unsigned char esym[sizeof (Elf64_External_Sym)];
572 Elf_External_Sym_Shndx eshndx;
573 Elf_Internal_Sym isym;
575 /* First we need to ensure the symbol table is available. Make sure
576 that it is a symbol table section. */
577 if (ghdr->sh_link >= elf_numsections (abfd))
579 hdr = elf_elfsections (abfd) [ghdr->sh_link];
580 if (hdr->sh_type != SHT_SYMTAB
581 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
584 /* Go read the symbol. */
585 hdr = &elf_tdata (abfd)->symtab_hdr;
586 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
587 &isym, esym, &eshndx) == NULL)
590 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
593 /* Set next_in_group list pointer, and group name for NEWSECT. */
596 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
598 unsigned int num_group = elf_tdata (abfd)->num_group;
600 /* If num_group is zero, read in all SHT_GROUP sections. The count
601 is set to -1 if there are no SHT_GROUP sections. */
604 unsigned int i, shnum;
606 /* First count the number of groups. If we have a SHT_GROUP
607 section with just a flag word (ie. sh_size is 4), ignore it. */
608 shnum = elf_numsections (abfd);
611 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
612 ( (shdr)->sh_type == SHT_GROUP \
613 && (shdr)->sh_size >= minsize \
614 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
615 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
617 for (i = 0; i < shnum; i++)
619 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
621 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
627 num_group = (unsigned) -1;
628 elf_tdata (abfd)->num_group = num_group;
629 elf_tdata (abfd)->group_sect_ptr = NULL;
633 /* We keep a list of elf section headers for group sections,
634 so we can find them quickly. */
637 elf_tdata (abfd)->num_group = num_group;
638 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
639 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
640 if (elf_tdata (abfd)->group_sect_ptr == NULL)
642 memset (elf_tdata (abfd)->group_sect_ptr, 0,
643 num_group * sizeof (Elf_Internal_Shdr *));
646 for (i = 0; i < shnum; i++)
648 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
650 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
653 Elf_Internal_Group *dest;
655 /* Make sure the group section has a BFD section
657 if (!bfd_section_from_shdr (abfd, i))
660 /* Add to list of sections. */
661 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest) >= 4);
666 amt = shdr->sh_size * sizeof (*dest) / 4;
667 shdr->contents = (unsigned char *)
668 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
669 /* PR binutils/4110: Handle corrupt group headers. */
670 if (shdr->contents == NULL)
673 /* xgettext:c-format */
674 (_("%pB: corrupt size field in group section"
675 " header: %#" PRIx64),
676 abfd, (uint64_t) shdr->sh_size);
677 bfd_set_error (bfd_error_bad_value);
682 memset (shdr->contents, 0, amt);
684 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
685 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
689 /* xgettext:c-format */
690 (_("%pB: invalid size field in group section"
691 " header: %#" PRIx64 ""),
692 abfd, (uint64_t) shdr->sh_size);
693 bfd_set_error (bfd_error_bad_value);
695 /* PR 17510: If the group contents are even
696 partially corrupt, do not allow any of the
697 contents to be used. */
698 memset (shdr->contents, 0, amt);
702 /* Translate raw contents, a flag word followed by an
703 array of elf section indices all in target byte order,
704 to the flag word followed by an array of elf section
706 src = shdr->contents + shdr->sh_size;
707 dest = (Elf_Internal_Group *) (shdr->contents + amt);
715 idx = H_GET_32 (abfd, src);
716 if (src == shdr->contents)
719 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
720 shdr->bfd_section->flags
721 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
726 dest->shdr = elf_elfsections (abfd)[idx];
727 /* PR binutils/23199: All sections in a
728 section group should be marked with
729 SHF_GROUP. But some tools generate
730 broken objects without SHF_GROUP. Fix
732 dest->shdr->sh_flags |= SHF_GROUP;
735 || dest->shdr->sh_type == SHT_GROUP)
738 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
746 /* PR 17510: Corrupt binaries might contain invalid groups. */
747 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
749 elf_tdata (abfd)->num_group = num_group;
751 /* If all groups are invalid then fail. */
754 elf_tdata (abfd)->group_sect_ptr = NULL;
755 elf_tdata (abfd)->num_group = num_group = -1;
757 (_("%pB: no valid group sections found"), abfd);
758 bfd_set_error (bfd_error_bad_value);
764 if (num_group != (unsigned) -1)
766 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
769 for (j = 0; j < num_group; j++)
771 /* Begin search from previous found group. */
772 unsigned i = (j + search_offset) % num_group;
774 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
775 Elf_Internal_Group *idx;
781 idx = (Elf_Internal_Group *) shdr->contents;
782 if (idx == NULL || shdr->sh_size < 4)
784 /* See PR 21957 for a reproducer. */
785 /* xgettext:c-format */
786 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
787 abfd, shdr->bfd_section);
788 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
789 bfd_set_error (bfd_error_bad_value);
792 n_elt = shdr->sh_size / 4;
794 /* Look through this group's sections to see if current
795 section is a member. */
797 if ((++idx)->shdr == hdr)
801 /* We are a member of this group. Go looking through
802 other members to see if any others are linked via
804 idx = (Elf_Internal_Group *) shdr->contents;
805 n_elt = shdr->sh_size / 4;
807 if ((++idx)->shdr != NULL
808 && (s = idx->shdr->bfd_section) != NULL
809 && elf_next_in_group (s) != NULL)
813 /* Snarf the group name from other member, and
814 insert current section in circular list. */
815 elf_group_name (newsect) = elf_group_name (s);
816 elf_next_in_group (newsect) = elf_next_in_group (s);
817 elf_next_in_group (s) = newsect;
823 gname = group_signature (abfd, shdr);
826 elf_group_name (newsect) = gname;
828 /* Start a circular list with one element. */
829 elf_next_in_group (newsect) = newsect;
832 /* If the group section has been created, point to the
834 if (shdr->bfd_section != NULL)
835 elf_next_in_group (shdr->bfd_section) = newsect;
837 elf_tdata (abfd)->group_search_offset = i;
844 if (elf_group_name (newsect) == NULL)
846 /* xgettext:c-format */
847 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
855 _bfd_elf_setup_sections (bfd *abfd)
858 unsigned int num_group = elf_tdata (abfd)->num_group;
859 bfd_boolean result = TRUE;
862 /* Process SHF_LINK_ORDER. */
863 for (s = abfd->sections; s != NULL; s = s->next)
865 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
866 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
868 unsigned int elfsec = this_hdr->sh_link;
869 /* FIXME: The old Intel compiler and old strip/objcopy may
870 not set the sh_link or sh_info fields. Hence we could
871 get the situation where elfsec is 0. */
874 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
875 if (bed->link_order_error_handler)
876 bed->link_order_error_handler
877 /* xgettext:c-format */
878 (_("%pB: warning: sh_link not set for section `%pA'"),
883 asection *linksec = NULL;
885 if (elfsec < elf_numsections (abfd))
887 this_hdr = elf_elfsections (abfd)[elfsec];
888 linksec = this_hdr->bfd_section;
892 Some strip/objcopy may leave an incorrect value in
893 sh_link. We don't want to proceed. */
897 /* xgettext:c-format */
898 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
899 s->owner, elfsec, s);
903 elf_linked_to_section (s) = linksec;
906 else if (this_hdr->sh_type == SHT_GROUP
907 && elf_next_in_group (s) == NULL)
910 /* xgettext:c-format */
911 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
912 abfd, elf_section_data (s)->this_idx);
917 /* Process section groups. */
918 if (num_group == (unsigned) -1)
921 for (i = 0; i < num_group; i++)
923 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
924 Elf_Internal_Group *idx;
927 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
928 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
931 /* xgettext:c-format */
932 (_("%pB: section group entry number %u is corrupt"),
938 idx = (Elf_Internal_Group *) shdr->contents;
939 n_elt = shdr->sh_size / 4;
945 if (idx->shdr == NULL)
947 else if (idx->shdr->bfd_section)
948 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
949 else if (idx->shdr->sh_type != SHT_RELA
950 && idx->shdr->sh_type != SHT_REL)
952 /* There are some unknown sections in the group. */
954 /* xgettext:c-format */
955 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
958 bfd_elf_string_from_elf_section (abfd,
959 (elf_elfheader (abfd)
972 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
974 return elf_next_in_group (sec) != NULL;
978 convert_debug_to_zdebug (bfd *abfd, const char *name)
980 unsigned int len = strlen (name);
981 char *new_name = bfd_alloc (abfd, len + 2);
982 if (new_name == NULL)
986 memcpy (new_name + 2, name + 1, len);
991 convert_zdebug_to_debug (bfd *abfd, const char *name)
993 unsigned int len = strlen (name);
994 char *new_name = bfd_alloc (abfd, len);
995 if (new_name == NULL)
998 memcpy (new_name + 1, name + 2, len - 1);
1002 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
1006 int16_t major_version;
1007 int16_t minor_version;
1008 unsigned char slim_object;
1010 /* Flags is a private field that is not defined publicly. */
1014 /* Make a BFD section from an ELF section. We store a pointer to the
1015 BFD section in the bfd_section field of the header. */
1018 _bfd_elf_make_section_from_shdr (bfd *abfd,
1019 Elf_Internal_Shdr *hdr,
1025 const struct elf_backend_data *bed;
1027 if (hdr->bfd_section != NULL)
1030 newsect = bfd_make_section_anyway (abfd, name);
1031 if (newsect == NULL)
1034 hdr->bfd_section = newsect;
1035 elf_section_data (newsect)->this_hdr = *hdr;
1036 elf_section_data (newsect)->this_idx = shindex;
1038 /* Always use the real type/flags. */
1039 elf_section_type (newsect) = hdr->sh_type;
1040 elf_section_flags (newsect) = hdr->sh_flags;
1042 newsect->filepos = hdr->sh_offset;
1044 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1045 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1046 || ! bfd_set_section_alignment (abfd, newsect,
1047 bfd_log2 (hdr->sh_addralign)))
1050 flags = SEC_NO_FLAGS;
1051 if (hdr->sh_type != SHT_NOBITS)
1052 flags |= SEC_HAS_CONTENTS;
1053 if (hdr->sh_type == SHT_GROUP)
1055 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1058 if (hdr->sh_type != SHT_NOBITS)
1061 if ((hdr->sh_flags & SHF_WRITE) == 0)
1062 flags |= SEC_READONLY;
1063 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1065 else if ((flags & SEC_LOAD) != 0)
1067 if ((hdr->sh_flags & SHF_MERGE) != 0)
1070 newsect->entsize = hdr->sh_entsize;
1072 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1073 flags |= SEC_STRINGS;
1074 if (hdr->sh_flags & SHF_GROUP)
1075 if (!setup_group (abfd, hdr, newsect))
1077 if ((hdr->sh_flags & SHF_TLS) != 0)
1078 flags |= SEC_THREAD_LOCAL;
1079 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1080 flags |= SEC_EXCLUDE;
1082 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1084 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1085 but binutils as of 2019-07-23 did not set the EI_OSABI header
1089 case ELFOSABI_FREEBSD:
1090 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1091 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1095 if ((flags & SEC_ALLOC) == 0)
1097 /* The debugging sections appear to be recognized only by name,
1098 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1099 if (name [0] == '.')
1104 p = ".debug", n = 6;
1105 else if (name[1] == 'g' && name[2] == 'n')
1106 p = ".gnu.linkonce.wi.", n = 17;
1107 else if (name[1] == 'g' && name[2] == 'd')
1108 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1109 else if (name[1] == 'l')
1111 else if (name[1] == 's')
1113 else if (name[1] == 'z')
1114 p = ".zdebug", n = 7;
1117 if (p != NULL && strncmp (name, p, n) == 0)
1118 flags |= SEC_DEBUGGING;
1122 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1123 only link a single copy of the section. This is used to support
1124 g++. g++ will emit each template expansion in its own section.
1125 The symbols will be defined as weak, so that multiple definitions
1126 are permitted. The GNU linker extension is to actually discard
1127 all but one of the sections. */
1128 if (CONST_STRNEQ (name, ".gnu.linkonce")
1129 && elf_next_in_group (newsect) == NULL)
1130 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1132 bed = get_elf_backend_data (abfd);
1133 if (bed->elf_backend_section_flags)
1134 if (! bed->elf_backend_section_flags (&flags, hdr))
1137 if (! bfd_set_section_flags (abfd, newsect, flags))
1140 /* We do not parse the PT_NOTE segments as we are interested even in the
1141 separate debug info files which may have the segments offsets corrupted.
1142 PT_NOTEs from the core files are currently not parsed using BFD. */
1143 if (hdr->sh_type == SHT_NOTE)
1147 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1150 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1151 hdr->sh_offset, hdr->sh_addralign);
1155 if ((flags & SEC_ALLOC) != 0)
1157 Elf_Internal_Phdr *phdr;
1158 unsigned int i, nload;
1160 /* Some ELF linkers produce binaries with all the program header
1161 p_paddr fields zero. If we have such a binary with more than
1162 one PT_LOAD header, then leave the section lma equal to vma
1163 so that we don't create sections with overlapping lma. */
1164 phdr = elf_tdata (abfd)->phdr;
1165 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1166 if (phdr->p_paddr != 0)
1168 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1170 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1173 phdr = elf_tdata (abfd)->phdr;
1174 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1176 if (((phdr->p_type == PT_LOAD
1177 && (hdr->sh_flags & SHF_TLS) == 0)
1178 || phdr->p_type == PT_TLS)
1179 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1181 if ((flags & SEC_LOAD) == 0)
1182 newsect->lma = (phdr->p_paddr
1183 + hdr->sh_addr - phdr->p_vaddr);
1185 /* We used to use the same adjustment for SEC_LOAD
1186 sections, but that doesn't work if the segment
1187 is packed with code from multiple VMAs.
1188 Instead we calculate the section LMA based on
1189 the segment LMA. It is assumed that the
1190 segment will contain sections with contiguous
1191 LMAs, even if the VMAs are not. */
1192 newsect->lma = (phdr->p_paddr
1193 + hdr->sh_offset - phdr->p_offset);
1195 /* With contiguous segments, we can't tell from file
1196 offsets whether a section with zero size should
1197 be placed at the end of one segment or the
1198 beginning of the next. Decide based on vaddr. */
1199 if (hdr->sh_addr >= phdr->p_vaddr
1200 && (hdr->sh_addr + hdr->sh_size
1201 <= phdr->p_vaddr + phdr->p_memsz))
1207 /* Compress/decompress DWARF debug sections with names: .debug_* and
1208 .zdebug_*, after the section flags is set. */
1209 if ((flags & SEC_DEBUGGING)
1210 && ((name[1] == 'd' && name[6] == '_')
1211 || (name[1] == 'z' && name[7] == '_')))
1213 enum { nothing, compress, decompress } action = nothing;
1214 int compression_header_size;
1215 bfd_size_type uncompressed_size;
1216 unsigned int uncompressed_align_power;
1217 bfd_boolean compressed
1218 = bfd_is_section_compressed_with_header (abfd, newsect,
1219 &compression_header_size,
1221 &uncompressed_align_power);
1224 /* Compressed section. Check if we should decompress. */
1225 if ((abfd->flags & BFD_DECOMPRESS))
1226 action = decompress;
1229 /* Compress the uncompressed section or convert from/to .zdebug*
1230 section. Check if we should compress. */
1231 if (action == nothing)
1233 if (newsect->size != 0
1234 && (abfd->flags & BFD_COMPRESS)
1235 && compression_header_size >= 0
1236 && uncompressed_size > 0
1238 || ((compression_header_size > 0)
1239 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1245 if (action == compress)
1247 if (!bfd_init_section_compress_status (abfd, newsect))
1250 /* xgettext:c-format */
1251 (_("%pB: unable to initialize compress status for section %s"),
1258 if (!bfd_init_section_decompress_status (abfd, newsect))
1261 /* xgettext:c-format */
1262 (_("%pB: unable to initialize decompress status for section %s"),
1268 if (abfd->is_linker_input)
1271 && (action == decompress
1272 || (action == compress
1273 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1275 /* Convert section name from .zdebug_* to .debug_* so
1276 that linker will consider this section as a debug
1278 char *new_name = convert_zdebug_to_debug (abfd, name);
1279 if (new_name == NULL)
1281 bfd_rename_section (abfd, newsect, new_name);
1285 /* For objdump, don't rename the section. For objcopy, delay
1286 section rename to elf_fake_sections. */
1287 newsect->flags |= SEC_ELF_RENAME;
1290 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1292 const char *lto_section_name = ".gnu.lto_.lto.";
1293 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1295 struct lto_section lsection;
1296 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1297 sizeof (struct lto_section)))
1298 abfd->lto_slim_object = lsection.slim_object;
1304 const char *const bfd_elf_section_type_names[] =
1306 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1307 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1308 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1311 /* ELF relocs are against symbols. If we are producing relocatable
1312 output, and the reloc is against an external symbol, and nothing
1313 has given us any additional addend, the resulting reloc will also
1314 be against the same symbol. In such a case, we don't want to
1315 change anything about the way the reloc is handled, since it will
1316 all be done at final link time. Rather than put special case code
1317 into bfd_perform_relocation, all the reloc types use this howto
1318 function. It just short circuits the reloc if producing
1319 relocatable output against an external symbol. */
1321 bfd_reloc_status_type
1322 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1323 arelent *reloc_entry,
1325 void *data ATTRIBUTE_UNUSED,
1326 asection *input_section,
1328 char **error_message ATTRIBUTE_UNUSED)
1330 if (output_bfd != NULL
1331 && (symbol->flags & BSF_SECTION_SYM) == 0
1332 && (! reloc_entry->howto->partial_inplace
1333 || reloc_entry->addend == 0))
1335 reloc_entry->address += input_section->output_offset;
1336 return bfd_reloc_ok;
1339 return bfd_reloc_continue;
1342 /* Returns TRUE if section A matches section B.
1343 Names, addresses and links may be different, but everything else
1344 should be the same. */
1347 section_match (const Elf_Internal_Shdr * a,
1348 const Elf_Internal_Shdr * b)
1350 if (a->sh_type != b->sh_type
1351 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1352 || a->sh_addralign != b->sh_addralign
1353 || a->sh_entsize != b->sh_entsize)
1355 if (a->sh_type == SHT_SYMTAB
1356 || a->sh_type == SHT_STRTAB)
1358 return a->sh_size == b->sh_size;
1361 /* Find a section in OBFD that has the same characteristics
1362 as IHEADER. Return the index of this section or SHN_UNDEF if
1363 none can be found. Check's section HINT first, as this is likely
1364 to be the correct section. */
1367 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1368 const unsigned int hint)
1370 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1373 BFD_ASSERT (iheader != NULL);
1375 /* See PR 20922 for a reproducer of the NULL test. */
1376 if (hint < elf_numsections (obfd)
1377 && oheaders[hint] != NULL
1378 && section_match (oheaders[hint], iheader))
1381 for (i = 1; i < elf_numsections (obfd); i++)
1383 Elf_Internal_Shdr * oheader = oheaders[i];
1385 if (oheader == NULL)
1387 if (section_match (oheader, iheader))
1388 /* FIXME: Do we care if there is a potential for
1389 multiple matches ? */
1396 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1397 Processor specific section, based upon a matching input section.
1398 Returns TRUE upon success, FALSE otherwise. */
1401 copy_special_section_fields (const bfd *ibfd,
1403 const Elf_Internal_Shdr *iheader,
1404 Elf_Internal_Shdr *oheader,
1405 const unsigned int secnum)
1407 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1408 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1409 bfd_boolean changed = FALSE;
1410 unsigned int sh_link;
1412 if (oheader->sh_type == SHT_NOBITS)
1414 /* This is a feature for objcopy --only-keep-debug:
1415 When a section's type is changed to NOBITS, we preserve
1416 the sh_link and sh_info fields so that they can be
1417 matched up with the original.
1419 Note: Strictly speaking these assignments are wrong.
1420 The sh_link and sh_info fields should point to the
1421 relevent sections in the output BFD, which may not be in
1422 the same location as they were in the input BFD. But
1423 the whole point of this action is to preserve the
1424 original values of the sh_link and sh_info fields, so
1425 that they can be matched up with the section headers in
1426 the original file. So strictly speaking we may be
1427 creating an invalid ELF file, but it is only for a file
1428 that just contains debug info and only for sections
1429 without any contents. */
1430 if (oheader->sh_link == 0)
1431 oheader->sh_link = iheader->sh_link;
1432 if (oheader->sh_info == 0)
1433 oheader->sh_info = iheader->sh_info;
1437 /* Allow the target a chance to decide how these fields should be set. */
1438 if (bed->elf_backend_copy_special_section_fields != NULL
1439 && bed->elf_backend_copy_special_section_fields
1440 (ibfd, obfd, iheader, oheader))
1443 /* We have an iheader which might match oheader, and which has non-zero
1444 sh_info and/or sh_link fields. Attempt to follow those links and find
1445 the section in the output bfd which corresponds to the linked section
1446 in the input bfd. */
1447 if (iheader->sh_link != SHN_UNDEF)
1449 /* See PR 20931 for a reproducer. */
1450 if (iheader->sh_link >= elf_numsections (ibfd))
1453 /* xgettext:c-format */
1454 (_("%pB: invalid sh_link field (%d) in section number %d"),
1455 ibfd, iheader->sh_link, secnum);
1459 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1460 if (sh_link != SHN_UNDEF)
1462 oheader->sh_link = sh_link;
1466 /* FIXME: Should we install iheader->sh_link
1467 if we could not find a match ? */
1469 /* xgettext:c-format */
1470 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1473 if (iheader->sh_info)
1475 /* The sh_info field can hold arbitrary information, but if the
1476 SHF_LINK_INFO flag is set then it should be interpreted as a
1478 if (iheader->sh_flags & SHF_INFO_LINK)
1480 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1482 if (sh_link != SHN_UNDEF)
1483 oheader->sh_flags |= SHF_INFO_LINK;
1486 /* No idea what it means - just copy it. */
1487 sh_link = iheader->sh_info;
1489 if (sh_link != SHN_UNDEF)
1491 oheader->sh_info = sh_link;
1496 /* xgettext:c-format */
1497 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1503 /* Copy the program header and other data from one object module to
1507 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1509 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1510 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1511 const struct elf_backend_data *bed;
1514 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1515 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1518 if (!elf_flags_init (obfd))
1520 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1521 elf_flags_init (obfd) = TRUE;
1524 elf_gp (obfd) = elf_gp (ibfd);
1526 /* Also copy the EI_OSABI field. */
1527 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1528 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1530 /* If set, copy the EI_ABIVERSION field. */
1531 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1532 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1533 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1535 /* Copy object attributes. */
1536 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1538 if (iheaders == NULL || oheaders == NULL)
1541 bed = get_elf_backend_data (obfd);
1543 /* Possibly copy other fields in the section header. */
1544 for (i = 1; i < elf_numsections (obfd); i++)
1547 Elf_Internal_Shdr * oheader = oheaders[i];
1549 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1550 because of a special case need for generating separate debug info
1551 files. See below for more details. */
1553 || (oheader->sh_type != SHT_NOBITS
1554 && oheader->sh_type < SHT_LOOS))
1557 /* Ignore empty sections, and sections whose
1558 fields have already been initialised. */
1559 if (oheader->sh_size == 0
1560 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1563 /* Scan for the matching section in the input bfd.
1564 First we try for a direct mapping between the input and output sections. */
1565 for (j = 1; j < elf_numsections (ibfd); j++)
1567 const Elf_Internal_Shdr * iheader = iheaders[j];
1569 if (iheader == NULL)
1572 if (oheader->bfd_section != NULL
1573 && iheader->bfd_section != NULL
1574 && iheader->bfd_section->output_section != NULL
1575 && iheader->bfd_section->output_section == oheader->bfd_section)
1577 /* We have found a connection from the input section to the
1578 output section. Attempt to copy the header fields. If
1579 this fails then do not try any further sections - there
1580 should only be a one-to-one mapping between input and output. */
1581 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1582 j = elf_numsections (ibfd);
1587 if (j < elf_numsections (ibfd))
1590 /* That failed. So try to deduce the corresponding input section.
1591 Unfortunately we cannot compare names as the output string table
1592 is empty, so instead we check size, address and type. */
1593 for (j = 1; j < elf_numsections (ibfd); j++)
1595 const Elf_Internal_Shdr * iheader = iheaders[j];
1597 if (iheader == NULL)
1600 /* Try matching fields in the input section's header.
1601 Since --only-keep-debug turns all non-debug sections into
1602 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1604 if ((oheader->sh_type == SHT_NOBITS
1605 || iheader->sh_type == oheader->sh_type)
1606 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1607 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1608 && iheader->sh_addralign == oheader->sh_addralign
1609 && iheader->sh_entsize == oheader->sh_entsize
1610 && iheader->sh_size == oheader->sh_size
1611 && iheader->sh_addr == oheader->sh_addr
1612 && (iheader->sh_info != oheader->sh_info
1613 || iheader->sh_link != oheader->sh_link))
1615 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1620 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1622 /* Final attempt. Call the backend copy function
1623 with a NULL input section. */
1624 if (bed->elf_backend_copy_special_section_fields != NULL)
1625 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1633 get_segment_type (unsigned int p_type)
1638 case PT_NULL: pt = "NULL"; break;
1639 case PT_LOAD: pt = "LOAD"; break;
1640 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1641 case PT_INTERP: pt = "INTERP"; break;
1642 case PT_NOTE: pt = "NOTE"; break;
1643 case PT_SHLIB: pt = "SHLIB"; break;
1644 case PT_PHDR: pt = "PHDR"; break;
1645 case PT_TLS: pt = "TLS"; break;
1646 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1647 case PT_GNU_STACK: pt = "STACK"; break;
1648 case PT_GNU_RELRO: pt = "RELRO"; break;
1649 default: pt = NULL; break;
1654 /* Print out the program headers. */
1657 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1659 FILE *f = (FILE *) farg;
1660 Elf_Internal_Phdr *p;
1662 bfd_byte *dynbuf = NULL;
1664 p = elf_tdata (abfd)->phdr;
1669 fprintf (f, _("\nProgram Header:\n"));
1670 c = elf_elfheader (abfd)->e_phnum;
1671 for (i = 0; i < c; i++, p++)
1673 const char *pt = get_segment_type (p->p_type);
1678 sprintf (buf, "0x%lx", p->p_type);
1681 fprintf (f, "%8s off 0x", pt);
1682 bfd_fprintf_vma (abfd, f, p->p_offset);
1683 fprintf (f, " vaddr 0x");
1684 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1685 fprintf (f, " paddr 0x");
1686 bfd_fprintf_vma (abfd, f, p->p_paddr);
1687 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1688 fprintf (f, " filesz 0x");
1689 bfd_fprintf_vma (abfd, f, p->p_filesz);
1690 fprintf (f, " memsz 0x");
1691 bfd_fprintf_vma (abfd, f, p->p_memsz);
1692 fprintf (f, " flags %c%c%c",
1693 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1694 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1695 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1696 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1697 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1702 s = bfd_get_section_by_name (abfd, ".dynamic");
1705 unsigned int elfsec;
1706 unsigned long shlink;
1707 bfd_byte *extdyn, *extdynend;
1709 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1711 fprintf (f, _("\nDynamic Section:\n"));
1713 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1716 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1717 if (elfsec == SHN_BAD)
1719 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1721 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1722 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1725 /* PR 17512: file: 6f427532. */
1726 if (s->size < extdynsize)
1728 extdynend = extdyn + s->size;
1729 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1731 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1733 Elf_Internal_Dyn dyn;
1734 const char *name = "";
1736 bfd_boolean stringp;
1737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1739 (*swap_dyn_in) (abfd, extdyn, &dyn);
1741 if (dyn.d_tag == DT_NULL)
1748 if (bed->elf_backend_get_target_dtag)
1749 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1751 if (!strcmp (name, ""))
1753 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1758 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1759 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1760 case DT_PLTGOT: name = "PLTGOT"; break;
1761 case DT_HASH: name = "HASH"; break;
1762 case DT_STRTAB: name = "STRTAB"; break;
1763 case DT_SYMTAB: name = "SYMTAB"; break;
1764 case DT_RELA: name = "RELA"; break;
1765 case DT_RELASZ: name = "RELASZ"; break;
1766 case DT_RELAENT: name = "RELAENT"; break;
1767 case DT_STRSZ: name = "STRSZ"; break;
1768 case DT_SYMENT: name = "SYMENT"; break;
1769 case DT_INIT: name = "INIT"; break;
1770 case DT_FINI: name = "FINI"; break;
1771 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1772 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1773 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1774 case DT_REL: name = "REL"; break;
1775 case DT_RELSZ: name = "RELSZ"; break;
1776 case DT_RELENT: name = "RELENT"; break;
1777 case DT_PLTREL: name = "PLTREL"; break;
1778 case DT_DEBUG: name = "DEBUG"; break;
1779 case DT_TEXTREL: name = "TEXTREL"; break;
1780 case DT_JMPREL: name = "JMPREL"; break;
1781 case DT_BIND_NOW: name = "BIND_NOW"; break;
1782 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1783 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1784 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1785 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1786 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1787 case DT_FLAGS: name = "FLAGS"; break;
1788 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1789 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1790 case DT_CHECKSUM: name = "CHECKSUM"; break;
1791 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1792 case DT_MOVEENT: name = "MOVEENT"; break;
1793 case DT_MOVESZ: name = "MOVESZ"; break;
1794 case DT_FEATURE: name = "FEATURE"; break;
1795 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1796 case DT_SYMINSZ: name = "SYMINSZ"; break;
1797 case DT_SYMINENT: name = "SYMINENT"; break;
1798 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1799 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1800 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1801 case DT_PLTPAD: name = "PLTPAD"; break;
1802 case DT_MOVETAB: name = "MOVETAB"; break;
1803 case DT_SYMINFO: name = "SYMINFO"; break;
1804 case DT_RELACOUNT: name = "RELACOUNT"; break;
1805 case DT_RELCOUNT: name = "RELCOUNT"; break;
1806 case DT_FLAGS_1: name = "FLAGS_1"; break;
1807 case DT_VERSYM: name = "VERSYM"; break;
1808 case DT_VERDEF: name = "VERDEF"; break;
1809 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1810 case DT_VERNEED: name = "VERNEED"; break;
1811 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1812 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1813 case DT_USED: name = "USED"; break;
1814 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1815 case DT_GNU_HASH: name = "GNU_HASH"; break;
1818 fprintf (f, " %-20s ", name);
1822 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1827 unsigned int tagv = dyn.d_un.d_val;
1829 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1832 fprintf (f, "%s", string);
1841 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1842 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1844 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1848 if (elf_dynverdef (abfd) != 0)
1850 Elf_Internal_Verdef *t;
1852 fprintf (f, _("\nVersion definitions:\n"));
1853 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1855 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1856 t->vd_flags, t->vd_hash,
1857 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1858 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1860 Elf_Internal_Verdaux *a;
1863 for (a = t->vd_auxptr->vda_nextptr;
1867 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1873 if (elf_dynverref (abfd) != 0)
1875 Elf_Internal_Verneed *t;
1877 fprintf (f, _("\nVersion References:\n"));
1878 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1880 Elf_Internal_Vernaux *a;
1882 fprintf (f, _(" required from %s:\n"),
1883 t->vn_filename ? t->vn_filename : "<corrupt>");
1884 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1885 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1886 a->vna_flags, a->vna_other,
1887 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1899 /* Get version string. */
1902 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1903 bfd_boolean *hidden)
1905 const char *version_string = NULL;
1906 if (elf_dynversym (abfd) != 0
1907 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1909 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1911 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1912 vernum &= VERSYM_VERSION;
1915 version_string = "";
1916 else if (vernum == 1
1917 && (vernum > elf_tdata (abfd)->cverdefs
1918 || (elf_tdata (abfd)->verdef[0].vd_flags
1920 version_string = "Base";
1921 else if (vernum <= elf_tdata (abfd)->cverdefs)
1923 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1926 Elf_Internal_Verneed *t;
1928 version_string = _("<corrupt>");
1929 for (t = elf_tdata (abfd)->verref;
1933 Elf_Internal_Vernaux *a;
1935 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1937 if (a->vna_other == vernum)
1939 version_string = a->vna_nodename;
1946 return version_string;
1949 /* Display ELF-specific fields of a symbol. */
1952 bfd_elf_print_symbol (bfd *abfd,
1955 bfd_print_symbol_type how)
1957 FILE *file = (FILE *) filep;
1960 case bfd_print_symbol_name:
1961 fprintf (file, "%s", symbol->name);
1963 case bfd_print_symbol_more:
1964 fprintf (file, "elf ");
1965 bfd_fprintf_vma (abfd, file, symbol->value);
1966 fprintf (file, " %x", symbol->flags);
1968 case bfd_print_symbol_all:
1970 const char *section_name;
1971 const char *name = NULL;
1972 const struct elf_backend_data *bed;
1973 unsigned char st_other;
1975 const char *version_string;
1978 section_name = symbol->section ? symbol->section->name : "(*none*)";
1980 bed = get_elf_backend_data (abfd);
1981 if (bed->elf_backend_print_symbol_all)
1982 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1986 name = symbol->name;
1987 bfd_print_symbol_vandf (abfd, file, symbol);
1990 fprintf (file, " %s\t", section_name);
1991 /* Print the "other" value for a symbol. For common symbols,
1992 we've already printed the size; now print the alignment.
1993 For other symbols, we have no specified alignment, and
1994 we've printed the address; now print the size. */
1995 if (symbol->section && bfd_is_com_section (symbol->section))
1996 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1998 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1999 bfd_fprintf_vma (abfd, file, val);
2001 /* If we have version information, print it. */
2002 version_string = _bfd_elf_get_symbol_version_string (abfd,
2008 fprintf (file, " %-11s", version_string);
2013 fprintf (file, " (%s)", version_string);
2014 for (i = 10 - strlen (version_string); i > 0; --i)
2019 /* If the st_other field is not zero, print it. */
2020 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2025 case STV_INTERNAL: fprintf (file, " .internal"); break;
2026 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2027 case STV_PROTECTED: fprintf (file, " .protected"); break;
2029 /* Some other non-defined flags are also present, so print
2031 fprintf (file, " 0x%02x", (unsigned int) st_other);
2034 fprintf (file, " %s", name);
2040 /* ELF .o/exec file reading */
2042 /* Create a new bfd section from an ELF section header. */
2045 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2047 Elf_Internal_Shdr *hdr;
2048 Elf_Internal_Ehdr *ehdr;
2049 const struct elf_backend_data *bed;
2051 bfd_boolean ret = TRUE;
2052 static bfd_boolean * sections_being_created = NULL;
2053 static bfd * sections_being_created_abfd = NULL;
2054 static unsigned int nesting = 0;
2056 if (shindex >= elf_numsections (abfd))
2061 /* PR17512: A corrupt ELF binary might contain a recursive group of
2062 sections, with each the string indices pointing to the next in the
2063 loop. Detect this here, by refusing to load a section that we are
2064 already in the process of loading. We only trigger this test if
2065 we have nested at least three sections deep as normal ELF binaries
2066 can expect to recurse at least once.
2068 FIXME: It would be better if this array was attached to the bfd,
2069 rather than being held in a static pointer. */
2071 if (sections_being_created_abfd != abfd)
2072 sections_being_created = NULL;
2073 if (sections_being_created == NULL)
2075 sections_being_created = (bfd_boolean *)
2076 bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean));
2077 sections_being_created_abfd = abfd;
2079 if (sections_being_created [shindex])
2082 (_("%pB: warning: loop in section dependencies detected"), abfd);
2085 sections_being_created [shindex] = TRUE;
2088 hdr = elf_elfsections (abfd)[shindex];
2089 ehdr = elf_elfheader (abfd);
2090 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2095 bed = get_elf_backend_data (abfd);
2096 switch (hdr->sh_type)
2099 /* Inactive section. Throw it away. */
2102 case SHT_PROGBITS: /* Normal section with contents. */
2103 case SHT_NOBITS: /* .bss section. */
2104 case SHT_HASH: /* .hash section. */
2105 case SHT_NOTE: /* .note section. */
2106 case SHT_INIT_ARRAY: /* .init_array section. */
2107 case SHT_FINI_ARRAY: /* .fini_array section. */
2108 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2109 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2110 case SHT_GNU_HASH: /* .gnu.hash section. */
2111 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2114 case SHT_DYNAMIC: /* Dynamic linking information. */
2115 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2118 if (hdr->sh_link > elf_numsections (abfd))
2120 /* PR 10478: Accept Solaris binaries with a sh_link
2121 field set to SHN_BEFORE or SHN_AFTER. */
2122 switch (bfd_get_arch (abfd))
2125 case bfd_arch_sparc:
2126 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2127 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2129 /* Otherwise fall through. */
2134 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2136 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2138 Elf_Internal_Shdr *dynsymhdr;
2140 /* The shared libraries distributed with hpux11 have a bogus
2141 sh_link field for the ".dynamic" section. Find the
2142 string table for the ".dynsym" section instead. */
2143 if (elf_dynsymtab (abfd) != 0)
2145 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2146 hdr->sh_link = dynsymhdr->sh_link;
2150 unsigned int i, num_sec;
2152 num_sec = elf_numsections (abfd);
2153 for (i = 1; i < num_sec; i++)
2155 dynsymhdr = elf_elfsections (abfd)[i];
2156 if (dynsymhdr->sh_type == SHT_DYNSYM)
2158 hdr->sh_link = dynsymhdr->sh_link;
2166 case SHT_SYMTAB: /* A symbol table. */
2167 if (elf_onesymtab (abfd) == shindex)
2170 if (hdr->sh_entsize != bed->s->sizeof_sym)
2173 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2175 if (hdr->sh_size != 0)
2177 /* Some assemblers erroneously set sh_info to one with a
2178 zero sh_size. ld sees this as a global symbol count
2179 of (unsigned) -1. Fix it here. */
2184 /* PR 18854: A binary might contain more than one symbol table.
2185 Unusual, but possible. Warn, but continue. */
2186 if (elf_onesymtab (abfd) != 0)
2189 /* xgettext:c-format */
2190 (_("%pB: warning: multiple symbol tables detected"
2191 " - ignoring the table in section %u"),
2195 elf_onesymtab (abfd) = shindex;
2196 elf_symtab_hdr (abfd) = *hdr;
2197 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2198 abfd->flags |= HAS_SYMS;
2200 /* Sometimes a shared object will map in the symbol table. If
2201 SHF_ALLOC is set, and this is a shared object, then we also
2202 treat this section as a BFD section. We can not base the
2203 decision purely on SHF_ALLOC, because that flag is sometimes
2204 set in a relocatable object file, which would confuse the
2206 if ((hdr->sh_flags & SHF_ALLOC) != 0
2207 && (abfd->flags & DYNAMIC) != 0
2208 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2212 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2213 can't read symbols without that section loaded as well. It
2214 is most likely specified by the next section header. */
2216 elf_section_list * entry;
2217 unsigned int i, num_sec;
2219 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2220 if (entry->hdr.sh_link == shindex)
2223 num_sec = elf_numsections (abfd);
2224 for (i = shindex + 1; i < num_sec; i++)
2226 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2228 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2229 && hdr2->sh_link == shindex)
2234 for (i = 1; i < shindex; i++)
2236 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2238 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2239 && hdr2->sh_link == shindex)
2244 ret = bfd_section_from_shdr (abfd, i);
2245 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2249 case SHT_DYNSYM: /* A dynamic symbol table. */
2250 if (elf_dynsymtab (abfd) == shindex)
2253 if (hdr->sh_entsize != bed->s->sizeof_sym)
2256 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2258 if (hdr->sh_size != 0)
2261 /* Some linkers erroneously set sh_info to one with a
2262 zero sh_size. ld sees this as a global symbol count
2263 of (unsigned) -1. Fix it here. */
2268 /* PR 18854: A binary might contain more than one dynamic symbol table.
2269 Unusual, but possible. Warn, but continue. */
2270 if (elf_dynsymtab (abfd) != 0)
2273 /* xgettext:c-format */
2274 (_("%pB: warning: multiple dynamic symbol tables detected"
2275 " - ignoring the table in section %u"),
2279 elf_dynsymtab (abfd) = shindex;
2280 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2281 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2282 abfd->flags |= HAS_SYMS;
2284 /* Besides being a symbol table, we also treat this as a regular
2285 section, so that objcopy can handle it. */
2286 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2289 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2291 elf_section_list * entry;
2293 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2294 if (entry->ndx == shindex)
2297 entry = bfd_alloc (abfd, sizeof (*entry));
2300 entry->ndx = shindex;
2302 entry->next = elf_symtab_shndx_list (abfd);
2303 elf_symtab_shndx_list (abfd) = entry;
2304 elf_elfsections (abfd)[shindex] = & entry->hdr;
2308 case SHT_STRTAB: /* A string table. */
2309 if (hdr->bfd_section != NULL)
2312 if (ehdr->e_shstrndx == shindex)
2314 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2315 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2319 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2322 elf_tdata (abfd)->strtab_hdr = *hdr;
2323 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2327 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2330 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2331 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2332 elf_elfsections (abfd)[shindex] = hdr;
2333 /* We also treat this as a regular section, so that objcopy
2335 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2340 /* If the string table isn't one of the above, then treat it as a
2341 regular section. We need to scan all the headers to be sure,
2342 just in case this strtab section appeared before the above. */
2343 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2345 unsigned int i, num_sec;
2347 num_sec = elf_numsections (abfd);
2348 for (i = 1; i < num_sec; i++)
2350 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2351 if (hdr2->sh_link == shindex)
2353 /* Prevent endless recursion on broken objects. */
2356 if (! bfd_section_from_shdr (abfd, i))
2358 if (elf_onesymtab (abfd) == i)
2360 if (elf_dynsymtab (abfd) == i)
2361 goto dynsymtab_strtab;
2365 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2370 /* *These* do a lot of work -- but build no sections! */
2372 asection *target_sect;
2373 Elf_Internal_Shdr *hdr2, **p_hdr;
2374 unsigned int num_sec = elf_numsections (abfd);
2375 struct bfd_elf_section_data *esdt;
2378 != (bfd_size_type) (hdr->sh_type == SHT_REL
2379 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2382 /* Check for a bogus link to avoid crashing. */
2383 if (hdr->sh_link >= num_sec)
2386 /* xgettext:c-format */
2387 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2388 abfd, hdr->sh_link, name, shindex);
2389 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2394 /* For some incomprehensible reason Oracle distributes
2395 libraries for Solaris in which some of the objects have
2396 bogus sh_link fields. It would be nice if we could just
2397 reject them, but, unfortunately, some people need to use
2398 them. We scan through the section headers; if we find only
2399 one suitable symbol table, we clobber the sh_link to point
2400 to it. I hope this doesn't break anything.
2402 Don't do it on executable nor shared library. */
2403 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2404 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2405 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2411 for (scan = 1; scan < num_sec; scan++)
2413 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2414 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2425 hdr->sh_link = found;
2428 /* Get the symbol table. */
2429 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2430 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2431 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2434 /* If this is an alloc section in an executable or shared
2435 library, or the reloc section does not use the main symbol
2436 table we don't treat it as a reloc section. BFD can't
2437 adequately represent such a section, so at least for now,
2438 we don't try. We just present it as a normal section. We
2439 also can't use it as a reloc section if it points to the
2440 null section, an invalid section, another reloc section, or
2441 its sh_link points to the null section. */
2442 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2443 && (hdr->sh_flags & SHF_ALLOC) != 0)
2444 || hdr->sh_link == SHN_UNDEF
2445 || hdr->sh_link != elf_onesymtab (abfd)
2446 || hdr->sh_info == SHN_UNDEF
2447 || hdr->sh_info >= num_sec
2448 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2449 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2451 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2456 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2459 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2460 if (target_sect == NULL)
2463 esdt = elf_section_data (target_sect);
2464 if (hdr->sh_type == SHT_RELA)
2465 p_hdr = &esdt->rela.hdr;
2467 p_hdr = &esdt->rel.hdr;
2469 /* PR 17512: file: 0b4f81b7.
2470 Also see PR 24456, for a file which deliberately has two reloc
2475 /* xgettext:c-format */
2476 (_("%pB: warning: multiple relocation sections for section %pA \
2477 found - ignoring all but the first"),
2481 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2486 elf_elfsections (abfd)[shindex] = hdr2;
2487 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2488 * bed->s->int_rels_per_ext_rel);
2489 target_sect->flags |= SEC_RELOC;
2490 target_sect->relocation = NULL;
2491 target_sect->rel_filepos = hdr->sh_offset;
2492 /* In the section to which the relocations apply, mark whether
2493 its relocations are of the REL or RELA variety. */
2494 if (hdr->sh_size != 0)
2496 if (hdr->sh_type == SHT_RELA)
2497 target_sect->use_rela_p = 1;
2499 abfd->flags |= HAS_RELOC;
2503 case SHT_GNU_verdef:
2504 elf_dynverdef (abfd) = shindex;
2505 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2506 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2509 case SHT_GNU_versym:
2510 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2513 elf_dynversym (abfd) = shindex;
2514 elf_tdata (abfd)->dynversym_hdr = *hdr;
2515 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2518 case SHT_GNU_verneed:
2519 elf_dynverref (abfd) = shindex;
2520 elf_tdata (abfd)->dynverref_hdr = *hdr;
2521 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2528 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2531 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2537 /* Possibly an attributes section. */
2538 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2539 || hdr->sh_type == bed->obj_attrs_section_type)
2541 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2543 _bfd_elf_parse_attributes (abfd, hdr);
2547 /* Check for any processor-specific section types. */
2548 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2551 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2553 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2554 /* FIXME: How to properly handle allocated section reserved
2555 for applications? */
2557 /* xgettext:c-format */
2558 (_("%pB: unknown type [%#x] section `%s'"),
2559 abfd, hdr->sh_type, name);
2562 /* Allow sections reserved for applications. */
2563 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2568 else if (hdr->sh_type >= SHT_LOPROC
2569 && hdr->sh_type <= SHT_HIPROC)
2570 /* FIXME: We should handle this section. */
2572 /* xgettext:c-format */
2573 (_("%pB: unknown type [%#x] section `%s'"),
2574 abfd, hdr->sh_type, name);
2575 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2577 /* Unrecognised OS-specific sections. */
2578 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2579 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2580 required to correctly process the section and the file should
2581 be rejected with an error message. */
2583 /* xgettext:c-format */
2584 (_("%pB: unknown type [%#x] section `%s'"),
2585 abfd, hdr->sh_type, name);
2588 /* Otherwise it should be processed. */
2589 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2594 /* FIXME: We should handle this section. */
2596 /* xgettext:c-format */
2597 (_("%pB: unknown type [%#x] section `%s'"),
2598 abfd, hdr->sh_type, name);
2606 if (sections_being_created && sections_being_created_abfd == abfd)
2607 sections_being_created [shindex] = FALSE;
2608 if (-- nesting == 0)
2610 sections_being_created = NULL;
2611 sections_being_created_abfd = abfd;
2616 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2619 bfd_sym_from_r_symndx (struct sym_cache *cache,
2621 unsigned long r_symndx)
2623 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2625 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2627 Elf_Internal_Shdr *symtab_hdr;
2628 unsigned char esym[sizeof (Elf64_External_Sym)];
2629 Elf_External_Sym_Shndx eshndx;
2631 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2632 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2633 &cache->sym[ent], esym, &eshndx) == NULL)
2636 if (cache->abfd != abfd)
2638 memset (cache->indx, -1, sizeof (cache->indx));
2641 cache->indx[ent] = r_symndx;
2644 return &cache->sym[ent];
2647 /* Given an ELF section number, retrieve the corresponding BFD
2651 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2653 if (sec_index >= elf_numsections (abfd))
2655 return elf_elfsections (abfd)[sec_index]->bfd_section;
2658 static const struct bfd_elf_special_section special_sections_b[] =
2660 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2661 { NULL, 0, 0, 0, 0 }
2664 static const struct bfd_elf_special_section special_sections_c[] =
2666 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2667 { NULL, 0, 0, 0, 0 }
2670 static const struct bfd_elf_special_section special_sections_d[] =
2672 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2673 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2674 /* There are more DWARF sections than these, but they needn't be added here
2675 unless you have to cope with broken compilers that don't emit section
2676 attributes or you want to help the user writing assembler. */
2677 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2678 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2679 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2680 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2681 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2682 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2683 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2684 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2685 { NULL, 0, 0, 0, 0 }
2688 static const struct bfd_elf_special_section special_sections_f[] =
2690 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2691 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2692 { NULL, 0 , 0, 0, 0 }
2695 static const struct bfd_elf_special_section special_sections_g[] =
2697 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2698 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2699 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2700 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2701 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2702 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2703 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2704 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2705 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2706 { NULL, 0, 0, 0, 0 }
2709 static const struct bfd_elf_special_section special_sections_h[] =
2711 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2712 { NULL, 0, 0, 0, 0 }
2715 static const struct bfd_elf_special_section special_sections_i[] =
2717 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2718 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2719 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2720 { NULL, 0, 0, 0, 0 }
2723 static const struct bfd_elf_special_section special_sections_l[] =
2725 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2726 { NULL, 0, 0, 0, 0 }
2729 static const struct bfd_elf_special_section special_sections_n[] =
2731 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2732 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2733 { NULL, 0, 0, 0, 0 }
2736 static const struct bfd_elf_special_section special_sections_p[] =
2738 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2739 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2740 { NULL, 0, 0, 0, 0 }
2743 static const struct bfd_elf_special_section special_sections_r[] =
2745 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2746 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2747 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2748 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2749 { NULL, 0, 0, 0, 0 }
2752 static const struct bfd_elf_special_section special_sections_s[] =
2754 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2755 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2756 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2757 /* See struct bfd_elf_special_section declaration for the semantics of
2758 this special case where .prefix_length != strlen (.prefix). */
2759 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2760 { NULL, 0, 0, 0, 0 }
2763 static const struct bfd_elf_special_section special_sections_t[] =
2765 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2766 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2767 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2768 { NULL, 0, 0, 0, 0 }
2771 static const struct bfd_elf_special_section special_sections_z[] =
2773 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2774 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2775 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2776 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2777 { NULL, 0, 0, 0, 0 }
2780 static const struct bfd_elf_special_section * const special_sections[] =
2782 special_sections_b, /* 'b' */
2783 special_sections_c, /* 'c' */
2784 special_sections_d, /* 'd' */
2786 special_sections_f, /* 'f' */
2787 special_sections_g, /* 'g' */
2788 special_sections_h, /* 'h' */
2789 special_sections_i, /* 'i' */
2792 special_sections_l, /* 'l' */
2794 special_sections_n, /* 'n' */
2796 special_sections_p, /* 'p' */
2798 special_sections_r, /* 'r' */
2799 special_sections_s, /* 's' */
2800 special_sections_t, /* 't' */
2806 special_sections_z /* 'z' */
2809 const struct bfd_elf_special_section *
2810 _bfd_elf_get_special_section (const char *name,
2811 const struct bfd_elf_special_section *spec,
2817 len = strlen (name);
2819 for (i = 0; spec[i].prefix != NULL; i++)
2822 int prefix_len = spec[i].prefix_length;
2824 if (len < prefix_len)
2826 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2829 suffix_len = spec[i].suffix_length;
2830 if (suffix_len <= 0)
2832 if (name[prefix_len] != 0)
2834 if (suffix_len == 0)
2836 if (name[prefix_len] != '.'
2837 && (suffix_len == -2
2838 || (rela && spec[i].type == SHT_REL)))
2844 if (len < prefix_len + suffix_len)
2846 if (memcmp (name + len - suffix_len,
2847 spec[i].prefix + prefix_len,
2857 const struct bfd_elf_special_section *
2858 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2861 const struct bfd_elf_special_section *spec;
2862 const struct elf_backend_data *bed;
2864 /* See if this is one of the special sections. */
2865 if (sec->name == NULL)
2868 bed = get_elf_backend_data (abfd);
2869 spec = bed->special_sections;
2872 spec = _bfd_elf_get_special_section (sec->name,
2873 bed->special_sections,
2879 if (sec->name[0] != '.')
2882 i = sec->name[1] - 'b';
2883 if (i < 0 || i > 'z' - 'b')
2886 spec = special_sections[i];
2891 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2895 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2897 struct bfd_elf_section_data *sdata;
2898 const struct elf_backend_data *bed;
2899 const struct bfd_elf_special_section *ssect;
2901 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2904 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2908 sec->used_by_bfd = sdata;
2911 /* Indicate whether or not this section should use RELA relocations. */
2912 bed = get_elf_backend_data (abfd);
2913 sec->use_rela_p = bed->default_use_rela_p;
2915 /* When we read a file, we don't need to set ELF section type and
2916 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2917 anyway. We will set ELF section type and flags for all linker
2918 created sections. If user specifies BFD section flags, we will
2919 set ELF section type and flags based on BFD section flags in
2920 elf_fake_sections. Special handling for .init_array/.fini_array
2921 output sections since they may contain .ctors/.dtors input
2922 sections. We don't want _bfd_elf_init_private_section_data to
2923 copy ELF section type from .ctors/.dtors input sections. */
2924 if (abfd->direction != read_direction
2925 || (sec->flags & SEC_LINKER_CREATED) != 0)
2927 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2930 || (sec->flags & SEC_LINKER_CREATED) != 0
2931 || ssect->type == SHT_INIT_ARRAY
2932 || ssect->type == SHT_FINI_ARRAY))
2934 elf_section_type (sec) = ssect->type;
2935 elf_section_flags (sec) = ssect->attr;
2939 return _bfd_generic_new_section_hook (abfd, sec);
2942 /* Create a new bfd section from an ELF program header.
2944 Since program segments have no names, we generate a synthetic name
2945 of the form segment<NUM>, where NUM is generally the index in the
2946 program header table. For segments that are split (see below) we
2947 generate the names segment<NUM>a and segment<NUM>b.
2949 Note that some program segments may have a file size that is different than
2950 (less than) the memory size. All this means is that at execution the
2951 system must allocate the amount of memory specified by the memory size,
2952 but only initialize it with the first "file size" bytes read from the
2953 file. This would occur for example, with program segments consisting
2954 of combined data+bss.
2956 To handle the above situation, this routine generates TWO bfd sections
2957 for the single program segment. The first has the length specified by
2958 the file size of the segment, and the second has the length specified
2959 by the difference between the two sizes. In effect, the segment is split
2960 into its initialized and uninitialized parts.
2965 _bfd_elf_make_section_from_phdr (bfd *abfd,
2966 Elf_Internal_Phdr *hdr,
2968 const char *type_name)
2976 split = ((hdr->p_memsz > 0)
2977 && (hdr->p_filesz > 0)
2978 && (hdr->p_memsz > hdr->p_filesz));
2980 if (hdr->p_filesz > 0)
2982 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2983 len = strlen (namebuf) + 1;
2984 name = (char *) bfd_alloc (abfd, len);
2987 memcpy (name, namebuf, len);
2988 newsect = bfd_make_section (abfd, name);
2989 if (newsect == NULL)
2991 newsect->vma = hdr->p_vaddr;
2992 newsect->lma = hdr->p_paddr;
2993 newsect->size = hdr->p_filesz;
2994 newsect->filepos = hdr->p_offset;
2995 newsect->flags |= SEC_HAS_CONTENTS;
2996 newsect->alignment_power = bfd_log2 (hdr->p_align);
2997 if (hdr->p_type == PT_LOAD)
2999 newsect->flags |= SEC_ALLOC;
3000 newsect->flags |= SEC_LOAD;
3001 if (hdr->p_flags & PF_X)
3003 /* FIXME: all we known is that it has execute PERMISSION,
3005 newsect->flags |= SEC_CODE;
3008 if (!(hdr->p_flags & PF_W))
3010 newsect->flags |= SEC_READONLY;
3014 if (hdr->p_memsz > hdr->p_filesz)
3018 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3019 len = strlen (namebuf) + 1;
3020 name = (char *) bfd_alloc (abfd, len);
3023 memcpy (name, namebuf, len);
3024 newsect = bfd_make_section (abfd, name);
3025 if (newsect == NULL)
3027 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
3028 newsect->lma = hdr->p_paddr + hdr->p_filesz;
3029 newsect->size = hdr->p_memsz - hdr->p_filesz;
3030 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3031 align = newsect->vma & -newsect->vma;
3032 if (align == 0 || align > hdr->p_align)
3033 align = hdr->p_align;
3034 newsect->alignment_power = bfd_log2 (align);
3035 if (hdr->p_type == PT_LOAD)
3037 /* Hack for gdb. Segments that have not been modified do
3038 not have their contents written to a core file, on the
3039 assumption that a debugger can find the contents in the
3040 executable. We flag this case by setting the fake
3041 section size to zero. Note that "real" bss sections will
3042 always have their contents dumped to the core file. */
3043 if (bfd_get_format (abfd) == bfd_core)
3045 newsect->flags |= SEC_ALLOC;
3046 if (hdr->p_flags & PF_X)
3047 newsect->flags |= SEC_CODE;
3049 if (!(hdr->p_flags & PF_W))
3050 newsect->flags |= SEC_READONLY;
3057 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3059 const struct elf_backend_data *bed;
3061 switch (hdr->p_type)
3064 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3067 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3076 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3078 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3084 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3087 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3089 case PT_GNU_EH_FRAME:
3090 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3094 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3097 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3100 /* Check for any processor-specific program segment types. */
3101 bed = get_elf_backend_data (abfd);
3102 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3106 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3110 _bfd_elf_single_rel_hdr (asection *sec)
3112 if (elf_section_data (sec)->rel.hdr)
3114 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3115 return elf_section_data (sec)->rel.hdr;
3118 return elf_section_data (sec)->rela.hdr;
3122 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3123 Elf_Internal_Shdr *rel_hdr,
3124 const char *sec_name,
3125 bfd_boolean use_rela_p)
3127 char *name = (char *) bfd_alloc (abfd,
3128 sizeof ".rela" + strlen (sec_name));
3132 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3134 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3136 if (rel_hdr->sh_name == (unsigned int) -1)
3142 /* Allocate and initialize a section-header for a new reloc section,
3143 containing relocations against ASECT. It is stored in RELDATA. If
3144 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3148 _bfd_elf_init_reloc_shdr (bfd *abfd,
3149 struct bfd_elf_section_reloc_data *reldata,
3150 const char *sec_name,
3151 bfd_boolean use_rela_p,
3152 bfd_boolean delay_st_name_p)
3154 Elf_Internal_Shdr *rel_hdr;
3155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3157 BFD_ASSERT (reldata->hdr == NULL);
3158 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3159 reldata->hdr = rel_hdr;
3161 if (delay_st_name_p)
3162 rel_hdr->sh_name = (unsigned int) -1;
3163 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3166 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3167 rel_hdr->sh_entsize = (use_rela_p
3168 ? bed->s->sizeof_rela
3169 : bed->s->sizeof_rel);
3170 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3171 rel_hdr->sh_flags = 0;
3172 rel_hdr->sh_addr = 0;
3173 rel_hdr->sh_size = 0;
3174 rel_hdr->sh_offset = 0;
3179 /* Return the default section type based on the passed in section flags. */
3182 bfd_elf_get_default_section_type (flagword flags)
3184 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3185 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3187 return SHT_PROGBITS;
3190 struct fake_section_arg
3192 struct bfd_link_info *link_info;
3196 /* Set up an ELF internal section header for a section. */
3199 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3201 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3203 struct bfd_elf_section_data *esd = elf_section_data (asect);
3204 Elf_Internal_Shdr *this_hdr;
3205 unsigned int sh_type;
3206 const char *name = asect->name;
3207 bfd_boolean delay_st_name_p = FALSE;
3211 /* We already failed; just get out of the bfd_map_over_sections
3216 this_hdr = &esd->this_hdr;
3220 /* ld: compress DWARF debug sections with names: .debug_*. */
3221 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3222 && (asect->flags & SEC_DEBUGGING)
3226 /* Set SEC_ELF_COMPRESS to indicate this section should be
3228 asect->flags |= SEC_ELF_COMPRESS;
3230 /* If this section will be compressed, delay adding section
3231 name to section name section after it is compressed in
3232 _bfd_elf_assign_file_positions_for_non_load. */
3233 delay_st_name_p = TRUE;
3236 else if ((asect->flags & SEC_ELF_RENAME))
3238 /* objcopy: rename output DWARF debug section. */
3239 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3241 /* When we decompress or compress with SHF_COMPRESSED,
3242 convert section name from .zdebug_* to .debug_* if
3246 char *new_name = convert_zdebug_to_debug (abfd, name);
3247 if (new_name == NULL)
3255 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3257 /* PR binutils/18087: Compression does not always make a
3258 section smaller. So only rename the section when
3259 compression has actually taken place. If input section
3260 name is .zdebug_*, we should never compress it again. */
3261 char *new_name = convert_debug_to_zdebug (abfd, name);
3262 if (new_name == NULL)
3267 BFD_ASSERT (name[1] != 'z');
3272 if (delay_st_name_p)
3273 this_hdr->sh_name = (unsigned int) -1;
3277 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3279 if (this_hdr->sh_name == (unsigned int) -1)
3286 /* Don't clear sh_flags. Assembler may set additional bits. */
3288 if ((asect->flags & SEC_ALLOC) != 0
3289 || asect->user_set_vma)
3290 this_hdr->sh_addr = asect->vma;
3292 this_hdr->sh_addr = 0;
3294 this_hdr->sh_offset = 0;
3295 this_hdr->sh_size = asect->size;
3296 this_hdr->sh_link = 0;
3297 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3298 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3301 /* xgettext:c-format */
3302 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3303 abfd, asect->alignment_power, asect);
3307 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
3308 /* The sh_entsize and sh_info fields may have been set already by
3309 copy_private_section_data. */
3311 this_hdr->bfd_section = asect;
3312 this_hdr->contents = NULL;
3314 /* If the section type is unspecified, we set it based on
3316 if ((asect->flags & SEC_GROUP) != 0)
3317 sh_type = SHT_GROUP;
3319 sh_type = bfd_elf_get_default_section_type (asect->flags);
3321 if (this_hdr->sh_type == SHT_NULL)
3322 this_hdr->sh_type = sh_type;
3323 else if (this_hdr->sh_type == SHT_NOBITS
3324 && sh_type == SHT_PROGBITS
3325 && (asect->flags & SEC_ALLOC) != 0)
3327 /* Warn if we are changing a NOBITS section to PROGBITS, but
3328 allow the link to proceed. This can happen when users link
3329 non-bss input sections to bss output sections, or emit data
3330 to a bss output section via a linker script. */
3332 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3333 this_hdr->sh_type = sh_type;
3336 switch (this_hdr->sh_type)
3347 case SHT_INIT_ARRAY:
3348 case SHT_FINI_ARRAY:
3349 case SHT_PREINIT_ARRAY:
3350 this_hdr->sh_entsize = bed->s->arch_size / 8;
3354 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3358 this_hdr->sh_entsize = bed->s->sizeof_sym;
3362 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3366 if (get_elf_backend_data (abfd)->may_use_rela_p)
3367 this_hdr->sh_entsize = bed->s->sizeof_rela;
3371 if (get_elf_backend_data (abfd)->may_use_rel_p)
3372 this_hdr->sh_entsize = bed->s->sizeof_rel;
3375 case SHT_GNU_versym:
3376 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3379 case SHT_GNU_verdef:
3380 this_hdr->sh_entsize = 0;
3381 /* objcopy or strip will copy over sh_info, but may not set
3382 cverdefs. The linker will set cverdefs, but sh_info will be
3384 if (this_hdr->sh_info == 0)
3385 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3387 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3388 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3391 case SHT_GNU_verneed:
3392 this_hdr->sh_entsize = 0;
3393 /* objcopy or strip will copy over sh_info, but may not set
3394 cverrefs. The linker will set cverrefs, but sh_info will be
3396 if (this_hdr->sh_info == 0)
3397 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3399 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3400 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3404 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3408 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3412 if ((asect->flags & SEC_ALLOC) != 0)
3413 this_hdr->sh_flags |= SHF_ALLOC;
3414 if ((asect->flags & SEC_READONLY) == 0)
3415 this_hdr->sh_flags |= SHF_WRITE;
3416 if ((asect->flags & SEC_CODE) != 0)
3417 this_hdr->sh_flags |= SHF_EXECINSTR;
3418 if ((asect->flags & SEC_MERGE) != 0)
3420 this_hdr->sh_flags |= SHF_MERGE;
3421 this_hdr->sh_entsize = asect->entsize;
3423 if ((asect->flags & SEC_STRINGS) != 0)
3424 this_hdr->sh_flags |= SHF_STRINGS;
3425 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3426 this_hdr->sh_flags |= SHF_GROUP;
3427 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3429 this_hdr->sh_flags |= SHF_TLS;
3430 if (asect->size == 0
3431 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3433 struct bfd_link_order *o = asect->map_tail.link_order;
3435 this_hdr->sh_size = 0;
3438 this_hdr->sh_size = o->offset + o->size;
3439 if (this_hdr->sh_size != 0)
3440 this_hdr->sh_type = SHT_NOBITS;
3444 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3445 this_hdr->sh_flags |= SHF_EXCLUDE;
3447 /* If the section has relocs, set up a section header for the
3448 SHT_REL[A] section. If two relocation sections are required for
3449 this section, it is up to the processor-specific back-end to
3450 create the other. */
3451 if ((asect->flags & SEC_RELOC) != 0)
3453 /* When doing a relocatable link, create both REL and RELA sections if
3456 /* Do the normal setup if we wouldn't create any sections here. */
3457 && esd->rel.count + esd->rela.count > 0
3458 && (bfd_link_relocatable (arg->link_info)
3459 || arg->link_info->emitrelocations))
3461 if (esd->rel.count && esd->rel.hdr == NULL
3462 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3463 FALSE, delay_st_name_p))
3468 if (esd->rela.count && esd->rela.hdr == NULL
3469 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3470 TRUE, delay_st_name_p))
3476 else if (!_bfd_elf_init_reloc_shdr (abfd,
3478 ? &esd->rela : &esd->rel),
3488 /* Check for processor-specific section types. */
3489 sh_type = this_hdr->sh_type;
3490 if (bed->elf_backend_fake_sections
3491 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3497 if (sh_type == SHT_NOBITS && asect->size != 0)
3499 /* Don't change the header type from NOBITS if we are being
3500 called for objcopy --only-keep-debug. */
3501 this_hdr->sh_type = sh_type;
3505 /* Fill in the contents of a SHT_GROUP section. Called from
3506 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3507 when ELF targets use the generic linker, ld. Called for ld -r
3508 from bfd_elf_final_link. */
3511 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3513 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3514 asection *elt, *first;
3518 /* Ignore linker created group section. See elfNN_ia64_object_p in
3520 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3525 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3527 unsigned long symindx = 0;
3529 /* elf_group_id will have been set up by objcopy and the
3531 if (elf_group_id (sec) != NULL)
3532 symindx = elf_group_id (sec)->udata.i;
3536 /* If called from the assembler, swap_out_syms will have set up
3537 elf_section_syms. */
3538 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3539 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3541 elf_section_data (sec)->this_hdr.sh_info = symindx;
3543 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3545 /* The ELF backend linker sets sh_info to -2 when the group
3546 signature symbol is global, and thus the index can't be
3547 set until all local symbols are output. */
3549 struct bfd_elf_section_data *sec_data;
3550 unsigned long symndx;
3551 unsigned long extsymoff;
3552 struct elf_link_hash_entry *h;
3554 /* The point of this little dance to the first SHF_GROUP section
3555 then back to the SHT_GROUP section is that this gets us to
3556 the SHT_GROUP in the input object. */
3557 igroup = elf_sec_group (elf_next_in_group (sec));
3558 sec_data = elf_section_data (igroup);
3559 symndx = sec_data->this_hdr.sh_info;
3561 if (!elf_bad_symtab (igroup->owner))
3563 Elf_Internal_Shdr *symtab_hdr;
3565 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3566 extsymoff = symtab_hdr->sh_info;
3568 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3569 while (h->root.type == bfd_link_hash_indirect
3570 || h->root.type == bfd_link_hash_warning)
3571 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3573 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3576 /* The contents won't be allocated for "ld -r" or objcopy. */
3578 if (sec->contents == NULL)
3581 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3583 /* Arrange for the section to be written out. */
3584 elf_section_data (sec)->this_hdr.contents = sec->contents;
3585 if (sec->contents == NULL)
3592 loc = sec->contents + sec->size;
3594 /* Get the pointer to the first section in the group that gas
3595 squirreled away here. objcopy arranges for this to be set to the
3596 start of the input section group. */
3597 first = elt = elf_next_in_group (sec);
3599 /* First element is a flag word. Rest of section is elf section
3600 indices for all the sections of the group. Write them backwards
3601 just to keep the group in the same order as given in .section
3602 directives, not that it matters. */
3609 s = s->output_section;
3611 && !bfd_is_abs_section (s))
3613 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3614 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3616 if (elf_sec->rel.hdr != NULL
3618 || (input_elf_sec->rel.hdr != NULL
3619 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3621 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3623 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3625 if (elf_sec->rela.hdr != NULL
3627 || (input_elf_sec->rela.hdr != NULL
3628 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3630 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3632 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3635 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3637 elt = elf_next_in_group (elt);
3643 BFD_ASSERT (loc == sec->contents);
3645 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3648 /* Given NAME, the name of a relocation section stripped of its
3649 .rel/.rela prefix, return the section in ABFD to which the
3650 relocations apply. */
3653 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3655 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3656 section likely apply to .got.plt or .got section. */
3657 if (get_elf_backend_data (abfd)->want_got_plt
3658 && strcmp (name, ".plt") == 0)
3663 sec = bfd_get_section_by_name (abfd, name);
3669 return bfd_get_section_by_name (abfd, name);
3672 /* Return the section to which RELOC_SEC applies. */
3675 elf_get_reloc_section (asection *reloc_sec)
3680 const struct elf_backend_data *bed;
3682 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3683 if (type != SHT_REL && type != SHT_RELA)
3686 /* We look up the section the relocs apply to by name. */
3687 name = reloc_sec->name;
3688 if (strncmp (name, ".rel", 4) != 0)
3691 if (type == SHT_RELA && *name++ != 'a')
3694 abfd = reloc_sec->owner;
3695 bed = get_elf_backend_data (abfd);
3696 return bed->get_reloc_section (abfd, name);
3699 /* Assign all ELF section numbers. The dummy first section is handled here
3700 too. The link/info pointers for the standard section types are filled
3701 in here too, while we're at it. */
3704 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3706 struct elf_obj_tdata *t = elf_tdata (abfd);
3708 unsigned int section_number;
3709 Elf_Internal_Shdr **i_shdrp;
3710 struct bfd_elf_section_data *d;
3711 bfd_boolean need_symtab;
3715 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3717 /* SHT_GROUP sections are in relocatable files only. */
3718 if (link_info == NULL || !link_info->resolve_section_groups)
3720 size_t reloc_count = 0;
3722 /* Put SHT_GROUP sections first. */
3723 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3725 d = elf_section_data (sec);
3727 if (d->this_hdr.sh_type == SHT_GROUP)
3729 if (sec->flags & SEC_LINKER_CREATED)
3731 /* Remove the linker created SHT_GROUP sections. */
3732 bfd_section_list_remove (abfd, sec);
3733 abfd->section_count--;
3736 d->this_idx = section_number++;
3739 /* Count relocations. */
3740 reloc_count += sec->reloc_count;
3743 /* Clear HAS_RELOC if there are no relocations. */
3744 if (reloc_count == 0)
3745 abfd->flags &= ~HAS_RELOC;
3748 for (sec = abfd->sections; sec; sec = sec->next)
3750 d = elf_section_data (sec);
3752 if (d->this_hdr.sh_type != SHT_GROUP)
3753 d->this_idx = section_number++;
3754 if (d->this_hdr.sh_name != (unsigned int) -1)
3755 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3758 d->rel.idx = section_number++;
3759 if (d->rel.hdr->sh_name != (unsigned int) -1)
3760 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3767 d->rela.idx = section_number++;
3768 if (d->rela.hdr->sh_name != (unsigned int) -1)
3769 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3775 need_symtab = (bfd_get_symcount (abfd) > 0
3776 || (link_info == NULL
3777 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3781 elf_onesymtab (abfd) = section_number++;
3782 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3783 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3785 elf_section_list *entry;
3787 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3789 entry = bfd_zalloc (abfd, sizeof (*entry));
3790 entry->ndx = section_number++;
3791 elf_symtab_shndx_list (abfd) = entry;
3793 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3794 ".symtab_shndx", FALSE);
3795 if (entry->hdr.sh_name == (unsigned int) -1)
3798 elf_strtab_sec (abfd) = section_number++;
3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3802 elf_shstrtab_sec (abfd) = section_number++;
3803 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3804 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3806 if (section_number >= SHN_LORESERVE)
3808 /* xgettext:c-format */
3809 _bfd_error_handler (_("%pB: too many sections: %u"),
3810 abfd, section_number);
3814 elf_numsections (abfd) = section_number;
3815 elf_elfheader (abfd)->e_shnum = section_number;
3817 /* Set up the list of section header pointers, in agreement with the
3819 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3820 sizeof (Elf_Internal_Shdr *));
3821 if (i_shdrp == NULL)
3824 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3825 sizeof (Elf_Internal_Shdr));
3826 if (i_shdrp[0] == NULL)
3828 bfd_release (abfd, i_shdrp);
3832 elf_elfsections (abfd) = i_shdrp;
3834 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3837 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3838 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3840 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3841 BFD_ASSERT (entry != NULL);
3842 i_shdrp[entry->ndx] = & entry->hdr;
3843 entry->hdr.sh_link = elf_onesymtab (abfd);
3845 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3846 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3849 for (sec = abfd->sections; sec; sec = sec->next)
3853 d = elf_section_data (sec);
3855 i_shdrp[d->this_idx] = &d->this_hdr;
3856 if (d->rel.idx != 0)
3857 i_shdrp[d->rel.idx] = d->rel.hdr;
3858 if (d->rela.idx != 0)
3859 i_shdrp[d->rela.idx] = d->rela.hdr;
3861 /* Fill in the sh_link and sh_info fields while we're at it. */
3863 /* sh_link of a reloc section is the section index of the symbol
3864 table. sh_info is the section index of the section to which
3865 the relocation entries apply. */
3866 if (d->rel.idx != 0)
3868 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3869 d->rel.hdr->sh_info = d->this_idx;
3870 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3872 if (d->rela.idx != 0)
3874 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3875 d->rela.hdr->sh_info = d->this_idx;
3876 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3879 /* We need to set up sh_link for SHF_LINK_ORDER. */
3880 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3882 s = elf_linked_to_section (sec);
3885 /* elf_linked_to_section points to the input section. */
3886 if (link_info != NULL)
3888 /* Check discarded linkonce section. */
3889 if (discarded_section (s))
3893 /* xgettext:c-format */
3894 (_("%pB: sh_link of section `%pA' points to"
3895 " discarded section `%pA' of `%pB'"),
3896 abfd, d->this_hdr.bfd_section,
3898 /* Point to the kept section if it has the same
3899 size as the discarded one. */
3900 kept = _bfd_elf_check_kept_section (s, link_info);
3903 bfd_set_error (bfd_error_bad_value);
3909 s = s->output_section;
3910 BFD_ASSERT (s != NULL);
3914 /* Handle objcopy. */
3915 if (s->output_section == NULL)
3918 /* xgettext:c-format */
3919 (_("%pB: sh_link of section `%pA' points to"
3920 " removed section `%pA' of `%pB'"),
3921 abfd, d->this_hdr.bfd_section, s, s->owner);
3922 bfd_set_error (bfd_error_bad_value);
3925 s = s->output_section;
3927 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3932 The Intel C compiler generates SHT_IA_64_UNWIND with
3933 SHF_LINK_ORDER. But it doesn't set the sh_link or
3934 sh_info fields. Hence we could get the situation
3936 const struct elf_backend_data *bed
3937 = get_elf_backend_data (abfd);
3938 if (bed->link_order_error_handler)
3939 bed->link_order_error_handler
3940 /* xgettext:c-format */
3941 (_("%pB: warning: sh_link not set for section `%pA'"),
3946 switch (d->this_hdr.sh_type)
3950 /* A reloc section which we are treating as a normal BFD
3951 section. sh_link is the section index of the symbol
3952 table. sh_info is the section index of the section to
3953 which the relocation entries apply. We assume that an
3954 allocated reloc section uses the dynamic symbol table.
3955 FIXME: How can we be sure? */
3956 s = bfd_get_section_by_name (abfd, ".dynsym");
3958 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3960 s = elf_get_reloc_section (sec);
3963 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3964 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3969 /* We assume that a section named .stab*str is a stabs
3970 string section. We look for a section with the same name
3971 but without the trailing ``str'', and set its sh_link
3972 field to point to this section. */
3973 if (CONST_STRNEQ (sec->name, ".stab")
3974 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3979 len = strlen (sec->name);
3980 alc = (char *) bfd_malloc (len - 2);
3983 memcpy (alc, sec->name, len - 3);
3984 alc[len - 3] = '\0';
3985 s = bfd_get_section_by_name (abfd, alc);
3989 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3991 /* This is a .stab section. */
3992 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3993 elf_section_data (s)->this_hdr.sh_entsize
3994 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
4001 case SHT_GNU_verneed:
4002 case SHT_GNU_verdef:
4003 /* sh_link is the section header index of the string table
4004 used for the dynamic entries, or the symbol table, or the
4006 s = bfd_get_section_by_name (abfd, ".dynstr");
4008 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4011 case SHT_GNU_LIBLIST:
4012 /* sh_link is the section header index of the prelink library
4013 list used for the dynamic entries, or the symbol table, or
4014 the version strings. */
4015 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4016 ? ".dynstr" : ".gnu.libstr");
4018 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4023 case SHT_GNU_versym:
4024 /* sh_link is the section header index of the symbol table
4025 this hash table or version table is for. */
4026 s = bfd_get_section_by_name (abfd, ".dynsym");
4028 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4032 d->this_hdr.sh_link = elf_onesymtab (abfd);
4036 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4037 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4038 debug section name from .debug_* to .zdebug_* if needed. */
4044 sym_is_global (bfd *abfd, asymbol *sym)
4046 /* If the backend has a special mapping, use it. */
4047 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4048 if (bed->elf_backend_sym_is_global)
4049 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4051 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4052 || bfd_is_und_section (bfd_get_section (sym))
4053 || bfd_is_com_section (bfd_get_section (sym)));
4056 /* Filter global symbols of ABFD to include in the import library. All
4057 SYMCOUNT symbols of ABFD can be examined from their pointers in
4058 SYMS. Pointers of symbols to keep should be stored contiguously at
4059 the beginning of that array.
4061 Returns the number of symbols to keep. */
4064 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4065 asymbol **syms, long symcount)
4067 long src_count, dst_count = 0;
4069 for (src_count = 0; src_count < symcount; src_count++)
4071 asymbol *sym = syms[src_count];
4072 char *name = (char *) bfd_asymbol_name (sym);
4073 struct bfd_link_hash_entry *h;
4075 if (!sym_is_global (abfd, sym))
4078 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
4081 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4083 if (h->linker_def || h->ldscript_def)
4086 syms[dst_count++] = sym;
4089 syms[dst_count] = NULL;
4094 /* Don't output section symbols for sections that are not going to be
4095 output, that are duplicates or there is no BFD section. */
4098 ignore_section_sym (bfd *abfd, asymbol *sym)
4100 elf_symbol_type *type_ptr;
4105 if ((sym->flags & BSF_SECTION_SYM) == 0)
4108 if (sym->section == NULL)
4111 type_ptr = elf_symbol_from (abfd, sym);
4112 return ((type_ptr != NULL
4113 && type_ptr->internal_elf_sym.st_shndx != 0
4114 && bfd_is_abs_section (sym->section))
4115 || !(sym->section->owner == abfd
4116 || (sym->section->output_section != NULL
4117 && sym->section->output_section->owner == abfd
4118 && sym->section->output_offset == 0)
4119 || bfd_is_abs_section (sym->section)));
4122 /* Map symbol from it's internal number to the external number, moving
4123 all local symbols to be at the head of the list. */
4126 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4128 unsigned int symcount = bfd_get_symcount (abfd);
4129 asymbol **syms = bfd_get_outsymbols (abfd);
4130 asymbol **sect_syms;
4131 unsigned int num_locals = 0;
4132 unsigned int num_globals = 0;
4133 unsigned int num_locals2 = 0;
4134 unsigned int num_globals2 = 0;
4135 unsigned int max_index = 0;
4141 fprintf (stderr, "elf_map_symbols\n");
4145 for (asect = abfd->sections; asect; asect = asect->next)
4147 if (max_index < asect->index)
4148 max_index = asect->index;
4152 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
4153 if (sect_syms == NULL)
4155 elf_section_syms (abfd) = sect_syms;
4156 elf_num_section_syms (abfd) = max_index;
4158 /* Init sect_syms entries for any section symbols we have already
4159 decided to output. */
4160 for (idx = 0; idx < symcount; idx++)
4162 asymbol *sym = syms[idx];
4164 if ((sym->flags & BSF_SECTION_SYM) != 0
4166 && !ignore_section_sym (abfd, sym)
4167 && !bfd_is_abs_section (sym->section))
4169 asection *sec = sym->section;
4171 if (sec->owner != abfd)
4172 sec = sec->output_section;
4174 sect_syms[sec->index] = syms[idx];
4178 /* Classify all of the symbols. */
4179 for (idx = 0; idx < symcount; idx++)
4181 if (sym_is_global (abfd, syms[idx]))
4183 else if (!ignore_section_sym (abfd, syms[idx]))
4187 /* We will be adding a section symbol for each normal BFD section. Most
4188 sections will already have a section symbol in outsymbols, but
4189 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4190 at least in that case. */
4191 for (asect = abfd->sections; asect; asect = asect->next)
4193 if (sect_syms[asect->index] == NULL)
4195 if (!sym_is_global (abfd, asect->symbol))
4202 /* Now sort the symbols so the local symbols are first. */
4203 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
4204 sizeof (asymbol *));
4206 if (new_syms == NULL)
4209 for (idx = 0; idx < symcount; idx++)
4211 asymbol *sym = syms[idx];
4214 if (sym_is_global (abfd, sym))
4215 i = num_locals + num_globals2++;
4216 else if (!ignore_section_sym (abfd, sym))
4221 sym->udata.i = i + 1;
4223 for (asect = abfd->sections; asect; asect = asect->next)
4225 if (sect_syms[asect->index] == NULL)
4227 asymbol *sym = asect->symbol;
4230 sect_syms[asect->index] = sym;
4231 if (!sym_is_global (abfd, sym))
4234 i = num_locals + num_globals2++;
4236 sym->udata.i = i + 1;
4240 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4242 *pnum_locals = num_locals;
4246 /* Align to the maximum file alignment that could be required for any
4247 ELF data structure. */
4249 static inline file_ptr
4250 align_file_position (file_ptr off, int align)
4252 return (off + align - 1) & ~(align - 1);
4255 /* Assign a file position to a section, optionally aligning to the
4256 required section alignment. */
4259 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4263 if (align && i_shdrp->sh_addralign > 1)
4264 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4265 i_shdrp->sh_offset = offset;
4266 if (i_shdrp->bfd_section != NULL)
4267 i_shdrp->bfd_section->filepos = offset;
4268 if (i_shdrp->sh_type != SHT_NOBITS)
4269 offset += i_shdrp->sh_size;
4273 /* Compute the file positions we are going to put the sections at, and
4274 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4275 is not NULL, this is being called by the ELF backend linker. */
4278 _bfd_elf_compute_section_file_positions (bfd *abfd,
4279 struct bfd_link_info *link_info)
4281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4282 struct fake_section_arg fsargs;
4284 struct elf_strtab_hash *strtab = NULL;
4285 Elf_Internal_Shdr *shstrtab_hdr;
4286 bfd_boolean need_symtab;
4288 if (abfd->output_has_begun)
4291 /* Do any elf backend specific processing first. */
4292 if (bed->elf_backend_begin_write_processing)
4293 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4295 if (! prep_headers (abfd))
4298 /* Post process the headers if necessary. */
4299 (*bed->elf_backend_post_process_headers) (abfd, link_info);
4301 fsargs.failed = FALSE;
4302 fsargs.link_info = link_info;
4303 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4307 if (!assign_section_numbers (abfd, link_info))
4310 /* The backend linker builds symbol table information itself. */
4311 need_symtab = (link_info == NULL
4312 && (bfd_get_symcount (abfd) > 0
4313 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4317 /* Non-zero if doing a relocatable link. */
4318 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4320 if (! swap_out_syms (abfd, &strtab, relocatable_p))
4325 if (link_info == NULL)
4327 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4332 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4333 /* sh_name was set in prep_headers. */
4334 shstrtab_hdr->sh_type = SHT_STRTAB;
4335 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4336 shstrtab_hdr->sh_addr = 0;
4337 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4338 shstrtab_hdr->sh_entsize = 0;
4339 shstrtab_hdr->sh_link = 0;
4340 shstrtab_hdr->sh_info = 0;
4341 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4342 shstrtab_hdr->sh_addralign = 1;
4344 if (!assign_file_positions_except_relocs (abfd, link_info))
4350 Elf_Internal_Shdr *hdr;
4352 off = elf_next_file_pos (abfd);
4354 hdr = & elf_symtab_hdr (abfd);
4355 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4357 if (elf_symtab_shndx_list (abfd) != NULL)
4359 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4360 if (hdr->sh_size != 0)
4361 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4362 /* FIXME: What about other symtab_shndx sections in the list ? */
4365 hdr = &elf_tdata (abfd)->strtab_hdr;
4366 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4368 elf_next_file_pos (abfd) = off;
4370 /* Now that we know where the .strtab section goes, write it
4372 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4373 || ! _bfd_elf_strtab_emit (abfd, strtab))
4375 _bfd_elf_strtab_free (strtab);
4378 abfd->output_has_begun = TRUE;
4383 /* Make an initial estimate of the size of the program header. If we
4384 get the number wrong here, we'll redo section placement. */
4386 static bfd_size_type
4387 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4391 const struct elf_backend_data *bed;
4393 /* Assume we will need exactly two PT_LOAD segments: one for text
4394 and one for data. */
4397 s = bfd_get_section_by_name (abfd, ".interp");
4398 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4400 /* If we have a loadable interpreter section, we need a
4401 PT_INTERP segment. In this case, assume we also need a
4402 PT_PHDR segment, although that may not be true for all
4407 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4409 /* We need a PT_DYNAMIC segment. */
4413 if (info != NULL && info->relro)
4415 /* We need a PT_GNU_RELRO segment. */
4419 if (elf_eh_frame_hdr (abfd))
4421 /* We need a PT_GNU_EH_FRAME segment. */
4425 if (elf_stack_flags (abfd))
4427 /* We need a PT_GNU_STACK segment. */
4431 for (s = abfd->sections; s != NULL; s = s->next)
4433 if ((s->flags & SEC_LOAD) != 0
4434 && elf_section_type (s) == SHT_NOTE)
4436 unsigned int alignment_power;
4437 /* We need a PT_NOTE segment. */
4439 /* Try to create just one PT_NOTE segment for all adjacent
4440 loadable SHT_NOTE sections. gABI requires that within a
4441 PT_NOTE segment (and also inside of each SHT_NOTE section)
4442 each note should have the same alignment. So we check
4443 whether the sections are correctly aligned. */
4444 alignment_power = s->alignment_power;
4445 while (s->next != NULL
4446 && s->next->alignment_power == alignment_power
4447 && (s->next->flags & SEC_LOAD) != 0
4448 && elf_section_type (s->next) == SHT_NOTE)
4453 for (s = abfd->sections; s != NULL; s = s->next)
4455 if (s->flags & SEC_THREAD_LOCAL)
4457 /* We need a PT_TLS segment. */
4463 bed = get_elf_backend_data (abfd);
4465 if ((abfd->flags & D_PAGED) != 0
4466 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4468 /* Add a PT_GNU_MBIND segment for each mbind section. */
4469 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4470 for (s = abfd->sections; s != NULL; s = s->next)
4471 if (elf_section_flags (s) & SHF_GNU_MBIND)
4473 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4476 /* xgettext:c-format */
4477 (_("%pB: GNU_MBIND section `%pA' has invalid "
4478 "sh_info field: %d"),
4479 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4482 /* Align mbind section to page size. */
4483 if (s->alignment_power < page_align_power)
4484 s->alignment_power = page_align_power;
4489 /* Let the backend count up any program headers it might need. */
4490 if (bed->elf_backend_additional_program_headers)
4494 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4500 return segs * bed->s->sizeof_phdr;
4503 /* Find the segment that contains the output_section of section. */
4506 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4508 struct elf_segment_map *m;
4509 Elf_Internal_Phdr *p;
4511 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4517 for (i = m->count - 1; i >= 0; i--)
4518 if (m->sections[i] == section)
4525 /* Create a mapping from a set of sections to a program segment. */
4527 static struct elf_segment_map *
4528 make_mapping (bfd *abfd,
4529 asection **sections,
4534 struct elf_segment_map *m;
4539 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4540 amt += (to - from) * sizeof (asection *);
4541 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4545 m->p_type = PT_LOAD;
4546 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4547 m->sections[i - from] = *hdrpp;
4548 m->count = to - from;
4550 if (from == 0 && phdr)
4552 /* Include the headers in the first PT_LOAD segment. */
4553 m->includes_filehdr = 1;
4554 m->includes_phdrs = 1;
4560 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4563 struct elf_segment_map *
4564 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4566 struct elf_segment_map *m;
4568 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4569 sizeof (struct elf_segment_map));
4573 m->p_type = PT_DYNAMIC;
4575 m->sections[0] = dynsec;
4580 /* Possibly add or remove segments from the segment map. */
4583 elf_modify_segment_map (bfd *abfd,
4584 struct bfd_link_info *info,
4585 bfd_boolean remove_empty_load)
4587 struct elf_segment_map **m;
4588 const struct elf_backend_data *bed;
4590 /* The placement algorithm assumes that non allocated sections are
4591 not in PT_LOAD segments. We ensure this here by removing such
4592 sections from the segment map. We also remove excluded
4593 sections. Finally, any PT_LOAD segment without sections is
4595 m = &elf_seg_map (abfd);
4598 unsigned int i, new_count;
4600 for (new_count = 0, i = 0; i < (*m)->count; i++)
4602 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4603 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4604 || (*m)->p_type != PT_LOAD))
4606 (*m)->sections[new_count] = (*m)->sections[i];
4610 (*m)->count = new_count;
4612 if (remove_empty_load
4613 && (*m)->p_type == PT_LOAD
4615 && !(*m)->includes_phdrs)
4621 bed = get_elf_backend_data (abfd);
4622 if (bed->elf_backend_modify_segment_map != NULL)
4624 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4631 #define IS_TBSS(s) \
4632 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4634 /* Set up a mapping from BFD sections to program segments. */
4637 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4640 struct elf_segment_map *m;
4641 asection **sections = NULL;
4642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4643 bfd_boolean no_user_phdrs;
4645 no_user_phdrs = elf_seg_map (abfd) == NULL;
4648 info->user_phdrs = !no_user_phdrs;
4650 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4654 struct elf_segment_map *mfirst;
4655 struct elf_segment_map **pm;
4658 unsigned int hdr_index;
4659 bfd_vma maxpagesize;
4661 bfd_boolean phdr_in_segment;
4662 bfd_boolean writable;
4663 bfd_boolean executable;
4665 asection *first_tls = NULL;
4666 asection *first_mbind = NULL;
4667 asection *dynsec, *eh_frame_hdr;
4669 bfd_vma addr_mask, wrap_to = 0;
4670 bfd_size_type phdr_size;
4672 /* Select the allocated sections, and sort them. */
4674 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4675 sizeof (asection *));
4676 if (sections == NULL)
4679 /* Calculate top address, avoiding undefined behaviour of shift
4680 left operator when shift count is equal to size of type
4682 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4683 addr_mask = (addr_mask << 1) + 1;
4686 for (s = abfd->sections; s != NULL; s = s->next)
4688 if ((s->flags & SEC_ALLOC) != 0)
4692 /* A wrapping section potentially clashes with header. */
4693 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4694 wrap_to = (s->lma + s->size) & addr_mask;
4697 BFD_ASSERT (i <= bfd_count_sections (abfd));
4700 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4702 phdr_size = elf_program_header_size (abfd);
4703 if (phdr_size == (bfd_size_type) -1)
4704 phdr_size = get_program_header_size (abfd, info);
4705 phdr_size += bed->s->sizeof_ehdr;
4706 maxpagesize = bed->maxpagesize;
4707 if (maxpagesize == 0)
4709 phdr_in_segment = info != NULL && info->load_phdrs;
4711 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4712 >= (phdr_size & (maxpagesize - 1))))
4713 /* For compatibility with old scripts that may not be using
4714 SIZEOF_HEADERS, add headers when it looks like space has
4715 been left for them. */
4716 phdr_in_segment = TRUE;
4718 /* Build the mapping. */
4722 /* If we have a .interp section, then create a PT_PHDR segment for
4723 the program headers and a PT_INTERP segment for the .interp
4725 s = bfd_get_section_by_name (abfd, ".interp");
4726 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4728 amt = sizeof (struct elf_segment_map);
4729 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4733 m->p_type = PT_PHDR;
4735 m->p_flags_valid = 1;
4736 m->includes_phdrs = 1;
4737 phdr_in_segment = TRUE;
4741 amt = sizeof (struct elf_segment_map);
4742 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4746 m->p_type = PT_INTERP;
4754 /* Look through the sections. We put sections in the same program
4755 segment when the start of the second section can be placed within
4756 a few bytes of the end of the first section. */
4762 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4764 && (dynsec->flags & SEC_LOAD) == 0)
4767 if ((abfd->flags & D_PAGED) == 0)
4768 phdr_in_segment = FALSE;
4770 /* Deal with -Ttext or something similar such that the first section
4771 is not adjacent to the program headers. This is an
4772 approximation, since at this point we don't know exactly how many
4773 program headers we will need. */
4774 if (phdr_in_segment && count > 0)
4777 bfd_boolean separate_phdr = FALSE;
4779 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4781 && info->separate_code
4782 && (sections[0]->flags & SEC_CODE) != 0)
4784 /* If data sections should be separate from code and
4785 thus not executable, and the first section is
4786 executable then put the file and program headers in
4787 their own PT_LOAD. */
4788 separate_phdr = TRUE;
4789 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4790 == (sections[0]->lma & addr_mask & -maxpagesize)))
4792 /* The file and program headers are currently on the
4793 same page as the first section. Put them on the
4794 previous page if we can. */
4795 if (phdr_lma >= maxpagesize)
4796 phdr_lma -= maxpagesize;
4798 separate_phdr = FALSE;
4801 if ((sections[0]->lma & addr_mask) < phdr_lma
4802 || (sections[0]->lma & addr_mask) < phdr_size)
4803 /* If file and program headers would be placed at the end
4804 of memory then it's probably better to omit them. */
4805 phdr_in_segment = FALSE;
4806 else if (phdr_lma < wrap_to)
4807 /* If a section wraps around to where we'll be placing
4808 file and program headers, then the headers will be
4810 phdr_in_segment = FALSE;
4811 else if (separate_phdr)
4813 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4816 m->p_paddr = phdr_lma;
4818 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4819 m->p_paddr_valid = 1;
4822 phdr_in_segment = FALSE;
4826 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4829 bfd_boolean new_segment;
4833 /* See if this section and the last one will fit in the same
4836 if (last_hdr == NULL)
4838 /* If we don't have a segment yet, then we don't need a new
4839 one (we build the last one after this loop). */
4840 new_segment = FALSE;
4842 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4844 /* If this section has a different relation between the
4845 virtual address and the load address, then we need a new
4849 else if (hdr->lma < last_hdr->lma + last_size
4850 || last_hdr->lma + last_size < last_hdr->lma)
4852 /* If this section has a load address that makes it overlap
4853 the previous section, then we need a new segment. */
4856 else if ((abfd->flags & D_PAGED) != 0
4857 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4858 == (hdr->lma & -maxpagesize)))
4860 /* If we are demand paged then we can't map two disk
4861 pages onto the same memory page. */
4862 new_segment = FALSE;
4864 /* In the next test we have to be careful when last_hdr->lma is close
4865 to the end of the address space. If the aligned address wraps
4866 around to the start of the address space, then there are no more
4867 pages left in memory and it is OK to assume that the current
4868 section can be included in the current segment. */
4869 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4870 + maxpagesize > last_hdr->lma)
4871 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4872 + maxpagesize <= hdr->lma))
4874 /* If putting this section in this segment would force us to
4875 skip a page in the segment, then we need a new segment. */
4878 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4879 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4881 /* We don't want to put a loaded section after a
4882 nonloaded (ie. bss style) section in the same segment
4883 as that will force the non-loaded section to be loaded.
4884 Consider .tbss sections as loaded for this purpose. */
4887 else if ((abfd->flags & D_PAGED) == 0)
4889 /* If the file is not demand paged, which means that we
4890 don't require the sections to be correctly aligned in the
4891 file, then there is no other reason for a new segment. */
4892 new_segment = FALSE;
4894 else if (info != NULL
4895 && info->separate_code
4896 && executable != ((hdr->flags & SEC_CODE) != 0))
4901 && (hdr->flags & SEC_READONLY) == 0)
4903 /* We don't want to put a writable section in a read only
4909 /* Otherwise, we can use the same segment. */
4910 new_segment = FALSE;
4913 /* Allow interested parties a chance to override our decision. */
4914 if (last_hdr != NULL
4916 && info->callbacks->override_segment_assignment != NULL)
4918 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4924 if ((hdr->flags & SEC_READONLY) == 0)
4926 if ((hdr->flags & SEC_CODE) != 0)
4929 /* .tbss sections effectively have zero size. */
4930 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4934 /* We need a new program segment. We must create a new program
4935 header holding all the sections from hdr_index until hdr. */
4937 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4944 if ((hdr->flags & SEC_READONLY) == 0)
4949 if ((hdr->flags & SEC_CODE) == 0)
4955 /* .tbss sections effectively have zero size. */
4956 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4958 phdr_in_segment = FALSE;
4961 /* Create a final PT_LOAD program segment, but not if it's just
4963 if (last_hdr != NULL
4964 && (i - hdr_index != 1
4965 || !IS_TBSS (last_hdr)))
4967 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4975 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4978 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4985 /* For each batch of consecutive loadable SHT_NOTE sections,
4986 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4987 because if we link together nonloadable .note sections and
4988 loadable .note sections, we will generate two .note sections
4989 in the output file. */
4990 for (s = abfd->sections; s != NULL; s = s->next)
4992 if ((s->flags & SEC_LOAD) != 0
4993 && elf_section_type (s) == SHT_NOTE)
4996 unsigned int alignment_power = s->alignment_power;
4999 for (s2 = s; s2->next != NULL; s2 = s2->next)
5001 if (s2->next->alignment_power == alignment_power
5002 && (s2->next->flags & SEC_LOAD) != 0
5003 && elf_section_type (s2->next) == SHT_NOTE
5004 && align_power (s2->lma + s2->size,
5011 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5012 amt += count * sizeof (asection *);
5013 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5017 m->p_type = PT_NOTE;
5021 m->sections[m->count - count--] = s;
5022 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5025 m->sections[m->count - 1] = s;
5026 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5030 if (s->flags & SEC_THREAD_LOCAL)
5036 if (first_mbind == NULL
5037 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5041 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5044 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5045 amt += tls_count * sizeof (asection *);
5046 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5051 m->count = tls_count;
5052 /* Mandated PF_R. */
5054 m->p_flags_valid = 1;
5056 for (i = 0; i < (unsigned int) tls_count; ++i)
5058 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5061 (_("%pB: TLS sections are not adjacent:"), abfd);
5064 while (i < (unsigned int) tls_count)
5066 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5068 _bfd_error_handler (_(" TLS: %pA"), s);
5072 _bfd_error_handler (_(" non-TLS: %pA"), s);
5075 bfd_set_error (bfd_error_bad_value);
5087 && (abfd->flags & D_PAGED) != 0
5088 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5089 for (s = first_mbind; s != NULL; s = s->next)
5090 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5091 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5093 /* Mandated PF_R. */
5094 unsigned long p_flags = PF_R;
5095 if ((s->flags & SEC_READONLY) == 0)
5097 if ((s->flags & SEC_CODE) != 0)
5100 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5101 m = bfd_zalloc (abfd, amt);
5105 m->p_type = (PT_GNU_MBIND_LO
5106 + elf_section_data (s)->this_hdr.sh_info);
5108 m->p_flags_valid = 1;
5110 m->p_flags = p_flags;
5116 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5118 eh_frame_hdr = elf_eh_frame_hdr (abfd);
5119 if (eh_frame_hdr != NULL
5120 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5122 amt = sizeof (struct elf_segment_map);
5123 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5127 m->p_type = PT_GNU_EH_FRAME;
5129 m->sections[0] = eh_frame_hdr->output_section;
5135 if (elf_stack_flags (abfd))
5137 amt = sizeof (struct elf_segment_map);
5138 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5142 m->p_type = PT_GNU_STACK;
5143 m->p_flags = elf_stack_flags (abfd);
5144 m->p_align = bed->stack_align;
5145 m->p_flags_valid = 1;
5146 m->p_align_valid = m->p_align != 0;
5147 if (info->stacksize > 0)
5149 m->p_size = info->stacksize;
5150 m->p_size_valid = 1;
5157 if (info != NULL && info->relro)
5159 for (m = mfirst; m != NULL; m = m->next)
5161 if (m->p_type == PT_LOAD
5163 && m->sections[0]->vma >= info->relro_start
5164 && m->sections[0]->vma < info->relro_end)
5167 while (--i != (unsigned) -1)
5168 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5169 == (SEC_LOAD | SEC_HAS_CONTENTS))
5172 if (i != (unsigned) -1)
5177 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5180 amt = sizeof (struct elf_segment_map);
5181 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5185 m->p_type = PT_GNU_RELRO;
5192 elf_seg_map (abfd) = mfirst;
5195 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5198 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5200 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5205 if (sections != NULL)
5210 /* Sort sections by address. */
5213 elf_sort_sections (const void *arg1, const void *arg2)
5215 const asection *sec1 = *(const asection **) arg1;
5216 const asection *sec2 = *(const asection **) arg2;
5217 bfd_size_type size1, size2;
5219 /* Sort by LMA first, since this is the address used to
5220 place the section into a segment. */
5221 if (sec1->lma < sec2->lma)
5223 else if (sec1->lma > sec2->lma)
5226 /* Then sort by VMA. Normally the LMA and the VMA will be
5227 the same, and this will do nothing. */
5228 if (sec1->vma < sec2->vma)
5230 else if (sec1->vma > sec2->vma)
5233 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5235 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5241 /* If the indices are the same, do not return 0
5242 here, but continue to try the next comparison. */
5243 if (sec1->target_index - sec2->target_index != 0)
5244 return sec1->target_index - sec2->target_index;
5249 else if (TOEND (sec2))
5254 /* Sort by size, to put zero sized sections
5255 before others at the same address. */
5257 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5258 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5265 return sec1->target_index - sec2->target_index;
5268 /* Ian Lance Taylor writes:
5270 We shouldn't be using % with a negative signed number. That's just
5271 not good. We have to make sure either that the number is not
5272 negative, or that the number has an unsigned type. When the types
5273 are all the same size they wind up as unsigned. When file_ptr is a
5274 larger signed type, the arithmetic winds up as signed long long,
5277 What we're trying to say here is something like ``increase OFF by
5278 the least amount that will cause it to be equal to the VMA modulo
5280 /* In other words, something like:
5282 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5283 off_offset = off % bed->maxpagesize;
5284 if (vma_offset < off_offset)
5285 adjustment = vma_offset + bed->maxpagesize - off_offset;
5287 adjustment = vma_offset - off_offset;
5289 which can be collapsed into the expression below. */
5292 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5294 /* PR binutils/16199: Handle an alignment of zero. */
5295 if (maxpagesize == 0)
5297 return ((vma - off) % maxpagesize);
5301 print_segment_map (const struct elf_segment_map *m)
5304 const char *pt = get_segment_type (m->p_type);
5309 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5310 sprintf (buf, "LOPROC+%7.7x",
5311 (unsigned int) (m->p_type - PT_LOPROC));
5312 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5313 sprintf (buf, "LOOS+%7.7x",
5314 (unsigned int) (m->p_type - PT_LOOS));
5316 snprintf (buf, sizeof (buf), "%8.8x",
5317 (unsigned int) m->p_type);
5321 fprintf (stderr, "%s:", pt);
5322 for (j = 0; j < m->count; j++)
5323 fprintf (stderr, " %s", m->sections [j]->name);
5329 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5334 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5336 buf = bfd_zmalloc (len);
5339 ret = bfd_bwrite (buf, len, abfd) == len;
5344 /* Assign file positions to the sections based on the mapping from
5345 sections to segments. This function also sets up some fields in
5349 assign_file_positions_for_load_sections (bfd *abfd,
5350 struct bfd_link_info *link_info)
5352 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5353 struct elf_segment_map *m;
5354 Elf_Internal_Phdr *phdrs;
5355 Elf_Internal_Phdr *p;
5357 bfd_size_type maxpagesize;
5358 unsigned int pt_load_count = 0;
5361 bfd_vma header_pad = 0;
5363 if (link_info == NULL
5364 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5368 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5372 header_pad = m->header_size;
5377 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5378 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5382 /* PR binutils/12467. */
5383 elf_elfheader (abfd)->e_phoff = 0;
5384 elf_elfheader (abfd)->e_phentsize = 0;
5387 elf_elfheader (abfd)->e_phnum = alloc;
5389 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5390 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5392 BFD_ASSERT (elf_program_header_size (abfd)
5393 >= alloc * bed->s->sizeof_phdr);
5397 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5401 /* We're writing the size in elf_program_header_size (abfd),
5402 see assign_file_positions_except_relocs, so make sure we have
5403 that amount allocated, with trailing space cleared.
5404 The variable alloc contains the computed need, while
5405 elf_program_header_size (abfd) contains the size used for the
5407 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5408 where the layout is forced to according to a larger size in the
5409 last iterations for the testcase ld-elf/header. */
5410 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
5412 phdrs = (Elf_Internal_Phdr *)
5414 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5415 sizeof (Elf_Internal_Phdr));
5416 elf_tdata (abfd)->phdr = phdrs;
5421 if ((abfd->flags & D_PAGED) != 0)
5422 maxpagesize = bed->maxpagesize;
5424 off = bed->s->sizeof_ehdr;
5425 off += alloc * bed->s->sizeof_phdr;
5426 if (header_pad < (bfd_vma) off)
5432 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
5434 m = m->next, p++, j++)
5438 bfd_boolean no_contents;
5440 /* If elf_segment_map is not from map_sections_to_segments, the
5441 sections may not be correctly ordered. NOTE: sorting should
5442 not be done to the PT_NOTE section of a corefile, which may
5443 contain several pseudo-sections artificially created by bfd.
5444 Sorting these pseudo-sections breaks things badly. */
5446 && !(elf_elfheader (abfd)->e_type == ET_CORE
5447 && m->p_type == PT_NOTE))
5448 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5451 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5452 number of sections with contents contributing to both p_filesz
5453 and p_memsz, followed by a number of sections with no contents
5454 that just contribute to p_memsz. In this loop, OFF tracks next
5455 available file offset for PT_LOAD and PT_NOTE segments. */
5456 p->p_type = m->p_type;
5457 p->p_flags = m->p_flags;
5460 p->p_vaddr = m->p_vaddr_offset;
5462 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
5464 if (m->p_paddr_valid)
5465 p->p_paddr = m->p_paddr;
5466 else if (m->count == 0)
5469 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
5471 if (p->p_type == PT_LOAD
5472 && (abfd->flags & D_PAGED) != 0)
5474 /* p_align in demand paged PT_LOAD segments effectively stores
5475 the maximum page size. When copying an executable with
5476 objcopy, we set m->p_align from the input file. Use this
5477 value for maxpagesize rather than bed->maxpagesize, which
5478 may be different. Note that we use maxpagesize for PT_TLS
5479 segment alignment later in this function, so we are relying
5480 on at least one PT_LOAD segment appearing before a PT_TLS
5482 if (m->p_align_valid)
5483 maxpagesize = m->p_align;
5485 p->p_align = maxpagesize;
5488 else if (m->p_align_valid)
5489 p->p_align = m->p_align;
5490 else if (m->count == 0)
5491 p->p_align = 1 << bed->s->log_file_align;
5495 no_contents = FALSE;
5497 if (p->p_type == PT_LOAD
5500 bfd_size_type align;
5501 unsigned int align_power = 0;
5503 if (m->p_align_valid)
5507 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5509 unsigned int secalign;
5511 secalign = bfd_get_section_alignment (abfd, *secpp);
5512 if (secalign > align_power)
5513 align_power = secalign;
5515 align = (bfd_size_type) 1 << align_power;
5516 if (align < maxpagesize)
5517 align = maxpagesize;
5520 for (i = 0; i < m->count; i++)
5521 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5522 /* If we aren't making room for this section, then
5523 it must be SHT_NOBITS regardless of what we've
5524 set via struct bfd_elf_special_section. */
5525 elf_section_type (m->sections[i]) = SHT_NOBITS;
5527 /* Find out whether this segment contains any loadable
5530 for (i = 0; i < m->count; i++)
5531 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5533 no_contents = FALSE;
5537 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5539 /* Broken hardware and/or kernel require that files do not
5540 map the same page with different permissions on some hppa
5542 if (pt_load_count > 1
5543 && bed->no_page_alias
5544 && (off & (maxpagesize - 1)) != 0
5545 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5546 off_adjust += maxpagesize;
5550 /* We shouldn't need to align the segment on disk since
5551 the segment doesn't need file space, but the gABI
5552 arguably requires the alignment and glibc ld.so
5553 checks it. So to comply with the alignment
5554 requirement but not waste file space, we adjust
5555 p_offset for just this segment. (OFF_ADJUST is
5556 subtracted from OFF later.) This may put p_offset
5557 past the end of file, but that shouldn't matter. */
5562 /* Make sure the .dynamic section is the first section in the
5563 PT_DYNAMIC segment. */
5564 else if (p->p_type == PT_DYNAMIC
5566 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5569 (_("%pB: The first section in the PT_DYNAMIC segment"
5570 " is not the .dynamic section"),
5572 bfd_set_error (bfd_error_bad_value);
5575 /* Set the note section type to SHT_NOTE. */
5576 else if (p->p_type == PT_NOTE)
5577 for (i = 0; i < m->count; i++)
5578 elf_section_type (m->sections[i]) = SHT_NOTE;
5584 if (m->includes_filehdr)
5586 if (!m->p_flags_valid)
5588 p->p_filesz = bed->s->sizeof_ehdr;
5589 p->p_memsz = bed->s->sizeof_ehdr;
5592 if (p->p_vaddr < (bfd_vma) off
5593 || (!m->p_paddr_valid
5594 && p->p_paddr < (bfd_vma) off))
5597 (_("%pB: not enough room for program headers,"
5598 " try linking with -N"),
5600 bfd_set_error (bfd_error_bad_value);
5605 if (!m->p_paddr_valid)
5610 if (m->includes_phdrs)
5612 if (!m->p_flags_valid)
5615 if (!m->includes_filehdr)
5617 p->p_offset = bed->s->sizeof_ehdr;
5621 p->p_vaddr -= off - p->p_offset;
5622 if (!m->p_paddr_valid)
5623 p->p_paddr -= off - p->p_offset;
5627 p->p_filesz += alloc * bed->s->sizeof_phdr;
5628 p->p_memsz += alloc * bed->s->sizeof_phdr;
5631 p->p_filesz += header_pad;
5632 p->p_memsz += header_pad;
5636 if (p->p_type == PT_LOAD
5637 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5639 if (!m->includes_filehdr && !m->includes_phdrs)
5645 adjust = off - (p->p_offset + p->p_filesz);
5647 p->p_filesz += adjust;
5648 p->p_memsz += adjust;
5652 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5653 maps. Set filepos for sections in PT_LOAD segments, and in
5654 core files, for sections in PT_NOTE segments.
5655 assign_file_positions_for_non_load_sections will set filepos
5656 for other sections and update p_filesz for other segments. */
5657 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5660 bfd_size_type align;
5661 Elf_Internal_Shdr *this_hdr;
5664 this_hdr = &elf_section_data (sec)->this_hdr;
5665 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5667 if ((p->p_type == PT_LOAD
5668 || p->p_type == PT_TLS)
5669 && (this_hdr->sh_type != SHT_NOBITS
5670 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5671 && ((this_hdr->sh_flags & SHF_TLS) == 0
5672 || p->p_type == PT_TLS))))
5674 bfd_vma p_start = p->p_paddr;
5675 bfd_vma p_end = p_start + p->p_memsz;
5676 bfd_vma s_start = sec->lma;
5677 bfd_vma adjust = s_start - p_end;
5681 || p_end < p_start))
5684 /* xgettext:c-format */
5685 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5686 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
5690 p->p_memsz += adjust;
5692 if (this_hdr->sh_type != SHT_NOBITS)
5694 if (p->p_filesz + adjust < p->p_memsz)
5696 /* We have a PROGBITS section following NOBITS ones.
5697 Allocate file space for the NOBITS section(s) and
5699 adjust = p->p_memsz - p->p_filesz;
5700 if (!write_zeros (abfd, off, adjust))
5704 p->p_filesz += adjust;
5708 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5710 /* The section at i == 0 is the one that actually contains
5714 this_hdr->sh_offset = sec->filepos = off;
5715 off += this_hdr->sh_size;
5716 p->p_filesz = this_hdr->sh_size;
5722 /* The rest are fake sections that shouldn't be written. */
5731 if (p->p_type == PT_LOAD)
5733 this_hdr->sh_offset = sec->filepos = off;
5734 if (this_hdr->sh_type != SHT_NOBITS)
5735 off += this_hdr->sh_size;
5737 else if (this_hdr->sh_type == SHT_NOBITS
5738 && (this_hdr->sh_flags & SHF_TLS) != 0
5739 && this_hdr->sh_offset == 0)
5741 /* This is a .tbss section that didn't get a PT_LOAD.
5742 (See _bfd_elf_map_sections_to_segments "Create a
5743 final PT_LOAD".) Set sh_offset to the value it
5744 would have if we had created a zero p_filesz and
5745 p_memsz PT_LOAD header for the section. This
5746 also makes the PT_TLS header have the same
5748 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5750 this_hdr->sh_offset = sec->filepos = off + adjust;
5753 if (this_hdr->sh_type != SHT_NOBITS)
5755 p->p_filesz += this_hdr->sh_size;
5756 /* A load section without SHF_ALLOC is something like
5757 a note section in a PT_NOTE segment. These take
5758 file space but are not loaded into memory. */
5759 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5760 p->p_memsz += this_hdr->sh_size;
5762 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5764 if (p->p_type == PT_TLS)
5765 p->p_memsz += this_hdr->sh_size;
5767 /* .tbss is special. It doesn't contribute to p_memsz of
5769 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5770 p->p_memsz += this_hdr->sh_size;
5773 if (align > p->p_align
5774 && !m->p_align_valid
5775 && (p->p_type != PT_LOAD
5776 || (abfd->flags & D_PAGED) == 0))
5780 if (!m->p_flags_valid)
5783 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5785 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5792 /* Check that all sections are in a PT_LOAD segment.
5793 Don't check funky gdb generated core files. */
5794 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5796 bfd_boolean check_vma = TRUE;
5798 for (i = 1; i < m->count; i++)
5799 if (m->sections[i]->vma == m->sections[i - 1]->vma
5800 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5801 ->this_hdr), p) != 0
5802 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5803 ->this_hdr), p) != 0)
5805 /* Looks like we have overlays packed into the segment. */
5810 for (i = 0; i < m->count; i++)
5812 Elf_Internal_Shdr *this_hdr;
5815 sec = m->sections[i];
5816 this_hdr = &(elf_section_data(sec)->this_hdr);
5817 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5818 && !ELF_TBSS_SPECIAL (this_hdr, p))
5821 /* xgettext:c-format */
5822 (_("%pB: section `%pA' can't be allocated in segment %d"),
5824 print_segment_map (m);
5830 elf_next_file_pos (abfd) = off;
5834 /* Determine if a bfd is a debuginfo file. Unfortunately there
5835 is no defined method for detecting such files, so we have to
5836 use heuristics instead. */
5839 is_debuginfo_file (bfd *abfd)
5841 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5844 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
5845 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
5846 Elf_Internal_Shdr **headerp;
5848 for (headerp = start_headers; headerp < end_headers; headerp ++)
5850 Elf_Internal_Shdr *header = * headerp;
5852 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5853 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5854 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
5855 && header->sh_type != SHT_NOBITS
5856 && header->sh_type != SHT_NOTE)
5863 /* Assign file positions for the other sections. */
5866 assign_file_positions_for_non_load_sections (bfd *abfd,
5867 struct bfd_link_info *link_info)
5869 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5870 Elf_Internal_Shdr **i_shdrpp;
5871 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5872 Elf_Internal_Phdr *phdrs;
5873 Elf_Internal_Phdr *p;
5874 struct elf_segment_map *m;
5875 struct elf_segment_map *hdrs_segment;
5876 bfd_vma filehdr_vaddr, filehdr_paddr;
5877 bfd_vma phdrs_vaddr, phdrs_paddr;
5881 i_shdrpp = elf_elfsections (abfd);
5882 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5883 off = elf_next_file_pos (abfd);
5884 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5886 Elf_Internal_Shdr *hdr;
5889 if (hdr->bfd_section != NULL
5890 && (hdr->bfd_section->filepos != 0
5891 || (hdr->sh_type == SHT_NOBITS
5892 && hdr->contents == NULL)))
5893 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5894 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5896 if (hdr->sh_size != 0
5897 /* PR 24717 - debuginfo files are known to be not strictly
5898 compliant with the ELF standard. In particular they often
5899 have .note.gnu.property sections that are outside of any
5900 loadable segment. This is not a problem for such files,
5901 so do not warn about them. */
5902 && ! is_debuginfo_file (abfd))
5904 /* xgettext:c-format */
5905 (_("%pB: warning: allocated section `%s' not in segment"),
5907 (hdr->bfd_section == NULL
5909 : hdr->bfd_section->name));
5910 /* We don't need to page align empty sections. */
5911 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5912 off += vma_page_aligned_bias (hdr->sh_addr, off,
5915 off += vma_page_aligned_bias (hdr->sh_addr, off,
5917 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5920 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5921 && hdr->bfd_section == NULL)
5922 || (hdr->bfd_section != NULL
5923 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5924 /* Compress DWARF debug sections. */
5925 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5926 || (elf_symtab_shndx_list (abfd) != NULL
5927 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5928 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5929 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5930 hdr->sh_offset = -1;
5932 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5935 /* Now that we have set the section file positions, we can set up
5936 the file positions for the non PT_LOAD segments. */
5940 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5942 hdrs_segment = NULL;
5943 phdrs = elf_tdata (abfd)->phdr;
5944 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5947 if (p->p_type != PT_LOAD)
5950 if (m->includes_filehdr)
5952 filehdr_vaddr = p->p_vaddr;
5953 filehdr_paddr = p->p_paddr;
5955 if (m->includes_phdrs)
5957 phdrs_vaddr = p->p_vaddr;
5958 phdrs_paddr = p->p_paddr;
5959 if (m->includes_filehdr)
5962 phdrs_vaddr += bed->s->sizeof_ehdr;
5963 phdrs_paddr += bed->s->sizeof_ehdr;
5968 if (hdrs_segment != NULL && link_info != NULL)
5970 /* There is a segment that contains both the file headers and the
5971 program headers, so provide a symbol __ehdr_start pointing there.
5972 A program can use this to examine itself robustly. */
5974 struct elf_link_hash_entry *hash
5975 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5976 FALSE, FALSE, TRUE);
5977 /* If the symbol was referenced and not defined, define it. */
5979 && (hash->root.type == bfd_link_hash_new
5980 || hash->root.type == bfd_link_hash_undefined
5981 || hash->root.type == bfd_link_hash_undefweak
5982 || hash->root.type == bfd_link_hash_common))
5985 if (hdrs_segment->count != 0)
5986 /* The segment contains sections, so use the first one. */
5987 s = hdrs_segment->sections[0];
5989 /* Use the first (i.e. lowest-addressed) section in any segment. */
5990 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5999 hash->root.u.def.value = filehdr_vaddr - s->vma;
6000 hash->root.u.def.section = s;
6004 hash->root.u.def.value = filehdr_vaddr;
6005 hash->root.u.def.section = bfd_abs_section_ptr;
6008 hash->root.type = bfd_link_hash_defined;
6009 hash->def_regular = 1;
6014 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6016 if (p->p_type == PT_GNU_RELRO)
6021 if (link_info != NULL)
6023 /* During linking the range of the RELRO segment is passed
6024 in link_info. Note that there may be padding between
6025 relro_start and the first RELRO section. */
6026 start = link_info->relro_start;
6027 end = link_info->relro_end;
6029 else if (m->count != 0)
6031 if (!m->p_size_valid)
6033 start = m->sections[0]->vma;
6034 end = start + m->p_size;
6045 struct elf_segment_map *lm;
6046 const Elf_Internal_Phdr *lp;
6049 /* Find a LOAD segment containing a section in the RELRO
6051 for (lm = elf_seg_map (abfd), lp = phdrs;
6053 lm = lm->next, lp++)
6055 if (lp->p_type == PT_LOAD
6057 && (lm->sections[lm->count - 1]->vma
6058 + (!IS_TBSS (lm->sections[lm->count - 1])
6059 ? lm->sections[lm->count - 1]->size
6061 && lm->sections[0]->vma < end)
6067 /* Find the section starting the RELRO segment. */
6068 for (i = 0; i < lm->count; i++)
6070 asection *s = lm->sections[i];
6079 p->p_vaddr = lm->sections[i]->vma;
6080 p->p_paddr = lm->sections[i]->lma;
6081 p->p_offset = lm->sections[i]->filepos;
6082 p->p_memsz = end - p->p_vaddr;
6083 p->p_filesz = p->p_memsz;
6085 /* The RELRO segment typically ends a few bytes
6086 into .got.plt but other layouts are possible.
6087 In cases where the end does not match any
6088 loaded section (for instance is in file
6089 padding), trim p_filesz back to correspond to
6090 the end of loaded section contents. */
6091 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6092 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6094 /* Preserve the alignment and flags if they are
6095 valid. The gold linker generates RW/4 for
6096 the PT_GNU_RELRO section. It is better for
6097 objcopy/strip to honor these attributes
6098 otherwise gdb will choke when using separate
6100 if (!m->p_align_valid)
6102 if (!m->p_flags_valid)
6108 if (link_info != NULL)
6111 memset (p, 0, sizeof *p);
6113 else if (p->p_type == PT_GNU_STACK)
6115 if (m->p_size_valid)
6116 p->p_memsz = m->p_size;
6118 else if (m->count != 0)
6122 if (p->p_type != PT_LOAD
6123 && (p->p_type != PT_NOTE
6124 || bfd_get_format (abfd) != bfd_core))
6126 /* A user specified segment layout may include a PHDR
6127 segment that overlaps with a LOAD segment... */
6128 if (p->p_type == PT_PHDR)
6134 if (m->includes_filehdr || m->includes_phdrs)
6136 /* PR 17512: file: 2195325e. */
6138 (_("%pB: error: non-load segment %d includes file header "
6139 "and/or program header"),
6140 abfd, (int) (p - phdrs));
6145 p->p_offset = m->sections[0]->filepos;
6146 for (i = m->count; i-- != 0;)
6148 asection *sect = m->sections[i];
6149 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6150 if (hdr->sh_type != SHT_NOBITS)
6152 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6159 else if (m->includes_filehdr)
6161 p->p_vaddr = filehdr_vaddr;
6162 if (! m->p_paddr_valid)
6163 p->p_paddr = filehdr_paddr;
6165 else if (m->includes_phdrs)
6167 p->p_vaddr = phdrs_vaddr;
6168 if (! m->p_paddr_valid)
6169 p->p_paddr = phdrs_paddr;
6173 elf_next_file_pos (abfd) = off;
6178 static elf_section_list *
6179 find_section_in_list (unsigned int i, elf_section_list * list)
6181 for (;list != NULL; list = list->next)
6187 /* Work out the file positions of all the sections. This is called by
6188 _bfd_elf_compute_section_file_positions. All the section sizes and
6189 VMAs must be known before this is called.
6191 Reloc sections come in two flavours: Those processed specially as
6192 "side-channel" data attached to a section to which they apply, and
6193 those that bfd doesn't process as relocations. The latter sort are
6194 stored in a normal bfd section by bfd_section_from_shdr. We don't
6195 consider the former sort here, unless they form part of the loadable
6196 image. Reloc sections not assigned here will be handled later by
6197 assign_file_positions_for_relocs.
6199 We also don't set the positions of the .symtab and .strtab here. */
6202 assign_file_positions_except_relocs (bfd *abfd,
6203 struct bfd_link_info *link_info)
6205 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6206 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6207 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6209 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6210 && bfd_get_format (abfd) != bfd_core)
6212 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6213 unsigned int num_sec = elf_numsections (abfd);
6214 Elf_Internal_Shdr **hdrpp;
6218 /* Start after the ELF header. */
6219 off = i_ehdrp->e_ehsize;
6221 /* We are not creating an executable, which means that we are
6222 not creating a program header, and that the actual order of
6223 the sections in the file is unimportant. */
6224 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6226 Elf_Internal_Shdr *hdr;
6229 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6230 && hdr->bfd_section == NULL)
6231 || (hdr->bfd_section != NULL
6232 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6233 /* Compress DWARF debug sections. */
6234 || i == elf_onesymtab (abfd)
6235 || (elf_symtab_shndx_list (abfd) != NULL
6236 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6237 || i == elf_strtab_sec (abfd)
6238 || i == elf_shstrtab_sec (abfd))
6240 hdr->sh_offset = -1;
6243 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6246 elf_next_file_pos (abfd) = off;
6252 /* Assign file positions for the loaded sections based on the
6253 assignment of sections to segments. */
6254 if (!assign_file_positions_for_load_sections (abfd, link_info))
6257 /* And for non-load sections. */
6258 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6261 if (bed->elf_backend_modify_program_headers != NULL)
6263 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6267 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6268 if (link_info != NULL && bfd_link_pie (link_info))
6270 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6271 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6272 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6274 /* Find the lowest p_vaddr in PT_LOAD segments. */
6275 bfd_vma p_vaddr = (bfd_vma) -1;
6276 for (; segment < end_segment; segment++)
6277 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6278 p_vaddr = segment->p_vaddr;
6280 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6281 segments is non-zero. */
6283 i_ehdrp->e_type = ET_EXEC;
6286 /* Write out the program headers. */
6287 alloc = elf_elfheader (abfd)->e_phnum;
6291 /* PR ld/20815 - Check that the program header segment, if present, will
6292 be loaded into memory. FIXME: The check below is not sufficient as
6293 really all PT_LOAD segments should be checked before issuing an error
6294 message. Plus the PHDR segment does not have to be the first segment
6295 in the program header table. But this version of the check should
6296 catch all real world use cases.
6298 FIXME: We used to have code here to sort the PT_LOAD segments into
6299 ascending order, as per the ELF spec. But this breaks some programs,
6300 including the Linux kernel. But really either the spec should be
6301 changed or the programs updated. */
6303 && tdata->phdr[0].p_type == PT_PHDR
6304 && (bed->elf_backend_allow_non_load_phdr == NULL
6305 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6307 && tdata->phdr[1].p_type == PT_LOAD
6308 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6309 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6310 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
6312 /* The fix for this error is usually to edit the linker script being
6313 used and set up the program headers manually. Either that or
6314 leave room for the headers at the start of the SECTIONS. */
6315 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6316 " by LOAD segment"),
6321 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
6322 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6330 prep_headers (bfd *abfd)
6332 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6333 struct elf_strtab_hash *shstrtab;
6334 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6336 i_ehdrp = elf_elfheader (abfd);
6338 shstrtab = _bfd_elf_strtab_init ();
6339 if (shstrtab == NULL)
6342 elf_shstrtab (abfd) = shstrtab;
6344 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6345 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6346 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6347 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6349 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6350 i_ehdrp->e_ident[EI_DATA] =
6351 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6352 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6354 if ((abfd->flags & DYNAMIC) != 0)
6355 i_ehdrp->e_type = ET_DYN;
6356 else if ((abfd->flags & EXEC_P) != 0)
6357 i_ehdrp->e_type = ET_EXEC;
6358 else if (bfd_get_format (abfd) == bfd_core)
6359 i_ehdrp->e_type = ET_CORE;
6361 i_ehdrp->e_type = ET_REL;
6363 switch (bfd_get_arch (abfd))
6365 case bfd_arch_unknown:
6366 i_ehdrp->e_machine = EM_NONE;
6369 /* There used to be a long list of cases here, each one setting
6370 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6371 in the corresponding bfd definition. To avoid duplication,
6372 the switch was removed. Machines that need special handling
6373 can generally do it in elf_backend_final_write_processing(),
6374 unless they need the information earlier than the final write.
6375 Such need can generally be supplied by replacing the tests for
6376 e_machine with the conditions used to determine it. */
6378 i_ehdrp->e_machine = bed->elf_machine_code;
6381 i_ehdrp->e_version = bed->s->ev_current;
6382 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6384 /* No program header, for now. */
6385 i_ehdrp->e_phoff = 0;
6386 i_ehdrp->e_phentsize = 0;
6387 i_ehdrp->e_phnum = 0;
6389 /* Each bfd section is section header entry. */
6390 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6391 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6393 /* If we're building an executable, we'll need a program header table. */
6394 if (abfd->flags & EXEC_P)
6395 /* It all happens later. */
6399 i_ehdrp->e_phentsize = 0;
6400 i_ehdrp->e_phoff = 0;
6403 elf_tdata (abfd)->symtab_hdr.sh_name =
6404 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6405 elf_tdata (abfd)->strtab_hdr.sh_name =
6406 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6407 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6408 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6409 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6410 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6411 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6417 /* Assign file positions for all the reloc sections which are not part
6418 of the loadable file image, and the file position of section headers. */
6421 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6424 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6425 Elf_Internal_Shdr *shdrp;
6426 Elf_Internal_Ehdr *i_ehdrp;
6427 const struct elf_backend_data *bed;
6429 off = elf_next_file_pos (abfd);
6431 shdrpp = elf_elfsections (abfd);
6432 end_shdrpp = shdrpp + elf_numsections (abfd);
6433 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6436 if (shdrp->sh_offset == -1)
6438 asection *sec = shdrp->bfd_section;
6439 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6440 || shdrp->sh_type == SHT_RELA);
6442 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6446 const char *name = sec->name;
6447 struct bfd_elf_section_data *d;
6449 /* Compress DWARF debug sections. */
6450 if (!bfd_compress_section (abfd, sec,
6454 if (sec->compress_status == COMPRESS_SECTION_DONE
6455 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6457 /* If section is compressed with zlib-gnu, convert
6458 section name from .debug_* to .zdebug_*. */
6460 = convert_debug_to_zdebug (abfd, name);
6461 if (new_name == NULL)
6465 /* Add section name to section name section. */
6466 if (shdrp->sh_name != (unsigned int) -1)
6469 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6471 d = elf_section_data (sec);
6473 /* Add reloc section name to section name section. */
6475 && !_bfd_elf_set_reloc_sh_name (abfd,
6480 && !_bfd_elf_set_reloc_sh_name (abfd,
6485 /* Update section size and contents. */
6486 shdrp->sh_size = sec->size;
6487 shdrp->contents = sec->contents;
6488 shdrp->bfd_section->contents = NULL;
6490 off = _bfd_elf_assign_file_position_for_section (shdrp,
6497 /* Place section name section after DWARF debug sections have been
6499 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6500 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6501 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6502 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6504 /* Place the section headers. */
6505 i_ehdrp = elf_elfheader (abfd);
6506 bed = get_elf_backend_data (abfd);
6507 off = align_file_position (off, 1 << bed->s->log_file_align);
6508 i_ehdrp->e_shoff = off;
6509 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6510 elf_next_file_pos (abfd) = off;
6516 _bfd_elf_write_object_contents (bfd *abfd)
6518 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6519 Elf_Internal_Shdr **i_shdrp;
6521 unsigned int count, num_sec;
6522 struct elf_obj_tdata *t;
6524 if (! abfd->output_has_begun
6525 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6527 /* Do not rewrite ELF data when the BFD has been opened for update.
6528 abfd->output_has_begun was set to TRUE on opening, so creation of new
6529 sections, and modification of existing section sizes was restricted.
6530 This means the ELF header, program headers and section headers can't have
6532 If the contents of any sections has been modified, then those changes have
6533 already been written to the BFD. */
6534 else if (abfd->direction == both_direction)
6536 BFD_ASSERT (abfd->output_has_begun);
6540 i_shdrp = elf_elfsections (abfd);
6543 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6547 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6550 /* After writing the headers, we need to write the sections too... */
6551 num_sec = elf_numsections (abfd);
6552 for (count = 1; count < num_sec; count++)
6554 i_shdrp[count]->sh_name
6555 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6556 i_shdrp[count]->sh_name);
6557 if (bed->elf_backend_section_processing)
6558 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6560 if (i_shdrp[count]->contents)
6562 bfd_size_type amt = i_shdrp[count]->sh_size;
6564 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6565 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6570 /* Write out the section header names. */
6571 t = elf_tdata (abfd);
6572 if (elf_shstrtab (abfd) != NULL
6573 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6574 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6577 if (!(*bed->elf_backend_final_write_processing) (abfd))
6580 if (!bed->s->write_shdrs_and_ehdr (abfd))
6583 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6584 if (t->o->build_id.after_write_object_contents != NULL)
6585 return (*t->o->build_id.after_write_object_contents) (abfd);
6591 _bfd_elf_write_corefile_contents (bfd *abfd)
6593 /* Hopefully this can be done just like an object file. */
6594 return _bfd_elf_write_object_contents (abfd);
6597 /* Given a section, search the header to find them. */
6600 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6602 const struct elf_backend_data *bed;
6603 unsigned int sec_index;
6605 if (elf_section_data (asect) != NULL
6606 && elf_section_data (asect)->this_idx != 0)
6607 return elf_section_data (asect)->this_idx;
6609 if (bfd_is_abs_section (asect))
6610 sec_index = SHN_ABS;
6611 else if (bfd_is_com_section (asect))
6612 sec_index = SHN_COMMON;
6613 else if (bfd_is_und_section (asect))
6614 sec_index = SHN_UNDEF;
6616 sec_index = SHN_BAD;
6618 bed = get_elf_backend_data (abfd);
6619 if (bed->elf_backend_section_from_bfd_section)
6621 int retval = sec_index;
6623 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6627 if (sec_index == SHN_BAD)
6628 bfd_set_error (bfd_error_nonrepresentable_section);
6633 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6637 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6639 asymbol *asym_ptr = *asym_ptr_ptr;
6641 flagword flags = asym_ptr->flags;
6643 /* When gas creates relocations against local labels, it creates its
6644 own symbol for the section, but does put the symbol into the
6645 symbol chain, so udata is 0. When the linker is generating
6646 relocatable output, this section symbol may be for one of the
6647 input sections rather than the output section. */
6648 if (asym_ptr->udata.i == 0
6649 && (flags & BSF_SECTION_SYM)
6650 && asym_ptr->section)
6655 sec = asym_ptr->section;
6656 if (sec->owner != abfd && sec->output_section != NULL)
6657 sec = sec->output_section;
6658 if (sec->owner == abfd
6659 && (indx = sec->index) < elf_num_section_syms (abfd)
6660 && elf_section_syms (abfd)[indx] != NULL)
6661 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6664 idx = asym_ptr->udata.i;
6668 /* This case can occur when using --strip-symbol on a symbol
6669 which is used in a relocation entry. */
6671 /* xgettext:c-format */
6672 (_("%pB: symbol `%s' required but not present"),
6673 abfd, bfd_asymbol_name (asym_ptr));
6674 bfd_set_error (bfd_error_no_symbols);
6681 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6682 (long) asym_ptr, asym_ptr->name, idx, flags);
6690 /* Rewrite program header information. */
6693 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6695 Elf_Internal_Ehdr *iehdr;
6696 struct elf_segment_map *map;
6697 struct elf_segment_map *map_first;
6698 struct elf_segment_map **pointer_to_map;
6699 Elf_Internal_Phdr *segment;
6702 unsigned int num_segments;
6703 bfd_boolean phdr_included = FALSE;
6704 bfd_boolean p_paddr_valid;
6705 bfd_vma maxpagesize;
6706 struct elf_segment_map *phdr_adjust_seg = NULL;
6707 unsigned int phdr_adjust_num = 0;
6708 const struct elf_backend_data *bed;
6710 bed = get_elf_backend_data (ibfd);
6711 iehdr = elf_elfheader (ibfd);
6714 pointer_to_map = &map_first;
6716 num_segments = elf_elfheader (ibfd)->e_phnum;
6717 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6719 /* Returns the end address of the segment + 1. */
6720 #define SEGMENT_END(segment, start) \
6721 (start + (segment->p_memsz > segment->p_filesz \
6722 ? segment->p_memsz : segment->p_filesz))
6724 #define SECTION_SIZE(section, segment) \
6725 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6726 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6727 ? section->size : 0)
6729 /* Returns TRUE if the given section is contained within
6730 the given segment. VMA addresses are compared. */
6731 #define IS_CONTAINED_BY_VMA(section, segment) \
6732 (section->vma >= segment->p_vaddr \
6733 && (section->vma + SECTION_SIZE (section, segment) \
6734 <= (SEGMENT_END (segment, segment->p_vaddr))))
6736 /* Returns TRUE if the given section is contained within
6737 the given segment. LMA addresses are compared. */
6738 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6739 (section->lma >= base \
6740 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6741 && (section->lma + SECTION_SIZE (section, segment) \
6742 <= SEGMENT_END (segment, base)))
6744 /* Handle PT_NOTE segment. */
6745 #define IS_NOTE(p, s) \
6746 (p->p_type == PT_NOTE \
6747 && elf_section_type (s) == SHT_NOTE \
6748 && (bfd_vma) s->filepos >= p->p_offset \
6749 && ((bfd_vma) s->filepos + s->size \
6750 <= p->p_offset + p->p_filesz))
6752 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6754 #define IS_COREFILE_NOTE(p, s) \
6756 && bfd_get_format (ibfd) == bfd_core \
6760 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6761 linker, which generates a PT_INTERP section with p_vaddr and
6762 p_memsz set to 0. */
6763 #define IS_SOLARIS_PT_INTERP(p, s) \
6765 && p->p_paddr == 0 \
6766 && p->p_memsz == 0 \
6767 && p->p_filesz > 0 \
6768 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6770 && (bfd_vma) s->filepos >= p->p_offset \
6771 && ((bfd_vma) s->filepos + s->size \
6772 <= p->p_offset + p->p_filesz))
6774 /* Decide if the given section should be included in the given segment.
6775 A section will be included if:
6776 1. It is within the address space of the segment -- we use the LMA
6777 if that is set for the segment and the VMA otherwise,
6778 2. It is an allocated section or a NOTE section in a PT_NOTE
6780 3. There is an output section associated with it,
6781 4. The section has not already been allocated to a previous segment.
6782 5. PT_GNU_STACK segments do not include any sections.
6783 6. PT_TLS segment includes only SHF_TLS sections.
6784 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6785 8. PT_DYNAMIC should not contain empty sections at the beginning
6786 (with the possible exception of .dynamic). */
6787 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6788 ((((segment->p_paddr \
6789 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6790 : IS_CONTAINED_BY_VMA (section, segment)) \
6791 && (section->flags & SEC_ALLOC) != 0) \
6792 || IS_NOTE (segment, section)) \
6793 && segment->p_type != PT_GNU_STACK \
6794 && (segment->p_type != PT_TLS \
6795 || (section->flags & SEC_THREAD_LOCAL)) \
6796 && (segment->p_type == PT_LOAD \
6797 || segment->p_type == PT_TLS \
6798 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6799 && (segment->p_type != PT_DYNAMIC \
6800 || SECTION_SIZE (section, segment) > 0 \
6801 || (segment->p_paddr \
6802 ? segment->p_paddr != section->lma \
6803 : segment->p_vaddr != section->vma) \
6804 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6806 && (segment->p_type != PT_LOAD || !section->segment_mark))
6808 /* If the output section of a section in the input segment is NULL,
6809 it is removed from the corresponding output segment. */
6810 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6811 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6812 && section->output_section != NULL)
6814 /* Returns TRUE iff seg1 starts after the end of seg2. */
6815 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6816 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6818 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6819 their VMA address ranges and their LMA address ranges overlap.
6820 It is possible to have overlapping VMA ranges without overlapping LMA
6821 ranges. RedBoot images for example can have both .data and .bss mapped
6822 to the same VMA range, but with the .data section mapped to a different
6824 #define SEGMENT_OVERLAPS(seg1, seg2) \
6825 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6826 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6827 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6828 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6830 /* Initialise the segment mark field. */
6831 for (section = ibfd->sections; section != NULL; section = section->next)
6832 section->segment_mark = FALSE;
6834 /* The Solaris linker creates program headers in which all the
6835 p_paddr fields are zero. When we try to objcopy or strip such a
6836 file, we get confused. Check for this case, and if we find it
6837 don't set the p_paddr_valid fields. */
6838 p_paddr_valid = FALSE;
6839 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6842 if (segment->p_paddr != 0)
6844 p_paddr_valid = TRUE;
6848 /* Scan through the segments specified in the program header
6849 of the input BFD. For this first scan we look for overlaps
6850 in the loadable segments. These can be created by weird
6851 parameters to objcopy. Also, fix some solaris weirdness. */
6852 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6857 Elf_Internal_Phdr *segment2;
6859 if (segment->p_type == PT_INTERP)
6860 for (section = ibfd->sections; section; section = section->next)
6861 if (IS_SOLARIS_PT_INTERP (segment, section))
6863 /* Mininal change so that the normal section to segment
6864 assignment code will work. */
6865 segment->p_vaddr = section->vma;
6869 if (segment->p_type != PT_LOAD)
6871 /* Remove PT_GNU_RELRO segment. */
6872 if (segment->p_type == PT_GNU_RELRO)
6873 segment->p_type = PT_NULL;
6877 /* Determine if this segment overlaps any previous segments. */
6878 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6880 bfd_signed_vma extra_length;
6882 if (segment2->p_type != PT_LOAD
6883 || !SEGMENT_OVERLAPS (segment, segment2))
6886 /* Merge the two segments together. */
6887 if (segment2->p_vaddr < segment->p_vaddr)
6889 /* Extend SEGMENT2 to include SEGMENT and then delete
6891 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6892 - SEGMENT_END (segment2, segment2->p_vaddr));
6894 if (extra_length > 0)
6896 segment2->p_memsz += extra_length;
6897 segment2->p_filesz += extra_length;
6900 segment->p_type = PT_NULL;
6902 /* Since we have deleted P we must restart the outer loop. */
6904 segment = elf_tdata (ibfd)->phdr;
6909 /* Extend SEGMENT to include SEGMENT2 and then delete
6911 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6912 - SEGMENT_END (segment, segment->p_vaddr));
6914 if (extra_length > 0)
6916 segment->p_memsz += extra_length;
6917 segment->p_filesz += extra_length;
6920 segment2->p_type = PT_NULL;
6925 /* The second scan attempts to assign sections to segments. */
6926 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6930 unsigned int section_count;
6931 asection **sections;
6932 asection *output_section;
6934 asection *matching_lma;
6935 asection *suggested_lma;
6938 asection *first_section;
6940 if (segment->p_type == PT_NULL)
6943 first_section = NULL;
6944 /* Compute how many sections might be placed into this segment. */
6945 for (section = ibfd->sections, section_count = 0;
6947 section = section->next)
6949 /* Find the first section in the input segment, which may be
6950 removed from the corresponding output segment. */
6951 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6953 if (first_section == NULL)
6954 first_section = section;
6955 if (section->output_section != NULL)
6960 /* Allocate a segment map big enough to contain
6961 all of the sections we have selected. */
6962 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6963 amt += (bfd_size_type) section_count * sizeof (asection *);
6964 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6968 /* Initialise the fields of the segment map. Default to
6969 using the physical address of the segment in the input BFD. */
6971 map->p_type = segment->p_type;
6972 map->p_flags = segment->p_flags;
6973 map->p_flags_valid = 1;
6975 /* If the first section in the input segment is removed, there is
6976 no need to preserve segment physical address in the corresponding
6978 if (!first_section || first_section->output_section != NULL)
6980 map->p_paddr = segment->p_paddr;
6981 map->p_paddr_valid = p_paddr_valid;
6984 /* Determine if this segment contains the ELF file header
6985 and if it contains the program headers themselves. */
6986 map->includes_filehdr = (segment->p_offset == 0
6987 && segment->p_filesz >= iehdr->e_ehsize);
6988 map->includes_phdrs = 0;
6990 if (!phdr_included || segment->p_type != PT_LOAD)
6992 map->includes_phdrs =
6993 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6994 && (segment->p_offset + segment->p_filesz
6995 >= ((bfd_vma) iehdr->e_phoff
6996 + iehdr->e_phnum * iehdr->e_phentsize)));
6998 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6999 phdr_included = TRUE;
7002 if (section_count == 0)
7004 /* Special segments, such as the PT_PHDR segment, may contain
7005 no sections, but ordinary, loadable segments should contain
7006 something. They are allowed by the ELF spec however, so only
7007 a warning is produced.
7008 There is however the valid use case of embedded systems which
7009 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7010 flash memory with zeros. No warning is shown for that case. */
7011 if (segment->p_type == PT_LOAD
7012 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7013 /* xgettext:c-format */
7015 (_("%pB: warning: empty loadable segment detected"
7016 " at vaddr=%#" PRIx64 ", is this intentional?"),
7017 ibfd, (uint64_t) segment->p_vaddr);
7019 map->p_vaddr_offset = segment->p_vaddr;
7021 *pointer_to_map = map;
7022 pointer_to_map = &map->next;
7027 /* Now scan the sections in the input BFD again and attempt
7028 to add their corresponding output sections to the segment map.
7029 The problem here is how to handle an output section which has
7030 been moved (ie had its LMA changed). There are four possibilities:
7032 1. None of the sections have been moved.
7033 In this case we can continue to use the segment LMA from the
7036 2. All of the sections have been moved by the same amount.
7037 In this case we can change the segment's LMA to match the LMA
7038 of the first section.
7040 3. Some of the sections have been moved, others have not.
7041 In this case those sections which have not been moved can be
7042 placed in the current segment which will have to have its size,
7043 and possibly its LMA changed, and a new segment or segments will
7044 have to be created to contain the other sections.
7046 4. The sections have been moved, but not by the same amount.
7047 In this case we can change the segment's LMA to match the LMA
7048 of the first section and we will have to create a new segment
7049 or segments to contain the other sections.
7051 In order to save time, we allocate an array to hold the section
7052 pointers that we are interested in. As these sections get assigned
7053 to a segment, they are removed from this array. */
7055 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
7056 if (sections == NULL)
7059 /* Step One: Scan for segment vs section LMA conflicts.
7060 Also add the sections to the section array allocated above.
7061 Also add the sections to the current segment. In the common
7062 case, where the sections have not been moved, this means that
7063 we have completely filled the segment, and there is nothing
7066 matching_lma = NULL;
7067 suggested_lma = NULL;
7069 for (section = first_section, j = 0;
7071 section = section->next)
7073 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
7075 output_section = section->output_section;
7077 sections[j++] = section;
7079 /* The Solaris native linker always sets p_paddr to 0.
7080 We try to catch that case here, and set it to the
7081 correct value. Note - some backends require that
7082 p_paddr be left as zero. */
7084 && segment->p_vaddr != 0
7085 && !bed->want_p_paddr_set_to_zero
7087 && output_section->lma != 0
7088 && (align_power (segment->p_vaddr
7089 + (map->includes_filehdr
7090 ? iehdr->e_ehsize : 0)
7091 + (map->includes_phdrs
7092 ? iehdr->e_phnum * iehdr->e_phentsize
7094 output_section->alignment_power)
7095 == output_section->vma))
7096 map->p_paddr = segment->p_vaddr;
7098 /* Match up the physical address of the segment with the
7099 LMA address of the output section. */
7100 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7101 || IS_COREFILE_NOTE (segment, section)
7102 || (bed->want_p_paddr_set_to_zero
7103 && IS_CONTAINED_BY_VMA (output_section, segment)))
7105 if (matching_lma == NULL
7106 || output_section->lma < matching_lma->lma)
7107 matching_lma = output_section;
7109 /* We assume that if the section fits within the segment
7110 then it does not overlap any other section within that
7112 map->sections[isec++] = output_section;
7114 else if (suggested_lma == NULL)
7115 suggested_lma = output_section;
7117 if (j == section_count)
7122 BFD_ASSERT (j == section_count);
7124 /* Step Two: Adjust the physical address of the current segment,
7126 if (isec == section_count)
7128 /* All of the sections fitted within the segment as currently
7129 specified. This is the default case. Add the segment to
7130 the list of built segments and carry on to process the next
7131 program header in the input BFD. */
7132 map->count = section_count;
7133 *pointer_to_map = map;
7134 pointer_to_map = &map->next;
7137 && !bed->want_p_paddr_set_to_zero
7138 && matching_lma->lma != map->p_paddr
7139 && !map->includes_filehdr
7140 && !map->includes_phdrs)
7141 /* There is some padding before the first section in the
7142 segment. So, we must account for that in the output
7144 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
7151 /* Change the current segment's physical address to match
7152 the LMA of the first section that fitted, or if no
7153 section fitted, the first section. */
7154 if (matching_lma == NULL)
7155 matching_lma = suggested_lma;
7157 map->p_paddr = matching_lma->lma;
7159 /* Offset the segment physical address from the lma
7160 to allow for space taken up by elf headers. */
7161 if (map->includes_phdrs)
7163 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7165 /* iehdr->e_phnum is just an estimate of the number
7166 of program headers that we will need. Make a note
7167 here of the number we used and the segment we chose
7168 to hold these headers, so that we can adjust the
7169 offset when we know the correct value. */
7170 phdr_adjust_num = iehdr->e_phnum;
7171 phdr_adjust_seg = map;
7174 if (map->includes_filehdr)
7176 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7177 map->p_paddr -= iehdr->e_ehsize;
7178 /* We've subtracted off the size of headers from the
7179 first section lma, but there may have been some
7180 alignment padding before that section too. Try to
7181 account for that by adjusting the segment lma down to
7182 the same alignment. */
7183 if (segment->p_align != 0 && segment->p_align < align)
7184 align = segment->p_align;
7185 map->p_paddr &= -align;
7189 /* Step Three: Loop over the sections again, this time assigning
7190 those that fit to the current segment and removing them from the
7191 sections array; but making sure not to leave large gaps. Once all
7192 possible sections have been assigned to the current segment it is
7193 added to the list of built segments and if sections still remain
7194 to be assigned, a new segment is constructed before repeating
7200 suggested_lma = NULL;
7202 /* Fill the current segment with sections that fit. */
7203 for (j = 0; j < section_count; j++)
7205 section = sections[j];
7207 if (section == NULL)
7210 output_section = section->output_section;
7212 BFD_ASSERT (output_section != NULL);
7214 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7215 || IS_COREFILE_NOTE (segment, section))
7217 if (map->count == 0)
7219 /* If the first section in a segment does not start at
7220 the beginning of the segment, then something is
7222 if (align_power (map->p_paddr
7223 + (map->includes_filehdr
7224 ? iehdr->e_ehsize : 0)
7225 + (map->includes_phdrs
7226 ? iehdr->e_phnum * iehdr->e_phentsize
7228 output_section->alignment_power)
7229 != output_section->lma)
7236 prev_sec = map->sections[map->count - 1];
7238 /* If the gap between the end of the previous section
7239 and the start of this section is more than
7240 maxpagesize then we need to start a new segment. */
7241 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7243 < BFD_ALIGN (output_section->lma, maxpagesize))
7244 || (prev_sec->lma + prev_sec->size
7245 > output_section->lma))
7247 if (suggested_lma == NULL)
7248 suggested_lma = output_section;
7254 map->sections[map->count++] = output_section;
7257 if (segment->p_type == PT_LOAD)
7258 section->segment_mark = TRUE;
7260 else if (suggested_lma == NULL)
7261 suggested_lma = output_section;
7264 /* PR 23932. A corrupt input file may contain sections that cannot
7265 be assigned to any segment - because for example they have a
7266 negative size - or segments that do not contain any sections. */
7267 if (map->count == 0)
7269 bfd_set_error (bfd_error_bad_value);
7274 /* Add the current segment to the list of built segments. */
7275 *pointer_to_map = map;
7276 pointer_to_map = &map->next;
7278 if (isec < section_count)
7280 /* We still have not allocated all of the sections to
7281 segments. Create a new segment here, initialise it
7282 and carry on looping. */
7283 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7284 amt += (bfd_size_type) section_count * sizeof (asection *);
7285 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7292 /* Initialise the fields of the segment map. Set the physical
7293 physical address to the LMA of the first section that has
7294 not yet been assigned. */
7296 map->p_type = segment->p_type;
7297 map->p_flags = segment->p_flags;
7298 map->p_flags_valid = 1;
7299 map->p_paddr = suggested_lma->lma;
7300 map->p_paddr_valid = p_paddr_valid;
7301 map->includes_filehdr = 0;
7302 map->includes_phdrs = 0;
7305 while (isec < section_count);
7310 elf_seg_map (obfd) = map_first;
7312 /* If we had to estimate the number of program headers that were
7313 going to be needed, then check our estimate now and adjust
7314 the offset if necessary. */
7315 if (phdr_adjust_seg != NULL)
7319 for (count = 0, map = map_first; map != NULL; map = map->next)
7322 if (count > phdr_adjust_num)
7323 phdr_adjust_seg->p_paddr
7324 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7326 for (map = map_first; map != NULL; map = map->next)
7327 if (map->p_type == PT_PHDR)
7330 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7331 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7338 #undef IS_CONTAINED_BY_VMA
7339 #undef IS_CONTAINED_BY_LMA
7341 #undef IS_COREFILE_NOTE
7342 #undef IS_SOLARIS_PT_INTERP
7343 #undef IS_SECTION_IN_INPUT_SEGMENT
7344 #undef INCLUDE_SECTION_IN_SEGMENT
7345 #undef SEGMENT_AFTER_SEGMENT
7346 #undef SEGMENT_OVERLAPS
7350 /* Copy ELF program header information. */
7353 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7355 Elf_Internal_Ehdr *iehdr;
7356 struct elf_segment_map *map;
7357 struct elf_segment_map *map_first;
7358 struct elf_segment_map **pointer_to_map;
7359 Elf_Internal_Phdr *segment;
7361 unsigned int num_segments;
7362 bfd_boolean phdr_included = FALSE;
7363 bfd_boolean p_paddr_valid;
7365 iehdr = elf_elfheader (ibfd);
7368 pointer_to_map = &map_first;
7370 /* If all the segment p_paddr fields are zero, don't set
7371 map->p_paddr_valid. */
7372 p_paddr_valid = FALSE;
7373 num_segments = elf_elfheader (ibfd)->e_phnum;
7374 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7377 if (segment->p_paddr != 0)
7379 p_paddr_valid = TRUE;
7383 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7388 unsigned int section_count;
7390 Elf_Internal_Shdr *this_hdr;
7391 asection *first_section = NULL;
7392 asection *lowest_section;
7393 bfd_boolean no_contents = TRUE;
7395 /* Compute how many sections are in this segment. */
7396 for (section = ibfd->sections, section_count = 0;
7398 section = section->next)
7400 this_hdr = &(elf_section_data(section)->this_hdr);
7401 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7403 if (first_section == NULL)
7404 first_section = section;
7405 if (elf_section_type (section) != SHT_NOBITS)
7406 no_contents = FALSE;
7411 /* Allocate a segment map big enough to contain
7412 all of the sections we have selected. */
7413 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7414 amt += (bfd_size_type) section_count * sizeof (asection *);
7415 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7419 /* Initialize the fields of the output segment map with the
7422 map->p_type = segment->p_type;
7423 map->p_flags = segment->p_flags;
7424 map->p_flags_valid = 1;
7425 map->p_paddr = segment->p_paddr;
7426 map->p_paddr_valid = p_paddr_valid;
7427 map->p_align = segment->p_align;
7428 map->p_align_valid = 1;
7429 map->p_vaddr_offset = 0;
7431 if (map->p_type == PT_GNU_RELRO
7432 || map->p_type == PT_GNU_STACK)
7434 /* The PT_GNU_RELRO segment may contain the first a few
7435 bytes in the .got.plt section even if the whole .got.plt
7436 section isn't in the PT_GNU_RELRO segment. We won't
7437 change the size of the PT_GNU_RELRO segment.
7438 Similarly, PT_GNU_STACK size is significant on uclinux
7440 map->p_size = segment->p_memsz;
7441 map->p_size_valid = 1;
7444 /* Determine if this segment contains the ELF file header
7445 and if it contains the program headers themselves. */
7446 map->includes_filehdr = (segment->p_offset == 0
7447 && segment->p_filesz >= iehdr->e_ehsize);
7449 map->includes_phdrs = 0;
7450 if (! phdr_included || segment->p_type != PT_LOAD)
7452 map->includes_phdrs =
7453 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7454 && (segment->p_offset + segment->p_filesz
7455 >= ((bfd_vma) iehdr->e_phoff
7456 + iehdr->e_phnum * iehdr->e_phentsize)));
7458 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7459 phdr_included = TRUE;
7462 lowest_section = NULL;
7463 if (section_count != 0)
7465 unsigned int isec = 0;
7467 for (section = first_section;
7469 section = section->next)
7471 this_hdr = &(elf_section_data(section)->this_hdr);
7472 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7474 map->sections[isec++] = section->output_section;
7475 if ((section->flags & SEC_ALLOC) != 0)
7479 if (lowest_section == NULL
7480 || section->lma < lowest_section->lma)
7481 lowest_section = section;
7483 /* Section lmas are set up from PT_LOAD header
7484 p_paddr in _bfd_elf_make_section_from_shdr.
7485 If this header has a p_paddr that disagrees
7486 with the section lma, flag the p_paddr as
7488 if ((section->flags & SEC_LOAD) != 0)
7489 seg_off = this_hdr->sh_offset - segment->p_offset;
7491 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7492 if (section->lma - segment->p_paddr != seg_off)
7493 map->p_paddr_valid = FALSE;
7495 if (isec == section_count)
7501 if (map->includes_filehdr && lowest_section != NULL)
7503 /* Try to keep the space used by the headers plus any
7504 padding fixed. If there are sections with file contents
7505 in this segment then the lowest sh_offset is the best
7506 guess. Otherwise the segment only has file contents for
7507 the headers, and p_filesz is the best guess. */
7509 map->header_size = segment->p_filesz;
7511 map->header_size = lowest_section->filepos;
7514 if (section_count == 0)
7515 map->p_vaddr_offset = segment->p_vaddr;
7516 else if (!map->includes_phdrs
7517 && !map->includes_filehdr
7518 && map->p_paddr_valid)
7519 /* Account for padding before the first section. */
7520 map->p_vaddr_offset = (segment->p_paddr
7521 - (lowest_section ? lowest_section->lma : 0));
7523 map->count = section_count;
7524 *pointer_to_map = map;
7525 pointer_to_map = &map->next;
7528 elf_seg_map (obfd) = map_first;
7532 /* Copy private BFD data. This copies or rewrites ELF program header
7536 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7538 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7539 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7542 if (elf_tdata (ibfd)->phdr == NULL)
7545 if (ibfd->xvec == obfd->xvec)
7547 /* Check to see if any sections in the input BFD
7548 covered by ELF program header have changed. */
7549 Elf_Internal_Phdr *segment;
7550 asection *section, *osec;
7551 unsigned int i, num_segments;
7552 Elf_Internal_Shdr *this_hdr;
7553 const struct elf_backend_data *bed;
7555 bed = get_elf_backend_data (ibfd);
7557 /* Regenerate the segment map if p_paddr is set to 0. */
7558 if (bed->want_p_paddr_set_to_zero)
7561 /* Initialize the segment mark field. */
7562 for (section = obfd->sections; section != NULL;
7563 section = section->next)
7564 section->segment_mark = FALSE;
7566 num_segments = elf_elfheader (ibfd)->e_phnum;
7567 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7571 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7572 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7573 which severly confuses things, so always regenerate the segment
7574 map in this case. */
7575 if (segment->p_paddr == 0
7576 && segment->p_memsz == 0
7577 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7580 for (section = ibfd->sections;
7581 section != NULL; section = section->next)
7583 /* We mark the output section so that we know it comes
7584 from the input BFD. */
7585 osec = section->output_section;
7587 osec->segment_mark = TRUE;
7589 /* Check if this section is covered by the segment. */
7590 this_hdr = &(elf_section_data(section)->this_hdr);
7591 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7593 /* FIXME: Check if its output section is changed or
7594 removed. What else do we need to check? */
7596 || section->flags != osec->flags
7597 || section->lma != osec->lma
7598 || section->vma != osec->vma
7599 || section->size != osec->size
7600 || section->rawsize != osec->rawsize
7601 || section->alignment_power != osec->alignment_power)
7607 /* Check to see if any output section do not come from the
7609 for (section = obfd->sections; section != NULL;
7610 section = section->next)
7612 if (!section->segment_mark)
7615 section->segment_mark = FALSE;
7618 return copy_elf_program_header (ibfd, obfd);
7622 if (ibfd->xvec == obfd->xvec)
7624 /* When rewriting program header, set the output maxpagesize to
7625 the maximum alignment of input PT_LOAD segments. */
7626 Elf_Internal_Phdr *segment;
7628 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7629 bfd_vma maxpagesize = 0;
7631 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7634 if (segment->p_type == PT_LOAD
7635 && maxpagesize < segment->p_align)
7637 /* PR 17512: file: f17299af. */
7638 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7639 /* xgettext:c-format */
7640 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7641 PRIx64 " is too large"),
7642 ibfd, (uint64_t) segment->p_align);
7644 maxpagesize = segment->p_align;
7647 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7648 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7651 return rewrite_elf_program_header (ibfd, obfd);
7654 /* Initialize private output section information from input section. */
7657 _bfd_elf_init_private_section_data (bfd *ibfd,
7661 struct bfd_link_info *link_info)
7664 Elf_Internal_Shdr *ihdr, *ohdr;
7665 bfd_boolean final_link = (link_info != NULL
7666 && !bfd_link_relocatable (link_info));
7668 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7669 || obfd->xvec->flavour != bfd_target_elf_flavour)
7672 BFD_ASSERT (elf_section_data (osec) != NULL);
7674 /* For objcopy and relocatable link, don't copy the output ELF
7675 section type from input if the output BFD section flags have been
7676 set to something different. For a final link allow some flags
7677 that the linker clears to differ. */
7678 if (elf_section_type (osec) == SHT_NULL
7679 && (osec->flags == isec->flags
7681 && ((osec->flags ^ isec->flags)
7682 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7683 elf_section_type (osec) = elf_section_type (isec);
7685 /* FIXME: Is this correct for all OS/PROC specific flags? */
7686 elf_section_flags (osec) |= (elf_section_flags (isec)
7687 & (SHF_MASKOS | SHF_MASKPROC));
7689 /* Copy sh_info from input for mbind section. */
7690 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7691 && elf_section_flags (isec) & SHF_GNU_MBIND)
7692 elf_section_data (osec)->this_hdr.sh_info
7693 = elf_section_data (isec)->this_hdr.sh_info;
7695 /* Set things up for objcopy and relocatable link. The output
7696 SHT_GROUP section will have its elf_next_in_group pointing back
7697 to the input group members. Ignore linker created group section.
7698 See elfNN_ia64_object_p in elfxx-ia64.c. */
7699 if ((link_info == NULL
7700 || !link_info->resolve_section_groups)
7701 && (elf_sec_group (isec) == NULL
7702 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7704 if (elf_section_flags (isec) & SHF_GROUP)
7705 elf_section_flags (osec) |= SHF_GROUP;
7706 elf_next_in_group (osec) = elf_next_in_group (isec);
7707 elf_section_data (osec)->group = elf_section_data (isec)->group;
7710 /* If not decompress, preserve SHF_COMPRESSED. */
7711 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7712 elf_section_flags (osec) |= (elf_section_flags (isec)
7715 ihdr = &elf_section_data (isec)->this_hdr;
7717 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7718 don't use the output section of the linked-to section since it
7719 may be NULL at this point. */
7720 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7722 ohdr = &elf_section_data (osec)->this_hdr;
7723 ohdr->sh_flags |= SHF_LINK_ORDER;
7724 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7727 osec->use_rela_p = isec->use_rela_p;
7732 /* Copy private section information. This copies over the entsize
7733 field, and sometimes the info field. */
7736 _bfd_elf_copy_private_section_data (bfd *ibfd,
7741 Elf_Internal_Shdr *ihdr, *ohdr;
7743 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7744 || obfd->xvec->flavour != bfd_target_elf_flavour)
7747 ihdr = &elf_section_data (isec)->this_hdr;
7748 ohdr = &elf_section_data (osec)->this_hdr;
7750 ohdr->sh_entsize = ihdr->sh_entsize;
7752 if (ihdr->sh_type == SHT_SYMTAB
7753 || ihdr->sh_type == SHT_DYNSYM
7754 || ihdr->sh_type == SHT_GNU_verneed
7755 || ihdr->sh_type == SHT_GNU_verdef)
7756 ohdr->sh_info = ihdr->sh_info;
7758 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7762 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7763 necessary if we are removing either the SHT_GROUP section or any of
7764 the group member sections. DISCARDED is the value that a section's
7765 output_section has if the section will be discarded, NULL when this
7766 function is called from objcopy, bfd_abs_section_ptr when called
7770 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7774 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7775 if (elf_section_type (isec) == SHT_GROUP)
7777 asection *first = elf_next_in_group (isec);
7778 asection *s = first;
7779 bfd_size_type removed = 0;
7783 /* If this member section is being output but the
7784 SHT_GROUP section is not, then clear the group info
7785 set up by _bfd_elf_copy_private_section_data. */
7786 if (s->output_section != discarded
7787 && isec->output_section == discarded)
7789 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7790 elf_group_name (s->output_section) = NULL;
7792 /* Conversely, if the member section is not being output
7793 but the SHT_GROUP section is, then adjust its size. */
7794 else if (s->output_section == discarded
7795 && isec->output_section != discarded)
7797 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7799 if (elf_sec->rel.hdr != NULL
7800 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7802 if (elf_sec->rela.hdr != NULL
7803 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7806 s = elf_next_in_group (s);
7812 if (discarded != NULL)
7814 /* If we've been called for ld -r, then we need to
7815 adjust the input section size. */
7816 if (isec->rawsize == 0)
7817 isec->rawsize = isec->size;
7818 isec->size = isec->rawsize - removed;
7819 if (isec->size <= 4)
7822 isec->flags |= SEC_EXCLUDE;
7827 /* Adjust the output section size when called from
7829 isec->output_section->size -= removed;
7830 if (isec->output_section->size <= 4)
7832 isec->output_section->size = 0;
7833 isec->output_section->flags |= SEC_EXCLUDE;
7842 /* Copy private header information. */
7845 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7847 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7848 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7851 /* Copy over private BFD data if it has not already been copied.
7852 This must be done here, rather than in the copy_private_bfd_data
7853 entry point, because the latter is called after the section
7854 contents have been set, which means that the program headers have
7855 already been worked out. */
7856 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7858 if (! copy_private_bfd_data (ibfd, obfd))
7862 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7865 /* Copy private symbol information. If this symbol is in a section
7866 which we did not map into a BFD section, try to map the section
7867 index correctly. We use special macro definitions for the mapped
7868 section indices; these definitions are interpreted by the
7869 swap_out_syms function. */
7871 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7872 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7873 #define MAP_STRTAB (SHN_HIOS + 3)
7874 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7875 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7878 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7883 elf_symbol_type *isym, *osym;
7885 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7886 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7889 isym = elf_symbol_from (ibfd, isymarg);
7890 osym = elf_symbol_from (obfd, osymarg);
7893 && isym->internal_elf_sym.st_shndx != 0
7895 && bfd_is_abs_section (isym->symbol.section))
7899 shndx = isym->internal_elf_sym.st_shndx;
7900 if (shndx == elf_onesymtab (ibfd))
7901 shndx = MAP_ONESYMTAB;
7902 else if (shndx == elf_dynsymtab (ibfd))
7903 shndx = MAP_DYNSYMTAB;
7904 else if (shndx == elf_strtab_sec (ibfd))
7906 else if (shndx == elf_shstrtab_sec (ibfd))
7907 shndx = MAP_SHSTRTAB;
7908 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7909 shndx = MAP_SYM_SHNDX;
7910 osym->internal_elf_sym.st_shndx = shndx;
7916 /* Swap out the symbols. */
7919 swap_out_syms (bfd *abfd,
7920 struct elf_strtab_hash **sttp,
7923 const struct elf_backend_data *bed;
7926 struct elf_strtab_hash *stt;
7927 Elf_Internal_Shdr *symtab_hdr;
7928 Elf_Internal_Shdr *symtab_shndx_hdr;
7929 Elf_Internal_Shdr *symstrtab_hdr;
7930 struct elf_sym_strtab *symstrtab;
7931 bfd_byte *outbound_syms;
7932 bfd_byte *outbound_shndx;
7933 unsigned long outbound_syms_index;
7934 unsigned long outbound_shndx_index;
7936 unsigned int num_locals;
7938 bfd_boolean name_local_sections;
7940 if (!elf_map_symbols (abfd, &num_locals))
7943 /* Dump out the symtabs. */
7944 stt = _bfd_elf_strtab_init ();
7948 bed = get_elf_backend_data (abfd);
7949 symcount = bfd_get_symcount (abfd);
7950 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7951 symtab_hdr->sh_type = SHT_SYMTAB;
7952 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7953 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7954 symtab_hdr->sh_info = num_locals + 1;
7955 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7957 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7958 symstrtab_hdr->sh_type = SHT_STRTAB;
7960 /* Allocate buffer to swap out the .strtab section. */
7961 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
7962 sizeof (*symstrtab));
7963 if (symstrtab == NULL)
7965 _bfd_elf_strtab_free (stt);
7969 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7970 bed->s->sizeof_sym);
7971 if (outbound_syms == NULL)
7974 _bfd_elf_strtab_free (stt);
7978 symtab_hdr->contents = outbound_syms;
7979 outbound_syms_index = 0;
7981 outbound_shndx = NULL;
7982 outbound_shndx_index = 0;
7984 if (elf_symtab_shndx_list (abfd))
7986 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7987 if (symtab_shndx_hdr->sh_name != 0)
7989 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7990 outbound_shndx = (bfd_byte *)
7991 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7992 if (outbound_shndx == NULL)
7995 symtab_shndx_hdr->contents = outbound_shndx;
7996 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7997 symtab_shndx_hdr->sh_size = amt;
7998 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7999 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8001 /* FIXME: What about any other headers in the list ? */
8004 /* Now generate the data (for "contents"). */
8006 /* Fill in zeroth symbol and swap it out. */
8007 Elf_Internal_Sym sym;
8013 sym.st_shndx = SHN_UNDEF;
8014 sym.st_target_internal = 0;
8015 symstrtab[0].sym = sym;
8016 symstrtab[0].dest_index = outbound_syms_index;
8017 symstrtab[0].destshndx_index = outbound_shndx_index;
8018 outbound_syms_index++;
8019 if (outbound_shndx != NULL)
8020 outbound_shndx_index++;
8024 = (bed->elf_backend_name_local_section_symbols
8025 && bed->elf_backend_name_local_section_symbols (abfd));
8027 syms = bfd_get_outsymbols (abfd);
8028 for (idx = 0; idx < symcount;)
8030 Elf_Internal_Sym sym;
8031 bfd_vma value = syms[idx]->value;
8032 elf_symbol_type *type_ptr;
8033 flagword flags = syms[idx]->flags;
8036 if (!name_local_sections
8037 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8039 /* Local section symbols have no name. */
8040 sym.st_name = (unsigned long) -1;
8044 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8045 to get the final offset for st_name. */
8047 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8049 if (sym.st_name == (unsigned long) -1)
8053 type_ptr = elf_symbol_from (abfd, syms[idx]);
8055 if ((flags & BSF_SECTION_SYM) == 0
8056 && bfd_is_com_section (syms[idx]->section))
8058 /* ELF common symbols put the alignment into the `value' field,
8059 and the size into the `size' field. This is backwards from
8060 how BFD handles it, so reverse it here. */
8061 sym.st_size = value;
8062 if (type_ptr == NULL
8063 || type_ptr->internal_elf_sym.st_value == 0)
8064 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8066 sym.st_value = type_ptr->internal_elf_sym.st_value;
8067 sym.st_shndx = _bfd_elf_section_from_bfd_section
8068 (abfd, syms[idx]->section);
8072 asection *sec = syms[idx]->section;
8075 if (sec->output_section)
8077 value += sec->output_offset;
8078 sec = sec->output_section;
8081 /* Don't add in the section vma for relocatable output. */
8082 if (! relocatable_p)
8084 sym.st_value = value;
8085 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8087 if (bfd_is_abs_section (sec)
8089 && type_ptr->internal_elf_sym.st_shndx != 0)
8091 /* This symbol is in a real ELF section which we did
8092 not create as a BFD section. Undo the mapping done
8093 by copy_private_symbol_data. */
8094 shndx = type_ptr->internal_elf_sym.st_shndx;
8098 shndx = elf_onesymtab (abfd);
8101 shndx = elf_dynsymtab (abfd);
8104 shndx = elf_strtab_sec (abfd);
8107 shndx = elf_shstrtab_sec (abfd);
8110 if (elf_symtab_shndx_list (abfd))
8111 shndx = elf_symtab_shndx_list (abfd)->ndx;
8120 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8122 if (shndx == SHN_BAD)
8126 /* Writing this would be a hell of a lot easier if
8127 we had some decent documentation on bfd, and
8128 knew what to expect of the library, and what to
8129 demand of applications. For example, it
8130 appears that `objcopy' might not set the
8131 section of a symbol to be a section that is
8132 actually in the output file. */
8133 sec2 = bfd_get_section_by_name (abfd, sec->name);
8135 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8136 if (shndx == SHN_BAD)
8138 /* xgettext:c-format */
8140 (_("unable to find equivalent output section"
8141 " for symbol '%s' from section '%s'"),
8142 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8144 bfd_set_error (bfd_error_invalid_operation);
8150 sym.st_shndx = shndx;
8153 if ((flags & BSF_THREAD_LOCAL) != 0)
8155 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8156 type = STT_GNU_IFUNC;
8157 else if ((flags & BSF_FUNCTION) != 0)
8159 else if ((flags & BSF_OBJECT) != 0)
8161 else if ((flags & BSF_RELC) != 0)
8163 else if ((flags & BSF_SRELC) != 0)
8168 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8171 /* Processor-specific types. */
8172 if (type_ptr != NULL
8173 && bed->elf_backend_get_symbol_type)
8174 type = ((*bed->elf_backend_get_symbol_type)
8175 (&type_ptr->internal_elf_sym, type));
8177 if (flags & BSF_SECTION_SYM)
8179 if (flags & BSF_GLOBAL)
8180 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8182 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8184 else if (bfd_is_com_section (syms[idx]->section))
8186 if (type != STT_TLS)
8188 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8189 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8190 ? STT_COMMON : STT_OBJECT);
8192 type = ((flags & BSF_ELF_COMMON) != 0
8193 ? STT_COMMON : STT_OBJECT);
8195 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8197 else if (bfd_is_und_section (syms[idx]->section))
8198 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8202 else if (flags & BSF_FILE)
8203 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8206 int bind = STB_LOCAL;
8208 if (flags & BSF_LOCAL)
8210 else if (flags & BSF_GNU_UNIQUE)
8211 bind = STB_GNU_UNIQUE;
8212 else if (flags & BSF_WEAK)
8214 else if (flags & BSF_GLOBAL)
8217 sym.st_info = ELF_ST_INFO (bind, type);
8220 if (type_ptr != NULL)
8222 sym.st_other = type_ptr->internal_elf_sym.st_other;
8223 sym.st_target_internal
8224 = type_ptr->internal_elf_sym.st_target_internal;
8229 sym.st_target_internal = 0;
8233 symstrtab[idx].sym = sym;
8234 symstrtab[idx].dest_index = outbound_syms_index;
8235 symstrtab[idx].destshndx_index = outbound_shndx_index;
8237 outbound_syms_index++;
8238 if (outbound_shndx != NULL)
8239 outbound_shndx_index++;
8242 /* Finalize the .strtab section. */
8243 _bfd_elf_strtab_finalize (stt);
8245 /* Swap out the .strtab section. */
8246 for (idx = 0; idx <= symcount; idx++)
8248 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8249 if (elfsym->sym.st_name == (unsigned long) -1)
8250 elfsym->sym.st_name = 0;
8252 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8253 elfsym->sym.st_name);
8254 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8256 + (elfsym->dest_index
8257 * bed->s->sizeof_sym)),
8259 + (elfsym->destshndx_index
8260 * sizeof (Elf_External_Sym_Shndx))));
8265 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8266 symstrtab_hdr->sh_type = SHT_STRTAB;
8267 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8268 symstrtab_hdr->sh_addr = 0;
8269 symstrtab_hdr->sh_entsize = 0;
8270 symstrtab_hdr->sh_link = 0;
8271 symstrtab_hdr->sh_info = 0;
8272 symstrtab_hdr->sh_addralign = 1;
8277 /* Return the number of bytes required to hold the symtab vector.
8279 Note that we base it on the count plus 1, since we will null terminate
8280 the vector allocated based on this size. However, the ELF symbol table
8281 always has a dummy entry as symbol #0, so it ends up even. */
8284 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8286 bfd_size_type symcount;
8288 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8290 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8291 if (symcount >= LONG_MAX / sizeof (asymbol *))
8293 bfd_set_error (bfd_error_file_too_big);
8296 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8298 symtab_size -= sizeof (asymbol *);
8304 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8306 bfd_size_type symcount;
8308 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8310 if (elf_dynsymtab (abfd) == 0)
8312 bfd_set_error (bfd_error_invalid_operation);
8316 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8317 if (symcount >= LONG_MAX / sizeof (asymbol *))
8319 bfd_set_error (bfd_error_file_too_big);
8322 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8324 symtab_size -= sizeof (asymbol *);
8330 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8333 #if SIZEOF_LONG == SIZEOF_INT
8334 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8336 bfd_set_error (bfd_error_file_too_big);
8340 return (asect->reloc_count + 1) * sizeof (arelent *);
8343 /* Canonicalize the relocs. */
8346 _bfd_elf_canonicalize_reloc (bfd *abfd,
8353 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8355 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8358 tblptr = section->relocation;
8359 for (i = 0; i < section->reloc_count; i++)
8360 *relptr++ = tblptr++;
8364 return section->reloc_count;
8368 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8370 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8371 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8374 bfd_get_symcount (abfd) = symcount;
8379 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8380 asymbol **allocation)
8382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8383 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8386 bfd_get_dynamic_symcount (abfd) = symcount;
8390 /* Return the size required for the dynamic reloc entries. Any loadable
8391 section that was actually installed in the BFD, and has type SHT_REL
8392 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8393 dynamic reloc section. */
8396 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8398 bfd_size_type count;
8401 if (elf_dynsymtab (abfd) == 0)
8403 bfd_set_error (bfd_error_invalid_operation);
8408 for (s = abfd->sections; s != NULL; s = s->next)
8409 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8410 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8411 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8413 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8414 if (count > LONG_MAX / sizeof (arelent *))
8416 bfd_set_error (bfd_error_file_too_big);
8420 return count * sizeof (arelent *);
8423 /* Canonicalize the dynamic relocation entries. Note that we return the
8424 dynamic relocations as a single block, although they are actually
8425 associated with particular sections; the interface, which was
8426 designed for SunOS style shared libraries, expects that there is only
8427 one set of dynamic relocs. Any loadable section that was actually
8428 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8429 dynamic symbol table, is considered to be a dynamic reloc section. */
8432 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8436 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8440 if (elf_dynsymtab (abfd) == 0)
8442 bfd_set_error (bfd_error_invalid_operation);
8446 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8448 for (s = abfd->sections; s != NULL; s = s->next)
8450 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8451 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8452 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8457 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8459 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8461 for (i = 0; i < count; i++)
8472 /* Read in the version information. */
8475 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8477 bfd_byte *contents = NULL;
8478 unsigned int freeidx = 0;
8480 if (elf_dynverref (abfd) != 0)
8482 Elf_Internal_Shdr *hdr;
8483 Elf_External_Verneed *everneed;
8484 Elf_Internal_Verneed *iverneed;
8486 bfd_byte *contents_end;
8488 hdr = &elf_tdata (abfd)->dynverref_hdr;
8490 if (hdr->sh_info == 0
8491 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8493 error_return_bad_verref:
8495 (_("%pB: .gnu.version_r invalid entry"), abfd);
8496 bfd_set_error (bfd_error_bad_value);
8497 error_return_verref:
8498 elf_tdata (abfd)->verref = NULL;
8499 elf_tdata (abfd)->cverrefs = 0;
8503 ufile_ptr filesize = bfd_get_file_size (abfd);
8504 if (filesize > 0 && filesize < hdr->sh_size)
8506 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8508 bfd_set_error (bfd_error_no_memory);
8510 /* xgettext:c-format */
8511 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8512 abfd, (uint64_t) hdr->sh_size);
8513 goto error_return_verref;
8515 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8516 if (contents == NULL)
8517 goto error_return_verref;
8519 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8520 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8521 goto error_return_verref;
8523 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8524 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8526 if (elf_tdata (abfd)->verref == NULL)
8527 goto error_return_verref;
8529 BFD_ASSERT (sizeof (Elf_External_Verneed)
8530 == sizeof (Elf_External_Vernaux));
8531 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8532 everneed = (Elf_External_Verneed *) contents;
8533 iverneed = elf_tdata (abfd)->verref;
8534 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8536 Elf_External_Vernaux *evernaux;
8537 Elf_Internal_Vernaux *ivernaux;
8540 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8542 iverneed->vn_bfd = abfd;
8544 iverneed->vn_filename =
8545 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8547 if (iverneed->vn_filename == NULL)
8548 goto error_return_bad_verref;
8550 if (iverneed->vn_cnt == 0)
8551 iverneed->vn_auxptr = NULL;
8554 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8555 bfd_alloc2 (abfd, iverneed->vn_cnt,
8556 sizeof (Elf_Internal_Vernaux));
8557 if (iverneed->vn_auxptr == NULL)
8558 goto error_return_verref;
8561 if (iverneed->vn_aux
8562 > (size_t) (contents_end - (bfd_byte *) everneed))
8563 goto error_return_bad_verref;
8565 evernaux = ((Elf_External_Vernaux *)
8566 ((bfd_byte *) everneed + iverneed->vn_aux));
8567 ivernaux = iverneed->vn_auxptr;
8568 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8570 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8572 ivernaux->vna_nodename =
8573 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8574 ivernaux->vna_name);
8575 if (ivernaux->vna_nodename == NULL)
8576 goto error_return_bad_verref;
8578 if (ivernaux->vna_other > freeidx)
8579 freeidx = ivernaux->vna_other;
8581 ivernaux->vna_nextptr = NULL;
8582 if (ivernaux->vna_next == 0)
8584 iverneed->vn_cnt = j + 1;
8587 if (j + 1 < iverneed->vn_cnt)
8588 ivernaux->vna_nextptr = ivernaux + 1;
8590 if (ivernaux->vna_next
8591 > (size_t) (contents_end - (bfd_byte *) evernaux))
8592 goto error_return_bad_verref;
8594 evernaux = ((Elf_External_Vernaux *)
8595 ((bfd_byte *) evernaux + ivernaux->vna_next));
8598 iverneed->vn_nextref = NULL;
8599 if (iverneed->vn_next == 0)
8601 if (i + 1 < hdr->sh_info)
8602 iverneed->vn_nextref = iverneed + 1;
8604 if (iverneed->vn_next
8605 > (size_t) (contents_end - (bfd_byte *) everneed))
8606 goto error_return_bad_verref;
8608 everneed = ((Elf_External_Verneed *)
8609 ((bfd_byte *) everneed + iverneed->vn_next));
8611 elf_tdata (abfd)->cverrefs = i;
8617 if (elf_dynverdef (abfd) != 0)
8619 Elf_Internal_Shdr *hdr;
8620 Elf_External_Verdef *everdef;
8621 Elf_Internal_Verdef *iverdef;
8622 Elf_Internal_Verdef *iverdefarr;
8623 Elf_Internal_Verdef iverdefmem;
8625 unsigned int maxidx;
8626 bfd_byte *contents_end_def, *contents_end_aux;
8628 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8630 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8632 error_return_bad_verdef:
8634 (_("%pB: .gnu.version_d invalid entry"), abfd);
8635 bfd_set_error (bfd_error_bad_value);
8636 error_return_verdef:
8637 elf_tdata (abfd)->verdef = NULL;
8638 elf_tdata (abfd)->cverdefs = 0;
8642 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8643 if (contents == NULL)
8644 goto error_return_verdef;
8645 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8646 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8647 goto error_return_verdef;
8649 BFD_ASSERT (sizeof (Elf_External_Verdef)
8650 >= sizeof (Elf_External_Verdaux));
8651 contents_end_def = contents + hdr->sh_size
8652 - sizeof (Elf_External_Verdef);
8653 contents_end_aux = contents + hdr->sh_size
8654 - sizeof (Elf_External_Verdaux);
8656 /* We know the number of entries in the section but not the maximum
8657 index. Therefore we have to run through all entries and find
8659 everdef = (Elf_External_Verdef *) contents;
8661 for (i = 0; i < hdr->sh_info; ++i)
8663 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8665 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8666 goto error_return_bad_verdef;
8667 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8668 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8670 if (iverdefmem.vd_next == 0)
8673 if (iverdefmem.vd_next
8674 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8675 goto error_return_bad_verdef;
8677 everdef = ((Elf_External_Verdef *)
8678 ((bfd_byte *) everdef + iverdefmem.vd_next));
8681 if (default_imported_symver)
8683 if (freeidx > maxidx)
8689 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8690 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8691 if (elf_tdata (abfd)->verdef == NULL)
8692 goto error_return_verdef;
8694 elf_tdata (abfd)->cverdefs = maxidx;
8696 everdef = (Elf_External_Verdef *) contents;
8697 iverdefarr = elf_tdata (abfd)->verdef;
8698 for (i = 0; i < hdr->sh_info; i++)
8700 Elf_External_Verdaux *everdaux;
8701 Elf_Internal_Verdaux *iverdaux;
8704 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8706 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8707 goto error_return_bad_verdef;
8709 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8710 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8712 iverdef->vd_bfd = abfd;
8714 if (iverdef->vd_cnt == 0)
8715 iverdef->vd_auxptr = NULL;
8718 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8719 bfd_alloc2 (abfd, iverdef->vd_cnt,
8720 sizeof (Elf_Internal_Verdaux));
8721 if (iverdef->vd_auxptr == NULL)
8722 goto error_return_verdef;
8726 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8727 goto error_return_bad_verdef;
8729 everdaux = ((Elf_External_Verdaux *)
8730 ((bfd_byte *) everdef + iverdef->vd_aux));
8731 iverdaux = iverdef->vd_auxptr;
8732 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8734 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8736 iverdaux->vda_nodename =
8737 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8738 iverdaux->vda_name);
8739 if (iverdaux->vda_nodename == NULL)
8740 goto error_return_bad_verdef;
8742 iverdaux->vda_nextptr = NULL;
8743 if (iverdaux->vda_next == 0)
8745 iverdef->vd_cnt = j + 1;
8748 if (j + 1 < iverdef->vd_cnt)
8749 iverdaux->vda_nextptr = iverdaux + 1;
8751 if (iverdaux->vda_next
8752 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8753 goto error_return_bad_verdef;
8755 everdaux = ((Elf_External_Verdaux *)
8756 ((bfd_byte *) everdaux + iverdaux->vda_next));
8759 iverdef->vd_nodename = NULL;
8760 if (iverdef->vd_cnt)
8761 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8763 iverdef->vd_nextdef = NULL;
8764 if (iverdef->vd_next == 0)
8766 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8767 iverdef->vd_nextdef = iverdef + 1;
8769 everdef = ((Elf_External_Verdef *)
8770 ((bfd_byte *) everdef + iverdef->vd_next));
8776 else if (default_imported_symver)
8783 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8784 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8785 if (elf_tdata (abfd)->verdef == NULL)
8788 elf_tdata (abfd)->cverdefs = freeidx;
8791 /* Create a default version based on the soname. */
8792 if (default_imported_symver)
8794 Elf_Internal_Verdef *iverdef;
8795 Elf_Internal_Verdaux *iverdaux;
8797 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8799 iverdef->vd_version = VER_DEF_CURRENT;
8800 iverdef->vd_flags = 0;
8801 iverdef->vd_ndx = freeidx;
8802 iverdef->vd_cnt = 1;
8804 iverdef->vd_bfd = abfd;
8806 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8807 if (iverdef->vd_nodename == NULL)
8808 goto error_return_verdef;
8809 iverdef->vd_nextdef = NULL;
8810 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8811 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8812 if (iverdef->vd_auxptr == NULL)
8813 goto error_return_verdef;
8815 iverdaux = iverdef->vd_auxptr;
8816 iverdaux->vda_nodename = iverdef->vd_nodename;
8822 if (contents != NULL)
8828 _bfd_elf_make_empty_symbol (bfd *abfd)
8830 elf_symbol_type *newsym;
8832 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
8835 newsym->symbol.the_bfd = abfd;
8836 return &newsym->symbol;
8840 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8844 bfd_symbol_info (symbol, ret);
8847 /* Return whether a symbol name implies a local symbol. Most targets
8848 use this function for the is_local_label_name entry point, but some
8852 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8855 /* Normal local symbols start with ``.L''. */
8856 if (name[0] == '.' && name[1] == 'L')
8859 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8860 DWARF debugging symbols starting with ``..''. */
8861 if (name[0] == '.' && name[1] == '.')
8864 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8865 emitting DWARF debugging output. I suspect this is actually a
8866 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8867 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8868 underscore to be emitted on some ELF targets). For ease of use,
8869 we treat such symbols as local. */
8870 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8873 /* Treat assembler generated fake symbols, dollar local labels and
8874 forward-backward labels (aka local labels) as locals.
8875 These labels have the form:
8877 L0^A.* (fake symbols)
8879 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8881 Versions which start with .L will have already been matched above,
8882 so we only need to match the rest. */
8883 if (name[0] == 'L' && ISDIGIT (name[1]))
8885 bfd_boolean ret = FALSE;
8889 for (p = name + 2; (c = *p); p++)
8891 if (c == 1 || c == 2)
8893 if (c == 1 && p == name + 2)
8894 /* A fake symbol. */
8897 /* FIXME: We are being paranoid here and treating symbols like
8898 L0^Bfoo as if there were non-local, on the grounds that the
8899 assembler will never generate them. But can any symbol
8900 containing an ASCII value in the range 1-31 ever be anything
8901 other than some kind of local ? */
8918 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8919 asymbol *symbol ATTRIBUTE_UNUSED)
8926 _bfd_elf_set_arch_mach (bfd *abfd,
8927 enum bfd_architecture arch,
8928 unsigned long machine)
8930 /* If this isn't the right architecture for this backend, and this
8931 isn't the generic backend, fail. */
8932 if (arch != get_elf_backend_data (abfd)->arch
8933 && arch != bfd_arch_unknown
8934 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8937 return bfd_default_set_arch_mach (abfd, arch, machine);
8940 /* Find the nearest line to a particular section and offset,
8941 for error reporting. */
8944 _bfd_elf_find_nearest_line (bfd *abfd,
8948 const char **filename_ptr,
8949 const char **functionname_ptr,
8950 unsigned int *line_ptr,
8951 unsigned int *discriminator_ptr)
8955 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8956 filename_ptr, functionname_ptr,
8957 line_ptr, discriminator_ptr,
8958 dwarf_debug_sections,
8959 &elf_tdata (abfd)->dwarf2_find_line_info)
8960 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8961 filename_ptr, functionname_ptr,
8964 if (!*functionname_ptr)
8965 _bfd_elf_find_function (abfd, symbols, section, offset,
8966 *filename_ptr ? NULL : filename_ptr,
8971 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8972 &found, filename_ptr,
8973 functionname_ptr, line_ptr,
8974 &elf_tdata (abfd)->line_info))
8976 if (found && (*functionname_ptr || *line_ptr))
8979 if (symbols == NULL)
8982 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8983 filename_ptr, functionname_ptr))
8990 /* Find the line for a symbol. */
8993 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8994 const char **filename_ptr, unsigned int *line_ptr)
8996 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8997 filename_ptr, NULL, line_ptr, NULL,
8998 dwarf_debug_sections,
8999 &elf_tdata (abfd)->dwarf2_find_line_info);
9002 /* After a call to bfd_find_nearest_line, successive calls to
9003 bfd_find_inliner_info can be used to get source information about
9004 each level of function inlining that terminated at the address
9005 passed to bfd_find_nearest_line. Currently this is only supported
9006 for DWARF2 with appropriate DWARF3 extensions. */
9009 _bfd_elf_find_inliner_info (bfd *abfd,
9010 const char **filename_ptr,
9011 const char **functionname_ptr,
9012 unsigned int *line_ptr)
9015 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9016 functionname_ptr, line_ptr,
9017 & elf_tdata (abfd)->dwarf2_find_line_info);
9022 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9024 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9025 int ret = bed->s->sizeof_ehdr;
9027 if (!bfd_link_relocatable (info))
9029 bfd_size_type phdr_size = elf_program_header_size (abfd);
9031 if (phdr_size == (bfd_size_type) -1)
9033 struct elf_segment_map *m;
9036 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9037 phdr_size += bed->s->sizeof_phdr;
9040 phdr_size = get_program_header_size (abfd, info);
9043 elf_program_header_size (abfd) = phdr_size;
9051 _bfd_elf_set_section_contents (bfd *abfd,
9053 const void *location,
9055 bfd_size_type count)
9057 Elf_Internal_Shdr *hdr;
9060 if (! abfd->output_has_begun
9061 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9067 hdr = &elf_section_data (section)->this_hdr;
9068 if (hdr->sh_offset == (file_ptr) -1)
9070 /* We must compress this section. Write output to the buffer. */
9071 unsigned char *contents = hdr->contents;
9072 if ((offset + count) > hdr->sh_size
9073 || (section->flags & SEC_ELF_COMPRESS) == 0
9074 || contents == NULL)
9076 memcpy (contents + offset, location, count);
9079 pos = hdr->sh_offset + offset;
9080 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9081 || bfd_bwrite (location, count, abfd) != count)
9088 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9089 arelent *cache_ptr ATTRIBUTE_UNUSED,
9090 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9096 /* Try to convert a non-ELF reloc into an ELF one. */
9099 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9101 /* Check whether we really have an ELF howto. */
9103 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9105 bfd_reloc_code_real_type code;
9106 reloc_howto_type *howto;
9108 /* Alien reloc: Try to determine its type to replace it with an
9109 equivalent ELF reloc. */
9111 if (areloc->howto->pc_relative)
9113 switch (areloc->howto->bitsize)
9116 code = BFD_RELOC_8_PCREL;
9119 code = BFD_RELOC_12_PCREL;
9122 code = BFD_RELOC_16_PCREL;
9125 code = BFD_RELOC_24_PCREL;
9128 code = BFD_RELOC_32_PCREL;
9131 code = BFD_RELOC_64_PCREL;
9137 howto = bfd_reloc_type_lookup (abfd, code);
9139 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9141 if (howto->pcrel_offset)
9142 areloc->addend += areloc->address;
9144 areloc->addend -= areloc->address; /* addend is unsigned!! */
9149 switch (areloc->howto->bitsize)
9155 code = BFD_RELOC_14;
9158 code = BFD_RELOC_16;
9161 code = BFD_RELOC_26;
9164 code = BFD_RELOC_32;
9167 code = BFD_RELOC_64;
9173 howto = bfd_reloc_type_lookup (abfd, code);
9177 areloc->howto = howto;
9185 /* xgettext:c-format */
9186 _bfd_error_handler (_("%pB: %s unsupported"),
9187 abfd, areloc->howto->name);
9188 bfd_set_error (bfd_error_bad_value);
9193 _bfd_elf_close_and_cleanup (bfd *abfd)
9195 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9196 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
9198 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9199 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9200 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9203 return _bfd_generic_close_and_cleanup (abfd);
9206 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9207 in the relocation's offset. Thus we cannot allow any sort of sanity
9208 range-checking to interfere. There is nothing else to do in processing
9211 bfd_reloc_status_type
9212 _bfd_elf_rel_vtable_reloc_fn
9213 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9214 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9215 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9216 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9218 return bfd_reloc_ok;
9221 /* Elf core file support. Much of this only works on native
9222 toolchains, since we rely on knowing the
9223 machine-dependent procfs structure in order to pick
9224 out details about the corefile. */
9226 #ifdef HAVE_SYS_PROCFS_H
9227 /* Needed for new procfs interface on sparc-solaris. */
9228 # define _STRUCTURED_PROC 1
9229 # include <sys/procfs.h>
9232 /* Return a PID that identifies a "thread" for threaded cores, or the
9233 PID of the main process for non-threaded cores. */
9236 elfcore_make_pid (bfd *abfd)
9240 pid = elf_tdata (abfd)->core->lwpid;
9242 pid = elf_tdata (abfd)->core->pid;
9247 /* If there isn't a section called NAME, make one, using
9248 data from SECT. Note, this function will generate a
9249 reference to NAME, so you shouldn't deallocate or
9253 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9257 if (bfd_get_section_by_name (abfd, name) != NULL)
9260 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9264 sect2->size = sect->size;
9265 sect2->filepos = sect->filepos;
9266 sect2->alignment_power = sect->alignment_power;
9270 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9271 actually creates up to two pseudosections:
9272 - For the single-threaded case, a section named NAME, unless
9273 such a section already exists.
9274 - For the multi-threaded case, a section named "NAME/PID", where
9275 PID is elfcore_make_pid (abfd).
9276 Both pseudosections have identical contents. */
9278 _bfd_elfcore_make_pseudosection (bfd *abfd,
9284 char *threaded_name;
9288 /* Build the section name. */
9290 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9291 len = strlen (buf) + 1;
9292 threaded_name = (char *) bfd_alloc (abfd, len);
9293 if (threaded_name == NULL)
9295 memcpy (threaded_name, buf, len);
9297 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9302 sect->filepos = filepos;
9303 sect->alignment_power = 2;
9305 return elfcore_maybe_make_sect (abfd, name, sect);
9309 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9312 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9318 sect->size = note->descsz - offs;
9319 sect->filepos = note->descpos + offs;
9320 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9325 /* prstatus_t exists on:
9327 linux 2.[01] + glibc
9331 #if defined (HAVE_PRSTATUS_T)
9334 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9339 if (note->descsz == sizeof (prstatus_t))
9343 size = sizeof (prstat.pr_reg);
9344 offset = offsetof (prstatus_t, pr_reg);
9345 memcpy (&prstat, note->descdata, sizeof (prstat));
9347 /* Do not overwrite the core signal if it
9348 has already been set by another thread. */
9349 if (elf_tdata (abfd)->core->signal == 0)
9350 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9351 if (elf_tdata (abfd)->core->pid == 0)
9352 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9354 /* pr_who exists on:
9357 pr_who doesn't exist on:
9360 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9361 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9363 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9366 #if defined (HAVE_PRSTATUS32_T)
9367 else if (note->descsz == sizeof (prstatus32_t))
9369 /* 64-bit host, 32-bit corefile */
9370 prstatus32_t prstat;
9372 size = sizeof (prstat.pr_reg);
9373 offset = offsetof (prstatus32_t, pr_reg);
9374 memcpy (&prstat, note->descdata, sizeof (prstat));
9376 /* Do not overwrite the core signal if it
9377 has already been set by another thread. */
9378 if (elf_tdata (abfd)->core->signal == 0)
9379 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9380 if (elf_tdata (abfd)->core->pid == 0)
9381 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9383 /* pr_who exists on:
9386 pr_who doesn't exist on:
9389 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9390 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9392 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9395 #endif /* HAVE_PRSTATUS32_T */
9398 /* Fail - we don't know how to handle any other
9399 note size (ie. data object type). */
9403 /* Make a ".reg/999" section and a ".reg" section. */
9404 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9405 size, note->descpos + offset);
9407 #endif /* defined (HAVE_PRSTATUS_T) */
9409 /* Create a pseudosection containing the exact contents of NOTE. */
9411 elfcore_make_note_pseudosection (bfd *abfd,
9413 Elf_Internal_Note *note)
9415 return _bfd_elfcore_make_pseudosection (abfd, name,
9416 note->descsz, note->descpos);
9419 /* There isn't a consistent prfpregset_t across platforms,
9420 but it doesn't matter, because we don't have to pick this
9421 data structure apart. */
9424 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9426 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9429 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9430 type of NT_PRXFPREG. Just include the whole note's contents
9434 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9436 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9439 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9440 with a note type of NT_X86_XSTATE. Just include the whole note's
9441 contents literally. */
9444 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9446 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9450 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9452 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9456 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9458 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9462 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9464 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9468 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9470 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9474 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9476 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9480 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9482 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9486 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9488 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9492 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9494 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9498 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9500 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9504 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9506 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9510 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9512 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9516 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9518 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9522 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9524 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9528 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9530 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9534 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9536 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9540 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9542 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9546 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9548 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9552 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9554 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9558 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9560 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9564 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9566 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9570 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9572 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9576 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9578 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9582 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9584 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9588 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9590 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9594 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9596 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9600 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9602 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9606 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9608 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9612 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9614 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9618 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9620 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9624 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9626 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9630 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9632 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9636 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9638 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9642 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9644 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9648 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9650 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9653 #if defined (HAVE_PRPSINFO_T)
9654 typedef prpsinfo_t elfcore_psinfo_t;
9655 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9656 typedef prpsinfo32_t elfcore_psinfo32_t;
9660 #if defined (HAVE_PSINFO_T)
9661 typedef psinfo_t elfcore_psinfo_t;
9662 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9663 typedef psinfo32_t elfcore_psinfo32_t;
9667 /* return a malloc'ed copy of a string at START which is at
9668 most MAX bytes long, possibly without a terminating '\0'.
9669 the copy will always have a terminating '\0'. */
9672 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9675 char *end = (char *) memchr (start, '\0', max);
9683 dups = (char *) bfd_alloc (abfd, len + 1);
9687 memcpy (dups, start, len);
9693 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9695 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9697 if (note->descsz == sizeof (elfcore_psinfo_t))
9699 elfcore_psinfo_t psinfo;
9701 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9703 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9704 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9706 elf_tdata (abfd)->core->program
9707 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9708 sizeof (psinfo.pr_fname));
9710 elf_tdata (abfd)->core->command
9711 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9712 sizeof (psinfo.pr_psargs));
9714 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9715 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9717 /* 64-bit host, 32-bit corefile */
9718 elfcore_psinfo32_t psinfo;
9720 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9722 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9723 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9725 elf_tdata (abfd)->core->program
9726 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9727 sizeof (psinfo.pr_fname));
9729 elf_tdata (abfd)->core->command
9730 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9731 sizeof (psinfo.pr_psargs));
9737 /* Fail - we don't know how to handle any other
9738 note size (ie. data object type). */
9742 /* Note that for some reason, a spurious space is tacked
9743 onto the end of the args in some (at least one anyway)
9744 implementations, so strip it off if it exists. */
9747 char *command = elf_tdata (abfd)->core->command;
9748 int n = strlen (command);
9750 if (0 < n && command[n - 1] == ' ')
9751 command[n - 1] = '\0';
9756 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9758 #if defined (HAVE_PSTATUS_T)
9760 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9762 if (note->descsz == sizeof (pstatus_t)
9763 #if defined (HAVE_PXSTATUS_T)
9764 || note->descsz == sizeof (pxstatus_t)
9770 memcpy (&pstat, note->descdata, sizeof (pstat));
9772 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9774 #if defined (HAVE_PSTATUS32_T)
9775 else if (note->descsz == sizeof (pstatus32_t))
9777 /* 64-bit host, 32-bit corefile */
9780 memcpy (&pstat, note->descdata, sizeof (pstat));
9782 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9785 /* Could grab some more details from the "representative"
9786 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9787 NT_LWPSTATUS note, presumably. */
9791 #endif /* defined (HAVE_PSTATUS_T) */
9793 #if defined (HAVE_LWPSTATUS_T)
9795 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9797 lwpstatus_t lwpstat;
9803 if (note->descsz != sizeof (lwpstat)
9804 #if defined (HAVE_LWPXSTATUS_T)
9805 && note->descsz != sizeof (lwpxstatus_t)
9810 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9812 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9813 /* Do not overwrite the core signal if it has already been set by
9815 if (elf_tdata (abfd)->core->signal == 0)
9816 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9818 /* Make a ".reg/999" section. */
9820 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
9821 len = strlen (buf) + 1;
9822 name = bfd_alloc (abfd, len);
9825 memcpy (name, buf, len);
9827 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9831 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9832 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
9833 sect->filepos = note->descpos
9834 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9837 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9838 sect->size = sizeof (lwpstat.pr_reg);
9839 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9842 sect->alignment_power = 2;
9844 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9847 /* Make a ".reg2/999" section */
9849 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9850 len = strlen (buf) + 1;
9851 name = bfd_alloc (abfd, len);
9854 memcpy (name, buf, len);
9856 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9860 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9861 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9862 sect->filepos = note->descpos
9863 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9866 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9867 sect->size = sizeof (lwpstat.pr_fpreg);
9868 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9871 sect->alignment_power = 2;
9873 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9875 #endif /* defined (HAVE_LWPSTATUS_T) */
9878 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9885 int is_active_thread;
9888 if (note->descsz < 728)
9891 if (! CONST_STRNEQ (note->namedata, "win32"))
9894 type = bfd_get_32 (abfd, note->descdata);
9898 case 1 /* NOTE_INFO_PROCESS */:
9899 /* FIXME: need to add ->core->command. */
9900 /* process_info.pid */
9901 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9902 /* process_info.signal */
9903 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9906 case 2 /* NOTE_INFO_THREAD */:
9907 /* Make a ".reg/999" section. */
9908 /* thread_info.tid */
9909 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9911 len = strlen (buf) + 1;
9912 name = (char *) bfd_alloc (abfd, len);
9916 memcpy (name, buf, len);
9918 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9922 /* sizeof (thread_info.thread_context) */
9924 /* offsetof (thread_info.thread_context) */
9925 sect->filepos = note->descpos + 12;
9926 sect->alignment_power = 2;
9928 /* thread_info.is_active_thread */
9929 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9931 if (is_active_thread)
9932 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9936 case 3 /* NOTE_INFO_MODULE */:
9937 /* Make a ".module/xxxxxxxx" section. */
9938 /* module_info.base_address */
9939 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9940 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9942 len = strlen (buf) + 1;
9943 name = (char *) bfd_alloc (abfd, len);
9947 memcpy (name, buf, len);
9949 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9954 sect->size = note->descsz;
9955 sect->filepos = note->descpos;
9956 sect->alignment_power = 2;
9967 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9969 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9977 if (bed->elf_backend_grok_prstatus)
9978 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
9980 #if defined (HAVE_PRSTATUS_T)
9981 return elfcore_grok_prstatus (abfd, note);
9986 #if defined (HAVE_PSTATUS_T)
9988 return elfcore_grok_pstatus (abfd, note);
9991 #if defined (HAVE_LWPSTATUS_T)
9993 return elfcore_grok_lwpstatus (abfd, note);
9996 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9997 return elfcore_grok_prfpreg (abfd, note);
9999 case NT_WIN32PSTATUS:
10000 return elfcore_grok_win32pstatus (abfd, note);
10002 case NT_PRXFPREG: /* Linux SSE extension */
10003 if (note->namesz == 6
10004 && strcmp (note->namedata, "LINUX") == 0)
10005 return elfcore_grok_prxfpreg (abfd, note);
10009 case NT_X86_XSTATE: /* Linux XSAVE extension */
10010 if (note->namesz == 6
10011 && strcmp (note->namedata, "LINUX") == 0)
10012 return elfcore_grok_xstatereg (abfd, note);
10017 if (note->namesz == 6
10018 && strcmp (note->namedata, "LINUX") == 0)
10019 return elfcore_grok_ppc_vmx (abfd, note);
10024 if (note->namesz == 6
10025 && strcmp (note->namedata, "LINUX") == 0)
10026 return elfcore_grok_ppc_vsx (abfd, note);
10031 if (note->namesz == 6
10032 && strcmp (note->namedata, "LINUX") == 0)
10033 return elfcore_grok_ppc_tar (abfd, note);
10038 if (note->namesz == 6
10039 && strcmp (note->namedata, "LINUX") == 0)
10040 return elfcore_grok_ppc_ppr (abfd, note);
10045 if (note->namesz == 6
10046 && strcmp (note->namedata, "LINUX") == 0)
10047 return elfcore_grok_ppc_dscr (abfd, note);
10052 if (note->namesz == 6
10053 && strcmp (note->namedata, "LINUX") == 0)
10054 return elfcore_grok_ppc_ebb (abfd, note);
10059 if (note->namesz == 6
10060 && strcmp (note->namedata, "LINUX") == 0)
10061 return elfcore_grok_ppc_pmu (abfd, note);
10065 case NT_PPC_TM_CGPR:
10066 if (note->namesz == 6
10067 && strcmp (note->namedata, "LINUX") == 0)
10068 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10072 case NT_PPC_TM_CFPR:
10073 if (note->namesz == 6
10074 && strcmp (note->namedata, "LINUX") == 0)
10075 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10079 case NT_PPC_TM_CVMX:
10080 if (note->namesz == 6
10081 && strcmp (note->namedata, "LINUX") == 0)
10082 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10086 case NT_PPC_TM_CVSX:
10087 if (note->namesz == 6
10088 && strcmp (note->namedata, "LINUX") == 0)
10089 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10093 case NT_PPC_TM_SPR:
10094 if (note->namesz == 6
10095 && strcmp (note->namedata, "LINUX") == 0)
10096 return elfcore_grok_ppc_tm_spr (abfd, note);
10100 case NT_PPC_TM_CTAR:
10101 if (note->namesz == 6
10102 && strcmp (note->namedata, "LINUX") == 0)
10103 return elfcore_grok_ppc_tm_ctar (abfd, note);
10107 case NT_PPC_TM_CPPR:
10108 if (note->namesz == 6
10109 && strcmp (note->namedata, "LINUX") == 0)
10110 return elfcore_grok_ppc_tm_cppr (abfd, note);
10114 case NT_PPC_TM_CDSCR:
10115 if (note->namesz == 6
10116 && strcmp (note->namedata, "LINUX") == 0)
10117 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10121 case NT_S390_HIGH_GPRS:
10122 if (note->namesz == 6
10123 && strcmp (note->namedata, "LINUX") == 0)
10124 return elfcore_grok_s390_high_gprs (abfd, note);
10128 case NT_S390_TIMER:
10129 if (note->namesz == 6
10130 && strcmp (note->namedata, "LINUX") == 0)
10131 return elfcore_grok_s390_timer (abfd, note);
10135 case NT_S390_TODCMP:
10136 if (note->namesz == 6
10137 && strcmp (note->namedata, "LINUX") == 0)
10138 return elfcore_grok_s390_todcmp (abfd, note);
10142 case NT_S390_TODPREG:
10143 if (note->namesz == 6
10144 && strcmp (note->namedata, "LINUX") == 0)
10145 return elfcore_grok_s390_todpreg (abfd, note);
10150 if (note->namesz == 6
10151 && strcmp (note->namedata, "LINUX") == 0)
10152 return elfcore_grok_s390_ctrs (abfd, note);
10156 case NT_S390_PREFIX:
10157 if (note->namesz == 6
10158 && strcmp (note->namedata, "LINUX") == 0)
10159 return elfcore_grok_s390_prefix (abfd, note);
10163 case NT_S390_LAST_BREAK:
10164 if (note->namesz == 6
10165 && strcmp (note->namedata, "LINUX") == 0)
10166 return elfcore_grok_s390_last_break (abfd, note);
10170 case NT_S390_SYSTEM_CALL:
10171 if (note->namesz == 6
10172 && strcmp (note->namedata, "LINUX") == 0)
10173 return elfcore_grok_s390_system_call (abfd, note);
10178 if (note->namesz == 6
10179 && strcmp (note->namedata, "LINUX") == 0)
10180 return elfcore_grok_s390_tdb (abfd, note);
10184 case NT_S390_VXRS_LOW:
10185 if (note->namesz == 6
10186 && strcmp (note->namedata, "LINUX") == 0)
10187 return elfcore_grok_s390_vxrs_low (abfd, note);
10191 case NT_S390_VXRS_HIGH:
10192 if (note->namesz == 6
10193 && strcmp (note->namedata, "LINUX") == 0)
10194 return elfcore_grok_s390_vxrs_high (abfd, note);
10198 case NT_S390_GS_CB:
10199 if (note->namesz == 6
10200 && strcmp (note->namedata, "LINUX") == 0)
10201 return elfcore_grok_s390_gs_cb (abfd, note);
10205 case NT_S390_GS_BC:
10206 if (note->namesz == 6
10207 && strcmp (note->namedata, "LINUX") == 0)
10208 return elfcore_grok_s390_gs_bc (abfd, note);
10213 if (note->namesz == 6
10214 && strcmp (note->namedata, "LINUX") == 0)
10215 return elfcore_grok_arm_vfp (abfd, note);
10220 if (note->namesz == 6
10221 && strcmp (note->namedata, "LINUX") == 0)
10222 return elfcore_grok_aarch_tls (abfd, note);
10226 case NT_ARM_HW_BREAK:
10227 if (note->namesz == 6
10228 && strcmp (note->namedata, "LINUX") == 0)
10229 return elfcore_grok_aarch_hw_break (abfd, note);
10233 case NT_ARM_HW_WATCH:
10234 if (note->namesz == 6
10235 && strcmp (note->namedata, "LINUX") == 0)
10236 return elfcore_grok_aarch_hw_watch (abfd, note);
10241 if (note->namesz == 6
10242 && strcmp (note->namedata, "LINUX") == 0)
10243 return elfcore_grok_aarch_sve (abfd, note);
10247 case NT_ARM_PAC_MASK:
10248 if (note->namesz == 6
10249 && strcmp (note->namedata, "LINUX") == 0)
10250 return elfcore_grok_aarch_pauth (abfd, note);
10256 if (bed->elf_backend_grok_psinfo)
10257 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10259 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10260 return elfcore_grok_psinfo (abfd, note);
10266 return elfcore_make_auxv_note_section (abfd, note, 0);
10269 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10273 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10280 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10282 struct bfd_build_id* build_id;
10284 if (note->descsz == 0)
10287 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10288 if (build_id == NULL)
10291 build_id->size = note->descsz;
10292 memcpy (build_id->data, note->descdata, note->descsz);
10293 abfd->build_id = build_id;
10299 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10301 switch (note->type)
10306 case NT_GNU_PROPERTY_TYPE_0:
10307 return _bfd_elf_parse_gnu_properties (abfd, note);
10309 case NT_GNU_BUILD_ID:
10310 return elfobj_grok_gnu_build_id (abfd, note);
10315 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10317 struct sdt_note *cur =
10318 (struct sdt_note *) bfd_alloc (abfd,
10319 sizeof (struct sdt_note) + note->descsz);
10321 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10322 cur->size = (bfd_size_type) note->descsz;
10323 memcpy (cur->data, note->descdata, note->descsz);
10325 elf_tdata (abfd)->sdt_note_head = cur;
10331 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10333 switch (note->type)
10336 return elfobj_grok_stapsdt_note_1 (abfd, note);
10344 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10348 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10351 if (note->descsz < 108)
10356 if (note->descsz < 120)
10364 /* Check for version 1 in pr_version. */
10365 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10370 /* Skip over pr_psinfosz. */
10371 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10375 offset += 4; /* Padding before pr_psinfosz. */
10379 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10380 elf_tdata (abfd)->core->program
10381 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10384 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10385 elf_tdata (abfd)->core->command
10386 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10389 /* Padding before pr_pid. */
10392 /* The pr_pid field was added in version "1a". */
10393 if (note->descsz < offset + 4)
10396 elf_tdata (abfd)->core->pid
10397 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10403 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10409 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10410 Also compute minimum size of this note. */
10411 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10415 min_size = offset + (4 * 2) + 4 + 4 + 4;
10419 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10420 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10427 if (note->descsz < min_size)
10430 /* Check for version 1 in pr_version. */
10431 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10434 /* Extract size of pr_reg from pr_gregsetsz. */
10435 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10436 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10438 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10443 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10447 /* Skip over pr_osreldate. */
10450 /* Read signal from pr_cursig. */
10451 if (elf_tdata (abfd)->core->signal == 0)
10452 elf_tdata (abfd)->core->signal
10453 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10456 /* Read TID from pr_pid. */
10457 elf_tdata (abfd)->core->lwpid
10458 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10461 /* Padding before pr_reg. */
10462 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10465 /* Make sure that there is enough data remaining in the note. */
10466 if ((note->descsz - offset) < size)
10469 /* Make a ".reg/999" section and a ".reg" section. */
10470 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10471 size, note->descpos + offset);
10475 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10477 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10479 switch (note->type)
10482 if (bed->elf_backend_grok_freebsd_prstatus)
10483 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10485 return elfcore_grok_freebsd_prstatus (abfd, note);
10488 return elfcore_grok_prfpreg (abfd, note);
10491 return elfcore_grok_freebsd_psinfo (abfd, note);
10493 case NT_FREEBSD_THRMISC:
10494 if (note->namesz == 8)
10495 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10499 case NT_FREEBSD_PROCSTAT_PROC:
10500 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10503 case NT_FREEBSD_PROCSTAT_FILES:
10504 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10507 case NT_FREEBSD_PROCSTAT_VMMAP:
10508 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10511 case NT_FREEBSD_PROCSTAT_AUXV:
10512 return elfcore_make_auxv_note_section (abfd, note, 4);
10514 case NT_X86_XSTATE:
10515 if (note->namesz == 8)
10516 return elfcore_grok_xstatereg (abfd, note);
10520 case NT_FREEBSD_PTLWPINFO:
10521 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10525 return elfcore_grok_arm_vfp (abfd, note);
10533 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10537 cp = strchr (note->namedata, '@');
10540 *lwpidp = atoi(cp + 1);
10547 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10549 if (note->descsz <= 0x7c + 31)
10552 /* Signal number at offset 0x08. */
10553 elf_tdata (abfd)->core->signal
10554 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10556 /* Process ID at offset 0x50. */
10557 elf_tdata (abfd)->core->pid
10558 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10560 /* Command name at 0x7c (max 32 bytes, including nul). */
10561 elf_tdata (abfd)->core->command
10562 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10564 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10569 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10573 if (elfcore_netbsd_get_lwpid (note, &lwp))
10574 elf_tdata (abfd)->core->lwpid = lwp;
10576 switch (note->type)
10578 case NT_NETBSDCORE_PROCINFO:
10579 /* NetBSD-specific core "procinfo". Note that we expect to
10580 find this note before any of the others, which is fine,
10581 since the kernel writes this note out first when it
10582 creates a core file. */
10583 return elfcore_grok_netbsd_procinfo (abfd, note);
10584 #ifdef NT_NETBSDCORE_AUXV
10585 case NT_NETBSDCORE_AUXV:
10586 /* NetBSD-specific Elf Auxiliary Vector data. */
10587 return elfcore_make_auxv_note_section (abfd, note, 4);
10593 /* As of March 2017 there are no other machine-independent notes
10594 defined for NetBSD core files. If the note type is less
10595 than the start of the machine-dependent note types, we don't
10598 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10602 switch (bfd_get_arch (abfd))
10604 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10605 PT_GETFPREGS == mach+2. */
10607 case bfd_arch_alpha:
10608 case bfd_arch_sparc:
10609 switch (note->type)
10611 case NT_NETBSDCORE_FIRSTMACH+0:
10612 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10614 case NT_NETBSDCORE_FIRSTMACH+2:
10615 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10621 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10622 There's also old PT___GETREGS40 == mach + 1 for old reg
10623 structure which lacks GBR. */
10626 switch (note->type)
10628 case NT_NETBSDCORE_FIRSTMACH+3:
10629 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10631 case NT_NETBSDCORE_FIRSTMACH+5:
10632 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10638 /* On all other arch's, PT_GETREGS == mach+1 and
10639 PT_GETFPREGS == mach+3. */
10642 switch (note->type)
10644 case NT_NETBSDCORE_FIRSTMACH+1:
10645 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10647 case NT_NETBSDCORE_FIRSTMACH+3:
10648 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10658 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10660 if (note->descsz <= 0x48 + 31)
10663 /* Signal number at offset 0x08. */
10664 elf_tdata (abfd)->core->signal
10665 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10667 /* Process ID at offset 0x20. */
10668 elf_tdata (abfd)->core->pid
10669 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10671 /* Command name at 0x48 (max 32 bytes, including nul). */
10672 elf_tdata (abfd)->core->command
10673 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10679 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10681 if (note->type == NT_OPENBSD_PROCINFO)
10682 return elfcore_grok_openbsd_procinfo (abfd, note);
10684 if (note->type == NT_OPENBSD_REGS)
10685 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10687 if (note->type == NT_OPENBSD_FPREGS)
10688 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10690 if (note->type == NT_OPENBSD_XFPREGS)
10691 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10693 if (note->type == NT_OPENBSD_AUXV)
10694 return elfcore_make_auxv_note_section (abfd, note, 0);
10696 if (note->type == NT_OPENBSD_WCOOKIE)
10698 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10703 sect->size = note->descsz;
10704 sect->filepos = note->descpos;
10705 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10714 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10716 void *ddata = note->descdata;
10723 if (note->descsz < 16)
10726 /* nto_procfs_status 'pid' field is at offset 0. */
10727 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10729 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10730 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10732 /* nto_procfs_status 'flags' field is at offset 8. */
10733 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10735 /* nto_procfs_status 'what' field is at offset 14. */
10736 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10738 elf_tdata (abfd)->core->signal = sig;
10739 elf_tdata (abfd)->core->lwpid = *tid;
10742 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10743 do not come from signals so we make sure we set the current
10744 thread just in case. */
10745 if (flags & 0x00000080)
10746 elf_tdata (abfd)->core->lwpid = *tid;
10748 /* Make a ".qnx_core_status/%d" section. */
10749 sprintf (buf, ".qnx_core_status/%ld", *tid);
10751 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10754 strcpy (name, buf);
10756 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10760 sect->size = note->descsz;
10761 sect->filepos = note->descpos;
10762 sect->alignment_power = 2;
10764 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10768 elfcore_grok_nto_regs (bfd *abfd,
10769 Elf_Internal_Note *note,
10777 /* Make a "(base)/%d" section. */
10778 sprintf (buf, "%s/%ld", base, tid);
10780 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10783 strcpy (name, buf);
10785 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10789 sect->size = note->descsz;
10790 sect->filepos = note->descpos;
10791 sect->alignment_power = 2;
10793 /* This is the current thread. */
10794 if (elf_tdata (abfd)->core->lwpid == tid)
10795 return elfcore_maybe_make_sect (abfd, base, sect);
10800 #define BFD_QNT_CORE_INFO 7
10801 #define BFD_QNT_CORE_STATUS 8
10802 #define BFD_QNT_CORE_GREG 9
10803 #define BFD_QNT_CORE_FPREG 10
10806 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10808 /* Every GREG section has a STATUS section before it. Store the
10809 tid from the previous call to pass down to the next gregs
10811 static long tid = 1;
10813 switch (note->type)
10815 case BFD_QNT_CORE_INFO:
10816 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10817 case BFD_QNT_CORE_STATUS:
10818 return elfcore_grok_nto_status (abfd, note, &tid);
10819 case BFD_QNT_CORE_GREG:
10820 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10821 case BFD_QNT_CORE_FPREG:
10822 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10829 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10835 /* Use note name as section name. */
10836 len = note->namesz;
10837 name = (char *) bfd_alloc (abfd, len);
10840 memcpy (name, note->namedata, len);
10841 name[len - 1] = '\0';
10843 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10847 sect->size = note->descsz;
10848 sect->filepos = note->descpos;
10849 sect->alignment_power = 1;
10854 /* Function: elfcore_write_note
10857 buffer to hold note, and current size of buffer
10861 size of data for note
10863 Writes note to end of buffer. ELF64 notes are written exactly as
10864 for ELF32, despite the current (as of 2006) ELF gabi specifying
10865 that they ought to have 8-byte namesz and descsz field, and have
10866 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10869 Pointer to realloc'd buffer, *BUFSIZ updated. */
10872 elfcore_write_note (bfd *abfd,
10880 Elf_External_Note *xnp;
10887 namesz = strlen (name) + 1;
10889 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
10891 buf = (char *) realloc (buf, *bufsiz + newspace);
10894 dest = buf + *bufsiz;
10895 *bufsiz += newspace;
10896 xnp = (Elf_External_Note *) dest;
10897 H_PUT_32 (abfd, namesz, xnp->namesz);
10898 H_PUT_32 (abfd, size, xnp->descsz);
10899 H_PUT_32 (abfd, type, xnp->type);
10903 memcpy (dest, name, namesz);
10911 memcpy (dest, input, size);
10921 /* gcc-8 warns (*) on all the strncpy calls in this function about
10922 possible string truncation. The "truncation" is not a bug. We
10923 have an external representation of structs with fields that are not
10924 necessarily NULL terminated and corresponding internal
10925 representation fields that are one larger so that they can always
10926 be NULL terminated.
10927 gcc versions between 4.2 and 4.6 do not allow pragma control of
10928 diagnostics inside functions, giving a hard error if you try to use
10929 the finer control available with later versions.
10930 gcc prior to 4.2 warns about diagnostic push and pop.
10931 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10932 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10933 (*) Depending on your system header files! */
10934 #if GCC_VERSION >= 8000
10935 # pragma GCC diagnostic push
10936 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10939 elfcore_write_prpsinfo (bfd *abfd,
10943 const char *psargs)
10945 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10947 if (bed->elf_backend_write_core_note != NULL)
10950 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10951 NT_PRPSINFO, fname, psargs);
10956 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10957 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10958 if (bed->s->elfclass == ELFCLASS32)
10960 # if defined (HAVE_PSINFO32_T)
10962 int note_type = NT_PSINFO;
10965 int note_type = NT_PRPSINFO;
10968 memset (&data, 0, sizeof (data));
10969 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10970 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10971 return elfcore_write_note (abfd, buf, bufsiz,
10972 "CORE", note_type, &data, sizeof (data));
10977 # if defined (HAVE_PSINFO_T)
10979 int note_type = NT_PSINFO;
10982 int note_type = NT_PRPSINFO;
10985 memset (&data, 0, sizeof (data));
10986 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10987 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10988 return elfcore_write_note (abfd, buf, bufsiz,
10989 "CORE", note_type, &data, sizeof (data));
10991 #endif /* PSINFO_T or PRPSINFO_T */
10996 #if GCC_VERSION >= 8000
10997 # pragma GCC diagnostic pop
11001 elfcore_write_linux_prpsinfo32
11002 (bfd *abfd, char *buf, int *bufsiz,
11003 const struct elf_internal_linux_prpsinfo *prpsinfo)
11005 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11007 struct elf_external_linux_prpsinfo32_ugid16 data;
11009 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11010 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11011 &data, sizeof (data));
11015 struct elf_external_linux_prpsinfo32_ugid32 data;
11017 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11018 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11019 &data, sizeof (data));
11024 elfcore_write_linux_prpsinfo64
11025 (bfd *abfd, char *buf, int *bufsiz,
11026 const struct elf_internal_linux_prpsinfo *prpsinfo)
11028 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11030 struct elf_external_linux_prpsinfo64_ugid16 data;
11032 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11033 return elfcore_write_note (abfd, buf, bufsiz,
11034 "CORE", NT_PRPSINFO, &data, sizeof (data));
11038 struct elf_external_linux_prpsinfo64_ugid32 data;
11040 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11041 return elfcore_write_note (abfd, buf, bufsiz,
11042 "CORE", NT_PRPSINFO, &data, sizeof (data));
11047 elfcore_write_prstatus (bfd *abfd,
11054 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11056 if (bed->elf_backend_write_core_note != NULL)
11059 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11061 pid, cursig, gregs);
11066 #if defined (HAVE_PRSTATUS_T)
11067 #if defined (HAVE_PRSTATUS32_T)
11068 if (bed->s->elfclass == ELFCLASS32)
11070 prstatus32_t prstat;
11072 memset (&prstat, 0, sizeof (prstat));
11073 prstat.pr_pid = pid;
11074 prstat.pr_cursig = cursig;
11075 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11076 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11077 NT_PRSTATUS, &prstat, sizeof (prstat));
11084 memset (&prstat, 0, sizeof (prstat));
11085 prstat.pr_pid = pid;
11086 prstat.pr_cursig = cursig;
11087 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11088 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11089 NT_PRSTATUS, &prstat, sizeof (prstat));
11091 #endif /* HAVE_PRSTATUS_T */
11097 #if defined (HAVE_LWPSTATUS_T)
11099 elfcore_write_lwpstatus (bfd *abfd,
11106 lwpstatus_t lwpstat;
11107 const char *note_name = "CORE";
11109 memset (&lwpstat, 0, sizeof (lwpstat));
11110 lwpstat.pr_lwpid = pid >> 16;
11111 lwpstat.pr_cursig = cursig;
11112 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11113 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11114 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11115 #if !defined(gregs)
11116 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11117 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11119 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11120 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11123 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11124 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11126 #endif /* HAVE_LWPSTATUS_T */
11128 #if defined (HAVE_PSTATUS_T)
11130 elfcore_write_pstatus (bfd *abfd,
11134 int cursig ATTRIBUTE_UNUSED,
11135 const void *gregs ATTRIBUTE_UNUSED)
11137 const char *note_name = "CORE";
11138 #if defined (HAVE_PSTATUS32_T)
11139 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11141 if (bed->s->elfclass == ELFCLASS32)
11145 memset (&pstat, 0, sizeof (pstat));
11146 pstat.pr_pid = pid & 0xffff;
11147 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11148 NT_PSTATUS, &pstat, sizeof (pstat));
11156 memset (&pstat, 0, sizeof (pstat));
11157 pstat.pr_pid = pid & 0xffff;
11158 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11159 NT_PSTATUS, &pstat, sizeof (pstat));
11163 #endif /* HAVE_PSTATUS_T */
11166 elfcore_write_prfpreg (bfd *abfd,
11169 const void *fpregs,
11172 const char *note_name = "CORE";
11173 return elfcore_write_note (abfd, buf, bufsiz,
11174 note_name, NT_FPREGSET, fpregs, size);
11178 elfcore_write_prxfpreg (bfd *abfd,
11181 const void *xfpregs,
11184 char *note_name = "LINUX";
11185 return elfcore_write_note (abfd, buf, bufsiz,
11186 note_name, NT_PRXFPREG, xfpregs, size);
11190 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11191 const void *xfpregs, int size)
11194 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11195 note_name = "FreeBSD";
11197 note_name = "LINUX";
11198 return elfcore_write_note (abfd, buf, bufsiz,
11199 note_name, NT_X86_XSTATE, xfpregs, size);
11203 elfcore_write_ppc_vmx (bfd *abfd,
11206 const void *ppc_vmx,
11209 char *note_name = "LINUX";
11210 return elfcore_write_note (abfd, buf, bufsiz,
11211 note_name, NT_PPC_VMX, ppc_vmx, size);
11215 elfcore_write_ppc_vsx (bfd *abfd,
11218 const void *ppc_vsx,
11221 char *note_name = "LINUX";
11222 return elfcore_write_note (abfd, buf, bufsiz,
11223 note_name, NT_PPC_VSX, ppc_vsx, size);
11227 elfcore_write_ppc_tar (bfd *abfd,
11230 const void *ppc_tar,
11233 char *note_name = "LINUX";
11234 return elfcore_write_note (abfd, buf, bufsiz,
11235 note_name, NT_PPC_TAR, ppc_tar, size);
11239 elfcore_write_ppc_ppr (bfd *abfd,
11242 const void *ppc_ppr,
11245 char *note_name = "LINUX";
11246 return elfcore_write_note (abfd, buf, bufsiz,
11247 note_name, NT_PPC_PPR, ppc_ppr, size);
11251 elfcore_write_ppc_dscr (bfd *abfd,
11254 const void *ppc_dscr,
11257 char *note_name = "LINUX";
11258 return elfcore_write_note (abfd, buf, bufsiz,
11259 note_name, NT_PPC_DSCR, ppc_dscr, size);
11263 elfcore_write_ppc_ebb (bfd *abfd,
11266 const void *ppc_ebb,
11269 char *note_name = "LINUX";
11270 return elfcore_write_note (abfd, buf, bufsiz,
11271 note_name, NT_PPC_EBB, ppc_ebb, size);
11275 elfcore_write_ppc_pmu (bfd *abfd,
11278 const void *ppc_pmu,
11281 char *note_name = "LINUX";
11282 return elfcore_write_note (abfd, buf, bufsiz,
11283 note_name, NT_PPC_PMU, ppc_pmu, size);
11287 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11290 const void *ppc_tm_cgpr,
11293 char *note_name = "LINUX";
11294 return elfcore_write_note (abfd, buf, bufsiz,
11295 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11299 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11302 const void *ppc_tm_cfpr,
11305 char *note_name = "LINUX";
11306 return elfcore_write_note (abfd, buf, bufsiz,
11307 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11311 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11314 const void *ppc_tm_cvmx,
11317 char *note_name = "LINUX";
11318 return elfcore_write_note (abfd, buf, bufsiz,
11319 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11323 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11326 const void *ppc_tm_cvsx,
11329 char *note_name = "LINUX";
11330 return elfcore_write_note (abfd, buf, bufsiz,
11331 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11335 elfcore_write_ppc_tm_spr (bfd *abfd,
11338 const void *ppc_tm_spr,
11341 char *note_name = "LINUX";
11342 return elfcore_write_note (abfd, buf, bufsiz,
11343 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11347 elfcore_write_ppc_tm_ctar (bfd *abfd,
11350 const void *ppc_tm_ctar,
11353 char *note_name = "LINUX";
11354 return elfcore_write_note (abfd, buf, bufsiz,
11355 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11359 elfcore_write_ppc_tm_cppr (bfd *abfd,
11362 const void *ppc_tm_cppr,
11365 char *note_name = "LINUX";
11366 return elfcore_write_note (abfd, buf, bufsiz,
11367 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11371 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11374 const void *ppc_tm_cdscr,
11377 char *note_name = "LINUX";
11378 return elfcore_write_note (abfd, buf, bufsiz,
11379 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11383 elfcore_write_s390_high_gprs (bfd *abfd,
11386 const void *s390_high_gprs,
11389 char *note_name = "LINUX";
11390 return elfcore_write_note (abfd, buf, bufsiz,
11391 note_name, NT_S390_HIGH_GPRS,
11392 s390_high_gprs, size);
11396 elfcore_write_s390_timer (bfd *abfd,
11399 const void *s390_timer,
11402 char *note_name = "LINUX";
11403 return elfcore_write_note (abfd, buf, bufsiz,
11404 note_name, NT_S390_TIMER, s390_timer, size);
11408 elfcore_write_s390_todcmp (bfd *abfd,
11411 const void *s390_todcmp,
11414 char *note_name = "LINUX";
11415 return elfcore_write_note (abfd, buf, bufsiz,
11416 note_name, NT_S390_TODCMP, s390_todcmp, size);
11420 elfcore_write_s390_todpreg (bfd *abfd,
11423 const void *s390_todpreg,
11426 char *note_name = "LINUX";
11427 return elfcore_write_note (abfd, buf, bufsiz,
11428 note_name, NT_S390_TODPREG, s390_todpreg, size);
11432 elfcore_write_s390_ctrs (bfd *abfd,
11435 const void *s390_ctrs,
11438 char *note_name = "LINUX";
11439 return elfcore_write_note (abfd, buf, bufsiz,
11440 note_name, NT_S390_CTRS, s390_ctrs, size);
11444 elfcore_write_s390_prefix (bfd *abfd,
11447 const void *s390_prefix,
11450 char *note_name = "LINUX";
11451 return elfcore_write_note (abfd, buf, bufsiz,
11452 note_name, NT_S390_PREFIX, s390_prefix, size);
11456 elfcore_write_s390_last_break (bfd *abfd,
11459 const void *s390_last_break,
11462 char *note_name = "LINUX";
11463 return elfcore_write_note (abfd, buf, bufsiz,
11464 note_name, NT_S390_LAST_BREAK,
11465 s390_last_break, size);
11469 elfcore_write_s390_system_call (bfd *abfd,
11472 const void *s390_system_call,
11475 char *note_name = "LINUX";
11476 return elfcore_write_note (abfd, buf, bufsiz,
11477 note_name, NT_S390_SYSTEM_CALL,
11478 s390_system_call, size);
11482 elfcore_write_s390_tdb (bfd *abfd,
11485 const void *s390_tdb,
11488 char *note_name = "LINUX";
11489 return elfcore_write_note (abfd, buf, bufsiz,
11490 note_name, NT_S390_TDB, s390_tdb, size);
11494 elfcore_write_s390_vxrs_low (bfd *abfd,
11497 const void *s390_vxrs_low,
11500 char *note_name = "LINUX";
11501 return elfcore_write_note (abfd, buf, bufsiz,
11502 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11506 elfcore_write_s390_vxrs_high (bfd *abfd,
11509 const void *s390_vxrs_high,
11512 char *note_name = "LINUX";
11513 return elfcore_write_note (abfd, buf, bufsiz,
11514 note_name, NT_S390_VXRS_HIGH,
11515 s390_vxrs_high, size);
11519 elfcore_write_s390_gs_cb (bfd *abfd,
11522 const void *s390_gs_cb,
11525 char *note_name = "LINUX";
11526 return elfcore_write_note (abfd, buf, bufsiz,
11527 note_name, NT_S390_GS_CB,
11532 elfcore_write_s390_gs_bc (bfd *abfd,
11535 const void *s390_gs_bc,
11538 char *note_name = "LINUX";
11539 return elfcore_write_note (abfd, buf, bufsiz,
11540 note_name, NT_S390_GS_BC,
11545 elfcore_write_arm_vfp (bfd *abfd,
11548 const void *arm_vfp,
11551 char *note_name = "LINUX";
11552 return elfcore_write_note (abfd, buf, bufsiz,
11553 note_name, NT_ARM_VFP, arm_vfp, size);
11557 elfcore_write_aarch_tls (bfd *abfd,
11560 const void *aarch_tls,
11563 char *note_name = "LINUX";
11564 return elfcore_write_note (abfd, buf, bufsiz,
11565 note_name, NT_ARM_TLS, aarch_tls, size);
11569 elfcore_write_aarch_hw_break (bfd *abfd,
11572 const void *aarch_hw_break,
11575 char *note_name = "LINUX";
11576 return elfcore_write_note (abfd, buf, bufsiz,
11577 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11581 elfcore_write_aarch_hw_watch (bfd *abfd,
11584 const void *aarch_hw_watch,
11587 char *note_name = "LINUX";
11588 return elfcore_write_note (abfd, buf, bufsiz,
11589 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11593 elfcore_write_aarch_sve (bfd *abfd,
11596 const void *aarch_sve,
11599 char *note_name = "LINUX";
11600 return elfcore_write_note (abfd, buf, bufsiz,
11601 note_name, NT_ARM_SVE, aarch_sve, size);
11605 elfcore_write_aarch_pauth (bfd *abfd,
11608 const void *aarch_pauth,
11611 char *note_name = "LINUX";
11612 return elfcore_write_note (abfd, buf, bufsiz,
11613 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11617 elfcore_write_register_note (bfd *abfd,
11620 const char *section,
11624 if (strcmp (section, ".reg2") == 0)
11625 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11626 if (strcmp (section, ".reg-xfp") == 0)
11627 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
11628 if (strcmp (section, ".reg-xstate") == 0)
11629 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
11630 if (strcmp (section, ".reg-ppc-vmx") == 0)
11631 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
11632 if (strcmp (section, ".reg-ppc-vsx") == 0)
11633 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
11634 if (strcmp (section, ".reg-ppc-tar") == 0)
11635 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11636 if (strcmp (section, ".reg-ppc-ppr") == 0)
11637 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11638 if (strcmp (section, ".reg-ppc-dscr") == 0)
11639 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11640 if (strcmp (section, ".reg-ppc-ebb") == 0)
11641 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11642 if (strcmp (section, ".reg-ppc-pmu") == 0)
11643 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11644 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11645 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11646 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11647 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11648 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11649 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11650 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11651 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11652 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11653 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11654 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11655 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11656 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11657 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11658 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11659 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
11660 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11661 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
11662 if (strcmp (section, ".reg-s390-timer") == 0)
11663 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11664 if (strcmp (section, ".reg-s390-todcmp") == 0)
11665 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11666 if (strcmp (section, ".reg-s390-todpreg") == 0)
11667 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11668 if (strcmp (section, ".reg-s390-ctrs") == 0)
11669 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11670 if (strcmp (section, ".reg-s390-prefix") == 0)
11671 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
11672 if (strcmp (section, ".reg-s390-last-break") == 0)
11673 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11674 if (strcmp (section, ".reg-s390-system-call") == 0)
11675 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
11676 if (strcmp (section, ".reg-s390-tdb") == 0)
11677 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
11678 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11679 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11680 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11681 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
11682 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11683 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11684 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11685 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
11686 if (strcmp (section, ".reg-arm-vfp") == 0)
11687 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
11688 if (strcmp (section, ".reg-aarch-tls") == 0)
11689 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11690 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11691 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11692 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11693 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
11694 if (strcmp (section, ".reg-aarch-sve") == 0)
11695 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
11696 if (strcmp (section, ".reg-aarch-pauth") == 0)
11697 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
11702 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11707 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11708 gABI specifies that PT_NOTE alignment should be aligned to 4
11709 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11710 align is less than 4, we use 4 byte alignment. */
11713 if (align != 4 && align != 8)
11717 while (p < buf + size)
11719 Elf_External_Note *xnp = (Elf_External_Note *) p;
11720 Elf_Internal_Note in;
11722 if (offsetof (Elf_External_Note, name) > buf - p + size)
11725 in.type = H_GET_32 (abfd, xnp->type);
11727 in.namesz = H_GET_32 (abfd, xnp->namesz);
11728 in.namedata = xnp->name;
11729 if (in.namesz > buf - in.namedata + size)
11732 in.descsz = H_GET_32 (abfd, xnp->descsz);
11733 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
11734 in.descpos = offset + (in.descdata - buf);
11736 && (in.descdata >= buf + size
11737 || in.descsz > buf - in.descdata + size))
11740 switch (bfd_get_format (abfd))
11747 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11750 const char * string;
11752 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
11756 GROKER_ELEMENT ("", elfcore_grok_note),
11757 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11758 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11759 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11760 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11761 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
11763 #undef GROKER_ELEMENT
11766 for (i = ARRAY_SIZE (grokers); i--;)
11768 if (in.namesz >= grokers[i].len
11769 && strncmp (in.namedata, grokers[i].string,
11770 grokers[i].len) == 0)
11772 if (! grokers[i].func (abfd, & in))
11781 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11783 if (! elfobj_grok_gnu_note (abfd, &in))
11786 else if (in.namesz == sizeof "stapsdt"
11787 && strcmp (in.namedata, "stapsdt") == 0)
11789 if (! elfobj_grok_stapsdt_note (abfd, &in))
11795 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
11802 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11807 if (size == 0 || (size + 1) == 0)
11810 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11813 buf = (char *) bfd_malloc (size + 1);
11817 /* PR 17512: file: ec08f814
11818 0-termintate the buffer so that string searches will not overflow. */
11821 if (bfd_bread (buf, size, abfd) != size
11822 || !elf_parse_notes (abfd, buf, size, offset, align))
11832 /* Providing external access to the ELF program header table. */
11834 /* Return an upper bound on the number of bytes required to store a
11835 copy of ABFD's program header table entries. Return -1 if an error
11836 occurs; bfd_get_error will return an appropriate code. */
11839 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11841 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11843 bfd_set_error (bfd_error_wrong_format);
11847 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11850 /* Copy ABFD's program header table entries to *PHDRS. The entries
11851 will be stored as an array of Elf_Internal_Phdr structures, as
11852 defined in include/elf/internal.h. To find out how large the
11853 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11855 Return the number of program header table entries read, or -1 if an
11856 error occurs; bfd_get_error will return an appropriate code. */
11859 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11863 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11865 bfd_set_error (bfd_error_wrong_format);
11869 num_phdrs = elf_elfheader (abfd)->e_phnum;
11870 if (num_phdrs != 0)
11871 memcpy (phdrs, elf_tdata (abfd)->phdr,
11872 num_phdrs * sizeof (Elf_Internal_Phdr));
11877 enum elf_reloc_type_class
11878 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11879 const asection *rel_sec ATTRIBUTE_UNUSED,
11880 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
11882 return reloc_class_normal;
11885 /* For RELA architectures, return the relocation value for a
11886 relocation against a local symbol. */
11889 _bfd_elf_rela_local_sym (bfd *abfd,
11890 Elf_Internal_Sym *sym,
11892 Elf_Internal_Rela *rel)
11894 asection *sec = *psec;
11895 bfd_vma relocation;
11897 relocation = (sec->output_section->vma
11898 + sec->output_offset
11900 if ((sec->flags & SEC_MERGE)
11901 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
11902 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
11905 _bfd_merged_section_offset (abfd, psec,
11906 elf_section_data (sec)->sec_info,
11907 sym->st_value + rel->r_addend);
11910 /* If we have changed the section, and our original section is
11911 marked with SEC_EXCLUDE, it means that the original
11912 SEC_MERGE section has been completely subsumed in some
11913 other SEC_MERGE section. In this case, we need to leave
11914 some info around for --emit-relocs. */
11915 if ((sec->flags & SEC_EXCLUDE) != 0)
11916 sec->kept_section = *psec;
11919 rel->r_addend -= relocation;
11920 rel->r_addend += sec->output_section->vma + sec->output_offset;
11926 _bfd_elf_rel_local_sym (bfd *abfd,
11927 Elf_Internal_Sym *sym,
11931 asection *sec = *psec;
11933 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
11934 return sym->st_value + addend;
11936 return _bfd_merged_section_offset (abfd, psec,
11937 elf_section_data (sec)->sec_info,
11938 sym->st_value + addend);
11941 /* Adjust an address within a section. Given OFFSET within SEC, return
11942 the new offset within the section, based upon changes made to the
11943 section. Returns -1 if the offset is now invalid.
11944 The offset (in abnd out) is in target sized bytes, however big a
11948 _bfd_elf_section_offset (bfd *abfd,
11949 struct bfd_link_info *info,
11953 switch (sec->sec_info_type)
11955 case SEC_INFO_TYPE_STABS:
11956 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11958 case SEC_INFO_TYPE_EH_FRAME:
11959 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
11962 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11964 /* Reverse the offset. */
11965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11966 bfd_size_type address_size = bed->s->arch_size / 8;
11968 /* address_size and sec->size are in octets. Convert
11969 to bytes before subtracting the original offset. */
11970 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
11976 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11977 reconstruct an ELF file by reading the segments out of remote memory
11978 based on the ELF file header at EHDR_VMA and the ELF program headers it
11979 points to. If not null, *LOADBASEP is filled in with the difference
11980 between the VMAs from which the segments were read, and the VMAs the
11981 file headers (and hence BFD's idea of each section's VMA) put them at.
11983 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11984 remote memory at target address VMA into the local buffer at MYADDR; it
11985 should return zero on success or an `errno' code on failure. TEMPL must
11986 be a BFD for an ELF target with the word size and byte order found in
11987 the remote memory. */
11990 bfd_elf_bfd_from_remote_memory
11993 bfd_size_type size,
11994 bfd_vma *loadbasep,
11995 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
11997 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
11998 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12002 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12003 long symcount ATTRIBUTE_UNUSED,
12004 asymbol **syms ATTRIBUTE_UNUSED,
12009 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12012 const char *relplt_name;
12013 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12017 Elf_Internal_Shdr *hdr;
12023 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12026 if (dynsymcount <= 0)
12029 if (!bed->plt_sym_val)
12032 relplt_name = bed->relplt_name;
12033 if (relplt_name == NULL)
12034 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12035 relplt = bfd_get_section_by_name (abfd, relplt_name);
12036 if (relplt == NULL)
12039 hdr = &elf_section_data (relplt)->this_hdr;
12040 if (hdr->sh_link != elf_dynsymtab (abfd)
12041 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12044 plt = bfd_get_section_by_name (abfd, ".plt");
12048 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12049 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
12052 count = relplt->size / hdr->sh_entsize;
12053 size = count * sizeof (asymbol);
12054 p = relplt->relocation;
12055 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12057 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12058 if (p->addend != 0)
12061 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12063 size += sizeof ("+0x") - 1 + 8;
12068 s = *ret = (asymbol *) bfd_malloc (size);
12072 names = (char *) (s + count);
12073 p = relplt->relocation;
12075 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12080 addr = bed->plt_sym_val (i, plt, p);
12081 if (addr == (bfd_vma) -1)
12084 *s = **p->sym_ptr_ptr;
12085 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12086 we are defining a symbol, ensure one of them is set. */
12087 if ((s->flags & BSF_LOCAL) == 0)
12088 s->flags |= BSF_GLOBAL;
12089 s->flags |= BSF_SYNTHETIC;
12091 s->value = addr - plt->vma;
12094 len = strlen ((*p->sym_ptr_ptr)->name);
12095 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12097 if (p->addend != 0)
12101 memcpy (names, "+0x", sizeof ("+0x") - 1);
12102 names += sizeof ("+0x") - 1;
12103 bfd_sprintf_vma (abfd, buf, p->addend);
12104 for (a = buf; *a == '0'; ++a)
12107 memcpy (names, a, len);
12110 memcpy (names, "@plt", sizeof ("@plt"));
12111 names += sizeof ("@plt");
12118 /* It is only used by x86-64 so far.
12119 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12120 but current usage would allow all of _bfd_std_section to be zero. */
12121 static const asymbol lcomm_sym
12122 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12123 asection _bfd_elf_large_com_section
12124 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12125 "LARGE_COMMON", 0, SEC_IS_COMMON);
12128 _bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED,
12129 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12134 _bfd_elf_final_write_processing (bfd *abfd)
12136 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
12138 i_ehdrp = elf_elfheader (abfd);
12140 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12141 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
12143 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12144 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12145 STB_GNU_UNIQUE binding. */
12146 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12148 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12149 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12150 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12151 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12153 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12154 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12155 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12156 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12157 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12158 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12159 bfd_set_error (bfd_error_bad_value);
12167 /* Return TRUE for ELF symbol types that represent functions.
12168 This is the default version of this function, which is sufficient for
12169 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12172 _bfd_elf_is_function_type (unsigned int type)
12174 return (type == STT_FUNC
12175 || type == STT_GNU_IFUNC);
12178 /* If the ELF symbol SYM might be a function in SEC, return the
12179 function size and set *CODE_OFF to the function's entry point,
12180 otherwise return zero. */
12183 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12186 bfd_size_type size;
12188 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
12189 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12190 || sym->section != sec)
12193 *code_off = sym->value;
12195 if (!(sym->flags & BSF_SYNTHETIC))
12196 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;