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 /* Make a BFD section from an ELF section. We store a pointer to the
1003 BFD section in the bfd_section field of the header. */
1006 _bfd_elf_make_section_from_shdr (bfd *abfd,
1007 Elf_Internal_Shdr *hdr,
1013 const struct elf_backend_data *bed;
1015 if (hdr->bfd_section != NULL)
1018 newsect = bfd_make_section_anyway (abfd, name);
1019 if (newsect == NULL)
1022 hdr->bfd_section = newsect;
1023 elf_section_data (newsect)->this_hdr = *hdr;
1024 elf_section_data (newsect)->this_idx = shindex;
1026 /* Always use the real type/flags. */
1027 elf_section_type (newsect) = hdr->sh_type;
1028 elf_section_flags (newsect) = hdr->sh_flags;
1030 newsect->filepos = hdr->sh_offset;
1032 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1033 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1034 || ! bfd_set_section_alignment (abfd, newsect,
1035 bfd_log2 (hdr->sh_addralign)))
1038 flags = SEC_NO_FLAGS;
1039 if (hdr->sh_type != SHT_NOBITS)
1040 flags |= SEC_HAS_CONTENTS;
1041 if (hdr->sh_type == SHT_GROUP)
1043 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1046 if (hdr->sh_type != SHT_NOBITS)
1049 if ((hdr->sh_flags & SHF_WRITE) == 0)
1050 flags |= SEC_READONLY;
1051 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1053 else if ((flags & SEC_LOAD) != 0)
1055 if ((hdr->sh_flags & SHF_MERGE) != 0)
1058 newsect->entsize = hdr->sh_entsize;
1060 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1061 flags |= SEC_STRINGS;
1062 if (hdr->sh_flags & SHF_GROUP)
1063 if (!setup_group (abfd, hdr, newsect))
1065 if ((hdr->sh_flags & SHF_TLS) != 0)
1066 flags |= SEC_THREAD_LOCAL;
1067 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1068 flags |= SEC_EXCLUDE;
1070 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1072 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1073 but binutils as of 2019-07-23 did not set the EI_OSABI header
1077 case ELFOSABI_FREEBSD:
1078 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1079 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1083 if ((flags & SEC_ALLOC) == 0)
1085 /* The debugging sections appear to be recognized only by name,
1086 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1087 if (name [0] == '.')
1092 p = ".debug", n = 6;
1093 else if (name[1] == 'g' && name[2] == 'n')
1094 p = ".gnu.linkonce.wi.", n = 17;
1095 else if (name[1] == 'g' && name[2] == 'd')
1096 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1097 else if (name[1] == 'l')
1099 else if (name[1] == 's')
1101 else if (name[1] == 'z')
1102 p = ".zdebug", n = 7;
1105 if (p != NULL && strncmp (name, p, n) == 0)
1106 flags |= SEC_DEBUGGING;
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
1116 if (CONST_STRNEQ (name, ".gnu.linkonce")
1117 && elf_next_in_group (newsect) == NULL)
1118 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1120 bed = get_elf_backend_data (abfd);
1121 if (bed->elf_backend_section_flags)
1122 if (! bed->elf_backend_section_flags (&flags, hdr))
1125 if (! bfd_set_section_flags (abfd, newsect, flags))
1128 /* We do not parse the PT_NOTE segments as we are interested even in the
1129 separate debug info files which may have the segments offsets corrupted.
1130 PT_NOTEs from the core files are currently not parsed using BFD. */
1131 if (hdr->sh_type == SHT_NOTE)
1135 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1138 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1139 hdr->sh_offset, hdr->sh_addralign);
1143 if ((flags & SEC_ALLOC) != 0)
1145 Elf_Internal_Phdr *phdr;
1146 unsigned int i, nload;
1148 /* Some ELF linkers produce binaries with all the program header
1149 p_paddr fields zero. If we have such a binary with more than
1150 one PT_LOAD header, then leave the section lma equal to vma
1151 so that we don't create sections with overlapping lma. */
1152 phdr = elf_tdata (abfd)->phdr;
1153 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1154 if (phdr->p_paddr != 0)
1156 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1158 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1161 phdr = elf_tdata (abfd)->phdr;
1162 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1164 if (((phdr->p_type == PT_LOAD
1165 && (hdr->sh_flags & SHF_TLS) == 0)
1166 || phdr->p_type == PT_TLS)
1167 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1169 if ((flags & SEC_LOAD) == 0)
1170 newsect->lma = (phdr->p_paddr
1171 + hdr->sh_addr - phdr->p_vaddr);
1173 /* We used to use the same adjustment for SEC_LOAD
1174 sections, but that doesn't work if the segment
1175 is packed with code from multiple VMAs.
1176 Instead we calculate the section LMA based on
1177 the segment LMA. It is assumed that the
1178 segment will contain sections with contiguous
1179 LMAs, even if the VMAs are not. */
1180 newsect->lma = (phdr->p_paddr
1181 + hdr->sh_offset - phdr->p_offset);
1183 /* With contiguous segments, we can't tell from file
1184 offsets whether a section with zero size should
1185 be placed at the end of one segment or the
1186 beginning of the next. Decide based on vaddr. */
1187 if (hdr->sh_addr >= phdr->p_vaddr
1188 && (hdr->sh_addr + hdr->sh_size
1189 <= phdr->p_vaddr + phdr->p_memsz))
1195 /* Compress/decompress DWARF debug sections with names: .debug_* and
1196 .zdebug_*, after the section flags is set. */
1197 if ((flags & SEC_DEBUGGING)
1198 && ((name[1] == 'd' && name[6] == '_')
1199 || (name[1] == 'z' && name[7] == '_')))
1201 enum { nothing, compress, decompress } action = nothing;
1202 int compression_header_size;
1203 bfd_size_type uncompressed_size;
1204 unsigned int uncompressed_align_power;
1205 bfd_boolean compressed
1206 = bfd_is_section_compressed_with_header (abfd, newsect,
1207 &compression_header_size,
1209 &uncompressed_align_power);
1212 /* Compressed section. Check if we should decompress. */
1213 if ((abfd->flags & BFD_DECOMPRESS))
1214 action = decompress;
1217 /* Compress the uncompressed section or convert from/to .zdebug*
1218 section. Check if we should compress. */
1219 if (action == nothing)
1221 if (newsect->size != 0
1222 && (abfd->flags & BFD_COMPRESS)
1223 && compression_header_size >= 0
1224 && uncompressed_size > 0
1226 || ((compression_header_size > 0)
1227 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1233 if (action == compress)
1235 if (!bfd_init_section_compress_status (abfd, newsect))
1238 /* xgettext:c-format */
1239 (_("%pB: unable to initialize compress status for section %s"),
1246 if (!bfd_init_section_decompress_status (abfd, newsect))
1249 /* xgettext:c-format */
1250 (_("%pB: unable to initialize decompress status for section %s"),
1256 if (abfd->is_linker_input)
1259 && (action == decompress
1260 || (action == compress
1261 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1263 /* Convert section name from .zdebug_* to .debug_* so
1264 that linker will consider this section as a debug
1266 char *new_name = convert_zdebug_to_debug (abfd, name);
1267 if (new_name == NULL)
1269 bfd_rename_section (abfd, newsect, new_name);
1273 /* For objdump, don't rename the section. For objcopy, delay
1274 section rename to elf_fake_sections. */
1275 newsect->flags |= SEC_ELF_RENAME;
1281 const char *const bfd_elf_section_type_names[] =
1283 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1284 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1285 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1288 /* ELF relocs are against symbols. If we are producing relocatable
1289 output, and the reloc is against an external symbol, and nothing
1290 has given us any additional addend, the resulting reloc will also
1291 be against the same symbol. In such a case, we don't want to
1292 change anything about the way the reloc is handled, since it will
1293 all be done at final link time. Rather than put special case code
1294 into bfd_perform_relocation, all the reloc types use this howto
1295 function. It just short circuits the reloc if producing
1296 relocatable output against an external symbol. */
1298 bfd_reloc_status_type
1299 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1300 arelent *reloc_entry,
1302 void *data ATTRIBUTE_UNUSED,
1303 asection *input_section,
1305 char **error_message ATTRIBUTE_UNUSED)
1307 if (output_bfd != NULL
1308 && (symbol->flags & BSF_SECTION_SYM) == 0
1309 && (! reloc_entry->howto->partial_inplace
1310 || reloc_entry->addend == 0))
1312 reloc_entry->address += input_section->output_offset;
1313 return bfd_reloc_ok;
1316 return bfd_reloc_continue;
1319 /* Returns TRUE if section A matches section B.
1320 Names, addresses and links may be different, but everything else
1321 should be the same. */
1324 section_match (const Elf_Internal_Shdr * a,
1325 const Elf_Internal_Shdr * b)
1327 if (a->sh_type != b->sh_type
1328 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1329 || a->sh_addralign != b->sh_addralign
1330 || a->sh_entsize != b->sh_entsize)
1332 if (a->sh_type == SHT_SYMTAB
1333 || a->sh_type == SHT_STRTAB)
1335 return a->sh_size == b->sh_size;
1338 /* Find a section in OBFD that has the same characteristics
1339 as IHEADER. Return the index of this section or SHN_UNDEF if
1340 none can be found. Check's section HINT first, as this is likely
1341 to be the correct section. */
1344 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1345 const unsigned int hint)
1347 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1350 BFD_ASSERT (iheader != NULL);
1352 /* See PR 20922 for a reproducer of the NULL test. */
1353 if (hint < elf_numsections (obfd)
1354 && oheaders[hint] != NULL
1355 && section_match (oheaders[hint], iheader))
1358 for (i = 1; i < elf_numsections (obfd); i++)
1360 Elf_Internal_Shdr * oheader = oheaders[i];
1362 if (oheader == NULL)
1364 if (section_match (oheader, iheader))
1365 /* FIXME: Do we care if there is a potential for
1366 multiple matches ? */
1373 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1374 Processor specific section, based upon a matching input section.
1375 Returns TRUE upon success, FALSE otherwise. */
1378 copy_special_section_fields (const bfd *ibfd,
1380 const Elf_Internal_Shdr *iheader,
1381 Elf_Internal_Shdr *oheader,
1382 const unsigned int secnum)
1384 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1385 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1386 bfd_boolean changed = FALSE;
1387 unsigned int sh_link;
1389 if (oheader->sh_type == SHT_NOBITS)
1391 /* This is a feature for objcopy --only-keep-debug:
1392 When a section's type is changed to NOBITS, we preserve
1393 the sh_link and sh_info fields so that they can be
1394 matched up with the original.
1396 Note: Strictly speaking these assignments are wrong.
1397 The sh_link and sh_info fields should point to the
1398 relevent sections in the output BFD, which may not be in
1399 the same location as they were in the input BFD. But
1400 the whole point of this action is to preserve the
1401 original values of the sh_link and sh_info fields, so
1402 that they can be matched up with the section headers in
1403 the original file. So strictly speaking we may be
1404 creating an invalid ELF file, but it is only for a file
1405 that just contains debug info and only for sections
1406 without any contents. */
1407 if (oheader->sh_link == 0)
1408 oheader->sh_link = iheader->sh_link;
1409 if (oheader->sh_info == 0)
1410 oheader->sh_info = iheader->sh_info;
1414 /* Allow the target a chance to decide how these fields should be set. */
1415 if (bed->elf_backend_copy_special_section_fields != NULL
1416 && bed->elf_backend_copy_special_section_fields
1417 (ibfd, obfd, iheader, oheader))
1420 /* We have an iheader which might match oheader, and which has non-zero
1421 sh_info and/or sh_link fields. Attempt to follow those links and find
1422 the section in the output bfd which corresponds to the linked section
1423 in the input bfd. */
1424 if (iheader->sh_link != SHN_UNDEF)
1426 /* See PR 20931 for a reproducer. */
1427 if (iheader->sh_link >= elf_numsections (ibfd))
1430 /* xgettext:c-format */
1431 (_("%pB: invalid sh_link field (%d) in section number %d"),
1432 ibfd, iheader->sh_link, secnum);
1436 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1437 if (sh_link != SHN_UNDEF)
1439 oheader->sh_link = sh_link;
1443 /* FIXME: Should we install iheader->sh_link
1444 if we could not find a match ? */
1446 /* xgettext:c-format */
1447 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1450 if (iheader->sh_info)
1452 /* The sh_info field can hold arbitrary information, but if the
1453 SHF_LINK_INFO flag is set then it should be interpreted as a
1455 if (iheader->sh_flags & SHF_INFO_LINK)
1457 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1459 if (sh_link != SHN_UNDEF)
1460 oheader->sh_flags |= SHF_INFO_LINK;
1463 /* No idea what it means - just copy it. */
1464 sh_link = iheader->sh_info;
1466 if (sh_link != SHN_UNDEF)
1468 oheader->sh_info = sh_link;
1473 /* xgettext:c-format */
1474 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1480 /* Copy the program header and other data from one object module to
1484 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1486 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1487 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1488 const struct elf_backend_data *bed;
1491 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1492 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1495 if (!elf_flags_init (obfd))
1497 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1498 elf_flags_init (obfd) = TRUE;
1501 elf_gp (obfd) = elf_gp (ibfd);
1503 /* Also copy the EI_OSABI field. */
1504 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1505 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1507 /* If set, copy the EI_ABIVERSION field. */
1508 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1509 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1510 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1512 /* Copy object attributes. */
1513 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1515 if (iheaders == NULL || oheaders == NULL)
1518 bed = get_elf_backend_data (obfd);
1520 /* Possibly copy other fields in the section header. */
1521 for (i = 1; i < elf_numsections (obfd); i++)
1524 Elf_Internal_Shdr * oheader = oheaders[i];
1526 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1527 because of a special case need for generating separate debug info
1528 files. See below for more details. */
1530 || (oheader->sh_type != SHT_NOBITS
1531 && oheader->sh_type < SHT_LOOS))
1534 /* Ignore empty sections, and sections whose
1535 fields have already been initialised. */
1536 if (oheader->sh_size == 0
1537 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1540 /* Scan for the matching section in the input bfd.
1541 First we try for a direct mapping between the input and output sections. */
1542 for (j = 1; j < elf_numsections (ibfd); j++)
1544 const Elf_Internal_Shdr * iheader = iheaders[j];
1546 if (iheader == NULL)
1549 if (oheader->bfd_section != NULL
1550 && iheader->bfd_section != NULL
1551 && iheader->bfd_section->output_section != NULL
1552 && iheader->bfd_section->output_section == oheader->bfd_section)
1554 /* We have found a connection from the input section to the
1555 output section. Attempt to copy the header fields. If
1556 this fails then do not try any further sections - there
1557 should only be a one-to-one mapping between input and output. */
1558 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1559 j = elf_numsections (ibfd);
1564 if (j < elf_numsections (ibfd))
1567 /* That failed. So try to deduce the corresponding input section.
1568 Unfortunately we cannot compare names as the output string table
1569 is empty, so instead we check size, address and type. */
1570 for (j = 1; j < elf_numsections (ibfd); j++)
1572 const Elf_Internal_Shdr * iheader = iheaders[j];
1574 if (iheader == NULL)
1577 /* Try matching fields in the input section's header.
1578 Since --only-keep-debug turns all non-debug sections into
1579 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1581 if ((oheader->sh_type == SHT_NOBITS
1582 || iheader->sh_type == oheader->sh_type)
1583 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1584 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1585 && iheader->sh_addralign == oheader->sh_addralign
1586 && iheader->sh_entsize == oheader->sh_entsize
1587 && iheader->sh_size == oheader->sh_size
1588 && iheader->sh_addr == oheader->sh_addr
1589 && (iheader->sh_info != oheader->sh_info
1590 || iheader->sh_link != oheader->sh_link))
1592 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1597 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1599 /* Final attempt. Call the backend copy function
1600 with a NULL input section. */
1601 if (bed->elf_backend_copy_special_section_fields != NULL)
1602 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1610 get_segment_type (unsigned int p_type)
1615 case PT_NULL: pt = "NULL"; break;
1616 case PT_LOAD: pt = "LOAD"; break;
1617 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1618 case PT_INTERP: pt = "INTERP"; break;
1619 case PT_NOTE: pt = "NOTE"; break;
1620 case PT_SHLIB: pt = "SHLIB"; break;
1621 case PT_PHDR: pt = "PHDR"; break;
1622 case PT_TLS: pt = "TLS"; break;
1623 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1624 case PT_GNU_STACK: pt = "STACK"; break;
1625 case PT_GNU_RELRO: pt = "RELRO"; break;
1626 default: pt = NULL; break;
1631 /* Print out the program headers. */
1634 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1636 FILE *f = (FILE *) farg;
1637 Elf_Internal_Phdr *p;
1639 bfd_byte *dynbuf = NULL;
1641 p = elf_tdata (abfd)->phdr;
1646 fprintf (f, _("\nProgram Header:\n"));
1647 c = elf_elfheader (abfd)->e_phnum;
1648 for (i = 0; i < c; i++, p++)
1650 const char *pt = get_segment_type (p->p_type);
1655 sprintf (buf, "0x%lx", p->p_type);
1658 fprintf (f, "%8s off 0x", pt);
1659 bfd_fprintf_vma (abfd, f, p->p_offset);
1660 fprintf (f, " vaddr 0x");
1661 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1662 fprintf (f, " paddr 0x");
1663 bfd_fprintf_vma (abfd, f, p->p_paddr);
1664 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1665 fprintf (f, " filesz 0x");
1666 bfd_fprintf_vma (abfd, f, p->p_filesz);
1667 fprintf (f, " memsz 0x");
1668 bfd_fprintf_vma (abfd, f, p->p_memsz);
1669 fprintf (f, " flags %c%c%c",
1670 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1671 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1672 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1673 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1674 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1679 s = bfd_get_section_by_name (abfd, ".dynamic");
1682 unsigned int elfsec;
1683 unsigned long shlink;
1684 bfd_byte *extdyn, *extdynend;
1686 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1688 fprintf (f, _("\nDynamic Section:\n"));
1690 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1693 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1694 if (elfsec == SHN_BAD)
1696 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1698 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1699 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1702 /* PR 17512: file: 6f427532. */
1703 if (s->size < extdynsize)
1705 extdynend = extdyn + s->size;
1706 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1708 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1710 Elf_Internal_Dyn dyn;
1711 const char *name = "";
1713 bfd_boolean stringp;
1714 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1716 (*swap_dyn_in) (abfd, extdyn, &dyn);
1718 if (dyn.d_tag == DT_NULL)
1725 if (bed->elf_backend_get_target_dtag)
1726 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1728 if (!strcmp (name, ""))
1730 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1735 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1736 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1737 case DT_PLTGOT: name = "PLTGOT"; break;
1738 case DT_HASH: name = "HASH"; break;
1739 case DT_STRTAB: name = "STRTAB"; break;
1740 case DT_SYMTAB: name = "SYMTAB"; break;
1741 case DT_RELA: name = "RELA"; break;
1742 case DT_RELASZ: name = "RELASZ"; break;
1743 case DT_RELAENT: name = "RELAENT"; break;
1744 case DT_STRSZ: name = "STRSZ"; break;
1745 case DT_SYMENT: name = "SYMENT"; break;
1746 case DT_INIT: name = "INIT"; break;
1747 case DT_FINI: name = "FINI"; break;
1748 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1749 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1750 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1751 case DT_REL: name = "REL"; break;
1752 case DT_RELSZ: name = "RELSZ"; break;
1753 case DT_RELENT: name = "RELENT"; break;
1754 case DT_PLTREL: name = "PLTREL"; break;
1755 case DT_DEBUG: name = "DEBUG"; break;
1756 case DT_TEXTREL: name = "TEXTREL"; break;
1757 case DT_JMPREL: name = "JMPREL"; break;
1758 case DT_BIND_NOW: name = "BIND_NOW"; break;
1759 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1760 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1761 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1762 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1763 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1764 case DT_FLAGS: name = "FLAGS"; break;
1765 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1766 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1767 case DT_CHECKSUM: name = "CHECKSUM"; break;
1768 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1769 case DT_MOVEENT: name = "MOVEENT"; break;
1770 case DT_MOVESZ: name = "MOVESZ"; break;
1771 case DT_FEATURE: name = "FEATURE"; break;
1772 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1773 case DT_SYMINSZ: name = "SYMINSZ"; break;
1774 case DT_SYMINENT: name = "SYMINENT"; break;
1775 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1776 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1777 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1778 case DT_PLTPAD: name = "PLTPAD"; break;
1779 case DT_MOVETAB: name = "MOVETAB"; break;
1780 case DT_SYMINFO: name = "SYMINFO"; break;
1781 case DT_RELACOUNT: name = "RELACOUNT"; break;
1782 case DT_RELCOUNT: name = "RELCOUNT"; break;
1783 case DT_FLAGS_1: name = "FLAGS_1"; break;
1784 case DT_VERSYM: name = "VERSYM"; break;
1785 case DT_VERDEF: name = "VERDEF"; break;
1786 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1787 case DT_VERNEED: name = "VERNEED"; break;
1788 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1789 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1790 case DT_USED: name = "USED"; break;
1791 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1792 case DT_GNU_HASH: name = "GNU_HASH"; break;
1795 fprintf (f, " %-20s ", name);
1799 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1804 unsigned int tagv = dyn.d_un.d_val;
1806 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1809 fprintf (f, "%s", string);
1818 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1819 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1821 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1825 if (elf_dynverdef (abfd) != 0)
1827 Elf_Internal_Verdef *t;
1829 fprintf (f, _("\nVersion definitions:\n"));
1830 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1832 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1833 t->vd_flags, t->vd_hash,
1834 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1835 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1837 Elf_Internal_Verdaux *a;
1840 for (a = t->vd_auxptr->vda_nextptr;
1844 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1850 if (elf_dynverref (abfd) != 0)
1852 Elf_Internal_Verneed *t;
1854 fprintf (f, _("\nVersion References:\n"));
1855 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1857 Elf_Internal_Vernaux *a;
1859 fprintf (f, _(" required from %s:\n"),
1860 t->vn_filename ? t->vn_filename : "<corrupt>");
1861 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1862 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1863 a->vna_flags, a->vna_other,
1864 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1876 /* Get version string. */
1879 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1880 bfd_boolean *hidden)
1882 const char *version_string = NULL;
1883 if (elf_dynversym (abfd) != 0
1884 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1886 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1888 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1889 vernum &= VERSYM_VERSION;
1892 version_string = "";
1893 else if (vernum == 1
1894 && (vernum > elf_tdata (abfd)->cverdefs
1895 || (elf_tdata (abfd)->verdef[0].vd_flags
1897 version_string = "Base";
1898 else if (vernum <= elf_tdata (abfd)->cverdefs)
1900 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1903 Elf_Internal_Verneed *t;
1905 version_string = _("<corrupt>");
1906 for (t = elf_tdata (abfd)->verref;
1910 Elf_Internal_Vernaux *a;
1912 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1914 if (a->vna_other == vernum)
1916 version_string = a->vna_nodename;
1923 return version_string;
1926 /* Display ELF-specific fields of a symbol. */
1929 bfd_elf_print_symbol (bfd *abfd,
1932 bfd_print_symbol_type how)
1934 FILE *file = (FILE *) filep;
1937 case bfd_print_symbol_name:
1938 fprintf (file, "%s", symbol->name);
1940 case bfd_print_symbol_more:
1941 fprintf (file, "elf ");
1942 bfd_fprintf_vma (abfd, file, symbol->value);
1943 fprintf (file, " %x", symbol->flags);
1945 case bfd_print_symbol_all:
1947 const char *section_name;
1948 const char *name = NULL;
1949 const struct elf_backend_data *bed;
1950 unsigned char st_other;
1952 const char *version_string;
1955 section_name = symbol->section ? symbol->section->name : "(*none*)";
1957 bed = get_elf_backend_data (abfd);
1958 if (bed->elf_backend_print_symbol_all)
1959 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1963 name = symbol->name;
1964 bfd_print_symbol_vandf (abfd, file, symbol);
1967 fprintf (file, " %s\t", section_name);
1968 /* Print the "other" value for a symbol. For common symbols,
1969 we've already printed the size; now print the alignment.
1970 For other symbols, we have no specified alignment, and
1971 we've printed the address; now print the size. */
1972 if (symbol->section && bfd_is_com_section (symbol->section))
1973 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1975 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1976 bfd_fprintf_vma (abfd, file, val);
1978 /* If we have version information, print it. */
1979 version_string = _bfd_elf_get_symbol_version_string (abfd,
1985 fprintf (file, " %-11s", version_string);
1990 fprintf (file, " (%s)", version_string);
1991 for (i = 10 - strlen (version_string); i > 0; --i)
1996 /* If the st_other field is not zero, print it. */
1997 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2002 case STV_INTERNAL: fprintf (file, " .internal"); break;
2003 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2004 case STV_PROTECTED: fprintf (file, " .protected"); break;
2006 /* Some other non-defined flags are also present, so print
2008 fprintf (file, " 0x%02x", (unsigned int) st_other);
2011 fprintf (file, " %s", name);
2017 /* ELF .o/exec file reading */
2019 /* Create a new bfd section from an ELF section header. */
2022 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2024 Elf_Internal_Shdr *hdr;
2025 Elf_Internal_Ehdr *ehdr;
2026 const struct elf_backend_data *bed;
2028 bfd_boolean ret = TRUE;
2029 static bfd_boolean * sections_being_created = NULL;
2030 static bfd * sections_being_created_abfd = NULL;
2031 static unsigned int nesting = 0;
2033 if (shindex >= elf_numsections (abfd))
2038 /* PR17512: A corrupt ELF binary might contain a recursive group of
2039 sections, with each the string indices pointing to the next in the
2040 loop. Detect this here, by refusing to load a section that we are
2041 already in the process of loading. We only trigger this test if
2042 we have nested at least three sections deep as normal ELF binaries
2043 can expect to recurse at least once.
2045 FIXME: It would be better if this array was attached to the bfd,
2046 rather than being held in a static pointer. */
2048 if (sections_being_created_abfd != abfd)
2049 sections_being_created = NULL;
2050 if (sections_being_created == NULL)
2052 sections_being_created = (bfd_boolean *)
2053 bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean));
2054 sections_being_created_abfd = abfd;
2056 if (sections_being_created [shindex])
2059 (_("%pB: warning: loop in section dependencies detected"), abfd);
2062 sections_being_created [shindex] = TRUE;
2065 hdr = elf_elfsections (abfd)[shindex];
2066 ehdr = elf_elfheader (abfd);
2067 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2072 bed = get_elf_backend_data (abfd);
2073 switch (hdr->sh_type)
2076 /* Inactive section. Throw it away. */
2079 case SHT_PROGBITS: /* Normal section with contents. */
2080 case SHT_NOBITS: /* .bss section. */
2081 case SHT_HASH: /* .hash section. */
2082 case SHT_NOTE: /* .note section. */
2083 case SHT_INIT_ARRAY: /* .init_array section. */
2084 case SHT_FINI_ARRAY: /* .fini_array section. */
2085 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2086 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2087 case SHT_GNU_HASH: /* .gnu.hash section. */
2088 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2091 case SHT_DYNAMIC: /* Dynamic linking information. */
2092 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2095 if (hdr->sh_link > elf_numsections (abfd))
2097 /* PR 10478: Accept Solaris binaries with a sh_link
2098 field set to SHN_BEFORE or SHN_AFTER. */
2099 switch (bfd_get_arch (abfd))
2102 case bfd_arch_sparc:
2103 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2104 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2106 /* Otherwise fall through. */
2111 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2113 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2115 Elf_Internal_Shdr *dynsymhdr;
2117 /* The shared libraries distributed with hpux11 have a bogus
2118 sh_link field for the ".dynamic" section. Find the
2119 string table for the ".dynsym" section instead. */
2120 if (elf_dynsymtab (abfd) != 0)
2122 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2123 hdr->sh_link = dynsymhdr->sh_link;
2127 unsigned int i, num_sec;
2129 num_sec = elf_numsections (abfd);
2130 for (i = 1; i < num_sec; i++)
2132 dynsymhdr = elf_elfsections (abfd)[i];
2133 if (dynsymhdr->sh_type == SHT_DYNSYM)
2135 hdr->sh_link = dynsymhdr->sh_link;
2143 case SHT_SYMTAB: /* A symbol table. */
2144 if (elf_onesymtab (abfd) == shindex)
2147 if (hdr->sh_entsize != bed->s->sizeof_sym)
2150 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2152 if (hdr->sh_size != 0)
2154 /* Some assemblers erroneously set sh_info to one with a
2155 zero sh_size. ld sees this as a global symbol count
2156 of (unsigned) -1. Fix it here. */
2161 /* PR 18854: A binary might contain more than one symbol table.
2162 Unusual, but possible. Warn, but continue. */
2163 if (elf_onesymtab (abfd) != 0)
2166 /* xgettext:c-format */
2167 (_("%pB: warning: multiple symbol tables detected"
2168 " - ignoring the table in section %u"),
2172 elf_onesymtab (abfd) = shindex;
2173 elf_symtab_hdr (abfd) = *hdr;
2174 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2175 abfd->flags |= HAS_SYMS;
2177 /* Sometimes a shared object will map in the symbol table. If
2178 SHF_ALLOC is set, and this is a shared object, then we also
2179 treat this section as a BFD section. We can not base the
2180 decision purely on SHF_ALLOC, because that flag is sometimes
2181 set in a relocatable object file, which would confuse the
2183 if ((hdr->sh_flags & SHF_ALLOC) != 0
2184 && (abfd->flags & DYNAMIC) != 0
2185 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2189 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2190 can't read symbols without that section loaded as well. It
2191 is most likely specified by the next section header. */
2193 elf_section_list * entry;
2194 unsigned int i, num_sec;
2196 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2197 if (entry->hdr.sh_link == shindex)
2200 num_sec = elf_numsections (abfd);
2201 for (i = shindex + 1; i < num_sec; i++)
2203 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2205 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2206 && hdr2->sh_link == shindex)
2211 for (i = 1; i < shindex; i++)
2213 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2215 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2216 && hdr2->sh_link == shindex)
2221 ret = bfd_section_from_shdr (abfd, i);
2222 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2226 case SHT_DYNSYM: /* A dynamic symbol table. */
2227 if (elf_dynsymtab (abfd) == shindex)
2230 if (hdr->sh_entsize != bed->s->sizeof_sym)
2233 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2235 if (hdr->sh_size != 0)
2238 /* Some linkers erroneously set sh_info to one with a
2239 zero sh_size. ld sees this as a global symbol count
2240 of (unsigned) -1. Fix it here. */
2245 /* PR 18854: A binary might contain more than one dynamic symbol table.
2246 Unusual, but possible. Warn, but continue. */
2247 if (elf_dynsymtab (abfd) != 0)
2250 /* xgettext:c-format */
2251 (_("%pB: warning: multiple dynamic symbol tables detected"
2252 " - ignoring the table in section %u"),
2256 elf_dynsymtab (abfd) = shindex;
2257 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2258 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2259 abfd->flags |= HAS_SYMS;
2261 /* Besides being a symbol table, we also treat this as a regular
2262 section, so that objcopy can handle it. */
2263 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2266 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2268 elf_section_list * entry;
2270 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2271 if (entry->ndx == shindex)
2274 entry = bfd_alloc (abfd, sizeof (*entry));
2277 entry->ndx = shindex;
2279 entry->next = elf_symtab_shndx_list (abfd);
2280 elf_symtab_shndx_list (abfd) = entry;
2281 elf_elfsections (abfd)[shindex] = & entry->hdr;
2285 case SHT_STRTAB: /* A string table. */
2286 if (hdr->bfd_section != NULL)
2289 if (ehdr->e_shstrndx == shindex)
2291 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2292 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2296 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2299 elf_tdata (abfd)->strtab_hdr = *hdr;
2300 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2304 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2307 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2308 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2309 elf_elfsections (abfd)[shindex] = hdr;
2310 /* We also treat this as a regular section, so that objcopy
2312 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2317 /* If the string table isn't one of the above, then treat it as a
2318 regular section. We need to scan all the headers to be sure,
2319 just in case this strtab section appeared before the above. */
2320 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2322 unsigned int i, num_sec;
2324 num_sec = elf_numsections (abfd);
2325 for (i = 1; i < num_sec; i++)
2327 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2328 if (hdr2->sh_link == shindex)
2330 /* Prevent endless recursion on broken objects. */
2333 if (! bfd_section_from_shdr (abfd, i))
2335 if (elf_onesymtab (abfd) == i)
2337 if (elf_dynsymtab (abfd) == i)
2338 goto dynsymtab_strtab;
2342 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2347 /* *These* do a lot of work -- but build no sections! */
2349 asection *target_sect;
2350 Elf_Internal_Shdr *hdr2, **p_hdr;
2351 unsigned int num_sec = elf_numsections (abfd);
2352 struct bfd_elf_section_data *esdt;
2355 != (bfd_size_type) (hdr->sh_type == SHT_REL
2356 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2359 /* Check for a bogus link to avoid crashing. */
2360 if (hdr->sh_link >= num_sec)
2363 /* xgettext:c-format */
2364 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2365 abfd, hdr->sh_link, name, shindex);
2366 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2371 /* For some incomprehensible reason Oracle distributes
2372 libraries for Solaris in which some of the objects have
2373 bogus sh_link fields. It would be nice if we could just
2374 reject them, but, unfortunately, some people need to use
2375 them. We scan through the section headers; if we find only
2376 one suitable symbol table, we clobber the sh_link to point
2377 to it. I hope this doesn't break anything.
2379 Don't do it on executable nor shared library. */
2380 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2381 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2382 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2388 for (scan = 1; scan < num_sec; scan++)
2390 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2391 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2402 hdr->sh_link = found;
2405 /* Get the symbol table. */
2406 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2407 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2408 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2411 /* If this is an alloc section in an executable or shared
2412 library, or the reloc section does not use the main symbol
2413 table we don't treat it as a reloc section. BFD can't
2414 adequately represent such a section, so at least for now,
2415 we don't try. We just present it as a normal section. We
2416 also can't use it as a reloc section if it points to the
2417 null section, an invalid section, another reloc section, or
2418 its sh_link points to the null section. */
2419 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2420 && (hdr->sh_flags & SHF_ALLOC) != 0)
2421 || hdr->sh_link == SHN_UNDEF
2422 || hdr->sh_link != elf_onesymtab (abfd)
2423 || hdr->sh_info == SHN_UNDEF
2424 || hdr->sh_info >= num_sec
2425 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2426 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2428 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2433 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2436 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2437 if (target_sect == NULL)
2440 esdt = elf_section_data (target_sect);
2441 if (hdr->sh_type == SHT_RELA)
2442 p_hdr = &esdt->rela.hdr;
2444 p_hdr = &esdt->rel.hdr;
2446 /* PR 17512: file: 0b4f81b7. */
2449 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2454 elf_elfsections (abfd)[shindex] = hdr2;
2455 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2456 * bed->s->int_rels_per_ext_rel);
2457 target_sect->flags |= SEC_RELOC;
2458 target_sect->relocation = NULL;
2459 target_sect->rel_filepos = hdr->sh_offset;
2460 /* In the section to which the relocations apply, mark whether
2461 its relocations are of the REL or RELA variety. */
2462 if (hdr->sh_size != 0)
2464 if (hdr->sh_type == SHT_RELA)
2465 target_sect->use_rela_p = 1;
2467 abfd->flags |= HAS_RELOC;
2471 case SHT_GNU_verdef:
2472 elf_dynverdef (abfd) = shindex;
2473 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2474 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2477 case SHT_GNU_versym:
2478 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2481 elf_dynversym (abfd) = shindex;
2482 elf_tdata (abfd)->dynversym_hdr = *hdr;
2483 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2486 case SHT_GNU_verneed:
2487 elf_dynverref (abfd) = shindex;
2488 elf_tdata (abfd)->dynverref_hdr = *hdr;
2489 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2496 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2499 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2505 /* Possibly an attributes section. */
2506 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2507 || hdr->sh_type == bed->obj_attrs_section_type)
2509 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2511 _bfd_elf_parse_attributes (abfd, hdr);
2515 /* Check for any processor-specific section types. */
2516 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2519 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2521 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2522 /* FIXME: How to properly handle allocated section reserved
2523 for applications? */
2525 /* xgettext:c-format */
2526 (_("%pB: unknown type [%#x] section `%s'"),
2527 abfd, hdr->sh_type, name);
2530 /* Allow sections reserved for applications. */
2531 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2536 else if (hdr->sh_type >= SHT_LOPROC
2537 && hdr->sh_type <= SHT_HIPROC)
2538 /* FIXME: We should handle this section. */
2540 /* xgettext:c-format */
2541 (_("%pB: unknown type [%#x] section `%s'"),
2542 abfd, hdr->sh_type, name);
2543 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2545 /* Unrecognised OS-specific sections. */
2546 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2547 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2548 required to correctly process the section and the file should
2549 be rejected with an error message. */
2551 /* xgettext:c-format */
2552 (_("%pB: unknown type [%#x] section `%s'"),
2553 abfd, hdr->sh_type, name);
2556 /* Otherwise it should be processed. */
2557 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2562 /* FIXME: We should handle this section. */
2564 /* xgettext:c-format */
2565 (_("%pB: unknown type [%#x] section `%s'"),
2566 abfd, hdr->sh_type, name);
2574 if (sections_being_created && sections_being_created_abfd == abfd)
2575 sections_being_created [shindex] = FALSE;
2576 if (-- nesting == 0)
2578 sections_being_created = NULL;
2579 sections_being_created_abfd = abfd;
2584 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2587 bfd_sym_from_r_symndx (struct sym_cache *cache,
2589 unsigned long r_symndx)
2591 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2593 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2595 Elf_Internal_Shdr *symtab_hdr;
2596 unsigned char esym[sizeof (Elf64_External_Sym)];
2597 Elf_External_Sym_Shndx eshndx;
2599 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2600 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2601 &cache->sym[ent], esym, &eshndx) == NULL)
2604 if (cache->abfd != abfd)
2606 memset (cache->indx, -1, sizeof (cache->indx));
2609 cache->indx[ent] = r_symndx;
2612 return &cache->sym[ent];
2615 /* Given an ELF section number, retrieve the corresponding BFD
2619 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2621 if (sec_index >= elf_numsections (abfd))
2623 return elf_elfsections (abfd)[sec_index]->bfd_section;
2626 static const struct bfd_elf_special_section special_sections_b[] =
2628 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2629 { NULL, 0, 0, 0, 0 }
2632 static const struct bfd_elf_special_section special_sections_c[] =
2634 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2635 { NULL, 0, 0, 0, 0 }
2638 static const struct bfd_elf_special_section special_sections_d[] =
2640 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2641 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2642 /* There are more DWARF sections than these, but they needn't be added here
2643 unless you have to cope with broken compilers that don't emit section
2644 attributes or you want to help the user writing assembler. */
2645 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2646 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2647 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2648 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2649 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2650 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2651 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2652 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2653 { NULL, 0, 0, 0, 0 }
2656 static const struct bfd_elf_special_section special_sections_f[] =
2658 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2659 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2660 { NULL, 0 , 0, 0, 0 }
2663 static const struct bfd_elf_special_section special_sections_g[] =
2665 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2666 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2667 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2668 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2669 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2670 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2671 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2672 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2673 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2674 { NULL, 0, 0, 0, 0 }
2677 static const struct bfd_elf_special_section special_sections_h[] =
2679 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2680 { NULL, 0, 0, 0, 0 }
2683 static const struct bfd_elf_special_section special_sections_i[] =
2685 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2686 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2687 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2688 { NULL, 0, 0, 0, 0 }
2691 static const struct bfd_elf_special_section special_sections_l[] =
2693 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2694 { NULL, 0, 0, 0, 0 }
2697 static const struct bfd_elf_special_section special_sections_n[] =
2699 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2700 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2701 { NULL, 0, 0, 0, 0 }
2704 static const struct bfd_elf_special_section special_sections_p[] =
2706 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2707 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2708 { NULL, 0, 0, 0, 0 }
2711 static const struct bfd_elf_special_section special_sections_r[] =
2713 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2714 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2715 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2716 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2717 { NULL, 0, 0, 0, 0 }
2720 static const struct bfd_elf_special_section special_sections_s[] =
2722 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2723 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2724 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2725 /* See struct bfd_elf_special_section declaration for the semantics of
2726 this special case where .prefix_length != strlen (.prefix). */
2727 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2728 { NULL, 0, 0, 0, 0 }
2731 static const struct bfd_elf_special_section special_sections_t[] =
2733 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2734 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2735 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2736 { NULL, 0, 0, 0, 0 }
2739 static const struct bfd_elf_special_section special_sections_z[] =
2741 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2742 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2743 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2744 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2745 { NULL, 0, 0, 0, 0 }
2748 static const struct bfd_elf_special_section * const special_sections[] =
2750 special_sections_b, /* 'b' */
2751 special_sections_c, /* 'c' */
2752 special_sections_d, /* 'd' */
2754 special_sections_f, /* 'f' */
2755 special_sections_g, /* 'g' */
2756 special_sections_h, /* 'h' */
2757 special_sections_i, /* 'i' */
2760 special_sections_l, /* 'l' */
2762 special_sections_n, /* 'n' */
2764 special_sections_p, /* 'p' */
2766 special_sections_r, /* 'r' */
2767 special_sections_s, /* 's' */
2768 special_sections_t, /* 't' */
2774 special_sections_z /* 'z' */
2777 const struct bfd_elf_special_section *
2778 _bfd_elf_get_special_section (const char *name,
2779 const struct bfd_elf_special_section *spec,
2785 len = strlen (name);
2787 for (i = 0; spec[i].prefix != NULL; i++)
2790 int prefix_len = spec[i].prefix_length;
2792 if (len < prefix_len)
2794 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2797 suffix_len = spec[i].suffix_length;
2798 if (suffix_len <= 0)
2800 if (name[prefix_len] != 0)
2802 if (suffix_len == 0)
2804 if (name[prefix_len] != '.'
2805 && (suffix_len == -2
2806 || (rela && spec[i].type == SHT_REL)))
2812 if (len < prefix_len + suffix_len)
2814 if (memcmp (name + len - suffix_len,
2815 spec[i].prefix + prefix_len,
2825 const struct bfd_elf_special_section *
2826 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2829 const struct bfd_elf_special_section *spec;
2830 const struct elf_backend_data *bed;
2832 /* See if this is one of the special sections. */
2833 if (sec->name == NULL)
2836 bed = get_elf_backend_data (abfd);
2837 spec = bed->special_sections;
2840 spec = _bfd_elf_get_special_section (sec->name,
2841 bed->special_sections,
2847 if (sec->name[0] != '.')
2850 i = sec->name[1] - 'b';
2851 if (i < 0 || i > 'z' - 'b')
2854 spec = special_sections[i];
2859 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2863 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2865 struct bfd_elf_section_data *sdata;
2866 const struct elf_backend_data *bed;
2867 const struct bfd_elf_special_section *ssect;
2869 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2872 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2876 sec->used_by_bfd = sdata;
2879 /* Indicate whether or not this section should use RELA relocations. */
2880 bed = get_elf_backend_data (abfd);
2881 sec->use_rela_p = bed->default_use_rela_p;
2883 /* When we read a file, we don't need to set ELF section type and
2884 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2885 anyway. We will set ELF section type and flags for all linker
2886 created sections. If user specifies BFD section flags, we will
2887 set ELF section type and flags based on BFD section flags in
2888 elf_fake_sections. Special handling for .init_array/.fini_array
2889 output sections since they may contain .ctors/.dtors input
2890 sections. We don't want _bfd_elf_init_private_section_data to
2891 copy ELF section type from .ctors/.dtors input sections. */
2892 if (abfd->direction != read_direction
2893 || (sec->flags & SEC_LINKER_CREATED) != 0)
2895 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2898 || (sec->flags & SEC_LINKER_CREATED) != 0
2899 || ssect->type == SHT_INIT_ARRAY
2900 || ssect->type == SHT_FINI_ARRAY))
2902 elf_section_type (sec) = ssect->type;
2903 elf_section_flags (sec) = ssect->attr;
2907 return _bfd_generic_new_section_hook (abfd, sec);
2910 /* Create a new bfd section from an ELF program header.
2912 Since program segments have no names, we generate a synthetic name
2913 of the form segment<NUM>, where NUM is generally the index in the
2914 program header table. For segments that are split (see below) we
2915 generate the names segment<NUM>a and segment<NUM>b.
2917 Note that some program segments may have a file size that is different than
2918 (less than) the memory size. All this means is that at execution the
2919 system must allocate the amount of memory specified by the memory size,
2920 but only initialize it with the first "file size" bytes read from the
2921 file. This would occur for example, with program segments consisting
2922 of combined data+bss.
2924 To handle the above situation, this routine generates TWO bfd sections
2925 for the single program segment. The first has the length specified by
2926 the file size of the segment, and the second has the length specified
2927 by the difference between the two sizes. In effect, the segment is split
2928 into its initialized and uninitialized parts.
2933 _bfd_elf_make_section_from_phdr (bfd *abfd,
2934 Elf_Internal_Phdr *hdr,
2936 const char *type_name)
2944 split = ((hdr->p_memsz > 0)
2945 && (hdr->p_filesz > 0)
2946 && (hdr->p_memsz > hdr->p_filesz));
2948 if (hdr->p_filesz > 0)
2950 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2951 len = strlen (namebuf) + 1;
2952 name = (char *) bfd_alloc (abfd, len);
2955 memcpy (name, namebuf, len);
2956 newsect = bfd_make_section (abfd, name);
2957 if (newsect == NULL)
2959 newsect->vma = hdr->p_vaddr;
2960 newsect->lma = hdr->p_paddr;
2961 newsect->size = hdr->p_filesz;
2962 newsect->filepos = hdr->p_offset;
2963 newsect->flags |= SEC_HAS_CONTENTS;
2964 newsect->alignment_power = bfd_log2 (hdr->p_align);
2965 if (hdr->p_type == PT_LOAD)
2967 newsect->flags |= SEC_ALLOC;
2968 newsect->flags |= SEC_LOAD;
2969 if (hdr->p_flags & PF_X)
2971 /* FIXME: all we known is that it has execute PERMISSION,
2973 newsect->flags |= SEC_CODE;
2976 if (!(hdr->p_flags & PF_W))
2978 newsect->flags |= SEC_READONLY;
2982 if (hdr->p_memsz > hdr->p_filesz)
2986 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2987 len = strlen (namebuf) + 1;
2988 name = (char *) bfd_alloc (abfd, len);
2991 memcpy (name, namebuf, len);
2992 newsect = bfd_make_section (abfd, name);
2993 if (newsect == NULL)
2995 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2996 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2997 newsect->size = hdr->p_memsz - hdr->p_filesz;
2998 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2999 align = newsect->vma & -newsect->vma;
3000 if (align == 0 || align > hdr->p_align)
3001 align = hdr->p_align;
3002 newsect->alignment_power = bfd_log2 (align);
3003 if (hdr->p_type == PT_LOAD)
3005 /* Hack for gdb. Segments that have not been modified do
3006 not have their contents written to a core file, on the
3007 assumption that a debugger can find the contents in the
3008 executable. We flag this case by setting the fake
3009 section size to zero. Note that "real" bss sections will
3010 always have their contents dumped to the core file. */
3011 if (bfd_get_format (abfd) == bfd_core)
3013 newsect->flags |= SEC_ALLOC;
3014 if (hdr->p_flags & PF_X)
3015 newsect->flags |= SEC_CODE;
3017 if (!(hdr->p_flags & PF_W))
3018 newsect->flags |= SEC_READONLY;
3025 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3027 const struct elf_backend_data *bed;
3029 switch (hdr->p_type)
3032 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3035 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
3038 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3041 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3044 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3046 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3052 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3055 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3057 case PT_GNU_EH_FRAME:
3058 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3062 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3065 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3068 /* Check for any processor-specific program segment types. */
3069 bed = get_elf_backend_data (abfd);
3070 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3074 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3078 _bfd_elf_single_rel_hdr (asection *sec)
3080 if (elf_section_data (sec)->rel.hdr)
3082 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3083 return elf_section_data (sec)->rel.hdr;
3086 return elf_section_data (sec)->rela.hdr;
3090 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3091 Elf_Internal_Shdr *rel_hdr,
3092 const char *sec_name,
3093 bfd_boolean use_rela_p)
3095 char *name = (char *) bfd_alloc (abfd,
3096 sizeof ".rela" + strlen (sec_name));
3100 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3102 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3104 if (rel_hdr->sh_name == (unsigned int) -1)
3110 /* Allocate and initialize a section-header for a new reloc section,
3111 containing relocations against ASECT. It is stored in RELDATA. If
3112 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3116 _bfd_elf_init_reloc_shdr (bfd *abfd,
3117 struct bfd_elf_section_reloc_data *reldata,
3118 const char *sec_name,
3119 bfd_boolean use_rela_p,
3120 bfd_boolean delay_st_name_p)
3122 Elf_Internal_Shdr *rel_hdr;
3123 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3125 BFD_ASSERT (reldata->hdr == NULL);
3126 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3127 reldata->hdr = rel_hdr;
3129 if (delay_st_name_p)
3130 rel_hdr->sh_name = (unsigned int) -1;
3131 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3134 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3135 rel_hdr->sh_entsize = (use_rela_p
3136 ? bed->s->sizeof_rela
3137 : bed->s->sizeof_rel);
3138 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3139 rel_hdr->sh_flags = 0;
3140 rel_hdr->sh_addr = 0;
3141 rel_hdr->sh_size = 0;
3142 rel_hdr->sh_offset = 0;
3147 /* Return the default section type based on the passed in section flags. */
3150 bfd_elf_get_default_section_type (flagword flags)
3152 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3153 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3155 return SHT_PROGBITS;
3158 struct fake_section_arg
3160 struct bfd_link_info *link_info;
3164 /* Set up an ELF internal section header for a section. */
3167 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3169 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3170 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3171 struct bfd_elf_section_data *esd = elf_section_data (asect);
3172 Elf_Internal_Shdr *this_hdr;
3173 unsigned int sh_type;
3174 const char *name = asect->name;
3175 bfd_boolean delay_st_name_p = FALSE;
3179 /* We already failed; just get out of the bfd_map_over_sections
3184 this_hdr = &esd->this_hdr;
3188 /* ld: compress DWARF debug sections with names: .debug_*. */
3189 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3190 && (asect->flags & SEC_DEBUGGING)
3194 /* Set SEC_ELF_COMPRESS to indicate this section should be
3196 asect->flags |= SEC_ELF_COMPRESS;
3198 /* If this section will be compressed, delay adding section
3199 name to section name section after it is compressed in
3200 _bfd_elf_assign_file_positions_for_non_load. */
3201 delay_st_name_p = TRUE;
3204 else if ((asect->flags & SEC_ELF_RENAME))
3206 /* objcopy: rename output DWARF debug section. */
3207 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3209 /* When we decompress or compress with SHF_COMPRESSED,
3210 convert section name from .zdebug_* to .debug_* if
3214 char *new_name = convert_zdebug_to_debug (abfd, name);
3215 if (new_name == NULL)
3223 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3225 /* PR binutils/18087: Compression does not always make a
3226 section smaller. So only rename the section when
3227 compression has actually taken place. If input section
3228 name is .zdebug_*, we should never compress it again. */
3229 char *new_name = convert_debug_to_zdebug (abfd, name);
3230 if (new_name == NULL)
3235 BFD_ASSERT (name[1] != 'z');
3240 if (delay_st_name_p)
3241 this_hdr->sh_name = (unsigned int) -1;
3245 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3247 if (this_hdr->sh_name == (unsigned int) -1)
3254 /* Don't clear sh_flags. Assembler may set additional bits. */
3256 if ((asect->flags & SEC_ALLOC) != 0
3257 || asect->user_set_vma)
3258 this_hdr->sh_addr = asect->vma;
3260 this_hdr->sh_addr = 0;
3262 this_hdr->sh_offset = 0;
3263 this_hdr->sh_size = asect->size;
3264 this_hdr->sh_link = 0;
3265 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3266 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3269 /* xgettext:c-format */
3270 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3271 abfd, asect->alignment_power, asect);
3275 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
3276 /* The sh_entsize and sh_info fields may have been set already by
3277 copy_private_section_data. */
3279 this_hdr->bfd_section = asect;
3280 this_hdr->contents = NULL;
3282 /* If the section type is unspecified, we set it based on
3284 if ((asect->flags & SEC_GROUP) != 0)
3285 sh_type = SHT_GROUP;
3287 sh_type = bfd_elf_get_default_section_type (asect->flags);
3289 if (this_hdr->sh_type == SHT_NULL)
3290 this_hdr->sh_type = sh_type;
3291 else if (this_hdr->sh_type == SHT_NOBITS
3292 && sh_type == SHT_PROGBITS
3293 && (asect->flags & SEC_ALLOC) != 0)
3295 /* Warn if we are changing a NOBITS section to PROGBITS, but
3296 allow the link to proceed. This can happen when users link
3297 non-bss input sections to bss output sections, or emit data
3298 to a bss output section via a linker script. */
3300 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3301 this_hdr->sh_type = sh_type;
3304 switch (this_hdr->sh_type)
3315 case SHT_INIT_ARRAY:
3316 case SHT_FINI_ARRAY:
3317 case SHT_PREINIT_ARRAY:
3318 this_hdr->sh_entsize = bed->s->arch_size / 8;
3322 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3326 this_hdr->sh_entsize = bed->s->sizeof_sym;
3330 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3334 if (get_elf_backend_data (abfd)->may_use_rela_p)
3335 this_hdr->sh_entsize = bed->s->sizeof_rela;
3339 if (get_elf_backend_data (abfd)->may_use_rel_p)
3340 this_hdr->sh_entsize = bed->s->sizeof_rel;
3343 case SHT_GNU_versym:
3344 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3347 case SHT_GNU_verdef:
3348 this_hdr->sh_entsize = 0;
3349 /* objcopy or strip will copy over sh_info, but may not set
3350 cverdefs. The linker will set cverdefs, but sh_info will be
3352 if (this_hdr->sh_info == 0)
3353 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3355 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3356 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3359 case SHT_GNU_verneed:
3360 this_hdr->sh_entsize = 0;
3361 /* objcopy or strip will copy over sh_info, but may not set
3362 cverrefs. The linker will set cverrefs, but sh_info will be
3364 if (this_hdr->sh_info == 0)
3365 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3367 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3368 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3372 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3376 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3380 if ((asect->flags & SEC_ALLOC) != 0)
3381 this_hdr->sh_flags |= SHF_ALLOC;
3382 if ((asect->flags & SEC_READONLY) == 0)
3383 this_hdr->sh_flags |= SHF_WRITE;
3384 if ((asect->flags & SEC_CODE) != 0)
3385 this_hdr->sh_flags |= SHF_EXECINSTR;
3386 if ((asect->flags & SEC_MERGE) != 0)
3388 this_hdr->sh_flags |= SHF_MERGE;
3389 this_hdr->sh_entsize = asect->entsize;
3391 if ((asect->flags & SEC_STRINGS) != 0)
3392 this_hdr->sh_flags |= SHF_STRINGS;
3393 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3394 this_hdr->sh_flags |= SHF_GROUP;
3395 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3397 this_hdr->sh_flags |= SHF_TLS;
3398 if (asect->size == 0
3399 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3401 struct bfd_link_order *o = asect->map_tail.link_order;
3403 this_hdr->sh_size = 0;
3406 this_hdr->sh_size = o->offset + o->size;
3407 if (this_hdr->sh_size != 0)
3408 this_hdr->sh_type = SHT_NOBITS;
3412 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3413 this_hdr->sh_flags |= SHF_EXCLUDE;
3415 /* If the section has relocs, set up a section header for the
3416 SHT_REL[A] section. If two relocation sections are required for
3417 this section, it is up to the processor-specific back-end to
3418 create the other. */
3419 if ((asect->flags & SEC_RELOC) != 0)
3421 /* When doing a relocatable link, create both REL and RELA sections if
3424 /* Do the normal setup if we wouldn't create any sections here. */
3425 && esd->rel.count + esd->rela.count > 0
3426 && (bfd_link_relocatable (arg->link_info)
3427 || arg->link_info->emitrelocations))
3429 if (esd->rel.count && esd->rel.hdr == NULL
3430 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3431 FALSE, delay_st_name_p))
3436 if (esd->rela.count && esd->rela.hdr == NULL
3437 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3438 TRUE, delay_st_name_p))
3444 else if (!_bfd_elf_init_reloc_shdr (abfd,
3446 ? &esd->rela : &esd->rel),
3456 /* Check for processor-specific section types. */
3457 sh_type = this_hdr->sh_type;
3458 if (bed->elf_backend_fake_sections
3459 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3465 if (sh_type == SHT_NOBITS && asect->size != 0)
3467 /* Don't change the header type from NOBITS if we are being
3468 called for objcopy --only-keep-debug. */
3469 this_hdr->sh_type = sh_type;
3473 /* Fill in the contents of a SHT_GROUP section. Called from
3474 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3475 when ELF targets use the generic linker, ld. Called for ld -r
3476 from bfd_elf_final_link. */
3479 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3481 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3482 asection *elt, *first;
3486 /* Ignore linker created group section. See elfNN_ia64_object_p in
3488 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3493 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3495 unsigned long symindx = 0;
3497 /* elf_group_id will have been set up by objcopy and the
3499 if (elf_group_id (sec) != NULL)
3500 symindx = elf_group_id (sec)->udata.i;
3504 /* If called from the assembler, swap_out_syms will have set up
3505 elf_section_syms. */
3506 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3507 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3509 elf_section_data (sec)->this_hdr.sh_info = symindx;
3511 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3513 /* The ELF backend linker sets sh_info to -2 when the group
3514 signature symbol is global, and thus the index can't be
3515 set until all local symbols are output. */
3517 struct bfd_elf_section_data *sec_data;
3518 unsigned long symndx;
3519 unsigned long extsymoff;
3520 struct elf_link_hash_entry *h;
3522 /* The point of this little dance to the first SHF_GROUP section
3523 then back to the SHT_GROUP section is that this gets us to
3524 the SHT_GROUP in the input object. */
3525 igroup = elf_sec_group (elf_next_in_group (sec));
3526 sec_data = elf_section_data (igroup);
3527 symndx = sec_data->this_hdr.sh_info;
3529 if (!elf_bad_symtab (igroup->owner))
3531 Elf_Internal_Shdr *symtab_hdr;
3533 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3534 extsymoff = symtab_hdr->sh_info;
3536 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3537 while (h->root.type == bfd_link_hash_indirect
3538 || h->root.type == bfd_link_hash_warning)
3539 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3541 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3544 /* The contents won't be allocated for "ld -r" or objcopy. */
3546 if (sec->contents == NULL)
3549 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3551 /* Arrange for the section to be written out. */
3552 elf_section_data (sec)->this_hdr.contents = sec->contents;
3553 if (sec->contents == NULL)
3560 loc = sec->contents + sec->size;
3562 /* Get the pointer to the first section in the group that gas
3563 squirreled away here. objcopy arranges for this to be set to the
3564 start of the input section group. */
3565 first = elt = elf_next_in_group (sec);
3567 /* First element is a flag word. Rest of section is elf section
3568 indices for all the sections of the group. Write them backwards
3569 just to keep the group in the same order as given in .section
3570 directives, not that it matters. */
3577 s = s->output_section;
3579 && !bfd_is_abs_section (s))
3581 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3582 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3584 if (elf_sec->rel.hdr != NULL
3586 || (input_elf_sec->rel.hdr != NULL
3587 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3589 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3591 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3593 if (elf_sec->rela.hdr != NULL
3595 || (input_elf_sec->rela.hdr != NULL
3596 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3598 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3600 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3603 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3605 elt = elf_next_in_group (elt);
3611 BFD_ASSERT (loc == sec->contents);
3613 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3616 /* Given NAME, the name of a relocation section stripped of its
3617 .rel/.rela prefix, return the section in ABFD to which the
3618 relocations apply. */
3621 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3623 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3624 section likely apply to .got.plt or .got section. */
3625 if (get_elf_backend_data (abfd)->want_got_plt
3626 && strcmp (name, ".plt") == 0)
3631 sec = bfd_get_section_by_name (abfd, name);
3637 return bfd_get_section_by_name (abfd, name);
3640 /* Return the section to which RELOC_SEC applies. */
3643 elf_get_reloc_section (asection *reloc_sec)
3648 const struct elf_backend_data *bed;
3650 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3651 if (type != SHT_REL && type != SHT_RELA)
3654 /* We look up the section the relocs apply to by name. */
3655 name = reloc_sec->name;
3656 if (strncmp (name, ".rel", 4) != 0)
3659 if (type == SHT_RELA && *name++ != 'a')
3662 abfd = reloc_sec->owner;
3663 bed = get_elf_backend_data (abfd);
3664 return bed->get_reloc_section (abfd, name);
3667 /* Assign all ELF section numbers. The dummy first section is handled here
3668 too. The link/info pointers for the standard section types are filled
3669 in here too, while we're at it. */
3672 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3674 struct elf_obj_tdata *t = elf_tdata (abfd);
3676 unsigned int section_number;
3677 Elf_Internal_Shdr **i_shdrp;
3678 struct bfd_elf_section_data *d;
3679 bfd_boolean need_symtab;
3683 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3685 /* SHT_GROUP sections are in relocatable files only. */
3686 if (link_info == NULL || !link_info->resolve_section_groups)
3688 size_t reloc_count = 0;
3690 /* Put SHT_GROUP sections first. */
3691 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3693 d = elf_section_data (sec);
3695 if (d->this_hdr.sh_type == SHT_GROUP)
3697 if (sec->flags & SEC_LINKER_CREATED)
3699 /* Remove the linker created SHT_GROUP sections. */
3700 bfd_section_list_remove (abfd, sec);
3701 abfd->section_count--;
3704 d->this_idx = section_number++;
3707 /* Count relocations. */
3708 reloc_count += sec->reloc_count;
3711 /* Clear HAS_RELOC if there are no relocations. */
3712 if (reloc_count == 0)
3713 abfd->flags &= ~HAS_RELOC;
3716 for (sec = abfd->sections; sec; sec = sec->next)
3718 d = elf_section_data (sec);
3720 if (d->this_hdr.sh_type != SHT_GROUP)
3721 d->this_idx = section_number++;
3722 if (d->this_hdr.sh_name != (unsigned int) -1)
3723 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3726 d->rel.idx = section_number++;
3727 if (d->rel.hdr->sh_name != (unsigned int) -1)
3728 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3735 d->rela.idx = section_number++;
3736 if (d->rela.hdr->sh_name != (unsigned int) -1)
3737 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3743 need_symtab = (bfd_get_symcount (abfd) > 0
3744 || (link_info == NULL
3745 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3749 elf_onesymtab (abfd) = section_number++;
3750 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3751 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3753 elf_section_list *entry;
3755 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3757 entry = bfd_zalloc (abfd, sizeof (*entry));
3758 entry->ndx = section_number++;
3759 elf_symtab_shndx_list (abfd) = entry;
3761 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3762 ".symtab_shndx", FALSE);
3763 if (entry->hdr.sh_name == (unsigned int) -1)
3766 elf_strtab_sec (abfd) = section_number++;
3767 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3770 elf_shstrtab_sec (abfd) = section_number++;
3771 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3772 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3774 if (section_number >= SHN_LORESERVE)
3776 /* xgettext:c-format */
3777 _bfd_error_handler (_("%pB: too many sections: %u"),
3778 abfd, section_number);
3782 elf_numsections (abfd) = section_number;
3783 elf_elfheader (abfd)->e_shnum = section_number;
3785 /* Set up the list of section header pointers, in agreement with the
3787 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3788 sizeof (Elf_Internal_Shdr *));
3789 if (i_shdrp == NULL)
3792 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3793 sizeof (Elf_Internal_Shdr));
3794 if (i_shdrp[0] == NULL)
3796 bfd_release (abfd, i_shdrp);
3800 elf_elfsections (abfd) = i_shdrp;
3802 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3805 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3806 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3808 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3809 BFD_ASSERT (entry != NULL);
3810 i_shdrp[entry->ndx] = & entry->hdr;
3811 entry->hdr.sh_link = elf_onesymtab (abfd);
3813 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3814 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3817 for (sec = abfd->sections; sec; sec = sec->next)
3821 d = elf_section_data (sec);
3823 i_shdrp[d->this_idx] = &d->this_hdr;
3824 if (d->rel.idx != 0)
3825 i_shdrp[d->rel.idx] = d->rel.hdr;
3826 if (d->rela.idx != 0)
3827 i_shdrp[d->rela.idx] = d->rela.hdr;
3829 /* Fill in the sh_link and sh_info fields while we're at it. */
3831 /* sh_link of a reloc section is the section index of the symbol
3832 table. sh_info is the section index of the section to which
3833 the relocation entries apply. */
3834 if (d->rel.idx != 0)
3836 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3837 d->rel.hdr->sh_info = d->this_idx;
3838 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3840 if (d->rela.idx != 0)
3842 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3843 d->rela.hdr->sh_info = d->this_idx;
3844 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3847 /* We need to set up sh_link for SHF_LINK_ORDER. */
3848 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3850 s = elf_linked_to_section (sec);
3853 /* elf_linked_to_section points to the input section. */
3854 if (link_info != NULL)
3856 /* Check discarded linkonce section. */
3857 if (discarded_section (s))
3861 /* xgettext:c-format */
3862 (_("%pB: sh_link of section `%pA' points to"
3863 " discarded section `%pA' of `%pB'"),
3864 abfd, d->this_hdr.bfd_section,
3866 /* Point to the kept section if it has the same
3867 size as the discarded one. */
3868 kept = _bfd_elf_check_kept_section (s, link_info);
3871 bfd_set_error (bfd_error_bad_value);
3877 s = s->output_section;
3878 BFD_ASSERT (s != NULL);
3882 /* Handle objcopy. */
3883 if (s->output_section == NULL)
3886 /* xgettext:c-format */
3887 (_("%pB: sh_link of section `%pA' points to"
3888 " removed section `%pA' of `%pB'"),
3889 abfd, d->this_hdr.bfd_section, s, s->owner);
3890 bfd_set_error (bfd_error_bad_value);
3893 s = s->output_section;
3895 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3900 The Intel C compiler generates SHT_IA_64_UNWIND with
3901 SHF_LINK_ORDER. But it doesn't set the sh_link or
3902 sh_info fields. Hence we could get the situation
3904 const struct elf_backend_data *bed
3905 = get_elf_backend_data (abfd);
3906 if (bed->link_order_error_handler)
3907 bed->link_order_error_handler
3908 /* xgettext:c-format */
3909 (_("%pB: warning: sh_link not set for section `%pA'"),
3914 switch (d->this_hdr.sh_type)
3918 /* A reloc section which we are treating as a normal BFD
3919 section. sh_link is the section index of the symbol
3920 table. sh_info is the section index of the section to
3921 which the relocation entries apply. We assume that an
3922 allocated reloc section uses the dynamic symbol table.
3923 FIXME: How can we be sure? */
3924 s = bfd_get_section_by_name (abfd, ".dynsym");
3926 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3928 s = elf_get_reloc_section (sec);
3931 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3932 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3937 /* We assume that a section named .stab*str is a stabs
3938 string section. We look for a section with the same name
3939 but without the trailing ``str'', and set its sh_link
3940 field to point to this section. */
3941 if (CONST_STRNEQ (sec->name, ".stab")
3942 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3947 len = strlen (sec->name);
3948 alc = (char *) bfd_malloc (len - 2);
3951 memcpy (alc, sec->name, len - 3);
3952 alc[len - 3] = '\0';
3953 s = bfd_get_section_by_name (abfd, alc);
3957 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3959 /* This is a .stab section. */
3960 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3961 elf_section_data (s)->this_hdr.sh_entsize
3962 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3969 case SHT_GNU_verneed:
3970 case SHT_GNU_verdef:
3971 /* sh_link is the section header index of the string table
3972 used for the dynamic entries, or the symbol table, or the
3974 s = bfd_get_section_by_name (abfd, ".dynstr");
3976 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3979 case SHT_GNU_LIBLIST:
3980 /* sh_link is the section header index of the prelink library
3981 list used for the dynamic entries, or the symbol table, or
3982 the version strings. */
3983 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3984 ? ".dynstr" : ".gnu.libstr");
3986 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3991 case SHT_GNU_versym:
3992 /* sh_link is the section header index of the symbol table
3993 this hash table or version table is for. */
3994 s = bfd_get_section_by_name (abfd, ".dynsym");
3996 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4000 d->this_hdr.sh_link = elf_onesymtab (abfd);
4004 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4005 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4006 debug section name from .debug_* to .zdebug_* if needed. */
4012 sym_is_global (bfd *abfd, asymbol *sym)
4014 /* If the backend has a special mapping, use it. */
4015 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4016 if (bed->elf_backend_sym_is_global)
4017 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4019 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4020 || bfd_is_und_section (bfd_get_section (sym))
4021 || bfd_is_com_section (bfd_get_section (sym)));
4024 /* Filter global symbols of ABFD to include in the import library. All
4025 SYMCOUNT symbols of ABFD can be examined from their pointers in
4026 SYMS. Pointers of symbols to keep should be stored contiguously at
4027 the beginning of that array.
4029 Returns the number of symbols to keep. */
4032 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4033 asymbol **syms, long symcount)
4035 long src_count, dst_count = 0;
4037 for (src_count = 0; src_count < symcount; src_count++)
4039 asymbol *sym = syms[src_count];
4040 char *name = (char *) bfd_asymbol_name (sym);
4041 struct bfd_link_hash_entry *h;
4043 if (!sym_is_global (abfd, sym))
4046 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
4049 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4051 if (h->linker_def || h->ldscript_def)
4054 syms[dst_count++] = sym;
4057 syms[dst_count] = NULL;
4062 /* Don't output section symbols for sections that are not going to be
4063 output, that are duplicates or there is no BFD section. */
4066 ignore_section_sym (bfd *abfd, asymbol *sym)
4068 elf_symbol_type *type_ptr;
4073 if ((sym->flags & BSF_SECTION_SYM) == 0)
4076 if (sym->section == NULL)
4079 type_ptr = elf_symbol_from (abfd, sym);
4080 return ((type_ptr != NULL
4081 && type_ptr->internal_elf_sym.st_shndx != 0
4082 && bfd_is_abs_section (sym->section))
4083 || !(sym->section->owner == abfd
4084 || (sym->section->output_section != NULL
4085 && sym->section->output_section->owner == abfd
4086 && sym->section->output_offset == 0)
4087 || bfd_is_abs_section (sym->section)));
4090 /* Map symbol from it's internal number to the external number, moving
4091 all local symbols to be at the head of the list. */
4094 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4096 unsigned int symcount = bfd_get_symcount (abfd);
4097 asymbol **syms = bfd_get_outsymbols (abfd);
4098 asymbol **sect_syms;
4099 unsigned int num_locals = 0;
4100 unsigned int num_globals = 0;
4101 unsigned int num_locals2 = 0;
4102 unsigned int num_globals2 = 0;
4103 unsigned int max_index = 0;
4109 fprintf (stderr, "elf_map_symbols\n");
4113 for (asect = abfd->sections; asect; asect = asect->next)
4115 if (max_index < asect->index)
4116 max_index = asect->index;
4120 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
4121 if (sect_syms == NULL)
4123 elf_section_syms (abfd) = sect_syms;
4124 elf_num_section_syms (abfd) = max_index;
4126 /* Init sect_syms entries for any section symbols we have already
4127 decided to output. */
4128 for (idx = 0; idx < symcount; idx++)
4130 asymbol *sym = syms[idx];
4132 if ((sym->flags & BSF_SECTION_SYM) != 0
4134 && !ignore_section_sym (abfd, sym)
4135 && !bfd_is_abs_section (sym->section))
4137 asection *sec = sym->section;
4139 if (sec->owner != abfd)
4140 sec = sec->output_section;
4142 sect_syms[sec->index] = syms[idx];
4146 /* Classify all of the symbols. */
4147 for (idx = 0; idx < symcount; idx++)
4149 if (sym_is_global (abfd, syms[idx]))
4151 else if (!ignore_section_sym (abfd, syms[idx]))
4155 /* We will be adding a section symbol for each normal BFD section. Most
4156 sections will already have a section symbol in outsymbols, but
4157 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4158 at least in that case. */
4159 for (asect = abfd->sections; asect; asect = asect->next)
4161 if (sect_syms[asect->index] == NULL)
4163 if (!sym_is_global (abfd, asect->symbol))
4170 /* Now sort the symbols so the local symbols are first. */
4171 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
4172 sizeof (asymbol *));
4174 if (new_syms == NULL)
4177 for (idx = 0; idx < symcount; idx++)
4179 asymbol *sym = syms[idx];
4182 if (sym_is_global (abfd, sym))
4183 i = num_locals + num_globals2++;
4184 else if (!ignore_section_sym (abfd, sym))
4189 sym->udata.i = i + 1;
4191 for (asect = abfd->sections; asect; asect = asect->next)
4193 if (sect_syms[asect->index] == NULL)
4195 asymbol *sym = asect->symbol;
4198 sect_syms[asect->index] = sym;
4199 if (!sym_is_global (abfd, sym))
4202 i = num_locals + num_globals2++;
4204 sym->udata.i = i + 1;
4208 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4210 *pnum_locals = num_locals;
4214 /* Align to the maximum file alignment that could be required for any
4215 ELF data structure. */
4217 static inline file_ptr
4218 align_file_position (file_ptr off, int align)
4220 return (off + align - 1) & ~(align - 1);
4223 /* Assign a file position to a section, optionally aligning to the
4224 required section alignment. */
4227 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4231 if (align && i_shdrp->sh_addralign > 1)
4232 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4233 i_shdrp->sh_offset = offset;
4234 if (i_shdrp->bfd_section != NULL)
4235 i_shdrp->bfd_section->filepos = offset;
4236 if (i_shdrp->sh_type != SHT_NOBITS)
4237 offset += i_shdrp->sh_size;
4241 /* Compute the file positions we are going to put the sections at, and
4242 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4243 is not NULL, this is being called by the ELF backend linker. */
4246 _bfd_elf_compute_section_file_positions (bfd *abfd,
4247 struct bfd_link_info *link_info)
4249 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4250 struct fake_section_arg fsargs;
4252 struct elf_strtab_hash *strtab = NULL;
4253 Elf_Internal_Shdr *shstrtab_hdr;
4254 bfd_boolean need_symtab;
4256 if (abfd->output_has_begun)
4259 /* Do any elf backend specific processing first. */
4260 if (bed->elf_backend_begin_write_processing)
4261 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4263 if (! prep_headers (abfd))
4266 /* Post process the headers if necessary. */
4267 (*bed->elf_backend_post_process_headers) (abfd, link_info);
4269 fsargs.failed = FALSE;
4270 fsargs.link_info = link_info;
4271 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4275 if (!assign_section_numbers (abfd, link_info))
4278 /* The backend linker builds symbol table information itself. */
4279 need_symtab = (link_info == NULL
4280 && (bfd_get_symcount (abfd) > 0
4281 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4285 /* Non-zero if doing a relocatable link. */
4286 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4288 if (! swap_out_syms (abfd, &strtab, relocatable_p))
4293 if (link_info == NULL)
4295 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4300 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4301 /* sh_name was set in prep_headers. */
4302 shstrtab_hdr->sh_type = SHT_STRTAB;
4303 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4304 shstrtab_hdr->sh_addr = 0;
4305 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4306 shstrtab_hdr->sh_entsize = 0;
4307 shstrtab_hdr->sh_link = 0;
4308 shstrtab_hdr->sh_info = 0;
4309 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4310 shstrtab_hdr->sh_addralign = 1;
4312 if (!assign_file_positions_except_relocs (abfd, link_info))
4318 Elf_Internal_Shdr *hdr;
4320 off = elf_next_file_pos (abfd);
4322 hdr = & elf_symtab_hdr (abfd);
4323 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4325 if (elf_symtab_shndx_list (abfd) != NULL)
4327 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4328 if (hdr->sh_size != 0)
4329 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4330 /* FIXME: What about other symtab_shndx sections in the list ? */
4333 hdr = &elf_tdata (abfd)->strtab_hdr;
4334 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4336 elf_next_file_pos (abfd) = off;
4338 /* Now that we know where the .strtab section goes, write it
4340 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4341 || ! _bfd_elf_strtab_emit (abfd, strtab))
4343 _bfd_elf_strtab_free (strtab);
4346 abfd->output_has_begun = TRUE;
4351 /* Make an initial estimate of the size of the program header. If we
4352 get the number wrong here, we'll redo section placement. */
4354 static bfd_size_type
4355 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4359 const struct elf_backend_data *bed;
4361 /* Assume we will need exactly two PT_LOAD segments: one for text
4362 and one for data. */
4365 s = bfd_get_section_by_name (abfd, ".interp");
4366 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4368 /* If we have a loadable interpreter section, we need a
4369 PT_INTERP segment. In this case, assume we also need a
4370 PT_PHDR segment, although that may not be true for all
4375 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4377 /* We need a PT_DYNAMIC segment. */
4381 if (info != NULL && info->relro)
4383 /* We need a PT_GNU_RELRO segment. */
4387 if (elf_eh_frame_hdr (abfd))
4389 /* We need a PT_GNU_EH_FRAME segment. */
4393 if (elf_stack_flags (abfd))
4395 /* We need a PT_GNU_STACK segment. */
4399 s = bfd_get_section_by_name (abfd,
4400 NOTE_GNU_PROPERTY_SECTION_NAME);
4401 if (s != NULL && s->size != 0)
4403 /* We need a PT_GNU_PROPERTY segment. */
4407 for (s = abfd->sections; s != NULL; s = s->next)
4409 if ((s->flags & SEC_LOAD) != 0
4410 && elf_section_type (s) == SHT_NOTE)
4412 unsigned int alignment_power;
4413 /* We need a PT_NOTE segment. */
4415 /* Try to create just one PT_NOTE segment for all adjacent
4416 loadable SHT_NOTE sections. gABI requires that within a
4417 PT_NOTE segment (and also inside of each SHT_NOTE section)
4418 each note should have the same alignment. So we check
4419 whether the sections are correctly aligned. */
4420 alignment_power = s->alignment_power;
4421 while (s->next != NULL
4422 && s->next->alignment_power == alignment_power
4423 && (s->next->flags & SEC_LOAD) != 0
4424 && elf_section_type (s->next) == SHT_NOTE)
4429 for (s = abfd->sections; s != NULL; s = s->next)
4431 if (s->flags & SEC_THREAD_LOCAL)
4433 /* We need a PT_TLS segment. */
4439 bed = get_elf_backend_data (abfd);
4441 if ((abfd->flags & D_PAGED) != 0
4442 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4444 /* Add a PT_GNU_MBIND segment for each mbind section. */
4445 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4446 for (s = abfd->sections; s != NULL; s = s->next)
4447 if (elf_section_flags (s) & SHF_GNU_MBIND)
4449 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4452 /* xgettext:c-format */
4453 (_("%pB: GNU_MBIND section `%pA' has invalid "
4454 "sh_info field: %d"),
4455 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4458 /* Align mbind section to page size. */
4459 if (s->alignment_power < page_align_power)
4460 s->alignment_power = page_align_power;
4465 /* Let the backend count up any program headers it might need. */
4466 if (bed->elf_backend_additional_program_headers)
4470 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4476 return segs * bed->s->sizeof_phdr;
4479 /* Find the segment that contains the output_section of section. */
4482 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4484 struct elf_segment_map *m;
4485 Elf_Internal_Phdr *p;
4487 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4493 for (i = m->count - 1; i >= 0; i--)
4494 if (m->sections[i] == section)
4501 /* Create a mapping from a set of sections to a program segment. */
4503 static struct elf_segment_map *
4504 make_mapping (bfd *abfd,
4505 asection **sections,
4510 struct elf_segment_map *m;
4515 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4516 amt += (to - from) * sizeof (asection *);
4517 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4521 m->p_type = PT_LOAD;
4522 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4523 m->sections[i - from] = *hdrpp;
4524 m->count = to - from;
4526 if (from == 0 && phdr)
4528 /* Include the headers in the first PT_LOAD segment. */
4529 m->includes_filehdr = 1;
4530 m->includes_phdrs = 1;
4536 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4539 struct elf_segment_map *
4540 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4542 struct elf_segment_map *m;
4544 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4545 sizeof (struct elf_segment_map));
4549 m->p_type = PT_DYNAMIC;
4551 m->sections[0] = dynsec;
4556 /* Possibly add or remove segments from the segment map. */
4559 elf_modify_segment_map (bfd *abfd,
4560 struct bfd_link_info *info,
4561 bfd_boolean remove_empty_load)
4563 struct elf_segment_map **m;
4564 const struct elf_backend_data *bed;
4566 /* The placement algorithm assumes that non allocated sections are
4567 not in PT_LOAD segments. We ensure this here by removing such
4568 sections from the segment map. We also remove excluded
4569 sections. Finally, any PT_LOAD segment without sections is
4571 m = &elf_seg_map (abfd);
4574 unsigned int i, new_count;
4576 for (new_count = 0, i = 0; i < (*m)->count; i++)
4578 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4579 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4580 || (*m)->p_type != PT_LOAD))
4582 (*m)->sections[new_count] = (*m)->sections[i];
4586 (*m)->count = new_count;
4588 if (remove_empty_load
4589 && (*m)->p_type == PT_LOAD
4591 && !(*m)->includes_phdrs)
4597 bed = get_elf_backend_data (abfd);
4598 if (bed->elf_backend_modify_segment_map != NULL)
4600 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4607 #define IS_TBSS(s) \
4608 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4610 /* Set up a mapping from BFD sections to program segments. */
4613 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4616 struct elf_segment_map *m;
4617 asection **sections = NULL;
4618 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4619 bfd_boolean no_user_phdrs;
4621 no_user_phdrs = elf_seg_map (abfd) == NULL;
4624 info->user_phdrs = !no_user_phdrs;
4626 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4630 struct elf_segment_map *mfirst;
4631 struct elf_segment_map **pm;
4634 unsigned int hdr_index;
4635 bfd_vma maxpagesize;
4637 bfd_boolean phdr_in_segment;
4638 bfd_boolean writable;
4639 bfd_boolean executable;
4641 asection *first_tls = NULL;
4642 asection *first_mbind = NULL;
4643 asection *dynsec, *eh_frame_hdr;
4645 bfd_vma addr_mask, wrap_to = 0;
4646 bfd_size_type phdr_size;
4648 /* Select the allocated sections, and sort them. */
4650 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4651 sizeof (asection *));
4652 if (sections == NULL)
4655 /* Calculate top address, avoiding undefined behaviour of shift
4656 left operator when shift count is equal to size of type
4658 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4659 addr_mask = (addr_mask << 1) + 1;
4662 for (s = abfd->sections; s != NULL; s = s->next)
4664 if ((s->flags & SEC_ALLOC) != 0)
4668 /* A wrapping section potentially clashes with header. */
4669 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4670 wrap_to = (s->lma + s->size) & addr_mask;
4673 BFD_ASSERT (i <= bfd_count_sections (abfd));
4676 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4678 phdr_size = elf_program_header_size (abfd);
4679 if (phdr_size == (bfd_size_type) -1)
4680 phdr_size = get_program_header_size (abfd, info);
4681 phdr_size += bed->s->sizeof_ehdr;
4682 maxpagesize = bed->maxpagesize;
4683 if (maxpagesize == 0)
4685 phdr_in_segment = info != NULL && info->load_phdrs;
4687 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4688 >= (phdr_size & (maxpagesize - 1))))
4689 /* For compatibility with old scripts that may not be using
4690 SIZEOF_HEADERS, add headers when it looks like space has
4691 been left for them. */
4692 phdr_in_segment = TRUE;
4694 /* Build the mapping. */
4698 /* If we have a .interp section, then create a PT_PHDR segment for
4699 the program headers and a PT_INTERP segment for the .interp
4701 s = bfd_get_section_by_name (abfd, ".interp");
4702 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4704 amt = sizeof (struct elf_segment_map);
4705 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4709 m->p_type = PT_PHDR;
4711 m->p_flags_valid = 1;
4712 m->includes_phdrs = 1;
4713 phdr_in_segment = TRUE;
4717 amt = sizeof (struct elf_segment_map);
4718 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4722 m->p_type = PT_INTERP;
4730 /* Look through the sections. We put sections in the same program
4731 segment when the start of the second section can be placed within
4732 a few bytes of the end of the first section. */
4738 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4740 && (dynsec->flags & SEC_LOAD) == 0)
4743 if ((abfd->flags & D_PAGED) == 0)
4744 phdr_in_segment = FALSE;
4746 /* Deal with -Ttext or something similar such that the first section
4747 is not adjacent to the program headers. This is an
4748 approximation, since at this point we don't know exactly how many
4749 program headers we will need. */
4750 if (phdr_in_segment && count > 0)
4753 bfd_boolean separate_phdr = FALSE;
4755 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4757 && info->separate_code
4758 && (sections[0]->flags & SEC_CODE) != 0)
4760 /* If data sections should be separate from code and
4761 thus not executable, and the first section is
4762 executable then put the file and program headers in
4763 their own PT_LOAD. */
4764 separate_phdr = TRUE;
4765 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4766 == (sections[0]->lma & addr_mask & -maxpagesize)))
4768 /* The file and program headers are currently on the
4769 same page as the first section. Put them on the
4770 previous page if we can. */
4771 if (phdr_lma >= maxpagesize)
4772 phdr_lma -= maxpagesize;
4774 separate_phdr = FALSE;
4777 if ((sections[0]->lma & addr_mask) < phdr_lma
4778 || (sections[0]->lma & addr_mask) < phdr_size)
4779 /* If file and program headers would be placed at the end
4780 of memory then it's probably better to omit them. */
4781 phdr_in_segment = FALSE;
4782 else if (phdr_lma < wrap_to)
4783 /* If a section wraps around to where we'll be placing
4784 file and program headers, then the headers will be
4786 phdr_in_segment = FALSE;
4787 else if (separate_phdr)
4789 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4792 m->p_paddr = phdr_lma;
4794 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4795 m->p_paddr_valid = 1;
4798 phdr_in_segment = FALSE;
4802 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4805 bfd_boolean new_segment;
4809 /* See if this section and the last one will fit in the same
4812 if (last_hdr == NULL)
4814 /* If we don't have a segment yet, then we don't need a new
4815 one (we build the last one after this loop). */
4816 new_segment = FALSE;
4818 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4820 /* If this section has a different relation between the
4821 virtual address and the load address, then we need a new
4825 else if (hdr->lma < last_hdr->lma + last_size
4826 || last_hdr->lma + last_size < last_hdr->lma)
4828 /* If this section has a load address that makes it overlap
4829 the previous section, then we need a new segment. */
4832 else if ((abfd->flags & D_PAGED) != 0
4833 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4834 == (hdr->lma & -maxpagesize)))
4836 /* If we are demand paged then we can't map two disk
4837 pages onto the same memory page. */
4838 new_segment = FALSE;
4840 /* In the next test we have to be careful when last_hdr->lma is close
4841 to the end of the address space. If the aligned address wraps
4842 around to the start of the address space, then there are no more
4843 pages left in memory and it is OK to assume that the current
4844 section can be included in the current segment. */
4845 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4846 + maxpagesize > last_hdr->lma)
4847 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4848 + maxpagesize <= hdr->lma))
4850 /* If putting this section in this segment would force us to
4851 skip a page in the segment, then we need a new segment. */
4854 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4855 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4857 /* We don't want to put a loaded section after a
4858 nonloaded (ie. bss style) section in the same segment
4859 as that will force the non-loaded section to be loaded.
4860 Consider .tbss sections as loaded for this purpose. */
4863 else if ((abfd->flags & D_PAGED) == 0)
4865 /* If the file is not demand paged, which means that we
4866 don't require the sections to be correctly aligned in the
4867 file, then there is no other reason for a new segment. */
4868 new_segment = FALSE;
4870 else if (info != NULL
4871 && info->separate_code
4872 && executable != ((hdr->flags & SEC_CODE) != 0))
4877 && (hdr->flags & SEC_READONLY) == 0)
4879 /* We don't want to put a writable section in a read only
4885 /* Otherwise, we can use the same segment. */
4886 new_segment = FALSE;
4889 /* Allow interested parties a chance to override our decision. */
4890 if (last_hdr != NULL
4892 && info->callbacks->override_segment_assignment != NULL)
4894 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4900 if ((hdr->flags & SEC_READONLY) == 0)
4902 if ((hdr->flags & SEC_CODE) != 0)
4905 /* .tbss sections effectively have zero size. */
4906 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4910 /* We need a new program segment. We must create a new program
4911 header holding all the sections from hdr_index until hdr. */
4913 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4920 if ((hdr->flags & SEC_READONLY) == 0)
4925 if ((hdr->flags & SEC_CODE) == 0)
4931 /* .tbss sections effectively have zero size. */
4932 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4934 phdr_in_segment = FALSE;
4937 /* Create a final PT_LOAD program segment, but not if it's just
4939 if (last_hdr != NULL
4940 && (i - hdr_index != 1
4941 || !IS_TBSS (last_hdr)))
4943 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4951 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4954 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4961 /* For each batch of consecutive loadable SHT_NOTE sections,
4962 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4963 because if we link together nonloadable .note sections and
4964 loadable .note sections, we will generate two .note sections
4965 in the output file. */
4966 for (s = abfd->sections; s != NULL; s = s->next)
4968 if ((s->flags & SEC_LOAD) != 0
4969 && elf_section_type (s) == SHT_NOTE)
4972 unsigned int alignment_power = s->alignment_power;
4975 for (s2 = s; s2->next != NULL; s2 = s2->next)
4977 if (s2->next->alignment_power == alignment_power
4978 && (s2->next->flags & SEC_LOAD) != 0
4979 && elf_section_type (s2->next) == SHT_NOTE
4980 && align_power (s2->lma + s2->size,
4987 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4988 amt += count * sizeof (asection *);
4989 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4993 m->p_type = PT_NOTE;
4997 m->sections[m->count - count--] = s;
4998 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5001 m->sections[m->count - 1] = s;
5002 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5006 if (s->flags & SEC_THREAD_LOCAL)
5012 if (first_mbind == NULL
5013 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5017 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5020 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5021 amt += tls_count * sizeof (asection *);
5022 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5027 m->count = tls_count;
5028 /* Mandated PF_R. */
5030 m->p_flags_valid = 1;
5032 for (i = 0; i < (unsigned int) tls_count; ++i)
5034 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5037 (_("%pB: TLS sections are not adjacent:"), abfd);
5040 while (i < (unsigned int) tls_count)
5042 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5044 _bfd_error_handler (_(" TLS: %pA"), s);
5048 _bfd_error_handler (_(" non-TLS: %pA"), s);
5051 bfd_set_error (bfd_error_bad_value);
5063 && (abfd->flags & D_PAGED) != 0
5064 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5065 for (s = first_mbind; s != NULL; s = s->next)
5066 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5067 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5069 /* Mandated PF_R. */
5070 unsigned long p_flags = PF_R;
5071 if ((s->flags & SEC_READONLY) == 0)
5073 if ((s->flags & SEC_CODE) != 0)
5076 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5077 m = bfd_zalloc (abfd, amt);
5081 m->p_type = (PT_GNU_MBIND_LO
5082 + elf_section_data (s)->this_hdr.sh_info);
5084 m->p_flags_valid = 1;
5086 m->p_flags = p_flags;
5092 s = bfd_get_section_by_name (abfd,
5093 NOTE_GNU_PROPERTY_SECTION_NAME);
5094 if (s != NULL && s->size != 0)
5096 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5097 m = bfd_zalloc (abfd, amt);
5101 m->p_type = PT_GNU_PROPERTY;
5103 m->p_flags_valid = 1;
5110 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5112 eh_frame_hdr = elf_eh_frame_hdr (abfd);
5113 if (eh_frame_hdr != NULL
5114 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5116 amt = sizeof (struct elf_segment_map);
5117 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5121 m->p_type = PT_GNU_EH_FRAME;
5123 m->sections[0] = eh_frame_hdr->output_section;
5129 if (elf_stack_flags (abfd))
5131 amt = sizeof (struct elf_segment_map);
5132 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5136 m->p_type = PT_GNU_STACK;
5137 m->p_flags = elf_stack_flags (abfd);
5138 m->p_align = bed->stack_align;
5139 m->p_flags_valid = 1;
5140 m->p_align_valid = m->p_align != 0;
5141 if (info->stacksize > 0)
5143 m->p_size = info->stacksize;
5144 m->p_size_valid = 1;
5151 if (info != NULL && info->relro)
5153 for (m = mfirst; m != NULL; m = m->next)
5155 if (m->p_type == PT_LOAD
5157 && m->sections[0]->vma >= info->relro_start
5158 && m->sections[0]->vma < info->relro_end)
5161 while (--i != (unsigned) -1)
5162 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5163 == (SEC_LOAD | SEC_HAS_CONTENTS))
5166 if (i != (unsigned) -1)
5171 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5174 amt = sizeof (struct elf_segment_map);
5175 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5179 m->p_type = PT_GNU_RELRO;
5186 elf_seg_map (abfd) = mfirst;
5189 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5192 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5194 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5199 if (sections != NULL)
5204 /* Sort sections by address. */
5207 elf_sort_sections (const void *arg1, const void *arg2)
5209 const asection *sec1 = *(const asection **) arg1;
5210 const asection *sec2 = *(const asection **) arg2;
5211 bfd_size_type size1, size2;
5213 /* Sort by LMA first, since this is the address used to
5214 place the section into a segment. */
5215 if (sec1->lma < sec2->lma)
5217 else if (sec1->lma > sec2->lma)
5220 /* Then sort by VMA. Normally the LMA and the VMA will be
5221 the same, and this will do nothing. */
5222 if (sec1->vma < sec2->vma)
5224 else if (sec1->vma > sec2->vma)
5227 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5229 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5235 /* If the indices are the same, do not return 0
5236 here, but continue to try the next comparison. */
5237 if (sec1->target_index - sec2->target_index != 0)
5238 return sec1->target_index - sec2->target_index;
5243 else if (TOEND (sec2))
5248 /* Sort by size, to put zero sized sections
5249 before others at the same address. */
5251 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5252 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5259 return sec1->target_index - sec2->target_index;
5262 /* Ian Lance Taylor writes:
5264 We shouldn't be using % with a negative signed number. That's just
5265 not good. We have to make sure either that the number is not
5266 negative, or that the number has an unsigned type. When the types
5267 are all the same size they wind up as unsigned. When file_ptr is a
5268 larger signed type, the arithmetic winds up as signed long long,
5271 What we're trying to say here is something like ``increase OFF by
5272 the least amount that will cause it to be equal to the VMA modulo
5274 /* In other words, something like:
5276 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5277 off_offset = off % bed->maxpagesize;
5278 if (vma_offset < off_offset)
5279 adjustment = vma_offset + bed->maxpagesize - off_offset;
5281 adjustment = vma_offset - off_offset;
5283 which can be collapsed into the expression below. */
5286 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5288 /* PR binutils/16199: Handle an alignment of zero. */
5289 if (maxpagesize == 0)
5291 return ((vma - off) % maxpagesize);
5295 print_segment_map (const struct elf_segment_map *m)
5298 const char *pt = get_segment_type (m->p_type);
5303 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5304 sprintf (buf, "LOPROC+%7.7x",
5305 (unsigned int) (m->p_type - PT_LOPROC));
5306 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5307 sprintf (buf, "LOOS+%7.7x",
5308 (unsigned int) (m->p_type - PT_LOOS));
5310 snprintf (buf, sizeof (buf), "%8.8x",
5311 (unsigned int) m->p_type);
5315 fprintf (stderr, "%s:", pt);
5316 for (j = 0; j < m->count; j++)
5317 fprintf (stderr, " %s", m->sections [j]->name);
5323 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5328 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5330 buf = bfd_zmalloc (len);
5333 ret = bfd_bwrite (buf, len, abfd) == len;
5338 /* Assign file positions to the sections based on the mapping from
5339 sections to segments. This function also sets up some fields in
5343 assign_file_positions_for_load_sections (bfd *abfd,
5344 struct bfd_link_info *link_info)
5346 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5347 struct elf_segment_map *m;
5348 Elf_Internal_Phdr *phdrs;
5349 Elf_Internal_Phdr *p;
5351 bfd_size_type maxpagesize;
5352 unsigned int pt_load_count = 0;
5355 bfd_vma header_pad = 0;
5357 if (link_info == NULL
5358 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5362 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5366 header_pad = m->header_size;
5371 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5372 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5376 /* PR binutils/12467. */
5377 elf_elfheader (abfd)->e_phoff = 0;
5378 elf_elfheader (abfd)->e_phentsize = 0;
5381 elf_elfheader (abfd)->e_phnum = alloc;
5383 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5384 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5386 BFD_ASSERT (elf_program_header_size (abfd)
5387 >= alloc * bed->s->sizeof_phdr);
5391 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5395 /* We're writing the size in elf_program_header_size (abfd),
5396 see assign_file_positions_except_relocs, so make sure we have
5397 that amount allocated, with trailing space cleared.
5398 The variable alloc contains the computed need, while
5399 elf_program_header_size (abfd) contains the size used for the
5401 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5402 where the layout is forced to according to a larger size in the
5403 last iterations for the testcase ld-elf/header. */
5404 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
5406 phdrs = (Elf_Internal_Phdr *)
5408 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5409 sizeof (Elf_Internal_Phdr));
5410 elf_tdata (abfd)->phdr = phdrs;
5415 if ((abfd->flags & D_PAGED) != 0)
5416 maxpagesize = bed->maxpagesize;
5418 off = bed->s->sizeof_ehdr;
5419 off += alloc * bed->s->sizeof_phdr;
5420 if (header_pad < (bfd_vma) off)
5426 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
5428 m = m->next, p++, j++)
5432 bfd_boolean no_contents;
5434 /* If elf_segment_map is not from map_sections_to_segments, the
5435 sections may not be correctly ordered. NOTE: sorting should
5436 not be done to the PT_NOTE section of a corefile, which may
5437 contain several pseudo-sections artificially created by bfd.
5438 Sorting these pseudo-sections breaks things badly. */
5440 && !(elf_elfheader (abfd)->e_type == ET_CORE
5441 && m->p_type == PT_NOTE))
5442 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5445 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5446 number of sections with contents contributing to both p_filesz
5447 and p_memsz, followed by a number of sections with no contents
5448 that just contribute to p_memsz. In this loop, OFF tracks next
5449 available file offset for PT_LOAD and PT_NOTE segments. */
5450 p->p_type = m->p_type;
5451 p->p_flags = m->p_flags;
5454 p->p_vaddr = m->p_vaddr_offset;
5456 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
5458 if (m->p_paddr_valid)
5459 p->p_paddr = m->p_paddr;
5460 else if (m->count == 0)
5463 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
5465 if (p->p_type == PT_LOAD
5466 && (abfd->flags & D_PAGED) != 0)
5468 /* p_align in demand paged PT_LOAD segments effectively stores
5469 the maximum page size. When copying an executable with
5470 objcopy, we set m->p_align from the input file. Use this
5471 value for maxpagesize rather than bed->maxpagesize, which
5472 may be different. Note that we use maxpagesize for PT_TLS
5473 segment alignment later in this function, so we are relying
5474 on at least one PT_LOAD segment appearing before a PT_TLS
5476 if (m->p_align_valid)
5477 maxpagesize = m->p_align;
5479 p->p_align = maxpagesize;
5482 else if (m->p_align_valid)
5483 p->p_align = m->p_align;
5484 else if (m->count == 0)
5485 p->p_align = 1 << bed->s->log_file_align;
5489 no_contents = FALSE;
5491 if (p->p_type == PT_LOAD
5494 bfd_size_type align;
5495 unsigned int align_power = 0;
5497 if (m->p_align_valid)
5501 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5503 unsigned int secalign;
5505 secalign = bfd_get_section_alignment (abfd, *secpp);
5506 if (secalign > align_power)
5507 align_power = secalign;
5509 align = (bfd_size_type) 1 << align_power;
5510 if (align < maxpagesize)
5511 align = maxpagesize;
5514 for (i = 0; i < m->count; i++)
5515 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5516 /* If we aren't making room for this section, then
5517 it must be SHT_NOBITS regardless of what we've
5518 set via struct bfd_elf_special_section. */
5519 elf_section_type (m->sections[i]) = SHT_NOBITS;
5521 /* Find out whether this segment contains any loadable
5524 for (i = 0; i < m->count; i++)
5525 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5527 no_contents = FALSE;
5531 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5533 /* Broken hardware and/or kernel require that files do not
5534 map the same page with different permissions on some hppa
5536 if (pt_load_count > 1
5537 && bed->no_page_alias
5538 && (off & (maxpagesize - 1)) != 0
5539 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5540 off_adjust += maxpagesize;
5544 /* We shouldn't need to align the segment on disk since
5545 the segment doesn't need file space, but the gABI
5546 arguably requires the alignment and glibc ld.so
5547 checks it. So to comply with the alignment
5548 requirement but not waste file space, we adjust
5549 p_offset for just this segment. (OFF_ADJUST is
5550 subtracted from OFF later.) This may put p_offset
5551 past the end of file, but that shouldn't matter. */
5556 /* Make sure the .dynamic section is the first section in the
5557 PT_DYNAMIC segment. */
5558 else if (p->p_type == PT_DYNAMIC
5560 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5563 (_("%pB: The first section in the PT_DYNAMIC segment"
5564 " is not the .dynamic section"),
5566 bfd_set_error (bfd_error_bad_value);
5569 /* Set the note section type to SHT_NOTE. */
5570 else if (p->p_type == PT_NOTE)
5571 for (i = 0; i < m->count; i++)
5572 elf_section_type (m->sections[i]) = SHT_NOTE;
5578 if (m->includes_filehdr)
5580 if (!m->p_flags_valid)
5582 p->p_filesz = bed->s->sizeof_ehdr;
5583 p->p_memsz = bed->s->sizeof_ehdr;
5586 if (p->p_vaddr < (bfd_vma) off
5587 || (!m->p_paddr_valid
5588 && p->p_paddr < (bfd_vma) off))
5591 (_("%pB: not enough room for program headers,"
5592 " try linking with -N"),
5594 bfd_set_error (bfd_error_bad_value);
5599 if (!m->p_paddr_valid)
5604 if (m->includes_phdrs)
5606 if (!m->p_flags_valid)
5609 if (!m->includes_filehdr)
5611 p->p_offset = bed->s->sizeof_ehdr;
5615 p->p_vaddr -= off - p->p_offset;
5616 if (!m->p_paddr_valid)
5617 p->p_paddr -= off - p->p_offset;
5621 p->p_filesz += alloc * bed->s->sizeof_phdr;
5622 p->p_memsz += alloc * bed->s->sizeof_phdr;
5625 p->p_filesz += header_pad;
5626 p->p_memsz += header_pad;
5630 if (p->p_type == PT_LOAD
5631 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5633 if (!m->includes_filehdr && !m->includes_phdrs)
5639 adjust = off - (p->p_offset + p->p_filesz);
5641 p->p_filesz += adjust;
5642 p->p_memsz += adjust;
5646 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5647 maps. Set filepos for sections in PT_LOAD segments, and in
5648 core files, for sections in PT_NOTE segments.
5649 assign_file_positions_for_non_load_sections will set filepos
5650 for other sections and update p_filesz for other segments. */
5651 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5654 bfd_size_type align;
5655 Elf_Internal_Shdr *this_hdr;
5658 this_hdr = &elf_section_data (sec)->this_hdr;
5659 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5661 if ((p->p_type == PT_LOAD
5662 || p->p_type == PT_TLS)
5663 && (this_hdr->sh_type != SHT_NOBITS
5664 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5665 && ((this_hdr->sh_flags & SHF_TLS) == 0
5666 || p->p_type == PT_TLS))))
5668 bfd_vma p_start = p->p_paddr;
5669 bfd_vma p_end = p_start + p->p_memsz;
5670 bfd_vma s_start = sec->lma;
5671 bfd_vma adjust = s_start - p_end;
5675 || p_end < p_start))
5678 /* xgettext:c-format */
5679 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5680 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
5684 p->p_memsz += adjust;
5686 if (this_hdr->sh_type != SHT_NOBITS)
5688 if (p->p_filesz + adjust < p->p_memsz)
5690 /* We have a PROGBITS section following NOBITS ones.
5691 Allocate file space for the NOBITS section(s) and
5693 adjust = p->p_memsz - p->p_filesz;
5694 if (!write_zeros (abfd, off, adjust))
5698 p->p_filesz += adjust;
5702 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5704 /* The section at i == 0 is the one that actually contains
5708 this_hdr->sh_offset = sec->filepos = off;
5709 off += this_hdr->sh_size;
5710 p->p_filesz = this_hdr->sh_size;
5716 /* The rest are fake sections that shouldn't be written. */
5725 if (p->p_type == PT_LOAD)
5727 this_hdr->sh_offset = sec->filepos = off;
5728 if (this_hdr->sh_type != SHT_NOBITS)
5729 off += this_hdr->sh_size;
5731 else if (this_hdr->sh_type == SHT_NOBITS
5732 && (this_hdr->sh_flags & SHF_TLS) != 0
5733 && this_hdr->sh_offset == 0)
5735 /* This is a .tbss section that didn't get a PT_LOAD.
5736 (See _bfd_elf_map_sections_to_segments "Create a
5737 final PT_LOAD".) Set sh_offset to the value it
5738 would have if we had created a zero p_filesz and
5739 p_memsz PT_LOAD header for the section. This
5740 also makes the PT_TLS header have the same
5742 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5744 this_hdr->sh_offset = sec->filepos = off + adjust;
5747 if (this_hdr->sh_type != SHT_NOBITS)
5749 p->p_filesz += this_hdr->sh_size;
5750 /* A load section without SHF_ALLOC is something like
5751 a note section in a PT_NOTE segment. These take
5752 file space but are not loaded into memory. */
5753 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5754 p->p_memsz += this_hdr->sh_size;
5756 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5758 if (p->p_type == PT_TLS)
5759 p->p_memsz += this_hdr->sh_size;
5761 /* .tbss is special. It doesn't contribute to p_memsz of
5763 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5764 p->p_memsz += this_hdr->sh_size;
5767 if (align > p->p_align
5768 && !m->p_align_valid
5769 && (p->p_type != PT_LOAD
5770 || (abfd->flags & D_PAGED) == 0))
5774 if (!m->p_flags_valid)
5777 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5779 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5786 /* Check that all sections are in a PT_LOAD segment.
5787 Don't check funky gdb generated core files. */
5788 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5790 bfd_boolean check_vma = TRUE;
5792 for (i = 1; i < m->count; i++)
5793 if (m->sections[i]->vma == m->sections[i - 1]->vma
5794 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5795 ->this_hdr), p) != 0
5796 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5797 ->this_hdr), p) != 0)
5799 /* Looks like we have overlays packed into the segment. */
5804 for (i = 0; i < m->count; i++)
5806 Elf_Internal_Shdr *this_hdr;
5809 sec = m->sections[i];
5810 this_hdr = &(elf_section_data(sec)->this_hdr);
5811 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5812 && !ELF_TBSS_SPECIAL (this_hdr, p))
5815 /* xgettext:c-format */
5816 (_("%pB: section `%pA' can't be allocated in segment %d"),
5818 print_segment_map (m);
5824 elf_next_file_pos (abfd) = off;
5828 /* Determine if a bfd is a debuginfo file. Unfortunately there
5829 is no defined method for detecting such files, so we have to
5830 use heuristics instead. */
5833 is_debuginfo_file (bfd *abfd)
5835 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5838 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
5839 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
5840 Elf_Internal_Shdr **headerp;
5842 for (headerp = start_headers; headerp < end_headers; headerp ++)
5844 Elf_Internal_Shdr *header = * headerp;
5846 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5847 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5848 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
5849 && header->sh_type != SHT_NOBITS
5850 && header->sh_type != SHT_NOTE)
5857 /* Assign file positions for the other sections. */
5860 assign_file_positions_for_non_load_sections (bfd *abfd,
5861 struct bfd_link_info *link_info)
5863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5864 Elf_Internal_Shdr **i_shdrpp;
5865 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5866 Elf_Internal_Phdr *phdrs;
5867 Elf_Internal_Phdr *p;
5868 struct elf_segment_map *m;
5869 struct elf_segment_map *hdrs_segment;
5870 bfd_vma filehdr_vaddr, filehdr_paddr;
5871 bfd_vma phdrs_vaddr, phdrs_paddr;
5875 i_shdrpp = elf_elfsections (abfd);
5876 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5877 off = elf_next_file_pos (abfd);
5878 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5880 Elf_Internal_Shdr *hdr;
5883 if (hdr->bfd_section != NULL
5884 && (hdr->bfd_section->filepos != 0
5885 || (hdr->sh_type == SHT_NOBITS
5886 && hdr->contents == NULL)))
5887 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5888 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5890 if (hdr->sh_size != 0
5891 /* PR 24717 - debuginfo files are known to be not strictly
5892 compliant with the ELF standard. In particular they often
5893 have .note.gnu.property sections that are outside of any
5894 loadable segment. This is not a problem for such files,
5895 so do not warn about them. */
5896 && ! is_debuginfo_file (abfd))
5898 /* xgettext:c-format */
5899 (_("%pB: warning: allocated section `%s' not in segment"),
5901 (hdr->bfd_section == NULL
5903 : hdr->bfd_section->name));
5904 /* We don't need to page align empty sections. */
5905 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5906 off += vma_page_aligned_bias (hdr->sh_addr, off,
5909 off += vma_page_aligned_bias (hdr->sh_addr, off,
5911 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5914 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5915 && hdr->bfd_section == NULL)
5916 || (hdr->bfd_section != NULL
5917 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5918 /* Compress DWARF debug sections. */
5919 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5920 || (elf_symtab_shndx_list (abfd) != NULL
5921 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5922 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5923 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5924 hdr->sh_offset = -1;
5926 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5929 /* Now that we have set the section file positions, we can set up
5930 the file positions for the non PT_LOAD segments. */
5934 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5936 hdrs_segment = NULL;
5937 phdrs = elf_tdata (abfd)->phdr;
5938 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5941 if (p->p_type != PT_LOAD)
5944 if (m->includes_filehdr)
5946 filehdr_vaddr = p->p_vaddr;
5947 filehdr_paddr = p->p_paddr;
5949 if (m->includes_phdrs)
5951 phdrs_vaddr = p->p_vaddr;
5952 phdrs_paddr = p->p_paddr;
5953 if (m->includes_filehdr)
5956 phdrs_vaddr += bed->s->sizeof_ehdr;
5957 phdrs_paddr += bed->s->sizeof_ehdr;
5962 if (hdrs_segment != NULL && link_info != NULL)
5964 /* There is a segment that contains both the file headers and the
5965 program headers, so provide a symbol __ehdr_start pointing there.
5966 A program can use this to examine itself robustly. */
5968 struct elf_link_hash_entry *hash
5969 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5970 FALSE, FALSE, TRUE);
5971 /* If the symbol was referenced and not defined, define it. */
5973 && (hash->root.type == bfd_link_hash_new
5974 || hash->root.type == bfd_link_hash_undefined
5975 || hash->root.type == bfd_link_hash_undefweak
5976 || hash->root.type == bfd_link_hash_common))
5979 if (hdrs_segment->count != 0)
5980 /* The segment contains sections, so use the first one. */
5981 s = hdrs_segment->sections[0];
5983 /* Use the first (i.e. lowest-addressed) section in any segment. */
5984 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5993 hash->root.u.def.value = filehdr_vaddr - s->vma;
5994 hash->root.u.def.section = s;
5998 hash->root.u.def.value = filehdr_vaddr;
5999 hash->root.u.def.section = bfd_abs_section_ptr;
6002 hash->root.type = bfd_link_hash_defined;
6003 hash->def_regular = 1;
6008 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6010 if (p->p_type == PT_GNU_RELRO)
6015 if (link_info != NULL)
6017 /* During linking the range of the RELRO segment is passed
6018 in link_info. Note that there may be padding between
6019 relro_start and the first RELRO section. */
6020 start = link_info->relro_start;
6021 end = link_info->relro_end;
6023 else if (m->count != 0)
6025 if (!m->p_size_valid)
6027 start = m->sections[0]->vma;
6028 end = start + m->p_size;
6039 struct elf_segment_map *lm;
6040 const Elf_Internal_Phdr *lp;
6043 /* Find a LOAD segment containing a section in the RELRO
6045 for (lm = elf_seg_map (abfd), lp = phdrs;
6047 lm = lm->next, lp++)
6049 if (lp->p_type == PT_LOAD
6051 && (lm->sections[lm->count - 1]->vma
6052 + (!IS_TBSS (lm->sections[lm->count - 1])
6053 ? lm->sections[lm->count - 1]->size
6055 && lm->sections[0]->vma < end)
6061 /* Find the section starting the RELRO segment. */
6062 for (i = 0; i < lm->count; i++)
6064 asection *s = lm->sections[i];
6073 p->p_vaddr = lm->sections[i]->vma;
6074 p->p_paddr = lm->sections[i]->lma;
6075 p->p_offset = lm->sections[i]->filepos;
6076 p->p_memsz = end - p->p_vaddr;
6077 p->p_filesz = p->p_memsz;
6079 /* The RELRO segment typically ends a few bytes
6080 into .got.plt but other layouts are possible.
6081 In cases where the end does not match any
6082 loaded section (for instance is in file
6083 padding), trim p_filesz back to correspond to
6084 the end of loaded section contents. */
6085 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6086 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6088 /* Preserve the alignment and flags if they are
6089 valid. The gold linker generates RW/4 for
6090 the PT_GNU_RELRO section. It is better for
6091 objcopy/strip to honor these attributes
6092 otherwise gdb will choke when using separate
6094 if (!m->p_align_valid)
6096 if (!m->p_flags_valid)
6102 if (link_info != NULL)
6105 memset (p, 0, sizeof *p);
6107 else if (p->p_type == PT_GNU_STACK)
6109 if (m->p_size_valid)
6110 p->p_memsz = m->p_size;
6112 else if (m->count != 0)
6116 if (p->p_type != PT_LOAD
6117 && (p->p_type != PT_NOTE
6118 || bfd_get_format (abfd) != bfd_core))
6120 /* A user specified segment layout may include a PHDR
6121 segment that overlaps with a LOAD segment... */
6122 if (p->p_type == PT_PHDR)
6128 if (m->includes_filehdr || m->includes_phdrs)
6130 /* PR 17512: file: 2195325e. */
6132 (_("%pB: error: non-load segment %d includes file header "
6133 "and/or program header"),
6134 abfd, (int) (p - phdrs));
6139 p->p_offset = m->sections[0]->filepos;
6140 for (i = m->count; i-- != 0;)
6142 asection *sect = m->sections[i];
6143 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6144 if (hdr->sh_type != SHT_NOBITS)
6146 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6153 else if (m->includes_filehdr)
6155 p->p_vaddr = filehdr_vaddr;
6156 if (! m->p_paddr_valid)
6157 p->p_paddr = filehdr_paddr;
6159 else if (m->includes_phdrs)
6161 p->p_vaddr = phdrs_vaddr;
6162 if (! m->p_paddr_valid)
6163 p->p_paddr = phdrs_paddr;
6167 elf_next_file_pos (abfd) = off;
6172 static elf_section_list *
6173 find_section_in_list (unsigned int i, elf_section_list * list)
6175 for (;list != NULL; list = list->next)
6181 /* Work out the file positions of all the sections. This is called by
6182 _bfd_elf_compute_section_file_positions. All the section sizes and
6183 VMAs must be known before this is called.
6185 Reloc sections come in two flavours: Those processed specially as
6186 "side-channel" data attached to a section to which they apply, and
6187 those that bfd doesn't process as relocations. The latter sort are
6188 stored in a normal bfd section by bfd_section_from_shdr. We don't
6189 consider the former sort here, unless they form part of the loadable
6190 image. Reloc sections not assigned here will be handled later by
6191 assign_file_positions_for_relocs.
6193 We also don't set the positions of the .symtab and .strtab here. */
6196 assign_file_positions_except_relocs (bfd *abfd,
6197 struct bfd_link_info *link_info)
6199 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6200 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6201 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6203 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6204 && bfd_get_format (abfd) != bfd_core)
6206 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6207 unsigned int num_sec = elf_numsections (abfd);
6208 Elf_Internal_Shdr **hdrpp;
6212 /* Start after the ELF header. */
6213 off = i_ehdrp->e_ehsize;
6215 /* We are not creating an executable, which means that we are
6216 not creating a program header, and that the actual order of
6217 the sections in the file is unimportant. */
6218 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6220 Elf_Internal_Shdr *hdr;
6223 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6224 && hdr->bfd_section == NULL)
6225 || (hdr->bfd_section != NULL
6226 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6227 /* Compress DWARF debug sections. */
6228 || i == elf_onesymtab (abfd)
6229 || (elf_symtab_shndx_list (abfd) != NULL
6230 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6231 || i == elf_strtab_sec (abfd)
6232 || i == elf_shstrtab_sec (abfd))
6234 hdr->sh_offset = -1;
6237 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6240 elf_next_file_pos (abfd) = off;
6246 /* Assign file positions for the loaded sections based on the
6247 assignment of sections to segments. */
6248 if (!assign_file_positions_for_load_sections (abfd, link_info))
6251 /* And for non-load sections. */
6252 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6255 if (bed->elf_backend_modify_program_headers != NULL)
6257 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6261 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6262 if (link_info != NULL && bfd_link_pie (link_info))
6264 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6265 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6266 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6268 /* Find the lowest p_vaddr in PT_LOAD segments. */
6269 bfd_vma p_vaddr = (bfd_vma) -1;
6270 for (; segment < end_segment; segment++)
6271 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6272 p_vaddr = segment->p_vaddr;
6274 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6275 segments is non-zero. */
6277 i_ehdrp->e_type = ET_EXEC;
6280 /* Write out the program headers. */
6281 alloc = elf_elfheader (abfd)->e_phnum;
6285 /* PR ld/20815 - Check that the program header segment, if present, will
6286 be loaded into memory. FIXME: The check below is not sufficient as
6287 really all PT_LOAD segments should be checked before issuing an error
6288 message. Plus the PHDR segment does not have to be the first segment
6289 in the program header table. But this version of the check should
6290 catch all real world use cases.
6292 FIXME: We used to have code here to sort the PT_LOAD segments into
6293 ascending order, as per the ELF spec. But this breaks some programs,
6294 including the Linux kernel. But really either the spec should be
6295 changed or the programs updated. */
6297 && tdata->phdr[0].p_type == PT_PHDR
6298 && (bed->elf_backend_allow_non_load_phdr == NULL
6299 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6301 && tdata->phdr[1].p_type == PT_LOAD
6302 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6303 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6304 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
6306 /* The fix for this error is usually to edit the linker script being
6307 used and set up the program headers manually. Either that or
6308 leave room for the headers at the start of the SECTIONS. */
6309 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6310 " by LOAD segment"),
6315 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
6316 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6324 prep_headers (bfd *abfd)
6326 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6327 struct elf_strtab_hash *shstrtab;
6328 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6330 i_ehdrp = elf_elfheader (abfd);
6332 shstrtab = _bfd_elf_strtab_init ();
6333 if (shstrtab == NULL)
6336 elf_shstrtab (abfd) = shstrtab;
6338 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6339 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6340 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6341 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6343 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6344 i_ehdrp->e_ident[EI_DATA] =
6345 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6346 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6348 if ((abfd->flags & DYNAMIC) != 0)
6349 i_ehdrp->e_type = ET_DYN;
6350 else if ((abfd->flags & EXEC_P) != 0)
6351 i_ehdrp->e_type = ET_EXEC;
6352 else if (bfd_get_format (abfd) == bfd_core)
6353 i_ehdrp->e_type = ET_CORE;
6355 i_ehdrp->e_type = ET_REL;
6357 switch (bfd_get_arch (abfd))
6359 case bfd_arch_unknown:
6360 i_ehdrp->e_machine = EM_NONE;
6363 /* There used to be a long list of cases here, each one setting
6364 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6365 in the corresponding bfd definition. To avoid duplication,
6366 the switch was removed. Machines that need special handling
6367 can generally do it in elf_backend_final_write_processing(),
6368 unless they need the information earlier than the final write.
6369 Such need can generally be supplied by replacing the tests for
6370 e_machine with the conditions used to determine it. */
6372 i_ehdrp->e_machine = bed->elf_machine_code;
6375 i_ehdrp->e_version = bed->s->ev_current;
6376 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6378 /* No program header, for now. */
6379 i_ehdrp->e_phoff = 0;
6380 i_ehdrp->e_phentsize = 0;
6381 i_ehdrp->e_phnum = 0;
6383 /* Each bfd section is section header entry. */
6384 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6385 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6387 /* If we're building an executable, we'll need a program header table. */
6388 if (abfd->flags & EXEC_P)
6389 /* It all happens later. */
6393 i_ehdrp->e_phentsize = 0;
6394 i_ehdrp->e_phoff = 0;
6397 elf_tdata (abfd)->symtab_hdr.sh_name =
6398 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6399 elf_tdata (abfd)->strtab_hdr.sh_name =
6400 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6401 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6402 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6403 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6404 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6405 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6411 /* Assign file positions for all the reloc sections which are not part
6412 of the loadable file image, and the file position of section headers. */
6415 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6418 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6419 Elf_Internal_Shdr *shdrp;
6420 Elf_Internal_Ehdr *i_ehdrp;
6421 const struct elf_backend_data *bed;
6423 off = elf_next_file_pos (abfd);
6425 shdrpp = elf_elfsections (abfd);
6426 end_shdrpp = shdrpp + elf_numsections (abfd);
6427 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6430 if (shdrp->sh_offset == -1)
6432 asection *sec = shdrp->bfd_section;
6433 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6434 || shdrp->sh_type == SHT_RELA);
6436 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6440 const char *name = sec->name;
6441 struct bfd_elf_section_data *d;
6443 /* Compress DWARF debug sections. */
6444 if (!bfd_compress_section (abfd, sec,
6448 if (sec->compress_status == COMPRESS_SECTION_DONE
6449 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6451 /* If section is compressed with zlib-gnu, convert
6452 section name from .debug_* to .zdebug_*. */
6454 = convert_debug_to_zdebug (abfd, name);
6455 if (new_name == NULL)
6459 /* Add section name to section name section. */
6460 if (shdrp->sh_name != (unsigned int) -1)
6463 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6465 d = elf_section_data (sec);
6467 /* Add reloc section name to section name section. */
6469 && !_bfd_elf_set_reloc_sh_name (abfd,
6474 && !_bfd_elf_set_reloc_sh_name (abfd,
6479 /* Update section size and contents. */
6480 shdrp->sh_size = sec->size;
6481 shdrp->contents = sec->contents;
6482 shdrp->bfd_section->contents = NULL;
6484 off = _bfd_elf_assign_file_position_for_section (shdrp,
6491 /* Place section name section after DWARF debug sections have been
6493 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6494 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6495 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6496 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6498 /* Place the section headers. */
6499 i_ehdrp = elf_elfheader (abfd);
6500 bed = get_elf_backend_data (abfd);
6501 off = align_file_position (off, 1 << bed->s->log_file_align);
6502 i_ehdrp->e_shoff = off;
6503 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6504 elf_next_file_pos (abfd) = off;
6510 _bfd_elf_write_object_contents (bfd *abfd)
6512 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6513 Elf_Internal_Shdr **i_shdrp;
6515 unsigned int count, num_sec;
6516 struct elf_obj_tdata *t;
6518 if (! abfd->output_has_begun
6519 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6521 /* Do not rewrite ELF data when the BFD has been opened for update.
6522 abfd->output_has_begun was set to TRUE on opening, so creation of new
6523 sections, and modification of existing section sizes was restricted.
6524 This means the ELF header, program headers and section headers can't have
6526 If the contents of any sections has been modified, then those changes have
6527 already been written to the BFD. */
6528 else if (abfd->direction == both_direction)
6530 BFD_ASSERT (abfd->output_has_begun);
6534 i_shdrp = elf_elfsections (abfd);
6537 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6541 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6544 /* After writing the headers, we need to write the sections too... */
6545 num_sec = elf_numsections (abfd);
6546 for (count = 1; count < num_sec; count++)
6548 i_shdrp[count]->sh_name
6549 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6550 i_shdrp[count]->sh_name);
6551 if (bed->elf_backend_section_processing)
6552 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6554 if (i_shdrp[count]->contents)
6556 bfd_size_type amt = i_shdrp[count]->sh_size;
6558 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6559 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6564 /* Write out the section header names. */
6565 t = elf_tdata (abfd);
6566 if (elf_shstrtab (abfd) != NULL
6567 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6568 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6571 if (!(*bed->elf_backend_final_write_processing) (abfd))
6574 if (!bed->s->write_shdrs_and_ehdr (abfd))
6577 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6578 if (t->o->build_id.after_write_object_contents != NULL)
6579 return (*t->o->build_id.after_write_object_contents) (abfd);
6585 _bfd_elf_write_corefile_contents (bfd *abfd)
6587 /* Hopefully this can be done just like an object file. */
6588 return _bfd_elf_write_object_contents (abfd);
6591 /* Given a section, search the header to find them. */
6594 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6596 const struct elf_backend_data *bed;
6597 unsigned int sec_index;
6599 if (elf_section_data (asect) != NULL
6600 && elf_section_data (asect)->this_idx != 0)
6601 return elf_section_data (asect)->this_idx;
6603 if (bfd_is_abs_section (asect))
6604 sec_index = SHN_ABS;
6605 else if (bfd_is_com_section (asect))
6606 sec_index = SHN_COMMON;
6607 else if (bfd_is_und_section (asect))
6608 sec_index = SHN_UNDEF;
6610 sec_index = SHN_BAD;
6612 bed = get_elf_backend_data (abfd);
6613 if (bed->elf_backend_section_from_bfd_section)
6615 int retval = sec_index;
6617 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6621 if (sec_index == SHN_BAD)
6622 bfd_set_error (bfd_error_nonrepresentable_section);
6627 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6631 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6633 asymbol *asym_ptr = *asym_ptr_ptr;
6635 flagword flags = asym_ptr->flags;
6637 /* When gas creates relocations against local labels, it creates its
6638 own symbol for the section, but does put the symbol into the
6639 symbol chain, so udata is 0. When the linker is generating
6640 relocatable output, this section symbol may be for one of the
6641 input sections rather than the output section. */
6642 if (asym_ptr->udata.i == 0
6643 && (flags & BSF_SECTION_SYM)
6644 && asym_ptr->section)
6649 sec = asym_ptr->section;
6650 if (sec->owner != abfd && sec->output_section != NULL)
6651 sec = sec->output_section;
6652 if (sec->owner == abfd
6653 && (indx = sec->index) < elf_num_section_syms (abfd)
6654 && elf_section_syms (abfd)[indx] != NULL)
6655 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6658 idx = asym_ptr->udata.i;
6662 /* This case can occur when using --strip-symbol on a symbol
6663 which is used in a relocation entry. */
6665 /* xgettext:c-format */
6666 (_("%pB: symbol `%s' required but not present"),
6667 abfd, bfd_asymbol_name (asym_ptr));
6668 bfd_set_error (bfd_error_no_symbols);
6675 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6676 (long) asym_ptr, asym_ptr->name, idx, flags);
6684 /* Rewrite program header information. */
6687 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6689 Elf_Internal_Ehdr *iehdr;
6690 struct elf_segment_map *map;
6691 struct elf_segment_map *map_first;
6692 struct elf_segment_map **pointer_to_map;
6693 Elf_Internal_Phdr *segment;
6696 unsigned int num_segments;
6697 bfd_boolean phdr_included = FALSE;
6698 bfd_boolean p_paddr_valid;
6699 bfd_vma maxpagesize;
6700 struct elf_segment_map *phdr_adjust_seg = NULL;
6701 unsigned int phdr_adjust_num = 0;
6702 const struct elf_backend_data *bed;
6704 bed = get_elf_backend_data (ibfd);
6705 iehdr = elf_elfheader (ibfd);
6708 pointer_to_map = &map_first;
6710 num_segments = elf_elfheader (ibfd)->e_phnum;
6711 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6713 /* Returns the end address of the segment + 1. */
6714 #define SEGMENT_END(segment, start) \
6715 (start + (segment->p_memsz > segment->p_filesz \
6716 ? segment->p_memsz : segment->p_filesz))
6718 #define SECTION_SIZE(section, segment) \
6719 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6720 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6721 ? section->size : 0)
6723 /* Returns TRUE if the given section is contained within
6724 the given segment. VMA addresses are compared. */
6725 #define IS_CONTAINED_BY_VMA(section, segment) \
6726 (section->vma >= segment->p_vaddr \
6727 && (section->vma + SECTION_SIZE (section, segment) \
6728 <= (SEGMENT_END (segment, segment->p_vaddr))))
6730 /* Returns TRUE if the given section is contained within
6731 the given segment. LMA addresses are compared. */
6732 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6733 (section->lma >= base \
6734 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6735 && (section->lma + SECTION_SIZE (section, segment) \
6736 <= SEGMENT_END (segment, base)))
6738 /* Handle PT_NOTE segment. */
6739 #define IS_NOTE(p, s) \
6740 (p->p_type == PT_NOTE \
6741 && elf_section_type (s) == SHT_NOTE \
6742 && (bfd_vma) s->filepos >= p->p_offset \
6743 && ((bfd_vma) s->filepos + s->size \
6744 <= p->p_offset + p->p_filesz))
6746 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6748 #define IS_COREFILE_NOTE(p, s) \
6750 && bfd_get_format (ibfd) == bfd_core \
6754 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6755 linker, which generates a PT_INTERP section with p_vaddr and
6756 p_memsz set to 0. */
6757 #define IS_SOLARIS_PT_INTERP(p, s) \
6759 && p->p_paddr == 0 \
6760 && p->p_memsz == 0 \
6761 && p->p_filesz > 0 \
6762 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6764 && (bfd_vma) s->filepos >= p->p_offset \
6765 && ((bfd_vma) s->filepos + s->size \
6766 <= p->p_offset + p->p_filesz))
6768 /* Decide if the given section should be included in the given segment.
6769 A section will be included if:
6770 1. It is within the address space of the segment -- we use the LMA
6771 if that is set for the segment and the VMA otherwise,
6772 2. It is an allocated section or a NOTE section in a PT_NOTE
6774 3. There is an output section associated with it,
6775 4. The section has not already been allocated to a previous segment.
6776 5. PT_GNU_STACK segments do not include any sections.
6777 6. PT_TLS segment includes only SHF_TLS sections.
6778 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6779 8. PT_DYNAMIC should not contain empty sections at the beginning
6780 (with the possible exception of .dynamic). */
6781 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6782 ((((segment->p_paddr \
6783 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6784 : IS_CONTAINED_BY_VMA (section, segment)) \
6785 && (section->flags & SEC_ALLOC) != 0) \
6786 || IS_NOTE (segment, section)) \
6787 && segment->p_type != PT_GNU_STACK \
6788 && (segment->p_type != PT_TLS \
6789 || (section->flags & SEC_THREAD_LOCAL)) \
6790 && (segment->p_type == PT_LOAD \
6791 || segment->p_type == PT_TLS \
6792 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6793 && (segment->p_type != PT_DYNAMIC \
6794 || SECTION_SIZE (section, segment) > 0 \
6795 || (segment->p_paddr \
6796 ? segment->p_paddr != section->lma \
6797 : segment->p_vaddr != section->vma) \
6798 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6800 && (segment->p_type != PT_LOAD || !section->segment_mark))
6802 /* If the output section of a section in the input segment is NULL,
6803 it is removed from the corresponding output segment. */
6804 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6805 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6806 && section->output_section != NULL)
6808 /* Returns TRUE iff seg1 starts after the end of seg2. */
6809 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6810 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6812 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6813 their VMA address ranges and their LMA address ranges overlap.
6814 It is possible to have overlapping VMA ranges without overlapping LMA
6815 ranges. RedBoot images for example can have both .data and .bss mapped
6816 to the same VMA range, but with the .data section mapped to a different
6818 #define SEGMENT_OVERLAPS(seg1, seg2) \
6819 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6820 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6821 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6822 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6824 /* Initialise the segment mark field. */
6825 for (section = ibfd->sections; section != NULL; section = section->next)
6826 section->segment_mark = FALSE;
6828 /* The Solaris linker creates program headers in which all the
6829 p_paddr fields are zero. When we try to objcopy or strip such a
6830 file, we get confused. Check for this case, and if we find it
6831 don't set the p_paddr_valid fields. */
6832 p_paddr_valid = FALSE;
6833 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6836 if (segment->p_paddr != 0)
6838 p_paddr_valid = TRUE;
6842 /* Scan through the segments specified in the program header
6843 of the input BFD. For this first scan we look for overlaps
6844 in the loadable segments. These can be created by weird
6845 parameters to objcopy. Also, fix some solaris weirdness. */
6846 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6851 Elf_Internal_Phdr *segment2;
6853 if (segment->p_type == PT_INTERP)
6854 for (section = ibfd->sections; section; section = section->next)
6855 if (IS_SOLARIS_PT_INTERP (segment, section))
6857 /* Mininal change so that the normal section to segment
6858 assignment code will work. */
6859 segment->p_vaddr = section->vma;
6863 if (segment->p_type != PT_LOAD)
6865 /* Remove PT_GNU_RELRO segment. */
6866 if (segment->p_type == PT_GNU_RELRO)
6867 segment->p_type = PT_NULL;
6871 /* Determine if this segment overlaps any previous segments. */
6872 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6874 bfd_signed_vma extra_length;
6876 if (segment2->p_type != PT_LOAD
6877 || !SEGMENT_OVERLAPS (segment, segment2))
6880 /* Merge the two segments together. */
6881 if (segment2->p_vaddr < segment->p_vaddr)
6883 /* Extend SEGMENT2 to include SEGMENT and then delete
6885 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6886 - SEGMENT_END (segment2, segment2->p_vaddr));
6888 if (extra_length > 0)
6890 segment2->p_memsz += extra_length;
6891 segment2->p_filesz += extra_length;
6894 segment->p_type = PT_NULL;
6896 /* Since we have deleted P we must restart the outer loop. */
6898 segment = elf_tdata (ibfd)->phdr;
6903 /* Extend SEGMENT to include SEGMENT2 and then delete
6905 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6906 - SEGMENT_END (segment, segment->p_vaddr));
6908 if (extra_length > 0)
6910 segment->p_memsz += extra_length;
6911 segment->p_filesz += extra_length;
6914 segment2->p_type = PT_NULL;
6919 /* The second scan attempts to assign sections to segments. */
6920 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6924 unsigned int section_count;
6925 asection **sections;
6926 asection *output_section;
6928 asection *matching_lma;
6929 asection *suggested_lma;
6932 asection *first_section;
6934 if (segment->p_type == PT_NULL)
6937 first_section = NULL;
6938 /* Compute how many sections might be placed into this segment. */
6939 for (section = ibfd->sections, section_count = 0;
6941 section = section->next)
6943 /* Find the first section in the input segment, which may be
6944 removed from the corresponding output segment. */
6945 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6947 if (first_section == NULL)
6948 first_section = section;
6949 if (section->output_section != NULL)
6954 /* Allocate a segment map big enough to contain
6955 all of the sections we have selected. */
6956 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6957 amt += (bfd_size_type) section_count * sizeof (asection *);
6958 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6962 /* Initialise the fields of the segment map. Default to
6963 using the physical address of the segment in the input BFD. */
6965 map->p_type = segment->p_type;
6966 map->p_flags = segment->p_flags;
6967 map->p_flags_valid = 1;
6969 /* If the first section in the input segment is removed, there is
6970 no need to preserve segment physical address in the corresponding
6972 if (!first_section || first_section->output_section != NULL)
6974 map->p_paddr = segment->p_paddr;
6975 map->p_paddr_valid = p_paddr_valid;
6978 /* Determine if this segment contains the ELF file header
6979 and if it contains the program headers themselves. */
6980 map->includes_filehdr = (segment->p_offset == 0
6981 && segment->p_filesz >= iehdr->e_ehsize);
6982 map->includes_phdrs = 0;
6984 if (!phdr_included || segment->p_type != PT_LOAD)
6986 map->includes_phdrs =
6987 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6988 && (segment->p_offset + segment->p_filesz
6989 >= ((bfd_vma) iehdr->e_phoff
6990 + iehdr->e_phnum * iehdr->e_phentsize)));
6992 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6993 phdr_included = TRUE;
6996 if (section_count == 0)
6998 /* Special segments, such as the PT_PHDR segment, may contain
6999 no sections, but ordinary, loadable segments should contain
7000 something. They are allowed by the ELF spec however, so only
7001 a warning is produced.
7002 There is however the valid use case of embedded systems which
7003 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7004 flash memory with zeros. No warning is shown for that case. */
7005 if (segment->p_type == PT_LOAD
7006 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7007 /* xgettext:c-format */
7009 (_("%pB: warning: empty loadable segment detected"
7010 " at vaddr=%#" PRIx64 ", is this intentional?"),
7011 ibfd, (uint64_t) segment->p_vaddr);
7013 map->p_vaddr_offset = segment->p_vaddr;
7015 *pointer_to_map = map;
7016 pointer_to_map = &map->next;
7021 /* Now scan the sections in the input BFD again and attempt
7022 to add their corresponding output sections to the segment map.
7023 The problem here is how to handle an output section which has
7024 been moved (ie had its LMA changed). There are four possibilities:
7026 1. None of the sections have been moved.
7027 In this case we can continue to use the segment LMA from the
7030 2. All of the sections have been moved by the same amount.
7031 In this case we can change the segment's LMA to match the LMA
7032 of the first section.
7034 3. Some of the sections have been moved, others have not.
7035 In this case those sections which have not been moved can be
7036 placed in the current segment which will have to have its size,
7037 and possibly its LMA changed, and a new segment or segments will
7038 have to be created to contain the other sections.
7040 4. The sections have been moved, but not by the same amount.
7041 In this case we can change the segment's LMA to match the LMA
7042 of the first section and we will have to create a new segment
7043 or segments to contain the other sections.
7045 In order to save time, we allocate an array to hold the section
7046 pointers that we are interested in. As these sections get assigned
7047 to a segment, they are removed from this array. */
7049 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
7050 if (sections == NULL)
7053 /* Step One: Scan for segment vs section LMA conflicts.
7054 Also add the sections to the section array allocated above.
7055 Also add the sections to the current segment. In the common
7056 case, where the sections have not been moved, this means that
7057 we have completely filled the segment, and there is nothing
7060 matching_lma = NULL;
7061 suggested_lma = NULL;
7063 for (section = first_section, j = 0;
7065 section = section->next)
7067 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
7069 output_section = section->output_section;
7071 sections[j++] = section;
7073 /* The Solaris native linker always sets p_paddr to 0.
7074 We try to catch that case here, and set it to the
7075 correct value. Note - some backends require that
7076 p_paddr be left as zero. */
7078 && segment->p_vaddr != 0
7079 && !bed->want_p_paddr_set_to_zero
7081 && output_section->lma != 0
7082 && (align_power (segment->p_vaddr
7083 + (map->includes_filehdr
7084 ? iehdr->e_ehsize : 0)
7085 + (map->includes_phdrs
7086 ? iehdr->e_phnum * iehdr->e_phentsize
7088 output_section->alignment_power)
7089 == output_section->vma))
7090 map->p_paddr = segment->p_vaddr;
7092 /* Match up the physical address of the segment with the
7093 LMA address of the output section. */
7094 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7095 || IS_COREFILE_NOTE (segment, section)
7096 || (bed->want_p_paddr_set_to_zero
7097 && IS_CONTAINED_BY_VMA (output_section, segment)))
7099 if (matching_lma == NULL
7100 || output_section->lma < matching_lma->lma)
7101 matching_lma = output_section;
7103 /* We assume that if the section fits within the segment
7104 then it does not overlap any other section within that
7106 map->sections[isec++] = output_section;
7108 else if (suggested_lma == NULL)
7109 suggested_lma = output_section;
7111 if (j == section_count)
7116 BFD_ASSERT (j == section_count);
7118 /* Step Two: Adjust the physical address of the current segment,
7120 if (isec == section_count)
7122 /* All of the sections fitted within the segment as currently
7123 specified. This is the default case. Add the segment to
7124 the list of built segments and carry on to process the next
7125 program header in the input BFD. */
7126 map->count = section_count;
7127 *pointer_to_map = map;
7128 pointer_to_map = &map->next;
7131 && !bed->want_p_paddr_set_to_zero
7132 && matching_lma->lma != map->p_paddr
7133 && !map->includes_filehdr
7134 && !map->includes_phdrs)
7135 /* There is some padding before the first section in the
7136 segment. So, we must account for that in the output
7138 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
7145 /* Change the current segment's physical address to match
7146 the LMA of the first section that fitted, or if no
7147 section fitted, the first section. */
7148 if (matching_lma == NULL)
7149 matching_lma = suggested_lma;
7151 map->p_paddr = matching_lma->lma;
7153 /* Offset the segment physical address from the lma
7154 to allow for space taken up by elf headers. */
7155 if (map->includes_phdrs)
7157 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7159 /* iehdr->e_phnum is just an estimate of the number
7160 of program headers that we will need. Make a note
7161 here of the number we used and the segment we chose
7162 to hold these headers, so that we can adjust the
7163 offset when we know the correct value. */
7164 phdr_adjust_num = iehdr->e_phnum;
7165 phdr_adjust_seg = map;
7168 if (map->includes_filehdr)
7170 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7171 map->p_paddr -= iehdr->e_ehsize;
7172 /* We've subtracted off the size of headers from the
7173 first section lma, but there may have been some
7174 alignment padding before that section too. Try to
7175 account for that by adjusting the segment lma down to
7176 the same alignment. */
7177 if (segment->p_align != 0 && segment->p_align < align)
7178 align = segment->p_align;
7179 map->p_paddr &= -align;
7183 /* Step Three: Loop over the sections again, this time assigning
7184 those that fit to the current segment and removing them from the
7185 sections array; but making sure not to leave large gaps. Once all
7186 possible sections have been assigned to the current segment it is
7187 added to the list of built segments and if sections still remain
7188 to be assigned, a new segment is constructed before repeating
7194 suggested_lma = NULL;
7196 /* Fill the current segment with sections that fit. */
7197 for (j = 0; j < section_count; j++)
7199 section = sections[j];
7201 if (section == NULL)
7204 output_section = section->output_section;
7206 BFD_ASSERT (output_section != NULL);
7208 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7209 || IS_COREFILE_NOTE (segment, section))
7211 if (map->count == 0)
7213 /* If the first section in a segment does not start at
7214 the beginning of the segment, then something is
7216 if (align_power (map->p_paddr
7217 + (map->includes_filehdr
7218 ? iehdr->e_ehsize : 0)
7219 + (map->includes_phdrs
7220 ? iehdr->e_phnum * iehdr->e_phentsize
7222 output_section->alignment_power)
7223 != output_section->lma)
7230 prev_sec = map->sections[map->count - 1];
7232 /* If the gap between the end of the previous section
7233 and the start of this section is more than
7234 maxpagesize then we need to start a new segment. */
7235 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7237 < BFD_ALIGN (output_section->lma, maxpagesize))
7238 || (prev_sec->lma + prev_sec->size
7239 > output_section->lma))
7241 if (suggested_lma == NULL)
7242 suggested_lma = output_section;
7248 map->sections[map->count++] = output_section;
7251 if (segment->p_type == PT_LOAD)
7252 section->segment_mark = TRUE;
7254 else if (suggested_lma == NULL)
7255 suggested_lma = output_section;
7258 /* PR 23932. A corrupt input file may contain sections that cannot
7259 be assigned to any segment - because for example they have a
7260 negative size - or segments that do not contain any sections. */
7261 if (map->count == 0)
7263 bfd_set_error (bfd_error_bad_value);
7268 /* Add the current segment to the list of built segments. */
7269 *pointer_to_map = map;
7270 pointer_to_map = &map->next;
7272 if (isec < section_count)
7274 /* We still have not allocated all of the sections to
7275 segments. Create a new segment here, initialise it
7276 and carry on looping. */
7277 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7278 amt += (bfd_size_type) section_count * sizeof (asection *);
7279 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7286 /* Initialise the fields of the segment map. Set the physical
7287 physical address to the LMA of the first section that has
7288 not yet been assigned. */
7290 map->p_type = segment->p_type;
7291 map->p_flags = segment->p_flags;
7292 map->p_flags_valid = 1;
7293 map->p_paddr = suggested_lma->lma;
7294 map->p_paddr_valid = p_paddr_valid;
7295 map->includes_filehdr = 0;
7296 map->includes_phdrs = 0;
7299 while (isec < section_count);
7304 elf_seg_map (obfd) = map_first;
7306 /* If we had to estimate the number of program headers that were
7307 going to be needed, then check our estimate now and adjust
7308 the offset if necessary. */
7309 if (phdr_adjust_seg != NULL)
7313 for (count = 0, map = map_first; map != NULL; map = map->next)
7316 if (count > phdr_adjust_num)
7317 phdr_adjust_seg->p_paddr
7318 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7320 for (map = map_first; map != NULL; map = map->next)
7321 if (map->p_type == PT_PHDR)
7324 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7325 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7332 #undef IS_CONTAINED_BY_VMA
7333 #undef IS_CONTAINED_BY_LMA
7335 #undef IS_COREFILE_NOTE
7336 #undef IS_SOLARIS_PT_INTERP
7337 #undef IS_SECTION_IN_INPUT_SEGMENT
7338 #undef INCLUDE_SECTION_IN_SEGMENT
7339 #undef SEGMENT_AFTER_SEGMENT
7340 #undef SEGMENT_OVERLAPS
7344 /* Copy ELF program header information. */
7347 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7349 Elf_Internal_Ehdr *iehdr;
7350 struct elf_segment_map *map;
7351 struct elf_segment_map *map_first;
7352 struct elf_segment_map **pointer_to_map;
7353 Elf_Internal_Phdr *segment;
7355 unsigned int num_segments;
7356 bfd_boolean phdr_included = FALSE;
7357 bfd_boolean p_paddr_valid;
7359 iehdr = elf_elfheader (ibfd);
7362 pointer_to_map = &map_first;
7364 /* If all the segment p_paddr fields are zero, don't set
7365 map->p_paddr_valid. */
7366 p_paddr_valid = FALSE;
7367 num_segments = elf_elfheader (ibfd)->e_phnum;
7368 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7371 if (segment->p_paddr != 0)
7373 p_paddr_valid = TRUE;
7377 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7382 unsigned int section_count;
7384 Elf_Internal_Shdr *this_hdr;
7385 asection *first_section = NULL;
7386 asection *lowest_section;
7387 bfd_boolean no_contents = TRUE;
7389 /* Compute how many sections are in this segment. */
7390 for (section = ibfd->sections, section_count = 0;
7392 section = section->next)
7394 this_hdr = &(elf_section_data(section)->this_hdr);
7395 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7397 if (first_section == NULL)
7398 first_section = section;
7399 if (elf_section_type (section) != SHT_NOBITS)
7400 no_contents = FALSE;
7405 /* Allocate a segment map big enough to contain
7406 all of the sections we have selected. */
7407 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7408 amt += (bfd_size_type) section_count * sizeof (asection *);
7409 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7413 /* Initialize the fields of the output segment map with the
7416 map->p_type = segment->p_type;
7417 map->p_flags = segment->p_flags;
7418 map->p_flags_valid = 1;
7419 map->p_paddr = segment->p_paddr;
7420 map->p_paddr_valid = p_paddr_valid;
7421 map->p_align = segment->p_align;
7422 map->p_align_valid = 1;
7423 map->p_vaddr_offset = 0;
7425 if (map->p_type == PT_GNU_RELRO
7426 || map->p_type == PT_GNU_STACK)
7428 /* The PT_GNU_RELRO segment may contain the first a few
7429 bytes in the .got.plt section even if the whole .got.plt
7430 section isn't in the PT_GNU_RELRO segment. We won't
7431 change the size of the PT_GNU_RELRO segment.
7432 Similarly, PT_GNU_STACK size is significant on uclinux
7434 map->p_size = segment->p_memsz;
7435 map->p_size_valid = 1;
7438 /* Determine if this segment contains the ELF file header
7439 and if it contains the program headers themselves. */
7440 map->includes_filehdr = (segment->p_offset == 0
7441 && segment->p_filesz >= iehdr->e_ehsize);
7443 map->includes_phdrs = 0;
7444 if (! phdr_included || segment->p_type != PT_LOAD)
7446 map->includes_phdrs =
7447 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7448 && (segment->p_offset + segment->p_filesz
7449 >= ((bfd_vma) iehdr->e_phoff
7450 + iehdr->e_phnum * iehdr->e_phentsize)));
7452 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7453 phdr_included = TRUE;
7456 lowest_section = NULL;
7457 if (section_count != 0)
7459 unsigned int isec = 0;
7461 for (section = first_section;
7463 section = section->next)
7465 this_hdr = &(elf_section_data(section)->this_hdr);
7466 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7468 map->sections[isec++] = section->output_section;
7469 if ((section->flags & SEC_ALLOC) != 0)
7473 if (lowest_section == NULL
7474 || section->lma < lowest_section->lma)
7475 lowest_section = section;
7477 /* Section lmas are set up from PT_LOAD header
7478 p_paddr in _bfd_elf_make_section_from_shdr.
7479 If this header has a p_paddr that disagrees
7480 with the section lma, flag the p_paddr as
7482 if ((section->flags & SEC_LOAD) != 0)
7483 seg_off = this_hdr->sh_offset - segment->p_offset;
7485 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7486 if (section->lma - segment->p_paddr != seg_off)
7487 map->p_paddr_valid = FALSE;
7489 if (isec == section_count)
7495 if (map->includes_filehdr && lowest_section != NULL)
7497 /* Try to keep the space used by the headers plus any
7498 padding fixed. If there are sections with file contents
7499 in this segment then the lowest sh_offset is the best
7500 guess. Otherwise the segment only has file contents for
7501 the headers, and p_filesz is the best guess. */
7503 map->header_size = segment->p_filesz;
7505 map->header_size = lowest_section->filepos;
7508 if (section_count == 0)
7509 map->p_vaddr_offset = segment->p_vaddr;
7510 else if (!map->includes_phdrs
7511 && !map->includes_filehdr
7512 && map->p_paddr_valid)
7513 /* Account for padding before the first section. */
7514 map->p_vaddr_offset = (segment->p_paddr
7515 - (lowest_section ? lowest_section->lma : 0));
7517 map->count = section_count;
7518 *pointer_to_map = map;
7519 pointer_to_map = &map->next;
7522 elf_seg_map (obfd) = map_first;
7526 /* Copy private BFD data. This copies or rewrites ELF program header
7530 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7532 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7533 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7536 if (elf_tdata (ibfd)->phdr == NULL)
7539 if (ibfd->xvec == obfd->xvec)
7541 /* Check to see if any sections in the input BFD
7542 covered by ELF program header have changed. */
7543 Elf_Internal_Phdr *segment;
7544 asection *section, *osec;
7545 unsigned int i, num_segments;
7546 Elf_Internal_Shdr *this_hdr;
7547 const struct elf_backend_data *bed;
7549 bed = get_elf_backend_data (ibfd);
7551 /* Regenerate the segment map if p_paddr is set to 0. */
7552 if (bed->want_p_paddr_set_to_zero)
7555 /* Initialize the segment mark field. */
7556 for (section = obfd->sections; section != NULL;
7557 section = section->next)
7558 section->segment_mark = FALSE;
7560 num_segments = elf_elfheader (ibfd)->e_phnum;
7561 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7565 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7566 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7567 which severly confuses things, so always regenerate the segment
7568 map in this case. */
7569 if (segment->p_paddr == 0
7570 && segment->p_memsz == 0
7571 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7574 for (section = ibfd->sections;
7575 section != NULL; section = section->next)
7577 /* We mark the output section so that we know it comes
7578 from the input BFD. */
7579 osec = section->output_section;
7581 osec->segment_mark = TRUE;
7583 /* Check if this section is covered by the segment. */
7584 this_hdr = &(elf_section_data(section)->this_hdr);
7585 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7587 /* FIXME: Check if its output section is changed or
7588 removed. What else do we need to check? */
7590 || section->flags != osec->flags
7591 || section->lma != osec->lma
7592 || section->vma != osec->vma
7593 || section->size != osec->size
7594 || section->rawsize != osec->rawsize
7595 || section->alignment_power != osec->alignment_power)
7601 /* Check to see if any output section do not come from the
7603 for (section = obfd->sections; section != NULL;
7604 section = section->next)
7606 if (!section->segment_mark)
7609 section->segment_mark = FALSE;
7612 return copy_elf_program_header (ibfd, obfd);
7616 if (ibfd->xvec == obfd->xvec)
7618 /* When rewriting program header, set the output maxpagesize to
7619 the maximum alignment of input PT_LOAD segments. */
7620 Elf_Internal_Phdr *segment;
7622 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7623 bfd_vma maxpagesize = 0;
7625 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7628 if (segment->p_type == PT_LOAD
7629 && maxpagesize < segment->p_align)
7631 /* PR 17512: file: f17299af. */
7632 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7633 /* xgettext:c-format */
7634 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7635 PRIx64 " is too large"),
7636 ibfd, (uint64_t) segment->p_align);
7638 maxpagesize = segment->p_align;
7641 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7642 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7645 return rewrite_elf_program_header (ibfd, obfd);
7648 /* Initialize private output section information from input section. */
7651 _bfd_elf_init_private_section_data (bfd *ibfd,
7655 struct bfd_link_info *link_info)
7658 Elf_Internal_Shdr *ihdr, *ohdr;
7659 bfd_boolean final_link = (link_info != NULL
7660 && !bfd_link_relocatable (link_info));
7662 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7663 || obfd->xvec->flavour != bfd_target_elf_flavour)
7666 BFD_ASSERT (elf_section_data (osec) != NULL);
7668 /* For objcopy and relocatable link, don't copy the output ELF
7669 section type from input if the output BFD section flags have been
7670 set to something different. For a final link allow some flags
7671 that the linker clears to differ. */
7672 if (elf_section_type (osec) == SHT_NULL
7673 && (osec->flags == isec->flags
7675 && ((osec->flags ^ isec->flags)
7676 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7677 elf_section_type (osec) = elf_section_type (isec);
7679 /* FIXME: Is this correct for all OS/PROC specific flags? */
7680 elf_section_flags (osec) |= (elf_section_flags (isec)
7681 & (SHF_MASKOS | SHF_MASKPROC));
7683 /* Copy sh_info from input for mbind section. */
7684 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7685 && elf_section_flags (isec) & SHF_GNU_MBIND)
7686 elf_section_data (osec)->this_hdr.sh_info
7687 = elf_section_data (isec)->this_hdr.sh_info;
7689 /* Set things up for objcopy and relocatable link. The output
7690 SHT_GROUP section will have its elf_next_in_group pointing back
7691 to the input group members. Ignore linker created group section.
7692 See elfNN_ia64_object_p in elfxx-ia64.c. */
7693 if ((link_info == NULL
7694 || !link_info->resolve_section_groups)
7695 && (elf_sec_group (isec) == NULL
7696 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7698 if (elf_section_flags (isec) & SHF_GROUP)
7699 elf_section_flags (osec) |= SHF_GROUP;
7700 elf_next_in_group (osec) = elf_next_in_group (isec);
7701 elf_section_data (osec)->group = elf_section_data (isec)->group;
7704 /* If not decompress, preserve SHF_COMPRESSED. */
7705 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7706 elf_section_flags (osec) |= (elf_section_flags (isec)
7709 ihdr = &elf_section_data (isec)->this_hdr;
7711 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7712 don't use the output section of the linked-to section since it
7713 may be NULL at this point. */
7714 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7716 ohdr = &elf_section_data (osec)->this_hdr;
7717 ohdr->sh_flags |= SHF_LINK_ORDER;
7718 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7721 osec->use_rela_p = isec->use_rela_p;
7726 /* Copy private section information. This copies over the entsize
7727 field, and sometimes the info field. */
7730 _bfd_elf_copy_private_section_data (bfd *ibfd,
7735 Elf_Internal_Shdr *ihdr, *ohdr;
7737 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7738 || obfd->xvec->flavour != bfd_target_elf_flavour)
7741 ihdr = &elf_section_data (isec)->this_hdr;
7742 ohdr = &elf_section_data (osec)->this_hdr;
7744 ohdr->sh_entsize = ihdr->sh_entsize;
7746 if (ihdr->sh_type == SHT_SYMTAB
7747 || ihdr->sh_type == SHT_DYNSYM
7748 || ihdr->sh_type == SHT_GNU_verneed
7749 || ihdr->sh_type == SHT_GNU_verdef)
7750 ohdr->sh_info = ihdr->sh_info;
7752 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7756 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7757 necessary if we are removing either the SHT_GROUP section or any of
7758 the group member sections. DISCARDED is the value that a section's
7759 output_section has if the section will be discarded, NULL when this
7760 function is called from objcopy, bfd_abs_section_ptr when called
7764 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7768 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7769 if (elf_section_type (isec) == SHT_GROUP)
7771 asection *first = elf_next_in_group (isec);
7772 asection *s = first;
7773 bfd_size_type removed = 0;
7777 /* If this member section is being output but the
7778 SHT_GROUP section is not, then clear the group info
7779 set up by _bfd_elf_copy_private_section_data. */
7780 if (s->output_section != discarded
7781 && isec->output_section == discarded)
7783 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7784 elf_group_name (s->output_section) = NULL;
7786 /* Conversely, if the member section is not being output
7787 but the SHT_GROUP section is, then adjust its size. */
7788 else if (s->output_section == discarded
7789 && isec->output_section != discarded)
7791 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7793 if (elf_sec->rel.hdr != NULL
7794 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7796 if (elf_sec->rela.hdr != NULL
7797 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7800 s = elf_next_in_group (s);
7806 if (discarded != NULL)
7808 /* If we've been called for ld -r, then we need to
7809 adjust the input section size. */
7810 if (isec->rawsize == 0)
7811 isec->rawsize = isec->size;
7812 isec->size = isec->rawsize - removed;
7813 if (isec->size <= 4)
7816 isec->flags |= SEC_EXCLUDE;
7821 /* Adjust the output section size when called from
7823 isec->output_section->size -= removed;
7824 if (isec->output_section->size <= 4)
7826 isec->output_section->size = 0;
7827 isec->output_section->flags |= SEC_EXCLUDE;
7836 /* Copy private header information. */
7839 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7841 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7842 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7845 /* Copy over private BFD data if it has not already been copied.
7846 This must be done here, rather than in the copy_private_bfd_data
7847 entry point, because the latter is called after the section
7848 contents have been set, which means that the program headers have
7849 already been worked out. */
7850 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7852 if (! copy_private_bfd_data (ibfd, obfd))
7856 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7859 /* Copy private symbol information. If this symbol is in a section
7860 which we did not map into a BFD section, try to map the section
7861 index correctly. We use special macro definitions for the mapped
7862 section indices; these definitions are interpreted by the
7863 swap_out_syms function. */
7865 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7866 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7867 #define MAP_STRTAB (SHN_HIOS + 3)
7868 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7869 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7872 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7877 elf_symbol_type *isym, *osym;
7879 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7880 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7883 isym = elf_symbol_from (ibfd, isymarg);
7884 osym = elf_symbol_from (obfd, osymarg);
7887 && isym->internal_elf_sym.st_shndx != 0
7889 && bfd_is_abs_section (isym->symbol.section))
7893 shndx = isym->internal_elf_sym.st_shndx;
7894 if (shndx == elf_onesymtab (ibfd))
7895 shndx = MAP_ONESYMTAB;
7896 else if (shndx == elf_dynsymtab (ibfd))
7897 shndx = MAP_DYNSYMTAB;
7898 else if (shndx == elf_strtab_sec (ibfd))
7900 else if (shndx == elf_shstrtab_sec (ibfd))
7901 shndx = MAP_SHSTRTAB;
7902 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7903 shndx = MAP_SYM_SHNDX;
7904 osym->internal_elf_sym.st_shndx = shndx;
7910 /* Swap out the symbols. */
7913 swap_out_syms (bfd *abfd,
7914 struct elf_strtab_hash **sttp,
7917 const struct elf_backend_data *bed;
7920 struct elf_strtab_hash *stt;
7921 Elf_Internal_Shdr *symtab_hdr;
7922 Elf_Internal_Shdr *symtab_shndx_hdr;
7923 Elf_Internal_Shdr *symstrtab_hdr;
7924 struct elf_sym_strtab *symstrtab;
7925 bfd_byte *outbound_syms;
7926 bfd_byte *outbound_shndx;
7927 unsigned long outbound_syms_index;
7928 unsigned long outbound_shndx_index;
7930 unsigned int num_locals;
7932 bfd_boolean name_local_sections;
7934 if (!elf_map_symbols (abfd, &num_locals))
7937 /* Dump out the symtabs. */
7938 stt = _bfd_elf_strtab_init ();
7942 bed = get_elf_backend_data (abfd);
7943 symcount = bfd_get_symcount (abfd);
7944 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7945 symtab_hdr->sh_type = SHT_SYMTAB;
7946 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7947 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7948 symtab_hdr->sh_info = num_locals + 1;
7949 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7951 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7952 symstrtab_hdr->sh_type = SHT_STRTAB;
7954 /* Allocate buffer to swap out the .strtab section. */
7955 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
7956 sizeof (*symstrtab));
7957 if (symstrtab == NULL)
7959 _bfd_elf_strtab_free (stt);
7963 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7964 bed->s->sizeof_sym);
7965 if (outbound_syms == NULL)
7968 _bfd_elf_strtab_free (stt);
7972 symtab_hdr->contents = outbound_syms;
7973 outbound_syms_index = 0;
7975 outbound_shndx = NULL;
7976 outbound_shndx_index = 0;
7978 if (elf_symtab_shndx_list (abfd))
7980 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7981 if (symtab_shndx_hdr->sh_name != 0)
7983 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7984 outbound_shndx = (bfd_byte *)
7985 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7986 if (outbound_shndx == NULL)
7989 symtab_shndx_hdr->contents = outbound_shndx;
7990 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7991 symtab_shndx_hdr->sh_size = amt;
7992 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7993 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7995 /* FIXME: What about any other headers in the list ? */
7998 /* Now generate the data (for "contents"). */
8000 /* Fill in zeroth symbol and swap it out. */
8001 Elf_Internal_Sym sym;
8007 sym.st_shndx = SHN_UNDEF;
8008 sym.st_target_internal = 0;
8009 symstrtab[0].sym = sym;
8010 symstrtab[0].dest_index = outbound_syms_index;
8011 symstrtab[0].destshndx_index = outbound_shndx_index;
8012 outbound_syms_index++;
8013 if (outbound_shndx != NULL)
8014 outbound_shndx_index++;
8018 = (bed->elf_backend_name_local_section_symbols
8019 && bed->elf_backend_name_local_section_symbols (abfd));
8021 syms = bfd_get_outsymbols (abfd);
8022 for (idx = 0; idx < symcount;)
8024 Elf_Internal_Sym sym;
8025 bfd_vma value = syms[idx]->value;
8026 elf_symbol_type *type_ptr;
8027 flagword flags = syms[idx]->flags;
8030 if (!name_local_sections
8031 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8033 /* Local section symbols have no name. */
8034 sym.st_name = (unsigned long) -1;
8038 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8039 to get the final offset for st_name. */
8041 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8043 if (sym.st_name == (unsigned long) -1)
8047 type_ptr = elf_symbol_from (abfd, syms[idx]);
8049 if ((flags & BSF_SECTION_SYM) == 0
8050 && bfd_is_com_section (syms[idx]->section))
8052 /* ELF common symbols put the alignment into the `value' field,
8053 and the size into the `size' field. This is backwards from
8054 how BFD handles it, so reverse it here. */
8055 sym.st_size = value;
8056 if (type_ptr == NULL
8057 || type_ptr->internal_elf_sym.st_value == 0)
8058 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8060 sym.st_value = type_ptr->internal_elf_sym.st_value;
8061 sym.st_shndx = _bfd_elf_section_from_bfd_section
8062 (abfd, syms[idx]->section);
8066 asection *sec = syms[idx]->section;
8069 if (sec->output_section)
8071 value += sec->output_offset;
8072 sec = sec->output_section;
8075 /* Don't add in the section vma for relocatable output. */
8076 if (! relocatable_p)
8078 sym.st_value = value;
8079 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8081 if (bfd_is_abs_section (sec)
8083 && type_ptr->internal_elf_sym.st_shndx != 0)
8085 /* This symbol is in a real ELF section which we did
8086 not create as a BFD section. Undo the mapping done
8087 by copy_private_symbol_data. */
8088 shndx = type_ptr->internal_elf_sym.st_shndx;
8092 shndx = elf_onesymtab (abfd);
8095 shndx = elf_dynsymtab (abfd);
8098 shndx = elf_strtab_sec (abfd);
8101 shndx = elf_shstrtab_sec (abfd);
8104 if (elf_symtab_shndx_list (abfd))
8105 shndx = elf_symtab_shndx_list (abfd)->ndx;
8114 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8116 if (shndx == SHN_BAD)
8120 /* Writing this would be a hell of a lot easier if
8121 we had some decent documentation on bfd, and
8122 knew what to expect of the library, and what to
8123 demand of applications. For example, it
8124 appears that `objcopy' might not set the
8125 section of a symbol to be a section that is
8126 actually in the output file. */
8127 sec2 = bfd_get_section_by_name (abfd, sec->name);
8129 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8130 if (shndx == SHN_BAD)
8132 /* xgettext:c-format */
8134 (_("unable to find equivalent output section"
8135 " for symbol '%s' from section '%s'"),
8136 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8138 bfd_set_error (bfd_error_invalid_operation);
8144 sym.st_shndx = shndx;
8147 if ((flags & BSF_THREAD_LOCAL) != 0)
8149 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8150 type = STT_GNU_IFUNC;
8151 else if ((flags & BSF_FUNCTION) != 0)
8153 else if ((flags & BSF_OBJECT) != 0)
8155 else if ((flags & BSF_RELC) != 0)
8157 else if ((flags & BSF_SRELC) != 0)
8162 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8165 /* Processor-specific types. */
8166 if (type_ptr != NULL
8167 && bed->elf_backend_get_symbol_type)
8168 type = ((*bed->elf_backend_get_symbol_type)
8169 (&type_ptr->internal_elf_sym, type));
8171 if (flags & BSF_SECTION_SYM)
8173 if (flags & BSF_GLOBAL)
8174 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8176 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8178 else if (bfd_is_com_section (syms[idx]->section))
8180 if (type != STT_TLS)
8182 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8183 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8184 ? STT_COMMON : STT_OBJECT);
8186 type = ((flags & BSF_ELF_COMMON) != 0
8187 ? STT_COMMON : STT_OBJECT);
8189 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8191 else if (bfd_is_und_section (syms[idx]->section))
8192 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8196 else if (flags & BSF_FILE)
8197 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8200 int bind = STB_LOCAL;
8202 if (flags & BSF_LOCAL)
8204 else if (flags & BSF_GNU_UNIQUE)
8205 bind = STB_GNU_UNIQUE;
8206 else if (flags & BSF_WEAK)
8208 else if (flags & BSF_GLOBAL)
8211 sym.st_info = ELF_ST_INFO (bind, type);
8214 if (type_ptr != NULL)
8216 sym.st_other = type_ptr->internal_elf_sym.st_other;
8217 sym.st_target_internal
8218 = type_ptr->internal_elf_sym.st_target_internal;
8223 sym.st_target_internal = 0;
8227 symstrtab[idx].sym = sym;
8228 symstrtab[idx].dest_index = outbound_syms_index;
8229 symstrtab[idx].destshndx_index = outbound_shndx_index;
8231 outbound_syms_index++;
8232 if (outbound_shndx != NULL)
8233 outbound_shndx_index++;
8236 /* Finalize the .strtab section. */
8237 _bfd_elf_strtab_finalize (stt);
8239 /* Swap out the .strtab section. */
8240 for (idx = 0; idx <= symcount; idx++)
8242 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8243 if (elfsym->sym.st_name == (unsigned long) -1)
8244 elfsym->sym.st_name = 0;
8246 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8247 elfsym->sym.st_name);
8248 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8250 + (elfsym->dest_index
8251 * bed->s->sizeof_sym)),
8253 + (elfsym->destshndx_index
8254 * sizeof (Elf_External_Sym_Shndx))));
8259 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8260 symstrtab_hdr->sh_type = SHT_STRTAB;
8261 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8262 symstrtab_hdr->sh_addr = 0;
8263 symstrtab_hdr->sh_entsize = 0;
8264 symstrtab_hdr->sh_link = 0;
8265 symstrtab_hdr->sh_info = 0;
8266 symstrtab_hdr->sh_addralign = 1;
8271 /* Return the number of bytes required to hold the symtab vector.
8273 Note that we base it on the count plus 1, since we will null terminate
8274 the vector allocated based on this size. However, the ELF symbol table
8275 always has a dummy entry as symbol #0, so it ends up even. */
8278 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8280 bfd_size_type symcount;
8282 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8284 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8285 if (symcount >= LONG_MAX / sizeof (asymbol *))
8287 bfd_set_error (bfd_error_file_too_big);
8290 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8292 symtab_size -= sizeof (asymbol *);
8298 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8300 bfd_size_type symcount;
8302 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8304 if (elf_dynsymtab (abfd) == 0)
8306 bfd_set_error (bfd_error_invalid_operation);
8310 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8311 if (symcount >= LONG_MAX / sizeof (asymbol *))
8313 bfd_set_error (bfd_error_file_too_big);
8316 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8318 symtab_size -= sizeof (asymbol *);
8324 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8327 #if SIZEOF_LONG == SIZEOF_INT
8328 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8330 bfd_set_error (bfd_error_file_too_big);
8334 return (asect->reloc_count + 1) * sizeof (arelent *);
8337 /* Canonicalize the relocs. */
8340 _bfd_elf_canonicalize_reloc (bfd *abfd,
8347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8349 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8352 tblptr = section->relocation;
8353 for (i = 0; i < section->reloc_count; i++)
8354 *relptr++ = tblptr++;
8358 return section->reloc_count;
8362 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8364 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8365 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8368 bfd_get_symcount (abfd) = symcount;
8373 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8374 asymbol **allocation)
8376 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8377 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8380 bfd_get_dynamic_symcount (abfd) = symcount;
8384 /* Return the size required for the dynamic reloc entries. Any loadable
8385 section that was actually installed in the BFD, and has type SHT_REL
8386 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8387 dynamic reloc section. */
8390 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8392 bfd_size_type count;
8395 if (elf_dynsymtab (abfd) == 0)
8397 bfd_set_error (bfd_error_invalid_operation);
8402 for (s = abfd->sections; s != NULL; s = s->next)
8403 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8404 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8405 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8407 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8408 if (count > LONG_MAX / sizeof (arelent *))
8410 bfd_set_error (bfd_error_file_too_big);
8414 return count * sizeof (arelent *);
8417 /* Canonicalize the dynamic relocation entries. Note that we return the
8418 dynamic relocations as a single block, although they are actually
8419 associated with particular sections; the interface, which was
8420 designed for SunOS style shared libraries, expects that there is only
8421 one set of dynamic relocs. Any loadable section that was actually
8422 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8423 dynamic symbol table, is considered to be a dynamic reloc section. */
8426 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8430 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8434 if (elf_dynsymtab (abfd) == 0)
8436 bfd_set_error (bfd_error_invalid_operation);
8440 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8442 for (s = abfd->sections; s != NULL; s = s->next)
8444 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8445 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8446 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8451 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8453 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8455 for (i = 0; i < count; i++)
8466 /* Read in the version information. */
8469 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8471 bfd_byte *contents = NULL;
8472 unsigned int freeidx = 0;
8474 if (elf_dynverref (abfd) != 0)
8476 Elf_Internal_Shdr *hdr;
8477 Elf_External_Verneed *everneed;
8478 Elf_Internal_Verneed *iverneed;
8480 bfd_byte *contents_end;
8482 hdr = &elf_tdata (abfd)->dynverref_hdr;
8484 if (hdr->sh_info == 0
8485 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8487 error_return_bad_verref:
8489 (_("%pB: .gnu.version_r invalid entry"), abfd);
8490 bfd_set_error (bfd_error_bad_value);
8491 error_return_verref:
8492 elf_tdata (abfd)->verref = NULL;
8493 elf_tdata (abfd)->cverrefs = 0;
8497 ufile_ptr filesize = bfd_get_file_size (abfd);
8498 if (filesize > 0 && filesize < hdr->sh_size)
8500 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8502 bfd_set_error (bfd_error_no_memory);
8504 /* xgettext:c-format */
8505 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8506 abfd, (uint64_t) hdr->sh_size);
8507 goto error_return_verref;
8509 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8510 if (contents == NULL)
8511 goto error_return_verref;
8513 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8514 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8515 goto error_return_verref;
8517 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8518 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8520 if (elf_tdata (abfd)->verref == NULL)
8521 goto error_return_verref;
8523 BFD_ASSERT (sizeof (Elf_External_Verneed)
8524 == sizeof (Elf_External_Vernaux));
8525 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8526 everneed = (Elf_External_Verneed *) contents;
8527 iverneed = elf_tdata (abfd)->verref;
8528 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8530 Elf_External_Vernaux *evernaux;
8531 Elf_Internal_Vernaux *ivernaux;
8534 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8536 iverneed->vn_bfd = abfd;
8538 iverneed->vn_filename =
8539 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8541 if (iverneed->vn_filename == NULL)
8542 goto error_return_bad_verref;
8544 if (iverneed->vn_cnt == 0)
8545 iverneed->vn_auxptr = NULL;
8548 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8549 bfd_alloc2 (abfd, iverneed->vn_cnt,
8550 sizeof (Elf_Internal_Vernaux));
8551 if (iverneed->vn_auxptr == NULL)
8552 goto error_return_verref;
8555 if (iverneed->vn_aux
8556 > (size_t) (contents_end - (bfd_byte *) everneed))
8557 goto error_return_bad_verref;
8559 evernaux = ((Elf_External_Vernaux *)
8560 ((bfd_byte *) everneed + iverneed->vn_aux));
8561 ivernaux = iverneed->vn_auxptr;
8562 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8564 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8566 ivernaux->vna_nodename =
8567 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8568 ivernaux->vna_name);
8569 if (ivernaux->vna_nodename == NULL)
8570 goto error_return_bad_verref;
8572 if (ivernaux->vna_other > freeidx)
8573 freeidx = ivernaux->vna_other;
8575 ivernaux->vna_nextptr = NULL;
8576 if (ivernaux->vna_next == 0)
8578 iverneed->vn_cnt = j + 1;
8581 if (j + 1 < iverneed->vn_cnt)
8582 ivernaux->vna_nextptr = ivernaux + 1;
8584 if (ivernaux->vna_next
8585 > (size_t) (contents_end - (bfd_byte *) evernaux))
8586 goto error_return_bad_verref;
8588 evernaux = ((Elf_External_Vernaux *)
8589 ((bfd_byte *) evernaux + ivernaux->vna_next));
8592 iverneed->vn_nextref = NULL;
8593 if (iverneed->vn_next == 0)
8595 if (i + 1 < hdr->sh_info)
8596 iverneed->vn_nextref = iverneed + 1;
8598 if (iverneed->vn_next
8599 > (size_t) (contents_end - (bfd_byte *) everneed))
8600 goto error_return_bad_verref;
8602 everneed = ((Elf_External_Verneed *)
8603 ((bfd_byte *) everneed + iverneed->vn_next));
8605 elf_tdata (abfd)->cverrefs = i;
8611 if (elf_dynverdef (abfd) != 0)
8613 Elf_Internal_Shdr *hdr;
8614 Elf_External_Verdef *everdef;
8615 Elf_Internal_Verdef *iverdef;
8616 Elf_Internal_Verdef *iverdefarr;
8617 Elf_Internal_Verdef iverdefmem;
8619 unsigned int maxidx;
8620 bfd_byte *contents_end_def, *contents_end_aux;
8622 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8624 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8626 error_return_bad_verdef:
8628 (_("%pB: .gnu.version_d invalid entry"), abfd);
8629 bfd_set_error (bfd_error_bad_value);
8630 error_return_verdef:
8631 elf_tdata (abfd)->verdef = NULL;
8632 elf_tdata (abfd)->cverdefs = 0;
8636 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8637 if (contents == NULL)
8638 goto error_return_verdef;
8639 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8640 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8641 goto error_return_verdef;
8643 BFD_ASSERT (sizeof (Elf_External_Verdef)
8644 >= sizeof (Elf_External_Verdaux));
8645 contents_end_def = contents + hdr->sh_size
8646 - sizeof (Elf_External_Verdef);
8647 contents_end_aux = contents + hdr->sh_size
8648 - sizeof (Elf_External_Verdaux);
8650 /* We know the number of entries in the section but not the maximum
8651 index. Therefore we have to run through all entries and find
8653 everdef = (Elf_External_Verdef *) contents;
8655 for (i = 0; i < hdr->sh_info; ++i)
8657 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8659 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8660 goto error_return_bad_verdef;
8661 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8662 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8664 if (iverdefmem.vd_next == 0)
8667 if (iverdefmem.vd_next
8668 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8669 goto error_return_bad_verdef;
8671 everdef = ((Elf_External_Verdef *)
8672 ((bfd_byte *) everdef + iverdefmem.vd_next));
8675 if (default_imported_symver)
8677 if (freeidx > maxidx)
8683 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8684 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8685 if (elf_tdata (abfd)->verdef == NULL)
8686 goto error_return_verdef;
8688 elf_tdata (abfd)->cverdefs = maxidx;
8690 everdef = (Elf_External_Verdef *) contents;
8691 iverdefarr = elf_tdata (abfd)->verdef;
8692 for (i = 0; i < hdr->sh_info; i++)
8694 Elf_External_Verdaux *everdaux;
8695 Elf_Internal_Verdaux *iverdaux;
8698 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8700 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8701 goto error_return_bad_verdef;
8703 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8704 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8706 iverdef->vd_bfd = abfd;
8708 if (iverdef->vd_cnt == 0)
8709 iverdef->vd_auxptr = NULL;
8712 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8713 bfd_alloc2 (abfd, iverdef->vd_cnt,
8714 sizeof (Elf_Internal_Verdaux));
8715 if (iverdef->vd_auxptr == NULL)
8716 goto error_return_verdef;
8720 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8721 goto error_return_bad_verdef;
8723 everdaux = ((Elf_External_Verdaux *)
8724 ((bfd_byte *) everdef + iverdef->vd_aux));
8725 iverdaux = iverdef->vd_auxptr;
8726 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8728 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8730 iverdaux->vda_nodename =
8731 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8732 iverdaux->vda_name);
8733 if (iverdaux->vda_nodename == NULL)
8734 goto error_return_bad_verdef;
8736 iverdaux->vda_nextptr = NULL;
8737 if (iverdaux->vda_next == 0)
8739 iverdef->vd_cnt = j + 1;
8742 if (j + 1 < iverdef->vd_cnt)
8743 iverdaux->vda_nextptr = iverdaux + 1;
8745 if (iverdaux->vda_next
8746 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8747 goto error_return_bad_verdef;
8749 everdaux = ((Elf_External_Verdaux *)
8750 ((bfd_byte *) everdaux + iverdaux->vda_next));
8753 iverdef->vd_nodename = NULL;
8754 if (iverdef->vd_cnt)
8755 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8757 iverdef->vd_nextdef = NULL;
8758 if (iverdef->vd_next == 0)
8760 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8761 iverdef->vd_nextdef = iverdef + 1;
8763 everdef = ((Elf_External_Verdef *)
8764 ((bfd_byte *) everdef + iverdef->vd_next));
8770 else if (default_imported_symver)
8777 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8778 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8779 if (elf_tdata (abfd)->verdef == NULL)
8782 elf_tdata (abfd)->cverdefs = freeidx;
8785 /* Create a default version based on the soname. */
8786 if (default_imported_symver)
8788 Elf_Internal_Verdef *iverdef;
8789 Elf_Internal_Verdaux *iverdaux;
8791 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8793 iverdef->vd_version = VER_DEF_CURRENT;
8794 iverdef->vd_flags = 0;
8795 iverdef->vd_ndx = freeidx;
8796 iverdef->vd_cnt = 1;
8798 iverdef->vd_bfd = abfd;
8800 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8801 if (iverdef->vd_nodename == NULL)
8802 goto error_return_verdef;
8803 iverdef->vd_nextdef = NULL;
8804 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8805 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8806 if (iverdef->vd_auxptr == NULL)
8807 goto error_return_verdef;
8809 iverdaux = iverdef->vd_auxptr;
8810 iverdaux->vda_nodename = iverdef->vd_nodename;
8816 if (contents != NULL)
8822 _bfd_elf_make_empty_symbol (bfd *abfd)
8824 elf_symbol_type *newsym;
8826 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
8829 newsym->symbol.the_bfd = abfd;
8830 return &newsym->symbol;
8834 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8838 bfd_symbol_info (symbol, ret);
8841 /* Return whether a symbol name implies a local symbol. Most targets
8842 use this function for the is_local_label_name entry point, but some
8846 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8849 /* Normal local symbols start with ``.L''. */
8850 if (name[0] == '.' && name[1] == 'L')
8853 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8854 DWARF debugging symbols starting with ``..''. */
8855 if (name[0] == '.' && name[1] == '.')
8858 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8859 emitting DWARF debugging output. I suspect this is actually a
8860 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8861 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8862 underscore to be emitted on some ELF targets). For ease of use,
8863 we treat such symbols as local. */
8864 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8867 /* Treat assembler generated fake symbols, dollar local labels and
8868 forward-backward labels (aka local labels) as locals.
8869 These labels have the form:
8871 L0^A.* (fake symbols)
8873 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8875 Versions which start with .L will have already been matched above,
8876 so we only need to match the rest. */
8877 if (name[0] == 'L' && ISDIGIT (name[1]))
8879 bfd_boolean ret = FALSE;
8883 for (p = name + 2; (c = *p); p++)
8885 if (c == 1 || c == 2)
8887 if (c == 1 && p == name + 2)
8888 /* A fake symbol. */
8891 /* FIXME: We are being paranoid here and treating symbols like
8892 L0^Bfoo as if there were non-local, on the grounds that the
8893 assembler will never generate them. But can any symbol
8894 containing an ASCII value in the range 1-31 ever be anything
8895 other than some kind of local ? */
8912 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8913 asymbol *symbol ATTRIBUTE_UNUSED)
8920 _bfd_elf_set_arch_mach (bfd *abfd,
8921 enum bfd_architecture arch,
8922 unsigned long machine)
8924 /* If this isn't the right architecture for this backend, and this
8925 isn't the generic backend, fail. */
8926 if (arch != get_elf_backend_data (abfd)->arch
8927 && arch != bfd_arch_unknown
8928 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8931 return bfd_default_set_arch_mach (abfd, arch, machine);
8934 /* Find the nearest line to a particular section and offset,
8935 for error reporting. */
8938 _bfd_elf_find_nearest_line (bfd *abfd,
8942 const char **filename_ptr,
8943 const char **functionname_ptr,
8944 unsigned int *line_ptr,
8945 unsigned int *discriminator_ptr)
8949 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8950 filename_ptr, functionname_ptr,
8951 line_ptr, discriminator_ptr,
8952 dwarf_debug_sections, 0,
8953 &elf_tdata (abfd)->dwarf2_find_line_info)
8954 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8955 filename_ptr, functionname_ptr,
8958 if (!*functionname_ptr)
8959 _bfd_elf_find_function (abfd, symbols, section, offset,
8960 *filename_ptr ? NULL : filename_ptr,
8965 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8966 &found, filename_ptr,
8967 functionname_ptr, line_ptr,
8968 &elf_tdata (abfd)->line_info))
8970 if (found && (*functionname_ptr || *line_ptr))
8973 if (symbols == NULL)
8976 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8977 filename_ptr, functionname_ptr))
8984 /* Find the line for a symbol. */
8987 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8988 const char **filename_ptr, unsigned int *line_ptr)
8990 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8991 filename_ptr, NULL, line_ptr, NULL,
8992 dwarf_debug_sections, 0,
8993 &elf_tdata (abfd)->dwarf2_find_line_info);
8996 /* After a call to bfd_find_nearest_line, successive calls to
8997 bfd_find_inliner_info can be used to get source information about
8998 each level of function inlining that terminated at the address
8999 passed to bfd_find_nearest_line. Currently this is only supported
9000 for DWARF2 with appropriate DWARF3 extensions. */
9003 _bfd_elf_find_inliner_info (bfd *abfd,
9004 const char **filename_ptr,
9005 const char **functionname_ptr,
9006 unsigned int *line_ptr)
9009 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9010 functionname_ptr, line_ptr,
9011 & elf_tdata (abfd)->dwarf2_find_line_info);
9016 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9018 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9019 int ret = bed->s->sizeof_ehdr;
9021 if (!bfd_link_relocatable (info))
9023 bfd_size_type phdr_size = elf_program_header_size (abfd);
9025 if (phdr_size == (bfd_size_type) -1)
9027 struct elf_segment_map *m;
9030 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9031 phdr_size += bed->s->sizeof_phdr;
9034 phdr_size = get_program_header_size (abfd, info);
9037 elf_program_header_size (abfd) = phdr_size;
9045 _bfd_elf_set_section_contents (bfd *abfd,
9047 const void *location,
9049 bfd_size_type count)
9051 Elf_Internal_Shdr *hdr;
9054 if (! abfd->output_has_begun
9055 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9061 hdr = &elf_section_data (section)->this_hdr;
9062 if (hdr->sh_offset == (file_ptr) -1)
9064 /* We must compress this section. Write output to the buffer. */
9065 unsigned char *contents = hdr->contents;
9066 if ((offset + count) > hdr->sh_size
9067 || (section->flags & SEC_ELF_COMPRESS) == 0
9068 || contents == NULL)
9070 memcpy (contents + offset, location, count);
9073 pos = hdr->sh_offset + offset;
9074 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9075 || bfd_bwrite (location, count, abfd) != count)
9082 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9083 arelent *cache_ptr ATTRIBUTE_UNUSED,
9084 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9090 /* Try to convert a non-ELF reloc into an ELF one. */
9093 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9095 /* Check whether we really have an ELF howto. */
9097 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9099 bfd_reloc_code_real_type code;
9100 reloc_howto_type *howto;
9102 /* Alien reloc: Try to determine its type to replace it with an
9103 equivalent ELF reloc. */
9105 if (areloc->howto->pc_relative)
9107 switch (areloc->howto->bitsize)
9110 code = BFD_RELOC_8_PCREL;
9113 code = BFD_RELOC_12_PCREL;
9116 code = BFD_RELOC_16_PCREL;
9119 code = BFD_RELOC_24_PCREL;
9122 code = BFD_RELOC_32_PCREL;
9125 code = BFD_RELOC_64_PCREL;
9131 howto = bfd_reloc_type_lookup (abfd, code);
9133 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9135 if (howto->pcrel_offset)
9136 areloc->addend += areloc->address;
9138 areloc->addend -= areloc->address; /* addend is unsigned!! */
9143 switch (areloc->howto->bitsize)
9149 code = BFD_RELOC_14;
9152 code = BFD_RELOC_16;
9155 code = BFD_RELOC_26;
9158 code = BFD_RELOC_32;
9161 code = BFD_RELOC_64;
9167 howto = bfd_reloc_type_lookup (abfd, code);
9171 areloc->howto = howto;
9179 /* xgettext:c-format */
9180 _bfd_error_handler (_("%pB: %s unsupported"),
9181 abfd, areloc->howto->name);
9182 bfd_set_error (bfd_error_bad_value);
9187 _bfd_elf_close_and_cleanup (bfd *abfd)
9189 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9190 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
9192 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9193 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9194 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9197 return _bfd_generic_close_and_cleanup (abfd);
9200 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9201 in the relocation's offset. Thus we cannot allow any sort of sanity
9202 range-checking to interfere. There is nothing else to do in processing
9205 bfd_reloc_status_type
9206 _bfd_elf_rel_vtable_reloc_fn
9207 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9208 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9209 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9210 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9212 return bfd_reloc_ok;
9215 /* Elf core file support. Much of this only works on native
9216 toolchains, since we rely on knowing the
9217 machine-dependent procfs structure in order to pick
9218 out details about the corefile. */
9220 #ifdef HAVE_SYS_PROCFS_H
9221 /* Needed for new procfs interface on sparc-solaris. */
9222 # define _STRUCTURED_PROC 1
9223 # include <sys/procfs.h>
9226 /* Return a PID that identifies a "thread" for threaded cores, or the
9227 PID of the main process for non-threaded cores. */
9230 elfcore_make_pid (bfd *abfd)
9234 pid = elf_tdata (abfd)->core->lwpid;
9236 pid = elf_tdata (abfd)->core->pid;
9241 /* If there isn't a section called NAME, make one, using
9242 data from SECT. Note, this function will generate a
9243 reference to NAME, so you shouldn't deallocate or
9247 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9251 if (bfd_get_section_by_name (abfd, name) != NULL)
9254 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9258 sect2->size = sect->size;
9259 sect2->filepos = sect->filepos;
9260 sect2->alignment_power = sect->alignment_power;
9264 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9265 actually creates up to two pseudosections:
9266 - For the single-threaded case, a section named NAME, unless
9267 such a section already exists.
9268 - For the multi-threaded case, a section named "NAME/PID", where
9269 PID is elfcore_make_pid (abfd).
9270 Both pseudosections have identical contents. */
9272 _bfd_elfcore_make_pseudosection (bfd *abfd,
9278 char *threaded_name;
9282 /* Build the section name. */
9284 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9285 len = strlen (buf) + 1;
9286 threaded_name = (char *) bfd_alloc (abfd, len);
9287 if (threaded_name == NULL)
9289 memcpy (threaded_name, buf, len);
9291 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9296 sect->filepos = filepos;
9297 sect->alignment_power = 2;
9299 return elfcore_maybe_make_sect (abfd, name, sect);
9303 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9306 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9312 sect->size = note->descsz - offs;
9313 sect->filepos = note->descpos + offs;
9314 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9319 /* prstatus_t exists on:
9321 linux 2.[01] + glibc
9325 #if defined (HAVE_PRSTATUS_T)
9328 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9333 if (note->descsz == sizeof (prstatus_t))
9337 size = sizeof (prstat.pr_reg);
9338 offset = offsetof (prstatus_t, pr_reg);
9339 memcpy (&prstat, note->descdata, sizeof (prstat));
9341 /* Do not overwrite the core signal if it
9342 has already been set by another thread. */
9343 if (elf_tdata (abfd)->core->signal == 0)
9344 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9345 if (elf_tdata (abfd)->core->pid == 0)
9346 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9348 /* pr_who exists on:
9351 pr_who doesn't exist on:
9354 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9355 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9357 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9360 #if defined (HAVE_PRSTATUS32_T)
9361 else if (note->descsz == sizeof (prstatus32_t))
9363 /* 64-bit host, 32-bit corefile */
9364 prstatus32_t prstat;
9366 size = sizeof (prstat.pr_reg);
9367 offset = offsetof (prstatus32_t, pr_reg);
9368 memcpy (&prstat, note->descdata, sizeof (prstat));
9370 /* Do not overwrite the core signal if it
9371 has already been set by another thread. */
9372 if (elf_tdata (abfd)->core->signal == 0)
9373 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9374 if (elf_tdata (abfd)->core->pid == 0)
9375 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9377 /* pr_who exists on:
9380 pr_who doesn't exist on:
9383 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9384 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9386 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9389 #endif /* HAVE_PRSTATUS32_T */
9392 /* Fail - we don't know how to handle any other
9393 note size (ie. data object type). */
9397 /* Make a ".reg/999" section and a ".reg" section. */
9398 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9399 size, note->descpos + offset);
9401 #endif /* defined (HAVE_PRSTATUS_T) */
9403 /* Create a pseudosection containing the exact contents of NOTE. */
9405 elfcore_make_note_pseudosection (bfd *abfd,
9407 Elf_Internal_Note *note)
9409 return _bfd_elfcore_make_pseudosection (abfd, name,
9410 note->descsz, note->descpos);
9413 /* There isn't a consistent prfpregset_t across platforms,
9414 but it doesn't matter, because we don't have to pick this
9415 data structure apart. */
9418 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9420 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9423 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9424 type of NT_PRXFPREG. Just include the whole note's contents
9428 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9430 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9433 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9434 with a note type of NT_X86_XSTATE. Just include the whole note's
9435 contents literally. */
9438 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9440 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9444 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9446 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9450 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9452 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9456 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9458 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9462 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9464 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9468 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9470 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9474 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9476 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9480 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9482 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9486 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9488 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9492 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9494 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9498 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9500 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9504 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9506 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9510 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9512 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9516 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9518 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9522 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9524 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9528 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9530 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9534 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9536 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9540 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9542 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9546 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9548 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9552 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9554 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9558 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9560 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9564 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9566 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9570 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9572 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9576 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9578 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9582 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9584 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9588 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9590 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9594 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9596 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9600 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9602 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9606 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9608 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9612 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9614 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9618 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9620 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9624 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9626 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9630 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9632 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9636 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9638 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9642 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9644 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9647 #if defined (HAVE_PRPSINFO_T)
9648 typedef prpsinfo_t elfcore_psinfo_t;
9649 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9650 typedef prpsinfo32_t elfcore_psinfo32_t;
9654 #if defined (HAVE_PSINFO_T)
9655 typedef psinfo_t elfcore_psinfo_t;
9656 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9657 typedef psinfo32_t elfcore_psinfo32_t;
9661 /* return a malloc'ed copy of a string at START which is at
9662 most MAX bytes long, possibly without a terminating '\0'.
9663 the copy will always have a terminating '\0'. */
9666 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9669 char *end = (char *) memchr (start, '\0', max);
9677 dups = (char *) bfd_alloc (abfd, len + 1);
9681 memcpy (dups, start, len);
9687 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9689 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9691 if (note->descsz == sizeof (elfcore_psinfo_t))
9693 elfcore_psinfo_t psinfo;
9695 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9697 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9698 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9700 elf_tdata (abfd)->core->program
9701 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9702 sizeof (psinfo.pr_fname));
9704 elf_tdata (abfd)->core->command
9705 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9706 sizeof (psinfo.pr_psargs));
9708 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9709 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9711 /* 64-bit host, 32-bit corefile */
9712 elfcore_psinfo32_t psinfo;
9714 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9716 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9717 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9719 elf_tdata (abfd)->core->program
9720 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9721 sizeof (psinfo.pr_fname));
9723 elf_tdata (abfd)->core->command
9724 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9725 sizeof (psinfo.pr_psargs));
9731 /* Fail - we don't know how to handle any other
9732 note size (ie. data object type). */
9736 /* Note that for some reason, a spurious space is tacked
9737 onto the end of the args in some (at least one anyway)
9738 implementations, so strip it off if it exists. */
9741 char *command = elf_tdata (abfd)->core->command;
9742 int n = strlen (command);
9744 if (0 < n && command[n - 1] == ' ')
9745 command[n - 1] = '\0';
9750 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9752 #if defined (HAVE_PSTATUS_T)
9754 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9756 if (note->descsz == sizeof (pstatus_t)
9757 #if defined (HAVE_PXSTATUS_T)
9758 || note->descsz == sizeof (pxstatus_t)
9764 memcpy (&pstat, note->descdata, sizeof (pstat));
9766 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9768 #if defined (HAVE_PSTATUS32_T)
9769 else if (note->descsz == sizeof (pstatus32_t))
9771 /* 64-bit host, 32-bit corefile */
9774 memcpy (&pstat, note->descdata, sizeof (pstat));
9776 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9779 /* Could grab some more details from the "representative"
9780 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9781 NT_LWPSTATUS note, presumably. */
9785 #endif /* defined (HAVE_PSTATUS_T) */
9787 #if defined (HAVE_LWPSTATUS_T)
9789 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9791 lwpstatus_t lwpstat;
9797 if (note->descsz != sizeof (lwpstat)
9798 #if defined (HAVE_LWPXSTATUS_T)
9799 && note->descsz != sizeof (lwpxstatus_t)
9804 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9806 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9807 /* Do not overwrite the core signal if it has already been set by
9809 if (elf_tdata (abfd)->core->signal == 0)
9810 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9812 /* Make a ".reg/999" section. */
9814 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
9815 len = strlen (buf) + 1;
9816 name = bfd_alloc (abfd, len);
9819 memcpy (name, buf, len);
9821 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9825 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9826 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
9827 sect->filepos = note->descpos
9828 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9831 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9832 sect->size = sizeof (lwpstat.pr_reg);
9833 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9836 sect->alignment_power = 2;
9838 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9841 /* Make a ".reg2/999" section */
9843 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9844 len = strlen (buf) + 1;
9845 name = bfd_alloc (abfd, len);
9848 memcpy (name, buf, len);
9850 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9854 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9855 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9856 sect->filepos = note->descpos
9857 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9860 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9861 sect->size = sizeof (lwpstat.pr_fpreg);
9862 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9865 sect->alignment_power = 2;
9867 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9869 #endif /* defined (HAVE_LWPSTATUS_T) */
9872 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9879 int is_active_thread;
9882 if (note->descsz < 728)
9885 if (! CONST_STRNEQ (note->namedata, "win32"))
9888 type = bfd_get_32 (abfd, note->descdata);
9892 case 1 /* NOTE_INFO_PROCESS */:
9893 /* FIXME: need to add ->core->command. */
9894 /* process_info.pid */
9895 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9896 /* process_info.signal */
9897 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9900 case 2 /* NOTE_INFO_THREAD */:
9901 /* Make a ".reg/999" section. */
9902 /* thread_info.tid */
9903 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9905 len = strlen (buf) + 1;
9906 name = (char *) bfd_alloc (abfd, len);
9910 memcpy (name, buf, len);
9912 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9916 /* sizeof (thread_info.thread_context) */
9918 /* offsetof (thread_info.thread_context) */
9919 sect->filepos = note->descpos + 12;
9920 sect->alignment_power = 2;
9922 /* thread_info.is_active_thread */
9923 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9925 if (is_active_thread)
9926 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9930 case 3 /* NOTE_INFO_MODULE */:
9931 /* Make a ".module/xxxxxxxx" section. */
9932 /* module_info.base_address */
9933 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9934 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9936 len = strlen (buf) + 1;
9937 name = (char *) bfd_alloc (abfd, len);
9941 memcpy (name, buf, len);
9943 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9948 sect->size = note->descsz;
9949 sect->filepos = note->descpos;
9950 sect->alignment_power = 2;
9961 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9963 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9971 if (bed->elf_backend_grok_prstatus)
9972 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
9974 #if defined (HAVE_PRSTATUS_T)
9975 return elfcore_grok_prstatus (abfd, note);
9980 #if defined (HAVE_PSTATUS_T)
9982 return elfcore_grok_pstatus (abfd, note);
9985 #if defined (HAVE_LWPSTATUS_T)
9987 return elfcore_grok_lwpstatus (abfd, note);
9990 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9991 return elfcore_grok_prfpreg (abfd, note);
9993 case NT_WIN32PSTATUS:
9994 return elfcore_grok_win32pstatus (abfd, note);
9996 case NT_PRXFPREG: /* Linux SSE extension */
9997 if (note->namesz == 6
9998 && strcmp (note->namedata, "LINUX") == 0)
9999 return elfcore_grok_prxfpreg (abfd, note);
10003 case NT_X86_XSTATE: /* Linux XSAVE extension */
10004 if (note->namesz == 6
10005 && strcmp (note->namedata, "LINUX") == 0)
10006 return elfcore_grok_xstatereg (abfd, note);
10011 if (note->namesz == 6
10012 && strcmp (note->namedata, "LINUX") == 0)
10013 return elfcore_grok_ppc_vmx (abfd, note);
10018 if (note->namesz == 6
10019 && strcmp (note->namedata, "LINUX") == 0)
10020 return elfcore_grok_ppc_vsx (abfd, note);
10025 if (note->namesz == 6
10026 && strcmp (note->namedata, "LINUX") == 0)
10027 return elfcore_grok_ppc_tar (abfd, note);
10032 if (note->namesz == 6
10033 && strcmp (note->namedata, "LINUX") == 0)
10034 return elfcore_grok_ppc_ppr (abfd, note);
10039 if (note->namesz == 6
10040 && strcmp (note->namedata, "LINUX") == 0)
10041 return elfcore_grok_ppc_dscr (abfd, note);
10046 if (note->namesz == 6
10047 && strcmp (note->namedata, "LINUX") == 0)
10048 return elfcore_grok_ppc_ebb (abfd, note);
10053 if (note->namesz == 6
10054 && strcmp (note->namedata, "LINUX") == 0)
10055 return elfcore_grok_ppc_pmu (abfd, note);
10059 case NT_PPC_TM_CGPR:
10060 if (note->namesz == 6
10061 && strcmp (note->namedata, "LINUX") == 0)
10062 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10066 case NT_PPC_TM_CFPR:
10067 if (note->namesz == 6
10068 && strcmp (note->namedata, "LINUX") == 0)
10069 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10073 case NT_PPC_TM_CVMX:
10074 if (note->namesz == 6
10075 && strcmp (note->namedata, "LINUX") == 0)
10076 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10080 case NT_PPC_TM_CVSX:
10081 if (note->namesz == 6
10082 && strcmp (note->namedata, "LINUX") == 0)
10083 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10087 case NT_PPC_TM_SPR:
10088 if (note->namesz == 6
10089 && strcmp (note->namedata, "LINUX") == 0)
10090 return elfcore_grok_ppc_tm_spr (abfd, note);
10094 case NT_PPC_TM_CTAR:
10095 if (note->namesz == 6
10096 && strcmp (note->namedata, "LINUX") == 0)
10097 return elfcore_grok_ppc_tm_ctar (abfd, note);
10101 case NT_PPC_TM_CPPR:
10102 if (note->namesz == 6
10103 && strcmp (note->namedata, "LINUX") == 0)
10104 return elfcore_grok_ppc_tm_cppr (abfd, note);
10108 case NT_PPC_TM_CDSCR:
10109 if (note->namesz == 6
10110 && strcmp (note->namedata, "LINUX") == 0)
10111 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10115 case NT_S390_HIGH_GPRS:
10116 if (note->namesz == 6
10117 && strcmp (note->namedata, "LINUX") == 0)
10118 return elfcore_grok_s390_high_gprs (abfd, note);
10122 case NT_S390_TIMER:
10123 if (note->namesz == 6
10124 && strcmp (note->namedata, "LINUX") == 0)
10125 return elfcore_grok_s390_timer (abfd, note);
10129 case NT_S390_TODCMP:
10130 if (note->namesz == 6
10131 && strcmp (note->namedata, "LINUX") == 0)
10132 return elfcore_grok_s390_todcmp (abfd, note);
10136 case NT_S390_TODPREG:
10137 if (note->namesz == 6
10138 && strcmp (note->namedata, "LINUX") == 0)
10139 return elfcore_grok_s390_todpreg (abfd, note);
10144 if (note->namesz == 6
10145 && strcmp (note->namedata, "LINUX") == 0)
10146 return elfcore_grok_s390_ctrs (abfd, note);
10150 case NT_S390_PREFIX:
10151 if (note->namesz == 6
10152 && strcmp (note->namedata, "LINUX") == 0)
10153 return elfcore_grok_s390_prefix (abfd, note);
10157 case NT_S390_LAST_BREAK:
10158 if (note->namesz == 6
10159 && strcmp (note->namedata, "LINUX") == 0)
10160 return elfcore_grok_s390_last_break (abfd, note);
10164 case NT_S390_SYSTEM_CALL:
10165 if (note->namesz == 6
10166 && strcmp (note->namedata, "LINUX") == 0)
10167 return elfcore_grok_s390_system_call (abfd, note);
10172 if (note->namesz == 6
10173 && strcmp (note->namedata, "LINUX") == 0)
10174 return elfcore_grok_s390_tdb (abfd, note);
10178 case NT_S390_VXRS_LOW:
10179 if (note->namesz == 6
10180 && strcmp (note->namedata, "LINUX") == 0)
10181 return elfcore_grok_s390_vxrs_low (abfd, note);
10185 case NT_S390_VXRS_HIGH:
10186 if (note->namesz == 6
10187 && strcmp (note->namedata, "LINUX") == 0)
10188 return elfcore_grok_s390_vxrs_high (abfd, note);
10192 case NT_S390_GS_CB:
10193 if (note->namesz == 6
10194 && strcmp (note->namedata, "LINUX") == 0)
10195 return elfcore_grok_s390_gs_cb (abfd, note);
10199 case NT_S390_GS_BC:
10200 if (note->namesz == 6
10201 && strcmp (note->namedata, "LINUX") == 0)
10202 return elfcore_grok_s390_gs_bc (abfd, note);
10207 if (note->namesz == 6
10208 && strcmp (note->namedata, "LINUX") == 0)
10209 return elfcore_grok_arm_vfp (abfd, note);
10214 if (note->namesz == 6
10215 && strcmp (note->namedata, "LINUX") == 0)
10216 return elfcore_grok_aarch_tls (abfd, note);
10220 case NT_ARM_HW_BREAK:
10221 if (note->namesz == 6
10222 && strcmp (note->namedata, "LINUX") == 0)
10223 return elfcore_grok_aarch_hw_break (abfd, note);
10227 case NT_ARM_HW_WATCH:
10228 if (note->namesz == 6
10229 && strcmp (note->namedata, "LINUX") == 0)
10230 return elfcore_grok_aarch_hw_watch (abfd, note);
10235 if (note->namesz == 6
10236 && strcmp (note->namedata, "LINUX") == 0)
10237 return elfcore_grok_aarch_sve (abfd, note);
10241 case NT_ARM_PAC_MASK:
10242 if (note->namesz == 6
10243 && strcmp (note->namedata, "LINUX") == 0)
10244 return elfcore_grok_aarch_pauth (abfd, note);
10250 if (bed->elf_backend_grok_psinfo)
10251 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10253 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10254 return elfcore_grok_psinfo (abfd, note);
10260 return elfcore_make_auxv_note_section (abfd, note, 0);
10263 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10267 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10274 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10276 struct bfd_build_id* build_id;
10278 if (note->descsz == 0)
10281 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10282 if (build_id == NULL)
10285 build_id->size = note->descsz;
10286 memcpy (build_id->data, note->descdata, note->descsz);
10287 abfd->build_id = build_id;
10293 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10295 switch (note->type)
10300 case NT_GNU_PROPERTY_TYPE_0:
10301 return _bfd_elf_parse_gnu_properties (abfd, note);
10303 case NT_GNU_BUILD_ID:
10304 return elfobj_grok_gnu_build_id (abfd, note);
10309 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10311 struct sdt_note *cur =
10312 (struct sdt_note *) bfd_alloc (abfd,
10313 sizeof (struct sdt_note) + note->descsz);
10315 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10316 cur->size = (bfd_size_type) note->descsz;
10317 memcpy (cur->data, note->descdata, note->descsz);
10319 elf_tdata (abfd)->sdt_note_head = cur;
10325 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10327 switch (note->type)
10330 return elfobj_grok_stapsdt_note_1 (abfd, note);
10338 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10342 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10345 if (note->descsz < 108)
10350 if (note->descsz < 120)
10358 /* Check for version 1 in pr_version. */
10359 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10364 /* Skip over pr_psinfosz. */
10365 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10369 offset += 4; /* Padding before pr_psinfosz. */
10373 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10374 elf_tdata (abfd)->core->program
10375 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10378 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10379 elf_tdata (abfd)->core->command
10380 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10383 /* Padding before pr_pid. */
10386 /* The pr_pid field was added in version "1a". */
10387 if (note->descsz < offset + 4)
10390 elf_tdata (abfd)->core->pid
10391 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10397 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10403 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10404 Also compute minimum size of this note. */
10405 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10409 min_size = offset + (4 * 2) + 4 + 4 + 4;
10413 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10414 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10421 if (note->descsz < min_size)
10424 /* Check for version 1 in pr_version. */
10425 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10428 /* Extract size of pr_reg from pr_gregsetsz. */
10429 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10430 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10432 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10437 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10441 /* Skip over pr_osreldate. */
10444 /* Read signal from pr_cursig. */
10445 if (elf_tdata (abfd)->core->signal == 0)
10446 elf_tdata (abfd)->core->signal
10447 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10450 /* Read TID from pr_pid. */
10451 elf_tdata (abfd)->core->lwpid
10452 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10455 /* Padding before pr_reg. */
10456 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10459 /* Make sure that there is enough data remaining in the note. */
10460 if ((note->descsz - offset) < size)
10463 /* Make a ".reg/999" section and a ".reg" section. */
10464 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10465 size, note->descpos + offset);
10469 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10473 switch (note->type)
10476 if (bed->elf_backend_grok_freebsd_prstatus)
10477 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10479 return elfcore_grok_freebsd_prstatus (abfd, note);
10482 return elfcore_grok_prfpreg (abfd, note);
10485 return elfcore_grok_freebsd_psinfo (abfd, note);
10487 case NT_FREEBSD_THRMISC:
10488 if (note->namesz == 8)
10489 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10493 case NT_FREEBSD_PROCSTAT_PROC:
10494 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10497 case NT_FREEBSD_PROCSTAT_FILES:
10498 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10501 case NT_FREEBSD_PROCSTAT_VMMAP:
10502 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10505 case NT_FREEBSD_PROCSTAT_AUXV:
10506 return elfcore_make_auxv_note_section (abfd, note, 4);
10508 case NT_X86_XSTATE:
10509 if (note->namesz == 8)
10510 return elfcore_grok_xstatereg (abfd, note);
10514 case NT_FREEBSD_PTLWPINFO:
10515 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10519 return elfcore_grok_arm_vfp (abfd, note);
10527 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10531 cp = strchr (note->namedata, '@');
10534 *lwpidp = atoi(cp + 1);
10541 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10543 if (note->descsz <= 0x7c + 31)
10546 /* Signal number at offset 0x08. */
10547 elf_tdata (abfd)->core->signal
10548 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10550 /* Process ID at offset 0x50. */
10551 elf_tdata (abfd)->core->pid
10552 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10554 /* Command name at 0x7c (max 32 bytes, including nul). */
10555 elf_tdata (abfd)->core->command
10556 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10558 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10563 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10567 if (elfcore_netbsd_get_lwpid (note, &lwp))
10568 elf_tdata (abfd)->core->lwpid = lwp;
10570 switch (note->type)
10572 case NT_NETBSDCORE_PROCINFO:
10573 /* NetBSD-specific core "procinfo". Note that we expect to
10574 find this note before any of the others, which is fine,
10575 since the kernel writes this note out first when it
10576 creates a core file. */
10577 return elfcore_grok_netbsd_procinfo (abfd, note);
10578 #ifdef NT_NETBSDCORE_AUXV
10579 case NT_NETBSDCORE_AUXV:
10580 /* NetBSD-specific Elf Auxiliary Vector data. */
10581 return elfcore_make_auxv_note_section (abfd, note, 4);
10587 /* As of March 2017 there are no other machine-independent notes
10588 defined for NetBSD core files. If the note type is less
10589 than the start of the machine-dependent note types, we don't
10592 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10596 switch (bfd_get_arch (abfd))
10598 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10599 PT_GETFPREGS == mach+2. */
10601 case bfd_arch_alpha:
10602 case bfd_arch_sparc:
10603 switch (note->type)
10605 case NT_NETBSDCORE_FIRSTMACH+0:
10606 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10608 case NT_NETBSDCORE_FIRSTMACH+2:
10609 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10615 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10616 There's also old PT___GETREGS40 == mach + 1 for old reg
10617 structure which lacks GBR. */
10620 switch (note->type)
10622 case NT_NETBSDCORE_FIRSTMACH+3:
10623 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10625 case NT_NETBSDCORE_FIRSTMACH+5:
10626 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10632 /* On all other arch's, PT_GETREGS == mach+1 and
10633 PT_GETFPREGS == mach+3. */
10636 switch (note->type)
10638 case NT_NETBSDCORE_FIRSTMACH+1:
10639 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10641 case NT_NETBSDCORE_FIRSTMACH+3:
10642 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10652 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10654 if (note->descsz <= 0x48 + 31)
10657 /* Signal number at offset 0x08. */
10658 elf_tdata (abfd)->core->signal
10659 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10661 /* Process ID at offset 0x20. */
10662 elf_tdata (abfd)->core->pid
10663 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10665 /* Command name at 0x48 (max 32 bytes, including nul). */
10666 elf_tdata (abfd)->core->command
10667 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10673 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10675 if (note->type == NT_OPENBSD_PROCINFO)
10676 return elfcore_grok_openbsd_procinfo (abfd, note);
10678 if (note->type == NT_OPENBSD_REGS)
10679 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10681 if (note->type == NT_OPENBSD_FPREGS)
10682 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10684 if (note->type == NT_OPENBSD_XFPREGS)
10685 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10687 if (note->type == NT_OPENBSD_AUXV)
10688 return elfcore_make_auxv_note_section (abfd, note, 0);
10690 if (note->type == NT_OPENBSD_WCOOKIE)
10692 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10697 sect->size = note->descsz;
10698 sect->filepos = note->descpos;
10699 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10708 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10710 void *ddata = note->descdata;
10717 if (note->descsz < 16)
10720 /* nto_procfs_status 'pid' field is at offset 0. */
10721 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10723 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10724 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10726 /* nto_procfs_status 'flags' field is at offset 8. */
10727 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10729 /* nto_procfs_status 'what' field is at offset 14. */
10730 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10732 elf_tdata (abfd)->core->signal = sig;
10733 elf_tdata (abfd)->core->lwpid = *tid;
10736 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10737 do not come from signals so we make sure we set the current
10738 thread just in case. */
10739 if (flags & 0x00000080)
10740 elf_tdata (abfd)->core->lwpid = *tid;
10742 /* Make a ".qnx_core_status/%d" section. */
10743 sprintf (buf, ".qnx_core_status/%ld", *tid);
10745 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10748 strcpy (name, buf);
10750 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10754 sect->size = note->descsz;
10755 sect->filepos = note->descpos;
10756 sect->alignment_power = 2;
10758 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10762 elfcore_grok_nto_regs (bfd *abfd,
10763 Elf_Internal_Note *note,
10771 /* Make a "(base)/%d" section. */
10772 sprintf (buf, "%s/%ld", base, tid);
10774 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10777 strcpy (name, buf);
10779 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10783 sect->size = note->descsz;
10784 sect->filepos = note->descpos;
10785 sect->alignment_power = 2;
10787 /* This is the current thread. */
10788 if (elf_tdata (abfd)->core->lwpid == tid)
10789 return elfcore_maybe_make_sect (abfd, base, sect);
10794 #define BFD_QNT_CORE_INFO 7
10795 #define BFD_QNT_CORE_STATUS 8
10796 #define BFD_QNT_CORE_GREG 9
10797 #define BFD_QNT_CORE_FPREG 10
10800 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10802 /* Every GREG section has a STATUS section before it. Store the
10803 tid from the previous call to pass down to the next gregs
10805 static long tid = 1;
10807 switch (note->type)
10809 case BFD_QNT_CORE_INFO:
10810 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10811 case BFD_QNT_CORE_STATUS:
10812 return elfcore_grok_nto_status (abfd, note, &tid);
10813 case BFD_QNT_CORE_GREG:
10814 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10815 case BFD_QNT_CORE_FPREG:
10816 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10823 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10829 /* Use note name as section name. */
10830 len = note->namesz;
10831 name = (char *) bfd_alloc (abfd, len);
10834 memcpy (name, note->namedata, len);
10835 name[len - 1] = '\0';
10837 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10841 sect->size = note->descsz;
10842 sect->filepos = note->descpos;
10843 sect->alignment_power = 1;
10848 /* Function: elfcore_write_note
10851 buffer to hold note, and current size of buffer
10855 size of data for note
10857 Writes note to end of buffer. ELF64 notes are written exactly as
10858 for ELF32, despite the current (as of 2006) ELF gabi specifying
10859 that they ought to have 8-byte namesz and descsz field, and have
10860 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10863 Pointer to realloc'd buffer, *BUFSIZ updated. */
10866 elfcore_write_note (bfd *abfd,
10874 Elf_External_Note *xnp;
10881 namesz = strlen (name) + 1;
10883 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
10885 buf = (char *) realloc (buf, *bufsiz + newspace);
10888 dest = buf + *bufsiz;
10889 *bufsiz += newspace;
10890 xnp = (Elf_External_Note *) dest;
10891 H_PUT_32 (abfd, namesz, xnp->namesz);
10892 H_PUT_32 (abfd, size, xnp->descsz);
10893 H_PUT_32 (abfd, type, xnp->type);
10897 memcpy (dest, name, namesz);
10905 memcpy (dest, input, size);
10915 /* gcc-8 warns (*) on all the strncpy calls in this function about
10916 possible string truncation. The "truncation" is not a bug. We
10917 have an external representation of structs with fields that are not
10918 necessarily NULL terminated and corresponding internal
10919 representation fields that are one larger so that they can always
10920 be NULL terminated.
10921 gcc versions between 4.2 and 4.6 do not allow pragma control of
10922 diagnostics inside functions, giving a hard error if you try to use
10923 the finer control available with later versions.
10924 gcc prior to 4.2 warns about diagnostic push and pop.
10925 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10926 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10927 (*) Depending on your system header files! */
10928 #if GCC_VERSION >= 8000
10929 # pragma GCC diagnostic push
10930 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10933 elfcore_write_prpsinfo (bfd *abfd,
10937 const char *psargs)
10939 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10941 if (bed->elf_backend_write_core_note != NULL)
10944 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10945 NT_PRPSINFO, fname, psargs);
10950 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10951 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10952 if (bed->s->elfclass == ELFCLASS32)
10954 # if defined (HAVE_PSINFO32_T)
10956 int note_type = NT_PSINFO;
10959 int note_type = NT_PRPSINFO;
10962 memset (&data, 0, sizeof (data));
10963 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10964 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10965 return elfcore_write_note (abfd, buf, bufsiz,
10966 "CORE", note_type, &data, sizeof (data));
10971 # if defined (HAVE_PSINFO_T)
10973 int note_type = NT_PSINFO;
10976 int note_type = NT_PRPSINFO;
10979 memset (&data, 0, sizeof (data));
10980 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10981 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10982 return elfcore_write_note (abfd, buf, bufsiz,
10983 "CORE", note_type, &data, sizeof (data));
10985 #endif /* PSINFO_T or PRPSINFO_T */
10990 #if GCC_VERSION >= 8000
10991 # pragma GCC diagnostic pop
10995 elfcore_write_linux_prpsinfo32
10996 (bfd *abfd, char *buf, int *bufsiz,
10997 const struct elf_internal_linux_prpsinfo *prpsinfo)
10999 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11001 struct elf_external_linux_prpsinfo32_ugid16 data;
11003 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11004 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11005 &data, sizeof (data));
11009 struct elf_external_linux_prpsinfo32_ugid32 data;
11011 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11012 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11013 &data, sizeof (data));
11018 elfcore_write_linux_prpsinfo64
11019 (bfd *abfd, char *buf, int *bufsiz,
11020 const struct elf_internal_linux_prpsinfo *prpsinfo)
11022 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11024 struct elf_external_linux_prpsinfo64_ugid16 data;
11026 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11027 return elfcore_write_note (abfd, buf, bufsiz,
11028 "CORE", NT_PRPSINFO, &data, sizeof (data));
11032 struct elf_external_linux_prpsinfo64_ugid32 data;
11034 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11035 return elfcore_write_note (abfd, buf, bufsiz,
11036 "CORE", NT_PRPSINFO, &data, sizeof (data));
11041 elfcore_write_prstatus (bfd *abfd,
11048 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11050 if (bed->elf_backend_write_core_note != NULL)
11053 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11055 pid, cursig, gregs);
11060 #if defined (HAVE_PRSTATUS_T)
11061 #if defined (HAVE_PRSTATUS32_T)
11062 if (bed->s->elfclass == ELFCLASS32)
11064 prstatus32_t prstat;
11066 memset (&prstat, 0, sizeof (prstat));
11067 prstat.pr_pid = pid;
11068 prstat.pr_cursig = cursig;
11069 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11070 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11071 NT_PRSTATUS, &prstat, sizeof (prstat));
11078 memset (&prstat, 0, sizeof (prstat));
11079 prstat.pr_pid = pid;
11080 prstat.pr_cursig = cursig;
11081 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11082 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11083 NT_PRSTATUS, &prstat, sizeof (prstat));
11085 #endif /* HAVE_PRSTATUS_T */
11091 #if defined (HAVE_LWPSTATUS_T)
11093 elfcore_write_lwpstatus (bfd *abfd,
11100 lwpstatus_t lwpstat;
11101 const char *note_name = "CORE";
11103 memset (&lwpstat, 0, sizeof (lwpstat));
11104 lwpstat.pr_lwpid = pid >> 16;
11105 lwpstat.pr_cursig = cursig;
11106 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11107 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11108 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11109 #if !defined(gregs)
11110 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11111 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11113 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11114 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11117 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11118 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11120 #endif /* HAVE_LWPSTATUS_T */
11122 #if defined (HAVE_PSTATUS_T)
11124 elfcore_write_pstatus (bfd *abfd,
11128 int cursig ATTRIBUTE_UNUSED,
11129 const void *gregs ATTRIBUTE_UNUSED)
11131 const char *note_name = "CORE";
11132 #if defined (HAVE_PSTATUS32_T)
11133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11135 if (bed->s->elfclass == ELFCLASS32)
11139 memset (&pstat, 0, sizeof (pstat));
11140 pstat.pr_pid = pid & 0xffff;
11141 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11142 NT_PSTATUS, &pstat, sizeof (pstat));
11150 memset (&pstat, 0, sizeof (pstat));
11151 pstat.pr_pid = pid & 0xffff;
11152 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11153 NT_PSTATUS, &pstat, sizeof (pstat));
11157 #endif /* HAVE_PSTATUS_T */
11160 elfcore_write_prfpreg (bfd *abfd,
11163 const void *fpregs,
11166 const char *note_name = "CORE";
11167 return elfcore_write_note (abfd, buf, bufsiz,
11168 note_name, NT_FPREGSET, fpregs, size);
11172 elfcore_write_prxfpreg (bfd *abfd,
11175 const void *xfpregs,
11178 char *note_name = "LINUX";
11179 return elfcore_write_note (abfd, buf, bufsiz,
11180 note_name, NT_PRXFPREG, xfpregs, size);
11184 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11185 const void *xfpregs, int size)
11188 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11189 note_name = "FreeBSD";
11191 note_name = "LINUX";
11192 return elfcore_write_note (abfd, buf, bufsiz,
11193 note_name, NT_X86_XSTATE, xfpregs, size);
11197 elfcore_write_ppc_vmx (bfd *abfd,
11200 const void *ppc_vmx,
11203 char *note_name = "LINUX";
11204 return elfcore_write_note (abfd, buf, bufsiz,
11205 note_name, NT_PPC_VMX, ppc_vmx, size);
11209 elfcore_write_ppc_vsx (bfd *abfd,
11212 const void *ppc_vsx,
11215 char *note_name = "LINUX";
11216 return elfcore_write_note (abfd, buf, bufsiz,
11217 note_name, NT_PPC_VSX, ppc_vsx, size);
11221 elfcore_write_ppc_tar (bfd *abfd,
11224 const void *ppc_tar,
11227 char *note_name = "LINUX";
11228 return elfcore_write_note (abfd, buf, bufsiz,
11229 note_name, NT_PPC_TAR, ppc_tar, size);
11233 elfcore_write_ppc_ppr (bfd *abfd,
11236 const void *ppc_ppr,
11239 char *note_name = "LINUX";
11240 return elfcore_write_note (abfd, buf, bufsiz,
11241 note_name, NT_PPC_PPR, ppc_ppr, size);
11245 elfcore_write_ppc_dscr (bfd *abfd,
11248 const void *ppc_dscr,
11251 char *note_name = "LINUX";
11252 return elfcore_write_note (abfd, buf, bufsiz,
11253 note_name, NT_PPC_DSCR, ppc_dscr, size);
11257 elfcore_write_ppc_ebb (bfd *abfd,
11260 const void *ppc_ebb,
11263 char *note_name = "LINUX";
11264 return elfcore_write_note (abfd, buf, bufsiz,
11265 note_name, NT_PPC_EBB, ppc_ebb, size);
11269 elfcore_write_ppc_pmu (bfd *abfd,
11272 const void *ppc_pmu,
11275 char *note_name = "LINUX";
11276 return elfcore_write_note (abfd, buf, bufsiz,
11277 note_name, NT_PPC_PMU, ppc_pmu, size);
11281 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11284 const void *ppc_tm_cgpr,
11287 char *note_name = "LINUX";
11288 return elfcore_write_note (abfd, buf, bufsiz,
11289 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11293 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11296 const void *ppc_tm_cfpr,
11299 char *note_name = "LINUX";
11300 return elfcore_write_note (abfd, buf, bufsiz,
11301 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11305 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11308 const void *ppc_tm_cvmx,
11311 char *note_name = "LINUX";
11312 return elfcore_write_note (abfd, buf, bufsiz,
11313 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11317 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11320 const void *ppc_tm_cvsx,
11323 char *note_name = "LINUX";
11324 return elfcore_write_note (abfd, buf, bufsiz,
11325 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11329 elfcore_write_ppc_tm_spr (bfd *abfd,
11332 const void *ppc_tm_spr,
11335 char *note_name = "LINUX";
11336 return elfcore_write_note (abfd, buf, bufsiz,
11337 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11341 elfcore_write_ppc_tm_ctar (bfd *abfd,
11344 const void *ppc_tm_ctar,
11347 char *note_name = "LINUX";
11348 return elfcore_write_note (abfd, buf, bufsiz,
11349 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11353 elfcore_write_ppc_tm_cppr (bfd *abfd,
11356 const void *ppc_tm_cppr,
11359 char *note_name = "LINUX";
11360 return elfcore_write_note (abfd, buf, bufsiz,
11361 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11365 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11368 const void *ppc_tm_cdscr,
11371 char *note_name = "LINUX";
11372 return elfcore_write_note (abfd, buf, bufsiz,
11373 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11377 elfcore_write_s390_high_gprs (bfd *abfd,
11380 const void *s390_high_gprs,
11383 char *note_name = "LINUX";
11384 return elfcore_write_note (abfd, buf, bufsiz,
11385 note_name, NT_S390_HIGH_GPRS,
11386 s390_high_gprs, size);
11390 elfcore_write_s390_timer (bfd *abfd,
11393 const void *s390_timer,
11396 char *note_name = "LINUX";
11397 return elfcore_write_note (abfd, buf, bufsiz,
11398 note_name, NT_S390_TIMER, s390_timer, size);
11402 elfcore_write_s390_todcmp (bfd *abfd,
11405 const void *s390_todcmp,
11408 char *note_name = "LINUX";
11409 return elfcore_write_note (abfd, buf, bufsiz,
11410 note_name, NT_S390_TODCMP, s390_todcmp, size);
11414 elfcore_write_s390_todpreg (bfd *abfd,
11417 const void *s390_todpreg,
11420 char *note_name = "LINUX";
11421 return elfcore_write_note (abfd, buf, bufsiz,
11422 note_name, NT_S390_TODPREG, s390_todpreg, size);
11426 elfcore_write_s390_ctrs (bfd *abfd,
11429 const void *s390_ctrs,
11432 char *note_name = "LINUX";
11433 return elfcore_write_note (abfd, buf, bufsiz,
11434 note_name, NT_S390_CTRS, s390_ctrs, size);
11438 elfcore_write_s390_prefix (bfd *abfd,
11441 const void *s390_prefix,
11444 char *note_name = "LINUX";
11445 return elfcore_write_note (abfd, buf, bufsiz,
11446 note_name, NT_S390_PREFIX, s390_prefix, size);
11450 elfcore_write_s390_last_break (bfd *abfd,
11453 const void *s390_last_break,
11456 char *note_name = "LINUX";
11457 return elfcore_write_note (abfd, buf, bufsiz,
11458 note_name, NT_S390_LAST_BREAK,
11459 s390_last_break, size);
11463 elfcore_write_s390_system_call (bfd *abfd,
11466 const void *s390_system_call,
11469 char *note_name = "LINUX";
11470 return elfcore_write_note (abfd, buf, bufsiz,
11471 note_name, NT_S390_SYSTEM_CALL,
11472 s390_system_call, size);
11476 elfcore_write_s390_tdb (bfd *abfd,
11479 const void *s390_tdb,
11482 char *note_name = "LINUX";
11483 return elfcore_write_note (abfd, buf, bufsiz,
11484 note_name, NT_S390_TDB, s390_tdb, size);
11488 elfcore_write_s390_vxrs_low (bfd *abfd,
11491 const void *s390_vxrs_low,
11494 char *note_name = "LINUX";
11495 return elfcore_write_note (abfd, buf, bufsiz,
11496 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11500 elfcore_write_s390_vxrs_high (bfd *abfd,
11503 const void *s390_vxrs_high,
11506 char *note_name = "LINUX";
11507 return elfcore_write_note (abfd, buf, bufsiz,
11508 note_name, NT_S390_VXRS_HIGH,
11509 s390_vxrs_high, size);
11513 elfcore_write_s390_gs_cb (bfd *abfd,
11516 const void *s390_gs_cb,
11519 char *note_name = "LINUX";
11520 return elfcore_write_note (abfd, buf, bufsiz,
11521 note_name, NT_S390_GS_CB,
11526 elfcore_write_s390_gs_bc (bfd *abfd,
11529 const void *s390_gs_bc,
11532 char *note_name = "LINUX";
11533 return elfcore_write_note (abfd, buf, bufsiz,
11534 note_name, NT_S390_GS_BC,
11539 elfcore_write_arm_vfp (bfd *abfd,
11542 const void *arm_vfp,
11545 char *note_name = "LINUX";
11546 return elfcore_write_note (abfd, buf, bufsiz,
11547 note_name, NT_ARM_VFP, arm_vfp, size);
11551 elfcore_write_aarch_tls (bfd *abfd,
11554 const void *aarch_tls,
11557 char *note_name = "LINUX";
11558 return elfcore_write_note (abfd, buf, bufsiz,
11559 note_name, NT_ARM_TLS, aarch_tls, size);
11563 elfcore_write_aarch_hw_break (bfd *abfd,
11566 const void *aarch_hw_break,
11569 char *note_name = "LINUX";
11570 return elfcore_write_note (abfd, buf, bufsiz,
11571 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11575 elfcore_write_aarch_hw_watch (bfd *abfd,
11578 const void *aarch_hw_watch,
11581 char *note_name = "LINUX";
11582 return elfcore_write_note (abfd, buf, bufsiz,
11583 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11587 elfcore_write_aarch_sve (bfd *abfd,
11590 const void *aarch_sve,
11593 char *note_name = "LINUX";
11594 return elfcore_write_note (abfd, buf, bufsiz,
11595 note_name, NT_ARM_SVE, aarch_sve, size);
11599 elfcore_write_aarch_pauth (bfd *abfd,
11602 const void *aarch_pauth,
11605 char *note_name = "LINUX";
11606 return elfcore_write_note (abfd, buf, bufsiz,
11607 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11611 elfcore_write_register_note (bfd *abfd,
11614 const char *section,
11618 if (strcmp (section, ".reg2") == 0)
11619 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11620 if (strcmp (section, ".reg-xfp") == 0)
11621 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
11622 if (strcmp (section, ".reg-xstate") == 0)
11623 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
11624 if (strcmp (section, ".reg-ppc-vmx") == 0)
11625 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
11626 if (strcmp (section, ".reg-ppc-vsx") == 0)
11627 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
11628 if (strcmp (section, ".reg-ppc-tar") == 0)
11629 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11630 if (strcmp (section, ".reg-ppc-ppr") == 0)
11631 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11632 if (strcmp (section, ".reg-ppc-dscr") == 0)
11633 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11634 if (strcmp (section, ".reg-ppc-ebb") == 0)
11635 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11636 if (strcmp (section, ".reg-ppc-pmu") == 0)
11637 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11638 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11639 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11640 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11641 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11642 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11643 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11644 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11645 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11646 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11647 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11648 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11649 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11650 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11651 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11652 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11653 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
11654 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11655 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
11656 if (strcmp (section, ".reg-s390-timer") == 0)
11657 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11658 if (strcmp (section, ".reg-s390-todcmp") == 0)
11659 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11660 if (strcmp (section, ".reg-s390-todpreg") == 0)
11661 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11662 if (strcmp (section, ".reg-s390-ctrs") == 0)
11663 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11664 if (strcmp (section, ".reg-s390-prefix") == 0)
11665 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
11666 if (strcmp (section, ".reg-s390-last-break") == 0)
11667 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11668 if (strcmp (section, ".reg-s390-system-call") == 0)
11669 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
11670 if (strcmp (section, ".reg-s390-tdb") == 0)
11671 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
11672 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11673 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11674 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11675 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
11676 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11677 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11678 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11679 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
11680 if (strcmp (section, ".reg-arm-vfp") == 0)
11681 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
11682 if (strcmp (section, ".reg-aarch-tls") == 0)
11683 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11684 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11685 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11686 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11687 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
11688 if (strcmp (section, ".reg-aarch-sve") == 0)
11689 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
11690 if (strcmp (section, ".reg-aarch-pauth") == 0)
11691 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
11696 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11701 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11702 gABI specifies that PT_NOTE alignment should be aligned to 4
11703 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11704 align is less than 4, we use 4 byte alignment. */
11707 if (align != 4 && align != 8)
11711 while (p < buf + size)
11713 Elf_External_Note *xnp = (Elf_External_Note *) p;
11714 Elf_Internal_Note in;
11716 if (offsetof (Elf_External_Note, name) > buf - p + size)
11719 in.type = H_GET_32 (abfd, xnp->type);
11721 in.namesz = H_GET_32 (abfd, xnp->namesz);
11722 in.namedata = xnp->name;
11723 if (in.namesz > buf - in.namedata + size)
11726 in.descsz = H_GET_32 (abfd, xnp->descsz);
11727 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
11728 in.descpos = offset + (in.descdata - buf);
11730 && (in.descdata >= buf + size
11731 || in.descsz > buf - in.descdata + size))
11734 switch (bfd_get_format (abfd))
11741 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11744 const char * string;
11746 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
11750 GROKER_ELEMENT ("", elfcore_grok_note),
11751 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11752 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11753 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11754 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11755 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
11757 #undef GROKER_ELEMENT
11760 for (i = ARRAY_SIZE (grokers); i--;)
11762 if (in.namesz >= grokers[i].len
11763 && strncmp (in.namedata, grokers[i].string,
11764 grokers[i].len) == 0)
11766 if (! grokers[i].func (abfd, & in))
11775 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11777 if (! elfobj_grok_gnu_note (abfd, &in))
11780 else if (in.namesz == sizeof "stapsdt"
11781 && strcmp (in.namedata, "stapsdt") == 0)
11783 if (! elfobj_grok_stapsdt_note (abfd, &in))
11789 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
11796 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11801 if (size == 0 || (size + 1) == 0)
11804 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11807 buf = (char *) bfd_malloc (size + 1);
11811 /* PR 17512: file: ec08f814
11812 0-termintate the buffer so that string searches will not overflow. */
11815 if (bfd_bread (buf, size, abfd) != size
11816 || !elf_parse_notes (abfd, buf, size, offset, align))
11826 /* Providing external access to the ELF program header table. */
11828 /* Return an upper bound on the number of bytes required to store a
11829 copy of ABFD's program header table entries. Return -1 if an error
11830 occurs; bfd_get_error will return an appropriate code. */
11833 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11835 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11837 bfd_set_error (bfd_error_wrong_format);
11841 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11844 /* Copy ABFD's program header table entries to *PHDRS. The entries
11845 will be stored as an array of Elf_Internal_Phdr structures, as
11846 defined in include/elf/internal.h. To find out how large the
11847 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11849 Return the number of program header table entries read, or -1 if an
11850 error occurs; bfd_get_error will return an appropriate code. */
11853 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11857 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11859 bfd_set_error (bfd_error_wrong_format);
11863 num_phdrs = elf_elfheader (abfd)->e_phnum;
11864 if (num_phdrs != 0)
11865 memcpy (phdrs, elf_tdata (abfd)->phdr,
11866 num_phdrs * sizeof (Elf_Internal_Phdr));
11871 enum elf_reloc_type_class
11872 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11873 const asection *rel_sec ATTRIBUTE_UNUSED,
11874 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
11876 return reloc_class_normal;
11879 /* For RELA architectures, return the relocation value for a
11880 relocation against a local symbol. */
11883 _bfd_elf_rela_local_sym (bfd *abfd,
11884 Elf_Internal_Sym *sym,
11886 Elf_Internal_Rela *rel)
11888 asection *sec = *psec;
11889 bfd_vma relocation;
11891 relocation = (sec->output_section->vma
11892 + sec->output_offset
11894 if ((sec->flags & SEC_MERGE)
11895 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
11896 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
11899 _bfd_merged_section_offset (abfd, psec,
11900 elf_section_data (sec)->sec_info,
11901 sym->st_value + rel->r_addend);
11904 /* If we have changed the section, and our original section is
11905 marked with SEC_EXCLUDE, it means that the original
11906 SEC_MERGE section has been completely subsumed in some
11907 other SEC_MERGE section. In this case, we need to leave
11908 some info around for --emit-relocs. */
11909 if ((sec->flags & SEC_EXCLUDE) != 0)
11910 sec->kept_section = *psec;
11913 rel->r_addend -= relocation;
11914 rel->r_addend += sec->output_section->vma + sec->output_offset;
11920 _bfd_elf_rel_local_sym (bfd *abfd,
11921 Elf_Internal_Sym *sym,
11925 asection *sec = *psec;
11927 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
11928 return sym->st_value + addend;
11930 return _bfd_merged_section_offset (abfd, psec,
11931 elf_section_data (sec)->sec_info,
11932 sym->st_value + addend);
11935 /* Adjust an address within a section. Given OFFSET within SEC, return
11936 the new offset within the section, based upon changes made to the
11937 section. Returns -1 if the offset is now invalid.
11938 The offset (in abnd out) is in target sized bytes, however big a
11942 _bfd_elf_section_offset (bfd *abfd,
11943 struct bfd_link_info *info,
11947 switch (sec->sec_info_type)
11949 case SEC_INFO_TYPE_STABS:
11950 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11952 case SEC_INFO_TYPE_EH_FRAME:
11953 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
11956 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11958 /* Reverse the offset. */
11959 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11960 bfd_size_type address_size = bed->s->arch_size / 8;
11962 /* address_size and sec->size are in octets. Convert
11963 to bytes before subtracting the original offset. */
11964 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
11970 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11971 reconstruct an ELF file by reading the segments out of remote memory
11972 based on the ELF file header at EHDR_VMA and the ELF program headers it
11973 points to. If not null, *LOADBASEP is filled in with the difference
11974 between the VMAs from which the segments were read, and the VMAs the
11975 file headers (and hence BFD's idea of each section's VMA) put them at.
11977 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11978 remote memory at target address VMA into the local buffer at MYADDR; it
11979 should return zero on success or an `errno' code on failure. TEMPL must
11980 be a BFD for an ELF target with the word size and byte order found in
11981 the remote memory. */
11984 bfd_elf_bfd_from_remote_memory
11987 bfd_size_type size,
11988 bfd_vma *loadbasep,
11989 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
11991 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
11992 (templ, ehdr_vma, size, loadbasep, target_read_memory);
11996 _bfd_elf_get_synthetic_symtab (bfd *abfd,
11997 long symcount ATTRIBUTE_UNUSED,
11998 asymbol **syms ATTRIBUTE_UNUSED,
12003 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12006 const char *relplt_name;
12007 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12011 Elf_Internal_Shdr *hdr;
12017 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12020 if (dynsymcount <= 0)
12023 if (!bed->plt_sym_val)
12026 relplt_name = bed->relplt_name;
12027 if (relplt_name == NULL)
12028 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12029 relplt = bfd_get_section_by_name (abfd, relplt_name);
12030 if (relplt == NULL)
12033 hdr = &elf_section_data (relplt)->this_hdr;
12034 if (hdr->sh_link != elf_dynsymtab (abfd)
12035 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12038 plt = bfd_get_section_by_name (abfd, ".plt");
12042 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12043 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
12046 count = relplt->size / hdr->sh_entsize;
12047 size = count * sizeof (asymbol);
12048 p = relplt->relocation;
12049 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12051 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12052 if (p->addend != 0)
12055 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12057 size += sizeof ("+0x") - 1 + 8;
12062 s = *ret = (asymbol *) bfd_malloc (size);
12066 names = (char *) (s + count);
12067 p = relplt->relocation;
12069 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12074 addr = bed->plt_sym_val (i, plt, p);
12075 if (addr == (bfd_vma) -1)
12078 *s = **p->sym_ptr_ptr;
12079 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12080 we are defining a symbol, ensure one of them is set. */
12081 if ((s->flags & BSF_LOCAL) == 0)
12082 s->flags |= BSF_GLOBAL;
12083 s->flags |= BSF_SYNTHETIC;
12085 s->value = addr - plt->vma;
12088 len = strlen ((*p->sym_ptr_ptr)->name);
12089 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12091 if (p->addend != 0)
12095 memcpy (names, "+0x", sizeof ("+0x") - 1);
12096 names += sizeof ("+0x") - 1;
12097 bfd_sprintf_vma (abfd, buf, p->addend);
12098 for (a = buf; *a == '0'; ++a)
12101 memcpy (names, a, len);
12104 memcpy (names, "@plt", sizeof ("@plt"));
12105 names += sizeof ("@plt");
12112 /* It is only used by x86-64 so far.
12113 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12114 but current usage would allow all of _bfd_std_section to be zero. */
12115 static const asymbol lcomm_sym
12116 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12117 asection _bfd_elf_large_com_section
12118 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12119 "LARGE_COMMON", 0, SEC_IS_COMMON);
12122 _bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED,
12123 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12128 _bfd_elf_final_write_processing (bfd *abfd)
12130 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
12132 i_ehdrp = elf_elfheader (abfd);
12134 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12135 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
12137 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12138 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12139 STB_GNU_UNIQUE binding. */
12140 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12142 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12143 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12144 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12145 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12147 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12148 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12149 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12150 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12151 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12152 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12153 bfd_set_error (bfd_error_bad_value);
12161 /* Return TRUE for ELF symbol types that represent functions.
12162 This is the default version of this function, which is sufficient for
12163 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12166 _bfd_elf_is_function_type (unsigned int type)
12168 return (type == STT_FUNC
12169 || type == STT_GNU_IFUNC);
12172 /* If the ELF symbol SYM might be a function in SEC, return the
12173 function size and set *CODE_OFF to the function's entry point,
12174 otherwise return zero. */
12177 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12180 bfd_size_type size;
12182 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
12183 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12184 || sym->section != sec)
12187 *code_off = sym->value;
12189 if (!(sym->flags & BSF_SYNTHETIC))
12190 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;