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 *));
57 /* Swap version information in and out. The version information is
58 currently size independent. If that ever changes, this code will
59 need to move into elfcode.h. */
61 /* Swap in a Verdef structure. */
64 _bfd_elf_swap_verdef_in (abfd, src, dst)
66 const Elf_External_Verdef *src;
67 Elf_Internal_Verdef *dst;
69 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
70 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
71 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
73 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
74 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
75 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
78 /* Swap out a Verdef structure. */
81 _bfd_elf_swap_verdef_out (abfd, src, dst)
83 const Elf_Internal_Verdef *src;
84 Elf_External_Verdef *dst;
86 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
87 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
88 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
91 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
92 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (abfd, src, dst)
100 const Elf_External_Verdaux *src;
101 Elf_Internal_Verdaux *dst;
103 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
104 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (abfd, src, dst)
112 const Elf_Internal_Verdaux *src;
113 Elf_External_Verdaux *dst;
115 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
116 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (abfd, src, dst)
124 const Elf_External_Verneed *src;
125 Elf_Internal_Verneed *dst;
127 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
128 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
129 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
130 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
131 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
134 /* Swap out a Verneed structure. */
137 _bfd_elf_swap_verneed_out (abfd, src, dst)
139 const Elf_Internal_Verneed *src;
140 Elf_External_Verneed *dst;
142 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
143 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
144 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
145 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
146 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
149 /* Swap in a Vernaux structure. */
152 _bfd_elf_swap_vernaux_in (abfd, src, dst)
154 const Elf_External_Vernaux *src;
155 Elf_Internal_Vernaux *dst;
157 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
158 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
159 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
160 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
161 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
164 /* Swap out a Vernaux structure. */
167 _bfd_elf_swap_vernaux_out (abfd, src, dst)
169 const Elf_Internal_Vernaux *src;
170 Elf_External_Vernaux *dst;
172 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
173 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
174 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
175 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
176 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
179 /* Swap in a Versym structure. */
182 _bfd_elf_swap_versym_in (abfd, src, dst)
184 const Elf_External_Versym *src;
185 Elf_Internal_Versym *dst;
187 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
190 /* Swap out a Versym structure. */
193 _bfd_elf_swap_versym_out (abfd, src, dst)
195 const Elf_Internal_Versym *src;
196 Elf_External_Versym *dst;
198 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
201 /* Standard ELF hash function. Do not change this function; you will
202 cause invalid hash tables to be generated. */
205 CONST unsigned char *name;
211 while ((ch = *name++) != '\0')
214 if ((g = (h & 0xf0000000)) != 0)
217 /* The ELF ABI says `h &= ~g', but this is equivalent in
218 this case and on some machines one insn instead of two. */
225 /* Read a specified number of bytes at a specified offset in an ELF
226 file, into a newly allocated buffer, and return a pointer to the
230 elf_read (abfd, offset, size)
237 if ((buf = bfd_alloc (abfd, size)) == NULL)
239 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
241 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
243 if (bfd_get_error () != bfd_error_system_call)
244 bfd_set_error (bfd_error_file_truncated);
251 bfd_elf_mkobject (abfd)
254 /* this just does initialization */
255 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
256 elf_tdata (abfd) = (struct elf_obj_tdata *)
257 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
258 if (elf_tdata (abfd) == 0)
260 /* since everything is done at close time, do we need any
267 bfd_elf_mkcorefile (abfd)
270 /* I think this can be done just like an object file. */
271 return bfd_elf_mkobject (abfd);
275 bfd_elf_get_str_section (abfd, shindex)
277 unsigned int shindex;
279 Elf_Internal_Shdr **i_shdrp;
280 char *shstrtab = NULL;
282 unsigned int shstrtabsize;
284 i_shdrp = elf_elfsections (abfd);
285 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
288 shstrtab = (char *) i_shdrp[shindex]->contents;
289 if (shstrtab == NULL)
291 /* No cached one, attempt to read, and cache what we read. */
292 offset = i_shdrp[shindex]->sh_offset;
293 shstrtabsize = i_shdrp[shindex]->sh_size;
294 shstrtab = elf_read (abfd, offset, shstrtabsize);
295 i_shdrp[shindex]->contents = (PTR) shstrtab;
301 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
303 unsigned int shindex;
304 unsigned int strindex;
306 Elf_Internal_Shdr *hdr;
311 hdr = elf_elfsections (abfd)[shindex];
313 if (hdr->contents == NULL
314 && bfd_elf_get_str_section (abfd, shindex) == NULL)
317 if (strindex >= hdr->sh_size)
319 (*_bfd_error_handler)
320 (_("%s: invalid string offset %u >= %lu for section `%s'"),
321 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
322 ((shindex == elf_elfheader(abfd)->e_shstrndx
323 && strindex == hdr->sh_name)
325 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
329 return ((char *) hdr->contents) + strindex;
332 /* Make a BFD section from an ELF section. We store a pointer to the
333 BFD section in the bfd_section field of the header. */
336 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
338 Elf_Internal_Shdr *hdr;
344 if (hdr->bfd_section != NULL)
346 BFD_ASSERT (strcmp (name,
347 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
351 newsect = bfd_make_section_anyway (abfd, name);
355 newsect->filepos = hdr->sh_offset;
357 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
358 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
359 || ! bfd_set_section_alignment (abfd, newsect,
360 bfd_log2 (hdr->sh_addralign)))
363 flags = SEC_NO_FLAGS;
364 if (hdr->sh_type != SHT_NOBITS)
365 flags |= SEC_HAS_CONTENTS;
366 if ((hdr->sh_flags & SHF_ALLOC) != 0)
369 if (hdr->sh_type != SHT_NOBITS)
372 if ((hdr->sh_flags & SHF_WRITE) == 0)
373 flags |= SEC_READONLY;
374 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
376 else if ((flags & SEC_LOAD) != 0)
379 /* The debugging sections appear to be recognized only by name, not
381 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
382 || strncmp (name, ".line", sizeof ".line" - 1) == 0
383 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
384 flags |= SEC_DEBUGGING;
386 /* As a GNU extension, if the name begins with .gnu.linkonce, we
387 only link a single copy of the section. This is used to support
388 g++. g++ will emit each template expansion in its own section.
389 The symbols will be defined as weak, so that multiple definitions
390 are permitted. The GNU linker extension is to actually discard
391 all but one of the sections. */
392 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
393 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
395 if (! bfd_set_section_flags (abfd, newsect, flags))
398 if ((flags & SEC_ALLOC) != 0)
400 Elf_Internal_Phdr *phdr;
403 /* Look through the phdrs to see if we need to adjust the lma.
404 If all the p_paddr fields are zero, we ignore them, since
405 some ELF linkers produce such output. */
406 phdr = elf_tdata (abfd)->phdr;
407 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
409 if (phdr->p_paddr != 0)
412 if (i < elf_elfheader (abfd)->e_phnum)
414 phdr = elf_tdata (abfd)->phdr;
415 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
417 if (phdr->p_type == PT_LOAD
418 && phdr->p_vaddr != phdr->p_paddr
419 && phdr->p_vaddr <= hdr->sh_addr
420 && (phdr->p_vaddr + phdr->p_memsz
421 >= hdr->sh_addr + hdr->sh_size)
422 && ((flags & SEC_LOAD) == 0
423 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
424 && (phdr->p_offset + phdr->p_filesz
425 >= hdr->sh_offset + hdr->sh_size))))
427 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
434 hdr->bfd_section = newsect;
435 elf_section_data (newsect)->this_hdr = *hdr;
445 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
448 Helper functions for GDB to locate the string tables.
449 Since BFD hides string tables from callers, GDB needs to use an
450 internal hook to find them. Sun's .stabstr, in particular,
451 isn't even pointed to by the .stab section, so ordinary
452 mechanisms wouldn't work to find it, even if we had some.
455 struct elf_internal_shdr *
456 bfd_elf_find_section (abfd, name)
460 Elf_Internal_Shdr **i_shdrp;
465 i_shdrp = elf_elfsections (abfd);
468 shstrtab = bfd_elf_get_str_section
469 (abfd, elf_elfheader (abfd)->e_shstrndx);
470 if (shstrtab != NULL)
472 max = elf_elfheader (abfd)->e_shnum;
473 for (i = 1; i < max; i++)
474 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
481 const char *const bfd_elf_section_type_names[] = {
482 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
483 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
484 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
487 /* ELF relocs are against symbols. If we are producing relocateable
488 output, and the reloc is against an external symbol, and nothing
489 has given us any additional addend, the resulting reloc will also
490 be against the same symbol. In such a case, we don't want to
491 change anything about the way the reloc is handled, since it will
492 all be done at final link time. Rather than put special case code
493 into bfd_perform_relocation, all the reloc types use this howto
494 function. It just short circuits the reloc if producing
495 relocateable output against an external symbol. */
498 bfd_reloc_status_type
499 bfd_elf_generic_reloc (abfd,
507 arelent *reloc_entry;
510 asection *input_section;
512 char **error_message;
514 if (output_bfd != (bfd *) NULL
515 && (symbol->flags & BSF_SECTION_SYM) == 0
516 && (! reloc_entry->howto->partial_inplace
517 || reloc_entry->addend == 0))
519 reloc_entry->address += input_section->output_offset;
523 return bfd_reloc_continue;
526 /* Print out the program headers. */
529 _bfd_elf_print_private_bfd_data (abfd, farg)
533 FILE *f = (FILE *) farg;
534 Elf_Internal_Phdr *p;
536 bfd_byte *dynbuf = NULL;
538 p = elf_tdata (abfd)->phdr;
543 fprintf (f, _("\nProgram Header:\n"));
544 c = elf_elfheader (abfd)->e_phnum;
545 for (i = 0; i < c; i++, p++)
552 case PT_NULL: s = "NULL"; break;
553 case PT_LOAD: s = "LOAD"; break;
554 case PT_DYNAMIC: s = "DYNAMIC"; break;
555 case PT_INTERP: s = "INTERP"; break;
556 case PT_NOTE: s = "NOTE"; break;
557 case PT_SHLIB: s = "SHLIB"; break;
558 case PT_PHDR: s = "PHDR"; break;
559 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
561 fprintf (f, "%8s off 0x", s);
562 fprintf_vma (f, p->p_offset);
563 fprintf (f, " vaddr 0x");
564 fprintf_vma (f, p->p_vaddr);
565 fprintf (f, " paddr 0x");
566 fprintf_vma (f, p->p_paddr);
567 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
568 fprintf (f, " filesz 0x");
569 fprintf_vma (f, p->p_filesz);
570 fprintf (f, " memsz 0x");
571 fprintf_vma (f, p->p_memsz);
572 fprintf (f, " flags %c%c%c",
573 (p->p_flags & PF_R) != 0 ? 'r' : '-',
574 (p->p_flags & PF_W) != 0 ? 'w' : '-',
575 (p->p_flags & PF_X) != 0 ? 'x' : '-');
576 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
577 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
582 s = bfd_get_section_by_name (abfd, ".dynamic");
587 bfd_byte *extdyn, *extdynend;
589 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
591 fprintf (f, _("\nDynamic Section:\n"));
593 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
596 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
600 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
603 link = elf_elfsections (abfd)[elfsec]->sh_link;
605 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
606 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
609 extdynend = extdyn + s->_raw_size;
610 for (; extdyn < extdynend; extdyn += extdynsize)
612 Elf_Internal_Dyn dyn;
617 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
619 if (dyn.d_tag == DT_NULL)
626 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
630 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
631 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
632 case DT_PLTGOT: name = "PLTGOT"; break;
633 case DT_HASH: name = "HASH"; break;
634 case DT_STRTAB: name = "STRTAB"; break;
635 case DT_SYMTAB: name = "SYMTAB"; break;
636 case DT_RELA: name = "RELA"; break;
637 case DT_RELASZ: name = "RELASZ"; break;
638 case DT_RELAENT: name = "RELAENT"; break;
639 case DT_STRSZ: name = "STRSZ"; break;
640 case DT_SYMENT: name = "SYMENT"; break;
641 case DT_INIT: name = "INIT"; break;
642 case DT_FINI: name = "FINI"; break;
643 case DT_SONAME: name = "SONAME"; stringp = true; break;
644 case DT_RPATH: name = "RPATH"; stringp = true; break;
645 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
646 case DT_REL: name = "REL"; break;
647 case DT_RELSZ: name = "RELSZ"; break;
648 case DT_RELENT: name = "RELENT"; break;
649 case DT_PLTREL: name = "PLTREL"; break;
650 case DT_DEBUG: name = "DEBUG"; break;
651 case DT_TEXTREL: name = "TEXTREL"; break;
652 case DT_JMPREL: name = "JMPREL"; break;
653 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
654 case DT_FILTER: name = "FILTER"; stringp = true; break;
655 case DT_VERSYM: name = "VERSYM"; break;
656 case DT_VERDEF: name = "VERDEF"; break;
657 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
658 case DT_VERNEED: name = "VERNEED"; break;
659 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
662 fprintf (f, " %-11s ", name);
664 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
669 string = bfd_elf_string_from_elf_section (abfd, link,
673 fprintf (f, "%s", string);
682 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
683 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
685 if (! _bfd_elf_slurp_version_tables (abfd))
689 if (elf_dynverdef (abfd) != 0)
691 Elf_Internal_Verdef *t;
693 fprintf (f, _("\nVersion definitions:\n"));
694 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
696 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
697 t->vd_flags, t->vd_hash, t->vd_nodename);
698 if (t->vd_auxptr->vda_nextptr != NULL)
700 Elf_Internal_Verdaux *a;
703 for (a = t->vd_auxptr->vda_nextptr;
706 fprintf (f, "%s ", a->vda_nodename);
712 if (elf_dynverref (abfd) != 0)
714 Elf_Internal_Verneed *t;
716 fprintf (f, _("\nVersion References:\n"));
717 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
719 Elf_Internal_Vernaux *a;
721 fprintf (f, _(" required from %s:\n"), t->vn_filename);
722 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
723 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
724 a->vna_flags, a->vna_other, a->vna_nodename);
736 /* Display ELF-specific fields of a symbol. */
739 bfd_elf_print_symbol (abfd, filep, symbol, how)
743 bfd_print_symbol_type how;
745 FILE *file = (FILE *) filep;
748 case bfd_print_symbol_name:
749 fprintf (file, "%s", symbol->name);
751 case bfd_print_symbol_more:
752 fprintf (file, "elf ");
753 fprintf_vma (file, symbol->value);
754 fprintf (file, " %lx", (long) symbol->flags);
756 case bfd_print_symbol_all:
758 CONST char *section_name;
759 section_name = symbol->section ? symbol->section->name : "(*none*)";
760 bfd_print_symbol_vandf ((PTR) file, symbol);
761 fprintf (file, " %s\t", section_name);
762 /* Print the "other" value for a symbol. For common symbols,
763 we've already printed the size; now print the alignment.
764 For other symbols, we have no specified alignment, and
765 we've printed the address; now print the size. */
767 (bfd_is_com_section (symbol->section)
768 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
769 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
771 /* If we have version information, print it. */
772 if (elf_tdata (abfd)->dynversym_section != 0
773 && (elf_tdata (abfd)->dynverdef_section != 0
774 || elf_tdata (abfd)->dynverref_section != 0))
777 const char *version_string;
779 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
783 else if (vernum == 1)
784 version_string = "Base";
785 else if (vernum <= elf_tdata (abfd)->cverdefs)
787 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
790 Elf_Internal_Verneed *t;
793 for (t = elf_tdata (abfd)->verref;
797 Elf_Internal_Vernaux *a;
799 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
801 if (a->vna_other == vernum)
803 version_string = a->vna_nodename;
810 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
811 fprintf (file, " %-11s", version_string);
816 fprintf (file, " (%s)", version_string);
817 for (i = 10 - strlen (version_string); i > 0; --i)
822 /* If the st_other field is not zero, print it. */
823 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
824 fprintf (file, " 0x%02x",
826 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
828 fprintf (file, " %s", symbol->name);
834 /* Create an entry in an ELF linker hash table. */
836 struct bfd_hash_entry *
837 _bfd_elf_link_hash_newfunc (entry, table, string)
838 struct bfd_hash_entry *entry;
839 struct bfd_hash_table *table;
842 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
844 /* Allocate the structure if it has not already been allocated by a
846 if (ret == (struct elf_link_hash_entry *) NULL)
847 ret = ((struct elf_link_hash_entry *)
848 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
849 if (ret == (struct elf_link_hash_entry *) NULL)
850 return (struct bfd_hash_entry *) ret;
852 /* Call the allocation method of the superclass. */
853 ret = ((struct elf_link_hash_entry *)
854 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
856 if (ret != (struct elf_link_hash_entry *) NULL)
858 /* Set local fields. */
862 ret->dynstr_index = 0;
864 ret->got.offset = (bfd_vma) -1;
865 ret->plt.offset = (bfd_vma) -1;
866 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
867 ret->verinfo.verdef = NULL;
868 ret->vtable_entries_used = NULL;
869 ret->vtable_entries_size = 0;
870 ret->vtable_parent = NULL;
871 ret->type = STT_NOTYPE;
873 /* Assume that we have been called by a non-ELF symbol reader.
874 This flag is then reset by the code which reads an ELF input
875 file. This ensures that a symbol created by a non-ELF symbol
876 reader will have the flag set correctly. */
877 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
880 return (struct bfd_hash_entry *) ret;
883 /* Initialize an ELF linker hash table. */
886 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
887 struct elf_link_hash_table *table;
889 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
890 struct bfd_hash_table *,
893 table->dynamic_sections_created = false;
894 table->dynobj = NULL;
895 /* The first dynamic symbol is a dummy. */
896 table->dynsymcount = 1;
897 table->dynstr = NULL;
898 table->bucketcount = 0;
899 table->needed = NULL;
901 table->stab_info = NULL;
902 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
905 /* Create an ELF linker hash table. */
907 struct bfd_link_hash_table *
908 _bfd_elf_link_hash_table_create (abfd)
911 struct elf_link_hash_table *ret;
913 ret = ((struct elf_link_hash_table *)
914 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
915 if (ret == (struct elf_link_hash_table *) NULL)
918 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
920 bfd_release (abfd, ret);
927 /* This is a hook for the ELF emulation code in the generic linker to
928 tell the backend linker what file name to use for the DT_NEEDED
929 entry for a dynamic object. The generic linker passes name as an
930 empty string to indicate that no DT_NEEDED entry should be made. */
933 bfd_elf_set_dt_needed_name (abfd, name)
937 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
938 && bfd_get_format (abfd) == bfd_object)
939 elf_dt_name (abfd) = name;
942 /* Get the list of DT_NEEDED entries for a link. This is a hook for
943 the linker ELF emulation code. */
945 struct bfd_link_needed_list *
946 bfd_elf_get_needed_list (abfd, info)
948 struct bfd_link_info *info;
950 if (info->hash->creator->flavour != bfd_target_elf_flavour)
952 return elf_hash_table (info)->needed;
955 /* Get the name actually used for a dynamic object for a link. This
956 is the SONAME entry if there is one. Otherwise, it is the string
957 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
960 bfd_elf_get_dt_soname (abfd)
963 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
964 && bfd_get_format (abfd) == bfd_object)
965 return elf_dt_name (abfd);
969 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
970 the ELF linker emulation code. */
973 bfd_elf_get_bfd_needed_list (abfd, pneeded)
975 struct bfd_link_needed_list **pneeded;
978 bfd_byte *dynbuf = NULL;
981 bfd_byte *extdyn, *extdynend;
983 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
987 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
988 || bfd_get_format (abfd) != bfd_object)
991 s = bfd_get_section_by_name (abfd, ".dynamic");
992 if (s == NULL || s->_raw_size == 0)
995 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
999 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1003 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1007 link = elf_elfsections (abfd)[elfsec]->sh_link;
1009 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1010 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1013 extdynend = extdyn + s->_raw_size;
1014 for (; extdyn < extdynend; extdyn += extdynsize)
1016 Elf_Internal_Dyn dyn;
1018 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1020 if (dyn.d_tag == DT_NULL)
1023 if (dyn.d_tag == DT_NEEDED)
1026 struct bfd_link_needed_list *l;
1028 string = bfd_elf_string_from_elf_section (abfd, link,
1033 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1054 /* Allocate an ELF string table--force the first byte to be zero. */
1056 struct bfd_strtab_hash *
1057 _bfd_elf_stringtab_init ()
1059 struct bfd_strtab_hash *ret;
1061 ret = _bfd_stringtab_init ();
1066 loc = _bfd_stringtab_add (ret, "", true, false);
1067 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1068 if (loc == (bfd_size_type) -1)
1070 _bfd_stringtab_free (ret);
1077 /* ELF .o/exec file reading */
1079 /* Create a new bfd section from an ELF section header. */
1082 bfd_section_from_shdr (abfd, shindex)
1084 unsigned int shindex;
1086 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1087 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1088 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1091 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1093 switch (hdr->sh_type)
1096 /* Inactive section. Throw it away. */
1099 case SHT_PROGBITS: /* Normal section with contents. */
1100 case SHT_DYNAMIC: /* Dynamic linking information. */
1101 case SHT_NOBITS: /* .bss section. */
1102 case SHT_HASH: /* .hash section. */
1103 case SHT_NOTE: /* .note section. */
1104 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1106 case SHT_SYMTAB: /* A symbol table */
1107 if (elf_onesymtab (abfd) == shindex)
1110 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1111 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1112 elf_onesymtab (abfd) = shindex;
1113 elf_tdata (abfd)->symtab_hdr = *hdr;
1114 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1115 abfd->flags |= HAS_SYMS;
1117 /* Sometimes a shared object will map in the symbol table. If
1118 SHF_ALLOC is set, and this is a shared object, then we also
1119 treat this section as a BFD section. We can not base the
1120 decision purely on SHF_ALLOC, because that flag is sometimes
1121 set in a relocateable object file, which would confuse the
1123 if ((hdr->sh_flags & SHF_ALLOC) != 0
1124 && (abfd->flags & DYNAMIC) != 0
1125 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1130 case SHT_DYNSYM: /* A dynamic symbol table */
1131 if (elf_dynsymtab (abfd) == shindex)
1134 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1135 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1136 elf_dynsymtab (abfd) = shindex;
1137 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1138 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1139 abfd->flags |= HAS_SYMS;
1141 /* Besides being a symbol table, we also treat this as a regular
1142 section, so that objcopy can handle it. */
1143 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1145 case SHT_STRTAB: /* A string table */
1146 if (hdr->bfd_section != NULL)
1148 if (ehdr->e_shstrndx == shindex)
1150 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1151 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1157 for (i = 1; i < ehdr->e_shnum; i++)
1159 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1160 if (hdr2->sh_link == shindex)
1162 if (! bfd_section_from_shdr (abfd, i))
1164 if (elf_onesymtab (abfd) == i)
1166 elf_tdata (abfd)->strtab_hdr = *hdr;
1167 elf_elfsections (abfd)[shindex] =
1168 &elf_tdata (abfd)->strtab_hdr;
1171 if (elf_dynsymtab (abfd) == i)
1173 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1174 elf_elfsections (abfd)[shindex] = hdr =
1175 &elf_tdata (abfd)->dynstrtab_hdr;
1176 /* We also treat this as a regular section, so
1177 that objcopy can handle it. */
1180 #if 0 /* Not handling other string tables specially right now. */
1181 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1182 /* We have a strtab for some random other section. */
1183 newsect = (asection *) hdr2->bfd_section;
1186 hdr->bfd_section = newsect;
1187 hdr2 = &elf_section_data (newsect)->str_hdr;
1189 elf_elfsections (abfd)[shindex] = hdr2;
1195 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1199 /* *These* do a lot of work -- but build no sections! */
1201 asection *target_sect;
1202 Elf_Internal_Shdr *hdr2;
1204 /* For some incomprehensible reason Oracle distributes
1205 libraries for Solaris in which some of the objects have
1206 bogus sh_link fields. It would be nice if we could just
1207 reject them, but, unfortunately, some people need to use
1208 them. We scan through the section headers; if we find only
1209 one suitable symbol table, we clobber the sh_link to point
1210 to it. I hope this doesn't break anything. */
1211 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1212 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1218 for (scan = 1; scan < ehdr->e_shnum; scan++)
1220 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1221 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1232 hdr->sh_link = found;
1235 /* Get the symbol table. */
1236 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1237 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1240 /* If this reloc section does not use the main symbol table we
1241 don't treat it as a reloc section. BFD can't adequately
1242 represent such a section, so at least for now, we don't
1243 try. We just present it as a normal section. */
1244 if (hdr->sh_link != elf_onesymtab (abfd))
1245 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1247 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1249 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1250 if (target_sect == NULL)
1253 if ((target_sect->flags & SEC_RELOC) == 0
1254 || target_sect->reloc_count == 0)
1255 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1258 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1259 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1260 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1263 elf_elfsections (abfd)[shindex] = hdr2;
1264 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1265 target_sect->flags |= SEC_RELOC;
1266 target_sect->relocation = NULL;
1267 target_sect->rel_filepos = hdr->sh_offset;
1268 abfd->flags |= HAS_RELOC;
1273 case SHT_GNU_verdef:
1274 elf_dynverdef (abfd) = shindex;
1275 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1276 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1279 case SHT_GNU_versym:
1280 elf_dynversym (abfd) = shindex;
1281 elf_tdata (abfd)->dynversym_hdr = *hdr;
1282 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1285 case SHT_GNU_verneed:
1286 elf_dynverref (abfd) = shindex;
1287 elf_tdata (abfd)->dynverref_hdr = *hdr;
1288 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1295 /* Check for any processor-specific section types. */
1297 if (bed->elf_backend_section_from_shdr)
1298 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1306 /* Given an ELF section number, retrieve the corresponding BFD
1310 bfd_section_from_elf_index (abfd, index)
1314 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1315 if (index >= elf_elfheader (abfd)->e_shnum)
1317 return elf_elfsections (abfd)[index]->bfd_section;
1321 _bfd_elf_new_section_hook (abfd, sec)
1325 struct bfd_elf_section_data *sdata;
1327 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
1330 sec->used_by_bfd = (PTR) sdata;
1331 memset (sdata, 0, sizeof (*sdata));
1335 /* Create a new bfd section from an ELF program header.
1337 Since program segments have no names, we generate a synthetic name
1338 of the form segment<NUM>, where NUM is generally the index in the
1339 program header table. For segments that are split (see below) we
1340 generate the names segment<NUM>a and segment<NUM>b.
1342 Note that some program segments may have a file size that is different than
1343 (less than) the memory size. All this means is that at execution the
1344 system must allocate the amount of memory specified by the memory size,
1345 but only initialize it with the first "file size" bytes read from the
1346 file. This would occur for example, with program segments consisting
1347 of combined data+bss.
1349 To handle the above situation, this routine generates TWO bfd sections
1350 for the single program segment. The first has the length specified by
1351 the file size of the segment, and the second has the length specified
1352 by the difference between the two sizes. In effect, the segment is split
1353 into it's initialized and uninitialized parts.
1358 bfd_section_from_phdr (abfd, hdr, index)
1360 Elf_Internal_Phdr *hdr;
1368 split = ((hdr->p_memsz > 0)
1369 && (hdr->p_filesz > 0)
1370 && (hdr->p_memsz > hdr->p_filesz));
1371 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1372 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1375 strcpy (name, namebuf);
1376 newsect = bfd_make_section (abfd, name);
1377 if (newsect == NULL)
1379 newsect->vma = hdr->p_vaddr;
1380 newsect->lma = hdr->p_paddr;
1381 newsect->_raw_size = hdr->p_filesz;
1382 newsect->filepos = hdr->p_offset;
1383 newsect->flags |= SEC_HAS_CONTENTS;
1384 if (hdr->p_type == PT_LOAD)
1386 newsect->flags |= SEC_ALLOC;
1387 newsect->flags |= SEC_LOAD;
1388 if (hdr->p_flags & PF_X)
1390 /* FIXME: all we known is that it has execute PERMISSION,
1392 newsect->flags |= SEC_CODE;
1395 if (!(hdr->p_flags & PF_W))
1397 newsect->flags |= SEC_READONLY;
1402 sprintf (namebuf, "segment%db", index);
1403 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1406 strcpy (name, namebuf);
1407 newsect = bfd_make_section (abfd, name);
1408 if (newsect == NULL)
1410 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1411 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1412 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1413 if (hdr->p_type == PT_LOAD)
1415 newsect->flags |= SEC_ALLOC;
1416 if (hdr->p_flags & PF_X)
1417 newsect->flags |= SEC_CODE;
1419 if (!(hdr->p_flags & PF_W))
1420 newsect->flags |= SEC_READONLY;
1426 /* Set up an ELF internal section header for a section. */
1430 elf_fake_sections (abfd, asect, failedptrarg)
1435 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1436 boolean *failedptr = (boolean *) failedptrarg;
1437 Elf_Internal_Shdr *this_hdr;
1441 /* We already failed; just get out of the bfd_map_over_sections
1446 this_hdr = &elf_section_data (asect)->this_hdr;
1448 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1451 if (this_hdr->sh_name == (unsigned long) -1)
1457 this_hdr->sh_flags = 0;
1459 if ((asect->flags & SEC_ALLOC) != 0
1460 || asect->user_set_vma)
1461 this_hdr->sh_addr = asect->vma;
1463 this_hdr->sh_addr = 0;
1465 this_hdr->sh_offset = 0;
1466 this_hdr->sh_size = asect->_raw_size;
1467 this_hdr->sh_link = 0;
1468 this_hdr->sh_addralign = 1 << asect->alignment_power;
1469 /* The sh_entsize and sh_info fields may have been set already by
1470 copy_private_section_data. */
1472 this_hdr->bfd_section = asect;
1473 this_hdr->contents = NULL;
1475 /* FIXME: This should not be based on section names. */
1476 if (strcmp (asect->name, ".dynstr") == 0)
1477 this_hdr->sh_type = SHT_STRTAB;
1478 else if (strcmp (asect->name, ".hash") == 0)
1480 this_hdr->sh_type = SHT_HASH;
1481 this_hdr->sh_entsize = bed->s->arch_size / 8;
1483 else if (strcmp (asect->name, ".dynsym") == 0)
1485 this_hdr->sh_type = SHT_DYNSYM;
1486 this_hdr->sh_entsize = bed->s->sizeof_sym;
1488 else if (strcmp (asect->name, ".dynamic") == 0)
1490 this_hdr->sh_type = SHT_DYNAMIC;
1491 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1493 else if (strncmp (asect->name, ".rela.", 6) == 0
1494 && get_elf_backend_data (abfd)->use_rela_p)
1496 this_hdr->sh_type = SHT_RELA;
1497 this_hdr->sh_entsize = bed->s->sizeof_rela;
1499 else if (strncmp (asect->name, ".rel.", 5) == 0
1500 && ! get_elf_backend_data (abfd)->use_rela_p)
1502 this_hdr->sh_type = SHT_REL;
1503 this_hdr->sh_entsize = bed->s->sizeof_rel;
1505 else if (strncmp (asect->name, ".note", 5) == 0)
1506 this_hdr->sh_type = SHT_NOTE;
1507 else if (strncmp (asect->name, ".stab", 5) == 0
1508 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1509 this_hdr->sh_type = SHT_STRTAB;
1510 else if (strcmp (asect->name, ".gnu.version") == 0)
1512 this_hdr->sh_type = SHT_GNU_versym;
1513 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1515 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1517 this_hdr->sh_type = SHT_GNU_verdef;
1518 this_hdr->sh_entsize = 0;
1519 /* objcopy or strip will copy over sh_info, but may not set
1520 cverdefs. The linker will set cverdefs, but sh_info will be
1522 if (this_hdr->sh_info == 0)
1523 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1525 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1526 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1528 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1530 this_hdr->sh_type = SHT_GNU_verneed;
1531 this_hdr->sh_entsize = 0;
1532 /* objcopy or strip will copy over sh_info, but may not set
1533 cverrefs. The linker will set cverrefs, but sh_info will be
1535 if (this_hdr->sh_info == 0)
1536 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1538 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1539 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1541 else if ((asect->flags & SEC_ALLOC) != 0
1542 && (asect->flags & SEC_LOAD) != 0)
1543 this_hdr->sh_type = SHT_PROGBITS;
1544 else if ((asect->flags & SEC_ALLOC) != 0
1545 && ((asect->flags & SEC_LOAD) == 0))
1546 this_hdr->sh_type = SHT_NOBITS;
1550 this_hdr->sh_type = SHT_PROGBITS;
1553 if ((asect->flags & SEC_ALLOC) != 0)
1554 this_hdr->sh_flags |= SHF_ALLOC;
1555 if ((asect->flags & SEC_READONLY) == 0)
1556 this_hdr->sh_flags |= SHF_WRITE;
1557 if ((asect->flags & SEC_CODE) != 0)
1558 this_hdr->sh_flags |= SHF_EXECINSTR;
1560 /* Check for processor-specific section types. */
1562 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1564 if (bed->elf_backend_fake_sections)
1565 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1568 /* If the section has relocs, set up a section header for the
1569 SHT_REL[A] section. */
1570 if ((asect->flags & SEC_RELOC) != 0)
1572 Elf_Internal_Shdr *rela_hdr;
1573 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1576 rela_hdr = &elf_section_data (asect)->rel_hdr;
1577 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1583 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1585 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1587 if (rela_hdr->sh_name == (unsigned int) -1)
1592 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1593 rela_hdr->sh_entsize = (use_rela_p
1594 ? bed->s->sizeof_rela
1595 : bed->s->sizeof_rel);
1596 rela_hdr->sh_addralign = bed->s->file_align;
1597 rela_hdr->sh_flags = 0;
1598 rela_hdr->sh_addr = 0;
1599 rela_hdr->sh_size = 0;
1600 rela_hdr->sh_offset = 0;
1604 /* Assign all ELF section numbers. The dummy first section is handled here
1605 too. The link/info pointers for the standard section types are filled
1606 in here too, while we're at it. */
1609 assign_section_numbers (abfd)
1612 struct elf_obj_tdata *t = elf_tdata (abfd);
1614 unsigned int section_number;
1615 Elf_Internal_Shdr **i_shdrp;
1616 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1620 for (sec = abfd->sections; sec; sec = sec->next)
1622 struct bfd_elf_section_data *d = elf_section_data (sec);
1624 d->this_idx = section_number++;
1625 if ((sec->flags & SEC_RELOC) == 0)
1628 d->rel_idx = section_number++;
1631 t->shstrtab_section = section_number++;
1632 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1633 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1635 if (bfd_get_symcount (abfd) > 0)
1637 t->symtab_section = section_number++;
1638 t->strtab_section = section_number++;
1641 elf_elfheader (abfd)->e_shnum = section_number;
1643 /* Set up the list of section header pointers, in agreement with the
1645 i_shdrp = ((Elf_Internal_Shdr **)
1646 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1647 if (i_shdrp == NULL)
1650 i_shdrp[0] = ((Elf_Internal_Shdr *)
1651 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1652 if (i_shdrp[0] == NULL)
1654 bfd_release (abfd, i_shdrp);
1657 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1659 elf_elfsections (abfd) = i_shdrp;
1661 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1662 if (bfd_get_symcount (abfd) > 0)
1664 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1665 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1666 t->symtab_hdr.sh_link = t->strtab_section;
1668 for (sec = abfd->sections; sec; sec = sec->next)
1670 struct bfd_elf_section_data *d = elf_section_data (sec);
1674 i_shdrp[d->this_idx] = &d->this_hdr;
1675 if (d->rel_idx != 0)
1676 i_shdrp[d->rel_idx] = &d->rel_hdr;
1678 /* Fill in the sh_link and sh_info fields while we're at it. */
1680 /* sh_link of a reloc section is the section index of the symbol
1681 table. sh_info is the section index of the section to which
1682 the relocation entries apply. */
1683 if (d->rel_idx != 0)
1685 d->rel_hdr.sh_link = t->symtab_section;
1686 d->rel_hdr.sh_info = d->this_idx;
1689 switch (d->this_hdr.sh_type)
1693 /* A reloc section which we are treating as a normal BFD
1694 section. sh_link is the section index of the symbol
1695 table. sh_info is the section index of the section to
1696 which the relocation entries apply. We assume that an
1697 allocated reloc section uses the dynamic symbol table.
1698 FIXME: How can we be sure? */
1699 s = bfd_get_section_by_name (abfd, ".dynsym");
1701 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1703 /* We look up the section the relocs apply to by name. */
1705 if (d->this_hdr.sh_type == SHT_REL)
1709 s = bfd_get_section_by_name (abfd, name);
1711 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1715 /* We assume that a section named .stab*str is a stabs
1716 string section. We look for a section with the same name
1717 but without the trailing ``str'', and set its sh_link
1718 field to point to this section. */
1719 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1720 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1725 len = strlen (sec->name);
1726 alc = (char *) bfd_malloc (len - 2);
1729 strncpy (alc, sec->name, len - 3);
1730 alc[len - 3] = '\0';
1731 s = bfd_get_section_by_name (abfd, alc);
1735 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1737 /* This is a .stab section. */
1738 elf_section_data (s)->this_hdr.sh_entsize =
1739 4 + 2 * (bed->s->arch_size / 8);
1746 case SHT_GNU_verneed:
1747 case SHT_GNU_verdef:
1748 /* sh_link is the section header index of the string table
1749 used for the dynamic entries, or the symbol table, or the
1751 s = bfd_get_section_by_name (abfd, ".dynstr");
1753 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1757 case SHT_GNU_versym:
1758 /* sh_link is the section header index of the symbol table
1759 this hash table or version table is for. */
1760 s = bfd_get_section_by_name (abfd, ".dynsym");
1762 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1770 /* Map symbol from it's internal number to the external number, moving
1771 all local symbols to be at the head of the list. */
1774 sym_is_global (abfd, sym)
1778 /* If the backend has a special mapping, use it. */
1779 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1780 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1783 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1784 || bfd_is_und_section (bfd_get_section (sym))
1785 || bfd_is_com_section (bfd_get_section (sym)));
1789 elf_map_symbols (abfd)
1792 int symcount = bfd_get_symcount (abfd);
1793 asymbol **syms = bfd_get_outsymbols (abfd);
1794 asymbol **sect_syms;
1796 int num_globals = 0;
1797 int num_locals2 = 0;
1798 int num_globals2 = 0;
1800 int num_sections = 0;
1807 fprintf (stderr, "elf_map_symbols\n");
1811 /* Add a section symbol for each BFD section. FIXME: Is this really
1813 for (asect = abfd->sections; asect; asect = asect->next)
1815 if (max_index < asect->index)
1816 max_index = asect->index;
1820 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1821 if (sect_syms == NULL)
1823 elf_section_syms (abfd) = sect_syms;
1825 for (idx = 0; idx < symcount; idx++)
1829 if ((sym->flags & BSF_SECTION_SYM) != 0
1836 if (sec->owner != NULL)
1838 if (sec->owner != abfd)
1840 if (sec->output_offset != 0)
1843 sec = sec->output_section;
1845 /* Empty sections in the input files may have had a section
1846 symbol created for them. (See the comment near the end of
1847 _bfd_generic_link_output_symbols in linker.c). If the linker
1848 script discards such sections then we will reach this point.
1849 Since we know that we cannot avoid this case, we detect it
1850 and skip the abort and the assignment to the sect_syms array.
1851 To reproduce this particular case try running the linker
1852 testsuite test ld-scripts/weak.exp for an ELF port that uses
1853 the generic linker. */
1854 if (sec->owner == NULL)
1857 BFD_ASSERT (sec->owner == abfd);
1859 sect_syms[sec->index] = syms[idx];
1864 for (asect = abfd->sections; asect; asect = asect->next)
1866 if (sect_syms[asect->index] != NULL)
1869 sym = bfd_make_empty_symbol (abfd);
1872 sym->the_bfd = abfd;
1873 sym->name = asect->name;
1875 /* Set the flags to 0 to indicate that this one was newly added. */
1877 sym->section = asect;
1878 sect_syms[asect->index] = sym;
1882 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1883 asect->name, (long) asect->vma, asect->index, (long) asect);
1887 /* Classify all of the symbols. */
1888 for (idx = 0; idx < symcount; idx++)
1890 if (!sym_is_global (abfd, syms[idx]))
1895 for (asect = abfd->sections; asect; asect = asect->next)
1897 if (sect_syms[asect->index] != NULL
1898 && sect_syms[asect->index]->flags == 0)
1900 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1901 if (!sym_is_global (abfd, sect_syms[asect->index]))
1905 sect_syms[asect->index]->flags = 0;
1909 /* Now sort the symbols so the local symbols are first. */
1910 new_syms = ((asymbol **)
1912 (num_locals + num_globals) * sizeof (asymbol *)));
1913 if (new_syms == NULL)
1916 for (idx = 0; idx < symcount; idx++)
1918 asymbol *sym = syms[idx];
1921 if (!sym_is_global (abfd, sym))
1924 i = num_locals + num_globals2++;
1926 sym->udata.i = i + 1;
1928 for (asect = abfd->sections; asect; asect = asect->next)
1930 if (sect_syms[asect->index] != NULL
1931 && sect_syms[asect->index]->flags == 0)
1933 asymbol *sym = sect_syms[asect->index];
1936 sym->flags = BSF_SECTION_SYM;
1937 if (!sym_is_global (abfd, sym))
1940 i = num_locals + num_globals2++;
1942 sym->udata.i = i + 1;
1946 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1948 elf_num_locals (abfd) = num_locals;
1949 elf_num_globals (abfd) = num_globals;
1953 /* Align to the maximum file alignment that could be required for any
1954 ELF data structure. */
1956 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1957 static INLINE file_ptr
1958 align_file_position (off, align)
1962 return (off + align - 1) & ~(align - 1);
1965 /* Assign a file position to a section, optionally aligning to the
1966 required section alignment. */
1969 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1970 Elf_Internal_Shdr *i_shdrp;
1978 al = i_shdrp->sh_addralign;
1980 offset = BFD_ALIGN (offset, al);
1982 i_shdrp->sh_offset = offset;
1983 if (i_shdrp->bfd_section != NULL)
1984 i_shdrp->bfd_section->filepos = offset;
1985 if (i_shdrp->sh_type != SHT_NOBITS)
1986 offset += i_shdrp->sh_size;
1990 /* Compute the file positions we are going to put the sections at, and
1991 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1992 is not NULL, this is being called by the ELF backend linker. */
1995 _bfd_elf_compute_section_file_positions (abfd, link_info)
1997 struct bfd_link_info *link_info;
1999 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2001 struct bfd_strtab_hash *strtab;
2002 Elf_Internal_Shdr *shstrtab_hdr;
2004 if (abfd->output_has_begun)
2007 /* Do any elf backend specific processing first. */
2008 if (bed->elf_backend_begin_write_processing)
2009 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2011 if (! prep_headers (abfd))
2015 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2019 if (!assign_section_numbers (abfd))
2022 /* The backend linker builds symbol table information itself. */
2023 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2025 /* Non-zero if doing a relocatable link. */
2026 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2028 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2032 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2033 /* sh_name was set in prep_headers. */
2034 shstrtab_hdr->sh_type = SHT_STRTAB;
2035 shstrtab_hdr->sh_flags = 0;
2036 shstrtab_hdr->sh_addr = 0;
2037 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2038 shstrtab_hdr->sh_entsize = 0;
2039 shstrtab_hdr->sh_link = 0;
2040 shstrtab_hdr->sh_info = 0;
2041 /* sh_offset is set in assign_file_positions_except_relocs. */
2042 shstrtab_hdr->sh_addralign = 1;
2044 if (!assign_file_positions_except_relocs (abfd))
2047 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2050 Elf_Internal_Shdr *hdr;
2052 off = elf_tdata (abfd)->next_file_pos;
2054 hdr = &elf_tdata (abfd)->symtab_hdr;
2055 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2057 hdr = &elf_tdata (abfd)->strtab_hdr;
2058 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2060 elf_tdata (abfd)->next_file_pos = off;
2062 /* Now that we know where the .strtab section goes, write it
2064 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2065 || ! _bfd_stringtab_emit (abfd, strtab))
2067 _bfd_stringtab_free (strtab);
2070 abfd->output_has_begun = true;
2075 /* Create a mapping from a set of sections to a program segment. */
2077 static INLINE struct elf_segment_map *
2078 make_mapping (abfd, sections, from, to, phdr)
2080 asection **sections;
2085 struct elf_segment_map *m;
2089 m = ((struct elf_segment_map *)
2091 (sizeof (struct elf_segment_map)
2092 + (to - from - 1) * sizeof (asection *))));
2096 m->p_type = PT_LOAD;
2097 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2098 m->sections[i - from] = *hdrpp;
2099 m->count = to - from;
2101 if (from == 0 && phdr)
2103 /* Include the headers in the first PT_LOAD segment. */
2104 m->includes_filehdr = 1;
2105 m->includes_phdrs = 1;
2111 /* Set up a mapping from BFD sections to program segments. */
2114 map_sections_to_segments (abfd)
2117 asection **sections = NULL;
2121 struct elf_segment_map *mfirst;
2122 struct elf_segment_map **pm;
2123 struct elf_segment_map *m;
2125 unsigned int phdr_index;
2126 bfd_vma maxpagesize;
2128 boolean phdr_in_segment = true;
2132 if (elf_tdata (abfd)->segment_map != NULL)
2135 if (bfd_count_sections (abfd) == 0)
2138 /* Select the allocated sections, and sort them. */
2140 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2141 * sizeof (asection *));
2142 if (sections == NULL)
2146 for (s = abfd->sections; s != NULL; s = s->next)
2148 if ((s->flags & SEC_ALLOC) != 0)
2154 BFD_ASSERT (i <= bfd_count_sections (abfd));
2157 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2159 /* Build the mapping. */
2164 /* If we have a .interp section, then create a PT_PHDR segment for
2165 the program headers and a PT_INTERP segment for the .interp
2167 s = bfd_get_section_by_name (abfd, ".interp");
2168 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2170 m = ((struct elf_segment_map *)
2171 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2175 m->p_type = PT_PHDR;
2176 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2177 m->p_flags = PF_R | PF_X;
2178 m->p_flags_valid = 1;
2179 m->includes_phdrs = 1;
2184 m = ((struct elf_segment_map *)
2185 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2189 m->p_type = PT_INTERP;
2197 /* Look through the sections. We put sections in the same program
2198 segment when the start of the second section can be placed within
2199 a few bytes of the end of the first section. */
2202 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2204 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2206 && (dynsec->flags & SEC_LOAD) == 0)
2209 /* Deal with -Ttext or something similar such that the first section
2210 is not adjacent to the program headers. This is an
2211 approximation, since at this point we don't know exactly how many
2212 program headers we will need. */
2215 bfd_size_type phdr_size;
2217 phdr_size = elf_tdata (abfd)->program_header_size;
2219 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2220 if ((abfd->flags & D_PAGED) == 0
2221 || sections[0]->lma < phdr_size
2222 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2223 phdr_in_segment = false;
2226 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2229 boolean new_segment;
2233 /* See if this section and the last one will fit in the same
2236 if (last_hdr == NULL)
2238 /* If we don't have a segment yet, then we don't need a new
2239 one (we build the last one after this loop). */
2240 new_segment = false;
2242 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2244 /* If this section has a different relation between the
2245 virtual address and the load address, then we need a new
2249 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2250 < BFD_ALIGN (hdr->lma, maxpagesize))
2252 /* If putting this section in this segment would force us to
2253 skip a page in the segment, then we need a new segment. */
2256 else if ((last_hdr->flags & SEC_LOAD) == 0
2257 && (hdr->flags & SEC_LOAD) != 0)
2259 /* We don't want to put a loadable section after a
2260 nonloadable section in the same segment. */
2263 else if ((abfd->flags & D_PAGED) == 0)
2265 /* If the file is not demand paged, which means that we
2266 don't require the sections to be correctly aligned in the
2267 file, then there is no other reason for a new segment. */
2268 new_segment = false;
2271 && (hdr->flags & SEC_READONLY) == 0
2272 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2275 /* We don't want to put a writable section in a read only
2276 segment, unless they are on the same page in memory
2277 anyhow. We already know that the last section does not
2278 bring us past the current section on the page, so the
2279 only case in which the new section is not on the same
2280 page as the previous section is when the previous section
2281 ends precisely on a page boundary. */
2286 /* Otherwise, we can use the same segment. */
2287 new_segment = false;
2292 if ((hdr->flags & SEC_READONLY) == 0)
2298 /* We need a new program segment. We must create a new program
2299 header holding all the sections from phdr_index until hdr. */
2301 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2308 if ((hdr->flags & SEC_READONLY) == 0)
2315 phdr_in_segment = false;
2318 /* Create a final PT_LOAD program segment. */
2319 if (last_hdr != NULL)
2321 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2329 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2332 m = ((struct elf_segment_map *)
2333 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2337 m->p_type = PT_DYNAMIC;
2339 m->sections[0] = dynsec;
2345 /* For each loadable .note section, add a PT_NOTE segment. We don't
2346 use bfd_get_section_by_name, because if we link together
2347 nonloadable .note sections and loadable .note sections, we will
2348 generate two .note sections in the output file. FIXME: Using
2349 names for section types is bogus anyhow. */
2350 for (s = abfd->sections; s != NULL; s = s->next)
2352 if ((s->flags & SEC_LOAD) != 0
2353 && strncmp (s->name, ".note", 5) == 0)
2355 m = ((struct elf_segment_map *)
2356 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2360 m->p_type = PT_NOTE;
2372 elf_tdata (abfd)->segment_map = mfirst;
2376 if (sections != NULL)
2381 /* Sort sections by address. */
2384 elf_sort_sections (arg1, arg2)
2388 const asection *sec1 = *(const asection **) arg1;
2389 const asection *sec2 = *(const asection **) arg2;
2391 /* Sort by LMA first, since this is the address used to
2392 place the section into a segment. */
2393 if (sec1->lma < sec2->lma)
2395 else if (sec1->lma > sec2->lma)
2398 /* Then sort by VMA. Normally the LMA and the VMA will be
2399 the same, and this will do nothing. */
2400 if (sec1->vma < sec2->vma)
2402 else if (sec1->vma > sec2->vma)
2405 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2407 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2412 return sec1->target_index - sec2->target_index;
2422 /* Sort by size, to put zero sized sections before others at the
2425 if (sec1->_raw_size < sec2->_raw_size)
2427 if (sec1->_raw_size > sec2->_raw_size)
2430 return sec1->target_index - sec2->target_index;
2433 /* Assign file positions to the sections based on the mapping from
2434 sections to segments. This function also sets up some fields in
2435 the file header, and writes out the program headers. */
2438 assign_file_positions_for_segments (abfd)
2441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2443 struct elf_segment_map *m;
2445 Elf_Internal_Phdr *phdrs;
2447 bfd_vma filehdr_vaddr, filehdr_paddr;
2448 bfd_vma phdrs_vaddr, phdrs_paddr;
2449 Elf_Internal_Phdr *p;
2451 if (elf_tdata (abfd)->segment_map == NULL)
2453 if (! map_sections_to_segments (abfd))
2457 if (bed->elf_backend_modify_segment_map)
2459 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2464 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2467 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2468 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2469 elf_elfheader (abfd)->e_phnum = count;
2474 /* If we already counted the number of program segments, make sure
2475 that we allocated enough space. This happens when SIZEOF_HEADERS
2476 is used in a linker script. */
2477 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2478 if (alloc != 0 && count > alloc)
2480 ((*_bfd_error_handler)
2481 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2482 bfd_get_filename (abfd), alloc, count));
2483 bfd_set_error (bfd_error_bad_value);
2490 phdrs = ((Elf_Internal_Phdr *)
2491 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2495 off = bed->s->sizeof_ehdr;
2496 off += alloc * bed->s->sizeof_phdr;
2503 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2510 /* If elf_segment_map is not from map_sections_to_segments, the
2511 sections may not be correctly ordered. */
2513 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2516 p->p_type = m->p_type;
2518 if (m->p_flags_valid)
2519 p->p_flags = m->p_flags;
2523 if (p->p_type == PT_LOAD
2525 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2527 if ((abfd->flags & D_PAGED) != 0)
2528 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2531 bfd_size_type align;
2534 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2536 bfd_size_type secalign;
2538 secalign = bfd_get_section_alignment (abfd, *secpp);
2539 if (secalign > align)
2543 off += (m->sections[0]->vma - off) % (1 << align);
2550 p->p_vaddr = m->sections[0]->vma;
2552 if (m->p_paddr_valid)
2553 p->p_paddr = m->p_paddr;
2554 else if (m->count == 0)
2557 p->p_paddr = m->sections[0]->lma;
2559 if (p->p_type == PT_LOAD
2560 && (abfd->flags & D_PAGED) != 0)
2561 p->p_align = bed->maxpagesize;
2562 else if (m->count == 0)
2563 p->p_align = bed->s->file_align;
2571 if (m->includes_filehdr)
2573 if (! m->p_flags_valid)
2576 p->p_filesz = bed->s->sizeof_ehdr;
2577 p->p_memsz = bed->s->sizeof_ehdr;
2580 BFD_ASSERT (p->p_type == PT_LOAD);
2582 if (p->p_vaddr < (bfd_vma) off)
2584 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2585 bfd_get_filename (abfd));
2586 bfd_set_error (bfd_error_bad_value);
2591 if (! m->p_paddr_valid)
2594 if (p->p_type == PT_LOAD)
2596 filehdr_vaddr = p->p_vaddr;
2597 filehdr_paddr = p->p_paddr;
2601 if (m->includes_phdrs)
2603 if (! m->p_flags_valid)
2606 if (m->includes_filehdr)
2608 if (p->p_type == PT_LOAD)
2610 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2611 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2616 p->p_offset = bed->s->sizeof_ehdr;
2620 BFD_ASSERT (p->p_type == PT_LOAD);
2621 p->p_vaddr -= off - p->p_offset;
2622 if (! m->p_paddr_valid)
2623 p->p_paddr -= off - p->p_offset;
2626 if (p->p_type == PT_LOAD)
2628 phdrs_vaddr = p->p_vaddr;
2629 phdrs_paddr = p->p_paddr;
2632 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2635 p->p_filesz += alloc * bed->s->sizeof_phdr;
2636 p->p_memsz += alloc * bed->s->sizeof_phdr;
2639 if (p->p_type == PT_LOAD
2640 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2642 if (! m->includes_filehdr && ! m->includes_phdrs)
2648 adjust = off - (p->p_offset + p->p_filesz);
2649 p->p_filesz += adjust;
2650 p->p_memsz += adjust;
2656 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2660 bfd_size_type align;
2664 align = 1 << bfd_get_section_alignment (abfd, sec);
2666 /* The section may have artificial alignment forced by a
2667 link script. Notice this case by the gap between the
2668 cumulative phdr vma and the section's vma. */
2669 if (p->p_vaddr + p->p_memsz < sec->vma)
2671 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2673 p->p_memsz += adjust;
2676 if ((flags & SEC_LOAD) != 0)
2677 p->p_filesz += adjust;
2680 if (p->p_type == PT_LOAD)
2682 bfd_signed_vma adjust;
2684 if ((flags & SEC_LOAD) != 0)
2686 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2690 else if ((flags & SEC_ALLOC) != 0)
2692 /* The section VMA must equal the file position
2693 modulo the page size. FIXME: I'm not sure if
2694 this adjustment is really necessary. We used to
2695 not have the SEC_LOAD case just above, and then
2696 this was necessary, but now I'm not sure. */
2697 if ((abfd->flags & D_PAGED) != 0)
2698 adjust = (sec->vma - voff) % bed->maxpagesize;
2700 adjust = (sec->vma - voff) % align;
2709 (* _bfd_error_handler)
2710 (_("Error: First section in segment (%s) starts at 0x%x"),
2711 bfd_section_name (abfd, sec), sec->lma);
2712 (* _bfd_error_handler)
2713 (_(" whereas segment starts at 0x%x"),
2718 p->p_memsz += adjust;
2721 if ((flags & SEC_LOAD) != 0)
2722 p->p_filesz += adjust;
2727 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2728 used in a linker script we may have a section with
2729 SEC_LOAD clear but which is supposed to have
2731 if ((flags & SEC_LOAD) != 0
2732 || (flags & SEC_HAS_CONTENTS) != 0)
2733 off += sec->_raw_size;
2735 if ((flags & SEC_ALLOC) != 0)
2736 voff += sec->_raw_size;
2739 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2741 if (i == 0) /* the actual "note" segment */
2742 { /* this one actually contains everything. */
2744 p->p_filesz = sec->_raw_size;
2745 off += sec->_raw_size;
2748 else /* fake sections -- don't need to be written */
2752 flags = sec->flags = 0; /* no contents */
2759 p->p_memsz += sec->_raw_size;
2761 if ((flags & SEC_LOAD) != 0)
2762 p->p_filesz += sec->_raw_size;
2764 if (align > p->p_align
2765 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2769 if (! m->p_flags_valid)
2772 if ((flags & SEC_CODE) != 0)
2774 if ((flags & SEC_READONLY) == 0)
2780 /* Now that we have set the section file positions, we can set up
2781 the file positions for the non PT_LOAD segments. */
2782 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2786 if (p->p_type != PT_LOAD && m->count > 0)
2788 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2789 p->p_offset = m->sections[0]->filepos;
2793 if (m->includes_filehdr)
2795 p->p_vaddr = filehdr_vaddr;
2796 if (! m->p_paddr_valid)
2797 p->p_paddr = filehdr_paddr;
2799 else if (m->includes_phdrs)
2801 p->p_vaddr = phdrs_vaddr;
2802 if (! m->p_paddr_valid)
2803 p->p_paddr = phdrs_paddr;
2808 /* Clear out any program headers we allocated but did not use. */
2809 for (; count < alloc; count++, p++)
2811 memset (p, 0, sizeof *p);
2812 p->p_type = PT_NULL;
2815 elf_tdata (abfd)->phdr = phdrs;
2817 elf_tdata (abfd)->next_file_pos = off;
2819 /* Write out the program headers. */
2820 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2821 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2827 /* Get the size of the program header.
2829 If this is called by the linker before any of the section VMA's are set, it
2830 can't calculate the correct value for a strange memory layout. This only
2831 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2832 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2833 data segment (exclusive of .interp and .dynamic).
2835 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2836 will be two segments. */
2838 static bfd_size_type
2839 get_program_header_size (abfd)
2844 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2846 /* We can't return a different result each time we're called. */
2847 if (elf_tdata (abfd)->program_header_size != 0)
2848 return elf_tdata (abfd)->program_header_size;
2850 if (elf_tdata (abfd)->segment_map != NULL)
2852 struct elf_segment_map *m;
2855 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2857 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2858 return elf_tdata (abfd)->program_header_size;
2861 /* Assume we will need exactly two PT_LOAD segments: one for text
2862 and one for data. */
2865 s = bfd_get_section_by_name (abfd, ".interp");
2866 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2868 /* If we have a loadable interpreter section, we need a
2869 PT_INTERP segment. In this case, assume we also need a
2870 PT_PHDR segment, although that may not be true for all
2875 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2877 /* We need a PT_DYNAMIC segment. */
2881 for (s = abfd->sections; s != NULL; s = s->next)
2883 if ((s->flags & SEC_LOAD) != 0
2884 && strncmp (s->name, ".note", 5) == 0)
2886 /* We need a PT_NOTE segment. */
2891 /* Let the backend count up any program headers it might need. */
2892 if (bed->elf_backend_additional_program_headers)
2896 a = (*bed->elf_backend_additional_program_headers) (abfd);
2902 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2903 return elf_tdata (abfd)->program_header_size;
2906 /* Work out the file positions of all the sections. This is called by
2907 _bfd_elf_compute_section_file_positions. All the section sizes and
2908 VMAs must be known before this is called.
2910 We do not consider reloc sections at this point, unless they form
2911 part of the loadable image. Reloc sections are assigned file
2912 positions in assign_file_positions_for_relocs, which is called by
2913 write_object_contents and final_link.
2915 We also don't set the positions of the .symtab and .strtab here. */
2918 assign_file_positions_except_relocs (abfd)
2921 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2922 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2923 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2925 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2927 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
2928 && bfd_get_format (abfd) != bfd_core)
2930 Elf_Internal_Shdr **hdrpp;
2933 /* Start after the ELF header. */
2934 off = i_ehdrp->e_ehsize;
2936 /* We are not creating an executable, which means that we are
2937 not creating a program header, and that the actual order of
2938 the sections in the file is unimportant. */
2939 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2941 Elf_Internal_Shdr *hdr;
2944 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2946 hdr->sh_offset = -1;
2949 if (i == tdata->symtab_section
2950 || i == tdata->strtab_section)
2952 hdr->sh_offset = -1;
2956 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2962 Elf_Internal_Shdr **hdrpp;
2964 /* Assign file positions for the loaded sections based on the
2965 assignment of sections to segments. */
2966 if (! assign_file_positions_for_segments (abfd))
2969 /* Assign file positions for the other sections. */
2971 off = elf_tdata (abfd)->next_file_pos;
2972 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2974 Elf_Internal_Shdr *hdr;
2977 if (hdr->bfd_section != NULL
2978 && hdr->bfd_section->filepos != 0)
2979 hdr->sh_offset = hdr->bfd_section->filepos;
2980 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2982 ((*_bfd_error_handler)
2983 (_("%s: warning: allocated section `%s' not in segment"),
2984 bfd_get_filename (abfd),
2985 (hdr->bfd_section == NULL
2987 : hdr->bfd_section->name)));
2988 if ((abfd->flags & D_PAGED) != 0)
2989 off += (hdr->sh_addr - off) % bed->maxpagesize;
2991 off += (hdr->sh_addr - off) % hdr->sh_addralign;
2992 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2995 else if (hdr->sh_type == SHT_REL
2996 || hdr->sh_type == SHT_RELA
2997 || hdr == i_shdrpp[tdata->symtab_section]
2998 || hdr == i_shdrpp[tdata->strtab_section])
2999 hdr->sh_offset = -1;
3001 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3005 /* Place the section headers. */
3006 off = align_file_position (off, bed->s->file_align);
3007 i_ehdrp->e_shoff = off;
3008 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3010 elf_tdata (abfd)->next_file_pos = off;
3019 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3020 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3021 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3023 struct bfd_strtab_hash *shstrtab;
3024 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3026 i_ehdrp = elf_elfheader (abfd);
3027 i_shdrp = elf_elfsections (abfd);
3029 shstrtab = _bfd_elf_stringtab_init ();
3030 if (shstrtab == NULL)
3033 elf_shstrtab (abfd) = shstrtab;
3035 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3036 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3037 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3038 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3040 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3041 i_ehdrp->e_ident[EI_DATA] =
3042 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3043 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3045 for (count = EI_PAD; count < EI_NIDENT; count++)
3046 i_ehdrp->e_ident[count] = 0;
3048 if ((abfd->flags & DYNAMIC) != 0)
3049 i_ehdrp->e_type = ET_DYN;
3050 else if ((abfd->flags & EXEC_P) != 0)
3051 i_ehdrp->e_type = ET_EXEC;
3052 else if (bfd_get_format (abfd) == bfd_core)
3053 i_ehdrp->e_type = ET_CORE;
3055 i_ehdrp->e_type = ET_REL;
3057 switch (bfd_get_arch (abfd))
3059 case bfd_arch_unknown:
3060 i_ehdrp->e_machine = EM_NONE;
3062 case bfd_arch_sparc:
3063 if (bed->s->arch_size == 64)
3064 i_ehdrp->e_machine = EM_SPARCV9;
3066 i_ehdrp->e_machine = EM_SPARC;
3069 i_ehdrp->e_machine = EM_386;
3072 i_ehdrp->e_machine = EM_68K;
3075 i_ehdrp->e_machine = EM_88K;
3078 i_ehdrp->e_machine = EM_860;
3080 case bfd_arch_mips: /* MIPS Rxxxx */
3081 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3084 i_ehdrp->e_machine = EM_PARISC;
3086 case bfd_arch_powerpc:
3087 i_ehdrp->e_machine = EM_PPC;
3089 case bfd_arch_alpha:
3090 i_ehdrp->e_machine = EM_ALPHA;
3093 i_ehdrp->e_machine = EM_SH;
3096 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3099 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3102 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3104 case bfd_arch_mcore:
3105 i_ehdrp->e_machine = EM_MCORE;
3108 switch (bfd_get_mach (abfd))
3111 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3115 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3118 i_ehdrp->e_machine = EM_ARM;
3121 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3123 case bfd_arch_mn10200:
3124 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3126 case bfd_arch_mn10300:
3127 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3129 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3131 i_ehdrp->e_machine = EM_NONE;
3133 i_ehdrp->e_version = bed->s->ev_current;
3134 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3136 /* no program header, for now. */
3137 i_ehdrp->e_phoff = 0;
3138 i_ehdrp->e_phentsize = 0;
3139 i_ehdrp->e_phnum = 0;
3141 /* each bfd section is section header entry */
3142 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3143 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3145 /* if we're building an executable, we'll need a program header table */
3146 if (abfd->flags & EXEC_P)
3148 /* it all happens later */
3150 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3152 /* elf_build_phdrs() returns a (NULL-terminated) array of
3153 Elf_Internal_Phdrs */
3154 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3155 i_ehdrp->e_phoff = outbase;
3156 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3161 i_ehdrp->e_phentsize = 0;
3163 i_ehdrp->e_phoff = 0;
3166 elf_tdata (abfd)->symtab_hdr.sh_name =
3167 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3168 elf_tdata (abfd)->strtab_hdr.sh_name =
3169 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3170 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3171 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3172 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3173 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3174 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3180 /* Assign file positions for all the reloc sections which are not part
3181 of the loadable file image. */
3184 _bfd_elf_assign_file_positions_for_relocs (abfd)
3189 Elf_Internal_Shdr **shdrpp;
3191 off = elf_tdata (abfd)->next_file_pos;
3193 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3194 i < elf_elfheader (abfd)->e_shnum;
3197 Elf_Internal_Shdr *shdrp;
3200 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3201 && shdrp->sh_offset == -1)
3202 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3205 elf_tdata (abfd)->next_file_pos = off;
3209 _bfd_elf_write_object_contents (abfd)
3212 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3213 Elf_Internal_Ehdr *i_ehdrp;
3214 Elf_Internal_Shdr **i_shdrp;
3218 if (! abfd->output_has_begun
3219 && ! _bfd_elf_compute_section_file_positions
3220 (abfd, (struct bfd_link_info *) NULL))
3223 i_shdrp = elf_elfsections (abfd);
3224 i_ehdrp = elf_elfheader (abfd);
3227 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3231 _bfd_elf_assign_file_positions_for_relocs (abfd);
3233 /* After writing the headers, we need to write the sections too... */
3234 for (count = 1; count < i_ehdrp->e_shnum; count++)
3236 if (bed->elf_backend_section_processing)
3237 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3238 if (i_shdrp[count]->contents)
3240 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3241 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3243 != i_shdrp[count]->sh_size))
3248 /* Write out the section header names. */
3249 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3250 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3253 if (bed->elf_backend_final_write_processing)
3254 (*bed->elf_backend_final_write_processing) (abfd,
3255 elf_tdata (abfd)->linker);
3257 return bed->s->write_shdrs_and_ehdr (abfd);
3261 _bfd_elf_write_corefile_contents (abfd)
3264 /* Hopefully this can be done just like an object file. */
3265 return _bfd_elf_write_object_contents (abfd);
3267 /* given a section, search the header to find them... */
3269 _bfd_elf_section_from_bfd_section (abfd, asect)
3273 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3274 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3276 Elf_Internal_Shdr *hdr;
3277 int maxindex = elf_elfheader (abfd)->e_shnum;
3279 for (index = 0; index < maxindex; index++)
3281 hdr = i_shdrp[index];
3282 if (hdr->bfd_section == asect)
3286 if (bed->elf_backend_section_from_bfd_section)
3288 for (index = 0; index < maxindex; index++)
3292 hdr = i_shdrp[index];
3294 if ((*bed->elf_backend_section_from_bfd_section)
3295 (abfd, hdr, asect, &retval))
3300 if (bfd_is_abs_section (asect))
3302 if (bfd_is_com_section (asect))
3304 if (bfd_is_und_section (asect))
3307 bfd_set_error (bfd_error_nonrepresentable_section);
3312 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3316 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3318 asymbol **asym_ptr_ptr;
3320 asymbol *asym_ptr = *asym_ptr_ptr;
3322 flagword flags = asym_ptr->flags;
3324 /* When gas creates relocations against local labels, it creates its
3325 own symbol for the section, but does put the symbol into the
3326 symbol chain, so udata is 0. When the linker is generating
3327 relocatable output, this section symbol may be for one of the
3328 input sections rather than the output section. */
3329 if (asym_ptr->udata.i == 0
3330 && (flags & BSF_SECTION_SYM)
3331 && asym_ptr->section)
3335 if (asym_ptr->section->output_section != NULL)
3336 indx = asym_ptr->section->output_section->index;
3338 indx = asym_ptr->section->index;
3339 if (elf_section_syms (abfd)[indx])
3340 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3343 idx = asym_ptr->udata.i;
3347 /* This case can occur when using --strip-symbol on a symbol
3348 which is used in a relocation entry. */
3349 (*_bfd_error_handler)
3350 (_("%s: symbol `%s' required but not present"),
3351 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3352 bfd_set_error (bfd_error_no_symbols);
3359 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3360 (long) asym_ptr, asym_ptr->name, idx, flags,
3361 elf_symbol_flags (flags));
3369 /* Copy private BFD data. This copies any program header information. */
3372 copy_private_bfd_data (ibfd, obfd)
3376 Elf_Internal_Ehdr *iehdr;
3377 struct elf_segment_map *mfirst;
3378 struct elf_segment_map **pm;
3379 struct elf_segment_map *m;
3380 Elf_Internal_Phdr *p;
3382 unsigned int num_segments;
3383 boolean phdr_included = false;
3385 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3386 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3389 if (elf_tdata (ibfd)->phdr == NULL)
3392 iehdr = elf_elfheader (ibfd);
3397 num_segments = elf_elfheader (ibfd)->e_phnum;
3399 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3400 ((addr) >= (bottom) \
3401 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3402 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3404 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3406 #define IS_COREFILE_NOTE(p, s) \
3407 (p->p_type == PT_NOTE \
3408 && bfd_get_format (ibfd) == bfd_core \
3409 && s->vma == 0 && s->lma == 0 \
3410 && (bfd_vma) s->filepos >= p->p_offset \
3411 && (bfd_vma) s->filepos + s->_raw_size \
3412 <= p->p_offset + p->p_filesz)
3414 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3415 linker, which generates a PT_INTERP section with p_vaddr and
3416 p_memsz set to 0. */
3418 #define IS_SOLARIS_PT_INTERP(p, s) \
3420 && p->p_filesz > 0 \
3421 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3422 && s->_raw_size > 0 \
3423 && (bfd_vma) s->filepos >= p->p_offset \
3424 && ((bfd_vma) s->filepos + s->_raw_size \
3425 <= p->p_offset + p->p_filesz))
3427 /* Scan through the segments specified in the program header
3428 of the input BFD. */
3429 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3433 asection **sections;
3436 bfd_vma matching_lma;
3437 bfd_vma suggested_lma;
3440 /* For each section in the input BFD, decide if it should be
3441 included in the current segment. A section will be included
3442 if it is within the address space of the segment, and it is
3443 an allocated segment, and there is an output section
3444 associated with it. */
3446 for (s = ibfd->sections; s != NULL; s = s->next)
3447 if (s->output_section != NULL)
3449 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3450 || IS_SOLARIS_PT_INTERP (p, s))
3451 && (s->flags & SEC_ALLOC) != 0)
3453 else if (IS_COREFILE_NOTE (p, s))
3457 /* Allocate a segment map big enough to contain all of the
3458 sections we have selected. */
3459 m = ((struct elf_segment_map *)
3461 (sizeof (struct elf_segment_map)
3462 + ((size_t) csecs - 1) * sizeof (asection *))));
3466 /* Initialise the fields of the segment map. Default to
3467 using the physical address of the segment in the input BFD. */
3469 m->p_type = p->p_type;
3470 m->p_flags = p->p_flags;
3471 m->p_flags_valid = 1;
3472 m->p_paddr = p->p_paddr;
3473 m->p_paddr_valid = 1;
3475 /* Determine if this segment contains the ELF file header
3476 and if it contains the program headers themselves. */
3477 m->includes_filehdr = (p->p_offset == 0
3478 && p->p_filesz >= iehdr->e_ehsize);
3480 m->includes_phdrs = 0;
3482 if (! phdr_included || p->p_type != PT_LOAD)
3485 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3486 && (p->p_offset + p->p_filesz
3487 >= ((bfd_vma) iehdr->e_phoff
3488 + iehdr->e_phnum * iehdr->e_phentsize)));
3489 if (p->p_type == PT_LOAD && m->includes_phdrs)
3490 phdr_included = true;
3495 /* Special segments, such as the PT_PHDR segment, may contain
3496 no sections, but ordinary, loadable segments should contain
3499 if (p->p_type == PT_LOAD)
3501 (_("%s: warning: Empty loadable segment detected\n"),
3502 bfd_get_filename (ibfd));
3511 /* Now scan the sections in the input BFD again and attempt
3512 to add their corresponding output sections to the segment map.
3513 The problem here is how to handle an output section which has
3514 been moved (ie had its LMA changed). There are four possibilities:
3516 1. None of the sections have been moved.
3517 In this case we can continue to use the segment LMA from the
3520 2. All of the sections have been moved by the same amount.
3521 In this case we can change the segment's LMA to match the LMA
3522 of the first section.
3524 3. Some of the sections have been moved, others have not.
3525 In this case those sections which have not been moved can be
3526 placed in the current segment which will have to have its size,
3527 and possibly its LMA changed, and a new segment or segments will
3528 have to be created to contain the other sections.
3530 4. The sections have been moved, but not be the same amount.
3531 In this case we can change the segment's LMA to match the LMA
3532 of the first section and we will have to create a new segment
3533 or segments to contain the other sections.
3535 In order to save time, we allocate an array to hold the section
3536 pointers that we are interested in. As these sections get assigned
3537 to a segment, they are removed from this array. */
3539 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3540 if (sections == NULL)
3543 /* Step One: Scan for segment vs section LMA conflicts.
3544 Also add the sections to the section array allocated above.
3545 Also add the sections to the current segment. In the common
3546 case, where the sections have not been moved, this means that
3547 we have completely filled the segment, and there is nothing
3551 matching_lma = false;
3554 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3556 os = s->output_section;
3558 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3559 || IS_SOLARIS_PT_INTERP (p, s))
3560 && (s->flags & SEC_ALLOC) != 0)
3561 || IS_COREFILE_NOTE (p, s))
3566 /* The Solaris native linker always sets p_paddr to 0.
3567 We try to catch that case here, and set it to the
3573 && (os->vma == (p->p_vaddr
3574 + (m->includes_filehdr
3577 + (m->includes_phdrs
3578 ? iehdr->e_phnum * iehdr->e_phentsize
3580 m->p_paddr = p->p_vaddr;
3582 /* Match up the physical address of the segment with the
3583 LMA address of the output section. */
3584 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3585 || IS_COREFILE_NOTE (p, s))
3587 if (matching_lma == 0)
3588 matching_lma = os->lma;
3590 /* We assume that if the section fits within the segment
3591 that it does not overlap any other section within that
3593 m->sections[isec++] = os;
3595 else if (suggested_lma == 0)
3596 suggested_lma = os->lma;
3600 BFD_ASSERT (j == csecs);
3602 /* Step Two: Adjust the physical address of the current segment,
3606 /* All of the sections fitted within the segment as currently
3607 specified. This is the default case. Add the segment to
3608 the list of built segments and carry on to process the next
3609 program header in the input BFD. */
3617 else if (matching_lma != 0)
3619 /* At least one section fits inside the current segment.
3620 Keep it, but modify its physical address to match the
3621 LMA of the first section that fitted. */
3623 m->p_paddr = matching_lma;
3627 /* None of the sections fitted inside the current segment.
3628 Change the current segment's physical address to match
3629 the LMA of the first section. */
3631 m->p_paddr = suggested_lma;
3634 /* Step Three: Loop over the sections again, this time assigning
3635 those that fit to the current segment and remvoing them from the
3636 sections array; but making sure not to leave large gaps. Once all
3637 possible sections have been assigned to the current segment it is
3638 added to the list of built segments and if sections still remain
3639 to be assigned, a new segment is constructed before repeating
3647 /* Fill the current segment with sections that fit. */
3648 for (j = 0; j < csecs; j++)
3655 os = s->output_section;
3657 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3658 || IS_COREFILE_NOTE (p, s))
3662 /* If the first section in a segment does not start at
3663 the beginning of the segment, then something is wrong. */
3664 if (os->lma != m->p_paddr)
3669 asection * prev_sec;
3670 bfd_vma maxpagesize;
3672 prev_sec = m->sections[m->count - 1];
3673 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3675 /* If the gap between the end of the previous section
3676 and the start of this section is more than maxpagesize
3677 then we need to start a new segment. */
3678 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3679 < BFD_ALIGN (os->lma, maxpagesize))
3681 if (suggested_lma == 0)
3682 suggested_lma = os->lma;
3688 m->sections[m->count++] = os;
3692 else if (suggested_lma == 0)
3693 suggested_lma = os->lma;
3696 BFD_ASSERT (m->count > 0);
3698 /* Add the current segment to the list of built segments. */
3704 /* We still have not allocated all of the sections to
3705 segments. Create a new segment here, initialise it
3706 and carry on looping. */
3708 m = ((struct elf_segment_map *)
3710 (sizeof (struct elf_segment_map)
3711 + ((size_t) csecs - 1) * sizeof (asection *))));
3715 /* Initialise the fields of the segment map. Set the physical
3716 physical address to the LMA of the first section that has
3717 not yet been assigned. */
3720 m->p_type = p->p_type;
3721 m->p_flags = p->p_flags;
3722 m->p_flags_valid = 1;
3723 m->p_paddr = suggested_lma;
3724 m->p_paddr_valid = 1;
3725 m->includes_filehdr = 0;
3726 m->includes_phdrs = 0;
3729 while (isec < csecs);
3734 /* The Solaris linker creates program headers in which all the
3735 p_paddr fields are zero. When we try to objcopy or strip such a
3736 file, we get confused. Check for this case, and if we find it
3737 reset the p_paddr_valid fields. */
3738 for (m = mfirst; m != NULL; m = m->next)
3739 if (m->p_paddr != 0)
3743 for (m = mfirst; m != NULL; m = m->next)
3744 m->p_paddr_valid = 0;
3747 elf_tdata (obfd)->segment_map = mfirst;
3750 /* Final Step: Sort the segments into ascending order of physical address. */
3753 struct elf_segment_map* prev;
3756 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3758 /* Yes I know - its a bubble sort....*/
3759 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3761 /* swap m and m->next */
3762 prev->next = m->next;
3763 m->next = m->next->next;
3764 prev->next->next = m;
3773 #undef IS_CONTAINED_BY
3774 #undef IS_SOLARIS_PT_INTERP
3775 #undef IS_COREFILE_NOTE
3779 /* Copy private section information. This copies over the entsize
3780 field, and sometimes the info field. */
3783 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3789 Elf_Internal_Shdr *ihdr, *ohdr;
3791 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3792 || obfd->xvec->flavour != bfd_target_elf_flavour)
3795 /* Copy over private BFD data if it has not already been copied.
3796 This must be done here, rather than in the copy_private_bfd_data
3797 entry point, because the latter is called after the section
3798 contents have been set, which means that the program headers have
3799 already been worked out. */
3800 if (elf_tdata (obfd)->segment_map == NULL
3801 && elf_tdata (ibfd)->phdr != NULL)
3805 /* Only set up the segments if there are no more SEC_ALLOC
3806 sections. FIXME: This won't do the right thing if objcopy is
3807 used to remove the last SEC_ALLOC section, since objcopy
3808 won't call this routine in that case. */
3809 for (s = isec->next; s != NULL; s = s->next)
3810 if ((s->flags & SEC_ALLOC) != 0)
3814 if (! copy_private_bfd_data (ibfd, obfd))
3819 ihdr = &elf_section_data (isec)->this_hdr;
3820 ohdr = &elf_section_data (osec)->this_hdr;
3822 ohdr->sh_entsize = ihdr->sh_entsize;
3824 if (ihdr->sh_type == SHT_SYMTAB
3825 || ihdr->sh_type == SHT_DYNSYM
3826 || ihdr->sh_type == SHT_GNU_verneed
3827 || ihdr->sh_type == SHT_GNU_verdef)
3828 ohdr->sh_info = ihdr->sh_info;
3833 /* Copy private symbol information. If this symbol is in a section
3834 which we did not map into a BFD section, try to map the section
3835 index correctly. We use special macro definitions for the mapped
3836 section indices; these definitions are interpreted by the
3837 swap_out_syms function. */
3839 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3840 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3841 #define MAP_STRTAB (SHN_LORESERVE - 3)
3842 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3845 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3851 elf_symbol_type *isym, *osym;
3853 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3854 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3857 isym = elf_symbol_from (ibfd, isymarg);
3858 osym = elf_symbol_from (obfd, osymarg);
3862 && bfd_is_abs_section (isym->symbol.section))
3866 shndx = isym->internal_elf_sym.st_shndx;
3867 if (shndx == elf_onesymtab (ibfd))
3868 shndx = MAP_ONESYMTAB;
3869 else if (shndx == elf_dynsymtab (ibfd))
3870 shndx = MAP_DYNSYMTAB;
3871 else if (shndx == elf_tdata (ibfd)->strtab_section)
3873 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3874 shndx = MAP_SHSTRTAB;
3875 osym->internal_elf_sym.st_shndx = shndx;
3881 /* Swap out the symbols. */
3884 swap_out_syms (abfd, sttp, relocatable_p)
3886 struct bfd_strtab_hash **sttp;
3889 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3891 if (!elf_map_symbols (abfd))
3894 /* Dump out the symtabs. */
3896 int symcount = bfd_get_symcount (abfd);
3897 asymbol **syms = bfd_get_outsymbols (abfd);
3898 struct bfd_strtab_hash *stt;
3899 Elf_Internal_Shdr *symtab_hdr;
3900 Elf_Internal_Shdr *symstrtab_hdr;
3901 char *outbound_syms;
3904 stt = _bfd_elf_stringtab_init ();
3908 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3909 symtab_hdr->sh_type = SHT_SYMTAB;
3910 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3911 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3912 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3913 symtab_hdr->sh_addralign = bed->s->file_align;
3915 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3916 symstrtab_hdr->sh_type = SHT_STRTAB;
3918 outbound_syms = bfd_alloc (abfd,
3919 (1 + symcount) * bed->s->sizeof_sym);
3920 if (outbound_syms == NULL)
3922 symtab_hdr->contents = (PTR) outbound_syms;
3924 /* now generate the data (for "contents") */
3926 /* Fill in zeroth symbol and swap it out. */
3927 Elf_Internal_Sym sym;
3933 sym.st_shndx = SHN_UNDEF;
3934 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3935 outbound_syms += bed->s->sizeof_sym;
3937 for (idx = 0; idx < symcount; idx++)
3939 Elf_Internal_Sym sym;
3940 bfd_vma value = syms[idx]->value;
3941 elf_symbol_type *type_ptr;
3942 flagword flags = syms[idx]->flags;
3945 if (flags & BSF_SECTION_SYM)
3946 /* Section symbols have no names. */
3950 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3953 if (sym.st_name == (unsigned long) -1)
3957 type_ptr = elf_symbol_from (abfd, syms[idx]);
3959 if ((flags & BSF_SECTION_SYM) == 0
3960 && bfd_is_com_section (syms[idx]->section))
3962 /* ELF common symbols put the alignment into the `value' field,
3963 and the size into the `size' field. This is backwards from
3964 how BFD handles it, so reverse it here. */
3965 sym.st_size = value;
3966 if (type_ptr == NULL
3967 || type_ptr->internal_elf_sym.st_value == 0)
3968 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3970 sym.st_value = type_ptr->internal_elf_sym.st_value;
3971 sym.st_shndx = _bfd_elf_section_from_bfd_section
3972 (abfd, syms[idx]->section);
3976 asection *sec = syms[idx]->section;
3979 if (sec->output_section)
3981 value += sec->output_offset;
3982 sec = sec->output_section;
3984 /* Don't add in the section vma for relocatable output. */
3985 if (! relocatable_p)
3987 sym.st_value = value;
3988 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
3990 if (bfd_is_abs_section (sec)
3992 && type_ptr->internal_elf_sym.st_shndx != 0)
3994 /* This symbol is in a real ELF section which we did
3995 not create as a BFD section. Undo the mapping done
3996 by copy_private_symbol_data. */
3997 shndx = type_ptr->internal_elf_sym.st_shndx;
4001 shndx = elf_onesymtab (abfd);
4004 shndx = elf_dynsymtab (abfd);
4007 shndx = elf_tdata (abfd)->strtab_section;
4010 shndx = elf_tdata (abfd)->shstrtab_section;
4018 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4024 /* Writing this would be a hell of a lot easier if
4025 we had some decent documentation on bfd, and
4026 knew what to expect of the library, and what to
4027 demand of applications. For example, it
4028 appears that `objcopy' might not set the
4029 section of a symbol to be a section that is
4030 actually in the output file. */
4031 sec2 = bfd_get_section_by_name (abfd, sec->name);
4032 BFD_ASSERT (sec2 != 0);
4033 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4034 BFD_ASSERT (shndx != -1);
4038 sym.st_shndx = shndx;
4041 if ((flags & BSF_FUNCTION) != 0)
4043 else if ((flags & BSF_OBJECT) != 0)
4048 /* Processor-specific types */
4049 if (bed->elf_backend_get_symbol_type)
4050 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4052 if (flags & BSF_SECTION_SYM)
4053 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4054 else if (bfd_is_com_section (syms[idx]->section))
4055 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4056 else if (bfd_is_und_section (syms[idx]->section))
4057 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4061 else if (flags & BSF_FILE)
4062 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4065 int bind = STB_LOCAL;
4067 if (flags & BSF_LOCAL)
4069 else if (flags & BSF_WEAK)
4071 else if (flags & BSF_GLOBAL)
4074 sym.st_info = ELF_ST_INFO (bind, type);
4077 if (type_ptr != NULL)
4078 sym.st_other = type_ptr->internal_elf_sym.st_other;
4082 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4083 outbound_syms += bed->s->sizeof_sym;
4087 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4088 symstrtab_hdr->sh_type = SHT_STRTAB;
4090 symstrtab_hdr->sh_flags = 0;
4091 symstrtab_hdr->sh_addr = 0;
4092 symstrtab_hdr->sh_entsize = 0;
4093 symstrtab_hdr->sh_link = 0;
4094 symstrtab_hdr->sh_info = 0;
4095 symstrtab_hdr->sh_addralign = 1;
4101 /* Return the number of bytes required to hold the symtab vector.
4103 Note that we base it on the count plus 1, since we will null terminate
4104 the vector allocated based on this size. However, the ELF symbol table
4105 always has a dummy entry as symbol #0, so it ends up even. */
4108 _bfd_elf_get_symtab_upper_bound (abfd)
4113 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4115 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4116 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4122 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4127 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4129 if (elf_dynsymtab (abfd) == 0)
4131 bfd_set_error (bfd_error_invalid_operation);
4135 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4136 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4142 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4146 return (asect->reloc_count + 1) * sizeof (arelent *);
4149 /* Canonicalize the relocs. */
4152 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4161 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4167 tblptr = section->relocation;
4168 for (i = 0; i < section->reloc_count; i++)
4169 *relptr++ = tblptr++;
4173 return section->reloc_count;
4177 _bfd_elf_get_symtab (abfd, alocation)
4179 asymbol **alocation;
4181 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4182 (abfd, alocation, false);
4185 bfd_get_symcount (abfd) = symcount;
4190 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4192 asymbol **alocation;
4194 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4195 (abfd, alocation, true);
4198 /* Return the size required for the dynamic reloc entries. Any
4199 section that was actually installed in the BFD, and has type
4200 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4201 considered to be a dynamic reloc section. */
4204 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4210 if (elf_dynsymtab (abfd) == 0)
4212 bfd_set_error (bfd_error_invalid_operation);
4216 ret = sizeof (arelent *);
4217 for (s = abfd->sections; s != NULL; s = s->next)
4218 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4219 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4220 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4221 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4222 * sizeof (arelent *));
4227 /* Canonicalize the dynamic relocation entries. Note that we return
4228 the dynamic relocations as a single block, although they are
4229 actually associated with particular sections; the interface, which
4230 was designed for SunOS style shared libraries, expects that there
4231 is only one set of dynamic relocs. Any section that was actually
4232 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4233 the dynamic symbol table, is considered to be a dynamic reloc
4237 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4242 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4246 if (elf_dynsymtab (abfd) == 0)
4248 bfd_set_error (bfd_error_invalid_operation);
4252 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4254 for (s = abfd->sections; s != NULL; s = s->next)
4256 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4257 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4258 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4263 if (! (*slurp_relocs) (abfd, s, syms, true))
4265 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4267 for (i = 0; i < count; i++)
4278 /* Read in the version information. */
4281 _bfd_elf_slurp_version_tables (abfd)
4284 bfd_byte *contents = NULL;
4286 if (elf_dynverdef (abfd) != 0)
4288 Elf_Internal_Shdr *hdr;
4289 Elf_External_Verdef *everdef;
4290 Elf_Internal_Verdef *iverdef;
4293 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4295 elf_tdata (abfd)->verdef =
4296 ((Elf_Internal_Verdef *)
4297 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4298 if (elf_tdata (abfd)->verdef == NULL)
4301 elf_tdata (abfd)->cverdefs = hdr->sh_info;
4303 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4304 if (contents == NULL)
4306 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4307 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4310 everdef = (Elf_External_Verdef *) contents;
4311 iverdef = elf_tdata (abfd)->verdef;
4312 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4314 Elf_External_Verdaux *everdaux;
4315 Elf_Internal_Verdaux *iverdaux;
4318 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4320 iverdef->vd_bfd = abfd;
4322 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4325 * sizeof (Elf_Internal_Verdaux))));
4326 if (iverdef->vd_auxptr == NULL)
4329 everdaux = ((Elf_External_Verdaux *)
4330 ((bfd_byte *) everdef + iverdef->vd_aux));
4331 iverdaux = iverdef->vd_auxptr;
4332 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4334 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4336 iverdaux->vda_nodename =
4337 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4338 iverdaux->vda_name);
4339 if (iverdaux->vda_nodename == NULL)
4342 if (j + 1 < iverdef->vd_cnt)
4343 iverdaux->vda_nextptr = iverdaux + 1;
4345 iverdaux->vda_nextptr = NULL;
4347 everdaux = ((Elf_External_Verdaux *)
4348 ((bfd_byte *) everdaux + iverdaux->vda_next));
4351 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4353 if (i + 1 < hdr->sh_info)
4354 iverdef->vd_nextdef = iverdef + 1;
4356 iverdef->vd_nextdef = NULL;
4358 everdef = ((Elf_External_Verdef *)
4359 ((bfd_byte *) everdef + iverdef->vd_next));
4366 if (elf_dynverref (abfd) != 0)
4368 Elf_Internal_Shdr *hdr;
4369 Elf_External_Verneed *everneed;
4370 Elf_Internal_Verneed *iverneed;
4373 hdr = &elf_tdata (abfd)->dynverref_hdr;
4375 elf_tdata (abfd)->verref =
4376 ((Elf_Internal_Verneed *)
4377 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4378 if (elf_tdata (abfd)->verref == NULL)
4381 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4383 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4384 if (contents == NULL)
4386 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4387 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4390 everneed = (Elf_External_Verneed *) contents;
4391 iverneed = elf_tdata (abfd)->verref;
4392 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4394 Elf_External_Vernaux *evernaux;
4395 Elf_Internal_Vernaux *ivernaux;
4398 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4400 iverneed->vn_bfd = abfd;
4402 iverneed->vn_filename =
4403 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4405 if (iverneed->vn_filename == NULL)
4408 iverneed->vn_auxptr =
4409 ((Elf_Internal_Vernaux *)
4411 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4413 evernaux = ((Elf_External_Vernaux *)
4414 ((bfd_byte *) everneed + iverneed->vn_aux));
4415 ivernaux = iverneed->vn_auxptr;
4416 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4418 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4420 ivernaux->vna_nodename =
4421 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4422 ivernaux->vna_name);
4423 if (ivernaux->vna_nodename == NULL)
4426 if (j + 1 < iverneed->vn_cnt)
4427 ivernaux->vna_nextptr = ivernaux + 1;
4429 ivernaux->vna_nextptr = NULL;
4431 evernaux = ((Elf_External_Vernaux *)
4432 ((bfd_byte *) evernaux + ivernaux->vna_next));
4435 if (i + 1 < hdr->sh_info)
4436 iverneed->vn_nextref = iverneed + 1;
4438 iverneed->vn_nextref = NULL;
4440 everneed = ((Elf_External_Verneed *)
4441 ((bfd_byte *) everneed + iverneed->vn_next));
4451 if (contents == NULL)
4457 _bfd_elf_make_empty_symbol (abfd)
4460 elf_symbol_type *newsym;
4462 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4467 newsym->symbol.the_bfd = abfd;
4468 return &newsym->symbol;
4473 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4478 bfd_symbol_info (symbol, ret);
4481 /* Return whether a symbol name implies a local symbol. Most targets
4482 use this function for the is_local_label_name entry point, but some
4486 _bfd_elf_is_local_label_name (abfd, name)
4490 /* Normal local symbols start with ``.L''. */
4491 if (name[0] == '.' && name[1] == 'L')
4494 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4495 DWARF debugging symbols starting with ``..''. */
4496 if (name[0] == '.' && name[1] == '.')
4499 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4500 emitting DWARF debugging output. I suspect this is actually a
4501 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4502 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4503 underscore to be emitted on some ELF targets). For ease of use,
4504 we treat such symbols as local. */
4505 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4512 _bfd_elf_get_lineno (ignore_abfd, symbol)
4521 _bfd_elf_set_arch_mach (abfd, arch, machine)
4523 enum bfd_architecture arch;
4524 unsigned long machine;
4526 /* If this isn't the right architecture for this backend, and this
4527 isn't the generic backend, fail. */
4528 if (arch != get_elf_backend_data (abfd)->arch
4529 && arch != bfd_arch_unknown
4530 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4533 return bfd_default_set_arch_mach (abfd, arch, machine);
4536 /* Find the nearest line to a particular section and offset, for error
4540 _bfd_elf_find_nearest_line (abfd,
4551 CONST char **filename_ptr;
4552 CONST char **functionname_ptr;
4553 unsigned int *line_ptr;
4556 const char *filename;
4561 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4562 filename_ptr, functionname_ptr,
4566 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4567 filename_ptr, functionname_ptr,
4571 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4572 &found, filename_ptr,
4573 functionname_ptr, line_ptr,
4574 &elf_tdata (abfd)->line_info))
4579 if (symbols == NULL)
4586 for (p = symbols; *p != NULL; p++)
4590 q = (elf_symbol_type *) *p;
4592 if (bfd_get_section (&q->symbol) != section)
4595 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4600 filename = bfd_asymbol_name (&q->symbol);
4604 if (q->symbol.section == section
4605 && q->symbol.value >= low_func
4606 && q->symbol.value <= offset)
4608 func = (asymbol *) q;
4609 low_func = q->symbol.value;
4618 *filename_ptr = filename;
4619 *functionname_ptr = bfd_asymbol_name (func);
4625 _bfd_elf_sizeof_headers (abfd, reloc)
4631 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4633 ret += get_program_header_size (abfd);
4638 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4643 bfd_size_type count;
4645 Elf_Internal_Shdr *hdr;
4647 if (! abfd->output_has_begun
4648 && ! _bfd_elf_compute_section_file_positions
4649 (abfd, (struct bfd_link_info *) NULL))
4652 hdr = &elf_section_data (section)->this_hdr;
4654 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4656 if (bfd_write (location, 1, count, abfd) != count)
4663 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4666 Elf_Internal_Rela *dst;
4673 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4676 Elf_Internal_Rel *dst;
4682 /* Try to convert a non-ELF reloc into an ELF one. */
4685 _bfd_elf_validate_reloc (abfd, areloc)
4689 /* Check whether we really have an ELF howto. */
4691 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4693 bfd_reloc_code_real_type code;
4694 reloc_howto_type *howto;
4696 /* Alien reloc: Try to determine its type to replace it with an
4697 equivalent ELF reloc. */
4699 if (areloc->howto->pc_relative)
4701 switch (areloc->howto->bitsize)
4704 code = BFD_RELOC_8_PCREL;
4707 code = BFD_RELOC_12_PCREL;
4710 code = BFD_RELOC_16_PCREL;
4713 code = BFD_RELOC_24_PCREL;
4716 code = BFD_RELOC_32_PCREL;
4719 code = BFD_RELOC_64_PCREL;
4725 howto = bfd_reloc_type_lookup (abfd, code);
4727 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4729 if (howto->pcrel_offset)
4730 areloc->addend += areloc->address;
4732 areloc->addend -= areloc->address; /* addend is unsigned!! */
4737 switch (areloc->howto->bitsize)
4743 code = BFD_RELOC_14;
4746 code = BFD_RELOC_16;
4749 code = BFD_RELOC_26;
4752 code = BFD_RELOC_32;
4755 code = BFD_RELOC_64;
4761 howto = bfd_reloc_type_lookup (abfd, code);
4765 areloc->howto = howto;
4773 (*_bfd_error_handler)
4774 (_("%s: unsupported relocation type %s"),
4775 bfd_get_filename (abfd), areloc->howto->name);
4776 bfd_set_error (bfd_error_bad_value);
4781 _bfd_elf_close_and_cleanup (abfd)
4784 if (bfd_get_format (abfd) == bfd_object)
4786 if (elf_shstrtab (abfd) != NULL)
4787 _bfd_stringtab_free (elf_shstrtab (abfd));
4790 return _bfd_generic_close_and_cleanup (abfd);
4793 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4794 in the relocation's offset. Thus we cannot allow any sort of sanity
4795 range-checking to interfere. There is nothing else to do in processing
4798 bfd_reloc_status_type
4799 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
4802 struct symbol_cache_entry *symbol;
4808 return bfd_reloc_ok;
4812 /* Elf core file support. Much of this only works on native
4813 toolchains, since we rely on knowing the
4814 machine-dependent procfs structure in order to pick
4815 out details about the corefile. */
4817 #ifdef HAVE_SYS_PROCFS_H
4818 # include <sys/procfs.h>
4822 /* Define offsetof for those systems which lack it. */
4825 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
4829 /* FIXME: this is kinda wrong, but it's what gdb wants. */
4832 elfcore_make_pid (abfd)
4835 return ((elf_tdata (abfd)->core_lwpid << 16)
4836 + (elf_tdata (abfd)->core_pid));
4840 /* If there isn't a section called NAME, make one, using
4841 data from SECT. Note, this function will generate a
4842 reference to NAME, so you shouldn't deallocate or
4846 elfcore_maybe_make_sect (abfd, name, sect)
4853 if (bfd_get_section_by_name (abfd, name) != NULL)
4856 sect2 = bfd_make_section (abfd, name);
4860 sect2->_raw_size = sect->_raw_size;
4861 sect2->filepos = sect->filepos;
4862 sect2->flags = sect->flags;
4863 sect2->alignment_power = sect->alignment_power;
4868 /* prstatus_t exists on:
4870 linux 2.[01] + glibc
4874 #if defined (HAVE_PRSTATUS_T)
4876 elfcore_grok_prstatus (abfd, note)
4878 Elf_Internal_Note* note;
4885 if (note->descsz != sizeof (prstat))
4888 memcpy (&prstat, note->descdata, sizeof (prstat));
4890 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4891 elf_tdata (abfd)->core_pid = prstat.pr_pid;
4893 /* pr_who exists on:
4896 pr_who doesn't exist on:
4899 #if defined (HAVE_PRSTATUS_T_PR_WHO)
4900 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
4903 /* Make a ".reg/999" section. */
4905 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
4906 name = bfd_alloc (abfd, strlen (buf) + 1);
4911 sect = bfd_make_section (abfd, name);
4914 sect->_raw_size = sizeof (prstat.pr_reg);
4915 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
4916 sect->flags = SEC_HAS_CONTENTS;
4917 sect->alignment_power = 2;
4919 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
4924 #endif /* defined (HAVE_PRSTATUS_T) */
4927 /* There isn't a consistent prfpregset_t across platforms,
4928 but it doesn't matter, because we don't have to pick this
4929 data structure apart. */
4932 elfcore_grok_prfpreg (abfd, note)
4934 Elf_Internal_Note* note;
4940 /* Make a ".reg2/999" section. */
4942 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
4943 name = bfd_alloc (abfd, strlen (buf) + 1);
4948 sect = bfd_make_section (abfd, name);
4951 sect->_raw_size = note->descsz;
4952 sect->filepos = note->descpos;
4953 sect->flags = SEC_HAS_CONTENTS;
4954 sect->alignment_power = 2;
4956 if (! elfcore_maybe_make_sect (abfd, ".reg2", sect))
4962 #if defined (HAVE_PRPSINFO_T)
4963 # define elfcore_psinfo_t prpsinfo_t
4966 #if defined (HAVE_PSINFO_T)
4967 # define elfcore_psinfo_t psinfo_t
4971 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
4973 /* return a malloc'ed copy of a string at START which is at
4974 most MAX bytes long, possibly without a terminating '\0'.
4975 the copy will always have a terminating '\0'. */
4978 elfcore_strndup (abfd, start, max)
4984 char* end = memchr (start, '\0', max);
4992 dup = bfd_alloc (abfd, len + 1);
4996 memcpy (dup, start, len);
5003 elfcore_grok_psinfo (abfd, note)
5005 Elf_Internal_Note* note;
5007 elfcore_psinfo_t psinfo;
5009 if (note->descsz != sizeof (elfcore_psinfo_t))
5012 memcpy (&psinfo, note->descdata, note->descsz);
5014 elf_tdata (abfd)->core_program
5015 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5017 elf_tdata (abfd)->core_command
5018 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5020 /* Note that for some reason, a spurious space is tacked
5021 onto the end of the args in some (at least one anyway)
5022 implementations, so strip it off if it exists. */
5025 char* command = elf_tdata (abfd)->core_command;
5026 int n = strlen (command);
5028 if (0 < n && command[n - 1] == ' ')
5029 command[n - 1] = '\0';
5034 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5037 #if defined (HAVE_PSTATUS_T)
5039 elfcore_grok_pstatus (abfd, note)
5041 Elf_Internal_Note* note;
5045 if (note->descsz != sizeof (pstat))
5048 memcpy (&pstat, note->descdata, sizeof (pstat));
5050 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5052 /* Could grab some more details from the "representative"
5053 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5054 NT_LWPSTATUS note, presumably. */
5058 #endif /* defined (HAVE_PSTATUS_T) */
5061 #if defined (HAVE_LWPSTATUS_T)
5063 elfcore_grok_lwpstatus (abfd, note)
5065 Elf_Internal_Note* note;
5067 lwpstatus_t lwpstat;
5072 if (note->descsz != sizeof (lwpstat))
5075 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5077 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5078 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5080 /* Make a ".reg/999" section. */
5082 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5083 name = bfd_alloc (abfd, strlen (buf) + 1);
5088 sect = bfd_make_section (abfd, name);
5092 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5093 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5094 sect->filepos = note->descpos
5095 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5098 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5099 sect->_raw_size = sizeof (lwpstat.pr_reg);
5100 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5103 sect->flags = SEC_HAS_CONTENTS;
5104 sect->alignment_power = 2;
5106 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5109 /* Make a ".reg2/999" section */
5111 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5112 name = bfd_alloc (abfd, strlen (buf) + 1);
5117 sect = bfd_make_section (abfd, name);
5121 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5122 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5123 sect->filepos = note->descpos
5124 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5127 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5128 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5129 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5132 sect->flags = SEC_HAS_CONTENTS;
5133 sect->alignment_power = 2;
5135 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5140 #endif /* defined (HAVE_LWPSTATUS_T) */
5145 elfcore_grok_note (abfd, note)
5147 Elf_Internal_Note* note;
5154 #if defined (HAVE_PRSTATUS_T)
5156 return elfcore_grok_prstatus (abfd, note);
5159 #if defined (HAVE_PSTATUS_T)
5161 return elfcore_grok_pstatus (abfd, note);
5164 #if defined (HAVE_LWPSTATUS_T)
5166 return elfcore_grok_lwpstatus (abfd, note);
5169 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5170 return elfcore_grok_prfpreg (abfd, note);
5172 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5175 return elfcore_grok_psinfo (abfd, note);
5182 elfcore_read_notes (abfd, offset, size)
5193 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5196 buf = bfd_malloc ((size_t) size);
5200 if (bfd_read (buf, size, 1, abfd) != size)
5208 while (p < buf + size)
5210 /* FIXME: bad alignment assumption. */
5211 Elf_External_Note* xnp = (Elf_External_Note*) p;
5212 Elf_Internal_Note in;
5214 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5216 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5217 in.namedata = xnp->name;
5219 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5220 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5221 in.descpos = offset + (in.descdata - buf);
5223 if (! elfcore_grok_note (abfd, &in))
5226 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5236 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5238 Elf_Internal_Phdr* phdr;
5241 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5244 if (phdr->p_type == PT_NOTE
5245 && ! elfcore_read_notes (abfd, phdr->p_offset, phdr->p_filesz))