1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2018 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. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-core.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
53 static bfd_boolean prep_headers (bfd *);
54 static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
55 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
57 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
58 file_ptr offset, size_t align);
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (bfd *abfd,
68 const Elf_External_Verdef *src,
69 Elf_Internal_Verdef *dst)
71 dst->vd_version = H_GET_16 (abfd, src->vd_version);
72 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
73 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
74 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
75 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
76 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
77 dst->vd_next = H_GET_32 (abfd, src->vd_next);
80 /* Swap out a Verdef structure. */
83 _bfd_elf_swap_verdef_out (bfd *abfd,
84 const Elf_Internal_Verdef *src,
85 Elf_External_Verdef *dst)
87 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
88 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
89 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
92 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
93 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
96 /* Swap in a Verdaux structure. */
99 _bfd_elf_swap_verdaux_in (bfd *abfd,
100 const Elf_External_Verdaux *src,
101 Elf_Internal_Verdaux *dst)
103 dst->vda_name = H_GET_32 (abfd, src->vda_name);
104 dst->vda_next = H_GET_32 (abfd, src->vda_next);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (bfd *abfd,
111 const Elf_Internal_Verdaux *src,
112 Elf_External_Verdaux *dst)
114 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
115 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
118 /* Swap in a Verneed structure. */
121 _bfd_elf_swap_verneed_in (bfd *abfd,
122 const Elf_External_Verneed *src,
123 Elf_Internal_Verneed *dst)
125 dst->vn_version = H_GET_16 (abfd, src->vn_version);
126 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
127 dst->vn_file = H_GET_32 (abfd, src->vn_file);
128 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
129 dst->vn_next = H_GET_32 (abfd, src->vn_next);
132 /* Swap out a Verneed structure. */
135 _bfd_elf_swap_verneed_out (bfd *abfd,
136 const Elf_Internal_Verneed *src,
137 Elf_External_Verneed *dst)
139 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
140 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
141 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
142 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
143 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
146 /* Swap in a Vernaux structure. */
149 _bfd_elf_swap_vernaux_in (bfd *abfd,
150 const Elf_External_Vernaux *src,
151 Elf_Internal_Vernaux *dst)
153 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
154 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
155 dst->vna_other = H_GET_16 (abfd, src->vna_other);
156 dst->vna_name = H_GET_32 (abfd, src->vna_name);
157 dst->vna_next = H_GET_32 (abfd, src->vna_next);
160 /* Swap out a Vernaux structure. */
163 _bfd_elf_swap_vernaux_out (bfd *abfd,
164 const Elf_Internal_Vernaux *src,
165 Elf_External_Vernaux *dst)
167 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
168 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
169 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
170 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
171 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
174 /* Swap in a Versym structure. */
177 _bfd_elf_swap_versym_in (bfd *abfd,
178 const Elf_External_Versym *src,
179 Elf_Internal_Versym *dst)
181 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
184 /* Swap out a Versym structure. */
187 _bfd_elf_swap_versym_out (bfd *abfd,
188 const Elf_Internal_Versym *src,
189 Elf_External_Versym *dst)
191 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
194 /* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
198 bfd_elf_hash (const char *namearg)
200 const unsigned char *name = (const unsigned char *) namearg;
205 while ((ch = *name++) != '\0')
208 if ((g = (h & 0xf0000000)) != 0)
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
216 return h & 0xffffffff;
219 /* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
223 bfd_elf_gnu_hash (const char *namearg)
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 5381;
229 while ((ch = *name++) != '\0')
230 h = (h << 5) + h + ch;
231 return h & 0xffffffff;
234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
237 bfd_elf_allocate_object (bfd *abfd,
239 enum elf_target_id object_id)
241 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
242 abfd->tdata.any = bfd_zalloc (abfd, object_size);
243 if (abfd->tdata.any == NULL)
246 elf_object_id (abfd) = object_id;
247 if (abfd->direction != read_direction)
249 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
252 elf_tdata (abfd)->o = o;
253 elf_program_header_size (abfd) = (bfd_size_type) -1;
260 bfd_elf_make_object (bfd *abfd)
262 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
263 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
268 bfd_elf_mkcorefile (bfd *abfd)
270 /* I think this can be done just like an object file. */
271 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
273 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
274 return elf_tdata (abfd)->core != NULL;
278 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
280 Elf_Internal_Shdr **i_shdrp;
281 bfd_byte *shstrtab = NULL;
283 bfd_size_type shstrtabsize;
285 i_shdrp = elf_elfsections (abfd);
287 || shindex >= elf_numsections (abfd)
288 || i_shdrp[shindex] == 0)
291 shstrtab = i_shdrp[shindex]->contents;
292 if (shstrtab == NULL)
294 /* No cached one, attempt to read, and cache what we read. */
295 offset = i_shdrp[shindex]->sh_offset;
296 shstrtabsize = i_shdrp[shindex]->sh_size;
298 /* Allocate and clear an extra byte at the end, to prevent crashes
299 in case the string table is not terminated. */
300 if (shstrtabsize + 1 <= 1
301 || shstrtabsize > bfd_get_file_size (abfd)
302 || bfd_seek (abfd, offset, SEEK_SET) != 0
303 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
305 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
307 if (bfd_get_error () != bfd_error_system_call)
308 bfd_set_error (bfd_error_file_truncated);
309 bfd_release (abfd, shstrtab);
311 /* Once we've failed to read it, make sure we don't keep
312 trying. Otherwise, we'll keep allocating space for
313 the string table over and over. */
314 i_shdrp[shindex]->sh_size = 0;
317 shstrtab[shstrtabsize] = '\0';
318 i_shdrp[shindex]->contents = shstrtab;
320 return (char *) shstrtab;
324 bfd_elf_string_from_elf_section (bfd *abfd,
325 unsigned int shindex,
326 unsigned int strindex)
328 Elf_Internal_Shdr *hdr;
333 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
336 hdr = elf_elfsections (abfd)[shindex];
338 if (hdr->contents == NULL)
340 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
342 /* PR 17512: file: f057ec89. */
343 /* xgettext:c-format */
344 _bfd_error_handler (_("%pB: attempt to load strings from"
345 " a non-string section (number %d)"),
350 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
354 if (strindex >= hdr->sh_size)
356 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
358 /* xgettext:c-format */
359 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
360 abfd, strindex, (uint64_t) hdr->sh_size,
361 (shindex == shstrndx && strindex == hdr->sh_name
363 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
367 return ((char *) hdr->contents) + strindex;
370 /* Read and convert symbols to internal format.
371 SYMCOUNT specifies the number of symbols to read, starting from
372 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
373 are non-NULL, they are used to store the internal symbols, external
374 symbols, and symbol section index extensions, respectively.
375 Returns a pointer to the internal symbol buffer (malloced if necessary)
376 or NULL if there were no symbols or some kind of problem. */
379 bfd_elf_get_elf_syms (bfd *ibfd,
380 Elf_Internal_Shdr *symtab_hdr,
383 Elf_Internal_Sym *intsym_buf,
385 Elf_External_Sym_Shndx *extshndx_buf)
387 Elf_Internal_Shdr *shndx_hdr;
389 const bfd_byte *esym;
390 Elf_External_Sym_Shndx *alloc_extshndx;
391 Elf_External_Sym_Shndx *shndx;
392 Elf_Internal_Sym *alloc_intsym;
393 Elf_Internal_Sym *isym;
394 Elf_Internal_Sym *isymend;
395 const struct elf_backend_data *bed;
400 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
406 /* Normal syms might have section extension entries. */
408 if (elf_symtab_shndx_list (ibfd) != NULL)
410 elf_section_list * entry;
411 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
413 /* Find an index section that is linked to this symtab section. */
414 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
417 if (entry->hdr.sh_link >= elf_numsections (ibfd))
420 if (sections[entry->hdr.sh_link] == symtab_hdr)
422 shndx_hdr = & entry->hdr;
427 if (shndx_hdr == NULL)
429 if (symtab_hdr == & elf_symtab_hdr (ibfd))
430 /* Not really accurate, but this was how the old code used to work. */
431 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
432 /* Otherwise we do nothing. The assumption is that
433 the index table will not be needed. */
437 /* Read the symbols. */
439 alloc_extshndx = NULL;
441 bed = get_elf_backend_data (ibfd);
442 extsym_size = bed->s->sizeof_sym;
443 amt = (bfd_size_type) symcount * extsym_size;
444 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
445 if (extsym_buf == NULL)
447 alloc_ext = bfd_malloc2 (symcount, extsym_size);
448 extsym_buf = alloc_ext;
450 if (extsym_buf == NULL
451 || bfd_seek (ibfd, pos, SEEK_SET) != 0
452 || bfd_bread (extsym_buf, amt, ibfd) != amt)
458 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
462 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
463 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
464 if (extshndx_buf == NULL)
466 alloc_extshndx = (Elf_External_Sym_Shndx *)
467 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
468 extshndx_buf = alloc_extshndx;
470 if (extshndx_buf == NULL
471 || bfd_seek (ibfd, pos, SEEK_SET) != 0
472 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
479 if (intsym_buf == NULL)
481 alloc_intsym = (Elf_Internal_Sym *)
482 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
483 intsym_buf = alloc_intsym;
484 if (intsym_buf == NULL)
488 /* Convert the symbols to internal form. */
489 isymend = intsym_buf + symcount;
490 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
491 shndx = extshndx_buf;
493 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
494 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
496 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
497 /* xgettext:c-format */
498 _bfd_error_handler (_("%pB symbol number %lu references"
499 " nonexistent SHT_SYMTAB_SHNDX section"),
500 ibfd, (unsigned long) symoffset);
501 if (alloc_intsym != NULL)
508 if (alloc_ext != NULL)
510 if (alloc_extshndx != NULL)
511 free (alloc_extshndx);
516 /* Look up a symbol name. */
518 bfd_elf_sym_name (bfd *abfd,
519 Elf_Internal_Shdr *symtab_hdr,
520 Elf_Internal_Sym *isym,
524 unsigned int iname = isym->st_name;
525 unsigned int shindex = symtab_hdr->sh_link;
527 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
528 /* Check for a bogus st_shndx to avoid crashing. */
529 && isym->st_shndx < elf_numsections (abfd))
531 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
532 shindex = elf_elfheader (abfd)->e_shstrndx;
535 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
538 else if (sym_sec && *name == '\0')
539 name = bfd_section_name (abfd, sym_sec);
544 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
545 sections. The first element is the flags, the rest are section
548 typedef union elf_internal_group {
549 Elf_Internal_Shdr *shdr;
551 } Elf_Internal_Group;
553 /* Return the name of the group signature symbol. Why isn't the
554 signature just a string? */
557 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
559 Elf_Internal_Shdr *hdr;
560 unsigned char esym[sizeof (Elf64_External_Sym)];
561 Elf_External_Sym_Shndx eshndx;
562 Elf_Internal_Sym isym;
564 /* First we need to ensure the symbol table is available. Make sure
565 that it is a symbol table section. */
566 if (ghdr->sh_link >= elf_numsections (abfd))
568 hdr = elf_elfsections (abfd) [ghdr->sh_link];
569 if (hdr->sh_type != SHT_SYMTAB
570 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
573 /* Go read the symbol. */
574 hdr = &elf_tdata (abfd)->symtab_hdr;
575 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
576 &isym, esym, &eshndx) == NULL)
579 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
582 /* Set next_in_group list pointer, and group name for NEWSECT. */
585 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
587 unsigned int num_group = elf_tdata (abfd)->num_group;
589 /* If num_group is zero, read in all SHT_GROUP sections. The count
590 is set to -1 if there are no SHT_GROUP sections. */
593 unsigned int i, shnum;
595 /* First count the number of groups. If we have a SHT_GROUP
596 section with just a flag word (ie. sh_size is 4), ignore it. */
597 shnum = elf_numsections (abfd);
600 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
601 ( (shdr)->sh_type == SHT_GROUP \
602 && (shdr)->sh_size >= minsize \
603 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
604 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
606 for (i = 0; i < shnum; i++)
608 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
610 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
616 num_group = (unsigned) -1;
617 elf_tdata (abfd)->num_group = num_group;
618 elf_tdata (abfd)->group_sect_ptr = NULL;
622 /* We keep a list of elf section headers for group sections,
623 so we can find them quickly. */
626 elf_tdata (abfd)->num_group = num_group;
627 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
628 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
629 if (elf_tdata (abfd)->group_sect_ptr == NULL)
631 memset (elf_tdata (abfd)->group_sect_ptr, 0,
632 num_group * sizeof (Elf_Internal_Shdr *));
635 for (i = 0; i < shnum; i++)
637 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
639 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
642 Elf_Internal_Group *dest;
644 /* Make sure the group section has a BFD section
646 if (!bfd_section_from_shdr (abfd, i))
649 /* Add to list of sections. */
650 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
653 /* Read the raw contents. */
654 BFD_ASSERT (sizeof (*dest) >= 4);
655 amt = shdr->sh_size * sizeof (*dest) / 4;
656 shdr->contents = (unsigned char *)
657 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
658 /* PR binutils/4110: Handle corrupt group headers. */
659 if (shdr->contents == NULL)
662 /* xgettext:c-format */
663 (_("%pB: corrupt size field in group section"
664 " header: %#" PRIx64),
665 abfd, (uint64_t) shdr->sh_size);
666 bfd_set_error (bfd_error_bad_value);
671 memset (shdr->contents, 0, amt);
673 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
674 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
678 /* xgettext:c-format */
679 (_("%pB: invalid size field in group section"
680 " header: %#" PRIx64 ""),
681 abfd, (uint64_t) shdr->sh_size);
682 bfd_set_error (bfd_error_bad_value);
684 /* PR 17510: If the group contents are even
685 partially corrupt, do not allow any of the
686 contents to be used. */
687 memset (shdr->contents, 0, amt);
691 /* Translate raw contents, a flag word followed by an
692 array of elf section indices all in target byte order,
693 to the flag word followed by an array of elf section
695 src = shdr->contents + shdr->sh_size;
696 dest = (Elf_Internal_Group *) (shdr->contents + amt);
704 idx = H_GET_32 (abfd, src);
705 if (src == shdr->contents)
708 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
709 shdr->bfd_section->flags
710 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
715 dest->shdr = elf_elfsections (abfd)[idx];
716 /* PR binutils/23199: All sections in a
717 section group should be marked with
718 SHF_GROUP. But some tools generate
719 broken objects without SHF_GROUP. Fix
721 dest->shdr->sh_flags |= SHF_GROUP;
724 || dest->shdr->sh_type == SHT_GROUP)
727 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
735 /* PR 17510: Corrupt binaries might contain invalid groups. */
736 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
738 elf_tdata (abfd)->num_group = num_group;
740 /* If all groups are invalid then fail. */
743 elf_tdata (abfd)->group_sect_ptr = NULL;
744 elf_tdata (abfd)->num_group = num_group = -1;
746 (_("%pB: no valid group sections found"), abfd);
747 bfd_set_error (bfd_error_bad_value);
753 if (num_group != (unsigned) -1)
755 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
758 for (j = 0; j < num_group; j++)
760 /* Begin search from previous found group. */
761 unsigned i = (j + search_offset) % num_group;
763 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
764 Elf_Internal_Group *idx;
770 idx = (Elf_Internal_Group *) shdr->contents;
771 if (idx == NULL || shdr->sh_size < 4)
773 /* See PR 21957 for a reproducer. */
774 /* xgettext:c-format */
775 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
776 abfd, shdr->bfd_section);
777 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
778 bfd_set_error (bfd_error_bad_value);
781 n_elt = shdr->sh_size / 4;
783 /* Look through this group's sections to see if current
784 section is a member. */
786 if ((++idx)->shdr == hdr)
790 /* We are a member of this group. Go looking through
791 other members to see if any others are linked via
793 idx = (Elf_Internal_Group *) shdr->contents;
794 n_elt = shdr->sh_size / 4;
796 if ((++idx)->shdr != NULL
797 && (s = idx->shdr->bfd_section) != NULL
798 && elf_next_in_group (s) != NULL)
802 /* Snarf the group name from other member, and
803 insert current section in circular list. */
804 elf_group_name (newsect) = elf_group_name (s);
805 elf_next_in_group (newsect) = elf_next_in_group (s);
806 elf_next_in_group (s) = newsect;
812 gname = group_signature (abfd, shdr);
815 elf_group_name (newsect) = gname;
817 /* Start a circular list with one element. */
818 elf_next_in_group (newsect) = newsect;
821 /* If the group section has been created, point to the
823 if (shdr->bfd_section != NULL)
824 elf_next_in_group (shdr->bfd_section) = newsect;
826 elf_tdata (abfd)->group_search_offset = i;
833 if (elf_group_name (newsect) == NULL)
835 /* xgettext:c-format */
836 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
844 _bfd_elf_setup_sections (bfd *abfd)
847 unsigned int num_group = elf_tdata (abfd)->num_group;
848 bfd_boolean result = TRUE;
851 /* Process SHF_LINK_ORDER. */
852 for (s = abfd->sections; s != NULL; s = s->next)
854 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
855 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
857 unsigned int elfsec = this_hdr->sh_link;
858 /* FIXME: The old Intel compiler and old strip/objcopy may
859 not set the sh_link or sh_info fields. Hence we could
860 get the situation where elfsec is 0. */
863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
864 if (bed->link_order_error_handler)
865 bed->link_order_error_handler
866 /* xgettext:c-format */
867 (_("%pB: warning: sh_link not set for section `%pA'"),
872 asection *linksec = NULL;
874 if (elfsec < elf_numsections (abfd))
876 this_hdr = elf_elfsections (abfd)[elfsec];
877 linksec = this_hdr->bfd_section;
881 Some strip/objcopy may leave an incorrect value in
882 sh_link. We don't want to proceed. */
886 /* xgettext:c-format */
887 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
888 s->owner, elfsec, s);
892 elf_linked_to_section (s) = linksec;
895 else if (this_hdr->sh_type == SHT_GROUP
896 && elf_next_in_group (s) == NULL)
899 /* xgettext:c-format */
900 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
901 abfd, elf_section_data (s)->this_idx);
906 /* Process section groups. */
907 if (num_group == (unsigned) -1)
910 for (i = 0; i < num_group; i++)
912 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
913 Elf_Internal_Group *idx;
916 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
917 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
920 /* xgettext:c-format */
921 (_("%pB: section group entry number %u is corrupt"),
927 idx = (Elf_Internal_Group *) shdr->contents;
928 n_elt = shdr->sh_size / 4;
934 if (idx->shdr == NULL)
936 else if (idx->shdr->bfd_section)
937 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
938 else if (idx->shdr->sh_type != SHT_RELA
939 && idx->shdr->sh_type != SHT_REL)
941 /* There are some unknown sections in the group. */
943 /* xgettext:c-format */
944 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
947 bfd_elf_string_from_elf_section (abfd,
948 (elf_elfheader (abfd)
961 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
963 return elf_next_in_group (sec) != NULL;
967 convert_debug_to_zdebug (bfd *abfd, const char *name)
969 unsigned int len = strlen (name);
970 char *new_name = bfd_alloc (abfd, len + 2);
971 if (new_name == NULL)
975 memcpy (new_name + 2, name + 1, len);
980 convert_zdebug_to_debug (bfd *abfd, const char *name)
982 unsigned int len = strlen (name);
983 char *new_name = bfd_alloc (abfd, len);
984 if (new_name == NULL)
987 memcpy (new_name + 1, name + 2, len - 1);
991 /* Make a BFD section from an ELF section. We store a pointer to the
992 BFD section in the bfd_section field of the header. */
995 _bfd_elf_make_section_from_shdr (bfd *abfd,
996 Elf_Internal_Shdr *hdr,
1002 const struct elf_backend_data *bed;
1004 if (hdr->bfd_section != NULL)
1007 newsect = bfd_make_section_anyway (abfd, name);
1008 if (newsect == NULL)
1011 hdr->bfd_section = newsect;
1012 elf_section_data (newsect)->this_hdr = *hdr;
1013 elf_section_data (newsect)->this_idx = shindex;
1015 /* Always use the real type/flags. */
1016 elf_section_type (newsect) = hdr->sh_type;
1017 elf_section_flags (newsect) = hdr->sh_flags;
1019 newsect->filepos = hdr->sh_offset;
1021 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1022 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1023 || ! bfd_set_section_alignment (abfd, newsect,
1024 bfd_log2 (hdr->sh_addralign)))
1027 flags = SEC_NO_FLAGS;
1028 if (hdr->sh_type != SHT_NOBITS)
1029 flags |= SEC_HAS_CONTENTS;
1030 if (hdr->sh_type == SHT_GROUP)
1032 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1035 if (hdr->sh_type != SHT_NOBITS)
1038 if ((hdr->sh_flags & SHF_WRITE) == 0)
1039 flags |= SEC_READONLY;
1040 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1042 else if ((flags & SEC_LOAD) != 0)
1044 if ((hdr->sh_flags & SHF_MERGE) != 0)
1047 newsect->entsize = hdr->sh_entsize;
1049 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1050 flags |= SEC_STRINGS;
1051 if (hdr->sh_flags & SHF_GROUP)
1052 if (!setup_group (abfd, hdr, newsect))
1054 if ((hdr->sh_flags & SHF_TLS) != 0)
1055 flags |= SEC_THREAD_LOCAL;
1056 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1057 flags |= SEC_EXCLUDE;
1059 if ((flags & SEC_ALLOC) == 0)
1061 /* The debugging sections appear to be recognized only by name,
1062 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1063 if (name [0] == '.')
1068 p = ".debug", n = 6;
1069 else if (name[1] == 'g' && name[2] == 'n')
1070 p = ".gnu.linkonce.wi.", n = 17;
1071 else if (name[1] == 'g' && name[2] == 'd')
1072 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1073 else if (name[1] == 'l')
1075 else if (name[1] == 's')
1077 else if (name[1] == 'z')
1078 p = ".zdebug", n = 7;
1081 if (p != NULL && strncmp (name, p, n) == 0)
1082 flags |= SEC_DEBUGGING;
1086 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1087 only link a single copy of the section. This is used to support
1088 g++. g++ will emit each template expansion in its own section.
1089 The symbols will be defined as weak, so that multiple definitions
1090 are permitted. The GNU linker extension is to actually discard
1091 all but one of the sections. */
1092 if (CONST_STRNEQ (name, ".gnu.linkonce")
1093 && elf_next_in_group (newsect) == NULL)
1094 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1096 bed = get_elf_backend_data (abfd);
1097 if (bed->elf_backend_section_flags)
1098 if (! bed->elf_backend_section_flags (&flags, hdr))
1101 if (! bfd_set_section_flags (abfd, newsect, flags))
1104 /* We do not parse the PT_NOTE segments as we are interested even in the
1105 separate debug info files which may have the segments offsets corrupted.
1106 PT_NOTEs from the core files are currently not parsed using BFD. */
1107 if (hdr->sh_type == SHT_NOTE)
1111 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1114 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1115 hdr->sh_offset, hdr->sh_addralign);
1119 if ((flags & SEC_ALLOC) != 0)
1121 Elf_Internal_Phdr *phdr;
1122 unsigned int i, nload;
1124 /* Some ELF linkers produce binaries with all the program header
1125 p_paddr fields zero. If we have such a binary with more than
1126 one PT_LOAD header, then leave the section lma equal to vma
1127 so that we don't create sections with overlapping lma. */
1128 phdr = elf_tdata (abfd)->phdr;
1129 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1130 if (phdr->p_paddr != 0)
1132 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1134 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1137 phdr = elf_tdata (abfd)->phdr;
1138 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1140 if (((phdr->p_type == PT_LOAD
1141 && (hdr->sh_flags & SHF_TLS) == 0)
1142 || phdr->p_type == PT_TLS)
1143 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1145 if ((flags & SEC_LOAD) == 0)
1146 newsect->lma = (phdr->p_paddr
1147 + hdr->sh_addr - phdr->p_vaddr);
1149 /* We used to use the same adjustment for SEC_LOAD
1150 sections, but that doesn't work if the segment
1151 is packed with code from multiple VMAs.
1152 Instead we calculate the section LMA based on
1153 the segment LMA. It is assumed that the
1154 segment will contain sections with contiguous
1155 LMAs, even if the VMAs are not. */
1156 newsect->lma = (phdr->p_paddr
1157 + hdr->sh_offset - phdr->p_offset);
1159 /* With contiguous segments, we can't tell from file
1160 offsets whether a section with zero size should
1161 be placed at the end of one segment or the
1162 beginning of the next. Decide based on vaddr. */
1163 if (hdr->sh_addr >= phdr->p_vaddr
1164 && (hdr->sh_addr + hdr->sh_size
1165 <= phdr->p_vaddr + phdr->p_memsz))
1171 /* Compress/decompress DWARF debug sections with names: .debug_* and
1172 .zdebug_*, after the section flags is set. */
1173 if ((flags & SEC_DEBUGGING)
1174 && ((name[1] == 'd' && name[6] == '_')
1175 || (name[1] == 'z' && name[7] == '_')))
1177 enum { nothing, compress, decompress } action = nothing;
1178 int compression_header_size;
1179 bfd_size_type uncompressed_size;
1180 bfd_boolean compressed
1181 = bfd_is_section_compressed_with_header (abfd, newsect,
1182 &compression_header_size,
1183 &uncompressed_size);
1187 /* Compressed section. Check if we should decompress. */
1188 if ((abfd->flags & BFD_DECOMPRESS))
1189 action = decompress;
1192 /* Compress the uncompressed section or convert from/to .zdebug*
1193 section. Check if we should compress. */
1194 if (action == nothing)
1196 if (newsect->size != 0
1197 && (abfd->flags & BFD_COMPRESS)
1198 && compression_header_size >= 0
1199 && uncompressed_size > 0
1201 || ((compression_header_size > 0)
1202 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1208 if (action == compress)
1210 if (!bfd_init_section_compress_status (abfd, newsect))
1213 /* xgettext:c-format */
1214 (_("%pB: unable to initialize compress status for section %s"),
1221 if (!bfd_init_section_decompress_status (abfd, newsect))
1224 /* xgettext:c-format */
1225 (_("%pB: unable to initialize decompress status for section %s"),
1231 if (abfd->is_linker_input)
1234 && (action == decompress
1235 || (action == compress
1236 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1238 /* Convert section name from .zdebug_* to .debug_* so
1239 that linker will consider this section as a debug
1241 char *new_name = convert_zdebug_to_debug (abfd, name);
1242 if (new_name == NULL)
1244 bfd_rename_section (abfd, newsect, new_name);
1248 /* For objdump, don't rename the section. For objcopy, delay
1249 section rename to elf_fake_sections. */
1250 newsect->flags |= SEC_ELF_RENAME;
1256 const char *const bfd_elf_section_type_names[] =
1258 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1259 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1260 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1263 /* ELF relocs are against symbols. If we are producing relocatable
1264 output, and the reloc is against an external symbol, and nothing
1265 has given us any additional addend, the resulting reloc will also
1266 be against the same symbol. In such a case, we don't want to
1267 change anything about the way the reloc is handled, since it will
1268 all be done at final link time. Rather than put special case code
1269 into bfd_perform_relocation, all the reloc types use this howto
1270 function. It just short circuits the reloc if producing
1271 relocatable output against an external symbol. */
1273 bfd_reloc_status_type
1274 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1275 arelent *reloc_entry,
1277 void *data ATTRIBUTE_UNUSED,
1278 asection *input_section,
1280 char **error_message ATTRIBUTE_UNUSED)
1282 if (output_bfd != NULL
1283 && (symbol->flags & BSF_SECTION_SYM) == 0
1284 && (! reloc_entry->howto->partial_inplace
1285 || reloc_entry->addend == 0))
1287 reloc_entry->address += input_section->output_offset;
1288 return bfd_reloc_ok;
1291 return bfd_reloc_continue;
1294 /* Returns TRUE if section A matches section B.
1295 Names, addresses and links may be different, but everything else
1296 should be the same. */
1299 section_match (const Elf_Internal_Shdr * a,
1300 const Elf_Internal_Shdr * b)
1303 a->sh_type == b->sh_type
1304 && (a->sh_flags & ~ SHF_INFO_LINK)
1305 == (b->sh_flags & ~ SHF_INFO_LINK)
1306 && a->sh_addralign == b->sh_addralign
1307 && a->sh_size == b->sh_size
1308 && a->sh_entsize == b->sh_entsize
1309 /* FIXME: Check sh_addr ? */
1313 /* Find a section in OBFD that has the same characteristics
1314 as IHEADER. Return the index of this section or SHN_UNDEF if
1315 none can be found. Check's section HINT first, as this is likely
1316 to be the correct section. */
1319 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1320 const unsigned int hint)
1322 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1325 BFD_ASSERT (iheader != NULL);
1327 /* See PR 20922 for a reproducer of the NULL test. */
1328 if (hint < elf_numsections (obfd)
1329 && oheaders[hint] != NULL
1330 && section_match (oheaders[hint], iheader))
1333 for (i = 1; i < elf_numsections (obfd); i++)
1335 Elf_Internal_Shdr * oheader = oheaders[i];
1337 if (oheader == NULL)
1339 if (section_match (oheader, iheader))
1340 /* FIXME: Do we care if there is a potential for
1341 multiple matches ? */
1348 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1349 Processor specific section, based upon a matching input section.
1350 Returns TRUE upon success, FALSE otherwise. */
1353 copy_special_section_fields (const bfd *ibfd,
1355 const Elf_Internal_Shdr *iheader,
1356 Elf_Internal_Shdr *oheader,
1357 const unsigned int secnum)
1359 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1360 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1361 bfd_boolean changed = FALSE;
1362 unsigned int sh_link;
1364 if (oheader->sh_type == SHT_NOBITS)
1366 /* This is a feature for objcopy --only-keep-debug:
1367 When a section's type is changed to NOBITS, we preserve
1368 the sh_link and sh_info fields so that they can be
1369 matched up with the original.
1371 Note: Strictly speaking these assignments are wrong.
1372 The sh_link and sh_info fields should point to the
1373 relevent sections in the output BFD, which may not be in
1374 the same location as they were in the input BFD. But
1375 the whole point of this action is to preserve the
1376 original values of the sh_link and sh_info fields, so
1377 that they can be matched up with the section headers in
1378 the original file. So strictly speaking we may be
1379 creating an invalid ELF file, but it is only for a file
1380 that just contains debug info and only for sections
1381 without any contents. */
1382 if (oheader->sh_link == 0)
1383 oheader->sh_link = iheader->sh_link;
1384 if (oheader->sh_info == 0)
1385 oheader->sh_info = iheader->sh_info;
1389 /* Allow the target a chance to decide how these fields should be set. */
1390 if (bed->elf_backend_copy_special_section_fields != NULL
1391 && bed->elf_backend_copy_special_section_fields
1392 (ibfd, obfd, iheader, oheader))
1395 /* We have an iheader which might match oheader, and which has non-zero
1396 sh_info and/or sh_link fields. Attempt to follow those links and find
1397 the section in the output bfd which corresponds to the linked section
1398 in the input bfd. */
1399 if (iheader->sh_link != SHN_UNDEF)
1401 /* See PR 20931 for a reproducer. */
1402 if (iheader->sh_link >= elf_numsections (ibfd))
1405 /* xgettext:c-format */
1406 (_("%pB: invalid sh_link field (%d) in section number %d"),
1407 ibfd, iheader->sh_link, secnum);
1411 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1412 if (sh_link != SHN_UNDEF)
1414 oheader->sh_link = sh_link;
1418 /* FIXME: Should we install iheader->sh_link
1419 if we could not find a match ? */
1421 /* xgettext:c-format */
1422 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1425 if (iheader->sh_info)
1427 /* The sh_info field can hold arbitrary information, but if the
1428 SHF_LINK_INFO flag is set then it should be interpreted as a
1430 if (iheader->sh_flags & SHF_INFO_LINK)
1432 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1434 if (sh_link != SHN_UNDEF)
1435 oheader->sh_flags |= SHF_INFO_LINK;
1438 /* No idea what it means - just copy it. */
1439 sh_link = iheader->sh_info;
1441 if (sh_link != SHN_UNDEF)
1443 oheader->sh_info = sh_link;
1448 /* xgettext:c-format */
1449 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1455 /* Copy the program header and other data from one object module to
1459 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1461 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1462 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1463 const struct elf_backend_data *bed;
1466 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1467 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1470 if (!elf_flags_init (obfd))
1472 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1473 elf_flags_init (obfd) = TRUE;
1476 elf_gp (obfd) = elf_gp (ibfd);
1478 /* Also copy the EI_OSABI field. */
1479 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1480 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1482 /* If set, copy the EI_ABIVERSION field. */
1483 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1484 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1485 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1487 /* Copy object attributes. */
1488 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1490 if (iheaders == NULL || oheaders == NULL)
1493 bed = get_elf_backend_data (obfd);
1495 /* Possibly copy other fields in the section header. */
1496 for (i = 1; i < elf_numsections (obfd); i++)
1499 Elf_Internal_Shdr * oheader = oheaders[i];
1501 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1502 because of a special case need for generating separate debug info
1503 files. See below for more details. */
1505 || (oheader->sh_type != SHT_NOBITS
1506 && oheader->sh_type < SHT_LOOS))
1509 /* Ignore empty sections, and sections whose
1510 fields have already been initialised. */
1511 if (oheader->sh_size == 0
1512 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1515 /* Scan for the matching section in the input bfd.
1516 First we try for a direct mapping between the input and output sections. */
1517 for (j = 1; j < elf_numsections (ibfd); j++)
1519 const Elf_Internal_Shdr * iheader = iheaders[j];
1521 if (iheader == NULL)
1524 if (oheader->bfd_section != NULL
1525 && iheader->bfd_section != NULL
1526 && iheader->bfd_section->output_section != NULL
1527 && iheader->bfd_section->output_section == oheader->bfd_section)
1529 /* We have found a connection from the input section to the
1530 output section. Attempt to copy the header fields. If
1531 this fails then do not try any further sections - there
1532 should only be a one-to-one mapping between input and output. */
1533 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1534 j = elf_numsections (ibfd);
1539 if (j < elf_numsections (ibfd))
1542 /* That failed. So try to deduce the corresponding input section.
1543 Unfortunately we cannot compare names as the output string table
1544 is empty, so instead we check size, address and type. */
1545 for (j = 1; j < elf_numsections (ibfd); j++)
1547 const Elf_Internal_Shdr * iheader = iheaders[j];
1549 if (iheader == NULL)
1552 /* Try matching fields in the input section's header.
1553 Since --only-keep-debug turns all non-debug sections into
1554 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1556 if ((oheader->sh_type == SHT_NOBITS
1557 || iheader->sh_type == oheader->sh_type)
1558 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1559 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1560 && iheader->sh_addralign == oheader->sh_addralign
1561 && iheader->sh_entsize == oheader->sh_entsize
1562 && iheader->sh_size == oheader->sh_size
1563 && iheader->sh_addr == oheader->sh_addr
1564 && (iheader->sh_info != oheader->sh_info
1565 || iheader->sh_link != oheader->sh_link))
1567 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1572 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1574 /* Final attempt. Call the backend copy function
1575 with a NULL input section. */
1576 if (bed->elf_backend_copy_special_section_fields != NULL)
1577 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1585 get_segment_type (unsigned int p_type)
1590 case PT_NULL: pt = "NULL"; break;
1591 case PT_LOAD: pt = "LOAD"; break;
1592 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1593 case PT_INTERP: pt = "INTERP"; break;
1594 case PT_NOTE: pt = "NOTE"; break;
1595 case PT_SHLIB: pt = "SHLIB"; break;
1596 case PT_PHDR: pt = "PHDR"; break;
1597 case PT_TLS: pt = "TLS"; break;
1598 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1599 case PT_GNU_STACK: pt = "STACK"; break;
1600 case PT_GNU_RELRO: pt = "RELRO"; break;
1601 default: pt = NULL; break;
1606 /* Print out the program headers. */
1609 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1611 FILE *f = (FILE *) farg;
1612 Elf_Internal_Phdr *p;
1614 bfd_byte *dynbuf = NULL;
1616 p = elf_tdata (abfd)->phdr;
1621 fprintf (f, _("\nProgram Header:\n"));
1622 c = elf_elfheader (abfd)->e_phnum;
1623 for (i = 0; i < c; i++, p++)
1625 const char *pt = get_segment_type (p->p_type);
1630 sprintf (buf, "0x%lx", p->p_type);
1633 fprintf (f, "%8s off 0x", pt);
1634 bfd_fprintf_vma (abfd, f, p->p_offset);
1635 fprintf (f, " vaddr 0x");
1636 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1637 fprintf (f, " paddr 0x");
1638 bfd_fprintf_vma (abfd, f, p->p_paddr);
1639 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1640 fprintf (f, " filesz 0x");
1641 bfd_fprintf_vma (abfd, f, p->p_filesz);
1642 fprintf (f, " memsz 0x");
1643 bfd_fprintf_vma (abfd, f, p->p_memsz);
1644 fprintf (f, " flags %c%c%c",
1645 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1646 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1647 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1648 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1649 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1654 s = bfd_get_section_by_name (abfd, ".dynamic");
1657 unsigned int elfsec;
1658 unsigned long shlink;
1659 bfd_byte *extdyn, *extdynend;
1661 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1663 fprintf (f, _("\nDynamic Section:\n"));
1665 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1668 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1669 if (elfsec == SHN_BAD)
1671 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1673 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1674 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1677 /* PR 17512: file: 6f427532. */
1678 if (s->size < extdynsize)
1680 extdynend = extdyn + s->size;
1681 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1683 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1685 Elf_Internal_Dyn dyn;
1686 const char *name = "";
1688 bfd_boolean stringp;
1689 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1691 (*swap_dyn_in) (abfd, extdyn, &dyn);
1693 if (dyn.d_tag == DT_NULL)
1700 if (bed->elf_backend_get_target_dtag)
1701 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1703 if (!strcmp (name, ""))
1705 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1710 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1711 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1712 case DT_PLTGOT: name = "PLTGOT"; break;
1713 case DT_HASH: name = "HASH"; break;
1714 case DT_STRTAB: name = "STRTAB"; break;
1715 case DT_SYMTAB: name = "SYMTAB"; break;
1716 case DT_RELA: name = "RELA"; break;
1717 case DT_RELASZ: name = "RELASZ"; break;
1718 case DT_RELAENT: name = "RELAENT"; break;
1719 case DT_STRSZ: name = "STRSZ"; break;
1720 case DT_SYMENT: name = "SYMENT"; break;
1721 case DT_INIT: name = "INIT"; break;
1722 case DT_FINI: name = "FINI"; break;
1723 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1724 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1725 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1726 case DT_REL: name = "REL"; break;
1727 case DT_RELSZ: name = "RELSZ"; break;
1728 case DT_RELENT: name = "RELENT"; break;
1729 case DT_PLTREL: name = "PLTREL"; break;
1730 case DT_DEBUG: name = "DEBUG"; break;
1731 case DT_TEXTREL: name = "TEXTREL"; break;
1732 case DT_JMPREL: name = "JMPREL"; break;
1733 case DT_BIND_NOW: name = "BIND_NOW"; break;
1734 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1735 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1736 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1737 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1738 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1739 case DT_FLAGS: name = "FLAGS"; break;
1740 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1741 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1742 case DT_CHECKSUM: name = "CHECKSUM"; break;
1743 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1744 case DT_MOVEENT: name = "MOVEENT"; break;
1745 case DT_MOVESZ: name = "MOVESZ"; break;
1746 case DT_FEATURE: name = "FEATURE"; break;
1747 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1748 case DT_SYMINSZ: name = "SYMINSZ"; break;
1749 case DT_SYMINENT: name = "SYMINENT"; break;
1750 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1751 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1752 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1753 case DT_PLTPAD: name = "PLTPAD"; break;
1754 case DT_MOVETAB: name = "MOVETAB"; break;
1755 case DT_SYMINFO: name = "SYMINFO"; break;
1756 case DT_RELACOUNT: name = "RELACOUNT"; break;
1757 case DT_RELCOUNT: name = "RELCOUNT"; break;
1758 case DT_FLAGS_1: name = "FLAGS_1"; break;
1759 case DT_VERSYM: name = "VERSYM"; break;
1760 case DT_VERDEF: name = "VERDEF"; break;
1761 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1762 case DT_VERNEED: name = "VERNEED"; break;
1763 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1764 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1765 case DT_USED: name = "USED"; break;
1766 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1767 case DT_GNU_HASH: name = "GNU_HASH"; break;
1770 fprintf (f, " %-20s ", name);
1774 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1779 unsigned int tagv = dyn.d_un.d_val;
1781 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1784 fprintf (f, "%s", string);
1793 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1794 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1796 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1800 if (elf_dynverdef (abfd) != 0)
1802 Elf_Internal_Verdef *t;
1804 fprintf (f, _("\nVersion definitions:\n"));
1805 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1807 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1808 t->vd_flags, t->vd_hash,
1809 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1810 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1812 Elf_Internal_Verdaux *a;
1815 for (a = t->vd_auxptr->vda_nextptr;
1819 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1825 if (elf_dynverref (abfd) != 0)
1827 Elf_Internal_Verneed *t;
1829 fprintf (f, _("\nVersion References:\n"));
1830 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1832 Elf_Internal_Vernaux *a;
1834 fprintf (f, _(" required from %s:\n"),
1835 t->vn_filename ? t->vn_filename : "<corrupt>");
1836 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1837 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1838 a->vna_flags, a->vna_other,
1839 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1851 /* Get version string. */
1854 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1855 bfd_boolean *hidden)
1857 const char *version_string = NULL;
1858 if (elf_dynversym (abfd) != 0
1859 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1861 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1863 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1864 vernum &= VERSYM_VERSION;
1867 version_string = "";
1868 else if (vernum == 1
1869 && (vernum > elf_tdata (abfd)->cverdefs
1870 || (elf_tdata (abfd)->verdef[0].vd_flags
1872 version_string = "Base";
1873 else if (vernum <= elf_tdata (abfd)->cverdefs)
1875 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1878 Elf_Internal_Verneed *t;
1880 version_string = _("<corrupt>");
1881 for (t = elf_tdata (abfd)->verref;
1885 Elf_Internal_Vernaux *a;
1887 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1889 if (a->vna_other == vernum)
1891 version_string = a->vna_nodename;
1898 return version_string;
1901 /* Display ELF-specific fields of a symbol. */
1904 bfd_elf_print_symbol (bfd *abfd,
1907 bfd_print_symbol_type how)
1909 FILE *file = (FILE *) filep;
1912 case bfd_print_symbol_name:
1913 fprintf (file, "%s", symbol->name);
1915 case bfd_print_symbol_more:
1916 fprintf (file, "elf ");
1917 bfd_fprintf_vma (abfd, file, symbol->value);
1918 fprintf (file, " %x", symbol->flags);
1920 case bfd_print_symbol_all:
1922 const char *section_name;
1923 const char *name = NULL;
1924 const struct elf_backend_data *bed;
1925 unsigned char st_other;
1927 const char *version_string;
1930 section_name = symbol->section ? symbol->section->name : "(*none*)";
1932 bed = get_elf_backend_data (abfd);
1933 if (bed->elf_backend_print_symbol_all)
1934 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1938 name = symbol->name;
1939 bfd_print_symbol_vandf (abfd, file, symbol);
1942 fprintf (file, " %s\t", section_name);
1943 /* Print the "other" value for a symbol. For common symbols,
1944 we've already printed the size; now print the alignment.
1945 For other symbols, we have no specified alignment, and
1946 we've printed the address; now print the size. */
1947 if (symbol->section && bfd_is_com_section (symbol->section))
1948 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1950 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1951 bfd_fprintf_vma (abfd, file, val);
1953 /* If we have version information, print it. */
1954 version_string = _bfd_elf_get_symbol_version_string (abfd,
1960 fprintf (file, " %-11s", version_string);
1965 fprintf (file, " (%s)", version_string);
1966 for (i = 10 - strlen (version_string); i > 0; --i)
1971 /* If the st_other field is not zero, print it. */
1972 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1977 case STV_INTERNAL: fprintf (file, " .internal"); break;
1978 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1979 case STV_PROTECTED: fprintf (file, " .protected"); break;
1981 /* Some other non-defined flags are also present, so print
1983 fprintf (file, " 0x%02x", (unsigned int) st_other);
1986 fprintf (file, " %s", name);
1992 /* ELF .o/exec file reading */
1994 /* Create a new bfd section from an ELF section header. */
1997 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1999 Elf_Internal_Shdr *hdr;
2000 Elf_Internal_Ehdr *ehdr;
2001 const struct elf_backend_data *bed;
2003 bfd_boolean ret = TRUE;
2004 static bfd_boolean * sections_being_created = NULL;
2005 static bfd * sections_being_created_abfd = NULL;
2006 static unsigned int nesting = 0;
2008 if (shindex >= elf_numsections (abfd))
2013 /* PR17512: A corrupt ELF binary might contain a recursive group of
2014 sections, with each the string indices pointing to the next in the
2015 loop. Detect this here, by refusing to load a section that we are
2016 already in the process of loading. We only trigger this test if
2017 we have nested at least three sections deep as normal ELF binaries
2018 can expect to recurse at least once.
2020 FIXME: It would be better if this array was attached to the bfd,
2021 rather than being held in a static pointer. */
2023 if (sections_being_created_abfd != abfd)
2024 sections_being_created = NULL;
2025 if (sections_being_created == NULL)
2027 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2028 sections_being_created = (bfd_boolean *)
2029 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
2030 sections_being_created_abfd = abfd;
2032 if (sections_being_created [shindex])
2035 (_("%pB: warning: loop in section dependencies detected"), abfd);
2038 sections_being_created [shindex] = TRUE;
2041 hdr = elf_elfsections (abfd)[shindex];
2042 ehdr = elf_elfheader (abfd);
2043 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2048 bed = get_elf_backend_data (abfd);
2049 switch (hdr->sh_type)
2052 /* Inactive section. Throw it away. */
2055 case SHT_PROGBITS: /* Normal section with contents. */
2056 case SHT_NOBITS: /* .bss section. */
2057 case SHT_HASH: /* .hash section. */
2058 case SHT_NOTE: /* .note section. */
2059 case SHT_INIT_ARRAY: /* .init_array section. */
2060 case SHT_FINI_ARRAY: /* .fini_array section. */
2061 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2062 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2063 case SHT_GNU_HASH: /* .gnu.hash section. */
2064 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2067 case SHT_DYNAMIC: /* Dynamic linking information. */
2068 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2071 if (hdr->sh_link > elf_numsections (abfd))
2073 /* PR 10478: Accept Solaris binaries with a sh_link
2074 field set to SHN_BEFORE or SHN_AFTER. */
2075 switch (bfd_get_arch (abfd))
2078 case bfd_arch_sparc:
2079 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2080 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2082 /* Otherwise fall through. */
2087 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2089 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2091 Elf_Internal_Shdr *dynsymhdr;
2093 /* The shared libraries distributed with hpux11 have a bogus
2094 sh_link field for the ".dynamic" section. Find the
2095 string table for the ".dynsym" section instead. */
2096 if (elf_dynsymtab (abfd) != 0)
2098 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2099 hdr->sh_link = dynsymhdr->sh_link;
2103 unsigned int i, num_sec;
2105 num_sec = elf_numsections (abfd);
2106 for (i = 1; i < num_sec; i++)
2108 dynsymhdr = elf_elfsections (abfd)[i];
2109 if (dynsymhdr->sh_type == SHT_DYNSYM)
2111 hdr->sh_link = dynsymhdr->sh_link;
2119 case SHT_SYMTAB: /* A symbol table. */
2120 if (elf_onesymtab (abfd) == shindex)
2123 if (hdr->sh_entsize != bed->s->sizeof_sym)
2126 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2128 if (hdr->sh_size != 0)
2130 /* Some assemblers erroneously set sh_info to one with a
2131 zero sh_size. ld sees this as a global symbol count
2132 of (unsigned) -1. Fix it here. */
2137 /* PR 18854: A binary might contain more than one symbol table.
2138 Unusual, but possible. Warn, but continue. */
2139 if (elf_onesymtab (abfd) != 0)
2142 /* xgettext:c-format */
2143 (_("%pB: warning: multiple symbol tables detected"
2144 " - ignoring the table in section %u"),
2148 elf_onesymtab (abfd) = shindex;
2149 elf_symtab_hdr (abfd) = *hdr;
2150 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2151 abfd->flags |= HAS_SYMS;
2153 /* Sometimes a shared object will map in the symbol table. If
2154 SHF_ALLOC is set, and this is a shared object, then we also
2155 treat this section as a BFD section. We can not base the
2156 decision purely on SHF_ALLOC, because that flag is sometimes
2157 set in a relocatable object file, which would confuse the
2159 if ((hdr->sh_flags & SHF_ALLOC) != 0
2160 && (abfd->flags & DYNAMIC) != 0
2161 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2165 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2166 can't read symbols without that section loaded as well. It
2167 is most likely specified by the next section header. */
2169 elf_section_list * entry;
2170 unsigned int i, num_sec;
2172 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2173 if (entry->hdr.sh_link == shindex)
2176 num_sec = elf_numsections (abfd);
2177 for (i = shindex + 1; i < num_sec; i++)
2179 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2181 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2182 && hdr2->sh_link == shindex)
2187 for (i = 1; i < shindex; i++)
2189 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2191 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2192 && hdr2->sh_link == shindex)
2197 ret = bfd_section_from_shdr (abfd, i);
2198 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2202 case SHT_DYNSYM: /* A dynamic symbol table. */
2203 if (elf_dynsymtab (abfd) == shindex)
2206 if (hdr->sh_entsize != bed->s->sizeof_sym)
2209 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2211 if (hdr->sh_size != 0)
2214 /* Some linkers erroneously set sh_info to one with a
2215 zero sh_size. ld sees this as a global symbol count
2216 of (unsigned) -1. Fix it here. */
2221 /* PR 18854: A binary might contain more than one dynamic symbol table.
2222 Unusual, but possible. Warn, but continue. */
2223 if (elf_dynsymtab (abfd) != 0)
2226 /* xgettext:c-format */
2227 (_("%pB: warning: multiple dynamic symbol tables detected"
2228 " - ignoring the table in section %u"),
2232 elf_dynsymtab (abfd) = shindex;
2233 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2234 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2235 abfd->flags |= HAS_SYMS;
2237 /* Besides being a symbol table, we also treat this as a regular
2238 section, so that objcopy can handle it. */
2239 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2242 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2244 elf_section_list * entry;
2246 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2247 if (entry->ndx == shindex)
2250 entry = bfd_alloc (abfd, sizeof * entry);
2253 entry->ndx = shindex;
2255 entry->next = elf_symtab_shndx_list (abfd);
2256 elf_symtab_shndx_list (abfd) = entry;
2257 elf_elfsections (abfd)[shindex] = & entry->hdr;
2261 case SHT_STRTAB: /* A string table. */
2262 if (hdr->bfd_section != NULL)
2265 if (ehdr->e_shstrndx == shindex)
2267 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2268 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2272 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2275 elf_tdata (abfd)->strtab_hdr = *hdr;
2276 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2280 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2283 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2284 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2285 elf_elfsections (abfd)[shindex] = hdr;
2286 /* We also treat this as a regular section, so that objcopy
2288 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2293 /* If the string table isn't one of the above, then treat it as a
2294 regular section. We need to scan all the headers to be sure,
2295 just in case this strtab section appeared before the above. */
2296 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2298 unsigned int i, num_sec;
2300 num_sec = elf_numsections (abfd);
2301 for (i = 1; i < num_sec; i++)
2303 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2304 if (hdr2->sh_link == shindex)
2306 /* Prevent endless recursion on broken objects. */
2309 if (! bfd_section_from_shdr (abfd, i))
2311 if (elf_onesymtab (abfd) == i)
2313 if (elf_dynsymtab (abfd) == i)
2314 goto dynsymtab_strtab;
2318 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2323 /* *These* do a lot of work -- but build no sections! */
2325 asection *target_sect;
2326 Elf_Internal_Shdr *hdr2, **p_hdr;
2327 unsigned int num_sec = elf_numsections (abfd);
2328 struct bfd_elf_section_data *esdt;
2331 != (bfd_size_type) (hdr->sh_type == SHT_REL
2332 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2335 /* Check for a bogus link to avoid crashing. */
2336 if (hdr->sh_link >= num_sec)
2339 /* xgettext:c-format */
2340 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2341 abfd, hdr->sh_link, name, shindex);
2342 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2347 /* For some incomprehensible reason Oracle distributes
2348 libraries for Solaris in which some of the objects have
2349 bogus sh_link fields. It would be nice if we could just
2350 reject them, but, unfortunately, some people need to use
2351 them. We scan through the section headers; if we find only
2352 one suitable symbol table, we clobber the sh_link to point
2353 to it. I hope this doesn't break anything.
2355 Don't do it on executable nor shared library. */
2356 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2357 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2358 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2364 for (scan = 1; scan < num_sec; scan++)
2366 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2367 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2378 hdr->sh_link = found;
2381 /* Get the symbol table. */
2382 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2383 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2384 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2387 /* If this reloc section does not use the main symbol table we
2388 don't treat it as a reloc section. BFD can't adequately
2389 represent such a section, so at least for now, we don't
2390 try. We just present it as a normal section. We also
2391 can't use it as a reloc section if it points to the null
2392 section, an invalid section, another reloc section, or its
2393 sh_link points to the null section. */
2394 if (hdr->sh_link != elf_onesymtab (abfd)
2395 || hdr->sh_link == SHN_UNDEF
2396 || hdr->sh_info == SHN_UNDEF
2397 || hdr->sh_info >= num_sec
2398 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2399 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2401 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2406 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2409 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2410 if (target_sect == NULL)
2413 esdt = elf_section_data (target_sect);
2414 if (hdr->sh_type == SHT_RELA)
2415 p_hdr = &esdt->rela.hdr;
2417 p_hdr = &esdt->rel.hdr;
2419 /* PR 17512: file: 0b4f81b7. */
2422 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2427 elf_elfsections (abfd)[shindex] = hdr2;
2428 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2429 * bed->s->int_rels_per_ext_rel);
2430 target_sect->flags |= SEC_RELOC;
2431 target_sect->relocation = NULL;
2432 target_sect->rel_filepos = hdr->sh_offset;
2433 /* In the section to which the relocations apply, mark whether
2434 its relocations are of the REL or RELA variety. */
2435 if (hdr->sh_size != 0)
2437 if (hdr->sh_type == SHT_RELA)
2438 target_sect->use_rela_p = 1;
2440 abfd->flags |= HAS_RELOC;
2444 case SHT_GNU_verdef:
2445 elf_dynverdef (abfd) = shindex;
2446 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2447 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2450 case SHT_GNU_versym:
2451 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2454 elf_dynversym (abfd) = shindex;
2455 elf_tdata (abfd)->dynversym_hdr = *hdr;
2456 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2459 case SHT_GNU_verneed:
2460 elf_dynverref (abfd) = shindex;
2461 elf_tdata (abfd)->dynverref_hdr = *hdr;
2462 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2469 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2472 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2478 /* Possibly an attributes section. */
2479 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2480 || hdr->sh_type == bed->obj_attrs_section_type)
2482 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2484 _bfd_elf_parse_attributes (abfd, hdr);
2488 /* Check for any processor-specific section types. */
2489 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2492 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2494 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2495 /* FIXME: How to properly handle allocated section reserved
2496 for applications? */
2498 /* xgettext:c-format */
2499 (_("%pB: unknown type [%#x] section `%s'"),
2500 abfd, hdr->sh_type, name);
2503 /* Allow sections reserved for applications. */
2504 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2509 else if (hdr->sh_type >= SHT_LOPROC
2510 && hdr->sh_type <= SHT_HIPROC)
2511 /* FIXME: We should handle this section. */
2513 /* xgettext:c-format */
2514 (_("%pB: unknown type [%#x] section `%s'"),
2515 abfd, hdr->sh_type, name);
2516 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2518 /* Unrecognised OS-specific sections. */
2519 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2520 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2521 required to correctly process the section and the file should
2522 be rejected with an error message. */
2524 /* xgettext:c-format */
2525 (_("%pB: unknown type [%#x] section `%s'"),
2526 abfd, hdr->sh_type, name);
2529 /* Otherwise it should be processed. */
2530 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2535 /* FIXME: We should handle this section. */
2537 /* xgettext:c-format */
2538 (_("%pB: unknown type [%#x] section `%s'"),
2539 abfd, hdr->sh_type, name);
2547 if (sections_being_created && sections_being_created_abfd == abfd)
2548 sections_being_created [shindex] = FALSE;
2549 if (-- nesting == 0)
2551 sections_being_created = NULL;
2552 sections_being_created_abfd = abfd;
2557 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2560 bfd_sym_from_r_symndx (struct sym_cache *cache,
2562 unsigned long r_symndx)
2564 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2566 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2568 Elf_Internal_Shdr *symtab_hdr;
2569 unsigned char esym[sizeof (Elf64_External_Sym)];
2570 Elf_External_Sym_Shndx eshndx;
2572 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2573 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2574 &cache->sym[ent], esym, &eshndx) == NULL)
2577 if (cache->abfd != abfd)
2579 memset (cache->indx, -1, sizeof (cache->indx));
2582 cache->indx[ent] = r_symndx;
2585 return &cache->sym[ent];
2588 /* Given an ELF section number, retrieve the corresponding BFD
2592 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2594 if (sec_index >= elf_numsections (abfd))
2596 return elf_elfsections (abfd)[sec_index]->bfd_section;
2599 static const struct bfd_elf_special_section special_sections_b[] =
2601 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2602 { NULL, 0, 0, 0, 0 }
2605 static const struct bfd_elf_special_section special_sections_c[] =
2607 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2608 { NULL, 0, 0, 0, 0 }
2611 static const struct bfd_elf_special_section special_sections_d[] =
2613 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2614 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2615 /* There are more DWARF sections than these, but they needn't be added here
2616 unless you have to cope with broken compilers that don't emit section
2617 attributes or you want to help the user writing assembler. */
2618 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2619 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2620 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2621 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2622 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2623 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2624 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2625 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2626 { NULL, 0, 0, 0, 0 }
2629 static const struct bfd_elf_special_section special_sections_f[] =
2631 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2632 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2633 { NULL, 0 , 0, 0, 0 }
2636 static const struct bfd_elf_special_section special_sections_g[] =
2638 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2639 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2640 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2641 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2642 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2643 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2644 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2645 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2646 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2647 { NULL, 0, 0, 0, 0 }
2650 static const struct bfd_elf_special_section special_sections_h[] =
2652 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2653 { NULL, 0, 0, 0, 0 }
2656 static const struct bfd_elf_special_section special_sections_i[] =
2658 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2659 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2660 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2661 { NULL, 0, 0, 0, 0 }
2664 static const struct bfd_elf_special_section special_sections_l[] =
2666 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2667 { NULL, 0, 0, 0, 0 }
2670 static const struct bfd_elf_special_section special_sections_n[] =
2672 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2673 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2674 { NULL, 0, 0, 0, 0 }
2677 static const struct bfd_elf_special_section special_sections_p[] =
2679 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2680 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2681 { NULL, 0, 0, 0, 0 }
2684 static const struct bfd_elf_special_section special_sections_r[] =
2686 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2687 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2688 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2689 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2690 { NULL, 0, 0, 0, 0 }
2693 static const struct bfd_elf_special_section special_sections_s[] =
2695 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2696 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2697 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2698 /* See struct bfd_elf_special_section declaration for the semantics of
2699 this special case where .prefix_length != strlen (.prefix). */
2700 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2701 { NULL, 0, 0, 0, 0 }
2704 static const struct bfd_elf_special_section special_sections_t[] =
2706 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2707 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2708 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2709 { NULL, 0, 0, 0, 0 }
2712 static const struct bfd_elf_special_section special_sections_z[] =
2714 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2715 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2716 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2717 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2718 { NULL, 0, 0, 0, 0 }
2721 static const struct bfd_elf_special_section * const special_sections[] =
2723 special_sections_b, /* 'b' */
2724 special_sections_c, /* 'c' */
2725 special_sections_d, /* 'd' */
2727 special_sections_f, /* 'f' */
2728 special_sections_g, /* 'g' */
2729 special_sections_h, /* 'h' */
2730 special_sections_i, /* 'i' */
2733 special_sections_l, /* 'l' */
2735 special_sections_n, /* 'n' */
2737 special_sections_p, /* 'p' */
2739 special_sections_r, /* 'r' */
2740 special_sections_s, /* 's' */
2741 special_sections_t, /* 't' */
2747 special_sections_z /* 'z' */
2750 const struct bfd_elf_special_section *
2751 _bfd_elf_get_special_section (const char *name,
2752 const struct bfd_elf_special_section *spec,
2758 len = strlen (name);
2760 for (i = 0; spec[i].prefix != NULL; i++)
2763 int prefix_len = spec[i].prefix_length;
2765 if (len < prefix_len)
2767 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2770 suffix_len = spec[i].suffix_length;
2771 if (suffix_len <= 0)
2773 if (name[prefix_len] != 0)
2775 if (suffix_len == 0)
2777 if (name[prefix_len] != '.'
2778 && (suffix_len == -2
2779 || (rela && spec[i].type == SHT_REL)))
2785 if (len < prefix_len + suffix_len)
2787 if (memcmp (name + len - suffix_len,
2788 spec[i].prefix + prefix_len,
2798 const struct bfd_elf_special_section *
2799 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2802 const struct bfd_elf_special_section *spec;
2803 const struct elf_backend_data *bed;
2805 /* See if this is one of the special sections. */
2806 if (sec->name == NULL)
2809 bed = get_elf_backend_data (abfd);
2810 spec = bed->special_sections;
2813 spec = _bfd_elf_get_special_section (sec->name,
2814 bed->special_sections,
2820 if (sec->name[0] != '.')
2823 i = sec->name[1] - 'b';
2824 if (i < 0 || i > 'z' - 'b')
2827 spec = special_sections[i];
2832 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2836 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2838 struct bfd_elf_section_data *sdata;
2839 const struct elf_backend_data *bed;
2840 const struct bfd_elf_special_section *ssect;
2842 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2845 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2849 sec->used_by_bfd = sdata;
2852 /* Indicate whether or not this section should use RELA relocations. */
2853 bed = get_elf_backend_data (abfd);
2854 sec->use_rela_p = bed->default_use_rela_p;
2856 /* When we read a file, we don't need to set ELF section type and
2857 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2858 anyway. We will set ELF section type and flags for all linker
2859 created sections. If user specifies BFD section flags, we will
2860 set ELF section type and flags based on BFD section flags in
2861 elf_fake_sections. Special handling for .init_array/.fini_array
2862 output sections since they may contain .ctors/.dtors input
2863 sections. We don't want _bfd_elf_init_private_section_data to
2864 copy ELF section type from .ctors/.dtors input sections. */
2865 if (abfd->direction != read_direction
2866 || (sec->flags & SEC_LINKER_CREATED) != 0)
2868 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2871 || (sec->flags & SEC_LINKER_CREATED) != 0
2872 || ssect->type == SHT_INIT_ARRAY
2873 || ssect->type == SHT_FINI_ARRAY))
2875 elf_section_type (sec) = ssect->type;
2876 elf_section_flags (sec) = ssect->attr;
2880 return _bfd_generic_new_section_hook (abfd, sec);
2883 /* Create a new bfd section from an ELF program header.
2885 Since program segments have no names, we generate a synthetic name
2886 of the form segment<NUM>, where NUM is generally the index in the
2887 program header table. For segments that are split (see below) we
2888 generate the names segment<NUM>a and segment<NUM>b.
2890 Note that some program segments may have a file size that is different than
2891 (less than) the memory size. All this means is that at execution the
2892 system must allocate the amount of memory specified by the memory size,
2893 but only initialize it with the first "file size" bytes read from the
2894 file. This would occur for example, with program segments consisting
2895 of combined data+bss.
2897 To handle the above situation, this routine generates TWO bfd sections
2898 for the single program segment. The first has the length specified by
2899 the file size of the segment, and the second has the length specified
2900 by the difference between the two sizes. In effect, the segment is split
2901 into its initialized and uninitialized parts.
2906 _bfd_elf_make_section_from_phdr (bfd *abfd,
2907 Elf_Internal_Phdr *hdr,
2909 const char *type_name)
2917 split = ((hdr->p_memsz > 0)
2918 && (hdr->p_filesz > 0)
2919 && (hdr->p_memsz > hdr->p_filesz));
2921 if (hdr->p_filesz > 0)
2923 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2924 len = strlen (namebuf) + 1;
2925 name = (char *) bfd_alloc (abfd, len);
2928 memcpy (name, namebuf, len);
2929 newsect = bfd_make_section (abfd, name);
2930 if (newsect == NULL)
2932 newsect->vma = hdr->p_vaddr;
2933 newsect->lma = hdr->p_paddr;
2934 newsect->size = hdr->p_filesz;
2935 newsect->filepos = hdr->p_offset;
2936 newsect->flags |= SEC_HAS_CONTENTS;
2937 newsect->alignment_power = bfd_log2 (hdr->p_align);
2938 if (hdr->p_type == PT_LOAD)
2940 newsect->flags |= SEC_ALLOC;
2941 newsect->flags |= SEC_LOAD;
2942 if (hdr->p_flags & PF_X)
2944 /* FIXME: all we known is that it has execute PERMISSION,
2946 newsect->flags |= SEC_CODE;
2949 if (!(hdr->p_flags & PF_W))
2951 newsect->flags |= SEC_READONLY;
2955 if (hdr->p_memsz > hdr->p_filesz)
2959 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2960 len = strlen (namebuf) + 1;
2961 name = (char *) bfd_alloc (abfd, len);
2964 memcpy (name, namebuf, len);
2965 newsect = bfd_make_section (abfd, name);
2966 if (newsect == NULL)
2968 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2969 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2970 newsect->size = hdr->p_memsz - hdr->p_filesz;
2971 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2972 align = newsect->vma & -newsect->vma;
2973 if (align == 0 || align > hdr->p_align)
2974 align = hdr->p_align;
2975 newsect->alignment_power = bfd_log2 (align);
2976 if (hdr->p_type == PT_LOAD)
2978 /* Hack for gdb. Segments that have not been modified do
2979 not have their contents written to a core file, on the
2980 assumption that a debugger can find the contents in the
2981 executable. We flag this case by setting the fake
2982 section size to zero. Note that "real" bss sections will
2983 always have their contents dumped to the core file. */
2984 if (bfd_get_format (abfd) == bfd_core)
2986 newsect->flags |= SEC_ALLOC;
2987 if (hdr->p_flags & PF_X)
2988 newsect->flags |= SEC_CODE;
2990 if (!(hdr->p_flags & PF_W))
2991 newsect->flags |= SEC_READONLY;
2998 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3000 const struct elf_backend_data *bed;
3002 switch (hdr->p_type)
3005 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3008 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
3011 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3014 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3017 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3019 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3025 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3028 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3030 case PT_GNU_EH_FRAME:
3031 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3035 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3038 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3041 /* Check for any processor-specific program segment types. */
3042 bed = get_elf_backend_data (abfd);
3043 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3047 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3051 _bfd_elf_single_rel_hdr (asection *sec)
3053 if (elf_section_data (sec)->rel.hdr)
3055 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3056 return elf_section_data (sec)->rel.hdr;
3059 return elf_section_data (sec)->rela.hdr;
3063 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3064 Elf_Internal_Shdr *rel_hdr,
3065 const char *sec_name,
3066 bfd_boolean use_rela_p)
3068 char *name = (char *) bfd_alloc (abfd,
3069 sizeof ".rela" + strlen (sec_name));
3073 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3075 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3077 if (rel_hdr->sh_name == (unsigned int) -1)
3083 /* Allocate and initialize a section-header for a new reloc section,
3084 containing relocations against ASECT. It is stored in RELDATA. If
3085 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3089 _bfd_elf_init_reloc_shdr (bfd *abfd,
3090 struct bfd_elf_section_reloc_data *reldata,
3091 const char *sec_name,
3092 bfd_boolean use_rela_p,
3093 bfd_boolean delay_st_name_p)
3095 Elf_Internal_Shdr *rel_hdr;
3096 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3098 BFD_ASSERT (reldata->hdr == NULL);
3099 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3100 reldata->hdr = rel_hdr;
3102 if (delay_st_name_p)
3103 rel_hdr->sh_name = (unsigned int) -1;
3104 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3107 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3108 rel_hdr->sh_entsize = (use_rela_p
3109 ? bed->s->sizeof_rela
3110 : bed->s->sizeof_rel);
3111 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3112 rel_hdr->sh_flags = 0;
3113 rel_hdr->sh_addr = 0;
3114 rel_hdr->sh_size = 0;
3115 rel_hdr->sh_offset = 0;
3120 /* Return the default section type based on the passed in section flags. */
3123 bfd_elf_get_default_section_type (flagword flags)
3125 if ((flags & SEC_ALLOC) != 0
3126 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3128 return SHT_PROGBITS;
3131 struct fake_section_arg
3133 struct bfd_link_info *link_info;
3137 /* Set up an ELF internal section header for a section. */
3140 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3142 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3143 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3144 struct bfd_elf_section_data *esd = elf_section_data (asect);
3145 Elf_Internal_Shdr *this_hdr;
3146 unsigned int sh_type;
3147 const char *name = asect->name;
3148 bfd_boolean delay_st_name_p = FALSE;
3152 /* We already failed; just get out of the bfd_map_over_sections
3157 this_hdr = &esd->this_hdr;
3161 /* ld: compress DWARF debug sections with names: .debug_*. */
3162 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3163 && (asect->flags & SEC_DEBUGGING)
3167 /* Set SEC_ELF_COMPRESS to indicate this section should be
3169 asect->flags |= SEC_ELF_COMPRESS;
3171 /* If this section will be compressed, delay adding section
3172 name to section name section after it is compressed in
3173 _bfd_elf_assign_file_positions_for_non_load. */
3174 delay_st_name_p = TRUE;
3177 else if ((asect->flags & SEC_ELF_RENAME))
3179 /* objcopy: rename output DWARF debug section. */
3180 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3182 /* When we decompress or compress with SHF_COMPRESSED,
3183 convert section name from .zdebug_* to .debug_* if
3187 char *new_name = convert_zdebug_to_debug (abfd, name);
3188 if (new_name == NULL)
3196 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3198 /* PR binutils/18087: Compression does not always make a
3199 section smaller. So only rename the section when
3200 compression has actually taken place. If input section
3201 name is .zdebug_*, we should never compress it again. */
3202 char *new_name = convert_debug_to_zdebug (abfd, name);
3203 if (new_name == NULL)
3208 BFD_ASSERT (name[1] != 'z');
3213 if (delay_st_name_p)
3214 this_hdr->sh_name = (unsigned int) -1;
3218 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3220 if (this_hdr->sh_name == (unsigned int) -1)
3227 /* Don't clear sh_flags. Assembler may set additional bits. */
3229 if ((asect->flags & SEC_ALLOC) != 0
3230 || asect->user_set_vma)
3231 this_hdr->sh_addr = asect->vma;
3233 this_hdr->sh_addr = 0;
3235 this_hdr->sh_offset = 0;
3236 this_hdr->sh_size = asect->size;
3237 this_hdr->sh_link = 0;
3238 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3239 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3242 /* xgettext:c-format */
3243 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3244 abfd, asect->alignment_power, asect);
3248 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
3249 /* The sh_entsize and sh_info fields may have been set already by
3250 copy_private_section_data. */
3252 this_hdr->bfd_section = asect;
3253 this_hdr->contents = NULL;
3255 /* If the section type is unspecified, we set it based on
3257 if ((asect->flags & SEC_GROUP) != 0)
3258 sh_type = SHT_GROUP;
3260 sh_type = bfd_elf_get_default_section_type (asect->flags);
3262 if (this_hdr->sh_type == SHT_NULL)
3263 this_hdr->sh_type = sh_type;
3264 else if (this_hdr->sh_type == SHT_NOBITS
3265 && sh_type == SHT_PROGBITS
3266 && (asect->flags & SEC_ALLOC) != 0)
3268 /* Warn if we are changing a NOBITS section to PROGBITS, but
3269 allow the link to proceed. This can happen when users link
3270 non-bss input sections to bss output sections, or emit data
3271 to a bss output section via a linker script. */
3273 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3274 this_hdr->sh_type = sh_type;
3277 switch (this_hdr->sh_type)
3288 case SHT_INIT_ARRAY:
3289 case SHT_FINI_ARRAY:
3290 case SHT_PREINIT_ARRAY:
3291 this_hdr->sh_entsize = bed->s->arch_size / 8;
3295 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3299 this_hdr->sh_entsize = bed->s->sizeof_sym;
3303 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3307 if (get_elf_backend_data (abfd)->may_use_rela_p)
3308 this_hdr->sh_entsize = bed->s->sizeof_rela;
3312 if (get_elf_backend_data (abfd)->may_use_rel_p)
3313 this_hdr->sh_entsize = bed->s->sizeof_rel;
3316 case SHT_GNU_versym:
3317 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3320 case SHT_GNU_verdef:
3321 this_hdr->sh_entsize = 0;
3322 /* objcopy or strip will copy over sh_info, but may not set
3323 cverdefs. The linker will set cverdefs, but sh_info will be
3325 if (this_hdr->sh_info == 0)
3326 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3328 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3329 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3332 case SHT_GNU_verneed:
3333 this_hdr->sh_entsize = 0;
3334 /* objcopy or strip will copy over sh_info, but may not set
3335 cverrefs. The linker will set cverrefs, but sh_info will be
3337 if (this_hdr->sh_info == 0)
3338 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3340 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3341 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3345 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3349 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3353 if ((asect->flags & SEC_ALLOC) != 0)
3354 this_hdr->sh_flags |= SHF_ALLOC;
3355 if ((asect->flags & SEC_READONLY) == 0)
3356 this_hdr->sh_flags |= SHF_WRITE;
3357 if ((asect->flags & SEC_CODE) != 0)
3358 this_hdr->sh_flags |= SHF_EXECINSTR;
3359 if ((asect->flags & SEC_MERGE) != 0)
3361 this_hdr->sh_flags |= SHF_MERGE;
3362 this_hdr->sh_entsize = asect->entsize;
3364 if ((asect->flags & SEC_STRINGS) != 0)
3365 this_hdr->sh_flags |= SHF_STRINGS;
3366 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3367 this_hdr->sh_flags |= SHF_GROUP;
3368 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3370 this_hdr->sh_flags |= SHF_TLS;
3371 if (asect->size == 0
3372 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3374 struct bfd_link_order *o = asect->map_tail.link_order;
3376 this_hdr->sh_size = 0;
3379 this_hdr->sh_size = o->offset + o->size;
3380 if (this_hdr->sh_size != 0)
3381 this_hdr->sh_type = SHT_NOBITS;
3385 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3386 this_hdr->sh_flags |= SHF_EXCLUDE;
3388 /* If the section has relocs, set up a section header for the
3389 SHT_REL[A] section. If two relocation sections are required for
3390 this section, it is up to the processor-specific back-end to
3391 create the other. */
3392 if ((asect->flags & SEC_RELOC) != 0)
3394 /* When doing a relocatable link, create both REL and RELA sections if
3397 /* Do the normal setup if we wouldn't create any sections here. */
3398 && esd->rel.count + esd->rela.count > 0
3399 && (bfd_link_relocatable (arg->link_info)
3400 || arg->link_info->emitrelocations))
3402 if (esd->rel.count && esd->rel.hdr == NULL
3403 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3404 FALSE, delay_st_name_p))
3409 if (esd->rela.count && esd->rela.hdr == NULL
3410 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3411 TRUE, delay_st_name_p))
3417 else if (!_bfd_elf_init_reloc_shdr (abfd,
3419 ? &esd->rela : &esd->rel),
3429 /* Check for processor-specific section types. */
3430 sh_type = this_hdr->sh_type;
3431 if (bed->elf_backend_fake_sections
3432 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3438 if (sh_type == SHT_NOBITS && asect->size != 0)
3440 /* Don't change the header type from NOBITS if we are being
3441 called for objcopy --only-keep-debug. */
3442 this_hdr->sh_type = sh_type;
3446 /* Fill in the contents of a SHT_GROUP section. Called from
3447 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3448 when ELF targets use the generic linker, ld. Called for ld -r
3449 from bfd_elf_final_link. */
3452 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3454 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3455 asection *elt, *first;
3459 /* Ignore linker created group section. See elfNN_ia64_object_p in
3461 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
3465 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3467 unsigned long symindx = 0;
3469 /* elf_group_id will have been set up by objcopy and the
3471 if (elf_group_id (sec) != NULL)
3472 symindx = elf_group_id (sec)->udata.i;
3476 /* If called from the assembler, swap_out_syms will have set up
3477 elf_section_syms. */
3478 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3479 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3481 elf_section_data (sec)->this_hdr.sh_info = symindx;
3483 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3485 /* The ELF backend linker sets sh_info to -2 when the group
3486 signature symbol is global, and thus the index can't be
3487 set until all local symbols are output. */
3489 struct bfd_elf_section_data *sec_data;
3490 unsigned long symndx;
3491 unsigned long extsymoff;
3492 struct elf_link_hash_entry *h;
3494 /* The point of this little dance to the first SHF_GROUP section
3495 then back to the SHT_GROUP section is that this gets us to
3496 the SHT_GROUP in the input object. */
3497 igroup = elf_sec_group (elf_next_in_group (sec));
3498 sec_data = elf_section_data (igroup);
3499 symndx = sec_data->this_hdr.sh_info;
3501 if (!elf_bad_symtab (igroup->owner))
3503 Elf_Internal_Shdr *symtab_hdr;
3505 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3506 extsymoff = symtab_hdr->sh_info;
3508 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3509 while (h->root.type == bfd_link_hash_indirect
3510 || h->root.type == bfd_link_hash_warning)
3511 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3513 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3516 /* The contents won't be allocated for "ld -r" or objcopy. */
3518 if (sec->contents == NULL)
3521 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3523 /* Arrange for the section to be written out. */
3524 elf_section_data (sec)->this_hdr.contents = sec->contents;
3525 if (sec->contents == NULL)
3532 loc = sec->contents + sec->size;
3534 /* Get the pointer to the first section in the group that gas
3535 squirreled away here. objcopy arranges for this to be set to the
3536 start of the input section group. */
3537 first = elt = elf_next_in_group (sec);
3539 /* First element is a flag word. Rest of section is elf section
3540 indices for all the sections of the group. Write them backwards
3541 just to keep the group in the same order as given in .section
3542 directives, not that it matters. */
3549 s = s->output_section;
3551 && !bfd_is_abs_section (s))
3553 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3554 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3556 if (elf_sec->rel.hdr != NULL
3558 || (input_elf_sec->rel.hdr != NULL
3559 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3561 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3563 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3565 if (elf_sec->rela.hdr != NULL
3567 || (input_elf_sec->rela.hdr != NULL
3568 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3570 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3572 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3575 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3577 elt = elf_next_in_group (elt);
3583 BFD_ASSERT (loc == sec->contents);
3585 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3588 /* Given NAME, the name of a relocation section stripped of its
3589 .rel/.rela prefix, return the section in ABFD to which the
3590 relocations apply. */
3593 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3595 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3596 section likely apply to .got.plt or .got section. */
3597 if (get_elf_backend_data (abfd)->want_got_plt
3598 && strcmp (name, ".plt") == 0)
3603 sec = bfd_get_section_by_name (abfd, name);
3609 return bfd_get_section_by_name (abfd, name);
3612 /* Return the section to which RELOC_SEC applies. */
3615 elf_get_reloc_section (asection *reloc_sec)
3620 const struct elf_backend_data *bed;
3622 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3623 if (type != SHT_REL && type != SHT_RELA)
3626 /* We look up the section the relocs apply to by name. */
3627 name = reloc_sec->name;
3628 if (strncmp (name, ".rel", 4) != 0)
3631 if (type == SHT_RELA && *name++ != 'a')
3634 abfd = reloc_sec->owner;
3635 bed = get_elf_backend_data (abfd);
3636 return bed->get_reloc_section (abfd, name);
3639 /* Assign all ELF section numbers. The dummy first section is handled here
3640 too. The link/info pointers for the standard section types are filled
3641 in here too, while we're at it. */
3644 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3646 struct elf_obj_tdata *t = elf_tdata (abfd);
3648 unsigned int section_number;
3649 Elf_Internal_Shdr **i_shdrp;
3650 struct bfd_elf_section_data *d;
3651 bfd_boolean need_symtab;
3655 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3657 /* SHT_GROUP sections are in relocatable files only. */
3658 if (link_info == NULL || !link_info->resolve_section_groups)
3660 size_t reloc_count = 0;
3662 /* Put SHT_GROUP sections first. */
3663 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3665 d = elf_section_data (sec);
3667 if (d->this_hdr.sh_type == SHT_GROUP)
3669 if (sec->flags & SEC_LINKER_CREATED)
3671 /* Remove the linker created SHT_GROUP sections. */
3672 bfd_section_list_remove (abfd, sec);
3673 abfd->section_count--;
3676 d->this_idx = section_number++;
3679 /* Count relocations. */
3680 reloc_count += sec->reloc_count;
3683 /* Clear HAS_RELOC if there are no relocations. */
3684 if (reloc_count == 0)
3685 abfd->flags &= ~HAS_RELOC;
3688 for (sec = abfd->sections; sec; sec = sec->next)
3690 d = elf_section_data (sec);
3692 if (d->this_hdr.sh_type != SHT_GROUP)
3693 d->this_idx = section_number++;
3694 if (d->this_hdr.sh_name != (unsigned int) -1)
3695 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3698 d->rel.idx = section_number++;
3699 if (d->rel.hdr->sh_name != (unsigned int) -1)
3700 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3707 d->rela.idx = section_number++;
3708 if (d->rela.hdr->sh_name != (unsigned int) -1)
3709 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3715 need_symtab = (bfd_get_symcount (abfd) > 0
3716 || (link_info == NULL
3717 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3721 elf_onesymtab (abfd) = section_number++;
3722 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3723 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3725 elf_section_list * entry;
3727 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3729 entry = bfd_zalloc (abfd, sizeof * entry);
3730 entry->ndx = section_number++;
3731 elf_symtab_shndx_list (abfd) = entry;
3733 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3734 ".symtab_shndx", FALSE);
3735 if (entry->hdr.sh_name == (unsigned int) -1)
3738 elf_strtab_sec (abfd) = section_number++;
3739 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3742 elf_shstrtab_sec (abfd) = section_number++;
3743 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3744 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3746 if (section_number >= SHN_LORESERVE)
3748 /* xgettext:c-format */
3749 _bfd_error_handler (_("%pB: too many sections: %u"),
3750 abfd, section_number);
3754 elf_numsections (abfd) = section_number;
3755 elf_elfheader (abfd)->e_shnum = section_number;
3757 /* Set up the list of section header pointers, in agreement with the
3759 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3760 sizeof (Elf_Internal_Shdr *));
3761 if (i_shdrp == NULL)
3764 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3765 sizeof (Elf_Internal_Shdr));
3766 if (i_shdrp[0] == NULL)
3768 bfd_release (abfd, i_shdrp);
3772 elf_elfsections (abfd) = i_shdrp;
3774 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3777 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3778 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3780 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3781 BFD_ASSERT (entry != NULL);
3782 i_shdrp[entry->ndx] = & entry->hdr;
3783 entry->hdr.sh_link = elf_onesymtab (abfd);
3785 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3786 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3789 for (sec = abfd->sections; sec; sec = sec->next)
3793 d = elf_section_data (sec);
3795 i_shdrp[d->this_idx] = &d->this_hdr;
3796 if (d->rel.idx != 0)
3797 i_shdrp[d->rel.idx] = d->rel.hdr;
3798 if (d->rela.idx != 0)
3799 i_shdrp[d->rela.idx] = d->rela.hdr;
3801 /* Fill in the sh_link and sh_info fields while we're at it. */
3803 /* sh_link of a reloc section is the section index of the symbol
3804 table. sh_info is the section index of the section to which
3805 the relocation entries apply. */
3806 if (d->rel.idx != 0)
3808 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3809 d->rel.hdr->sh_info = d->this_idx;
3810 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3812 if (d->rela.idx != 0)
3814 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3815 d->rela.hdr->sh_info = d->this_idx;
3816 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3819 /* We need to set up sh_link for SHF_LINK_ORDER. */
3820 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3822 s = elf_linked_to_section (sec);
3825 /* elf_linked_to_section points to the input section. */
3826 if (link_info != NULL)
3828 /* Check discarded linkonce section. */
3829 if (discarded_section (s))
3833 /* xgettext:c-format */
3834 (_("%pB: sh_link of section `%pA' points to"
3835 " discarded section `%pA' of `%pB'"),
3836 abfd, d->this_hdr.bfd_section,
3838 /* Point to the kept section if it has the same
3839 size as the discarded one. */
3840 kept = _bfd_elf_check_kept_section (s, link_info);
3843 bfd_set_error (bfd_error_bad_value);
3849 s = s->output_section;
3850 BFD_ASSERT (s != NULL);
3854 /* Handle objcopy. */
3855 if (s->output_section == NULL)
3858 /* xgettext:c-format */
3859 (_("%pB: sh_link of section `%pA' points to"
3860 " removed section `%pA' of `%pB'"),
3861 abfd, d->this_hdr.bfd_section, s, s->owner);
3862 bfd_set_error (bfd_error_bad_value);
3865 s = s->output_section;
3867 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3872 The Intel C compiler generates SHT_IA_64_UNWIND with
3873 SHF_LINK_ORDER. But it doesn't set the sh_link or
3874 sh_info fields. Hence we could get the situation
3876 const struct elf_backend_data *bed
3877 = get_elf_backend_data (abfd);
3878 if (bed->link_order_error_handler)
3879 bed->link_order_error_handler
3880 /* xgettext:c-format */
3881 (_("%pB: warning: sh_link not set for section `%pA'"),
3886 switch (d->this_hdr.sh_type)
3890 /* A reloc section which we are treating as a normal BFD
3891 section. sh_link is the section index of the symbol
3892 table. sh_info is the section index of the section to
3893 which the relocation entries apply. We assume that an
3894 allocated reloc section uses the dynamic symbol table.
3895 FIXME: How can we be sure? */
3896 s = bfd_get_section_by_name (abfd, ".dynsym");
3898 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3900 s = elf_get_reloc_section (sec);
3903 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3904 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3909 /* We assume that a section named .stab*str is a stabs
3910 string section. We look for a section with the same name
3911 but without the trailing ``str'', and set its sh_link
3912 field to point to this section. */
3913 if (CONST_STRNEQ (sec->name, ".stab")
3914 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3919 len = strlen (sec->name);
3920 alc = (char *) bfd_malloc (len - 2);
3923 memcpy (alc, sec->name, len - 3);
3924 alc[len - 3] = '\0';
3925 s = bfd_get_section_by_name (abfd, alc);
3929 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3931 /* This is a .stab section. */
3932 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3933 elf_section_data (s)->this_hdr.sh_entsize
3934 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3941 case SHT_GNU_verneed:
3942 case SHT_GNU_verdef:
3943 /* sh_link is the section header index of the string table
3944 used for the dynamic entries, or the symbol table, or the
3946 s = bfd_get_section_by_name (abfd, ".dynstr");
3948 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3951 case SHT_GNU_LIBLIST:
3952 /* sh_link is the section header index of the prelink library
3953 list used for the dynamic entries, or the symbol table, or
3954 the version strings. */
3955 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3956 ? ".dynstr" : ".gnu.libstr");
3958 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3963 case SHT_GNU_versym:
3964 /* sh_link is the section header index of the symbol table
3965 this hash table or version table is for. */
3966 s = bfd_get_section_by_name (abfd, ".dynsym");
3968 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3972 d->this_hdr.sh_link = elf_onesymtab (abfd);
3976 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3977 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3978 debug section name from .debug_* to .zdebug_* if needed. */
3984 sym_is_global (bfd *abfd, asymbol *sym)
3986 /* If the backend has a special mapping, use it. */
3987 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3988 if (bed->elf_backend_sym_is_global)
3989 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3991 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3992 || bfd_is_und_section (bfd_get_section (sym))
3993 || bfd_is_com_section (bfd_get_section (sym)));
3996 /* Filter global symbols of ABFD to include in the import library. All
3997 SYMCOUNT symbols of ABFD can be examined from their pointers in
3998 SYMS. Pointers of symbols to keep should be stored contiguously at
3999 the beginning of that array.
4001 Returns the number of symbols to keep. */
4004 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4005 asymbol **syms, long symcount)
4007 long src_count, dst_count = 0;
4009 for (src_count = 0; src_count < symcount; src_count++)
4011 asymbol *sym = syms[src_count];
4012 char *name = (char *) bfd_asymbol_name (sym);
4013 struct bfd_link_hash_entry *h;
4015 if (!sym_is_global (abfd, sym))
4018 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
4021 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4023 if (h->linker_def || h->ldscript_def)
4026 syms[dst_count++] = sym;
4029 syms[dst_count] = NULL;
4034 /* Don't output section symbols for sections that are not going to be
4035 output, that are duplicates or there is no BFD section. */
4038 ignore_section_sym (bfd *abfd, asymbol *sym)
4040 elf_symbol_type *type_ptr;
4045 if ((sym->flags & BSF_SECTION_SYM) == 0)
4048 if (sym->section == NULL)
4051 type_ptr = elf_symbol_from (abfd, sym);
4052 return ((type_ptr != NULL
4053 && type_ptr->internal_elf_sym.st_shndx != 0
4054 && bfd_is_abs_section (sym->section))
4055 || !(sym->section->owner == abfd
4056 || (sym->section->output_section != NULL
4057 && sym->section->output_section->owner == abfd
4058 && sym->section->output_offset == 0)
4059 || bfd_is_abs_section (sym->section)));
4062 /* Map symbol from it's internal number to the external number, moving
4063 all local symbols to be at the head of the list. */
4066 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4068 unsigned int symcount = bfd_get_symcount (abfd);
4069 asymbol **syms = bfd_get_outsymbols (abfd);
4070 asymbol **sect_syms;
4071 unsigned int num_locals = 0;
4072 unsigned int num_globals = 0;
4073 unsigned int num_locals2 = 0;
4074 unsigned int num_globals2 = 0;
4075 unsigned int max_index = 0;
4081 fprintf (stderr, "elf_map_symbols\n");
4085 for (asect = abfd->sections; asect; asect = asect->next)
4087 if (max_index < asect->index)
4088 max_index = asect->index;
4092 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
4093 if (sect_syms == NULL)
4095 elf_section_syms (abfd) = sect_syms;
4096 elf_num_section_syms (abfd) = max_index;
4098 /* Init sect_syms entries for any section symbols we have already
4099 decided to output. */
4100 for (idx = 0; idx < symcount; idx++)
4102 asymbol *sym = syms[idx];
4104 if ((sym->flags & BSF_SECTION_SYM) != 0
4106 && !ignore_section_sym (abfd, sym)
4107 && !bfd_is_abs_section (sym->section))
4109 asection *sec = sym->section;
4111 if (sec->owner != abfd)
4112 sec = sec->output_section;
4114 sect_syms[sec->index] = syms[idx];
4118 /* Classify all of the symbols. */
4119 for (idx = 0; idx < symcount; idx++)
4121 if (sym_is_global (abfd, syms[idx]))
4123 else if (!ignore_section_sym (abfd, syms[idx]))
4127 /* We will be adding a section symbol for each normal BFD section. Most
4128 sections will already have a section symbol in outsymbols, but
4129 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4130 at least in that case. */
4131 for (asect = abfd->sections; asect; asect = asect->next)
4133 if (sect_syms[asect->index] == NULL)
4135 if (!sym_is_global (abfd, asect->symbol))
4142 /* Now sort the symbols so the local symbols are first. */
4143 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
4144 sizeof (asymbol *));
4146 if (new_syms == NULL)
4149 for (idx = 0; idx < symcount; idx++)
4151 asymbol *sym = syms[idx];
4154 if (sym_is_global (abfd, sym))
4155 i = num_locals + num_globals2++;
4156 else if (!ignore_section_sym (abfd, sym))
4161 sym->udata.i = i + 1;
4163 for (asect = abfd->sections; asect; asect = asect->next)
4165 if (sect_syms[asect->index] == NULL)
4167 asymbol *sym = asect->symbol;
4170 sect_syms[asect->index] = sym;
4171 if (!sym_is_global (abfd, sym))
4174 i = num_locals + num_globals2++;
4176 sym->udata.i = i + 1;
4180 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4182 *pnum_locals = num_locals;
4186 /* Align to the maximum file alignment that could be required for any
4187 ELF data structure. */
4189 static inline file_ptr
4190 align_file_position (file_ptr off, int align)
4192 return (off + align - 1) & ~(align - 1);
4195 /* Assign a file position to a section, optionally aligning to the
4196 required section alignment. */
4199 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4203 if (align && i_shdrp->sh_addralign > 1)
4204 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4205 i_shdrp->sh_offset = offset;
4206 if (i_shdrp->bfd_section != NULL)
4207 i_shdrp->bfd_section->filepos = offset;
4208 if (i_shdrp->sh_type != SHT_NOBITS)
4209 offset += i_shdrp->sh_size;
4213 /* Compute the file positions we are going to put the sections at, and
4214 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4215 is not NULL, this is being called by the ELF backend linker. */
4218 _bfd_elf_compute_section_file_positions (bfd *abfd,
4219 struct bfd_link_info *link_info)
4221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4222 struct fake_section_arg fsargs;
4224 struct elf_strtab_hash *strtab = NULL;
4225 Elf_Internal_Shdr *shstrtab_hdr;
4226 bfd_boolean need_symtab;
4228 if (abfd->output_has_begun)
4231 /* Do any elf backend specific processing first. */
4232 if (bed->elf_backend_begin_write_processing)
4233 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4235 if (! prep_headers (abfd))
4238 /* Post process the headers if necessary. */
4239 (*bed->elf_backend_post_process_headers) (abfd, link_info);
4241 fsargs.failed = FALSE;
4242 fsargs.link_info = link_info;
4243 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4247 if (!assign_section_numbers (abfd, link_info))
4250 /* The backend linker builds symbol table information itself. */
4251 need_symtab = (link_info == NULL
4252 && (bfd_get_symcount (abfd) > 0
4253 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4257 /* Non-zero if doing a relocatable link. */
4258 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4260 if (! swap_out_syms (abfd, &strtab, relocatable_p))
4265 if (link_info == NULL)
4267 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4272 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4273 /* sh_name was set in prep_headers. */
4274 shstrtab_hdr->sh_type = SHT_STRTAB;
4275 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4276 shstrtab_hdr->sh_addr = 0;
4277 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4278 shstrtab_hdr->sh_entsize = 0;
4279 shstrtab_hdr->sh_link = 0;
4280 shstrtab_hdr->sh_info = 0;
4281 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4282 shstrtab_hdr->sh_addralign = 1;
4284 if (!assign_file_positions_except_relocs (abfd, link_info))
4290 Elf_Internal_Shdr *hdr;
4292 off = elf_next_file_pos (abfd);
4294 hdr = & elf_symtab_hdr (abfd);
4295 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4297 if (elf_symtab_shndx_list (abfd) != NULL)
4299 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4300 if (hdr->sh_size != 0)
4301 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4302 /* FIXME: What about other symtab_shndx sections in the list ? */
4305 hdr = &elf_tdata (abfd)->strtab_hdr;
4306 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4308 elf_next_file_pos (abfd) = off;
4310 /* Now that we know where the .strtab section goes, write it
4312 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4313 || ! _bfd_elf_strtab_emit (abfd, strtab))
4315 _bfd_elf_strtab_free (strtab);
4318 abfd->output_has_begun = TRUE;
4323 /* Make an initial estimate of the size of the program header. If we
4324 get the number wrong here, we'll redo section placement. */
4326 static bfd_size_type
4327 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4331 const struct elf_backend_data *bed;
4333 /* Assume we will need exactly two PT_LOAD segments: one for text
4334 and one for data. */
4337 s = bfd_get_section_by_name (abfd, ".interp");
4338 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4340 /* If we have a loadable interpreter section, we need a
4341 PT_INTERP segment. In this case, assume we also need a
4342 PT_PHDR segment, although that may not be true for all
4347 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4349 /* We need a PT_DYNAMIC segment. */
4353 if (info != NULL && info->relro)
4355 /* We need a PT_GNU_RELRO segment. */
4359 if (elf_eh_frame_hdr (abfd))
4361 /* We need a PT_GNU_EH_FRAME segment. */
4365 if (elf_stack_flags (abfd))
4367 /* We need a PT_GNU_STACK segment. */
4371 for (s = abfd->sections; s != NULL; s = s->next)
4373 if ((s->flags & SEC_LOAD) != 0
4374 && elf_section_type (s) == SHT_NOTE)
4376 unsigned int alignment_power;
4377 /* We need a PT_NOTE segment. */
4379 /* Try to create just one PT_NOTE segment for all adjacent
4380 loadable SHT_NOTE sections. gABI requires that within a
4381 PT_NOTE segment (and also inside of each SHT_NOTE section)
4382 each note should have the same alignment. So we check
4383 whether the sections are correctly aligned. */
4384 alignment_power = s->alignment_power;
4385 while (s->next != NULL
4386 && s->next->alignment_power == alignment_power
4387 && (s->next->flags & SEC_LOAD) != 0
4388 && elf_section_type (s->next) == SHT_NOTE)
4393 for (s = abfd->sections; s != NULL; s = s->next)
4395 if (s->flags & SEC_THREAD_LOCAL)
4397 /* We need a PT_TLS segment. */
4403 bed = get_elf_backend_data (abfd);
4405 if ((abfd->flags & D_PAGED) != 0)
4407 /* Add a PT_GNU_MBIND segment for each mbind section. */
4408 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4409 for (s = abfd->sections; s != NULL; s = s->next)
4410 if (elf_section_flags (s) & SHF_GNU_MBIND)
4412 if (elf_section_data (s)->this_hdr.sh_info
4416 /* xgettext:c-format */
4417 (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"),
4418 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4421 /* Align mbind section to page size. */
4422 if (s->alignment_power < page_align_power)
4423 s->alignment_power = page_align_power;
4428 /* Let the backend count up any program headers it might need. */
4429 if (bed->elf_backend_additional_program_headers)
4433 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4439 return segs * bed->s->sizeof_phdr;
4442 /* Find the segment that contains the output_section of section. */
4445 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4447 struct elf_segment_map *m;
4448 Elf_Internal_Phdr *p;
4450 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4456 for (i = m->count - 1; i >= 0; i--)
4457 if (m->sections[i] == section)
4464 /* Create a mapping from a set of sections to a program segment. */
4466 static struct elf_segment_map *
4467 make_mapping (bfd *abfd,
4468 asection **sections,
4473 struct elf_segment_map *m;
4478 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4479 amt += (to - from) * sizeof (asection *);
4480 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4484 m->p_type = PT_LOAD;
4485 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4486 m->sections[i - from] = *hdrpp;
4487 m->count = to - from;
4489 if (from == 0 && phdr)
4491 /* Include the headers in the first PT_LOAD segment. */
4492 m->includes_filehdr = 1;
4493 m->includes_phdrs = 1;
4499 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4502 struct elf_segment_map *
4503 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4505 struct elf_segment_map *m;
4507 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4508 sizeof (struct elf_segment_map));
4512 m->p_type = PT_DYNAMIC;
4514 m->sections[0] = dynsec;
4519 /* Possibly add or remove segments from the segment map. */
4522 elf_modify_segment_map (bfd *abfd,
4523 struct bfd_link_info *info,
4524 bfd_boolean remove_empty_load)
4526 struct elf_segment_map **m;
4527 const struct elf_backend_data *bed;
4529 /* The placement algorithm assumes that non allocated sections are
4530 not in PT_LOAD segments. We ensure this here by removing such
4531 sections from the segment map. We also remove excluded
4532 sections. Finally, any PT_LOAD segment without sections is
4534 m = &elf_seg_map (abfd);
4537 unsigned int i, new_count;
4539 for (new_count = 0, i = 0; i < (*m)->count; i++)
4541 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4542 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4543 || (*m)->p_type != PT_LOAD))
4545 (*m)->sections[new_count] = (*m)->sections[i];
4549 (*m)->count = new_count;
4551 if (remove_empty_load
4552 && (*m)->p_type == PT_LOAD
4554 && !(*m)->includes_phdrs)
4560 bed = get_elf_backend_data (abfd);
4561 if (bed->elf_backend_modify_segment_map != NULL)
4563 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4570 #define IS_TBSS(s) \
4571 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4573 /* Set up a mapping from BFD sections to program segments. */
4576 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4579 struct elf_segment_map *m;
4580 asection **sections = NULL;
4581 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4582 bfd_boolean no_user_phdrs;
4584 no_user_phdrs = elf_seg_map (abfd) == NULL;
4587 info->user_phdrs = !no_user_phdrs;
4589 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4593 struct elf_segment_map *mfirst;
4594 struct elf_segment_map **pm;
4597 unsigned int hdr_index;
4598 bfd_vma maxpagesize;
4600 bfd_boolean phdr_in_segment = TRUE;
4601 bfd_boolean writable;
4602 bfd_boolean executable;
4604 asection *first_tls = NULL;
4605 asection *first_mbind = NULL;
4606 asection *dynsec, *eh_frame_hdr;
4608 bfd_vma addr_mask, wrap_to = 0;
4609 bfd_boolean linker_created_pt_phdr_segment = FALSE;
4611 /* Select the allocated sections, and sort them. */
4613 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4614 sizeof (asection *));
4615 if (sections == NULL)
4618 /* Calculate top address, avoiding undefined behaviour of shift
4619 left operator when shift count is equal to size of type
4621 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4622 addr_mask = (addr_mask << 1) + 1;
4625 for (s = abfd->sections; s != NULL; s = s->next)
4627 if ((s->flags & SEC_ALLOC) != 0)
4631 /* A wrapping section potentially clashes with header. */
4632 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4633 wrap_to = (s->lma + s->size) & addr_mask;
4636 BFD_ASSERT (i <= bfd_count_sections (abfd));
4639 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4641 /* Build the mapping. */
4646 /* If we have a .interp section, then create a PT_PHDR segment for
4647 the program headers and a PT_INTERP segment for the .interp
4649 s = bfd_get_section_by_name (abfd, ".interp");
4650 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4652 amt = sizeof (struct elf_segment_map);
4653 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4657 m->p_type = PT_PHDR;
4659 m->p_flags_valid = 1;
4660 m->includes_phdrs = 1;
4661 linker_created_pt_phdr_segment = TRUE;
4665 amt = sizeof (struct elf_segment_map);
4666 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4670 m->p_type = PT_INTERP;
4678 /* Look through the sections. We put sections in the same program
4679 segment when the start of the second section can be placed within
4680 a few bytes of the end of the first section. */
4684 maxpagesize = bed->maxpagesize;
4685 /* PR 17512: file: c8455299.
4686 Avoid divide-by-zero errors later on.
4687 FIXME: Should we abort if the maxpagesize is zero ? */
4688 if (maxpagesize == 0)
4692 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4694 && (dynsec->flags & SEC_LOAD) == 0)
4697 /* Deal with -Ttext or something similar such that the first section
4698 is not adjacent to the program headers. This is an
4699 approximation, since at this point we don't know exactly how many
4700 program headers we will need. */
4703 bfd_size_type phdr_size = elf_program_header_size (abfd);
4705 if (phdr_size == (bfd_size_type) -1)
4706 phdr_size = get_program_header_size (abfd, info);
4707 phdr_size += bed->s->sizeof_ehdr;
4708 if ((abfd->flags & D_PAGED) == 0
4709 || (sections[0]->lma & addr_mask) < phdr_size
4710 || ((sections[0]->lma & addr_mask) % maxpagesize
4711 < phdr_size % maxpagesize)
4712 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4714 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4715 present, must be included as part of the memory image of the
4716 program. Ie it must be part of a PT_LOAD segment as well.
4717 If we have had to create our own PT_PHDR segment, but it is
4718 not going to be covered by the first PT_LOAD segment, then
4719 force the inclusion if we can... */
4720 if ((abfd->flags & D_PAGED) != 0
4721 && linker_created_pt_phdr_segment)
4722 phdr_in_segment = TRUE;
4724 phdr_in_segment = FALSE;
4728 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4731 bfd_boolean new_segment;
4735 /* See if this section and the last one will fit in the same
4738 if (last_hdr == NULL)
4740 /* If we don't have a segment yet, then we don't need a new
4741 one (we build the last one after this loop). */
4742 new_segment = FALSE;
4744 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4746 /* If this section has a different relation between the
4747 virtual address and the load address, then we need a new
4751 else if (hdr->lma < last_hdr->lma + last_size
4752 || last_hdr->lma + last_size < last_hdr->lma)
4754 /* If this section has a load address that makes it overlap
4755 the previous section, then we need a new segment. */
4758 else if ((abfd->flags & D_PAGED) != 0
4759 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4760 == (hdr->lma & -maxpagesize)))
4762 /* If we are demand paged then we can't map two disk
4763 pages onto the same memory page. */
4764 new_segment = FALSE;
4766 /* In the next test we have to be careful when last_hdr->lma is close
4767 to the end of the address space. If the aligned address wraps
4768 around to the start of the address space, then there are no more
4769 pages left in memory and it is OK to assume that the current
4770 section can be included in the current segment. */
4771 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4772 + maxpagesize > last_hdr->lma)
4773 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4774 + maxpagesize <= hdr->lma))
4776 /* If putting this section in this segment would force us to
4777 skip a page in the segment, then we need a new segment. */
4780 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4781 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4783 /* We don't want to put a loaded section after a
4784 nonloaded (ie. bss style) section in the same segment
4785 as that will force the non-loaded section to be loaded.
4786 Consider .tbss sections as loaded for this purpose. */
4789 else if ((abfd->flags & D_PAGED) == 0)
4791 /* If the file is not demand paged, which means that we
4792 don't require the sections to be correctly aligned in the
4793 file, then there is no other reason for a new segment. */
4794 new_segment = FALSE;
4796 else if (info != NULL
4797 && info->separate_code
4798 && executable != ((hdr->flags & SEC_CODE) != 0))
4803 && (hdr->flags & SEC_READONLY) == 0)
4805 /* We don't want to put a writable section in a read only
4811 /* Otherwise, we can use the same segment. */
4812 new_segment = FALSE;
4815 /* Allow interested parties a chance to override our decision. */
4816 if (last_hdr != NULL
4818 && info->callbacks->override_segment_assignment != NULL)
4820 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4826 if ((hdr->flags & SEC_READONLY) == 0)
4828 if ((hdr->flags & SEC_CODE) != 0)
4831 /* .tbss sections effectively have zero size. */
4832 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4836 /* We need a new program segment. We must create a new program
4837 header holding all the sections from hdr_index until hdr. */
4839 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4846 if ((hdr->flags & SEC_READONLY) == 0)
4851 if ((hdr->flags & SEC_CODE) == 0)
4857 /* .tbss sections effectively have zero size. */
4858 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4860 phdr_in_segment = FALSE;
4863 /* Create a final PT_LOAD program segment, but not if it's just
4865 if (last_hdr != NULL
4866 && (i - hdr_index != 1
4867 || !IS_TBSS (last_hdr)))
4869 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4877 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4880 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4887 /* For each batch of consecutive loadable SHT_NOTE sections,
4888 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4889 because if we link together nonloadable .note sections and
4890 loadable .note sections, we will generate two .note sections
4891 in the output file. */
4892 for (s = abfd->sections; s != NULL; s = s->next)
4894 if ((s->flags & SEC_LOAD) != 0
4895 && elf_section_type (s) == SHT_NOTE)
4898 unsigned int alignment_power = s->alignment_power;
4901 for (s2 = s; s2->next != NULL; s2 = s2->next)
4903 if (s2->next->alignment_power == alignment_power
4904 && (s2->next->flags & SEC_LOAD) != 0
4905 && elf_section_type (s2->next) == SHT_NOTE
4906 && align_power (s2->lma + s2->size,
4913 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4914 amt += count * sizeof (asection *);
4915 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4919 m->p_type = PT_NOTE;
4923 m->sections[m->count - count--] = s;
4924 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4927 m->sections[m->count - 1] = s;
4928 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4932 if (s->flags & SEC_THREAD_LOCAL)
4938 if (first_mbind == NULL
4939 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
4943 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4946 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4947 amt += tls_count * sizeof (asection *);
4948 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4953 m->count = tls_count;
4954 /* Mandated PF_R. */
4956 m->p_flags_valid = 1;
4958 for (i = 0; i < (unsigned int) tls_count; ++i)
4960 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4963 (_("%pB: TLS sections are not adjacent:"), abfd);
4966 while (i < (unsigned int) tls_count)
4968 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4970 _bfd_error_handler (_(" TLS: %pA"), s);
4974 _bfd_error_handler (_(" non-TLS: %pA"), s);
4977 bfd_set_error (bfd_error_bad_value);
4988 if (first_mbind && (abfd->flags & D_PAGED) != 0)
4989 for (s = first_mbind; s != NULL; s = s->next)
4990 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
4991 && (elf_section_data (s)->this_hdr.sh_info
4992 <= PT_GNU_MBIND_NUM))
4994 /* Mandated PF_R. */
4995 unsigned long p_flags = PF_R;
4996 if ((s->flags & SEC_READONLY) == 0)
4998 if ((s->flags & SEC_CODE) != 0)
5001 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5002 m = bfd_zalloc (abfd, amt);
5006 m->p_type = (PT_GNU_MBIND_LO
5007 + elf_section_data (s)->this_hdr.sh_info);
5009 m->p_flags_valid = 1;
5011 m->p_flags = p_flags;
5017 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5019 eh_frame_hdr = elf_eh_frame_hdr (abfd);
5020 if (eh_frame_hdr != NULL
5021 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5023 amt = sizeof (struct elf_segment_map);
5024 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5028 m->p_type = PT_GNU_EH_FRAME;
5030 m->sections[0] = eh_frame_hdr->output_section;
5036 if (elf_stack_flags (abfd))
5038 amt = sizeof (struct elf_segment_map);
5039 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5043 m->p_type = PT_GNU_STACK;
5044 m->p_flags = elf_stack_flags (abfd);
5045 m->p_align = bed->stack_align;
5046 m->p_flags_valid = 1;
5047 m->p_align_valid = m->p_align != 0;
5048 if (info->stacksize > 0)
5050 m->p_size = info->stacksize;
5051 m->p_size_valid = 1;
5058 if (info != NULL && info->relro)
5060 for (m = mfirst; m != NULL; m = m->next)
5062 if (m->p_type == PT_LOAD
5064 && m->sections[0]->vma >= info->relro_start
5065 && m->sections[0]->vma < info->relro_end)
5068 while (--i != (unsigned) -1)
5069 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5070 == (SEC_LOAD | SEC_HAS_CONTENTS))
5073 if (i != (unsigned) -1)
5078 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5081 amt = sizeof (struct elf_segment_map);
5082 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5086 m->p_type = PT_GNU_RELRO;
5093 elf_seg_map (abfd) = mfirst;
5096 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5099 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5101 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5106 if (sections != NULL)
5111 /* Sort sections by address. */
5114 elf_sort_sections (const void *arg1, const void *arg2)
5116 const asection *sec1 = *(const asection **) arg1;
5117 const asection *sec2 = *(const asection **) arg2;
5118 bfd_size_type size1, size2;
5120 /* Sort by LMA first, since this is the address used to
5121 place the section into a segment. */
5122 if (sec1->lma < sec2->lma)
5124 else if (sec1->lma > sec2->lma)
5127 /* Then sort by VMA. Normally the LMA and the VMA will be
5128 the same, and this will do nothing. */
5129 if (sec1->vma < sec2->vma)
5131 else if (sec1->vma > sec2->vma)
5134 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5136 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5142 /* If the indices are the same, do not return 0
5143 here, but continue to try the next comparison. */
5144 if (sec1->target_index - sec2->target_index != 0)
5145 return sec1->target_index - sec2->target_index;
5150 else if (TOEND (sec2))
5155 /* Sort by size, to put zero sized sections
5156 before others at the same address. */
5158 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5159 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5166 return sec1->target_index - sec2->target_index;
5169 /* Ian Lance Taylor writes:
5171 We shouldn't be using % with a negative signed number. That's just
5172 not good. We have to make sure either that the number is not
5173 negative, or that the number has an unsigned type. When the types
5174 are all the same size they wind up as unsigned. When file_ptr is a
5175 larger signed type, the arithmetic winds up as signed long long,
5178 What we're trying to say here is something like ``increase OFF by
5179 the least amount that will cause it to be equal to the VMA modulo
5181 /* In other words, something like:
5183 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5184 off_offset = off % bed->maxpagesize;
5185 if (vma_offset < off_offset)
5186 adjustment = vma_offset + bed->maxpagesize - off_offset;
5188 adjustment = vma_offset - off_offset;
5190 which can be collapsed into the expression below. */
5193 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5195 /* PR binutils/16199: Handle an alignment of zero. */
5196 if (maxpagesize == 0)
5198 return ((vma - off) % maxpagesize);
5202 print_segment_map (const struct elf_segment_map *m)
5205 const char *pt = get_segment_type (m->p_type);
5210 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5211 sprintf (buf, "LOPROC+%7.7x",
5212 (unsigned int) (m->p_type - PT_LOPROC));
5213 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5214 sprintf (buf, "LOOS+%7.7x",
5215 (unsigned int) (m->p_type - PT_LOOS));
5217 snprintf (buf, sizeof (buf), "%8.8x",
5218 (unsigned int) m->p_type);
5222 fprintf (stderr, "%s:", pt);
5223 for (j = 0; j < m->count; j++)
5224 fprintf (stderr, " %s", m->sections [j]->name);
5230 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5235 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5237 buf = bfd_zmalloc (len);
5240 ret = bfd_bwrite (buf, len, abfd) == len;
5245 /* Assign file positions to the sections based on the mapping from
5246 sections to segments. This function also sets up some fields in
5250 assign_file_positions_for_load_sections (bfd *abfd,
5251 struct bfd_link_info *link_info)
5253 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5254 struct elf_segment_map *m;
5255 Elf_Internal_Phdr *phdrs;
5256 Elf_Internal_Phdr *p;
5258 bfd_size_type maxpagesize;
5259 unsigned int pt_load_count = 0;
5262 bfd_vma header_pad = 0;
5264 if (link_info == NULL
5265 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5269 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5273 header_pad = m->header_size;
5278 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5279 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5283 /* PR binutils/12467. */
5284 elf_elfheader (abfd)->e_phoff = 0;
5285 elf_elfheader (abfd)->e_phentsize = 0;
5288 elf_elfheader (abfd)->e_phnum = alloc;
5290 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5291 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5293 BFD_ASSERT (elf_program_header_size (abfd)
5294 >= alloc * bed->s->sizeof_phdr);
5298 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5302 /* We're writing the size in elf_program_header_size (abfd),
5303 see assign_file_positions_except_relocs, so make sure we have
5304 that amount allocated, with trailing space cleared.
5305 The variable alloc contains the computed need, while
5306 elf_program_header_size (abfd) contains the size used for the
5308 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5309 where the layout is forced to according to a larger size in the
5310 last iterations for the testcase ld-elf/header. */
5311 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
5313 phdrs = (Elf_Internal_Phdr *)
5315 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5316 sizeof (Elf_Internal_Phdr));
5317 elf_tdata (abfd)->phdr = phdrs;
5322 if ((abfd->flags & D_PAGED) != 0)
5323 maxpagesize = bed->maxpagesize;
5325 off = bed->s->sizeof_ehdr;
5326 off += alloc * bed->s->sizeof_phdr;
5327 if (header_pad < (bfd_vma) off)
5333 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
5335 m = m->next, p++, j++)
5339 bfd_boolean no_contents;
5341 /* If elf_segment_map is not from map_sections_to_segments, the
5342 sections may not be correctly ordered. NOTE: sorting should
5343 not be done to the PT_NOTE section of a corefile, which may
5344 contain several pseudo-sections artificially created by bfd.
5345 Sorting these pseudo-sections breaks things badly. */
5347 && !(elf_elfheader (abfd)->e_type == ET_CORE
5348 && m->p_type == PT_NOTE))
5349 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5352 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5353 number of sections with contents contributing to both p_filesz
5354 and p_memsz, followed by a number of sections with no contents
5355 that just contribute to p_memsz. In this loop, OFF tracks next
5356 available file offset for PT_LOAD and PT_NOTE segments. */
5357 p->p_type = m->p_type;
5358 p->p_flags = m->p_flags;
5361 p->p_vaddr = m->p_vaddr_offset;
5363 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
5365 if (m->p_paddr_valid)
5366 p->p_paddr = m->p_paddr;
5367 else if (m->count == 0)
5370 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
5372 if (p->p_type == PT_LOAD
5373 && (abfd->flags & D_PAGED) != 0)
5375 /* p_align in demand paged PT_LOAD segments effectively stores
5376 the maximum page size. When copying an executable with
5377 objcopy, we set m->p_align from the input file. Use this
5378 value for maxpagesize rather than bed->maxpagesize, which
5379 may be different. Note that we use maxpagesize for PT_TLS
5380 segment alignment later in this function, so we are relying
5381 on at least one PT_LOAD segment appearing before a PT_TLS
5383 if (m->p_align_valid)
5384 maxpagesize = m->p_align;
5386 p->p_align = maxpagesize;
5389 else if (m->p_align_valid)
5390 p->p_align = m->p_align;
5391 else if (m->count == 0)
5392 p->p_align = 1 << bed->s->log_file_align;
5396 no_contents = FALSE;
5398 if (p->p_type == PT_LOAD
5401 bfd_size_type align;
5402 unsigned int align_power = 0;
5404 if (m->p_align_valid)
5408 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5410 unsigned int secalign;
5412 secalign = bfd_get_section_alignment (abfd, *secpp);
5413 if (secalign > align_power)
5414 align_power = secalign;
5416 align = (bfd_size_type) 1 << align_power;
5417 if (align < maxpagesize)
5418 align = maxpagesize;
5421 for (i = 0; i < m->count; i++)
5422 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5423 /* If we aren't making room for this section, then
5424 it must be SHT_NOBITS regardless of what we've
5425 set via struct bfd_elf_special_section. */
5426 elf_section_type (m->sections[i]) = SHT_NOBITS;
5428 /* Find out whether this segment contains any loadable
5431 for (i = 0; i < m->count; i++)
5432 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5434 no_contents = FALSE;
5438 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5440 /* Broken hardware and/or kernel require that files do not
5441 map the same page with different permissions on some hppa
5443 if (pt_load_count > 1
5444 && bed->no_page_alias
5445 && (off & (maxpagesize - 1)) != 0
5446 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5447 off_adjust += maxpagesize;
5451 /* We shouldn't need to align the segment on disk since
5452 the segment doesn't need file space, but the gABI
5453 arguably requires the alignment and glibc ld.so
5454 checks it. So to comply with the alignment
5455 requirement but not waste file space, we adjust
5456 p_offset for just this segment. (OFF_ADJUST is
5457 subtracted from OFF later.) This may put p_offset
5458 past the end of file, but that shouldn't matter. */
5463 /* Make sure the .dynamic section is the first section in the
5464 PT_DYNAMIC segment. */
5465 else if (p->p_type == PT_DYNAMIC
5467 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5470 (_("%pB: The first section in the PT_DYNAMIC segment"
5471 " is not the .dynamic section"),
5473 bfd_set_error (bfd_error_bad_value);
5476 /* Set the note section type to SHT_NOTE. */
5477 else if (p->p_type == PT_NOTE)
5478 for (i = 0; i < m->count; i++)
5479 elf_section_type (m->sections[i]) = SHT_NOTE;
5485 if (m->includes_filehdr)
5487 if (!m->p_flags_valid)
5489 p->p_filesz = bed->s->sizeof_ehdr;
5490 p->p_memsz = bed->s->sizeof_ehdr;
5493 if (p->p_vaddr < (bfd_vma) off
5494 || (!m->p_paddr_valid
5495 && p->p_paddr < (bfd_vma) off))
5498 (_("%pB: not enough room for program headers,"
5499 " try linking with -N"),
5501 bfd_set_error (bfd_error_bad_value);
5506 if (!m->p_paddr_valid)
5511 if (m->includes_phdrs)
5513 if (!m->p_flags_valid)
5516 if (!m->includes_filehdr)
5518 p->p_offset = bed->s->sizeof_ehdr;
5522 p->p_vaddr -= off - p->p_offset;
5523 if (!m->p_paddr_valid)
5524 p->p_paddr -= off - p->p_offset;
5528 p->p_filesz += alloc * bed->s->sizeof_phdr;
5529 p->p_memsz += alloc * bed->s->sizeof_phdr;
5532 p->p_filesz += header_pad;
5533 p->p_memsz += header_pad;
5537 if (p->p_type == PT_LOAD
5538 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5540 if (!m->includes_filehdr && !m->includes_phdrs)
5546 adjust = off - (p->p_offset + p->p_filesz);
5548 p->p_filesz += adjust;
5549 p->p_memsz += adjust;
5553 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5554 maps. Set filepos for sections in PT_LOAD segments, and in
5555 core files, for sections in PT_NOTE segments.
5556 assign_file_positions_for_non_load_sections will set filepos
5557 for other sections and update p_filesz for other segments. */
5558 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5561 bfd_size_type align;
5562 Elf_Internal_Shdr *this_hdr;
5565 this_hdr = &elf_section_data (sec)->this_hdr;
5566 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5568 if ((p->p_type == PT_LOAD
5569 || p->p_type == PT_TLS)
5570 && (this_hdr->sh_type != SHT_NOBITS
5571 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5572 && ((this_hdr->sh_flags & SHF_TLS) == 0
5573 || p->p_type == PT_TLS))))
5575 bfd_vma p_start = p->p_paddr;
5576 bfd_vma p_end = p_start + p->p_memsz;
5577 bfd_vma s_start = sec->lma;
5578 bfd_vma adjust = s_start - p_end;
5582 || p_end < p_start))
5585 /* xgettext:c-format */
5586 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5587 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
5591 p->p_memsz += adjust;
5593 if (this_hdr->sh_type != SHT_NOBITS)
5595 if (p->p_filesz + adjust < p->p_memsz)
5597 /* We have a PROGBITS section following NOBITS ones.
5598 Allocate file space for the NOBITS section(s) and
5600 adjust = p->p_memsz - p->p_filesz;
5601 if (!write_zeros (abfd, off, adjust))
5605 p->p_filesz += adjust;
5609 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5611 /* The section at i == 0 is the one that actually contains
5615 this_hdr->sh_offset = sec->filepos = off;
5616 off += this_hdr->sh_size;
5617 p->p_filesz = this_hdr->sh_size;
5623 /* The rest are fake sections that shouldn't be written. */
5632 if (p->p_type == PT_LOAD)
5634 this_hdr->sh_offset = sec->filepos = off;
5635 if (this_hdr->sh_type != SHT_NOBITS)
5636 off += this_hdr->sh_size;
5638 else if (this_hdr->sh_type == SHT_NOBITS
5639 && (this_hdr->sh_flags & SHF_TLS) != 0
5640 && this_hdr->sh_offset == 0)
5642 /* This is a .tbss section that didn't get a PT_LOAD.
5643 (See _bfd_elf_map_sections_to_segments "Create a
5644 final PT_LOAD".) Set sh_offset to the value it
5645 would have if we had created a zero p_filesz and
5646 p_memsz PT_LOAD header for the section. This
5647 also makes the PT_TLS header have the same
5649 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5651 this_hdr->sh_offset = sec->filepos = off + adjust;
5654 if (this_hdr->sh_type != SHT_NOBITS)
5656 p->p_filesz += this_hdr->sh_size;
5657 /* A load section without SHF_ALLOC is something like
5658 a note section in a PT_NOTE segment. These take
5659 file space but are not loaded into memory. */
5660 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5661 p->p_memsz += this_hdr->sh_size;
5663 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5665 if (p->p_type == PT_TLS)
5666 p->p_memsz += this_hdr->sh_size;
5668 /* .tbss is special. It doesn't contribute to p_memsz of
5670 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5671 p->p_memsz += this_hdr->sh_size;
5674 if (align > p->p_align
5675 && !m->p_align_valid
5676 && (p->p_type != PT_LOAD
5677 || (abfd->flags & D_PAGED) == 0))
5681 if (!m->p_flags_valid)
5684 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5686 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5693 /* Check that all sections are in a PT_LOAD segment.
5694 Don't check funky gdb generated core files. */
5695 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5697 bfd_boolean check_vma = TRUE;
5699 for (i = 1; i < m->count; i++)
5700 if (m->sections[i]->vma == m->sections[i - 1]->vma
5701 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5702 ->this_hdr), p) != 0
5703 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5704 ->this_hdr), p) != 0)
5706 /* Looks like we have overlays packed into the segment. */
5711 for (i = 0; i < m->count; i++)
5713 Elf_Internal_Shdr *this_hdr;
5716 sec = m->sections[i];
5717 this_hdr = &(elf_section_data(sec)->this_hdr);
5718 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5719 && !ELF_TBSS_SPECIAL (this_hdr, p))
5722 /* xgettext:c-format */
5723 (_("%pB: section `%pA' can't be allocated in segment %d"),
5725 print_segment_map (m);
5731 elf_next_file_pos (abfd) = off;
5735 /* Assign file positions for the other sections. */
5738 assign_file_positions_for_non_load_sections (bfd *abfd,
5739 struct bfd_link_info *link_info)
5741 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5742 Elf_Internal_Shdr **i_shdrpp;
5743 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5744 Elf_Internal_Phdr *phdrs;
5745 Elf_Internal_Phdr *p;
5746 struct elf_segment_map *m;
5747 struct elf_segment_map *hdrs_segment;
5748 bfd_vma filehdr_vaddr, filehdr_paddr;
5749 bfd_vma phdrs_vaddr, phdrs_paddr;
5753 i_shdrpp = elf_elfsections (abfd);
5754 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5755 off = elf_next_file_pos (abfd);
5756 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5758 Elf_Internal_Shdr *hdr;
5761 if (hdr->bfd_section != NULL
5762 && (hdr->bfd_section->filepos != 0
5763 || (hdr->sh_type == SHT_NOBITS
5764 && hdr->contents == NULL)))
5765 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5766 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5768 if (hdr->sh_size != 0)
5770 /* xgettext:c-format */
5771 (_("%pB: warning: allocated section `%s' not in segment"),
5773 (hdr->bfd_section == NULL
5775 : hdr->bfd_section->name));
5776 /* We don't need to page align empty sections. */
5777 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5778 off += vma_page_aligned_bias (hdr->sh_addr, off,
5781 off += vma_page_aligned_bias (hdr->sh_addr, off,
5783 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5786 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5787 && hdr->bfd_section == NULL)
5788 || (hdr->bfd_section != NULL
5789 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5790 /* Compress DWARF debug sections. */
5791 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5792 || (elf_symtab_shndx_list (abfd) != NULL
5793 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5794 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5795 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5796 hdr->sh_offset = -1;
5798 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5801 /* Now that we have set the section file positions, we can set up
5802 the file positions for the non PT_LOAD segments. */
5806 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5808 hdrs_segment = NULL;
5809 phdrs = elf_tdata (abfd)->phdr;
5810 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5813 if (p->p_type != PT_LOAD)
5816 if (m->includes_filehdr)
5818 filehdr_vaddr = p->p_vaddr;
5819 filehdr_paddr = p->p_paddr;
5821 if (m->includes_phdrs)
5823 phdrs_vaddr = p->p_vaddr;
5824 phdrs_paddr = p->p_paddr;
5825 if (m->includes_filehdr)
5828 phdrs_vaddr += bed->s->sizeof_ehdr;
5829 phdrs_paddr += bed->s->sizeof_ehdr;
5834 if (hdrs_segment != NULL && link_info != NULL)
5836 /* There is a segment that contains both the file headers and the
5837 program headers, so provide a symbol __ehdr_start pointing there.
5838 A program can use this to examine itself robustly. */
5840 struct elf_link_hash_entry *hash
5841 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5842 FALSE, FALSE, TRUE);
5843 /* If the symbol was referenced and not defined, define it. */
5845 && (hash->root.type == bfd_link_hash_new
5846 || hash->root.type == bfd_link_hash_undefined
5847 || hash->root.type == bfd_link_hash_undefweak
5848 || hash->root.type == bfd_link_hash_common))
5851 if (hdrs_segment->count != 0)
5852 /* The segment contains sections, so use the first one. */
5853 s = hdrs_segment->sections[0];
5855 /* Use the first (i.e. lowest-addressed) section in any segment. */
5856 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5865 hash->root.u.def.value = filehdr_vaddr - s->vma;
5866 hash->root.u.def.section = s;
5870 hash->root.u.def.value = filehdr_vaddr;
5871 hash->root.u.def.section = bfd_abs_section_ptr;
5874 hash->root.type = bfd_link_hash_defined;
5875 hash->def_regular = 1;
5880 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5882 if (p->p_type == PT_GNU_RELRO)
5887 if (link_info != NULL)
5889 /* During linking the range of the RELRO segment is passed
5890 in link_info. Note that there may be padding between
5891 relro_start and the first RELRO section. */
5892 start = link_info->relro_start;
5893 end = link_info->relro_end;
5895 else if (m->count != 0)
5897 if (!m->p_size_valid)
5899 start = m->sections[0]->vma;
5900 end = start + m->p_size;
5911 struct elf_segment_map *lm;
5912 const Elf_Internal_Phdr *lp;
5915 /* Find a LOAD segment containing a section in the RELRO
5917 for (lm = elf_seg_map (abfd), lp = phdrs;
5919 lm = lm->next, lp++)
5921 if (lp->p_type == PT_LOAD
5923 && (lm->sections[lm->count - 1]->vma
5924 + (!IS_TBSS (lm->sections[lm->count - 1])
5925 ? lm->sections[lm->count - 1]->size
5927 && lm->sections[0]->vma < end)
5933 /* Find the section starting the RELRO segment. */
5934 for (i = 0; i < lm->count; i++)
5936 asection *s = lm->sections[i];
5945 p->p_vaddr = lm->sections[i]->vma;
5946 p->p_paddr = lm->sections[i]->lma;
5947 p->p_offset = lm->sections[i]->filepos;
5948 p->p_memsz = end - p->p_vaddr;
5949 p->p_filesz = p->p_memsz;
5951 /* The RELRO segment typically ends a few bytes
5952 into .got.plt but other layouts are possible.
5953 In cases where the end does not match any
5954 loaded section (for instance is in file
5955 padding), trim p_filesz back to correspond to
5956 the end of loaded section contents. */
5957 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
5958 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
5960 /* Preserve the alignment and flags if they are
5961 valid. The gold linker generates RW/4 for
5962 the PT_GNU_RELRO section. It is better for
5963 objcopy/strip to honor these attributes
5964 otherwise gdb will choke when using separate
5966 if (!m->p_align_valid)
5968 if (!m->p_flags_valid)
5974 if (link_info != NULL)
5977 memset (p, 0, sizeof *p);
5979 else if (p->p_type == PT_GNU_STACK)
5981 if (m->p_size_valid)
5982 p->p_memsz = m->p_size;
5984 else if (m->count != 0)
5988 if (p->p_type != PT_LOAD
5989 && (p->p_type != PT_NOTE
5990 || bfd_get_format (abfd) != bfd_core))
5992 /* A user specified segment layout may include a PHDR
5993 segment that overlaps with a LOAD segment... */
5994 if (p->p_type == PT_PHDR)
6000 if (m->includes_filehdr || m->includes_phdrs)
6002 /* PR 17512: file: 2195325e. */
6004 (_("%pB: error: non-load segment %d includes file header "
6005 "and/or program header"),
6006 abfd, (int) (p - phdrs));
6011 p->p_offset = m->sections[0]->filepos;
6012 for (i = m->count; i-- != 0;)
6014 asection *sect = m->sections[i];
6015 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6016 if (hdr->sh_type != SHT_NOBITS)
6018 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6025 else if (m->includes_filehdr)
6027 p->p_vaddr = filehdr_vaddr;
6028 if (! m->p_paddr_valid)
6029 p->p_paddr = filehdr_paddr;
6031 else if (m->includes_phdrs)
6033 p->p_vaddr = phdrs_vaddr;
6034 if (! m->p_paddr_valid)
6035 p->p_paddr = phdrs_paddr;
6039 elf_next_file_pos (abfd) = off;
6044 static elf_section_list *
6045 find_section_in_list (unsigned int i, elf_section_list * list)
6047 for (;list != NULL; list = list->next)
6053 /* Work out the file positions of all the sections. This is called by
6054 _bfd_elf_compute_section_file_positions. All the section sizes and
6055 VMAs must be known before this is called.
6057 Reloc sections come in two flavours: Those processed specially as
6058 "side-channel" data attached to a section to which they apply, and
6059 those that bfd doesn't process as relocations. The latter sort are
6060 stored in a normal bfd section by bfd_section_from_shdr. We don't
6061 consider the former sort here, unless they form part of the loadable
6062 image. Reloc sections not assigned here will be handled later by
6063 assign_file_positions_for_relocs.
6065 We also don't set the positions of the .symtab and .strtab here. */
6068 assign_file_positions_except_relocs (bfd *abfd,
6069 struct bfd_link_info *link_info)
6071 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6072 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6073 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6075 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6076 && bfd_get_format (abfd) != bfd_core)
6078 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6079 unsigned int num_sec = elf_numsections (abfd);
6080 Elf_Internal_Shdr **hdrpp;
6084 /* Start after the ELF header. */
6085 off = i_ehdrp->e_ehsize;
6087 /* We are not creating an executable, which means that we are
6088 not creating a program header, and that the actual order of
6089 the sections in the file is unimportant. */
6090 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6092 Elf_Internal_Shdr *hdr;
6095 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6096 && hdr->bfd_section == NULL)
6097 || (hdr->bfd_section != NULL
6098 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6099 /* Compress DWARF debug sections. */
6100 || i == elf_onesymtab (abfd)
6101 || (elf_symtab_shndx_list (abfd) != NULL
6102 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6103 || i == elf_strtab_sec (abfd)
6104 || i == elf_shstrtab_sec (abfd))
6106 hdr->sh_offset = -1;
6109 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6112 elf_next_file_pos (abfd) = off;
6118 /* Assign file positions for the loaded sections based on the
6119 assignment of sections to segments. */
6120 if (!assign_file_positions_for_load_sections (abfd, link_info))
6123 /* And for non-load sections. */
6124 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6127 if (bed->elf_backend_modify_program_headers != NULL)
6129 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6133 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6134 if (link_info != NULL && bfd_link_pie (link_info))
6136 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6137 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6138 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6140 /* Find the lowest p_vaddr in PT_LOAD segments. */
6141 bfd_vma p_vaddr = (bfd_vma) -1;
6142 for (; segment < end_segment; segment++)
6143 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6144 p_vaddr = segment->p_vaddr;
6146 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6147 segments is non-zero. */
6149 i_ehdrp->e_type = ET_EXEC;
6152 /* Write out the program headers. */
6153 alloc = elf_elfheader (abfd)->e_phnum;
6157 /* PR ld/20815 - Check that the program header segment, if present, will
6158 be loaded into memory. FIXME: The check below is not sufficient as
6159 really all PT_LOAD segments should be checked before issuing an error
6160 message. Plus the PHDR segment does not have to be the first segment
6161 in the program header table. But this version of the check should
6162 catch all real world use cases.
6164 FIXME: We used to have code here to sort the PT_LOAD segments into
6165 ascending order, as per the ELF spec. But this breaks some programs,
6166 including the Linux kernel. But really either the spec should be
6167 changed or the programs updated. */
6169 && tdata->phdr[0].p_type == PT_PHDR
6170 && (bed->elf_backend_allow_non_load_phdr == NULL
6171 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6173 && tdata->phdr[1].p_type == PT_LOAD
6174 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6175 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6176 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
6178 /* The fix for this error is usually to edit the linker script being
6179 used and set up the program headers manually. Either that or
6180 leave room for the headers at the start of the SECTIONS. */
6181 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6182 " by LOAD segment"),
6187 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
6188 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6196 prep_headers (bfd *abfd)
6198 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6199 struct elf_strtab_hash *shstrtab;
6200 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6202 i_ehdrp = elf_elfheader (abfd);
6204 shstrtab = _bfd_elf_strtab_init ();
6205 if (shstrtab == NULL)
6208 elf_shstrtab (abfd) = shstrtab;
6210 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6211 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6212 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6213 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6215 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6216 i_ehdrp->e_ident[EI_DATA] =
6217 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6218 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6220 if ((abfd->flags & DYNAMIC) != 0)
6221 i_ehdrp->e_type = ET_DYN;
6222 else if ((abfd->flags & EXEC_P) != 0)
6223 i_ehdrp->e_type = ET_EXEC;
6224 else if (bfd_get_format (abfd) == bfd_core)
6225 i_ehdrp->e_type = ET_CORE;
6227 i_ehdrp->e_type = ET_REL;
6229 switch (bfd_get_arch (abfd))
6231 case bfd_arch_unknown:
6232 i_ehdrp->e_machine = EM_NONE;
6235 /* There used to be a long list of cases here, each one setting
6236 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6237 in the corresponding bfd definition. To avoid duplication,
6238 the switch was removed. Machines that need special handling
6239 can generally do it in elf_backend_final_write_processing(),
6240 unless they need the information earlier than the final write.
6241 Such need can generally be supplied by replacing the tests for
6242 e_machine with the conditions used to determine it. */
6244 i_ehdrp->e_machine = bed->elf_machine_code;
6247 i_ehdrp->e_version = bed->s->ev_current;
6248 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6250 /* No program header, for now. */
6251 i_ehdrp->e_phoff = 0;
6252 i_ehdrp->e_phentsize = 0;
6253 i_ehdrp->e_phnum = 0;
6255 /* Each bfd section is section header entry. */
6256 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6257 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6259 /* If we're building an executable, we'll need a program header table. */
6260 if (abfd->flags & EXEC_P)
6261 /* It all happens later. */
6265 i_ehdrp->e_phentsize = 0;
6266 i_ehdrp->e_phoff = 0;
6269 elf_tdata (abfd)->symtab_hdr.sh_name =
6270 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6271 elf_tdata (abfd)->strtab_hdr.sh_name =
6272 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6273 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6274 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6275 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6276 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6277 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6283 /* Assign file positions for all the reloc sections which are not part
6284 of the loadable file image, and the file position of section headers. */
6287 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6290 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6291 Elf_Internal_Shdr *shdrp;
6292 Elf_Internal_Ehdr *i_ehdrp;
6293 const struct elf_backend_data *bed;
6295 off = elf_next_file_pos (abfd);
6297 shdrpp = elf_elfsections (abfd);
6298 end_shdrpp = shdrpp + elf_numsections (abfd);
6299 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6302 if (shdrp->sh_offset == -1)
6304 asection *sec = shdrp->bfd_section;
6305 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6306 || shdrp->sh_type == SHT_RELA);
6308 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6312 const char *name = sec->name;
6313 struct bfd_elf_section_data *d;
6315 /* Compress DWARF debug sections. */
6316 if (!bfd_compress_section (abfd, sec,
6320 if (sec->compress_status == COMPRESS_SECTION_DONE
6321 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6323 /* If section is compressed with zlib-gnu, convert
6324 section name from .debug_* to .zdebug_*. */
6326 = convert_debug_to_zdebug (abfd, name);
6327 if (new_name == NULL)
6331 /* Add section name to section name section. */
6332 if (shdrp->sh_name != (unsigned int) -1)
6335 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6337 d = elf_section_data (sec);
6339 /* Add reloc section name to section name section. */
6341 && !_bfd_elf_set_reloc_sh_name (abfd,
6346 && !_bfd_elf_set_reloc_sh_name (abfd,
6351 /* Update section size and contents. */
6352 shdrp->sh_size = sec->size;
6353 shdrp->contents = sec->contents;
6354 shdrp->bfd_section->contents = NULL;
6356 off = _bfd_elf_assign_file_position_for_section (shdrp,
6363 /* Place section name section after DWARF debug sections have been
6365 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6366 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6367 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6368 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6370 /* Place the section headers. */
6371 i_ehdrp = elf_elfheader (abfd);
6372 bed = get_elf_backend_data (abfd);
6373 off = align_file_position (off, 1 << bed->s->log_file_align);
6374 i_ehdrp->e_shoff = off;
6375 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6376 elf_next_file_pos (abfd) = off;
6382 _bfd_elf_write_object_contents (bfd *abfd)
6384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6385 Elf_Internal_Shdr **i_shdrp;
6387 unsigned int count, num_sec;
6388 struct elf_obj_tdata *t;
6390 if (! abfd->output_has_begun
6391 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6393 /* Do not rewrite ELF data when the BFD has been opened for update.
6394 abfd->output_has_begun was set to TRUE on opening, so creation of new
6395 sections, and modification of existing section sizes was restricted.
6396 This means the ELF header, program headers and section headers can't have
6398 If the contents of any sections has been modified, then those changes have
6399 already been written to the BFD. */
6400 else if (abfd->direction == both_direction)
6402 BFD_ASSERT (abfd->output_has_begun);
6406 i_shdrp = elf_elfsections (abfd);
6409 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6413 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6416 /* After writing the headers, we need to write the sections too... */
6417 num_sec = elf_numsections (abfd);
6418 for (count = 1; count < num_sec; count++)
6420 i_shdrp[count]->sh_name
6421 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6422 i_shdrp[count]->sh_name);
6423 if (bed->elf_backend_section_processing)
6424 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6426 if (i_shdrp[count]->contents)
6428 bfd_size_type amt = i_shdrp[count]->sh_size;
6430 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6431 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6436 /* Write out the section header names. */
6437 t = elf_tdata (abfd);
6438 if (elf_shstrtab (abfd) != NULL
6439 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6440 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6443 if (bed->elf_backend_final_write_processing)
6444 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
6446 if (!bed->s->write_shdrs_and_ehdr (abfd))
6449 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6450 if (t->o->build_id.after_write_object_contents != NULL)
6451 return (*t->o->build_id.after_write_object_contents) (abfd);
6457 _bfd_elf_write_corefile_contents (bfd *abfd)
6459 /* Hopefully this can be done just like an object file. */
6460 return _bfd_elf_write_object_contents (abfd);
6463 /* Given a section, search the header to find them. */
6466 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6468 const struct elf_backend_data *bed;
6469 unsigned int sec_index;
6471 if (elf_section_data (asect) != NULL
6472 && elf_section_data (asect)->this_idx != 0)
6473 return elf_section_data (asect)->this_idx;
6475 if (bfd_is_abs_section (asect))
6476 sec_index = SHN_ABS;
6477 else if (bfd_is_com_section (asect))
6478 sec_index = SHN_COMMON;
6479 else if (bfd_is_und_section (asect))
6480 sec_index = SHN_UNDEF;
6482 sec_index = SHN_BAD;
6484 bed = get_elf_backend_data (abfd);
6485 if (bed->elf_backend_section_from_bfd_section)
6487 int retval = sec_index;
6489 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6493 if (sec_index == SHN_BAD)
6494 bfd_set_error (bfd_error_nonrepresentable_section);
6499 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6503 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6505 asymbol *asym_ptr = *asym_ptr_ptr;
6507 flagword flags = asym_ptr->flags;
6509 /* When gas creates relocations against local labels, it creates its
6510 own symbol for the section, but does put the symbol into the
6511 symbol chain, so udata is 0. When the linker is generating
6512 relocatable output, this section symbol may be for one of the
6513 input sections rather than the output section. */
6514 if (asym_ptr->udata.i == 0
6515 && (flags & BSF_SECTION_SYM)
6516 && asym_ptr->section)
6521 sec = asym_ptr->section;
6522 if (sec->owner != abfd && sec->output_section != NULL)
6523 sec = sec->output_section;
6524 if (sec->owner == abfd
6525 && (indx = sec->index) < elf_num_section_syms (abfd)
6526 && elf_section_syms (abfd)[indx] != NULL)
6527 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6530 idx = asym_ptr->udata.i;
6534 /* This case can occur when using --strip-symbol on a symbol
6535 which is used in a relocation entry. */
6537 /* xgettext:c-format */
6538 (_("%pB: symbol `%s' required but not present"),
6539 abfd, bfd_asymbol_name (asym_ptr));
6540 bfd_set_error (bfd_error_no_symbols);
6547 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6548 (long) asym_ptr, asym_ptr->name, idx, flags);
6556 /* Rewrite program header information. */
6559 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6561 Elf_Internal_Ehdr *iehdr;
6562 struct elf_segment_map *map;
6563 struct elf_segment_map *map_first;
6564 struct elf_segment_map **pointer_to_map;
6565 Elf_Internal_Phdr *segment;
6568 unsigned int num_segments;
6569 bfd_boolean phdr_included = FALSE;
6570 bfd_boolean p_paddr_valid;
6571 bfd_vma maxpagesize;
6572 struct elf_segment_map *phdr_adjust_seg = NULL;
6573 unsigned int phdr_adjust_num = 0;
6574 const struct elf_backend_data *bed;
6576 bed = get_elf_backend_data (ibfd);
6577 iehdr = elf_elfheader (ibfd);
6580 pointer_to_map = &map_first;
6582 num_segments = elf_elfheader (ibfd)->e_phnum;
6583 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6585 /* Returns the end address of the segment + 1. */
6586 #define SEGMENT_END(segment, start) \
6587 (start + (segment->p_memsz > segment->p_filesz \
6588 ? segment->p_memsz : segment->p_filesz))
6590 #define SECTION_SIZE(section, segment) \
6591 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6592 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6593 ? section->size : 0)
6595 /* Returns TRUE if the given section is contained within
6596 the given segment. VMA addresses are compared. */
6597 #define IS_CONTAINED_BY_VMA(section, segment) \
6598 (section->vma >= segment->p_vaddr \
6599 && (section->vma + SECTION_SIZE (section, segment) \
6600 <= (SEGMENT_END (segment, segment->p_vaddr))))
6602 /* Returns TRUE if the given section is contained within
6603 the given segment. LMA addresses are compared. */
6604 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6605 (section->lma >= base \
6606 && (section->lma + SECTION_SIZE (section, segment) \
6607 <= SEGMENT_END (segment, base)))
6609 /* Handle PT_NOTE segment. */
6610 #define IS_NOTE(p, s) \
6611 (p->p_type == PT_NOTE \
6612 && elf_section_type (s) == SHT_NOTE \
6613 && (bfd_vma) s->filepos >= p->p_offset \
6614 && ((bfd_vma) s->filepos + s->size \
6615 <= p->p_offset + p->p_filesz))
6617 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6619 #define IS_COREFILE_NOTE(p, s) \
6621 && bfd_get_format (ibfd) == bfd_core \
6625 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6626 linker, which generates a PT_INTERP section with p_vaddr and
6627 p_memsz set to 0. */
6628 #define IS_SOLARIS_PT_INTERP(p, s) \
6630 && p->p_paddr == 0 \
6631 && p->p_memsz == 0 \
6632 && p->p_filesz > 0 \
6633 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6635 && (bfd_vma) s->filepos >= p->p_offset \
6636 && ((bfd_vma) s->filepos + s->size \
6637 <= p->p_offset + p->p_filesz))
6639 /* Decide if the given section should be included in the given segment.
6640 A section will be included if:
6641 1. It is within the address space of the segment -- we use the LMA
6642 if that is set for the segment and the VMA otherwise,
6643 2. It is an allocated section or a NOTE section in a PT_NOTE
6645 3. There is an output section associated with it,
6646 4. The section has not already been allocated to a previous segment.
6647 5. PT_GNU_STACK segments do not include any sections.
6648 6. PT_TLS segment includes only SHF_TLS sections.
6649 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6650 8. PT_DYNAMIC should not contain empty sections at the beginning
6651 (with the possible exception of .dynamic). */
6652 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6653 ((((segment->p_paddr \
6654 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6655 : IS_CONTAINED_BY_VMA (section, segment)) \
6656 && (section->flags & SEC_ALLOC) != 0) \
6657 || IS_NOTE (segment, section)) \
6658 && segment->p_type != PT_GNU_STACK \
6659 && (segment->p_type != PT_TLS \
6660 || (section->flags & SEC_THREAD_LOCAL)) \
6661 && (segment->p_type == PT_LOAD \
6662 || segment->p_type == PT_TLS \
6663 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6664 && (segment->p_type != PT_DYNAMIC \
6665 || SECTION_SIZE (section, segment) > 0 \
6666 || (segment->p_paddr \
6667 ? segment->p_paddr != section->lma \
6668 : segment->p_vaddr != section->vma) \
6669 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6671 && (segment->p_type != PT_LOAD || !section->segment_mark))
6673 /* If the output section of a section in the input segment is NULL,
6674 it is removed from the corresponding output segment. */
6675 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6676 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6677 && section->output_section != NULL)
6679 /* Returns TRUE iff seg1 starts after the end of seg2. */
6680 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6681 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6683 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6684 their VMA address ranges and their LMA address ranges overlap.
6685 It is possible to have overlapping VMA ranges without overlapping LMA
6686 ranges. RedBoot images for example can have both .data and .bss mapped
6687 to the same VMA range, but with the .data section mapped to a different
6689 #define SEGMENT_OVERLAPS(seg1, seg2) \
6690 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6691 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6692 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6693 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6695 /* Initialise the segment mark field. */
6696 for (section = ibfd->sections; section != NULL; section = section->next)
6697 section->segment_mark = FALSE;
6699 /* The Solaris linker creates program headers in which all the
6700 p_paddr fields are zero. When we try to objcopy or strip such a
6701 file, we get confused. Check for this case, and if we find it
6702 don't set the p_paddr_valid fields. */
6703 p_paddr_valid = FALSE;
6704 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6707 if (segment->p_paddr != 0)
6709 p_paddr_valid = TRUE;
6713 /* Scan through the segments specified in the program header
6714 of the input BFD. For this first scan we look for overlaps
6715 in the loadable segments. These can be created by weird
6716 parameters to objcopy. Also, fix some solaris weirdness. */
6717 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6722 Elf_Internal_Phdr *segment2;
6724 if (segment->p_type == PT_INTERP)
6725 for (section = ibfd->sections; section; section = section->next)
6726 if (IS_SOLARIS_PT_INTERP (segment, section))
6728 /* Mininal change so that the normal section to segment
6729 assignment code will work. */
6730 segment->p_vaddr = section->vma;
6734 if (segment->p_type != PT_LOAD)
6736 /* Remove PT_GNU_RELRO segment. */
6737 if (segment->p_type == PT_GNU_RELRO)
6738 segment->p_type = PT_NULL;
6742 /* Determine if this segment overlaps any previous segments. */
6743 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6745 bfd_signed_vma extra_length;
6747 if (segment2->p_type != PT_LOAD
6748 || !SEGMENT_OVERLAPS (segment, segment2))
6751 /* Merge the two segments together. */
6752 if (segment2->p_vaddr < segment->p_vaddr)
6754 /* Extend SEGMENT2 to include SEGMENT and then delete
6756 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6757 - SEGMENT_END (segment2, segment2->p_vaddr));
6759 if (extra_length > 0)
6761 segment2->p_memsz += extra_length;
6762 segment2->p_filesz += extra_length;
6765 segment->p_type = PT_NULL;
6767 /* Since we have deleted P we must restart the outer loop. */
6769 segment = elf_tdata (ibfd)->phdr;
6774 /* Extend SEGMENT to include SEGMENT2 and then delete
6776 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6777 - SEGMENT_END (segment, segment->p_vaddr));
6779 if (extra_length > 0)
6781 segment->p_memsz += extra_length;
6782 segment->p_filesz += extra_length;
6785 segment2->p_type = PT_NULL;
6790 /* The second scan attempts to assign sections to segments. */
6791 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6795 unsigned int section_count;
6796 asection **sections;
6797 asection *output_section;
6799 asection *matching_lma;
6800 asection *suggested_lma;
6803 asection *first_section;
6805 if (segment->p_type == PT_NULL)
6808 first_section = NULL;
6809 /* Compute how many sections might be placed into this segment. */
6810 for (section = ibfd->sections, section_count = 0;
6812 section = section->next)
6814 /* Find the first section in the input segment, which may be
6815 removed from the corresponding output segment. */
6816 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6818 if (first_section == NULL)
6819 first_section = section;
6820 if (section->output_section != NULL)
6825 /* Allocate a segment map big enough to contain
6826 all of the sections we have selected. */
6827 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6828 amt += (bfd_size_type) section_count * sizeof (asection *);
6829 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6833 /* Initialise the fields of the segment map. Default to
6834 using the physical address of the segment in the input BFD. */
6836 map->p_type = segment->p_type;
6837 map->p_flags = segment->p_flags;
6838 map->p_flags_valid = 1;
6840 /* If the first section in the input segment is removed, there is
6841 no need to preserve segment physical address in the corresponding
6843 if (!first_section || first_section->output_section != NULL)
6845 map->p_paddr = segment->p_paddr;
6846 map->p_paddr_valid = p_paddr_valid;
6849 /* Determine if this segment contains the ELF file header
6850 and if it contains the program headers themselves. */
6851 map->includes_filehdr = (segment->p_offset == 0
6852 && segment->p_filesz >= iehdr->e_ehsize);
6853 map->includes_phdrs = 0;
6855 if (!phdr_included || segment->p_type != PT_LOAD)
6857 map->includes_phdrs =
6858 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6859 && (segment->p_offset + segment->p_filesz
6860 >= ((bfd_vma) iehdr->e_phoff
6861 + iehdr->e_phnum * iehdr->e_phentsize)));
6863 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6864 phdr_included = TRUE;
6867 if (section_count == 0)
6869 /* Special segments, such as the PT_PHDR segment, may contain
6870 no sections, but ordinary, loadable segments should contain
6871 something. They are allowed by the ELF spec however, so only
6872 a warning is produced.
6873 There is however the valid use case of embedded systems which
6874 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6875 flash memory with zeros. No warning is shown for that case. */
6876 if (segment->p_type == PT_LOAD
6877 && (segment->p_filesz > 0 || segment->p_memsz == 0))
6878 /* xgettext:c-format */
6880 (_("%pB: warning: empty loadable segment detected"
6881 " at vaddr=%#" PRIx64 ", is this intentional?"),
6882 ibfd, (uint64_t) segment->p_vaddr);
6884 map->p_vaddr_offset = segment->p_vaddr;
6886 *pointer_to_map = map;
6887 pointer_to_map = &map->next;
6892 /* Now scan the sections in the input BFD again and attempt
6893 to add their corresponding output sections to the segment map.
6894 The problem here is how to handle an output section which has
6895 been moved (ie had its LMA changed). There are four possibilities:
6897 1. None of the sections have been moved.
6898 In this case we can continue to use the segment LMA from the
6901 2. All of the sections have been moved by the same amount.
6902 In this case we can change the segment's LMA to match the LMA
6903 of the first section.
6905 3. Some of the sections have been moved, others have not.
6906 In this case those sections which have not been moved can be
6907 placed in the current segment which will have to have its size,
6908 and possibly its LMA changed, and a new segment or segments will
6909 have to be created to contain the other sections.
6911 4. The sections have been moved, but not by the same amount.
6912 In this case we can change the segment's LMA to match the LMA
6913 of the first section and we will have to create a new segment
6914 or segments to contain the other sections.
6916 In order to save time, we allocate an array to hold the section
6917 pointers that we are interested in. As these sections get assigned
6918 to a segment, they are removed from this array. */
6920 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
6921 if (sections == NULL)
6924 /* Step One: Scan for segment vs section LMA conflicts.
6925 Also add the sections to the section array allocated above.
6926 Also add the sections to the current segment. In the common
6927 case, where the sections have not been moved, this means that
6928 we have completely filled the segment, and there is nothing
6931 matching_lma = NULL;
6932 suggested_lma = NULL;
6934 for (section = first_section, j = 0;
6936 section = section->next)
6938 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
6940 output_section = section->output_section;
6942 sections[j++] = section;
6944 /* The Solaris native linker always sets p_paddr to 0.
6945 We try to catch that case here, and set it to the
6946 correct value. Note - some backends require that
6947 p_paddr be left as zero. */
6949 && segment->p_vaddr != 0
6950 && !bed->want_p_paddr_set_to_zero
6952 && output_section->lma != 0
6953 && (align_power (segment->p_vaddr
6954 + (map->includes_filehdr
6955 ? iehdr->e_ehsize : 0)
6956 + (map->includes_phdrs
6957 ? iehdr->e_phnum * iehdr->e_phentsize
6959 output_section->alignment_power)
6960 == output_section->vma))
6961 map->p_paddr = segment->p_vaddr;
6963 /* Match up the physical address of the segment with the
6964 LMA address of the output section. */
6965 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6966 || IS_COREFILE_NOTE (segment, section)
6967 || (bed->want_p_paddr_set_to_zero
6968 && IS_CONTAINED_BY_VMA (output_section, segment)))
6970 if (matching_lma == NULL
6971 || output_section->lma < matching_lma->lma)
6972 matching_lma = output_section;
6974 /* We assume that if the section fits within the segment
6975 then it does not overlap any other section within that
6977 map->sections[isec++] = output_section;
6979 else if (suggested_lma == NULL)
6980 suggested_lma = output_section;
6982 if (j == section_count)
6987 BFD_ASSERT (j == section_count);
6989 /* Step Two: Adjust the physical address of the current segment,
6991 if (isec == section_count)
6993 /* All of the sections fitted within the segment as currently
6994 specified. This is the default case. Add the segment to
6995 the list of built segments and carry on to process the next
6996 program header in the input BFD. */
6997 map->count = section_count;
6998 *pointer_to_map = map;
6999 pointer_to_map = &map->next;
7002 && !bed->want_p_paddr_set_to_zero
7003 && matching_lma->lma != map->p_paddr
7004 && !map->includes_filehdr
7005 && !map->includes_phdrs)
7006 /* There is some padding before the first section in the
7007 segment. So, we must account for that in the output
7009 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
7016 /* Change the current segment's physical address to match
7017 the LMA of the first section that fitted, or if no
7018 section fitted, the first section. */
7019 if (matching_lma == NULL)
7020 matching_lma = suggested_lma;
7022 map->p_paddr = matching_lma->lma;
7024 /* Offset the segment physical address from the lma
7025 to allow for space taken up by elf headers. */
7026 if (map->includes_phdrs)
7028 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7030 /* iehdr->e_phnum is just an estimate of the number
7031 of program headers that we will need. Make a note
7032 here of the number we used and the segment we chose
7033 to hold these headers, so that we can adjust the
7034 offset when we know the correct value. */
7035 phdr_adjust_num = iehdr->e_phnum;
7036 phdr_adjust_seg = map;
7039 if (map->includes_filehdr)
7041 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7042 map->p_paddr -= iehdr->e_ehsize;
7043 /* We've subtracted off the size of headers from the
7044 first section lma, but there may have been some
7045 alignment padding before that section too. Try to
7046 account for that by adjusting the segment lma down to
7047 the same alignment. */
7048 if (segment->p_align != 0 && segment->p_align < align)
7049 align = segment->p_align;
7050 map->p_paddr &= -align;
7054 /* Step Three: Loop over the sections again, this time assigning
7055 those that fit to the current segment and removing them from the
7056 sections array; but making sure not to leave large gaps. Once all
7057 possible sections have been assigned to the current segment it is
7058 added to the list of built segments and if sections still remain
7059 to be assigned, a new segment is constructed before repeating
7065 suggested_lma = NULL;
7067 /* Fill the current segment with sections that fit. */
7068 for (j = 0; j < section_count; j++)
7070 section = sections[j];
7072 if (section == NULL)
7075 output_section = section->output_section;
7077 BFD_ASSERT (output_section != NULL);
7079 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7080 || IS_COREFILE_NOTE (segment, section))
7082 if (map->count == 0)
7084 /* If the first section in a segment does not start at
7085 the beginning of the segment, then something is
7087 if (align_power (map->p_paddr
7088 + (map->includes_filehdr
7089 ? iehdr->e_ehsize : 0)
7090 + (map->includes_phdrs
7091 ? iehdr->e_phnum * iehdr->e_phentsize
7093 output_section->alignment_power)
7094 != output_section->lma)
7101 prev_sec = map->sections[map->count - 1];
7103 /* If the gap between the end of the previous section
7104 and the start of this section is more than
7105 maxpagesize then we need to start a new segment. */
7106 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7108 < BFD_ALIGN (output_section->lma, maxpagesize))
7109 || (prev_sec->lma + prev_sec->size
7110 > output_section->lma))
7112 if (suggested_lma == NULL)
7113 suggested_lma = output_section;
7119 map->sections[map->count++] = output_section;
7122 if (segment->p_type == PT_LOAD)
7123 section->segment_mark = TRUE;
7125 else if (suggested_lma == NULL)
7126 suggested_lma = output_section;
7129 BFD_ASSERT (map->count > 0);
7131 /* Add the current segment to the list of built segments. */
7132 *pointer_to_map = map;
7133 pointer_to_map = &map->next;
7135 if (isec < section_count)
7137 /* We still have not allocated all of the sections to
7138 segments. Create a new segment here, initialise it
7139 and carry on looping. */
7140 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7141 amt += (bfd_size_type) section_count * sizeof (asection *);
7142 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7149 /* Initialise the fields of the segment map. Set the physical
7150 physical address to the LMA of the first section that has
7151 not yet been assigned. */
7153 map->p_type = segment->p_type;
7154 map->p_flags = segment->p_flags;
7155 map->p_flags_valid = 1;
7156 map->p_paddr = suggested_lma->lma;
7157 map->p_paddr_valid = p_paddr_valid;
7158 map->includes_filehdr = 0;
7159 map->includes_phdrs = 0;
7162 while (isec < section_count);
7167 elf_seg_map (obfd) = map_first;
7169 /* If we had to estimate the number of program headers that were
7170 going to be needed, then check our estimate now and adjust
7171 the offset if necessary. */
7172 if (phdr_adjust_seg != NULL)
7176 for (count = 0, map = map_first; map != NULL; map = map->next)
7179 if (count > phdr_adjust_num)
7180 phdr_adjust_seg->p_paddr
7181 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7183 for (map = map_first; map != NULL; map = map->next)
7184 if (map->p_type == PT_PHDR)
7187 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7188 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7195 #undef IS_CONTAINED_BY_VMA
7196 #undef IS_CONTAINED_BY_LMA
7198 #undef IS_COREFILE_NOTE
7199 #undef IS_SOLARIS_PT_INTERP
7200 #undef IS_SECTION_IN_INPUT_SEGMENT
7201 #undef INCLUDE_SECTION_IN_SEGMENT
7202 #undef SEGMENT_AFTER_SEGMENT
7203 #undef SEGMENT_OVERLAPS
7207 /* Copy ELF program header information. */
7210 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7212 Elf_Internal_Ehdr *iehdr;
7213 struct elf_segment_map *map;
7214 struct elf_segment_map *map_first;
7215 struct elf_segment_map **pointer_to_map;
7216 Elf_Internal_Phdr *segment;
7218 unsigned int num_segments;
7219 bfd_boolean phdr_included = FALSE;
7220 bfd_boolean p_paddr_valid;
7222 iehdr = elf_elfheader (ibfd);
7225 pointer_to_map = &map_first;
7227 /* If all the segment p_paddr fields are zero, don't set
7228 map->p_paddr_valid. */
7229 p_paddr_valid = FALSE;
7230 num_segments = elf_elfheader (ibfd)->e_phnum;
7231 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7234 if (segment->p_paddr != 0)
7236 p_paddr_valid = TRUE;
7240 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7245 unsigned int section_count;
7247 Elf_Internal_Shdr *this_hdr;
7248 asection *first_section = NULL;
7249 asection *lowest_section;
7250 bfd_boolean no_contents = TRUE;
7252 /* Compute how many sections are in this segment. */
7253 for (section = ibfd->sections, section_count = 0;
7255 section = section->next)
7257 this_hdr = &(elf_section_data(section)->this_hdr);
7258 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7260 if (first_section == NULL)
7261 first_section = section;
7262 if (elf_section_type (section) != SHT_NOBITS)
7263 no_contents = FALSE;
7268 /* Allocate a segment map big enough to contain
7269 all of the sections we have selected. */
7270 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7271 amt += (bfd_size_type) section_count * sizeof (asection *);
7272 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7276 /* Initialize the fields of the output segment map with the
7279 map->p_type = segment->p_type;
7280 map->p_flags = segment->p_flags;
7281 map->p_flags_valid = 1;
7282 map->p_paddr = segment->p_paddr;
7283 map->p_paddr_valid = p_paddr_valid;
7284 map->p_align = segment->p_align;
7285 map->p_align_valid = 1;
7286 map->p_vaddr_offset = 0;
7288 if (map->p_type == PT_GNU_RELRO
7289 || map->p_type == PT_GNU_STACK)
7291 /* The PT_GNU_RELRO segment may contain the first a few
7292 bytes in the .got.plt section even if the whole .got.plt
7293 section isn't in the PT_GNU_RELRO segment. We won't
7294 change the size of the PT_GNU_RELRO segment.
7295 Similarly, PT_GNU_STACK size is significant on uclinux
7297 map->p_size = segment->p_memsz;
7298 map->p_size_valid = 1;
7301 /* Determine if this segment contains the ELF file header
7302 and if it contains the program headers themselves. */
7303 map->includes_filehdr = (segment->p_offset == 0
7304 && segment->p_filesz >= iehdr->e_ehsize);
7306 map->includes_phdrs = 0;
7307 if (! phdr_included || segment->p_type != PT_LOAD)
7309 map->includes_phdrs =
7310 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7311 && (segment->p_offset + segment->p_filesz
7312 >= ((bfd_vma) iehdr->e_phoff
7313 + iehdr->e_phnum * iehdr->e_phentsize)));
7315 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7316 phdr_included = TRUE;
7319 lowest_section = NULL;
7320 if (section_count != 0)
7322 unsigned int isec = 0;
7324 for (section = first_section;
7326 section = section->next)
7328 this_hdr = &(elf_section_data(section)->this_hdr);
7329 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7331 map->sections[isec++] = section->output_section;
7332 if ((section->flags & SEC_ALLOC) != 0)
7336 if (lowest_section == NULL
7337 || section->lma < lowest_section->lma)
7338 lowest_section = section;
7340 /* Section lmas are set up from PT_LOAD header
7341 p_paddr in _bfd_elf_make_section_from_shdr.
7342 If this header has a p_paddr that disagrees
7343 with the section lma, flag the p_paddr as
7345 if ((section->flags & SEC_LOAD) != 0)
7346 seg_off = this_hdr->sh_offset - segment->p_offset;
7348 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7349 if (section->lma - segment->p_paddr != seg_off)
7350 map->p_paddr_valid = FALSE;
7352 if (isec == section_count)
7358 if (map->includes_filehdr && lowest_section != NULL)
7360 /* Try to keep the space used by the headers plus any
7361 padding fixed. If there are sections with file contents
7362 in this segment then the lowest sh_offset is the best
7363 guess. Otherwise the segment only has file contents for
7364 the headers, and p_filesz is the best guess. */
7366 map->header_size = segment->p_filesz;
7368 map->header_size = lowest_section->filepos;
7371 if (section_count == 0)
7372 map->p_vaddr_offset = segment->p_vaddr;
7373 else if (!map->includes_phdrs
7374 && !map->includes_filehdr
7375 && map->p_paddr_valid)
7376 /* Account for padding before the first section. */
7377 map->p_vaddr_offset = (segment->p_paddr
7378 - (lowest_section ? lowest_section->lma : 0));
7380 map->count = section_count;
7381 *pointer_to_map = map;
7382 pointer_to_map = &map->next;
7385 elf_seg_map (obfd) = map_first;
7389 /* Copy private BFD data. This copies or rewrites ELF program header
7393 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7395 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7396 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7399 if (elf_tdata (ibfd)->phdr == NULL)
7402 if (ibfd->xvec == obfd->xvec)
7404 /* Check to see if any sections in the input BFD
7405 covered by ELF program header have changed. */
7406 Elf_Internal_Phdr *segment;
7407 asection *section, *osec;
7408 unsigned int i, num_segments;
7409 Elf_Internal_Shdr *this_hdr;
7410 const struct elf_backend_data *bed;
7412 bed = get_elf_backend_data (ibfd);
7414 /* Regenerate the segment map if p_paddr is set to 0. */
7415 if (bed->want_p_paddr_set_to_zero)
7418 /* Initialize the segment mark field. */
7419 for (section = obfd->sections; section != NULL;
7420 section = section->next)
7421 section->segment_mark = FALSE;
7423 num_segments = elf_elfheader (ibfd)->e_phnum;
7424 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7428 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7429 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7430 which severly confuses things, so always regenerate the segment
7431 map in this case. */
7432 if (segment->p_paddr == 0
7433 && segment->p_memsz == 0
7434 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7437 for (section = ibfd->sections;
7438 section != NULL; section = section->next)
7440 /* We mark the output section so that we know it comes
7441 from the input BFD. */
7442 osec = section->output_section;
7444 osec->segment_mark = TRUE;
7446 /* Check if this section is covered by the segment. */
7447 this_hdr = &(elf_section_data(section)->this_hdr);
7448 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7450 /* FIXME: Check if its output section is changed or
7451 removed. What else do we need to check? */
7453 || section->flags != osec->flags
7454 || section->lma != osec->lma
7455 || section->vma != osec->vma
7456 || section->size != osec->size
7457 || section->rawsize != osec->rawsize
7458 || section->alignment_power != osec->alignment_power)
7464 /* Check to see if any output section do not come from the
7466 for (section = obfd->sections; section != NULL;
7467 section = section->next)
7469 if (!section->segment_mark)
7472 section->segment_mark = FALSE;
7475 return copy_elf_program_header (ibfd, obfd);
7479 if (ibfd->xvec == obfd->xvec)
7481 /* When rewriting program header, set the output maxpagesize to
7482 the maximum alignment of input PT_LOAD segments. */
7483 Elf_Internal_Phdr *segment;
7485 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7486 bfd_vma maxpagesize = 0;
7488 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7491 if (segment->p_type == PT_LOAD
7492 && maxpagesize < segment->p_align)
7494 /* PR 17512: file: f17299af. */
7495 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7496 /* xgettext:c-format */
7497 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7498 PRIx64 " is too large"),
7499 ibfd, (uint64_t) segment->p_align);
7501 maxpagesize = segment->p_align;
7504 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7505 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7508 return rewrite_elf_program_header (ibfd, obfd);
7511 /* Initialize private output section information from input section. */
7514 _bfd_elf_init_private_section_data (bfd *ibfd,
7518 struct bfd_link_info *link_info)
7521 Elf_Internal_Shdr *ihdr, *ohdr;
7522 bfd_boolean final_link = (link_info != NULL
7523 && !bfd_link_relocatable (link_info));
7525 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7526 || obfd->xvec->flavour != bfd_target_elf_flavour)
7529 BFD_ASSERT (elf_section_data (osec) != NULL);
7531 /* For objcopy and relocatable link, don't copy the output ELF
7532 section type from input if the output BFD section flags have been
7533 set to something different. For a final link allow some flags
7534 that the linker clears to differ. */
7535 if (elf_section_type (osec) == SHT_NULL
7536 && (osec->flags == isec->flags
7538 && ((osec->flags ^ isec->flags)
7539 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7540 elf_section_type (osec) = elf_section_type (isec);
7542 /* FIXME: Is this correct for all OS/PROC specific flags? */
7543 elf_section_flags (osec) |= (elf_section_flags (isec)
7544 & (SHF_MASKOS | SHF_MASKPROC));
7546 /* Copy sh_info from input for mbind section. */
7547 if (elf_section_flags (isec) & SHF_GNU_MBIND)
7548 elf_section_data (osec)->this_hdr.sh_info
7549 = elf_section_data (isec)->this_hdr.sh_info;
7551 /* Set things up for objcopy and relocatable link. The output
7552 SHT_GROUP section will have its elf_next_in_group pointing back
7553 to the input group members. Ignore linker created group section.
7554 See elfNN_ia64_object_p in elfxx-ia64.c. */
7555 if ((link_info == NULL
7556 || !link_info->resolve_section_groups)
7557 && (elf_sec_group (isec) == NULL
7558 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7560 if (elf_section_flags (isec) & SHF_GROUP)
7561 elf_section_flags (osec) |= SHF_GROUP;
7562 elf_next_in_group (osec) = elf_next_in_group (isec);
7563 elf_section_data (osec)->group = elf_section_data (isec)->group;
7566 /* If not decompress, preserve SHF_COMPRESSED. */
7567 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7568 elf_section_flags (osec) |= (elf_section_flags (isec)
7571 ihdr = &elf_section_data (isec)->this_hdr;
7573 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7574 don't use the output section of the linked-to section since it
7575 may be NULL at this point. */
7576 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7578 ohdr = &elf_section_data (osec)->this_hdr;
7579 ohdr->sh_flags |= SHF_LINK_ORDER;
7580 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7583 osec->use_rela_p = isec->use_rela_p;
7588 /* Copy private section information. This copies over the entsize
7589 field, and sometimes the info field. */
7592 _bfd_elf_copy_private_section_data (bfd *ibfd,
7597 Elf_Internal_Shdr *ihdr, *ohdr;
7599 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7600 || obfd->xvec->flavour != bfd_target_elf_flavour)
7603 ihdr = &elf_section_data (isec)->this_hdr;
7604 ohdr = &elf_section_data (osec)->this_hdr;
7606 ohdr->sh_entsize = ihdr->sh_entsize;
7608 if (ihdr->sh_type == SHT_SYMTAB
7609 || ihdr->sh_type == SHT_DYNSYM
7610 || ihdr->sh_type == SHT_GNU_verneed
7611 || ihdr->sh_type == SHT_GNU_verdef)
7612 ohdr->sh_info = ihdr->sh_info;
7614 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7618 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7619 necessary if we are removing either the SHT_GROUP section or any of
7620 the group member sections. DISCARDED is the value that a section's
7621 output_section has if the section will be discarded, NULL when this
7622 function is called from objcopy, bfd_abs_section_ptr when called
7626 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7630 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7631 if (elf_section_type (isec) == SHT_GROUP)
7633 asection *first = elf_next_in_group (isec);
7634 asection *s = first;
7635 bfd_size_type removed = 0;
7639 /* If this member section is being output but the
7640 SHT_GROUP section is not, then clear the group info
7641 set up by _bfd_elf_copy_private_section_data. */
7642 if (s->output_section != discarded
7643 && isec->output_section == discarded)
7645 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7646 elf_group_name (s->output_section) = NULL;
7648 /* Conversely, if the member section is not being output
7649 but the SHT_GROUP section is, then adjust its size. */
7650 else if (s->output_section == discarded
7651 && isec->output_section != discarded)
7653 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7655 if (elf_sec->rel.hdr != NULL
7656 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7658 if (elf_sec->rela.hdr != NULL
7659 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7662 s = elf_next_in_group (s);
7668 if (discarded != NULL)
7670 /* If we've been called for ld -r, then we need to
7671 adjust the input section size. */
7672 if (isec->rawsize == 0)
7673 isec->rawsize = isec->size;
7674 isec->size = isec->rawsize - removed;
7675 if (isec->size <= 4)
7678 isec->flags |= SEC_EXCLUDE;
7683 /* Adjust the output section size when called from
7685 isec->output_section->size -= removed;
7686 if (isec->output_section->size <= 4)
7688 isec->output_section->size = 0;
7689 isec->output_section->flags |= SEC_EXCLUDE;
7698 /* Copy private header information. */
7701 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7703 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7704 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7707 /* Copy over private BFD data if it has not already been copied.
7708 This must be done here, rather than in the copy_private_bfd_data
7709 entry point, because the latter is called after the section
7710 contents have been set, which means that the program headers have
7711 already been worked out. */
7712 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7714 if (! copy_private_bfd_data (ibfd, obfd))
7718 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7721 /* Copy private symbol information. If this symbol is in a section
7722 which we did not map into a BFD section, try to map the section
7723 index correctly. We use special macro definitions for the mapped
7724 section indices; these definitions are interpreted by the
7725 swap_out_syms function. */
7727 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7728 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7729 #define MAP_STRTAB (SHN_HIOS + 3)
7730 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7731 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7734 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7739 elf_symbol_type *isym, *osym;
7741 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7742 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7745 isym = elf_symbol_from (ibfd, isymarg);
7746 osym = elf_symbol_from (obfd, osymarg);
7749 && isym->internal_elf_sym.st_shndx != 0
7751 && bfd_is_abs_section (isym->symbol.section))
7755 shndx = isym->internal_elf_sym.st_shndx;
7756 if (shndx == elf_onesymtab (ibfd))
7757 shndx = MAP_ONESYMTAB;
7758 else if (shndx == elf_dynsymtab (ibfd))
7759 shndx = MAP_DYNSYMTAB;
7760 else if (shndx == elf_strtab_sec (ibfd))
7762 else if (shndx == elf_shstrtab_sec (ibfd))
7763 shndx = MAP_SHSTRTAB;
7764 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7765 shndx = MAP_SYM_SHNDX;
7766 osym->internal_elf_sym.st_shndx = shndx;
7772 /* Swap out the symbols. */
7775 swap_out_syms (bfd *abfd,
7776 struct elf_strtab_hash **sttp,
7779 const struct elf_backend_data *bed;
7782 struct elf_strtab_hash *stt;
7783 Elf_Internal_Shdr *symtab_hdr;
7784 Elf_Internal_Shdr *symtab_shndx_hdr;
7785 Elf_Internal_Shdr *symstrtab_hdr;
7786 struct elf_sym_strtab *symstrtab;
7787 bfd_byte *outbound_syms;
7788 bfd_byte *outbound_shndx;
7789 unsigned long outbound_syms_index;
7790 unsigned long outbound_shndx_index;
7792 unsigned int num_locals;
7794 bfd_boolean name_local_sections;
7796 if (!elf_map_symbols (abfd, &num_locals))
7799 /* Dump out the symtabs. */
7800 stt = _bfd_elf_strtab_init ();
7804 bed = get_elf_backend_data (abfd);
7805 symcount = bfd_get_symcount (abfd);
7806 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7807 symtab_hdr->sh_type = SHT_SYMTAB;
7808 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7809 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7810 symtab_hdr->sh_info = num_locals + 1;
7811 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7813 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7814 symstrtab_hdr->sh_type = SHT_STRTAB;
7816 /* Allocate buffer to swap out the .strtab section. */
7817 symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
7818 * sizeof (*symstrtab));
7819 if (symstrtab == NULL)
7821 _bfd_elf_strtab_free (stt);
7825 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7826 bed->s->sizeof_sym);
7827 if (outbound_syms == NULL)
7830 _bfd_elf_strtab_free (stt);
7834 symtab_hdr->contents = outbound_syms;
7835 outbound_syms_index = 0;
7837 outbound_shndx = NULL;
7838 outbound_shndx_index = 0;
7840 if (elf_symtab_shndx_list (abfd))
7842 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7843 if (symtab_shndx_hdr->sh_name != 0)
7845 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7846 outbound_shndx = (bfd_byte *)
7847 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7848 if (outbound_shndx == NULL)
7851 symtab_shndx_hdr->contents = outbound_shndx;
7852 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7853 symtab_shndx_hdr->sh_size = amt;
7854 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7855 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7857 /* FIXME: What about any other headers in the list ? */
7860 /* Now generate the data (for "contents"). */
7862 /* Fill in zeroth symbol and swap it out. */
7863 Elf_Internal_Sym sym;
7869 sym.st_shndx = SHN_UNDEF;
7870 sym.st_target_internal = 0;
7871 symstrtab[0].sym = sym;
7872 symstrtab[0].dest_index = outbound_syms_index;
7873 symstrtab[0].destshndx_index = outbound_shndx_index;
7874 outbound_syms_index++;
7875 if (outbound_shndx != NULL)
7876 outbound_shndx_index++;
7880 = (bed->elf_backend_name_local_section_symbols
7881 && bed->elf_backend_name_local_section_symbols (abfd));
7883 syms = bfd_get_outsymbols (abfd);
7884 for (idx = 0; idx < symcount;)
7886 Elf_Internal_Sym sym;
7887 bfd_vma value = syms[idx]->value;
7888 elf_symbol_type *type_ptr;
7889 flagword flags = syms[idx]->flags;
7892 if (!name_local_sections
7893 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
7895 /* Local section symbols have no name. */
7896 sym.st_name = (unsigned long) -1;
7900 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7901 to get the final offset for st_name. */
7903 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
7905 if (sym.st_name == (unsigned long) -1)
7909 type_ptr = elf_symbol_from (abfd, syms[idx]);
7911 if ((flags & BSF_SECTION_SYM) == 0
7912 && bfd_is_com_section (syms[idx]->section))
7914 /* ELF common symbols put the alignment into the `value' field,
7915 and the size into the `size' field. This is backwards from
7916 how BFD handles it, so reverse it here. */
7917 sym.st_size = value;
7918 if (type_ptr == NULL
7919 || type_ptr->internal_elf_sym.st_value == 0)
7920 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
7922 sym.st_value = type_ptr->internal_elf_sym.st_value;
7923 sym.st_shndx = _bfd_elf_section_from_bfd_section
7924 (abfd, syms[idx]->section);
7928 asection *sec = syms[idx]->section;
7931 if (sec->output_section)
7933 value += sec->output_offset;
7934 sec = sec->output_section;
7937 /* Don't add in the section vma for relocatable output. */
7938 if (! relocatable_p)
7940 sym.st_value = value;
7941 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
7943 if (bfd_is_abs_section (sec)
7945 && type_ptr->internal_elf_sym.st_shndx != 0)
7947 /* This symbol is in a real ELF section which we did
7948 not create as a BFD section. Undo the mapping done
7949 by copy_private_symbol_data. */
7950 shndx = type_ptr->internal_elf_sym.st_shndx;
7954 shndx = elf_onesymtab (abfd);
7957 shndx = elf_dynsymtab (abfd);
7960 shndx = elf_strtab_sec (abfd);
7963 shndx = elf_shstrtab_sec (abfd);
7966 if (elf_symtab_shndx_list (abfd))
7967 shndx = elf_symtab_shndx_list (abfd)->ndx;
7976 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
7978 if (shndx == SHN_BAD)
7982 /* Writing this would be a hell of a lot easier if
7983 we had some decent documentation on bfd, and
7984 knew what to expect of the library, and what to
7985 demand of applications. For example, it
7986 appears that `objcopy' might not set the
7987 section of a symbol to be a section that is
7988 actually in the output file. */
7989 sec2 = bfd_get_section_by_name (abfd, sec->name);
7991 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7992 if (shndx == SHN_BAD)
7994 /* xgettext:c-format */
7996 (_("unable to find equivalent output section"
7997 " for symbol '%s' from section '%s'"),
7998 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8000 bfd_set_error (bfd_error_invalid_operation);
8006 sym.st_shndx = shndx;
8009 if ((flags & BSF_THREAD_LOCAL) != 0)
8011 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8012 type = STT_GNU_IFUNC;
8013 else if ((flags & BSF_FUNCTION) != 0)
8015 else if ((flags & BSF_OBJECT) != 0)
8017 else if ((flags & BSF_RELC) != 0)
8019 else if ((flags & BSF_SRELC) != 0)
8024 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8027 /* Processor-specific types. */
8028 if (type_ptr != NULL
8029 && bed->elf_backend_get_symbol_type)
8030 type = ((*bed->elf_backend_get_symbol_type)
8031 (&type_ptr->internal_elf_sym, type));
8033 if (flags & BSF_SECTION_SYM)
8035 if (flags & BSF_GLOBAL)
8036 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8038 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8040 else if (bfd_is_com_section (syms[idx]->section))
8042 if (type != STT_TLS)
8044 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8045 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8046 ? STT_COMMON : STT_OBJECT);
8048 type = ((flags & BSF_ELF_COMMON) != 0
8049 ? STT_COMMON : STT_OBJECT);
8051 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8053 else if (bfd_is_und_section (syms[idx]->section))
8054 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8058 else if (flags & BSF_FILE)
8059 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8062 int bind = STB_LOCAL;
8064 if (flags & BSF_LOCAL)
8066 else if (flags & BSF_GNU_UNIQUE)
8067 bind = STB_GNU_UNIQUE;
8068 else if (flags & BSF_WEAK)
8070 else if (flags & BSF_GLOBAL)
8073 sym.st_info = ELF_ST_INFO (bind, type);
8076 if (type_ptr != NULL)
8078 sym.st_other = type_ptr->internal_elf_sym.st_other;
8079 sym.st_target_internal
8080 = type_ptr->internal_elf_sym.st_target_internal;
8085 sym.st_target_internal = 0;
8089 symstrtab[idx].sym = sym;
8090 symstrtab[idx].dest_index = outbound_syms_index;
8091 symstrtab[idx].destshndx_index = outbound_shndx_index;
8093 outbound_syms_index++;
8094 if (outbound_shndx != NULL)
8095 outbound_shndx_index++;
8098 /* Finalize the .strtab section. */
8099 _bfd_elf_strtab_finalize (stt);
8101 /* Swap out the .strtab section. */
8102 for (idx = 0; idx <= symcount; idx++)
8104 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8105 if (elfsym->sym.st_name == (unsigned long) -1)
8106 elfsym->sym.st_name = 0;
8108 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8109 elfsym->sym.st_name);
8110 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8112 + (elfsym->dest_index
8113 * bed->s->sizeof_sym)),
8115 + (elfsym->destshndx_index
8116 * sizeof (Elf_External_Sym_Shndx))));
8121 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8122 symstrtab_hdr->sh_type = SHT_STRTAB;
8123 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8124 symstrtab_hdr->sh_addr = 0;
8125 symstrtab_hdr->sh_entsize = 0;
8126 symstrtab_hdr->sh_link = 0;
8127 symstrtab_hdr->sh_info = 0;
8128 symstrtab_hdr->sh_addralign = 1;
8133 /* Return the number of bytes required to hold the symtab vector.
8135 Note that we base it on the count plus 1, since we will null terminate
8136 the vector allocated based on this size. However, the ELF symbol table
8137 always has a dummy entry as symbol #0, so it ends up even. */
8140 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8144 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8146 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8147 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8149 symtab_size -= sizeof (asymbol *);
8155 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8159 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8161 if (elf_dynsymtab (abfd) == 0)
8163 bfd_set_error (bfd_error_invalid_operation);
8167 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8168 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8170 symtab_size -= sizeof (asymbol *);
8176 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8179 return (asect->reloc_count + 1) * sizeof (arelent *);
8182 /* Canonicalize the relocs. */
8185 _bfd_elf_canonicalize_reloc (bfd *abfd,
8192 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8194 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8197 tblptr = section->relocation;
8198 for (i = 0; i < section->reloc_count; i++)
8199 *relptr++ = tblptr++;
8203 return section->reloc_count;
8207 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8209 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8210 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8213 bfd_get_symcount (abfd) = symcount;
8218 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8219 asymbol **allocation)
8221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8222 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8225 bfd_get_dynamic_symcount (abfd) = symcount;
8229 /* Return the size required for the dynamic reloc entries. Any loadable
8230 section that was actually installed in the BFD, and has type SHT_REL
8231 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8232 dynamic reloc section. */
8235 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8240 if (elf_dynsymtab (abfd) == 0)
8242 bfd_set_error (bfd_error_invalid_operation);
8246 ret = sizeof (arelent *);
8247 for (s = abfd->sections; s != NULL; s = s->next)
8248 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8249 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8250 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8251 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
8252 * sizeof (arelent *));
8257 /* Canonicalize the dynamic relocation entries. Note that we return the
8258 dynamic relocations as a single block, although they are actually
8259 associated with particular sections; the interface, which was
8260 designed for SunOS style shared libraries, expects that there is only
8261 one set of dynamic relocs. Any loadable section that was actually
8262 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8263 dynamic symbol table, is considered to be a dynamic reloc section. */
8266 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8270 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8274 if (elf_dynsymtab (abfd) == 0)
8276 bfd_set_error (bfd_error_invalid_operation);
8280 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8282 for (s = abfd->sections; s != NULL; s = s->next)
8284 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8285 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8286 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8291 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8293 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8295 for (i = 0; i < count; i++)
8306 /* Read in the version information. */
8309 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8311 bfd_byte *contents = NULL;
8312 unsigned int freeidx = 0;
8314 if (elf_dynverref (abfd) != 0)
8316 Elf_Internal_Shdr *hdr;
8317 Elf_External_Verneed *everneed;
8318 Elf_Internal_Verneed *iverneed;
8320 bfd_byte *contents_end;
8322 hdr = &elf_tdata (abfd)->dynverref_hdr;
8324 if (hdr->sh_info == 0
8325 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8327 error_return_bad_verref:
8329 (_("%pB: .gnu.version_r invalid entry"), abfd);
8330 bfd_set_error (bfd_error_bad_value);
8331 error_return_verref:
8332 elf_tdata (abfd)->verref = NULL;
8333 elf_tdata (abfd)->cverrefs = 0;
8337 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8338 if (contents == NULL)
8339 goto error_return_verref;
8341 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8342 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8343 goto error_return_verref;
8345 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8346 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8348 if (elf_tdata (abfd)->verref == NULL)
8349 goto error_return_verref;
8351 BFD_ASSERT (sizeof (Elf_External_Verneed)
8352 == sizeof (Elf_External_Vernaux));
8353 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8354 everneed = (Elf_External_Verneed *) contents;
8355 iverneed = elf_tdata (abfd)->verref;
8356 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8358 Elf_External_Vernaux *evernaux;
8359 Elf_Internal_Vernaux *ivernaux;
8362 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8364 iverneed->vn_bfd = abfd;
8366 iverneed->vn_filename =
8367 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8369 if (iverneed->vn_filename == NULL)
8370 goto error_return_bad_verref;
8372 if (iverneed->vn_cnt == 0)
8373 iverneed->vn_auxptr = NULL;
8376 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8377 bfd_alloc2 (abfd, iverneed->vn_cnt,
8378 sizeof (Elf_Internal_Vernaux));
8379 if (iverneed->vn_auxptr == NULL)
8380 goto error_return_verref;
8383 if (iverneed->vn_aux
8384 > (size_t) (contents_end - (bfd_byte *) everneed))
8385 goto error_return_bad_verref;
8387 evernaux = ((Elf_External_Vernaux *)
8388 ((bfd_byte *) everneed + iverneed->vn_aux));
8389 ivernaux = iverneed->vn_auxptr;
8390 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8392 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8394 ivernaux->vna_nodename =
8395 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8396 ivernaux->vna_name);
8397 if (ivernaux->vna_nodename == NULL)
8398 goto error_return_bad_verref;
8400 if (ivernaux->vna_other > freeidx)
8401 freeidx = ivernaux->vna_other;
8403 ivernaux->vna_nextptr = NULL;
8404 if (ivernaux->vna_next == 0)
8406 iverneed->vn_cnt = j + 1;
8409 if (j + 1 < iverneed->vn_cnt)
8410 ivernaux->vna_nextptr = ivernaux + 1;
8412 if (ivernaux->vna_next
8413 > (size_t) (contents_end - (bfd_byte *) evernaux))
8414 goto error_return_bad_verref;
8416 evernaux = ((Elf_External_Vernaux *)
8417 ((bfd_byte *) evernaux + ivernaux->vna_next));
8420 iverneed->vn_nextref = NULL;
8421 if (iverneed->vn_next == 0)
8423 if (i + 1 < hdr->sh_info)
8424 iverneed->vn_nextref = iverneed + 1;
8426 if (iverneed->vn_next
8427 > (size_t) (contents_end - (bfd_byte *) everneed))
8428 goto error_return_bad_verref;
8430 everneed = ((Elf_External_Verneed *)
8431 ((bfd_byte *) everneed + iverneed->vn_next));
8433 elf_tdata (abfd)->cverrefs = i;
8439 if (elf_dynverdef (abfd) != 0)
8441 Elf_Internal_Shdr *hdr;
8442 Elf_External_Verdef *everdef;
8443 Elf_Internal_Verdef *iverdef;
8444 Elf_Internal_Verdef *iverdefarr;
8445 Elf_Internal_Verdef iverdefmem;
8447 unsigned int maxidx;
8448 bfd_byte *contents_end_def, *contents_end_aux;
8450 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8452 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8454 error_return_bad_verdef:
8456 (_("%pB: .gnu.version_d invalid entry"), abfd);
8457 bfd_set_error (bfd_error_bad_value);
8458 error_return_verdef:
8459 elf_tdata (abfd)->verdef = NULL;
8460 elf_tdata (abfd)->cverdefs = 0;
8464 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8465 if (contents == NULL)
8466 goto error_return_verdef;
8467 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8468 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8469 goto error_return_verdef;
8471 BFD_ASSERT (sizeof (Elf_External_Verdef)
8472 >= sizeof (Elf_External_Verdaux));
8473 contents_end_def = contents + hdr->sh_size
8474 - sizeof (Elf_External_Verdef);
8475 contents_end_aux = contents + hdr->sh_size
8476 - sizeof (Elf_External_Verdaux);
8478 /* We know the number of entries in the section but not the maximum
8479 index. Therefore we have to run through all entries and find
8481 everdef = (Elf_External_Verdef *) contents;
8483 for (i = 0; i < hdr->sh_info; ++i)
8485 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8487 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8488 goto error_return_bad_verdef;
8489 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8490 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8492 if (iverdefmem.vd_next == 0)
8495 if (iverdefmem.vd_next
8496 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8497 goto error_return_bad_verdef;
8499 everdef = ((Elf_External_Verdef *)
8500 ((bfd_byte *) everdef + iverdefmem.vd_next));
8503 if (default_imported_symver)
8505 if (freeidx > maxidx)
8511 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8512 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8513 if (elf_tdata (abfd)->verdef == NULL)
8514 goto error_return_verdef;
8516 elf_tdata (abfd)->cverdefs = maxidx;
8518 everdef = (Elf_External_Verdef *) contents;
8519 iverdefarr = elf_tdata (abfd)->verdef;
8520 for (i = 0; i < hdr->sh_info; i++)
8522 Elf_External_Verdaux *everdaux;
8523 Elf_Internal_Verdaux *iverdaux;
8526 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8528 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8529 goto error_return_bad_verdef;
8531 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8532 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8534 iverdef->vd_bfd = abfd;
8536 if (iverdef->vd_cnt == 0)
8537 iverdef->vd_auxptr = NULL;
8540 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8541 bfd_alloc2 (abfd, iverdef->vd_cnt,
8542 sizeof (Elf_Internal_Verdaux));
8543 if (iverdef->vd_auxptr == NULL)
8544 goto error_return_verdef;
8548 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8549 goto error_return_bad_verdef;
8551 everdaux = ((Elf_External_Verdaux *)
8552 ((bfd_byte *) everdef + iverdef->vd_aux));
8553 iverdaux = iverdef->vd_auxptr;
8554 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8556 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8558 iverdaux->vda_nodename =
8559 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8560 iverdaux->vda_name);
8561 if (iverdaux->vda_nodename == NULL)
8562 goto error_return_bad_verdef;
8564 iverdaux->vda_nextptr = NULL;
8565 if (iverdaux->vda_next == 0)
8567 iverdef->vd_cnt = j + 1;
8570 if (j + 1 < iverdef->vd_cnt)
8571 iverdaux->vda_nextptr = iverdaux + 1;
8573 if (iverdaux->vda_next
8574 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8575 goto error_return_bad_verdef;
8577 everdaux = ((Elf_External_Verdaux *)
8578 ((bfd_byte *) everdaux + iverdaux->vda_next));
8581 iverdef->vd_nodename = NULL;
8582 if (iverdef->vd_cnt)
8583 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8585 iverdef->vd_nextdef = NULL;
8586 if (iverdef->vd_next == 0)
8588 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8589 iverdef->vd_nextdef = iverdef + 1;
8591 everdef = ((Elf_External_Verdef *)
8592 ((bfd_byte *) everdef + iverdef->vd_next));
8598 else if (default_imported_symver)
8605 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8606 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8607 if (elf_tdata (abfd)->verdef == NULL)
8610 elf_tdata (abfd)->cverdefs = freeidx;
8613 /* Create a default version based on the soname. */
8614 if (default_imported_symver)
8616 Elf_Internal_Verdef *iverdef;
8617 Elf_Internal_Verdaux *iverdaux;
8619 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8621 iverdef->vd_version = VER_DEF_CURRENT;
8622 iverdef->vd_flags = 0;
8623 iverdef->vd_ndx = freeidx;
8624 iverdef->vd_cnt = 1;
8626 iverdef->vd_bfd = abfd;
8628 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8629 if (iverdef->vd_nodename == NULL)
8630 goto error_return_verdef;
8631 iverdef->vd_nextdef = NULL;
8632 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8633 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8634 if (iverdef->vd_auxptr == NULL)
8635 goto error_return_verdef;
8637 iverdaux = iverdef->vd_auxptr;
8638 iverdaux->vda_nodename = iverdef->vd_nodename;
8644 if (contents != NULL)
8650 _bfd_elf_make_empty_symbol (bfd *abfd)
8652 elf_symbol_type *newsym;
8654 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
8657 newsym->symbol.the_bfd = abfd;
8658 return &newsym->symbol;
8662 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8666 bfd_symbol_info (symbol, ret);
8669 /* Return whether a symbol name implies a local symbol. Most targets
8670 use this function for the is_local_label_name entry point, but some
8674 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8677 /* Normal local symbols start with ``.L''. */
8678 if (name[0] == '.' && name[1] == 'L')
8681 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8682 DWARF debugging symbols starting with ``..''. */
8683 if (name[0] == '.' && name[1] == '.')
8686 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8687 emitting DWARF debugging output. I suspect this is actually a
8688 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8689 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8690 underscore to be emitted on some ELF targets). For ease of use,
8691 we treat such symbols as local. */
8692 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8695 /* Treat assembler generated fake symbols, dollar local labels and
8696 forward-backward labels (aka local labels) as locals.
8697 These labels have the form:
8699 L0^A.* (fake symbols)
8701 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8703 Versions which start with .L will have already been matched above,
8704 so we only need to match the rest. */
8705 if (name[0] == 'L' && ISDIGIT (name[1]))
8707 bfd_boolean ret = FALSE;
8711 for (p = name + 2; (c = *p); p++)
8713 if (c == 1 || c == 2)
8715 if (c == 1 && p == name + 2)
8716 /* A fake symbol. */
8719 /* FIXME: We are being paranoid here and treating symbols like
8720 L0^Bfoo as if there were non-local, on the grounds that the
8721 assembler will never generate them. But can any symbol
8722 containing an ASCII value in the range 1-31 ever be anything
8723 other than some kind of local ? */
8740 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8741 asymbol *symbol ATTRIBUTE_UNUSED)
8748 _bfd_elf_set_arch_mach (bfd *abfd,
8749 enum bfd_architecture arch,
8750 unsigned long machine)
8752 /* If this isn't the right architecture for this backend, and this
8753 isn't the generic backend, fail. */
8754 if (arch != get_elf_backend_data (abfd)->arch
8755 && arch != bfd_arch_unknown
8756 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8759 return bfd_default_set_arch_mach (abfd, arch, machine);
8762 /* Find the nearest line to a particular section and offset,
8763 for error reporting. */
8766 _bfd_elf_find_nearest_line (bfd *abfd,
8770 const char **filename_ptr,
8771 const char **functionname_ptr,
8772 unsigned int *line_ptr,
8773 unsigned int *discriminator_ptr)
8777 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8778 filename_ptr, functionname_ptr,
8779 line_ptr, discriminator_ptr,
8780 dwarf_debug_sections, 0,
8781 &elf_tdata (abfd)->dwarf2_find_line_info)
8782 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8783 filename_ptr, functionname_ptr,
8786 if (!*functionname_ptr)
8787 _bfd_elf_find_function (abfd, symbols, section, offset,
8788 *filename_ptr ? NULL : filename_ptr,
8793 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8794 &found, filename_ptr,
8795 functionname_ptr, line_ptr,
8796 &elf_tdata (abfd)->line_info))
8798 if (found && (*functionname_ptr || *line_ptr))
8801 if (symbols == NULL)
8804 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8805 filename_ptr, functionname_ptr))
8812 /* Find the line for a symbol. */
8815 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8816 const char **filename_ptr, unsigned int *line_ptr)
8818 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8819 filename_ptr, NULL, line_ptr, NULL,
8820 dwarf_debug_sections, 0,
8821 &elf_tdata (abfd)->dwarf2_find_line_info);
8824 /* After a call to bfd_find_nearest_line, successive calls to
8825 bfd_find_inliner_info can be used to get source information about
8826 each level of function inlining that terminated at the address
8827 passed to bfd_find_nearest_line. Currently this is only supported
8828 for DWARF2 with appropriate DWARF3 extensions. */
8831 _bfd_elf_find_inliner_info (bfd *abfd,
8832 const char **filename_ptr,
8833 const char **functionname_ptr,
8834 unsigned int *line_ptr)
8837 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8838 functionname_ptr, line_ptr,
8839 & elf_tdata (abfd)->dwarf2_find_line_info);
8844 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
8846 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8847 int ret = bed->s->sizeof_ehdr;
8849 if (!bfd_link_relocatable (info))
8851 bfd_size_type phdr_size = elf_program_header_size (abfd);
8853 if (phdr_size == (bfd_size_type) -1)
8855 struct elf_segment_map *m;
8858 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
8859 phdr_size += bed->s->sizeof_phdr;
8862 phdr_size = get_program_header_size (abfd, info);
8865 elf_program_header_size (abfd) = phdr_size;
8873 _bfd_elf_set_section_contents (bfd *abfd,
8875 const void *location,
8877 bfd_size_type count)
8879 Elf_Internal_Shdr *hdr;
8882 if (! abfd->output_has_begun
8883 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
8889 hdr = &elf_section_data (section)->this_hdr;
8890 if (hdr->sh_offset == (file_ptr) -1)
8892 /* We must compress this section. Write output to the buffer. */
8893 unsigned char *contents = hdr->contents;
8894 if ((offset + count) > hdr->sh_size
8895 || (section->flags & SEC_ELF_COMPRESS) == 0
8896 || contents == NULL)
8898 memcpy (contents + offset, location, count);
8901 pos = hdr->sh_offset + offset;
8902 if (bfd_seek (abfd, pos, SEEK_SET) != 0
8903 || bfd_bwrite (location, count, abfd) != count)
8910 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
8911 arelent *cache_ptr ATTRIBUTE_UNUSED,
8912 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
8918 /* Try to convert a non-ELF reloc into an ELF one. */
8921 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
8923 /* Check whether we really have an ELF howto. */
8925 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
8927 bfd_reloc_code_real_type code;
8928 reloc_howto_type *howto;
8930 /* Alien reloc: Try to determine its type to replace it with an
8931 equivalent ELF reloc. */
8933 if (areloc->howto->pc_relative)
8935 switch (areloc->howto->bitsize)
8938 code = BFD_RELOC_8_PCREL;
8941 code = BFD_RELOC_12_PCREL;
8944 code = BFD_RELOC_16_PCREL;
8947 code = BFD_RELOC_24_PCREL;
8950 code = BFD_RELOC_32_PCREL;
8953 code = BFD_RELOC_64_PCREL;
8959 howto = bfd_reloc_type_lookup (abfd, code);
8961 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
8963 if (howto->pcrel_offset)
8964 areloc->addend += areloc->address;
8966 areloc->addend -= areloc->address; /* addend is unsigned!! */
8971 switch (areloc->howto->bitsize)
8977 code = BFD_RELOC_14;
8980 code = BFD_RELOC_16;
8983 code = BFD_RELOC_26;
8986 code = BFD_RELOC_32;
8989 code = BFD_RELOC_64;
8995 howto = bfd_reloc_type_lookup (abfd, code);
8999 areloc->howto = howto;
9007 /* xgettext:c-format */
9008 _bfd_error_handler (_("%pB: %s unsupported"),
9009 abfd, areloc->howto->name);
9010 bfd_set_error (bfd_error_bad_value);
9015 _bfd_elf_close_and_cleanup (bfd *abfd)
9017 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9018 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
9020 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9021 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9022 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9025 return _bfd_generic_close_and_cleanup (abfd);
9028 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9029 in the relocation's offset. Thus we cannot allow any sort of sanity
9030 range-checking to interfere. There is nothing else to do in processing
9033 bfd_reloc_status_type
9034 _bfd_elf_rel_vtable_reloc_fn
9035 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9036 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9037 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9038 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9040 return bfd_reloc_ok;
9043 /* Elf core file support. Much of this only works on native
9044 toolchains, since we rely on knowing the
9045 machine-dependent procfs structure in order to pick
9046 out details about the corefile. */
9048 #ifdef HAVE_SYS_PROCFS_H
9049 /* Needed for new procfs interface on sparc-solaris. */
9050 # define _STRUCTURED_PROC 1
9051 # include <sys/procfs.h>
9054 /* Return a PID that identifies a "thread" for threaded cores, or the
9055 PID of the main process for non-threaded cores. */
9058 elfcore_make_pid (bfd *abfd)
9062 pid = elf_tdata (abfd)->core->lwpid;
9064 pid = elf_tdata (abfd)->core->pid;
9069 /* If there isn't a section called NAME, make one, using
9070 data from SECT. Note, this function will generate a
9071 reference to NAME, so you shouldn't deallocate or
9075 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9079 if (bfd_get_section_by_name (abfd, name) != NULL)
9082 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9086 sect2->size = sect->size;
9087 sect2->filepos = sect->filepos;
9088 sect2->alignment_power = sect->alignment_power;
9092 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9093 actually creates up to two pseudosections:
9094 - For the single-threaded case, a section named NAME, unless
9095 such a section already exists.
9096 - For the multi-threaded case, a section named "NAME/PID", where
9097 PID is elfcore_make_pid (abfd).
9098 Both pseudosections have identical contents. */
9100 _bfd_elfcore_make_pseudosection (bfd *abfd,
9106 char *threaded_name;
9110 /* Build the section name. */
9112 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9113 len = strlen (buf) + 1;
9114 threaded_name = (char *) bfd_alloc (abfd, len);
9115 if (threaded_name == NULL)
9117 memcpy (threaded_name, buf, len);
9119 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9124 sect->filepos = filepos;
9125 sect->alignment_power = 2;
9127 return elfcore_maybe_make_sect (abfd, name, sect);
9130 /* prstatus_t exists on:
9132 linux 2.[01] + glibc
9136 #if defined (HAVE_PRSTATUS_T)
9139 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9144 if (note->descsz == sizeof (prstatus_t))
9148 size = sizeof (prstat.pr_reg);
9149 offset = offsetof (prstatus_t, pr_reg);
9150 memcpy (&prstat, note->descdata, sizeof (prstat));
9152 /* Do not overwrite the core signal if it
9153 has already been set by another thread. */
9154 if (elf_tdata (abfd)->core->signal == 0)
9155 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9156 if (elf_tdata (abfd)->core->pid == 0)
9157 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9159 /* pr_who exists on:
9162 pr_who doesn't exist on:
9165 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9166 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9168 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9171 #if defined (HAVE_PRSTATUS32_T)
9172 else if (note->descsz == sizeof (prstatus32_t))
9174 /* 64-bit host, 32-bit corefile */
9175 prstatus32_t prstat;
9177 size = sizeof (prstat.pr_reg);
9178 offset = offsetof (prstatus32_t, pr_reg);
9179 memcpy (&prstat, note->descdata, sizeof (prstat));
9181 /* Do not overwrite the core signal if it
9182 has already been set by another thread. */
9183 if (elf_tdata (abfd)->core->signal == 0)
9184 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9185 if (elf_tdata (abfd)->core->pid == 0)
9186 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9188 /* pr_who exists on:
9191 pr_who doesn't exist on:
9194 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9195 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9197 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9200 #endif /* HAVE_PRSTATUS32_T */
9203 /* Fail - we don't know how to handle any other
9204 note size (ie. data object type). */
9208 /* Make a ".reg/999" section and a ".reg" section. */
9209 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9210 size, note->descpos + offset);
9212 #endif /* defined (HAVE_PRSTATUS_T) */
9214 /* Create a pseudosection containing the exact contents of NOTE. */
9216 elfcore_make_note_pseudosection (bfd *abfd,
9218 Elf_Internal_Note *note)
9220 return _bfd_elfcore_make_pseudosection (abfd, name,
9221 note->descsz, note->descpos);
9224 /* There isn't a consistent prfpregset_t across platforms,
9225 but it doesn't matter, because we don't have to pick this
9226 data structure apart. */
9229 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9231 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9234 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9235 type of NT_PRXFPREG. Just include the whole note's contents
9239 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9241 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9244 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9245 with a note type of NT_X86_XSTATE. Just include the whole note's
9246 contents literally. */
9249 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9251 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9255 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9257 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9261 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9263 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9267 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9269 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9273 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9275 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9279 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9281 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9285 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9287 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9291 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9293 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9297 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9299 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9303 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9305 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9309 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9311 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9315 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9317 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9321 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9323 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9327 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9329 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9333 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9335 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9339 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9341 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9345 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9347 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9351 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9353 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9357 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9359 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9363 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9365 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9369 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9371 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9375 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9377 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9381 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9383 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9387 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9389 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9393 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9395 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9399 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9401 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9405 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9407 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9411 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9413 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9417 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9419 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9423 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9425 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9429 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9431 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9435 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9437 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9441 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9443 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9447 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9449 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9452 #if defined (HAVE_PRPSINFO_T)
9453 typedef prpsinfo_t elfcore_psinfo_t;
9454 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9455 typedef prpsinfo32_t elfcore_psinfo32_t;
9459 #if defined (HAVE_PSINFO_T)
9460 typedef psinfo_t elfcore_psinfo_t;
9461 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9462 typedef psinfo32_t elfcore_psinfo32_t;
9466 /* return a malloc'ed copy of a string at START which is at
9467 most MAX bytes long, possibly without a terminating '\0'.
9468 the copy will always have a terminating '\0'. */
9471 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9474 char *end = (char *) memchr (start, '\0', max);
9482 dups = (char *) bfd_alloc (abfd, len + 1);
9486 memcpy (dups, start, len);
9492 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9494 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9496 if (note->descsz == sizeof (elfcore_psinfo_t))
9498 elfcore_psinfo_t psinfo;
9500 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9502 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9503 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9505 elf_tdata (abfd)->core->program
9506 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9507 sizeof (psinfo.pr_fname));
9509 elf_tdata (abfd)->core->command
9510 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9511 sizeof (psinfo.pr_psargs));
9513 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9514 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9516 /* 64-bit host, 32-bit corefile */
9517 elfcore_psinfo32_t psinfo;
9519 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9521 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9522 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9524 elf_tdata (abfd)->core->program
9525 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9526 sizeof (psinfo.pr_fname));
9528 elf_tdata (abfd)->core->command
9529 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9530 sizeof (psinfo.pr_psargs));
9536 /* Fail - we don't know how to handle any other
9537 note size (ie. data object type). */
9541 /* Note that for some reason, a spurious space is tacked
9542 onto the end of the args in some (at least one anyway)
9543 implementations, so strip it off if it exists. */
9546 char *command = elf_tdata (abfd)->core->command;
9547 int n = strlen (command);
9549 if (0 < n && command[n - 1] == ' ')
9550 command[n - 1] = '\0';
9555 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9557 #if defined (HAVE_PSTATUS_T)
9559 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9561 if (note->descsz == sizeof (pstatus_t)
9562 #if defined (HAVE_PXSTATUS_T)
9563 || note->descsz == sizeof (pxstatus_t)
9569 memcpy (&pstat, note->descdata, sizeof (pstat));
9571 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9573 #if defined (HAVE_PSTATUS32_T)
9574 else if (note->descsz == sizeof (pstatus32_t))
9576 /* 64-bit host, 32-bit corefile */
9579 memcpy (&pstat, note->descdata, sizeof (pstat));
9581 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9584 /* Could grab some more details from the "representative"
9585 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9586 NT_LWPSTATUS note, presumably. */
9590 #endif /* defined (HAVE_PSTATUS_T) */
9592 #if defined (HAVE_LWPSTATUS_T)
9594 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9596 lwpstatus_t lwpstat;
9602 if (note->descsz != sizeof (lwpstat)
9603 #if defined (HAVE_LWPXSTATUS_T)
9604 && note->descsz != sizeof (lwpxstatus_t)
9609 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9611 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9612 /* Do not overwrite the core signal if it has already been set by
9614 if (elf_tdata (abfd)->core->signal == 0)
9615 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9617 /* Make a ".reg/999" section. */
9619 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
9620 len = strlen (buf) + 1;
9621 name = bfd_alloc (abfd, len);
9624 memcpy (name, buf, len);
9626 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9630 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9631 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
9632 sect->filepos = note->descpos
9633 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9636 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9637 sect->size = sizeof (lwpstat.pr_reg);
9638 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9641 sect->alignment_power = 2;
9643 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9646 /* Make a ".reg2/999" section */
9648 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9649 len = strlen (buf) + 1;
9650 name = bfd_alloc (abfd, len);
9653 memcpy (name, buf, len);
9655 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9659 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9660 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9661 sect->filepos = note->descpos
9662 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9665 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9666 sect->size = sizeof (lwpstat.pr_fpreg);
9667 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9670 sect->alignment_power = 2;
9672 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9674 #endif /* defined (HAVE_LWPSTATUS_T) */
9677 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9684 int is_active_thread;
9687 if (note->descsz < 728)
9690 if (! CONST_STRNEQ (note->namedata, "win32"))
9693 type = bfd_get_32 (abfd, note->descdata);
9697 case 1 /* NOTE_INFO_PROCESS */:
9698 /* FIXME: need to add ->core->command. */
9699 /* process_info.pid */
9700 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9701 /* process_info.signal */
9702 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9705 case 2 /* NOTE_INFO_THREAD */:
9706 /* Make a ".reg/999" section. */
9707 /* thread_info.tid */
9708 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9710 len = strlen (buf) + 1;
9711 name = (char *) bfd_alloc (abfd, len);
9715 memcpy (name, buf, len);
9717 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9721 /* sizeof (thread_info.thread_context) */
9723 /* offsetof (thread_info.thread_context) */
9724 sect->filepos = note->descpos + 12;
9725 sect->alignment_power = 2;
9727 /* thread_info.is_active_thread */
9728 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9730 if (is_active_thread)
9731 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9735 case 3 /* NOTE_INFO_MODULE */:
9736 /* Make a ".module/xxxxxxxx" section. */
9737 /* module_info.base_address */
9738 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9739 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9741 len = strlen (buf) + 1;
9742 name = (char *) bfd_alloc (abfd, len);
9746 memcpy (name, buf, len);
9748 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9753 sect->size = note->descsz;
9754 sect->filepos = note->descpos;
9755 sect->alignment_power = 2;
9766 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9768 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9776 if (bed->elf_backend_grok_prstatus)
9777 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
9779 #if defined (HAVE_PRSTATUS_T)
9780 return elfcore_grok_prstatus (abfd, note);
9785 #if defined (HAVE_PSTATUS_T)
9787 return elfcore_grok_pstatus (abfd, note);
9790 #if defined (HAVE_LWPSTATUS_T)
9792 return elfcore_grok_lwpstatus (abfd, note);
9795 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9796 return elfcore_grok_prfpreg (abfd, note);
9798 case NT_WIN32PSTATUS:
9799 return elfcore_grok_win32pstatus (abfd, note);
9801 case NT_PRXFPREG: /* Linux SSE extension */
9802 if (note->namesz == 6
9803 && strcmp (note->namedata, "LINUX") == 0)
9804 return elfcore_grok_prxfpreg (abfd, note);
9808 case NT_X86_XSTATE: /* Linux XSAVE extension */
9809 if (note->namesz == 6
9810 && strcmp (note->namedata, "LINUX") == 0)
9811 return elfcore_grok_xstatereg (abfd, note);
9816 if (note->namesz == 6
9817 && strcmp (note->namedata, "LINUX") == 0)
9818 return elfcore_grok_ppc_vmx (abfd, note);
9823 if (note->namesz == 6
9824 && strcmp (note->namedata, "LINUX") == 0)
9825 return elfcore_grok_ppc_vsx (abfd, note);
9830 if (note->namesz == 6
9831 && strcmp (note->namedata, "LINUX") == 0)
9832 return elfcore_grok_ppc_tar (abfd, note);
9837 if (note->namesz == 6
9838 && strcmp (note->namedata, "LINUX") == 0)
9839 return elfcore_grok_ppc_ppr (abfd, note);
9844 if (note->namesz == 6
9845 && strcmp (note->namedata, "LINUX") == 0)
9846 return elfcore_grok_ppc_dscr (abfd, note);
9851 if (note->namesz == 6
9852 && strcmp (note->namedata, "LINUX") == 0)
9853 return elfcore_grok_ppc_ebb (abfd, note);
9858 if (note->namesz == 6
9859 && strcmp (note->namedata, "LINUX") == 0)
9860 return elfcore_grok_ppc_pmu (abfd, note);
9864 case NT_PPC_TM_CGPR:
9865 if (note->namesz == 6
9866 && strcmp (note->namedata, "LINUX") == 0)
9867 return elfcore_grok_ppc_tm_cgpr (abfd, note);
9871 case NT_PPC_TM_CFPR:
9872 if (note->namesz == 6
9873 && strcmp (note->namedata, "LINUX") == 0)
9874 return elfcore_grok_ppc_tm_cfpr (abfd, note);
9878 case NT_PPC_TM_CVMX:
9879 if (note->namesz == 6
9880 && strcmp (note->namedata, "LINUX") == 0)
9881 return elfcore_grok_ppc_tm_cvmx (abfd, note);
9885 case NT_PPC_TM_CVSX:
9886 if (note->namesz == 6
9887 && strcmp (note->namedata, "LINUX") == 0)
9888 return elfcore_grok_ppc_tm_cvsx (abfd, note);
9893 if (note->namesz == 6
9894 && strcmp (note->namedata, "LINUX") == 0)
9895 return elfcore_grok_ppc_tm_spr (abfd, note);
9899 case NT_PPC_TM_CTAR:
9900 if (note->namesz == 6
9901 && strcmp (note->namedata, "LINUX") == 0)
9902 return elfcore_grok_ppc_tm_ctar (abfd, note);
9906 case NT_PPC_TM_CPPR:
9907 if (note->namesz == 6
9908 && strcmp (note->namedata, "LINUX") == 0)
9909 return elfcore_grok_ppc_tm_cppr (abfd, note);
9913 case NT_PPC_TM_CDSCR:
9914 if (note->namesz == 6
9915 && strcmp (note->namedata, "LINUX") == 0)
9916 return elfcore_grok_ppc_tm_cdscr (abfd, note);
9920 case NT_S390_HIGH_GPRS:
9921 if (note->namesz == 6
9922 && strcmp (note->namedata, "LINUX") == 0)
9923 return elfcore_grok_s390_high_gprs (abfd, note);
9928 if (note->namesz == 6
9929 && strcmp (note->namedata, "LINUX") == 0)
9930 return elfcore_grok_s390_timer (abfd, note);
9934 case NT_S390_TODCMP:
9935 if (note->namesz == 6
9936 && strcmp (note->namedata, "LINUX") == 0)
9937 return elfcore_grok_s390_todcmp (abfd, note);
9941 case NT_S390_TODPREG:
9942 if (note->namesz == 6
9943 && strcmp (note->namedata, "LINUX") == 0)
9944 return elfcore_grok_s390_todpreg (abfd, note);
9949 if (note->namesz == 6
9950 && strcmp (note->namedata, "LINUX") == 0)
9951 return elfcore_grok_s390_ctrs (abfd, note);
9955 case NT_S390_PREFIX:
9956 if (note->namesz == 6
9957 && strcmp (note->namedata, "LINUX") == 0)
9958 return elfcore_grok_s390_prefix (abfd, note);
9962 case NT_S390_LAST_BREAK:
9963 if (note->namesz == 6
9964 && strcmp (note->namedata, "LINUX") == 0)
9965 return elfcore_grok_s390_last_break (abfd, note);
9969 case NT_S390_SYSTEM_CALL:
9970 if (note->namesz == 6
9971 && strcmp (note->namedata, "LINUX") == 0)
9972 return elfcore_grok_s390_system_call (abfd, note);
9977 if (note->namesz == 6
9978 && strcmp (note->namedata, "LINUX") == 0)
9979 return elfcore_grok_s390_tdb (abfd, note);
9983 case NT_S390_VXRS_LOW:
9984 if (note->namesz == 6
9985 && strcmp (note->namedata, "LINUX") == 0)
9986 return elfcore_grok_s390_vxrs_low (abfd, note);
9990 case NT_S390_VXRS_HIGH:
9991 if (note->namesz == 6
9992 && strcmp (note->namedata, "LINUX") == 0)
9993 return elfcore_grok_s390_vxrs_high (abfd, note);
9998 if (note->namesz == 6
9999 && strcmp (note->namedata, "LINUX") == 0)
10000 return elfcore_grok_s390_gs_cb (abfd, note);
10004 case NT_S390_GS_BC:
10005 if (note->namesz == 6
10006 && strcmp (note->namedata, "LINUX") == 0)
10007 return elfcore_grok_s390_gs_bc (abfd, note);
10012 if (note->namesz == 6
10013 && strcmp (note->namedata, "LINUX") == 0)
10014 return elfcore_grok_arm_vfp (abfd, note);
10019 if (note->namesz == 6
10020 && strcmp (note->namedata, "LINUX") == 0)
10021 return elfcore_grok_aarch_tls (abfd, note);
10025 case NT_ARM_HW_BREAK:
10026 if (note->namesz == 6
10027 && strcmp (note->namedata, "LINUX") == 0)
10028 return elfcore_grok_aarch_hw_break (abfd, note);
10032 case NT_ARM_HW_WATCH:
10033 if (note->namesz == 6
10034 && strcmp (note->namedata, "LINUX") == 0)
10035 return elfcore_grok_aarch_hw_watch (abfd, note);
10040 if (note->namesz == 6
10041 && strcmp (note->namedata, "LINUX") == 0)
10042 return elfcore_grok_aarch_sve (abfd, note);
10048 if (bed->elf_backend_grok_psinfo)
10049 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10051 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10052 return elfcore_grok_psinfo (abfd, note);
10059 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10064 sect->size = note->descsz;
10065 sect->filepos = note->descpos;
10066 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10072 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10076 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10083 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10085 struct bfd_build_id* build_id;
10087 if (note->descsz == 0)
10090 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10091 if (build_id == NULL)
10094 build_id->size = note->descsz;
10095 memcpy (build_id->data, note->descdata, note->descsz);
10096 abfd->build_id = build_id;
10102 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10104 switch (note->type)
10109 case NT_GNU_PROPERTY_TYPE_0:
10110 return _bfd_elf_parse_gnu_properties (abfd, note);
10112 case NT_GNU_BUILD_ID:
10113 return elfobj_grok_gnu_build_id (abfd, note);
10118 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10120 struct sdt_note *cur =
10121 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
10124 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10125 cur->size = (bfd_size_type) note->descsz;
10126 memcpy (cur->data, note->descdata, note->descsz);
10128 elf_tdata (abfd)->sdt_note_head = cur;
10134 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10136 switch (note->type)
10139 return elfobj_grok_stapsdt_note_1 (abfd, note);
10147 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10151 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10154 if (note->descsz < 108)
10159 if (note->descsz < 120)
10167 /* Check for version 1 in pr_version. */
10168 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10173 /* Skip over pr_psinfosz. */
10174 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10178 offset += 4; /* Padding before pr_psinfosz. */
10182 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10183 elf_tdata (abfd)->core->program
10184 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10187 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10188 elf_tdata (abfd)->core->command
10189 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10192 /* Padding before pr_pid. */
10195 /* The pr_pid field was added in version "1a". */
10196 if (note->descsz < offset + 4)
10199 elf_tdata (abfd)->core->pid
10200 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10206 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10212 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10213 Also compute minimum size of this note. */
10214 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10218 min_size = offset + (4 * 2) + 4 + 4 + 4;
10222 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10223 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10230 if (note->descsz < min_size)
10233 /* Check for version 1 in pr_version. */
10234 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10237 /* Extract size of pr_reg from pr_gregsetsz. */
10238 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10239 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10241 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10246 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10250 /* Skip over pr_osreldate. */
10253 /* Read signal from pr_cursig. */
10254 if (elf_tdata (abfd)->core->signal == 0)
10255 elf_tdata (abfd)->core->signal
10256 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10259 /* Read TID from pr_pid. */
10260 elf_tdata (abfd)->core->lwpid
10261 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10264 /* Padding before pr_reg. */
10265 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10268 /* Make sure that there is enough data remaining in the note. */
10269 if ((note->descsz - offset) < size)
10272 /* Make a ".reg/999" section and a ".reg" section. */
10273 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10274 size, note->descpos + offset);
10278 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10280 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10282 switch (note->type)
10285 if (bed->elf_backend_grok_freebsd_prstatus)
10286 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10288 return elfcore_grok_freebsd_prstatus (abfd, note);
10291 return elfcore_grok_prfpreg (abfd, note);
10294 return elfcore_grok_freebsd_psinfo (abfd, note);
10296 case NT_FREEBSD_THRMISC:
10297 if (note->namesz == 8)
10298 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10302 case NT_FREEBSD_PROCSTAT_PROC:
10303 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10306 case NT_FREEBSD_PROCSTAT_FILES:
10307 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10310 case NT_FREEBSD_PROCSTAT_VMMAP:
10311 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10314 case NT_FREEBSD_PROCSTAT_AUXV:
10316 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10321 sect->size = note->descsz - 4;
10322 sect->filepos = note->descpos + 4;
10323 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10328 case NT_X86_XSTATE:
10329 if (note->namesz == 8)
10330 return elfcore_grok_xstatereg (abfd, note);
10334 case NT_FREEBSD_PTLWPINFO:
10335 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10339 return elfcore_grok_arm_vfp (abfd, note);
10347 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10351 cp = strchr (note->namedata, '@');
10354 *lwpidp = atoi(cp + 1);
10361 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10363 if (note->descsz <= 0x7c + 31)
10366 /* Signal number at offset 0x08. */
10367 elf_tdata (abfd)->core->signal
10368 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10370 /* Process ID at offset 0x50. */
10371 elf_tdata (abfd)->core->pid
10372 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10374 /* Command name at 0x7c (max 32 bytes, including nul). */
10375 elf_tdata (abfd)->core->command
10376 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10378 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10383 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10387 if (elfcore_netbsd_get_lwpid (note, &lwp))
10388 elf_tdata (abfd)->core->lwpid = lwp;
10390 if (note->type == NT_NETBSDCORE_PROCINFO)
10392 /* NetBSD-specific core "procinfo". Note that we expect to
10393 find this note before any of the others, which is fine,
10394 since the kernel writes this note out first when it
10395 creates a core file. */
10397 return elfcore_grok_netbsd_procinfo (abfd, note);
10400 /* As of Jan 2002 there are no other machine-independent notes
10401 defined for NetBSD core files. If the note type is less
10402 than the start of the machine-dependent note types, we don't
10405 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10409 switch (bfd_get_arch (abfd))
10411 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10412 PT_GETFPREGS == mach+2. */
10414 case bfd_arch_alpha:
10415 case bfd_arch_sparc:
10416 switch (note->type)
10418 case NT_NETBSDCORE_FIRSTMACH+0:
10419 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10421 case NT_NETBSDCORE_FIRSTMACH+2:
10422 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10428 /* On all other arch's, PT_GETREGS == mach+1 and
10429 PT_GETFPREGS == mach+3. */
10432 switch (note->type)
10434 case NT_NETBSDCORE_FIRSTMACH+1:
10435 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10437 case NT_NETBSDCORE_FIRSTMACH+3:
10438 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10448 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10450 if (note->descsz <= 0x48 + 31)
10453 /* Signal number at offset 0x08. */
10454 elf_tdata (abfd)->core->signal
10455 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10457 /* Process ID at offset 0x20. */
10458 elf_tdata (abfd)->core->pid
10459 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10461 /* Command name at 0x48 (max 32 bytes, including nul). */
10462 elf_tdata (abfd)->core->command
10463 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10469 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10471 if (note->type == NT_OPENBSD_PROCINFO)
10472 return elfcore_grok_openbsd_procinfo (abfd, note);
10474 if (note->type == NT_OPENBSD_REGS)
10475 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10477 if (note->type == NT_OPENBSD_FPREGS)
10478 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10480 if (note->type == NT_OPENBSD_XFPREGS)
10481 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10483 if (note->type == NT_OPENBSD_AUXV)
10485 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10490 sect->size = note->descsz;
10491 sect->filepos = note->descpos;
10492 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10497 if (note->type == NT_OPENBSD_WCOOKIE)
10499 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10504 sect->size = note->descsz;
10505 sect->filepos = note->descpos;
10506 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10515 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10517 void *ddata = note->descdata;
10524 if (note->descsz < 16)
10527 /* nto_procfs_status 'pid' field is at offset 0. */
10528 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10530 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10531 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10533 /* nto_procfs_status 'flags' field is at offset 8. */
10534 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10536 /* nto_procfs_status 'what' field is at offset 14. */
10537 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10539 elf_tdata (abfd)->core->signal = sig;
10540 elf_tdata (abfd)->core->lwpid = *tid;
10543 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10544 do not come from signals so we make sure we set the current
10545 thread just in case. */
10546 if (flags & 0x00000080)
10547 elf_tdata (abfd)->core->lwpid = *tid;
10549 /* Make a ".qnx_core_status/%d" section. */
10550 sprintf (buf, ".qnx_core_status/%ld", *tid);
10552 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10555 strcpy (name, buf);
10557 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10561 sect->size = note->descsz;
10562 sect->filepos = note->descpos;
10563 sect->alignment_power = 2;
10565 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10569 elfcore_grok_nto_regs (bfd *abfd,
10570 Elf_Internal_Note *note,
10578 /* Make a "(base)/%d" section. */
10579 sprintf (buf, "%s/%ld", base, tid);
10581 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10584 strcpy (name, buf);
10586 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10590 sect->size = note->descsz;
10591 sect->filepos = note->descpos;
10592 sect->alignment_power = 2;
10594 /* This is the current thread. */
10595 if (elf_tdata (abfd)->core->lwpid == tid)
10596 return elfcore_maybe_make_sect (abfd, base, sect);
10601 #define BFD_QNT_CORE_INFO 7
10602 #define BFD_QNT_CORE_STATUS 8
10603 #define BFD_QNT_CORE_GREG 9
10604 #define BFD_QNT_CORE_FPREG 10
10607 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10609 /* Every GREG section has a STATUS section before it. Store the
10610 tid from the previous call to pass down to the next gregs
10612 static long tid = 1;
10614 switch (note->type)
10616 case BFD_QNT_CORE_INFO:
10617 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10618 case BFD_QNT_CORE_STATUS:
10619 return elfcore_grok_nto_status (abfd, note, &tid);
10620 case BFD_QNT_CORE_GREG:
10621 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10622 case BFD_QNT_CORE_FPREG:
10623 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10630 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10636 /* Use note name as section name. */
10637 len = note->namesz;
10638 name = (char *) bfd_alloc (abfd, len);
10641 memcpy (name, note->namedata, len);
10642 name[len - 1] = '\0';
10644 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10648 sect->size = note->descsz;
10649 sect->filepos = note->descpos;
10650 sect->alignment_power = 1;
10655 /* Function: elfcore_write_note
10658 buffer to hold note, and current size of buffer
10662 size of data for note
10664 Writes note to end of buffer. ELF64 notes are written exactly as
10665 for ELF32, despite the current (as of 2006) ELF gabi specifying
10666 that they ought to have 8-byte namesz and descsz field, and have
10667 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10670 Pointer to realloc'd buffer, *BUFSIZ updated. */
10673 elfcore_write_note (bfd *abfd,
10681 Elf_External_Note *xnp;
10688 namesz = strlen (name) + 1;
10690 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
10692 buf = (char *) realloc (buf, *bufsiz + newspace);
10695 dest = buf + *bufsiz;
10696 *bufsiz += newspace;
10697 xnp = (Elf_External_Note *) dest;
10698 H_PUT_32 (abfd, namesz, xnp->namesz);
10699 H_PUT_32 (abfd, size, xnp->descsz);
10700 H_PUT_32 (abfd, type, xnp->type);
10704 memcpy (dest, name, namesz);
10712 memcpy (dest, input, size);
10722 /* gcc-8 warns (*) on all the strncpy calls in this function about
10723 possible string truncation. The "truncation" is not a bug. We
10724 have an external representation of structs with fields that are not
10725 necessarily NULL terminated and corresponding internal
10726 representation fields that are one larger so that they can always
10727 be NULL terminated.
10728 gcc versions between 4.2 and 4.6 do not allow pragma control of
10729 diagnostics inside functions, giving a hard error if you try to use
10730 the finer control available with later versions.
10731 gcc prior to 4.2 warns about diagnostic push and pop.
10732 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10733 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10734 (*) Depending on your system header files! */
10735 #if GCC_VERSION >= 8000
10736 # pragma GCC diagnostic push
10737 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10740 elfcore_write_prpsinfo (bfd *abfd,
10744 const char *psargs)
10746 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10748 if (bed->elf_backend_write_core_note != NULL)
10751 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10752 NT_PRPSINFO, fname, psargs);
10757 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10758 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10759 if (bed->s->elfclass == ELFCLASS32)
10761 # if defined (HAVE_PSINFO32_T)
10763 int note_type = NT_PSINFO;
10766 int note_type = NT_PRPSINFO;
10769 memset (&data, 0, sizeof (data));
10770 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10771 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10772 return elfcore_write_note (abfd, buf, bufsiz,
10773 "CORE", note_type, &data, sizeof (data));
10778 # if defined (HAVE_PSINFO_T)
10780 int note_type = NT_PSINFO;
10783 int note_type = NT_PRPSINFO;
10786 memset (&data, 0, sizeof (data));
10787 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10788 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10789 return elfcore_write_note (abfd, buf, bufsiz,
10790 "CORE", note_type, &data, sizeof (data));
10792 #endif /* PSINFO_T or PRPSINFO_T */
10797 #if GCC_VERSION >= 8000
10798 # pragma GCC diagnostic pop
10802 elfcore_write_linux_prpsinfo32
10803 (bfd *abfd, char *buf, int *bufsiz,
10804 const struct elf_internal_linux_prpsinfo *prpsinfo)
10806 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
10808 struct elf_external_linux_prpsinfo32_ugid16 data;
10810 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
10811 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10812 &data, sizeof (data));
10816 struct elf_external_linux_prpsinfo32_ugid32 data;
10818 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
10819 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10820 &data, sizeof (data));
10825 elfcore_write_linux_prpsinfo64
10826 (bfd *abfd, char *buf, int *bufsiz,
10827 const struct elf_internal_linux_prpsinfo *prpsinfo)
10829 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
10831 struct elf_external_linux_prpsinfo64_ugid16 data;
10833 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
10834 return elfcore_write_note (abfd, buf, bufsiz,
10835 "CORE", NT_PRPSINFO, &data, sizeof (data));
10839 struct elf_external_linux_prpsinfo64_ugid32 data;
10841 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
10842 return elfcore_write_note (abfd, buf, bufsiz,
10843 "CORE", NT_PRPSINFO, &data, sizeof (data));
10848 elfcore_write_prstatus (bfd *abfd,
10855 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10857 if (bed->elf_backend_write_core_note != NULL)
10860 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10862 pid, cursig, gregs);
10867 #if defined (HAVE_PRSTATUS_T)
10868 #if defined (HAVE_PRSTATUS32_T)
10869 if (bed->s->elfclass == ELFCLASS32)
10871 prstatus32_t prstat;
10873 memset (&prstat, 0, sizeof (prstat));
10874 prstat.pr_pid = pid;
10875 prstat.pr_cursig = cursig;
10876 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
10877 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
10878 NT_PRSTATUS, &prstat, sizeof (prstat));
10885 memset (&prstat, 0, sizeof (prstat));
10886 prstat.pr_pid = pid;
10887 prstat.pr_cursig = cursig;
10888 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
10889 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
10890 NT_PRSTATUS, &prstat, sizeof (prstat));
10892 #endif /* HAVE_PRSTATUS_T */
10898 #if defined (HAVE_LWPSTATUS_T)
10900 elfcore_write_lwpstatus (bfd *abfd,
10907 lwpstatus_t lwpstat;
10908 const char *note_name = "CORE";
10910 memset (&lwpstat, 0, sizeof (lwpstat));
10911 lwpstat.pr_lwpid = pid >> 16;
10912 lwpstat.pr_cursig = cursig;
10913 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10914 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
10915 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10916 #if !defined(gregs)
10917 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
10918 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
10920 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
10921 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
10924 return elfcore_write_note (abfd, buf, bufsiz, note_name,
10925 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
10927 #endif /* HAVE_LWPSTATUS_T */
10929 #if defined (HAVE_PSTATUS_T)
10931 elfcore_write_pstatus (bfd *abfd,
10935 int cursig ATTRIBUTE_UNUSED,
10936 const void *gregs ATTRIBUTE_UNUSED)
10938 const char *note_name = "CORE";
10939 #if defined (HAVE_PSTATUS32_T)
10940 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10942 if (bed->s->elfclass == ELFCLASS32)
10946 memset (&pstat, 0, sizeof (pstat));
10947 pstat.pr_pid = pid & 0xffff;
10948 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
10949 NT_PSTATUS, &pstat, sizeof (pstat));
10957 memset (&pstat, 0, sizeof (pstat));
10958 pstat.pr_pid = pid & 0xffff;
10959 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
10960 NT_PSTATUS, &pstat, sizeof (pstat));
10964 #endif /* HAVE_PSTATUS_T */
10967 elfcore_write_prfpreg (bfd *abfd,
10970 const void *fpregs,
10973 const char *note_name = "CORE";
10974 return elfcore_write_note (abfd, buf, bufsiz,
10975 note_name, NT_FPREGSET, fpregs, size);
10979 elfcore_write_prxfpreg (bfd *abfd,
10982 const void *xfpregs,
10985 char *note_name = "LINUX";
10986 return elfcore_write_note (abfd, buf, bufsiz,
10987 note_name, NT_PRXFPREG, xfpregs, size);
10991 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
10992 const void *xfpregs, int size)
10995 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
10996 note_name = "FreeBSD";
10998 note_name = "LINUX";
10999 return elfcore_write_note (abfd, buf, bufsiz,
11000 note_name, NT_X86_XSTATE, xfpregs, size);
11004 elfcore_write_ppc_vmx (bfd *abfd,
11007 const void *ppc_vmx,
11010 char *note_name = "LINUX";
11011 return elfcore_write_note (abfd, buf, bufsiz,
11012 note_name, NT_PPC_VMX, ppc_vmx, size);
11016 elfcore_write_ppc_vsx (bfd *abfd,
11019 const void *ppc_vsx,
11022 char *note_name = "LINUX";
11023 return elfcore_write_note (abfd, buf, bufsiz,
11024 note_name, NT_PPC_VSX, ppc_vsx, size);
11028 elfcore_write_ppc_tar (bfd *abfd,
11031 const void *ppc_tar,
11034 char *note_name = "LINUX";
11035 return elfcore_write_note (abfd, buf, bufsiz,
11036 note_name, NT_PPC_TAR, ppc_tar, size);
11040 elfcore_write_ppc_ppr (bfd *abfd,
11043 const void *ppc_ppr,
11046 char *note_name = "LINUX";
11047 return elfcore_write_note (abfd, buf, bufsiz,
11048 note_name, NT_PPC_PPR, ppc_ppr, size);
11052 elfcore_write_ppc_dscr (bfd *abfd,
11055 const void *ppc_dscr,
11058 char *note_name = "LINUX";
11059 return elfcore_write_note (abfd, buf, bufsiz,
11060 note_name, NT_PPC_DSCR, ppc_dscr, size);
11064 elfcore_write_ppc_ebb (bfd *abfd,
11067 const void *ppc_ebb,
11070 char *note_name = "LINUX";
11071 return elfcore_write_note (abfd, buf, bufsiz,
11072 note_name, NT_PPC_EBB, ppc_ebb, size);
11076 elfcore_write_ppc_pmu (bfd *abfd,
11079 const void *ppc_pmu,
11082 char *note_name = "LINUX";
11083 return elfcore_write_note (abfd, buf, bufsiz,
11084 note_name, NT_PPC_PMU, ppc_pmu, size);
11088 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11091 const void *ppc_tm_cgpr,
11094 char *note_name = "LINUX";
11095 return elfcore_write_note (abfd, buf, bufsiz,
11096 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11100 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11103 const void *ppc_tm_cfpr,
11106 char *note_name = "LINUX";
11107 return elfcore_write_note (abfd, buf, bufsiz,
11108 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11112 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11115 const void *ppc_tm_cvmx,
11118 char *note_name = "LINUX";
11119 return elfcore_write_note (abfd, buf, bufsiz,
11120 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11124 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11127 const void *ppc_tm_cvsx,
11130 char *note_name = "LINUX";
11131 return elfcore_write_note (abfd, buf, bufsiz,
11132 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11136 elfcore_write_ppc_tm_spr (bfd *abfd,
11139 const void *ppc_tm_spr,
11142 char *note_name = "LINUX";
11143 return elfcore_write_note (abfd, buf, bufsiz,
11144 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11148 elfcore_write_ppc_tm_ctar (bfd *abfd,
11151 const void *ppc_tm_ctar,
11154 char *note_name = "LINUX";
11155 return elfcore_write_note (abfd, buf, bufsiz,
11156 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11160 elfcore_write_ppc_tm_cppr (bfd *abfd,
11163 const void *ppc_tm_cppr,
11166 char *note_name = "LINUX";
11167 return elfcore_write_note (abfd, buf, bufsiz,
11168 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11172 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11175 const void *ppc_tm_cdscr,
11178 char *note_name = "LINUX";
11179 return elfcore_write_note (abfd, buf, bufsiz,
11180 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11184 elfcore_write_s390_high_gprs (bfd *abfd,
11187 const void *s390_high_gprs,
11190 char *note_name = "LINUX";
11191 return elfcore_write_note (abfd, buf, bufsiz,
11192 note_name, NT_S390_HIGH_GPRS,
11193 s390_high_gprs, size);
11197 elfcore_write_s390_timer (bfd *abfd,
11200 const void *s390_timer,
11203 char *note_name = "LINUX";
11204 return elfcore_write_note (abfd, buf, bufsiz,
11205 note_name, NT_S390_TIMER, s390_timer, size);
11209 elfcore_write_s390_todcmp (bfd *abfd,
11212 const void *s390_todcmp,
11215 char *note_name = "LINUX";
11216 return elfcore_write_note (abfd, buf, bufsiz,
11217 note_name, NT_S390_TODCMP, s390_todcmp, size);
11221 elfcore_write_s390_todpreg (bfd *abfd,
11224 const void *s390_todpreg,
11227 char *note_name = "LINUX";
11228 return elfcore_write_note (abfd, buf, bufsiz,
11229 note_name, NT_S390_TODPREG, s390_todpreg, size);
11233 elfcore_write_s390_ctrs (bfd *abfd,
11236 const void *s390_ctrs,
11239 char *note_name = "LINUX";
11240 return elfcore_write_note (abfd, buf, bufsiz,
11241 note_name, NT_S390_CTRS, s390_ctrs, size);
11245 elfcore_write_s390_prefix (bfd *abfd,
11248 const void *s390_prefix,
11251 char *note_name = "LINUX";
11252 return elfcore_write_note (abfd, buf, bufsiz,
11253 note_name, NT_S390_PREFIX, s390_prefix, size);
11257 elfcore_write_s390_last_break (bfd *abfd,
11260 const void *s390_last_break,
11263 char *note_name = "LINUX";
11264 return elfcore_write_note (abfd, buf, bufsiz,
11265 note_name, NT_S390_LAST_BREAK,
11266 s390_last_break, size);
11270 elfcore_write_s390_system_call (bfd *abfd,
11273 const void *s390_system_call,
11276 char *note_name = "LINUX";
11277 return elfcore_write_note (abfd, buf, bufsiz,
11278 note_name, NT_S390_SYSTEM_CALL,
11279 s390_system_call, size);
11283 elfcore_write_s390_tdb (bfd *abfd,
11286 const void *s390_tdb,
11289 char *note_name = "LINUX";
11290 return elfcore_write_note (abfd, buf, bufsiz,
11291 note_name, NT_S390_TDB, s390_tdb, size);
11295 elfcore_write_s390_vxrs_low (bfd *abfd,
11298 const void *s390_vxrs_low,
11301 char *note_name = "LINUX";
11302 return elfcore_write_note (abfd, buf, bufsiz,
11303 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11307 elfcore_write_s390_vxrs_high (bfd *abfd,
11310 const void *s390_vxrs_high,
11313 char *note_name = "LINUX";
11314 return elfcore_write_note (abfd, buf, bufsiz,
11315 note_name, NT_S390_VXRS_HIGH,
11316 s390_vxrs_high, size);
11320 elfcore_write_s390_gs_cb (bfd *abfd,
11323 const void *s390_gs_cb,
11326 char *note_name = "LINUX";
11327 return elfcore_write_note (abfd, buf, bufsiz,
11328 note_name, NT_S390_GS_CB,
11333 elfcore_write_s390_gs_bc (bfd *abfd,
11336 const void *s390_gs_bc,
11339 char *note_name = "LINUX";
11340 return elfcore_write_note (abfd, buf, bufsiz,
11341 note_name, NT_S390_GS_BC,
11346 elfcore_write_arm_vfp (bfd *abfd,
11349 const void *arm_vfp,
11352 char *note_name = "LINUX";
11353 return elfcore_write_note (abfd, buf, bufsiz,
11354 note_name, NT_ARM_VFP, arm_vfp, size);
11358 elfcore_write_aarch_tls (bfd *abfd,
11361 const void *aarch_tls,
11364 char *note_name = "LINUX";
11365 return elfcore_write_note (abfd, buf, bufsiz,
11366 note_name, NT_ARM_TLS, aarch_tls, size);
11370 elfcore_write_aarch_hw_break (bfd *abfd,
11373 const void *aarch_hw_break,
11376 char *note_name = "LINUX";
11377 return elfcore_write_note (abfd, buf, bufsiz,
11378 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11382 elfcore_write_aarch_hw_watch (bfd *abfd,
11385 const void *aarch_hw_watch,
11388 char *note_name = "LINUX";
11389 return elfcore_write_note (abfd, buf, bufsiz,
11390 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11394 elfcore_write_aarch_sve (bfd *abfd,
11397 const void *aarch_sve,
11400 char *note_name = "LINUX";
11401 return elfcore_write_note (abfd, buf, bufsiz,
11402 note_name, NT_ARM_SVE, aarch_sve, size);
11406 elfcore_write_register_note (bfd *abfd,
11409 const char *section,
11413 if (strcmp (section, ".reg2") == 0)
11414 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11415 if (strcmp (section, ".reg-xfp") == 0)
11416 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
11417 if (strcmp (section, ".reg-xstate") == 0)
11418 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
11419 if (strcmp (section, ".reg-ppc-vmx") == 0)
11420 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
11421 if (strcmp (section, ".reg-ppc-vsx") == 0)
11422 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
11423 if (strcmp (section, ".reg-ppc-tar") == 0)
11424 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11425 if (strcmp (section, ".reg-ppc-ppr") == 0)
11426 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11427 if (strcmp (section, ".reg-ppc-dscr") == 0)
11428 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11429 if (strcmp (section, ".reg-ppc-ebb") == 0)
11430 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11431 if (strcmp (section, ".reg-ppc-pmu") == 0)
11432 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11433 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11434 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11435 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11436 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11437 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11438 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11439 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11440 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11441 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11442 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11443 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11444 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11445 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11446 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11447 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11448 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
11449 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11450 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
11451 if (strcmp (section, ".reg-s390-timer") == 0)
11452 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11453 if (strcmp (section, ".reg-s390-todcmp") == 0)
11454 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11455 if (strcmp (section, ".reg-s390-todpreg") == 0)
11456 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11457 if (strcmp (section, ".reg-s390-ctrs") == 0)
11458 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11459 if (strcmp (section, ".reg-s390-prefix") == 0)
11460 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
11461 if (strcmp (section, ".reg-s390-last-break") == 0)
11462 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11463 if (strcmp (section, ".reg-s390-system-call") == 0)
11464 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
11465 if (strcmp (section, ".reg-s390-tdb") == 0)
11466 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
11467 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11468 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11469 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11470 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
11471 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11472 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11473 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11474 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
11475 if (strcmp (section, ".reg-arm-vfp") == 0)
11476 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
11477 if (strcmp (section, ".reg-aarch-tls") == 0)
11478 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11479 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11480 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11481 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11482 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
11483 if (strcmp (section, ".reg-aarch-sve") == 0)
11484 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
11489 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11494 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11495 gABI specifies that PT_NOTE alignment should be aligned to 4
11496 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11497 align is less than 4, we use 4 byte alignment. */
11500 if (align != 4 && align != 8)
11504 while (p < buf + size)
11506 Elf_External_Note *xnp = (Elf_External_Note *) p;
11507 Elf_Internal_Note in;
11509 if (offsetof (Elf_External_Note, name) > buf - p + size)
11512 in.type = H_GET_32 (abfd, xnp->type);
11514 in.namesz = H_GET_32 (abfd, xnp->namesz);
11515 in.namedata = xnp->name;
11516 if (in.namesz > buf - in.namedata + size)
11519 in.descsz = H_GET_32 (abfd, xnp->descsz);
11520 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
11521 in.descpos = offset + (in.descdata - buf);
11523 && (in.descdata >= buf + size
11524 || in.descsz > buf - in.descdata + size))
11527 switch (bfd_get_format (abfd))
11534 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11537 const char * string;
11539 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
11543 GROKER_ELEMENT ("", elfcore_grok_note),
11544 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11545 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11546 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11547 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11548 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
11550 #undef GROKER_ELEMENT
11553 for (i = ARRAY_SIZE (grokers); i--;)
11555 if (in.namesz >= grokers[i].len
11556 && strncmp (in.namedata, grokers[i].string,
11557 grokers[i].len) == 0)
11559 if (! grokers[i].func (abfd, & in))
11568 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11570 if (! elfobj_grok_gnu_note (abfd, &in))
11573 else if (in.namesz == sizeof "stapsdt"
11574 && strcmp (in.namedata, "stapsdt") == 0)
11576 if (! elfobj_grok_stapsdt_note (abfd, &in))
11582 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
11589 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11594 if (size == 0 || (size + 1) == 0)
11597 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11600 buf = (char *) bfd_malloc (size + 1);
11604 /* PR 17512: file: ec08f814
11605 0-termintate the buffer so that string searches will not overflow. */
11608 if (bfd_bread (buf, size, abfd) != size
11609 || !elf_parse_notes (abfd, buf, size, offset, align))
11619 /* Providing external access to the ELF program header table. */
11621 /* Return an upper bound on the number of bytes required to store a
11622 copy of ABFD's program header table entries. Return -1 if an error
11623 occurs; bfd_get_error will return an appropriate code. */
11626 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11628 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11630 bfd_set_error (bfd_error_wrong_format);
11634 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11637 /* Copy ABFD's program header table entries to *PHDRS. The entries
11638 will be stored as an array of Elf_Internal_Phdr structures, as
11639 defined in include/elf/internal.h. To find out how large the
11640 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11642 Return the number of program header table entries read, or -1 if an
11643 error occurs; bfd_get_error will return an appropriate code. */
11646 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11650 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11652 bfd_set_error (bfd_error_wrong_format);
11656 num_phdrs = elf_elfheader (abfd)->e_phnum;
11657 if (num_phdrs != 0)
11658 memcpy (phdrs, elf_tdata (abfd)->phdr,
11659 num_phdrs * sizeof (Elf_Internal_Phdr));
11664 enum elf_reloc_type_class
11665 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11666 const asection *rel_sec ATTRIBUTE_UNUSED,
11667 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
11669 return reloc_class_normal;
11672 /* For RELA architectures, return the relocation value for a
11673 relocation against a local symbol. */
11676 _bfd_elf_rela_local_sym (bfd *abfd,
11677 Elf_Internal_Sym *sym,
11679 Elf_Internal_Rela *rel)
11681 asection *sec = *psec;
11682 bfd_vma relocation;
11684 relocation = (sec->output_section->vma
11685 + sec->output_offset
11687 if ((sec->flags & SEC_MERGE)
11688 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
11689 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
11692 _bfd_merged_section_offset (abfd, psec,
11693 elf_section_data (sec)->sec_info,
11694 sym->st_value + rel->r_addend);
11697 /* If we have changed the section, and our original section is
11698 marked with SEC_EXCLUDE, it means that the original
11699 SEC_MERGE section has been completely subsumed in some
11700 other SEC_MERGE section. In this case, we need to leave
11701 some info around for --emit-relocs. */
11702 if ((sec->flags & SEC_EXCLUDE) != 0)
11703 sec->kept_section = *psec;
11706 rel->r_addend -= relocation;
11707 rel->r_addend += sec->output_section->vma + sec->output_offset;
11713 _bfd_elf_rel_local_sym (bfd *abfd,
11714 Elf_Internal_Sym *sym,
11718 asection *sec = *psec;
11720 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
11721 return sym->st_value + addend;
11723 return _bfd_merged_section_offset (abfd, psec,
11724 elf_section_data (sec)->sec_info,
11725 sym->st_value + addend);
11728 /* Adjust an address within a section. Given OFFSET within SEC, return
11729 the new offset within the section, based upon changes made to the
11730 section. Returns -1 if the offset is now invalid.
11731 The offset (in abnd out) is in target sized bytes, however big a
11735 _bfd_elf_section_offset (bfd *abfd,
11736 struct bfd_link_info *info,
11740 switch (sec->sec_info_type)
11742 case SEC_INFO_TYPE_STABS:
11743 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11745 case SEC_INFO_TYPE_EH_FRAME:
11746 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
11749 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11751 /* Reverse the offset. */
11752 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11753 bfd_size_type address_size = bed->s->arch_size / 8;
11755 /* address_size and sec->size are in octets. Convert
11756 to bytes before subtracting the original offset. */
11757 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
11763 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11764 reconstruct an ELF file by reading the segments out of remote memory
11765 based on the ELF file header at EHDR_VMA and the ELF program headers it
11766 points to. If not null, *LOADBASEP is filled in with the difference
11767 between the VMAs from which the segments were read, and the VMAs the
11768 file headers (and hence BFD's idea of each section's VMA) put them at.
11770 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11771 remote memory at target address VMA into the local buffer at MYADDR; it
11772 should return zero on success or an `errno' code on failure. TEMPL must
11773 be a BFD for an ELF target with the word size and byte order found in
11774 the remote memory. */
11777 bfd_elf_bfd_from_remote_memory
11780 bfd_size_type size,
11781 bfd_vma *loadbasep,
11782 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
11784 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
11785 (templ, ehdr_vma, size, loadbasep, target_read_memory);
11789 _bfd_elf_get_synthetic_symtab (bfd *abfd,
11790 long symcount ATTRIBUTE_UNUSED,
11791 asymbol **syms ATTRIBUTE_UNUSED,
11796 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11799 const char *relplt_name;
11800 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
11804 Elf_Internal_Shdr *hdr;
11810 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
11813 if (dynsymcount <= 0)
11816 if (!bed->plt_sym_val)
11819 relplt_name = bed->relplt_name;
11820 if (relplt_name == NULL)
11821 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
11822 relplt = bfd_get_section_by_name (abfd, relplt_name);
11823 if (relplt == NULL)
11826 hdr = &elf_section_data (relplt)->this_hdr;
11827 if (hdr->sh_link != elf_dynsymtab (abfd)
11828 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
11831 plt = bfd_get_section_by_name (abfd, ".plt");
11835 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
11836 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
11839 count = relplt->size / hdr->sh_entsize;
11840 size = count * sizeof (asymbol);
11841 p = relplt->relocation;
11842 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
11844 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
11845 if (p->addend != 0)
11848 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
11850 size += sizeof ("+0x") - 1 + 8;
11855 s = *ret = (asymbol *) bfd_malloc (size);
11859 names = (char *) (s + count);
11860 p = relplt->relocation;
11862 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
11867 addr = bed->plt_sym_val (i, plt, p);
11868 if (addr == (bfd_vma) -1)
11871 *s = **p->sym_ptr_ptr;
11872 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11873 we are defining a symbol, ensure one of them is set. */
11874 if ((s->flags & BSF_LOCAL) == 0)
11875 s->flags |= BSF_GLOBAL;
11876 s->flags |= BSF_SYNTHETIC;
11878 s->value = addr - plt->vma;
11881 len = strlen ((*p->sym_ptr_ptr)->name);
11882 memcpy (names, (*p->sym_ptr_ptr)->name, len);
11884 if (p->addend != 0)
11888 memcpy (names, "+0x", sizeof ("+0x") - 1);
11889 names += sizeof ("+0x") - 1;
11890 bfd_sprintf_vma (abfd, buf, p->addend);
11891 for (a = buf; *a == '0'; ++a)
11894 memcpy (names, a, len);
11897 memcpy (names, "@plt", sizeof ("@plt"));
11898 names += sizeof ("@plt");
11905 /* It is only used by x86-64 so far.
11906 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11907 but current usage would allow all of _bfd_std_section to be zero. */
11908 static const asymbol lcomm_sym
11909 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
11910 asection _bfd_elf_large_com_section
11911 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
11912 "LARGE_COMMON", 0, SEC_IS_COMMON);
11915 _bfd_elf_post_process_headers (bfd * abfd,
11916 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
11918 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
11920 i_ehdrp = elf_elfheader (abfd);
11922 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
11924 /* To make things simpler for the loader on Linux systems we set the
11925 osabi field to ELFOSABI_GNU if the binary contains symbols of
11926 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11927 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
11928 && elf_tdata (abfd)->has_gnu_symbols)
11929 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
11933 /* Return TRUE for ELF symbol types that represent functions.
11934 This is the default version of this function, which is sufficient for
11935 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11938 _bfd_elf_is_function_type (unsigned int type)
11940 return (type == STT_FUNC
11941 || type == STT_GNU_IFUNC);
11944 /* If the ELF symbol SYM might be a function in SEC, return the
11945 function size and set *CODE_OFF to the function's entry point,
11946 otherwise return zero. */
11949 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
11952 bfd_size_type size;
11954 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
11955 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
11956 || sym->section != sec)
11959 *code_off = sym->value;
11961 if (!(sym->flags & BSF_SYNTHETIC))
11962 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;