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 elf_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 (elf_symtab_shndx_list (ibfd) != NULL)
405 elf_section_list * entry;
406 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
408 /* Find an index section that is linked to this symtab section. */
409 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
410 if (sections[entry->hdr.sh_link] == symtab_hdr)
412 shndx_hdr = & entry->hdr;
416 if (shndx_hdr == NULL)
418 if (symtab_hdr == & elf_symtab_hdr (ibfd))
419 /* Not really accurate, but this was how the old code used to work. */
420 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
421 /* Otherwise we do nothing. The assumption is that
422 the index table will not be needed. */
426 /* Read the symbols. */
428 alloc_extshndx = NULL;
430 bed = get_elf_backend_data (ibfd);
431 extsym_size = bed->s->sizeof_sym;
432 amt = symcount * extsym_size;
433 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
434 if (extsym_buf == NULL)
436 alloc_ext = bfd_malloc2 (symcount, extsym_size);
437 extsym_buf = alloc_ext;
439 if (extsym_buf == NULL
440 || bfd_seek (ibfd, pos, SEEK_SET) != 0
441 || bfd_bread (extsym_buf, amt, ibfd) != amt)
447 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
451 amt = symcount * sizeof (Elf_External_Sym_Shndx);
452 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
453 if (extshndx_buf == NULL)
455 alloc_extshndx = (Elf_External_Sym_Shndx *)
456 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
457 extshndx_buf = alloc_extshndx;
459 if (extshndx_buf == NULL
460 || bfd_seek (ibfd, pos, SEEK_SET) != 0
461 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
468 if (intsym_buf == NULL)
470 alloc_intsym = (Elf_Internal_Sym *)
471 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
472 intsym_buf = alloc_intsym;
473 if (intsym_buf == NULL)
477 /* Convert the symbols to internal form. */
478 isymend = intsym_buf + symcount;
479 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
480 shndx = extshndx_buf;
482 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
483 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
485 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
486 (*_bfd_error_handler) (_("%B symbol number %lu references "
487 "nonexistent SHT_SYMTAB_SHNDX section"),
488 ibfd, (unsigned long) symoffset);
489 if (alloc_intsym != NULL)
496 if (alloc_ext != NULL)
498 if (alloc_extshndx != NULL)
499 free (alloc_extshndx);
504 /* Look up a symbol name. */
506 bfd_elf_sym_name (bfd *abfd,
507 Elf_Internal_Shdr *symtab_hdr,
508 Elf_Internal_Sym *isym,
512 unsigned int iname = isym->st_name;
513 unsigned int shindex = symtab_hdr->sh_link;
515 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
516 /* Check for a bogus st_shndx to avoid crashing. */
517 && isym->st_shndx < elf_numsections (abfd))
519 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
520 shindex = elf_elfheader (abfd)->e_shstrndx;
523 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
526 else if (sym_sec && *name == '\0')
527 name = bfd_section_name (abfd, sym_sec);
532 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
533 sections. The first element is the flags, the rest are section
536 typedef union elf_internal_group {
537 Elf_Internal_Shdr *shdr;
539 } Elf_Internal_Group;
541 /* Return the name of the group signature symbol. Why isn't the
542 signature just a string? */
545 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
547 Elf_Internal_Shdr *hdr;
548 unsigned char esym[sizeof (Elf64_External_Sym)];
549 Elf_External_Sym_Shndx eshndx;
550 Elf_Internal_Sym isym;
552 /* First we need to ensure the symbol table is available. Make sure
553 that it is a symbol table section. */
554 if (ghdr->sh_link >= elf_numsections (abfd))
556 hdr = elf_elfsections (abfd) [ghdr->sh_link];
557 if (hdr->sh_type != SHT_SYMTAB
558 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
561 /* Go read the symbol. */
562 hdr = &elf_tdata (abfd)->symtab_hdr;
563 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
564 &isym, esym, &eshndx) == NULL)
567 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
570 /* Set next_in_group list pointer, and group name for NEWSECT. */
573 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
575 unsigned int num_group = elf_tdata (abfd)->num_group;
577 /* If num_group is zero, read in all SHT_GROUP sections. The count
578 is set to -1 if there are no SHT_GROUP sections. */
581 unsigned int i, shnum;
583 /* First count the number of groups. If we have a SHT_GROUP
584 section with just a flag word (ie. sh_size is 4), ignore it. */
585 shnum = elf_numsections (abfd);
588 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
589 ( (shdr)->sh_type == SHT_GROUP \
590 && (shdr)->sh_size >= minsize \
591 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
592 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
594 for (i = 0; i < shnum; i++)
596 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
598 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
604 num_group = (unsigned) -1;
605 elf_tdata (abfd)->num_group = num_group;
609 /* We keep a list of elf section headers for group sections,
610 so we can find them quickly. */
613 elf_tdata (abfd)->num_group = num_group;
614 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
615 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
616 if (elf_tdata (abfd)->group_sect_ptr == NULL)
620 for (i = 0; i < shnum; i++)
622 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
624 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
627 Elf_Internal_Group *dest;
629 /* Add to list of sections. */
630 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
633 /* Read the raw contents. */
634 BFD_ASSERT (sizeof (*dest) >= 4);
635 amt = shdr->sh_size * sizeof (*dest) / 4;
636 shdr->contents = (unsigned char *)
637 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
638 /* PR binutils/4110: Handle corrupt group headers. */
639 if (shdr->contents == NULL)
642 (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
643 bfd_set_error (bfd_error_bad_value);
648 memset (shdr->contents, 0, amt);
650 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
651 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
655 (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size);
656 bfd_set_error (bfd_error_bad_value);
658 /* PR 17510: If the group contents are even partially
659 corrupt, do not allow any of the contents to be used. */
660 memset (shdr->contents, 0, amt);
664 /* Translate raw contents, a flag word followed by an
665 array of elf section indices all in target byte order,
666 to the flag word followed by an array of elf section
668 src = shdr->contents + shdr->sh_size;
669 dest = (Elf_Internal_Group *) (shdr->contents + amt);
677 idx = H_GET_32 (abfd, src);
678 if (src == shdr->contents)
681 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
682 shdr->bfd_section->flags
683 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
688 ((*_bfd_error_handler)
689 (_("%B: invalid SHT_GROUP entry"), abfd));
692 dest->shdr = elf_elfsections (abfd)[idx];
697 /* PR 17510: Corrupt binaries might contain invalid groups. */
698 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
700 elf_tdata (abfd)->num_group = num_group;
702 /* If all groups are invalid then fail. */
705 elf_tdata (abfd)->group_sect_ptr = NULL;
706 elf_tdata (abfd)->num_group = num_group = -1;
707 (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd);
708 bfd_set_error (bfd_error_bad_value);
714 if (num_group != (unsigned) -1)
718 for (i = 0; i < num_group; i++)
720 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
721 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
722 unsigned int n_elt = shdr->sh_size / 4;
724 /* Look through this group's sections to see if current
725 section is a member. */
727 if ((++idx)->shdr == hdr)
731 /* We are a member of this group. Go looking through
732 other members to see if any others are linked via
734 idx = (Elf_Internal_Group *) shdr->contents;
735 n_elt = shdr->sh_size / 4;
737 if ((s = (++idx)->shdr->bfd_section) != NULL
738 && elf_next_in_group (s) != NULL)
742 /* Snarf the group name from other member, and
743 insert current section in circular list. */
744 elf_group_name (newsect) = elf_group_name (s);
745 elf_next_in_group (newsect) = elf_next_in_group (s);
746 elf_next_in_group (s) = newsect;
752 gname = group_signature (abfd, shdr);
755 elf_group_name (newsect) = gname;
757 /* Start a circular list with one element. */
758 elf_next_in_group (newsect) = newsect;
761 /* If the group section has been created, point to the
763 if (shdr->bfd_section != NULL)
764 elf_next_in_group (shdr->bfd_section) = newsect;
772 if (elf_group_name (newsect) == NULL)
774 (*_bfd_error_handler) (_("%B: no group info for section %A"),
782 _bfd_elf_setup_sections (bfd *abfd)
785 unsigned int num_group = elf_tdata (abfd)->num_group;
786 bfd_boolean result = TRUE;
789 /* Process SHF_LINK_ORDER. */
790 for (s = abfd->sections; s != NULL; s = s->next)
792 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
793 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
795 unsigned int elfsec = this_hdr->sh_link;
796 /* FIXME: The old Intel compiler and old strip/objcopy may
797 not set the sh_link or sh_info fields. Hence we could
798 get the situation where elfsec is 0. */
801 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
802 if (bed->link_order_error_handler)
803 bed->link_order_error_handler
804 (_("%B: warning: sh_link not set for section `%A'"),
809 asection *linksec = NULL;
811 if (elfsec < elf_numsections (abfd))
813 this_hdr = elf_elfsections (abfd)[elfsec];
814 linksec = this_hdr->bfd_section;
818 Some strip/objcopy may leave an incorrect value in
819 sh_link. We don't want to proceed. */
822 (*_bfd_error_handler)
823 (_("%B: sh_link [%d] in section `%A' is incorrect"),
824 s->owner, s, elfsec);
828 elf_linked_to_section (s) = linksec;
833 /* Process section groups. */
834 if (num_group == (unsigned) -1)
837 for (i = 0; i < num_group; i++)
839 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
840 Elf_Internal_Group *idx;
843 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
844 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
846 (*_bfd_error_handler)
847 (_("%B: section group entry number %u is corrupt"),
853 idx = (Elf_Internal_Group *) shdr->contents;
854 n_elt = shdr->sh_size / 4;
857 if ((++idx)->shdr->bfd_section)
858 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
859 else if (idx->shdr->sh_type == SHT_RELA
860 || idx->shdr->sh_type == SHT_REL)
861 /* We won't include relocation sections in section groups in
862 output object files. We adjust the group section size here
863 so that relocatable link will work correctly when
864 relocation sections are in section group in input object
866 shdr->bfd_section->size -= 4;
869 /* There are some unknown sections in the group. */
870 (*_bfd_error_handler)
871 (_("%B: unknown [%d] section `%s' in group [%s]"),
873 (unsigned int) idx->shdr->sh_type,
874 bfd_elf_string_from_elf_section (abfd,
875 (elf_elfheader (abfd)
878 shdr->bfd_section->name);
886 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
888 return elf_next_in_group (sec) != NULL;
892 convert_debug_to_zdebug (bfd *abfd, const char *name)
894 unsigned int len = strlen (name);
895 char *new_name = bfd_alloc (abfd, len + 2);
896 if (new_name == NULL)
900 memcpy (new_name + 2, name + 1, len);
905 convert_zdebug_to_debug (bfd *abfd, const char *name)
907 unsigned int len = strlen (name);
908 char *new_name = bfd_alloc (abfd, len);
909 if (new_name == NULL)
912 memcpy (new_name + 1, name + 2, len - 1);
916 /* Make a BFD section from an ELF section. We store a pointer to the
917 BFD section in the bfd_section field of the header. */
920 _bfd_elf_make_section_from_shdr (bfd *abfd,
921 Elf_Internal_Shdr *hdr,
927 const struct elf_backend_data *bed;
929 if (hdr->bfd_section != NULL)
932 newsect = bfd_make_section_anyway (abfd, name);
936 hdr->bfd_section = newsect;
937 elf_section_data (newsect)->this_hdr = *hdr;
938 elf_section_data (newsect)->this_idx = shindex;
940 /* Always use the real type/flags. */
941 elf_section_type (newsect) = hdr->sh_type;
942 elf_section_flags (newsect) = hdr->sh_flags;
944 newsect->filepos = hdr->sh_offset;
946 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
947 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
948 || ! bfd_set_section_alignment (abfd, newsect,
949 bfd_log2 (hdr->sh_addralign)))
952 flags = SEC_NO_FLAGS;
953 if (hdr->sh_type != SHT_NOBITS)
954 flags |= SEC_HAS_CONTENTS;
955 if (hdr->sh_type == SHT_GROUP)
956 flags |= SEC_GROUP | SEC_EXCLUDE;
957 if ((hdr->sh_flags & SHF_ALLOC) != 0)
960 if (hdr->sh_type != SHT_NOBITS)
963 if ((hdr->sh_flags & SHF_WRITE) == 0)
964 flags |= SEC_READONLY;
965 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
967 else if ((flags & SEC_LOAD) != 0)
969 if ((hdr->sh_flags & SHF_MERGE) != 0)
972 newsect->entsize = hdr->sh_entsize;
973 if ((hdr->sh_flags & SHF_STRINGS) != 0)
974 flags |= SEC_STRINGS;
976 if (hdr->sh_flags & SHF_GROUP)
977 if (!setup_group (abfd, hdr, newsect))
979 if ((hdr->sh_flags & SHF_TLS) != 0)
980 flags |= SEC_THREAD_LOCAL;
981 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
982 flags |= SEC_EXCLUDE;
984 if ((flags & SEC_ALLOC) == 0)
986 /* The debugging sections appear to be recognized only by name,
987 not any sort of flag. Their SEC_ALLOC bits are cleared. */
994 else if (name[1] == 'g' && name[2] == 'n')
995 p = ".gnu.linkonce.wi.", n = 17;
996 else if (name[1] == 'g' && name[2] == 'd')
997 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
998 else if (name[1] == 'l')
1000 else if (name[1] == 's')
1002 else if (name[1] == 'z')
1003 p = ".zdebug", n = 7;
1006 if (p != NULL && strncmp (name, p, n) == 0)
1007 flags |= SEC_DEBUGGING;
1011 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1012 only link a single copy of the section. This is used to support
1013 g++. g++ will emit each template expansion in its own section.
1014 The symbols will be defined as weak, so that multiple definitions
1015 are permitted. The GNU linker extension is to actually discard
1016 all but one of the sections. */
1017 if (CONST_STRNEQ (name, ".gnu.linkonce")
1018 && elf_next_in_group (newsect) == NULL)
1019 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1021 bed = get_elf_backend_data (abfd);
1022 if (bed->elf_backend_section_flags)
1023 if (! bed->elf_backend_section_flags (&flags, hdr))
1026 if (! bfd_set_section_flags (abfd, newsect, flags))
1029 /* We do not parse the PT_NOTE segments as we are interested even in the
1030 separate debug info files which may have the segments offsets corrupted.
1031 PT_NOTEs from the core files are currently not parsed using BFD. */
1032 if (hdr->sh_type == SHT_NOTE)
1036 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1039 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
1043 if ((flags & SEC_ALLOC) != 0)
1045 Elf_Internal_Phdr *phdr;
1046 unsigned int i, nload;
1048 /* Some ELF linkers produce binaries with all the program header
1049 p_paddr fields zero. If we have such a binary with more than
1050 one PT_LOAD header, then leave the section lma equal to vma
1051 so that we don't create sections with overlapping lma. */
1052 phdr = elf_tdata (abfd)->phdr;
1053 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1054 if (phdr->p_paddr != 0)
1056 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1058 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1061 phdr = elf_tdata (abfd)->phdr;
1062 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1064 if (((phdr->p_type == PT_LOAD
1065 && (hdr->sh_flags & SHF_TLS) == 0)
1066 || phdr->p_type == PT_TLS)
1067 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1069 if ((flags & SEC_LOAD) == 0)
1070 newsect->lma = (phdr->p_paddr
1071 + hdr->sh_addr - phdr->p_vaddr);
1073 /* We used to use the same adjustment for SEC_LOAD
1074 sections, but that doesn't work if the segment
1075 is packed with code from multiple VMAs.
1076 Instead we calculate the section LMA based on
1077 the segment LMA. It is assumed that the
1078 segment will contain sections with contiguous
1079 LMAs, even if the VMAs are not. */
1080 newsect->lma = (phdr->p_paddr
1081 + hdr->sh_offset - phdr->p_offset);
1083 /* With contiguous segments, we can't tell from file
1084 offsets whether a section with zero size should
1085 be placed at the end of one segment or the
1086 beginning of the next. Decide based on vaddr. */
1087 if (hdr->sh_addr >= phdr->p_vaddr
1088 && (hdr->sh_addr + hdr->sh_size
1089 <= phdr->p_vaddr + phdr->p_memsz))
1095 /* Compress/decompress DWARF debug sections with names: .debug_* and
1096 .zdebug_*, after the section flags is set. */
1097 if ((flags & SEC_DEBUGGING)
1098 && ((name[1] == 'd' && name[6] == '_')
1099 || (name[1] == 'z' && name[7] == '_')))
1101 enum { nothing, compress, decompress } action = nothing;
1102 int compression_header_size;
1103 bfd_size_type uncompressed_size;
1104 bfd_boolean compressed
1105 = bfd_is_section_compressed_with_header (abfd, newsect,
1106 &compression_header_size,
1107 &uncompressed_size);
1111 /* Compressed section. Check if we should decompress. */
1112 if ((abfd->flags & BFD_DECOMPRESS))
1113 action = decompress;
1116 /* Compress the uncompressed section or convert from/to .zdebug*
1117 section. Check if we should compress. */
1118 if (action == nothing)
1120 if (newsect->size != 0
1121 && (abfd->flags & BFD_COMPRESS)
1122 && compression_header_size >= 0
1123 && uncompressed_size > 0
1125 || ((compression_header_size > 0)
1126 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1132 if (action == compress)
1134 if (!bfd_init_section_compress_status (abfd, newsect))
1136 (*_bfd_error_handler)
1137 (_("%B: unable to initialize compress status for section %s"),
1144 if (!bfd_init_section_decompress_status (abfd, newsect))
1146 (*_bfd_error_handler)
1147 (_("%B: unable to initialize decompress status for section %s"),
1153 if (abfd->is_linker_input)
1156 && (action == decompress
1157 || (action == compress
1158 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1160 /* Convert section name from .zdebug_* to .debug_* so
1161 that linker will consider this section as a debug
1163 char *new_name = convert_zdebug_to_debug (abfd, name);
1164 if (new_name == NULL)
1166 bfd_rename_section (abfd, newsect, new_name);
1170 /* For objdump, don't rename the section. For objcopy, delay
1171 section rename to elf_fake_sections. */
1172 newsect->flags |= SEC_ELF_RENAME;
1178 const char *const bfd_elf_section_type_names[] = {
1179 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1180 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1181 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1184 /* ELF relocs are against symbols. If we are producing relocatable
1185 output, and the reloc is against an external symbol, and nothing
1186 has given us any additional addend, the resulting reloc will also
1187 be against the same symbol. In such a case, we don't want to
1188 change anything about the way the reloc is handled, since it will
1189 all be done at final link time. Rather than put special case code
1190 into bfd_perform_relocation, all the reloc types use this howto
1191 function. It just short circuits the reloc if producing
1192 relocatable output against an external symbol. */
1194 bfd_reloc_status_type
1195 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1196 arelent *reloc_entry,
1198 void *data ATTRIBUTE_UNUSED,
1199 asection *input_section,
1201 char **error_message ATTRIBUTE_UNUSED)
1203 if (output_bfd != NULL
1204 && (symbol->flags & BSF_SECTION_SYM) == 0
1205 && (! reloc_entry->howto->partial_inplace
1206 || reloc_entry->addend == 0))
1208 reloc_entry->address += input_section->output_offset;
1209 return bfd_reloc_ok;
1212 return bfd_reloc_continue;
1215 /* Copy the program header and other data from one object module to
1219 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1221 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1222 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1225 if (!elf_flags_init (obfd))
1227 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1228 elf_flags_init (obfd) = TRUE;
1231 elf_gp (obfd) = elf_gp (ibfd);
1233 /* Also copy the EI_OSABI field. */
1234 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1235 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1237 /* Copy object attributes. */
1238 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1240 /* This is an feature for objcopy --only-keep-debug: When a section's type
1241 is changed to NOBITS, we preserve the sh_link and sh_info fields so that
1242 they can be matched up with the original. */
1243 Elf_Internal_Shdr ** iheaders = elf_elfsections (ibfd);
1244 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1246 if (iheaders != NULL && oheaders != NULL)
1250 for (i = 0; i < elf_numsections (obfd); i++)
1253 Elf_Internal_Shdr * oheader = oheaders[i];
1256 || oheader->sh_type != SHT_NOBITS
1257 || oheader->sh_size == 0
1258 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1261 /* Scan for the matching section in the input bfd.
1262 FIXME: We could use something better than a linear scan here.
1263 Unfortunately we cannot compare names as the output string table
1264 is empty, so instead we check size, address and type. */
1265 for (j = 0; j < elf_numsections (ibfd); j++)
1267 Elf_Internal_Shdr * iheader = iheaders[j];
1269 /* Since --only-keep-debug turns all non-debug sections
1270 into SHT_NOBITS sections, the output SHT_NOBITS type
1271 matches any input type. */
1272 if ((oheader->sh_type == SHT_NOBITS
1273 || iheader->sh_type == oheader->sh_type)
1274 && iheader->sh_flags == oheader->sh_flags
1275 && iheader->sh_addralign == oheader->sh_addralign
1276 && iheader->sh_entsize == oheader->sh_entsize
1277 && iheader->sh_size == oheader->sh_size
1278 && iheader->sh_addr == oheader->sh_addr
1279 && (iheader->sh_info != oheader->sh_info
1280 || iheader->sh_link != oheader->sh_link))
1282 /* Note: Strictly speaking these assignments are wrong.
1283 The sh_link and sh_info fields should point to the
1284 relevent sections in the output BFD, which may not be in
1285 the same location as they were in the input BFD. But the
1286 whole point of this action is to preserve the original
1287 values of the sh_link and sh_info fields, so that they
1288 can be matched up with the section headers in the
1289 original file. So strictly speaking we may be creating
1290 an invalid ELF file, but it is only for a file that just
1291 contains debug info and only for sections without any
1293 if (oheader->sh_link == 0)
1294 oheader->sh_link = iheader->sh_link;
1295 if (oheader->sh_info == 0)
1296 oheader->sh_info = iheader->sh_info;
1307 get_segment_type (unsigned int p_type)
1312 case PT_NULL: pt = "NULL"; break;
1313 case PT_LOAD: pt = "LOAD"; break;
1314 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1315 case PT_INTERP: pt = "INTERP"; break;
1316 case PT_NOTE: pt = "NOTE"; break;
1317 case PT_SHLIB: pt = "SHLIB"; break;
1318 case PT_PHDR: pt = "PHDR"; break;
1319 case PT_TLS: pt = "TLS"; break;
1320 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1321 case PT_GNU_STACK: pt = "STACK"; break;
1322 case PT_GNU_RELRO: pt = "RELRO"; break;
1323 default: pt = NULL; break;
1328 /* Print out the program headers. */
1331 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1333 FILE *f = (FILE *) farg;
1334 Elf_Internal_Phdr *p;
1336 bfd_byte *dynbuf = NULL;
1338 p = elf_tdata (abfd)->phdr;
1343 fprintf (f, _("\nProgram Header:\n"));
1344 c = elf_elfheader (abfd)->e_phnum;
1345 for (i = 0; i < c; i++, p++)
1347 const char *pt = get_segment_type (p->p_type);
1352 sprintf (buf, "0x%lx", p->p_type);
1355 fprintf (f, "%8s off 0x", pt);
1356 bfd_fprintf_vma (abfd, f, p->p_offset);
1357 fprintf (f, " vaddr 0x");
1358 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1359 fprintf (f, " paddr 0x");
1360 bfd_fprintf_vma (abfd, f, p->p_paddr);
1361 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1362 fprintf (f, " filesz 0x");
1363 bfd_fprintf_vma (abfd, f, p->p_filesz);
1364 fprintf (f, " memsz 0x");
1365 bfd_fprintf_vma (abfd, f, p->p_memsz);
1366 fprintf (f, " flags %c%c%c",
1367 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1368 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1369 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1370 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1371 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1376 s = bfd_get_section_by_name (abfd, ".dynamic");
1379 unsigned int elfsec;
1380 unsigned long shlink;
1381 bfd_byte *extdyn, *extdynend;
1383 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1385 fprintf (f, _("\nDynamic Section:\n"));
1387 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1390 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1391 if (elfsec == SHN_BAD)
1393 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1395 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1396 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1399 /* PR 17512: file: 6f427532. */
1400 if (s->size < extdynsize)
1402 extdynend = extdyn + s->size;
1403 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1405 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1407 Elf_Internal_Dyn dyn;
1408 const char *name = "";
1410 bfd_boolean stringp;
1411 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1413 (*swap_dyn_in) (abfd, extdyn, &dyn);
1415 if (dyn.d_tag == DT_NULL)
1422 if (bed->elf_backend_get_target_dtag)
1423 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1425 if (!strcmp (name, ""))
1427 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1432 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1433 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1434 case DT_PLTGOT: name = "PLTGOT"; break;
1435 case DT_HASH: name = "HASH"; break;
1436 case DT_STRTAB: name = "STRTAB"; break;
1437 case DT_SYMTAB: name = "SYMTAB"; break;
1438 case DT_RELA: name = "RELA"; break;
1439 case DT_RELASZ: name = "RELASZ"; break;
1440 case DT_RELAENT: name = "RELAENT"; break;
1441 case DT_STRSZ: name = "STRSZ"; break;
1442 case DT_SYMENT: name = "SYMENT"; break;
1443 case DT_INIT: name = "INIT"; break;
1444 case DT_FINI: name = "FINI"; break;
1445 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1446 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1447 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1448 case DT_REL: name = "REL"; break;
1449 case DT_RELSZ: name = "RELSZ"; break;
1450 case DT_RELENT: name = "RELENT"; break;
1451 case DT_PLTREL: name = "PLTREL"; break;
1452 case DT_DEBUG: name = "DEBUG"; break;
1453 case DT_TEXTREL: name = "TEXTREL"; break;
1454 case DT_JMPREL: name = "JMPREL"; break;
1455 case DT_BIND_NOW: name = "BIND_NOW"; break;
1456 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1457 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1458 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1459 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1460 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1461 case DT_FLAGS: name = "FLAGS"; break;
1462 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1463 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1464 case DT_CHECKSUM: name = "CHECKSUM"; break;
1465 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1466 case DT_MOVEENT: name = "MOVEENT"; break;
1467 case DT_MOVESZ: name = "MOVESZ"; break;
1468 case DT_FEATURE: name = "FEATURE"; break;
1469 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1470 case DT_SYMINSZ: name = "SYMINSZ"; break;
1471 case DT_SYMINENT: name = "SYMINENT"; break;
1472 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1473 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1474 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1475 case DT_PLTPAD: name = "PLTPAD"; break;
1476 case DT_MOVETAB: name = "MOVETAB"; break;
1477 case DT_SYMINFO: name = "SYMINFO"; break;
1478 case DT_RELACOUNT: name = "RELACOUNT"; break;
1479 case DT_RELCOUNT: name = "RELCOUNT"; break;
1480 case DT_FLAGS_1: name = "FLAGS_1"; break;
1481 case DT_VERSYM: name = "VERSYM"; break;
1482 case DT_VERDEF: name = "VERDEF"; break;
1483 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1484 case DT_VERNEED: name = "VERNEED"; break;
1485 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1486 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1487 case DT_USED: name = "USED"; break;
1488 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1489 case DT_GNU_HASH: name = "GNU_HASH"; break;
1492 fprintf (f, " %-20s ", name);
1496 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1501 unsigned int tagv = dyn.d_un.d_val;
1503 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1506 fprintf (f, "%s", string);
1515 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1516 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1518 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1522 if (elf_dynverdef (abfd) != 0)
1524 Elf_Internal_Verdef *t;
1526 fprintf (f, _("\nVersion definitions:\n"));
1527 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1529 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1530 t->vd_flags, t->vd_hash,
1531 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1532 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1534 Elf_Internal_Verdaux *a;
1537 for (a = t->vd_auxptr->vda_nextptr;
1541 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1547 if (elf_dynverref (abfd) != 0)
1549 Elf_Internal_Verneed *t;
1551 fprintf (f, _("\nVersion References:\n"));
1552 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1554 Elf_Internal_Vernaux *a;
1556 fprintf (f, _(" required from %s:\n"),
1557 t->vn_filename ? t->vn_filename : "<corrupt>");
1558 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1559 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1560 a->vna_flags, a->vna_other,
1561 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1573 /* Get version string. */
1576 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1577 bfd_boolean *hidden)
1579 const char *version_string = NULL;
1580 if (elf_dynversym (abfd) != 0
1581 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1583 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1585 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1586 vernum &= VERSYM_VERSION;
1589 version_string = "";
1590 else if (vernum == 1)
1591 version_string = "Base";
1592 else if (vernum <= elf_tdata (abfd)->cverdefs)
1594 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1597 Elf_Internal_Verneed *t;
1599 version_string = "";
1600 for (t = elf_tdata (abfd)->verref;
1604 Elf_Internal_Vernaux *a;
1606 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1608 if (a->vna_other == vernum)
1610 version_string = a->vna_nodename;
1617 return version_string;
1620 /* Display ELF-specific fields of a symbol. */
1623 bfd_elf_print_symbol (bfd *abfd,
1626 bfd_print_symbol_type how)
1628 FILE *file = (FILE *) filep;
1631 case bfd_print_symbol_name:
1632 fprintf (file, "%s", symbol->name);
1634 case bfd_print_symbol_more:
1635 fprintf (file, "elf ");
1636 bfd_fprintf_vma (abfd, file, symbol->value);
1637 fprintf (file, " %lx", (unsigned long) symbol->flags);
1639 case bfd_print_symbol_all:
1641 const char *section_name;
1642 const char *name = NULL;
1643 const struct elf_backend_data *bed;
1644 unsigned char st_other;
1646 const char *version_string;
1649 section_name = symbol->section ? symbol->section->name : "(*none*)";
1651 bed = get_elf_backend_data (abfd);
1652 if (bed->elf_backend_print_symbol_all)
1653 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1657 name = symbol->name;
1658 bfd_print_symbol_vandf (abfd, file, symbol);
1661 fprintf (file, " %s\t", section_name);
1662 /* Print the "other" value for a symbol. For common symbols,
1663 we've already printed the size; now print the alignment.
1664 For other symbols, we have no specified alignment, and
1665 we've printed the address; now print the size. */
1666 if (symbol->section && bfd_is_com_section (symbol->section))
1667 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1669 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1670 bfd_fprintf_vma (abfd, file, val);
1672 /* If we have version information, print it. */
1673 version_string = _bfd_elf_get_symbol_version_string (abfd,
1679 fprintf (file, " %-11s", version_string);
1684 fprintf (file, " (%s)", version_string);
1685 for (i = 10 - strlen (version_string); i > 0; --i)
1690 /* If the st_other field is not zero, print it. */
1691 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1696 case STV_INTERNAL: fprintf (file, " .internal"); break;
1697 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1698 case STV_PROTECTED: fprintf (file, " .protected"); break;
1700 /* Some other non-defined flags are also present, so print
1702 fprintf (file, " 0x%02x", (unsigned int) st_other);
1705 fprintf (file, " %s", name);
1711 /* ELF .o/exec file reading */
1713 /* Create a new bfd section from an ELF section header. */
1716 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1718 Elf_Internal_Shdr *hdr;
1719 Elf_Internal_Ehdr *ehdr;
1720 const struct elf_backend_data *bed;
1722 bfd_boolean ret = TRUE;
1723 static bfd_boolean * sections_being_created = NULL;
1724 static bfd * sections_being_created_abfd = NULL;
1725 static unsigned int nesting = 0;
1727 if (shindex >= elf_numsections (abfd))
1732 /* PR17512: A corrupt ELF binary might contain a recursive group of
1733 sections, with each the string indicies pointing to the next in the
1734 loop. Detect this here, by refusing to load a section that we are
1735 already in the process of loading. We only trigger this test if
1736 we have nested at least three sections deep as normal ELF binaries
1737 can expect to recurse at least once.
1739 FIXME: It would be better if this array was attached to the bfd,
1740 rather than being held in a static pointer. */
1742 if (sections_being_created_abfd != abfd)
1743 sections_being_created = NULL;
1744 if (sections_being_created == NULL)
1746 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1747 sections_being_created = (bfd_boolean *)
1748 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
1749 sections_being_created_abfd = abfd;
1751 if (sections_being_created [shindex])
1753 (*_bfd_error_handler)
1754 (_("%B: warning: loop in section dependencies detected"), abfd);
1757 sections_being_created [shindex] = TRUE;
1760 hdr = elf_elfsections (abfd)[shindex];
1761 ehdr = elf_elfheader (abfd);
1762 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1767 bed = get_elf_backend_data (abfd);
1768 switch (hdr->sh_type)
1771 /* Inactive section. Throw it away. */
1774 case SHT_PROGBITS: /* Normal section with contents. */
1775 case SHT_NOBITS: /* .bss section. */
1776 case SHT_HASH: /* .hash section. */
1777 case SHT_NOTE: /* .note section. */
1778 case SHT_INIT_ARRAY: /* .init_array section. */
1779 case SHT_FINI_ARRAY: /* .fini_array section. */
1780 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1781 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1782 case SHT_GNU_HASH: /* .gnu.hash section. */
1783 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1786 case SHT_DYNAMIC: /* Dynamic linking information. */
1787 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1790 if (hdr->sh_link > elf_numsections (abfd))
1792 /* PR 10478: Accept Solaris binaries with a sh_link
1793 field set to SHN_BEFORE or SHN_AFTER. */
1794 switch (bfd_get_arch (abfd))
1797 case bfd_arch_sparc:
1798 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1799 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1801 /* Otherwise fall through. */
1806 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1808 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1810 Elf_Internal_Shdr *dynsymhdr;
1812 /* The shared libraries distributed with hpux11 have a bogus
1813 sh_link field for the ".dynamic" section. Find the
1814 string table for the ".dynsym" section instead. */
1815 if (elf_dynsymtab (abfd) != 0)
1817 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1818 hdr->sh_link = dynsymhdr->sh_link;
1822 unsigned int i, num_sec;
1824 num_sec = elf_numsections (abfd);
1825 for (i = 1; i < num_sec; i++)
1827 dynsymhdr = elf_elfsections (abfd)[i];
1828 if (dynsymhdr->sh_type == SHT_DYNSYM)
1830 hdr->sh_link = dynsymhdr->sh_link;
1838 case SHT_SYMTAB: /* A symbol table. */
1839 if (elf_onesymtab (abfd) == shindex)
1842 if (hdr->sh_entsize != bed->s->sizeof_sym)
1845 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1847 if (hdr->sh_size != 0)
1849 /* Some assemblers erroneously set sh_info to one with a
1850 zero sh_size. ld sees this as a global symbol count
1851 of (unsigned) -1. Fix it here. */
1856 /* PR 18854: A binary might contain more than one symbol table.
1857 Unusual, but possible. Warn, but continue. */
1858 if (elf_onesymtab (abfd) != 0)
1860 (*_bfd_error_handler)
1861 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
1865 elf_onesymtab (abfd) = shindex;
1866 elf_symtab_hdr (abfd) = *hdr;
1867 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
1868 abfd->flags |= HAS_SYMS;
1870 /* Sometimes a shared object will map in the symbol table. If
1871 SHF_ALLOC is set, and this is a shared object, then we also
1872 treat this section as a BFD section. We can not base the
1873 decision purely on SHF_ALLOC, because that flag is sometimes
1874 set in a relocatable object file, which would confuse the
1876 if ((hdr->sh_flags & SHF_ALLOC) != 0
1877 && (abfd->flags & DYNAMIC) != 0
1878 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1882 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1883 can't read symbols without that section loaded as well. It
1884 is most likely specified by the next section header. */
1886 elf_section_list * entry;
1887 unsigned int i, num_sec;
1889 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
1890 if (entry->hdr.sh_link == shindex)
1893 num_sec = elf_numsections (abfd);
1894 for (i = shindex + 1; i < num_sec; i++)
1896 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1898 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1899 && hdr2->sh_link == shindex)
1904 for (i = 1; i < shindex; i++)
1906 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1908 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1909 && hdr2->sh_link == shindex)
1914 ret = bfd_section_from_shdr (abfd, i);
1915 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
1919 case SHT_DYNSYM: /* A dynamic symbol table. */
1920 if (elf_dynsymtab (abfd) == shindex)
1923 if (hdr->sh_entsize != bed->s->sizeof_sym)
1926 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1928 if (hdr->sh_size != 0)
1931 /* Some linkers erroneously set sh_info to one with a
1932 zero sh_size. ld sees this as a global symbol count
1933 of (unsigned) -1. Fix it here. */
1938 /* PR 18854: A binary might contain more than one dynamic symbol table.
1939 Unusual, but possible. Warn, but continue. */
1940 if (elf_dynsymtab (abfd) != 0)
1942 (*_bfd_error_handler)
1943 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
1947 elf_dynsymtab (abfd) = shindex;
1948 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1949 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1950 abfd->flags |= HAS_SYMS;
1952 /* Besides being a symbol table, we also treat this as a regular
1953 section, so that objcopy can handle it. */
1954 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1957 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
1959 elf_section_list * entry;
1961 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
1962 if (entry->ndx == shindex)
1965 entry = bfd_alloc (abfd, sizeof * entry);
1968 entry->ndx = shindex;
1970 entry->next = elf_symtab_shndx_list (abfd);
1971 elf_symtab_shndx_list (abfd) = entry;
1972 elf_elfsections (abfd)[shindex] = & entry->hdr;
1976 case SHT_STRTAB: /* A string table. */
1977 if (hdr->bfd_section != NULL)
1980 if (ehdr->e_shstrndx == shindex)
1982 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1983 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1987 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1990 elf_tdata (abfd)->strtab_hdr = *hdr;
1991 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1995 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1998 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1999 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2000 elf_elfsections (abfd)[shindex] = hdr;
2001 /* We also treat this as a regular section, so that objcopy
2003 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2008 /* If the string table isn't one of the above, then treat it as a
2009 regular section. We need to scan all the headers to be sure,
2010 just in case this strtab section appeared before the above. */
2011 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2013 unsigned int i, num_sec;
2015 num_sec = elf_numsections (abfd);
2016 for (i = 1; i < num_sec; i++)
2018 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2019 if (hdr2->sh_link == shindex)
2021 /* Prevent endless recursion on broken objects. */
2024 if (! bfd_section_from_shdr (abfd, i))
2026 if (elf_onesymtab (abfd) == i)
2028 if (elf_dynsymtab (abfd) == i)
2029 goto dynsymtab_strtab;
2033 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2038 /* *These* do a lot of work -- but build no sections! */
2040 asection *target_sect;
2041 Elf_Internal_Shdr *hdr2, **p_hdr;
2042 unsigned int num_sec = elf_numsections (abfd);
2043 struct bfd_elf_section_data *esdt;
2047 != (bfd_size_type) (hdr->sh_type == SHT_REL
2048 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2051 /* Check for a bogus link to avoid crashing. */
2052 if (hdr->sh_link >= num_sec)
2054 ((*_bfd_error_handler)
2055 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2056 abfd, hdr->sh_link, name, shindex));
2057 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2062 /* For some incomprehensible reason Oracle distributes
2063 libraries for Solaris in which some of the objects have
2064 bogus sh_link fields. It would be nice if we could just
2065 reject them, but, unfortunately, some people need to use
2066 them. We scan through the section headers; if we find only
2067 one suitable symbol table, we clobber the sh_link to point
2068 to it. I hope this doesn't break anything.
2070 Don't do it on executable nor shared library. */
2071 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2072 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2073 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2079 for (scan = 1; scan < num_sec; scan++)
2081 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2082 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2093 hdr->sh_link = found;
2096 /* Get the symbol table. */
2097 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2098 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2099 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2102 /* If this reloc section does not use the main symbol table we
2103 don't treat it as a reloc section. BFD can't adequately
2104 represent such a section, so at least for now, we don't
2105 try. We just present it as a normal section. We also
2106 can't use it as a reloc section if it points to the null
2107 section, an invalid section, another reloc section, or its
2108 sh_link points to the null section. */
2109 if (hdr->sh_link != elf_onesymtab (abfd)
2110 || hdr->sh_link == SHN_UNDEF
2111 || hdr->sh_info == SHN_UNDEF
2112 || hdr->sh_info >= num_sec
2113 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2114 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2116 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2121 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2124 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2125 if (target_sect == NULL)
2128 esdt = elf_section_data (target_sect);
2129 if (hdr->sh_type == SHT_RELA)
2130 p_hdr = &esdt->rela.hdr;
2132 p_hdr = &esdt->rel.hdr;
2134 /* PR 17512: file: 0b4f81b7. */
2137 amt = sizeof (*hdr2);
2138 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2143 elf_elfsections (abfd)[shindex] = hdr2;
2144 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2145 target_sect->flags |= SEC_RELOC;
2146 target_sect->relocation = NULL;
2147 target_sect->rel_filepos = hdr->sh_offset;
2148 /* In the section to which the relocations apply, mark whether
2149 its relocations are of the REL or RELA variety. */
2150 if (hdr->sh_size != 0)
2152 if (hdr->sh_type == SHT_RELA)
2153 target_sect->use_rela_p = 1;
2155 abfd->flags |= HAS_RELOC;
2159 case SHT_GNU_verdef:
2160 elf_dynverdef (abfd) = shindex;
2161 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2162 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2165 case SHT_GNU_versym:
2166 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2169 elf_dynversym (abfd) = shindex;
2170 elf_tdata (abfd)->dynversym_hdr = *hdr;
2171 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2174 case SHT_GNU_verneed:
2175 elf_dynverref (abfd) = shindex;
2176 elf_tdata (abfd)->dynverref_hdr = *hdr;
2177 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2184 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2187 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2190 if (hdr->contents != NULL)
2192 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2193 unsigned int n_elt = hdr->sh_size / sizeof (* idx);
2198 if (idx->flags & GRP_COMDAT)
2199 hdr->bfd_section->flags
2200 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2202 /* We try to keep the same section order as it comes in. */
2205 while (--n_elt != 0)
2209 if (idx->shdr != NULL
2210 && (s = idx->shdr->bfd_section) != NULL
2211 && elf_next_in_group (s) != NULL)
2213 elf_next_in_group (hdr->bfd_section) = s;
2221 /* Possibly an attributes section. */
2222 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2223 || hdr->sh_type == bed->obj_attrs_section_type)
2225 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2227 _bfd_elf_parse_attributes (abfd, hdr);
2231 /* Check for any processor-specific section types. */
2232 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2235 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2237 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2238 /* FIXME: How to properly handle allocated section reserved
2239 for applications? */
2240 (*_bfd_error_handler)
2241 (_("%B: don't know how to handle allocated, application "
2242 "specific section `%s' [0x%8x]"),
2243 abfd, name, hdr->sh_type);
2246 /* Allow sections reserved for applications. */
2247 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2252 else if (hdr->sh_type >= SHT_LOPROC
2253 && hdr->sh_type <= SHT_HIPROC)
2254 /* FIXME: We should handle this section. */
2255 (*_bfd_error_handler)
2256 (_("%B: don't know how to handle processor specific section "
2258 abfd, name, hdr->sh_type);
2259 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2261 /* Unrecognised OS-specific sections. */
2262 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2263 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2264 required to correctly process the section and the file should
2265 be rejected with an error message. */
2266 (*_bfd_error_handler)
2267 (_("%B: don't know how to handle OS specific section "
2269 abfd, name, hdr->sh_type);
2272 /* Otherwise it should be processed. */
2273 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2278 /* FIXME: We should handle this section. */
2279 (*_bfd_error_handler)
2280 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2281 abfd, name, hdr->sh_type);
2289 if (sections_being_created && sections_being_created_abfd == abfd)
2290 sections_being_created [shindex] = FALSE;
2291 if (-- nesting == 0)
2293 sections_being_created = NULL;
2294 sections_being_created_abfd = abfd;
2299 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2302 bfd_sym_from_r_symndx (struct sym_cache *cache,
2304 unsigned long r_symndx)
2306 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2308 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2310 Elf_Internal_Shdr *symtab_hdr;
2311 unsigned char esym[sizeof (Elf64_External_Sym)];
2312 Elf_External_Sym_Shndx eshndx;
2314 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2315 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2316 &cache->sym[ent], esym, &eshndx) == NULL)
2319 if (cache->abfd != abfd)
2321 memset (cache->indx, -1, sizeof (cache->indx));
2324 cache->indx[ent] = r_symndx;
2327 return &cache->sym[ent];
2330 /* Given an ELF section number, retrieve the corresponding BFD
2334 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2336 if (sec_index >= elf_numsections (abfd))
2338 return elf_elfsections (abfd)[sec_index]->bfd_section;
2341 static const struct bfd_elf_special_section special_sections_b[] =
2343 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2344 { NULL, 0, 0, 0, 0 }
2347 static const struct bfd_elf_special_section special_sections_c[] =
2349 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2350 { NULL, 0, 0, 0, 0 }
2353 static const struct bfd_elf_special_section special_sections_d[] =
2355 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2356 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2357 /* There are more DWARF sections than these, but they needn't be added here
2358 unless you have to cope with broken compilers that don't emit section
2359 attributes or you want to help the user writing assembler. */
2360 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2361 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2362 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2363 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2364 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2365 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2366 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2367 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2368 { NULL, 0, 0, 0, 0 }
2371 static const struct bfd_elf_special_section special_sections_f[] =
2373 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2374 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2375 { NULL, 0, 0, 0, 0 }
2378 static const struct bfd_elf_special_section special_sections_g[] =
2380 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2381 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2382 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2383 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2384 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2385 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2386 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2387 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2388 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2389 { NULL, 0, 0, 0, 0 }
2392 static const struct bfd_elf_special_section special_sections_h[] =
2394 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2395 { NULL, 0, 0, 0, 0 }
2398 static const struct bfd_elf_special_section special_sections_i[] =
2400 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2401 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2402 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2403 { NULL, 0, 0, 0, 0 }
2406 static const struct bfd_elf_special_section special_sections_l[] =
2408 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2409 { NULL, 0, 0, 0, 0 }
2412 static const struct bfd_elf_special_section special_sections_n[] =
2414 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2415 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2416 { NULL, 0, 0, 0, 0 }
2419 static const struct bfd_elf_special_section special_sections_p[] =
2421 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2422 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2423 { NULL, 0, 0, 0, 0 }
2426 static const struct bfd_elf_special_section special_sections_r[] =
2428 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2429 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2430 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2431 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2432 { NULL, 0, 0, 0, 0 }
2435 static const struct bfd_elf_special_section special_sections_s[] =
2437 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2438 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2439 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2440 /* See struct bfd_elf_special_section declaration for the semantics of
2441 this special case where .prefix_length != strlen (.prefix). */
2442 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2443 { NULL, 0, 0, 0, 0 }
2446 static const struct bfd_elf_special_section special_sections_t[] =
2448 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2449 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2450 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2451 { NULL, 0, 0, 0, 0 }
2454 static const struct bfd_elf_special_section special_sections_z[] =
2456 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2457 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2458 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2459 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2460 { NULL, 0, 0, 0, 0 }
2463 static const struct bfd_elf_special_section * const special_sections[] =
2465 special_sections_b, /* 'b' */
2466 special_sections_c, /* 'c' */
2467 special_sections_d, /* 'd' */
2469 special_sections_f, /* 'f' */
2470 special_sections_g, /* 'g' */
2471 special_sections_h, /* 'h' */
2472 special_sections_i, /* 'i' */
2475 special_sections_l, /* 'l' */
2477 special_sections_n, /* 'n' */
2479 special_sections_p, /* 'p' */
2481 special_sections_r, /* 'r' */
2482 special_sections_s, /* 's' */
2483 special_sections_t, /* 't' */
2489 special_sections_z /* 'z' */
2492 const struct bfd_elf_special_section *
2493 _bfd_elf_get_special_section (const char *name,
2494 const struct bfd_elf_special_section *spec,
2500 len = strlen (name);
2502 for (i = 0; spec[i].prefix != NULL; i++)
2505 int prefix_len = spec[i].prefix_length;
2507 if (len < prefix_len)
2509 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2512 suffix_len = spec[i].suffix_length;
2513 if (suffix_len <= 0)
2515 if (name[prefix_len] != 0)
2517 if (suffix_len == 0)
2519 if (name[prefix_len] != '.'
2520 && (suffix_len == -2
2521 || (rela && spec[i].type == SHT_REL)))
2527 if (len < prefix_len + suffix_len)
2529 if (memcmp (name + len - suffix_len,
2530 spec[i].prefix + prefix_len,
2540 const struct bfd_elf_special_section *
2541 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2544 const struct bfd_elf_special_section *spec;
2545 const struct elf_backend_data *bed;
2547 /* See if this is one of the special sections. */
2548 if (sec->name == NULL)
2551 bed = get_elf_backend_data (abfd);
2552 spec = bed->special_sections;
2555 spec = _bfd_elf_get_special_section (sec->name,
2556 bed->special_sections,
2562 if (sec->name[0] != '.')
2565 i = sec->name[1] - 'b';
2566 if (i < 0 || i > 'z' - 'b')
2569 spec = special_sections[i];
2574 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2578 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2580 struct bfd_elf_section_data *sdata;
2581 const struct elf_backend_data *bed;
2582 const struct bfd_elf_special_section *ssect;
2584 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2587 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2591 sec->used_by_bfd = sdata;
2594 /* Indicate whether or not this section should use RELA relocations. */
2595 bed = get_elf_backend_data (abfd);
2596 sec->use_rela_p = bed->default_use_rela_p;
2598 /* When we read a file, we don't need to set ELF section type and
2599 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2600 anyway. We will set ELF section type and flags for all linker
2601 created sections. If user specifies BFD section flags, we will
2602 set ELF section type and flags based on BFD section flags in
2603 elf_fake_sections. Special handling for .init_array/.fini_array
2604 output sections since they may contain .ctors/.dtors input
2605 sections. We don't want _bfd_elf_init_private_section_data to
2606 copy ELF section type from .ctors/.dtors input sections. */
2607 if (abfd->direction != read_direction
2608 || (sec->flags & SEC_LINKER_CREATED) != 0)
2610 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2613 || (sec->flags & SEC_LINKER_CREATED) != 0
2614 || ssect->type == SHT_INIT_ARRAY
2615 || ssect->type == SHT_FINI_ARRAY))
2617 elf_section_type (sec) = ssect->type;
2618 elf_section_flags (sec) = ssect->attr;
2622 return _bfd_generic_new_section_hook (abfd, sec);
2625 /* Create a new bfd section from an ELF program header.
2627 Since program segments have no names, we generate a synthetic name
2628 of the form segment<NUM>, where NUM is generally the index in the
2629 program header table. For segments that are split (see below) we
2630 generate the names segment<NUM>a and segment<NUM>b.
2632 Note that some program segments may have a file size that is different than
2633 (less than) the memory size. All this means is that at execution the
2634 system must allocate the amount of memory specified by the memory size,
2635 but only initialize it with the first "file size" bytes read from the
2636 file. This would occur for example, with program segments consisting
2637 of combined data+bss.
2639 To handle the above situation, this routine generates TWO bfd sections
2640 for the single program segment. The first has the length specified by
2641 the file size of the segment, and the second has the length specified
2642 by the difference between the two sizes. In effect, the segment is split
2643 into its initialized and uninitialized parts.
2648 _bfd_elf_make_section_from_phdr (bfd *abfd,
2649 Elf_Internal_Phdr *hdr,
2651 const char *type_name)
2659 split = ((hdr->p_memsz > 0)
2660 && (hdr->p_filesz > 0)
2661 && (hdr->p_memsz > hdr->p_filesz));
2663 if (hdr->p_filesz > 0)
2665 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2666 len = strlen (namebuf) + 1;
2667 name = (char *) bfd_alloc (abfd, len);
2670 memcpy (name, namebuf, len);
2671 newsect = bfd_make_section (abfd, name);
2672 if (newsect == NULL)
2674 newsect->vma = hdr->p_vaddr;
2675 newsect->lma = hdr->p_paddr;
2676 newsect->size = hdr->p_filesz;
2677 newsect->filepos = hdr->p_offset;
2678 newsect->flags |= SEC_HAS_CONTENTS;
2679 newsect->alignment_power = bfd_log2 (hdr->p_align);
2680 if (hdr->p_type == PT_LOAD)
2682 newsect->flags |= SEC_ALLOC;
2683 newsect->flags |= SEC_LOAD;
2684 if (hdr->p_flags & PF_X)
2686 /* FIXME: all we known is that it has execute PERMISSION,
2688 newsect->flags |= SEC_CODE;
2691 if (!(hdr->p_flags & PF_W))
2693 newsect->flags |= SEC_READONLY;
2697 if (hdr->p_memsz > hdr->p_filesz)
2701 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2702 len = strlen (namebuf) + 1;
2703 name = (char *) bfd_alloc (abfd, len);
2706 memcpy (name, namebuf, len);
2707 newsect = bfd_make_section (abfd, name);
2708 if (newsect == NULL)
2710 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2711 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2712 newsect->size = hdr->p_memsz - hdr->p_filesz;
2713 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2714 align = newsect->vma & -newsect->vma;
2715 if (align == 0 || align > hdr->p_align)
2716 align = hdr->p_align;
2717 newsect->alignment_power = bfd_log2 (align);
2718 if (hdr->p_type == PT_LOAD)
2720 /* Hack for gdb. Segments that have not been modified do
2721 not have their contents written to a core file, on the
2722 assumption that a debugger can find the contents in the
2723 executable. We flag this case by setting the fake
2724 section size to zero. Note that "real" bss sections will
2725 always have their contents dumped to the core file. */
2726 if (bfd_get_format (abfd) == bfd_core)
2728 newsect->flags |= SEC_ALLOC;
2729 if (hdr->p_flags & PF_X)
2730 newsect->flags |= SEC_CODE;
2732 if (!(hdr->p_flags & PF_W))
2733 newsect->flags |= SEC_READONLY;
2740 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2742 const struct elf_backend_data *bed;
2744 switch (hdr->p_type)
2747 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2750 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2753 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2756 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2759 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2761 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2766 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2769 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2771 case PT_GNU_EH_FRAME:
2772 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2776 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2779 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2782 /* Check for any processor-specific program segment types. */
2783 bed = get_elf_backend_data (abfd);
2784 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2788 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2792 _bfd_elf_single_rel_hdr (asection *sec)
2794 if (elf_section_data (sec)->rel.hdr)
2796 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2797 return elf_section_data (sec)->rel.hdr;
2800 return elf_section_data (sec)->rela.hdr;
2804 _bfd_elf_set_reloc_sh_name (bfd *abfd,
2805 Elf_Internal_Shdr *rel_hdr,
2806 const char *sec_name,
2807 bfd_boolean use_rela_p)
2809 char *name = (char *) bfd_alloc (abfd,
2810 sizeof ".rela" + strlen (sec_name));
2814 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
2816 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2818 if (rel_hdr->sh_name == (unsigned int) -1)
2824 /* Allocate and initialize a section-header for a new reloc section,
2825 containing relocations against ASECT. It is stored in RELDATA. If
2826 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2830 _bfd_elf_init_reloc_shdr (bfd *abfd,
2831 struct bfd_elf_section_reloc_data *reldata,
2832 const char *sec_name,
2833 bfd_boolean use_rela_p,
2834 bfd_boolean delay_st_name_p)
2836 Elf_Internal_Shdr *rel_hdr;
2837 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2840 amt = sizeof (Elf_Internal_Shdr);
2841 BFD_ASSERT (reldata->hdr == NULL);
2842 rel_hdr = bfd_zalloc (abfd, amt);
2843 reldata->hdr = rel_hdr;
2845 if (delay_st_name_p)
2846 rel_hdr->sh_name = (unsigned int) -1;
2847 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
2850 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2851 rel_hdr->sh_entsize = (use_rela_p
2852 ? bed->s->sizeof_rela
2853 : bed->s->sizeof_rel);
2854 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2855 rel_hdr->sh_flags = 0;
2856 rel_hdr->sh_addr = 0;
2857 rel_hdr->sh_size = 0;
2858 rel_hdr->sh_offset = 0;
2863 /* Return the default section type based on the passed in section flags. */
2866 bfd_elf_get_default_section_type (flagword flags)
2868 if ((flags & SEC_ALLOC) != 0
2869 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2871 return SHT_PROGBITS;
2874 struct fake_section_arg
2876 struct bfd_link_info *link_info;
2880 /* Set up an ELF internal section header for a section. */
2883 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2885 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2886 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2887 struct bfd_elf_section_data *esd = elf_section_data (asect);
2888 Elf_Internal_Shdr *this_hdr;
2889 unsigned int sh_type;
2890 const char *name = asect->name;
2891 bfd_boolean delay_st_name_p = FALSE;
2895 /* We already failed; just get out of the bfd_map_over_sections
2900 this_hdr = &esd->this_hdr;
2904 /* ld: compress DWARF debug sections with names: .debug_*. */
2905 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
2906 && (asect->flags & SEC_DEBUGGING)
2910 /* Set SEC_ELF_COMPRESS to indicate this section should be
2912 asect->flags |= SEC_ELF_COMPRESS;
2914 /* If this section will be compressed, delay adding setion
2915 name to section name section after it is compressed in
2916 _bfd_elf_assign_file_positions_for_non_load. */
2917 delay_st_name_p = TRUE;
2920 else if ((asect->flags & SEC_ELF_RENAME))
2922 /* objcopy: rename output DWARF debug section. */
2923 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
2925 /* When we decompress or compress with SHF_COMPRESSED,
2926 convert section name from .zdebug_* to .debug_* if
2930 char *new_name = convert_zdebug_to_debug (abfd, name);
2931 if (new_name == NULL)
2939 else if (asect->compress_status == COMPRESS_SECTION_DONE)
2941 /* PR binutils/18087: Compression does not always make a
2942 section smaller. So only rename the section when
2943 compression has actually taken place. If input section
2944 name is .zdebug_*, we should never compress it again. */
2945 char *new_name = convert_debug_to_zdebug (abfd, name);
2946 if (new_name == NULL)
2951 BFD_ASSERT (name[1] != 'z');
2956 if (delay_st_name_p)
2957 this_hdr->sh_name = (unsigned int) -1;
2961 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2963 if (this_hdr->sh_name == (unsigned int) -1)
2970 /* Don't clear sh_flags. Assembler may set additional bits. */
2972 if ((asect->flags & SEC_ALLOC) != 0
2973 || asect->user_set_vma)
2974 this_hdr->sh_addr = asect->vma;
2976 this_hdr->sh_addr = 0;
2978 this_hdr->sh_offset = 0;
2979 this_hdr->sh_size = asect->size;
2980 this_hdr->sh_link = 0;
2981 /* PR 17512: file: 0eb809fe, 8b0535ee. */
2982 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
2984 (*_bfd_error_handler)
2985 (_("%B: error: Alignment power %d of section `%A' is too big"),
2986 abfd, asect, asect->alignment_power);
2990 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2991 /* The sh_entsize and sh_info fields may have been set already by
2992 copy_private_section_data. */
2994 this_hdr->bfd_section = asect;
2995 this_hdr->contents = NULL;
2997 /* If the section type is unspecified, we set it based on
2999 if ((asect->flags & SEC_GROUP) != 0)
3000 sh_type = SHT_GROUP;
3002 sh_type = bfd_elf_get_default_section_type (asect->flags);
3004 if (this_hdr->sh_type == SHT_NULL)
3005 this_hdr->sh_type = sh_type;
3006 else if (this_hdr->sh_type == SHT_NOBITS
3007 && sh_type == SHT_PROGBITS
3008 && (asect->flags & SEC_ALLOC) != 0)
3010 /* Warn if we are changing a NOBITS section to PROGBITS, but
3011 allow the link to proceed. This can happen when users link
3012 non-bss input sections to bss output sections, or emit data
3013 to a bss output section via a linker script. */
3014 (*_bfd_error_handler)
3015 (_("warning: section `%A' type changed to PROGBITS"), asect);
3016 this_hdr->sh_type = sh_type;
3019 switch (this_hdr->sh_type)
3025 case SHT_INIT_ARRAY:
3026 case SHT_FINI_ARRAY:
3027 case SHT_PREINIT_ARRAY:
3034 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3038 this_hdr->sh_entsize = bed->s->sizeof_sym;
3042 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3046 if (get_elf_backend_data (abfd)->may_use_rela_p)
3047 this_hdr->sh_entsize = bed->s->sizeof_rela;
3051 if (get_elf_backend_data (abfd)->may_use_rel_p)
3052 this_hdr->sh_entsize = bed->s->sizeof_rel;
3055 case SHT_GNU_versym:
3056 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3059 case SHT_GNU_verdef:
3060 this_hdr->sh_entsize = 0;
3061 /* objcopy or strip will copy over sh_info, but may not set
3062 cverdefs. The linker will set cverdefs, but sh_info will be
3064 if (this_hdr->sh_info == 0)
3065 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3067 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3068 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3071 case SHT_GNU_verneed:
3072 this_hdr->sh_entsize = 0;
3073 /* objcopy or strip will copy over sh_info, but may not set
3074 cverrefs. The linker will set cverrefs, but sh_info will be
3076 if (this_hdr->sh_info == 0)
3077 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3079 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3080 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3084 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3088 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3092 if ((asect->flags & SEC_ALLOC) != 0)
3093 this_hdr->sh_flags |= SHF_ALLOC;
3094 if ((asect->flags & SEC_READONLY) == 0)
3095 this_hdr->sh_flags |= SHF_WRITE;
3096 if ((asect->flags & SEC_CODE) != 0)
3097 this_hdr->sh_flags |= SHF_EXECINSTR;
3098 if ((asect->flags & SEC_MERGE) != 0)
3100 this_hdr->sh_flags |= SHF_MERGE;
3101 this_hdr->sh_entsize = asect->entsize;
3102 if ((asect->flags & SEC_STRINGS) != 0)
3103 this_hdr->sh_flags |= SHF_STRINGS;
3105 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3106 this_hdr->sh_flags |= SHF_GROUP;
3107 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3109 this_hdr->sh_flags |= SHF_TLS;
3110 if (asect->size == 0
3111 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3113 struct bfd_link_order *o = asect->map_tail.link_order;
3115 this_hdr->sh_size = 0;
3118 this_hdr->sh_size = o->offset + o->size;
3119 if (this_hdr->sh_size != 0)
3120 this_hdr->sh_type = SHT_NOBITS;
3124 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3125 this_hdr->sh_flags |= SHF_EXCLUDE;
3127 /* If the section has relocs, set up a section header for the
3128 SHT_REL[A] section. If two relocation sections are required for
3129 this section, it is up to the processor-specific back-end to
3130 create the other. */
3131 if ((asect->flags & SEC_RELOC) != 0)
3133 /* When doing a relocatable link, create both REL and RELA sections if
3136 /* Do the normal setup if we wouldn't create any sections here. */
3137 && esd->rel.count + esd->rela.count > 0
3138 && (bfd_link_relocatable (arg->link_info)
3139 || arg->link_info->emitrelocations))
3141 if (esd->rel.count && esd->rel.hdr == NULL
3142 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE,
3148 if (esd->rela.count && esd->rela.hdr == NULL
3149 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE,
3156 else if (!_bfd_elf_init_reloc_shdr (abfd,
3158 ? &esd->rela : &esd->rel),
3165 /* Check for processor-specific section types. */
3166 sh_type = this_hdr->sh_type;
3167 if (bed->elf_backend_fake_sections
3168 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3171 if (sh_type == SHT_NOBITS && asect->size != 0)
3173 /* Don't change the header type from NOBITS if we are being
3174 called for objcopy --only-keep-debug. */
3175 this_hdr->sh_type = sh_type;
3179 /* Fill in the contents of a SHT_GROUP section. Called from
3180 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3181 when ELF targets use the generic linker, ld. Called for ld -r
3182 from bfd_elf_final_link. */
3185 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3187 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3188 asection *elt, *first;
3192 /* Ignore linker created group section. See elfNN_ia64_object_p in
3194 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
3198 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3200 unsigned long symindx = 0;
3202 /* elf_group_id will have been set up by objcopy and the
3204 if (elf_group_id (sec) != NULL)
3205 symindx = elf_group_id (sec)->udata.i;
3209 /* If called from the assembler, swap_out_syms will have set up
3210 elf_section_syms. */
3211 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3212 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3214 elf_section_data (sec)->this_hdr.sh_info = symindx;
3216 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3218 /* The ELF backend linker sets sh_info to -2 when the group
3219 signature symbol is global, and thus the index can't be
3220 set until all local symbols are output. */
3221 asection *igroup = elf_sec_group (elf_next_in_group (sec));
3222 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
3223 unsigned long symndx = sec_data->this_hdr.sh_info;
3224 unsigned long extsymoff = 0;
3225 struct elf_link_hash_entry *h;
3227 if (!elf_bad_symtab (igroup->owner))
3229 Elf_Internal_Shdr *symtab_hdr;
3231 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3232 extsymoff = symtab_hdr->sh_info;
3234 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3235 while (h->root.type == bfd_link_hash_indirect
3236 || h->root.type == bfd_link_hash_warning)
3237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3239 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3242 /* The contents won't be allocated for "ld -r" or objcopy. */
3244 if (sec->contents == NULL)
3247 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3249 /* Arrange for the section to be written out. */
3250 elf_section_data (sec)->this_hdr.contents = sec->contents;
3251 if (sec->contents == NULL)
3258 loc = sec->contents + sec->size;
3260 /* Get the pointer to the first section in the group that gas
3261 squirreled away here. objcopy arranges for this to be set to the
3262 start of the input section group. */
3263 first = elt = elf_next_in_group (sec);
3265 /* First element is a flag word. Rest of section is elf section
3266 indices for all the sections of the group. Write them backwards
3267 just to keep the group in the same order as given in .section
3268 directives, not that it matters. */
3275 s = s->output_section;
3277 && !bfd_is_abs_section (s))
3279 unsigned int idx = elf_section_data (s)->this_idx;
3282 H_PUT_32 (abfd, idx, loc);
3284 elt = elf_next_in_group (elt);
3289 if ((loc -= 4) != sec->contents)
3292 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3295 /* Return the section which RELOC_SEC applies to. */
3298 _bfd_elf_get_reloc_section (asection *reloc_sec)
3304 if (reloc_sec == NULL)
3307 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3308 if (type != SHT_REL && type != SHT_RELA)
3311 /* We look up the section the relocs apply to by name. */
3312 name = reloc_sec->name;
3313 if (type == SHT_REL)
3318 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3319 section apply to .got.plt section. */
3320 abfd = reloc_sec->owner;
3321 if (get_elf_backend_data (abfd)->want_got_plt
3322 && strcmp (name, ".plt") == 0)
3324 /* .got.plt is a linker created input section. It may be mapped
3325 to some other output section. Try two likely sections. */
3327 reloc_sec = bfd_get_section_by_name (abfd, name);
3328 if (reloc_sec != NULL)
3333 reloc_sec = bfd_get_section_by_name (abfd, name);
3337 /* Assign all ELF section numbers. The dummy first section is handled here
3338 too. The link/info pointers for the standard section types are filled
3339 in here too, while we're at it. */
3342 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3344 struct elf_obj_tdata *t = elf_tdata (abfd);
3346 unsigned int section_number;
3347 Elf_Internal_Shdr **i_shdrp;
3348 struct bfd_elf_section_data *d;
3349 bfd_boolean need_symtab;
3353 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3355 /* SHT_GROUP sections are in relocatable files only. */
3356 if (link_info == NULL || bfd_link_relocatable (link_info))
3358 /* Put SHT_GROUP sections first. */
3359 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3361 d = elf_section_data (sec);
3363 if (d->this_hdr.sh_type == SHT_GROUP)
3365 if (sec->flags & SEC_LINKER_CREATED)
3367 /* Remove the linker created SHT_GROUP sections. */
3368 bfd_section_list_remove (abfd, sec);
3369 abfd->section_count--;
3372 d->this_idx = section_number++;
3377 for (sec = abfd->sections; sec; sec = sec->next)
3379 d = elf_section_data (sec);
3381 if (d->this_hdr.sh_type != SHT_GROUP)
3382 d->this_idx = section_number++;
3383 if (d->this_hdr.sh_name != (unsigned int) -1)
3384 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3387 d->rel.idx = section_number++;
3388 if (d->rel.hdr->sh_name != (unsigned int) -1)
3389 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3396 d->rela.idx = section_number++;
3397 if (d->rela.hdr->sh_name != (unsigned int) -1)
3398 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3404 elf_shstrtab_sec (abfd) = section_number++;
3405 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3406 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3408 need_symtab = (bfd_get_symcount (abfd) > 0
3409 || (link_info == NULL
3410 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3414 elf_onesymtab (abfd) = section_number++;
3415 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3416 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3418 elf_section_list * entry;
3420 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3422 entry = bfd_zalloc (abfd, sizeof * entry);
3423 entry->ndx = section_number++;
3424 elf_symtab_shndx_list (abfd) = entry;
3426 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3427 ".symtab_shndx", FALSE);
3428 if (entry->hdr.sh_name == (unsigned int) -1)
3431 elf_strtab_sec (abfd) = section_number++;
3432 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3435 if (section_number >= SHN_LORESERVE)
3437 _bfd_error_handler (_("%B: too many sections: %u"),
3438 abfd, section_number);
3442 elf_numsections (abfd) = section_number;
3443 elf_elfheader (abfd)->e_shnum = section_number;
3445 /* Set up the list of section header pointers, in agreement with the
3447 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3448 sizeof (Elf_Internal_Shdr *));
3449 if (i_shdrp == NULL)
3452 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3453 sizeof (Elf_Internal_Shdr));
3454 if (i_shdrp[0] == NULL)
3456 bfd_release (abfd, i_shdrp);
3460 elf_elfsections (abfd) = i_shdrp;
3462 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3465 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3466 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3468 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3469 BFD_ASSERT (entry != NULL);
3470 i_shdrp[entry->ndx] = & entry->hdr;
3471 entry->hdr.sh_link = elf_onesymtab (abfd);
3473 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3474 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3477 for (sec = abfd->sections; sec; sec = sec->next)
3481 d = elf_section_data (sec);
3483 i_shdrp[d->this_idx] = &d->this_hdr;
3484 if (d->rel.idx != 0)
3485 i_shdrp[d->rel.idx] = d->rel.hdr;
3486 if (d->rela.idx != 0)
3487 i_shdrp[d->rela.idx] = d->rela.hdr;
3489 /* Fill in the sh_link and sh_info fields while we're at it. */
3491 /* sh_link of a reloc section is the section index of the symbol
3492 table. sh_info is the section index of the section to which
3493 the relocation entries apply. */
3494 if (d->rel.idx != 0)
3496 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3497 d->rel.hdr->sh_info = d->this_idx;
3498 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3500 if (d->rela.idx != 0)
3502 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3503 d->rela.hdr->sh_info = d->this_idx;
3504 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3507 /* We need to set up sh_link for SHF_LINK_ORDER. */
3508 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3510 s = elf_linked_to_section (sec);
3513 /* elf_linked_to_section points to the input section. */
3514 if (link_info != NULL)
3516 /* Check discarded linkonce section. */
3517 if (discarded_section (s))
3520 (*_bfd_error_handler)
3521 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3522 abfd, d->this_hdr.bfd_section,
3524 /* Point to the kept section if it has the same
3525 size as the discarded one. */
3526 kept = _bfd_elf_check_kept_section (s, link_info);
3529 bfd_set_error (bfd_error_bad_value);
3535 s = s->output_section;
3536 BFD_ASSERT (s != NULL);
3540 /* Handle objcopy. */
3541 if (s->output_section == NULL)
3543 (*_bfd_error_handler)
3544 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3545 abfd, d->this_hdr.bfd_section, s, s->owner);
3546 bfd_set_error (bfd_error_bad_value);
3549 s = s->output_section;
3551 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3556 The Intel C compiler generates SHT_IA_64_UNWIND with
3557 SHF_LINK_ORDER. But it doesn't set the sh_link or
3558 sh_info fields. Hence we could get the situation
3560 const struct elf_backend_data *bed
3561 = get_elf_backend_data (abfd);
3562 if (bed->link_order_error_handler)
3563 bed->link_order_error_handler
3564 (_("%B: warning: sh_link not set for section `%A'"),
3569 switch (d->this_hdr.sh_type)
3573 /* A reloc section which we are treating as a normal BFD
3574 section. sh_link is the section index of the symbol
3575 table. sh_info is the section index of the section to
3576 which the relocation entries apply. We assume that an
3577 allocated reloc section uses the dynamic symbol table.
3578 FIXME: How can we be sure? */
3579 s = bfd_get_section_by_name (abfd, ".dynsym");
3581 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3583 s = get_elf_backend_data (abfd)->get_reloc_section (sec);
3586 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3587 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3592 /* We assume that a section named .stab*str is a stabs
3593 string section. We look for a section with the same name
3594 but without the trailing ``str'', and set its sh_link
3595 field to point to this section. */
3596 if (CONST_STRNEQ (sec->name, ".stab")
3597 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3602 len = strlen (sec->name);
3603 alc = (char *) bfd_malloc (len - 2);
3606 memcpy (alc, sec->name, len - 3);
3607 alc[len - 3] = '\0';
3608 s = bfd_get_section_by_name (abfd, alc);
3612 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3614 /* This is a .stab section. */
3615 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3616 elf_section_data (s)->this_hdr.sh_entsize
3617 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3624 case SHT_GNU_verneed:
3625 case SHT_GNU_verdef:
3626 /* sh_link is the section header index of the string table
3627 used for the dynamic entries, or the symbol table, or the
3629 s = bfd_get_section_by_name (abfd, ".dynstr");
3631 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3634 case SHT_GNU_LIBLIST:
3635 /* sh_link is the section header index of the prelink library
3636 list used for the dynamic entries, or the symbol table, or
3637 the version strings. */
3638 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3639 ? ".dynstr" : ".gnu.libstr");
3641 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3646 case SHT_GNU_versym:
3647 /* sh_link is the section header index of the symbol table
3648 this hash table or version table is for. */
3649 s = bfd_get_section_by_name (abfd, ".dynsym");
3651 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3655 d->this_hdr.sh_link = elf_onesymtab (abfd);
3659 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3660 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3661 debug section name from .debug_* to .zdebug_* if needed. */
3667 sym_is_global (bfd *abfd, asymbol *sym)
3669 /* If the backend has a special mapping, use it. */
3670 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3671 if (bed->elf_backend_sym_is_global)
3672 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3674 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3675 || bfd_is_und_section (bfd_get_section (sym))
3676 || bfd_is_com_section (bfd_get_section (sym)));
3679 /* Don't output section symbols for sections that are not going to be
3680 output, that are duplicates or there is no BFD section. */
3683 ignore_section_sym (bfd *abfd, asymbol *sym)
3685 elf_symbol_type *type_ptr;
3687 if ((sym->flags & BSF_SECTION_SYM) == 0)
3690 type_ptr = elf_symbol_from (abfd, sym);
3691 return ((type_ptr != NULL
3692 && type_ptr->internal_elf_sym.st_shndx != 0
3693 && bfd_is_abs_section (sym->section))
3694 || !(sym->section->owner == abfd
3695 || (sym->section->output_section->owner == abfd
3696 && sym->section->output_offset == 0)
3697 || bfd_is_abs_section (sym->section)));
3700 /* Map symbol from it's internal number to the external number, moving
3701 all local symbols to be at the head of the list. */
3704 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3706 unsigned int symcount = bfd_get_symcount (abfd);
3707 asymbol **syms = bfd_get_outsymbols (abfd);
3708 asymbol **sect_syms;
3709 unsigned int num_locals = 0;
3710 unsigned int num_globals = 0;
3711 unsigned int num_locals2 = 0;
3712 unsigned int num_globals2 = 0;
3713 unsigned int max_index = 0;
3719 fprintf (stderr, "elf_map_symbols\n");
3723 for (asect = abfd->sections; asect; asect = asect->next)
3725 if (max_index < asect->index)
3726 max_index = asect->index;
3730 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3731 if (sect_syms == NULL)
3733 elf_section_syms (abfd) = sect_syms;
3734 elf_num_section_syms (abfd) = max_index;
3736 /* Init sect_syms entries for any section symbols we have already
3737 decided to output. */
3738 for (idx = 0; idx < symcount; idx++)
3740 asymbol *sym = syms[idx];
3742 if ((sym->flags & BSF_SECTION_SYM) != 0
3744 && !ignore_section_sym (abfd, sym)
3745 && !bfd_is_abs_section (sym->section))
3747 asection *sec = sym->section;
3749 if (sec->owner != abfd)
3750 sec = sec->output_section;
3752 sect_syms[sec->index] = syms[idx];
3756 /* Classify all of the symbols. */
3757 for (idx = 0; idx < symcount; idx++)
3759 if (sym_is_global (abfd, syms[idx]))
3761 else if (!ignore_section_sym (abfd, syms[idx]))
3765 /* We will be adding a section symbol for each normal BFD section. Most
3766 sections will already have a section symbol in outsymbols, but
3767 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3768 at least in that case. */
3769 for (asect = abfd->sections; asect; asect = asect->next)
3771 if (sect_syms[asect->index] == NULL)
3773 if (!sym_is_global (abfd, asect->symbol))
3780 /* Now sort the symbols so the local symbols are first. */
3781 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3782 sizeof (asymbol *));
3784 if (new_syms == NULL)
3787 for (idx = 0; idx < symcount; idx++)
3789 asymbol *sym = syms[idx];
3792 if (sym_is_global (abfd, sym))
3793 i = num_locals + num_globals2++;
3794 else if (!ignore_section_sym (abfd, sym))
3799 sym->udata.i = i + 1;
3801 for (asect = abfd->sections; asect; asect = asect->next)
3803 if (sect_syms[asect->index] == NULL)
3805 asymbol *sym = asect->symbol;
3808 sect_syms[asect->index] = sym;
3809 if (!sym_is_global (abfd, sym))
3812 i = num_locals + num_globals2++;
3814 sym->udata.i = i + 1;
3818 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3820 *pnum_locals = num_locals;
3824 /* Align to the maximum file alignment that could be required for any
3825 ELF data structure. */
3827 static inline file_ptr
3828 align_file_position (file_ptr off, int align)
3830 return (off + align - 1) & ~(align - 1);
3833 /* Assign a file position to a section, optionally aligning to the
3834 required section alignment. */
3837 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3841 if (align && i_shdrp->sh_addralign > 1)
3842 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3843 i_shdrp->sh_offset = offset;
3844 if (i_shdrp->bfd_section != NULL)
3845 i_shdrp->bfd_section->filepos = offset;
3846 if (i_shdrp->sh_type != SHT_NOBITS)
3847 offset += i_shdrp->sh_size;
3851 /* Compute the file positions we are going to put the sections at, and
3852 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3853 is not NULL, this is being called by the ELF backend linker. */
3856 _bfd_elf_compute_section_file_positions (bfd *abfd,
3857 struct bfd_link_info *link_info)
3859 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3860 struct fake_section_arg fsargs;
3862 struct elf_strtab_hash *strtab = NULL;
3863 Elf_Internal_Shdr *shstrtab_hdr;
3864 bfd_boolean need_symtab;
3866 if (abfd->output_has_begun)
3869 /* Do any elf backend specific processing first. */
3870 if (bed->elf_backend_begin_write_processing)
3871 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3873 if (! prep_headers (abfd))
3876 /* Post process the headers if necessary. */
3877 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3879 fsargs.failed = FALSE;
3880 fsargs.link_info = link_info;
3881 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3885 if (!assign_section_numbers (abfd, link_info))
3888 /* The backend linker builds symbol table information itself. */
3889 need_symtab = (link_info == NULL
3890 && (bfd_get_symcount (abfd) > 0
3891 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3895 /* Non-zero if doing a relocatable link. */
3896 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3898 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3903 if (link_info == NULL)
3905 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3910 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3911 /* sh_name was set in prep_headers. */
3912 shstrtab_hdr->sh_type = SHT_STRTAB;
3913 shstrtab_hdr->sh_flags = 0;
3914 shstrtab_hdr->sh_addr = 0;
3915 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
3916 shstrtab_hdr->sh_entsize = 0;
3917 shstrtab_hdr->sh_link = 0;
3918 shstrtab_hdr->sh_info = 0;
3919 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
3920 shstrtab_hdr->sh_addralign = 1;
3922 if (!assign_file_positions_except_relocs (abfd, link_info))
3928 Elf_Internal_Shdr *hdr;
3930 off = elf_next_file_pos (abfd);
3932 hdr = & elf_symtab_hdr (abfd);
3933 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3935 if (elf_symtab_shndx_list (abfd) != NULL)
3937 hdr = & elf_symtab_shndx_list (abfd)->hdr;
3938 if (hdr->sh_size != 0)
3939 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3940 /* FIXME: What about other symtab_shndx sections in the list ? */
3943 hdr = &elf_tdata (abfd)->strtab_hdr;
3944 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3946 elf_next_file_pos (abfd) = off;
3948 /* Now that we know where the .strtab section goes, write it
3950 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3951 || ! _bfd_elf_strtab_emit (abfd, strtab))
3953 _bfd_elf_strtab_free (strtab);
3956 abfd->output_has_begun = TRUE;
3961 /* Make an initial estimate of the size of the program header. If we
3962 get the number wrong here, we'll redo section placement. */
3964 static bfd_size_type
3965 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3969 const struct elf_backend_data *bed;
3971 /* Assume we will need exactly two PT_LOAD segments: one for text
3972 and one for data. */
3975 s = bfd_get_section_by_name (abfd, ".interp");
3976 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3978 /* If we have a loadable interpreter section, we need a
3979 PT_INTERP segment. In this case, assume we also need a
3980 PT_PHDR segment, although that may not be true for all
3985 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3987 /* We need a PT_DYNAMIC segment. */
3991 if (info != NULL && info->relro)
3993 /* We need a PT_GNU_RELRO segment. */
3997 if (elf_eh_frame_hdr (abfd))
3999 /* We need a PT_GNU_EH_FRAME segment. */
4003 if (elf_stack_flags (abfd))
4005 /* We need a PT_GNU_STACK segment. */
4009 for (s = abfd->sections; s != NULL; s = s->next)
4011 if ((s->flags & SEC_LOAD) != 0
4012 && CONST_STRNEQ (s->name, ".note"))
4014 /* We need a PT_NOTE segment. */
4016 /* Try to create just one PT_NOTE segment
4017 for all adjacent loadable .note* sections.
4018 gABI requires that within a PT_NOTE segment
4019 (and also inside of each SHT_NOTE section)
4020 each note is padded to a multiple of 4 size,
4021 so we check whether the sections are correctly
4023 if (s->alignment_power == 2)
4024 while (s->next != NULL
4025 && s->next->alignment_power == 2
4026 && (s->next->flags & SEC_LOAD) != 0
4027 && CONST_STRNEQ (s->next->name, ".note"))
4032 for (s = abfd->sections; s != NULL; s = s->next)
4034 if (s->flags & SEC_THREAD_LOCAL)
4036 /* We need a PT_TLS segment. */
4042 /* Let the backend count up any program headers it might need. */
4043 bed = get_elf_backend_data (abfd);
4044 if (bed->elf_backend_additional_program_headers)
4048 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4054 return segs * bed->s->sizeof_phdr;
4057 /* Find the segment that contains the output_section of section. */
4060 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4062 struct elf_segment_map *m;
4063 Elf_Internal_Phdr *p;
4065 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4071 for (i = m->count - 1; i >= 0; i--)
4072 if (m->sections[i] == section)
4079 /* Create a mapping from a set of sections to a program segment. */
4081 static struct elf_segment_map *
4082 make_mapping (bfd *abfd,
4083 asection **sections,
4088 struct elf_segment_map *m;
4093 amt = sizeof (struct elf_segment_map);
4094 amt += (to - from - 1) * sizeof (asection *);
4095 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4099 m->p_type = PT_LOAD;
4100 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4101 m->sections[i - from] = *hdrpp;
4102 m->count = to - from;
4104 if (from == 0 && phdr)
4106 /* Include the headers in the first PT_LOAD segment. */
4107 m->includes_filehdr = 1;
4108 m->includes_phdrs = 1;
4114 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4117 struct elf_segment_map *
4118 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4120 struct elf_segment_map *m;
4122 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4123 sizeof (struct elf_segment_map));
4127 m->p_type = PT_DYNAMIC;
4129 m->sections[0] = dynsec;
4134 /* Possibly add or remove segments from the segment map. */
4137 elf_modify_segment_map (bfd *abfd,
4138 struct bfd_link_info *info,
4139 bfd_boolean remove_empty_load)
4141 struct elf_segment_map **m;
4142 const struct elf_backend_data *bed;
4144 /* The placement algorithm assumes that non allocated sections are
4145 not in PT_LOAD segments. We ensure this here by removing such
4146 sections from the segment map. We also remove excluded
4147 sections. Finally, any PT_LOAD segment without sections is
4149 m = &elf_seg_map (abfd);
4152 unsigned int i, new_count;
4154 for (new_count = 0, i = 0; i < (*m)->count; i++)
4156 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4157 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4158 || (*m)->p_type != PT_LOAD))
4160 (*m)->sections[new_count] = (*m)->sections[i];
4164 (*m)->count = new_count;
4166 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
4172 bed = get_elf_backend_data (abfd);
4173 if (bed->elf_backend_modify_segment_map != NULL)
4175 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4182 /* Set up a mapping from BFD sections to program segments. */
4185 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4188 struct elf_segment_map *m;
4189 asection **sections = NULL;
4190 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4191 bfd_boolean no_user_phdrs;
4193 no_user_phdrs = elf_seg_map (abfd) == NULL;
4196 info->user_phdrs = !no_user_phdrs;
4198 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4202 struct elf_segment_map *mfirst;
4203 struct elf_segment_map **pm;
4206 unsigned int phdr_index;
4207 bfd_vma maxpagesize;
4209 bfd_boolean phdr_in_segment = TRUE;
4210 bfd_boolean writable;
4212 asection *first_tls = NULL;
4213 asection *dynsec, *eh_frame_hdr;
4215 bfd_vma addr_mask, wrap_to = 0;
4217 /* Select the allocated sections, and sort them. */
4219 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4220 sizeof (asection *));
4221 if (sections == NULL)
4224 /* Calculate top address, avoiding undefined behaviour of shift
4225 left operator when shift count is equal to size of type
4227 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4228 addr_mask = (addr_mask << 1) + 1;
4231 for (s = abfd->sections; s != NULL; s = s->next)
4233 if ((s->flags & SEC_ALLOC) != 0)
4237 /* A wrapping section potentially clashes with header. */
4238 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4239 wrap_to = (s->lma + s->size) & addr_mask;
4242 BFD_ASSERT (i <= bfd_count_sections (abfd));
4245 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4247 /* Build the mapping. */
4252 /* If we have a .interp section, then create a PT_PHDR segment for
4253 the program headers and a PT_INTERP segment for the .interp
4255 s = bfd_get_section_by_name (abfd, ".interp");
4256 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4258 amt = sizeof (struct elf_segment_map);
4259 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4263 m->p_type = PT_PHDR;
4264 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4265 m->p_flags = PF_R | PF_X;
4266 m->p_flags_valid = 1;
4267 m->includes_phdrs = 1;
4272 amt = sizeof (struct elf_segment_map);
4273 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4277 m->p_type = PT_INTERP;
4285 /* Look through the sections. We put sections in the same program
4286 segment when the start of the second section can be placed within
4287 a few bytes of the end of the first section. */
4291 maxpagesize = bed->maxpagesize;
4292 /* PR 17512: file: c8455299.
4293 Avoid divide-by-zero errors later on.
4294 FIXME: Should we abort if the maxpagesize is zero ? */
4295 if (maxpagesize == 0)
4298 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4300 && (dynsec->flags & SEC_LOAD) == 0)
4303 /* Deal with -Ttext or something similar such that the first section
4304 is not adjacent to the program headers. This is an
4305 approximation, since at this point we don't know exactly how many
4306 program headers we will need. */
4309 bfd_size_type phdr_size = elf_program_header_size (abfd);
4311 if (phdr_size == (bfd_size_type) -1)
4312 phdr_size = get_program_header_size (abfd, info);
4313 phdr_size += bed->s->sizeof_ehdr;
4314 if ((abfd->flags & D_PAGED) == 0
4315 || (sections[0]->lma & addr_mask) < phdr_size
4316 || ((sections[0]->lma & addr_mask) % maxpagesize
4317 < phdr_size % maxpagesize)
4318 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4319 phdr_in_segment = FALSE;
4322 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4325 bfd_boolean new_segment;
4329 /* See if this section and the last one will fit in the same
4332 if (last_hdr == NULL)
4334 /* If we don't have a segment yet, then we don't need a new
4335 one (we build the last one after this loop). */
4336 new_segment = FALSE;
4338 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4340 /* If this section has a different relation between the
4341 virtual address and the load address, then we need a new
4345 else if (hdr->lma < last_hdr->lma + last_size
4346 || last_hdr->lma + last_size < last_hdr->lma)
4348 /* If this section has a load address that makes it overlap
4349 the previous section, then we need a new segment. */
4352 /* In the next test we have to be careful when last_hdr->lma is close
4353 to the end of the address space. If the aligned address wraps
4354 around to the start of the address space, then there are no more
4355 pages left in memory and it is OK to assume that the current
4356 section can be included in the current segment. */
4357 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4359 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4362 /* If putting this section in this segment would force us to
4363 skip a page in the segment, then we need a new segment. */
4366 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4367 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0
4368 && ((abfd->flags & D_PAGED) == 0
4369 || (((last_hdr->lma + last_size - 1) & -maxpagesize)
4370 != (hdr->lma & -maxpagesize))))
4372 /* We don't want to put a loaded section after a
4373 nonloaded (ie. bss style) section in the same segment
4374 as that will force the non-loaded section to be loaded.
4375 Consider .tbss sections as loaded for this purpose.
4376 However, like the writable/non-writable case below,
4377 if they are on the same page then they must be put
4378 in the same segment. */
4381 else if ((abfd->flags & D_PAGED) == 0)
4383 /* If the file is not demand paged, which means that we
4384 don't require the sections to be correctly aligned in the
4385 file, then there is no other reason for a new segment. */
4386 new_segment = FALSE;
4389 && (hdr->flags & SEC_READONLY) == 0
4390 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4391 != (hdr->lma & -maxpagesize)))
4393 /* We don't want to put a writable section in a read only
4394 segment, unless they are on the same page in memory
4395 anyhow. We already know that the last section does not
4396 bring us past the current section on the page, so the
4397 only case in which the new section is not on the same
4398 page as the previous section is when the previous section
4399 ends precisely on a page boundary. */
4404 /* Otherwise, we can use the same segment. */
4405 new_segment = FALSE;
4408 /* Allow interested parties a chance to override our decision. */
4409 if (last_hdr != NULL
4411 && info->callbacks->override_segment_assignment != NULL)
4413 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4419 if ((hdr->flags & SEC_READONLY) == 0)
4422 /* .tbss sections effectively have zero size. */
4423 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4424 != SEC_THREAD_LOCAL)
4425 last_size = hdr->size;
4431 /* We need a new program segment. We must create a new program
4432 header holding all the sections from phdr_index until hdr. */
4434 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4441 if ((hdr->flags & SEC_READONLY) == 0)
4447 /* .tbss sections effectively have zero size. */
4448 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4449 last_size = hdr->size;
4453 phdr_in_segment = FALSE;
4456 /* Create a final PT_LOAD program segment, but not if it's just
4458 if (last_hdr != NULL
4459 && (i - phdr_index != 1
4460 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4461 != SEC_THREAD_LOCAL)))
4463 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4471 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4474 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4481 /* For each batch of consecutive loadable .note sections,
4482 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4483 because if we link together nonloadable .note sections and
4484 loadable .note sections, we will generate two .note sections
4485 in the output file. FIXME: Using names for section types is
4487 for (s = abfd->sections; s != NULL; s = s->next)
4489 if ((s->flags & SEC_LOAD) != 0
4490 && CONST_STRNEQ (s->name, ".note"))
4495 amt = sizeof (struct elf_segment_map);
4496 if (s->alignment_power == 2)
4497 for (s2 = s; s2->next != NULL; s2 = s2->next)
4499 if (s2->next->alignment_power == 2
4500 && (s2->next->flags & SEC_LOAD) != 0
4501 && CONST_STRNEQ (s2->next->name, ".note")
4502 && align_power (s2->lma + s2->size, 2)
4508 amt += (count - 1) * sizeof (asection *);
4509 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4513 m->p_type = PT_NOTE;
4517 m->sections[m->count - count--] = s;
4518 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4521 m->sections[m->count - 1] = s;
4522 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4526 if (s->flags & SEC_THREAD_LOCAL)
4534 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4537 amt = sizeof (struct elf_segment_map);
4538 amt += (tls_count - 1) * sizeof (asection *);
4539 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4544 m->count = tls_count;
4545 /* Mandated PF_R. */
4547 m->p_flags_valid = 1;
4549 for (i = 0; i < (unsigned int) tls_count; ++i)
4551 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4554 (_("%B: TLS sections are not adjacent:"), abfd);
4557 while (i < (unsigned int) tls_count)
4559 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4561 _bfd_error_handler (_(" TLS: %A"), s);
4565 _bfd_error_handler (_(" non-TLS: %A"), s);
4568 bfd_set_error (bfd_error_bad_value);
4579 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4581 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4582 if (eh_frame_hdr != NULL
4583 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4585 amt = sizeof (struct elf_segment_map);
4586 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4590 m->p_type = PT_GNU_EH_FRAME;
4592 m->sections[0] = eh_frame_hdr->output_section;
4598 if (elf_stack_flags (abfd))
4600 amt = sizeof (struct elf_segment_map);
4601 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4605 m->p_type = PT_GNU_STACK;
4606 m->p_flags = elf_stack_flags (abfd);
4607 m->p_align = bed->stack_align;
4608 m->p_flags_valid = 1;
4609 m->p_align_valid = m->p_align != 0;
4610 if (info->stacksize > 0)
4612 m->p_size = info->stacksize;
4613 m->p_size_valid = 1;
4620 if (info != NULL && info->relro)
4622 for (m = mfirst; m != NULL; m = m->next)
4624 if (m->p_type == PT_LOAD
4626 && m->sections[0]->vma >= info->relro_start
4627 && m->sections[0]->vma < info->relro_end)
4630 while (--i != (unsigned) -1)
4631 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4632 == (SEC_LOAD | SEC_HAS_CONTENTS))
4635 if (i != (unsigned) -1)
4640 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4643 amt = sizeof (struct elf_segment_map);
4644 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4648 m->p_type = PT_GNU_RELRO;
4655 elf_seg_map (abfd) = mfirst;
4658 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4661 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4663 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4668 if (sections != NULL)
4673 /* Sort sections by address. */
4676 elf_sort_sections (const void *arg1, const void *arg2)
4678 const asection *sec1 = *(const asection **) arg1;
4679 const asection *sec2 = *(const asection **) arg2;
4680 bfd_size_type size1, size2;
4682 /* Sort by LMA first, since this is the address used to
4683 place the section into a segment. */
4684 if (sec1->lma < sec2->lma)
4686 else if (sec1->lma > sec2->lma)
4689 /* Then sort by VMA. Normally the LMA and the VMA will be
4690 the same, and this will do nothing. */
4691 if (sec1->vma < sec2->vma)
4693 else if (sec1->vma > sec2->vma)
4696 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4698 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4704 /* If the indicies are the same, do not return 0
4705 here, but continue to try the next comparison. */
4706 if (sec1->target_index - sec2->target_index != 0)
4707 return sec1->target_index - sec2->target_index;
4712 else if (TOEND (sec2))
4717 /* Sort by size, to put zero sized sections
4718 before others at the same address. */
4720 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4721 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4728 return sec1->target_index - sec2->target_index;
4731 /* Ian Lance Taylor writes:
4733 We shouldn't be using % with a negative signed number. That's just
4734 not good. We have to make sure either that the number is not
4735 negative, or that the number has an unsigned type. When the types
4736 are all the same size they wind up as unsigned. When file_ptr is a
4737 larger signed type, the arithmetic winds up as signed long long,
4740 What we're trying to say here is something like ``increase OFF by
4741 the least amount that will cause it to be equal to the VMA modulo
4743 /* In other words, something like:
4745 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4746 off_offset = off % bed->maxpagesize;
4747 if (vma_offset < off_offset)
4748 adjustment = vma_offset + bed->maxpagesize - off_offset;
4750 adjustment = vma_offset - off_offset;
4752 which can can be collapsed into the expression below. */
4755 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4757 /* PR binutils/16199: Handle an alignment of zero. */
4758 if (maxpagesize == 0)
4760 return ((vma - off) % maxpagesize);
4764 print_segment_map (const struct elf_segment_map *m)
4767 const char *pt = get_segment_type (m->p_type);
4772 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4773 sprintf (buf, "LOPROC+%7.7x",
4774 (unsigned int) (m->p_type - PT_LOPROC));
4775 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4776 sprintf (buf, "LOOS+%7.7x",
4777 (unsigned int) (m->p_type - PT_LOOS));
4779 snprintf (buf, sizeof (buf), "%8.8x",
4780 (unsigned int) m->p_type);
4784 fprintf (stderr, "%s:", pt);
4785 for (j = 0; j < m->count; j++)
4786 fprintf (stderr, " %s", m->sections [j]->name);
4792 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4797 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4799 buf = bfd_zmalloc (len);
4802 ret = bfd_bwrite (buf, len, abfd) == len;
4807 /* Assign file positions to the sections based on the mapping from
4808 sections to segments. This function also sets up some fields in
4812 assign_file_positions_for_load_sections (bfd *abfd,
4813 struct bfd_link_info *link_info)
4815 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4816 struct elf_segment_map *m;
4817 Elf_Internal_Phdr *phdrs;
4818 Elf_Internal_Phdr *p;
4820 bfd_size_type maxpagesize;
4823 bfd_vma header_pad = 0;
4825 if (link_info == NULL
4826 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4830 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4834 header_pad = m->header_size;
4839 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4840 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4844 /* PR binutils/12467. */
4845 elf_elfheader (abfd)->e_phoff = 0;
4846 elf_elfheader (abfd)->e_phentsize = 0;
4849 elf_elfheader (abfd)->e_phnum = alloc;
4851 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4852 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4854 BFD_ASSERT (elf_program_header_size (abfd)
4855 >= alloc * bed->s->sizeof_phdr);
4859 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4863 /* We're writing the size in elf_program_header_size (abfd),
4864 see assign_file_positions_except_relocs, so make sure we have
4865 that amount allocated, with trailing space cleared.
4866 The variable alloc contains the computed need, while
4867 elf_program_header_size (abfd) contains the size used for the
4869 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4870 where the layout is forced to according to a larger size in the
4871 last iterations for the testcase ld-elf/header. */
4872 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4874 phdrs = (Elf_Internal_Phdr *)
4876 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4877 sizeof (Elf_Internal_Phdr));
4878 elf_tdata (abfd)->phdr = phdrs;
4883 if ((abfd->flags & D_PAGED) != 0)
4884 maxpagesize = bed->maxpagesize;
4886 off = bed->s->sizeof_ehdr;
4887 off += alloc * bed->s->sizeof_phdr;
4888 if (header_pad < (bfd_vma) off)
4894 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4896 m = m->next, p++, j++)
4900 bfd_boolean no_contents;
4902 /* If elf_segment_map is not from map_sections_to_segments, the
4903 sections may not be correctly ordered. NOTE: sorting should
4904 not be done to the PT_NOTE section of a corefile, which may
4905 contain several pseudo-sections artificially created by bfd.
4906 Sorting these pseudo-sections breaks things badly. */
4908 && !(elf_elfheader (abfd)->e_type == ET_CORE
4909 && m->p_type == PT_NOTE))
4910 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4913 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4914 number of sections with contents contributing to both p_filesz
4915 and p_memsz, followed by a number of sections with no contents
4916 that just contribute to p_memsz. In this loop, OFF tracks next
4917 available file offset for PT_LOAD and PT_NOTE segments. */
4918 p->p_type = m->p_type;
4919 p->p_flags = m->p_flags;
4924 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4926 if (m->p_paddr_valid)
4927 p->p_paddr = m->p_paddr;
4928 else if (m->count == 0)
4931 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4933 if (p->p_type == PT_LOAD
4934 && (abfd->flags & D_PAGED) != 0)
4936 /* p_align in demand paged PT_LOAD segments effectively stores
4937 the maximum page size. When copying an executable with
4938 objcopy, we set m->p_align from the input file. Use this
4939 value for maxpagesize rather than bed->maxpagesize, which
4940 may be different. Note that we use maxpagesize for PT_TLS
4941 segment alignment later in this function, so we are relying
4942 on at least one PT_LOAD segment appearing before a PT_TLS
4944 if (m->p_align_valid)
4945 maxpagesize = m->p_align;
4947 p->p_align = maxpagesize;
4949 else if (m->p_align_valid)
4950 p->p_align = m->p_align;
4951 else if (m->count == 0)
4952 p->p_align = 1 << bed->s->log_file_align;
4956 no_contents = FALSE;
4958 if (p->p_type == PT_LOAD
4961 bfd_size_type align;
4962 unsigned int align_power = 0;
4964 if (m->p_align_valid)
4968 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4970 unsigned int secalign;
4972 secalign = bfd_get_section_alignment (abfd, *secpp);
4973 if (secalign > align_power)
4974 align_power = secalign;
4976 align = (bfd_size_type) 1 << align_power;
4977 if (align < maxpagesize)
4978 align = maxpagesize;
4981 for (i = 0; i < m->count; i++)
4982 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4983 /* If we aren't making room for this section, then
4984 it must be SHT_NOBITS regardless of what we've
4985 set via struct bfd_elf_special_section. */
4986 elf_section_type (m->sections[i]) = SHT_NOBITS;
4988 /* Find out whether this segment contains any loadable
4991 for (i = 0; i < m->count; i++)
4992 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4994 no_contents = FALSE;
4998 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5002 /* We shouldn't need to align the segment on disk since
5003 the segment doesn't need file space, but the gABI
5004 arguably requires the alignment and glibc ld.so
5005 checks it. So to comply with the alignment
5006 requirement but not waste file space, we adjust
5007 p_offset for just this segment. (OFF_ADJUST is
5008 subtracted from OFF later.) This may put p_offset
5009 past the end of file, but that shouldn't matter. */
5014 /* Make sure the .dynamic section is the first section in the
5015 PT_DYNAMIC segment. */
5016 else if (p->p_type == PT_DYNAMIC
5018 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5021 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5023 bfd_set_error (bfd_error_bad_value);
5026 /* Set the note section type to SHT_NOTE. */
5027 else if (p->p_type == PT_NOTE)
5028 for (i = 0; i < m->count; i++)
5029 elf_section_type (m->sections[i]) = SHT_NOTE;
5035 if (m->includes_filehdr)
5037 if (!m->p_flags_valid)
5039 p->p_filesz = bed->s->sizeof_ehdr;
5040 p->p_memsz = bed->s->sizeof_ehdr;
5043 if (p->p_vaddr < (bfd_vma) off)
5045 (*_bfd_error_handler)
5046 (_("%B: Not enough room for program headers, try linking with -N"),
5048 bfd_set_error (bfd_error_bad_value);
5053 if (!m->p_paddr_valid)
5058 if (m->includes_phdrs)
5060 if (!m->p_flags_valid)
5063 if (!m->includes_filehdr)
5065 p->p_offset = bed->s->sizeof_ehdr;
5069 p->p_vaddr -= off - p->p_offset;
5070 if (!m->p_paddr_valid)
5071 p->p_paddr -= off - p->p_offset;
5075 p->p_filesz += alloc * bed->s->sizeof_phdr;
5076 p->p_memsz += alloc * bed->s->sizeof_phdr;
5079 p->p_filesz += header_pad;
5080 p->p_memsz += header_pad;
5084 if (p->p_type == PT_LOAD
5085 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5087 if (!m->includes_filehdr && !m->includes_phdrs)
5093 adjust = off - (p->p_offset + p->p_filesz);
5095 p->p_filesz += adjust;
5096 p->p_memsz += adjust;
5100 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5101 maps. Set filepos for sections in PT_LOAD segments, and in
5102 core files, for sections in PT_NOTE segments.
5103 assign_file_positions_for_non_load_sections will set filepos
5104 for other sections and update p_filesz for other segments. */
5105 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5108 bfd_size_type align;
5109 Elf_Internal_Shdr *this_hdr;
5112 this_hdr = &elf_section_data (sec)->this_hdr;
5113 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5115 if ((p->p_type == PT_LOAD
5116 || p->p_type == PT_TLS)
5117 && (this_hdr->sh_type != SHT_NOBITS
5118 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5119 && ((this_hdr->sh_flags & SHF_TLS) == 0
5120 || p->p_type == PT_TLS))))
5122 bfd_vma p_start = p->p_paddr;
5123 bfd_vma p_end = p_start + p->p_memsz;
5124 bfd_vma s_start = sec->lma;
5125 bfd_vma adjust = s_start - p_end;
5129 || p_end < p_start))
5131 (*_bfd_error_handler)
5132 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
5133 (unsigned long) s_start, (unsigned long) p_end);
5137 p->p_memsz += adjust;
5139 if (this_hdr->sh_type != SHT_NOBITS)
5141 if (p->p_filesz + adjust < p->p_memsz)
5143 /* We have a PROGBITS section following NOBITS ones.
5144 Allocate file space for the NOBITS section(s) and
5146 adjust = p->p_memsz - p->p_filesz;
5147 if (!write_zeros (abfd, off, adjust))
5151 p->p_filesz += adjust;
5155 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5157 /* The section at i == 0 is the one that actually contains
5161 this_hdr->sh_offset = sec->filepos = off;
5162 off += this_hdr->sh_size;
5163 p->p_filesz = this_hdr->sh_size;
5169 /* The rest are fake sections that shouldn't be written. */
5178 if (p->p_type == PT_LOAD)
5180 this_hdr->sh_offset = sec->filepos = off;
5181 if (this_hdr->sh_type != SHT_NOBITS)
5182 off += this_hdr->sh_size;
5184 else if (this_hdr->sh_type == SHT_NOBITS
5185 && (this_hdr->sh_flags & SHF_TLS) != 0
5186 && this_hdr->sh_offset == 0)
5188 /* This is a .tbss section that didn't get a PT_LOAD.
5189 (See _bfd_elf_map_sections_to_segments "Create a
5190 final PT_LOAD".) Set sh_offset to the value it
5191 would have if we had created a zero p_filesz and
5192 p_memsz PT_LOAD header for the section. This
5193 also makes the PT_TLS header have the same
5195 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5197 this_hdr->sh_offset = sec->filepos = off + adjust;
5200 if (this_hdr->sh_type != SHT_NOBITS)
5202 p->p_filesz += this_hdr->sh_size;
5203 /* A load section without SHF_ALLOC is something like
5204 a note section in a PT_NOTE segment. These take
5205 file space but are not loaded into memory. */
5206 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5207 p->p_memsz += this_hdr->sh_size;
5209 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5211 if (p->p_type == PT_TLS)
5212 p->p_memsz += this_hdr->sh_size;
5214 /* .tbss is special. It doesn't contribute to p_memsz of
5216 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5217 p->p_memsz += this_hdr->sh_size;
5220 if (align > p->p_align
5221 && !m->p_align_valid
5222 && (p->p_type != PT_LOAD
5223 || (abfd->flags & D_PAGED) == 0))
5227 if (!m->p_flags_valid)
5230 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5232 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5239 /* Check that all sections are in a PT_LOAD segment.
5240 Don't check funky gdb generated core files. */
5241 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5243 bfd_boolean check_vma = TRUE;
5245 for (i = 1; i < m->count; i++)
5246 if (m->sections[i]->vma == m->sections[i - 1]->vma
5247 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5248 ->this_hdr), p) != 0
5249 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5250 ->this_hdr), p) != 0)
5252 /* Looks like we have overlays packed into the segment. */
5257 for (i = 0; i < m->count; i++)
5259 Elf_Internal_Shdr *this_hdr;
5262 sec = m->sections[i];
5263 this_hdr = &(elf_section_data(sec)->this_hdr);
5264 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5265 && !ELF_TBSS_SPECIAL (this_hdr, p))
5267 (*_bfd_error_handler)
5268 (_("%B: section `%A' can't be allocated in segment %d"),
5270 print_segment_map (m);
5276 elf_next_file_pos (abfd) = off;
5280 /* Assign file positions for the other sections. */
5283 assign_file_positions_for_non_load_sections (bfd *abfd,
5284 struct bfd_link_info *link_info)
5286 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5287 Elf_Internal_Shdr **i_shdrpp;
5288 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5289 Elf_Internal_Phdr *phdrs;
5290 Elf_Internal_Phdr *p;
5291 struct elf_segment_map *m;
5292 struct elf_segment_map *hdrs_segment;
5293 bfd_vma filehdr_vaddr, filehdr_paddr;
5294 bfd_vma phdrs_vaddr, phdrs_paddr;
5298 i_shdrpp = elf_elfsections (abfd);
5299 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5300 off = elf_next_file_pos (abfd);
5301 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5303 Elf_Internal_Shdr *hdr;
5306 if (hdr->bfd_section != NULL
5307 && (hdr->bfd_section->filepos != 0
5308 || (hdr->sh_type == SHT_NOBITS
5309 && hdr->contents == NULL)))
5310 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5311 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5313 if (hdr->sh_size != 0)
5314 (*_bfd_error_handler)
5315 (_("%B: warning: allocated section `%s' not in segment"),
5317 (hdr->bfd_section == NULL
5319 : hdr->bfd_section->name));
5320 /* We don't need to page align empty sections. */
5321 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5322 off += vma_page_aligned_bias (hdr->sh_addr, off,
5325 off += vma_page_aligned_bias (hdr->sh_addr, off,
5327 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5330 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5331 && hdr->bfd_section == NULL)
5332 || (hdr->bfd_section != NULL
5333 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5334 /* Compress DWARF debug sections. */
5335 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5336 || (elf_symtab_shndx_list (abfd) != NULL
5337 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5338 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5339 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5340 hdr->sh_offset = -1;
5342 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5345 /* Now that we have set the section file positions, we can set up
5346 the file positions for the non PT_LOAD segments. */
5350 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5352 hdrs_segment = NULL;
5353 phdrs = elf_tdata (abfd)->phdr;
5354 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5357 if (p->p_type != PT_LOAD)
5360 if (m->includes_filehdr)
5362 filehdr_vaddr = p->p_vaddr;
5363 filehdr_paddr = p->p_paddr;
5365 if (m->includes_phdrs)
5367 phdrs_vaddr = p->p_vaddr;
5368 phdrs_paddr = p->p_paddr;
5369 if (m->includes_filehdr)
5372 phdrs_vaddr += bed->s->sizeof_ehdr;
5373 phdrs_paddr += bed->s->sizeof_ehdr;
5378 if (hdrs_segment != NULL && link_info != NULL)
5380 /* There is a segment that contains both the file headers and the
5381 program headers, so provide a symbol __ehdr_start pointing there.
5382 A program can use this to examine itself robustly. */
5384 struct elf_link_hash_entry *hash
5385 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5386 FALSE, FALSE, TRUE);
5387 /* If the symbol was referenced and not defined, define it. */
5389 && (hash->root.type == bfd_link_hash_new
5390 || hash->root.type == bfd_link_hash_undefined
5391 || hash->root.type == bfd_link_hash_undefweak
5392 || hash->root.type == bfd_link_hash_common))
5395 if (hdrs_segment->count != 0)
5396 /* The segment contains sections, so use the first one. */
5397 s = hdrs_segment->sections[0];
5399 /* Use the first (i.e. lowest-addressed) section in any segment. */
5400 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5409 hash->root.u.def.value = filehdr_vaddr - s->vma;
5410 hash->root.u.def.section = s;
5414 hash->root.u.def.value = filehdr_vaddr;
5415 hash->root.u.def.section = bfd_abs_section_ptr;
5418 hash->root.type = bfd_link_hash_defined;
5419 hash->def_regular = 1;
5424 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5426 if (p->p_type == PT_GNU_RELRO)
5428 const Elf_Internal_Phdr *lp;
5429 struct elf_segment_map *lm;
5431 if (link_info != NULL)
5433 /* During linking the range of the RELRO segment is passed
5435 for (lm = elf_seg_map (abfd), lp = phdrs;
5437 lm = lm->next, lp++)
5439 if (lp->p_type == PT_LOAD
5440 && lp->p_vaddr < link_info->relro_end
5442 && lm->sections[0]->vma >= link_info->relro_start)
5446 BFD_ASSERT (lm != NULL);
5450 /* Otherwise we are copying an executable or shared
5451 library, but we need to use the same linker logic. */
5452 for (lp = phdrs; lp < phdrs + count; ++lp)
5454 if (lp->p_type == PT_LOAD
5455 && lp->p_paddr == p->p_paddr)
5460 if (lp < phdrs + count)
5462 p->p_vaddr = lp->p_vaddr;
5463 p->p_paddr = lp->p_paddr;
5464 p->p_offset = lp->p_offset;
5465 if (link_info != NULL)
5466 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5467 else if (m->p_size_valid)
5468 p->p_filesz = m->p_size;
5471 p->p_memsz = p->p_filesz;
5472 /* Preserve the alignment and flags if they are valid. The
5473 gold linker generates RW/4 for the PT_GNU_RELRO section.
5474 It is better for objcopy/strip to honor these attributes
5475 otherwise gdb will choke when using separate debug files.
5477 if (!m->p_align_valid)
5479 if (!m->p_flags_valid)
5484 memset (p, 0, sizeof *p);
5485 p->p_type = PT_NULL;
5488 else if (p->p_type == PT_GNU_STACK)
5490 if (m->p_size_valid)
5491 p->p_memsz = m->p_size;
5493 else if (m->count != 0)
5496 if (p->p_type != PT_LOAD
5497 && (p->p_type != PT_NOTE
5498 || bfd_get_format (abfd) != bfd_core))
5500 if (m->includes_filehdr || m->includes_phdrs)
5502 /* PR 17512: file: 2195325e. */
5503 (*_bfd_error_handler)
5504 (_("%B: warning: non-load segment includes file header and/or program header"),
5510 p->p_offset = m->sections[0]->filepos;
5511 for (i = m->count; i-- != 0;)
5513 asection *sect = m->sections[i];
5514 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5515 if (hdr->sh_type != SHT_NOBITS)
5517 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5524 else if (m->includes_filehdr)
5526 p->p_vaddr = filehdr_vaddr;
5527 if (! m->p_paddr_valid)
5528 p->p_paddr = filehdr_paddr;
5530 else if (m->includes_phdrs)
5532 p->p_vaddr = phdrs_vaddr;
5533 if (! m->p_paddr_valid)
5534 p->p_paddr = phdrs_paddr;
5538 elf_next_file_pos (abfd) = off;
5543 static elf_section_list *
5544 find_section_in_list (unsigned int i, elf_section_list * list)
5546 for (;list != NULL; list = list->next)
5552 /* Work out the file positions of all the sections. This is called by
5553 _bfd_elf_compute_section_file_positions. All the section sizes and
5554 VMAs must be known before this is called.
5556 Reloc sections come in two flavours: Those processed specially as
5557 "side-channel" data attached to a section to which they apply, and
5558 those that bfd doesn't process as relocations. The latter sort are
5559 stored in a normal bfd section by bfd_section_from_shdr. We don't
5560 consider the former sort here, unless they form part of the loadable
5561 image. Reloc sections not assigned here will be handled later by
5562 assign_file_positions_for_relocs.
5564 We also don't set the positions of the .symtab and .strtab here. */
5567 assign_file_positions_except_relocs (bfd *abfd,
5568 struct bfd_link_info *link_info)
5570 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5571 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5572 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5574 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5575 && bfd_get_format (abfd) != bfd_core)
5577 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5578 unsigned int num_sec = elf_numsections (abfd);
5579 Elf_Internal_Shdr **hdrpp;
5583 /* Start after the ELF header. */
5584 off = i_ehdrp->e_ehsize;
5586 /* We are not creating an executable, which means that we are
5587 not creating a program header, and that the actual order of
5588 the sections in the file is unimportant. */
5589 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5591 Elf_Internal_Shdr *hdr;
5594 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5595 && hdr->bfd_section == NULL)
5596 || (hdr->bfd_section != NULL
5597 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5598 /* Compress DWARF debug sections. */
5599 || i == elf_onesymtab (abfd)
5600 || (elf_symtab_shndx_list (abfd) != NULL
5601 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5602 || i == elf_strtab_sec (abfd)
5603 || i == elf_shstrtab_sec (abfd))
5605 hdr->sh_offset = -1;
5608 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5611 elf_next_file_pos (abfd) = off;
5617 /* Assign file positions for the loaded sections based on the
5618 assignment of sections to segments. */
5619 if (!assign_file_positions_for_load_sections (abfd, link_info))
5622 /* And for non-load sections. */
5623 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5626 if (bed->elf_backend_modify_program_headers != NULL)
5628 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5632 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5633 if (link_info != NULL && bfd_link_pie (link_info))
5635 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5636 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5637 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5639 /* Find the lowest p_vaddr in PT_LOAD segments. */
5640 bfd_vma p_vaddr = (bfd_vma) -1;
5641 for (; segment < end_segment; segment++)
5642 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5643 p_vaddr = segment->p_vaddr;
5645 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5646 segments is non-zero. */
5648 i_ehdrp->e_type = ET_EXEC;
5651 /* Write out the program headers. */
5652 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5653 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5654 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5662 prep_headers (bfd *abfd)
5664 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5665 struct elf_strtab_hash *shstrtab;
5666 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5668 i_ehdrp = elf_elfheader (abfd);
5670 shstrtab = _bfd_elf_strtab_init ();
5671 if (shstrtab == NULL)
5674 elf_shstrtab (abfd) = shstrtab;
5676 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5677 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5678 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5679 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5681 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5682 i_ehdrp->e_ident[EI_DATA] =
5683 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5684 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5686 if ((abfd->flags & DYNAMIC) != 0)
5687 i_ehdrp->e_type = ET_DYN;
5688 else if ((abfd->flags & EXEC_P) != 0)
5689 i_ehdrp->e_type = ET_EXEC;
5690 else if (bfd_get_format (abfd) == bfd_core)
5691 i_ehdrp->e_type = ET_CORE;
5693 i_ehdrp->e_type = ET_REL;
5695 switch (bfd_get_arch (abfd))
5697 case bfd_arch_unknown:
5698 i_ehdrp->e_machine = EM_NONE;
5701 /* There used to be a long list of cases here, each one setting
5702 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5703 in the corresponding bfd definition. To avoid duplication,
5704 the switch was removed. Machines that need special handling
5705 can generally do it in elf_backend_final_write_processing(),
5706 unless they need the information earlier than the final write.
5707 Such need can generally be supplied by replacing the tests for
5708 e_machine with the conditions used to determine it. */
5710 i_ehdrp->e_machine = bed->elf_machine_code;
5713 i_ehdrp->e_version = bed->s->ev_current;
5714 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5716 /* No program header, for now. */
5717 i_ehdrp->e_phoff = 0;
5718 i_ehdrp->e_phentsize = 0;
5719 i_ehdrp->e_phnum = 0;
5721 /* Each bfd section is section header entry. */
5722 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5723 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5725 /* If we're building an executable, we'll need a program header table. */
5726 if (abfd->flags & EXEC_P)
5727 /* It all happens later. */
5731 i_ehdrp->e_phentsize = 0;
5732 i_ehdrp->e_phoff = 0;
5735 elf_tdata (abfd)->symtab_hdr.sh_name =
5736 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5737 elf_tdata (abfd)->strtab_hdr.sh_name =
5738 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5739 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5740 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5741 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5742 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5743 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5749 /* Assign file positions for all the reloc sections which are not part
5750 of the loadable file image, and the file position of section headers. */
5753 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
5756 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
5757 Elf_Internal_Shdr *shdrp;
5758 Elf_Internal_Ehdr *i_ehdrp;
5759 const struct elf_backend_data *bed;
5761 off = elf_next_file_pos (abfd);
5763 shdrpp = elf_elfsections (abfd);
5764 end_shdrpp = shdrpp + elf_numsections (abfd);
5765 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
5768 if (shdrp->sh_offset == -1)
5770 asection *sec = shdrp->bfd_section;
5771 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
5772 || shdrp->sh_type == SHT_RELA);
5774 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
5778 const char *name = sec->name;
5779 struct bfd_elf_section_data *d;
5781 /* Compress DWARF debug sections. */
5782 if (!bfd_compress_section (abfd, sec,
5786 if (sec->compress_status == COMPRESS_SECTION_DONE
5787 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
5789 /* If section is compressed with zlib-gnu, convert
5790 section name from .debug_* to .zdebug_*. */
5792 = convert_debug_to_zdebug (abfd, name);
5793 if (new_name == NULL)
5797 /* Add setion name to section name section. */
5798 if (shdrp->sh_name != (unsigned int) -1)
5801 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
5803 d = elf_section_data (sec);
5805 /* Add reloc setion name to section name section. */
5807 && !_bfd_elf_set_reloc_sh_name (abfd,
5812 && !_bfd_elf_set_reloc_sh_name (abfd,
5817 /* Update section size and contents. */
5818 shdrp->sh_size = sec->size;
5819 shdrp->contents = sec->contents;
5820 shdrp->bfd_section->contents = NULL;
5822 off = _bfd_elf_assign_file_position_for_section (shdrp,
5829 /* Place section name section after DWARF debug sections have been
5831 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
5832 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
5833 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
5834 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5836 /* Place the section headers. */
5837 i_ehdrp = elf_elfheader (abfd);
5838 bed = get_elf_backend_data (abfd);
5839 off = align_file_position (off, 1 << bed->s->log_file_align);
5840 i_ehdrp->e_shoff = off;
5841 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5842 elf_next_file_pos (abfd) = off;
5848 _bfd_elf_write_object_contents (bfd *abfd)
5850 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5851 Elf_Internal_Shdr **i_shdrp;
5853 unsigned int count, num_sec;
5854 struct elf_obj_tdata *t;
5856 if (! abfd->output_has_begun
5857 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5860 i_shdrp = elf_elfsections (abfd);
5863 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5867 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
5870 /* After writing the headers, we need to write the sections too... */
5871 num_sec = elf_numsections (abfd);
5872 for (count = 1; count < num_sec; count++)
5874 i_shdrp[count]->sh_name
5875 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
5876 i_shdrp[count]->sh_name);
5877 if (bed->elf_backend_section_processing)
5878 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5879 if (i_shdrp[count]->contents)
5881 bfd_size_type amt = i_shdrp[count]->sh_size;
5883 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5884 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5889 /* Write out the section header names. */
5890 t = elf_tdata (abfd);
5891 if (elf_shstrtab (abfd) != NULL
5892 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5893 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5896 if (bed->elf_backend_final_write_processing)
5897 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5899 if (!bed->s->write_shdrs_and_ehdr (abfd))
5902 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5903 if (t->o->build_id.after_write_object_contents != NULL)
5904 return (*t->o->build_id.after_write_object_contents) (abfd);
5910 _bfd_elf_write_corefile_contents (bfd *abfd)
5912 /* Hopefully this can be done just like an object file. */
5913 return _bfd_elf_write_object_contents (abfd);
5916 /* Given a section, search the header to find them. */
5919 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5921 const struct elf_backend_data *bed;
5922 unsigned int sec_index;
5924 if (elf_section_data (asect) != NULL
5925 && elf_section_data (asect)->this_idx != 0)
5926 return elf_section_data (asect)->this_idx;
5928 if (bfd_is_abs_section (asect))
5929 sec_index = SHN_ABS;
5930 else if (bfd_is_com_section (asect))
5931 sec_index = SHN_COMMON;
5932 else if (bfd_is_und_section (asect))
5933 sec_index = SHN_UNDEF;
5935 sec_index = SHN_BAD;
5937 bed = get_elf_backend_data (abfd);
5938 if (bed->elf_backend_section_from_bfd_section)
5940 int retval = sec_index;
5942 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5946 if (sec_index == SHN_BAD)
5947 bfd_set_error (bfd_error_nonrepresentable_section);
5952 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5956 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5958 asymbol *asym_ptr = *asym_ptr_ptr;
5960 flagword flags = asym_ptr->flags;
5962 /* When gas creates relocations against local labels, it creates its
5963 own symbol for the section, but does put the symbol into the
5964 symbol chain, so udata is 0. When the linker is generating
5965 relocatable output, this section symbol may be for one of the
5966 input sections rather than the output section. */
5967 if (asym_ptr->udata.i == 0
5968 && (flags & BSF_SECTION_SYM)
5969 && asym_ptr->section)
5974 sec = asym_ptr->section;
5975 if (sec->owner != abfd && sec->output_section != NULL)
5976 sec = sec->output_section;
5977 if (sec->owner == abfd
5978 && (indx = sec->index) < elf_num_section_syms (abfd)
5979 && elf_section_syms (abfd)[indx] != NULL)
5980 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5983 idx = asym_ptr->udata.i;
5987 /* This case can occur when using --strip-symbol on a symbol
5988 which is used in a relocation entry. */
5989 (*_bfd_error_handler)
5990 (_("%B: symbol `%s' required but not present"),
5991 abfd, bfd_asymbol_name (asym_ptr));
5992 bfd_set_error (bfd_error_no_symbols);
5999 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6000 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
6008 /* Rewrite program header information. */
6011 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6013 Elf_Internal_Ehdr *iehdr;
6014 struct elf_segment_map *map;
6015 struct elf_segment_map *map_first;
6016 struct elf_segment_map **pointer_to_map;
6017 Elf_Internal_Phdr *segment;
6020 unsigned int num_segments;
6021 bfd_boolean phdr_included = FALSE;
6022 bfd_boolean p_paddr_valid;
6023 bfd_vma maxpagesize;
6024 struct elf_segment_map *phdr_adjust_seg = NULL;
6025 unsigned int phdr_adjust_num = 0;
6026 const struct elf_backend_data *bed;
6028 bed = get_elf_backend_data (ibfd);
6029 iehdr = elf_elfheader (ibfd);
6032 pointer_to_map = &map_first;
6034 num_segments = elf_elfheader (ibfd)->e_phnum;
6035 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6037 /* Returns the end address of the segment + 1. */
6038 #define SEGMENT_END(segment, start) \
6039 (start + (segment->p_memsz > segment->p_filesz \
6040 ? segment->p_memsz : segment->p_filesz))
6042 #define SECTION_SIZE(section, segment) \
6043 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6044 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6045 ? section->size : 0)
6047 /* Returns TRUE if the given section is contained within
6048 the given segment. VMA addresses are compared. */
6049 #define IS_CONTAINED_BY_VMA(section, segment) \
6050 (section->vma >= segment->p_vaddr \
6051 && (section->vma + SECTION_SIZE (section, segment) \
6052 <= (SEGMENT_END (segment, segment->p_vaddr))))
6054 /* Returns TRUE if the given section is contained within
6055 the given segment. LMA addresses are compared. */
6056 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6057 (section->lma >= base \
6058 && (section->lma + SECTION_SIZE (section, segment) \
6059 <= SEGMENT_END (segment, base)))
6061 /* Handle PT_NOTE segment. */
6062 #define IS_NOTE(p, s) \
6063 (p->p_type == PT_NOTE \
6064 && elf_section_type (s) == SHT_NOTE \
6065 && (bfd_vma) s->filepos >= p->p_offset \
6066 && ((bfd_vma) s->filepos + s->size \
6067 <= p->p_offset + p->p_filesz))
6069 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6071 #define IS_COREFILE_NOTE(p, s) \
6073 && bfd_get_format (ibfd) == bfd_core \
6077 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6078 linker, which generates a PT_INTERP section with p_vaddr and
6079 p_memsz set to 0. */
6080 #define IS_SOLARIS_PT_INTERP(p, s) \
6082 && p->p_paddr == 0 \
6083 && p->p_memsz == 0 \
6084 && p->p_filesz > 0 \
6085 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6087 && (bfd_vma) s->filepos >= p->p_offset \
6088 && ((bfd_vma) s->filepos + s->size \
6089 <= p->p_offset + p->p_filesz))
6091 /* Decide if the given section should be included in the given segment.
6092 A section will be included if:
6093 1. It is within the address space of the segment -- we use the LMA
6094 if that is set for the segment and the VMA otherwise,
6095 2. It is an allocated section or a NOTE section in a PT_NOTE
6097 3. There is an output section associated with it,
6098 4. The section has not already been allocated to a previous segment.
6099 5. PT_GNU_STACK segments do not include any sections.
6100 6. PT_TLS segment includes only SHF_TLS sections.
6101 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6102 8. PT_DYNAMIC should not contain empty sections at the beginning
6103 (with the possible exception of .dynamic). */
6104 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6105 ((((segment->p_paddr \
6106 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6107 : IS_CONTAINED_BY_VMA (section, segment)) \
6108 && (section->flags & SEC_ALLOC) != 0) \
6109 || IS_NOTE (segment, section)) \
6110 && segment->p_type != PT_GNU_STACK \
6111 && (segment->p_type != PT_TLS \
6112 || (section->flags & SEC_THREAD_LOCAL)) \
6113 && (segment->p_type == PT_LOAD \
6114 || segment->p_type == PT_TLS \
6115 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6116 && (segment->p_type != PT_DYNAMIC \
6117 || SECTION_SIZE (section, segment) > 0 \
6118 || (segment->p_paddr \
6119 ? segment->p_paddr != section->lma \
6120 : segment->p_vaddr != section->vma) \
6121 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6123 && !section->segment_mark)
6125 /* If the output section of a section in the input segment is NULL,
6126 it is removed from the corresponding output segment. */
6127 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6128 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6129 && section->output_section != NULL)
6131 /* Returns TRUE iff seg1 starts after the end of seg2. */
6132 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6133 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6135 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6136 their VMA address ranges and their LMA address ranges overlap.
6137 It is possible to have overlapping VMA ranges without overlapping LMA
6138 ranges. RedBoot images for example can have both .data and .bss mapped
6139 to the same VMA range, but with the .data section mapped to a different
6141 #define SEGMENT_OVERLAPS(seg1, seg2) \
6142 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6143 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6144 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6145 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6147 /* Initialise the segment mark field. */
6148 for (section = ibfd->sections; section != NULL; section = section->next)
6149 section->segment_mark = FALSE;
6151 /* The Solaris linker creates program headers in which all the
6152 p_paddr fields are zero. When we try to objcopy or strip such a
6153 file, we get confused. Check for this case, and if we find it
6154 don't set the p_paddr_valid fields. */
6155 p_paddr_valid = FALSE;
6156 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6159 if (segment->p_paddr != 0)
6161 p_paddr_valid = TRUE;
6165 /* Scan through the segments specified in the program header
6166 of the input BFD. For this first scan we look for overlaps
6167 in the loadable segments. These can be created by weird
6168 parameters to objcopy. Also, fix some solaris weirdness. */
6169 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6174 Elf_Internal_Phdr *segment2;
6176 if (segment->p_type == PT_INTERP)
6177 for (section = ibfd->sections; section; section = section->next)
6178 if (IS_SOLARIS_PT_INTERP (segment, section))
6180 /* Mininal change so that the normal section to segment
6181 assignment code will work. */
6182 segment->p_vaddr = section->vma;
6186 if (segment->p_type != PT_LOAD)
6188 /* Remove PT_GNU_RELRO segment. */
6189 if (segment->p_type == PT_GNU_RELRO)
6190 segment->p_type = PT_NULL;
6194 /* Determine if this segment overlaps any previous segments. */
6195 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6197 bfd_signed_vma extra_length;
6199 if (segment2->p_type != PT_LOAD
6200 || !SEGMENT_OVERLAPS (segment, segment2))
6203 /* Merge the two segments together. */
6204 if (segment2->p_vaddr < segment->p_vaddr)
6206 /* Extend SEGMENT2 to include SEGMENT and then delete
6208 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6209 - SEGMENT_END (segment2, segment2->p_vaddr));
6211 if (extra_length > 0)
6213 segment2->p_memsz += extra_length;
6214 segment2->p_filesz += extra_length;
6217 segment->p_type = PT_NULL;
6219 /* Since we have deleted P we must restart the outer loop. */
6221 segment = elf_tdata (ibfd)->phdr;
6226 /* Extend SEGMENT to include SEGMENT2 and then delete
6228 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6229 - SEGMENT_END (segment, segment->p_vaddr));
6231 if (extra_length > 0)
6233 segment->p_memsz += extra_length;
6234 segment->p_filesz += extra_length;
6237 segment2->p_type = PT_NULL;
6242 /* The second scan attempts to assign sections to segments. */
6243 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6247 unsigned int section_count;
6248 asection **sections;
6249 asection *output_section;
6251 bfd_vma matching_lma;
6252 bfd_vma suggested_lma;
6255 asection *first_section;
6256 bfd_boolean first_matching_lma;
6257 bfd_boolean first_suggested_lma;
6259 if (segment->p_type == PT_NULL)
6262 first_section = NULL;
6263 /* Compute how many sections might be placed into this segment. */
6264 for (section = ibfd->sections, section_count = 0;
6266 section = section->next)
6268 /* Find the first section in the input segment, which may be
6269 removed from the corresponding output segment. */
6270 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6272 if (first_section == NULL)
6273 first_section = section;
6274 if (section->output_section != NULL)
6279 /* Allocate a segment map big enough to contain
6280 all of the sections we have selected. */
6281 amt = sizeof (struct elf_segment_map);
6282 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6283 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6287 /* Initialise the fields of the segment map. Default to
6288 using the physical address of the segment in the input BFD. */
6290 map->p_type = segment->p_type;
6291 map->p_flags = segment->p_flags;
6292 map->p_flags_valid = 1;
6294 /* If the first section in the input segment is removed, there is
6295 no need to preserve segment physical address in the corresponding
6297 if (!first_section || first_section->output_section != NULL)
6299 map->p_paddr = segment->p_paddr;
6300 map->p_paddr_valid = p_paddr_valid;
6303 /* Determine if this segment contains the ELF file header
6304 and if it contains the program headers themselves. */
6305 map->includes_filehdr = (segment->p_offset == 0
6306 && segment->p_filesz >= iehdr->e_ehsize);
6307 map->includes_phdrs = 0;
6309 if (!phdr_included || segment->p_type != PT_LOAD)
6311 map->includes_phdrs =
6312 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6313 && (segment->p_offset + segment->p_filesz
6314 >= ((bfd_vma) iehdr->e_phoff
6315 + iehdr->e_phnum * iehdr->e_phentsize)));
6317 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6318 phdr_included = TRUE;
6321 if (section_count == 0)
6323 /* Special segments, such as the PT_PHDR segment, may contain
6324 no sections, but ordinary, loadable segments should contain
6325 something. They are allowed by the ELF spec however, so only
6326 a warning is produced. */
6327 if (segment->p_type == PT_LOAD)
6328 (*_bfd_error_handler) (_("\
6329 %B: warning: Empty loadable segment detected, is this intentional ?"),
6333 *pointer_to_map = map;
6334 pointer_to_map = &map->next;
6339 /* Now scan the sections in the input BFD again and attempt
6340 to add their corresponding output sections to the segment map.
6341 The problem here is how to handle an output section which has
6342 been moved (ie had its LMA changed). There are four possibilities:
6344 1. None of the sections have been moved.
6345 In this case we can continue to use the segment LMA from the
6348 2. All of the sections have been moved by the same amount.
6349 In this case we can change the segment's LMA to match the LMA
6350 of the first section.
6352 3. Some of the sections have been moved, others have not.
6353 In this case those sections which have not been moved can be
6354 placed in the current segment which will have to have its size,
6355 and possibly its LMA changed, and a new segment or segments will
6356 have to be created to contain the other sections.
6358 4. The sections have been moved, but not by the same amount.
6359 In this case we can change the segment's LMA to match the LMA
6360 of the first section and we will have to create a new segment
6361 or segments to contain the other sections.
6363 In order to save time, we allocate an array to hold the section
6364 pointers that we are interested in. As these sections get assigned
6365 to a segment, they are removed from this array. */
6367 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
6368 if (sections == NULL)
6371 /* Step One: Scan for segment vs section LMA conflicts.
6372 Also add the sections to the section array allocated above.
6373 Also add the sections to the current segment. In the common
6374 case, where the sections have not been moved, this means that
6375 we have completely filled the segment, and there is nothing
6380 first_matching_lma = TRUE;
6381 first_suggested_lma = TRUE;
6383 for (section = first_section, j = 0;
6385 section = section->next)
6387 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
6389 output_section = section->output_section;
6391 sections[j++] = section;
6393 /* The Solaris native linker always sets p_paddr to 0.
6394 We try to catch that case here, and set it to the
6395 correct value. Note - some backends require that
6396 p_paddr be left as zero. */
6398 && segment->p_vaddr != 0
6399 && !bed->want_p_paddr_set_to_zero
6401 && output_section->lma != 0
6402 && output_section->vma == (segment->p_vaddr
6403 + (map->includes_filehdr
6406 + (map->includes_phdrs
6408 * iehdr->e_phentsize)
6410 map->p_paddr = segment->p_vaddr;
6412 /* Match up the physical address of the segment with the
6413 LMA address of the output section. */
6414 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6415 || IS_COREFILE_NOTE (segment, section)
6416 || (bed->want_p_paddr_set_to_zero
6417 && IS_CONTAINED_BY_VMA (output_section, segment)))
6419 if (first_matching_lma || output_section->lma < matching_lma)
6421 matching_lma = output_section->lma;
6422 first_matching_lma = FALSE;
6425 /* We assume that if the section fits within the segment
6426 then it does not overlap any other section within that
6428 map->sections[isec++] = output_section;
6430 else if (first_suggested_lma)
6432 suggested_lma = output_section->lma;
6433 first_suggested_lma = FALSE;
6436 if (j == section_count)
6441 BFD_ASSERT (j == section_count);
6443 /* Step Two: Adjust the physical address of the current segment,
6445 if (isec == section_count)
6447 /* All of the sections fitted within the segment as currently
6448 specified. This is the default case. Add the segment to
6449 the list of built segments and carry on to process the next
6450 program header in the input BFD. */
6451 map->count = section_count;
6452 *pointer_to_map = map;
6453 pointer_to_map = &map->next;
6456 && !bed->want_p_paddr_set_to_zero
6457 && matching_lma != map->p_paddr
6458 && !map->includes_filehdr
6459 && !map->includes_phdrs)
6460 /* There is some padding before the first section in the
6461 segment. So, we must account for that in the output
6463 map->p_vaddr_offset = matching_lma - map->p_paddr;
6470 if (!first_matching_lma)
6472 /* At least one section fits inside the current segment.
6473 Keep it, but modify its physical address to match the
6474 LMA of the first section that fitted. */
6475 map->p_paddr = matching_lma;
6479 /* None of the sections fitted inside the current segment.
6480 Change the current segment's physical address to match
6481 the LMA of the first section. */
6482 map->p_paddr = suggested_lma;
6485 /* Offset the segment physical address from the lma
6486 to allow for space taken up by elf headers. */
6487 if (map->includes_filehdr)
6489 if (map->p_paddr >= iehdr->e_ehsize)
6490 map->p_paddr -= iehdr->e_ehsize;
6493 map->includes_filehdr = FALSE;
6494 map->includes_phdrs = FALSE;
6498 if (map->includes_phdrs)
6500 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6502 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6504 /* iehdr->e_phnum is just an estimate of the number
6505 of program headers that we will need. Make a note
6506 here of the number we used and the segment we chose
6507 to hold these headers, so that we can adjust the
6508 offset when we know the correct value. */
6509 phdr_adjust_num = iehdr->e_phnum;
6510 phdr_adjust_seg = map;
6513 map->includes_phdrs = FALSE;
6517 /* Step Three: Loop over the sections again, this time assigning
6518 those that fit to the current segment and removing them from the
6519 sections array; but making sure not to leave large gaps. Once all
6520 possible sections have been assigned to the current segment it is
6521 added to the list of built segments and if sections still remain
6522 to be assigned, a new segment is constructed before repeating
6529 first_suggested_lma = TRUE;
6531 /* Fill the current segment with sections that fit. */
6532 for (j = 0; j < section_count; j++)
6534 section = sections[j];
6536 if (section == NULL)
6539 output_section = section->output_section;
6541 BFD_ASSERT (output_section != NULL);
6543 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6544 || IS_COREFILE_NOTE (segment, section))
6546 if (map->count == 0)
6548 /* If the first section in a segment does not start at
6549 the beginning of the segment, then something is
6551 if (output_section->lma
6553 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6554 + (map->includes_phdrs
6555 ? iehdr->e_phnum * iehdr->e_phentsize
6563 prev_sec = map->sections[map->count - 1];
6565 /* If the gap between the end of the previous section
6566 and the start of this section is more than
6567 maxpagesize then we need to start a new segment. */
6568 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6570 < BFD_ALIGN (output_section->lma, maxpagesize))
6571 || (prev_sec->lma + prev_sec->size
6572 > output_section->lma))
6574 if (first_suggested_lma)
6576 suggested_lma = output_section->lma;
6577 first_suggested_lma = FALSE;
6584 map->sections[map->count++] = output_section;
6587 section->segment_mark = TRUE;
6589 else if (first_suggested_lma)
6591 suggested_lma = output_section->lma;
6592 first_suggested_lma = FALSE;
6596 BFD_ASSERT (map->count > 0);
6598 /* Add the current segment to the list of built segments. */
6599 *pointer_to_map = map;
6600 pointer_to_map = &map->next;
6602 if (isec < section_count)
6604 /* We still have not allocated all of the sections to
6605 segments. Create a new segment here, initialise it
6606 and carry on looping. */
6607 amt = sizeof (struct elf_segment_map);
6608 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6609 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6616 /* Initialise the fields of the segment map. Set the physical
6617 physical address to the LMA of the first section that has
6618 not yet been assigned. */
6620 map->p_type = segment->p_type;
6621 map->p_flags = segment->p_flags;
6622 map->p_flags_valid = 1;
6623 map->p_paddr = suggested_lma;
6624 map->p_paddr_valid = p_paddr_valid;
6625 map->includes_filehdr = 0;
6626 map->includes_phdrs = 0;
6629 while (isec < section_count);
6634 elf_seg_map (obfd) = map_first;
6636 /* If we had to estimate the number of program headers that were
6637 going to be needed, then check our estimate now and adjust
6638 the offset if necessary. */
6639 if (phdr_adjust_seg != NULL)
6643 for (count = 0, map = map_first; map != NULL; map = map->next)
6646 if (count > phdr_adjust_num)
6647 phdr_adjust_seg->p_paddr
6648 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6653 #undef IS_CONTAINED_BY_VMA
6654 #undef IS_CONTAINED_BY_LMA
6656 #undef IS_COREFILE_NOTE
6657 #undef IS_SOLARIS_PT_INTERP
6658 #undef IS_SECTION_IN_INPUT_SEGMENT
6659 #undef INCLUDE_SECTION_IN_SEGMENT
6660 #undef SEGMENT_AFTER_SEGMENT
6661 #undef SEGMENT_OVERLAPS
6665 /* Copy ELF program header information. */
6668 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6670 Elf_Internal_Ehdr *iehdr;
6671 struct elf_segment_map *map;
6672 struct elf_segment_map *map_first;
6673 struct elf_segment_map **pointer_to_map;
6674 Elf_Internal_Phdr *segment;
6676 unsigned int num_segments;
6677 bfd_boolean phdr_included = FALSE;
6678 bfd_boolean p_paddr_valid;
6680 iehdr = elf_elfheader (ibfd);
6683 pointer_to_map = &map_first;
6685 /* If all the segment p_paddr fields are zero, don't set
6686 map->p_paddr_valid. */
6687 p_paddr_valid = FALSE;
6688 num_segments = elf_elfheader (ibfd)->e_phnum;
6689 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6692 if (segment->p_paddr != 0)
6694 p_paddr_valid = TRUE;
6698 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6703 unsigned int section_count;
6705 Elf_Internal_Shdr *this_hdr;
6706 asection *first_section = NULL;
6707 asection *lowest_section;
6709 /* Compute how many sections are in this segment. */
6710 for (section = ibfd->sections, section_count = 0;
6712 section = section->next)
6714 this_hdr = &(elf_section_data(section)->this_hdr);
6715 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6717 if (first_section == NULL)
6718 first_section = section;
6723 /* Allocate a segment map big enough to contain
6724 all of the sections we have selected. */
6725 amt = sizeof (struct elf_segment_map);
6726 if (section_count != 0)
6727 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6728 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6732 /* Initialize the fields of the output segment map with the
6735 map->p_type = segment->p_type;
6736 map->p_flags = segment->p_flags;
6737 map->p_flags_valid = 1;
6738 map->p_paddr = segment->p_paddr;
6739 map->p_paddr_valid = p_paddr_valid;
6740 map->p_align = segment->p_align;
6741 map->p_align_valid = 1;
6742 map->p_vaddr_offset = 0;
6744 if (map->p_type == PT_GNU_RELRO
6745 || map->p_type == PT_GNU_STACK)
6747 /* The PT_GNU_RELRO segment may contain the first a few
6748 bytes in the .got.plt section even if the whole .got.plt
6749 section isn't in the PT_GNU_RELRO segment. We won't
6750 change the size of the PT_GNU_RELRO segment.
6751 Similarly, PT_GNU_STACK size is significant on uclinux
6753 map->p_size = segment->p_memsz;
6754 map->p_size_valid = 1;
6757 /* Determine if this segment contains the ELF file header
6758 and if it contains the program headers themselves. */
6759 map->includes_filehdr = (segment->p_offset == 0
6760 && segment->p_filesz >= iehdr->e_ehsize);
6762 map->includes_phdrs = 0;
6763 if (! phdr_included || segment->p_type != PT_LOAD)
6765 map->includes_phdrs =
6766 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6767 && (segment->p_offset + segment->p_filesz
6768 >= ((bfd_vma) iehdr->e_phoff
6769 + iehdr->e_phnum * iehdr->e_phentsize)));
6771 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6772 phdr_included = TRUE;
6775 lowest_section = NULL;
6776 if (section_count != 0)
6778 unsigned int isec = 0;
6780 for (section = first_section;
6782 section = section->next)
6784 this_hdr = &(elf_section_data(section)->this_hdr);
6785 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6787 map->sections[isec++] = section->output_section;
6788 if ((section->flags & SEC_ALLOC) != 0)
6792 if (lowest_section == NULL
6793 || section->lma < lowest_section->lma)
6794 lowest_section = section;
6796 /* Section lmas are set up from PT_LOAD header
6797 p_paddr in _bfd_elf_make_section_from_shdr.
6798 If this header has a p_paddr that disagrees
6799 with the section lma, flag the p_paddr as
6801 if ((section->flags & SEC_LOAD) != 0)
6802 seg_off = this_hdr->sh_offset - segment->p_offset;
6804 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6805 if (section->lma - segment->p_paddr != seg_off)
6806 map->p_paddr_valid = FALSE;
6808 if (isec == section_count)
6814 if (map->includes_filehdr && lowest_section != NULL)
6815 /* We need to keep the space used by the headers fixed. */
6816 map->header_size = lowest_section->vma - segment->p_vaddr;
6818 if (!map->includes_phdrs
6819 && !map->includes_filehdr
6820 && map->p_paddr_valid)
6821 /* There is some other padding before the first section. */
6822 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6823 - segment->p_paddr);
6825 map->count = section_count;
6826 *pointer_to_map = map;
6827 pointer_to_map = &map->next;
6830 elf_seg_map (obfd) = map_first;
6834 /* Copy private BFD data. This copies or rewrites ELF program header
6838 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6840 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6841 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6844 if (elf_tdata (ibfd)->phdr == NULL)
6847 if (ibfd->xvec == obfd->xvec)
6849 /* Check to see if any sections in the input BFD
6850 covered by ELF program header have changed. */
6851 Elf_Internal_Phdr *segment;
6852 asection *section, *osec;
6853 unsigned int i, num_segments;
6854 Elf_Internal_Shdr *this_hdr;
6855 const struct elf_backend_data *bed;
6857 bed = get_elf_backend_data (ibfd);
6859 /* Regenerate the segment map if p_paddr is set to 0. */
6860 if (bed->want_p_paddr_set_to_zero)
6863 /* Initialize the segment mark field. */
6864 for (section = obfd->sections; section != NULL;
6865 section = section->next)
6866 section->segment_mark = FALSE;
6868 num_segments = elf_elfheader (ibfd)->e_phnum;
6869 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6873 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6874 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6875 which severly confuses things, so always regenerate the segment
6876 map in this case. */
6877 if (segment->p_paddr == 0
6878 && segment->p_memsz == 0
6879 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6882 for (section = ibfd->sections;
6883 section != NULL; section = section->next)
6885 /* We mark the output section so that we know it comes
6886 from the input BFD. */
6887 osec = section->output_section;
6889 osec->segment_mark = TRUE;
6891 /* Check if this section is covered by the segment. */
6892 this_hdr = &(elf_section_data(section)->this_hdr);
6893 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6895 /* FIXME: Check if its output section is changed or
6896 removed. What else do we need to check? */
6898 || section->flags != osec->flags
6899 || section->lma != osec->lma
6900 || section->vma != osec->vma
6901 || section->size != osec->size
6902 || section->rawsize != osec->rawsize
6903 || section->alignment_power != osec->alignment_power)
6909 /* Check to see if any output section do not come from the
6911 for (section = obfd->sections; section != NULL;
6912 section = section->next)
6914 if (section->segment_mark == FALSE)
6917 section->segment_mark = FALSE;
6920 return copy_elf_program_header (ibfd, obfd);
6924 if (ibfd->xvec == obfd->xvec)
6926 /* When rewriting program header, set the output maxpagesize to
6927 the maximum alignment of input PT_LOAD segments. */
6928 Elf_Internal_Phdr *segment;
6930 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6931 bfd_vma maxpagesize = 0;
6933 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6936 if (segment->p_type == PT_LOAD
6937 && maxpagesize < segment->p_align)
6939 /* PR 17512: file: f17299af. */
6940 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
6941 (*_bfd_error_handler) (_("\
6942 %B: warning: segment alignment of 0x%llx is too large"),
6943 ibfd, (long long) segment->p_align);
6945 maxpagesize = segment->p_align;
6948 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6949 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6952 return rewrite_elf_program_header (ibfd, obfd);
6955 /* Initialize private output section information from input section. */
6958 _bfd_elf_init_private_section_data (bfd *ibfd,
6962 struct bfd_link_info *link_info)
6965 Elf_Internal_Shdr *ihdr, *ohdr;
6966 bfd_boolean final_link = (link_info != NULL
6967 && !bfd_link_relocatable (link_info));
6969 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6970 || obfd->xvec->flavour != bfd_target_elf_flavour)
6973 BFD_ASSERT (elf_section_data (osec) != NULL);
6975 /* For objcopy and relocatable link, don't copy the output ELF
6976 section type from input if the output BFD section flags have been
6977 set to something different. For a final link allow some flags
6978 that the linker clears to differ. */
6979 if (elf_section_type (osec) == SHT_NULL
6980 && (osec->flags == isec->flags
6982 && ((osec->flags ^ isec->flags)
6983 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6984 elf_section_type (osec) = elf_section_type (isec);
6986 /* FIXME: Is this correct for all OS/PROC specific flags? */
6987 elf_section_flags (osec) |= (elf_section_flags (isec)
6988 & (SHF_MASKOS | SHF_MASKPROC));
6990 /* Set things up for objcopy and relocatable link. The output
6991 SHT_GROUP section will have its elf_next_in_group pointing back
6992 to the input group members. Ignore linker created group section.
6993 See elfNN_ia64_object_p in elfxx-ia64.c. */
6996 if (elf_sec_group (isec) == NULL
6997 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6999 if (elf_section_flags (isec) & SHF_GROUP)
7000 elf_section_flags (osec) |= SHF_GROUP;
7001 elf_next_in_group (osec) = elf_next_in_group (isec);
7002 elf_section_data (osec)->group = elf_section_data (isec)->group;
7005 /* If not decompress, preserve SHF_COMPRESSED. */
7006 if ((ibfd->flags & BFD_DECOMPRESS) == 0)
7007 elf_section_flags (osec) |= (elf_section_flags (isec)
7011 ihdr = &elf_section_data (isec)->this_hdr;
7013 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7014 don't use the output section of the linked-to section since it
7015 may be NULL at this point. */
7016 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7018 ohdr = &elf_section_data (osec)->this_hdr;
7019 ohdr->sh_flags |= SHF_LINK_ORDER;
7020 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7023 osec->use_rela_p = isec->use_rela_p;
7028 /* Copy private section information. This copies over the entsize
7029 field, and sometimes the info field. */
7032 _bfd_elf_copy_private_section_data (bfd *ibfd,
7037 Elf_Internal_Shdr *ihdr, *ohdr;
7039 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7040 || obfd->xvec->flavour != bfd_target_elf_flavour)
7043 ihdr = &elf_section_data (isec)->this_hdr;
7044 ohdr = &elf_section_data (osec)->this_hdr;
7046 ohdr->sh_entsize = ihdr->sh_entsize;
7048 if (ihdr->sh_type == SHT_SYMTAB
7049 || ihdr->sh_type == SHT_DYNSYM
7050 || ihdr->sh_type == SHT_GNU_verneed
7051 || ihdr->sh_type == SHT_GNU_verdef)
7052 ohdr->sh_info = ihdr->sh_info;
7054 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7058 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7059 necessary if we are removing either the SHT_GROUP section or any of
7060 the group member sections. DISCARDED is the value that a section's
7061 output_section has if the section will be discarded, NULL when this
7062 function is called from objcopy, bfd_abs_section_ptr when called
7066 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7070 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7071 if (elf_section_type (isec) == SHT_GROUP)
7073 asection *first = elf_next_in_group (isec);
7074 asection *s = first;
7075 bfd_size_type removed = 0;
7079 /* If this member section is being output but the
7080 SHT_GROUP section is not, then clear the group info
7081 set up by _bfd_elf_copy_private_section_data. */
7082 if (s->output_section != discarded
7083 && isec->output_section == discarded)
7085 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7086 elf_group_name (s->output_section) = NULL;
7088 /* Conversely, if the member section is not being output
7089 but the SHT_GROUP section is, then adjust its size. */
7090 else if (s->output_section == discarded
7091 && isec->output_section != discarded)
7093 s = elf_next_in_group (s);
7099 if (discarded != NULL)
7101 /* If we've been called for ld -r, then we need to
7102 adjust the input section size. This function may
7103 be called multiple times, so save the original
7105 if (isec->rawsize == 0)
7106 isec->rawsize = isec->size;
7107 isec->size = isec->rawsize - removed;
7111 /* Adjust the output section size when called from
7113 isec->output_section->size -= removed;
7121 /* Copy private header information. */
7124 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7126 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7127 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7130 /* Copy over private BFD data if it has not already been copied.
7131 This must be done here, rather than in the copy_private_bfd_data
7132 entry point, because the latter is called after the section
7133 contents have been set, which means that the program headers have
7134 already been worked out. */
7135 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7137 if (! copy_private_bfd_data (ibfd, obfd))
7141 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7144 /* Copy private symbol information. If this symbol is in a section
7145 which we did not map into a BFD section, try to map the section
7146 index correctly. We use special macro definitions for the mapped
7147 section indices; these definitions are interpreted by the
7148 swap_out_syms function. */
7150 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7151 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7152 #define MAP_STRTAB (SHN_HIOS + 3)
7153 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7154 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7157 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7162 elf_symbol_type *isym, *osym;
7164 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7165 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7168 isym = elf_symbol_from (ibfd, isymarg);
7169 osym = elf_symbol_from (obfd, osymarg);
7172 && isym->internal_elf_sym.st_shndx != 0
7174 && bfd_is_abs_section (isym->symbol.section))
7178 shndx = isym->internal_elf_sym.st_shndx;
7179 if (shndx == elf_onesymtab (ibfd))
7180 shndx = MAP_ONESYMTAB;
7181 else if (shndx == elf_dynsymtab (ibfd))
7182 shndx = MAP_DYNSYMTAB;
7183 else if (shndx == elf_strtab_sec (ibfd))
7185 else if (shndx == elf_shstrtab_sec (ibfd))
7186 shndx = MAP_SHSTRTAB;
7187 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7188 shndx = MAP_SYM_SHNDX;
7189 osym->internal_elf_sym.st_shndx = shndx;
7195 /* Swap out the symbols. */
7198 swap_out_syms (bfd *abfd,
7199 struct elf_strtab_hash **sttp,
7202 const struct elf_backend_data *bed;
7205 struct elf_strtab_hash *stt;
7206 Elf_Internal_Shdr *symtab_hdr;
7207 Elf_Internal_Shdr *symtab_shndx_hdr;
7208 Elf_Internal_Shdr *symstrtab_hdr;
7209 struct elf_sym_strtab *symstrtab;
7210 bfd_byte *outbound_syms;
7211 bfd_byte *outbound_shndx;
7212 unsigned long outbound_syms_index;
7213 unsigned long outbound_shndx_index;
7215 unsigned int num_locals;
7217 bfd_boolean name_local_sections;
7219 if (!elf_map_symbols (abfd, &num_locals))
7222 /* Dump out the symtabs. */
7223 stt = _bfd_elf_strtab_init ();
7227 bed = get_elf_backend_data (abfd);
7228 symcount = bfd_get_symcount (abfd);
7229 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7230 symtab_hdr->sh_type = SHT_SYMTAB;
7231 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7232 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7233 symtab_hdr->sh_info = num_locals + 1;
7234 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7236 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7237 symstrtab_hdr->sh_type = SHT_STRTAB;
7239 /* Allocate buffer to swap out the .strtab section. */
7240 symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
7241 * sizeof (*symstrtab));
7242 if (symstrtab == NULL)
7244 _bfd_elf_strtab_free (stt);
7248 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7249 bed->s->sizeof_sym);
7250 if (outbound_syms == NULL)
7253 _bfd_elf_strtab_free (stt);
7257 symtab_hdr->contents = outbound_syms;
7258 outbound_syms_index = 0;
7260 outbound_shndx = NULL;
7261 outbound_shndx_index = 0;
7263 if (elf_symtab_shndx_list (abfd))
7265 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7266 if (symtab_shndx_hdr->sh_name != 0)
7268 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7269 outbound_shndx = (bfd_byte *)
7270 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7271 if (outbound_shndx == NULL)
7274 symtab_shndx_hdr->contents = outbound_shndx;
7275 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7276 symtab_shndx_hdr->sh_size = amt;
7277 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7278 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7280 /* FIXME: What about any other headers in the list ? */
7283 /* Now generate the data (for "contents"). */
7285 /* Fill in zeroth symbol and swap it out. */
7286 Elf_Internal_Sym sym;
7292 sym.st_shndx = SHN_UNDEF;
7293 sym.st_target_internal = 0;
7294 symstrtab[0].sym = sym;
7295 symstrtab[0].dest_index = outbound_syms_index;
7296 symstrtab[0].destshndx_index = outbound_shndx_index;
7297 outbound_syms_index++;
7298 if (outbound_shndx != NULL)
7299 outbound_shndx_index++;
7303 = (bed->elf_backend_name_local_section_symbols
7304 && bed->elf_backend_name_local_section_symbols (abfd));
7306 syms = bfd_get_outsymbols (abfd);
7307 for (idx = 0; idx < symcount;)
7309 Elf_Internal_Sym sym;
7310 bfd_vma value = syms[idx]->value;
7311 elf_symbol_type *type_ptr;
7312 flagword flags = syms[idx]->flags;
7315 if (!name_local_sections
7316 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
7318 /* Local section symbols have no name. */
7319 sym.st_name = (unsigned long) -1;
7323 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7324 to get the final offset for st_name. */
7326 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
7328 if (sym.st_name == (unsigned long) -1)
7332 type_ptr = elf_symbol_from (abfd, syms[idx]);
7334 if ((flags & BSF_SECTION_SYM) == 0
7335 && bfd_is_com_section (syms[idx]->section))
7337 /* ELF common symbols put the alignment into the `value' field,
7338 and the size into the `size' field. This is backwards from
7339 how BFD handles it, so reverse it here. */
7340 sym.st_size = value;
7341 if (type_ptr == NULL
7342 || type_ptr->internal_elf_sym.st_value == 0)
7343 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
7345 sym.st_value = type_ptr->internal_elf_sym.st_value;
7346 sym.st_shndx = _bfd_elf_section_from_bfd_section
7347 (abfd, syms[idx]->section);
7351 asection *sec = syms[idx]->section;
7354 if (sec->output_section)
7356 value += sec->output_offset;
7357 sec = sec->output_section;
7360 /* Don't add in the section vma for relocatable output. */
7361 if (! relocatable_p)
7363 sym.st_value = value;
7364 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
7366 if (bfd_is_abs_section (sec)
7368 && type_ptr->internal_elf_sym.st_shndx != 0)
7370 /* This symbol is in a real ELF section which we did
7371 not create as a BFD section. Undo the mapping done
7372 by copy_private_symbol_data. */
7373 shndx = type_ptr->internal_elf_sym.st_shndx;
7377 shndx = elf_onesymtab (abfd);
7380 shndx = elf_dynsymtab (abfd);
7383 shndx = elf_strtab_sec (abfd);
7386 shndx = elf_shstrtab_sec (abfd);
7389 if (elf_symtab_shndx_list (abfd))
7390 shndx = elf_symtab_shndx_list (abfd)->ndx;
7399 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
7401 if (shndx == SHN_BAD)
7405 /* Writing this would be a hell of a lot easier if
7406 we had some decent documentation on bfd, and
7407 knew what to expect of the library, and what to
7408 demand of applications. For example, it
7409 appears that `objcopy' might not set the
7410 section of a symbol to be a section that is
7411 actually in the output file. */
7412 sec2 = bfd_get_section_by_name (abfd, sec->name);
7415 _bfd_error_handler (_("\
7416 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7417 syms[idx]->name ? syms[idx]->name : "<Local sym>",
7419 bfd_set_error (bfd_error_invalid_operation);
7423 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7424 BFD_ASSERT (shndx != SHN_BAD);
7428 sym.st_shndx = shndx;
7431 if ((flags & BSF_THREAD_LOCAL) != 0)
7433 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7434 type = STT_GNU_IFUNC;
7435 else if ((flags & BSF_FUNCTION) != 0)
7437 else if ((flags & BSF_OBJECT) != 0)
7439 else if ((flags & BSF_RELC) != 0)
7441 else if ((flags & BSF_SRELC) != 0)
7446 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7449 /* Processor-specific types. */
7450 if (type_ptr != NULL
7451 && bed->elf_backend_get_symbol_type)
7452 type = ((*bed->elf_backend_get_symbol_type)
7453 (&type_ptr->internal_elf_sym, type));
7455 if (flags & BSF_SECTION_SYM)
7457 if (flags & BSF_GLOBAL)
7458 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7460 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7462 else if (bfd_is_com_section (syms[idx]->section))
7464 #ifdef USE_STT_COMMON
7465 if (type == STT_OBJECT)
7466 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
7469 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7471 else if (bfd_is_und_section (syms[idx]->section))
7472 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7476 else if (flags & BSF_FILE)
7477 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7480 int bind = STB_LOCAL;
7482 if (flags & BSF_LOCAL)
7484 else if (flags & BSF_GNU_UNIQUE)
7485 bind = STB_GNU_UNIQUE;
7486 else if (flags & BSF_WEAK)
7488 else if (flags & BSF_GLOBAL)
7491 sym.st_info = ELF_ST_INFO (bind, type);
7494 if (type_ptr != NULL)
7496 sym.st_other = type_ptr->internal_elf_sym.st_other;
7497 sym.st_target_internal
7498 = type_ptr->internal_elf_sym.st_target_internal;
7503 sym.st_target_internal = 0;
7507 symstrtab[idx].sym = sym;
7508 symstrtab[idx].dest_index = outbound_syms_index;
7509 symstrtab[idx].destshndx_index = outbound_shndx_index;
7511 outbound_syms_index++;
7512 if (outbound_shndx != NULL)
7513 outbound_shndx_index++;
7516 /* Finalize the .strtab section. */
7517 _bfd_elf_strtab_finalize (stt);
7519 /* Swap out the .strtab section. */
7520 for (idx = 0; idx <= symcount; idx++)
7522 struct elf_sym_strtab *elfsym = &symstrtab[idx];
7523 if (elfsym->sym.st_name == (unsigned long) -1)
7524 elfsym->sym.st_name = 0;
7526 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
7527 elfsym->sym.st_name);
7528 bed->s->swap_symbol_out (abfd, &elfsym->sym,
7530 + (elfsym->dest_index
7531 * bed->s->sizeof_sym)),
7533 + (elfsym->destshndx_index
7534 * sizeof (Elf_External_Sym_Shndx))));
7539 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
7540 symstrtab_hdr->sh_type = SHT_STRTAB;
7542 symstrtab_hdr->sh_flags = 0;
7543 symstrtab_hdr->sh_addr = 0;
7544 symstrtab_hdr->sh_entsize = 0;
7545 symstrtab_hdr->sh_link = 0;
7546 symstrtab_hdr->sh_info = 0;
7547 symstrtab_hdr->sh_addralign = 1;
7552 /* Return the number of bytes required to hold the symtab vector.
7554 Note that we base it on the count plus 1, since we will null terminate
7555 the vector allocated based on this size. However, the ELF symbol table
7556 always has a dummy entry as symbol #0, so it ends up even. */
7559 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
7563 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7565 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7566 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7568 symtab_size -= sizeof (asymbol *);
7574 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7578 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7580 if (elf_dynsymtab (abfd) == 0)
7582 bfd_set_error (bfd_error_invalid_operation);
7586 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7587 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7589 symtab_size -= sizeof (asymbol *);
7595 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7598 return (asect->reloc_count + 1) * sizeof (arelent *);
7601 /* Canonicalize the relocs. */
7604 _bfd_elf_canonicalize_reloc (bfd *abfd,
7611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7613 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7616 tblptr = section->relocation;
7617 for (i = 0; i < section->reloc_count; i++)
7618 *relptr++ = tblptr++;
7622 return section->reloc_count;
7626 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7629 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7632 bfd_get_symcount (abfd) = symcount;
7637 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7638 asymbol **allocation)
7640 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7641 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7644 bfd_get_dynamic_symcount (abfd) = symcount;
7648 /* Return the size required for the dynamic reloc entries. Any loadable
7649 section that was actually installed in the BFD, and has type SHT_REL
7650 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7651 dynamic reloc section. */
7654 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7659 if (elf_dynsymtab (abfd) == 0)
7661 bfd_set_error (bfd_error_invalid_operation);
7665 ret = sizeof (arelent *);
7666 for (s = abfd->sections; s != NULL; s = s->next)
7667 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7668 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7669 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7670 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7671 * sizeof (arelent *));
7676 /* Canonicalize the dynamic relocation entries. Note that we return the
7677 dynamic relocations as a single block, although they are actually
7678 associated with particular sections; the interface, which was
7679 designed for SunOS style shared libraries, expects that there is only
7680 one set of dynamic relocs. Any loadable section that was actually
7681 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7682 dynamic symbol table, is considered to be a dynamic reloc section. */
7685 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7689 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7693 if (elf_dynsymtab (abfd) == 0)
7695 bfd_set_error (bfd_error_invalid_operation);
7699 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7701 for (s = abfd->sections; s != NULL; s = s->next)
7703 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7704 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7705 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7710 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7712 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7714 for (i = 0; i < count; i++)
7725 /* Read in the version information. */
7728 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7730 bfd_byte *contents = NULL;
7731 unsigned int freeidx = 0;
7733 if (elf_dynverref (abfd) != 0)
7735 Elf_Internal_Shdr *hdr;
7736 Elf_External_Verneed *everneed;
7737 Elf_Internal_Verneed *iverneed;
7739 bfd_byte *contents_end;
7741 hdr = &elf_tdata (abfd)->dynverref_hdr;
7743 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed))
7745 error_return_bad_verref:
7746 (*_bfd_error_handler)
7747 (_("%B: .gnu.version_r invalid entry"), abfd);
7748 bfd_set_error (bfd_error_bad_value);
7749 error_return_verref:
7750 elf_tdata (abfd)->verref = NULL;
7751 elf_tdata (abfd)->cverrefs = 0;
7755 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7756 if (contents == NULL)
7757 goto error_return_verref;
7759 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7760 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7761 goto error_return_verref;
7763 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7764 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7766 if (elf_tdata (abfd)->verref == NULL)
7767 goto error_return_verref;
7769 BFD_ASSERT (sizeof (Elf_External_Verneed)
7770 == sizeof (Elf_External_Vernaux));
7771 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7772 everneed = (Elf_External_Verneed *) contents;
7773 iverneed = elf_tdata (abfd)->verref;
7774 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7776 Elf_External_Vernaux *evernaux;
7777 Elf_Internal_Vernaux *ivernaux;
7780 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7782 iverneed->vn_bfd = abfd;
7784 iverneed->vn_filename =
7785 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7787 if (iverneed->vn_filename == NULL)
7788 goto error_return_bad_verref;
7790 if (iverneed->vn_cnt == 0)
7791 iverneed->vn_auxptr = NULL;
7794 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7795 bfd_alloc2 (abfd, iverneed->vn_cnt,
7796 sizeof (Elf_Internal_Vernaux));
7797 if (iverneed->vn_auxptr == NULL)
7798 goto error_return_verref;
7801 if (iverneed->vn_aux
7802 > (size_t) (contents_end - (bfd_byte *) everneed))
7803 goto error_return_bad_verref;
7805 evernaux = ((Elf_External_Vernaux *)
7806 ((bfd_byte *) everneed + iverneed->vn_aux));
7807 ivernaux = iverneed->vn_auxptr;
7808 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7810 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7812 ivernaux->vna_nodename =
7813 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7814 ivernaux->vna_name);
7815 if (ivernaux->vna_nodename == NULL)
7816 goto error_return_bad_verref;
7818 if (ivernaux->vna_other > freeidx)
7819 freeidx = ivernaux->vna_other;
7821 ivernaux->vna_nextptr = NULL;
7822 if (ivernaux->vna_next == 0)
7824 iverneed->vn_cnt = j + 1;
7827 if (j + 1 < iverneed->vn_cnt)
7828 ivernaux->vna_nextptr = ivernaux + 1;
7830 if (ivernaux->vna_next
7831 > (size_t) (contents_end - (bfd_byte *) evernaux))
7832 goto error_return_bad_verref;
7834 evernaux = ((Elf_External_Vernaux *)
7835 ((bfd_byte *) evernaux + ivernaux->vna_next));
7838 iverneed->vn_nextref = NULL;
7839 if (iverneed->vn_next == 0)
7841 if (i + 1 < hdr->sh_info)
7842 iverneed->vn_nextref = iverneed + 1;
7844 if (iverneed->vn_next
7845 > (size_t) (contents_end - (bfd_byte *) everneed))
7846 goto error_return_bad_verref;
7848 everneed = ((Elf_External_Verneed *)
7849 ((bfd_byte *) everneed + iverneed->vn_next));
7851 elf_tdata (abfd)->cverrefs = i;
7857 if (elf_dynverdef (abfd) != 0)
7859 Elf_Internal_Shdr *hdr;
7860 Elf_External_Verdef *everdef;
7861 Elf_Internal_Verdef *iverdef;
7862 Elf_Internal_Verdef *iverdefarr;
7863 Elf_Internal_Verdef iverdefmem;
7865 unsigned int maxidx;
7866 bfd_byte *contents_end_def, *contents_end_aux;
7868 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7870 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
7872 error_return_bad_verdef:
7873 (*_bfd_error_handler)
7874 (_("%B: .gnu.version_d invalid entry"), abfd);
7875 bfd_set_error (bfd_error_bad_value);
7876 error_return_verdef:
7877 elf_tdata (abfd)->verdef = NULL;
7878 elf_tdata (abfd)->cverdefs = 0;
7882 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7883 if (contents == NULL)
7884 goto error_return_verdef;
7885 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7886 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7887 goto error_return_verdef;
7889 BFD_ASSERT (sizeof (Elf_External_Verdef)
7890 >= sizeof (Elf_External_Verdaux));
7891 contents_end_def = contents + hdr->sh_size
7892 - sizeof (Elf_External_Verdef);
7893 contents_end_aux = contents + hdr->sh_size
7894 - sizeof (Elf_External_Verdaux);
7896 /* We know the number of entries in the section but not the maximum
7897 index. Therefore we have to run through all entries and find
7899 everdef = (Elf_External_Verdef *) contents;
7901 for (i = 0; i < hdr->sh_info; ++i)
7903 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7905 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
7906 goto error_return_bad_verdef;
7907 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7908 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7910 if (iverdefmem.vd_next == 0)
7913 if (iverdefmem.vd_next
7914 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7915 goto error_return_bad_verdef;
7917 everdef = ((Elf_External_Verdef *)
7918 ((bfd_byte *) everdef + iverdefmem.vd_next));
7921 if (default_imported_symver)
7923 if (freeidx > maxidx)
7929 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7930 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7931 if (elf_tdata (abfd)->verdef == NULL)
7932 goto error_return_verdef;
7934 elf_tdata (abfd)->cverdefs = maxidx;
7936 everdef = (Elf_External_Verdef *) contents;
7937 iverdefarr = elf_tdata (abfd)->verdef;
7938 for (i = 0; i < hdr->sh_info; i++)
7940 Elf_External_Verdaux *everdaux;
7941 Elf_Internal_Verdaux *iverdaux;
7944 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7946 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7947 goto error_return_bad_verdef;
7949 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7950 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
7952 iverdef->vd_bfd = abfd;
7954 if (iverdef->vd_cnt == 0)
7955 iverdef->vd_auxptr = NULL;
7958 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7959 bfd_alloc2 (abfd, iverdef->vd_cnt,
7960 sizeof (Elf_Internal_Verdaux));
7961 if (iverdef->vd_auxptr == NULL)
7962 goto error_return_verdef;
7966 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7967 goto error_return_bad_verdef;
7969 everdaux = ((Elf_External_Verdaux *)
7970 ((bfd_byte *) everdef + iverdef->vd_aux));
7971 iverdaux = iverdef->vd_auxptr;
7972 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7974 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7976 iverdaux->vda_nodename =
7977 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7978 iverdaux->vda_name);
7979 if (iverdaux->vda_nodename == NULL)
7980 goto error_return_bad_verdef;
7982 iverdaux->vda_nextptr = NULL;
7983 if (iverdaux->vda_next == 0)
7985 iverdef->vd_cnt = j + 1;
7988 if (j + 1 < iverdef->vd_cnt)
7989 iverdaux->vda_nextptr = iverdaux + 1;
7991 if (iverdaux->vda_next
7992 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7993 goto error_return_bad_verdef;
7995 everdaux = ((Elf_External_Verdaux *)
7996 ((bfd_byte *) everdaux + iverdaux->vda_next));
7999 iverdef->vd_nodename = NULL;
8000 if (iverdef->vd_cnt)
8001 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8003 iverdef->vd_nextdef = NULL;
8004 if (iverdef->vd_next == 0)
8006 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8007 iverdef->vd_nextdef = iverdef + 1;
8009 everdef = ((Elf_External_Verdef *)
8010 ((bfd_byte *) everdef + iverdef->vd_next));
8016 else if (default_imported_symver)
8023 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8024 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8025 if (elf_tdata (abfd)->verdef == NULL)
8028 elf_tdata (abfd)->cverdefs = freeidx;
8031 /* Create a default version based on the soname. */
8032 if (default_imported_symver)
8034 Elf_Internal_Verdef *iverdef;
8035 Elf_Internal_Verdaux *iverdaux;
8037 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8039 iverdef->vd_version = VER_DEF_CURRENT;
8040 iverdef->vd_flags = 0;
8041 iverdef->vd_ndx = freeidx;
8042 iverdef->vd_cnt = 1;
8044 iverdef->vd_bfd = abfd;
8046 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8047 if (iverdef->vd_nodename == NULL)
8048 goto error_return_verdef;
8049 iverdef->vd_nextdef = NULL;
8050 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8051 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8052 if (iverdef->vd_auxptr == NULL)
8053 goto error_return_verdef;
8055 iverdaux = iverdef->vd_auxptr;
8056 iverdaux->vda_nodename = iverdef->vd_nodename;
8062 if (contents != NULL)
8068 _bfd_elf_make_empty_symbol (bfd *abfd)
8070 elf_symbol_type *newsym;
8072 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
8075 newsym->symbol.the_bfd = abfd;
8076 return &newsym->symbol;
8080 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8084 bfd_symbol_info (symbol, ret);
8087 /* Return whether a symbol name implies a local symbol. Most targets
8088 use this function for the is_local_label_name entry point, but some
8092 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8095 /* Normal local symbols start with ``.L''. */
8096 if (name[0] == '.' && name[1] == 'L')
8099 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8100 DWARF debugging symbols starting with ``..''. */
8101 if (name[0] == '.' && name[1] == '.')
8104 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8105 emitting DWARF debugging output. I suspect this is actually a
8106 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8107 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8108 underscore to be emitted on some ELF targets). For ease of use,
8109 we treat such symbols as local. */
8110 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8113 /* Treat assembler generated fake symbols, dollar local labels and
8114 forward-backward labels (aka local labels) as locals.
8115 These labels have the form:
8117 L0^A.* (fake symbols)
8119 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8121 Versions which start with .L will have already been matched above,
8122 so we only need to match the rest. */
8123 if (name[0] == 'L' && ISDIGIT (name[1]))
8125 bfd_boolean ret = FALSE;
8129 for (p = name + 2; (c = *p); p++)
8131 if (c == 1 || c == 2)
8133 if (c == 1 && p == name + 2)
8134 /* A fake symbol. */
8137 /* FIXME: We are being paranoid here and treating symbols like
8138 L0^Bfoo as if there were non-local, on the grounds that the
8139 assembler will never generate them. But can any symbol
8140 containing an ASCII value in the range 1-31 ever be anything
8141 other than some kind of local ? */
8158 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8159 asymbol *symbol ATTRIBUTE_UNUSED)
8166 _bfd_elf_set_arch_mach (bfd *abfd,
8167 enum bfd_architecture arch,
8168 unsigned long machine)
8170 /* If this isn't the right architecture for this backend, and this
8171 isn't the generic backend, fail. */
8172 if (arch != get_elf_backend_data (abfd)->arch
8173 && arch != bfd_arch_unknown
8174 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8177 return bfd_default_set_arch_mach (abfd, arch, machine);
8180 /* Find the nearest line to a particular section and offset,
8181 for error reporting. */
8184 _bfd_elf_find_nearest_line (bfd *abfd,
8188 const char **filename_ptr,
8189 const char **functionname_ptr,
8190 unsigned int *line_ptr,
8191 unsigned int *discriminator_ptr)
8195 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8196 filename_ptr, functionname_ptr,
8197 line_ptr, discriminator_ptr,
8198 dwarf_debug_sections, 0,
8199 &elf_tdata (abfd)->dwarf2_find_line_info)
8200 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8201 filename_ptr, functionname_ptr,
8204 if (!*functionname_ptr)
8205 _bfd_elf_find_function (abfd, symbols, section, offset,
8206 *filename_ptr ? NULL : filename_ptr,
8211 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8212 &found, filename_ptr,
8213 functionname_ptr, line_ptr,
8214 &elf_tdata (abfd)->line_info))
8216 if (found && (*functionname_ptr || *line_ptr))
8219 if (symbols == NULL)
8222 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8223 filename_ptr, functionname_ptr))
8230 /* Find the line for a symbol. */
8233 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8234 const char **filename_ptr, unsigned int *line_ptr)
8236 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8237 filename_ptr, NULL, line_ptr, NULL,
8238 dwarf_debug_sections, 0,
8239 &elf_tdata (abfd)->dwarf2_find_line_info);
8242 /* After a call to bfd_find_nearest_line, successive calls to
8243 bfd_find_inliner_info can be used to get source information about
8244 each level of function inlining that terminated at the address
8245 passed to bfd_find_nearest_line. Currently this is only supported
8246 for DWARF2 with appropriate DWARF3 extensions. */
8249 _bfd_elf_find_inliner_info (bfd *abfd,
8250 const char **filename_ptr,
8251 const char **functionname_ptr,
8252 unsigned int *line_ptr)
8255 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8256 functionname_ptr, line_ptr,
8257 & elf_tdata (abfd)->dwarf2_find_line_info);
8262 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
8264 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8265 int ret = bed->s->sizeof_ehdr;
8267 if (!bfd_link_relocatable (info))
8269 bfd_size_type phdr_size = elf_program_header_size (abfd);
8271 if (phdr_size == (bfd_size_type) -1)
8273 struct elf_segment_map *m;
8276 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
8277 phdr_size += bed->s->sizeof_phdr;
8280 phdr_size = get_program_header_size (abfd, info);
8283 elf_program_header_size (abfd) = phdr_size;
8291 _bfd_elf_set_section_contents (bfd *abfd,
8293 const void *location,
8295 bfd_size_type count)
8297 Elf_Internal_Shdr *hdr;
8300 if (! abfd->output_has_begun
8301 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
8307 hdr = &elf_section_data (section)->this_hdr;
8308 if (hdr->sh_offset == (file_ptr) -1)
8310 /* We must compress this section. Write output to the buffer. */
8311 unsigned char *contents = hdr->contents;
8312 if ((offset + count) > hdr->sh_size
8313 || (section->flags & SEC_ELF_COMPRESS) == 0
8314 || contents == NULL)
8316 memcpy (contents + offset, location, count);
8319 pos = hdr->sh_offset + offset;
8320 if (bfd_seek (abfd, pos, SEEK_SET) != 0
8321 || bfd_bwrite (location, count, abfd) != count)
8328 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
8329 arelent *cache_ptr ATTRIBUTE_UNUSED,
8330 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
8335 /* Try to convert a non-ELF reloc into an ELF one. */
8338 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
8340 /* Check whether we really have an ELF howto. */
8342 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
8344 bfd_reloc_code_real_type code;
8345 reloc_howto_type *howto;
8347 /* Alien reloc: Try to determine its type to replace it with an
8348 equivalent ELF reloc. */
8350 if (areloc->howto->pc_relative)
8352 switch (areloc->howto->bitsize)
8355 code = BFD_RELOC_8_PCREL;
8358 code = BFD_RELOC_12_PCREL;
8361 code = BFD_RELOC_16_PCREL;
8364 code = BFD_RELOC_24_PCREL;
8367 code = BFD_RELOC_32_PCREL;
8370 code = BFD_RELOC_64_PCREL;
8376 howto = bfd_reloc_type_lookup (abfd, code);
8378 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
8380 if (howto->pcrel_offset)
8381 areloc->addend += areloc->address;
8383 areloc->addend -= areloc->address; /* addend is unsigned!! */
8388 switch (areloc->howto->bitsize)
8394 code = BFD_RELOC_14;
8397 code = BFD_RELOC_16;
8400 code = BFD_RELOC_26;
8403 code = BFD_RELOC_32;
8406 code = BFD_RELOC_64;
8412 howto = bfd_reloc_type_lookup (abfd, code);
8416 areloc->howto = howto;
8424 (*_bfd_error_handler)
8425 (_("%B: unsupported relocation type %s"),
8426 abfd, areloc->howto->name);
8427 bfd_set_error (bfd_error_bad_value);
8432 _bfd_elf_close_and_cleanup (bfd *abfd)
8434 struct elf_obj_tdata *tdata = elf_tdata (abfd);
8435 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
8437 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
8438 _bfd_elf_strtab_free (elf_shstrtab (abfd));
8439 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
8442 return _bfd_generic_close_and_cleanup (abfd);
8445 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8446 in the relocation's offset. Thus we cannot allow any sort of sanity
8447 range-checking to interfere. There is nothing else to do in processing
8450 bfd_reloc_status_type
8451 _bfd_elf_rel_vtable_reloc_fn
8452 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
8453 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
8454 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
8455 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
8457 return bfd_reloc_ok;
8460 /* Elf core file support. Much of this only works on native
8461 toolchains, since we rely on knowing the
8462 machine-dependent procfs structure in order to pick
8463 out details about the corefile. */
8465 #ifdef HAVE_SYS_PROCFS_H
8466 /* Needed for new procfs interface on sparc-solaris. */
8467 # define _STRUCTURED_PROC 1
8468 # include <sys/procfs.h>
8471 /* Return a PID that identifies a "thread" for threaded cores, or the
8472 PID of the main process for non-threaded cores. */
8475 elfcore_make_pid (bfd *abfd)
8479 pid = elf_tdata (abfd)->core->lwpid;
8481 pid = elf_tdata (abfd)->core->pid;
8486 /* If there isn't a section called NAME, make one, using
8487 data from SECT. Note, this function will generate a
8488 reference to NAME, so you shouldn't deallocate or
8492 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8496 if (bfd_get_section_by_name (abfd, name) != NULL)
8499 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8503 sect2->size = sect->size;
8504 sect2->filepos = sect->filepos;
8505 sect2->alignment_power = sect->alignment_power;
8509 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8510 actually creates up to two pseudosections:
8511 - For the single-threaded case, a section named NAME, unless
8512 such a section already exists.
8513 - For the multi-threaded case, a section named "NAME/PID", where
8514 PID is elfcore_make_pid (abfd).
8515 Both pseudosections have identical contents. */
8517 _bfd_elfcore_make_pseudosection (bfd *abfd,
8523 char *threaded_name;
8527 /* Build the section name. */
8529 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8530 len = strlen (buf) + 1;
8531 threaded_name = (char *) bfd_alloc (abfd, len);
8532 if (threaded_name == NULL)
8534 memcpy (threaded_name, buf, len);
8536 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8541 sect->filepos = filepos;
8542 sect->alignment_power = 2;
8544 return elfcore_maybe_make_sect (abfd, name, sect);
8547 /* prstatus_t exists on:
8549 linux 2.[01] + glibc
8553 #if defined (HAVE_PRSTATUS_T)
8556 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8561 if (note->descsz == sizeof (prstatus_t))
8565 size = sizeof (prstat.pr_reg);
8566 offset = offsetof (prstatus_t, pr_reg);
8567 memcpy (&prstat, note->descdata, sizeof (prstat));
8569 /* Do not overwrite the core signal if it
8570 has already been set by another thread. */
8571 if (elf_tdata (abfd)->core->signal == 0)
8572 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8573 if (elf_tdata (abfd)->core->pid == 0)
8574 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8576 /* pr_who exists on:
8579 pr_who doesn't exist on:
8582 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8583 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8585 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8588 #if defined (HAVE_PRSTATUS32_T)
8589 else if (note->descsz == sizeof (prstatus32_t))
8591 /* 64-bit host, 32-bit corefile */
8592 prstatus32_t prstat;
8594 size = sizeof (prstat.pr_reg);
8595 offset = offsetof (prstatus32_t, pr_reg);
8596 memcpy (&prstat, note->descdata, sizeof (prstat));
8598 /* Do not overwrite the core signal if it
8599 has already been set by another thread. */
8600 if (elf_tdata (abfd)->core->signal == 0)
8601 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8602 if (elf_tdata (abfd)->core->pid == 0)
8603 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8605 /* pr_who exists on:
8608 pr_who doesn't exist on:
8611 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8612 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8614 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8617 #endif /* HAVE_PRSTATUS32_T */
8620 /* Fail - we don't know how to handle any other
8621 note size (ie. data object type). */
8625 /* Make a ".reg/999" section and a ".reg" section. */
8626 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8627 size, note->descpos + offset);
8629 #endif /* defined (HAVE_PRSTATUS_T) */
8631 /* Create a pseudosection containing the exact contents of NOTE. */
8633 elfcore_make_note_pseudosection (bfd *abfd,
8635 Elf_Internal_Note *note)
8637 return _bfd_elfcore_make_pseudosection (abfd, name,
8638 note->descsz, note->descpos);
8641 /* There isn't a consistent prfpregset_t across platforms,
8642 but it doesn't matter, because we don't have to pick this
8643 data structure apart. */
8646 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8648 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8651 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8652 type of NT_PRXFPREG. Just include the whole note's contents
8656 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8658 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8661 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8662 with a note type of NT_X86_XSTATE. Just include the whole note's
8663 contents literally. */
8666 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8668 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8672 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8674 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8678 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8680 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8684 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8686 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8690 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8692 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8696 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8698 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8702 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8704 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8708 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8710 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8714 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8716 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8720 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8722 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8726 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8728 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8732 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8734 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8738 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
8740 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
8744 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
8746 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
8750 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8752 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8756 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8758 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8762 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8764 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8768 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8770 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8773 #if defined (HAVE_PRPSINFO_T)
8774 typedef prpsinfo_t elfcore_psinfo_t;
8775 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8776 typedef prpsinfo32_t elfcore_psinfo32_t;
8780 #if defined (HAVE_PSINFO_T)
8781 typedef psinfo_t elfcore_psinfo_t;
8782 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8783 typedef psinfo32_t elfcore_psinfo32_t;
8787 /* return a malloc'ed copy of a string at START which is at
8788 most MAX bytes long, possibly without a terminating '\0'.
8789 the copy will always have a terminating '\0'. */
8792 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8795 char *end = (char *) memchr (start, '\0', max);
8803 dups = (char *) bfd_alloc (abfd, len + 1);
8807 memcpy (dups, start, len);
8813 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8815 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8817 if (note->descsz == sizeof (elfcore_psinfo_t))
8819 elfcore_psinfo_t psinfo;
8821 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8823 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8824 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8826 elf_tdata (abfd)->core->program
8827 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8828 sizeof (psinfo.pr_fname));
8830 elf_tdata (abfd)->core->command
8831 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8832 sizeof (psinfo.pr_psargs));
8834 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8835 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8837 /* 64-bit host, 32-bit corefile */
8838 elfcore_psinfo32_t psinfo;
8840 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8842 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8843 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8845 elf_tdata (abfd)->core->program
8846 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8847 sizeof (psinfo.pr_fname));
8849 elf_tdata (abfd)->core->command
8850 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8851 sizeof (psinfo.pr_psargs));
8857 /* Fail - we don't know how to handle any other
8858 note size (ie. data object type). */
8862 /* Note that for some reason, a spurious space is tacked
8863 onto the end of the args in some (at least one anyway)
8864 implementations, so strip it off if it exists. */
8867 char *command = elf_tdata (abfd)->core->command;
8868 int n = strlen (command);
8870 if (0 < n && command[n - 1] == ' ')
8871 command[n - 1] = '\0';
8876 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8878 #if defined (HAVE_PSTATUS_T)
8880 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8882 if (note->descsz == sizeof (pstatus_t)
8883 #if defined (HAVE_PXSTATUS_T)
8884 || note->descsz == sizeof (pxstatus_t)
8890 memcpy (&pstat, note->descdata, sizeof (pstat));
8892 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8894 #if defined (HAVE_PSTATUS32_T)
8895 else if (note->descsz == sizeof (pstatus32_t))
8897 /* 64-bit host, 32-bit corefile */
8900 memcpy (&pstat, note->descdata, sizeof (pstat));
8902 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8905 /* Could grab some more details from the "representative"
8906 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8907 NT_LWPSTATUS note, presumably. */
8911 #endif /* defined (HAVE_PSTATUS_T) */
8913 #if defined (HAVE_LWPSTATUS_T)
8915 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8917 lwpstatus_t lwpstat;
8923 if (note->descsz != sizeof (lwpstat)
8924 #if defined (HAVE_LWPXSTATUS_T)
8925 && note->descsz != sizeof (lwpxstatus_t)
8930 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8932 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8933 /* Do not overwrite the core signal if it has already been set by
8935 if (elf_tdata (abfd)->core->signal == 0)
8936 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8938 /* Make a ".reg/999" section. */
8940 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8941 len = strlen (buf) + 1;
8942 name = bfd_alloc (abfd, len);
8945 memcpy (name, buf, len);
8947 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8951 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8952 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8953 sect->filepos = note->descpos
8954 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8957 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8958 sect->size = sizeof (lwpstat.pr_reg);
8959 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8962 sect->alignment_power = 2;
8964 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8967 /* Make a ".reg2/999" section */
8969 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8970 len = strlen (buf) + 1;
8971 name = bfd_alloc (abfd, len);
8974 memcpy (name, buf, len);
8976 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8980 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8981 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8982 sect->filepos = note->descpos
8983 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8986 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8987 sect->size = sizeof (lwpstat.pr_fpreg);
8988 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8991 sect->alignment_power = 2;
8993 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8995 #endif /* defined (HAVE_LWPSTATUS_T) */
8998 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9005 int is_active_thread;
9008 if (note->descsz < 728)
9011 if (! CONST_STRNEQ (note->namedata, "win32"))
9014 type = bfd_get_32 (abfd, note->descdata);
9018 case 1 /* NOTE_INFO_PROCESS */:
9019 /* FIXME: need to add ->core->command. */
9020 /* process_info.pid */
9021 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9022 /* process_info.signal */
9023 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9026 case 2 /* NOTE_INFO_THREAD */:
9027 /* Make a ".reg/999" section. */
9028 /* thread_info.tid */
9029 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9031 len = strlen (buf) + 1;
9032 name = (char *) bfd_alloc (abfd, len);
9036 memcpy (name, buf, len);
9038 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9042 /* sizeof (thread_info.thread_context) */
9044 /* offsetof (thread_info.thread_context) */
9045 sect->filepos = note->descpos + 12;
9046 sect->alignment_power = 2;
9048 /* thread_info.is_active_thread */
9049 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9051 if (is_active_thread)
9052 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9056 case 3 /* NOTE_INFO_MODULE */:
9057 /* Make a ".module/xxxxxxxx" section. */
9058 /* module_info.base_address */
9059 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9060 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9062 len = strlen (buf) + 1;
9063 name = (char *) bfd_alloc (abfd, len);
9067 memcpy (name, buf, len);
9069 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9074 sect->size = note->descsz;
9075 sect->filepos = note->descpos;
9076 sect->alignment_power = 2;
9087 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9089 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9097 if (bed->elf_backend_grok_prstatus)
9098 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
9100 #if defined (HAVE_PRSTATUS_T)
9101 return elfcore_grok_prstatus (abfd, note);
9106 #if defined (HAVE_PSTATUS_T)
9108 return elfcore_grok_pstatus (abfd, note);
9111 #if defined (HAVE_LWPSTATUS_T)
9113 return elfcore_grok_lwpstatus (abfd, note);
9116 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9117 return elfcore_grok_prfpreg (abfd, note);
9119 case NT_WIN32PSTATUS:
9120 return elfcore_grok_win32pstatus (abfd, note);
9122 case NT_PRXFPREG: /* Linux SSE extension */
9123 if (note->namesz == 6
9124 && strcmp (note->namedata, "LINUX") == 0)
9125 return elfcore_grok_prxfpreg (abfd, note);
9129 case NT_X86_XSTATE: /* Linux XSAVE extension */
9130 if (note->namesz == 6
9131 && strcmp (note->namedata, "LINUX") == 0)
9132 return elfcore_grok_xstatereg (abfd, note);
9133 else if (note->namesz == 8
9134 && strcmp (note->namedata, "FreeBSD") == 0)
9135 return elfcore_grok_xstatereg (abfd, note);
9140 if (note->namesz == 6
9141 && strcmp (note->namedata, "LINUX") == 0)
9142 return elfcore_grok_ppc_vmx (abfd, note);
9147 if (note->namesz == 6
9148 && strcmp (note->namedata, "LINUX") == 0)
9149 return elfcore_grok_ppc_vsx (abfd, note);
9153 case NT_S390_HIGH_GPRS:
9154 if (note->namesz == 6
9155 && strcmp (note->namedata, "LINUX") == 0)
9156 return elfcore_grok_s390_high_gprs (abfd, note);
9161 if (note->namesz == 6
9162 && strcmp (note->namedata, "LINUX") == 0)
9163 return elfcore_grok_s390_timer (abfd, note);
9167 case NT_S390_TODCMP:
9168 if (note->namesz == 6
9169 && strcmp (note->namedata, "LINUX") == 0)
9170 return elfcore_grok_s390_todcmp (abfd, note);
9174 case NT_S390_TODPREG:
9175 if (note->namesz == 6
9176 && strcmp (note->namedata, "LINUX") == 0)
9177 return elfcore_grok_s390_todpreg (abfd, note);
9182 if (note->namesz == 6
9183 && strcmp (note->namedata, "LINUX") == 0)
9184 return elfcore_grok_s390_ctrs (abfd, note);
9188 case NT_S390_PREFIX:
9189 if (note->namesz == 6
9190 && strcmp (note->namedata, "LINUX") == 0)
9191 return elfcore_grok_s390_prefix (abfd, note);
9195 case NT_S390_LAST_BREAK:
9196 if (note->namesz == 6
9197 && strcmp (note->namedata, "LINUX") == 0)
9198 return elfcore_grok_s390_last_break (abfd, note);
9202 case NT_S390_SYSTEM_CALL:
9203 if (note->namesz == 6
9204 && strcmp (note->namedata, "LINUX") == 0)
9205 return elfcore_grok_s390_system_call (abfd, note);
9210 if (note->namesz == 6
9211 && strcmp (note->namedata, "LINUX") == 0)
9212 return elfcore_grok_s390_tdb (abfd, note);
9216 case NT_S390_VXRS_LOW:
9217 if (note->namesz == 6
9218 && strcmp (note->namedata, "LINUX") == 0)
9219 return elfcore_grok_s390_vxrs_low (abfd, note);
9223 case NT_S390_VXRS_HIGH:
9224 if (note->namesz == 6
9225 && strcmp (note->namedata, "LINUX") == 0)
9226 return elfcore_grok_s390_vxrs_high (abfd, note);
9231 if (note->namesz == 6
9232 && strcmp (note->namedata, "LINUX") == 0)
9233 return elfcore_grok_arm_vfp (abfd, note);
9238 if (note->namesz == 6
9239 && strcmp (note->namedata, "LINUX") == 0)
9240 return elfcore_grok_aarch_tls (abfd, note);
9244 case NT_ARM_HW_BREAK:
9245 if (note->namesz == 6
9246 && strcmp (note->namedata, "LINUX") == 0)
9247 return elfcore_grok_aarch_hw_break (abfd, note);
9251 case NT_ARM_HW_WATCH:
9252 if (note->namesz == 6
9253 && strcmp (note->namedata, "LINUX") == 0)
9254 return elfcore_grok_aarch_hw_watch (abfd, note);
9260 if (bed->elf_backend_grok_psinfo)
9261 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
9263 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9264 return elfcore_grok_psinfo (abfd, note);
9271 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9276 sect->size = note->descsz;
9277 sect->filepos = note->descpos;
9278 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9284 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
9288 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
9294 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
9296 struct bfd_build_id* build_id;
9298 if (note->descsz == 0)
9301 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
9302 if (build_id == NULL)
9305 build_id->size = note->descsz;
9306 memcpy (build_id->data, note->descdata, note->descsz);
9307 abfd->build_id = build_id;
9313 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
9320 case NT_GNU_BUILD_ID:
9321 return elfobj_grok_gnu_build_id (abfd, note);
9326 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
9328 struct sdt_note *cur =
9329 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
9332 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
9333 cur->size = (bfd_size_type) note->descsz;
9334 memcpy (cur->data, note->descdata, note->descsz);
9336 elf_tdata (abfd)->sdt_note_head = cur;
9342 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
9347 return elfobj_grok_stapsdt_note_1 (abfd, note);
9355 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
9359 cp = strchr (note->namedata, '@');
9362 *lwpidp = atoi(cp + 1);
9369 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
9371 /* Signal number at offset 0x08. */
9372 elf_tdata (abfd)->core->signal
9373 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
9375 /* Process ID at offset 0x50. */
9376 elf_tdata (abfd)->core->pid
9377 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
9379 /* Command name at 0x7c (max 32 bytes, including nul). */
9380 elf_tdata (abfd)->core->command
9381 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
9383 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
9388 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
9392 if (elfcore_netbsd_get_lwpid (note, &lwp))
9393 elf_tdata (abfd)->core->lwpid = lwp;
9395 if (note->type == NT_NETBSDCORE_PROCINFO)
9397 /* NetBSD-specific core "procinfo". Note that we expect to
9398 find this note before any of the others, which is fine,
9399 since the kernel writes this note out first when it
9400 creates a core file. */
9402 return elfcore_grok_netbsd_procinfo (abfd, note);
9405 /* As of Jan 2002 there are no other machine-independent notes
9406 defined for NetBSD core files. If the note type is less
9407 than the start of the machine-dependent note types, we don't
9410 if (note->type < NT_NETBSDCORE_FIRSTMACH)
9414 switch (bfd_get_arch (abfd))
9416 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9417 PT_GETFPREGS == mach+2. */
9419 case bfd_arch_alpha:
9420 case bfd_arch_sparc:
9423 case NT_NETBSDCORE_FIRSTMACH+0:
9424 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9426 case NT_NETBSDCORE_FIRSTMACH+2:
9427 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9433 /* On all other arch's, PT_GETREGS == mach+1 and
9434 PT_GETFPREGS == mach+3. */
9439 case NT_NETBSDCORE_FIRSTMACH+1:
9440 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9442 case NT_NETBSDCORE_FIRSTMACH+3:
9443 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9453 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
9455 /* Signal number at offset 0x08. */
9456 elf_tdata (abfd)->core->signal
9457 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
9459 /* Process ID at offset 0x20. */
9460 elf_tdata (abfd)->core->pid
9461 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
9463 /* Command name at 0x48 (max 32 bytes, including nul). */
9464 elf_tdata (abfd)->core->command
9465 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
9471 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
9473 if (note->type == NT_OPENBSD_PROCINFO)
9474 return elfcore_grok_openbsd_procinfo (abfd, note);
9476 if (note->type == NT_OPENBSD_REGS)
9477 return elfcore_make_note_pseudosection (abfd, ".reg", note);
9479 if (note->type == NT_OPENBSD_FPREGS)
9480 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9482 if (note->type == NT_OPENBSD_XFPREGS)
9483 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9485 if (note->type == NT_OPENBSD_AUXV)
9487 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9492 sect->size = note->descsz;
9493 sect->filepos = note->descpos;
9494 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9499 if (note->type == NT_OPENBSD_WCOOKIE)
9501 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
9506 sect->size = note->descsz;
9507 sect->filepos = note->descpos;
9508 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9517 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
9519 void *ddata = note->descdata;
9526 /* nto_procfs_status 'pid' field is at offset 0. */
9527 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
9529 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9530 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
9532 /* nto_procfs_status 'flags' field is at offset 8. */
9533 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
9535 /* nto_procfs_status 'what' field is at offset 14. */
9536 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9538 elf_tdata (abfd)->core->signal = sig;
9539 elf_tdata (abfd)->core->lwpid = *tid;
9542 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9543 do not come from signals so we make sure we set the current
9544 thread just in case. */
9545 if (flags & 0x00000080)
9546 elf_tdata (abfd)->core->lwpid = *tid;
9548 /* Make a ".qnx_core_status/%d" section. */
9549 sprintf (buf, ".qnx_core_status/%ld", *tid);
9551 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9556 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9560 sect->size = note->descsz;
9561 sect->filepos = note->descpos;
9562 sect->alignment_power = 2;
9564 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9568 elfcore_grok_nto_regs (bfd *abfd,
9569 Elf_Internal_Note *note,
9577 /* Make a "(base)/%d" section. */
9578 sprintf (buf, "%s/%ld", base, tid);
9580 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9585 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9589 sect->size = note->descsz;
9590 sect->filepos = note->descpos;
9591 sect->alignment_power = 2;
9593 /* This is the current thread. */
9594 if (elf_tdata (abfd)->core->lwpid == tid)
9595 return elfcore_maybe_make_sect (abfd, base, sect);
9600 #define BFD_QNT_CORE_INFO 7
9601 #define BFD_QNT_CORE_STATUS 8
9602 #define BFD_QNT_CORE_GREG 9
9603 #define BFD_QNT_CORE_FPREG 10
9606 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9608 /* Every GREG section has a STATUS section before it. Store the
9609 tid from the previous call to pass down to the next gregs
9611 static long tid = 1;
9615 case BFD_QNT_CORE_INFO:
9616 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9617 case BFD_QNT_CORE_STATUS:
9618 return elfcore_grok_nto_status (abfd, note, &tid);
9619 case BFD_QNT_CORE_GREG:
9620 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9621 case BFD_QNT_CORE_FPREG:
9622 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9629 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9635 /* Use note name as section name. */
9637 name = (char *) bfd_alloc (abfd, len);
9640 memcpy (name, note->namedata, len);
9641 name[len - 1] = '\0';
9643 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9647 sect->size = note->descsz;
9648 sect->filepos = note->descpos;
9649 sect->alignment_power = 1;
9654 /* Function: elfcore_write_note
9657 buffer to hold note, and current size of buffer
9661 size of data for note
9663 Writes note to end of buffer. ELF64 notes are written exactly as
9664 for ELF32, despite the current (as of 2006) ELF gabi specifying
9665 that they ought to have 8-byte namesz and descsz field, and have
9666 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9669 Pointer to realloc'd buffer, *BUFSIZ updated. */
9672 elfcore_write_note (bfd *abfd,
9680 Elf_External_Note *xnp;
9687 namesz = strlen (name) + 1;
9689 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9691 buf = (char *) realloc (buf, *bufsiz + newspace);
9694 dest = buf + *bufsiz;
9695 *bufsiz += newspace;
9696 xnp = (Elf_External_Note *) dest;
9697 H_PUT_32 (abfd, namesz, xnp->namesz);
9698 H_PUT_32 (abfd, size, xnp->descsz);
9699 H_PUT_32 (abfd, type, xnp->type);
9703 memcpy (dest, name, namesz);
9711 memcpy (dest, input, size);
9722 elfcore_write_prpsinfo (bfd *abfd,
9728 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9730 if (bed->elf_backend_write_core_note != NULL)
9733 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9734 NT_PRPSINFO, fname, psargs);
9739 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9740 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9741 if (bed->s->elfclass == ELFCLASS32)
9743 #if defined (HAVE_PSINFO32_T)
9745 int note_type = NT_PSINFO;
9748 int note_type = NT_PRPSINFO;
9751 memset (&data, 0, sizeof (data));
9752 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9753 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9754 return elfcore_write_note (abfd, buf, bufsiz,
9755 "CORE", note_type, &data, sizeof (data));
9760 #if defined (HAVE_PSINFO_T)
9762 int note_type = NT_PSINFO;
9765 int note_type = NT_PRPSINFO;
9768 memset (&data, 0, sizeof (data));
9769 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9770 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9771 return elfcore_write_note (abfd, buf, bufsiz,
9772 "CORE", note_type, &data, sizeof (data));
9774 #endif /* PSINFO_T or PRPSINFO_T */
9781 elfcore_write_linux_prpsinfo32
9782 (bfd *abfd, char *buf, int *bufsiz,
9783 const struct elf_internal_linux_prpsinfo *prpsinfo)
9785 struct elf_external_linux_prpsinfo32 data;
9787 memset (&data, 0, sizeof (data));
9788 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9790 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9791 &data, sizeof (data));
9795 elfcore_write_linux_prpsinfo64
9796 (bfd *abfd, char *buf, int *bufsiz,
9797 const struct elf_internal_linux_prpsinfo *prpsinfo)
9799 struct elf_external_linux_prpsinfo64 data;
9801 memset (&data, 0, sizeof (data));
9802 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9804 return elfcore_write_note (abfd, buf, bufsiz,
9805 "CORE", NT_PRPSINFO, &data, sizeof (data));
9809 elfcore_write_prstatus (bfd *abfd,
9816 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9818 if (bed->elf_backend_write_core_note != NULL)
9821 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9823 pid, cursig, gregs);
9828 #if defined (HAVE_PRSTATUS_T)
9829 #if defined (HAVE_PRSTATUS32_T)
9830 if (bed->s->elfclass == ELFCLASS32)
9832 prstatus32_t prstat;
9834 memset (&prstat, 0, sizeof (prstat));
9835 prstat.pr_pid = pid;
9836 prstat.pr_cursig = cursig;
9837 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9838 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9839 NT_PRSTATUS, &prstat, sizeof (prstat));
9846 memset (&prstat, 0, sizeof (prstat));
9847 prstat.pr_pid = pid;
9848 prstat.pr_cursig = cursig;
9849 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9850 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9851 NT_PRSTATUS, &prstat, sizeof (prstat));
9853 #endif /* HAVE_PRSTATUS_T */
9859 #if defined (HAVE_LWPSTATUS_T)
9861 elfcore_write_lwpstatus (bfd *abfd,
9868 lwpstatus_t lwpstat;
9869 const char *note_name = "CORE";
9871 memset (&lwpstat, 0, sizeof (lwpstat));
9872 lwpstat.pr_lwpid = pid >> 16;
9873 lwpstat.pr_cursig = cursig;
9874 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9875 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9876 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9878 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9879 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9881 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9882 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9885 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9886 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9888 #endif /* HAVE_LWPSTATUS_T */
9890 #if defined (HAVE_PSTATUS_T)
9892 elfcore_write_pstatus (bfd *abfd,
9896 int cursig ATTRIBUTE_UNUSED,
9897 const void *gregs ATTRIBUTE_UNUSED)
9899 const char *note_name = "CORE";
9900 #if defined (HAVE_PSTATUS32_T)
9901 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9903 if (bed->s->elfclass == ELFCLASS32)
9907 memset (&pstat, 0, sizeof (pstat));
9908 pstat.pr_pid = pid & 0xffff;
9909 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9910 NT_PSTATUS, &pstat, sizeof (pstat));
9918 memset (&pstat, 0, sizeof (pstat));
9919 pstat.pr_pid = pid & 0xffff;
9920 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9921 NT_PSTATUS, &pstat, sizeof (pstat));
9925 #endif /* HAVE_PSTATUS_T */
9928 elfcore_write_prfpreg (bfd *abfd,
9934 const char *note_name = "CORE";
9935 return elfcore_write_note (abfd, buf, bufsiz,
9936 note_name, NT_FPREGSET, fpregs, size);
9940 elfcore_write_prxfpreg (bfd *abfd,
9943 const void *xfpregs,
9946 char *note_name = "LINUX";
9947 return elfcore_write_note (abfd, buf, bufsiz,
9948 note_name, NT_PRXFPREG, xfpregs, size);
9952 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9953 const void *xfpregs, int size)
9956 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
9957 note_name = "FreeBSD";
9959 note_name = "LINUX";
9960 return elfcore_write_note (abfd, buf, bufsiz,
9961 note_name, NT_X86_XSTATE, xfpregs, size);
9965 elfcore_write_ppc_vmx (bfd *abfd,
9968 const void *ppc_vmx,
9971 char *note_name = "LINUX";
9972 return elfcore_write_note (abfd, buf, bufsiz,
9973 note_name, NT_PPC_VMX, ppc_vmx, size);
9977 elfcore_write_ppc_vsx (bfd *abfd,
9980 const void *ppc_vsx,
9983 char *note_name = "LINUX";
9984 return elfcore_write_note (abfd, buf, bufsiz,
9985 note_name, NT_PPC_VSX, ppc_vsx, size);
9989 elfcore_write_s390_high_gprs (bfd *abfd,
9992 const void *s390_high_gprs,
9995 char *note_name = "LINUX";
9996 return elfcore_write_note (abfd, buf, bufsiz,
9997 note_name, NT_S390_HIGH_GPRS,
9998 s390_high_gprs, size);
10002 elfcore_write_s390_timer (bfd *abfd,
10005 const void *s390_timer,
10008 char *note_name = "LINUX";
10009 return elfcore_write_note (abfd, buf, bufsiz,
10010 note_name, NT_S390_TIMER, s390_timer, size);
10014 elfcore_write_s390_todcmp (bfd *abfd,
10017 const void *s390_todcmp,
10020 char *note_name = "LINUX";
10021 return elfcore_write_note (abfd, buf, bufsiz,
10022 note_name, NT_S390_TODCMP, s390_todcmp, size);
10026 elfcore_write_s390_todpreg (bfd *abfd,
10029 const void *s390_todpreg,
10032 char *note_name = "LINUX";
10033 return elfcore_write_note (abfd, buf, bufsiz,
10034 note_name, NT_S390_TODPREG, s390_todpreg, size);
10038 elfcore_write_s390_ctrs (bfd *abfd,
10041 const void *s390_ctrs,
10044 char *note_name = "LINUX";
10045 return elfcore_write_note (abfd, buf, bufsiz,
10046 note_name, NT_S390_CTRS, s390_ctrs, size);
10050 elfcore_write_s390_prefix (bfd *abfd,
10053 const void *s390_prefix,
10056 char *note_name = "LINUX";
10057 return elfcore_write_note (abfd, buf, bufsiz,
10058 note_name, NT_S390_PREFIX, s390_prefix, size);
10062 elfcore_write_s390_last_break (bfd *abfd,
10065 const void *s390_last_break,
10068 char *note_name = "LINUX";
10069 return elfcore_write_note (abfd, buf, bufsiz,
10070 note_name, NT_S390_LAST_BREAK,
10071 s390_last_break, size);
10075 elfcore_write_s390_system_call (bfd *abfd,
10078 const void *s390_system_call,
10081 char *note_name = "LINUX";
10082 return elfcore_write_note (abfd, buf, bufsiz,
10083 note_name, NT_S390_SYSTEM_CALL,
10084 s390_system_call, size);
10088 elfcore_write_s390_tdb (bfd *abfd,
10091 const void *s390_tdb,
10094 char *note_name = "LINUX";
10095 return elfcore_write_note (abfd, buf, bufsiz,
10096 note_name, NT_S390_TDB, s390_tdb, size);
10100 elfcore_write_s390_vxrs_low (bfd *abfd,
10103 const void *s390_vxrs_low,
10106 char *note_name = "LINUX";
10107 return elfcore_write_note (abfd, buf, bufsiz,
10108 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
10112 elfcore_write_s390_vxrs_high (bfd *abfd,
10115 const void *s390_vxrs_high,
10118 char *note_name = "LINUX";
10119 return elfcore_write_note (abfd, buf, bufsiz,
10120 note_name, NT_S390_VXRS_HIGH,
10121 s390_vxrs_high, size);
10125 elfcore_write_arm_vfp (bfd *abfd,
10128 const void *arm_vfp,
10131 char *note_name = "LINUX";
10132 return elfcore_write_note (abfd, buf, bufsiz,
10133 note_name, NT_ARM_VFP, arm_vfp, size);
10137 elfcore_write_aarch_tls (bfd *abfd,
10140 const void *aarch_tls,
10143 char *note_name = "LINUX";
10144 return elfcore_write_note (abfd, buf, bufsiz,
10145 note_name, NT_ARM_TLS, aarch_tls, size);
10149 elfcore_write_aarch_hw_break (bfd *abfd,
10152 const void *aarch_hw_break,
10155 char *note_name = "LINUX";
10156 return elfcore_write_note (abfd, buf, bufsiz,
10157 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
10161 elfcore_write_aarch_hw_watch (bfd *abfd,
10164 const void *aarch_hw_watch,
10167 char *note_name = "LINUX";
10168 return elfcore_write_note (abfd, buf, bufsiz,
10169 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
10173 elfcore_write_register_note (bfd *abfd,
10176 const char *section,
10180 if (strcmp (section, ".reg2") == 0)
10181 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
10182 if (strcmp (section, ".reg-xfp") == 0)
10183 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
10184 if (strcmp (section, ".reg-xstate") == 0)
10185 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
10186 if (strcmp (section, ".reg-ppc-vmx") == 0)
10187 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
10188 if (strcmp (section, ".reg-ppc-vsx") == 0)
10189 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
10190 if (strcmp (section, ".reg-s390-high-gprs") == 0)
10191 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
10192 if (strcmp (section, ".reg-s390-timer") == 0)
10193 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
10194 if (strcmp (section, ".reg-s390-todcmp") == 0)
10195 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
10196 if (strcmp (section, ".reg-s390-todpreg") == 0)
10197 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
10198 if (strcmp (section, ".reg-s390-ctrs") == 0)
10199 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
10200 if (strcmp (section, ".reg-s390-prefix") == 0)
10201 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
10202 if (strcmp (section, ".reg-s390-last-break") == 0)
10203 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
10204 if (strcmp (section, ".reg-s390-system-call") == 0)
10205 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
10206 if (strcmp (section, ".reg-s390-tdb") == 0)
10207 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
10208 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
10209 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
10210 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
10211 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
10212 if (strcmp (section, ".reg-arm-vfp") == 0)
10213 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
10214 if (strcmp (section, ".reg-aarch-tls") == 0)
10215 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
10216 if (strcmp (section, ".reg-aarch-hw-break") == 0)
10217 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
10218 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
10219 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
10224 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
10229 while (p < buf + size)
10231 /* FIXME: bad alignment assumption. */
10232 Elf_External_Note *xnp = (Elf_External_Note *) p;
10233 Elf_Internal_Note in;
10235 if (offsetof (Elf_External_Note, name) > buf - p + size)
10238 in.type = H_GET_32 (abfd, xnp->type);
10240 in.namesz = H_GET_32 (abfd, xnp->namesz);
10241 in.namedata = xnp->name;
10242 if (in.namesz > buf - in.namedata + size)
10245 in.descsz = H_GET_32 (abfd, xnp->descsz);
10246 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
10247 in.descpos = offset + (in.descdata - buf);
10249 && (in.descdata >= buf + size
10250 || in.descsz > buf - in.descdata + size))
10253 switch (bfd_get_format (abfd))
10260 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10263 const char * string;
10265 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
10269 GROKER_ELEMENT ("", elfcore_grok_note),
10270 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
10271 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
10272 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
10273 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
10275 #undef GROKER_ELEMENT
10278 for (i = ARRAY_SIZE (grokers); i--;)
10280 if (in.namesz >= grokers[i].len
10281 && strncmp (in.namedata, grokers[i].string,
10282 grokers[i].len) == 0)
10284 if (! grokers[i].func (abfd, & in))
10293 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
10295 if (! elfobj_grok_gnu_note (abfd, &in))
10298 else if (in.namesz == sizeof "stapsdt"
10299 && strcmp (in.namedata, "stapsdt") == 0)
10301 if (! elfobj_grok_stapsdt_note (abfd, &in))
10307 p = in.descdata + BFD_ALIGN (in.descsz, 4);
10314 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
10321 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
10324 buf = (char *) bfd_malloc (size + 1);
10328 /* PR 17512: file: ec08f814
10329 0-termintate the buffer so that string searches will not overflow. */
10332 if (bfd_bread (buf, size, abfd) != size
10333 || !elf_parse_notes (abfd, buf, size, offset))
10343 /* Providing external access to the ELF program header table. */
10345 /* Return an upper bound on the number of bytes required to store a
10346 copy of ABFD's program header table entries. Return -1 if an error
10347 occurs; bfd_get_error will return an appropriate code. */
10350 bfd_get_elf_phdr_upper_bound (bfd *abfd)
10352 if (abfd->xvec->flavour != bfd_target_elf_flavour)
10354 bfd_set_error (bfd_error_wrong_format);
10358 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
10361 /* Copy ABFD's program header table entries to *PHDRS. The entries
10362 will be stored as an array of Elf_Internal_Phdr structures, as
10363 defined in include/elf/internal.h. To find out how large the
10364 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10366 Return the number of program header table entries read, or -1 if an
10367 error occurs; bfd_get_error will return an appropriate code. */
10370 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
10374 if (abfd->xvec->flavour != bfd_target_elf_flavour)
10376 bfd_set_error (bfd_error_wrong_format);
10380 num_phdrs = elf_elfheader (abfd)->e_phnum;
10381 memcpy (phdrs, elf_tdata (abfd)->phdr,
10382 num_phdrs * sizeof (Elf_Internal_Phdr));
10387 enum elf_reloc_type_class
10388 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
10389 const asection *rel_sec ATTRIBUTE_UNUSED,
10390 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
10392 return reloc_class_normal;
10395 /* For RELA architectures, return the relocation value for a
10396 relocation against a local symbol. */
10399 _bfd_elf_rela_local_sym (bfd *abfd,
10400 Elf_Internal_Sym *sym,
10402 Elf_Internal_Rela *rel)
10404 asection *sec = *psec;
10405 bfd_vma relocation;
10407 relocation = (sec->output_section->vma
10408 + sec->output_offset
10410 if ((sec->flags & SEC_MERGE)
10411 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
10412 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
10415 _bfd_merged_section_offset (abfd, psec,
10416 elf_section_data (sec)->sec_info,
10417 sym->st_value + rel->r_addend);
10420 /* If we have changed the section, and our original section is
10421 marked with SEC_EXCLUDE, it means that the original
10422 SEC_MERGE section has been completely subsumed in some
10423 other SEC_MERGE section. In this case, we need to leave
10424 some info around for --emit-relocs. */
10425 if ((sec->flags & SEC_EXCLUDE) != 0)
10426 sec->kept_section = *psec;
10429 rel->r_addend -= relocation;
10430 rel->r_addend += sec->output_section->vma + sec->output_offset;
10436 _bfd_elf_rel_local_sym (bfd *abfd,
10437 Elf_Internal_Sym *sym,
10441 asection *sec = *psec;
10443 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
10444 return sym->st_value + addend;
10446 return _bfd_merged_section_offset (abfd, psec,
10447 elf_section_data (sec)->sec_info,
10448 sym->st_value + addend);
10452 _bfd_elf_section_offset (bfd *abfd,
10453 struct bfd_link_info *info,
10457 switch (sec->sec_info_type)
10459 case SEC_INFO_TYPE_STABS:
10460 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
10462 case SEC_INFO_TYPE_EH_FRAME:
10463 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
10465 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
10467 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10468 bfd_size_type address_size = bed->s->arch_size / 8;
10469 offset = sec->size - offset - address_size;
10475 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10476 reconstruct an ELF file by reading the segments out of remote memory
10477 based on the ELF file header at EHDR_VMA and the ELF program headers it
10478 points to. If not null, *LOADBASEP is filled in with the difference
10479 between the VMAs from which the segments were read, and the VMAs the
10480 file headers (and hence BFD's idea of each section's VMA) put them at.
10482 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10483 remote memory at target address VMA into the local buffer at MYADDR; it
10484 should return zero on success or an `errno' code on failure. TEMPL must
10485 be a BFD for an ELF target with the word size and byte order found in
10486 the remote memory. */
10489 bfd_elf_bfd_from_remote_memory
10492 bfd_size_type size,
10493 bfd_vma *loadbasep,
10494 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
10496 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
10497 (templ, ehdr_vma, size, loadbasep, target_read_memory);
10501 _bfd_elf_get_synthetic_symtab (bfd *abfd,
10502 long symcount ATTRIBUTE_UNUSED,
10503 asymbol **syms ATTRIBUTE_UNUSED,
10508 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10511 const char *relplt_name;
10512 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
10516 Elf_Internal_Shdr *hdr;
10522 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
10525 if (dynsymcount <= 0)
10528 if (!bed->plt_sym_val)
10531 relplt_name = bed->relplt_name;
10532 if (relplt_name == NULL)
10533 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
10534 relplt = bfd_get_section_by_name (abfd, relplt_name);
10535 if (relplt == NULL)
10538 hdr = &elf_section_data (relplt)->this_hdr;
10539 if (hdr->sh_link != elf_dynsymtab (abfd)
10540 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
10543 plt = bfd_get_section_by_name (abfd, ".plt");
10547 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
10548 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
10551 count = relplt->size / hdr->sh_entsize;
10552 size = count * sizeof (asymbol);
10553 p = relplt->relocation;
10554 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10556 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
10557 if (p->addend != 0)
10560 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
10562 size += sizeof ("+0x") - 1 + 8;
10567 s = *ret = (asymbol *) bfd_malloc (size);
10571 names = (char *) (s + count);
10572 p = relplt->relocation;
10574 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10579 addr = bed->plt_sym_val (i, plt, p);
10580 if (addr == (bfd_vma) -1)
10583 *s = **p->sym_ptr_ptr;
10584 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10585 we are defining a symbol, ensure one of them is set. */
10586 if ((s->flags & BSF_LOCAL) == 0)
10587 s->flags |= BSF_GLOBAL;
10588 s->flags |= BSF_SYNTHETIC;
10590 s->value = addr - plt->vma;
10593 len = strlen ((*p->sym_ptr_ptr)->name);
10594 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10596 if (p->addend != 0)
10600 memcpy (names, "+0x", sizeof ("+0x") - 1);
10601 names += sizeof ("+0x") - 1;
10602 bfd_sprintf_vma (abfd, buf, p->addend);
10603 for (a = buf; *a == '0'; ++a)
10606 memcpy (names, a, len);
10609 memcpy (names, "@plt", sizeof ("@plt"));
10610 names += sizeof ("@plt");
10617 /* It is only used by x86-64 so far. */
10618 asection _bfd_elf_large_com_section
10619 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10620 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10623 _bfd_elf_post_process_headers (bfd * abfd,
10624 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10626 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10628 i_ehdrp = elf_elfheader (abfd);
10630 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10632 /* To make things simpler for the loader on Linux systems we set the
10633 osabi field to ELFOSABI_GNU if the binary contains symbols of
10634 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10635 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10636 && elf_tdata (abfd)->has_gnu_symbols)
10637 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10641 /* Return TRUE for ELF symbol types that represent functions.
10642 This is the default version of this function, which is sufficient for
10643 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10646 _bfd_elf_is_function_type (unsigned int type)
10648 return (type == STT_FUNC
10649 || type == STT_GNU_IFUNC);
10652 /* If the ELF symbol SYM might be a function in SEC, return the
10653 function size and set *CODE_OFF to the function's entry point,
10654 otherwise return zero. */
10657 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10660 bfd_size_type size;
10662 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10663 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10664 || sym->section != sec)
10667 *code_off = sym->value;
10669 if (!(sym->flags & BSF_SYNTHETIC))
10670 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;