1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
35 /* For sparc64-cross-sparc32. */
44 static INLINE struct elf_segment_map *make_mapping
45 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
46 static boolean map_sections_to_segments PARAMS ((bfd *));
47 static int elf_sort_sections PARAMS ((const PTR, const PTR));
48 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
49 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
50 static boolean prep_headers PARAMS ((bfd *));
51 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
52 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
53 static char *elf_read PARAMS ((bfd *, long, unsigned int));
54 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
55 static boolean assign_section_numbers PARAMS ((bfd *));
56 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
57 static boolean elf_map_symbols PARAMS ((bfd *));
58 static bfd_size_type get_program_header_size PARAMS ((bfd *));
59 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
60 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
61 bfd_vma, const char **,
63 static int elfcore_make_pid PARAMS ((bfd *));
64 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
65 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
66 Elf_Internal_Note *));
67 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
68 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
69 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
71 /* Swap version information in and out. The version information is
72 currently size independent. If that ever changes, this code will
73 need to move into elfcode.h. */
75 /* Swap in a Verdef structure. */
78 _bfd_elf_swap_verdef_in (abfd, src, dst)
80 const Elf_External_Verdef *src;
81 Elf_Internal_Verdef *dst;
83 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
84 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
85 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
86 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
87 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
88 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
89 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
92 /* Swap out a Verdef structure. */
95 _bfd_elf_swap_verdef_out (abfd, src, dst)
97 const Elf_Internal_Verdef *src;
98 Elf_External_Verdef *dst;
100 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
101 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
102 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
103 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
104 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
105 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
106 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
109 /* Swap in a Verdaux structure. */
112 _bfd_elf_swap_verdaux_in (abfd, src, dst)
114 const Elf_External_Verdaux *src;
115 Elf_Internal_Verdaux *dst;
117 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
118 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
121 /* Swap out a Verdaux structure. */
124 _bfd_elf_swap_verdaux_out (abfd, src, dst)
126 const Elf_Internal_Verdaux *src;
127 Elf_External_Verdaux *dst;
129 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
130 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
133 /* Swap in a Verneed structure. */
136 _bfd_elf_swap_verneed_in (abfd, src, dst)
138 const Elf_External_Verneed *src;
139 Elf_Internal_Verneed *dst;
141 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
142 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
143 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
144 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
145 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
148 /* Swap out a Verneed structure. */
151 _bfd_elf_swap_verneed_out (abfd, src, dst)
153 const Elf_Internal_Verneed *src;
154 Elf_External_Verneed *dst;
156 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
157 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
158 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
159 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
160 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
163 /* Swap in a Vernaux structure. */
166 _bfd_elf_swap_vernaux_in (abfd, src, dst)
168 const Elf_External_Vernaux *src;
169 Elf_Internal_Vernaux *dst;
171 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
172 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
173 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
174 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
175 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
178 /* Swap out a Vernaux structure. */
181 _bfd_elf_swap_vernaux_out (abfd, src, dst)
183 const Elf_Internal_Vernaux *src;
184 Elf_External_Vernaux *dst;
186 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
187 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
188 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
189 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
190 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
193 /* Swap in a Versym structure. */
196 _bfd_elf_swap_versym_in (abfd, src, dst)
198 const Elf_External_Versym *src;
199 Elf_Internal_Versym *dst;
201 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
204 /* Swap out a Versym structure. */
207 _bfd_elf_swap_versym_out (abfd, src, dst)
209 const Elf_Internal_Versym *src;
210 Elf_External_Versym *dst;
212 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
215 /* Standard ELF hash function. Do not change this function; you will
216 cause invalid hash tables to be generated. */
219 bfd_elf_hash (namearg)
222 const unsigned char *name = (const unsigned char *) namearg;
227 while ((ch = *name++) != '\0')
230 if ((g = (h & 0xf0000000)) != 0)
233 /* The ELF ABI says `h &= ~g', but this is equivalent in
234 this case and on some machines one insn instead of two. */
241 /* Read a specified number of bytes at a specified offset in an ELF
242 file, into a newly allocated buffer, and return a pointer to the
246 elf_read (abfd, offset, size)
253 if ((buf = bfd_alloc (abfd, size)) == NULL)
255 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
257 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
259 if (bfd_get_error () != bfd_error_system_call)
260 bfd_set_error (bfd_error_file_truncated);
267 bfd_elf_mkobject (abfd)
270 /* This just does initialization. */
271 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
272 elf_tdata (abfd) = (struct elf_obj_tdata *)
273 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
274 if (elf_tdata (abfd) == 0)
276 /* Since everything is done at close time, do we need any
283 bfd_elf_mkcorefile (abfd)
286 /* I think this can be done just like an object file. */
287 return bfd_elf_mkobject (abfd);
291 bfd_elf_get_str_section (abfd, shindex)
293 unsigned int shindex;
295 Elf_Internal_Shdr **i_shdrp;
296 char *shstrtab = NULL;
298 unsigned int shstrtabsize;
300 i_shdrp = elf_elfsections (abfd);
301 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
304 shstrtab = (char *) i_shdrp[shindex]->contents;
305 if (shstrtab == NULL)
307 /* No cached one, attempt to read, and cache what we read. */
308 offset = i_shdrp[shindex]->sh_offset;
309 shstrtabsize = i_shdrp[shindex]->sh_size;
310 shstrtab = elf_read (abfd, offset, shstrtabsize);
311 i_shdrp[shindex]->contents = (PTR) shstrtab;
317 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
319 unsigned int shindex;
320 unsigned int strindex;
322 Elf_Internal_Shdr *hdr;
327 hdr = elf_elfsections (abfd)[shindex];
329 if (hdr->contents == NULL
330 && bfd_elf_get_str_section (abfd, shindex) == NULL)
333 if (strindex >= hdr->sh_size)
335 (*_bfd_error_handler)
336 (_("%s: invalid string offset %u >= %lu for section `%s'"),
337 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
338 ((shindex == elf_elfheader(abfd)->e_shstrndx
339 && strindex == hdr->sh_name)
341 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
345 return ((char *) hdr->contents) + strindex;
348 /* Make a BFD section from an ELF section. We store a pointer to the
349 BFD section in the bfd_section field of the header. */
352 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
354 Elf_Internal_Shdr *hdr;
359 struct elf_backend_data *bed;
361 if (hdr->bfd_section != NULL)
363 BFD_ASSERT (strcmp (name,
364 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
368 newsect = bfd_make_section_anyway (abfd, name);
372 newsect->filepos = hdr->sh_offset;
374 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
375 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
376 || ! bfd_set_section_alignment (abfd, newsect,
377 bfd_log2 (hdr->sh_addralign)))
380 flags = SEC_NO_FLAGS;
381 if (hdr->sh_type != SHT_NOBITS)
382 flags |= SEC_HAS_CONTENTS;
383 if ((hdr->sh_flags & SHF_ALLOC) != 0)
386 if (hdr->sh_type != SHT_NOBITS)
389 if ((hdr->sh_flags & SHF_WRITE) == 0)
390 flags |= SEC_READONLY;
391 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
393 else if ((flags & SEC_LOAD) != 0)
395 if ((hdr->sh_flags & SHF_MERGE) != 0)
398 newsect->entsize = hdr->sh_entsize;
399 if ((hdr->sh_flags & SHF_STRINGS) != 0)
400 flags |= SEC_STRINGS;
403 /* The debugging sections appear to be recognized only by name, not
406 static const char *debug_sec_names [] =
415 for (i = sizeof (debug_sec_names) / sizeof (debug_sec_names[0]); i--;)
416 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
420 flags |= SEC_DEBUGGING;
423 /* As a GNU extension, if the name begins with .gnu.linkonce, we
424 only link a single copy of the section. This is used to support
425 g++. g++ will emit each template expansion in its own section.
426 The symbols will be defined as weak, so that multiple definitions
427 are permitted. The GNU linker extension is to actually discard
428 all but one of the sections. */
429 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
430 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
432 bed = get_elf_backend_data (abfd);
433 if (bed->elf_backend_section_flags)
434 if (! bed->elf_backend_section_flags (&flags, hdr))
437 if (! bfd_set_section_flags (abfd, newsect, flags))
440 if ((flags & SEC_ALLOC) != 0)
442 Elf_Internal_Phdr *phdr;
445 /* Look through the phdrs to see if we need to adjust the lma.
446 If all the p_paddr fields are zero, we ignore them, since
447 some ELF linkers produce such output. */
448 phdr = elf_tdata (abfd)->phdr;
449 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
451 if (phdr->p_paddr != 0)
454 if (i < elf_elfheader (abfd)->e_phnum)
456 phdr = elf_tdata (abfd)->phdr;
457 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
459 if (phdr->p_type == PT_LOAD
460 && phdr->p_vaddr != phdr->p_paddr
461 && phdr->p_vaddr <= hdr->sh_addr
462 && (phdr->p_vaddr + phdr->p_memsz
463 >= hdr->sh_addr + hdr->sh_size)
464 && ((flags & SEC_LOAD) == 0
465 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
466 && (phdr->p_offset + phdr->p_filesz
467 >= hdr->sh_offset + hdr->sh_size))))
469 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
476 hdr->bfd_section = newsect;
477 elf_section_data (newsect)->this_hdr = *hdr;
487 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
490 Helper functions for GDB to locate the string tables.
491 Since BFD hides string tables from callers, GDB needs to use an
492 internal hook to find them. Sun's .stabstr, in particular,
493 isn't even pointed to by the .stab section, so ordinary
494 mechanisms wouldn't work to find it, even if we had some.
497 struct elf_internal_shdr *
498 bfd_elf_find_section (abfd, name)
502 Elf_Internal_Shdr **i_shdrp;
507 i_shdrp = elf_elfsections (abfd);
510 shstrtab = bfd_elf_get_str_section
511 (abfd, elf_elfheader (abfd)->e_shstrndx);
512 if (shstrtab != NULL)
514 max = elf_elfheader (abfd)->e_shnum;
515 for (i = 1; i < max; i++)
516 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
523 const char *const bfd_elf_section_type_names[] = {
524 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
525 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
526 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
529 /* ELF relocs are against symbols. If we are producing relocateable
530 output, and the reloc is against an external symbol, and nothing
531 has given us any additional addend, the resulting reloc will also
532 be against the same symbol. In such a case, we don't want to
533 change anything about the way the reloc is handled, since it will
534 all be done at final link time. Rather than put special case code
535 into bfd_perform_relocation, all the reloc types use this howto
536 function. It just short circuits the reloc if producing
537 relocateable output against an external symbol. */
539 bfd_reloc_status_type
540 bfd_elf_generic_reloc (abfd,
547 bfd *abfd ATTRIBUTE_UNUSED;
548 arelent *reloc_entry;
550 PTR data ATTRIBUTE_UNUSED;
551 asection *input_section;
553 char **error_message ATTRIBUTE_UNUSED;
555 if (output_bfd != (bfd *) NULL
556 && (symbol->flags & BSF_SECTION_SYM) == 0
557 && (! reloc_entry->howto->partial_inplace
558 || reloc_entry->addend == 0))
560 reloc_entry->address += input_section->output_offset;
564 return bfd_reloc_continue;
567 /* Finish SHF_MERGE section merging. */
570 _bfd_elf_merge_sections (abfd, info)
572 struct bfd_link_info *info;
574 if (elf_hash_table (info)->merge_info)
575 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info);
579 /* Print out the program headers. */
582 _bfd_elf_print_private_bfd_data (abfd, farg)
586 FILE *f = (FILE *) farg;
587 Elf_Internal_Phdr *p;
589 bfd_byte *dynbuf = NULL;
591 p = elf_tdata (abfd)->phdr;
596 fprintf (f, _("\nProgram Header:\n"));
597 c = elf_elfheader (abfd)->e_phnum;
598 for (i = 0; i < c; i++, p++)
605 case PT_NULL: s = "NULL"; break;
606 case PT_LOAD: s = "LOAD"; break;
607 case PT_DYNAMIC: s = "DYNAMIC"; break;
608 case PT_INTERP: s = "INTERP"; break;
609 case PT_NOTE: s = "NOTE"; break;
610 case PT_SHLIB: s = "SHLIB"; break;
611 case PT_PHDR: s = "PHDR"; break;
612 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
614 fprintf (f, "%8s off 0x", s);
615 fprintf_vma (f, p->p_offset);
616 fprintf (f, " vaddr 0x");
617 fprintf_vma (f, p->p_vaddr);
618 fprintf (f, " paddr 0x");
619 fprintf_vma (f, p->p_paddr);
620 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
621 fprintf (f, " filesz 0x");
622 fprintf_vma (f, p->p_filesz);
623 fprintf (f, " memsz 0x");
624 fprintf_vma (f, p->p_memsz);
625 fprintf (f, " flags %c%c%c",
626 (p->p_flags & PF_R) != 0 ? 'r' : '-',
627 (p->p_flags & PF_W) != 0 ? 'w' : '-',
628 (p->p_flags & PF_X) != 0 ? 'x' : '-');
629 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
630 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
635 s = bfd_get_section_by_name (abfd, ".dynamic");
640 bfd_byte *extdyn, *extdynend;
642 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
644 fprintf (f, _("\nDynamic Section:\n"));
646 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
649 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
653 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
656 link = elf_elfsections (abfd)[elfsec]->sh_link;
658 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
659 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
662 extdynend = extdyn + s->_raw_size;
663 for (; extdyn < extdynend; extdyn += extdynsize)
665 Elf_Internal_Dyn dyn;
670 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
672 if (dyn.d_tag == DT_NULL)
679 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
683 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
684 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
685 case DT_PLTGOT: name = "PLTGOT"; break;
686 case DT_HASH: name = "HASH"; break;
687 case DT_STRTAB: name = "STRTAB"; break;
688 case DT_SYMTAB: name = "SYMTAB"; break;
689 case DT_RELA: name = "RELA"; break;
690 case DT_RELASZ: name = "RELASZ"; break;
691 case DT_RELAENT: name = "RELAENT"; break;
692 case DT_STRSZ: name = "STRSZ"; break;
693 case DT_SYMENT: name = "SYMENT"; break;
694 case DT_INIT: name = "INIT"; break;
695 case DT_FINI: name = "FINI"; break;
696 case DT_SONAME: name = "SONAME"; stringp = true; break;
697 case DT_RPATH: name = "RPATH"; stringp = true; break;
698 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
699 case DT_REL: name = "REL"; break;
700 case DT_RELSZ: name = "RELSZ"; break;
701 case DT_RELENT: name = "RELENT"; break;
702 case DT_PLTREL: name = "PLTREL"; break;
703 case DT_DEBUG: name = "DEBUG"; break;
704 case DT_TEXTREL: name = "TEXTREL"; break;
705 case DT_JMPREL: name = "JMPREL"; break;
706 case DT_BIND_NOW: name = "BIND_NOW"; break;
707 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
708 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
709 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
710 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
711 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
712 case DT_FLAGS: name = "FLAGS"; break;
713 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
714 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
715 case DT_CHECKSUM: name = "CHECKSUM"; break;
716 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
717 case DT_MOVEENT: name = "MOVEENT"; break;
718 case DT_MOVESZ: name = "MOVESZ"; break;
719 case DT_FEATURE: name = "FEATURE"; break;
720 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
721 case DT_SYMINSZ: name = "SYMINSZ"; break;
722 case DT_SYMINENT: name = "SYMINENT"; break;
723 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
724 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
725 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
726 case DT_PLTPAD: name = "PLTPAD"; break;
727 case DT_MOVETAB: name = "MOVETAB"; break;
728 case DT_SYMINFO: name = "SYMINFO"; break;
729 case DT_RELACOUNT: name = "RELACOUNT"; break;
730 case DT_RELCOUNT: name = "RELCOUNT"; break;
731 case DT_FLAGS_1: name = "FLAGS_1"; break;
732 case DT_VERSYM: name = "VERSYM"; break;
733 case DT_VERDEF: name = "VERDEF"; break;
734 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
735 case DT_VERNEED: name = "VERNEED"; break;
736 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
737 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
738 case DT_USED: name = "USED"; break;
739 case DT_FILTER: name = "FILTER"; stringp = true; break;
742 fprintf (f, " %-11s ", name);
744 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
749 string = bfd_elf_string_from_elf_section (abfd, link,
753 fprintf (f, "%s", string);
762 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
763 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
765 if (! _bfd_elf_slurp_version_tables (abfd))
769 if (elf_dynverdef (abfd) != 0)
771 Elf_Internal_Verdef *t;
773 fprintf (f, _("\nVersion definitions:\n"));
774 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
776 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
777 t->vd_flags, t->vd_hash, t->vd_nodename);
778 if (t->vd_auxptr->vda_nextptr != NULL)
780 Elf_Internal_Verdaux *a;
783 for (a = t->vd_auxptr->vda_nextptr;
786 fprintf (f, "%s ", a->vda_nodename);
792 if (elf_dynverref (abfd) != 0)
794 Elf_Internal_Verneed *t;
796 fprintf (f, _("\nVersion References:\n"));
797 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
799 Elf_Internal_Vernaux *a;
801 fprintf (f, _(" required from %s:\n"), t->vn_filename);
802 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
803 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
804 a->vna_flags, a->vna_other, a->vna_nodename);
816 /* Display ELF-specific fields of a symbol. */
819 bfd_elf_print_symbol (abfd, filep, symbol, how)
823 bfd_print_symbol_type how;
825 FILE *file = (FILE *) filep;
828 case bfd_print_symbol_name:
829 fprintf (file, "%s", symbol->name);
831 case bfd_print_symbol_more:
832 fprintf (file, "elf ");
833 fprintf_vma (file, symbol->value);
834 fprintf (file, " %lx", (long) symbol->flags);
836 case bfd_print_symbol_all:
838 const char *section_name;
839 const char *name = NULL;
840 struct elf_backend_data *bed;
841 unsigned char st_other;
843 section_name = symbol->section ? symbol->section->name : "(*none*)";
845 bed = get_elf_backend_data (abfd);
846 if (bed->elf_backend_print_symbol_all)
847 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
852 bfd_print_symbol_vandf ((PTR) file, symbol);
855 fprintf (file, " %s\t", section_name);
856 /* Print the "other" value for a symbol. For common symbols,
857 we've already printed the size; now print the alignment.
858 For other symbols, we have no specified alignment, and
859 we've printed the address; now print the size. */
861 (bfd_is_com_section (symbol->section)
862 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
863 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
865 /* If we have version information, print it. */
866 if (elf_tdata (abfd)->dynversym_section != 0
867 && (elf_tdata (abfd)->dynverdef_section != 0
868 || elf_tdata (abfd)->dynverref_section != 0))
871 const char *version_string;
873 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
877 else if (vernum == 1)
878 version_string = "Base";
879 else if (vernum <= elf_tdata (abfd)->cverdefs)
881 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
884 Elf_Internal_Verneed *t;
887 for (t = elf_tdata (abfd)->verref;
891 Elf_Internal_Vernaux *a;
893 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
895 if (a->vna_other == vernum)
897 version_string = a->vna_nodename;
904 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
905 fprintf (file, " %-11s", version_string);
910 fprintf (file, " (%s)", version_string);
911 for (i = 10 - strlen (version_string); i > 0; --i)
916 /* If the st_other field is not zero, print it. */
917 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
922 case STV_INTERNAL: fprintf (file, " .internal"); break;
923 case STV_HIDDEN: fprintf (file, " .hidden"); break;
924 case STV_PROTECTED: fprintf (file, " .protected"); break;
926 /* Some other non-defined flags are also present, so print
928 fprintf (file, " 0x%02x", (unsigned int) st_other);
931 fprintf (file, " %s", name);
937 /* Create an entry in an ELF linker hash table. */
939 struct bfd_hash_entry *
940 _bfd_elf_link_hash_newfunc (entry, table, string)
941 struct bfd_hash_entry *entry;
942 struct bfd_hash_table *table;
945 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
947 /* Allocate the structure if it has not already been allocated by a
949 if (ret == (struct elf_link_hash_entry *) NULL)
950 ret = ((struct elf_link_hash_entry *)
951 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
952 if (ret == (struct elf_link_hash_entry *) NULL)
953 return (struct bfd_hash_entry *) ret;
955 /* Call the allocation method of the superclass. */
956 ret = ((struct elf_link_hash_entry *)
957 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
959 if (ret != (struct elf_link_hash_entry *) NULL)
961 /* Set local fields. */
965 ret->dynstr_index = 0;
967 ret->got.offset = (bfd_vma) -1;
968 ret->plt.offset = (bfd_vma) -1;
969 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
970 ret->verinfo.verdef = NULL;
971 ret->vtable_entries_used = NULL;
972 ret->vtable_entries_size = 0;
973 ret->vtable_parent = NULL;
974 ret->type = STT_NOTYPE;
976 /* Assume that we have been called by a non-ELF symbol reader.
977 This flag is then reset by the code which reads an ELF input
978 file. This ensures that a symbol created by a non-ELF symbol
979 reader will have the flag set correctly. */
980 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
983 return (struct bfd_hash_entry *) ret;
986 /* Copy data from an indirect symbol to its direct symbol, hiding the
987 old indirect symbol. */
990 _bfd_elf_link_hash_copy_indirect (dir, ind)
991 struct elf_link_hash_entry *dir, *ind;
993 /* Copy down any references that we may have already seen to the
994 symbol which just became indirect. */
996 dir->elf_link_hash_flags |=
997 (ind->elf_link_hash_flags
998 & (ELF_LINK_HASH_REF_DYNAMIC
999 | ELF_LINK_HASH_REF_REGULAR
1000 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1001 | ELF_LINK_NON_GOT_REF));
1003 /* Copy over the global and procedure linkage table offset entries.
1004 These may have been already set up by a check_relocs routine. */
1005 if (dir->got.offset == (bfd_vma) -1)
1007 dir->got.offset = ind->got.offset;
1008 ind->got.offset = (bfd_vma) -1;
1010 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
1012 if (dir->plt.offset == (bfd_vma) -1)
1014 dir->plt.offset = ind->plt.offset;
1015 ind->plt.offset = (bfd_vma) -1;
1017 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
1019 if (dir->dynindx == -1)
1021 dir->dynindx = ind->dynindx;
1022 dir->dynstr_index = ind->dynstr_index;
1024 ind->dynstr_index = 0;
1026 BFD_ASSERT (ind->dynindx == -1);
1030 _bfd_elf_link_hash_hide_symbol (info, h)
1031 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1032 struct elf_link_hash_entry *h;
1034 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1035 h->plt.offset = (bfd_vma) -1;
1036 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1040 /* Initialize an ELF linker hash table. */
1043 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1044 struct elf_link_hash_table *table;
1046 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1047 struct bfd_hash_table *,
1050 table->dynamic_sections_created = false;
1051 table->dynobj = NULL;
1052 /* The first dynamic symbol is a dummy. */
1053 table->dynsymcount = 1;
1054 table->dynstr = NULL;
1055 table->bucketcount = 0;
1056 table->needed = NULL;
1057 table->runpath = NULL;
1059 table->stab_info = NULL;
1060 table->merge_info = NULL;
1061 table->dynlocal = NULL;
1062 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1065 /* Create an ELF linker hash table. */
1067 struct bfd_link_hash_table *
1068 _bfd_elf_link_hash_table_create (abfd)
1071 struct elf_link_hash_table *ret;
1073 ret = ((struct elf_link_hash_table *)
1074 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1075 if (ret == (struct elf_link_hash_table *) NULL)
1078 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1080 bfd_release (abfd, ret);
1087 /* This is a hook for the ELF emulation code in the generic linker to
1088 tell the backend linker what file name to use for the DT_NEEDED
1089 entry for a dynamic object. The generic linker passes name as an
1090 empty string to indicate that no DT_NEEDED entry should be made. */
1093 bfd_elf_set_dt_needed_name (abfd, name)
1097 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1098 && bfd_get_format (abfd) == bfd_object)
1099 elf_dt_name (abfd) = name;
1103 bfd_elf_set_dt_needed_soname (abfd, name)
1107 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1108 && bfd_get_format (abfd) == bfd_object)
1109 elf_dt_soname (abfd) = name;
1112 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1113 the linker ELF emulation code. */
1115 struct bfd_link_needed_list *
1116 bfd_elf_get_needed_list (abfd, info)
1117 bfd *abfd ATTRIBUTE_UNUSED;
1118 struct bfd_link_info *info;
1120 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1122 return elf_hash_table (info)->needed;
1125 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1126 hook for the linker ELF emulation code. */
1128 struct bfd_link_needed_list *
1129 bfd_elf_get_runpath_list (abfd, info)
1130 bfd *abfd ATTRIBUTE_UNUSED;
1131 struct bfd_link_info *info;
1133 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1135 return elf_hash_table (info)->runpath;
1138 /* Get the name actually used for a dynamic object for a link. This
1139 is the SONAME entry if there is one. Otherwise, it is the string
1140 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1143 bfd_elf_get_dt_soname (abfd)
1146 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1147 && bfd_get_format (abfd) == bfd_object)
1148 return elf_dt_name (abfd);
1152 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1153 the ELF linker emulation code. */
1156 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1158 struct bfd_link_needed_list **pneeded;
1161 bfd_byte *dynbuf = NULL;
1164 bfd_byte *extdyn, *extdynend;
1166 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1170 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1171 || bfd_get_format (abfd) != bfd_object)
1174 s = bfd_get_section_by_name (abfd, ".dynamic");
1175 if (s == NULL || s->_raw_size == 0)
1178 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1182 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1186 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1190 link = elf_elfsections (abfd)[elfsec]->sh_link;
1192 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1193 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1196 extdynend = extdyn + s->_raw_size;
1197 for (; extdyn < extdynend; extdyn += extdynsize)
1199 Elf_Internal_Dyn dyn;
1201 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1203 if (dyn.d_tag == DT_NULL)
1206 if (dyn.d_tag == DT_NEEDED)
1209 struct bfd_link_needed_list *l;
1211 string = bfd_elf_string_from_elf_section (abfd, link,
1216 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1237 /* Allocate an ELF string table--force the first byte to be zero. */
1239 struct bfd_strtab_hash *
1240 _bfd_elf_stringtab_init ()
1242 struct bfd_strtab_hash *ret;
1244 ret = _bfd_stringtab_init ();
1249 loc = _bfd_stringtab_add (ret, "", true, false);
1250 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1251 if (loc == (bfd_size_type) -1)
1253 _bfd_stringtab_free (ret);
1260 /* ELF .o/exec file reading */
1262 /* Create a new bfd section from an ELF section header. */
1265 bfd_section_from_shdr (abfd, shindex)
1267 unsigned int shindex;
1269 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1270 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1271 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1274 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1276 switch (hdr->sh_type)
1279 /* Inactive section. Throw it away. */
1282 case SHT_PROGBITS: /* Normal section with contents. */
1283 case SHT_DYNAMIC: /* Dynamic linking information. */
1284 case SHT_NOBITS: /* .bss section. */
1285 case SHT_HASH: /* .hash section. */
1286 case SHT_NOTE: /* .note section. */
1287 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1289 case SHT_SYMTAB: /* A symbol table */
1290 if (elf_onesymtab (abfd) == shindex)
1293 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1294 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1295 elf_onesymtab (abfd) = shindex;
1296 elf_tdata (abfd)->symtab_hdr = *hdr;
1297 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1298 abfd->flags |= HAS_SYMS;
1300 /* Sometimes a shared object will map in the symbol table. If
1301 SHF_ALLOC is set, and this is a shared object, then we also
1302 treat this section as a BFD section. We can not base the
1303 decision purely on SHF_ALLOC, because that flag is sometimes
1304 set in a relocateable object file, which would confuse the
1306 if ((hdr->sh_flags & SHF_ALLOC) != 0
1307 && (abfd->flags & DYNAMIC) != 0
1308 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1313 case SHT_DYNSYM: /* A dynamic symbol table */
1314 if (elf_dynsymtab (abfd) == shindex)
1317 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1318 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1319 elf_dynsymtab (abfd) = shindex;
1320 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1321 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1322 abfd->flags |= HAS_SYMS;
1324 /* Besides being a symbol table, we also treat this as a regular
1325 section, so that objcopy can handle it. */
1326 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1328 case SHT_STRTAB: /* A string table */
1329 if (hdr->bfd_section != NULL)
1331 if (ehdr->e_shstrndx == shindex)
1333 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1334 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1340 for (i = 1; i < ehdr->e_shnum; i++)
1342 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1343 if (hdr2->sh_link == shindex)
1345 if (! bfd_section_from_shdr (abfd, i))
1347 if (elf_onesymtab (abfd) == i)
1349 elf_tdata (abfd)->strtab_hdr = *hdr;
1350 elf_elfsections (abfd)[shindex] =
1351 &elf_tdata (abfd)->strtab_hdr;
1354 if (elf_dynsymtab (abfd) == i)
1356 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1357 elf_elfsections (abfd)[shindex] = hdr =
1358 &elf_tdata (abfd)->dynstrtab_hdr;
1359 /* We also treat this as a regular section, so
1360 that objcopy can handle it. */
1363 #if 0 /* Not handling other string tables specially right now. */
1364 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1365 /* We have a strtab for some random other section. */
1366 newsect = (asection *) hdr2->bfd_section;
1369 hdr->bfd_section = newsect;
1370 hdr2 = &elf_section_data (newsect)->str_hdr;
1372 elf_elfsections (abfd)[shindex] = hdr2;
1378 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1382 /* *These* do a lot of work -- but build no sections! */
1384 asection *target_sect;
1385 Elf_Internal_Shdr *hdr2;
1387 /* Check for a bogus link to avoid crashing. */
1388 if (hdr->sh_link >= ehdr->e_shnum)
1390 ((*_bfd_error_handler)
1391 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1392 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1393 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1396 /* For some incomprehensible reason Oracle distributes
1397 libraries for Solaris in which some of the objects have
1398 bogus sh_link fields. It would be nice if we could just
1399 reject them, but, unfortunately, some people need to use
1400 them. We scan through the section headers; if we find only
1401 one suitable symbol table, we clobber the sh_link to point
1402 to it. I hope this doesn't break anything. */
1403 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1404 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1410 for (scan = 1; scan < ehdr->e_shnum; scan++)
1412 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1413 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1424 hdr->sh_link = found;
1427 /* Get the symbol table. */
1428 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1429 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1432 /* If this reloc section does not use the main symbol table we
1433 don't treat it as a reloc section. BFD can't adequately
1434 represent such a section, so at least for now, we don't
1435 try. We just present it as a normal section. We also
1436 can't use it as a reloc section if it points to the null
1438 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1439 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1441 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1443 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1444 if (target_sect == NULL)
1447 if ((target_sect->flags & SEC_RELOC) == 0
1448 || target_sect->reloc_count == 0)
1449 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1452 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1453 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1454 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1457 elf_elfsections (abfd)[shindex] = hdr2;
1458 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1459 target_sect->flags |= SEC_RELOC;
1460 target_sect->relocation = NULL;
1461 target_sect->rel_filepos = hdr->sh_offset;
1462 /* In the section to which the relocations apply, mark whether
1463 its relocations are of the REL or RELA variety. */
1464 if (hdr->sh_size != 0)
1465 elf_section_data (target_sect)->use_rela_p
1466 = (hdr->sh_type == SHT_RELA);
1467 abfd->flags |= HAS_RELOC;
1472 case SHT_GNU_verdef:
1473 elf_dynverdef (abfd) = shindex;
1474 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1475 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1478 case SHT_GNU_versym:
1479 elf_dynversym (abfd) = shindex;
1480 elf_tdata (abfd)->dynversym_hdr = *hdr;
1481 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1484 case SHT_GNU_verneed:
1485 elf_dynverref (abfd) = shindex;
1486 elf_tdata (abfd)->dynverref_hdr = *hdr;
1487 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1494 /* Check for any processor-specific section types. */
1496 if (bed->elf_backend_section_from_shdr)
1497 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1505 /* Given an ELF section number, retrieve the corresponding BFD
1509 bfd_section_from_elf_index (abfd, index)
1513 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1514 if (index >= elf_elfheader (abfd)->e_shnum)
1516 return elf_elfsections (abfd)[index]->bfd_section;
1520 _bfd_elf_new_section_hook (abfd, sec)
1524 struct bfd_elf_section_data *sdata;
1526 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1529 sec->used_by_bfd = (PTR) sdata;
1531 /* Indicate whether or not this section should use RELA relocations. */
1533 = get_elf_backend_data (abfd)->default_use_rela_p;
1538 /* Create a new bfd section from an ELF program header.
1540 Since program segments have no names, we generate a synthetic name
1541 of the form segment<NUM>, where NUM is generally the index in the
1542 program header table. For segments that are split (see below) we
1543 generate the names segment<NUM>a and segment<NUM>b.
1545 Note that some program segments may have a file size that is different than
1546 (less than) the memory size. All this means is that at execution the
1547 system must allocate the amount of memory specified by the memory size,
1548 but only initialize it with the first "file size" bytes read from the
1549 file. This would occur for example, with program segments consisting
1550 of combined data+bss.
1552 To handle the above situation, this routine generates TWO bfd sections
1553 for the single program segment. The first has the length specified by
1554 the file size of the segment, and the second has the length specified
1555 by the difference between the two sizes. In effect, the segment is split
1556 into it's initialized and uninitialized parts.
1561 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1563 Elf_Internal_Phdr *hdr;
1565 const char *typename;
1572 split = ((hdr->p_memsz > 0)
1573 && (hdr->p_filesz > 0)
1574 && (hdr->p_memsz > hdr->p_filesz));
1575 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1576 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1579 strcpy (name, namebuf);
1580 newsect = bfd_make_section (abfd, name);
1581 if (newsect == NULL)
1583 newsect->vma = hdr->p_vaddr;
1584 newsect->lma = hdr->p_paddr;
1585 newsect->_raw_size = hdr->p_filesz;
1586 newsect->filepos = hdr->p_offset;
1587 newsect->flags |= SEC_HAS_CONTENTS;
1588 if (hdr->p_type == PT_LOAD)
1590 newsect->flags |= SEC_ALLOC;
1591 newsect->flags |= SEC_LOAD;
1592 if (hdr->p_flags & PF_X)
1594 /* FIXME: all we known is that it has execute PERMISSION,
1596 newsect->flags |= SEC_CODE;
1599 if (!(hdr->p_flags & PF_W))
1601 newsect->flags |= SEC_READONLY;
1606 sprintf (namebuf, "%s%db", typename, index);
1607 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1610 strcpy (name, namebuf);
1611 newsect = bfd_make_section (abfd, name);
1612 if (newsect == NULL)
1614 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1615 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1616 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1617 if (hdr->p_type == PT_LOAD)
1619 newsect->flags |= SEC_ALLOC;
1620 if (hdr->p_flags & PF_X)
1621 newsect->flags |= SEC_CODE;
1623 if (!(hdr->p_flags & PF_W))
1624 newsect->flags |= SEC_READONLY;
1631 bfd_section_from_phdr (abfd, hdr, index)
1633 Elf_Internal_Phdr *hdr;
1636 struct elf_backend_data *bed;
1638 switch (hdr->p_type)
1641 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1644 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1647 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1650 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1653 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1655 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1660 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1663 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1666 /* Check for any processor-specific program segment types.
1667 If no handler for them, default to making "segment" sections. */
1668 bed = get_elf_backend_data (abfd);
1669 if (bed->elf_backend_section_from_phdr)
1670 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1672 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1676 /* Initialize REL_HDR, the section-header for new section, containing
1677 relocations against ASECT. If USE_RELA_P is true, we use RELA
1678 relocations; otherwise, we use REL relocations. */
1681 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1683 Elf_Internal_Shdr *rel_hdr;
1688 struct elf_backend_data *bed;
1690 bed = get_elf_backend_data (abfd);
1691 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1694 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1696 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1698 if (rel_hdr->sh_name == (unsigned int) -1)
1700 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1701 rel_hdr->sh_entsize = (use_rela_p
1702 ? bed->s->sizeof_rela
1703 : bed->s->sizeof_rel);
1704 rel_hdr->sh_addralign = bed->s->file_align;
1705 rel_hdr->sh_flags = 0;
1706 rel_hdr->sh_addr = 0;
1707 rel_hdr->sh_size = 0;
1708 rel_hdr->sh_offset = 0;
1713 /* Set up an ELF internal section header for a section. */
1716 elf_fake_sections (abfd, asect, failedptrarg)
1721 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1722 boolean *failedptr = (boolean *) failedptrarg;
1723 Elf_Internal_Shdr *this_hdr;
1727 /* We already failed; just get out of the bfd_map_over_sections
1732 this_hdr = &elf_section_data (asect)->this_hdr;
1734 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1737 if (this_hdr->sh_name == (unsigned long) -1)
1743 this_hdr->sh_flags = 0;
1745 if ((asect->flags & SEC_ALLOC) != 0
1746 || asect->user_set_vma)
1747 this_hdr->sh_addr = asect->vma;
1749 this_hdr->sh_addr = 0;
1751 this_hdr->sh_offset = 0;
1752 this_hdr->sh_size = asect->_raw_size;
1753 this_hdr->sh_link = 0;
1754 this_hdr->sh_addralign = 1 << asect->alignment_power;
1755 /* The sh_entsize and sh_info fields may have been set already by
1756 copy_private_section_data. */
1758 this_hdr->bfd_section = asect;
1759 this_hdr->contents = NULL;
1761 /* FIXME: This should not be based on section names. */
1762 if (strcmp (asect->name, ".dynstr") == 0)
1763 this_hdr->sh_type = SHT_STRTAB;
1764 else if (strcmp (asect->name, ".hash") == 0)
1766 this_hdr->sh_type = SHT_HASH;
1767 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1769 else if (strcmp (asect->name, ".dynsym") == 0)
1771 this_hdr->sh_type = SHT_DYNSYM;
1772 this_hdr->sh_entsize = bed->s->sizeof_sym;
1774 else if (strcmp (asect->name, ".dynamic") == 0)
1776 this_hdr->sh_type = SHT_DYNAMIC;
1777 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1779 else if (strncmp (asect->name, ".rela", 5) == 0
1780 && get_elf_backend_data (abfd)->may_use_rela_p)
1782 this_hdr->sh_type = SHT_RELA;
1783 this_hdr->sh_entsize = bed->s->sizeof_rela;
1785 else if (strncmp (asect->name, ".rel", 4) == 0
1786 && get_elf_backend_data (abfd)->may_use_rel_p)
1788 this_hdr->sh_type = SHT_REL;
1789 this_hdr->sh_entsize = bed->s->sizeof_rel;
1791 else if (strncmp (asect->name, ".note", 5) == 0)
1792 this_hdr->sh_type = SHT_NOTE;
1793 else if (strncmp (asect->name, ".stab", 5) == 0
1794 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1795 this_hdr->sh_type = SHT_STRTAB;
1796 else if (strcmp (asect->name, ".gnu.version") == 0)
1798 this_hdr->sh_type = SHT_GNU_versym;
1799 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1801 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1803 this_hdr->sh_type = SHT_GNU_verdef;
1804 this_hdr->sh_entsize = 0;
1805 /* objcopy or strip will copy over sh_info, but may not set
1806 cverdefs. The linker will set cverdefs, but sh_info will be
1808 if (this_hdr->sh_info == 0)
1809 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1811 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1812 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1814 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1816 this_hdr->sh_type = SHT_GNU_verneed;
1817 this_hdr->sh_entsize = 0;
1818 /* objcopy or strip will copy over sh_info, but may not set
1819 cverrefs. The linker will set cverrefs, but sh_info will be
1821 if (this_hdr->sh_info == 0)
1822 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1824 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1825 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1827 else if ((asect->flags & SEC_ALLOC) != 0
1828 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
1829 this_hdr->sh_type = SHT_NOBITS;
1831 this_hdr->sh_type = SHT_PROGBITS;
1833 if ((asect->flags & SEC_ALLOC) != 0)
1834 this_hdr->sh_flags |= SHF_ALLOC;
1835 if ((asect->flags & SEC_READONLY) == 0)
1836 this_hdr->sh_flags |= SHF_WRITE;
1837 if ((asect->flags & SEC_CODE) != 0)
1838 this_hdr->sh_flags |= SHF_EXECINSTR;
1839 if ((asect->flags & SEC_MERGE) != 0)
1841 this_hdr->sh_flags |= SHF_MERGE;
1842 this_hdr->sh_entsize = asect->entsize;
1843 if ((asect->flags & SEC_STRINGS) != 0)
1844 this_hdr->sh_flags |= SHF_STRINGS;
1847 /* Check for processor-specific section types. */
1848 if (bed->elf_backend_fake_sections)
1849 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1851 /* If the section has relocs, set up a section header for the
1852 SHT_REL[A] section. If two relocation sections are required for
1853 this section, it is up to the processor-specific back-end to
1854 create the other. */
1855 if ((asect->flags & SEC_RELOC) != 0
1856 && !_bfd_elf_init_reloc_shdr (abfd,
1857 &elf_section_data (asect)->rel_hdr,
1859 elf_section_data (asect)->use_rela_p))
1863 /* Assign all ELF section numbers. The dummy first section is handled here
1864 too. The link/info pointers for the standard section types are filled
1865 in here too, while we're at it. */
1868 assign_section_numbers (abfd)
1871 struct elf_obj_tdata *t = elf_tdata (abfd);
1873 unsigned int section_number;
1874 Elf_Internal_Shdr **i_shdrp;
1878 for (sec = abfd->sections; sec; sec = sec->next)
1880 struct bfd_elf_section_data *d = elf_section_data (sec);
1882 d->this_idx = section_number++;
1883 if ((sec->flags & SEC_RELOC) == 0)
1886 d->rel_idx = section_number++;
1889 d->rel_idx2 = section_number++;
1894 t->shstrtab_section = section_number++;
1895 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1896 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1898 if (bfd_get_symcount (abfd) > 0)
1900 t->symtab_section = section_number++;
1901 t->strtab_section = section_number++;
1904 elf_elfheader (abfd)->e_shnum = section_number;
1906 /* Set up the list of section header pointers, in agreement with the
1908 i_shdrp = ((Elf_Internal_Shdr **)
1909 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1910 if (i_shdrp == NULL)
1913 i_shdrp[0] = ((Elf_Internal_Shdr *)
1914 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1915 if (i_shdrp[0] == NULL)
1917 bfd_release (abfd, i_shdrp);
1920 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1922 elf_elfsections (abfd) = i_shdrp;
1924 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1925 if (bfd_get_symcount (abfd) > 0)
1927 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1928 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1929 t->symtab_hdr.sh_link = t->strtab_section;
1931 for (sec = abfd->sections; sec; sec = sec->next)
1933 struct bfd_elf_section_data *d = elf_section_data (sec);
1937 i_shdrp[d->this_idx] = &d->this_hdr;
1938 if (d->rel_idx != 0)
1939 i_shdrp[d->rel_idx] = &d->rel_hdr;
1940 if (d->rel_idx2 != 0)
1941 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1943 /* Fill in the sh_link and sh_info fields while we're at it. */
1945 /* sh_link of a reloc section is the section index of the symbol
1946 table. sh_info is the section index of the section to which
1947 the relocation entries apply. */
1948 if (d->rel_idx != 0)
1950 d->rel_hdr.sh_link = t->symtab_section;
1951 d->rel_hdr.sh_info = d->this_idx;
1953 if (d->rel_idx2 != 0)
1955 d->rel_hdr2->sh_link = t->symtab_section;
1956 d->rel_hdr2->sh_info = d->this_idx;
1959 switch (d->this_hdr.sh_type)
1963 /* A reloc section which we are treating as a normal BFD
1964 section. sh_link is the section index of the symbol
1965 table. sh_info is the section index of the section to
1966 which the relocation entries apply. We assume that an
1967 allocated reloc section uses the dynamic symbol table.
1968 FIXME: How can we be sure? */
1969 s = bfd_get_section_by_name (abfd, ".dynsym");
1971 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1973 /* We look up the section the relocs apply to by name. */
1975 if (d->this_hdr.sh_type == SHT_REL)
1979 s = bfd_get_section_by_name (abfd, name);
1981 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1985 /* We assume that a section named .stab*str is a stabs
1986 string section. We look for a section with the same name
1987 but without the trailing ``str'', and set its sh_link
1988 field to point to this section. */
1989 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1990 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1995 len = strlen (sec->name);
1996 alc = (char *) bfd_malloc (len - 2);
1999 strncpy (alc, sec->name, len - 3);
2000 alc[len - 3] = '\0';
2001 s = bfd_get_section_by_name (abfd, alc);
2005 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2007 /* This is a .stab section. */
2008 elf_section_data (s)->this_hdr.sh_entsize =
2009 4 + 2 * bfd_get_arch_size (abfd) / 8;
2016 case SHT_GNU_verneed:
2017 case SHT_GNU_verdef:
2018 /* sh_link is the section header index of the string table
2019 used for the dynamic entries, or the symbol table, or the
2021 s = bfd_get_section_by_name (abfd, ".dynstr");
2023 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2027 case SHT_GNU_versym:
2028 /* sh_link is the section header index of the symbol table
2029 this hash table or version table is for. */
2030 s = bfd_get_section_by_name (abfd, ".dynsym");
2032 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2040 /* Map symbol from it's internal number to the external number, moving
2041 all local symbols to be at the head of the list. */
2044 sym_is_global (abfd, sym)
2048 /* If the backend has a special mapping, use it. */
2049 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2050 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2053 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2054 || bfd_is_und_section (bfd_get_section (sym))
2055 || bfd_is_com_section (bfd_get_section (sym)));
2059 elf_map_symbols (abfd)
2062 int symcount = bfd_get_symcount (abfd);
2063 asymbol **syms = bfd_get_outsymbols (abfd);
2064 asymbol **sect_syms;
2066 int num_globals = 0;
2067 int num_locals2 = 0;
2068 int num_globals2 = 0;
2070 int num_sections = 0;
2077 fprintf (stderr, "elf_map_symbols\n");
2081 /* Add a section symbol for each BFD section. FIXME: Is this really
2083 for (asect = abfd->sections; asect; asect = asect->next)
2085 if (max_index < asect->index)
2086 max_index = asect->index;
2090 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2091 if (sect_syms == NULL)
2093 elf_section_syms (abfd) = sect_syms;
2095 for (idx = 0; idx < symcount; idx++)
2099 if ((sym->flags & BSF_SECTION_SYM) != 0
2106 if (sec->owner != NULL)
2108 if (sec->owner != abfd)
2110 if (sec->output_offset != 0)
2113 sec = sec->output_section;
2115 /* Empty sections in the input files may have had a section
2116 symbol created for them. (See the comment near the end of
2117 _bfd_generic_link_output_symbols in linker.c). If the linker
2118 script discards such sections then we will reach this point.
2119 Since we know that we cannot avoid this case, we detect it
2120 and skip the abort and the assignment to the sect_syms array.
2121 To reproduce this particular case try running the linker
2122 testsuite test ld-scripts/weak.exp for an ELF port that uses
2123 the generic linker. */
2124 if (sec->owner == NULL)
2127 BFD_ASSERT (sec->owner == abfd);
2129 sect_syms[sec->index] = syms[idx];
2134 for (asect = abfd->sections; asect; asect = asect->next)
2136 if (sect_syms[asect->index] != NULL)
2139 sym = bfd_make_empty_symbol (abfd);
2142 sym->the_bfd = abfd;
2143 sym->name = asect->name;
2145 /* Set the flags to 0 to indicate that this one was newly added. */
2147 sym->section = asect;
2148 sect_syms[asect->index] = sym;
2152 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2153 asect->name, (long) asect->vma, asect->index, (long) asect);
2157 /* Classify all of the symbols. */
2158 for (idx = 0; idx < symcount; idx++)
2160 if (!sym_is_global (abfd, syms[idx]))
2165 for (asect = abfd->sections; asect; asect = asect->next)
2167 if (sect_syms[asect->index] != NULL
2168 && sect_syms[asect->index]->flags == 0)
2170 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2171 if (!sym_is_global (abfd, sect_syms[asect->index]))
2175 sect_syms[asect->index]->flags = 0;
2179 /* Now sort the symbols so the local symbols are first. */
2180 new_syms = ((asymbol **)
2182 (num_locals + num_globals) * sizeof (asymbol *)));
2183 if (new_syms == NULL)
2186 for (idx = 0; idx < symcount; idx++)
2188 asymbol *sym = syms[idx];
2191 if (!sym_is_global (abfd, sym))
2194 i = num_locals + num_globals2++;
2196 sym->udata.i = i + 1;
2198 for (asect = abfd->sections; asect; asect = asect->next)
2200 if (sect_syms[asect->index] != NULL
2201 && sect_syms[asect->index]->flags == 0)
2203 asymbol *sym = sect_syms[asect->index];
2206 sym->flags = BSF_SECTION_SYM;
2207 if (!sym_is_global (abfd, sym))
2210 i = num_locals + num_globals2++;
2212 sym->udata.i = i + 1;
2216 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2218 elf_num_locals (abfd) = num_locals;
2219 elf_num_globals (abfd) = num_globals;
2223 /* Align to the maximum file alignment that could be required for any
2224 ELF data structure. */
2226 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2227 static INLINE file_ptr
2228 align_file_position (off, align)
2232 return (off + align - 1) & ~(align - 1);
2235 /* Assign a file position to a section, optionally aligning to the
2236 required section alignment. */
2239 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2240 Elf_Internal_Shdr *i_shdrp;
2248 al = i_shdrp->sh_addralign;
2250 offset = BFD_ALIGN (offset, al);
2252 i_shdrp->sh_offset = offset;
2253 if (i_shdrp->bfd_section != NULL)
2254 i_shdrp->bfd_section->filepos = offset;
2255 if (i_shdrp->sh_type != SHT_NOBITS)
2256 offset += i_shdrp->sh_size;
2260 /* Compute the file positions we are going to put the sections at, and
2261 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2262 is not NULL, this is being called by the ELF backend linker. */
2265 _bfd_elf_compute_section_file_positions (abfd, link_info)
2267 struct bfd_link_info *link_info;
2269 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2271 struct bfd_strtab_hash *strtab;
2272 Elf_Internal_Shdr *shstrtab_hdr;
2274 if (abfd->output_has_begun)
2277 /* Do any elf backend specific processing first. */
2278 if (bed->elf_backend_begin_write_processing)
2279 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2281 if (! prep_headers (abfd))
2284 /* Post process the headers if necessary. */
2285 if (bed->elf_backend_post_process_headers)
2286 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2289 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2293 if (!assign_section_numbers (abfd))
2296 /* The backend linker builds symbol table information itself. */
2297 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2299 /* Non-zero if doing a relocatable link. */
2300 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2302 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2306 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2307 /* sh_name was set in prep_headers. */
2308 shstrtab_hdr->sh_type = SHT_STRTAB;
2309 shstrtab_hdr->sh_flags = 0;
2310 shstrtab_hdr->sh_addr = 0;
2311 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2312 shstrtab_hdr->sh_entsize = 0;
2313 shstrtab_hdr->sh_link = 0;
2314 shstrtab_hdr->sh_info = 0;
2315 /* sh_offset is set in assign_file_positions_except_relocs. */
2316 shstrtab_hdr->sh_addralign = 1;
2318 if (!assign_file_positions_except_relocs (abfd))
2321 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2324 Elf_Internal_Shdr *hdr;
2326 off = elf_tdata (abfd)->next_file_pos;
2328 hdr = &elf_tdata (abfd)->symtab_hdr;
2329 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2331 hdr = &elf_tdata (abfd)->strtab_hdr;
2332 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2334 elf_tdata (abfd)->next_file_pos = off;
2336 /* Now that we know where the .strtab section goes, write it
2338 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2339 || ! _bfd_stringtab_emit (abfd, strtab))
2341 _bfd_stringtab_free (strtab);
2344 abfd->output_has_begun = true;
2349 /* Create a mapping from a set of sections to a program segment. */
2351 static INLINE struct elf_segment_map *
2352 make_mapping (abfd, sections, from, to, phdr)
2354 asection **sections;
2359 struct elf_segment_map *m;
2363 m = ((struct elf_segment_map *)
2365 (sizeof (struct elf_segment_map)
2366 + (to - from - 1) * sizeof (asection *))));
2370 m->p_type = PT_LOAD;
2371 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2372 m->sections[i - from] = *hdrpp;
2373 m->count = to - from;
2375 if (from == 0 && phdr)
2377 /* Include the headers in the first PT_LOAD segment. */
2378 m->includes_filehdr = 1;
2379 m->includes_phdrs = 1;
2385 /* Set up a mapping from BFD sections to program segments. */
2388 map_sections_to_segments (abfd)
2391 asection **sections = NULL;
2395 struct elf_segment_map *mfirst;
2396 struct elf_segment_map **pm;
2397 struct elf_segment_map *m;
2399 unsigned int phdr_index;
2400 bfd_vma maxpagesize;
2402 boolean phdr_in_segment = true;
2406 if (elf_tdata (abfd)->segment_map != NULL)
2409 if (bfd_count_sections (abfd) == 0)
2412 /* Select the allocated sections, and sort them. */
2414 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2415 * sizeof (asection *));
2416 if (sections == NULL)
2420 for (s = abfd->sections; s != NULL; s = s->next)
2422 if ((s->flags & SEC_ALLOC) != 0)
2428 BFD_ASSERT (i <= bfd_count_sections (abfd));
2431 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2433 /* Build the mapping. */
2438 /* If we have a .interp section, then create a PT_PHDR segment for
2439 the program headers and a PT_INTERP segment for the .interp
2441 s = bfd_get_section_by_name (abfd, ".interp");
2442 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2444 m = ((struct elf_segment_map *)
2445 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2449 m->p_type = PT_PHDR;
2450 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2451 m->p_flags = PF_R | PF_X;
2452 m->p_flags_valid = 1;
2453 m->includes_phdrs = 1;
2458 m = ((struct elf_segment_map *)
2459 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2463 m->p_type = PT_INTERP;
2471 /* Look through the sections. We put sections in the same program
2472 segment when the start of the second section can be placed within
2473 a few bytes of the end of the first section. */
2476 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2478 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2480 && (dynsec->flags & SEC_LOAD) == 0)
2483 /* Deal with -Ttext or something similar such that the first section
2484 is not adjacent to the program headers. This is an
2485 approximation, since at this point we don't know exactly how many
2486 program headers we will need. */
2489 bfd_size_type phdr_size;
2491 phdr_size = elf_tdata (abfd)->program_header_size;
2493 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2494 if ((abfd->flags & D_PAGED) == 0
2495 || sections[0]->lma < phdr_size
2496 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2497 phdr_in_segment = false;
2500 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2503 boolean new_segment;
2507 /* See if this section and the last one will fit in the same
2510 if (last_hdr == NULL)
2512 /* If we don't have a segment yet, then we don't need a new
2513 one (we build the last one after this loop). */
2514 new_segment = false;
2516 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2518 /* If this section has a different relation between the
2519 virtual address and the load address, then we need a new
2523 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2524 < BFD_ALIGN (hdr->lma, maxpagesize))
2526 /* If putting this section in this segment would force us to
2527 skip a page in the segment, then we need a new segment. */
2530 else if ((last_hdr->flags & SEC_LOAD) == 0
2531 && (hdr->flags & SEC_LOAD) != 0)
2533 /* We don't want to put a loadable section after a
2534 nonloadable section in the same segment. */
2537 else if ((abfd->flags & D_PAGED) == 0)
2539 /* If the file is not demand paged, which means that we
2540 don't require the sections to be correctly aligned in the
2541 file, then there is no other reason for a new segment. */
2542 new_segment = false;
2545 && (hdr->flags & SEC_READONLY) == 0
2546 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2549 /* We don't want to put a writable section in a read only
2550 segment, unless they are on the same page in memory
2551 anyhow. We already know that the last section does not
2552 bring us past the current section on the page, so the
2553 only case in which the new section is not on the same
2554 page as the previous section is when the previous section
2555 ends precisely on a page boundary. */
2560 /* Otherwise, we can use the same segment. */
2561 new_segment = false;
2566 if ((hdr->flags & SEC_READONLY) == 0)
2572 /* We need a new program segment. We must create a new program
2573 header holding all the sections from phdr_index until hdr. */
2575 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2582 if ((hdr->flags & SEC_READONLY) == 0)
2589 phdr_in_segment = false;
2592 /* Create a final PT_LOAD program segment. */
2593 if (last_hdr != NULL)
2595 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2603 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2606 m = ((struct elf_segment_map *)
2607 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2611 m->p_type = PT_DYNAMIC;
2613 m->sections[0] = dynsec;
2619 /* For each loadable .note section, add a PT_NOTE segment. We don't
2620 use bfd_get_section_by_name, because if we link together
2621 nonloadable .note sections and loadable .note sections, we will
2622 generate two .note sections in the output file. FIXME: Using
2623 names for section types is bogus anyhow. */
2624 for (s = abfd->sections; s != NULL; s = s->next)
2626 if ((s->flags & SEC_LOAD) != 0
2627 && strncmp (s->name, ".note", 5) == 0)
2629 m = ((struct elf_segment_map *)
2630 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2634 m->p_type = PT_NOTE;
2646 elf_tdata (abfd)->segment_map = mfirst;
2650 if (sections != NULL)
2655 /* Sort sections by address. */
2658 elf_sort_sections (arg1, arg2)
2662 const asection *sec1 = *(const asection **) arg1;
2663 const asection *sec2 = *(const asection **) arg2;
2665 /* Sort by LMA first, since this is the address used to
2666 place the section into a segment. */
2667 if (sec1->lma < sec2->lma)
2669 else if (sec1->lma > sec2->lma)
2672 /* Then sort by VMA. Normally the LMA and the VMA will be
2673 the same, and this will do nothing. */
2674 if (sec1->vma < sec2->vma)
2676 else if (sec1->vma > sec2->vma)
2679 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2681 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2687 /* If the indicies are the same, do not return 0
2688 here, but continue to try the next comparison. */
2689 if (sec1->target_index - sec2->target_index != 0)
2690 return sec1->target_index - sec2->target_index;
2695 else if (TOEND (sec2))
2700 /* Sort by size, to put zero sized sections
2701 before others at the same address. */
2703 if (sec1->_raw_size < sec2->_raw_size)
2705 if (sec1->_raw_size > sec2->_raw_size)
2708 return sec1->target_index - sec2->target_index;
2711 /* Assign file positions to the sections based on the mapping from
2712 sections to segments. This function also sets up some fields in
2713 the file header, and writes out the program headers. */
2716 assign_file_positions_for_segments (abfd)
2719 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2721 struct elf_segment_map *m;
2723 Elf_Internal_Phdr *phdrs;
2725 bfd_vma filehdr_vaddr, filehdr_paddr;
2726 bfd_vma phdrs_vaddr, phdrs_paddr;
2727 Elf_Internal_Phdr *p;
2729 if (elf_tdata (abfd)->segment_map == NULL)
2731 if (! map_sections_to_segments (abfd))
2735 if (bed->elf_backend_modify_segment_map)
2737 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2742 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2745 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2746 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2747 elf_elfheader (abfd)->e_phnum = count;
2752 /* If we already counted the number of program segments, make sure
2753 that we allocated enough space. This happens when SIZEOF_HEADERS
2754 is used in a linker script. */
2755 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2756 if (alloc != 0 && count > alloc)
2758 ((*_bfd_error_handler)
2759 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2760 bfd_get_filename (abfd), alloc, count));
2761 bfd_set_error (bfd_error_bad_value);
2768 phdrs = ((Elf_Internal_Phdr *)
2769 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2773 off = bed->s->sizeof_ehdr;
2774 off += alloc * bed->s->sizeof_phdr;
2781 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2788 /* If elf_segment_map is not from map_sections_to_segments, the
2789 sections may not be correctly ordered. */
2791 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2794 p->p_type = m->p_type;
2795 p->p_flags = m->p_flags;
2797 if (p->p_type == PT_LOAD
2799 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2801 if ((abfd->flags & D_PAGED) != 0)
2802 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2805 bfd_size_type align;
2808 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2810 bfd_size_type secalign;
2812 secalign = bfd_get_section_alignment (abfd, *secpp);
2813 if (secalign > align)
2817 off += (m->sections[0]->vma - off) % (1 << align);
2824 p->p_vaddr = m->sections[0]->vma;
2826 if (m->p_paddr_valid)
2827 p->p_paddr = m->p_paddr;
2828 else if (m->count == 0)
2831 p->p_paddr = m->sections[0]->lma;
2833 if (p->p_type == PT_LOAD
2834 && (abfd->flags & D_PAGED) != 0)
2835 p->p_align = bed->maxpagesize;
2836 else if (m->count == 0)
2837 p->p_align = bed->s->file_align;
2845 if (m->includes_filehdr)
2847 if (! m->p_flags_valid)
2850 p->p_filesz = bed->s->sizeof_ehdr;
2851 p->p_memsz = bed->s->sizeof_ehdr;
2854 BFD_ASSERT (p->p_type == PT_LOAD);
2856 if (p->p_vaddr < (bfd_vma) off)
2858 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2859 bfd_get_filename (abfd));
2860 bfd_set_error (bfd_error_bad_value);
2865 if (! m->p_paddr_valid)
2868 if (p->p_type == PT_LOAD)
2870 filehdr_vaddr = p->p_vaddr;
2871 filehdr_paddr = p->p_paddr;
2875 if (m->includes_phdrs)
2877 if (! m->p_flags_valid)
2880 if (m->includes_filehdr)
2882 if (p->p_type == PT_LOAD)
2884 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2885 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2890 p->p_offset = bed->s->sizeof_ehdr;
2894 BFD_ASSERT (p->p_type == PT_LOAD);
2895 p->p_vaddr -= off - p->p_offset;
2896 if (! m->p_paddr_valid)
2897 p->p_paddr -= off - p->p_offset;
2900 if (p->p_type == PT_LOAD)
2902 phdrs_vaddr = p->p_vaddr;
2903 phdrs_paddr = p->p_paddr;
2906 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2909 p->p_filesz += alloc * bed->s->sizeof_phdr;
2910 p->p_memsz += alloc * bed->s->sizeof_phdr;
2913 if (p->p_type == PT_LOAD
2914 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2916 if (! m->includes_filehdr && ! m->includes_phdrs)
2922 adjust = off - (p->p_offset + p->p_filesz);
2923 p->p_filesz += adjust;
2924 p->p_memsz += adjust;
2930 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2934 bfd_size_type align;
2938 align = 1 << bfd_get_section_alignment (abfd, sec);
2940 /* The section may have artificial alignment forced by a
2941 link script. Notice this case by the gap between the
2942 cumulative phdr vma and the section's vma. */
2943 if (p->p_vaddr + p->p_memsz < sec->vma)
2945 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2947 p->p_memsz += adjust;
2950 if ((flags & SEC_LOAD) != 0)
2951 p->p_filesz += adjust;
2954 if (p->p_type == PT_LOAD)
2956 bfd_signed_vma adjust;
2958 if ((flags & SEC_LOAD) != 0)
2960 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2964 else if ((flags & SEC_ALLOC) != 0)
2966 /* The section VMA must equal the file position
2967 modulo the page size. FIXME: I'm not sure if
2968 this adjustment is really necessary. We used to
2969 not have the SEC_LOAD case just above, and then
2970 this was necessary, but now I'm not sure. */
2971 if ((abfd->flags & D_PAGED) != 0)
2972 adjust = (sec->vma - voff) % bed->maxpagesize;
2974 adjust = (sec->vma - voff) % align;
2983 (* _bfd_error_handler)
2984 (_("Error: First section in segment (%s) starts at 0x%x"),
2985 bfd_section_name (abfd, sec), sec->lma);
2986 (* _bfd_error_handler)
2987 (_(" whereas segment starts at 0x%x"),
2992 p->p_memsz += adjust;
2995 if ((flags & SEC_LOAD) != 0)
2996 p->p_filesz += adjust;
3001 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3002 used in a linker script we may have a section with
3003 SEC_LOAD clear but which is supposed to have
3005 if ((flags & SEC_LOAD) != 0
3006 || (flags & SEC_HAS_CONTENTS) != 0)
3007 off += sec->_raw_size;
3009 if ((flags & SEC_ALLOC) != 0)
3010 voff += sec->_raw_size;
3013 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3015 /* The actual "note" segment has i == 0.
3016 This is the one that actually contains everything. */
3020 p->p_filesz = sec->_raw_size;
3021 off += sec->_raw_size;
3026 /* Fake sections -- don't need to be written. */
3029 flags = sec->flags = 0;
3036 p->p_memsz += sec->_raw_size;
3038 if ((flags & SEC_LOAD) != 0)
3039 p->p_filesz += sec->_raw_size;
3041 if (align > p->p_align
3042 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3046 if (! m->p_flags_valid)
3049 if ((flags & SEC_CODE) != 0)
3051 if ((flags & SEC_READONLY) == 0)
3057 /* Now that we have set the section file positions, we can set up
3058 the file positions for the non PT_LOAD segments. */
3059 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3063 if (p->p_type != PT_LOAD && m->count > 0)
3065 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3066 p->p_offset = m->sections[0]->filepos;
3070 if (m->includes_filehdr)
3072 p->p_vaddr = filehdr_vaddr;
3073 if (! m->p_paddr_valid)
3074 p->p_paddr = filehdr_paddr;
3076 else if (m->includes_phdrs)
3078 p->p_vaddr = phdrs_vaddr;
3079 if (! m->p_paddr_valid)
3080 p->p_paddr = phdrs_paddr;
3085 /* Clear out any program headers we allocated but did not use. */
3086 for (; count < alloc; count++, p++)
3088 memset (p, 0, sizeof *p);
3089 p->p_type = PT_NULL;
3092 elf_tdata (abfd)->phdr = phdrs;
3094 elf_tdata (abfd)->next_file_pos = off;
3096 /* Write out the program headers. */
3097 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3098 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3104 /* Get the size of the program header.
3106 If this is called by the linker before any of the section VMA's are set, it
3107 can't calculate the correct value for a strange memory layout. This only
3108 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3109 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3110 data segment (exclusive of .interp and .dynamic).
3112 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3113 will be two segments. */
3115 static bfd_size_type
3116 get_program_header_size (abfd)
3121 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3123 /* We can't return a different result each time we're called. */
3124 if (elf_tdata (abfd)->program_header_size != 0)
3125 return elf_tdata (abfd)->program_header_size;
3127 if (elf_tdata (abfd)->segment_map != NULL)
3129 struct elf_segment_map *m;
3132 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3134 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3135 return elf_tdata (abfd)->program_header_size;
3138 /* Assume we will need exactly two PT_LOAD segments: one for text
3139 and one for data. */
3142 s = bfd_get_section_by_name (abfd, ".interp");
3143 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3145 /* If we have a loadable interpreter section, we need a
3146 PT_INTERP segment. In this case, assume we also need a
3147 PT_PHDR segment, although that may not be true for all
3152 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3154 /* We need a PT_DYNAMIC segment. */
3158 for (s = abfd->sections; s != NULL; s = s->next)
3160 if ((s->flags & SEC_LOAD) != 0
3161 && strncmp (s->name, ".note", 5) == 0)
3163 /* We need a PT_NOTE segment. */
3168 /* Let the backend count up any program headers it might need. */
3169 if (bed->elf_backend_additional_program_headers)
3173 a = (*bed->elf_backend_additional_program_headers) (abfd);
3179 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3180 return elf_tdata (abfd)->program_header_size;
3183 /* Work out the file positions of all the sections. This is called by
3184 _bfd_elf_compute_section_file_positions. All the section sizes and
3185 VMAs must be known before this is called.
3187 We do not consider reloc sections at this point, unless they form
3188 part of the loadable image. Reloc sections are assigned file
3189 positions in assign_file_positions_for_relocs, which is called by
3190 write_object_contents and final_link.
3192 We also don't set the positions of the .symtab and .strtab here. */
3195 assign_file_positions_except_relocs (abfd)
3198 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3199 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3200 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3202 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3204 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3205 && bfd_get_format (abfd) != bfd_core)
3207 Elf_Internal_Shdr **hdrpp;
3210 /* Start after the ELF header. */
3211 off = i_ehdrp->e_ehsize;
3213 /* We are not creating an executable, which means that we are
3214 not creating a program header, and that the actual order of
3215 the sections in the file is unimportant. */
3216 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3218 Elf_Internal_Shdr *hdr;
3221 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3223 hdr->sh_offset = -1;
3226 if (i == tdata->symtab_section
3227 || i == tdata->strtab_section)
3229 hdr->sh_offset = -1;
3233 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3239 Elf_Internal_Shdr **hdrpp;
3241 /* Assign file positions for the loaded sections based on the
3242 assignment of sections to segments. */
3243 if (! assign_file_positions_for_segments (abfd))
3246 /* Assign file positions for the other sections. */
3248 off = elf_tdata (abfd)->next_file_pos;
3249 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3251 Elf_Internal_Shdr *hdr;
3254 if (hdr->bfd_section != NULL
3255 && hdr->bfd_section->filepos != 0)
3256 hdr->sh_offset = hdr->bfd_section->filepos;
3257 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3259 ((*_bfd_error_handler)
3260 (_("%s: warning: allocated section `%s' not in segment"),
3261 bfd_get_filename (abfd),
3262 (hdr->bfd_section == NULL
3264 : hdr->bfd_section->name)));
3265 if ((abfd->flags & D_PAGED) != 0)
3266 off += (hdr->sh_addr - off) % bed->maxpagesize;
3268 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3269 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3272 else if (hdr->sh_type == SHT_REL
3273 || hdr->sh_type == SHT_RELA
3274 || hdr == i_shdrpp[tdata->symtab_section]
3275 || hdr == i_shdrpp[tdata->strtab_section])
3276 hdr->sh_offset = -1;
3278 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3282 /* Place the section headers. */
3283 off = align_file_position (off, bed->s->file_align);
3284 i_ehdrp->e_shoff = off;
3285 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3287 elf_tdata (abfd)->next_file_pos = off;
3296 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3297 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3298 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3300 struct bfd_strtab_hash *shstrtab;
3301 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3303 i_ehdrp = elf_elfheader (abfd);
3304 i_shdrp = elf_elfsections (abfd);
3306 shstrtab = _bfd_elf_stringtab_init ();
3307 if (shstrtab == NULL)
3310 elf_shstrtab (abfd) = shstrtab;
3312 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3313 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3314 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3315 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3317 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3318 i_ehdrp->e_ident[EI_DATA] =
3319 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3320 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3322 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3323 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3325 for (count = EI_PAD; count < EI_NIDENT; count++)
3326 i_ehdrp->e_ident[count] = 0;
3328 if ((abfd->flags & DYNAMIC) != 0)
3329 i_ehdrp->e_type = ET_DYN;
3330 else if ((abfd->flags & EXEC_P) != 0)
3331 i_ehdrp->e_type = ET_EXEC;
3332 else if (bfd_get_format (abfd) == bfd_core)
3333 i_ehdrp->e_type = ET_CORE;
3335 i_ehdrp->e_type = ET_REL;
3337 switch (bfd_get_arch (abfd))
3339 case bfd_arch_unknown:
3340 i_ehdrp->e_machine = EM_NONE;
3342 case bfd_arch_sparc:
3343 if (bfd_get_arch_size (abfd) == 64)
3344 i_ehdrp->e_machine = EM_SPARCV9;
3346 i_ehdrp->e_machine = EM_SPARC;
3349 i_ehdrp->e_machine = EM_S370;
3352 if (bfd_get_arch_size (abfd) == 64)
3353 i_ehdrp->e_machine = EM_X86_64;
3355 i_ehdrp->e_machine = EM_386;
3358 i_ehdrp->e_machine = EM_IA_64;
3360 case bfd_arch_m68hc11:
3361 i_ehdrp->e_machine = EM_68HC11;
3363 case bfd_arch_m68hc12:
3364 i_ehdrp->e_machine = EM_68HC12;
3367 i_ehdrp->e_machine = EM_S390;
3370 i_ehdrp->e_machine = EM_68K;
3373 i_ehdrp->e_machine = EM_88K;
3376 i_ehdrp->e_machine = EM_860;
3379 i_ehdrp->e_machine = EM_960;
3381 case bfd_arch_mips: /* MIPS Rxxxx */
3382 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3385 i_ehdrp->e_machine = EM_PARISC;
3387 case bfd_arch_powerpc:
3388 i_ehdrp->e_machine = EM_PPC;
3390 case bfd_arch_alpha:
3391 i_ehdrp->e_machine = EM_ALPHA;
3394 i_ehdrp->e_machine = EM_SH;
3397 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3400 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3403 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3405 case bfd_arch_mcore:
3406 i_ehdrp->e_machine = EM_MCORE;
3409 i_ehdrp->e_machine = EM_AVR;
3412 switch (bfd_get_mach (abfd))
3415 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3419 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3422 i_ehdrp->e_machine = EM_ARM;
3425 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3427 case bfd_arch_mn10200:
3428 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3430 case bfd_arch_mn10300:
3431 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3434 i_ehdrp->e_machine = EM_PJ;
3437 i_ehdrp->e_machine = EM_CRIS;
3439 case bfd_arch_openrisc:
3440 i_ehdrp->e_machine = EM_OPENRISC;
3442 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3444 i_ehdrp->e_machine = EM_NONE;
3446 i_ehdrp->e_version = bed->s->ev_current;
3447 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3449 /* No program header, for now. */
3450 i_ehdrp->e_phoff = 0;
3451 i_ehdrp->e_phentsize = 0;
3452 i_ehdrp->e_phnum = 0;
3454 /* Each bfd section is section header entry. */
3455 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3456 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3458 /* If we're building an executable, we'll need a program header table. */
3459 if (abfd->flags & EXEC_P)
3461 /* It all happens later. */
3463 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3465 /* elf_build_phdrs() returns a (NULL-terminated) array of
3466 Elf_Internal_Phdrs. */
3467 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3468 i_ehdrp->e_phoff = outbase;
3469 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3474 i_ehdrp->e_phentsize = 0;
3476 i_ehdrp->e_phoff = 0;
3479 elf_tdata (abfd)->symtab_hdr.sh_name =
3480 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3481 elf_tdata (abfd)->strtab_hdr.sh_name =
3482 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3483 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3484 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3485 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3486 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3487 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3493 /* Assign file positions for all the reloc sections which are not part
3494 of the loadable file image. */
3497 _bfd_elf_assign_file_positions_for_relocs (abfd)
3502 Elf_Internal_Shdr **shdrpp;
3504 off = elf_tdata (abfd)->next_file_pos;
3506 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3507 i < elf_elfheader (abfd)->e_shnum;
3510 Elf_Internal_Shdr *shdrp;
3513 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3514 && shdrp->sh_offset == -1)
3515 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3518 elf_tdata (abfd)->next_file_pos = off;
3522 _bfd_elf_write_object_contents (abfd)
3525 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3526 Elf_Internal_Ehdr *i_ehdrp;
3527 Elf_Internal_Shdr **i_shdrp;
3531 if (! abfd->output_has_begun
3532 && ! _bfd_elf_compute_section_file_positions
3533 (abfd, (struct bfd_link_info *) NULL))
3536 i_shdrp = elf_elfsections (abfd);
3537 i_ehdrp = elf_elfheader (abfd);
3540 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3544 _bfd_elf_assign_file_positions_for_relocs (abfd);
3546 /* After writing the headers, we need to write the sections too... */
3547 for (count = 1; count < i_ehdrp->e_shnum; count++)
3549 if (bed->elf_backend_section_processing)
3550 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3551 if (i_shdrp[count]->contents)
3553 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3554 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3556 != i_shdrp[count]->sh_size))
3561 /* Write out the section header names. */
3562 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3563 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3566 if (bed->elf_backend_final_write_processing)
3567 (*bed->elf_backend_final_write_processing) (abfd,
3568 elf_tdata (abfd)->linker);
3570 return bed->s->write_shdrs_and_ehdr (abfd);
3574 _bfd_elf_write_corefile_contents (abfd)
3577 /* Hopefully this can be done just like an object file. */
3578 return _bfd_elf_write_object_contents (abfd);
3581 /* Given a section, search the header to find them. */
3584 _bfd_elf_section_from_bfd_section (abfd, asect)
3588 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3589 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3591 Elf_Internal_Shdr *hdr;
3592 int maxindex = elf_elfheader (abfd)->e_shnum;
3594 for (index = 0; index < maxindex; index++)
3596 hdr = i_shdrp[index];
3597 if (hdr->bfd_section == asect)
3601 if (bed->elf_backend_section_from_bfd_section)
3603 for (index = 0; index < maxindex; index++)
3607 hdr = i_shdrp[index];
3609 if ((*bed->elf_backend_section_from_bfd_section)
3610 (abfd, hdr, asect, &retval))
3615 if (bfd_is_abs_section (asect))
3617 if (bfd_is_com_section (asect))
3619 if (bfd_is_und_section (asect))
3622 bfd_set_error (bfd_error_nonrepresentable_section);
3627 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3631 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3633 asymbol **asym_ptr_ptr;
3635 asymbol *asym_ptr = *asym_ptr_ptr;
3637 flagword flags = asym_ptr->flags;
3639 /* When gas creates relocations against local labels, it creates its
3640 own symbol for the section, but does put the symbol into the
3641 symbol chain, so udata is 0. When the linker is generating
3642 relocatable output, this section symbol may be for one of the
3643 input sections rather than the output section. */
3644 if (asym_ptr->udata.i == 0
3645 && (flags & BSF_SECTION_SYM)
3646 && asym_ptr->section)
3650 if (asym_ptr->section->output_section != NULL)
3651 indx = asym_ptr->section->output_section->index;
3653 indx = asym_ptr->section->index;
3654 if (elf_section_syms (abfd)[indx])
3655 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3658 idx = asym_ptr->udata.i;
3662 /* This case can occur when using --strip-symbol on a symbol
3663 which is used in a relocation entry. */
3664 (*_bfd_error_handler)
3665 (_("%s: symbol `%s' required but not present"),
3666 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3667 bfd_set_error (bfd_error_no_symbols);
3674 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3675 (long) asym_ptr, asym_ptr->name, idx, flags,
3676 elf_symbol_flags (flags));
3684 /* Copy private BFD data. This copies any program header information. */
3687 copy_private_bfd_data (ibfd, obfd)
3691 Elf_Internal_Ehdr * iehdr;
3692 struct elf_segment_map * map;
3693 struct elf_segment_map * map_first;
3694 struct elf_segment_map ** pointer_to_map;
3695 Elf_Internal_Phdr * segment;
3698 unsigned int num_segments;
3699 boolean phdr_included = false;
3700 bfd_vma maxpagesize;
3701 struct elf_segment_map * phdr_adjust_seg = NULL;
3702 unsigned int phdr_adjust_num = 0;
3704 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3705 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3708 if (elf_tdata (ibfd)->phdr == NULL)
3711 iehdr = elf_elfheader (ibfd);
3714 pointer_to_map = &map_first;
3716 num_segments = elf_elfheader (ibfd)->e_phnum;
3717 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3719 /* Returns the end address of the segment + 1. */
3720 #define SEGMENT_END(segment, start) \
3721 (start + (segment->p_memsz > segment->p_filesz \
3722 ? segment->p_memsz : segment->p_filesz))
3724 /* Returns true if the given section is contained within
3725 the given segment. VMA addresses are compared. */
3726 #define IS_CONTAINED_BY_VMA(section, segment) \
3727 (section->vma >= segment->p_vaddr \
3728 && (section->vma + section->_raw_size) \
3729 <= (SEGMENT_END (segment, segment->p_vaddr)))
3731 /* Returns true if the given section is contained within
3732 the given segment. LMA addresses are compared. */
3733 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3734 (section->lma >= base \
3735 && (section->lma + section->_raw_size) \
3736 <= SEGMENT_END (segment, base))
3738 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3739 #define IS_COREFILE_NOTE(p, s) \
3740 (p->p_type == PT_NOTE \
3741 && bfd_get_format (ibfd) == bfd_core \
3742 && s->vma == 0 && s->lma == 0 \
3743 && (bfd_vma) s->filepos >= p->p_offset \
3744 && (bfd_vma) s->filepos + s->_raw_size \
3745 <= p->p_offset + p->p_filesz)
3747 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3748 linker, which generates a PT_INTERP section with p_vaddr and
3749 p_memsz set to 0. */
3750 #define IS_SOLARIS_PT_INTERP(p, s) \
3752 && p->p_filesz > 0 \
3753 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3754 && s->_raw_size > 0 \
3755 && (bfd_vma) s->filepos >= p->p_offset \
3756 && ((bfd_vma) s->filepos + s->_raw_size \
3757 <= p->p_offset + p->p_filesz))
3759 /* Decide if the given section should be included in the given segment.
3760 A section will be included if:
3761 1. It is within the address space of the segment,
3762 2. It is an allocated segment,
3763 3. There is an output section associated with it,
3764 4. The section has not already been allocated to a previous segment. */
3765 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3766 ((((IS_CONTAINED_BY_VMA (section, segment) \
3767 || IS_SOLARIS_PT_INTERP (segment, section)) \
3768 && (section->flags & SEC_ALLOC) != 0) \
3769 || IS_COREFILE_NOTE (segment, section)) \
3770 && section->output_section != NULL \
3771 && section->segment_mark == false)
3773 /* Returns true iff seg1 starts after the end of seg2. */
3774 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3775 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3777 /* Returns true iff seg1 and seg2 overlap. */
3778 #define SEGMENT_OVERLAPS(seg1, seg2) \
3779 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3781 /* Initialise the segment mark field. */
3782 for (section = ibfd->sections; section != NULL; section = section->next)
3783 section->segment_mark = false;
3785 /* Scan through the segments specified in the program header
3786 of the input BFD. For this first scan we look for overlaps
3787 in the loadable segments. These can be created by wierd
3788 parameters to objcopy. */
3789 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3794 Elf_Internal_Phdr *segment2;
3796 if (segment->p_type != PT_LOAD)
3799 /* Determine if this segment overlaps any previous segments. */
3800 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3802 bfd_signed_vma extra_length;
3804 if (segment2->p_type != PT_LOAD
3805 || ! SEGMENT_OVERLAPS (segment, segment2))
3808 /* Merge the two segments together. */
3809 if (segment2->p_vaddr < segment->p_vaddr)
3811 /* Extend SEGMENT2 to include SEGMENT and then delete
3814 SEGMENT_END (segment, segment->p_vaddr)
3815 - SEGMENT_END (segment2, segment2->p_vaddr);
3817 if (extra_length > 0)
3819 segment2->p_memsz += extra_length;
3820 segment2->p_filesz += extra_length;
3823 segment->p_type = PT_NULL;
3825 /* Since we have deleted P we must restart the outer loop. */
3827 segment = elf_tdata (ibfd)->phdr;
3832 /* Extend SEGMENT to include SEGMENT2 and then delete
3835 SEGMENT_END (segment2, segment2->p_vaddr)
3836 - SEGMENT_END (segment, segment->p_vaddr);
3838 if (extra_length > 0)
3840 segment->p_memsz += extra_length;
3841 segment->p_filesz += extra_length;
3844 segment2->p_type = PT_NULL;
3849 /* The second scan attempts to assign sections to segments. */
3850 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3854 unsigned int section_count;
3855 asection ** sections;
3856 asection * output_section;
3858 bfd_vma matching_lma;
3859 bfd_vma suggested_lma;
3862 if (segment->p_type == PT_NULL)
3865 /* Compute how many sections might be placed into this segment. */
3867 for (section = ibfd->sections; section != NULL; section = section->next)
3868 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3871 /* Allocate a segment map big enough to contain all of the
3872 sections we have selected. */
3873 map = ((struct elf_segment_map *)
3875 (sizeof (struct elf_segment_map)
3876 + ((size_t) section_count - 1) * sizeof (asection *))));
3880 /* Initialise the fields of the segment map. Default to
3881 using the physical address of the segment in the input BFD. */
3883 map->p_type = segment->p_type;
3884 map->p_flags = segment->p_flags;
3885 map->p_flags_valid = 1;
3886 map->p_paddr = segment->p_paddr;
3887 map->p_paddr_valid = 1;
3889 /* Determine if this segment contains the ELF file header
3890 and if it contains the program headers themselves. */
3891 map->includes_filehdr = (segment->p_offset == 0
3892 && segment->p_filesz >= iehdr->e_ehsize);
3894 map->includes_phdrs = 0;
3896 if (! phdr_included || segment->p_type != PT_LOAD)
3898 map->includes_phdrs =
3899 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3900 && (segment->p_offset + segment->p_filesz
3901 >= ((bfd_vma) iehdr->e_phoff
3902 + iehdr->e_phnum * iehdr->e_phentsize)));
3904 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3905 phdr_included = true;
3908 if (section_count == 0)
3910 /* Special segments, such as the PT_PHDR segment, may contain
3911 no sections, but ordinary, loadable segments should contain
3913 if (segment->p_type == PT_LOAD)
3915 (_("%s: warning: Empty loadable segment detected\n"),
3916 bfd_get_filename (ibfd));
3919 *pointer_to_map = map;
3920 pointer_to_map = &map->next;
3925 /* Now scan the sections in the input BFD again and attempt
3926 to add their corresponding output sections to the segment map.
3927 The problem here is how to handle an output section which has
3928 been moved (ie had its LMA changed). There are four possibilities:
3930 1. None of the sections have been moved.
3931 In this case we can continue to use the segment LMA from the
3934 2. All of the sections have been moved by the same amount.
3935 In this case we can change the segment's LMA to match the LMA
3936 of the first section.
3938 3. Some of the sections have been moved, others have not.
3939 In this case those sections which have not been moved can be
3940 placed in the current segment which will have to have its size,
3941 and possibly its LMA changed, and a new segment or segments will
3942 have to be created to contain the other sections.
3944 4. The sections have been moved, but not be the same amount.
3945 In this case we can change the segment's LMA to match the LMA
3946 of the first section and we will have to create a new segment
3947 or segments to contain the other sections.
3949 In order to save time, we allocate an array to hold the section
3950 pointers that we are interested in. As these sections get assigned
3951 to a segment, they are removed from this array. */
3953 sections = (asection **) bfd_malloc
3954 (sizeof (asection *) * section_count);
3955 if (sections == NULL)
3958 /* Step One: Scan for segment vs section LMA conflicts.
3959 Also add the sections to the section array allocated above.
3960 Also add the sections to the current segment. In the common
3961 case, where the sections have not been moved, this means that
3962 we have completely filled the segment, and there is nothing
3968 for (j = 0, section = ibfd->sections;
3970 section = section->next)
3972 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3974 output_section = section->output_section;
3976 sections[j ++] = section;
3978 /* The Solaris native linker always sets p_paddr to 0.
3979 We try to catch that case here, and set it to the
3981 if (segment->p_paddr == 0
3982 && segment->p_vaddr != 0
3984 && output_section->lma != 0
3985 && (output_section->vma == (segment->p_vaddr
3986 + (map->includes_filehdr
3989 + (map->includes_phdrs
3990 ? iehdr->e_phnum * iehdr->e_phentsize
3992 map->p_paddr = segment->p_vaddr;
3994 /* Match up the physical address of the segment with the
3995 LMA address of the output section. */
3996 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3997 || IS_COREFILE_NOTE (segment, section))
3999 if (matching_lma == 0)
4000 matching_lma = output_section->lma;
4002 /* We assume that if the section fits within the segment
4003 then it does not overlap any other section within that
4005 map->sections[isec ++] = output_section;
4007 else if (suggested_lma == 0)
4008 suggested_lma = output_section->lma;
4012 BFD_ASSERT (j == section_count);
4014 /* Step Two: Adjust the physical address of the current segment,
4016 if (isec == section_count)
4018 /* All of the sections fitted within the segment as currently
4019 specified. This is the default case. Add the segment to
4020 the list of built segments and carry on to process the next
4021 program header in the input BFD. */
4022 map->count = section_count;
4023 *pointer_to_map = map;
4024 pointer_to_map = &map->next;
4031 if (matching_lma != 0)
4033 /* At least one section fits inside the current segment.
4034 Keep it, but modify its physical address to match the
4035 LMA of the first section that fitted. */
4036 map->p_paddr = matching_lma;
4040 /* None of the sections fitted inside the current segment.
4041 Change the current segment's physical address to match
4042 the LMA of the first section. */
4043 map->p_paddr = suggested_lma;
4046 /* Offset the segment physical address from the lma
4047 to allow for space taken up by elf headers. */
4048 if (map->includes_filehdr)
4049 map->p_paddr -= iehdr->e_ehsize;
4051 if (map->includes_phdrs)
4053 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4055 /* iehdr->e_phnum is just an estimate of the number
4056 of program headers that we will need. Make a note
4057 here of the number we used and the segment we chose
4058 to hold these headers, so that we can adjust the
4059 offset when we know the correct value. */
4060 phdr_adjust_num = iehdr->e_phnum;
4061 phdr_adjust_seg = map;
4065 /* Step Three: Loop over the sections again, this time assigning
4066 those that fit to the current segment and remvoing them from the
4067 sections array; but making sure not to leave large gaps. Once all
4068 possible sections have been assigned to the current segment it is
4069 added to the list of built segments and if sections still remain
4070 to be assigned, a new segment is constructed before repeating
4078 /* Fill the current segment with sections that fit. */
4079 for (j = 0; j < section_count; j++)
4081 section = sections[j];
4083 if (section == NULL)
4086 output_section = section->output_section;
4088 BFD_ASSERT (output_section != NULL);
4090 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4091 || IS_COREFILE_NOTE (segment, section))
4093 if (map->count == 0)
4095 /* If the first section in a segment does not start at
4096 the beginning of the segment, then something is
4098 if (output_section->lma !=
4100 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4101 + (map->includes_phdrs
4102 ? iehdr->e_phnum * iehdr->e_phentsize
4108 asection * prev_sec;
4110 prev_sec = map->sections[map->count - 1];
4112 /* If the gap between the end of the previous section
4113 and the start of this section is more than
4114 maxpagesize then we need to start a new segment. */
4115 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4116 < BFD_ALIGN (output_section->lma, maxpagesize))
4117 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4119 if (suggested_lma == 0)
4120 suggested_lma = output_section->lma;
4126 map->sections[map->count++] = output_section;
4129 section->segment_mark = true;
4131 else if (suggested_lma == 0)
4132 suggested_lma = output_section->lma;
4135 BFD_ASSERT (map->count > 0);
4137 /* Add the current segment to the list of built segments. */
4138 *pointer_to_map = map;
4139 pointer_to_map = &map->next;
4141 if (isec < section_count)
4143 /* We still have not allocated all of the sections to
4144 segments. Create a new segment here, initialise it
4145 and carry on looping. */
4146 map = ((struct elf_segment_map *)
4148 (sizeof (struct elf_segment_map)
4149 + ((size_t) section_count - 1)
4150 * sizeof (asection *))));
4154 /* Initialise the fields of the segment map. Set the physical
4155 physical address to the LMA of the first section that has
4156 not yet been assigned. */
4158 map->p_type = segment->p_type;
4159 map->p_flags = segment->p_flags;
4160 map->p_flags_valid = 1;
4161 map->p_paddr = suggested_lma;
4162 map->p_paddr_valid = 1;
4163 map->includes_filehdr = 0;
4164 map->includes_phdrs = 0;
4167 while (isec < section_count);
4172 /* The Solaris linker creates program headers in which all the
4173 p_paddr fields are zero. When we try to objcopy or strip such a
4174 file, we get confused. Check for this case, and if we find it
4175 reset the p_paddr_valid fields. */
4176 for (map = map_first; map != NULL; map = map->next)
4177 if (map->p_paddr != 0)
4181 for (map = map_first; map != NULL; map = map->next)
4182 map->p_paddr_valid = 0;
4185 elf_tdata (obfd)->segment_map = map_first;
4187 /* If we had to estimate the number of program headers that were
4188 going to be needed, then check our estimate know and adjust
4189 the offset if necessary. */
4190 if (phdr_adjust_seg != NULL)
4194 for (count = 0, map = map_first; map != NULL; map = map->next)
4197 if (count > phdr_adjust_num)
4198 phdr_adjust_seg->p_paddr
4199 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4203 /* Final Step: Sort the segments into ascending order of physical
4205 if (map_first != NULL)
4207 struct elf_segment_map *prev;
4210 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4212 /* Yes I know - its a bubble sort.... */
4213 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4215 /* Swap map and map->next. */
4216 prev->next = map->next;
4217 map->next = map->next->next;
4218 prev->next->next = map;
4228 #undef IS_CONTAINED_BY_VMA
4229 #undef IS_CONTAINED_BY_LMA
4230 #undef IS_COREFILE_NOTE
4231 #undef IS_SOLARIS_PT_INTERP
4232 #undef INCLUDE_SECTION_IN_SEGMENT
4233 #undef SEGMENT_AFTER_SEGMENT
4234 #undef SEGMENT_OVERLAPS
4238 /* Copy private section information. This copies over the entsize
4239 field, and sometimes the info field. */
4242 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4248 Elf_Internal_Shdr *ihdr, *ohdr;
4250 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4251 || obfd->xvec->flavour != bfd_target_elf_flavour)
4254 /* Copy over private BFD data if it has not already been copied.
4255 This must be done here, rather than in the copy_private_bfd_data
4256 entry point, because the latter is called after the section
4257 contents have been set, which means that the program headers have
4258 already been worked out. */
4259 if (elf_tdata (obfd)->segment_map == NULL
4260 && elf_tdata (ibfd)->phdr != NULL)
4264 /* Only set up the segments if there are no more SEC_ALLOC
4265 sections. FIXME: This won't do the right thing if objcopy is
4266 used to remove the last SEC_ALLOC section, since objcopy
4267 won't call this routine in that case. */
4268 for (s = isec->next; s != NULL; s = s->next)
4269 if ((s->flags & SEC_ALLOC) != 0)
4273 if (! copy_private_bfd_data (ibfd, obfd))
4278 ihdr = &elf_section_data (isec)->this_hdr;
4279 ohdr = &elf_section_data (osec)->this_hdr;
4281 ohdr->sh_entsize = ihdr->sh_entsize;
4283 if (ihdr->sh_type == SHT_SYMTAB
4284 || ihdr->sh_type == SHT_DYNSYM
4285 || ihdr->sh_type == SHT_GNU_verneed
4286 || ihdr->sh_type == SHT_GNU_verdef)
4287 ohdr->sh_info = ihdr->sh_info;
4289 elf_section_data (osec)->use_rela_p
4290 = elf_section_data (isec)->use_rela_p;
4295 /* Copy private symbol information. If this symbol is in a section
4296 which we did not map into a BFD section, try to map the section
4297 index correctly. We use special macro definitions for the mapped
4298 section indices; these definitions are interpreted by the
4299 swap_out_syms function. */
4301 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4302 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4303 #define MAP_STRTAB (SHN_LORESERVE - 3)
4304 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4307 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4313 elf_symbol_type *isym, *osym;
4315 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4316 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4319 isym = elf_symbol_from (ibfd, isymarg);
4320 osym = elf_symbol_from (obfd, osymarg);
4324 && bfd_is_abs_section (isym->symbol.section))
4328 shndx = isym->internal_elf_sym.st_shndx;
4329 if (shndx == elf_onesymtab (ibfd))
4330 shndx = MAP_ONESYMTAB;
4331 else if (shndx == elf_dynsymtab (ibfd))
4332 shndx = MAP_DYNSYMTAB;
4333 else if (shndx == elf_tdata (ibfd)->strtab_section)
4335 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4336 shndx = MAP_SHSTRTAB;
4337 osym->internal_elf_sym.st_shndx = shndx;
4343 /* Swap out the symbols. */
4346 swap_out_syms (abfd, sttp, relocatable_p)
4348 struct bfd_strtab_hash **sttp;
4351 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4353 if (!elf_map_symbols (abfd))
4356 /* Dump out the symtabs. */
4358 int symcount = bfd_get_symcount (abfd);
4359 asymbol **syms = bfd_get_outsymbols (abfd);
4360 struct bfd_strtab_hash *stt;
4361 Elf_Internal_Shdr *symtab_hdr;
4362 Elf_Internal_Shdr *symstrtab_hdr;
4363 char *outbound_syms;
4366 stt = _bfd_elf_stringtab_init ();
4370 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4371 symtab_hdr->sh_type = SHT_SYMTAB;
4372 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4373 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4374 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4375 symtab_hdr->sh_addralign = bed->s->file_align;
4377 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4378 symstrtab_hdr->sh_type = SHT_STRTAB;
4380 outbound_syms = bfd_alloc (abfd,
4381 (1 + symcount) * bed->s->sizeof_sym);
4382 if (outbound_syms == NULL)
4384 symtab_hdr->contents = (PTR) outbound_syms;
4386 /* now generate the data (for "contents") */
4388 /* Fill in zeroth symbol and swap it out. */
4389 Elf_Internal_Sym sym;
4395 sym.st_shndx = SHN_UNDEF;
4396 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4397 outbound_syms += bed->s->sizeof_sym;
4399 for (idx = 0; idx < symcount; idx++)
4401 Elf_Internal_Sym sym;
4402 bfd_vma value = syms[idx]->value;
4403 elf_symbol_type *type_ptr;
4404 flagword flags = syms[idx]->flags;
4407 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4409 /* Local section symbols have no name. */
4414 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4417 if (sym.st_name == (unsigned long) -1)
4421 type_ptr = elf_symbol_from (abfd, syms[idx]);
4423 if ((flags & BSF_SECTION_SYM) == 0
4424 && bfd_is_com_section (syms[idx]->section))
4426 /* ELF common symbols put the alignment into the `value' field,
4427 and the size into the `size' field. This is backwards from
4428 how BFD handles it, so reverse it here. */
4429 sym.st_size = value;
4430 if (type_ptr == NULL
4431 || type_ptr->internal_elf_sym.st_value == 0)
4432 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4434 sym.st_value = type_ptr->internal_elf_sym.st_value;
4435 sym.st_shndx = _bfd_elf_section_from_bfd_section
4436 (abfd, syms[idx]->section);
4440 asection *sec = syms[idx]->section;
4443 if (sec->output_section)
4445 value += sec->output_offset;
4446 sec = sec->output_section;
4448 /* Don't add in the section vma for relocatable output. */
4449 if (! relocatable_p)
4451 sym.st_value = value;
4452 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4454 if (bfd_is_abs_section (sec)
4456 && type_ptr->internal_elf_sym.st_shndx != 0)
4458 /* This symbol is in a real ELF section which we did
4459 not create as a BFD section. Undo the mapping done
4460 by copy_private_symbol_data. */
4461 shndx = type_ptr->internal_elf_sym.st_shndx;
4465 shndx = elf_onesymtab (abfd);
4468 shndx = elf_dynsymtab (abfd);
4471 shndx = elf_tdata (abfd)->strtab_section;
4474 shndx = elf_tdata (abfd)->shstrtab_section;
4482 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4488 /* Writing this would be a hell of a lot easier if
4489 we had some decent documentation on bfd, and
4490 knew what to expect of the library, and what to
4491 demand of applications. For example, it
4492 appears that `objcopy' might not set the
4493 section of a symbol to be a section that is
4494 actually in the output file. */
4495 sec2 = bfd_get_section_by_name (abfd, sec->name);
4496 BFD_ASSERT (sec2 != 0);
4497 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4498 BFD_ASSERT (shndx != -1);
4502 sym.st_shndx = shndx;
4505 if ((flags & BSF_FUNCTION) != 0)
4507 else if ((flags & BSF_OBJECT) != 0)
4512 /* Processor-specific types */
4513 if (type_ptr != NULL
4514 && bed->elf_backend_get_symbol_type)
4515 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4517 if (flags & BSF_SECTION_SYM)
4519 if (flags & BSF_GLOBAL)
4520 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
4522 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4524 else if (bfd_is_com_section (syms[idx]->section))
4525 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4526 else if (bfd_is_und_section (syms[idx]->section))
4527 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4531 else if (flags & BSF_FILE)
4532 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4535 int bind = STB_LOCAL;
4537 if (flags & BSF_LOCAL)
4539 else if (flags & BSF_WEAK)
4541 else if (flags & BSF_GLOBAL)
4544 sym.st_info = ELF_ST_INFO (bind, type);
4547 if (type_ptr != NULL)
4548 sym.st_other = type_ptr->internal_elf_sym.st_other;
4552 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4553 outbound_syms += bed->s->sizeof_sym;
4557 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4558 symstrtab_hdr->sh_type = SHT_STRTAB;
4560 symstrtab_hdr->sh_flags = 0;
4561 symstrtab_hdr->sh_addr = 0;
4562 symstrtab_hdr->sh_entsize = 0;
4563 symstrtab_hdr->sh_link = 0;
4564 symstrtab_hdr->sh_info = 0;
4565 symstrtab_hdr->sh_addralign = 1;
4571 /* Return the number of bytes required to hold the symtab vector.
4573 Note that we base it on the count plus 1, since we will null terminate
4574 the vector allocated based on this size. However, the ELF symbol table
4575 always has a dummy entry as symbol #0, so it ends up even. */
4578 _bfd_elf_get_symtab_upper_bound (abfd)
4583 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4585 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4586 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4592 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4597 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4599 if (elf_dynsymtab (abfd) == 0)
4601 bfd_set_error (bfd_error_invalid_operation);
4605 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4606 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4612 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4613 bfd *abfd ATTRIBUTE_UNUSED;
4616 return (asect->reloc_count + 1) * sizeof (arelent *);
4619 /* Canonicalize the relocs. */
4622 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4631 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4637 tblptr = section->relocation;
4638 for (i = 0; i < section->reloc_count; i++)
4639 *relptr++ = tblptr++;
4643 return section->reloc_count;
4647 _bfd_elf_get_symtab (abfd, alocation)
4649 asymbol **alocation;
4651 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4652 (abfd, alocation, false);
4655 bfd_get_symcount (abfd) = symcount;
4660 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4662 asymbol **alocation;
4664 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4665 (abfd, alocation, true);
4668 /* Return the size required for the dynamic reloc entries. Any
4669 section that was actually installed in the BFD, and has type
4670 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4671 considered to be a dynamic reloc section. */
4674 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4680 if (elf_dynsymtab (abfd) == 0)
4682 bfd_set_error (bfd_error_invalid_operation);
4686 ret = sizeof (arelent *);
4687 for (s = abfd->sections; s != NULL; s = s->next)
4688 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4689 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4690 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4691 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4692 * sizeof (arelent *));
4697 /* Canonicalize the dynamic relocation entries. Note that we return
4698 the dynamic relocations as a single block, although they are
4699 actually associated with particular sections; the interface, which
4700 was designed for SunOS style shared libraries, expects that there
4701 is only one set of dynamic relocs. Any section that was actually
4702 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4703 the dynamic symbol table, is considered to be a dynamic reloc
4707 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4712 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4716 if (elf_dynsymtab (abfd) == 0)
4718 bfd_set_error (bfd_error_invalid_operation);
4722 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4724 for (s = abfd->sections; s != NULL; s = s->next)
4726 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4727 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4728 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4733 if (! (*slurp_relocs) (abfd, s, syms, true))
4735 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4737 for (i = 0; i < count; i++)
4748 /* Read in the version information. */
4751 _bfd_elf_slurp_version_tables (abfd)
4754 bfd_byte *contents = NULL;
4756 if (elf_dynverdef (abfd) != 0)
4758 Elf_Internal_Shdr *hdr;
4759 Elf_External_Verdef *everdef;
4760 Elf_Internal_Verdef *iverdef;
4761 Elf_Internal_Verdef *iverdefarr;
4762 Elf_Internal_Verdef iverdefmem;
4764 unsigned int maxidx;
4766 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4768 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4769 if (contents == NULL)
4771 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4772 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4775 /* We know the number of entries in the section but not the maximum
4776 index. Therefore we have to run through all entries and find
4778 everdef = (Elf_External_Verdef *) contents;
4780 for (i = 0; i < hdr->sh_info; ++i)
4782 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4784 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
4785 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
4787 everdef = ((Elf_External_Verdef *)
4788 ((bfd_byte *) everdef + iverdefmem.vd_next));
4791 elf_tdata (abfd)->verdef =
4792 ((Elf_Internal_Verdef *)
4793 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4794 if (elf_tdata (abfd)->verdef == NULL)
4797 elf_tdata (abfd)->cverdefs = maxidx;
4799 everdef = (Elf_External_Verdef *) contents;
4800 iverdefarr = elf_tdata (abfd)->verdef;
4801 for (i = 0; i < hdr->sh_info; i++)
4803 Elf_External_Verdaux *everdaux;
4804 Elf_Internal_Verdaux *iverdaux;
4807 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4809 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4810 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4812 iverdef->vd_bfd = abfd;
4814 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4817 * sizeof (Elf_Internal_Verdaux))));
4818 if (iverdef->vd_auxptr == NULL)
4821 everdaux = ((Elf_External_Verdaux *)
4822 ((bfd_byte *) everdef + iverdef->vd_aux));
4823 iverdaux = iverdef->vd_auxptr;
4824 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4826 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4828 iverdaux->vda_nodename =
4829 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4830 iverdaux->vda_name);
4831 if (iverdaux->vda_nodename == NULL)
4834 if (j + 1 < iverdef->vd_cnt)
4835 iverdaux->vda_nextptr = iverdaux + 1;
4837 iverdaux->vda_nextptr = NULL;
4839 everdaux = ((Elf_External_Verdaux *)
4840 ((bfd_byte *) everdaux + iverdaux->vda_next));
4843 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4845 if (i + 1 < hdr->sh_info)
4846 iverdef->vd_nextdef = iverdef + 1;
4848 iverdef->vd_nextdef = NULL;
4850 everdef = ((Elf_External_Verdef *)
4851 ((bfd_byte *) everdef + iverdef->vd_next));
4858 if (elf_dynverref (abfd) != 0)
4860 Elf_Internal_Shdr *hdr;
4861 Elf_External_Verneed *everneed;
4862 Elf_Internal_Verneed *iverneed;
4865 hdr = &elf_tdata (abfd)->dynverref_hdr;
4867 elf_tdata (abfd)->verref =
4868 ((Elf_Internal_Verneed *)
4869 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4870 if (elf_tdata (abfd)->verref == NULL)
4873 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4875 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4876 if (contents == NULL)
4878 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4879 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4882 everneed = (Elf_External_Verneed *) contents;
4883 iverneed = elf_tdata (abfd)->verref;
4884 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4886 Elf_External_Vernaux *evernaux;
4887 Elf_Internal_Vernaux *ivernaux;
4890 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4892 iverneed->vn_bfd = abfd;
4894 iverneed->vn_filename =
4895 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4897 if (iverneed->vn_filename == NULL)
4900 iverneed->vn_auxptr =
4901 ((Elf_Internal_Vernaux *)
4903 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4905 evernaux = ((Elf_External_Vernaux *)
4906 ((bfd_byte *) everneed + iverneed->vn_aux));
4907 ivernaux = iverneed->vn_auxptr;
4908 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4910 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4912 ivernaux->vna_nodename =
4913 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4914 ivernaux->vna_name);
4915 if (ivernaux->vna_nodename == NULL)
4918 if (j + 1 < iverneed->vn_cnt)
4919 ivernaux->vna_nextptr = ivernaux + 1;
4921 ivernaux->vna_nextptr = NULL;
4923 evernaux = ((Elf_External_Vernaux *)
4924 ((bfd_byte *) evernaux + ivernaux->vna_next));
4927 if (i + 1 < hdr->sh_info)
4928 iverneed->vn_nextref = iverneed + 1;
4930 iverneed->vn_nextref = NULL;
4932 everneed = ((Elf_External_Verneed *)
4933 ((bfd_byte *) everneed + iverneed->vn_next));
4943 if (contents == NULL)
4949 _bfd_elf_make_empty_symbol (abfd)
4952 elf_symbol_type *newsym;
4954 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4959 newsym->symbol.the_bfd = abfd;
4960 return &newsym->symbol;
4965 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4966 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4970 bfd_symbol_info (symbol, ret);
4973 /* Return whether a symbol name implies a local symbol. Most targets
4974 use this function for the is_local_label_name entry point, but some
4978 _bfd_elf_is_local_label_name (abfd, name)
4979 bfd *abfd ATTRIBUTE_UNUSED;
4982 /* Normal local symbols start with ``.L''. */
4983 if (name[0] == '.' && name[1] == 'L')
4986 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4987 DWARF debugging symbols starting with ``..''. */
4988 if (name[0] == '.' && name[1] == '.')
4991 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4992 emitting DWARF debugging output. I suspect this is actually a
4993 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4994 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4995 underscore to be emitted on some ELF targets). For ease of use,
4996 we treat such symbols as local. */
4997 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5004 _bfd_elf_get_lineno (ignore_abfd, symbol)
5005 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5006 asymbol *symbol ATTRIBUTE_UNUSED;
5013 _bfd_elf_set_arch_mach (abfd, arch, machine)
5015 enum bfd_architecture arch;
5016 unsigned long machine;
5018 /* If this isn't the right architecture for this backend, and this
5019 isn't the generic backend, fail. */
5020 if (arch != get_elf_backend_data (abfd)->arch
5021 && arch != bfd_arch_unknown
5022 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5025 return bfd_default_set_arch_mach (abfd, arch, machine);
5028 /* Find the function to a particular section and offset,
5029 for error reporting. */
5032 elf_find_function (abfd, section, symbols, offset,
5033 filename_ptr, functionname_ptr)
5034 bfd *abfd ATTRIBUTE_UNUSED;
5038 const char **filename_ptr;
5039 const char **functionname_ptr;
5041 const char *filename;
5050 for (p = symbols; *p != NULL; p++)
5054 q = (elf_symbol_type *) *p;
5056 if (bfd_get_section (&q->symbol) != section)
5059 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5064 filename = bfd_asymbol_name (&q->symbol);
5068 if (q->symbol.section == section
5069 && q->symbol.value >= low_func
5070 && q->symbol.value <= offset)
5072 func = (asymbol *) q;
5073 low_func = q->symbol.value;
5083 *filename_ptr = filename;
5084 if (functionname_ptr)
5085 *functionname_ptr = bfd_asymbol_name (func);
5090 /* Find the nearest line to a particular section and offset,
5091 for error reporting. */
5094 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5095 filename_ptr, functionname_ptr, line_ptr)
5100 const char **filename_ptr;
5101 const char **functionname_ptr;
5102 unsigned int *line_ptr;
5106 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5107 filename_ptr, functionname_ptr,
5110 if (!*functionname_ptr)
5111 elf_find_function (abfd, section, symbols, offset,
5112 *filename_ptr ? NULL : filename_ptr,
5118 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5119 filename_ptr, functionname_ptr,
5121 &elf_tdata (abfd)->dwarf2_find_line_info))
5123 if (!*functionname_ptr)
5124 elf_find_function (abfd, section, symbols, offset,
5125 *filename_ptr ? NULL : filename_ptr,
5131 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5132 &found, filename_ptr,
5133 functionname_ptr, line_ptr,
5134 &elf_tdata (abfd)->line_info))
5139 if (symbols == NULL)
5142 if (! elf_find_function (abfd, section, symbols, offset,
5143 filename_ptr, functionname_ptr))
5151 _bfd_elf_sizeof_headers (abfd, reloc)
5157 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5159 ret += get_program_header_size (abfd);
5164 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5169 bfd_size_type count;
5171 Elf_Internal_Shdr *hdr;
5173 if (! abfd->output_has_begun
5174 && ! _bfd_elf_compute_section_file_positions
5175 (abfd, (struct bfd_link_info *) NULL))
5178 hdr = &elf_section_data (section)->this_hdr;
5180 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5182 if (bfd_write (location, 1, count, abfd) != count)
5189 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5190 bfd *abfd ATTRIBUTE_UNUSED;
5191 arelent *cache_ptr ATTRIBUTE_UNUSED;
5192 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5199 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5202 Elf_Internal_Rel *dst;
5208 /* Try to convert a non-ELF reloc into an ELF one. */
5211 _bfd_elf_validate_reloc (abfd, areloc)
5215 /* Check whether we really have an ELF howto. */
5217 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5219 bfd_reloc_code_real_type code;
5220 reloc_howto_type *howto;
5222 /* Alien reloc: Try to determine its type to replace it with an
5223 equivalent ELF reloc. */
5225 if (areloc->howto->pc_relative)
5227 switch (areloc->howto->bitsize)
5230 code = BFD_RELOC_8_PCREL;
5233 code = BFD_RELOC_12_PCREL;
5236 code = BFD_RELOC_16_PCREL;
5239 code = BFD_RELOC_24_PCREL;
5242 code = BFD_RELOC_32_PCREL;
5245 code = BFD_RELOC_64_PCREL;
5251 howto = bfd_reloc_type_lookup (abfd, code);
5253 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5255 if (howto->pcrel_offset)
5256 areloc->addend += areloc->address;
5258 areloc->addend -= areloc->address; /* addend is unsigned!! */
5263 switch (areloc->howto->bitsize)
5269 code = BFD_RELOC_14;
5272 code = BFD_RELOC_16;
5275 code = BFD_RELOC_26;
5278 code = BFD_RELOC_32;
5281 code = BFD_RELOC_64;
5287 howto = bfd_reloc_type_lookup (abfd, code);
5291 areloc->howto = howto;
5299 (*_bfd_error_handler)
5300 (_("%s: unsupported relocation type %s"),
5301 bfd_get_filename (abfd), areloc->howto->name);
5302 bfd_set_error (bfd_error_bad_value);
5307 _bfd_elf_close_and_cleanup (abfd)
5310 if (bfd_get_format (abfd) == bfd_object)
5312 if (elf_shstrtab (abfd) != NULL)
5313 _bfd_stringtab_free (elf_shstrtab (abfd));
5316 return _bfd_generic_close_and_cleanup (abfd);
5319 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5320 in the relocation's offset. Thus we cannot allow any sort of sanity
5321 range-checking to interfere. There is nothing else to do in processing
5324 bfd_reloc_status_type
5325 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5326 bfd *abfd ATTRIBUTE_UNUSED;
5327 arelent *re ATTRIBUTE_UNUSED;
5328 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5329 PTR data ATTRIBUTE_UNUSED;
5330 asection *is ATTRIBUTE_UNUSED;
5331 bfd *obfd ATTRIBUTE_UNUSED;
5332 char **errmsg ATTRIBUTE_UNUSED;
5334 return bfd_reloc_ok;
5337 /* Elf core file support. Much of this only works on native
5338 toolchains, since we rely on knowing the
5339 machine-dependent procfs structure in order to pick
5340 out details about the corefile. */
5342 #ifdef HAVE_SYS_PROCFS_H
5343 # include <sys/procfs.h>
5346 /* Define offsetof for those systems which lack it. */
5349 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5352 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5355 elfcore_make_pid (abfd)
5358 return ((elf_tdata (abfd)->core_lwpid << 16)
5359 + (elf_tdata (abfd)->core_pid));
5362 /* If there isn't a section called NAME, make one, using
5363 data from SECT. Note, this function will generate a
5364 reference to NAME, so you shouldn't deallocate or
5368 elfcore_maybe_make_sect (abfd, name, sect)
5375 if (bfd_get_section_by_name (abfd, name) != NULL)
5378 sect2 = bfd_make_section (abfd, name);
5382 sect2->_raw_size = sect->_raw_size;
5383 sect2->filepos = sect->filepos;
5384 sect2->flags = sect->flags;
5385 sect2->alignment_power = sect->alignment_power;
5389 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5390 actually creates up to two pseudosections:
5391 - For the single-threaded case, a section named NAME, unless
5392 such a section already exists.
5393 - For the multi-threaded case, a section named "NAME/PID", where
5394 PID is elfcore_make_pid (abfd).
5395 Both pseudosections have identical contents. */
5397 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5404 char *threaded_name;
5407 /* Build the section name. */
5409 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5410 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5411 if (threaded_name == NULL)
5413 strcpy (threaded_name, buf);
5415 sect = bfd_make_section (abfd, threaded_name);
5418 sect->_raw_size = size;
5419 sect->filepos = filepos;
5420 sect->flags = SEC_HAS_CONTENTS;
5421 sect->alignment_power = 2;
5423 return elfcore_maybe_make_sect (abfd, name, sect);
5426 /* prstatus_t exists on:
5428 linux 2.[01] + glibc
5432 #if defined (HAVE_PRSTATUS_T)
5433 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5436 elfcore_grok_prstatus (abfd, note)
5438 Elf_Internal_Note *note;
5443 if (note->descsz == sizeof (prstatus_t))
5447 raw_size = sizeof (prstat.pr_reg);
5448 offset = offsetof (prstatus_t, pr_reg);
5449 memcpy (&prstat, note->descdata, sizeof (prstat));
5451 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5452 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5454 /* pr_who exists on:
5457 pr_who doesn't exist on:
5460 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5461 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5464 #if defined (HAVE_PRSTATUS32_T)
5465 else if (note->descsz == sizeof (prstatus32_t))
5467 /* 64-bit host, 32-bit corefile */
5468 prstatus32_t prstat;
5470 raw_size = sizeof (prstat.pr_reg);
5471 offset = offsetof (prstatus32_t, pr_reg);
5472 memcpy (&prstat, note->descdata, sizeof (prstat));
5474 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5475 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5477 /* pr_who exists on:
5480 pr_who doesn't exist on:
5483 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5484 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5487 #endif /* HAVE_PRSTATUS32_T */
5490 /* Fail - we don't know how to handle any other
5491 note size (ie. data object type). */
5495 /* Make a ".reg/999" section and a ".reg" section. */
5496 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5497 raw_size, note->descpos + offset);
5499 #endif /* defined (HAVE_PRSTATUS_T) */
5501 /* Create a pseudosection containing the exact contents of NOTE. */
5503 elfcore_make_note_pseudosection (abfd, name, note)
5506 Elf_Internal_Note *note;
5508 return _bfd_elfcore_make_pseudosection (abfd, name,
5509 note->descsz, note->descpos);
5512 /* There isn't a consistent prfpregset_t across platforms,
5513 but it doesn't matter, because we don't have to pick this
5514 data structure apart. */
5517 elfcore_grok_prfpreg (abfd, note)
5519 Elf_Internal_Note *note;
5521 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5524 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5525 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5529 elfcore_grok_prxfpreg (abfd, note)
5531 Elf_Internal_Note *note;
5533 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5536 #if defined (HAVE_PRPSINFO_T)
5537 typedef prpsinfo_t elfcore_psinfo_t;
5538 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5539 typedef prpsinfo32_t elfcore_psinfo32_t;
5543 #if defined (HAVE_PSINFO_T)
5544 typedef psinfo_t elfcore_psinfo_t;
5545 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5546 typedef psinfo32_t elfcore_psinfo32_t;
5550 /* return a malloc'ed copy of a string at START which is at
5551 most MAX bytes long, possibly without a terminating '\0'.
5552 the copy will always have a terminating '\0'. */
5555 _bfd_elfcore_strndup (abfd, start, max)
5561 char *end = memchr (start, '\0', max);
5569 dup = bfd_alloc (abfd, len + 1);
5573 memcpy (dup, start, len);
5579 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5580 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
5583 elfcore_grok_psinfo (abfd, note)
5585 Elf_Internal_Note *note;
5587 if (note->descsz == sizeof (elfcore_psinfo_t))
5589 elfcore_psinfo_t psinfo;
5591 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5593 elf_tdata (abfd)->core_program
5594 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5595 sizeof (psinfo.pr_fname));
5597 elf_tdata (abfd)->core_command
5598 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5599 sizeof (psinfo.pr_psargs));
5601 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5602 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5604 /* 64-bit host, 32-bit corefile */
5605 elfcore_psinfo32_t psinfo;
5607 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5609 elf_tdata (abfd)->core_program
5610 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5611 sizeof (psinfo.pr_fname));
5613 elf_tdata (abfd)->core_command
5614 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5615 sizeof (psinfo.pr_psargs));
5621 /* Fail - we don't know how to handle any other
5622 note size (ie. data object type). */
5626 /* Note that for some reason, a spurious space is tacked
5627 onto the end of the args in some (at least one anyway)
5628 implementations, so strip it off if it exists. */
5631 char *command = elf_tdata (abfd)->core_command;
5632 int n = strlen (command);
5634 if (0 < n && command[n - 1] == ' ')
5635 command[n - 1] = '\0';
5640 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5642 #if defined (HAVE_PSTATUS_T)
5644 elfcore_grok_pstatus (abfd, note)
5646 Elf_Internal_Note *note;
5648 if (note->descsz == sizeof (pstatus_t)
5649 #if defined (HAVE_PXSTATUS_T)
5650 || note->descsz == sizeof (pxstatus_t)
5656 memcpy (&pstat, note->descdata, sizeof (pstat));
5658 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5660 #if defined (HAVE_PSTATUS32_T)
5661 else if (note->descsz == sizeof (pstatus32_t))
5663 /* 64-bit host, 32-bit corefile */
5666 memcpy (&pstat, note->descdata, sizeof (pstat));
5668 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5671 /* Could grab some more details from the "representative"
5672 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5673 NT_LWPSTATUS note, presumably. */
5677 #endif /* defined (HAVE_PSTATUS_T) */
5679 #if defined (HAVE_LWPSTATUS_T)
5681 elfcore_grok_lwpstatus (abfd, note)
5683 Elf_Internal_Note *note;
5685 lwpstatus_t lwpstat;
5690 if (note->descsz != sizeof (lwpstat)
5691 #if defined (HAVE_LWPXSTATUS_T)
5692 && note->descsz != sizeof (lwpxstatus_t)
5697 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5699 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5700 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5702 /* Make a ".reg/999" section. */
5704 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5705 name = bfd_alloc (abfd, strlen (buf) + 1);
5710 sect = bfd_make_section (abfd, name);
5714 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5715 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5716 sect->filepos = note->descpos
5717 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5720 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5721 sect->_raw_size = sizeof (lwpstat.pr_reg);
5722 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5725 sect->flags = SEC_HAS_CONTENTS;
5726 sect->alignment_power = 2;
5728 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5731 /* Make a ".reg2/999" section */
5733 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5734 name = bfd_alloc (abfd, strlen (buf) + 1);
5739 sect = bfd_make_section (abfd, name);
5743 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5744 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5745 sect->filepos = note->descpos
5746 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5749 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5750 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5751 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5754 sect->flags = SEC_HAS_CONTENTS;
5755 sect->alignment_power = 2;
5757 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
5759 #endif /* defined (HAVE_LWPSTATUS_T) */
5761 #if defined (HAVE_WIN32_PSTATUS_T)
5763 elfcore_grok_win32pstatus (abfd, note)
5765 Elf_Internal_Note *note;
5770 win32_pstatus_t pstatus;
5772 if (note->descsz < sizeof (pstatus))
5775 memcpy (&pstatus, note->descdata, note->descsz);
5777 switch (pstatus.data_type)
5779 case NOTE_INFO_PROCESS:
5780 /* FIXME: need to add ->core_command. */
5781 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5782 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5785 case NOTE_INFO_THREAD:
5786 /* Make a ".reg/999" section. */
5787 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5789 name = bfd_alloc (abfd, strlen (buf) + 1);
5795 sect = bfd_make_section (abfd, name);
5799 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5800 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5801 data.thread_info.thread_context);
5802 sect->flags = SEC_HAS_CONTENTS;
5803 sect->alignment_power = 2;
5805 if (pstatus.data.thread_info.is_active_thread)
5806 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5810 case NOTE_INFO_MODULE:
5811 /* Make a ".module/xxxxxxxx" section. */
5812 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5814 name = bfd_alloc (abfd, strlen (buf) + 1);
5820 sect = bfd_make_section (abfd, name);
5825 sect->_raw_size = note->descsz;
5826 sect->filepos = note->descpos;
5827 sect->flags = SEC_HAS_CONTENTS;
5828 sect->alignment_power = 2;
5837 #endif /* HAVE_WIN32_PSTATUS_T */
5840 elfcore_grok_note (abfd, note)
5842 Elf_Internal_Note *note;
5844 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5852 if (bed->elf_backend_grok_prstatus)
5853 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
5855 #if defined (HAVE_PRSTATUS_T)
5856 return elfcore_grok_prstatus (abfd, note);
5861 #if defined (HAVE_PSTATUS_T)
5863 return elfcore_grok_pstatus (abfd, note);
5866 #if defined (HAVE_LWPSTATUS_T)
5868 return elfcore_grok_lwpstatus (abfd, note);
5871 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5872 return elfcore_grok_prfpreg (abfd, note);
5874 #if defined (HAVE_WIN32_PSTATUS_T)
5875 case NT_WIN32PSTATUS:
5876 return elfcore_grok_win32pstatus (abfd, note);
5879 case NT_PRXFPREG: /* Linux SSE extension */
5880 if (note->namesz == 5
5881 && ! strcmp (note->namedata, "LINUX"))
5882 return elfcore_grok_prxfpreg (abfd, note);
5888 if (bed->elf_backend_grok_psinfo)
5889 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
5891 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5892 return elfcore_grok_psinfo (abfd, note);
5900 elfcore_read_notes (abfd, offset, size)
5911 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5914 buf = bfd_malloc ((size_t) size);
5918 if (bfd_read (buf, size, 1, abfd) != size)
5926 while (p < buf + size)
5928 /* FIXME: bad alignment assumption. */
5929 Elf_External_Note *xnp = (Elf_External_Note *) p;
5930 Elf_Internal_Note in;
5932 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5934 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5935 in.namedata = xnp->name;
5937 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5938 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5939 in.descpos = offset + (in.descdata - buf);
5941 if (! elfcore_grok_note (abfd, &in))
5944 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5951 /* Providing external access to the ELF program header table. */
5953 /* Return an upper bound on the number of bytes required to store a
5954 copy of ABFD's program header table entries. Return -1 if an error
5955 occurs; bfd_get_error will return an appropriate code. */
5958 bfd_get_elf_phdr_upper_bound (abfd)
5961 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5963 bfd_set_error (bfd_error_wrong_format);
5967 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
5970 /* Copy ABFD's program header table entries to *PHDRS. The entries
5971 will be stored as an array of Elf_Internal_Phdr structures, as
5972 defined in include/elf/internal.h. To find out how large the
5973 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5975 Return the number of program header table entries read, or -1 if an
5976 error occurs; bfd_get_error will return an appropriate code. */
5979 bfd_get_elf_phdrs (abfd, phdrs)
5985 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5987 bfd_set_error (bfd_error_wrong_format);
5991 num_phdrs = elf_elfheader (abfd)->e_phnum;
5992 memcpy (phdrs, elf_tdata (abfd)->phdr,
5993 num_phdrs * sizeof (Elf_Internal_Phdr));
5999 bfd_elf_sprintf_vma (abfd, buf, value)
6005 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6007 i_ehdrp = elf_elfheader (abfd);
6008 if (i_ehdrp == NULL)
6009 sprintf_vma (buf, value);
6012 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6013 #if BFD_HOST_64BIT_LONG
6014 sprintf (buf, "%016lx", value);
6016 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6017 _bfd_int64_low (value));
6020 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6023 sprintf_vma (buf, value);
6028 bfd_elf_fprintf_vma (abfd, stream, value)
6034 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6036 i_ehdrp = elf_elfheader (abfd);
6037 if (i_ehdrp == NULL)
6038 fprintf_vma ((FILE *) stream, value);
6041 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6042 #if BFD_HOST_64BIT_LONG
6043 fprintf ((FILE *) stream, "%016lx", value);
6045 fprintf ((FILE *) stream, "%08lx%08lx",
6046 _bfd_int64_high (value), _bfd_int64_low (value));
6049 fprintf ((FILE *) stream, "%08lx",
6050 (unsigned long) (value & 0xffffffff));
6053 fprintf_vma ((FILE *) stream, value);