1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 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
34 /* For sparc64-cross-sparc32. */
43 static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
45 static boolean map_sections_to_segments PARAMS ((bfd *));
46 static int elf_sort_sections PARAMS ((const PTR, const PTR));
47 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
48 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
49 static boolean prep_headers PARAMS ((bfd *));
50 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
51 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
52 static char *elf_read PARAMS ((bfd *, long, unsigned int));
53 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
54 static boolean assign_section_numbers PARAMS ((bfd *));
55 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
56 static boolean elf_map_symbols PARAMS ((bfd *));
57 static bfd_size_type get_program_header_size PARAMS ((bfd *));
58 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (abfd, src, dst)
69 const Elf_External_Verdef *src;
70 Elf_Internal_Verdef *dst;
72 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
73 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
74 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
75 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
76 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
77 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
78 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (abfd, src, dst)
86 const Elf_Internal_Verdef *src;
87 Elf_External_Verdef *dst;
89 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
90 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
91 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
92 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
93 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
94 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
95 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
98 /* Swap in a Verdaux structure. */
101 _bfd_elf_swap_verdaux_in (abfd, src, dst)
103 const Elf_External_Verdaux *src;
104 Elf_Internal_Verdaux *dst;
106 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
107 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
110 /* Swap out a Verdaux structure. */
113 _bfd_elf_swap_verdaux_out (abfd, src, dst)
115 const Elf_Internal_Verdaux *src;
116 Elf_External_Verdaux *dst;
118 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
119 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
122 /* Swap in a Verneed structure. */
125 _bfd_elf_swap_verneed_in (abfd, src, dst)
127 const Elf_External_Verneed *src;
128 Elf_Internal_Verneed *dst;
130 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
131 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
132 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
133 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
134 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
137 /* Swap out a Verneed structure. */
140 _bfd_elf_swap_verneed_out (abfd, src, dst)
142 const Elf_Internal_Verneed *src;
143 Elf_External_Verneed *dst;
145 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
146 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
147 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
148 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
149 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
152 /* Swap in a Vernaux structure. */
155 _bfd_elf_swap_vernaux_in (abfd, src, dst)
157 const Elf_External_Vernaux *src;
158 Elf_Internal_Vernaux *dst;
160 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
161 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
162 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
163 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
164 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
167 /* Swap out a Vernaux structure. */
170 _bfd_elf_swap_vernaux_out (abfd, src, dst)
172 const Elf_Internal_Vernaux *src;
173 Elf_External_Vernaux *dst;
175 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
176 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
177 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
178 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
179 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
182 /* Swap in a Versym structure. */
185 _bfd_elf_swap_versym_in (abfd, src, dst)
187 const Elf_External_Versym *src;
188 Elf_Internal_Versym *dst;
190 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
193 /* Swap out a Versym structure. */
196 _bfd_elf_swap_versym_out (abfd, src, dst)
198 const Elf_Internal_Versym *src;
199 Elf_External_Versym *dst;
201 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
204 /* Standard ELF hash function. Do not change this function; you will
205 cause invalid hash tables to be generated. */
208 bfd_elf_hash (namearg)
211 const unsigned char *name = (const unsigned char *) namearg;
216 while ((ch = *name++) != '\0')
219 if ((g = (h & 0xf0000000)) != 0)
222 /* The ELF ABI says `h &= ~g', but this is equivalent in
223 this case and on some machines one insn instead of two. */
230 /* Read a specified number of bytes at a specified offset in an ELF
231 file, into a newly allocated buffer, and return a pointer to the
235 elf_read (abfd, offset, size)
242 if ((buf = bfd_alloc (abfd, size)) == NULL)
244 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
246 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
248 if (bfd_get_error () != bfd_error_system_call)
249 bfd_set_error (bfd_error_file_truncated);
256 bfd_elf_mkobject (abfd)
259 /* this just does initialization */
260 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
261 elf_tdata (abfd) = (struct elf_obj_tdata *)
262 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
263 if (elf_tdata (abfd) == 0)
265 /* since everything is done at close time, do we need any
272 bfd_elf_mkcorefile (abfd)
275 /* I think this can be done just like an object file. */
276 return bfd_elf_mkobject (abfd);
280 bfd_elf_get_str_section (abfd, shindex)
282 unsigned int shindex;
284 Elf_Internal_Shdr **i_shdrp;
285 char *shstrtab = NULL;
287 unsigned int shstrtabsize;
289 i_shdrp = elf_elfsections (abfd);
290 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
293 shstrtab = (char *) i_shdrp[shindex]->contents;
294 if (shstrtab == NULL)
296 /* No cached one, attempt to read, and cache what we read. */
297 offset = i_shdrp[shindex]->sh_offset;
298 shstrtabsize = i_shdrp[shindex]->sh_size;
299 shstrtab = elf_read (abfd, offset, shstrtabsize);
300 i_shdrp[shindex]->contents = (PTR) shstrtab;
306 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
308 unsigned int shindex;
309 unsigned int strindex;
311 Elf_Internal_Shdr *hdr;
316 hdr = elf_elfsections (abfd)[shindex];
318 if (hdr->contents == NULL
319 && bfd_elf_get_str_section (abfd, shindex) == NULL)
322 if (strindex >= hdr->sh_size)
324 (*_bfd_error_handler)
325 (_("%s: invalid string offset %u >= %lu for section `%s'"),
326 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
327 ((shindex == elf_elfheader(abfd)->e_shstrndx
328 && strindex == hdr->sh_name)
330 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
334 return ((char *) hdr->contents) + strindex;
337 /* Make a BFD section from an ELF section. We store a pointer to the
338 BFD section in the bfd_section field of the header. */
341 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
343 Elf_Internal_Shdr *hdr;
349 if (hdr->bfd_section != NULL)
351 BFD_ASSERT (strcmp (name,
352 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
356 newsect = bfd_make_section_anyway (abfd, name);
360 newsect->filepos = hdr->sh_offset;
362 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
363 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
364 || ! bfd_set_section_alignment (abfd, newsect,
365 bfd_log2 (hdr->sh_addralign)))
368 flags = SEC_NO_FLAGS;
369 if (hdr->sh_type != SHT_NOBITS)
370 flags |= SEC_HAS_CONTENTS;
371 if ((hdr->sh_flags & SHF_ALLOC) != 0)
374 if (hdr->sh_type != SHT_NOBITS)
377 if ((hdr->sh_flags & SHF_WRITE) == 0)
378 flags |= SEC_READONLY;
379 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
381 else if ((flags & SEC_LOAD) != 0)
384 /* The debugging sections appear to be recognized only by name, not
386 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
387 || strncmp (name, ".line", sizeof ".line" - 1) == 0
388 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
389 flags |= SEC_DEBUGGING;
391 /* As a GNU extension, if the name begins with .gnu.linkonce, we
392 only link a single copy of the section. This is used to support
393 g++. g++ will emit each template expansion in its own section.
394 The symbols will be defined as weak, so that multiple definitions
395 are permitted. The GNU linker extension is to actually discard
396 all but one of the sections. */
397 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
398 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
400 if (! bfd_set_section_flags (abfd, newsect, flags))
403 if ((flags & SEC_ALLOC) != 0)
405 Elf_Internal_Phdr *phdr;
408 /* Look through the phdrs to see if we need to adjust the lma.
409 If all the p_paddr fields are zero, we ignore them, since
410 some ELF linkers produce such output. */
411 phdr = elf_tdata (abfd)->phdr;
412 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
414 if (phdr->p_paddr != 0)
417 if (i < elf_elfheader (abfd)->e_phnum)
419 phdr = elf_tdata (abfd)->phdr;
420 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
422 if (phdr->p_type == PT_LOAD
423 && phdr->p_vaddr != phdr->p_paddr
424 && phdr->p_vaddr <= hdr->sh_addr
425 && (phdr->p_vaddr + phdr->p_memsz
426 >= hdr->sh_addr + hdr->sh_size)
427 && ((flags & SEC_LOAD) == 0
428 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
429 && (phdr->p_offset + phdr->p_filesz
430 >= hdr->sh_offset + hdr->sh_size))))
432 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
439 hdr->bfd_section = newsect;
440 elf_section_data (newsect)->this_hdr = *hdr;
450 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
453 Helper functions for GDB to locate the string tables.
454 Since BFD hides string tables from callers, GDB needs to use an
455 internal hook to find them. Sun's .stabstr, in particular,
456 isn't even pointed to by the .stab section, so ordinary
457 mechanisms wouldn't work to find it, even if we had some.
460 struct elf_internal_shdr *
461 bfd_elf_find_section (abfd, name)
465 Elf_Internal_Shdr **i_shdrp;
470 i_shdrp = elf_elfsections (abfd);
473 shstrtab = bfd_elf_get_str_section
474 (abfd, elf_elfheader (abfd)->e_shstrndx);
475 if (shstrtab != NULL)
477 max = elf_elfheader (abfd)->e_shnum;
478 for (i = 1; i < max; i++)
479 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
486 const char *const bfd_elf_section_type_names[] = {
487 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
488 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
489 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
492 /* ELF relocs are against symbols. If we are producing relocateable
493 output, and the reloc is against an external symbol, and nothing
494 has given us any additional addend, the resulting reloc will also
495 be against the same symbol. In such a case, we don't want to
496 change anything about the way the reloc is handled, since it will
497 all be done at final link time. Rather than put special case code
498 into bfd_perform_relocation, all the reloc types use this howto
499 function. It just short circuits the reloc if producing
500 relocateable output against an external symbol. */
503 bfd_reloc_status_type
504 bfd_elf_generic_reloc (abfd,
511 bfd *abfd ATTRIBUTE_UNUSED;
512 arelent *reloc_entry;
514 PTR data ATTRIBUTE_UNUSED;
515 asection *input_section;
517 char **error_message ATTRIBUTE_UNUSED;
519 if (output_bfd != (bfd *) NULL
520 && (symbol->flags & BSF_SECTION_SYM) == 0
521 && (! reloc_entry->howto->partial_inplace
522 || reloc_entry->addend == 0))
524 reloc_entry->address += input_section->output_offset;
528 return bfd_reloc_continue;
531 /* Print out the program headers. */
534 _bfd_elf_print_private_bfd_data (abfd, farg)
538 FILE *f = (FILE *) farg;
539 Elf_Internal_Phdr *p;
541 bfd_byte *dynbuf = NULL;
543 p = elf_tdata (abfd)->phdr;
548 fprintf (f, _("\nProgram Header:\n"));
549 c = elf_elfheader (abfd)->e_phnum;
550 for (i = 0; i < c; i++, p++)
557 case PT_NULL: s = "NULL"; break;
558 case PT_LOAD: s = "LOAD"; break;
559 case PT_DYNAMIC: s = "DYNAMIC"; break;
560 case PT_INTERP: s = "INTERP"; break;
561 case PT_NOTE: s = "NOTE"; break;
562 case PT_SHLIB: s = "SHLIB"; break;
563 case PT_PHDR: s = "PHDR"; break;
564 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
566 fprintf (f, "%8s off 0x", s);
567 fprintf_vma (f, p->p_offset);
568 fprintf (f, " vaddr 0x");
569 fprintf_vma (f, p->p_vaddr);
570 fprintf (f, " paddr 0x");
571 fprintf_vma (f, p->p_paddr);
572 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
573 fprintf (f, " filesz 0x");
574 fprintf_vma (f, p->p_filesz);
575 fprintf (f, " memsz 0x");
576 fprintf_vma (f, p->p_memsz);
577 fprintf (f, " flags %c%c%c",
578 (p->p_flags & PF_R) != 0 ? 'r' : '-',
579 (p->p_flags & PF_W) != 0 ? 'w' : '-',
580 (p->p_flags & PF_X) != 0 ? 'x' : '-');
581 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
582 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
587 s = bfd_get_section_by_name (abfd, ".dynamic");
592 bfd_byte *extdyn, *extdynend;
594 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
596 fprintf (f, _("\nDynamic Section:\n"));
598 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
601 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
605 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
608 link = elf_elfsections (abfd)[elfsec]->sh_link;
610 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
611 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
614 extdynend = extdyn + s->_raw_size;
615 for (; extdyn < extdynend; extdyn += extdynsize)
617 Elf_Internal_Dyn dyn;
622 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
624 if (dyn.d_tag == DT_NULL)
631 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
635 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
636 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
637 case DT_PLTGOT: name = "PLTGOT"; break;
638 case DT_HASH: name = "HASH"; break;
639 case DT_STRTAB: name = "STRTAB"; break;
640 case DT_SYMTAB: name = "SYMTAB"; break;
641 case DT_RELA: name = "RELA"; break;
642 case DT_RELASZ: name = "RELASZ"; break;
643 case DT_RELAENT: name = "RELAENT"; break;
644 case DT_STRSZ: name = "STRSZ"; break;
645 case DT_SYMENT: name = "SYMENT"; break;
646 case DT_INIT: name = "INIT"; break;
647 case DT_FINI: name = "FINI"; break;
648 case DT_SONAME: name = "SONAME"; stringp = true; break;
649 case DT_RPATH: name = "RPATH"; stringp = true; break;
650 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
651 case DT_REL: name = "REL"; break;
652 case DT_RELSZ: name = "RELSZ"; break;
653 case DT_RELENT: name = "RELENT"; break;
654 case DT_PLTREL: name = "PLTREL"; break;
655 case DT_DEBUG: name = "DEBUG"; break;
656 case DT_TEXTREL: name = "TEXTREL"; break;
657 case DT_JMPREL: name = "JMPREL"; break;
658 case DT_BIND_NOW: name = "BIND_NOW"; break;
659 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
660 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
661 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
662 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
663 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
664 case DT_FLAGS: name = "FLAGS"; break;
665 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
666 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
667 case DT_CHECKSUM: name = "CHECKSUM"; break;
668 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
669 case DT_MOVEENT: name = "MOVEENT"; break;
670 case DT_MOVESZ: name = "MOVESZ"; break;
671 case DT_FEATURE: name = "FEATURE"; break;
672 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
673 case DT_SYMINSZ: name = "SYMINSZ"; break;
674 case DT_SYMINENT: name = "SYMINENT"; break;
675 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
676 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
677 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
678 case DT_PLTPAD: name = "PLTPAD"; break;
679 case DT_MOVETAB: name = "MOVETAB"; break;
680 case DT_SYMINFO: name = "SYMINFO"; break;
681 case DT_RELACOUNT: name = "RELACOUNT"; break;
682 case DT_RELCOUNT: name = "RELCOUNT"; break;
683 case DT_FLAGS_1: name = "FLAGS_1"; break;
684 case DT_VERSYM: name = "VERSYM"; break;
685 case DT_VERDEF: name = "VERDEF"; break;
686 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
687 case DT_VERNEED: name = "VERNEED"; break;
688 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
689 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
690 case DT_USED: name = "USED"; break;
691 case DT_FILTER: name = "FILTER"; stringp = true; break;
694 fprintf (f, " %-11s ", name);
696 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
701 string = bfd_elf_string_from_elf_section (abfd, link,
705 fprintf (f, "%s", string);
714 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
715 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
717 if (! _bfd_elf_slurp_version_tables (abfd))
721 if (elf_dynverdef (abfd) != 0)
723 Elf_Internal_Verdef *t;
725 fprintf (f, _("\nVersion definitions:\n"));
726 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
728 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
729 t->vd_flags, t->vd_hash, t->vd_nodename);
730 if (t->vd_auxptr->vda_nextptr != NULL)
732 Elf_Internal_Verdaux *a;
735 for (a = t->vd_auxptr->vda_nextptr;
738 fprintf (f, "%s ", a->vda_nodename);
744 if (elf_dynverref (abfd) != 0)
746 Elf_Internal_Verneed *t;
748 fprintf (f, _("\nVersion References:\n"));
749 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
751 Elf_Internal_Vernaux *a;
753 fprintf (f, _(" required from %s:\n"), t->vn_filename);
754 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
755 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
756 a->vna_flags, a->vna_other, a->vna_nodename);
768 /* Display ELF-specific fields of a symbol. */
771 bfd_elf_print_symbol (abfd, filep, symbol, how)
775 bfd_print_symbol_type how;
777 FILE *file = (FILE *) filep;
780 case bfd_print_symbol_name:
781 fprintf (file, "%s", symbol->name);
783 case bfd_print_symbol_more:
784 fprintf (file, "elf ");
785 fprintf_vma (file, symbol->value);
786 fprintf (file, " %lx", (long) symbol->flags);
788 case bfd_print_symbol_all:
790 CONST char *section_name;
791 CONST char *name = NULL;
792 struct elf_backend_data *bed;
793 unsigned char st_other;
795 section_name = symbol->section ? symbol->section->name : "(*none*)";
797 bed = get_elf_backend_data (abfd);
798 if (bed->elf_backend_print_symbol_all)
799 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
804 bfd_print_symbol_vandf ((PTR) file, symbol);
807 fprintf (file, " %s\t", section_name);
808 /* Print the "other" value for a symbol. For common symbols,
809 we've already printed the size; now print the alignment.
810 For other symbols, we have no specified alignment, and
811 we've printed the address; now print the size. */
813 (bfd_is_com_section (symbol->section)
814 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
815 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
817 /* If we have version information, print it. */
818 if (elf_tdata (abfd)->dynversym_section != 0
819 && (elf_tdata (abfd)->dynverdef_section != 0
820 || elf_tdata (abfd)->dynverref_section != 0))
823 const char *version_string;
825 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
829 else if (vernum == 1)
830 version_string = "Base";
831 else if (vernum <= elf_tdata (abfd)->cverdefs)
833 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
836 Elf_Internal_Verneed *t;
839 for (t = elf_tdata (abfd)->verref;
843 Elf_Internal_Vernaux *a;
845 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
847 if (a->vna_other == vernum)
849 version_string = a->vna_nodename;
856 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
857 fprintf (file, " %-11s", version_string);
862 fprintf (file, " (%s)", version_string);
863 for (i = 10 - strlen (version_string); i > 0; --i)
868 /* If the st_other field is not zero, print it. */
869 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
874 case STV_INTERNAL: fprintf (file, " .internal"); break;
875 case STV_HIDDEN: fprintf (file, " .hidden"); break;
876 case STV_PROTECTED: fprintf (file, " .protected"); break;
878 /* Some other non-defined flags are also present, so print
880 fprintf (file, " 0x%02x", (unsigned int) st_other);
883 fprintf (file, " %s", name);
889 /* Create an entry in an ELF linker hash table. */
891 struct bfd_hash_entry *
892 _bfd_elf_link_hash_newfunc (entry, table, string)
893 struct bfd_hash_entry *entry;
894 struct bfd_hash_table *table;
897 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
899 /* Allocate the structure if it has not already been allocated by a
901 if (ret == (struct elf_link_hash_entry *) NULL)
902 ret = ((struct elf_link_hash_entry *)
903 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
904 if (ret == (struct elf_link_hash_entry *) NULL)
905 return (struct bfd_hash_entry *) ret;
907 /* Call the allocation method of the superclass. */
908 ret = ((struct elf_link_hash_entry *)
909 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
911 if (ret != (struct elf_link_hash_entry *) NULL)
913 /* Set local fields. */
917 ret->dynstr_index = 0;
919 ret->got.offset = (bfd_vma) -1;
920 ret->plt.offset = (bfd_vma) -1;
921 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
922 ret->verinfo.verdef = NULL;
923 ret->vtable_entries_used = NULL;
924 ret->vtable_entries_size = 0;
925 ret->vtable_parent = NULL;
926 ret->type = STT_NOTYPE;
928 /* Assume that we have been called by a non-ELF symbol reader.
929 This flag is then reset by the code which reads an ELF input
930 file. This ensures that a symbol created by a non-ELF symbol
931 reader will have the flag set correctly. */
932 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
935 return (struct bfd_hash_entry *) ret;
938 /* Copy data from an indirect symbol to its direct symbol, hiding the
939 old indirect symbol. */
942 _bfd_elf_link_hash_copy_indirect (dir, ind)
943 struct elf_link_hash_entry *dir, *ind;
945 /* Copy down any references that we may have already seen to the
946 symbol which just became indirect. */
948 dir->elf_link_hash_flags |=
949 (ind->elf_link_hash_flags
950 & (ELF_LINK_HASH_REF_DYNAMIC
951 | ELF_LINK_HASH_REF_REGULAR
952 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
953 | ELF_LINK_NON_GOT_REF));
955 /* Copy over the global and procedure linkage table offset entries.
956 These may have been already set up by a check_relocs routine. */
957 if (dir->got.offset == (bfd_vma) -1)
959 dir->got.offset = ind->got.offset;
960 ind->got.offset = (bfd_vma) -1;
962 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
964 if (dir->plt.offset == (bfd_vma) -1)
966 dir->plt.offset = ind->plt.offset;
967 ind->plt.offset = (bfd_vma) -1;
969 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
971 if (dir->dynindx == -1)
973 dir->dynindx = ind->dynindx;
974 dir->dynstr_index = ind->dynstr_index;
976 ind->dynstr_index = 0;
978 BFD_ASSERT (ind->dynindx == -1);
982 _bfd_elf_link_hash_hide_symbol (info, h)
983 struct bfd_link_info *info ATTRIBUTE_UNUSED;
984 struct elf_link_hash_entry *h;
986 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
988 h->plt.offset = (bfd_vma) -1;
991 /* Initialize an ELF linker hash table. */
994 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
995 struct elf_link_hash_table *table;
997 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
998 struct bfd_hash_table *,
1001 table->dynamic_sections_created = false;
1002 table->dynobj = NULL;
1003 /* The first dynamic symbol is a dummy. */
1004 table->dynsymcount = 1;
1005 table->dynstr = NULL;
1006 table->bucketcount = 0;
1007 table->needed = NULL;
1008 table->runpath = NULL;
1010 table->stab_info = NULL;
1011 table->dynlocal = NULL;
1012 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1015 /* Create an ELF linker hash table. */
1017 struct bfd_link_hash_table *
1018 _bfd_elf_link_hash_table_create (abfd)
1021 struct elf_link_hash_table *ret;
1023 ret = ((struct elf_link_hash_table *)
1024 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1025 if (ret == (struct elf_link_hash_table *) NULL)
1028 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1030 bfd_release (abfd, ret);
1037 /* This is a hook for the ELF emulation code in the generic linker to
1038 tell the backend linker what file name to use for the DT_NEEDED
1039 entry for a dynamic object. The generic linker passes name as an
1040 empty string to indicate that no DT_NEEDED entry should be made. */
1043 bfd_elf_set_dt_needed_name (abfd, name)
1047 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1048 && bfd_get_format (abfd) == bfd_object)
1049 elf_dt_name (abfd) = name;
1053 bfd_elf_set_dt_needed_soname (abfd, name)
1057 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1058 && bfd_get_format (abfd) == bfd_object)
1059 elf_dt_soname (abfd) = name;
1062 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1063 the linker ELF emulation code. */
1065 struct bfd_link_needed_list *
1066 bfd_elf_get_needed_list (abfd, info)
1067 bfd *abfd ATTRIBUTE_UNUSED;
1068 struct bfd_link_info *info;
1070 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1072 return elf_hash_table (info)->needed;
1075 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1076 hook for the linker ELF emulation code. */
1078 struct bfd_link_needed_list *
1079 bfd_elf_get_runpath_list (abfd, info)
1080 bfd *abfd ATTRIBUTE_UNUSED;
1081 struct bfd_link_info *info;
1083 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1085 return elf_hash_table (info)->runpath;
1088 /* Get the name actually used for a dynamic object for a link. This
1089 is the SONAME entry if there is one. Otherwise, it is the string
1090 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1093 bfd_elf_get_dt_soname (abfd)
1096 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1097 && bfd_get_format (abfd) == bfd_object)
1098 return elf_dt_name (abfd);
1102 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1103 the ELF linker emulation code. */
1106 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1108 struct bfd_link_needed_list **pneeded;
1111 bfd_byte *dynbuf = NULL;
1114 bfd_byte *extdyn, *extdynend;
1116 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1120 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1121 || bfd_get_format (abfd) != bfd_object)
1124 s = bfd_get_section_by_name (abfd, ".dynamic");
1125 if (s == NULL || s->_raw_size == 0)
1128 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1132 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1136 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1140 link = elf_elfsections (abfd)[elfsec]->sh_link;
1142 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1143 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1146 extdynend = extdyn + s->_raw_size;
1147 for (; extdyn < extdynend; extdyn += extdynsize)
1149 Elf_Internal_Dyn dyn;
1151 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1153 if (dyn.d_tag == DT_NULL)
1156 if (dyn.d_tag == DT_NEEDED)
1159 struct bfd_link_needed_list *l;
1161 string = bfd_elf_string_from_elf_section (abfd, link,
1166 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1187 /* Allocate an ELF string table--force the first byte to be zero. */
1189 struct bfd_strtab_hash *
1190 _bfd_elf_stringtab_init ()
1192 struct bfd_strtab_hash *ret;
1194 ret = _bfd_stringtab_init ();
1199 loc = _bfd_stringtab_add (ret, "", true, false);
1200 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1201 if (loc == (bfd_size_type) -1)
1203 _bfd_stringtab_free (ret);
1210 /* ELF .o/exec file reading */
1212 /* Create a new bfd section from an ELF section header. */
1215 bfd_section_from_shdr (abfd, shindex)
1217 unsigned int shindex;
1219 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1220 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1221 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1224 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1226 switch (hdr->sh_type)
1229 /* Inactive section. Throw it away. */
1232 case SHT_PROGBITS: /* Normal section with contents. */
1233 case SHT_DYNAMIC: /* Dynamic linking information. */
1234 case SHT_NOBITS: /* .bss section. */
1235 case SHT_HASH: /* .hash section. */
1236 case SHT_NOTE: /* .note section. */
1237 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1239 case SHT_SYMTAB: /* A symbol table */
1240 if (elf_onesymtab (abfd) == shindex)
1243 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1244 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1245 elf_onesymtab (abfd) = shindex;
1246 elf_tdata (abfd)->symtab_hdr = *hdr;
1247 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1248 abfd->flags |= HAS_SYMS;
1250 /* Sometimes a shared object will map in the symbol table. If
1251 SHF_ALLOC is set, and this is a shared object, then we also
1252 treat this section as a BFD section. We can not base the
1253 decision purely on SHF_ALLOC, because that flag is sometimes
1254 set in a relocateable object file, which would confuse the
1256 if ((hdr->sh_flags & SHF_ALLOC) != 0
1257 && (abfd->flags & DYNAMIC) != 0
1258 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1263 case SHT_DYNSYM: /* A dynamic symbol table */
1264 if (elf_dynsymtab (abfd) == shindex)
1267 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1268 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1269 elf_dynsymtab (abfd) = shindex;
1270 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1271 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1272 abfd->flags |= HAS_SYMS;
1274 /* Besides being a symbol table, we also treat this as a regular
1275 section, so that objcopy can handle it. */
1276 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1278 case SHT_STRTAB: /* A string table */
1279 if (hdr->bfd_section != NULL)
1281 if (ehdr->e_shstrndx == shindex)
1283 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1284 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1290 for (i = 1; i < ehdr->e_shnum; i++)
1292 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1293 if (hdr2->sh_link == shindex)
1295 if (! bfd_section_from_shdr (abfd, i))
1297 if (elf_onesymtab (abfd) == i)
1299 elf_tdata (abfd)->strtab_hdr = *hdr;
1300 elf_elfsections (abfd)[shindex] =
1301 &elf_tdata (abfd)->strtab_hdr;
1304 if (elf_dynsymtab (abfd) == i)
1306 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1307 elf_elfsections (abfd)[shindex] = hdr =
1308 &elf_tdata (abfd)->dynstrtab_hdr;
1309 /* We also treat this as a regular section, so
1310 that objcopy can handle it. */
1313 #if 0 /* Not handling other string tables specially right now. */
1314 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1315 /* We have a strtab for some random other section. */
1316 newsect = (asection *) hdr2->bfd_section;
1319 hdr->bfd_section = newsect;
1320 hdr2 = &elf_section_data (newsect)->str_hdr;
1322 elf_elfsections (abfd)[shindex] = hdr2;
1328 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1332 /* *These* do a lot of work -- but build no sections! */
1334 asection *target_sect;
1335 Elf_Internal_Shdr *hdr2;
1337 /* Check for a bogus link to avoid crashing. */
1338 if (hdr->sh_link >= ehdr->e_shnum)
1340 ((*_bfd_error_handler)
1341 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1342 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1343 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1346 /* For some incomprehensible reason Oracle distributes
1347 libraries for Solaris in which some of the objects have
1348 bogus sh_link fields. It would be nice if we could just
1349 reject them, but, unfortunately, some people need to use
1350 them. We scan through the section headers; if we find only
1351 one suitable symbol table, we clobber the sh_link to point
1352 to it. I hope this doesn't break anything. */
1353 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1354 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1360 for (scan = 1; scan < ehdr->e_shnum; scan++)
1362 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1363 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1374 hdr->sh_link = found;
1377 /* Get the symbol table. */
1378 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1379 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1382 /* If this reloc section does not use the main symbol table we
1383 don't treat it as a reloc section. BFD can't adequately
1384 represent such a section, so at least for now, we don't
1385 try. We just present it as a normal section. We also
1386 can't use it as a reloc section if it points to the null
1388 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1389 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1391 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1393 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1394 if (target_sect == NULL)
1397 if ((target_sect->flags & SEC_RELOC) == 0
1398 || target_sect->reloc_count == 0)
1399 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1402 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1403 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1404 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1407 elf_elfsections (abfd)[shindex] = hdr2;
1408 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1409 target_sect->flags |= SEC_RELOC;
1410 target_sect->relocation = NULL;
1411 target_sect->rel_filepos = hdr->sh_offset;
1412 /* In the section to which the relocations apply, mark whether
1413 its relocations are of the REL or RELA variety. */
1414 if (hdr->sh_size != 0)
1415 elf_section_data (target_sect)->use_rela_p
1416 = (hdr->sh_type == SHT_RELA);
1417 abfd->flags |= HAS_RELOC;
1422 case SHT_GNU_verdef:
1423 elf_dynverdef (abfd) = shindex;
1424 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1425 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1428 case SHT_GNU_versym:
1429 elf_dynversym (abfd) = shindex;
1430 elf_tdata (abfd)->dynversym_hdr = *hdr;
1431 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1434 case SHT_GNU_verneed:
1435 elf_dynverref (abfd) = shindex;
1436 elf_tdata (abfd)->dynverref_hdr = *hdr;
1437 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1444 /* Check for any processor-specific section types. */
1446 if (bed->elf_backend_section_from_shdr)
1447 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1455 /* Given an ELF section number, retrieve the corresponding BFD
1459 bfd_section_from_elf_index (abfd, index)
1463 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1464 if (index >= elf_elfheader (abfd)->e_shnum)
1466 return elf_elfsections (abfd)[index]->bfd_section;
1470 _bfd_elf_new_section_hook (abfd, sec)
1474 struct bfd_elf_section_data *sdata;
1476 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1479 sec->used_by_bfd = (PTR) sdata;
1481 /* Indicate whether or not this section should use RELA relocations. */
1483 = get_elf_backend_data (abfd)->default_use_rela_p;
1488 /* Create a new bfd section from an ELF program header.
1490 Since program segments have no names, we generate a synthetic name
1491 of the form segment<NUM>, where NUM is generally the index in the
1492 program header table. For segments that are split (see below) we
1493 generate the names segment<NUM>a and segment<NUM>b.
1495 Note that some program segments may have a file size that is different than
1496 (less than) the memory size. All this means is that at execution the
1497 system must allocate the amount of memory specified by the memory size,
1498 but only initialize it with the first "file size" bytes read from the
1499 file. This would occur for example, with program segments consisting
1500 of combined data+bss.
1502 To handle the above situation, this routine generates TWO bfd sections
1503 for the single program segment. The first has the length specified by
1504 the file size of the segment, and the second has the length specified
1505 by the difference between the two sizes. In effect, the segment is split
1506 into it's initialized and uninitialized parts.
1511 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1513 Elf_Internal_Phdr *hdr;
1515 const char *typename;
1522 split = ((hdr->p_memsz > 0)
1523 && (hdr->p_filesz > 0)
1524 && (hdr->p_memsz > hdr->p_filesz));
1525 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1526 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1529 strcpy (name, namebuf);
1530 newsect = bfd_make_section (abfd, name);
1531 if (newsect == NULL)
1533 newsect->vma = hdr->p_vaddr;
1534 newsect->lma = hdr->p_paddr;
1535 newsect->_raw_size = hdr->p_filesz;
1536 newsect->filepos = hdr->p_offset;
1537 newsect->flags |= SEC_HAS_CONTENTS;
1538 if (hdr->p_type == PT_LOAD)
1540 newsect->flags |= SEC_ALLOC;
1541 newsect->flags |= SEC_LOAD;
1542 if (hdr->p_flags & PF_X)
1544 /* FIXME: all we known is that it has execute PERMISSION,
1546 newsect->flags |= SEC_CODE;
1549 if (!(hdr->p_flags & PF_W))
1551 newsect->flags |= SEC_READONLY;
1556 sprintf (namebuf, "%s%db", typename, index);
1557 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1560 strcpy (name, namebuf);
1561 newsect = bfd_make_section (abfd, name);
1562 if (newsect == NULL)
1564 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1565 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1566 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1567 if (hdr->p_type == PT_LOAD)
1569 newsect->flags |= SEC_ALLOC;
1570 if (hdr->p_flags & PF_X)
1571 newsect->flags |= SEC_CODE;
1573 if (!(hdr->p_flags & PF_W))
1574 newsect->flags |= SEC_READONLY;
1581 bfd_section_from_phdr (abfd, hdr, index)
1583 Elf_Internal_Phdr *hdr;
1586 struct elf_backend_data *bed;
1588 switch (hdr->p_type)
1591 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1594 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1597 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1600 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1603 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1605 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1610 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1613 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1616 /* Check for any processor-specific program segment types.
1617 If no handler for them, default to making "segment" sections. */
1618 bed = get_elf_backend_data (abfd);
1619 if (bed->elf_backend_section_from_phdr)
1620 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1622 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1626 /* Initialize REL_HDR, the section-header for new section, containing
1627 relocations against ASECT. If USE_RELA_P is true, we use RELA
1628 relocations; otherwise, we use REL relocations. */
1631 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1633 Elf_Internal_Shdr *rel_hdr;
1638 struct elf_backend_data *bed;
1640 bed = get_elf_backend_data (abfd);
1641 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1644 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1646 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1648 if (rel_hdr->sh_name == (unsigned int) -1)
1650 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1651 rel_hdr->sh_entsize = (use_rela_p
1652 ? bed->s->sizeof_rela
1653 : bed->s->sizeof_rel);
1654 rel_hdr->sh_addralign = bed->s->file_align;
1655 rel_hdr->sh_flags = 0;
1656 rel_hdr->sh_addr = 0;
1657 rel_hdr->sh_size = 0;
1658 rel_hdr->sh_offset = 0;
1663 /* Set up an ELF internal section header for a section. */
1667 elf_fake_sections (abfd, asect, failedptrarg)
1672 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1673 boolean *failedptr = (boolean *) failedptrarg;
1674 Elf_Internal_Shdr *this_hdr;
1678 /* We already failed; just get out of the bfd_map_over_sections
1683 this_hdr = &elf_section_data (asect)->this_hdr;
1685 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1688 if (this_hdr->sh_name == (unsigned long) -1)
1694 this_hdr->sh_flags = 0;
1696 if ((asect->flags & SEC_ALLOC) != 0
1697 || asect->user_set_vma)
1698 this_hdr->sh_addr = asect->vma;
1700 this_hdr->sh_addr = 0;
1702 this_hdr->sh_offset = 0;
1703 this_hdr->sh_size = asect->_raw_size;
1704 this_hdr->sh_link = 0;
1705 this_hdr->sh_addralign = 1 << asect->alignment_power;
1706 /* The sh_entsize and sh_info fields may have been set already by
1707 copy_private_section_data. */
1709 this_hdr->bfd_section = asect;
1710 this_hdr->contents = NULL;
1712 /* FIXME: This should not be based on section names. */
1713 if (strcmp (asect->name, ".dynstr") == 0)
1714 this_hdr->sh_type = SHT_STRTAB;
1715 else if (strcmp (asect->name, ".hash") == 0)
1717 this_hdr->sh_type = SHT_HASH;
1718 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1720 else if (strcmp (asect->name, ".dynsym") == 0)
1722 this_hdr->sh_type = SHT_DYNSYM;
1723 this_hdr->sh_entsize = bed->s->sizeof_sym;
1725 else if (strcmp (asect->name, ".dynamic") == 0)
1727 this_hdr->sh_type = SHT_DYNAMIC;
1728 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1730 else if (strncmp (asect->name, ".rela", 5) == 0
1731 && get_elf_backend_data (abfd)->may_use_rela_p)
1733 this_hdr->sh_type = SHT_RELA;
1734 this_hdr->sh_entsize = bed->s->sizeof_rela;
1736 else if (strncmp (asect->name, ".rel", 4) == 0
1737 && get_elf_backend_data (abfd)->may_use_rel_p)
1739 this_hdr->sh_type = SHT_REL;
1740 this_hdr->sh_entsize = bed->s->sizeof_rel;
1742 else if (strncmp (asect->name, ".note", 5) == 0)
1743 this_hdr->sh_type = SHT_NOTE;
1744 else if (strncmp (asect->name, ".stab", 5) == 0
1745 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1746 this_hdr->sh_type = SHT_STRTAB;
1747 else if (strcmp (asect->name, ".gnu.version") == 0)
1749 this_hdr->sh_type = SHT_GNU_versym;
1750 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1752 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1754 this_hdr->sh_type = SHT_GNU_verdef;
1755 this_hdr->sh_entsize = 0;
1756 /* objcopy or strip will copy over sh_info, but may not set
1757 cverdefs. The linker will set cverdefs, but sh_info will be
1759 if (this_hdr->sh_info == 0)
1760 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1762 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1763 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1765 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1767 this_hdr->sh_type = SHT_GNU_verneed;
1768 this_hdr->sh_entsize = 0;
1769 /* objcopy or strip will copy over sh_info, but may not set
1770 cverrefs. The linker will set cverrefs, but sh_info will be
1772 if (this_hdr->sh_info == 0)
1773 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1775 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1776 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1778 else if ((asect->flags & SEC_ALLOC) != 0
1779 && (asect->flags & SEC_LOAD) != 0)
1780 this_hdr->sh_type = SHT_PROGBITS;
1781 else if ((asect->flags & SEC_ALLOC) != 0
1782 && ((asect->flags & SEC_LOAD) == 0))
1783 this_hdr->sh_type = SHT_NOBITS;
1787 this_hdr->sh_type = SHT_PROGBITS;
1790 if ((asect->flags & SEC_ALLOC) != 0)
1791 this_hdr->sh_flags |= SHF_ALLOC;
1792 if ((asect->flags & SEC_READONLY) == 0)
1793 this_hdr->sh_flags |= SHF_WRITE;
1794 if ((asect->flags & SEC_CODE) != 0)
1795 this_hdr->sh_flags |= SHF_EXECINSTR;
1797 /* Check for processor-specific section types. */
1798 if (bed->elf_backend_fake_sections)
1799 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1801 /* If the section has relocs, set up a section header for the
1802 SHT_REL[A] section. If two relocation sections are required for
1803 this section, it is up to the processor-specific back-end to
1804 create the other. */
1805 if ((asect->flags & SEC_RELOC) != 0
1806 && !_bfd_elf_init_reloc_shdr (abfd,
1807 &elf_section_data (asect)->rel_hdr,
1809 elf_section_data (asect)->use_rela_p))
1813 /* Assign all ELF section numbers. The dummy first section is handled here
1814 too. The link/info pointers for the standard section types are filled
1815 in here too, while we're at it. */
1818 assign_section_numbers (abfd)
1821 struct elf_obj_tdata *t = elf_tdata (abfd);
1823 unsigned int section_number;
1824 Elf_Internal_Shdr **i_shdrp;
1828 for (sec = abfd->sections; sec; sec = sec->next)
1830 struct bfd_elf_section_data *d = elf_section_data (sec);
1832 d->this_idx = section_number++;
1833 if ((sec->flags & SEC_RELOC) == 0)
1836 d->rel_idx = section_number++;
1839 d->rel_idx2 = section_number++;
1844 t->shstrtab_section = section_number++;
1845 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1846 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1848 if (bfd_get_symcount (abfd) > 0)
1850 t->symtab_section = section_number++;
1851 t->strtab_section = section_number++;
1854 elf_elfheader (abfd)->e_shnum = section_number;
1856 /* Set up the list of section header pointers, in agreement with the
1858 i_shdrp = ((Elf_Internal_Shdr **)
1859 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1860 if (i_shdrp == NULL)
1863 i_shdrp[0] = ((Elf_Internal_Shdr *)
1864 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1865 if (i_shdrp[0] == NULL)
1867 bfd_release (abfd, i_shdrp);
1870 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1872 elf_elfsections (abfd) = i_shdrp;
1874 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1875 if (bfd_get_symcount (abfd) > 0)
1877 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1878 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1879 t->symtab_hdr.sh_link = t->strtab_section;
1881 for (sec = abfd->sections; sec; sec = sec->next)
1883 struct bfd_elf_section_data *d = elf_section_data (sec);
1887 i_shdrp[d->this_idx] = &d->this_hdr;
1888 if (d->rel_idx != 0)
1889 i_shdrp[d->rel_idx] = &d->rel_hdr;
1890 if (d->rel_idx2 != 0)
1891 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1893 /* Fill in the sh_link and sh_info fields while we're at it. */
1895 /* sh_link of a reloc section is the section index of the symbol
1896 table. sh_info is the section index of the section to which
1897 the relocation entries apply. */
1898 if (d->rel_idx != 0)
1900 d->rel_hdr.sh_link = t->symtab_section;
1901 d->rel_hdr.sh_info = d->this_idx;
1903 if (d->rel_idx2 != 0)
1905 d->rel_hdr2->sh_link = t->symtab_section;
1906 d->rel_hdr2->sh_info = d->this_idx;
1909 switch (d->this_hdr.sh_type)
1913 /* A reloc section which we are treating as a normal BFD
1914 section. sh_link is the section index of the symbol
1915 table. sh_info is the section index of the section to
1916 which the relocation entries apply. We assume that an
1917 allocated reloc section uses the dynamic symbol table.
1918 FIXME: How can we be sure? */
1919 s = bfd_get_section_by_name (abfd, ".dynsym");
1921 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1923 /* We look up the section the relocs apply to by name. */
1925 if (d->this_hdr.sh_type == SHT_REL)
1929 s = bfd_get_section_by_name (abfd, name);
1931 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1935 /* We assume that a section named .stab*str is a stabs
1936 string section. We look for a section with the same name
1937 but without the trailing ``str'', and set its sh_link
1938 field to point to this section. */
1939 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1940 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1945 len = strlen (sec->name);
1946 alc = (char *) bfd_malloc (len - 2);
1949 strncpy (alc, sec->name, len - 3);
1950 alc[len - 3] = '\0';
1951 s = bfd_get_section_by_name (abfd, alc);
1955 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1957 /* This is a .stab section. */
1958 elf_section_data (s)->this_hdr.sh_entsize =
1959 4 + 2 * bfd_get_arch_size (abfd) / 8;
1966 case SHT_GNU_verneed:
1967 case SHT_GNU_verdef:
1968 /* sh_link is the section header index of the string table
1969 used for the dynamic entries, or the symbol table, or the
1971 s = bfd_get_section_by_name (abfd, ".dynstr");
1973 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1977 case SHT_GNU_versym:
1978 /* sh_link is the section header index of the symbol table
1979 this hash table or version table is for. */
1980 s = bfd_get_section_by_name (abfd, ".dynsym");
1982 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1990 /* Map symbol from it's internal number to the external number, moving
1991 all local symbols to be at the head of the list. */
1994 sym_is_global (abfd, sym)
1998 /* If the backend has a special mapping, use it. */
1999 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2000 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2003 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2004 || bfd_is_und_section (bfd_get_section (sym))
2005 || bfd_is_com_section (bfd_get_section (sym)));
2009 elf_map_symbols (abfd)
2012 int symcount = bfd_get_symcount (abfd);
2013 asymbol **syms = bfd_get_outsymbols (abfd);
2014 asymbol **sect_syms;
2016 int num_globals = 0;
2017 int num_locals2 = 0;
2018 int num_globals2 = 0;
2020 int num_sections = 0;
2027 fprintf (stderr, "elf_map_symbols\n");
2031 /* Add a section symbol for each BFD section. FIXME: Is this really
2033 for (asect = abfd->sections; asect; asect = asect->next)
2035 if (max_index < asect->index)
2036 max_index = asect->index;
2040 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2041 if (sect_syms == NULL)
2043 elf_section_syms (abfd) = sect_syms;
2045 for (idx = 0; idx < symcount; idx++)
2049 if ((sym->flags & BSF_SECTION_SYM) != 0
2056 if (sec->owner != NULL)
2058 if (sec->owner != abfd)
2060 if (sec->output_offset != 0)
2063 sec = sec->output_section;
2065 /* Empty sections in the input files may have had a section
2066 symbol created for them. (See the comment near the end of
2067 _bfd_generic_link_output_symbols in linker.c). If the linker
2068 script discards such sections then we will reach this point.
2069 Since we know that we cannot avoid this case, we detect it
2070 and skip the abort and the assignment to the sect_syms array.
2071 To reproduce this particular case try running the linker
2072 testsuite test ld-scripts/weak.exp for an ELF port that uses
2073 the generic linker. */
2074 if (sec->owner == NULL)
2077 BFD_ASSERT (sec->owner == abfd);
2079 sect_syms[sec->index] = syms[idx];
2084 for (asect = abfd->sections; asect; asect = asect->next)
2086 if (sect_syms[asect->index] != NULL)
2089 sym = bfd_make_empty_symbol (abfd);
2092 sym->the_bfd = abfd;
2093 sym->name = asect->name;
2095 /* Set the flags to 0 to indicate that this one was newly added. */
2097 sym->section = asect;
2098 sect_syms[asect->index] = sym;
2102 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2103 asect->name, (long) asect->vma, asect->index, (long) asect);
2107 /* Classify all of the symbols. */
2108 for (idx = 0; idx < symcount; idx++)
2110 if (!sym_is_global (abfd, syms[idx]))
2115 for (asect = abfd->sections; asect; asect = asect->next)
2117 if (sect_syms[asect->index] != NULL
2118 && sect_syms[asect->index]->flags == 0)
2120 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2121 if (!sym_is_global (abfd, sect_syms[asect->index]))
2125 sect_syms[asect->index]->flags = 0;
2129 /* Now sort the symbols so the local symbols are first. */
2130 new_syms = ((asymbol **)
2132 (num_locals + num_globals) * sizeof (asymbol *)));
2133 if (new_syms == NULL)
2136 for (idx = 0; idx < symcount; idx++)
2138 asymbol *sym = syms[idx];
2141 if (!sym_is_global (abfd, sym))
2144 i = num_locals + num_globals2++;
2146 sym->udata.i = i + 1;
2148 for (asect = abfd->sections; asect; asect = asect->next)
2150 if (sect_syms[asect->index] != NULL
2151 && sect_syms[asect->index]->flags == 0)
2153 asymbol *sym = sect_syms[asect->index];
2156 sym->flags = BSF_SECTION_SYM;
2157 if (!sym_is_global (abfd, sym))
2160 i = num_locals + num_globals2++;
2162 sym->udata.i = i + 1;
2166 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2168 elf_num_locals (abfd) = num_locals;
2169 elf_num_globals (abfd) = num_globals;
2173 /* Align to the maximum file alignment that could be required for any
2174 ELF data structure. */
2176 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2177 static INLINE file_ptr
2178 align_file_position (off, align)
2182 return (off + align - 1) & ~(align - 1);
2185 /* Assign a file position to a section, optionally aligning to the
2186 required section alignment. */
2189 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2190 Elf_Internal_Shdr *i_shdrp;
2198 al = i_shdrp->sh_addralign;
2200 offset = BFD_ALIGN (offset, al);
2202 i_shdrp->sh_offset = offset;
2203 if (i_shdrp->bfd_section != NULL)
2204 i_shdrp->bfd_section->filepos = offset;
2205 if (i_shdrp->sh_type != SHT_NOBITS)
2206 offset += i_shdrp->sh_size;
2210 /* Compute the file positions we are going to put the sections at, and
2211 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2212 is not NULL, this is being called by the ELF backend linker. */
2215 _bfd_elf_compute_section_file_positions (abfd, link_info)
2217 struct bfd_link_info *link_info;
2219 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2221 struct bfd_strtab_hash *strtab;
2222 Elf_Internal_Shdr *shstrtab_hdr;
2224 if (abfd->output_has_begun)
2227 /* Do any elf backend specific processing first. */
2228 if (bed->elf_backend_begin_write_processing)
2229 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2231 if (! prep_headers (abfd))
2234 /* Post process the headers if necessary. */
2235 if (bed->elf_backend_post_process_headers)
2236 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2239 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2243 if (!assign_section_numbers (abfd))
2246 /* The backend linker builds symbol table information itself. */
2247 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2249 /* Non-zero if doing a relocatable link. */
2250 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2252 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2256 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2257 /* sh_name was set in prep_headers. */
2258 shstrtab_hdr->sh_type = SHT_STRTAB;
2259 shstrtab_hdr->sh_flags = 0;
2260 shstrtab_hdr->sh_addr = 0;
2261 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2262 shstrtab_hdr->sh_entsize = 0;
2263 shstrtab_hdr->sh_link = 0;
2264 shstrtab_hdr->sh_info = 0;
2265 /* sh_offset is set in assign_file_positions_except_relocs. */
2266 shstrtab_hdr->sh_addralign = 1;
2268 if (!assign_file_positions_except_relocs (abfd))
2271 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2274 Elf_Internal_Shdr *hdr;
2276 off = elf_tdata (abfd)->next_file_pos;
2278 hdr = &elf_tdata (abfd)->symtab_hdr;
2279 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2281 hdr = &elf_tdata (abfd)->strtab_hdr;
2282 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2284 elf_tdata (abfd)->next_file_pos = off;
2286 /* Now that we know where the .strtab section goes, write it
2288 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2289 || ! _bfd_stringtab_emit (abfd, strtab))
2291 _bfd_stringtab_free (strtab);
2294 abfd->output_has_begun = true;
2299 /* Create a mapping from a set of sections to a program segment. */
2301 static INLINE struct elf_segment_map *
2302 make_mapping (abfd, sections, from, to, phdr)
2304 asection **sections;
2309 struct elf_segment_map *m;
2313 m = ((struct elf_segment_map *)
2315 (sizeof (struct elf_segment_map)
2316 + (to - from - 1) * sizeof (asection *))));
2320 m->p_type = PT_LOAD;
2321 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2322 m->sections[i - from] = *hdrpp;
2323 m->count = to - from;
2325 if (from == 0 && phdr)
2327 /* Include the headers in the first PT_LOAD segment. */
2328 m->includes_filehdr = 1;
2329 m->includes_phdrs = 1;
2335 /* Set up a mapping from BFD sections to program segments. */
2338 map_sections_to_segments (abfd)
2341 asection **sections = NULL;
2345 struct elf_segment_map *mfirst;
2346 struct elf_segment_map **pm;
2347 struct elf_segment_map *m;
2349 unsigned int phdr_index;
2350 bfd_vma maxpagesize;
2352 boolean phdr_in_segment = true;
2356 if (elf_tdata (abfd)->segment_map != NULL)
2359 if (bfd_count_sections (abfd) == 0)
2362 /* Select the allocated sections, and sort them. */
2364 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2365 * sizeof (asection *));
2366 if (sections == NULL)
2370 for (s = abfd->sections; s != NULL; s = s->next)
2372 if ((s->flags & SEC_ALLOC) != 0)
2378 BFD_ASSERT (i <= bfd_count_sections (abfd));
2381 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2383 /* Build the mapping. */
2388 /* If we have a .interp section, then create a PT_PHDR segment for
2389 the program headers and a PT_INTERP segment for the .interp
2391 s = bfd_get_section_by_name (abfd, ".interp");
2392 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2394 m = ((struct elf_segment_map *)
2395 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2399 m->p_type = PT_PHDR;
2400 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2401 m->p_flags = PF_R | PF_X;
2402 m->p_flags_valid = 1;
2403 m->includes_phdrs = 1;
2408 m = ((struct elf_segment_map *)
2409 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2413 m->p_type = PT_INTERP;
2421 /* Look through the sections. We put sections in the same program
2422 segment when the start of the second section can be placed within
2423 a few bytes of the end of the first section. */
2426 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2428 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2430 && (dynsec->flags & SEC_LOAD) == 0)
2433 /* Deal with -Ttext or something similar such that the first section
2434 is not adjacent to the program headers. This is an
2435 approximation, since at this point we don't know exactly how many
2436 program headers we will need. */
2439 bfd_size_type phdr_size;
2441 phdr_size = elf_tdata (abfd)->program_header_size;
2443 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2444 if ((abfd->flags & D_PAGED) == 0
2445 || sections[0]->lma < phdr_size
2446 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2447 phdr_in_segment = false;
2450 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2453 boolean new_segment;
2457 /* See if this section and the last one will fit in the same
2460 if (last_hdr == NULL)
2462 /* If we don't have a segment yet, then we don't need a new
2463 one (we build the last one after this loop). */
2464 new_segment = false;
2466 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2468 /* If this section has a different relation between the
2469 virtual address and the load address, then we need a new
2473 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2474 < BFD_ALIGN (hdr->lma, maxpagesize))
2476 /* If putting this section in this segment would force us to
2477 skip a page in the segment, then we need a new segment. */
2480 else if ((last_hdr->flags & SEC_LOAD) == 0
2481 && (hdr->flags & SEC_LOAD) != 0)
2483 /* We don't want to put a loadable section after a
2484 nonloadable section in the same segment. */
2487 else if ((abfd->flags & D_PAGED) == 0)
2489 /* If the file is not demand paged, which means that we
2490 don't require the sections to be correctly aligned in the
2491 file, then there is no other reason for a new segment. */
2492 new_segment = false;
2495 && (hdr->flags & SEC_READONLY) == 0
2496 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2499 /* We don't want to put a writable section in a read only
2500 segment, unless they are on the same page in memory
2501 anyhow. We already know that the last section does not
2502 bring us past the current section on the page, so the
2503 only case in which the new section is not on the same
2504 page as the previous section is when the previous section
2505 ends precisely on a page boundary. */
2510 /* Otherwise, we can use the same segment. */
2511 new_segment = false;
2516 if ((hdr->flags & SEC_READONLY) == 0)
2522 /* We need a new program segment. We must create a new program
2523 header holding all the sections from phdr_index until hdr. */
2525 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2532 if ((hdr->flags & SEC_READONLY) == 0)
2539 phdr_in_segment = false;
2542 /* Create a final PT_LOAD program segment. */
2543 if (last_hdr != NULL)
2545 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2553 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2556 m = ((struct elf_segment_map *)
2557 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2561 m->p_type = PT_DYNAMIC;
2563 m->sections[0] = dynsec;
2569 /* For each loadable .note section, add a PT_NOTE segment. We don't
2570 use bfd_get_section_by_name, because if we link together
2571 nonloadable .note sections and loadable .note sections, we will
2572 generate two .note sections in the output file. FIXME: Using
2573 names for section types is bogus anyhow. */
2574 for (s = abfd->sections; s != NULL; s = s->next)
2576 if ((s->flags & SEC_LOAD) != 0
2577 && strncmp (s->name, ".note", 5) == 0)
2579 m = ((struct elf_segment_map *)
2580 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2584 m->p_type = PT_NOTE;
2596 elf_tdata (abfd)->segment_map = mfirst;
2600 if (sections != NULL)
2605 /* Sort sections by address. */
2608 elf_sort_sections (arg1, arg2)
2612 const asection *sec1 = *(const asection **) arg1;
2613 const asection *sec2 = *(const asection **) arg2;
2615 /* Sort by LMA first, since this is the address used to
2616 place the section into a segment. */
2617 if (sec1->lma < sec2->lma)
2619 else if (sec1->lma > sec2->lma)
2622 /* Then sort by VMA. Normally the LMA and the VMA will be
2623 the same, and this will do nothing. */
2624 if (sec1->vma < sec2->vma)
2626 else if (sec1->vma > sec2->vma)
2629 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2631 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2636 return sec1->target_index - sec2->target_index;
2646 /* Sort by size, to put zero sized sections before others at the
2649 if (sec1->_raw_size < sec2->_raw_size)
2651 if (sec1->_raw_size > sec2->_raw_size)
2654 return sec1->target_index - sec2->target_index;
2657 /* Assign file positions to the sections based on the mapping from
2658 sections to segments. This function also sets up some fields in
2659 the file header, and writes out the program headers. */
2662 assign_file_positions_for_segments (abfd)
2665 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2667 struct elf_segment_map *m;
2669 Elf_Internal_Phdr *phdrs;
2671 bfd_vma filehdr_vaddr, filehdr_paddr;
2672 bfd_vma phdrs_vaddr, phdrs_paddr;
2673 Elf_Internal_Phdr *p;
2675 if (elf_tdata (abfd)->segment_map == NULL)
2677 if (! map_sections_to_segments (abfd))
2681 if (bed->elf_backend_modify_segment_map)
2683 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2688 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2691 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2692 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2693 elf_elfheader (abfd)->e_phnum = count;
2698 /* If we already counted the number of program segments, make sure
2699 that we allocated enough space. This happens when SIZEOF_HEADERS
2700 is used in a linker script. */
2701 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2702 if (alloc != 0 && count > alloc)
2704 ((*_bfd_error_handler)
2705 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2706 bfd_get_filename (abfd), alloc, count));
2707 bfd_set_error (bfd_error_bad_value);
2714 phdrs = ((Elf_Internal_Phdr *)
2715 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2719 off = bed->s->sizeof_ehdr;
2720 off += alloc * bed->s->sizeof_phdr;
2727 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2734 /* If elf_segment_map is not from map_sections_to_segments, the
2735 sections may not be correctly ordered. */
2737 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2740 p->p_type = m->p_type;
2741 p->p_flags = m->p_flags;
2743 if (p->p_type == PT_LOAD
2745 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2747 if ((abfd->flags & D_PAGED) != 0)
2748 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2751 bfd_size_type align;
2754 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2756 bfd_size_type secalign;
2758 secalign = bfd_get_section_alignment (abfd, *secpp);
2759 if (secalign > align)
2763 off += (m->sections[0]->vma - off) % (1 << align);
2770 p->p_vaddr = m->sections[0]->vma;
2772 if (m->p_paddr_valid)
2773 p->p_paddr = m->p_paddr;
2774 else if (m->count == 0)
2777 p->p_paddr = m->sections[0]->lma;
2779 if (p->p_type == PT_LOAD
2780 && (abfd->flags & D_PAGED) != 0)
2781 p->p_align = bed->maxpagesize;
2782 else if (m->count == 0)
2783 p->p_align = bed->s->file_align;
2791 if (m->includes_filehdr)
2793 if (! m->p_flags_valid)
2796 p->p_filesz = bed->s->sizeof_ehdr;
2797 p->p_memsz = bed->s->sizeof_ehdr;
2800 BFD_ASSERT (p->p_type == PT_LOAD);
2802 if (p->p_vaddr < (bfd_vma) off)
2804 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2805 bfd_get_filename (abfd));
2806 bfd_set_error (bfd_error_bad_value);
2811 if (! m->p_paddr_valid)
2814 if (p->p_type == PT_LOAD)
2816 filehdr_vaddr = p->p_vaddr;
2817 filehdr_paddr = p->p_paddr;
2821 if (m->includes_phdrs)
2823 if (! m->p_flags_valid)
2826 if (m->includes_filehdr)
2828 if (p->p_type == PT_LOAD)
2830 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2831 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2836 p->p_offset = bed->s->sizeof_ehdr;
2840 BFD_ASSERT (p->p_type == PT_LOAD);
2841 p->p_vaddr -= off - p->p_offset;
2842 if (! m->p_paddr_valid)
2843 p->p_paddr -= off - p->p_offset;
2846 if (p->p_type == PT_LOAD)
2848 phdrs_vaddr = p->p_vaddr;
2849 phdrs_paddr = p->p_paddr;
2852 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2855 p->p_filesz += alloc * bed->s->sizeof_phdr;
2856 p->p_memsz += alloc * bed->s->sizeof_phdr;
2859 if (p->p_type == PT_LOAD
2860 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2862 if (! m->includes_filehdr && ! m->includes_phdrs)
2868 adjust = off - (p->p_offset + p->p_filesz);
2869 p->p_filesz += adjust;
2870 p->p_memsz += adjust;
2876 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2880 bfd_size_type align;
2884 align = 1 << bfd_get_section_alignment (abfd, sec);
2886 /* The section may have artificial alignment forced by a
2887 link script. Notice this case by the gap between the
2888 cumulative phdr vma and the section's vma. */
2889 if (p->p_vaddr + p->p_memsz < sec->vma)
2891 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2893 p->p_memsz += adjust;
2896 if ((flags & SEC_LOAD) != 0)
2897 p->p_filesz += adjust;
2900 if (p->p_type == PT_LOAD)
2902 bfd_signed_vma adjust;
2904 if ((flags & SEC_LOAD) != 0)
2906 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2910 else if ((flags & SEC_ALLOC) != 0)
2912 /* The section VMA must equal the file position
2913 modulo the page size. FIXME: I'm not sure if
2914 this adjustment is really necessary. We used to
2915 not have the SEC_LOAD case just above, and then
2916 this was necessary, but now I'm not sure. */
2917 if ((abfd->flags & D_PAGED) != 0)
2918 adjust = (sec->vma - voff) % bed->maxpagesize;
2920 adjust = (sec->vma - voff) % align;
2929 (* _bfd_error_handler)
2930 (_("Error: First section in segment (%s) starts at 0x%x"),
2931 bfd_section_name (abfd, sec), sec->lma);
2932 (* _bfd_error_handler)
2933 (_(" whereas segment starts at 0x%x"),
2938 p->p_memsz += adjust;
2941 if ((flags & SEC_LOAD) != 0)
2942 p->p_filesz += adjust;
2947 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2948 used in a linker script we may have a section with
2949 SEC_LOAD clear but which is supposed to have
2951 if ((flags & SEC_LOAD) != 0
2952 || (flags & SEC_HAS_CONTENTS) != 0)
2953 off += sec->_raw_size;
2955 if ((flags & SEC_ALLOC) != 0)
2956 voff += sec->_raw_size;
2959 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2961 /* The actual "note" segment has i == 0.
2962 This is the one that actually contains everything. */
2966 p->p_filesz = sec->_raw_size;
2967 off += sec->_raw_size;
2972 /* Fake sections -- don't need to be written. */
2975 flags = sec->flags = 0;
2982 p->p_memsz += sec->_raw_size;
2984 if ((flags & SEC_LOAD) != 0)
2985 p->p_filesz += sec->_raw_size;
2987 if (align > p->p_align
2988 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2992 if (! m->p_flags_valid)
2995 if ((flags & SEC_CODE) != 0)
2997 if ((flags & SEC_READONLY) == 0)
3003 /* Now that we have set the section file positions, we can set up
3004 the file positions for the non PT_LOAD segments. */
3005 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3009 if (p->p_type != PT_LOAD && m->count > 0)
3011 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3012 p->p_offset = m->sections[0]->filepos;
3016 if (m->includes_filehdr)
3018 p->p_vaddr = filehdr_vaddr;
3019 if (! m->p_paddr_valid)
3020 p->p_paddr = filehdr_paddr;
3022 else if (m->includes_phdrs)
3024 p->p_vaddr = phdrs_vaddr;
3025 if (! m->p_paddr_valid)
3026 p->p_paddr = phdrs_paddr;
3031 /* Clear out any program headers we allocated but did not use. */
3032 for (; count < alloc; count++, p++)
3034 memset (p, 0, sizeof *p);
3035 p->p_type = PT_NULL;
3038 elf_tdata (abfd)->phdr = phdrs;
3040 elf_tdata (abfd)->next_file_pos = off;
3042 /* Write out the program headers. */
3043 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3044 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3050 /* Get the size of the program header.
3052 If this is called by the linker before any of the section VMA's are set, it
3053 can't calculate the correct value for a strange memory layout. This only
3054 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3055 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3056 data segment (exclusive of .interp and .dynamic).
3058 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3059 will be two segments. */
3061 static bfd_size_type
3062 get_program_header_size (abfd)
3067 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3069 /* We can't return a different result each time we're called. */
3070 if (elf_tdata (abfd)->program_header_size != 0)
3071 return elf_tdata (abfd)->program_header_size;
3073 if (elf_tdata (abfd)->segment_map != NULL)
3075 struct elf_segment_map *m;
3078 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3080 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3081 return elf_tdata (abfd)->program_header_size;
3084 /* Assume we will need exactly two PT_LOAD segments: one for text
3085 and one for data. */
3088 s = bfd_get_section_by_name (abfd, ".interp");
3089 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3091 /* If we have a loadable interpreter section, we need a
3092 PT_INTERP segment. In this case, assume we also need a
3093 PT_PHDR segment, although that may not be true for all
3098 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3100 /* We need a PT_DYNAMIC segment. */
3104 for (s = abfd->sections; s != NULL; s = s->next)
3106 if ((s->flags & SEC_LOAD) != 0
3107 && strncmp (s->name, ".note", 5) == 0)
3109 /* We need a PT_NOTE segment. */
3114 /* Let the backend count up any program headers it might need. */
3115 if (bed->elf_backend_additional_program_headers)
3119 a = (*bed->elf_backend_additional_program_headers) (abfd);
3125 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3126 return elf_tdata (abfd)->program_header_size;
3129 /* Work out the file positions of all the sections. This is called by
3130 _bfd_elf_compute_section_file_positions. All the section sizes and
3131 VMAs must be known before this is called.
3133 We do not consider reloc sections at this point, unless they form
3134 part of the loadable image. Reloc sections are assigned file
3135 positions in assign_file_positions_for_relocs, which is called by
3136 write_object_contents and final_link.
3138 We also don't set the positions of the .symtab and .strtab here. */
3141 assign_file_positions_except_relocs (abfd)
3144 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3145 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3146 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3148 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3150 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3151 && bfd_get_format (abfd) != bfd_core)
3153 Elf_Internal_Shdr **hdrpp;
3156 /* Start after the ELF header. */
3157 off = i_ehdrp->e_ehsize;
3159 /* We are not creating an executable, which means that we are
3160 not creating a program header, and that the actual order of
3161 the sections in the file is unimportant. */
3162 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3164 Elf_Internal_Shdr *hdr;
3167 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3169 hdr->sh_offset = -1;
3172 if (i == tdata->symtab_section
3173 || i == tdata->strtab_section)
3175 hdr->sh_offset = -1;
3179 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3185 Elf_Internal_Shdr **hdrpp;
3187 /* Assign file positions for the loaded sections based on the
3188 assignment of sections to segments. */
3189 if (! assign_file_positions_for_segments (abfd))
3192 /* Assign file positions for the other sections. */
3194 off = elf_tdata (abfd)->next_file_pos;
3195 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3197 Elf_Internal_Shdr *hdr;
3200 if (hdr->bfd_section != NULL
3201 && hdr->bfd_section->filepos != 0)
3202 hdr->sh_offset = hdr->bfd_section->filepos;
3203 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3205 ((*_bfd_error_handler)
3206 (_("%s: warning: allocated section `%s' not in segment"),
3207 bfd_get_filename (abfd),
3208 (hdr->bfd_section == NULL
3210 : hdr->bfd_section->name)));
3211 if ((abfd->flags & D_PAGED) != 0)
3212 off += (hdr->sh_addr - off) % bed->maxpagesize;
3214 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3215 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3218 else if (hdr->sh_type == SHT_REL
3219 || hdr->sh_type == SHT_RELA
3220 || hdr == i_shdrpp[tdata->symtab_section]
3221 || hdr == i_shdrpp[tdata->strtab_section])
3222 hdr->sh_offset = -1;
3224 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3228 /* Place the section headers. */
3229 off = align_file_position (off, bed->s->file_align);
3230 i_ehdrp->e_shoff = off;
3231 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3233 elf_tdata (abfd)->next_file_pos = off;
3242 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3243 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3244 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3246 struct bfd_strtab_hash *shstrtab;
3247 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3249 i_ehdrp = elf_elfheader (abfd);
3250 i_shdrp = elf_elfsections (abfd);
3252 shstrtab = _bfd_elf_stringtab_init ();
3253 if (shstrtab == NULL)
3256 elf_shstrtab (abfd) = shstrtab;
3258 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3259 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3260 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3261 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3263 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3264 i_ehdrp->e_ident[EI_DATA] =
3265 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3266 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3268 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3269 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3271 for (count = EI_PAD; count < EI_NIDENT; count++)
3272 i_ehdrp->e_ident[count] = 0;
3274 if ((abfd->flags & DYNAMIC) != 0)
3275 i_ehdrp->e_type = ET_DYN;
3276 else if ((abfd->flags & EXEC_P) != 0)
3277 i_ehdrp->e_type = ET_EXEC;
3278 else if (bfd_get_format (abfd) == bfd_core)
3279 i_ehdrp->e_type = ET_CORE;
3281 i_ehdrp->e_type = ET_REL;
3283 switch (bfd_get_arch (abfd))
3285 case bfd_arch_unknown:
3286 i_ehdrp->e_machine = EM_NONE;
3288 case bfd_arch_sparc:
3289 if (bfd_get_arch_size (abfd) == 64)
3290 i_ehdrp->e_machine = EM_SPARCV9;
3292 i_ehdrp->e_machine = EM_SPARC;
3295 i_ehdrp->e_machine = EM_S370;
3298 i_ehdrp->e_machine = EM_386;
3301 i_ehdrp->e_machine = EM_IA_64;
3303 case bfd_arch_m68hc11:
3304 i_ehdrp->e_machine = EM_68HC11;
3306 case bfd_arch_m68hc12:
3307 i_ehdrp->e_machine = EM_68HC12;
3310 i_ehdrp->e_machine = EM_68K;
3313 i_ehdrp->e_machine = EM_88K;
3316 i_ehdrp->e_machine = EM_860;
3319 i_ehdrp->e_machine = EM_960;
3321 case bfd_arch_mips: /* MIPS Rxxxx */
3322 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3325 i_ehdrp->e_machine = EM_PARISC;
3327 case bfd_arch_powerpc:
3328 i_ehdrp->e_machine = EM_PPC;
3330 case bfd_arch_alpha:
3331 i_ehdrp->e_machine = EM_ALPHA;
3334 i_ehdrp->e_machine = EM_SH;
3337 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3340 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3343 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3345 case bfd_arch_mcore:
3346 i_ehdrp->e_machine = EM_MCORE;
3349 i_ehdrp->e_machine = EM_AVR;
3352 switch (bfd_get_mach (abfd))
3355 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3359 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3362 i_ehdrp->e_machine = EM_ARM;
3365 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3367 case bfd_arch_mn10200:
3368 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3370 case bfd_arch_mn10300:
3371 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3374 i_ehdrp->e_machine = EM_PJ;
3377 i_ehdrp->e_machine = EM_CRIS;
3379 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3381 i_ehdrp->e_machine = EM_NONE;
3383 i_ehdrp->e_version = bed->s->ev_current;
3384 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3386 /* no program header, for now. */
3387 i_ehdrp->e_phoff = 0;
3388 i_ehdrp->e_phentsize = 0;
3389 i_ehdrp->e_phnum = 0;
3391 /* each bfd section is section header entry */
3392 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3393 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3395 /* if we're building an executable, we'll need a program header table */
3396 if (abfd->flags & EXEC_P)
3398 /* it all happens later */
3400 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3402 /* elf_build_phdrs() returns a (NULL-terminated) array of
3403 Elf_Internal_Phdrs */
3404 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3405 i_ehdrp->e_phoff = outbase;
3406 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3411 i_ehdrp->e_phentsize = 0;
3413 i_ehdrp->e_phoff = 0;
3416 elf_tdata (abfd)->symtab_hdr.sh_name =
3417 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3418 elf_tdata (abfd)->strtab_hdr.sh_name =
3419 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3420 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3421 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3422 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3423 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3424 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3430 /* Assign file positions for all the reloc sections which are not part
3431 of the loadable file image. */
3434 _bfd_elf_assign_file_positions_for_relocs (abfd)
3439 Elf_Internal_Shdr **shdrpp;
3441 off = elf_tdata (abfd)->next_file_pos;
3443 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3444 i < elf_elfheader (abfd)->e_shnum;
3447 Elf_Internal_Shdr *shdrp;
3450 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3451 && shdrp->sh_offset == -1)
3452 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3455 elf_tdata (abfd)->next_file_pos = off;
3459 _bfd_elf_write_object_contents (abfd)
3462 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3463 Elf_Internal_Ehdr *i_ehdrp;
3464 Elf_Internal_Shdr **i_shdrp;
3468 if (! abfd->output_has_begun
3469 && ! _bfd_elf_compute_section_file_positions
3470 (abfd, (struct bfd_link_info *) NULL))
3473 i_shdrp = elf_elfsections (abfd);
3474 i_ehdrp = elf_elfheader (abfd);
3477 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3481 _bfd_elf_assign_file_positions_for_relocs (abfd);
3483 /* After writing the headers, we need to write the sections too... */
3484 for (count = 1; count < i_ehdrp->e_shnum; count++)
3486 if (bed->elf_backend_section_processing)
3487 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3488 if (i_shdrp[count]->contents)
3490 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3491 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3493 != i_shdrp[count]->sh_size))
3498 /* Write out the section header names. */
3499 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3500 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3503 if (bed->elf_backend_final_write_processing)
3504 (*bed->elf_backend_final_write_processing) (abfd,
3505 elf_tdata (abfd)->linker);
3507 return bed->s->write_shdrs_and_ehdr (abfd);
3511 _bfd_elf_write_corefile_contents (abfd)
3514 /* Hopefully this can be done just like an object file. */
3515 return _bfd_elf_write_object_contents (abfd);
3517 /* given a section, search the header to find them... */
3519 _bfd_elf_section_from_bfd_section (abfd, asect)
3523 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3524 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3526 Elf_Internal_Shdr *hdr;
3527 int maxindex = elf_elfheader (abfd)->e_shnum;
3529 for (index = 0; index < maxindex; index++)
3531 hdr = i_shdrp[index];
3532 if (hdr->bfd_section == asect)
3536 if (bed->elf_backend_section_from_bfd_section)
3538 for (index = 0; index < maxindex; index++)
3542 hdr = i_shdrp[index];
3544 if ((*bed->elf_backend_section_from_bfd_section)
3545 (abfd, hdr, asect, &retval))
3550 if (bfd_is_abs_section (asect))
3552 if (bfd_is_com_section (asect))
3554 if (bfd_is_und_section (asect))
3557 bfd_set_error (bfd_error_nonrepresentable_section);
3562 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3566 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3568 asymbol **asym_ptr_ptr;
3570 asymbol *asym_ptr = *asym_ptr_ptr;
3572 flagword flags = asym_ptr->flags;
3574 /* When gas creates relocations against local labels, it creates its
3575 own symbol for the section, but does put the symbol into the
3576 symbol chain, so udata is 0. When the linker is generating
3577 relocatable output, this section symbol may be for one of the
3578 input sections rather than the output section. */
3579 if (asym_ptr->udata.i == 0
3580 && (flags & BSF_SECTION_SYM)
3581 && asym_ptr->section)
3585 if (asym_ptr->section->output_section != NULL)
3586 indx = asym_ptr->section->output_section->index;
3588 indx = asym_ptr->section->index;
3589 if (elf_section_syms (abfd)[indx])
3590 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3593 idx = asym_ptr->udata.i;
3597 /* This case can occur when using --strip-symbol on a symbol
3598 which is used in a relocation entry. */
3599 (*_bfd_error_handler)
3600 (_("%s: symbol `%s' required but not present"),
3601 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3602 bfd_set_error (bfd_error_no_symbols);
3609 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3610 (long) asym_ptr, asym_ptr->name, idx, flags,
3611 elf_symbol_flags (flags));
3619 /* Copy private BFD data. This copies any program header information. */
3622 copy_private_bfd_data (ibfd, obfd)
3626 Elf_Internal_Ehdr * iehdr;
3627 struct elf_segment_map * map;
3628 struct elf_segment_map * map_first;
3629 struct elf_segment_map ** pointer_to_map;
3630 Elf_Internal_Phdr * segment;
3633 unsigned int num_segments;
3634 boolean phdr_included = false;
3635 bfd_vma maxpagesize;
3636 struct elf_segment_map * phdr_adjust_seg = NULL;
3637 unsigned int phdr_adjust_num = 0;
3639 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3640 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3643 if (elf_tdata (ibfd)->phdr == NULL)
3646 iehdr = elf_elfheader (ibfd);
3649 pointer_to_map = & map_first;
3651 num_segments = elf_elfheader (ibfd)->e_phnum;
3652 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3654 /* Returns the end address of the segment + 1. */
3655 #define SEGMENT_END(segment, start) \
3656 (start + (segment->p_memsz > segment->p_filesz \
3657 ? segment->p_memsz : segment->p_filesz))
3659 /* Returns true if the given section is contained within
3660 the given segment. VMA addresses are compared. */
3661 #define IS_CONTAINED_BY_VMA(section, segment) \
3662 (section->vma >= segment->p_vaddr \
3663 && (section->vma + section->_raw_size) \
3664 <= (SEGMENT_END (segment, segment->p_vaddr)))
3666 /* Returns true if the given section is contained within
3667 the given segment. LMA addresses are compared. */
3668 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3669 (section->lma >= base \
3670 && (section->lma + section->_raw_size) \
3671 <= SEGMENT_END (segment, base))
3673 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3674 #define IS_COREFILE_NOTE(p, s) \
3675 (p->p_type == PT_NOTE \
3676 && bfd_get_format (ibfd) == bfd_core \
3677 && s->vma == 0 && s->lma == 0 \
3678 && (bfd_vma) s->filepos >= p->p_offset \
3679 && (bfd_vma) s->filepos + s->_raw_size \
3680 <= p->p_offset + p->p_filesz)
3682 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3683 linker, which generates a PT_INTERP section with p_vaddr and
3684 p_memsz set to 0. */
3685 #define IS_SOLARIS_PT_INTERP(p, s) \
3687 && p->p_filesz > 0 \
3688 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3689 && s->_raw_size > 0 \
3690 && (bfd_vma) s->filepos >= p->p_offset \
3691 && ((bfd_vma) s->filepos + s->_raw_size \
3692 <= p->p_offset + p->p_filesz))
3694 /* Decide if the given section should be included in the given segment.
3695 A section will be included if:
3696 1. It is within the address space of the segment,
3697 2. It is an allocated segment,
3698 3. There is an output section associated with it,
3699 4. The section has not already been allocated to a previous segment. */
3700 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3701 ((((IS_CONTAINED_BY_VMA (section, segment) \
3702 || IS_SOLARIS_PT_INTERP (segment, section)) \
3703 && (section->flags & SEC_ALLOC) != 0) \
3704 || IS_COREFILE_NOTE (segment, section)) \
3705 && section->output_section != NULL \
3706 && section->segment_mark == false)
3708 /* Returns true iff seg1 starts after the end of seg2. */
3709 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3710 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3712 /* Returns true iff seg1 and seg2 overlap. */
3713 #define SEGMENT_OVERLAPS(seg1, seg2) \
3714 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3716 /* Initialise the segment mark field. */
3717 for (section = ibfd->sections; section != NULL; section = section->next)
3718 section->segment_mark = false;
3720 /* Scan through the segments specified in the program header
3721 of the input BFD. For this first scan we look for overlaps
3722 in the loadable segments. These can be created by wierd
3723 parameters to objcopy. */
3724 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3729 Elf_Internal_Phdr * segment2;
3731 if (segment->p_type != PT_LOAD)
3734 /* Determine if this segment overlaps any previous segments. */
3735 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j ++, segment2 ++)
3737 bfd_signed_vma extra_length;
3739 if (segment2->p_type != PT_LOAD
3740 || ! SEGMENT_OVERLAPS (segment, segment2))
3743 /* Merge the two segments together. */
3744 if (segment2->p_vaddr < segment->p_vaddr)
3746 /* Extend SEGMENT2 to include SEGMENT and then delete SEGMENT. */
3748 SEGMENT_END (segment, segment->p_vaddr)
3749 - SEGMENT_END (segment2, segment2->p_vaddr);
3751 if (extra_length > 0)
3753 segment2->p_memsz += extra_length;
3754 segment2->p_filesz += extra_length;
3757 segment->p_type = PT_NULL;
3759 /* Since we have deleted P we must restart the outer loop. */
3761 segment = elf_tdata (ibfd)->phdr;
3766 /* Extend SEGMENT to include SEGMENT2 and then delete SEGMENT2. */
3768 SEGMENT_END (segment2, segment2->p_vaddr)
3769 - SEGMENT_END (segment, segment->p_vaddr);
3771 if (extra_length > 0)
3773 segment->p_memsz += extra_length;
3774 segment->p_filesz += extra_length;
3777 segment2->p_type = PT_NULL;
3782 /* The second scan attempts to assign sections to segments. */
3783 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3787 unsigned int section_count;
3788 asection ** sections;
3789 asection * output_section;
3791 bfd_vma matching_lma;
3792 bfd_vma suggested_lma;
3795 if (segment->p_type == PT_NULL)
3798 /* Compute how many sections might be placed into this segment. */
3800 for (section = ibfd->sections; section != NULL; section = section->next)
3801 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3804 /* Allocate a segment map big enough to contain all of the
3805 sections we have selected. */
3806 map = ((struct elf_segment_map *)
3808 (sizeof (struct elf_segment_map)
3809 + ((size_t) section_count - 1) * sizeof (asection *))));
3813 /* Initialise the fields of the segment map. Default to
3814 using the physical address of the segment in the input BFD. */
3816 map->p_type = segment->p_type;
3817 map->p_flags = segment->p_flags;
3818 map->p_flags_valid = 1;
3819 map->p_paddr = segment->p_paddr;
3820 map->p_paddr_valid = 1;
3822 /* Determine if this segment contains the ELF file header
3823 and if it contains the program headers themselves. */
3824 map->includes_filehdr = (segment->p_offset == 0
3825 && segment->p_filesz >= iehdr->e_ehsize);
3827 map->includes_phdrs = 0;
3829 if (! phdr_included || segment->p_type != PT_LOAD)
3831 map->includes_phdrs =
3832 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3833 && (segment->p_offset + segment->p_filesz
3834 >= ((bfd_vma) iehdr->e_phoff
3835 + iehdr->e_phnum * iehdr->e_phentsize)));
3837 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3838 phdr_included = true;
3841 if (section_count == 0)
3843 /* Special segments, such as the PT_PHDR segment, may contain
3844 no sections, but ordinary, loadable segments should contain
3846 if (segment->p_type == PT_LOAD)
3848 (_("%s: warning: Empty loadable segment detected\n"),
3849 bfd_get_filename (ibfd));
3852 * pointer_to_map = map;
3853 pointer_to_map = & map->next;
3858 /* Now scan the sections in the input BFD again and attempt
3859 to add their corresponding output sections to the segment map.
3860 The problem here is how to handle an output section which has
3861 been moved (ie had its LMA changed). There are four possibilities:
3863 1. None of the sections have been moved.
3864 In this case we can continue to use the segment LMA from the
3867 2. All of the sections have been moved by the same amount.
3868 In this case we can change the segment's LMA to match the LMA
3869 of the first section.
3871 3. Some of the sections have been moved, others have not.
3872 In this case those sections which have not been moved can be
3873 placed in the current segment which will have to have its size,
3874 and possibly its LMA changed, and a new segment or segments will
3875 have to be created to contain the other sections.
3877 4. The sections have been moved, but not be the same amount.
3878 In this case we can change the segment's LMA to match the LMA
3879 of the first section and we will have to create a new segment
3880 or segments to contain the other sections.
3882 In order to save time, we allocate an array to hold the section
3883 pointers that we are interested in. As these sections get assigned
3884 to a segment, they are removed from this array. */
3886 sections = (asection **) bfd_malloc
3887 (sizeof (asection *) * section_count);
3888 if (sections == NULL)
3891 /* Step One: Scan for segment vs section LMA conflicts.
3892 Also add the sections to the section array allocated above.
3893 Also add the sections to the current segment. In the common
3894 case, where the sections have not been moved, this means that
3895 we have completely filled the segment, and there is nothing
3901 for (j = 0, section = ibfd->sections;
3903 section = section->next)
3905 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3907 output_section = section->output_section;
3909 sections[j ++] = section;
3911 /* The Solaris native linker always sets p_paddr to 0.
3912 We try to catch that case here, and set it to the
3914 if (segment->p_paddr == 0
3915 && segment->p_vaddr != 0
3917 && output_section->lma != 0
3918 && (output_section->vma == (segment->p_vaddr
3919 + (map->includes_filehdr
3922 + (map->includes_phdrs
3923 ? iehdr->e_phnum * iehdr->e_phentsize
3925 map->p_paddr = segment->p_vaddr;
3927 /* Match up the physical address of the segment with the
3928 LMA address of the output section. */
3929 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3930 || IS_COREFILE_NOTE (segment, section))
3932 if (matching_lma == 0)
3933 matching_lma = output_section->lma;
3935 /* We assume that if the section fits within the segment
3936 then it does not overlap any other section within that
3938 map->sections[isec ++] = output_section;
3940 else if (suggested_lma == 0)
3941 suggested_lma = output_section->lma;
3945 BFD_ASSERT (j == section_count);
3947 /* Step Two: Adjust the physical address of the current segment,
3949 if (isec == section_count)
3951 /* All of the sections fitted within the segment as currently
3952 specified. This is the default case. Add the segment to
3953 the list of built segments and carry on to process the next
3954 program header in the input BFD. */
3955 map->count = section_count;
3956 * pointer_to_map = map;
3957 pointer_to_map = & map->next;
3964 if (matching_lma != 0)
3966 /* At least one section fits inside the current segment.
3967 Keep it, but modify its physical address to match the
3968 LMA of the first section that fitted. */
3969 map->p_paddr = matching_lma;
3973 /* None of the sections fitted inside the current segment.
3974 Change the current segment's physical address to match
3975 the LMA of the first section. */
3976 map->p_paddr = suggested_lma;
3979 /* Offset the segment physical address from the lma
3980 to allow for space taken up by elf headers. */
3981 if (map->includes_filehdr)
3982 map->p_paddr -= iehdr->e_ehsize;
3984 if (map->includes_phdrs)
3986 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
3988 /* iehdr->e_phnum is just an estimate of the number
3989 of program headers that we will need. Make a note
3990 here of the number we used and the segment we chose
3991 to hold these headers, so that we can adjust the
3992 offset when we know the correct value. */
3993 phdr_adjust_num = iehdr->e_phnum;
3994 phdr_adjust_seg = map;
3998 /* Step Three: Loop over the sections again, this time assigning
3999 those that fit to the current segment and remvoing them from the
4000 sections array; but making sure not to leave large gaps. Once all
4001 possible sections have been assigned to the current segment it is
4002 added to the list of built segments and if sections still remain
4003 to be assigned, a new segment is constructed before repeating
4011 /* Fill the current segment with sections that fit. */
4012 for (j = 0; j < section_count; j++)
4014 section = sections[j];
4016 if (section == NULL)
4019 output_section = section->output_section;
4021 BFD_ASSERT (output_section != NULL);
4023 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4024 || IS_COREFILE_NOTE (segment, section))
4026 if (map->count == 0)
4028 /* If the first section in a segment does not start at
4029 the beginning of the segment, then something is
4031 if (output_section->lma !=
4033 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4034 + (map->includes_phdrs
4035 ? iehdr->e_phnum * iehdr->e_phentsize
4041 asection * prev_sec;
4043 prev_sec = map->sections[map->count - 1];
4045 /* If the gap between the end of the previous section
4046 and the start of this section is more than
4047 maxpagesize then we need to start a new segment. */
4048 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4049 < BFD_ALIGN (output_section->lma, maxpagesize))
4050 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4052 if (suggested_lma == 0)
4053 suggested_lma = output_section->lma;
4059 map->sections[map->count++] = output_section;
4062 section->segment_mark = true;
4064 else if (suggested_lma == 0)
4065 suggested_lma = output_section->lma;
4068 BFD_ASSERT (map->count > 0);
4070 /* Add the current segment to the list of built segments. */
4071 * pointer_to_map = map;
4072 pointer_to_map = & map->next;
4074 if (isec < section_count)
4076 /* We still have not allocated all of the sections to
4077 segments. Create a new segment here, initialise it
4078 and carry on looping. */
4079 map = ((struct elf_segment_map *)
4081 (sizeof (struct elf_segment_map)
4082 + ((size_t) section_count - 1)
4083 * sizeof (asection *))));
4087 /* Initialise the fields of the segment map. Set the physical
4088 physical address to the LMA of the first section that has
4089 not yet been assigned. */
4091 map->p_type = segment->p_type;
4092 map->p_flags = segment->p_flags;
4093 map->p_flags_valid = 1;
4094 map->p_paddr = suggested_lma;
4095 map->p_paddr_valid = 1;
4096 map->includes_filehdr = 0;
4097 map->includes_phdrs = 0;
4100 while (isec < section_count);
4105 /* The Solaris linker creates program headers in which all the
4106 p_paddr fields are zero. When we try to objcopy or strip such a
4107 file, we get confused. Check for this case, and if we find it
4108 reset the p_paddr_valid fields. */
4109 for (map = map_first; map != NULL; map = map->next)
4110 if (map->p_paddr != 0)
4114 for (map = map_first; map != NULL; map = map->next)
4115 map->p_paddr_valid = 0;
4118 elf_tdata (obfd)->segment_map = map_first;
4120 /* If we had to estimate the number of program headers that were
4121 going to be needed, then check our estimate know and adjust
4122 the offset if necessary. */
4123 if (phdr_adjust_seg != NULL)
4127 for (count = 0, map = map_first; map != NULL; map = map->next)
4130 if (count > phdr_adjust_num)
4131 phdr_adjust_seg->p_paddr
4132 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4136 /* Final Step: Sort the segments into ascending order of physical address. */
4137 if (map_first != NULL)
4139 struct elf_segment_map * prev;
4142 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4144 /* Yes I know - its a bubble sort.... */
4145 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4147 /* Swap map and map->next. */
4148 prev->next = map->next;
4149 map->next = map->next->next;
4150 prev->next->next = map;
4160 #undef IS_CONTAINED_BY_VMA
4161 #undef IS_CONTAINED_BY_LMA
4162 #undef IS_COREFILE_NOTE
4163 #undef IS_SOLARIS_PT_INTERP
4164 #undef INCLUDE_SECTION_IN_SEGMENT
4165 #undef SEGMENT_AFTER_SEGMENT
4166 #undef SEGMENT_OVERLAPS
4170 /* Copy private section information. This copies over the entsize
4171 field, and sometimes the info field. */
4174 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4180 Elf_Internal_Shdr *ihdr, *ohdr;
4182 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4183 || obfd->xvec->flavour != bfd_target_elf_flavour)
4186 /* Copy over private BFD data if it has not already been copied.
4187 This must be done here, rather than in the copy_private_bfd_data
4188 entry point, because the latter is called after the section
4189 contents have been set, which means that the program headers have
4190 already been worked out. */
4191 if (elf_tdata (obfd)->segment_map == NULL
4192 && elf_tdata (ibfd)->phdr != NULL)
4196 /* Only set up the segments if there are no more SEC_ALLOC
4197 sections. FIXME: This won't do the right thing if objcopy is
4198 used to remove the last SEC_ALLOC section, since objcopy
4199 won't call this routine in that case. */
4200 for (s = isec->next; s != NULL; s = s->next)
4201 if ((s->flags & SEC_ALLOC) != 0)
4205 if (! copy_private_bfd_data (ibfd, obfd))
4210 ihdr = &elf_section_data (isec)->this_hdr;
4211 ohdr = &elf_section_data (osec)->this_hdr;
4213 ohdr->sh_entsize = ihdr->sh_entsize;
4215 if (ihdr->sh_type == SHT_SYMTAB
4216 || ihdr->sh_type == SHT_DYNSYM
4217 || ihdr->sh_type == SHT_GNU_verneed
4218 || ihdr->sh_type == SHT_GNU_verdef)
4219 ohdr->sh_info = ihdr->sh_info;
4221 elf_section_data (osec)->use_rela_p
4222 = elf_section_data (isec)->use_rela_p;
4227 /* Copy private symbol information. If this symbol is in a section
4228 which we did not map into a BFD section, try to map the section
4229 index correctly. We use special macro definitions for the mapped
4230 section indices; these definitions are interpreted by the
4231 swap_out_syms function. */
4233 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4234 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4235 #define MAP_STRTAB (SHN_LORESERVE - 3)
4236 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4239 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4245 elf_symbol_type *isym, *osym;
4247 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4248 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4251 isym = elf_symbol_from (ibfd, isymarg);
4252 osym = elf_symbol_from (obfd, osymarg);
4256 && bfd_is_abs_section (isym->symbol.section))
4260 shndx = isym->internal_elf_sym.st_shndx;
4261 if (shndx == elf_onesymtab (ibfd))
4262 shndx = MAP_ONESYMTAB;
4263 else if (shndx == elf_dynsymtab (ibfd))
4264 shndx = MAP_DYNSYMTAB;
4265 else if (shndx == elf_tdata (ibfd)->strtab_section)
4267 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4268 shndx = MAP_SHSTRTAB;
4269 osym->internal_elf_sym.st_shndx = shndx;
4275 /* Swap out the symbols. */
4278 swap_out_syms (abfd, sttp, relocatable_p)
4280 struct bfd_strtab_hash **sttp;
4283 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4285 if (!elf_map_symbols (abfd))
4288 /* Dump out the symtabs. */
4290 int symcount = bfd_get_symcount (abfd);
4291 asymbol **syms = bfd_get_outsymbols (abfd);
4292 struct bfd_strtab_hash *stt;
4293 Elf_Internal_Shdr *symtab_hdr;
4294 Elf_Internal_Shdr *symstrtab_hdr;
4295 char *outbound_syms;
4298 stt = _bfd_elf_stringtab_init ();
4302 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4303 symtab_hdr->sh_type = SHT_SYMTAB;
4304 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4305 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4306 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4307 symtab_hdr->sh_addralign = bed->s->file_align;
4309 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4310 symstrtab_hdr->sh_type = SHT_STRTAB;
4312 outbound_syms = bfd_alloc (abfd,
4313 (1 + symcount) * bed->s->sizeof_sym);
4314 if (outbound_syms == NULL)
4316 symtab_hdr->contents = (PTR) outbound_syms;
4318 /* now generate the data (for "contents") */
4320 /* Fill in zeroth symbol and swap it out. */
4321 Elf_Internal_Sym sym;
4327 sym.st_shndx = SHN_UNDEF;
4328 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4329 outbound_syms += bed->s->sizeof_sym;
4331 for (idx = 0; idx < symcount; idx++)
4333 Elf_Internal_Sym sym;
4334 bfd_vma value = syms[idx]->value;
4335 elf_symbol_type *type_ptr;
4336 flagword flags = syms[idx]->flags;
4339 if (flags & BSF_SECTION_SYM)
4340 /* Section symbols have no names. */
4344 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4347 if (sym.st_name == (unsigned long) -1)
4351 type_ptr = elf_symbol_from (abfd, syms[idx]);
4353 if ((flags & BSF_SECTION_SYM) == 0
4354 && bfd_is_com_section (syms[idx]->section))
4356 /* ELF common symbols put the alignment into the `value' field,
4357 and the size into the `size' field. This is backwards from
4358 how BFD handles it, so reverse it here. */
4359 sym.st_size = value;
4360 if (type_ptr == NULL
4361 || type_ptr->internal_elf_sym.st_value == 0)
4362 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4364 sym.st_value = type_ptr->internal_elf_sym.st_value;
4365 sym.st_shndx = _bfd_elf_section_from_bfd_section
4366 (abfd, syms[idx]->section);
4370 asection *sec = syms[idx]->section;
4373 if (sec->output_section)
4375 value += sec->output_offset;
4376 sec = sec->output_section;
4378 /* Don't add in the section vma for relocatable output. */
4379 if (! relocatable_p)
4381 sym.st_value = value;
4382 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4384 if (bfd_is_abs_section (sec)
4386 && type_ptr->internal_elf_sym.st_shndx != 0)
4388 /* This symbol is in a real ELF section which we did
4389 not create as a BFD section. Undo the mapping done
4390 by copy_private_symbol_data. */
4391 shndx = type_ptr->internal_elf_sym.st_shndx;
4395 shndx = elf_onesymtab (abfd);
4398 shndx = elf_dynsymtab (abfd);
4401 shndx = elf_tdata (abfd)->strtab_section;
4404 shndx = elf_tdata (abfd)->shstrtab_section;
4412 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4418 /* Writing this would be a hell of a lot easier if
4419 we had some decent documentation on bfd, and
4420 knew what to expect of the library, and what to
4421 demand of applications. For example, it
4422 appears that `objcopy' might not set the
4423 section of a symbol to be a section that is
4424 actually in the output file. */
4425 sec2 = bfd_get_section_by_name (abfd, sec->name);
4426 BFD_ASSERT (sec2 != 0);
4427 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4428 BFD_ASSERT (shndx != -1);
4432 sym.st_shndx = shndx;
4435 if ((flags & BSF_FUNCTION) != 0)
4437 else if ((flags & BSF_OBJECT) != 0)
4442 /* Processor-specific types */
4443 if (type_ptr != NULL
4444 && bed->elf_backend_get_symbol_type)
4445 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4447 if (flags & BSF_SECTION_SYM)
4448 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4449 else if (bfd_is_com_section (syms[idx]->section))
4450 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4451 else if (bfd_is_und_section (syms[idx]->section))
4452 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4456 else if (flags & BSF_FILE)
4457 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4460 int bind = STB_LOCAL;
4462 if (flags & BSF_LOCAL)
4464 else if (flags & BSF_WEAK)
4466 else if (flags & BSF_GLOBAL)
4469 sym.st_info = ELF_ST_INFO (bind, type);
4472 if (type_ptr != NULL)
4473 sym.st_other = type_ptr->internal_elf_sym.st_other;
4477 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4478 outbound_syms += bed->s->sizeof_sym;
4482 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4483 symstrtab_hdr->sh_type = SHT_STRTAB;
4485 symstrtab_hdr->sh_flags = 0;
4486 symstrtab_hdr->sh_addr = 0;
4487 symstrtab_hdr->sh_entsize = 0;
4488 symstrtab_hdr->sh_link = 0;
4489 symstrtab_hdr->sh_info = 0;
4490 symstrtab_hdr->sh_addralign = 1;
4496 /* Return the number of bytes required to hold the symtab vector.
4498 Note that we base it on the count plus 1, since we will null terminate
4499 the vector allocated based on this size. However, the ELF symbol table
4500 always has a dummy entry as symbol #0, so it ends up even. */
4503 _bfd_elf_get_symtab_upper_bound (abfd)
4508 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4510 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4511 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4517 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4522 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4524 if (elf_dynsymtab (abfd) == 0)
4526 bfd_set_error (bfd_error_invalid_operation);
4530 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4531 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4537 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4538 bfd *abfd ATTRIBUTE_UNUSED;
4541 return (asect->reloc_count + 1) * sizeof (arelent *);
4544 /* Canonicalize the relocs. */
4547 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4556 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4562 tblptr = section->relocation;
4563 for (i = 0; i < section->reloc_count; i++)
4564 *relptr++ = tblptr++;
4568 return section->reloc_count;
4572 _bfd_elf_get_symtab (abfd, alocation)
4574 asymbol **alocation;
4576 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4577 (abfd, alocation, false);
4580 bfd_get_symcount (abfd) = symcount;
4585 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4587 asymbol **alocation;
4589 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4590 (abfd, alocation, true);
4593 /* Return the size required for the dynamic reloc entries. Any
4594 section that was actually installed in the BFD, and has type
4595 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4596 considered to be a dynamic reloc section. */
4599 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4605 if (elf_dynsymtab (abfd) == 0)
4607 bfd_set_error (bfd_error_invalid_operation);
4611 ret = sizeof (arelent *);
4612 for (s = abfd->sections; s != NULL; s = s->next)
4613 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4614 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4615 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4616 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4617 * sizeof (arelent *));
4622 /* Canonicalize the dynamic relocation entries. Note that we return
4623 the dynamic relocations as a single block, although they are
4624 actually associated with particular sections; the interface, which
4625 was designed for SunOS style shared libraries, expects that there
4626 is only one set of dynamic relocs. Any section that was actually
4627 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4628 the dynamic symbol table, is considered to be a dynamic reloc
4632 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4637 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4641 if (elf_dynsymtab (abfd) == 0)
4643 bfd_set_error (bfd_error_invalid_operation);
4647 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4649 for (s = abfd->sections; s != NULL; s = s->next)
4651 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4652 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4653 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4658 if (! (*slurp_relocs) (abfd, s, syms, true))
4660 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4662 for (i = 0; i < count; i++)
4673 /* Read in the version information. */
4676 _bfd_elf_slurp_version_tables (abfd)
4679 bfd_byte *contents = NULL;
4681 if (elf_dynverdef (abfd) != 0)
4683 Elf_Internal_Shdr *hdr;
4684 Elf_External_Verdef *everdef;
4685 Elf_Internal_Verdef *iverdef;
4686 Elf_Internal_Verdef *iverdefarr;
4687 Elf_Internal_Verdef iverdefmem;
4689 unsigned int maxidx;
4691 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4693 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4694 if (contents == NULL)
4696 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4697 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4700 /* We know the number of entries in the section but not the maximum
4701 index. Therefore we have to run through all entries and find
4703 everdef = (Elf_External_Verdef *) contents;
4705 for (i = 0; i < hdr->sh_info; ++i)
4707 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4709 if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4710 maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4712 everdef = ((Elf_External_Verdef *)
4713 ((bfd_byte *) everdef + iverdefmem.vd_next));
4716 elf_tdata (abfd)->verdef =
4717 ((Elf_Internal_Verdef *)
4718 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4719 if (elf_tdata (abfd)->verdef == NULL)
4722 elf_tdata (abfd)->cverdefs = maxidx;
4724 everdef = (Elf_External_Verdef *) contents;
4725 iverdefarr = elf_tdata (abfd)->verdef;
4726 for (i = 0; i < hdr->sh_info; i++)
4728 Elf_External_Verdaux *everdaux;
4729 Elf_Internal_Verdaux *iverdaux;
4732 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4734 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4735 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4737 iverdef->vd_bfd = abfd;
4739 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4742 * sizeof (Elf_Internal_Verdaux))));
4743 if (iverdef->vd_auxptr == NULL)
4746 everdaux = ((Elf_External_Verdaux *)
4747 ((bfd_byte *) everdef + iverdef->vd_aux));
4748 iverdaux = iverdef->vd_auxptr;
4749 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4751 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4753 iverdaux->vda_nodename =
4754 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4755 iverdaux->vda_name);
4756 if (iverdaux->vda_nodename == NULL)
4759 if (j + 1 < iverdef->vd_cnt)
4760 iverdaux->vda_nextptr = iverdaux + 1;
4762 iverdaux->vda_nextptr = NULL;
4764 everdaux = ((Elf_External_Verdaux *)
4765 ((bfd_byte *) everdaux + iverdaux->vda_next));
4768 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4770 if (i + 1 < hdr->sh_info)
4771 iverdef->vd_nextdef = iverdef + 1;
4773 iverdef->vd_nextdef = NULL;
4775 everdef = ((Elf_External_Verdef *)
4776 ((bfd_byte *) everdef + iverdef->vd_next));
4783 if (elf_dynverref (abfd) != 0)
4785 Elf_Internal_Shdr *hdr;
4786 Elf_External_Verneed *everneed;
4787 Elf_Internal_Verneed *iverneed;
4790 hdr = &elf_tdata (abfd)->dynverref_hdr;
4792 elf_tdata (abfd)->verref =
4793 ((Elf_Internal_Verneed *)
4794 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4795 if (elf_tdata (abfd)->verref == NULL)
4798 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4800 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4801 if (contents == NULL)
4803 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4804 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4807 everneed = (Elf_External_Verneed *) contents;
4808 iverneed = elf_tdata (abfd)->verref;
4809 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4811 Elf_External_Vernaux *evernaux;
4812 Elf_Internal_Vernaux *ivernaux;
4815 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4817 iverneed->vn_bfd = abfd;
4819 iverneed->vn_filename =
4820 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4822 if (iverneed->vn_filename == NULL)
4825 iverneed->vn_auxptr =
4826 ((Elf_Internal_Vernaux *)
4828 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4830 evernaux = ((Elf_External_Vernaux *)
4831 ((bfd_byte *) everneed + iverneed->vn_aux));
4832 ivernaux = iverneed->vn_auxptr;
4833 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4835 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4837 ivernaux->vna_nodename =
4838 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4839 ivernaux->vna_name);
4840 if (ivernaux->vna_nodename == NULL)
4843 if (j + 1 < iverneed->vn_cnt)
4844 ivernaux->vna_nextptr = ivernaux + 1;
4846 ivernaux->vna_nextptr = NULL;
4848 evernaux = ((Elf_External_Vernaux *)
4849 ((bfd_byte *) evernaux + ivernaux->vna_next));
4852 if (i + 1 < hdr->sh_info)
4853 iverneed->vn_nextref = iverneed + 1;
4855 iverneed->vn_nextref = NULL;
4857 everneed = ((Elf_External_Verneed *)
4858 ((bfd_byte *) everneed + iverneed->vn_next));
4868 if (contents == NULL)
4874 _bfd_elf_make_empty_symbol (abfd)
4877 elf_symbol_type *newsym;
4879 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4884 newsym->symbol.the_bfd = abfd;
4885 return &newsym->symbol;
4890 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4891 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4895 bfd_symbol_info (symbol, ret);
4898 /* Return whether a symbol name implies a local symbol. Most targets
4899 use this function for the is_local_label_name entry point, but some
4903 _bfd_elf_is_local_label_name (abfd, name)
4904 bfd *abfd ATTRIBUTE_UNUSED;
4907 /* Normal local symbols start with ``.L''. */
4908 if (name[0] == '.' && name[1] == 'L')
4911 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4912 DWARF debugging symbols starting with ``..''. */
4913 if (name[0] == '.' && name[1] == '.')
4916 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4917 emitting DWARF debugging output. I suspect this is actually a
4918 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4919 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4920 underscore to be emitted on some ELF targets). For ease of use,
4921 we treat such symbols as local. */
4922 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4929 _bfd_elf_get_lineno (ignore_abfd, symbol)
4930 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4931 asymbol *symbol ATTRIBUTE_UNUSED;
4938 _bfd_elf_set_arch_mach (abfd, arch, machine)
4940 enum bfd_architecture arch;
4941 unsigned long machine;
4943 /* If this isn't the right architecture for this backend, and this
4944 isn't the generic backend, fail. */
4945 if (arch != get_elf_backend_data (abfd)->arch
4946 && arch != bfd_arch_unknown
4947 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4950 return bfd_default_set_arch_mach (abfd, arch, machine);
4953 /* Find the nearest line to a particular section and offset, for error
4957 _bfd_elf_find_nearest_line (abfd,
4968 CONST char **filename_ptr;
4969 CONST char **functionname_ptr;
4970 unsigned int *line_ptr;
4973 const char *filename;
4978 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4979 filename_ptr, functionname_ptr,
4983 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4984 filename_ptr, functionname_ptr,
4988 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4989 &found, filename_ptr,
4990 functionname_ptr, line_ptr,
4991 &elf_tdata (abfd)->line_info))
4996 if (symbols == NULL)
5003 for (p = symbols; *p != NULL; p++)
5007 q = (elf_symbol_type *) *p;
5009 if (bfd_get_section (&q->symbol) != section)
5012 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5017 filename = bfd_asymbol_name (&q->symbol);
5021 if (q->symbol.section == section
5022 && q->symbol.value >= low_func
5023 && q->symbol.value <= offset)
5025 func = (asymbol *) q;
5026 low_func = q->symbol.value;
5035 *filename_ptr = filename;
5036 *functionname_ptr = bfd_asymbol_name (func);
5042 _bfd_elf_sizeof_headers (abfd, reloc)
5048 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5050 ret += get_program_header_size (abfd);
5055 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5060 bfd_size_type count;
5062 Elf_Internal_Shdr *hdr;
5064 if (! abfd->output_has_begun
5065 && ! _bfd_elf_compute_section_file_positions
5066 (abfd, (struct bfd_link_info *) NULL))
5069 hdr = &elf_section_data (section)->this_hdr;
5071 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5073 if (bfd_write (location, 1, count, abfd) != count)
5080 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5081 bfd *abfd ATTRIBUTE_UNUSED;
5082 arelent *cache_ptr ATTRIBUTE_UNUSED;
5083 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5090 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5093 Elf_Internal_Rel *dst;
5099 /* Try to convert a non-ELF reloc into an ELF one. */
5102 _bfd_elf_validate_reloc (abfd, areloc)
5106 /* Check whether we really have an ELF howto. */
5108 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5110 bfd_reloc_code_real_type code;
5111 reloc_howto_type *howto;
5113 /* Alien reloc: Try to determine its type to replace it with an
5114 equivalent ELF reloc. */
5116 if (areloc->howto->pc_relative)
5118 switch (areloc->howto->bitsize)
5121 code = BFD_RELOC_8_PCREL;
5124 code = BFD_RELOC_12_PCREL;
5127 code = BFD_RELOC_16_PCREL;
5130 code = BFD_RELOC_24_PCREL;
5133 code = BFD_RELOC_32_PCREL;
5136 code = BFD_RELOC_64_PCREL;
5142 howto = bfd_reloc_type_lookup (abfd, code);
5144 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5146 if (howto->pcrel_offset)
5147 areloc->addend += areloc->address;
5149 areloc->addend -= areloc->address; /* addend is unsigned!! */
5154 switch (areloc->howto->bitsize)
5160 code = BFD_RELOC_14;
5163 code = BFD_RELOC_16;
5166 code = BFD_RELOC_26;
5169 code = BFD_RELOC_32;
5172 code = BFD_RELOC_64;
5178 howto = bfd_reloc_type_lookup (abfd, code);
5182 areloc->howto = howto;
5190 (*_bfd_error_handler)
5191 (_("%s: unsupported relocation type %s"),
5192 bfd_get_filename (abfd), areloc->howto->name);
5193 bfd_set_error (bfd_error_bad_value);
5198 _bfd_elf_close_and_cleanup (abfd)
5201 if (bfd_get_format (abfd) == bfd_object)
5203 if (elf_shstrtab (abfd) != NULL)
5204 _bfd_stringtab_free (elf_shstrtab (abfd));
5207 return _bfd_generic_close_and_cleanup (abfd);
5210 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5211 in the relocation's offset. Thus we cannot allow any sort of sanity
5212 range-checking to interfere. There is nothing else to do in processing
5215 bfd_reloc_status_type
5216 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5217 bfd *abfd ATTRIBUTE_UNUSED;
5218 arelent *re ATTRIBUTE_UNUSED;
5219 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5220 PTR data ATTRIBUTE_UNUSED;
5221 asection *is ATTRIBUTE_UNUSED;
5222 bfd *obfd ATTRIBUTE_UNUSED;
5223 char **errmsg ATTRIBUTE_UNUSED;
5225 return bfd_reloc_ok;
5229 /* Elf core file support. Much of this only works on native
5230 toolchains, since we rely on knowing the
5231 machine-dependent procfs structure in order to pick
5232 out details about the corefile. */
5234 #ifdef HAVE_SYS_PROCFS_H
5235 # include <sys/procfs.h>
5239 /* Define offsetof for those systems which lack it. */
5242 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5246 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5249 elfcore_make_pid (abfd)
5252 return ((elf_tdata (abfd)->core_lwpid << 16)
5253 + (elf_tdata (abfd)->core_pid));
5257 /* If there isn't a section called NAME, make one, using
5258 data from SECT. Note, this function will generate a
5259 reference to NAME, so you shouldn't deallocate or
5263 elfcore_maybe_make_sect (abfd, name, sect)
5270 if (bfd_get_section_by_name (abfd, name) != NULL)
5273 sect2 = bfd_make_section (abfd, name);
5277 sect2->_raw_size = sect->_raw_size;
5278 sect2->filepos = sect->filepos;
5279 sect2->flags = sect->flags;
5280 sect2->alignment_power = sect->alignment_power;
5285 /* prstatus_t exists on:
5287 linux 2.[01] + glibc
5291 #if defined (HAVE_PRSTATUS_T)
5293 elfcore_grok_prstatus (abfd, note)
5295 Elf_Internal_Note* note;
5303 if (note->descsz == sizeof (prstatus_t))
5307 raw_size = sizeof (prstat.pr_reg);
5308 offset = offsetof (prstatus_t, pr_reg);
5309 memcpy (&prstat, note->descdata, sizeof (prstat));
5311 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5312 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5314 /* pr_who exists on:
5317 pr_who doesn't exist on:
5320 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5321 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5324 #if defined (HAVE_PRSTATUS32_T)
5325 else if (note->descsz == sizeof (prstatus32_t))
5327 /* 64-bit host, 32-bit corefile */
5328 prstatus32_t prstat;
5330 raw_size = sizeof (prstat.pr_reg);
5331 offset = offsetof (prstatus32_t, pr_reg);
5332 memcpy (&prstat, note->descdata, sizeof (prstat));
5334 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5335 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5337 /* pr_who exists on:
5340 pr_who doesn't exist on:
5343 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5344 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5347 #endif /* HAVE_PRSTATUS32_T */
5350 /* Fail - we don't know how to handle any other
5351 note size (ie. data object type). */
5355 /* Make a ".reg/999" section. */
5357 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5358 name = bfd_alloc (abfd, strlen (buf) + 1);
5363 sect = bfd_make_section (abfd, name);
5367 sect->_raw_size = raw_size;
5368 sect->filepos = note->descpos + offset;
5370 sect->flags = SEC_HAS_CONTENTS;
5371 sect->alignment_power = 2;
5373 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5378 #endif /* defined (HAVE_PRSTATUS_T) */
5381 /* Create a pseudosection containing the exact contents of NOTE. This
5382 actually creates up to two pseudosections:
5383 - For the single-threaded case, a section named NAME, unless
5384 such a section already exists.
5385 - For the multi-threaded case, a section named "NAME/PID", where
5386 PID is elfcore_make_pid (abfd).
5387 Both pseudosections have identical contents: the contents of NOTE. */
5390 elfcore_make_note_pseudosection (abfd, name, note)
5393 Elf_Internal_Note* note;
5396 char *threaded_name;
5399 /* Build the section name. */
5401 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5402 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5403 if (threaded_name == NULL)
5405 strcpy (threaded_name, buf);
5407 sect = bfd_make_section (abfd, threaded_name);
5410 sect->_raw_size = note->descsz;
5411 sect->filepos = note->descpos;
5412 sect->flags = SEC_HAS_CONTENTS;
5413 sect->alignment_power = 2;
5415 if (! elfcore_maybe_make_sect (abfd, name, sect))
5422 /* There isn't a consistent prfpregset_t across platforms,
5423 but it doesn't matter, because we don't have to pick this
5424 data structure apart. */
5426 elfcore_grok_prfpreg (abfd, note)
5428 Elf_Internal_Note* note;
5430 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5434 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5435 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5438 elfcore_grok_prxfpreg (abfd, note)
5440 Elf_Internal_Note* note;
5442 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5446 #if defined (HAVE_PRPSINFO_T)
5447 typedef prpsinfo_t elfcore_psinfo_t;
5448 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5449 typedef prpsinfo32_t elfcore_psinfo32_t;
5453 #if defined (HAVE_PSINFO_T)
5454 typedef psinfo_t elfcore_psinfo_t;
5455 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5456 typedef psinfo32_t elfcore_psinfo32_t;
5461 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5463 /* return a malloc'ed copy of a string at START which is at
5464 most MAX bytes long, possibly without a terminating '\0'.
5465 the copy will always have a terminating '\0'. */
5468 elfcore_strndup (abfd, start, max)
5474 char* end = memchr (start, '\0', max);
5482 dup = bfd_alloc (abfd, len + 1);
5486 memcpy (dup, start, len);
5493 elfcore_grok_psinfo (abfd, note)
5495 Elf_Internal_Note* note;
5497 if (note->descsz == sizeof (elfcore_psinfo_t))
5499 elfcore_psinfo_t psinfo;
5501 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5503 elf_tdata (abfd)->core_program
5504 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5506 elf_tdata (abfd)->core_command
5507 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5509 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5510 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5512 /* 64-bit host, 32-bit corefile */
5513 elfcore_psinfo32_t psinfo;
5515 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5517 elf_tdata (abfd)->core_program
5518 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5520 elf_tdata (abfd)->core_command
5521 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5527 /* Fail - we don't know how to handle any other
5528 note size (ie. data object type). */
5532 /* Note that for some reason, a spurious space is tacked
5533 onto the end of the args in some (at least one anyway)
5534 implementations, so strip it off if it exists. */
5537 char* command = elf_tdata (abfd)->core_command;
5538 int n = strlen (command);
5540 if (0 < n && command[n - 1] == ' ')
5541 command[n - 1] = '\0';
5546 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5549 #if defined (HAVE_PSTATUS_T)
5551 elfcore_grok_pstatus (abfd, note)
5553 Elf_Internal_Note* note;
5555 if (note->descsz == sizeof (pstatus_t))
5559 memcpy (&pstat, note->descdata, sizeof (pstat));
5561 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5563 #if defined (HAVE_PSTATUS32_T)
5564 else if (note->descsz == sizeof (pstatus32_t))
5566 /* 64-bit host, 32-bit corefile */
5569 memcpy (&pstat, note->descdata, sizeof (pstat));
5571 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5574 /* Could grab some more details from the "representative"
5575 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5576 NT_LWPSTATUS note, presumably. */
5580 #endif /* defined (HAVE_PSTATUS_T) */
5583 #if defined (HAVE_LWPSTATUS_T)
5585 elfcore_grok_lwpstatus (abfd, note)
5587 Elf_Internal_Note* note;
5589 lwpstatus_t lwpstat;
5594 if (note->descsz != sizeof (lwpstat))
5597 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5599 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5600 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5602 /* Make a ".reg/999" section. */
5604 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5605 name = bfd_alloc (abfd, strlen (buf) + 1);
5610 sect = bfd_make_section (abfd, name);
5614 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5615 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5616 sect->filepos = note->descpos
5617 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5620 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5621 sect->_raw_size = sizeof (lwpstat.pr_reg);
5622 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5625 sect->flags = SEC_HAS_CONTENTS;
5626 sect->alignment_power = 2;
5628 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5631 /* Make a ".reg2/999" section */
5633 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5634 name = bfd_alloc (abfd, strlen (buf) + 1);
5639 sect = bfd_make_section (abfd, name);
5643 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5644 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5645 sect->filepos = note->descpos
5646 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5649 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5650 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5651 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5654 sect->flags = SEC_HAS_CONTENTS;
5655 sect->alignment_power = 2;
5657 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5662 #endif /* defined (HAVE_LWPSTATUS_T) */
5664 #if defined (HAVE_WIN32_PSTATUS_T)
5666 elfcore_grok_win32pstatus (abfd, note)
5668 Elf_Internal_Note * note;
5673 win32_pstatus_t pstatus;
5675 if (note->descsz < sizeof (pstatus))
5678 memcpy (& pstatus, note->descdata, note->descsz);
5680 switch (pstatus.data_type)
5682 case NOTE_INFO_PROCESS:
5683 /* FIXME: need to add ->core_command. */
5684 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5685 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5688 case NOTE_INFO_THREAD:
5689 /* Make a ".reg/999" section. */
5690 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5692 name = bfd_alloc (abfd, strlen (buf) + 1);
5698 sect = bfd_make_section (abfd, name);
5702 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5703 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5704 data.thread_info.thread_context);
5705 sect->flags = SEC_HAS_CONTENTS;
5706 sect->alignment_power = 2;
5708 if (pstatus.data.thread_info.is_active_thread)
5709 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5713 case NOTE_INFO_MODULE:
5714 /* Make a ".module/xxxxxxxx" section. */
5715 sprintf (buf, ".module/%08x" , pstatus.data.module_info.base_address);
5717 name = bfd_alloc (abfd, strlen (buf) + 1);
5723 sect = bfd_make_section (abfd, name);
5728 sect->_raw_size = note->descsz;
5729 sect->filepos = note->descpos;
5730 sect->flags = SEC_HAS_CONTENTS;
5731 sect->alignment_power = 2;
5740 #endif /* HAVE_WIN32_PSTATUS_T */
5743 elfcore_grok_note (abfd, note)
5745 Elf_Internal_Note* note;
5752 #if defined (HAVE_PRSTATUS_T)
5754 return elfcore_grok_prstatus (abfd, note);
5757 #if defined (HAVE_PSTATUS_T)
5759 return elfcore_grok_pstatus (abfd, note);
5762 #if defined (HAVE_LWPSTATUS_T)
5764 return elfcore_grok_lwpstatus (abfd, note);
5767 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5768 return elfcore_grok_prfpreg (abfd, note);
5770 #if defined (HAVE_WIN32_PSTATUS_T)
5771 case NT_WIN32PSTATUS:
5772 return elfcore_grok_win32pstatus (abfd, note);
5775 case NT_PRXFPREG: /* Linux SSE extension */
5776 if (note->namesz == 5
5777 && ! strcmp (note->namedata, "LINUX"))
5778 return elfcore_grok_prxfpreg (abfd, note);
5782 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5785 return elfcore_grok_psinfo (abfd, note);
5792 elfcore_read_notes (abfd, offset, size)
5803 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5806 buf = bfd_malloc ((size_t) size);
5810 if (bfd_read (buf, size, 1, abfd) != size)
5818 while (p < buf + size)
5820 /* FIXME: bad alignment assumption. */
5821 Elf_External_Note* xnp = (Elf_External_Note*) p;
5822 Elf_Internal_Note in;
5824 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5826 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5827 in.namedata = xnp->name;
5829 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5830 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5831 in.descpos = offset + (in.descdata - buf);
5833 if (! elfcore_grok_note (abfd, &in))
5836 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5844 /* FIXME: This function is now unnecessary. Callers can just call
5845 bfd_section_from_phdr directly. */
5848 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5850 Elf_Internal_Phdr* phdr;
5853 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5861 /* Providing external access to the ELF program header table. */
5863 /* Return an upper bound on the number of bytes required to store a
5864 copy of ABFD's program header table entries. Return -1 if an error
5865 occurs; bfd_get_error will return an appropriate code. */
5867 bfd_get_elf_phdr_upper_bound (abfd)
5870 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5872 bfd_set_error (bfd_error_wrong_format);
5876 return (elf_elfheader (abfd)->e_phnum
5877 * sizeof (Elf_Internal_Phdr));
5881 /* Copy ABFD's program header table entries to *PHDRS. The entries
5882 will be stored as an array of Elf_Internal_Phdr structures, as
5883 defined in include/elf/internal.h. To find out how large the
5884 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5886 Return the number of program header table entries read, or -1 if an
5887 error occurs; bfd_get_error will return an appropriate code. */
5889 bfd_get_elf_phdrs (abfd, phdrs)
5895 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5897 bfd_set_error (bfd_error_wrong_format);
5901 num_phdrs = elf_elfheader (abfd)->e_phnum;
5902 memcpy (phdrs, elf_tdata (abfd)->phdr,
5903 num_phdrs * sizeof (Elf_Internal_Phdr));