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. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bfd_boolean prep_headers (bfd *);
55 static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
56 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
58 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
59 file_ptr offset, size_t align);
61 /* Swap version information in and out. The version information is
62 currently size independent. If that ever changes, this code will
63 need to move into elfcode.h. */
65 /* Swap in a Verdef structure. */
68 _bfd_elf_swap_verdef_in (bfd *abfd,
69 const Elf_External_Verdef *src,
70 Elf_Internal_Verdef *dst)
72 dst->vd_version = H_GET_16 (abfd, src->vd_version);
73 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
74 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
75 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
76 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
77 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
78 dst->vd_next = H_GET_32 (abfd, src->vd_next);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (bfd *abfd,
85 const Elf_Internal_Verdef *src,
86 Elf_External_Verdef *dst)
88 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
89 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
90 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
91 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
92 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
93 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
94 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
97 /* Swap in a Verdaux structure. */
100 _bfd_elf_swap_verdaux_in (bfd *abfd,
101 const Elf_External_Verdaux *src,
102 Elf_Internal_Verdaux *dst)
104 dst->vda_name = H_GET_32 (abfd, src->vda_name);
105 dst->vda_next = H_GET_32 (abfd, src->vda_next);
108 /* Swap out a Verdaux structure. */
111 _bfd_elf_swap_verdaux_out (bfd *abfd,
112 const Elf_Internal_Verdaux *src,
113 Elf_External_Verdaux *dst)
115 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
116 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (bfd *abfd,
123 const Elf_External_Verneed *src,
124 Elf_Internal_Verneed *dst)
126 dst->vn_version = H_GET_16 (abfd, src->vn_version);
127 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
128 dst->vn_file = H_GET_32 (abfd, src->vn_file);
129 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
130 dst->vn_next = H_GET_32 (abfd, src->vn_next);
133 /* Swap out a Verneed structure. */
136 _bfd_elf_swap_verneed_out (bfd *abfd,
137 const Elf_Internal_Verneed *src,
138 Elf_External_Verneed *dst)
140 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
141 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
142 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
143 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
144 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
147 /* Swap in a Vernaux structure. */
150 _bfd_elf_swap_vernaux_in (bfd *abfd,
151 const Elf_External_Vernaux *src,
152 Elf_Internal_Vernaux *dst)
154 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
155 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
156 dst->vna_other = H_GET_16 (abfd, src->vna_other);
157 dst->vna_name = H_GET_32 (abfd, src->vna_name);
158 dst->vna_next = H_GET_32 (abfd, src->vna_next);
161 /* Swap out a Vernaux structure. */
164 _bfd_elf_swap_vernaux_out (bfd *abfd,
165 const Elf_Internal_Vernaux *src,
166 Elf_External_Vernaux *dst)
168 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
169 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
170 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
171 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
172 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
175 /* Swap in a Versym structure. */
178 _bfd_elf_swap_versym_in (bfd *abfd,
179 const Elf_External_Versym *src,
180 Elf_Internal_Versym *dst)
182 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
185 /* Swap out a Versym structure. */
188 _bfd_elf_swap_versym_out (bfd *abfd,
189 const Elf_Internal_Versym *src,
190 Elf_External_Versym *dst)
192 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
195 /* Standard ELF hash function. Do not change this function; you will
196 cause invalid hash tables to be generated. */
199 bfd_elf_hash (const char *namearg)
201 const unsigned char *name = (const unsigned char *) namearg;
206 while ((ch = *name++) != '\0')
209 if ((g = (h & 0xf0000000)) != 0)
212 /* The ELF ABI says `h &= ~g', but this is equivalent in
213 this case and on some machines one insn instead of two. */
217 return h & 0xffffffff;
220 /* DT_GNU_HASH hash function. Do not change this function; you will
221 cause invalid hash tables to be generated. */
224 bfd_elf_gnu_hash (const char *namearg)
226 const unsigned char *name = (const unsigned char *) namearg;
227 unsigned long h = 5381;
230 while ((ch = *name++) != '\0')
231 h = (h << 5) + h + ch;
232 return h & 0xffffffff;
235 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
236 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
238 bfd_elf_allocate_object (bfd *abfd,
240 enum elf_target_id object_id)
242 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
243 abfd->tdata.any = bfd_zalloc (abfd, object_size);
244 if (abfd->tdata.any == NULL)
247 elf_object_id (abfd) = object_id;
248 if (abfd->direction != read_direction)
250 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
253 elf_tdata (abfd)->o = o;
254 elf_program_header_size (abfd) = (bfd_size_type) -1;
261 bfd_elf_make_object (bfd *abfd)
263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
264 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
269 bfd_elf_mkcorefile (bfd *abfd)
271 /* I think this can be done just like an object file. */
272 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
274 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
275 return elf_tdata (abfd)->core != NULL;
279 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
281 Elf_Internal_Shdr **i_shdrp;
282 bfd_byte *shstrtab = NULL;
284 bfd_size_type shstrtabsize;
286 i_shdrp = elf_elfsections (abfd);
288 || shindex >= elf_numsections (abfd)
289 || i_shdrp[shindex] == 0)
292 shstrtab = i_shdrp[shindex]->contents;
293 if (shstrtab == NULL)
295 /* No cached one, attempt to read, and cache what we read. */
296 offset = i_shdrp[shindex]->sh_offset;
297 shstrtabsize = i_shdrp[shindex]->sh_size;
299 /* Allocate and clear an extra byte at the end, to prevent crashes
300 in case the string table is not terminated. */
301 if (shstrtabsize + 1 <= 1
302 || shstrtabsize > bfd_get_file_size (abfd)
303 || bfd_seek (abfd, offset, SEEK_SET) != 0
304 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
306 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
308 if (bfd_get_error () != bfd_error_system_call)
309 bfd_set_error (bfd_error_file_truncated);
310 bfd_release (abfd, shstrtab);
312 /* Once we've failed to read it, make sure we don't keep
313 trying. Otherwise, we'll keep allocating space for
314 the string table over and over. */
315 i_shdrp[shindex]->sh_size = 0;
318 shstrtab[shstrtabsize] = '\0';
319 i_shdrp[shindex]->contents = shstrtab;
321 return (char *) shstrtab;
325 bfd_elf_string_from_elf_section (bfd *abfd,
326 unsigned int shindex,
327 unsigned int strindex)
329 Elf_Internal_Shdr *hdr;
334 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
337 hdr = elf_elfsections (abfd)[shindex];
339 if (hdr->contents == NULL)
341 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
343 /* PR 17512: file: f057ec89. */
344 /* xgettext:c-format */
345 _bfd_error_handler (_("%pB: attempt to load strings from"
346 " a non-string section (number %d)"),
351 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
355 if (strindex >= hdr->sh_size)
357 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
359 /* xgettext:c-format */
360 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
361 abfd, strindex, (uint64_t) hdr->sh_size,
362 (shindex == shstrndx && strindex == hdr->sh_name
364 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
368 return ((char *) hdr->contents) + strindex;
371 /* Read and convert symbols to internal format.
372 SYMCOUNT specifies the number of symbols to read, starting from
373 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
374 are non-NULL, they are used to store the internal symbols, external
375 symbols, and symbol section index extensions, respectively.
376 Returns a pointer to the internal symbol buffer (malloced if necessary)
377 or NULL if there were no symbols or some kind of problem. */
380 bfd_elf_get_elf_syms (bfd *ibfd,
381 Elf_Internal_Shdr *symtab_hdr,
384 Elf_Internal_Sym *intsym_buf,
386 Elf_External_Sym_Shndx *extshndx_buf)
388 Elf_Internal_Shdr *shndx_hdr;
390 const bfd_byte *esym;
391 Elf_External_Sym_Shndx *alloc_extshndx;
392 Elf_External_Sym_Shndx *shndx;
393 Elf_Internal_Sym *alloc_intsym;
394 Elf_Internal_Sym *isym;
395 Elf_Internal_Sym *isymend;
396 const struct elf_backend_data *bed;
401 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
407 /* Normal syms might have section extension entries. */
409 if (elf_symtab_shndx_list (ibfd) != NULL)
411 elf_section_list * entry;
412 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
414 /* Find an index section that is linked to this symtab section. */
415 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
418 if (entry->hdr.sh_link >= elf_numsections (ibfd))
421 if (sections[entry->hdr.sh_link] == symtab_hdr)
423 shndx_hdr = & entry->hdr;
428 if (shndx_hdr == NULL)
430 if (symtab_hdr == & elf_symtab_hdr (ibfd))
431 /* Not really accurate, but this was how the old code used to work. */
432 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
433 /* Otherwise we do nothing. The assumption is that
434 the index table will not be needed. */
438 /* Read the symbols. */
440 alloc_extshndx = NULL;
442 bed = get_elf_backend_data (ibfd);
443 extsym_size = bed->s->sizeof_sym;
444 amt = (bfd_size_type) symcount * extsym_size;
445 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
446 if (extsym_buf == NULL)
448 alloc_ext = bfd_malloc2 (symcount, extsym_size);
449 extsym_buf = alloc_ext;
451 if (extsym_buf == NULL
452 || bfd_seek (ibfd, pos, SEEK_SET) != 0
453 || bfd_bread (extsym_buf, amt, ibfd) != amt)
459 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
463 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
464 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
465 if (extshndx_buf == NULL)
467 alloc_extshndx = (Elf_External_Sym_Shndx *)
468 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
469 extshndx_buf = alloc_extshndx;
471 if (extshndx_buf == NULL
472 || bfd_seek (ibfd, pos, SEEK_SET) != 0
473 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
480 if (intsym_buf == NULL)
482 alloc_intsym = (Elf_Internal_Sym *)
483 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
484 intsym_buf = alloc_intsym;
485 if (intsym_buf == NULL)
489 /* Convert the symbols to internal form. */
490 isymend = intsym_buf + symcount;
491 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
492 shndx = extshndx_buf;
494 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
495 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
497 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
498 /* xgettext:c-format */
499 _bfd_error_handler (_("%pB symbol number %lu references"
500 " nonexistent SHT_SYMTAB_SHNDX section"),
501 ibfd, (unsigned long) symoffset);
502 if (alloc_intsym != NULL)
509 if (alloc_ext != NULL)
511 if (alloc_extshndx != NULL)
512 free (alloc_extshndx);
517 /* Look up a symbol name. */
519 bfd_elf_sym_name (bfd *abfd,
520 Elf_Internal_Shdr *symtab_hdr,
521 Elf_Internal_Sym *isym,
525 unsigned int iname = isym->st_name;
526 unsigned int shindex = symtab_hdr->sh_link;
528 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
529 /* Check for a bogus st_shndx to avoid crashing. */
530 && isym->st_shndx < elf_numsections (abfd))
532 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
533 shindex = elf_elfheader (abfd)->e_shstrndx;
536 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
539 else if (sym_sec && *name == '\0')
540 name = bfd_section_name (abfd, sym_sec);
545 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
546 sections. The first element is the flags, the rest are section
549 typedef union elf_internal_group {
550 Elf_Internal_Shdr *shdr;
552 } Elf_Internal_Group;
554 /* Return the name of the group signature symbol. Why isn't the
555 signature just a string? */
558 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
560 Elf_Internal_Shdr *hdr;
561 unsigned char esym[sizeof (Elf64_External_Sym)];
562 Elf_External_Sym_Shndx eshndx;
563 Elf_Internal_Sym isym;
565 /* First we need to ensure the symbol table is available. Make sure
566 that it is a symbol table section. */
567 if (ghdr->sh_link >= elf_numsections (abfd))
569 hdr = elf_elfsections (abfd) [ghdr->sh_link];
570 if (hdr->sh_type != SHT_SYMTAB
571 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
574 /* Go read the symbol. */
575 hdr = &elf_tdata (abfd)->symtab_hdr;
576 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
577 &isym, esym, &eshndx) == NULL)
580 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
583 /* Set next_in_group list pointer, and group name for NEWSECT. */
586 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
588 unsigned int num_group = elf_tdata (abfd)->num_group;
590 /* If num_group is zero, read in all SHT_GROUP sections. The count
591 is set to -1 if there are no SHT_GROUP sections. */
594 unsigned int i, shnum;
596 /* First count the number of groups. If we have a SHT_GROUP
597 section with just a flag word (ie. sh_size is 4), ignore it. */
598 shnum = elf_numsections (abfd);
601 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
602 ( (shdr)->sh_type == SHT_GROUP \
603 && (shdr)->sh_size >= minsize \
604 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
605 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
607 for (i = 0; i < shnum; i++)
609 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
611 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
617 num_group = (unsigned) -1;
618 elf_tdata (abfd)->num_group = num_group;
619 elf_tdata (abfd)->group_sect_ptr = NULL;
623 /* We keep a list of elf section headers for group sections,
624 so we can find them quickly. */
627 elf_tdata (abfd)->num_group = num_group;
628 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
629 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
630 if (elf_tdata (abfd)->group_sect_ptr == NULL)
632 memset (elf_tdata (abfd)->group_sect_ptr, 0,
633 num_group * sizeof (Elf_Internal_Shdr *));
636 for (i = 0; i < shnum; i++)
638 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
640 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
643 Elf_Internal_Group *dest;
645 /* Make sure the group section has a BFD section
647 if (!bfd_section_from_shdr (abfd, i))
650 /* Add to list of sections. */
651 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
654 /* Read the raw contents. */
655 BFD_ASSERT (sizeof (*dest) >= 4);
656 amt = shdr->sh_size * sizeof (*dest) / 4;
657 shdr->contents = (unsigned char *)
658 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
659 /* PR binutils/4110: Handle corrupt group headers. */
660 if (shdr->contents == NULL)
663 /* xgettext:c-format */
664 (_("%pB: corrupt size field in group section"
665 " header: %#" PRIx64),
666 abfd, (uint64_t) shdr->sh_size);
667 bfd_set_error (bfd_error_bad_value);
672 memset (shdr->contents, 0, amt);
674 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
675 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
679 /* xgettext:c-format */
680 (_("%pB: invalid size field in group section"
681 " header: %#" PRIx64 ""),
682 abfd, (uint64_t) shdr->sh_size);
683 bfd_set_error (bfd_error_bad_value);
685 /* PR 17510: If the group contents are even
686 partially corrupt, do not allow any of the
687 contents to be used. */
688 memset (shdr->contents, 0, amt);
692 /* Translate raw contents, a flag word followed by an
693 array of elf section indices all in target byte order,
694 to the flag word followed by an array of elf section
696 src = shdr->contents + shdr->sh_size;
697 dest = (Elf_Internal_Group *) (shdr->contents + amt);
705 idx = H_GET_32 (abfd, src);
706 if (src == shdr->contents)
709 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
710 shdr->bfd_section->flags
711 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
716 dest->shdr = elf_elfsections (abfd)[idx];
717 /* PR binutils/23199: All sections in a
718 section group should be marked with
719 SHF_GROUP. But some tools generate
720 broken objects without SHF_GROUP. Fix
722 dest->shdr->sh_flags |= SHF_GROUP;
725 || dest->shdr->sh_type == SHT_GROUP)
728 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
736 /* PR 17510: Corrupt binaries might contain invalid groups. */
737 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
739 elf_tdata (abfd)->num_group = num_group;
741 /* If all groups are invalid then fail. */
744 elf_tdata (abfd)->group_sect_ptr = NULL;
745 elf_tdata (abfd)->num_group = num_group = -1;
747 (_("%pB: no valid group sections found"), abfd);
748 bfd_set_error (bfd_error_bad_value);
754 if (num_group != (unsigned) -1)
756 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
759 for (j = 0; j < num_group; j++)
761 /* Begin search from previous found group. */
762 unsigned i = (j + search_offset) % num_group;
764 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
765 Elf_Internal_Group *idx;
771 idx = (Elf_Internal_Group *) shdr->contents;
772 if (idx == NULL || shdr->sh_size < 4)
774 /* See PR 21957 for a reproducer. */
775 /* xgettext:c-format */
776 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
777 abfd, shdr->bfd_section);
778 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
779 bfd_set_error (bfd_error_bad_value);
782 n_elt = shdr->sh_size / 4;
784 /* Look through this group's sections to see if current
785 section is a member. */
787 if ((++idx)->shdr == hdr)
791 /* We are a member of this group. Go looking through
792 other members to see if any others are linked via
794 idx = (Elf_Internal_Group *) shdr->contents;
795 n_elt = shdr->sh_size / 4;
797 if ((++idx)->shdr != NULL
798 && (s = idx->shdr->bfd_section) != NULL
799 && elf_next_in_group (s) != NULL)
803 /* Snarf the group name from other member, and
804 insert current section in circular list. */
805 elf_group_name (newsect) = elf_group_name (s);
806 elf_next_in_group (newsect) = elf_next_in_group (s);
807 elf_next_in_group (s) = newsect;
813 gname = group_signature (abfd, shdr);
816 elf_group_name (newsect) = gname;
818 /* Start a circular list with one element. */
819 elf_next_in_group (newsect) = newsect;
822 /* If the group section has been created, point to the
824 if (shdr->bfd_section != NULL)
825 elf_next_in_group (shdr->bfd_section) = newsect;
827 elf_tdata (abfd)->group_search_offset = i;
834 if (elf_group_name (newsect) == NULL)
836 /* xgettext:c-format */
837 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
845 _bfd_elf_setup_sections (bfd *abfd)
848 unsigned int num_group = elf_tdata (abfd)->num_group;
849 bfd_boolean result = TRUE;
852 /* Process SHF_LINK_ORDER. */
853 for (s = abfd->sections; s != NULL; s = s->next)
855 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
856 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
858 unsigned int elfsec = this_hdr->sh_link;
859 /* FIXME: The old Intel compiler and old strip/objcopy may
860 not set the sh_link or sh_info fields. Hence we could
861 get the situation where elfsec is 0. */
864 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
865 if (bed->link_order_error_handler)
866 bed->link_order_error_handler
867 /* xgettext:c-format */
868 (_("%pB: warning: sh_link not set for section `%pA'"),
873 asection *linksec = NULL;
875 if (elfsec < elf_numsections (abfd))
877 this_hdr = elf_elfsections (abfd)[elfsec];
878 linksec = this_hdr->bfd_section;
882 Some strip/objcopy may leave an incorrect value in
883 sh_link. We don't want to proceed. */
887 /* xgettext:c-format */
888 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
889 s->owner, elfsec, s);
893 elf_linked_to_section (s) = linksec;
896 else if (this_hdr->sh_type == SHT_GROUP
897 && elf_next_in_group (s) == NULL)
900 /* xgettext:c-format */
901 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
902 abfd, elf_section_data (s)->this_idx);
907 /* Process section groups. */
908 if (num_group == (unsigned) -1)
911 for (i = 0; i < num_group; i++)
913 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
914 Elf_Internal_Group *idx;
917 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
918 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
921 /* xgettext:c-format */
922 (_("%pB: section group entry number %u is corrupt"),
928 idx = (Elf_Internal_Group *) shdr->contents;
929 n_elt = shdr->sh_size / 4;
935 if (idx->shdr == NULL)
937 else if (idx->shdr->bfd_section)
938 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
939 else if (idx->shdr->sh_type != SHT_RELA
940 && idx->shdr->sh_type != SHT_REL)
942 /* There are some unknown sections in the group. */
944 /* xgettext:c-format */
945 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
948 bfd_elf_string_from_elf_section (abfd,
949 (elf_elfheader (abfd)
962 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
964 return elf_next_in_group (sec) != NULL;
968 convert_debug_to_zdebug (bfd *abfd, const char *name)
970 unsigned int len = strlen (name);
971 char *new_name = bfd_alloc (abfd, len + 2);
972 if (new_name == NULL)
976 memcpy (new_name + 2, name + 1, len);
981 convert_zdebug_to_debug (bfd *abfd, const char *name)
983 unsigned int len = strlen (name);
984 char *new_name = bfd_alloc (abfd, len);
985 if (new_name == NULL)
988 memcpy (new_name + 1, name + 2, len - 1);
992 /* Make a BFD section from an ELF section. We store a pointer to the
993 BFD section in the bfd_section field of the header. */
996 _bfd_elf_make_section_from_shdr (bfd *abfd,
997 Elf_Internal_Shdr *hdr,
1003 const struct elf_backend_data *bed;
1005 if (hdr->bfd_section != NULL)
1008 newsect = bfd_make_section_anyway (abfd, name);
1009 if (newsect == NULL)
1012 hdr->bfd_section = newsect;
1013 elf_section_data (newsect)->this_hdr = *hdr;
1014 elf_section_data (newsect)->this_idx = shindex;
1016 /* Always use the real type/flags. */
1017 elf_section_type (newsect) = hdr->sh_type;
1018 elf_section_flags (newsect) = hdr->sh_flags;
1020 newsect->filepos = hdr->sh_offset;
1022 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1023 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1024 || ! bfd_set_section_alignment (abfd, newsect,
1025 bfd_log2 (hdr->sh_addralign)))
1028 flags = SEC_NO_FLAGS;
1029 if (hdr->sh_type != SHT_NOBITS)
1030 flags |= SEC_HAS_CONTENTS;
1031 if (hdr->sh_type == SHT_GROUP)
1033 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1036 if (hdr->sh_type != SHT_NOBITS)
1039 if ((hdr->sh_flags & SHF_WRITE) == 0)
1040 flags |= SEC_READONLY;
1041 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1043 else if ((flags & SEC_LOAD) != 0)
1045 if ((hdr->sh_flags & SHF_MERGE) != 0)
1048 newsect->entsize = hdr->sh_entsize;
1050 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1051 flags |= SEC_STRINGS;
1052 if (hdr->sh_flags & SHF_GROUP)
1053 if (!setup_group (abfd, hdr, newsect))
1055 if ((hdr->sh_flags & SHF_TLS) != 0)
1056 flags |= SEC_THREAD_LOCAL;
1057 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1058 flags |= SEC_EXCLUDE;
1060 if ((flags & SEC_ALLOC) == 0)
1062 /* The debugging sections appear to be recognized only by name,
1063 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1064 if (name [0] == '.')
1069 p = ".debug", n = 6;
1070 else if (name[1] == 'g' && name[2] == 'n')
1071 p = ".gnu.linkonce.wi.", n = 17;
1072 else if (name[1] == 'g' && name[2] == 'd')
1073 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1074 else if (name[1] == 'l')
1076 else if (name[1] == 's')
1078 else if (name[1] == 'z')
1079 p = ".zdebug", n = 7;
1082 if (p != NULL && strncmp (name, p, n) == 0)
1083 flags |= SEC_DEBUGGING;
1087 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1088 only link a single copy of the section. This is used to support
1089 g++. g++ will emit each template expansion in its own section.
1090 The symbols will be defined as weak, so that multiple definitions
1091 are permitted. The GNU linker extension is to actually discard
1092 all but one of the sections. */
1093 if (CONST_STRNEQ (name, ".gnu.linkonce")
1094 && elf_next_in_group (newsect) == NULL)
1095 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1097 bed = get_elf_backend_data (abfd);
1098 if (bed->elf_backend_section_flags)
1099 if (! bed->elf_backend_section_flags (&flags, hdr))
1102 if (! bfd_set_section_flags (abfd, newsect, flags))
1105 /* We do not parse the PT_NOTE segments as we are interested even in the
1106 separate debug info files which may have the segments offsets corrupted.
1107 PT_NOTEs from the core files are currently not parsed using BFD. */
1108 if (hdr->sh_type == SHT_NOTE)
1112 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1115 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1116 hdr->sh_offset, hdr->sh_addralign);
1120 if ((flags & SEC_ALLOC) != 0)
1122 Elf_Internal_Phdr *phdr;
1123 unsigned int i, nload;
1125 /* Some ELF linkers produce binaries with all the program header
1126 p_paddr fields zero. If we have such a binary with more than
1127 one PT_LOAD header, then leave the section lma equal to vma
1128 so that we don't create sections with overlapping lma. */
1129 phdr = elf_tdata (abfd)->phdr;
1130 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1131 if (phdr->p_paddr != 0)
1133 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1135 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1138 phdr = elf_tdata (abfd)->phdr;
1139 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1141 if (((phdr->p_type == PT_LOAD
1142 && (hdr->sh_flags & SHF_TLS) == 0)
1143 || phdr->p_type == PT_TLS)
1144 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1146 if ((flags & SEC_LOAD) == 0)
1147 newsect->lma = (phdr->p_paddr
1148 + hdr->sh_addr - phdr->p_vaddr);
1150 /* We used to use the same adjustment for SEC_LOAD
1151 sections, but that doesn't work if the segment
1152 is packed with code from multiple VMAs.
1153 Instead we calculate the section LMA based on
1154 the segment LMA. It is assumed that the
1155 segment will contain sections with contiguous
1156 LMAs, even if the VMAs are not. */
1157 newsect->lma = (phdr->p_paddr
1158 + hdr->sh_offset - phdr->p_offset);
1160 /* With contiguous segments, we can't tell from file
1161 offsets whether a section with zero size should
1162 be placed at the end of one segment or the
1163 beginning of the next. Decide based on vaddr. */
1164 if (hdr->sh_addr >= phdr->p_vaddr
1165 && (hdr->sh_addr + hdr->sh_size
1166 <= phdr->p_vaddr + phdr->p_memsz))
1172 /* Compress/decompress DWARF debug sections with names: .debug_* and
1173 .zdebug_*, after the section flags is set. */
1174 if ((flags & SEC_DEBUGGING)
1175 && ((name[1] == 'd' && name[6] == '_')
1176 || (name[1] == 'z' && name[7] == '_')))
1178 enum { nothing, compress, decompress } action = nothing;
1179 int compression_header_size;
1180 bfd_size_type uncompressed_size;
1181 unsigned int uncompressed_align_power;
1182 bfd_boolean compressed
1183 = bfd_is_section_compressed_with_header (abfd, newsect,
1184 &compression_header_size,
1186 &uncompressed_align_power);
1189 /* Compressed section. Check if we should decompress. */
1190 if ((abfd->flags & BFD_DECOMPRESS))
1191 action = decompress;
1194 /* Compress the uncompressed section or convert from/to .zdebug*
1195 section. Check if we should compress. */
1196 if (action == nothing)
1198 if (newsect->size != 0
1199 && (abfd->flags & BFD_COMPRESS)
1200 && compression_header_size >= 0
1201 && uncompressed_size > 0
1203 || ((compression_header_size > 0)
1204 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1210 if (action == compress)
1212 if (!bfd_init_section_compress_status (abfd, newsect))
1215 /* xgettext:c-format */
1216 (_("%pB: unable to initialize compress status for section %s"),
1223 if (!bfd_init_section_decompress_status (abfd, newsect))
1226 /* xgettext:c-format */
1227 (_("%pB: unable to initialize decompress status for section %s"),
1233 if (abfd->is_linker_input)
1236 && (action == decompress
1237 || (action == compress
1238 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1240 /* Convert section name from .zdebug_* to .debug_* so
1241 that linker will consider this section as a debug
1243 char *new_name = convert_zdebug_to_debug (abfd, name);
1244 if (new_name == NULL)
1246 bfd_rename_section (abfd, newsect, new_name);
1250 /* For objdump, don't rename the section. For objcopy, delay
1251 section rename to elf_fake_sections. */
1252 newsect->flags |= SEC_ELF_RENAME;
1258 const char *const bfd_elf_section_type_names[] =
1260 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1261 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1262 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1265 /* ELF relocs are against symbols. If we are producing relocatable
1266 output, and the reloc is against an external symbol, and nothing
1267 has given us any additional addend, the resulting reloc will also
1268 be against the same symbol. In such a case, we don't want to
1269 change anything about the way the reloc is handled, since it will
1270 all be done at final link time. Rather than put special case code
1271 into bfd_perform_relocation, all the reloc types use this howto
1272 function. It just short circuits the reloc if producing
1273 relocatable output against an external symbol. */
1275 bfd_reloc_status_type
1276 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1277 arelent *reloc_entry,
1279 void *data ATTRIBUTE_UNUSED,
1280 asection *input_section,
1282 char **error_message ATTRIBUTE_UNUSED)
1284 if (output_bfd != NULL
1285 && (symbol->flags & BSF_SECTION_SYM) == 0
1286 && (! reloc_entry->howto->partial_inplace
1287 || reloc_entry->addend == 0))
1289 reloc_entry->address += input_section->output_offset;
1290 return bfd_reloc_ok;
1293 return bfd_reloc_continue;
1296 /* Returns TRUE if section A matches section B.
1297 Names, addresses and links may be different, but everything else
1298 should be the same. */
1301 section_match (const Elf_Internal_Shdr * a,
1302 const Elf_Internal_Shdr * b)
1304 if (a->sh_type != b->sh_type
1305 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1306 || a->sh_addralign != b->sh_addralign
1307 || a->sh_entsize != b->sh_entsize)
1309 if (a->sh_type == SHT_SYMTAB
1310 || a->sh_type == SHT_STRTAB)
1312 return a->sh_size == b->sh_size;
1315 /* Find a section in OBFD that has the same characteristics
1316 as IHEADER. Return the index of this section or SHN_UNDEF if
1317 none can be found. Check's section HINT first, as this is likely
1318 to be the correct section. */
1321 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1322 const unsigned int hint)
1324 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1327 BFD_ASSERT (iheader != NULL);
1329 /* See PR 20922 for a reproducer of the NULL test. */
1330 if (hint < elf_numsections (obfd)
1331 && oheaders[hint] != NULL
1332 && section_match (oheaders[hint], iheader))
1335 for (i = 1; i < elf_numsections (obfd); i++)
1337 Elf_Internal_Shdr * oheader = oheaders[i];
1339 if (oheader == NULL)
1341 if (section_match (oheader, iheader))
1342 /* FIXME: Do we care if there is a potential for
1343 multiple matches ? */
1350 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1351 Processor specific section, based upon a matching input section.
1352 Returns TRUE upon success, FALSE otherwise. */
1355 copy_special_section_fields (const bfd *ibfd,
1357 const Elf_Internal_Shdr *iheader,
1358 Elf_Internal_Shdr *oheader,
1359 const unsigned int secnum)
1361 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1362 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1363 bfd_boolean changed = FALSE;
1364 unsigned int sh_link;
1366 if (oheader->sh_type == SHT_NOBITS)
1368 /* This is a feature for objcopy --only-keep-debug:
1369 When a section's type is changed to NOBITS, we preserve
1370 the sh_link and sh_info fields so that they can be
1371 matched up with the original.
1373 Note: Strictly speaking these assignments are wrong.
1374 The sh_link and sh_info fields should point to the
1375 relevent sections in the output BFD, which may not be in
1376 the same location as they were in the input BFD. But
1377 the whole point of this action is to preserve the
1378 original values of the sh_link and sh_info fields, so
1379 that they can be matched up with the section headers in
1380 the original file. So strictly speaking we may be
1381 creating an invalid ELF file, but it is only for a file
1382 that just contains debug info and only for sections
1383 without any contents. */
1384 if (oheader->sh_link == 0)
1385 oheader->sh_link = iheader->sh_link;
1386 if (oheader->sh_info == 0)
1387 oheader->sh_info = iheader->sh_info;
1391 /* Allow the target a chance to decide how these fields should be set. */
1392 if (bed->elf_backend_copy_special_section_fields != NULL
1393 && bed->elf_backend_copy_special_section_fields
1394 (ibfd, obfd, iheader, oheader))
1397 /* We have an iheader which might match oheader, and which has non-zero
1398 sh_info and/or sh_link fields. Attempt to follow those links and find
1399 the section in the output bfd which corresponds to the linked section
1400 in the input bfd. */
1401 if (iheader->sh_link != SHN_UNDEF)
1403 /* See PR 20931 for a reproducer. */
1404 if (iheader->sh_link >= elf_numsections (ibfd))
1407 /* xgettext:c-format */
1408 (_("%pB: invalid sh_link field (%d) in section number %d"),
1409 ibfd, iheader->sh_link, secnum);
1413 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1414 if (sh_link != SHN_UNDEF)
1416 oheader->sh_link = sh_link;
1420 /* FIXME: Should we install iheader->sh_link
1421 if we could not find a match ? */
1423 /* xgettext:c-format */
1424 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1427 if (iheader->sh_info)
1429 /* The sh_info field can hold arbitrary information, but if the
1430 SHF_LINK_INFO flag is set then it should be interpreted as a
1432 if (iheader->sh_flags & SHF_INFO_LINK)
1434 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1436 if (sh_link != SHN_UNDEF)
1437 oheader->sh_flags |= SHF_INFO_LINK;
1440 /* No idea what it means - just copy it. */
1441 sh_link = iheader->sh_info;
1443 if (sh_link != SHN_UNDEF)
1445 oheader->sh_info = sh_link;
1450 /* xgettext:c-format */
1451 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1457 /* Copy the program header and other data from one object module to
1461 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1463 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1464 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1465 const struct elf_backend_data *bed;
1468 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1469 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1472 if (!elf_flags_init (obfd))
1474 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1475 elf_flags_init (obfd) = TRUE;
1478 elf_gp (obfd) = elf_gp (ibfd);
1480 /* Also copy the EI_OSABI field. */
1481 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1482 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1484 /* If set, copy the EI_ABIVERSION field. */
1485 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1486 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1487 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1489 /* Copy object attributes. */
1490 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1492 if (iheaders == NULL || oheaders == NULL)
1495 bed = get_elf_backend_data (obfd);
1497 /* Possibly copy other fields in the section header. */
1498 for (i = 1; i < elf_numsections (obfd); i++)
1501 Elf_Internal_Shdr * oheader = oheaders[i];
1503 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1504 because of a special case need for generating separate debug info
1505 files. See below for more details. */
1507 || (oheader->sh_type != SHT_NOBITS
1508 && oheader->sh_type < SHT_LOOS))
1511 /* Ignore empty sections, and sections whose
1512 fields have already been initialised. */
1513 if (oheader->sh_size == 0
1514 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1517 /* Scan for the matching section in the input bfd.
1518 First we try for a direct mapping between the input and output sections. */
1519 for (j = 1; j < elf_numsections (ibfd); j++)
1521 const Elf_Internal_Shdr * iheader = iheaders[j];
1523 if (iheader == NULL)
1526 if (oheader->bfd_section != NULL
1527 && iheader->bfd_section != NULL
1528 && iheader->bfd_section->output_section != NULL
1529 && iheader->bfd_section->output_section == oheader->bfd_section)
1531 /* We have found a connection from the input section to the
1532 output section. Attempt to copy the header fields. If
1533 this fails then do not try any further sections - there
1534 should only be a one-to-one mapping between input and output. */
1535 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1536 j = elf_numsections (ibfd);
1541 if (j < elf_numsections (ibfd))
1544 /* That failed. So try to deduce the corresponding input section.
1545 Unfortunately we cannot compare names as the output string table
1546 is empty, so instead we check size, address and type. */
1547 for (j = 1; j < elf_numsections (ibfd); j++)
1549 const Elf_Internal_Shdr * iheader = iheaders[j];
1551 if (iheader == NULL)
1554 /* Try matching fields in the input section's header.
1555 Since --only-keep-debug turns all non-debug sections into
1556 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1558 if ((oheader->sh_type == SHT_NOBITS
1559 || iheader->sh_type == oheader->sh_type)
1560 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1561 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1562 && iheader->sh_addralign == oheader->sh_addralign
1563 && iheader->sh_entsize == oheader->sh_entsize
1564 && iheader->sh_size == oheader->sh_size
1565 && iheader->sh_addr == oheader->sh_addr
1566 && (iheader->sh_info != oheader->sh_info
1567 || iheader->sh_link != oheader->sh_link))
1569 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1574 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1576 /* Final attempt. Call the backend copy function
1577 with a NULL input section. */
1578 if (bed->elf_backend_copy_special_section_fields != NULL)
1579 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1587 get_segment_type (unsigned int p_type)
1592 case PT_NULL: pt = "NULL"; break;
1593 case PT_LOAD: pt = "LOAD"; break;
1594 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1595 case PT_INTERP: pt = "INTERP"; break;
1596 case PT_NOTE: pt = "NOTE"; break;
1597 case PT_SHLIB: pt = "SHLIB"; break;
1598 case PT_PHDR: pt = "PHDR"; break;
1599 case PT_TLS: pt = "TLS"; break;
1600 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1601 case PT_GNU_STACK: pt = "STACK"; break;
1602 case PT_GNU_RELRO: pt = "RELRO"; break;
1603 default: pt = NULL; break;
1608 /* Print out the program headers. */
1611 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1613 FILE *f = (FILE *) farg;
1614 Elf_Internal_Phdr *p;
1616 bfd_byte *dynbuf = NULL;
1618 p = elf_tdata (abfd)->phdr;
1623 fprintf (f, _("\nProgram Header:\n"));
1624 c = elf_elfheader (abfd)->e_phnum;
1625 for (i = 0; i < c; i++, p++)
1627 const char *pt = get_segment_type (p->p_type);
1632 sprintf (buf, "0x%lx", p->p_type);
1635 fprintf (f, "%8s off 0x", pt);
1636 bfd_fprintf_vma (abfd, f, p->p_offset);
1637 fprintf (f, " vaddr 0x");
1638 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1639 fprintf (f, " paddr 0x");
1640 bfd_fprintf_vma (abfd, f, p->p_paddr);
1641 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1642 fprintf (f, " filesz 0x");
1643 bfd_fprintf_vma (abfd, f, p->p_filesz);
1644 fprintf (f, " memsz 0x");
1645 bfd_fprintf_vma (abfd, f, p->p_memsz);
1646 fprintf (f, " flags %c%c%c",
1647 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1648 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1649 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1650 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1651 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1656 s = bfd_get_section_by_name (abfd, ".dynamic");
1659 unsigned int elfsec;
1660 unsigned long shlink;
1661 bfd_byte *extdyn, *extdynend;
1663 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1665 fprintf (f, _("\nDynamic Section:\n"));
1667 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1670 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1671 if (elfsec == SHN_BAD)
1673 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1675 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1676 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1679 /* PR 17512: file: 6f427532. */
1680 if (s->size < extdynsize)
1682 extdynend = extdyn + s->size;
1683 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1685 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1687 Elf_Internal_Dyn dyn;
1688 const char *name = "";
1690 bfd_boolean stringp;
1691 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1693 (*swap_dyn_in) (abfd, extdyn, &dyn);
1695 if (dyn.d_tag == DT_NULL)
1702 if (bed->elf_backend_get_target_dtag)
1703 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1705 if (!strcmp (name, ""))
1707 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1712 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1713 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1714 case DT_PLTGOT: name = "PLTGOT"; break;
1715 case DT_HASH: name = "HASH"; break;
1716 case DT_STRTAB: name = "STRTAB"; break;
1717 case DT_SYMTAB: name = "SYMTAB"; break;
1718 case DT_RELA: name = "RELA"; break;
1719 case DT_RELASZ: name = "RELASZ"; break;
1720 case DT_RELAENT: name = "RELAENT"; break;
1721 case DT_STRSZ: name = "STRSZ"; break;
1722 case DT_SYMENT: name = "SYMENT"; break;
1723 case DT_INIT: name = "INIT"; break;
1724 case DT_FINI: name = "FINI"; break;
1725 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1726 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1727 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1728 case DT_REL: name = "REL"; break;
1729 case DT_RELSZ: name = "RELSZ"; break;
1730 case DT_RELENT: name = "RELENT"; break;
1731 case DT_PLTREL: name = "PLTREL"; break;
1732 case DT_DEBUG: name = "DEBUG"; break;
1733 case DT_TEXTREL: name = "TEXTREL"; break;
1734 case DT_JMPREL: name = "JMPREL"; break;
1735 case DT_BIND_NOW: name = "BIND_NOW"; break;
1736 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1737 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1738 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1739 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1740 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1741 case DT_FLAGS: name = "FLAGS"; break;
1742 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1743 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1744 case DT_CHECKSUM: name = "CHECKSUM"; break;
1745 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1746 case DT_MOVEENT: name = "MOVEENT"; break;
1747 case DT_MOVESZ: name = "MOVESZ"; break;
1748 case DT_FEATURE: name = "FEATURE"; break;
1749 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1750 case DT_SYMINSZ: name = "SYMINSZ"; break;
1751 case DT_SYMINENT: name = "SYMINENT"; break;
1752 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1753 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1754 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1755 case DT_PLTPAD: name = "PLTPAD"; break;
1756 case DT_MOVETAB: name = "MOVETAB"; break;
1757 case DT_SYMINFO: name = "SYMINFO"; break;
1758 case DT_RELACOUNT: name = "RELACOUNT"; break;
1759 case DT_RELCOUNT: name = "RELCOUNT"; break;
1760 case DT_FLAGS_1: name = "FLAGS_1"; break;
1761 case DT_VERSYM: name = "VERSYM"; break;
1762 case DT_VERDEF: name = "VERDEF"; break;
1763 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1764 case DT_VERNEED: name = "VERNEED"; break;
1765 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1766 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1767 case DT_USED: name = "USED"; break;
1768 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1769 case DT_GNU_HASH: name = "GNU_HASH"; break;
1772 fprintf (f, " %-20s ", name);
1776 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1781 unsigned int tagv = dyn.d_un.d_val;
1783 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1786 fprintf (f, "%s", string);
1795 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1796 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1798 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1802 if (elf_dynverdef (abfd) != 0)
1804 Elf_Internal_Verdef *t;
1806 fprintf (f, _("\nVersion definitions:\n"));
1807 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1809 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1810 t->vd_flags, t->vd_hash,
1811 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1812 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1814 Elf_Internal_Verdaux *a;
1817 for (a = t->vd_auxptr->vda_nextptr;
1821 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1827 if (elf_dynverref (abfd) != 0)
1829 Elf_Internal_Verneed *t;
1831 fprintf (f, _("\nVersion References:\n"));
1832 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1834 Elf_Internal_Vernaux *a;
1836 fprintf (f, _(" required from %s:\n"),
1837 t->vn_filename ? t->vn_filename : "<corrupt>");
1838 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1839 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1840 a->vna_flags, a->vna_other,
1841 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1853 /* Get version string. */
1856 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1857 bfd_boolean *hidden)
1859 const char *version_string = NULL;
1860 if (elf_dynversym (abfd) != 0
1861 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1863 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1865 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1866 vernum &= VERSYM_VERSION;
1869 version_string = "";
1870 else if (vernum == 1
1871 && (vernum > elf_tdata (abfd)->cverdefs
1872 || (elf_tdata (abfd)->verdef[0].vd_flags
1874 version_string = "Base";
1875 else if (vernum <= elf_tdata (abfd)->cverdefs)
1877 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1880 Elf_Internal_Verneed *t;
1882 version_string = _("<corrupt>");
1883 for (t = elf_tdata (abfd)->verref;
1887 Elf_Internal_Vernaux *a;
1889 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1891 if (a->vna_other == vernum)
1893 version_string = a->vna_nodename;
1900 return version_string;
1903 /* Display ELF-specific fields of a symbol. */
1906 bfd_elf_print_symbol (bfd *abfd,
1909 bfd_print_symbol_type how)
1911 FILE *file = (FILE *) filep;
1914 case bfd_print_symbol_name:
1915 fprintf (file, "%s", symbol->name);
1917 case bfd_print_symbol_more:
1918 fprintf (file, "elf ");
1919 bfd_fprintf_vma (abfd, file, symbol->value);
1920 fprintf (file, " %x", symbol->flags);
1922 case bfd_print_symbol_all:
1924 const char *section_name;
1925 const char *name = NULL;
1926 const struct elf_backend_data *bed;
1927 unsigned char st_other;
1929 const char *version_string;
1932 section_name = symbol->section ? symbol->section->name : "(*none*)";
1934 bed = get_elf_backend_data (abfd);
1935 if (bed->elf_backend_print_symbol_all)
1936 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1940 name = symbol->name;
1941 bfd_print_symbol_vandf (abfd, file, symbol);
1944 fprintf (file, " %s\t", section_name);
1945 /* Print the "other" value for a symbol. For common symbols,
1946 we've already printed the size; now print the alignment.
1947 For other symbols, we have no specified alignment, and
1948 we've printed the address; now print the size. */
1949 if (symbol->section && bfd_is_com_section (symbol->section))
1950 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1952 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1953 bfd_fprintf_vma (abfd, file, val);
1955 /* If we have version information, print it. */
1956 version_string = _bfd_elf_get_symbol_version_string (abfd,
1962 fprintf (file, " %-11s", version_string);
1967 fprintf (file, " (%s)", version_string);
1968 for (i = 10 - strlen (version_string); i > 0; --i)
1973 /* If the st_other field is not zero, print it. */
1974 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1979 case STV_INTERNAL: fprintf (file, " .internal"); break;
1980 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1981 case STV_PROTECTED: fprintf (file, " .protected"); break;
1983 /* Some other non-defined flags are also present, so print
1985 fprintf (file, " 0x%02x", (unsigned int) st_other);
1988 fprintf (file, " %s", name);
1994 /* ELF .o/exec file reading */
1996 /* Create a new bfd section from an ELF section header. */
1999 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2001 Elf_Internal_Shdr *hdr;
2002 Elf_Internal_Ehdr *ehdr;
2003 const struct elf_backend_data *bed;
2005 bfd_boolean ret = TRUE;
2006 static bfd_boolean * sections_being_created = NULL;
2007 static bfd * sections_being_created_abfd = NULL;
2008 static unsigned int nesting = 0;
2010 if (shindex >= elf_numsections (abfd))
2015 /* PR17512: A corrupt ELF binary might contain a recursive group of
2016 sections, with each the string indices pointing to the next in the
2017 loop. Detect this here, by refusing to load a section that we are
2018 already in the process of loading. We only trigger this test if
2019 we have nested at least three sections deep as normal ELF binaries
2020 can expect to recurse at least once.
2022 FIXME: It would be better if this array was attached to the bfd,
2023 rather than being held in a static pointer. */
2025 if (sections_being_created_abfd != abfd)
2026 sections_being_created = NULL;
2027 if (sections_being_created == NULL)
2029 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2030 sections_being_created = (bfd_boolean *)
2031 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
2032 sections_being_created_abfd = abfd;
2034 if (sections_being_created [shindex])
2037 (_("%pB: warning: loop in section dependencies detected"), abfd);
2040 sections_being_created [shindex] = TRUE;
2043 hdr = elf_elfsections (abfd)[shindex];
2044 ehdr = elf_elfheader (abfd);
2045 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2050 bed = get_elf_backend_data (abfd);
2051 switch (hdr->sh_type)
2054 /* Inactive section. Throw it away. */
2057 case SHT_PROGBITS: /* Normal section with contents. */
2058 case SHT_NOBITS: /* .bss section. */
2059 case SHT_HASH: /* .hash section. */
2060 case SHT_NOTE: /* .note section. */
2061 case SHT_INIT_ARRAY: /* .init_array section. */
2062 case SHT_FINI_ARRAY: /* .fini_array section. */
2063 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2064 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2065 case SHT_GNU_HASH: /* .gnu.hash section. */
2066 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2069 case SHT_DYNAMIC: /* Dynamic linking information. */
2070 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2073 if (hdr->sh_link > elf_numsections (abfd))
2075 /* PR 10478: Accept Solaris binaries with a sh_link
2076 field set to SHN_BEFORE or SHN_AFTER. */
2077 switch (bfd_get_arch (abfd))
2080 case bfd_arch_sparc:
2081 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2082 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2084 /* Otherwise fall through. */
2089 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2091 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2093 Elf_Internal_Shdr *dynsymhdr;
2095 /* The shared libraries distributed with hpux11 have a bogus
2096 sh_link field for the ".dynamic" section. Find the
2097 string table for the ".dynsym" section instead. */
2098 if (elf_dynsymtab (abfd) != 0)
2100 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2101 hdr->sh_link = dynsymhdr->sh_link;
2105 unsigned int i, num_sec;
2107 num_sec = elf_numsections (abfd);
2108 for (i = 1; i < num_sec; i++)
2110 dynsymhdr = elf_elfsections (abfd)[i];
2111 if (dynsymhdr->sh_type == SHT_DYNSYM)
2113 hdr->sh_link = dynsymhdr->sh_link;
2121 case SHT_SYMTAB: /* A symbol table. */
2122 if (elf_onesymtab (abfd) == shindex)
2125 if (hdr->sh_entsize != bed->s->sizeof_sym)
2128 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2130 if (hdr->sh_size != 0)
2132 /* Some assemblers erroneously set sh_info to one with a
2133 zero sh_size. ld sees this as a global symbol count
2134 of (unsigned) -1. Fix it here. */
2139 /* PR 18854: A binary might contain more than one symbol table.
2140 Unusual, but possible. Warn, but continue. */
2141 if (elf_onesymtab (abfd) != 0)
2144 /* xgettext:c-format */
2145 (_("%pB: warning: multiple symbol tables detected"
2146 " - ignoring the table in section %u"),
2150 elf_onesymtab (abfd) = shindex;
2151 elf_symtab_hdr (abfd) = *hdr;
2152 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2153 abfd->flags |= HAS_SYMS;
2155 /* Sometimes a shared object will map in the symbol table. If
2156 SHF_ALLOC is set, and this is a shared object, then we also
2157 treat this section as a BFD section. We can not base the
2158 decision purely on SHF_ALLOC, because that flag is sometimes
2159 set in a relocatable object file, which would confuse the
2161 if ((hdr->sh_flags & SHF_ALLOC) != 0
2162 && (abfd->flags & DYNAMIC) != 0
2163 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2167 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2168 can't read symbols without that section loaded as well. It
2169 is most likely specified by the next section header. */
2171 elf_section_list * entry;
2172 unsigned int i, num_sec;
2174 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2175 if (entry->hdr.sh_link == shindex)
2178 num_sec = elf_numsections (abfd);
2179 for (i = shindex + 1; i < num_sec; i++)
2181 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2183 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2184 && hdr2->sh_link == shindex)
2189 for (i = 1; i < shindex; i++)
2191 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2193 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2194 && hdr2->sh_link == shindex)
2199 ret = bfd_section_from_shdr (abfd, i);
2200 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2204 case SHT_DYNSYM: /* A dynamic symbol table. */
2205 if (elf_dynsymtab (abfd) == shindex)
2208 if (hdr->sh_entsize != bed->s->sizeof_sym)
2211 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2213 if (hdr->sh_size != 0)
2216 /* Some linkers erroneously set sh_info to one with a
2217 zero sh_size. ld sees this as a global symbol count
2218 of (unsigned) -1. Fix it here. */
2223 /* PR 18854: A binary might contain more than one dynamic symbol table.
2224 Unusual, but possible. Warn, but continue. */
2225 if (elf_dynsymtab (abfd) != 0)
2228 /* xgettext:c-format */
2229 (_("%pB: warning: multiple dynamic symbol tables detected"
2230 " - ignoring the table in section %u"),
2234 elf_dynsymtab (abfd) = shindex;
2235 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2236 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2237 abfd->flags |= HAS_SYMS;
2239 /* Besides being a symbol table, we also treat this as a regular
2240 section, so that objcopy can handle it. */
2241 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2244 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2246 elf_section_list * entry;
2248 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2249 if (entry->ndx == shindex)
2252 entry = bfd_alloc (abfd, sizeof * entry);
2255 entry->ndx = shindex;
2257 entry->next = elf_symtab_shndx_list (abfd);
2258 elf_symtab_shndx_list (abfd) = entry;
2259 elf_elfsections (abfd)[shindex] = & entry->hdr;
2263 case SHT_STRTAB: /* A string table. */
2264 if (hdr->bfd_section != NULL)
2267 if (ehdr->e_shstrndx == shindex)
2269 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2270 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2274 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2277 elf_tdata (abfd)->strtab_hdr = *hdr;
2278 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2282 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2285 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2286 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2287 elf_elfsections (abfd)[shindex] = hdr;
2288 /* We also treat this as a regular section, so that objcopy
2290 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2295 /* If the string table isn't one of the above, then treat it as a
2296 regular section. We need to scan all the headers to be sure,
2297 just in case this strtab section appeared before the above. */
2298 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2300 unsigned int i, num_sec;
2302 num_sec = elf_numsections (abfd);
2303 for (i = 1; i < num_sec; i++)
2305 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2306 if (hdr2->sh_link == shindex)
2308 /* Prevent endless recursion on broken objects. */
2311 if (! bfd_section_from_shdr (abfd, i))
2313 if (elf_onesymtab (abfd) == i)
2315 if (elf_dynsymtab (abfd) == i)
2316 goto dynsymtab_strtab;
2320 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2325 /* *These* do a lot of work -- but build no sections! */
2327 asection *target_sect;
2328 Elf_Internal_Shdr *hdr2, **p_hdr;
2329 unsigned int num_sec = elf_numsections (abfd);
2330 struct bfd_elf_section_data *esdt;
2333 != (bfd_size_type) (hdr->sh_type == SHT_REL
2334 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2337 /* Check for a bogus link to avoid crashing. */
2338 if (hdr->sh_link >= num_sec)
2341 /* xgettext:c-format */
2342 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2343 abfd, hdr->sh_link, name, shindex);
2344 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2349 /* For some incomprehensible reason Oracle distributes
2350 libraries for Solaris in which some of the objects have
2351 bogus sh_link fields. It would be nice if we could just
2352 reject them, but, unfortunately, some people need to use
2353 them. We scan through the section headers; if we find only
2354 one suitable symbol table, we clobber the sh_link to point
2355 to it. I hope this doesn't break anything.
2357 Don't do it on executable nor shared library. */
2358 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2359 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2360 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2366 for (scan = 1; scan < num_sec; scan++)
2368 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2369 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2380 hdr->sh_link = found;
2383 /* Get the symbol table. */
2384 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2385 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2386 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2389 /* If this is an alloc section in an executable or shared
2390 library, or the reloc section does not use the main symbol
2391 table we don't treat it as a reloc section. BFD can't
2392 adequately represent such a section, so at least for now,
2393 we don't try. We just present it as a normal section. We
2394 also can't use it as a reloc section if it points to the
2395 null section, an invalid section, another reloc section, or
2396 its sh_link points to the null section. */
2397 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2398 && (hdr->sh_flags & SHF_ALLOC) != 0)
2399 || hdr->sh_link == SHN_UNDEF
2400 || hdr->sh_link != elf_onesymtab (abfd)
2401 || hdr->sh_info == SHN_UNDEF
2402 || hdr->sh_info >= num_sec
2403 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2404 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2406 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2411 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2414 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2415 if (target_sect == NULL)
2418 esdt = elf_section_data (target_sect);
2419 if (hdr->sh_type == SHT_RELA)
2420 p_hdr = &esdt->rela.hdr;
2422 p_hdr = &esdt->rel.hdr;
2424 /* PR 17512: file: 0b4f81b7. */
2427 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2432 elf_elfsections (abfd)[shindex] = hdr2;
2433 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2434 * bed->s->int_rels_per_ext_rel);
2435 target_sect->flags |= SEC_RELOC;
2436 target_sect->relocation = NULL;
2437 target_sect->rel_filepos = hdr->sh_offset;
2438 /* In the section to which the relocations apply, mark whether
2439 its relocations are of the REL or RELA variety. */
2440 if (hdr->sh_size != 0)
2442 if (hdr->sh_type == SHT_RELA)
2443 target_sect->use_rela_p = 1;
2445 abfd->flags |= HAS_RELOC;
2449 case SHT_GNU_verdef:
2450 elf_dynverdef (abfd) = shindex;
2451 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2452 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2455 case SHT_GNU_versym:
2456 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2459 elf_dynversym (abfd) = shindex;
2460 elf_tdata (abfd)->dynversym_hdr = *hdr;
2461 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2464 case SHT_GNU_verneed:
2465 elf_dynverref (abfd) = shindex;
2466 elf_tdata (abfd)->dynverref_hdr = *hdr;
2467 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2474 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2477 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2483 /* Possibly an attributes section. */
2484 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2485 || hdr->sh_type == bed->obj_attrs_section_type)
2487 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2489 _bfd_elf_parse_attributes (abfd, hdr);
2493 /* Check for any processor-specific section types. */
2494 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2497 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2499 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2500 /* FIXME: How to properly handle allocated section reserved
2501 for applications? */
2503 /* xgettext:c-format */
2504 (_("%pB: unknown type [%#x] section `%s'"),
2505 abfd, hdr->sh_type, name);
2508 /* Allow sections reserved for applications. */
2509 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2514 else if (hdr->sh_type >= SHT_LOPROC
2515 && hdr->sh_type <= SHT_HIPROC)
2516 /* FIXME: We should handle this section. */
2518 /* xgettext:c-format */
2519 (_("%pB: unknown type [%#x] section `%s'"),
2520 abfd, hdr->sh_type, name);
2521 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2523 /* Unrecognised OS-specific sections. */
2524 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2525 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2526 required to correctly process the section and the file should
2527 be rejected with an error message. */
2529 /* xgettext:c-format */
2530 (_("%pB: unknown type [%#x] section `%s'"),
2531 abfd, hdr->sh_type, name);
2534 /* Otherwise it should be processed. */
2535 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2540 /* FIXME: We should handle this section. */
2542 /* xgettext:c-format */
2543 (_("%pB: unknown type [%#x] section `%s'"),
2544 abfd, hdr->sh_type, name);
2552 if (sections_being_created && sections_being_created_abfd == abfd)
2553 sections_being_created [shindex] = FALSE;
2554 if (-- nesting == 0)
2556 sections_being_created = NULL;
2557 sections_being_created_abfd = abfd;
2562 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2565 bfd_sym_from_r_symndx (struct sym_cache *cache,
2567 unsigned long r_symndx)
2569 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2571 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2573 Elf_Internal_Shdr *symtab_hdr;
2574 unsigned char esym[sizeof (Elf64_External_Sym)];
2575 Elf_External_Sym_Shndx eshndx;
2577 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2578 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2579 &cache->sym[ent], esym, &eshndx) == NULL)
2582 if (cache->abfd != abfd)
2584 memset (cache->indx, -1, sizeof (cache->indx));
2587 cache->indx[ent] = r_symndx;
2590 return &cache->sym[ent];
2593 /* Given an ELF section number, retrieve the corresponding BFD
2597 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2599 if (sec_index >= elf_numsections (abfd))
2601 return elf_elfsections (abfd)[sec_index]->bfd_section;
2604 static const struct bfd_elf_special_section special_sections_b[] =
2606 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2607 { NULL, 0, 0, 0, 0 }
2610 static const struct bfd_elf_special_section special_sections_c[] =
2612 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2613 { NULL, 0, 0, 0, 0 }
2616 static const struct bfd_elf_special_section special_sections_d[] =
2618 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2619 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2620 /* There are more DWARF sections than these, but they needn't be added here
2621 unless you have to cope with broken compilers that don't emit section
2622 attributes or you want to help the user writing assembler. */
2623 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2624 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2625 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2626 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2627 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2628 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2629 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2630 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2631 { NULL, 0, 0, 0, 0 }
2634 static const struct bfd_elf_special_section special_sections_f[] =
2636 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2637 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2638 { NULL, 0 , 0, 0, 0 }
2641 static const struct bfd_elf_special_section special_sections_g[] =
2643 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2644 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2645 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2646 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2647 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2648 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2649 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2650 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2651 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2652 { NULL, 0, 0, 0, 0 }
2655 static const struct bfd_elf_special_section special_sections_h[] =
2657 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2658 { NULL, 0, 0, 0, 0 }
2661 static const struct bfd_elf_special_section special_sections_i[] =
2663 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2664 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2665 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2666 { NULL, 0, 0, 0, 0 }
2669 static const struct bfd_elf_special_section special_sections_l[] =
2671 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2672 { NULL, 0, 0, 0, 0 }
2675 static const struct bfd_elf_special_section special_sections_n[] =
2677 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2678 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2679 { NULL, 0, 0, 0, 0 }
2682 static const struct bfd_elf_special_section special_sections_p[] =
2684 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2685 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2686 { NULL, 0, 0, 0, 0 }
2689 static const struct bfd_elf_special_section special_sections_r[] =
2691 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2692 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2693 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2694 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2695 { NULL, 0, 0, 0, 0 }
2698 static const struct bfd_elf_special_section special_sections_s[] =
2700 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2701 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2702 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2703 /* See struct bfd_elf_special_section declaration for the semantics of
2704 this special case where .prefix_length != strlen (.prefix). */
2705 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2706 { NULL, 0, 0, 0, 0 }
2709 static const struct bfd_elf_special_section special_sections_t[] =
2711 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2712 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2713 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2714 { NULL, 0, 0, 0, 0 }
2717 static const struct bfd_elf_special_section special_sections_z[] =
2719 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2720 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2721 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2722 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2723 { NULL, 0, 0, 0, 0 }
2726 static const struct bfd_elf_special_section * const special_sections[] =
2728 special_sections_b, /* 'b' */
2729 special_sections_c, /* 'c' */
2730 special_sections_d, /* 'd' */
2732 special_sections_f, /* 'f' */
2733 special_sections_g, /* 'g' */
2734 special_sections_h, /* 'h' */
2735 special_sections_i, /* 'i' */
2738 special_sections_l, /* 'l' */
2740 special_sections_n, /* 'n' */
2742 special_sections_p, /* 'p' */
2744 special_sections_r, /* 'r' */
2745 special_sections_s, /* 's' */
2746 special_sections_t, /* 't' */
2752 special_sections_z /* 'z' */
2755 const struct bfd_elf_special_section *
2756 _bfd_elf_get_special_section (const char *name,
2757 const struct bfd_elf_special_section *spec,
2763 len = strlen (name);
2765 for (i = 0; spec[i].prefix != NULL; i++)
2768 int prefix_len = spec[i].prefix_length;
2770 if (len < prefix_len)
2772 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2775 suffix_len = spec[i].suffix_length;
2776 if (suffix_len <= 0)
2778 if (name[prefix_len] != 0)
2780 if (suffix_len == 0)
2782 if (name[prefix_len] != '.'
2783 && (suffix_len == -2
2784 || (rela && spec[i].type == SHT_REL)))
2790 if (len < prefix_len + suffix_len)
2792 if (memcmp (name + len - suffix_len,
2793 spec[i].prefix + prefix_len,
2803 const struct bfd_elf_special_section *
2804 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2807 const struct bfd_elf_special_section *spec;
2808 const struct elf_backend_data *bed;
2810 /* See if this is one of the special sections. */
2811 if (sec->name == NULL)
2814 bed = get_elf_backend_data (abfd);
2815 spec = bed->special_sections;
2818 spec = _bfd_elf_get_special_section (sec->name,
2819 bed->special_sections,
2825 if (sec->name[0] != '.')
2828 i = sec->name[1] - 'b';
2829 if (i < 0 || i > 'z' - 'b')
2832 spec = special_sections[i];
2837 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2841 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2843 struct bfd_elf_section_data *sdata;
2844 const struct elf_backend_data *bed;
2845 const struct bfd_elf_special_section *ssect;
2847 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2850 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2854 sec->used_by_bfd = sdata;
2857 /* Indicate whether or not this section should use RELA relocations. */
2858 bed = get_elf_backend_data (abfd);
2859 sec->use_rela_p = bed->default_use_rela_p;
2861 /* When we read a file, we don't need to set ELF section type and
2862 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2863 anyway. We will set ELF section type and flags for all linker
2864 created sections. If user specifies BFD section flags, we will
2865 set ELF section type and flags based on BFD section flags in
2866 elf_fake_sections. Special handling for .init_array/.fini_array
2867 output sections since they may contain .ctors/.dtors input
2868 sections. We don't want _bfd_elf_init_private_section_data to
2869 copy ELF section type from .ctors/.dtors input sections. */
2870 if (abfd->direction != read_direction
2871 || (sec->flags & SEC_LINKER_CREATED) != 0)
2873 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2876 || (sec->flags & SEC_LINKER_CREATED) != 0
2877 || ssect->type == SHT_INIT_ARRAY
2878 || ssect->type == SHT_FINI_ARRAY))
2880 elf_section_type (sec) = ssect->type;
2881 elf_section_flags (sec) = ssect->attr;
2885 return _bfd_generic_new_section_hook (abfd, sec);
2888 /* Create a new bfd section from an ELF program header.
2890 Since program segments have no names, we generate a synthetic name
2891 of the form segment<NUM>, where NUM is generally the index in the
2892 program header table. For segments that are split (see below) we
2893 generate the names segment<NUM>a and segment<NUM>b.
2895 Note that some program segments may have a file size that is different than
2896 (less than) the memory size. All this means is that at execution the
2897 system must allocate the amount of memory specified by the memory size,
2898 but only initialize it with the first "file size" bytes read from the
2899 file. This would occur for example, with program segments consisting
2900 of combined data+bss.
2902 To handle the above situation, this routine generates TWO bfd sections
2903 for the single program segment. The first has the length specified by
2904 the file size of the segment, and the second has the length specified
2905 by the difference between the two sizes. In effect, the segment is split
2906 into its initialized and uninitialized parts.
2911 _bfd_elf_make_section_from_phdr (bfd *abfd,
2912 Elf_Internal_Phdr *hdr,
2914 const char *type_name)
2922 split = ((hdr->p_memsz > 0)
2923 && (hdr->p_filesz > 0)
2924 && (hdr->p_memsz > hdr->p_filesz));
2926 if (hdr->p_filesz > 0)
2928 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2929 len = strlen (namebuf) + 1;
2930 name = (char *) bfd_alloc (abfd, len);
2933 memcpy (name, namebuf, len);
2934 newsect = bfd_make_section (abfd, name);
2935 if (newsect == NULL)
2937 newsect->vma = hdr->p_vaddr;
2938 newsect->lma = hdr->p_paddr;
2939 newsect->size = hdr->p_filesz;
2940 newsect->filepos = hdr->p_offset;
2941 newsect->flags |= SEC_HAS_CONTENTS;
2942 newsect->alignment_power = bfd_log2 (hdr->p_align);
2943 if (hdr->p_type == PT_LOAD)
2945 newsect->flags |= SEC_ALLOC;
2946 newsect->flags |= SEC_LOAD;
2947 if (hdr->p_flags & PF_X)
2949 /* FIXME: all we known is that it has execute PERMISSION,
2951 newsect->flags |= SEC_CODE;
2954 if (!(hdr->p_flags & PF_W))
2956 newsect->flags |= SEC_READONLY;
2960 if (hdr->p_memsz > hdr->p_filesz)
2964 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2965 len = strlen (namebuf) + 1;
2966 name = (char *) bfd_alloc (abfd, len);
2969 memcpy (name, namebuf, len);
2970 newsect = bfd_make_section (abfd, name);
2971 if (newsect == NULL)
2973 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2974 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2975 newsect->size = hdr->p_memsz - hdr->p_filesz;
2976 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2977 align = newsect->vma & -newsect->vma;
2978 if (align == 0 || align > hdr->p_align)
2979 align = hdr->p_align;
2980 newsect->alignment_power = bfd_log2 (align);
2981 if (hdr->p_type == PT_LOAD)
2983 /* Hack for gdb. Segments that have not been modified do
2984 not have their contents written to a core file, on the
2985 assumption that a debugger can find the contents in the
2986 executable. We flag this case by setting the fake
2987 section size to zero. Note that "real" bss sections will
2988 always have their contents dumped to the core file. */
2989 if (bfd_get_format (abfd) == bfd_core)
2991 newsect->flags |= SEC_ALLOC;
2992 if (hdr->p_flags & PF_X)
2993 newsect->flags |= SEC_CODE;
2995 if (!(hdr->p_flags & PF_W))
2996 newsect->flags |= SEC_READONLY;
3003 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3005 const struct elf_backend_data *bed;
3007 switch (hdr->p_type)
3010 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3013 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
3016 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3019 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3022 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3024 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3030 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3033 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3035 case PT_GNU_EH_FRAME:
3036 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3040 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3043 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3046 /* Check for any processor-specific program segment types. */
3047 bed = get_elf_backend_data (abfd);
3048 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3052 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3056 _bfd_elf_single_rel_hdr (asection *sec)
3058 if (elf_section_data (sec)->rel.hdr)
3060 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3061 return elf_section_data (sec)->rel.hdr;
3064 return elf_section_data (sec)->rela.hdr;
3068 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3069 Elf_Internal_Shdr *rel_hdr,
3070 const char *sec_name,
3071 bfd_boolean use_rela_p)
3073 char *name = (char *) bfd_alloc (abfd,
3074 sizeof ".rela" + strlen (sec_name));
3078 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3080 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3082 if (rel_hdr->sh_name == (unsigned int) -1)
3088 /* Allocate and initialize a section-header for a new reloc section,
3089 containing relocations against ASECT. It is stored in RELDATA. If
3090 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3094 _bfd_elf_init_reloc_shdr (bfd *abfd,
3095 struct bfd_elf_section_reloc_data *reldata,
3096 const char *sec_name,
3097 bfd_boolean use_rela_p,
3098 bfd_boolean delay_st_name_p)
3100 Elf_Internal_Shdr *rel_hdr;
3101 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3103 BFD_ASSERT (reldata->hdr == NULL);
3104 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3105 reldata->hdr = rel_hdr;
3107 if (delay_st_name_p)
3108 rel_hdr->sh_name = (unsigned int) -1;
3109 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3112 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3113 rel_hdr->sh_entsize = (use_rela_p
3114 ? bed->s->sizeof_rela
3115 : bed->s->sizeof_rel);
3116 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3117 rel_hdr->sh_flags = 0;
3118 rel_hdr->sh_addr = 0;
3119 rel_hdr->sh_size = 0;
3120 rel_hdr->sh_offset = 0;
3125 /* Return the default section type based on the passed in section flags. */
3128 bfd_elf_get_default_section_type (flagword flags)
3130 if ((flags & SEC_ALLOC) != 0
3131 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3133 return SHT_PROGBITS;
3136 struct fake_section_arg
3138 struct bfd_link_info *link_info;
3142 /* Set up an ELF internal section header for a section. */
3145 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3147 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3149 struct bfd_elf_section_data *esd = elf_section_data (asect);
3150 Elf_Internal_Shdr *this_hdr;
3151 unsigned int sh_type;
3152 const char *name = asect->name;
3153 bfd_boolean delay_st_name_p = FALSE;
3157 /* We already failed; just get out of the bfd_map_over_sections
3162 this_hdr = &esd->this_hdr;
3166 /* ld: compress DWARF debug sections with names: .debug_*. */
3167 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3168 && (asect->flags & SEC_DEBUGGING)
3172 /* Set SEC_ELF_COMPRESS to indicate this section should be
3174 asect->flags |= SEC_ELF_COMPRESS;
3176 /* If this section will be compressed, delay adding section
3177 name to section name section after it is compressed in
3178 _bfd_elf_assign_file_positions_for_non_load. */
3179 delay_st_name_p = TRUE;
3182 else if ((asect->flags & SEC_ELF_RENAME))
3184 /* objcopy: rename output DWARF debug section. */
3185 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3187 /* When we decompress or compress with SHF_COMPRESSED,
3188 convert section name from .zdebug_* to .debug_* if
3192 char *new_name = convert_zdebug_to_debug (abfd, name);
3193 if (new_name == NULL)
3201 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3203 /* PR binutils/18087: Compression does not always make a
3204 section smaller. So only rename the section when
3205 compression has actually taken place. If input section
3206 name is .zdebug_*, we should never compress it again. */
3207 char *new_name = convert_debug_to_zdebug (abfd, name);
3208 if (new_name == NULL)
3213 BFD_ASSERT (name[1] != 'z');
3218 if (delay_st_name_p)
3219 this_hdr->sh_name = (unsigned int) -1;
3223 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3225 if (this_hdr->sh_name == (unsigned int) -1)
3232 /* Don't clear sh_flags. Assembler may set additional bits. */
3234 if ((asect->flags & SEC_ALLOC) != 0
3235 || asect->user_set_vma)
3236 this_hdr->sh_addr = asect->vma;
3238 this_hdr->sh_addr = 0;
3240 this_hdr->sh_offset = 0;
3241 this_hdr->sh_size = asect->size;
3242 this_hdr->sh_link = 0;
3243 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3244 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3247 /* xgettext:c-format */
3248 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3249 abfd, asect->alignment_power, asect);
3253 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
3254 /* The sh_entsize and sh_info fields may have been set already by
3255 copy_private_section_data. */
3257 this_hdr->bfd_section = asect;
3258 this_hdr->contents = NULL;
3260 /* If the section type is unspecified, we set it based on
3262 if ((asect->flags & SEC_GROUP) != 0)
3263 sh_type = SHT_GROUP;
3265 sh_type = bfd_elf_get_default_section_type (asect->flags);
3267 if (this_hdr->sh_type == SHT_NULL)
3268 this_hdr->sh_type = sh_type;
3269 else if (this_hdr->sh_type == SHT_NOBITS
3270 && sh_type == SHT_PROGBITS
3271 && (asect->flags & SEC_ALLOC) != 0)
3273 /* Warn if we are changing a NOBITS section to PROGBITS, but
3274 allow the link to proceed. This can happen when users link
3275 non-bss input sections to bss output sections, or emit data
3276 to a bss output section via a linker script. */
3278 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3279 this_hdr->sh_type = sh_type;
3282 switch (this_hdr->sh_type)
3293 case SHT_INIT_ARRAY:
3294 case SHT_FINI_ARRAY:
3295 case SHT_PREINIT_ARRAY:
3296 this_hdr->sh_entsize = bed->s->arch_size / 8;
3300 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3304 this_hdr->sh_entsize = bed->s->sizeof_sym;
3308 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3312 if (get_elf_backend_data (abfd)->may_use_rela_p)
3313 this_hdr->sh_entsize = bed->s->sizeof_rela;
3317 if (get_elf_backend_data (abfd)->may_use_rel_p)
3318 this_hdr->sh_entsize = bed->s->sizeof_rel;
3321 case SHT_GNU_versym:
3322 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3325 case SHT_GNU_verdef:
3326 this_hdr->sh_entsize = 0;
3327 /* objcopy or strip will copy over sh_info, but may not set
3328 cverdefs. The linker will set cverdefs, but sh_info will be
3330 if (this_hdr->sh_info == 0)
3331 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3333 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3334 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3337 case SHT_GNU_verneed:
3338 this_hdr->sh_entsize = 0;
3339 /* objcopy or strip will copy over sh_info, but may not set
3340 cverrefs. The linker will set cverrefs, but sh_info will be
3342 if (this_hdr->sh_info == 0)
3343 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3345 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3346 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3350 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3354 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3358 if ((asect->flags & SEC_ALLOC) != 0)
3359 this_hdr->sh_flags |= SHF_ALLOC;
3360 if ((asect->flags & SEC_READONLY) == 0)
3361 this_hdr->sh_flags |= SHF_WRITE;
3362 if ((asect->flags & SEC_CODE) != 0)
3363 this_hdr->sh_flags |= SHF_EXECINSTR;
3364 if ((asect->flags & SEC_MERGE) != 0)
3366 this_hdr->sh_flags |= SHF_MERGE;
3367 this_hdr->sh_entsize = asect->entsize;
3369 if ((asect->flags & SEC_STRINGS) != 0)
3370 this_hdr->sh_flags |= SHF_STRINGS;
3371 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3372 this_hdr->sh_flags |= SHF_GROUP;
3373 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3375 this_hdr->sh_flags |= SHF_TLS;
3376 if (asect->size == 0
3377 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3379 struct bfd_link_order *o = asect->map_tail.link_order;
3381 this_hdr->sh_size = 0;
3384 this_hdr->sh_size = o->offset + o->size;
3385 if (this_hdr->sh_size != 0)
3386 this_hdr->sh_type = SHT_NOBITS;
3390 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3391 this_hdr->sh_flags |= SHF_EXCLUDE;
3393 /* If the section has relocs, set up a section header for the
3394 SHT_REL[A] section. If two relocation sections are required for
3395 this section, it is up to the processor-specific back-end to
3396 create the other. */
3397 if ((asect->flags & SEC_RELOC) != 0)
3399 /* When doing a relocatable link, create both REL and RELA sections if
3402 /* Do the normal setup if we wouldn't create any sections here. */
3403 && esd->rel.count + esd->rela.count > 0
3404 && (bfd_link_relocatable (arg->link_info)
3405 || arg->link_info->emitrelocations))
3407 if (esd->rel.count && esd->rel.hdr == NULL
3408 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3409 FALSE, delay_st_name_p))
3414 if (esd->rela.count && esd->rela.hdr == NULL
3415 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3416 TRUE, delay_st_name_p))
3422 else if (!_bfd_elf_init_reloc_shdr (abfd,
3424 ? &esd->rela : &esd->rel),
3434 /* Check for processor-specific section types. */
3435 sh_type = this_hdr->sh_type;
3436 if (bed->elf_backend_fake_sections
3437 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3443 if (sh_type == SHT_NOBITS && asect->size != 0)
3445 /* Don't change the header type from NOBITS if we are being
3446 called for objcopy --only-keep-debug. */
3447 this_hdr->sh_type = sh_type;
3451 /* Fill in the contents of a SHT_GROUP section. Called from
3452 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3453 when ELF targets use the generic linker, ld. Called for ld -r
3454 from bfd_elf_final_link. */
3457 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3459 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3460 asection *elt, *first;
3464 /* Ignore linker created group section. See elfNN_ia64_object_p in
3466 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
3470 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3472 unsigned long symindx = 0;
3474 /* elf_group_id will have been set up by objcopy and the
3476 if (elf_group_id (sec) != NULL)
3477 symindx = elf_group_id (sec)->udata.i;
3481 /* If called from the assembler, swap_out_syms will have set up
3482 elf_section_syms. */
3483 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3484 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3486 elf_section_data (sec)->this_hdr.sh_info = symindx;
3488 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3490 /* The ELF backend linker sets sh_info to -2 when the group
3491 signature symbol is global, and thus the index can't be
3492 set until all local symbols are output. */
3494 struct bfd_elf_section_data *sec_data;
3495 unsigned long symndx;
3496 unsigned long extsymoff;
3497 struct elf_link_hash_entry *h;
3499 /* The point of this little dance to the first SHF_GROUP section
3500 then back to the SHT_GROUP section is that this gets us to
3501 the SHT_GROUP in the input object. */
3502 igroup = elf_sec_group (elf_next_in_group (sec));
3503 sec_data = elf_section_data (igroup);
3504 symndx = sec_data->this_hdr.sh_info;
3506 if (!elf_bad_symtab (igroup->owner))
3508 Elf_Internal_Shdr *symtab_hdr;
3510 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3511 extsymoff = symtab_hdr->sh_info;
3513 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3514 while (h->root.type == bfd_link_hash_indirect
3515 || h->root.type == bfd_link_hash_warning)
3516 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3518 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3521 /* The contents won't be allocated for "ld -r" or objcopy. */
3523 if (sec->contents == NULL)
3526 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3528 /* Arrange for the section to be written out. */
3529 elf_section_data (sec)->this_hdr.contents = sec->contents;
3530 if (sec->contents == NULL)
3537 loc = sec->contents + sec->size;
3539 /* Get the pointer to the first section in the group that gas
3540 squirreled away here. objcopy arranges for this to be set to the
3541 start of the input section group. */
3542 first = elt = elf_next_in_group (sec);
3544 /* First element is a flag word. Rest of section is elf section
3545 indices for all the sections of the group. Write them backwards
3546 just to keep the group in the same order as given in .section
3547 directives, not that it matters. */
3554 s = s->output_section;
3556 && !bfd_is_abs_section (s))
3558 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3559 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3561 if (elf_sec->rel.hdr != NULL
3563 || (input_elf_sec->rel.hdr != NULL
3564 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3566 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3568 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3570 if (elf_sec->rela.hdr != NULL
3572 || (input_elf_sec->rela.hdr != NULL
3573 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3575 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3577 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3580 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3582 elt = elf_next_in_group (elt);
3588 BFD_ASSERT (loc == sec->contents);
3590 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3593 /* Given NAME, the name of a relocation section stripped of its
3594 .rel/.rela prefix, return the section in ABFD to which the
3595 relocations apply. */
3598 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3600 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3601 section likely apply to .got.plt or .got section. */
3602 if (get_elf_backend_data (abfd)->want_got_plt
3603 && strcmp (name, ".plt") == 0)
3608 sec = bfd_get_section_by_name (abfd, name);
3614 return bfd_get_section_by_name (abfd, name);
3617 /* Return the section to which RELOC_SEC applies. */
3620 elf_get_reloc_section (asection *reloc_sec)
3625 const struct elf_backend_data *bed;
3627 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3628 if (type != SHT_REL && type != SHT_RELA)
3631 /* We look up the section the relocs apply to by name. */
3632 name = reloc_sec->name;
3633 if (strncmp (name, ".rel", 4) != 0)
3636 if (type == SHT_RELA && *name++ != 'a')
3639 abfd = reloc_sec->owner;
3640 bed = get_elf_backend_data (abfd);
3641 return bed->get_reloc_section (abfd, name);
3644 /* Assign all ELF section numbers. The dummy first section is handled here
3645 too. The link/info pointers for the standard section types are filled
3646 in here too, while we're at it. */
3649 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3651 struct elf_obj_tdata *t = elf_tdata (abfd);
3653 unsigned int section_number;
3654 Elf_Internal_Shdr **i_shdrp;
3655 struct bfd_elf_section_data *d;
3656 bfd_boolean need_symtab;
3660 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3662 /* SHT_GROUP sections are in relocatable files only. */
3663 if (link_info == NULL || !link_info->resolve_section_groups)
3665 size_t reloc_count = 0;
3667 /* Put SHT_GROUP sections first. */
3668 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3670 d = elf_section_data (sec);
3672 if (d->this_hdr.sh_type == SHT_GROUP)
3674 if (sec->flags & SEC_LINKER_CREATED)
3676 /* Remove the linker created SHT_GROUP sections. */
3677 bfd_section_list_remove (abfd, sec);
3678 abfd->section_count--;
3681 d->this_idx = section_number++;
3684 /* Count relocations. */
3685 reloc_count += sec->reloc_count;
3688 /* Clear HAS_RELOC if there are no relocations. */
3689 if (reloc_count == 0)
3690 abfd->flags &= ~HAS_RELOC;
3693 for (sec = abfd->sections; sec; sec = sec->next)
3695 d = elf_section_data (sec);
3697 if (d->this_hdr.sh_type != SHT_GROUP)
3698 d->this_idx = section_number++;
3699 if (d->this_hdr.sh_name != (unsigned int) -1)
3700 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3703 d->rel.idx = section_number++;
3704 if (d->rel.hdr->sh_name != (unsigned int) -1)
3705 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3712 d->rela.idx = section_number++;
3713 if (d->rela.hdr->sh_name != (unsigned int) -1)
3714 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3720 need_symtab = (bfd_get_symcount (abfd) > 0
3721 || (link_info == NULL
3722 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3726 elf_onesymtab (abfd) = section_number++;
3727 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3728 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3730 elf_section_list * entry;
3732 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3734 entry = bfd_zalloc (abfd, sizeof * entry);
3735 entry->ndx = section_number++;
3736 elf_symtab_shndx_list (abfd) = entry;
3738 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3739 ".symtab_shndx", FALSE);
3740 if (entry->hdr.sh_name == (unsigned int) -1)
3743 elf_strtab_sec (abfd) = section_number++;
3744 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3747 elf_shstrtab_sec (abfd) = section_number++;
3748 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3749 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3751 if (section_number >= SHN_LORESERVE)
3753 /* xgettext:c-format */
3754 _bfd_error_handler (_("%pB: too many sections: %u"),
3755 abfd, section_number);
3759 elf_numsections (abfd) = section_number;
3760 elf_elfheader (abfd)->e_shnum = section_number;
3762 /* Set up the list of section header pointers, in agreement with the
3764 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3765 sizeof (Elf_Internal_Shdr *));
3766 if (i_shdrp == NULL)
3769 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3770 sizeof (Elf_Internal_Shdr));
3771 if (i_shdrp[0] == NULL)
3773 bfd_release (abfd, i_shdrp);
3777 elf_elfsections (abfd) = i_shdrp;
3779 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3782 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3783 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3785 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3786 BFD_ASSERT (entry != NULL);
3787 i_shdrp[entry->ndx] = & entry->hdr;
3788 entry->hdr.sh_link = elf_onesymtab (abfd);
3790 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3791 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3794 for (sec = abfd->sections; sec; sec = sec->next)
3798 d = elf_section_data (sec);
3800 i_shdrp[d->this_idx] = &d->this_hdr;
3801 if (d->rel.idx != 0)
3802 i_shdrp[d->rel.idx] = d->rel.hdr;
3803 if (d->rela.idx != 0)
3804 i_shdrp[d->rela.idx] = d->rela.hdr;
3806 /* Fill in the sh_link and sh_info fields while we're at it. */
3808 /* sh_link of a reloc section is the section index of the symbol
3809 table. sh_info is the section index of the section to which
3810 the relocation entries apply. */
3811 if (d->rel.idx != 0)
3813 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3814 d->rel.hdr->sh_info = d->this_idx;
3815 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3817 if (d->rela.idx != 0)
3819 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3820 d->rela.hdr->sh_info = d->this_idx;
3821 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3824 /* We need to set up sh_link for SHF_LINK_ORDER. */
3825 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3827 s = elf_linked_to_section (sec);
3830 /* elf_linked_to_section points to the input section. */
3831 if (link_info != NULL)
3833 /* Check discarded linkonce section. */
3834 if (discarded_section (s))
3838 /* xgettext:c-format */
3839 (_("%pB: sh_link of section `%pA' points to"
3840 " discarded section `%pA' of `%pB'"),
3841 abfd, d->this_hdr.bfd_section,
3843 /* Point to the kept section if it has the same
3844 size as the discarded one. */
3845 kept = _bfd_elf_check_kept_section (s, link_info);
3848 bfd_set_error (bfd_error_bad_value);
3854 s = s->output_section;
3855 BFD_ASSERT (s != NULL);
3859 /* Handle objcopy. */
3860 if (s->output_section == NULL)
3863 /* xgettext:c-format */
3864 (_("%pB: sh_link of section `%pA' points to"
3865 " removed section `%pA' of `%pB'"),
3866 abfd, d->this_hdr.bfd_section, s, s->owner);
3867 bfd_set_error (bfd_error_bad_value);
3870 s = s->output_section;
3872 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3877 The Intel C compiler generates SHT_IA_64_UNWIND with
3878 SHF_LINK_ORDER. But it doesn't set the sh_link or
3879 sh_info fields. Hence we could get the situation
3881 const struct elf_backend_data *bed
3882 = get_elf_backend_data (abfd);
3883 if (bed->link_order_error_handler)
3884 bed->link_order_error_handler
3885 /* xgettext:c-format */
3886 (_("%pB: warning: sh_link not set for section `%pA'"),
3891 switch (d->this_hdr.sh_type)
3895 /* A reloc section which we are treating as a normal BFD
3896 section. sh_link is the section index of the symbol
3897 table. sh_info is the section index of the section to
3898 which the relocation entries apply. We assume that an
3899 allocated reloc section uses the dynamic symbol table.
3900 FIXME: How can we be sure? */
3901 s = bfd_get_section_by_name (abfd, ".dynsym");
3903 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3905 s = elf_get_reloc_section (sec);
3908 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3909 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3914 /* We assume that a section named .stab*str is a stabs
3915 string section. We look for a section with the same name
3916 but without the trailing ``str'', and set its sh_link
3917 field to point to this section. */
3918 if (CONST_STRNEQ (sec->name, ".stab")
3919 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3924 len = strlen (sec->name);
3925 alc = (char *) bfd_malloc (len - 2);
3928 memcpy (alc, sec->name, len - 3);
3929 alc[len - 3] = '\0';
3930 s = bfd_get_section_by_name (abfd, alc);
3934 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3936 /* This is a .stab section. */
3937 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3938 elf_section_data (s)->this_hdr.sh_entsize
3939 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3946 case SHT_GNU_verneed:
3947 case SHT_GNU_verdef:
3948 /* sh_link is the section header index of the string table
3949 used for the dynamic entries, or the symbol table, or the
3951 s = bfd_get_section_by_name (abfd, ".dynstr");
3953 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3956 case SHT_GNU_LIBLIST:
3957 /* sh_link is the section header index of the prelink library
3958 list used for the dynamic entries, or the symbol table, or
3959 the version strings. */
3960 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3961 ? ".dynstr" : ".gnu.libstr");
3963 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3968 case SHT_GNU_versym:
3969 /* sh_link is the section header index of the symbol table
3970 this hash table or version table is for. */
3971 s = bfd_get_section_by_name (abfd, ".dynsym");
3973 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3977 d->this_hdr.sh_link = elf_onesymtab (abfd);
3981 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3982 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3983 debug section name from .debug_* to .zdebug_* if needed. */
3989 sym_is_global (bfd *abfd, asymbol *sym)
3991 /* If the backend has a special mapping, use it. */
3992 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3993 if (bed->elf_backend_sym_is_global)
3994 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3996 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3997 || bfd_is_und_section (bfd_get_section (sym))
3998 || bfd_is_com_section (bfd_get_section (sym)));
4001 /* Filter global symbols of ABFD to include in the import library. All
4002 SYMCOUNT symbols of ABFD can be examined from their pointers in
4003 SYMS. Pointers of symbols to keep should be stored contiguously at
4004 the beginning of that array.
4006 Returns the number of symbols to keep. */
4009 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4010 asymbol **syms, long symcount)
4012 long src_count, dst_count = 0;
4014 for (src_count = 0; src_count < symcount; src_count++)
4016 asymbol *sym = syms[src_count];
4017 char *name = (char *) bfd_asymbol_name (sym);
4018 struct bfd_link_hash_entry *h;
4020 if (!sym_is_global (abfd, sym))
4023 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
4026 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4028 if (h->linker_def || h->ldscript_def)
4031 syms[dst_count++] = sym;
4034 syms[dst_count] = NULL;
4039 /* Don't output section symbols for sections that are not going to be
4040 output, that are duplicates or there is no BFD section. */
4043 ignore_section_sym (bfd *abfd, asymbol *sym)
4045 elf_symbol_type *type_ptr;
4050 if ((sym->flags & BSF_SECTION_SYM) == 0)
4053 if (sym->section == NULL)
4056 type_ptr = elf_symbol_from (abfd, sym);
4057 return ((type_ptr != NULL
4058 && type_ptr->internal_elf_sym.st_shndx != 0
4059 && bfd_is_abs_section (sym->section))
4060 || !(sym->section->owner == abfd
4061 || (sym->section->output_section != NULL
4062 && sym->section->output_section->owner == abfd
4063 && sym->section->output_offset == 0)
4064 || bfd_is_abs_section (sym->section)));
4067 /* Map symbol from it's internal number to the external number, moving
4068 all local symbols to be at the head of the list. */
4071 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4073 unsigned int symcount = bfd_get_symcount (abfd);
4074 asymbol **syms = bfd_get_outsymbols (abfd);
4075 asymbol **sect_syms;
4076 unsigned int num_locals = 0;
4077 unsigned int num_globals = 0;
4078 unsigned int num_locals2 = 0;
4079 unsigned int num_globals2 = 0;
4080 unsigned int max_index = 0;
4086 fprintf (stderr, "elf_map_symbols\n");
4090 for (asect = abfd->sections; asect; asect = asect->next)
4092 if (max_index < asect->index)
4093 max_index = asect->index;
4097 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
4098 if (sect_syms == NULL)
4100 elf_section_syms (abfd) = sect_syms;
4101 elf_num_section_syms (abfd) = max_index;
4103 /* Init sect_syms entries for any section symbols we have already
4104 decided to output. */
4105 for (idx = 0; idx < symcount; idx++)
4107 asymbol *sym = syms[idx];
4109 if ((sym->flags & BSF_SECTION_SYM) != 0
4111 && !ignore_section_sym (abfd, sym)
4112 && !bfd_is_abs_section (sym->section))
4114 asection *sec = sym->section;
4116 if (sec->owner != abfd)
4117 sec = sec->output_section;
4119 sect_syms[sec->index] = syms[idx];
4123 /* Classify all of the symbols. */
4124 for (idx = 0; idx < symcount; idx++)
4126 if (sym_is_global (abfd, syms[idx]))
4128 else if (!ignore_section_sym (abfd, syms[idx]))
4132 /* We will be adding a section symbol for each normal BFD section. Most
4133 sections will already have a section symbol in outsymbols, but
4134 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4135 at least in that case. */
4136 for (asect = abfd->sections; asect; asect = asect->next)
4138 if (sect_syms[asect->index] == NULL)
4140 if (!sym_is_global (abfd, asect->symbol))
4147 /* Now sort the symbols so the local symbols are first. */
4148 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
4149 sizeof (asymbol *));
4151 if (new_syms == NULL)
4154 for (idx = 0; idx < symcount; idx++)
4156 asymbol *sym = syms[idx];
4159 if (sym_is_global (abfd, sym))
4160 i = num_locals + num_globals2++;
4161 else if (!ignore_section_sym (abfd, sym))
4166 sym->udata.i = i + 1;
4168 for (asect = abfd->sections; asect; asect = asect->next)
4170 if (sect_syms[asect->index] == NULL)
4172 asymbol *sym = asect->symbol;
4175 sect_syms[asect->index] = sym;
4176 if (!sym_is_global (abfd, sym))
4179 i = num_locals + num_globals2++;
4181 sym->udata.i = i + 1;
4185 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4187 *pnum_locals = num_locals;
4191 /* Align to the maximum file alignment that could be required for any
4192 ELF data structure. */
4194 static inline file_ptr
4195 align_file_position (file_ptr off, int align)
4197 return (off + align - 1) & ~(align - 1);
4200 /* Assign a file position to a section, optionally aligning to the
4201 required section alignment. */
4204 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4208 if (align && i_shdrp->sh_addralign > 1)
4209 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4210 i_shdrp->sh_offset = offset;
4211 if (i_shdrp->bfd_section != NULL)
4212 i_shdrp->bfd_section->filepos = offset;
4213 if (i_shdrp->sh_type != SHT_NOBITS)
4214 offset += i_shdrp->sh_size;
4218 /* Compute the file positions we are going to put the sections at, and
4219 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4220 is not NULL, this is being called by the ELF backend linker. */
4223 _bfd_elf_compute_section_file_positions (bfd *abfd,
4224 struct bfd_link_info *link_info)
4226 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4227 struct fake_section_arg fsargs;
4229 struct elf_strtab_hash *strtab = NULL;
4230 Elf_Internal_Shdr *shstrtab_hdr;
4231 bfd_boolean need_symtab;
4233 if (abfd->output_has_begun)
4236 /* Do any elf backend specific processing first. */
4237 if (bed->elf_backend_begin_write_processing)
4238 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4240 if (! prep_headers (abfd))
4243 /* Post process the headers if necessary. */
4244 (*bed->elf_backend_post_process_headers) (abfd, link_info);
4246 fsargs.failed = FALSE;
4247 fsargs.link_info = link_info;
4248 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4252 if (!assign_section_numbers (abfd, link_info))
4255 /* The backend linker builds symbol table information itself. */
4256 need_symtab = (link_info == NULL
4257 && (bfd_get_symcount (abfd) > 0
4258 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4262 /* Non-zero if doing a relocatable link. */
4263 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4265 if (! swap_out_syms (abfd, &strtab, relocatable_p))
4270 if (link_info == NULL)
4272 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4277 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4278 /* sh_name was set in prep_headers. */
4279 shstrtab_hdr->sh_type = SHT_STRTAB;
4280 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4281 shstrtab_hdr->sh_addr = 0;
4282 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4283 shstrtab_hdr->sh_entsize = 0;
4284 shstrtab_hdr->sh_link = 0;
4285 shstrtab_hdr->sh_info = 0;
4286 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4287 shstrtab_hdr->sh_addralign = 1;
4289 if (!assign_file_positions_except_relocs (abfd, link_info))
4295 Elf_Internal_Shdr *hdr;
4297 off = elf_next_file_pos (abfd);
4299 hdr = & elf_symtab_hdr (abfd);
4300 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4302 if (elf_symtab_shndx_list (abfd) != NULL)
4304 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4305 if (hdr->sh_size != 0)
4306 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4307 /* FIXME: What about other symtab_shndx sections in the list ? */
4310 hdr = &elf_tdata (abfd)->strtab_hdr;
4311 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4313 elf_next_file_pos (abfd) = off;
4315 /* Now that we know where the .strtab section goes, write it
4317 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4318 || ! _bfd_elf_strtab_emit (abfd, strtab))
4320 _bfd_elf_strtab_free (strtab);
4323 abfd->output_has_begun = TRUE;
4328 /* Make an initial estimate of the size of the program header. If we
4329 get the number wrong here, we'll redo section placement. */
4331 static bfd_size_type
4332 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4336 const struct elf_backend_data *bed;
4338 /* Assume we will need exactly two PT_LOAD segments: one for text
4339 and one for data. */
4342 s = bfd_get_section_by_name (abfd, ".interp");
4343 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4345 /* If we have a loadable interpreter section, we need a
4346 PT_INTERP segment. In this case, assume we also need a
4347 PT_PHDR segment, although that may not be true for all
4352 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4354 /* We need a PT_DYNAMIC segment. */
4358 if (info != NULL && info->relro)
4360 /* We need a PT_GNU_RELRO segment. */
4364 if (elf_eh_frame_hdr (abfd))
4366 /* We need a PT_GNU_EH_FRAME segment. */
4370 if (elf_stack_flags (abfd))
4372 /* We need a PT_GNU_STACK segment. */
4376 s = bfd_get_section_by_name (abfd,
4377 NOTE_GNU_PROPERTY_SECTION_NAME);
4378 if (s != NULL && s->size != 0)
4380 /* We need a PT_GNU_PROPERTY segment. */
4384 for (s = abfd->sections; s != NULL; s = s->next)
4386 if ((s->flags & SEC_LOAD) != 0
4387 && elf_section_type (s) == SHT_NOTE)
4389 unsigned int alignment_power;
4390 /* We need a PT_NOTE segment. */
4392 /* Try to create just one PT_NOTE segment for all adjacent
4393 loadable SHT_NOTE sections. gABI requires that within a
4394 PT_NOTE segment (and also inside of each SHT_NOTE section)
4395 each note should have the same alignment. So we check
4396 whether the sections are correctly aligned. */
4397 alignment_power = s->alignment_power;
4398 while (s->next != NULL
4399 && s->next->alignment_power == alignment_power
4400 && (s->next->flags & SEC_LOAD) != 0
4401 && elf_section_type (s->next) == SHT_NOTE)
4406 for (s = abfd->sections; s != NULL; s = s->next)
4408 if (s->flags & SEC_THREAD_LOCAL)
4410 /* We need a PT_TLS segment. */
4416 bed = get_elf_backend_data (abfd);
4418 if ((abfd->flags & D_PAGED) != 0)
4420 /* Add a PT_GNU_MBIND segment for each mbind section. */
4421 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4422 for (s = abfd->sections; s != NULL; s = s->next)
4423 if (elf_section_flags (s) & SHF_GNU_MBIND)
4425 if (elf_section_data (s)->this_hdr.sh_info
4429 /* xgettext:c-format */
4430 (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"),
4431 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4434 /* Align mbind section to page size. */
4435 if (s->alignment_power < page_align_power)
4436 s->alignment_power = page_align_power;
4441 /* Let the backend count up any program headers it might need. */
4442 if (bed->elf_backend_additional_program_headers)
4446 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4452 return segs * bed->s->sizeof_phdr;
4455 /* Find the segment that contains the output_section of section. */
4458 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4460 struct elf_segment_map *m;
4461 Elf_Internal_Phdr *p;
4463 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4469 for (i = m->count - 1; i >= 0; i--)
4470 if (m->sections[i] == section)
4477 /* Create a mapping from a set of sections to a program segment. */
4479 static struct elf_segment_map *
4480 make_mapping (bfd *abfd,
4481 asection **sections,
4486 struct elf_segment_map *m;
4491 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4492 amt += (to - from) * sizeof (asection *);
4493 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4497 m->p_type = PT_LOAD;
4498 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4499 m->sections[i - from] = *hdrpp;
4500 m->count = to - from;
4502 if (from == 0 && phdr)
4504 /* Include the headers in the first PT_LOAD segment. */
4505 m->includes_filehdr = 1;
4506 m->includes_phdrs = 1;
4512 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4515 struct elf_segment_map *
4516 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4518 struct elf_segment_map *m;
4520 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4521 sizeof (struct elf_segment_map));
4525 m->p_type = PT_DYNAMIC;
4527 m->sections[0] = dynsec;
4532 /* Possibly add or remove segments from the segment map. */
4535 elf_modify_segment_map (bfd *abfd,
4536 struct bfd_link_info *info,
4537 bfd_boolean remove_empty_load)
4539 struct elf_segment_map **m;
4540 const struct elf_backend_data *bed;
4542 /* The placement algorithm assumes that non allocated sections are
4543 not in PT_LOAD segments. We ensure this here by removing such
4544 sections from the segment map. We also remove excluded
4545 sections. Finally, any PT_LOAD segment without sections is
4547 m = &elf_seg_map (abfd);
4550 unsigned int i, new_count;
4552 for (new_count = 0, i = 0; i < (*m)->count; i++)
4554 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4555 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4556 || (*m)->p_type != PT_LOAD))
4558 (*m)->sections[new_count] = (*m)->sections[i];
4562 (*m)->count = new_count;
4564 if (remove_empty_load
4565 && (*m)->p_type == PT_LOAD
4567 && !(*m)->includes_phdrs)
4573 bed = get_elf_backend_data (abfd);
4574 if (bed->elf_backend_modify_segment_map != NULL)
4576 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4583 #define IS_TBSS(s) \
4584 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4586 /* Set up a mapping from BFD sections to program segments. */
4589 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4592 struct elf_segment_map *m;
4593 asection **sections = NULL;
4594 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4595 bfd_boolean no_user_phdrs;
4597 no_user_phdrs = elf_seg_map (abfd) == NULL;
4600 info->user_phdrs = !no_user_phdrs;
4602 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4606 struct elf_segment_map *mfirst;
4607 struct elf_segment_map **pm;
4610 unsigned int hdr_index;
4611 bfd_vma maxpagesize;
4613 bfd_boolean phdr_in_segment;
4614 bfd_boolean writable;
4615 bfd_boolean executable;
4617 asection *first_tls = NULL;
4618 asection *first_mbind = NULL;
4619 asection *dynsec, *eh_frame_hdr;
4621 bfd_vma addr_mask, wrap_to = 0;
4622 bfd_size_type phdr_size;
4624 /* Select the allocated sections, and sort them. */
4626 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4627 sizeof (asection *));
4628 if (sections == NULL)
4631 /* Calculate top address, avoiding undefined behaviour of shift
4632 left operator when shift count is equal to size of type
4634 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4635 addr_mask = (addr_mask << 1) + 1;
4638 for (s = abfd->sections; s != NULL; s = s->next)
4640 if ((s->flags & SEC_ALLOC) != 0)
4644 /* A wrapping section potentially clashes with header. */
4645 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4646 wrap_to = (s->lma + s->size) & addr_mask;
4649 BFD_ASSERT (i <= bfd_count_sections (abfd));
4652 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4654 phdr_size = elf_program_header_size (abfd);
4655 if (phdr_size == (bfd_size_type) -1)
4656 phdr_size = get_program_header_size (abfd, info);
4657 phdr_size += bed->s->sizeof_ehdr;
4658 maxpagesize = bed->maxpagesize;
4659 if (maxpagesize == 0)
4661 phdr_in_segment = info != NULL && info->load_phdrs;
4663 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4664 >= (phdr_size & (maxpagesize - 1))))
4665 /* For compatibility with old scripts that may not be using
4666 SIZEOF_HEADERS, add headers when it looks like space has
4667 been left for them. */
4668 phdr_in_segment = TRUE;
4670 /* Build the mapping. */
4674 /* If we have a .interp section, then create a PT_PHDR segment for
4675 the program headers and a PT_INTERP segment for the .interp
4677 s = bfd_get_section_by_name (abfd, ".interp");
4678 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4680 amt = sizeof (struct elf_segment_map);
4681 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4685 m->p_type = PT_PHDR;
4687 m->p_flags_valid = 1;
4688 m->includes_phdrs = 1;
4689 phdr_in_segment = TRUE;
4693 amt = sizeof (struct elf_segment_map);
4694 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4698 m->p_type = PT_INTERP;
4706 /* Look through the sections. We put sections in the same program
4707 segment when the start of the second section can be placed within
4708 a few bytes of the end of the first section. */
4714 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4716 && (dynsec->flags & SEC_LOAD) == 0)
4719 if ((abfd->flags & D_PAGED) == 0)
4720 phdr_in_segment = FALSE;
4722 /* Deal with -Ttext or something similar such that the first section
4723 is not adjacent to the program headers. This is an
4724 approximation, since at this point we don't know exactly how many
4725 program headers we will need. */
4726 if (phdr_in_segment && count > 0)
4729 bfd_boolean separate_phdr = FALSE;
4731 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4733 && info->separate_code
4734 && (sections[0]->flags & SEC_CODE) != 0)
4736 /* If data sections should be separate from code and
4737 thus not executable, and the first section is
4738 executable then put the file and program headers in
4739 their own PT_LOAD. */
4740 separate_phdr = TRUE;
4741 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4742 == (sections[0]->lma & addr_mask & -maxpagesize)))
4744 /* The file and program headers are currently on the
4745 same page as the first section. Put them on the
4746 previous page if we can. */
4747 if (phdr_lma >= maxpagesize)
4748 phdr_lma -= maxpagesize;
4750 separate_phdr = FALSE;
4753 if ((sections[0]->lma & addr_mask) < phdr_lma
4754 || (sections[0]->lma & addr_mask) < phdr_size)
4755 /* If file and program headers would be placed at the end
4756 of memory then it's probably better to omit them. */
4757 phdr_in_segment = FALSE;
4758 else if (phdr_lma < wrap_to)
4759 /* If a section wraps around to where we'll be placing
4760 file and program headers, then the headers will be
4762 phdr_in_segment = FALSE;
4763 else if (separate_phdr)
4765 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4768 m->p_paddr = phdr_lma;
4770 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4771 m->p_paddr_valid = 1;
4774 phdr_in_segment = FALSE;
4778 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4781 bfd_boolean new_segment;
4785 /* See if this section and the last one will fit in the same
4788 if (last_hdr == NULL)
4790 /* If we don't have a segment yet, then we don't need a new
4791 one (we build the last one after this loop). */
4792 new_segment = FALSE;
4794 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4796 /* If this section has a different relation between the
4797 virtual address and the load address, then we need a new
4801 else if (hdr->lma < last_hdr->lma + last_size
4802 || last_hdr->lma + last_size < last_hdr->lma)
4804 /* If this section has a load address that makes it overlap
4805 the previous section, then we need a new segment. */
4808 else if ((abfd->flags & D_PAGED) != 0
4809 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4810 == (hdr->lma & -maxpagesize)))
4812 /* If we are demand paged then we can't map two disk
4813 pages onto the same memory page. */
4814 new_segment = FALSE;
4816 /* In the next test we have to be careful when last_hdr->lma is close
4817 to the end of the address space. If the aligned address wraps
4818 around to the start of the address space, then there are no more
4819 pages left in memory and it is OK to assume that the current
4820 section can be included in the current segment. */
4821 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4822 + maxpagesize > last_hdr->lma)
4823 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4824 + maxpagesize <= hdr->lma))
4826 /* If putting this section in this segment would force us to
4827 skip a page in the segment, then we need a new segment. */
4830 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4831 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4833 /* We don't want to put a loaded section after a
4834 nonloaded (ie. bss style) section in the same segment
4835 as that will force the non-loaded section to be loaded.
4836 Consider .tbss sections as loaded for this purpose. */
4839 else if ((abfd->flags & D_PAGED) == 0)
4841 /* If the file is not demand paged, which means that we
4842 don't require the sections to be correctly aligned in the
4843 file, then there is no other reason for a new segment. */
4844 new_segment = FALSE;
4846 else if (info != NULL
4847 && info->separate_code
4848 && executable != ((hdr->flags & SEC_CODE) != 0))
4853 && (hdr->flags & SEC_READONLY) == 0)
4855 /* We don't want to put a writable section in a read only
4861 /* Otherwise, we can use the same segment. */
4862 new_segment = FALSE;
4865 /* Allow interested parties a chance to override our decision. */
4866 if (last_hdr != NULL
4868 && info->callbacks->override_segment_assignment != NULL)
4870 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4876 if ((hdr->flags & SEC_READONLY) == 0)
4878 if ((hdr->flags & SEC_CODE) != 0)
4881 /* .tbss sections effectively have zero size. */
4882 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4886 /* We need a new program segment. We must create a new program
4887 header holding all the sections from hdr_index until hdr. */
4889 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4896 if ((hdr->flags & SEC_READONLY) == 0)
4901 if ((hdr->flags & SEC_CODE) == 0)
4907 /* .tbss sections effectively have zero size. */
4908 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
4910 phdr_in_segment = FALSE;
4913 /* Create a final PT_LOAD program segment, but not if it's just
4915 if (last_hdr != NULL
4916 && (i - hdr_index != 1
4917 || !IS_TBSS (last_hdr)))
4919 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4927 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4930 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4937 /* For each batch of consecutive loadable SHT_NOTE sections,
4938 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4939 because if we link together nonloadable .note sections and
4940 loadable .note sections, we will generate two .note sections
4941 in the output file. */
4942 for (s = abfd->sections; s != NULL; s = s->next)
4944 if ((s->flags & SEC_LOAD) != 0
4945 && elf_section_type (s) == SHT_NOTE)
4948 unsigned int alignment_power = s->alignment_power;
4951 for (s2 = s; s2->next != NULL; s2 = s2->next)
4953 if (s2->next->alignment_power == alignment_power
4954 && (s2->next->flags & SEC_LOAD) != 0
4955 && elf_section_type (s2->next) == SHT_NOTE
4956 && align_power (s2->lma + s2->size,
4963 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4964 amt += count * sizeof (asection *);
4965 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4969 m->p_type = PT_NOTE;
4973 m->sections[m->count - count--] = s;
4974 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4977 m->sections[m->count - 1] = s;
4978 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4982 if (s->flags & SEC_THREAD_LOCAL)
4988 if (first_mbind == NULL
4989 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
4993 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4996 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4997 amt += tls_count * sizeof (asection *);
4998 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5003 m->count = tls_count;
5004 /* Mandated PF_R. */
5006 m->p_flags_valid = 1;
5008 for (i = 0; i < (unsigned int) tls_count; ++i)
5010 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5013 (_("%pB: TLS sections are not adjacent:"), abfd);
5016 while (i < (unsigned int) tls_count)
5018 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5020 _bfd_error_handler (_(" TLS: %pA"), s);
5024 _bfd_error_handler (_(" non-TLS: %pA"), s);
5027 bfd_set_error (bfd_error_bad_value);
5038 if (first_mbind && (abfd->flags & D_PAGED) != 0)
5039 for (s = first_mbind; s != NULL; s = s->next)
5040 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5041 && (elf_section_data (s)->this_hdr.sh_info
5042 <= PT_GNU_MBIND_NUM))
5044 /* Mandated PF_R. */
5045 unsigned long p_flags = PF_R;
5046 if ((s->flags & SEC_READONLY) == 0)
5048 if ((s->flags & SEC_CODE) != 0)
5051 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5052 m = bfd_zalloc (abfd, amt);
5056 m->p_type = (PT_GNU_MBIND_LO
5057 + elf_section_data (s)->this_hdr.sh_info);
5059 m->p_flags_valid = 1;
5061 m->p_flags = p_flags;
5067 s = bfd_get_section_by_name (abfd,
5068 NOTE_GNU_PROPERTY_SECTION_NAME);
5069 if (s != NULL && s->size != 0)
5071 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5072 m = bfd_zalloc (abfd, amt);
5076 m->p_type = PT_GNU_PROPERTY;
5078 m->p_flags_valid = 1;
5085 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5087 eh_frame_hdr = elf_eh_frame_hdr (abfd);
5088 if (eh_frame_hdr != NULL
5089 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5091 amt = sizeof (struct elf_segment_map);
5092 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5096 m->p_type = PT_GNU_EH_FRAME;
5098 m->sections[0] = eh_frame_hdr->output_section;
5104 if (elf_stack_flags (abfd))
5106 amt = sizeof (struct elf_segment_map);
5107 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5111 m->p_type = PT_GNU_STACK;
5112 m->p_flags = elf_stack_flags (abfd);
5113 m->p_align = bed->stack_align;
5114 m->p_flags_valid = 1;
5115 m->p_align_valid = m->p_align != 0;
5116 if (info->stacksize > 0)
5118 m->p_size = info->stacksize;
5119 m->p_size_valid = 1;
5126 if (info != NULL && info->relro)
5128 for (m = mfirst; m != NULL; m = m->next)
5130 if (m->p_type == PT_LOAD
5132 && m->sections[0]->vma >= info->relro_start
5133 && m->sections[0]->vma < info->relro_end)
5136 while (--i != (unsigned) -1)
5137 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5138 == (SEC_LOAD | SEC_HAS_CONTENTS))
5141 if (i != (unsigned) -1)
5146 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5149 amt = sizeof (struct elf_segment_map);
5150 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5154 m->p_type = PT_GNU_RELRO;
5161 elf_seg_map (abfd) = mfirst;
5164 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5167 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5169 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5174 if (sections != NULL)
5179 /* Sort sections by address. */
5182 elf_sort_sections (const void *arg1, const void *arg2)
5184 const asection *sec1 = *(const asection **) arg1;
5185 const asection *sec2 = *(const asection **) arg2;
5186 bfd_size_type size1, size2;
5188 /* Sort by LMA first, since this is the address used to
5189 place the section into a segment. */
5190 if (sec1->lma < sec2->lma)
5192 else if (sec1->lma > sec2->lma)
5195 /* Then sort by VMA. Normally the LMA and the VMA will be
5196 the same, and this will do nothing. */
5197 if (sec1->vma < sec2->vma)
5199 else if (sec1->vma > sec2->vma)
5202 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5204 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5210 /* If the indices are the same, do not return 0
5211 here, but continue to try the next comparison. */
5212 if (sec1->target_index - sec2->target_index != 0)
5213 return sec1->target_index - sec2->target_index;
5218 else if (TOEND (sec2))
5223 /* Sort by size, to put zero sized sections
5224 before others at the same address. */
5226 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5227 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5234 return sec1->target_index - sec2->target_index;
5237 /* Ian Lance Taylor writes:
5239 We shouldn't be using % with a negative signed number. That's just
5240 not good. We have to make sure either that the number is not
5241 negative, or that the number has an unsigned type. When the types
5242 are all the same size they wind up as unsigned. When file_ptr is a
5243 larger signed type, the arithmetic winds up as signed long long,
5246 What we're trying to say here is something like ``increase OFF by
5247 the least amount that will cause it to be equal to the VMA modulo
5249 /* In other words, something like:
5251 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5252 off_offset = off % bed->maxpagesize;
5253 if (vma_offset < off_offset)
5254 adjustment = vma_offset + bed->maxpagesize - off_offset;
5256 adjustment = vma_offset - off_offset;
5258 which can be collapsed into the expression below. */
5261 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5263 /* PR binutils/16199: Handle an alignment of zero. */
5264 if (maxpagesize == 0)
5266 return ((vma - off) % maxpagesize);
5270 print_segment_map (const struct elf_segment_map *m)
5273 const char *pt = get_segment_type (m->p_type);
5278 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5279 sprintf (buf, "LOPROC+%7.7x",
5280 (unsigned int) (m->p_type - PT_LOPROC));
5281 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5282 sprintf (buf, "LOOS+%7.7x",
5283 (unsigned int) (m->p_type - PT_LOOS));
5285 snprintf (buf, sizeof (buf), "%8.8x",
5286 (unsigned int) m->p_type);
5290 fprintf (stderr, "%s:", pt);
5291 for (j = 0; j < m->count; j++)
5292 fprintf (stderr, " %s", m->sections [j]->name);
5298 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5303 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5305 buf = bfd_zmalloc (len);
5308 ret = bfd_bwrite (buf, len, abfd) == len;
5313 /* Assign file positions to the sections based on the mapping from
5314 sections to segments. This function also sets up some fields in
5318 assign_file_positions_for_load_sections (bfd *abfd,
5319 struct bfd_link_info *link_info)
5321 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5322 struct elf_segment_map *m;
5323 Elf_Internal_Phdr *phdrs;
5324 Elf_Internal_Phdr *p;
5326 bfd_size_type maxpagesize;
5327 unsigned int pt_load_count = 0;
5330 bfd_vma header_pad = 0;
5332 if (link_info == NULL
5333 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5337 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5341 header_pad = m->header_size;
5346 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5347 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5351 /* PR binutils/12467. */
5352 elf_elfheader (abfd)->e_phoff = 0;
5353 elf_elfheader (abfd)->e_phentsize = 0;
5356 elf_elfheader (abfd)->e_phnum = alloc;
5358 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5359 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5361 BFD_ASSERT (elf_program_header_size (abfd)
5362 >= alloc * bed->s->sizeof_phdr);
5366 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5370 /* We're writing the size in elf_program_header_size (abfd),
5371 see assign_file_positions_except_relocs, so make sure we have
5372 that amount allocated, with trailing space cleared.
5373 The variable alloc contains the computed need, while
5374 elf_program_header_size (abfd) contains the size used for the
5376 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5377 where the layout is forced to according to a larger size in the
5378 last iterations for the testcase ld-elf/header. */
5379 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
5381 phdrs = (Elf_Internal_Phdr *)
5383 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5384 sizeof (Elf_Internal_Phdr));
5385 elf_tdata (abfd)->phdr = phdrs;
5390 if ((abfd->flags & D_PAGED) != 0)
5391 maxpagesize = bed->maxpagesize;
5393 off = bed->s->sizeof_ehdr;
5394 off += alloc * bed->s->sizeof_phdr;
5395 if (header_pad < (bfd_vma) off)
5401 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
5403 m = m->next, p++, j++)
5407 bfd_boolean no_contents;
5409 /* If elf_segment_map is not from map_sections_to_segments, the
5410 sections may not be correctly ordered. NOTE: sorting should
5411 not be done to the PT_NOTE section of a corefile, which may
5412 contain several pseudo-sections artificially created by bfd.
5413 Sorting these pseudo-sections breaks things badly. */
5415 && !(elf_elfheader (abfd)->e_type == ET_CORE
5416 && m->p_type == PT_NOTE))
5417 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5420 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5421 number of sections with contents contributing to both p_filesz
5422 and p_memsz, followed by a number of sections with no contents
5423 that just contribute to p_memsz. In this loop, OFF tracks next
5424 available file offset for PT_LOAD and PT_NOTE segments. */
5425 p->p_type = m->p_type;
5426 p->p_flags = m->p_flags;
5429 p->p_vaddr = m->p_vaddr_offset;
5431 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
5433 if (m->p_paddr_valid)
5434 p->p_paddr = m->p_paddr;
5435 else if (m->count == 0)
5438 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
5440 if (p->p_type == PT_LOAD
5441 && (abfd->flags & D_PAGED) != 0)
5443 /* p_align in demand paged PT_LOAD segments effectively stores
5444 the maximum page size. When copying an executable with
5445 objcopy, we set m->p_align from the input file. Use this
5446 value for maxpagesize rather than bed->maxpagesize, which
5447 may be different. Note that we use maxpagesize for PT_TLS
5448 segment alignment later in this function, so we are relying
5449 on at least one PT_LOAD segment appearing before a PT_TLS
5451 if (m->p_align_valid)
5452 maxpagesize = m->p_align;
5454 p->p_align = maxpagesize;
5457 else if (m->p_align_valid)
5458 p->p_align = m->p_align;
5459 else if (m->count == 0)
5460 p->p_align = 1 << bed->s->log_file_align;
5464 no_contents = FALSE;
5466 if (p->p_type == PT_LOAD
5469 bfd_size_type align;
5470 unsigned int align_power = 0;
5472 if (m->p_align_valid)
5476 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5478 unsigned int secalign;
5480 secalign = bfd_get_section_alignment (abfd, *secpp);
5481 if (secalign > align_power)
5482 align_power = secalign;
5484 align = (bfd_size_type) 1 << align_power;
5485 if (align < maxpagesize)
5486 align = maxpagesize;
5489 for (i = 0; i < m->count; i++)
5490 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5491 /* If we aren't making room for this section, then
5492 it must be SHT_NOBITS regardless of what we've
5493 set via struct bfd_elf_special_section. */
5494 elf_section_type (m->sections[i]) = SHT_NOBITS;
5496 /* Find out whether this segment contains any loadable
5499 for (i = 0; i < m->count; i++)
5500 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5502 no_contents = FALSE;
5506 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5508 /* Broken hardware and/or kernel require that files do not
5509 map the same page with different permissions on some hppa
5511 if (pt_load_count > 1
5512 && bed->no_page_alias
5513 && (off & (maxpagesize - 1)) != 0
5514 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5515 off_adjust += maxpagesize;
5519 /* We shouldn't need to align the segment on disk since
5520 the segment doesn't need file space, but the gABI
5521 arguably requires the alignment and glibc ld.so
5522 checks it. So to comply with the alignment
5523 requirement but not waste file space, we adjust
5524 p_offset for just this segment. (OFF_ADJUST is
5525 subtracted from OFF later.) This may put p_offset
5526 past the end of file, but that shouldn't matter. */
5531 /* Make sure the .dynamic section is the first section in the
5532 PT_DYNAMIC segment. */
5533 else if (p->p_type == PT_DYNAMIC
5535 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5538 (_("%pB: The first section in the PT_DYNAMIC segment"
5539 " is not the .dynamic section"),
5541 bfd_set_error (bfd_error_bad_value);
5544 /* Set the note section type to SHT_NOTE. */
5545 else if (p->p_type == PT_NOTE)
5546 for (i = 0; i < m->count; i++)
5547 elf_section_type (m->sections[i]) = SHT_NOTE;
5553 if (m->includes_filehdr)
5555 if (!m->p_flags_valid)
5557 p->p_filesz = bed->s->sizeof_ehdr;
5558 p->p_memsz = bed->s->sizeof_ehdr;
5561 if (p->p_vaddr < (bfd_vma) off
5562 || (!m->p_paddr_valid
5563 && p->p_paddr < (bfd_vma) off))
5566 (_("%pB: not enough room for program headers,"
5567 " try linking with -N"),
5569 bfd_set_error (bfd_error_bad_value);
5574 if (!m->p_paddr_valid)
5579 if (m->includes_phdrs)
5581 if (!m->p_flags_valid)
5584 if (!m->includes_filehdr)
5586 p->p_offset = bed->s->sizeof_ehdr;
5590 p->p_vaddr -= off - p->p_offset;
5591 if (!m->p_paddr_valid)
5592 p->p_paddr -= off - p->p_offset;
5596 p->p_filesz += alloc * bed->s->sizeof_phdr;
5597 p->p_memsz += alloc * bed->s->sizeof_phdr;
5600 p->p_filesz += header_pad;
5601 p->p_memsz += header_pad;
5605 if (p->p_type == PT_LOAD
5606 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5608 if (!m->includes_filehdr && !m->includes_phdrs)
5614 adjust = off - (p->p_offset + p->p_filesz);
5616 p->p_filesz += adjust;
5617 p->p_memsz += adjust;
5621 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5622 maps. Set filepos for sections in PT_LOAD segments, and in
5623 core files, for sections in PT_NOTE segments.
5624 assign_file_positions_for_non_load_sections will set filepos
5625 for other sections and update p_filesz for other segments. */
5626 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5629 bfd_size_type align;
5630 Elf_Internal_Shdr *this_hdr;
5633 this_hdr = &elf_section_data (sec)->this_hdr;
5634 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5636 if ((p->p_type == PT_LOAD
5637 || p->p_type == PT_TLS)
5638 && (this_hdr->sh_type != SHT_NOBITS
5639 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5640 && ((this_hdr->sh_flags & SHF_TLS) == 0
5641 || p->p_type == PT_TLS))))
5643 bfd_vma p_start = p->p_paddr;
5644 bfd_vma p_end = p_start + p->p_memsz;
5645 bfd_vma s_start = sec->lma;
5646 bfd_vma adjust = s_start - p_end;
5650 || p_end < p_start))
5653 /* xgettext:c-format */
5654 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5655 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
5659 p->p_memsz += adjust;
5661 if (this_hdr->sh_type != SHT_NOBITS)
5663 if (p->p_filesz + adjust < p->p_memsz)
5665 /* We have a PROGBITS section following NOBITS ones.
5666 Allocate file space for the NOBITS section(s) and
5668 adjust = p->p_memsz - p->p_filesz;
5669 if (!write_zeros (abfd, off, adjust))
5673 p->p_filesz += adjust;
5677 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5679 /* The section at i == 0 is the one that actually contains
5683 this_hdr->sh_offset = sec->filepos = off;
5684 off += this_hdr->sh_size;
5685 p->p_filesz = this_hdr->sh_size;
5691 /* The rest are fake sections that shouldn't be written. */
5700 if (p->p_type == PT_LOAD)
5702 this_hdr->sh_offset = sec->filepos = off;
5703 if (this_hdr->sh_type != SHT_NOBITS)
5704 off += this_hdr->sh_size;
5706 else if (this_hdr->sh_type == SHT_NOBITS
5707 && (this_hdr->sh_flags & SHF_TLS) != 0
5708 && this_hdr->sh_offset == 0)
5710 /* This is a .tbss section that didn't get a PT_LOAD.
5711 (See _bfd_elf_map_sections_to_segments "Create a
5712 final PT_LOAD".) Set sh_offset to the value it
5713 would have if we had created a zero p_filesz and
5714 p_memsz PT_LOAD header for the section. This
5715 also makes the PT_TLS header have the same
5717 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5719 this_hdr->sh_offset = sec->filepos = off + adjust;
5722 if (this_hdr->sh_type != SHT_NOBITS)
5724 p->p_filesz += this_hdr->sh_size;
5725 /* A load section without SHF_ALLOC is something like
5726 a note section in a PT_NOTE segment. These take
5727 file space but are not loaded into memory. */
5728 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5729 p->p_memsz += this_hdr->sh_size;
5731 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5733 if (p->p_type == PT_TLS)
5734 p->p_memsz += this_hdr->sh_size;
5736 /* .tbss is special. It doesn't contribute to p_memsz of
5738 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5739 p->p_memsz += this_hdr->sh_size;
5742 if (align > p->p_align
5743 && !m->p_align_valid
5744 && (p->p_type != PT_LOAD
5745 || (abfd->flags & D_PAGED) == 0))
5749 if (!m->p_flags_valid)
5752 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5754 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5761 /* Check that all sections are in a PT_LOAD segment.
5762 Don't check funky gdb generated core files. */
5763 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5765 bfd_boolean check_vma = TRUE;
5767 for (i = 1; i < m->count; i++)
5768 if (m->sections[i]->vma == m->sections[i - 1]->vma
5769 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5770 ->this_hdr), p) != 0
5771 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5772 ->this_hdr), p) != 0)
5774 /* Looks like we have overlays packed into the segment. */
5779 for (i = 0; i < m->count; i++)
5781 Elf_Internal_Shdr *this_hdr;
5784 sec = m->sections[i];
5785 this_hdr = &(elf_section_data(sec)->this_hdr);
5786 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5787 && !ELF_TBSS_SPECIAL (this_hdr, p))
5790 /* xgettext:c-format */
5791 (_("%pB: section `%pA' can't be allocated in segment %d"),
5793 print_segment_map (m);
5799 elf_next_file_pos (abfd) = off;
5803 /* Assign file positions for the other sections. */
5806 assign_file_positions_for_non_load_sections (bfd *abfd,
5807 struct bfd_link_info *link_info)
5809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5810 Elf_Internal_Shdr **i_shdrpp;
5811 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5812 Elf_Internal_Phdr *phdrs;
5813 Elf_Internal_Phdr *p;
5814 struct elf_segment_map *m;
5815 struct elf_segment_map *hdrs_segment;
5816 bfd_vma filehdr_vaddr, filehdr_paddr;
5817 bfd_vma phdrs_vaddr, phdrs_paddr;
5821 i_shdrpp = elf_elfsections (abfd);
5822 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5823 off = elf_next_file_pos (abfd);
5824 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5826 Elf_Internal_Shdr *hdr;
5829 if (hdr->bfd_section != NULL
5830 && (hdr->bfd_section->filepos != 0
5831 || (hdr->sh_type == SHT_NOBITS
5832 && hdr->contents == NULL)))
5833 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5834 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5836 if (hdr->sh_size != 0)
5838 /* xgettext:c-format */
5839 (_("%pB: warning: allocated section `%s' not in segment"),
5841 (hdr->bfd_section == NULL
5843 : hdr->bfd_section->name));
5844 /* We don't need to page align empty sections. */
5845 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5846 off += vma_page_aligned_bias (hdr->sh_addr, off,
5849 off += vma_page_aligned_bias (hdr->sh_addr, off,
5851 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5854 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5855 && hdr->bfd_section == NULL)
5856 || (hdr->bfd_section != NULL
5857 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5858 /* Compress DWARF debug sections. */
5859 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5860 || (elf_symtab_shndx_list (abfd) != NULL
5861 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5862 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5863 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5864 hdr->sh_offset = -1;
5866 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5869 /* Now that we have set the section file positions, we can set up
5870 the file positions for the non PT_LOAD segments. */
5874 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5876 hdrs_segment = NULL;
5877 phdrs = elf_tdata (abfd)->phdr;
5878 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5881 if (p->p_type != PT_LOAD)
5884 if (m->includes_filehdr)
5886 filehdr_vaddr = p->p_vaddr;
5887 filehdr_paddr = p->p_paddr;
5889 if (m->includes_phdrs)
5891 phdrs_vaddr = p->p_vaddr;
5892 phdrs_paddr = p->p_paddr;
5893 if (m->includes_filehdr)
5896 phdrs_vaddr += bed->s->sizeof_ehdr;
5897 phdrs_paddr += bed->s->sizeof_ehdr;
5902 if (hdrs_segment != NULL && link_info != NULL)
5904 /* There is a segment that contains both the file headers and the
5905 program headers, so provide a symbol __ehdr_start pointing there.
5906 A program can use this to examine itself robustly. */
5908 struct elf_link_hash_entry *hash
5909 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5910 FALSE, FALSE, TRUE);
5911 /* If the symbol was referenced and not defined, define it. */
5913 && (hash->root.type == bfd_link_hash_new
5914 || hash->root.type == bfd_link_hash_undefined
5915 || hash->root.type == bfd_link_hash_undefweak
5916 || hash->root.type == bfd_link_hash_common))
5919 if (hdrs_segment->count != 0)
5920 /* The segment contains sections, so use the first one. */
5921 s = hdrs_segment->sections[0];
5923 /* Use the first (i.e. lowest-addressed) section in any segment. */
5924 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5933 hash->root.u.def.value = filehdr_vaddr - s->vma;
5934 hash->root.u.def.section = s;
5938 hash->root.u.def.value = filehdr_vaddr;
5939 hash->root.u.def.section = bfd_abs_section_ptr;
5942 hash->root.type = bfd_link_hash_defined;
5943 hash->def_regular = 1;
5948 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5950 if (p->p_type == PT_GNU_RELRO)
5955 if (link_info != NULL)
5957 /* During linking the range of the RELRO segment is passed
5958 in link_info. Note that there may be padding between
5959 relro_start and the first RELRO section. */
5960 start = link_info->relro_start;
5961 end = link_info->relro_end;
5963 else if (m->count != 0)
5965 if (!m->p_size_valid)
5967 start = m->sections[0]->vma;
5968 end = start + m->p_size;
5979 struct elf_segment_map *lm;
5980 const Elf_Internal_Phdr *lp;
5983 /* Find a LOAD segment containing a section in the RELRO
5985 for (lm = elf_seg_map (abfd), lp = phdrs;
5987 lm = lm->next, lp++)
5989 if (lp->p_type == PT_LOAD
5991 && (lm->sections[lm->count - 1]->vma
5992 + (!IS_TBSS (lm->sections[lm->count - 1])
5993 ? lm->sections[lm->count - 1]->size
5995 && lm->sections[0]->vma < end)
6001 /* Find the section starting the RELRO segment. */
6002 for (i = 0; i < lm->count; i++)
6004 asection *s = lm->sections[i];
6013 p->p_vaddr = lm->sections[i]->vma;
6014 p->p_paddr = lm->sections[i]->lma;
6015 p->p_offset = lm->sections[i]->filepos;
6016 p->p_memsz = end - p->p_vaddr;
6017 p->p_filesz = p->p_memsz;
6019 /* The RELRO segment typically ends a few bytes
6020 into .got.plt but other layouts are possible.
6021 In cases where the end does not match any
6022 loaded section (for instance is in file
6023 padding), trim p_filesz back to correspond to
6024 the end of loaded section contents. */
6025 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6026 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6028 /* Preserve the alignment and flags if they are
6029 valid. The gold linker generates RW/4 for
6030 the PT_GNU_RELRO section. It is better for
6031 objcopy/strip to honor these attributes
6032 otherwise gdb will choke when using separate
6034 if (!m->p_align_valid)
6036 if (!m->p_flags_valid)
6042 if (link_info != NULL)
6045 memset (p, 0, sizeof *p);
6047 else if (p->p_type == PT_GNU_STACK)
6049 if (m->p_size_valid)
6050 p->p_memsz = m->p_size;
6052 else if (m->count != 0)
6056 if (p->p_type != PT_LOAD
6057 && (p->p_type != PT_NOTE
6058 || bfd_get_format (abfd) != bfd_core))
6060 /* A user specified segment layout may include a PHDR
6061 segment that overlaps with a LOAD segment... */
6062 if (p->p_type == PT_PHDR)
6068 if (m->includes_filehdr || m->includes_phdrs)
6070 /* PR 17512: file: 2195325e. */
6072 (_("%pB: error: non-load segment %d includes file header "
6073 "and/or program header"),
6074 abfd, (int) (p - phdrs));
6079 p->p_offset = m->sections[0]->filepos;
6080 for (i = m->count; i-- != 0;)
6082 asection *sect = m->sections[i];
6083 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6084 if (hdr->sh_type != SHT_NOBITS)
6086 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6093 else if (m->includes_filehdr)
6095 p->p_vaddr = filehdr_vaddr;
6096 if (! m->p_paddr_valid)
6097 p->p_paddr = filehdr_paddr;
6099 else if (m->includes_phdrs)
6101 p->p_vaddr = phdrs_vaddr;
6102 if (! m->p_paddr_valid)
6103 p->p_paddr = phdrs_paddr;
6107 elf_next_file_pos (abfd) = off;
6112 static elf_section_list *
6113 find_section_in_list (unsigned int i, elf_section_list * list)
6115 for (;list != NULL; list = list->next)
6121 /* Work out the file positions of all the sections. This is called by
6122 _bfd_elf_compute_section_file_positions. All the section sizes and
6123 VMAs must be known before this is called.
6125 Reloc sections come in two flavours: Those processed specially as
6126 "side-channel" data attached to a section to which they apply, and
6127 those that bfd doesn't process as relocations. The latter sort are
6128 stored in a normal bfd section by bfd_section_from_shdr. We don't
6129 consider the former sort here, unless they form part of the loadable
6130 image. Reloc sections not assigned here will be handled later by
6131 assign_file_positions_for_relocs.
6133 We also don't set the positions of the .symtab and .strtab here. */
6136 assign_file_positions_except_relocs (bfd *abfd,
6137 struct bfd_link_info *link_info)
6139 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6140 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6141 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6143 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6144 && bfd_get_format (abfd) != bfd_core)
6146 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6147 unsigned int num_sec = elf_numsections (abfd);
6148 Elf_Internal_Shdr **hdrpp;
6152 /* Start after the ELF header. */
6153 off = i_ehdrp->e_ehsize;
6155 /* We are not creating an executable, which means that we are
6156 not creating a program header, and that the actual order of
6157 the sections in the file is unimportant. */
6158 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6160 Elf_Internal_Shdr *hdr;
6163 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6164 && hdr->bfd_section == NULL)
6165 || (hdr->bfd_section != NULL
6166 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6167 /* Compress DWARF debug sections. */
6168 || i == elf_onesymtab (abfd)
6169 || (elf_symtab_shndx_list (abfd) != NULL
6170 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6171 || i == elf_strtab_sec (abfd)
6172 || i == elf_shstrtab_sec (abfd))
6174 hdr->sh_offset = -1;
6177 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6180 elf_next_file_pos (abfd) = off;
6186 /* Assign file positions for the loaded sections based on the
6187 assignment of sections to segments. */
6188 if (!assign_file_positions_for_load_sections (abfd, link_info))
6191 /* And for non-load sections. */
6192 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6195 if (bed->elf_backend_modify_program_headers != NULL)
6197 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6201 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6202 if (link_info != NULL && bfd_link_pie (link_info))
6204 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6205 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6206 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6208 /* Find the lowest p_vaddr in PT_LOAD segments. */
6209 bfd_vma p_vaddr = (bfd_vma) -1;
6210 for (; segment < end_segment; segment++)
6211 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6212 p_vaddr = segment->p_vaddr;
6214 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6215 segments is non-zero. */
6217 i_ehdrp->e_type = ET_EXEC;
6220 /* Write out the program headers. */
6221 alloc = elf_elfheader (abfd)->e_phnum;
6225 /* PR ld/20815 - Check that the program header segment, if present, will
6226 be loaded into memory. FIXME: The check below is not sufficient as
6227 really all PT_LOAD segments should be checked before issuing an error
6228 message. Plus the PHDR segment does not have to be the first segment
6229 in the program header table. But this version of the check should
6230 catch all real world use cases.
6232 FIXME: We used to have code here to sort the PT_LOAD segments into
6233 ascending order, as per the ELF spec. But this breaks some programs,
6234 including the Linux kernel. But really either the spec should be
6235 changed or the programs updated. */
6237 && tdata->phdr[0].p_type == PT_PHDR
6238 && (bed->elf_backend_allow_non_load_phdr == NULL
6239 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6241 && tdata->phdr[1].p_type == PT_LOAD
6242 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6243 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6244 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
6246 /* The fix for this error is usually to edit the linker script being
6247 used and set up the program headers manually. Either that or
6248 leave room for the headers at the start of the SECTIONS. */
6249 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6250 " by LOAD segment"),
6255 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
6256 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6264 prep_headers (bfd *abfd)
6266 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6267 struct elf_strtab_hash *shstrtab;
6268 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6270 i_ehdrp = elf_elfheader (abfd);
6272 shstrtab = _bfd_elf_strtab_init ();
6273 if (shstrtab == NULL)
6276 elf_shstrtab (abfd) = shstrtab;
6278 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6279 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6280 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6281 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6283 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6284 i_ehdrp->e_ident[EI_DATA] =
6285 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6286 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6288 if ((abfd->flags & DYNAMIC) != 0)
6289 i_ehdrp->e_type = ET_DYN;
6290 else if ((abfd->flags & EXEC_P) != 0)
6291 i_ehdrp->e_type = ET_EXEC;
6292 else if (bfd_get_format (abfd) == bfd_core)
6293 i_ehdrp->e_type = ET_CORE;
6295 i_ehdrp->e_type = ET_REL;
6297 switch (bfd_get_arch (abfd))
6299 case bfd_arch_unknown:
6300 i_ehdrp->e_machine = EM_NONE;
6303 /* There used to be a long list of cases here, each one setting
6304 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6305 in the corresponding bfd definition. To avoid duplication,
6306 the switch was removed. Machines that need special handling
6307 can generally do it in elf_backend_final_write_processing(),
6308 unless they need the information earlier than the final write.
6309 Such need can generally be supplied by replacing the tests for
6310 e_machine with the conditions used to determine it. */
6312 i_ehdrp->e_machine = bed->elf_machine_code;
6315 i_ehdrp->e_version = bed->s->ev_current;
6316 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6318 /* No program header, for now. */
6319 i_ehdrp->e_phoff = 0;
6320 i_ehdrp->e_phentsize = 0;
6321 i_ehdrp->e_phnum = 0;
6323 /* Each bfd section is section header entry. */
6324 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6325 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6327 /* If we're building an executable, we'll need a program header table. */
6328 if (abfd->flags & EXEC_P)
6329 /* It all happens later. */
6333 i_ehdrp->e_phentsize = 0;
6334 i_ehdrp->e_phoff = 0;
6337 elf_tdata (abfd)->symtab_hdr.sh_name =
6338 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6339 elf_tdata (abfd)->strtab_hdr.sh_name =
6340 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6341 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6342 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6343 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6344 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6345 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6351 /* Assign file positions for all the reloc sections which are not part
6352 of the loadable file image, and the file position of section headers. */
6355 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6358 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6359 Elf_Internal_Shdr *shdrp;
6360 Elf_Internal_Ehdr *i_ehdrp;
6361 const struct elf_backend_data *bed;
6363 off = elf_next_file_pos (abfd);
6365 shdrpp = elf_elfsections (abfd);
6366 end_shdrpp = shdrpp + elf_numsections (abfd);
6367 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6370 if (shdrp->sh_offset == -1)
6372 asection *sec = shdrp->bfd_section;
6373 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6374 || shdrp->sh_type == SHT_RELA);
6376 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6380 const char *name = sec->name;
6381 struct bfd_elf_section_data *d;
6383 /* Compress DWARF debug sections. */
6384 if (!bfd_compress_section (abfd, sec,
6388 if (sec->compress_status == COMPRESS_SECTION_DONE
6389 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6391 /* If section is compressed with zlib-gnu, convert
6392 section name from .debug_* to .zdebug_*. */
6394 = convert_debug_to_zdebug (abfd, name);
6395 if (new_name == NULL)
6399 /* Add section name to section name section. */
6400 if (shdrp->sh_name != (unsigned int) -1)
6403 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6405 d = elf_section_data (sec);
6407 /* Add reloc section name to section name section. */
6409 && !_bfd_elf_set_reloc_sh_name (abfd,
6414 && !_bfd_elf_set_reloc_sh_name (abfd,
6419 /* Update section size and contents. */
6420 shdrp->sh_size = sec->size;
6421 shdrp->contents = sec->contents;
6422 shdrp->bfd_section->contents = NULL;
6424 off = _bfd_elf_assign_file_position_for_section (shdrp,
6431 /* Place section name section after DWARF debug sections have been
6433 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6434 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6435 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6436 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6438 /* Place the section headers. */
6439 i_ehdrp = elf_elfheader (abfd);
6440 bed = get_elf_backend_data (abfd);
6441 off = align_file_position (off, 1 << bed->s->log_file_align);
6442 i_ehdrp->e_shoff = off;
6443 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6444 elf_next_file_pos (abfd) = off;
6450 _bfd_elf_write_object_contents (bfd *abfd)
6452 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6453 Elf_Internal_Shdr **i_shdrp;
6455 unsigned int count, num_sec;
6456 struct elf_obj_tdata *t;
6458 if (! abfd->output_has_begun
6459 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6461 /* Do not rewrite ELF data when the BFD has been opened for update.
6462 abfd->output_has_begun was set to TRUE on opening, so creation of new
6463 sections, and modification of existing section sizes was restricted.
6464 This means the ELF header, program headers and section headers can't have
6466 If the contents of any sections has been modified, then those changes have
6467 already been written to the BFD. */
6468 else if (abfd->direction == both_direction)
6470 BFD_ASSERT (abfd->output_has_begun);
6474 i_shdrp = elf_elfsections (abfd);
6477 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6481 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6484 /* After writing the headers, we need to write the sections too... */
6485 num_sec = elf_numsections (abfd);
6486 for (count = 1; count < num_sec; count++)
6488 i_shdrp[count]->sh_name
6489 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6490 i_shdrp[count]->sh_name);
6491 if (bed->elf_backend_section_processing)
6492 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6494 if (i_shdrp[count]->contents)
6496 bfd_size_type amt = i_shdrp[count]->sh_size;
6498 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6499 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6504 /* Write out the section header names. */
6505 t = elf_tdata (abfd);
6506 if (elf_shstrtab (abfd) != NULL
6507 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6508 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6511 if (bed->elf_backend_final_write_processing)
6512 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
6514 if (!bed->s->write_shdrs_and_ehdr (abfd))
6517 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6518 if (t->o->build_id.after_write_object_contents != NULL)
6519 return (*t->o->build_id.after_write_object_contents) (abfd);
6525 _bfd_elf_write_corefile_contents (bfd *abfd)
6527 /* Hopefully this can be done just like an object file. */
6528 return _bfd_elf_write_object_contents (abfd);
6531 /* Given a section, search the header to find them. */
6534 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6536 const struct elf_backend_data *bed;
6537 unsigned int sec_index;
6539 if (elf_section_data (asect) != NULL
6540 && elf_section_data (asect)->this_idx != 0)
6541 return elf_section_data (asect)->this_idx;
6543 if (bfd_is_abs_section (asect))
6544 sec_index = SHN_ABS;
6545 else if (bfd_is_com_section (asect))
6546 sec_index = SHN_COMMON;
6547 else if (bfd_is_und_section (asect))
6548 sec_index = SHN_UNDEF;
6550 sec_index = SHN_BAD;
6552 bed = get_elf_backend_data (abfd);
6553 if (bed->elf_backend_section_from_bfd_section)
6555 int retval = sec_index;
6557 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6561 if (sec_index == SHN_BAD)
6562 bfd_set_error (bfd_error_nonrepresentable_section);
6567 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6571 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6573 asymbol *asym_ptr = *asym_ptr_ptr;
6575 flagword flags = asym_ptr->flags;
6577 /* When gas creates relocations against local labels, it creates its
6578 own symbol for the section, but does put the symbol into the
6579 symbol chain, so udata is 0. When the linker is generating
6580 relocatable output, this section symbol may be for one of the
6581 input sections rather than the output section. */
6582 if (asym_ptr->udata.i == 0
6583 && (flags & BSF_SECTION_SYM)
6584 && asym_ptr->section)
6589 sec = asym_ptr->section;
6590 if (sec->owner != abfd && sec->output_section != NULL)
6591 sec = sec->output_section;
6592 if (sec->owner == abfd
6593 && (indx = sec->index) < elf_num_section_syms (abfd)
6594 && elf_section_syms (abfd)[indx] != NULL)
6595 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6598 idx = asym_ptr->udata.i;
6602 /* This case can occur when using --strip-symbol on a symbol
6603 which is used in a relocation entry. */
6605 /* xgettext:c-format */
6606 (_("%pB: symbol `%s' required but not present"),
6607 abfd, bfd_asymbol_name (asym_ptr));
6608 bfd_set_error (bfd_error_no_symbols);
6615 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6616 (long) asym_ptr, asym_ptr->name, idx, flags);
6624 /* Rewrite program header information. */
6627 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6629 Elf_Internal_Ehdr *iehdr;
6630 struct elf_segment_map *map;
6631 struct elf_segment_map *map_first;
6632 struct elf_segment_map **pointer_to_map;
6633 Elf_Internal_Phdr *segment;
6636 unsigned int num_segments;
6637 bfd_boolean phdr_included = FALSE;
6638 bfd_boolean p_paddr_valid;
6639 bfd_vma maxpagesize;
6640 struct elf_segment_map *phdr_adjust_seg = NULL;
6641 unsigned int phdr_adjust_num = 0;
6642 const struct elf_backend_data *bed;
6644 bed = get_elf_backend_data (ibfd);
6645 iehdr = elf_elfheader (ibfd);
6648 pointer_to_map = &map_first;
6650 num_segments = elf_elfheader (ibfd)->e_phnum;
6651 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6653 /* Returns the end address of the segment + 1. */
6654 #define SEGMENT_END(segment, start) \
6655 (start + (segment->p_memsz > segment->p_filesz \
6656 ? segment->p_memsz : segment->p_filesz))
6658 #define SECTION_SIZE(section, segment) \
6659 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6660 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6661 ? section->size : 0)
6663 /* Returns TRUE if the given section is contained within
6664 the given segment. VMA addresses are compared. */
6665 #define IS_CONTAINED_BY_VMA(section, segment) \
6666 (section->vma >= segment->p_vaddr \
6667 && (section->vma + SECTION_SIZE (section, segment) \
6668 <= (SEGMENT_END (segment, segment->p_vaddr))))
6670 /* Returns TRUE if the given section is contained within
6671 the given segment. LMA addresses are compared. */
6672 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6673 (section->lma >= base \
6674 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6675 && (section->lma + SECTION_SIZE (section, segment) \
6676 <= SEGMENT_END (segment, base)))
6678 /* Handle PT_NOTE segment. */
6679 #define IS_NOTE(p, s) \
6680 (p->p_type == PT_NOTE \
6681 && elf_section_type (s) == SHT_NOTE \
6682 && (bfd_vma) s->filepos >= p->p_offset \
6683 && ((bfd_vma) s->filepos + s->size \
6684 <= p->p_offset + p->p_filesz))
6686 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6688 #define IS_COREFILE_NOTE(p, s) \
6690 && bfd_get_format (ibfd) == bfd_core \
6694 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6695 linker, which generates a PT_INTERP section with p_vaddr and
6696 p_memsz set to 0. */
6697 #define IS_SOLARIS_PT_INTERP(p, s) \
6699 && p->p_paddr == 0 \
6700 && p->p_memsz == 0 \
6701 && p->p_filesz > 0 \
6702 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6704 && (bfd_vma) s->filepos >= p->p_offset \
6705 && ((bfd_vma) s->filepos + s->size \
6706 <= p->p_offset + p->p_filesz))
6708 /* Decide if the given section should be included in the given segment.
6709 A section will be included if:
6710 1. It is within the address space of the segment -- we use the LMA
6711 if that is set for the segment and the VMA otherwise,
6712 2. It is an allocated section or a NOTE section in a PT_NOTE
6714 3. There is an output section associated with it,
6715 4. The section has not already been allocated to a previous segment.
6716 5. PT_GNU_STACK segments do not include any sections.
6717 6. PT_TLS segment includes only SHF_TLS sections.
6718 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6719 8. PT_DYNAMIC should not contain empty sections at the beginning
6720 (with the possible exception of .dynamic). */
6721 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6722 ((((segment->p_paddr \
6723 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6724 : IS_CONTAINED_BY_VMA (section, segment)) \
6725 && (section->flags & SEC_ALLOC) != 0) \
6726 || IS_NOTE (segment, section)) \
6727 && segment->p_type != PT_GNU_STACK \
6728 && (segment->p_type != PT_TLS \
6729 || (section->flags & SEC_THREAD_LOCAL)) \
6730 && (segment->p_type == PT_LOAD \
6731 || segment->p_type == PT_TLS \
6732 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6733 && (segment->p_type != PT_DYNAMIC \
6734 || SECTION_SIZE (section, segment) > 0 \
6735 || (segment->p_paddr \
6736 ? segment->p_paddr != section->lma \
6737 : segment->p_vaddr != section->vma) \
6738 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6740 && (segment->p_type != PT_LOAD || !section->segment_mark))
6742 /* If the output section of a section in the input segment is NULL,
6743 it is removed from the corresponding output segment. */
6744 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6745 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6746 && section->output_section != NULL)
6748 /* Returns TRUE iff seg1 starts after the end of seg2. */
6749 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6750 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6752 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6753 their VMA address ranges and their LMA address ranges overlap.
6754 It is possible to have overlapping VMA ranges without overlapping LMA
6755 ranges. RedBoot images for example can have both .data and .bss mapped
6756 to the same VMA range, but with the .data section mapped to a different
6758 #define SEGMENT_OVERLAPS(seg1, seg2) \
6759 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6760 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6761 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6762 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6764 /* Initialise the segment mark field. */
6765 for (section = ibfd->sections; section != NULL; section = section->next)
6766 section->segment_mark = FALSE;
6768 /* The Solaris linker creates program headers in which all the
6769 p_paddr fields are zero. When we try to objcopy or strip such a
6770 file, we get confused. Check for this case, and if we find it
6771 don't set the p_paddr_valid fields. */
6772 p_paddr_valid = FALSE;
6773 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6776 if (segment->p_paddr != 0)
6778 p_paddr_valid = TRUE;
6782 /* Scan through the segments specified in the program header
6783 of the input BFD. For this first scan we look for overlaps
6784 in the loadable segments. These can be created by weird
6785 parameters to objcopy. Also, fix some solaris weirdness. */
6786 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6791 Elf_Internal_Phdr *segment2;
6793 if (segment->p_type == PT_INTERP)
6794 for (section = ibfd->sections; section; section = section->next)
6795 if (IS_SOLARIS_PT_INTERP (segment, section))
6797 /* Mininal change so that the normal section to segment
6798 assignment code will work. */
6799 segment->p_vaddr = section->vma;
6803 if (segment->p_type != PT_LOAD)
6805 /* Remove PT_GNU_RELRO segment. */
6806 if (segment->p_type == PT_GNU_RELRO)
6807 segment->p_type = PT_NULL;
6811 /* Determine if this segment overlaps any previous segments. */
6812 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6814 bfd_signed_vma extra_length;
6816 if (segment2->p_type != PT_LOAD
6817 || !SEGMENT_OVERLAPS (segment, segment2))
6820 /* Merge the two segments together. */
6821 if (segment2->p_vaddr < segment->p_vaddr)
6823 /* Extend SEGMENT2 to include SEGMENT and then delete
6825 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6826 - SEGMENT_END (segment2, segment2->p_vaddr));
6828 if (extra_length > 0)
6830 segment2->p_memsz += extra_length;
6831 segment2->p_filesz += extra_length;
6834 segment->p_type = PT_NULL;
6836 /* Since we have deleted P we must restart the outer loop. */
6838 segment = elf_tdata (ibfd)->phdr;
6843 /* Extend SEGMENT to include SEGMENT2 and then delete
6845 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6846 - SEGMENT_END (segment, segment->p_vaddr));
6848 if (extra_length > 0)
6850 segment->p_memsz += extra_length;
6851 segment->p_filesz += extra_length;
6854 segment2->p_type = PT_NULL;
6859 /* The second scan attempts to assign sections to segments. */
6860 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6864 unsigned int section_count;
6865 asection **sections;
6866 asection *output_section;
6868 asection *matching_lma;
6869 asection *suggested_lma;
6872 asection *first_section;
6874 if (segment->p_type == PT_NULL)
6877 first_section = NULL;
6878 /* Compute how many sections might be placed into this segment. */
6879 for (section = ibfd->sections, section_count = 0;
6881 section = section->next)
6883 /* Find the first section in the input segment, which may be
6884 removed from the corresponding output segment. */
6885 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6887 if (first_section == NULL)
6888 first_section = section;
6889 if (section->output_section != NULL)
6894 /* Allocate a segment map big enough to contain
6895 all of the sections we have selected. */
6896 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6897 amt += (bfd_size_type) section_count * sizeof (asection *);
6898 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6902 /* Initialise the fields of the segment map. Default to
6903 using the physical address of the segment in the input BFD. */
6905 map->p_type = segment->p_type;
6906 map->p_flags = segment->p_flags;
6907 map->p_flags_valid = 1;
6909 /* If the first section in the input segment is removed, there is
6910 no need to preserve segment physical address in the corresponding
6912 if (!first_section || first_section->output_section != NULL)
6914 map->p_paddr = segment->p_paddr;
6915 map->p_paddr_valid = p_paddr_valid;
6918 /* Determine if this segment contains the ELF file header
6919 and if it contains the program headers themselves. */
6920 map->includes_filehdr = (segment->p_offset == 0
6921 && segment->p_filesz >= iehdr->e_ehsize);
6922 map->includes_phdrs = 0;
6924 if (!phdr_included || segment->p_type != PT_LOAD)
6926 map->includes_phdrs =
6927 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6928 && (segment->p_offset + segment->p_filesz
6929 >= ((bfd_vma) iehdr->e_phoff
6930 + iehdr->e_phnum * iehdr->e_phentsize)));
6932 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6933 phdr_included = TRUE;
6936 if (section_count == 0)
6938 /* Special segments, such as the PT_PHDR segment, may contain
6939 no sections, but ordinary, loadable segments should contain
6940 something. They are allowed by the ELF spec however, so only
6941 a warning is produced.
6942 There is however the valid use case of embedded systems which
6943 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6944 flash memory with zeros. No warning is shown for that case. */
6945 if (segment->p_type == PT_LOAD
6946 && (segment->p_filesz > 0 || segment->p_memsz == 0))
6947 /* xgettext:c-format */
6949 (_("%pB: warning: empty loadable segment detected"
6950 " at vaddr=%#" PRIx64 ", is this intentional?"),
6951 ibfd, (uint64_t) segment->p_vaddr);
6953 map->p_vaddr_offset = segment->p_vaddr;
6955 *pointer_to_map = map;
6956 pointer_to_map = &map->next;
6961 /* Now scan the sections in the input BFD again and attempt
6962 to add their corresponding output sections to the segment map.
6963 The problem here is how to handle an output section which has
6964 been moved (ie had its LMA changed). There are four possibilities:
6966 1. None of the sections have been moved.
6967 In this case we can continue to use the segment LMA from the
6970 2. All of the sections have been moved by the same amount.
6971 In this case we can change the segment's LMA to match the LMA
6972 of the first section.
6974 3. Some of the sections have been moved, others have not.
6975 In this case those sections which have not been moved can be
6976 placed in the current segment which will have to have its size,
6977 and possibly its LMA changed, and a new segment or segments will
6978 have to be created to contain the other sections.
6980 4. The sections have been moved, but not by the same amount.
6981 In this case we can change the segment's LMA to match the LMA
6982 of the first section and we will have to create a new segment
6983 or segments to contain the other sections.
6985 In order to save time, we allocate an array to hold the section
6986 pointers that we are interested in. As these sections get assigned
6987 to a segment, they are removed from this array. */
6989 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
6990 if (sections == NULL)
6993 /* Step One: Scan for segment vs section LMA conflicts.
6994 Also add the sections to the section array allocated above.
6995 Also add the sections to the current segment. In the common
6996 case, where the sections have not been moved, this means that
6997 we have completely filled the segment, and there is nothing
7000 matching_lma = NULL;
7001 suggested_lma = NULL;
7003 for (section = first_section, j = 0;
7005 section = section->next)
7007 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
7009 output_section = section->output_section;
7011 sections[j++] = section;
7013 /* The Solaris native linker always sets p_paddr to 0.
7014 We try to catch that case here, and set it to the
7015 correct value. Note - some backends require that
7016 p_paddr be left as zero. */
7018 && segment->p_vaddr != 0
7019 && !bed->want_p_paddr_set_to_zero
7021 && output_section->lma != 0
7022 && (align_power (segment->p_vaddr
7023 + (map->includes_filehdr
7024 ? iehdr->e_ehsize : 0)
7025 + (map->includes_phdrs
7026 ? iehdr->e_phnum * iehdr->e_phentsize
7028 output_section->alignment_power)
7029 == output_section->vma))
7030 map->p_paddr = segment->p_vaddr;
7032 /* Match up the physical address of the segment with the
7033 LMA address of the output section. */
7034 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7035 || IS_COREFILE_NOTE (segment, section)
7036 || (bed->want_p_paddr_set_to_zero
7037 && IS_CONTAINED_BY_VMA (output_section, segment)))
7039 if (matching_lma == NULL
7040 || output_section->lma < matching_lma->lma)
7041 matching_lma = output_section;
7043 /* We assume that if the section fits within the segment
7044 then it does not overlap any other section within that
7046 map->sections[isec++] = output_section;
7048 else if (suggested_lma == NULL)
7049 suggested_lma = output_section;
7051 if (j == section_count)
7056 BFD_ASSERT (j == section_count);
7058 /* Step Two: Adjust the physical address of the current segment,
7060 if (isec == section_count)
7062 /* All of the sections fitted within the segment as currently
7063 specified. This is the default case. Add the segment to
7064 the list of built segments and carry on to process the next
7065 program header in the input BFD. */
7066 map->count = section_count;
7067 *pointer_to_map = map;
7068 pointer_to_map = &map->next;
7071 && !bed->want_p_paddr_set_to_zero
7072 && matching_lma->lma != map->p_paddr
7073 && !map->includes_filehdr
7074 && !map->includes_phdrs)
7075 /* There is some padding before the first section in the
7076 segment. So, we must account for that in the output
7078 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
7085 /* Change the current segment's physical address to match
7086 the LMA of the first section that fitted, or if no
7087 section fitted, the first section. */
7088 if (matching_lma == NULL)
7089 matching_lma = suggested_lma;
7091 map->p_paddr = matching_lma->lma;
7093 /* Offset the segment physical address from the lma
7094 to allow for space taken up by elf headers. */
7095 if (map->includes_phdrs)
7097 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7099 /* iehdr->e_phnum is just an estimate of the number
7100 of program headers that we will need. Make a note
7101 here of the number we used and the segment we chose
7102 to hold these headers, so that we can adjust the
7103 offset when we know the correct value. */
7104 phdr_adjust_num = iehdr->e_phnum;
7105 phdr_adjust_seg = map;
7108 if (map->includes_filehdr)
7110 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7111 map->p_paddr -= iehdr->e_ehsize;
7112 /* We've subtracted off the size of headers from the
7113 first section lma, but there may have been some
7114 alignment padding before that section too. Try to
7115 account for that by adjusting the segment lma down to
7116 the same alignment. */
7117 if (segment->p_align != 0 && segment->p_align < align)
7118 align = segment->p_align;
7119 map->p_paddr &= -align;
7123 /* Step Three: Loop over the sections again, this time assigning
7124 those that fit to the current segment and removing them from the
7125 sections array; but making sure not to leave large gaps. Once all
7126 possible sections have been assigned to the current segment it is
7127 added to the list of built segments and if sections still remain
7128 to be assigned, a new segment is constructed before repeating
7134 suggested_lma = NULL;
7136 /* Fill the current segment with sections that fit. */
7137 for (j = 0; j < section_count; j++)
7139 section = sections[j];
7141 if (section == NULL)
7144 output_section = section->output_section;
7146 BFD_ASSERT (output_section != NULL);
7148 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7149 || IS_COREFILE_NOTE (segment, section))
7151 if (map->count == 0)
7153 /* If the first section in a segment does not start at
7154 the beginning of the segment, then something is
7156 if (align_power (map->p_paddr
7157 + (map->includes_filehdr
7158 ? iehdr->e_ehsize : 0)
7159 + (map->includes_phdrs
7160 ? iehdr->e_phnum * iehdr->e_phentsize
7162 output_section->alignment_power)
7163 != output_section->lma)
7170 prev_sec = map->sections[map->count - 1];
7172 /* If the gap between the end of the previous section
7173 and the start of this section is more than
7174 maxpagesize then we need to start a new segment. */
7175 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7177 < BFD_ALIGN (output_section->lma, maxpagesize))
7178 || (prev_sec->lma + prev_sec->size
7179 > output_section->lma))
7181 if (suggested_lma == NULL)
7182 suggested_lma = output_section;
7188 map->sections[map->count++] = output_section;
7191 if (segment->p_type == PT_LOAD)
7192 section->segment_mark = TRUE;
7194 else if (suggested_lma == NULL)
7195 suggested_lma = output_section;
7198 /* PR 23932. A corrupt input file may contain sections that cannot
7199 be assigned to any segment - because for example they have a
7200 negative size - or segments that do not contain any sections. */
7201 if (map->count == 0)
7203 bfd_set_error (bfd_error_bad_value);
7208 /* Add the current segment to the list of built segments. */
7209 *pointer_to_map = map;
7210 pointer_to_map = &map->next;
7212 if (isec < section_count)
7214 /* We still have not allocated all of the sections to
7215 segments. Create a new segment here, initialise it
7216 and carry on looping. */
7217 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7218 amt += (bfd_size_type) section_count * sizeof (asection *);
7219 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7226 /* Initialise the fields of the segment map. Set the physical
7227 physical address to the LMA of the first section that has
7228 not yet been assigned. */
7230 map->p_type = segment->p_type;
7231 map->p_flags = segment->p_flags;
7232 map->p_flags_valid = 1;
7233 map->p_paddr = suggested_lma->lma;
7234 map->p_paddr_valid = p_paddr_valid;
7235 map->includes_filehdr = 0;
7236 map->includes_phdrs = 0;
7239 while (isec < section_count);
7244 elf_seg_map (obfd) = map_first;
7246 /* If we had to estimate the number of program headers that were
7247 going to be needed, then check our estimate now and adjust
7248 the offset if necessary. */
7249 if (phdr_adjust_seg != NULL)
7253 for (count = 0, map = map_first; map != NULL; map = map->next)
7256 if (count > phdr_adjust_num)
7257 phdr_adjust_seg->p_paddr
7258 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7260 for (map = map_first; map != NULL; map = map->next)
7261 if (map->p_type == PT_PHDR)
7264 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7265 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7272 #undef IS_CONTAINED_BY_VMA
7273 #undef IS_CONTAINED_BY_LMA
7275 #undef IS_COREFILE_NOTE
7276 #undef IS_SOLARIS_PT_INTERP
7277 #undef IS_SECTION_IN_INPUT_SEGMENT
7278 #undef INCLUDE_SECTION_IN_SEGMENT
7279 #undef SEGMENT_AFTER_SEGMENT
7280 #undef SEGMENT_OVERLAPS
7284 /* Copy ELF program header information. */
7287 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7289 Elf_Internal_Ehdr *iehdr;
7290 struct elf_segment_map *map;
7291 struct elf_segment_map *map_first;
7292 struct elf_segment_map **pointer_to_map;
7293 Elf_Internal_Phdr *segment;
7295 unsigned int num_segments;
7296 bfd_boolean phdr_included = FALSE;
7297 bfd_boolean p_paddr_valid;
7299 iehdr = elf_elfheader (ibfd);
7302 pointer_to_map = &map_first;
7304 /* If all the segment p_paddr fields are zero, don't set
7305 map->p_paddr_valid. */
7306 p_paddr_valid = FALSE;
7307 num_segments = elf_elfheader (ibfd)->e_phnum;
7308 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7311 if (segment->p_paddr != 0)
7313 p_paddr_valid = TRUE;
7317 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7322 unsigned int section_count;
7324 Elf_Internal_Shdr *this_hdr;
7325 asection *first_section = NULL;
7326 asection *lowest_section;
7327 bfd_boolean no_contents = TRUE;
7329 /* Compute how many sections are in this segment. */
7330 for (section = ibfd->sections, section_count = 0;
7332 section = section->next)
7334 this_hdr = &(elf_section_data(section)->this_hdr);
7335 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7337 if (first_section == NULL)
7338 first_section = section;
7339 if (elf_section_type (section) != SHT_NOBITS)
7340 no_contents = FALSE;
7345 /* Allocate a segment map big enough to contain
7346 all of the sections we have selected. */
7347 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7348 amt += (bfd_size_type) section_count * sizeof (asection *);
7349 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7353 /* Initialize the fields of the output segment map with the
7356 map->p_type = segment->p_type;
7357 map->p_flags = segment->p_flags;
7358 map->p_flags_valid = 1;
7359 map->p_paddr = segment->p_paddr;
7360 map->p_paddr_valid = p_paddr_valid;
7361 map->p_align = segment->p_align;
7362 map->p_align_valid = 1;
7363 map->p_vaddr_offset = 0;
7365 if (map->p_type == PT_GNU_RELRO
7366 || map->p_type == PT_GNU_STACK)
7368 /* The PT_GNU_RELRO segment may contain the first a few
7369 bytes in the .got.plt section even if the whole .got.plt
7370 section isn't in the PT_GNU_RELRO segment. We won't
7371 change the size of the PT_GNU_RELRO segment.
7372 Similarly, PT_GNU_STACK size is significant on uclinux
7374 map->p_size = segment->p_memsz;
7375 map->p_size_valid = 1;
7378 /* Determine if this segment contains the ELF file header
7379 and if it contains the program headers themselves. */
7380 map->includes_filehdr = (segment->p_offset == 0
7381 && segment->p_filesz >= iehdr->e_ehsize);
7383 map->includes_phdrs = 0;
7384 if (! phdr_included || segment->p_type != PT_LOAD)
7386 map->includes_phdrs =
7387 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7388 && (segment->p_offset + segment->p_filesz
7389 >= ((bfd_vma) iehdr->e_phoff
7390 + iehdr->e_phnum * iehdr->e_phentsize)));
7392 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7393 phdr_included = TRUE;
7396 lowest_section = NULL;
7397 if (section_count != 0)
7399 unsigned int isec = 0;
7401 for (section = first_section;
7403 section = section->next)
7405 this_hdr = &(elf_section_data(section)->this_hdr);
7406 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7408 map->sections[isec++] = section->output_section;
7409 if ((section->flags & SEC_ALLOC) != 0)
7413 if (lowest_section == NULL
7414 || section->lma < lowest_section->lma)
7415 lowest_section = section;
7417 /* Section lmas are set up from PT_LOAD header
7418 p_paddr in _bfd_elf_make_section_from_shdr.
7419 If this header has a p_paddr that disagrees
7420 with the section lma, flag the p_paddr as
7422 if ((section->flags & SEC_LOAD) != 0)
7423 seg_off = this_hdr->sh_offset - segment->p_offset;
7425 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7426 if (section->lma - segment->p_paddr != seg_off)
7427 map->p_paddr_valid = FALSE;
7429 if (isec == section_count)
7435 if (map->includes_filehdr && lowest_section != NULL)
7437 /* Try to keep the space used by the headers plus any
7438 padding fixed. If there are sections with file contents
7439 in this segment then the lowest sh_offset is the best
7440 guess. Otherwise the segment only has file contents for
7441 the headers, and p_filesz is the best guess. */
7443 map->header_size = segment->p_filesz;
7445 map->header_size = lowest_section->filepos;
7448 if (section_count == 0)
7449 map->p_vaddr_offset = segment->p_vaddr;
7450 else if (!map->includes_phdrs
7451 && !map->includes_filehdr
7452 && map->p_paddr_valid)
7453 /* Account for padding before the first section. */
7454 map->p_vaddr_offset = (segment->p_paddr
7455 - (lowest_section ? lowest_section->lma : 0));
7457 map->count = section_count;
7458 *pointer_to_map = map;
7459 pointer_to_map = &map->next;
7462 elf_seg_map (obfd) = map_first;
7466 /* Copy private BFD data. This copies or rewrites ELF program header
7470 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7472 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7473 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7476 if (elf_tdata (ibfd)->phdr == NULL)
7479 if (ibfd->xvec == obfd->xvec)
7481 /* Check to see if any sections in the input BFD
7482 covered by ELF program header have changed. */
7483 Elf_Internal_Phdr *segment;
7484 asection *section, *osec;
7485 unsigned int i, num_segments;
7486 Elf_Internal_Shdr *this_hdr;
7487 const struct elf_backend_data *bed;
7489 bed = get_elf_backend_data (ibfd);
7491 /* Regenerate the segment map if p_paddr is set to 0. */
7492 if (bed->want_p_paddr_set_to_zero)
7495 /* Initialize the segment mark field. */
7496 for (section = obfd->sections; section != NULL;
7497 section = section->next)
7498 section->segment_mark = FALSE;
7500 num_segments = elf_elfheader (ibfd)->e_phnum;
7501 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7505 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7506 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7507 which severly confuses things, so always regenerate the segment
7508 map in this case. */
7509 if (segment->p_paddr == 0
7510 && segment->p_memsz == 0
7511 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7514 for (section = ibfd->sections;
7515 section != NULL; section = section->next)
7517 /* We mark the output section so that we know it comes
7518 from the input BFD. */
7519 osec = section->output_section;
7521 osec->segment_mark = TRUE;
7523 /* Check if this section is covered by the segment. */
7524 this_hdr = &(elf_section_data(section)->this_hdr);
7525 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7527 /* FIXME: Check if its output section is changed or
7528 removed. What else do we need to check? */
7530 || section->flags != osec->flags
7531 || section->lma != osec->lma
7532 || section->vma != osec->vma
7533 || section->size != osec->size
7534 || section->rawsize != osec->rawsize
7535 || section->alignment_power != osec->alignment_power)
7541 /* Check to see if any output section do not come from the
7543 for (section = obfd->sections; section != NULL;
7544 section = section->next)
7546 if (!section->segment_mark)
7549 section->segment_mark = FALSE;
7552 return copy_elf_program_header (ibfd, obfd);
7556 if (ibfd->xvec == obfd->xvec)
7558 /* When rewriting program header, set the output maxpagesize to
7559 the maximum alignment of input PT_LOAD segments. */
7560 Elf_Internal_Phdr *segment;
7562 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7563 bfd_vma maxpagesize = 0;
7565 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7568 if (segment->p_type == PT_LOAD
7569 && maxpagesize < segment->p_align)
7571 /* PR 17512: file: f17299af. */
7572 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7573 /* xgettext:c-format */
7574 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7575 PRIx64 " is too large"),
7576 ibfd, (uint64_t) segment->p_align);
7578 maxpagesize = segment->p_align;
7581 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7582 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7585 return rewrite_elf_program_header (ibfd, obfd);
7588 /* Initialize private output section information from input section. */
7591 _bfd_elf_init_private_section_data (bfd *ibfd,
7595 struct bfd_link_info *link_info)
7598 Elf_Internal_Shdr *ihdr, *ohdr;
7599 bfd_boolean final_link = (link_info != NULL
7600 && !bfd_link_relocatable (link_info));
7602 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7603 || obfd->xvec->flavour != bfd_target_elf_flavour)
7606 BFD_ASSERT (elf_section_data (osec) != NULL);
7608 /* For objcopy and relocatable link, don't copy the output ELF
7609 section type from input if the output BFD section flags have been
7610 set to something different. For a final link allow some flags
7611 that the linker clears to differ. */
7612 if (elf_section_type (osec) == SHT_NULL
7613 && (osec->flags == isec->flags
7615 && ((osec->flags ^ isec->flags)
7616 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7617 elf_section_type (osec) = elf_section_type (isec);
7619 /* FIXME: Is this correct for all OS/PROC specific flags? */
7620 elf_section_flags (osec) |= (elf_section_flags (isec)
7621 & (SHF_MASKOS | SHF_MASKPROC));
7623 /* Copy sh_info from input for mbind section. */
7624 if (elf_section_flags (isec) & SHF_GNU_MBIND)
7625 elf_section_data (osec)->this_hdr.sh_info
7626 = elf_section_data (isec)->this_hdr.sh_info;
7628 /* Set things up for objcopy and relocatable link. The output
7629 SHT_GROUP section will have its elf_next_in_group pointing back
7630 to the input group members. Ignore linker created group section.
7631 See elfNN_ia64_object_p in elfxx-ia64.c. */
7632 if ((link_info == NULL
7633 || !link_info->resolve_section_groups)
7634 && (elf_sec_group (isec) == NULL
7635 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7637 if (elf_section_flags (isec) & SHF_GROUP)
7638 elf_section_flags (osec) |= SHF_GROUP;
7639 elf_next_in_group (osec) = elf_next_in_group (isec);
7640 elf_section_data (osec)->group = elf_section_data (isec)->group;
7643 /* If not decompress, preserve SHF_COMPRESSED. */
7644 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7645 elf_section_flags (osec) |= (elf_section_flags (isec)
7648 ihdr = &elf_section_data (isec)->this_hdr;
7650 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7651 don't use the output section of the linked-to section since it
7652 may be NULL at this point. */
7653 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7655 ohdr = &elf_section_data (osec)->this_hdr;
7656 ohdr->sh_flags |= SHF_LINK_ORDER;
7657 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7660 osec->use_rela_p = isec->use_rela_p;
7665 /* Copy private section information. This copies over the entsize
7666 field, and sometimes the info field. */
7669 _bfd_elf_copy_private_section_data (bfd *ibfd,
7674 Elf_Internal_Shdr *ihdr, *ohdr;
7676 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7677 || obfd->xvec->flavour != bfd_target_elf_flavour)
7680 ihdr = &elf_section_data (isec)->this_hdr;
7681 ohdr = &elf_section_data (osec)->this_hdr;
7683 ohdr->sh_entsize = ihdr->sh_entsize;
7685 if (ihdr->sh_type == SHT_SYMTAB
7686 || ihdr->sh_type == SHT_DYNSYM
7687 || ihdr->sh_type == SHT_GNU_verneed
7688 || ihdr->sh_type == SHT_GNU_verdef)
7689 ohdr->sh_info = ihdr->sh_info;
7691 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7695 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7696 necessary if we are removing either the SHT_GROUP section or any of
7697 the group member sections. DISCARDED is the value that a section's
7698 output_section has if the section will be discarded, NULL when this
7699 function is called from objcopy, bfd_abs_section_ptr when called
7703 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7707 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7708 if (elf_section_type (isec) == SHT_GROUP)
7710 asection *first = elf_next_in_group (isec);
7711 asection *s = first;
7712 bfd_size_type removed = 0;
7716 /* If this member section is being output but the
7717 SHT_GROUP section is not, then clear the group info
7718 set up by _bfd_elf_copy_private_section_data. */
7719 if (s->output_section != discarded
7720 && isec->output_section == discarded)
7722 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7723 elf_group_name (s->output_section) = NULL;
7725 /* Conversely, if the member section is not being output
7726 but the SHT_GROUP section is, then adjust its size. */
7727 else if (s->output_section == discarded
7728 && isec->output_section != discarded)
7730 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7732 if (elf_sec->rel.hdr != NULL
7733 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7735 if (elf_sec->rela.hdr != NULL
7736 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7739 s = elf_next_in_group (s);
7745 if (discarded != NULL)
7747 /* If we've been called for ld -r, then we need to
7748 adjust the input section size. */
7749 if (isec->rawsize == 0)
7750 isec->rawsize = isec->size;
7751 isec->size = isec->rawsize - removed;
7752 if (isec->size <= 4)
7755 isec->flags |= SEC_EXCLUDE;
7760 /* Adjust the output section size when called from
7762 isec->output_section->size -= removed;
7763 if (isec->output_section->size <= 4)
7765 isec->output_section->size = 0;
7766 isec->output_section->flags |= SEC_EXCLUDE;
7775 /* Copy private header information. */
7778 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7780 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7781 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7784 /* Copy over private BFD data if it has not already been copied.
7785 This must be done here, rather than in the copy_private_bfd_data
7786 entry point, because the latter is called after the section
7787 contents have been set, which means that the program headers have
7788 already been worked out. */
7789 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7791 if (! copy_private_bfd_data (ibfd, obfd))
7795 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7798 /* Copy private symbol information. If this symbol is in a section
7799 which we did not map into a BFD section, try to map the section
7800 index correctly. We use special macro definitions for the mapped
7801 section indices; these definitions are interpreted by the
7802 swap_out_syms function. */
7804 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7805 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7806 #define MAP_STRTAB (SHN_HIOS + 3)
7807 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7808 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7811 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7816 elf_symbol_type *isym, *osym;
7818 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7819 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7822 isym = elf_symbol_from (ibfd, isymarg);
7823 osym = elf_symbol_from (obfd, osymarg);
7826 && isym->internal_elf_sym.st_shndx != 0
7828 && bfd_is_abs_section (isym->symbol.section))
7832 shndx = isym->internal_elf_sym.st_shndx;
7833 if (shndx == elf_onesymtab (ibfd))
7834 shndx = MAP_ONESYMTAB;
7835 else if (shndx == elf_dynsymtab (ibfd))
7836 shndx = MAP_DYNSYMTAB;
7837 else if (shndx == elf_strtab_sec (ibfd))
7839 else if (shndx == elf_shstrtab_sec (ibfd))
7840 shndx = MAP_SHSTRTAB;
7841 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7842 shndx = MAP_SYM_SHNDX;
7843 osym->internal_elf_sym.st_shndx = shndx;
7849 /* Swap out the symbols. */
7852 swap_out_syms (bfd *abfd,
7853 struct elf_strtab_hash **sttp,
7856 const struct elf_backend_data *bed;
7859 struct elf_strtab_hash *stt;
7860 Elf_Internal_Shdr *symtab_hdr;
7861 Elf_Internal_Shdr *symtab_shndx_hdr;
7862 Elf_Internal_Shdr *symstrtab_hdr;
7863 struct elf_sym_strtab *symstrtab;
7864 bfd_byte *outbound_syms;
7865 bfd_byte *outbound_shndx;
7866 unsigned long outbound_syms_index;
7867 unsigned long outbound_shndx_index;
7869 unsigned int num_locals;
7871 bfd_boolean name_local_sections;
7873 if (!elf_map_symbols (abfd, &num_locals))
7876 /* Dump out the symtabs. */
7877 stt = _bfd_elf_strtab_init ();
7881 bed = get_elf_backend_data (abfd);
7882 symcount = bfd_get_symcount (abfd);
7883 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7884 symtab_hdr->sh_type = SHT_SYMTAB;
7885 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7886 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7887 symtab_hdr->sh_info = num_locals + 1;
7888 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7890 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7891 symstrtab_hdr->sh_type = SHT_STRTAB;
7893 /* Allocate buffer to swap out the .strtab section. */
7894 symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
7895 * sizeof (*symstrtab));
7896 if (symstrtab == NULL)
7898 _bfd_elf_strtab_free (stt);
7902 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7903 bed->s->sizeof_sym);
7904 if (outbound_syms == NULL)
7907 _bfd_elf_strtab_free (stt);
7911 symtab_hdr->contents = outbound_syms;
7912 outbound_syms_index = 0;
7914 outbound_shndx = NULL;
7915 outbound_shndx_index = 0;
7917 if (elf_symtab_shndx_list (abfd))
7919 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7920 if (symtab_shndx_hdr->sh_name != 0)
7922 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7923 outbound_shndx = (bfd_byte *)
7924 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7925 if (outbound_shndx == NULL)
7928 symtab_shndx_hdr->contents = outbound_shndx;
7929 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7930 symtab_shndx_hdr->sh_size = amt;
7931 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7932 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7934 /* FIXME: What about any other headers in the list ? */
7937 /* Now generate the data (for "contents"). */
7939 /* Fill in zeroth symbol and swap it out. */
7940 Elf_Internal_Sym sym;
7946 sym.st_shndx = SHN_UNDEF;
7947 sym.st_target_internal = 0;
7948 symstrtab[0].sym = sym;
7949 symstrtab[0].dest_index = outbound_syms_index;
7950 symstrtab[0].destshndx_index = outbound_shndx_index;
7951 outbound_syms_index++;
7952 if (outbound_shndx != NULL)
7953 outbound_shndx_index++;
7957 = (bed->elf_backend_name_local_section_symbols
7958 && bed->elf_backend_name_local_section_symbols (abfd));
7960 syms = bfd_get_outsymbols (abfd);
7961 for (idx = 0; idx < symcount;)
7963 Elf_Internal_Sym sym;
7964 bfd_vma value = syms[idx]->value;
7965 elf_symbol_type *type_ptr;
7966 flagword flags = syms[idx]->flags;
7969 if (!name_local_sections
7970 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
7972 /* Local section symbols have no name. */
7973 sym.st_name = (unsigned long) -1;
7977 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7978 to get the final offset for st_name. */
7980 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
7982 if (sym.st_name == (unsigned long) -1)
7986 type_ptr = elf_symbol_from (abfd, syms[idx]);
7988 if ((flags & BSF_SECTION_SYM) == 0
7989 && bfd_is_com_section (syms[idx]->section))
7991 /* ELF common symbols put the alignment into the `value' field,
7992 and the size into the `size' field. This is backwards from
7993 how BFD handles it, so reverse it here. */
7994 sym.st_size = value;
7995 if (type_ptr == NULL
7996 || type_ptr->internal_elf_sym.st_value == 0)
7997 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
7999 sym.st_value = type_ptr->internal_elf_sym.st_value;
8000 sym.st_shndx = _bfd_elf_section_from_bfd_section
8001 (abfd, syms[idx]->section);
8005 asection *sec = syms[idx]->section;
8008 if (sec->output_section)
8010 value += sec->output_offset;
8011 sec = sec->output_section;
8014 /* Don't add in the section vma for relocatable output. */
8015 if (! relocatable_p)
8017 sym.st_value = value;
8018 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8020 if (bfd_is_abs_section (sec)
8022 && type_ptr->internal_elf_sym.st_shndx != 0)
8024 /* This symbol is in a real ELF section which we did
8025 not create as a BFD section. Undo the mapping done
8026 by copy_private_symbol_data. */
8027 shndx = type_ptr->internal_elf_sym.st_shndx;
8031 shndx = elf_onesymtab (abfd);
8034 shndx = elf_dynsymtab (abfd);
8037 shndx = elf_strtab_sec (abfd);
8040 shndx = elf_shstrtab_sec (abfd);
8043 if (elf_symtab_shndx_list (abfd))
8044 shndx = elf_symtab_shndx_list (abfd)->ndx;
8053 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8055 if (shndx == SHN_BAD)
8059 /* Writing this would be a hell of a lot easier if
8060 we had some decent documentation on bfd, and
8061 knew what to expect of the library, and what to
8062 demand of applications. For example, it
8063 appears that `objcopy' might not set the
8064 section of a symbol to be a section that is
8065 actually in the output file. */
8066 sec2 = bfd_get_section_by_name (abfd, sec->name);
8068 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8069 if (shndx == SHN_BAD)
8071 /* xgettext:c-format */
8073 (_("unable to find equivalent output section"
8074 " for symbol '%s' from section '%s'"),
8075 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8077 bfd_set_error (bfd_error_invalid_operation);
8083 sym.st_shndx = shndx;
8086 if ((flags & BSF_THREAD_LOCAL) != 0)
8088 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8089 type = STT_GNU_IFUNC;
8090 else if ((flags & BSF_FUNCTION) != 0)
8092 else if ((flags & BSF_OBJECT) != 0)
8094 else if ((flags & BSF_RELC) != 0)
8096 else if ((flags & BSF_SRELC) != 0)
8101 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8104 /* Processor-specific types. */
8105 if (type_ptr != NULL
8106 && bed->elf_backend_get_symbol_type)
8107 type = ((*bed->elf_backend_get_symbol_type)
8108 (&type_ptr->internal_elf_sym, type));
8110 if (flags & BSF_SECTION_SYM)
8112 if (flags & BSF_GLOBAL)
8113 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8115 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8117 else if (bfd_is_com_section (syms[idx]->section))
8119 if (type != STT_TLS)
8121 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8122 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8123 ? STT_COMMON : STT_OBJECT);
8125 type = ((flags & BSF_ELF_COMMON) != 0
8126 ? STT_COMMON : STT_OBJECT);
8128 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8130 else if (bfd_is_und_section (syms[idx]->section))
8131 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8135 else if (flags & BSF_FILE)
8136 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8139 int bind = STB_LOCAL;
8141 if (flags & BSF_LOCAL)
8143 else if (flags & BSF_GNU_UNIQUE)
8144 bind = STB_GNU_UNIQUE;
8145 else if (flags & BSF_WEAK)
8147 else if (flags & BSF_GLOBAL)
8150 sym.st_info = ELF_ST_INFO (bind, type);
8153 if (type_ptr != NULL)
8155 sym.st_other = type_ptr->internal_elf_sym.st_other;
8156 sym.st_target_internal
8157 = type_ptr->internal_elf_sym.st_target_internal;
8162 sym.st_target_internal = 0;
8166 symstrtab[idx].sym = sym;
8167 symstrtab[idx].dest_index = outbound_syms_index;
8168 symstrtab[idx].destshndx_index = outbound_shndx_index;
8170 outbound_syms_index++;
8171 if (outbound_shndx != NULL)
8172 outbound_shndx_index++;
8175 /* Finalize the .strtab section. */
8176 _bfd_elf_strtab_finalize (stt);
8178 /* Swap out the .strtab section. */
8179 for (idx = 0; idx <= symcount; idx++)
8181 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8182 if (elfsym->sym.st_name == (unsigned long) -1)
8183 elfsym->sym.st_name = 0;
8185 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8186 elfsym->sym.st_name);
8187 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8189 + (elfsym->dest_index
8190 * bed->s->sizeof_sym)),
8192 + (elfsym->destshndx_index
8193 * sizeof (Elf_External_Sym_Shndx))));
8198 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8199 symstrtab_hdr->sh_type = SHT_STRTAB;
8200 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8201 symstrtab_hdr->sh_addr = 0;
8202 symstrtab_hdr->sh_entsize = 0;
8203 symstrtab_hdr->sh_link = 0;
8204 symstrtab_hdr->sh_info = 0;
8205 symstrtab_hdr->sh_addralign = 1;
8210 /* Return the number of bytes required to hold the symtab vector.
8212 Note that we base it on the count plus 1, since we will null terminate
8213 the vector allocated based on this size. However, the ELF symbol table
8214 always has a dummy entry as symbol #0, so it ends up even. */
8217 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8219 bfd_size_type symcount;
8221 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8223 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8224 if (symcount >= LONG_MAX / sizeof (asymbol *))
8226 bfd_set_error (bfd_error_file_too_big);
8229 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8231 symtab_size -= sizeof (asymbol *);
8237 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8239 bfd_size_type symcount;
8241 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8243 if (elf_dynsymtab (abfd) == 0)
8245 bfd_set_error (bfd_error_invalid_operation);
8249 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8250 if (symcount >= LONG_MAX / sizeof (asymbol *))
8252 bfd_set_error (bfd_error_file_too_big);
8255 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8257 symtab_size -= sizeof (asymbol *);
8263 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8266 return (asect->reloc_count + 1) * sizeof (arelent *);
8269 /* Canonicalize the relocs. */
8272 _bfd_elf_canonicalize_reloc (bfd *abfd,
8279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8281 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8284 tblptr = section->relocation;
8285 for (i = 0; i < section->reloc_count; i++)
8286 *relptr++ = tblptr++;
8290 return section->reloc_count;
8294 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8296 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8297 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8300 bfd_get_symcount (abfd) = symcount;
8305 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8306 asymbol **allocation)
8308 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8309 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8312 bfd_get_dynamic_symcount (abfd) = symcount;
8316 /* Return the size required for the dynamic reloc entries. Any loadable
8317 section that was actually installed in the BFD, and has type SHT_REL
8318 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8319 dynamic reloc section. */
8322 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8324 bfd_size_type count;
8327 if (elf_dynsymtab (abfd) == 0)
8329 bfd_set_error (bfd_error_invalid_operation);
8334 for (s = abfd->sections; s != NULL; s = s->next)
8335 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8336 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8337 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8339 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8340 if (count > LONG_MAX / sizeof (arelent *))
8342 bfd_set_error (bfd_error_file_too_big);
8346 return count * sizeof (arelent *);
8349 /* Canonicalize the dynamic relocation entries. Note that we return the
8350 dynamic relocations as a single block, although they are actually
8351 associated with particular sections; the interface, which was
8352 designed for SunOS style shared libraries, expects that there is only
8353 one set of dynamic relocs. Any loadable section that was actually
8354 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8355 dynamic symbol table, is considered to be a dynamic reloc section. */
8358 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8362 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8366 if (elf_dynsymtab (abfd) == 0)
8368 bfd_set_error (bfd_error_invalid_operation);
8372 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8374 for (s = abfd->sections; s != NULL; s = s->next)
8376 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8377 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8378 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8383 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8385 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8387 for (i = 0; i < count; i++)
8398 /* Read in the version information. */
8401 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8403 bfd_byte *contents = NULL;
8404 unsigned int freeidx = 0;
8406 if (elf_dynverref (abfd) != 0)
8408 Elf_Internal_Shdr *hdr;
8409 Elf_External_Verneed *everneed;
8410 Elf_Internal_Verneed *iverneed;
8412 bfd_byte *contents_end;
8414 hdr = &elf_tdata (abfd)->dynverref_hdr;
8416 if (hdr->sh_info == 0
8417 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8419 error_return_bad_verref:
8421 (_("%pB: .gnu.version_r invalid entry"), abfd);
8422 bfd_set_error (bfd_error_bad_value);
8423 error_return_verref:
8424 elf_tdata (abfd)->verref = NULL;
8425 elf_tdata (abfd)->cverrefs = 0;
8429 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8430 if (contents == NULL)
8431 goto error_return_verref;
8433 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8434 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8435 goto error_return_verref;
8437 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8438 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8440 if (elf_tdata (abfd)->verref == NULL)
8441 goto error_return_verref;
8443 BFD_ASSERT (sizeof (Elf_External_Verneed)
8444 == sizeof (Elf_External_Vernaux));
8445 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8446 everneed = (Elf_External_Verneed *) contents;
8447 iverneed = elf_tdata (abfd)->verref;
8448 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8450 Elf_External_Vernaux *evernaux;
8451 Elf_Internal_Vernaux *ivernaux;
8454 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8456 iverneed->vn_bfd = abfd;
8458 iverneed->vn_filename =
8459 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8461 if (iverneed->vn_filename == NULL)
8462 goto error_return_bad_verref;
8464 if (iverneed->vn_cnt == 0)
8465 iverneed->vn_auxptr = NULL;
8468 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8469 bfd_alloc2 (abfd, iverneed->vn_cnt,
8470 sizeof (Elf_Internal_Vernaux));
8471 if (iverneed->vn_auxptr == NULL)
8472 goto error_return_verref;
8475 if (iverneed->vn_aux
8476 > (size_t) (contents_end - (bfd_byte *) everneed))
8477 goto error_return_bad_verref;
8479 evernaux = ((Elf_External_Vernaux *)
8480 ((bfd_byte *) everneed + iverneed->vn_aux));
8481 ivernaux = iverneed->vn_auxptr;
8482 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8484 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8486 ivernaux->vna_nodename =
8487 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8488 ivernaux->vna_name);
8489 if (ivernaux->vna_nodename == NULL)
8490 goto error_return_bad_verref;
8492 if (ivernaux->vna_other > freeidx)
8493 freeidx = ivernaux->vna_other;
8495 ivernaux->vna_nextptr = NULL;
8496 if (ivernaux->vna_next == 0)
8498 iverneed->vn_cnt = j + 1;
8501 if (j + 1 < iverneed->vn_cnt)
8502 ivernaux->vna_nextptr = ivernaux + 1;
8504 if (ivernaux->vna_next
8505 > (size_t) (contents_end - (bfd_byte *) evernaux))
8506 goto error_return_bad_verref;
8508 evernaux = ((Elf_External_Vernaux *)
8509 ((bfd_byte *) evernaux + ivernaux->vna_next));
8512 iverneed->vn_nextref = NULL;
8513 if (iverneed->vn_next == 0)
8515 if (i + 1 < hdr->sh_info)
8516 iverneed->vn_nextref = iverneed + 1;
8518 if (iverneed->vn_next
8519 > (size_t) (contents_end - (bfd_byte *) everneed))
8520 goto error_return_bad_verref;
8522 everneed = ((Elf_External_Verneed *)
8523 ((bfd_byte *) everneed + iverneed->vn_next));
8525 elf_tdata (abfd)->cverrefs = i;
8531 if (elf_dynverdef (abfd) != 0)
8533 Elf_Internal_Shdr *hdr;
8534 Elf_External_Verdef *everdef;
8535 Elf_Internal_Verdef *iverdef;
8536 Elf_Internal_Verdef *iverdefarr;
8537 Elf_Internal_Verdef iverdefmem;
8539 unsigned int maxidx;
8540 bfd_byte *contents_end_def, *contents_end_aux;
8542 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8544 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8546 error_return_bad_verdef:
8548 (_("%pB: .gnu.version_d invalid entry"), abfd);
8549 bfd_set_error (bfd_error_bad_value);
8550 error_return_verdef:
8551 elf_tdata (abfd)->verdef = NULL;
8552 elf_tdata (abfd)->cverdefs = 0;
8556 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8557 if (contents == NULL)
8558 goto error_return_verdef;
8559 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8560 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8561 goto error_return_verdef;
8563 BFD_ASSERT (sizeof (Elf_External_Verdef)
8564 >= sizeof (Elf_External_Verdaux));
8565 contents_end_def = contents + hdr->sh_size
8566 - sizeof (Elf_External_Verdef);
8567 contents_end_aux = contents + hdr->sh_size
8568 - sizeof (Elf_External_Verdaux);
8570 /* We know the number of entries in the section but not the maximum
8571 index. Therefore we have to run through all entries and find
8573 everdef = (Elf_External_Verdef *) contents;
8575 for (i = 0; i < hdr->sh_info; ++i)
8577 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8579 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8580 goto error_return_bad_verdef;
8581 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8582 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8584 if (iverdefmem.vd_next == 0)
8587 if (iverdefmem.vd_next
8588 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8589 goto error_return_bad_verdef;
8591 everdef = ((Elf_External_Verdef *)
8592 ((bfd_byte *) everdef + iverdefmem.vd_next));
8595 if (default_imported_symver)
8597 if (freeidx > maxidx)
8603 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8604 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8605 if (elf_tdata (abfd)->verdef == NULL)
8606 goto error_return_verdef;
8608 elf_tdata (abfd)->cverdefs = maxidx;
8610 everdef = (Elf_External_Verdef *) contents;
8611 iverdefarr = elf_tdata (abfd)->verdef;
8612 for (i = 0; i < hdr->sh_info; i++)
8614 Elf_External_Verdaux *everdaux;
8615 Elf_Internal_Verdaux *iverdaux;
8618 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8620 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8621 goto error_return_bad_verdef;
8623 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8624 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8626 iverdef->vd_bfd = abfd;
8628 if (iverdef->vd_cnt == 0)
8629 iverdef->vd_auxptr = NULL;
8632 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8633 bfd_alloc2 (abfd, iverdef->vd_cnt,
8634 sizeof (Elf_Internal_Verdaux));
8635 if (iverdef->vd_auxptr == NULL)
8636 goto error_return_verdef;
8640 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8641 goto error_return_bad_verdef;
8643 everdaux = ((Elf_External_Verdaux *)
8644 ((bfd_byte *) everdef + iverdef->vd_aux));
8645 iverdaux = iverdef->vd_auxptr;
8646 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8648 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8650 iverdaux->vda_nodename =
8651 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8652 iverdaux->vda_name);
8653 if (iverdaux->vda_nodename == NULL)
8654 goto error_return_bad_verdef;
8656 iverdaux->vda_nextptr = NULL;
8657 if (iverdaux->vda_next == 0)
8659 iverdef->vd_cnt = j + 1;
8662 if (j + 1 < iverdef->vd_cnt)
8663 iverdaux->vda_nextptr = iverdaux + 1;
8665 if (iverdaux->vda_next
8666 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8667 goto error_return_bad_verdef;
8669 everdaux = ((Elf_External_Verdaux *)
8670 ((bfd_byte *) everdaux + iverdaux->vda_next));
8673 iverdef->vd_nodename = NULL;
8674 if (iverdef->vd_cnt)
8675 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8677 iverdef->vd_nextdef = NULL;
8678 if (iverdef->vd_next == 0)
8680 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8681 iverdef->vd_nextdef = iverdef + 1;
8683 everdef = ((Elf_External_Verdef *)
8684 ((bfd_byte *) everdef + iverdef->vd_next));
8690 else if (default_imported_symver)
8697 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8698 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8699 if (elf_tdata (abfd)->verdef == NULL)
8702 elf_tdata (abfd)->cverdefs = freeidx;
8705 /* Create a default version based on the soname. */
8706 if (default_imported_symver)
8708 Elf_Internal_Verdef *iverdef;
8709 Elf_Internal_Verdaux *iverdaux;
8711 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8713 iverdef->vd_version = VER_DEF_CURRENT;
8714 iverdef->vd_flags = 0;
8715 iverdef->vd_ndx = freeidx;
8716 iverdef->vd_cnt = 1;
8718 iverdef->vd_bfd = abfd;
8720 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8721 if (iverdef->vd_nodename == NULL)
8722 goto error_return_verdef;
8723 iverdef->vd_nextdef = NULL;
8724 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8725 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8726 if (iverdef->vd_auxptr == NULL)
8727 goto error_return_verdef;
8729 iverdaux = iverdef->vd_auxptr;
8730 iverdaux->vda_nodename = iverdef->vd_nodename;
8736 if (contents != NULL)
8742 _bfd_elf_make_empty_symbol (bfd *abfd)
8744 elf_symbol_type *newsym;
8746 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
8749 newsym->symbol.the_bfd = abfd;
8750 return &newsym->symbol;
8754 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8758 bfd_symbol_info (symbol, ret);
8761 /* Return whether a symbol name implies a local symbol. Most targets
8762 use this function for the is_local_label_name entry point, but some
8766 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8769 /* Normal local symbols start with ``.L''. */
8770 if (name[0] == '.' && name[1] == 'L')
8773 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8774 DWARF debugging symbols starting with ``..''. */
8775 if (name[0] == '.' && name[1] == '.')
8778 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8779 emitting DWARF debugging output. I suspect this is actually a
8780 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8781 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8782 underscore to be emitted on some ELF targets). For ease of use,
8783 we treat such symbols as local. */
8784 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8787 /* Treat assembler generated fake symbols, dollar local labels and
8788 forward-backward labels (aka local labels) as locals.
8789 These labels have the form:
8791 L0^A.* (fake symbols)
8793 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8795 Versions which start with .L will have already been matched above,
8796 so we only need to match the rest. */
8797 if (name[0] == 'L' && ISDIGIT (name[1]))
8799 bfd_boolean ret = FALSE;
8803 for (p = name + 2; (c = *p); p++)
8805 if (c == 1 || c == 2)
8807 if (c == 1 && p == name + 2)
8808 /* A fake symbol. */
8811 /* FIXME: We are being paranoid here and treating symbols like
8812 L0^Bfoo as if there were non-local, on the grounds that the
8813 assembler will never generate them. But can any symbol
8814 containing an ASCII value in the range 1-31 ever be anything
8815 other than some kind of local ? */
8832 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8833 asymbol *symbol ATTRIBUTE_UNUSED)
8840 _bfd_elf_set_arch_mach (bfd *abfd,
8841 enum bfd_architecture arch,
8842 unsigned long machine)
8844 /* If this isn't the right architecture for this backend, and this
8845 isn't the generic backend, fail. */
8846 if (arch != get_elf_backend_data (abfd)->arch
8847 && arch != bfd_arch_unknown
8848 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8851 return bfd_default_set_arch_mach (abfd, arch, machine);
8854 /* Find the nearest line to a particular section and offset,
8855 for error reporting. */
8858 _bfd_elf_find_nearest_line (bfd *abfd,
8862 const char **filename_ptr,
8863 const char **functionname_ptr,
8864 unsigned int *line_ptr,
8865 unsigned int *discriminator_ptr)
8869 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8870 filename_ptr, functionname_ptr,
8871 line_ptr, discriminator_ptr,
8872 dwarf_debug_sections, 0,
8873 &elf_tdata (abfd)->dwarf2_find_line_info)
8874 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8875 filename_ptr, functionname_ptr,
8878 if (!*functionname_ptr)
8879 _bfd_elf_find_function (abfd, symbols, section, offset,
8880 *filename_ptr ? NULL : filename_ptr,
8885 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8886 &found, filename_ptr,
8887 functionname_ptr, line_ptr,
8888 &elf_tdata (abfd)->line_info))
8890 if (found && (*functionname_ptr || *line_ptr))
8893 if (symbols == NULL)
8896 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8897 filename_ptr, functionname_ptr))
8904 /* Find the line for a symbol. */
8907 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8908 const char **filename_ptr, unsigned int *line_ptr)
8910 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8911 filename_ptr, NULL, line_ptr, NULL,
8912 dwarf_debug_sections, 0,
8913 &elf_tdata (abfd)->dwarf2_find_line_info);
8916 /* After a call to bfd_find_nearest_line, successive calls to
8917 bfd_find_inliner_info can be used to get source information about
8918 each level of function inlining that terminated at the address
8919 passed to bfd_find_nearest_line. Currently this is only supported
8920 for DWARF2 with appropriate DWARF3 extensions. */
8923 _bfd_elf_find_inliner_info (bfd *abfd,
8924 const char **filename_ptr,
8925 const char **functionname_ptr,
8926 unsigned int *line_ptr)
8929 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8930 functionname_ptr, line_ptr,
8931 & elf_tdata (abfd)->dwarf2_find_line_info);
8936 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
8938 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8939 int ret = bed->s->sizeof_ehdr;
8941 if (!bfd_link_relocatable (info))
8943 bfd_size_type phdr_size = elf_program_header_size (abfd);
8945 if (phdr_size == (bfd_size_type) -1)
8947 struct elf_segment_map *m;
8950 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
8951 phdr_size += bed->s->sizeof_phdr;
8954 phdr_size = get_program_header_size (abfd, info);
8957 elf_program_header_size (abfd) = phdr_size;
8965 _bfd_elf_set_section_contents (bfd *abfd,
8967 const void *location,
8969 bfd_size_type count)
8971 Elf_Internal_Shdr *hdr;
8974 if (! abfd->output_has_begun
8975 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
8981 hdr = &elf_section_data (section)->this_hdr;
8982 if (hdr->sh_offset == (file_ptr) -1)
8984 /* We must compress this section. Write output to the buffer. */
8985 unsigned char *contents = hdr->contents;
8986 if ((offset + count) > hdr->sh_size
8987 || (section->flags & SEC_ELF_COMPRESS) == 0
8988 || contents == NULL)
8990 memcpy (contents + offset, location, count);
8993 pos = hdr->sh_offset + offset;
8994 if (bfd_seek (abfd, pos, SEEK_SET) != 0
8995 || bfd_bwrite (location, count, abfd) != count)
9002 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9003 arelent *cache_ptr ATTRIBUTE_UNUSED,
9004 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9010 /* Try to convert a non-ELF reloc into an ELF one. */
9013 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9015 /* Check whether we really have an ELF howto. */
9017 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9019 bfd_reloc_code_real_type code;
9020 reloc_howto_type *howto;
9022 /* Alien reloc: Try to determine its type to replace it with an
9023 equivalent ELF reloc. */
9025 if (areloc->howto->pc_relative)
9027 switch (areloc->howto->bitsize)
9030 code = BFD_RELOC_8_PCREL;
9033 code = BFD_RELOC_12_PCREL;
9036 code = BFD_RELOC_16_PCREL;
9039 code = BFD_RELOC_24_PCREL;
9042 code = BFD_RELOC_32_PCREL;
9045 code = BFD_RELOC_64_PCREL;
9051 howto = bfd_reloc_type_lookup (abfd, code);
9053 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9055 if (howto->pcrel_offset)
9056 areloc->addend += areloc->address;
9058 areloc->addend -= areloc->address; /* addend is unsigned!! */
9063 switch (areloc->howto->bitsize)
9069 code = BFD_RELOC_14;
9072 code = BFD_RELOC_16;
9075 code = BFD_RELOC_26;
9078 code = BFD_RELOC_32;
9081 code = BFD_RELOC_64;
9087 howto = bfd_reloc_type_lookup (abfd, code);
9091 areloc->howto = howto;
9099 /* xgettext:c-format */
9100 _bfd_error_handler (_("%pB: %s unsupported"),
9101 abfd, areloc->howto->name);
9102 bfd_set_error (bfd_error_bad_value);
9107 _bfd_elf_close_and_cleanup (bfd *abfd)
9109 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9110 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
9112 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9113 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9114 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9117 return _bfd_generic_close_and_cleanup (abfd);
9120 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9121 in the relocation's offset. Thus we cannot allow any sort of sanity
9122 range-checking to interfere. There is nothing else to do in processing
9125 bfd_reloc_status_type
9126 _bfd_elf_rel_vtable_reloc_fn
9127 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9128 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9129 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9130 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9132 return bfd_reloc_ok;
9135 /* Elf core file support. Much of this only works on native
9136 toolchains, since we rely on knowing the
9137 machine-dependent procfs structure in order to pick
9138 out details about the corefile. */
9140 #ifdef HAVE_SYS_PROCFS_H
9141 /* Needed for new procfs interface on sparc-solaris. */
9142 # define _STRUCTURED_PROC 1
9143 # include <sys/procfs.h>
9146 /* Return a PID that identifies a "thread" for threaded cores, or the
9147 PID of the main process for non-threaded cores. */
9150 elfcore_make_pid (bfd *abfd)
9154 pid = elf_tdata (abfd)->core->lwpid;
9156 pid = elf_tdata (abfd)->core->pid;
9161 /* If there isn't a section called NAME, make one, using
9162 data from SECT. Note, this function will generate a
9163 reference to NAME, so you shouldn't deallocate or
9167 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9171 if (bfd_get_section_by_name (abfd, name) != NULL)
9174 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9178 sect2->size = sect->size;
9179 sect2->filepos = sect->filepos;
9180 sect2->alignment_power = sect->alignment_power;
9184 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9185 actually creates up to two pseudosections:
9186 - For the single-threaded case, a section named NAME, unless
9187 such a section already exists.
9188 - For the multi-threaded case, a section named "NAME/PID", where
9189 PID is elfcore_make_pid (abfd).
9190 Both pseudosections have identical contents. */
9192 _bfd_elfcore_make_pseudosection (bfd *abfd,
9198 char *threaded_name;
9202 /* Build the section name. */
9204 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9205 len = strlen (buf) + 1;
9206 threaded_name = (char *) bfd_alloc (abfd, len);
9207 if (threaded_name == NULL)
9209 memcpy (threaded_name, buf, len);
9211 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9216 sect->filepos = filepos;
9217 sect->alignment_power = 2;
9219 return elfcore_maybe_make_sect (abfd, name, sect);
9222 /* prstatus_t exists on:
9224 linux 2.[01] + glibc
9228 #if defined (HAVE_PRSTATUS_T)
9231 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9236 if (note->descsz == sizeof (prstatus_t))
9240 size = sizeof (prstat.pr_reg);
9241 offset = offsetof (prstatus_t, pr_reg);
9242 memcpy (&prstat, note->descdata, sizeof (prstat));
9244 /* Do not overwrite the core signal if it
9245 has already been set by another thread. */
9246 if (elf_tdata (abfd)->core->signal == 0)
9247 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9248 if (elf_tdata (abfd)->core->pid == 0)
9249 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9251 /* pr_who exists on:
9254 pr_who doesn't exist on:
9257 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9258 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9260 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9263 #if defined (HAVE_PRSTATUS32_T)
9264 else if (note->descsz == sizeof (prstatus32_t))
9266 /* 64-bit host, 32-bit corefile */
9267 prstatus32_t prstat;
9269 size = sizeof (prstat.pr_reg);
9270 offset = offsetof (prstatus32_t, pr_reg);
9271 memcpy (&prstat, note->descdata, sizeof (prstat));
9273 /* Do not overwrite the core signal if it
9274 has already been set by another thread. */
9275 if (elf_tdata (abfd)->core->signal == 0)
9276 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9277 if (elf_tdata (abfd)->core->pid == 0)
9278 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9280 /* pr_who exists on:
9283 pr_who doesn't exist on:
9286 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9287 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9289 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9292 #endif /* HAVE_PRSTATUS32_T */
9295 /* Fail - we don't know how to handle any other
9296 note size (ie. data object type). */
9300 /* Make a ".reg/999" section and a ".reg" section. */
9301 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9302 size, note->descpos + offset);
9304 #endif /* defined (HAVE_PRSTATUS_T) */
9306 /* Create a pseudosection containing the exact contents of NOTE. */
9308 elfcore_make_note_pseudosection (bfd *abfd,
9310 Elf_Internal_Note *note)
9312 return _bfd_elfcore_make_pseudosection (abfd, name,
9313 note->descsz, note->descpos);
9316 /* There isn't a consistent prfpregset_t across platforms,
9317 but it doesn't matter, because we don't have to pick this
9318 data structure apart. */
9321 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9323 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9326 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9327 type of NT_PRXFPREG. Just include the whole note's contents
9331 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9333 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9336 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9337 with a note type of NT_X86_XSTATE. Just include the whole note's
9338 contents literally. */
9341 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9343 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9347 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9349 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9353 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9355 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9359 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9361 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9365 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9367 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9371 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9373 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9377 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9379 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9383 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9385 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9389 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9391 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9395 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9397 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9401 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9403 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9407 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9409 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9413 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9415 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9419 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9421 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9425 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9427 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9431 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9433 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9437 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9439 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9443 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9445 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9449 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9451 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9455 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9457 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9461 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9463 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9467 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9469 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9473 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9475 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9479 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9481 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9485 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9487 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9491 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9493 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9497 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9499 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9503 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9505 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9509 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9511 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9515 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9517 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9521 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9523 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9527 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9529 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9533 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9535 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9539 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9541 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9544 #if defined (HAVE_PRPSINFO_T)
9545 typedef prpsinfo_t elfcore_psinfo_t;
9546 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9547 typedef prpsinfo32_t elfcore_psinfo32_t;
9551 #if defined (HAVE_PSINFO_T)
9552 typedef psinfo_t elfcore_psinfo_t;
9553 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9554 typedef psinfo32_t elfcore_psinfo32_t;
9558 /* return a malloc'ed copy of a string at START which is at
9559 most MAX bytes long, possibly without a terminating '\0'.
9560 the copy will always have a terminating '\0'. */
9563 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9566 char *end = (char *) memchr (start, '\0', max);
9574 dups = (char *) bfd_alloc (abfd, len + 1);
9578 memcpy (dups, start, len);
9584 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9586 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9588 if (note->descsz == sizeof (elfcore_psinfo_t))
9590 elfcore_psinfo_t psinfo;
9592 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9594 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9595 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9597 elf_tdata (abfd)->core->program
9598 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9599 sizeof (psinfo.pr_fname));
9601 elf_tdata (abfd)->core->command
9602 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9603 sizeof (psinfo.pr_psargs));
9605 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9606 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9608 /* 64-bit host, 32-bit corefile */
9609 elfcore_psinfo32_t psinfo;
9611 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9613 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9614 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9616 elf_tdata (abfd)->core->program
9617 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9618 sizeof (psinfo.pr_fname));
9620 elf_tdata (abfd)->core->command
9621 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9622 sizeof (psinfo.pr_psargs));
9628 /* Fail - we don't know how to handle any other
9629 note size (ie. data object type). */
9633 /* Note that for some reason, a spurious space is tacked
9634 onto the end of the args in some (at least one anyway)
9635 implementations, so strip it off if it exists. */
9638 char *command = elf_tdata (abfd)->core->command;
9639 int n = strlen (command);
9641 if (0 < n && command[n - 1] == ' ')
9642 command[n - 1] = '\0';
9647 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9649 #if defined (HAVE_PSTATUS_T)
9651 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9653 if (note->descsz == sizeof (pstatus_t)
9654 #if defined (HAVE_PXSTATUS_T)
9655 || note->descsz == sizeof (pxstatus_t)
9661 memcpy (&pstat, note->descdata, sizeof (pstat));
9663 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9665 #if defined (HAVE_PSTATUS32_T)
9666 else if (note->descsz == sizeof (pstatus32_t))
9668 /* 64-bit host, 32-bit corefile */
9671 memcpy (&pstat, note->descdata, sizeof (pstat));
9673 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9676 /* Could grab some more details from the "representative"
9677 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9678 NT_LWPSTATUS note, presumably. */
9682 #endif /* defined (HAVE_PSTATUS_T) */
9684 #if defined (HAVE_LWPSTATUS_T)
9686 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9688 lwpstatus_t lwpstat;
9694 if (note->descsz != sizeof (lwpstat)
9695 #if defined (HAVE_LWPXSTATUS_T)
9696 && note->descsz != sizeof (lwpxstatus_t)
9701 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9703 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9704 /* Do not overwrite the core signal if it has already been set by
9706 if (elf_tdata (abfd)->core->signal == 0)
9707 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9709 /* Make a ".reg/999" section. */
9711 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
9712 len = strlen (buf) + 1;
9713 name = bfd_alloc (abfd, len);
9716 memcpy (name, buf, len);
9718 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9722 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9723 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
9724 sect->filepos = note->descpos
9725 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9728 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9729 sect->size = sizeof (lwpstat.pr_reg);
9730 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9733 sect->alignment_power = 2;
9735 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9738 /* Make a ".reg2/999" section */
9740 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9741 len = strlen (buf) + 1;
9742 name = bfd_alloc (abfd, len);
9745 memcpy (name, buf, len);
9747 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9751 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9752 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9753 sect->filepos = note->descpos
9754 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9757 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9758 sect->size = sizeof (lwpstat.pr_fpreg);
9759 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9762 sect->alignment_power = 2;
9764 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9766 #endif /* defined (HAVE_LWPSTATUS_T) */
9769 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9776 int is_active_thread;
9779 if (note->descsz < 728)
9782 if (! CONST_STRNEQ (note->namedata, "win32"))
9785 type = bfd_get_32 (abfd, note->descdata);
9789 case 1 /* NOTE_INFO_PROCESS */:
9790 /* FIXME: need to add ->core->command. */
9791 /* process_info.pid */
9792 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9793 /* process_info.signal */
9794 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9797 case 2 /* NOTE_INFO_THREAD */:
9798 /* Make a ".reg/999" section. */
9799 /* thread_info.tid */
9800 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9802 len = strlen (buf) + 1;
9803 name = (char *) bfd_alloc (abfd, len);
9807 memcpy (name, buf, len);
9809 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9813 /* sizeof (thread_info.thread_context) */
9815 /* offsetof (thread_info.thread_context) */
9816 sect->filepos = note->descpos + 12;
9817 sect->alignment_power = 2;
9819 /* thread_info.is_active_thread */
9820 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9822 if (is_active_thread)
9823 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9827 case 3 /* NOTE_INFO_MODULE */:
9828 /* Make a ".module/xxxxxxxx" section. */
9829 /* module_info.base_address */
9830 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9831 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9833 len = strlen (buf) + 1;
9834 name = (char *) bfd_alloc (abfd, len);
9838 memcpy (name, buf, len);
9840 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9845 sect->size = note->descsz;
9846 sect->filepos = note->descpos;
9847 sect->alignment_power = 2;
9858 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9860 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9868 if (bed->elf_backend_grok_prstatus)
9869 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
9871 #if defined (HAVE_PRSTATUS_T)
9872 return elfcore_grok_prstatus (abfd, note);
9877 #if defined (HAVE_PSTATUS_T)
9879 return elfcore_grok_pstatus (abfd, note);
9882 #if defined (HAVE_LWPSTATUS_T)
9884 return elfcore_grok_lwpstatus (abfd, note);
9887 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9888 return elfcore_grok_prfpreg (abfd, note);
9890 case NT_WIN32PSTATUS:
9891 return elfcore_grok_win32pstatus (abfd, note);
9893 case NT_PRXFPREG: /* Linux SSE extension */
9894 if (note->namesz == 6
9895 && strcmp (note->namedata, "LINUX") == 0)
9896 return elfcore_grok_prxfpreg (abfd, note);
9900 case NT_X86_XSTATE: /* Linux XSAVE extension */
9901 if (note->namesz == 6
9902 && strcmp (note->namedata, "LINUX") == 0)
9903 return elfcore_grok_xstatereg (abfd, note);
9908 if (note->namesz == 6
9909 && strcmp (note->namedata, "LINUX") == 0)
9910 return elfcore_grok_ppc_vmx (abfd, note);
9915 if (note->namesz == 6
9916 && strcmp (note->namedata, "LINUX") == 0)
9917 return elfcore_grok_ppc_vsx (abfd, note);
9922 if (note->namesz == 6
9923 && strcmp (note->namedata, "LINUX") == 0)
9924 return elfcore_grok_ppc_tar (abfd, note);
9929 if (note->namesz == 6
9930 && strcmp (note->namedata, "LINUX") == 0)
9931 return elfcore_grok_ppc_ppr (abfd, note);
9936 if (note->namesz == 6
9937 && strcmp (note->namedata, "LINUX") == 0)
9938 return elfcore_grok_ppc_dscr (abfd, note);
9943 if (note->namesz == 6
9944 && strcmp (note->namedata, "LINUX") == 0)
9945 return elfcore_grok_ppc_ebb (abfd, note);
9950 if (note->namesz == 6
9951 && strcmp (note->namedata, "LINUX") == 0)
9952 return elfcore_grok_ppc_pmu (abfd, note);
9956 case NT_PPC_TM_CGPR:
9957 if (note->namesz == 6
9958 && strcmp (note->namedata, "LINUX") == 0)
9959 return elfcore_grok_ppc_tm_cgpr (abfd, note);
9963 case NT_PPC_TM_CFPR:
9964 if (note->namesz == 6
9965 && strcmp (note->namedata, "LINUX") == 0)
9966 return elfcore_grok_ppc_tm_cfpr (abfd, note);
9970 case NT_PPC_TM_CVMX:
9971 if (note->namesz == 6
9972 && strcmp (note->namedata, "LINUX") == 0)
9973 return elfcore_grok_ppc_tm_cvmx (abfd, note);
9977 case NT_PPC_TM_CVSX:
9978 if (note->namesz == 6
9979 && strcmp (note->namedata, "LINUX") == 0)
9980 return elfcore_grok_ppc_tm_cvsx (abfd, note);
9985 if (note->namesz == 6
9986 && strcmp (note->namedata, "LINUX") == 0)
9987 return elfcore_grok_ppc_tm_spr (abfd, note);
9991 case NT_PPC_TM_CTAR:
9992 if (note->namesz == 6
9993 && strcmp (note->namedata, "LINUX") == 0)
9994 return elfcore_grok_ppc_tm_ctar (abfd, note);
9998 case NT_PPC_TM_CPPR:
9999 if (note->namesz == 6
10000 && strcmp (note->namedata, "LINUX") == 0)
10001 return elfcore_grok_ppc_tm_cppr (abfd, note);
10005 case NT_PPC_TM_CDSCR:
10006 if (note->namesz == 6
10007 && strcmp (note->namedata, "LINUX") == 0)
10008 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10012 case NT_S390_HIGH_GPRS:
10013 if (note->namesz == 6
10014 && strcmp (note->namedata, "LINUX") == 0)
10015 return elfcore_grok_s390_high_gprs (abfd, note);
10019 case NT_S390_TIMER:
10020 if (note->namesz == 6
10021 && strcmp (note->namedata, "LINUX") == 0)
10022 return elfcore_grok_s390_timer (abfd, note);
10026 case NT_S390_TODCMP:
10027 if (note->namesz == 6
10028 && strcmp (note->namedata, "LINUX") == 0)
10029 return elfcore_grok_s390_todcmp (abfd, note);
10033 case NT_S390_TODPREG:
10034 if (note->namesz == 6
10035 && strcmp (note->namedata, "LINUX") == 0)
10036 return elfcore_grok_s390_todpreg (abfd, note);
10041 if (note->namesz == 6
10042 && strcmp (note->namedata, "LINUX") == 0)
10043 return elfcore_grok_s390_ctrs (abfd, note);
10047 case NT_S390_PREFIX:
10048 if (note->namesz == 6
10049 && strcmp (note->namedata, "LINUX") == 0)
10050 return elfcore_grok_s390_prefix (abfd, note);
10054 case NT_S390_LAST_BREAK:
10055 if (note->namesz == 6
10056 && strcmp (note->namedata, "LINUX") == 0)
10057 return elfcore_grok_s390_last_break (abfd, note);
10061 case NT_S390_SYSTEM_CALL:
10062 if (note->namesz == 6
10063 && strcmp (note->namedata, "LINUX") == 0)
10064 return elfcore_grok_s390_system_call (abfd, note);
10069 if (note->namesz == 6
10070 && strcmp (note->namedata, "LINUX") == 0)
10071 return elfcore_grok_s390_tdb (abfd, note);
10075 case NT_S390_VXRS_LOW:
10076 if (note->namesz == 6
10077 && strcmp (note->namedata, "LINUX") == 0)
10078 return elfcore_grok_s390_vxrs_low (abfd, note);
10082 case NT_S390_VXRS_HIGH:
10083 if (note->namesz == 6
10084 && strcmp (note->namedata, "LINUX") == 0)
10085 return elfcore_grok_s390_vxrs_high (abfd, note);
10089 case NT_S390_GS_CB:
10090 if (note->namesz == 6
10091 && strcmp (note->namedata, "LINUX") == 0)
10092 return elfcore_grok_s390_gs_cb (abfd, note);
10096 case NT_S390_GS_BC:
10097 if (note->namesz == 6
10098 && strcmp (note->namedata, "LINUX") == 0)
10099 return elfcore_grok_s390_gs_bc (abfd, note);
10104 if (note->namesz == 6
10105 && strcmp (note->namedata, "LINUX") == 0)
10106 return elfcore_grok_arm_vfp (abfd, note);
10111 if (note->namesz == 6
10112 && strcmp (note->namedata, "LINUX") == 0)
10113 return elfcore_grok_aarch_tls (abfd, note);
10117 case NT_ARM_HW_BREAK:
10118 if (note->namesz == 6
10119 && strcmp (note->namedata, "LINUX") == 0)
10120 return elfcore_grok_aarch_hw_break (abfd, note);
10124 case NT_ARM_HW_WATCH:
10125 if (note->namesz == 6
10126 && strcmp (note->namedata, "LINUX") == 0)
10127 return elfcore_grok_aarch_hw_watch (abfd, note);
10132 if (note->namesz == 6
10133 && strcmp (note->namedata, "LINUX") == 0)
10134 return elfcore_grok_aarch_sve (abfd, note);
10140 if (bed->elf_backend_grok_psinfo)
10141 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10143 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10144 return elfcore_grok_psinfo (abfd, note);
10151 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10156 sect->size = note->descsz;
10157 sect->filepos = note->descpos;
10158 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10164 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10168 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10175 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10177 struct bfd_build_id* build_id;
10179 if (note->descsz == 0)
10182 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10183 if (build_id == NULL)
10186 build_id->size = note->descsz;
10187 memcpy (build_id->data, note->descdata, note->descsz);
10188 abfd->build_id = build_id;
10194 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10196 switch (note->type)
10201 case NT_GNU_PROPERTY_TYPE_0:
10202 return _bfd_elf_parse_gnu_properties (abfd, note);
10204 case NT_GNU_BUILD_ID:
10205 return elfobj_grok_gnu_build_id (abfd, note);
10210 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10212 struct sdt_note *cur =
10213 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
10216 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10217 cur->size = (bfd_size_type) note->descsz;
10218 memcpy (cur->data, note->descdata, note->descsz);
10220 elf_tdata (abfd)->sdt_note_head = cur;
10226 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10228 switch (note->type)
10231 return elfobj_grok_stapsdt_note_1 (abfd, note);
10239 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10243 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10246 if (note->descsz < 108)
10251 if (note->descsz < 120)
10259 /* Check for version 1 in pr_version. */
10260 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10265 /* Skip over pr_psinfosz. */
10266 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10270 offset += 4; /* Padding before pr_psinfosz. */
10274 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10275 elf_tdata (abfd)->core->program
10276 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10279 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10280 elf_tdata (abfd)->core->command
10281 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10284 /* Padding before pr_pid. */
10287 /* The pr_pid field was added in version "1a". */
10288 if (note->descsz < offset + 4)
10291 elf_tdata (abfd)->core->pid
10292 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10298 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10304 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10305 Also compute minimum size of this note. */
10306 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10310 min_size = offset + (4 * 2) + 4 + 4 + 4;
10314 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10315 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10322 if (note->descsz < min_size)
10325 /* Check for version 1 in pr_version. */
10326 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10329 /* Extract size of pr_reg from pr_gregsetsz. */
10330 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10331 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10333 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10338 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10342 /* Skip over pr_osreldate. */
10345 /* Read signal from pr_cursig. */
10346 if (elf_tdata (abfd)->core->signal == 0)
10347 elf_tdata (abfd)->core->signal
10348 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10351 /* Read TID from pr_pid. */
10352 elf_tdata (abfd)->core->lwpid
10353 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10356 /* Padding before pr_reg. */
10357 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10360 /* Make sure that there is enough data remaining in the note. */
10361 if ((note->descsz - offset) < size)
10364 /* Make a ".reg/999" section and a ".reg" section. */
10365 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10366 size, note->descpos + offset);
10370 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10374 switch (note->type)
10377 if (bed->elf_backend_grok_freebsd_prstatus)
10378 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10380 return elfcore_grok_freebsd_prstatus (abfd, note);
10383 return elfcore_grok_prfpreg (abfd, note);
10386 return elfcore_grok_freebsd_psinfo (abfd, note);
10388 case NT_FREEBSD_THRMISC:
10389 if (note->namesz == 8)
10390 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10394 case NT_FREEBSD_PROCSTAT_PROC:
10395 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10398 case NT_FREEBSD_PROCSTAT_FILES:
10399 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10402 case NT_FREEBSD_PROCSTAT_VMMAP:
10403 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10406 case NT_FREEBSD_PROCSTAT_AUXV:
10408 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10413 sect->size = note->descsz - 4;
10414 sect->filepos = note->descpos + 4;
10415 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10420 case NT_X86_XSTATE:
10421 if (note->namesz == 8)
10422 return elfcore_grok_xstatereg (abfd, note);
10426 case NT_FREEBSD_PTLWPINFO:
10427 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10431 return elfcore_grok_arm_vfp (abfd, note);
10439 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10443 cp = strchr (note->namedata, '@');
10446 *lwpidp = atoi(cp + 1);
10453 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10455 if (note->descsz <= 0x7c + 31)
10458 /* Signal number at offset 0x08. */
10459 elf_tdata (abfd)->core->signal
10460 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10462 /* Process ID at offset 0x50. */
10463 elf_tdata (abfd)->core->pid
10464 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10466 /* Command name at 0x7c (max 32 bytes, including nul). */
10467 elf_tdata (abfd)->core->command
10468 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10470 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10475 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10479 if (elfcore_netbsd_get_lwpid (note, &lwp))
10480 elf_tdata (abfd)->core->lwpid = lwp;
10482 if (note->type == NT_NETBSDCORE_PROCINFO)
10484 /* NetBSD-specific core "procinfo". Note that we expect to
10485 find this note before any of the others, which is fine,
10486 since the kernel writes this note out first when it
10487 creates a core file. */
10489 return elfcore_grok_netbsd_procinfo (abfd, note);
10492 /* As of Jan 2002 there are no other machine-independent notes
10493 defined for NetBSD core files. If the note type is less
10494 than the start of the machine-dependent note types, we don't
10497 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10501 switch (bfd_get_arch (abfd))
10503 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10504 PT_GETFPREGS == mach+2. */
10506 case bfd_arch_alpha:
10507 case bfd_arch_sparc:
10508 switch (note->type)
10510 case NT_NETBSDCORE_FIRSTMACH+0:
10511 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10513 case NT_NETBSDCORE_FIRSTMACH+2:
10514 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10520 /* On all other arch's, PT_GETREGS == mach+1 and
10521 PT_GETFPREGS == mach+3. */
10524 switch (note->type)
10526 case NT_NETBSDCORE_FIRSTMACH+1:
10527 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10529 case NT_NETBSDCORE_FIRSTMACH+3:
10530 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10540 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10542 if (note->descsz <= 0x48 + 31)
10545 /* Signal number at offset 0x08. */
10546 elf_tdata (abfd)->core->signal
10547 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10549 /* Process ID at offset 0x20. */
10550 elf_tdata (abfd)->core->pid
10551 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10553 /* Command name at 0x48 (max 32 bytes, including nul). */
10554 elf_tdata (abfd)->core->command
10555 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10561 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10563 if (note->type == NT_OPENBSD_PROCINFO)
10564 return elfcore_grok_openbsd_procinfo (abfd, note);
10566 if (note->type == NT_OPENBSD_REGS)
10567 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10569 if (note->type == NT_OPENBSD_FPREGS)
10570 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10572 if (note->type == NT_OPENBSD_XFPREGS)
10573 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10575 if (note->type == NT_OPENBSD_AUXV)
10577 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10582 sect->size = note->descsz;
10583 sect->filepos = note->descpos;
10584 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10589 if (note->type == NT_OPENBSD_WCOOKIE)
10591 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10596 sect->size = note->descsz;
10597 sect->filepos = note->descpos;
10598 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10607 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10609 void *ddata = note->descdata;
10616 if (note->descsz < 16)
10619 /* nto_procfs_status 'pid' field is at offset 0. */
10620 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10622 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10623 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10625 /* nto_procfs_status 'flags' field is at offset 8. */
10626 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10628 /* nto_procfs_status 'what' field is at offset 14. */
10629 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10631 elf_tdata (abfd)->core->signal = sig;
10632 elf_tdata (abfd)->core->lwpid = *tid;
10635 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10636 do not come from signals so we make sure we set the current
10637 thread just in case. */
10638 if (flags & 0x00000080)
10639 elf_tdata (abfd)->core->lwpid = *tid;
10641 /* Make a ".qnx_core_status/%d" section. */
10642 sprintf (buf, ".qnx_core_status/%ld", *tid);
10644 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10647 strcpy (name, buf);
10649 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10653 sect->size = note->descsz;
10654 sect->filepos = note->descpos;
10655 sect->alignment_power = 2;
10657 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10661 elfcore_grok_nto_regs (bfd *abfd,
10662 Elf_Internal_Note *note,
10670 /* Make a "(base)/%d" section. */
10671 sprintf (buf, "%s/%ld", base, tid);
10673 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10676 strcpy (name, buf);
10678 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10682 sect->size = note->descsz;
10683 sect->filepos = note->descpos;
10684 sect->alignment_power = 2;
10686 /* This is the current thread. */
10687 if (elf_tdata (abfd)->core->lwpid == tid)
10688 return elfcore_maybe_make_sect (abfd, base, sect);
10693 #define BFD_QNT_CORE_INFO 7
10694 #define BFD_QNT_CORE_STATUS 8
10695 #define BFD_QNT_CORE_GREG 9
10696 #define BFD_QNT_CORE_FPREG 10
10699 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10701 /* Every GREG section has a STATUS section before it. Store the
10702 tid from the previous call to pass down to the next gregs
10704 static long tid = 1;
10706 switch (note->type)
10708 case BFD_QNT_CORE_INFO:
10709 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10710 case BFD_QNT_CORE_STATUS:
10711 return elfcore_grok_nto_status (abfd, note, &tid);
10712 case BFD_QNT_CORE_GREG:
10713 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10714 case BFD_QNT_CORE_FPREG:
10715 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10722 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10728 /* Use note name as section name. */
10729 len = note->namesz;
10730 name = (char *) bfd_alloc (abfd, len);
10733 memcpy (name, note->namedata, len);
10734 name[len - 1] = '\0';
10736 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10740 sect->size = note->descsz;
10741 sect->filepos = note->descpos;
10742 sect->alignment_power = 1;
10747 /* Function: elfcore_write_note
10750 buffer to hold note, and current size of buffer
10754 size of data for note
10756 Writes note to end of buffer. ELF64 notes are written exactly as
10757 for ELF32, despite the current (as of 2006) ELF gabi specifying
10758 that they ought to have 8-byte namesz and descsz field, and have
10759 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10762 Pointer to realloc'd buffer, *BUFSIZ updated. */
10765 elfcore_write_note (bfd *abfd,
10773 Elf_External_Note *xnp;
10780 namesz = strlen (name) + 1;
10782 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
10784 buf = (char *) realloc (buf, *bufsiz + newspace);
10787 dest = buf + *bufsiz;
10788 *bufsiz += newspace;
10789 xnp = (Elf_External_Note *) dest;
10790 H_PUT_32 (abfd, namesz, xnp->namesz);
10791 H_PUT_32 (abfd, size, xnp->descsz);
10792 H_PUT_32 (abfd, type, xnp->type);
10796 memcpy (dest, name, namesz);
10804 memcpy (dest, input, size);
10814 /* gcc-8 warns (*) on all the strncpy calls in this function about
10815 possible string truncation. The "truncation" is not a bug. We
10816 have an external representation of structs with fields that are not
10817 necessarily NULL terminated and corresponding internal
10818 representation fields that are one larger so that they can always
10819 be NULL terminated.
10820 gcc versions between 4.2 and 4.6 do not allow pragma control of
10821 diagnostics inside functions, giving a hard error if you try to use
10822 the finer control available with later versions.
10823 gcc prior to 4.2 warns about diagnostic push and pop.
10824 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10825 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10826 (*) Depending on your system header files! */
10827 #if GCC_VERSION >= 8000
10828 # pragma GCC diagnostic push
10829 # pragma GCC diagnostic ignored "-Wstringop-truncation"
10832 elfcore_write_prpsinfo (bfd *abfd,
10836 const char *psargs)
10838 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10840 if (bed->elf_backend_write_core_note != NULL)
10843 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10844 NT_PRPSINFO, fname, psargs);
10849 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10850 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10851 if (bed->s->elfclass == ELFCLASS32)
10853 # if defined (HAVE_PSINFO32_T)
10855 int note_type = NT_PSINFO;
10858 int note_type = NT_PRPSINFO;
10861 memset (&data, 0, sizeof (data));
10862 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10863 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10864 return elfcore_write_note (abfd, buf, bufsiz,
10865 "CORE", note_type, &data, sizeof (data));
10870 # if defined (HAVE_PSINFO_T)
10872 int note_type = NT_PSINFO;
10875 int note_type = NT_PRPSINFO;
10878 memset (&data, 0, sizeof (data));
10879 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10880 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10881 return elfcore_write_note (abfd, buf, bufsiz,
10882 "CORE", note_type, &data, sizeof (data));
10884 #endif /* PSINFO_T or PRPSINFO_T */
10889 #if GCC_VERSION >= 8000
10890 # pragma GCC diagnostic pop
10894 elfcore_write_linux_prpsinfo32
10895 (bfd *abfd, char *buf, int *bufsiz,
10896 const struct elf_internal_linux_prpsinfo *prpsinfo)
10898 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
10900 struct elf_external_linux_prpsinfo32_ugid16 data;
10902 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
10903 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10904 &data, sizeof (data));
10908 struct elf_external_linux_prpsinfo32_ugid32 data;
10910 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
10911 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10912 &data, sizeof (data));
10917 elfcore_write_linux_prpsinfo64
10918 (bfd *abfd, char *buf, int *bufsiz,
10919 const struct elf_internal_linux_prpsinfo *prpsinfo)
10921 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
10923 struct elf_external_linux_prpsinfo64_ugid16 data;
10925 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
10926 return elfcore_write_note (abfd, buf, bufsiz,
10927 "CORE", NT_PRPSINFO, &data, sizeof (data));
10931 struct elf_external_linux_prpsinfo64_ugid32 data;
10933 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
10934 return elfcore_write_note (abfd, buf, bufsiz,
10935 "CORE", NT_PRPSINFO, &data, sizeof (data));
10940 elfcore_write_prstatus (bfd *abfd,
10947 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10949 if (bed->elf_backend_write_core_note != NULL)
10952 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10954 pid, cursig, gregs);
10959 #if defined (HAVE_PRSTATUS_T)
10960 #if defined (HAVE_PRSTATUS32_T)
10961 if (bed->s->elfclass == ELFCLASS32)
10963 prstatus32_t prstat;
10965 memset (&prstat, 0, sizeof (prstat));
10966 prstat.pr_pid = pid;
10967 prstat.pr_cursig = cursig;
10968 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
10969 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
10970 NT_PRSTATUS, &prstat, sizeof (prstat));
10977 memset (&prstat, 0, sizeof (prstat));
10978 prstat.pr_pid = pid;
10979 prstat.pr_cursig = cursig;
10980 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
10981 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
10982 NT_PRSTATUS, &prstat, sizeof (prstat));
10984 #endif /* HAVE_PRSTATUS_T */
10990 #if defined (HAVE_LWPSTATUS_T)
10992 elfcore_write_lwpstatus (bfd *abfd,
10999 lwpstatus_t lwpstat;
11000 const char *note_name = "CORE";
11002 memset (&lwpstat, 0, sizeof (lwpstat));
11003 lwpstat.pr_lwpid = pid >> 16;
11004 lwpstat.pr_cursig = cursig;
11005 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11006 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11007 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11008 #if !defined(gregs)
11009 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11010 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11012 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11013 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11016 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11017 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11019 #endif /* HAVE_LWPSTATUS_T */
11021 #if defined (HAVE_PSTATUS_T)
11023 elfcore_write_pstatus (bfd *abfd,
11027 int cursig ATTRIBUTE_UNUSED,
11028 const void *gregs ATTRIBUTE_UNUSED)
11030 const char *note_name = "CORE";
11031 #if defined (HAVE_PSTATUS32_T)
11032 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11034 if (bed->s->elfclass == ELFCLASS32)
11038 memset (&pstat, 0, sizeof (pstat));
11039 pstat.pr_pid = pid & 0xffff;
11040 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11041 NT_PSTATUS, &pstat, sizeof (pstat));
11049 memset (&pstat, 0, sizeof (pstat));
11050 pstat.pr_pid = pid & 0xffff;
11051 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11052 NT_PSTATUS, &pstat, sizeof (pstat));
11056 #endif /* HAVE_PSTATUS_T */
11059 elfcore_write_prfpreg (bfd *abfd,
11062 const void *fpregs,
11065 const char *note_name = "CORE";
11066 return elfcore_write_note (abfd, buf, bufsiz,
11067 note_name, NT_FPREGSET, fpregs, size);
11071 elfcore_write_prxfpreg (bfd *abfd,
11074 const void *xfpregs,
11077 char *note_name = "LINUX";
11078 return elfcore_write_note (abfd, buf, bufsiz,
11079 note_name, NT_PRXFPREG, xfpregs, size);
11083 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11084 const void *xfpregs, int size)
11087 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11088 note_name = "FreeBSD";
11090 note_name = "LINUX";
11091 return elfcore_write_note (abfd, buf, bufsiz,
11092 note_name, NT_X86_XSTATE, xfpregs, size);
11096 elfcore_write_ppc_vmx (bfd *abfd,
11099 const void *ppc_vmx,
11102 char *note_name = "LINUX";
11103 return elfcore_write_note (abfd, buf, bufsiz,
11104 note_name, NT_PPC_VMX, ppc_vmx, size);
11108 elfcore_write_ppc_vsx (bfd *abfd,
11111 const void *ppc_vsx,
11114 char *note_name = "LINUX";
11115 return elfcore_write_note (abfd, buf, bufsiz,
11116 note_name, NT_PPC_VSX, ppc_vsx, size);
11120 elfcore_write_ppc_tar (bfd *abfd,
11123 const void *ppc_tar,
11126 char *note_name = "LINUX";
11127 return elfcore_write_note (abfd, buf, bufsiz,
11128 note_name, NT_PPC_TAR, ppc_tar, size);
11132 elfcore_write_ppc_ppr (bfd *abfd,
11135 const void *ppc_ppr,
11138 char *note_name = "LINUX";
11139 return elfcore_write_note (abfd, buf, bufsiz,
11140 note_name, NT_PPC_PPR, ppc_ppr, size);
11144 elfcore_write_ppc_dscr (bfd *abfd,
11147 const void *ppc_dscr,
11150 char *note_name = "LINUX";
11151 return elfcore_write_note (abfd, buf, bufsiz,
11152 note_name, NT_PPC_DSCR, ppc_dscr, size);
11156 elfcore_write_ppc_ebb (bfd *abfd,
11159 const void *ppc_ebb,
11162 char *note_name = "LINUX";
11163 return elfcore_write_note (abfd, buf, bufsiz,
11164 note_name, NT_PPC_EBB, ppc_ebb, size);
11168 elfcore_write_ppc_pmu (bfd *abfd,
11171 const void *ppc_pmu,
11174 char *note_name = "LINUX";
11175 return elfcore_write_note (abfd, buf, bufsiz,
11176 note_name, NT_PPC_PMU, ppc_pmu, size);
11180 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11183 const void *ppc_tm_cgpr,
11186 char *note_name = "LINUX";
11187 return elfcore_write_note (abfd, buf, bufsiz,
11188 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11192 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11195 const void *ppc_tm_cfpr,
11198 char *note_name = "LINUX";
11199 return elfcore_write_note (abfd, buf, bufsiz,
11200 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11204 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11207 const void *ppc_tm_cvmx,
11210 char *note_name = "LINUX";
11211 return elfcore_write_note (abfd, buf, bufsiz,
11212 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11216 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11219 const void *ppc_tm_cvsx,
11222 char *note_name = "LINUX";
11223 return elfcore_write_note (abfd, buf, bufsiz,
11224 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11228 elfcore_write_ppc_tm_spr (bfd *abfd,
11231 const void *ppc_tm_spr,
11234 char *note_name = "LINUX";
11235 return elfcore_write_note (abfd, buf, bufsiz,
11236 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11240 elfcore_write_ppc_tm_ctar (bfd *abfd,
11243 const void *ppc_tm_ctar,
11246 char *note_name = "LINUX";
11247 return elfcore_write_note (abfd, buf, bufsiz,
11248 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11252 elfcore_write_ppc_tm_cppr (bfd *abfd,
11255 const void *ppc_tm_cppr,
11258 char *note_name = "LINUX";
11259 return elfcore_write_note (abfd, buf, bufsiz,
11260 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11264 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11267 const void *ppc_tm_cdscr,
11270 char *note_name = "LINUX";
11271 return elfcore_write_note (abfd, buf, bufsiz,
11272 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11276 elfcore_write_s390_high_gprs (bfd *abfd,
11279 const void *s390_high_gprs,
11282 char *note_name = "LINUX";
11283 return elfcore_write_note (abfd, buf, bufsiz,
11284 note_name, NT_S390_HIGH_GPRS,
11285 s390_high_gprs, size);
11289 elfcore_write_s390_timer (bfd *abfd,
11292 const void *s390_timer,
11295 char *note_name = "LINUX";
11296 return elfcore_write_note (abfd, buf, bufsiz,
11297 note_name, NT_S390_TIMER, s390_timer, size);
11301 elfcore_write_s390_todcmp (bfd *abfd,
11304 const void *s390_todcmp,
11307 char *note_name = "LINUX";
11308 return elfcore_write_note (abfd, buf, bufsiz,
11309 note_name, NT_S390_TODCMP, s390_todcmp, size);
11313 elfcore_write_s390_todpreg (bfd *abfd,
11316 const void *s390_todpreg,
11319 char *note_name = "LINUX";
11320 return elfcore_write_note (abfd, buf, bufsiz,
11321 note_name, NT_S390_TODPREG, s390_todpreg, size);
11325 elfcore_write_s390_ctrs (bfd *abfd,
11328 const void *s390_ctrs,
11331 char *note_name = "LINUX";
11332 return elfcore_write_note (abfd, buf, bufsiz,
11333 note_name, NT_S390_CTRS, s390_ctrs, size);
11337 elfcore_write_s390_prefix (bfd *abfd,
11340 const void *s390_prefix,
11343 char *note_name = "LINUX";
11344 return elfcore_write_note (abfd, buf, bufsiz,
11345 note_name, NT_S390_PREFIX, s390_prefix, size);
11349 elfcore_write_s390_last_break (bfd *abfd,
11352 const void *s390_last_break,
11355 char *note_name = "LINUX";
11356 return elfcore_write_note (abfd, buf, bufsiz,
11357 note_name, NT_S390_LAST_BREAK,
11358 s390_last_break, size);
11362 elfcore_write_s390_system_call (bfd *abfd,
11365 const void *s390_system_call,
11368 char *note_name = "LINUX";
11369 return elfcore_write_note (abfd, buf, bufsiz,
11370 note_name, NT_S390_SYSTEM_CALL,
11371 s390_system_call, size);
11375 elfcore_write_s390_tdb (bfd *abfd,
11378 const void *s390_tdb,
11381 char *note_name = "LINUX";
11382 return elfcore_write_note (abfd, buf, bufsiz,
11383 note_name, NT_S390_TDB, s390_tdb, size);
11387 elfcore_write_s390_vxrs_low (bfd *abfd,
11390 const void *s390_vxrs_low,
11393 char *note_name = "LINUX";
11394 return elfcore_write_note (abfd, buf, bufsiz,
11395 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11399 elfcore_write_s390_vxrs_high (bfd *abfd,
11402 const void *s390_vxrs_high,
11405 char *note_name = "LINUX";
11406 return elfcore_write_note (abfd, buf, bufsiz,
11407 note_name, NT_S390_VXRS_HIGH,
11408 s390_vxrs_high, size);
11412 elfcore_write_s390_gs_cb (bfd *abfd,
11415 const void *s390_gs_cb,
11418 char *note_name = "LINUX";
11419 return elfcore_write_note (abfd, buf, bufsiz,
11420 note_name, NT_S390_GS_CB,
11425 elfcore_write_s390_gs_bc (bfd *abfd,
11428 const void *s390_gs_bc,
11431 char *note_name = "LINUX";
11432 return elfcore_write_note (abfd, buf, bufsiz,
11433 note_name, NT_S390_GS_BC,
11438 elfcore_write_arm_vfp (bfd *abfd,
11441 const void *arm_vfp,
11444 char *note_name = "LINUX";
11445 return elfcore_write_note (abfd, buf, bufsiz,
11446 note_name, NT_ARM_VFP, arm_vfp, size);
11450 elfcore_write_aarch_tls (bfd *abfd,
11453 const void *aarch_tls,
11456 char *note_name = "LINUX";
11457 return elfcore_write_note (abfd, buf, bufsiz,
11458 note_name, NT_ARM_TLS, aarch_tls, size);
11462 elfcore_write_aarch_hw_break (bfd *abfd,
11465 const void *aarch_hw_break,
11468 char *note_name = "LINUX";
11469 return elfcore_write_note (abfd, buf, bufsiz,
11470 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11474 elfcore_write_aarch_hw_watch (bfd *abfd,
11477 const void *aarch_hw_watch,
11480 char *note_name = "LINUX";
11481 return elfcore_write_note (abfd, buf, bufsiz,
11482 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11486 elfcore_write_aarch_sve (bfd *abfd,
11489 const void *aarch_sve,
11492 char *note_name = "LINUX";
11493 return elfcore_write_note (abfd, buf, bufsiz,
11494 note_name, NT_ARM_SVE, aarch_sve, size);
11498 elfcore_write_register_note (bfd *abfd,
11501 const char *section,
11505 if (strcmp (section, ".reg2") == 0)
11506 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11507 if (strcmp (section, ".reg-xfp") == 0)
11508 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
11509 if (strcmp (section, ".reg-xstate") == 0)
11510 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
11511 if (strcmp (section, ".reg-ppc-vmx") == 0)
11512 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
11513 if (strcmp (section, ".reg-ppc-vsx") == 0)
11514 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
11515 if (strcmp (section, ".reg-ppc-tar") == 0)
11516 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11517 if (strcmp (section, ".reg-ppc-ppr") == 0)
11518 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11519 if (strcmp (section, ".reg-ppc-dscr") == 0)
11520 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11521 if (strcmp (section, ".reg-ppc-ebb") == 0)
11522 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11523 if (strcmp (section, ".reg-ppc-pmu") == 0)
11524 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11525 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11526 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11527 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11528 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11529 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11530 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11531 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11532 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11533 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11534 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11535 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11536 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11537 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11538 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11539 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11540 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
11541 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11542 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
11543 if (strcmp (section, ".reg-s390-timer") == 0)
11544 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11545 if (strcmp (section, ".reg-s390-todcmp") == 0)
11546 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11547 if (strcmp (section, ".reg-s390-todpreg") == 0)
11548 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11549 if (strcmp (section, ".reg-s390-ctrs") == 0)
11550 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11551 if (strcmp (section, ".reg-s390-prefix") == 0)
11552 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
11553 if (strcmp (section, ".reg-s390-last-break") == 0)
11554 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11555 if (strcmp (section, ".reg-s390-system-call") == 0)
11556 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
11557 if (strcmp (section, ".reg-s390-tdb") == 0)
11558 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
11559 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11560 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11561 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11562 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
11563 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11564 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11565 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11566 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
11567 if (strcmp (section, ".reg-arm-vfp") == 0)
11568 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
11569 if (strcmp (section, ".reg-aarch-tls") == 0)
11570 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11571 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11572 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11573 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11574 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
11575 if (strcmp (section, ".reg-aarch-sve") == 0)
11576 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
11581 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11586 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11587 gABI specifies that PT_NOTE alignment should be aligned to 4
11588 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11589 align is less than 4, we use 4 byte alignment. */
11592 if (align != 4 && align != 8)
11596 while (p < buf + size)
11598 Elf_External_Note *xnp = (Elf_External_Note *) p;
11599 Elf_Internal_Note in;
11601 if (offsetof (Elf_External_Note, name) > buf - p + size)
11604 in.type = H_GET_32 (abfd, xnp->type);
11606 in.namesz = H_GET_32 (abfd, xnp->namesz);
11607 in.namedata = xnp->name;
11608 if (in.namesz > buf - in.namedata + size)
11611 in.descsz = H_GET_32 (abfd, xnp->descsz);
11612 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
11613 in.descpos = offset + (in.descdata - buf);
11615 && (in.descdata >= buf + size
11616 || in.descsz > buf - in.descdata + size))
11619 switch (bfd_get_format (abfd))
11626 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11629 const char * string;
11631 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
11635 GROKER_ELEMENT ("", elfcore_grok_note),
11636 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11637 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11638 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11639 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11640 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
11642 #undef GROKER_ELEMENT
11645 for (i = ARRAY_SIZE (grokers); i--;)
11647 if (in.namesz >= grokers[i].len
11648 && strncmp (in.namedata, grokers[i].string,
11649 grokers[i].len) == 0)
11651 if (! grokers[i].func (abfd, & in))
11660 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11662 if (! elfobj_grok_gnu_note (abfd, &in))
11665 else if (in.namesz == sizeof "stapsdt"
11666 && strcmp (in.namedata, "stapsdt") == 0)
11668 if (! elfobj_grok_stapsdt_note (abfd, &in))
11674 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
11681 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11686 if (size == 0 || (size + 1) == 0)
11689 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11692 buf = (char *) bfd_malloc (size + 1);
11696 /* PR 17512: file: ec08f814
11697 0-termintate the buffer so that string searches will not overflow. */
11700 if (bfd_bread (buf, size, abfd) != size
11701 || !elf_parse_notes (abfd, buf, size, offset, align))
11711 /* Providing external access to the ELF program header table. */
11713 /* Return an upper bound on the number of bytes required to store a
11714 copy of ABFD's program header table entries. Return -1 if an error
11715 occurs; bfd_get_error will return an appropriate code. */
11718 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11720 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11722 bfd_set_error (bfd_error_wrong_format);
11726 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11729 /* Copy ABFD's program header table entries to *PHDRS. The entries
11730 will be stored as an array of Elf_Internal_Phdr structures, as
11731 defined in include/elf/internal.h. To find out how large the
11732 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11734 Return the number of program header table entries read, or -1 if an
11735 error occurs; bfd_get_error will return an appropriate code. */
11738 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11742 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11744 bfd_set_error (bfd_error_wrong_format);
11748 num_phdrs = elf_elfheader (abfd)->e_phnum;
11749 if (num_phdrs != 0)
11750 memcpy (phdrs, elf_tdata (abfd)->phdr,
11751 num_phdrs * sizeof (Elf_Internal_Phdr));
11756 enum elf_reloc_type_class
11757 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11758 const asection *rel_sec ATTRIBUTE_UNUSED,
11759 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
11761 return reloc_class_normal;
11764 /* For RELA architectures, return the relocation value for a
11765 relocation against a local symbol. */
11768 _bfd_elf_rela_local_sym (bfd *abfd,
11769 Elf_Internal_Sym *sym,
11771 Elf_Internal_Rela *rel)
11773 asection *sec = *psec;
11774 bfd_vma relocation;
11776 relocation = (sec->output_section->vma
11777 + sec->output_offset
11779 if ((sec->flags & SEC_MERGE)
11780 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
11781 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
11784 _bfd_merged_section_offset (abfd, psec,
11785 elf_section_data (sec)->sec_info,
11786 sym->st_value + rel->r_addend);
11789 /* If we have changed the section, and our original section is
11790 marked with SEC_EXCLUDE, it means that the original
11791 SEC_MERGE section has been completely subsumed in some
11792 other SEC_MERGE section. In this case, we need to leave
11793 some info around for --emit-relocs. */
11794 if ((sec->flags & SEC_EXCLUDE) != 0)
11795 sec->kept_section = *psec;
11798 rel->r_addend -= relocation;
11799 rel->r_addend += sec->output_section->vma + sec->output_offset;
11805 _bfd_elf_rel_local_sym (bfd *abfd,
11806 Elf_Internal_Sym *sym,
11810 asection *sec = *psec;
11812 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
11813 return sym->st_value + addend;
11815 return _bfd_merged_section_offset (abfd, psec,
11816 elf_section_data (sec)->sec_info,
11817 sym->st_value + addend);
11820 /* Adjust an address within a section. Given OFFSET within SEC, return
11821 the new offset within the section, based upon changes made to the
11822 section. Returns -1 if the offset is now invalid.
11823 The offset (in abnd out) is in target sized bytes, however big a
11827 _bfd_elf_section_offset (bfd *abfd,
11828 struct bfd_link_info *info,
11832 switch (sec->sec_info_type)
11834 case SEC_INFO_TYPE_STABS:
11835 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11837 case SEC_INFO_TYPE_EH_FRAME:
11838 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
11841 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11843 /* Reverse the offset. */
11844 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11845 bfd_size_type address_size = bed->s->arch_size / 8;
11847 /* address_size and sec->size are in octets. Convert
11848 to bytes before subtracting the original offset. */
11849 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
11855 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11856 reconstruct an ELF file by reading the segments out of remote memory
11857 based on the ELF file header at EHDR_VMA and the ELF program headers it
11858 points to. If not null, *LOADBASEP is filled in with the difference
11859 between the VMAs from which the segments were read, and the VMAs the
11860 file headers (and hence BFD's idea of each section's VMA) put them at.
11862 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11863 remote memory at target address VMA into the local buffer at MYADDR; it
11864 should return zero on success or an `errno' code on failure. TEMPL must
11865 be a BFD for an ELF target with the word size and byte order found in
11866 the remote memory. */
11869 bfd_elf_bfd_from_remote_memory
11872 bfd_size_type size,
11873 bfd_vma *loadbasep,
11874 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
11876 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
11877 (templ, ehdr_vma, size, loadbasep, target_read_memory);
11881 _bfd_elf_get_synthetic_symtab (bfd *abfd,
11882 long symcount ATTRIBUTE_UNUSED,
11883 asymbol **syms ATTRIBUTE_UNUSED,
11888 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11891 const char *relplt_name;
11892 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
11896 Elf_Internal_Shdr *hdr;
11902 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
11905 if (dynsymcount <= 0)
11908 if (!bed->plt_sym_val)
11911 relplt_name = bed->relplt_name;
11912 if (relplt_name == NULL)
11913 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
11914 relplt = bfd_get_section_by_name (abfd, relplt_name);
11915 if (relplt == NULL)
11918 hdr = &elf_section_data (relplt)->this_hdr;
11919 if (hdr->sh_link != elf_dynsymtab (abfd)
11920 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
11923 plt = bfd_get_section_by_name (abfd, ".plt");
11927 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
11928 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
11931 count = relplt->size / hdr->sh_entsize;
11932 size = count * sizeof (asymbol);
11933 p = relplt->relocation;
11934 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
11936 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
11937 if (p->addend != 0)
11940 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
11942 size += sizeof ("+0x") - 1 + 8;
11947 s = *ret = (asymbol *) bfd_malloc (size);
11951 names = (char *) (s + count);
11952 p = relplt->relocation;
11954 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
11959 addr = bed->plt_sym_val (i, plt, p);
11960 if (addr == (bfd_vma) -1)
11963 *s = **p->sym_ptr_ptr;
11964 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11965 we are defining a symbol, ensure one of them is set. */
11966 if ((s->flags & BSF_LOCAL) == 0)
11967 s->flags |= BSF_GLOBAL;
11968 s->flags |= BSF_SYNTHETIC;
11970 s->value = addr - plt->vma;
11973 len = strlen ((*p->sym_ptr_ptr)->name);
11974 memcpy (names, (*p->sym_ptr_ptr)->name, len);
11976 if (p->addend != 0)
11980 memcpy (names, "+0x", sizeof ("+0x") - 1);
11981 names += sizeof ("+0x") - 1;
11982 bfd_sprintf_vma (abfd, buf, p->addend);
11983 for (a = buf; *a == '0'; ++a)
11986 memcpy (names, a, len);
11989 memcpy (names, "@plt", sizeof ("@plt"));
11990 names += sizeof ("@plt");
11997 /* It is only used by x86-64 so far.
11998 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11999 but current usage would allow all of _bfd_std_section to be zero. */
12000 static const asymbol lcomm_sym
12001 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12002 asection _bfd_elf_large_com_section
12003 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12004 "LARGE_COMMON", 0, SEC_IS_COMMON);
12007 _bfd_elf_post_process_headers (bfd * abfd,
12008 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
12010 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
12012 i_ehdrp = elf_elfheader (abfd);
12014 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
12016 /* To make things simpler for the loader on Linux systems we set the
12017 osabi field to ELFOSABI_GNU if the binary contains symbols of
12018 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
12019 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
12020 && elf_tdata (abfd)->has_gnu_symbols)
12021 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12025 /* Return TRUE for ELF symbol types that represent functions.
12026 This is the default version of this function, which is sufficient for
12027 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12030 _bfd_elf_is_function_type (unsigned int type)
12032 return (type == STT_FUNC
12033 || type == STT_GNU_IFUNC);
12036 /* If the ELF symbol SYM might be a function in SEC, return the
12037 function size and set *CODE_OFF to the function's entry point,
12038 otherwise return zero. */
12041 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12044 bfd_size_type size;
12046 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
12047 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12048 || sym->section != sec)
12051 *code_off = sym->value;
12053 if (!(sym->flags & BSF_SYNTHETIC))
12054 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;