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 bfd_fprintf_vma (abfd, f, p->p_offset);
616 fprintf (f, " vaddr 0x");
617 bfd_fprintf_vma (abfd, f, p->p_vaddr);
618 fprintf (f, " paddr 0x");
619 bfd_fprintf_vma (abfd, f, p->p_paddr);
620 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
621 fprintf (f, " filesz 0x");
622 bfd_fprintf_vma (abfd, f, p->p_filesz);
623 fprintf (f, " memsz 0x");
624 bfd_fprintf_vma (abfd, 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 bfd_fprintf_vma (abfd, 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 (abfd, (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. */
860 bfd_fprintf_vma (abfd, file,
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;
2094 elf_num_section_syms (abfd) = max_index;
2096 for (idx = 0; idx < symcount; idx++)
2100 if ((sym->flags & BSF_SECTION_SYM) != 0
2107 if (sec->owner != NULL)
2109 if (sec->owner != abfd)
2111 if (sec->output_offset != 0)
2114 sec = sec->output_section;
2116 /* Empty sections in the input files may have had a section
2117 symbol created for them. (See the comment near the end of
2118 _bfd_generic_link_output_symbols in linker.c). If the linker
2119 script discards such sections then we will reach this point.
2120 Since we know that we cannot avoid this case, we detect it
2121 and skip the abort and the assignment to the sect_syms array.
2122 To reproduce this particular case try running the linker
2123 testsuite test ld-scripts/weak.exp for an ELF port that uses
2124 the generic linker. */
2125 if (sec->owner == NULL)
2128 BFD_ASSERT (sec->owner == abfd);
2130 sect_syms[sec->index] = syms[idx];
2135 for (asect = abfd->sections; asect; asect = asect->next)
2137 if (sect_syms[asect->index] != NULL)
2140 sym = bfd_make_empty_symbol (abfd);
2143 sym->the_bfd = abfd;
2144 sym->name = asect->name;
2146 /* Set the flags to 0 to indicate that this one was newly added. */
2148 sym->section = asect;
2149 sect_syms[asect->index] = sym;
2153 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2154 asect->name, (long) asect->vma, asect->index, (long) asect);
2158 /* Classify all of the symbols. */
2159 for (idx = 0; idx < symcount; idx++)
2161 if (!sym_is_global (abfd, syms[idx]))
2166 for (asect = abfd->sections; asect; asect = asect->next)
2168 if (sect_syms[asect->index] != NULL
2169 && sect_syms[asect->index]->flags == 0)
2171 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2172 if (!sym_is_global (abfd, sect_syms[asect->index]))
2176 sect_syms[asect->index]->flags = 0;
2180 /* Now sort the symbols so the local symbols are first. */
2181 new_syms = ((asymbol **)
2183 (num_locals + num_globals) * sizeof (asymbol *)));
2184 if (new_syms == NULL)
2187 for (idx = 0; idx < symcount; idx++)
2189 asymbol *sym = syms[idx];
2192 if (!sym_is_global (abfd, sym))
2195 i = num_locals + num_globals2++;
2197 sym->udata.i = i + 1;
2199 for (asect = abfd->sections; asect; asect = asect->next)
2201 if (sect_syms[asect->index] != NULL
2202 && sect_syms[asect->index]->flags == 0)
2204 asymbol *sym = sect_syms[asect->index];
2207 sym->flags = BSF_SECTION_SYM;
2208 if (!sym_is_global (abfd, sym))
2211 i = num_locals + num_globals2++;
2213 sym->udata.i = i + 1;
2217 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2219 elf_num_locals (abfd) = num_locals;
2220 elf_num_globals (abfd) = num_globals;
2224 /* Align to the maximum file alignment that could be required for any
2225 ELF data structure. */
2227 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2228 static INLINE file_ptr
2229 align_file_position (off, align)
2233 return (off + align - 1) & ~(align - 1);
2236 /* Assign a file position to a section, optionally aligning to the
2237 required section alignment. */
2240 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2241 Elf_Internal_Shdr *i_shdrp;
2249 al = i_shdrp->sh_addralign;
2251 offset = BFD_ALIGN (offset, al);
2253 i_shdrp->sh_offset = offset;
2254 if (i_shdrp->bfd_section != NULL)
2255 i_shdrp->bfd_section->filepos = offset;
2256 if (i_shdrp->sh_type != SHT_NOBITS)
2257 offset += i_shdrp->sh_size;
2261 /* Compute the file positions we are going to put the sections at, and
2262 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2263 is not NULL, this is being called by the ELF backend linker. */
2266 _bfd_elf_compute_section_file_positions (abfd, link_info)
2268 struct bfd_link_info *link_info;
2270 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2272 struct bfd_strtab_hash *strtab;
2273 Elf_Internal_Shdr *shstrtab_hdr;
2275 if (abfd->output_has_begun)
2278 /* Do any elf backend specific processing first. */
2279 if (bed->elf_backend_begin_write_processing)
2280 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2282 if (! prep_headers (abfd))
2285 /* Post process the headers if necessary. */
2286 if (bed->elf_backend_post_process_headers)
2287 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2290 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2294 if (!assign_section_numbers (abfd))
2297 /* The backend linker builds symbol table information itself. */
2298 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2300 /* Non-zero if doing a relocatable link. */
2301 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2303 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2307 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2308 /* sh_name was set in prep_headers. */
2309 shstrtab_hdr->sh_type = SHT_STRTAB;
2310 shstrtab_hdr->sh_flags = 0;
2311 shstrtab_hdr->sh_addr = 0;
2312 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2313 shstrtab_hdr->sh_entsize = 0;
2314 shstrtab_hdr->sh_link = 0;
2315 shstrtab_hdr->sh_info = 0;
2316 /* sh_offset is set in assign_file_positions_except_relocs. */
2317 shstrtab_hdr->sh_addralign = 1;
2319 if (!assign_file_positions_except_relocs (abfd))
2322 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2325 Elf_Internal_Shdr *hdr;
2327 off = elf_tdata (abfd)->next_file_pos;
2329 hdr = &elf_tdata (abfd)->symtab_hdr;
2330 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2332 hdr = &elf_tdata (abfd)->strtab_hdr;
2333 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2335 elf_tdata (abfd)->next_file_pos = off;
2337 /* Now that we know where the .strtab section goes, write it
2339 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2340 || ! _bfd_stringtab_emit (abfd, strtab))
2342 _bfd_stringtab_free (strtab);
2345 abfd->output_has_begun = true;
2350 /* Create a mapping from a set of sections to a program segment. */
2352 static INLINE struct elf_segment_map *
2353 make_mapping (abfd, sections, from, to, phdr)
2355 asection **sections;
2360 struct elf_segment_map *m;
2364 m = ((struct elf_segment_map *)
2366 (sizeof (struct elf_segment_map)
2367 + (to - from - 1) * sizeof (asection *))));
2371 m->p_type = PT_LOAD;
2372 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2373 m->sections[i - from] = *hdrpp;
2374 m->count = to - from;
2376 if (from == 0 && phdr)
2378 /* Include the headers in the first PT_LOAD segment. */
2379 m->includes_filehdr = 1;
2380 m->includes_phdrs = 1;
2386 /* Set up a mapping from BFD sections to program segments. */
2389 map_sections_to_segments (abfd)
2392 asection **sections = NULL;
2396 struct elf_segment_map *mfirst;
2397 struct elf_segment_map **pm;
2398 struct elf_segment_map *m;
2400 unsigned int phdr_index;
2401 bfd_vma maxpagesize;
2403 boolean phdr_in_segment = true;
2407 if (elf_tdata (abfd)->segment_map != NULL)
2410 if (bfd_count_sections (abfd) == 0)
2413 /* Select the allocated sections, and sort them. */
2415 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2416 * sizeof (asection *));
2417 if (sections == NULL)
2421 for (s = abfd->sections; s != NULL; s = s->next)
2423 if ((s->flags & SEC_ALLOC) != 0)
2429 BFD_ASSERT (i <= bfd_count_sections (abfd));
2432 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2434 /* Build the mapping. */
2439 /* If we have a .interp section, then create a PT_PHDR segment for
2440 the program headers and a PT_INTERP segment for the .interp
2442 s = bfd_get_section_by_name (abfd, ".interp");
2443 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2445 m = ((struct elf_segment_map *)
2446 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2450 m->p_type = PT_PHDR;
2451 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2452 m->p_flags = PF_R | PF_X;
2453 m->p_flags_valid = 1;
2454 m->includes_phdrs = 1;
2459 m = ((struct elf_segment_map *)
2460 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2464 m->p_type = PT_INTERP;
2472 /* Look through the sections. We put sections in the same program
2473 segment when the start of the second section can be placed within
2474 a few bytes of the end of the first section. */
2477 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2479 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2481 && (dynsec->flags & SEC_LOAD) == 0)
2484 /* Deal with -Ttext or something similar such that the first section
2485 is not adjacent to the program headers. This is an
2486 approximation, since at this point we don't know exactly how many
2487 program headers we will need. */
2490 bfd_size_type phdr_size;
2492 phdr_size = elf_tdata (abfd)->program_header_size;
2494 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2495 if ((abfd->flags & D_PAGED) == 0
2496 || sections[0]->lma < phdr_size
2497 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2498 phdr_in_segment = false;
2501 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2504 boolean new_segment;
2508 /* See if this section and the last one will fit in the same
2511 if (last_hdr == NULL)
2513 /* If we don't have a segment yet, then we don't need a new
2514 one (we build the last one after this loop). */
2515 new_segment = false;
2517 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2519 /* If this section has a different relation between the
2520 virtual address and the load address, then we need a new
2524 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2525 < BFD_ALIGN (hdr->lma, maxpagesize))
2527 /* If putting this section in this segment would force us to
2528 skip a page in the segment, then we need a new segment. */
2531 else if ((last_hdr->flags & SEC_LOAD) == 0
2532 && (hdr->flags & SEC_LOAD) != 0)
2534 /* We don't want to put a loadable section after a
2535 nonloadable section in the same segment. */
2538 else if ((abfd->flags & D_PAGED) == 0)
2540 /* If the file is not demand paged, which means that we
2541 don't require the sections to be correctly aligned in the
2542 file, then there is no other reason for a new segment. */
2543 new_segment = false;
2546 && (hdr->flags & SEC_READONLY) == 0
2547 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2550 /* We don't want to put a writable section in a read only
2551 segment, unless they are on the same page in memory
2552 anyhow. We already know that the last section does not
2553 bring us past the current section on the page, so the
2554 only case in which the new section is not on the same
2555 page as the previous section is when the previous section
2556 ends precisely on a page boundary. */
2561 /* Otherwise, we can use the same segment. */
2562 new_segment = false;
2567 if ((hdr->flags & SEC_READONLY) == 0)
2573 /* We need a new program segment. We must create a new program
2574 header holding all the sections from phdr_index until hdr. */
2576 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2583 if ((hdr->flags & SEC_READONLY) == 0)
2590 phdr_in_segment = false;
2593 /* Create a final PT_LOAD program segment. */
2594 if (last_hdr != NULL)
2596 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2604 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2607 m = ((struct elf_segment_map *)
2608 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2612 m->p_type = PT_DYNAMIC;
2614 m->sections[0] = dynsec;
2620 /* For each loadable .note section, add a PT_NOTE segment. We don't
2621 use bfd_get_section_by_name, because if we link together
2622 nonloadable .note sections and loadable .note sections, we will
2623 generate two .note sections in the output file. FIXME: Using
2624 names for section types is bogus anyhow. */
2625 for (s = abfd->sections; s != NULL; s = s->next)
2627 if ((s->flags & SEC_LOAD) != 0
2628 && strncmp (s->name, ".note", 5) == 0)
2630 m = ((struct elf_segment_map *)
2631 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2635 m->p_type = PT_NOTE;
2647 elf_tdata (abfd)->segment_map = mfirst;
2651 if (sections != NULL)
2656 /* Sort sections by address. */
2659 elf_sort_sections (arg1, arg2)
2663 const asection *sec1 = *(const asection **) arg1;
2664 const asection *sec2 = *(const asection **) arg2;
2666 /* Sort by LMA first, since this is the address used to
2667 place the section into a segment. */
2668 if (sec1->lma < sec2->lma)
2670 else if (sec1->lma > sec2->lma)
2673 /* Then sort by VMA. Normally the LMA and the VMA will be
2674 the same, and this will do nothing. */
2675 if (sec1->vma < sec2->vma)
2677 else if (sec1->vma > sec2->vma)
2680 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2682 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2688 /* If the indicies are the same, do not return 0
2689 here, but continue to try the next comparison. */
2690 if (sec1->target_index - sec2->target_index != 0)
2691 return sec1->target_index - sec2->target_index;
2696 else if (TOEND (sec2))
2701 /* Sort by size, to put zero sized sections
2702 before others at the same address. */
2704 if (sec1->_raw_size < sec2->_raw_size)
2706 if (sec1->_raw_size > sec2->_raw_size)
2709 return sec1->target_index - sec2->target_index;
2712 /* Assign file positions to the sections based on the mapping from
2713 sections to segments. This function also sets up some fields in
2714 the file header, and writes out the program headers. */
2717 assign_file_positions_for_segments (abfd)
2720 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2722 struct elf_segment_map *m;
2724 Elf_Internal_Phdr *phdrs;
2726 bfd_vma filehdr_vaddr, filehdr_paddr;
2727 bfd_vma phdrs_vaddr, phdrs_paddr;
2728 Elf_Internal_Phdr *p;
2730 if (elf_tdata (abfd)->segment_map == NULL)
2732 if (! map_sections_to_segments (abfd))
2736 if (bed->elf_backend_modify_segment_map)
2738 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2743 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2746 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2747 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2748 elf_elfheader (abfd)->e_phnum = count;
2753 /* If we already counted the number of program segments, make sure
2754 that we allocated enough space. This happens when SIZEOF_HEADERS
2755 is used in a linker script. */
2756 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2757 if (alloc != 0 && count > alloc)
2759 ((*_bfd_error_handler)
2760 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2761 bfd_get_filename (abfd), alloc, count));
2762 bfd_set_error (bfd_error_bad_value);
2769 phdrs = ((Elf_Internal_Phdr *)
2770 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2774 off = bed->s->sizeof_ehdr;
2775 off += alloc * bed->s->sizeof_phdr;
2782 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2789 /* If elf_segment_map is not from map_sections_to_segments, the
2790 sections may not be correctly ordered. */
2792 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2795 p->p_type = m->p_type;
2796 p->p_flags = m->p_flags;
2798 if (p->p_type == PT_LOAD
2800 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2802 if ((abfd->flags & D_PAGED) != 0)
2803 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2806 bfd_size_type align;
2809 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2811 bfd_size_type secalign;
2813 secalign = bfd_get_section_alignment (abfd, *secpp);
2814 if (secalign > align)
2818 off += (m->sections[0]->vma - off) % (1 << align);
2825 p->p_vaddr = m->sections[0]->vma;
2827 if (m->p_paddr_valid)
2828 p->p_paddr = m->p_paddr;
2829 else if (m->count == 0)
2832 p->p_paddr = m->sections[0]->lma;
2834 if (p->p_type == PT_LOAD
2835 && (abfd->flags & D_PAGED) != 0)
2836 p->p_align = bed->maxpagesize;
2837 else if (m->count == 0)
2838 p->p_align = bed->s->file_align;
2846 if (m->includes_filehdr)
2848 if (! m->p_flags_valid)
2851 p->p_filesz = bed->s->sizeof_ehdr;
2852 p->p_memsz = bed->s->sizeof_ehdr;
2855 BFD_ASSERT (p->p_type == PT_LOAD);
2857 if (p->p_vaddr < (bfd_vma) off)
2859 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2860 bfd_get_filename (abfd));
2861 bfd_set_error (bfd_error_bad_value);
2866 if (! m->p_paddr_valid)
2869 if (p->p_type == PT_LOAD)
2871 filehdr_vaddr = p->p_vaddr;
2872 filehdr_paddr = p->p_paddr;
2876 if (m->includes_phdrs)
2878 if (! m->p_flags_valid)
2881 if (m->includes_filehdr)
2883 if (p->p_type == PT_LOAD)
2885 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2886 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2891 p->p_offset = bed->s->sizeof_ehdr;
2895 BFD_ASSERT (p->p_type == PT_LOAD);
2896 p->p_vaddr -= off - p->p_offset;
2897 if (! m->p_paddr_valid)
2898 p->p_paddr -= off - p->p_offset;
2901 if (p->p_type == PT_LOAD)
2903 phdrs_vaddr = p->p_vaddr;
2904 phdrs_paddr = p->p_paddr;
2907 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2910 p->p_filesz += alloc * bed->s->sizeof_phdr;
2911 p->p_memsz += alloc * bed->s->sizeof_phdr;
2914 if (p->p_type == PT_LOAD
2915 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2917 if (! m->includes_filehdr && ! m->includes_phdrs)
2923 adjust = off - (p->p_offset + p->p_filesz);
2924 p->p_filesz += adjust;
2925 p->p_memsz += adjust;
2931 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2935 bfd_size_type align;
2939 align = 1 << bfd_get_section_alignment (abfd, sec);
2941 /* The section may have artificial alignment forced by a
2942 link script. Notice this case by the gap between the
2943 cumulative phdr vma and the section's vma. */
2944 if (p->p_vaddr + p->p_memsz < sec->vma)
2946 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2948 p->p_memsz += adjust;
2951 if ((flags & SEC_LOAD) != 0)
2952 p->p_filesz += adjust;
2955 if (p->p_type == PT_LOAD)
2957 bfd_signed_vma adjust;
2959 if ((flags & SEC_LOAD) != 0)
2961 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2965 else if ((flags & SEC_ALLOC) != 0)
2967 /* The section VMA must equal the file position
2968 modulo the page size. FIXME: I'm not sure if
2969 this adjustment is really necessary. We used to
2970 not have the SEC_LOAD case just above, and then
2971 this was necessary, but now I'm not sure. */
2972 if ((abfd->flags & D_PAGED) != 0)
2973 adjust = (sec->vma - voff) % bed->maxpagesize;
2975 adjust = (sec->vma - voff) % align;
2984 (* _bfd_error_handler)
2985 (_("Error: First section in segment (%s) starts at 0x%x"),
2986 bfd_section_name (abfd, sec), sec->lma);
2987 (* _bfd_error_handler)
2988 (_(" whereas segment starts at 0x%x"),
2993 p->p_memsz += adjust;
2996 if ((flags & SEC_LOAD) != 0)
2997 p->p_filesz += adjust;
3002 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3003 used in a linker script we may have a section with
3004 SEC_LOAD clear but which is supposed to have
3006 if ((flags & SEC_LOAD) != 0
3007 || (flags & SEC_HAS_CONTENTS) != 0)
3008 off += sec->_raw_size;
3010 if ((flags & SEC_ALLOC) != 0)
3011 voff += sec->_raw_size;
3014 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3016 /* The actual "note" segment has i == 0.
3017 This is the one that actually contains everything. */
3021 p->p_filesz = sec->_raw_size;
3022 off += sec->_raw_size;
3027 /* Fake sections -- don't need to be written. */
3030 flags = sec->flags = 0;
3037 p->p_memsz += sec->_raw_size;
3039 if ((flags & SEC_LOAD) != 0)
3040 p->p_filesz += sec->_raw_size;
3042 if (align > p->p_align
3043 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3047 if (! m->p_flags_valid)
3050 if ((flags & SEC_CODE) != 0)
3052 if ((flags & SEC_READONLY) == 0)
3058 /* Now that we have set the section file positions, we can set up
3059 the file positions for the non PT_LOAD segments. */
3060 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3064 if (p->p_type != PT_LOAD && m->count > 0)
3066 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3067 p->p_offset = m->sections[0]->filepos;
3071 if (m->includes_filehdr)
3073 p->p_vaddr = filehdr_vaddr;
3074 if (! m->p_paddr_valid)
3075 p->p_paddr = filehdr_paddr;
3077 else if (m->includes_phdrs)
3079 p->p_vaddr = phdrs_vaddr;
3080 if (! m->p_paddr_valid)
3081 p->p_paddr = phdrs_paddr;
3086 /* Clear out any program headers we allocated but did not use. */
3087 for (; count < alloc; count++, p++)
3089 memset (p, 0, sizeof *p);
3090 p->p_type = PT_NULL;
3093 elf_tdata (abfd)->phdr = phdrs;
3095 elf_tdata (abfd)->next_file_pos = off;
3097 /* Write out the program headers. */
3098 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3099 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3105 /* Get the size of the program header.
3107 If this is called by the linker before any of the section VMA's are set, it
3108 can't calculate the correct value for a strange memory layout. This only
3109 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3110 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3111 data segment (exclusive of .interp and .dynamic).
3113 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3114 will be two segments. */
3116 static bfd_size_type
3117 get_program_header_size (abfd)
3122 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3124 /* We can't return a different result each time we're called. */
3125 if (elf_tdata (abfd)->program_header_size != 0)
3126 return elf_tdata (abfd)->program_header_size;
3128 if (elf_tdata (abfd)->segment_map != NULL)
3130 struct elf_segment_map *m;
3133 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3135 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3136 return elf_tdata (abfd)->program_header_size;
3139 /* Assume we will need exactly two PT_LOAD segments: one for text
3140 and one for data. */
3143 s = bfd_get_section_by_name (abfd, ".interp");
3144 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3146 /* If we have a loadable interpreter section, we need a
3147 PT_INTERP segment. In this case, assume we also need a
3148 PT_PHDR segment, although that may not be true for all
3153 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3155 /* We need a PT_DYNAMIC segment. */
3159 for (s = abfd->sections; s != NULL; s = s->next)
3161 if ((s->flags & SEC_LOAD) != 0
3162 && strncmp (s->name, ".note", 5) == 0)
3164 /* We need a PT_NOTE segment. */
3169 /* Let the backend count up any program headers it might need. */
3170 if (bed->elf_backend_additional_program_headers)
3174 a = (*bed->elf_backend_additional_program_headers) (abfd);
3180 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3181 return elf_tdata (abfd)->program_header_size;
3184 /* Work out the file positions of all the sections. This is called by
3185 _bfd_elf_compute_section_file_positions. All the section sizes and
3186 VMAs must be known before this is called.
3188 We do not consider reloc sections at this point, unless they form
3189 part of the loadable image. Reloc sections are assigned file
3190 positions in assign_file_positions_for_relocs, which is called by
3191 write_object_contents and final_link.
3193 We also don't set the positions of the .symtab and .strtab here. */
3196 assign_file_positions_except_relocs (abfd)
3199 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3200 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3201 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3203 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3205 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3206 && bfd_get_format (abfd) != bfd_core)
3208 Elf_Internal_Shdr **hdrpp;
3211 /* Start after the ELF header. */
3212 off = i_ehdrp->e_ehsize;
3214 /* We are not creating an executable, which means that we are
3215 not creating a program header, and that the actual order of
3216 the sections in the file is unimportant. */
3217 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3219 Elf_Internal_Shdr *hdr;
3222 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3224 hdr->sh_offset = -1;
3227 if (i == tdata->symtab_section
3228 || i == tdata->strtab_section)
3230 hdr->sh_offset = -1;
3234 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3240 Elf_Internal_Shdr **hdrpp;
3242 /* Assign file positions for the loaded sections based on the
3243 assignment of sections to segments. */
3244 if (! assign_file_positions_for_segments (abfd))
3247 /* Assign file positions for the other sections. */
3249 off = elf_tdata (abfd)->next_file_pos;
3250 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3252 Elf_Internal_Shdr *hdr;
3255 if (hdr->bfd_section != NULL
3256 && hdr->bfd_section->filepos != 0)
3257 hdr->sh_offset = hdr->bfd_section->filepos;
3258 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3260 ((*_bfd_error_handler)
3261 (_("%s: warning: allocated section `%s' not in segment"),
3262 bfd_get_filename (abfd),
3263 (hdr->bfd_section == NULL
3265 : hdr->bfd_section->name)));
3266 if ((abfd->flags & D_PAGED) != 0)
3267 off += (hdr->sh_addr - off) % bed->maxpagesize;
3269 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3270 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3273 else if (hdr->sh_type == SHT_REL
3274 || hdr->sh_type == SHT_RELA
3275 || hdr == i_shdrpp[tdata->symtab_section]
3276 || hdr == i_shdrpp[tdata->strtab_section])
3277 hdr->sh_offset = -1;
3279 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3283 /* Place the section headers. */
3284 off = align_file_position (off, bed->s->file_align);
3285 i_ehdrp->e_shoff = off;
3286 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3288 elf_tdata (abfd)->next_file_pos = off;
3297 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3298 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3299 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3301 struct bfd_strtab_hash *shstrtab;
3302 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3304 i_ehdrp = elf_elfheader (abfd);
3305 i_shdrp = elf_elfsections (abfd);
3307 shstrtab = _bfd_elf_stringtab_init ();
3308 if (shstrtab == NULL)
3311 elf_shstrtab (abfd) = shstrtab;
3313 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3314 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3315 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3316 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3318 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3319 i_ehdrp->e_ident[EI_DATA] =
3320 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3321 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3323 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3324 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3326 for (count = EI_PAD; count < EI_NIDENT; count++)
3327 i_ehdrp->e_ident[count] = 0;
3329 if ((abfd->flags & DYNAMIC) != 0)
3330 i_ehdrp->e_type = ET_DYN;
3331 else if ((abfd->flags & EXEC_P) != 0)
3332 i_ehdrp->e_type = ET_EXEC;
3333 else if (bfd_get_format (abfd) == bfd_core)
3334 i_ehdrp->e_type = ET_CORE;
3336 i_ehdrp->e_type = ET_REL;
3338 switch (bfd_get_arch (abfd))
3340 case bfd_arch_unknown:
3341 i_ehdrp->e_machine = EM_NONE;
3343 case bfd_arch_sparc:
3344 if (bfd_get_arch_size (abfd) == 64)
3345 i_ehdrp->e_machine = EM_SPARCV9;
3347 i_ehdrp->e_machine = EM_SPARC;
3350 i_ehdrp->e_machine = EM_S370;
3353 if (bfd_get_arch_size (abfd) == 64)
3354 i_ehdrp->e_machine = EM_X86_64;
3356 i_ehdrp->e_machine = EM_386;
3359 i_ehdrp->e_machine = EM_IA_64;
3361 case bfd_arch_m68hc11:
3362 i_ehdrp->e_machine = EM_68HC11;
3364 case bfd_arch_m68hc12:
3365 i_ehdrp->e_machine = EM_68HC12;
3368 i_ehdrp->e_machine = EM_S390;
3371 i_ehdrp->e_machine = EM_68K;
3374 i_ehdrp->e_machine = EM_88K;
3377 i_ehdrp->e_machine = EM_860;
3380 i_ehdrp->e_machine = EM_960;
3382 case bfd_arch_mips: /* MIPS Rxxxx */
3383 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3386 i_ehdrp->e_machine = EM_PARISC;
3388 case bfd_arch_powerpc:
3389 i_ehdrp->e_machine = EM_PPC;
3391 case bfd_arch_alpha:
3392 i_ehdrp->e_machine = EM_ALPHA;
3395 i_ehdrp->e_machine = EM_SH;
3398 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3401 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3404 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3406 case bfd_arch_mcore:
3407 i_ehdrp->e_machine = EM_MCORE;
3410 i_ehdrp->e_machine = EM_AVR;
3413 switch (bfd_get_mach (abfd))
3416 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3420 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3423 i_ehdrp->e_machine = EM_ARM;
3426 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3428 case bfd_arch_mn10200:
3429 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3431 case bfd_arch_mn10300:
3432 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3435 i_ehdrp->e_machine = EM_PJ;
3438 i_ehdrp->e_machine = EM_CRIS;
3440 case bfd_arch_openrisc:
3441 i_ehdrp->e_machine = EM_OPENRISC;
3443 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3445 i_ehdrp->e_machine = EM_NONE;
3447 i_ehdrp->e_version = bed->s->ev_current;
3448 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3450 /* No program header, for now. */
3451 i_ehdrp->e_phoff = 0;
3452 i_ehdrp->e_phentsize = 0;
3453 i_ehdrp->e_phnum = 0;
3455 /* Each bfd section is section header entry. */
3456 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3457 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3459 /* If we're building an executable, we'll need a program header table. */
3460 if (abfd->flags & EXEC_P)
3462 /* It all happens later. */
3464 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3466 /* elf_build_phdrs() returns a (NULL-terminated) array of
3467 Elf_Internal_Phdrs. */
3468 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3469 i_ehdrp->e_phoff = outbase;
3470 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3475 i_ehdrp->e_phentsize = 0;
3477 i_ehdrp->e_phoff = 0;
3480 elf_tdata (abfd)->symtab_hdr.sh_name =
3481 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3482 elf_tdata (abfd)->strtab_hdr.sh_name =
3483 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3484 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3485 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3486 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3487 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3488 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3494 /* Assign file positions for all the reloc sections which are not part
3495 of the loadable file image. */
3498 _bfd_elf_assign_file_positions_for_relocs (abfd)
3503 Elf_Internal_Shdr **shdrpp;
3505 off = elf_tdata (abfd)->next_file_pos;
3507 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3508 i < elf_elfheader (abfd)->e_shnum;
3511 Elf_Internal_Shdr *shdrp;
3514 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3515 && shdrp->sh_offset == -1)
3516 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3519 elf_tdata (abfd)->next_file_pos = off;
3523 _bfd_elf_write_object_contents (abfd)
3526 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3527 Elf_Internal_Ehdr *i_ehdrp;
3528 Elf_Internal_Shdr **i_shdrp;
3532 if (! abfd->output_has_begun
3533 && ! _bfd_elf_compute_section_file_positions
3534 (abfd, (struct bfd_link_info *) NULL))
3537 i_shdrp = elf_elfsections (abfd);
3538 i_ehdrp = elf_elfheader (abfd);
3541 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3545 _bfd_elf_assign_file_positions_for_relocs (abfd);
3547 /* After writing the headers, we need to write the sections too... */
3548 for (count = 1; count < i_ehdrp->e_shnum; count++)
3550 if (bed->elf_backend_section_processing)
3551 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3552 if (i_shdrp[count]->contents)
3554 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3555 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3557 != i_shdrp[count]->sh_size))
3562 /* Write out the section header names. */
3563 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3564 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3567 if (bed->elf_backend_final_write_processing)
3568 (*bed->elf_backend_final_write_processing) (abfd,
3569 elf_tdata (abfd)->linker);
3571 return bed->s->write_shdrs_and_ehdr (abfd);
3575 _bfd_elf_write_corefile_contents (abfd)
3578 /* Hopefully this can be done just like an object file. */
3579 return _bfd_elf_write_object_contents (abfd);
3582 /* Given a section, search the header to find them. */
3585 _bfd_elf_section_from_bfd_section (abfd, asect)
3589 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3590 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3592 Elf_Internal_Shdr *hdr;
3593 int maxindex = elf_elfheader (abfd)->e_shnum;
3595 for (index = 0; index < maxindex; index++)
3597 hdr = i_shdrp[index];
3598 if (hdr->bfd_section == asect)
3602 if (bed->elf_backend_section_from_bfd_section)
3604 for (index = 0; index < maxindex; index++)
3608 hdr = i_shdrp[index];
3610 if ((*bed->elf_backend_section_from_bfd_section)
3611 (abfd, hdr, asect, &retval))
3616 if (bfd_is_abs_section (asect))
3618 if (bfd_is_com_section (asect))
3620 if (bfd_is_und_section (asect))
3623 bfd_set_error (bfd_error_nonrepresentable_section);
3628 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3632 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3634 asymbol **asym_ptr_ptr;
3636 asymbol *asym_ptr = *asym_ptr_ptr;
3638 flagword flags = asym_ptr->flags;
3640 /* When gas creates relocations against local labels, it creates its
3641 own symbol for the section, but does put the symbol into the
3642 symbol chain, so udata is 0. When the linker is generating
3643 relocatable output, this section symbol may be for one of the
3644 input sections rather than the output section. */
3645 if (asym_ptr->udata.i == 0
3646 && (flags & BSF_SECTION_SYM)
3647 && asym_ptr->section)
3651 if (asym_ptr->section->output_section != NULL)
3652 indx = asym_ptr->section->output_section->index;
3654 indx = asym_ptr->section->index;
3655 if (indx < elf_num_section_syms (abfd)
3656 && elf_section_syms (abfd)[indx] != NULL)
3657 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3660 idx = asym_ptr->udata.i;
3664 /* This case can occur when using --strip-symbol on a symbol
3665 which is used in a relocation entry. */
3666 (*_bfd_error_handler)
3667 (_("%s: symbol `%s' required but not present"),
3668 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3669 bfd_set_error (bfd_error_no_symbols);
3676 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3677 (long) asym_ptr, asym_ptr->name, idx, flags,
3678 elf_symbol_flags (flags));
3686 /* Copy private BFD data. This copies any program header information. */
3689 copy_private_bfd_data (ibfd, obfd)
3693 Elf_Internal_Ehdr * iehdr;
3694 struct elf_segment_map * map;
3695 struct elf_segment_map * map_first;
3696 struct elf_segment_map ** pointer_to_map;
3697 Elf_Internal_Phdr * segment;
3700 unsigned int num_segments;
3701 boolean phdr_included = false;
3702 bfd_vma maxpagesize;
3703 struct elf_segment_map * phdr_adjust_seg = NULL;
3704 unsigned int phdr_adjust_num = 0;
3706 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3707 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3710 if (elf_tdata (ibfd)->phdr == NULL)
3713 iehdr = elf_elfheader (ibfd);
3716 pointer_to_map = &map_first;
3718 num_segments = elf_elfheader (ibfd)->e_phnum;
3719 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3721 /* Returns the end address of the segment + 1. */
3722 #define SEGMENT_END(segment, start) \
3723 (start + (segment->p_memsz > segment->p_filesz \
3724 ? segment->p_memsz : segment->p_filesz))
3726 /* Returns true if the given section is contained within
3727 the given segment. VMA addresses are compared. */
3728 #define IS_CONTAINED_BY_VMA(section, segment) \
3729 (section->vma >= segment->p_vaddr \
3730 && (section->vma + section->_raw_size) \
3731 <= (SEGMENT_END (segment, segment->p_vaddr)))
3733 /* Returns true if the given section is contained within
3734 the given segment. LMA addresses are compared. */
3735 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3736 (section->lma >= base \
3737 && (section->lma + section->_raw_size) \
3738 <= SEGMENT_END (segment, base))
3740 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3741 #define IS_COREFILE_NOTE(p, s) \
3742 (p->p_type == PT_NOTE \
3743 && bfd_get_format (ibfd) == bfd_core \
3744 && s->vma == 0 && s->lma == 0 \
3745 && (bfd_vma) s->filepos >= p->p_offset \
3746 && (bfd_vma) s->filepos + s->_raw_size \
3747 <= p->p_offset + p->p_filesz)
3749 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3750 linker, which generates a PT_INTERP section with p_vaddr and
3751 p_memsz set to 0. */
3752 #define IS_SOLARIS_PT_INTERP(p, s) \
3754 && p->p_filesz > 0 \
3755 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3756 && s->_raw_size > 0 \
3757 && (bfd_vma) s->filepos >= p->p_offset \
3758 && ((bfd_vma) s->filepos + s->_raw_size \
3759 <= p->p_offset + p->p_filesz))
3761 /* Decide if the given section should be included in the given segment.
3762 A section will be included if:
3763 1. It is within the address space of the segment,
3764 2. It is an allocated segment,
3765 3. There is an output section associated with it,
3766 4. The section has not already been allocated to a previous segment. */
3767 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3768 ((((IS_CONTAINED_BY_VMA (section, segment) \
3769 || IS_SOLARIS_PT_INTERP (segment, section)) \
3770 && (section->flags & SEC_ALLOC) != 0) \
3771 || IS_COREFILE_NOTE (segment, section)) \
3772 && section->output_section != NULL \
3773 && section->segment_mark == false)
3775 /* Returns true iff seg1 starts after the end of seg2. */
3776 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3777 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3779 /* Returns true iff seg1 and seg2 overlap. */
3780 #define SEGMENT_OVERLAPS(seg1, seg2) \
3781 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3783 /* Initialise the segment mark field. */
3784 for (section = ibfd->sections; section != NULL; section = section->next)
3785 section->segment_mark = false;
3787 /* Scan through the segments specified in the program header
3788 of the input BFD. For this first scan we look for overlaps
3789 in the loadable segments. These can be created by wierd
3790 parameters to objcopy. */
3791 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3796 Elf_Internal_Phdr *segment2;
3798 if (segment->p_type != PT_LOAD)
3801 /* Determine if this segment overlaps any previous segments. */
3802 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3804 bfd_signed_vma extra_length;
3806 if (segment2->p_type != PT_LOAD
3807 || ! SEGMENT_OVERLAPS (segment, segment2))
3810 /* Merge the two segments together. */
3811 if (segment2->p_vaddr < segment->p_vaddr)
3813 /* Extend SEGMENT2 to include SEGMENT and then delete
3816 SEGMENT_END (segment, segment->p_vaddr)
3817 - SEGMENT_END (segment2, segment2->p_vaddr);
3819 if (extra_length > 0)
3821 segment2->p_memsz += extra_length;
3822 segment2->p_filesz += extra_length;
3825 segment->p_type = PT_NULL;
3827 /* Since we have deleted P we must restart the outer loop. */
3829 segment = elf_tdata (ibfd)->phdr;
3834 /* Extend SEGMENT to include SEGMENT2 and then delete
3837 SEGMENT_END (segment2, segment2->p_vaddr)
3838 - SEGMENT_END (segment, segment->p_vaddr);
3840 if (extra_length > 0)
3842 segment->p_memsz += extra_length;
3843 segment->p_filesz += extra_length;
3846 segment2->p_type = PT_NULL;
3851 /* The second scan attempts to assign sections to segments. */
3852 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3856 unsigned int section_count;
3857 asection ** sections;
3858 asection * output_section;
3860 bfd_vma matching_lma;
3861 bfd_vma suggested_lma;
3864 if (segment->p_type == PT_NULL)
3867 /* Compute how many sections might be placed into this segment. */
3869 for (section = ibfd->sections; section != NULL; section = section->next)
3870 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3873 /* Allocate a segment map big enough to contain all of the
3874 sections we have selected. */
3875 map = ((struct elf_segment_map *)
3877 (sizeof (struct elf_segment_map)
3878 + ((size_t) section_count - 1) * sizeof (asection *))));
3882 /* Initialise the fields of the segment map. Default to
3883 using the physical address of the segment in the input BFD. */
3885 map->p_type = segment->p_type;
3886 map->p_flags = segment->p_flags;
3887 map->p_flags_valid = 1;
3888 map->p_paddr = segment->p_paddr;
3889 map->p_paddr_valid = 1;
3891 /* Determine if this segment contains the ELF file header
3892 and if it contains the program headers themselves. */
3893 map->includes_filehdr = (segment->p_offset == 0
3894 && segment->p_filesz >= iehdr->e_ehsize);
3896 map->includes_phdrs = 0;
3898 if (! phdr_included || segment->p_type != PT_LOAD)
3900 map->includes_phdrs =
3901 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3902 && (segment->p_offset + segment->p_filesz
3903 >= ((bfd_vma) iehdr->e_phoff
3904 + iehdr->e_phnum * iehdr->e_phentsize)));
3906 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3907 phdr_included = true;
3910 if (section_count == 0)
3912 /* Special segments, such as the PT_PHDR segment, may contain
3913 no sections, but ordinary, loadable segments should contain
3915 if (segment->p_type == PT_LOAD)
3917 (_("%s: warning: Empty loadable segment detected\n"),
3918 bfd_get_filename (ibfd));
3921 *pointer_to_map = map;
3922 pointer_to_map = &map->next;
3927 /* Now scan the sections in the input BFD again and attempt
3928 to add their corresponding output sections to the segment map.
3929 The problem here is how to handle an output section which has
3930 been moved (ie had its LMA changed). There are four possibilities:
3932 1. None of the sections have been moved.
3933 In this case we can continue to use the segment LMA from the
3936 2. All of the sections have been moved by the same amount.
3937 In this case we can change the segment's LMA to match the LMA
3938 of the first section.
3940 3. Some of the sections have been moved, others have not.
3941 In this case those sections which have not been moved can be
3942 placed in the current segment which will have to have its size,
3943 and possibly its LMA changed, and a new segment or segments will
3944 have to be created to contain the other sections.
3946 4. The sections have been moved, but not be the same amount.
3947 In this case we can change the segment's LMA to match the LMA
3948 of the first section and we will have to create a new segment
3949 or segments to contain the other sections.
3951 In order to save time, we allocate an array to hold the section
3952 pointers that we are interested in. As these sections get assigned
3953 to a segment, they are removed from this array. */
3955 sections = (asection **) bfd_malloc
3956 (sizeof (asection *) * section_count);
3957 if (sections == NULL)
3960 /* Step One: Scan for segment vs section LMA conflicts.
3961 Also add the sections to the section array allocated above.
3962 Also add the sections to the current segment. In the common
3963 case, where the sections have not been moved, this means that
3964 we have completely filled the segment, and there is nothing
3970 for (j = 0, section = ibfd->sections;
3972 section = section->next)
3974 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3976 output_section = section->output_section;
3978 sections[j ++] = section;
3980 /* The Solaris native linker always sets p_paddr to 0.
3981 We try to catch that case here, and set it to the
3983 if (segment->p_paddr == 0
3984 && segment->p_vaddr != 0
3986 && output_section->lma != 0
3987 && (output_section->vma == (segment->p_vaddr
3988 + (map->includes_filehdr
3991 + (map->includes_phdrs
3992 ? iehdr->e_phnum * iehdr->e_phentsize
3994 map->p_paddr = segment->p_vaddr;
3996 /* Match up the physical address of the segment with the
3997 LMA address of the output section. */
3998 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3999 || IS_COREFILE_NOTE (segment, section))
4001 if (matching_lma == 0)
4002 matching_lma = output_section->lma;
4004 /* We assume that if the section fits within the segment
4005 then it does not overlap any other section within that
4007 map->sections[isec ++] = output_section;
4009 else if (suggested_lma == 0)
4010 suggested_lma = output_section->lma;
4014 BFD_ASSERT (j == section_count);
4016 /* Step Two: Adjust the physical address of the current segment,
4018 if (isec == section_count)
4020 /* All of the sections fitted within the segment as currently
4021 specified. This is the default case. Add the segment to
4022 the list of built segments and carry on to process the next
4023 program header in the input BFD. */
4024 map->count = section_count;
4025 *pointer_to_map = map;
4026 pointer_to_map = &map->next;
4033 if (matching_lma != 0)
4035 /* At least one section fits inside the current segment.
4036 Keep it, but modify its physical address to match the
4037 LMA of the first section that fitted. */
4038 map->p_paddr = matching_lma;
4042 /* None of the sections fitted inside the current segment.
4043 Change the current segment's physical address to match
4044 the LMA of the first section. */
4045 map->p_paddr = suggested_lma;
4048 /* Offset the segment physical address from the lma
4049 to allow for space taken up by elf headers. */
4050 if (map->includes_filehdr)
4051 map->p_paddr -= iehdr->e_ehsize;
4053 if (map->includes_phdrs)
4055 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4057 /* iehdr->e_phnum is just an estimate of the number
4058 of program headers that we will need. Make a note
4059 here of the number we used and the segment we chose
4060 to hold these headers, so that we can adjust the
4061 offset when we know the correct value. */
4062 phdr_adjust_num = iehdr->e_phnum;
4063 phdr_adjust_seg = map;
4067 /* Step Three: Loop over the sections again, this time assigning
4068 those that fit to the current segment and remvoing them from the
4069 sections array; but making sure not to leave large gaps. Once all
4070 possible sections have been assigned to the current segment it is
4071 added to the list of built segments and if sections still remain
4072 to be assigned, a new segment is constructed before repeating
4080 /* Fill the current segment with sections that fit. */
4081 for (j = 0; j < section_count; j++)
4083 section = sections[j];
4085 if (section == NULL)
4088 output_section = section->output_section;
4090 BFD_ASSERT (output_section != NULL);
4092 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4093 || IS_COREFILE_NOTE (segment, section))
4095 if (map->count == 0)
4097 /* If the first section in a segment does not start at
4098 the beginning of the segment, then something is
4100 if (output_section->lma !=
4102 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4103 + (map->includes_phdrs
4104 ? iehdr->e_phnum * iehdr->e_phentsize
4110 asection * prev_sec;
4112 prev_sec = map->sections[map->count - 1];
4114 /* If the gap between the end of the previous section
4115 and the start of this section is more than
4116 maxpagesize then we need to start a new segment. */
4117 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4118 < BFD_ALIGN (output_section->lma, maxpagesize))
4119 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4121 if (suggested_lma == 0)
4122 suggested_lma = output_section->lma;
4128 map->sections[map->count++] = output_section;
4131 section->segment_mark = true;
4133 else if (suggested_lma == 0)
4134 suggested_lma = output_section->lma;
4137 BFD_ASSERT (map->count > 0);
4139 /* Add the current segment to the list of built segments. */
4140 *pointer_to_map = map;
4141 pointer_to_map = &map->next;
4143 if (isec < section_count)
4145 /* We still have not allocated all of the sections to
4146 segments. Create a new segment here, initialise it
4147 and carry on looping. */
4148 map = ((struct elf_segment_map *)
4150 (sizeof (struct elf_segment_map)
4151 + ((size_t) section_count - 1)
4152 * sizeof (asection *))));
4156 /* Initialise the fields of the segment map. Set the physical
4157 physical address to the LMA of the first section that has
4158 not yet been assigned. */
4160 map->p_type = segment->p_type;
4161 map->p_flags = segment->p_flags;
4162 map->p_flags_valid = 1;
4163 map->p_paddr = suggested_lma;
4164 map->p_paddr_valid = 1;
4165 map->includes_filehdr = 0;
4166 map->includes_phdrs = 0;
4169 while (isec < section_count);
4174 /* The Solaris linker creates program headers in which all the
4175 p_paddr fields are zero. When we try to objcopy or strip such a
4176 file, we get confused. Check for this case, and if we find it
4177 reset the p_paddr_valid fields. */
4178 for (map = map_first; map != NULL; map = map->next)
4179 if (map->p_paddr != 0)
4183 for (map = map_first; map != NULL; map = map->next)
4184 map->p_paddr_valid = 0;
4187 elf_tdata (obfd)->segment_map = map_first;
4189 /* If we had to estimate the number of program headers that were
4190 going to be needed, then check our estimate know and adjust
4191 the offset if necessary. */
4192 if (phdr_adjust_seg != NULL)
4196 for (count = 0, map = map_first; map != NULL; map = map->next)
4199 if (count > phdr_adjust_num)
4200 phdr_adjust_seg->p_paddr
4201 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4205 /* Final Step: Sort the segments into ascending order of physical
4207 if (map_first != NULL)
4209 struct elf_segment_map *prev;
4212 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4214 /* Yes I know - its a bubble sort.... */
4215 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4217 /* Swap map and map->next. */
4218 prev->next = map->next;
4219 map->next = map->next->next;
4220 prev->next->next = map;
4230 #undef IS_CONTAINED_BY_VMA
4231 #undef IS_CONTAINED_BY_LMA
4232 #undef IS_COREFILE_NOTE
4233 #undef IS_SOLARIS_PT_INTERP
4234 #undef INCLUDE_SECTION_IN_SEGMENT
4235 #undef SEGMENT_AFTER_SEGMENT
4236 #undef SEGMENT_OVERLAPS
4240 /* Copy private section information. This copies over the entsize
4241 field, and sometimes the info field. */
4244 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4250 Elf_Internal_Shdr *ihdr, *ohdr;
4252 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4253 || obfd->xvec->flavour != bfd_target_elf_flavour)
4256 /* Copy over private BFD data if it has not already been copied.
4257 This must be done here, rather than in the copy_private_bfd_data
4258 entry point, because the latter is called after the section
4259 contents have been set, which means that the program headers have
4260 already been worked out. */
4261 if (elf_tdata (obfd)->segment_map == NULL
4262 && elf_tdata (ibfd)->phdr != NULL)
4266 /* Only set up the segments if there are no more SEC_ALLOC
4267 sections. FIXME: This won't do the right thing if objcopy is
4268 used to remove the last SEC_ALLOC section, since objcopy
4269 won't call this routine in that case. */
4270 for (s = isec->next; s != NULL; s = s->next)
4271 if ((s->flags & SEC_ALLOC) != 0)
4275 if (! copy_private_bfd_data (ibfd, obfd))
4280 ihdr = &elf_section_data (isec)->this_hdr;
4281 ohdr = &elf_section_data (osec)->this_hdr;
4283 ohdr->sh_entsize = ihdr->sh_entsize;
4285 if (ihdr->sh_type == SHT_SYMTAB
4286 || ihdr->sh_type == SHT_DYNSYM
4287 || ihdr->sh_type == SHT_GNU_verneed
4288 || ihdr->sh_type == SHT_GNU_verdef)
4289 ohdr->sh_info = ihdr->sh_info;
4291 elf_section_data (osec)->use_rela_p
4292 = elf_section_data (isec)->use_rela_p;
4297 /* Copy private symbol information. If this symbol is in a section
4298 which we did not map into a BFD section, try to map the section
4299 index correctly. We use special macro definitions for the mapped
4300 section indices; these definitions are interpreted by the
4301 swap_out_syms function. */
4303 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4304 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4305 #define MAP_STRTAB (SHN_LORESERVE - 3)
4306 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4309 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4315 elf_symbol_type *isym, *osym;
4317 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4318 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4321 isym = elf_symbol_from (ibfd, isymarg);
4322 osym = elf_symbol_from (obfd, osymarg);
4326 && bfd_is_abs_section (isym->symbol.section))
4330 shndx = isym->internal_elf_sym.st_shndx;
4331 if (shndx == elf_onesymtab (ibfd))
4332 shndx = MAP_ONESYMTAB;
4333 else if (shndx == elf_dynsymtab (ibfd))
4334 shndx = MAP_DYNSYMTAB;
4335 else if (shndx == elf_tdata (ibfd)->strtab_section)
4337 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4338 shndx = MAP_SHSTRTAB;
4339 osym->internal_elf_sym.st_shndx = shndx;
4345 /* Swap out the symbols. */
4348 swap_out_syms (abfd, sttp, relocatable_p)
4350 struct bfd_strtab_hash **sttp;
4353 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4355 if (!elf_map_symbols (abfd))
4358 /* Dump out the symtabs. */
4360 int symcount = bfd_get_symcount (abfd);
4361 asymbol **syms = bfd_get_outsymbols (abfd);
4362 struct bfd_strtab_hash *stt;
4363 Elf_Internal_Shdr *symtab_hdr;
4364 Elf_Internal_Shdr *symstrtab_hdr;
4365 char *outbound_syms;
4368 stt = _bfd_elf_stringtab_init ();
4372 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4373 symtab_hdr->sh_type = SHT_SYMTAB;
4374 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4375 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4376 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4377 symtab_hdr->sh_addralign = bed->s->file_align;
4379 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4380 symstrtab_hdr->sh_type = SHT_STRTAB;
4382 outbound_syms = bfd_alloc (abfd,
4383 (1 + symcount) * bed->s->sizeof_sym);
4384 if (outbound_syms == NULL)
4386 symtab_hdr->contents = (PTR) outbound_syms;
4388 /* now generate the data (for "contents") */
4390 /* Fill in zeroth symbol and swap it out. */
4391 Elf_Internal_Sym sym;
4397 sym.st_shndx = SHN_UNDEF;
4398 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4399 outbound_syms += bed->s->sizeof_sym;
4401 for (idx = 0; idx < symcount; idx++)
4403 Elf_Internal_Sym sym;
4404 bfd_vma value = syms[idx]->value;
4405 elf_symbol_type *type_ptr;
4406 flagword flags = syms[idx]->flags;
4409 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4411 /* Local section symbols have no name. */
4416 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4419 if (sym.st_name == (unsigned long) -1)
4423 type_ptr = elf_symbol_from (abfd, syms[idx]);
4425 if ((flags & BSF_SECTION_SYM) == 0
4426 && bfd_is_com_section (syms[idx]->section))
4428 /* ELF common symbols put the alignment into the `value' field,
4429 and the size into the `size' field. This is backwards from
4430 how BFD handles it, so reverse it here. */
4431 sym.st_size = value;
4432 if (type_ptr == NULL
4433 || type_ptr->internal_elf_sym.st_value == 0)
4434 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4436 sym.st_value = type_ptr->internal_elf_sym.st_value;
4437 sym.st_shndx = _bfd_elf_section_from_bfd_section
4438 (abfd, syms[idx]->section);
4442 asection *sec = syms[idx]->section;
4445 if (sec->output_section)
4447 value += sec->output_offset;
4448 sec = sec->output_section;
4450 /* Don't add in the section vma for relocatable output. */
4451 if (! relocatable_p)
4453 sym.st_value = value;
4454 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4456 if (bfd_is_abs_section (sec)
4458 && type_ptr->internal_elf_sym.st_shndx != 0)
4460 /* This symbol is in a real ELF section which we did
4461 not create as a BFD section. Undo the mapping done
4462 by copy_private_symbol_data. */
4463 shndx = type_ptr->internal_elf_sym.st_shndx;
4467 shndx = elf_onesymtab (abfd);
4470 shndx = elf_dynsymtab (abfd);
4473 shndx = elf_tdata (abfd)->strtab_section;
4476 shndx = elf_tdata (abfd)->shstrtab_section;
4484 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4490 /* Writing this would be a hell of a lot easier if
4491 we had some decent documentation on bfd, and
4492 knew what to expect of the library, and what to
4493 demand of applications. For example, it
4494 appears that `objcopy' might not set the
4495 section of a symbol to be a section that is
4496 actually in the output file. */
4497 sec2 = bfd_get_section_by_name (abfd, sec->name);
4498 BFD_ASSERT (sec2 != 0);
4499 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4500 BFD_ASSERT (shndx != -1);
4504 sym.st_shndx = shndx;
4507 if ((flags & BSF_FUNCTION) != 0)
4509 else if ((flags & BSF_OBJECT) != 0)
4514 /* Processor-specific types */
4515 if (type_ptr != NULL
4516 && bed->elf_backend_get_symbol_type)
4517 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4519 if (flags & BSF_SECTION_SYM)
4521 if (flags & BSF_GLOBAL)
4522 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
4524 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4526 else if (bfd_is_com_section (syms[idx]->section))
4527 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4528 else if (bfd_is_und_section (syms[idx]->section))
4529 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4533 else if (flags & BSF_FILE)
4534 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4537 int bind = STB_LOCAL;
4539 if (flags & BSF_LOCAL)
4541 else if (flags & BSF_WEAK)
4543 else if (flags & BSF_GLOBAL)
4546 sym.st_info = ELF_ST_INFO (bind, type);
4549 if (type_ptr != NULL)
4550 sym.st_other = type_ptr->internal_elf_sym.st_other;
4554 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4555 outbound_syms += bed->s->sizeof_sym;
4559 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4560 symstrtab_hdr->sh_type = SHT_STRTAB;
4562 symstrtab_hdr->sh_flags = 0;
4563 symstrtab_hdr->sh_addr = 0;
4564 symstrtab_hdr->sh_entsize = 0;
4565 symstrtab_hdr->sh_link = 0;
4566 symstrtab_hdr->sh_info = 0;
4567 symstrtab_hdr->sh_addralign = 1;
4573 /* Return the number of bytes required to hold the symtab vector.
4575 Note that we base it on the count plus 1, since we will null terminate
4576 the vector allocated based on this size. However, the ELF symbol table
4577 always has a dummy entry as symbol #0, so it ends up even. */
4580 _bfd_elf_get_symtab_upper_bound (abfd)
4585 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4587 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4588 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4594 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4599 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4601 if (elf_dynsymtab (abfd) == 0)
4603 bfd_set_error (bfd_error_invalid_operation);
4607 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4608 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4614 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4615 bfd *abfd ATTRIBUTE_UNUSED;
4618 return (asect->reloc_count + 1) * sizeof (arelent *);
4621 /* Canonicalize the relocs. */
4624 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4633 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4639 tblptr = section->relocation;
4640 for (i = 0; i < section->reloc_count; i++)
4641 *relptr++ = tblptr++;
4645 return section->reloc_count;
4649 _bfd_elf_get_symtab (abfd, alocation)
4651 asymbol **alocation;
4653 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4654 (abfd, alocation, false);
4657 bfd_get_symcount (abfd) = symcount;
4662 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4664 asymbol **alocation;
4666 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4667 (abfd, alocation, true);
4670 /* Return the size required for the dynamic reloc entries. Any
4671 section that was actually installed in the BFD, and has type
4672 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4673 considered to be a dynamic reloc section. */
4676 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4682 if (elf_dynsymtab (abfd) == 0)
4684 bfd_set_error (bfd_error_invalid_operation);
4688 ret = sizeof (arelent *);
4689 for (s = abfd->sections; s != NULL; s = s->next)
4690 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4691 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4692 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4693 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4694 * sizeof (arelent *));
4699 /* Canonicalize the dynamic relocation entries. Note that we return
4700 the dynamic relocations as a single block, although they are
4701 actually associated with particular sections; the interface, which
4702 was designed for SunOS style shared libraries, expects that there
4703 is only one set of dynamic relocs. Any section that was actually
4704 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4705 the dynamic symbol table, is considered to be a dynamic reloc
4709 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4714 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4718 if (elf_dynsymtab (abfd) == 0)
4720 bfd_set_error (bfd_error_invalid_operation);
4724 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4726 for (s = abfd->sections; s != NULL; s = s->next)
4728 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4729 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4730 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4735 if (! (*slurp_relocs) (abfd, s, syms, true))
4737 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4739 for (i = 0; i < count; i++)
4750 /* Read in the version information. */
4753 _bfd_elf_slurp_version_tables (abfd)
4756 bfd_byte *contents = NULL;
4758 if (elf_dynverdef (abfd) != 0)
4760 Elf_Internal_Shdr *hdr;
4761 Elf_External_Verdef *everdef;
4762 Elf_Internal_Verdef *iverdef;
4763 Elf_Internal_Verdef *iverdefarr;
4764 Elf_Internal_Verdef iverdefmem;
4766 unsigned int maxidx;
4768 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4770 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4771 if (contents == NULL)
4773 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4774 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4777 /* We know the number of entries in the section but not the maximum
4778 index. Therefore we have to run through all entries and find
4780 everdef = (Elf_External_Verdef *) contents;
4782 for (i = 0; i < hdr->sh_info; ++i)
4784 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4786 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
4787 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
4789 everdef = ((Elf_External_Verdef *)
4790 ((bfd_byte *) everdef + iverdefmem.vd_next));
4793 elf_tdata (abfd)->verdef =
4794 ((Elf_Internal_Verdef *)
4795 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4796 if (elf_tdata (abfd)->verdef == NULL)
4799 elf_tdata (abfd)->cverdefs = maxidx;
4801 everdef = (Elf_External_Verdef *) contents;
4802 iverdefarr = elf_tdata (abfd)->verdef;
4803 for (i = 0; i < hdr->sh_info; i++)
4805 Elf_External_Verdaux *everdaux;
4806 Elf_Internal_Verdaux *iverdaux;
4809 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4811 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4812 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4814 iverdef->vd_bfd = abfd;
4816 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4819 * sizeof (Elf_Internal_Verdaux))));
4820 if (iverdef->vd_auxptr == NULL)
4823 everdaux = ((Elf_External_Verdaux *)
4824 ((bfd_byte *) everdef + iverdef->vd_aux));
4825 iverdaux = iverdef->vd_auxptr;
4826 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4828 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4830 iverdaux->vda_nodename =
4831 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4832 iverdaux->vda_name);
4833 if (iverdaux->vda_nodename == NULL)
4836 if (j + 1 < iverdef->vd_cnt)
4837 iverdaux->vda_nextptr = iverdaux + 1;
4839 iverdaux->vda_nextptr = NULL;
4841 everdaux = ((Elf_External_Verdaux *)
4842 ((bfd_byte *) everdaux + iverdaux->vda_next));
4845 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4847 if (i + 1 < hdr->sh_info)
4848 iverdef->vd_nextdef = iverdef + 1;
4850 iverdef->vd_nextdef = NULL;
4852 everdef = ((Elf_External_Verdef *)
4853 ((bfd_byte *) everdef + iverdef->vd_next));
4860 if (elf_dynverref (abfd) != 0)
4862 Elf_Internal_Shdr *hdr;
4863 Elf_External_Verneed *everneed;
4864 Elf_Internal_Verneed *iverneed;
4867 hdr = &elf_tdata (abfd)->dynverref_hdr;
4869 elf_tdata (abfd)->verref =
4870 ((Elf_Internal_Verneed *)
4871 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4872 if (elf_tdata (abfd)->verref == NULL)
4875 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4877 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4878 if (contents == NULL)
4880 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4881 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4884 everneed = (Elf_External_Verneed *) contents;
4885 iverneed = elf_tdata (abfd)->verref;
4886 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4888 Elf_External_Vernaux *evernaux;
4889 Elf_Internal_Vernaux *ivernaux;
4892 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4894 iverneed->vn_bfd = abfd;
4896 iverneed->vn_filename =
4897 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4899 if (iverneed->vn_filename == NULL)
4902 iverneed->vn_auxptr =
4903 ((Elf_Internal_Vernaux *)
4905 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4907 evernaux = ((Elf_External_Vernaux *)
4908 ((bfd_byte *) everneed + iverneed->vn_aux));
4909 ivernaux = iverneed->vn_auxptr;
4910 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4912 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4914 ivernaux->vna_nodename =
4915 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4916 ivernaux->vna_name);
4917 if (ivernaux->vna_nodename == NULL)
4920 if (j + 1 < iverneed->vn_cnt)
4921 ivernaux->vna_nextptr = ivernaux + 1;
4923 ivernaux->vna_nextptr = NULL;
4925 evernaux = ((Elf_External_Vernaux *)
4926 ((bfd_byte *) evernaux + ivernaux->vna_next));
4929 if (i + 1 < hdr->sh_info)
4930 iverneed->vn_nextref = iverneed + 1;
4932 iverneed->vn_nextref = NULL;
4934 everneed = ((Elf_External_Verneed *)
4935 ((bfd_byte *) everneed + iverneed->vn_next));
4945 if (contents == NULL)
4951 _bfd_elf_make_empty_symbol (abfd)
4954 elf_symbol_type *newsym;
4956 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4961 newsym->symbol.the_bfd = abfd;
4962 return &newsym->symbol;
4967 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4968 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4972 bfd_symbol_info (symbol, ret);
4975 /* Return whether a symbol name implies a local symbol. Most targets
4976 use this function for the is_local_label_name entry point, but some
4980 _bfd_elf_is_local_label_name (abfd, name)
4981 bfd *abfd ATTRIBUTE_UNUSED;
4984 /* Normal local symbols start with ``.L''. */
4985 if (name[0] == '.' && name[1] == 'L')
4988 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4989 DWARF debugging symbols starting with ``..''. */
4990 if (name[0] == '.' && name[1] == '.')
4993 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4994 emitting DWARF debugging output. I suspect this is actually a
4995 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4996 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4997 underscore to be emitted on some ELF targets). For ease of use,
4998 we treat such symbols as local. */
4999 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5006 _bfd_elf_get_lineno (ignore_abfd, symbol)
5007 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5008 asymbol *symbol ATTRIBUTE_UNUSED;
5015 _bfd_elf_set_arch_mach (abfd, arch, machine)
5017 enum bfd_architecture arch;
5018 unsigned long machine;
5020 /* If this isn't the right architecture for this backend, and this
5021 isn't the generic backend, fail. */
5022 if (arch != get_elf_backend_data (abfd)->arch
5023 && arch != bfd_arch_unknown
5024 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5027 return bfd_default_set_arch_mach (abfd, arch, machine);
5030 /* Find the function to a particular section and offset,
5031 for error reporting. */
5034 elf_find_function (abfd, section, symbols, offset,
5035 filename_ptr, functionname_ptr)
5036 bfd *abfd ATTRIBUTE_UNUSED;
5040 const char **filename_ptr;
5041 const char **functionname_ptr;
5043 const char *filename;
5052 for (p = symbols; *p != NULL; p++)
5056 q = (elf_symbol_type *) *p;
5058 if (bfd_get_section (&q->symbol) != section)
5061 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5066 filename = bfd_asymbol_name (&q->symbol);
5070 if (q->symbol.section == section
5071 && q->symbol.value >= low_func
5072 && q->symbol.value <= offset)
5074 func = (asymbol *) q;
5075 low_func = q->symbol.value;
5085 *filename_ptr = filename;
5086 if (functionname_ptr)
5087 *functionname_ptr = bfd_asymbol_name (func);
5092 /* Find the nearest line to a particular section and offset,
5093 for error reporting. */
5096 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5097 filename_ptr, functionname_ptr, line_ptr)
5102 const char **filename_ptr;
5103 const char **functionname_ptr;
5104 unsigned int *line_ptr;
5108 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5109 filename_ptr, functionname_ptr,
5112 if (!*functionname_ptr)
5113 elf_find_function (abfd, section, symbols, offset,
5114 *filename_ptr ? NULL : filename_ptr,
5120 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5121 filename_ptr, functionname_ptr,
5123 &elf_tdata (abfd)->dwarf2_find_line_info))
5125 if (!*functionname_ptr)
5126 elf_find_function (abfd, section, symbols, offset,
5127 *filename_ptr ? NULL : filename_ptr,
5133 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5134 &found, filename_ptr,
5135 functionname_ptr, line_ptr,
5136 &elf_tdata (abfd)->line_info))
5141 if (symbols == NULL)
5144 if (! elf_find_function (abfd, section, symbols, offset,
5145 filename_ptr, functionname_ptr))
5153 _bfd_elf_sizeof_headers (abfd, reloc)
5159 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5161 ret += get_program_header_size (abfd);
5166 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5171 bfd_size_type count;
5173 Elf_Internal_Shdr *hdr;
5175 if (! abfd->output_has_begun
5176 && ! _bfd_elf_compute_section_file_positions
5177 (abfd, (struct bfd_link_info *) NULL))
5180 hdr = &elf_section_data (section)->this_hdr;
5182 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5184 if (bfd_write (location, 1, count, abfd) != count)
5191 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5192 bfd *abfd ATTRIBUTE_UNUSED;
5193 arelent *cache_ptr ATTRIBUTE_UNUSED;
5194 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5201 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5204 Elf_Internal_Rel *dst;
5210 /* Try to convert a non-ELF reloc into an ELF one. */
5213 _bfd_elf_validate_reloc (abfd, areloc)
5217 /* Check whether we really have an ELF howto. */
5219 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5221 bfd_reloc_code_real_type code;
5222 reloc_howto_type *howto;
5224 /* Alien reloc: Try to determine its type to replace it with an
5225 equivalent ELF reloc. */
5227 if (areloc->howto->pc_relative)
5229 switch (areloc->howto->bitsize)
5232 code = BFD_RELOC_8_PCREL;
5235 code = BFD_RELOC_12_PCREL;
5238 code = BFD_RELOC_16_PCREL;
5241 code = BFD_RELOC_24_PCREL;
5244 code = BFD_RELOC_32_PCREL;
5247 code = BFD_RELOC_64_PCREL;
5253 howto = bfd_reloc_type_lookup (abfd, code);
5255 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5257 if (howto->pcrel_offset)
5258 areloc->addend += areloc->address;
5260 areloc->addend -= areloc->address; /* addend is unsigned!! */
5265 switch (areloc->howto->bitsize)
5271 code = BFD_RELOC_14;
5274 code = BFD_RELOC_16;
5277 code = BFD_RELOC_26;
5280 code = BFD_RELOC_32;
5283 code = BFD_RELOC_64;
5289 howto = bfd_reloc_type_lookup (abfd, code);
5293 areloc->howto = howto;
5301 (*_bfd_error_handler)
5302 (_("%s: unsupported relocation type %s"),
5303 bfd_get_filename (abfd), areloc->howto->name);
5304 bfd_set_error (bfd_error_bad_value);
5309 _bfd_elf_close_and_cleanup (abfd)
5312 if (bfd_get_format (abfd) == bfd_object)
5314 if (elf_shstrtab (abfd) != NULL)
5315 _bfd_stringtab_free (elf_shstrtab (abfd));
5318 return _bfd_generic_close_and_cleanup (abfd);
5321 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5322 in the relocation's offset. Thus we cannot allow any sort of sanity
5323 range-checking to interfere. There is nothing else to do in processing
5326 bfd_reloc_status_type
5327 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5328 bfd *abfd ATTRIBUTE_UNUSED;
5329 arelent *re ATTRIBUTE_UNUSED;
5330 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5331 PTR data ATTRIBUTE_UNUSED;
5332 asection *is ATTRIBUTE_UNUSED;
5333 bfd *obfd ATTRIBUTE_UNUSED;
5334 char **errmsg ATTRIBUTE_UNUSED;
5336 return bfd_reloc_ok;
5339 /* Elf core file support. Much of this only works on native
5340 toolchains, since we rely on knowing the
5341 machine-dependent procfs structure in order to pick
5342 out details about the corefile. */
5344 #ifdef HAVE_SYS_PROCFS_H
5345 # include <sys/procfs.h>
5348 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5351 elfcore_make_pid (abfd)
5354 return ((elf_tdata (abfd)->core_lwpid << 16)
5355 + (elf_tdata (abfd)->core_pid));
5358 /* If there isn't a section called NAME, make one, using
5359 data from SECT. Note, this function will generate a
5360 reference to NAME, so you shouldn't deallocate or
5364 elfcore_maybe_make_sect (abfd, name, sect)
5371 if (bfd_get_section_by_name (abfd, name) != NULL)
5374 sect2 = bfd_make_section (abfd, name);
5378 sect2->_raw_size = sect->_raw_size;
5379 sect2->filepos = sect->filepos;
5380 sect2->flags = sect->flags;
5381 sect2->alignment_power = sect->alignment_power;
5385 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5386 actually creates up to two pseudosections:
5387 - For the single-threaded case, a section named NAME, unless
5388 such a section already exists.
5389 - For the multi-threaded case, a section named "NAME/PID", where
5390 PID is elfcore_make_pid (abfd).
5391 Both pseudosections have identical contents. */
5393 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5400 char *threaded_name;
5403 /* Build the section name. */
5405 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5406 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5407 if (threaded_name == NULL)
5409 strcpy (threaded_name, buf);
5411 sect = bfd_make_section (abfd, threaded_name);
5414 sect->_raw_size = size;
5415 sect->filepos = filepos;
5416 sect->flags = SEC_HAS_CONTENTS;
5417 sect->alignment_power = 2;
5419 return elfcore_maybe_make_sect (abfd, name, sect);
5422 /* prstatus_t exists on:
5424 linux 2.[01] + glibc
5428 #if defined (HAVE_PRSTATUS_T)
5429 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5432 elfcore_grok_prstatus (abfd, note)
5434 Elf_Internal_Note *note;
5439 if (note->descsz == sizeof (prstatus_t))
5443 raw_size = sizeof (prstat.pr_reg);
5444 offset = offsetof (prstatus_t, pr_reg);
5445 memcpy (&prstat, note->descdata, sizeof (prstat));
5447 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5448 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5450 /* pr_who exists on:
5453 pr_who doesn't exist on:
5456 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5457 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5460 #if defined (HAVE_PRSTATUS32_T)
5461 else if (note->descsz == sizeof (prstatus32_t))
5463 /* 64-bit host, 32-bit corefile */
5464 prstatus32_t prstat;
5466 raw_size = sizeof (prstat.pr_reg);
5467 offset = offsetof (prstatus32_t, pr_reg);
5468 memcpy (&prstat, note->descdata, sizeof (prstat));
5470 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5471 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5473 /* pr_who exists on:
5476 pr_who doesn't exist on:
5479 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5480 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5483 #endif /* HAVE_PRSTATUS32_T */
5486 /* Fail - we don't know how to handle any other
5487 note size (ie. data object type). */
5491 /* Make a ".reg/999" section and a ".reg" section. */
5492 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5493 raw_size, note->descpos + offset);
5495 #endif /* defined (HAVE_PRSTATUS_T) */
5497 /* Create a pseudosection containing the exact contents of NOTE. */
5499 elfcore_make_note_pseudosection (abfd, name, note)
5502 Elf_Internal_Note *note;
5504 return _bfd_elfcore_make_pseudosection (abfd, name,
5505 note->descsz, note->descpos);
5508 /* There isn't a consistent prfpregset_t across platforms,
5509 but it doesn't matter, because we don't have to pick this
5510 data structure apart. */
5513 elfcore_grok_prfpreg (abfd, note)
5515 Elf_Internal_Note *note;
5517 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5520 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5521 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5525 elfcore_grok_prxfpreg (abfd, note)
5527 Elf_Internal_Note *note;
5529 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5532 #if defined (HAVE_PRPSINFO_T)
5533 typedef prpsinfo_t elfcore_psinfo_t;
5534 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5535 typedef prpsinfo32_t elfcore_psinfo32_t;
5539 #if defined (HAVE_PSINFO_T)
5540 typedef psinfo_t elfcore_psinfo_t;
5541 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5542 typedef psinfo32_t elfcore_psinfo32_t;
5546 /* return a malloc'ed copy of a string at START which is at
5547 most MAX bytes long, possibly without a terminating '\0'.
5548 the copy will always have a terminating '\0'. */
5551 _bfd_elfcore_strndup (abfd, start, max)
5557 char *end = memchr (start, '\0', max);
5565 dup = bfd_alloc (abfd, len + 1);
5569 memcpy (dup, start, len);
5575 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5576 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
5579 elfcore_grok_psinfo (abfd, note)
5581 Elf_Internal_Note *note;
5583 if (note->descsz == sizeof (elfcore_psinfo_t))
5585 elfcore_psinfo_t psinfo;
5587 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5589 elf_tdata (abfd)->core_program
5590 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5591 sizeof (psinfo.pr_fname));
5593 elf_tdata (abfd)->core_command
5594 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5595 sizeof (psinfo.pr_psargs));
5597 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5598 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5600 /* 64-bit host, 32-bit corefile */
5601 elfcore_psinfo32_t psinfo;
5603 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5605 elf_tdata (abfd)->core_program
5606 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5607 sizeof (psinfo.pr_fname));
5609 elf_tdata (abfd)->core_command
5610 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5611 sizeof (psinfo.pr_psargs));
5617 /* Fail - we don't know how to handle any other
5618 note size (ie. data object type). */
5622 /* Note that for some reason, a spurious space is tacked
5623 onto the end of the args in some (at least one anyway)
5624 implementations, so strip it off if it exists. */
5627 char *command = elf_tdata (abfd)->core_command;
5628 int n = strlen (command);
5630 if (0 < n && command[n - 1] == ' ')
5631 command[n - 1] = '\0';
5636 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5638 #if defined (HAVE_PSTATUS_T)
5640 elfcore_grok_pstatus (abfd, note)
5642 Elf_Internal_Note *note;
5644 if (note->descsz == sizeof (pstatus_t)
5645 #if defined (HAVE_PXSTATUS_T)
5646 || note->descsz == sizeof (pxstatus_t)
5652 memcpy (&pstat, note->descdata, sizeof (pstat));
5654 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5656 #if defined (HAVE_PSTATUS32_T)
5657 else if (note->descsz == sizeof (pstatus32_t))
5659 /* 64-bit host, 32-bit corefile */
5662 memcpy (&pstat, note->descdata, sizeof (pstat));
5664 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5667 /* Could grab some more details from the "representative"
5668 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5669 NT_LWPSTATUS note, presumably. */
5673 #endif /* defined (HAVE_PSTATUS_T) */
5675 #if defined (HAVE_LWPSTATUS_T)
5677 elfcore_grok_lwpstatus (abfd, note)
5679 Elf_Internal_Note *note;
5681 lwpstatus_t lwpstat;
5686 if (note->descsz != sizeof (lwpstat)
5687 #if defined (HAVE_LWPXSTATUS_T)
5688 && note->descsz != sizeof (lwpxstatus_t)
5693 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5695 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5696 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5698 /* Make a ".reg/999" section. */
5700 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5701 name = bfd_alloc (abfd, strlen (buf) + 1);
5706 sect = bfd_make_section (abfd, name);
5710 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5711 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5712 sect->filepos = note->descpos
5713 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5716 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5717 sect->_raw_size = sizeof (lwpstat.pr_reg);
5718 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5721 sect->flags = SEC_HAS_CONTENTS;
5722 sect->alignment_power = 2;
5724 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5727 /* Make a ".reg2/999" section */
5729 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5730 name = bfd_alloc (abfd, strlen (buf) + 1);
5735 sect = bfd_make_section (abfd, name);
5739 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5740 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5741 sect->filepos = note->descpos
5742 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5745 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5746 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5747 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5750 sect->flags = SEC_HAS_CONTENTS;
5751 sect->alignment_power = 2;
5753 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
5755 #endif /* defined (HAVE_LWPSTATUS_T) */
5757 #if defined (HAVE_WIN32_PSTATUS_T)
5759 elfcore_grok_win32pstatus (abfd, note)
5761 Elf_Internal_Note *note;
5766 win32_pstatus_t pstatus;
5768 if (note->descsz < sizeof (pstatus))
5771 memcpy (&pstatus, note->descdata, note->descsz);
5773 switch (pstatus.data_type)
5775 case NOTE_INFO_PROCESS:
5776 /* FIXME: need to add ->core_command. */
5777 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5778 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5781 case NOTE_INFO_THREAD:
5782 /* Make a ".reg/999" section. */
5783 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5785 name = bfd_alloc (abfd, strlen (buf) + 1);
5791 sect = bfd_make_section (abfd, name);
5795 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5796 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5797 data.thread_info.thread_context);
5798 sect->flags = SEC_HAS_CONTENTS;
5799 sect->alignment_power = 2;
5801 if (pstatus.data.thread_info.is_active_thread)
5802 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5806 case NOTE_INFO_MODULE:
5807 /* Make a ".module/xxxxxxxx" section. */
5808 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5810 name = bfd_alloc (abfd, strlen (buf) + 1);
5816 sect = bfd_make_section (abfd, name);
5821 sect->_raw_size = note->descsz;
5822 sect->filepos = note->descpos;
5823 sect->flags = SEC_HAS_CONTENTS;
5824 sect->alignment_power = 2;
5833 #endif /* HAVE_WIN32_PSTATUS_T */
5836 elfcore_grok_note (abfd, note)
5838 Elf_Internal_Note *note;
5840 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5848 if (bed->elf_backend_grok_prstatus)
5849 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
5851 #if defined (HAVE_PRSTATUS_T)
5852 return elfcore_grok_prstatus (abfd, note);
5857 #if defined (HAVE_PSTATUS_T)
5859 return elfcore_grok_pstatus (abfd, note);
5862 #if defined (HAVE_LWPSTATUS_T)
5864 return elfcore_grok_lwpstatus (abfd, note);
5867 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5868 return elfcore_grok_prfpreg (abfd, note);
5870 #if defined (HAVE_WIN32_PSTATUS_T)
5871 case NT_WIN32PSTATUS:
5872 return elfcore_grok_win32pstatus (abfd, note);
5875 case NT_PRXFPREG: /* Linux SSE extension */
5876 if (note->namesz == 5
5877 && ! strcmp (note->namedata, "LINUX"))
5878 return elfcore_grok_prxfpreg (abfd, note);
5884 if (bed->elf_backend_grok_psinfo)
5885 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
5887 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5888 return elfcore_grok_psinfo (abfd, note);
5896 elfcore_read_notes (abfd, offset, size)
5907 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5910 buf = bfd_malloc ((size_t) size);
5914 if (bfd_read (buf, size, 1, abfd) != size)
5922 while (p < buf + size)
5924 /* FIXME: bad alignment assumption. */
5925 Elf_External_Note *xnp = (Elf_External_Note *) p;
5926 Elf_Internal_Note in;
5928 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5930 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5931 in.namedata = xnp->name;
5933 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5934 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5935 in.descpos = offset + (in.descdata - buf);
5937 if (! elfcore_grok_note (abfd, &in))
5940 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5947 /* Providing external access to the ELF program header table. */
5949 /* Return an upper bound on the number of bytes required to store a
5950 copy of ABFD's program header table entries. Return -1 if an error
5951 occurs; bfd_get_error will return an appropriate code. */
5954 bfd_get_elf_phdr_upper_bound (abfd)
5957 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5959 bfd_set_error (bfd_error_wrong_format);
5963 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
5966 /* Copy ABFD's program header table entries to *PHDRS. The entries
5967 will be stored as an array of Elf_Internal_Phdr structures, as
5968 defined in include/elf/internal.h. To find out how large the
5969 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5971 Return the number of program header table entries read, or -1 if an
5972 error occurs; bfd_get_error will return an appropriate code. */
5975 bfd_get_elf_phdrs (abfd, phdrs)
5981 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5983 bfd_set_error (bfd_error_wrong_format);
5987 num_phdrs = elf_elfheader (abfd)->e_phnum;
5988 memcpy (phdrs, elf_tdata (abfd)->phdr,
5989 num_phdrs * sizeof (Elf_Internal_Phdr));
5995 _bfd_elf_sprintf_vma (abfd, buf, value)
5996 bfd *abfd ATTRIBUTE_UNUSED;
6001 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6003 i_ehdrp = elf_elfheader (abfd);
6004 if (i_ehdrp == NULL)
6005 sprintf_vma (buf, value);
6008 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6010 #if BFD_HOST_64BIT_LONG
6011 sprintf (buf, "%016lx", value);
6013 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6014 _bfd_int64_low (value));
6018 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6021 sprintf_vma (buf, value);
6026 _bfd_elf_fprintf_vma (abfd, stream, value)
6027 bfd *abfd ATTRIBUTE_UNUSED;
6032 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6034 i_ehdrp = elf_elfheader (abfd);
6035 if (i_ehdrp == NULL)
6036 fprintf_vma ((FILE *) stream, value);
6039 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6041 #if BFD_HOST_64BIT_LONG
6042 fprintf ((FILE *) stream, "%016lx", value);
6044 fprintf ((FILE *) stream, "%08lx%08lx",
6045 _bfd_int64_high (value), _bfd_int64_low (value));
6049 fprintf ((FILE *) stream, "%08lx",
6050 (unsigned long) (value & 0xffffffff));
6053 fprintf_vma ((FILE *) stream, value);
6057 enum elf_reloc_type_class
6058 _bfd_elf_reloc_type_class (type)
6061 return reloc_class_normal;