1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet. */
34 /* For sparc64-cross-sparc32. */
42 #include "libiberty.h"
44 static int elf_sort_sections (const void *, const void *);
45 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
46 static bfd_boolean prep_headers (bfd *);
47 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
48 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
50 /* Swap version information in and out. The version information is
51 currently size independent. If that ever changes, this code will
52 need to move into elfcode.h. */
54 /* Swap in a Verdef structure. */
57 _bfd_elf_swap_verdef_in (bfd *abfd,
58 const Elf_External_Verdef *src,
59 Elf_Internal_Verdef *dst)
61 dst->vd_version = H_GET_16 (abfd, src->vd_version);
62 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
63 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
64 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
65 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
66 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
67 dst->vd_next = H_GET_32 (abfd, src->vd_next);
70 /* Swap out a Verdef structure. */
73 _bfd_elf_swap_verdef_out (bfd *abfd,
74 const Elf_Internal_Verdef *src,
75 Elf_External_Verdef *dst)
77 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
78 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
79 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
80 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
81 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
82 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
83 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
86 /* Swap in a Verdaux structure. */
89 _bfd_elf_swap_verdaux_in (bfd *abfd,
90 const Elf_External_Verdaux *src,
91 Elf_Internal_Verdaux *dst)
93 dst->vda_name = H_GET_32 (abfd, src->vda_name);
94 dst->vda_next = H_GET_32 (abfd, src->vda_next);
97 /* Swap out a Verdaux structure. */
100 _bfd_elf_swap_verdaux_out (bfd *abfd,
101 const Elf_Internal_Verdaux *src,
102 Elf_External_Verdaux *dst)
104 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
105 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
108 /* Swap in a Verneed structure. */
111 _bfd_elf_swap_verneed_in (bfd *abfd,
112 const Elf_External_Verneed *src,
113 Elf_Internal_Verneed *dst)
115 dst->vn_version = H_GET_16 (abfd, src->vn_version);
116 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
117 dst->vn_file = H_GET_32 (abfd, src->vn_file);
118 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
119 dst->vn_next = H_GET_32 (abfd, src->vn_next);
122 /* Swap out a Verneed structure. */
125 _bfd_elf_swap_verneed_out (bfd *abfd,
126 const Elf_Internal_Verneed *src,
127 Elf_External_Verneed *dst)
129 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
130 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
131 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
132 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
133 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
136 /* Swap in a Vernaux structure. */
139 _bfd_elf_swap_vernaux_in (bfd *abfd,
140 const Elf_External_Vernaux *src,
141 Elf_Internal_Vernaux *dst)
143 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
144 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
145 dst->vna_other = H_GET_16 (abfd, src->vna_other);
146 dst->vna_name = H_GET_32 (abfd, src->vna_name);
147 dst->vna_next = H_GET_32 (abfd, src->vna_next);
150 /* Swap out a Vernaux structure. */
153 _bfd_elf_swap_vernaux_out (bfd *abfd,
154 const Elf_Internal_Vernaux *src,
155 Elf_External_Vernaux *dst)
157 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
158 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
159 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
160 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
161 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
164 /* Swap in a Versym structure. */
167 _bfd_elf_swap_versym_in (bfd *abfd,
168 const Elf_External_Versym *src,
169 Elf_Internal_Versym *dst)
171 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
174 /* Swap out a Versym structure. */
177 _bfd_elf_swap_versym_out (bfd *abfd,
178 const Elf_Internal_Versym *src,
179 Elf_External_Versym *dst)
181 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
184 /* Standard ELF hash function. Do not change this function; you will
185 cause invalid hash tables to be generated. */
188 bfd_elf_hash (const char *namearg)
190 const unsigned char *name = (const unsigned char *) namearg;
195 while ((ch = *name++) != '\0')
198 if ((g = (h & 0xf0000000)) != 0)
201 /* The ELF ABI says `h &= ~g', but this is equivalent in
202 this case and on some machines one insn instead of two. */
206 return h & 0xffffffff;
209 /* Read a specified number of bytes at a specified offset in an ELF
210 file, into a newly allocated buffer, and return a pointer to the
214 elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
218 if ((buf = bfd_alloc (abfd, size)) == NULL)
220 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
222 if (bfd_bread (buf, size, abfd) != size)
224 if (bfd_get_error () != bfd_error_system_call)
225 bfd_set_error (bfd_error_file_truncated);
232 bfd_elf_mkobject (bfd *abfd)
234 /* This just does initialization. */
235 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
236 elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
237 if (elf_tdata (abfd) == 0)
239 /* Since everything is done at close time, do we need any
246 bfd_elf_mkcorefile (bfd *abfd)
248 /* I think this can be done just like an object file. */
249 return bfd_elf_mkobject (abfd);
253 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
255 Elf_Internal_Shdr **i_shdrp;
256 char *shstrtab = NULL;
258 bfd_size_type shstrtabsize;
260 i_shdrp = elf_elfsections (abfd);
261 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
264 shstrtab = (char *) i_shdrp[shindex]->contents;
265 if (shstrtab == NULL)
267 /* No cached one, attempt to read, and cache what we read. */
268 offset = i_shdrp[shindex]->sh_offset;
269 shstrtabsize = i_shdrp[shindex]->sh_size;
270 shstrtab = elf_read (abfd, offset, shstrtabsize);
271 i_shdrp[shindex]->contents = shstrtab;
277 bfd_elf_string_from_elf_section (bfd *abfd,
278 unsigned int shindex,
279 unsigned int strindex)
281 Elf_Internal_Shdr *hdr;
286 hdr = elf_elfsections (abfd)[shindex];
288 if (hdr->contents == NULL
289 && bfd_elf_get_str_section (abfd, shindex) == NULL)
292 if (strindex >= hdr->sh_size)
294 (*_bfd_error_handler)
295 (_("%B: invalid string offset %u >= %lu for section `%s'"),
296 abfd, strindex, (unsigned long) hdr->sh_size,
297 ((shindex == elf_elfheader(abfd)->e_shstrndx
298 && strindex == hdr->sh_name)
300 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
304 return ((char *) hdr->contents) + strindex;
307 /* Read and convert symbols to internal format.
308 SYMCOUNT specifies the number of symbols to read, starting from
309 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
310 are non-NULL, they are used to store the internal symbols, external
311 symbols, and symbol section index extensions, respectively. */
314 bfd_elf_get_elf_syms (bfd *ibfd,
315 Elf_Internal_Shdr *symtab_hdr,
318 Elf_Internal_Sym *intsym_buf,
320 Elf_External_Sym_Shndx *extshndx_buf)
322 Elf_Internal_Shdr *shndx_hdr;
324 const bfd_byte *esym;
325 Elf_External_Sym_Shndx *alloc_extshndx;
326 Elf_External_Sym_Shndx *shndx;
327 Elf_Internal_Sym *isym;
328 Elf_Internal_Sym *isymend;
329 const struct elf_backend_data *bed;
337 /* Normal syms might have section extension entries. */
339 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
340 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
342 /* Read the symbols. */
344 alloc_extshndx = NULL;
345 bed = get_elf_backend_data (ibfd);
346 extsym_size = bed->s->sizeof_sym;
347 amt = symcount * extsym_size;
348 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
349 if (extsym_buf == NULL)
351 alloc_ext = bfd_malloc (amt);
352 extsym_buf = alloc_ext;
354 if (extsym_buf == NULL
355 || bfd_seek (ibfd, pos, SEEK_SET) != 0
356 || bfd_bread (extsym_buf, amt, ibfd) != amt)
362 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
366 amt = symcount * sizeof (Elf_External_Sym_Shndx);
367 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
368 if (extshndx_buf == NULL)
370 alloc_extshndx = bfd_malloc (amt);
371 extshndx_buf = alloc_extshndx;
373 if (extshndx_buf == NULL
374 || bfd_seek (ibfd, pos, SEEK_SET) != 0
375 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
382 if (intsym_buf == NULL)
384 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
385 intsym_buf = bfd_malloc (amt);
386 if (intsym_buf == NULL)
390 /* Convert the symbols to internal form. */
391 isymend = intsym_buf + symcount;
392 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
394 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
395 (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym);
398 if (alloc_ext != NULL)
400 if (alloc_extshndx != NULL)
401 free (alloc_extshndx);
406 /* Look up a symbol name. */
408 bfd_elf_sym_name (bfd *abfd,
409 Elf_Internal_Shdr *symtab_hdr,
410 Elf_Internal_Sym *isym)
412 unsigned int iname = isym->st_name;
413 unsigned int shindex = symtab_hdr->sh_link;
414 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
415 /* Check for a bogus st_shndx to avoid crashing. */
416 && isym->st_shndx < elf_numsections (abfd)
417 && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
419 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
420 shindex = elf_elfheader (abfd)->e_shstrndx;
423 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
426 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
427 sections. The first element is the flags, the rest are section
430 typedef union elf_internal_group {
431 Elf_Internal_Shdr *shdr;
433 } Elf_Internal_Group;
435 /* Return the name of the group signature symbol. Why isn't the
436 signature just a string? */
439 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
441 Elf_Internal_Shdr *hdr;
442 unsigned char esym[sizeof (Elf64_External_Sym)];
443 Elf_External_Sym_Shndx eshndx;
444 Elf_Internal_Sym isym;
446 /* First we need to ensure the symbol table is available. */
447 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
450 /* Go read the symbol. */
451 hdr = &elf_tdata (abfd)->symtab_hdr;
452 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
453 &isym, esym, &eshndx) == NULL)
456 return bfd_elf_sym_name (abfd, hdr, &isym);
459 /* Set next_in_group list pointer, and group name for NEWSECT. */
462 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
464 unsigned int num_group = elf_tdata (abfd)->num_group;
466 /* If num_group is zero, read in all SHT_GROUP sections. The count
467 is set to -1 if there are no SHT_GROUP sections. */
470 unsigned int i, shnum;
472 /* First count the number of groups. If we have a SHT_GROUP
473 section with just a flag word (ie. sh_size is 4), ignore it. */
474 shnum = elf_numsections (abfd);
476 for (i = 0; i < shnum; i++)
478 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
479 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
484 num_group = (unsigned) -1;
485 elf_tdata (abfd)->num_group = num_group;
489 /* We keep a list of elf section headers for group sections,
490 so we can find them quickly. */
491 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
492 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
493 if (elf_tdata (abfd)->group_sect_ptr == NULL)
497 for (i = 0; i < shnum; i++)
499 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
500 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
503 Elf_Internal_Group *dest;
505 /* Add to list of sections. */
506 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
509 /* Read the raw contents. */
510 BFD_ASSERT (sizeof (*dest) >= 4);
511 amt = shdr->sh_size * sizeof (*dest) / 4;
512 shdr->contents = bfd_alloc (abfd, amt);
513 if (shdr->contents == NULL
514 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
515 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
519 /* Translate raw contents, a flag word followed by an
520 array of elf section indices all in target byte order,
521 to the flag word followed by an array of elf section
523 src = shdr->contents + shdr->sh_size;
524 dest = (Elf_Internal_Group *) (shdr->contents + amt);
531 idx = H_GET_32 (abfd, src);
532 if (src == shdr->contents)
535 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
536 shdr->bfd_section->flags
537 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
542 ((*_bfd_error_handler)
543 (_("%B: invalid SHT_GROUP entry"), abfd));
546 dest->shdr = elf_elfsections (abfd)[idx];
553 if (num_group != (unsigned) -1)
557 for (i = 0; i < num_group; i++)
559 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
560 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
561 unsigned int n_elt = shdr->sh_size / 4;
563 /* Look through this group's sections to see if current
564 section is a member. */
566 if ((++idx)->shdr == hdr)
570 /* We are a member of this group. Go looking through
571 other members to see if any others are linked via
573 idx = (Elf_Internal_Group *) shdr->contents;
574 n_elt = shdr->sh_size / 4;
576 if ((s = (++idx)->shdr->bfd_section) != NULL
577 && elf_next_in_group (s) != NULL)
581 /* Snarf the group name from other member, and
582 insert current section in circular list. */
583 elf_group_name (newsect) = elf_group_name (s);
584 elf_next_in_group (newsect) = elf_next_in_group (s);
585 elf_next_in_group (s) = newsect;
591 gname = group_signature (abfd, shdr);
594 elf_group_name (newsect) = gname;
596 /* Start a circular list with one element. */
597 elf_next_in_group (newsect) = newsect;
600 /* If the group section has been created, point to the
602 if (shdr->bfd_section != NULL)
603 elf_next_in_group (shdr->bfd_section) = newsect;
611 if (elf_group_name (newsect) == NULL)
613 (*_bfd_error_handler) (_("%B: no group info for section %A"),
620 _bfd_elf_setup_group_pointers (bfd *abfd)
623 unsigned int num_group = elf_tdata (abfd)->num_group;
624 bfd_boolean result = TRUE;
626 if (num_group == (unsigned) -1)
629 for (i = 0; i < num_group; i++)
631 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
632 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
633 unsigned int n_elt = shdr->sh_size / 4;
636 if ((++idx)->shdr->bfd_section)
637 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
638 else if (idx->shdr->sh_type == SHT_RELA
639 || idx->shdr->sh_type == SHT_REL)
640 /* We won't include relocation sections in section groups in
641 output object files. We adjust the group section size here
642 so that relocatable link will work correctly when
643 relocation sections are in section group in input object
645 shdr->bfd_section->size -= 4;
648 /* There are some unknown sections in the group. */
649 (*_bfd_error_handler)
650 (_("%B: unknown [%d] section `%s' in group [%s]"),
652 (unsigned int) idx->shdr->sh_type,
653 elf_string_from_elf_strtab (abfd, idx->shdr->sh_name),
654 shdr->bfd_section->name);
662 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
664 return elf_next_in_group (sec) != NULL;
667 /* Make a BFD section from an ELF section. We store a pointer to the
668 BFD section in the bfd_section field of the header. */
671 _bfd_elf_make_section_from_shdr (bfd *abfd,
672 Elf_Internal_Shdr *hdr,
677 const struct elf_backend_data *bed;
679 if (hdr->bfd_section != NULL)
681 BFD_ASSERT (strcmp (name,
682 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
686 newsect = bfd_make_section_anyway (abfd, name);
690 hdr->bfd_section = newsect;
691 elf_section_data (newsect)->this_hdr = *hdr;
693 /* Always use the real type/flags. */
694 elf_section_type (newsect) = hdr->sh_type;
695 elf_section_flags (newsect) = hdr->sh_flags;
697 newsect->filepos = hdr->sh_offset;
699 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
700 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
701 || ! bfd_set_section_alignment (abfd, newsect,
702 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
705 flags = SEC_NO_FLAGS;
706 if (hdr->sh_type != SHT_NOBITS)
707 flags |= SEC_HAS_CONTENTS;
708 if (hdr->sh_type == SHT_GROUP)
709 flags |= SEC_GROUP | SEC_EXCLUDE;
710 if ((hdr->sh_flags & SHF_ALLOC) != 0)
713 if (hdr->sh_type != SHT_NOBITS)
716 if ((hdr->sh_flags & SHF_WRITE) == 0)
717 flags |= SEC_READONLY;
718 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
720 else if ((flags & SEC_LOAD) != 0)
722 if ((hdr->sh_flags & SHF_MERGE) != 0)
725 newsect->entsize = hdr->sh_entsize;
726 if ((hdr->sh_flags & SHF_STRINGS) != 0)
727 flags |= SEC_STRINGS;
729 if (hdr->sh_flags & SHF_GROUP)
730 if (!setup_group (abfd, hdr, newsect))
732 if ((hdr->sh_flags & SHF_TLS) != 0)
733 flags |= SEC_THREAD_LOCAL;
735 /* The debugging sections appear to be recognized only by name, not
738 static const char *debug_sec_names [] =
747 for (i = ARRAY_SIZE (debug_sec_names); i--;)
748 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
752 flags |= SEC_DEBUGGING;
755 /* As a GNU extension, if the name begins with .gnu.linkonce, we
756 only link a single copy of the section. This is used to support
757 g++. g++ will emit each template expansion in its own section.
758 The symbols will be defined as weak, so that multiple definitions
759 are permitted. The GNU linker extension is to actually discard
760 all but one of the sections. */
761 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
762 && elf_next_in_group (newsect) == NULL)
763 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
765 bed = get_elf_backend_data (abfd);
766 if (bed->elf_backend_section_flags)
767 if (! bed->elf_backend_section_flags (&flags, hdr))
770 if (! bfd_set_section_flags (abfd, newsect, flags))
773 if ((flags & SEC_ALLOC) != 0)
775 Elf_Internal_Phdr *phdr;
778 /* Look through the phdrs to see if we need to adjust the lma.
779 If all the p_paddr fields are zero, we ignore them, since
780 some ELF linkers produce such output. */
781 phdr = elf_tdata (abfd)->phdr;
782 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
784 if (phdr->p_paddr != 0)
787 if (i < elf_elfheader (abfd)->e_phnum)
789 phdr = elf_tdata (abfd)->phdr;
790 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
792 /* This section is part of this segment if its file
793 offset plus size lies within the segment's memory
794 span and, if the section is loaded, the extent of the
795 loaded data lies within the extent of the segment.
797 Note - we used to check the p_paddr field as well, and
798 refuse to set the LMA if it was 0. This is wrong
799 though, as a perfectly valid initialised segment can
800 have a p_paddr of zero. Some architectures, eg ARM,
801 place special significance on the address 0 and
802 executables need to be able to have a segment which
803 covers this address. */
804 if (phdr->p_type == PT_LOAD
805 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
806 && (hdr->sh_offset + hdr->sh_size
807 <= phdr->p_offset + phdr->p_memsz)
808 && ((flags & SEC_LOAD) == 0
809 || (hdr->sh_offset + hdr->sh_size
810 <= phdr->p_offset + phdr->p_filesz)))
812 if ((flags & SEC_LOAD) == 0)
813 newsect->lma = (phdr->p_paddr
814 + hdr->sh_addr - phdr->p_vaddr);
816 /* We used to use the same adjustment for SEC_LOAD
817 sections, but that doesn't work if the segment
818 is packed with code from multiple VMAs.
819 Instead we calculate the section LMA based on
820 the segment LMA. It is assumed that the
821 segment will contain sections with contiguous
822 LMAs, even if the VMAs are not. */
823 newsect->lma = (phdr->p_paddr
824 + hdr->sh_offset - phdr->p_offset);
826 /* With contiguous segments, we can't tell from file
827 offsets whether a section with zero size should
828 be placed at the end of one segment or the
829 beginning of the next. Decide based on vaddr. */
830 if (hdr->sh_addr >= phdr->p_vaddr
831 && (hdr->sh_addr + hdr->sh_size
832 <= phdr->p_vaddr + phdr->p_memsz))
847 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
850 Helper functions for GDB to locate the string tables.
851 Since BFD hides string tables from callers, GDB needs to use an
852 internal hook to find them. Sun's .stabstr, in particular,
853 isn't even pointed to by the .stab section, so ordinary
854 mechanisms wouldn't work to find it, even if we had some.
857 struct elf_internal_shdr *
858 bfd_elf_find_section (bfd *abfd, char *name)
860 Elf_Internal_Shdr **i_shdrp;
865 i_shdrp = elf_elfsections (abfd);
868 shstrtab = bfd_elf_get_str_section (abfd,
869 elf_elfheader (abfd)->e_shstrndx);
870 if (shstrtab != NULL)
872 max = elf_numsections (abfd);
873 for (i = 1; i < max; i++)
874 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
881 const char *const bfd_elf_section_type_names[] = {
882 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
883 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
884 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
887 /* ELF relocs are against symbols. If we are producing relocatable
888 output, and the reloc is against an external symbol, and nothing
889 has given us any additional addend, the resulting reloc will also
890 be against the same symbol. In such a case, we don't want to
891 change anything about the way the reloc is handled, since it will
892 all be done at final link time. Rather than put special case code
893 into bfd_perform_relocation, all the reloc types use this howto
894 function. It just short circuits the reloc if producing
895 relocatable output against an external symbol. */
897 bfd_reloc_status_type
898 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
899 arelent *reloc_entry,
901 void *data ATTRIBUTE_UNUSED,
902 asection *input_section,
904 char **error_message ATTRIBUTE_UNUSED)
906 if (output_bfd != NULL
907 && (symbol->flags & BSF_SECTION_SYM) == 0
908 && (! reloc_entry->howto->partial_inplace
909 || reloc_entry->addend == 0))
911 reloc_entry->address += input_section->output_offset;
915 return bfd_reloc_continue;
918 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
921 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
924 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
925 sec->sec_info_type = ELF_INFO_TYPE_NONE;
928 /* Finish SHF_MERGE section merging. */
931 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
936 if (!is_elf_hash_table (info->hash))
939 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
940 if ((ibfd->flags & DYNAMIC) == 0)
941 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
942 if ((sec->flags & SEC_MERGE) != 0
943 && !bfd_is_abs_section (sec->output_section))
945 struct bfd_elf_section_data *secdata;
947 secdata = elf_section_data (sec);
948 if (! _bfd_add_merge_section (abfd,
949 &elf_hash_table (info)->merge_info,
950 sec, &secdata->sec_info))
952 else if (secdata->sec_info)
953 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
956 if (elf_hash_table (info)->merge_info != NULL)
957 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
958 merge_sections_remove_hook);
963 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
965 sec->output_section = bfd_abs_section_ptr;
966 sec->output_offset = sec->vma;
967 if (!is_elf_hash_table (info->hash))
970 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
973 /* Copy the program header and other data from one object module to
977 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
979 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
980 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
983 BFD_ASSERT (!elf_flags_init (obfd)
984 || (elf_elfheader (obfd)->e_flags
985 == elf_elfheader (ibfd)->e_flags));
987 elf_gp (obfd) = elf_gp (ibfd);
988 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
989 elf_flags_init (obfd) = TRUE;
993 /* Print out the program headers. */
996 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
999 Elf_Internal_Phdr *p;
1001 bfd_byte *dynbuf = NULL;
1003 p = elf_tdata (abfd)->phdr;
1008 fprintf (f, _("\nProgram Header:\n"));
1009 c = elf_elfheader (abfd)->e_phnum;
1010 for (i = 0; i < c; i++, p++)
1017 case PT_NULL: pt = "NULL"; break;
1018 case PT_LOAD: pt = "LOAD"; break;
1019 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1020 case PT_INTERP: pt = "INTERP"; break;
1021 case PT_NOTE: pt = "NOTE"; break;
1022 case PT_SHLIB: pt = "SHLIB"; break;
1023 case PT_PHDR: pt = "PHDR"; break;
1024 case PT_TLS: pt = "TLS"; break;
1025 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1026 case PT_GNU_STACK: pt = "STACK"; break;
1027 case PT_GNU_RELRO: pt = "RELRO"; break;
1028 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1030 fprintf (f, "%8s off 0x", pt);
1031 bfd_fprintf_vma (abfd, f, p->p_offset);
1032 fprintf (f, " vaddr 0x");
1033 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1034 fprintf (f, " paddr 0x");
1035 bfd_fprintf_vma (abfd, f, p->p_paddr);
1036 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1037 fprintf (f, " filesz 0x");
1038 bfd_fprintf_vma (abfd, f, p->p_filesz);
1039 fprintf (f, " memsz 0x");
1040 bfd_fprintf_vma (abfd, f, p->p_memsz);
1041 fprintf (f, " flags %c%c%c",
1042 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1043 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1044 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1045 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1046 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1051 s = bfd_get_section_by_name (abfd, ".dynamic");
1055 unsigned long shlink;
1056 bfd_byte *extdyn, *extdynend;
1058 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1060 fprintf (f, _("\nDynamic Section:\n"));
1062 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1065 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1068 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1070 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1071 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1074 extdynend = extdyn + s->size;
1075 for (; extdyn < extdynend; extdyn += extdynsize)
1077 Elf_Internal_Dyn dyn;
1080 bfd_boolean stringp;
1082 (*swap_dyn_in) (abfd, extdyn, &dyn);
1084 if (dyn.d_tag == DT_NULL)
1091 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1095 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1096 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1097 case DT_PLTGOT: name = "PLTGOT"; break;
1098 case DT_HASH: name = "HASH"; break;
1099 case DT_STRTAB: name = "STRTAB"; break;
1100 case DT_SYMTAB: name = "SYMTAB"; break;
1101 case DT_RELA: name = "RELA"; break;
1102 case DT_RELASZ: name = "RELASZ"; break;
1103 case DT_RELAENT: name = "RELAENT"; break;
1104 case DT_STRSZ: name = "STRSZ"; break;
1105 case DT_SYMENT: name = "SYMENT"; break;
1106 case DT_INIT: name = "INIT"; break;
1107 case DT_FINI: name = "FINI"; break;
1108 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1109 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1110 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1111 case DT_REL: name = "REL"; break;
1112 case DT_RELSZ: name = "RELSZ"; break;
1113 case DT_RELENT: name = "RELENT"; break;
1114 case DT_PLTREL: name = "PLTREL"; break;
1115 case DT_DEBUG: name = "DEBUG"; break;
1116 case DT_TEXTREL: name = "TEXTREL"; break;
1117 case DT_JMPREL: name = "JMPREL"; break;
1118 case DT_BIND_NOW: name = "BIND_NOW"; break;
1119 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1120 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1121 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1122 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1123 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1124 case DT_FLAGS: name = "FLAGS"; break;
1125 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1126 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1127 case DT_CHECKSUM: name = "CHECKSUM"; break;
1128 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1129 case DT_MOVEENT: name = "MOVEENT"; break;
1130 case DT_MOVESZ: name = "MOVESZ"; break;
1131 case DT_FEATURE: name = "FEATURE"; break;
1132 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1133 case DT_SYMINSZ: name = "SYMINSZ"; break;
1134 case DT_SYMINENT: name = "SYMINENT"; break;
1135 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1136 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1137 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1138 case DT_PLTPAD: name = "PLTPAD"; break;
1139 case DT_MOVETAB: name = "MOVETAB"; break;
1140 case DT_SYMINFO: name = "SYMINFO"; break;
1141 case DT_RELACOUNT: name = "RELACOUNT"; break;
1142 case DT_RELCOUNT: name = "RELCOUNT"; break;
1143 case DT_FLAGS_1: name = "FLAGS_1"; break;
1144 case DT_VERSYM: name = "VERSYM"; break;
1145 case DT_VERDEF: name = "VERDEF"; break;
1146 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1147 case DT_VERNEED: name = "VERNEED"; break;
1148 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1149 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1150 case DT_USED: name = "USED"; break;
1151 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1154 fprintf (f, " %-11s ", name);
1156 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1160 unsigned int tagv = dyn.d_un.d_val;
1162 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1165 fprintf (f, "%s", string);
1174 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1175 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1177 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1181 if (elf_dynverdef (abfd) != 0)
1183 Elf_Internal_Verdef *t;
1185 fprintf (f, _("\nVersion definitions:\n"));
1186 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1188 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1189 t->vd_flags, t->vd_hash, t->vd_nodename);
1190 if (t->vd_auxptr->vda_nextptr != NULL)
1192 Elf_Internal_Verdaux *a;
1195 for (a = t->vd_auxptr->vda_nextptr;
1198 fprintf (f, "%s ", a->vda_nodename);
1204 if (elf_dynverref (abfd) != 0)
1206 Elf_Internal_Verneed *t;
1208 fprintf (f, _("\nVersion References:\n"));
1209 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1211 Elf_Internal_Vernaux *a;
1213 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1214 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1215 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1216 a->vna_flags, a->vna_other, a->vna_nodename);
1228 /* Display ELF-specific fields of a symbol. */
1231 bfd_elf_print_symbol (bfd *abfd,
1234 bfd_print_symbol_type how)
1239 case bfd_print_symbol_name:
1240 fprintf (file, "%s", symbol->name);
1242 case bfd_print_symbol_more:
1243 fprintf (file, "elf ");
1244 bfd_fprintf_vma (abfd, file, symbol->value);
1245 fprintf (file, " %lx", (long) symbol->flags);
1247 case bfd_print_symbol_all:
1249 const char *section_name;
1250 const char *name = NULL;
1251 const struct elf_backend_data *bed;
1252 unsigned char st_other;
1255 section_name = symbol->section ? symbol->section->name : "(*none*)";
1257 bed = get_elf_backend_data (abfd);
1258 if (bed->elf_backend_print_symbol_all)
1259 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1263 name = symbol->name;
1264 bfd_print_symbol_vandf (abfd, file, symbol);
1267 fprintf (file, " %s\t", section_name);
1268 /* Print the "other" value for a symbol. For common symbols,
1269 we've already printed the size; now print the alignment.
1270 For other symbols, we have no specified alignment, and
1271 we've printed the address; now print the size. */
1272 if (bfd_is_com_section (symbol->section))
1273 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1275 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1276 bfd_fprintf_vma (abfd, file, val);
1278 /* If we have version information, print it. */
1279 if (elf_tdata (abfd)->dynversym_section != 0
1280 && (elf_tdata (abfd)->dynverdef_section != 0
1281 || elf_tdata (abfd)->dynverref_section != 0))
1283 unsigned int vernum;
1284 const char *version_string;
1286 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1289 version_string = "";
1290 else if (vernum == 1)
1291 version_string = "Base";
1292 else if (vernum <= elf_tdata (abfd)->cverdefs)
1294 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1297 Elf_Internal_Verneed *t;
1299 version_string = "";
1300 for (t = elf_tdata (abfd)->verref;
1304 Elf_Internal_Vernaux *a;
1306 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1308 if (a->vna_other == vernum)
1310 version_string = a->vna_nodename;
1317 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1318 fprintf (file, " %-11s", version_string);
1323 fprintf (file, " (%s)", version_string);
1324 for (i = 10 - strlen (version_string); i > 0; --i)
1329 /* If the st_other field is not zero, print it. */
1330 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1335 case STV_INTERNAL: fprintf (file, " .internal"); break;
1336 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1337 case STV_PROTECTED: fprintf (file, " .protected"); break;
1339 /* Some other non-defined flags are also present, so print
1341 fprintf (file, " 0x%02x", (unsigned int) st_other);
1344 fprintf (file, " %s", name);
1350 /* Create an entry in an ELF linker hash table. */
1352 struct bfd_hash_entry *
1353 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1354 struct bfd_hash_table *table,
1357 /* Allocate the structure if it has not already been allocated by a
1361 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1366 /* Call the allocation method of the superclass. */
1367 entry = _bfd_link_hash_newfunc (entry, table, string);
1370 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1371 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1373 /* Set local fields. */
1376 ret->got = ret->plt = htab->init_refcount;
1377 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
1378 - offsetof (struct elf_link_hash_entry, size)));
1379 /* Assume that we have been called by a non-ELF symbol reader.
1380 This flag is then reset by the code which reads an ELF input
1381 file. This ensures that a symbol created by a non-ELF symbol
1382 reader will have the flag set correctly. */
1389 /* Copy data from an indirect symbol to its direct symbol, hiding the
1390 old indirect symbol. Also used for copying flags to a weakdef. */
1393 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data *bed,
1394 struct elf_link_hash_entry *dir,
1395 struct elf_link_hash_entry *ind)
1398 bfd_signed_vma lowest_valid = bed->can_refcount;
1400 /* Copy down any references that we may have already seen to the
1401 symbol which just became indirect. */
1403 dir->ref_dynamic |= ind->ref_dynamic;
1404 dir->ref_regular |= ind->ref_regular;
1405 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1406 dir->non_got_ref |= ind->non_got_ref;
1407 dir->needs_plt |= ind->needs_plt;
1408 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1410 if (ind->root.type != bfd_link_hash_indirect)
1413 /* Copy over the global and procedure linkage table refcount entries.
1414 These may have been already set up by a check_relocs routine. */
1415 tmp = dir->got.refcount;
1416 if (tmp < lowest_valid)
1418 dir->got.refcount = ind->got.refcount;
1419 ind->got.refcount = tmp;
1422 BFD_ASSERT (ind->got.refcount < lowest_valid);
1424 tmp = dir->plt.refcount;
1425 if (tmp < lowest_valid)
1427 dir->plt.refcount = ind->plt.refcount;
1428 ind->plt.refcount = tmp;
1431 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1433 if (dir->dynindx == -1)
1435 dir->dynindx = ind->dynindx;
1436 dir->dynstr_index = ind->dynstr_index;
1438 ind->dynstr_index = 0;
1441 BFD_ASSERT (ind->dynindx == -1);
1445 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1446 struct elf_link_hash_entry *h,
1447 bfd_boolean force_local)
1449 h->plt = elf_hash_table (info)->init_offset;
1453 h->forced_local = 1;
1454 if (h->dynindx != -1)
1457 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1463 /* Initialize an ELF linker hash table. */
1466 _bfd_elf_link_hash_table_init
1467 (struct elf_link_hash_table *table,
1469 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1470 struct bfd_hash_table *,
1475 table->dynamic_sections_created = FALSE;
1476 table->dynobj = NULL;
1477 /* Make sure can_refcount is extended to the width and signedness of
1478 init_refcount before we subtract one from it. */
1479 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1480 table->init_refcount.refcount -= 1;
1481 table->init_offset.offset = -(bfd_vma) 1;
1482 /* The first dynamic symbol is a dummy. */
1483 table->dynsymcount = 1;
1484 table->dynstr = NULL;
1485 table->bucketcount = 0;
1486 table->needed = NULL;
1488 table->merge_info = NULL;
1489 memset (&table->stab_info, 0, sizeof (table->stab_info));
1490 memset (&table->eh_info, 0, sizeof (table->eh_info));
1491 table->dynlocal = NULL;
1492 table->runpath = NULL;
1493 table->tls_sec = NULL;
1494 table->tls_size = 0;
1495 table->loaded = NULL;
1497 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1498 table->root.type = bfd_link_elf_hash_table;
1503 /* Create an ELF linker hash table. */
1505 struct bfd_link_hash_table *
1506 _bfd_elf_link_hash_table_create (bfd *abfd)
1508 struct elf_link_hash_table *ret;
1509 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1511 ret = bfd_malloc (amt);
1515 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1524 /* This is a hook for the ELF emulation code in the generic linker to
1525 tell the backend linker what file name to use for the DT_NEEDED
1526 entry for a dynamic object. */
1529 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1531 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1532 && bfd_get_format (abfd) == bfd_object)
1533 elf_dt_name (abfd) = name;
1537 bfd_elf_get_dyn_lib_class (bfd *abfd)
1540 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1541 && bfd_get_format (abfd) == bfd_object)
1542 lib_class = elf_dyn_lib_class (abfd);
1549 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
1551 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1552 && bfd_get_format (abfd) == bfd_object)
1553 elf_dyn_lib_class (abfd) = lib_class;
1556 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1557 the linker ELF emulation code. */
1559 struct bfd_link_needed_list *
1560 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1561 struct bfd_link_info *info)
1563 if (! is_elf_hash_table (info->hash))
1565 return elf_hash_table (info)->needed;
1568 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1569 hook for the linker ELF emulation code. */
1571 struct bfd_link_needed_list *
1572 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1573 struct bfd_link_info *info)
1575 if (! is_elf_hash_table (info->hash))
1577 return elf_hash_table (info)->runpath;
1580 /* Get the name actually used for a dynamic object for a link. This
1581 is the SONAME entry if there is one. Otherwise, it is the string
1582 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1585 bfd_elf_get_dt_soname (bfd *abfd)
1587 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1588 && bfd_get_format (abfd) == bfd_object)
1589 return elf_dt_name (abfd);
1593 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1594 the ELF linker emulation code. */
1597 bfd_elf_get_bfd_needed_list (bfd *abfd,
1598 struct bfd_link_needed_list **pneeded)
1601 bfd_byte *dynbuf = NULL;
1603 unsigned long shlink;
1604 bfd_byte *extdyn, *extdynend;
1606 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1610 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1611 || bfd_get_format (abfd) != bfd_object)
1614 s = bfd_get_section_by_name (abfd, ".dynamic");
1615 if (s == NULL || s->size == 0)
1618 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1621 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1625 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1627 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1628 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1631 extdynend = extdyn + s->size;
1632 for (; extdyn < extdynend; extdyn += extdynsize)
1634 Elf_Internal_Dyn dyn;
1636 (*swap_dyn_in) (abfd, extdyn, &dyn);
1638 if (dyn.d_tag == DT_NULL)
1641 if (dyn.d_tag == DT_NEEDED)
1644 struct bfd_link_needed_list *l;
1645 unsigned int tagv = dyn.d_un.d_val;
1648 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1653 l = bfd_alloc (abfd, amt);
1674 /* Allocate an ELF string table--force the first byte to be zero. */
1676 struct bfd_strtab_hash *
1677 _bfd_elf_stringtab_init (void)
1679 struct bfd_strtab_hash *ret;
1681 ret = _bfd_stringtab_init ();
1686 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1687 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1688 if (loc == (bfd_size_type) -1)
1690 _bfd_stringtab_free (ret);
1697 /* ELF .o/exec file reading */
1699 /* Create a new bfd section from an ELF section header. */
1702 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1704 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1705 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1706 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1709 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1711 switch (hdr->sh_type)
1714 /* Inactive section. Throw it away. */
1717 case SHT_PROGBITS: /* Normal section with contents. */
1718 case SHT_NOBITS: /* .bss section. */
1719 case SHT_HASH: /* .hash section. */
1720 case SHT_NOTE: /* .note section. */
1721 case SHT_INIT_ARRAY: /* .init_array section. */
1722 case SHT_FINI_ARRAY: /* .fini_array section. */
1723 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1724 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1725 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1727 case SHT_DYNAMIC: /* Dynamic linking information. */
1728 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1730 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1732 Elf_Internal_Shdr *dynsymhdr;
1734 /* The shared libraries distributed with hpux11 have a bogus
1735 sh_link field for the ".dynamic" section. Find the
1736 string table for the ".dynsym" section instead. */
1737 if (elf_dynsymtab (abfd) != 0)
1739 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1740 hdr->sh_link = dynsymhdr->sh_link;
1744 unsigned int i, num_sec;
1746 num_sec = elf_numsections (abfd);
1747 for (i = 1; i < num_sec; i++)
1749 dynsymhdr = elf_elfsections (abfd)[i];
1750 if (dynsymhdr->sh_type == SHT_DYNSYM)
1752 hdr->sh_link = dynsymhdr->sh_link;
1760 case SHT_SYMTAB: /* A symbol table */
1761 if (elf_onesymtab (abfd) == shindex)
1764 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1765 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1766 elf_onesymtab (abfd) = shindex;
1767 elf_tdata (abfd)->symtab_hdr = *hdr;
1768 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1769 abfd->flags |= HAS_SYMS;
1771 /* Sometimes a shared object will map in the symbol table. If
1772 SHF_ALLOC is set, and this is a shared object, then we also
1773 treat this section as a BFD section. We can not base the
1774 decision purely on SHF_ALLOC, because that flag is sometimes
1775 set in a relocatable object file, which would confuse the
1777 if ((hdr->sh_flags & SHF_ALLOC) != 0
1778 && (abfd->flags & DYNAMIC) != 0
1779 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1784 case SHT_DYNSYM: /* A dynamic symbol table */
1785 if (elf_dynsymtab (abfd) == shindex)
1788 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1789 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1790 elf_dynsymtab (abfd) = shindex;
1791 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1792 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1793 abfd->flags |= HAS_SYMS;
1795 /* Besides being a symbol table, we also treat this as a regular
1796 section, so that objcopy can handle it. */
1797 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1799 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1800 if (elf_symtab_shndx (abfd) == shindex)
1803 /* Get the associated symbol table. */
1804 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1805 || hdr->sh_link != elf_onesymtab (abfd))
1808 elf_symtab_shndx (abfd) = shindex;
1809 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1810 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1813 case SHT_STRTAB: /* A string table */
1814 if (hdr->bfd_section != NULL)
1816 if (ehdr->e_shstrndx == shindex)
1818 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1819 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1823 unsigned int i, num_sec;
1825 num_sec = elf_numsections (abfd);
1826 for (i = 1; i < num_sec; i++)
1828 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1829 if (hdr2->sh_link == shindex)
1831 if (! bfd_section_from_shdr (abfd, i))
1833 if (elf_onesymtab (abfd) == i)
1835 elf_tdata (abfd)->strtab_hdr = *hdr;
1836 elf_elfsections (abfd)[shindex] =
1837 &elf_tdata (abfd)->strtab_hdr;
1840 if (elf_dynsymtab (abfd) == i)
1842 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1843 elf_elfsections (abfd)[shindex] = hdr =
1844 &elf_tdata (abfd)->dynstrtab_hdr;
1845 /* We also treat this as a regular section, so
1846 that objcopy can handle it. */
1849 #if 0 /* Not handling other string tables specially right now. */
1850 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1851 /* We have a strtab for some random other section. */
1852 newsect = (asection *) hdr2->bfd_section;
1855 hdr->bfd_section = newsect;
1856 hdr2 = &elf_section_data (newsect)->str_hdr;
1858 elf_elfsections (abfd)[shindex] = hdr2;
1864 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1868 /* *These* do a lot of work -- but build no sections! */
1870 asection *target_sect;
1871 Elf_Internal_Shdr *hdr2;
1872 unsigned int num_sec = elf_numsections (abfd);
1874 /* Check for a bogus link to avoid crashing. */
1875 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1876 || hdr->sh_link >= num_sec)
1878 ((*_bfd_error_handler)
1879 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1880 abfd, hdr->sh_link, name, shindex));
1881 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1884 /* For some incomprehensible reason Oracle distributes
1885 libraries for Solaris in which some of the objects have
1886 bogus sh_link fields. It would be nice if we could just
1887 reject them, but, unfortunately, some people need to use
1888 them. We scan through the section headers; if we find only
1889 one suitable symbol table, we clobber the sh_link to point
1890 to it. I hope this doesn't break anything. */
1891 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1892 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1898 for (scan = 1; scan < num_sec; scan++)
1900 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1901 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1912 hdr->sh_link = found;
1915 /* Get the symbol table. */
1916 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1917 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1920 /* If this reloc section does not use the main symbol table we
1921 don't treat it as a reloc section. BFD can't adequately
1922 represent such a section, so at least for now, we don't
1923 try. We just present it as a normal section. We also
1924 can't use it as a reloc section if it points to the null
1926 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1927 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1929 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1931 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1932 if (target_sect == NULL)
1935 if ((target_sect->flags & SEC_RELOC) == 0
1936 || target_sect->reloc_count == 0)
1937 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1941 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1942 amt = sizeof (*hdr2);
1943 hdr2 = bfd_alloc (abfd, amt);
1944 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1947 elf_elfsections (abfd)[shindex] = hdr2;
1948 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1949 target_sect->flags |= SEC_RELOC;
1950 target_sect->relocation = NULL;
1951 target_sect->rel_filepos = hdr->sh_offset;
1952 /* In the section to which the relocations apply, mark whether
1953 its relocations are of the REL or RELA variety. */
1954 if (hdr->sh_size != 0)
1955 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1956 abfd->flags |= HAS_RELOC;
1961 case SHT_GNU_verdef:
1962 elf_dynverdef (abfd) = shindex;
1963 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1964 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1967 case SHT_GNU_versym:
1968 elf_dynversym (abfd) = shindex;
1969 elf_tdata (abfd)->dynversym_hdr = *hdr;
1970 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1973 case SHT_GNU_verneed:
1974 elf_dynverref (abfd) = shindex;
1975 elf_tdata (abfd)->dynverref_hdr = *hdr;
1976 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1983 /* We need a BFD section for objcopy and relocatable linking,
1984 and it's handy to have the signature available as the section
1986 name = group_signature (abfd, hdr);
1989 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1991 if (hdr->contents != NULL)
1993 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1994 unsigned int n_elt = hdr->sh_size / 4;
1997 if (idx->flags & GRP_COMDAT)
1998 hdr->bfd_section->flags
1999 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2001 /* We try to keep the same section order as it comes in. */
2003 while (--n_elt != 0)
2004 if ((s = (--idx)->shdr->bfd_section) != NULL
2005 && elf_next_in_group (s) != NULL)
2007 elf_next_in_group (hdr->bfd_section) = s;
2014 /* Check for any processor-specific section types. */
2016 if (bed->elf_backend_section_from_shdr)
2017 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2025 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2026 Return SEC for sections that have no elf section, and NULL on error. */
2029 bfd_section_from_r_symndx (bfd *abfd,
2030 struct sym_sec_cache *cache,
2032 unsigned long r_symndx)
2034 Elf_Internal_Shdr *symtab_hdr;
2035 unsigned char esym[sizeof (Elf64_External_Sym)];
2036 Elf_External_Sym_Shndx eshndx;
2037 Elf_Internal_Sym isym;
2038 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2040 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2041 return cache->sec[ent];
2043 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2044 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2045 &isym, esym, &eshndx) == NULL)
2048 if (cache->abfd != abfd)
2050 memset (cache->indx, -1, sizeof (cache->indx));
2053 cache->indx[ent] = r_symndx;
2054 cache->sec[ent] = sec;
2055 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2056 || isym.st_shndx > SHN_HIRESERVE)
2059 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2061 cache->sec[ent] = s;
2063 return cache->sec[ent];
2066 /* Given an ELF section number, retrieve the corresponding BFD
2070 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2072 if (index >= elf_numsections (abfd))
2074 return elf_elfsections (abfd)[index]->bfd_section;
2077 static struct bfd_elf_special_section const special_sections[] =
2079 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2080 { ".gnu.linkonce.b",15, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2081 { ".comment", 8, 0, SHT_PROGBITS, 0 },
2082 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2083 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2084 { ".debug", 6, 0, SHT_PROGBITS, 0 },
2085 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2086 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2087 { ".line", 5, 0, SHT_PROGBITS, 0 },
2088 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC },
2089 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC },
2090 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2091 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2092 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2093 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2094 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2095 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2096 { ".debug_line", 11, 0, SHT_PROGBITS, 0 },
2097 { ".debug_info", 11, 0, SHT_PROGBITS, 0 },
2098 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 },
2099 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 },
2100 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC },
2101 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC },
2102 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC },
2103 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2104 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC },
2105 { ".interp", 7, 0, SHT_PROGBITS, 0 },
2106 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2107 { ".shstrtab", 9, 0, SHT_STRTAB, 0 },
2108 { ".strtab", 7, 0, SHT_STRTAB, 0 },
2109 { ".symtab", 7, 0, SHT_SYMTAB, 0 },
2110 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 },
2111 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 },
2112 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 },
2113 { ".note.GNU-stack",15, 0, SHT_PROGBITS, 0 },
2114 { ".note", 5, -1, SHT_NOTE, 0 },
2115 { ".rela", 5, -1, SHT_RELA, 0 },
2116 { ".rel", 4, -1, SHT_REL, 0 },
2117 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2118 { ".gnu.liblist", 12, 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2119 { ".gnu.conflict", 13, 0, SHT_RELA, SHF_ALLOC },
2120 { NULL, 0, 0, 0, 0 }
2123 static const struct bfd_elf_special_section *
2124 get_special_section (const char *name,
2125 const struct bfd_elf_special_section *special_sections,
2129 int len = strlen (name);
2131 for (i = 0; special_sections[i].prefix != NULL; i++)
2134 int prefix_len = special_sections[i].prefix_length;
2136 if (len < prefix_len)
2138 if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2141 suffix_len = special_sections[i].suffix_length;
2142 if (suffix_len <= 0)
2144 if (name[prefix_len] != 0)
2146 if (suffix_len == 0)
2148 if (name[prefix_len] != '.'
2149 && (suffix_len == -2
2150 || (rela && special_sections[i].type == SHT_REL)))
2156 if (len < prefix_len + suffix_len)
2158 if (memcmp (name + len - suffix_len,
2159 special_sections[i].prefix + prefix_len,
2163 return &special_sections[i];
2169 const struct bfd_elf_special_section *
2170 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2173 const struct bfd_elf_special_section *ssect = NULL;
2175 /* See if this is one of the special sections. */
2178 unsigned int rela = bed->default_use_rela_p;
2180 if (bed->special_sections)
2181 ssect = get_special_section (name, bed->special_sections, rela);
2184 ssect = get_special_section (name, special_sections, rela);
2191 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2193 struct bfd_elf_section_data *sdata;
2194 const struct bfd_elf_special_section *ssect;
2196 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2199 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2202 sec->used_by_bfd = sdata;
2205 elf_section_type (sec) = SHT_NULL;
2206 ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2209 elf_section_type (sec) = ssect->type;
2210 elf_section_flags (sec) = ssect->attr;
2213 /* Indicate whether or not this section should use RELA relocations. */
2214 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2219 /* Create a new bfd section from an ELF program header.
2221 Since program segments have no names, we generate a synthetic name
2222 of the form segment<NUM>, where NUM is generally the index in the
2223 program header table. For segments that are split (see below) we
2224 generate the names segment<NUM>a and segment<NUM>b.
2226 Note that some program segments may have a file size that is different than
2227 (less than) the memory size. All this means is that at execution the
2228 system must allocate the amount of memory specified by the memory size,
2229 but only initialize it with the first "file size" bytes read from the
2230 file. This would occur for example, with program segments consisting
2231 of combined data+bss.
2233 To handle the above situation, this routine generates TWO bfd sections
2234 for the single program segment. The first has the length specified by
2235 the file size of the segment, and the second has the length specified
2236 by the difference between the two sizes. In effect, the segment is split
2237 into it's initialized and uninitialized parts.
2242 _bfd_elf_make_section_from_phdr (bfd *abfd,
2243 Elf_Internal_Phdr *hdr,
2245 const char *typename)
2253 split = ((hdr->p_memsz > 0)
2254 && (hdr->p_filesz > 0)
2255 && (hdr->p_memsz > hdr->p_filesz));
2256 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2257 len = strlen (namebuf) + 1;
2258 name = bfd_alloc (abfd, len);
2261 memcpy (name, namebuf, len);
2262 newsect = bfd_make_section (abfd, name);
2263 if (newsect == NULL)
2265 newsect->vma = hdr->p_vaddr;
2266 newsect->lma = hdr->p_paddr;
2267 newsect->size = hdr->p_filesz;
2268 newsect->filepos = hdr->p_offset;
2269 newsect->flags |= SEC_HAS_CONTENTS;
2270 newsect->alignment_power = bfd_log2 (hdr->p_align);
2271 if (hdr->p_type == PT_LOAD)
2273 newsect->flags |= SEC_ALLOC;
2274 newsect->flags |= SEC_LOAD;
2275 if (hdr->p_flags & PF_X)
2277 /* FIXME: all we known is that it has execute PERMISSION,
2279 newsect->flags |= SEC_CODE;
2282 if (!(hdr->p_flags & PF_W))
2284 newsect->flags |= SEC_READONLY;
2289 sprintf (namebuf, "%s%db", typename, index);
2290 len = strlen (namebuf) + 1;
2291 name = bfd_alloc (abfd, len);
2294 memcpy (name, namebuf, len);
2295 newsect = bfd_make_section (abfd, name);
2296 if (newsect == NULL)
2298 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2299 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2300 newsect->size = hdr->p_memsz - hdr->p_filesz;
2301 if (hdr->p_type == PT_LOAD)
2303 newsect->flags |= SEC_ALLOC;
2304 if (hdr->p_flags & PF_X)
2305 newsect->flags |= SEC_CODE;
2307 if (!(hdr->p_flags & PF_W))
2308 newsect->flags |= SEC_READONLY;
2315 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2317 const struct elf_backend_data *bed;
2319 switch (hdr->p_type)
2322 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2325 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2328 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2331 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2334 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2336 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2341 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2344 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2346 case PT_GNU_EH_FRAME:
2347 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2351 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2354 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2357 /* Check for any processor-specific program segment types.
2358 If no handler for them, default to making "segment" sections. */
2359 bed = get_elf_backend_data (abfd);
2360 if (bed->elf_backend_section_from_phdr)
2361 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2363 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2367 /* Initialize REL_HDR, the section-header for new section, containing
2368 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2369 relocations; otherwise, we use REL relocations. */
2372 _bfd_elf_init_reloc_shdr (bfd *abfd,
2373 Elf_Internal_Shdr *rel_hdr,
2375 bfd_boolean use_rela_p)
2378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2379 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2381 name = bfd_alloc (abfd, amt);
2384 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2386 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2388 if (rel_hdr->sh_name == (unsigned int) -1)
2390 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2391 rel_hdr->sh_entsize = (use_rela_p
2392 ? bed->s->sizeof_rela
2393 : bed->s->sizeof_rel);
2394 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2395 rel_hdr->sh_flags = 0;
2396 rel_hdr->sh_addr = 0;
2397 rel_hdr->sh_size = 0;
2398 rel_hdr->sh_offset = 0;
2403 /* Set up an ELF internal section header for a section. */
2406 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2409 bfd_boolean *failedptr = failedptrarg;
2410 Elf_Internal_Shdr *this_hdr;
2414 /* We already failed; just get out of the bfd_map_over_sections
2419 this_hdr = &elf_section_data (asect)->this_hdr;
2421 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2422 asect->name, FALSE);
2423 if (this_hdr->sh_name == (unsigned int) -1)
2429 this_hdr->sh_flags = 0;
2431 if ((asect->flags & SEC_ALLOC) != 0
2432 || asect->user_set_vma)
2433 this_hdr->sh_addr = asect->vma;
2435 this_hdr->sh_addr = 0;
2437 this_hdr->sh_offset = 0;
2438 this_hdr->sh_size = asect->size;
2439 this_hdr->sh_link = 0;
2440 this_hdr->sh_addralign = 1 << asect->alignment_power;
2441 /* The sh_entsize and sh_info fields may have been set already by
2442 copy_private_section_data. */
2444 this_hdr->bfd_section = asect;
2445 this_hdr->contents = NULL;
2447 /* If the section type is unspecified, we set it based on
2449 if (this_hdr->sh_type == SHT_NULL)
2451 if ((asect->flags & SEC_GROUP) != 0)
2453 /* We also need to mark SHF_GROUP here for relocatable
2455 struct bfd_link_order *l;
2458 for (l = asect->link_order_head; l != NULL; l = l->next)
2459 if (l->type == bfd_indirect_link_order
2460 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2463 /* The name is not important. Anything will do. */
2464 elf_group_name (elt->output_section) = "G";
2465 elf_section_flags (elt->output_section) |= SHF_GROUP;
2467 elt = elf_next_in_group (elt);
2468 /* During a relocatable link, the lists are
2471 while (elt != elf_next_in_group (l->u.indirect.section));
2473 this_hdr->sh_type = SHT_GROUP;
2475 else if ((asect->flags & SEC_ALLOC) != 0
2476 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2477 || (asect->flags & SEC_NEVER_LOAD) != 0))
2478 this_hdr->sh_type = SHT_NOBITS;
2480 this_hdr->sh_type = SHT_PROGBITS;
2483 switch (this_hdr->sh_type)
2489 case SHT_INIT_ARRAY:
2490 case SHT_FINI_ARRAY:
2491 case SHT_PREINIT_ARRAY:
2498 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2502 this_hdr->sh_entsize = bed->s->sizeof_sym;
2506 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2510 if (get_elf_backend_data (abfd)->may_use_rela_p)
2511 this_hdr->sh_entsize = bed->s->sizeof_rela;
2515 if (get_elf_backend_data (abfd)->may_use_rel_p)
2516 this_hdr->sh_entsize = bed->s->sizeof_rel;
2519 case SHT_GNU_versym:
2520 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2523 case SHT_GNU_verdef:
2524 this_hdr->sh_entsize = 0;
2525 /* objcopy or strip will copy over sh_info, but may not set
2526 cverdefs. The linker will set cverdefs, but sh_info will be
2528 if (this_hdr->sh_info == 0)
2529 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2531 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2532 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2535 case SHT_GNU_verneed:
2536 this_hdr->sh_entsize = 0;
2537 /* objcopy or strip will copy over sh_info, but may not set
2538 cverrefs. The linker will set cverrefs, but sh_info will be
2540 if (this_hdr->sh_info == 0)
2541 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2543 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2544 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2548 this_hdr->sh_entsize = 4;
2552 if ((asect->flags & SEC_ALLOC) != 0)
2553 this_hdr->sh_flags |= SHF_ALLOC;
2554 if ((asect->flags & SEC_READONLY) == 0)
2555 this_hdr->sh_flags |= SHF_WRITE;
2556 if ((asect->flags & SEC_CODE) != 0)
2557 this_hdr->sh_flags |= SHF_EXECINSTR;
2558 if ((asect->flags & SEC_MERGE) != 0)
2560 this_hdr->sh_flags |= SHF_MERGE;
2561 this_hdr->sh_entsize = asect->entsize;
2562 if ((asect->flags & SEC_STRINGS) != 0)
2563 this_hdr->sh_flags |= SHF_STRINGS;
2565 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2566 this_hdr->sh_flags |= SHF_GROUP;
2567 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2569 this_hdr->sh_flags |= SHF_TLS;
2570 if (asect->size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2572 struct bfd_link_order *o;
2574 this_hdr->sh_size = 0;
2575 for (o = asect->link_order_head; o != NULL; o = o->next)
2576 if (this_hdr->sh_size < o->offset + o->size)
2577 this_hdr->sh_size = o->offset + o->size;
2578 if (this_hdr->sh_size)
2579 this_hdr->sh_type = SHT_NOBITS;
2583 /* Check for processor-specific section types. */
2584 if (bed->elf_backend_fake_sections
2585 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2588 /* If the section has relocs, set up a section header for the
2589 SHT_REL[A] section. If two relocation sections are required for
2590 this section, it is up to the processor-specific back-end to
2591 create the other. */
2592 if ((asect->flags & SEC_RELOC) != 0
2593 && !_bfd_elf_init_reloc_shdr (abfd,
2594 &elf_section_data (asect)->rel_hdr,
2600 /* Fill in the contents of a SHT_GROUP section. */
2603 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2605 bfd_boolean *failedptr = failedptrarg;
2606 unsigned long symindx;
2607 asection *elt, *first;
2609 struct bfd_link_order *l;
2612 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2617 if (elf_group_id (sec) != NULL)
2618 symindx = elf_group_id (sec)->udata.i;
2622 /* If called from the assembler, swap_out_syms will have set up
2623 elf_section_syms; If called for "ld -r", use target_index. */
2624 if (elf_section_syms (abfd) != NULL)
2625 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2627 symindx = sec->target_index;
2629 elf_section_data (sec)->this_hdr.sh_info = symindx;
2631 /* The contents won't be allocated for "ld -r" or objcopy. */
2633 if (sec->contents == NULL)
2636 sec->contents = bfd_alloc (abfd, sec->size);
2638 /* Arrange for the section to be written out. */
2639 elf_section_data (sec)->this_hdr.contents = sec->contents;
2640 if (sec->contents == NULL)
2647 loc = sec->contents + sec->size;
2649 /* Get the pointer to the first section in the group that gas
2650 squirreled away here. objcopy arranges for this to be set to the
2651 start of the input section group. */
2652 first = elt = elf_next_in_group (sec);
2654 /* First element is a flag word. Rest of section is elf section
2655 indices for all the sections of the group. Write them backwards
2656 just to keep the group in the same order as given in .section
2657 directives, not that it matters. */
2666 s = s->output_section;
2669 idx = elf_section_data (s)->this_idx;
2670 H_PUT_32 (abfd, idx, loc);
2671 elt = elf_next_in_group (elt);
2676 /* If this is a relocatable link, then the above did nothing because
2677 SEC is the output section. Look through the input sections
2679 for (l = sec->link_order_head; l != NULL; l = l->next)
2680 if (l->type == bfd_indirect_link_order
2681 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2686 elf_section_data (elt->output_section)->this_idx, loc);
2687 elt = elf_next_in_group (elt);
2688 /* During a relocatable link, the lists are circular. */
2690 while (elt != elf_next_in_group (l->u.indirect.section));
2692 if ((loc -= 4) != sec->contents)
2695 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2698 /* Assign all ELF section numbers. The dummy first section is handled here
2699 too. The link/info pointers for the standard section types are filled
2700 in here too, while we're at it. */
2703 assign_section_numbers (bfd *abfd)
2705 struct elf_obj_tdata *t = elf_tdata (abfd);
2707 unsigned int section_number, secn;
2708 Elf_Internal_Shdr **i_shdrp;
2710 struct bfd_elf_section_data *d;
2714 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2716 /* Put SHT_GROUP sections first. */
2717 for (sec = abfd->sections; sec; sec = sec->next)
2719 d = elf_section_data (sec);
2721 if (d->this_hdr.sh_type == SHT_GROUP)
2723 if (section_number == SHN_LORESERVE)
2724 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2725 d->this_idx = section_number++;
2729 for (sec = abfd->sections; sec; sec = sec->next)
2731 d = elf_section_data (sec);
2733 if (d->this_hdr.sh_type != SHT_GROUP)
2735 if (section_number == SHN_LORESERVE)
2736 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2737 d->this_idx = section_number++;
2739 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2740 if ((sec->flags & SEC_RELOC) == 0)
2744 if (section_number == SHN_LORESERVE)
2745 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2746 d->rel_idx = section_number++;
2747 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2752 if (section_number == SHN_LORESERVE)
2753 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2754 d->rel_idx2 = section_number++;
2755 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2761 if (section_number == SHN_LORESERVE)
2762 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2763 t->shstrtab_section = section_number++;
2764 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2765 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2767 if (bfd_get_symcount (abfd) > 0)
2769 if (section_number == SHN_LORESERVE)
2770 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2771 t->symtab_section = section_number++;
2772 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2773 if (section_number > SHN_LORESERVE - 2)
2775 if (section_number == SHN_LORESERVE)
2776 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2777 t->symtab_shndx_section = section_number++;
2778 t->symtab_shndx_hdr.sh_name
2779 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2780 ".symtab_shndx", FALSE);
2781 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2784 if (section_number == SHN_LORESERVE)
2785 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2786 t->strtab_section = section_number++;
2787 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2790 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2791 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2793 elf_numsections (abfd) = section_number;
2794 elf_elfheader (abfd)->e_shnum = section_number;
2795 if (section_number > SHN_LORESERVE)
2796 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2798 /* Set up the list of section header pointers, in agreement with the
2800 amt = section_number * sizeof (Elf_Internal_Shdr *);
2801 i_shdrp = bfd_zalloc (abfd, amt);
2802 if (i_shdrp == NULL)
2805 amt = sizeof (Elf_Internal_Shdr);
2806 i_shdrp[0] = bfd_zalloc (abfd, amt);
2807 if (i_shdrp[0] == NULL)
2809 bfd_release (abfd, i_shdrp);
2813 elf_elfsections (abfd) = i_shdrp;
2815 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2816 if (bfd_get_symcount (abfd) > 0)
2818 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2819 if (elf_numsections (abfd) > SHN_LORESERVE)
2821 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2822 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2824 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2825 t->symtab_hdr.sh_link = t->strtab_section;
2828 for (sec = abfd->sections; sec; sec = sec->next)
2830 struct bfd_elf_section_data *d = elf_section_data (sec);
2834 i_shdrp[d->this_idx] = &d->this_hdr;
2835 if (d->rel_idx != 0)
2836 i_shdrp[d->rel_idx] = &d->rel_hdr;
2837 if (d->rel_idx2 != 0)
2838 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2840 /* Fill in the sh_link and sh_info fields while we're at it. */
2842 /* sh_link of a reloc section is the section index of the symbol
2843 table. sh_info is the section index of the section to which
2844 the relocation entries apply. */
2845 if (d->rel_idx != 0)
2847 d->rel_hdr.sh_link = t->symtab_section;
2848 d->rel_hdr.sh_info = d->this_idx;
2850 if (d->rel_idx2 != 0)
2852 d->rel_hdr2->sh_link = t->symtab_section;
2853 d->rel_hdr2->sh_info = d->this_idx;
2856 /* We need to set up sh_link for SHF_LINK_ORDER. */
2857 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2859 s = elf_linked_to_section (sec);
2861 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2864 struct bfd_link_order *p;
2866 /* Find out what the corresponding section in output
2868 for (p = sec->link_order_head; p != NULL; p = p->next)
2870 s = p->u.indirect.section;
2871 if (p->type == bfd_indirect_link_order
2872 && (bfd_get_flavour (s->owner)
2873 == bfd_target_elf_flavour))
2875 Elf_Internal_Shdr ** const elf_shdrp
2876 = elf_elfsections (s->owner);
2878 = _bfd_elf_section_from_bfd_section (s->owner, s);
2879 elfsec = elf_shdrp[elfsec]->sh_link;
2881 The Intel C compiler generates SHT_IA_64_UNWIND with
2882 SHF_LINK_ORDER. But it doesn't set theh sh_link or
2883 sh_info fields. Hence we could get the situation
2884 where elfsec is 0. */
2887 const struct elf_backend_data *bed
2888 = get_elf_backend_data (abfd);
2889 if (bed->link_order_error_handler)
2890 bed->link_order_error_handler
2891 (_("%B: warning: sh_link not set for section `%S'"),
2896 s = elf_shdrp[elfsec]->bfd_section->output_section;
2897 BFD_ASSERT (s != NULL);
2898 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2906 switch (d->this_hdr.sh_type)
2910 /* A reloc section which we are treating as a normal BFD
2911 section. sh_link is the section index of the symbol
2912 table. sh_info is the section index of the section to
2913 which the relocation entries apply. We assume that an
2914 allocated reloc section uses the dynamic symbol table.
2915 FIXME: How can we be sure? */
2916 s = bfd_get_section_by_name (abfd, ".dynsym");
2918 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2920 /* We look up the section the relocs apply to by name. */
2922 if (d->this_hdr.sh_type == SHT_REL)
2926 s = bfd_get_section_by_name (abfd, name);
2928 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2932 /* We assume that a section named .stab*str is a stabs
2933 string section. We look for a section with the same name
2934 but without the trailing ``str'', and set its sh_link
2935 field to point to this section. */
2936 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2937 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2942 len = strlen (sec->name);
2943 alc = bfd_malloc (len - 2);
2946 memcpy (alc, sec->name, len - 3);
2947 alc[len - 3] = '\0';
2948 s = bfd_get_section_by_name (abfd, alc);
2952 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2954 /* This is a .stab section. */
2955 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2956 elf_section_data (s)->this_hdr.sh_entsize
2957 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2964 case SHT_GNU_verneed:
2965 case SHT_GNU_verdef:
2966 /* sh_link is the section header index of the string table
2967 used for the dynamic entries, or the symbol table, or the
2969 s = bfd_get_section_by_name (abfd, ".dynstr");
2971 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2974 case SHT_GNU_LIBLIST:
2975 /* sh_link is the section header index of the prelink library
2977 used for the dynamic entries, or the symbol table, or the
2979 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
2980 ? ".dynstr" : ".gnu.libstr");
2982 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2986 case SHT_GNU_versym:
2987 /* sh_link is the section header index of the symbol table
2988 this hash table or version table is for. */
2989 s = bfd_get_section_by_name (abfd, ".dynsym");
2991 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2995 d->this_hdr.sh_link = t->symtab_section;
2999 for (secn = 1; secn < section_number; ++secn)
3000 if (i_shdrp[secn] == NULL)
3001 i_shdrp[secn] = i_shdrp[0];
3003 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3004 i_shdrp[secn]->sh_name);
3008 /* Map symbol from it's internal number to the external number, moving
3009 all local symbols to be at the head of the list. */
3012 sym_is_global (bfd *abfd, asymbol *sym)
3014 /* If the backend has a special mapping, use it. */
3015 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3016 if (bed->elf_backend_sym_is_global)
3017 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3019 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3020 || bfd_is_und_section (bfd_get_section (sym))
3021 || bfd_is_com_section (bfd_get_section (sym)));
3025 elf_map_symbols (bfd *abfd)
3027 unsigned int symcount = bfd_get_symcount (abfd);
3028 asymbol **syms = bfd_get_outsymbols (abfd);
3029 asymbol **sect_syms;
3030 unsigned int num_locals = 0;
3031 unsigned int num_globals = 0;
3032 unsigned int num_locals2 = 0;
3033 unsigned int num_globals2 = 0;
3041 fprintf (stderr, "elf_map_symbols\n");
3045 for (asect = abfd->sections; asect; asect = asect->next)
3047 if (max_index < asect->index)
3048 max_index = asect->index;
3052 amt = max_index * sizeof (asymbol *);
3053 sect_syms = bfd_zalloc (abfd, amt);
3054 if (sect_syms == NULL)
3056 elf_section_syms (abfd) = sect_syms;
3057 elf_num_section_syms (abfd) = max_index;
3059 /* Init sect_syms entries for any section symbols we have already
3060 decided to output. */
3061 for (idx = 0; idx < symcount; idx++)
3063 asymbol *sym = syms[idx];
3065 if ((sym->flags & BSF_SECTION_SYM) != 0
3072 if (sec->owner != NULL)
3074 if (sec->owner != abfd)
3076 if (sec->output_offset != 0)
3079 sec = sec->output_section;
3081 /* Empty sections in the input files may have had a
3082 section symbol created for them. (See the comment
3083 near the end of _bfd_generic_link_output_symbols in
3084 linker.c). If the linker script discards such
3085 sections then we will reach this point. Since we know
3086 that we cannot avoid this case, we detect it and skip
3087 the abort and the assignment to the sect_syms array.
3088 To reproduce this particular case try running the
3089 linker testsuite test ld-scripts/weak.exp for an ELF
3090 port that uses the generic linker. */
3091 if (sec->owner == NULL)
3094 BFD_ASSERT (sec->owner == abfd);
3096 sect_syms[sec->index] = syms[idx];
3101 /* Classify all of the symbols. */
3102 for (idx = 0; idx < symcount; idx++)
3104 if (!sym_is_global (abfd, syms[idx]))
3110 /* We will be adding a section symbol for each BFD section. Most normal
3111 sections will already have a section symbol in outsymbols, but
3112 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3113 at least in that case. */
3114 for (asect = abfd->sections; asect; asect = asect->next)
3116 if (sect_syms[asect->index] == NULL)
3118 if (!sym_is_global (abfd, asect->symbol))
3125 /* Now sort the symbols so the local symbols are first. */
3126 amt = (num_locals + num_globals) * sizeof (asymbol *);
3127 new_syms = bfd_alloc (abfd, amt);
3129 if (new_syms == NULL)
3132 for (idx = 0; idx < symcount; idx++)
3134 asymbol *sym = syms[idx];
3137 if (!sym_is_global (abfd, sym))
3140 i = num_locals + num_globals2++;
3142 sym->udata.i = i + 1;
3144 for (asect = abfd->sections; asect; asect = asect->next)
3146 if (sect_syms[asect->index] == NULL)
3148 asymbol *sym = asect->symbol;
3151 sect_syms[asect->index] = sym;
3152 if (!sym_is_global (abfd, sym))
3155 i = num_locals + num_globals2++;
3157 sym->udata.i = i + 1;
3161 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3163 elf_num_locals (abfd) = num_locals;
3164 elf_num_globals (abfd) = num_globals;
3168 /* Align to the maximum file alignment that could be required for any
3169 ELF data structure. */
3171 static inline file_ptr
3172 align_file_position (file_ptr off, int align)
3174 return (off + align - 1) & ~(align - 1);
3177 /* Assign a file position to a section, optionally aligning to the
3178 required section alignment. */
3181 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3189 al = i_shdrp->sh_addralign;
3191 offset = BFD_ALIGN (offset, al);
3193 i_shdrp->sh_offset = offset;
3194 if (i_shdrp->bfd_section != NULL)
3195 i_shdrp->bfd_section->filepos = offset;
3196 if (i_shdrp->sh_type != SHT_NOBITS)
3197 offset += i_shdrp->sh_size;
3201 /* Compute the file positions we are going to put the sections at, and
3202 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3203 is not NULL, this is being called by the ELF backend linker. */
3206 _bfd_elf_compute_section_file_positions (bfd *abfd,
3207 struct bfd_link_info *link_info)
3209 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3211 struct bfd_strtab_hash *strtab;
3212 Elf_Internal_Shdr *shstrtab_hdr;
3214 if (abfd->output_has_begun)
3217 /* Do any elf backend specific processing first. */
3218 if (bed->elf_backend_begin_write_processing)
3219 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3221 if (! prep_headers (abfd))
3224 /* Post process the headers if necessary. */
3225 if (bed->elf_backend_post_process_headers)
3226 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3229 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3233 if (!assign_section_numbers (abfd))
3236 /* The backend linker builds symbol table information itself. */
3237 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3239 /* Non-zero if doing a relocatable link. */
3240 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3242 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3246 if (link_info == NULL)
3248 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3253 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3254 /* sh_name was set in prep_headers. */
3255 shstrtab_hdr->sh_type = SHT_STRTAB;
3256 shstrtab_hdr->sh_flags = 0;
3257 shstrtab_hdr->sh_addr = 0;
3258 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3259 shstrtab_hdr->sh_entsize = 0;
3260 shstrtab_hdr->sh_link = 0;
3261 shstrtab_hdr->sh_info = 0;
3262 /* sh_offset is set in assign_file_positions_except_relocs. */
3263 shstrtab_hdr->sh_addralign = 1;
3265 if (!assign_file_positions_except_relocs (abfd, link_info))
3268 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3271 Elf_Internal_Shdr *hdr;
3273 off = elf_tdata (abfd)->next_file_pos;
3275 hdr = &elf_tdata (abfd)->symtab_hdr;
3276 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3278 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3279 if (hdr->sh_size != 0)
3280 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3282 hdr = &elf_tdata (abfd)->strtab_hdr;
3283 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3285 elf_tdata (abfd)->next_file_pos = off;
3287 /* Now that we know where the .strtab section goes, write it
3289 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3290 || ! _bfd_stringtab_emit (abfd, strtab))
3292 _bfd_stringtab_free (strtab);
3295 abfd->output_has_begun = TRUE;
3300 /* Create a mapping from a set of sections to a program segment. */
3302 static struct elf_segment_map *
3303 make_mapping (bfd *abfd,
3304 asection **sections,
3309 struct elf_segment_map *m;
3314 amt = sizeof (struct elf_segment_map);
3315 amt += (to - from - 1) * sizeof (asection *);
3316 m = bfd_zalloc (abfd, amt);
3320 m->p_type = PT_LOAD;
3321 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3322 m->sections[i - from] = *hdrpp;
3323 m->count = to - from;
3325 if (from == 0 && phdr)
3327 /* Include the headers in the first PT_LOAD segment. */
3328 m->includes_filehdr = 1;
3329 m->includes_phdrs = 1;
3335 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3338 struct elf_segment_map *
3339 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3341 struct elf_segment_map *m;
3343 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3347 m->p_type = PT_DYNAMIC;
3349 m->sections[0] = dynsec;
3354 /* Set up a mapping from BFD sections to program segments. */
3357 map_sections_to_segments (bfd *abfd)
3359 asection **sections = NULL;
3363 struct elf_segment_map *mfirst;
3364 struct elf_segment_map **pm;
3365 struct elf_segment_map *m;
3368 unsigned int phdr_index;
3369 bfd_vma maxpagesize;
3371 bfd_boolean phdr_in_segment = TRUE;
3372 bfd_boolean writable;
3374 asection *first_tls = NULL;
3375 asection *dynsec, *eh_frame_hdr;
3378 if (elf_tdata (abfd)->segment_map != NULL)
3381 if (bfd_count_sections (abfd) == 0)
3384 /* Select the allocated sections, and sort them. */
3386 amt = bfd_count_sections (abfd) * sizeof (asection *);
3387 sections = bfd_malloc (amt);
3388 if (sections == NULL)
3392 for (s = abfd->sections; s != NULL; s = s->next)
3394 if ((s->flags & SEC_ALLOC) != 0)
3400 BFD_ASSERT (i <= bfd_count_sections (abfd));
3403 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3405 /* Build the mapping. */
3410 /* If we have a .interp section, then create a PT_PHDR segment for
3411 the program headers and a PT_INTERP segment for the .interp
3413 s = bfd_get_section_by_name (abfd, ".interp");
3414 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3416 amt = sizeof (struct elf_segment_map);
3417 m = bfd_zalloc (abfd, amt);
3421 m->p_type = PT_PHDR;
3422 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3423 m->p_flags = PF_R | PF_X;
3424 m->p_flags_valid = 1;
3425 m->includes_phdrs = 1;
3430 amt = sizeof (struct elf_segment_map);
3431 m = bfd_zalloc (abfd, amt);
3435 m->p_type = PT_INTERP;
3443 /* Look through the sections. We put sections in the same program
3444 segment when the start of the second section can be placed within
3445 a few bytes of the end of the first section. */
3449 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3451 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3453 && (dynsec->flags & SEC_LOAD) == 0)
3456 /* Deal with -Ttext or something similar such that the first section
3457 is not adjacent to the program headers. This is an
3458 approximation, since at this point we don't know exactly how many
3459 program headers we will need. */
3462 bfd_size_type phdr_size;
3464 phdr_size = elf_tdata (abfd)->program_header_size;
3466 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3467 if ((abfd->flags & D_PAGED) == 0
3468 || sections[0]->lma < phdr_size
3469 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3470 phdr_in_segment = FALSE;
3473 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3476 bfd_boolean new_segment;
3480 /* See if this section and the last one will fit in the same
3483 if (last_hdr == NULL)
3485 /* If we don't have a segment yet, then we don't need a new
3486 one (we build the last one after this loop). */
3487 new_segment = FALSE;
3489 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3491 /* If this section has a different relation between the
3492 virtual address and the load address, then we need a new
3496 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3497 < BFD_ALIGN (hdr->lma, maxpagesize))
3499 /* If putting this section in this segment would force us to
3500 skip a page in the segment, then we need a new segment. */
3503 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3504 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3506 /* We don't want to put a loadable section after a
3507 nonloadable section in the same segment.
3508 Consider .tbss sections as loadable for this purpose. */
3511 else if ((abfd->flags & D_PAGED) == 0)
3513 /* If the file is not demand paged, which means that we
3514 don't require the sections to be correctly aligned in the
3515 file, then there is no other reason for a new segment. */
3516 new_segment = FALSE;
3519 && (hdr->flags & SEC_READONLY) == 0
3520 && (((last_hdr->lma + last_size - 1)
3521 & ~(maxpagesize - 1))
3522 != (hdr->lma & ~(maxpagesize - 1))))
3524 /* We don't want to put a writable section in a read only
3525 segment, unless they are on the same page in memory
3526 anyhow. We already know that the last section does not
3527 bring us past the current section on the page, so the
3528 only case in which the new section is not on the same
3529 page as the previous section is when the previous section
3530 ends precisely on a page boundary. */
3535 /* Otherwise, we can use the same segment. */
3536 new_segment = FALSE;
3541 if ((hdr->flags & SEC_READONLY) == 0)
3544 /* .tbss sections effectively have zero size. */
3545 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3546 last_size = hdr->size;
3552 /* We need a new program segment. We must create a new program
3553 header holding all the sections from phdr_index until hdr. */
3555 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3562 if ((hdr->flags & SEC_READONLY) == 0)
3568 /* .tbss sections effectively have zero size. */
3569 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3570 last_size = hdr->size;
3574 phdr_in_segment = FALSE;
3577 /* Create a final PT_LOAD program segment. */
3578 if (last_hdr != NULL)
3580 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3588 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3591 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3598 /* For each loadable .note section, add a PT_NOTE segment. We don't
3599 use bfd_get_section_by_name, because if we link together
3600 nonloadable .note sections and loadable .note sections, we will
3601 generate two .note sections in the output file. FIXME: Using
3602 names for section types is bogus anyhow. */
3603 for (s = abfd->sections; s != NULL; s = s->next)
3605 if ((s->flags & SEC_LOAD) != 0
3606 && strncmp (s->name, ".note", 5) == 0)
3608 amt = sizeof (struct elf_segment_map);
3609 m = bfd_zalloc (abfd, amt);
3613 m->p_type = PT_NOTE;
3620 if (s->flags & SEC_THREAD_LOCAL)
3628 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3633 amt = sizeof (struct elf_segment_map);
3634 amt += (tls_count - 1) * sizeof (asection *);
3635 m = bfd_zalloc (abfd, amt);
3640 m->count = tls_count;
3641 /* Mandated PF_R. */
3643 m->p_flags_valid = 1;
3644 for (i = 0; i < tls_count; ++i)
3646 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3647 m->sections[i] = first_tls;
3648 first_tls = first_tls->next;
3655 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3657 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3658 if (eh_frame_hdr != NULL
3659 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3661 amt = sizeof (struct elf_segment_map);
3662 m = bfd_zalloc (abfd, amt);
3666 m->p_type = PT_GNU_EH_FRAME;
3668 m->sections[0] = eh_frame_hdr->output_section;
3674 if (elf_tdata (abfd)->stack_flags)
3676 amt = sizeof (struct elf_segment_map);
3677 m = bfd_zalloc (abfd, amt);
3681 m->p_type = PT_GNU_STACK;
3682 m->p_flags = elf_tdata (abfd)->stack_flags;
3683 m->p_flags_valid = 1;
3689 if (elf_tdata (abfd)->relro)
3691 amt = sizeof (struct elf_segment_map);
3692 m = bfd_zalloc (abfd, amt);
3696 m->p_type = PT_GNU_RELRO;
3698 m->p_flags_valid = 1;
3707 elf_tdata (abfd)->segment_map = mfirst;
3711 if (sections != NULL)
3716 /* Sort sections by address. */
3719 elf_sort_sections (const void *arg1, const void *arg2)
3721 const asection *sec1 = *(const asection **) arg1;
3722 const asection *sec2 = *(const asection **) arg2;
3723 bfd_size_type size1, size2;
3725 /* Sort by LMA first, since this is the address used to
3726 place the section into a segment. */
3727 if (sec1->lma < sec2->lma)
3729 else if (sec1->lma > sec2->lma)
3732 /* Then sort by VMA. Normally the LMA and the VMA will be
3733 the same, and this will do nothing. */
3734 if (sec1->vma < sec2->vma)
3736 else if (sec1->vma > sec2->vma)
3739 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3741 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3747 /* If the indicies are the same, do not return 0
3748 here, but continue to try the next comparison. */
3749 if (sec1->target_index - sec2->target_index != 0)
3750 return sec1->target_index - sec2->target_index;
3755 else if (TOEND (sec2))
3760 /* Sort by size, to put zero sized sections
3761 before others at the same address. */
3763 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
3764 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
3771 return sec1->target_index - sec2->target_index;
3774 /* Ian Lance Taylor writes:
3776 We shouldn't be using % with a negative signed number. That's just
3777 not good. We have to make sure either that the number is not
3778 negative, or that the number has an unsigned type. When the types
3779 are all the same size they wind up as unsigned. When file_ptr is a
3780 larger signed type, the arithmetic winds up as signed long long,
3783 What we're trying to say here is something like ``increase OFF by
3784 the least amount that will cause it to be equal to the VMA modulo
3786 /* In other words, something like:
3788 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3789 off_offset = off % bed->maxpagesize;
3790 if (vma_offset < off_offset)
3791 adjustment = vma_offset + bed->maxpagesize - off_offset;
3793 adjustment = vma_offset - off_offset;
3795 which can can be collapsed into the expression below. */
3798 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3800 return ((vma - off) % maxpagesize);
3803 /* Assign file positions to the sections based on the mapping from
3804 sections to segments. This function also sets up some fields in
3805 the file header, and writes out the program headers. */
3808 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
3810 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3812 struct elf_segment_map *m;
3814 Elf_Internal_Phdr *phdrs;
3816 bfd_vma filehdr_vaddr, filehdr_paddr;
3817 bfd_vma phdrs_vaddr, phdrs_paddr;
3818 Elf_Internal_Phdr *p;
3821 if (elf_tdata (abfd)->segment_map == NULL)
3823 if (! map_sections_to_segments (abfd))
3828 /* The placement algorithm assumes that non allocated sections are
3829 not in PT_LOAD segments. We ensure this here by removing such
3830 sections from the segment map. */
3831 for (m = elf_tdata (abfd)->segment_map;
3835 unsigned int new_count;
3838 if (m->p_type != PT_LOAD)
3842 for (i = 0; i < m->count; i ++)
3844 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3847 m->sections[new_count] = m->sections[i];
3853 if (new_count != m->count)
3854 m->count = new_count;
3858 if (bed->elf_backend_modify_segment_map)
3860 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3865 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3868 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3869 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3870 elf_elfheader (abfd)->e_phnum = count;
3874 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
3878 /* If we already counted the number of program segments, make sure
3879 that we allocated enough space. This happens when SIZEOF_HEADERS
3880 is used in a linker script. */
3881 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3882 if (alloc != 0 && count > alloc)
3884 ((*_bfd_error_handler)
3885 (_("%B: Not enough room for program headers (allocated %u, need %u)"),
3886 abfd, alloc, count));
3887 bfd_set_error (bfd_error_bad_value);
3894 amt = alloc * sizeof (Elf_Internal_Phdr);
3895 phdrs = bfd_alloc (abfd, amt);
3899 off = bed->s->sizeof_ehdr;
3900 off += alloc * bed->s->sizeof_phdr;
3907 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3914 /* If elf_segment_map is not from map_sections_to_segments, the
3915 sections may not be correctly ordered. NOTE: sorting should
3916 not be done to the PT_NOTE section of a corefile, which may
3917 contain several pseudo-sections artificially created by bfd.
3918 Sorting these pseudo-sections breaks things badly. */
3920 && !(elf_elfheader (abfd)->e_type == ET_CORE
3921 && m->p_type == PT_NOTE))
3922 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3925 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
3926 number of sections with contents contributing to both p_filesz
3927 and p_memsz, followed by a number of sections with no contents
3928 that just contribute to p_memsz. In this loop, OFF tracks next
3929 available file offset for PT_LOAD and PT_NOTE segments. VOFF is
3930 an adjustment we use for segments that have no file contents
3931 but need zero filled memory allocation. */
3933 p->p_type = m->p_type;
3934 p->p_flags = m->p_flags;
3936 if (p->p_type == PT_LOAD
3939 bfd_size_type align;
3942 if ((abfd->flags & D_PAGED) != 0)
3943 align = bed->maxpagesize;
3946 unsigned int align_power = 0;
3947 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3949 unsigned int secalign;
3951 secalign = bfd_get_section_alignment (abfd, *secpp);
3952 if (secalign > align_power)
3953 align_power = secalign;
3955 align = (bfd_size_type) 1 << align_power;
3958 adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
3961 && !m->includes_filehdr
3962 && !m->includes_phdrs
3963 && (ufile_ptr) off >= align)
3965 /* If the first section isn't loadable, the same holds for
3966 any other sections. Since the segment won't need file
3967 space, we can make p_offset overlap some prior segment.
3968 However, .tbss is special. If a segment starts with
3969 .tbss, we need to look at the next section to decide
3970 whether the segment has any loadable sections. */
3972 while ((m->sections[i]->flags & SEC_LOAD) == 0)
3974 if ((m->sections[i]->flags & SEC_THREAD_LOCAL) == 0
3978 voff = adjust - align;
3984 /* Make sure the .dynamic section is the first section in the
3985 PT_DYNAMIC segment. */
3986 else if (p->p_type == PT_DYNAMIC
3988 && strcmp (m->sections[0]->name, ".dynamic") != 0)
3991 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
3993 bfd_set_error (bfd_error_bad_value);
4000 p->p_vaddr = m->sections[0]->vma;
4002 if (m->p_paddr_valid)
4003 p->p_paddr = m->p_paddr;
4004 else if (m->count == 0)
4007 p->p_paddr = m->sections[0]->lma;
4009 if (p->p_type == PT_LOAD
4010 && (abfd->flags & D_PAGED) != 0)
4011 p->p_align = bed->maxpagesize;
4012 else if (m->count == 0)
4013 p->p_align = 1 << bed->s->log_file_align;
4021 if (m->includes_filehdr)
4023 if (! m->p_flags_valid)
4026 p->p_filesz = bed->s->sizeof_ehdr;
4027 p->p_memsz = bed->s->sizeof_ehdr;
4030 BFD_ASSERT (p->p_type == PT_LOAD);
4032 if (p->p_vaddr < (bfd_vma) off)
4034 (*_bfd_error_handler)
4035 (_("%B: Not enough room for program headers, try linking with -N"),
4037 bfd_set_error (bfd_error_bad_value);
4042 if (! m->p_paddr_valid)
4045 if (p->p_type == PT_LOAD)
4047 filehdr_vaddr = p->p_vaddr;
4048 filehdr_paddr = p->p_paddr;
4052 if (m->includes_phdrs)
4054 if (! m->p_flags_valid)
4057 if (m->includes_filehdr)
4059 if (p->p_type == PT_LOAD)
4061 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
4062 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
4067 p->p_offset = bed->s->sizeof_ehdr;
4071 BFD_ASSERT (p->p_type == PT_LOAD);
4072 p->p_vaddr -= off - p->p_offset;
4073 if (! m->p_paddr_valid)
4074 p->p_paddr -= off - p->p_offset;
4077 if (p->p_type == PT_LOAD)
4079 phdrs_vaddr = p->p_vaddr;
4080 phdrs_paddr = p->p_paddr;
4083 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4086 p->p_filesz += alloc * bed->s->sizeof_phdr;
4087 p->p_memsz += alloc * bed->s->sizeof_phdr;
4090 if (p->p_type == PT_LOAD
4091 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4093 if (! m->includes_filehdr && ! m->includes_phdrs)
4094 p->p_offset = off + voff;
4099 adjust = off - (p->p_offset + p->p_filesz);
4100 p->p_filesz += adjust;
4101 p->p_memsz += adjust;
4105 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4109 bfd_size_type align;
4113 align = 1 << bfd_get_section_alignment (abfd, sec);
4115 if (p->p_type == PT_LOAD
4116 || p->p_type == PT_TLS)
4118 bfd_signed_vma adjust;
4120 if ((flags & SEC_LOAD) != 0)
4122 adjust = sec->lma - (p->p_paddr + p->p_filesz);
4125 (*_bfd_error_handler)
4126 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4127 abfd, sec, (unsigned long) sec->lma);
4131 p->p_filesz += adjust;
4132 p->p_memsz += adjust;
4134 /* .tbss is special. It doesn't contribute to p_memsz of
4136 else if ((flags & SEC_THREAD_LOCAL) == 0
4137 || p->p_type == PT_TLS)
4139 /* The section VMA must equal the file position
4140 modulo the page size. */
4141 bfd_size_type page = align;
4142 if ((abfd->flags & D_PAGED) != 0)
4143 page = bed->maxpagesize;
4144 adjust = vma_page_aligned_bias (sec->vma,
4145 p->p_vaddr + p->p_memsz,
4147 p->p_memsz += adjust;
4151 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4153 /* The section at i == 0 is the one that actually contains
4159 p->p_filesz = sec->size;
4165 /* The rest are fake sections that shouldn't be written. */
4174 if (p->p_type == PT_LOAD)
4177 /* FIXME: The SEC_HAS_CONTENTS test here dates back to
4178 1997, and the exact reason for it isn't clear. One
4179 plausible explanation is that it is to work around
4180 a problem we have with linker scripts using data
4181 statements in NOLOAD sections. I don't think it
4182 makes a great deal of sense to have such a section
4183 assigned to a PT_LOAD segment, but apparently
4184 people do this. The data statement results in a
4185 bfd_data_link_order being built, and these need
4186 section contents to write into. Eventually, we get
4187 to _bfd_elf_write_object_contents which writes any
4188 section with contents to the output. Make room
4189 here for the write, so that following segments are
4191 if ((flags & SEC_LOAD) != 0
4192 || (flags & SEC_HAS_CONTENTS) != 0)
4196 if ((flags & SEC_LOAD) != 0)
4198 p->p_filesz += sec->size;
4199 p->p_memsz += sec->size;
4201 /* .tbss is special. It doesn't contribute to p_memsz of
4203 else if ((flags & SEC_THREAD_LOCAL) == 0
4204 || p->p_type == PT_TLS)
4205 p->p_memsz += sec->size;
4207 if (p->p_type == PT_TLS
4209 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4211 struct bfd_link_order *o;
4212 bfd_vma tbss_size = 0;
4214 for (o = sec->link_order_head; o != NULL; o = o->next)
4215 if (tbss_size < o->offset + o->size)
4216 tbss_size = o->offset + o->size;
4218 p->p_memsz += tbss_size;
4221 if (align > p->p_align
4222 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
4226 if (! m->p_flags_valid)
4229 if ((flags & SEC_CODE) != 0)
4231 if ((flags & SEC_READONLY) == 0)
4237 /* Now that we have set the section file positions, we can set up
4238 the file positions for the non PT_LOAD segments. */
4239 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4243 if (p->p_type != PT_LOAD && m->count > 0)
4245 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4246 /* If the section has not yet been assigned a file position,
4247 do so now. The ARM BPABI requires that .dynamic section
4248 not be marked SEC_ALLOC because it is not part of any
4249 PT_LOAD segment, so it will not be processed above. */
4250 if (p->p_type == PT_DYNAMIC && m->sections[0]->filepos == 0)
4253 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4256 while (i_shdrpp[i]->bfd_section != m->sections[0])
4258 off = (_bfd_elf_assign_file_position_for_section
4259 (i_shdrpp[i], off, TRUE));
4260 p->p_filesz = m->sections[0]->size;
4262 p->p_offset = m->sections[0]->filepos;
4266 if (m->includes_filehdr)
4268 p->p_vaddr = filehdr_vaddr;
4269 if (! m->p_paddr_valid)
4270 p->p_paddr = filehdr_paddr;
4272 else if (m->includes_phdrs)
4274 p->p_vaddr = phdrs_vaddr;
4275 if (! m->p_paddr_valid)
4276 p->p_paddr = phdrs_paddr;
4278 else if (p->p_type == PT_GNU_RELRO)
4280 Elf_Internal_Phdr *lp;
4282 for (lp = phdrs; lp < phdrs + count; ++lp)
4284 if (lp->p_type == PT_LOAD
4285 && lp->p_vaddr <= link_info->relro_end
4286 && lp->p_vaddr >= link_info->relro_start
4287 && lp->p_vaddr + lp->p_filesz
4288 >= link_info->relro_end)
4292 if (lp < phdrs + count
4293 && link_info->relro_end > lp->p_vaddr)
4295 p->p_vaddr = lp->p_vaddr;
4296 p->p_paddr = lp->p_paddr;
4297 p->p_offset = lp->p_offset;
4298 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4299 p->p_memsz = p->p_filesz;
4301 p->p_flags = (lp->p_flags & ~PF_W);
4305 memset (p, 0, sizeof *p);
4306 p->p_type = PT_NULL;
4312 /* Clear out any program headers we allocated but did not use. */
4313 for (; count < alloc; count++, p++)
4315 memset (p, 0, sizeof *p);
4316 p->p_type = PT_NULL;
4319 elf_tdata (abfd)->phdr = phdrs;
4321 elf_tdata (abfd)->next_file_pos = off;
4323 /* Write out the program headers. */
4324 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4325 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4331 /* Get the size of the program header.
4333 If this is called by the linker before any of the section VMA's are set, it
4334 can't calculate the correct value for a strange memory layout. This only
4335 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4336 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4337 data segment (exclusive of .interp and .dynamic).
4339 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4340 will be two segments. */
4342 static bfd_size_type
4343 get_program_header_size (bfd *abfd)
4347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4349 /* We can't return a different result each time we're called. */
4350 if (elf_tdata (abfd)->program_header_size != 0)
4351 return elf_tdata (abfd)->program_header_size;
4353 if (elf_tdata (abfd)->segment_map != NULL)
4355 struct elf_segment_map *m;
4358 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4360 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4361 return elf_tdata (abfd)->program_header_size;
4364 /* Assume we will need exactly two PT_LOAD segments: one for text
4365 and one for data. */
4368 s = bfd_get_section_by_name (abfd, ".interp");
4369 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4371 /* If we have a loadable interpreter section, we need a
4372 PT_INTERP segment. In this case, assume we also need a
4373 PT_PHDR segment, although that may not be true for all
4378 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4380 /* We need a PT_DYNAMIC segment. */
4384 if (elf_tdata (abfd)->eh_frame_hdr)
4386 /* We need a PT_GNU_EH_FRAME segment. */
4390 if (elf_tdata (abfd)->stack_flags)
4392 /* We need a PT_GNU_STACK segment. */
4396 if (elf_tdata (abfd)->relro)
4398 /* We need a PT_GNU_RELRO segment. */
4402 for (s = abfd->sections; s != NULL; s = s->next)
4404 if ((s->flags & SEC_LOAD) != 0
4405 && strncmp (s->name, ".note", 5) == 0)
4407 /* We need a PT_NOTE segment. */
4412 for (s = abfd->sections; s != NULL; s = s->next)
4414 if (s->flags & SEC_THREAD_LOCAL)
4416 /* We need a PT_TLS segment. */
4422 /* Let the backend count up any program headers it might need. */
4423 if (bed->elf_backend_additional_program_headers)
4427 a = (*bed->elf_backend_additional_program_headers) (abfd);
4433 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4434 return elf_tdata (abfd)->program_header_size;
4437 /* Work out the file positions of all the sections. This is called by
4438 _bfd_elf_compute_section_file_positions. All the section sizes and
4439 VMAs must be known before this is called.
4441 Reloc sections come in two flavours: Those processed specially as
4442 "side-channel" data attached to a section to which they apply, and
4443 those that bfd doesn't process as relocations. The latter sort are
4444 stored in a normal bfd section by bfd_section_from_shdr. We don't
4445 consider the former sort here, unless they form part of the loadable
4446 image. Reloc sections not assigned here will be handled later by
4447 assign_file_positions_for_relocs.
4449 We also don't set the positions of the .symtab and .strtab here. */
4452 assign_file_positions_except_relocs (bfd *abfd,
4453 struct bfd_link_info *link_info)
4455 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4456 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4457 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4458 unsigned int num_sec = elf_numsections (abfd);
4460 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4462 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4463 && bfd_get_format (abfd) != bfd_core)
4465 Elf_Internal_Shdr **hdrpp;
4468 /* Start after the ELF header. */
4469 off = i_ehdrp->e_ehsize;
4471 /* We are not creating an executable, which means that we are
4472 not creating a program header, and that the actual order of
4473 the sections in the file is unimportant. */
4474 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4476 Elf_Internal_Shdr *hdr;
4479 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4480 && hdr->bfd_section == NULL)
4481 || i == tdata->symtab_section
4482 || i == tdata->symtab_shndx_section
4483 || i == tdata->strtab_section)
4485 hdr->sh_offset = -1;
4488 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4490 if (i == SHN_LORESERVE - 1)
4492 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4493 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4500 Elf_Internal_Shdr **hdrpp;
4502 /* Assign file positions for the loaded sections based on the
4503 assignment of sections to segments. */
4504 if (! assign_file_positions_for_segments (abfd, link_info))
4507 /* Assign file positions for the other sections. */
4509 off = elf_tdata (abfd)->next_file_pos;
4510 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4512 Elf_Internal_Shdr *hdr;
4515 if (hdr->bfd_section != NULL
4516 && hdr->bfd_section->filepos != 0)
4517 hdr->sh_offset = hdr->bfd_section->filepos;
4518 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4520 ((*_bfd_error_handler)
4521 (_("%B: warning: allocated section `%s' not in segment"),
4523 (hdr->bfd_section == NULL
4525 : hdr->bfd_section->name)));
4526 if ((abfd->flags & D_PAGED) != 0)
4527 off += vma_page_aligned_bias (hdr->sh_addr, off,
4530 off += vma_page_aligned_bias (hdr->sh_addr, off,
4532 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4535 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4536 && hdr->bfd_section == NULL)
4537 || hdr == i_shdrpp[tdata->symtab_section]
4538 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4539 || hdr == i_shdrpp[tdata->strtab_section])
4540 hdr->sh_offset = -1;
4542 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4544 if (i == SHN_LORESERVE - 1)
4546 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4547 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4552 /* Place the section headers. */
4553 off = align_file_position (off, 1 << bed->s->log_file_align);
4554 i_ehdrp->e_shoff = off;
4555 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4557 elf_tdata (abfd)->next_file_pos = off;
4563 prep_headers (bfd *abfd)
4565 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4566 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4567 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4568 struct elf_strtab_hash *shstrtab;
4569 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4571 i_ehdrp = elf_elfheader (abfd);
4572 i_shdrp = elf_elfsections (abfd);
4574 shstrtab = _bfd_elf_strtab_init ();
4575 if (shstrtab == NULL)
4578 elf_shstrtab (abfd) = shstrtab;
4580 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4581 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4582 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4583 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4585 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4586 i_ehdrp->e_ident[EI_DATA] =
4587 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4588 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4590 if ((abfd->flags & DYNAMIC) != 0)
4591 i_ehdrp->e_type = ET_DYN;
4592 else if ((abfd->flags & EXEC_P) != 0)
4593 i_ehdrp->e_type = ET_EXEC;
4594 else if (bfd_get_format (abfd) == bfd_core)
4595 i_ehdrp->e_type = ET_CORE;
4597 i_ehdrp->e_type = ET_REL;
4599 switch (bfd_get_arch (abfd))
4601 case bfd_arch_unknown:
4602 i_ehdrp->e_machine = EM_NONE;
4605 /* There used to be a long list of cases here, each one setting
4606 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4607 in the corresponding bfd definition. To avoid duplication,
4608 the switch was removed. Machines that need special handling
4609 can generally do it in elf_backend_final_write_processing(),
4610 unless they need the information earlier than the final write.
4611 Such need can generally be supplied by replacing the tests for
4612 e_machine with the conditions used to determine it. */
4614 i_ehdrp->e_machine = bed->elf_machine_code;
4617 i_ehdrp->e_version = bed->s->ev_current;
4618 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4620 /* No program header, for now. */
4621 i_ehdrp->e_phoff = 0;
4622 i_ehdrp->e_phentsize = 0;
4623 i_ehdrp->e_phnum = 0;
4625 /* Each bfd section is section header entry. */
4626 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4627 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4629 /* If we're building an executable, we'll need a program header table. */
4630 if (abfd->flags & EXEC_P)
4632 /* It all happens later. */
4634 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4636 /* elf_build_phdrs() returns a (NULL-terminated) array of
4637 Elf_Internal_Phdrs. */
4638 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4639 i_ehdrp->e_phoff = outbase;
4640 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4645 i_ehdrp->e_phentsize = 0;
4647 i_ehdrp->e_phoff = 0;
4650 elf_tdata (abfd)->symtab_hdr.sh_name =
4651 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4652 elf_tdata (abfd)->strtab_hdr.sh_name =
4653 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4654 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4655 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4656 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4657 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4658 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4664 /* Assign file positions for all the reloc sections which are not part
4665 of the loadable file image. */
4668 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4671 unsigned int i, num_sec;
4672 Elf_Internal_Shdr **shdrpp;
4674 off = elf_tdata (abfd)->next_file_pos;
4676 num_sec = elf_numsections (abfd);
4677 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4679 Elf_Internal_Shdr *shdrp;
4682 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4683 && shdrp->sh_offset == -1)
4684 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4687 elf_tdata (abfd)->next_file_pos = off;
4691 _bfd_elf_write_object_contents (bfd *abfd)
4693 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4694 Elf_Internal_Ehdr *i_ehdrp;
4695 Elf_Internal_Shdr **i_shdrp;
4697 unsigned int count, num_sec;
4699 if (! abfd->output_has_begun
4700 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4703 i_shdrp = elf_elfsections (abfd);
4704 i_ehdrp = elf_elfheader (abfd);
4707 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4711 _bfd_elf_assign_file_positions_for_relocs (abfd);
4713 /* After writing the headers, we need to write the sections too... */
4714 num_sec = elf_numsections (abfd);
4715 for (count = 1; count < num_sec; count++)
4717 if (bed->elf_backend_section_processing)
4718 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4719 if (i_shdrp[count]->contents)
4721 bfd_size_type amt = i_shdrp[count]->sh_size;
4723 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4724 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4727 if (count == SHN_LORESERVE - 1)
4728 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4731 /* Write out the section header names. */
4732 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4733 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4736 if (bed->elf_backend_final_write_processing)
4737 (*bed->elf_backend_final_write_processing) (abfd,
4738 elf_tdata (abfd)->linker);
4740 return bed->s->write_shdrs_and_ehdr (abfd);
4744 _bfd_elf_write_corefile_contents (bfd *abfd)
4746 /* Hopefully this can be done just like an object file. */
4747 return _bfd_elf_write_object_contents (abfd);
4750 /* Given a section, search the header to find them. */
4753 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4755 const struct elf_backend_data *bed;
4758 if (elf_section_data (asect) != NULL
4759 && elf_section_data (asect)->this_idx != 0)
4760 return elf_section_data (asect)->this_idx;
4762 if (bfd_is_abs_section (asect))
4764 else if (bfd_is_com_section (asect))
4766 else if (bfd_is_und_section (asect))
4770 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4771 int maxindex = elf_numsections (abfd);
4773 for (index = 1; index < maxindex; index++)
4775 Elf_Internal_Shdr *hdr = i_shdrp[index];
4777 if (hdr != NULL && hdr->bfd_section == asect)
4783 bed = get_elf_backend_data (abfd);
4784 if (bed->elf_backend_section_from_bfd_section)
4788 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4793 bfd_set_error (bfd_error_nonrepresentable_section);
4798 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4802 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4804 asymbol *asym_ptr = *asym_ptr_ptr;
4806 flagword flags = asym_ptr->flags;
4808 /* When gas creates relocations against local labels, it creates its
4809 own symbol for the section, but does put the symbol into the
4810 symbol chain, so udata is 0. When the linker is generating
4811 relocatable output, this section symbol may be for one of the
4812 input sections rather than the output section. */
4813 if (asym_ptr->udata.i == 0
4814 && (flags & BSF_SECTION_SYM)
4815 && asym_ptr->section)
4819 if (asym_ptr->section->output_section != NULL)
4820 indx = asym_ptr->section->output_section->index;
4822 indx = asym_ptr->section->index;
4823 if (indx < elf_num_section_syms (abfd)
4824 && elf_section_syms (abfd)[indx] != NULL)
4825 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4828 idx = asym_ptr->udata.i;
4832 /* This case can occur when using --strip-symbol on a symbol
4833 which is used in a relocation entry. */
4834 (*_bfd_error_handler)
4835 (_("%B: symbol `%s' required but not present"),
4836 abfd, bfd_asymbol_name (asym_ptr));
4837 bfd_set_error (bfd_error_no_symbols);
4844 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4845 (long) asym_ptr, asym_ptr->name, idx, flags,
4846 elf_symbol_flags (flags));
4854 /* Copy private BFD data. This copies any program header information. */
4857 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4859 Elf_Internal_Ehdr *iehdr;
4860 struct elf_segment_map *map;
4861 struct elf_segment_map *map_first;
4862 struct elf_segment_map **pointer_to_map;
4863 Elf_Internal_Phdr *segment;
4866 unsigned int num_segments;
4867 bfd_boolean phdr_included = FALSE;
4868 bfd_vma maxpagesize;
4869 struct elf_segment_map *phdr_adjust_seg = NULL;
4870 unsigned int phdr_adjust_num = 0;
4871 const struct elf_backend_data *bed;
4873 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4874 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4877 if (elf_tdata (ibfd)->phdr == NULL)
4880 bed = get_elf_backend_data (ibfd);
4881 iehdr = elf_elfheader (ibfd);
4884 pointer_to_map = &map_first;
4886 num_segments = elf_elfheader (ibfd)->e_phnum;
4887 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4889 /* Returns the end address of the segment + 1. */
4890 #define SEGMENT_END(segment, start) \
4891 (start + (segment->p_memsz > segment->p_filesz \
4892 ? segment->p_memsz : segment->p_filesz))
4894 #define SECTION_SIZE(section, segment) \
4895 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4896 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4897 ? section->size : 0)
4899 /* Returns TRUE if the given section is contained within
4900 the given segment. VMA addresses are compared. */
4901 #define IS_CONTAINED_BY_VMA(section, segment) \
4902 (section->vma >= segment->p_vaddr \
4903 && (section->vma + SECTION_SIZE (section, segment) \
4904 <= (SEGMENT_END (segment, segment->p_vaddr))))
4906 /* Returns TRUE if the given section is contained within
4907 the given segment. LMA addresses are compared. */
4908 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4909 (section->lma >= base \
4910 && (section->lma + SECTION_SIZE (section, segment) \
4911 <= SEGMENT_END (segment, base)))
4913 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4914 #define IS_COREFILE_NOTE(p, s) \
4915 (p->p_type == PT_NOTE \
4916 && bfd_get_format (ibfd) == bfd_core \
4917 && s->vma == 0 && s->lma == 0 \
4918 && (bfd_vma) s->filepos >= p->p_offset \
4919 && ((bfd_vma) s->filepos + s->size \
4920 <= p->p_offset + p->p_filesz))
4922 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4923 linker, which generates a PT_INTERP section with p_vaddr and
4924 p_memsz set to 0. */
4925 #define IS_SOLARIS_PT_INTERP(p, s) \
4927 && p->p_paddr == 0 \
4928 && p->p_memsz == 0 \
4929 && p->p_filesz > 0 \
4930 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4932 && (bfd_vma) s->filepos >= p->p_offset \
4933 && ((bfd_vma) s->filepos + s->size \
4934 <= p->p_offset + p->p_filesz))
4936 /* Decide if the given section should be included in the given segment.
4937 A section will be included if:
4938 1. It is within the address space of the segment -- we use the LMA
4939 if that is set for the segment and the VMA otherwise,
4940 2. It is an allocated segment,
4941 3. There is an output section associated with it,
4942 4. The section has not already been allocated to a previous segment.
4943 5. PT_GNU_STACK segments do not include any sections.
4944 6. PT_TLS segment includes only SHF_TLS sections.
4945 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
4946 8. PT_DYNAMIC should not contain empty sections at the beginning
4947 (with the possible exception of .dynamic). */
4948 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4949 ((((segment->p_paddr \
4950 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4951 : IS_CONTAINED_BY_VMA (section, segment)) \
4952 && (section->flags & SEC_ALLOC) != 0) \
4953 || IS_COREFILE_NOTE (segment, section)) \
4954 && section->output_section != NULL \
4955 && segment->p_type != PT_GNU_STACK \
4956 && (segment->p_type != PT_TLS \
4957 || (section->flags & SEC_THREAD_LOCAL)) \
4958 && (segment->p_type == PT_LOAD \
4959 || segment->p_type == PT_TLS \
4960 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4961 && (segment->p_type != PT_DYNAMIC \
4962 || SECTION_SIZE (section, segment) > 0 \
4963 || (segment->p_paddr \
4964 ? segment->p_paddr != section->lma \
4965 : segment->p_vaddr != section->vma) \
4966 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
4968 && ! section->segment_mark)
4970 /* Returns TRUE iff seg1 starts after the end of seg2. */
4971 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4972 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4974 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4975 their VMA address ranges and their LMA address ranges overlap.
4976 It is possible to have overlapping VMA ranges without overlapping LMA
4977 ranges. RedBoot images for example can have both .data and .bss mapped
4978 to the same VMA range, but with the .data section mapped to a different
4980 #define SEGMENT_OVERLAPS(seg1, seg2) \
4981 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4982 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4983 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4984 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4986 /* Initialise the segment mark field. */
4987 for (section = ibfd->sections; section != NULL; section = section->next)
4988 section->segment_mark = FALSE;
4990 /* Scan through the segments specified in the program header
4991 of the input BFD. For this first scan we look for overlaps
4992 in the loadable segments. These can be created by weird
4993 parameters to objcopy. Also, fix some solaris weirdness. */
4994 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4999 Elf_Internal_Phdr *segment2;
5001 if (segment->p_type == PT_INTERP)
5002 for (section = ibfd->sections; section; section = section->next)
5003 if (IS_SOLARIS_PT_INTERP (segment, section))
5005 /* Mininal change so that the normal section to segment
5006 assignment code will work. */
5007 segment->p_vaddr = section->vma;
5011 if (segment->p_type != PT_LOAD)
5014 /* Determine if this segment overlaps any previous segments. */
5015 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
5017 bfd_signed_vma extra_length;
5019 if (segment2->p_type != PT_LOAD
5020 || ! SEGMENT_OVERLAPS (segment, segment2))
5023 /* Merge the two segments together. */
5024 if (segment2->p_vaddr < segment->p_vaddr)
5026 /* Extend SEGMENT2 to include SEGMENT and then delete
5029 SEGMENT_END (segment, segment->p_vaddr)
5030 - SEGMENT_END (segment2, segment2->p_vaddr);
5032 if (extra_length > 0)
5034 segment2->p_memsz += extra_length;
5035 segment2->p_filesz += extra_length;
5038 segment->p_type = PT_NULL;
5040 /* Since we have deleted P we must restart the outer loop. */
5042 segment = elf_tdata (ibfd)->phdr;
5047 /* Extend SEGMENT to include SEGMENT2 and then delete
5050 SEGMENT_END (segment2, segment2->p_vaddr)
5051 - SEGMENT_END (segment, segment->p_vaddr);
5053 if (extra_length > 0)
5055 segment->p_memsz += extra_length;
5056 segment->p_filesz += extra_length;
5059 segment2->p_type = PT_NULL;
5064 /* The second scan attempts to assign sections to segments. */
5065 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5069 unsigned int section_count;
5070 asection ** sections;
5071 asection * output_section;
5073 bfd_vma matching_lma;
5074 bfd_vma suggested_lma;
5078 if (segment->p_type == PT_NULL)
5081 /* Compute how many sections might be placed into this segment. */
5082 for (section = ibfd->sections, section_count = 0;
5084 section = section->next)
5085 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5088 /* Allocate a segment map big enough to contain
5089 all of the sections we have selected. */
5090 amt = sizeof (struct elf_segment_map);
5091 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5092 map = bfd_alloc (obfd, amt);
5096 /* Initialise the fields of the segment map. Default to
5097 using the physical address of the segment in the input BFD. */
5099 map->p_type = segment->p_type;
5100 map->p_flags = segment->p_flags;
5101 map->p_flags_valid = 1;
5102 map->p_paddr = segment->p_paddr;
5103 map->p_paddr_valid = 1;
5105 /* Determine if this segment contains the ELF file header
5106 and if it contains the program headers themselves. */
5107 map->includes_filehdr = (segment->p_offset == 0
5108 && segment->p_filesz >= iehdr->e_ehsize);
5110 map->includes_phdrs = 0;
5112 if (! phdr_included || segment->p_type != PT_LOAD)
5114 map->includes_phdrs =
5115 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5116 && (segment->p_offset + segment->p_filesz
5117 >= ((bfd_vma) iehdr->e_phoff
5118 + iehdr->e_phnum * iehdr->e_phentsize)));
5120 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5121 phdr_included = TRUE;
5124 if (section_count == 0)
5126 /* Special segments, such as the PT_PHDR segment, may contain
5127 no sections, but ordinary, loadable segments should contain
5128 something. They are allowed by the ELF spec however, so only
5129 a warning is produced. */
5130 if (segment->p_type == PT_LOAD)
5131 (*_bfd_error_handler)
5132 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5136 *pointer_to_map = map;
5137 pointer_to_map = &map->next;
5142 /* Now scan the sections in the input BFD again and attempt
5143 to add their corresponding output sections to the segment map.
5144 The problem here is how to handle an output section which has
5145 been moved (ie had its LMA changed). There are four possibilities:
5147 1. None of the sections have been moved.
5148 In this case we can continue to use the segment LMA from the
5151 2. All of the sections have been moved by the same amount.
5152 In this case we can change the segment's LMA to match the LMA
5153 of the first section.
5155 3. Some of the sections have been moved, others have not.
5156 In this case those sections which have not been moved can be
5157 placed in the current segment which will have to have its size,
5158 and possibly its LMA changed, and a new segment or segments will
5159 have to be created to contain the other sections.
5161 4. The sections have been moved, but not by the same amount.
5162 In this case we can change the segment's LMA to match the LMA
5163 of the first section and we will have to create a new segment
5164 or segments to contain the other sections.
5166 In order to save time, we allocate an array to hold the section
5167 pointers that we are interested in. As these sections get assigned
5168 to a segment, they are removed from this array. */
5170 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5171 to work around this long long bug. */
5172 amt = section_count * sizeof (asection *);
5173 sections = bfd_malloc (amt);
5174 if (sections == NULL)
5177 /* Step One: Scan for segment vs section LMA conflicts.
5178 Also add the sections to the section array allocated above.
5179 Also add the sections to the current segment. In the common
5180 case, where the sections have not been moved, this means that
5181 we have completely filled the segment, and there is nothing
5187 for (j = 0, section = ibfd->sections;
5189 section = section->next)
5191 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5193 output_section = section->output_section;
5195 sections[j ++] = section;
5197 /* The Solaris native linker always sets p_paddr to 0.
5198 We try to catch that case here, and set it to the
5199 correct value. Note - some backends require that
5200 p_paddr be left as zero. */
5201 if (segment->p_paddr == 0
5202 && segment->p_vaddr != 0
5203 && (! bed->want_p_paddr_set_to_zero)
5205 && output_section->lma != 0
5206 && (output_section->vma == (segment->p_vaddr
5207 + (map->includes_filehdr
5210 + (map->includes_phdrs
5212 * iehdr->e_phentsize)
5214 map->p_paddr = segment->p_vaddr;
5216 /* Match up the physical address of the segment with the
5217 LMA address of the output section. */
5218 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5219 || IS_COREFILE_NOTE (segment, section)
5220 || (bed->want_p_paddr_set_to_zero &&
5221 IS_CONTAINED_BY_VMA (output_section, segment))
5224 if (matching_lma == 0)
5225 matching_lma = output_section->lma;
5227 /* We assume that if the section fits within the segment
5228 then it does not overlap any other section within that
5230 map->sections[isec ++] = output_section;
5232 else if (suggested_lma == 0)
5233 suggested_lma = output_section->lma;
5237 BFD_ASSERT (j == section_count);
5239 /* Step Two: Adjust the physical address of the current segment,
5241 if (isec == section_count)
5243 /* All of the sections fitted within the segment as currently
5244 specified. This is the default case. Add the segment to
5245 the list of built segments and carry on to process the next
5246 program header in the input BFD. */
5247 map->count = section_count;
5248 *pointer_to_map = map;
5249 pointer_to_map = &map->next;
5256 if (matching_lma != 0)
5258 /* At least one section fits inside the current segment.
5259 Keep it, but modify its physical address to match the
5260 LMA of the first section that fitted. */
5261 map->p_paddr = matching_lma;
5265 /* None of the sections fitted inside the current segment.
5266 Change the current segment's physical address to match
5267 the LMA of the first section. */
5268 map->p_paddr = suggested_lma;
5271 /* Offset the segment physical address from the lma
5272 to allow for space taken up by elf headers. */
5273 if (map->includes_filehdr)
5274 map->p_paddr -= iehdr->e_ehsize;
5276 if (map->includes_phdrs)
5278 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5280 /* iehdr->e_phnum is just an estimate of the number
5281 of program headers that we will need. Make a note
5282 here of the number we used and the segment we chose
5283 to hold these headers, so that we can adjust the
5284 offset when we know the correct value. */
5285 phdr_adjust_num = iehdr->e_phnum;
5286 phdr_adjust_seg = map;
5290 /* Step Three: Loop over the sections again, this time assigning
5291 those that fit to the current segment and removing them from the
5292 sections array; but making sure not to leave large gaps. Once all
5293 possible sections have been assigned to the current segment it is
5294 added to the list of built segments and if sections still remain
5295 to be assigned, a new segment is constructed before repeating
5303 /* Fill the current segment with sections that fit. */
5304 for (j = 0; j < section_count; j++)
5306 section = sections[j];
5308 if (section == NULL)
5311 output_section = section->output_section;
5313 BFD_ASSERT (output_section != NULL);
5315 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5316 || IS_COREFILE_NOTE (segment, section))
5318 if (map->count == 0)
5320 /* If the first section in a segment does not start at
5321 the beginning of the segment, then something is
5323 if (output_section->lma !=
5325 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5326 + (map->includes_phdrs
5327 ? iehdr->e_phnum * iehdr->e_phentsize
5333 asection * prev_sec;
5335 prev_sec = map->sections[map->count - 1];
5337 /* If the gap between the end of the previous section
5338 and the start of this section is more than
5339 maxpagesize then we need to start a new segment. */
5340 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5342 < BFD_ALIGN (output_section->lma, maxpagesize))
5343 || ((prev_sec->lma + prev_sec->size)
5344 > output_section->lma))
5346 if (suggested_lma == 0)
5347 suggested_lma = output_section->lma;
5353 map->sections[map->count++] = output_section;
5356 section->segment_mark = TRUE;
5358 else if (suggested_lma == 0)
5359 suggested_lma = output_section->lma;
5362 BFD_ASSERT (map->count > 0);
5364 /* Add the current segment to the list of built segments. */
5365 *pointer_to_map = map;
5366 pointer_to_map = &map->next;
5368 if (isec < section_count)
5370 /* We still have not allocated all of the sections to
5371 segments. Create a new segment here, initialise it
5372 and carry on looping. */
5373 amt = sizeof (struct elf_segment_map);
5374 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5375 map = bfd_alloc (obfd, amt);
5382 /* Initialise the fields of the segment map. Set the physical
5383 physical address to the LMA of the first section that has
5384 not yet been assigned. */
5386 map->p_type = segment->p_type;
5387 map->p_flags = segment->p_flags;
5388 map->p_flags_valid = 1;
5389 map->p_paddr = suggested_lma;
5390 map->p_paddr_valid = 1;
5391 map->includes_filehdr = 0;
5392 map->includes_phdrs = 0;
5395 while (isec < section_count);
5400 /* The Solaris linker creates program headers in which all the
5401 p_paddr fields are zero. When we try to objcopy or strip such a
5402 file, we get confused. Check for this case, and if we find it
5403 reset the p_paddr_valid fields. */
5404 for (map = map_first; map != NULL; map = map->next)
5405 if (map->p_paddr != 0)
5408 for (map = map_first; map != NULL; map = map->next)
5409 map->p_paddr_valid = 0;
5411 elf_tdata (obfd)->segment_map = map_first;
5413 /* If we had to estimate the number of program headers that were
5414 going to be needed, then check our estimate now and adjust
5415 the offset if necessary. */
5416 if (phdr_adjust_seg != NULL)
5420 for (count = 0, map = map_first; map != NULL; map = map->next)
5423 if (count > phdr_adjust_num)
5424 phdr_adjust_seg->p_paddr
5425 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5429 /* Final Step: Sort the segments into ascending order of physical
5431 if (map_first != NULL)
5433 struct elf_segment_map *prev;
5436 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5438 /* Yes I know - its a bubble sort.... */
5439 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5441 /* Swap map and map->next. */
5442 prev->next = map->next;
5443 map->next = map->next->next;
5444 prev->next->next = map;
5455 #undef IS_CONTAINED_BY_VMA
5456 #undef IS_CONTAINED_BY_LMA
5457 #undef IS_COREFILE_NOTE
5458 #undef IS_SOLARIS_PT_INTERP
5459 #undef INCLUDE_SECTION_IN_SEGMENT
5460 #undef SEGMENT_AFTER_SEGMENT
5461 #undef SEGMENT_OVERLAPS
5465 /* Copy private section information. This copies over the entsize
5466 field, and sometimes the info field. */
5469 _bfd_elf_copy_private_section_data (bfd *ibfd,
5474 Elf_Internal_Shdr *ihdr, *ohdr;
5476 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5477 || obfd->xvec->flavour != bfd_target_elf_flavour)
5480 ihdr = &elf_section_data (isec)->this_hdr;
5481 ohdr = &elf_section_data (osec)->this_hdr;
5483 ohdr->sh_entsize = ihdr->sh_entsize;
5485 if (ihdr->sh_type == SHT_SYMTAB
5486 || ihdr->sh_type == SHT_DYNSYM
5487 || ihdr->sh_type == SHT_GNU_verneed
5488 || ihdr->sh_type == SHT_GNU_verdef)
5489 ohdr->sh_info = ihdr->sh_info;
5491 /* Set things up for objcopy. The output SHT_GROUP section will
5492 have its elf_next_in_group pointing back to the input group
5494 elf_next_in_group (osec) = elf_next_in_group (isec);
5495 elf_group_name (osec) = elf_group_name (isec);
5497 osec->use_rela_p = isec->use_rela_p;
5502 /* Copy private header information. */
5505 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5507 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5508 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5511 /* Copy over private BFD data if it has not already been copied.
5512 This must be done here, rather than in the copy_private_bfd_data
5513 entry point, because the latter is called after the section
5514 contents have been set, which means that the program headers have
5515 already been worked out. */
5516 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5518 if (! copy_private_bfd_data (ibfd, obfd))
5525 /* Copy private symbol information. If this symbol is in a section
5526 which we did not map into a BFD section, try to map the section
5527 index correctly. We use special macro definitions for the mapped
5528 section indices; these definitions are interpreted by the
5529 swap_out_syms function. */
5531 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5532 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5533 #define MAP_STRTAB (SHN_HIOS + 3)
5534 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5535 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5538 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5543 elf_symbol_type *isym, *osym;
5545 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5546 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5549 isym = elf_symbol_from (ibfd, isymarg);
5550 osym = elf_symbol_from (obfd, osymarg);
5554 && bfd_is_abs_section (isym->symbol.section))
5558 shndx = isym->internal_elf_sym.st_shndx;
5559 if (shndx == elf_onesymtab (ibfd))
5560 shndx = MAP_ONESYMTAB;
5561 else if (shndx == elf_dynsymtab (ibfd))
5562 shndx = MAP_DYNSYMTAB;
5563 else if (shndx == elf_tdata (ibfd)->strtab_section)
5565 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5566 shndx = MAP_SHSTRTAB;
5567 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5568 shndx = MAP_SYM_SHNDX;
5569 osym->internal_elf_sym.st_shndx = shndx;
5575 /* Swap out the symbols. */
5578 swap_out_syms (bfd *abfd,
5579 struct bfd_strtab_hash **sttp,
5582 const struct elf_backend_data *bed;
5585 struct bfd_strtab_hash *stt;
5586 Elf_Internal_Shdr *symtab_hdr;
5587 Elf_Internal_Shdr *symtab_shndx_hdr;
5588 Elf_Internal_Shdr *symstrtab_hdr;
5589 char *outbound_syms;
5590 char *outbound_shndx;
5593 bfd_boolean name_local_sections;
5595 if (!elf_map_symbols (abfd))
5598 /* Dump out the symtabs. */
5599 stt = _bfd_elf_stringtab_init ();
5603 bed = get_elf_backend_data (abfd);
5604 symcount = bfd_get_symcount (abfd);
5605 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5606 symtab_hdr->sh_type = SHT_SYMTAB;
5607 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5608 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5609 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5610 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5612 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5613 symstrtab_hdr->sh_type = SHT_STRTAB;
5615 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5616 outbound_syms = bfd_alloc (abfd, amt);
5617 if (outbound_syms == NULL)
5619 _bfd_stringtab_free (stt);
5622 symtab_hdr->contents = outbound_syms;
5624 outbound_shndx = NULL;
5625 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5626 if (symtab_shndx_hdr->sh_name != 0)
5628 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5629 outbound_shndx = bfd_zalloc (abfd, amt);
5630 if (outbound_shndx == NULL)
5632 _bfd_stringtab_free (stt);
5636 symtab_shndx_hdr->contents = outbound_shndx;
5637 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5638 symtab_shndx_hdr->sh_size = amt;
5639 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5640 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5643 /* Now generate the data (for "contents"). */
5645 /* Fill in zeroth symbol and swap it out. */
5646 Elf_Internal_Sym sym;
5652 sym.st_shndx = SHN_UNDEF;
5653 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5654 outbound_syms += bed->s->sizeof_sym;
5655 if (outbound_shndx != NULL)
5656 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5660 = (bed->elf_backend_name_local_section_symbols
5661 && bed->elf_backend_name_local_section_symbols (abfd));
5663 syms = bfd_get_outsymbols (abfd);
5664 for (idx = 0; idx < symcount; idx++)
5666 Elf_Internal_Sym sym;
5667 bfd_vma value = syms[idx]->value;
5668 elf_symbol_type *type_ptr;
5669 flagword flags = syms[idx]->flags;
5672 if (!name_local_sections
5673 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5675 /* Local section symbols have no name. */
5680 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5683 if (sym.st_name == (unsigned long) -1)
5685 _bfd_stringtab_free (stt);
5690 type_ptr = elf_symbol_from (abfd, syms[idx]);
5692 if ((flags & BSF_SECTION_SYM) == 0
5693 && bfd_is_com_section (syms[idx]->section))
5695 /* ELF common symbols put the alignment into the `value' field,
5696 and the size into the `size' field. This is backwards from
5697 how BFD handles it, so reverse it here. */
5698 sym.st_size = value;
5699 if (type_ptr == NULL
5700 || type_ptr->internal_elf_sym.st_value == 0)
5701 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5703 sym.st_value = type_ptr->internal_elf_sym.st_value;
5704 sym.st_shndx = _bfd_elf_section_from_bfd_section
5705 (abfd, syms[idx]->section);
5709 asection *sec = syms[idx]->section;
5712 if (sec->output_section)
5714 value += sec->output_offset;
5715 sec = sec->output_section;
5718 /* Don't add in the section vma for relocatable output. */
5719 if (! relocatable_p)
5721 sym.st_value = value;
5722 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5724 if (bfd_is_abs_section (sec)
5726 && type_ptr->internal_elf_sym.st_shndx != 0)
5728 /* This symbol is in a real ELF section which we did
5729 not create as a BFD section. Undo the mapping done
5730 by copy_private_symbol_data. */
5731 shndx = type_ptr->internal_elf_sym.st_shndx;
5735 shndx = elf_onesymtab (abfd);
5738 shndx = elf_dynsymtab (abfd);
5741 shndx = elf_tdata (abfd)->strtab_section;
5744 shndx = elf_tdata (abfd)->shstrtab_section;
5747 shndx = elf_tdata (abfd)->symtab_shndx_section;
5755 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5761 /* Writing this would be a hell of a lot easier if
5762 we had some decent documentation on bfd, and
5763 knew what to expect of the library, and what to
5764 demand of applications. For example, it
5765 appears that `objcopy' might not set the
5766 section of a symbol to be a section that is
5767 actually in the output file. */
5768 sec2 = bfd_get_section_by_name (abfd, sec->name);
5771 _bfd_error_handler (_("\
5772 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5773 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5775 bfd_set_error (bfd_error_invalid_operation);
5776 _bfd_stringtab_free (stt);
5780 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5781 BFD_ASSERT (shndx != -1);
5785 sym.st_shndx = shndx;
5788 if ((flags & BSF_THREAD_LOCAL) != 0)
5790 else if ((flags & BSF_FUNCTION) != 0)
5792 else if ((flags & BSF_OBJECT) != 0)
5797 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5800 /* Processor-specific types. */
5801 if (type_ptr != NULL
5802 && bed->elf_backend_get_symbol_type)
5803 type = ((*bed->elf_backend_get_symbol_type)
5804 (&type_ptr->internal_elf_sym, type));
5806 if (flags & BSF_SECTION_SYM)
5808 if (flags & BSF_GLOBAL)
5809 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5811 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5813 else if (bfd_is_com_section (syms[idx]->section))
5814 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5815 else if (bfd_is_und_section (syms[idx]->section))
5816 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5820 else if (flags & BSF_FILE)
5821 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5824 int bind = STB_LOCAL;
5826 if (flags & BSF_LOCAL)
5828 else if (flags & BSF_WEAK)
5830 else if (flags & BSF_GLOBAL)
5833 sym.st_info = ELF_ST_INFO (bind, type);
5836 if (type_ptr != NULL)
5837 sym.st_other = type_ptr->internal_elf_sym.st_other;
5841 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5842 outbound_syms += bed->s->sizeof_sym;
5843 if (outbound_shndx != NULL)
5844 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5848 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5849 symstrtab_hdr->sh_type = SHT_STRTAB;
5851 symstrtab_hdr->sh_flags = 0;
5852 symstrtab_hdr->sh_addr = 0;
5853 symstrtab_hdr->sh_entsize = 0;
5854 symstrtab_hdr->sh_link = 0;
5855 symstrtab_hdr->sh_info = 0;
5856 symstrtab_hdr->sh_addralign = 1;
5861 /* Return the number of bytes required to hold the symtab vector.
5863 Note that we base it on the count plus 1, since we will null terminate
5864 the vector allocated based on this size. However, the ELF symbol table
5865 always has a dummy entry as symbol #0, so it ends up even. */
5868 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5872 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5874 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5875 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5877 symtab_size -= sizeof (asymbol *);
5883 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5887 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5889 if (elf_dynsymtab (abfd) == 0)
5891 bfd_set_error (bfd_error_invalid_operation);
5895 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5896 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5898 symtab_size -= sizeof (asymbol *);
5904 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5907 return (asect->reloc_count + 1) * sizeof (arelent *);
5910 /* Canonicalize the relocs. */
5913 _bfd_elf_canonicalize_reloc (bfd *abfd,
5920 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5922 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5925 tblptr = section->relocation;
5926 for (i = 0; i < section->reloc_count; i++)
5927 *relptr++ = tblptr++;
5931 return section->reloc_count;
5935 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
5937 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5938 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5941 bfd_get_symcount (abfd) = symcount;
5946 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5947 asymbol **allocation)
5949 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5950 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5953 bfd_get_dynamic_symcount (abfd) = symcount;
5957 /* Return the size required for the dynamic reloc entries. Any loadable
5958 section that was actually installed in the BFD, and has type SHT_REL
5959 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
5960 dynamic reloc section. */
5963 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5968 if (elf_dynsymtab (abfd) == 0)
5970 bfd_set_error (bfd_error_invalid_operation);
5974 ret = sizeof (arelent *);
5975 for (s = abfd->sections; s != NULL; s = s->next)
5976 if ((s->flags & SEC_LOAD) != 0
5977 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5978 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5979 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5980 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
5981 * sizeof (arelent *));
5986 /* Canonicalize the dynamic relocation entries. Note that we return the
5987 dynamic relocations as a single block, although they are actually
5988 associated with particular sections; the interface, which was
5989 designed for SunOS style shared libraries, expects that there is only
5990 one set of dynamic relocs. Any loadable section that was actually
5991 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
5992 dynamic symbol table, is considered to be a dynamic reloc section. */
5995 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5999 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6003 if (elf_dynsymtab (abfd) == 0)
6005 bfd_set_error (bfd_error_invalid_operation);
6009 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6011 for (s = abfd->sections; s != NULL; s = s->next)
6013 if ((s->flags & SEC_LOAD) != 0
6014 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6015 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6016 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6021 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6023 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6025 for (i = 0; i < count; i++)
6036 /* Read in the version information. */
6039 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6041 bfd_byte *contents = NULL;
6043 unsigned int freeidx = 0;
6045 if (elf_dynverref (abfd) != 0)
6047 Elf_Internal_Shdr *hdr;
6048 Elf_External_Verneed *everneed;
6049 Elf_Internal_Verneed *iverneed;
6052 hdr = &elf_tdata (abfd)->dynverref_hdr;
6054 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
6055 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
6056 if (elf_tdata (abfd)->verref == NULL)
6059 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6061 contents = bfd_malloc (hdr->sh_size);
6062 if (contents == NULL)
6064 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6065 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6068 everneed = (Elf_External_Verneed *) contents;
6069 iverneed = elf_tdata (abfd)->verref;
6070 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6072 Elf_External_Vernaux *evernaux;
6073 Elf_Internal_Vernaux *ivernaux;
6076 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6078 iverneed->vn_bfd = abfd;
6080 iverneed->vn_filename =
6081 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6083 if (iverneed->vn_filename == NULL)
6086 amt = iverneed->vn_cnt;
6087 amt *= sizeof (Elf_Internal_Vernaux);
6088 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
6090 evernaux = ((Elf_External_Vernaux *)
6091 ((bfd_byte *) everneed + iverneed->vn_aux));
6092 ivernaux = iverneed->vn_auxptr;
6093 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6095 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6097 ivernaux->vna_nodename =
6098 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6099 ivernaux->vna_name);
6100 if (ivernaux->vna_nodename == NULL)
6103 if (j + 1 < iverneed->vn_cnt)
6104 ivernaux->vna_nextptr = ivernaux + 1;
6106 ivernaux->vna_nextptr = NULL;
6108 evernaux = ((Elf_External_Vernaux *)
6109 ((bfd_byte *) evernaux + ivernaux->vna_next));
6111 if (ivernaux->vna_other > freeidx)
6112 freeidx = ivernaux->vna_other;
6115 if (i + 1 < hdr->sh_info)
6116 iverneed->vn_nextref = iverneed + 1;
6118 iverneed->vn_nextref = NULL;
6120 everneed = ((Elf_External_Verneed *)
6121 ((bfd_byte *) everneed + iverneed->vn_next));
6128 if (elf_dynverdef (abfd) != 0)
6130 Elf_Internal_Shdr *hdr;
6131 Elf_External_Verdef *everdef;
6132 Elf_Internal_Verdef *iverdef;
6133 Elf_Internal_Verdef *iverdefarr;
6134 Elf_Internal_Verdef iverdefmem;
6136 unsigned int maxidx;
6138 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6140 contents = bfd_malloc (hdr->sh_size);
6141 if (contents == NULL)
6143 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6144 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6147 /* We know the number of entries in the section but not the maximum
6148 index. Therefore we have to run through all entries and find
6150 everdef = (Elf_External_Verdef *) contents;
6152 for (i = 0; i < hdr->sh_info; ++i)
6154 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6156 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6157 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6159 everdef = ((Elf_External_Verdef *)
6160 ((bfd_byte *) everdef + iverdefmem.vd_next));
6163 if (default_imported_symver)
6165 if (freeidx > maxidx)
6170 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
6171 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
6172 if (elf_tdata (abfd)->verdef == NULL)
6175 elf_tdata (abfd)->cverdefs = maxidx;
6177 everdef = (Elf_External_Verdef *) contents;
6178 iverdefarr = elf_tdata (abfd)->verdef;
6179 for (i = 0; i < hdr->sh_info; i++)
6181 Elf_External_Verdaux *everdaux;
6182 Elf_Internal_Verdaux *iverdaux;
6185 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6187 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6188 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6190 iverdef->vd_bfd = abfd;
6192 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
6193 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
6194 if (iverdef->vd_auxptr == NULL)
6197 everdaux = ((Elf_External_Verdaux *)
6198 ((bfd_byte *) everdef + iverdef->vd_aux));
6199 iverdaux = iverdef->vd_auxptr;
6200 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6202 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6204 iverdaux->vda_nodename =
6205 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6206 iverdaux->vda_name);
6207 if (iverdaux->vda_nodename == NULL)
6210 if (j + 1 < iverdef->vd_cnt)
6211 iverdaux->vda_nextptr = iverdaux + 1;
6213 iverdaux->vda_nextptr = NULL;
6215 everdaux = ((Elf_External_Verdaux *)
6216 ((bfd_byte *) everdaux + iverdaux->vda_next));
6219 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6221 if (i + 1 < hdr->sh_info)
6222 iverdef->vd_nextdef = iverdef + 1;
6224 iverdef->vd_nextdef = NULL;
6226 everdef = ((Elf_External_Verdef *)
6227 ((bfd_byte *) everdef + iverdef->vd_next));
6233 else if (default_imported_symver)
6240 amt = (bfd_size_type) freeidx * sizeof (Elf_Internal_Verdef);
6241 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
6242 if (elf_tdata (abfd)->verdef == NULL)
6245 elf_tdata (abfd)->cverdefs = freeidx;
6248 /* Create a default version based on the soname. */
6249 if (default_imported_symver)
6251 Elf_Internal_Verdef *iverdef;
6252 Elf_Internal_Verdaux *iverdaux;
6254 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6256 iverdef->vd_version = VER_DEF_CURRENT;
6257 iverdef->vd_flags = 0;
6258 iverdef->vd_ndx = freeidx;
6259 iverdef->vd_cnt = 1;
6261 iverdef->vd_bfd = abfd;
6263 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6264 if (iverdef->vd_nodename == NULL)
6266 iverdef->vd_nextdef = NULL;
6267 amt = (bfd_size_type) sizeof (Elf_Internal_Verdaux);
6268 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
6270 iverdaux = iverdef->vd_auxptr;
6271 iverdaux->vda_nodename = iverdef->vd_nodename;
6272 iverdaux->vda_nextptr = NULL;
6278 if (contents != NULL)
6284 _bfd_elf_make_empty_symbol (bfd *abfd)
6286 elf_symbol_type *newsym;
6287 bfd_size_type amt = sizeof (elf_symbol_type);
6289 newsym = bfd_zalloc (abfd, amt);
6294 newsym->symbol.the_bfd = abfd;
6295 return &newsym->symbol;
6300 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6304 bfd_symbol_info (symbol, ret);
6307 /* Return whether a symbol name implies a local symbol. Most targets
6308 use this function for the is_local_label_name entry point, but some
6312 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6315 /* Normal local symbols start with ``.L''. */
6316 if (name[0] == '.' && name[1] == 'L')
6319 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6320 DWARF debugging symbols starting with ``..''. */
6321 if (name[0] == '.' && name[1] == '.')
6324 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6325 emitting DWARF debugging output. I suspect this is actually a
6326 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6327 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6328 underscore to be emitted on some ELF targets). For ease of use,
6329 we treat such symbols as local. */
6330 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6337 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6338 asymbol *symbol ATTRIBUTE_UNUSED)
6345 _bfd_elf_set_arch_mach (bfd *abfd,
6346 enum bfd_architecture arch,
6347 unsigned long machine)
6349 /* If this isn't the right architecture for this backend, and this
6350 isn't the generic backend, fail. */
6351 if (arch != get_elf_backend_data (abfd)->arch
6352 && arch != bfd_arch_unknown
6353 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6356 return bfd_default_set_arch_mach (abfd, arch, machine);
6359 /* Find the function to a particular section and offset,
6360 for error reporting. */
6363 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6367 const char **filename_ptr,
6368 const char **functionname_ptr)
6370 const char *filename;
6371 asymbol *func, *file;
6374 /* ??? Given multiple file symbols, it is impossible to reliably
6375 choose the right file name for global symbols. File symbols are
6376 local symbols, and thus all file symbols must sort before any
6377 global symbols. The ELF spec may be interpreted to say that a
6378 file symbol must sort before other local symbols, but currently
6379 ld -r doesn't do this. So, for ld -r output, it is possible to
6380 make a better choice of file name for local symbols by ignoring
6381 file symbols appearing after a given local symbol. */
6382 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6388 state = nothing_seen;
6390 for (p = symbols; *p != NULL; p++)
6394 q = (elf_symbol_type *) *p;
6396 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6402 if (state == symbol_seen)
6403 state = file_after_symbol_seen;
6409 if (bfd_get_section (&q->symbol) == section
6410 && q->symbol.value >= low_func
6411 && q->symbol.value <= offset)
6413 func = (asymbol *) q;
6414 low_func = q->symbol.value;
6417 else if (ELF_ST_BIND (q->internal_elf_sym.st_info) != STB_LOCAL
6418 && state == file_after_symbol_seen)
6421 filename = bfd_asymbol_name (file);
6425 if (state == nothing_seen)
6426 state = symbol_seen;
6433 *filename_ptr = filename;
6434 if (functionname_ptr)
6435 *functionname_ptr = bfd_asymbol_name (func);
6440 /* Find the nearest line to a particular section and offset,
6441 for error reporting. */
6444 _bfd_elf_find_nearest_line (bfd *abfd,
6448 const char **filename_ptr,
6449 const char **functionname_ptr,
6450 unsigned int *line_ptr)
6454 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6455 filename_ptr, functionname_ptr,
6458 if (!*functionname_ptr)
6459 elf_find_function (abfd, section, symbols, offset,
6460 *filename_ptr ? NULL : filename_ptr,
6466 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6467 filename_ptr, functionname_ptr,
6469 &elf_tdata (abfd)->dwarf2_find_line_info))
6471 if (!*functionname_ptr)
6472 elf_find_function (abfd, section, symbols, offset,
6473 *filename_ptr ? NULL : filename_ptr,
6479 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6480 &found, filename_ptr,
6481 functionname_ptr, line_ptr,
6482 &elf_tdata (abfd)->line_info))
6484 if (found && (*functionname_ptr || *line_ptr))
6487 if (symbols == NULL)
6490 if (! elf_find_function (abfd, section, symbols, offset,
6491 filename_ptr, functionname_ptr))
6499 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6503 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6505 ret += get_program_header_size (abfd);
6510 _bfd_elf_set_section_contents (bfd *abfd,
6512 const void *location,
6514 bfd_size_type count)
6516 Elf_Internal_Shdr *hdr;
6519 if (! abfd->output_has_begun
6520 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6523 hdr = &elf_section_data (section)->this_hdr;
6524 pos = hdr->sh_offset + offset;
6525 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6526 || bfd_bwrite (location, count, abfd) != count)
6533 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6534 arelent *cache_ptr ATTRIBUTE_UNUSED,
6535 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6540 /* Try to convert a non-ELF reloc into an ELF one. */
6543 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6545 /* Check whether we really have an ELF howto. */
6547 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6549 bfd_reloc_code_real_type code;
6550 reloc_howto_type *howto;
6552 /* Alien reloc: Try to determine its type to replace it with an
6553 equivalent ELF reloc. */
6555 if (areloc->howto->pc_relative)
6557 switch (areloc->howto->bitsize)
6560 code = BFD_RELOC_8_PCREL;
6563 code = BFD_RELOC_12_PCREL;
6566 code = BFD_RELOC_16_PCREL;
6569 code = BFD_RELOC_24_PCREL;
6572 code = BFD_RELOC_32_PCREL;
6575 code = BFD_RELOC_64_PCREL;
6581 howto = bfd_reloc_type_lookup (abfd, code);
6583 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6585 if (howto->pcrel_offset)
6586 areloc->addend += areloc->address;
6588 areloc->addend -= areloc->address; /* addend is unsigned!! */
6593 switch (areloc->howto->bitsize)
6599 code = BFD_RELOC_14;
6602 code = BFD_RELOC_16;
6605 code = BFD_RELOC_26;
6608 code = BFD_RELOC_32;
6611 code = BFD_RELOC_64;
6617 howto = bfd_reloc_type_lookup (abfd, code);
6621 areloc->howto = howto;
6629 (*_bfd_error_handler)
6630 (_("%B: unsupported relocation type %s"),
6631 abfd, areloc->howto->name);
6632 bfd_set_error (bfd_error_bad_value);
6637 _bfd_elf_close_and_cleanup (bfd *abfd)
6639 if (bfd_get_format (abfd) == bfd_object)
6641 if (elf_shstrtab (abfd) != NULL)
6642 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6645 return _bfd_generic_close_and_cleanup (abfd);
6648 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6649 in the relocation's offset. Thus we cannot allow any sort of sanity
6650 range-checking to interfere. There is nothing else to do in processing
6653 bfd_reloc_status_type
6654 _bfd_elf_rel_vtable_reloc_fn
6655 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6656 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6657 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6658 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6660 return bfd_reloc_ok;
6663 /* Elf core file support. Much of this only works on native
6664 toolchains, since we rely on knowing the
6665 machine-dependent procfs structure in order to pick
6666 out details about the corefile. */
6668 #ifdef HAVE_SYS_PROCFS_H
6669 # include <sys/procfs.h>
6672 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6675 elfcore_make_pid (bfd *abfd)
6677 return ((elf_tdata (abfd)->core_lwpid << 16)
6678 + (elf_tdata (abfd)->core_pid));
6681 /* If there isn't a section called NAME, make one, using
6682 data from SECT. Note, this function will generate a
6683 reference to NAME, so you shouldn't deallocate or
6687 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6691 if (bfd_get_section_by_name (abfd, name) != NULL)
6694 sect2 = bfd_make_section (abfd, name);
6698 sect2->size = sect->size;
6699 sect2->filepos = sect->filepos;
6700 sect2->flags = sect->flags;
6701 sect2->alignment_power = sect->alignment_power;
6705 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6706 actually creates up to two pseudosections:
6707 - For the single-threaded case, a section named NAME, unless
6708 such a section already exists.
6709 - For the multi-threaded case, a section named "NAME/PID", where
6710 PID is elfcore_make_pid (abfd).
6711 Both pseudosections have identical contents. */
6713 _bfd_elfcore_make_pseudosection (bfd *abfd,
6719 char *threaded_name;
6723 /* Build the section name. */
6725 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6726 len = strlen (buf) + 1;
6727 threaded_name = bfd_alloc (abfd, len);
6728 if (threaded_name == NULL)
6730 memcpy (threaded_name, buf, len);
6732 sect = bfd_make_section_anyway (abfd, threaded_name);
6736 sect->filepos = filepos;
6737 sect->flags = SEC_HAS_CONTENTS;
6738 sect->alignment_power = 2;
6740 return elfcore_maybe_make_sect (abfd, name, sect);
6743 /* prstatus_t exists on:
6745 linux 2.[01] + glibc
6749 #if defined (HAVE_PRSTATUS_T)
6752 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6757 if (note->descsz == sizeof (prstatus_t))
6761 size = sizeof (prstat.pr_reg);
6762 offset = offsetof (prstatus_t, pr_reg);
6763 memcpy (&prstat, note->descdata, sizeof (prstat));
6765 /* Do not overwrite the core signal if it
6766 has already been set by another thread. */
6767 if (elf_tdata (abfd)->core_signal == 0)
6768 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6769 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6771 /* pr_who exists on:
6774 pr_who doesn't exist on:
6777 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6778 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6781 #if defined (HAVE_PRSTATUS32_T)
6782 else if (note->descsz == sizeof (prstatus32_t))
6784 /* 64-bit host, 32-bit corefile */
6785 prstatus32_t prstat;
6787 size = sizeof (prstat.pr_reg);
6788 offset = offsetof (prstatus32_t, pr_reg);
6789 memcpy (&prstat, note->descdata, sizeof (prstat));
6791 /* Do not overwrite the core signal if it
6792 has already been set by another thread. */
6793 if (elf_tdata (abfd)->core_signal == 0)
6794 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6795 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6797 /* pr_who exists on:
6800 pr_who doesn't exist on:
6803 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6804 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6807 #endif /* HAVE_PRSTATUS32_T */
6810 /* Fail - we don't know how to handle any other
6811 note size (ie. data object type). */
6815 /* Make a ".reg/999" section and a ".reg" section. */
6816 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6817 size, note->descpos + offset);
6819 #endif /* defined (HAVE_PRSTATUS_T) */
6821 /* Create a pseudosection containing the exact contents of NOTE. */
6823 elfcore_make_note_pseudosection (bfd *abfd,
6825 Elf_Internal_Note *note)
6827 return _bfd_elfcore_make_pseudosection (abfd, name,
6828 note->descsz, note->descpos);
6831 /* There isn't a consistent prfpregset_t across platforms,
6832 but it doesn't matter, because we don't have to pick this
6833 data structure apart. */
6836 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6838 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6841 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6842 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6846 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6848 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6851 #if defined (HAVE_PRPSINFO_T)
6852 typedef prpsinfo_t elfcore_psinfo_t;
6853 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6854 typedef prpsinfo32_t elfcore_psinfo32_t;
6858 #if defined (HAVE_PSINFO_T)
6859 typedef psinfo_t elfcore_psinfo_t;
6860 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6861 typedef psinfo32_t elfcore_psinfo32_t;
6865 /* return a malloc'ed copy of a string at START which is at
6866 most MAX bytes long, possibly without a terminating '\0'.
6867 the copy will always have a terminating '\0'. */
6870 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6873 char *end = memchr (start, '\0', max);
6881 dups = bfd_alloc (abfd, len + 1);
6885 memcpy (dups, start, len);
6891 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6893 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6895 if (note->descsz == sizeof (elfcore_psinfo_t))
6897 elfcore_psinfo_t psinfo;
6899 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6901 elf_tdata (abfd)->core_program
6902 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6903 sizeof (psinfo.pr_fname));
6905 elf_tdata (abfd)->core_command
6906 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6907 sizeof (psinfo.pr_psargs));
6909 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6910 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6912 /* 64-bit host, 32-bit corefile */
6913 elfcore_psinfo32_t psinfo;
6915 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6917 elf_tdata (abfd)->core_program
6918 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6919 sizeof (psinfo.pr_fname));
6921 elf_tdata (abfd)->core_command
6922 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6923 sizeof (psinfo.pr_psargs));
6929 /* Fail - we don't know how to handle any other
6930 note size (ie. data object type). */
6934 /* Note that for some reason, a spurious space is tacked
6935 onto the end of the args in some (at least one anyway)
6936 implementations, so strip it off if it exists. */
6939 char *command = elf_tdata (abfd)->core_command;
6940 int n = strlen (command);
6942 if (0 < n && command[n - 1] == ' ')
6943 command[n - 1] = '\0';
6948 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6950 #if defined (HAVE_PSTATUS_T)
6952 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6954 if (note->descsz == sizeof (pstatus_t)
6955 #if defined (HAVE_PXSTATUS_T)
6956 || note->descsz == sizeof (pxstatus_t)
6962 memcpy (&pstat, note->descdata, sizeof (pstat));
6964 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6966 #if defined (HAVE_PSTATUS32_T)
6967 else if (note->descsz == sizeof (pstatus32_t))
6969 /* 64-bit host, 32-bit corefile */
6972 memcpy (&pstat, note->descdata, sizeof (pstat));
6974 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6977 /* Could grab some more details from the "representative"
6978 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6979 NT_LWPSTATUS note, presumably. */
6983 #endif /* defined (HAVE_PSTATUS_T) */
6985 #if defined (HAVE_LWPSTATUS_T)
6987 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6989 lwpstatus_t lwpstat;
6995 if (note->descsz != sizeof (lwpstat)
6996 #if defined (HAVE_LWPXSTATUS_T)
6997 && note->descsz != sizeof (lwpxstatus_t)
7002 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7004 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7005 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7007 /* Make a ".reg/999" section. */
7009 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7010 len = strlen (buf) + 1;
7011 name = bfd_alloc (abfd, len);
7014 memcpy (name, buf, len);
7016 sect = bfd_make_section_anyway (abfd, name);
7020 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7021 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7022 sect->filepos = note->descpos
7023 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7026 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7027 sect->size = sizeof (lwpstat.pr_reg);
7028 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7031 sect->flags = SEC_HAS_CONTENTS;
7032 sect->alignment_power = 2;
7034 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7037 /* Make a ".reg2/999" section */
7039 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7040 len = strlen (buf) + 1;
7041 name = bfd_alloc (abfd, len);
7044 memcpy (name, buf, len);
7046 sect = bfd_make_section_anyway (abfd, name);
7050 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7051 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7052 sect->filepos = note->descpos
7053 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7056 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7057 sect->size = sizeof (lwpstat.pr_fpreg);
7058 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7061 sect->flags = SEC_HAS_CONTENTS;
7062 sect->alignment_power = 2;
7064 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7066 #endif /* defined (HAVE_LWPSTATUS_T) */
7068 #if defined (HAVE_WIN32_PSTATUS_T)
7070 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7076 win32_pstatus_t pstatus;
7078 if (note->descsz < sizeof (pstatus))
7081 memcpy (&pstatus, note->descdata, sizeof (pstatus));
7083 switch (pstatus.data_type)
7085 case NOTE_INFO_PROCESS:
7086 /* FIXME: need to add ->core_command. */
7087 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
7088 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
7091 case NOTE_INFO_THREAD:
7092 /* Make a ".reg/999" section. */
7093 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
7095 len = strlen (buf) + 1;
7096 name = bfd_alloc (abfd, len);
7100 memcpy (name, buf, len);
7102 sect = bfd_make_section_anyway (abfd, name);
7106 sect->size = sizeof (pstatus.data.thread_info.thread_context);
7107 sect->filepos = (note->descpos
7108 + offsetof (struct win32_pstatus,
7109 data.thread_info.thread_context));
7110 sect->flags = SEC_HAS_CONTENTS;
7111 sect->alignment_power = 2;
7113 if (pstatus.data.thread_info.is_active_thread)
7114 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7118 case NOTE_INFO_MODULE:
7119 /* Make a ".module/xxxxxxxx" section. */
7120 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
7122 len = strlen (buf) + 1;
7123 name = bfd_alloc (abfd, len);
7127 memcpy (name, buf, len);
7129 sect = bfd_make_section_anyway (abfd, name);
7134 sect->size = note->descsz;
7135 sect->filepos = note->descpos;
7136 sect->flags = SEC_HAS_CONTENTS;
7137 sect->alignment_power = 2;
7146 #endif /* HAVE_WIN32_PSTATUS_T */
7149 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7159 if (bed->elf_backend_grok_prstatus)
7160 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7162 #if defined (HAVE_PRSTATUS_T)
7163 return elfcore_grok_prstatus (abfd, note);
7168 #if defined (HAVE_PSTATUS_T)
7170 return elfcore_grok_pstatus (abfd, note);
7173 #if defined (HAVE_LWPSTATUS_T)
7175 return elfcore_grok_lwpstatus (abfd, note);
7178 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7179 return elfcore_grok_prfpreg (abfd, note);
7181 #if defined (HAVE_WIN32_PSTATUS_T)
7182 case NT_WIN32PSTATUS:
7183 return elfcore_grok_win32pstatus (abfd, note);
7186 case NT_PRXFPREG: /* Linux SSE extension */
7187 if (note->namesz == 6
7188 && strcmp (note->namedata, "LINUX") == 0)
7189 return elfcore_grok_prxfpreg (abfd, note);
7195 if (bed->elf_backend_grok_psinfo)
7196 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7198 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7199 return elfcore_grok_psinfo (abfd, note);
7206 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
7210 sect->size = note->descsz;
7211 sect->filepos = note->descpos;
7212 sect->flags = SEC_HAS_CONTENTS;
7213 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7221 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7225 cp = strchr (note->namedata, '@');
7228 *lwpidp = atoi(cp + 1);
7235 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7238 /* Signal number at offset 0x08. */
7239 elf_tdata (abfd)->core_signal
7240 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7242 /* Process ID at offset 0x50. */
7243 elf_tdata (abfd)->core_pid
7244 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7246 /* Command name at 0x7c (max 32 bytes, including nul). */
7247 elf_tdata (abfd)->core_command
7248 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7250 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7255 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7259 if (elfcore_netbsd_get_lwpid (note, &lwp))
7260 elf_tdata (abfd)->core_lwpid = lwp;
7262 if (note->type == NT_NETBSDCORE_PROCINFO)
7264 /* NetBSD-specific core "procinfo". Note that we expect to
7265 find this note before any of the others, which is fine,
7266 since the kernel writes this note out first when it
7267 creates a core file. */
7269 return elfcore_grok_netbsd_procinfo (abfd, note);
7272 /* As of Jan 2002 there are no other machine-independent notes
7273 defined for NetBSD core files. If the note type is less
7274 than the start of the machine-dependent note types, we don't
7277 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7281 switch (bfd_get_arch (abfd))
7283 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7284 PT_GETFPREGS == mach+2. */
7286 case bfd_arch_alpha:
7287 case bfd_arch_sparc:
7290 case NT_NETBSDCORE_FIRSTMACH+0:
7291 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7293 case NT_NETBSDCORE_FIRSTMACH+2:
7294 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7300 /* On all other arch's, PT_GETREGS == mach+1 and
7301 PT_GETFPREGS == mach+3. */
7306 case NT_NETBSDCORE_FIRSTMACH+1:
7307 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7309 case NT_NETBSDCORE_FIRSTMACH+3:
7310 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7320 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
7322 void *ddata = note->descdata;
7329 /* nto_procfs_status 'pid' field is at offset 0. */
7330 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7332 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7333 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7335 /* nto_procfs_status 'flags' field is at offset 8. */
7336 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7338 /* nto_procfs_status 'what' field is at offset 14. */
7339 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7341 elf_tdata (abfd)->core_signal = sig;
7342 elf_tdata (abfd)->core_lwpid = *tid;
7345 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7346 do not come from signals so we make sure we set the current
7347 thread just in case. */
7348 if (flags & 0x00000080)
7349 elf_tdata (abfd)->core_lwpid = *tid;
7351 /* Make a ".qnx_core_status/%d" section. */
7352 sprintf (buf, ".qnx_core_status/%d", *tid);
7354 name = bfd_alloc (abfd, strlen (buf) + 1);
7359 sect = bfd_make_section_anyway (abfd, name);
7363 sect->size = note->descsz;
7364 sect->filepos = note->descpos;
7365 sect->flags = SEC_HAS_CONTENTS;
7366 sect->alignment_power = 2;
7368 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7372 elfcore_grok_nto_regs (bfd *abfd,
7373 Elf_Internal_Note *note,
7381 /* Make a "(base)/%d" section. */
7382 sprintf (buf, "%s/%d", base, tid);
7384 name = bfd_alloc (abfd, strlen (buf) + 1);
7389 sect = bfd_make_section_anyway (abfd, name);
7393 sect->size = note->descsz;
7394 sect->filepos = note->descpos;
7395 sect->flags = SEC_HAS_CONTENTS;
7396 sect->alignment_power = 2;
7398 /* This is the current thread. */
7399 if (elf_tdata (abfd)->core_lwpid == tid)
7400 return elfcore_maybe_make_sect (abfd, base, sect);
7405 #define BFD_QNT_CORE_INFO 7
7406 #define BFD_QNT_CORE_STATUS 8
7407 #define BFD_QNT_CORE_GREG 9
7408 #define BFD_QNT_CORE_FPREG 10
7411 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7413 /* Every GREG section has a STATUS section before it. Store the
7414 tid from the previous call to pass down to the next gregs
7416 static pid_t tid = 1;
7420 case BFD_QNT_CORE_INFO:
7421 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7422 case BFD_QNT_CORE_STATUS:
7423 return elfcore_grok_nto_status (abfd, note, &tid);
7424 case BFD_QNT_CORE_GREG:
7425 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
7426 case BFD_QNT_CORE_FPREG:
7427 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
7433 /* Function: elfcore_write_note
7440 size of data for note
7443 End of buffer containing note. */
7446 elfcore_write_note (bfd *abfd,
7454 Elf_External_Note *xnp;
7464 const struct elf_backend_data *bed;
7466 namesz = strlen (name) + 1;
7467 bed = get_elf_backend_data (abfd);
7468 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7471 newspace = 12 + namesz + pad + size;
7473 p = realloc (buf, *bufsiz + newspace);
7475 *bufsiz += newspace;
7476 xnp = (Elf_External_Note *) dest;
7477 H_PUT_32 (abfd, namesz, xnp->namesz);
7478 H_PUT_32 (abfd, size, xnp->descsz);
7479 H_PUT_32 (abfd, type, xnp->type);
7483 memcpy (dest, name, namesz);
7491 memcpy (dest, input, size);
7495 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7497 elfcore_write_prpsinfo (bfd *abfd,
7504 char *note_name = "CORE";
7506 #if defined (HAVE_PSINFO_T)
7508 note_type = NT_PSINFO;
7511 note_type = NT_PRPSINFO;
7514 memset (&data, 0, sizeof (data));
7515 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7516 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7517 return elfcore_write_note (abfd, buf, bufsiz,
7518 note_name, note_type, &data, sizeof (data));
7520 #endif /* PSINFO_T or PRPSINFO_T */
7522 #if defined (HAVE_PRSTATUS_T)
7524 elfcore_write_prstatus (bfd *abfd,
7532 char *note_name = "CORE";
7534 memset (&prstat, 0, sizeof (prstat));
7535 prstat.pr_pid = pid;
7536 prstat.pr_cursig = cursig;
7537 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7538 return elfcore_write_note (abfd, buf, bufsiz,
7539 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7541 #endif /* HAVE_PRSTATUS_T */
7543 #if defined (HAVE_LWPSTATUS_T)
7545 elfcore_write_lwpstatus (bfd *abfd,
7552 lwpstatus_t lwpstat;
7553 char *note_name = "CORE";
7555 memset (&lwpstat, 0, sizeof (lwpstat));
7556 lwpstat.pr_lwpid = pid >> 16;
7557 lwpstat.pr_cursig = cursig;
7558 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7559 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7560 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7562 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7563 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7565 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7566 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7569 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7570 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7572 #endif /* HAVE_LWPSTATUS_T */
7574 #if defined (HAVE_PSTATUS_T)
7576 elfcore_write_pstatus (bfd *abfd,
7584 char *note_name = "CORE";
7586 memset (&pstat, 0, sizeof (pstat));
7587 pstat.pr_pid = pid & 0xffff;
7588 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7589 NT_PSTATUS, &pstat, sizeof (pstat));
7592 #endif /* HAVE_PSTATUS_T */
7595 elfcore_write_prfpreg (bfd *abfd,
7601 char *note_name = "CORE";
7602 return elfcore_write_note (abfd, buf, bufsiz,
7603 note_name, NT_FPREGSET, fpregs, size);
7607 elfcore_write_prxfpreg (bfd *abfd,
7610 const void *xfpregs,
7613 char *note_name = "LINUX";
7614 return elfcore_write_note (abfd, buf, bufsiz,
7615 note_name, NT_PRXFPREG, xfpregs, size);
7619 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7627 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7630 buf = bfd_malloc (size);
7634 if (bfd_bread (buf, size, abfd) != size)
7642 while (p < buf + size)
7644 /* FIXME: bad alignment assumption. */
7645 Elf_External_Note *xnp = (Elf_External_Note *) p;
7646 Elf_Internal_Note in;
7648 in.type = H_GET_32 (abfd, xnp->type);
7650 in.namesz = H_GET_32 (abfd, xnp->namesz);
7651 in.namedata = xnp->name;
7653 in.descsz = H_GET_32 (abfd, xnp->descsz);
7654 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7655 in.descpos = offset + (in.descdata - buf);
7657 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7659 if (! elfcore_grok_netbsd_note (abfd, &in))
7662 else if (strncmp (in.namedata, "QNX", 3) == 0)
7664 if (! elfcore_grok_nto_note (abfd, &in))
7669 if (! elfcore_grok_note (abfd, &in))
7673 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7680 /* Providing external access to the ELF program header table. */
7682 /* Return an upper bound on the number of bytes required to store a
7683 copy of ABFD's program header table entries. Return -1 if an error
7684 occurs; bfd_get_error will return an appropriate code. */
7687 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7689 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7691 bfd_set_error (bfd_error_wrong_format);
7695 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7698 /* Copy ABFD's program header table entries to *PHDRS. The entries
7699 will be stored as an array of Elf_Internal_Phdr structures, as
7700 defined in include/elf/internal.h. To find out how large the
7701 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7703 Return the number of program header table entries read, or -1 if an
7704 error occurs; bfd_get_error will return an appropriate code. */
7707 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7711 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7713 bfd_set_error (bfd_error_wrong_format);
7717 num_phdrs = elf_elfheader (abfd)->e_phnum;
7718 memcpy (phdrs, elf_tdata (abfd)->phdr,
7719 num_phdrs * sizeof (Elf_Internal_Phdr));
7725 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7728 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7730 i_ehdrp = elf_elfheader (abfd);
7731 if (i_ehdrp == NULL)
7732 sprintf_vma (buf, value);
7735 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7737 #if BFD_HOST_64BIT_LONG
7738 sprintf (buf, "%016lx", value);
7740 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7741 _bfd_int64_low (value));
7745 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7748 sprintf_vma (buf, value);
7753 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7756 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7758 i_ehdrp = elf_elfheader (abfd);
7759 if (i_ehdrp == NULL)
7760 fprintf_vma ((FILE *) stream, value);
7763 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7765 #if BFD_HOST_64BIT_LONG
7766 fprintf ((FILE *) stream, "%016lx", value);
7768 fprintf ((FILE *) stream, "%08lx%08lx",
7769 _bfd_int64_high (value), _bfd_int64_low (value));
7773 fprintf ((FILE *) stream, "%08lx",
7774 (unsigned long) (value & 0xffffffff));
7777 fprintf_vma ((FILE *) stream, value);
7781 enum elf_reloc_type_class
7782 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7784 return reloc_class_normal;
7787 /* For RELA architectures, return the relocation value for a
7788 relocation against a local symbol. */
7791 _bfd_elf_rela_local_sym (bfd *abfd,
7792 Elf_Internal_Sym *sym,
7794 Elf_Internal_Rela *rel)
7796 asection *sec = *psec;
7799 relocation = (sec->output_section->vma
7800 + sec->output_offset
7802 if ((sec->flags & SEC_MERGE)
7803 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7804 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7807 _bfd_merged_section_offset (abfd, psec,
7808 elf_section_data (sec)->sec_info,
7809 sym->st_value + rel->r_addend);
7812 /* If we have changed the section, and our original section is
7813 marked with SEC_EXCLUDE, it means that the original
7814 SEC_MERGE section has been completely subsumed in some
7815 other SEC_MERGE section. In this case, we need to leave
7816 some info around for --emit-relocs. */
7817 if ((sec->flags & SEC_EXCLUDE) != 0)
7818 sec->kept_section = *psec;
7821 rel->r_addend -= relocation;
7822 rel->r_addend += sec->output_section->vma + sec->output_offset;
7828 _bfd_elf_rel_local_sym (bfd *abfd,
7829 Elf_Internal_Sym *sym,
7833 asection *sec = *psec;
7835 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7836 return sym->st_value + addend;
7838 return _bfd_merged_section_offset (abfd, psec,
7839 elf_section_data (sec)->sec_info,
7840 sym->st_value + addend);
7844 _bfd_elf_section_offset (bfd *abfd,
7845 struct bfd_link_info *info,
7849 switch (sec->sec_info_type)
7851 case ELF_INFO_TYPE_STABS:
7852 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
7854 case ELF_INFO_TYPE_EH_FRAME:
7855 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
7861 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7862 reconstruct an ELF file by reading the segments out of remote memory
7863 based on the ELF file header at EHDR_VMA and the ELF program headers it
7864 points to. If not null, *LOADBASEP is filled in with the difference
7865 between the VMAs from which the segments were read, and the VMAs the
7866 file headers (and hence BFD's idea of each section's VMA) put them at.
7868 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7869 remote memory at target address VMA into the local buffer at MYADDR; it
7870 should return zero on success or an `errno' code on failure. TEMPL must
7871 be a BFD for an ELF target with the word size and byte order found in
7872 the remote memory. */
7875 bfd_elf_bfd_from_remote_memory
7879 int (*target_read_memory) (bfd_vma, char *, int))
7881 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7882 (templ, ehdr_vma, loadbasep, target_read_memory);
7886 _bfd_elf_get_synthetic_symtab (bfd *abfd,
7887 long symcount ATTRIBUTE_UNUSED,
7888 asymbol **syms ATTRIBUTE_UNUSED,
7893 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7896 const char *relplt_name;
7897 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7901 Elf_Internal_Shdr *hdr;
7907 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
7910 if (dynsymcount <= 0)
7913 if (!bed->plt_sym_val)
7916 relplt_name = bed->relplt_name;
7917 if (relplt_name == NULL)
7918 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7919 relplt = bfd_get_section_by_name (abfd, relplt_name);
7923 hdr = &elf_section_data (relplt)->this_hdr;
7924 if (hdr->sh_link != elf_dynsymtab (abfd)
7925 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7928 plt = bfd_get_section_by_name (abfd, ".plt");
7932 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7933 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
7936 count = relplt->size / hdr->sh_entsize;
7937 size = count * sizeof (asymbol);
7938 p = relplt->relocation;
7939 for (i = 0; i < count; i++, s++, p++)
7940 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7942 s = *ret = bfd_malloc (size);
7946 names = (char *) (s + count);
7947 p = relplt->relocation;
7949 for (i = 0; i < count; i++, s++, p++)
7954 addr = bed->plt_sym_val (i, plt, p);
7955 if (addr == (bfd_vma) -1)
7958 *s = **p->sym_ptr_ptr;
7960 s->value = addr - plt->vma;
7962 len = strlen ((*p->sym_ptr_ptr)->name);
7963 memcpy (names, (*p->sym_ptr_ptr)->name, len);
7965 memcpy (names, "@plt", sizeof ("@plt"));
7966 names += sizeof ("@plt");
7973 /* Sort symbol by binding and section. We want to put definitions
7974 sorted by section at the beginning. */
7977 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7979 const Elf_Internal_Sym *s1;
7980 const Elf_Internal_Sym *s2;
7983 /* Make sure that undefined symbols are at the end. */
7984 s1 = (const Elf_Internal_Sym *) arg1;
7985 if (s1->st_shndx == SHN_UNDEF)
7987 s2 = (const Elf_Internal_Sym *) arg2;
7988 if (s2->st_shndx == SHN_UNDEF)
7991 /* Sorted by section index. */
7992 shndx = s1->st_shndx - s2->st_shndx;
7996 /* Sorted by binding. */
7997 return ELF_ST_BIND (s1->st_info) - ELF_ST_BIND (s2->st_info);
8002 Elf_Internal_Sym *sym;
8007 elf_sym_name_compare (const void *arg1, const void *arg2)
8009 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8010 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8011 return strcmp (s1->name, s2->name);
8014 /* Check if 2 sections define the same set of local and global
8018 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2)
8021 const struct elf_backend_data *bed1, *bed2;
8022 Elf_Internal_Shdr *hdr1, *hdr2;
8023 bfd_size_type symcount1, symcount2;
8024 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8025 Elf_Internal_Sym *isymstart1 = NULL, *isymstart2 = NULL, *isym;
8026 Elf_Internal_Sym *isymend;
8027 struct elf_symbol *symp, *symtable1 = NULL, *symtable2 = NULL;
8028 bfd_size_type count1, count2, i;
8035 /* If both are .gnu.linkonce sections, they have to have the same
8037 if (strncmp (sec1->name, ".gnu.linkonce",
8038 sizeof ".gnu.linkonce" - 1) == 0
8039 && strncmp (sec2->name, ".gnu.linkonce",
8040 sizeof ".gnu.linkonce" - 1) == 0)
8041 return strcmp (sec1->name + sizeof ".gnu.linkonce",
8042 sec2->name + sizeof ".gnu.linkonce") == 0;
8044 /* Both sections have to be in ELF. */
8045 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8046 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8049 if (elf_section_type (sec1) != elf_section_type (sec2))
8052 if ((elf_section_flags (sec1) & SHF_GROUP) != 0
8053 && (elf_section_flags (sec2) & SHF_GROUP) != 0)
8055 /* If both are members of section groups, they have to have the
8057 if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0)
8061 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8062 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8063 if (shndx1 == -1 || shndx2 == -1)
8066 bed1 = get_elf_backend_data (bfd1);
8067 bed2 = get_elf_backend_data (bfd2);
8068 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8069 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8070 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8071 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8073 if (symcount1 == 0 || symcount2 == 0)
8076 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8078 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8082 if (isymbuf1 == NULL || isymbuf2 == NULL)
8085 /* Sort symbols by binding and section. Global definitions are at
8087 qsort (isymbuf1, symcount1, sizeof (Elf_Internal_Sym),
8088 elf_sort_elf_symbol);
8089 qsort (isymbuf2, symcount2, sizeof (Elf_Internal_Sym),
8090 elf_sort_elf_symbol);
8092 /* Count definitions in the section. */
8094 for (isym = isymbuf1, isymend = isym + symcount1;
8095 isym < isymend; isym++)
8097 if (isym->st_shndx == (unsigned int) shndx1)
8104 if (count1 && isym->st_shndx != (unsigned int) shndx1)
8109 for (isym = isymbuf2, isymend = isym + symcount2;
8110 isym < isymend; isym++)
8112 if (isym->st_shndx == (unsigned int) shndx2)
8119 if (count2 && isym->st_shndx != (unsigned int) shndx2)
8123 if (count1 == 0 || count2 == 0 || count1 != count2)
8126 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8127 symtable2 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8129 if (symtable1 == NULL || symtable2 == NULL)
8133 for (isym = isymstart1, isymend = isym + count1;
8134 isym < isymend; isym++)
8137 symp->name = bfd_elf_string_from_elf_section (bfd1,
8144 for (isym = isymstart2, isymend = isym + count1;
8145 isym < isymend; isym++)
8148 symp->name = bfd_elf_string_from_elf_section (bfd2,
8154 /* Sort symbol by name. */
8155 qsort (symtable1, count1, sizeof (struct elf_symbol),
8156 elf_sym_name_compare);
8157 qsort (symtable2, count1, sizeof (struct elf_symbol),
8158 elf_sym_name_compare);
8160 for (i = 0; i < count1; i++)
8161 /* Two symbols must have the same binding, type and name. */
8162 if (symtable1 [i].sym->st_info != symtable2 [i].sym->st_info
8163 || symtable1 [i].sym->st_other != symtable2 [i].sym->st_other
8164 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)