1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
41 static INLINE struct elf_segment_map *make_mapping
42 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
43 static boolean map_sections_to_segments PARAMS ((bfd *));
44 static int elf_sort_sections PARAMS ((const PTR, const PTR));
45 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
46 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
47 static boolean prep_headers PARAMS ((bfd *));
48 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **));
49 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
50 static char *elf_read PARAMS ((bfd *, long, unsigned int));
51 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
52 static boolean assign_section_numbers PARAMS ((bfd *));
53 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
54 static boolean elf_map_symbols PARAMS ((bfd *));
55 static bfd_size_type get_program_header_size PARAMS ((bfd *));
57 /* Swap version information in and out. The version information is
58 currently size independent. If that ever changes, this code will
59 need to move into elfcode.h. */
61 /* Swap in a Verdef structure. */
64 _bfd_elf_swap_verdef_in (abfd, src, dst)
66 const Elf_External_Verdef *src;
67 Elf_Internal_Verdef *dst;
69 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
70 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
71 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
73 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
74 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
75 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
78 /* Swap out a Verdef structure. */
81 _bfd_elf_swap_verdef_out (abfd, src, dst)
83 const Elf_Internal_Verdef *src;
84 Elf_External_Verdef *dst;
86 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
87 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
88 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
91 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
92 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (abfd, src, dst)
100 const Elf_External_Verdaux *src;
101 Elf_Internal_Verdaux *dst;
103 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
104 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (abfd, src, dst)
112 const Elf_Internal_Verdaux *src;
113 Elf_External_Verdaux *dst;
115 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
116 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (abfd, src, dst)
124 const Elf_External_Verneed *src;
125 Elf_Internal_Verneed *dst;
127 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
128 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
129 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
130 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
131 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
134 /* Swap out a Verneed structure. */
137 _bfd_elf_swap_verneed_out (abfd, src, dst)
139 const Elf_Internal_Verneed *src;
140 Elf_External_Verneed *dst;
142 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
143 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
144 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
145 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
146 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
149 /* Swap in a Vernaux structure. */
152 _bfd_elf_swap_vernaux_in (abfd, src, dst)
154 const Elf_External_Vernaux *src;
155 Elf_Internal_Vernaux *dst;
157 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
158 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
159 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
160 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
161 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
164 /* Swap out a Vernaux structure. */
167 _bfd_elf_swap_vernaux_out (abfd, src, dst)
169 const Elf_Internal_Vernaux *src;
170 Elf_External_Vernaux *dst;
172 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
173 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
174 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
175 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
176 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
179 /* Swap in a Versym structure. */
182 _bfd_elf_swap_versym_in (abfd, src, dst)
184 const Elf_External_Versym *src;
185 Elf_Internal_Versym *dst;
187 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
190 /* Swap out a Versym structure. */
193 _bfd_elf_swap_versym_out (abfd, src, dst)
195 const Elf_Internal_Versym *src;
196 Elf_External_Versym *dst;
198 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
201 /* Standard ELF hash function. Do not change this function; you will
202 cause invalid hash tables to be generated. (Well, you would if this
203 were being used yet.) */
206 CONST unsigned char *name;
212 while ((ch = *name++) != '\0')
215 if ((g = (h & 0xf0000000)) != 0)
224 /* Read a specified number of bytes at a specified offset in an ELF
225 file, into a newly allocated buffer, and return a pointer to the
229 elf_read (abfd, offset, size)
236 if ((buf = bfd_alloc (abfd, size)) == NULL)
238 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
240 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
242 if (bfd_get_error () != bfd_error_system_call)
243 bfd_set_error (bfd_error_file_truncated);
250 bfd_elf_mkobject (abfd)
253 /* this just does initialization */
254 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
255 elf_tdata (abfd) = (struct elf_obj_tdata *)
256 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
257 if (elf_tdata (abfd) == 0)
259 /* since everything is done at close time, do we need any
266 bfd_elf_get_str_section (abfd, shindex)
268 unsigned int shindex;
270 Elf_Internal_Shdr **i_shdrp;
271 char *shstrtab = NULL;
273 unsigned int shstrtabsize;
275 i_shdrp = elf_elfsections (abfd);
276 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
279 shstrtab = (char *) i_shdrp[shindex]->contents;
280 if (shstrtab == NULL)
282 /* No cached one, attempt to read, and cache what we read. */
283 offset = i_shdrp[shindex]->sh_offset;
284 shstrtabsize = i_shdrp[shindex]->sh_size;
285 shstrtab = elf_read (abfd, offset, shstrtabsize);
286 i_shdrp[shindex]->contents = (PTR) shstrtab;
292 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
294 unsigned int shindex;
295 unsigned int strindex;
297 Elf_Internal_Shdr *hdr;
302 hdr = elf_elfsections (abfd)[shindex];
304 if (hdr->contents == NULL
305 && bfd_elf_get_str_section (abfd, shindex) == NULL)
308 if (strindex >= hdr->sh_size)
310 (*_bfd_error_handler)
311 ("%s: invalid string offset %u >= %lu for section `%s'",
312 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
313 ((shindex == elf_elfheader(abfd)->e_shstrndx
314 && strindex == hdr->sh_name)
316 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
320 return ((char *) hdr->contents) + strindex;
323 /* Make a BFD section from an ELF section. We store a pointer to the
324 BFD section in the bfd_section field of the header. */
327 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
329 Elf_Internal_Shdr *hdr;
335 if (hdr->bfd_section != NULL)
337 BFD_ASSERT (strcmp (name,
338 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
342 newsect = bfd_make_section_anyway (abfd, name);
346 newsect->filepos = hdr->sh_offset;
348 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
349 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
350 || ! bfd_set_section_alignment (abfd, newsect,
351 bfd_log2 (hdr->sh_addralign)))
354 flags = SEC_NO_FLAGS;
355 if (hdr->sh_type != SHT_NOBITS)
356 flags |= SEC_HAS_CONTENTS;
357 if ((hdr->sh_flags & SHF_ALLOC) != 0)
360 if (hdr->sh_type != SHT_NOBITS)
363 if ((hdr->sh_flags & SHF_WRITE) == 0)
364 flags |= SEC_READONLY;
365 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
367 else if ((flags & SEC_LOAD) != 0)
370 /* The debugging sections appear to be recognized only by name, not
372 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
373 || strncmp (name, ".line", sizeof ".line" - 1) == 0
374 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
375 flags |= SEC_DEBUGGING;
377 /* As a GNU extension, if the name begins with .gnu.linkonce, we
378 only link a single copy of the section. This is used to support
379 g++. g++ will emit each template expansion in its own section.
380 The symbols will be defined as weak, so that multiple definitions
381 are permitted. The GNU linker extension is to actually discard
382 all but one of the sections. */
383 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
384 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
386 if (! bfd_set_section_flags (abfd, newsect, flags))
389 if ((flags & SEC_ALLOC) != 0)
391 Elf_Internal_Phdr *phdr;
394 /* Look through the phdrs to see if we need to adjust the lma. */
395 phdr = elf_tdata (abfd)->phdr;
396 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
398 if (phdr->p_type == PT_LOAD
399 && phdr->p_paddr != 0
400 && phdr->p_vaddr != phdr->p_paddr
401 && phdr->p_vaddr <= hdr->sh_addr
402 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size
403 && ((flags & SEC_LOAD) == 0
404 || (phdr->p_offset <= hdr->sh_offset
405 && (phdr->p_offset + phdr->p_filesz
406 >= hdr->sh_offset + hdr->sh_size))))
408 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
414 hdr->bfd_section = newsect;
415 elf_section_data (newsect)->this_hdr = *hdr;
425 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
428 Helper functions for GDB to locate the string tables.
429 Since BFD hides string tables from callers, GDB needs to use an
430 internal hook to find them. Sun's .stabstr, in particular,
431 isn't even pointed to by the .stab section, so ordinary
432 mechanisms wouldn't work to find it, even if we had some.
435 struct elf_internal_shdr *
436 bfd_elf_find_section (abfd, name)
440 Elf_Internal_Shdr **i_shdrp;
445 i_shdrp = elf_elfsections (abfd);
448 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
449 if (shstrtab != NULL)
451 max = elf_elfheader (abfd)->e_shnum;
452 for (i = 1; i < max; i++)
453 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
460 const char *const bfd_elf_section_type_names[] = {
461 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
462 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
463 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
466 /* ELF relocs are against symbols. If we are producing relocateable
467 output, and the reloc is against an external symbol, and nothing
468 has given us any additional addend, the resulting reloc will also
469 be against the same symbol. In such a case, we don't want to
470 change anything about the way the reloc is handled, since it will
471 all be done at final link time. Rather than put special case code
472 into bfd_perform_relocation, all the reloc types use this howto
473 function. It just short circuits the reloc if producing
474 relocateable output against an external symbol. */
477 bfd_reloc_status_type
478 bfd_elf_generic_reloc (abfd,
486 arelent *reloc_entry;
489 asection *input_section;
491 char **error_message;
493 if (output_bfd != (bfd *) NULL
494 && (symbol->flags & BSF_SECTION_SYM) == 0
495 && (! reloc_entry->howto->partial_inplace
496 || reloc_entry->addend == 0))
498 reloc_entry->address += input_section->output_offset;
502 return bfd_reloc_continue;
505 /* Print out the program headers. */
508 _bfd_elf_print_private_bfd_data (abfd, farg)
512 FILE *f = (FILE *) farg;
513 Elf_Internal_Phdr *p;
515 bfd_byte *dynbuf = NULL;
517 p = elf_tdata (abfd)->phdr;
522 fprintf (f, "\nProgram Header:\n");
523 c = elf_elfheader (abfd)->e_phnum;
524 for (i = 0; i < c; i++, p++)
531 case PT_NULL: s = "NULL"; break;
532 case PT_LOAD: s = "LOAD"; break;
533 case PT_DYNAMIC: s = "DYNAMIC"; break;
534 case PT_INTERP: s = "INTERP"; break;
535 case PT_NOTE: s = "NOTE"; break;
536 case PT_SHLIB: s = "SHLIB"; break;
537 case PT_PHDR: s = "PHDR"; break;
538 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
540 fprintf (f, "%8s off 0x", s);
541 fprintf_vma (f, p->p_offset);
542 fprintf (f, " vaddr 0x");
543 fprintf_vma (f, p->p_vaddr);
544 fprintf (f, " paddr 0x");
545 fprintf_vma (f, p->p_paddr);
546 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
547 fprintf (f, " filesz 0x");
548 fprintf_vma (f, p->p_filesz);
549 fprintf (f, " memsz 0x");
550 fprintf_vma (f, p->p_memsz);
551 fprintf (f, " flags %c%c%c",
552 (p->p_flags & PF_R) != 0 ? 'r' : '-',
553 (p->p_flags & PF_W) != 0 ? 'w' : '-',
554 (p->p_flags & PF_X) != 0 ? 'x' : '-');
555 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
556 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
561 s = bfd_get_section_by_name (abfd, ".dynamic");
566 bfd_byte *extdyn, *extdynend;
568 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
570 fprintf (f, "\nDynamic Section:\n");
572 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
575 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
579 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
582 link = elf_elfsections (abfd)[elfsec]->sh_link;
584 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
585 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
588 extdynend = extdyn + s->_raw_size;
589 for (; extdyn < extdynend; extdyn += extdynsize)
591 Elf_Internal_Dyn dyn;
596 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
598 if (dyn.d_tag == DT_NULL)
605 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
609 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
610 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
611 case DT_PLTGOT: name = "PLTGOT"; break;
612 case DT_HASH: name = "HASH"; break;
613 case DT_STRTAB: name = "STRTAB"; break;
614 case DT_SYMTAB: name = "SYMTAB"; break;
615 case DT_RELA: name = "RELA"; break;
616 case DT_RELASZ: name = "RELASZ"; break;
617 case DT_RELAENT: name = "RELAENT"; break;
618 case DT_STRSZ: name = "STRSZ"; break;
619 case DT_SYMENT: name = "SYMENT"; break;
620 case DT_INIT: name = "INIT"; break;
621 case DT_FINI: name = "FINI"; break;
622 case DT_SONAME: name = "SONAME"; stringp = true; break;
623 case DT_RPATH: name = "RPATH"; stringp = true; break;
624 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
625 case DT_REL: name = "REL"; break;
626 case DT_RELSZ: name = "RELSZ"; break;
627 case DT_RELENT: name = "RELENT"; break;
628 case DT_PLTREL: name = "PLTREL"; break;
629 case DT_DEBUG: name = "DEBUG"; break;
630 case DT_TEXTREL: name = "TEXTREL"; break;
631 case DT_JMPREL: name = "JMPREL"; break;
632 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
633 case DT_FILTER: name = "FILTER"; stringp = true; break;
634 case DT_VERSYM: name = "VERSYM"; break;
635 case DT_VERDEF: name = "VERDEF"; break;
636 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
637 case DT_VERNEED: name = "VERNEED"; break;
638 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
641 fprintf (f, " %-11s ", name);
643 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
648 string = bfd_elf_string_from_elf_section (abfd, link,
652 fprintf (f, "%s", string);
661 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
662 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
664 if (! _bfd_elf_slurp_version_tables (abfd))
668 if (elf_dynverdef (abfd) != 0)
670 Elf_Internal_Verdef *t;
672 fprintf (f, "\nVersion definitions:\n");
673 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
675 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
676 t->vd_flags, t->vd_hash, t->vd_nodename);
677 if (t->vd_auxptr->vda_nextptr != NULL)
679 Elf_Internal_Verdaux *a;
682 for (a = t->vd_auxptr->vda_nextptr;
685 fprintf (f, "%s ", a->vda_nodename);
691 if (elf_dynverref (abfd) != 0)
693 Elf_Internal_Verneed *t;
695 fprintf (f, "\nVersion References:\n");
696 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
698 Elf_Internal_Vernaux *a;
700 fprintf (f, " required from %s:\n", t->vn_filename);
701 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
702 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
703 a->vna_flags, a->vna_other, a->vna_nodename);
715 /* Display ELF-specific fields of a symbol. */
718 bfd_elf_print_symbol (abfd, filep, symbol, how)
722 bfd_print_symbol_type how;
724 FILE *file = (FILE *) filep;
727 case bfd_print_symbol_name:
728 fprintf (file, "%s", symbol->name);
730 case bfd_print_symbol_more:
731 fprintf (file, "elf ");
732 fprintf_vma (file, symbol->value);
733 fprintf (file, " %lx", (long) symbol->flags);
735 case bfd_print_symbol_all:
737 CONST char *section_name;
738 section_name = symbol->section ? symbol->section->name : "(*none*)";
739 bfd_print_symbol_vandf ((PTR) file, symbol);
740 fprintf (file, " %s\t", section_name);
741 /* Print the "other" value for a symbol. For common symbols,
742 we've already printed the size; now print the alignment.
743 For other symbols, we have no specified alignment, and
744 we've printed the address; now print the size. */
746 (bfd_is_com_section (symbol->section)
747 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
748 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
750 /* If we have version information, print it. */
751 if (elf_tdata (abfd)->dynversym_section != 0
752 && (elf_tdata (abfd)->dynverdef_section != 0
753 || elf_tdata (abfd)->dynverref_section != 0))
756 const char *version_string;
758 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
762 else if (vernum == 1)
763 version_string = "Base";
764 else if (vernum <= elf_tdata (abfd)->cverdefs)
766 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
769 Elf_Internal_Verneed *t;
772 for (t = elf_tdata (abfd)->verref;
776 Elf_Internal_Vernaux *a;
778 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
780 if (a->vna_other == vernum)
782 version_string = a->vna_nodename;
789 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
790 fprintf (file, " %-11s", version_string);
795 fprintf (file, " (%s)", version_string);
796 for (i = 10 - strlen (version_string); i > 0; --i)
801 /* If the st_other field is not zero, print it. */
802 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
803 fprintf (file, " 0x%02x",
805 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
807 fprintf (file, " %s", symbol->name);
813 /* Create an entry in an ELF linker hash table. */
815 struct bfd_hash_entry *
816 _bfd_elf_link_hash_newfunc (entry, table, string)
817 struct bfd_hash_entry *entry;
818 struct bfd_hash_table *table;
821 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
823 /* Allocate the structure if it has not already been allocated by a
825 if (ret == (struct elf_link_hash_entry *) NULL)
826 ret = ((struct elf_link_hash_entry *)
827 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
828 if (ret == (struct elf_link_hash_entry *) NULL)
829 return (struct bfd_hash_entry *) ret;
831 /* Call the allocation method of the superclass. */
832 ret = ((struct elf_link_hash_entry *)
833 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
835 if (ret != (struct elf_link_hash_entry *) NULL)
837 /* Set local fields. */
841 ret->dynstr_index = 0;
843 ret->got_offset = (bfd_vma) -1;
844 ret->plt_offset = (bfd_vma) -1;
845 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
846 ret->verinfo.verdef = NULL;
847 ret->type = STT_NOTYPE;
849 /* Assume that we have been called by a non-ELF symbol reader.
850 This flag is then reset by the code which reads an ELF input
851 file. This ensures that a symbol created by a non-ELF symbol
852 reader will have the flag set correctly. */
853 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
856 return (struct bfd_hash_entry *) ret;
859 /* Initialize an ELF linker hash table. */
862 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
863 struct elf_link_hash_table *table;
865 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
866 struct bfd_hash_table *,
869 table->dynamic_sections_created = false;
870 table->dynobj = NULL;
871 /* The first dynamic symbol is a dummy. */
872 table->dynsymcount = 1;
873 table->dynstr = NULL;
874 table->bucketcount = 0;
875 table->needed = NULL;
877 table->stab_info = NULL;
878 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
881 /* Create an ELF linker hash table. */
883 struct bfd_link_hash_table *
884 _bfd_elf_link_hash_table_create (abfd)
887 struct elf_link_hash_table *ret;
889 ret = ((struct elf_link_hash_table *)
890 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
891 if (ret == (struct elf_link_hash_table *) NULL)
894 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
896 bfd_release (abfd, ret);
903 /* This is a hook for the ELF emulation code in the generic linker to
904 tell the backend linker what file name to use for the DT_NEEDED
905 entry for a dynamic object. The generic linker passes name as an
906 empty string to indicate that no DT_NEEDED entry should be made. */
909 bfd_elf_set_dt_needed_name (abfd, name)
913 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
914 && bfd_get_format (abfd) == bfd_object)
915 elf_dt_name (abfd) = name;
918 /* Get the list of DT_NEEDED entries for a link. This is a hook for
919 the ELF emulation code. */
921 struct bfd_link_needed_list *
922 bfd_elf_get_needed_list (abfd, info)
924 struct bfd_link_info *info;
926 if (info->hash->creator->flavour != bfd_target_elf_flavour)
928 return elf_hash_table (info)->needed;
931 /* Get the name actually used for a dynamic object for a link. This
932 is the SONAME entry if there is one. Otherwise, it is the string
933 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
936 bfd_elf_get_dt_soname (abfd)
939 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
940 && bfd_get_format (abfd) == bfd_object)
941 return elf_dt_name (abfd);
945 /* Allocate an ELF string table--force the first byte to be zero. */
947 struct bfd_strtab_hash *
948 _bfd_elf_stringtab_init ()
950 struct bfd_strtab_hash *ret;
952 ret = _bfd_stringtab_init ();
957 loc = _bfd_stringtab_add (ret, "", true, false);
958 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
959 if (loc == (bfd_size_type) -1)
961 _bfd_stringtab_free (ret);
968 /* ELF .o/exec file reading */
970 /* Create a new bfd section from an ELF section header. */
973 bfd_section_from_shdr (abfd, shindex)
975 unsigned int shindex;
977 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
978 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
979 struct elf_backend_data *bed = get_elf_backend_data (abfd);
982 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
984 switch (hdr->sh_type)
987 /* Inactive section. Throw it away. */
990 case SHT_PROGBITS: /* Normal section with contents. */
991 case SHT_DYNAMIC: /* Dynamic linking information. */
992 case SHT_NOBITS: /* .bss section. */
993 case SHT_HASH: /* .hash section. */
994 case SHT_NOTE: /* .note section. */
995 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
997 case SHT_SYMTAB: /* A symbol table */
998 if (elf_onesymtab (abfd) == shindex)
1001 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1002 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1003 elf_onesymtab (abfd) = shindex;
1004 elf_tdata (abfd)->symtab_hdr = *hdr;
1005 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1006 abfd->flags |= HAS_SYMS;
1008 /* Sometimes a shared object will map in the symbol table. If
1009 SHF_ALLOC is set, and this is a shared object, then we also
1010 treat this section as a BFD section. We can not base the
1011 decision purely on SHF_ALLOC, because that flag is sometimes
1012 set in a relocateable object file, which would confuse the
1014 if ((hdr->sh_flags & SHF_ALLOC) != 0
1015 && (abfd->flags & DYNAMIC) != 0
1016 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1021 case SHT_DYNSYM: /* A dynamic symbol table */
1022 if (elf_dynsymtab (abfd) == shindex)
1025 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1026 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1027 elf_dynsymtab (abfd) = shindex;
1028 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1029 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1030 abfd->flags |= HAS_SYMS;
1032 /* Besides being a symbol table, we also treat this as a regular
1033 section, so that objcopy can handle it. */
1034 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1036 case SHT_STRTAB: /* A string table */
1037 if (hdr->bfd_section != NULL)
1039 if (ehdr->e_shstrndx == shindex)
1041 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1042 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1048 for (i = 1; i < ehdr->e_shnum; i++)
1050 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1051 if (hdr2->sh_link == shindex)
1053 if (! bfd_section_from_shdr (abfd, i))
1055 if (elf_onesymtab (abfd) == i)
1057 elf_tdata (abfd)->strtab_hdr = *hdr;
1058 elf_elfsections (abfd)[shindex] =
1059 &elf_tdata (abfd)->strtab_hdr;
1062 if (elf_dynsymtab (abfd) == i)
1064 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1065 elf_elfsections (abfd)[shindex] = hdr =
1066 &elf_tdata (abfd)->dynstrtab_hdr;
1067 /* We also treat this as a regular section, so
1068 that objcopy can handle it. */
1071 #if 0 /* Not handling other string tables specially right now. */
1072 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1073 /* We have a strtab for some random other section. */
1074 newsect = (asection *) hdr2->bfd_section;
1077 hdr->bfd_section = newsect;
1078 hdr2 = &elf_section_data (newsect)->str_hdr;
1080 elf_elfsections (abfd)[shindex] = hdr2;
1086 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1090 /* *These* do a lot of work -- but build no sections! */
1092 asection *target_sect;
1093 Elf_Internal_Shdr *hdr2;
1095 /* For some incomprehensible reason Oracle distributes
1096 libraries for Solaris in which some of the objects have
1097 bogus sh_link fields. It would be nice if we could just
1098 reject them, but, unfortunately, some people need to use
1099 them. We scan through the section headers; if we find only
1100 one suitable symbol table, we clobber the sh_link to point
1101 to it. I hope this doesn't break anything. */
1102 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1103 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1109 for (scan = 1; scan < ehdr->e_shnum; scan++)
1111 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1112 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1123 hdr->sh_link = found;
1126 /* Get the symbol table. */
1127 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1128 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1131 /* If this reloc section does not use the main symbol table we
1132 don't treat it as a reloc section. BFD can't adequately
1133 represent such a section, so at least for now, we don't
1134 try. We just present it as a normal section. */
1135 if (hdr->sh_link != elf_onesymtab (abfd))
1136 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1138 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1140 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1141 if (target_sect == NULL)
1144 if ((target_sect->flags & SEC_RELOC) == 0
1145 || target_sect->reloc_count == 0)
1146 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1149 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1150 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1151 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1154 elf_elfsections (abfd)[shindex] = hdr2;
1155 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1156 target_sect->flags |= SEC_RELOC;
1157 target_sect->relocation = NULL;
1158 target_sect->rel_filepos = hdr->sh_offset;
1159 abfd->flags |= HAS_RELOC;
1164 case SHT_GNU_verdef:
1165 elf_dynverdef (abfd) = shindex;
1166 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1167 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1170 case SHT_GNU_versym:
1171 elf_dynversym (abfd) = shindex;
1172 elf_tdata (abfd)->dynversym_hdr = *hdr;
1173 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1176 case SHT_GNU_verneed:
1177 elf_dynverref (abfd) = shindex;
1178 elf_tdata (abfd)->dynverref_hdr = *hdr;
1179 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1186 /* Check for any processor-specific section types. */
1188 if (bed->elf_backend_section_from_shdr)
1189 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1197 /* Given an ELF section number, retrieve the corresponding BFD
1201 bfd_section_from_elf_index (abfd, index)
1205 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1206 if (index >= elf_elfheader (abfd)->e_shnum)
1208 return elf_elfsections (abfd)[index]->bfd_section;
1212 _bfd_elf_new_section_hook (abfd, sec)
1216 struct bfd_elf_section_data *sdata;
1218 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
1221 sec->used_by_bfd = (PTR) sdata;
1222 memset (sdata, 0, sizeof (*sdata));
1226 /* Create a new bfd section from an ELF program header.
1228 Since program segments have no names, we generate a synthetic name
1229 of the form segment<NUM>, where NUM is generally the index in the
1230 program header table. For segments that are split (see below) we
1231 generate the names segment<NUM>a and segment<NUM>b.
1233 Note that some program segments may have a file size that is different than
1234 (less than) the memory size. All this means is that at execution the
1235 system must allocate the amount of memory specified by the memory size,
1236 but only initialize it with the first "file size" bytes read from the
1237 file. This would occur for example, with program segments consisting
1238 of combined data+bss.
1240 To handle the above situation, this routine generates TWO bfd sections
1241 for the single program segment. The first has the length specified by
1242 the file size of the segment, and the second has the length specified
1243 by the difference between the two sizes. In effect, the segment is split
1244 into it's initialized and uninitialized parts.
1249 bfd_section_from_phdr (abfd, hdr, index)
1251 Elf_Internal_Phdr *hdr;
1259 split = ((hdr->p_memsz > 0) &&
1260 (hdr->p_filesz > 0) &&
1261 (hdr->p_memsz > hdr->p_filesz));
1262 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1263 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1266 strcpy (name, namebuf);
1267 newsect = bfd_make_section (abfd, name);
1268 if (newsect == NULL)
1270 newsect->vma = hdr->p_vaddr;
1271 newsect->lma = hdr->p_paddr;
1272 newsect->_raw_size = hdr->p_filesz;
1273 newsect->filepos = hdr->p_offset;
1274 newsect->flags |= SEC_HAS_CONTENTS;
1275 if (hdr->p_type == PT_LOAD)
1277 newsect->flags |= SEC_ALLOC;
1278 newsect->flags |= SEC_LOAD;
1279 if (hdr->p_flags & PF_X)
1281 /* FIXME: all we known is that it has execute PERMISSION,
1283 newsect->flags |= SEC_CODE;
1286 if (!(hdr->p_flags & PF_W))
1288 newsect->flags |= SEC_READONLY;
1293 sprintf (namebuf, "segment%db", index);
1294 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1297 strcpy (name, namebuf);
1298 newsect = bfd_make_section (abfd, name);
1299 if (newsect == NULL)
1301 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1302 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1303 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1304 if (hdr->p_type == PT_LOAD)
1306 newsect->flags |= SEC_ALLOC;
1307 if (hdr->p_flags & PF_X)
1308 newsect->flags |= SEC_CODE;
1310 if (!(hdr->p_flags & PF_W))
1311 newsect->flags |= SEC_READONLY;
1317 /* Set up an ELF internal section header for a section. */
1321 elf_fake_sections (abfd, asect, failedptrarg)
1326 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1327 boolean *failedptr = (boolean *) failedptrarg;
1328 Elf_Internal_Shdr *this_hdr;
1332 /* We already failed; just get out of the bfd_map_over_sections
1337 this_hdr = &elf_section_data (asect)->this_hdr;
1339 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1342 if (this_hdr->sh_name == (unsigned long) -1)
1348 this_hdr->sh_flags = 0;
1350 if ((asect->flags & SEC_ALLOC) != 0
1351 || asect->user_set_vma)
1352 this_hdr->sh_addr = asect->vma;
1354 this_hdr->sh_addr = 0;
1356 this_hdr->sh_offset = 0;
1357 this_hdr->sh_size = asect->_raw_size;
1358 this_hdr->sh_link = 0;
1359 this_hdr->sh_addralign = 1 << asect->alignment_power;
1360 /* The sh_entsize and sh_info fields may have been set already by
1361 copy_private_section_data. */
1363 this_hdr->bfd_section = asect;
1364 this_hdr->contents = NULL;
1366 /* FIXME: This should not be based on section names. */
1367 if (strcmp (asect->name, ".dynstr") == 0)
1368 this_hdr->sh_type = SHT_STRTAB;
1369 else if (strcmp (asect->name, ".hash") == 0)
1371 this_hdr->sh_type = SHT_HASH;
1372 this_hdr->sh_entsize = bed->s->arch_size / 8;
1374 else if (strcmp (asect->name, ".dynsym") == 0)
1376 this_hdr->sh_type = SHT_DYNSYM;
1377 this_hdr->sh_entsize = bed->s->sizeof_sym;
1379 else if (strcmp (asect->name, ".dynamic") == 0)
1381 this_hdr->sh_type = SHT_DYNAMIC;
1382 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1384 else if (strncmp (asect->name, ".rela", 5) == 0
1385 && get_elf_backend_data (abfd)->use_rela_p)
1387 this_hdr->sh_type = SHT_RELA;
1388 this_hdr->sh_entsize = bed->s->sizeof_rela;
1390 else if (strncmp (asect->name, ".rel", 4) == 0
1391 && ! get_elf_backend_data (abfd)->use_rela_p)
1393 this_hdr->sh_type = SHT_REL;
1394 this_hdr->sh_entsize = bed->s->sizeof_rel;
1396 else if (strncmp (asect->name, ".note", 5) == 0)
1397 this_hdr->sh_type = SHT_NOTE;
1398 else if (strncmp (asect->name, ".stab", 5) == 0
1399 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1400 this_hdr->sh_type = SHT_STRTAB;
1401 else if (strcmp (asect->name, ".gnu.version") == 0)
1403 this_hdr->sh_type = SHT_GNU_versym;
1404 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1406 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1408 this_hdr->sh_type = SHT_GNU_verdef;
1409 this_hdr->sh_entsize = 0;
1410 /* objcopy or strip will copy over sh_info, but may not set
1411 cverdefs. The linker will set cverdefs, but sh_info will be
1413 if (this_hdr->sh_info == 0)
1414 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1416 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1417 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1419 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1421 this_hdr->sh_type = SHT_GNU_verneed;
1422 this_hdr->sh_entsize = 0;
1423 /* objcopy or strip will copy over sh_info, but may not set
1424 cverrefs. The linker will set cverrefs, but sh_info will be
1426 if (this_hdr->sh_info == 0)
1427 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1429 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1430 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1432 else if ((asect->flags & SEC_ALLOC) != 0
1433 && (asect->flags & SEC_LOAD) != 0)
1434 this_hdr->sh_type = SHT_PROGBITS;
1435 else if ((asect->flags & SEC_ALLOC) != 0
1436 && ((asect->flags & SEC_LOAD) == 0))
1437 this_hdr->sh_type = SHT_NOBITS;
1441 this_hdr->sh_type = SHT_PROGBITS;
1444 if ((asect->flags & SEC_ALLOC) != 0)
1445 this_hdr->sh_flags |= SHF_ALLOC;
1446 if ((asect->flags & SEC_READONLY) == 0)
1447 this_hdr->sh_flags |= SHF_WRITE;
1448 if ((asect->flags & SEC_CODE) != 0)
1449 this_hdr->sh_flags |= SHF_EXECINSTR;
1451 /* Check for processor-specific section types. */
1453 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1455 if (bed->elf_backend_fake_sections)
1456 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1459 /* If the section has relocs, set up a section header for the
1460 SHT_REL[A] section. */
1461 if ((asect->flags & SEC_RELOC) != 0)
1463 Elf_Internal_Shdr *rela_hdr;
1464 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1467 rela_hdr = &elf_section_data (asect)->rel_hdr;
1468 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1474 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1476 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1478 if (rela_hdr->sh_name == (unsigned int) -1)
1483 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1484 rela_hdr->sh_entsize = (use_rela_p
1485 ? bed->s->sizeof_rela
1486 : bed->s->sizeof_rel);
1487 rela_hdr->sh_addralign = bed->s->file_align;
1488 rela_hdr->sh_flags = 0;
1489 rela_hdr->sh_addr = 0;
1490 rela_hdr->sh_size = 0;
1491 rela_hdr->sh_offset = 0;
1495 /* Assign all ELF section numbers. The dummy first section is handled here
1496 too. The link/info pointers for the standard section types are filled
1497 in here too, while we're at it. */
1500 assign_section_numbers (abfd)
1503 struct elf_obj_tdata *t = elf_tdata (abfd);
1505 unsigned int section_number;
1506 Elf_Internal_Shdr **i_shdrp;
1507 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1511 for (sec = abfd->sections; sec; sec = sec->next)
1513 struct bfd_elf_section_data *d = elf_section_data (sec);
1515 d->this_idx = section_number++;
1516 if ((sec->flags & SEC_RELOC) == 0)
1519 d->rel_idx = section_number++;
1522 t->shstrtab_section = section_number++;
1523 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1524 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1526 if (abfd->symcount > 0)
1528 t->symtab_section = section_number++;
1529 t->strtab_section = section_number++;
1532 elf_elfheader (abfd)->e_shnum = section_number;
1534 /* Set up the list of section header pointers, in agreement with the
1536 i_shdrp = ((Elf_Internal_Shdr **)
1537 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1538 if (i_shdrp == NULL)
1541 i_shdrp[0] = ((Elf_Internal_Shdr *)
1542 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1543 if (i_shdrp[0] == NULL)
1545 bfd_release (abfd, i_shdrp);
1548 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1550 elf_elfsections (abfd) = i_shdrp;
1552 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1553 if (abfd->symcount > 0)
1555 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1556 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1557 t->symtab_hdr.sh_link = t->strtab_section;
1559 for (sec = abfd->sections; sec; sec = sec->next)
1561 struct bfd_elf_section_data *d = elf_section_data (sec);
1565 i_shdrp[d->this_idx] = &d->this_hdr;
1566 if (d->rel_idx != 0)
1567 i_shdrp[d->rel_idx] = &d->rel_hdr;
1569 /* Fill in the sh_link and sh_info fields while we're at it. */
1571 /* sh_link of a reloc section is the section index of the symbol
1572 table. sh_info is the section index of the section to which
1573 the relocation entries apply. */
1574 if (d->rel_idx != 0)
1576 d->rel_hdr.sh_link = t->symtab_section;
1577 d->rel_hdr.sh_info = d->this_idx;
1580 switch (d->this_hdr.sh_type)
1584 /* A reloc section which we are treating as a normal BFD
1585 section. sh_link is the section index of the symbol
1586 table. sh_info is the section index of the section to
1587 which the relocation entries apply. We assume that an
1588 allocated reloc section uses the dynamic symbol table.
1589 FIXME: How can we be sure? */
1590 s = bfd_get_section_by_name (abfd, ".dynsym");
1592 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1594 /* We look up the section the relocs apply to by name. */
1596 if (d->this_hdr.sh_type == SHT_REL)
1600 s = bfd_get_section_by_name (abfd, name);
1602 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1606 /* We assume that a section named .stab*str is a stabs
1607 string section. We look for a section with the same name
1608 but without the trailing ``str'', and set its sh_link
1609 field to point to this section. */
1610 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1611 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1616 len = strlen (sec->name);
1617 alc = (char *) bfd_malloc (len - 2);
1620 strncpy (alc, sec->name, len - 3);
1621 alc[len - 3] = '\0';
1622 s = bfd_get_section_by_name (abfd, alc);
1626 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1628 /* This is a .stab section. */
1629 elf_section_data (s)->this_hdr.sh_entsize =
1630 4 + 2 * (bed->s->arch_size / 8);
1637 case SHT_GNU_verneed:
1638 case SHT_GNU_verdef:
1639 /* sh_link is the section header index of the string table
1640 used for the dynamic entries, or the symbol table, or the
1642 s = bfd_get_section_by_name (abfd, ".dynstr");
1644 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1648 case SHT_GNU_versym:
1649 /* sh_link is the section header index of the symbol table
1650 this hash table or version table is for. */
1651 s = bfd_get_section_by_name (abfd, ".dynsym");
1653 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1661 /* Map symbol from it's internal number to the external number, moving
1662 all local symbols to be at the head of the list. */
1665 sym_is_global (abfd, sym)
1669 /* If the backend has a special mapping, use it. */
1670 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1671 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1674 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1675 || bfd_is_und_section (bfd_get_section (sym))
1676 || bfd_is_com_section (bfd_get_section (sym)));
1680 elf_map_symbols (abfd)
1683 int symcount = bfd_get_symcount (abfd);
1684 asymbol **syms = bfd_get_outsymbols (abfd);
1685 asymbol **sect_syms;
1687 int num_globals = 0;
1688 int num_locals2 = 0;
1689 int num_globals2 = 0;
1691 int num_sections = 0;
1697 fprintf (stderr, "elf_map_symbols\n");
1701 /* Add a section symbol for each BFD section. FIXME: Is this really
1703 for (asect = abfd->sections; asect; asect = asect->next)
1705 if (max_index < asect->index)
1706 max_index = asect->index;
1710 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1711 if (sect_syms == NULL)
1713 elf_section_syms (abfd) = sect_syms;
1715 for (idx = 0; idx < symcount; idx++)
1717 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1718 && (syms[idx]->value + syms[idx]->section->vma) == 0)
1722 sec = syms[idx]->section;
1723 if (sec->owner != NULL)
1725 if (sec->owner != abfd)
1727 if (sec->output_offset != 0)
1729 sec = sec->output_section;
1730 BFD_ASSERT (sec->owner == abfd);
1732 sect_syms[sec->index] = syms[idx];
1737 for (asect = abfd->sections; asect; asect = asect->next)
1741 if (sect_syms[asect->index] != NULL)
1744 sym = bfd_make_empty_symbol (abfd);
1747 sym->the_bfd = abfd;
1748 sym->name = asect->name;
1750 /* Set the flags to 0 to indicate that this one was newly added. */
1752 sym->section = asect;
1753 sect_syms[asect->index] = sym;
1757 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1758 asect->name, (long) asect->vma, asect->index, (long) asect);
1762 /* Classify all of the symbols. */
1763 for (idx = 0; idx < symcount; idx++)
1765 if (!sym_is_global (abfd, syms[idx]))
1770 for (asect = abfd->sections; asect; asect = asect->next)
1772 if (sect_syms[asect->index] != NULL
1773 && sect_syms[asect->index]->flags == 0)
1775 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1776 if (!sym_is_global (abfd, sect_syms[asect->index]))
1780 sect_syms[asect->index]->flags = 0;
1784 /* Now sort the symbols so the local symbols are first. */
1785 new_syms = ((asymbol **)
1787 (num_locals + num_globals) * sizeof (asymbol *)));
1788 if (new_syms == NULL)
1791 for (idx = 0; idx < symcount; idx++)
1793 asymbol *sym = syms[idx];
1796 if (!sym_is_global (abfd, sym))
1799 i = num_locals + num_globals2++;
1801 sym->udata.i = i + 1;
1803 for (asect = abfd->sections; asect; asect = asect->next)
1805 if (sect_syms[asect->index] != NULL
1806 && sect_syms[asect->index]->flags == 0)
1808 asymbol *sym = sect_syms[asect->index];
1811 sym->flags = BSF_SECTION_SYM;
1812 if (!sym_is_global (abfd, sym))
1815 i = num_locals + num_globals2++;
1817 sym->udata.i = i + 1;
1821 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1823 elf_num_locals (abfd) = num_locals;
1824 elf_num_globals (abfd) = num_globals;
1828 /* Align to the maximum file alignment that could be required for any
1829 ELF data structure. */
1831 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1832 static INLINE file_ptr
1833 align_file_position (off, align)
1837 return (off + align - 1) & ~(align - 1);
1840 /* Assign a file position to a section, optionally aligning to the
1841 required section alignment. */
1844 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1845 Elf_Internal_Shdr *i_shdrp;
1853 al = i_shdrp->sh_addralign;
1855 offset = BFD_ALIGN (offset, al);
1857 i_shdrp->sh_offset = offset;
1858 if (i_shdrp->bfd_section != NULL)
1859 i_shdrp->bfd_section->filepos = offset;
1860 if (i_shdrp->sh_type != SHT_NOBITS)
1861 offset += i_shdrp->sh_size;
1865 /* Compute the file positions we are going to put the sections at, and
1866 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1867 is not NULL, this is being called by the ELF backend linker. */
1870 _bfd_elf_compute_section_file_positions (abfd, link_info)
1872 struct bfd_link_info *link_info;
1874 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1876 struct bfd_strtab_hash *strtab;
1877 Elf_Internal_Shdr *shstrtab_hdr;
1879 if (abfd->output_has_begun)
1882 /* Do any elf backend specific processing first. */
1883 if (bed->elf_backend_begin_write_processing)
1884 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1886 if (! prep_headers (abfd))
1890 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1894 if (!assign_section_numbers (abfd))
1897 /* The backend linker builds symbol table information itself. */
1898 if (link_info == NULL && abfd->symcount > 0)
1900 if (! swap_out_syms (abfd, &strtab))
1904 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1905 /* sh_name was set in prep_headers. */
1906 shstrtab_hdr->sh_type = SHT_STRTAB;
1907 shstrtab_hdr->sh_flags = 0;
1908 shstrtab_hdr->sh_addr = 0;
1909 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1910 shstrtab_hdr->sh_entsize = 0;
1911 shstrtab_hdr->sh_link = 0;
1912 shstrtab_hdr->sh_info = 0;
1913 /* sh_offset is set in assign_file_positions_except_relocs. */
1914 shstrtab_hdr->sh_addralign = 1;
1916 if (!assign_file_positions_except_relocs (abfd))
1919 if (link_info == NULL && abfd->symcount > 0)
1922 Elf_Internal_Shdr *hdr;
1924 off = elf_tdata (abfd)->next_file_pos;
1926 hdr = &elf_tdata (abfd)->symtab_hdr;
1927 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1929 hdr = &elf_tdata (abfd)->strtab_hdr;
1930 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1932 elf_tdata (abfd)->next_file_pos = off;
1934 /* Now that we know where the .strtab section goes, write it
1936 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
1937 || ! _bfd_stringtab_emit (abfd, strtab))
1939 _bfd_stringtab_free (strtab);
1942 abfd->output_has_begun = true;
1947 /* Create a mapping from a set of sections to a program segment. */
1949 static INLINE struct elf_segment_map *
1950 make_mapping (abfd, sections, from, to, phdr)
1952 asection **sections;
1957 struct elf_segment_map *m;
1961 m = ((struct elf_segment_map *)
1963 (sizeof (struct elf_segment_map)
1964 + (to - from - 1) * sizeof (asection *))));
1968 m->p_type = PT_LOAD;
1969 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1970 m->sections[i - from] = *hdrpp;
1971 m->count = to - from;
1973 if (from == 0 && phdr)
1975 /* Include the headers in the first PT_LOAD segment. */
1976 m->includes_filehdr = 1;
1977 m->includes_phdrs = 1;
1983 /* Set up a mapping from BFD sections to program segments. */
1986 map_sections_to_segments (abfd)
1989 asection **sections = NULL;
1993 struct elf_segment_map *mfirst;
1994 struct elf_segment_map **pm;
1995 struct elf_segment_map *m;
1997 unsigned int phdr_index;
1998 bfd_vma maxpagesize;
2000 boolean phdr_in_section = true;
2004 if (elf_tdata (abfd)->segment_map != NULL)
2007 if (bfd_count_sections (abfd) == 0)
2010 /* Select the allocated sections, and sort them. */
2012 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2013 * sizeof (asection *));
2014 if (sections == NULL)
2018 for (s = abfd->sections; s != NULL; s = s->next)
2020 if ((s->flags & SEC_ALLOC) != 0)
2026 BFD_ASSERT (i <= bfd_count_sections (abfd));
2029 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2031 /* Build the mapping. */
2036 /* If we have a .interp section, then create a PT_PHDR segment for
2037 the program headers and a PT_INTERP segment for the .interp
2039 s = bfd_get_section_by_name (abfd, ".interp");
2040 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2042 m = ((struct elf_segment_map *)
2043 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2047 m->p_type = PT_PHDR;
2048 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2049 m->p_flags = PF_R | PF_X;
2050 m->p_flags_valid = 1;
2051 m->includes_phdrs = 1;
2056 m = ((struct elf_segment_map *)
2057 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2061 m->p_type = PT_INTERP;
2069 /* Look through the sections. We put sections in the same program
2070 segment when the start of the second section can be placed within
2071 a few bytes of the end of the first section. */
2074 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2076 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2078 && (dynsec->flags & SEC_LOAD) == 0)
2081 /* Deal with -Ttext or something similar such that the first section
2082 is not adjacent to the program headers. This is an
2083 approximation, since at this point we don't know exactly how many
2084 program headers we will need. */
2087 bfd_size_type phdr_size;
2089 phdr_size = elf_tdata (abfd)->program_header_size;
2091 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2092 if ((abfd->flags & D_PAGED) == 0
2093 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2094 phdr_in_section = false;
2097 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2100 boolean new_segment;
2104 /* See if this section and the last one will fit in the same
2107 if (last_hdr == NULL)
2109 /* If we don't have a segment yet, then we don't need a new
2110 one (we build the last one after this loop). */
2111 new_segment = false;
2113 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2115 /* If this section has a different relation between the
2116 virtual address and the load address, then we need a new
2120 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2123 /* If putting this section in this segment would force us to
2124 skip a page in the segment, then we need a new segment. */
2127 else if ((abfd->flags & D_PAGED) == 0)
2129 /* If the file is not demand paged, which means that we
2130 don't require the sections to be correctly aligned in the
2131 file, then there is no other reason for a new segment. */
2132 new_segment = false;
2134 else if ((last_hdr->flags & SEC_LOAD) == 0
2135 && (hdr->flags & SEC_LOAD) != 0)
2137 /* We don't want to put a loadable section after a
2138 nonloadable section in the same segment. */
2142 && (hdr->flags & SEC_READONLY) == 0
2143 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2146 /* We don't want to put a writable section in a read only
2147 segment, unless they are on the same page in memory
2148 anyhow. We already know that the last section does not
2149 bring us past the current section on the page, so the
2150 only case in which the new section is not on the same
2151 page as the previous section is when the previous section
2152 ends precisely on a page boundary. */
2157 /* Otherwise, we can use the same segment. */
2158 new_segment = false;
2163 if ((hdr->flags & SEC_READONLY) == 0)
2169 /* We need a new program segment. We must create a new program
2170 header holding all the sections from phdr_index until hdr. */
2172 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
2179 if ((hdr->flags & SEC_READONLY) == 0)
2186 phdr_in_section = false;
2189 /* Create a final PT_LOAD program segment. */
2190 if (last_hdr != NULL)
2192 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
2200 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2203 m = ((struct elf_segment_map *)
2204 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2208 m->p_type = PT_DYNAMIC;
2210 m->sections[0] = dynsec;
2216 /* For each loadable .note section, add a PT_NOTE segment. We don't
2217 use bfd_get_section_by_name, because if we link together
2218 nonloadable .note sections and loadable .note sections, we will
2219 generate two .note sections in the output file. FIXME: Using
2220 names for section types is bogus anyhow. */
2221 for (s = abfd->sections; s != NULL; s = s->next)
2223 if ((s->flags & SEC_LOAD) != 0
2224 && strncmp (s->name, ".note", 5) == 0)
2226 m = ((struct elf_segment_map *)
2227 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2231 m->p_type = PT_NOTE;
2243 elf_tdata (abfd)->segment_map = mfirst;
2247 if (sections != NULL)
2252 /* Sort sections by VMA. */
2255 elf_sort_sections (arg1, arg2)
2259 const asection *sec1 = *(const asection **) arg1;
2260 const asection *sec2 = *(const asection **) arg2;
2262 if (sec1->vma < sec2->vma)
2264 else if (sec1->vma > sec2->vma)
2267 /* Sort by LMA. Normally the LMA and the VMA will be the same, and
2268 this will do nothing. */
2269 if (sec1->lma < sec2->lma)
2271 else if (sec1->lma > sec2->lma)
2274 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2276 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2280 return sec1->target_index - sec2->target_index;
2289 /* Sort by size, to put zero sized sections before others at the
2292 if (sec1->_raw_size < sec2->_raw_size)
2294 if (sec1->_raw_size > sec2->_raw_size)
2297 return sec1->target_index - sec2->target_index;
2300 /* Assign file positions to the sections based on the mapping from
2301 sections to segments. This function also sets up some fields in
2302 the file header, and writes out the program headers. */
2305 assign_file_positions_for_segments (abfd)
2308 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2310 struct elf_segment_map *m;
2312 Elf_Internal_Phdr *phdrs;
2314 bfd_vma filehdr_vaddr, filehdr_paddr;
2315 bfd_vma phdrs_vaddr, phdrs_paddr;
2316 Elf_Internal_Phdr *p;
2318 if (elf_tdata (abfd)->segment_map == NULL)
2320 if (! map_sections_to_segments (abfd))
2324 if (bed->elf_backend_modify_segment_map)
2326 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2331 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2334 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2335 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2336 elf_elfheader (abfd)->e_phnum = count;
2341 /* If we already counted the number of program segments, make sure
2342 that we allocated enough space. This happens when SIZEOF_HEADERS
2343 is used in a linker script. */
2344 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2345 if (alloc != 0 && count > alloc)
2347 ((*_bfd_error_handler)
2348 ("%s: Not enough room for program headers (allocated %u, need %u)",
2349 bfd_get_filename (abfd), alloc, count));
2350 bfd_set_error (bfd_error_bad_value);
2357 phdrs = ((Elf_Internal_Phdr *)
2358 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2362 off = bed->s->sizeof_ehdr;
2363 off += alloc * bed->s->sizeof_phdr;
2369 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2376 /* If elf_segment_map is not from map_sections_to_segments, the
2377 sections may not be correctly ordered. */
2379 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2382 p->p_type = m->p_type;
2384 if (m->p_flags_valid)
2385 p->p_flags = m->p_flags;
2389 if (p->p_type == PT_LOAD
2391 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2393 if ((abfd->flags & D_PAGED) != 0)
2394 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2396 off += ((m->sections[0]->vma - off)
2397 % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2403 p->p_vaddr = m->sections[0]->vma;
2405 if (m->p_paddr_valid)
2406 p->p_paddr = m->p_paddr;
2407 else if (m->count == 0)
2410 p->p_paddr = m->sections[0]->lma;
2412 if (p->p_type == PT_LOAD
2413 && (abfd->flags & D_PAGED) != 0)
2414 p->p_align = bed->maxpagesize;
2415 else if (m->count == 0)
2416 p->p_align = bed->s->file_align;
2424 if (m->includes_filehdr)
2426 if (! m->p_flags_valid)
2429 p->p_filesz = bed->s->sizeof_ehdr;
2430 p->p_memsz = bed->s->sizeof_ehdr;
2433 BFD_ASSERT (p->p_type == PT_LOAD);
2435 if (! m->p_paddr_valid)
2438 if (p->p_type == PT_LOAD)
2440 filehdr_vaddr = p->p_vaddr;
2441 filehdr_paddr = p->p_paddr;
2445 if (m->includes_phdrs)
2447 if (! m->p_flags_valid)
2449 if (m->includes_filehdr)
2451 if (p->p_type == PT_LOAD)
2453 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2454 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2459 p->p_offset = bed->s->sizeof_ehdr;
2462 BFD_ASSERT (p->p_type == PT_LOAD);
2463 p->p_vaddr -= off - p->p_offset;
2464 if (! m->p_paddr_valid)
2465 p->p_paddr -= off - p->p_offset;
2467 if (p->p_type == PT_LOAD)
2469 phdrs_vaddr = p->p_vaddr;
2470 phdrs_paddr = p->p_paddr;
2473 p->p_filesz += alloc * bed->s->sizeof_phdr;
2474 p->p_memsz += alloc * bed->s->sizeof_phdr;
2477 if (p->p_type == PT_LOAD)
2479 if (! m->includes_filehdr && ! m->includes_phdrs)
2485 adjust = off - (p->p_offset + p->p_filesz);
2486 p->p_filesz += adjust;
2487 p->p_memsz += adjust;
2492 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2496 bfd_size_type align;
2500 align = 1 << bfd_get_section_alignment (abfd, sec);
2502 if (p->p_type == PT_LOAD)
2506 /* The section VMA must equal the file position modulo
2508 if ((flags & SEC_ALLOC) != 0)
2510 if ((abfd->flags & D_PAGED) != 0)
2511 adjust = (sec->vma - voff) % bed->maxpagesize;
2513 adjust = (sec->vma - voff) % align;
2518 p->p_memsz += adjust;
2521 if ((flags & SEC_LOAD) != 0)
2522 p->p_filesz += adjust;
2528 if ((flags & SEC_LOAD) != 0)
2529 off += sec->_raw_size;
2530 if ((flags & SEC_ALLOC) != 0)
2531 voff += sec->_raw_size;
2534 p->p_memsz += sec->_raw_size;
2536 if ((flags & SEC_LOAD) != 0)
2537 p->p_filesz += sec->_raw_size;
2539 if (align > p->p_align)
2542 if (! m->p_flags_valid)
2545 if ((flags & SEC_CODE) != 0)
2547 if ((flags & SEC_READONLY) == 0)
2553 /* Now that we have set the section file positions, we can set up
2554 the file positions for the non PT_LOAD segments. */
2555 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2559 if (p->p_type != PT_LOAD && m->count > 0)
2561 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2562 p->p_offset = m->sections[0]->filepos;
2566 if (m->includes_filehdr)
2568 p->p_vaddr = filehdr_vaddr;
2569 if (! m->p_paddr_valid)
2570 p->p_paddr = filehdr_paddr;
2572 else if (m->includes_phdrs)
2574 p->p_vaddr = phdrs_vaddr;
2575 if (! m->p_paddr_valid)
2576 p->p_paddr = phdrs_paddr;
2581 /* Clear out any program headers we allocated but did not use. */
2582 for (; count < alloc; count++, p++)
2584 memset (p, 0, sizeof *p);
2585 p->p_type = PT_NULL;
2588 elf_tdata (abfd)->phdr = phdrs;
2590 elf_tdata (abfd)->next_file_pos = off;
2592 /* Write out the program headers. */
2593 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2594 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2600 /* Get the size of the program header.
2602 If this is called by the linker before any of the section VMA's are set, it
2603 can't calculate the correct value for a strange memory layout. This only
2604 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2605 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2606 data segment (exclusive of .interp and .dynamic).
2608 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2609 will be two segments. */
2611 static bfd_size_type
2612 get_program_header_size (abfd)
2617 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2619 /* We can't return a different result each time we're called. */
2620 if (elf_tdata (abfd)->program_header_size != 0)
2621 return elf_tdata (abfd)->program_header_size;
2623 if (elf_tdata (abfd)->segment_map != NULL)
2625 struct elf_segment_map *m;
2628 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2630 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2631 return elf_tdata (abfd)->program_header_size;
2634 /* Assume we will need exactly two PT_LOAD segments: one for text
2635 and one for data. */
2638 s = bfd_get_section_by_name (abfd, ".interp");
2639 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2641 /* If we have a loadable interpreter section, we need a
2642 PT_INTERP segment. In this case, assume we also need a
2643 PT_PHDR segment, although that may not be true for all
2648 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2650 /* We need a PT_DYNAMIC segment. */
2654 for (s = abfd->sections; s != NULL; s = s->next)
2656 if ((s->flags & SEC_LOAD) != 0
2657 && strncmp (s->name, ".note", 5) == 0)
2659 /* We need a PT_NOTE segment. */
2664 /* Let the backend count up any program headers it might need. */
2665 if (bed->elf_backend_additional_program_headers)
2669 a = (*bed->elf_backend_additional_program_headers) (abfd);
2675 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2676 return elf_tdata (abfd)->program_header_size;
2679 /* Work out the file positions of all the sections. This is called by
2680 _bfd_elf_compute_section_file_positions. All the section sizes and
2681 VMAs must be known before this is called.
2683 We do not consider reloc sections at this point, unless they form
2684 part of the loadable image. Reloc sections are assigned file
2685 positions in assign_file_positions_for_relocs, which is called by
2686 write_object_contents and final_link.
2688 We also don't set the positions of the .symtab and .strtab here. */
2691 assign_file_positions_except_relocs (abfd)
2694 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2695 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2696 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2698 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2700 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2702 Elf_Internal_Shdr **hdrpp;
2705 /* Start after the ELF header. */
2706 off = i_ehdrp->e_ehsize;
2708 /* We are not creating an executable, which means that we are
2709 not creating a program header, and that the actual order of
2710 the sections in the file is unimportant. */
2711 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2713 Elf_Internal_Shdr *hdr;
2716 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2718 hdr->sh_offset = -1;
2721 if (i == tdata->symtab_section
2722 || i == tdata->strtab_section)
2724 hdr->sh_offset = -1;
2728 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2734 Elf_Internal_Shdr **hdrpp;
2736 /* Assign file positions for the loaded sections based on the
2737 assignment of sections to segments. */
2738 if (! assign_file_positions_for_segments (abfd))
2741 /* Assign file positions for the other sections. */
2743 off = elf_tdata (abfd)->next_file_pos;
2744 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2746 Elf_Internal_Shdr *hdr;
2749 if (hdr->bfd_section != NULL
2750 && hdr->bfd_section->filepos != 0)
2751 hdr->sh_offset = hdr->bfd_section->filepos;
2752 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2754 ((*_bfd_error_handler)
2755 ("%s: warning: allocated section `%s' not in segment",
2756 bfd_get_filename (abfd),
2757 (hdr->bfd_section == NULL
2759 : hdr->bfd_section->name)));
2760 if ((abfd->flags & D_PAGED) != 0)
2761 off += (hdr->sh_addr - off) % bed->maxpagesize;
2763 off += (hdr->sh_addr - off) % hdr->sh_addralign;
2764 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2767 else if (hdr->sh_type == SHT_REL
2768 || hdr->sh_type == SHT_RELA
2769 || hdr == i_shdrpp[tdata->symtab_section]
2770 || hdr == i_shdrpp[tdata->strtab_section])
2771 hdr->sh_offset = -1;
2773 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2777 /* Place the section headers. */
2778 off = align_file_position (off, bed->s->file_align);
2779 i_ehdrp->e_shoff = off;
2780 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2782 elf_tdata (abfd)->next_file_pos = off;
2791 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2792 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2793 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2795 struct bfd_strtab_hash *shstrtab;
2796 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2798 i_ehdrp = elf_elfheader (abfd);
2799 i_shdrp = elf_elfsections (abfd);
2801 shstrtab = _bfd_elf_stringtab_init ();
2802 if (shstrtab == NULL)
2805 elf_shstrtab (abfd) = shstrtab;
2807 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2808 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2809 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2810 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2812 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2813 i_ehdrp->e_ident[EI_DATA] =
2814 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
2815 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2817 for (count = EI_PAD; count < EI_NIDENT; count++)
2818 i_ehdrp->e_ident[count] = 0;
2820 if ((abfd->flags & DYNAMIC) != 0)
2821 i_ehdrp->e_type = ET_DYN;
2822 else if ((abfd->flags & EXEC_P) != 0)
2823 i_ehdrp->e_type = ET_EXEC;
2825 i_ehdrp->e_type = ET_REL;
2827 switch (bfd_get_arch (abfd))
2829 case bfd_arch_unknown:
2830 i_ehdrp->e_machine = EM_NONE;
2832 case bfd_arch_sparc:
2833 if (bed->s->arch_size == 64)
2834 i_ehdrp->e_machine = EM_SPARC64;
2836 i_ehdrp->e_machine = EM_SPARC;
2839 i_ehdrp->e_machine = EM_386;
2842 i_ehdrp->e_machine = EM_68K;
2845 i_ehdrp->e_machine = EM_88K;
2848 i_ehdrp->e_machine = EM_860;
2850 case bfd_arch_mips: /* MIPS Rxxxx */
2851 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2854 i_ehdrp->e_machine = EM_PARISC;
2856 case bfd_arch_powerpc:
2857 i_ehdrp->e_machine = EM_PPC;
2859 case bfd_arch_alpha:
2860 i_ehdrp->e_machine = EM_ALPHA;
2863 i_ehdrp->e_machine = EM_SH;
2866 i_ehdrp->e_machine = EM_CYGNUS_D10V;
2868 /* start-sanitize-d30v */
2870 i_ehdrp->e_machine = EM_CYGNUS_D30V;
2872 /* end-sanitize-d30v */
2873 /* start-sanitize-v850 */
2875 i_ehdrp->e_machine = EM_CYGNUS_V850;
2877 /* end-sanitize-v850 */
2878 /* start-sanitize-arc */
2880 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2882 /* end-sanitize-arc */
2884 i_ehdrp->e_machine = EM_CYGNUS_M32R;
2886 case bfd_arch_mn10200:
2887 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
2889 case bfd_arch_mn10300:
2890 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
2892 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2894 i_ehdrp->e_machine = EM_NONE;
2896 i_ehdrp->e_version = bed->s->ev_current;
2897 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2899 /* no program header, for now. */
2900 i_ehdrp->e_phoff = 0;
2901 i_ehdrp->e_phentsize = 0;
2902 i_ehdrp->e_phnum = 0;
2904 /* each bfd section is section header entry */
2905 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2906 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2908 /* if we're building an executable, we'll need a program header table */
2909 if (abfd->flags & EXEC_P)
2911 /* it all happens later */
2913 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2915 /* elf_build_phdrs() returns a (NULL-terminated) array of
2916 Elf_Internal_Phdrs */
2917 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2918 i_ehdrp->e_phoff = outbase;
2919 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2924 i_ehdrp->e_phentsize = 0;
2926 i_ehdrp->e_phoff = 0;
2929 elf_tdata (abfd)->symtab_hdr.sh_name =
2930 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2931 elf_tdata (abfd)->strtab_hdr.sh_name =
2932 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2933 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2934 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2935 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2936 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2937 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2943 /* Assign file positions for all the reloc sections which are not part
2944 of the loadable file image. */
2947 _bfd_elf_assign_file_positions_for_relocs (abfd)
2952 Elf_Internal_Shdr **shdrpp;
2954 off = elf_tdata (abfd)->next_file_pos;
2956 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2957 i < elf_elfheader (abfd)->e_shnum;
2960 Elf_Internal_Shdr *shdrp;
2963 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2964 && shdrp->sh_offset == -1)
2965 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
2968 elf_tdata (abfd)->next_file_pos = off;
2972 _bfd_elf_write_object_contents (abfd)
2975 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2976 Elf_Internal_Ehdr *i_ehdrp;
2977 Elf_Internal_Shdr **i_shdrp;
2981 if (! abfd->output_has_begun
2982 && ! _bfd_elf_compute_section_file_positions (abfd,
2983 (struct bfd_link_info *) NULL))
2986 i_shdrp = elf_elfsections (abfd);
2987 i_ehdrp = elf_elfheader (abfd);
2990 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2993 _bfd_elf_assign_file_positions_for_relocs (abfd);
2995 /* After writing the headers, we need to write the sections too... */
2996 for (count = 1; count < i_ehdrp->e_shnum; count++)
2998 if (bed->elf_backend_section_processing)
2999 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3000 if (i_shdrp[count]->contents)
3002 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3003 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3005 != i_shdrp[count]->sh_size))
3010 /* Write out the section header names. */
3011 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3012 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3015 if (bed->elf_backend_final_write_processing)
3016 (*bed->elf_backend_final_write_processing) (abfd,
3017 elf_tdata (abfd)->linker);
3019 return bed->s->write_shdrs_and_ehdr (abfd);
3022 /* given a section, search the header to find them... */
3024 _bfd_elf_section_from_bfd_section (abfd, asect)
3028 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3029 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3031 Elf_Internal_Shdr *hdr;
3032 int maxindex = elf_elfheader (abfd)->e_shnum;
3034 for (index = 0; index < maxindex; index++)
3036 hdr = i_shdrp[index];
3037 if (hdr->bfd_section == asect)
3041 if (bed->elf_backend_section_from_bfd_section)
3043 for (index = 0; index < maxindex; index++)
3047 hdr = i_shdrp[index];
3049 if ((*bed->elf_backend_section_from_bfd_section)
3050 (abfd, hdr, asect, &retval))
3055 if (bfd_is_abs_section (asect))
3057 if (bfd_is_com_section (asect))
3059 if (bfd_is_und_section (asect))
3065 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3069 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3071 asymbol **asym_ptr_ptr;
3073 asymbol *asym_ptr = *asym_ptr_ptr;
3075 flagword flags = asym_ptr->flags;
3077 /* When gas creates relocations against local labels, it creates its
3078 own symbol for the section, but does put the symbol into the
3079 symbol chain, so udata is 0. When the linker is generating
3080 relocatable output, this section symbol may be for one of the
3081 input sections rather than the output section. */
3082 if (asym_ptr->udata.i == 0
3083 && (flags & BSF_SECTION_SYM)
3084 && asym_ptr->section)
3088 if (asym_ptr->section->output_section != NULL)
3089 indx = asym_ptr->section->output_section->index;
3091 indx = asym_ptr->section->index;
3092 if (elf_section_syms (abfd)[indx])
3093 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3096 idx = asym_ptr->udata.i;
3100 /* This case can occur when using --strip-symbol on a symbol
3101 which is used in a relocation entry. */
3102 (*_bfd_error_handler)
3103 ("%s: symbol `%s' required but not present",
3104 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3105 bfd_set_error (bfd_error_no_symbols);
3112 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
3113 (long) asym_ptr, asym_ptr->name, idx, flags,
3114 elf_symbol_flags (flags));
3122 /* Copy private BFD data. This copies any program header information. */
3125 copy_private_bfd_data (ibfd, obfd)
3129 Elf_Internal_Ehdr *iehdr;
3130 struct elf_segment_map *mfirst;
3131 struct elf_segment_map **pm;
3132 Elf_Internal_Phdr *p;
3135 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3136 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3139 if (elf_tdata (ibfd)->phdr == NULL)
3142 iehdr = elf_elfheader (ibfd);
3147 c = elf_elfheader (ibfd)->e_phnum;
3148 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
3152 struct elf_segment_map *m;
3157 /* The complicated case when p_vaddr is 0 is to handle the
3158 Solaris linker, which generates a PT_INTERP section with
3159 p_vaddr and p_memsz set to 0. */
3160 for (s = ibfd->sections; s != NULL; s = s->next)
3161 if (((s->vma >= p->p_vaddr
3162 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
3163 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
3166 && (s->flags & SEC_HAS_CONTENTS) != 0
3167 && (bfd_vma) s->filepos >= p->p_offset
3168 && ((bfd_vma) s->filepos + s->_raw_size
3169 <= p->p_offset + p->p_filesz)))
3170 && (s->flags & SEC_ALLOC) != 0
3171 && s->output_section != NULL)
3174 m = ((struct elf_segment_map *)
3176 (sizeof (struct elf_segment_map)
3177 + ((size_t) csecs - 1) * sizeof (asection *))));
3182 m->p_type = p->p_type;
3183 m->p_flags = p->p_flags;
3184 m->p_flags_valid = 1;
3185 m->p_paddr = p->p_paddr;
3186 m->p_paddr_valid = 1;
3188 m->includes_filehdr = (p->p_offset == 0
3189 && p->p_filesz >= iehdr->e_ehsize);
3191 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
3192 && (p->p_offset + p->p_filesz
3193 >= ((bfd_vma) iehdr->e_phoff
3194 + iehdr->e_phnum * iehdr->e_phentsize)));
3197 for (s = ibfd->sections; s != NULL; s = s->next)
3199 if (((s->vma >= p->p_vaddr
3200 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
3201 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
3204 && (s->flags & SEC_HAS_CONTENTS) != 0
3205 && (bfd_vma) s->filepos >= p->p_offset
3206 && ((bfd_vma) s->filepos + s->_raw_size
3207 <= p->p_offset + p->p_filesz)))
3208 && (s->flags & SEC_ALLOC) != 0
3209 && s->output_section != NULL)
3211 m->sections[isec] = s->output_section;
3215 BFD_ASSERT (isec == csecs);
3222 elf_tdata (obfd)->segment_map = mfirst;
3227 /* Copy private section information. This copies over the entsize
3228 field, and sometimes the info field. */
3231 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3237 Elf_Internal_Shdr *ihdr, *ohdr;
3239 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3240 || obfd->xvec->flavour != bfd_target_elf_flavour)
3243 /* Copy over private BFD data if it has not already been copied.
3244 This must be done here, rather than in the copy_private_bfd_data
3245 entry point, because the latter is called after the section
3246 contents have been set, which means that the program headers have
3247 already been worked out. */
3248 if (elf_tdata (obfd)->segment_map == NULL
3249 && elf_tdata (ibfd)->phdr != NULL)
3253 /* Only set up the segments when all the sections have been set
3255 for (s = ibfd->sections; s != NULL; s = s->next)
3256 if (s->output_section == NULL)
3260 if (! copy_private_bfd_data (ibfd, obfd))
3265 ihdr = &elf_section_data (isec)->this_hdr;
3266 ohdr = &elf_section_data (osec)->this_hdr;
3268 ohdr->sh_entsize = ihdr->sh_entsize;
3270 if (ihdr->sh_type == SHT_SYMTAB
3271 || ihdr->sh_type == SHT_DYNSYM
3272 || ihdr->sh_type == SHT_GNU_verneed
3273 || ihdr->sh_type == SHT_GNU_verdef)
3274 ohdr->sh_info = ihdr->sh_info;
3279 /* Copy private symbol information. If this symbol is in a section
3280 which we did not map into a BFD section, try to map the section
3281 index correctly. We use special macro definitions for the mapped
3282 section indices; these definitions are interpreted by the
3283 swap_out_syms function. */
3285 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3286 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3287 #define MAP_STRTAB (SHN_LORESERVE - 3)
3288 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3291 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3297 elf_symbol_type *isym, *osym;
3299 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3300 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3303 isym = elf_symbol_from (ibfd, isymarg);
3304 osym = elf_symbol_from (obfd, osymarg);
3308 && bfd_is_abs_section (isym->symbol.section))
3312 shndx = isym->internal_elf_sym.st_shndx;
3313 if (shndx == elf_onesymtab (ibfd))
3314 shndx = MAP_ONESYMTAB;
3315 else if (shndx == elf_dynsymtab (ibfd))
3316 shndx = MAP_DYNSYMTAB;
3317 else if (shndx == elf_tdata (ibfd)->strtab_section)
3319 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3320 shndx = MAP_SHSTRTAB;
3321 osym->internal_elf_sym.st_shndx = shndx;
3327 /* Swap out the symbols. */
3330 swap_out_syms (abfd, sttp)
3332 struct bfd_strtab_hash **sttp;
3334 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3336 if (!elf_map_symbols (abfd))
3339 /* Dump out the symtabs. */
3341 int symcount = bfd_get_symcount (abfd);
3342 asymbol **syms = bfd_get_outsymbols (abfd);
3343 struct bfd_strtab_hash *stt;
3344 Elf_Internal_Shdr *symtab_hdr;
3345 Elf_Internal_Shdr *symstrtab_hdr;
3346 char *outbound_syms;
3349 stt = _bfd_elf_stringtab_init ();
3353 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3354 symtab_hdr->sh_type = SHT_SYMTAB;
3355 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3356 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3357 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3358 symtab_hdr->sh_addralign = bed->s->file_align;
3360 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3361 symstrtab_hdr->sh_type = SHT_STRTAB;
3363 outbound_syms = bfd_alloc (abfd,
3364 (1 + symcount) * bed->s->sizeof_sym);
3365 if (outbound_syms == NULL)
3367 symtab_hdr->contents = (PTR) outbound_syms;
3369 /* now generate the data (for "contents") */
3371 /* Fill in zeroth symbol and swap it out. */
3372 Elf_Internal_Sym sym;
3378 sym.st_shndx = SHN_UNDEF;
3379 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3380 outbound_syms += bed->s->sizeof_sym;
3382 for (idx = 0; idx < symcount; idx++)
3384 Elf_Internal_Sym sym;
3385 bfd_vma value = syms[idx]->value;
3386 elf_symbol_type *type_ptr;
3387 flagword flags = syms[idx]->flags;
3390 if (flags & BSF_SECTION_SYM)
3391 /* Section symbols have no names. */
3395 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3398 if (sym.st_name == (unsigned long) -1)
3402 type_ptr = elf_symbol_from (abfd, syms[idx]);
3404 if (bfd_is_com_section (syms[idx]->section))
3406 /* ELF common symbols put the alignment into the `value' field,
3407 and the size into the `size' field. This is backwards from
3408 how BFD handles it, so reverse it here. */
3409 sym.st_size = value;
3410 if (type_ptr == NULL
3411 || type_ptr->internal_elf_sym.st_value == 0)
3412 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3414 sym.st_value = type_ptr->internal_elf_sym.st_value;
3415 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
3416 syms[idx]->section);
3420 asection *sec = syms[idx]->section;
3423 if (sec->output_section)
3425 value += sec->output_offset;
3426 sec = sec->output_section;
3429 sym.st_value = value;
3430 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
3432 if (bfd_is_abs_section (sec)
3434 && type_ptr->internal_elf_sym.st_shndx != 0)
3436 /* This symbol is in a real ELF section which we did
3437 not create as a BFD section. Undo the mapping done
3438 by copy_private_symbol_data. */
3439 shndx = type_ptr->internal_elf_sym.st_shndx;
3443 shndx = elf_onesymtab (abfd);
3446 shndx = elf_dynsymtab (abfd);
3449 shndx = elf_tdata (abfd)->strtab_section;
3452 shndx = elf_tdata (abfd)->shstrtab_section;
3460 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3466 /* Writing this would be a hell of a lot easier if
3467 we had some decent documentation on bfd, and
3468 knew what to expect of the library, and what to
3469 demand of applications. For example, it
3470 appears that `objcopy' might not set the
3471 section of a symbol to be a section that is
3472 actually in the output file. */
3473 sec2 = bfd_get_section_by_name (abfd, sec->name);
3474 BFD_ASSERT (sec2 != 0);
3475 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3476 BFD_ASSERT (shndx != -1);
3480 sym.st_shndx = shndx;
3483 if ((flags & BSF_FUNCTION) != 0)
3485 else if ((flags & BSF_OBJECT) != 0)
3490 if (bfd_is_com_section (syms[idx]->section))
3491 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
3492 else if (bfd_is_und_section (syms[idx]->section))
3493 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
3497 else if (flags & BSF_SECTION_SYM)
3498 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3499 else if (flags & BSF_FILE)
3500 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3503 int bind = STB_LOCAL;
3505 if (flags & BSF_LOCAL)
3507 else if (flags & BSF_WEAK)
3509 else if (flags & BSF_GLOBAL)
3512 sym.st_info = ELF_ST_INFO (bind, type);
3515 if (type_ptr != NULL)
3516 sym.st_other = type_ptr->internal_elf_sym.st_other;
3520 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3521 outbound_syms += bed->s->sizeof_sym;
3525 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3526 symstrtab_hdr->sh_type = SHT_STRTAB;
3528 symstrtab_hdr->sh_flags = 0;
3529 symstrtab_hdr->sh_addr = 0;
3530 symstrtab_hdr->sh_entsize = 0;
3531 symstrtab_hdr->sh_link = 0;
3532 symstrtab_hdr->sh_info = 0;
3533 symstrtab_hdr->sh_addralign = 1;
3539 /* Return the number of bytes required to hold the symtab vector.
3541 Note that we base it on the count plus 1, since we will null terminate
3542 the vector allocated based on this size. However, the ELF symbol table
3543 always has a dummy entry as symbol #0, so it ends up even. */
3546 _bfd_elf_get_symtab_upper_bound (abfd)
3551 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3553 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3554 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3560 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3565 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3567 if (elf_dynsymtab (abfd) == 0)
3569 bfd_set_error (bfd_error_invalid_operation);
3573 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3574 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3580 _bfd_elf_get_reloc_upper_bound (abfd, asect)
3584 return (asect->reloc_count + 1) * sizeof (arelent *);
3587 /* Canonicalize the relocs. */
3590 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3599 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
3605 tblptr = section->relocation;
3606 for (i = 0; i < section->reloc_count; i++)
3607 *relptr++ = tblptr++;
3611 return section->reloc_count;
3615 _bfd_elf_get_symtab (abfd, alocation)
3617 asymbol **alocation;
3619 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3622 bfd_get_symcount (abfd) = symcount;
3627 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3629 asymbol **alocation;
3631 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3634 /* Return the size required for the dynamic reloc entries. Any
3635 section that was actually installed in the BFD, and has type
3636 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
3637 considered to be a dynamic reloc section. */
3640 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
3646 if (elf_dynsymtab (abfd) == 0)
3648 bfd_set_error (bfd_error_invalid_operation);
3652 ret = sizeof (arelent *);
3653 for (s = abfd->sections; s != NULL; s = s->next)
3654 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3655 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3656 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3657 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
3658 * sizeof (arelent *));
3663 /* Canonicalize the dynamic relocation entries. Note that we return
3664 the dynamic relocations as a single block, although they are
3665 actually associated with particular sections; the interface, which
3666 was designed for SunOS style shared libraries, expects that there
3667 is only one set of dynamic relocs. Any section that was actually
3668 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
3669 the dynamic symbol table, is considered to be a dynamic reloc
3673 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
3678 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
3682 if (elf_dynsymtab (abfd) == 0)
3684 bfd_set_error (bfd_error_invalid_operation);
3688 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3690 for (s = abfd->sections; s != NULL; s = s->next)
3692 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3693 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3694 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3699 if (! (*slurp_relocs) (abfd, s, syms, true))
3701 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
3703 for (i = 0; i < count; i++)
3714 /* Read in the version information. */
3717 _bfd_elf_slurp_version_tables (abfd)
3720 bfd_byte *contents = NULL;
3722 if (elf_dynverdef (abfd) != 0)
3724 Elf_Internal_Shdr *hdr;
3725 Elf_External_Verdef *everdef;
3726 Elf_Internal_Verdef *iverdef;
3729 hdr = &elf_tdata (abfd)->dynverdef_hdr;
3731 elf_tdata (abfd)->verdef =
3732 ((Elf_Internal_Verdef *)
3733 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
3734 if (elf_tdata (abfd)->verdef == NULL)
3737 elf_tdata (abfd)->cverdefs = hdr->sh_info;
3739 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3740 if (contents == NULL)
3742 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3743 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
3746 everdef = (Elf_External_Verdef *) contents;
3747 iverdef = elf_tdata (abfd)->verdef;
3748 for (i = 0; i < hdr->sh_info; i++, iverdef++)
3750 Elf_External_Verdaux *everdaux;
3751 Elf_Internal_Verdaux *iverdaux;
3754 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
3756 iverdef->vd_bfd = abfd;
3758 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
3761 * sizeof (Elf_Internal_Verdaux))));
3762 if (iverdef->vd_auxptr == NULL)
3765 everdaux = ((Elf_External_Verdaux *)
3766 ((bfd_byte *) everdef + iverdef->vd_aux));
3767 iverdaux = iverdef->vd_auxptr;
3768 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
3770 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
3772 iverdaux->vda_nodename =
3773 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3774 iverdaux->vda_name);
3775 if (iverdaux->vda_nodename == NULL)
3778 if (j + 1 < iverdef->vd_cnt)
3779 iverdaux->vda_nextptr = iverdaux + 1;
3781 iverdaux->vda_nextptr = NULL;
3783 everdaux = ((Elf_External_Verdaux *)
3784 ((bfd_byte *) everdaux + iverdaux->vda_next));
3787 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
3789 if (i + 1 < hdr->sh_info)
3790 iverdef->vd_nextdef = iverdef + 1;
3792 iverdef->vd_nextdef = NULL;
3794 everdef = ((Elf_External_Verdef *)
3795 ((bfd_byte *) everdef + iverdef->vd_next));
3802 if (elf_dynverref (abfd) != 0)
3804 Elf_Internal_Shdr *hdr;
3805 Elf_External_Verneed *everneed;
3806 Elf_Internal_Verneed *iverneed;
3809 hdr = &elf_tdata (abfd)->dynverref_hdr;
3811 elf_tdata (abfd)->verref =
3812 ((Elf_Internal_Verneed *)
3813 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
3814 if (elf_tdata (abfd)->verref == NULL)
3817 elf_tdata (abfd)->cverrefs = hdr->sh_info;
3819 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3820 if (contents == NULL)
3822 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3823 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
3826 everneed = (Elf_External_Verneed *) contents;
3827 iverneed = elf_tdata (abfd)->verref;
3828 for (i = 0; i < hdr->sh_info; i++, iverneed++)
3830 Elf_External_Vernaux *evernaux;
3831 Elf_Internal_Vernaux *ivernaux;
3834 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
3836 iverneed->vn_bfd = abfd;
3838 iverneed->vn_filename =
3839 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3841 if (iverneed->vn_filename == NULL)
3844 iverneed->vn_auxptr =
3845 ((Elf_Internal_Vernaux *)
3847 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
3849 evernaux = ((Elf_External_Vernaux *)
3850 ((bfd_byte *) everneed + iverneed->vn_aux));
3851 ivernaux = iverneed->vn_auxptr;
3852 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
3854 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
3856 ivernaux->vna_nodename =
3857 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3858 ivernaux->vna_name);
3859 if (ivernaux->vna_nodename == NULL)
3862 if (j + 1 < iverneed->vn_cnt)
3863 ivernaux->vna_nextptr = ivernaux + 1;
3865 ivernaux->vna_nextptr = NULL;
3867 evernaux = ((Elf_External_Vernaux *)
3868 ((bfd_byte *) evernaux + ivernaux->vna_next));
3871 if (i + 1 < hdr->sh_info)
3872 iverneed->vn_nextref = iverneed + 1;
3874 iverneed->vn_nextref = NULL;
3876 everneed = ((Elf_External_Verneed *)
3877 ((bfd_byte *) everneed + iverneed->vn_next));
3887 if (contents == NULL)
3893 _bfd_elf_make_empty_symbol (abfd)
3896 elf_symbol_type *newsym;
3898 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3903 newsym->symbol.the_bfd = abfd;
3904 return &newsym->symbol;
3909 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3914 bfd_symbol_info (symbol, ret);
3917 /* Return whether a symbol name implies a local symbol. Most targets
3918 use this function for the is_local_label_name entry point, but some
3922 _bfd_elf_is_local_label_name (abfd, name)
3926 /* Normal local symbols start with ``.L''. */
3927 if (name[0] == '.' && name[1] == 'L')
3930 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
3931 DWARF debugging symbols starting with ``..''. */
3932 if (name[0] == '.' && name[1] == '.')
3935 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
3936 emitting DWARF debugging output. I suspect this is actually a
3937 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
3938 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
3939 underscore to be emitted on some ELF targets). For ease of use,
3940 we treat such symbols as local. */
3941 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
3948 _bfd_elf_get_lineno (ignore_abfd, symbol)
3957 _bfd_elf_set_arch_mach (abfd, arch, machine)
3959 enum bfd_architecture arch;
3960 unsigned long machine;
3962 /* If this isn't the right architecture for this backend, and this
3963 isn't the generic backend, fail. */
3964 if (arch != get_elf_backend_data (abfd)->arch
3965 && arch != bfd_arch_unknown
3966 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3969 return bfd_default_set_arch_mach (abfd, arch, machine);
3972 /* Find the nearest line to a particular section and offset, for error
3976 _bfd_elf_find_nearest_line (abfd,
3987 CONST char **filename_ptr;
3988 CONST char **functionname_ptr;
3989 unsigned int *line_ptr;
3992 const char *filename;
3997 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3998 &found, filename_ptr,
3999 functionname_ptr, line_ptr,
4000 &elf_tdata (abfd)->line_info))
4005 if (symbols == NULL)
4012 for (p = symbols; *p != NULL; p++)
4016 q = (elf_symbol_type *) *p;
4018 if (bfd_get_section (&q->symbol) != section)
4021 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4026 filename = bfd_asymbol_name (&q->symbol);
4029 if (q->symbol.section == section
4030 && q->symbol.value >= low_func
4031 && q->symbol.value <= offset)
4033 func = (asymbol *) q;
4034 low_func = q->symbol.value;
4043 *filename_ptr = filename;
4044 *functionname_ptr = bfd_asymbol_name (func);
4050 _bfd_elf_sizeof_headers (abfd, reloc)
4056 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4058 ret += get_program_header_size (abfd);
4063 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4068 bfd_size_type count;
4070 Elf_Internal_Shdr *hdr;
4072 if (! abfd->output_has_begun
4073 && ! _bfd_elf_compute_section_file_positions (abfd,
4074 (struct bfd_link_info *) NULL))
4077 hdr = &elf_section_data (section)->this_hdr;
4079 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4081 if (bfd_write (location, 1, count, abfd) != count)
4088 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4091 Elf_Internal_Rela *dst;
4098 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4101 Elf_Internal_Rel *dst;
4107 /* Try to convert a non-ELF reloc into an ELF one. */
4110 _bfd_elf_validate_reloc (abfd, areloc)
4114 /* Check whether we really have an ELF howto. */
4116 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4118 bfd_reloc_code_real_type code;
4119 reloc_howto_type *howto;
4121 /* Alien reloc: Try to determine its type to replace it with an
4122 equivalent ELF reloc. */
4124 if (areloc->howto->pc_relative)
4126 switch (areloc->howto->bitsize)
4129 code = BFD_RELOC_8_PCREL;
4132 code = BFD_RELOC_12_PCREL;
4135 code = BFD_RELOC_16_PCREL;
4138 code = BFD_RELOC_24_PCREL;
4141 code = BFD_RELOC_32_PCREL;
4144 code = BFD_RELOC_64_PCREL;
4150 howto = bfd_reloc_type_lookup (abfd, code);
4152 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4154 if (howto->pcrel_offset)
4155 areloc->addend += areloc->address;
4157 areloc->addend -= areloc->address; /* addend is unsigned!! */
4162 switch (areloc->howto->bitsize)
4168 code = BFD_RELOC_14;
4171 code = BFD_RELOC_16;
4174 code = BFD_RELOC_26;
4177 code = BFD_RELOC_32;
4180 code = BFD_RELOC_64;
4186 howto = bfd_reloc_type_lookup (abfd, code);
4190 areloc->howto = howto;
4198 (*_bfd_error_handler)
4199 ("%s: unsupported relocation type %s",
4200 bfd_get_filename (abfd), areloc->howto->name);
4201 bfd_set_error (bfd_error_bad_value);