1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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. */
22 /* $FreeBSD: src/contrib/binutils/bfd/elf.c,v 1.3.6.6 2002/09/01 23:43:36 obrien Exp $ */
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
45 #include "libiberty.h"
47 static INLINE struct elf_segment_map *make_mapping
48 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
49 static boolean map_sections_to_segments PARAMS ((bfd *));
50 static int elf_sort_sections PARAMS ((const PTR, const PTR));
51 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
52 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
53 static boolean prep_headers PARAMS ((bfd *));
54 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
55 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
56 static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
57 static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
58 static void merge_sections_remove_hook PARAMS ((bfd *, asection *));
59 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
60 static void set_group_contents PARAMS ((bfd *, asection *, PTR));
61 static boolean assign_section_numbers PARAMS ((bfd *));
62 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
63 static boolean elf_map_symbols PARAMS ((bfd *));
64 static bfd_size_type get_program_header_size PARAMS ((bfd *));
65 static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
66 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
67 bfd_vma, const char **,
69 static int elfcore_make_pid PARAMS ((bfd *));
70 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
71 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
72 Elf_Internal_Note *));
73 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
74 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
75 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
77 static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));
78 static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,
79 Elf_Internal_Note *));
80 static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));
82 /* Swap version information in and out. The version information is
83 currently size independent. If that ever changes, this code will
84 need to move into elfcode.h. */
86 /* Swap in a Verdef structure. */
89 _bfd_elf_swap_verdef_in (abfd, src, dst)
91 const Elf_External_Verdef *src;
92 Elf_Internal_Verdef *dst;
94 dst->vd_version = H_GET_16 (abfd, src->vd_version);
95 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
96 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
97 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
98 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
99 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
100 dst->vd_next = H_GET_32 (abfd, src->vd_next);
103 /* Swap out a Verdef structure. */
106 _bfd_elf_swap_verdef_out (abfd, src, dst)
108 const Elf_Internal_Verdef *src;
109 Elf_External_Verdef *dst;
111 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
112 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
113 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
114 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
115 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
116 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
117 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
120 /* Swap in a Verdaux structure. */
123 _bfd_elf_swap_verdaux_in (abfd, src, dst)
125 const Elf_External_Verdaux *src;
126 Elf_Internal_Verdaux *dst;
128 dst->vda_name = H_GET_32 (abfd, src->vda_name);
129 dst->vda_next = H_GET_32 (abfd, src->vda_next);
132 /* Swap out a Verdaux structure. */
135 _bfd_elf_swap_verdaux_out (abfd, src, dst)
137 const Elf_Internal_Verdaux *src;
138 Elf_External_Verdaux *dst;
140 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
141 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
144 /* Swap in a Verneed structure. */
147 _bfd_elf_swap_verneed_in (abfd, src, dst)
149 const Elf_External_Verneed *src;
150 Elf_Internal_Verneed *dst;
152 dst->vn_version = H_GET_16 (abfd, src->vn_version);
153 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
154 dst->vn_file = H_GET_32 (abfd, src->vn_file);
155 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
156 dst->vn_next = H_GET_32 (abfd, src->vn_next);
159 /* Swap out a Verneed structure. */
162 _bfd_elf_swap_verneed_out (abfd, src, dst)
164 const Elf_Internal_Verneed *src;
165 Elf_External_Verneed *dst;
167 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
168 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
169 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
170 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
171 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
174 /* Swap in a Vernaux structure. */
177 _bfd_elf_swap_vernaux_in (abfd, src, dst)
179 const Elf_External_Vernaux *src;
180 Elf_Internal_Vernaux *dst;
182 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
183 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
184 dst->vna_other = H_GET_16 (abfd, src->vna_other);
185 dst->vna_name = H_GET_32 (abfd, src->vna_name);
186 dst->vna_next = H_GET_32 (abfd, src->vna_next);
189 /* Swap out a Vernaux structure. */
192 _bfd_elf_swap_vernaux_out (abfd, src, dst)
194 const Elf_Internal_Vernaux *src;
195 Elf_External_Vernaux *dst;
197 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
198 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
199 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
200 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
201 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
204 /* Swap in a Versym structure. */
207 _bfd_elf_swap_versym_in (abfd, src, dst)
209 const Elf_External_Versym *src;
210 Elf_Internal_Versym *dst;
212 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
215 /* Swap out a Versym structure. */
218 _bfd_elf_swap_versym_out (abfd, src, dst)
220 const Elf_Internal_Versym *src;
221 Elf_External_Versym *dst;
223 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
226 /* Standard ELF hash function. Do not change this function; you will
227 cause invalid hash tables to be generated. */
230 bfd_elf_hash (namearg)
233 const unsigned char *name = (const unsigned char *) namearg;
238 while ((ch = *name++) != '\0')
241 if ((g = (h & 0xf0000000)) != 0)
244 /* The ELF ABI says `h &= ~g', but this is equivalent in
245 this case and on some machines one insn instead of two. */
252 /* Read a specified number of bytes at a specified offset in an ELF
253 file, into a newly allocated buffer, and return a pointer to the
257 elf_read (abfd, offset, size)
264 if ((buf = bfd_alloc (abfd, size)) == NULL)
266 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
268 if (bfd_bread ((PTR) buf, size, abfd) != size)
270 if (bfd_get_error () != bfd_error_system_call)
271 bfd_set_error (bfd_error_file_truncated);
278 bfd_elf_mkobject (abfd)
281 /* This just does initialization. */
282 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
283 bfd_size_type amt = sizeof (struct elf_obj_tdata);
284 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
285 if (elf_tdata (abfd) == 0)
287 /* Since everything is done at close time, do we need any
294 bfd_elf_mkcorefile (abfd)
297 /* I think this can be done just like an object file. */
298 return bfd_elf_mkobject (abfd);
302 bfd_elf_get_str_section (abfd, shindex)
304 unsigned int shindex;
306 Elf_Internal_Shdr **i_shdrp;
307 char *shstrtab = NULL;
309 bfd_size_type shstrtabsize;
311 i_shdrp = elf_elfsections (abfd);
312 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
315 shstrtab = (char *) i_shdrp[shindex]->contents;
316 if (shstrtab == NULL)
318 /* No cached one, attempt to read, and cache what we read. */
319 offset = i_shdrp[shindex]->sh_offset;
320 shstrtabsize = i_shdrp[shindex]->sh_size;
321 shstrtab = elf_read (abfd, offset, shstrtabsize);
322 i_shdrp[shindex]->contents = (PTR) shstrtab;
328 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
330 unsigned int shindex;
331 unsigned int strindex;
333 Elf_Internal_Shdr *hdr;
338 hdr = elf_elfsections (abfd)[shindex];
340 if (hdr->contents == NULL
341 && bfd_elf_get_str_section (abfd, shindex) == NULL)
344 if (strindex >= hdr->sh_size)
346 (*_bfd_error_handler)
347 (_("%s: invalid string offset %u >= %lu for section `%s'"),
348 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
349 ((shindex == elf_elfheader(abfd)->e_shstrndx
350 && strindex == hdr->sh_name)
352 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
356 return ((char *) hdr->contents) + strindex;
359 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
360 sections. The first element is the flags, the rest are section
363 typedef union elf_internal_group {
364 Elf_Internal_Shdr *shdr;
366 } Elf_Internal_Group;
368 /* Set next_in_group list pointer, and group name for NEWSECT. */
371 setup_group (abfd, hdr, newsect)
373 Elf_Internal_Shdr *hdr;
376 unsigned int num_group = elf_tdata (abfd)->num_group;
378 /* If num_group is zero, read in all SHT_GROUP sections. The count
379 is set to -1 if there are no SHT_GROUP sections. */
382 unsigned int i, shnum;
384 /* First count the number of groups. If we have a SHT_GROUP
385 section with just a flag word (ie. sh_size is 4), ignore it. */
386 shnum = elf_numsections (abfd);
388 for (i = 0; i < shnum; i++)
390 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
391 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
396 num_group = (unsigned) -1;
397 elf_tdata (abfd)->num_group = num_group;
401 /* We keep a list of elf section headers for group sections,
402 so we can find them quickly. */
403 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
404 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
405 if (elf_tdata (abfd)->group_sect_ptr == NULL)
409 for (i = 0; i < shnum; i++)
411 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
412 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
415 Elf_Internal_Group *dest;
417 /* Add to list of sections. */
418 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
421 /* Read the raw contents. */
422 BFD_ASSERT (sizeof (*dest) >= 4);
423 amt = shdr->sh_size * sizeof (*dest) / 4;
424 shdr->contents = bfd_alloc (abfd, amt);
425 if (shdr->contents == NULL
426 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
427 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
431 /* Translate raw contents, a flag word followed by an
432 array of elf section indices all in target byte order,
433 to the flag word followed by an array of elf section
435 src = shdr->contents + shdr->sh_size;
436 dest = (Elf_Internal_Group *) (shdr->contents + amt);
443 idx = H_GET_32 (abfd, src);
444 if (src == shdr->contents)
451 ((*_bfd_error_handler)
452 (_("%s: invalid SHT_GROUP entry"),
453 bfd_archive_filename (abfd)));
456 dest->shdr = elf_elfsections (abfd)[idx];
463 if (num_group != (unsigned) -1)
467 for (i = 0; i < num_group; i++)
469 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
470 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
471 unsigned int n_elt = shdr->sh_size / 4;
473 /* Look through this group's sections to see if current
474 section is a member. */
476 if ((++idx)->shdr == hdr)
480 /* We are a member of this group. Go looking through
481 other members to see if any others are linked via
483 idx = (Elf_Internal_Group *) shdr->contents;
484 n_elt = shdr->sh_size / 4;
486 if ((s = (++idx)->shdr->bfd_section) != NULL
487 && elf_next_in_group (s) != NULL)
491 /* Snarf the group name from other member, and
492 insert current section in circular list. */
493 elf_group_name (newsect) = elf_group_name (s);
494 elf_next_in_group (newsect) = elf_next_in_group (s);
495 elf_next_in_group (s) = newsect;
499 struct elf_backend_data *bed;
501 unsigned char ename[4];
505 /* Humbug. Get the name from the group signature
506 symbol. Why isn't the signature just a string?
507 Fortunately, the name index is at the same
508 place in the external symbol for both 32 and 64
510 bed = get_elf_backend_data (abfd);
511 pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
512 pos += shdr->sh_info * bed->s->sizeof_sym;
513 if (bfd_seek (abfd, pos, SEEK_SET) != 0
514 || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
516 iname = H_GET_32 (abfd, ename);
517 gname = elf_string_from_elf_strtab (abfd, iname);
518 elf_group_name (newsect) = gname;
520 /* Start a circular list with one element. */
521 elf_next_in_group (newsect) = newsect;
523 if (shdr->bfd_section != NULL)
524 elf_next_in_group (shdr->bfd_section) = newsect;
531 if (elf_group_name (newsect) == NULL)
533 (*_bfd_error_handler) (_("%s: no group info for section %s"),
534 bfd_archive_filename (abfd), newsect->name);
539 /* Make a BFD section from an ELF section. We store a pointer to the
540 BFD section in the bfd_section field of the header. */
543 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
545 Elf_Internal_Shdr *hdr;
550 struct elf_backend_data *bed;
552 if (hdr->bfd_section != NULL)
554 BFD_ASSERT (strcmp (name,
555 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
559 newsect = bfd_make_section_anyway (abfd, name);
563 newsect->filepos = hdr->sh_offset;
565 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
566 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
567 || ! bfd_set_section_alignment (abfd, newsect,
568 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
571 flags = SEC_NO_FLAGS;
572 if (hdr->sh_type != SHT_NOBITS)
573 flags |= SEC_HAS_CONTENTS;
574 if (hdr->sh_type == SHT_GROUP)
575 flags |= SEC_GROUP | SEC_EXCLUDE;
576 if ((hdr->sh_flags & SHF_ALLOC) != 0)
579 if (hdr->sh_type != SHT_NOBITS)
582 if ((hdr->sh_flags & SHF_WRITE) == 0)
583 flags |= SEC_READONLY;
584 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
586 else if ((flags & SEC_LOAD) != 0)
588 if ((hdr->sh_flags & SHF_MERGE) != 0)
591 newsect->entsize = hdr->sh_entsize;
592 if ((hdr->sh_flags & SHF_STRINGS) != 0)
593 flags |= SEC_STRINGS;
595 if (hdr->sh_flags & SHF_GROUP)
596 if (!setup_group (abfd, hdr, newsect))
599 /* The debugging sections appear to be recognized only by name, not
602 static const char *debug_sec_names [] =
611 for (i = ARRAY_SIZE (debug_sec_names); i--;)
612 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
616 flags |= SEC_DEBUGGING;
619 /* As a GNU extension, if the name begins with .gnu.linkonce, we
620 only link a single copy of the section. This is used to support
621 g++. g++ will emit each template expansion in its own section.
622 The symbols will be defined as weak, so that multiple definitions
623 are permitted. The GNU linker extension is to actually discard
624 all but one of the sections. */
625 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
626 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
628 bed = get_elf_backend_data (abfd);
629 if (bed->elf_backend_section_flags)
630 if (! bed->elf_backend_section_flags (&flags, hdr))
633 if (! bfd_set_section_flags (abfd, newsect, flags))
636 if ((flags & SEC_ALLOC) != 0)
638 Elf_Internal_Phdr *phdr;
641 /* Look through the phdrs to see if we need to adjust the lma.
642 If all the p_paddr fields are zero, we ignore them, since
643 some ELF linkers produce such output. */
644 phdr = elf_tdata (abfd)->phdr;
645 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
647 if (phdr->p_paddr != 0)
650 if (i < elf_elfheader (abfd)->e_phnum)
652 phdr = elf_tdata (abfd)->phdr;
653 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
655 /* This section is part of this segment if its file
656 offset plus size lies within the segment's memory
657 span and, if the section is loaded, the extent of the
658 loaded data lies within the extent of the segment.
660 Note - we used to check the p_paddr field as well, and
661 refuse to set the LMA if it was 0. This is wrong
662 though, as a perfectly valid initialised segment can
663 have a p_paddr of zero. Some architectures, eg ARM,
664 place special significance on the address 0 and
665 executables need to be able to have a segment which
666 covers this address. */
667 if (phdr->p_type == PT_LOAD
668 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
669 && (hdr->sh_offset + hdr->sh_size
670 <= phdr->p_offset + phdr->p_memsz)
671 && ((flags & SEC_LOAD) == 0
672 || (hdr->sh_offset + hdr->sh_size
673 <= phdr->p_offset + phdr->p_filesz)))
675 if ((flags & SEC_LOAD) == 0)
676 newsect->lma = (phdr->p_paddr
677 + hdr->sh_addr - phdr->p_vaddr);
679 /* We used to use the same adjustment for SEC_LOAD
680 sections, but that doesn't work if the segment
681 is packed with code from multiple VMAs.
682 Instead we calculate the section LMA based on
683 the segment LMA. It is assumed that the
684 segment will contain sections with contiguous
685 LMAs, even if the VMAs are not. */
686 newsect->lma = (phdr->p_paddr
687 + hdr->sh_offset - phdr->p_offset);
689 /* With contiguous segments, we can't tell from file
690 offsets whether a section with zero size should
691 be placed at the end of one segment or the
692 beginning of the next. Decide based on vaddr. */
693 if (hdr->sh_addr >= phdr->p_vaddr
694 && (hdr->sh_addr + hdr->sh_size
695 <= phdr->p_vaddr + phdr->p_memsz))
702 hdr->bfd_section = newsect;
703 elf_section_data (newsect)->this_hdr = *hdr;
713 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
716 Helper functions for GDB to locate the string tables.
717 Since BFD hides string tables from callers, GDB needs to use an
718 internal hook to find them. Sun's .stabstr, in particular,
719 isn't even pointed to by the .stab section, so ordinary
720 mechanisms wouldn't work to find it, even if we had some.
723 struct elf_internal_shdr *
724 bfd_elf_find_section (abfd, name)
728 Elf_Internal_Shdr **i_shdrp;
733 i_shdrp = elf_elfsections (abfd);
736 shstrtab = bfd_elf_get_str_section (abfd,
737 elf_elfheader (abfd)->e_shstrndx);
738 if (shstrtab != NULL)
740 max = elf_numsections (abfd);
741 for (i = 1; i < max; i++)
742 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
749 const char *const bfd_elf_section_type_names[] = {
750 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
751 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
752 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
755 /* ELF relocs are against symbols. If we are producing relocateable
756 output, and the reloc is against an external symbol, and nothing
757 has given us any additional addend, the resulting reloc will also
758 be against the same symbol. In such a case, we don't want to
759 change anything about the way the reloc is handled, since it will
760 all be done at final link time. Rather than put special case code
761 into bfd_perform_relocation, all the reloc types use this howto
762 function. It just short circuits the reloc if producing
763 relocateable output against an external symbol. */
765 bfd_reloc_status_type
766 bfd_elf_generic_reloc (abfd,
773 bfd *abfd ATTRIBUTE_UNUSED;
774 arelent *reloc_entry;
776 PTR data ATTRIBUTE_UNUSED;
777 asection *input_section;
779 char **error_message ATTRIBUTE_UNUSED;
781 if (output_bfd != (bfd *) NULL
782 && (symbol->flags & BSF_SECTION_SYM) == 0
783 && (! reloc_entry->howto->partial_inplace
784 || reloc_entry->addend == 0))
786 reloc_entry->address += input_section->output_offset;
790 return bfd_reloc_continue;
793 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
796 merge_sections_remove_hook (abfd, sec)
797 bfd *abfd ATTRIBUTE_UNUSED;
800 struct bfd_elf_section_data *sec_data;
802 sec_data = elf_section_data (sec);
803 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
804 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
807 /* Finish SHF_MERGE section merging. */
810 _bfd_elf_merge_sections (abfd, info)
812 struct bfd_link_info *info;
814 if (!is_elf_hash_table (info))
816 if (elf_hash_table (info)->merge_info)
817 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
818 merge_sections_remove_hook);
822 /* Copy the program header and other data from one object module to
826 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
830 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
831 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
834 BFD_ASSERT (!elf_flags_init (obfd)
835 || (elf_elfheader (obfd)->e_flags
836 == elf_elfheader (ibfd)->e_flags));
838 elf_gp (obfd) = elf_gp (ibfd);
839 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
840 elf_flags_init (obfd) = true;
844 /* Print out the program headers. */
847 _bfd_elf_print_private_bfd_data (abfd, farg)
851 FILE *f = (FILE *) farg;
852 Elf_Internal_Phdr *p;
854 bfd_byte *dynbuf = NULL;
856 p = elf_tdata (abfd)->phdr;
861 fprintf (f, _("\nProgram Header:\n"));
862 c = elf_elfheader (abfd)->e_phnum;
863 for (i = 0; i < c; i++, p++)
870 case PT_NULL: pt = "NULL"; break;
871 case PT_LOAD: pt = "LOAD"; break;
872 case PT_DYNAMIC: pt = "DYNAMIC"; break;
873 case PT_INTERP: pt = "INTERP"; break;
874 case PT_NOTE: pt = "NOTE"; break;
875 case PT_SHLIB: pt = "SHLIB"; break;
876 case PT_PHDR: pt = "PHDR"; break;
877 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
878 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
880 fprintf (f, "%8s off 0x", pt);
881 bfd_fprintf_vma (abfd, f, p->p_offset);
882 fprintf (f, " vaddr 0x");
883 bfd_fprintf_vma (abfd, f, p->p_vaddr);
884 fprintf (f, " paddr 0x");
885 bfd_fprintf_vma (abfd, f, p->p_paddr);
886 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
887 fprintf (f, " filesz 0x");
888 bfd_fprintf_vma (abfd, f, p->p_filesz);
889 fprintf (f, " memsz 0x");
890 bfd_fprintf_vma (abfd, f, p->p_memsz);
891 fprintf (f, " flags %c%c%c",
892 (p->p_flags & PF_R) != 0 ? 'r' : '-',
893 (p->p_flags & PF_W) != 0 ? 'w' : '-',
894 (p->p_flags & PF_X) != 0 ? 'x' : '-');
895 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
896 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
901 s = bfd_get_section_by_name (abfd, ".dynamic");
905 unsigned long shlink;
906 bfd_byte *extdyn, *extdynend;
908 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
910 fprintf (f, _("\nDynamic Section:\n"));
912 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
915 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
919 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
922 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
924 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
925 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
928 extdynend = extdyn + s->_raw_size;
929 for (; extdyn < extdynend; extdyn += extdynsize)
931 Elf_Internal_Dyn dyn;
936 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
938 if (dyn.d_tag == DT_NULL)
945 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
949 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
950 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
951 case DT_PLTGOT: name = "PLTGOT"; break;
952 case DT_HASH: name = "HASH"; break;
953 case DT_STRTAB: name = "STRTAB"; break;
954 case DT_SYMTAB: name = "SYMTAB"; break;
955 case DT_RELA: name = "RELA"; break;
956 case DT_RELASZ: name = "RELASZ"; break;
957 case DT_RELAENT: name = "RELAENT"; break;
958 case DT_STRSZ: name = "STRSZ"; break;
959 case DT_SYMENT: name = "SYMENT"; break;
960 case DT_INIT: name = "INIT"; break;
961 case DT_FINI: name = "FINI"; break;
962 case DT_SONAME: name = "SONAME"; stringp = true; break;
963 case DT_RPATH: name = "RPATH"; stringp = true; break;
964 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
965 case DT_REL: name = "REL"; break;
966 case DT_RELSZ: name = "RELSZ"; break;
967 case DT_RELENT: name = "RELENT"; break;
968 case DT_PLTREL: name = "PLTREL"; break;
969 case DT_DEBUG: name = "DEBUG"; break;
970 case DT_TEXTREL: name = "TEXTREL"; break;
971 case DT_JMPREL: name = "JMPREL"; break;
972 case DT_BIND_NOW: name = "BIND_NOW"; break;
973 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
974 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
975 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
976 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
977 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
978 case DT_FLAGS: name = "FLAGS"; break;
979 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
980 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
981 case DT_CHECKSUM: name = "CHECKSUM"; break;
982 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
983 case DT_MOVEENT: name = "MOVEENT"; break;
984 case DT_MOVESZ: name = "MOVESZ"; break;
985 case DT_FEATURE: name = "FEATURE"; break;
986 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
987 case DT_SYMINSZ: name = "SYMINSZ"; break;
988 case DT_SYMINENT: name = "SYMINENT"; break;
989 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
990 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
991 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
992 case DT_PLTPAD: name = "PLTPAD"; break;
993 case DT_MOVETAB: name = "MOVETAB"; break;
994 case DT_SYMINFO: name = "SYMINFO"; break;
995 case DT_RELACOUNT: name = "RELACOUNT"; break;
996 case DT_RELCOUNT: name = "RELCOUNT"; break;
997 case DT_FLAGS_1: name = "FLAGS_1"; break;
998 case DT_VERSYM: name = "VERSYM"; break;
999 case DT_VERDEF: name = "VERDEF"; break;
1000 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1001 case DT_VERNEED: name = "VERNEED"; break;
1002 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1003 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1004 case DT_USED: name = "USED"; break;
1005 case DT_FILTER: name = "FILTER"; stringp = true; break;
1008 fprintf (f, " %-11s ", name);
1010 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1014 unsigned int tagv = dyn.d_un.d_val;
1016 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1019 fprintf (f, "%s", string);
1028 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1029 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1031 if (! _bfd_elf_slurp_version_tables (abfd))
1035 if (elf_dynverdef (abfd) != 0)
1037 Elf_Internal_Verdef *t;
1039 fprintf (f, _("\nVersion definitions:\n"));
1040 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1042 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1043 t->vd_flags, t->vd_hash, t->vd_nodename);
1044 if (t->vd_auxptr->vda_nextptr != NULL)
1046 Elf_Internal_Verdaux *a;
1049 for (a = t->vd_auxptr->vda_nextptr;
1052 fprintf (f, "%s ", a->vda_nodename);
1058 if (elf_dynverref (abfd) != 0)
1060 Elf_Internal_Verneed *t;
1062 fprintf (f, _("\nVersion References:\n"));
1063 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1065 Elf_Internal_Vernaux *a;
1067 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1068 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1069 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1070 a->vna_flags, a->vna_other, a->vna_nodename);
1082 /* Display ELF-specific fields of a symbol. */
1085 bfd_elf_print_symbol (abfd, filep, symbol, how)
1089 bfd_print_symbol_type how;
1091 FILE *file = (FILE *) filep;
1094 case bfd_print_symbol_name:
1095 fprintf (file, "%s", symbol->name);
1097 case bfd_print_symbol_more:
1098 fprintf (file, "elf ");
1099 bfd_fprintf_vma (abfd, file, symbol->value);
1100 fprintf (file, " %lx", (long) symbol->flags);
1102 case bfd_print_symbol_all:
1104 const char *section_name;
1105 const char *name = NULL;
1106 struct elf_backend_data *bed;
1107 unsigned char st_other;
1110 section_name = symbol->section ? symbol->section->name : "(*none*)";
1112 bed = get_elf_backend_data (abfd);
1113 if (bed->elf_backend_print_symbol_all)
1114 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1118 name = symbol->name;
1119 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1122 fprintf (file, " %s\t", section_name);
1123 /* Print the "other" value for a symbol. For common symbols,
1124 we've already printed the size; now print the alignment.
1125 For other symbols, we have no specified alignment, and
1126 we've printed the address; now print the size. */
1127 if (bfd_is_com_section (symbol->section))
1128 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1130 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1131 bfd_fprintf_vma (abfd, file, val);
1133 /* If we have version information, print it. */
1134 if (elf_tdata (abfd)->dynversym_section != 0
1135 && (elf_tdata (abfd)->dynverdef_section != 0
1136 || elf_tdata (abfd)->dynverref_section != 0))
1138 unsigned int vernum;
1139 const char *version_string;
1141 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1144 version_string = "";
1145 else if (vernum == 1)
1146 version_string = "Base";
1147 else if (vernum <= elf_tdata (abfd)->cverdefs)
1149 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1152 Elf_Internal_Verneed *t;
1154 version_string = "";
1155 for (t = elf_tdata (abfd)->verref;
1159 Elf_Internal_Vernaux *a;
1161 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1163 if (a->vna_other == vernum)
1165 version_string = a->vna_nodename;
1172 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1173 fprintf (file, " %-11s", version_string);
1178 fprintf (file, " (%s)", version_string);
1179 for (i = 10 - strlen (version_string); i > 0; --i)
1184 /* If the st_other field is not zero, print it. */
1185 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1190 case STV_INTERNAL: fprintf (file, " .internal"); break;
1191 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1192 case STV_PROTECTED: fprintf (file, " .protected"); break;
1194 /* Some other non-defined flags are also present, so print
1196 fprintf (file, " 0x%02x", (unsigned int) st_other);
1199 fprintf (file, " %s", name);
1205 /* Create an entry in an ELF linker hash table. */
1207 struct bfd_hash_entry *
1208 _bfd_elf_link_hash_newfunc (entry, table, string)
1209 struct bfd_hash_entry *entry;
1210 struct bfd_hash_table *table;
1213 /* Allocate the structure if it has not already been allocated by a
1217 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1222 /* Call the allocation method of the superclass. */
1223 entry = _bfd_link_hash_newfunc (entry, table, string);
1226 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1227 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1229 /* Set local fields. */
1233 ret->dynstr_index = 0;
1234 ret->weakdef = NULL;
1235 ret->got.refcount = htab->init_refcount;
1236 ret->plt.refcount = htab->init_refcount;
1237 ret->linker_section_pointer = NULL;
1238 ret->verinfo.verdef = NULL;
1239 ret->vtable_entries_used = NULL;
1240 ret->vtable_entries_size = 0;
1241 ret->vtable_parent = NULL;
1242 ret->type = STT_NOTYPE;
1244 /* Assume that we have been called by a non-ELF symbol reader.
1245 This flag is then reset by the code which reads an ELF input
1246 file. This ensures that a symbol created by a non-ELF symbol
1247 reader will have the flag set correctly. */
1248 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1254 /* Copy data from an indirect symbol to its direct symbol, hiding the
1255 old indirect symbol. Also used for copying flags to a weakdef. */
1258 _bfd_elf_link_hash_copy_indirect (dir, ind)
1259 struct elf_link_hash_entry *dir, *ind;
1263 /* Copy down any references that we may have already seen to the
1264 symbol which just became indirect. */
1266 dir->elf_link_hash_flags |=
1267 (ind->elf_link_hash_flags
1268 & (ELF_LINK_HASH_REF_DYNAMIC
1269 | ELF_LINK_HASH_REF_REGULAR
1270 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1271 | ELF_LINK_NON_GOT_REF));
1273 if (ind->root.type != bfd_link_hash_indirect)
1276 /* Copy over the global and procedure linkage table refcount entries.
1277 These may have been already set up by a check_relocs routine. */
1278 tmp = dir->got.refcount;
1281 dir->got.refcount = ind->got.refcount;
1282 ind->got.refcount = tmp;
1285 BFD_ASSERT (ind->got.refcount <= 0);
1287 tmp = dir->plt.refcount;
1290 dir->plt.refcount = ind->plt.refcount;
1291 ind->plt.refcount = tmp;
1294 BFD_ASSERT (ind->plt.refcount <= 0);
1296 if (dir->dynindx == -1)
1298 dir->dynindx = ind->dynindx;
1299 dir->dynstr_index = ind->dynstr_index;
1301 ind->dynstr_index = 0;
1304 BFD_ASSERT (ind->dynindx == -1);
1308 _bfd_elf_link_hash_hide_symbol (info, h, force_local)
1309 struct bfd_link_info *info;
1310 struct elf_link_hash_entry *h;
1311 boolean force_local;
1313 h->plt.offset = (bfd_vma) -1;
1314 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1317 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1318 if (h->dynindx != -1)
1321 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1327 /* Initialize an ELF linker hash table. */
1330 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1331 struct elf_link_hash_table *table;
1333 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1334 struct bfd_hash_table *,
1339 table->dynamic_sections_created = false;
1340 table->dynobj = NULL;
1341 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
1342 /* The first dynamic symbol is a dummy. */
1343 table->dynsymcount = 1;
1344 table->dynstr = NULL;
1345 table->bucketcount = 0;
1346 table->needed = NULL;
1347 table->runpath = NULL;
1349 table->stab_info = NULL;
1350 table->merge_info = NULL;
1351 table->dynlocal = NULL;
1352 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1353 table->root.type = bfd_link_elf_hash_table;
1358 /* Create an ELF linker hash table. */
1360 struct bfd_link_hash_table *
1361 _bfd_elf_link_hash_table_create (abfd)
1364 struct elf_link_hash_table *ret;
1365 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1367 ret = (struct elf_link_hash_table *) bfd_alloc (abfd, amt);
1368 if (ret == (struct elf_link_hash_table *) NULL)
1371 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1373 bfd_release (abfd, ret);
1380 /* This is a hook for the ELF emulation code in the generic linker to
1381 tell the backend linker what file name to use for the DT_NEEDED
1382 entry for a dynamic object. The generic linker passes name as an
1383 empty string to indicate that no DT_NEEDED entry should be made. */
1386 bfd_elf_set_dt_needed_name (abfd, name)
1390 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1391 && bfd_get_format (abfd) == bfd_object)
1392 elf_dt_name (abfd) = name;
1396 bfd_elf_set_dt_needed_soname (abfd, name)
1400 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1401 && bfd_get_format (abfd) == bfd_object)
1402 elf_dt_soname (abfd) = name;
1405 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1406 the linker ELF emulation code. */
1408 struct bfd_link_needed_list *
1409 bfd_elf_get_needed_list (abfd, info)
1410 bfd *abfd ATTRIBUTE_UNUSED;
1411 struct bfd_link_info *info;
1413 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1415 return elf_hash_table (info)->needed;
1418 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1419 hook for the linker ELF emulation code. */
1421 struct bfd_link_needed_list *
1422 bfd_elf_get_runpath_list (abfd, info)
1423 bfd *abfd ATTRIBUTE_UNUSED;
1424 struct bfd_link_info *info;
1426 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1428 return elf_hash_table (info)->runpath;
1431 /* Get the name actually used for a dynamic object for a link. This
1432 is the SONAME entry if there is one. Otherwise, it is the string
1433 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1436 bfd_elf_get_dt_soname (abfd)
1439 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1440 && bfd_get_format (abfd) == bfd_object)
1441 return elf_dt_name (abfd);
1445 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1446 the ELF linker emulation code. */
1449 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1451 struct bfd_link_needed_list **pneeded;
1454 bfd_byte *dynbuf = NULL;
1456 unsigned long shlink;
1457 bfd_byte *extdyn, *extdynend;
1459 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1463 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1464 || bfd_get_format (abfd) != bfd_object)
1467 s = bfd_get_section_by_name (abfd, ".dynamic");
1468 if (s == NULL || s->_raw_size == 0)
1471 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1475 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1479 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1483 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1485 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1486 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1489 extdynend = extdyn + s->_raw_size;
1490 for (; extdyn < extdynend; extdyn += extdynsize)
1492 Elf_Internal_Dyn dyn;
1494 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1496 if (dyn.d_tag == DT_NULL)
1499 if (dyn.d_tag == DT_NEEDED)
1502 struct bfd_link_needed_list *l;
1503 unsigned int tagv = dyn.d_un.d_val;
1506 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1511 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1532 /* Allocate an ELF string table--force the first byte to be zero. */
1534 struct bfd_strtab_hash *
1535 _bfd_elf_stringtab_init ()
1537 struct bfd_strtab_hash *ret;
1539 ret = _bfd_stringtab_init ();
1544 loc = _bfd_stringtab_add (ret, "", true, false);
1545 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1546 if (loc == (bfd_size_type) -1)
1548 _bfd_stringtab_free (ret);
1555 /* ELF .o/exec file reading */
1557 /* Create a new bfd section from an ELF section header. */
1560 bfd_section_from_shdr (abfd, shindex)
1562 unsigned int shindex;
1564 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1565 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1566 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1569 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1571 switch (hdr->sh_type)
1574 /* Inactive section. Throw it away. */
1577 case SHT_PROGBITS: /* Normal section with contents. */
1578 case SHT_DYNAMIC: /* Dynamic linking information. */
1579 case SHT_NOBITS: /* .bss section. */
1580 case SHT_HASH: /* .hash section. */
1581 case SHT_NOTE: /* .note section. */
1582 case SHT_INIT_ARRAY: /* .init_array section. */
1583 case SHT_FINI_ARRAY: /* .fini_array section. */
1584 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1585 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1587 case SHT_SYMTAB: /* A symbol table */
1588 if (elf_onesymtab (abfd) == shindex)
1591 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1592 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1593 elf_onesymtab (abfd) = shindex;
1594 elf_tdata (abfd)->symtab_hdr = *hdr;
1595 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1596 abfd->flags |= HAS_SYMS;
1598 /* Sometimes a shared object will map in the symbol table. If
1599 SHF_ALLOC is set, and this is a shared object, then we also
1600 treat this section as a BFD section. We can not base the
1601 decision purely on SHF_ALLOC, because that flag is sometimes
1602 set in a relocateable object file, which would confuse the
1604 if ((hdr->sh_flags & SHF_ALLOC) != 0
1605 && (abfd->flags & DYNAMIC) != 0
1606 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1611 case SHT_DYNSYM: /* A dynamic symbol table */
1612 if (elf_dynsymtab (abfd) == shindex)
1615 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1616 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1617 elf_dynsymtab (abfd) = shindex;
1618 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1619 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1620 abfd->flags |= HAS_SYMS;
1622 /* Besides being a symbol table, we also treat this as a regular
1623 section, so that objcopy can handle it. */
1624 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1626 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1627 if (elf_symtab_shndx (abfd) == shindex)
1630 /* Get the associated symbol table. */
1631 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1632 || hdr->sh_link != elf_onesymtab (abfd))
1635 elf_symtab_shndx (abfd) = shindex;
1636 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1637 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1640 case SHT_STRTAB: /* A string table */
1641 if (hdr->bfd_section != NULL)
1643 if (ehdr->e_shstrndx == shindex)
1645 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1646 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1650 unsigned int i, num_sec;
1652 num_sec = elf_numsections (abfd);
1653 for (i = 1; i < num_sec; i++)
1655 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1656 if (hdr2->sh_link == shindex)
1658 if (! bfd_section_from_shdr (abfd, i))
1660 if (elf_onesymtab (abfd) == i)
1662 elf_tdata (abfd)->strtab_hdr = *hdr;
1663 elf_elfsections (abfd)[shindex] =
1664 &elf_tdata (abfd)->strtab_hdr;
1667 if (elf_dynsymtab (abfd) == i)
1669 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1670 elf_elfsections (abfd)[shindex] = hdr =
1671 &elf_tdata (abfd)->dynstrtab_hdr;
1672 /* We also treat this as a regular section, so
1673 that objcopy can handle it. */
1676 #if 0 /* Not handling other string tables specially right now. */
1677 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1678 /* We have a strtab for some random other section. */
1679 newsect = (asection *) hdr2->bfd_section;
1682 hdr->bfd_section = newsect;
1683 hdr2 = &elf_section_data (newsect)->str_hdr;
1685 elf_elfsections (abfd)[shindex] = hdr2;
1691 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1695 /* *These* do a lot of work -- but build no sections! */
1697 asection *target_sect;
1698 Elf_Internal_Shdr *hdr2;
1699 unsigned int num_sec = elf_numsections (abfd);
1701 /* Check for a bogus link to avoid crashing. */
1702 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1703 || hdr->sh_link >= num_sec)
1705 ((*_bfd_error_handler)
1706 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1707 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1708 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1711 /* For some incomprehensible reason Oracle distributes
1712 libraries for Solaris in which some of the objects have
1713 bogus sh_link fields. It would be nice if we could just
1714 reject them, but, unfortunately, some people need to use
1715 them. We scan through the section headers; if we find only
1716 one suitable symbol table, we clobber the sh_link to point
1717 to it. I hope this doesn't break anything. */
1718 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1719 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1725 for (scan = 1; scan < num_sec; scan++)
1727 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1728 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1739 hdr->sh_link = found;
1742 /* Get the symbol table. */
1743 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1744 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1747 /* If this reloc section does not use the main symbol table we
1748 don't treat it as a reloc section. BFD can't adequately
1749 represent such a section, so at least for now, we don't
1750 try. We just present it as a normal section. We also
1751 can't use it as a reloc section if it points to the null
1753 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1754 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1756 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1758 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1759 if (target_sect == NULL)
1762 if ((target_sect->flags & SEC_RELOC) == 0
1763 || target_sect->reloc_count == 0)
1764 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1768 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1769 amt = sizeof (*hdr2);
1770 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1771 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1774 elf_elfsections (abfd)[shindex] = hdr2;
1775 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1776 target_sect->flags |= SEC_RELOC;
1777 target_sect->relocation = NULL;
1778 target_sect->rel_filepos = hdr->sh_offset;
1779 /* In the section to which the relocations apply, mark whether
1780 its relocations are of the REL or RELA variety. */
1781 if (hdr->sh_size != 0)
1782 elf_section_data (target_sect)->use_rela_p
1783 = (hdr->sh_type == SHT_RELA);
1784 abfd->flags |= HAS_RELOC;
1789 case SHT_GNU_verdef:
1790 elf_dynverdef (abfd) = shindex;
1791 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1792 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1795 case SHT_GNU_versym:
1796 elf_dynversym (abfd) = shindex;
1797 elf_tdata (abfd)->dynversym_hdr = *hdr;
1798 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1801 case SHT_GNU_verneed:
1802 elf_dynverref (abfd) = shindex;
1803 elf_tdata (abfd)->dynverref_hdr = *hdr;
1804 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1811 /* Make a section for objcopy and relocatable links. */
1812 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1814 if (hdr->contents != NULL)
1816 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1817 unsigned int n_elt = hdr->sh_size / 4;
1820 while (--n_elt != 0)
1821 if ((s = (++idx)->shdr->bfd_section) != NULL
1822 && elf_next_in_group (s) != NULL)
1824 elf_next_in_group (hdr->bfd_section) = s;
1831 /* Check for any processor-specific section types. */
1833 if (bed->elf_backend_section_from_shdr)
1834 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1842 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1843 Return SEC for sections that have no elf section, and NULL on error. */
1846 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
1848 struct sym_sec_cache *cache;
1850 unsigned long r_symndx;
1852 unsigned char esym_shndx[4];
1853 unsigned int isym_shndx;
1854 Elf_Internal_Shdr *symtab_hdr;
1857 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1859 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1860 return cache->sec[ent];
1862 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1863 pos = symtab_hdr->sh_offset;
1864 if (get_elf_backend_data (abfd)->s->sizeof_sym
1865 == sizeof (Elf64_External_Sym))
1867 pos += r_symndx * sizeof (Elf64_External_Sym);
1868 pos += offsetof (Elf64_External_Sym, st_shndx);
1869 amt = sizeof (((Elf64_External_Sym *) 0)->st_shndx);
1873 pos += r_symndx * sizeof (Elf32_External_Sym);
1874 pos += offsetof (Elf32_External_Sym, st_shndx);
1875 amt = sizeof (((Elf32_External_Sym *) 0)->st_shndx);
1877 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1878 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1880 isym_shndx = H_GET_16 (abfd, esym_shndx);
1882 if (isym_shndx == SHN_XINDEX)
1884 Elf_Internal_Shdr *shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1885 if (shndx_hdr->sh_size != 0)
1887 pos = shndx_hdr->sh_offset;
1888 pos += r_symndx * sizeof (Elf_External_Sym_Shndx);
1889 amt = sizeof (Elf_External_Sym_Shndx);
1890 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1891 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1893 isym_shndx = H_GET_32 (abfd, esym_shndx);
1897 if (cache->abfd != abfd)
1899 memset (cache->indx, -1, sizeof (cache->indx));
1902 cache->indx[ent] = r_symndx;
1903 cache->sec[ent] = sec;
1904 if (isym_shndx < SHN_LORESERVE || isym_shndx > SHN_HIRESERVE)
1907 s = bfd_section_from_elf_index (abfd, isym_shndx);
1909 cache->sec[ent] = s;
1911 return cache->sec[ent];
1914 /* Given an ELF section number, retrieve the corresponding BFD
1918 bfd_section_from_elf_index (abfd, index)
1922 if (index >= elf_numsections (abfd))
1924 return elf_elfsections (abfd)[index]->bfd_section;
1928 _bfd_elf_new_section_hook (abfd, sec)
1932 struct bfd_elf_section_data *sdata;
1933 bfd_size_type amt = sizeof (*sdata);
1935 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
1938 sec->used_by_bfd = (PTR) sdata;
1940 /* Indicate whether or not this section should use RELA relocations. */
1942 = get_elf_backend_data (abfd)->default_use_rela_p;
1947 /* Create a new bfd section from an ELF program header.
1949 Since program segments have no names, we generate a synthetic name
1950 of the form segment<NUM>, where NUM is generally the index in the
1951 program header table. For segments that are split (see below) we
1952 generate the names segment<NUM>a and segment<NUM>b.
1954 Note that some program segments may have a file size that is different than
1955 (less than) the memory size. All this means is that at execution the
1956 system must allocate the amount of memory specified by the memory size,
1957 but only initialize it with the first "file size" bytes read from the
1958 file. This would occur for example, with program segments consisting
1959 of combined data+bss.
1961 To handle the above situation, this routine generates TWO bfd sections
1962 for the single program segment. The first has the length specified by
1963 the file size of the segment, and the second has the length specified
1964 by the difference between the two sizes. In effect, the segment is split
1965 into it's initialized and uninitialized parts.
1970 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1972 Elf_Internal_Phdr *hdr;
1974 const char *typename;
1981 split = ((hdr->p_memsz > 0)
1982 && (hdr->p_filesz > 0)
1983 && (hdr->p_memsz > hdr->p_filesz));
1984 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1985 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1988 strcpy (name, namebuf);
1989 newsect = bfd_make_section (abfd, name);
1990 if (newsect == NULL)
1992 newsect->vma = hdr->p_vaddr;
1993 newsect->lma = hdr->p_paddr;
1994 newsect->_raw_size = hdr->p_filesz;
1995 newsect->filepos = hdr->p_offset;
1996 newsect->flags |= SEC_HAS_CONTENTS;
1997 if (hdr->p_type == PT_LOAD)
1999 newsect->flags |= SEC_ALLOC;
2000 newsect->flags |= SEC_LOAD;
2001 if (hdr->p_flags & PF_X)
2003 /* FIXME: all we known is that it has execute PERMISSION,
2005 newsect->flags |= SEC_CODE;
2008 if (!(hdr->p_flags & PF_W))
2010 newsect->flags |= SEC_READONLY;
2015 sprintf (namebuf, "%s%db", typename, index);
2016 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
2019 strcpy (name, namebuf);
2020 newsect = bfd_make_section (abfd, name);
2021 if (newsect == NULL)
2023 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2024 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2025 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2026 if (hdr->p_type == PT_LOAD)
2028 newsect->flags |= SEC_ALLOC;
2029 if (hdr->p_flags & PF_X)
2030 newsect->flags |= SEC_CODE;
2032 if (!(hdr->p_flags & PF_W))
2033 newsect->flags |= SEC_READONLY;
2040 bfd_section_from_phdr (abfd, hdr, index)
2042 Elf_Internal_Phdr *hdr;
2045 struct elf_backend_data *bed;
2047 switch (hdr->p_type)
2050 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2053 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2056 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2059 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2062 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2064 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2069 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2072 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2075 /* Check for any processor-specific program segment types.
2076 If no handler for them, default to making "segment" sections. */
2077 bed = get_elf_backend_data (abfd);
2078 if (bed->elf_backend_section_from_phdr)
2079 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2081 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2085 /* Initialize REL_HDR, the section-header for new section, containing
2086 relocations against ASECT. If USE_RELA_P is true, we use RELA
2087 relocations; otherwise, we use REL relocations. */
2090 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2092 Elf_Internal_Shdr *rel_hdr;
2097 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2098 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2100 name = bfd_alloc (abfd, amt);
2103 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2105 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2107 if (rel_hdr->sh_name == (unsigned int) -1)
2109 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2110 rel_hdr->sh_entsize = (use_rela_p
2111 ? bed->s->sizeof_rela
2112 : bed->s->sizeof_rel);
2113 rel_hdr->sh_addralign = bed->s->file_align;
2114 rel_hdr->sh_flags = 0;
2115 rel_hdr->sh_addr = 0;
2116 rel_hdr->sh_size = 0;
2117 rel_hdr->sh_offset = 0;
2122 /* Set up an ELF internal section header for a section. */
2125 elf_fake_sections (abfd, asect, failedptrarg)
2130 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2131 boolean *failedptr = (boolean *) failedptrarg;
2132 Elf_Internal_Shdr *this_hdr;
2136 /* We already failed; just get out of the bfd_map_over_sections
2141 this_hdr = &elf_section_data (asect)->this_hdr;
2143 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2144 asect->name, false);
2145 if (this_hdr->sh_name == (unsigned long) -1)
2151 this_hdr->sh_flags = 0;
2153 if ((asect->flags & SEC_ALLOC) != 0
2154 || asect->user_set_vma)
2155 this_hdr->sh_addr = asect->vma;
2157 this_hdr->sh_addr = 0;
2159 this_hdr->sh_offset = 0;
2160 this_hdr->sh_size = asect->_raw_size;
2161 this_hdr->sh_link = 0;
2162 this_hdr->sh_addralign = 1 << asect->alignment_power;
2163 /* The sh_entsize and sh_info fields may have been set already by
2164 copy_private_section_data. */
2166 this_hdr->bfd_section = asect;
2167 this_hdr->contents = NULL;
2169 /* FIXME: This should not be based on section names. */
2170 if (strcmp (asect->name, ".dynstr") == 0)
2171 this_hdr->sh_type = SHT_STRTAB;
2172 else if (strcmp (asect->name, ".hash") == 0)
2174 this_hdr->sh_type = SHT_HASH;
2175 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2177 else if (strcmp (asect->name, ".dynsym") == 0)
2179 this_hdr->sh_type = SHT_DYNSYM;
2180 this_hdr->sh_entsize = bed->s->sizeof_sym;
2182 else if (strcmp (asect->name, ".dynamic") == 0)
2184 this_hdr->sh_type = SHT_DYNAMIC;
2185 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2187 else if (strncmp (asect->name, ".rela", 5) == 0
2188 && get_elf_backend_data (abfd)->may_use_rela_p)
2190 this_hdr->sh_type = SHT_RELA;
2191 this_hdr->sh_entsize = bed->s->sizeof_rela;
2193 else if (strncmp (asect->name, ".rel", 4) == 0
2194 && get_elf_backend_data (abfd)->may_use_rel_p)
2196 this_hdr->sh_type = SHT_REL;
2197 this_hdr->sh_entsize = bed->s->sizeof_rel;
2199 else if (strcmp (asect->name, ".init_array") == 0)
2200 this_hdr->sh_type = SHT_INIT_ARRAY;
2201 else if (strcmp (asect->name, ".fini_array") == 0)
2202 this_hdr->sh_type = SHT_FINI_ARRAY;
2203 else if (strcmp (asect->name, ".preinit_array") == 0)
2204 this_hdr->sh_type = SHT_PREINIT_ARRAY;
2205 else if (strncmp (asect->name, ".note", 5) == 0)
2206 this_hdr->sh_type = SHT_NOTE;
2207 else if (strncmp (asect->name, ".stab", 5) == 0
2208 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2209 this_hdr->sh_type = SHT_STRTAB;
2210 else if (strcmp (asect->name, ".gnu.version") == 0)
2212 this_hdr->sh_type = SHT_GNU_versym;
2213 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2215 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2217 this_hdr->sh_type = SHT_GNU_verdef;
2218 this_hdr->sh_entsize = 0;
2219 /* objcopy or strip will copy over sh_info, but may not set
2220 cverdefs. The linker will set cverdefs, but sh_info will be
2222 if (this_hdr->sh_info == 0)
2223 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2225 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2226 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2228 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2230 this_hdr->sh_type = SHT_GNU_verneed;
2231 this_hdr->sh_entsize = 0;
2232 /* objcopy or strip will copy over sh_info, but may not set
2233 cverrefs. The linker will set cverrefs, but sh_info will be
2235 if (this_hdr->sh_info == 0)
2236 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2238 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2239 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2241 else if ((asect->flags & SEC_GROUP) != 0)
2243 this_hdr->sh_type = SHT_GROUP;
2244 this_hdr->sh_entsize = 4;
2246 else if ((asect->flags & SEC_ALLOC) != 0
2247 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2248 || (asect->flags & SEC_NEVER_LOAD) != 0))
2249 this_hdr->sh_type = SHT_NOBITS;
2251 this_hdr->sh_type = SHT_PROGBITS;
2253 if ((asect->flags & SEC_ALLOC) != 0)
2254 this_hdr->sh_flags |= SHF_ALLOC;
2255 if ((asect->flags & SEC_READONLY) == 0)
2256 this_hdr->sh_flags |= SHF_WRITE;
2257 if ((asect->flags & SEC_CODE) != 0)
2258 this_hdr->sh_flags |= SHF_EXECINSTR;
2259 if ((asect->flags & SEC_MERGE) != 0)
2261 this_hdr->sh_flags |= SHF_MERGE;
2262 this_hdr->sh_entsize = asect->entsize;
2263 if ((asect->flags & SEC_STRINGS) != 0)
2264 this_hdr->sh_flags |= SHF_STRINGS;
2266 if (elf_group_name (asect) != NULL)
2267 this_hdr->sh_flags |= SHF_GROUP;
2269 /* Check for processor-specific section types. */
2270 if (bed->elf_backend_fake_sections
2271 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2274 /* If the section has relocs, set up a section header for the
2275 SHT_REL[A] section. If two relocation sections are required for
2276 this section, it is up to the processor-specific back-end to
2277 create the other. */
2278 if ((asect->flags & SEC_RELOC) != 0
2279 && !_bfd_elf_init_reloc_shdr (abfd,
2280 &elf_section_data (asect)->rel_hdr,
2282 elf_section_data (asect)->use_rela_p))
2286 /* Fill in the contents of a SHT_GROUP section. */
2289 set_group_contents (abfd, sec, failedptrarg)
2292 PTR failedptrarg ATTRIBUTE_UNUSED;
2294 boolean *failedptr = (boolean *) failedptrarg;
2295 unsigned long symindx;
2298 struct bfd_link_order *l;
2300 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2304 /* If called from the assembler, swap_out_syms will have set up
2305 elf_section_syms; If called for "ld -r", the symbols won't yet
2306 be mapped, so emulate elf_bfd_final_link. */
2307 if (elf_section_syms (abfd) != NULL)
2308 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2310 symindx = elf_section_data (sec)->this_idx;
2311 elf_section_data (sec)->this_hdr.sh_info = symindx;
2313 /* Nor will the contents be allocated for "ld -r". */
2314 if (sec->contents == NULL)
2316 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2317 if (sec->contents == NULL)
2324 loc = sec->contents + sec->_raw_size;
2326 /* Get the pointer to the first section in the group that we
2327 squirreled away here. */
2328 elt = elf_next_in_group (sec);
2330 /* First element is a flag word. Rest of section is elf section
2331 indices for all the sections of the group. Write them backwards
2332 just to keep the group in the same order as given in .section
2333 directives, not that it matters. */
2337 H_PUT_32 (abfd, elf_section_data (elt)->this_idx, loc);
2338 elt = elf_next_in_group (elt);
2341 /* If this is a relocatable link, then the above did nothing because
2342 SEC is the output section. Look through the input sections
2344 for (l = sec->link_order_head; l != NULL; l = l->next)
2345 if (l->type == bfd_indirect_link_order
2346 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2351 elf_section_data (elt->output_section)->this_idx, loc);
2352 elt = elf_next_in_group (elt);
2353 /* During a relocatable link, the lists are circular. */
2355 while (elt != elf_next_in_group (l->u.indirect.section));
2358 H_PUT_32 (abfd, 0, loc);
2360 BFD_ASSERT (loc == sec->contents);
2363 /* Assign all ELF section numbers. The dummy first section is handled here
2364 too. The link/info pointers for the standard section types are filled
2365 in here too, while we're at it. */
2368 assign_section_numbers (abfd)
2371 struct elf_obj_tdata *t = elf_tdata (abfd);
2373 unsigned int section_number, secn;
2374 Elf_Internal_Shdr **i_shdrp;
2379 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2381 for (sec = abfd->sections; sec; sec = sec->next)
2383 struct bfd_elf_section_data *d = elf_section_data (sec);
2385 if (section_number == SHN_LORESERVE)
2386 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2387 d->this_idx = section_number++;
2388 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2389 if ((sec->flags & SEC_RELOC) == 0)
2393 if (section_number == SHN_LORESERVE)
2394 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2395 d->rel_idx = section_number++;
2396 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2401 if (section_number == SHN_LORESERVE)
2402 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2403 d->rel_idx2 = section_number++;
2404 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2410 if (section_number == SHN_LORESERVE)
2411 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2412 t->shstrtab_section = section_number++;
2413 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2414 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2416 if (bfd_get_symcount (abfd) > 0)
2418 if (section_number == SHN_LORESERVE)
2419 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2420 t->symtab_section = section_number++;
2421 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2422 if (section_number > SHN_LORESERVE - 2)
2424 if (section_number == SHN_LORESERVE)
2425 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2426 t->symtab_shndx_section = section_number++;
2427 t->symtab_shndx_hdr.sh_name
2428 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2429 ".symtab_shndx", false);
2430 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2433 if (section_number == SHN_LORESERVE)
2434 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2435 t->strtab_section = section_number++;
2436 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2439 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2440 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2442 elf_numsections (abfd) = section_number;
2443 elf_elfheader (abfd)->e_shnum = section_number;
2444 if (section_number > SHN_LORESERVE)
2445 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2447 /* Set up the list of section header pointers, in agreement with the
2449 amt = section_number * sizeof (Elf_Internal_Shdr *);
2450 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
2451 if (i_shdrp == NULL)
2454 amt = sizeof (Elf_Internal_Shdr);
2455 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2456 if (i_shdrp[0] == NULL)
2458 bfd_release (abfd, i_shdrp);
2461 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2463 elf_elfsections (abfd) = i_shdrp;
2465 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2466 if (bfd_get_symcount (abfd) > 0)
2468 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2469 if (elf_numsections (abfd) > SHN_LORESERVE)
2471 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2472 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2474 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2475 t->symtab_hdr.sh_link = t->strtab_section;
2477 for (sec = abfd->sections; sec; sec = sec->next)
2479 struct bfd_elf_section_data *d = elf_section_data (sec);
2483 i_shdrp[d->this_idx] = &d->this_hdr;
2484 if (d->rel_idx != 0)
2485 i_shdrp[d->rel_idx] = &d->rel_hdr;
2486 if (d->rel_idx2 != 0)
2487 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2489 /* Fill in the sh_link and sh_info fields while we're at it. */
2491 /* sh_link of a reloc section is the section index of the symbol
2492 table. sh_info is the section index of the section to which
2493 the relocation entries apply. */
2494 if (d->rel_idx != 0)
2496 d->rel_hdr.sh_link = t->symtab_section;
2497 d->rel_hdr.sh_info = d->this_idx;
2499 if (d->rel_idx2 != 0)
2501 d->rel_hdr2->sh_link = t->symtab_section;
2502 d->rel_hdr2->sh_info = d->this_idx;
2505 switch (d->this_hdr.sh_type)
2509 /* A reloc section which we are treating as a normal BFD
2510 section. sh_link is the section index of the symbol
2511 table. sh_info is the section index of the section to
2512 which the relocation entries apply. We assume that an
2513 allocated reloc section uses the dynamic symbol table.
2514 FIXME: How can we be sure? */
2515 s = bfd_get_section_by_name (abfd, ".dynsym");
2517 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2519 /* We look up the section the relocs apply to by name. */
2521 if (d->this_hdr.sh_type == SHT_REL)
2525 s = bfd_get_section_by_name (abfd, name);
2527 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2531 /* We assume that a section named .stab*str is a stabs
2532 string section. We look for a section with the same name
2533 but without the trailing ``str'', and set its sh_link
2534 field to point to this section. */
2535 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2536 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2541 len = strlen (sec->name);
2542 alc = (char *) bfd_malloc ((bfd_size_type) len - 2);
2545 strncpy (alc, sec->name, len - 3);
2546 alc[len - 3] = '\0';
2547 s = bfd_get_section_by_name (abfd, alc);
2551 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2553 /* This is a .stab section. */
2554 elf_section_data (s)->this_hdr.sh_entsize =
2555 4 + 2 * bfd_get_arch_size (abfd) / 8;
2562 case SHT_GNU_verneed:
2563 case SHT_GNU_verdef:
2564 /* sh_link is the section header index of the string table
2565 used for the dynamic entries, or the symbol table, or the
2567 s = bfd_get_section_by_name (abfd, ".dynstr");
2569 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2573 case SHT_GNU_versym:
2574 /* sh_link is the section header index of the symbol table
2575 this hash table or version table is for. */
2576 s = bfd_get_section_by_name (abfd, ".dynsym");
2578 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2582 d->this_hdr.sh_link = t->symtab_section;
2586 for (secn = 1; secn < section_number; ++secn)
2587 if (i_shdrp[secn] == NULL)
2588 i_shdrp[secn] = i_shdrp[0];
2590 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2591 i_shdrp[secn]->sh_name);
2595 /* Map symbol from it's internal number to the external number, moving
2596 all local symbols to be at the head of the list. */
2599 sym_is_global (abfd, sym)
2603 /* If the backend has a special mapping, use it. */
2604 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2605 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2608 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2609 || bfd_is_und_section (bfd_get_section (sym))
2610 || bfd_is_com_section (bfd_get_section (sym)));
2614 elf_map_symbols (abfd)
2617 unsigned int symcount = bfd_get_symcount (abfd);
2618 asymbol **syms = bfd_get_outsymbols (abfd);
2619 asymbol **sect_syms;
2620 unsigned int num_locals = 0;
2621 unsigned int num_globals = 0;
2622 unsigned int num_locals2 = 0;
2623 unsigned int num_globals2 = 0;
2631 fprintf (stderr, "elf_map_symbols\n");
2635 for (asect = abfd->sections; asect; asect = asect->next)
2637 if (max_index < asect->index)
2638 max_index = asect->index;
2642 amt = max_index * sizeof (asymbol *);
2643 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2644 if (sect_syms == NULL)
2646 elf_section_syms (abfd) = sect_syms;
2647 elf_num_section_syms (abfd) = max_index;
2649 /* Init sect_syms entries for any section symbols we have already
2650 decided to output. */
2651 for (idx = 0; idx < symcount; idx++)
2653 asymbol *sym = syms[idx];
2655 if ((sym->flags & BSF_SECTION_SYM) != 0
2662 if (sec->owner != NULL)
2664 if (sec->owner != abfd)
2666 if (sec->output_offset != 0)
2669 sec = sec->output_section;
2671 /* Empty sections in the input files may have had a
2672 section symbol created for them. (See the comment
2673 near the end of _bfd_generic_link_output_symbols in
2674 linker.c). If the linker script discards such
2675 sections then we will reach this point. Since we know
2676 that we cannot avoid this case, we detect it and skip
2677 the abort and the assignment to the sect_syms array.
2678 To reproduce this particular case try running the
2679 linker testsuite test ld-scripts/weak.exp for an ELF
2680 port that uses the generic linker. */
2681 if (sec->owner == NULL)
2684 BFD_ASSERT (sec->owner == abfd);
2686 sect_syms[sec->index] = syms[idx];
2691 /* Classify all of the symbols. */
2692 for (idx = 0; idx < symcount; idx++)
2694 if (!sym_is_global (abfd, syms[idx]))
2700 /* We will be adding a section symbol for each BFD section. Most normal
2701 sections will already have a section symbol in outsymbols, but
2702 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2703 at least in that case. */
2704 for (asect = abfd->sections; asect; asect = asect->next)
2706 if (sect_syms[asect->index] == NULL)
2708 if (!sym_is_global (abfd, asect->symbol))
2715 /* Now sort the symbols so the local symbols are first. */
2716 amt = (num_locals + num_globals) * sizeof (asymbol *);
2717 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2719 if (new_syms == NULL)
2722 for (idx = 0; idx < symcount; idx++)
2724 asymbol *sym = syms[idx];
2727 if (!sym_is_global (abfd, sym))
2730 i = num_locals + num_globals2++;
2732 sym->udata.i = i + 1;
2734 for (asect = abfd->sections; asect; asect = asect->next)
2736 if (sect_syms[asect->index] == NULL)
2738 asymbol *sym = asect->symbol;
2741 sect_syms[asect->index] = sym;
2742 if (!sym_is_global (abfd, sym))
2745 i = num_locals + num_globals2++;
2747 sym->udata.i = i + 1;
2751 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2753 elf_num_locals (abfd) = num_locals;
2754 elf_num_globals (abfd) = num_globals;
2758 /* Align to the maximum file alignment that could be required for any
2759 ELF data structure. */
2761 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2762 static INLINE file_ptr
2763 align_file_position (off, align)
2767 return (off + align - 1) & ~(align - 1);
2770 /* Assign a file position to a section, optionally aligning to the
2771 required section alignment. */
2774 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2775 Elf_Internal_Shdr *i_shdrp;
2783 al = i_shdrp->sh_addralign;
2785 offset = BFD_ALIGN (offset, al);
2787 i_shdrp->sh_offset = offset;
2788 if (i_shdrp->bfd_section != NULL)
2789 i_shdrp->bfd_section->filepos = offset;
2790 if (i_shdrp->sh_type != SHT_NOBITS)
2791 offset += i_shdrp->sh_size;
2795 /* Compute the file positions we are going to put the sections at, and
2796 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2797 is not NULL, this is being called by the ELF backend linker. */
2800 _bfd_elf_compute_section_file_positions (abfd, link_info)
2802 struct bfd_link_info *link_info;
2804 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2806 struct bfd_strtab_hash *strtab;
2807 Elf_Internal_Shdr *shstrtab_hdr;
2809 if (abfd->output_has_begun)
2812 /* Do any elf backend specific processing first. */
2813 if (bed->elf_backend_begin_write_processing)
2814 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2816 if (! prep_headers (abfd))
2819 /* Post process the headers if necessary. */
2820 if (bed->elf_backend_post_process_headers)
2821 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2824 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2828 if (!assign_section_numbers (abfd))
2831 /* The backend linker builds symbol table information itself. */
2832 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2834 /* Non-zero if doing a relocatable link. */
2835 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2837 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2841 if (link_info == NULL || link_info->relocateable)
2843 bfd_map_over_sections (abfd, set_group_contents, &failed);
2848 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2849 /* sh_name was set in prep_headers. */
2850 shstrtab_hdr->sh_type = SHT_STRTAB;
2851 shstrtab_hdr->sh_flags = 0;
2852 shstrtab_hdr->sh_addr = 0;
2853 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2854 shstrtab_hdr->sh_entsize = 0;
2855 shstrtab_hdr->sh_link = 0;
2856 shstrtab_hdr->sh_info = 0;
2857 /* sh_offset is set in assign_file_positions_except_relocs. */
2858 shstrtab_hdr->sh_addralign = 1;
2860 if (!assign_file_positions_except_relocs (abfd))
2863 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2866 Elf_Internal_Shdr *hdr;
2868 off = elf_tdata (abfd)->next_file_pos;
2870 hdr = &elf_tdata (abfd)->symtab_hdr;
2871 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2873 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2874 if (hdr->sh_size != 0)
2875 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2877 hdr = &elf_tdata (abfd)->strtab_hdr;
2878 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2880 elf_tdata (abfd)->next_file_pos = off;
2882 /* Now that we know where the .strtab section goes, write it
2884 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2885 || ! _bfd_stringtab_emit (abfd, strtab))
2887 _bfd_stringtab_free (strtab);
2890 abfd->output_has_begun = true;
2895 /* Create a mapping from a set of sections to a program segment. */
2897 static INLINE struct elf_segment_map *
2898 make_mapping (abfd, sections, from, to, phdr)
2900 asection **sections;
2905 struct elf_segment_map *m;
2910 amt = sizeof (struct elf_segment_map);
2911 amt += (to - from - 1) * sizeof (asection *);
2912 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2916 m->p_type = PT_LOAD;
2917 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2918 m->sections[i - from] = *hdrpp;
2919 m->count = to - from;
2921 if (from == 0 && phdr)
2923 /* Include the headers in the first PT_LOAD segment. */
2924 m->includes_filehdr = 1;
2925 m->includes_phdrs = 1;
2931 /* Set up a mapping from BFD sections to program segments. */
2934 map_sections_to_segments (abfd)
2937 asection **sections = NULL;
2941 struct elf_segment_map *mfirst;
2942 struct elf_segment_map **pm;
2943 struct elf_segment_map *m;
2945 unsigned int phdr_index;
2946 bfd_vma maxpagesize;
2948 boolean phdr_in_segment = true;
2950 asection *dynsec, *eh_frame_hdr;
2953 if (elf_tdata (abfd)->segment_map != NULL)
2956 if (bfd_count_sections (abfd) == 0)
2959 /* Select the allocated sections, and sort them. */
2961 amt = bfd_count_sections (abfd) * sizeof (asection *);
2962 sections = (asection **) bfd_malloc (amt);
2963 if (sections == NULL)
2967 for (s = abfd->sections; s != NULL; s = s->next)
2969 if ((s->flags & SEC_ALLOC) != 0)
2975 BFD_ASSERT (i <= bfd_count_sections (abfd));
2978 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2980 /* Build the mapping. */
2985 /* If we have a .interp section, then create a PT_PHDR segment for
2986 the program headers and a PT_INTERP segment for the .interp
2988 s = bfd_get_section_by_name (abfd, ".interp");
2989 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2991 amt = sizeof (struct elf_segment_map);
2992 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2996 m->p_type = PT_PHDR;
2997 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2998 m->p_flags = PF_R | PF_X;
2999 m->p_flags_valid = 1;
3000 m->includes_phdrs = 1;
3005 amt = sizeof (struct elf_segment_map);
3006 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3010 m->p_type = PT_INTERP;
3018 /* Look through the sections. We put sections in the same program
3019 segment when the start of the second section can be placed within
3020 a few bytes of the end of the first section. */
3023 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3025 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3027 && (dynsec->flags & SEC_LOAD) == 0)
3030 /* Deal with -Ttext or something similar such that the first section
3031 is not adjacent to the program headers. This is an
3032 approximation, since at this point we don't know exactly how many
3033 program headers we will need. */
3036 bfd_size_type phdr_size;
3038 phdr_size = elf_tdata (abfd)->program_header_size;
3040 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3041 if ((abfd->flags & D_PAGED) == 0
3042 || sections[0]->lma < phdr_size
3043 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3044 phdr_in_segment = false;
3047 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3050 boolean new_segment;
3054 /* See if this section and the last one will fit in the same
3057 if (last_hdr == NULL)
3059 /* If we don't have a segment yet, then we don't need a new
3060 one (we build the last one after this loop). */
3061 new_segment = false;
3063 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3065 /* If this section has a different relation between the
3066 virtual address and the load address, then we need a new
3070 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3071 < BFD_ALIGN (hdr->lma, maxpagesize))
3073 /* If putting this section in this segment would force us to
3074 skip a page in the segment, then we need a new segment. */
3077 else if ((last_hdr->flags & SEC_LOAD) == 0
3078 && (hdr->flags & SEC_LOAD) != 0)
3080 /* We don't want to put a loadable section after a
3081 nonloadable section in the same segment. */
3084 else if ((abfd->flags & D_PAGED) == 0)
3086 /* If the file is not demand paged, which means that we
3087 don't require the sections to be correctly aligned in the
3088 file, then there is no other reason for a new segment. */
3089 new_segment = false;
3092 && (hdr->flags & SEC_READONLY) == 0
3093 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3096 /* We don't want to put a writable section in a read only
3097 segment, unless they are on the same page in memory
3098 anyhow. We already know that the last section does not
3099 bring us past the current section on the page, so the
3100 only case in which the new section is not on the same
3101 page as the previous section is when the previous section
3102 ends precisely on a page boundary. */
3107 /* Otherwise, we can use the same segment. */
3108 new_segment = false;
3113 if ((hdr->flags & SEC_READONLY) == 0)
3119 /* We need a new program segment. We must create a new program
3120 header holding all the sections from phdr_index until hdr. */
3122 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3129 if ((hdr->flags & SEC_READONLY) == 0)
3136 phdr_in_segment = false;
3139 /* Create a final PT_LOAD program segment. */
3140 if (last_hdr != NULL)
3142 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3150 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3153 amt = sizeof (struct elf_segment_map);
3154 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3158 m->p_type = PT_DYNAMIC;
3160 m->sections[0] = dynsec;
3166 /* For each loadable .note section, add a PT_NOTE segment. We don't
3167 use bfd_get_section_by_name, because if we link together
3168 nonloadable .note sections and loadable .note sections, we will
3169 generate two .note sections in the output file. FIXME: Using
3170 names for section types is bogus anyhow. */
3171 for (s = abfd->sections; s != NULL; s = s->next)
3173 if ((s->flags & SEC_LOAD) != 0
3174 && strncmp (s->name, ".note", 5) == 0)
3176 amt = sizeof (struct elf_segment_map);
3177 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3181 m->p_type = PT_NOTE;
3190 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3192 eh_frame_hdr = NULL;
3193 if (elf_tdata (abfd)->eh_frame_hdr)
3194 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
3195 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3197 amt = sizeof (struct elf_segment_map);
3198 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3202 m->p_type = PT_GNU_EH_FRAME;
3204 m->sections[0] = eh_frame_hdr;
3213 elf_tdata (abfd)->segment_map = mfirst;
3217 if (sections != NULL)
3222 /* Sort sections by address. */
3225 elf_sort_sections (arg1, arg2)
3229 const asection *sec1 = *(const asection **) arg1;
3230 const asection *sec2 = *(const asection **) arg2;
3232 /* Sort by LMA first, since this is the address used to
3233 place the section into a segment. */
3234 if (sec1->lma < sec2->lma)
3236 else if (sec1->lma > sec2->lma)
3239 /* Then sort by VMA. Normally the LMA and the VMA will be
3240 the same, and this will do nothing. */
3241 if (sec1->vma < sec2->vma)
3243 else if (sec1->vma > sec2->vma)
3246 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3248 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3254 /* If the indicies are the same, do not return 0
3255 here, but continue to try the next comparison. */
3256 if (sec1->target_index - sec2->target_index != 0)
3257 return sec1->target_index - sec2->target_index;
3262 else if (TOEND (sec2))
3267 /* Sort by size, to put zero sized sections
3268 before others at the same address. */
3270 if (sec1->_raw_size < sec2->_raw_size)
3272 if (sec1->_raw_size > sec2->_raw_size)
3275 return sec1->target_index - sec2->target_index;
3278 /* Assign file positions to the sections based on the mapping from
3279 sections to segments. This function also sets up some fields in
3280 the file header, and writes out the program headers. */
3283 assign_file_positions_for_segments (abfd)
3286 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3288 struct elf_segment_map *m;
3290 Elf_Internal_Phdr *phdrs;
3292 bfd_vma filehdr_vaddr, filehdr_paddr;
3293 bfd_vma phdrs_vaddr, phdrs_paddr;
3294 Elf_Internal_Phdr *p;
3297 if (elf_tdata (abfd)->segment_map == NULL)
3299 if (! map_sections_to_segments (abfd))
3303 if (bed->elf_backend_modify_segment_map)
3305 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3310 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3313 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3314 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3315 elf_elfheader (abfd)->e_phnum = count;
3320 /* If we already counted the number of program segments, make sure
3321 that we allocated enough space. This happens when SIZEOF_HEADERS
3322 is used in a linker script. */
3323 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3324 if (alloc != 0 && count > alloc)
3326 ((*_bfd_error_handler)
3327 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3328 bfd_get_filename (abfd), alloc, count));
3329 bfd_set_error (bfd_error_bad_value);
3336 amt = alloc * sizeof (Elf_Internal_Phdr);
3337 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3341 off = bed->s->sizeof_ehdr;
3342 off += alloc * bed->s->sizeof_phdr;
3349 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3356 /* If elf_segment_map is not from map_sections_to_segments, the
3357 sections may not be correctly ordered. NOTE: sorting should
3358 not be done to the PT_NOTE section of a corefile, which may
3359 contain several pseudo-sections artificially created by bfd.
3360 Sorting these pseudo-sections breaks things badly. */
3362 && !(elf_elfheader (abfd)->e_type == ET_CORE
3363 && m->p_type == PT_NOTE))
3364 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3367 p->p_type = m->p_type;
3368 p->p_flags = m->p_flags;
3370 if (p->p_type == PT_LOAD
3372 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3374 if ((abfd->flags & D_PAGED) != 0)
3375 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3378 bfd_size_type align;
3381 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3383 bfd_size_type secalign;
3385 secalign = bfd_get_section_alignment (abfd, *secpp);
3386 if (secalign > align)
3390 off += (m->sections[0]->vma - off) % (1 << align);
3397 p->p_vaddr = m->sections[0]->vma;
3399 if (m->p_paddr_valid)
3400 p->p_paddr = m->p_paddr;
3401 else if (m->count == 0)
3404 p->p_paddr = m->sections[0]->lma;
3406 if (p->p_type == PT_LOAD
3407 && (abfd->flags & D_PAGED) != 0)
3408 p->p_align = bed->maxpagesize;
3409 else if (m->count == 0)
3410 p->p_align = bed->s->file_align;
3418 if (m->includes_filehdr)
3420 if (! m->p_flags_valid)
3423 p->p_filesz = bed->s->sizeof_ehdr;
3424 p->p_memsz = bed->s->sizeof_ehdr;
3427 BFD_ASSERT (p->p_type == PT_LOAD);
3429 if (p->p_vaddr < (bfd_vma) off)
3431 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3432 bfd_get_filename (abfd));
3433 bfd_set_error (bfd_error_bad_value);
3438 if (! m->p_paddr_valid)
3441 if (p->p_type == PT_LOAD)
3443 filehdr_vaddr = p->p_vaddr;
3444 filehdr_paddr = p->p_paddr;
3448 if (m->includes_phdrs)
3450 if (! m->p_flags_valid)
3453 if (m->includes_filehdr)
3455 if (p->p_type == PT_LOAD)
3457 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3458 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3463 p->p_offset = bed->s->sizeof_ehdr;
3467 BFD_ASSERT (p->p_type == PT_LOAD);
3468 p->p_vaddr -= off - p->p_offset;
3469 if (! m->p_paddr_valid)
3470 p->p_paddr -= off - p->p_offset;
3473 if (p->p_type == PT_LOAD)
3475 phdrs_vaddr = p->p_vaddr;
3476 phdrs_paddr = p->p_paddr;
3479 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3482 p->p_filesz += alloc * bed->s->sizeof_phdr;
3483 p->p_memsz += alloc * bed->s->sizeof_phdr;
3486 if (p->p_type == PT_LOAD
3487 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3489 if (! m->includes_filehdr && ! m->includes_phdrs)
3495 adjust = off - (p->p_offset + p->p_filesz);
3496 p->p_filesz += adjust;
3497 p->p_memsz += adjust;
3503 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3507 bfd_size_type align;
3511 align = 1 << bfd_get_section_alignment (abfd, sec);
3513 /* The section may have artificial alignment forced by a
3514 link script. Notice this case by the gap between the
3515 cumulative phdr lma and the section's lma. */
3516 if (p->p_paddr + p->p_memsz < sec->lma)
3518 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3520 p->p_memsz += adjust;
3523 if ((flags & SEC_LOAD) != 0)
3524 p->p_filesz += adjust;
3527 if (p->p_type == PT_LOAD)
3529 bfd_signed_vma adjust;
3531 if ((flags & SEC_LOAD) != 0)
3533 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3537 else if ((flags & SEC_ALLOC) != 0)
3539 /* The section VMA must equal the file position
3540 modulo the page size. FIXME: I'm not sure if
3541 this adjustment is really necessary. We used to
3542 not have the SEC_LOAD case just above, and then
3543 this was necessary, but now I'm not sure. */
3544 if ((abfd->flags & D_PAGED) != 0)
3545 adjust = (sec->vma - voff) % bed->maxpagesize;
3547 adjust = (sec->vma - voff) % align;
3556 (* _bfd_error_handler) (_("\
3557 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3558 bfd_section_name (abfd, sec),
3563 p->p_memsz += adjust;
3566 if ((flags & SEC_LOAD) != 0)
3567 p->p_filesz += adjust;
3572 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3573 used in a linker script we may have a section with
3574 SEC_LOAD clear but which is supposed to have
3576 if ((flags & SEC_LOAD) != 0
3577 || (flags & SEC_HAS_CONTENTS) != 0)
3578 off += sec->_raw_size;
3580 if ((flags & SEC_ALLOC) != 0)
3581 voff += sec->_raw_size;
3584 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3586 /* The actual "note" segment has i == 0.
3587 This is the one that actually contains everything. */
3591 p->p_filesz = sec->_raw_size;
3592 off += sec->_raw_size;
3597 /* Fake sections -- don't need to be written. */
3600 flags = sec->flags = 0;
3607 p->p_memsz += sec->_raw_size;
3609 if ((flags & SEC_LOAD) != 0)
3610 p->p_filesz += sec->_raw_size;
3612 if (align > p->p_align
3613 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3617 if (! m->p_flags_valid)
3620 if ((flags & SEC_CODE) != 0)
3622 if ((flags & SEC_READONLY) == 0)
3628 /* Now that we have set the section file positions, we can set up
3629 the file positions for the non PT_LOAD segments. */
3630 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3634 if (p->p_type != PT_LOAD && m->count > 0)
3636 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3637 p->p_offset = m->sections[0]->filepos;
3641 if (m->includes_filehdr)
3643 p->p_vaddr = filehdr_vaddr;
3644 if (! m->p_paddr_valid)
3645 p->p_paddr = filehdr_paddr;
3647 else if (m->includes_phdrs)
3649 p->p_vaddr = phdrs_vaddr;
3650 if (! m->p_paddr_valid)
3651 p->p_paddr = phdrs_paddr;
3656 /* Clear out any program headers we allocated but did not use. */
3657 for (; count < alloc; count++, p++)
3659 memset (p, 0, sizeof *p);
3660 p->p_type = PT_NULL;
3663 elf_tdata (abfd)->phdr = phdrs;
3665 elf_tdata (abfd)->next_file_pos = off;
3667 /* Write out the program headers. */
3668 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
3669 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3675 /* Get the size of the program header.
3677 If this is called by the linker before any of the section VMA's are set, it
3678 can't calculate the correct value for a strange memory layout. This only
3679 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3680 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3681 data segment (exclusive of .interp and .dynamic).
3683 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3684 will be two segments. */
3686 static bfd_size_type
3687 get_program_header_size (abfd)
3692 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3694 /* We can't return a different result each time we're called. */
3695 if (elf_tdata (abfd)->program_header_size != 0)
3696 return elf_tdata (abfd)->program_header_size;
3698 if (elf_tdata (abfd)->segment_map != NULL)
3700 struct elf_segment_map *m;
3703 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3705 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3706 return elf_tdata (abfd)->program_header_size;
3709 /* Assume we will need exactly two PT_LOAD segments: one for text
3710 and one for data. */
3713 s = bfd_get_section_by_name (abfd, ".interp");
3714 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3716 /* If we have a loadable interpreter section, we need a
3717 PT_INTERP segment. In this case, assume we also need a
3718 PT_PHDR segment, although that may not be true for all
3723 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3725 /* We need a PT_DYNAMIC segment. */
3729 if (elf_tdata (abfd)->eh_frame_hdr
3730 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
3732 /* We need a PT_GNU_EH_FRAME segment. */
3736 for (s = abfd->sections; s != NULL; s = s->next)
3738 if ((s->flags & SEC_LOAD) != 0
3739 && strncmp (s->name, ".note", 5) == 0)
3741 /* We need a PT_NOTE segment. */
3746 /* Let the backend count up any program headers it might need. */
3747 if (bed->elf_backend_additional_program_headers)
3751 a = (*bed->elf_backend_additional_program_headers) (abfd);
3757 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3758 return elf_tdata (abfd)->program_header_size;
3761 /* Work out the file positions of all the sections. This is called by
3762 _bfd_elf_compute_section_file_positions. All the section sizes and
3763 VMAs must be known before this is called.
3765 We do not consider reloc sections at this point, unless they form
3766 part of the loadable image. Reloc sections are assigned file
3767 positions in assign_file_positions_for_relocs, which is called by
3768 write_object_contents and final_link.
3770 We also don't set the positions of the .symtab and .strtab here. */
3773 assign_file_positions_except_relocs (abfd)
3776 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3777 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3778 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3779 unsigned int num_sec = elf_numsections (abfd);
3781 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3783 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3784 && bfd_get_format (abfd) != bfd_core)
3786 Elf_Internal_Shdr **hdrpp;
3789 /* Start after the ELF header. */
3790 off = i_ehdrp->e_ehsize;
3792 /* We are not creating an executable, which means that we are
3793 not creating a program header, and that the actual order of
3794 the sections in the file is unimportant. */
3795 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3797 Elf_Internal_Shdr *hdr;
3800 if (hdr->sh_type == SHT_REL
3801 || hdr->sh_type == SHT_RELA
3802 || i == tdata->symtab_section
3803 || i == tdata->symtab_shndx_section
3804 || i == tdata->strtab_section)
3806 hdr->sh_offset = -1;
3809 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3811 if (i == SHN_LORESERVE - 1)
3813 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3814 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3821 Elf_Internal_Shdr **hdrpp;
3823 /* Assign file positions for the loaded sections based on the
3824 assignment of sections to segments. */
3825 if (! assign_file_positions_for_segments (abfd))
3828 /* Assign file positions for the other sections. */
3830 off = elf_tdata (abfd)->next_file_pos;
3831 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3833 Elf_Internal_Shdr *hdr;
3836 if (hdr->bfd_section != NULL
3837 && hdr->bfd_section->filepos != 0)
3838 hdr->sh_offset = hdr->bfd_section->filepos;
3839 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3841 ((*_bfd_error_handler)
3842 (_("%s: warning: allocated section `%s' not in segment"),
3843 bfd_get_filename (abfd),
3844 (hdr->bfd_section == NULL
3846 : hdr->bfd_section->name)));
3847 if ((abfd->flags & D_PAGED) != 0)
3848 off += (hdr->sh_addr - off) % bed->maxpagesize;
3850 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3851 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3854 else if (hdr->sh_type == SHT_REL
3855 || hdr->sh_type == SHT_RELA
3856 || hdr == i_shdrpp[tdata->symtab_section]
3857 || hdr == i_shdrpp[tdata->symtab_shndx_section]
3858 || hdr == i_shdrpp[tdata->strtab_section])
3859 hdr->sh_offset = -1;
3861 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3863 if (i == SHN_LORESERVE - 1)
3865 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3866 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3871 /* Place the section headers. */
3872 off = align_file_position (off, bed->s->file_align);
3873 i_ehdrp->e_shoff = off;
3874 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3876 elf_tdata (abfd)->next_file_pos = off;
3885 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3886 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3887 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3888 struct elf_strtab_hash *shstrtab;
3889 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3891 i_ehdrp = elf_elfheader (abfd);
3892 i_shdrp = elf_elfsections (abfd);
3894 shstrtab = _bfd_elf_strtab_init ();
3895 if (shstrtab == NULL)
3898 elf_shstrtab (abfd) = shstrtab;
3900 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3901 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3902 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3903 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3905 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3906 i_ehdrp->e_ident[EI_DATA] =
3907 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3908 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3910 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
3911 #ifdef WANT_OLD_BRANDELF_METHOD
3912 #define _OLD_EI_BRAND_OFFSET 8
3913 #define _OLD_BRANDING "FreeBSD"
3914 strncpy((char *) &i_ehdrp->e_ident[_OLD_EI_BRAND_OFFSET], _OLD_BRANDING,
3915 EI_NIDENT-_OLD_EI_BRAND_OFFSET);
3918 if ((abfd->flags & DYNAMIC) != 0)
3919 i_ehdrp->e_type = ET_DYN;
3920 else if ((abfd->flags & EXEC_P) != 0)
3921 i_ehdrp->e_type = ET_EXEC;
3922 else if (bfd_get_format (abfd) == bfd_core)
3923 i_ehdrp->e_type = ET_CORE;
3925 i_ehdrp->e_type = ET_REL;
3927 switch (bfd_get_arch (abfd))
3929 case bfd_arch_unknown:
3930 i_ehdrp->e_machine = EM_NONE;
3933 /* There used to be a long list of cases here, each one setting
3934 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3935 in the corresponding bfd definition. To avoid duplication,
3936 the switch was removed. Machines that need special handling
3937 can generally do it in elf_backend_final_write_processing(),
3938 unless they need the information earlier than the final write.
3939 Such need can generally be supplied by replacing the tests for
3940 e_machine with the conditions used to determine it. */
3942 if (get_elf_backend_data (abfd) != NULL)
3943 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
3945 i_ehdrp->e_machine = EM_NONE;
3948 i_ehdrp->e_version = bed->s->ev_current;
3949 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3951 /* No program header, for now. */
3952 i_ehdrp->e_phoff = 0;
3953 i_ehdrp->e_phentsize = 0;
3954 i_ehdrp->e_phnum = 0;
3956 /* Each bfd section is section header entry. */
3957 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3958 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3960 /* If we're building an executable, we'll need a program header table. */
3961 if (abfd->flags & EXEC_P)
3963 /* It all happens later. */
3965 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3967 /* elf_build_phdrs() returns a (NULL-terminated) array of
3968 Elf_Internal_Phdrs. */
3969 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3970 i_ehdrp->e_phoff = outbase;
3971 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3976 i_ehdrp->e_phentsize = 0;
3978 i_ehdrp->e_phoff = 0;
3981 elf_tdata (abfd)->symtab_hdr.sh_name =
3982 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
3983 elf_tdata (abfd)->strtab_hdr.sh_name =
3984 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
3985 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3986 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
3987 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3988 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3989 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3995 /* Assign file positions for all the reloc sections which are not part
3996 of the loadable file image. */
3999 _bfd_elf_assign_file_positions_for_relocs (abfd)
4003 unsigned int i, num_sec;
4004 Elf_Internal_Shdr **shdrpp;
4006 off = elf_tdata (abfd)->next_file_pos;
4008 num_sec = elf_numsections (abfd);
4009 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4011 Elf_Internal_Shdr *shdrp;
4014 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4015 && shdrp->sh_offset == -1)
4016 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
4019 elf_tdata (abfd)->next_file_pos = off;
4023 _bfd_elf_write_object_contents (abfd)
4026 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4027 Elf_Internal_Ehdr *i_ehdrp;
4028 Elf_Internal_Shdr **i_shdrp;
4030 unsigned int count, num_sec;
4032 if (! abfd->output_has_begun
4033 && ! _bfd_elf_compute_section_file_positions
4034 (abfd, (struct bfd_link_info *) NULL))
4037 i_shdrp = elf_elfsections (abfd);
4038 i_ehdrp = elf_elfheader (abfd);
4041 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4045 _bfd_elf_assign_file_positions_for_relocs (abfd);
4047 /* After writing the headers, we need to write the sections too... */
4048 num_sec = elf_numsections (abfd);
4049 for (count = 1; count < num_sec; count++)
4051 if (bed->elf_backend_section_processing)
4052 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4053 if (i_shdrp[count]->contents)
4055 bfd_size_type amt = i_shdrp[count]->sh_size;
4057 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4058 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4061 if (count == SHN_LORESERVE - 1)
4062 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4065 /* Write out the section header names. */
4066 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4067 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4070 if (bed->elf_backend_final_write_processing)
4071 (*bed->elf_backend_final_write_processing) (abfd,
4072 elf_tdata (abfd)->linker);
4074 return bed->s->write_shdrs_and_ehdr (abfd);
4078 _bfd_elf_write_corefile_contents (abfd)
4081 /* Hopefully this can be done just like an object file. */
4082 return _bfd_elf_write_object_contents (abfd);
4085 /* Given a section, search the header to find them. */
4088 _bfd_elf_section_from_bfd_section (abfd, asect)
4092 struct elf_backend_data *bed;
4095 if (elf_section_data (asect) != NULL
4096 && elf_section_data (asect)->this_idx != 0)
4097 return elf_section_data (asect)->this_idx;
4099 if (bfd_is_abs_section (asect))
4101 else if (bfd_is_com_section (asect))
4103 else if (bfd_is_und_section (asect))
4107 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4108 int maxindex = elf_numsections (abfd);
4110 for (index = 1; index < maxindex; index++)
4112 Elf_Internal_Shdr *hdr = i_shdrp[index];
4114 if (hdr != NULL && hdr->bfd_section == asect)
4120 bed = get_elf_backend_data (abfd);
4121 if (bed->elf_backend_section_from_bfd_section)
4125 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4130 bfd_set_error (bfd_error_nonrepresentable_section);
4135 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4139 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4141 asymbol **asym_ptr_ptr;
4143 asymbol *asym_ptr = *asym_ptr_ptr;
4145 flagword flags = asym_ptr->flags;
4147 /* When gas creates relocations against local labels, it creates its
4148 own symbol for the section, but does put the symbol into the
4149 symbol chain, so udata is 0. When the linker is generating
4150 relocatable output, this section symbol may be for one of the
4151 input sections rather than the output section. */
4152 if (asym_ptr->udata.i == 0
4153 && (flags & BSF_SECTION_SYM)
4154 && asym_ptr->section)
4158 if (asym_ptr->section->output_section != NULL)
4159 indx = asym_ptr->section->output_section->index;
4161 indx = asym_ptr->section->index;
4162 if (indx < elf_num_section_syms (abfd)
4163 && elf_section_syms (abfd)[indx] != NULL)
4164 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4167 idx = asym_ptr->udata.i;
4171 /* This case can occur when using --strip-symbol on a symbol
4172 which is used in a relocation entry. */
4173 (*_bfd_error_handler)
4174 (_("%s: symbol `%s' required but not present"),
4175 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4176 bfd_set_error (bfd_error_no_symbols);
4183 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4184 (long) asym_ptr, asym_ptr->name, idx, flags,
4185 elf_symbol_flags (flags));
4193 /* Copy private BFD data. This copies any program header information. */
4196 copy_private_bfd_data (ibfd, obfd)
4200 Elf_Internal_Ehdr * iehdr;
4201 struct elf_segment_map * map;
4202 struct elf_segment_map * map_first;
4203 struct elf_segment_map ** pointer_to_map;
4204 Elf_Internal_Phdr * segment;
4207 unsigned int num_segments;
4208 boolean phdr_included = false;
4209 bfd_vma maxpagesize;
4210 struct elf_segment_map * phdr_adjust_seg = NULL;
4211 unsigned int phdr_adjust_num = 0;
4213 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4214 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4217 if (elf_tdata (ibfd)->phdr == NULL)
4220 iehdr = elf_elfheader (ibfd);
4223 pointer_to_map = &map_first;
4225 num_segments = elf_elfheader (ibfd)->e_phnum;
4226 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4228 /* Returns the end address of the segment + 1. */
4229 #define SEGMENT_END(segment, start) \
4230 (start + (segment->p_memsz > segment->p_filesz \
4231 ? segment->p_memsz : segment->p_filesz))
4233 /* Returns true if the given section is contained within
4234 the given segment. VMA addresses are compared. */
4235 #define IS_CONTAINED_BY_VMA(section, segment) \
4236 (section->vma >= segment->p_vaddr \
4237 && (section->vma + section->_raw_size) \
4238 <= (SEGMENT_END (segment, segment->p_vaddr)))
4240 /* Returns true if the given section is contained within
4241 the given segment. LMA addresses are compared. */
4242 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4243 (section->lma >= base \
4244 && (section->lma + section->_raw_size) \
4245 <= SEGMENT_END (segment, base))
4247 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4248 #define IS_COREFILE_NOTE(p, s) \
4249 (p->p_type == PT_NOTE \
4250 && bfd_get_format (ibfd) == bfd_core \
4251 && s->vma == 0 && s->lma == 0 \
4252 && (bfd_vma) s->filepos >= p->p_offset \
4253 && (bfd_vma) s->filepos + s->_raw_size \
4254 <= p->p_offset + p->p_filesz)
4256 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4257 linker, which generates a PT_INTERP section with p_vaddr and
4258 p_memsz set to 0. */
4259 #define IS_SOLARIS_PT_INTERP(p, s) \
4261 && p->p_filesz > 0 \
4262 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4263 && s->_raw_size > 0 \
4264 && (bfd_vma) s->filepos >= p->p_offset \
4265 && ((bfd_vma) s->filepos + s->_raw_size \
4266 <= p->p_offset + p->p_filesz))
4268 /* Decide if the given section should be included in the given segment.
4269 A section will be included if:
4270 1. It is within the address space of the segment -- we use the LMA
4271 if that is set for the segment and the VMA otherwise,
4272 2. It is an allocated segment,
4273 3. There is an output section associated with it,
4274 4. The section has not already been allocated to a previous segment. */
4275 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4276 (((((segment->p_paddr \
4277 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4278 : IS_CONTAINED_BY_VMA (section, segment)) \
4279 || IS_SOLARIS_PT_INTERP (segment, section)) \
4280 && (section->flags & SEC_ALLOC) != 0) \
4281 || IS_COREFILE_NOTE (segment, section)) \
4282 && section->output_section != NULL \
4283 && section->segment_mark == false)
4285 /* Returns true iff seg1 starts after the end of seg2. */
4286 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4287 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4289 /* Returns true iff seg1 and seg2 overlap. */
4290 #define SEGMENT_OVERLAPS(seg1, seg2) \
4291 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4293 /* Initialise the segment mark field. */
4294 for (section = ibfd->sections; section != NULL; section = section->next)
4295 section->segment_mark = false;
4297 /* Scan through the segments specified in the program header
4298 of the input BFD. For this first scan we look for overlaps
4299 in the loadable segments. These can be created by weird
4300 parameters to objcopy. */
4301 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4306 Elf_Internal_Phdr *segment2;
4308 if (segment->p_type != PT_LOAD)
4311 /* Determine if this segment overlaps any previous segments. */
4312 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4314 bfd_signed_vma extra_length;
4316 if (segment2->p_type != PT_LOAD
4317 || ! SEGMENT_OVERLAPS (segment, segment2))
4320 /* Merge the two segments together. */
4321 if (segment2->p_vaddr < segment->p_vaddr)
4323 /* Extend SEGMENT2 to include SEGMENT and then delete
4326 SEGMENT_END (segment, segment->p_vaddr)
4327 - SEGMENT_END (segment2, segment2->p_vaddr);
4329 if (extra_length > 0)
4331 segment2->p_memsz += extra_length;
4332 segment2->p_filesz += extra_length;
4335 segment->p_type = PT_NULL;
4337 /* Since we have deleted P we must restart the outer loop. */
4339 segment = elf_tdata (ibfd)->phdr;
4344 /* Extend SEGMENT to include SEGMENT2 and then delete
4347 SEGMENT_END (segment2, segment2->p_vaddr)
4348 - SEGMENT_END (segment, segment->p_vaddr);
4350 if (extra_length > 0)
4352 segment->p_memsz += extra_length;
4353 segment->p_filesz += extra_length;
4356 segment2->p_type = PT_NULL;
4361 /* The second scan attempts to assign sections to segments. */
4362 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4366 unsigned int section_count;
4367 asection ** sections;
4368 asection * output_section;
4370 bfd_vma matching_lma;
4371 bfd_vma suggested_lma;
4375 if (segment->p_type == PT_NULL)
4378 /* Compute how many sections might be placed into this segment. */
4380 for (section = ibfd->sections; section != NULL; section = section->next)
4381 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4384 /* Allocate a segment map big enough to contain all of the
4385 sections we have selected. */
4386 amt = sizeof (struct elf_segment_map);
4387 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4388 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4392 /* Initialise the fields of the segment map. Default to
4393 using the physical address of the segment in the input BFD. */
4395 map->p_type = segment->p_type;
4396 map->p_flags = segment->p_flags;
4397 map->p_flags_valid = 1;
4398 map->p_paddr = segment->p_paddr;
4399 map->p_paddr_valid = 1;
4401 /* Determine if this segment contains the ELF file header
4402 and if it contains the program headers themselves. */
4403 map->includes_filehdr = (segment->p_offset == 0
4404 && segment->p_filesz >= iehdr->e_ehsize);
4406 map->includes_phdrs = 0;
4408 if (! phdr_included || segment->p_type != PT_LOAD)
4410 map->includes_phdrs =
4411 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4412 && (segment->p_offset + segment->p_filesz
4413 >= ((bfd_vma) iehdr->e_phoff
4414 + iehdr->e_phnum * iehdr->e_phentsize)));
4416 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4417 phdr_included = true;
4420 if (section_count == 0)
4422 /* Special segments, such as the PT_PHDR segment, may contain
4423 no sections, but ordinary, loadable segments should contain
4425 if (segment->p_type == PT_LOAD)
4427 (_("%s: warning: Empty loadable segment detected\n"),
4428 bfd_archive_filename (ibfd));
4431 *pointer_to_map = map;
4432 pointer_to_map = &map->next;
4437 /* Now scan the sections in the input BFD again and attempt
4438 to add their corresponding output sections to the segment map.
4439 The problem here is how to handle an output section which has
4440 been moved (ie had its LMA changed). There are four possibilities:
4442 1. None of the sections have been moved.
4443 In this case we can continue to use the segment LMA from the
4446 2. All of the sections have been moved by the same amount.
4447 In this case we can change the segment's LMA to match the LMA
4448 of the first section.
4450 3. Some of the sections have been moved, others have not.
4451 In this case those sections which have not been moved can be
4452 placed in the current segment which will have to have its size,
4453 and possibly its LMA changed, and a new segment or segments will
4454 have to be created to contain the other sections.
4456 4. The sections have been moved, but not be the same amount.
4457 In this case we can change the segment's LMA to match the LMA
4458 of the first section and we will have to create a new segment
4459 or segments to contain the other sections.
4461 In order to save time, we allocate an array to hold the section
4462 pointers that we are interested in. As these sections get assigned
4463 to a segment, they are removed from this array. */
4465 amt = (bfd_size_type) section_count * sizeof (asection *);
4466 sections = (asection **) bfd_malloc (amt);
4467 if (sections == NULL)
4470 /* Step One: Scan for segment vs section LMA conflicts.
4471 Also add the sections to the section array allocated above.
4472 Also add the sections to the current segment. In the common
4473 case, where the sections have not been moved, this means that
4474 we have completely filled the segment, and there is nothing
4480 for (j = 0, section = ibfd->sections;
4482 section = section->next)
4484 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4486 output_section = section->output_section;
4488 sections[j ++] = section;
4490 /* The Solaris native linker always sets p_paddr to 0.
4491 We try to catch that case here, and set it to the
4493 if (segment->p_paddr == 0
4494 && segment->p_vaddr != 0
4496 && output_section->lma != 0
4497 && (output_section->vma == (segment->p_vaddr
4498 + (map->includes_filehdr
4501 + (map->includes_phdrs
4503 * iehdr->e_phentsize)
4505 map->p_paddr = segment->p_vaddr;
4507 /* Match up the physical address of the segment with the
4508 LMA address of the output section. */
4509 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4510 || IS_COREFILE_NOTE (segment, section))
4512 if (matching_lma == 0)
4513 matching_lma = output_section->lma;
4515 /* We assume that if the section fits within the segment
4516 then it does not overlap any other section within that
4518 map->sections[isec ++] = output_section;
4520 else if (suggested_lma == 0)
4521 suggested_lma = output_section->lma;
4525 BFD_ASSERT (j == section_count);
4527 /* Step Two: Adjust the physical address of the current segment,
4529 if (isec == section_count)
4531 /* All of the sections fitted within the segment as currently
4532 specified. This is the default case. Add the segment to
4533 the list of built segments and carry on to process the next
4534 program header in the input BFD. */
4535 map->count = section_count;
4536 *pointer_to_map = map;
4537 pointer_to_map = &map->next;
4544 if (matching_lma != 0)
4546 /* At least one section fits inside the current segment.
4547 Keep it, but modify its physical address to match the
4548 LMA of the first section that fitted. */
4549 map->p_paddr = matching_lma;
4553 /* None of the sections fitted inside the current segment.
4554 Change the current segment's physical address to match
4555 the LMA of the first section. */
4556 map->p_paddr = suggested_lma;
4559 /* Offset the segment physical address from the lma
4560 to allow for space taken up by elf headers. */
4561 if (map->includes_filehdr)
4562 map->p_paddr -= iehdr->e_ehsize;
4564 if (map->includes_phdrs)
4566 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4568 /* iehdr->e_phnum is just an estimate of the number
4569 of program headers that we will need. Make a note
4570 here of the number we used and the segment we chose
4571 to hold these headers, so that we can adjust the
4572 offset when we know the correct value. */
4573 phdr_adjust_num = iehdr->e_phnum;
4574 phdr_adjust_seg = map;
4578 /* Step Three: Loop over the sections again, this time assigning
4579 those that fit to the current segment and remvoing them from the
4580 sections array; but making sure not to leave large gaps. Once all
4581 possible sections have been assigned to the current segment it is
4582 added to the list of built segments and if sections still remain
4583 to be assigned, a new segment is constructed before repeating
4591 /* Fill the current segment with sections that fit. */
4592 for (j = 0; j < section_count; j++)
4594 section = sections[j];
4596 if (section == NULL)
4599 output_section = section->output_section;
4601 BFD_ASSERT (output_section != NULL);
4603 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4604 || IS_COREFILE_NOTE (segment, section))
4606 if (map->count == 0)
4608 /* If the first section in a segment does not start at
4609 the beginning of the segment, then something is
4611 if (output_section->lma !=
4613 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4614 + (map->includes_phdrs
4615 ? iehdr->e_phnum * iehdr->e_phentsize
4621 asection * prev_sec;
4623 prev_sec = map->sections[map->count - 1];
4625 /* If the gap between the end of the previous section
4626 and the start of this section is more than
4627 maxpagesize then we need to start a new segment. */
4628 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4630 < BFD_ALIGN (output_section->lma, maxpagesize))
4631 || ((prev_sec->lma + prev_sec->_raw_size)
4632 > output_section->lma))
4634 if (suggested_lma == 0)
4635 suggested_lma = output_section->lma;
4641 map->sections[map->count++] = output_section;
4644 section->segment_mark = true;
4646 else if (suggested_lma == 0)
4647 suggested_lma = output_section->lma;
4650 BFD_ASSERT (map->count > 0);
4652 /* Add the current segment to the list of built segments. */
4653 *pointer_to_map = map;
4654 pointer_to_map = &map->next;
4656 if (isec < section_count)
4658 /* We still have not allocated all of the sections to
4659 segments. Create a new segment here, initialise it
4660 and carry on looping. */
4661 amt = sizeof (struct elf_segment_map);
4662 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4663 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4667 /* Initialise the fields of the segment map. Set the physical
4668 physical address to the LMA of the first section that has
4669 not yet been assigned. */
4671 map->p_type = segment->p_type;
4672 map->p_flags = segment->p_flags;
4673 map->p_flags_valid = 1;
4674 map->p_paddr = suggested_lma;
4675 map->p_paddr_valid = 1;
4676 map->includes_filehdr = 0;
4677 map->includes_phdrs = 0;
4680 while (isec < section_count);
4685 /* The Solaris linker creates program headers in which all the
4686 p_paddr fields are zero. When we try to objcopy or strip such a
4687 file, we get confused. Check for this case, and if we find it
4688 reset the p_paddr_valid fields. */
4689 for (map = map_first; map != NULL; map = map->next)
4690 if (map->p_paddr != 0)
4694 for (map = map_first; map != NULL; map = map->next)
4695 map->p_paddr_valid = 0;
4698 elf_tdata (obfd)->segment_map = map_first;
4700 /* If we had to estimate the number of program headers that were
4701 going to be needed, then check our estimate now and adjust
4702 the offset if necessary. */
4703 if (phdr_adjust_seg != NULL)
4707 for (count = 0, map = map_first; map != NULL; map = map->next)
4710 if (count > phdr_adjust_num)
4711 phdr_adjust_seg->p_paddr
4712 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4716 /* Final Step: Sort the segments into ascending order of physical
4718 if (map_first != NULL)
4720 struct elf_segment_map *prev;
4723 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4725 /* Yes I know - its a bubble sort.... */
4726 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4728 /* Swap map and map->next. */
4729 prev->next = map->next;
4730 map->next = map->next->next;
4731 prev->next->next = map;
4741 #undef IS_CONTAINED_BY_VMA
4742 #undef IS_CONTAINED_BY_LMA
4743 #undef IS_COREFILE_NOTE
4744 #undef IS_SOLARIS_PT_INTERP
4745 #undef INCLUDE_SECTION_IN_SEGMENT
4746 #undef SEGMENT_AFTER_SEGMENT
4747 #undef SEGMENT_OVERLAPS
4751 /* Copy private section information. This copies over the entsize
4752 field, and sometimes the info field. */
4755 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4761 Elf_Internal_Shdr *ihdr, *ohdr;
4763 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4764 || obfd->xvec->flavour != bfd_target_elf_flavour)
4767 /* Copy over private BFD data if it has not already been copied.
4768 This must be done here, rather than in the copy_private_bfd_data
4769 entry point, because the latter is called after the section
4770 contents have been set, which means that the program headers have
4771 already been worked out. */
4772 if (elf_tdata (obfd)->segment_map == NULL
4773 && elf_tdata (ibfd)->phdr != NULL)
4777 /* Only set up the segments if there are no more SEC_ALLOC
4778 sections. FIXME: This won't do the right thing if objcopy is
4779 used to remove the last SEC_ALLOC section, since objcopy
4780 won't call this routine in that case. */
4781 for (s = isec->next; s != NULL; s = s->next)
4782 if ((s->flags & SEC_ALLOC) != 0)
4786 if (! copy_private_bfd_data (ibfd, obfd))
4791 ihdr = &elf_section_data (isec)->this_hdr;
4792 ohdr = &elf_section_data (osec)->this_hdr;
4794 ohdr->sh_entsize = ihdr->sh_entsize;
4796 if (ihdr->sh_type == SHT_SYMTAB
4797 || ihdr->sh_type == SHT_DYNSYM
4798 || ihdr->sh_type == SHT_GNU_verneed
4799 || ihdr->sh_type == SHT_GNU_verdef)
4800 ohdr->sh_info = ihdr->sh_info;
4802 elf_section_data (osec)->use_rela_p
4803 = elf_section_data (isec)->use_rela_p;
4808 /* Copy private symbol information. If this symbol is in a section
4809 which we did not map into a BFD section, try to map the section
4810 index correctly. We use special macro definitions for the mapped
4811 section indices; these definitions are interpreted by the
4812 swap_out_syms function. */
4814 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4815 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4816 #define MAP_STRTAB (SHN_HIOS + 3)
4817 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4818 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4821 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4827 elf_symbol_type *isym, *osym;
4829 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4830 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4833 isym = elf_symbol_from (ibfd, isymarg);
4834 osym = elf_symbol_from (obfd, osymarg);
4838 && bfd_is_abs_section (isym->symbol.section))
4842 shndx = isym->internal_elf_sym.st_shndx;
4843 if (shndx == elf_onesymtab (ibfd))
4844 shndx = MAP_ONESYMTAB;
4845 else if (shndx == elf_dynsymtab (ibfd))
4846 shndx = MAP_DYNSYMTAB;
4847 else if (shndx == elf_tdata (ibfd)->strtab_section)
4849 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4850 shndx = MAP_SHSTRTAB;
4851 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
4852 shndx = MAP_SYM_SHNDX;
4853 osym->internal_elf_sym.st_shndx = shndx;
4859 /* Swap out the symbols. */
4862 swap_out_syms (abfd, sttp, relocatable_p)
4864 struct bfd_strtab_hash **sttp;
4867 struct elf_backend_data *bed;
4870 struct bfd_strtab_hash *stt;
4871 Elf_Internal_Shdr *symtab_hdr;
4872 Elf_Internal_Shdr *symtab_shndx_hdr;
4873 Elf_Internal_Shdr *symstrtab_hdr;
4874 char *outbound_syms;
4875 char *outbound_shndx;
4879 if (!elf_map_symbols (abfd))
4882 /* Dump out the symtabs. */
4883 stt = _bfd_elf_stringtab_init ();
4887 bed = get_elf_backend_data (abfd);
4888 symcount = bfd_get_symcount (abfd);
4889 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4890 symtab_hdr->sh_type = SHT_SYMTAB;
4891 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4892 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4893 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4894 symtab_hdr->sh_addralign = bed->s->file_align;
4896 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4897 symstrtab_hdr->sh_type = SHT_STRTAB;
4899 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
4900 outbound_syms = bfd_alloc (abfd, amt);
4901 if (outbound_syms == NULL)
4903 symtab_hdr->contents = (PTR) outbound_syms;
4905 outbound_shndx = NULL;
4906 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
4907 if (symtab_shndx_hdr->sh_name != 0)
4909 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
4910 outbound_shndx = bfd_alloc (abfd, amt);
4911 if (outbound_shndx == NULL)
4913 memset (outbound_shndx, 0, (unsigned long) amt);
4914 symtab_shndx_hdr->contents = outbound_shndx;
4915 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
4916 symtab_shndx_hdr->sh_size = amt;
4917 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
4918 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
4921 /* now generate the data (for "contents") */
4923 /* Fill in zeroth symbol and swap it out. */
4924 Elf_Internal_Sym sym;
4930 sym.st_shndx = SHN_UNDEF;
4931 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
4932 outbound_syms += bed->s->sizeof_sym;
4933 if (outbound_shndx != NULL)
4934 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
4937 syms = bfd_get_outsymbols (abfd);
4938 for (idx = 0; idx < symcount; idx++)
4940 Elf_Internal_Sym sym;
4941 bfd_vma value = syms[idx]->value;
4942 elf_symbol_type *type_ptr;
4943 flagword flags = syms[idx]->flags;
4946 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4948 /* Local section symbols have no name. */
4953 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4956 if (sym.st_name == (unsigned long) -1)
4960 type_ptr = elf_symbol_from (abfd, syms[idx]);
4962 if ((flags & BSF_SECTION_SYM) == 0
4963 && bfd_is_com_section (syms[idx]->section))
4965 /* ELF common symbols put the alignment into the `value' field,
4966 and the size into the `size' field. This is backwards from
4967 how BFD handles it, so reverse it here. */
4968 sym.st_size = value;
4969 if (type_ptr == NULL
4970 || type_ptr->internal_elf_sym.st_value == 0)
4971 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4973 sym.st_value = type_ptr->internal_elf_sym.st_value;
4974 sym.st_shndx = _bfd_elf_section_from_bfd_section
4975 (abfd, syms[idx]->section);
4979 asection *sec = syms[idx]->section;
4982 if (sec->output_section)
4984 value += sec->output_offset;
4985 sec = sec->output_section;
4987 /* Don't add in the section vma for relocatable output. */
4988 if (! relocatable_p)
4990 sym.st_value = value;
4991 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4993 if (bfd_is_abs_section (sec)
4995 && type_ptr->internal_elf_sym.st_shndx != 0)
4997 /* This symbol is in a real ELF section which we did
4998 not create as a BFD section. Undo the mapping done
4999 by copy_private_symbol_data. */
5000 shndx = type_ptr->internal_elf_sym.st_shndx;
5004 shndx = elf_onesymtab (abfd);
5007 shndx = elf_dynsymtab (abfd);
5010 shndx = elf_tdata (abfd)->strtab_section;
5013 shndx = elf_tdata (abfd)->shstrtab_section;
5016 shndx = elf_tdata (abfd)->symtab_shndx_section;
5024 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5030 /* Writing this would be a hell of a lot easier if
5031 we had some decent documentation on bfd, and
5032 knew what to expect of the library, and what to
5033 demand of applications. For example, it
5034 appears that `objcopy' might not set the
5035 section of a symbol to be a section that is
5036 actually in the output file. */
5037 sec2 = bfd_get_section_by_name (abfd, sec->name);
5038 BFD_ASSERT (sec2 != 0);
5039 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5040 BFD_ASSERT (shndx != -1);
5044 sym.st_shndx = shndx;
5047 if ((flags & BSF_FUNCTION) != 0)
5049 else if ((flags & BSF_OBJECT) != 0)
5054 /* Processor-specific types */
5055 if (type_ptr != NULL
5056 && bed->elf_backend_get_symbol_type)
5057 type = ((*bed->elf_backend_get_symbol_type)
5058 (&type_ptr->internal_elf_sym, type));
5060 if (flags & BSF_SECTION_SYM)
5062 if (flags & BSF_GLOBAL)
5063 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5065 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5067 else if (bfd_is_com_section (syms[idx]->section))
5068 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5069 else if (bfd_is_und_section (syms[idx]->section))
5070 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5074 else if (flags & BSF_FILE)
5075 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5078 int bind = STB_LOCAL;
5080 if (flags & BSF_LOCAL)
5082 else if (flags & BSF_WEAK)
5084 else if (flags & BSF_GLOBAL)
5087 sym.st_info = ELF_ST_INFO (bind, type);
5090 if (type_ptr != NULL)
5091 sym.st_other = type_ptr->internal_elf_sym.st_other;
5095 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5096 outbound_syms += bed->s->sizeof_sym;
5097 if (outbound_shndx != NULL)
5098 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5102 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5103 symstrtab_hdr->sh_type = SHT_STRTAB;
5105 symstrtab_hdr->sh_flags = 0;
5106 symstrtab_hdr->sh_addr = 0;
5107 symstrtab_hdr->sh_entsize = 0;
5108 symstrtab_hdr->sh_link = 0;
5109 symstrtab_hdr->sh_info = 0;
5110 symstrtab_hdr->sh_addralign = 1;
5115 /* Return the number of bytes required to hold the symtab vector.
5117 Note that we base it on the count plus 1, since we will null terminate
5118 the vector allocated based on this size. However, the ELF symbol table
5119 always has a dummy entry as symbol #0, so it ends up even. */
5122 _bfd_elf_get_symtab_upper_bound (abfd)
5127 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5129 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5130 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5132 symtab_size -= sizeof (asymbol *);
5138 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5143 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5145 if (elf_dynsymtab (abfd) == 0)
5147 bfd_set_error (bfd_error_invalid_operation);
5151 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5152 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5154 symtab_size -= sizeof (asymbol *);
5160 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5161 bfd *abfd ATTRIBUTE_UNUSED;
5164 return (asect->reloc_count + 1) * sizeof (arelent *);
5167 /* Canonicalize the relocs. */
5170 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5178 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5180 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
5183 tblptr = section->relocation;
5184 for (i = 0; i < section->reloc_count; i++)
5185 *relptr++ = tblptr++;
5189 return section->reloc_count;
5193 _bfd_elf_get_symtab (abfd, alocation)
5195 asymbol **alocation;
5197 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5198 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
5201 bfd_get_symcount (abfd) = symcount;
5206 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5208 asymbol **alocation;
5210 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5211 return bed->s->slurp_symbol_table (abfd, alocation, true);
5214 /* Return the size required for the dynamic reloc entries. Any
5215 section that was actually installed in the BFD, and has type
5216 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5217 considered to be a dynamic reloc section. */
5220 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5226 if (elf_dynsymtab (abfd) == 0)
5228 bfd_set_error (bfd_error_invalid_operation);
5232 ret = sizeof (arelent *);
5233 for (s = abfd->sections; s != NULL; s = s->next)
5234 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5235 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5236 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5237 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5238 * sizeof (arelent *));
5243 /* Canonicalize the dynamic relocation entries. Note that we return
5244 the dynamic relocations as a single block, although they are
5245 actually associated with particular sections; the interface, which
5246 was designed for SunOS style shared libraries, expects that there
5247 is only one set of dynamic relocs. Any section that was actually
5248 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5249 the dynamic symbol table, is considered to be a dynamic reloc
5253 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5258 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5262 if (elf_dynsymtab (abfd) == 0)
5264 bfd_set_error (bfd_error_invalid_operation);
5268 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5270 for (s = abfd->sections; s != NULL; s = s->next)
5272 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5273 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5274 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5279 if (! (*slurp_relocs) (abfd, s, syms, true))
5281 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5283 for (i = 0; i < count; i++)
5294 /* Read in the version information. */
5297 _bfd_elf_slurp_version_tables (abfd)
5300 bfd_byte *contents = NULL;
5303 if (elf_dynverdef (abfd) != 0)
5305 Elf_Internal_Shdr *hdr;
5306 Elf_External_Verdef *everdef;
5307 Elf_Internal_Verdef *iverdef;
5308 Elf_Internal_Verdef *iverdefarr;
5309 Elf_Internal_Verdef iverdefmem;
5311 unsigned int maxidx;
5313 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5315 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5316 if (contents == NULL)
5318 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5319 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5322 /* We know the number of entries in the section but not the maximum
5323 index. Therefore we have to run through all entries and find
5325 everdef = (Elf_External_Verdef *) contents;
5327 for (i = 0; i < hdr->sh_info; ++i)
5329 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5331 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5332 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5334 everdef = ((Elf_External_Verdef *)
5335 ((bfd_byte *) everdef + iverdefmem.vd_next));
5338 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5339 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5340 if (elf_tdata (abfd)->verdef == NULL)
5343 elf_tdata (abfd)->cverdefs = maxidx;
5345 everdef = (Elf_External_Verdef *) contents;
5346 iverdefarr = elf_tdata (abfd)->verdef;
5347 for (i = 0; i < hdr->sh_info; i++)
5349 Elf_External_Verdaux *everdaux;
5350 Elf_Internal_Verdaux *iverdaux;
5353 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5355 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5356 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5358 iverdef->vd_bfd = abfd;
5360 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5361 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5362 if (iverdef->vd_auxptr == NULL)
5365 everdaux = ((Elf_External_Verdaux *)
5366 ((bfd_byte *) everdef + iverdef->vd_aux));
5367 iverdaux = iverdef->vd_auxptr;
5368 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5370 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5372 iverdaux->vda_nodename =
5373 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5374 iverdaux->vda_name);
5375 if (iverdaux->vda_nodename == NULL)
5378 if (j + 1 < iverdef->vd_cnt)
5379 iverdaux->vda_nextptr = iverdaux + 1;
5381 iverdaux->vda_nextptr = NULL;
5383 everdaux = ((Elf_External_Verdaux *)
5384 ((bfd_byte *) everdaux + iverdaux->vda_next));
5387 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5389 if (i + 1 < hdr->sh_info)
5390 iverdef->vd_nextdef = iverdef + 1;
5392 iverdef->vd_nextdef = NULL;
5394 everdef = ((Elf_External_Verdef *)
5395 ((bfd_byte *) everdef + iverdef->vd_next));
5402 if (elf_dynverref (abfd) != 0)
5404 Elf_Internal_Shdr *hdr;
5405 Elf_External_Verneed *everneed;
5406 Elf_Internal_Verneed *iverneed;
5409 hdr = &elf_tdata (abfd)->dynverref_hdr;
5411 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5412 elf_tdata (abfd)->verref =
5413 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5414 if (elf_tdata (abfd)->verref == NULL)
5417 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5419 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5420 if (contents == NULL)
5422 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5423 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5426 everneed = (Elf_External_Verneed *) contents;
5427 iverneed = elf_tdata (abfd)->verref;
5428 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5430 Elf_External_Vernaux *evernaux;
5431 Elf_Internal_Vernaux *ivernaux;
5434 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5436 iverneed->vn_bfd = abfd;
5438 iverneed->vn_filename =
5439 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5441 if (iverneed->vn_filename == NULL)
5444 amt = iverneed->vn_cnt;
5445 amt *= sizeof (Elf_Internal_Vernaux);
5446 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5448 evernaux = ((Elf_External_Vernaux *)
5449 ((bfd_byte *) everneed + iverneed->vn_aux));
5450 ivernaux = iverneed->vn_auxptr;
5451 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5453 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5455 ivernaux->vna_nodename =
5456 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5457 ivernaux->vna_name);
5458 if (ivernaux->vna_nodename == NULL)
5461 if (j + 1 < iverneed->vn_cnt)
5462 ivernaux->vna_nextptr = ivernaux + 1;
5464 ivernaux->vna_nextptr = NULL;
5466 evernaux = ((Elf_External_Vernaux *)
5467 ((bfd_byte *) evernaux + ivernaux->vna_next));
5470 if (i + 1 < hdr->sh_info)
5471 iverneed->vn_nextref = iverneed + 1;
5473 iverneed->vn_nextref = NULL;
5475 everneed = ((Elf_External_Verneed *)
5476 ((bfd_byte *) everneed + iverneed->vn_next));
5486 if (contents == NULL)
5492 _bfd_elf_make_empty_symbol (abfd)
5495 elf_symbol_type *newsym;
5496 bfd_size_type amt = sizeof (elf_symbol_type);
5498 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5503 newsym->symbol.the_bfd = abfd;
5504 return &newsym->symbol;
5509 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5510 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5514 bfd_symbol_info (symbol, ret);
5517 /* Return whether a symbol name implies a local symbol. Most targets
5518 use this function for the is_local_label_name entry point, but some
5522 _bfd_elf_is_local_label_name (abfd, name)
5523 bfd *abfd ATTRIBUTE_UNUSED;
5526 /* Normal local symbols start with ``.L''. */
5527 if (name[0] == '.' && name[1] == 'L')
5530 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5531 DWARF debugging symbols starting with ``..''. */
5532 if (name[0] == '.' && name[1] == '.')
5535 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5536 emitting DWARF debugging output. I suspect this is actually a
5537 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5538 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5539 underscore to be emitted on some ELF targets). For ease of use,
5540 we treat such symbols as local. */
5541 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5548 _bfd_elf_get_lineno (ignore_abfd, symbol)
5549 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5550 asymbol *symbol ATTRIBUTE_UNUSED;
5557 _bfd_elf_set_arch_mach (abfd, arch, machine)
5559 enum bfd_architecture arch;
5560 unsigned long machine;
5562 /* If this isn't the right architecture for this backend, and this
5563 isn't the generic backend, fail. */
5564 if (arch != get_elf_backend_data (abfd)->arch
5565 && arch != bfd_arch_unknown
5566 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5569 return bfd_default_set_arch_mach (abfd, arch, machine);
5572 /* Find the function to a particular section and offset,
5573 for error reporting. */
5576 elf_find_function (abfd, section, symbols, offset,
5577 filename_ptr, functionname_ptr)
5578 bfd *abfd ATTRIBUTE_UNUSED;
5582 const char **filename_ptr;
5583 const char **functionname_ptr;
5585 const char *filename;
5594 for (p = symbols; *p != NULL; p++)
5598 q = (elf_symbol_type *) *p;
5600 if (bfd_get_section (&q->symbol) != section)
5603 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5608 filename = bfd_asymbol_name (&q->symbol);
5612 if (q->symbol.section == section
5613 && q->symbol.value >= low_func
5614 && q->symbol.value <= offset)
5616 func = (asymbol *) q;
5617 low_func = q->symbol.value;
5627 *filename_ptr = filename;
5628 if (functionname_ptr)
5629 *functionname_ptr = bfd_asymbol_name (func);
5634 /* Find the nearest line to a particular section and offset,
5635 for error reporting. */
5638 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5639 filename_ptr, functionname_ptr, line_ptr)
5644 const char **filename_ptr;
5645 const char **functionname_ptr;
5646 unsigned int *line_ptr;
5650 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5651 filename_ptr, functionname_ptr,
5654 if (!*functionname_ptr)
5655 elf_find_function (abfd, section, symbols, offset,
5656 *filename_ptr ? NULL : filename_ptr,
5662 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5663 filename_ptr, functionname_ptr,
5665 &elf_tdata (abfd)->dwarf2_find_line_info))
5667 if (!*functionname_ptr)
5668 elf_find_function (abfd, section, symbols, offset,
5669 *filename_ptr ? NULL : filename_ptr,
5675 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5676 &found, filename_ptr,
5677 functionname_ptr, line_ptr,
5678 &elf_tdata (abfd)->line_info))
5683 if (symbols == NULL)
5686 if (! elf_find_function (abfd, section, symbols, offset,
5687 filename_ptr, functionname_ptr))
5695 _bfd_elf_sizeof_headers (abfd, reloc)
5701 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5703 ret += get_program_header_size (abfd);
5708 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5713 bfd_size_type count;
5715 Elf_Internal_Shdr *hdr;
5718 if (! abfd->output_has_begun
5719 && ! _bfd_elf_compute_section_file_positions
5720 (abfd, (struct bfd_link_info *) NULL))
5723 hdr = &elf_section_data (section)->this_hdr;
5724 pos = hdr->sh_offset + offset;
5725 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5726 || bfd_bwrite (location, count, abfd) != count)
5733 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5734 bfd *abfd ATTRIBUTE_UNUSED;
5735 arelent *cache_ptr ATTRIBUTE_UNUSED;
5736 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5743 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5746 Elf_Internal_Rel *dst;
5752 /* Try to convert a non-ELF reloc into an ELF one. */
5755 _bfd_elf_validate_reloc (abfd, areloc)
5759 /* Check whether we really have an ELF howto. */
5761 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5763 bfd_reloc_code_real_type code;
5764 reloc_howto_type *howto;
5766 /* Alien reloc: Try to determine its type to replace it with an
5767 equivalent ELF reloc. */
5769 if (areloc->howto->pc_relative)
5771 switch (areloc->howto->bitsize)
5774 code = BFD_RELOC_8_PCREL;
5777 code = BFD_RELOC_12_PCREL;
5780 code = BFD_RELOC_16_PCREL;
5783 code = BFD_RELOC_24_PCREL;
5786 code = BFD_RELOC_32_PCREL;
5789 code = BFD_RELOC_64_PCREL;
5795 howto = bfd_reloc_type_lookup (abfd, code);
5797 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5799 if (howto->pcrel_offset)
5800 areloc->addend += areloc->address;
5802 areloc->addend -= areloc->address; /* addend is unsigned!! */
5807 switch (areloc->howto->bitsize)
5813 code = BFD_RELOC_14;
5816 code = BFD_RELOC_16;
5819 code = BFD_RELOC_26;
5822 code = BFD_RELOC_32;
5825 code = BFD_RELOC_64;
5831 howto = bfd_reloc_type_lookup (abfd, code);
5835 areloc->howto = howto;
5843 (*_bfd_error_handler)
5844 (_("%s: unsupported relocation type %s"),
5845 bfd_archive_filename (abfd), areloc->howto->name);
5846 bfd_set_error (bfd_error_bad_value);
5851 _bfd_elf_close_and_cleanup (abfd)
5854 if (bfd_get_format (abfd) == bfd_object)
5856 if (elf_shstrtab (abfd) != NULL)
5857 _bfd_elf_strtab_free (elf_shstrtab (abfd));
5860 return _bfd_generic_close_and_cleanup (abfd);
5863 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5864 in the relocation's offset. Thus we cannot allow any sort of sanity
5865 range-checking to interfere. There is nothing else to do in processing
5868 bfd_reloc_status_type
5869 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5870 bfd *abfd ATTRIBUTE_UNUSED;
5871 arelent *re ATTRIBUTE_UNUSED;
5872 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5873 PTR data ATTRIBUTE_UNUSED;
5874 asection *is ATTRIBUTE_UNUSED;
5875 bfd *obfd ATTRIBUTE_UNUSED;
5876 char **errmsg ATTRIBUTE_UNUSED;
5878 return bfd_reloc_ok;
5881 /* Elf core file support. Much of this only works on native
5882 toolchains, since we rely on knowing the
5883 machine-dependent procfs structure in order to pick
5884 out details about the corefile. */
5886 #ifdef HAVE_SYS_PROCFS_H
5887 # include <sys/procfs.h>
5890 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5893 elfcore_make_pid (abfd)
5896 return ((elf_tdata (abfd)->core_lwpid << 16)
5897 + (elf_tdata (abfd)->core_pid));
5900 /* If there isn't a section called NAME, make one, using
5901 data from SECT. Note, this function will generate a
5902 reference to NAME, so you shouldn't deallocate or
5906 elfcore_maybe_make_sect (abfd, name, sect)
5913 if (bfd_get_section_by_name (abfd, name) != NULL)
5916 sect2 = bfd_make_section (abfd, name);
5920 sect2->_raw_size = sect->_raw_size;
5921 sect2->filepos = sect->filepos;
5922 sect2->flags = sect->flags;
5923 sect2->alignment_power = sect->alignment_power;
5927 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5928 actually creates up to two pseudosections:
5929 - For the single-threaded case, a section named NAME, unless
5930 such a section already exists.
5931 - For the multi-threaded case, a section named "NAME/PID", where
5932 PID is elfcore_make_pid (abfd).
5933 Both pseudosections have identical contents. */
5935 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5942 char *threaded_name;
5945 /* Build the section name. */
5947 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5948 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
5949 if (threaded_name == NULL)
5951 strcpy (threaded_name, buf);
5953 sect = bfd_make_section (abfd, threaded_name);
5956 sect->_raw_size = size;
5957 sect->filepos = filepos;
5958 sect->flags = SEC_HAS_CONTENTS;
5959 sect->alignment_power = 2;
5961 return elfcore_maybe_make_sect (abfd, name, sect);
5964 /* prstatus_t exists on:
5966 linux 2.[01] + glibc
5970 #if defined (HAVE_PRSTATUS_T)
5971 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5974 elfcore_grok_prstatus (abfd, note)
5976 Elf_Internal_Note *note;
5981 if (note->descsz == sizeof (prstatus_t))
5985 raw_size = sizeof (prstat.pr_reg);
5986 offset = offsetof (prstatus_t, pr_reg);
5987 memcpy (&prstat, note->descdata, sizeof (prstat));
5989 /* Do not overwrite the core signal if it
5990 has already been set by another thread. */
5991 if (elf_tdata (abfd)->core_signal == 0)
5992 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5993 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5995 /* pr_who exists on:
5998 pr_who doesn't exist on:
6001 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6002 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6005 #if defined (HAVE_PRSTATUS32_T)
6006 else if (note->descsz == sizeof (prstatus32_t))
6008 /* 64-bit host, 32-bit corefile */
6009 prstatus32_t prstat;
6011 raw_size = sizeof (prstat.pr_reg);
6012 offset = offsetof (prstatus32_t, pr_reg);
6013 memcpy (&prstat, note->descdata, sizeof (prstat));
6015 /* Do not overwrite the core signal if it
6016 has already been set by another thread. */
6017 if (elf_tdata (abfd)->core_signal == 0)
6018 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6019 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6021 /* pr_who exists on:
6024 pr_who doesn't exist on:
6027 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6028 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6031 #endif /* HAVE_PRSTATUS32_T */
6034 /* Fail - we don't know how to handle any other
6035 note size (ie. data object type). */
6039 /* Make a ".reg/999" section and a ".reg" section. */
6040 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6041 raw_size, note->descpos + offset);
6043 #endif /* defined (HAVE_PRSTATUS_T) */
6045 /* Create a pseudosection containing the exact contents of NOTE. */
6047 elfcore_make_note_pseudosection (abfd, name, note)
6050 Elf_Internal_Note *note;
6052 return _bfd_elfcore_make_pseudosection (abfd, name,
6053 note->descsz, note->descpos);
6056 /* There isn't a consistent prfpregset_t across platforms,
6057 but it doesn't matter, because we don't have to pick this
6058 data structure apart. */
6061 elfcore_grok_prfpreg (abfd, note)
6063 Elf_Internal_Note *note;
6065 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6068 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6069 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6073 elfcore_grok_prxfpreg (abfd, note)
6075 Elf_Internal_Note *note;
6077 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6080 #if defined (HAVE_PRPSINFO_T)
6081 typedef prpsinfo_t elfcore_psinfo_t;
6082 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6083 typedef prpsinfo32_t elfcore_psinfo32_t;
6087 #if defined (HAVE_PSINFO_T)
6088 typedef psinfo_t elfcore_psinfo_t;
6089 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6090 typedef psinfo32_t elfcore_psinfo32_t;
6094 /* return a malloc'ed copy of a string at START which is at
6095 most MAX bytes long, possibly without a terminating '\0'.
6096 the copy will always have a terminating '\0'. */
6099 _bfd_elfcore_strndup (abfd, start, max)
6105 char *end = memchr (start, '\0', max);
6113 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6117 memcpy (dups, start, len);
6123 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6124 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
6127 elfcore_grok_psinfo (abfd, note)
6129 Elf_Internal_Note *note;
6131 if (note->descsz == sizeof (elfcore_psinfo_t))
6133 elfcore_psinfo_t psinfo;
6135 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6137 elf_tdata (abfd)->core_program
6138 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6139 sizeof (psinfo.pr_fname));
6141 elf_tdata (abfd)->core_command
6142 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6143 sizeof (psinfo.pr_psargs));
6145 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6146 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6148 /* 64-bit host, 32-bit corefile */
6149 elfcore_psinfo32_t psinfo;
6151 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6153 elf_tdata (abfd)->core_program
6154 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6155 sizeof (psinfo.pr_fname));
6157 elf_tdata (abfd)->core_command
6158 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6159 sizeof (psinfo.pr_psargs));
6165 /* Fail - we don't know how to handle any other
6166 note size (ie. data object type). */
6170 /* Note that for some reason, a spurious space is tacked
6171 onto the end of the args in some (at least one anyway)
6172 implementations, so strip it off if it exists. */
6175 char *command = elf_tdata (abfd)->core_command;
6176 int n = strlen (command);
6178 if (0 < n && command[n - 1] == ' ')
6179 command[n - 1] = '\0';
6184 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6186 #if defined (HAVE_PSTATUS_T)
6187 static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *));
6190 elfcore_grok_pstatus (abfd, note)
6192 Elf_Internal_Note *note;
6194 if (note->descsz == sizeof (pstatus_t)
6195 #if defined (HAVE_PXSTATUS_T)
6196 || note->descsz == sizeof (pxstatus_t)
6202 memcpy (&pstat, note->descdata, sizeof (pstat));
6204 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6206 #if defined (HAVE_PSTATUS32_T)
6207 else if (note->descsz == sizeof (pstatus32_t))
6209 /* 64-bit host, 32-bit corefile */
6212 memcpy (&pstat, note->descdata, sizeof (pstat));
6214 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6217 /* Could grab some more details from the "representative"
6218 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6219 NT_LWPSTATUS note, presumably. */
6223 #endif /* defined (HAVE_PSTATUS_T) */
6225 #if defined (HAVE_LWPSTATUS_T)
6226 static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *));
6229 elfcore_grok_lwpstatus (abfd, note)
6231 Elf_Internal_Note *note;
6233 lwpstatus_t lwpstat;
6238 if (note->descsz != sizeof (lwpstat)
6239 #if defined (HAVE_LWPXSTATUS_T)
6240 && note->descsz != sizeof (lwpxstatus_t)
6245 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6247 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6248 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6250 /* Make a ".reg/999" section. */
6252 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6253 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6258 sect = bfd_make_section (abfd, name);
6262 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6263 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6264 sect->filepos = note->descpos
6265 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6268 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6269 sect->_raw_size = sizeof (lwpstat.pr_reg);
6270 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6273 sect->flags = SEC_HAS_CONTENTS;
6274 sect->alignment_power = 2;
6276 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6279 /* Make a ".reg2/999" section */
6281 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6282 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6287 sect = bfd_make_section (abfd, name);
6291 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6292 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6293 sect->filepos = note->descpos
6294 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6297 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6298 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6299 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6302 sect->flags = SEC_HAS_CONTENTS;
6303 sect->alignment_power = 2;
6305 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6307 #endif /* defined (HAVE_LWPSTATUS_T) */
6309 #if defined (HAVE_WIN32_PSTATUS_T)
6311 elfcore_grok_win32pstatus (abfd, note)
6313 Elf_Internal_Note *note;
6318 win32_pstatus_t pstatus;
6320 if (note->descsz < sizeof (pstatus))
6323 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6325 switch (pstatus.data_type)
6327 case NOTE_INFO_PROCESS:
6328 /* FIXME: need to add ->core_command. */
6329 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6330 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6333 case NOTE_INFO_THREAD:
6334 /* Make a ".reg/999" section. */
6335 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6337 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6343 sect = bfd_make_section (abfd, name);
6347 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6348 sect->filepos = (note->descpos
6349 + offsetof (struct win32_pstatus,
6350 data.thread_info.thread_context));
6351 sect->flags = SEC_HAS_CONTENTS;
6352 sect->alignment_power = 2;
6354 if (pstatus.data.thread_info.is_active_thread)
6355 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6359 case NOTE_INFO_MODULE:
6360 /* Make a ".module/xxxxxxxx" section. */
6361 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6363 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6369 sect = bfd_make_section (abfd, name);
6374 sect->_raw_size = note->descsz;
6375 sect->filepos = note->descpos;
6376 sect->flags = SEC_HAS_CONTENTS;
6377 sect->alignment_power = 2;
6386 #endif /* HAVE_WIN32_PSTATUS_T */
6389 elfcore_grok_note (abfd, note)
6391 Elf_Internal_Note *note;
6393 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6401 if (bed->elf_backend_grok_prstatus)
6402 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6404 #if defined (HAVE_PRSTATUS_T)
6405 return elfcore_grok_prstatus (abfd, note);
6410 #if defined (HAVE_PSTATUS_T)
6412 return elfcore_grok_pstatus (abfd, note);
6415 #if defined (HAVE_LWPSTATUS_T)
6417 return elfcore_grok_lwpstatus (abfd, note);
6420 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6421 return elfcore_grok_prfpreg (abfd, note);
6423 #if defined (HAVE_WIN32_PSTATUS_T)
6424 case NT_WIN32PSTATUS:
6425 return elfcore_grok_win32pstatus (abfd, note);
6428 case NT_PRXFPREG: /* Linux SSE extension */
6429 if (note->namesz == 5
6430 && ! strcmp (note->namedata, "LINUX"))
6431 return elfcore_grok_prxfpreg (abfd, note);
6437 if (bed->elf_backend_grok_psinfo)
6438 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6440 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6441 return elfcore_grok_psinfo (abfd, note);
6449 elfcore_netbsd_get_lwpid (note, lwpidp)
6450 Elf_Internal_Note *note;
6455 cp = strchr (note->namedata, '@');
6458 *lwpidp = atoi(cp + 1);
6465 elfcore_grok_netbsd_procinfo (abfd, note)
6467 Elf_Internal_Note *note;
6470 /* Signal number at offset 0x08. */
6471 elf_tdata (abfd)->core_signal
6472 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6474 /* Process ID at offset 0x50. */
6475 elf_tdata (abfd)->core_pid
6476 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6478 /* Command name at 0x7c (max 32 bytes, including nul). */
6479 elf_tdata (abfd)->core_command
6480 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6486 elfcore_grok_netbsd_note (abfd, note)
6488 Elf_Internal_Note *note;
6492 if (elfcore_netbsd_get_lwpid (note, &lwp))
6493 elf_tdata (abfd)->core_lwpid = lwp;
6495 if (note->type == NT_NETBSDCORE_PROCINFO)
6497 /* NetBSD-specific core "procinfo". Note that we expect to
6498 find this note before any of the others, which is fine,
6499 since the kernel writes this note out first when it
6500 creates a core file. */
6502 return elfcore_grok_netbsd_procinfo (abfd, note);
6505 /* As of Jan 2002 there are no other machine-independent notes
6506 defined for NetBSD core files. If the note type is less
6507 than the start of the machine-dependent note types, we don't
6510 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6514 switch (bfd_get_arch (abfd))
6516 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6517 PT_GETFPREGS == mach+2. */
6519 case bfd_arch_alpha:
6520 case bfd_arch_sparc:
6523 case NT_NETBSDCORE_FIRSTMACH+0:
6524 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6526 case NT_NETBSDCORE_FIRSTMACH+2:
6527 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6533 /* On all other arch's, PT_GETREGS == mach+1 and
6534 PT_GETFPREGS == mach+3. */
6539 case NT_NETBSDCORE_FIRSTMACH+1:
6540 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6542 case NT_NETBSDCORE_FIRSTMACH+3:
6543 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6552 /* Function: elfcore_write_note
6559 size of data for note
6562 End of buffer containing note. */
6565 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6574 Elf_External_Note *xnp;
6575 int namesz = strlen (name);
6576 int newspace = BFD_ALIGN (sizeof (Elf_External_Note) + size + namesz - 1, 4);
6579 p = realloc (buf, *bufsiz + newspace);
6581 *bufsiz += newspace;
6582 xnp = (Elf_External_Note *) dest;
6583 H_PUT_32 (abfd, namesz, xnp->namesz);
6584 H_PUT_32 (abfd, size, xnp->descsz);
6585 H_PUT_32 (abfd, type, xnp->type);
6586 strcpy (xnp->name, name);
6587 memcpy (xnp->name + BFD_ALIGN (namesz, 4), input, size);
6591 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6593 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6601 char *note_name = "CORE";
6603 #if defined (HAVE_PSINFO_T)
6605 note_type = NT_PSINFO;
6608 note_type = NT_PRPSINFO;
6611 memset (&data, 0, sizeof (data));
6612 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
6613 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
6614 return elfcore_write_note (abfd, buf, bufsiz,
6615 note_name, note_type, &data, sizeof (data));
6617 #endif /* PSINFO_T or PRPSINFO_T */
6619 #if defined (HAVE_PRSTATUS_T)
6621 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6630 char *note_name = "CORE";
6632 memset (&prstat, 0, sizeof (prstat));
6633 prstat.pr_pid = pid;
6634 prstat.pr_cursig = cursig;
6635 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
6636 return elfcore_write_note (abfd, buf, bufsiz,
6637 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
6639 #endif /* HAVE_PRSTATUS_T */
6641 #if defined (HAVE_LWPSTATUS_T)
6643 elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6651 lwpstatus_t lwpstat;
6652 char *note_name = "CORE";
6654 memset (&lwpstat, 0, sizeof (lwpstat));
6655 lwpstat.pr_lwpid = pid >> 16;
6656 lwpstat.pr_cursig = cursig;
6657 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6658 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
6659 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6661 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
6662 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
6664 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
6665 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
6668 return elfcore_write_note (abfd, buf, bufsiz, note_name,
6669 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
6671 #endif /* HAVE_LWPSTATUS_T */
6673 #if defined (HAVE_PSTATUS_T)
6675 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6684 char *note_name = "CORE";
6686 memset (&pstat, 0, sizeof (pstat));
6687 pstat.pr_pid = pid & 0xffff;
6688 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
6689 NT_PSTATUS, &pstat, sizeof (pstat));
6692 #endif /* HAVE_PSTATUS_T */
6695 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
6702 char *note_name = "CORE";
6703 return elfcore_write_note (abfd, buf, bufsiz,
6704 note_name, NT_FPREGSET, fpregs, size);
6708 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
6715 char *note_name = "LINUX";
6716 return elfcore_write_note (abfd, buf, bufsiz,
6717 note_name, NT_PRXFPREG, xfpregs, size);
6721 elfcore_read_notes (abfd, offset, size)
6732 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
6735 buf = bfd_malloc (size);
6739 if (bfd_bread (buf, size, abfd) != size)
6747 while (p < buf + size)
6749 /* FIXME: bad alignment assumption. */
6750 Elf_External_Note *xnp = (Elf_External_Note *) p;
6751 Elf_Internal_Note in;
6753 in.type = H_GET_32 (abfd, xnp->type);
6755 in.namesz = H_GET_32 (abfd, xnp->namesz);
6756 in.namedata = xnp->name;
6758 in.descsz = H_GET_32 (abfd, xnp->descsz);
6759 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
6760 in.descpos = offset + (in.descdata - buf);
6762 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
6764 if (! elfcore_grok_netbsd_note (abfd, &in))
6769 if (! elfcore_grok_note (abfd, &in))
6773 p = in.descdata + BFD_ALIGN (in.descsz, 4);
6780 /* Providing external access to the ELF program header table. */
6782 /* Return an upper bound on the number of bytes required to store a
6783 copy of ABFD's program header table entries. Return -1 if an error
6784 occurs; bfd_get_error will return an appropriate code. */
6787 bfd_get_elf_phdr_upper_bound (abfd)
6790 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6792 bfd_set_error (bfd_error_wrong_format);
6796 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
6799 /* Copy ABFD's program header table entries to *PHDRS. The entries
6800 will be stored as an array of Elf_Internal_Phdr structures, as
6801 defined in include/elf/internal.h. To find out how large the
6802 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6804 Return the number of program header table entries read, or -1 if an
6805 error occurs; bfd_get_error will return an appropriate code. */
6808 bfd_get_elf_phdrs (abfd, phdrs)
6814 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6816 bfd_set_error (bfd_error_wrong_format);
6820 num_phdrs = elf_elfheader (abfd)->e_phnum;
6821 memcpy (phdrs, elf_tdata (abfd)->phdr,
6822 num_phdrs * sizeof (Elf_Internal_Phdr));
6828 _bfd_elf_sprintf_vma (abfd, buf, value)
6829 bfd *abfd ATTRIBUTE_UNUSED;
6834 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6836 i_ehdrp = elf_elfheader (abfd);
6837 if (i_ehdrp == NULL)
6838 sprintf_vma (buf, value);
6841 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6843 #if BFD_HOST_64BIT_LONG
6844 sprintf (buf, "%016lx", value);
6846 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6847 _bfd_int64_low (value));
6851 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6854 sprintf_vma (buf, value);
6859 _bfd_elf_fprintf_vma (abfd, stream, value)
6860 bfd *abfd ATTRIBUTE_UNUSED;
6865 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6867 i_ehdrp = elf_elfheader (abfd);
6868 if (i_ehdrp == NULL)
6869 fprintf_vma ((FILE *) stream, value);
6872 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6874 #if BFD_HOST_64BIT_LONG
6875 fprintf ((FILE *) stream, "%016lx", value);
6877 fprintf ((FILE *) stream, "%08lx%08lx",
6878 _bfd_int64_high (value), _bfd_int64_low (value));
6882 fprintf ((FILE *) stream, "%08lx",
6883 (unsigned long) (value & 0xffffffff));
6886 fprintf_vma ((FILE *) stream, value);
6890 enum elf_reloc_type_class
6891 _bfd_elf_reloc_type_class (rela)
6892 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
6894 return reloc_class_normal;
6897 /* For RELA architectures, return what the relocation value for
6898 relocation against a local symbol. */
6901 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
6903 Elf_Internal_Sym *sym;
6905 Elf_Internal_Rela *rel;
6909 relocation = (sec->output_section->vma
6910 + sec->output_offset
6912 if ((sec->flags & SEC_MERGE)
6913 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
6914 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6920 _bfd_merged_section_offset (abfd, &msec,
6921 elf_section_data (sec)->sec_info,
6922 sym->st_value + rel->r_addend,
6925 rel->r_addend += msec->output_section->vma + msec->output_offset;
6931 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
6933 Elf_Internal_Sym *sym;
6937 asection *sec = *psec;
6939 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
6940 return sym->st_value + addend;
6942 return _bfd_merged_section_offset (abfd, psec,
6943 elf_section_data (sec)->sec_info,
6944 sym->st_value + addend, (bfd_vma) 0);
6948 _bfd_elf_section_offset (abfd, info, sec, offset)
6950 struct bfd_link_info *info;
6954 struct bfd_elf_section_data *sec_data;
6956 sec_data = elf_section_data (sec);
6957 switch (sec_data->sec_info_type)
6959 case ELF_INFO_TYPE_STABS:
6960 return _bfd_stab_section_offset
6961 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
6963 case ELF_INFO_TYPE_EH_FRAME:
6964 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);