1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 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
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
45 static INLINE struct elf_segment_map *make_mapping
46 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
47 static boolean map_sections_to_segments PARAMS ((bfd *));
48 static int elf_sort_sections PARAMS ((const PTR, const PTR));
49 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
50 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
51 static boolean prep_headers PARAMS ((bfd *));
52 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
53 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
54 static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
55 static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
56 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
57 static void set_group_contents PARAMS ((bfd *, asection *, PTR));
58 static boolean assign_section_numbers PARAMS ((bfd *));
59 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
60 static boolean elf_map_symbols PARAMS ((bfd *));
61 static bfd_size_type get_program_header_size PARAMS ((bfd *));
62 static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
63 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
64 bfd_vma, const char **,
66 static int elfcore_make_pid PARAMS ((bfd *));
67 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
68 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
69 Elf_Internal_Note *));
70 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
71 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
72 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
74 static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));
75 static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,
76 Elf_Internal_Note *));
77 static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));
79 /* Swap version information in and out. The version information is
80 currently size independent. If that ever changes, this code will
81 need to move into elfcode.h. */
83 /* Swap in a Verdef structure. */
86 _bfd_elf_swap_verdef_in (abfd, src, dst)
88 const Elf_External_Verdef *src;
89 Elf_Internal_Verdef *dst;
91 dst->vd_version = H_GET_16 (abfd, src->vd_version);
92 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
93 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
94 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
95 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
96 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
97 dst->vd_next = H_GET_32 (abfd, src->vd_next);
100 /* Swap out a Verdef structure. */
103 _bfd_elf_swap_verdef_out (abfd, src, dst)
105 const Elf_Internal_Verdef *src;
106 Elf_External_Verdef *dst;
108 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
109 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
110 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
111 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
112 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
113 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
114 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
117 /* Swap in a Verdaux structure. */
120 _bfd_elf_swap_verdaux_in (abfd, src, dst)
122 const Elf_External_Verdaux *src;
123 Elf_Internal_Verdaux *dst;
125 dst->vda_name = H_GET_32 (abfd, src->vda_name);
126 dst->vda_next = H_GET_32 (abfd, src->vda_next);
129 /* Swap out a Verdaux structure. */
132 _bfd_elf_swap_verdaux_out (abfd, src, dst)
134 const Elf_Internal_Verdaux *src;
135 Elf_External_Verdaux *dst;
137 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
138 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
141 /* Swap in a Verneed structure. */
144 _bfd_elf_swap_verneed_in (abfd, src, dst)
146 const Elf_External_Verneed *src;
147 Elf_Internal_Verneed *dst;
149 dst->vn_version = H_GET_16 (abfd, src->vn_version);
150 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
151 dst->vn_file = H_GET_32 (abfd, src->vn_file);
152 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
153 dst->vn_next = H_GET_32 (abfd, src->vn_next);
156 /* Swap out a Verneed structure. */
159 _bfd_elf_swap_verneed_out (abfd, src, dst)
161 const Elf_Internal_Verneed *src;
162 Elf_External_Verneed *dst;
164 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
165 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
166 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
167 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
168 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
171 /* Swap in a Vernaux structure. */
174 _bfd_elf_swap_vernaux_in (abfd, src, dst)
176 const Elf_External_Vernaux *src;
177 Elf_Internal_Vernaux *dst;
179 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
180 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
181 dst->vna_other = H_GET_16 (abfd, src->vna_other);
182 dst->vna_name = H_GET_32 (abfd, src->vna_name);
183 dst->vna_next = H_GET_32 (abfd, src->vna_next);
186 /* Swap out a Vernaux structure. */
189 _bfd_elf_swap_vernaux_out (abfd, src, dst)
191 const Elf_Internal_Vernaux *src;
192 Elf_External_Vernaux *dst;
194 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
195 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
196 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
197 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
198 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
201 /* Swap in a Versym structure. */
204 _bfd_elf_swap_versym_in (abfd, src, dst)
206 const Elf_External_Versym *src;
207 Elf_Internal_Versym *dst;
209 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
212 /* Swap out a Versym structure. */
215 _bfd_elf_swap_versym_out (abfd, src, dst)
217 const Elf_Internal_Versym *src;
218 Elf_External_Versym *dst;
220 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
223 /* Standard ELF hash function. Do not change this function; you will
224 cause invalid hash tables to be generated. */
227 bfd_elf_hash (namearg)
230 const unsigned char *name = (const unsigned char *) namearg;
235 while ((ch = *name++) != '\0')
238 if ((g = (h & 0xf0000000)) != 0)
241 /* The ELF ABI says `h &= ~g', but this is equivalent in
242 this case and on some machines one insn instead of two. */
249 /* Read a specified number of bytes at a specified offset in an ELF
250 file, into a newly allocated buffer, and return a pointer to the
254 elf_read (abfd, offset, size)
261 if ((buf = bfd_alloc (abfd, size)) == NULL)
263 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
265 if (bfd_bread ((PTR) buf, size, abfd) != size)
267 if (bfd_get_error () != bfd_error_system_call)
268 bfd_set_error (bfd_error_file_truncated);
275 bfd_elf_mkobject (abfd)
278 /* This just does initialization. */
279 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
280 bfd_size_type amt = sizeof (struct elf_obj_tdata);
281 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
282 if (elf_tdata (abfd) == 0)
284 /* Since everything is done at close time, do we need any
291 bfd_elf_mkcorefile (abfd)
294 /* I think this can be done just like an object file. */
295 return bfd_elf_mkobject (abfd);
299 bfd_elf_get_str_section (abfd, shindex)
301 unsigned int shindex;
303 Elf_Internal_Shdr **i_shdrp;
304 char *shstrtab = NULL;
306 bfd_size_type shstrtabsize;
308 i_shdrp = elf_elfsections (abfd);
309 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
312 shstrtab = (char *) i_shdrp[shindex]->contents;
313 if (shstrtab == NULL)
315 /* No cached one, attempt to read, and cache what we read. */
316 offset = i_shdrp[shindex]->sh_offset;
317 shstrtabsize = i_shdrp[shindex]->sh_size;
318 shstrtab = elf_read (abfd, offset, shstrtabsize);
319 i_shdrp[shindex]->contents = (PTR) shstrtab;
325 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
327 unsigned int shindex;
328 unsigned int strindex;
330 Elf_Internal_Shdr *hdr;
335 hdr = elf_elfsections (abfd)[shindex];
337 if (hdr->contents == NULL
338 && bfd_elf_get_str_section (abfd, shindex) == NULL)
341 if (strindex >= hdr->sh_size)
343 (*_bfd_error_handler)
344 (_("%s: invalid string offset %u >= %lu for section `%s'"),
345 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
346 ((shindex == elf_elfheader(abfd)->e_shstrndx
347 && strindex == hdr->sh_name)
349 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
353 return ((char *) hdr->contents) + strindex;
356 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
357 sections. The first element is the flags, the rest are section
360 typedef union elf_internal_group {
361 Elf_Internal_Shdr *shdr;
363 } Elf_Internal_Group;
365 /* Set next_in_group list pointer, and group name for NEWSECT. */
368 setup_group (abfd, hdr, newsect)
370 Elf_Internal_Shdr *hdr;
373 unsigned int num_group = elf_tdata (abfd)->num_group;
375 /* If num_group is zero, read in all SHT_GROUP sections. The count
376 is set to -1 if there are no SHT_GROUP sections. */
379 unsigned int i, shnum;
381 /* First count the number of groups. If we have a SHT_GROUP
382 section with just a flag word (ie. sh_size is 4), ignore it. */
383 shnum = elf_numsections (abfd);
385 for (i = 0; i < shnum; i++)
387 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
388 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
393 num_group = (unsigned) -1;
394 elf_tdata (abfd)->num_group = num_group;
398 /* We keep a list of elf section headers for group sections,
399 so we can find them quickly. */
400 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
401 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
402 if (elf_tdata (abfd)->group_sect_ptr == NULL)
406 for (i = 0; i < shnum; i++)
408 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
409 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
412 Elf_Internal_Group *dest;
414 /* Add to list of sections. */
415 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
418 /* Read the raw contents. */
419 BFD_ASSERT (sizeof (*dest) >= 4);
420 amt = shdr->sh_size * sizeof (*dest) / 4;
421 shdr->contents = bfd_alloc (abfd, amt);
422 if (shdr->contents == NULL
423 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
424 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
428 /* Translate raw contents, a flag word followed by an
429 array of elf section indices all in target byte order,
430 to the flag word followed by an array of elf section
432 src = shdr->contents + shdr->sh_size;
433 dest = (Elf_Internal_Group *) (shdr->contents + amt);
440 idx = H_GET_32 (abfd, src);
441 if (src == shdr->contents)
448 ((*_bfd_error_handler)
449 (_("%s: invalid SHT_GROUP entry"),
450 bfd_archive_filename (abfd)));
453 dest->shdr = elf_elfsections (abfd)[idx];
460 if (num_group != (unsigned) -1)
464 for (i = 0; i < num_group; i++)
466 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
467 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
468 unsigned int n_elt = shdr->sh_size / 4;
470 /* Look through this group's sections to see if current
471 section is a member. */
473 if ((++idx)->shdr == hdr)
477 /* We are a member of this group. Go looking through
478 other members to see if any others are linked via
480 idx = (Elf_Internal_Group *) shdr->contents;
481 n_elt = shdr->sh_size / 4;
483 if ((s = (++idx)->shdr->bfd_section) != NULL
484 && elf_next_in_group (s) != NULL)
488 /* Snarf the group name from other member, and
489 insert current section in circular list. */
490 elf_group_name (newsect) = elf_group_name (s);
491 elf_next_in_group (newsect) = elf_next_in_group (s);
492 elf_next_in_group (s) = newsect;
496 struct elf_backend_data *bed;
498 unsigned char ename[4];
502 /* Humbug. Get the name from the group signature
503 symbol. Why isn't the signature just a string?
504 Fortunately, the name index is at the same
505 place in the external symbol for both 32 and 64
507 bed = get_elf_backend_data (abfd);
508 pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
509 pos += shdr->sh_info * bed->s->sizeof_sym;
510 if (bfd_seek (abfd, pos, SEEK_SET) != 0
511 || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
513 iname = H_GET_32 (abfd, ename);
514 gname = elf_string_from_elf_strtab (abfd, iname);
515 elf_group_name (newsect) = gname;
517 /* Start a circular list with one element. */
518 elf_next_in_group (newsect) = newsect;
520 if (shdr->bfd_section != NULL)
521 elf_next_in_group (shdr->bfd_section) = newsect;
528 if (elf_group_name (newsect) == NULL)
530 (*_bfd_error_handler) (_("%s: no group info for section %s"),
531 bfd_archive_filename (abfd), newsect->name);
536 /* Make a BFD section from an ELF section. We store a pointer to the
537 BFD section in the bfd_section field of the header. */
540 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
542 Elf_Internal_Shdr *hdr;
547 struct elf_backend_data *bed;
549 if (hdr->bfd_section != NULL)
551 BFD_ASSERT (strcmp (name,
552 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
556 newsect = bfd_make_section_anyway (abfd, name);
560 newsect->filepos = hdr->sh_offset;
562 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
563 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
564 || ! bfd_set_section_alignment (abfd, newsect,
565 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
568 flags = SEC_NO_FLAGS;
569 if (hdr->sh_type != SHT_NOBITS)
570 flags |= SEC_HAS_CONTENTS;
571 if (hdr->sh_type == SHT_GROUP)
572 flags |= SEC_GROUP | SEC_EXCLUDE;
573 if ((hdr->sh_flags & SHF_ALLOC) != 0)
576 if (hdr->sh_type != SHT_NOBITS)
579 if ((hdr->sh_flags & SHF_WRITE) == 0)
580 flags |= SEC_READONLY;
581 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
583 else if ((flags & SEC_LOAD) != 0)
585 if ((hdr->sh_flags & SHF_MERGE) != 0)
588 newsect->entsize = hdr->sh_entsize;
589 if ((hdr->sh_flags & SHF_STRINGS) != 0)
590 flags |= SEC_STRINGS;
592 if (hdr->sh_flags & SHF_GROUP)
593 if (!setup_group (abfd, hdr, newsect))
596 /* The debugging sections appear to be recognized only by name, not
599 static const char *debug_sec_names [] =
608 for (i = ARRAY_SIZE (debug_sec_names); i--;)
609 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
613 flags |= SEC_DEBUGGING;
616 /* As a GNU extension, if the name begins with .gnu.linkonce, we
617 only link a single copy of the section. This is used to support
618 g++. g++ will emit each template expansion in its own section.
619 The symbols will be defined as weak, so that multiple definitions
620 are permitted. The GNU linker extension is to actually discard
621 all but one of the sections. */
622 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
623 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
625 bed = get_elf_backend_data (abfd);
626 if (bed->elf_backend_section_flags)
627 if (! bed->elf_backend_section_flags (&flags, hdr))
630 if (! bfd_set_section_flags (abfd, newsect, flags))
633 if ((flags & SEC_ALLOC) != 0)
635 Elf_Internal_Phdr *phdr;
638 /* Look through the phdrs to see if we need to adjust the lma.
639 If all the p_paddr fields are zero, we ignore them, since
640 some ELF linkers produce such output. */
641 phdr = elf_tdata (abfd)->phdr;
642 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
644 if (phdr->p_paddr != 0)
647 if (i < elf_elfheader (abfd)->e_phnum)
649 phdr = elf_tdata (abfd)->phdr;
650 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
652 /* This section is part of this segment if its file
653 offset plus size lies within the segment's memory
654 span and, if the section is loaded, the extent of the
655 loaded data lies within the extent of the segment.
656 If the p_paddr field is not set, we don't alter the
658 if (phdr->p_type == PT_LOAD
660 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
661 && (hdr->sh_offset + hdr->sh_size
662 <= phdr->p_offset + phdr->p_memsz)
663 && ((flags & SEC_LOAD) == 0
664 || (phdr->p_offset + phdr->p_filesz
665 >= hdr->sh_offset + hdr->sh_size)))
667 /* We used to do a relative adjustment here, but
668 that doesn't work if the segment is packed with
669 code from multiple VMAs. Instead we calculate
670 the LMA absoultely, based on the LMA of the
671 segment (it is assumed that the segment will
672 contain sections with contiguous LMAs, even if
673 the VMAs are not). */
674 newsect->lma = phdr->p_paddr
675 + hdr->sh_offset - phdr->p_offset;
682 hdr->bfd_section = newsect;
683 elf_section_data (newsect)->this_hdr = *hdr;
693 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
696 Helper functions for GDB to locate the string tables.
697 Since BFD hides string tables from callers, GDB needs to use an
698 internal hook to find them. Sun's .stabstr, in particular,
699 isn't even pointed to by the .stab section, so ordinary
700 mechanisms wouldn't work to find it, even if we had some.
703 struct elf_internal_shdr *
704 bfd_elf_find_section (abfd, name)
708 Elf_Internal_Shdr **i_shdrp;
713 i_shdrp = elf_elfsections (abfd);
716 shstrtab = bfd_elf_get_str_section (abfd,
717 elf_elfheader (abfd)->e_shstrndx);
718 if (shstrtab != NULL)
720 max = elf_numsections (abfd);
721 for (i = 1; i < max; i++)
722 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
729 const char *const bfd_elf_section_type_names[] = {
730 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
731 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
732 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
735 /* ELF relocs are against symbols. If we are producing relocateable
736 output, and the reloc is against an external symbol, and nothing
737 has given us any additional addend, the resulting reloc will also
738 be against the same symbol. In such a case, we don't want to
739 change anything about the way the reloc is handled, since it will
740 all be done at final link time. Rather than put special case code
741 into bfd_perform_relocation, all the reloc types use this howto
742 function. It just short circuits the reloc if producing
743 relocateable output against an external symbol. */
745 bfd_reloc_status_type
746 bfd_elf_generic_reloc (abfd,
753 bfd *abfd ATTRIBUTE_UNUSED;
754 arelent *reloc_entry;
756 PTR data ATTRIBUTE_UNUSED;
757 asection *input_section;
759 char **error_message ATTRIBUTE_UNUSED;
761 if (output_bfd != (bfd *) NULL
762 && (symbol->flags & BSF_SECTION_SYM) == 0
763 && (! reloc_entry->howto->partial_inplace
764 || reloc_entry->addend == 0))
766 reloc_entry->address += input_section->output_offset;
770 return bfd_reloc_continue;
773 /* Finish SHF_MERGE section merging. */
776 _bfd_elf_merge_sections (abfd, info)
778 struct bfd_link_info *info;
780 if (!is_elf_hash_table (info))
782 if (elf_hash_table (info)->merge_info)
783 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info);
787 /* Copy the program header and other data from one object module to
791 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
795 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
796 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
799 BFD_ASSERT (!elf_flags_init (obfd)
800 || (elf_elfheader (obfd)->e_flags
801 == elf_elfheader (ibfd)->e_flags));
803 elf_gp (obfd) = elf_gp (ibfd);
804 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
805 elf_flags_init (obfd) = true;
809 /* Print out the program headers. */
812 _bfd_elf_print_private_bfd_data (abfd, farg)
816 FILE *f = (FILE *) farg;
817 Elf_Internal_Phdr *p;
819 bfd_byte *dynbuf = NULL;
821 p = elf_tdata (abfd)->phdr;
826 fprintf (f, _("\nProgram Header:\n"));
827 c = elf_elfheader (abfd)->e_phnum;
828 for (i = 0; i < c; i++, p++)
835 case PT_NULL: pt = "NULL"; break;
836 case PT_LOAD: pt = "LOAD"; break;
837 case PT_DYNAMIC: pt = "DYNAMIC"; break;
838 case PT_INTERP: pt = "INTERP"; break;
839 case PT_NOTE: pt = "NOTE"; break;
840 case PT_SHLIB: pt = "SHLIB"; break;
841 case PT_PHDR: pt = "PHDR"; break;
842 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
843 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
845 fprintf (f, "%8s off 0x", pt);
846 bfd_fprintf_vma (abfd, f, p->p_offset);
847 fprintf (f, " vaddr 0x");
848 bfd_fprintf_vma (abfd, f, p->p_vaddr);
849 fprintf (f, " paddr 0x");
850 bfd_fprintf_vma (abfd, f, p->p_paddr);
851 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
852 fprintf (f, " filesz 0x");
853 bfd_fprintf_vma (abfd, f, p->p_filesz);
854 fprintf (f, " memsz 0x");
855 bfd_fprintf_vma (abfd, f, p->p_memsz);
856 fprintf (f, " flags %c%c%c",
857 (p->p_flags & PF_R) != 0 ? 'r' : '-',
858 (p->p_flags & PF_W) != 0 ? 'w' : '-',
859 (p->p_flags & PF_X) != 0 ? 'x' : '-');
860 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
861 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
866 s = bfd_get_section_by_name (abfd, ".dynamic");
870 unsigned long shlink;
871 bfd_byte *extdyn, *extdynend;
873 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
875 fprintf (f, _("\nDynamic Section:\n"));
877 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
880 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
884 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
887 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
889 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
890 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
893 extdynend = extdyn + s->_raw_size;
894 for (; extdyn < extdynend; extdyn += extdynsize)
896 Elf_Internal_Dyn dyn;
901 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
903 if (dyn.d_tag == DT_NULL)
910 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
914 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
915 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
916 case DT_PLTGOT: name = "PLTGOT"; break;
917 case DT_HASH: name = "HASH"; break;
918 case DT_STRTAB: name = "STRTAB"; break;
919 case DT_SYMTAB: name = "SYMTAB"; break;
920 case DT_RELA: name = "RELA"; break;
921 case DT_RELASZ: name = "RELASZ"; break;
922 case DT_RELAENT: name = "RELAENT"; break;
923 case DT_STRSZ: name = "STRSZ"; break;
924 case DT_SYMENT: name = "SYMENT"; break;
925 case DT_INIT: name = "INIT"; break;
926 case DT_FINI: name = "FINI"; break;
927 case DT_SONAME: name = "SONAME"; stringp = true; break;
928 case DT_RPATH: name = "RPATH"; stringp = true; break;
929 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
930 case DT_REL: name = "REL"; break;
931 case DT_RELSZ: name = "RELSZ"; break;
932 case DT_RELENT: name = "RELENT"; break;
933 case DT_PLTREL: name = "PLTREL"; break;
934 case DT_DEBUG: name = "DEBUG"; break;
935 case DT_TEXTREL: name = "TEXTREL"; break;
936 case DT_JMPREL: name = "JMPREL"; break;
937 case DT_BIND_NOW: name = "BIND_NOW"; break;
938 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
939 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
940 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
941 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
942 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
943 case DT_FLAGS: name = "FLAGS"; break;
944 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
945 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
946 case DT_CHECKSUM: name = "CHECKSUM"; break;
947 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
948 case DT_MOVEENT: name = "MOVEENT"; break;
949 case DT_MOVESZ: name = "MOVESZ"; break;
950 case DT_FEATURE: name = "FEATURE"; break;
951 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
952 case DT_SYMINSZ: name = "SYMINSZ"; break;
953 case DT_SYMINENT: name = "SYMINENT"; break;
954 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
955 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
956 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
957 case DT_PLTPAD: name = "PLTPAD"; break;
958 case DT_MOVETAB: name = "MOVETAB"; break;
959 case DT_SYMINFO: name = "SYMINFO"; break;
960 case DT_RELACOUNT: name = "RELACOUNT"; break;
961 case DT_RELCOUNT: name = "RELCOUNT"; break;
962 case DT_FLAGS_1: name = "FLAGS_1"; break;
963 case DT_VERSYM: name = "VERSYM"; break;
964 case DT_VERDEF: name = "VERDEF"; break;
965 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
966 case DT_VERNEED: name = "VERNEED"; break;
967 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
968 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
969 case DT_USED: name = "USED"; break;
970 case DT_FILTER: name = "FILTER"; stringp = true; break;
973 fprintf (f, " %-11s ", name);
975 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
979 unsigned int tagv = dyn.d_un.d_val;
981 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
984 fprintf (f, "%s", string);
993 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
994 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
996 if (! _bfd_elf_slurp_version_tables (abfd))
1000 if (elf_dynverdef (abfd) != 0)
1002 Elf_Internal_Verdef *t;
1004 fprintf (f, _("\nVersion definitions:\n"));
1005 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1007 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1008 t->vd_flags, t->vd_hash, t->vd_nodename);
1009 if (t->vd_auxptr->vda_nextptr != NULL)
1011 Elf_Internal_Verdaux *a;
1014 for (a = t->vd_auxptr->vda_nextptr;
1017 fprintf (f, "%s ", a->vda_nodename);
1023 if (elf_dynverref (abfd) != 0)
1025 Elf_Internal_Verneed *t;
1027 fprintf (f, _("\nVersion References:\n"));
1028 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1030 Elf_Internal_Vernaux *a;
1032 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1033 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1034 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1035 a->vna_flags, a->vna_other, a->vna_nodename);
1047 /* Display ELF-specific fields of a symbol. */
1050 bfd_elf_print_symbol (abfd, filep, symbol, how)
1054 bfd_print_symbol_type how;
1056 FILE *file = (FILE *) filep;
1059 case bfd_print_symbol_name:
1060 fprintf (file, "%s", symbol->name);
1062 case bfd_print_symbol_more:
1063 fprintf (file, "elf ");
1064 bfd_fprintf_vma (abfd, file, symbol->value);
1065 fprintf (file, " %lx", (long) symbol->flags);
1067 case bfd_print_symbol_all:
1069 const char *section_name;
1070 const char *name = NULL;
1071 struct elf_backend_data *bed;
1072 unsigned char st_other;
1075 section_name = symbol->section ? symbol->section->name : "(*none*)";
1077 bed = get_elf_backend_data (abfd);
1078 if (bed->elf_backend_print_symbol_all)
1079 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1083 name = symbol->name;
1084 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1087 fprintf (file, " %s\t", section_name);
1088 /* Print the "other" value for a symbol. For common symbols,
1089 we've already printed the size; now print the alignment.
1090 For other symbols, we have no specified alignment, and
1091 we've printed the address; now print the size. */
1092 if (bfd_is_com_section (symbol->section))
1093 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1095 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1096 bfd_fprintf_vma (abfd, file, val);
1098 /* If we have version information, print it. */
1099 if (elf_tdata (abfd)->dynversym_section != 0
1100 && (elf_tdata (abfd)->dynverdef_section != 0
1101 || elf_tdata (abfd)->dynverref_section != 0))
1103 unsigned int vernum;
1104 const char *version_string;
1106 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1109 version_string = "";
1110 else if (vernum == 1)
1111 version_string = "Base";
1112 else if (vernum <= elf_tdata (abfd)->cverdefs)
1114 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1117 Elf_Internal_Verneed *t;
1119 version_string = "";
1120 for (t = elf_tdata (abfd)->verref;
1124 Elf_Internal_Vernaux *a;
1126 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1128 if (a->vna_other == vernum)
1130 version_string = a->vna_nodename;
1137 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1138 fprintf (file, " %-11s", version_string);
1143 fprintf (file, " (%s)", version_string);
1144 for (i = 10 - strlen (version_string); i > 0; --i)
1149 /* If the st_other field is not zero, print it. */
1150 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1155 case STV_INTERNAL: fprintf (file, " .internal"); break;
1156 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1157 case STV_PROTECTED: fprintf (file, " .protected"); break;
1159 /* Some other non-defined flags are also present, so print
1161 fprintf (file, " 0x%02x", (unsigned int) st_other);
1164 fprintf (file, " %s", name);
1170 /* Create an entry in an ELF linker hash table. */
1172 struct bfd_hash_entry *
1173 _bfd_elf_link_hash_newfunc (entry, table, string)
1174 struct bfd_hash_entry *entry;
1175 struct bfd_hash_table *table;
1178 /* Allocate the structure if it has not already been allocated by a
1182 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1187 /* Call the allocation method of the superclass. */
1188 entry = _bfd_link_hash_newfunc (entry, table, string);
1191 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1192 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1194 /* Set local fields. */
1198 ret->dynstr_index = 0;
1199 ret->weakdef = NULL;
1200 ret->got.refcount = htab->init_refcount;
1201 ret->plt.refcount = htab->init_refcount;
1202 ret->linker_section_pointer = NULL;
1203 ret->verinfo.verdef = NULL;
1204 ret->vtable_entries_used = NULL;
1205 ret->vtable_entries_size = 0;
1206 ret->vtable_parent = NULL;
1207 ret->type = STT_NOTYPE;
1209 /* Assume that we have been called by a non-ELF symbol reader.
1210 This flag is then reset by the code which reads an ELF input
1211 file. This ensures that a symbol created by a non-ELF symbol
1212 reader will have the flag set correctly. */
1213 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1219 /* Copy data from an indirect symbol to its direct symbol, hiding the
1220 old indirect symbol. Also used for copying flags to a weakdef. */
1223 _bfd_elf_link_hash_copy_indirect (dir, ind)
1224 struct elf_link_hash_entry *dir, *ind;
1228 /* Copy down any references that we may have already seen to the
1229 symbol which just became indirect. */
1231 dir->elf_link_hash_flags |=
1232 (ind->elf_link_hash_flags
1233 & (ELF_LINK_HASH_REF_DYNAMIC
1234 | ELF_LINK_HASH_REF_REGULAR
1235 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1236 | ELF_LINK_NON_GOT_REF));
1238 if (ind->root.type != bfd_link_hash_indirect)
1241 /* Copy over the global and procedure linkage table refcount entries.
1242 These may have been already set up by a check_relocs routine. */
1243 tmp = dir->got.refcount;
1246 dir->got.refcount = ind->got.refcount;
1247 ind->got.refcount = tmp;
1250 BFD_ASSERT (ind->got.refcount <= 0);
1252 tmp = dir->plt.refcount;
1255 dir->plt.refcount = ind->plt.refcount;
1256 ind->plt.refcount = tmp;
1259 BFD_ASSERT (ind->plt.refcount <= 0);
1261 if (dir->dynindx == -1)
1263 dir->dynindx = ind->dynindx;
1264 dir->dynstr_index = ind->dynstr_index;
1266 ind->dynstr_index = 0;
1269 BFD_ASSERT (ind->dynindx == -1);
1273 _bfd_elf_link_hash_hide_symbol (info, h)
1274 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1275 struct elf_link_hash_entry *h;
1277 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1278 h->plt.offset = (bfd_vma) -1;
1279 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1283 /* Initialize an ELF linker hash table. */
1286 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1287 struct elf_link_hash_table *table;
1289 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1290 struct bfd_hash_table *,
1295 table->dynamic_sections_created = false;
1296 table->dynobj = NULL;
1297 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
1298 /* The first dynamic symbol is a dummy. */
1299 table->dynsymcount = 1;
1300 table->dynstr = NULL;
1301 table->bucketcount = 0;
1302 table->needed = NULL;
1303 table->runpath = NULL;
1305 table->stab_info = NULL;
1306 table->merge_info = NULL;
1307 table->dynlocal = NULL;
1308 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1309 table->root.type = bfd_link_elf_hash_table;
1314 /* Create an ELF linker hash table. */
1316 struct bfd_link_hash_table *
1317 _bfd_elf_link_hash_table_create (abfd)
1320 struct elf_link_hash_table *ret;
1321 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1323 ret = (struct elf_link_hash_table *) bfd_alloc (abfd, amt);
1324 if (ret == (struct elf_link_hash_table *) NULL)
1327 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1329 bfd_release (abfd, ret);
1336 /* This is a hook for the ELF emulation code in the generic linker to
1337 tell the backend linker what file name to use for the DT_NEEDED
1338 entry for a dynamic object. The generic linker passes name as an
1339 empty string to indicate that no DT_NEEDED entry should be made. */
1342 bfd_elf_set_dt_needed_name (abfd, name)
1346 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1347 && bfd_get_format (abfd) == bfd_object)
1348 elf_dt_name (abfd) = name;
1352 bfd_elf_set_dt_needed_soname (abfd, name)
1356 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1357 && bfd_get_format (abfd) == bfd_object)
1358 elf_dt_soname (abfd) = name;
1361 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1362 the linker ELF emulation code. */
1364 struct bfd_link_needed_list *
1365 bfd_elf_get_needed_list (abfd, info)
1366 bfd *abfd ATTRIBUTE_UNUSED;
1367 struct bfd_link_info *info;
1369 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1371 return elf_hash_table (info)->needed;
1374 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1375 hook for the linker ELF emulation code. */
1377 struct bfd_link_needed_list *
1378 bfd_elf_get_runpath_list (abfd, info)
1379 bfd *abfd ATTRIBUTE_UNUSED;
1380 struct bfd_link_info *info;
1382 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1384 return elf_hash_table (info)->runpath;
1387 /* Get the name actually used for a dynamic object for a link. This
1388 is the SONAME entry if there is one. Otherwise, it is the string
1389 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1392 bfd_elf_get_dt_soname (abfd)
1395 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1396 && bfd_get_format (abfd) == bfd_object)
1397 return elf_dt_name (abfd);
1401 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1402 the ELF linker emulation code. */
1405 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1407 struct bfd_link_needed_list **pneeded;
1410 bfd_byte *dynbuf = NULL;
1412 unsigned long shlink;
1413 bfd_byte *extdyn, *extdynend;
1415 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1419 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1420 || bfd_get_format (abfd) != bfd_object)
1423 s = bfd_get_section_by_name (abfd, ".dynamic");
1424 if (s == NULL || s->_raw_size == 0)
1427 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1431 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1435 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1439 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1441 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1442 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1445 extdynend = extdyn + s->_raw_size;
1446 for (; extdyn < extdynend; extdyn += extdynsize)
1448 Elf_Internal_Dyn dyn;
1450 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1452 if (dyn.d_tag == DT_NULL)
1455 if (dyn.d_tag == DT_NEEDED)
1458 struct bfd_link_needed_list *l;
1459 unsigned int tagv = dyn.d_un.d_val;
1462 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1467 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1488 /* Allocate an ELF string table--force the first byte to be zero. */
1490 struct bfd_strtab_hash *
1491 _bfd_elf_stringtab_init ()
1493 struct bfd_strtab_hash *ret;
1495 ret = _bfd_stringtab_init ();
1500 loc = _bfd_stringtab_add (ret, "", true, false);
1501 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1502 if (loc == (bfd_size_type) -1)
1504 _bfd_stringtab_free (ret);
1511 /* ELF .o/exec file reading */
1513 /* Create a new bfd section from an ELF section header. */
1516 bfd_section_from_shdr (abfd, shindex)
1518 unsigned int shindex;
1520 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1521 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1522 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1525 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1527 switch (hdr->sh_type)
1530 /* Inactive section. Throw it away. */
1533 case SHT_PROGBITS: /* Normal section with contents. */
1534 case SHT_DYNAMIC: /* Dynamic linking information. */
1535 case SHT_NOBITS: /* .bss section. */
1536 case SHT_HASH: /* .hash section. */
1537 case SHT_NOTE: /* .note section. */
1538 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1540 case SHT_SYMTAB: /* A symbol table */
1541 if (elf_onesymtab (abfd) == shindex)
1544 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1545 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1546 elf_onesymtab (abfd) = shindex;
1547 elf_tdata (abfd)->symtab_hdr = *hdr;
1548 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1549 abfd->flags |= HAS_SYMS;
1551 /* Sometimes a shared object will map in the symbol table. If
1552 SHF_ALLOC is set, and this is a shared object, then we also
1553 treat this section as a BFD section. We can not base the
1554 decision purely on SHF_ALLOC, because that flag is sometimes
1555 set in a relocateable object file, which would confuse the
1557 if ((hdr->sh_flags & SHF_ALLOC) != 0
1558 && (abfd->flags & DYNAMIC) != 0
1559 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1564 case SHT_DYNSYM: /* A dynamic symbol table */
1565 if (elf_dynsymtab (abfd) == shindex)
1568 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1569 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1570 elf_dynsymtab (abfd) = shindex;
1571 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1572 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1573 abfd->flags |= HAS_SYMS;
1575 /* Besides being a symbol table, we also treat this as a regular
1576 section, so that objcopy can handle it. */
1577 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1579 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1580 if (elf_symtab_shndx (abfd) == shindex)
1583 /* Get the associated symbol table. */
1584 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1585 || hdr->sh_link != elf_onesymtab (abfd))
1588 elf_symtab_shndx (abfd) = shindex;
1589 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1590 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1593 case SHT_STRTAB: /* A string table */
1594 if (hdr->bfd_section != NULL)
1596 if (ehdr->e_shstrndx == shindex)
1598 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1599 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1603 unsigned int i, num_sec;
1605 num_sec = elf_numsections (abfd);
1606 for (i = 1; i < num_sec; i++)
1608 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1609 if (hdr2->sh_link == shindex)
1611 if (! bfd_section_from_shdr (abfd, i))
1613 if (elf_onesymtab (abfd) == i)
1615 elf_tdata (abfd)->strtab_hdr = *hdr;
1616 elf_elfsections (abfd)[shindex] =
1617 &elf_tdata (abfd)->strtab_hdr;
1620 if (elf_dynsymtab (abfd) == i)
1622 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1623 elf_elfsections (abfd)[shindex] = hdr =
1624 &elf_tdata (abfd)->dynstrtab_hdr;
1625 /* We also treat this as a regular section, so
1626 that objcopy can handle it. */
1629 #if 0 /* Not handling other string tables specially right now. */
1630 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1631 /* We have a strtab for some random other section. */
1632 newsect = (asection *) hdr2->bfd_section;
1635 hdr->bfd_section = newsect;
1636 hdr2 = &elf_section_data (newsect)->str_hdr;
1638 elf_elfsections (abfd)[shindex] = hdr2;
1644 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1648 /* *These* do a lot of work -- but build no sections! */
1650 asection *target_sect;
1651 Elf_Internal_Shdr *hdr2;
1652 unsigned int num_sec = elf_numsections (abfd);
1654 /* Check for a bogus link to avoid crashing. */
1655 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1656 || hdr->sh_link >= num_sec)
1658 ((*_bfd_error_handler)
1659 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1660 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1661 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1664 /* For some incomprehensible reason Oracle distributes
1665 libraries for Solaris in which some of the objects have
1666 bogus sh_link fields. It would be nice if we could just
1667 reject them, but, unfortunately, some people need to use
1668 them. We scan through the section headers; if we find only
1669 one suitable symbol table, we clobber the sh_link to point
1670 to it. I hope this doesn't break anything. */
1671 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1672 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1678 for (scan = 1; scan < num_sec; scan++)
1680 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1681 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1692 hdr->sh_link = found;
1695 /* Get the symbol table. */
1696 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1697 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1700 /* If this reloc section does not use the main symbol table we
1701 don't treat it as a reloc section. BFD can't adequately
1702 represent such a section, so at least for now, we don't
1703 try. We just present it as a normal section. We also
1704 can't use it as a reloc section if it points to the null
1706 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1707 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1709 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1711 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1712 if (target_sect == NULL)
1715 if ((target_sect->flags & SEC_RELOC) == 0
1716 || target_sect->reloc_count == 0)
1717 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1721 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1722 amt = sizeof (*hdr2);
1723 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1724 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1727 elf_elfsections (abfd)[shindex] = hdr2;
1728 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1729 target_sect->flags |= SEC_RELOC;
1730 target_sect->relocation = NULL;
1731 target_sect->rel_filepos = hdr->sh_offset;
1732 /* In the section to which the relocations apply, mark whether
1733 its relocations are of the REL or RELA variety. */
1734 if (hdr->sh_size != 0)
1735 elf_section_data (target_sect)->use_rela_p
1736 = (hdr->sh_type == SHT_RELA);
1737 abfd->flags |= HAS_RELOC;
1742 case SHT_GNU_verdef:
1743 elf_dynverdef (abfd) = shindex;
1744 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1745 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1748 case SHT_GNU_versym:
1749 elf_dynversym (abfd) = shindex;
1750 elf_tdata (abfd)->dynversym_hdr = *hdr;
1751 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1754 case SHT_GNU_verneed:
1755 elf_dynverref (abfd) = shindex;
1756 elf_tdata (abfd)->dynverref_hdr = *hdr;
1757 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1764 /* Make a section for objcopy and relocatable links. */
1765 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1767 if (hdr->contents != NULL)
1769 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1770 unsigned int n_elt = hdr->sh_size / 4;
1773 while (--n_elt != 0)
1774 if ((s = (++idx)->shdr->bfd_section) != NULL
1775 && elf_next_in_group (s) != NULL)
1777 elf_next_in_group (hdr->bfd_section) = s;
1784 /* Check for any processor-specific section types. */
1786 if (bed->elf_backend_section_from_shdr)
1787 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1795 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1796 Return SEC for sections that have no elf section, and NULL on error. */
1799 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
1801 struct sym_sec_cache *cache;
1803 unsigned long r_symndx;
1805 unsigned char esym_shndx[4];
1806 unsigned int isym_shndx;
1807 Elf_Internal_Shdr *symtab_hdr;
1810 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1812 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1813 return cache->sec[ent];
1815 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1816 pos = symtab_hdr->sh_offset;
1817 if (get_elf_backend_data (abfd)->s->sizeof_sym
1818 == sizeof (Elf64_External_Sym))
1820 pos += r_symndx * sizeof (Elf64_External_Sym);
1821 pos += offsetof (Elf64_External_Sym, st_shndx);
1822 amt = sizeof (((Elf64_External_Sym *) 0)->st_shndx);
1826 pos += r_symndx * sizeof (Elf32_External_Sym);
1827 pos += offsetof (Elf32_External_Sym, st_shndx);
1828 amt = sizeof (((Elf32_External_Sym *) 0)->st_shndx);
1830 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1831 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1833 isym_shndx = H_GET_16 (abfd, esym_shndx);
1835 if (isym_shndx == SHN_XINDEX)
1837 Elf_Internal_Shdr *shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1838 if (shndx_hdr->sh_size != 0)
1840 pos = shndx_hdr->sh_offset;
1841 pos += r_symndx * sizeof (Elf_External_Sym_Shndx);
1842 amt = sizeof (Elf_External_Sym_Shndx);
1843 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1844 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1846 isym_shndx = H_GET_32 (abfd, esym_shndx);
1850 if (cache->abfd != abfd)
1852 memset (cache->indx, -1, sizeof (cache->indx));
1855 cache->indx[ent] = r_symndx;
1856 cache->sec[ent] = sec;
1857 if (isym_shndx < SHN_LORESERVE || isym_shndx > SHN_HIRESERVE)
1860 s = bfd_section_from_elf_index (abfd, isym_shndx);
1862 cache->sec[ent] = s;
1864 return cache->sec[ent];
1867 /* Given an ELF section number, retrieve the corresponding BFD
1871 bfd_section_from_elf_index (abfd, index)
1875 if (index >= elf_numsections (abfd))
1877 return elf_elfsections (abfd)[index]->bfd_section;
1881 _bfd_elf_new_section_hook (abfd, sec)
1885 struct bfd_elf_section_data *sdata;
1886 bfd_size_type amt = sizeof (*sdata);
1888 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
1891 sec->used_by_bfd = (PTR) sdata;
1893 /* Indicate whether or not this section should use RELA relocations. */
1895 = get_elf_backend_data (abfd)->default_use_rela_p;
1900 /* Create a new bfd section from an ELF program header.
1902 Since program segments have no names, we generate a synthetic name
1903 of the form segment<NUM>, where NUM is generally the index in the
1904 program header table. For segments that are split (see below) we
1905 generate the names segment<NUM>a and segment<NUM>b.
1907 Note that some program segments may have a file size that is different than
1908 (less than) the memory size. All this means is that at execution the
1909 system must allocate the amount of memory specified by the memory size,
1910 but only initialize it with the first "file size" bytes read from the
1911 file. This would occur for example, with program segments consisting
1912 of combined data+bss.
1914 To handle the above situation, this routine generates TWO bfd sections
1915 for the single program segment. The first has the length specified by
1916 the file size of the segment, and the second has the length specified
1917 by the difference between the two sizes. In effect, the segment is split
1918 into it's initialized and uninitialized parts.
1923 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1925 Elf_Internal_Phdr *hdr;
1927 const char *typename;
1934 split = ((hdr->p_memsz > 0)
1935 && (hdr->p_filesz > 0)
1936 && (hdr->p_memsz > hdr->p_filesz));
1937 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1938 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1941 strcpy (name, namebuf);
1942 newsect = bfd_make_section (abfd, name);
1943 if (newsect == NULL)
1945 newsect->vma = hdr->p_vaddr;
1946 newsect->lma = hdr->p_paddr;
1947 newsect->_raw_size = hdr->p_filesz;
1948 newsect->filepos = hdr->p_offset;
1949 newsect->flags |= SEC_HAS_CONTENTS;
1950 if (hdr->p_type == PT_LOAD)
1952 newsect->flags |= SEC_ALLOC;
1953 newsect->flags |= SEC_LOAD;
1954 if (hdr->p_flags & PF_X)
1956 /* FIXME: all we known is that it has execute PERMISSION,
1958 newsect->flags |= SEC_CODE;
1961 if (!(hdr->p_flags & PF_W))
1963 newsect->flags |= SEC_READONLY;
1968 sprintf (namebuf, "%s%db", typename, index);
1969 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1972 strcpy (name, namebuf);
1973 newsect = bfd_make_section (abfd, name);
1974 if (newsect == NULL)
1976 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1977 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1978 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1979 if (hdr->p_type == PT_LOAD)
1981 newsect->flags |= SEC_ALLOC;
1982 if (hdr->p_flags & PF_X)
1983 newsect->flags |= SEC_CODE;
1985 if (!(hdr->p_flags & PF_W))
1986 newsect->flags |= SEC_READONLY;
1993 bfd_section_from_phdr (abfd, hdr, index)
1995 Elf_Internal_Phdr *hdr;
1998 struct elf_backend_data *bed;
2000 switch (hdr->p_type)
2003 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2006 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2009 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2012 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2015 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2017 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2022 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2025 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2028 /* Check for any processor-specific program segment types.
2029 If no handler for them, default to making "segment" sections. */
2030 bed = get_elf_backend_data (abfd);
2031 if (bed->elf_backend_section_from_phdr)
2032 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2034 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2038 /* Initialize REL_HDR, the section-header for new section, containing
2039 relocations against ASECT. If USE_RELA_P is true, we use RELA
2040 relocations; otherwise, we use REL relocations. */
2043 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2045 Elf_Internal_Shdr *rel_hdr;
2050 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2051 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2053 name = bfd_alloc (abfd, amt);
2056 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2058 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2060 if (rel_hdr->sh_name == (unsigned int) -1)
2062 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2063 rel_hdr->sh_entsize = (use_rela_p
2064 ? bed->s->sizeof_rela
2065 : bed->s->sizeof_rel);
2066 rel_hdr->sh_addralign = bed->s->file_align;
2067 rel_hdr->sh_flags = 0;
2068 rel_hdr->sh_addr = 0;
2069 rel_hdr->sh_size = 0;
2070 rel_hdr->sh_offset = 0;
2075 /* Set up an ELF internal section header for a section. */
2078 elf_fake_sections (abfd, asect, failedptrarg)
2083 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2084 boolean *failedptr = (boolean *) failedptrarg;
2085 Elf_Internal_Shdr *this_hdr;
2089 /* We already failed; just get out of the bfd_map_over_sections
2094 this_hdr = &elf_section_data (asect)->this_hdr;
2096 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2097 asect->name, false);
2098 if (this_hdr->sh_name == (unsigned long) -1)
2104 this_hdr->sh_flags = 0;
2106 if ((asect->flags & SEC_ALLOC) != 0
2107 || asect->user_set_vma)
2108 this_hdr->sh_addr = asect->vma;
2110 this_hdr->sh_addr = 0;
2112 this_hdr->sh_offset = 0;
2113 this_hdr->sh_size = asect->_raw_size;
2114 this_hdr->sh_link = 0;
2115 this_hdr->sh_addralign = 1 << asect->alignment_power;
2116 /* The sh_entsize and sh_info fields may have been set already by
2117 copy_private_section_data. */
2119 this_hdr->bfd_section = asect;
2120 this_hdr->contents = NULL;
2122 /* FIXME: This should not be based on section names. */
2123 if (strcmp (asect->name, ".dynstr") == 0)
2124 this_hdr->sh_type = SHT_STRTAB;
2125 else if (strcmp (asect->name, ".hash") == 0)
2127 this_hdr->sh_type = SHT_HASH;
2128 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2130 else if (strcmp (asect->name, ".dynsym") == 0)
2132 this_hdr->sh_type = SHT_DYNSYM;
2133 this_hdr->sh_entsize = bed->s->sizeof_sym;
2135 else if (strcmp (asect->name, ".dynamic") == 0)
2137 this_hdr->sh_type = SHT_DYNAMIC;
2138 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2140 else if (strncmp (asect->name, ".rela", 5) == 0
2141 && get_elf_backend_data (abfd)->may_use_rela_p)
2143 this_hdr->sh_type = SHT_RELA;
2144 this_hdr->sh_entsize = bed->s->sizeof_rela;
2146 else if (strncmp (asect->name, ".rel", 4) == 0
2147 && get_elf_backend_data (abfd)->may_use_rel_p)
2149 this_hdr->sh_type = SHT_REL;
2150 this_hdr->sh_entsize = bed->s->sizeof_rel;
2152 else if (strncmp (asect->name, ".note", 5) == 0)
2153 this_hdr->sh_type = SHT_NOTE;
2154 else if (strncmp (asect->name, ".stab", 5) == 0
2155 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2156 this_hdr->sh_type = SHT_STRTAB;
2157 else if (strcmp (asect->name, ".gnu.version") == 0)
2159 this_hdr->sh_type = SHT_GNU_versym;
2160 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2162 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2164 this_hdr->sh_type = SHT_GNU_verdef;
2165 this_hdr->sh_entsize = 0;
2166 /* objcopy or strip will copy over sh_info, but may not set
2167 cverdefs. The linker will set cverdefs, but sh_info will be
2169 if (this_hdr->sh_info == 0)
2170 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2172 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2173 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2175 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2177 this_hdr->sh_type = SHT_GNU_verneed;
2178 this_hdr->sh_entsize = 0;
2179 /* objcopy or strip will copy over sh_info, but may not set
2180 cverrefs. The linker will set cverrefs, but sh_info will be
2182 if (this_hdr->sh_info == 0)
2183 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2185 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2186 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2188 else if ((asect->flags & SEC_GROUP) != 0)
2190 this_hdr->sh_type = SHT_GROUP;
2191 this_hdr->sh_entsize = 4;
2193 else if ((asect->flags & SEC_ALLOC) != 0
2194 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
2195 this_hdr->sh_type = SHT_NOBITS;
2197 this_hdr->sh_type = SHT_PROGBITS;
2199 if ((asect->flags & SEC_ALLOC) != 0)
2200 this_hdr->sh_flags |= SHF_ALLOC;
2201 if ((asect->flags & SEC_READONLY) == 0)
2202 this_hdr->sh_flags |= SHF_WRITE;
2203 if ((asect->flags & SEC_CODE) != 0)
2204 this_hdr->sh_flags |= SHF_EXECINSTR;
2205 if ((asect->flags & SEC_MERGE) != 0)
2207 this_hdr->sh_flags |= SHF_MERGE;
2208 this_hdr->sh_entsize = asect->entsize;
2209 if ((asect->flags & SEC_STRINGS) != 0)
2210 this_hdr->sh_flags |= SHF_STRINGS;
2212 if (elf_group_name (asect) != NULL)
2213 this_hdr->sh_flags |= SHF_GROUP;
2215 /* Check for processor-specific section types. */
2216 if (bed->elf_backend_fake_sections)
2217 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
2219 /* If the section has relocs, set up a section header for the
2220 SHT_REL[A] section. If two relocation sections are required for
2221 this section, it is up to the processor-specific back-end to
2222 create the other. */
2223 if ((asect->flags & SEC_RELOC) != 0
2224 && !_bfd_elf_init_reloc_shdr (abfd,
2225 &elf_section_data (asect)->rel_hdr,
2227 elf_section_data (asect)->use_rela_p))
2231 /* Fill in the contents of a SHT_GROUP section. */
2234 set_group_contents (abfd, sec, failedptrarg)
2237 PTR failedptrarg ATTRIBUTE_UNUSED;
2239 boolean *failedptr = (boolean *) failedptrarg;
2240 unsigned long symindx;
2243 struct bfd_link_order *l;
2245 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2249 /* If called from the assembler, swap_out_syms will have set up
2250 elf_section_syms; If called for "ld -r", the symbols won't yet
2251 be mapped, so emulate elf_bfd_final_link. */
2252 if (elf_section_syms (abfd) != NULL)
2253 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2255 symindx = elf_section_data (sec)->this_idx;
2256 elf_section_data (sec)->this_hdr.sh_info = symindx;
2258 /* Nor will the contents be allocated for "ld -r". */
2259 if (sec->contents == NULL)
2261 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2262 if (sec->contents == NULL)
2269 loc = sec->contents + sec->_raw_size;
2271 /* Get the pointer to the first section in the group that we
2272 squirreled away here. */
2273 elt = elf_next_in_group (sec);
2275 /* First element is a flag word. Rest of section is elf section
2276 indices for all the sections of the group. Write them backwards
2277 just to keep the group in the same order as given in .section
2278 directives, not that it matters. */
2282 H_PUT_32 (abfd, elf_section_data (elt)->this_idx, loc);
2283 elt = elf_next_in_group (elt);
2286 /* If this is a relocatable link, then the above did nothing because
2287 SEC is the output section. Look through the input sections
2289 for (l = sec->link_order_head; l != NULL; l = l->next)
2290 if (l->type == bfd_indirect_link_order
2291 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2296 elf_section_data (elt->output_section)->this_idx, loc);
2297 elt = elf_next_in_group (elt);
2298 /* During a relocatable link, the lists are circular. */
2300 while (elt != elf_next_in_group (l->u.indirect.section));
2303 H_PUT_32 (abfd, 0, loc);
2305 BFD_ASSERT (loc == sec->contents);
2308 /* Assign all ELF section numbers. The dummy first section is handled here
2309 too. The link/info pointers for the standard section types are filled
2310 in here too, while we're at it. */
2313 assign_section_numbers (abfd)
2316 struct elf_obj_tdata *t = elf_tdata (abfd);
2318 unsigned int section_number, secn;
2319 Elf_Internal_Shdr **i_shdrp;
2324 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2326 for (sec = abfd->sections; sec; sec = sec->next)
2328 struct bfd_elf_section_data *d = elf_section_data (sec);
2330 if (section_number == SHN_LORESERVE)
2331 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2332 d->this_idx = section_number++;
2333 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2334 if ((sec->flags & SEC_RELOC) == 0)
2338 if (section_number == SHN_LORESERVE)
2339 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2340 d->rel_idx = section_number++;
2341 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2346 if (section_number == SHN_LORESERVE)
2347 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2348 d->rel_idx2 = section_number++;
2349 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2355 if (section_number == SHN_LORESERVE)
2356 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2357 t->shstrtab_section = section_number++;
2358 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2359 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2361 if (bfd_get_symcount (abfd) > 0)
2363 if (section_number == SHN_LORESERVE)
2364 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2365 t->symtab_section = section_number++;
2366 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2367 if (section_number > SHN_LORESERVE - 2)
2369 if (section_number == SHN_LORESERVE)
2370 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2371 t->symtab_shndx_section = section_number++;
2372 t->symtab_shndx_hdr.sh_name
2373 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2374 ".symtab_shndx", false);
2375 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2378 if (section_number == SHN_LORESERVE)
2379 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2380 t->strtab_section = section_number++;
2381 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2384 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2385 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2387 elf_numsections (abfd) = section_number;
2388 elf_elfheader (abfd)->e_shnum = section_number;
2389 if (section_number > SHN_LORESERVE)
2390 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2392 /* Set up the list of section header pointers, in agreement with the
2394 amt = section_number * sizeof (Elf_Internal_Shdr *);
2395 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
2396 if (i_shdrp == NULL)
2399 amt = sizeof (Elf_Internal_Shdr);
2400 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2401 if (i_shdrp[0] == NULL)
2403 bfd_release (abfd, i_shdrp);
2406 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2408 elf_elfsections (abfd) = i_shdrp;
2410 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2411 if (bfd_get_symcount (abfd) > 0)
2413 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2414 if (elf_numsections (abfd) > SHN_LORESERVE)
2416 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2417 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2419 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2420 t->symtab_hdr.sh_link = t->strtab_section;
2422 for (sec = abfd->sections; sec; sec = sec->next)
2424 struct bfd_elf_section_data *d = elf_section_data (sec);
2428 i_shdrp[d->this_idx] = &d->this_hdr;
2429 if (d->rel_idx != 0)
2430 i_shdrp[d->rel_idx] = &d->rel_hdr;
2431 if (d->rel_idx2 != 0)
2432 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2434 /* Fill in the sh_link and sh_info fields while we're at it. */
2436 /* sh_link of a reloc section is the section index of the symbol
2437 table. sh_info is the section index of the section to which
2438 the relocation entries apply. */
2439 if (d->rel_idx != 0)
2441 d->rel_hdr.sh_link = t->symtab_section;
2442 d->rel_hdr.sh_info = d->this_idx;
2444 if (d->rel_idx2 != 0)
2446 d->rel_hdr2->sh_link = t->symtab_section;
2447 d->rel_hdr2->sh_info = d->this_idx;
2450 switch (d->this_hdr.sh_type)
2454 /* A reloc section which we are treating as a normal BFD
2455 section. sh_link is the section index of the symbol
2456 table. sh_info is the section index of the section to
2457 which the relocation entries apply. We assume that an
2458 allocated reloc section uses the dynamic symbol table.
2459 FIXME: How can we be sure? */
2460 s = bfd_get_section_by_name (abfd, ".dynsym");
2462 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2464 /* We look up the section the relocs apply to by name. */
2466 if (d->this_hdr.sh_type == SHT_REL)
2470 s = bfd_get_section_by_name (abfd, name);
2472 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2476 /* We assume that a section named .stab*str is a stabs
2477 string section. We look for a section with the same name
2478 but without the trailing ``str'', and set its sh_link
2479 field to point to this section. */
2480 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2481 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2486 len = strlen (sec->name);
2487 alc = (char *) bfd_malloc ((bfd_size_type) len - 2);
2490 strncpy (alc, sec->name, len - 3);
2491 alc[len - 3] = '\0';
2492 s = bfd_get_section_by_name (abfd, alc);
2496 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2498 /* This is a .stab section. */
2499 elf_section_data (s)->this_hdr.sh_entsize =
2500 4 + 2 * bfd_get_arch_size (abfd) / 8;
2507 case SHT_GNU_verneed:
2508 case SHT_GNU_verdef:
2509 /* sh_link is the section header index of the string table
2510 used for the dynamic entries, or the symbol table, or the
2512 s = bfd_get_section_by_name (abfd, ".dynstr");
2514 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2518 case SHT_GNU_versym:
2519 /* sh_link is the section header index of the symbol table
2520 this hash table or version table is for. */
2521 s = bfd_get_section_by_name (abfd, ".dynsym");
2523 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2527 d->this_hdr.sh_link = t->symtab_section;
2531 for (secn = 1; secn < section_number; ++secn)
2532 if (i_shdrp[secn] == NULL)
2533 i_shdrp[secn] = i_shdrp[0];
2535 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2536 i_shdrp[secn]->sh_name);
2540 /* Map symbol from it's internal number to the external number, moving
2541 all local symbols to be at the head of the list. */
2544 sym_is_global (abfd, sym)
2548 /* If the backend has a special mapping, use it. */
2549 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2550 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2553 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2554 || bfd_is_und_section (bfd_get_section (sym))
2555 || bfd_is_com_section (bfd_get_section (sym)));
2559 elf_map_symbols (abfd)
2562 unsigned int symcount = bfd_get_symcount (abfd);
2563 asymbol **syms = bfd_get_outsymbols (abfd);
2564 asymbol **sect_syms;
2565 unsigned int num_locals = 0;
2566 unsigned int num_globals = 0;
2567 unsigned int num_locals2 = 0;
2568 unsigned int num_globals2 = 0;
2576 fprintf (stderr, "elf_map_symbols\n");
2580 for (asect = abfd->sections; asect; asect = asect->next)
2582 if (max_index < asect->index)
2583 max_index = asect->index;
2587 amt = max_index * sizeof (asymbol *);
2588 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2589 if (sect_syms == NULL)
2591 elf_section_syms (abfd) = sect_syms;
2592 elf_num_section_syms (abfd) = max_index;
2594 /* Init sect_syms entries for any section symbols we have already
2595 decided to output. */
2596 for (idx = 0; idx < symcount; idx++)
2598 asymbol *sym = syms[idx];
2600 if ((sym->flags & BSF_SECTION_SYM) != 0
2607 if (sec->owner != NULL)
2609 if (sec->owner != abfd)
2611 if (sec->output_offset != 0)
2614 sec = sec->output_section;
2616 /* Empty sections in the input files may have had a
2617 section symbol created for them. (See the comment
2618 near the end of _bfd_generic_link_output_symbols in
2619 linker.c). If the linker script discards such
2620 sections then we will reach this point. Since we know
2621 that we cannot avoid this case, we detect it and skip
2622 the abort and the assignment to the sect_syms array.
2623 To reproduce this particular case try running the
2624 linker testsuite test ld-scripts/weak.exp for an ELF
2625 port that uses the generic linker. */
2626 if (sec->owner == NULL)
2629 BFD_ASSERT (sec->owner == abfd);
2631 sect_syms[sec->index] = syms[idx];
2636 /* Classify all of the symbols. */
2637 for (idx = 0; idx < symcount; idx++)
2639 if (!sym_is_global (abfd, syms[idx]))
2645 /* We will be adding a section symbol for each BFD section. Most normal
2646 sections will already have a section symbol in outsymbols, but
2647 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2648 at least in that case. */
2649 for (asect = abfd->sections; asect; asect = asect->next)
2651 if (sect_syms[asect->index] == NULL)
2653 if (!sym_is_global (abfd, asect->symbol))
2660 /* Now sort the symbols so the local symbols are first. */
2661 amt = (num_locals + num_globals) * sizeof (asymbol *);
2662 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2664 if (new_syms == NULL)
2667 for (idx = 0; idx < symcount; idx++)
2669 asymbol *sym = syms[idx];
2672 if (!sym_is_global (abfd, sym))
2675 i = num_locals + num_globals2++;
2677 sym->udata.i = i + 1;
2679 for (asect = abfd->sections; asect; asect = asect->next)
2681 if (sect_syms[asect->index] == NULL)
2683 asymbol *sym = asect->symbol;
2686 sect_syms[asect->index] = sym;
2687 if (!sym_is_global (abfd, sym))
2690 i = num_locals + num_globals2++;
2692 sym->udata.i = i + 1;
2696 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2698 elf_num_locals (abfd) = num_locals;
2699 elf_num_globals (abfd) = num_globals;
2703 /* Align to the maximum file alignment that could be required for any
2704 ELF data structure. */
2706 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2707 static INLINE file_ptr
2708 align_file_position (off, align)
2712 return (off + align - 1) & ~(align - 1);
2715 /* Assign a file position to a section, optionally aligning to the
2716 required section alignment. */
2719 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2720 Elf_Internal_Shdr *i_shdrp;
2728 al = i_shdrp->sh_addralign;
2730 offset = BFD_ALIGN (offset, al);
2732 i_shdrp->sh_offset = offset;
2733 if (i_shdrp->bfd_section != NULL)
2734 i_shdrp->bfd_section->filepos = offset;
2735 if (i_shdrp->sh_type != SHT_NOBITS)
2736 offset += i_shdrp->sh_size;
2740 /* Compute the file positions we are going to put the sections at, and
2741 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2742 is not NULL, this is being called by the ELF backend linker. */
2745 _bfd_elf_compute_section_file_positions (abfd, link_info)
2747 struct bfd_link_info *link_info;
2749 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2751 struct bfd_strtab_hash *strtab;
2752 Elf_Internal_Shdr *shstrtab_hdr;
2754 if (abfd->output_has_begun)
2757 /* Do any elf backend specific processing first. */
2758 if (bed->elf_backend_begin_write_processing)
2759 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2761 if (! prep_headers (abfd))
2764 /* Post process the headers if necessary. */
2765 if (bed->elf_backend_post_process_headers)
2766 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2769 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2773 if (!assign_section_numbers (abfd))
2776 /* The backend linker builds symbol table information itself. */
2777 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2779 /* Non-zero if doing a relocatable link. */
2780 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2782 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2786 if (link_info == NULL || link_info->relocateable)
2788 bfd_map_over_sections (abfd, set_group_contents, &failed);
2793 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2794 /* sh_name was set in prep_headers. */
2795 shstrtab_hdr->sh_type = SHT_STRTAB;
2796 shstrtab_hdr->sh_flags = 0;
2797 shstrtab_hdr->sh_addr = 0;
2798 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2799 shstrtab_hdr->sh_entsize = 0;
2800 shstrtab_hdr->sh_link = 0;
2801 shstrtab_hdr->sh_info = 0;
2802 /* sh_offset is set in assign_file_positions_except_relocs. */
2803 shstrtab_hdr->sh_addralign = 1;
2805 if (!assign_file_positions_except_relocs (abfd))
2808 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2811 Elf_Internal_Shdr *hdr;
2813 off = elf_tdata (abfd)->next_file_pos;
2815 hdr = &elf_tdata (abfd)->symtab_hdr;
2816 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2818 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2819 if (hdr->sh_size != 0)
2820 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2822 hdr = &elf_tdata (abfd)->strtab_hdr;
2823 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2825 elf_tdata (abfd)->next_file_pos = off;
2827 /* Now that we know where the .strtab section goes, write it
2829 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2830 || ! _bfd_stringtab_emit (abfd, strtab))
2832 _bfd_stringtab_free (strtab);
2835 abfd->output_has_begun = true;
2840 /* Create a mapping from a set of sections to a program segment. */
2842 static INLINE struct elf_segment_map *
2843 make_mapping (abfd, sections, from, to, phdr)
2845 asection **sections;
2850 struct elf_segment_map *m;
2855 amt = sizeof (struct elf_segment_map);
2856 amt += (to - from - 1) * sizeof (asection *);
2857 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2861 m->p_type = PT_LOAD;
2862 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2863 m->sections[i - from] = *hdrpp;
2864 m->count = to - from;
2866 if (from == 0 && phdr)
2868 /* Include the headers in the first PT_LOAD segment. */
2869 m->includes_filehdr = 1;
2870 m->includes_phdrs = 1;
2876 /* Set up a mapping from BFD sections to program segments. */
2879 map_sections_to_segments (abfd)
2882 asection **sections = NULL;
2886 struct elf_segment_map *mfirst;
2887 struct elf_segment_map **pm;
2888 struct elf_segment_map *m;
2890 unsigned int phdr_index;
2891 bfd_vma maxpagesize;
2893 boolean phdr_in_segment = true;
2895 asection *dynsec, *eh_frame_hdr;
2898 if (elf_tdata (abfd)->segment_map != NULL)
2901 if (bfd_count_sections (abfd) == 0)
2904 /* Select the allocated sections, and sort them. */
2906 amt = bfd_count_sections (abfd) * sizeof (asection *);
2907 sections = (asection **) bfd_malloc (amt);
2908 if (sections == NULL)
2912 for (s = abfd->sections; s != NULL; s = s->next)
2914 if ((s->flags & SEC_ALLOC) != 0)
2920 BFD_ASSERT (i <= bfd_count_sections (abfd));
2923 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2925 /* Build the mapping. */
2930 /* If we have a .interp section, then create a PT_PHDR segment for
2931 the program headers and a PT_INTERP segment for the .interp
2933 s = bfd_get_section_by_name (abfd, ".interp");
2934 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2936 amt = sizeof (struct elf_segment_map);
2937 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2941 m->p_type = PT_PHDR;
2942 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2943 m->p_flags = PF_R | PF_X;
2944 m->p_flags_valid = 1;
2945 m->includes_phdrs = 1;
2950 amt = sizeof (struct elf_segment_map);
2951 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2955 m->p_type = PT_INTERP;
2963 /* Look through the sections. We put sections in the same program
2964 segment when the start of the second section can be placed within
2965 a few bytes of the end of the first section. */
2968 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2970 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2972 && (dynsec->flags & SEC_LOAD) == 0)
2975 /* Deal with -Ttext or something similar such that the first section
2976 is not adjacent to the program headers. This is an
2977 approximation, since at this point we don't know exactly how many
2978 program headers we will need. */
2981 bfd_size_type phdr_size;
2983 phdr_size = elf_tdata (abfd)->program_header_size;
2985 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2986 if ((abfd->flags & D_PAGED) == 0
2987 || sections[0]->lma < phdr_size
2988 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2989 phdr_in_segment = false;
2992 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2995 boolean new_segment;
2999 /* See if this section and the last one will fit in the same
3002 if (last_hdr == NULL)
3004 /* If we don't have a segment yet, then we don't need a new
3005 one (we build the last one after this loop). */
3006 new_segment = false;
3008 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3010 /* If this section has a different relation between the
3011 virtual address and the load address, then we need a new
3015 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3016 < BFD_ALIGN (hdr->lma, maxpagesize))
3018 /* If putting this section in this segment would force us to
3019 skip a page in the segment, then we need a new segment. */
3022 else if ((last_hdr->flags & SEC_LOAD) == 0
3023 && (hdr->flags & SEC_LOAD) != 0)
3025 /* We don't want to put a loadable section after a
3026 nonloadable section in the same segment. */
3029 else if ((abfd->flags & D_PAGED) == 0)
3031 /* If the file is not demand paged, which means that we
3032 don't require the sections to be correctly aligned in the
3033 file, then there is no other reason for a new segment. */
3034 new_segment = false;
3037 && (hdr->flags & SEC_READONLY) == 0
3038 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3041 /* We don't want to put a writable section in a read only
3042 segment, unless they are on the same page in memory
3043 anyhow. We already know that the last section does not
3044 bring us past the current section on the page, so the
3045 only case in which the new section is not on the same
3046 page as the previous section is when the previous section
3047 ends precisely on a page boundary. */
3052 /* Otherwise, we can use the same segment. */
3053 new_segment = false;
3058 if ((hdr->flags & SEC_READONLY) == 0)
3064 /* We need a new program segment. We must create a new program
3065 header holding all the sections from phdr_index until hdr. */
3067 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3074 if ((hdr->flags & SEC_READONLY) == 0)
3081 phdr_in_segment = false;
3084 /* Create a final PT_LOAD program segment. */
3085 if (last_hdr != NULL)
3087 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3095 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3098 amt = sizeof (struct elf_segment_map);
3099 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3103 m->p_type = PT_DYNAMIC;
3105 m->sections[0] = dynsec;
3111 /* For each loadable .note section, add a PT_NOTE segment. We don't
3112 use bfd_get_section_by_name, because if we link together
3113 nonloadable .note sections and loadable .note sections, we will
3114 generate two .note sections in the output file. FIXME: Using
3115 names for section types is bogus anyhow. */
3116 for (s = abfd->sections; s != NULL; s = s->next)
3118 if ((s->flags & SEC_LOAD) != 0
3119 && strncmp (s->name, ".note", 5) == 0)
3121 amt = sizeof (struct elf_segment_map);
3122 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3126 m->p_type = PT_NOTE;
3135 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3137 eh_frame_hdr = NULL;
3138 if (elf_tdata (abfd)->eh_frame_hdr)
3139 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
3140 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3142 amt = sizeof (struct elf_segment_map);
3143 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3147 m->p_type = PT_GNU_EH_FRAME;
3149 m->sections[0] = eh_frame_hdr;
3158 elf_tdata (abfd)->segment_map = mfirst;
3162 if (sections != NULL)
3167 /* Sort sections by address. */
3170 elf_sort_sections (arg1, arg2)
3174 const asection *sec1 = *(const asection **) arg1;
3175 const asection *sec2 = *(const asection **) arg2;
3177 /* Sort by LMA first, since this is the address used to
3178 place the section into a segment. */
3179 if (sec1->lma < sec2->lma)
3181 else if (sec1->lma > sec2->lma)
3184 /* Then sort by VMA. Normally the LMA and the VMA will be
3185 the same, and this will do nothing. */
3186 if (sec1->vma < sec2->vma)
3188 else if (sec1->vma > sec2->vma)
3191 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3193 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3199 /* If the indicies are the same, do not return 0
3200 here, but continue to try the next comparison. */
3201 if (sec1->target_index - sec2->target_index != 0)
3202 return sec1->target_index - sec2->target_index;
3207 else if (TOEND (sec2))
3212 /* Sort by size, to put zero sized sections
3213 before others at the same address. */
3215 if (sec1->_raw_size < sec2->_raw_size)
3217 if (sec1->_raw_size > sec2->_raw_size)
3220 return sec1->target_index - sec2->target_index;
3223 /* Assign file positions to the sections based on the mapping from
3224 sections to segments. This function also sets up some fields in
3225 the file header, and writes out the program headers. */
3228 assign_file_positions_for_segments (abfd)
3231 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3233 struct elf_segment_map *m;
3235 Elf_Internal_Phdr *phdrs;
3237 bfd_vma filehdr_vaddr, filehdr_paddr;
3238 bfd_vma phdrs_vaddr, phdrs_paddr;
3239 Elf_Internal_Phdr *p;
3242 if (elf_tdata (abfd)->segment_map == NULL)
3244 if (! map_sections_to_segments (abfd))
3248 if (bed->elf_backend_modify_segment_map)
3250 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3255 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3258 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3259 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3260 elf_elfheader (abfd)->e_phnum = count;
3265 /* If we already counted the number of program segments, make sure
3266 that we allocated enough space. This happens when SIZEOF_HEADERS
3267 is used in a linker script. */
3268 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3269 if (alloc != 0 && count > alloc)
3271 ((*_bfd_error_handler)
3272 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3273 bfd_get_filename (abfd), alloc, count));
3274 bfd_set_error (bfd_error_bad_value);
3281 amt = alloc * sizeof (Elf_Internal_Phdr);
3282 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3286 off = bed->s->sizeof_ehdr;
3287 off += alloc * bed->s->sizeof_phdr;
3294 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3301 /* If elf_segment_map is not from map_sections_to_segments, the
3302 sections may not be correctly ordered. NOTE: sorting should
3303 not be done to the PT_NOTE section of a corefile, which may
3304 contain several pseudo-sections artificially created by bfd.
3305 Sorting these pseudo-sections breaks things badly. */
3307 && !(elf_elfheader (abfd)->e_type == ET_CORE
3308 && m->p_type == PT_NOTE))
3309 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3312 p->p_type = m->p_type;
3313 p->p_flags = m->p_flags;
3315 if (p->p_type == PT_LOAD
3317 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3319 if ((abfd->flags & D_PAGED) != 0)
3320 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3323 bfd_size_type align;
3326 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3328 bfd_size_type secalign;
3330 secalign = bfd_get_section_alignment (abfd, *secpp);
3331 if (secalign > align)
3335 off += (m->sections[0]->vma - off) % (1 << align);
3342 p->p_vaddr = m->sections[0]->vma;
3344 if (m->p_paddr_valid)
3345 p->p_paddr = m->p_paddr;
3346 else if (m->count == 0)
3349 p->p_paddr = m->sections[0]->lma;
3351 if (p->p_type == PT_LOAD
3352 && (abfd->flags & D_PAGED) != 0)
3353 p->p_align = bed->maxpagesize;
3354 else if (m->count == 0)
3355 p->p_align = bed->s->file_align;
3363 if (m->includes_filehdr)
3365 if (! m->p_flags_valid)
3368 p->p_filesz = bed->s->sizeof_ehdr;
3369 p->p_memsz = bed->s->sizeof_ehdr;
3372 BFD_ASSERT (p->p_type == PT_LOAD);
3374 if (p->p_vaddr < (bfd_vma) off)
3376 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3377 bfd_get_filename (abfd));
3378 bfd_set_error (bfd_error_bad_value);
3383 if (! m->p_paddr_valid)
3386 if (p->p_type == PT_LOAD)
3388 filehdr_vaddr = p->p_vaddr;
3389 filehdr_paddr = p->p_paddr;
3393 if (m->includes_phdrs)
3395 if (! m->p_flags_valid)
3398 if (m->includes_filehdr)
3400 if (p->p_type == PT_LOAD)
3402 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3403 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3408 p->p_offset = bed->s->sizeof_ehdr;
3412 BFD_ASSERT (p->p_type == PT_LOAD);
3413 p->p_vaddr -= off - p->p_offset;
3414 if (! m->p_paddr_valid)
3415 p->p_paddr -= off - p->p_offset;
3418 if (p->p_type == PT_LOAD)
3420 phdrs_vaddr = p->p_vaddr;
3421 phdrs_paddr = p->p_paddr;
3424 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3427 p->p_filesz += alloc * bed->s->sizeof_phdr;
3428 p->p_memsz += alloc * bed->s->sizeof_phdr;
3431 if (p->p_type == PT_LOAD
3432 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3434 if (! m->includes_filehdr && ! m->includes_phdrs)
3440 adjust = off - (p->p_offset + p->p_filesz);
3441 p->p_filesz += adjust;
3442 p->p_memsz += adjust;
3448 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3452 bfd_size_type align;
3456 align = 1 << bfd_get_section_alignment (abfd, sec);
3458 /* The section may have artificial alignment forced by a
3459 link script. Notice this case by the gap between the
3460 cumulative phdr lma and the section's lma. */
3461 if (p->p_paddr + p->p_memsz < sec->lma)
3463 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3465 p->p_memsz += adjust;
3468 if ((flags & SEC_LOAD) != 0)
3469 p->p_filesz += adjust;
3472 if (p->p_type == PT_LOAD)
3474 bfd_signed_vma adjust;
3476 if ((flags & SEC_LOAD) != 0)
3478 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3482 else if ((flags & SEC_ALLOC) != 0)
3484 /* The section VMA must equal the file position
3485 modulo the page size. FIXME: I'm not sure if
3486 this adjustment is really necessary. We used to
3487 not have the SEC_LOAD case just above, and then
3488 this was necessary, but now I'm not sure. */
3489 if ((abfd->flags & D_PAGED) != 0)
3490 adjust = (sec->vma - voff) % bed->maxpagesize;
3492 adjust = (sec->vma - voff) % align;
3501 (* _bfd_error_handler) (_("\
3502 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3503 bfd_section_name (abfd, sec),
3508 p->p_memsz += adjust;
3511 if ((flags & SEC_LOAD) != 0)
3512 p->p_filesz += adjust;
3517 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3518 used in a linker script we may have a section with
3519 SEC_LOAD clear but which is supposed to have
3521 if ((flags & SEC_LOAD) != 0
3522 || (flags & SEC_HAS_CONTENTS) != 0)
3523 off += sec->_raw_size;
3525 if ((flags & SEC_ALLOC) != 0)
3526 voff += sec->_raw_size;
3529 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3531 /* The actual "note" segment has i == 0.
3532 This is the one that actually contains everything. */
3536 p->p_filesz = sec->_raw_size;
3537 off += sec->_raw_size;
3542 /* Fake sections -- don't need to be written. */
3545 flags = sec->flags = 0;
3552 p->p_memsz += sec->_raw_size;
3554 if ((flags & SEC_LOAD) != 0)
3555 p->p_filesz += sec->_raw_size;
3557 if (align > p->p_align
3558 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3562 if (! m->p_flags_valid)
3565 if ((flags & SEC_CODE) != 0)
3567 if ((flags & SEC_READONLY) == 0)
3573 /* Now that we have set the section file positions, we can set up
3574 the file positions for the non PT_LOAD segments. */
3575 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3579 if (p->p_type != PT_LOAD && m->count > 0)
3581 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3582 p->p_offset = m->sections[0]->filepos;
3586 if (m->includes_filehdr)
3588 p->p_vaddr = filehdr_vaddr;
3589 if (! m->p_paddr_valid)
3590 p->p_paddr = filehdr_paddr;
3592 else if (m->includes_phdrs)
3594 p->p_vaddr = phdrs_vaddr;
3595 if (! m->p_paddr_valid)
3596 p->p_paddr = phdrs_paddr;
3601 /* Clear out any program headers we allocated but did not use. */
3602 for (; count < alloc; count++, p++)
3604 memset (p, 0, sizeof *p);
3605 p->p_type = PT_NULL;
3608 elf_tdata (abfd)->phdr = phdrs;
3610 elf_tdata (abfd)->next_file_pos = off;
3612 /* Write out the program headers. */
3613 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
3614 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3620 /* Get the size of the program header.
3622 If this is called by the linker before any of the section VMA's are set, it
3623 can't calculate the correct value for a strange memory layout. This only
3624 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3625 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3626 data segment (exclusive of .interp and .dynamic).
3628 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3629 will be two segments. */
3631 static bfd_size_type
3632 get_program_header_size (abfd)
3637 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3639 /* We can't return a different result each time we're called. */
3640 if (elf_tdata (abfd)->program_header_size != 0)
3641 return elf_tdata (abfd)->program_header_size;
3643 if (elf_tdata (abfd)->segment_map != NULL)
3645 struct elf_segment_map *m;
3648 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3650 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3651 return elf_tdata (abfd)->program_header_size;
3654 /* Assume we will need exactly two PT_LOAD segments: one for text
3655 and one for data. */
3658 s = bfd_get_section_by_name (abfd, ".interp");
3659 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3661 /* If we have a loadable interpreter section, we need a
3662 PT_INTERP segment. In this case, assume we also need a
3663 PT_PHDR segment, although that may not be true for all
3668 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3670 /* We need a PT_DYNAMIC segment. */
3674 if (elf_tdata (abfd)->eh_frame_hdr
3675 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
3677 /* We need a PT_GNU_EH_FRAME segment. */
3681 for (s = abfd->sections; s != NULL; s = s->next)
3683 if ((s->flags & SEC_LOAD) != 0
3684 && strncmp (s->name, ".note", 5) == 0)
3686 /* We need a PT_NOTE segment. */
3691 /* Let the backend count up any program headers it might need. */
3692 if (bed->elf_backend_additional_program_headers)
3696 a = (*bed->elf_backend_additional_program_headers) (abfd);
3702 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3703 return elf_tdata (abfd)->program_header_size;
3706 /* Work out the file positions of all the sections. This is called by
3707 _bfd_elf_compute_section_file_positions. All the section sizes and
3708 VMAs must be known before this is called.
3710 We do not consider reloc sections at this point, unless they form
3711 part of the loadable image. Reloc sections are assigned file
3712 positions in assign_file_positions_for_relocs, which is called by
3713 write_object_contents and final_link.
3715 We also don't set the positions of the .symtab and .strtab here. */
3718 assign_file_positions_except_relocs (abfd)
3721 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3722 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3723 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3724 unsigned int num_sec = elf_numsections (abfd);
3726 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3728 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3729 && bfd_get_format (abfd) != bfd_core)
3731 Elf_Internal_Shdr **hdrpp;
3734 /* Start after the ELF header. */
3735 off = i_ehdrp->e_ehsize;
3737 /* We are not creating an executable, which means that we are
3738 not creating a program header, and that the actual order of
3739 the sections in the file is unimportant. */
3740 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3742 Elf_Internal_Shdr *hdr;
3745 if (hdr->sh_type == SHT_REL
3746 || hdr->sh_type == SHT_RELA
3747 || i == tdata->symtab_section
3748 || i == tdata->symtab_shndx_section
3749 || i == tdata->strtab_section)
3751 hdr->sh_offset = -1;
3754 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3756 if (i == SHN_LORESERVE - 1)
3758 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3759 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3766 Elf_Internal_Shdr **hdrpp;
3768 /* Assign file positions for the loaded sections based on the
3769 assignment of sections to segments. */
3770 if (! assign_file_positions_for_segments (abfd))
3773 /* Assign file positions for the other sections. */
3775 off = elf_tdata (abfd)->next_file_pos;
3776 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3778 Elf_Internal_Shdr *hdr;
3781 if (hdr->bfd_section != NULL
3782 && hdr->bfd_section->filepos != 0)
3783 hdr->sh_offset = hdr->bfd_section->filepos;
3784 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3786 ((*_bfd_error_handler)
3787 (_("%s: warning: allocated section `%s' not in segment"),
3788 bfd_get_filename (abfd),
3789 (hdr->bfd_section == NULL
3791 : hdr->bfd_section->name)));
3792 if ((abfd->flags & D_PAGED) != 0)
3793 off += (hdr->sh_addr - off) % bed->maxpagesize;
3795 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3796 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3799 else if (hdr->sh_type == SHT_REL
3800 || hdr->sh_type == SHT_RELA
3801 || hdr == i_shdrpp[tdata->symtab_section]
3802 || hdr == i_shdrpp[tdata->symtab_shndx_section]
3803 || hdr == i_shdrpp[tdata->strtab_section])
3804 hdr->sh_offset = -1;
3806 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3808 if (i == SHN_LORESERVE - 1)
3810 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3811 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3816 /* Place the section headers. */
3817 off = align_file_position (off, bed->s->file_align);
3818 i_ehdrp->e_shoff = off;
3819 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3821 elf_tdata (abfd)->next_file_pos = off;
3830 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3831 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3832 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3834 struct elf_strtab_hash *shstrtab;
3835 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3837 i_ehdrp = elf_elfheader (abfd);
3838 i_shdrp = elf_elfsections (abfd);
3840 shstrtab = _bfd_elf_strtab_init ();
3841 if (shstrtab == NULL)
3844 elf_shstrtab (abfd) = shstrtab;
3846 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3847 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3848 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3849 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3851 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3852 i_ehdrp->e_ident[EI_DATA] =
3853 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3854 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3856 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3857 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3859 for (count = EI_PAD; count < EI_NIDENT; count++)
3860 i_ehdrp->e_ident[count] = 0;
3862 if ((abfd->flags & DYNAMIC) != 0)
3863 i_ehdrp->e_type = ET_DYN;
3864 else if ((abfd->flags & EXEC_P) != 0)
3865 i_ehdrp->e_type = ET_EXEC;
3866 else if (bfd_get_format (abfd) == bfd_core)
3867 i_ehdrp->e_type = ET_CORE;
3869 i_ehdrp->e_type = ET_REL;
3871 switch (bfd_get_arch (abfd))
3873 case bfd_arch_unknown:
3874 i_ehdrp->e_machine = EM_NONE;
3877 /* There used to be a long list of cases here, each one setting
3878 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3879 in the corresponding bfd definition. To avoid duplication,
3880 the switch was removed. Machines that need special handling
3881 can generally do it in elf_backend_final_write_processing(),
3882 unless they need the information earlier than the final write.
3883 Such need can generally be supplied by replacing the tests for
3884 e_machine with the conditions used to determine it. */
3886 if (get_elf_backend_data (abfd) != NULL)
3887 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
3889 i_ehdrp->e_machine = EM_NONE;
3892 i_ehdrp->e_version = bed->s->ev_current;
3893 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3895 /* No program header, for now. */
3896 i_ehdrp->e_phoff = 0;
3897 i_ehdrp->e_phentsize = 0;
3898 i_ehdrp->e_phnum = 0;
3900 /* Each bfd section is section header entry. */
3901 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3902 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3904 /* If we're building an executable, we'll need a program header table. */
3905 if (abfd->flags & EXEC_P)
3907 /* It all happens later. */
3909 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3911 /* elf_build_phdrs() returns a (NULL-terminated) array of
3912 Elf_Internal_Phdrs. */
3913 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3914 i_ehdrp->e_phoff = outbase;
3915 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3920 i_ehdrp->e_phentsize = 0;
3922 i_ehdrp->e_phoff = 0;
3925 elf_tdata (abfd)->symtab_hdr.sh_name =
3926 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
3927 elf_tdata (abfd)->strtab_hdr.sh_name =
3928 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
3929 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3930 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
3931 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3932 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3933 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3939 /* Assign file positions for all the reloc sections which are not part
3940 of the loadable file image. */
3943 _bfd_elf_assign_file_positions_for_relocs (abfd)
3947 unsigned int i, num_sec;
3948 Elf_Internal_Shdr **shdrpp;
3950 off = elf_tdata (abfd)->next_file_pos;
3952 num_sec = elf_numsections (abfd);
3953 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
3955 Elf_Internal_Shdr *shdrp;
3958 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3959 && shdrp->sh_offset == -1)
3960 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3963 elf_tdata (abfd)->next_file_pos = off;
3967 _bfd_elf_write_object_contents (abfd)
3970 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3971 Elf_Internal_Ehdr *i_ehdrp;
3972 Elf_Internal_Shdr **i_shdrp;
3974 unsigned int count, num_sec;
3976 if (! abfd->output_has_begun
3977 && ! _bfd_elf_compute_section_file_positions
3978 (abfd, (struct bfd_link_info *) NULL))
3981 i_shdrp = elf_elfsections (abfd);
3982 i_ehdrp = elf_elfheader (abfd);
3985 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3989 _bfd_elf_assign_file_positions_for_relocs (abfd);
3991 /* After writing the headers, we need to write the sections too... */
3992 num_sec = elf_numsections (abfd);
3993 for (count = 1; count < num_sec; count++)
3995 if (bed->elf_backend_section_processing)
3996 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3997 if (i_shdrp[count]->contents)
3999 bfd_size_type amt = i_shdrp[count]->sh_size;
4001 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4002 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4005 if (count == SHN_LORESERVE - 1)
4006 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4009 /* Write out the section header names. */
4010 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4011 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4014 if (bed->elf_backend_final_write_processing)
4015 (*bed->elf_backend_final_write_processing) (abfd,
4016 elf_tdata (abfd)->linker);
4018 return bed->s->write_shdrs_and_ehdr (abfd);
4022 _bfd_elf_write_corefile_contents (abfd)
4025 /* Hopefully this can be done just like an object file. */
4026 return _bfd_elf_write_object_contents (abfd);
4029 /* Given a section, search the header to find them. */
4032 _bfd_elf_section_from_bfd_section (abfd, asect)
4036 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4037 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4039 Elf_Internal_Shdr *hdr;
4040 int maxindex = elf_numsections (abfd);
4042 if (elf_section_data (asect) != NULL
4043 && elf_section_data (asect)->this_idx != 0)
4044 return elf_section_data (asect)->this_idx;
4046 if (bfd_is_abs_section (asect))
4048 if (bfd_is_com_section (asect))
4050 if (bfd_is_und_section (asect))
4053 for (index = 1; index < maxindex; index++)
4055 hdr = i_shdrp[index];
4056 if (hdr != NULL && hdr->bfd_section == asect)
4060 if (bed->elf_backend_section_from_bfd_section)
4062 for (index = 0; index < maxindex; index++)
4066 hdr = i_shdrp[index];
4071 if ((*bed->elf_backend_section_from_bfd_section)
4072 (abfd, hdr, asect, &retval))
4077 bfd_set_error (bfd_error_nonrepresentable_section);
4082 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4086 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4088 asymbol **asym_ptr_ptr;
4090 asymbol *asym_ptr = *asym_ptr_ptr;
4092 flagword flags = asym_ptr->flags;
4094 /* When gas creates relocations against local labels, it creates its
4095 own symbol for the section, but does put the symbol into the
4096 symbol chain, so udata is 0. When the linker is generating
4097 relocatable output, this section symbol may be for one of the
4098 input sections rather than the output section. */
4099 if (asym_ptr->udata.i == 0
4100 && (flags & BSF_SECTION_SYM)
4101 && asym_ptr->section)
4105 if (asym_ptr->section->output_section != NULL)
4106 indx = asym_ptr->section->output_section->index;
4108 indx = asym_ptr->section->index;
4109 if (indx < elf_num_section_syms (abfd)
4110 && elf_section_syms (abfd)[indx] != NULL)
4111 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4114 idx = asym_ptr->udata.i;
4118 /* This case can occur when using --strip-symbol on a symbol
4119 which is used in a relocation entry. */
4120 (*_bfd_error_handler)
4121 (_("%s: symbol `%s' required but not present"),
4122 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4123 bfd_set_error (bfd_error_no_symbols);
4130 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4131 (long) asym_ptr, asym_ptr->name, idx, flags,
4132 elf_symbol_flags (flags));
4140 /* Copy private BFD data. This copies any program header information. */
4143 copy_private_bfd_data (ibfd, obfd)
4147 Elf_Internal_Ehdr * iehdr;
4148 struct elf_segment_map * map;
4149 struct elf_segment_map * map_first;
4150 struct elf_segment_map ** pointer_to_map;
4151 Elf_Internal_Phdr * segment;
4154 unsigned int num_segments;
4155 boolean phdr_included = false;
4156 bfd_vma maxpagesize;
4157 struct elf_segment_map * phdr_adjust_seg = NULL;
4158 unsigned int phdr_adjust_num = 0;
4160 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4161 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4164 if (elf_tdata (ibfd)->phdr == NULL)
4167 iehdr = elf_elfheader (ibfd);
4170 pointer_to_map = &map_first;
4172 num_segments = elf_elfheader (ibfd)->e_phnum;
4173 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4175 /* Returns the end address of the segment + 1. */
4176 #define SEGMENT_END(segment, start) \
4177 (start + (segment->p_memsz > segment->p_filesz \
4178 ? segment->p_memsz : segment->p_filesz))
4180 /* Returns true if the given section is contained within
4181 the given segment. VMA addresses are compared. */
4182 #define IS_CONTAINED_BY_VMA(section, segment) \
4183 (section->vma >= segment->p_vaddr \
4184 && (section->vma + section->_raw_size) \
4185 <= (SEGMENT_END (segment, segment->p_vaddr)))
4187 /* Returns true if the given section is contained within
4188 the given segment. LMA addresses are compared. */
4189 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4190 (section->lma >= base \
4191 && (section->lma + section->_raw_size) \
4192 <= SEGMENT_END (segment, base))
4194 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4195 #define IS_COREFILE_NOTE(p, s) \
4196 (p->p_type == PT_NOTE \
4197 && bfd_get_format (ibfd) == bfd_core \
4198 && s->vma == 0 && s->lma == 0 \
4199 && (bfd_vma) s->filepos >= p->p_offset \
4200 && (bfd_vma) s->filepos + s->_raw_size \
4201 <= p->p_offset + p->p_filesz)
4203 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4204 linker, which generates a PT_INTERP section with p_vaddr and
4205 p_memsz set to 0. */
4206 #define IS_SOLARIS_PT_INTERP(p, s) \
4208 && p->p_filesz > 0 \
4209 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4210 && s->_raw_size > 0 \
4211 && (bfd_vma) s->filepos >= p->p_offset \
4212 && ((bfd_vma) s->filepos + s->_raw_size \
4213 <= p->p_offset + p->p_filesz))
4215 /* Decide if the given section should be included in the given segment.
4216 A section will be included if:
4217 1. It is within the address space of the segment -- we use the LMA
4218 if that is set for the segment and the VMA otherwise,
4219 2. It is an allocated segment,
4220 3. There is an output section associated with it,
4221 4. The section has not already been allocated to a previous segment. */
4222 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4223 (((((segment->p_paddr \
4224 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4225 : IS_CONTAINED_BY_VMA (section, segment)) \
4226 || IS_SOLARIS_PT_INTERP (segment, section)) \
4227 && (section->flags & SEC_ALLOC) != 0) \
4228 || IS_COREFILE_NOTE (segment, section)) \
4229 && section->output_section != NULL \
4230 && section->segment_mark == false)
4232 /* Returns true iff seg1 starts after the end of seg2. */
4233 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4234 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4236 /* Returns true iff seg1 and seg2 overlap. */
4237 #define SEGMENT_OVERLAPS(seg1, seg2) \
4238 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4240 /* Initialise the segment mark field. */
4241 for (section = ibfd->sections; section != NULL; section = section->next)
4242 section->segment_mark = false;
4244 /* Scan through the segments specified in the program header
4245 of the input BFD. For this first scan we look for overlaps
4246 in the loadable segments. These can be created by weird
4247 parameters to objcopy. */
4248 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4253 Elf_Internal_Phdr *segment2;
4255 if (segment->p_type != PT_LOAD)
4258 /* Determine if this segment overlaps any previous segments. */
4259 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4261 bfd_signed_vma extra_length;
4263 if (segment2->p_type != PT_LOAD
4264 || ! SEGMENT_OVERLAPS (segment, segment2))
4267 /* Merge the two segments together. */
4268 if (segment2->p_vaddr < segment->p_vaddr)
4270 /* Extend SEGMENT2 to include SEGMENT and then delete
4273 SEGMENT_END (segment, segment->p_vaddr)
4274 - SEGMENT_END (segment2, segment2->p_vaddr);
4276 if (extra_length > 0)
4278 segment2->p_memsz += extra_length;
4279 segment2->p_filesz += extra_length;
4282 segment->p_type = PT_NULL;
4284 /* Since we have deleted P we must restart the outer loop. */
4286 segment = elf_tdata (ibfd)->phdr;
4291 /* Extend SEGMENT to include SEGMENT2 and then delete
4294 SEGMENT_END (segment2, segment2->p_vaddr)
4295 - SEGMENT_END (segment, segment->p_vaddr);
4297 if (extra_length > 0)
4299 segment->p_memsz += extra_length;
4300 segment->p_filesz += extra_length;
4303 segment2->p_type = PT_NULL;
4308 /* The second scan attempts to assign sections to segments. */
4309 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4313 unsigned int section_count;
4314 asection ** sections;
4315 asection * output_section;
4317 bfd_vma matching_lma;
4318 bfd_vma suggested_lma;
4322 if (segment->p_type == PT_NULL)
4325 /* Compute how many sections might be placed into this segment. */
4327 for (section = ibfd->sections; section != NULL; section = section->next)
4328 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4331 /* Allocate a segment map big enough to contain all of the
4332 sections we have selected. */
4333 amt = sizeof (struct elf_segment_map);
4334 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4335 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4339 /* Initialise the fields of the segment map. Default to
4340 using the physical address of the segment in the input BFD. */
4342 map->p_type = segment->p_type;
4343 map->p_flags = segment->p_flags;
4344 map->p_flags_valid = 1;
4345 map->p_paddr = segment->p_paddr;
4346 map->p_paddr_valid = 1;
4348 /* Determine if this segment contains the ELF file header
4349 and if it contains the program headers themselves. */
4350 map->includes_filehdr = (segment->p_offset == 0
4351 && segment->p_filesz >= iehdr->e_ehsize);
4353 map->includes_phdrs = 0;
4355 if (! phdr_included || segment->p_type != PT_LOAD)
4357 map->includes_phdrs =
4358 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4359 && (segment->p_offset + segment->p_filesz
4360 >= ((bfd_vma) iehdr->e_phoff
4361 + iehdr->e_phnum * iehdr->e_phentsize)));
4363 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4364 phdr_included = true;
4367 if (section_count == 0)
4369 /* Special segments, such as the PT_PHDR segment, may contain
4370 no sections, but ordinary, loadable segments should contain
4372 if (segment->p_type == PT_LOAD)
4374 (_("%s: warning: Empty loadable segment detected\n"),
4375 bfd_archive_filename (ibfd));
4378 *pointer_to_map = map;
4379 pointer_to_map = &map->next;
4384 /* Now scan the sections in the input BFD again and attempt
4385 to add their corresponding output sections to the segment map.
4386 The problem here is how to handle an output section which has
4387 been moved (ie had its LMA changed). There are four possibilities:
4389 1. None of the sections have been moved.
4390 In this case we can continue to use the segment LMA from the
4393 2. All of the sections have been moved by the same amount.
4394 In this case we can change the segment's LMA to match the LMA
4395 of the first section.
4397 3. Some of the sections have been moved, others have not.
4398 In this case those sections which have not been moved can be
4399 placed in the current segment which will have to have its size,
4400 and possibly its LMA changed, and a new segment or segments will
4401 have to be created to contain the other sections.
4403 4. The sections have been moved, but not be the same amount.
4404 In this case we can change the segment's LMA to match the LMA
4405 of the first section and we will have to create a new segment
4406 or segments to contain the other sections.
4408 In order to save time, we allocate an array to hold the section
4409 pointers that we are interested in. As these sections get assigned
4410 to a segment, they are removed from this array. */
4412 amt = (bfd_size_type) section_count * sizeof (asection *);
4413 sections = (asection **) bfd_malloc (amt);
4414 if (sections == NULL)
4417 /* Step One: Scan for segment vs section LMA conflicts.
4418 Also add the sections to the section array allocated above.
4419 Also add the sections to the current segment. In the common
4420 case, where the sections have not been moved, this means that
4421 we have completely filled the segment, and there is nothing
4427 for (j = 0, section = ibfd->sections;
4429 section = section->next)
4431 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4433 output_section = section->output_section;
4435 sections[j ++] = section;
4437 /* The Solaris native linker always sets p_paddr to 0.
4438 We try to catch that case here, and set it to the
4440 if (segment->p_paddr == 0
4441 && segment->p_vaddr != 0
4443 && output_section->lma != 0
4444 && (output_section->vma == (segment->p_vaddr
4445 + (map->includes_filehdr
4448 + (map->includes_phdrs
4450 * iehdr->e_phentsize)
4452 map->p_paddr = segment->p_vaddr;
4454 /* Match up the physical address of the segment with the
4455 LMA address of the output section. */
4456 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4457 || IS_COREFILE_NOTE (segment, section))
4459 if (matching_lma == 0)
4460 matching_lma = output_section->lma;
4462 /* We assume that if the section fits within the segment
4463 then it does not overlap any other section within that
4465 map->sections[isec ++] = output_section;
4467 else if (suggested_lma == 0)
4468 suggested_lma = output_section->lma;
4472 BFD_ASSERT (j == section_count);
4474 /* Step Two: Adjust the physical address of the current segment,
4476 if (isec == section_count)
4478 /* All of the sections fitted within the segment as currently
4479 specified. This is the default case. Add the segment to
4480 the list of built segments and carry on to process the next
4481 program header in the input BFD. */
4482 map->count = section_count;
4483 *pointer_to_map = map;
4484 pointer_to_map = &map->next;
4491 if (matching_lma != 0)
4493 /* At least one section fits inside the current segment.
4494 Keep it, but modify its physical address to match the
4495 LMA of the first section that fitted. */
4496 map->p_paddr = matching_lma;
4500 /* None of the sections fitted inside the current segment.
4501 Change the current segment's physical address to match
4502 the LMA of the first section. */
4503 map->p_paddr = suggested_lma;
4506 /* Offset the segment physical address from the lma
4507 to allow for space taken up by elf headers. */
4508 if (map->includes_filehdr)
4509 map->p_paddr -= iehdr->e_ehsize;
4511 if (map->includes_phdrs)
4513 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4515 /* iehdr->e_phnum is just an estimate of the number
4516 of program headers that we will need. Make a note
4517 here of the number we used and the segment we chose
4518 to hold these headers, so that we can adjust the
4519 offset when we know the correct value. */
4520 phdr_adjust_num = iehdr->e_phnum;
4521 phdr_adjust_seg = map;
4525 /* Step Three: Loop over the sections again, this time assigning
4526 those that fit to the current segment and remvoing them from the
4527 sections array; but making sure not to leave large gaps. Once all
4528 possible sections have been assigned to the current segment it is
4529 added to the list of built segments and if sections still remain
4530 to be assigned, a new segment is constructed before repeating
4538 /* Fill the current segment with sections that fit. */
4539 for (j = 0; j < section_count; j++)
4541 section = sections[j];
4543 if (section == NULL)
4546 output_section = section->output_section;
4548 BFD_ASSERT (output_section != NULL);
4550 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4551 || IS_COREFILE_NOTE (segment, section))
4553 if (map->count == 0)
4555 /* If the first section in a segment does not start at
4556 the beginning of the segment, then something is
4558 if (output_section->lma !=
4560 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4561 + (map->includes_phdrs
4562 ? iehdr->e_phnum * iehdr->e_phentsize
4568 asection * prev_sec;
4570 prev_sec = map->sections[map->count - 1];
4572 /* If the gap between the end of the previous section
4573 and the start of this section is more than
4574 maxpagesize then we need to start a new segment. */
4575 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4577 < BFD_ALIGN (output_section->lma, maxpagesize))
4578 || ((prev_sec->lma + prev_sec->_raw_size)
4579 > output_section->lma))
4581 if (suggested_lma == 0)
4582 suggested_lma = output_section->lma;
4588 map->sections[map->count++] = output_section;
4591 section->segment_mark = true;
4593 else if (suggested_lma == 0)
4594 suggested_lma = output_section->lma;
4597 BFD_ASSERT (map->count > 0);
4599 /* Add the current segment to the list of built segments. */
4600 *pointer_to_map = map;
4601 pointer_to_map = &map->next;
4603 if (isec < section_count)
4605 /* We still have not allocated all of the sections to
4606 segments. Create a new segment here, initialise it
4607 and carry on looping. */
4608 amt = sizeof (struct elf_segment_map);
4609 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4610 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4614 /* Initialise the fields of the segment map. Set the physical
4615 physical address to the LMA of the first section that has
4616 not yet been assigned. */
4618 map->p_type = segment->p_type;
4619 map->p_flags = segment->p_flags;
4620 map->p_flags_valid = 1;
4621 map->p_paddr = suggested_lma;
4622 map->p_paddr_valid = 1;
4623 map->includes_filehdr = 0;
4624 map->includes_phdrs = 0;
4627 while (isec < section_count);
4632 /* The Solaris linker creates program headers in which all the
4633 p_paddr fields are zero. When we try to objcopy or strip such a
4634 file, we get confused. Check for this case, and if we find it
4635 reset the p_paddr_valid fields. */
4636 for (map = map_first; map != NULL; map = map->next)
4637 if (map->p_paddr != 0)
4641 for (map = map_first; map != NULL; map = map->next)
4642 map->p_paddr_valid = 0;
4645 elf_tdata (obfd)->segment_map = map_first;
4647 /* If we had to estimate the number of program headers that were
4648 going to be needed, then check our estimate now and adjust
4649 the offset if necessary. */
4650 if (phdr_adjust_seg != NULL)
4654 for (count = 0, map = map_first; map != NULL; map = map->next)
4657 if (count > phdr_adjust_num)
4658 phdr_adjust_seg->p_paddr
4659 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4663 /* Final Step: Sort the segments into ascending order of physical
4665 if (map_first != NULL)
4667 struct elf_segment_map *prev;
4670 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4672 /* Yes I know - its a bubble sort.... */
4673 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4675 /* Swap map and map->next. */
4676 prev->next = map->next;
4677 map->next = map->next->next;
4678 prev->next->next = map;
4688 #undef IS_CONTAINED_BY_VMA
4689 #undef IS_CONTAINED_BY_LMA
4690 #undef IS_COREFILE_NOTE
4691 #undef IS_SOLARIS_PT_INTERP
4692 #undef INCLUDE_SECTION_IN_SEGMENT
4693 #undef SEGMENT_AFTER_SEGMENT
4694 #undef SEGMENT_OVERLAPS
4698 /* Copy private section information. This copies over the entsize
4699 field, and sometimes the info field. */
4702 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4708 Elf_Internal_Shdr *ihdr, *ohdr;
4710 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4711 || obfd->xvec->flavour != bfd_target_elf_flavour)
4714 /* Copy over private BFD data if it has not already been copied.
4715 This must be done here, rather than in the copy_private_bfd_data
4716 entry point, because the latter is called after the section
4717 contents have been set, which means that the program headers have
4718 already been worked out. */
4719 if (elf_tdata (obfd)->segment_map == NULL
4720 && elf_tdata (ibfd)->phdr != NULL)
4724 /* Only set up the segments if there are no more SEC_ALLOC
4725 sections. FIXME: This won't do the right thing if objcopy is
4726 used to remove the last SEC_ALLOC section, since objcopy
4727 won't call this routine in that case. */
4728 for (s = isec->next; s != NULL; s = s->next)
4729 if ((s->flags & SEC_ALLOC) != 0)
4733 if (! copy_private_bfd_data (ibfd, obfd))
4738 ihdr = &elf_section_data (isec)->this_hdr;
4739 ohdr = &elf_section_data (osec)->this_hdr;
4741 ohdr->sh_entsize = ihdr->sh_entsize;
4743 if (ihdr->sh_type == SHT_SYMTAB
4744 || ihdr->sh_type == SHT_DYNSYM
4745 || ihdr->sh_type == SHT_GNU_verneed
4746 || ihdr->sh_type == SHT_GNU_verdef)
4747 ohdr->sh_info = ihdr->sh_info;
4749 elf_section_data (osec)->use_rela_p
4750 = elf_section_data (isec)->use_rela_p;
4755 /* Copy private symbol information. If this symbol is in a section
4756 which we did not map into a BFD section, try to map the section
4757 index correctly. We use special macro definitions for the mapped
4758 section indices; these definitions are interpreted by the
4759 swap_out_syms function. */
4761 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4762 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4763 #define MAP_STRTAB (SHN_HIOS + 3)
4764 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4765 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4768 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4774 elf_symbol_type *isym, *osym;
4776 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4777 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4780 isym = elf_symbol_from (ibfd, isymarg);
4781 osym = elf_symbol_from (obfd, osymarg);
4785 && bfd_is_abs_section (isym->symbol.section))
4789 shndx = isym->internal_elf_sym.st_shndx;
4790 if (shndx == elf_onesymtab (ibfd))
4791 shndx = MAP_ONESYMTAB;
4792 else if (shndx == elf_dynsymtab (ibfd))
4793 shndx = MAP_DYNSYMTAB;
4794 else if (shndx == elf_tdata (ibfd)->strtab_section)
4796 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4797 shndx = MAP_SHSTRTAB;
4798 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
4799 shndx = MAP_SYM_SHNDX;
4800 osym->internal_elf_sym.st_shndx = shndx;
4806 /* Swap out the symbols. */
4809 swap_out_syms (abfd, sttp, relocatable_p)
4811 struct bfd_strtab_hash **sttp;
4814 struct elf_backend_data *bed;
4817 struct bfd_strtab_hash *stt;
4818 Elf_Internal_Shdr *symtab_hdr;
4819 Elf_Internal_Shdr *symtab_shndx_hdr;
4820 Elf_Internal_Shdr *symstrtab_hdr;
4821 char *outbound_syms;
4822 char *outbound_shndx;
4826 if (!elf_map_symbols (abfd))
4829 /* Dump out the symtabs. */
4830 stt = _bfd_elf_stringtab_init ();
4834 bed = get_elf_backend_data (abfd);
4835 symcount = bfd_get_symcount (abfd);
4836 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4837 symtab_hdr->sh_type = SHT_SYMTAB;
4838 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4839 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4840 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4841 symtab_hdr->sh_addralign = bed->s->file_align;
4843 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4844 symstrtab_hdr->sh_type = SHT_STRTAB;
4846 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
4847 outbound_syms = bfd_alloc (abfd, amt);
4848 if (outbound_syms == NULL)
4850 symtab_hdr->contents = (PTR) outbound_syms;
4852 outbound_shndx = NULL;
4853 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
4854 if (symtab_shndx_hdr->sh_name != 0)
4856 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
4857 outbound_shndx = bfd_alloc (abfd, amt);
4858 if (outbound_shndx == NULL)
4860 memset (outbound_shndx, 0, (unsigned long) amt);
4861 symtab_shndx_hdr->contents = outbound_shndx;
4862 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
4863 symtab_shndx_hdr->sh_size = amt;
4864 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
4865 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
4868 /* now generate the data (for "contents") */
4870 /* Fill in zeroth symbol and swap it out. */
4871 Elf_Internal_Sym sym;
4877 sym.st_shndx = SHN_UNDEF;
4878 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
4879 outbound_syms += bed->s->sizeof_sym;
4880 if (outbound_shndx != NULL)
4881 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
4884 syms = bfd_get_outsymbols (abfd);
4885 for (idx = 0; idx < symcount; idx++)
4887 Elf_Internal_Sym sym;
4888 bfd_vma value = syms[idx]->value;
4889 elf_symbol_type *type_ptr;
4890 flagword flags = syms[idx]->flags;
4893 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4895 /* Local section symbols have no name. */
4900 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4903 if (sym.st_name == (unsigned long) -1)
4907 type_ptr = elf_symbol_from (abfd, syms[idx]);
4909 if ((flags & BSF_SECTION_SYM) == 0
4910 && bfd_is_com_section (syms[idx]->section))
4912 /* ELF common symbols put the alignment into the `value' field,
4913 and the size into the `size' field. This is backwards from
4914 how BFD handles it, so reverse it here. */
4915 sym.st_size = value;
4916 if (type_ptr == NULL
4917 || type_ptr->internal_elf_sym.st_value == 0)
4918 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4920 sym.st_value = type_ptr->internal_elf_sym.st_value;
4921 sym.st_shndx = _bfd_elf_section_from_bfd_section
4922 (abfd, syms[idx]->section);
4926 asection *sec = syms[idx]->section;
4929 if (sec->output_section)
4931 value += sec->output_offset;
4932 sec = sec->output_section;
4934 /* Don't add in the section vma for relocatable output. */
4935 if (! relocatable_p)
4937 sym.st_value = value;
4938 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4940 if (bfd_is_abs_section (sec)
4942 && type_ptr->internal_elf_sym.st_shndx != 0)
4944 /* This symbol is in a real ELF section which we did
4945 not create as a BFD section. Undo the mapping done
4946 by copy_private_symbol_data. */
4947 shndx = type_ptr->internal_elf_sym.st_shndx;
4951 shndx = elf_onesymtab (abfd);
4954 shndx = elf_dynsymtab (abfd);
4957 shndx = elf_tdata (abfd)->strtab_section;
4960 shndx = elf_tdata (abfd)->shstrtab_section;
4963 shndx = elf_tdata (abfd)->symtab_shndx_section;
4971 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4977 /* Writing this would be a hell of a lot easier if
4978 we had some decent documentation on bfd, and
4979 knew what to expect of the library, and what to
4980 demand of applications. For example, it
4981 appears that `objcopy' might not set the
4982 section of a symbol to be a section that is
4983 actually in the output file. */
4984 sec2 = bfd_get_section_by_name (abfd, sec->name);
4985 BFD_ASSERT (sec2 != 0);
4986 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4987 BFD_ASSERT (shndx != -1);
4991 sym.st_shndx = shndx;
4994 if ((flags & BSF_FUNCTION) != 0)
4996 else if ((flags & BSF_OBJECT) != 0)
5001 /* Processor-specific types */
5002 if (type_ptr != NULL
5003 && bed->elf_backend_get_symbol_type)
5004 type = ((*bed->elf_backend_get_symbol_type)
5005 (&type_ptr->internal_elf_sym, type));
5007 if (flags & BSF_SECTION_SYM)
5009 if (flags & BSF_GLOBAL)
5010 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5012 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5014 else if (bfd_is_com_section (syms[idx]->section))
5015 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5016 else if (bfd_is_und_section (syms[idx]->section))
5017 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5021 else if (flags & BSF_FILE)
5022 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5025 int bind = STB_LOCAL;
5027 if (flags & BSF_LOCAL)
5029 else if (flags & BSF_WEAK)
5031 else if (flags & BSF_GLOBAL)
5034 sym.st_info = ELF_ST_INFO (bind, type);
5037 if (type_ptr != NULL)
5038 sym.st_other = type_ptr->internal_elf_sym.st_other;
5042 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5043 outbound_syms += bed->s->sizeof_sym;
5044 if (outbound_shndx != NULL)
5045 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5049 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5050 symstrtab_hdr->sh_type = SHT_STRTAB;
5052 symstrtab_hdr->sh_flags = 0;
5053 symstrtab_hdr->sh_addr = 0;
5054 symstrtab_hdr->sh_entsize = 0;
5055 symstrtab_hdr->sh_link = 0;
5056 symstrtab_hdr->sh_info = 0;
5057 symstrtab_hdr->sh_addralign = 1;
5062 /* Return the number of bytes required to hold the symtab vector.
5064 Note that we base it on the count plus 1, since we will null terminate
5065 the vector allocated based on this size. However, the ELF symbol table
5066 always has a dummy entry as symbol #0, so it ends up even. */
5069 _bfd_elf_get_symtab_upper_bound (abfd)
5074 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5076 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5077 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5083 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5088 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5090 if (elf_dynsymtab (abfd) == 0)
5092 bfd_set_error (bfd_error_invalid_operation);
5096 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5097 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5103 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5104 bfd *abfd ATTRIBUTE_UNUSED;
5107 return (asect->reloc_count + 1) * sizeof (arelent *);
5110 /* Canonicalize the relocs. */
5113 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5121 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5123 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
5126 tblptr = section->relocation;
5127 for (i = 0; i < section->reloc_count; i++)
5128 *relptr++ = tblptr++;
5132 return section->reloc_count;
5136 _bfd_elf_get_symtab (abfd, alocation)
5138 asymbol **alocation;
5140 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5141 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
5144 bfd_get_symcount (abfd) = symcount;
5149 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5151 asymbol **alocation;
5153 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5154 return bed->s->slurp_symbol_table (abfd, alocation, true);
5157 /* Return the size required for the dynamic reloc entries. Any
5158 section that was actually installed in the BFD, and has type
5159 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5160 considered to be a dynamic reloc section. */
5163 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5169 if (elf_dynsymtab (abfd) == 0)
5171 bfd_set_error (bfd_error_invalid_operation);
5175 ret = sizeof (arelent *);
5176 for (s = abfd->sections; s != NULL; s = s->next)
5177 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5178 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5179 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5180 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5181 * sizeof (arelent *));
5186 /* Canonicalize the dynamic relocation entries. Note that we return
5187 the dynamic relocations as a single block, although they are
5188 actually associated with particular sections; the interface, which
5189 was designed for SunOS style shared libraries, expects that there
5190 is only one set of dynamic relocs. Any section that was actually
5191 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5192 the dynamic symbol table, is considered to be a dynamic reloc
5196 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5201 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5205 if (elf_dynsymtab (abfd) == 0)
5207 bfd_set_error (bfd_error_invalid_operation);
5211 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5213 for (s = abfd->sections; s != NULL; s = s->next)
5215 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5216 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5217 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5222 if (! (*slurp_relocs) (abfd, s, syms, true))
5224 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5226 for (i = 0; i < count; i++)
5237 /* Read in the version information. */
5240 _bfd_elf_slurp_version_tables (abfd)
5243 bfd_byte *contents = NULL;
5246 if (elf_dynverdef (abfd) != 0)
5248 Elf_Internal_Shdr *hdr;
5249 Elf_External_Verdef *everdef;
5250 Elf_Internal_Verdef *iverdef;
5251 Elf_Internal_Verdef *iverdefarr;
5252 Elf_Internal_Verdef iverdefmem;
5254 unsigned int maxidx;
5256 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5258 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5259 if (contents == NULL)
5261 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5262 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5265 /* We know the number of entries in the section but not the maximum
5266 index. Therefore we have to run through all entries and find
5268 everdef = (Elf_External_Verdef *) contents;
5270 for (i = 0; i < hdr->sh_info; ++i)
5272 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5274 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5275 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5277 everdef = ((Elf_External_Verdef *)
5278 ((bfd_byte *) everdef + iverdefmem.vd_next));
5281 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5282 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5283 if (elf_tdata (abfd)->verdef == NULL)
5286 elf_tdata (abfd)->cverdefs = maxidx;
5288 everdef = (Elf_External_Verdef *) contents;
5289 iverdefarr = elf_tdata (abfd)->verdef;
5290 for (i = 0; i < hdr->sh_info; i++)
5292 Elf_External_Verdaux *everdaux;
5293 Elf_Internal_Verdaux *iverdaux;
5296 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5298 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5299 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5301 iverdef->vd_bfd = abfd;
5303 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5304 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5305 if (iverdef->vd_auxptr == NULL)
5308 everdaux = ((Elf_External_Verdaux *)
5309 ((bfd_byte *) everdef + iverdef->vd_aux));
5310 iverdaux = iverdef->vd_auxptr;
5311 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5313 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5315 iverdaux->vda_nodename =
5316 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5317 iverdaux->vda_name);
5318 if (iverdaux->vda_nodename == NULL)
5321 if (j + 1 < iverdef->vd_cnt)
5322 iverdaux->vda_nextptr = iverdaux + 1;
5324 iverdaux->vda_nextptr = NULL;
5326 everdaux = ((Elf_External_Verdaux *)
5327 ((bfd_byte *) everdaux + iverdaux->vda_next));
5330 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5332 if (i + 1 < hdr->sh_info)
5333 iverdef->vd_nextdef = iverdef + 1;
5335 iverdef->vd_nextdef = NULL;
5337 everdef = ((Elf_External_Verdef *)
5338 ((bfd_byte *) everdef + iverdef->vd_next));
5345 if (elf_dynverref (abfd) != 0)
5347 Elf_Internal_Shdr *hdr;
5348 Elf_External_Verneed *everneed;
5349 Elf_Internal_Verneed *iverneed;
5352 hdr = &elf_tdata (abfd)->dynverref_hdr;
5354 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5355 elf_tdata (abfd)->verref =
5356 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5357 if (elf_tdata (abfd)->verref == NULL)
5360 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5362 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5363 if (contents == NULL)
5365 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5366 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5369 everneed = (Elf_External_Verneed *) contents;
5370 iverneed = elf_tdata (abfd)->verref;
5371 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5373 Elf_External_Vernaux *evernaux;
5374 Elf_Internal_Vernaux *ivernaux;
5377 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5379 iverneed->vn_bfd = abfd;
5381 iverneed->vn_filename =
5382 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5384 if (iverneed->vn_filename == NULL)
5387 amt = iverneed->vn_cnt;
5388 amt *= sizeof (Elf_Internal_Vernaux);
5389 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5391 evernaux = ((Elf_External_Vernaux *)
5392 ((bfd_byte *) everneed + iverneed->vn_aux));
5393 ivernaux = iverneed->vn_auxptr;
5394 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5396 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5398 ivernaux->vna_nodename =
5399 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5400 ivernaux->vna_name);
5401 if (ivernaux->vna_nodename == NULL)
5404 if (j + 1 < iverneed->vn_cnt)
5405 ivernaux->vna_nextptr = ivernaux + 1;
5407 ivernaux->vna_nextptr = NULL;
5409 evernaux = ((Elf_External_Vernaux *)
5410 ((bfd_byte *) evernaux + ivernaux->vna_next));
5413 if (i + 1 < hdr->sh_info)
5414 iverneed->vn_nextref = iverneed + 1;
5416 iverneed->vn_nextref = NULL;
5418 everneed = ((Elf_External_Verneed *)
5419 ((bfd_byte *) everneed + iverneed->vn_next));
5429 if (contents == NULL)
5435 _bfd_elf_make_empty_symbol (abfd)
5438 elf_symbol_type *newsym;
5439 bfd_size_type amt = sizeof (elf_symbol_type);
5441 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5446 newsym->symbol.the_bfd = abfd;
5447 return &newsym->symbol;
5452 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5453 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5457 bfd_symbol_info (symbol, ret);
5460 /* Return whether a symbol name implies a local symbol. Most targets
5461 use this function for the is_local_label_name entry point, but some
5465 _bfd_elf_is_local_label_name (abfd, name)
5466 bfd *abfd ATTRIBUTE_UNUSED;
5469 /* Normal local symbols start with ``.L''. */
5470 if (name[0] == '.' && name[1] == 'L')
5473 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5474 DWARF debugging symbols starting with ``..''. */
5475 if (name[0] == '.' && name[1] == '.')
5478 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5479 emitting DWARF debugging output. I suspect this is actually a
5480 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5481 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5482 underscore to be emitted on some ELF targets). For ease of use,
5483 we treat such symbols as local. */
5484 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5491 _bfd_elf_get_lineno (ignore_abfd, symbol)
5492 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5493 asymbol *symbol ATTRIBUTE_UNUSED;
5500 _bfd_elf_set_arch_mach (abfd, arch, machine)
5502 enum bfd_architecture arch;
5503 unsigned long machine;
5505 /* If this isn't the right architecture for this backend, and this
5506 isn't the generic backend, fail. */
5507 if (arch != get_elf_backend_data (abfd)->arch
5508 && arch != bfd_arch_unknown
5509 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5512 return bfd_default_set_arch_mach (abfd, arch, machine);
5515 /* Find the function to a particular section and offset,
5516 for error reporting. */
5519 elf_find_function (abfd, section, symbols, offset,
5520 filename_ptr, functionname_ptr)
5521 bfd *abfd ATTRIBUTE_UNUSED;
5525 const char **filename_ptr;
5526 const char **functionname_ptr;
5528 const char *filename;
5537 for (p = symbols; *p != NULL; p++)
5541 q = (elf_symbol_type *) *p;
5543 if (bfd_get_section (&q->symbol) != section)
5546 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5551 filename = bfd_asymbol_name (&q->symbol);
5555 if (q->symbol.section == section
5556 && q->symbol.value >= low_func
5557 && q->symbol.value <= offset)
5559 func = (asymbol *) q;
5560 low_func = q->symbol.value;
5570 *filename_ptr = filename;
5571 if (functionname_ptr)
5572 *functionname_ptr = bfd_asymbol_name (func);
5577 /* Find the nearest line to a particular section and offset,
5578 for error reporting. */
5581 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5582 filename_ptr, functionname_ptr, line_ptr)
5587 const char **filename_ptr;
5588 const char **functionname_ptr;
5589 unsigned int *line_ptr;
5593 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5594 filename_ptr, functionname_ptr,
5597 if (!*functionname_ptr)
5598 elf_find_function (abfd, section, symbols, offset,
5599 *filename_ptr ? NULL : filename_ptr,
5605 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5606 filename_ptr, functionname_ptr,
5608 &elf_tdata (abfd)->dwarf2_find_line_info))
5610 if (!*functionname_ptr)
5611 elf_find_function (abfd, section, symbols, offset,
5612 *filename_ptr ? NULL : filename_ptr,
5618 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5619 &found, filename_ptr,
5620 functionname_ptr, line_ptr,
5621 &elf_tdata (abfd)->line_info))
5626 if (symbols == NULL)
5629 if (! elf_find_function (abfd, section, symbols, offset,
5630 filename_ptr, functionname_ptr))
5638 _bfd_elf_sizeof_headers (abfd, reloc)
5644 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5646 ret += get_program_header_size (abfd);
5651 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5656 bfd_size_type count;
5658 Elf_Internal_Shdr *hdr;
5661 if (! abfd->output_has_begun
5662 && ! _bfd_elf_compute_section_file_positions
5663 (abfd, (struct bfd_link_info *) NULL))
5666 hdr = &elf_section_data (section)->this_hdr;
5667 pos = hdr->sh_offset + offset;
5668 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5669 || bfd_bwrite (location, count, abfd) != count)
5676 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5677 bfd *abfd ATTRIBUTE_UNUSED;
5678 arelent *cache_ptr ATTRIBUTE_UNUSED;
5679 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5686 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5689 Elf_Internal_Rel *dst;
5695 /* Try to convert a non-ELF reloc into an ELF one. */
5698 _bfd_elf_validate_reloc (abfd, areloc)
5702 /* Check whether we really have an ELF howto. */
5704 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5706 bfd_reloc_code_real_type code;
5707 reloc_howto_type *howto;
5709 /* Alien reloc: Try to determine its type to replace it with an
5710 equivalent ELF reloc. */
5712 if (areloc->howto->pc_relative)
5714 switch (areloc->howto->bitsize)
5717 code = BFD_RELOC_8_PCREL;
5720 code = BFD_RELOC_12_PCREL;
5723 code = BFD_RELOC_16_PCREL;
5726 code = BFD_RELOC_24_PCREL;
5729 code = BFD_RELOC_32_PCREL;
5732 code = BFD_RELOC_64_PCREL;
5738 howto = bfd_reloc_type_lookup (abfd, code);
5740 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5742 if (howto->pcrel_offset)
5743 areloc->addend += areloc->address;
5745 areloc->addend -= areloc->address; /* addend is unsigned!! */
5750 switch (areloc->howto->bitsize)
5756 code = BFD_RELOC_14;
5759 code = BFD_RELOC_16;
5762 code = BFD_RELOC_26;
5765 code = BFD_RELOC_32;
5768 code = BFD_RELOC_64;
5774 howto = bfd_reloc_type_lookup (abfd, code);
5778 areloc->howto = howto;
5786 (*_bfd_error_handler)
5787 (_("%s: unsupported relocation type %s"),
5788 bfd_archive_filename (abfd), areloc->howto->name);
5789 bfd_set_error (bfd_error_bad_value);
5794 _bfd_elf_close_and_cleanup (abfd)
5797 if (bfd_get_format (abfd) == bfd_object)
5799 if (elf_shstrtab (abfd) != NULL)
5800 _bfd_elf_strtab_free (elf_shstrtab (abfd));
5803 return _bfd_generic_close_and_cleanup (abfd);
5806 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5807 in the relocation's offset. Thus we cannot allow any sort of sanity
5808 range-checking to interfere. There is nothing else to do in processing
5811 bfd_reloc_status_type
5812 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5813 bfd *abfd ATTRIBUTE_UNUSED;
5814 arelent *re ATTRIBUTE_UNUSED;
5815 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5816 PTR data ATTRIBUTE_UNUSED;
5817 asection *is ATTRIBUTE_UNUSED;
5818 bfd *obfd ATTRIBUTE_UNUSED;
5819 char **errmsg ATTRIBUTE_UNUSED;
5821 return bfd_reloc_ok;
5824 /* Elf core file support. Much of this only works on native
5825 toolchains, since we rely on knowing the
5826 machine-dependent procfs structure in order to pick
5827 out details about the corefile. */
5829 #ifdef HAVE_SYS_PROCFS_H
5830 # include <sys/procfs.h>
5833 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5836 elfcore_make_pid (abfd)
5839 return ((elf_tdata (abfd)->core_lwpid << 16)
5840 + (elf_tdata (abfd)->core_pid));
5843 /* If there isn't a section called NAME, make one, using
5844 data from SECT. Note, this function will generate a
5845 reference to NAME, so you shouldn't deallocate or
5849 elfcore_maybe_make_sect (abfd, name, sect)
5856 if (bfd_get_section_by_name (abfd, name) != NULL)
5859 sect2 = bfd_make_section (abfd, name);
5863 sect2->_raw_size = sect->_raw_size;
5864 sect2->filepos = sect->filepos;
5865 sect2->flags = sect->flags;
5866 sect2->alignment_power = sect->alignment_power;
5870 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5871 actually creates up to two pseudosections:
5872 - For the single-threaded case, a section named NAME, unless
5873 such a section already exists.
5874 - For the multi-threaded case, a section named "NAME/PID", where
5875 PID is elfcore_make_pid (abfd).
5876 Both pseudosections have identical contents. */
5878 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5885 char *threaded_name;
5888 /* Build the section name. */
5890 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5891 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
5892 if (threaded_name == NULL)
5894 strcpy (threaded_name, buf);
5896 sect = bfd_make_section (abfd, threaded_name);
5899 sect->_raw_size = size;
5900 sect->filepos = filepos;
5901 sect->flags = SEC_HAS_CONTENTS;
5902 sect->alignment_power = 2;
5904 return elfcore_maybe_make_sect (abfd, name, sect);
5907 /* prstatus_t exists on:
5909 linux 2.[01] + glibc
5913 #if defined (HAVE_PRSTATUS_T)
5914 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5917 elfcore_grok_prstatus (abfd, note)
5919 Elf_Internal_Note *note;
5924 if (note->descsz == sizeof (prstatus_t))
5928 raw_size = sizeof (prstat.pr_reg);
5929 offset = offsetof (prstatus_t, pr_reg);
5930 memcpy (&prstat, note->descdata, sizeof (prstat));
5932 /* Do not overwrite the core signal if it
5933 has already been set by another thread. */
5934 if (elf_tdata (abfd)->core_signal == 0)
5935 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5936 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5938 /* pr_who exists on:
5941 pr_who doesn't exist on:
5944 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5945 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5948 #if defined (HAVE_PRSTATUS32_T)
5949 else if (note->descsz == sizeof (prstatus32_t))
5951 /* 64-bit host, 32-bit corefile */
5952 prstatus32_t prstat;
5954 raw_size = sizeof (prstat.pr_reg);
5955 offset = offsetof (prstatus32_t, pr_reg);
5956 memcpy (&prstat, note->descdata, sizeof (prstat));
5958 /* Do not overwrite the core signal if it
5959 has already been set by another thread. */
5960 if (elf_tdata (abfd)->core_signal == 0)
5961 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5962 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5964 /* pr_who exists on:
5967 pr_who doesn't exist on:
5970 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5971 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5974 #endif /* HAVE_PRSTATUS32_T */
5977 /* Fail - we don't know how to handle any other
5978 note size (ie. data object type). */
5982 /* Make a ".reg/999" section and a ".reg" section. */
5983 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5984 raw_size, note->descpos + offset);
5986 #endif /* defined (HAVE_PRSTATUS_T) */
5988 /* Create a pseudosection containing the exact contents of NOTE. */
5990 elfcore_make_note_pseudosection (abfd, name, note)
5993 Elf_Internal_Note *note;
5995 return _bfd_elfcore_make_pseudosection (abfd, name,
5996 note->descsz, note->descpos);
5999 /* There isn't a consistent prfpregset_t across platforms,
6000 but it doesn't matter, because we don't have to pick this
6001 data structure apart. */
6004 elfcore_grok_prfpreg (abfd, note)
6006 Elf_Internal_Note *note;
6008 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6011 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6012 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6016 elfcore_grok_prxfpreg (abfd, note)
6018 Elf_Internal_Note *note;
6020 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6023 #if defined (HAVE_PRPSINFO_T)
6024 typedef prpsinfo_t elfcore_psinfo_t;
6025 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6026 typedef prpsinfo32_t elfcore_psinfo32_t;
6030 #if defined (HAVE_PSINFO_T)
6031 typedef psinfo_t elfcore_psinfo_t;
6032 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6033 typedef psinfo32_t elfcore_psinfo32_t;
6037 /* return a malloc'ed copy of a string at START which is at
6038 most MAX bytes long, possibly without a terminating '\0'.
6039 the copy will always have a terminating '\0'. */
6042 _bfd_elfcore_strndup (abfd, start, max)
6048 char *end = memchr (start, '\0', max);
6056 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6060 memcpy (dups, start, len);
6066 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6067 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
6070 elfcore_grok_psinfo (abfd, note)
6072 Elf_Internal_Note *note;
6074 if (note->descsz == sizeof (elfcore_psinfo_t))
6076 elfcore_psinfo_t psinfo;
6078 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6080 elf_tdata (abfd)->core_program
6081 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6082 sizeof (psinfo.pr_fname));
6084 elf_tdata (abfd)->core_command
6085 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6086 sizeof (psinfo.pr_psargs));
6088 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6089 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6091 /* 64-bit host, 32-bit corefile */
6092 elfcore_psinfo32_t psinfo;
6094 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6096 elf_tdata (abfd)->core_program
6097 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6098 sizeof (psinfo.pr_fname));
6100 elf_tdata (abfd)->core_command
6101 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6102 sizeof (psinfo.pr_psargs));
6108 /* Fail - we don't know how to handle any other
6109 note size (ie. data object type). */
6113 /* Note that for some reason, a spurious space is tacked
6114 onto the end of the args in some (at least one anyway)
6115 implementations, so strip it off if it exists. */
6118 char *command = elf_tdata (abfd)->core_command;
6119 int n = strlen (command);
6121 if (0 < n && command[n - 1] == ' ')
6122 command[n - 1] = '\0';
6127 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6129 #if defined (HAVE_PSTATUS_T)
6131 elfcore_grok_pstatus (abfd, note)
6133 Elf_Internal_Note *note;
6135 if (note->descsz == sizeof (pstatus_t)
6136 #if defined (HAVE_PXSTATUS_T)
6137 || note->descsz == sizeof (pxstatus_t)
6143 memcpy (&pstat, note->descdata, sizeof (pstat));
6145 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6147 #if defined (HAVE_PSTATUS32_T)
6148 else if (note->descsz == sizeof (pstatus32_t))
6150 /* 64-bit host, 32-bit corefile */
6153 memcpy (&pstat, note->descdata, sizeof (pstat));
6155 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6158 /* Could grab some more details from the "representative"
6159 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6160 NT_LWPSTATUS note, presumably. */
6164 #endif /* defined (HAVE_PSTATUS_T) */
6166 #if defined (HAVE_LWPSTATUS_T)
6168 elfcore_grok_lwpstatus (abfd, note)
6170 Elf_Internal_Note *note;
6172 lwpstatus_t lwpstat;
6177 if (note->descsz != sizeof (lwpstat)
6178 #if defined (HAVE_LWPXSTATUS_T)
6179 && note->descsz != sizeof (lwpxstatus_t)
6184 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6186 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6187 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6189 /* Make a ".reg/999" section. */
6191 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6192 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6197 sect = bfd_make_section (abfd, name);
6201 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6202 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6203 sect->filepos = note->descpos
6204 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6207 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6208 sect->_raw_size = sizeof (lwpstat.pr_reg);
6209 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6212 sect->flags = SEC_HAS_CONTENTS;
6213 sect->alignment_power = 2;
6215 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6218 /* Make a ".reg2/999" section */
6220 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6221 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6226 sect = bfd_make_section (abfd, name);
6230 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6231 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6232 sect->filepos = note->descpos
6233 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6236 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6237 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6238 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6241 sect->flags = SEC_HAS_CONTENTS;
6242 sect->alignment_power = 2;
6244 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6246 #endif /* defined (HAVE_LWPSTATUS_T) */
6248 #if defined (HAVE_WIN32_PSTATUS_T)
6250 elfcore_grok_win32pstatus (abfd, note)
6252 Elf_Internal_Note *note;
6257 win32_pstatus_t pstatus;
6259 if (note->descsz < sizeof (pstatus))
6262 memcpy (&pstatus, note->descdata, note->descsz);
6264 switch (pstatus.data_type)
6266 case NOTE_INFO_PROCESS:
6267 /* FIXME: need to add ->core_command. */
6268 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6269 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6272 case NOTE_INFO_THREAD:
6273 /* Make a ".reg/999" section. */
6274 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6276 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6282 sect = bfd_make_section (abfd, name);
6286 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6287 sect->filepos = (note->descpos
6288 + offsetof (struct win32_pstatus,
6289 data.thread_info.thread_context));
6290 sect->flags = SEC_HAS_CONTENTS;
6291 sect->alignment_power = 2;
6293 if (pstatus.data.thread_info.is_active_thread)
6294 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6298 case NOTE_INFO_MODULE:
6299 /* Make a ".module/xxxxxxxx" section. */
6300 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6302 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6308 sect = bfd_make_section (abfd, name);
6313 sect->_raw_size = note->descsz;
6314 sect->filepos = note->descpos;
6315 sect->flags = SEC_HAS_CONTENTS;
6316 sect->alignment_power = 2;
6325 #endif /* HAVE_WIN32_PSTATUS_T */
6328 elfcore_grok_note (abfd, note)
6330 Elf_Internal_Note *note;
6332 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6340 if (bed->elf_backend_grok_prstatus)
6341 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6343 #if defined (HAVE_PRSTATUS_T)
6344 return elfcore_grok_prstatus (abfd, note);
6349 #if defined (HAVE_PSTATUS_T)
6351 return elfcore_grok_pstatus (abfd, note);
6354 #if defined (HAVE_LWPSTATUS_T)
6356 return elfcore_grok_lwpstatus (abfd, note);
6359 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6360 return elfcore_grok_prfpreg (abfd, note);
6362 #if defined (HAVE_WIN32_PSTATUS_T)
6363 case NT_WIN32PSTATUS:
6364 return elfcore_grok_win32pstatus (abfd, note);
6367 case NT_PRXFPREG: /* Linux SSE extension */
6368 if (note->namesz == 5
6369 && ! strcmp (note->namedata, "LINUX"))
6370 return elfcore_grok_prxfpreg (abfd, note);
6376 if (bed->elf_backend_grok_psinfo)
6377 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6379 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6380 return elfcore_grok_psinfo (abfd, note);
6388 elfcore_netbsd_get_lwpid (note, lwpidp)
6389 Elf_Internal_Note *note;
6394 cp = strchr (note->namedata, '@');
6404 elfcore_grok_netbsd_procinfo (abfd, note)
6406 Elf_Internal_Note *note;
6409 /* Signal number at offset 0x08. */
6410 elf_tdata (abfd)->core_signal
6411 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6413 /* Process ID at offset 0x50. */
6414 elf_tdata (abfd)->core_pid
6415 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6417 /* Command name at 0x7c (max 32 bytes, including nul). */
6418 elf_tdata (abfd)->core_command
6419 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6425 elfcore_grok_netbsd_note (abfd, note)
6427 Elf_Internal_Note *note;
6431 if (elfcore_netbsd_get_lwpid (note, &lwp))
6432 elf_tdata (abfd)->core_lwpid = lwp;
6434 if (note->type == 1)
6436 /* NetBSD-specific core "procinfo". Note that we expect to
6437 find this note before any of the others, which is fine,
6438 since the kernel writes this note out first when it
6439 creates a core file. */
6441 return elfcore_grok_netbsd_procinfo (abfd, note);
6444 /* There are not currently any other machine-independent notes defined
6445 for NetBSD ELF core files. If the note type is less than the start
6446 of the machine-dependent note types, we don't understand it. */
6448 if (note->type < 32)
6452 switch (bfd_get_arch (abfd))
6454 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6455 PT_GETFPREGS == mach+2. */
6457 case bfd_arch_alpha:
6458 case bfd_arch_sparc:
6462 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6465 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6471 /* On all other arch's, PT_GETREGS == mach+1 and
6472 PT_GETFPREGS == mach+3. */
6478 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6481 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6491 elfcore_read_notes (abfd, offset, size)
6502 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
6505 buf = bfd_malloc (size);
6509 if (bfd_bread (buf, size, abfd) != size)
6517 while (p < buf + size)
6519 /* FIXME: bad alignment assumption. */
6520 Elf_External_Note *xnp = (Elf_External_Note *) p;
6521 Elf_Internal_Note in;
6523 in.type = H_GET_32 (abfd, xnp->type);
6525 in.namesz = H_GET_32 (abfd, xnp->namesz);
6526 in.namedata = xnp->name;
6528 in.descsz = H_GET_32 (abfd, xnp->descsz);
6529 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
6530 in.descpos = offset + (in.descdata - buf);
6532 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
6534 if (! elfcore_grok_netbsd_note (abfd, &in))
6539 if (! elfcore_grok_note (abfd, &in))
6543 p = in.descdata + BFD_ALIGN (in.descsz, 4);
6550 /* Providing external access to the ELF program header table. */
6552 /* Return an upper bound on the number of bytes required to store a
6553 copy of ABFD's program header table entries. Return -1 if an error
6554 occurs; bfd_get_error will return an appropriate code. */
6557 bfd_get_elf_phdr_upper_bound (abfd)
6560 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6562 bfd_set_error (bfd_error_wrong_format);
6566 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
6569 /* Copy ABFD's program header table entries to *PHDRS. The entries
6570 will be stored as an array of Elf_Internal_Phdr structures, as
6571 defined in include/elf/internal.h. To find out how large the
6572 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6574 Return the number of program header table entries read, or -1 if an
6575 error occurs; bfd_get_error will return an appropriate code. */
6578 bfd_get_elf_phdrs (abfd, phdrs)
6584 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6586 bfd_set_error (bfd_error_wrong_format);
6590 num_phdrs = elf_elfheader (abfd)->e_phnum;
6591 memcpy (phdrs, elf_tdata (abfd)->phdr,
6592 num_phdrs * sizeof (Elf_Internal_Phdr));
6598 _bfd_elf_sprintf_vma (abfd, buf, value)
6599 bfd *abfd ATTRIBUTE_UNUSED;
6604 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6606 i_ehdrp = elf_elfheader (abfd);
6607 if (i_ehdrp == NULL)
6608 sprintf_vma (buf, value);
6611 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6613 #if BFD_HOST_64BIT_LONG
6614 sprintf (buf, "%016lx", value);
6616 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6617 _bfd_int64_low (value));
6621 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6624 sprintf_vma (buf, value);
6629 _bfd_elf_fprintf_vma (abfd, stream, value)
6630 bfd *abfd ATTRIBUTE_UNUSED;
6635 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6637 i_ehdrp = elf_elfheader (abfd);
6638 if (i_ehdrp == NULL)
6639 fprintf_vma ((FILE *) stream, value);
6642 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6644 #if BFD_HOST_64BIT_LONG
6645 fprintf ((FILE *) stream, "%016lx", value);
6647 fprintf ((FILE *) stream, "%08lx%08lx",
6648 _bfd_int64_high (value), _bfd_int64_low (value));
6652 fprintf ((FILE *) stream, "%08lx",
6653 (unsigned long) (value & 0xffffffff));
6656 fprintf_vma ((FILE *) stream, value);
6660 enum elf_reloc_type_class
6661 _bfd_elf_reloc_type_class (rela)
6662 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
6664 return reloc_class_normal;
6667 /* For RELA architectures, return what the relocation value for
6668 relocation against a local symbol. */
6671 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
6673 Elf_Internal_Sym *sym;
6675 Elf_Internal_Rela *rel;
6679 relocation = (sec->output_section->vma
6680 + sec->output_offset
6682 if ((sec->flags & SEC_MERGE)
6683 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
6684 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6690 _bfd_merged_section_offset (abfd, &msec,
6691 elf_section_data (sec)->sec_info,
6692 sym->st_value + rel->r_addend,
6695 rel->r_addend += msec->output_section->vma + msec->output_offset;
6701 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
6703 Elf_Internal_Sym *sym;
6707 asection *sec = *psec;
6709 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
6710 return sym->st_value + addend;
6712 return _bfd_merged_section_offset (abfd, psec,
6713 elf_section_data (sec)->sec_info,
6714 sym->st_value + addend, (bfd_vma) 0);
6718 _bfd_elf_section_offset (abfd, info, sec, offset)
6720 struct bfd_link_info *info;
6724 struct bfd_elf_section_data *sec_data;
6726 sec_data = elf_section_data (sec);
6727 switch (sec_data->sec_info_type)
6729 case ELF_INFO_TYPE_STABS:
6730 return _bfd_stab_section_offset
6731 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
6733 case ELF_INFO_TYPE_EH_FRAME:
6734 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);