1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 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 *));
56 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
58 /* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
62 /* Swap in a Verdef structure. */
65 _bfd_elf_swap_verdef_in (abfd, src, dst)
67 const Elf_External_Verdef *src;
68 Elf_Internal_Verdef *dst;
70 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
71 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
72 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
74 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
75 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
76 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (abfd, src, dst)
84 const Elf_Internal_Verdef *src;
85 Elf_External_Verdef *dst;
87 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
88 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
89 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
92 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
93 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
96 /* Swap in a Verdaux structure. */
99 _bfd_elf_swap_verdaux_in (abfd, src, dst)
101 const Elf_External_Verdaux *src;
102 Elf_Internal_Verdaux *dst;
104 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
105 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
108 /* Swap out a Verdaux structure. */
111 _bfd_elf_swap_verdaux_out (abfd, src, dst)
113 const Elf_Internal_Verdaux *src;
114 Elf_External_Verdaux *dst;
116 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
117 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
120 /* Swap in a Verneed structure. */
123 _bfd_elf_swap_verneed_in (abfd, src, dst)
125 const Elf_External_Verneed *src;
126 Elf_Internal_Verneed *dst;
128 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
129 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
130 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
131 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
132 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
135 /* Swap out a Verneed structure. */
138 _bfd_elf_swap_verneed_out (abfd, src, dst)
140 const Elf_Internal_Verneed *src;
141 Elf_External_Verneed *dst;
143 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
144 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
145 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
146 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
147 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
150 /* Swap in a Vernaux structure. */
153 _bfd_elf_swap_vernaux_in (abfd, src, dst)
155 const Elf_External_Vernaux *src;
156 Elf_Internal_Vernaux *dst;
158 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
159 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
160 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
161 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
162 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
165 /* Swap out a Vernaux structure. */
168 _bfd_elf_swap_vernaux_out (abfd, src, dst)
170 const Elf_Internal_Vernaux *src;
171 Elf_External_Vernaux *dst;
173 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
174 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
175 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
176 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
177 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
180 /* Swap in a Versym structure. */
183 _bfd_elf_swap_versym_in (abfd, src, dst)
185 const Elf_External_Versym *src;
186 Elf_Internal_Versym *dst;
188 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
191 /* Swap out a Versym structure. */
194 _bfd_elf_swap_versym_out (abfd, src, dst)
196 const Elf_Internal_Versym *src;
197 Elf_External_Versym *dst;
199 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
202 /* Standard ELF hash function. Do not change this function; you will
203 cause invalid hash tables to be generated. */
206 bfd_elf_hash (namearg)
209 const unsigned char *name = (const unsigned char *) namearg;
214 while ((ch = *name++) != '\0')
217 if ((g = (h & 0xf0000000)) != 0)
220 /* The ELF ABI says `h &= ~g', but this is equivalent in
221 this case and on some machines one insn instead of two. */
228 /* Read a specified number of bytes at a specified offset in an ELF
229 file, into a newly allocated buffer, and return a pointer to the
233 elf_read (abfd, offset, size)
240 if ((buf = bfd_alloc (abfd, size)) == NULL)
242 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
244 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
246 if (bfd_get_error () != bfd_error_system_call)
247 bfd_set_error (bfd_error_file_truncated);
254 bfd_elf_mkobject (abfd)
257 /* this just does initialization */
258 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
259 elf_tdata (abfd) = (struct elf_obj_tdata *)
260 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
261 if (elf_tdata (abfd) == 0)
263 /* since everything is done at close time, do we need any
270 bfd_elf_mkcorefile (abfd)
273 /* I think this can be done just like an object file. */
274 return bfd_elf_mkobject (abfd);
278 bfd_elf_get_str_section (abfd, shindex)
280 unsigned int shindex;
282 Elf_Internal_Shdr **i_shdrp;
283 char *shstrtab = NULL;
285 unsigned int shstrtabsize;
287 i_shdrp = elf_elfsections (abfd);
288 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
291 shstrtab = (char *) i_shdrp[shindex]->contents;
292 if (shstrtab == NULL)
294 /* No cached one, attempt to read, and cache what we read. */
295 offset = i_shdrp[shindex]->sh_offset;
296 shstrtabsize = i_shdrp[shindex]->sh_size;
297 shstrtab = elf_read (abfd, offset, shstrtabsize);
298 i_shdrp[shindex]->contents = (PTR) shstrtab;
304 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
306 unsigned int shindex;
307 unsigned int strindex;
309 Elf_Internal_Shdr *hdr;
314 hdr = elf_elfsections (abfd)[shindex];
316 if (hdr->contents == NULL
317 && bfd_elf_get_str_section (abfd, shindex) == NULL)
320 if (strindex >= hdr->sh_size)
322 (*_bfd_error_handler)
323 (_("%s: invalid string offset %u >= %lu for section `%s'"),
324 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
325 ((shindex == elf_elfheader(abfd)->e_shstrndx
326 && strindex == hdr->sh_name)
328 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
332 return ((char *) hdr->contents) + strindex;
335 /* Make a BFD section from an ELF section. We store a pointer to the
336 BFD section in the bfd_section field of the header. */
339 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
341 Elf_Internal_Shdr *hdr;
347 if (hdr->bfd_section != NULL)
349 BFD_ASSERT (strcmp (name,
350 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
354 newsect = bfd_make_section_anyway (abfd, name);
358 newsect->filepos = hdr->sh_offset;
360 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
361 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
362 || ! bfd_set_section_alignment (abfd, newsect,
363 bfd_log2 (hdr->sh_addralign)))
366 flags = SEC_NO_FLAGS;
367 if (hdr->sh_type != SHT_NOBITS)
368 flags |= SEC_HAS_CONTENTS;
369 if ((hdr->sh_flags & SHF_ALLOC) != 0)
372 if (hdr->sh_type != SHT_NOBITS)
375 if ((hdr->sh_flags & SHF_WRITE) == 0)
376 flags |= SEC_READONLY;
377 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
379 else if ((flags & SEC_LOAD) != 0)
382 /* The debugging sections appear to be recognized only by name, not
384 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
385 || strncmp (name, ".line", sizeof ".line" - 1) == 0
386 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
387 flags |= SEC_DEBUGGING;
389 /* As a GNU extension, if the name begins with .gnu.linkonce, we
390 only link a single copy of the section. This is used to support
391 g++. g++ will emit each template expansion in its own section.
392 The symbols will be defined as weak, so that multiple definitions
393 are permitted. The GNU linker extension is to actually discard
394 all but one of the sections. */
395 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
396 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
398 if (! bfd_set_section_flags (abfd, newsect, flags))
401 if ((flags & SEC_ALLOC) != 0)
403 Elf_Internal_Phdr *phdr;
406 /* Look through the phdrs to see if we need to adjust the lma.
407 If all the p_paddr fields are zero, we ignore them, since
408 some ELF linkers produce such output. */
409 phdr = elf_tdata (abfd)->phdr;
410 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
412 if (phdr->p_paddr != 0)
415 if (i < elf_elfheader (abfd)->e_phnum)
417 phdr = elf_tdata (abfd)->phdr;
418 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
420 if (phdr->p_type == PT_LOAD
421 && phdr->p_vaddr != phdr->p_paddr
422 && phdr->p_vaddr <= hdr->sh_addr
423 && (phdr->p_vaddr + phdr->p_memsz
424 >= hdr->sh_addr + hdr->sh_size)
425 && ((flags & SEC_LOAD) == 0
426 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
427 && (phdr->p_offset + phdr->p_filesz
428 >= hdr->sh_offset + hdr->sh_size))))
430 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
437 hdr->bfd_section = newsect;
438 elf_section_data (newsect)->this_hdr = *hdr;
448 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
451 Helper functions for GDB to locate the string tables.
452 Since BFD hides string tables from callers, GDB needs to use an
453 internal hook to find them. Sun's .stabstr, in particular,
454 isn't even pointed to by the .stab section, so ordinary
455 mechanisms wouldn't work to find it, even if we had some.
458 struct elf_internal_shdr *
459 bfd_elf_find_section (abfd, name)
463 Elf_Internal_Shdr **i_shdrp;
468 i_shdrp = elf_elfsections (abfd);
471 shstrtab = bfd_elf_get_str_section
472 (abfd, elf_elfheader (abfd)->e_shstrndx);
473 if (shstrtab != NULL)
475 max = elf_elfheader (abfd)->e_shnum;
476 for (i = 1; i < max; i++)
477 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
484 const char *const bfd_elf_section_type_names[] = {
485 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
486 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
487 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
490 /* ELF relocs are against symbols. If we are producing relocateable
491 output, and the reloc is against an external symbol, and nothing
492 has given us any additional addend, the resulting reloc will also
493 be against the same symbol. In such a case, we don't want to
494 change anything about the way the reloc is handled, since it will
495 all be done at final link time. Rather than put special case code
496 into bfd_perform_relocation, all the reloc types use this howto
497 function. It just short circuits the reloc if producing
498 relocateable output against an external symbol. */
501 bfd_reloc_status_type
502 bfd_elf_generic_reloc (abfd,
509 bfd *abfd ATTRIBUTE_UNUSED;
510 arelent *reloc_entry;
512 PTR data ATTRIBUTE_UNUSED;
513 asection *input_section;
515 char **error_message ATTRIBUTE_UNUSED;
517 if (output_bfd != (bfd *) NULL
518 && (symbol->flags & BSF_SECTION_SYM) == 0
519 && (! reloc_entry->howto->partial_inplace
520 || reloc_entry->addend == 0))
522 reloc_entry->address += input_section->output_offset;
526 return bfd_reloc_continue;
529 /* Print out the program headers. */
532 _bfd_elf_print_private_bfd_data (abfd, farg)
536 FILE *f = (FILE *) farg;
537 Elf_Internal_Phdr *p;
539 bfd_byte *dynbuf = NULL;
541 p = elf_tdata (abfd)->phdr;
546 fprintf (f, _("\nProgram Header:\n"));
547 c = elf_elfheader (abfd)->e_phnum;
548 for (i = 0; i < c; i++, p++)
555 case PT_NULL: s = "NULL"; break;
556 case PT_LOAD: s = "LOAD"; break;
557 case PT_DYNAMIC: s = "DYNAMIC"; break;
558 case PT_INTERP: s = "INTERP"; break;
559 case PT_NOTE: s = "NOTE"; break;
560 case PT_SHLIB: s = "SHLIB"; break;
561 case PT_PHDR: s = "PHDR"; break;
562 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
564 fprintf (f, "%8s off 0x", s);
565 fprintf_vma (f, p->p_offset);
566 fprintf (f, " vaddr 0x");
567 fprintf_vma (f, p->p_vaddr);
568 fprintf (f, " paddr 0x");
569 fprintf_vma (f, p->p_paddr);
570 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
571 fprintf (f, " filesz 0x");
572 fprintf_vma (f, p->p_filesz);
573 fprintf (f, " memsz 0x");
574 fprintf_vma (f, p->p_memsz);
575 fprintf (f, " flags %c%c%c",
576 (p->p_flags & PF_R) != 0 ? 'r' : '-',
577 (p->p_flags & PF_W) != 0 ? 'w' : '-',
578 (p->p_flags & PF_X) != 0 ? 'x' : '-');
579 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
580 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
585 s = bfd_get_section_by_name (abfd, ".dynamic");
590 bfd_byte *extdyn, *extdynend;
592 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
594 fprintf (f, _("\nDynamic Section:\n"));
596 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
599 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
603 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
606 link = elf_elfsections (abfd)[elfsec]->sh_link;
608 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
609 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
612 extdynend = extdyn + s->_raw_size;
613 for (; extdyn < extdynend; extdyn += extdynsize)
615 Elf_Internal_Dyn dyn;
620 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
622 if (dyn.d_tag == DT_NULL)
629 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
633 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
634 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
635 case DT_PLTGOT: name = "PLTGOT"; break;
636 case DT_HASH: name = "HASH"; break;
637 case DT_STRTAB: name = "STRTAB"; break;
638 case DT_SYMTAB: name = "SYMTAB"; break;
639 case DT_RELA: name = "RELA"; break;
640 case DT_RELASZ: name = "RELASZ"; break;
641 case DT_RELAENT: name = "RELAENT"; break;
642 case DT_STRSZ: name = "STRSZ"; break;
643 case DT_SYMENT: name = "SYMENT"; break;
644 case DT_INIT: name = "INIT"; break;
645 case DT_FINI: name = "FINI"; break;
646 case DT_SONAME: name = "SONAME"; stringp = true; break;
647 case DT_RPATH: name = "RPATH"; stringp = true; break;
648 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
649 case DT_REL: name = "REL"; break;
650 case DT_RELSZ: name = "RELSZ"; break;
651 case DT_RELENT: name = "RELENT"; break;
652 case DT_PLTREL: name = "PLTREL"; break;
653 case DT_DEBUG: name = "DEBUG"; break;
654 case DT_TEXTREL: name = "TEXTREL"; break;
655 case DT_JMPREL: name = "JMPREL"; break;
656 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
657 case DT_FILTER: name = "FILTER"; stringp = true; break;
658 case DT_VERSYM: name = "VERSYM"; break;
659 case DT_VERDEF: name = "VERDEF"; break;
660 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
661 case DT_VERNEED: name = "VERNEED"; break;
662 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
665 fprintf (f, " %-11s ", name);
667 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
672 string = bfd_elf_string_from_elf_section (abfd, link,
676 fprintf (f, "%s", string);
685 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
686 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
688 if (! _bfd_elf_slurp_version_tables (abfd))
692 if (elf_dynverdef (abfd) != 0)
694 Elf_Internal_Verdef *t;
696 fprintf (f, _("\nVersion definitions:\n"));
697 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
699 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
700 t->vd_flags, t->vd_hash, t->vd_nodename);
701 if (t->vd_auxptr->vda_nextptr != NULL)
703 Elf_Internal_Verdaux *a;
706 for (a = t->vd_auxptr->vda_nextptr;
709 fprintf (f, "%s ", a->vda_nodename);
715 if (elf_dynverref (abfd) != 0)
717 Elf_Internal_Verneed *t;
719 fprintf (f, _("\nVersion References:\n"));
720 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
722 Elf_Internal_Vernaux *a;
724 fprintf (f, _(" required from %s:\n"), t->vn_filename);
725 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
726 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
727 a->vna_flags, a->vna_other, a->vna_nodename);
739 /* Display ELF-specific fields of a symbol. */
742 bfd_elf_print_symbol (abfd, filep, symbol, how)
746 bfd_print_symbol_type how;
748 FILE *file = (FILE *) filep;
751 case bfd_print_symbol_name:
752 fprintf (file, "%s", symbol->name);
754 case bfd_print_symbol_more:
755 fprintf (file, "elf ");
756 fprintf_vma (file, symbol->value);
757 fprintf (file, " %lx", (long) symbol->flags);
759 case bfd_print_symbol_all:
761 CONST char *section_name;
762 CONST char *name = NULL;
763 struct elf_backend_data *bed;
765 section_name = symbol->section ? symbol->section->name : "(*none*)";
767 bed = get_elf_backend_data (abfd);
768 if (bed->elf_backend_print_symbol_all)
769 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
774 bfd_print_symbol_vandf ((PTR) file, symbol);
777 fprintf (file, " %s\t", section_name);
778 /* Print the "other" value for a symbol. For common symbols,
779 we've already printed the size; now print the alignment.
780 For other symbols, we have no specified alignment, and
781 we've printed the address; now print the size. */
783 (bfd_is_com_section (symbol->section)
784 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
785 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
787 /* If we have version information, print it. */
788 if (elf_tdata (abfd)->dynversym_section != 0
789 && (elf_tdata (abfd)->dynverdef_section != 0
790 || elf_tdata (abfd)->dynverref_section != 0))
793 const char *version_string;
795 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
799 else if (vernum == 1)
800 version_string = "Base";
801 else if (vernum <= elf_tdata (abfd)->cverdefs)
803 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
806 Elf_Internal_Verneed *t;
809 for (t = elf_tdata (abfd)->verref;
813 Elf_Internal_Vernaux *a;
815 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
817 if (a->vna_other == vernum)
819 version_string = a->vna_nodename;
826 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
827 fprintf (file, " %-11s", version_string);
832 fprintf (file, " (%s)", version_string);
833 for (i = 10 - strlen (version_string); i > 0; --i)
838 /* If the st_other field is not zero, print it. */
839 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
840 fprintf (file, " 0x%02x",
842 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
844 fprintf (file, " %s", name);
850 /* Create an entry in an ELF linker hash table. */
852 struct bfd_hash_entry *
853 _bfd_elf_link_hash_newfunc (entry, table, string)
854 struct bfd_hash_entry *entry;
855 struct bfd_hash_table *table;
858 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
860 /* Allocate the structure if it has not already been allocated by a
862 if (ret == (struct elf_link_hash_entry *) NULL)
863 ret = ((struct elf_link_hash_entry *)
864 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
865 if (ret == (struct elf_link_hash_entry *) NULL)
866 return (struct bfd_hash_entry *) ret;
868 /* Call the allocation method of the superclass. */
869 ret = ((struct elf_link_hash_entry *)
870 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
872 if (ret != (struct elf_link_hash_entry *) NULL)
874 /* Set local fields. */
878 ret->dynstr_index = 0;
880 ret->got.offset = (bfd_vma) -1;
881 ret->plt.offset = (bfd_vma) -1;
882 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
883 ret->verinfo.verdef = NULL;
884 ret->vtable_entries_used = NULL;
885 ret->vtable_entries_size = 0;
886 ret->vtable_parent = NULL;
887 ret->type = STT_NOTYPE;
889 /* Assume that we have been called by a non-ELF symbol reader.
890 This flag is then reset by the code which reads an ELF input
891 file. This ensures that a symbol created by a non-ELF symbol
892 reader will have the flag set correctly. */
893 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
896 return (struct bfd_hash_entry *) ret;
899 /* Initialize an ELF linker hash table. */
902 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
903 struct elf_link_hash_table *table;
905 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
906 struct bfd_hash_table *,
909 table->dynamic_sections_created = false;
910 table->dynobj = NULL;
911 /* The first dynamic symbol is a dummy. */
912 table->dynsymcount = 1;
913 table->dynstr = NULL;
914 table->bucketcount = 0;
915 table->needed = NULL;
917 table->stab_info = NULL;
918 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
921 /* Create an ELF linker hash table. */
923 struct bfd_link_hash_table *
924 _bfd_elf_link_hash_table_create (abfd)
927 struct elf_link_hash_table *ret;
929 ret = ((struct elf_link_hash_table *)
930 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
931 if (ret == (struct elf_link_hash_table *) NULL)
934 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
936 bfd_release (abfd, ret);
943 /* This is a hook for the ELF emulation code in the generic linker to
944 tell the backend linker what file name to use for the DT_NEEDED
945 entry for a dynamic object. The generic linker passes name as an
946 empty string to indicate that no DT_NEEDED entry should be made. */
949 bfd_elf_set_dt_needed_name (abfd, name)
953 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
954 && bfd_get_format (abfd) == bfd_object)
955 elf_dt_name (abfd) = name;
958 /* Get the list of DT_NEEDED entries for a link. This is a hook for
959 the linker ELF emulation code. */
961 struct bfd_link_needed_list *
962 bfd_elf_get_needed_list (abfd, info)
963 bfd *abfd ATTRIBUTE_UNUSED;
964 struct bfd_link_info *info;
966 if (info->hash->creator->flavour != bfd_target_elf_flavour)
968 return elf_hash_table (info)->needed;
971 /* Get the name actually used for a dynamic object for a link. This
972 is the SONAME entry if there is one. Otherwise, it is the string
973 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
976 bfd_elf_get_dt_soname (abfd)
979 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
980 && bfd_get_format (abfd) == bfd_object)
981 return elf_dt_name (abfd);
985 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
986 the ELF linker emulation code. */
989 bfd_elf_get_bfd_needed_list (abfd, pneeded)
991 struct bfd_link_needed_list **pneeded;
994 bfd_byte *dynbuf = NULL;
997 bfd_byte *extdyn, *extdynend;
999 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1003 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1004 || bfd_get_format (abfd) != bfd_object)
1007 s = bfd_get_section_by_name (abfd, ".dynamic");
1008 if (s == NULL || s->_raw_size == 0)
1011 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1015 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1019 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1023 link = elf_elfsections (abfd)[elfsec]->sh_link;
1025 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1026 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1029 extdynend = extdyn + s->_raw_size;
1030 for (; extdyn < extdynend; extdyn += extdynsize)
1032 Elf_Internal_Dyn dyn;
1034 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1036 if (dyn.d_tag == DT_NULL)
1039 if (dyn.d_tag == DT_NEEDED)
1042 struct bfd_link_needed_list *l;
1044 string = bfd_elf_string_from_elf_section (abfd, link,
1049 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1070 /* Allocate an ELF string table--force the first byte to be zero. */
1072 struct bfd_strtab_hash *
1073 _bfd_elf_stringtab_init ()
1075 struct bfd_strtab_hash *ret;
1077 ret = _bfd_stringtab_init ();
1082 loc = _bfd_stringtab_add (ret, "", true, false);
1083 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1084 if (loc == (bfd_size_type) -1)
1086 _bfd_stringtab_free (ret);
1093 /* ELF .o/exec file reading */
1095 /* Create a new bfd section from an ELF section header. */
1098 bfd_section_from_shdr (abfd, shindex)
1100 unsigned int shindex;
1102 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1103 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1104 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1107 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1109 switch (hdr->sh_type)
1112 /* Inactive section. Throw it away. */
1115 case SHT_PROGBITS: /* Normal section with contents. */
1116 case SHT_DYNAMIC: /* Dynamic linking information. */
1117 case SHT_NOBITS: /* .bss section. */
1118 case SHT_HASH: /* .hash section. */
1119 case SHT_NOTE: /* .note section. */
1120 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1122 case SHT_SYMTAB: /* A symbol table */
1123 if (elf_onesymtab (abfd) == shindex)
1126 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1127 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1128 elf_onesymtab (abfd) = shindex;
1129 elf_tdata (abfd)->symtab_hdr = *hdr;
1130 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1131 abfd->flags |= HAS_SYMS;
1133 /* Sometimes a shared object will map in the symbol table. If
1134 SHF_ALLOC is set, and this is a shared object, then we also
1135 treat this section as a BFD section. We can not base the
1136 decision purely on SHF_ALLOC, because that flag is sometimes
1137 set in a relocateable object file, which would confuse the
1139 if ((hdr->sh_flags & SHF_ALLOC) != 0
1140 && (abfd->flags & DYNAMIC) != 0
1141 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1146 case SHT_DYNSYM: /* A dynamic symbol table */
1147 if (elf_dynsymtab (abfd) == shindex)
1150 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1151 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1152 elf_dynsymtab (abfd) = shindex;
1153 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1154 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1155 abfd->flags |= HAS_SYMS;
1157 /* Besides being a symbol table, we also treat this as a regular
1158 section, so that objcopy can handle it. */
1159 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1161 case SHT_STRTAB: /* A string table */
1162 if (hdr->bfd_section != NULL)
1164 if (ehdr->e_shstrndx == shindex)
1166 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1167 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1173 for (i = 1; i < ehdr->e_shnum; i++)
1175 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1176 if (hdr2->sh_link == shindex)
1178 if (! bfd_section_from_shdr (abfd, i))
1180 if (elf_onesymtab (abfd) == i)
1182 elf_tdata (abfd)->strtab_hdr = *hdr;
1183 elf_elfsections (abfd)[shindex] =
1184 &elf_tdata (abfd)->strtab_hdr;
1187 if (elf_dynsymtab (abfd) == i)
1189 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1190 elf_elfsections (abfd)[shindex] = hdr =
1191 &elf_tdata (abfd)->dynstrtab_hdr;
1192 /* We also treat this as a regular section, so
1193 that objcopy can handle it. */
1196 #if 0 /* Not handling other string tables specially right now. */
1197 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1198 /* We have a strtab for some random other section. */
1199 newsect = (asection *) hdr2->bfd_section;
1202 hdr->bfd_section = newsect;
1203 hdr2 = &elf_section_data (newsect)->str_hdr;
1205 elf_elfsections (abfd)[shindex] = hdr2;
1211 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1215 /* *These* do a lot of work -- but build no sections! */
1217 asection *target_sect;
1218 Elf_Internal_Shdr *hdr2;
1220 /* Check for a bogus link to avoid crashing. */
1221 if (hdr->sh_link >= ehdr->e_shnum)
1223 ((*_bfd_error_handler)
1224 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1225 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1226 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1229 /* For some incomprehensible reason Oracle distributes
1230 libraries for Solaris in which some of the objects have
1231 bogus sh_link fields. It would be nice if we could just
1232 reject them, but, unfortunately, some people need to use
1233 them. We scan through the section headers; if we find only
1234 one suitable symbol table, we clobber the sh_link to point
1235 to it. I hope this doesn't break anything. */
1236 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1237 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1243 for (scan = 1; scan < ehdr->e_shnum; scan++)
1245 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1246 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1257 hdr->sh_link = found;
1260 /* Get the symbol table. */
1261 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1262 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1265 /* If this reloc section does not use the main symbol table we
1266 don't treat it as a reloc section. BFD can't adequately
1267 represent such a section, so at least for now, we don't
1268 try. We just present it as a normal section. */
1269 if (hdr->sh_link != elf_onesymtab (abfd))
1270 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1272 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1274 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1275 if (target_sect == NULL)
1278 if ((target_sect->flags & SEC_RELOC) == 0
1279 || target_sect->reloc_count == 0)
1280 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1283 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1284 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1285 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1288 elf_elfsections (abfd)[shindex] = hdr2;
1289 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1290 target_sect->flags |= SEC_RELOC;
1291 target_sect->relocation = NULL;
1292 target_sect->rel_filepos = hdr->sh_offset;
1293 /* In the section to which the relocations apply, mark whether
1294 its relocations are of the REL or RELA variety. */
1295 elf_section_data (target_sect)->use_rela_p
1296 = (hdr->sh_type == SHT_RELA);
1297 abfd->flags |= HAS_RELOC;
1302 case SHT_GNU_verdef:
1303 elf_dynverdef (abfd) = shindex;
1304 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1305 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1308 case SHT_GNU_versym:
1309 elf_dynversym (abfd) = shindex;
1310 elf_tdata (abfd)->dynversym_hdr = *hdr;
1311 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1314 case SHT_GNU_verneed:
1315 elf_dynverref (abfd) = shindex;
1316 elf_tdata (abfd)->dynverref_hdr = *hdr;
1317 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1324 /* Check for any processor-specific section types. */
1326 if (bed->elf_backend_section_from_shdr)
1327 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1335 /* Given an ELF section number, retrieve the corresponding BFD
1339 bfd_section_from_elf_index (abfd, index)
1343 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1344 if (index >= elf_elfheader (abfd)->e_shnum)
1346 return elf_elfsections (abfd)[index]->bfd_section;
1350 _bfd_elf_new_section_hook (abfd, sec)
1354 struct bfd_elf_section_data *sdata;
1356 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1359 sec->used_by_bfd = (PTR) sdata;
1361 /* Indicate whether or not this section should use RELA relocations. */
1363 = get_elf_backend_data (abfd)->default_use_rela_p;
1368 /* Create a new bfd section from an ELF program header.
1370 Since program segments have no names, we generate a synthetic name
1371 of the form segment<NUM>, where NUM is generally the index in the
1372 program header table. For segments that are split (see below) we
1373 generate the names segment<NUM>a and segment<NUM>b.
1375 Note that some program segments may have a file size that is different than
1376 (less than) the memory size. All this means is that at execution the
1377 system must allocate the amount of memory specified by the memory size,
1378 but only initialize it with the first "file size" bytes read from the
1379 file. This would occur for example, with program segments consisting
1380 of combined data+bss.
1382 To handle the above situation, this routine generates TWO bfd sections
1383 for the single program segment. The first has the length specified by
1384 the file size of the segment, and the second has the length specified
1385 by the difference between the two sizes. In effect, the segment is split
1386 into it's initialized and uninitialized parts.
1391 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1393 Elf_Internal_Phdr *hdr;
1395 const char *typename;
1402 split = ((hdr->p_memsz > 0)
1403 && (hdr->p_filesz > 0)
1404 && (hdr->p_memsz > hdr->p_filesz));
1405 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1406 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1409 strcpy (name, namebuf);
1410 newsect = bfd_make_section (abfd, name);
1411 if (newsect == NULL)
1413 newsect->vma = hdr->p_vaddr;
1414 newsect->lma = hdr->p_paddr;
1415 newsect->_raw_size = hdr->p_filesz;
1416 newsect->filepos = hdr->p_offset;
1417 newsect->flags |= SEC_HAS_CONTENTS;
1418 if (hdr->p_type == PT_LOAD)
1420 newsect->flags |= SEC_ALLOC;
1421 newsect->flags |= SEC_LOAD;
1422 if (hdr->p_flags & PF_X)
1424 /* FIXME: all we known is that it has execute PERMISSION,
1426 newsect->flags |= SEC_CODE;
1429 if (!(hdr->p_flags & PF_W))
1431 newsect->flags |= SEC_READONLY;
1436 sprintf (namebuf, "%s%db", typename, index);
1437 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1440 strcpy (name, namebuf);
1441 newsect = bfd_make_section (abfd, name);
1442 if (newsect == NULL)
1444 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1445 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1446 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1447 if (hdr->p_type == PT_LOAD)
1449 newsect->flags |= SEC_ALLOC;
1450 if (hdr->p_flags & PF_X)
1451 newsect->flags |= SEC_CODE;
1453 if (!(hdr->p_flags & PF_W))
1454 newsect->flags |= SEC_READONLY;
1461 bfd_section_from_phdr (abfd, hdr, index)
1463 Elf_Internal_Phdr *hdr;
1466 struct elf_backend_data *bed;
1468 switch (hdr->p_type)
1471 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1474 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1477 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1480 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1483 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1485 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1490 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1493 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1496 /* Check for any processor-specific program segment types.
1497 If no handler for them, default to making "segment" sections. */
1498 bed = get_elf_backend_data (abfd);
1499 if (bed->elf_backend_section_from_phdr)
1500 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1502 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1506 /* Initialize REL_HDR, the section-header for new section, containing
1507 relocations against ASECT. If USE_RELA_P is true, we use RELA
1508 relocations; otherwise, we use REL relocations. */
1511 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1513 Elf_Internal_Shdr *rel_hdr;
1518 struct elf_backend_data *bed;
1520 bed = get_elf_backend_data (abfd);
1521 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1524 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1526 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1528 if (rel_hdr->sh_name == (unsigned int) -1)
1530 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1531 rel_hdr->sh_entsize = (use_rela_p
1532 ? bed->s->sizeof_rela
1533 : bed->s->sizeof_rel);
1534 rel_hdr->sh_addralign = bed->s->file_align;
1535 rel_hdr->sh_flags = 0;
1536 rel_hdr->sh_addr = 0;
1537 rel_hdr->sh_size = 0;
1538 rel_hdr->sh_offset = 0;
1543 /* Set up an ELF internal section header for a section. */
1547 elf_fake_sections (abfd, asect, failedptrarg)
1552 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1553 boolean *failedptr = (boolean *) failedptrarg;
1554 Elf_Internal_Shdr *this_hdr;
1558 /* We already failed; just get out of the bfd_map_over_sections
1563 this_hdr = &elf_section_data (asect)->this_hdr;
1565 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1568 if (this_hdr->sh_name == (unsigned long) -1)
1574 this_hdr->sh_flags = 0;
1576 if ((asect->flags & SEC_ALLOC) != 0
1577 || asect->user_set_vma)
1578 this_hdr->sh_addr = asect->vma;
1580 this_hdr->sh_addr = 0;
1582 this_hdr->sh_offset = 0;
1583 this_hdr->sh_size = asect->_raw_size;
1584 this_hdr->sh_link = 0;
1585 this_hdr->sh_addralign = 1 << asect->alignment_power;
1586 /* The sh_entsize and sh_info fields may have been set already by
1587 copy_private_section_data. */
1589 this_hdr->bfd_section = asect;
1590 this_hdr->contents = NULL;
1592 /* FIXME: This should not be based on section names. */
1593 if (strcmp (asect->name, ".dynstr") == 0)
1594 this_hdr->sh_type = SHT_STRTAB;
1595 else if (strcmp (asect->name, ".hash") == 0)
1597 this_hdr->sh_type = SHT_HASH;
1598 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1600 else if (strcmp (asect->name, ".dynsym") == 0)
1602 this_hdr->sh_type = SHT_DYNSYM;
1603 this_hdr->sh_entsize = bed->s->sizeof_sym;
1605 else if (strcmp (asect->name, ".dynamic") == 0)
1607 this_hdr->sh_type = SHT_DYNAMIC;
1608 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1610 else if (strncmp (asect->name, ".rela", 5) == 0
1611 && get_elf_backend_data (abfd)->may_use_rela_p)
1613 this_hdr->sh_type = SHT_RELA;
1614 this_hdr->sh_entsize = bed->s->sizeof_rela;
1616 else if (strncmp (asect->name, ".rel", 4) == 0
1617 && get_elf_backend_data (abfd)->may_use_rel_p)
1619 this_hdr->sh_type = SHT_REL;
1620 this_hdr->sh_entsize = bed->s->sizeof_rel;
1622 else if (strncmp (asect->name, ".note", 5) == 0)
1623 this_hdr->sh_type = SHT_NOTE;
1624 else if (strncmp (asect->name, ".stab", 5) == 0
1625 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1626 this_hdr->sh_type = SHT_STRTAB;
1627 else if (strcmp (asect->name, ".gnu.version") == 0)
1629 this_hdr->sh_type = SHT_GNU_versym;
1630 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1632 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1634 this_hdr->sh_type = SHT_GNU_verdef;
1635 this_hdr->sh_entsize = 0;
1636 /* objcopy or strip will copy over sh_info, but may not set
1637 cverdefs. The linker will set cverdefs, but sh_info will be
1639 if (this_hdr->sh_info == 0)
1640 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1642 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1643 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1645 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1647 this_hdr->sh_type = SHT_GNU_verneed;
1648 this_hdr->sh_entsize = 0;
1649 /* objcopy or strip will copy over sh_info, but may not set
1650 cverrefs. The linker will set cverrefs, but sh_info will be
1652 if (this_hdr->sh_info == 0)
1653 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1655 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1656 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1658 else if ((asect->flags & SEC_ALLOC) != 0
1659 && (asect->flags & SEC_LOAD) != 0)
1660 this_hdr->sh_type = SHT_PROGBITS;
1661 else if ((asect->flags & SEC_ALLOC) != 0
1662 && ((asect->flags & SEC_LOAD) == 0))
1663 this_hdr->sh_type = SHT_NOBITS;
1667 this_hdr->sh_type = SHT_PROGBITS;
1670 if ((asect->flags & SEC_ALLOC) != 0)
1671 this_hdr->sh_flags |= SHF_ALLOC;
1672 if ((asect->flags & SEC_READONLY) == 0)
1673 this_hdr->sh_flags |= SHF_WRITE;
1674 if ((asect->flags & SEC_CODE) != 0)
1675 this_hdr->sh_flags |= SHF_EXECINSTR;
1677 /* Check for processor-specific section types. */
1678 if (bed->elf_backend_fake_sections)
1679 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1681 /* If the section has relocs, set up a section header for the
1682 SHT_REL[A] section. If two relocation sections are required for
1683 this section, it is up to the processor-specific back-end to
1684 create the other. */
1685 if ((asect->flags & SEC_RELOC) != 0
1686 && !_bfd_elf_init_reloc_shdr (abfd,
1687 &elf_section_data (asect)->rel_hdr,
1689 elf_section_data (asect)->use_rela_p))
1693 /* Assign all ELF section numbers. The dummy first section is handled here
1694 too. The link/info pointers for the standard section types are filled
1695 in here too, while we're at it. */
1698 assign_section_numbers (abfd)
1701 struct elf_obj_tdata *t = elf_tdata (abfd);
1703 unsigned int section_number;
1704 Elf_Internal_Shdr **i_shdrp;
1705 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1709 for (sec = abfd->sections; sec; sec = sec->next)
1711 struct bfd_elf_section_data *d = elf_section_data (sec);
1713 d->this_idx = section_number++;
1714 if ((sec->flags & SEC_RELOC) == 0)
1717 d->rel_idx = section_number++;
1720 d->rel_idx2 = section_number++;
1725 t->shstrtab_section = section_number++;
1726 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1727 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1729 if (bfd_get_symcount (abfd) > 0)
1731 t->symtab_section = section_number++;
1732 t->strtab_section = section_number++;
1735 elf_elfheader (abfd)->e_shnum = section_number;
1737 /* Set up the list of section header pointers, in agreement with the
1739 i_shdrp = ((Elf_Internal_Shdr **)
1740 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1741 if (i_shdrp == NULL)
1744 i_shdrp[0] = ((Elf_Internal_Shdr *)
1745 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1746 if (i_shdrp[0] == NULL)
1748 bfd_release (abfd, i_shdrp);
1751 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1753 elf_elfsections (abfd) = i_shdrp;
1755 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1756 if (bfd_get_symcount (abfd) > 0)
1758 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1759 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1760 t->symtab_hdr.sh_link = t->strtab_section;
1762 for (sec = abfd->sections; sec; sec = sec->next)
1764 struct bfd_elf_section_data *d = elf_section_data (sec);
1768 i_shdrp[d->this_idx] = &d->this_hdr;
1769 if (d->rel_idx != 0)
1770 i_shdrp[d->rel_idx] = &d->rel_hdr;
1771 if (d->rel_idx2 != 0)
1772 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1774 /* Fill in the sh_link and sh_info fields while we're at it. */
1776 /* sh_link of a reloc section is the section index of the symbol
1777 table. sh_info is the section index of the section to which
1778 the relocation entries apply. */
1779 if (d->rel_idx != 0)
1781 d->rel_hdr.sh_link = t->symtab_section;
1782 d->rel_hdr.sh_info = d->this_idx;
1784 if (d->rel_idx2 != 0)
1786 d->rel_hdr2->sh_link = t->symtab_section;
1787 d->rel_hdr2->sh_info = d->this_idx;
1790 switch (d->this_hdr.sh_type)
1794 /* A reloc section which we are treating as a normal BFD
1795 section. sh_link is the section index of the symbol
1796 table. sh_info is the section index of the section to
1797 which the relocation entries apply. We assume that an
1798 allocated reloc section uses the dynamic symbol table.
1799 FIXME: How can we be sure? */
1800 s = bfd_get_section_by_name (abfd, ".dynsym");
1802 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1804 /* We look up the section the relocs apply to by name. */
1806 if (d->this_hdr.sh_type == SHT_REL)
1810 s = bfd_get_section_by_name (abfd, name);
1812 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1816 /* We assume that a section named .stab*str is a stabs
1817 string section. We look for a section with the same name
1818 but without the trailing ``str'', and set its sh_link
1819 field to point to this section. */
1820 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1821 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1826 len = strlen (sec->name);
1827 alc = (char *) bfd_malloc (len - 2);
1830 strncpy (alc, sec->name, len - 3);
1831 alc[len - 3] = '\0';
1832 s = bfd_get_section_by_name (abfd, alc);
1836 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1838 /* This is a .stab section. */
1839 elf_section_data (s)->this_hdr.sh_entsize =
1840 4 + 2 * (bed->s->arch_size / 8);
1847 case SHT_GNU_verneed:
1848 case SHT_GNU_verdef:
1849 /* sh_link is the section header index of the string table
1850 used for the dynamic entries, or the symbol table, or the
1852 s = bfd_get_section_by_name (abfd, ".dynstr");
1854 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1858 case SHT_GNU_versym:
1859 /* sh_link is the section header index of the symbol table
1860 this hash table or version table is for. */
1861 s = bfd_get_section_by_name (abfd, ".dynsym");
1863 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1871 /* Map symbol from it's internal number to the external number, moving
1872 all local symbols to be at the head of the list. */
1875 sym_is_global (abfd, sym)
1879 /* If the backend has a special mapping, use it. */
1880 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1881 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1884 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1885 || bfd_is_und_section (bfd_get_section (sym))
1886 || bfd_is_com_section (bfd_get_section (sym)));
1890 elf_map_symbols (abfd)
1893 int symcount = bfd_get_symcount (abfd);
1894 asymbol **syms = bfd_get_outsymbols (abfd);
1895 asymbol **sect_syms;
1897 int num_globals = 0;
1898 int num_locals2 = 0;
1899 int num_globals2 = 0;
1901 int num_sections = 0;
1908 fprintf (stderr, "elf_map_symbols\n");
1912 /* Add a section symbol for each BFD section. FIXME: Is this really
1914 for (asect = abfd->sections; asect; asect = asect->next)
1916 if (max_index < asect->index)
1917 max_index = asect->index;
1921 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1922 if (sect_syms == NULL)
1924 elf_section_syms (abfd) = sect_syms;
1926 for (idx = 0; idx < symcount; idx++)
1930 if ((sym->flags & BSF_SECTION_SYM) != 0
1937 if (sec->owner != NULL)
1939 if (sec->owner != abfd)
1941 if (sec->output_offset != 0)
1944 sec = sec->output_section;
1946 /* Empty sections in the input files may have had a section
1947 symbol created for them. (See the comment near the end of
1948 _bfd_generic_link_output_symbols in linker.c). If the linker
1949 script discards such sections then we will reach this point.
1950 Since we know that we cannot avoid this case, we detect it
1951 and skip the abort and the assignment to the sect_syms array.
1952 To reproduce this particular case try running the linker
1953 testsuite test ld-scripts/weak.exp for an ELF port that uses
1954 the generic linker. */
1955 if (sec->owner == NULL)
1958 BFD_ASSERT (sec->owner == abfd);
1960 sect_syms[sec->index] = syms[idx];
1965 for (asect = abfd->sections; asect; asect = asect->next)
1967 if (sect_syms[asect->index] != NULL)
1970 sym = bfd_make_empty_symbol (abfd);
1973 sym->the_bfd = abfd;
1974 sym->name = asect->name;
1976 /* Set the flags to 0 to indicate that this one was newly added. */
1978 sym->section = asect;
1979 sect_syms[asect->index] = sym;
1983 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1984 asect->name, (long) asect->vma, asect->index, (long) asect);
1988 /* Classify all of the symbols. */
1989 for (idx = 0; idx < symcount; idx++)
1991 if (!sym_is_global (abfd, syms[idx]))
1996 for (asect = abfd->sections; asect; asect = asect->next)
1998 if (sect_syms[asect->index] != NULL
1999 && sect_syms[asect->index]->flags == 0)
2001 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2002 if (!sym_is_global (abfd, sect_syms[asect->index]))
2006 sect_syms[asect->index]->flags = 0;
2010 /* Now sort the symbols so the local symbols are first. */
2011 new_syms = ((asymbol **)
2013 (num_locals + num_globals) * sizeof (asymbol *)));
2014 if (new_syms == NULL)
2017 for (idx = 0; idx < symcount; idx++)
2019 asymbol *sym = syms[idx];
2022 if (!sym_is_global (abfd, sym))
2025 i = num_locals + num_globals2++;
2027 sym->udata.i = i + 1;
2029 for (asect = abfd->sections; asect; asect = asect->next)
2031 if (sect_syms[asect->index] != NULL
2032 && sect_syms[asect->index]->flags == 0)
2034 asymbol *sym = sect_syms[asect->index];
2037 sym->flags = BSF_SECTION_SYM;
2038 if (!sym_is_global (abfd, sym))
2041 i = num_locals + num_globals2++;
2043 sym->udata.i = i + 1;
2047 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2049 elf_num_locals (abfd) = num_locals;
2050 elf_num_globals (abfd) = num_globals;
2054 /* Align to the maximum file alignment that could be required for any
2055 ELF data structure. */
2057 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2058 static INLINE file_ptr
2059 align_file_position (off, align)
2063 return (off + align - 1) & ~(align - 1);
2066 /* Assign a file position to a section, optionally aligning to the
2067 required section alignment. */
2070 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2071 Elf_Internal_Shdr *i_shdrp;
2079 al = i_shdrp->sh_addralign;
2081 offset = BFD_ALIGN (offset, al);
2083 i_shdrp->sh_offset = offset;
2084 if (i_shdrp->bfd_section != NULL)
2085 i_shdrp->bfd_section->filepos = offset;
2086 if (i_shdrp->sh_type != SHT_NOBITS)
2087 offset += i_shdrp->sh_size;
2091 /* Compute the file positions we are going to put the sections at, and
2092 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2093 is not NULL, this is being called by the ELF backend linker. */
2096 _bfd_elf_compute_section_file_positions (abfd, link_info)
2098 struct bfd_link_info *link_info;
2100 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2102 struct bfd_strtab_hash *strtab;
2103 Elf_Internal_Shdr *shstrtab_hdr;
2105 if (abfd->output_has_begun)
2108 /* Do any elf backend specific processing first. */
2109 if (bed->elf_backend_begin_write_processing)
2110 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2112 if (! prep_headers (abfd))
2115 /* Post process the headers if necessary. */
2116 if (bed->elf_backend_post_process_headers)
2117 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2120 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2124 if (!assign_section_numbers (abfd))
2127 /* The backend linker builds symbol table information itself. */
2128 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2130 /* Non-zero if doing a relocatable link. */
2131 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2133 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2137 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2138 /* sh_name was set in prep_headers. */
2139 shstrtab_hdr->sh_type = SHT_STRTAB;
2140 shstrtab_hdr->sh_flags = 0;
2141 shstrtab_hdr->sh_addr = 0;
2142 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2143 shstrtab_hdr->sh_entsize = 0;
2144 shstrtab_hdr->sh_link = 0;
2145 shstrtab_hdr->sh_info = 0;
2146 /* sh_offset is set in assign_file_positions_except_relocs. */
2147 shstrtab_hdr->sh_addralign = 1;
2149 if (!assign_file_positions_except_relocs (abfd))
2152 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2155 Elf_Internal_Shdr *hdr;
2157 off = elf_tdata (abfd)->next_file_pos;
2159 hdr = &elf_tdata (abfd)->symtab_hdr;
2160 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2162 hdr = &elf_tdata (abfd)->strtab_hdr;
2163 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2165 elf_tdata (abfd)->next_file_pos = off;
2167 /* Now that we know where the .strtab section goes, write it
2169 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2170 || ! _bfd_stringtab_emit (abfd, strtab))
2172 _bfd_stringtab_free (strtab);
2175 abfd->output_has_begun = true;
2180 /* Create a mapping from a set of sections to a program segment. */
2182 static INLINE struct elf_segment_map *
2183 make_mapping (abfd, sections, from, to, phdr)
2185 asection **sections;
2190 struct elf_segment_map *m;
2194 m = ((struct elf_segment_map *)
2196 (sizeof (struct elf_segment_map)
2197 + (to - from - 1) * sizeof (asection *))));
2201 m->p_type = PT_LOAD;
2202 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2203 m->sections[i - from] = *hdrpp;
2204 m->count = to - from;
2206 if (from == 0 && phdr)
2208 /* Include the headers in the first PT_LOAD segment. */
2209 m->includes_filehdr = 1;
2210 m->includes_phdrs = 1;
2216 /* Set up a mapping from BFD sections to program segments. */
2219 map_sections_to_segments (abfd)
2222 asection **sections = NULL;
2226 struct elf_segment_map *mfirst;
2227 struct elf_segment_map **pm;
2228 struct elf_segment_map *m;
2230 unsigned int phdr_index;
2231 bfd_vma maxpagesize;
2233 boolean phdr_in_segment = true;
2237 if (elf_tdata (abfd)->segment_map != NULL)
2240 if (bfd_count_sections (abfd) == 0)
2243 /* Select the allocated sections, and sort them. */
2245 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2246 * sizeof (asection *));
2247 if (sections == NULL)
2251 for (s = abfd->sections; s != NULL; s = s->next)
2253 if ((s->flags & SEC_ALLOC) != 0)
2259 BFD_ASSERT (i <= bfd_count_sections (abfd));
2262 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2264 /* Build the mapping. */
2269 /* If we have a .interp section, then create a PT_PHDR segment for
2270 the program headers and a PT_INTERP segment for the .interp
2272 s = bfd_get_section_by_name (abfd, ".interp");
2273 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2275 m = ((struct elf_segment_map *)
2276 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2280 m->p_type = PT_PHDR;
2281 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2282 m->p_flags = PF_R | PF_X;
2283 m->p_flags_valid = 1;
2284 m->includes_phdrs = 1;
2289 m = ((struct elf_segment_map *)
2290 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2294 m->p_type = PT_INTERP;
2302 /* Look through the sections. We put sections in the same program
2303 segment when the start of the second section can be placed within
2304 a few bytes of the end of the first section. */
2307 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2309 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2311 && (dynsec->flags & SEC_LOAD) == 0)
2314 /* Deal with -Ttext or something similar such that the first section
2315 is not adjacent to the program headers. This is an
2316 approximation, since at this point we don't know exactly how many
2317 program headers we will need. */
2320 bfd_size_type phdr_size;
2322 phdr_size = elf_tdata (abfd)->program_header_size;
2324 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2325 if ((abfd->flags & D_PAGED) == 0
2326 || sections[0]->lma < phdr_size
2327 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2328 phdr_in_segment = false;
2331 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2334 boolean new_segment;
2338 /* See if this section and the last one will fit in the same
2341 if (last_hdr == NULL)
2343 /* If we don't have a segment yet, then we don't need a new
2344 one (we build the last one after this loop). */
2345 new_segment = false;
2347 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2349 /* If this section has a different relation between the
2350 virtual address and the load address, then we need a new
2354 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2355 < BFD_ALIGN (hdr->lma, maxpagesize))
2357 /* If putting this section in this segment would force us to
2358 skip a page in the segment, then we need a new segment. */
2361 else if ((last_hdr->flags & SEC_LOAD) == 0
2362 && (hdr->flags & SEC_LOAD) != 0)
2364 /* We don't want to put a loadable section after a
2365 nonloadable section in the same segment. */
2368 else if ((abfd->flags & D_PAGED) == 0)
2370 /* If the file is not demand paged, which means that we
2371 don't require the sections to be correctly aligned in the
2372 file, then there is no other reason for a new segment. */
2373 new_segment = false;
2376 && (hdr->flags & SEC_READONLY) == 0
2377 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2380 /* We don't want to put a writable section in a read only
2381 segment, unless they are on the same page in memory
2382 anyhow. We already know that the last section does not
2383 bring us past the current section on the page, so the
2384 only case in which the new section is not on the same
2385 page as the previous section is when the previous section
2386 ends precisely on a page boundary. */
2391 /* Otherwise, we can use the same segment. */
2392 new_segment = false;
2397 if ((hdr->flags & SEC_READONLY) == 0)
2403 /* We need a new program segment. We must create a new program
2404 header holding all the sections from phdr_index until hdr. */
2406 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2413 if ((hdr->flags & SEC_READONLY) == 0)
2420 phdr_in_segment = false;
2423 /* Create a final PT_LOAD program segment. */
2424 if (last_hdr != NULL)
2426 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2434 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2437 m = ((struct elf_segment_map *)
2438 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2442 m->p_type = PT_DYNAMIC;
2444 m->sections[0] = dynsec;
2450 /* For each loadable .note section, add a PT_NOTE segment. We don't
2451 use bfd_get_section_by_name, because if we link together
2452 nonloadable .note sections and loadable .note sections, we will
2453 generate two .note sections in the output file. FIXME: Using
2454 names for section types is bogus anyhow. */
2455 for (s = abfd->sections; s != NULL; s = s->next)
2457 if ((s->flags & SEC_LOAD) != 0
2458 && strncmp (s->name, ".note", 5) == 0)
2460 m = ((struct elf_segment_map *)
2461 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2465 m->p_type = PT_NOTE;
2477 elf_tdata (abfd)->segment_map = mfirst;
2481 if (sections != NULL)
2486 /* Sort sections by address. */
2489 elf_sort_sections (arg1, arg2)
2493 const asection *sec1 = *(const asection **) arg1;
2494 const asection *sec2 = *(const asection **) arg2;
2496 /* Sort by LMA first, since this is the address used to
2497 place the section into a segment. */
2498 if (sec1->lma < sec2->lma)
2500 else if (sec1->lma > sec2->lma)
2503 /* Then sort by VMA. Normally the LMA and the VMA will be
2504 the same, and this will do nothing. */
2505 if (sec1->vma < sec2->vma)
2507 else if (sec1->vma > sec2->vma)
2510 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2512 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2517 return sec1->target_index - sec2->target_index;
2527 /* Sort by size, to put zero sized sections before others at the
2530 if (sec1->_raw_size < sec2->_raw_size)
2532 if (sec1->_raw_size > sec2->_raw_size)
2535 return sec1->target_index - sec2->target_index;
2538 /* Assign file positions to the sections based on the mapping from
2539 sections to segments. This function also sets up some fields in
2540 the file header, and writes out the program headers. */
2543 assign_file_positions_for_segments (abfd)
2546 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2548 struct elf_segment_map *m;
2550 Elf_Internal_Phdr *phdrs;
2552 bfd_vma filehdr_vaddr, filehdr_paddr;
2553 bfd_vma phdrs_vaddr, phdrs_paddr;
2554 Elf_Internal_Phdr *p;
2556 if (elf_tdata (abfd)->segment_map == NULL)
2558 if (! map_sections_to_segments (abfd))
2562 if (bed->elf_backend_modify_segment_map)
2564 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2569 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2572 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2573 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2574 elf_elfheader (abfd)->e_phnum = count;
2579 /* If we already counted the number of program segments, make sure
2580 that we allocated enough space. This happens when SIZEOF_HEADERS
2581 is used in a linker script. */
2582 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2583 if (alloc != 0 && count > alloc)
2585 ((*_bfd_error_handler)
2586 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2587 bfd_get_filename (abfd), alloc, count));
2588 bfd_set_error (bfd_error_bad_value);
2595 phdrs = ((Elf_Internal_Phdr *)
2596 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2600 off = bed->s->sizeof_ehdr;
2601 off += alloc * bed->s->sizeof_phdr;
2608 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2615 /* If elf_segment_map is not from map_sections_to_segments, the
2616 sections may not be correctly ordered. */
2618 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2621 p->p_type = m->p_type;
2622 p->p_flags = m->p_flags;
2624 if (p->p_type == PT_LOAD
2626 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2628 if ((abfd->flags & D_PAGED) != 0)
2629 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2632 bfd_size_type align;
2635 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2637 bfd_size_type secalign;
2639 secalign = bfd_get_section_alignment (abfd, *secpp);
2640 if (secalign > align)
2644 off += (m->sections[0]->vma - off) % (1 << align);
2651 p->p_vaddr = m->sections[0]->vma;
2653 if (m->p_paddr_valid)
2654 p->p_paddr = m->p_paddr;
2655 else if (m->count == 0)
2658 p->p_paddr = m->sections[0]->lma;
2660 if (p->p_type == PT_LOAD
2661 && (abfd->flags & D_PAGED) != 0)
2662 p->p_align = bed->maxpagesize;
2663 else if (m->count == 0)
2664 p->p_align = bed->s->file_align;
2672 if (m->includes_filehdr)
2674 if (! m->p_flags_valid)
2677 p->p_filesz = bed->s->sizeof_ehdr;
2678 p->p_memsz = bed->s->sizeof_ehdr;
2681 BFD_ASSERT (p->p_type == PT_LOAD);
2683 if (p->p_vaddr < (bfd_vma) off)
2685 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2686 bfd_get_filename (abfd));
2687 bfd_set_error (bfd_error_bad_value);
2692 if (! m->p_paddr_valid)
2695 if (p->p_type == PT_LOAD)
2697 filehdr_vaddr = p->p_vaddr;
2698 filehdr_paddr = p->p_paddr;
2702 if (m->includes_phdrs)
2704 if (! m->p_flags_valid)
2707 if (m->includes_filehdr)
2709 if (p->p_type == PT_LOAD)
2711 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2712 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2717 p->p_offset = bed->s->sizeof_ehdr;
2721 BFD_ASSERT (p->p_type == PT_LOAD);
2722 p->p_vaddr -= off - p->p_offset;
2723 if (! m->p_paddr_valid)
2724 p->p_paddr -= off - p->p_offset;
2727 if (p->p_type == PT_LOAD)
2729 phdrs_vaddr = p->p_vaddr;
2730 phdrs_paddr = p->p_paddr;
2733 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2736 p->p_filesz += alloc * bed->s->sizeof_phdr;
2737 p->p_memsz += alloc * bed->s->sizeof_phdr;
2740 if (p->p_type == PT_LOAD
2741 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2743 if (! m->includes_filehdr && ! m->includes_phdrs)
2749 adjust = off - (p->p_offset + p->p_filesz);
2750 p->p_filesz += adjust;
2751 p->p_memsz += adjust;
2757 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2761 bfd_size_type align;
2765 align = 1 << bfd_get_section_alignment (abfd, sec);
2767 /* The section may have artificial alignment forced by a
2768 link script. Notice this case by the gap between the
2769 cumulative phdr vma and the section's vma. */
2770 if (p->p_vaddr + p->p_memsz < sec->vma)
2772 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2774 p->p_memsz += adjust;
2777 if ((flags & SEC_LOAD) != 0)
2778 p->p_filesz += adjust;
2781 if (p->p_type == PT_LOAD)
2783 bfd_signed_vma adjust;
2785 if ((flags & SEC_LOAD) != 0)
2787 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2791 else if ((flags & SEC_ALLOC) != 0)
2793 /* The section VMA must equal the file position
2794 modulo the page size. FIXME: I'm not sure if
2795 this adjustment is really necessary. We used to
2796 not have the SEC_LOAD case just above, and then
2797 this was necessary, but now I'm not sure. */
2798 if ((abfd->flags & D_PAGED) != 0)
2799 adjust = (sec->vma - voff) % bed->maxpagesize;
2801 adjust = (sec->vma - voff) % align;
2810 (* _bfd_error_handler)
2811 (_("Error: First section in segment (%s) starts at 0x%x"),
2812 bfd_section_name (abfd, sec), sec->lma);
2813 (* _bfd_error_handler)
2814 (_(" whereas segment starts at 0x%x"),
2819 p->p_memsz += adjust;
2822 if ((flags & SEC_LOAD) != 0)
2823 p->p_filesz += adjust;
2828 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2829 used in a linker script we may have a section with
2830 SEC_LOAD clear but which is supposed to have
2832 if ((flags & SEC_LOAD) != 0
2833 || (flags & SEC_HAS_CONTENTS) != 0)
2834 off += sec->_raw_size;
2836 if ((flags & SEC_ALLOC) != 0)
2837 voff += sec->_raw_size;
2840 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2842 if (i == 0) /* the actual "note" segment */
2843 { /* this one actually contains everything. */
2845 p->p_filesz = sec->_raw_size;
2846 off += sec->_raw_size;
2849 else /* fake sections -- don't need to be written */
2853 flags = sec->flags = 0; /* no contents */
2860 p->p_memsz += sec->_raw_size;
2862 if ((flags & SEC_LOAD) != 0)
2863 p->p_filesz += sec->_raw_size;
2865 if (align > p->p_align
2866 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2870 if (! m->p_flags_valid)
2873 if ((flags & SEC_CODE) != 0)
2875 if ((flags & SEC_READONLY) == 0)
2881 /* Now that we have set the section file positions, we can set up
2882 the file positions for the non PT_LOAD segments. */
2883 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2887 if (p->p_type != PT_LOAD && m->count > 0)
2889 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2890 p->p_offset = m->sections[0]->filepos;
2894 if (m->includes_filehdr)
2896 p->p_vaddr = filehdr_vaddr;
2897 if (! m->p_paddr_valid)
2898 p->p_paddr = filehdr_paddr;
2900 else if (m->includes_phdrs)
2902 p->p_vaddr = phdrs_vaddr;
2903 if (! m->p_paddr_valid)
2904 p->p_paddr = phdrs_paddr;
2909 /* Clear out any program headers we allocated but did not use. */
2910 for (; count < alloc; count++, p++)
2912 memset (p, 0, sizeof *p);
2913 p->p_type = PT_NULL;
2916 elf_tdata (abfd)->phdr = phdrs;
2918 elf_tdata (abfd)->next_file_pos = off;
2920 /* Write out the program headers. */
2921 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2922 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2928 /* Get the size of the program header.
2930 If this is called by the linker before any of the section VMA's are set, it
2931 can't calculate the correct value for a strange memory layout. This only
2932 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2933 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2934 data segment (exclusive of .interp and .dynamic).
2936 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2937 will be two segments. */
2939 static bfd_size_type
2940 get_program_header_size (abfd)
2945 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2947 /* We can't return a different result each time we're called. */
2948 if (elf_tdata (abfd)->program_header_size != 0)
2949 return elf_tdata (abfd)->program_header_size;
2951 if (elf_tdata (abfd)->segment_map != NULL)
2953 struct elf_segment_map *m;
2956 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2958 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2959 return elf_tdata (abfd)->program_header_size;
2962 /* Assume we will need exactly two PT_LOAD segments: one for text
2963 and one for data. */
2966 s = bfd_get_section_by_name (abfd, ".interp");
2967 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2969 /* If we have a loadable interpreter section, we need a
2970 PT_INTERP segment. In this case, assume we also need a
2971 PT_PHDR segment, although that may not be true for all
2976 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2978 /* We need a PT_DYNAMIC segment. */
2982 for (s = abfd->sections; s != NULL; s = s->next)
2984 if ((s->flags & SEC_LOAD) != 0
2985 && strncmp (s->name, ".note", 5) == 0)
2987 /* We need a PT_NOTE segment. */
2992 /* Let the backend count up any program headers it might need. */
2993 if (bed->elf_backend_additional_program_headers)
2997 a = (*bed->elf_backend_additional_program_headers) (abfd);
3003 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3004 return elf_tdata (abfd)->program_header_size;
3007 /* Work out the file positions of all the sections. This is called by
3008 _bfd_elf_compute_section_file_positions. All the section sizes and
3009 VMAs must be known before this is called.
3011 We do not consider reloc sections at this point, unless they form
3012 part of the loadable image. Reloc sections are assigned file
3013 positions in assign_file_positions_for_relocs, which is called by
3014 write_object_contents and final_link.
3016 We also don't set the positions of the .symtab and .strtab here. */
3019 assign_file_positions_except_relocs (abfd)
3022 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3023 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3024 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3026 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3028 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3029 && bfd_get_format (abfd) != bfd_core)
3031 Elf_Internal_Shdr **hdrpp;
3034 /* Start after the ELF header. */
3035 off = i_ehdrp->e_ehsize;
3037 /* We are not creating an executable, which means that we are
3038 not creating a program header, and that the actual order of
3039 the sections in the file is unimportant. */
3040 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3042 Elf_Internal_Shdr *hdr;
3045 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3047 hdr->sh_offset = -1;
3050 if (i == tdata->symtab_section
3051 || i == tdata->strtab_section)
3053 hdr->sh_offset = -1;
3057 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3063 Elf_Internal_Shdr **hdrpp;
3065 /* Assign file positions for the loaded sections based on the
3066 assignment of sections to segments. */
3067 if (! assign_file_positions_for_segments (abfd))
3070 /* Assign file positions for the other sections. */
3072 off = elf_tdata (abfd)->next_file_pos;
3073 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3075 Elf_Internal_Shdr *hdr;
3078 if (hdr->bfd_section != NULL
3079 && hdr->bfd_section->filepos != 0)
3080 hdr->sh_offset = hdr->bfd_section->filepos;
3081 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3083 ((*_bfd_error_handler)
3084 (_("%s: warning: allocated section `%s' not in segment"),
3085 bfd_get_filename (abfd),
3086 (hdr->bfd_section == NULL
3088 : hdr->bfd_section->name)));
3089 if ((abfd->flags & D_PAGED) != 0)
3090 off += (hdr->sh_addr - off) % bed->maxpagesize;
3092 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3093 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3096 else if (hdr->sh_type == SHT_REL
3097 || hdr->sh_type == SHT_RELA
3098 || hdr == i_shdrpp[tdata->symtab_section]
3099 || hdr == i_shdrpp[tdata->strtab_section])
3100 hdr->sh_offset = -1;
3102 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3106 /* Place the section headers. */
3107 off = align_file_position (off, bed->s->file_align);
3108 i_ehdrp->e_shoff = off;
3109 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3111 elf_tdata (abfd)->next_file_pos = off;
3120 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3121 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3122 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3124 struct bfd_strtab_hash *shstrtab;
3125 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3127 i_ehdrp = elf_elfheader (abfd);
3128 i_shdrp = elf_elfsections (abfd);
3130 shstrtab = _bfd_elf_stringtab_init ();
3131 if (shstrtab == NULL)
3134 elf_shstrtab (abfd) = shstrtab;
3136 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3137 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3138 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3139 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3141 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3142 i_ehdrp->e_ident[EI_DATA] =
3143 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3144 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3146 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_SYSV;
3147 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3149 for (count = EI_PAD; count < EI_NIDENT; count++)
3150 i_ehdrp->e_ident[count] = 0;
3152 if ((abfd->flags & DYNAMIC) != 0)
3153 i_ehdrp->e_type = ET_DYN;
3154 else if ((abfd->flags & EXEC_P) != 0)
3155 i_ehdrp->e_type = ET_EXEC;
3156 else if (bfd_get_format (abfd) == bfd_core)
3157 i_ehdrp->e_type = ET_CORE;
3159 i_ehdrp->e_type = ET_REL;
3161 switch (bfd_get_arch (abfd))
3163 case bfd_arch_unknown:
3164 i_ehdrp->e_machine = EM_NONE;
3166 case bfd_arch_sparc:
3167 if (bed->s->arch_size == 64)
3168 i_ehdrp->e_machine = EM_SPARCV9;
3170 i_ehdrp->e_machine = EM_SPARC;
3173 i_ehdrp->e_machine = EM_386;
3176 i_ehdrp->e_machine = EM_68K;
3179 i_ehdrp->e_machine = EM_88K;
3182 i_ehdrp->e_machine = EM_860;
3185 i_ehdrp->e_machine = EM_960;
3187 case bfd_arch_mips: /* MIPS Rxxxx */
3188 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3191 i_ehdrp->e_machine = EM_PARISC;
3193 case bfd_arch_powerpc:
3194 i_ehdrp->e_machine = EM_PPC;
3196 case bfd_arch_alpha:
3197 i_ehdrp->e_machine = EM_ALPHA;
3200 i_ehdrp->e_machine = EM_SH;
3203 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3206 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3209 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3211 case bfd_arch_mcore:
3212 i_ehdrp->e_machine = EM_MCORE;
3215 switch (bfd_get_mach (abfd))
3218 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3222 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3225 i_ehdrp->e_machine = EM_ARM;
3228 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3230 case bfd_arch_mn10200:
3231 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3233 case bfd_arch_mn10300:
3234 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3237 i_ehdrp->e_machine = EM_PJ;
3239 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3241 i_ehdrp->e_machine = EM_NONE;
3243 i_ehdrp->e_version = bed->s->ev_current;
3244 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3246 /* no program header, for now. */
3247 i_ehdrp->e_phoff = 0;
3248 i_ehdrp->e_phentsize = 0;
3249 i_ehdrp->e_phnum = 0;
3251 /* each bfd section is section header entry */
3252 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3253 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3255 /* if we're building an executable, we'll need a program header table */
3256 if (abfd->flags & EXEC_P)
3258 /* it all happens later */
3260 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3262 /* elf_build_phdrs() returns a (NULL-terminated) array of
3263 Elf_Internal_Phdrs */
3264 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3265 i_ehdrp->e_phoff = outbase;
3266 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3271 i_ehdrp->e_phentsize = 0;
3273 i_ehdrp->e_phoff = 0;
3276 elf_tdata (abfd)->symtab_hdr.sh_name =
3277 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3278 elf_tdata (abfd)->strtab_hdr.sh_name =
3279 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3280 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3281 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3282 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3283 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3284 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3290 /* Assign file positions for all the reloc sections which are not part
3291 of the loadable file image. */
3294 _bfd_elf_assign_file_positions_for_relocs (abfd)
3299 Elf_Internal_Shdr **shdrpp;
3301 off = elf_tdata (abfd)->next_file_pos;
3303 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3304 i < elf_elfheader (abfd)->e_shnum;
3307 Elf_Internal_Shdr *shdrp;
3310 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3311 && shdrp->sh_offset == -1)
3312 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3315 elf_tdata (abfd)->next_file_pos = off;
3319 _bfd_elf_write_object_contents (abfd)
3322 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3323 Elf_Internal_Ehdr *i_ehdrp;
3324 Elf_Internal_Shdr **i_shdrp;
3328 if (! abfd->output_has_begun
3329 && ! _bfd_elf_compute_section_file_positions
3330 (abfd, (struct bfd_link_info *) NULL))
3333 i_shdrp = elf_elfsections (abfd);
3334 i_ehdrp = elf_elfheader (abfd);
3337 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3341 _bfd_elf_assign_file_positions_for_relocs (abfd);
3343 /* After writing the headers, we need to write the sections too... */
3344 for (count = 1; count < i_ehdrp->e_shnum; count++)
3346 if (bed->elf_backend_section_processing)
3347 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3348 if (i_shdrp[count]->contents)
3350 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3351 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3353 != i_shdrp[count]->sh_size))
3358 /* Write out the section header names. */
3359 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3360 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3363 if (bed->elf_backend_final_write_processing)
3364 (*bed->elf_backend_final_write_processing) (abfd,
3365 elf_tdata (abfd)->linker);
3367 return bed->s->write_shdrs_and_ehdr (abfd);
3371 _bfd_elf_write_corefile_contents (abfd)
3374 /* Hopefully this can be done just like an object file. */
3375 return _bfd_elf_write_object_contents (abfd);
3377 /* given a section, search the header to find them... */
3379 _bfd_elf_section_from_bfd_section (abfd, asect)
3383 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3384 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3386 Elf_Internal_Shdr *hdr;
3387 int maxindex = elf_elfheader (abfd)->e_shnum;
3389 for (index = 0; index < maxindex; index++)
3391 hdr = i_shdrp[index];
3392 if (hdr->bfd_section == asect)
3396 if (bed->elf_backend_section_from_bfd_section)
3398 for (index = 0; index < maxindex; index++)
3402 hdr = i_shdrp[index];
3404 if ((*bed->elf_backend_section_from_bfd_section)
3405 (abfd, hdr, asect, &retval))
3410 if (bfd_is_abs_section (asect))
3412 if (bfd_is_com_section (asect))
3414 if (bfd_is_und_section (asect))
3417 bfd_set_error (bfd_error_nonrepresentable_section);
3422 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3426 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3428 asymbol **asym_ptr_ptr;
3430 asymbol *asym_ptr = *asym_ptr_ptr;
3432 flagword flags = asym_ptr->flags;
3434 /* When gas creates relocations against local labels, it creates its
3435 own symbol for the section, but does put the symbol into the
3436 symbol chain, so udata is 0. When the linker is generating
3437 relocatable output, this section symbol may be for one of the
3438 input sections rather than the output section. */
3439 if (asym_ptr->udata.i == 0
3440 && (flags & BSF_SECTION_SYM)
3441 && asym_ptr->section)
3445 if (asym_ptr->section->output_section != NULL)
3446 indx = asym_ptr->section->output_section->index;
3448 indx = asym_ptr->section->index;
3449 if (elf_section_syms (abfd)[indx])
3450 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3453 idx = asym_ptr->udata.i;
3457 /* This case can occur when using --strip-symbol on a symbol
3458 which is used in a relocation entry. */
3459 (*_bfd_error_handler)
3460 (_("%s: symbol `%s' required but not present"),
3461 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3462 bfd_set_error (bfd_error_no_symbols);
3469 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3470 (long) asym_ptr, asym_ptr->name, idx, flags,
3471 elf_symbol_flags (flags));
3479 /* Copy private BFD data. This copies any program header information. */
3482 copy_private_bfd_data (ibfd, obfd)
3486 Elf_Internal_Ehdr *iehdr;
3487 struct elf_segment_map *mfirst;
3488 struct elf_segment_map **pm;
3489 struct elf_segment_map *m;
3490 Elf_Internal_Phdr *p;
3492 unsigned int num_segments;
3493 boolean phdr_included = false;
3495 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3496 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3499 if (elf_tdata (ibfd)->phdr == NULL)
3502 iehdr = elf_elfheader (ibfd);
3507 num_segments = elf_elfheader (ibfd)->e_phnum;
3509 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3510 ((addr) >= (bottom) \
3511 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3512 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3514 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3516 #define IS_COREFILE_NOTE(p, s) \
3517 (p->p_type == PT_NOTE \
3518 && bfd_get_format (ibfd) == bfd_core \
3519 && s->vma == 0 && s->lma == 0 \
3520 && (bfd_vma) s->filepos >= p->p_offset \
3521 && (bfd_vma) s->filepos + s->_raw_size \
3522 <= p->p_offset + p->p_filesz)
3524 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3525 linker, which generates a PT_INTERP section with p_vaddr and
3526 p_memsz set to 0. */
3528 #define IS_SOLARIS_PT_INTERP(p, s) \
3530 && p->p_filesz > 0 \
3531 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3532 && s->_raw_size > 0 \
3533 && (bfd_vma) s->filepos >= p->p_offset \
3534 && ((bfd_vma) s->filepos + s->_raw_size \
3535 <= p->p_offset + p->p_filesz))
3537 /* Scan through the segments specified in the program header
3538 of the input BFD. */
3539 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3543 asection **sections;
3546 bfd_vma matching_lma;
3547 bfd_vma suggested_lma;
3550 /* For each section in the input BFD, decide if it should be
3551 included in the current segment. A section will be included
3552 if it is within the address space of the segment, and it is
3553 an allocated segment, and there is an output section
3554 associated with it. */
3556 for (s = ibfd->sections; s != NULL; s = s->next)
3557 if (s->output_section != NULL)
3559 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3560 || IS_SOLARIS_PT_INTERP (p, s))
3561 && (s->flags & SEC_ALLOC) != 0)
3563 else if (IS_COREFILE_NOTE (p, s))
3567 /* Allocate a segment map big enough to contain all of the
3568 sections we have selected. */
3569 m = ((struct elf_segment_map *)
3571 (sizeof (struct elf_segment_map)
3572 + ((size_t) csecs - 1) * sizeof (asection *))));
3576 /* Initialise the fields of the segment map. Default to
3577 using the physical address of the segment in the input BFD. */
3579 m->p_type = p->p_type;
3580 m->p_flags = p->p_flags;
3581 m->p_flags_valid = 1;
3582 m->p_paddr = p->p_paddr;
3583 m->p_paddr_valid = 1;
3585 /* Determine if this segment contains the ELF file header
3586 and if it contains the program headers themselves. */
3587 m->includes_filehdr = (p->p_offset == 0
3588 && p->p_filesz >= iehdr->e_ehsize);
3590 m->includes_phdrs = 0;
3592 if (! phdr_included || p->p_type != PT_LOAD)
3595 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3596 && (p->p_offset + p->p_filesz
3597 >= ((bfd_vma) iehdr->e_phoff
3598 + iehdr->e_phnum * iehdr->e_phentsize)));
3599 if (p->p_type == PT_LOAD && m->includes_phdrs)
3600 phdr_included = true;
3605 /* Special segments, such as the PT_PHDR segment, may contain
3606 no sections, but ordinary, loadable segments should contain
3609 if (p->p_type == PT_LOAD)
3611 (_("%s: warning: Empty loadable segment detected\n"),
3612 bfd_get_filename (ibfd));
3621 /* Now scan the sections in the input BFD again and attempt
3622 to add their corresponding output sections to the segment map.
3623 The problem here is how to handle an output section which has
3624 been moved (ie had its LMA changed). There are four possibilities:
3626 1. None of the sections have been moved.
3627 In this case we can continue to use the segment LMA from the
3630 2. All of the sections have been moved by the same amount.
3631 In this case we can change the segment's LMA to match the LMA
3632 of the first section.
3634 3. Some of the sections have been moved, others have not.
3635 In this case those sections which have not been moved can be
3636 placed in the current segment which will have to have its size,
3637 and possibly its LMA changed, and a new segment or segments will
3638 have to be created to contain the other sections.
3640 4. The sections have been moved, but not be the same amount.
3641 In this case we can change the segment's LMA to match the LMA
3642 of the first section and we will have to create a new segment
3643 or segments to contain the other sections.
3645 In order to save time, we allocate an array to hold the section
3646 pointers that we are interested in. As these sections get assigned
3647 to a segment, they are removed from this array. */
3649 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3650 if (sections == NULL)
3653 /* Step One: Scan for segment vs section LMA conflicts.
3654 Also add the sections to the section array allocated above.
3655 Also add the sections to the current segment. In the common
3656 case, where the sections have not been moved, this means that
3657 we have completely filled the segment, and there is nothing
3661 matching_lma = false;
3664 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3666 os = s->output_section;
3668 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3669 || IS_SOLARIS_PT_INTERP (p, s))
3670 && (s->flags & SEC_ALLOC) != 0)
3671 || IS_COREFILE_NOTE (p, s))
3676 /* The Solaris native linker always sets p_paddr to 0.
3677 We try to catch that case here, and set it to the
3683 && (os->vma == (p->p_vaddr
3684 + (m->includes_filehdr
3687 + (m->includes_phdrs
3688 ? iehdr->e_phnum * iehdr->e_phentsize
3690 m->p_paddr = p->p_vaddr;
3692 /* Match up the physical address of the segment with the
3693 LMA address of the output section. */
3694 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3695 || IS_COREFILE_NOTE (p, s))
3697 if (matching_lma == 0)
3698 matching_lma = os->lma;
3700 /* We assume that if the section fits within the segment
3701 that it does not overlap any other section within that
3703 m->sections[isec++] = os;
3705 else if (suggested_lma == 0)
3706 suggested_lma = os->lma;
3710 BFD_ASSERT (j == csecs);
3712 /* Step Two: Adjust the physical address of the current segment,
3716 /* All of the sections fitted within the segment as currently
3717 specified. This is the default case. Add the segment to
3718 the list of built segments and carry on to process the next
3719 program header in the input BFD. */
3727 else if (matching_lma != 0)
3729 /* At least one section fits inside the current segment.
3730 Keep it, but modify its physical address to match the
3731 LMA of the first section that fitted. */
3733 m->p_paddr = matching_lma;
3737 /* None of the sections fitted inside the current segment.
3738 Change the current segment's physical address to match
3739 the LMA of the first section. */
3741 m->p_paddr = suggested_lma;
3744 /* Step Three: Loop over the sections again, this time assigning
3745 those that fit to the current segment and remvoing them from the
3746 sections array; but making sure not to leave large gaps. Once all
3747 possible sections have been assigned to the current segment it is
3748 added to the list of built segments and if sections still remain
3749 to be assigned, a new segment is constructed before repeating
3757 /* Fill the current segment with sections that fit. */
3758 for (j = 0; j < csecs; j++)
3765 os = s->output_section;
3767 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3768 || IS_COREFILE_NOTE (p, s))
3772 /* If the first section in a segment does not start at
3773 the beginning of the segment, then something is wrong. */
3774 if (os->lma != m->p_paddr)
3779 asection * prev_sec;
3780 bfd_vma maxpagesize;
3782 prev_sec = m->sections[m->count - 1];
3783 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3785 /* If the gap between the end of the previous section
3786 and the start of this section is more than maxpagesize
3787 then we need to start a new segment. */
3788 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3789 < BFD_ALIGN (os->lma, maxpagesize))
3791 if (suggested_lma == 0)
3792 suggested_lma = os->lma;
3798 m->sections[m->count++] = os;
3802 else if (suggested_lma == 0)
3803 suggested_lma = os->lma;
3806 BFD_ASSERT (m->count > 0);
3808 /* Add the current segment to the list of built segments. */
3814 /* We still have not allocated all of the sections to
3815 segments. Create a new segment here, initialise it
3816 and carry on looping. */
3818 m = ((struct elf_segment_map *)
3820 (sizeof (struct elf_segment_map)
3821 + ((size_t) csecs - 1) * sizeof (asection *))));
3825 /* Initialise the fields of the segment map. Set the physical
3826 physical address to the LMA of the first section that has
3827 not yet been assigned. */
3830 m->p_type = p->p_type;
3831 m->p_flags = p->p_flags;
3832 m->p_flags_valid = 1;
3833 m->p_paddr = suggested_lma;
3834 m->p_paddr_valid = 1;
3835 m->includes_filehdr = 0;
3836 m->includes_phdrs = 0;
3839 while (isec < csecs);
3844 /* The Solaris linker creates program headers in which all the
3845 p_paddr fields are zero. When we try to objcopy or strip such a
3846 file, we get confused. Check for this case, and if we find it
3847 reset the p_paddr_valid fields. */
3848 for (m = mfirst; m != NULL; m = m->next)
3849 if (m->p_paddr != 0)
3853 for (m = mfirst; m != NULL; m = m->next)
3854 m->p_paddr_valid = 0;
3857 elf_tdata (obfd)->segment_map = mfirst;
3860 /* Final Step: Sort the segments into ascending order of physical address. */
3863 struct elf_segment_map* prev;
3866 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3868 /* Yes I know - its a bubble sort....*/
3869 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3871 /* swap m and m->next */
3872 prev->next = m->next;
3873 m->next = m->next->next;
3874 prev->next->next = m;
3883 #undef IS_CONTAINED_BY
3884 #undef IS_SOLARIS_PT_INTERP
3885 #undef IS_COREFILE_NOTE
3889 /* Copy private section information. This copies over the entsize
3890 field, and sometimes the info field. */
3893 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3899 Elf_Internal_Shdr *ihdr, *ohdr;
3901 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3902 || obfd->xvec->flavour != bfd_target_elf_flavour)
3905 /* Copy over private BFD data if it has not already been copied.
3906 This must be done here, rather than in the copy_private_bfd_data
3907 entry point, because the latter is called after the section
3908 contents have been set, which means that the program headers have
3909 already been worked out. */
3910 if (elf_tdata (obfd)->segment_map == NULL
3911 && elf_tdata (ibfd)->phdr != NULL)
3915 /* Only set up the segments if there are no more SEC_ALLOC
3916 sections. FIXME: This won't do the right thing if objcopy is
3917 used to remove the last SEC_ALLOC section, since objcopy
3918 won't call this routine in that case. */
3919 for (s = isec->next; s != NULL; s = s->next)
3920 if ((s->flags & SEC_ALLOC) != 0)
3924 if (! copy_private_bfd_data (ibfd, obfd))
3929 ihdr = &elf_section_data (isec)->this_hdr;
3930 ohdr = &elf_section_data (osec)->this_hdr;
3932 ohdr->sh_entsize = ihdr->sh_entsize;
3934 if (ihdr->sh_type == SHT_SYMTAB
3935 || ihdr->sh_type == SHT_DYNSYM
3936 || ihdr->sh_type == SHT_GNU_verneed
3937 || ihdr->sh_type == SHT_GNU_verdef)
3938 ohdr->sh_info = ihdr->sh_info;
3940 elf_section_data (osec)->use_rela_p
3941 = elf_section_data (isec)->use_rela_p;
3946 /* Copy private symbol information. If this symbol is in a section
3947 which we did not map into a BFD section, try to map the section
3948 index correctly. We use special macro definitions for the mapped
3949 section indices; these definitions are interpreted by the
3950 swap_out_syms function. */
3952 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3953 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3954 #define MAP_STRTAB (SHN_LORESERVE - 3)
3955 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3958 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3964 elf_symbol_type *isym, *osym;
3966 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3967 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3970 isym = elf_symbol_from (ibfd, isymarg);
3971 osym = elf_symbol_from (obfd, osymarg);
3975 && bfd_is_abs_section (isym->symbol.section))
3979 shndx = isym->internal_elf_sym.st_shndx;
3980 if (shndx == elf_onesymtab (ibfd))
3981 shndx = MAP_ONESYMTAB;
3982 else if (shndx == elf_dynsymtab (ibfd))
3983 shndx = MAP_DYNSYMTAB;
3984 else if (shndx == elf_tdata (ibfd)->strtab_section)
3986 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3987 shndx = MAP_SHSTRTAB;
3988 osym->internal_elf_sym.st_shndx = shndx;
3994 /* Swap out the symbols. */
3997 swap_out_syms (abfd, sttp, relocatable_p)
3999 struct bfd_strtab_hash **sttp;
4002 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4004 if (!elf_map_symbols (abfd))
4007 /* Dump out the symtabs. */
4009 int symcount = bfd_get_symcount (abfd);
4010 asymbol **syms = bfd_get_outsymbols (abfd);
4011 struct bfd_strtab_hash *stt;
4012 Elf_Internal_Shdr *symtab_hdr;
4013 Elf_Internal_Shdr *symstrtab_hdr;
4014 char *outbound_syms;
4017 stt = _bfd_elf_stringtab_init ();
4021 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4022 symtab_hdr->sh_type = SHT_SYMTAB;
4023 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4024 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4025 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4026 symtab_hdr->sh_addralign = bed->s->file_align;
4028 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4029 symstrtab_hdr->sh_type = SHT_STRTAB;
4031 outbound_syms = bfd_alloc (abfd,
4032 (1 + symcount) * bed->s->sizeof_sym);
4033 if (outbound_syms == NULL)
4035 symtab_hdr->contents = (PTR) outbound_syms;
4037 /* now generate the data (for "contents") */
4039 /* Fill in zeroth symbol and swap it out. */
4040 Elf_Internal_Sym sym;
4046 sym.st_shndx = SHN_UNDEF;
4047 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4048 outbound_syms += bed->s->sizeof_sym;
4050 for (idx = 0; idx < symcount; idx++)
4052 Elf_Internal_Sym sym;
4053 bfd_vma value = syms[idx]->value;
4054 elf_symbol_type *type_ptr;
4055 flagword flags = syms[idx]->flags;
4058 if (flags & BSF_SECTION_SYM)
4059 /* Section symbols have no names. */
4063 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4066 if (sym.st_name == (unsigned long) -1)
4070 type_ptr = elf_symbol_from (abfd, syms[idx]);
4072 if ((flags & BSF_SECTION_SYM) == 0
4073 && bfd_is_com_section (syms[idx]->section))
4075 /* ELF common symbols put the alignment into the `value' field,
4076 and the size into the `size' field. This is backwards from
4077 how BFD handles it, so reverse it here. */
4078 sym.st_size = value;
4079 if (type_ptr == NULL
4080 || type_ptr->internal_elf_sym.st_value == 0)
4081 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4083 sym.st_value = type_ptr->internal_elf_sym.st_value;
4084 sym.st_shndx = _bfd_elf_section_from_bfd_section
4085 (abfd, syms[idx]->section);
4089 asection *sec = syms[idx]->section;
4092 if (sec->output_section)
4094 value += sec->output_offset;
4095 sec = sec->output_section;
4097 /* Don't add in the section vma for relocatable output. */
4098 if (! relocatable_p)
4100 sym.st_value = value;
4101 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4103 if (bfd_is_abs_section (sec)
4105 && type_ptr->internal_elf_sym.st_shndx != 0)
4107 /* This symbol is in a real ELF section which we did
4108 not create as a BFD section. Undo the mapping done
4109 by copy_private_symbol_data. */
4110 shndx = type_ptr->internal_elf_sym.st_shndx;
4114 shndx = elf_onesymtab (abfd);
4117 shndx = elf_dynsymtab (abfd);
4120 shndx = elf_tdata (abfd)->strtab_section;
4123 shndx = elf_tdata (abfd)->shstrtab_section;
4131 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4137 /* Writing this would be a hell of a lot easier if
4138 we had some decent documentation on bfd, and
4139 knew what to expect of the library, and what to
4140 demand of applications. For example, it
4141 appears that `objcopy' might not set the
4142 section of a symbol to be a section that is
4143 actually in the output file. */
4144 sec2 = bfd_get_section_by_name (abfd, sec->name);
4145 BFD_ASSERT (sec2 != 0);
4146 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4147 BFD_ASSERT (shndx != -1);
4151 sym.st_shndx = shndx;
4154 if ((flags & BSF_FUNCTION) != 0)
4156 else if ((flags & BSF_OBJECT) != 0)
4161 /* Processor-specific types */
4162 if (bed->elf_backend_get_symbol_type)
4163 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4165 if (flags & BSF_SECTION_SYM)
4166 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4167 else if (bfd_is_com_section (syms[idx]->section))
4168 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4169 else if (bfd_is_und_section (syms[idx]->section))
4170 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4174 else if (flags & BSF_FILE)
4175 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4178 int bind = STB_LOCAL;
4180 if (flags & BSF_LOCAL)
4182 else if (flags & BSF_WEAK)
4184 else if (flags & BSF_GLOBAL)
4187 sym.st_info = ELF_ST_INFO (bind, type);
4190 if (type_ptr != NULL)
4191 sym.st_other = type_ptr->internal_elf_sym.st_other;
4195 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4196 outbound_syms += bed->s->sizeof_sym;
4200 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4201 symstrtab_hdr->sh_type = SHT_STRTAB;
4203 symstrtab_hdr->sh_flags = 0;
4204 symstrtab_hdr->sh_addr = 0;
4205 symstrtab_hdr->sh_entsize = 0;
4206 symstrtab_hdr->sh_link = 0;
4207 symstrtab_hdr->sh_info = 0;
4208 symstrtab_hdr->sh_addralign = 1;
4214 /* Return the number of bytes required to hold the symtab vector.
4216 Note that we base it on the count plus 1, since we will null terminate
4217 the vector allocated based on this size. However, the ELF symbol table
4218 always has a dummy entry as symbol #0, so it ends up even. */
4221 _bfd_elf_get_symtab_upper_bound (abfd)
4226 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4228 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4229 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4235 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4240 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4242 if (elf_dynsymtab (abfd) == 0)
4244 bfd_set_error (bfd_error_invalid_operation);
4248 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4249 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4255 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4256 bfd *abfd ATTRIBUTE_UNUSED;
4259 return (asect->reloc_count + 1) * sizeof (arelent *);
4262 /* Canonicalize the relocs. */
4265 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4274 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4280 tblptr = section->relocation;
4281 for (i = 0; i < section->reloc_count; i++)
4282 *relptr++ = tblptr++;
4286 return section->reloc_count;
4290 _bfd_elf_get_symtab (abfd, alocation)
4292 asymbol **alocation;
4294 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4295 (abfd, alocation, false);
4298 bfd_get_symcount (abfd) = symcount;
4303 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4305 asymbol **alocation;
4307 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4308 (abfd, alocation, true);
4311 /* Return the size required for the dynamic reloc entries. Any
4312 section that was actually installed in the BFD, and has type
4313 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4314 considered to be a dynamic reloc section. */
4317 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4323 if (elf_dynsymtab (abfd) == 0)
4325 bfd_set_error (bfd_error_invalid_operation);
4329 ret = sizeof (arelent *);
4330 for (s = abfd->sections; s != NULL; s = s->next)
4331 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4332 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4333 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4334 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4335 * sizeof (arelent *));
4340 /* Canonicalize the dynamic relocation entries. Note that we return
4341 the dynamic relocations as a single block, although they are
4342 actually associated with particular sections; the interface, which
4343 was designed for SunOS style shared libraries, expects that there
4344 is only one set of dynamic relocs. Any section that was actually
4345 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4346 the dynamic symbol table, is considered to be a dynamic reloc
4350 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4355 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4359 if (elf_dynsymtab (abfd) == 0)
4361 bfd_set_error (bfd_error_invalid_operation);
4365 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4367 for (s = abfd->sections; s != NULL; s = s->next)
4369 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4370 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4371 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4376 if (! (*slurp_relocs) (abfd, s, syms, true))
4378 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4380 for (i = 0; i < count; i++)
4391 /* Read in the version information. */
4394 _bfd_elf_slurp_version_tables (abfd)
4397 bfd_byte *contents = NULL;
4399 if (elf_dynverdef (abfd) != 0)
4401 Elf_Internal_Shdr *hdr;
4402 Elf_External_Verdef *everdef;
4403 Elf_Internal_Verdef *iverdef;
4406 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4408 elf_tdata (abfd)->verdef =
4409 ((Elf_Internal_Verdef *)
4410 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4411 if (elf_tdata (abfd)->verdef == NULL)
4414 elf_tdata (abfd)->cverdefs = hdr->sh_info;
4416 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4417 if (contents == NULL)
4419 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4420 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4423 everdef = (Elf_External_Verdef *) contents;
4424 iverdef = elf_tdata (abfd)->verdef;
4425 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4427 Elf_External_Verdaux *everdaux;
4428 Elf_Internal_Verdaux *iverdaux;
4431 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4433 iverdef->vd_bfd = abfd;
4435 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4438 * sizeof (Elf_Internal_Verdaux))));
4439 if (iverdef->vd_auxptr == NULL)
4442 everdaux = ((Elf_External_Verdaux *)
4443 ((bfd_byte *) everdef + iverdef->vd_aux));
4444 iverdaux = iverdef->vd_auxptr;
4445 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4447 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4449 iverdaux->vda_nodename =
4450 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4451 iverdaux->vda_name);
4452 if (iverdaux->vda_nodename == NULL)
4455 if (j + 1 < iverdef->vd_cnt)
4456 iverdaux->vda_nextptr = iverdaux + 1;
4458 iverdaux->vda_nextptr = NULL;
4460 everdaux = ((Elf_External_Verdaux *)
4461 ((bfd_byte *) everdaux + iverdaux->vda_next));
4464 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4466 if (i + 1 < hdr->sh_info)
4467 iverdef->vd_nextdef = iverdef + 1;
4469 iverdef->vd_nextdef = NULL;
4471 everdef = ((Elf_External_Verdef *)
4472 ((bfd_byte *) everdef + iverdef->vd_next));
4479 if (elf_dynverref (abfd) != 0)
4481 Elf_Internal_Shdr *hdr;
4482 Elf_External_Verneed *everneed;
4483 Elf_Internal_Verneed *iverneed;
4486 hdr = &elf_tdata (abfd)->dynverref_hdr;
4488 elf_tdata (abfd)->verref =
4489 ((Elf_Internal_Verneed *)
4490 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4491 if (elf_tdata (abfd)->verref == NULL)
4494 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4496 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4497 if (contents == NULL)
4499 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4500 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4503 everneed = (Elf_External_Verneed *) contents;
4504 iverneed = elf_tdata (abfd)->verref;
4505 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4507 Elf_External_Vernaux *evernaux;
4508 Elf_Internal_Vernaux *ivernaux;
4511 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4513 iverneed->vn_bfd = abfd;
4515 iverneed->vn_filename =
4516 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4518 if (iverneed->vn_filename == NULL)
4521 iverneed->vn_auxptr =
4522 ((Elf_Internal_Vernaux *)
4524 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4526 evernaux = ((Elf_External_Vernaux *)
4527 ((bfd_byte *) everneed + iverneed->vn_aux));
4528 ivernaux = iverneed->vn_auxptr;
4529 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4531 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4533 ivernaux->vna_nodename =
4534 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4535 ivernaux->vna_name);
4536 if (ivernaux->vna_nodename == NULL)
4539 if (j + 1 < iverneed->vn_cnt)
4540 ivernaux->vna_nextptr = ivernaux + 1;
4542 ivernaux->vna_nextptr = NULL;
4544 evernaux = ((Elf_External_Vernaux *)
4545 ((bfd_byte *) evernaux + ivernaux->vna_next));
4548 if (i + 1 < hdr->sh_info)
4549 iverneed->vn_nextref = iverneed + 1;
4551 iverneed->vn_nextref = NULL;
4553 everneed = ((Elf_External_Verneed *)
4554 ((bfd_byte *) everneed + iverneed->vn_next));
4564 if (contents == NULL)
4570 _bfd_elf_make_empty_symbol (abfd)
4573 elf_symbol_type *newsym;
4575 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4580 newsym->symbol.the_bfd = abfd;
4581 return &newsym->symbol;
4586 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4587 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4591 bfd_symbol_info (symbol, ret);
4594 /* Return whether a symbol name implies a local symbol. Most targets
4595 use this function for the is_local_label_name entry point, but some
4599 _bfd_elf_is_local_label_name (abfd, name)
4600 bfd *abfd ATTRIBUTE_UNUSED;
4603 /* Normal local symbols start with ``.L''. */
4604 if (name[0] == '.' && name[1] == 'L')
4607 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4608 DWARF debugging symbols starting with ``..''. */
4609 if (name[0] == '.' && name[1] == '.')
4612 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4613 emitting DWARF debugging output. I suspect this is actually a
4614 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4615 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4616 underscore to be emitted on some ELF targets). For ease of use,
4617 we treat such symbols as local. */
4618 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4625 _bfd_elf_get_lineno (ignore_abfd, symbol)
4626 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4627 asymbol *symbol ATTRIBUTE_UNUSED;
4634 _bfd_elf_set_arch_mach (abfd, arch, machine)
4636 enum bfd_architecture arch;
4637 unsigned long machine;
4639 /* If this isn't the right architecture for this backend, and this
4640 isn't the generic backend, fail. */
4641 if (arch != get_elf_backend_data (abfd)->arch
4642 && arch != bfd_arch_unknown
4643 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4646 return bfd_default_set_arch_mach (abfd, arch, machine);
4649 /* Find the nearest line to a particular section and offset, for error
4653 _bfd_elf_find_nearest_line (abfd,
4664 CONST char **filename_ptr;
4665 CONST char **functionname_ptr;
4666 unsigned int *line_ptr;
4669 const char *filename;
4674 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4675 filename_ptr, functionname_ptr,
4679 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4680 filename_ptr, functionname_ptr,
4684 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4685 &found, filename_ptr,
4686 functionname_ptr, line_ptr,
4687 &elf_tdata (abfd)->line_info))
4692 if (symbols == NULL)
4699 for (p = symbols; *p != NULL; p++)
4703 q = (elf_symbol_type *) *p;
4705 if (bfd_get_section (&q->symbol) != section)
4708 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4713 filename = bfd_asymbol_name (&q->symbol);
4717 if (q->symbol.section == section
4718 && q->symbol.value >= low_func
4719 && q->symbol.value <= offset)
4721 func = (asymbol *) q;
4722 low_func = q->symbol.value;
4731 *filename_ptr = filename;
4732 *functionname_ptr = bfd_asymbol_name (func);
4738 _bfd_elf_sizeof_headers (abfd, reloc)
4744 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4746 ret += get_program_header_size (abfd);
4751 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4756 bfd_size_type count;
4758 Elf_Internal_Shdr *hdr;
4760 if (! abfd->output_has_begun
4761 && ! _bfd_elf_compute_section_file_positions
4762 (abfd, (struct bfd_link_info *) NULL))
4765 hdr = &elf_section_data (section)->this_hdr;
4767 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4769 if (bfd_write (location, 1, count, abfd) != count)
4776 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4777 bfd *abfd ATTRIBUTE_UNUSED;
4778 arelent *cache_ptr ATTRIBUTE_UNUSED;
4779 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
4786 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4789 Elf_Internal_Rel *dst;
4795 /* Try to convert a non-ELF reloc into an ELF one. */
4798 _bfd_elf_validate_reloc (abfd, areloc)
4802 /* Check whether we really have an ELF howto. */
4804 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4806 bfd_reloc_code_real_type code;
4807 reloc_howto_type *howto;
4809 /* Alien reloc: Try to determine its type to replace it with an
4810 equivalent ELF reloc. */
4812 if (areloc->howto->pc_relative)
4814 switch (areloc->howto->bitsize)
4817 code = BFD_RELOC_8_PCREL;
4820 code = BFD_RELOC_12_PCREL;
4823 code = BFD_RELOC_16_PCREL;
4826 code = BFD_RELOC_24_PCREL;
4829 code = BFD_RELOC_32_PCREL;
4832 code = BFD_RELOC_64_PCREL;
4838 howto = bfd_reloc_type_lookup (abfd, code);
4840 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4842 if (howto->pcrel_offset)
4843 areloc->addend += areloc->address;
4845 areloc->addend -= areloc->address; /* addend is unsigned!! */
4850 switch (areloc->howto->bitsize)
4856 code = BFD_RELOC_14;
4859 code = BFD_RELOC_16;
4862 code = BFD_RELOC_26;
4865 code = BFD_RELOC_32;
4868 code = BFD_RELOC_64;
4874 howto = bfd_reloc_type_lookup (abfd, code);
4878 areloc->howto = howto;
4886 (*_bfd_error_handler)
4887 (_("%s: unsupported relocation type %s"),
4888 bfd_get_filename (abfd), areloc->howto->name);
4889 bfd_set_error (bfd_error_bad_value);
4894 _bfd_elf_close_and_cleanup (abfd)
4897 if (bfd_get_format (abfd) == bfd_object)
4899 if (elf_shstrtab (abfd) != NULL)
4900 _bfd_stringtab_free (elf_shstrtab (abfd));
4903 return _bfd_generic_close_and_cleanup (abfd);
4906 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4907 in the relocation's offset. Thus we cannot allow any sort of sanity
4908 range-checking to interfere. There is nothing else to do in processing
4911 bfd_reloc_status_type
4912 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
4913 bfd *abfd ATTRIBUTE_UNUSED;
4914 arelent *re ATTRIBUTE_UNUSED;
4915 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
4916 PTR data ATTRIBUTE_UNUSED;
4917 asection *is ATTRIBUTE_UNUSED;
4918 bfd *obfd ATTRIBUTE_UNUSED;
4919 char **errmsg ATTRIBUTE_UNUSED;
4921 return bfd_reloc_ok;
4925 /* Elf core file support. Much of this only works on native
4926 toolchains, since we rely on knowing the
4927 machine-dependent procfs structure in order to pick
4928 out details about the corefile. */
4930 #ifdef HAVE_SYS_PROCFS_H
4931 # include <sys/procfs.h>
4935 /* Define offsetof for those systems which lack it. */
4938 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
4942 /* FIXME: this is kinda wrong, but it's what gdb wants. */
4945 elfcore_make_pid (abfd)
4948 return ((elf_tdata (abfd)->core_lwpid << 16)
4949 + (elf_tdata (abfd)->core_pid));
4953 /* If there isn't a section called NAME, make one, using
4954 data from SECT. Note, this function will generate a
4955 reference to NAME, so you shouldn't deallocate or
4959 elfcore_maybe_make_sect (abfd, name, sect)
4966 if (bfd_get_section_by_name (abfd, name) != NULL)
4969 sect2 = bfd_make_section (abfd, name);
4973 sect2->_raw_size = sect->_raw_size;
4974 sect2->filepos = sect->filepos;
4975 sect2->flags = sect->flags;
4976 sect2->alignment_power = sect->alignment_power;
4981 /* prstatus_t exists on:
4983 linux 2.[01] + glibc
4987 #if defined (HAVE_PRSTATUS_T)
4989 elfcore_grok_prstatus (abfd, note)
4991 Elf_Internal_Note* note;
4998 if (note->descsz != sizeof (prstat))
5001 memcpy (&prstat, note->descdata, sizeof (prstat));
5003 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5004 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5006 /* pr_who exists on:
5009 pr_who doesn't exist on:
5012 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5013 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5016 /* Make a ".reg/999" section. */
5018 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5019 name = bfd_alloc (abfd, strlen (buf) + 1);
5024 sect = bfd_make_section (abfd, name);
5027 sect->_raw_size = sizeof (prstat.pr_reg);
5028 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
5029 sect->flags = SEC_HAS_CONTENTS;
5030 sect->alignment_power = 2;
5032 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5037 #endif /* defined (HAVE_PRSTATUS_T) */
5040 /* Create a pseudosection containing the exact contents of NOTE. This
5041 actually creates up to two pseudosections:
5042 - For the single-threaded case, a section named NAME, unless
5043 such a section already exists.
5044 - For the multi-threaded case, a section named "NAME/PID", where
5045 PID is elfcore_make_pid (abfd).
5046 Both pseudosections have identical contents: the contents of NOTE. */
5049 elfcore_make_note_pseudosection (abfd, name, note)
5052 Elf_Internal_Note* note;
5055 char *threaded_name;
5058 /* Build the section name. */
5060 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5061 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5062 if (threaded_name == NULL)
5064 strcpy (threaded_name, buf);
5066 sect = bfd_make_section (abfd, threaded_name);
5069 sect->_raw_size = note->descsz;
5070 sect->filepos = note->descpos;
5071 sect->flags = SEC_HAS_CONTENTS;
5072 sect->alignment_power = 2;
5074 if (! elfcore_maybe_make_sect (abfd, name, sect))
5081 /* There isn't a consistent prfpregset_t across platforms,
5082 but it doesn't matter, because we don't have to pick this
5083 data structure apart. */
5085 elfcore_grok_prfpreg (abfd, note)
5087 Elf_Internal_Note* note;
5089 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5093 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5094 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5097 elfcore_grok_prxfpreg (abfd, note)
5099 Elf_Internal_Note* note;
5101 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5105 #if defined (HAVE_PRPSINFO_T)
5106 # define elfcore_psinfo_t prpsinfo_t
5109 #if defined (HAVE_PSINFO_T)
5110 # define elfcore_psinfo_t psinfo_t
5114 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5116 /* return a malloc'ed copy of a string at START which is at
5117 most MAX bytes long, possibly without a terminating '\0'.
5118 the copy will always have a terminating '\0'. */
5121 elfcore_strndup (abfd, start, max)
5127 char* end = memchr (start, '\0', max);
5135 dup = bfd_alloc (abfd, len + 1);
5139 memcpy (dup, start, len);
5146 elfcore_grok_psinfo (abfd, note)
5148 Elf_Internal_Note* note;
5150 elfcore_psinfo_t psinfo;
5152 if (note->descsz != sizeof (elfcore_psinfo_t))
5155 memcpy (&psinfo, note->descdata, note->descsz);
5157 elf_tdata (abfd)->core_program
5158 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5160 elf_tdata (abfd)->core_command
5161 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5163 /* Note that for some reason, a spurious space is tacked
5164 onto the end of the args in some (at least one anyway)
5165 implementations, so strip it off if it exists. */
5168 char* command = elf_tdata (abfd)->core_command;
5169 int n = strlen (command);
5171 if (0 < n && command[n - 1] == ' ')
5172 command[n - 1] = '\0';
5177 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5180 #if defined (HAVE_PSTATUS_T)
5182 elfcore_grok_pstatus (abfd, note)
5184 Elf_Internal_Note* note;
5188 if (note->descsz != sizeof (pstat))
5191 memcpy (&pstat, note->descdata, sizeof (pstat));
5193 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5195 /* Could grab some more details from the "representative"
5196 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5197 NT_LWPSTATUS note, presumably. */
5201 #endif /* defined (HAVE_PSTATUS_T) */
5204 #if defined (HAVE_LWPSTATUS_T)
5206 elfcore_grok_lwpstatus (abfd, note)
5208 Elf_Internal_Note* note;
5210 lwpstatus_t lwpstat;
5215 if (note->descsz != sizeof (lwpstat))
5218 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5220 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5221 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5223 /* Make a ".reg/999" section. */
5225 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5226 name = bfd_alloc (abfd, strlen (buf) + 1);
5231 sect = bfd_make_section (abfd, name);
5235 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5236 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5237 sect->filepos = note->descpos
5238 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5241 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5242 sect->_raw_size = sizeof (lwpstat.pr_reg);
5243 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5246 sect->flags = SEC_HAS_CONTENTS;
5247 sect->alignment_power = 2;
5249 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5252 /* Make a ".reg2/999" section */
5254 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5255 name = bfd_alloc (abfd, strlen (buf) + 1);
5260 sect = bfd_make_section (abfd, name);
5264 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5265 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5266 sect->filepos = note->descpos
5267 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5270 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5271 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5272 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5275 sect->flags = SEC_HAS_CONTENTS;
5276 sect->alignment_power = 2;
5278 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5283 #endif /* defined (HAVE_LWPSTATUS_T) */
5288 elfcore_grok_note (abfd, note)
5290 Elf_Internal_Note* note;
5297 #if defined (HAVE_PRSTATUS_T)
5299 return elfcore_grok_prstatus (abfd, note);
5302 #if defined (HAVE_PSTATUS_T)
5304 return elfcore_grok_pstatus (abfd, note);
5307 #if defined (HAVE_LWPSTATUS_T)
5309 return elfcore_grok_lwpstatus (abfd, note);
5312 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5313 return elfcore_grok_prfpreg (abfd, note);
5315 case NT_PRXFPREG: /* Linux SSE extension */
5316 if (note->namesz == 5
5317 && ! strcmp (note->namedata, "LINUX"))
5318 return elfcore_grok_prxfpreg (abfd, note);
5322 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5325 return elfcore_grok_psinfo (abfd, note);
5332 elfcore_read_notes (abfd, offset, size)
5343 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5346 buf = bfd_malloc ((size_t) size);
5350 if (bfd_read (buf, size, 1, abfd) != size)
5358 while (p < buf + size)
5360 /* FIXME: bad alignment assumption. */
5361 Elf_External_Note* xnp = (Elf_External_Note*) p;
5362 Elf_Internal_Note in;
5364 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5366 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5367 in.namedata = xnp->name;
5369 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5370 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5371 in.descpos = offset + (in.descdata - buf);
5373 if (! elfcore_grok_note (abfd, &in))
5376 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5384 /* FIXME: This function is now unnecessary. Callers can just call
5385 bfd_section_from_phdr directly. */
5388 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5390 Elf_Internal_Phdr* phdr;
5393 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5401 /* Providing external access to the ELF program header table. */
5403 /* Return an upper bound on the number of bytes required to store a
5404 copy of ABFD's program header table entries. Return -1 if an error
5405 occurs; bfd_get_error will return an appropriate code. */
5407 bfd_get_elf_phdr_upper_bound (abfd)
5410 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5412 bfd_set_error (bfd_error_wrong_format);
5416 return (elf_elfheader (abfd)->e_phnum
5417 * sizeof (Elf_Internal_Phdr));
5421 /* Copy ABFD's program header table entries to *PHDRS. The entries
5422 will be stored as an array of Elf_Internal_Phdr structures, as
5423 defined in include/elf/internal.h. To find out how large the
5424 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5426 Return the number of program header table entries read, or -1 if an
5427 error occurs; bfd_get_error will return an appropriate code. */
5429 bfd_get_elf_phdrs (abfd, phdrs)
5435 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5437 bfd_set_error (bfd_error_wrong_format);
5441 num_phdrs = elf_elfheader (abfd)->e_phnum;
5442 memcpy (phdrs, elf_tdata (abfd)->phdr,
5443 num_phdrs * sizeof (Elf_Internal_Phdr));