1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2015 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-psinfo.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
53 static bfd_boolean prep_headers (bfd *);
54 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
55 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
56 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg)
199 const unsigned char *name = (const unsigned char *) namearg;
204 while ((ch = *name++) != '\0')
207 if ((g = (h & 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h & 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg)
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd *abfd,
238 enum elf_target_id object_id)
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
245 elf_object_id (abfd) = object_id;
246 if (abfd->direction != read_direction)
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
259 bfd_elf_make_object (bfd *abfd)
261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
267 bfd_elf_mkcorefile (bfd *abfd)
269 /* I think this can be done just like an object file. */
270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
279 Elf_Internal_Shdr **i_shdrp;
280 bfd_byte *shstrtab = NULL;
282 bfd_size_type shstrtabsize;
284 i_shdrp = elf_elfsections (abfd);
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
290 shstrtab = i_shdrp[shindex]->contents;
291 if (shstrtab == NULL)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize + 1 <= 1
300 || bfd_seek (abfd, offset, SEEK_SET) != 0
301 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
303 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
305 if (bfd_get_error () != bfd_error_system_call)
306 bfd_set_error (bfd_error_file_truncated);
307 bfd_release (abfd, shstrtab);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp[shindex]->sh_size = 0;
315 shstrtab[shstrtabsize] = '\0';
316 i_shdrp[shindex]->contents = shstrtab;
318 return (char *) shstrtab;
322 bfd_elf_string_from_elf_section (bfd *abfd,
323 unsigned int shindex,
324 unsigned int strindex)
326 Elf_Internal_Shdr *hdr;
331 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
334 hdr = elf_elfsections (abfd)[shindex];
336 if (hdr->contents == NULL)
338 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
340 /* PR 17512: file: f057ec89. */
341 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
346 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
350 if (strindex >= hdr->sh_size)
352 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
353 (*_bfd_error_handler)
354 (_("%B: invalid string offset %u >= %lu for section `%s'"),
355 abfd, strindex, (unsigned long) hdr->sh_size,
356 (shindex == shstrndx && strindex == hdr->sh_name
358 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
362 return ((char *) hdr->contents) + strindex;
365 /* Read and convert symbols to internal format.
366 SYMCOUNT specifies the number of symbols to read, starting from
367 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
368 are non-NULL, they are used to store the internal symbols, external
369 symbols, and symbol section index extensions, respectively.
370 Returns a pointer to the internal symbol buffer (malloced if necessary)
371 or NULL if there were no symbols or some kind of problem. */
374 bfd_elf_get_elf_syms (bfd *ibfd,
375 Elf_Internal_Shdr *symtab_hdr,
378 Elf_Internal_Sym *intsym_buf,
380 Elf_External_Sym_Shndx *extshndx_buf)
382 Elf_Internal_Shdr *shndx_hdr;
384 const bfd_byte *esym;
385 Elf_External_Sym_Shndx *alloc_extshndx;
386 Elf_External_Sym_Shndx *shndx;
387 Elf_Internal_Sym *alloc_intsym;
388 Elf_Internal_Sym *isym;
389 Elf_Internal_Sym *isymend;
390 const struct elf_backend_data *bed;
395 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
401 /* Normal syms might have section extension entries. */
403 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
404 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
406 /* Read the symbols. */
408 alloc_extshndx = NULL;
410 bed = get_elf_backend_data (ibfd);
411 extsym_size = bed->s->sizeof_sym;
412 amt = symcount * extsym_size;
413 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
414 if (extsym_buf == NULL)
416 alloc_ext = bfd_malloc2 (symcount, extsym_size);
417 extsym_buf = alloc_ext;
419 if (extsym_buf == NULL
420 || bfd_seek (ibfd, pos, SEEK_SET) != 0
421 || bfd_bread (extsym_buf, amt, ibfd) != amt)
427 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
431 amt = symcount * sizeof (Elf_External_Sym_Shndx);
432 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
433 if (extshndx_buf == NULL)
435 alloc_extshndx = (Elf_External_Sym_Shndx *)
436 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
437 extshndx_buf = alloc_extshndx;
439 if (extshndx_buf == NULL
440 || bfd_seek (ibfd, pos, SEEK_SET) != 0
441 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
448 if (intsym_buf == NULL)
450 alloc_intsym = (Elf_Internal_Sym *)
451 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
452 intsym_buf = alloc_intsym;
453 if (intsym_buf == NULL)
457 /* Convert the symbols to internal form. */
458 isymend = intsym_buf + symcount;
459 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
460 shndx = extshndx_buf;
462 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
463 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
465 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
466 (*_bfd_error_handler) (_("%B symbol number %lu references "
467 "nonexistent SHT_SYMTAB_SHNDX section"),
468 ibfd, (unsigned long) symoffset);
469 if (alloc_intsym != NULL)
476 if (alloc_ext != NULL)
478 if (alloc_extshndx != NULL)
479 free (alloc_extshndx);
484 /* Look up a symbol name. */
486 bfd_elf_sym_name (bfd *abfd,
487 Elf_Internal_Shdr *symtab_hdr,
488 Elf_Internal_Sym *isym,
492 unsigned int iname = isym->st_name;
493 unsigned int shindex = symtab_hdr->sh_link;
495 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
496 /* Check for a bogus st_shndx to avoid crashing. */
497 && isym->st_shndx < elf_numsections (abfd))
499 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
500 shindex = elf_elfheader (abfd)->e_shstrndx;
503 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
506 else if (sym_sec && *name == '\0')
507 name = bfd_section_name (abfd, sym_sec);
512 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
513 sections. The first element is the flags, the rest are section
516 typedef union elf_internal_group {
517 Elf_Internal_Shdr *shdr;
519 } Elf_Internal_Group;
521 /* Return the name of the group signature symbol. Why isn't the
522 signature just a string? */
525 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
527 Elf_Internal_Shdr *hdr;
528 unsigned char esym[sizeof (Elf64_External_Sym)];
529 Elf_External_Sym_Shndx eshndx;
530 Elf_Internal_Sym isym;
532 /* First we need to ensure the symbol table is available. Make sure
533 that it is a symbol table section. */
534 if (ghdr->sh_link >= elf_numsections (abfd))
536 hdr = elf_elfsections (abfd) [ghdr->sh_link];
537 if (hdr->sh_type != SHT_SYMTAB
538 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
541 /* Go read the symbol. */
542 hdr = &elf_tdata (abfd)->symtab_hdr;
543 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
544 &isym, esym, &eshndx) == NULL)
547 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
550 /* Set next_in_group list pointer, and group name for NEWSECT. */
553 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
555 unsigned int num_group = elf_tdata (abfd)->num_group;
557 /* If num_group is zero, read in all SHT_GROUP sections. The count
558 is set to -1 if there are no SHT_GROUP sections. */
561 unsigned int i, shnum;
563 /* First count the number of groups. If we have a SHT_GROUP
564 section with just a flag word (ie. sh_size is 4), ignore it. */
565 shnum = elf_numsections (abfd);
568 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
569 ( (shdr)->sh_type == SHT_GROUP \
570 && (shdr)->sh_size >= minsize \
571 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
572 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
574 for (i = 0; i < shnum; i++)
576 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
578 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
584 num_group = (unsigned) -1;
585 elf_tdata (abfd)->num_group = num_group;
589 /* We keep a list of elf section headers for group sections,
590 so we can find them quickly. */
593 elf_tdata (abfd)->num_group = num_group;
594 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
595 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
596 if (elf_tdata (abfd)->group_sect_ptr == NULL)
600 for (i = 0; i < shnum; i++)
602 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
604 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
607 Elf_Internal_Group *dest;
609 /* Add to list of sections. */
610 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
613 /* Read the raw contents. */
614 BFD_ASSERT (sizeof (*dest) >= 4);
615 amt = shdr->sh_size * sizeof (*dest) / 4;
616 shdr->contents = (unsigned char *)
617 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
618 /* PR binutils/4110: Handle corrupt group headers. */
619 if (shdr->contents == NULL)
622 (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
623 bfd_set_error (bfd_error_bad_value);
628 memset (shdr->contents, 0, amt);
630 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
631 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
635 (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size);
636 bfd_set_error (bfd_error_bad_value);
638 /* PR 17510: If the group contents are even partially
639 corrupt, do not allow any of the contents to be used. */
640 memset (shdr->contents, 0, amt);
644 /* Translate raw contents, a flag word followed by an
645 array of elf section indices all in target byte order,
646 to the flag word followed by an array of elf section
648 src = shdr->contents + shdr->sh_size;
649 dest = (Elf_Internal_Group *) (shdr->contents + amt);
657 idx = H_GET_32 (abfd, src);
658 if (src == shdr->contents)
661 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
662 shdr->bfd_section->flags
663 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
668 ((*_bfd_error_handler)
669 (_("%B: invalid SHT_GROUP entry"), abfd));
672 dest->shdr = elf_elfsections (abfd)[idx];
677 /* PR 17510: Corrupt binaries might contain invalid groups. */
678 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
680 elf_tdata (abfd)->num_group = num_group;
682 /* If all groups are invalid then fail. */
685 elf_tdata (abfd)->group_sect_ptr = NULL;
686 elf_tdata (abfd)->num_group = num_group = -1;
687 (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd);
688 bfd_set_error (bfd_error_bad_value);
694 if (num_group != (unsigned) -1)
698 for (i = 0; i < num_group; i++)
700 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
701 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
702 unsigned int n_elt = shdr->sh_size / 4;
704 /* Look through this group's sections to see if current
705 section is a member. */
707 if ((++idx)->shdr == hdr)
711 /* We are a member of this group. Go looking through
712 other members to see if any others are linked via
714 idx = (Elf_Internal_Group *) shdr->contents;
715 n_elt = shdr->sh_size / 4;
717 if ((s = (++idx)->shdr->bfd_section) != NULL
718 && elf_next_in_group (s) != NULL)
722 /* Snarf the group name from other member, and
723 insert current section in circular list. */
724 elf_group_name (newsect) = elf_group_name (s);
725 elf_next_in_group (newsect) = elf_next_in_group (s);
726 elf_next_in_group (s) = newsect;
732 gname = group_signature (abfd, shdr);
735 elf_group_name (newsect) = gname;
737 /* Start a circular list with one element. */
738 elf_next_in_group (newsect) = newsect;
741 /* If the group section has been created, point to the
743 if (shdr->bfd_section != NULL)
744 elf_next_in_group (shdr->bfd_section) = newsect;
752 if (elf_group_name (newsect) == NULL)
754 (*_bfd_error_handler) (_("%B: no group info for section %A"),
762 _bfd_elf_setup_sections (bfd *abfd)
765 unsigned int num_group = elf_tdata (abfd)->num_group;
766 bfd_boolean result = TRUE;
769 /* Process SHF_LINK_ORDER. */
770 for (s = abfd->sections; s != NULL; s = s->next)
772 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
773 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
775 unsigned int elfsec = this_hdr->sh_link;
776 /* FIXME: The old Intel compiler and old strip/objcopy may
777 not set the sh_link or sh_info fields. Hence we could
778 get the situation where elfsec is 0. */
781 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
782 if (bed->link_order_error_handler)
783 bed->link_order_error_handler
784 (_("%B: warning: sh_link not set for section `%A'"),
789 asection *linksec = NULL;
791 if (elfsec < elf_numsections (abfd))
793 this_hdr = elf_elfsections (abfd)[elfsec];
794 linksec = this_hdr->bfd_section;
798 Some strip/objcopy may leave an incorrect value in
799 sh_link. We don't want to proceed. */
802 (*_bfd_error_handler)
803 (_("%B: sh_link [%d] in section `%A' is incorrect"),
804 s->owner, s, elfsec);
808 elf_linked_to_section (s) = linksec;
813 /* Process section groups. */
814 if (num_group == (unsigned) -1)
817 for (i = 0; i < num_group; i++)
819 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
820 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
821 unsigned int n_elt = shdr->sh_size / 4;
824 if ((++idx)->shdr->bfd_section)
825 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
826 else if (idx->shdr->sh_type == SHT_RELA
827 || idx->shdr->sh_type == SHT_REL)
828 /* We won't include relocation sections in section groups in
829 output object files. We adjust the group section size here
830 so that relocatable link will work correctly when
831 relocation sections are in section group in input object
833 shdr->bfd_section->size -= 4;
836 /* There are some unknown sections in the group. */
837 (*_bfd_error_handler)
838 (_("%B: unknown [%d] section `%s' in group [%s]"),
840 (unsigned int) idx->shdr->sh_type,
841 bfd_elf_string_from_elf_section (abfd,
842 (elf_elfheader (abfd)
845 shdr->bfd_section->name);
853 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
855 return elf_next_in_group (sec) != NULL;
858 /* Make a BFD section from an ELF section. We store a pointer to the
859 BFD section in the bfd_section field of the header. */
862 _bfd_elf_make_section_from_shdr (bfd *abfd,
863 Elf_Internal_Shdr *hdr,
869 const struct elf_backend_data *bed;
871 if (hdr->bfd_section != NULL)
874 newsect = bfd_make_section_anyway (abfd, name);
878 hdr->bfd_section = newsect;
879 elf_section_data (newsect)->this_hdr = *hdr;
880 elf_section_data (newsect)->this_idx = shindex;
882 /* Always use the real type/flags. */
883 elf_section_type (newsect) = hdr->sh_type;
884 elf_section_flags (newsect) = hdr->sh_flags;
886 newsect->filepos = hdr->sh_offset;
888 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
889 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
890 || ! bfd_set_section_alignment (abfd, newsect,
891 bfd_log2 (hdr->sh_addralign)))
894 flags = SEC_NO_FLAGS;
895 if (hdr->sh_type != SHT_NOBITS)
896 flags |= SEC_HAS_CONTENTS;
897 if (hdr->sh_type == SHT_GROUP)
898 flags |= SEC_GROUP | SEC_EXCLUDE;
899 if ((hdr->sh_flags & SHF_ALLOC) != 0)
902 if (hdr->sh_type != SHT_NOBITS)
905 if ((hdr->sh_flags & SHF_WRITE) == 0)
906 flags |= SEC_READONLY;
907 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
909 else if ((flags & SEC_LOAD) != 0)
911 if ((hdr->sh_flags & SHF_MERGE) != 0)
914 newsect->entsize = hdr->sh_entsize;
915 if ((hdr->sh_flags & SHF_STRINGS) != 0)
916 flags |= SEC_STRINGS;
918 if (hdr->sh_flags & SHF_GROUP)
919 if (!setup_group (abfd, hdr, newsect))
921 if ((hdr->sh_flags & SHF_TLS) != 0)
922 flags |= SEC_THREAD_LOCAL;
923 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
924 flags |= SEC_EXCLUDE;
926 if ((flags & SEC_ALLOC) == 0)
928 /* The debugging sections appear to be recognized only by name,
929 not any sort of flag. Their SEC_ALLOC bits are cleared. */
936 else if (name[1] == 'g' && name[2] == 'n')
937 p = ".gnu.linkonce.wi.", n = 17;
938 else if (name[1] == 'g' && name[2] == 'd')
939 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
940 else if (name[1] == 'l')
942 else if (name[1] == 's')
944 else if (name[1] == 'z')
945 p = ".zdebug", n = 7;
948 if (p != NULL && strncmp (name, p, n) == 0)
949 flags |= SEC_DEBUGGING;
953 /* As a GNU extension, if the name begins with .gnu.linkonce, we
954 only link a single copy of the section. This is used to support
955 g++. g++ will emit each template expansion in its own section.
956 The symbols will be defined as weak, so that multiple definitions
957 are permitted. The GNU linker extension is to actually discard
958 all but one of the sections. */
959 if (CONST_STRNEQ (name, ".gnu.linkonce")
960 && elf_next_in_group (newsect) == NULL)
961 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
963 bed = get_elf_backend_data (abfd);
964 if (bed->elf_backend_section_flags)
965 if (! bed->elf_backend_section_flags (&flags, hdr))
968 if (! bfd_set_section_flags (abfd, newsect, flags))
971 /* We do not parse the PT_NOTE segments as we are interested even in the
972 separate debug info files which may have the segments offsets corrupted.
973 PT_NOTEs from the core files are currently not parsed using BFD. */
974 if (hdr->sh_type == SHT_NOTE)
978 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
981 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
985 if ((flags & SEC_ALLOC) != 0)
987 Elf_Internal_Phdr *phdr;
988 unsigned int i, nload;
990 /* Some ELF linkers produce binaries with all the program header
991 p_paddr fields zero. If we have such a binary with more than
992 one PT_LOAD header, then leave the section lma equal to vma
993 so that we don't create sections with overlapping lma. */
994 phdr = elf_tdata (abfd)->phdr;
995 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
996 if (phdr->p_paddr != 0)
998 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1000 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1003 phdr = elf_tdata (abfd)->phdr;
1004 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1006 if (((phdr->p_type == PT_LOAD
1007 && (hdr->sh_flags & SHF_TLS) == 0)
1008 || phdr->p_type == PT_TLS)
1009 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1011 if ((flags & SEC_LOAD) == 0)
1012 newsect->lma = (phdr->p_paddr
1013 + hdr->sh_addr - phdr->p_vaddr);
1015 /* We used to use the same adjustment for SEC_LOAD
1016 sections, but that doesn't work if the segment
1017 is packed with code from multiple VMAs.
1018 Instead we calculate the section LMA based on
1019 the segment LMA. It is assumed that the
1020 segment will contain sections with contiguous
1021 LMAs, even if the VMAs are not. */
1022 newsect->lma = (phdr->p_paddr
1023 + hdr->sh_offset - phdr->p_offset);
1025 /* With contiguous segments, we can't tell from file
1026 offsets whether a section with zero size should
1027 be placed at the end of one segment or the
1028 beginning of the next. Decide based on vaddr. */
1029 if (hdr->sh_addr >= phdr->p_vaddr
1030 && (hdr->sh_addr + hdr->sh_size
1031 <= phdr->p_vaddr + phdr->p_memsz))
1037 /* Compress/decompress DWARF debug sections with names: .debug_* and
1038 .zdebug_*, after the section flags is set. */
1039 if ((flags & SEC_DEBUGGING)
1040 && ((name[1] == 'd' && name[6] == '_')
1041 || (name[1] == 'z' && name[7] == '_')))
1043 enum { nothing, compress, decompress } action = nothing;
1045 int compression_header_size;
1046 bfd_boolean compressed
1047 = bfd_is_section_compressed_with_header (abfd, newsect,
1048 &compression_header_size);
1052 /* Compressed section. Check if we should decompress. */
1053 if ((abfd->flags & BFD_DECOMPRESS))
1054 action = decompress;
1057 /* Compress the uncompressed section or convert from/to .zdebug*
1058 section. Check if we should compress. */
1059 if (action == nothing)
1061 if (newsect->size != 0
1062 && (abfd->flags & BFD_COMPRESS)
1063 && compression_header_size >= 0
1065 || ((compression_header_size > 0)
1066 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1072 if (action == compress)
1074 if (!bfd_init_section_compress_status (abfd, newsect))
1076 (*_bfd_error_handler)
1077 (_("%B: unable to initialize compress status for section %s"),
1084 if (!bfd_init_section_decompress_status (abfd, newsect))
1086 (*_bfd_error_handler)
1087 (_("%B: unable to initialize decompress status for section %s"),
1094 if (action == decompress
1095 || (action == compress
1096 && (abfd->flags & BFD_COMPRESS_GABI) != 0))
1100 unsigned int len = strlen (name);
1102 new_name = bfd_alloc (abfd, len);
1103 if (new_name == NULL)
1106 memcpy (new_name + 1, name + 2, len - 1);
1109 else if (action == compress
1110 && newsect->compress_status == COMPRESS_SECTION_DONE)
1112 /* PR binutils/18087: Compression does not always make a section
1113 smaller. So only rename the section when compression has
1114 actually taken place. */
1117 unsigned int len = strlen (name);
1119 new_name = bfd_alloc (abfd, len + 2);
1120 if (new_name == NULL)
1124 memcpy (new_name + 2, name + 1, len);
1127 if (new_name != NULL)
1128 bfd_rename_section (abfd, newsect, new_name);
1134 const char *const bfd_elf_section_type_names[] = {
1135 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1136 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1137 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1140 /* ELF relocs are against symbols. If we are producing relocatable
1141 output, and the reloc is against an external symbol, and nothing
1142 has given us any additional addend, the resulting reloc will also
1143 be against the same symbol. In such a case, we don't want to
1144 change anything about the way the reloc is handled, since it will
1145 all be done at final link time. Rather than put special case code
1146 into bfd_perform_relocation, all the reloc types use this howto
1147 function. It just short circuits the reloc if producing
1148 relocatable output against an external symbol. */
1150 bfd_reloc_status_type
1151 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1152 arelent *reloc_entry,
1154 void *data ATTRIBUTE_UNUSED,
1155 asection *input_section,
1157 char **error_message ATTRIBUTE_UNUSED)
1159 if (output_bfd != NULL
1160 && (symbol->flags & BSF_SECTION_SYM) == 0
1161 && (! reloc_entry->howto->partial_inplace
1162 || reloc_entry->addend == 0))
1164 reloc_entry->address += input_section->output_offset;
1165 return bfd_reloc_ok;
1168 return bfd_reloc_continue;
1171 /* Copy the program header and other data from one object module to
1175 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1177 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1178 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1181 if (!elf_flags_init (obfd))
1183 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1184 elf_flags_init (obfd) = TRUE;
1187 elf_gp (obfd) = elf_gp (ibfd);
1189 /* Also copy the EI_OSABI field. */
1190 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1191 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1193 /* Copy object attributes. */
1194 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1199 get_segment_type (unsigned int p_type)
1204 case PT_NULL: pt = "NULL"; break;
1205 case PT_LOAD: pt = "LOAD"; break;
1206 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1207 case PT_INTERP: pt = "INTERP"; break;
1208 case PT_NOTE: pt = "NOTE"; break;
1209 case PT_SHLIB: pt = "SHLIB"; break;
1210 case PT_PHDR: pt = "PHDR"; break;
1211 case PT_TLS: pt = "TLS"; break;
1212 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1213 case PT_GNU_STACK: pt = "STACK"; break;
1214 case PT_GNU_RELRO: pt = "RELRO"; break;
1215 default: pt = NULL; break;
1220 /* Print out the program headers. */
1223 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1225 FILE *f = (FILE *) farg;
1226 Elf_Internal_Phdr *p;
1228 bfd_byte *dynbuf = NULL;
1230 p = elf_tdata (abfd)->phdr;
1235 fprintf (f, _("\nProgram Header:\n"));
1236 c = elf_elfheader (abfd)->e_phnum;
1237 for (i = 0; i < c; i++, p++)
1239 const char *pt = get_segment_type (p->p_type);
1244 sprintf (buf, "0x%lx", p->p_type);
1247 fprintf (f, "%8s off 0x", pt);
1248 bfd_fprintf_vma (abfd, f, p->p_offset);
1249 fprintf (f, " vaddr 0x");
1250 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1251 fprintf (f, " paddr 0x");
1252 bfd_fprintf_vma (abfd, f, p->p_paddr);
1253 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1254 fprintf (f, " filesz 0x");
1255 bfd_fprintf_vma (abfd, f, p->p_filesz);
1256 fprintf (f, " memsz 0x");
1257 bfd_fprintf_vma (abfd, f, p->p_memsz);
1258 fprintf (f, " flags %c%c%c",
1259 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1260 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1261 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1262 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1263 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1268 s = bfd_get_section_by_name (abfd, ".dynamic");
1271 unsigned int elfsec;
1272 unsigned long shlink;
1273 bfd_byte *extdyn, *extdynend;
1275 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1277 fprintf (f, _("\nDynamic Section:\n"));
1279 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1282 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1283 if (elfsec == SHN_BAD)
1285 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1287 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1288 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1291 /* PR 17512: file: 6f427532. */
1292 if (s->size < extdynsize)
1294 extdynend = extdyn + s->size;
1295 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1297 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1299 Elf_Internal_Dyn dyn;
1300 const char *name = "";
1302 bfd_boolean stringp;
1303 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1305 (*swap_dyn_in) (abfd, extdyn, &dyn);
1307 if (dyn.d_tag == DT_NULL)
1314 if (bed->elf_backend_get_target_dtag)
1315 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1317 if (!strcmp (name, ""))
1319 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1324 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1325 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1326 case DT_PLTGOT: name = "PLTGOT"; break;
1327 case DT_HASH: name = "HASH"; break;
1328 case DT_STRTAB: name = "STRTAB"; break;
1329 case DT_SYMTAB: name = "SYMTAB"; break;
1330 case DT_RELA: name = "RELA"; break;
1331 case DT_RELASZ: name = "RELASZ"; break;
1332 case DT_RELAENT: name = "RELAENT"; break;
1333 case DT_STRSZ: name = "STRSZ"; break;
1334 case DT_SYMENT: name = "SYMENT"; break;
1335 case DT_INIT: name = "INIT"; break;
1336 case DT_FINI: name = "FINI"; break;
1337 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1338 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1339 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1340 case DT_REL: name = "REL"; break;
1341 case DT_RELSZ: name = "RELSZ"; break;
1342 case DT_RELENT: name = "RELENT"; break;
1343 case DT_PLTREL: name = "PLTREL"; break;
1344 case DT_DEBUG: name = "DEBUG"; break;
1345 case DT_TEXTREL: name = "TEXTREL"; break;
1346 case DT_JMPREL: name = "JMPREL"; break;
1347 case DT_BIND_NOW: name = "BIND_NOW"; break;
1348 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1349 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1350 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1351 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1352 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1353 case DT_FLAGS: name = "FLAGS"; break;
1354 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1355 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1356 case DT_CHECKSUM: name = "CHECKSUM"; break;
1357 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1358 case DT_MOVEENT: name = "MOVEENT"; break;
1359 case DT_MOVESZ: name = "MOVESZ"; break;
1360 case DT_FEATURE: name = "FEATURE"; break;
1361 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1362 case DT_SYMINSZ: name = "SYMINSZ"; break;
1363 case DT_SYMINENT: name = "SYMINENT"; break;
1364 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1365 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1366 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1367 case DT_PLTPAD: name = "PLTPAD"; break;
1368 case DT_MOVETAB: name = "MOVETAB"; break;
1369 case DT_SYMINFO: name = "SYMINFO"; break;
1370 case DT_RELACOUNT: name = "RELACOUNT"; break;
1371 case DT_RELCOUNT: name = "RELCOUNT"; break;
1372 case DT_FLAGS_1: name = "FLAGS_1"; break;
1373 case DT_VERSYM: name = "VERSYM"; break;
1374 case DT_VERDEF: name = "VERDEF"; break;
1375 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1376 case DT_VERNEED: name = "VERNEED"; break;
1377 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1378 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1379 case DT_USED: name = "USED"; break;
1380 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1381 case DT_GNU_HASH: name = "GNU_HASH"; break;
1384 fprintf (f, " %-20s ", name);
1388 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1393 unsigned int tagv = dyn.d_un.d_val;
1395 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1398 fprintf (f, "%s", string);
1407 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1408 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1410 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1414 if (elf_dynverdef (abfd) != 0)
1416 Elf_Internal_Verdef *t;
1418 fprintf (f, _("\nVersion definitions:\n"));
1419 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1421 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1422 t->vd_flags, t->vd_hash,
1423 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1424 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1426 Elf_Internal_Verdaux *a;
1429 for (a = t->vd_auxptr->vda_nextptr;
1433 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1439 if (elf_dynverref (abfd) != 0)
1441 Elf_Internal_Verneed *t;
1443 fprintf (f, _("\nVersion References:\n"));
1444 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1446 Elf_Internal_Vernaux *a;
1448 fprintf (f, _(" required from %s:\n"),
1449 t->vn_filename ? t->vn_filename : "<corrupt>");
1450 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1451 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1452 a->vna_flags, a->vna_other,
1453 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1465 /* Get version string. */
1468 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1469 bfd_boolean *hidden)
1471 const char *version_string = NULL;
1472 if (elf_dynversym (abfd) != 0
1473 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1475 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1477 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1478 vernum &= VERSYM_VERSION;
1481 version_string = "";
1482 else if (vernum == 1)
1483 version_string = "Base";
1484 else if (vernum <= elf_tdata (abfd)->cverdefs)
1486 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1489 Elf_Internal_Verneed *t;
1491 version_string = "";
1492 for (t = elf_tdata (abfd)->verref;
1496 Elf_Internal_Vernaux *a;
1498 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1500 if (a->vna_other == vernum)
1502 version_string = a->vna_nodename;
1509 return version_string;
1512 /* Display ELF-specific fields of a symbol. */
1515 bfd_elf_print_symbol (bfd *abfd,
1518 bfd_print_symbol_type how)
1520 FILE *file = (FILE *) filep;
1523 case bfd_print_symbol_name:
1524 fprintf (file, "%s", symbol->name);
1526 case bfd_print_symbol_more:
1527 fprintf (file, "elf ");
1528 bfd_fprintf_vma (abfd, file, symbol->value);
1529 fprintf (file, " %lx", (unsigned long) symbol->flags);
1531 case bfd_print_symbol_all:
1533 const char *section_name;
1534 const char *name = NULL;
1535 const struct elf_backend_data *bed;
1536 unsigned char st_other;
1538 const char *version_string;
1541 section_name = symbol->section ? symbol->section->name : "(*none*)";
1543 bed = get_elf_backend_data (abfd);
1544 if (bed->elf_backend_print_symbol_all)
1545 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1549 name = symbol->name;
1550 bfd_print_symbol_vandf (abfd, file, symbol);
1553 fprintf (file, " %s\t", section_name);
1554 /* Print the "other" value for a symbol. For common symbols,
1555 we've already printed the size; now print the alignment.
1556 For other symbols, we have no specified alignment, and
1557 we've printed the address; now print the size. */
1558 if (symbol->section && bfd_is_com_section (symbol->section))
1559 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1561 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1562 bfd_fprintf_vma (abfd, file, val);
1564 /* If we have version information, print it. */
1565 version_string = _bfd_elf_get_symbol_version_string (abfd,
1571 fprintf (file, " %-11s", version_string);
1576 fprintf (file, " (%s)", version_string);
1577 for (i = 10 - strlen (version_string); i > 0; --i)
1582 /* If the st_other field is not zero, print it. */
1583 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1588 case STV_INTERNAL: fprintf (file, " .internal"); break;
1589 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1590 case STV_PROTECTED: fprintf (file, " .protected"); break;
1592 /* Some other non-defined flags are also present, so print
1594 fprintf (file, " 0x%02x", (unsigned int) st_other);
1597 fprintf (file, " %s", name);
1603 /* Allocate an ELF string table--force the first byte to be zero. */
1605 struct bfd_strtab_hash *
1606 _bfd_elf_stringtab_init (void)
1608 struct bfd_strtab_hash *ret;
1610 ret = _bfd_stringtab_init ();
1615 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1616 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1617 if (loc == (bfd_size_type) -1)
1619 _bfd_stringtab_free (ret);
1626 /* ELF .o/exec file reading */
1628 /* Create a new bfd section from an ELF section header. */
1631 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1633 Elf_Internal_Shdr *hdr;
1634 Elf_Internal_Ehdr *ehdr;
1635 const struct elf_backend_data *bed;
1637 bfd_boolean ret = TRUE;
1638 static bfd_boolean * sections_being_created = NULL;
1639 static bfd * sections_being_created_abfd = NULL;
1640 static unsigned int nesting = 0;
1642 if (shindex >= elf_numsections (abfd))
1647 /* PR17512: A corrupt ELF binary might contain a recursive group of
1648 sections, with each the string indicies pointing to the next in the
1649 loop. Detect this here, by refusing to load a section that we are
1650 already in the process of loading. We only trigger this test if
1651 we have nested at least three sections deep as normal ELF binaries
1652 can expect to recurse at least once.
1654 FIXME: It would be better if this array was attached to the bfd,
1655 rather than being held in a static pointer. */
1657 if (sections_being_created_abfd != abfd)
1658 sections_being_created = NULL;
1659 if (sections_being_created == NULL)
1661 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1662 sections_being_created = (bfd_boolean *)
1663 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
1664 sections_being_created_abfd = abfd;
1666 if (sections_being_created [shindex])
1668 (*_bfd_error_handler)
1669 (_("%B: warning: loop in section dependencies detected"), abfd);
1672 sections_being_created [shindex] = TRUE;
1675 hdr = elf_elfsections (abfd)[shindex];
1676 ehdr = elf_elfheader (abfd);
1677 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1682 bed = get_elf_backend_data (abfd);
1683 switch (hdr->sh_type)
1686 /* Inactive section. Throw it away. */
1689 case SHT_PROGBITS: /* Normal section with contents. */
1690 case SHT_NOBITS: /* .bss section. */
1691 case SHT_HASH: /* .hash section. */
1692 case SHT_NOTE: /* .note section. */
1693 case SHT_INIT_ARRAY: /* .init_array section. */
1694 case SHT_FINI_ARRAY: /* .fini_array section. */
1695 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1696 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1697 case SHT_GNU_HASH: /* .gnu.hash section. */
1698 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1701 case SHT_DYNAMIC: /* Dynamic linking information. */
1702 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1705 if (hdr->sh_link > elf_numsections (abfd))
1707 /* PR 10478: Accept Solaris binaries with a sh_link
1708 field set to SHN_BEFORE or SHN_AFTER. */
1709 switch (bfd_get_arch (abfd))
1712 case bfd_arch_sparc:
1713 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1714 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1716 /* Otherwise fall through. */
1721 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1723 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1725 Elf_Internal_Shdr *dynsymhdr;
1727 /* The shared libraries distributed with hpux11 have a bogus
1728 sh_link field for the ".dynamic" section. Find the
1729 string table for the ".dynsym" section instead. */
1730 if (elf_dynsymtab (abfd) != 0)
1732 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1733 hdr->sh_link = dynsymhdr->sh_link;
1737 unsigned int i, num_sec;
1739 num_sec = elf_numsections (abfd);
1740 for (i = 1; i < num_sec; i++)
1742 dynsymhdr = elf_elfsections (abfd)[i];
1743 if (dynsymhdr->sh_type == SHT_DYNSYM)
1745 hdr->sh_link = dynsymhdr->sh_link;
1753 case SHT_SYMTAB: /* A symbol table. */
1754 if (elf_onesymtab (abfd) == shindex)
1757 if (hdr->sh_entsize != bed->s->sizeof_sym)
1760 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1762 if (hdr->sh_size != 0)
1764 /* Some assemblers erroneously set sh_info to one with a
1765 zero sh_size. ld sees this as a global symbol count
1766 of (unsigned) -1. Fix it here. */
1771 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1772 elf_onesymtab (abfd) = shindex;
1773 elf_tdata (abfd)->symtab_hdr = *hdr;
1774 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1775 abfd->flags |= HAS_SYMS;
1777 /* Sometimes a shared object will map in the symbol table. If
1778 SHF_ALLOC is set, and this is a shared object, then we also
1779 treat this section as a BFD section. We can not base the
1780 decision purely on SHF_ALLOC, because that flag is sometimes
1781 set in a relocatable object file, which would confuse the
1783 if ((hdr->sh_flags & SHF_ALLOC) != 0
1784 && (abfd->flags & DYNAMIC) != 0
1785 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1789 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1790 can't read symbols without that section loaded as well. It
1791 is most likely specified by the next section header. */
1792 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1794 unsigned int i, num_sec;
1796 num_sec = elf_numsections (abfd);
1797 for (i = shindex + 1; i < num_sec; i++)
1799 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1800 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1801 && hdr2->sh_link == shindex)
1805 for (i = 1; i < shindex; i++)
1807 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1808 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1809 && hdr2->sh_link == shindex)
1813 ret = bfd_section_from_shdr (abfd, i);
1817 case SHT_DYNSYM: /* A dynamic symbol table. */
1818 if (elf_dynsymtab (abfd) == shindex)
1821 if (hdr->sh_entsize != bed->s->sizeof_sym)
1824 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1826 if (hdr->sh_size != 0)
1829 /* Some linkers erroneously set sh_info to one with a
1830 zero sh_size. ld sees this as a global symbol count
1831 of (unsigned) -1. Fix it here. */
1836 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1837 elf_dynsymtab (abfd) = shindex;
1838 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1839 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1840 abfd->flags |= HAS_SYMS;
1842 /* Besides being a symbol table, we also treat this as a regular
1843 section, so that objcopy can handle it. */
1844 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1847 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
1848 if (elf_symtab_shndx (abfd) == shindex)
1851 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1852 elf_symtab_shndx (abfd) = shindex;
1853 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1854 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1857 case SHT_STRTAB: /* A string table. */
1858 if (hdr->bfd_section != NULL)
1861 if (ehdr->e_shstrndx == shindex)
1863 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1864 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1868 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1871 elf_tdata (abfd)->strtab_hdr = *hdr;
1872 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1876 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1879 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1880 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1881 elf_elfsections (abfd)[shindex] = hdr;
1882 /* We also treat this as a regular section, so that objcopy
1884 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1889 /* If the string table isn't one of the above, then treat it as a
1890 regular section. We need to scan all the headers to be sure,
1891 just in case this strtab section appeared before the above. */
1892 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1894 unsigned int i, num_sec;
1896 num_sec = elf_numsections (abfd);
1897 for (i = 1; i < num_sec; i++)
1899 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1900 if (hdr2->sh_link == shindex)
1902 /* Prevent endless recursion on broken objects. */
1905 if (! bfd_section_from_shdr (abfd, i))
1907 if (elf_onesymtab (abfd) == i)
1909 if (elf_dynsymtab (abfd) == i)
1910 goto dynsymtab_strtab;
1914 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1919 /* *These* do a lot of work -- but build no sections! */
1921 asection *target_sect;
1922 Elf_Internal_Shdr *hdr2, **p_hdr;
1923 unsigned int num_sec = elf_numsections (abfd);
1924 struct bfd_elf_section_data *esdt;
1928 != (bfd_size_type) (hdr->sh_type == SHT_REL
1929 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1932 /* Check for a bogus link to avoid crashing. */
1933 if (hdr->sh_link >= num_sec)
1935 ((*_bfd_error_handler)
1936 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1937 abfd, hdr->sh_link, name, shindex));
1938 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1943 /* For some incomprehensible reason Oracle distributes
1944 libraries for Solaris in which some of the objects have
1945 bogus sh_link fields. It would be nice if we could just
1946 reject them, but, unfortunately, some people need to use
1947 them. We scan through the section headers; if we find only
1948 one suitable symbol table, we clobber the sh_link to point
1949 to it. I hope this doesn't break anything.
1951 Don't do it on executable nor shared library. */
1952 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1953 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1954 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1960 for (scan = 1; scan < num_sec; scan++)
1962 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1963 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1974 hdr->sh_link = found;
1977 /* Get the symbol table. */
1978 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1979 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1980 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1983 /* If this reloc section does not use the main symbol table we
1984 don't treat it as a reloc section. BFD can't adequately
1985 represent such a section, so at least for now, we don't
1986 try. We just present it as a normal section. We also
1987 can't use it as a reloc section if it points to the null
1988 section, an invalid section, another reloc section, or its
1989 sh_link points to the null section. */
1990 if (hdr->sh_link != elf_onesymtab (abfd)
1991 || hdr->sh_link == SHN_UNDEF
1992 || hdr->sh_info == SHN_UNDEF
1993 || hdr->sh_info >= num_sec
1994 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1995 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1997 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2002 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2005 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2006 if (target_sect == NULL)
2009 esdt = elf_section_data (target_sect);
2010 if (hdr->sh_type == SHT_RELA)
2011 p_hdr = &esdt->rela.hdr;
2013 p_hdr = &esdt->rel.hdr;
2015 /* PR 17512: file: 0b4f81b7. */
2018 amt = sizeof (*hdr2);
2019 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2024 elf_elfsections (abfd)[shindex] = hdr2;
2025 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2026 target_sect->flags |= SEC_RELOC;
2027 target_sect->relocation = NULL;
2028 target_sect->rel_filepos = hdr->sh_offset;
2029 /* In the section to which the relocations apply, mark whether
2030 its relocations are of the REL or RELA variety. */
2031 if (hdr->sh_size != 0)
2033 if (hdr->sh_type == SHT_RELA)
2034 target_sect->use_rela_p = 1;
2036 abfd->flags |= HAS_RELOC;
2040 case SHT_GNU_verdef:
2041 elf_dynverdef (abfd) = shindex;
2042 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2043 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2046 case SHT_GNU_versym:
2047 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2050 elf_dynversym (abfd) = shindex;
2051 elf_tdata (abfd)->dynversym_hdr = *hdr;
2052 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2055 case SHT_GNU_verneed:
2056 elf_dynverref (abfd) = shindex;
2057 elf_tdata (abfd)->dynverref_hdr = *hdr;
2058 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2065 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2068 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2071 if (hdr->contents != NULL)
2073 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2074 unsigned int n_elt = hdr->sh_size / sizeof (* idx);
2079 if (idx->flags & GRP_COMDAT)
2080 hdr->bfd_section->flags
2081 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2083 /* We try to keep the same section order as it comes in. */
2086 while (--n_elt != 0)
2090 if (idx->shdr != NULL
2091 && (s = idx->shdr->bfd_section) != NULL
2092 && elf_next_in_group (s) != NULL)
2094 elf_next_in_group (hdr->bfd_section) = s;
2102 /* Possibly an attributes section. */
2103 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2104 || hdr->sh_type == bed->obj_attrs_section_type)
2106 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2108 _bfd_elf_parse_attributes (abfd, hdr);
2112 /* Check for any processor-specific section types. */
2113 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2116 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2118 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2119 /* FIXME: How to properly handle allocated section reserved
2120 for applications? */
2121 (*_bfd_error_handler)
2122 (_("%B: don't know how to handle allocated, application "
2123 "specific section `%s' [0x%8x]"),
2124 abfd, name, hdr->sh_type);
2127 /* Allow sections reserved for applications. */
2128 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2133 else if (hdr->sh_type >= SHT_LOPROC
2134 && hdr->sh_type <= SHT_HIPROC)
2135 /* FIXME: We should handle this section. */
2136 (*_bfd_error_handler)
2137 (_("%B: don't know how to handle processor specific section "
2139 abfd, name, hdr->sh_type);
2140 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2142 /* Unrecognised OS-specific sections. */
2143 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2144 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2145 required to correctly process the section and the file should
2146 be rejected with an error message. */
2147 (*_bfd_error_handler)
2148 (_("%B: don't know how to handle OS specific section "
2150 abfd, name, hdr->sh_type);
2153 /* Otherwise it should be processed. */
2154 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2159 /* FIXME: We should handle this section. */
2160 (*_bfd_error_handler)
2161 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2162 abfd, name, hdr->sh_type);
2170 if (sections_being_created && sections_being_created_abfd == abfd)
2171 sections_being_created [shindex] = FALSE;
2172 if (-- nesting == 0)
2174 sections_being_created = NULL;
2175 sections_being_created_abfd = abfd;
2180 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2183 bfd_sym_from_r_symndx (struct sym_cache *cache,
2185 unsigned long r_symndx)
2187 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2189 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2191 Elf_Internal_Shdr *symtab_hdr;
2192 unsigned char esym[sizeof (Elf64_External_Sym)];
2193 Elf_External_Sym_Shndx eshndx;
2195 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2196 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2197 &cache->sym[ent], esym, &eshndx) == NULL)
2200 if (cache->abfd != abfd)
2202 memset (cache->indx, -1, sizeof (cache->indx));
2205 cache->indx[ent] = r_symndx;
2208 return &cache->sym[ent];
2211 /* Given an ELF section number, retrieve the corresponding BFD
2215 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2217 if (sec_index >= elf_numsections (abfd))
2219 return elf_elfsections (abfd)[sec_index]->bfd_section;
2222 static const struct bfd_elf_special_section special_sections_b[] =
2224 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2225 { NULL, 0, 0, 0, 0 }
2228 static const struct bfd_elf_special_section special_sections_c[] =
2230 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2231 { NULL, 0, 0, 0, 0 }
2234 static const struct bfd_elf_special_section special_sections_d[] =
2236 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2237 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2238 /* There are more DWARF sections than these, but they needn't be added here
2239 unless you have to cope with broken compilers that don't emit section
2240 attributes or you want to help the user writing assembler. */
2241 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2242 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2243 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2244 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2245 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2246 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2247 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2248 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2249 { NULL, 0, 0, 0, 0 }
2252 static const struct bfd_elf_special_section special_sections_f[] =
2254 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2255 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2256 { NULL, 0, 0, 0, 0 }
2259 static const struct bfd_elf_special_section special_sections_g[] =
2261 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2262 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2263 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2264 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2265 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2266 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2267 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2268 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2269 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2270 { NULL, 0, 0, 0, 0 }
2273 static const struct bfd_elf_special_section special_sections_h[] =
2275 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2276 { NULL, 0, 0, 0, 0 }
2279 static const struct bfd_elf_special_section special_sections_i[] =
2281 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2282 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2283 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2284 { NULL, 0, 0, 0, 0 }
2287 static const struct bfd_elf_special_section special_sections_l[] =
2289 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2290 { NULL, 0, 0, 0, 0 }
2293 static const struct bfd_elf_special_section special_sections_n[] =
2295 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2296 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2297 { NULL, 0, 0, 0, 0 }
2300 static const struct bfd_elf_special_section special_sections_p[] =
2302 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2303 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2304 { NULL, 0, 0, 0, 0 }
2307 static const struct bfd_elf_special_section special_sections_r[] =
2309 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2310 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2311 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2312 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2313 { NULL, 0, 0, 0, 0 }
2316 static const struct bfd_elf_special_section special_sections_s[] =
2318 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2319 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2320 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2321 /* See struct bfd_elf_special_section declaration for the semantics of
2322 this special case where .prefix_length != strlen (.prefix). */
2323 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2324 { NULL, 0, 0, 0, 0 }
2327 static const struct bfd_elf_special_section special_sections_t[] =
2329 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2330 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2331 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2332 { NULL, 0, 0, 0, 0 }
2335 static const struct bfd_elf_special_section special_sections_z[] =
2337 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2338 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2339 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2340 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2341 { NULL, 0, 0, 0, 0 }
2344 static const struct bfd_elf_special_section * const special_sections[] =
2346 special_sections_b, /* 'b' */
2347 special_sections_c, /* 'c' */
2348 special_sections_d, /* 'd' */
2350 special_sections_f, /* 'f' */
2351 special_sections_g, /* 'g' */
2352 special_sections_h, /* 'h' */
2353 special_sections_i, /* 'i' */
2356 special_sections_l, /* 'l' */
2358 special_sections_n, /* 'n' */
2360 special_sections_p, /* 'p' */
2362 special_sections_r, /* 'r' */
2363 special_sections_s, /* 's' */
2364 special_sections_t, /* 't' */
2370 special_sections_z /* 'z' */
2373 const struct bfd_elf_special_section *
2374 _bfd_elf_get_special_section (const char *name,
2375 const struct bfd_elf_special_section *spec,
2381 len = strlen (name);
2383 for (i = 0; spec[i].prefix != NULL; i++)
2386 int prefix_len = spec[i].prefix_length;
2388 if (len < prefix_len)
2390 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2393 suffix_len = spec[i].suffix_length;
2394 if (suffix_len <= 0)
2396 if (name[prefix_len] != 0)
2398 if (suffix_len == 0)
2400 if (name[prefix_len] != '.'
2401 && (suffix_len == -2
2402 || (rela && spec[i].type == SHT_REL)))
2408 if (len < prefix_len + suffix_len)
2410 if (memcmp (name + len - suffix_len,
2411 spec[i].prefix + prefix_len,
2421 const struct bfd_elf_special_section *
2422 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2425 const struct bfd_elf_special_section *spec;
2426 const struct elf_backend_data *bed;
2428 /* See if this is one of the special sections. */
2429 if (sec->name == NULL)
2432 bed = get_elf_backend_data (abfd);
2433 spec = bed->special_sections;
2436 spec = _bfd_elf_get_special_section (sec->name,
2437 bed->special_sections,
2443 if (sec->name[0] != '.')
2446 i = sec->name[1] - 'b';
2447 if (i < 0 || i > 'z' - 'b')
2450 spec = special_sections[i];
2455 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2459 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2461 struct bfd_elf_section_data *sdata;
2462 const struct elf_backend_data *bed;
2463 const struct bfd_elf_special_section *ssect;
2465 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2468 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2472 sec->used_by_bfd = sdata;
2475 /* Indicate whether or not this section should use RELA relocations. */
2476 bed = get_elf_backend_data (abfd);
2477 sec->use_rela_p = bed->default_use_rela_p;
2479 /* When we read a file, we don't need to set ELF section type and
2480 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2481 anyway. We will set ELF section type and flags for all linker
2482 created sections. If user specifies BFD section flags, we will
2483 set ELF section type and flags based on BFD section flags in
2484 elf_fake_sections. Special handling for .init_array/.fini_array
2485 output sections since they may contain .ctors/.dtors input
2486 sections. We don't want _bfd_elf_init_private_section_data to
2487 copy ELF section type from .ctors/.dtors input sections. */
2488 if (abfd->direction != read_direction
2489 || (sec->flags & SEC_LINKER_CREATED) != 0)
2491 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2494 || (sec->flags & SEC_LINKER_CREATED) != 0
2495 || ssect->type == SHT_INIT_ARRAY
2496 || ssect->type == SHT_FINI_ARRAY))
2498 elf_section_type (sec) = ssect->type;
2499 elf_section_flags (sec) = ssect->attr;
2503 return _bfd_generic_new_section_hook (abfd, sec);
2506 /* Create a new bfd section from an ELF program header.
2508 Since program segments have no names, we generate a synthetic name
2509 of the form segment<NUM>, where NUM is generally the index in the
2510 program header table. For segments that are split (see below) we
2511 generate the names segment<NUM>a and segment<NUM>b.
2513 Note that some program segments may have a file size that is different than
2514 (less than) the memory size. All this means is that at execution the
2515 system must allocate the amount of memory specified by the memory size,
2516 but only initialize it with the first "file size" bytes read from the
2517 file. This would occur for example, with program segments consisting
2518 of combined data+bss.
2520 To handle the above situation, this routine generates TWO bfd sections
2521 for the single program segment. The first has the length specified by
2522 the file size of the segment, and the second has the length specified
2523 by the difference between the two sizes. In effect, the segment is split
2524 into its initialized and uninitialized parts.
2529 _bfd_elf_make_section_from_phdr (bfd *abfd,
2530 Elf_Internal_Phdr *hdr,
2532 const char *type_name)
2540 split = ((hdr->p_memsz > 0)
2541 && (hdr->p_filesz > 0)
2542 && (hdr->p_memsz > hdr->p_filesz));
2544 if (hdr->p_filesz > 0)
2546 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2547 len = strlen (namebuf) + 1;
2548 name = (char *) bfd_alloc (abfd, len);
2551 memcpy (name, namebuf, len);
2552 newsect = bfd_make_section (abfd, name);
2553 if (newsect == NULL)
2555 newsect->vma = hdr->p_vaddr;
2556 newsect->lma = hdr->p_paddr;
2557 newsect->size = hdr->p_filesz;
2558 newsect->filepos = hdr->p_offset;
2559 newsect->flags |= SEC_HAS_CONTENTS;
2560 newsect->alignment_power = bfd_log2 (hdr->p_align);
2561 if (hdr->p_type == PT_LOAD)
2563 newsect->flags |= SEC_ALLOC;
2564 newsect->flags |= SEC_LOAD;
2565 if (hdr->p_flags & PF_X)
2567 /* FIXME: all we known is that it has execute PERMISSION,
2569 newsect->flags |= SEC_CODE;
2572 if (!(hdr->p_flags & PF_W))
2574 newsect->flags |= SEC_READONLY;
2578 if (hdr->p_memsz > hdr->p_filesz)
2582 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2583 len = strlen (namebuf) + 1;
2584 name = (char *) bfd_alloc (abfd, len);
2587 memcpy (name, namebuf, len);
2588 newsect = bfd_make_section (abfd, name);
2589 if (newsect == NULL)
2591 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2592 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2593 newsect->size = hdr->p_memsz - hdr->p_filesz;
2594 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2595 align = newsect->vma & -newsect->vma;
2596 if (align == 0 || align > hdr->p_align)
2597 align = hdr->p_align;
2598 newsect->alignment_power = bfd_log2 (align);
2599 if (hdr->p_type == PT_LOAD)
2601 /* Hack for gdb. Segments that have not been modified do
2602 not have their contents written to a core file, on the
2603 assumption that a debugger can find the contents in the
2604 executable. We flag this case by setting the fake
2605 section size to zero. Note that "real" bss sections will
2606 always have their contents dumped to the core file. */
2607 if (bfd_get_format (abfd) == bfd_core)
2609 newsect->flags |= SEC_ALLOC;
2610 if (hdr->p_flags & PF_X)
2611 newsect->flags |= SEC_CODE;
2613 if (!(hdr->p_flags & PF_W))
2614 newsect->flags |= SEC_READONLY;
2621 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2623 const struct elf_backend_data *bed;
2625 switch (hdr->p_type)
2628 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2631 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2634 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2637 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2640 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2642 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2647 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2650 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2652 case PT_GNU_EH_FRAME:
2653 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2657 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2660 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2663 /* Check for any processor-specific program segment types. */
2664 bed = get_elf_backend_data (abfd);
2665 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2669 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2673 _bfd_elf_single_rel_hdr (asection *sec)
2675 if (elf_section_data (sec)->rel.hdr)
2677 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2678 return elf_section_data (sec)->rel.hdr;
2681 return elf_section_data (sec)->rela.hdr;
2684 /* Allocate and initialize a section-header for a new reloc section,
2685 containing relocations against ASECT. It is stored in RELDATA. If
2686 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2690 _bfd_elf_init_reloc_shdr (bfd *abfd,
2691 struct bfd_elf_section_reloc_data *reldata,
2693 bfd_boolean use_rela_p)
2695 Elf_Internal_Shdr *rel_hdr;
2697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2700 amt = sizeof (Elf_Internal_Shdr);
2701 BFD_ASSERT (reldata->hdr == NULL);
2702 rel_hdr = bfd_zalloc (abfd, amt);
2703 reldata->hdr = rel_hdr;
2705 amt = sizeof ".rela" + strlen (asect->name);
2706 name = (char *) bfd_alloc (abfd, amt);
2709 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2711 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2713 if (rel_hdr->sh_name == (unsigned int) -1)
2715 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2716 rel_hdr->sh_entsize = (use_rela_p
2717 ? bed->s->sizeof_rela
2718 : bed->s->sizeof_rel);
2719 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2720 rel_hdr->sh_flags = 0;
2721 rel_hdr->sh_addr = 0;
2722 rel_hdr->sh_size = 0;
2723 rel_hdr->sh_offset = 0;
2728 /* Return the default section type based on the passed in section flags. */
2731 bfd_elf_get_default_section_type (flagword flags)
2733 if ((flags & SEC_ALLOC) != 0
2734 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2736 return SHT_PROGBITS;
2739 struct fake_section_arg
2741 struct bfd_link_info *link_info;
2745 /* Set up an ELF internal section header for a section. */
2748 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2750 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2751 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2752 struct bfd_elf_section_data *esd = elf_section_data (asect);
2753 Elf_Internal_Shdr *this_hdr;
2754 unsigned int sh_type;
2755 const char *name = asect->name;
2759 /* We already failed; just get out of the bfd_map_over_sections
2764 this_hdr = &esd->this_hdr;
2766 /* For linking, compress DWARF debug sections with names: .debug_*. */
2768 && (arg->link_info->compress_debug & COMPRESS_DEBUG)
2769 && (asect->flags & SEC_DEBUGGING)
2773 /* Set SEC_ELF_COMPRESS to indicate this section should be
2775 asect->flags |= SEC_ELF_COMPRESS;
2777 if (arg->link_info->compress_debug != COMPRESS_DEBUG_GABI_ZLIB)
2779 /* If SHF_COMPRESSED isn't used, rename compressed DWARF
2780 debug section to .zdebug_*. */
2781 unsigned int len = strlen (name);
2782 char *new_name = bfd_alloc (abfd, len + 2);
2783 if (new_name == NULL)
2790 memcpy (new_name + 2, name + 1, len);
2791 bfd_rename_section (abfd, asect, new_name);
2796 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2798 if (this_hdr->sh_name == (unsigned int) -1)
2804 /* Don't clear sh_flags. Assembler may set additional bits. */
2806 if ((asect->flags & SEC_ALLOC) != 0
2807 || asect->user_set_vma)
2808 this_hdr->sh_addr = asect->vma;
2810 this_hdr->sh_addr = 0;
2812 this_hdr->sh_offset = 0;
2813 this_hdr->sh_size = asect->size;
2814 this_hdr->sh_link = 0;
2815 /* PR 17512: file: 0eb809fe, 8b0535ee. */
2816 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
2818 (*_bfd_error_handler)
2819 (_("%B: error: Alignment power %d of section `%A' is too big"),
2820 abfd, asect, asect->alignment_power);
2824 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2825 /* The sh_entsize and sh_info fields may have been set already by
2826 copy_private_section_data. */
2828 this_hdr->bfd_section = asect;
2829 this_hdr->contents = NULL;
2831 /* If the section type is unspecified, we set it based on
2833 if ((asect->flags & SEC_GROUP) != 0)
2834 sh_type = SHT_GROUP;
2836 sh_type = bfd_elf_get_default_section_type (asect->flags);
2838 if (this_hdr->sh_type == SHT_NULL)
2839 this_hdr->sh_type = sh_type;
2840 else if (this_hdr->sh_type == SHT_NOBITS
2841 && sh_type == SHT_PROGBITS
2842 && (asect->flags & SEC_ALLOC) != 0)
2844 /* Warn if we are changing a NOBITS section to PROGBITS, but
2845 allow the link to proceed. This can happen when users link
2846 non-bss input sections to bss output sections, or emit data
2847 to a bss output section via a linker script. */
2848 (*_bfd_error_handler)
2849 (_("warning: section `%A' type changed to PROGBITS"), asect);
2850 this_hdr->sh_type = sh_type;
2853 switch (this_hdr->sh_type)
2859 case SHT_INIT_ARRAY:
2860 case SHT_FINI_ARRAY:
2861 case SHT_PREINIT_ARRAY:
2868 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2872 this_hdr->sh_entsize = bed->s->sizeof_sym;
2876 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2880 if (get_elf_backend_data (abfd)->may_use_rela_p)
2881 this_hdr->sh_entsize = bed->s->sizeof_rela;
2885 if (get_elf_backend_data (abfd)->may_use_rel_p)
2886 this_hdr->sh_entsize = bed->s->sizeof_rel;
2889 case SHT_GNU_versym:
2890 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2893 case SHT_GNU_verdef:
2894 this_hdr->sh_entsize = 0;
2895 /* objcopy or strip will copy over sh_info, but may not set
2896 cverdefs. The linker will set cverdefs, but sh_info will be
2898 if (this_hdr->sh_info == 0)
2899 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2901 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2902 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2905 case SHT_GNU_verneed:
2906 this_hdr->sh_entsize = 0;
2907 /* objcopy or strip will copy over sh_info, but may not set
2908 cverrefs. The linker will set cverrefs, but sh_info will be
2910 if (this_hdr->sh_info == 0)
2911 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2913 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2914 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2918 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2922 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2926 if ((asect->flags & SEC_ALLOC) != 0)
2927 this_hdr->sh_flags |= SHF_ALLOC;
2928 if ((asect->flags & SEC_READONLY) == 0)
2929 this_hdr->sh_flags |= SHF_WRITE;
2930 if ((asect->flags & SEC_CODE) != 0)
2931 this_hdr->sh_flags |= SHF_EXECINSTR;
2932 if ((asect->flags & SEC_MERGE) != 0)
2934 this_hdr->sh_flags |= SHF_MERGE;
2935 this_hdr->sh_entsize = asect->entsize;
2936 if ((asect->flags & SEC_STRINGS) != 0)
2937 this_hdr->sh_flags |= SHF_STRINGS;
2939 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2940 this_hdr->sh_flags |= SHF_GROUP;
2941 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2943 this_hdr->sh_flags |= SHF_TLS;
2944 if (asect->size == 0
2945 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2947 struct bfd_link_order *o = asect->map_tail.link_order;
2949 this_hdr->sh_size = 0;
2952 this_hdr->sh_size = o->offset + o->size;
2953 if (this_hdr->sh_size != 0)
2954 this_hdr->sh_type = SHT_NOBITS;
2958 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2959 this_hdr->sh_flags |= SHF_EXCLUDE;
2961 /* If the section has relocs, set up a section header for the
2962 SHT_REL[A] section. If two relocation sections are required for
2963 this section, it is up to the processor-specific back-end to
2964 create the other. */
2965 if ((asect->flags & SEC_RELOC) != 0)
2967 /* When doing a relocatable link, create both REL and RELA sections if
2970 /* Do the normal setup if we wouldn't create any sections here. */
2971 && esd->rel.count + esd->rela.count > 0
2972 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2974 if (esd->rel.count && esd->rel.hdr == NULL
2975 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2980 if (esd->rela.count && esd->rela.hdr == NULL
2981 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2987 else if (!_bfd_elf_init_reloc_shdr (abfd,
2989 ? &esd->rela : &esd->rel),
2995 /* Check for processor-specific section types. */
2996 sh_type = this_hdr->sh_type;
2997 if (bed->elf_backend_fake_sections
2998 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3001 if (sh_type == SHT_NOBITS && asect->size != 0)
3003 /* Don't change the header type from NOBITS if we are being
3004 called for objcopy --only-keep-debug. */
3005 this_hdr->sh_type = sh_type;
3009 /* Fill in the contents of a SHT_GROUP section. Called from
3010 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3011 when ELF targets use the generic linker, ld. Called for ld -r
3012 from bfd_elf_final_link. */
3015 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3017 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3018 asection *elt, *first;
3022 /* Ignore linker created group section. See elfNN_ia64_object_p in
3024 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
3028 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3030 unsigned long symindx = 0;
3032 /* elf_group_id will have been set up by objcopy and the
3034 if (elf_group_id (sec) != NULL)
3035 symindx = elf_group_id (sec)->udata.i;
3039 /* If called from the assembler, swap_out_syms will have set up
3040 elf_section_syms. */
3041 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3042 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3044 elf_section_data (sec)->this_hdr.sh_info = symindx;
3046 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3048 /* The ELF backend linker sets sh_info to -2 when the group
3049 signature symbol is global, and thus the index can't be
3050 set until all local symbols are output. */
3051 asection *igroup = elf_sec_group (elf_next_in_group (sec));
3052 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
3053 unsigned long symndx = sec_data->this_hdr.sh_info;
3054 unsigned long extsymoff = 0;
3055 struct elf_link_hash_entry *h;
3057 if (!elf_bad_symtab (igroup->owner))
3059 Elf_Internal_Shdr *symtab_hdr;
3061 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3062 extsymoff = symtab_hdr->sh_info;
3064 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3065 while (h->root.type == bfd_link_hash_indirect
3066 || h->root.type == bfd_link_hash_warning)
3067 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3069 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3072 /* The contents won't be allocated for "ld -r" or objcopy. */
3074 if (sec->contents == NULL)
3077 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3079 /* Arrange for the section to be written out. */
3080 elf_section_data (sec)->this_hdr.contents = sec->contents;
3081 if (sec->contents == NULL)
3088 loc = sec->contents + sec->size;
3090 /* Get the pointer to the first section in the group that gas
3091 squirreled away here. objcopy arranges for this to be set to the
3092 start of the input section group. */
3093 first = elt = elf_next_in_group (sec);
3095 /* First element is a flag word. Rest of section is elf section
3096 indices for all the sections of the group. Write them backwards
3097 just to keep the group in the same order as given in .section
3098 directives, not that it matters. */
3105 s = s->output_section;
3107 && !bfd_is_abs_section (s))
3109 unsigned int idx = elf_section_data (s)->this_idx;
3112 H_PUT_32 (abfd, idx, loc);
3114 elt = elf_next_in_group (elt);
3119 if ((loc -= 4) != sec->contents)
3122 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3125 /* Return the section which RELOC_SEC applies to. */
3128 _bfd_elf_get_reloc_section (asection *reloc_sec)
3134 if (reloc_sec == NULL)
3137 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3138 if (type != SHT_REL && type != SHT_RELA)
3141 /* We look up the section the relocs apply to by name. */
3142 name = reloc_sec->name;
3143 if (type == SHT_REL)
3148 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3149 section apply to .got.plt section. */
3150 abfd = reloc_sec->owner;
3151 if (get_elf_backend_data (abfd)->want_got_plt
3152 && strcmp (name, ".plt") == 0)
3154 /* .got.plt is a linker created input section. It may be mapped
3155 to some other output section. Try two likely sections. */
3157 reloc_sec = bfd_get_section_by_name (abfd, name);
3158 if (reloc_sec != NULL)
3163 reloc_sec = bfd_get_section_by_name (abfd, name);
3167 /* Assign all ELF section numbers. The dummy first section is handled here
3168 too. The link/info pointers for the standard section types are filled
3169 in here too, while we're at it. */
3172 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3174 struct elf_obj_tdata *t = elf_tdata (abfd);
3176 unsigned int section_number, secn;
3177 Elf_Internal_Shdr **i_shdrp;
3178 struct bfd_elf_section_data *d;
3179 bfd_boolean need_symtab;
3183 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3185 /* SHT_GROUP sections are in relocatable files only. */
3186 if (link_info == NULL || link_info->relocatable)
3188 /* Put SHT_GROUP sections first. */
3189 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3191 d = elf_section_data (sec);
3193 if (d->this_hdr.sh_type == SHT_GROUP)
3195 if (sec->flags & SEC_LINKER_CREATED)
3197 /* Remove the linker created SHT_GROUP sections. */
3198 bfd_section_list_remove (abfd, sec);
3199 abfd->section_count--;
3202 d->this_idx = section_number++;
3207 for (sec = abfd->sections; sec; sec = sec->next)
3209 d = elf_section_data (sec);
3211 if (d->this_hdr.sh_type != SHT_GROUP)
3212 d->this_idx = section_number++;
3213 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3216 d->rel.idx = section_number++;
3217 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3224 d->rela.idx = section_number++;
3225 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3231 elf_shstrtab_sec (abfd) = section_number++;
3232 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3233 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3235 need_symtab = (bfd_get_symcount (abfd) > 0
3236 || (link_info == NULL
3237 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3241 elf_onesymtab (abfd) = section_number++;
3242 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3243 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3245 elf_symtab_shndx (abfd) = section_number++;
3246 t->symtab_shndx_hdr.sh_name
3247 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3248 ".symtab_shndx", FALSE);
3249 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3252 elf_strtab_sec (abfd) = section_number++;
3253 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3256 if (section_number >= SHN_LORESERVE)
3258 _bfd_error_handler (_("%B: too many sections: %u"),
3259 abfd, section_number);
3263 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3264 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3266 elf_numsections (abfd) = section_number;
3267 elf_elfheader (abfd)->e_shnum = section_number;
3269 /* Set up the list of section header pointers, in agreement with the
3271 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3272 sizeof (Elf_Internal_Shdr *));
3273 if (i_shdrp == NULL)
3276 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3277 sizeof (Elf_Internal_Shdr));
3278 if (i_shdrp[0] == NULL)
3280 bfd_release (abfd, i_shdrp);
3284 elf_elfsections (abfd) = i_shdrp;
3286 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3289 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3290 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3292 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3293 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3295 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3296 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3299 for (sec = abfd->sections; sec; sec = sec->next)
3303 d = elf_section_data (sec);
3305 i_shdrp[d->this_idx] = &d->this_hdr;
3306 if (d->rel.idx != 0)
3307 i_shdrp[d->rel.idx] = d->rel.hdr;
3308 if (d->rela.idx != 0)
3309 i_shdrp[d->rela.idx] = d->rela.hdr;
3311 /* Fill in the sh_link and sh_info fields while we're at it. */
3313 /* sh_link of a reloc section is the section index of the symbol
3314 table. sh_info is the section index of the section to which
3315 the relocation entries apply. */
3316 if (d->rel.idx != 0)
3318 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3319 d->rel.hdr->sh_info = d->this_idx;
3320 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3322 if (d->rela.idx != 0)
3324 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3325 d->rela.hdr->sh_info = d->this_idx;
3326 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3329 /* We need to set up sh_link for SHF_LINK_ORDER. */
3330 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3332 s = elf_linked_to_section (sec);
3335 /* elf_linked_to_section points to the input section. */
3336 if (link_info != NULL)
3338 /* Check discarded linkonce section. */
3339 if (discarded_section (s))
3342 (*_bfd_error_handler)
3343 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3344 abfd, d->this_hdr.bfd_section,
3346 /* Point to the kept section if it has the same
3347 size as the discarded one. */
3348 kept = _bfd_elf_check_kept_section (s, link_info);
3351 bfd_set_error (bfd_error_bad_value);
3357 s = s->output_section;
3358 BFD_ASSERT (s != NULL);
3362 /* Handle objcopy. */
3363 if (s->output_section == NULL)
3365 (*_bfd_error_handler)
3366 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3367 abfd, d->this_hdr.bfd_section, s, s->owner);
3368 bfd_set_error (bfd_error_bad_value);
3371 s = s->output_section;
3373 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3378 The Intel C compiler generates SHT_IA_64_UNWIND with
3379 SHF_LINK_ORDER. But it doesn't set the sh_link or
3380 sh_info fields. Hence we could get the situation
3382 const struct elf_backend_data *bed
3383 = get_elf_backend_data (abfd);
3384 if (bed->link_order_error_handler)
3385 bed->link_order_error_handler
3386 (_("%B: warning: sh_link not set for section `%A'"),
3391 switch (d->this_hdr.sh_type)
3395 /* A reloc section which we are treating as a normal BFD
3396 section. sh_link is the section index of the symbol
3397 table. sh_info is the section index of the section to
3398 which the relocation entries apply. We assume that an
3399 allocated reloc section uses the dynamic symbol table.
3400 FIXME: How can we be sure? */
3401 s = bfd_get_section_by_name (abfd, ".dynsym");
3403 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3405 s = get_elf_backend_data (abfd)->get_reloc_section (sec);
3408 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3409 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3414 /* We assume that a section named .stab*str is a stabs
3415 string section. We look for a section with the same name
3416 but without the trailing ``str'', and set its sh_link
3417 field to point to this section. */
3418 if (CONST_STRNEQ (sec->name, ".stab")
3419 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3424 len = strlen (sec->name);
3425 alc = (char *) bfd_malloc (len - 2);
3428 memcpy (alc, sec->name, len - 3);
3429 alc[len - 3] = '\0';
3430 s = bfd_get_section_by_name (abfd, alc);
3434 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3436 /* This is a .stab section. */
3437 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3438 elf_section_data (s)->this_hdr.sh_entsize
3439 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3446 case SHT_GNU_verneed:
3447 case SHT_GNU_verdef:
3448 /* sh_link is the section header index of the string table
3449 used for the dynamic entries, or the symbol table, or the
3451 s = bfd_get_section_by_name (abfd, ".dynstr");
3453 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3456 case SHT_GNU_LIBLIST:
3457 /* sh_link is the section header index of the prelink library
3458 list used for the dynamic entries, or the symbol table, or
3459 the version strings. */
3460 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3461 ? ".dynstr" : ".gnu.libstr");
3463 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3468 case SHT_GNU_versym:
3469 /* sh_link is the section header index of the symbol table
3470 this hash table or version table is for. */
3471 s = bfd_get_section_by_name (abfd, ".dynsym");
3473 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3477 d->this_hdr.sh_link = elf_onesymtab (abfd);
3481 for (secn = 1; secn < section_number; ++secn)
3482 if (i_shdrp[secn] == NULL)
3483 i_shdrp[secn] = i_shdrp[0];
3485 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3486 i_shdrp[secn]->sh_name);
3491 sym_is_global (bfd *abfd, asymbol *sym)
3493 /* If the backend has a special mapping, use it. */
3494 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3495 if (bed->elf_backend_sym_is_global)
3496 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3498 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3499 || bfd_is_und_section (bfd_get_section (sym))
3500 || bfd_is_com_section (bfd_get_section (sym)));
3503 /* Don't output section symbols for sections that are not going to be
3504 output, that are duplicates or there is no BFD section. */
3507 ignore_section_sym (bfd *abfd, asymbol *sym)
3509 elf_symbol_type *type_ptr;
3511 if ((sym->flags & BSF_SECTION_SYM) == 0)
3514 type_ptr = elf_symbol_from (abfd, sym);
3515 return ((type_ptr != NULL
3516 && type_ptr->internal_elf_sym.st_shndx != 0
3517 && bfd_is_abs_section (sym->section))
3518 || !(sym->section->owner == abfd
3519 || (sym->section->output_section->owner == abfd
3520 && sym->section->output_offset == 0)
3521 || bfd_is_abs_section (sym->section)));
3524 /* Map symbol from it's internal number to the external number, moving
3525 all local symbols to be at the head of the list. */
3528 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3530 unsigned int symcount = bfd_get_symcount (abfd);
3531 asymbol **syms = bfd_get_outsymbols (abfd);
3532 asymbol **sect_syms;
3533 unsigned int num_locals = 0;
3534 unsigned int num_globals = 0;
3535 unsigned int num_locals2 = 0;
3536 unsigned int num_globals2 = 0;
3543 fprintf (stderr, "elf_map_symbols\n");
3547 for (asect = abfd->sections; asect; asect = asect->next)
3549 if (max_index < asect->index)
3550 max_index = asect->index;
3554 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3555 if (sect_syms == NULL)
3557 elf_section_syms (abfd) = sect_syms;
3558 elf_num_section_syms (abfd) = max_index;
3560 /* Init sect_syms entries for any section symbols we have already
3561 decided to output. */
3562 for (idx = 0; idx < symcount; idx++)
3564 asymbol *sym = syms[idx];
3566 if ((sym->flags & BSF_SECTION_SYM) != 0
3568 && !ignore_section_sym (abfd, sym)
3569 && !bfd_is_abs_section (sym->section))
3571 asection *sec = sym->section;
3573 if (sec->owner != abfd)
3574 sec = sec->output_section;
3576 sect_syms[sec->index] = syms[idx];
3580 /* Classify all of the symbols. */
3581 for (idx = 0; idx < symcount; idx++)
3583 if (sym_is_global (abfd, syms[idx]))
3585 else if (!ignore_section_sym (abfd, syms[idx]))
3589 /* We will be adding a section symbol for each normal BFD section. Most
3590 sections will already have a section symbol in outsymbols, but
3591 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3592 at least in that case. */
3593 for (asect = abfd->sections; asect; asect = asect->next)
3595 if (sect_syms[asect->index] == NULL)
3597 if (!sym_is_global (abfd, asect->symbol))
3604 /* Now sort the symbols so the local symbols are first. */
3605 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3606 sizeof (asymbol *));
3608 if (new_syms == NULL)
3611 for (idx = 0; idx < symcount; idx++)
3613 asymbol *sym = syms[idx];
3616 if (sym_is_global (abfd, sym))
3617 i = num_locals + num_globals2++;
3618 else if (!ignore_section_sym (abfd, sym))
3623 sym->udata.i = i + 1;
3625 for (asect = abfd->sections; asect; asect = asect->next)
3627 if (sect_syms[asect->index] == NULL)
3629 asymbol *sym = asect->symbol;
3632 sect_syms[asect->index] = sym;
3633 if (!sym_is_global (abfd, sym))
3636 i = num_locals + num_globals2++;
3638 sym->udata.i = i + 1;
3642 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3644 *pnum_locals = num_locals;
3648 /* Align to the maximum file alignment that could be required for any
3649 ELF data structure. */
3651 static inline file_ptr
3652 align_file_position (file_ptr off, int align)
3654 return (off + align - 1) & ~(align - 1);
3657 /* Assign a file position to a section, optionally aligning to the
3658 required section alignment. */
3661 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3665 if (align && i_shdrp->sh_addralign > 1)
3666 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3667 i_shdrp->sh_offset = offset;
3668 if (i_shdrp->bfd_section != NULL)
3669 i_shdrp->bfd_section->filepos = offset;
3670 if (i_shdrp->sh_type != SHT_NOBITS)
3671 offset += i_shdrp->sh_size;
3675 /* Compute the file positions we are going to put the sections at, and
3676 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3677 is not NULL, this is being called by the ELF backend linker. */
3680 _bfd_elf_compute_section_file_positions (bfd *abfd,
3681 struct bfd_link_info *link_info)
3683 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3684 struct fake_section_arg fsargs;
3686 struct bfd_strtab_hash *strtab = NULL;
3687 Elf_Internal_Shdr *shstrtab_hdr;
3688 bfd_boolean need_symtab;
3690 if (abfd->output_has_begun)
3693 /* Do any elf backend specific processing first. */
3694 if (bed->elf_backend_begin_write_processing)
3695 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3697 if (! prep_headers (abfd))
3700 /* Post process the headers if necessary. */
3701 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3703 fsargs.failed = FALSE;
3704 fsargs.link_info = link_info;
3705 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3709 if (!assign_section_numbers (abfd, link_info))
3712 /* The backend linker builds symbol table information itself. */
3713 need_symtab = (link_info == NULL
3714 && (bfd_get_symcount (abfd) > 0
3715 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3719 /* Non-zero if doing a relocatable link. */
3720 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3722 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3727 if (link_info == NULL)
3729 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3734 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3735 /* sh_name was set in prep_headers. */
3736 shstrtab_hdr->sh_type = SHT_STRTAB;
3737 shstrtab_hdr->sh_flags = 0;
3738 shstrtab_hdr->sh_addr = 0;
3739 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3740 shstrtab_hdr->sh_entsize = 0;
3741 shstrtab_hdr->sh_link = 0;
3742 shstrtab_hdr->sh_info = 0;
3743 /* sh_offset is set in assign_file_positions_except_relocs. */
3744 shstrtab_hdr->sh_addralign = 1;
3746 if (!assign_file_positions_except_relocs (abfd, link_info))
3752 Elf_Internal_Shdr *hdr;
3754 off = elf_next_file_pos (abfd);
3756 hdr = &elf_tdata (abfd)->symtab_hdr;
3757 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3759 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3760 if (hdr->sh_size != 0)
3761 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3763 hdr = &elf_tdata (abfd)->strtab_hdr;
3764 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3766 elf_next_file_pos (abfd) = off;
3768 /* Now that we know where the .strtab section goes, write it
3770 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3771 || ! _bfd_stringtab_emit (abfd, strtab))
3773 _bfd_stringtab_free (strtab);
3776 abfd->output_has_begun = TRUE;
3781 /* Make an initial estimate of the size of the program header. If we
3782 get the number wrong here, we'll redo section placement. */
3784 static bfd_size_type
3785 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3789 const struct elf_backend_data *bed;
3791 /* Assume we will need exactly two PT_LOAD segments: one for text
3792 and one for data. */
3795 s = bfd_get_section_by_name (abfd, ".interp");
3796 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3798 /* If we have a loadable interpreter section, we need a
3799 PT_INTERP segment. In this case, assume we also need a
3800 PT_PHDR segment, although that may not be true for all
3805 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3807 /* We need a PT_DYNAMIC segment. */
3811 if (info != NULL && info->relro)
3813 /* We need a PT_GNU_RELRO segment. */
3817 if (elf_eh_frame_hdr (abfd))
3819 /* We need a PT_GNU_EH_FRAME segment. */
3823 if (elf_stack_flags (abfd))
3825 /* We need a PT_GNU_STACK segment. */
3829 for (s = abfd->sections; s != NULL; s = s->next)
3831 if ((s->flags & SEC_LOAD) != 0
3832 && CONST_STRNEQ (s->name, ".note"))
3834 /* We need a PT_NOTE segment. */
3836 /* Try to create just one PT_NOTE segment
3837 for all adjacent loadable .note* sections.
3838 gABI requires that within a PT_NOTE segment
3839 (and also inside of each SHT_NOTE section)
3840 each note is padded to a multiple of 4 size,
3841 so we check whether the sections are correctly
3843 if (s->alignment_power == 2)
3844 while (s->next != NULL
3845 && s->next->alignment_power == 2
3846 && (s->next->flags & SEC_LOAD) != 0
3847 && CONST_STRNEQ (s->next->name, ".note"))
3852 for (s = abfd->sections; s != NULL; s = s->next)
3854 if (s->flags & SEC_THREAD_LOCAL)
3856 /* We need a PT_TLS segment. */
3862 /* Let the backend count up any program headers it might need. */
3863 bed = get_elf_backend_data (abfd);
3864 if (bed->elf_backend_additional_program_headers)
3868 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3874 return segs * bed->s->sizeof_phdr;
3877 /* Find the segment that contains the output_section of section. */
3880 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3882 struct elf_segment_map *m;
3883 Elf_Internal_Phdr *p;
3885 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3891 for (i = m->count - 1; i >= 0; i--)
3892 if (m->sections[i] == section)
3899 /* Create a mapping from a set of sections to a program segment. */
3901 static struct elf_segment_map *
3902 make_mapping (bfd *abfd,
3903 asection **sections,
3908 struct elf_segment_map *m;
3913 amt = sizeof (struct elf_segment_map);
3914 amt += (to - from - 1) * sizeof (asection *);
3915 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3919 m->p_type = PT_LOAD;
3920 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3921 m->sections[i - from] = *hdrpp;
3922 m->count = to - from;
3924 if (from == 0 && phdr)
3926 /* Include the headers in the first PT_LOAD segment. */
3927 m->includes_filehdr = 1;
3928 m->includes_phdrs = 1;
3934 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3937 struct elf_segment_map *
3938 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3940 struct elf_segment_map *m;
3942 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3943 sizeof (struct elf_segment_map));
3947 m->p_type = PT_DYNAMIC;
3949 m->sections[0] = dynsec;
3954 /* Possibly add or remove segments from the segment map. */
3957 elf_modify_segment_map (bfd *abfd,
3958 struct bfd_link_info *info,
3959 bfd_boolean remove_empty_load)
3961 struct elf_segment_map **m;
3962 const struct elf_backend_data *bed;
3964 /* The placement algorithm assumes that non allocated sections are
3965 not in PT_LOAD segments. We ensure this here by removing such
3966 sections from the segment map. We also remove excluded
3967 sections. Finally, any PT_LOAD segment without sections is
3969 m = &elf_seg_map (abfd);
3972 unsigned int i, new_count;
3974 for (new_count = 0, i = 0; i < (*m)->count; i++)
3976 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3977 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3978 || (*m)->p_type != PT_LOAD))
3980 (*m)->sections[new_count] = (*m)->sections[i];
3984 (*m)->count = new_count;
3986 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3992 bed = get_elf_backend_data (abfd);
3993 if (bed->elf_backend_modify_segment_map != NULL)
3995 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4002 /* Set up a mapping from BFD sections to program segments. */
4005 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4008 struct elf_segment_map *m;
4009 asection **sections = NULL;
4010 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4011 bfd_boolean no_user_phdrs;
4013 no_user_phdrs = elf_seg_map (abfd) == NULL;
4016 info->user_phdrs = !no_user_phdrs;
4018 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4022 struct elf_segment_map *mfirst;
4023 struct elf_segment_map **pm;
4026 unsigned int phdr_index;
4027 bfd_vma maxpagesize;
4029 bfd_boolean phdr_in_segment = TRUE;
4030 bfd_boolean writable;
4032 asection *first_tls = NULL;
4033 asection *dynsec, *eh_frame_hdr;
4035 bfd_vma addr_mask, wrap_to = 0;
4037 /* Select the allocated sections, and sort them. */
4039 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4040 sizeof (asection *));
4041 if (sections == NULL)
4044 /* Calculate top address, avoiding undefined behaviour of shift
4045 left operator when shift count is equal to size of type
4047 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4048 addr_mask = (addr_mask << 1) + 1;
4051 for (s = abfd->sections; s != NULL; s = s->next)
4053 if ((s->flags & SEC_ALLOC) != 0)
4057 /* A wrapping section potentially clashes with header. */
4058 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4059 wrap_to = (s->lma + s->size) & addr_mask;
4062 BFD_ASSERT (i <= bfd_count_sections (abfd));
4065 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4067 /* Build the mapping. */
4072 /* If we have a .interp section, then create a PT_PHDR segment for
4073 the program headers and a PT_INTERP segment for the .interp
4075 s = bfd_get_section_by_name (abfd, ".interp");
4076 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4078 amt = sizeof (struct elf_segment_map);
4079 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4083 m->p_type = PT_PHDR;
4084 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4085 m->p_flags = PF_R | PF_X;
4086 m->p_flags_valid = 1;
4087 m->includes_phdrs = 1;
4092 amt = sizeof (struct elf_segment_map);
4093 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4097 m->p_type = PT_INTERP;
4105 /* Look through the sections. We put sections in the same program
4106 segment when the start of the second section can be placed within
4107 a few bytes of the end of the first section. */
4111 maxpagesize = bed->maxpagesize;
4112 /* PR 17512: file: c8455299.
4113 Avoid divide-by-zero errors later on.
4114 FIXME: Should we abort if the maxpagesize is zero ? */
4115 if (maxpagesize == 0)
4118 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4120 && (dynsec->flags & SEC_LOAD) == 0)
4123 /* Deal with -Ttext or something similar such that the first section
4124 is not adjacent to the program headers. This is an
4125 approximation, since at this point we don't know exactly how many
4126 program headers we will need. */
4129 bfd_size_type phdr_size = elf_program_header_size (abfd);
4131 if (phdr_size == (bfd_size_type) -1)
4132 phdr_size = get_program_header_size (abfd, info);
4133 phdr_size += bed->s->sizeof_ehdr;
4134 if ((abfd->flags & D_PAGED) == 0
4135 || (sections[0]->lma & addr_mask) < phdr_size
4136 || ((sections[0]->lma & addr_mask) % maxpagesize
4137 < phdr_size % maxpagesize)
4138 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4139 phdr_in_segment = FALSE;
4142 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4145 bfd_boolean new_segment;
4149 /* See if this section and the last one will fit in the same
4152 if (last_hdr == NULL)
4154 /* If we don't have a segment yet, then we don't need a new
4155 one (we build the last one after this loop). */
4156 new_segment = FALSE;
4158 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4160 /* If this section has a different relation between the
4161 virtual address and the load address, then we need a new
4165 else if (hdr->lma < last_hdr->lma + last_size
4166 || last_hdr->lma + last_size < last_hdr->lma)
4168 /* If this section has a load address that makes it overlap
4169 the previous section, then we need a new segment. */
4172 /* In the next test we have to be careful when last_hdr->lma is close
4173 to the end of the address space. If the aligned address wraps
4174 around to the start of the address space, then there are no more
4175 pages left in memory and it is OK to assume that the current
4176 section can be included in the current segment. */
4177 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4179 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4182 /* If putting this section in this segment would force us to
4183 skip a page in the segment, then we need a new segment. */
4186 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4187 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4189 /* We don't want to put a loadable section after a
4190 nonloadable section in the same segment.
4191 Consider .tbss sections as loadable for this purpose. */
4194 else if ((abfd->flags & D_PAGED) == 0)
4196 /* If the file is not demand paged, which means that we
4197 don't require the sections to be correctly aligned in the
4198 file, then there is no other reason for a new segment. */
4199 new_segment = FALSE;
4202 && (hdr->flags & SEC_READONLY) == 0
4203 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4204 != (hdr->lma & -maxpagesize)))
4206 /* We don't want to put a writable section in a read only
4207 segment, unless they are on the same page in memory
4208 anyhow. We already know that the last section does not
4209 bring us past the current section on the page, so the
4210 only case in which the new section is not on the same
4211 page as the previous section is when the previous section
4212 ends precisely on a page boundary. */
4217 /* Otherwise, we can use the same segment. */
4218 new_segment = FALSE;
4221 /* Allow interested parties a chance to override our decision. */
4222 if (last_hdr != NULL
4224 && info->callbacks->override_segment_assignment != NULL)
4226 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4232 if ((hdr->flags & SEC_READONLY) == 0)
4235 /* .tbss sections effectively have zero size. */
4236 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4237 != SEC_THREAD_LOCAL)
4238 last_size = hdr->size;
4244 /* We need a new program segment. We must create a new program
4245 header holding all the sections from phdr_index until hdr. */
4247 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4254 if ((hdr->flags & SEC_READONLY) == 0)
4260 /* .tbss sections effectively have zero size. */
4261 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4262 last_size = hdr->size;
4266 phdr_in_segment = FALSE;
4269 /* Create a final PT_LOAD program segment, but not if it's just
4271 if (last_hdr != NULL
4272 && (i - phdr_index != 1
4273 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4274 != SEC_THREAD_LOCAL)))
4276 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4284 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4287 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4294 /* For each batch of consecutive loadable .note sections,
4295 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4296 because if we link together nonloadable .note sections and
4297 loadable .note sections, we will generate two .note sections
4298 in the output file. FIXME: Using names for section types is
4300 for (s = abfd->sections; s != NULL; s = s->next)
4302 if ((s->flags & SEC_LOAD) != 0
4303 && CONST_STRNEQ (s->name, ".note"))
4308 amt = sizeof (struct elf_segment_map);
4309 if (s->alignment_power == 2)
4310 for (s2 = s; s2->next != NULL; s2 = s2->next)
4312 if (s2->next->alignment_power == 2
4313 && (s2->next->flags & SEC_LOAD) != 0
4314 && CONST_STRNEQ (s2->next->name, ".note")
4315 && align_power (s2->lma + s2->size, 2)
4321 amt += (count - 1) * sizeof (asection *);
4322 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4326 m->p_type = PT_NOTE;
4330 m->sections[m->count - count--] = s;
4331 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4334 m->sections[m->count - 1] = s;
4335 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4339 if (s->flags & SEC_THREAD_LOCAL)
4347 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4350 amt = sizeof (struct elf_segment_map);
4351 amt += (tls_count - 1) * sizeof (asection *);
4352 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4357 m->count = tls_count;
4358 /* Mandated PF_R. */
4360 m->p_flags_valid = 1;
4362 for (i = 0; i < (unsigned int) tls_count; ++i)
4364 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4367 (_("%B: TLS sections are not adjacent:"), abfd);
4370 while (i < (unsigned int) tls_count)
4372 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4374 _bfd_error_handler (_(" TLS: %A"), s);
4378 _bfd_error_handler (_(" non-TLS: %A"), s);
4381 bfd_set_error (bfd_error_bad_value);
4392 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4394 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4395 if (eh_frame_hdr != NULL
4396 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4398 amt = sizeof (struct elf_segment_map);
4399 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4403 m->p_type = PT_GNU_EH_FRAME;
4405 m->sections[0] = eh_frame_hdr->output_section;
4411 if (elf_stack_flags (abfd))
4413 amt = sizeof (struct elf_segment_map);
4414 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4418 m->p_type = PT_GNU_STACK;
4419 m->p_flags = elf_stack_flags (abfd);
4420 m->p_align = bed->stack_align;
4421 m->p_flags_valid = 1;
4422 m->p_align_valid = m->p_align != 0;
4423 if (info->stacksize > 0)
4425 m->p_size = info->stacksize;
4426 m->p_size_valid = 1;
4433 if (info != NULL && info->relro)
4435 for (m = mfirst; m != NULL; m = m->next)
4437 if (m->p_type == PT_LOAD
4439 && m->sections[0]->vma >= info->relro_start
4440 && m->sections[0]->vma < info->relro_end)
4443 while (--i != (unsigned) -1)
4444 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4445 == (SEC_LOAD | SEC_HAS_CONTENTS))
4448 if (i != (unsigned) -1)
4453 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4456 amt = sizeof (struct elf_segment_map);
4457 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4461 m->p_type = PT_GNU_RELRO;
4463 m->p_flags_valid = 1;
4471 elf_seg_map (abfd) = mfirst;
4474 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4477 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4479 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4484 if (sections != NULL)
4489 /* Sort sections by address. */
4492 elf_sort_sections (const void *arg1, const void *arg2)
4494 const asection *sec1 = *(const asection **) arg1;
4495 const asection *sec2 = *(const asection **) arg2;
4496 bfd_size_type size1, size2;
4498 /* Sort by LMA first, since this is the address used to
4499 place the section into a segment. */
4500 if (sec1->lma < sec2->lma)
4502 else if (sec1->lma > sec2->lma)
4505 /* Then sort by VMA. Normally the LMA and the VMA will be
4506 the same, and this will do nothing. */
4507 if (sec1->vma < sec2->vma)
4509 else if (sec1->vma > sec2->vma)
4512 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4514 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4520 /* If the indicies are the same, do not return 0
4521 here, but continue to try the next comparison. */
4522 if (sec1->target_index - sec2->target_index != 0)
4523 return sec1->target_index - sec2->target_index;
4528 else if (TOEND (sec2))
4533 /* Sort by size, to put zero sized sections
4534 before others at the same address. */
4536 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4537 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4544 return sec1->target_index - sec2->target_index;
4547 /* Ian Lance Taylor writes:
4549 We shouldn't be using % with a negative signed number. That's just
4550 not good. We have to make sure either that the number is not
4551 negative, or that the number has an unsigned type. When the types
4552 are all the same size they wind up as unsigned. When file_ptr is a
4553 larger signed type, the arithmetic winds up as signed long long,
4556 What we're trying to say here is something like ``increase OFF by
4557 the least amount that will cause it to be equal to the VMA modulo
4559 /* In other words, something like:
4561 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4562 off_offset = off % bed->maxpagesize;
4563 if (vma_offset < off_offset)
4564 adjustment = vma_offset + bed->maxpagesize - off_offset;
4566 adjustment = vma_offset - off_offset;
4568 which can can be collapsed into the expression below. */
4571 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4573 /* PR binutils/16199: Handle an alignment of zero. */
4574 if (maxpagesize == 0)
4576 return ((vma - off) % maxpagesize);
4580 print_segment_map (const struct elf_segment_map *m)
4583 const char *pt = get_segment_type (m->p_type);
4588 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4589 sprintf (buf, "LOPROC+%7.7x",
4590 (unsigned int) (m->p_type - PT_LOPROC));
4591 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4592 sprintf (buf, "LOOS+%7.7x",
4593 (unsigned int) (m->p_type - PT_LOOS));
4595 snprintf (buf, sizeof (buf), "%8.8x",
4596 (unsigned int) m->p_type);
4600 fprintf (stderr, "%s:", pt);
4601 for (j = 0; j < m->count; j++)
4602 fprintf (stderr, " %s", m->sections [j]->name);
4608 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4613 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4615 buf = bfd_zmalloc (len);
4618 ret = bfd_bwrite (buf, len, abfd) == len;
4623 /* Assign file positions to the sections based on the mapping from
4624 sections to segments. This function also sets up some fields in
4628 assign_file_positions_for_load_sections (bfd *abfd,
4629 struct bfd_link_info *link_info)
4631 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4632 struct elf_segment_map *m;
4633 Elf_Internal_Phdr *phdrs;
4634 Elf_Internal_Phdr *p;
4636 bfd_size_type maxpagesize;
4639 bfd_vma header_pad = 0;
4641 if (link_info == NULL
4642 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4646 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4650 header_pad = m->header_size;
4655 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4656 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4660 /* PR binutils/12467. */
4661 elf_elfheader (abfd)->e_phoff = 0;
4662 elf_elfheader (abfd)->e_phentsize = 0;
4665 elf_elfheader (abfd)->e_phnum = alloc;
4667 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4668 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4670 BFD_ASSERT (elf_program_header_size (abfd)
4671 >= alloc * bed->s->sizeof_phdr);
4675 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4679 /* We're writing the size in elf_program_header_size (abfd),
4680 see assign_file_positions_except_relocs, so make sure we have
4681 that amount allocated, with trailing space cleared.
4682 The variable alloc contains the computed need, while
4683 elf_program_header_size (abfd) contains the size used for the
4685 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4686 where the layout is forced to according to a larger size in the
4687 last iterations for the testcase ld-elf/header. */
4688 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4690 phdrs = (Elf_Internal_Phdr *)
4692 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4693 sizeof (Elf_Internal_Phdr));
4694 elf_tdata (abfd)->phdr = phdrs;
4699 if ((abfd->flags & D_PAGED) != 0)
4700 maxpagesize = bed->maxpagesize;
4702 off = bed->s->sizeof_ehdr;
4703 off += alloc * bed->s->sizeof_phdr;
4704 if (header_pad < (bfd_vma) off)
4710 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4712 m = m->next, p++, j++)
4716 bfd_boolean no_contents;
4718 /* If elf_segment_map is not from map_sections_to_segments, the
4719 sections may not be correctly ordered. NOTE: sorting should
4720 not be done to the PT_NOTE section of a corefile, which may
4721 contain several pseudo-sections artificially created by bfd.
4722 Sorting these pseudo-sections breaks things badly. */
4724 && !(elf_elfheader (abfd)->e_type == ET_CORE
4725 && m->p_type == PT_NOTE))
4726 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4729 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4730 number of sections with contents contributing to both p_filesz
4731 and p_memsz, followed by a number of sections with no contents
4732 that just contribute to p_memsz. In this loop, OFF tracks next
4733 available file offset for PT_LOAD and PT_NOTE segments. */
4734 p->p_type = m->p_type;
4735 p->p_flags = m->p_flags;
4740 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4742 if (m->p_paddr_valid)
4743 p->p_paddr = m->p_paddr;
4744 else if (m->count == 0)
4747 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4749 if (p->p_type == PT_LOAD
4750 && (abfd->flags & D_PAGED) != 0)
4752 /* p_align in demand paged PT_LOAD segments effectively stores
4753 the maximum page size. When copying an executable with
4754 objcopy, we set m->p_align from the input file. Use this
4755 value for maxpagesize rather than bed->maxpagesize, which
4756 may be different. Note that we use maxpagesize for PT_TLS
4757 segment alignment later in this function, so we are relying
4758 on at least one PT_LOAD segment appearing before a PT_TLS
4760 if (m->p_align_valid)
4761 maxpagesize = m->p_align;
4763 p->p_align = maxpagesize;
4765 else if (m->p_align_valid)
4766 p->p_align = m->p_align;
4767 else if (m->count == 0)
4768 p->p_align = 1 << bed->s->log_file_align;
4772 no_contents = FALSE;
4774 if (p->p_type == PT_LOAD
4777 bfd_size_type align;
4778 unsigned int align_power = 0;
4780 if (m->p_align_valid)
4784 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4786 unsigned int secalign;
4788 secalign = bfd_get_section_alignment (abfd, *secpp);
4789 if (secalign > align_power)
4790 align_power = secalign;
4792 align = (bfd_size_type) 1 << align_power;
4793 if (align < maxpagesize)
4794 align = maxpagesize;
4797 for (i = 0; i < m->count; i++)
4798 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4799 /* If we aren't making room for this section, then
4800 it must be SHT_NOBITS regardless of what we've
4801 set via struct bfd_elf_special_section. */
4802 elf_section_type (m->sections[i]) = SHT_NOBITS;
4804 /* Find out whether this segment contains any loadable
4807 for (i = 0; i < m->count; i++)
4808 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4810 no_contents = FALSE;
4814 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4818 /* We shouldn't need to align the segment on disk since
4819 the segment doesn't need file space, but the gABI
4820 arguably requires the alignment and glibc ld.so
4821 checks it. So to comply with the alignment
4822 requirement but not waste file space, we adjust
4823 p_offset for just this segment. (OFF_ADJUST is
4824 subtracted from OFF later.) This may put p_offset
4825 past the end of file, but that shouldn't matter. */
4830 /* Make sure the .dynamic section is the first section in the
4831 PT_DYNAMIC segment. */
4832 else if (p->p_type == PT_DYNAMIC
4834 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4837 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4839 bfd_set_error (bfd_error_bad_value);
4842 /* Set the note section type to SHT_NOTE. */
4843 else if (p->p_type == PT_NOTE)
4844 for (i = 0; i < m->count; i++)
4845 elf_section_type (m->sections[i]) = SHT_NOTE;
4851 if (m->includes_filehdr)
4853 if (!m->p_flags_valid)
4855 p->p_filesz = bed->s->sizeof_ehdr;
4856 p->p_memsz = bed->s->sizeof_ehdr;
4859 if (p->p_vaddr < (bfd_vma) off)
4861 (*_bfd_error_handler)
4862 (_("%B: Not enough room for program headers, try linking with -N"),
4864 bfd_set_error (bfd_error_bad_value);
4869 if (!m->p_paddr_valid)
4874 if (m->includes_phdrs)
4876 if (!m->p_flags_valid)
4879 if (!m->includes_filehdr)
4881 p->p_offset = bed->s->sizeof_ehdr;
4885 p->p_vaddr -= off - p->p_offset;
4886 if (!m->p_paddr_valid)
4887 p->p_paddr -= off - p->p_offset;
4891 p->p_filesz += alloc * bed->s->sizeof_phdr;
4892 p->p_memsz += alloc * bed->s->sizeof_phdr;
4895 p->p_filesz += header_pad;
4896 p->p_memsz += header_pad;
4900 if (p->p_type == PT_LOAD
4901 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4903 if (!m->includes_filehdr && !m->includes_phdrs)
4909 adjust = off - (p->p_offset + p->p_filesz);
4911 p->p_filesz += adjust;
4912 p->p_memsz += adjust;
4916 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4917 maps. Set filepos for sections in PT_LOAD segments, and in
4918 core files, for sections in PT_NOTE segments.
4919 assign_file_positions_for_non_load_sections will set filepos
4920 for other sections and update p_filesz for other segments. */
4921 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4924 bfd_size_type align;
4925 Elf_Internal_Shdr *this_hdr;
4928 this_hdr = &elf_section_data (sec)->this_hdr;
4929 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4931 if ((p->p_type == PT_LOAD
4932 || p->p_type == PT_TLS)
4933 && (this_hdr->sh_type != SHT_NOBITS
4934 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4935 && ((this_hdr->sh_flags & SHF_TLS) == 0
4936 || p->p_type == PT_TLS))))
4938 bfd_vma p_start = p->p_paddr;
4939 bfd_vma p_end = p_start + p->p_memsz;
4940 bfd_vma s_start = sec->lma;
4941 bfd_vma adjust = s_start - p_end;
4945 || p_end < p_start))
4947 (*_bfd_error_handler)
4948 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4949 (unsigned long) s_start, (unsigned long) p_end);
4953 p->p_memsz += adjust;
4955 if (this_hdr->sh_type != SHT_NOBITS)
4957 if (p->p_filesz + adjust < p->p_memsz)
4959 /* We have a PROGBITS section following NOBITS ones.
4960 Allocate file space for the NOBITS section(s) and
4962 adjust = p->p_memsz - p->p_filesz;
4963 if (!write_zeros (abfd, off, adjust))
4967 p->p_filesz += adjust;
4971 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4973 /* The section at i == 0 is the one that actually contains
4977 this_hdr->sh_offset = sec->filepos = off;
4978 off += this_hdr->sh_size;
4979 p->p_filesz = this_hdr->sh_size;
4985 /* The rest are fake sections that shouldn't be written. */
4994 if (p->p_type == PT_LOAD)
4996 this_hdr->sh_offset = sec->filepos = off;
4997 if (this_hdr->sh_type != SHT_NOBITS)
4998 off += this_hdr->sh_size;
5000 else if (this_hdr->sh_type == SHT_NOBITS
5001 && (this_hdr->sh_flags & SHF_TLS) != 0
5002 && this_hdr->sh_offset == 0)
5004 /* This is a .tbss section that didn't get a PT_LOAD.
5005 (See _bfd_elf_map_sections_to_segments "Create a
5006 final PT_LOAD".) Set sh_offset to the value it
5007 would have if we had created a zero p_filesz and
5008 p_memsz PT_LOAD header for the section. This
5009 also makes the PT_TLS header have the same
5011 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5013 this_hdr->sh_offset = sec->filepos = off + adjust;
5016 if (this_hdr->sh_type != SHT_NOBITS)
5018 p->p_filesz += this_hdr->sh_size;
5019 /* A load section without SHF_ALLOC is something like
5020 a note section in a PT_NOTE segment. These take
5021 file space but are not loaded into memory. */
5022 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5023 p->p_memsz += this_hdr->sh_size;
5025 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5027 if (p->p_type == PT_TLS)
5028 p->p_memsz += this_hdr->sh_size;
5030 /* .tbss is special. It doesn't contribute to p_memsz of
5032 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5033 p->p_memsz += this_hdr->sh_size;
5036 if (align > p->p_align
5037 && !m->p_align_valid
5038 && (p->p_type != PT_LOAD
5039 || (abfd->flags & D_PAGED) == 0))
5043 if (!m->p_flags_valid)
5046 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5048 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5055 /* Check that all sections are in a PT_LOAD segment.
5056 Don't check funky gdb generated core files. */
5057 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5059 bfd_boolean check_vma = TRUE;
5061 for (i = 1; i < m->count; i++)
5062 if (m->sections[i]->vma == m->sections[i - 1]->vma
5063 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5064 ->this_hdr), p) != 0
5065 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5066 ->this_hdr), p) != 0)
5068 /* Looks like we have overlays packed into the segment. */
5073 for (i = 0; i < m->count; i++)
5075 Elf_Internal_Shdr *this_hdr;
5078 sec = m->sections[i];
5079 this_hdr = &(elf_section_data(sec)->this_hdr);
5080 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5081 && !ELF_TBSS_SPECIAL (this_hdr, p))
5083 (*_bfd_error_handler)
5084 (_("%B: section `%A' can't be allocated in segment %d"),
5086 print_segment_map (m);
5092 elf_next_file_pos (abfd) = off;
5096 /* Assign file positions for the other sections. */
5099 assign_file_positions_for_non_load_sections (bfd *abfd,
5100 struct bfd_link_info *link_info)
5102 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5103 Elf_Internal_Shdr **i_shdrpp;
5104 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5105 Elf_Internal_Phdr *phdrs;
5106 Elf_Internal_Phdr *p;
5107 struct elf_segment_map *m;
5108 struct elf_segment_map *hdrs_segment;
5109 bfd_vma filehdr_vaddr, filehdr_paddr;
5110 bfd_vma phdrs_vaddr, phdrs_paddr;
5114 i_shdrpp = elf_elfsections (abfd);
5115 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5116 off = elf_next_file_pos (abfd);
5117 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5119 Elf_Internal_Shdr *hdr;
5122 if (hdr->bfd_section != NULL
5123 && (hdr->bfd_section->filepos != 0
5124 || (hdr->sh_type == SHT_NOBITS
5125 && hdr->contents == NULL)))
5126 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5127 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5129 if (hdr->sh_size != 0)
5130 (*_bfd_error_handler)
5131 (_("%B: warning: allocated section `%s' not in segment"),
5133 (hdr->bfd_section == NULL
5135 : hdr->bfd_section->name));
5136 /* We don't need to page align empty sections. */
5137 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5138 off += vma_page_aligned_bias (hdr->sh_addr, off,
5141 off += vma_page_aligned_bias (hdr->sh_addr, off,
5143 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5146 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5147 && hdr->bfd_section == NULL)
5148 || (hdr->bfd_section != NULL
5149 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5150 /* Compress DWARF debug sections. */
5151 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5152 || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
5153 || hdr == i_shdrpp[elf_strtab_sec (abfd)])
5154 hdr->sh_offset = -1;
5156 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5159 /* Now that we have set the section file positions, we can set up
5160 the file positions for the non PT_LOAD segments. */
5164 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5166 hdrs_segment = NULL;
5167 phdrs = elf_tdata (abfd)->phdr;
5168 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5171 if (p->p_type != PT_LOAD)
5174 if (m->includes_filehdr)
5176 filehdr_vaddr = p->p_vaddr;
5177 filehdr_paddr = p->p_paddr;
5179 if (m->includes_phdrs)
5181 phdrs_vaddr = p->p_vaddr;
5182 phdrs_paddr = p->p_paddr;
5183 if (m->includes_filehdr)
5186 phdrs_vaddr += bed->s->sizeof_ehdr;
5187 phdrs_paddr += bed->s->sizeof_ehdr;
5192 if (hdrs_segment != NULL && link_info != NULL)
5194 /* There is a segment that contains both the file headers and the
5195 program headers, so provide a symbol __ehdr_start pointing there.
5196 A program can use this to examine itself robustly. */
5198 struct elf_link_hash_entry *hash
5199 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5200 FALSE, FALSE, TRUE);
5201 /* If the symbol was referenced and not defined, define it. */
5203 && (hash->root.type == bfd_link_hash_new
5204 || hash->root.type == bfd_link_hash_undefined
5205 || hash->root.type == bfd_link_hash_undefweak
5206 || hash->root.type == bfd_link_hash_common))
5209 if (hdrs_segment->count != 0)
5210 /* The segment contains sections, so use the first one. */
5211 s = hdrs_segment->sections[0];
5213 /* Use the first (i.e. lowest-addressed) section in any segment. */
5214 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5223 hash->root.u.def.value = filehdr_vaddr - s->vma;
5224 hash->root.u.def.section = s;
5228 hash->root.u.def.value = filehdr_vaddr;
5229 hash->root.u.def.section = bfd_abs_section_ptr;
5232 hash->root.type = bfd_link_hash_defined;
5233 hash->def_regular = 1;
5238 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5240 if (p->p_type == PT_GNU_RELRO)
5242 const Elf_Internal_Phdr *lp;
5243 struct elf_segment_map *lm;
5245 if (link_info != NULL)
5247 /* During linking the range of the RELRO segment is passed
5249 for (lm = elf_seg_map (abfd), lp = phdrs;
5251 lm = lm->next, lp++)
5253 if (lp->p_type == PT_LOAD
5254 && lp->p_vaddr < link_info->relro_end
5256 && lm->sections[0]->vma >= link_info->relro_start)
5260 BFD_ASSERT (lm != NULL);
5264 /* Otherwise we are copying an executable or shared
5265 library, but we need to use the same linker logic. */
5266 for (lp = phdrs; lp < phdrs + count; ++lp)
5268 if (lp->p_type == PT_LOAD
5269 && lp->p_paddr == p->p_paddr)
5274 if (lp < phdrs + count)
5276 p->p_vaddr = lp->p_vaddr;
5277 p->p_paddr = lp->p_paddr;
5278 p->p_offset = lp->p_offset;
5279 if (link_info != NULL)
5280 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5281 else if (m->p_size_valid)
5282 p->p_filesz = m->p_size;
5285 p->p_memsz = p->p_filesz;
5286 /* Preserve the alignment and flags if they are valid. The
5287 gold linker generates RW/4 for the PT_GNU_RELRO section.
5288 It is better for objcopy/strip to honor these attributes
5289 otherwise gdb will choke when using separate debug files.
5291 if (!m->p_align_valid)
5293 if (!m->p_flags_valid)
5294 p->p_flags = (lp->p_flags & ~PF_W);
5298 memset (p, 0, sizeof *p);
5299 p->p_type = PT_NULL;
5302 else if (p->p_type == PT_GNU_STACK)
5304 if (m->p_size_valid)
5305 p->p_memsz = m->p_size;
5307 else if (m->count != 0)
5310 if (p->p_type != PT_LOAD
5311 && (p->p_type != PT_NOTE
5312 || bfd_get_format (abfd) != bfd_core))
5314 if (m->includes_filehdr || m->includes_phdrs)
5316 /* PR 17512: file: 2195325e. */
5317 (*_bfd_error_handler)
5318 (_("%B: warning: non-load segment includes file header and/or program header"),
5324 p->p_offset = m->sections[0]->filepos;
5325 for (i = m->count; i-- != 0;)
5327 asection *sect = m->sections[i];
5328 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5329 if (hdr->sh_type != SHT_NOBITS)
5331 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5338 else if (m->includes_filehdr)
5340 p->p_vaddr = filehdr_vaddr;
5341 if (! m->p_paddr_valid)
5342 p->p_paddr = filehdr_paddr;
5344 else if (m->includes_phdrs)
5346 p->p_vaddr = phdrs_vaddr;
5347 if (! m->p_paddr_valid)
5348 p->p_paddr = phdrs_paddr;
5352 elf_next_file_pos (abfd) = off;
5357 /* Work out the file positions of all the sections. This is called by
5358 _bfd_elf_compute_section_file_positions. All the section sizes and
5359 VMAs must be known before this is called.
5361 Reloc sections come in two flavours: Those processed specially as
5362 "side-channel" data attached to a section to which they apply, and
5363 those that bfd doesn't process as relocations. The latter sort are
5364 stored in a normal bfd section by bfd_section_from_shdr. We don't
5365 consider the former sort here, unless they form part of the loadable
5366 image. Reloc sections not assigned here will be handled later by
5367 assign_file_positions_for_relocs.
5369 We also don't set the positions of the .symtab and .strtab here. */
5372 assign_file_positions_except_relocs (bfd *abfd,
5373 struct bfd_link_info *link_info)
5375 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5376 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5379 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5380 && bfd_get_format (abfd) != bfd_core)
5382 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5383 unsigned int num_sec = elf_numsections (abfd);
5384 Elf_Internal_Shdr **hdrpp;
5388 /* Start after the ELF header. */
5389 off = i_ehdrp->e_ehsize;
5391 /* We are not creating an executable, which means that we are
5392 not creating a program header, and that the actual order of
5393 the sections in the file is unimportant. */
5394 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5396 Elf_Internal_Shdr *hdr;
5399 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5400 && hdr->bfd_section == NULL)
5401 || (hdr->bfd_section != NULL
5402 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5403 /* Compress DWARF debug sections. */
5404 || i == elf_onesymtab (abfd)
5405 || i == elf_symtab_shndx (abfd)
5406 || i == elf_strtab_sec (abfd))
5408 hdr->sh_offset = -1;
5411 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5414 elf_next_file_pos (abfd) = off;
5420 /* Assign file positions for the loaded sections based on the
5421 assignment of sections to segments. */
5422 if (!assign_file_positions_for_load_sections (abfd, link_info))
5425 /* And for non-load sections. */
5426 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5429 if (bed->elf_backend_modify_program_headers != NULL)
5431 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5435 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5436 if (link_info != NULL
5437 && link_info->executable
5438 && link_info->shared)
5440 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5441 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5442 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5444 /* Find the lowest p_vaddr in PT_LOAD segments. */
5445 bfd_vma p_vaddr = (bfd_vma) -1;
5446 for (; segment < end_segment; segment++)
5447 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5448 p_vaddr = segment->p_vaddr;
5450 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5451 segments is non-zero. */
5453 i_ehdrp->e_type = ET_EXEC;
5456 /* Write out the program headers. */
5457 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5458 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5459 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5467 prep_headers (bfd *abfd)
5469 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5470 struct elf_strtab_hash *shstrtab;
5471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5473 i_ehdrp = elf_elfheader (abfd);
5475 shstrtab = _bfd_elf_strtab_init ();
5476 if (shstrtab == NULL)
5479 elf_shstrtab (abfd) = shstrtab;
5481 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5482 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5483 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5484 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5486 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5487 i_ehdrp->e_ident[EI_DATA] =
5488 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5489 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5491 if ((abfd->flags & DYNAMIC) != 0)
5492 i_ehdrp->e_type = ET_DYN;
5493 else if ((abfd->flags & EXEC_P) != 0)
5494 i_ehdrp->e_type = ET_EXEC;
5495 else if (bfd_get_format (abfd) == bfd_core)
5496 i_ehdrp->e_type = ET_CORE;
5498 i_ehdrp->e_type = ET_REL;
5500 switch (bfd_get_arch (abfd))
5502 case bfd_arch_unknown:
5503 i_ehdrp->e_machine = EM_NONE;
5506 /* There used to be a long list of cases here, each one setting
5507 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5508 in the corresponding bfd definition. To avoid duplication,
5509 the switch was removed. Machines that need special handling
5510 can generally do it in elf_backend_final_write_processing(),
5511 unless they need the information earlier than the final write.
5512 Such need can generally be supplied by replacing the tests for
5513 e_machine with the conditions used to determine it. */
5515 i_ehdrp->e_machine = bed->elf_machine_code;
5518 i_ehdrp->e_version = bed->s->ev_current;
5519 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5521 /* No program header, for now. */
5522 i_ehdrp->e_phoff = 0;
5523 i_ehdrp->e_phentsize = 0;
5524 i_ehdrp->e_phnum = 0;
5526 /* Each bfd section is section header entry. */
5527 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5528 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5530 /* If we're building an executable, we'll need a program header table. */
5531 if (abfd->flags & EXEC_P)
5532 /* It all happens later. */
5536 i_ehdrp->e_phentsize = 0;
5537 i_ehdrp->e_phoff = 0;
5540 elf_tdata (abfd)->symtab_hdr.sh_name =
5541 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5542 elf_tdata (abfd)->strtab_hdr.sh_name =
5543 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5544 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5545 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5546 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5547 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5548 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5554 /* Assign file positions for all the reloc sections which are not part
5555 of the loadable file image, and the file position of section headers. */
5558 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
5561 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
5562 Elf_Internal_Ehdr *i_ehdrp;
5563 const struct elf_backend_data *bed;
5565 off = elf_next_file_pos (abfd);
5567 shdrpp = elf_elfsections (abfd);
5568 end_shdrpp = shdrpp + elf_numsections (abfd);
5569 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
5571 Elf_Internal_Shdr *shdrp;
5574 if (shdrp->sh_offset == -1)
5576 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
5577 || shdrp->sh_type == SHT_RELA);
5579 || (shdrp->bfd_section != NULL
5580 && (shdrp->bfd_section->flags & SEC_ELF_COMPRESS)))
5584 /* Compress DWARF debug sections. */
5585 if (!bfd_compress_section (abfd, shdrp->bfd_section,
5588 /* Update section size and contents. */
5589 shdrp->sh_size = shdrp->bfd_section->size;
5590 shdrp->contents = shdrp->bfd_section->contents;
5591 shdrp->bfd_section->contents = NULL;
5593 off = _bfd_elf_assign_file_position_for_section (shdrp,
5600 /* Place the section headers. */
5601 i_ehdrp = elf_elfheader (abfd);
5602 bed = get_elf_backend_data (abfd);
5603 off = align_file_position (off, 1 << bed->s->log_file_align);
5604 i_ehdrp->e_shoff = off;
5605 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5606 elf_next_file_pos (abfd) = off;
5612 _bfd_elf_write_object_contents (bfd *abfd)
5614 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5615 Elf_Internal_Shdr **i_shdrp;
5617 unsigned int count, num_sec;
5618 struct elf_obj_tdata *t;
5620 if (! abfd->output_has_begun
5621 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5624 i_shdrp = elf_elfsections (abfd);
5627 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5631 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
5634 /* After writing the headers, we need to write the sections too... */
5635 num_sec = elf_numsections (abfd);
5636 for (count = 1; count < num_sec; count++)
5638 if (bed->elf_backend_section_processing)
5639 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5640 if (i_shdrp[count]->contents)
5642 bfd_size_type amt = i_shdrp[count]->sh_size;
5644 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5645 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5650 /* Write out the section header names. */
5651 t = elf_tdata (abfd);
5652 if (elf_shstrtab (abfd) != NULL
5653 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5654 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5657 if (bed->elf_backend_final_write_processing)
5658 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5660 if (!bed->s->write_shdrs_and_ehdr (abfd))
5663 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5664 if (t->o->build_id.after_write_object_contents != NULL)
5665 return (*t->o->build_id.after_write_object_contents) (abfd);
5671 _bfd_elf_write_corefile_contents (bfd *abfd)
5673 /* Hopefully this can be done just like an object file. */
5674 return _bfd_elf_write_object_contents (abfd);
5677 /* Given a section, search the header to find them. */
5680 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5682 const struct elf_backend_data *bed;
5683 unsigned int sec_index;
5685 if (elf_section_data (asect) != NULL
5686 && elf_section_data (asect)->this_idx != 0)
5687 return elf_section_data (asect)->this_idx;
5689 if (bfd_is_abs_section (asect))
5690 sec_index = SHN_ABS;
5691 else if (bfd_is_com_section (asect))
5692 sec_index = SHN_COMMON;
5693 else if (bfd_is_und_section (asect))
5694 sec_index = SHN_UNDEF;
5696 sec_index = SHN_BAD;
5698 bed = get_elf_backend_data (abfd);
5699 if (bed->elf_backend_section_from_bfd_section)
5701 int retval = sec_index;
5703 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5707 if (sec_index == SHN_BAD)
5708 bfd_set_error (bfd_error_nonrepresentable_section);
5713 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5717 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5719 asymbol *asym_ptr = *asym_ptr_ptr;
5721 flagword flags = asym_ptr->flags;
5723 /* When gas creates relocations against local labels, it creates its
5724 own symbol for the section, but does put the symbol into the
5725 symbol chain, so udata is 0. When the linker is generating
5726 relocatable output, this section symbol may be for one of the
5727 input sections rather than the output section. */
5728 if (asym_ptr->udata.i == 0
5729 && (flags & BSF_SECTION_SYM)
5730 && asym_ptr->section)
5735 sec = asym_ptr->section;
5736 if (sec->owner != abfd && sec->output_section != NULL)
5737 sec = sec->output_section;
5738 if (sec->owner == abfd
5739 && (indx = sec->index) < elf_num_section_syms (abfd)
5740 && elf_section_syms (abfd)[indx] != NULL)
5741 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5744 idx = asym_ptr->udata.i;
5748 /* This case can occur when using --strip-symbol on a symbol
5749 which is used in a relocation entry. */
5750 (*_bfd_error_handler)
5751 (_("%B: symbol `%s' required but not present"),
5752 abfd, bfd_asymbol_name (asym_ptr));
5753 bfd_set_error (bfd_error_no_symbols);
5760 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5761 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5769 /* Rewrite program header information. */
5772 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5774 Elf_Internal_Ehdr *iehdr;
5775 struct elf_segment_map *map;
5776 struct elf_segment_map *map_first;
5777 struct elf_segment_map **pointer_to_map;
5778 Elf_Internal_Phdr *segment;
5781 unsigned int num_segments;
5782 bfd_boolean phdr_included = FALSE;
5783 bfd_boolean p_paddr_valid;
5784 bfd_vma maxpagesize;
5785 struct elf_segment_map *phdr_adjust_seg = NULL;
5786 unsigned int phdr_adjust_num = 0;
5787 const struct elf_backend_data *bed;
5789 bed = get_elf_backend_data (ibfd);
5790 iehdr = elf_elfheader (ibfd);
5793 pointer_to_map = &map_first;
5795 num_segments = elf_elfheader (ibfd)->e_phnum;
5796 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5798 /* Returns the end address of the segment + 1. */
5799 #define SEGMENT_END(segment, start) \
5800 (start + (segment->p_memsz > segment->p_filesz \
5801 ? segment->p_memsz : segment->p_filesz))
5803 #define SECTION_SIZE(section, segment) \
5804 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5805 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5806 ? section->size : 0)
5808 /* Returns TRUE if the given section is contained within
5809 the given segment. VMA addresses are compared. */
5810 #define IS_CONTAINED_BY_VMA(section, segment) \
5811 (section->vma >= segment->p_vaddr \
5812 && (section->vma + SECTION_SIZE (section, segment) \
5813 <= (SEGMENT_END (segment, segment->p_vaddr))))
5815 /* Returns TRUE if the given section is contained within
5816 the given segment. LMA addresses are compared. */
5817 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5818 (section->lma >= base \
5819 && (section->lma + SECTION_SIZE (section, segment) \
5820 <= SEGMENT_END (segment, base)))
5822 /* Handle PT_NOTE segment. */
5823 #define IS_NOTE(p, s) \
5824 (p->p_type == PT_NOTE \
5825 && elf_section_type (s) == SHT_NOTE \
5826 && (bfd_vma) s->filepos >= p->p_offset \
5827 && ((bfd_vma) s->filepos + s->size \
5828 <= p->p_offset + p->p_filesz))
5830 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5832 #define IS_COREFILE_NOTE(p, s) \
5834 && bfd_get_format (ibfd) == bfd_core \
5838 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5839 linker, which generates a PT_INTERP section with p_vaddr and
5840 p_memsz set to 0. */
5841 #define IS_SOLARIS_PT_INTERP(p, s) \
5843 && p->p_paddr == 0 \
5844 && p->p_memsz == 0 \
5845 && p->p_filesz > 0 \
5846 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5848 && (bfd_vma) s->filepos >= p->p_offset \
5849 && ((bfd_vma) s->filepos + s->size \
5850 <= p->p_offset + p->p_filesz))
5852 /* Decide if the given section should be included in the given segment.
5853 A section will be included if:
5854 1. It is within the address space of the segment -- we use the LMA
5855 if that is set for the segment and the VMA otherwise,
5856 2. It is an allocated section or a NOTE section in a PT_NOTE
5858 3. There is an output section associated with it,
5859 4. The section has not already been allocated to a previous segment.
5860 5. PT_GNU_STACK segments do not include any sections.
5861 6. PT_TLS segment includes only SHF_TLS sections.
5862 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5863 8. PT_DYNAMIC should not contain empty sections at the beginning
5864 (with the possible exception of .dynamic). */
5865 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5866 ((((segment->p_paddr \
5867 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5868 : IS_CONTAINED_BY_VMA (section, segment)) \
5869 && (section->flags & SEC_ALLOC) != 0) \
5870 || IS_NOTE (segment, section)) \
5871 && segment->p_type != PT_GNU_STACK \
5872 && (segment->p_type != PT_TLS \
5873 || (section->flags & SEC_THREAD_LOCAL)) \
5874 && (segment->p_type == PT_LOAD \
5875 || segment->p_type == PT_TLS \
5876 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5877 && (segment->p_type != PT_DYNAMIC \
5878 || SECTION_SIZE (section, segment) > 0 \
5879 || (segment->p_paddr \
5880 ? segment->p_paddr != section->lma \
5881 : segment->p_vaddr != section->vma) \
5882 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5884 && !section->segment_mark)
5886 /* If the output section of a section in the input segment is NULL,
5887 it is removed from the corresponding output segment. */
5888 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5889 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5890 && section->output_section != NULL)
5892 /* Returns TRUE iff seg1 starts after the end of seg2. */
5893 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5894 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5896 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5897 their VMA address ranges and their LMA address ranges overlap.
5898 It is possible to have overlapping VMA ranges without overlapping LMA
5899 ranges. RedBoot images for example can have both .data and .bss mapped
5900 to the same VMA range, but with the .data section mapped to a different
5902 #define SEGMENT_OVERLAPS(seg1, seg2) \
5903 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5904 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5905 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5906 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5908 /* Initialise the segment mark field. */
5909 for (section = ibfd->sections; section != NULL; section = section->next)
5910 section->segment_mark = FALSE;
5912 /* The Solaris linker creates program headers in which all the
5913 p_paddr fields are zero. When we try to objcopy or strip such a
5914 file, we get confused. Check for this case, and if we find it
5915 don't set the p_paddr_valid fields. */
5916 p_paddr_valid = FALSE;
5917 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5920 if (segment->p_paddr != 0)
5922 p_paddr_valid = TRUE;
5926 /* Scan through the segments specified in the program header
5927 of the input BFD. For this first scan we look for overlaps
5928 in the loadable segments. These can be created by weird
5929 parameters to objcopy. Also, fix some solaris weirdness. */
5930 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5935 Elf_Internal_Phdr *segment2;
5937 if (segment->p_type == PT_INTERP)
5938 for (section = ibfd->sections; section; section = section->next)
5939 if (IS_SOLARIS_PT_INTERP (segment, section))
5941 /* Mininal change so that the normal section to segment
5942 assignment code will work. */
5943 segment->p_vaddr = section->vma;
5947 if (segment->p_type != PT_LOAD)
5949 /* Remove PT_GNU_RELRO segment. */
5950 if (segment->p_type == PT_GNU_RELRO)
5951 segment->p_type = PT_NULL;
5955 /* Determine if this segment overlaps any previous segments. */
5956 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5958 bfd_signed_vma extra_length;
5960 if (segment2->p_type != PT_LOAD
5961 || !SEGMENT_OVERLAPS (segment, segment2))
5964 /* Merge the two segments together. */
5965 if (segment2->p_vaddr < segment->p_vaddr)
5967 /* Extend SEGMENT2 to include SEGMENT and then delete
5969 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5970 - SEGMENT_END (segment2, segment2->p_vaddr));
5972 if (extra_length > 0)
5974 segment2->p_memsz += extra_length;
5975 segment2->p_filesz += extra_length;
5978 segment->p_type = PT_NULL;
5980 /* Since we have deleted P we must restart the outer loop. */
5982 segment = elf_tdata (ibfd)->phdr;
5987 /* Extend SEGMENT to include SEGMENT2 and then delete
5989 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5990 - SEGMENT_END (segment, segment->p_vaddr));
5992 if (extra_length > 0)
5994 segment->p_memsz += extra_length;
5995 segment->p_filesz += extra_length;
5998 segment2->p_type = PT_NULL;
6003 /* The second scan attempts to assign sections to segments. */
6004 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6008 unsigned int section_count;
6009 asection **sections;
6010 asection *output_section;
6012 bfd_vma matching_lma;
6013 bfd_vma suggested_lma;
6016 asection *first_section;
6017 bfd_boolean first_matching_lma;
6018 bfd_boolean first_suggested_lma;
6020 if (segment->p_type == PT_NULL)
6023 first_section = NULL;
6024 /* Compute how many sections might be placed into this segment. */
6025 for (section = ibfd->sections, section_count = 0;
6027 section = section->next)
6029 /* Find the first section in the input segment, which may be
6030 removed from the corresponding output segment. */
6031 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6033 if (first_section == NULL)
6034 first_section = section;
6035 if (section->output_section != NULL)
6040 /* Allocate a segment map big enough to contain
6041 all of the sections we have selected. */
6042 amt = sizeof (struct elf_segment_map);
6043 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6044 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6048 /* Initialise the fields of the segment map. Default to
6049 using the physical address of the segment in the input BFD. */
6051 map->p_type = segment->p_type;
6052 map->p_flags = segment->p_flags;
6053 map->p_flags_valid = 1;
6055 /* If the first section in the input segment is removed, there is
6056 no need to preserve segment physical address in the corresponding
6058 if (!first_section || first_section->output_section != NULL)
6060 map->p_paddr = segment->p_paddr;
6061 map->p_paddr_valid = p_paddr_valid;
6064 /* Determine if this segment contains the ELF file header
6065 and if it contains the program headers themselves. */
6066 map->includes_filehdr = (segment->p_offset == 0
6067 && segment->p_filesz >= iehdr->e_ehsize);
6068 map->includes_phdrs = 0;
6070 if (!phdr_included || segment->p_type != PT_LOAD)
6072 map->includes_phdrs =
6073 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6074 && (segment->p_offset + segment->p_filesz
6075 >= ((bfd_vma) iehdr->e_phoff
6076 + iehdr->e_phnum * iehdr->e_phentsize)));
6078 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6079 phdr_included = TRUE;
6082 if (section_count == 0)
6084 /* Special segments, such as the PT_PHDR segment, may contain
6085 no sections, but ordinary, loadable segments should contain
6086 something. They are allowed by the ELF spec however, so only
6087 a warning is produced. */
6088 if (segment->p_type == PT_LOAD)
6089 (*_bfd_error_handler) (_("\
6090 %B: warning: Empty loadable segment detected, is this intentional ?"),
6094 *pointer_to_map = map;
6095 pointer_to_map = &map->next;
6100 /* Now scan the sections in the input BFD again and attempt
6101 to add their corresponding output sections to the segment map.
6102 The problem here is how to handle an output section which has
6103 been moved (ie had its LMA changed). There are four possibilities:
6105 1. None of the sections have been moved.
6106 In this case we can continue to use the segment LMA from the
6109 2. All of the sections have been moved by the same amount.
6110 In this case we can change the segment's LMA to match the LMA
6111 of the first section.
6113 3. Some of the sections have been moved, others have not.
6114 In this case those sections which have not been moved can be
6115 placed in the current segment which will have to have its size,
6116 and possibly its LMA changed, and a new segment or segments will
6117 have to be created to contain the other sections.
6119 4. The sections have been moved, but not by the same amount.
6120 In this case we can change the segment's LMA to match the LMA
6121 of the first section and we will have to create a new segment
6122 or segments to contain the other sections.
6124 In order to save time, we allocate an array to hold the section
6125 pointers that we are interested in. As these sections get assigned
6126 to a segment, they are removed from this array. */
6128 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
6129 if (sections == NULL)
6132 /* Step One: Scan for segment vs section LMA conflicts.
6133 Also add the sections to the section array allocated above.
6134 Also add the sections to the current segment. In the common
6135 case, where the sections have not been moved, this means that
6136 we have completely filled the segment, and there is nothing
6141 first_matching_lma = TRUE;
6142 first_suggested_lma = TRUE;
6144 for (section = ibfd->sections;
6146 section = section->next)
6147 if (section == first_section)
6150 for (j = 0; section != NULL; section = section->next)
6152 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
6154 output_section = section->output_section;
6156 sections[j++] = section;
6158 /* The Solaris native linker always sets p_paddr to 0.
6159 We try to catch that case here, and set it to the
6160 correct value. Note - some backends require that
6161 p_paddr be left as zero. */
6163 && segment->p_vaddr != 0
6164 && !bed->want_p_paddr_set_to_zero
6166 && output_section->lma != 0
6167 && output_section->vma == (segment->p_vaddr
6168 + (map->includes_filehdr
6171 + (map->includes_phdrs
6173 * iehdr->e_phentsize)
6175 map->p_paddr = segment->p_vaddr;
6177 /* Match up the physical address of the segment with the
6178 LMA address of the output section. */
6179 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6180 || IS_COREFILE_NOTE (segment, section)
6181 || (bed->want_p_paddr_set_to_zero
6182 && IS_CONTAINED_BY_VMA (output_section, segment)))
6184 if (first_matching_lma || output_section->lma < matching_lma)
6186 matching_lma = output_section->lma;
6187 first_matching_lma = FALSE;
6190 /* We assume that if the section fits within the segment
6191 then it does not overlap any other section within that
6193 map->sections[isec++] = output_section;
6195 else if (first_suggested_lma)
6197 suggested_lma = output_section->lma;
6198 first_suggested_lma = FALSE;
6201 if (j == section_count)
6206 BFD_ASSERT (j == section_count);
6208 /* Step Two: Adjust the physical address of the current segment,
6210 if (isec == section_count)
6212 /* All of the sections fitted within the segment as currently
6213 specified. This is the default case. Add the segment to
6214 the list of built segments and carry on to process the next
6215 program header in the input BFD. */
6216 map->count = section_count;
6217 *pointer_to_map = map;
6218 pointer_to_map = &map->next;
6221 && !bed->want_p_paddr_set_to_zero
6222 && matching_lma != map->p_paddr
6223 && !map->includes_filehdr
6224 && !map->includes_phdrs)
6225 /* There is some padding before the first section in the
6226 segment. So, we must account for that in the output
6228 map->p_vaddr_offset = matching_lma - map->p_paddr;
6235 if (!first_matching_lma)
6237 /* At least one section fits inside the current segment.
6238 Keep it, but modify its physical address to match the
6239 LMA of the first section that fitted. */
6240 map->p_paddr = matching_lma;
6244 /* None of the sections fitted inside the current segment.
6245 Change the current segment's physical address to match
6246 the LMA of the first section. */
6247 map->p_paddr = suggested_lma;
6250 /* Offset the segment physical address from the lma
6251 to allow for space taken up by elf headers. */
6252 if (map->includes_filehdr)
6254 if (map->p_paddr >= iehdr->e_ehsize)
6255 map->p_paddr -= iehdr->e_ehsize;
6258 map->includes_filehdr = FALSE;
6259 map->includes_phdrs = FALSE;
6263 if (map->includes_phdrs)
6265 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6267 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6269 /* iehdr->e_phnum is just an estimate of the number
6270 of program headers that we will need. Make a note
6271 here of the number we used and the segment we chose
6272 to hold these headers, so that we can adjust the
6273 offset when we know the correct value. */
6274 phdr_adjust_num = iehdr->e_phnum;
6275 phdr_adjust_seg = map;
6278 map->includes_phdrs = FALSE;
6282 /* Step Three: Loop over the sections again, this time assigning
6283 those that fit to the current segment and removing them from the
6284 sections array; but making sure not to leave large gaps. Once all
6285 possible sections have been assigned to the current segment it is
6286 added to the list of built segments and if sections still remain
6287 to be assigned, a new segment is constructed before repeating
6294 first_suggested_lma = TRUE;
6296 /* Fill the current segment with sections that fit. */
6297 for (j = 0; j < section_count; j++)
6299 section = sections[j];
6301 if (section == NULL)
6304 output_section = section->output_section;
6306 BFD_ASSERT (output_section != NULL);
6308 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6309 || IS_COREFILE_NOTE (segment, section))
6311 if (map->count == 0)
6313 /* If the first section in a segment does not start at
6314 the beginning of the segment, then something is
6316 if (output_section->lma
6318 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6319 + (map->includes_phdrs
6320 ? iehdr->e_phnum * iehdr->e_phentsize
6328 prev_sec = map->sections[map->count - 1];
6330 /* If the gap between the end of the previous section
6331 and the start of this section is more than
6332 maxpagesize then we need to start a new segment. */
6333 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6335 < BFD_ALIGN (output_section->lma, maxpagesize))
6336 || (prev_sec->lma + prev_sec->size
6337 > output_section->lma))
6339 if (first_suggested_lma)
6341 suggested_lma = output_section->lma;
6342 first_suggested_lma = FALSE;
6349 map->sections[map->count++] = output_section;
6352 section->segment_mark = TRUE;
6354 else if (first_suggested_lma)
6356 suggested_lma = output_section->lma;
6357 first_suggested_lma = FALSE;
6361 BFD_ASSERT (map->count > 0);
6363 /* Add the current segment to the list of built segments. */
6364 *pointer_to_map = map;
6365 pointer_to_map = &map->next;
6367 if (isec < section_count)
6369 /* We still have not allocated all of the sections to
6370 segments. Create a new segment here, initialise it
6371 and carry on looping. */
6372 amt = sizeof (struct elf_segment_map);
6373 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6374 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6381 /* Initialise the fields of the segment map. Set the physical
6382 physical address to the LMA of the first section that has
6383 not yet been assigned. */
6385 map->p_type = segment->p_type;
6386 map->p_flags = segment->p_flags;
6387 map->p_flags_valid = 1;
6388 map->p_paddr = suggested_lma;
6389 map->p_paddr_valid = p_paddr_valid;
6390 map->includes_filehdr = 0;
6391 map->includes_phdrs = 0;
6394 while (isec < section_count);
6399 elf_seg_map (obfd) = map_first;
6401 /* If we had to estimate the number of program headers that were
6402 going to be needed, then check our estimate now and adjust
6403 the offset if necessary. */
6404 if (phdr_adjust_seg != NULL)
6408 for (count = 0, map = map_first; map != NULL; map = map->next)
6411 if (count > phdr_adjust_num)
6412 phdr_adjust_seg->p_paddr
6413 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6418 #undef IS_CONTAINED_BY_VMA
6419 #undef IS_CONTAINED_BY_LMA
6421 #undef IS_COREFILE_NOTE
6422 #undef IS_SOLARIS_PT_INTERP
6423 #undef IS_SECTION_IN_INPUT_SEGMENT
6424 #undef INCLUDE_SECTION_IN_SEGMENT
6425 #undef SEGMENT_AFTER_SEGMENT
6426 #undef SEGMENT_OVERLAPS
6430 /* Copy ELF program header information. */
6433 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6435 Elf_Internal_Ehdr *iehdr;
6436 struct elf_segment_map *map;
6437 struct elf_segment_map *map_first;
6438 struct elf_segment_map **pointer_to_map;
6439 Elf_Internal_Phdr *segment;
6441 unsigned int num_segments;
6442 bfd_boolean phdr_included = FALSE;
6443 bfd_boolean p_paddr_valid;
6445 iehdr = elf_elfheader (ibfd);
6448 pointer_to_map = &map_first;
6450 /* If all the segment p_paddr fields are zero, don't set
6451 map->p_paddr_valid. */
6452 p_paddr_valid = FALSE;
6453 num_segments = elf_elfheader (ibfd)->e_phnum;
6454 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6457 if (segment->p_paddr != 0)
6459 p_paddr_valid = TRUE;
6463 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6468 unsigned int section_count;
6470 Elf_Internal_Shdr *this_hdr;
6471 asection *first_section = NULL;
6472 asection *lowest_section;
6474 /* Compute how many sections are in this segment. */
6475 for (section = ibfd->sections, section_count = 0;
6477 section = section->next)
6479 this_hdr = &(elf_section_data(section)->this_hdr);
6480 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6482 if (first_section == NULL)
6483 first_section = section;
6488 /* Allocate a segment map big enough to contain
6489 all of the sections we have selected. */
6490 amt = sizeof (struct elf_segment_map);
6491 if (section_count != 0)
6492 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6493 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6497 /* Initialize the fields of the output segment map with the
6500 map->p_type = segment->p_type;
6501 map->p_flags = segment->p_flags;
6502 map->p_flags_valid = 1;
6503 map->p_paddr = segment->p_paddr;
6504 map->p_paddr_valid = p_paddr_valid;
6505 map->p_align = segment->p_align;
6506 map->p_align_valid = 1;
6507 map->p_vaddr_offset = 0;
6509 if (map->p_type == PT_GNU_RELRO
6510 || map->p_type == PT_GNU_STACK)
6512 /* The PT_GNU_RELRO segment may contain the first a few
6513 bytes in the .got.plt section even if the whole .got.plt
6514 section isn't in the PT_GNU_RELRO segment. We won't
6515 change the size of the PT_GNU_RELRO segment.
6516 Similarly, PT_GNU_STACK size is significant on uclinux
6518 map->p_size = segment->p_memsz;
6519 map->p_size_valid = 1;
6522 /* Determine if this segment contains the ELF file header
6523 and if it contains the program headers themselves. */
6524 map->includes_filehdr = (segment->p_offset == 0
6525 && segment->p_filesz >= iehdr->e_ehsize);
6527 map->includes_phdrs = 0;
6528 if (! phdr_included || segment->p_type != PT_LOAD)
6530 map->includes_phdrs =
6531 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6532 && (segment->p_offset + segment->p_filesz
6533 >= ((bfd_vma) iehdr->e_phoff
6534 + iehdr->e_phnum * iehdr->e_phentsize)));
6536 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6537 phdr_included = TRUE;
6540 lowest_section = NULL;
6541 if (section_count != 0)
6543 unsigned int isec = 0;
6545 for (section = first_section;
6547 section = section->next)
6549 this_hdr = &(elf_section_data(section)->this_hdr);
6550 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6552 map->sections[isec++] = section->output_section;
6553 if ((section->flags & SEC_ALLOC) != 0)
6557 if (lowest_section == NULL
6558 || section->lma < lowest_section->lma)
6559 lowest_section = section;
6561 /* Section lmas are set up from PT_LOAD header
6562 p_paddr in _bfd_elf_make_section_from_shdr.
6563 If this header has a p_paddr that disagrees
6564 with the section lma, flag the p_paddr as
6566 if ((section->flags & SEC_LOAD) != 0)
6567 seg_off = this_hdr->sh_offset - segment->p_offset;
6569 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6570 if (section->lma - segment->p_paddr != seg_off)
6571 map->p_paddr_valid = FALSE;
6573 if (isec == section_count)
6579 if (map->includes_filehdr && lowest_section != NULL)
6580 /* We need to keep the space used by the headers fixed. */
6581 map->header_size = lowest_section->vma - segment->p_vaddr;
6583 if (!map->includes_phdrs
6584 && !map->includes_filehdr
6585 && map->p_paddr_valid)
6586 /* There is some other padding before the first section. */
6587 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6588 - segment->p_paddr);
6590 map->count = section_count;
6591 *pointer_to_map = map;
6592 pointer_to_map = &map->next;
6595 elf_seg_map (obfd) = map_first;
6599 /* Copy private BFD data. This copies or rewrites ELF program header
6603 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6605 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6606 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6609 if (elf_tdata (ibfd)->phdr == NULL)
6612 if (ibfd->xvec == obfd->xvec)
6614 /* Check to see if any sections in the input BFD
6615 covered by ELF program header have changed. */
6616 Elf_Internal_Phdr *segment;
6617 asection *section, *osec;
6618 unsigned int i, num_segments;
6619 Elf_Internal_Shdr *this_hdr;
6620 const struct elf_backend_data *bed;
6622 bed = get_elf_backend_data (ibfd);
6624 /* Regenerate the segment map if p_paddr is set to 0. */
6625 if (bed->want_p_paddr_set_to_zero)
6628 /* Initialize the segment mark field. */
6629 for (section = obfd->sections; section != NULL;
6630 section = section->next)
6631 section->segment_mark = FALSE;
6633 num_segments = elf_elfheader (ibfd)->e_phnum;
6634 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6638 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6639 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6640 which severly confuses things, so always regenerate the segment
6641 map in this case. */
6642 if (segment->p_paddr == 0
6643 && segment->p_memsz == 0
6644 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6647 for (section = ibfd->sections;
6648 section != NULL; section = section->next)
6650 /* We mark the output section so that we know it comes
6651 from the input BFD. */
6652 osec = section->output_section;
6654 osec->segment_mark = TRUE;
6656 /* Check if this section is covered by the segment. */
6657 this_hdr = &(elf_section_data(section)->this_hdr);
6658 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6660 /* FIXME: Check if its output section is changed or
6661 removed. What else do we need to check? */
6663 || section->flags != osec->flags
6664 || section->lma != osec->lma
6665 || section->vma != osec->vma
6666 || section->size != osec->size
6667 || section->rawsize != osec->rawsize
6668 || section->alignment_power != osec->alignment_power)
6674 /* Check to see if any output section do not come from the
6676 for (section = obfd->sections; section != NULL;
6677 section = section->next)
6679 if (section->segment_mark == FALSE)
6682 section->segment_mark = FALSE;
6685 return copy_elf_program_header (ibfd, obfd);
6689 if (ibfd->xvec == obfd->xvec)
6691 /* When rewriting program header, set the output maxpagesize to
6692 the maximum alignment of input PT_LOAD segments. */
6693 Elf_Internal_Phdr *segment;
6695 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6696 bfd_vma maxpagesize = 0;
6698 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6701 if (segment->p_type == PT_LOAD
6702 && maxpagesize < segment->p_align)
6704 /* PR 17512: file: f17299af. */
6705 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
6706 (*_bfd_error_handler) (_("\
6707 %B: warning: segment alignment of 0x%llx is too large"),
6708 ibfd, (long long) segment->p_align);
6710 maxpagesize = segment->p_align;
6713 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6714 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6717 return rewrite_elf_program_header (ibfd, obfd);
6720 /* Initialize private output section information from input section. */
6723 _bfd_elf_init_private_section_data (bfd *ibfd,
6727 struct bfd_link_info *link_info)
6730 Elf_Internal_Shdr *ihdr, *ohdr;
6731 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6733 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6734 || obfd->xvec->flavour != bfd_target_elf_flavour)
6737 BFD_ASSERT (elf_section_data (osec) != NULL);
6739 /* For objcopy and relocatable link, don't copy the output ELF
6740 section type from input if the output BFD section flags have been
6741 set to something different. For a final link allow some flags
6742 that the linker clears to differ. */
6743 if (elf_section_type (osec) == SHT_NULL
6744 && (osec->flags == isec->flags
6746 && ((osec->flags ^ isec->flags)
6747 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6748 elf_section_type (osec) = elf_section_type (isec);
6750 /* FIXME: Is this correct for all OS/PROC specific flags? */
6751 elf_section_flags (osec) |= (elf_section_flags (isec)
6752 & (SHF_MASKOS | SHF_MASKPROC));
6754 /* Set things up for objcopy and relocatable link. The output
6755 SHT_GROUP section will have its elf_next_in_group pointing back
6756 to the input group members. Ignore linker created group section.
6757 See elfNN_ia64_object_p in elfxx-ia64.c. */
6760 if (elf_sec_group (isec) == NULL
6761 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6763 if (elf_section_flags (isec) & SHF_GROUP)
6764 elf_section_flags (osec) |= SHF_GROUP;
6765 elf_next_in_group (osec) = elf_next_in_group (isec);
6766 elf_section_data (osec)->group = elf_section_data (isec)->group;
6769 /* If not decompress, preserve SHF_COMPRESSED. */
6770 if ((ibfd->flags & BFD_DECOMPRESS) == 0)
6771 elf_section_flags (osec) |= (elf_section_flags (isec)
6775 ihdr = &elf_section_data (isec)->this_hdr;
6777 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6778 don't use the output section of the linked-to section since it
6779 may be NULL at this point. */
6780 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6782 ohdr = &elf_section_data (osec)->this_hdr;
6783 ohdr->sh_flags |= SHF_LINK_ORDER;
6784 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6787 osec->use_rela_p = isec->use_rela_p;
6792 /* Copy private section information. This copies over the entsize
6793 field, and sometimes the info field. */
6796 _bfd_elf_copy_private_section_data (bfd *ibfd,
6801 Elf_Internal_Shdr *ihdr, *ohdr;
6803 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6804 || obfd->xvec->flavour != bfd_target_elf_flavour)
6807 ihdr = &elf_section_data (isec)->this_hdr;
6808 ohdr = &elf_section_data (osec)->this_hdr;
6810 ohdr->sh_entsize = ihdr->sh_entsize;
6812 if (ihdr->sh_type == SHT_SYMTAB
6813 || ihdr->sh_type == SHT_DYNSYM
6814 || ihdr->sh_type == SHT_GNU_verneed
6815 || ihdr->sh_type == SHT_GNU_verdef)
6816 ohdr->sh_info = ihdr->sh_info;
6818 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6822 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6823 necessary if we are removing either the SHT_GROUP section or any of
6824 the group member sections. DISCARDED is the value that a section's
6825 output_section has if the section will be discarded, NULL when this
6826 function is called from objcopy, bfd_abs_section_ptr when called
6830 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6834 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6835 if (elf_section_type (isec) == SHT_GROUP)
6837 asection *first = elf_next_in_group (isec);
6838 asection *s = first;
6839 bfd_size_type removed = 0;
6843 /* If this member section is being output but the
6844 SHT_GROUP section is not, then clear the group info
6845 set up by _bfd_elf_copy_private_section_data. */
6846 if (s->output_section != discarded
6847 && isec->output_section == discarded)
6849 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6850 elf_group_name (s->output_section) = NULL;
6852 /* Conversely, if the member section is not being output
6853 but the SHT_GROUP section is, then adjust its size. */
6854 else if (s->output_section == discarded
6855 && isec->output_section != discarded)
6857 s = elf_next_in_group (s);
6863 if (discarded != NULL)
6865 /* If we've been called for ld -r, then we need to
6866 adjust the input section size. This function may
6867 be called multiple times, so save the original
6869 if (isec->rawsize == 0)
6870 isec->rawsize = isec->size;
6871 isec->size = isec->rawsize - removed;
6875 /* Adjust the output section size when called from
6877 isec->output_section->size -= removed;
6885 /* Copy private header information. */
6888 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6890 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6891 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6894 /* Copy over private BFD data if it has not already been copied.
6895 This must be done here, rather than in the copy_private_bfd_data
6896 entry point, because the latter is called after the section
6897 contents have been set, which means that the program headers have
6898 already been worked out. */
6899 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6901 if (! copy_private_bfd_data (ibfd, obfd))
6905 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6908 /* Copy private symbol information. If this symbol is in a section
6909 which we did not map into a BFD section, try to map the section
6910 index correctly. We use special macro definitions for the mapped
6911 section indices; these definitions are interpreted by the
6912 swap_out_syms function. */
6914 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6915 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6916 #define MAP_STRTAB (SHN_HIOS + 3)
6917 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6918 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6921 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6926 elf_symbol_type *isym, *osym;
6928 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6929 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6932 isym = elf_symbol_from (ibfd, isymarg);
6933 osym = elf_symbol_from (obfd, osymarg);
6936 && isym->internal_elf_sym.st_shndx != 0
6938 && bfd_is_abs_section (isym->symbol.section))
6942 shndx = isym->internal_elf_sym.st_shndx;
6943 if (shndx == elf_onesymtab (ibfd))
6944 shndx = MAP_ONESYMTAB;
6945 else if (shndx == elf_dynsymtab (ibfd))
6946 shndx = MAP_DYNSYMTAB;
6947 else if (shndx == elf_strtab_sec (ibfd))
6949 else if (shndx == elf_shstrtab_sec (ibfd))
6950 shndx = MAP_SHSTRTAB;
6951 else if (shndx == elf_symtab_shndx (ibfd))
6952 shndx = MAP_SYM_SHNDX;
6953 osym->internal_elf_sym.st_shndx = shndx;
6959 /* Swap out the symbols. */
6962 swap_out_syms (bfd *abfd,
6963 struct bfd_strtab_hash **sttp,
6966 const struct elf_backend_data *bed;
6969 struct bfd_strtab_hash *stt;
6970 Elf_Internal_Shdr *symtab_hdr;
6971 Elf_Internal_Shdr *symtab_shndx_hdr;
6972 Elf_Internal_Shdr *symstrtab_hdr;
6973 bfd_byte *outbound_syms;
6974 bfd_byte *outbound_shndx;
6976 unsigned int num_locals;
6978 bfd_boolean name_local_sections;
6980 if (!elf_map_symbols (abfd, &num_locals))
6983 /* Dump out the symtabs. */
6984 stt = _bfd_elf_stringtab_init ();
6988 bed = get_elf_backend_data (abfd);
6989 symcount = bfd_get_symcount (abfd);
6990 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6991 symtab_hdr->sh_type = SHT_SYMTAB;
6992 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6993 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6994 symtab_hdr->sh_info = num_locals + 1;
6995 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6997 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6998 symstrtab_hdr->sh_type = SHT_STRTAB;
7000 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7001 bed->s->sizeof_sym);
7002 if (outbound_syms == NULL)
7004 _bfd_stringtab_free (stt);
7007 symtab_hdr->contents = outbound_syms;
7009 outbound_shndx = NULL;
7010 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7011 if (symtab_shndx_hdr->sh_name != 0)
7013 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7014 outbound_shndx = (bfd_byte *)
7015 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7016 if (outbound_shndx == NULL)
7018 _bfd_stringtab_free (stt);
7022 symtab_shndx_hdr->contents = outbound_shndx;
7023 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7024 symtab_shndx_hdr->sh_size = amt;
7025 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7026 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7029 /* Now generate the data (for "contents"). */
7031 /* Fill in zeroth symbol and swap it out. */
7032 Elf_Internal_Sym sym;
7038 sym.st_shndx = SHN_UNDEF;
7039 sym.st_target_internal = 0;
7040 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7041 outbound_syms += bed->s->sizeof_sym;
7042 if (outbound_shndx != NULL)
7043 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7047 = (bed->elf_backend_name_local_section_symbols
7048 && bed->elf_backend_name_local_section_symbols (abfd));
7050 syms = bfd_get_outsymbols (abfd);
7051 for (idx = 0; idx < symcount; idx++)
7053 Elf_Internal_Sym sym;
7054 bfd_vma value = syms[idx]->value;
7055 elf_symbol_type *type_ptr;
7056 flagword flags = syms[idx]->flags;
7059 if (!name_local_sections
7060 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
7062 /* Local section symbols have no name. */
7067 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
7070 if (sym.st_name == (unsigned long) -1)
7072 _bfd_stringtab_free (stt);
7077 type_ptr = elf_symbol_from (abfd, syms[idx]);
7079 if ((flags & BSF_SECTION_SYM) == 0
7080 && bfd_is_com_section (syms[idx]->section))
7082 /* ELF common symbols put the alignment into the `value' field,
7083 and the size into the `size' field. This is backwards from
7084 how BFD handles it, so reverse it here. */
7085 sym.st_size = value;
7086 if (type_ptr == NULL
7087 || type_ptr->internal_elf_sym.st_value == 0)
7088 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
7090 sym.st_value = type_ptr->internal_elf_sym.st_value;
7091 sym.st_shndx = _bfd_elf_section_from_bfd_section
7092 (abfd, syms[idx]->section);
7096 asection *sec = syms[idx]->section;
7099 if (sec->output_section)
7101 value += sec->output_offset;
7102 sec = sec->output_section;
7105 /* Don't add in the section vma for relocatable output. */
7106 if (! relocatable_p)
7108 sym.st_value = value;
7109 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
7111 if (bfd_is_abs_section (sec)
7113 && type_ptr->internal_elf_sym.st_shndx != 0)
7115 /* This symbol is in a real ELF section which we did
7116 not create as a BFD section. Undo the mapping done
7117 by copy_private_symbol_data. */
7118 shndx = type_ptr->internal_elf_sym.st_shndx;
7122 shndx = elf_onesymtab (abfd);
7125 shndx = elf_dynsymtab (abfd);
7128 shndx = elf_strtab_sec (abfd);
7131 shndx = elf_shstrtab_sec (abfd);
7134 shndx = elf_symtab_shndx (abfd);
7143 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
7145 if (shndx == SHN_BAD)
7149 /* Writing this would be a hell of a lot easier if
7150 we had some decent documentation on bfd, and
7151 knew what to expect of the library, and what to
7152 demand of applications. For example, it
7153 appears that `objcopy' might not set the
7154 section of a symbol to be a section that is
7155 actually in the output file. */
7156 sec2 = bfd_get_section_by_name (abfd, sec->name);
7159 _bfd_error_handler (_("\
7160 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7161 syms[idx]->name ? syms[idx]->name : "<Local sym>",
7163 bfd_set_error (bfd_error_invalid_operation);
7164 _bfd_stringtab_free (stt);
7168 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7169 BFD_ASSERT (shndx != SHN_BAD);
7173 sym.st_shndx = shndx;
7176 if ((flags & BSF_THREAD_LOCAL) != 0)
7178 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7179 type = STT_GNU_IFUNC;
7180 else if ((flags & BSF_FUNCTION) != 0)
7182 else if ((flags & BSF_OBJECT) != 0)
7184 else if ((flags & BSF_RELC) != 0)
7186 else if ((flags & BSF_SRELC) != 0)
7191 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7194 /* Processor-specific types. */
7195 if (type_ptr != NULL
7196 && bed->elf_backend_get_symbol_type)
7197 type = ((*bed->elf_backend_get_symbol_type)
7198 (&type_ptr->internal_elf_sym, type));
7200 if (flags & BSF_SECTION_SYM)
7202 if (flags & BSF_GLOBAL)
7203 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7205 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7207 else if (bfd_is_com_section (syms[idx]->section))
7209 #ifdef USE_STT_COMMON
7210 if (type == STT_OBJECT)
7211 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
7214 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7216 else if (bfd_is_und_section (syms[idx]->section))
7217 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7221 else if (flags & BSF_FILE)
7222 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7225 int bind = STB_LOCAL;
7227 if (flags & BSF_LOCAL)
7229 else if (flags & BSF_GNU_UNIQUE)
7230 bind = STB_GNU_UNIQUE;
7231 else if (flags & BSF_WEAK)
7233 else if (flags & BSF_GLOBAL)
7236 sym.st_info = ELF_ST_INFO (bind, type);
7239 if (type_ptr != NULL)
7241 sym.st_other = type_ptr->internal_elf_sym.st_other;
7242 sym.st_target_internal
7243 = type_ptr->internal_elf_sym.st_target_internal;
7248 sym.st_target_internal = 0;
7251 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7252 outbound_syms += bed->s->sizeof_sym;
7253 if (outbound_shndx != NULL)
7254 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7258 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
7259 symstrtab_hdr->sh_type = SHT_STRTAB;
7261 symstrtab_hdr->sh_flags = 0;
7262 symstrtab_hdr->sh_addr = 0;
7263 symstrtab_hdr->sh_entsize = 0;
7264 symstrtab_hdr->sh_link = 0;
7265 symstrtab_hdr->sh_info = 0;
7266 symstrtab_hdr->sh_addralign = 1;
7271 /* Return the number of bytes required to hold the symtab vector.
7273 Note that we base it on the count plus 1, since we will null terminate
7274 the vector allocated based on this size. However, the ELF symbol table
7275 always has a dummy entry as symbol #0, so it ends up even. */
7278 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
7282 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7284 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7285 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7287 symtab_size -= sizeof (asymbol *);
7293 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7297 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7299 if (elf_dynsymtab (abfd) == 0)
7301 bfd_set_error (bfd_error_invalid_operation);
7305 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7306 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7308 symtab_size -= sizeof (asymbol *);
7314 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7317 return (asect->reloc_count + 1) * sizeof (arelent *);
7320 /* Canonicalize the relocs. */
7323 _bfd_elf_canonicalize_reloc (bfd *abfd,
7330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7332 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7335 tblptr = section->relocation;
7336 for (i = 0; i < section->reloc_count; i++)
7337 *relptr++ = tblptr++;
7341 return section->reloc_count;
7345 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7348 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7351 bfd_get_symcount (abfd) = symcount;
7356 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7357 asymbol **allocation)
7359 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7360 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7363 bfd_get_dynamic_symcount (abfd) = symcount;
7367 /* Return the size required for the dynamic reloc entries. Any loadable
7368 section that was actually installed in the BFD, and has type SHT_REL
7369 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7370 dynamic reloc section. */
7373 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7378 if (elf_dynsymtab (abfd) == 0)
7380 bfd_set_error (bfd_error_invalid_operation);
7384 ret = sizeof (arelent *);
7385 for (s = abfd->sections; s != NULL; s = s->next)
7386 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7387 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7388 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7389 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7390 * sizeof (arelent *));
7395 /* Canonicalize the dynamic relocation entries. Note that we return the
7396 dynamic relocations as a single block, although they are actually
7397 associated with particular sections; the interface, which was
7398 designed for SunOS style shared libraries, expects that there is only
7399 one set of dynamic relocs. Any loadable section that was actually
7400 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7401 dynamic symbol table, is considered to be a dynamic reloc section. */
7404 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7408 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7412 if (elf_dynsymtab (abfd) == 0)
7414 bfd_set_error (bfd_error_invalid_operation);
7418 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7420 for (s = abfd->sections; s != NULL; s = s->next)
7422 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7423 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7424 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7429 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7431 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7433 for (i = 0; i < count; i++)
7444 /* Read in the version information. */
7447 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7449 bfd_byte *contents = NULL;
7450 unsigned int freeidx = 0;
7452 if (elf_dynverref (abfd) != 0)
7454 Elf_Internal_Shdr *hdr;
7455 Elf_External_Verneed *everneed;
7456 Elf_Internal_Verneed *iverneed;
7458 bfd_byte *contents_end;
7460 hdr = &elf_tdata (abfd)->dynverref_hdr;
7462 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed))
7464 error_return_bad_verref:
7465 (*_bfd_error_handler)
7466 (_("%B: .gnu.version_r invalid entry"), abfd);
7467 bfd_set_error (bfd_error_bad_value);
7468 error_return_verref:
7469 elf_tdata (abfd)->verref = NULL;
7470 elf_tdata (abfd)->cverrefs = 0;
7474 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7475 if (contents == NULL)
7476 goto error_return_verref;
7478 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7479 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7480 goto error_return_verref;
7482 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7483 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7485 if (elf_tdata (abfd)->verref == NULL)
7486 goto error_return_verref;
7488 BFD_ASSERT (sizeof (Elf_External_Verneed)
7489 == sizeof (Elf_External_Vernaux));
7490 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7491 everneed = (Elf_External_Verneed *) contents;
7492 iverneed = elf_tdata (abfd)->verref;
7493 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7495 Elf_External_Vernaux *evernaux;
7496 Elf_Internal_Vernaux *ivernaux;
7499 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7501 iverneed->vn_bfd = abfd;
7503 iverneed->vn_filename =
7504 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7506 if (iverneed->vn_filename == NULL)
7507 goto error_return_bad_verref;
7509 if (iverneed->vn_cnt == 0)
7510 iverneed->vn_auxptr = NULL;
7513 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7514 bfd_alloc2 (abfd, iverneed->vn_cnt,
7515 sizeof (Elf_Internal_Vernaux));
7516 if (iverneed->vn_auxptr == NULL)
7517 goto error_return_verref;
7520 if (iverneed->vn_aux
7521 > (size_t) (contents_end - (bfd_byte *) everneed))
7522 goto error_return_bad_verref;
7524 evernaux = ((Elf_External_Vernaux *)
7525 ((bfd_byte *) everneed + iverneed->vn_aux));
7526 ivernaux = iverneed->vn_auxptr;
7527 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7529 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7531 ivernaux->vna_nodename =
7532 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7533 ivernaux->vna_name);
7534 if (ivernaux->vna_nodename == NULL)
7535 goto error_return_bad_verref;
7537 if (ivernaux->vna_other > freeidx)
7538 freeidx = ivernaux->vna_other;
7540 ivernaux->vna_nextptr = NULL;
7541 if (ivernaux->vna_next == 0)
7543 iverneed->vn_cnt = j + 1;
7546 if (j + 1 < iverneed->vn_cnt)
7547 ivernaux->vna_nextptr = ivernaux + 1;
7549 if (ivernaux->vna_next
7550 > (size_t) (contents_end - (bfd_byte *) evernaux))
7551 goto error_return_bad_verref;
7553 evernaux = ((Elf_External_Vernaux *)
7554 ((bfd_byte *) evernaux + ivernaux->vna_next));
7557 iverneed->vn_nextref = NULL;
7558 if (iverneed->vn_next == 0)
7560 if (i + 1 < hdr->sh_info)
7561 iverneed->vn_nextref = iverneed + 1;
7563 if (iverneed->vn_next
7564 > (size_t) (contents_end - (bfd_byte *) everneed))
7565 goto error_return_bad_verref;
7567 everneed = ((Elf_External_Verneed *)
7568 ((bfd_byte *) everneed + iverneed->vn_next));
7570 elf_tdata (abfd)->cverrefs = i;
7576 if (elf_dynverdef (abfd) != 0)
7578 Elf_Internal_Shdr *hdr;
7579 Elf_External_Verdef *everdef;
7580 Elf_Internal_Verdef *iverdef;
7581 Elf_Internal_Verdef *iverdefarr;
7582 Elf_Internal_Verdef iverdefmem;
7584 unsigned int maxidx;
7585 bfd_byte *contents_end_def, *contents_end_aux;
7587 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7589 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
7591 error_return_bad_verdef:
7592 (*_bfd_error_handler)
7593 (_("%B: .gnu.version_d invalid entry"), abfd);
7594 bfd_set_error (bfd_error_bad_value);
7595 error_return_verdef:
7596 elf_tdata (abfd)->verdef = NULL;
7597 elf_tdata (abfd)->cverdefs = 0;
7601 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7602 if (contents == NULL)
7603 goto error_return_verdef;
7604 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7605 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7606 goto error_return_verdef;
7608 BFD_ASSERT (sizeof (Elf_External_Verdef)
7609 >= sizeof (Elf_External_Verdaux));
7610 contents_end_def = contents + hdr->sh_size
7611 - sizeof (Elf_External_Verdef);
7612 contents_end_aux = contents + hdr->sh_size
7613 - sizeof (Elf_External_Verdaux);
7615 /* We know the number of entries in the section but not the maximum
7616 index. Therefore we have to run through all entries and find
7618 everdef = (Elf_External_Verdef *) contents;
7620 for (i = 0; i < hdr->sh_info; ++i)
7622 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7624 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
7625 goto error_return_bad_verdef;
7626 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7627 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7629 if (iverdefmem.vd_next == 0)
7632 if (iverdefmem.vd_next
7633 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7634 goto error_return_bad_verdef;
7636 everdef = ((Elf_External_Verdef *)
7637 ((bfd_byte *) everdef + iverdefmem.vd_next));
7640 if (default_imported_symver)
7642 if (freeidx > maxidx)
7648 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7649 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7650 if (elf_tdata (abfd)->verdef == NULL)
7651 goto error_return_verdef;
7653 elf_tdata (abfd)->cverdefs = maxidx;
7655 everdef = (Elf_External_Verdef *) contents;
7656 iverdefarr = elf_tdata (abfd)->verdef;
7657 for (i = 0; i < hdr->sh_info; i++)
7659 Elf_External_Verdaux *everdaux;
7660 Elf_Internal_Verdaux *iverdaux;
7663 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7665 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7666 goto error_return_bad_verdef;
7668 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7669 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7671 iverdef->vd_bfd = abfd;
7673 if (iverdef->vd_cnt == 0)
7674 iverdef->vd_auxptr = NULL;
7677 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7678 bfd_alloc2 (abfd, iverdef->vd_cnt,
7679 sizeof (Elf_Internal_Verdaux));
7680 if (iverdef->vd_auxptr == NULL)
7681 goto error_return_verdef;
7685 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7686 goto error_return_bad_verdef;
7688 everdaux = ((Elf_External_Verdaux *)
7689 ((bfd_byte *) everdef + iverdef->vd_aux));
7690 iverdaux = iverdef->vd_auxptr;
7691 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7693 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7695 iverdaux->vda_nodename =
7696 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7697 iverdaux->vda_name);
7698 if (iverdaux->vda_nodename == NULL)
7699 goto error_return_bad_verdef;
7701 iverdaux->vda_nextptr = NULL;
7702 if (iverdaux->vda_next == 0)
7704 iverdef->vd_cnt = j + 1;
7707 if (j + 1 < iverdef->vd_cnt)
7708 iverdaux->vda_nextptr = iverdaux + 1;
7710 if (iverdaux->vda_next
7711 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7712 goto error_return_bad_verdef;
7714 everdaux = ((Elf_External_Verdaux *)
7715 ((bfd_byte *) everdaux + iverdaux->vda_next));
7718 if (iverdef->vd_cnt)
7719 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7721 iverdef->vd_nextdef = NULL;
7722 if (iverdef->vd_next == 0)
7724 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7725 iverdef->vd_nextdef = iverdef + 1;
7727 everdef = ((Elf_External_Verdef *)
7728 ((bfd_byte *) everdef + iverdef->vd_next));
7734 else if (default_imported_symver)
7741 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7742 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7743 if (elf_tdata (abfd)->verdef == NULL)
7746 elf_tdata (abfd)->cverdefs = freeidx;
7749 /* Create a default version based on the soname. */
7750 if (default_imported_symver)
7752 Elf_Internal_Verdef *iverdef;
7753 Elf_Internal_Verdaux *iverdaux;
7755 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7757 iverdef->vd_version = VER_DEF_CURRENT;
7758 iverdef->vd_flags = 0;
7759 iverdef->vd_ndx = freeidx;
7760 iverdef->vd_cnt = 1;
7762 iverdef->vd_bfd = abfd;
7764 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7765 if (iverdef->vd_nodename == NULL)
7766 goto error_return_verdef;
7767 iverdef->vd_nextdef = NULL;
7768 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
7769 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
7770 if (iverdef->vd_auxptr == NULL)
7771 goto error_return_verdef;
7773 iverdaux = iverdef->vd_auxptr;
7774 iverdaux->vda_nodename = iverdef->vd_nodename;
7780 if (contents != NULL)
7786 _bfd_elf_make_empty_symbol (bfd *abfd)
7788 elf_symbol_type *newsym;
7790 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
7793 newsym->symbol.the_bfd = abfd;
7794 return &newsym->symbol;
7798 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7802 bfd_symbol_info (symbol, ret);
7805 /* Return whether a symbol name implies a local symbol. Most targets
7806 use this function for the is_local_label_name entry point, but some
7810 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7813 /* Normal local symbols start with ``.L''. */
7814 if (name[0] == '.' && name[1] == 'L')
7817 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7818 DWARF debugging symbols starting with ``..''. */
7819 if (name[0] == '.' && name[1] == '.')
7822 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7823 emitting DWARF debugging output. I suspect this is actually a
7824 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7825 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7826 underscore to be emitted on some ELF targets). For ease of use,
7827 we treat such symbols as local. */
7828 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7831 /* Treat assembler generated local labels as local. */
7832 if (name[0] == 'L' && name[strlen (name) - 1] < 32)
7839 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7840 asymbol *symbol ATTRIBUTE_UNUSED)
7847 _bfd_elf_set_arch_mach (bfd *abfd,
7848 enum bfd_architecture arch,
7849 unsigned long machine)
7851 /* If this isn't the right architecture for this backend, and this
7852 isn't the generic backend, fail. */
7853 if (arch != get_elf_backend_data (abfd)->arch
7854 && arch != bfd_arch_unknown
7855 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7858 return bfd_default_set_arch_mach (abfd, arch, machine);
7861 /* Find the nearest line to a particular section and offset,
7862 for error reporting. */
7865 _bfd_elf_find_nearest_line (bfd *abfd,
7869 const char **filename_ptr,
7870 const char **functionname_ptr,
7871 unsigned int *line_ptr,
7872 unsigned int *discriminator_ptr)
7876 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7877 filename_ptr, functionname_ptr,
7878 line_ptr, discriminator_ptr,
7879 dwarf_debug_sections, 0,
7880 &elf_tdata (abfd)->dwarf2_find_line_info)
7881 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
7882 filename_ptr, functionname_ptr,
7885 if (!*functionname_ptr)
7886 _bfd_elf_find_function (abfd, symbols, section, offset,
7887 *filename_ptr ? NULL : filename_ptr,
7892 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7893 &found, filename_ptr,
7894 functionname_ptr, line_ptr,
7895 &elf_tdata (abfd)->line_info))
7897 if (found && (*functionname_ptr || *line_ptr))
7900 if (symbols == NULL)
7903 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
7904 filename_ptr, functionname_ptr))
7911 /* Find the line for a symbol. */
7914 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7915 const char **filename_ptr, unsigned int *line_ptr)
7917 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
7918 filename_ptr, NULL, line_ptr, NULL,
7919 dwarf_debug_sections, 0,
7920 &elf_tdata (abfd)->dwarf2_find_line_info);
7923 /* After a call to bfd_find_nearest_line, successive calls to
7924 bfd_find_inliner_info can be used to get source information about
7925 each level of function inlining that terminated at the address
7926 passed to bfd_find_nearest_line. Currently this is only supported
7927 for DWARF2 with appropriate DWARF3 extensions. */
7930 _bfd_elf_find_inliner_info (bfd *abfd,
7931 const char **filename_ptr,
7932 const char **functionname_ptr,
7933 unsigned int *line_ptr)
7936 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7937 functionname_ptr, line_ptr,
7938 & elf_tdata (abfd)->dwarf2_find_line_info);
7943 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7945 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7946 int ret = bed->s->sizeof_ehdr;
7948 if (!info->relocatable)
7950 bfd_size_type phdr_size = elf_program_header_size (abfd);
7952 if (phdr_size == (bfd_size_type) -1)
7954 struct elf_segment_map *m;
7957 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7958 phdr_size += bed->s->sizeof_phdr;
7961 phdr_size = get_program_header_size (abfd, info);
7964 elf_program_header_size (abfd) = phdr_size;
7972 _bfd_elf_set_section_contents (bfd *abfd,
7974 const void *location,
7976 bfd_size_type count)
7978 Elf_Internal_Shdr *hdr;
7981 if (! abfd->output_has_begun
7982 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7988 hdr = &elf_section_data (section)->this_hdr;
7989 if (hdr->sh_offset == (file_ptr) -1)
7991 /* We must compress this section. Write output to the buffer. */
7992 unsigned char *contents = hdr->contents;
7993 if ((offset + count) > hdr->sh_size
7994 || (section->flags & SEC_ELF_COMPRESS) == 0
7995 || contents == NULL)
7997 memcpy (contents + offset, location, count);
8000 pos = hdr->sh_offset + offset;
8001 if (bfd_seek (abfd, pos, SEEK_SET) != 0
8002 || bfd_bwrite (location, count, abfd) != count)
8009 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
8010 arelent *cache_ptr ATTRIBUTE_UNUSED,
8011 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
8016 /* Try to convert a non-ELF reloc into an ELF one. */
8019 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
8021 /* Check whether we really have an ELF howto. */
8023 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
8025 bfd_reloc_code_real_type code;
8026 reloc_howto_type *howto;
8028 /* Alien reloc: Try to determine its type to replace it with an
8029 equivalent ELF reloc. */
8031 if (areloc->howto->pc_relative)
8033 switch (areloc->howto->bitsize)
8036 code = BFD_RELOC_8_PCREL;
8039 code = BFD_RELOC_12_PCREL;
8042 code = BFD_RELOC_16_PCREL;
8045 code = BFD_RELOC_24_PCREL;
8048 code = BFD_RELOC_32_PCREL;
8051 code = BFD_RELOC_64_PCREL;
8057 howto = bfd_reloc_type_lookup (abfd, code);
8059 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
8061 if (howto->pcrel_offset)
8062 areloc->addend += areloc->address;
8064 areloc->addend -= areloc->address; /* addend is unsigned!! */
8069 switch (areloc->howto->bitsize)
8075 code = BFD_RELOC_14;
8078 code = BFD_RELOC_16;
8081 code = BFD_RELOC_26;
8084 code = BFD_RELOC_32;
8087 code = BFD_RELOC_64;
8093 howto = bfd_reloc_type_lookup (abfd, code);
8097 areloc->howto = howto;
8105 (*_bfd_error_handler)
8106 (_("%B: unsupported relocation type %s"),
8107 abfd, areloc->howto->name);
8108 bfd_set_error (bfd_error_bad_value);
8113 _bfd_elf_close_and_cleanup (bfd *abfd)
8115 struct elf_obj_tdata *tdata = elf_tdata (abfd);
8116 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
8118 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
8119 _bfd_elf_strtab_free (elf_shstrtab (abfd));
8120 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
8123 return _bfd_generic_close_and_cleanup (abfd);
8126 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8127 in the relocation's offset. Thus we cannot allow any sort of sanity
8128 range-checking to interfere. There is nothing else to do in processing
8131 bfd_reloc_status_type
8132 _bfd_elf_rel_vtable_reloc_fn
8133 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
8134 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
8135 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
8136 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
8138 return bfd_reloc_ok;
8141 /* Elf core file support. Much of this only works on native
8142 toolchains, since we rely on knowing the
8143 machine-dependent procfs structure in order to pick
8144 out details about the corefile. */
8146 #ifdef HAVE_SYS_PROCFS_H
8147 /* Needed for new procfs interface on sparc-solaris. */
8148 # define _STRUCTURED_PROC 1
8149 # include <sys/procfs.h>
8152 /* Return a PID that identifies a "thread" for threaded cores, or the
8153 PID of the main process for non-threaded cores. */
8156 elfcore_make_pid (bfd *abfd)
8160 pid = elf_tdata (abfd)->core->lwpid;
8162 pid = elf_tdata (abfd)->core->pid;
8167 /* If there isn't a section called NAME, make one, using
8168 data from SECT. Note, this function will generate a
8169 reference to NAME, so you shouldn't deallocate or
8173 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8177 if (bfd_get_section_by_name (abfd, name) != NULL)
8180 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8184 sect2->size = sect->size;
8185 sect2->filepos = sect->filepos;
8186 sect2->alignment_power = sect->alignment_power;
8190 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8191 actually creates up to two pseudosections:
8192 - For the single-threaded case, a section named NAME, unless
8193 such a section already exists.
8194 - For the multi-threaded case, a section named "NAME/PID", where
8195 PID is elfcore_make_pid (abfd).
8196 Both pseudosections have identical contents. */
8198 _bfd_elfcore_make_pseudosection (bfd *abfd,
8204 char *threaded_name;
8208 /* Build the section name. */
8210 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8211 len = strlen (buf) + 1;
8212 threaded_name = (char *) bfd_alloc (abfd, len);
8213 if (threaded_name == NULL)
8215 memcpy (threaded_name, buf, len);
8217 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8222 sect->filepos = filepos;
8223 sect->alignment_power = 2;
8225 return elfcore_maybe_make_sect (abfd, name, sect);
8228 /* prstatus_t exists on:
8230 linux 2.[01] + glibc
8234 #if defined (HAVE_PRSTATUS_T)
8237 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8242 if (note->descsz == sizeof (prstatus_t))
8246 size = sizeof (prstat.pr_reg);
8247 offset = offsetof (prstatus_t, pr_reg);
8248 memcpy (&prstat, note->descdata, sizeof (prstat));
8250 /* Do not overwrite the core signal if it
8251 has already been set by another thread. */
8252 if (elf_tdata (abfd)->core->signal == 0)
8253 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8254 if (elf_tdata (abfd)->core->pid == 0)
8255 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8257 /* pr_who exists on:
8260 pr_who doesn't exist on:
8263 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8264 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8266 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8269 #if defined (HAVE_PRSTATUS32_T)
8270 else if (note->descsz == sizeof (prstatus32_t))
8272 /* 64-bit host, 32-bit corefile */
8273 prstatus32_t prstat;
8275 size = sizeof (prstat.pr_reg);
8276 offset = offsetof (prstatus32_t, pr_reg);
8277 memcpy (&prstat, note->descdata, sizeof (prstat));
8279 /* Do not overwrite the core signal if it
8280 has already been set by another thread. */
8281 if (elf_tdata (abfd)->core->signal == 0)
8282 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8283 if (elf_tdata (abfd)->core->pid == 0)
8284 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8286 /* pr_who exists on:
8289 pr_who doesn't exist on:
8292 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8293 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8295 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8298 #endif /* HAVE_PRSTATUS32_T */
8301 /* Fail - we don't know how to handle any other
8302 note size (ie. data object type). */
8306 /* Make a ".reg/999" section and a ".reg" section. */
8307 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8308 size, note->descpos + offset);
8310 #endif /* defined (HAVE_PRSTATUS_T) */
8312 /* Create a pseudosection containing the exact contents of NOTE. */
8314 elfcore_make_note_pseudosection (bfd *abfd,
8316 Elf_Internal_Note *note)
8318 return _bfd_elfcore_make_pseudosection (abfd, name,
8319 note->descsz, note->descpos);
8322 /* There isn't a consistent prfpregset_t across platforms,
8323 but it doesn't matter, because we don't have to pick this
8324 data structure apart. */
8327 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8329 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8332 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8333 type of NT_PRXFPREG. Just include the whole note's contents
8337 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8339 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8342 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8343 with a note type of NT_X86_XSTATE. Just include the whole note's
8344 contents literally. */
8347 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8349 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8353 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8355 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8359 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8361 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8365 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8367 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8371 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8373 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8377 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8379 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8383 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8385 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8389 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8391 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8395 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8397 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8401 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8403 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8407 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8409 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8413 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8415 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8419 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
8421 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
8425 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
8427 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
8431 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8433 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8437 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8439 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8443 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8445 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8449 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8451 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8454 #if defined (HAVE_PRPSINFO_T)
8455 typedef prpsinfo_t elfcore_psinfo_t;
8456 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8457 typedef prpsinfo32_t elfcore_psinfo32_t;
8461 #if defined (HAVE_PSINFO_T)
8462 typedef psinfo_t elfcore_psinfo_t;
8463 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8464 typedef psinfo32_t elfcore_psinfo32_t;
8468 /* return a malloc'ed copy of a string at START which is at
8469 most MAX bytes long, possibly without a terminating '\0'.
8470 the copy will always have a terminating '\0'. */
8473 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8476 char *end = (char *) memchr (start, '\0', max);
8484 dups = (char *) bfd_alloc (abfd, len + 1);
8488 memcpy (dups, start, len);
8494 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8496 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8498 if (note->descsz == sizeof (elfcore_psinfo_t))
8500 elfcore_psinfo_t psinfo;
8502 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8504 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8505 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8507 elf_tdata (abfd)->core->program
8508 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8509 sizeof (psinfo.pr_fname));
8511 elf_tdata (abfd)->core->command
8512 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8513 sizeof (psinfo.pr_psargs));
8515 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8516 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8518 /* 64-bit host, 32-bit corefile */
8519 elfcore_psinfo32_t psinfo;
8521 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8523 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8524 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8526 elf_tdata (abfd)->core->program
8527 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8528 sizeof (psinfo.pr_fname));
8530 elf_tdata (abfd)->core->command
8531 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8532 sizeof (psinfo.pr_psargs));
8538 /* Fail - we don't know how to handle any other
8539 note size (ie. data object type). */
8543 /* Note that for some reason, a spurious space is tacked
8544 onto the end of the args in some (at least one anyway)
8545 implementations, so strip it off if it exists. */
8548 char *command = elf_tdata (abfd)->core->command;
8549 int n = strlen (command);
8551 if (0 < n && command[n - 1] == ' ')
8552 command[n - 1] = '\0';
8557 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8559 #if defined (HAVE_PSTATUS_T)
8561 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8563 if (note->descsz == sizeof (pstatus_t)
8564 #if defined (HAVE_PXSTATUS_T)
8565 || note->descsz == sizeof (pxstatus_t)
8571 memcpy (&pstat, note->descdata, sizeof (pstat));
8573 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8575 #if defined (HAVE_PSTATUS32_T)
8576 else if (note->descsz == sizeof (pstatus32_t))
8578 /* 64-bit host, 32-bit corefile */
8581 memcpy (&pstat, note->descdata, sizeof (pstat));
8583 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8586 /* Could grab some more details from the "representative"
8587 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8588 NT_LWPSTATUS note, presumably. */
8592 #endif /* defined (HAVE_PSTATUS_T) */
8594 #if defined (HAVE_LWPSTATUS_T)
8596 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8598 lwpstatus_t lwpstat;
8604 if (note->descsz != sizeof (lwpstat)
8605 #if defined (HAVE_LWPXSTATUS_T)
8606 && note->descsz != sizeof (lwpxstatus_t)
8611 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8613 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8614 /* Do not overwrite the core signal if it has already been set by
8616 if (elf_tdata (abfd)->core->signal == 0)
8617 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8619 /* Make a ".reg/999" section. */
8621 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8622 len = strlen (buf) + 1;
8623 name = bfd_alloc (abfd, len);
8626 memcpy (name, buf, len);
8628 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8632 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8633 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8634 sect->filepos = note->descpos
8635 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8638 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8639 sect->size = sizeof (lwpstat.pr_reg);
8640 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8643 sect->alignment_power = 2;
8645 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8648 /* Make a ".reg2/999" section */
8650 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8651 len = strlen (buf) + 1;
8652 name = bfd_alloc (abfd, len);
8655 memcpy (name, buf, len);
8657 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8661 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8662 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8663 sect->filepos = note->descpos
8664 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8667 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8668 sect->size = sizeof (lwpstat.pr_fpreg);
8669 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8672 sect->alignment_power = 2;
8674 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8676 #endif /* defined (HAVE_LWPSTATUS_T) */
8679 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8686 int is_active_thread;
8689 if (note->descsz < 728)
8692 if (! CONST_STRNEQ (note->namedata, "win32"))
8695 type = bfd_get_32 (abfd, note->descdata);
8699 case 1 /* NOTE_INFO_PROCESS */:
8700 /* FIXME: need to add ->core->command. */
8701 /* process_info.pid */
8702 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8703 /* process_info.signal */
8704 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8707 case 2 /* NOTE_INFO_THREAD */:
8708 /* Make a ".reg/999" section. */
8709 /* thread_info.tid */
8710 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8712 len = strlen (buf) + 1;
8713 name = (char *) bfd_alloc (abfd, len);
8717 memcpy (name, buf, len);
8719 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8723 /* sizeof (thread_info.thread_context) */
8725 /* offsetof (thread_info.thread_context) */
8726 sect->filepos = note->descpos + 12;
8727 sect->alignment_power = 2;
8729 /* thread_info.is_active_thread */
8730 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8732 if (is_active_thread)
8733 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8737 case 3 /* NOTE_INFO_MODULE */:
8738 /* Make a ".module/xxxxxxxx" section. */
8739 /* module_info.base_address */
8740 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8741 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8743 len = strlen (buf) + 1;
8744 name = (char *) bfd_alloc (abfd, len);
8748 memcpy (name, buf, len);
8750 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8755 sect->size = note->descsz;
8756 sect->filepos = note->descpos;
8757 sect->alignment_power = 2;
8768 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8770 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8778 if (bed->elf_backend_grok_prstatus)
8779 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8781 #if defined (HAVE_PRSTATUS_T)
8782 return elfcore_grok_prstatus (abfd, note);
8787 #if defined (HAVE_PSTATUS_T)
8789 return elfcore_grok_pstatus (abfd, note);
8792 #if defined (HAVE_LWPSTATUS_T)
8794 return elfcore_grok_lwpstatus (abfd, note);
8797 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8798 return elfcore_grok_prfpreg (abfd, note);
8800 case NT_WIN32PSTATUS:
8801 return elfcore_grok_win32pstatus (abfd, note);
8803 case NT_PRXFPREG: /* Linux SSE extension */
8804 if (note->namesz == 6
8805 && strcmp (note->namedata, "LINUX") == 0)
8806 return elfcore_grok_prxfpreg (abfd, note);
8810 case NT_X86_XSTATE: /* Linux XSAVE extension */
8811 if (note->namesz == 6
8812 && strcmp (note->namedata, "LINUX") == 0)
8813 return elfcore_grok_xstatereg (abfd, note);
8814 else if (note->namesz == 8
8815 && strcmp (note->namedata, "FreeBSD") == 0)
8816 return elfcore_grok_xstatereg (abfd, note);
8821 if (note->namesz == 6
8822 && strcmp (note->namedata, "LINUX") == 0)
8823 return elfcore_grok_ppc_vmx (abfd, note);
8828 if (note->namesz == 6
8829 && strcmp (note->namedata, "LINUX") == 0)
8830 return elfcore_grok_ppc_vsx (abfd, note);
8834 case NT_S390_HIGH_GPRS:
8835 if (note->namesz == 6
8836 && strcmp (note->namedata, "LINUX") == 0)
8837 return elfcore_grok_s390_high_gprs (abfd, note);
8842 if (note->namesz == 6
8843 && strcmp (note->namedata, "LINUX") == 0)
8844 return elfcore_grok_s390_timer (abfd, note);
8848 case NT_S390_TODCMP:
8849 if (note->namesz == 6
8850 && strcmp (note->namedata, "LINUX") == 0)
8851 return elfcore_grok_s390_todcmp (abfd, note);
8855 case NT_S390_TODPREG:
8856 if (note->namesz == 6
8857 && strcmp (note->namedata, "LINUX") == 0)
8858 return elfcore_grok_s390_todpreg (abfd, note);
8863 if (note->namesz == 6
8864 && strcmp (note->namedata, "LINUX") == 0)
8865 return elfcore_grok_s390_ctrs (abfd, note);
8869 case NT_S390_PREFIX:
8870 if (note->namesz == 6
8871 && strcmp (note->namedata, "LINUX") == 0)
8872 return elfcore_grok_s390_prefix (abfd, note);
8876 case NT_S390_LAST_BREAK:
8877 if (note->namesz == 6
8878 && strcmp (note->namedata, "LINUX") == 0)
8879 return elfcore_grok_s390_last_break (abfd, note);
8883 case NT_S390_SYSTEM_CALL:
8884 if (note->namesz == 6
8885 && strcmp (note->namedata, "LINUX") == 0)
8886 return elfcore_grok_s390_system_call (abfd, note);
8891 if (note->namesz == 6
8892 && strcmp (note->namedata, "LINUX") == 0)
8893 return elfcore_grok_s390_tdb (abfd, note);
8897 case NT_S390_VXRS_LOW:
8898 if (note->namesz == 6
8899 && strcmp (note->namedata, "LINUX") == 0)
8900 return elfcore_grok_s390_vxrs_low (abfd, note);
8904 case NT_S390_VXRS_HIGH:
8905 if (note->namesz == 6
8906 && strcmp (note->namedata, "LINUX") == 0)
8907 return elfcore_grok_s390_vxrs_high (abfd, note);
8912 if (note->namesz == 6
8913 && strcmp (note->namedata, "LINUX") == 0)
8914 return elfcore_grok_arm_vfp (abfd, note);
8919 if (note->namesz == 6
8920 && strcmp (note->namedata, "LINUX") == 0)
8921 return elfcore_grok_aarch_tls (abfd, note);
8925 case NT_ARM_HW_BREAK:
8926 if (note->namesz == 6
8927 && strcmp (note->namedata, "LINUX") == 0)
8928 return elfcore_grok_aarch_hw_break (abfd, note);
8932 case NT_ARM_HW_WATCH:
8933 if (note->namesz == 6
8934 && strcmp (note->namedata, "LINUX") == 0)
8935 return elfcore_grok_aarch_hw_watch (abfd, note);
8941 if (bed->elf_backend_grok_psinfo)
8942 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8944 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8945 return elfcore_grok_psinfo (abfd, note);
8952 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8957 sect->size = note->descsz;
8958 sect->filepos = note->descpos;
8959 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8965 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8969 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8975 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8977 struct elf_obj_tdata *t;
8979 if (note->descsz == 0)
8982 t = elf_tdata (abfd);
8983 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
8984 if (t->build_id == NULL)
8987 t->build_id->size = note->descsz;
8988 memcpy (t->build_id->data, note->descdata, note->descsz);
8994 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
9001 case NT_GNU_BUILD_ID:
9002 return elfobj_grok_gnu_build_id (abfd, note);
9007 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
9009 struct sdt_note *cur =
9010 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
9013 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
9014 cur->size = (bfd_size_type) note->descsz;
9015 memcpy (cur->data, note->descdata, note->descsz);
9017 elf_tdata (abfd)->sdt_note_head = cur;
9023 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
9028 return elfobj_grok_stapsdt_note_1 (abfd, note);
9036 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
9040 cp = strchr (note->namedata, '@');
9043 *lwpidp = atoi(cp + 1);
9050 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
9052 /* Signal number at offset 0x08. */
9053 elf_tdata (abfd)->core->signal
9054 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
9056 /* Process ID at offset 0x50. */
9057 elf_tdata (abfd)->core->pid
9058 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
9060 /* Command name at 0x7c (max 32 bytes, including nul). */
9061 elf_tdata (abfd)->core->command
9062 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
9064 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
9069 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
9073 if (elfcore_netbsd_get_lwpid (note, &lwp))
9074 elf_tdata (abfd)->core->lwpid = lwp;
9076 if (note->type == NT_NETBSDCORE_PROCINFO)
9078 /* NetBSD-specific core "procinfo". Note that we expect to
9079 find this note before any of the others, which is fine,
9080 since the kernel writes this note out first when it
9081 creates a core file. */
9083 return elfcore_grok_netbsd_procinfo (abfd, note);
9086 /* As of Jan 2002 there are no other machine-independent notes
9087 defined for NetBSD core files. If the note type is less
9088 than the start of the machine-dependent note types, we don't
9091 if (note->type < NT_NETBSDCORE_FIRSTMACH)
9095 switch (bfd_get_arch (abfd))
9097 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9098 PT_GETFPREGS == mach+2. */
9100 case bfd_arch_alpha:
9101 case bfd_arch_sparc:
9104 case NT_NETBSDCORE_FIRSTMACH+0:
9105 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9107 case NT_NETBSDCORE_FIRSTMACH+2:
9108 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9114 /* On all other arch's, PT_GETREGS == mach+1 and
9115 PT_GETFPREGS == mach+3. */
9120 case NT_NETBSDCORE_FIRSTMACH+1:
9121 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9123 case NT_NETBSDCORE_FIRSTMACH+3:
9124 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9134 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
9136 /* Signal number at offset 0x08. */
9137 elf_tdata (abfd)->core->signal
9138 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
9140 /* Process ID at offset 0x20. */
9141 elf_tdata (abfd)->core->pid
9142 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
9144 /* Command name at 0x48 (max 32 bytes, including nul). */
9145 elf_tdata (abfd)->core->command
9146 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
9152 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
9154 if (note->type == NT_OPENBSD_PROCINFO)
9155 return elfcore_grok_openbsd_procinfo (abfd, note);
9157 if (note->type == NT_OPENBSD_REGS)
9158 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9160 if (note->type == NT_OPENBSD_FPREGS)
9161 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9163 if (note->type == NT_OPENBSD_XFPREGS)
9164 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9166 if (note->type == NT_OPENBSD_AUXV)
9168 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9173 sect->size = note->descsz;
9174 sect->filepos = note->descpos;
9175 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9180 if (note->type == NT_OPENBSD_WCOOKIE)
9182 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
9187 sect->size = note->descsz;
9188 sect->filepos = note->descpos;
9189 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9198 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
9200 void *ddata = note->descdata;
9207 /* nto_procfs_status 'pid' field is at offset 0. */
9208 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
9210 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9211 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
9213 /* nto_procfs_status 'flags' field is at offset 8. */
9214 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
9216 /* nto_procfs_status 'what' field is at offset 14. */
9217 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9219 elf_tdata (abfd)->core->signal = sig;
9220 elf_tdata (abfd)->core->lwpid = *tid;
9223 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9224 do not come from signals so we make sure we set the current
9225 thread just in case. */
9226 if (flags & 0x00000080)
9227 elf_tdata (abfd)->core->lwpid = *tid;
9229 /* Make a ".qnx_core_status/%d" section. */
9230 sprintf (buf, ".qnx_core_status/%ld", *tid);
9232 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9237 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9241 sect->size = note->descsz;
9242 sect->filepos = note->descpos;
9243 sect->alignment_power = 2;
9245 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9249 elfcore_grok_nto_regs (bfd *abfd,
9250 Elf_Internal_Note *note,
9258 /* Make a "(base)/%d" section. */
9259 sprintf (buf, "%s/%ld", base, tid);
9261 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9266 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9270 sect->size = note->descsz;
9271 sect->filepos = note->descpos;
9272 sect->alignment_power = 2;
9274 /* This is the current thread. */
9275 if (elf_tdata (abfd)->core->lwpid == tid)
9276 return elfcore_maybe_make_sect (abfd, base, sect);
9281 #define BFD_QNT_CORE_INFO 7
9282 #define BFD_QNT_CORE_STATUS 8
9283 #define BFD_QNT_CORE_GREG 9
9284 #define BFD_QNT_CORE_FPREG 10
9287 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9289 /* Every GREG section has a STATUS section before it. Store the
9290 tid from the previous call to pass down to the next gregs
9292 static long tid = 1;
9296 case BFD_QNT_CORE_INFO:
9297 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9298 case BFD_QNT_CORE_STATUS:
9299 return elfcore_grok_nto_status (abfd, note, &tid);
9300 case BFD_QNT_CORE_GREG:
9301 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9302 case BFD_QNT_CORE_FPREG:
9303 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9310 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9316 /* Use note name as section name. */
9318 name = (char *) bfd_alloc (abfd, len);
9321 memcpy (name, note->namedata, len);
9322 name[len - 1] = '\0';
9324 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9328 sect->size = note->descsz;
9329 sect->filepos = note->descpos;
9330 sect->alignment_power = 1;
9335 /* Function: elfcore_write_note
9338 buffer to hold note, and current size of buffer
9342 size of data for note
9344 Writes note to end of buffer. ELF64 notes are written exactly as
9345 for ELF32, despite the current (as of 2006) ELF gabi specifying
9346 that they ought to have 8-byte namesz and descsz field, and have
9347 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9350 Pointer to realloc'd buffer, *BUFSIZ updated. */
9353 elfcore_write_note (bfd *abfd,
9361 Elf_External_Note *xnp;
9368 namesz = strlen (name) + 1;
9370 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9372 buf = (char *) realloc (buf, *bufsiz + newspace);
9375 dest = buf + *bufsiz;
9376 *bufsiz += newspace;
9377 xnp = (Elf_External_Note *) dest;
9378 H_PUT_32 (abfd, namesz, xnp->namesz);
9379 H_PUT_32 (abfd, size, xnp->descsz);
9380 H_PUT_32 (abfd, type, xnp->type);
9384 memcpy (dest, name, namesz);
9392 memcpy (dest, input, size);
9403 elfcore_write_prpsinfo (bfd *abfd,
9409 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9411 if (bed->elf_backend_write_core_note != NULL)
9414 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9415 NT_PRPSINFO, fname, psargs);
9420 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9421 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9422 if (bed->s->elfclass == ELFCLASS32)
9424 #if defined (HAVE_PSINFO32_T)
9426 int note_type = NT_PSINFO;
9429 int note_type = NT_PRPSINFO;
9432 memset (&data, 0, sizeof (data));
9433 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9434 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9435 return elfcore_write_note (abfd, buf, bufsiz,
9436 "CORE", note_type, &data, sizeof (data));
9441 #if defined (HAVE_PSINFO_T)
9443 int note_type = NT_PSINFO;
9446 int note_type = NT_PRPSINFO;
9449 memset (&data, 0, sizeof (data));
9450 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9451 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9452 return elfcore_write_note (abfd, buf, bufsiz,
9453 "CORE", note_type, &data, sizeof (data));
9455 #endif /* PSINFO_T or PRPSINFO_T */
9462 elfcore_write_linux_prpsinfo32
9463 (bfd *abfd, char *buf, int *bufsiz,
9464 const struct elf_internal_linux_prpsinfo *prpsinfo)
9466 struct elf_external_linux_prpsinfo32 data;
9468 memset (&data, 0, sizeof (data));
9469 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9471 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9472 &data, sizeof (data));
9476 elfcore_write_linux_prpsinfo64
9477 (bfd *abfd, char *buf, int *bufsiz,
9478 const struct elf_internal_linux_prpsinfo *prpsinfo)
9480 struct elf_external_linux_prpsinfo64 data;
9482 memset (&data, 0, sizeof (data));
9483 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9485 return elfcore_write_note (abfd, buf, bufsiz,
9486 "CORE", NT_PRPSINFO, &data, sizeof (data));
9490 elfcore_write_prstatus (bfd *abfd,
9497 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9499 if (bed->elf_backend_write_core_note != NULL)
9502 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9504 pid, cursig, gregs);
9509 #if defined (HAVE_PRSTATUS_T)
9510 #if defined (HAVE_PRSTATUS32_T)
9511 if (bed->s->elfclass == ELFCLASS32)
9513 prstatus32_t prstat;
9515 memset (&prstat, 0, sizeof (prstat));
9516 prstat.pr_pid = pid;
9517 prstat.pr_cursig = cursig;
9518 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9519 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9520 NT_PRSTATUS, &prstat, sizeof (prstat));
9527 memset (&prstat, 0, sizeof (prstat));
9528 prstat.pr_pid = pid;
9529 prstat.pr_cursig = cursig;
9530 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9531 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9532 NT_PRSTATUS, &prstat, sizeof (prstat));
9534 #endif /* HAVE_PRSTATUS_T */
9540 #if defined (HAVE_LWPSTATUS_T)
9542 elfcore_write_lwpstatus (bfd *abfd,
9549 lwpstatus_t lwpstat;
9550 const char *note_name = "CORE";
9552 memset (&lwpstat, 0, sizeof (lwpstat));
9553 lwpstat.pr_lwpid = pid >> 16;
9554 lwpstat.pr_cursig = cursig;
9555 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9556 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9557 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9559 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9560 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9562 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9563 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9566 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9567 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9569 #endif /* HAVE_LWPSTATUS_T */
9571 #if defined (HAVE_PSTATUS_T)
9573 elfcore_write_pstatus (bfd *abfd,
9577 int cursig ATTRIBUTE_UNUSED,
9578 const void *gregs ATTRIBUTE_UNUSED)
9580 const char *note_name = "CORE";
9581 #if defined (HAVE_PSTATUS32_T)
9582 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9584 if (bed->s->elfclass == ELFCLASS32)
9588 memset (&pstat, 0, sizeof (pstat));
9589 pstat.pr_pid = pid & 0xffff;
9590 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9591 NT_PSTATUS, &pstat, sizeof (pstat));
9599 memset (&pstat, 0, sizeof (pstat));
9600 pstat.pr_pid = pid & 0xffff;
9601 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9602 NT_PSTATUS, &pstat, sizeof (pstat));
9606 #endif /* HAVE_PSTATUS_T */
9609 elfcore_write_prfpreg (bfd *abfd,
9615 const char *note_name = "CORE";
9616 return elfcore_write_note (abfd, buf, bufsiz,
9617 note_name, NT_FPREGSET, fpregs, size);
9621 elfcore_write_prxfpreg (bfd *abfd,
9624 const void *xfpregs,
9627 char *note_name = "LINUX";
9628 return elfcore_write_note (abfd, buf, bufsiz,
9629 note_name, NT_PRXFPREG, xfpregs, size);
9633 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9634 const void *xfpregs, int size)
9637 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
9638 note_name = "FreeBSD";
9640 note_name = "LINUX";
9641 return elfcore_write_note (abfd, buf, bufsiz,
9642 note_name, NT_X86_XSTATE, xfpregs, size);
9646 elfcore_write_ppc_vmx (bfd *abfd,
9649 const void *ppc_vmx,
9652 char *note_name = "LINUX";
9653 return elfcore_write_note (abfd, buf, bufsiz,
9654 note_name, NT_PPC_VMX, ppc_vmx, size);
9658 elfcore_write_ppc_vsx (bfd *abfd,
9661 const void *ppc_vsx,
9664 char *note_name = "LINUX";
9665 return elfcore_write_note (abfd, buf, bufsiz,
9666 note_name, NT_PPC_VSX, ppc_vsx, size);
9670 elfcore_write_s390_high_gprs (bfd *abfd,
9673 const void *s390_high_gprs,
9676 char *note_name = "LINUX";
9677 return elfcore_write_note (abfd, buf, bufsiz,
9678 note_name, NT_S390_HIGH_GPRS,
9679 s390_high_gprs, size);
9683 elfcore_write_s390_timer (bfd *abfd,
9686 const void *s390_timer,
9689 char *note_name = "LINUX";
9690 return elfcore_write_note (abfd, buf, bufsiz,
9691 note_name, NT_S390_TIMER, s390_timer, size);
9695 elfcore_write_s390_todcmp (bfd *abfd,
9698 const void *s390_todcmp,
9701 char *note_name = "LINUX";
9702 return elfcore_write_note (abfd, buf, bufsiz,
9703 note_name, NT_S390_TODCMP, s390_todcmp, size);
9707 elfcore_write_s390_todpreg (bfd *abfd,
9710 const void *s390_todpreg,
9713 char *note_name = "LINUX";
9714 return elfcore_write_note (abfd, buf, bufsiz,
9715 note_name, NT_S390_TODPREG, s390_todpreg, size);
9719 elfcore_write_s390_ctrs (bfd *abfd,
9722 const void *s390_ctrs,
9725 char *note_name = "LINUX";
9726 return elfcore_write_note (abfd, buf, bufsiz,
9727 note_name, NT_S390_CTRS, s390_ctrs, size);
9731 elfcore_write_s390_prefix (bfd *abfd,
9734 const void *s390_prefix,
9737 char *note_name = "LINUX";
9738 return elfcore_write_note (abfd, buf, bufsiz,
9739 note_name, NT_S390_PREFIX, s390_prefix, size);
9743 elfcore_write_s390_last_break (bfd *abfd,
9746 const void *s390_last_break,
9749 char *note_name = "LINUX";
9750 return elfcore_write_note (abfd, buf, bufsiz,
9751 note_name, NT_S390_LAST_BREAK,
9752 s390_last_break, size);
9756 elfcore_write_s390_system_call (bfd *abfd,
9759 const void *s390_system_call,
9762 char *note_name = "LINUX";
9763 return elfcore_write_note (abfd, buf, bufsiz,
9764 note_name, NT_S390_SYSTEM_CALL,
9765 s390_system_call, size);
9769 elfcore_write_s390_tdb (bfd *abfd,
9772 const void *s390_tdb,
9775 char *note_name = "LINUX";
9776 return elfcore_write_note (abfd, buf, bufsiz,
9777 note_name, NT_S390_TDB, s390_tdb, size);
9781 elfcore_write_s390_vxrs_low (bfd *abfd,
9784 const void *s390_vxrs_low,
9787 char *note_name = "LINUX";
9788 return elfcore_write_note (abfd, buf, bufsiz,
9789 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
9793 elfcore_write_s390_vxrs_high (bfd *abfd,
9796 const void *s390_vxrs_high,
9799 char *note_name = "LINUX";
9800 return elfcore_write_note (abfd, buf, bufsiz,
9801 note_name, NT_S390_VXRS_HIGH,
9802 s390_vxrs_high, size);
9806 elfcore_write_arm_vfp (bfd *abfd,
9809 const void *arm_vfp,
9812 char *note_name = "LINUX";
9813 return elfcore_write_note (abfd, buf, bufsiz,
9814 note_name, NT_ARM_VFP, arm_vfp, size);
9818 elfcore_write_aarch_tls (bfd *abfd,
9821 const void *aarch_tls,
9824 char *note_name = "LINUX";
9825 return elfcore_write_note (abfd, buf, bufsiz,
9826 note_name, NT_ARM_TLS, aarch_tls, size);
9830 elfcore_write_aarch_hw_break (bfd *abfd,
9833 const void *aarch_hw_break,
9836 char *note_name = "LINUX";
9837 return elfcore_write_note (abfd, buf, bufsiz,
9838 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9842 elfcore_write_aarch_hw_watch (bfd *abfd,
9845 const void *aarch_hw_watch,
9848 char *note_name = "LINUX";
9849 return elfcore_write_note (abfd, buf, bufsiz,
9850 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9854 elfcore_write_register_note (bfd *abfd,
9857 const char *section,
9861 if (strcmp (section, ".reg2") == 0)
9862 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9863 if (strcmp (section, ".reg-xfp") == 0)
9864 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9865 if (strcmp (section, ".reg-xstate") == 0)
9866 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9867 if (strcmp (section, ".reg-ppc-vmx") == 0)
9868 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9869 if (strcmp (section, ".reg-ppc-vsx") == 0)
9870 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9871 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9872 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9873 if (strcmp (section, ".reg-s390-timer") == 0)
9874 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9875 if (strcmp (section, ".reg-s390-todcmp") == 0)
9876 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9877 if (strcmp (section, ".reg-s390-todpreg") == 0)
9878 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9879 if (strcmp (section, ".reg-s390-ctrs") == 0)
9880 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9881 if (strcmp (section, ".reg-s390-prefix") == 0)
9882 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9883 if (strcmp (section, ".reg-s390-last-break") == 0)
9884 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9885 if (strcmp (section, ".reg-s390-system-call") == 0)
9886 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9887 if (strcmp (section, ".reg-s390-tdb") == 0)
9888 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
9889 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
9890 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
9891 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
9892 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
9893 if (strcmp (section, ".reg-arm-vfp") == 0)
9894 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9895 if (strcmp (section, ".reg-aarch-tls") == 0)
9896 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9897 if (strcmp (section, ".reg-aarch-hw-break") == 0)
9898 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9899 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9900 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9905 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9910 while (p < buf + size)
9912 /* FIXME: bad alignment assumption. */
9913 Elf_External_Note *xnp = (Elf_External_Note *) p;
9914 Elf_Internal_Note in;
9916 if (offsetof (Elf_External_Note, name) > buf - p + size)
9919 in.type = H_GET_32 (abfd, xnp->type);
9921 in.namesz = H_GET_32 (abfd, xnp->namesz);
9922 in.namedata = xnp->name;
9923 if (in.namesz > buf - in.namedata + size)
9926 in.descsz = H_GET_32 (abfd, xnp->descsz);
9927 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9928 in.descpos = offset + (in.descdata - buf);
9930 && (in.descdata >= buf + size
9931 || in.descsz > buf - in.descdata + size))
9934 switch (bfd_get_format (abfd))
9941 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
9944 const char * string;
9946 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
9950 GROKER_ELEMENT ("", elfcore_grok_note),
9951 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
9952 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
9953 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
9954 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
9956 #undef GROKER_ELEMENT
9959 for (i = ARRAY_SIZE (grokers); i--;)
9961 if (in.namesz >= grokers[i].len
9962 && strncmp (in.namedata, grokers[i].string,
9963 grokers[i].len) == 0)
9965 if (! grokers[i].func (abfd, & in))
9974 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9976 if (! elfobj_grok_gnu_note (abfd, &in))
9979 else if (in.namesz == sizeof "stapsdt"
9980 && strcmp (in.namedata, "stapsdt") == 0)
9982 if (! elfobj_grok_stapsdt_note (abfd, &in))
9988 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9995 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
10002 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
10005 buf = (char *) bfd_malloc (size + 1);
10009 /* PR 17512: file: ec08f814
10010 0-termintate the buffer so that string searches will not overflow. */
10013 if (bfd_bread (buf, size, abfd) != size
10014 || !elf_parse_notes (abfd, buf, size, offset))
10024 /* Providing external access to the ELF program header table. */
10026 /* Return an upper bound on the number of bytes required to store a
10027 copy of ABFD's program header table entries. Return -1 if an error
10028 occurs; bfd_get_error will return an appropriate code. */
10031 bfd_get_elf_phdr_upper_bound (bfd *abfd)
10033 if (abfd->xvec->flavour != bfd_target_elf_flavour)
10035 bfd_set_error (bfd_error_wrong_format);
10039 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
10042 /* Copy ABFD's program header table entries to *PHDRS. The entries
10043 will be stored as an array of Elf_Internal_Phdr structures, as
10044 defined in include/elf/internal.h. To find out how large the
10045 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10047 Return the number of program header table entries read, or -1 if an
10048 error occurs; bfd_get_error will return an appropriate code. */
10051 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
10055 if (abfd->xvec->flavour != bfd_target_elf_flavour)
10057 bfd_set_error (bfd_error_wrong_format);
10061 num_phdrs = elf_elfheader (abfd)->e_phnum;
10062 memcpy (phdrs, elf_tdata (abfd)->phdr,
10063 num_phdrs * sizeof (Elf_Internal_Phdr));
10068 enum elf_reloc_type_class
10069 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
10070 const asection *rel_sec ATTRIBUTE_UNUSED,
10071 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
10073 return reloc_class_normal;
10076 /* For RELA architectures, return the relocation value for a
10077 relocation against a local symbol. */
10080 _bfd_elf_rela_local_sym (bfd *abfd,
10081 Elf_Internal_Sym *sym,
10083 Elf_Internal_Rela *rel)
10085 asection *sec = *psec;
10086 bfd_vma relocation;
10088 relocation = (sec->output_section->vma
10089 + sec->output_offset
10091 if ((sec->flags & SEC_MERGE)
10092 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
10093 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
10096 _bfd_merged_section_offset (abfd, psec,
10097 elf_section_data (sec)->sec_info,
10098 sym->st_value + rel->r_addend);
10101 /* If we have changed the section, and our original section is
10102 marked with SEC_EXCLUDE, it means that the original
10103 SEC_MERGE section has been completely subsumed in some
10104 other SEC_MERGE section. In this case, we need to leave
10105 some info around for --emit-relocs. */
10106 if ((sec->flags & SEC_EXCLUDE) != 0)
10107 sec->kept_section = *psec;
10110 rel->r_addend -= relocation;
10111 rel->r_addend += sec->output_section->vma + sec->output_offset;
10117 _bfd_elf_rel_local_sym (bfd *abfd,
10118 Elf_Internal_Sym *sym,
10122 asection *sec = *psec;
10124 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
10125 return sym->st_value + addend;
10127 return _bfd_merged_section_offset (abfd, psec,
10128 elf_section_data (sec)->sec_info,
10129 sym->st_value + addend);
10133 _bfd_elf_section_offset (bfd *abfd,
10134 struct bfd_link_info *info,
10138 switch (sec->sec_info_type)
10140 case SEC_INFO_TYPE_STABS:
10141 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
10143 case SEC_INFO_TYPE_EH_FRAME:
10144 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
10146 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
10148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10149 bfd_size_type address_size = bed->s->arch_size / 8;
10150 offset = sec->size - offset - address_size;
10156 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10157 reconstruct an ELF file by reading the segments out of remote memory
10158 based on the ELF file header at EHDR_VMA and the ELF program headers it
10159 points to. If not null, *LOADBASEP is filled in with the difference
10160 between the VMAs from which the segments were read, and the VMAs the
10161 file headers (and hence BFD's idea of each section's VMA) put them at.
10163 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10164 remote memory at target address VMA into the local buffer at MYADDR; it
10165 should return zero on success or an `errno' code on failure. TEMPL must
10166 be a BFD for an ELF target with the word size and byte order found in
10167 the remote memory. */
10170 bfd_elf_bfd_from_remote_memory
10173 bfd_size_type size,
10174 bfd_vma *loadbasep,
10175 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
10177 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
10178 (templ, ehdr_vma, size, loadbasep, target_read_memory);
10182 _bfd_elf_get_synthetic_symtab (bfd *abfd,
10183 long symcount ATTRIBUTE_UNUSED,
10184 asymbol **syms ATTRIBUTE_UNUSED,
10189 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10192 const char *relplt_name;
10193 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
10197 Elf_Internal_Shdr *hdr;
10203 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
10206 if (dynsymcount <= 0)
10209 if (!bed->plt_sym_val)
10212 relplt_name = bed->relplt_name;
10213 if (relplt_name == NULL)
10214 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
10215 relplt = bfd_get_section_by_name (abfd, relplt_name);
10216 if (relplt == NULL)
10219 hdr = &elf_section_data (relplt)->this_hdr;
10220 if (hdr->sh_link != elf_dynsymtab (abfd)
10221 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
10224 plt = bfd_get_section_by_name (abfd, ".plt");
10228 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
10229 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
10232 count = relplt->size / hdr->sh_entsize;
10233 size = count * sizeof (asymbol);
10234 p = relplt->relocation;
10235 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10237 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
10238 if (p->addend != 0)
10241 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
10243 size += sizeof ("+0x") - 1 + 8;
10248 s = *ret = (asymbol *) bfd_malloc (size);
10252 names = (char *) (s + count);
10253 p = relplt->relocation;
10255 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10260 addr = bed->plt_sym_val (i, plt, p);
10261 if (addr == (bfd_vma) -1)
10264 *s = **p->sym_ptr_ptr;
10265 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10266 we are defining a symbol, ensure one of them is set. */
10267 if ((s->flags & BSF_LOCAL) == 0)
10268 s->flags |= BSF_GLOBAL;
10269 s->flags |= BSF_SYNTHETIC;
10271 s->value = addr - plt->vma;
10274 len = strlen ((*p->sym_ptr_ptr)->name);
10275 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10277 if (p->addend != 0)
10281 memcpy (names, "+0x", sizeof ("+0x") - 1);
10282 names += sizeof ("+0x") - 1;
10283 bfd_sprintf_vma (abfd, buf, p->addend);
10284 for (a = buf; *a == '0'; ++a)
10287 memcpy (names, a, len);
10290 memcpy (names, "@plt", sizeof ("@plt"));
10291 names += sizeof ("@plt");
10298 /* It is only used by x86-64 so far. */
10299 asection _bfd_elf_large_com_section
10300 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10301 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10304 _bfd_elf_post_process_headers (bfd * abfd,
10305 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10307 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10309 i_ehdrp = elf_elfheader (abfd);
10311 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10313 /* To make things simpler for the loader on Linux systems we set the
10314 osabi field to ELFOSABI_GNU if the binary contains symbols of
10315 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10316 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10317 && elf_tdata (abfd)->has_gnu_symbols)
10318 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10322 /* Return TRUE for ELF symbol types that represent functions.
10323 This is the default version of this function, which is sufficient for
10324 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10327 _bfd_elf_is_function_type (unsigned int type)
10329 return (type == STT_FUNC
10330 || type == STT_GNU_IFUNC);
10333 /* If the ELF symbol SYM might be a function in SEC, return the
10334 function size and set *CODE_OFF to the function's entry point,
10335 otherwise return zero. */
10338 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10341 bfd_size_type size;
10343 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10344 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10345 || sym->section != sec)
10348 *code_off = sym->value;
10350 if (!(sym->flags & BSF_SYNTHETIC))
10351 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;