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. */
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 const char *group_signature PARAMS ((bfd *, Elf_Internal_Shdr *));
58 static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
59 static void merge_sections_remove_hook PARAMS ((bfd *, asection *));
60 static void elf_fake_sections 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 /* Read and convert symbols to internal format.
360 SYMCOUNT specifies the number of symbols to read, starting from
361 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
362 are non-NULL, they are used to store the internal symbols, external
363 symbols, and symbol section index extensions, respectively. */
366 bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset,
367 intsym_buf, extsym_buf, extshndx_buf)
369 Elf_Internal_Shdr *symtab_hdr;
372 Elf_Internal_Sym *intsym_buf;
374 Elf_External_Sym_Shndx *extshndx_buf;
376 Elf_Internal_Shdr *shndx_hdr;
378 const bfd_byte *esym;
379 Elf_External_Sym_Shndx *alloc_extshndx;
380 Elf_External_Sym_Shndx *shndx;
381 Elf_Internal_Sym *isym;
382 Elf_Internal_Sym *isymend;
383 struct elf_backend_data *bed;
391 /* Normal syms might have section extension entries. */
393 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
394 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
396 /* Read the symbols. */
398 alloc_extshndx = NULL;
399 bed = get_elf_backend_data (ibfd);
400 extsym_size = bed->s->sizeof_sym;
401 amt = symcount * extsym_size;
402 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
403 if (extsym_buf == NULL)
405 alloc_ext = bfd_malloc (amt);
406 extsym_buf = alloc_ext;
408 if (extsym_buf == NULL
409 || bfd_seek (ibfd, pos, SEEK_SET) != 0
410 || bfd_bread (extsym_buf, amt, ibfd) != amt)
416 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
420 amt = symcount * sizeof (Elf_External_Sym_Shndx);
421 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
422 if (extshndx_buf == NULL)
424 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
425 extshndx_buf = alloc_extshndx;
427 if (extshndx_buf == NULL
428 || bfd_seek (ibfd, pos, SEEK_SET) != 0
429 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
436 if (intsym_buf == NULL)
438 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
439 intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt);
440 if (intsym_buf == NULL)
444 /* Convert the symbols to internal form. */
445 isymend = intsym_buf + symcount;
446 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
448 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
449 (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym);
452 if (alloc_ext != NULL)
454 if (alloc_extshndx != NULL)
455 free (alloc_extshndx);
460 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
461 sections. The first element is the flags, the rest are section
464 typedef union elf_internal_group {
465 Elf_Internal_Shdr *shdr;
467 } Elf_Internal_Group;
469 /* Return the name of the group signature symbol. Why isn't the
470 signature just a string? */
473 group_signature (abfd, ghdr)
475 Elf_Internal_Shdr *ghdr;
477 Elf_Internal_Shdr *hdr;
478 unsigned char esym[sizeof (Elf64_External_Sym)];
479 Elf_External_Sym_Shndx eshndx;
480 Elf_Internal_Sym isym;
482 unsigned int shindex;
484 /* First we need to ensure the symbol table is available. */
485 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
488 /* Go read the symbol. */
489 hdr = &elf_tdata (abfd)->symtab_hdr;
490 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
491 &isym, esym, &eshndx) == NULL)
494 /* Look up the symbol name. */
495 iname = isym.st_name;
496 shindex = hdr->sh_link;
497 if (iname == 0 && ELF_ST_TYPE (isym.st_info) == STT_SECTION)
499 iname = elf_elfsections (abfd)[isym.st_shndx]->sh_name;
500 shindex = elf_elfheader (abfd)->e_shstrndx;
503 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
506 /* Set next_in_group list pointer, and group name for NEWSECT. */
509 setup_group (abfd, hdr, newsect)
511 Elf_Internal_Shdr *hdr;
514 unsigned int num_group = elf_tdata (abfd)->num_group;
516 /* If num_group is zero, read in all SHT_GROUP sections. The count
517 is set to -1 if there are no SHT_GROUP sections. */
520 unsigned int i, shnum;
522 /* First count the number of groups. If we have a SHT_GROUP
523 section with just a flag word (ie. sh_size is 4), ignore it. */
524 shnum = elf_numsections (abfd);
526 for (i = 0; i < shnum; i++)
528 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
529 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
534 num_group = (unsigned) -1;
535 elf_tdata (abfd)->num_group = num_group;
539 /* We keep a list of elf section headers for group sections,
540 so we can find them quickly. */
541 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
542 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
543 if (elf_tdata (abfd)->group_sect_ptr == NULL)
547 for (i = 0; i < shnum; i++)
549 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
550 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
553 Elf_Internal_Group *dest;
555 /* Add to list of sections. */
556 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
559 /* Read the raw contents. */
560 BFD_ASSERT (sizeof (*dest) >= 4);
561 amt = shdr->sh_size * sizeof (*dest) / 4;
562 shdr->contents = bfd_alloc (abfd, amt);
563 if (shdr->contents == NULL
564 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
565 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
569 /* Translate raw contents, a flag word followed by an
570 array of elf section indices all in target byte order,
571 to the flag word followed by an array of elf section
573 src = shdr->contents + shdr->sh_size;
574 dest = (Elf_Internal_Group *) (shdr->contents + amt);
581 idx = H_GET_32 (abfd, src);
582 if (src == shdr->contents)
585 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
586 shdr->bfd_section->flags
587 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
592 ((*_bfd_error_handler)
593 (_("%s: invalid SHT_GROUP entry"),
594 bfd_archive_filename (abfd)));
597 dest->shdr = elf_elfsections (abfd)[idx];
604 if (num_group != (unsigned) -1)
608 for (i = 0; i < num_group; i++)
610 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
611 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
612 unsigned int n_elt = shdr->sh_size / 4;
614 /* Look through this group's sections to see if current
615 section is a member. */
617 if ((++idx)->shdr == hdr)
621 /* We are a member of this group. Go looking through
622 other members to see if any others are linked via
624 idx = (Elf_Internal_Group *) shdr->contents;
625 n_elt = shdr->sh_size / 4;
627 if ((s = (++idx)->shdr->bfd_section) != NULL
628 && elf_next_in_group (s) != NULL)
632 /* Snarf the group name from other member, and
633 insert current section in circular list. */
634 elf_group_name (newsect) = elf_group_name (s);
635 elf_next_in_group (newsect) = elf_next_in_group (s);
636 elf_next_in_group (s) = newsect;
642 gname = group_signature (abfd, shdr);
645 elf_group_name (newsect) = gname;
647 /* Start a circular list with one element. */
648 elf_next_in_group (newsect) = newsect;
651 /* If the group section has been created, point to the
653 if (shdr->bfd_section != NULL)
654 elf_next_in_group (shdr->bfd_section) = newsect;
662 if (elf_group_name (newsect) == NULL)
664 (*_bfd_error_handler) (_("%s: no group info for section %s"),
665 bfd_archive_filename (abfd), newsect->name);
671 bfd_elf_discard_group (abfd, group)
672 bfd *abfd ATTRIBUTE_UNUSED;
675 asection *first = elf_next_in_group (group);
680 s->output_section = bfd_abs_section_ptr;
681 s = elf_next_in_group (s);
682 /* These lists are circular. */
689 /* Make a BFD section from an ELF section. We store a pointer to the
690 BFD section in the bfd_section field of the header. */
693 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
695 Elf_Internal_Shdr *hdr;
700 struct elf_backend_data *bed;
702 if (hdr->bfd_section != NULL)
704 BFD_ASSERT (strcmp (name,
705 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
709 newsect = bfd_make_section_anyway (abfd, name);
713 newsect->filepos = hdr->sh_offset;
715 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
716 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
717 || ! bfd_set_section_alignment (abfd, newsect,
718 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
721 flags = SEC_NO_FLAGS;
722 if (hdr->sh_type != SHT_NOBITS)
723 flags |= SEC_HAS_CONTENTS;
724 if (hdr->sh_type == SHT_GROUP)
725 flags |= SEC_GROUP | SEC_EXCLUDE;
726 if ((hdr->sh_flags & SHF_ALLOC) != 0)
729 if (hdr->sh_type != SHT_NOBITS)
732 if ((hdr->sh_flags & SHF_WRITE) == 0)
733 flags |= SEC_READONLY;
734 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
736 else if ((flags & SEC_LOAD) != 0)
738 if ((hdr->sh_flags & SHF_MERGE) != 0)
741 newsect->entsize = hdr->sh_entsize;
742 if ((hdr->sh_flags & SHF_STRINGS) != 0)
743 flags |= SEC_STRINGS;
745 if (hdr->sh_flags & SHF_GROUP)
746 if (!setup_group (abfd, hdr, newsect))
748 if ((hdr->sh_flags & SHF_TLS) != 0)
749 flags |= SEC_THREAD_LOCAL;
751 /* The debugging sections appear to be recognized only by name, not
754 static const char *debug_sec_names [] =
763 for (i = ARRAY_SIZE (debug_sec_names); i--;)
764 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
768 flags |= SEC_DEBUGGING;
771 /* As a GNU extension, if the name begins with .gnu.linkonce, we
772 only link a single copy of the section. This is used to support
773 g++. g++ will emit each template expansion in its own section.
774 The symbols will be defined as weak, so that multiple definitions
775 are permitted. The GNU linker extension is to actually discard
776 all but one of the sections. */
777 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
778 && elf_next_in_group (newsect) == NULL)
779 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
781 bed = get_elf_backend_data (abfd);
782 if (bed->elf_backend_section_flags)
783 if (! bed->elf_backend_section_flags (&flags, hdr))
786 if (! bfd_set_section_flags (abfd, newsect, flags))
789 if ((flags & SEC_ALLOC) != 0)
791 Elf_Internal_Phdr *phdr;
794 /* Look through the phdrs to see if we need to adjust the lma.
795 If all the p_paddr fields are zero, we ignore them, since
796 some ELF linkers produce such output. */
797 phdr = elf_tdata (abfd)->phdr;
798 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
800 if (phdr->p_paddr != 0)
803 if (i < elf_elfheader (abfd)->e_phnum)
805 phdr = elf_tdata (abfd)->phdr;
806 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
808 /* This section is part of this segment if its file
809 offset plus size lies within the segment's memory
810 span and, if the section is loaded, the extent of the
811 loaded data lies within the extent of the segment.
813 Note - we used to check the p_paddr field as well, and
814 refuse to set the LMA if it was 0. This is wrong
815 though, as a perfectly valid initialised segment can
816 have a p_paddr of zero. Some architectures, eg ARM,
817 place special significance on the address 0 and
818 executables need to be able to have a segment which
819 covers this address. */
820 if (phdr->p_type == PT_LOAD
821 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
822 && (hdr->sh_offset + hdr->sh_size
823 <= phdr->p_offset + phdr->p_memsz)
824 && ((flags & SEC_LOAD) == 0
825 || (hdr->sh_offset + hdr->sh_size
826 <= phdr->p_offset + phdr->p_filesz)))
828 if ((flags & SEC_LOAD) == 0)
829 newsect->lma = (phdr->p_paddr
830 + hdr->sh_addr - phdr->p_vaddr);
832 /* We used to use the same adjustment for SEC_LOAD
833 sections, but that doesn't work if the segment
834 is packed with code from multiple VMAs.
835 Instead we calculate the section LMA based on
836 the segment LMA. It is assumed that the
837 segment will contain sections with contiguous
838 LMAs, even if the VMAs are not. */
839 newsect->lma = (phdr->p_paddr
840 + hdr->sh_offset - phdr->p_offset);
842 /* With contiguous segments, we can't tell from file
843 offsets whether a section with zero size should
844 be placed at the end of one segment or the
845 beginning of the next. Decide based on vaddr. */
846 if (hdr->sh_addr >= phdr->p_vaddr
847 && (hdr->sh_addr + hdr->sh_size
848 <= phdr->p_vaddr + phdr->p_memsz))
855 hdr->bfd_section = newsect;
856 elf_section_data (newsect)->this_hdr = *hdr;
866 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
869 Helper functions for GDB to locate the string tables.
870 Since BFD hides string tables from callers, GDB needs to use an
871 internal hook to find them. Sun's .stabstr, in particular,
872 isn't even pointed to by the .stab section, so ordinary
873 mechanisms wouldn't work to find it, even if we had some.
876 struct elf_internal_shdr *
877 bfd_elf_find_section (abfd, name)
881 Elf_Internal_Shdr **i_shdrp;
886 i_shdrp = elf_elfsections (abfd);
889 shstrtab = bfd_elf_get_str_section (abfd,
890 elf_elfheader (abfd)->e_shstrndx);
891 if (shstrtab != NULL)
893 max = elf_numsections (abfd);
894 for (i = 1; i < max; i++)
895 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
902 const char *const bfd_elf_section_type_names[] = {
903 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
904 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
905 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
908 /* ELF relocs are against symbols. If we are producing relocateable
909 output, and the reloc is against an external symbol, and nothing
910 has given us any additional addend, the resulting reloc will also
911 be against the same symbol. In such a case, we don't want to
912 change anything about the way the reloc is handled, since it will
913 all be done at final link time. Rather than put special case code
914 into bfd_perform_relocation, all the reloc types use this howto
915 function. It just short circuits the reloc if producing
916 relocateable output against an external symbol. */
918 bfd_reloc_status_type
919 bfd_elf_generic_reloc (abfd,
926 bfd *abfd ATTRIBUTE_UNUSED;
927 arelent *reloc_entry;
929 PTR data ATTRIBUTE_UNUSED;
930 asection *input_section;
932 char **error_message ATTRIBUTE_UNUSED;
934 if (output_bfd != (bfd *) NULL
935 && (symbol->flags & BSF_SECTION_SYM) == 0
936 && (! reloc_entry->howto->partial_inplace
937 || reloc_entry->addend == 0))
939 reloc_entry->address += input_section->output_offset;
943 return bfd_reloc_continue;
946 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
949 merge_sections_remove_hook (abfd, sec)
950 bfd *abfd ATTRIBUTE_UNUSED;
953 struct bfd_elf_section_data *sec_data;
955 sec_data = elf_section_data (sec);
956 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
957 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
960 /* Finish SHF_MERGE section merging. */
963 _bfd_elf_merge_sections (abfd, info)
965 struct bfd_link_info *info;
967 if (!is_elf_hash_table (info))
969 if (elf_hash_table (info)->merge_info)
970 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
971 merge_sections_remove_hook);
976 _bfd_elf_link_just_syms (sec, info)
978 struct bfd_link_info *info;
980 sec->output_section = bfd_abs_section_ptr;
981 sec->output_offset = sec->vma;
982 if (!is_elf_hash_table (info))
985 elf_section_data (sec)->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
988 /* Copy the program header and other data from one object module to
992 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
996 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
997 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1000 BFD_ASSERT (!elf_flags_init (obfd)
1001 || (elf_elfheader (obfd)->e_flags
1002 == elf_elfheader (ibfd)->e_flags));
1004 elf_gp (obfd) = elf_gp (ibfd);
1005 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1006 elf_flags_init (obfd) = true;
1010 /* Print out the program headers. */
1013 _bfd_elf_print_private_bfd_data (abfd, farg)
1017 FILE *f = (FILE *) farg;
1018 Elf_Internal_Phdr *p;
1020 bfd_byte *dynbuf = NULL;
1022 p = elf_tdata (abfd)->phdr;
1027 fprintf (f, _("\nProgram Header:\n"));
1028 c = elf_elfheader (abfd)->e_phnum;
1029 for (i = 0; i < c; i++, p++)
1036 case PT_NULL: pt = "NULL"; break;
1037 case PT_LOAD: pt = "LOAD"; break;
1038 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1039 case PT_INTERP: pt = "INTERP"; break;
1040 case PT_NOTE: pt = "NOTE"; break;
1041 case PT_SHLIB: pt = "SHLIB"; break;
1042 case PT_PHDR: pt = "PHDR"; break;
1043 case PT_TLS: pt = "TLS"; break;
1044 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1045 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1047 fprintf (f, "%8s off 0x", pt);
1048 bfd_fprintf_vma (abfd, f, p->p_offset);
1049 fprintf (f, " vaddr 0x");
1050 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1051 fprintf (f, " paddr 0x");
1052 bfd_fprintf_vma (abfd, f, p->p_paddr);
1053 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1054 fprintf (f, " filesz 0x");
1055 bfd_fprintf_vma (abfd, f, p->p_filesz);
1056 fprintf (f, " memsz 0x");
1057 bfd_fprintf_vma (abfd, f, p->p_memsz);
1058 fprintf (f, " flags %c%c%c",
1059 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1060 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1061 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1062 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1063 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1068 s = bfd_get_section_by_name (abfd, ".dynamic");
1072 unsigned long shlink;
1073 bfd_byte *extdyn, *extdynend;
1075 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1077 fprintf (f, _("\nDynamic Section:\n"));
1079 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1082 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1086 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1089 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1091 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1092 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1095 extdynend = extdyn + s->_raw_size;
1096 for (; extdyn < extdynend; extdyn += extdynsize)
1098 Elf_Internal_Dyn dyn;
1103 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1105 if (dyn.d_tag == DT_NULL)
1112 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1116 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1117 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1118 case DT_PLTGOT: name = "PLTGOT"; break;
1119 case DT_HASH: name = "HASH"; break;
1120 case DT_STRTAB: name = "STRTAB"; break;
1121 case DT_SYMTAB: name = "SYMTAB"; break;
1122 case DT_RELA: name = "RELA"; break;
1123 case DT_RELASZ: name = "RELASZ"; break;
1124 case DT_RELAENT: name = "RELAENT"; break;
1125 case DT_STRSZ: name = "STRSZ"; break;
1126 case DT_SYMENT: name = "SYMENT"; break;
1127 case DT_INIT: name = "INIT"; break;
1128 case DT_FINI: name = "FINI"; break;
1129 case DT_SONAME: name = "SONAME"; stringp = true; break;
1130 case DT_RPATH: name = "RPATH"; stringp = true; break;
1131 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1132 case DT_REL: name = "REL"; break;
1133 case DT_RELSZ: name = "RELSZ"; break;
1134 case DT_RELENT: name = "RELENT"; break;
1135 case DT_PLTREL: name = "PLTREL"; break;
1136 case DT_DEBUG: name = "DEBUG"; break;
1137 case DT_TEXTREL: name = "TEXTREL"; break;
1138 case DT_JMPREL: name = "JMPREL"; break;
1139 case DT_BIND_NOW: name = "BIND_NOW"; break;
1140 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1141 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1142 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1143 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1144 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1145 case DT_FLAGS: name = "FLAGS"; break;
1146 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1147 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1148 case DT_CHECKSUM: name = "CHECKSUM"; break;
1149 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1150 case DT_MOVEENT: name = "MOVEENT"; break;
1151 case DT_MOVESZ: name = "MOVESZ"; break;
1152 case DT_FEATURE: name = "FEATURE"; break;
1153 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1154 case DT_SYMINSZ: name = "SYMINSZ"; break;
1155 case DT_SYMINENT: name = "SYMINENT"; break;
1156 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1157 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1158 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1159 case DT_PLTPAD: name = "PLTPAD"; break;
1160 case DT_MOVETAB: name = "MOVETAB"; break;
1161 case DT_SYMINFO: name = "SYMINFO"; break;
1162 case DT_RELACOUNT: name = "RELACOUNT"; break;
1163 case DT_RELCOUNT: name = "RELCOUNT"; break;
1164 case DT_FLAGS_1: name = "FLAGS_1"; break;
1165 case DT_VERSYM: name = "VERSYM"; break;
1166 case DT_VERDEF: name = "VERDEF"; break;
1167 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1168 case DT_VERNEED: name = "VERNEED"; break;
1169 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1170 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1171 case DT_USED: name = "USED"; break;
1172 case DT_FILTER: name = "FILTER"; stringp = true; break;
1175 fprintf (f, " %-11s ", name);
1177 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1181 unsigned int tagv = dyn.d_un.d_val;
1183 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1186 fprintf (f, "%s", string);
1195 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1196 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1198 if (! _bfd_elf_slurp_version_tables (abfd))
1202 if (elf_dynverdef (abfd) != 0)
1204 Elf_Internal_Verdef *t;
1206 fprintf (f, _("\nVersion definitions:\n"));
1207 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1209 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1210 t->vd_flags, t->vd_hash, t->vd_nodename);
1211 if (t->vd_auxptr->vda_nextptr != NULL)
1213 Elf_Internal_Verdaux *a;
1216 for (a = t->vd_auxptr->vda_nextptr;
1219 fprintf (f, "%s ", a->vda_nodename);
1225 if (elf_dynverref (abfd) != 0)
1227 Elf_Internal_Verneed *t;
1229 fprintf (f, _("\nVersion References:\n"));
1230 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1232 Elf_Internal_Vernaux *a;
1234 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1235 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1236 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1237 a->vna_flags, a->vna_other, a->vna_nodename);
1249 /* Display ELF-specific fields of a symbol. */
1252 bfd_elf_print_symbol (abfd, filep, symbol, how)
1256 bfd_print_symbol_type how;
1258 FILE *file = (FILE *) filep;
1261 case bfd_print_symbol_name:
1262 fprintf (file, "%s", symbol->name);
1264 case bfd_print_symbol_more:
1265 fprintf (file, "elf ");
1266 bfd_fprintf_vma (abfd, file, symbol->value);
1267 fprintf (file, " %lx", (long) symbol->flags);
1269 case bfd_print_symbol_all:
1271 const char *section_name;
1272 const char *name = NULL;
1273 struct elf_backend_data *bed;
1274 unsigned char st_other;
1277 section_name = symbol->section ? symbol->section->name : "(*none*)";
1279 bed = get_elf_backend_data (abfd);
1280 if (bed->elf_backend_print_symbol_all)
1281 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1285 name = symbol->name;
1286 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1289 fprintf (file, " %s\t", section_name);
1290 /* Print the "other" value for a symbol. For common symbols,
1291 we've already printed the size; now print the alignment.
1292 For other symbols, we have no specified alignment, and
1293 we've printed the address; now print the size. */
1294 if (bfd_is_com_section (symbol->section))
1295 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1297 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1298 bfd_fprintf_vma (abfd, file, val);
1300 /* If we have version information, print it. */
1301 if (elf_tdata (abfd)->dynversym_section != 0
1302 && (elf_tdata (abfd)->dynverdef_section != 0
1303 || elf_tdata (abfd)->dynverref_section != 0))
1305 unsigned int vernum;
1306 const char *version_string;
1308 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1311 version_string = "";
1312 else if (vernum == 1)
1313 version_string = "Base";
1314 else if (vernum <= elf_tdata (abfd)->cverdefs)
1316 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1319 Elf_Internal_Verneed *t;
1321 version_string = "";
1322 for (t = elf_tdata (abfd)->verref;
1326 Elf_Internal_Vernaux *a;
1328 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1330 if (a->vna_other == vernum)
1332 version_string = a->vna_nodename;
1339 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1340 fprintf (file, " %-11s", version_string);
1345 fprintf (file, " (%s)", version_string);
1346 for (i = 10 - strlen (version_string); i > 0; --i)
1351 /* If the st_other field is not zero, print it. */
1352 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1357 case STV_INTERNAL: fprintf (file, " .internal"); break;
1358 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1359 case STV_PROTECTED: fprintf (file, " .protected"); break;
1361 /* Some other non-defined flags are also present, so print
1363 fprintf (file, " 0x%02x", (unsigned int) st_other);
1366 fprintf (file, " %s", name);
1372 /* Create an entry in an ELF linker hash table. */
1374 struct bfd_hash_entry *
1375 _bfd_elf_link_hash_newfunc (entry, table, string)
1376 struct bfd_hash_entry *entry;
1377 struct bfd_hash_table *table;
1380 /* Allocate the structure if it has not already been allocated by a
1384 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1389 /* Call the allocation method of the superclass. */
1390 entry = _bfd_link_hash_newfunc (entry, table, string);
1393 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1394 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1396 /* Set local fields. */
1400 ret->dynstr_index = 0;
1401 ret->weakdef = NULL;
1402 ret->got.refcount = htab->init_refcount;
1403 ret->plt.refcount = htab->init_refcount;
1404 ret->linker_section_pointer = NULL;
1405 ret->verinfo.verdef = NULL;
1406 ret->vtable_entries_used = NULL;
1407 ret->vtable_entries_size = 0;
1408 ret->vtable_parent = NULL;
1409 ret->type = STT_NOTYPE;
1411 /* Assume that we have been called by a non-ELF symbol reader.
1412 This flag is then reset by the code which reads an ELF input
1413 file. This ensures that a symbol created by a non-ELF symbol
1414 reader will have the flag set correctly. */
1415 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1421 /* Copy data from an indirect symbol to its direct symbol, hiding the
1422 old indirect symbol. Also used for copying flags to a weakdef. */
1425 _bfd_elf_link_hash_copy_indirect (bed, dir, ind)
1426 struct elf_backend_data *bed;
1427 struct elf_link_hash_entry *dir, *ind;
1430 bfd_signed_vma lowest_valid = bed->can_refcount;
1432 /* Copy down any references that we may have already seen to the
1433 symbol which just became indirect. */
1435 dir->elf_link_hash_flags |=
1436 (ind->elf_link_hash_flags
1437 & (ELF_LINK_HASH_REF_DYNAMIC
1438 | ELF_LINK_HASH_REF_REGULAR
1439 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1440 | ELF_LINK_NON_GOT_REF));
1442 if (ind->root.type != bfd_link_hash_indirect)
1445 /* Copy over the global and procedure linkage table refcount entries.
1446 These may have been already set up by a check_relocs routine. */
1447 tmp = dir->got.refcount;
1448 if (tmp < lowest_valid)
1450 dir->got.refcount = ind->got.refcount;
1451 ind->got.refcount = tmp;
1454 BFD_ASSERT (ind->got.refcount < lowest_valid);
1456 tmp = dir->plt.refcount;
1457 if (tmp < lowest_valid)
1459 dir->plt.refcount = ind->plt.refcount;
1460 ind->plt.refcount = tmp;
1463 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1465 if (dir->dynindx == -1)
1467 dir->dynindx = ind->dynindx;
1468 dir->dynstr_index = ind->dynstr_index;
1470 ind->dynstr_index = 0;
1473 BFD_ASSERT (ind->dynindx == -1);
1477 _bfd_elf_link_hash_hide_symbol (info, h, force_local)
1478 struct bfd_link_info *info;
1479 struct elf_link_hash_entry *h;
1480 boolean force_local;
1482 h->plt.offset = (bfd_vma) -1;
1483 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1486 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1487 if (h->dynindx != -1)
1490 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1496 /* Initialize an ELF linker hash table. */
1499 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1500 struct elf_link_hash_table *table;
1502 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1503 struct bfd_hash_table *,
1508 table->dynamic_sections_created = false;
1509 table->dynobj = NULL;
1510 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
1511 /* The first dynamic symbol is a dummy. */
1512 table->dynsymcount = 1;
1513 table->dynstr = NULL;
1514 table->bucketcount = 0;
1515 table->needed = NULL;
1516 table->runpath = NULL;
1517 table->loaded = NULL;
1519 table->stab_info = NULL;
1520 table->merge_info = NULL;
1521 table->dynlocal = NULL;
1522 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1523 table->root.type = bfd_link_elf_hash_table;
1528 /* Create an ELF linker hash table. */
1530 struct bfd_link_hash_table *
1531 _bfd_elf_link_hash_table_create (abfd)
1534 struct elf_link_hash_table *ret;
1535 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1537 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
1538 if (ret == (struct elf_link_hash_table *) NULL)
1541 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1550 /* This is a hook for the ELF emulation code in the generic linker to
1551 tell the backend linker what file name to use for the DT_NEEDED
1552 entry for a dynamic object. The generic linker passes name as an
1553 empty string to indicate that no DT_NEEDED entry should be made. */
1556 bfd_elf_set_dt_needed_name (abfd, name)
1560 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1561 && bfd_get_format (abfd) == bfd_object)
1562 elf_dt_name (abfd) = name;
1566 bfd_elf_set_dt_needed_soname (abfd, name)
1570 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1571 && bfd_get_format (abfd) == bfd_object)
1572 elf_dt_soname (abfd) = name;
1575 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1576 the linker ELF emulation code. */
1578 struct bfd_link_needed_list *
1579 bfd_elf_get_needed_list (abfd, info)
1580 bfd *abfd ATTRIBUTE_UNUSED;
1581 struct bfd_link_info *info;
1583 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1585 return elf_hash_table (info)->needed;
1588 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1589 hook for the linker ELF emulation code. */
1591 struct bfd_link_needed_list *
1592 bfd_elf_get_runpath_list (abfd, info)
1593 bfd *abfd ATTRIBUTE_UNUSED;
1594 struct bfd_link_info *info;
1596 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1598 return elf_hash_table (info)->runpath;
1601 /* Get the name actually used for a dynamic object for a link. This
1602 is the SONAME entry if there is one. Otherwise, it is the string
1603 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1606 bfd_elf_get_dt_soname (abfd)
1609 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1610 && bfd_get_format (abfd) == bfd_object)
1611 return elf_dt_name (abfd);
1615 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1616 the ELF linker emulation code. */
1619 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1621 struct bfd_link_needed_list **pneeded;
1624 bfd_byte *dynbuf = NULL;
1626 unsigned long shlink;
1627 bfd_byte *extdyn, *extdynend;
1629 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1633 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1634 || bfd_get_format (abfd) != bfd_object)
1637 s = bfd_get_section_by_name (abfd, ".dynamic");
1638 if (s == NULL || s->_raw_size == 0)
1641 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1645 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1649 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1653 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1655 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1656 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1659 extdynend = extdyn + s->_raw_size;
1660 for (; extdyn < extdynend; extdyn += extdynsize)
1662 Elf_Internal_Dyn dyn;
1664 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1666 if (dyn.d_tag == DT_NULL)
1669 if (dyn.d_tag == DT_NEEDED)
1672 struct bfd_link_needed_list *l;
1673 unsigned int tagv = dyn.d_un.d_val;
1676 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1681 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1702 /* Allocate an ELF string table--force the first byte to be zero. */
1704 struct bfd_strtab_hash *
1705 _bfd_elf_stringtab_init ()
1707 struct bfd_strtab_hash *ret;
1709 ret = _bfd_stringtab_init ();
1714 loc = _bfd_stringtab_add (ret, "", true, false);
1715 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1716 if (loc == (bfd_size_type) -1)
1718 _bfd_stringtab_free (ret);
1725 /* ELF .o/exec file reading */
1727 /* Create a new bfd section from an ELF section header. */
1730 bfd_section_from_shdr (abfd, shindex)
1732 unsigned int shindex;
1734 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1735 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1736 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1739 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1741 switch (hdr->sh_type)
1744 /* Inactive section. Throw it away. */
1747 case SHT_PROGBITS: /* Normal section with contents. */
1748 case SHT_NOBITS: /* .bss section. */
1749 case SHT_HASH: /* .hash section. */
1750 case SHT_NOTE: /* .note section. */
1751 case SHT_INIT_ARRAY: /* .init_array section. */
1752 case SHT_FINI_ARRAY: /* .fini_array section. */
1753 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1754 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1756 case SHT_DYNAMIC: /* Dynamic linking information. */
1757 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1759 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1761 Elf_Internal_Shdr *dynsymhdr;
1763 /* The shared libraries distributed with hpux11 have a bogus
1764 sh_link field for the ".dynamic" section. Find the
1765 string table for the ".dynsym" section instead. */
1766 if (elf_dynsymtab (abfd) != 0)
1768 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1769 hdr->sh_link = dynsymhdr->sh_link;
1773 unsigned int i, num_sec;
1775 num_sec = elf_numsections (abfd);
1776 for (i = 1; i < num_sec; i++)
1778 dynsymhdr = elf_elfsections (abfd)[i];
1779 if (dynsymhdr->sh_type == SHT_DYNSYM)
1781 hdr->sh_link = dynsymhdr->sh_link;
1789 case SHT_SYMTAB: /* A symbol table */
1790 if (elf_onesymtab (abfd) == shindex)
1793 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1794 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1795 elf_onesymtab (abfd) = shindex;
1796 elf_tdata (abfd)->symtab_hdr = *hdr;
1797 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1798 abfd->flags |= HAS_SYMS;
1800 /* Sometimes a shared object will map in the symbol table. If
1801 SHF_ALLOC is set, and this is a shared object, then we also
1802 treat this section as a BFD section. We can not base the
1803 decision purely on SHF_ALLOC, because that flag is sometimes
1804 set in a relocateable object file, which would confuse the
1806 if ((hdr->sh_flags & SHF_ALLOC) != 0
1807 && (abfd->flags & DYNAMIC) != 0
1808 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1813 case SHT_DYNSYM: /* A dynamic symbol table */
1814 if (elf_dynsymtab (abfd) == shindex)
1817 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1818 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1819 elf_dynsymtab (abfd) = shindex;
1820 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1821 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1822 abfd->flags |= HAS_SYMS;
1824 /* Besides being a symbol table, we also treat this as a regular
1825 section, so that objcopy can handle it. */
1826 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1828 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1829 if (elf_symtab_shndx (abfd) == shindex)
1832 /* Get the associated symbol table. */
1833 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1834 || hdr->sh_link != elf_onesymtab (abfd))
1837 elf_symtab_shndx (abfd) = shindex;
1838 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1839 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1842 case SHT_STRTAB: /* A string table */
1843 if (hdr->bfd_section != NULL)
1845 if (ehdr->e_shstrndx == shindex)
1847 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1848 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1852 unsigned int i, num_sec;
1854 num_sec = elf_numsections (abfd);
1855 for (i = 1; i < num_sec; i++)
1857 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1858 if (hdr2->sh_link == shindex)
1860 if (! bfd_section_from_shdr (abfd, i))
1862 if (elf_onesymtab (abfd) == i)
1864 elf_tdata (abfd)->strtab_hdr = *hdr;
1865 elf_elfsections (abfd)[shindex] =
1866 &elf_tdata (abfd)->strtab_hdr;
1869 if (elf_dynsymtab (abfd) == i)
1871 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1872 elf_elfsections (abfd)[shindex] = hdr =
1873 &elf_tdata (abfd)->dynstrtab_hdr;
1874 /* We also treat this as a regular section, so
1875 that objcopy can handle it. */
1878 #if 0 /* Not handling other string tables specially right now. */
1879 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1880 /* We have a strtab for some random other section. */
1881 newsect = (asection *) hdr2->bfd_section;
1884 hdr->bfd_section = newsect;
1885 hdr2 = &elf_section_data (newsect)->str_hdr;
1887 elf_elfsections (abfd)[shindex] = hdr2;
1893 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1897 /* *These* do a lot of work -- but build no sections! */
1899 asection *target_sect;
1900 Elf_Internal_Shdr *hdr2;
1901 unsigned int num_sec = elf_numsections (abfd);
1903 /* Check for a bogus link to avoid crashing. */
1904 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1905 || hdr->sh_link >= num_sec)
1907 ((*_bfd_error_handler)
1908 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1909 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1910 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1913 /* For some incomprehensible reason Oracle distributes
1914 libraries for Solaris in which some of the objects have
1915 bogus sh_link fields. It would be nice if we could just
1916 reject them, but, unfortunately, some people need to use
1917 them. We scan through the section headers; if we find only
1918 one suitable symbol table, we clobber the sh_link to point
1919 to it. I hope this doesn't break anything. */
1920 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1921 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1927 for (scan = 1; scan < num_sec; scan++)
1929 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1930 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1941 hdr->sh_link = found;
1944 /* Get the symbol table. */
1945 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1946 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1949 /* If this reloc section does not use the main symbol table we
1950 don't treat it as a reloc section. BFD can't adequately
1951 represent such a section, so at least for now, we don't
1952 try. We just present it as a normal section. We also
1953 can't use it as a reloc section if it points to the null
1955 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1956 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1958 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1960 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1961 if (target_sect == NULL)
1964 if ((target_sect->flags & SEC_RELOC) == 0
1965 || target_sect->reloc_count == 0)
1966 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1970 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1971 amt = sizeof (*hdr2);
1972 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1973 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1976 elf_elfsections (abfd)[shindex] = hdr2;
1977 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1978 target_sect->flags |= SEC_RELOC;
1979 target_sect->relocation = NULL;
1980 target_sect->rel_filepos = hdr->sh_offset;
1981 /* In the section to which the relocations apply, mark whether
1982 its relocations are of the REL or RELA variety. */
1983 if (hdr->sh_size != 0)
1984 elf_section_data (target_sect)->use_rela_p
1985 = (hdr->sh_type == SHT_RELA);
1986 abfd->flags |= HAS_RELOC;
1991 case SHT_GNU_verdef:
1992 elf_dynverdef (abfd) = shindex;
1993 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1994 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1997 case SHT_GNU_versym:
1998 elf_dynversym (abfd) = shindex;
1999 elf_tdata (abfd)->dynversym_hdr = *hdr;
2000 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2003 case SHT_GNU_verneed:
2004 elf_dynverref (abfd) = shindex;
2005 elf_tdata (abfd)->dynverref_hdr = *hdr;
2006 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2013 /* We need a BFD section for objcopy and relocatable linking,
2014 and it's handy to have the signature available as the section
2016 name = group_signature (abfd, hdr);
2019 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2021 if (hdr->contents != NULL)
2023 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2024 unsigned int n_elt = hdr->sh_size / 4;
2027 if (idx->flags & GRP_COMDAT)
2028 hdr->bfd_section->flags
2029 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2031 while (--n_elt != 0)
2032 if ((s = (++idx)->shdr->bfd_section) != NULL
2033 && elf_next_in_group (s) != NULL)
2035 elf_next_in_group (hdr->bfd_section) = s;
2042 /* Check for any processor-specific section types. */
2044 if (bed->elf_backend_section_from_shdr)
2045 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2053 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2054 Return SEC for sections that have no elf section, and NULL on error. */
2057 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
2059 struct sym_sec_cache *cache;
2061 unsigned long r_symndx;
2063 Elf_Internal_Shdr *symtab_hdr;
2064 unsigned char esym[sizeof (Elf64_External_Sym)];
2065 Elf_External_Sym_Shndx eshndx;
2066 Elf_Internal_Sym isym;
2067 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2069 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2070 return cache->sec[ent];
2072 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2073 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2074 &isym, esym, &eshndx) == NULL)
2077 if (cache->abfd != abfd)
2079 memset (cache->indx, -1, sizeof (cache->indx));
2082 cache->indx[ent] = r_symndx;
2083 cache->sec[ent] = sec;
2084 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
2087 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2089 cache->sec[ent] = s;
2091 return cache->sec[ent];
2094 /* Given an ELF section number, retrieve the corresponding BFD
2098 bfd_section_from_elf_index (abfd, index)
2102 if (index >= elf_numsections (abfd))
2104 return elf_elfsections (abfd)[index]->bfd_section;
2108 _bfd_elf_new_section_hook (abfd, sec)
2112 struct bfd_elf_section_data *sdata;
2113 bfd_size_type amt = sizeof (*sdata);
2115 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
2118 sec->used_by_bfd = (PTR) sdata;
2120 /* Indicate whether or not this section should use RELA relocations. */
2122 = get_elf_backend_data (abfd)->default_use_rela_p;
2127 /* Create a new bfd section from an ELF program header.
2129 Since program segments have no names, we generate a synthetic name
2130 of the form segment<NUM>, where NUM is generally the index in the
2131 program header table. For segments that are split (see below) we
2132 generate the names segment<NUM>a and segment<NUM>b.
2134 Note that some program segments may have a file size that is different than
2135 (less than) the memory size. All this means is that at execution the
2136 system must allocate the amount of memory specified by the memory size,
2137 but only initialize it with the first "file size" bytes read from the
2138 file. This would occur for example, with program segments consisting
2139 of combined data+bss.
2141 To handle the above situation, this routine generates TWO bfd sections
2142 for the single program segment. The first has the length specified by
2143 the file size of the segment, and the second has the length specified
2144 by the difference between the two sizes. In effect, the segment is split
2145 into it's initialized and uninitialized parts.
2150 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
2152 Elf_Internal_Phdr *hdr;
2154 const char *typename;
2162 split = ((hdr->p_memsz > 0)
2163 && (hdr->p_filesz > 0)
2164 && (hdr->p_memsz > hdr->p_filesz));
2165 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2166 len = strlen (namebuf) + 1;
2167 name = bfd_alloc (abfd, (bfd_size_type) len);
2170 memcpy (name, namebuf, len);
2171 newsect = bfd_make_section (abfd, name);
2172 if (newsect == NULL)
2174 newsect->vma = hdr->p_vaddr;
2175 newsect->lma = hdr->p_paddr;
2176 newsect->_raw_size = hdr->p_filesz;
2177 newsect->filepos = hdr->p_offset;
2178 newsect->flags |= SEC_HAS_CONTENTS;
2179 if (hdr->p_type == PT_LOAD)
2181 newsect->flags |= SEC_ALLOC;
2182 newsect->flags |= SEC_LOAD;
2183 if (hdr->p_flags & PF_X)
2185 /* FIXME: all we known is that it has execute PERMISSION,
2187 newsect->flags |= SEC_CODE;
2190 if (!(hdr->p_flags & PF_W))
2192 newsect->flags |= SEC_READONLY;
2197 sprintf (namebuf, "%s%db", typename, index);
2198 len = strlen (namebuf) + 1;
2199 name = bfd_alloc (abfd, (bfd_size_type) len);
2202 memcpy (name, namebuf, len);
2203 newsect = bfd_make_section (abfd, name);
2204 if (newsect == NULL)
2206 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2207 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2208 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2209 if (hdr->p_type == PT_LOAD)
2211 newsect->flags |= SEC_ALLOC;
2212 if (hdr->p_flags & PF_X)
2213 newsect->flags |= SEC_CODE;
2215 if (!(hdr->p_flags & PF_W))
2216 newsect->flags |= SEC_READONLY;
2223 bfd_section_from_phdr (abfd, hdr, index)
2225 Elf_Internal_Phdr *hdr;
2228 struct elf_backend_data *bed;
2230 switch (hdr->p_type)
2233 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2236 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2239 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2242 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2245 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2247 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2252 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2255 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2258 /* Check for any processor-specific program segment types.
2259 If no handler for them, default to making "segment" sections. */
2260 bed = get_elf_backend_data (abfd);
2261 if (bed->elf_backend_section_from_phdr)
2262 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2264 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2268 /* Initialize REL_HDR, the section-header for new section, containing
2269 relocations against ASECT. If USE_RELA_P is true, we use RELA
2270 relocations; otherwise, we use REL relocations. */
2273 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2275 Elf_Internal_Shdr *rel_hdr;
2280 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2281 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2283 name = bfd_alloc (abfd, amt);
2286 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2288 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2290 if (rel_hdr->sh_name == (unsigned int) -1)
2292 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2293 rel_hdr->sh_entsize = (use_rela_p
2294 ? bed->s->sizeof_rela
2295 : bed->s->sizeof_rel);
2296 rel_hdr->sh_addralign = bed->s->file_align;
2297 rel_hdr->sh_flags = 0;
2298 rel_hdr->sh_addr = 0;
2299 rel_hdr->sh_size = 0;
2300 rel_hdr->sh_offset = 0;
2305 /* Set up an ELF internal section header for a section. */
2308 elf_fake_sections (abfd, asect, failedptrarg)
2313 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2314 boolean *failedptr = (boolean *) failedptrarg;
2315 Elf_Internal_Shdr *this_hdr;
2319 /* We already failed; just get out of the bfd_map_over_sections
2324 this_hdr = &elf_section_data (asect)->this_hdr;
2326 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2327 asect->name, false);
2328 if (this_hdr->sh_name == (unsigned long) -1)
2334 this_hdr->sh_flags = 0;
2336 if ((asect->flags & SEC_ALLOC) != 0
2337 || asect->user_set_vma)
2338 this_hdr->sh_addr = asect->vma;
2340 this_hdr->sh_addr = 0;
2342 this_hdr->sh_offset = 0;
2343 this_hdr->sh_size = asect->_raw_size;
2344 this_hdr->sh_link = 0;
2345 this_hdr->sh_addralign = 1 << asect->alignment_power;
2346 /* The sh_entsize and sh_info fields may have been set already by
2347 copy_private_section_data. */
2349 this_hdr->bfd_section = asect;
2350 this_hdr->contents = NULL;
2352 /* FIXME: This should not be based on section names. */
2353 if (strcmp (asect->name, ".dynstr") == 0)
2354 this_hdr->sh_type = SHT_STRTAB;
2355 else if (strcmp (asect->name, ".hash") == 0)
2357 this_hdr->sh_type = SHT_HASH;
2358 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2360 else if (strcmp (asect->name, ".dynsym") == 0)
2362 this_hdr->sh_type = SHT_DYNSYM;
2363 this_hdr->sh_entsize = bed->s->sizeof_sym;
2365 else if (strcmp (asect->name, ".dynamic") == 0)
2367 this_hdr->sh_type = SHT_DYNAMIC;
2368 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2370 else if (strncmp (asect->name, ".rela", 5) == 0
2371 && get_elf_backend_data (abfd)->may_use_rela_p)
2373 this_hdr->sh_type = SHT_RELA;
2374 this_hdr->sh_entsize = bed->s->sizeof_rela;
2376 else if (strncmp (asect->name, ".rel", 4) == 0
2377 && get_elf_backend_data (abfd)->may_use_rel_p)
2379 this_hdr->sh_type = SHT_REL;
2380 this_hdr->sh_entsize = bed->s->sizeof_rel;
2382 else if (strcmp (asect->name, ".init_array") == 0)
2383 this_hdr->sh_type = SHT_INIT_ARRAY;
2384 else if (strcmp (asect->name, ".fini_array") == 0)
2385 this_hdr->sh_type = SHT_FINI_ARRAY;
2386 else if (strcmp (asect->name, ".preinit_array") == 0)
2387 this_hdr->sh_type = SHT_PREINIT_ARRAY;
2388 else if (strncmp (asect->name, ".note", 5) == 0)
2389 this_hdr->sh_type = SHT_NOTE;
2390 else if (strncmp (asect->name, ".stab", 5) == 0
2391 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2392 this_hdr->sh_type = SHT_STRTAB;
2393 else if (strcmp (asect->name, ".gnu.version") == 0)
2395 this_hdr->sh_type = SHT_GNU_versym;
2396 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2398 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2400 this_hdr->sh_type = SHT_GNU_verdef;
2401 this_hdr->sh_entsize = 0;
2402 /* objcopy or strip will copy over sh_info, but may not set
2403 cverdefs. The linker will set cverdefs, but sh_info will be
2405 if (this_hdr->sh_info == 0)
2406 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2408 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2409 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2411 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2413 this_hdr->sh_type = SHT_GNU_verneed;
2414 this_hdr->sh_entsize = 0;
2415 /* objcopy or strip will copy over sh_info, but may not set
2416 cverrefs. The linker will set cverrefs, but sh_info will be
2418 if (this_hdr->sh_info == 0)
2419 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2421 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2422 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2424 else if ((asect->flags & SEC_GROUP) != 0)
2426 this_hdr->sh_type = SHT_GROUP;
2427 this_hdr->sh_entsize = 4;
2429 else if ((asect->flags & SEC_ALLOC) != 0
2430 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2431 || (asect->flags & SEC_NEVER_LOAD) != 0))
2432 this_hdr->sh_type = SHT_NOBITS;
2434 this_hdr->sh_type = SHT_PROGBITS;
2436 if ((asect->flags & SEC_ALLOC) != 0)
2437 this_hdr->sh_flags |= SHF_ALLOC;
2438 if ((asect->flags & SEC_READONLY) == 0)
2439 this_hdr->sh_flags |= SHF_WRITE;
2440 if ((asect->flags & SEC_CODE) != 0)
2441 this_hdr->sh_flags |= SHF_EXECINSTR;
2442 if ((asect->flags & SEC_MERGE) != 0)
2444 this_hdr->sh_flags |= SHF_MERGE;
2445 this_hdr->sh_entsize = asect->entsize;
2446 if ((asect->flags & SEC_STRINGS) != 0)
2447 this_hdr->sh_flags |= SHF_STRINGS;
2449 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2450 this_hdr->sh_flags |= SHF_GROUP;
2451 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2453 this_hdr->sh_flags |= SHF_TLS;
2454 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2456 struct bfd_link_order *o;
2458 this_hdr->sh_size = 0;
2459 for (o = asect->link_order_head; o != NULL; o = o->next)
2460 if (this_hdr->sh_size < o->offset + o->size)
2461 this_hdr->sh_size = o->offset + o->size;
2462 if (this_hdr->sh_size)
2463 this_hdr->sh_type = SHT_NOBITS;
2467 /* Check for processor-specific section types. */
2468 if (bed->elf_backend_fake_sections
2469 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2472 /* If the section has relocs, set up a section header for the
2473 SHT_REL[A] section. If two relocation sections are required for
2474 this section, it is up to the processor-specific back-end to
2475 create the other. */
2476 if ((asect->flags & SEC_RELOC) != 0
2477 && !_bfd_elf_init_reloc_shdr (abfd,
2478 &elf_section_data (asect)->rel_hdr,
2480 elf_section_data (asect)->use_rela_p))
2484 /* Fill in the contents of a SHT_GROUP section. */
2487 bfd_elf_set_group_contents (abfd, sec, failedptrarg)
2492 boolean *failedptr = (boolean *) failedptrarg;
2493 unsigned long symindx;
2494 asection *elt, *first;
2496 struct bfd_link_order *l;
2499 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2504 if (elf_group_id (sec) != NULL)
2505 symindx = elf_group_id (sec)->udata.i;
2509 /* If called from the assembler, swap_out_syms will have set up
2510 elf_section_syms; If called for "ld -r", use target_index. */
2511 if (elf_section_syms (abfd) != NULL)
2512 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2514 symindx = sec->target_index;
2516 elf_section_data (sec)->this_hdr.sh_info = symindx;
2518 /* The contents won't be allocated for "ld -r" or objcopy. */
2520 if (sec->contents == NULL)
2523 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2525 /* Arrange for the section to be written out. */
2526 elf_section_data (sec)->this_hdr.contents = sec->contents;
2527 if (sec->contents == NULL)
2534 loc = sec->contents + sec->_raw_size;
2536 /* Get the pointer to the first section in the group that gas
2537 squirreled away here. objcopy arranges for this to be set to the
2538 start of the input section group. */
2539 first = elt = elf_next_in_group (sec);
2541 /* First element is a flag word. Rest of section is elf section
2542 indices for all the sections of the group. Write them backwards
2543 just to keep the group in the same order as given in .section
2544 directives, not that it matters. */
2553 s = s->output_section;
2556 idx = elf_section_data (s)->this_idx;
2557 H_PUT_32 (abfd, idx, loc);
2558 elt = elf_next_in_group (elt);
2563 /* If this is a relocatable link, then the above did nothing because
2564 SEC is the output section. Look through the input sections
2566 for (l = sec->link_order_head; l != NULL; l = l->next)
2567 if (l->type == bfd_indirect_link_order
2568 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2573 elf_section_data (elt->output_section)->this_idx, loc);
2574 elt = elf_next_in_group (elt);
2575 /* During a relocatable link, the lists are circular. */
2577 while (elt != elf_next_in_group (l->u.indirect.section));
2579 /* With ld -r, merging SHT_GROUP sections results in wasted space
2580 due to allowing for the flag word on each input. We may well
2581 duplicate entries too. */
2582 while ((loc -= 4) > sec->contents)
2583 H_PUT_32 (abfd, 0, loc);
2585 if (loc != sec->contents)
2588 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2591 /* Assign all ELF section numbers. The dummy first section is handled here
2592 too. The link/info pointers for the standard section types are filled
2593 in here too, while we're at it. */
2596 assign_section_numbers (abfd)
2599 struct elf_obj_tdata *t = elf_tdata (abfd);
2601 unsigned int section_number, secn;
2602 Elf_Internal_Shdr **i_shdrp;
2607 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2609 for (sec = abfd->sections; sec; sec = sec->next)
2611 struct bfd_elf_section_data *d = elf_section_data (sec);
2613 if (section_number == SHN_LORESERVE)
2614 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2615 d->this_idx = section_number++;
2616 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2617 if ((sec->flags & SEC_RELOC) == 0)
2621 if (section_number == SHN_LORESERVE)
2622 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2623 d->rel_idx = section_number++;
2624 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2629 if (section_number == SHN_LORESERVE)
2630 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2631 d->rel_idx2 = section_number++;
2632 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2638 if (section_number == SHN_LORESERVE)
2639 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2640 t->shstrtab_section = section_number++;
2641 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2642 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2644 if (bfd_get_symcount (abfd) > 0)
2646 if (section_number == SHN_LORESERVE)
2647 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2648 t->symtab_section = section_number++;
2649 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2650 if (section_number > SHN_LORESERVE - 2)
2652 if (section_number == SHN_LORESERVE)
2653 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2654 t->symtab_shndx_section = section_number++;
2655 t->symtab_shndx_hdr.sh_name
2656 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2657 ".symtab_shndx", false);
2658 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2661 if (section_number == SHN_LORESERVE)
2662 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2663 t->strtab_section = section_number++;
2664 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2667 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2668 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2670 elf_numsections (abfd) = section_number;
2671 elf_elfheader (abfd)->e_shnum = section_number;
2672 if (section_number > SHN_LORESERVE)
2673 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2675 /* Set up the list of section header pointers, in agreement with the
2677 amt = section_number * sizeof (Elf_Internal_Shdr *);
2678 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
2679 if (i_shdrp == NULL)
2682 amt = sizeof (Elf_Internal_Shdr);
2683 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2684 if (i_shdrp[0] == NULL)
2686 bfd_release (abfd, i_shdrp);
2689 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2691 elf_elfsections (abfd) = i_shdrp;
2693 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2694 if (bfd_get_symcount (abfd) > 0)
2696 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2697 if (elf_numsections (abfd) > SHN_LORESERVE)
2699 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2700 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2702 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2703 t->symtab_hdr.sh_link = t->strtab_section;
2705 for (sec = abfd->sections; sec; sec = sec->next)
2707 struct bfd_elf_section_data *d = elf_section_data (sec);
2711 i_shdrp[d->this_idx] = &d->this_hdr;
2712 if (d->rel_idx != 0)
2713 i_shdrp[d->rel_idx] = &d->rel_hdr;
2714 if (d->rel_idx2 != 0)
2715 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2717 /* Fill in the sh_link and sh_info fields while we're at it. */
2719 /* sh_link of a reloc section is the section index of the symbol
2720 table. sh_info is the section index of the section to which
2721 the relocation entries apply. */
2722 if (d->rel_idx != 0)
2724 d->rel_hdr.sh_link = t->symtab_section;
2725 d->rel_hdr.sh_info = d->this_idx;
2727 if (d->rel_idx2 != 0)
2729 d->rel_hdr2->sh_link = t->symtab_section;
2730 d->rel_hdr2->sh_info = d->this_idx;
2733 switch (d->this_hdr.sh_type)
2737 /* A reloc section which we are treating as a normal BFD
2738 section. sh_link is the section index of the symbol
2739 table. sh_info is the section index of the section to
2740 which the relocation entries apply. We assume that an
2741 allocated reloc section uses the dynamic symbol table.
2742 FIXME: How can we be sure? */
2743 s = bfd_get_section_by_name (abfd, ".dynsym");
2745 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2747 /* We look up the section the relocs apply to by name. */
2749 if (d->this_hdr.sh_type == SHT_REL)
2753 s = bfd_get_section_by_name (abfd, name);
2755 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2759 /* We assume that a section named .stab*str is a stabs
2760 string section. We look for a section with the same name
2761 but without the trailing ``str'', and set its sh_link
2762 field to point to this section. */
2763 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2764 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2769 len = strlen (sec->name);
2770 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));
2773 memcpy (alc, sec->name, len - 3);
2774 alc[len - 3] = '\0';
2775 s = bfd_get_section_by_name (abfd, alc);
2779 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2781 /* This is a .stab section. */
2782 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2783 elf_section_data (s)->this_hdr.sh_entsize
2784 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2791 case SHT_GNU_verneed:
2792 case SHT_GNU_verdef:
2793 /* sh_link is the section header index of the string table
2794 used for the dynamic entries, or the symbol table, or the
2796 s = bfd_get_section_by_name (abfd, ".dynstr");
2798 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2802 case SHT_GNU_versym:
2803 /* sh_link is the section header index of the symbol table
2804 this hash table or version table is for. */
2805 s = bfd_get_section_by_name (abfd, ".dynsym");
2807 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2811 d->this_hdr.sh_link = t->symtab_section;
2815 for (secn = 1; secn < section_number; ++secn)
2816 if (i_shdrp[secn] == NULL)
2817 i_shdrp[secn] = i_shdrp[0];
2819 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2820 i_shdrp[secn]->sh_name);
2824 /* Map symbol from it's internal number to the external number, moving
2825 all local symbols to be at the head of the list. */
2828 sym_is_global (abfd, sym)
2832 /* If the backend has a special mapping, use it. */
2833 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2834 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2837 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2838 || bfd_is_und_section (bfd_get_section (sym))
2839 || bfd_is_com_section (bfd_get_section (sym)));
2843 elf_map_symbols (abfd)
2846 unsigned int symcount = bfd_get_symcount (abfd);
2847 asymbol **syms = bfd_get_outsymbols (abfd);
2848 asymbol **sect_syms;
2849 unsigned int num_locals = 0;
2850 unsigned int num_globals = 0;
2851 unsigned int num_locals2 = 0;
2852 unsigned int num_globals2 = 0;
2860 fprintf (stderr, "elf_map_symbols\n");
2864 for (asect = abfd->sections; asect; asect = asect->next)
2866 if (max_index < asect->index)
2867 max_index = asect->index;
2871 amt = max_index * sizeof (asymbol *);
2872 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2873 if (sect_syms == NULL)
2875 elf_section_syms (abfd) = sect_syms;
2876 elf_num_section_syms (abfd) = max_index;
2878 /* Init sect_syms entries for any section symbols we have already
2879 decided to output. */
2880 for (idx = 0; idx < symcount; idx++)
2882 asymbol *sym = syms[idx];
2884 if ((sym->flags & BSF_SECTION_SYM) != 0
2891 if (sec->owner != NULL)
2893 if (sec->owner != abfd)
2895 if (sec->output_offset != 0)
2898 sec = sec->output_section;
2900 /* Empty sections in the input files may have had a
2901 section symbol created for them. (See the comment
2902 near the end of _bfd_generic_link_output_symbols in
2903 linker.c). If the linker script discards such
2904 sections then we will reach this point. Since we know
2905 that we cannot avoid this case, we detect it and skip
2906 the abort and the assignment to the sect_syms array.
2907 To reproduce this particular case try running the
2908 linker testsuite test ld-scripts/weak.exp for an ELF
2909 port that uses the generic linker. */
2910 if (sec->owner == NULL)
2913 BFD_ASSERT (sec->owner == abfd);
2915 sect_syms[sec->index] = syms[idx];
2920 /* Classify all of the symbols. */
2921 for (idx = 0; idx < symcount; idx++)
2923 if (!sym_is_global (abfd, syms[idx]))
2929 /* We will be adding a section symbol for each BFD section. Most normal
2930 sections will already have a section symbol in outsymbols, but
2931 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2932 at least in that case. */
2933 for (asect = abfd->sections; asect; asect = asect->next)
2935 if (sect_syms[asect->index] == NULL)
2937 if (!sym_is_global (abfd, asect->symbol))
2944 /* Now sort the symbols so the local symbols are first. */
2945 amt = (num_locals + num_globals) * sizeof (asymbol *);
2946 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2948 if (new_syms == NULL)
2951 for (idx = 0; idx < symcount; idx++)
2953 asymbol *sym = syms[idx];
2956 if (!sym_is_global (abfd, sym))
2959 i = num_locals + num_globals2++;
2961 sym->udata.i = i + 1;
2963 for (asect = abfd->sections; asect; asect = asect->next)
2965 if (sect_syms[asect->index] == NULL)
2967 asymbol *sym = asect->symbol;
2970 sect_syms[asect->index] = sym;
2971 if (!sym_is_global (abfd, sym))
2974 i = num_locals + num_globals2++;
2976 sym->udata.i = i + 1;
2980 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2982 elf_num_locals (abfd) = num_locals;
2983 elf_num_globals (abfd) = num_globals;
2987 /* Align to the maximum file alignment that could be required for any
2988 ELF data structure. */
2990 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2991 static INLINE file_ptr
2992 align_file_position (off, align)
2996 return (off + align - 1) & ~(align - 1);
2999 /* Assign a file position to a section, optionally aligning to the
3000 required section alignment. */
3003 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
3004 Elf_Internal_Shdr *i_shdrp;
3012 al = i_shdrp->sh_addralign;
3014 offset = BFD_ALIGN (offset, al);
3016 i_shdrp->sh_offset = offset;
3017 if (i_shdrp->bfd_section != NULL)
3018 i_shdrp->bfd_section->filepos = offset;
3019 if (i_shdrp->sh_type != SHT_NOBITS)
3020 offset += i_shdrp->sh_size;
3024 /* Compute the file positions we are going to put the sections at, and
3025 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3026 is not NULL, this is being called by the ELF backend linker. */
3029 _bfd_elf_compute_section_file_positions (abfd, link_info)
3031 struct bfd_link_info *link_info;
3033 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3035 struct bfd_strtab_hash *strtab;
3036 Elf_Internal_Shdr *shstrtab_hdr;
3038 if (abfd->output_has_begun)
3041 /* Do any elf backend specific processing first. */
3042 if (bed->elf_backend_begin_write_processing)
3043 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3045 if (! prep_headers (abfd))
3048 /* Post process the headers if necessary. */
3049 if (bed->elf_backend_post_process_headers)
3050 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3053 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3057 if (!assign_section_numbers (abfd))
3060 /* The backend linker builds symbol table information itself. */
3061 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3063 /* Non-zero if doing a relocatable link. */
3064 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3066 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3070 if (link_info == NULL)
3072 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3077 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3078 /* sh_name was set in prep_headers. */
3079 shstrtab_hdr->sh_type = SHT_STRTAB;
3080 shstrtab_hdr->sh_flags = 0;
3081 shstrtab_hdr->sh_addr = 0;
3082 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3083 shstrtab_hdr->sh_entsize = 0;
3084 shstrtab_hdr->sh_link = 0;
3085 shstrtab_hdr->sh_info = 0;
3086 /* sh_offset is set in assign_file_positions_except_relocs. */
3087 shstrtab_hdr->sh_addralign = 1;
3089 if (!assign_file_positions_except_relocs (abfd))
3092 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3095 Elf_Internal_Shdr *hdr;
3097 off = elf_tdata (abfd)->next_file_pos;
3099 hdr = &elf_tdata (abfd)->symtab_hdr;
3100 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3102 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3103 if (hdr->sh_size != 0)
3104 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3106 hdr = &elf_tdata (abfd)->strtab_hdr;
3107 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3109 elf_tdata (abfd)->next_file_pos = off;
3111 /* Now that we know where the .strtab section goes, write it
3113 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3114 || ! _bfd_stringtab_emit (abfd, strtab))
3116 _bfd_stringtab_free (strtab);
3119 abfd->output_has_begun = true;
3124 /* Create a mapping from a set of sections to a program segment. */
3126 static INLINE struct elf_segment_map *
3127 make_mapping (abfd, sections, from, to, phdr)
3129 asection **sections;
3134 struct elf_segment_map *m;
3139 amt = sizeof (struct elf_segment_map);
3140 amt += (to - from - 1) * sizeof (asection *);
3141 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3145 m->p_type = PT_LOAD;
3146 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3147 m->sections[i - from] = *hdrpp;
3148 m->count = to - from;
3150 if (from == 0 && phdr)
3152 /* Include the headers in the first PT_LOAD segment. */
3153 m->includes_filehdr = 1;
3154 m->includes_phdrs = 1;
3160 /* Set up a mapping from BFD sections to program segments. */
3163 map_sections_to_segments (abfd)
3166 asection **sections = NULL;
3170 struct elf_segment_map *mfirst;
3171 struct elf_segment_map **pm;
3172 struct elf_segment_map *m;
3174 unsigned int phdr_index;
3175 bfd_vma maxpagesize;
3177 boolean phdr_in_segment = true;
3180 asection *first_tls = NULL;
3181 asection *dynsec, *eh_frame_hdr;
3184 if (elf_tdata (abfd)->segment_map != NULL)
3187 if (bfd_count_sections (abfd) == 0)
3190 /* Select the allocated sections, and sort them. */
3192 amt = bfd_count_sections (abfd) * sizeof (asection *);
3193 sections = (asection **) bfd_malloc (amt);
3194 if (sections == NULL)
3198 for (s = abfd->sections; s != NULL; s = s->next)
3200 if ((s->flags & SEC_ALLOC) != 0)
3206 BFD_ASSERT (i <= bfd_count_sections (abfd));
3209 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3211 /* Build the mapping. */
3216 /* If we have a .interp section, then create a PT_PHDR segment for
3217 the program headers and a PT_INTERP segment for the .interp
3219 s = bfd_get_section_by_name (abfd, ".interp");
3220 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3222 amt = sizeof (struct elf_segment_map);
3223 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3227 m->p_type = PT_PHDR;
3228 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3229 m->p_flags = PF_R | PF_X;
3230 m->p_flags_valid = 1;
3231 m->includes_phdrs = 1;
3236 amt = sizeof (struct elf_segment_map);
3237 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3241 m->p_type = PT_INTERP;
3249 /* Look through the sections. We put sections in the same program
3250 segment when the start of the second section can be placed within
3251 a few bytes of the end of the first section. */
3254 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3256 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3258 && (dynsec->flags & SEC_LOAD) == 0)
3261 /* Deal with -Ttext or something similar such that the first section
3262 is not adjacent to the program headers. This is an
3263 approximation, since at this point we don't know exactly how many
3264 program headers we will need. */
3267 bfd_size_type phdr_size;
3269 phdr_size = elf_tdata (abfd)->program_header_size;
3271 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3272 if ((abfd->flags & D_PAGED) == 0
3273 || sections[0]->lma < phdr_size
3274 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3275 phdr_in_segment = false;
3278 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3281 boolean new_segment;
3285 /* See if this section and the last one will fit in the same
3288 if (last_hdr == NULL)
3290 /* If we don't have a segment yet, then we don't need a new
3291 one (we build the last one after this loop). */
3292 new_segment = false;
3294 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3296 /* If this section has a different relation between the
3297 virtual address and the load address, then we need a new
3301 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3302 < BFD_ALIGN (hdr->lma, maxpagesize))
3304 /* If putting this section in this segment would force us to
3305 skip a page in the segment, then we need a new segment. */
3308 else if ((last_hdr->flags & SEC_LOAD) == 0
3309 && (hdr->flags & SEC_LOAD) != 0)
3311 /* We don't want to put a loadable section after a
3312 nonloadable section in the same segment. */
3315 else if ((abfd->flags & D_PAGED) == 0)
3317 /* If the file is not demand paged, which means that we
3318 don't require the sections to be correctly aligned in the
3319 file, then there is no other reason for a new segment. */
3320 new_segment = false;
3323 && (hdr->flags & SEC_READONLY) == 0
3324 && (((last_hdr->lma + last_hdr->_raw_size - 1)
3325 & ~(maxpagesize - 1))
3326 != (hdr->lma & ~(maxpagesize - 1))))
3328 /* We don't want to put a writable section in a read only
3329 segment, unless they are on the same page in memory
3330 anyhow. We already know that the last section does not
3331 bring us past the current section on the page, so the
3332 only case in which the new section is not on the same
3333 page as the previous section is when the previous section
3334 ends precisely on a page boundary. */
3339 /* Otherwise, we can use the same segment. */
3340 new_segment = false;
3345 if ((hdr->flags & SEC_READONLY) == 0)
3351 /* We need a new program segment. We must create a new program
3352 header holding all the sections from phdr_index until hdr. */
3354 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3361 if ((hdr->flags & SEC_READONLY) == 0)
3368 phdr_in_segment = false;
3371 /* Create a final PT_LOAD program segment. */
3372 if (last_hdr != NULL)
3374 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3382 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3385 amt = sizeof (struct elf_segment_map);
3386 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3390 m->p_type = PT_DYNAMIC;
3392 m->sections[0] = dynsec;
3398 /* For each loadable .note section, add a PT_NOTE segment. We don't
3399 use bfd_get_section_by_name, because if we link together
3400 nonloadable .note sections and loadable .note sections, we will
3401 generate two .note sections in the output file. FIXME: Using
3402 names for section types is bogus anyhow. */
3403 for (s = abfd->sections; s != NULL; s = s->next)
3405 if ((s->flags & SEC_LOAD) != 0
3406 && strncmp (s->name, ".note", 5) == 0)
3408 amt = sizeof (struct elf_segment_map);
3409 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3413 m->p_type = PT_NOTE;
3420 if (s->flags & SEC_THREAD_LOCAL)
3428 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3433 amt = sizeof (struct elf_segment_map);
3434 amt += (tls_count - 1) * sizeof (asection *);
3435 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3440 m->count = tls_count;
3441 /* Mandated PF_R. */
3443 m->p_flags_valid = 1;
3444 for (i = 0; i < tls_count; ++i)
3446 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3447 m->sections[i] = first_tls;
3448 first_tls = first_tls->next;
3455 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3457 eh_frame_hdr = NULL;
3458 if (elf_tdata (abfd)->eh_frame_hdr)
3459 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
3460 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3462 amt = sizeof (struct elf_segment_map);
3463 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3467 m->p_type = PT_GNU_EH_FRAME;
3469 m->sections[0] = eh_frame_hdr;
3478 elf_tdata (abfd)->segment_map = mfirst;
3482 if (sections != NULL)
3487 /* Sort sections by address. */
3490 elf_sort_sections (arg1, arg2)
3494 const asection *sec1 = *(const asection **) arg1;
3495 const asection *sec2 = *(const asection **) arg2;
3497 /* Sort by LMA first, since this is the address used to
3498 place the section into a segment. */
3499 if (sec1->lma < sec2->lma)
3501 else if (sec1->lma > sec2->lma)
3504 /* Then sort by VMA. Normally the LMA and the VMA will be
3505 the same, and this will do nothing. */
3506 if (sec1->vma < sec2->vma)
3508 else if (sec1->vma > sec2->vma)
3511 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3513 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3519 /* If the indicies are the same, do not return 0
3520 here, but continue to try the next comparison. */
3521 if (sec1->target_index - sec2->target_index != 0)
3522 return sec1->target_index - sec2->target_index;
3527 else if (TOEND (sec2))
3532 /* Sort by size, to put zero sized sections
3533 before others at the same address. */
3535 if (sec1->_raw_size < sec2->_raw_size)
3537 if (sec1->_raw_size > sec2->_raw_size)
3540 return sec1->target_index - sec2->target_index;
3543 /* Assign file positions to the sections based on the mapping from
3544 sections to segments. This function also sets up some fields in
3545 the file header, and writes out the program headers. */
3548 assign_file_positions_for_segments (abfd)
3551 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3553 struct elf_segment_map *m;
3555 Elf_Internal_Phdr *phdrs;
3557 bfd_vma filehdr_vaddr, filehdr_paddr;
3558 bfd_vma phdrs_vaddr, phdrs_paddr;
3559 Elf_Internal_Phdr *p;
3562 if (elf_tdata (abfd)->segment_map == NULL)
3564 if (! map_sections_to_segments (abfd))
3569 /* The placement algorithm assumes that non allocated sections are
3570 not in PT_LOAD segments. We ensure this here by removing such
3571 sections from the segment map. */
3572 for (m = elf_tdata (abfd)->segment_map;
3576 unsigned int new_count;
3579 if (m->p_type != PT_LOAD)
3583 for (i = 0; i < m->count; i ++)
3585 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3588 m->sections[new_count] = m->sections[i];
3594 if (new_count != m->count)
3595 m->count = new_count;
3599 if (bed->elf_backend_modify_segment_map)
3601 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3606 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3609 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3610 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3611 elf_elfheader (abfd)->e_phnum = count;
3616 /* If we already counted the number of program segments, make sure
3617 that we allocated enough space. This happens when SIZEOF_HEADERS
3618 is used in a linker script. */
3619 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3620 if (alloc != 0 && count > alloc)
3622 ((*_bfd_error_handler)
3623 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3624 bfd_get_filename (abfd), alloc, count));
3625 bfd_set_error (bfd_error_bad_value);
3632 amt = alloc * sizeof (Elf_Internal_Phdr);
3633 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3637 off = bed->s->sizeof_ehdr;
3638 off += alloc * bed->s->sizeof_phdr;
3645 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3652 /* If elf_segment_map is not from map_sections_to_segments, the
3653 sections may not be correctly ordered. NOTE: sorting should
3654 not be done to the PT_NOTE section of a corefile, which may
3655 contain several pseudo-sections artificially created by bfd.
3656 Sorting these pseudo-sections breaks things badly. */
3658 && !(elf_elfheader (abfd)->e_type == ET_CORE
3659 && m->p_type == PT_NOTE))
3660 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3663 p->p_type = m->p_type;
3664 p->p_flags = m->p_flags;
3666 if (p->p_type == PT_LOAD
3668 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3670 if ((abfd->flags & D_PAGED) != 0)
3671 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3674 bfd_size_type align;
3677 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3679 bfd_size_type secalign;
3681 secalign = bfd_get_section_alignment (abfd, *secpp);
3682 if (secalign > align)
3686 off += (m->sections[0]->vma - off) % (1 << align);
3693 p->p_vaddr = m->sections[0]->vma;
3695 if (m->p_paddr_valid)
3696 p->p_paddr = m->p_paddr;
3697 else if (m->count == 0)
3700 p->p_paddr = m->sections[0]->lma;
3702 if (p->p_type == PT_LOAD
3703 && (abfd->flags & D_PAGED) != 0)
3704 p->p_align = bed->maxpagesize;
3705 else if (m->count == 0)
3706 p->p_align = bed->s->file_align;
3714 if (m->includes_filehdr)
3716 if (! m->p_flags_valid)
3719 p->p_filesz = bed->s->sizeof_ehdr;
3720 p->p_memsz = bed->s->sizeof_ehdr;
3723 BFD_ASSERT (p->p_type == PT_LOAD);
3725 if (p->p_vaddr < (bfd_vma) off)
3727 (*_bfd_error_handler)
3728 (_("%s: Not enough room for program headers, try linking with -N"),
3729 bfd_get_filename (abfd));
3730 bfd_set_error (bfd_error_bad_value);
3735 if (! m->p_paddr_valid)
3738 if (p->p_type == PT_LOAD)
3740 filehdr_vaddr = p->p_vaddr;
3741 filehdr_paddr = p->p_paddr;
3745 if (m->includes_phdrs)
3747 if (! m->p_flags_valid)
3750 if (m->includes_filehdr)
3752 if (p->p_type == PT_LOAD)
3754 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3755 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3760 p->p_offset = bed->s->sizeof_ehdr;
3764 BFD_ASSERT (p->p_type == PT_LOAD);
3765 p->p_vaddr -= off - p->p_offset;
3766 if (! m->p_paddr_valid)
3767 p->p_paddr -= off - p->p_offset;
3770 if (p->p_type == PT_LOAD)
3772 phdrs_vaddr = p->p_vaddr;
3773 phdrs_paddr = p->p_paddr;
3776 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3779 p->p_filesz += alloc * bed->s->sizeof_phdr;
3780 p->p_memsz += alloc * bed->s->sizeof_phdr;
3783 if (p->p_type == PT_LOAD
3784 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3786 if (! m->includes_filehdr && ! m->includes_phdrs)
3792 adjust = off - (p->p_offset + p->p_filesz);
3793 p->p_filesz += adjust;
3794 p->p_memsz += adjust;
3800 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3804 bfd_size_type align;
3808 align = 1 << bfd_get_section_alignment (abfd, sec);
3810 /* The section may have artificial alignment forced by a
3811 link script. Notice this case by the gap between the
3812 cumulative phdr lma and the section's lma. */
3813 if (p->p_paddr + p->p_memsz < sec->lma)
3815 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3817 p->p_memsz += adjust;
3820 if ((flags & SEC_LOAD) != 0)
3821 p->p_filesz += adjust;
3824 if (p->p_type == PT_LOAD)
3826 bfd_signed_vma adjust;
3828 if ((flags & SEC_LOAD) != 0)
3830 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3834 else if ((flags & SEC_ALLOC) != 0)
3836 /* The section VMA must equal the file position
3837 modulo the page size. FIXME: I'm not sure if
3838 this adjustment is really necessary. We used to
3839 not have the SEC_LOAD case just above, and then
3840 this was necessary, but now I'm not sure. */
3841 if ((abfd->flags & D_PAGED) != 0)
3842 adjust = (sec->vma - voff) % bed->maxpagesize;
3844 adjust = (sec->vma - voff) % align;
3853 (* _bfd_error_handler) (_("\
3854 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3855 bfd_section_name (abfd, sec),
3860 p->p_memsz += adjust;
3863 if ((flags & SEC_LOAD) != 0)
3864 p->p_filesz += adjust;
3869 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3870 used in a linker script we may have a section with
3871 SEC_LOAD clear but which is supposed to have
3873 if ((flags & SEC_LOAD) != 0
3874 || (flags & SEC_HAS_CONTENTS) != 0)
3875 off += sec->_raw_size;
3877 if ((flags & SEC_ALLOC) != 0)
3878 voff += sec->_raw_size;
3881 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3883 /* The actual "note" segment has i == 0.
3884 This is the one that actually contains everything. */
3888 p->p_filesz = sec->_raw_size;
3889 off += sec->_raw_size;
3894 /* Fake sections -- don't need to be written. */
3897 flags = sec->flags = 0;
3904 p->p_memsz += sec->_raw_size;
3906 if ((flags & SEC_LOAD) != 0)
3907 p->p_filesz += sec->_raw_size;
3909 if (p->p_type == PT_TLS
3910 && sec->_raw_size == 0
3911 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3913 struct bfd_link_order *o;
3914 bfd_vma tbss_size = 0;
3916 for (o = sec->link_order_head; o != NULL; o = o->next)
3917 if (tbss_size < o->offset + o->size)
3918 tbss_size = o->offset + o->size;
3920 p->p_memsz += tbss_size;
3923 if (align > p->p_align
3924 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3928 if (! m->p_flags_valid)
3931 if ((flags & SEC_CODE) != 0)
3933 if ((flags & SEC_READONLY) == 0)
3939 /* Now that we have set the section file positions, we can set up
3940 the file positions for the non PT_LOAD segments. */
3941 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3945 if (p->p_type != PT_LOAD && m->count > 0)
3947 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3948 p->p_offset = m->sections[0]->filepos;
3952 if (m->includes_filehdr)
3954 p->p_vaddr = filehdr_vaddr;
3955 if (! m->p_paddr_valid)
3956 p->p_paddr = filehdr_paddr;
3958 else if (m->includes_phdrs)
3960 p->p_vaddr = phdrs_vaddr;
3961 if (! m->p_paddr_valid)
3962 p->p_paddr = phdrs_paddr;
3967 /* Clear out any program headers we allocated but did not use. */
3968 for (; count < alloc; count++, p++)
3970 memset (p, 0, sizeof *p);
3971 p->p_type = PT_NULL;
3974 elf_tdata (abfd)->phdr = phdrs;
3976 elf_tdata (abfd)->next_file_pos = off;
3978 /* Write out the program headers. */
3979 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
3980 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3986 /* Get the size of the program header.
3988 If this is called by the linker before any of the section VMA's are set, it
3989 can't calculate the correct value for a strange memory layout. This only
3990 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3991 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3992 data segment (exclusive of .interp and .dynamic).
3994 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3995 will be two segments. */
3997 static bfd_size_type
3998 get_program_header_size (abfd)
4003 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4005 /* We can't return a different result each time we're called. */
4006 if (elf_tdata (abfd)->program_header_size != 0)
4007 return elf_tdata (abfd)->program_header_size;
4009 if (elf_tdata (abfd)->segment_map != NULL)
4011 struct elf_segment_map *m;
4014 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4016 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4017 return elf_tdata (abfd)->program_header_size;
4020 /* Assume we will need exactly two PT_LOAD segments: one for text
4021 and one for data. */
4024 s = bfd_get_section_by_name (abfd, ".interp");
4025 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4027 /* If we have a loadable interpreter section, we need a
4028 PT_INTERP segment. In this case, assume we also need a
4029 PT_PHDR segment, although that may not be true for all
4034 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4036 /* We need a PT_DYNAMIC segment. */
4040 if (elf_tdata (abfd)->eh_frame_hdr
4041 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
4043 /* We need a PT_GNU_EH_FRAME segment. */
4047 for (s = abfd->sections; s != NULL; s = s->next)
4049 if ((s->flags & SEC_LOAD) != 0
4050 && strncmp (s->name, ".note", 5) == 0)
4052 /* We need a PT_NOTE segment. */
4057 for (s = abfd->sections; s != NULL; s = s->next)
4059 if (s->flags & SEC_THREAD_LOCAL)
4061 /* We need a PT_TLS segment. */
4067 /* Let the backend count up any program headers it might need. */
4068 if (bed->elf_backend_additional_program_headers)
4072 a = (*bed->elf_backend_additional_program_headers) (abfd);
4078 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4079 return elf_tdata (abfd)->program_header_size;
4082 /* Work out the file positions of all the sections. This is called by
4083 _bfd_elf_compute_section_file_positions. All the section sizes and
4084 VMAs must be known before this is called.
4086 We do not consider reloc sections at this point, unless they form
4087 part of the loadable image. Reloc sections are assigned file
4088 positions in assign_file_positions_for_relocs, which is called by
4089 write_object_contents and final_link.
4091 We also don't set the positions of the .symtab and .strtab here. */
4094 assign_file_positions_except_relocs (abfd)
4097 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4098 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4099 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4100 unsigned int num_sec = elf_numsections (abfd);
4102 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4104 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4105 && bfd_get_format (abfd) != bfd_core)
4107 Elf_Internal_Shdr **hdrpp;
4110 /* Start after the ELF header. */
4111 off = i_ehdrp->e_ehsize;
4113 /* We are not creating an executable, which means that we are
4114 not creating a program header, and that the actual order of
4115 the sections in the file is unimportant. */
4116 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4118 Elf_Internal_Shdr *hdr;
4121 if (hdr->sh_type == SHT_REL
4122 || hdr->sh_type == SHT_RELA
4123 || i == tdata->symtab_section
4124 || i == tdata->symtab_shndx_section
4125 || i == tdata->strtab_section)
4127 hdr->sh_offset = -1;
4130 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4132 if (i == SHN_LORESERVE - 1)
4134 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4135 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4142 Elf_Internal_Shdr **hdrpp;
4144 /* Assign file positions for the loaded sections based on the
4145 assignment of sections to segments. */
4146 if (! assign_file_positions_for_segments (abfd))
4149 /* Assign file positions for the other sections. */
4151 off = elf_tdata (abfd)->next_file_pos;
4152 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4154 Elf_Internal_Shdr *hdr;
4157 if (hdr->bfd_section != NULL
4158 && hdr->bfd_section->filepos != 0)
4159 hdr->sh_offset = hdr->bfd_section->filepos;
4160 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4162 ((*_bfd_error_handler)
4163 (_("%s: warning: allocated section `%s' not in segment"),
4164 bfd_get_filename (abfd),
4165 (hdr->bfd_section == NULL
4167 : hdr->bfd_section->name)));
4168 if ((abfd->flags & D_PAGED) != 0)
4169 off += (hdr->sh_addr - off) % bed->maxpagesize;
4171 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4172 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4175 else if (hdr->sh_type == SHT_REL
4176 || hdr->sh_type == SHT_RELA
4177 || hdr == i_shdrpp[tdata->symtab_section]
4178 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4179 || hdr == i_shdrpp[tdata->strtab_section])
4180 hdr->sh_offset = -1;
4182 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4184 if (i == SHN_LORESERVE - 1)
4186 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4187 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4192 /* Place the section headers. */
4193 off = align_file_position (off, bed->s->file_align);
4194 i_ehdrp->e_shoff = off;
4195 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4197 elf_tdata (abfd)->next_file_pos = off;
4206 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4207 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4208 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4209 struct elf_strtab_hash *shstrtab;
4210 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4212 i_ehdrp = elf_elfheader (abfd);
4213 i_shdrp = elf_elfsections (abfd);
4215 shstrtab = _bfd_elf_strtab_init ();
4216 if (shstrtab == NULL)
4219 elf_shstrtab (abfd) = shstrtab;
4221 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4222 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4223 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4224 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4226 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4227 i_ehdrp->e_ident[EI_DATA] =
4228 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4229 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4231 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
4233 if ((abfd->flags & DYNAMIC) != 0)
4234 i_ehdrp->e_type = ET_DYN;
4235 else if ((abfd->flags & EXEC_P) != 0)
4236 i_ehdrp->e_type = ET_EXEC;
4237 else if (bfd_get_format (abfd) == bfd_core)
4238 i_ehdrp->e_type = ET_CORE;
4240 i_ehdrp->e_type = ET_REL;
4242 switch (bfd_get_arch (abfd))
4244 case bfd_arch_unknown:
4245 i_ehdrp->e_machine = EM_NONE;
4248 /* There used to be a long list of cases here, each one setting
4249 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4250 in the corresponding bfd definition. To avoid duplication,
4251 the switch was removed. Machines that need special handling
4252 can generally do it in elf_backend_final_write_processing(),
4253 unless they need the information earlier than the final write.
4254 Such need can generally be supplied by replacing the tests for
4255 e_machine with the conditions used to determine it. */
4257 if (get_elf_backend_data (abfd) != NULL)
4258 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4260 i_ehdrp->e_machine = EM_NONE;
4263 i_ehdrp->e_version = bed->s->ev_current;
4264 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4266 /* No program header, for now. */
4267 i_ehdrp->e_phoff = 0;
4268 i_ehdrp->e_phentsize = 0;
4269 i_ehdrp->e_phnum = 0;
4271 /* Each bfd section is section header entry. */
4272 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4273 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4275 /* If we're building an executable, we'll need a program header table. */
4276 if (abfd->flags & EXEC_P)
4278 /* It all happens later. */
4280 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4282 /* elf_build_phdrs() returns a (NULL-terminated) array of
4283 Elf_Internal_Phdrs. */
4284 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4285 i_ehdrp->e_phoff = outbase;
4286 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4291 i_ehdrp->e_phentsize = 0;
4293 i_ehdrp->e_phoff = 0;
4296 elf_tdata (abfd)->symtab_hdr.sh_name =
4297 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
4298 elf_tdata (abfd)->strtab_hdr.sh_name =
4299 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
4300 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4301 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
4302 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4303 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4304 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4310 /* Assign file positions for all the reloc sections which are not part
4311 of the loadable file image. */
4314 _bfd_elf_assign_file_positions_for_relocs (abfd)
4318 unsigned int i, num_sec;
4319 Elf_Internal_Shdr **shdrpp;
4321 off = elf_tdata (abfd)->next_file_pos;
4323 num_sec = elf_numsections (abfd);
4324 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4326 Elf_Internal_Shdr *shdrp;
4329 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4330 && shdrp->sh_offset == -1)
4331 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
4334 elf_tdata (abfd)->next_file_pos = off;
4338 _bfd_elf_write_object_contents (abfd)
4341 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4342 Elf_Internal_Ehdr *i_ehdrp;
4343 Elf_Internal_Shdr **i_shdrp;
4345 unsigned int count, num_sec;
4347 if (! abfd->output_has_begun
4348 && ! _bfd_elf_compute_section_file_positions
4349 (abfd, (struct bfd_link_info *) NULL))
4352 i_shdrp = elf_elfsections (abfd);
4353 i_ehdrp = elf_elfheader (abfd);
4356 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4360 _bfd_elf_assign_file_positions_for_relocs (abfd);
4362 /* After writing the headers, we need to write the sections too... */
4363 num_sec = elf_numsections (abfd);
4364 for (count = 1; count < num_sec; count++)
4366 if (bed->elf_backend_section_processing)
4367 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4368 if (i_shdrp[count]->contents)
4370 bfd_size_type amt = i_shdrp[count]->sh_size;
4372 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4373 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4376 if (count == SHN_LORESERVE - 1)
4377 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4380 /* Write out the section header names. */
4381 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4382 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4385 if (bed->elf_backend_final_write_processing)
4386 (*bed->elf_backend_final_write_processing) (abfd,
4387 elf_tdata (abfd)->linker);
4389 return bed->s->write_shdrs_and_ehdr (abfd);
4393 _bfd_elf_write_corefile_contents (abfd)
4396 /* Hopefully this can be done just like an object file. */
4397 return _bfd_elf_write_object_contents (abfd);
4400 /* Given a section, search the header to find them. */
4403 _bfd_elf_section_from_bfd_section (abfd, asect)
4407 struct elf_backend_data *bed;
4410 if (elf_section_data (asect) != NULL
4411 && elf_section_data (asect)->this_idx != 0)
4412 return elf_section_data (asect)->this_idx;
4414 if (bfd_is_abs_section (asect))
4416 else if (bfd_is_com_section (asect))
4418 else if (bfd_is_und_section (asect))
4422 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4423 int maxindex = elf_numsections (abfd);
4425 for (index = 1; index < maxindex; index++)
4427 Elf_Internal_Shdr *hdr = i_shdrp[index];
4429 if (hdr != NULL && hdr->bfd_section == asect)
4435 bed = get_elf_backend_data (abfd);
4436 if (bed->elf_backend_section_from_bfd_section)
4440 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4445 bfd_set_error (bfd_error_nonrepresentable_section);
4450 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4454 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4456 asymbol **asym_ptr_ptr;
4458 asymbol *asym_ptr = *asym_ptr_ptr;
4460 flagword flags = asym_ptr->flags;
4462 /* When gas creates relocations against local labels, it creates its
4463 own symbol for the section, but does put the symbol into the
4464 symbol chain, so udata is 0. When the linker is generating
4465 relocatable output, this section symbol may be for one of the
4466 input sections rather than the output section. */
4467 if (asym_ptr->udata.i == 0
4468 && (flags & BSF_SECTION_SYM)
4469 && asym_ptr->section)
4473 if (asym_ptr->section->output_section != NULL)
4474 indx = asym_ptr->section->output_section->index;
4476 indx = asym_ptr->section->index;
4477 if (indx < elf_num_section_syms (abfd)
4478 && elf_section_syms (abfd)[indx] != NULL)
4479 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4482 idx = asym_ptr->udata.i;
4486 /* This case can occur when using --strip-symbol on a symbol
4487 which is used in a relocation entry. */
4488 (*_bfd_error_handler)
4489 (_("%s: symbol `%s' required but not present"),
4490 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4491 bfd_set_error (bfd_error_no_symbols);
4498 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4499 (long) asym_ptr, asym_ptr->name, idx, flags,
4500 elf_symbol_flags (flags));
4508 /* Copy private BFD data. This copies any program header information. */
4511 copy_private_bfd_data (ibfd, obfd)
4515 Elf_Internal_Ehdr * iehdr;
4516 struct elf_segment_map * map;
4517 struct elf_segment_map * map_first;
4518 struct elf_segment_map ** pointer_to_map;
4519 Elf_Internal_Phdr * segment;
4522 unsigned int num_segments;
4523 boolean phdr_included = false;
4524 bfd_vma maxpagesize;
4525 struct elf_segment_map * phdr_adjust_seg = NULL;
4526 unsigned int phdr_adjust_num = 0;
4527 struct elf_backend_data * bed;
4529 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4530 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4533 if (elf_tdata (ibfd)->phdr == NULL)
4536 bed = get_elf_backend_data (ibfd);
4537 iehdr = elf_elfheader (ibfd);
4540 pointer_to_map = &map_first;
4542 num_segments = elf_elfheader (ibfd)->e_phnum;
4543 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4545 /* Returns the end address of the segment + 1. */
4546 #define SEGMENT_END(segment, start) \
4547 (start + (segment->p_memsz > segment->p_filesz \
4548 ? segment->p_memsz : segment->p_filesz))
4550 /* Returns true if the given section is contained within
4551 the given segment. VMA addresses are compared. */
4552 #define IS_CONTAINED_BY_VMA(section, segment) \
4553 (section->vma >= segment->p_vaddr \
4554 && (section->vma + section->_raw_size \
4555 <= (SEGMENT_END (segment, segment->p_vaddr))))
4557 /* Returns true if the given section is contained within
4558 the given segment. LMA addresses are compared. */
4559 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4560 (section->lma >= base \
4561 && (section->lma + section->_raw_size \
4562 <= SEGMENT_END (segment, base)))
4564 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4565 #define IS_COREFILE_NOTE(p, s) \
4566 (p->p_type == PT_NOTE \
4567 && bfd_get_format (ibfd) == bfd_core \
4568 && s->vma == 0 && s->lma == 0 \
4569 && (bfd_vma) s->filepos >= p->p_offset \
4570 && ((bfd_vma) s->filepos + s->_raw_size \
4571 <= p->p_offset + p->p_filesz))
4573 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4574 linker, which generates a PT_INTERP section with p_vaddr and
4575 p_memsz set to 0. */
4576 #define IS_SOLARIS_PT_INTERP(p, s) \
4578 && p->p_paddr == 0 \
4579 && p->p_memsz == 0 \
4580 && p->p_filesz > 0 \
4581 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4582 && s->_raw_size > 0 \
4583 && (bfd_vma) s->filepos >= p->p_offset \
4584 && ((bfd_vma) s->filepos + s->_raw_size \
4585 <= p->p_offset + p->p_filesz))
4587 /* Decide if the given section should be included in the given segment.
4588 A section will be included if:
4589 1. It is within the address space of the segment -- we use the LMA
4590 if that is set for the segment and the VMA otherwise,
4591 2. It is an allocated segment,
4592 3. There is an output section associated with it,
4593 4. The section has not already been allocated to a previous segment. */
4594 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4595 ((((segment->p_paddr \
4596 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4597 : IS_CONTAINED_BY_VMA (section, segment)) \
4598 && (section->flags & SEC_ALLOC) != 0) \
4599 || IS_COREFILE_NOTE (segment, section)) \
4600 && section->output_section != NULL \
4601 && ! section->segment_mark)
4603 /* Returns true iff seg1 starts after the end of seg2. */
4604 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4605 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4607 /* Returns true iff seg1 and seg2 overlap. */
4608 #define SEGMENT_OVERLAPS(seg1, seg2) \
4609 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \
4610 || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4612 /* Initialise the segment mark field. */
4613 for (section = ibfd->sections; section != NULL; section = section->next)
4614 section->segment_mark = false;
4616 /* Scan through the segments specified in the program header
4617 of the input BFD. For this first scan we look for overlaps
4618 in the loadable segments. These can be created by weird
4619 parameters to objcopy. Also, fix some solaris weirdness. */
4620 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4625 Elf_Internal_Phdr *segment2;
4627 if (segment->p_type == PT_INTERP)
4628 for (section = ibfd->sections; section; section = section->next)
4629 if (IS_SOLARIS_PT_INTERP (segment, section))
4631 /* Mininal change so that the normal section to segment
4632 assigment code will work. */
4633 segment->p_vaddr = section->vma;
4637 if (segment->p_type != PT_LOAD)
4640 /* Determine if this segment overlaps any previous segments. */
4641 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4643 bfd_signed_vma extra_length;
4645 if (segment2->p_type != PT_LOAD
4646 || ! SEGMENT_OVERLAPS (segment, segment2))
4649 /* Merge the two segments together. */
4650 if (segment2->p_vaddr < segment->p_vaddr)
4652 /* Extend SEGMENT2 to include SEGMENT and then delete
4655 SEGMENT_END (segment, segment->p_vaddr)
4656 - SEGMENT_END (segment2, segment2->p_vaddr);
4658 if (extra_length > 0)
4660 segment2->p_memsz += extra_length;
4661 segment2->p_filesz += extra_length;
4664 segment->p_type = PT_NULL;
4666 /* Since we have deleted P we must restart the outer loop. */
4668 segment = elf_tdata (ibfd)->phdr;
4673 /* Extend SEGMENT to include SEGMENT2 and then delete
4676 SEGMENT_END (segment2, segment2->p_vaddr)
4677 - SEGMENT_END (segment, segment->p_vaddr);
4679 if (extra_length > 0)
4681 segment->p_memsz += extra_length;
4682 segment->p_filesz += extra_length;
4685 segment2->p_type = PT_NULL;
4690 /* The second scan attempts to assign sections to segments. */
4691 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4695 unsigned int section_count;
4696 asection ** sections;
4697 asection * output_section;
4699 bfd_vma matching_lma;
4700 bfd_vma suggested_lma;
4704 if (segment->p_type == PT_NULL)
4707 /* Compute how many sections might be placed into this segment. */
4709 for (section = ibfd->sections; section != NULL; section = section->next)
4710 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4713 /* Allocate a segment map big enough to contain all of the
4714 sections we have selected. */
4715 amt = sizeof (struct elf_segment_map);
4716 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4717 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4721 /* Initialise the fields of the segment map. Default to
4722 using the physical address of the segment in the input BFD. */
4724 map->p_type = segment->p_type;
4725 map->p_flags = segment->p_flags;
4726 map->p_flags_valid = 1;
4727 map->p_paddr = segment->p_paddr;
4728 map->p_paddr_valid = 1;
4730 /* Determine if this segment contains the ELF file header
4731 and if it contains the program headers themselves. */
4732 map->includes_filehdr = (segment->p_offset == 0
4733 && segment->p_filesz >= iehdr->e_ehsize);
4735 map->includes_phdrs = 0;
4737 if (! phdr_included || segment->p_type != PT_LOAD)
4739 map->includes_phdrs =
4740 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4741 && (segment->p_offset + segment->p_filesz
4742 >= ((bfd_vma) iehdr->e_phoff
4743 + iehdr->e_phnum * iehdr->e_phentsize)));
4745 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4746 phdr_included = true;
4749 if (section_count == 0)
4751 /* Special segments, such as the PT_PHDR segment, may contain
4752 no sections, but ordinary, loadable segments should contain
4753 something. They are allowed by the ELF spec however, so only
4754 a warning is produced. */
4755 if (segment->p_type == PT_LOAD)
4756 (*_bfd_error_handler)
4757 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4758 bfd_archive_filename (ibfd));
4761 *pointer_to_map = map;
4762 pointer_to_map = &map->next;
4767 /* Now scan the sections in the input BFD again and attempt
4768 to add their corresponding output sections to the segment map.
4769 The problem here is how to handle an output section which has
4770 been moved (ie had its LMA changed). There are four possibilities:
4772 1. None of the sections have been moved.
4773 In this case we can continue to use the segment LMA from the
4776 2. All of the sections have been moved by the same amount.
4777 In this case we can change the segment's LMA to match the LMA
4778 of the first section.
4780 3. Some of the sections have been moved, others have not.
4781 In this case those sections which have not been moved can be
4782 placed in the current segment which will have to have its size,
4783 and possibly its LMA changed, and a new segment or segments will
4784 have to be created to contain the other sections.
4786 4. The sections have been moved, but not be the same amount.
4787 In this case we can change the segment's LMA to match the LMA
4788 of the first section and we will have to create a new segment
4789 or segments to contain the other sections.
4791 In order to save time, we allocate an array to hold the section
4792 pointers that we are interested in. As these sections get assigned
4793 to a segment, they are removed from this array. */
4795 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4796 to work around this long long bug. */
4797 amt = section_count * sizeof (asection *);
4798 sections = (asection **) bfd_malloc (amt);
4799 if (sections == NULL)
4802 /* Step One: Scan for segment vs section LMA conflicts.
4803 Also add the sections to the section array allocated above.
4804 Also add the sections to the current segment. In the common
4805 case, where the sections have not been moved, this means that
4806 we have completely filled the segment, and there is nothing
4812 for (j = 0, section = ibfd->sections;
4814 section = section->next)
4816 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4818 output_section = section->output_section;
4820 sections[j ++] = section;
4822 /* The Solaris native linker always sets p_paddr to 0.
4823 We try to catch that case here, and set it to the
4824 correct value. Note - some backends require that
4825 p_paddr be left as zero. */
4826 if (segment->p_paddr == 0
4827 && segment->p_vaddr != 0
4828 && (! bed->want_p_paddr_set_to_zero)
4830 && output_section->lma != 0
4831 && (output_section->vma == (segment->p_vaddr
4832 + (map->includes_filehdr
4835 + (map->includes_phdrs
4837 * iehdr->e_phentsize)
4839 map->p_paddr = segment->p_vaddr;
4841 /* Match up the physical address of the segment with the
4842 LMA address of the output section. */
4843 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4844 || IS_COREFILE_NOTE (segment, section)
4845 || (bed->want_p_paddr_set_to_zero &&
4846 IS_CONTAINED_BY_VMA (output_section, segment))
4849 if (matching_lma == 0)
4850 matching_lma = output_section->lma;
4852 /* We assume that if the section fits within the segment
4853 then it does not overlap any other section within that
4855 map->sections[isec ++] = output_section;
4857 else if (suggested_lma == 0)
4858 suggested_lma = output_section->lma;
4862 BFD_ASSERT (j == section_count);
4864 /* Step Two: Adjust the physical address of the current segment,
4866 if (isec == section_count)
4868 /* All of the sections fitted within the segment as currently
4869 specified. This is the default case. Add the segment to
4870 the list of built segments and carry on to process the next
4871 program header in the input BFD. */
4872 map->count = section_count;
4873 *pointer_to_map = map;
4874 pointer_to_map = &map->next;
4881 if (matching_lma != 0)
4883 /* At least one section fits inside the current segment.
4884 Keep it, but modify its physical address to match the
4885 LMA of the first section that fitted. */
4886 map->p_paddr = matching_lma;
4890 /* None of the sections fitted inside the current segment.
4891 Change the current segment's physical address to match
4892 the LMA of the first section. */
4893 map->p_paddr = suggested_lma;
4896 /* Offset the segment physical address from the lma
4897 to allow for space taken up by elf headers. */
4898 if (map->includes_filehdr)
4899 map->p_paddr -= iehdr->e_ehsize;
4901 if (map->includes_phdrs)
4903 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4905 /* iehdr->e_phnum is just an estimate of the number
4906 of program headers that we will need. Make a note
4907 here of the number we used and the segment we chose
4908 to hold these headers, so that we can adjust the
4909 offset when we know the correct value. */
4910 phdr_adjust_num = iehdr->e_phnum;
4911 phdr_adjust_seg = map;
4915 /* Step Three: Loop over the sections again, this time assigning
4916 those that fit to the current segment and removing them from the
4917 sections array; but making sure not to leave large gaps. Once all
4918 possible sections have been assigned to the current segment it is
4919 added to the list of built segments and if sections still remain
4920 to be assigned, a new segment is constructed before repeating
4928 /* Fill the current segment with sections that fit. */
4929 for (j = 0; j < section_count; j++)
4931 section = sections[j];
4933 if (section == NULL)
4936 output_section = section->output_section;
4938 BFD_ASSERT (output_section != NULL);
4940 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4941 || IS_COREFILE_NOTE (segment, section))
4943 if (map->count == 0)
4945 /* If the first section in a segment does not start at
4946 the beginning of the segment, then something is
4948 if (output_section->lma !=
4950 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4951 + (map->includes_phdrs
4952 ? iehdr->e_phnum * iehdr->e_phentsize
4958 asection * prev_sec;
4960 prev_sec = map->sections[map->count - 1];
4962 /* If the gap between the end of the previous section
4963 and the start of this section is more than
4964 maxpagesize then we need to start a new segment. */
4965 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4967 < BFD_ALIGN (output_section->lma, maxpagesize))
4968 || ((prev_sec->lma + prev_sec->_raw_size)
4969 > output_section->lma))
4971 if (suggested_lma == 0)
4972 suggested_lma = output_section->lma;
4978 map->sections[map->count++] = output_section;
4981 section->segment_mark = true;
4983 else if (suggested_lma == 0)
4984 suggested_lma = output_section->lma;
4987 BFD_ASSERT (map->count > 0);
4989 /* Add the current segment to the list of built segments. */
4990 *pointer_to_map = map;
4991 pointer_to_map = &map->next;
4993 if (isec < section_count)
4995 /* We still have not allocated all of the sections to
4996 segments. Create a new segment here, initialise it
4997 and carry on looping. */
4998 amt = sizeof (struct elf_segment_map);
4999 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5000 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5004 /* Initialise the fields of the segment map. Set the physical
5005 physical address to the LMA of the first section that has
5006 not yet been assigned. */
5008 map->p_type = segment->p_type;
5009 map->p_flags = segment->p_flags;
5010 map->p_flags_valid = 1;
5011 map->p_paddr = suggested_lma;
5012 map->p_paddr_valid = 1;
5013 map->includes_filehdr = 0;
5014 map->includes_phdrs = 0;
5017 while (isec < section_count);
5022 /* The Solaris linker creates program headers in which all the
5023 p_paddr fields are zero. When we try to objcopy or strip such a
5024 file, we get confused. Check for this case, and if we find it
5025 reset the p_paddr_valid fields. */
5026 for (map = map_first; map != NULL; map = map->next)
5027 if (map->p_paddr != 0)
5031 for (map = map_first; map != NULL; map = map->next)
5032 map->p_paddr_valid = 0;
5035 elf_tdata (obfd)->segment_map = map_first;
5037 /* If we had to estimate the number of program headers that were
5038 going to be needed, then check our estimate now and adjust
5039 the offset if necessary. */
5040 if (phdr_adjust_seg != NULL)
5044 for (count = 0, map = map_first; map != NULL; map = map->next)
5047 if (count > phdr_adjust_num)
5048 phdr_adjust_seg->p_paddr
5049 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5053 /* Final Step: Sort the segments into ascending order of physical
5055 if (map_first != NULL)
5057 struct elf_segment_map *prev;
5060 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5062 /* Yes I know - its a bubble sort.... */
5063 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5065 /* Swap map and map->next. */
5066 prev->next = map->next;
5067 map->next = map->next->next;
5068 prev->next->next = map;
5078 #undef IS_CONTAINED_BY_VMA
5079 #undef IS_CONTAINED_BY_LMA
5080 #undef IS_COREFILE_NOTE
5081 #undef IS_SOLARIS_PT_INTERP
5082 #undef INCLUDE_SECTION_IN_SEGMENT
5083 #undef SEGMENT_AFTER_SEGMENT
5084 #undef SEGMENT_OVERLAPS
5088 /* Copy private section information. This copies over the entsize
5089 field, and sometimes the info field. */
5092 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
5098 Elf_Internal_Shdr *ihdr, *ohdr;
5100 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5101 || obfd->xvec->flavour != bfd_target_elf_flavour)
5104 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5108 /* Only set up the segments if there are no more SEC_ALLOC
5109 sections. FIXME: This won't do the right thing if objcopy is
5110 used to remove the last SEC_ALLOC section, since objcopy
5111 won't call this routine in that case. */
5112 for (s = isec->next; s != NULL; s = s->next)
5113 if ((s->flags & SEC_ALLOC) != 0)
5117 if (! copy_private_bfd_data (ibfd, obfd))
5122 ihdr = &elf_section_data (isec)->this_hdr;
5123 ohdr = &elf_section_data (osec)->this_hdr;
5125 ohdr->sh_entsize = ihdr->sh_entsize;
5127 if (ihdr->sh_type == SHT_SYMTAB
5128 || ihdr->sh_type == SHT_DYNSYM
5129 || ihdr->sh_type == SHT_GNU_verneed
5130 || ihdr->sh_type == SHT_GNU_verdef)
5131 ohdr->sh_info = ihdr->sh_info;
5133 /* Set things up for objcopy. The output SHT_GROUP section will
5134 have its elf_next_in_group pointing back to the input group
5136 elf_next_in_group (osec) = elf_next_in_group (isec);
5137 elf_group_name (osec) = elf_group_name (isec);
5139 elf_section_data (osec)->use_rela_p
5140 = elf_section_data (isec)->use_rela_p;
5145 /* Copy private symbol information. If this symbol is in a section
5146 which we did not map into a BFD section, try to map the section
5147 index correctly. We use special macro definitions for the mapped
5148 section indices; these definitions are interpreted by the
5149 swap_out_syms function. */
5151 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5152 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5153 #define MAP_STRTAB (SHN_HIOS + 3)
5154 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5155 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5158 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5164 elf_symbol_type *isym, *osym;
5166 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5167 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5170 isym = elf_symbol_from (ibfd, isymarg);
5171 osym = elf_symbol_from (obfd, osymarg);
5175 && bfd_is_abs_section (isym->symbol.section))
5179 shndx = isym->internal_elf_sym.st_shndx;
5180 if (shndx == elf_onesymtab (ibfd))
5181 shndx = MAP_ONESYMTAB;
5182 else if (shndx == elf_dynsymtab (ibfd))
5183 shndx = MAP_DYNSYMTAB;
5184 else if (shndx == elf_tdata (ibfd)->strtab_section)
5186 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5187 shndx = MAP_SHSTRTAB;
5188 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5189 shndx = MAP_SYM_SHNDX;
5190 osym->internal_elf_sym.st_shndx = shndx;
5196 /* Swap out the symbols. */
5199 swap_out_syms (abfd, sttp, relocatable_p)
5201 struct bfd_strtab_hash **sttp;
5204 struct elf_backend_data *bed;
5207 struct bfd_strtab_hash *stt;
5208 Elf_Internal_Shdr *symtab_hdr;
5209 Elf_Internal_Shdr *symtab_shndx_hdr;
5210 Elf_Internal_Shdr *symstrtab_hdr;
5211 char *outbound_syms;
5212 char *outbound_shndx;
5216 if (!elf_map_symbols (abfd))
5219 /* Dump out the symtabs. */
5220 stt = _bfd_elf_stringtab_init ();
5224 bed = get_elf_backend_data (abfd);
5225 symcount = bfd_get_symcount (abfd);
5226 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5227 symtab_hdr->sh_type = SHT_SYMTAB;
5228 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5229 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5230 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5231 symtab_hdr->sh_addralign = bed->s->file_align;
5233 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5234 symstrtab_hdr->sh_type = SHT_STRTAB;
5236 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5237 outbound_syms = bfd_alloc (abfd, amt);
5238 if (outbound_syms == NULL)
5240 symtab_hdr->contents = (PTR) outbound_syms;
5242 outbound_shndx = NULL;
5243 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5244 if (symtab_shndx_hdr->sh_name != 0)
5246 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5247 outbound_shndx = bfd_zalloc (abfd, amt);
5248 if (outbound_shndx == NULL)
5250 symtab_shndx_hdr->contents = outbound_shndx;
5251 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5252 symtab_shndx_hdr->sh_size = amt;
5253 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5254 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5257 /* now generate the data (for "contents") */
5259 /* Fill in zeroth symbol and swap it out. */
5260 Elf_Internal_Sym sym;
5266 sym.st_shndx = SHN_UNDEF;
5267 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5268 outbound_syms += bed->s->sizeof_sym;
5269 if (outbound_shndx != NULL)
5270 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5273 syms = bfd_get_outsymbols (abfd);
5274 for (idx = 0; idx < symcount; idx++)
5276 Elf_Internal_Sym sym;
5277 bfd_vma value = syms[idx]->value;
5278 elf_symbol_type *type_ptr;
5279 flagword flags = syms[idx]->flags;
5282 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5284 /* Local section symbols have no name. */
5289 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5292 if (sym.st_name == (unsigned long) -1)
5296 type_ptr = elf_symbol_from (abfd, syms[idx]);
5298 if ((flags & BSF_SECTION_SYM) == 0
5299 && bfd_is_com_section (syms[idx]->section))
5301 /* ELF common symbols put the alignment into the `value' field,
5302 and the size into the `size' field. This is backwards from
5303 how BFD handles it, so reverse it here. */
5304 sym.st_size = value;
5305 if (type_ptr == NULL
5306 || type_ptr->internal_elf_sym.st_value == 0)
5307 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5309 sym.st_value = type_ptr->internal_elf_sym.st_value;
5310 sym.st_shndx = _bfd_elf_section_from_bfd_section
5311 (abfd, syms[idx]->section);
5315 asection *sec = syms[idx]->section;
5318 if (sec->output_section)
5320 value += sec->output_offset;
5321 sec = sec->output_section;
5323 /* Don't add in the section vma for relocatable output. */
5324 if (! relocatable_p)
5326 sym.st_value = value;
5327 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5329 if (bfd_is_abs_section (sec)
5331 && type_ptr->internal_elf_sym.st_shndx != 0)
5333 /* This symbol is in a real ELF section which we did
5334 not create as a BFD section. Undo the mapping done
5335 by copy_private_symbol_data. */
5336 shndx = type_ptr->internal_elf_sym.st_shndx;
5340 shndx = elf_onesymtab (abfd);
5343 shndx = elf_dynsymtab (abfd);
5346 shndx = elf_tdata (abfd)->strtab_section;
5349 shndx = elf_tdata (abfd)->shstrtab_section;
5352 shndx = elf_tdata (abfd)->symtab_shndx_section;
5360 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5366 /* Writing this would be a hell of a lot easier if
5367 we had some decent documentation on bfd, and
5368 knew what to expect of the library, and what to
5369 demand of applications. For example, it
5370 appears that `objcopy' might not set the
5371 section of a symbol to be a section that is
5372 actually in the output file. */
5373 sec2 = bfd_get_section_by_name (abfd, sec->name);
5374 BFD_ASSERT (sec2 != 0);
5375 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5376 BFD_ASSERT (shndx != -1);
5380 sym.st_shndx = shndx;
5383 if ((flags & BSF_THREAD_LOCAL) != 0)
5385 else if ((flags & BSF_FUNCTION) != 0)
5387 else if ((flags & BSF_OBJECT) != 0)
5392 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5395 /* Processor-specific types */
5396 if (type_ptr != NULL
5397 && bed->elf_backend_get_symbol_type)
5398 type = ((*bed->elf_backend_get_symbol_type)
5399 (&type_ptr->internal_elf_sym, type));
5401 if (flags & BSF_SECTION_SYM)
5403 if (flags & BSF_GLOBAL)
5404 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5406 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5408 else if (bfd_is_com_section (syms[idx]->section))
5409 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5410 else if (bfd_is_und_section (syms[idx]->section))
5411 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5415 else if (flags & BSF_FILE)
5416 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5419 int bind = STB_LOCAL;
5421 if (flags & BSF_LOCAL)
5423 else if (flags & BSF_WEAK)
5425 else if (flags & BSF_GLOBAL)
5428 sym.st_info = ELF_ST_INFO (bind, type);
5431 if (type_ptr != NULL)
5432 sym.st_other = type_ptr->internal_elf_sym.st_other;
5436 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5437 outbound_syms += bed->s->sizeof_sym;
5438 if (outbound_shndx != NULL)
5439 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5443 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5444 symstrtab_hdr->sh_type = SHT_STRTAB;
5446 symstrtab_hdr->sh_flags = 0;
5447 symstrtab_hdr->sh_addr = 0;
5448 symstrtab_hdr->sh_entsize = 0;
5449 symstrtab_hdr->sh_link = 0;
5450 symstrtab_hdr->sh_info = 0;
5451 symstrtab_hdr->sh_addralign = 1;
5456 /* Return the number of bytes required to hold the symtab vector.
5458 Note that we base it on the count plus 1, since we will null terminate
5459 the vector allocated based on this size. However, the ELF symbol table
5460 always has a dummy entry as symbol #0, so it ends up even. */
5463 _bfd_elf_get_symtab_upper_bound (abfd)
5468 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5470 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5471 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5473 symtab_size -= sizeof (asymbol *);
5479 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5484 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5486 if (elf_dynsymtab (abfd) == 0)
5488 bfd_set_error (bfd_error_invalid_operation);
5492 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5493 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5495 symtab_size -= sizeof (asymbol *);
5501 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5502 bfd *abfd ATTRIBUTE_UNUSED;
5505 return (asect->reloc_count + 1) * sizeof (arelent *);
5508 /* Canonicalize the relocs. */
5511 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5519 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5521 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
5524 tblptr = section->relocation;
5525 for (i = 0; i < section->reloc_count; i++)
5526 *relptr++ = tblptr++;
5530 return section->reloc_count;
5534 _bfd_elf_get_symtab (abfd, alocation)
5536 asymbol **alocation;
5538 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5539 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
5542 bfd_get_symcount (abfd) = symcount;
5547 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5549 asymbol **alocation;
5551 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5552 long symcount = bed->s->slurp_symbol_table (abfd, alocation, true);
5555 bfd_get_dynamic_symcount (abfd) = symcount;
5559 /* Return the size required for the dynamic reloc entries. Any
5560 section that was actually installed in the BFD, and has type
5561 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5562 considered to be a dynamic reloc section. */
5565 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5571 if (elf_dynsymtab (abfd) == 0)
5573 bfd_set_error (bfd_error_invalid_operation);
5577 ret = sizeof (arelent *);
5578 for (s = abfd->sections; s != NULL; s = s->next)
5579 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5580 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5581 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5582 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5583 * sizeof (arelent *));
5588 /* Canonicalize the dynamic relocation entries. Note that we return
5589 the dynamic relocations as a single block, although they are
5590 actually associated with particular sections; the interface, which
5591 was designed for SunOS style shared libraries, expects that there
5592 is only one set of dynamic relocs. Any section that was actually
5593 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5594 the dynamic symbol table, is considered to be a dynamic reloc
5598 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5603 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5607 if (elf_dynsymtab (abfd) == 0)
5609 bfd_set_error (bfd_error_invalid_operation);
5613 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5615 for (s = abfd->sections; s != NULL; s = s->next)
5617 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5618 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5619 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5624 if (! (*slurp_relocs) (abfd, s, syms, true))
5626 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5628 for (i = 0; i < count; i++)
5639 /* Read in the version information. */
5642 _bfd_elf_slurp_version_tables (abfd)
5645 bfd_byte *contents = NULL;
5648 if (elf_dynverdef (abfd) != 0)
5650 Elf_Internal_Shdr *hdr;
5651 Elf_External_Verdef *everdef;
5652 Elf_Internal_Verdef *iverdef;
5653 Elf_Internal_Verdef *iverdefarr;
5654 Elf_Internal_Verdef iverdefmem;
5656 unsigned int maxidx;
5658 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5660 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5661 if (contents == NULL)
5663 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5664 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5667 /* We know the number of entries in the section but not the maximum
5668 index. Therefore we have to run through all entries and find
5670 everdef = (Elf_External_Verdef *) contents;
5672 for (i = 0; i < hdr->sh_info; ++i)
5674 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5676 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5677 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5679 everdef = ((Elf_External_Verdef *)
5680 ((bfd_byte *) everdef + iverdefmem.vd_next));
5683 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5684 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5685 if (elf_tdata (abfd)->verdef == NULL)
5688 elf_tdata (abfd)->cverdefs = maxidx;
5690 everdef = (Elf_External_Verdef *) contents;
5691 iverdefarr = elf_tdata (abfd)->verdef;
5692 for (i = 0; i < hdr->sh_info; i++)
5694 Elf_External_Verdaux *everdaux;
5695 Elf_Internal_Verdaux *iverdaux;
5698 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5700 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5701 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5703 iverdef->vd_bfd = abfd;
5705 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5706 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5707 if (iverdef->vd_auxptr == NULL)
5710 everdaux = ((Elf_External_Verdaux *)
5711 ((bfd_byte *) everdef + iverdef->vd_aux));
5712 iverdaux = iverdef->vd_auxptr;
5713 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5715 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5717 iverdaux->vda_nodename =
5718 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5719 iverdaux->vda_name);
5720 if (iverdaux->vda_nodename == NULL)
5723 if (j + 1 < iverdef->vd_cnt)
5724 iverdaux->vda_nextptr = iverdaux + 1;
5726 iverdaux->vda_nextptr = NULL;
5728 everdaux = ((Elf_External_Verdaux *)
5729 ((bfd_byte *) everdaux + iverdaux->vda_next));
5732 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5734 if (i + 1 < hdr->sh_info)
5735 iverdef->vd_nextdef = iverdef + 1;
5737 iverdef->vd_nextdef = NULL;
5739 everdef = ((Elf_External_Verdef *)
5740 ((bfd_byte *) everdef + iverdef->vd_next));
5747 if (elf_dynverref (abfd) != 0)
5749 Elf_Internal_Shdr *hdr;
5750 Elf_External_Verneed *everneed;
5751 Elf_Internal_Verneed *iverneed;
5754 hdr = &elf_tdata (abfd)->dynverref_hdr;
5756 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5757 elf_tdata (abfd)->verref =
5758 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5759 if (elf_tdata (abfd)->verref == NULL)
5762 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5764 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5765 if (contents == NULL)
5767 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5768 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5771 everneed = (Elf_External_Verneed *) contents;
5772 iverneed = elf_tdata (abfd)->verref;
5773 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5775 Elf_External_Vernaux *evernaux;
5776 Elf_Internal_Vernaux *ivernaux;
5779 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5781 iverneed->vn_bfd = abfd;
5783 iverneed->vn_filename =
5784 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5786 if (iverneed->vn_filename == NULL)
5789 amt = iverneed->vn_cnt;
5790 amt *= sizeof (Elf_Internal_Vernaux);
5791 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5793 evernaux = ((Elf_External_Vernaux *)
5794 ((bfd_byte *) everneed + iverneed->vn_aux));
5795 ivernaux = iverneed->vn_auxptr;
5796 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5798 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5800 ivernaux->vna_nodename =
5801 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5802 ivernaux->vna_name);
5803 if (ivernaux->vna_nodename == NULL)
5806 if (j + 1 < iverneed->vn_cnt)
5807 ivernaux->vna_nextptr = ivernaux + 1;
5809 ivernaux->vna_nextptr = NULL;
5811 evernaux = ((Elf_External_Vernaux *)
5812 ((bfd_byte *) evernaux + ivernaux->vna_next));
5815 if (i + 1 < hdr->sh_info)
5816 iverneed->vn_nextref = iverneed + 1;
5818 iverneed->vn_nextref = NULL;
5820 everneed = ((Elf_External_Verneed *)
5821 ((bfd_byte *) everneed + iverneed->vn_next));
5831 if (contents == NULL)
5837 _bfd_elf_make_empty_symbol (abfd)
5840 elf_symbol_type *newsym;
5841 bfd_size_type amt = sizeof (elf_symbol_type);
5843 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5848 newsym->symbol.the_bfd = abfd;
5849 return &newsym->symbol;
5854 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5855 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5859 bfd_symbol_info (symbol, ret);
5862 /* Return whether a symbol name implies a local symbol. Most targets
5863 use this function for the is_local_label_name entry point, but some
5867 _bfd_elf_is_local_label_name (abfd, name)
5868 bfd *abfd ATTRIBUTE_UNUSED;
5871 /* Normal local symbols start with ``.L''. */
5872 if (name[0] == '.' && name[1] == 'L')
5875 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5876 DWARF debugging symbols starting with ``..''. */
5877 if (name[0] == '.' && name[1] == '.')
5880 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5881 emitting DWARF debugging output. I suspect this is actually a
5882 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5883 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5884 underscore to be emitted on some ELF targets). For ease of use,
5885 we treat such symbols as local. */
5886 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5893 _bfd_elf_get_lineno (ignore_abfd, symbol)
5894 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5895 asymbol *symbol ATTRIBUTE_UNUSED;
5902 _bfd_elf_set_arch_mach (abfd, arch, machine)
5904 enum bfd_architecture arch;
5905 unsigned long machine;
5907 /* If this isn't the right architecture for this backend, and this
5908 isn't the generic backend, fail. */
5909 if (arch != get_elf_backend_data (abfd)->arch
5910 && arch != bfd_arch_unknown
5911 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5914 return bfd_default_set_arch_mach (abfd, arch, machine);
5917 /* Find the function to a particular section and offset,
5918 for error reporting. */
5921 elf_find_function (abfd, section, symbols, offset,
5922 filename_ptr, functionname_ptr)
5923 bfd *abfd ATTRIBUTE_UNUSED;
5927 const char **filename_ptr;
5928 const char **functionname_ptr;
5930 const char *filename;
5939 for (p = symbols; *p != NULL; p++)
5943 q = (elf_symbol_type *) *p;
5945 if (bfd_get_section (&q->symbol) != section)
5948 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5953 filename = bfd_asymbol_name (&q->symbol);
5957 if (q->symbol.section == section
5958 && q->symbol.value >= low_func
5959 && q->symbol.value <= offset)
5961 func = (asymbol *) q;
5962 low_func = q->symbol.value;
5972 *filename_ptr = filename;
5973 if (functionname_ptr)
5974 *functionname_ptr = bfd_asymbol_name (func);
5979 /* Find the nearest line to a particular section and offset,
5980 for error reporting. */
5983 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5984 filename_ptr, functionname_ptr, line_ptr)
5989 const char **filename_ptr;
5990 const char **functionname_ptr;
5991 unsigned int *line_ptr;
5995 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5996 filename_ptr, functionname_ptr,
5999 if (!*functionname_ptr)
6000 elf_find_function (abfd, section, symbols, offset,
6001 *filename_ptr ? NULL : filename_ptr,
6007 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6008 filename_ptr, functionname_ptr,
6010 &elf_tdata (abfd)->dwarf2_find_line_info))
6012 if (!*functionname_ptr)
6013 elf_find_function (abfd, section, symbols, offset,
6014 *filename_ptr ? NULL : filename_ptr,
6020 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6021 &found, filename_ptr,
6022 functionname_ptr, line_ptr,
6023 &elf_tdata (abfd)->line_info))
6025 if (found && (*functionname_ptr || *line_ptr))
6028 if (symbols == NULL)
6031 if (! elf_find_function (abfd, section, symbols, offset,
6032 filename_ptr, functionname_ptr))
6040 _bfd_elf_sizeof_headers (abfd, reloc)
6046 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6048 ret += get_program_header_size (abfd);
6053 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
6058 bfd_size_type count;
6060 Elf_Internal_Shdr *hdr;
6063 if (! abfd->output_has_begun
6064 && ! (_bfd_elf_compute_section_file_positions
6065 (abfd, (struct bfd_link_info *) NULL)))
6068 hdr = &elf_section_data (section)->this_hdr;
6069 pos = hdr->sh_offset + offset;
6070 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6071 || bfd_bwrite (location, count, abfd) != count)
6078 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
6079 bfd *abfd ATTRIBUTE_UNUSED;
6080 arelent *cache_ptr ATTRIBUTE_UNUSED;
6081 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
6088 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
6091 Elf_Internal_Rel *dst;
6097 /* Try to convert a non-ELF reloc into an ELF one. */
6100 _bfd_elf_validate_reloc (abfd, areloc)
6104 /* Check whether we really have an ELF howto. */
6106 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6108 bfd_reloc_code_real_type code;
6109 reloc_howto_type *howto;
6111 /* Alien reloc: Try to determine its type to replace it with an
6112 equivalent ELF reloc. */
6114 if (areloc->howto->pc_relative)
6116 switch (areloc->howto->bitsize)
6119 code = BFD_RELOC_8_PCREL;
6122 code = BFD_RELOC_12_PCREL;
6125 code = BFD_RELOC_16_PCREL;
6128 code = BFD_RELOC_24_PCREL;
6131 code = BFD_RELOC_32_PCREL;
6134 code = BFD_RELOC_64_PCREL;
6140 howto = bfd_reloc_type_lookup (abfd, code);
6142 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6144 if (howto->pcrel_offset)
6145 areloc->addend += areloc->address;
6147 areloc->addend -= areloc->address; /* addend is unsigned!! */
6152 switch (areloc->howto->bitsize)
6158 code = BFD_RELOC_14;
6161 code = BFD_RELOC_16;
6164 code = BFD_RELOC_26;
6167 code = BFD_RELOC_32;
6170 code = BFD_RELOC_64;
6176 howto = bfd_reloc_type_lookup (abfd, code);
6180 areloc->howto = howto;
6188 (*_bfd_error_handler)
6189 (_("%s: unsupported relocation type %s"),
6190 bfd_archive_filename (abfd), areloc->howto->name);
6191 bfd_set_error (bfd_error_bad_value);
6196 _bfd_elf_close_and_cleanup (abfd)
6199 if (bfd_get_format (abfd) == bfd_object)
6201 if (elf_shstrtab (abfd) != NULL)
6202 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6205 return _bfd_generic_close_and_cleanup (abfd);
6208 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6209 in the relocation's offset. Thus we cannot allow any sort of sanity
6210 range-checking to interfere. There is nothing else to do in processing
6213 bfd_reloc_status_type
6214 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
6215 bfd *abfd ATTRIBUTE_UNUSED;
6216 arelent *re ATTRIBUTE_UNUSED;
6217 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6218 PTR data ATTRIBUTE_UNUSED;
6219 asection *is ATTRIBUTE_UNUSED;
6220 bfd *obfd ATTRIBUTE_UNUSED;
6221 char **errmsg ATTRIBUTE_UNUSED;
6223 return bfd_reloc_ok;
6226 /* Elf core file support. Much of this only works on native
6227 toolchains, since we rely on knowing the
6228 machine-dependent procfs structure in order to pick
6229 out details about the corefile. */
6231 #ifdef HAVE_SYS_PROCFS_H
6232 # include <sys/procfs.h>
6235 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6238 elfcore_make_pid (abfd)
6241 return ((elf_tdata (abfd)->core_lwpid << 16)
6242 + (elf_tdata (abfd)->core_pid));
6245 /* If there isn't a section called NAME, make one, using
6246 data from SECT. Note, this function will generate a
6247 reference to NAME, so you shouldn't deallocate or
6251 elfcore_maybe_make_sect (abfd, name, sect)
6258 if (bfd_get_section_by_name (abfd, name) != NULL)
6261 sect2 = bfd_make_section (abfd, name);
6265 sect2->_raw_size = sect->_raw_size;
6266 sect2->filepos = sect->filepos;
6267 sect2->flags = sect->flags;
6268 sect2->alignment_power = sect->alignment_power;
6272 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6273 actually creates up to two pseudosections:
6274 - For the single-threaded case, a section named NAME, unless
6275 such a section already exists.
6276 - For the multi-threaded case, a section named "NAME/PID", where
6277 PID is elfcore_make_pid (abfd).
6278 Both pseudosections have identical contents. */
6280 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6287 char *threaded_name;
6291 /* Build the section name. */
6293 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6294 len = strlen (buf) + 1;
6295 threaded_name = bfd_alloc (abfd, (bfd_size_type) len);
6296 if (threaded_name == NULL)
6298 memcpy (threaded_name, buf, len);
6300 sect = bfd_make_section (abfd, threaded_name);
6303 sect->_raw_size = size;
6304 sect->filepos = filepos;
6305 sect->flags = SEC_HAS_CONTENTS;
6306 sect->alignment_power = 2;
6308 return elfcore_maybe_make_sect (abfd, name, sect);
6311 /* prstatus_t exists on:
6313 linux 2.[01] + glibc
6317 #if defined (HAVE_PRSTATUS_T)
6318 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
6321 elfcore_grok_prstatus (abfd, note)
6323 Elf_Internal_Note *note;
6328 if (note->descsz == sizeof (prstatus_t))
6332 raw_size = sizeof (prstat.pr_reg);
6333 offset = offsetof (prstatus_t, pr_reg);
6334 memcpy (&prstat, note->descdata, sizeof (prstat));
6336 /* Do not overwrite the core signal if it
6337 has already been set by another thread. */
6338 if (elf_tdata (abfd)->core_signal == 0)
6339 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6340 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6342 /* pr_who exists on:
6345 pr_who doesn't exist on:
6348 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6349 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6352 #if defined (HAVE_PRSTATUS32_T)
6353 else if (note->descsz == sizeof (prstatus32_t))
6355 /* 64-bit host, 32-bit corefile */
6356 prstatus32_t prstat;
6358 raw_size = sizeof (prstat.pr_reg);
6359 offset = offsetof (prstatus32_t, pr_reg);
6360 memcpy (&prstat, note->descdata, sizeof (prstat));
6362 /* Do not overwrite the core signal if it
6363 has already been set by another thread. */
6364 if (elf_tdata (abfd)->core_signal == 0)
6365 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6366 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6368 /* pr_who exists on:
6371 pr_who doesn't exist on:
6374 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6375 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6378 #endif /* HAVE_PRSTATUS32_T */
6381 /* Fail - we don't know how to handle any other
6382 note size (ie. data object type). */
6386 /* Make a ".reg/999" section and a ".reg" section. */
6387 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6388 raw_size, note->descpos + offset);
6390 #endif /* defined (HAVE_PRSTATUS_T) */
6392 /* Create a pseudosection containing the exact contents of NOTE. */
6394 elfcore_make_note_pseudosection (abfd, name, note)
6397 Elf_Internal_Note *note;
6399 return _bfd_elfcore_make_pseudosection (abfd, name,
6400 note->descsz, note->descpos);
6403 /* There isn't a consistent prfpregset_t across platforms,
6404 but it doesn't matter, because we don't have to pick this
6405 data structure apart. */
6408 elfcore_grok_prfpreg (abfd, note)
6410 Elf_Internal_Note *note;
6412 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6415 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6416 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6420 elfcore_grok_prxfpreg (abfd, note)
6422 Elf_Internal_Note *note;
6424 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6427 #if defined (HAVE_PRPSINFO_T)
6428 typedef prpsinfo_t elfcore_psinfo_t;
6429 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6430 typedef prpsinfo32_t elfcore_psinfo32_t;
6434 #if defined (HAVE_PSINFO_T)
6435 typedef psinfo_t elfcore_psinfo_t;
6436 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6437 typedef psinfo32_t elfcore_psinfo32_t;
6441 /* return a malloc'ed copy of a string at START which is at
6442 most MAX bytes long, possibly without a terminating '\0'.
6443 the copy will always have a terminating '\0'. */
6446 _bfd_elfcore_strndup (abfd, start, max)
6452 char *end = memchr (start, '\0', max);
6460 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6464 memcpy (dups, start, len);
6470 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6471 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
6474 elfcore_grok_psinfo (abfd, note)
6476 Elf_Internal_Note *note;
6478 if (note->descsz == sizeof (elfcore_psinfo_t))
6480 elfcore_psinfo_t psinfo;
6482 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6484 elf_tdata (abfd)->core_program
6485 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6486 sizeof (psinfo.pr_fname));
6488 elf_tdata (abfd)->core_command
6489 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6490 sizeof (psinfo.pr_psargs));
6492 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6493 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6495 /* 64-bit host, 32-bit corefile */
6496 elfcore_psinfo32_t psinfo;
6498 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6500 elf_tdata (abfd)->core_program
6501 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6502 sizeof (psinfo.pr_fname));
6504 elf_tdata (abfd)->core_command
6505 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6506 sizeof (psinfo.pr_psargs));
6512 /* Fail - we don't know how to handle any other
6513 note size (ie. data object type). */
6517 /* Note that for some reason, a spurious space is tacked
6518 onto the end of the args in some (at least one anyway)
6519 implementations, so strip it off if it exists. */
6522 char *command = elf_tdata (abfd)->core_command;
6523 int n = strlen (command);
6525 if (0 < n && command[n - 1] == ' ')
6526 command[n - 1] = '\0';
6531 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6533 #if defined (HAVE_PSTATUS_T)
6534 static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *));
6537 elfcore_grok_pstatus (abfd, note)
6539 Elf_Internal_Note *note;
6541 if (note->descsz == sizeof (pstatus_t)
6542 #if defined (HAVE_PXSTATUS_T)
6543 || note->descsz == sizeof (pxstatus_t)
6549 memcpy (&pstat, note->descdata, sizeof (pstat));
6551 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6553 #if defined (HAVE_PSTATUS32_T)
6554 else if (note->descsz == sizeof (pstatus32_t))
6556 /* 64-bit host, 32-bit corefile */
6559 memcpy (&pstat, note->descdata, sizeof (pstat));
6561 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6564 /* Could grab some more details from the "representative"
6565 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6566 NT_LWPSTATUS note, presumably. */
6570 #endif /* defined (HAVE_PSTATUS_T) */
6572 #if defined (HAVE_LWPSTATUS_T)
6573 static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *));
6576 elfcore_grok_lwpstatus (abfd, note)
6578 Elf_Internal_Note *note;
6580 lwpstatus_t lwpstat;
6586 if (note->descsz != sizeof (lwpstat)
6587 #if defined (HAVE_LWPXSTATUS_T)
6588 && note->descsz != sizeof (lwpxstatus_t)
6593 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6595 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6596 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6598 /* Make a ".reg/999" section. */
6600 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6601 len = strlen (buf) + 1;
6602 name = bfd_alloc (abfd, (bfd_size_type) len);
6605 memcpy (name, buf, len);
6607 sect = bfd_make_section (abfd, name);
6611 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6612 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6613 sect->filepos = note->descpos
6614 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6617 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6618 sect->_raw_size = sizeof (lwpstat.pr_reg);
6619 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6622 sect->flags = SEC_HAS_CONTENTS;
6623 sect->alignment_power = 2;
6625 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6628 /* Make a ".reg2/999" section */
6630 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6631 len = strlen (buf) + 1;
6632 name = bfd_alloc (abfd, (bfd_size_type) len);
6635 memcpy (name, buf, len);
6637 sect = bfd_make_section (abfd, name);
6641 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6642 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6643 sect->filepos = note->descpos
6644 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6647 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6648 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6649 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6652 sect->flags = SEC_HAS_CONTENTS;
6653 sect->alignment_power = 2;
6655 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6657 #endif /* defined (HAVE_LWPSTATUS_T) */
6659 #if defined (HAVE_WIN32_PSTATUS_T)
6661 elfcore_grok_win32pstatus (abfd, note)
6663 Elf_Internal_Note *note;
6669 win32_pstatus_t pstatus;
6671 if (note->descsz < sizeof (pstatus))
6674 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6676 switch (pstatus.data_type)
6678 case NOTE_INFO_PROCESS:
6679 /* FIXME: need to add ->core_command. */
6680 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6681 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6684 case NOTE_INFO_THREAD:
6685 /* Make a ".reg/999" section. */
6686 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6688 len = strlen (buf) + 1;
6689 name = bfd_alloc (abfd, (bfd_size_type) len);
6693 memcpy (name, buf, len);
6695 sect = bfd_make_section (abfd, name);
6699 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6700 sect->filepos = (note->descpos
6701 + offsetof (struct win32_pstatus,
6702 data.thread_info.thread_context));
6703 sect->flags = SEC_HAS_CONTENTS;
6704 sect->alignment_power = 2;
6706 if (pstatus.data.thread_info.is_active_thread)
6707 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6711 case NOTE_INFO_MODULE:
6712 /* Make a ".module/xxxxxxxx" section. */
6713 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6715 len = strlen (buf) + 1;
6716 name = bfd_alloc (abfd, (bfd_size_type) len);
6720 memcpy (name, buf, len);
6722 sect = bfd_make_section (abfd, name);
6727 sect->_raw_size = note->descsz;
6728 sect->filepos = note->descpos;
6729 sect->flags = SEC_HAS_CONTENTS;
6730 sect->alignment_power = 2;
6739 #endif /* HAVE_WIN32_PSTATUS_T */
6742 elfcore_grok_note (abfd, note)
6744 Elf_Internal_Note *note;
6746 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6754 if (bed->elf_backend_grok_prstatus)
6755 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6757 #if defined (HAVE_PRSTATUS_T)
6758 return elfcore_grok_prstatus (abfd, note);
6763 #if defined (HAVE_PSTATUS_T)
6765 return elfcore_grok_pstatus (abfd, note);
6768 #if defined (HAVE_LWPSTATUS_T)
6770 return elfcore_grok_lwpstatus (abfd, note);
6773 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6774 return elfcore_grok_prfpreg (abfd, note);
6776 #if defined (HAVE_WIN32_PSTATUS_T)
6777 case NT_WIN32PSTATUS:
6778 return elfcore_grok_win32pstatus (abfd, note);
6781 case NT_PRXFPREG: /* Linux SSE extension */
6782 if (note->namesz == 5
6783 && ! strcmp (note->namedata, "LINUX"))
6784 return elfcore_grok_prxfpreg (abfd, note);
6790 if (bed->elf_backend_grok_psinfo)
6791 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6793 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6794 return elfcore_grok_psinfo (abfd, note);
6802 elfcore_netbsd_get_lwpid (note, lwpidp)
6803 Elf_Internal_Note *note;
6808 cp = strchr (note->namedata, '@');
6811 *lwpidp = atoi(cp + 1);
6818 elfcore_grok_netbsd_procinfo (abfd, note)
6820 Elf_Internal_Note *note;
6823 /* Signal number at offset 0x08. */
6824 elf_tdata (abfd)->core_signal
6825 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6827 /* Process ID at offset 0x50. */
6828 elf_tdata (abfd)->core_pid
6829 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6831 /* Command name at 0x7c (max 32 bytes, including nul). */
6832 elf_tdata (abfd)->core_command
6833 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6839 elfcore_grok_netbsd_note (abfd, note)
6841 Elf_Internal_Note *note;
6845 if (elfcore_netbsd_get_lwpid (note, &lwp))
6846 elf_tdata (abfd)->core_lwpid = lwp;
6848 if (note->type == NT_NETBSDCORE_PROCINFO)
6850 /* NetBSD-specific core "procinfo". Note that we expect to
6851 find this note before any of the others, which is fine,
6852 since the kernel writes this note out first when it
6853 creates a core file. */
6855 return elfcore_grok_netbsd_procinfo (abfd, note);
6858 /* As of Jan 2002 there are no other machine-independent notes
6859 defined for NetBSD core files. If the note type is less
6860 than the start of the machine-dependent note types, we don't
6863 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6867 switch (bfd_get_arch (abfd))
6869 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6870 PT_GETFPREGS == mach+2. */
6872 case bfd_arch_alpha:
6873 case bfd_arch_sparc:
6876 case NT_NETBSDCORE_FIRSTMACH+0:
6877 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6879 case NT_NETBSDCORE_FIRSTMACH+2:
6880 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6886 /* On all other arch's, PT_GETREGS == mach+1 and
6887 PT_GETFPREGS == mach+3. */
6892 case NT_NETBSDCORE_FIRSTMACH+1:
6893 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6895 case NT_NETBSDCORE_FIRSTMACH+3:
6896 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6905 /* Function: elfcore_write_note
6912 size of data for note
6915 End of buffer containing note. */
6918 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6927 Elf_External_Note *xnp;
6937 struct elf_backend_data *bed;
6939 namesz = strlen (name) + 1;
6940 bed = get_elf_backend_data (abfd);
6941 pad = -namesz & (bed->s->file_align - 1);
6944 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
6946 p = realloc (buf, *bufsiz + newspace);
6948 *bufsiz += newspace;
6949 xnp = (Elf_External_Note *) dest;
6950 H_PUT_32 (abfd, namesz, xnp->namesz);
6951 H_PUT_32 (abfd, size, xnp->descsz);
6952 H_PUT_32 (abfd, type, xnp->type);
6956 memcpy (dest, name, namesz);
6964 memcpy (dest, input, size);
6968 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6970 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6978 char *note_name = "CORE";
6980 #if defined (HAVE_PSINFO_T)
6982 note_type = NT_PSINFO;
6985 note_type = NT_PRPSINFO;
6988 memset (&data, 0, sizeof (data));
6989 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
6990 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
6991 return elfcore_write_note (abfd, buf, bufsiz,
6992 note_name, note_type, &data, sizeof (data));
6994 #endif /* PSINFO_T or PRPSINFO_T */
6996 #if defined (HAVE_PRSTATUS_T)
6998 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7007 char *note_name = "CORE";
7009 memset (&prstat, 0, sizeof (prstat));
7010 prstat.pr_pid = pid;
7011 prstat.pr_cursig = cursig;
7012 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7013 return elfcore_write_note (abfd, buf, bufsiz,
7014 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7016 #endif /* HAVE_PRSTATUS_T */
7018 #if defined (HAVE_LWPSTATUS_T)
7020 elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7028 lwpstatus_t lwpstat;
7029 char *note_name = "CORE";
7031 memset (&lwpstat, 0, sizeof (lwpstat));
7032 lwpstat.pr_lwpid = pid >> 16;
7033 lwpstat.pr_cursig = cursig;
7034 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7035 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7036 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7038 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7039 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7041 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7042 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7045 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7046 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7048 #endif /* HAVE_LWPSTATUS_T */
7050 #if defined (HAVE_PSTATUS_T)
7052 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7061 char *note_name = "CORE";
7063 memset (&pstat, 0, sizeof (pstat));
7064 pstat.pr_pid = pid & 0xffff;
7065 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7066 NT_PSTATUS, &pstat, sizeof (pstat));
7069 #endif /* HAVE_PSTATUS_T */
7072 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
7079 char *note_name = "CORE";
7080 return elfcore_write_note (abfd, buf, bufsiz,
7081 note_name, NT_FPREGSET, fpregs, size);
7085 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
7092 char *note_name = "LINUX";
7093 return elfcore_write_note (abfd, buf, bufsiz,
7094 note_name, NT_PRXFPREG, xfpregs, size);
7098 elfcore_read_notes (abfd, offset, size)
7109 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7112 buf = bfd_malloc (size);
7116 if (bfd_bread (buf, size, abfd) != size)
7124 while (p < buf + size)
7126 /* FIXME: bad alignment assumption. */
7127 Elf_External_Note *xnp = (Elf_External_Note *) p;
7128 Elf_Internal_Note in;
7130 in.type = H_GET_32 (abfd, xnp->type);
7132 in.namesz = H_GET_32 (abfd, xnp->namesz);
7133 in.namedata = xnp->name;
7135 in.descsz = H_GET_32 (abfd, xnp->descsz);
7136 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7137 in.descpos = offset + (in.descdata - buf);
7139 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7141 if (! elfcore_grok_netbsd_note (abfd, &in))
7146 if (! elfcore_grok_note (abfd, &in))
7150 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7157 /* Providing external access to the ELF program header table. */
7159 /* Return an upper bound on the number of bytes required to store a
7160 copy of ABFD's program header table entries. Return -1 if an error
7161 occurs; bfd_get_error will return an appropriate code. */
7164 bfd_get_elf_phdr_upper_bound (abfd)
7167 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7169 bfd_set_error (bfd_error_wrong_format);
7173 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7176 /* Copy ABFD's program header table entries to *PHDRS. The entries
7177 will be stored as an array of Elf_Internal_Phdr structures, as
7178 defined in include/elf/internal.h. To find out how large the
7179 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7181 Return the number of program header table entries read, or -1 if an
7182 error occurs; bfd_get_error will return an appropriate code. */
7185 bfd_get_elf_phdrs (abfd, phdrs)
7191 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7193 bfd_set_error (bfd_error_wrong_format);
7197 num_phdrs = elf_elfheader (abfd)->e_phnum;
7198 memcpy (phdrs, elf_tdata (abfd)->phdr,
7199 num_phdrs * sizeof (Elf_Internal_Phdr));
7205 _bfd_elf_sprintf_vma (abfd, buf, value)
7206 bfd *abfd ATTRIBUTE_UNUSED;
7211 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7213 i_ehdrp = elf_elfheader (abfd);
7214 if (i_ehdrp == NULL)
7215 sprintf_vma (buf, value);
7218 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7220 #if BFD_HOST_64BIT_LONG
7221 sprintf (buf, "%016lx", value);
7223 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7224 _bfd_int64_low (value));
7228 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7231 sprintf_vma (buf, value);
7236 _bfd_elf_fprintf_vma (abfd, stream, value)
7237 bfd *abfd ATTRIBUTE_UNUSED;
7242 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7244 i_ehdrp = elf_elfheader (abfd);
7245 if (i_ehdrp == NULL)
7246 fprintf_vma ((FILE *) stream, value);
7249 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7251 #if BFD_HOST_64BIT_LONG
7252 fprintf ((FILE *) stream, "%016lx", value);
7254 fprintf ((FILE *) stream, "%08lx%08lx",
7255 _bfd_int64_high (value), _bfd_int64_low (value));
7259 fprintf ((FILE *) stream, "%08lx",
7260 (unsigned long) (value & 0xffffffff));
7263 fprintf_vma ((FILE *) stream, value);
7267 enum elf_reloc_type_class
7268 _bfd_elf_reloc_type_class (rela)
7269 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
7271 return reloc_class_normal;
7274 /* For RELA architectures, return the relocation value for a
7275 relocation against a local symbol. */
7278 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7280 Elf_Internal_Sym *sym;
7282 Elf_Internal_Rela *rel;
7286 relocation = (sec->output_section->vma
7287 + sec->output_offset
7289 if ((sec->flags & SEC_MERGE)
7290 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7291 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
7297 _bfd_merged_section_offset (abfd, &msec,
7298 elf_section_data (sec)->sec_info,
7299 sym->st_value + rel->r_addend,
7302 rel->r_addend += msec->output_section->vma + msec->output_offset;
7308 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7310 Elf_Internal_Sym *sym;
7314 asection *sec = *psec;
7316 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
7317 return sym->st_value + addend;
7319 return _bfd_merged_section_offset (abfd, psec,
7320 elf_section_data (sec)->sec_info,
7321 sym->st_value + addend, (bfd_vma) 0);
7325 _bfd_elf_section_offset (abfd, info, sec, offset)
7327 struct bfd_link_info *info;
7331 struct bfd_elf_section_data *sec_data;
7333 sec_data = elf_section_data (sec);
7334 switch (sec_data->sec_info_type)
7336 case ELF_INFO_TYPE_STABS:
7337 return _bfd_stab_section_offset
7338 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
7340 case ELF_INFO_TYPE_EH_FRAME:
7341 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);