1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 1998 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 **, int));
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. */
205 CONST unsigned char *name;
211 while ((ch = *name++) != '\0')
214 if ((g = (h & 0xf0000000)) != 0)
217 /* The ELF ABI says `h &= ~g', but this is equivalent in
218 this case and on some machines one insn instead of two. */
225 /* Read a specified number of bytes at a specified offset in an ELF
226 file, into a newly allocated buffer, and return a pointer to the
230 elf_read (abfd, offset, size)
237 if ((buf = bfd_alloc (abfd, size)) == NULL)
239 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
241 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
243 if (bfd_get_error () != bfd_error_system_call)
244 bfd_set_error (bfd_error_file_truncated);
251 bfd_elf_mkobject (abfd)
254 /* this just does initialization */
255 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
256 elf_tdata (abfd) = (struct elf_obj_tdata *)
257 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
258 if (elf_tdata (abfd) == 0)
260 /* since everything is done at close time, do we need any
267 bfd_elf_mkcorefile (abfd)
270 /* I think this can be done just like an object file. */
271 return bfd_elf_mkobject (abfd);
275 bfd_elf_get_str_section (abfd, shindex)
277 unsigned int shindex;
279 Elf_Internal_Shdr **i_shdrp;
280 char *shstrtab = NULL;
282 unsigned int shstrtabsize;
284 i_shdrp = elf_elfsections (abfd);
285 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
288 shstrtab = (char *) i_shdrp[shindex]->contents;
289 if (shstrtab == NULL)
291 /* No cached one, attempt to read, and cache what we read. */
292 offset = i_shdrp[shindex]->sh_offset;
293 shstrtabsize = i_shdrp[shindex]->sh_size;
294 shstrtab = elf_read (abfd, offset, shstrtabsize);
295 i_shdrp[shindex]->contents = (PTR) shstrtab;
301 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
303 unsigned int shindex;
304 unsigned int strindex;
306 Elf_Internal_Shdr *hdr;
311 hdr = elf_elfsections (abfd)[shindex];
313 if (hdr->contents == NULL
314 && bfd_elf_get_str_section (abfd, shindex) == NULL)
317 if (strindex >= hdr->sh_size)
319 (*_bfd_error_handler)
320 (_("%s: invalid string offset %u >= %lu for section `%s'"),
321 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
322 ((shindex == elf_elfheader(abfd)->e_shstrndx
323 && strindex == hdr->sh_name)
325 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
329 return ((char *) hdr->contents) + strindex;
332 /* Make a BFD section from an ELF section. We store a pointer to the
333 BFD section in the bfd_section field of the header. */
336 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
338 Elf_Internal_Shdr *hdr;
344 if (hdr->bfd_section != NULL)
346 BFD_ASSERT (strcmp (name,
347 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
351 newsect = bfd_make_section_anyway (abfd, name);
355 newsect->filepos = hdr->sh_offset;
357 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
358 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
359 || ! bfd_set_section_alignment (abfd, newsect,
360 bfd_log2 (hdr->sh_addralign)))
363 flags = SEC_NO_FLAGS;
364 if (hdr->sh_type != SHT_NOBITS)
365 flags |= SEC_HAS_CONTENTS;
366 if ((hdr->sh_flags & SHF_ALLOC) != 0)
369 if (hdr->sh_type != SHT_NOBITS)
372 if ((hdr->sh_flags & SHF_WRITE) == 0)
373 flags |= SEC_READONLY;
374 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
376 else if ((flags & SEC_LOAD) != 0)
379 /* The debugging sections appear to be recognized only by name, not
381 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
382 || strncmp (name, ".line", sizeof ".line" - 1) == 0
383 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
384 flags |= SEC_DEBUGGING;
386 /* As a GNU extension, if the name begins with .gnu.linkonce, we
387 only link a single copy of the section. This is used to support
388 g++. g++ will emit each template expansion in its own section.
389 The symbols will be defined as weak, so that multiple definitions
390 are permitted. The GNU linker extension is to actually discard
391 all but one of the sections. */
392 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
393 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
395 if (! bfd_set_section_flags (abfd, newsect, flags))
398 if ((flags & SEC_ALLOC) != 0)
400 Elf_Internal_Phdr *phdr;
403 /* Look through the phdrs to see if we need to adjust the lma.
404 If all the p_paddr fields are zero, we ignore them, since
405 some ELF linkers produce such output. */
406 phdr = elf_tdata (abfd)->phdr;
407 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
409 if (phdr->p_paddr != 0)
412 if (i < elf_elfheader (abfd)->e_phnum)
414 phdr = elf_tdata (abfd)->phdr;
415 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
417 if (phdr->p_type == PT_LOAD
418 && phdr->p_vaddr != phdr->p_paddr
419 && phdr->p_vaddr <= hdr->sh_addr
420 && (phdr->p_vaddr + phdr->p_memsz
421 >= hdr->sh_addr + hdr->sh_size)
422 && ((flags & SEC_LOAD) == 0
423 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
424 && (phdr->p_offset + phdr->p_filesz
425 >= hdr->sh_offset + hdr->sh_size))))
427 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
434 hdr->bfd_section = newsect;
435 elf_section_data (newsect)->this_hdr = *hdr;
445 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
448 Helper functions for GDB to locate the string tables.
449 Since BFD hides string tables from callers, GDB needs to use an
450 internal hook to find them. Sun's .stabstr, in particular,
451 isn't even pointed to by the .stab section, so ordinary
452 mechanisms wouldn't work to find it, even if we had some.
455 struct elf_internal_shdr *
456 bfd_elf_find_section (abfd, name)
460 Elf_Internal_Shdr **i_shdrp;
465 i_shdrp = elf_elfsections (abfd);
468 shstrtab = bfd_elf_get_str_section
469 (abfd, elf_elfheader (abfd)->e_shstrndx);
470 if (shstrtab != NULL)
472 max = elf_elfheader (abfd)->e_shnum;
473 for (i = 1; i < max; i++)
474 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
481 const char *const bfd_elf_section_type_names[] = {
482 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
483 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
484 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
487 /* ELF relocs are against symbols. If we are producing relocateable
488 output, and the reloc is against an external symbol, and nothing
489 has given us any additional addend, the resulting reloc will also
490 be against the same symbol. In such a case, we don't want to
491 change anything about the way the reloc is handled, since it will
492 all be done at final link time. Rather than put special case code
493 into bfd_perform_relocation, all the reloc types use this howto
494 function. It just short circuits the reloc if producing
495 relocateable output against an external symbol. */
498 bfd_reloc_status_type
499 bfd_elf_generic_reloc (abfd,
507 arelent *reloc_entry;
510 asection *input_section;
512 char **error_message;
514 if (output_bfd != (bfd *) NULL
515 && (symbol->flags & BSF_SECTION_SYM) == 0
516 && (! reloc_entry->howto->partial_inplace
517 || reloc_entry->addend == 0))
519 reloc_entry->address += input_section->output_offset;
523 return bfd_reloc_continue;
526 /* Print out the program headers. */
529 _bfd_elf_print_private_bfd_data (abfd, farg)
533 FILE *f = (FILE *) farg;
534 Elf_Internal_Phdr *p;
536 bfd_byte *dynbuf = NULL;
538 p = elf_tdata (abfd)->phdr;
543 fprintf (f, _("\nProgram Header:\n"));
544 c = elf_elfheader (abfd)->e_phnum;
545 for (i = 0; i < c; i++, p++)
552 case PT_NULL: s = "NULL"; break;
553 case PT_LOAD: s = "LOAD"; break;
554 case PT_DYNAMIC: s = "DYNAMIC"; break;
555 case PT_INTERP: s = "INTERP"; break;
556 case PT_NOTE: s = "NOTE"; break;
557 case PT_SHLIB: s = "SHLIB"; break;
558 case PT_PHDR: s = "PHDR"; break;
559 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
561 fprintf (f, "%8s off 0x", s);
562 fprintf_vma (f, p->p_offset);
563 fprintf (f, " vaddr 0x");
564 fprintf_vma (f, p->p_vaddr);
565 fprintf (f, " paddr 0x");
566 fprintf_vma (f, p->p_paddr);
567 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
568 fprintf (f, " filesz 0x");
569 fprintf_vma (f, p->p_filesz);
570 fprintf (f, " memsz 0x");
571 fprintf_vma (f, p->p_memsz);
572 fprintf (f, " flags %c%c%c",
573 (p->p_flags & PF_R) != 0 ? 'r' : '-',
574 (p->p_flags & PF_W) != 0 ? 'w' : '-',
575 (p->p_flags & PF_X) != 0 ? 'x' : '-');
576 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
577 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
582 s = bfd_get_section_by_name (abfd, ".dynamic");
587 bfd_byte *extdyn, *extdynend;
589 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
591 fprintf (f, _("\nDynamic Section:\n"));
593 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
596 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
600 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
603 link = elf_elfsections (abfd)[elfsec]->sh_link;
605 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
606 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
609 extdynend = extdyn + s->_raw_size;
610 for (; extdyn < extdynend; extdyn += extdynsize)
612 Elf_Internal_Dyn dyn;
617 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
619 if (dyn.d_tag == DT_NULL)
626 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
630 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
631 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
632 case DT_PLTGOT: name = "PLTGOT"; break;
633 case DT_HASH: name = "HASH"; break;
634 case DT_STRTAB: name = "STRTAB"; break;
635 case DT_SYMTAB: name = "SYMTAB"; break;
636 case DT_RELA: name = "RELA"; break;
637 case DT_RELASZ: name = "RELASZ"; break;
638 case DT_RELAENT: name = "RELAENT"; break;
639 case DT_STRSZ: name = "STRSZ"; break;
640 case DT_SYMENT: name = "SYMENT"; break;
641 case DT_INIT: name = "INIT"; break;
642 case DT_FINI: name = "FINI"; break;
643 case DT_SONAME: name = "SONAME"; stringp = true; break;
644 case DT_RPATH: name = "RPATH"; stringp = true; break;
645 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
646 case DT_REL: name = "REL"; break;
647 case DT_RELSZ: name = "RELSZ"; break;
648 case DT_RELENT: name = "RELENT"; break;
649 case DT_PLTREL: name = "PLTREL"; break;
650 case DT_DEBUG: name = "DEBUG"; break;
651 case DT_TEXTREL: name = "TEXTREL"; break;
652 case DT_JMPREL: name = "JMPREL"; break;
653 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
654 case DT_FILTER: name = "FILTER"; stringp = true; break;
655 case DT_VERSYM: name = "VERSYM"; break;
656 case DT_VERDEF: name = "VERDEF"; break;
657 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
658 case DT_VERNEED: name = "VERNEED"; break;
659 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
662 fprintf (f, " %-11s ", name);
664 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
669 string = bfd_elf_string_from_elf_section (abfd, link,
673 fprintf (f, "%s", string);
682 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
683 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
685 if (! _bfd_elf_slurp_version_tables (abfd))
689 if (elf_dynverdef (abfd) != 0)
691 Elf_Internal_Verdef *t;
693 fprintf (f, _("\nVersion definitions:\n"));
694 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
696 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
697 t->vd_flags, t->vd_hash, t->vd_nodename);
698 if (t->vd_auxptr->vda_nextptr != NULL)
700 Elf_Internal_Verdaux *a;
703 for (a = t->vd_auxptr->vda_nextptr;
706 fprintf (f, "%s ", a->vda_nodename);
712 if (elf_dynverref (abfd) != 0)
714 Elf_Internal_Verneed *t;
716 fprintf (f, _("\nVersion References:\n"));
717 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
719 Elf_Internal_Vernaux *a;
721 fprintf (f, _(" required from %s:\n"), t->vn_filename);
722 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
723 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
724 a->vna_flags, a->vna_other, a->vna_nodename);
736 /* Display ELF-specific fields of a symbol. */
739 bfd_elf_print_symbol (abfd, filep, symbol, how)
743 bfd_print_symbol_type how;
745 FILE *file = (FILE *) filep;
748 case bfd_print_symbol_name:
749 fprintf (file, "%s", symbol->name);
751 case bfd_print_symbol_more:
752 fprintf (file, "elf ");
753 fprintf_vma (file, symbol->value);
754 fprintf (file, " %lx", (long) symbol->flags);
756 case bfd_print_symbol_all:
758 CONST char *section_name;
759 section_name = symbol->section ? symbol->section->name : "(*none*)";
760 bfd_print_symbol_vandf ((PTR) file, symbol);
761 fprintf (file, " %s\t", section_name);
762 /* Print the "other" value for a symbol. For common symbols,
763 we've already printed the size; now print the alignment.
764 For other symbols, we have no specified alignment, and
765 we've printed the address; now print the size. */
767 (bfd_is_com_section (symbol->section)
768 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
769 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
771 /* If we have version information, print it. */
772 if (elf_tdata (abfd)->dynversym_section != 0
773 && (elf_tdata (abfd)->dynverdef_section != 0
774 || elf_tdata (abfd)->dynverref_section != 0))
777 const char *version_string;
779 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
783 else if (vernum == 1)
784 version_string = "Base";
785 else if (vernum <= elf_tdata (abfd)->cverdefs)
787 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
790 Elf_Internal_Verneed *t;
793 for (t = elf_tdata (abfd)->verref;
797 Elf_Internal_Vernaux *a;
799 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
801 if (a->vna_other == vernum)
803 version_string = a->vna_nodename;
810 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
811 fprintf (file, " %-11s", version_string);
816 fprintf (file, " (%s)", version_string);
817 for (i = 10 - strlen (version_string); i > 0; --i)
822 /* If the st_other field is not zero, print it. */
823 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
824 fprintf (file, " 0x%02x",
826 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
828 fprintf (file, " %s", symbol->name);
834 /* Create an entry in an ELF linker hash table. */
836 struct bfd_hash_entry *
837 _bfd_elf_link_hash_newfunc (entry, table, string)
838 struct bfd_hash_entry *entry;
839 struct bfd_hash_table *table;
842 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
844 /* Allocate the structure if it has not already been allocated by a
846 if (ret == (struct elf_link_hash_entry *) NULL)
847 ret = ((struct elf_link_hash_entry *)
848 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
849 if (ret == (struct elf_link_hash_entry *) NULL)
850 return (struct bfd_hash_entry *) ret;
852 /* Call the allocation method of the superclass. */
853 ret = ((struct elf_link_hash_entry *)
854 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
856 if (ret != (struct elf_link_hash_entry *) NULL)
858 /* Set local fields. */
862 ret->dynstr_index = 0;
864 ret->got.offset = (bfd_vma) -1;
865 ret->plt.offset = (bfd_vma) -1;
866 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
867 ret->verinfo.verdef = NULL;
868 ret->vtable_entries_used = NULL;
869 ret->vtable_parent = NULL;
870 ret->type = STT_NOTYPE;
872 /* Assume that we have been called by a non-ELF symbol reader.
873 This flag is then reset by the code which reads an ELF input
874 file. This ensures that a symbol created by a non-ELF symbol
875 reader will have the flag set correctly. */
876 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
879 return (struct bfd_hash_entry *) ret;
882 /* Initialize an ELF linker hash table. */
885 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
886 struct elf_link_hash_table *table;
888 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
889 struct bfd_hash_table *,
892 table->dynamic_sections_created = false;
893 table->dynobj = NULL;
894 /* The first dynamic symbol is a dummy. */
895 table->dynsymcount = 1;
896 table->dynstr = NULL;
897 table->bucketcount = 0;
898 table->needed = NULL;
900 table->stab_info = NULL;
901 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
904 /* Create an ELF linker hash table. */
906 struct bfd_link_hash_table *
907 _bfd_elf_link_hash_table_create (abfd)
910 struct elf_link_hash_table *ret;
912 ret = ((struct elf_link_hash_table *)
913 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
914 if (ret == (struct elf_link_hash_table *) NULL)
917 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
919 bfd_release (abfd, ret);
926 /* This is a hook for the ELF emulation code in the generic linker to
927 tell the backend linker what file name to use for the DT_NEEDED
928 entry for a dynamic object. The generic linker passes name as an
929 empty string to indicate that no DT_NEEDED entry should be made. */
932 bfd_elf_set_dt_needed_name (abfd, name)
936 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
937 && bfd_get_format (abfd) == bfd_object)
938 elf_dt_name (abfd) = name;
941 /* Get the list of DT_NEEDED entries for a link. This is a hook for
942 the linker ELF emulation code. */
944 struct bfd_link_needed_list *
945 bfd_elf_get_needed_list (abfd, info)
947 struct bfd_link_info *info;
949 if (info->hash->creator->flavour != bfd_target_elf_flavour)
951 return elf_hash_table (info)->needed;
954 /* Get the name actually used for a dynamic object for a link. This
955 is the SONAME entry if there is one. Otherwise, it is the string
956 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
959 bfd_elf_get_dt_soname (abfd)
962 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
963 && bfd_get_format (abfd) == bfd_object)
964 return elf_dt_name (abfd);
968 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
969 the ELF linker emulation code. */
972 bfd_elf_get_bfd_needed_list (abfd, pneeded)
974 struct bfd_link_needed_list **pneeded;
977 bfd_byte *dynbuf = NULL;
980 bfd_byte *extdyn, *extdynend;
982 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
986 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
987 || bfd_get_format (abfd) != bfd_object)
990 s = bfd_get_section_by_name (abfd, ".dynamic");
991 if (s == NULL || s->_raw_size == 0)
994 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
998 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1002 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1006 link = elf_elfsections (abfd)[elfsec]->sh_link;
1008 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1009 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1012 extdynend = extdyn + s->_raw_size;
1013 for (; extdyn < extdynend; extdyn += extdynsize)
1015 Elf_Internal_Dyn dyn;
1017 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1019 if (dyn.d_tag == DT_NULL)
1022 if (dyn.d_tag == DT_NEEDED)
1025 struct bfd_link_needed_list *l;
1027 string = bfd_elf_string_from_elf_section (abfd, link,
1032 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1053 /* Allocate an ELF string table--force the first byte to be zero. */
1055 struct bfd_strtab_hash *
1056 _bfd_elf_stringtab_init ()
1058 struct bfd_strtab_hash *ret;
1060 ret = _bfd_stringtab_init ();
1065 loc = _bfd_stringtab_add (ret, "", true, false);
1066 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1067 if (loc == (bfd_size_type) -1)
1069 _bfd_stringtab_free (ret);
1076 /* ELF .o/exec file reading */
1078 /* Create a new bfd section from an ELF section header. */
1081 bfd_section_from_shdr (abfd, shindex)
1083 unsigned int shindex;
1085 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1086 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1087 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1090 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1092 switch (hdr->sh_type)
1095 /* Inactive section. Throw it away. */
1098 case SHT_PROGBITS: /* Normal section with contents. */
1099 case SHT_DYNAMIC: /* Dynamic linking information. */
1100 case SHT_NOBITS: /* .bss section. */
1101 case SHT_HASH: /* .hash section. */
1102 case SHT_NOTE: /* .note section. */
1103 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1105 case SHT_SYMTAB: /* A symbol table */
1106 if (elf_onesymtab (abfd) == shindex)
1109 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1110 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1111 elf_onesymtab (abfd) = shindex;
1112 elf_tdata (abfd)->symtab_hdr = *hdr;
1113 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1114 abfd->flags |= HAS_SYMS;
1116 /* Sometimes a shared object will map in the symbol table. If
1117 SHF_ALLOC is set, and this is a shared object, then we also
1118 treat this section as a BFD section. We can not base the
1119 decision purely on SHF_ALLOC, because that flag is sometimes
1120 set in a relocateable object file, which would confuse the
1122 if ((hdr->sh_flags & SHF_ALLOC) != 0
1123 && (abfd->flags & DYNAMIC) != 0
1124 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1129 case SHT_DYNSYM: /* A dynamic symbol table */
1130 if (elf_dynsymtab (abfd) == shindex)
1133 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1134 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1135 elf_dynsymtab (abfd) = shindex;
1136 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1137 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1138 abfd->flags |= HAS_SYMS;
1140 /* Besides being a symbol table, we also treat this as a regular
1141 section, so that objcopy can handle it. */
1142 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1144 case SHT_STRTAB: /* A string table */
1145 if (hdr->bfd_section != NULL)
1147 if (ehdr->e_shstrndx == shindex)
1149 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1150 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1156 for (i = 1; i < ehdr->e_shnum; i++)
1158 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1159 if (hdr2->sh_link == shindex)
1161 if (! bfd_section_from_shdr (abfd, i))
1163 if (elf_onesymtab (abfd) == i)
1165 elf_tdata (abfd)->strtab_hdr = *hdr;
1166 elf_elfsections (abfd)[shindex] =
1167 &elf_tdata (abfd)->strtab_hdr;
1170 if (elf_dynsymtab (abfd) == i)
1172 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1173 elf_elfsections (abfd)[shindex] = hdr =
1174 &elf_tdata (abfd)->dynstrtab_hdr;
1175 /* We also treat this as a regular section, so
1176 that objcopy can handle it. */
1179 #if 0 /* Not handling other string tables specially right now. */
1180 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1181 /* We have a strtab for some random other section. */
1182 newsect = (asection *) hdr2->bfd_section;
1185 hdr->bfd_section = newsect;
1186 hdr2 = &elf_section_data (newsect)->str_hdr;
1188 elf_elfsections (abfd)[shindex] = hdr2;
1194 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1198 /* *These* do a lot of work -- but build no sections! */
1200 asection *target_sect;
1201 Elf_Internal_Shdr *hdr2;
1203 /* For some incomprehensible reason Oracle distributes
1204 libraries for Solaris in which some of the objects have
1205 bogus sh_link fields. It would be nice if we could just
1206 reject them, but, unfortunately, some people need to use
1207 them. We scan through the section headers; if we find only
1208 one suitable symbol table, we clobber the sh_link to point
1209 to it. I hope this doesn't break anything. */
1210 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1211 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1217 for (scan = 1; scan < ehdr->e_shnum; scan++)
1219 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1220 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1231 hdr->sh_link = found;
1234 /* Get the symbol table. */
1235 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1236 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1239 /* If this reloc section does not use the main symbol table we
1240 don't treat it as a reloc section. BFD can't adequately
1241 represent such a section, so at least for now, we don't
1242 try. We just present it as a normal section. */
1243 if (hdr->sh_link != elf_onesymtab (abfd))
1244 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1246 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1248 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1249 if (target_sect == NULL)
1252 if ((target_sect->flags & SEC_RELOC) == 0
1253 || target_sect->reloc_count == 0)
1254 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1257 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1258 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1259 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1262 elf_elfsections (abfd)[shindex] = hdr2;
1263 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1264 target_sect->flags |= SEC_RELOC;
1265 target_sect->relocation = NULL;
1266 target_sect->rel_filepos = hdr->sh_offset;
1267 abfd->flags |= HAS_RELOC;
1272 case SHT_GNU_verdef:
1273 elf_dynverdef (abfd) = shindex;
1274 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1275 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1278 case SHT_GNU_versym:
1279 elf_dynversym (abfd) = shindex;
1280 elf_tdata (abfd)->dynversym_hdr = *hdr;
1281 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1284 case SHT_GNU_verneed:
1285 elf_dynverref (abfd) = shindex;
1286 elf_tdata (abfd)->dynverref_hdr = *hdr;
1287 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1294 /* Check for any processor-specific section types. */
1296 if (bed->elf_backend_section_from_shdr)
1297 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1305 /* Given an ELF section number, retrieve the corresponding BFD
1309 bfd_section_from_elf_index (abfd, index)
1313 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1314 if (index >= elf_elfheader (abfd)->e_shnum)
1316 return elf_elfsections (abfd)[index]->bfd_section;
1320 _bfd_elf_new_section_hook (abfd, sec)
1324 struct bfd_elf_section_data *sdata;
1326 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
1329 sec->used_by_bfd = (PTR) sdata;
1330 memset (sdata, 0, sizeof (*sdata));
1334 /* Create a new bfd section from an ELF program header.
1336 Since program segments have no names, we generate a synthetic name
1337 of the form segment<NUM>, where NUM is generally the index in the
1338 program header table. For segments that are split (see below) we
1339 generate the names segment<NUM>a and segment<NUM>b.
1341 Note that some program segments may have a file size that is different than
1342 (less than) the memory size. All this means is that at execution the
1343 system must allocate the amount of memory specified by the memory size,
1344 but only initialize it with the first "file size" bytes read from the
1345 file. This would occur for example, with program segments consisting
1346 of combined data+bss.
1348 To handle the above situation, this routine generates TWO bfd sections
1349 for the single program segment. The first has the length specified by
1350 the file size of the segment, and the second has the length specified
1351 by the difference between the two sizes. In effect, the segment is split
1352 into it's initialized and uninitialized parts.
1357 bfd_section_from_phdr (abfd, hdr, index)
1359 Elf_Internal_Phdr *hdr;
1367 split = ((hdr->p_memsz > 0)
1368 && (hdr->p_filesz > 0)
1369 && (hdr->p_memsz > hdr->p_filesz));
1370 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1371 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1374 strcpy (name, namebuf);
1375 newsect = bfd_make_section (abfd, name);
1376 if (newsect == NULL)
1378 newsect->vma = hdr->p_vaddr;
1379 newsect->lma = hdr->p_paddr;
1380 newsect->_raw_size = hdr->p_filesz;
1381 newsect->filepos = hdr->p_offset;
1382 newsect->flags |= SEC_HAS_CONTENTS;
1383 if (hdr->p_type == PT_LOAD)
1385 newsect->flags |= SEC_ALLOC;
1386 newsect->flags |= SEC_LOAD;
1387 if (hdr->p_flags & PF_X)
1389 /* FIXME: all we known is that it has execute PERMISSION,
1391 newsect->flags |= SEC_CODE;
1394 if (!(hdr->p_flags & PF_W))
1396 newsect->flags |= SEC_READONLY;
1401 sprintf (namebuf, "segment%db", index);
1402 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1405 strcpy (name, namebuf);
1406 newsect = bfd_make_section (abfd, name);
1407 if (newsect == NULL)
1409 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1410 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1411 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1412 if (hdr->p_type == PT_LOAD)
1414 newsect->flags |= SEC_ALLOC;
1415 if (hdr->p_flags & PF_X)
1416 newsect->flags |= SEC_CODE;
1418 if (!(hdr->p_flags & PF_W))
1419 newsect->flags |= SEC_READONLY;
1425 /* Set up an ELF internal section header for a section. */
1429 elf_fake_sections (abfd, asect, failedptrarg)
1434 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1435 boolean *failedptr = (boolean *) failedptrarg;
1436 Elf_Internal_Shdr *this_hdr;
1440 /* We already failed; just get out of the bfd_map_over_sections
1445 this_hdr = &elf_section_data (asect)->this_hdr;
1447 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1450 if (this_hdr->sh_name == (unsigned long) -1)
1456 this_hdr->sh_flags = 0;
1458 if ((asect->flags & SEC_ALLOC) != 0
1459 || asect->user_set_vma)
1460 this_hdr->sh_addr = asect->vma;
1462 this_hdr->sh_addr = 0;
1464 this_hdr->sh_offset = 0;
1465 this_hdr->sh_size = asect->_raw_size;
1466 this_hdr->sh_link = 0;
1467 this_hdr->sh_addralign = 1 << asect->alignment_power;
1468 /* The sh_entsize and sh_info fields may have been set already by
1469 copy_private_section_data. */
1471 this_hdr->bfd_section = asect;
1472 this_hdr->contents = NULL;
1474 /* FIXME: This should not be based on section names. */
1475 if (strcmp (asect->name, ".dynstr") == 0)
1476 this_hdr->sh_type = SHT_STRTAB;
1477 else if (strcmp (asect->name, ".hash") == 0)
1479 this_hdr->sh_type = SHT_HASH;
1480 this_hdr->sh_entsize = bed->s->arch_size / 8;
1482 else if (strcmp (asect->name, ".dynsym") == 0)
1484 this_hdr->sh_type = SHT_DYNSYM;
1485 this_hdr->sh_entsize = bed->s->sizeof_sym;
1487 else if (strcmp (asect->name, ".dynamic") == 0)
1489 this_hdr->sh_type = SHT_DYNAMIC;
1490 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1492 else if (strncmp (asect->name, ".rela", 5) == 0
1493 && get_elf_backend_data (abfd)->use_rela_p)
1495 this_hdr->sh_type = SHT_RELA;
1496 this_hdr->sh_entsize = bed->s->sizeof_rela;
1498 else if (strncmp (asect->name, ".rel", 4) == 0
1499 && ! get_elf_backend_data (abfd)->use_rela_p)
1501 this_hdr->sh_type = SHT_REL;
1502 this_hdr->sh_entsize = bed->s->sizeof_rel;
1504 else if (strncmp (asect->name, ".note", 5) == 0)
1505 this_hdr->sh_type = SHT_NOTE;
1506 else if (strncmp (asect->name, ".stab", 5) == 0
1507 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1508 this_hdr->sh_type = SHT_STRTAB;
1509 else if (strcmp (asect->name, ".gnu.version") == 0)
1511 this_hdr->sh_type = SHT_GNU_versym;
1512 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1514 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1516 this_hdr->sh_type = SHT_GNU_verdef;
1517 this_hdr->sh_entsize = 0;
1518 /* objcopy or strip will copy over sh_info, but may not set
1519 cverdefs. The linker will set cverdefs, but sh_info will be
1521 if (this_hdr->sh_info == 0)
1522 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1524 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1525 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1527 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1529 this_hdr->sh_type = SHT_GNU_verneed;
1530 this_hdr->sh_entsize = 0;
1531 /* objcopy or strip will copy over sh_info, but may not set
1532 cverrefs. The linker will set cverrefs, but sh_info will be
1534 if (this_hdr->sh_info == 0)
1535 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1537 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1538 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1540 else if ((asect->flags & SEC_ALLOC) != 0
1541 && (asect->flags & SEC_LOAD) != 0)
1542 this_hdr->sh_type = SHT_PROGBITS;
1543 else if ((asect->flags & SEC_ALLOC) != 0
1544 && ((asect->flags & SEC_LOAD) == 0))
1545 this_hdr->sh_type = SHT_NOBITS;
1549 this_hdr->sh_type = SHT_PROGBITS;
1552 if ((asect->flags & SEC_ALLOC) != 0)
1553 this_hdr->sh_flags |= SHF_ALLOC;
1554 if ((asect->flags & SEC_READONLY) == 0)
1555 this_hdr->sh_flags |= SHF_WRITE;
1556 if ((asect->flags & SEC_CODE) != 0)
1557 this_hdr->sh_flags |= SHF_EXECINSTR;
1559 /* Check for processor-specific section types. */
1561 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1563 if (bed->elf_backend_fake_sections)
1564 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1567 /* If the section has relocs, set up a section header for the
1568 SHT_REL[A] section. */
1569 if ((asect->flags & SEC_RELOC) != 0)
1571 Elf_Internal_Shdr *rela_hdr;
1572 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1575 rela_hdr = &elf_section_data (asect)->rel_hdr;
1576 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1582 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1584 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1586 if (rela_hdr->sh_name == (unsigned int) -1)
1591 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1592 rela_hdr->sh_entsize = (use_rela_p
1593 ? bed->s->sizeof_rela
1594 : bed->s->sizeof_rel);
1595 rela_hdr->sh_addralign = bed->s->file_align;
1596 rela_hdr->sh_flags = 0;
1597 rela_hdr->sh_addr = 0;
1598 rela_hdr->sh_size = 0;
1599 rela_hdr->sh_offset = 0;
1603 /* Assign all ELF section numbers. The dummy first section is handled here
1604 too. The link/info pointers for the standard section types are filled
1605 in here too, while we're at it. */
1608 assign_section_numbers (abfd)
1611 struct elf_obj_tdata *t = elf_tdata (abfd);
1613 unsigned int section_number;
1614 Elf_Internal_Shdr **i_shdrp;
1615 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1619 for (sec = abfd->sections; sec; sec = sec->next)
1621 struct bfd_elf_section_data *d = elf_section_data (sec);
1623 d->this_idx = section_number++;
1624 if ((sec->flags & SEC_RELOC) == 0)
1627 d->rel_idx = section_number++;
1630 t->shstrtab_section = section_number++;
1631 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1632 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1634 if (abfd->symcount > 0)
1636 t->symtab_section = section_number++;
1637 t->strtab_section = section_number++;
1640 elf_elfheader (abfd)->e_shnum = section_number;
1642 /* Set up the list of section header pointers, in agreement with the
1644 i_shdrp = ((Elf_Internal_Shdr **)
1645 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1646 if (i_shdrp == NULL)
1649 i_shdrp[0] = ((Elf_Internal_Shdr *)
1650 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1651 if (i_shdrp[0] == NULL)
1653 bfd_release (abfd, i_shdrp);
1656 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1658 elf_elfsections (abfd) = i_shdrp;
1660 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1661 if (abfd->symcount > 0)
1663 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1664 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1665 t->symtab_hdr.sh_link = t->strtab_section;
1667 for (sec = abfd->sections; sec; sec = sec->next)
1669 struct bfd_elf_section_data *d = elf_section_data (sec);
1673 i_shdrp[d->this_idx] = &d->this_hdr;
1674 if (d->rel_idx != 0)
1675 i_shdrp[d->rel_idx] = &d->rel_hdr;
1677 /* Fill in the sh_link and sh_info fields while we're at it. */
1679 /* sh_link of a reloc section is the section index of the symbol
1680 table. sh_info is the section index of the section to which
1681 the relocation entries apply. */
1682 if (d->rel_idx != 0)
1684 d->rel_hdr.sh_link = t->symtab_section;
1685 d->rel_hdr.sh_info = d->this_idx;
1688 switch (d->this_hdr.sh_type)
1692 /* A reloc section which we are treating as a normal BFD
1693 section. sh_link is the section index of the symbol
1694 table. sh_info is the section index of the section to
1695 which the relocation entries apply. We assume that an
1696 allocated reloc section uses the dynamic symbol table.
1697 FIXME: How can we be sure? */
1698 s = bfd_get_section_by_name (abfd, ".dynsym");
1700 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1702 /* We look up the section the relocs apply to by name. */
1704 if (d->this_hdr.sh_type == SHT_REL)
1708 s = bfd_get_section_by_name (abfd, name);
1710 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1714 /* We assume that a section named .stab*str is a stabs
1715 string section. We look for a section with the same name
1716 but without the trailing ``str'', and set its sh_link
1717 field to point to this section. */
1718 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1719 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1724 len = strlen (sec->name);
1725 alc = (char *) bfd_malloc (len - 2);
1728 strncpy (alc, sec->name, len - 3);
1729 alc[len - 3] = '\0';
1730 s = bfd_get_section_by_name (abfd, alc);
1734 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1736 /* This is a .stab section. */
1737 elf_section_data (s)->this_hdr.sh_entsize =
1738 4 + 2 * (bed->s->arch_size / 8);
1745 case SHT_GNU_verneed:
1746 case SHT_GNU_verdef:
1747 /* sh_link is the section header index of the string table
1748 used for the dynamic entries, or the symbol table, or the
1750 s = bfd_get_section_by_name (abfd, ".dynstr");
1752 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1756 case SHT_GNU_versym:
1757 /* sh_link is the section header index of the symbol table
1758 this hash table or version table is for. */
1759 s = bfd_get_section_by_name (abfd, ".dynsym");
1761 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1769 /* Map symbol from it's internal number to the external number, moving
1770 all local symbols to be at the head of the list. */
1773 sym_is_global (abfd, sym)
1777 /* If the backend has a special mapping, use it. */
1778 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1779 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1782 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1783 || bfd_is_und_section (bfd_get_section (sym))
1784 || bfd_is_com_section (bfd_get_section (sym)));
1788 elf_map_symbols (abfd)
1791 int symcount = bfd_get_symcount (abfd);
1792 asymbol **syms = bfd_get_outsymbols (abfd);
1793 asymbol **sect_syms;
1795 int num_globals = 0;
1796 int num_locals2 = 0;
1797 int num_globals2 = 0;
1799 int num_sections = 0;
1805 fprintf (stderr, "elf_map_symbols\n");
1809 /* Add a section symbol for each BFD section. FIXME: Is this really
1811 for (asect = abfd->sections; asect; asect = asect->next)
1813 if (max_index < asect->index)
1814 max_index = asect->index;
1818 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1819 if (sect_syms == NULL)
1821 elf_section_syms (abfd) = sect_syms;
1823 for (idx = 0; idx < symcount; idx++)
1825 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1826 && syms[idx]->value == 0)
1830 sec = syms[idx]->section;
1831 if (sec->owner != NULL)
1833 if (sec->owner != abfd)
1835 if (sec->output_offset != 0)
1837 sec = sec->output_section;
1838 BFD_ASSERT (sec->owner == abfd);
1840 sect_syms[sec->index] = syms[idx];
1845 for (asect = abfd->sections; asect; asect = asect->next)
1849 if (sect_syms[asect->index] != NULL)
1852 sym = bfd_make_empty_symbol (abfd);
1855 sym->the_bfd = abfd;
1856 sym->name = asect->name;
1858 /* Set the flags to 0 to indicate that this one was newly added. */
1860 sym->section = asect;
1861 sect_syms[asect->index] = sym;
1865 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1866 asect->name, (long) asect->vma, asect->index, (long) asect);
1870 /* Classify all of the symbols. */
1871 for (idx = 0; idx < symcount; idx++)
1873 if (!sym_is_global (abfd, syms[idx]))
1878 for (asect = abfd->sections; asect; asect = asect->next)
1880 if (sect_syms[asect->index] != NULL
1881 && sect_syms[asect->index]->flags == 0)
1883 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1884 if (!sym_is_global (abfd, sect_syms[asect->index]))
1888 sect_syms[asect->index]->flags = 0;
1892 /* Now sort the symbols so the local symbols are first. */
1893 new_syms = ((asymbol **)
1895 (num_locals + num_globals) * sizeof (asymbol *)));
1896 if (new_syms == NULL)
1899 for (idx = 0; idx < symcount; idx++)
1901 asymbol *sym = syms[idx];
1904 if (!sym_is_global (abfd, sym))
1907 i = num_locals + num_globals2++;
1909 sym->udata.i = i + 1;
1911 for (asect = abfd->sections; asect; asect = asect->next)
1913 if (sect_syms[asect->index] != NULL
1914 && sect_syms[asect->index]->flags == 0)
1916 asymbol *sym = sect_syms[asect->index];
1919 sym->flags = BSF_SECTION_SYM;
1920 if (!sym_is_global (abfd, sym))
1923 i = num_locals + num_globals2++;
1925 sym->udata.i = i + 1;
1929 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1931 elf_num_locals (abfd) = num_locals;
1932 elf_num_globals (abfd) = num_globals;
1936 /* Align to the maximum file alignment that could be required for any
1937 ELF data structure. */
1939 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1940 static INLINE file_ptr
1941 align_file_position (off, align)
1945 return (off + align - 1) & ~(align - 1);
1948 /* Assign a file position to a section, optionally aligning to the
1949 required section alignment. */
1952 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1953 Elf_Internal_Shdr *i_shdrp;
1961 al = i_shdrp->sh_addralign;
1963 offset = BFD_ALIGN (offset, al);
1965 i_shdrp->sh_offset = offset;
1966 if (i_shdrp->bfd_section != NULL)
1967 i_shdrp->bfd_section->filepos = offset;
1968 if (i_shdrp->sh_type != SHT_NOBITS)
1969 offset += i_shdrp->sh_size;
1973 /* Compute the file positions we are going to put the sections at, and
1974 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1975 is not NULL, this is being called by the ELF backend linker. */
1978 _bfd_elf_compute_section_file_positions (abfd, link_info)
1980 struct bfd_link_info *link_info;
1982 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1984 struct bfd_strtab_hash *strtab;
1985 Elf_Internal_Shdr *shstrtab_hdr;
1987 if (abfd->output_has_begun)
1990 /* Do any elf backend specific processing first. */
1991 if (bed->elf_backend_begin_write_processing)
1992 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1994 if (! prep_headers (abfd))
1998 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2002 if (!assign_section_numbers (abfd))
2005 /* The backend linker builds symbol table information itself. */
2006 if (link_info == NULL && abfd->symcount > 0)
2008 /* Non-zero if doing a relocatable link. */
2009 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2011 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2015 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2016 /* sh_name was set in prep_headers. */
2017 shstrtab_hdr->sh_type = SHT_STRTAB;
2018 shstrtab_hdr->sh_flags = 0;
2019 shstrtab_hdr->sh_addr = 0;
2020 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2021 shstrtab_hdr->sh_entsize = 0;
2022 shstrtab_hdr->sh_link = 0;
2023 shstrtab_hdr->sh_info = 0;
2024 /* sh_offset is set in assign_file_positions_except_relocs. */
2025 shstrtab_hdr->sh_addralign = 1;
2027 if (!assign_file_positions_except_relocs (abfd))
2030 if (link_info == NULL && abfd->symcount > 0)
2033 Elf_Internal_Shdr *hdr;
2035 off = elf_tdata (abfd)->next_file_pos;
2037 hdr = &elf_tdata (abfd)->symtab_hdr;
2038 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2040 hdr = &elf_tdata (abfd)->strtab_hdr;
2041 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2043 elf_tdata (abfd)->next_file_pos = off;
2045 /* Now that we know where the .strtab section goes, write it
2047 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2048 || ! _bfd_stringtab_emit (abfd, strtab))
2050 _bfd_stringtab_free (strtab);
2053 abfd->output_has_begun = true;
2058 /* Create a mapping from a set of sections to a program segment. */
2060 static INLINE struct elf_segment_map *
2061 make_mapping (abfd, sections, from, to, phdr)
2063 asection **sections;
2068 struct elf_segment_map *m;
2072 m = ((struct elf_segment_map *)
2074 (sizeof (struct elf_segment_map)
2075 + (to - from - 1) * sizeof (asection *))));
2079 m->p_type = PT_LOAD;
2080 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2081 m->sections[i - from] = *hdrpp;
2082 m->count = to - from;
2084 if (from == 0 && phdr)
2086 /* Include the headers in the first PT_LOAD segment. */
2087 m->includes_filehdr = 1;
2088 m->includes_phdrs = 1;
2094 /* Set up a mapping from BFD sections to program segments. */
2097 map_sections_to_segments (abfd)
2100 asection **sections = NULL;
2104 struct elf_segment_map *mfirst;
2105 struct elf_segment_map **pm;
2106 struct elf_segment_map *m;
2108 unsigned int phdr_index;
2109 bfd_vma maxpagesize;
2111 boolean phdr_in_segment = true;
2115 if (elf_tdata (abfd)->segment_map != NULL)
2118 if (bfd_count_sections (abfd) == 0)
2121 /* Select the allocated sections, and sort them. */
2123 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2124 * sizeof (asection *));
2125 if (sections == NULL)
2129 for (s = abfd->sections; s != NULL; s = s->next)
2131 if ((s->flags & SEC_ALLOC) != 0)
2137 BFD_ASSERT (i <= bfd_count_sections (abfd));
2140 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2142 /* Build the mapping. */
2147 /* If we have a .interp section, then create a PT_PHDR segment for
2148 the program headers and a PT_INTERP segment for the .interp
2150 s = bfd_get_section_by_name (abfd, ".interp");
2151 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2153 m = ((struct elf_segment_map *)
2154 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2158 m->p_type = PT_PHDR;
2159 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2160 m->p_flags = PF_R | PF_X;
2161 m->p_flags_valid = 1;
2162 m->includes_phdrs = 1;
2167 m = ((struct elf_segment_map *)
2168 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2172 m->p_type = PT_INTERP;
2180 /* Look through the sections. We put sections in the same program
2181 segment when the start of the second section can be placed within
2182 a few bytes of the end of the first section. */
2185 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2187 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2189 && (dynsec->flags & SEC_LOAD) == 0)
2192 /* Deal with -Ttext or something similar such that the first section
2193 is not adjacent to the program headers. This is an
2194 approximation, since at this point we don't know exactly how many
2195 program headers we will need. */
2198 bfd_size_type phdr_size;
2200 phdr_size = elf_tdata (abfd)->program_header_size;
2202 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2203 if ((abfd->flags & D_PAGED) == 0
2204 || sections[0]->lma < phdr_size
2205 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2206 phdr_in_segment = false;
2209 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2212 boolean new_segment;
2216 /* See if this section and the last one will fit in the same
2219 if (last_hdr == NULL)
2221 /* If we don't have a segment yet, then we don't need a new
2222 one (we build the last one after this loop). */
2223 new_segment = false;
2225 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2227 /* If this section has a different relation between the
2228 virtual address and the load address, then we need a new
2232 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2233 < BFD_ALIGN (hdr->lma, maxpagesize))
2235 /* If putting this section in this segment would force us to
2236 skip a page in the segment, then we need a new segment. */
2239 else if ((last_hdr->flags & SEC_LOAD) == 0
2240 && (hdr->flags & SEC_LOAD) != 0)
2242 /* We don't want to put a loadable section after a
2243 nonloadable section in the same segment. */
2246 else if ((abfd->flags & D_PAGED) == 0)
2248 /* If the file is not demand paged, which means that we
2249 don't require the sections to be correctly aligned in the
2250 file, then there is no other reason for a new segment. */
2251 new_segment = false;
2254 && (hdr->flags & SEC_READONLY) == 0
2255 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2258 /* We don't want to put a writable section in a read only
2259 segment, unless they are on the same page in memory
2260 anyhow. We already know that the last section does not
2261 bring us past the current section on the page, so the
2262 only case in which the new section is not on the same
2263 page as the previous section is when the previous section
2264 ends precisely on a page boundary. */
2269 /* Otherwise, we can use the same segment. */
2270 new_segment = false;
2275 if ((hdr->flags & SEC_READONLY) == 0)
2281 /* We need a new program segment. We must create a new program
2282 header holding all the sections from phdr_index until hdr. */
2284 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2291 if ((hdr->flags & SEC_READONLY) == 0)
2298 phdr_in_segment = false;
2301 /* Create a final PT_LOAD program segment. */
2302 if (last_hdr != NULL)
2304 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2312 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2315 m = ((struct elf_segment_map *)
2316 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2320 m->p_type = PT_DYNAMIC;
2322 m->sections[0] = dynsec;
2328 /* For each loadable .note section, add a PT_NOTE segment. We don't
2329 use bfd_get_section_by_name, because if we link together
2330 nonloadable .note sections and loadable .note sections, we will
2331 generate two .note sections in the output file. FIXME: Using
2332 names for section types is bogus anyhow. */
2333 for (s = abfd->sections; s != NULL; s = s->next)
2335 if ((s->flags & SEC_LOAD) != 0
2336 && strncmp (s->name, ".note", 5) == 0)
2338 m = ((struct elf_segment_map *)
2339 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2343 m->p_type = PT_NOTE;
2355 elf_tdata (abfd)->segment_map = mfirst;
2359 if (sections != NULL)
2364 /* Sort sections by address. */
2367 elf_sort_sections (arg1, arg2)
2371 const asection *sec1 = *(const asection **) arg1;
2372 const asection *sec2 = *(const asection **) arg2;
2374 /* Sort by LMA first, since this is the address used to
2375 place the section into a segment. */
2376 if (sec1->lma < sec2->lma)
2378 else if (sec1->lma > sec2->lma)
2381 /* Then sort by VMA. Normally the LMA and the VMA will be
2382 the same, and this will do nothing. */
2383 if (sec1->vma < sec2->vma)
2385 else if (sec1->vma > sec2->vma)
2388 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2390 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2395 return sec1->target_index - sec2->target_index;
2405 /* Sort by size, to put zero sized sections before others at the
2408 if (sec1->_raw_size < sec2->_raw_size)
2410 if (sec1->_raw_size > sec2->_raw_size)
2413 return sec1->target_index - sec2->target_index;
2416 /* Assign file positions to the sections based on the mapping from
2417 sections to segments. This function also sets up some fields in
2418 the file header, and writes out the program headers. */
2421 assign_file_positions_for_segments (abfd)
2424 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2426 struct elf_segment_map *m;
2428 Elf_Internal_Phdr *phdrs;
2430 bfd_vma filehdr_vaddr, filehdr_paddr;
2431 bfd_vma phdrs_vaddr, phdrs_paddr;
2432 Elf_Internal_Phdr *p;
2434 if (elf_tdata (abfd)->segment_map == NULL)
2436 if (! map_sections_to_segments (abfd))
2440 if (bed->elf_backend_modify_segment_map)
2442 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2447 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2450 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2451 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2452 elf_elfheader (abfd)->e_phnum = count;
2457 /* If we already counted the number of program segments, make sure
2458 that we allocated enough space. This happens when SIZEOF_HEADERS
2459 is used in a linker script. */
2460 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2461 if (alloc != 0 && count > alloc)
2463 ((*_bfd_error_handler)
2464 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2465 bfd_get_filename (abfd), alloc, count));
2466 bfd_set_error (bfd_error_bad_value);
2473 phdrs = ((Elf_Internal_Phdr *)
2474 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2478 off = bed->s->sizeof_ehdr;
2479 off += alloc * bed->s->sizeof_phdr;
2486 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2493 /* If elf_segment_map is not from map_sections_to_segments, the
2494 sections may not be correctly ordered. */
2496 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2499 p->p_type = m->p_type;
2501 if (m->p_flags_valid)
2502 p->p_flags = m->p_flags;
2506 if (p->p_type == PT_LOAD
2508 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2510 if ((abfd->flags & D_PAGED) != 0)
2511 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2513 off += ((m->sections[0]->vma - off)
2514 % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2520 p->p_vaddr = m->sections[0]->vma;
2522 if (m->p_paddr_valid)
2523 p->p_paddr = m->p_paddr;
2524 else if (m->count == 0)
2527 p->p_paddr = m->sections[0]->lma;
2529 if (p->p_type == PT_LOAD
2530 && (abfd->flags & D_PAGED) != 0)
2531 p->p_align = bed->maxpagesize;
2532 else if (m->count == 0)
2533 p->p_align = bed->s->file_align;
2541 if (m->includes_filehdr)
2543 if (! m->p_flags_valid)
2546 p->p_filesz = bed->s->sizeof_ehdr;
2547 p->p_memsz = bed->s->sizeof_ehdr;
2550 BFD_ASSERT (p->p_type == PT_LOAD);
2552 if (p->p_vaddr < (bfd_vma) off)
2554 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2555 bfd_get_filename (abfd));
2556 bfd_set_error (bfd_error_bad_value);
2561 if (! m->p_paddr_valid)
2564 if (p->p_type == PT_LOAD)
2566 filehdr_vaddr = p->p_vaddr;
2567 filehdr_paddr = p->p_paddr;
2571 if (m->includes_phdrs)
2573 if (! m->p_flags_valid)
2576 if (m->includes_filehdr)
2578 if (p->p_type == PT_LOAD)
2580 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2581 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2586 p->p_offset = bed->s->sizeof_ehdr;
2590 BFD_ASSERT (p->p_type == PT_LOAD);
2591 p->p_vaddr -= off - p->p_offset;
2592 if (! m->p_paddr_valid)
2593 p->p_paddr -= off - p->p_offset;
2596 if (p->p_type == PT_LOAD)
2598 phdrs_vaddr = p->p_vaddr;
2599 phdrs_paddr = p->p_paddr;
2602 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2605 p->p_filesz += alloc * bed->s->sizeof_phdr;
2606 p->p_memsz += alloc * bed->s->sizeof_phdr;
2609 if (p->p_type == PT_LOAD
2610 || (p->p_type == PT_NOTE && abfd->format == bfd_core))
2612 if (! m->includes_filehdr && ! m->includes_phdrs)
2618 adjust = off - (p->p_offset + p->p_filesz);
2619 p->p_filesz += adjust;
2620 p->p_memsz += adjust;
2626 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2630 bfd_size_type align;
2634 align = 1 << bfd_get_section_alignment (abfd, sec);
2636 /* The section may have artificial alignment forced by a
2637 link script. Notice this case by the gap between the
2638 cumulative phdr vma and the section's vma. */
2639 if (p->p_vaddr + p->p_memsz < sec->vma)
2641 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2643 p->p_memsz += adjust;
2646 if ((flags & SEC_LOAD) != 0)
2647 p->p_filesz += adjust;
2650 if (p->p_type == PT_LOAD)
2652 bfd_signed_vma adjust;
2654 if ((flags & SEC_LOAD) != 0)
2656 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2660 else if ((flags & SEC_ALLOC) != 0)
2662 /* The section VMA must equal the file position
2663 modulo the page size. FIXME: I'm not sure if
2664 this adjustment is really necessary. We used to
2665 not have the SEC_LOAD case just above, and then
2666 this was necessary, but now I'm not sure. */
2667 if ((abfd->flags & D_PAGED) != 0)
2668 adjust = (sec->vma - voff) % bed->maxpagesize;
2670 adjust = (sec->vma - voff) % align;
2679 (* _bfd_error_handler)
2680 (_("Error: First section in segment (%s) starts at 0x%x"),
2681 bfd_section_name (abfd, sec), sec->lma);
2682 (* _bfd_error_handler)
2683 (_(" whereas segment starts at 0x%x"),
2688 p->p_memsz += adjust;
2691 if ((flags & SEC_LOAD) != 0)
2692 p->p_filesz += adjust;
2697 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2698 used in a linker script we may have a section with
2699 SEC_LOAD clear but which is supposed to have
2701 if ((flags & SEC_LOAD) != 0
2702 || (flags & SEC_HAS_CONTENTS) != 0)
2703 off += sec->_raw_size;
2705 if ((flags & SEC_ALLOC) != 0)
2706 voff += sec->_raw_size;
2709 if (p->p_type == PT_NOTE && abfd->format == bfd_core)
2711 if (i == 0) /* the actual "note" segment */
2712 { /* this one actually contains everything. */
2714 p->p_filesz = sec->_raw_size;
2715 off += sec->_raw_size;
2718 else /* fake sections -- don't need to be written */
2722 flags = sec->flags = 0; /* no contents */
2729 p->p_memsz += sec->_raw_size;
2731 if ((flags & SEC_LOAD) != 0)
2732 p->p_filesz += sec->_raw_size;
2734 if (align > p->p_align)
2738 if (! m->p_flags_valid)
2741 if ((flags & SEC_CODE) != 0)
2743 if ((flags & SEC_READONLY) == 0)
2749 /* Now that we have set the section file positions, we can set up
2750 the file positions for the non PT_LOAD segments. */
2751 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2755 if (p->p_type != PT_LOAD && m->count > 0)
2757 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2758 p->p_offset = m->sections[0]->filepos;
2762 if (m->includes_filehdr)
2764 p->p_vaddr = filehdr_vaddr;
2765 if (! m->p_paddr_valid)
2766 p->p_paddr = filehdr_paddr;
2768 else if (m->includes_phdrs)
2770 p->p_vaddr = phdrs_vaddr;
2771 if (! m->p_paddr_valid)
2772 p->p_paddr = phdrs_paddr;
2777 /* Clear out any program headers we allocated but did not use. */
2778 for (; count < alloc; count++, p++)
2780 memset (p, 0, sizeof *p);
2781 p->p_type = PT_NULL;
2784 elf_tdata (abfd)->phdr = phdrs;
2786 elf_tdata (abfd)->next_file_pos = off;
2788 /* Write out the program headers. */
2789 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2790 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2796 /* Get the size of the program header.
2798 If this is called by the linker before any of the section VMA's are set, it
2799 can't calculate the correct value for a strange memory layout. This only
2800 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2801 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2802 data segment (exclusive of .interp and .dynamic).
2804 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2805 will be two segments. */
2807 static bfd_size_type
2808 get_program_header_size (abfd)
2813 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2815 /* We can't return a different result each time we're called. */
2816 if (elf_tdata (abfd)->program_header_size != 0)
2817 return elf_tdata (abfd)->program_header_size;
2819 if (elf_tdata (abfd)->segment_map != NULL)
2821 struct elf_segment_map *m;
2824 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2826 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2827 return elf_tdata (abfd)->program_header_size;
2830 /* Assume we will need exactly two PT_LOAD segments: one for text
2831 and one for data. */
2834 s = bfd_get_section_by_name (abfd, ".interp");
2835 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2837 /* If we have a loadable interpreter section, we need a
2838 PT_INTERP segment. In this case, assume we also need a
2839 PT_PHDR segment, although that may not be true for all
2844 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2846 /* We need a PT_DYNAMIC segment. */
2850 for (s = abfd->sections; s != NULL; s = s->next)
2852 if ((s->flags & SEC_LOAD) != 0
2853 && strncmp (s->name, ".note", 5) == 0)
2855 /* We need a PT_NOTE segment. */
2860 /* Let the backend count up any program headers it might need. */
2861 if (bed->elf_backend_additional_program_headers)
2865 a = (*bed->elf_backend_additional_program_headers) (abfd);
2871 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2872 return elf_tdata (abfd)->program_header_size;
2875 /* Work out the file positions of all the sections. This is called by
2876 _bfd_elf_compute_section_file_positions. All the section sizes and
2877 VMAs must be known before this is called.
2879 We do not consider reloc sections at this point, unless they form
2880 part of the loadable image. Reloc sections are assigned file
2881 positions in assign_file_positions_for_relocs, which is called by
2882 write_object_contents and final_link.
2884 We also don't set the positions of the .symtab and .strtab here. */
2887 assign_file_positions_except_relocs (abfd)
2890 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2891 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2892 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2894 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2896 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
2897 && abfd->format != bfd_core)
2899 Elf_Internal_Shdr **hdrpp;
2902 /* Start after the ELF header. */
2903 off = i_ehdrp->e_ehsize;
2905 /* We are not creating an executable, which means that we are
2906 not creating a program header, and that the actual order of
2907 the sections in the file is unimportant. */
2908 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2910 Elf_Internal_Shdr *hdr;
2913 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2915 hdr->sh_offset = -1;
2918 if (i == tdata->symtab_section
2919 || i == tdata->strtab_section)
2921 hdr->sh_offset = -1;
2925 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2931 Elf_Internal_Shdr **hdrpp;
2933 /* Assign file positions for the loaded sections based on the
2934 assignment of sections to segments. */
2935 if (! assign_file_positions_for_segments (abfd))
2938 /* Assign file positions for the other sections. */
2940 off = elf_tdata (abfd)->next_file_pos;
2941 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2943 Elf_Internal_Shdr *hdr;
2946 if (hdr->bfd_section != NULL
2947 && hdr->bfd_section->filepos != 0)
2948 hdr->sh_offset = hdr->bfd_section->filepos;
2949 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2951 ((*_bfd_error_handler)
2952 (_("%s: warning: allocated section `%s' not in segment"),
2953 bfd_get_filename (abfd),
2954 (hdr->bfd_section == NULL
2956 : hdr->bfd_section->name)));
2957 if ((abfd->flags & D_PAGED) != 0)
2958 off += (hdr->sh_addr - off) % bed->maxpagesize;
2960 off += (hdr->sh_addr - off) % hdr->sh_addralign;
2961 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2964 else if (hdr->sh_type == SHT_REL
2965 || hdr->sh_type == SHT_RELA
2966 || hdr == i_shdrpp[tdata->symtab_section]
2967 || hdr == i_shdrpp[tdata->strtab_section])
2968 hdr->sh_offset = -1;
2970 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2974 /* Place the section headers. */
2975 off = align_file_position (off, bed->s->file_align);
2976 i_ehdrp->e_shoff = off;
2977 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2979 elf_tdata (abfd)->next_file_pos = off;
2988 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2989 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2990 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2992 struct bfd_strtab_hash *shstrtab;
2993 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2995 i_ehdrp = elf_elfheader (abfd);
2996 i_shdrp = elf_elfsections (abfd);
2998 shstrtab = _bfd_elf_stringtab_init ();
2999 if (shstrtab == NULL)
3002 elf_shstrtab (abfd) = shstrtab;
3004 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3005 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3006 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3007 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3009 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3010 i_ehdrp->e_ident[EI_DATA] =
3011 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3012 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3014 for (count = EI_PAD; count < EI_NIDENT; count++)
3015 i_ehdrp->e_ident[count] = 0;
3017 if ((abfd->flags & DYNAMIC) != 0)
3018 i_ehdrp->e_type = ET_DYN;
3019 else if ((abfd->flags & EXEC_P) != 0)
3020 i_ehdrp->e_type = ET_EXEC;
3021 else if (bfd_get_format (abfd) == bfd_core)
3022 i_ehdrp->e_type = ET_CORE;
3024 i_ehdrp->e_type = ET_REL;
3026 switch (bfd_get_arch (abfd))
3028 case bfd_arch_unknown:
3029 i_ehdrp->e_machine = EM_NONE;
3031 case bfd_arch_sparc:
3032 if (bed->s->arch_size == 64)
3033 i_ehdrp->e_machine = EM_SPARCV9;
3035 i_ehdrp->e_machine = EM_SPARC;
3038 i_ehdrp->e_machine = EM_386;
3041 i_ehdrp->e_machine = EM_68K;
3044 i_ehdrp->e_machine = EM_88K;
3047 i_ehdrp->e_machine = EM_860;
3049 case bfd_arch_mips: /* MIPS Rxxxx */
3050 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3053 i_ehdrp->e_machine = EM_PARISC;
3055 case bfd_arch_powerpc:
3056 i_ehdrp->e_machine = EM_PPC;
3058 case bfd_arch_alpha:
3059 i_ehdrp->e_machine = EM_ALPHA;
3062 i_ehdrp->e_machine = EM_SH;
3065 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3068 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3071 switch (bfd_get_mach (abfd))
3074 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3078 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3081 i_ehdrp->e_machine = EM_ARM;
3084 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3086 case bfd_arch_mn10200:
3087 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3089 case bfd_arch_mn10300:
3090 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3092 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3094 i_ehdrp->e_machine = EM_NONE;
3096 i_ehdrp->e_version = bed->s->ev_current;
3097 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3099 /* no program header, for now. */
3100 i_ehdrp->e_phoff = 0;
3101 i_ehdrp->e_phentsize = 0;
3102 i_ehdrp->e_phnum = 0;
3104 /* each bfd section is section header entry */
3105 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3106 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3108 /* if we're building an executable, we'll need a program header table */
3109 if (abfd->flags & EXEC_P)
3111 /* it all happens later */
3113 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3115 /* elf_build_phdrs() returns a (NULL-terminated) array of
3116 Elf_Internal_Phdrs */
3117 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3118 i_ehdrp->e_phoff = outbase;
3119 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3124 i_ehdrp->e_phentsize = 0;
3126 i_ehdrp->e_phoff = 0;
3129 elf_tdata (abfd)->symtab_hdr.sh_name =
3130 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3131 elf_tdata (abfd)->strtab_hdr.sh_name =
3132 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3133 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3134 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3135 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3136 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3137 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3143 /* Assign file positions for all the reloc sections which are not part
3144 of the loadable file image. */
3147 _bfd_elf_assign_file_positions_for_relocs (abfd)
3152 Elf_Internal_Shdr **shdrpp;
3154 off = elf_tdata (abfd)->next_file_pos;
3156 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3157 i < elf_elfheader (abfd)->e_shnum;
3160 Elf_Internal_Shdr *shdrp;
3163 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3164 && shdrp->sh_offset == -1)
3165 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3168 elf_tdata (abfd)->next_file_pos = off;
3172 _bfd_elf_write_object_contents (abfd)
3175 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3176 Elf_Internal_Ehdr *i_ehdrp;
3177 Elf_Internal_Shdr **i_shdrp;
3181 if (! abfd->output_has_begun
3182 && ! _bfd_elf_compute_section_file_positions
3183 (abfd, (struct bfd_link_info *) NULL))
3186 i_shdrp = elf_elfsections (abfd);
3187 i_ehdrp = elf_elfheader (abfd);
3190 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3194 _bfd_elf_assign_file_positions_for_relocs (abfd);
3196 /* After writing the headers, we need to write the sections too... */
3197 for (count = 1; count < i_ehdrp->e_shnum; count++)
3199 if (bed->elf_backend_section_processing)
3200 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3201 if (i_shdrp[count]->contents)
3203 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3204 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3206 != i_shdrp[count]->sh_size))
3211 /* Write out the section header names. */
3212 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3213 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3216 if (bed->elf_backend_final_write_processing)
3217 (*bed->elf_backend_final_write_processing) (abfd,
3218 elf_tdata (abfd)->linker);
3220 return bed->s->write_shdrs_and_ehdr (abfd);
3224 _bfd_elf_write_corefile_contents (abfd)
3227 /* Hopefully this can be done just like an object file. */
3228 return _bfd_elf_write_object_contents (abfd);
3230 /* given a section, search the header to find them... */
3232 _bfd_elf_section_from_bfd_section (abfd, asect)
3236 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3237 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3239 Elf_Internal_Shdr *hdr;
3240 int maxindex = elf_elfheader (abfd)->e_shnum;
3242 for (index = 0; index < maxindex; index++)
3244 hdr = i_shdrp[index];
3245 if (hdr->bfd_section == asect)
3249 if (bed->elf_backend_section_from_bfd_section)
3251 for (index = 0; index < maxindex; index++)
3255 hdr = i_shdrp[index];
3257 if ((*bed->elf_backend_section_from_bfd_section)
3258 (abfd, hdr, asect, &retval))
3263 if (bfd_is_abs_section (asect))
3265 if (bfd_is_com_section (asect))
3267 if (bfd_is_und_section (asect))
3270 bfd_set_error (bfd_error_nonrepresentable_section);
3275 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3279 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3281 asymbol **asym_ptr_ptr;
3283 asymbol *asym_ptr = *asym_ptr_ptr;
3285 flagword flags = asym_ptr->flags;
3287 /* When gas creates relocations against local labels, it creates its
3288 own symbol for the section, but does put the symbol into the
3289 symbol chain, so udata is 0. When the linker is generating
3290 relocatable output, this section symbol may be for one of the
3291 input sections rather than the output section. */
3292 if (asym_ptr->udata.i == 0
3293 && (flags & BSF_SECTION_SYM)
3294 && asym_ptr->section)
3298 if (asym_ptr->section->output_section != NULL)
3299 indx = asym_ptr->section->output_section->index;
3301 indx = asym_ptr->section->index;
3302 if (elf_section_syms (abfd)[indx])
3303 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3306 idx = asym_ptr->udata.i;
3310 /* This case can occur when using --strip-symbol on a symbol
3311 which is used in a relocation entry. */
3312 (*_bfd_error_handler)
3313 (_("%s: symbol `%s' required but not present"),
3314 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3315 bfd_set_error (bfd_error_no_symbols);
3322 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3323 (long) asym_ptr, asym_ptr->name, idx, flags,
3324 elf_symbol_flags (flags));
3332 /* Copy private BFD data. This copies any program header information. */
3335 copy_private_bfd_data (ibfd, obfd)
3339 Elf_Internal_Ehdr *iehdr;
3340 struct elf_segment_map *mfirst;
3341 struct elf_segment_map **pm;
3342 struct elf_segment_map *m;
3343 Elf_Internal_Phdr *p;
3345 unsigned int num_segments;
3346 boolean phdr_included = false;
3348 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3349 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3352 if (elf_tdata (ibfd)->phdr == NULL)
3355 iehdr = elf_elfheader (ibfd);
3360 num_segments = elf_elfheader (ibfd)->e_phnum;
3362 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3363 ((addr) >= (bottom) \
3364 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3365 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3367 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3369 #define IS_COREFILE_NOTE(p, s) \
3370 (p->p_type == PT_NOTE \
3371 && ibfd->format == bfd_core \
3372 && s->vma == 0 && s->lma == 0 \
3373 && (bfd_vma) s->filepos >= p->p_offset \
3374 && (bfd_vma) s->filepos + s->_raw_size \
3375 <= p->p_offset + p->p_filesz)
3377 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3378 linker, which generates a PT_INTERP section with p_vaddr and
3379 p_memsz set to 0. */
3381 #define IS_SOLARIS_PT_INTERP(p, s) \
3383 && p->p_filesz > 0 \
3384 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3385 && s->_raw_size > 0 \
3386 && (bfd_vma) s->filepos >= p->p_offset \
3387 && ((bfd_vma) s->filepos + s->_raw_size \
3388 <= p->p_offset + p->p_filesz))
3390 /* Scan through the segments specified in the program header
3391 of the input BFD. */
3392 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3396 asection **sections;
3399 bfd_vma matching_lma;
3400 bfd_vma suggested_lma;
3403 /* For each section in the input BFD, decide if it should be
3404 included in the current segment. A section will be included
3405 if it is within the address space of the segment, and it is
3406 an allocated segment, and there is an output section
3407 associated with it. */
3409 for (s = ibfd->sections; s != NULL; s = s->next)
3410 if (s->output_section != NULL)
3412 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3413 || IS_SOLARIS_PT_INTERP (p, s))
3414 && (s->flags & SEC_ALLOC) != 0)
3416 else if (IS_COREFILE_NOTE (p, s))
3420 /* Allocate a segment map big enough to contain all of the
3421 sections we have selected. */
3422 m = ((struct elf_segment_map *)
3424 (sizeof (struct elf_segment_map)
3425 + ((size_t) csecs - 1) * sizeof (asection *))));
3429 /* Initialise the fields of the segment map. Default to
3430 using the physical address of the segment in the input BFD. */
3432 m->p_type = p->p_type;
3433 m->p_flags = p->p_flags;
3434 m->p_flags_valid = 1;
3435 m->p_paddr = p->p_paddr;
3436 m->p_paddr_valid = 1;
3438 /* Determine if this segment contains the ELF file header
3439 and if it contains the program headers themselves. */
3440 m->includes_filehdr = (p->p_offset == 0
3441 && p->p_filesz >= iehdr->e_ehsize);
3443 if (! phdr_included || p->p_type != PT_LOAD)
3446 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3447 && (p->p_offset + p->p_filesz
3448 >= ((bfd_vma) iehdr->e_phoff
3449 + iehdr->e_phnum * iehdr->e_phentsize)));
3450 if (p->p_type == PT_LOAD && m->includes_phdrs)
3451 phdr_included = true;
3456 /* Special segments, such as the PT_PHDR segment, may contain
3457 no sections, but ordinary, loadable segments should contain
3460 if (p->p_type == PT_LOAD)
3462 (_("%s: warning: Empty loadable segment detected\n"),
3463 bfd_get_filename (ibfd));
3472 /* Now scan the sections in the input BFD again and attempt
3473 to add their corresponding output sections to the segment map.
3474 The problem here is how to handle an output section which has
3475 been moved (ie had its LMA changed). There are four possibilities:
3477 1. None of the sections have been moved.
3478 In this case we can continue to use the segment LMA from the
3481 2. All of the sections have been moved by the same amount.
3482 In this case we can change the segment's LMA to match the LMA
3483 of the first section.
3485 3. Some of the sections have been moved, others have not.
3486 In this case those sections which have not been moved can be
3487 placed in the current segment which will have to have its size,
3488 and possibly its LMA changed, and a new segment or segments will
3489 have to be created to contain the other sections.
3491 4. The sections have been moved, but not be the same amount.
3492 In this case we can change the segment's LMA to match the LMA
3493 of the first section and we will have to create a new segment
3494 or segments to contain the other sections.
3496 In order to save time, we allocate an array to hold the section
3497 pointers that we are interested in. As these sections get assigned
3498 to a segment, they are removed from this array. */
3500 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3501 if (sections == NULL)
3504 /* Step One: Scan for segment vs section LMA conflicts.
3505 Also add the sections to the section array allocated above.
3506 Also add the sections to the current segment. In the common
3507 case, where the sections have not been moved, this means that
3508 we have completely filled the segment, and there is nothing
3512 matching_lma = false;
3515 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3517 os = s->output_section;
3519 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3520 || IS_SOLARIS_PT_INTERP (p, s))
3521 && (s->flags & SEC_ALLOC) != 0)
3522 || IS_COREFILE_NOTE (p, s))
3527 /* The Solaris native linker always sets p_paddr to 0.
3528 We try to catch that case here, and set it to the
3534 && (os->vma == (p->p_vaddr
3535 + (m->includes_filehdr
3538 + (m->includes_phdrs
3539 ? iehdr->e_phnum * iehdr->e_phentsize
3541 m->p_paddr = p->p_vaddr;
3543 /* Match up the physical address of the segment with the
3544 LMA address of the output section. */
3545 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3546 || IS_COREFILE_NOTE (p, s))
3548 if (matching_lma == 0)
3549 matching_lma = os->lma;
3551 /* We assume that if the section fits within the segment
3552 that it does not overlap any other section within that
3554 m->sections[isec++] = os;
3556 else if (suggested_lma == 0)
3557 suggested_lma = os->lma;
3561 BFD_ASSERT (j == csecs);
3563 /* Step Two: Adjust the physical address of the current segment,
3567 /* All of the sections fitted within the segment as currently
3568 specified. This is the default case. Add the segment to
3569 the list of built segments and carry on to process the next
3570 program header in the input BFD. */
3578 else if (matching_lma != 0)
3580 /* At least one section fits inside the current segment.
3581 Keep it, but modify its physical address to match the
3582 LMA of the first section that fitted. */
3584 m->p_paddr = matching_lma;
3588 /* None of the sections fitted inside the current segment.
3589 Change the current segment's physical address to match
3590 the LMA of the first section. */
3592 m->p_paddr = suggested_lma;
3595 /* Step Three: Loop over the sections again, this time assigning
3596 those that fit to the current segment and remvoing them from the
3597 sections array; but making sure not to leave large gaps. Once all
3598 possible sections have been assigned to the current segment it is
3599 added to the list of built segments and if sections still remain
3600 to be assigned, a new segment is constructed before repeating
3608 /* Fill the current segment with sections that fit. */
3609 for (j = 0; j < csecs; j++)
3616 os = s->output_section;
3618 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3619 || IS_COREFILE_NOTE (p, s))
3623 /* If the first section in a segment does not start at
3624 the beginning of the segment, then something is wrong. */
3625 if (os->lma != m->p_paddr)
3630 asection * prev_sec;
3631 bfd_vma maxpagesize;
3633 prev_sec = m->sections[m->count - 1];
3634 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3636 /* If the gap between the end of the previous section
3637 and the start of this section is more than maxpagesize
3638 then we need to start a new segment. */
3639 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3640 < BFD_ALIGN (os->lma, maxpagesize))
3642 if (suggested_lma == 0)
3643 suggested_lma = os->lma;
3649 m->sections[m->count++] = os;
3653 else if (suggested_lma == 0)
3654 suggested_lma = os->lma;
3657 BFD_ASSERT (m->count > 0);
3659 /* Add the current segment to the list of built segments. */
3665 /* We still have not allocated all of the sections to
3666 segments. Create a new segment here, initialise it
3667 and carry on looping. */
3669 m = ((struct elf_segment_map *)
3671 (sizeof (struct elf_segment_map)
3672 + ((size_t) csecs - 1) * sizeof (asection *))));
3676 /* Initialise the fields of the segment map. Set the physical
3677 physical address to the LMA of the first section that has
3678 not yet been assigned. */
3681 m->p_type = p->p_type;
3682 m->p_flags = p->p_flags;
3683 m->p_flags_valid = 1;
3684 m->p_paddr = suggested_lma;
3685 m->p_paddr_valid = 1;
3686 m->includes_filehdr = 0;
3687 m->includes_phdrs = 0;
3690 while (isec < csecs);
3695 /* The Solaris linker creates program headers in which all the
3696 p_paddr fields are zero. When we try to objcopy or strip such a
3697 file, we get confused. Check for this case, and if we find it
3698 reset the p_paddr_valid fields. */
3699 for (m = mfirst; m != NULL; m = m->next)
3700 if (m->p_paddr != 0)
3704 for (m = mfirst; m != NULL; m = m->next)
3705 m->p_paddr_valid = 0;
3708 elf_tdata (obfd)->segment_map = mfirst;
3711 /* Final Step: Sort the segments into ascending order of physical address. */
3714 struct elf_segment_map* prev;
3717 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3719 /* Yes I know - its a bubble sort....*/
3720 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3722 /* swap m and m->next */
3723 prev->next = m->next;
3724 m->next = m->next->next;
3725 prev->next->next = m;
3734 #undef IS_CONTAINED_BY
3735 #undef IS_SOLARIS_PT_INTERP
3736 #undef IS_COREFILE_NOTE
3740 /* Copy private section information. This copies over the entsize
3741 field, and sometimes the info field. */
3744 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3750 Elf_Internal_Shdr *ihdr, *ohdr;
3752 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3753 || obfd->xvec->flavour != bfd_target_elf_flavour)
3756 /* Copy over private BFD data if it has not already been copied.
3757 This must be done here, rather than in the copy_private_bfd_data
3758 entry point, because the latter is called after the section
3759 contents have been set, which means that the program headers have
3760 already been worked out. */
3761 if (elf_tdata (obfd)->segment_map == NULL
3762 && elf_tdata (ibfd)->phdr != NULL)
3766 /* Only set up the segments if there are no more SEC_ALLOC
3767 sections. FIXME: This won't do the right thing if objcopy is
3768 used to remove the last SEC_ALLOC section, since objcopy
3769 won't call this routine in that case. */
3770 for (s = isec->next; s != NULL; s = s->next)
3771 if ((s->flags & SEC_ALLOC) != 0)
3775 if (! copy_private_bfd_data (ibfd, obfd))
3780 ihdr = &elf_section_data (isec)->this_hdr;
3781 ohdr = &elf_section_data (osec)->this_hdr;
3783 ohdr->sh_entsize = ihdr->sh_entsize;
3785 if (ihdr->sh_type == SHT_SYMTAB
3786 || ihdr->sh_type == SHT_DYNSYM
3787 || ihdr->sh_type == SHT_GNU_verneed
3788 || ihdr->sh_type == SHT_GNU_verdef)
3789 ohdr->sh_info = ihdr->sh_info;
3794 /* Copy private symbol information. If this symbol is in a section
3795 which we did not map into a BFD section, try to map the section
3796 index correctly. We use special macro definitions for the mapped
3797 section indices; these definitions are interpreted by the
3798 swap_out_syms function. */
3800 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3801 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3802 #define MAP_STRTAB (SHN_LORESERVE - 3)
3803 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3806 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3812 elf_symbol_type *isym, *osym;
3814 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3815 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3818 isym = elf_symbol_from (ibfd, isymarg);
3819 osym = elf_symbol_from (obfd, osymarg);
3823 && bfd_is_abs_section (isym->symbol.section))
3827 shndx = isym->internal_elf_sym.st_shndx;
3828 if (shndx == elf_onesymtab (ibfd))
3829 shndx = MAP_ONESYMTAB;
3830 else if (shndx == elf_dynsymtab (ibfd))
3831 shndx = MAP_DYNSYMTAB;
3832 else if (shndx == elf_tdata (ibfd)->strtab_section)
3834 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3835 shndx = MAP_SHSTRTAB;
3836 osym->internal_elf_sym.st_shndx = shndx;
3842 /* Swap out the symbols. */
3845 swap_out_syms (abfd, sttp, relocatable_p)
3847 struct bfd_strtab_hash **sttp;
3850 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3852 if (!elf_map_symbols (abfd))
3855 /* Dump out the symtabs. */
3857 int symcount = bfd_get_symcount (abfd);
3858 asymbol **syms = bfd_get_outsymbols (abfd);
3859 struct bfd_strtab_hash *stt;
3860 Elf_Internal_Shdr *symtab_hdr;
3861 Elf_Internal_Shdr *symstrtab_hdr;
3862 char *outbound_syms;
3865 stt = _bfd_elf_stringtab_init ();
3869 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3870 symtab_hdr->sh_type = SHT_SYMTAB;
3871 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3872 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3873 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3874 symtab_hdr->sh_addralign = bed->s->file_align;
3876 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3877 symstrtab_hdr->sh_type = SHT_STRTAB;
3879 outbound_syms = bfd_alloc (abfd,
3880 (1 + symcount) * bed->s->sizeof_sym);
3881 if (outbound_syms == NULL)
3883 symtab_hdr->contents = (PTR) outbound_syms;
3885 /* now generate the data (for "contents") */
3887 /* Fill in zeroth symbol and swap it out. */
3888 Elf_Internal_Sym sym;
3894 sym.st_shndx = SHN_UNDEF;
3895 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3896 outbound_syms += bed->s->sizeof_sym;
3898 for (idx = 0; idx < symcount; idx++)
3900 Elf_Internal_Sym sym;
3901 bfd_vma value = syms[idx]->value;
3902 elf_symbol_type *type_ptr;
3903 flagword flags = syms[idx]->flags;
3906 if (flags & BSF_SECTION_SYM)
3907 /* Section symbols have no names. */
3911 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3914 if (sym.st_name == (unsigned long) -1)
3918 type_ptr = elf_symbol_from (abfd, syms[idx]);
3920 if ((flags & BSF_SECTION_SYM) == 0
3921 && bfd_is_com_section (syms[idx]->section))
3923 /* ELF common symbols put the alignment into the `value' field,
3924 and the size into the `size' field. This is backwards from
3925 how BFD handles it, so reverse it here. */
3926 sym.st_size = value;
3927 if (type_ptr == NULL
3928 || type_ptr->internal_elf_sym.st_value == 0)
3929 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3931 sym.st_value = type_ptr->internal_elf_sym.st_value;
3932 sym.st_shndx = _bfd_elf_section_from_bfd_section
3933 (abfd, syms[idx]->section);
3937 asection *sec = syms[idx]->section;
3940 if (sec->output_section)
3942 value += sec->output_offset;
3943 sec = sec->output_section;
3945 /* Don't add in the section vma for relocatable output. */
3946 if (! relocatable_p)
3948 sym.st_value = value;
3949 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
3951 if (bfd_is_abs_section (sec)
3953 && type_ptr->internal_elf_sym.st_shndx != 0)
3955 /* This symbol is in a real ELF section which we did
3956 not create as a BFD section. Undo the mapping done
3957 by copy_private_symbol_data. */
3958 shndx = type_ptr->internal_elf_sym.st_shndx;
3962 shndx = elf_onesymtab (abfd);
3965 shndx = elf_dynsymtab (abfd);
3968 shndx = elf_tdata (abfd)->strtab_section;
3971 shndx = elf_tdata (abfd)->shstrtab_section;
3979 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3985 /* Writing this would be a hell of a lot easier if
3986 we had some decent documentation on bfd, and
3987 knew what to expect of the library, and what to
3988 demand of applications. For example, it
3989 appears that `objcopy' might not set the
3990 section of a symbol to be a section that is
3991 actually in the output file. */
3992 sec2 = bfd_get_section_by_name (abfd, sec->name);
3993 BFD_ASSERT (sec2 != 0);
3994 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3995 BFD_ASSERT (shndx != -1);
3999 sym.st_shndx = shndx;
4002 if ((flags & BSF_FUNCTION) != 0)
4004 else if ((flags & BSF_OBJECT) != 0)
4009 /* Processor-specific types */
4010 if (bed->elf_backend_get_symbol_type)
4011 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4013 if (flags & BSF_SECTION_SYM)
4014 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4015 else if (bfd_is_com_section (syms[idx]->section))
4016 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4017 else if (bfd_is_und_section (syms[idx]->section))
4018 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4022 else if (flags & BSF_FILE)
4023 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4026 int bind = STB_LOCAL;
4028 if (flags & BSF_LOCAL)
4030 else if (flags & BSF_WEAK)
4032 else if (flags & BSF_GLOBAL)
4035 sym.st_info = ELF_ST_INFO (bind, type);
4038 if (type_ptr != NULL)
4039 sym.st_other = type_ptr->internal_elf_sym.st_other;
4043 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4044 outbound_syms += bed->s->sizeof_sym;
4048 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4049 symstrtab_hdr->sh_type = SHT_STRTAB;
4051 symstrtab_hdr->sh_flags = 0;
4052 symstrtab_hdr->sh_addr = 0;
4053 symstrtab_hdr->sh_entsize = 0;
4054 symstrtab_hdr->sh_link = 0;
4055 symstrtab_hdr->sh_info = 0;
4056 symstrtab_hdr->sh_addralign = 1;
4062 /* Return the number of bytes required to hold the symtab vector.
4064 Note that we base it on the count plus 1, since we will null terminate
4065 the vector allocated based on this size. However, the ELF symbol table
4066 always has a dummy entry as symbol #0, so it ends up even. */
4069 _bfd_elf_get_symtab_upper_bound (abfd)
4074 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4076 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4077 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4083 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4088 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4090 if (elf_dynsymtab (abfd) == 0)
4092 bfd_set_error (bfd_error_invalid_operation);
4096 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4097 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4103 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4107 return (asect->reloc_count + 1) * sizeof (arelent *);
4110 /* Canonicalize the relocs. */
4113 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4122 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4128 tblptr = section->relocation;
4129 for (i = 0; i < section->reloc_count; i++)
4130 *relptr++ = tblptr++;
4134 return section->reloc_count;
4138 _bfd_elf_get_symtab (abfd, alocation)
4140 asymbol **alocation;
4142 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4143 (abfd, alocation, false);
4146 bfd_get_symcount (abfd) = symcount;
4151 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4153 asymbol **alocation;
4155 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4156 (abfd, alocation, true);
4159 /* Return the size required for the dynamic reloc entries. Any
4160 section that was actually installed in the BFD, and has type
4161 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4162 considered to be a dynamic reloc section. */
4165 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4171 if (elf_dynsymtab (abfd) == 0)
4173 bfd_set_error (bfd_error_invalid_operation);
4177 ret = sizeof (arelent *);
4178 for (s = abfd->sections; s != NULL; s = s->next)
4179 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4180 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4181 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4182 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4183 * sizeof (arelent *));
4188 /* Canonicalize the dynamic relocation entries. Note that we return
4189 the dynamic relocations as a single block, although they are
4190 actually associated with particular sections; the interface, which
4191 was designed for SunOS style shared libraries, expects that there
4192 is only one set of dynamic relocs. Any section that was actually
4193 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4194 the dynamic symbol table, is considered to be a dynamic reloc
4198 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4203 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4207 if (elf_dynsymtab (abfd) == 0)
4209 bfd_set_error (bfd_error_invalid_operation);
4213 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4215 for (s = abfd->sections; s != NULL; s = s->next)
4217 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4218 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4219 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4224 if (! (*slurp_relocs) (abfd, s, syms, true))
4226 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4228 for (i = 0; i < count; i++)
4239 /* Read in the version information. */
4242 _bfd_elf_slurp_version_tables (abfd)
4245 bfd_byte *contents = NULL;
4247 if (elf_dynverdef (abfd) != 0)
4249 Elf_Internal_Shdr *hdr;
4250 Elf_External_Verdef *everdef;
4251 Elf_Internal_Verdef *iverdef;
4254 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4256 elf_tdata (abfd)->verdef =
4257 ((Elf_Internal_Verdef *)
4258 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4259 if (elf_tdata (abfd)->verdef == NULL)
4262 elf_tdata (abfd)->cverdefs = hdr->sh_info;
4264 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4265 if (contents == NULL)
4267 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4268 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4271 everdef = (Elf_External_Verdef *) contents;
4272 iverdef = elf_tdata (abfd)->verdef;
4273 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4275 Elf_External_Verdaux *everdaux;
4276 Elf_Internal_Verdaux *iverdaux;
4279 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4281 iverdef->vd_bfd = abfd;
4283 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4286 * sizeof (Elf_Internal_Verdaux))));
4287 if (iverdef->vd_auxptr == NULL)
4290 everdaux = ((Elf_External_Verdaux *)
4291 ((bfd_byte *) everdef + iverdef->vd_aux));
4292 iverdaux = iverdef->vd_auxptr;
4293 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4295 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4297 iverdaux->vda_nodename =
4298 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4299 iverdaux->vda_name);
4300 if (iverdaux->vda_nodename == NULL)
4303 if (j + 1 < iverdef->vd_cnt)
4304 iverdaux->vda_nextptr = iverdaux + 1;
4306 iverdaux->vda_nextptr = NULL;
4308 everdaux = ((Elf_External_Verdaux *)
4309 ((bfd_byte *) everdaux + iverdaux->vda_next));
4312 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4314 if (i + 1 < hdr->sh_info)
4315 iverdef->vd_nextdef = iverdef + 1;
4317 iverdef->vd_nextdef = NULL;
4319 everdef = ((Elf_External_Verdef *)
4320 ((bfd_byte *) everdef + iverdef->vd_next));
4327 if (elf_dynverref (abfd) != 0)
4329 Elf_Internal_Shdr *hdr;
4330 Elf_External_Verneed *everneed;
4331 Elf_Internal_Verneed *iverneed;
4334 hdr = &elf_tdata (abfd)->dynverref_hdr;
4336 elf_tdata (abfd)->verref =
4337 ((Elf_Internal_Verneed *)
4338 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4339 if (elf_tdata (abfd)->verref == NULL)
4342 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4344 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4345 if (contents == NULL)
4347 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4348 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4351 everneed = (Elf_External_Verneed *) contents;
4352 iverneed = elf_tdata (abfd)->verref;
4353 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4355 Elf_External_Vernaux *evernaux;
4356 Elf_Internal_Vernaux *ivernaux;
4359 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4361 iverneed->vn_bfd = abfd;
4363 iverneed->vn_filename =
4364 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4366 if (iverneed->vn_filename == NULL)
4369 iverneed->vn_auxptr =
4370 ((Elf_Internal_Vernaux *)
4372 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4374 evernaux = ((Elf_External_Vernaux *)
4375 ((bfd_byte *) everneed + iverneed->vn_aux));
4376 ivernaux = iverneed->vn_auxptr;
4377 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4379 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4381 ivernaux->vna_nodename =
4382 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4383 ivernaux->vna_name);
4384 if (ivernaux->vna_nodename == NULL)
4387 if (j + 1 < iverneed->vn_cnt)
4388 ivernaux->vna_nextptr = ivernaux + 1;
4390 ivernaux->vna_nextptr = NULL;
4392 evernaux = ((Elf_External_Vernaux *)
4393 ((bfd_byte *) evernaux + ivernaux->vna_next));
4396 if (i + 1 < hdr->sh_info)
4397 iverneed->vn_nextref = iverneed + 1;
4399 iverneed->vn_nextref = NULL;
4401 everneed = ((Elf_External_Verneed *)
4402 ((bfd_byte *) everneed + iverneed->vn_next));
4412 if (contents == NULL)
4418 _bfd_elf_make_empty_symbol (abfd)
4421 elf_symbol_type *newsym;
4423 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4428 newsym->symbol.the_bfd = abfd;
4429 return &newsym->symbol;
4434 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4439 bfd_symbol_info (symbol, ret);
4442 /* Return whether a symbol name implies a local symbol. Most targets
4443 use this function for the is_local_label_name entry point, but some
4447 _bfd_elf_is_local_label_name (abfd, name)
4451 /* Normal local symbols start with ``.L''. */
4452 if (name[0] == '.' && name[1] == 'L')
4455 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4456 DWARF debugging symbols starting with ``..''. */
4457 if (name[0] == '.' && name[1] == '.')
4460 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4461 emitting DWARF debugging output. I suspect this is actually a
4462 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4463 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4464 underscore to be emitted on some ELF targets). For ease of use,
4465 we treat such symbols as local. */
4466 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4473 _bfd_elf_get_lineno (ignore_abfd, symbol)
4482 _bfd_elf_set_arch_mach (abfd, arch, machine)
4484 enum bfd_architecture arch;
4485 unsigned long machine;
4487 /* If this isn't the right architecture for this backend, and this
4488 isn't the generic backend, fail. */
4489 if (arch != get_elf_backend_data (abfd)->arch
4490 && arch != bfd_arch_unknown
4491 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4494 return bfd_default_set_arch_mach (abfd, arch, machine);
4497 /* Find the nearest line to a particular section and offset, for error
4501 _bfd_elf_find_nearest_line (abfd,
4512 CONST char **filename_ptr;
4513 CONST char **functionname_ptr;
4514 unsigned int *line_ptr;
4517 const char *filename;
4522 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4523 filename_ptr, functionname_ptr,
4527 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4528 filename_ptr, functionname_ptr,
4532 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4533 &found, filename_ptr,
4534 functionname_ptr, line_ptr,
4535 &elf_tdata (abfd)->line_info))
4540 if (symbols == NULL)
4547 for (p = symbols; *p != NULL; p++)
4551 q = (elf_symbol_type *) *p;
4553 if (bfd_get_section (&q->symbol) != section)
4556 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4561 filename = bfd_asymbol_name (&q->symbol);
4565 if (q->symbol.section == section
4566 && q->symbol.value >= low_func
4567 && q->symbol.value <= offset)
4569 func = (asymbol *) q;
4570 low_func = q->symbol.value;
4579 *filename_ptr = filename;
4580 *functionname_ptr = bfd_asymbol_name (func);
4586 _bfd_elf_sizeof_headers (abfd, reloc)
4592 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4594 ret += get_program_header_size (abfd);
4599 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4604 bfd_size_type count;
4606 Elf_Internal_Shdr *hdr;
4608 if (! abfd->output_has_begun
4609 && ! _bfd_elf_compute_section_file_positions
4610 (abfd, (struct bfd_link_info *) NULL))
4613 hdr = &elf_section_data (section)->this_hdr;
4615 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4617 if (bfd_write (location, 1, count, abfd) != count)
4624 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4627 Elf_Internal_Rela *dst;
4634 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4637 Elf_Internal_Rel *dst;
4643 /* Try to convert a non-ELF reloc into an ELF one. */
4646 _bfd_elf_validate_reloc (abfd, areloc)
4650 /* Check whether we really have an ELF howto. */
4652 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4654 bfd_reloc_code_real_type code;
4655 reloc_howto_type *howto;
4657 /* Alien reloc: Try to determine its type to replace it with an
4658 equivalent ELF reloc. */
4660 if (areloc->howto->pc_relative)
4662 switch (areloc->howto->bitsize)
4665 code = BFD_RELOC_8_PCREL;
4668 code = BFD_RELOC_12_PCREL;
4671 code = BFD_RELOC_16_PCREL;
4674 code = BFD_RELOC_24_PCREL;
4677 code = BFD_RELOC_32_PCREL;
4680 code = BFD_RELOC_64_PCREL;
4686 howto = bfd_reloc_type_lookup (abfd, code);
4688 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4690 if (howto->pcrel_offset)
4691 areloc->addend += areloc->address;
4693 areloc->addend -= areloc->address; /* addend is unsigned!! */
4698 switch (areloc->howto->bitsize)
4704 code = BFD_RELOC_14;
4707 code = BFD_RELOC_16;
4710 code = BFD_RELOC_26;
4713 code = BFD_RELOC_32;
4716 code = BFD_RELOC_64;
4722 howto = bfd_reloc_type_lookup (abfd, code);
4726 areloc->howto = howto;
4734 (*_bfd_error_handler)
4735 (_("%s: unsupported relocation type %s"),
4736 bfd_get_filename (abfd), areloc->howto->name);
4737 bfd_set_error (bfd_error_bad_value);
4742 _bfd_elf_close_and_cleanup (abfd)
4745 if (bfd_get_format (abfd) == bfd_object)
4747 if (elf_shstrtab (abfd) != NULL)
4748 _bfd_stringtab_free (elf_shstrtab (abfd));
4751 return _bfd_generic_close_and_cleanup (abfd);
4754 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4755 in the relocation's offset. Thus we cannot allow any sort of sanity
4756 range-checking to interfere. There is nothing else to do in processing
4759 bfd_reloc_status_type
4760 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
4763 struct symbol_cache_entry *symbol;
4769 return bfd_reloc_ok;
4773 /* Elf core file support. Much of this only works on native
4774 toolchains, since we rely on knowing the
4775 machine-dependent procfs structure in order to pick
4776 out details about the corefile. */
4778 #ifdef HAVE_SYS_PROCFS_H
4779 # include <sys/procfs.h>
4783 /* Define offsetof for those systems which lack it. */
4786 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
4790 /* FIXME: this is kinda wrong, but it's what gdb wants. */
4793 elfcore_make_pid (abfd)
4796 return ((elf_tdata (abfd)->core_lwpid << 16)
4797 + (elf_tdata (abfd)->core_pid));
4801 /* If there isn't a section called NAME, make one, using
4802 data from SECT. Note, this function will generate a
4803 reference to NAME, so you shouldn't deallocate or
4807 elfcore_maybe_make_sect (abfd, name, sect)
4814 if (bfd_get_section_by_name (abfd, name) != NULL)
4817 sect2 = bfd_make_section (abfd, name);
4821 sect2->_raw_size = sect->_raw_size;
4822 sect2->filepos = sect->filepos;
4823 sect2->flags = sect->flags;
4824 sect2->alignment_power = sect->alignment_power;
4829 /* prstatus_t exists on:
4831 linux 2.[01] + glibc
4835 #if defined (HAVE_PRSTATUS_T)
4837 elfcore_grok_prstatus (abfd, note)
4839 Elf_Internal_Note* note;
4846 if (note->descsz != sizeof (prstat))
4849 memcpy (&prstat, note->descdata, sizeof (prstat));
4851 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4852 elf_tdata (abfd)->core_pid = prstat.pr_pid;
4854 /* pr_who exists on:
4857 pr_who doesn't exist on:
4860 #if defined (HAVE_PRSTATUS_T_PR_WHO)
4861 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
4864 /* Make a ".reg/999" section. */
4866 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
4867 name = bfd_alloc (abfd, strlen (buf) + 1);
4872 sect = bfd_make_section (abfd, name);
4875 sect->_raw_size = sizeof (prstat.pr_reg);
4876 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
4877 sect->flags = SEC_HAS_CONTENTS;
4878 sect->alignment_power = 2;
4880 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
4885 #endif /* defined (HAVE_PRSTATUS_T) */
4888 /* There isn't a consistent prfpregset_t across platforms,
4889 but it doesn't matter, because we don't have to pick this
4890 data structure apart. */
4893 elfcore_grok_prfpreg (abfd, note)
4895 Elf_Internal_Note* note;
4901 /* Make a ".reg2/999" section. */
4903 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
4904 name = bfd_alloc (abfd, strlen (buf) + 1);
4909 sect = bfd_make_section (abfd, name);
4912 sect->_raw_size = note->descsz;
4913 sect->filepos = note->descpos;
4914 sect->flags = SEC_HAS_CONTENTS;
4915 sect->alignment_power = 2;
4917 if (! elfcore_maybe_make_sect (abfd, ".reg2", sect))
4924 /* return a malloc'ed copy of a string at START which is at
4925 most MAX bytes long, possibly without a terminating '\0'.
4926 the copy will always have a terminating '\0'. */
4929 elfcore_strndup (abfd, start, max)
4935 char* end = memchr (start, '\0', max);
4943 dup = bfd_alloc (abfd, len + 1);
4947 memcpy (dup, start, len);
4954 #if defined (HAVE_PRPSINFO_T)
4955 # define elfcore_psinfo_t prpsinfo_t
4958 #if defined (HAVE_PSINFO_T)
4959 # define elfcore_psinfo_t psinfo_t
4963 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
4965 elfcore_grok_psinfo (abfd, note)
4967 Elf_Internal_Note* note;
4969 elfcore_psinfo_t psinfo;
4971 if (note->descsz != sizeof (elfcore_psinfo_t))
4974 memcpy (&psinfo, note->descdata, note->descsz);
4976 elf_tdata (abfd)->core_program
4977 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
4979 elf_tdata (abfd)->core_command
4980 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
4982 /* Note that for some reason, a spurious space is tacked
4983 onto the end of the args in some (at least one anyway)
4984 implementations, so strip it off if it exists. */
4987 char* command = elf_tdata (abfd)->core_command;
4988 int n = strlen (command);
4990 if (0 < n && command[n - 1] == ' ')
4991 command[n - 1] = '\0';
4996 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
4999 #if defined (HAVE_PSTATUS_T)
5001 elfcore_grok_pstatus (abfd, note)
5003 Elf_Internal_Note* note;
5007 if (note->descsz != sizeof (pstat))
5010 memcpy (&pstat, note->descdata, sizeof (pstat));
5012 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5014 /* Could grab some more details from the "representative"
5015 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5016 NT_LWPSTATUS note, presumably. */
5020 #endif /* defined (HAVE_PSTATUS_T) */
5023 #if defined (HAVE_LWPSTATUS_T)
5025 elfcore_grok_lwpstatus (abfd, note)
5027 Elf_Internal_Note* note;
5029 lwpstatus_t lwpstat;
5034 if (note->descsz != sizeof (lwpstat))
5037 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5039 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5040 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5042 /* Make a ".reg/999" section. */
5044 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5045 name = bfd_alloc (abfd, strlen (buf) + 1);
5050 sect = bfd_make_section (abfd, name);
5054 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5055 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5056 sect->filepos = note->descpos
5057 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5060 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5061 sect->_raw_size = sizeof (lwpstat.pr_reg);
5062 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5065 sect->flags = SEC_HAS_CONTENTS;
5066 sect->alignment_power = 2;
5068 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5071 /* Make a ".reg2/999" section */
5073 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5074 name = bfd_alloc (abfd, strlen (buf) + 1);
5079 sect = bfd_make_section (abfd, name);
5083 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5084 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5085 sect->filepos = note->descpos
5086 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5089 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5090 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5091 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5094 sect->flags = SEC_HAS_CONTENTS;
5095 sect->alignment_power = 2;
5097 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5102 #endif /* defined (HAVE_LWPSTATUS_T) */
5107 elfcore_grok_note (abfd, note)
5109 Elf_Internal_Note* note;
5116 #if defined (HAVE_PRSTATUS_T)
5118 return elfcore_grok_prstatus (abfd, note);
5121 #if defined (HAVE_PSTATUS_T)
5123 return elfcore_grok_pstatus (abfd, note);
5126 #if defined (HAVE_LWPSTATUS_T)
5128 return elfcore_grok_lwpstatus (abfd, note);
5131 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5132 return elfcore_grok_prfpreg (abfd, note);
5134 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5137 return elfcore_grok_psinfo (abfd, note);
5144 elfcore_read_notes (abfd, offset, size)
5155 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5158 buf = bfd_malloc ((size_t) size);
5162 if (bfd_read (buf, size, 1, abfd) != size)
5170 while (p < buf + size)
5172 /* FIXME: bad alignment assumption. */
5173 Elf_External_Note* xnp = (Elf_External_Note*) p;
5174 Elf_Internal_Note in;
5176 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5178 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5179 in.namedata = xnp->name;
5181 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5182 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5183 in.descpos = offset + (in.descdata - buf);
5185 if (! elfcore_grok_note (abfd, &in))
5188 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5198 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5200 Elf_Internal_Phdr* phdr;
5203 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5206 if (phdr->p_type == PT_NOTE
5207 && ! elfcore_read_notes (abfd, phdr->p_offset, phdr->p_filesz))