1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
34 /* For sparc64-cross-sparc32. */
43 static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
45 static boolean map_sections_to_segments PARAMS ((bfd *));
46 static int elf_sort_sections PARAMS ((const PTR, const PTR));
47 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
48 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
49 static boolean prep_headers PARAMS ((bfd *));
50 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
51 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
52 static char *elf_read PARAMS ((bfd *, long, unsigned int));
53 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
54 static boolean assign_section_numbers PARAMS ((bfd *));
55 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
56 static boolean elf_map_symbols PARAMS ((bfd *));
57 static bfd_size_type get_program_header_size PARAMS ((bfd *));
58 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
59 static boolean elf_find_function PARAMS ((bfd *, asection *,
61 bfd_vma, const char **,
64 /* Swap version information in and out. The version information is
65 currently size independent. If that ever changes, this code will
66 need to move into elfcode.h. */
68 /* Swap in a Verdef structure. */
71 _bfd_elf_swap_verdef_in (abfd, src, dst)
73 const Elf_External_Verdef *src;
74 Elf_Internal_Verdef *dst;
76 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
77 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
78 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
79 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
80 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
81 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
82 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
85 /* Swap out a Verdef structure. */
88 _bfd_elf_swap_verdef_out (abfd, src, dst)
90 const Elf_Internal_Verdef *src;
91 Elf_External_Verdef *dst;
93 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
94 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
95 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
96 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
97 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
98 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
99 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
102 /* Swap in a Verdaux structure. */
105 _bfd_elf_swap_verdaux_in (abfd, src, dst)
107 const Elf_External_Verdaux *src;
108 Elf_Internal_Verdaux *dst;
110 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
111 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
114 /* Swap out a Verdaux structure. */
117 _bfd_elf_swap_verdaux_out (abfd, src, dst)
119 const Elf_Internal_Verdaux *src;
120 Elf_External_Verdaux *dst;
122 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
123 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
126 /* Swap in a Verneed structure. */
129 _bfd_elf_swap_verneed_in (abfd, src, dst)
131 const Elf_External_Verneed *src;
132 Elf_Internal_Verneed *dst;
134 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
135 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
136 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
137 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
138 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
141 /* Swap out a Verneed structure. */
144 _bfd_elf_swap_verneed_out (abfd, src, dst)
146 const Elf_Internal_Verneed *src;
147 Elf_External_Verneed *dst;
149 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
150 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
151 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
152 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
153 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
156 /* Swap in a Vernaux structure. */
159 _bfd_elf_swap_vernaux_in (abfd, src, dst)
161 const Elf_External_Vernaux *src;
162 Elf_Internal_Vernaux *dst;
164 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
165 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
166 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
167 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
168 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
171 /* Swap out a Vernaux structure. */
174 _bfd_elf_swap_vernaux_out (abfd, src, dst)
176 const Elf_Internal_Vernaux *src;
177 Elf_External_Vernaux *dst;
179 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
180 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
181 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
182 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
183 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
186 /* Swap in a Versym structure. */
189 _bfd_elf_swap_versym_in (abfd, src, dst)
191 const Elf_External_Versym *src;
192 Elf_Internal_Versym *dst;
194 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
197 /* Swap out a Versym structure. */
200 _bfd_elf_swap_versym_out (abfd, src, dst)
202 const Elf_Internal_Versym *src;
203 Elf_External_Versym *dst;
205 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
208 /* Standard ELF hash function. Do not change this function; you will
209 cause invalid hash tables to be generated. */
212 bfd_elf_hash (namearg)
215 const unsigned char *name = (const unsigned char *) namearg;
220 while ((ch = *name++) != '\0')
223 if ((g = (h & 0xf0000000)) != 0)
226 /* The ELF ABI says `h &= ~g', but this is equivalent in
227 this case and on some machines one insn instead of two. */
234 /* Read a specified number of bytes at a specified offset in an ELF
235 file, into a newly allocated buffer, and return a pointer to the
239 elf_read (abfd, offset, size)
246 if ((buf = bfd_alloc (abfd, size)) == NULL)
248 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
250 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
252 if (bfd_get_error () != bfd_error_system_call)
253 bfd_set_error (bfd_error_file_truncated);
260 bfd_elf_mkobject (abfd)
263 /* This just does initialization. */
264 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
265 elf_tdata (abfd) = (struct elf_obj_tdata *)
266 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
267 if (elf_tdata (abfd) == 0)
269 /* Since everything is done at close time, do we need any
276 bfd_elf_mkcorefile (abfd)
279 /* I think this can be done just like an object file. */
280 return bfd_elf_mkobject (abfd);
284 bfd_elf_get_str_section (abfd, shindex)
286 unsigned int shindex;
288 Elf_Internal_Shdr **i_shdrp;
289 char *shstrtab = NULL;
291 unsigned int shstrtabsize;
293 i_shdrp = elf_elfsections (abfd);
294 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
297 shstrtab = (char *) i_shdrp[shindex]->contents;
298 if (shstrtab == NULL)
300 /* No cached one, attempt to read, and cache what we read. */
301 offset = i_shdrp[shindex]->sh_offset;
302 shstrtabsize = i_shdrp[shindex]->sh_size;
303 shstrtab = elf_read (abfd, offset, shstrtabsize);
304 i_shdrp[shindex]->contents = (PTR) shstrtab;
310 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
312 unsigned int shindex;
313 unsigned int strindex;
315 Elf_Internal_Shdr *hdr;
320 hdr = elf_elfsections (abfd)[shindex];
322 if (hdr->contents == NULL
323 && bfd_elf_get_str_section (abfd, shindex) == NULL)
326 if (strindex >= hdr->sh_size)
328 (*_bfd_error_handler)
329 (_("%s: invalid string offset %u >= %lu for section `%s'"),
330 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
331 ((shindex == elf_elfheader(abfd)->e_shstrndx
332 && strindex == hdr->sh_name)
334 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
338 return ((char *) hdr->contents) + strindex;
341 /* Make a BFD section from an ELF section. We store a pointer to the
342 BFD section in the bfd_section field of the header. */
345 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
347 Elf_Internal_Shdr *hdr;
352 struct elf_backend_data *bed;
354 if (hdr->bfd_section != NULL)
356 BFD_ASSERT (strcmp (name,
357 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
361 newsect = bfd_make_section_anyway (abfd, name);
365 newsect->filepos = hdr->sh_offset;
367 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
368 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
369 || ! bfd_set_section_alignment (abfd, newsect,
370 bfd_log2 (hdr->sh_addralign)))
373 flags = SEC_NO_FLAGS;
374 if (hdr->sh_type != SHT_NOBITS)
375 flags |= SEC_HAS_CONTENTS;
376 if ((hdr->sh_flags & SHF_ALLOC) != 0)
379 if (hdr->sh_type != SHT_NOBITS)
382 if ((hdr->sh_flags & SHF_WRITE) == 0)
383 flags |= SEC_READONLY;
384 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
386 else if ((flags & SEC_LOAD) != 0)
389 /* The debugging sections appear to be recognized only by name, not
392 static const char *debug_sec_names [] =
401 for (i = sizeof (debug_sec_names) / sizeof (debug_sec_names[0]); i--;)
402 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
406 flags |= SEC_DEBUGGING;
409 /* As a GNU extension, if the name begins with .gnu.linkonce, we
410 only link a single copy of the section. This is used to support
411 g++. g++ will emit each template expansion in its own section.
412 The symbols will be defined as weak, so that multiple definitions
413 are permitted. The GNU linker extension is to actually discard
414 all but one of the sections. */
415 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
416 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
418 bed = get_elf_backend_data (abfd);
419 if (bed->elf_backend_section_flags)
420 if (! bed->elf_backend_section_flags (&flags, hdr))
423 if (! bfd_set_section_flags (abfd, newsect, flags))
426 if ((flags & SEC_ALLOC) != 0)
428 Elf_Internal_Phdr *phdr;
431 /* Look through the phdrs to see if we need to adjust the lma.
432 If all the p_paddr fields are zero, we ignore them, since
433 some ELF linkers produce such output. */
434 phdr = elf_tdata (abfd)->phdr;
435 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
437 if (phdr->p_paddr != 0)
440 if (i < elf_elfheader (abfd)->e_phnum)
442 phdr = elf_tdata (abfd)->phdr;
443 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
445 if (phdr->p_type == PT_LOAD
446 && phdr->p_vaddr != phdr->p_paddr
447 && phdr->p_vaddr <= hdr->sh_addr
448 && (phdr->p_vaddr + phdr->p_memsz
449 >= hdr->sh_addr + hdr->sh_size)
450 && ((flags & SEC_LOAD) == 0
451 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
452 && (phdr->p_offset + phdr->p_filesz
453 >= hdr->sh_offset + hdr->sh_size))))
455 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
462 hdr->bfd_section = newsect;
463 elf_section_data (newsect)->this_hdr = *hdr;
473 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
476 Helper functions for GDB to locate the string tables.
477 Since BFD hides string tables from callers, GDB needs to use an
478 internal hook to find them. Sun's .stabstr, in particular,
479 isn't even pointed to by the .stab section, so ordinary
480 mechanisms wouldn't work to find it, even if we had some.
483 struct elf_internal_shdr *
484 bfd_elf_find_section (abfd, name)
488 Elf_Internal_Shdr **i_shdrp;
493 i_shdrp = elf_elfsections (abfd);
496 shstrtab = bfd_elf_get_str_section
497 (abfd, elf_elfheader (abfd)->e_shstrndx);
498 if (shstrtab != NULL)
500 max = elf_elfheader (abfd)->e_shnum;
501 for (i = 1; i < max; i++)
502 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
509 const char *const bfd_elf_section_type_names[] = {
510 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
511 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
512 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
515 /* ELF relocs are against symbols. If we are producing relocateable
516 output, and the reloc is against an external symbol, and nothing
517 has given us any additional addend, the resulting reloc will also
518 be against the same symbol. In such a case, we don't want to
519 change anything about the way the reloc is handled, since it will
520 all be done at final link time. Rather than put special case code
521 into bfd_perform_relocation, all the reloc types use this howto
522 function. It just short circuits the reloc if producing
523 relocateable output against an external symbol. */
525 bfd_reloc_status_type
526 bfd_elf_generic_reloc (abfd,
533 bfd *abfd ATTRIBUTE_UNUSED;
534 arelent *reloc_entry;
536 PTR data ATTRIBUTE_UNUSED;
537 asection *input_section;
539 char **error_message ATTRIBUTE_UNUSED;
541 if (output_bfd != (bfd *) NULL
542 && (symbol->flags & BSF_SECTION_SYM) == 0
543 && (! reloc_entry->howto->partial_inplace
544 || reloc_entry->addend == 0))
546 reloc_entry->address += input_section->output_offset;
550 return bfd_reloc_continue;
553 /* Print out the program headers. */
556 _bfd_elf_print_private_bfd_data (abfd, farg)
560 FILE *f = (FILE *) farg;
561 Elf_Internal_Phdr *p;
563 bfd_byte *dynbuf = NULL;
565 p = elf_tdata (abfd)->phdr;
570 fprintf (f, _("\nProgram Header:\n"));
571 c = elf_elfheader (abfd)->e_phnum;
572 for (i = 0; i < c; i++, p++)
579 case PT_NULL: s = "NULL"; break;
580 case PT_LOAD: s = "LOAD"; break;
581 case PT_DYNAMIC: s = "DYNAMIC"; break;
582 case PT_INTERP: s = "INTERP"; break;
583 case PT_NOTE: s = "NOTE"; break;
584 case PT_SHLIB: s = "SHLIB"; break;
585 case PT_PHDR: s = "PHDR"; break;
586 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
588 fprintf (f, "%8s off 0x", s);
589 fprintf_vma (f, p->p_offset);
590 fprintf (f, " vaddr 0x");
591 fprintf_vma (f, p->p_vaddr);
592 fprintf (f, " paddr 0x");
593 fprintf_vma (f, p->p_paddr);
594 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
595 fprintf (f, " filesz 0x");
596 fprintf_vma (f, p->p_filesz);
597 fprintf (f, " memsz 0x");
598 fprintf_vma (f, p->p_memsz);
599 fprintf (f, " flags %c%c%c",
600 (p->p_flags & PF_R) != 0 ? 'r' : '-',
601 (p->p_flags & PF_W) != 0 ? 'w' : '-',
602 (p->p_flags & PF_X) != 0 ? 'x' : '-');
603 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
604 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
609 s = bfd_get_section_by_name (abfd, ".dynamic");
614 bfd_byte *extdyn, *extdynend;
616 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
618 fprintf (f, _("\nDynamic Section:\n"));
620 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
623 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
627 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
630 link = elf_elfsections (abfd)[elfsec]->sh_link;
632 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
633 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
636 extdynend = extdyn + s->_raw_size;
637 for (; extdyn < extdynend; extdyn += extdynsize)
639 Elf_Internal_Dyn dyn;
644 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
646 if (dyn.d_tag == DT_NULL)
653 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
657 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
658 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
659 case DT_PLTGOT: name = "PLTGOT"; break;
660 case DT_HASH: name = "HASH"; break;
661 case DT_STRTAB: name = "STRTAB"; break;
662 case DT_SYMTAB: name = "SYMTAB"; break;
663 case DT_RELA: name = "RELA"; break;
664 case DT_RELASZ: name = "RELASZ"; break;
665 case DT_RELAENT: name = "RELAENT"; break;
666 case DT_STRSZ: name = "STRSZ"; break;
667 case DT_SYMENT: name = "SYMENT"; break;
668 case DT_INIT: name = "INIT"; break;
669 case DT_FINI: name = "FINI"; break;
670 case DT_SONAME: name = "SONAME"; stringp = true; break;
671 case DT_RPATH: name = "RPATH"; stringp = true; break;
672 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
673 case DT_REL: name = "REL"; break;
674 case DT_RELSZ: name = "RELSZ"; break;
675 case DT_RELENT: name = "RELENT"; break;
676 case DT_PLTREL: name = "PLTREL"; break;
677 case DT_DEBUG: name = "DEBUG"; break;
678 case DT_TEXTREL: name = "TEXTREL"; break;
679 case DT_JMPREL: name = "JMPREL"; break;
680 case DT_BIND_NOW: name = "BIND_NOW"; break;
681 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
682 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
683 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
684 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
685 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
686 case DT_FLAGS: name = "FLAGS"; break;
687 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
688 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
689 case DT_CHECKSUM: name = "CHECKSUM"; break;
690 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
691 case DT_MOVEENT: name = "MOVEENT"; break;
692 case DT_MOVESZ: name = "MOVESZ"; break;
693 case DT_FEATURE: name = "FEATURE"; break;
694 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
695 case DT_SYMINSZ: name = "SYMINSZ"; break;
696 case DT_SYMINENT: name = "SYMINENT"; break;
697 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
698 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
699 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
700 case DT_PLTPAD: name = "PLTPAD"; break;
701 case DT_MOVETAB: name = "MOVETAB"; break;
702 case DT_SYMINFO: name = "SYMINFO"; break;
703 case DT_RELACOUNT: name = "RELACOUNT"; break;
704 case DT_RELCOUNT: name = "RELCOUNT"; break;
705 case DT_FLAGS_1: name = "FLAGS_1"; break;
706 case DT_VERSYM: name = "VERSYM"; break;
707 case DT_VERDEF: name = "VERDEF"; break;
708 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
709 case DT_VERNEED: name = "VERNEED"; break;
710 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
711 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
712 case DT_USED: name = "USED"; break;
713 case DT_FILTER: name = "FILTER"; stringp = true; break;
716 fprintf (f, " %-11s ", name);
718 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
723 string = bfd_elf_string_from_elf_section (abfd, link,
727 fprintf (f, "%s", string);
736 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
737 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
739 if (! _bfd_elf_slurp_version_tables (abfd))
743 if (elf_dynverdef (abfd) != 0)
745 Elf_Internal_Verdef *t;
747 fprintf (f, _("\nVersion definitions:\n"));
748 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
750 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
751 t->vd_flags, t->vd_hash, t->vd_nodename);
752 if (t->vd_auxptr->vda_nextptr != NULL)
754 Elf_Internal_Verdaux *a;
757 for (a = t->vd_auxptr->vda_nextptr;
760 fprintf (f, "%s ", a->vda_nodename);
766 if (elf_dynverref (abfd) != 0)
768 Elf_Internal_Verneed *t;
770 fprintf (f, _("\nVersion References:\n"));
771 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
773 Elf_Internal_Vernaux *a;
775 fprintf (f, _(" required from %s:\n"), t->vn_filename);
776 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
777 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
778 a->vna_flags, a->vna_other, a->vna_nodename);
790 /* Display ELF-specific fields of a symbol. */
793 bfd_elf_print_symbol (abfd, filep, symbol, how)
797 bfd_print_symbol_type how;
799 FILE *file = (FILE *) filep;
802 case bfd_print_symbol_name:
803 fprintf (file, "%s", symbol->name);
805 case bfd_print_symbol_more:
806 fprintf (file, "elf ");
807 fprintf_vma (file, symbol->value);
808 fprintf (file, " %lx", (long) symbol->flags);
810 case bfd_print_symbol_all:
812 CONST char *section_name;
813 CONST char *name = NULL;
814 struct elf_backend_data *bed;
815 unsigned char st_other;
817 section_name = symbol->section ? symbol->section->name : "(*none*)";
819 bed = get_elf_backend_data (abfd);
820 if (bed->elf_backend_print_symbol_all)
821 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
826 bfd_print_symbol_vandf ((PTR) file, symbol);
829 fprintf (file, " %s\t", section_name);
830 /* Print the "other" value for a symbol. For common symbols,
831 we've already printed the size; now print the alignment.
832 For other symbols, we have no specified alignment, and
833 we've printed the address; now print the size. */
835 (bfd_is_com_section (symbol->section)
836 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
837 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
839 /* If we have version information, print it. */
840 if (elf_tdata (abfd)->dynversym_section != 0
841 && (elf_tdata (abfd)->dynverdef_section != 0
842 || elf_tdata (abfd)->dynverref_section != 0))
845 const char *version_string;
847 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
851 else if (vernum == 1)
852 version_string = "Base";
853 else if (vernum <= elf_tdata (abfd)->cverdefs)
855 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
858 Elf_Internal_Verneed *t;
861 for (t = elf_tdata (abfd)->verref;
865 Elf_Internal_Vernaux *a;
867 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
869 if (a->vna_other == vernum)
871 version_string = a->vna_nodename;
878 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
879 fprintf (file, " %-11s", version_string);
884 fprintf (file, " (%s)", version_string);
885 for (i = 10 - strlen (version_string); i > 0; --i)
890 /* If the st_other field is not zero, print it. */
891 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
896 case STV_INTERNAL: fprintf (file, " .internal"); break;
897 case STV_HIDDEN: fprintf (file, " .hidden"); break;
898 case STV_PROTECTED: fprintf (file, " .protected"); break;
900 /* Some other non-defined flags are also present, so print
902 fprintf (file, " 0x%02x", (unsigned int) st_other);
905 fprintf (file, " %s", name);
911 /* Create an entry in an ELF linker hash table. */
913 struct bfd_hash_entry *
914 _bfd_elf_link_hash_newfunc (entry, table, string)
915 struct bfd_hash_entry *entry;
916 struct bfd_hash_table *table;
919 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
921 /* Allocate the structure if it has not already been allocated by a
923 if (ret == (struct elf_link_hash_entry *) NULL)
924 ret = ((struct elf_link_hash_entry *)
925 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
926 if (ret == (struct elf_link_hash_entry *) NULL)
927 return (struct bfd_hash_entry *) ret;
929 /* Call the allocation method of the superclass. */
930 ret = ((struct elf_link_hash_entry *)
931 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
933 if (ret != (struct elf_link_hash_entry *) NULL)
935 /* Set local fields. */
939 ret->dynstr_index = 0;
941 ret->got.offset = (bfd_vma) -1;
942 ret->plt.offset = (bfd_vma) -1;
943 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
944 ret->verinfo.verdef = NULL;
945 ret->vtable_entries_used = NULL;
946 ret->vtable_entries_size = 0;
947 ret->vtable_parent = NULL;
948 ret->type = STT_NOTYPE;
950 /* Assume that we have been called by a non-ELF symbol reader.
951 This flag is then reset by the code which reads an ELF input
952 file. This ensures that a symbol created by a non-ELF symbol
953 reader will have the flag set correctly. */
954 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
957 return (struct bfd_hash_entry *) ret;
960 /* Copy data from an indirect symbol to its direct symbol, hiding the
961 old indirect symbol. */
964 _bfd_elf_link_hash_copy_indirect (dir, ind)
965 struct elf_link_hash_entry *dir, *ind;
967 /* Copy down any references that we may have already seen to the
968 symbol which just became indirect. */
970 dir->elf_link_hash_flags |=
971 (ind->elf_link_hash_flags
972 & (ELF_LINK_HASH_REF_DYNAMIC
973 | ELF_LINK_HASH_REF_REGULAR
974 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
975 | ELF_LINK_NON_GOT_REF));
977 /* Copy over the global and procedure linkage table offset entries.
978 These may have been already set up by a check_relocs routine. */
979 if (dir->got.offset == (bfd_vma) -1)
981 dir->got.offset = ind->got.offset;
982 ind->got.offset = (bfd_vma) -1;
984 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
986 if (dir->plt.offset == (bfd_vma) -1)
988 dir->plt.offset = ind->plt.offset;
989 ind->plt.offset = (bfd_vma) -1;
991 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
993 if (dir->dynindx == -1)
995 dir->dynindx = ind->dynindx;
996 dir->dynstr_index = ind->dynstr_index;
998 ind->dynstr_index = 0;
1000 BFD_ASSERT (ind->dynindx == -1);
1004 _bfd_elf_link_hash_hide_symbol (info, h)
1005 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1006 struct elf_link_hash_entry *h;
1008 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1010 h->plt.offset = (bfd_vma) -1;
1013 /* Initialize an ELF linker hash table. */
1016 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1017 struct elf_link_hash_table *table;
1019 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1020 struct bfd_hash_table *,
1023 table->dynamic_sections_created = false;
1024 table->dynobj = NULL;
1025 /* The first dynamic symbol is a dummy. */
1026 table->dynsymcount = 1;
1027 table->dynstr = NULL;
1028 table->bucketcount = 0;
1029 table->needed = NULL;
1030 table->runpath = NULL;
1032 table->stab_info = NULL;
1033 table->dynlocal = NULL;
1034 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1037 /* Create an ELF linker hash table. */
1039 struct bfd_link_hash_table *
1040 _bfd_elf_link_hash_table_create (abfd)
1043 struct elf_link_hash_table *ret;
1045 ret = ((struct elf_link_hash_table *)
1046 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1047 if (ret == (struct elf_link_hash_table *) NULL)
1050 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1052 bfd_release (abfd, ret);
1059 /* This is a hook for the ELF emulation code in the generic linker to
1060 tell the backend linker what file name to use for the DT_NEEDED
1061 entry for a dynamic object. The generic linker passes name as an
1062 empty string to indicate that no DT_NEEDED entry should be made. */
1065 bfd_elf_set_dt_needed_name (abfd, name)
1069 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1070 && bfd_get_format (abfd) == bfd_object)
1071 elf_dt_name (abfd) = name;
1075 bfd_elf_set_dt_needed_soname (abfd, name)
1079 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1080 && bfd_get_format (abfd) == bfd_object)
1081 elf_dt_soname (abfd) = name;
1084 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1085 the linker ELF emulation code. */
1087 struct bfd_link_needed_list *
1088 bfd_elf_get_needed_list (abfd, info)
1089 bfd *abfd ATTRIBUTE_UNUSED;
1090 struct bfd_link_info *info;
1092 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1094 return elf_hash_table (info)->needed;
1097 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1098 hook for the linker ELF emulation code. */
1100 struct bfd_link_needed_list *
1101 bfd_elf_get_runpath_list (abfd, info)
1102 bfd *abfd ATTRIBUTE_UNUSED;
1103 struct bfd_link_info *info;
1105 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1107 return elf_hash_table (info)->runpath;
1110 /* Get the name actually used for a dynamic object for a link. This
1111 is the SONAME entry if there is one. Otherwise, it is the string
1112 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1115 bfd_elf_get_dt_soname (abfd)
1118 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1119 && bfd_get_format (abfd) == bfd_object)
1120 return elf_dt_name (abfd);
1124 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1125 the ELF linker emulation code. */
1128 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1130 struct bfd_link_needed_list **pneeded;
1133 bfd_byte *dynbuf = NULL;
1136 bfd_byte *extdyn, *extdynend;
1138 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1142 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1143 || bfd_get_format (abfd) != bfd_object)
1146 s = bfd_get_section_by_name (abfd, ".dynamic");
1147 if (s == NULL || s->_raw_size == 0)
1150 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1154 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1158 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1162 link = elf_elfsections (abfd)[elfsec]->sh_link;
1164 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1165 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1168 extdynend = extdyn + s->_raw_size;
1169 for (; extdyn < extdynend; extdyn += extdynsize)
1171 Elf_Internal_Dyn dyn;
1173 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1175 if (dyn.d_tag == DT_NULL)
1178 if (dyn.d_tag == DT_NEEDED)
1181 struct bfd_link_needed_list *l;
1183 string = bfd_elf_string_from_elf_section (abfd, link,
1188 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1209 /* Allocate an ELF string table--force the first byte to be zero. */
1211 struct bfd_strtab_hash *
1212 _bfd_elf_stringtab_init ()
1214 struct bfd_strtab_hash *ret;
1216 ret = _bfd_stringtab_init ();
1221 loc = _bfd_stringtab_add (ret, "", true, false);
1222 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1223 if (loc == (bfd_size_type) -1)
1225 _bfd_stringtab_free (ret);
1232 /* ELF .o/exec file reading */
1234 /* Create a new bfd section from an ELF section header. */
1237 bfd_section_from_shdr (abfd, shindex)
1239 unsigned int shindex;
1241 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1242 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1243 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1246 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1248 switch (hdr->sh_type)
1251 /* Inactive section. Throw it away. */
1254 case SHT_PROGBITS: /* Normal section with contents. */
1255 case SHT_DYNAMIC: /* Dynamic linking information. */
1256 case SHT_NOBITS: /* .bss section. */
1257 case SHT_HASH: /* .hash section. */
1258 case SHT_NOTE: /* .note section. */
1259 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1261 case SHT_SYMTAB: /* A symbol table */
1262 if (elf_onesymtab (abfd) == shindex)
1265 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1266 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1267 elf_onesymtab (abfd) = shindex;
1268 elf_tdata (abfd)->symtab_hdr = *hdr;
1269 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1270 abfd->flags |= HAS_SYMS;
1272 /* Sometimes a shared object will map in the symbol table. If
1273 SHF_ALLOC is set, and this is a shared object, then we also
1274 treat this section as a BFD section. We can not base the
1275 decision purely on SHF_ALLOC, because that flag is sometimes
1276 set in a relocateable object file, which would confuse the
1278 if ((hdr->sh_flags & SHF_ALLOC) != 0
1279 && (abfd->flags & DYNAMIC) != 0
1280 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1285 case SHT_DYNSYM: /* A dynamic symbol table */
1286 if (elf_dynsymtab (abfd) == shindex)
1289 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1290 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1291 elf_dynsymtab (abfd) = shindex;
1292 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1293 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1294 abfd->flags |= HAS_SYMS;
1296 /* Besides being a symbol table, we also treat this as a regular
1297 section, so that objcopy can handle it. */
1298 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1300 case SHT_STRTAB: /* A string table */
1301 if (hdr->bfd_section != NULL)
1303 if (ehdr->e_shstrndx == shindex)
1305 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1306 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1312 for (i = 1; i < ehdr->e_shnum; i++)
1314 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1315 if (hdr2->sh_link == shindex)
1317 if (! bfd_section_from_shdr (abfd, i))
1319 if (elf_onesymtab (abfd) == i)
1321 elf_tdata (abfd)->strtab_hdr = *hdr;
1322 elf_elfsections (abfd)[shindex] =
1323 &elf_tdata (abfd)->strtab_hdr;
1326 if (elf_dynsymtab (abfd) == i)
1328 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1329 elf_elfsections (abfd)[shindex] = hdr =
1330 &elf_tdata (abfd)->dynstrtab_hdr;
1331 /* We also treat this as a regular section, so
1332 that objcopy can handle it. */
1335 #if 0 /* Not handling other string tables specially right now. */
1336 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1337 /* We have a strtab for some random other section. */
1338 newsect = (asection *) hdr2->bfd_section;
1341 hdr->bfd_section = newsect;
1342 hdr2 = &elf_section_data (newsect)->str_hdr;
1344 elf_elfsections (abfd)[shindex] = hdr2;
1350 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1354 /* *These* do a lot of work -- but build no sections! */
1356 asection *target_sect;
1357 Elf_Internal_Shdr *hdr2;
1359 /* Check for a bogus link to avoid crashing. */
1360 if (hdr->sh_link >= ehdr->e_shnum)
1362 ((*_bfd_error_handler)
1363 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1364 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1365 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1368 /* For some incomprehensible reason Oracle distributes
1369 libraries for Solaris in which some of the objects have
1370 bogus sh_link fields. It would be nice if we could just
1371 reject them, but, unfortunately, some people need to use
1372 them. We scan through the section headers; if we find only
1373 one suitable symbol table, we clobber the sh_link to point
1374 to it. I hope this doesn't break anything. */
1375 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1376 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1382 for (scan = 1; scan < ehdr->e_shnum; scan++)
1384 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1385 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1396 hdr->sh_link = found;
1399 /* Get the symbol table. */
1400 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1401 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1404 /* If this reloc section does not use the main symbol table we
1405 don't treat it as a reloc section. BFD can't adequately
1406 represent such a section, so at least for now, we don't
1407 try. We just present it as a normal section. We also
1408 can't use it as a reloc section if it points to the null
1410 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1411 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1413 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1415 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1416 if (target_sect == NULL)
1419 if ((target_sect->flags & SEC_RELOC) == 0
1420 || target_sect->reloc_count == 0)
1421 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1424 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1425 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1426 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1429 elf_elfsections (abfd)[shindex] = hdr2;
1430 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1431 target_sect->flags |= SEC_RELOC;
1432 target_sect->relocation = NULL;
1433 target_sect->rel_filepos = hdr->sh_offset;
1434 /* In the section to which the relocations apply, mark whether
1435 its relocations are of the REL or RELA variety. */
1436 if (hdr->sh_size != 0)
1437 elf_section_data (target_sect)->use_rela_p
1438 = (hdr->sh_type == SHT_RELA);
1439 abfd->flags |= HAS_RELOC;
1444 case SHT_GNU_verdef:
1445 elf_dynverdef (abfd) = shindex;
1446 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1447 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1450 case SHT_GNU_versym:
1451 elf_dynversym (abfd) = shindex;
1452 elf_tdata (abfd)->dynversym_hdr = *hdr;
1453 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1456 case SHT_GNU_verneed:
1457 elf_dynverref (abfd) = shindex;
1458 elf_tdata (abfd)->dynverref_hdr = *hdr;
1459 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1466 /* Check for any processor-specific section types. */
1468 if (bed->elf_backend_section_from_shdr)
1469 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1477 /* Given an ELF section number, retrieve the corresponding BFD
1481 bfd_section_from_elf_index (abfd, index)
1485 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1486 if (index >= elf_elfheader (abfd)->e_shnum)
1488 return elf_elfsections (abfd)[index]->bfd_section;
1492 _bfd_elf_new_section_hook (abfd, sec)
1496 struct bfd_elf_section_data *sdata;
1498 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1501 sec->used_by_bfd = (PTR) sdata;
1503 /* Indicate whether or not this section should use RELA relocations. */
1505 = get_elf_backend_data (abfd)->default_use_rela_p;
1510 /* Create a new bfd section from an ELF program header.
1512 Since program segments have no names, we generate a synthetic name
1513 of the form segment<NUM>, where NUM is generally the index in the
1514 program header table. For segments that are split (see below) we
1515 generate the names segment<NUM>a and segment<NUM>b.
1517 Note that some program segments may have a file size that is different than
1518 (less than) the memory size. All this means is that at execution the
1519 system must allocate the amount of memory specified by the memory size,
1520 but only initialize it with the first "file size" bytes read from the
1521 file. This would occur for example, with program segments consisting
1522 of combined data+bss.
1524 To handle the above situation, this routine generates TWO bfd sections
1525 for the single program segment. The first has the length specified by
1526 the file size of the segment, and the second has the length specified
1527 by the difference between the two sizes. In effect, the segment is split
1528 into it's initialized and uninitialized parts.
1533 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1535 Elf_Internal_Phdr *hdr;
1537 const char *typename;
1544 split = ((hdr->p_memsz > 0)
1545 && (hdr->p_filesz > 0)
1546 && (hdr->p_memsz > hdr->p_filesz));
1547 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1548 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1551 strcpy (name, namebuf);
1552 newsect = bfd_make_section (abfd, name);
1553 if (newsect == NULL)
1555 newsect->vma = hdr->p_vaddr;
1556 newsect->lma = hdr->p_paddr;
1557 newsect->_raw_size = hdr->p_filesz;
1558 newsect->filepos = hdr->p_offset;
1559 newsect->flags |= SEC_HAS_CONTENTS;
1560 if (hdr->p_type == PT_LOAD)
1562 newsect->flags |= SEC_ALLOC;
1563 newsect->flags |= SEC_LOAD;
1564 if (hdr->p_flags & PF_X)
1566 /* FIXME: all we known is that it has execute PERMISSION,
1568 newsect->flags |= SEC_CODE;
1571 if (!(hdr->p_flags & PF_W))
1573 newsect->flags |= SEC_READONLY;
1578 sprintf (namebuf, "%s%db", typename, index);
1579 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1582 strcpy (name, namebuf);
1583 newsect = bfd_make_section (abfd, name);
1584 if (newsect == NULL)
1586 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1587 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1588 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1589 if (hdr->p_type == PT_LOAD)
1591 newsect->flags |= SEC_ALLOC;
1592 if (hdr->p_flags & PF_X)
1593 newsect->flags |= SEC_CODE;
1595 if (!(hdr->p_flags & PF_W))
1596 newsect->flags |= SEC_READONLY;
1603 bfd_section_from_phdr (abfd, hdr, index)
1605 Elf_Internal_Phdr *hdr;
1608 struct elf_backend_data *bed;
1610 switch (hdr->p_type)
1613 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1616 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1619 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1622 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1625 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1627 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1632 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1635 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1638 /* Check for any processor-specific program segment types.
1639 If no handler for them, default to making "segment" sections. */
1640 bed = get_elf_backend_data (abfd);
1641 if (bed->elf_backend_section_from_phdr)
1642 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1644 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1648 /* Initialize REL_HDR, the section-header for new section, containing
1649 relocations against ASECT. If USE_RELA_P is true, we use RELA
1650 relocations; otherwise, we use REL relocations. */
1653 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1655 Elf_Internal_Shdr *rel_hdr;
1660 struct elf_backend_data *bed;
1662 bed = get_elf_backend_data (abfd);
1663 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1666 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1668 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1670 if (rel_hdr->sh_name == (unsigned int) -1)
1672 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1673 rel_hdr->sh_entsize = (use_rela_p
1674 ? bed->s->sizeof_rela
1675 : bed->s->sizeof_rel);
1676 rel_hdr->sh_addralign = bed->s->file_align;
1677 rel_hdr->sh_flags = 0;
1678 rel_hdr->sh_addr = 0;
1679 rel_hdr->sh_size = 0;
1680 rel_hdr->sh_offset = 0;
1685 /* Set up an ELF internal section header for a section. */
1688 elf_fake_sections (abfd, asect, failedptrarg)
1693 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1694 boolean *failedptr = (boolean *) failedptrarg;
1695 Elf_Internal_Shdr *this_hdr;
1699 /* We already failed; just get out of the bfd_map_over_sections
1704 this_hdr = &elf_section_data (asect)->this_hdr;
1706 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1709 if (this_hdr->sh_name == (unsigned long) -1)
1715 this_hdr->sh_flags = 0;
1717 if ((asect->flags & SEC_ALLOC) != 0
1718 || asect->user_set_vma)
1719 this_hdr->sh_addr = asect->vma;
1721 this_hdr->sh_addr = 0;
1723 this_hdr->sh_offset = 0;
1724 this_hdr->sh_size = asect->_raw_size;
1725 this_hdr->sh_link = 0;
1726 this_hdr->sh_addralign = 1 << asect->alignment_power;
1727 /* The sh_entsize and sh_info fields may have been set already by
1728 copy_private_section_data. */
1730 this_hdr->bfd_section = asect;
1731 this_hdr->contents = NULL;
1733 /* FIXME: This should not be based on section names. */
1734 if (strcmp (asect->name, ".dynstr") == 0)
1735 this_hdr->sh_type = SHT_STRTAB;
1736 else if (strcmp (asect->name, ".hash") == 0)
1738 this_hdr->sh_type = SHT_HASH;
1739 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1741 else if (strcmp (asect->name, ".dynsym") == 0)
1743 this_hdr->sh_type = SHT_DYNSYM;
1744 this_hdr->sh_entsize = bed->s->sizeof_sym;
1746 else if (strcmp (asect->name, ".dynamic") == 0)
1748 this_hdr->sh_type = SHT_DYNAMIC;
1749 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1751 else if (strncmp (asect->name, ".rela", 5) == 0
1752 && get_elf_backend_data (abfd)->may_use_rela_p)
1754 this_hdr->sh_type = SHT_RELA;
1755 this_hdr->sh_entsize = bed->s->sizeof_rela;
1757 else if (strncmp (asect->name, ".rel", 4) == 0
1758 && get_elf_backend_data (abfd)->may_use_rel_p)
1760 this_hdr->sh_type = SHT_REL;
1761 this_hdr->sh_entsize = bed->s->sizeof_rel;
1763 else if (strncmp (asect->name, ".note", 5) == 0)
1764 this_hdr->sh_type = SHT_NOTE;
1765 else if (strncmp (asect->name, ".stab", 5) == 0
1766 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1767 this_hdr->sh_type = SHT_STRTAB;
1768 else if (strcmp (asect->name, ".gnu.version") == 0)
1770 this_hdr->sh_type = SHT_GNU_versym;
1771 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1773 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1775 this_hdr->sh_type = SHT_GNU_verdef;
1776 this_hdr->sh_entsize = 0;
1777 /* objcopy or strip will copy over sh_info, but may not set
1778 cverdefs. The linker will set cverdefs, but sh_info will be
1780 if (this_hdr->sh_info == 0)
1781 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1783 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1784 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1786 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1788 this_hdr->sh_type = SHT_GNU_verneed;
1789 this_hdr->sh_entsize = 0;
1790 /* objcopy or strip will copy over sh_info, but may not set
1791 cverrefs. The linker will set cverrefs, but sh_info will be
1793 if (this_hdr->sh_info == 0)
1794 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1796 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1797 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1799 else if ((asect->flags & SEC_ALLOC) != 0
1800 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
1801 this_hdr->sh_type = SHT_NOBITS;
1803 this_hdr->sh_type = SHT_PROGBITS;
1805 if ((asect->flags & SEC_ALLOC) != 0)
1806 this_hdr->sh_flags |= SHF_ALLOC;
1807 if ((asect->flags & SEC_READONLY) == 0)
1808 this_hdr->sh_flags |= SHF_WRITE;
1809 if ((asect->flags & SEC_CODE) != 0)
1810 this_hdr->sh_flags |= SHF_EXECINSTR;
1812 /* Check for processor-specific section types. */
1813 if (bed->elf_backend_fake_sections)
1814 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1816 /* If the section has relocs, set up a section header for the
1817 SHT_REL[A] section. If two relocation sections are required for
1818 this section, it is up to the processor-specific back-end to
1819 create the other. */
1820 if ((asect->flags & SEC_RELOC) != 0
1821 && !_bfd_elf_init_reloc_shdr (abfd,
1822 &elf_section_data (asect)->rel_hdr,
1824 elf_section_data (asect)->use_rela_p))
1828 /* Assign all ELF section numbers. The dummy first section is handled here
1829 too. The link/info pointers for the standard section types are filled
1830 in here too, while we're at it. */
1833 assign_section_numbers (abfd)
1836 struct elf_obj_tdata *t = elf_tdata (abfd);
1838 unsigned int section_number;
1839 Elf_Internal_Shdr **i_shdrp;
1843 for (sec = abfd->sections; sec; sec = sec->next)
1845 struct bfd_elf_section_data *d = elf_section_data (sec);
1847 d->this_idx = section_number++;
1848 if ((sec->flags & SEC_RELOC) == 0)
1851 d->rel_idx = section_number++;
1854 d->rel_idx2 = section_number++;
1859 t->shstrtab_section = section_number++;
1860 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1861 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1863 if (bfd_get_symcount (abfd) > 0)
1865 t->symtab_section = section_number++;
1866 t->strtab_section = section_number++;
1869 elf_elfheader (abfd)->e_shnum = section_number;
1871 /* Set up the list of section header pointers, in agreement with the
1873 i_shdrp = ((Elf_Internal_Shdr **)
1874 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1875 if (i_shdrp == NULL)
1878 i_shdrp[0] = ((Elf_Internal_Shdr *)
1879 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1880 if (i_shdrp[0] == NULL)
1882 bfd_release (abfd, i_shdrp);
1885 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1887 elf_elfsections (abfd) = i_shdrp;
1889 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1890 if (bfd_get_symcount (abfd) > 0)
1892 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1893 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1894 t->symtab_hdr.sh_link = t->strtab_section;
1896 for (sec = abfd->sections; sec; sec = sec->next)
1898 struct bfd_elf_section_data *d = elf_section_data (sec);
1902 i_shdrp[d->this_idx] = &d->this_hdr;
1903 if (d->rel_idx != 0)
1904 i_shdrp[d->rel_idx] = &d->rel_hdr;
1905 if (d->rel_idx2 != 0)
1906 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1908 /* Fill in the sh_link and sh_info fields while we're at it. */
1910 /* sh_link of a reloc section is the section index of the symbol
1911 table. sh_info is the section index of the section to which
1912 the relocation entries apply. */
1913 if (d->rel_idx != 0)
1915 d->rel_hdr.sh_link = t->symtab_section;
1916 d->rel_hdr.sh_info = d->this_idx;
1918 if (d->rel_idx2 != 0)
1920 d->rel_hdr2->sh_link = t->symtab_section;
1921 d->rel_hdr2->sh_info = d->this_idx;
1924 switch (d->this_hdr.sh_type)
1928 /* A reloc section which we are treating as a normal BFD
1929 section. sh_link is the section index of the symbol
1930 table. sh_info is the section index of the section to
1931 which the relocation entries apply. We assume that an
1932 allocated reloc section uses the dynamic symbol table.
1933 FIXME: How can we be sure? */
1934 s = bfd_get_section_by_name (abfd, ".dynsym");
1936 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1938 /* We look up the section the relocs apply to by name. */
1940 if (d->this_hdr.sh_type == SHT_REL)
1944 s = bfd_get_section_by_name (abfd, name);
1946 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1950 /* We assume that a section named .stab*str is a stabs
1951 string section. We look for a section with the same name
1952 but without the trailing ``str'', and set its sh_link
1953 field to point to this section. */
1954 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1955 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1960 len = strlen (sec->name);
1961 alc = (char *) bfd_malloc (len - 2);
1964 strncpy (alc, sec->name, len - 3);
1965 alc[len - 3] = '\0';
1966 s = bfd_get_section_by_name (abfd, alc);
1970 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1972 /* This is a .stab section. */
1973 elf_section_data (s)->this_hdr.sh_entsize =
1974 4 + 2 * bfd_get_arch_size (abfd) / 8;
1981 case SHT_GNU_verneed:
1982 case SHT_GNU_verdef:
1983 /* sh_link is the section header index of the string table
1984 used for the dynamic entries, or the symbol table, or the
1986 s = bfd_get_section_by_name (abfd, ".dynstr");
1988 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1992 case SHT_GNU_versym:
1993 /* sh_link is the section header index of the symbol table
1994 this hash table or version table is for. */
1995 s = bfd_get_section_by_name (abfd, ".dynsym");
1997 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2005 /* Map symbol from it's internal number to the external number, moving
2006 all local symbols to be at the head of the list. */
2009 sym_is_global (abfd, sym)
2013 /* If the backend has a special mapping, use it. */
2014 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2015 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2018 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2019 || bfd_is_und_section (bfd_get_section (sym))
2020 || bfd_is_com_section (bfd_get_section (sym)));
2024 elf_map_symbols (abfd)
2027 int symcount = bfd_get_symcount (abfd);
2028 asymbol **syms = bfd_get_outsymbols (abfd);
2029 asymbol **sect_syms;
2031 int num_globals = 0;
2032 int num_locals2 = 0;
2033 int num_globals2 = 0;
2035 int num_sections = 0;
2042 fprintf (stderr, "elf_map_symbols\n");
2046 /* Add a section symbol for each BFD section. FIXME: Is this really
2048 for (asect = abfd->sections; asect; asect = asect->next)
2050 if (max_index < asect->index)
2051 max_index = asect->index;
2055 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2056 if (sect_syms == NULL)
2058 elf_section_syms (abfd) = sect_syms;
2060 for (idx = 0; idx < symcount; idx++)
2064 if ((sym->flags & BSF_SECTION_SYM) != 0
2071 if (sec->owner != NULL)
2073 if (sec->owner != abfd)
2075 if (sec->output_offset != 0)
2078 sec = sec->output_section;
2080 /* Empty sections in the input files may have had a section
2081 symbol created for them. (See the comment near the end of
2082 _bfd_generic_link_output_symbols in linker.c). If the linker
2083 script discards such sections then we will reach this point.
2084 Since we know that we cannot avoid this case, we detect it
2085 and skip the abort and the assignment to the sect_syms array.
2086 To reproduce this particular case try running the linker
2087 testsuite test ld-scripts/weak.exp for an ELF port that uses
2088 the generic linker. */
2089 if (sec->owner == NULL)
2092 BFD_ASSERT (sec->owner == abfd);
2094 sect_syms[sec->index] = syms[idx];
2099 for (asect = abfd->sections; asect; asect = asect->next)
2101 if (sect_syms[asect->index] != NULL)
2104 sym = bfd_make_empty_symbol (abfd);
2107 sym->the_bfd = abfd;
2108 sym->name = asect->name;
2110 /* Set the flags to 0 to indicate that this one was newly added. */
2112 sym->section = asect;
2113 sect_syms[asect->index] = sym;
2117 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2118 asect->name, (long) asect->vma, asect->index, (long) asect);
2122 /* Classify all of the symbols. */
2123 for (idx = 0; idx < symcount; idx++)
2125 if (!sym_is_global (abfd, syms[idx]))
2130 for (asect = abfd->sections; asect; asect = asect->next)
2132 if (sect_syms[asect->index] != NULL
2133 && sect_syms[asect->index]->flags == 0)
2135 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2136 if (!sym_is_global (abfd, sect_syms[asect->index]))
2140 sect_syms[asect->index]->flags = 0;
2144 /* Now sort the symbols so the local symbols are first. */
2145 new_syms = ((asymbol **)
2147 (num_locals + num_globals) * sizeof (asymbol *)));
2148 if (new_syms == NULL)
2151 for (idx = 0; idx < symcount; idx++)
2153 asymbol *sym = syms[idx];
2156 if (!sym_is_global (abfd, sym))
2159 i = num_locals + num_globals2++;
2161 sym->udata.i = i + 1;
2163 for (asect = abfd->sections; asect; asect = asect->next)
2165 if (sect_syms[asect->index] != NULL
2166 && sect_syms[asect->index]->flags == 0)
2168 asymbol *sym = sect_syms[asect->index];
2171 sym->flags = BSF_SECTION_SYM;
2172 if (!sym_is_global (abfd, sym))
2175 i = num_locals + num_globals2++;
2177 sym->udata.i = i + 1;
2181 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2183 elf_num_locals (abfd) = num_locals;
2184 elf_num_globals (abfd) = num_globals;
2188 /* Align to the maximum file alignment that could be required for any
2189 ELF data structure. */
2191 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2192 static INLINE file_ptr
2193 align_file_position (off, align)
2197 return (off + align - 1) & ~(align - 1);
2200 /* Assign a file position to a section, optionally aligning to the
2201 required section alignment. */
2204 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2205 Elf_Internal_Shdr *i_shdrp;
2213 al = i_shdrp->sh_addralign;
2215 offset = BFD_ALIGN (offset, al);
2217 i_shdrp->sh_offset = offset;
2218 if (i_shdrp->bfd_section != NULL)
2219 i_shdrp->bfd_section->filepos = offset;
2220 if (i_shdrp->sh_type != SHT_NOBITS)
2221 offset += i_shdrp->sh_size;
2225 /* Compute the file positions we are going to put the sections at, and
2226 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2227 is not NULL, this is being called by the ELF backend linker. */
2230 _bfd_elf_compute_section_file_positions (abfd, link_info)
2232 struct bfd_link_info *link_info;
2234 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2236 struct bfd_strtab_hash *strtab;
2237 Elf_Internal_Shdr *shstrtab_hdr;
2239 if (abfd->output_has_begun)
2242 /* Do any elf backend specific processing first. */
2243 if (bed->elf_backend_begin_write_processing)
2244 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2246 if (! prep_headers (abfd))
2249 /* Post process the headers if necessary. */
2250 if (bed->elf_backend_post_process_headers)
2251 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2254 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2258 if (!assign_section_numbers (abfd))
2261 /* The backend linker builds symbol table information itself. */
2262 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2264 /* Non-zero if doing a relocatable link. */
2265 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2267 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2271 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2272 /* sh_name was set in prep_headers. */
2273 shstrtab_hdr->sh_type = SHT_STRTAB;
2274 shstrtab_hdr->sh_flags = 0;
2275 shstrtab_hdr->sh_addr = 0;
2276 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2277 shstrtab_hdr->sh_entsize = 0;
2278 shstrtab_hdr->sh_link = 0;
2279 shstrtab_hdr->sh_info = 0;
2280 /* sh_offset is set in assign_file_positions_except_relocs. */
2281 shstrtab_hdr->sh_addralign = 1;
2283 if (!assign_file_positions_except_relocs (abfd))
2286 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2289 Elf_Internal_Shdr *hdr;
2291 off = elf_tdata (abfd)->next_file_pos;
2293 hdr = &elf_tdata (abfd)->symtab_hdr;
2294 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2296 hdr = &elf_tdata (abfd)->strtab_hdr;
2297 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2299 elf_tdata (abfd)->next_file_pos = off;
2301 /* Now that we know where the .strtab section goes, write it
2303 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2304 || ! _bfd_stringtab_emit (abfd, strtab))
2306 _bfd_stringtab_free (strtab);
2309 abfd->output_has_begun = true;
2314 /* Create a mapping from a set of sections to a program segment. */
2316 static INLINE struct elf_segment_map *
2317 make_mapping (abfd, sections, from, to, phdr)
2319 asection **sections;
2324 struct elf_segment_map *m;
2328 m = ((struct elf_segment_map *)
2330 (sizeof (struct elf_segment_map)
2331 + (to - from - 1) * sizeof (asection *))));
2335 m->p_type = PT_LOAD;
2336 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2337 m->sections[i - from] = *hdrpp;
2338 m->count = to - from;
2340 if (from == 0 && phdr)
2342 /* Include the headers in the first PT_LOAD segment. */
2343 m->includes_filehdr = 1;
2344 m->includes_phdrs = 1;
2350 /* Set up a mapping from BFD sections to program segments. */
2353 map_sections_to_segments (abfd)
2356 asection **sections = NULL;
2360 struct elf_segment_map *mfirst;
2361 struct elf_segment_map **pm;
2362 struct elf_segment_map *m;
2364 unsigned int phdr_index;
2365 bfd_vma maxpagesize;
2367 boolean phdr_in_segment = true;
2371 if (elf_tdata (abfd)->segment_map != NULL)
2374 if (bfd_count_sections (abfd) == 0)
2377 /* Select the allocated sections, and sort them. */
2379 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2380 * sizeof (asection *));
2381 if (sections == NULL)
2385 for (s = abfd->sections; s != NULL; s = s->next)
2387 if ((s->flags & SEC_ALLOC) != 0)
2393 BFD_ASSERT (i <= bfd_count_sections (abfd));
2396 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2398 /* Build the mapping. */
2403 /* If we have a .interp section, then create a PT_PHDR segment for
2404 the program headers and a PT_INTERP segment for the .interp
2406 s = bfd_get_section_by_name (abfd, ".interp");
2407 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2409 m = ((struct elf_segment_map *)
2410 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2414 m->p_type = PT_PHDR;
2415 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2416 m->p_flags = PF_R | PF_X;
2417 m->p_flags_valid = 1;
2418 m->includes_phdrs = 1;
2423 m = ((struct elf_segment_map *)
2424 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2428 m->p_type = PT_INTERP;
2436 /* Look through the sections. We put sections in the same program
2437 segment when the start of the second section can be placed within
2438 a few bytes of the end of the first section. */
2441 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2443 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2445 && (dynsec->flags & SEC_LOAD) == 0)
2448 /* Deal with -Ttext or something similar such that the first section
2449 is not adjacent to the program headers. This is an
2450 approximation, since at this point we don't know exactly how many
2451 program headers we will need. */
2454 bfd_size_type phdr_size;
2456 phdr_size = elf_tdata (abfd)->program_header_size;
2458 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2459 if ((abfd->flags & D_PAGED) == 0
2460 || sections[0]->lma < phdr_size
2461 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2462 phdr_in_segment = false;
2465 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2468 boolean new_segment;
2472 /* See if this section and the last one will fit in the same
2475 if (last_hdr == NULL)
2477 /* If we don't have a segment yet, then we don't need a new
2478 one (we build the last one after this loop). */
2479 new_segment = false;
2481 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2483 /* If this section has a different relation between the
2484 virtual address and the load address, then we need a new
2488 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2489 < BFD_ALIGN (hdr->lma, maxpagesize))
2491 /* If putting this section in this segment would force us to
2492 skip a page in the segment, then we need a new segment. */
2495 else if ((last_hdr->flags & SEC_LOAD) == 0
2496 && (hdr->flags & SEC_LOAD) != 0)
2498 /* We don't want to put a loadable section after a
2499 nonloadable section in the same segment. */
2502 else if ((abfd->flags & D_PAGED) == 0)
2504 /* If the file is not demand paged, which means that we
2505 don't require the sections to be correctly aligned in the
2506 file, then there is no other reason for a new segment. */
2507 new_segment = false;
2510 && (hdr->flags & SEC_READONLY) == 0
2511 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2514 /* We don't want to put a writable section in a read only
2515 segment, unless they are on the same page in memory
2516 anyhow. We already know that the last section does not
2517 bring us past the current section on the page, so the
2518 only case in which the new section is not on the same
2519 page as the previous section is when the previous section
2520 ends precisely on a page boundary. */
2525 /* Otherwise, we can use the same segment. */
2526 new_segment = false;
2531 if ((hdr->flags & SEC_READONLY) == 0)
2537 /* We need a new program segment. We must create a new program
2538 header holding all the sections from phdr_index until hdr. */
2540 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2547 if ((hdr->flags & SEC_READONLY) == 0)
2554 phdr_in_segment = false;
2557 /* Create a final PT_LOAD program segment. */
2558 if (last_hdr != NULL)
2560 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2568 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2571 m = ((struct elf_segment_map *)
2572 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2576 m->p_type = PT_DYNAMIC;
2578 m->sections[0] = dynsec;
2584 /* For each loadable .note section, add a PT_NOTE segment. We don't
2585 use bfd_get_section_by_name, because if we link together
2586 nonloadable .note sections and loadable .note sections, we will
2587 generate two .note sections in the output file. FIXME: Using
2588 names for section types is bogus anyhow. */
2589 for (s = abfd->sections; s != NULL; s = s->next)
2591 if ((s->flags & SEC_LOAD) != 0
2592 && strncmp (s->name, ".note", 5) == 0)
2594 m = ((struct elf_segment_map *)
2595 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2599 m->p_type = PT_NOTE;
2611 elf_tdata (abfd)->segment_map = mfirst;
2615 if (sections != NULL)
2620 /* Sort sections by address. */
2623 elf_sort_sections (arg1, arg2)
2627 const asection *sec1 = *(const asection **) arg1;
2628 const asection *sec2 = *(const asection **) arg2;
2630 /* Sort by LMA first, since this is the address used to
2631 place the section into a segment. */
2632 if (sec1->lma < sec2->lma)
2634 else if (sec1->lma > sec2->lma)
2637 /* Then sort by VMA. Normally the LMA and the VMA will be
2638 the same, and this will do nothing. */
2639 if (sec1->vma < sec2->vma)
2641 else if (sec1->vma > sec2->vma)
2644 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2646 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2651 return sec1->target_index - sec2->target_index;
2661 /* Sort by size, to put zero sized sections before others at the
2664 if (sec1->_raw_size < sec2->_raw_size)
2666 if (sec1->_raw_size > sec2->_raw_size)
2669 return sec1->target_index - sec2->target_index;
2672 /* Assign file positions to the sections based on the mapping from
2673 sections to segments. This function also sets up some fields in
2674 the file header, and writes out the program headers. */
2677 assign_file_positions_for_segments (abfd)
2680 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2682 struct elf_segment_map *m;
2684 Elf_Internal_Phdr *phdrs;
2686 bfd_vma filehdr_vaddr, filehdr_paddr;
2687 bfd_vma phdrs_vaddr, phdrs_paddr;
2688 Elf_Internal_Phdr *p;
2690 if (elf_tdata (abfd)->segment_map == NULL)
2692 if (! map_sections_to_segments (abfd))
2696 if (bed->elf_backend_modify_segment_map)
2698 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2703 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2706 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2707 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2708 elf_elfheader (abfd)->e_phnum = count;
2713 /* If we already counted the number of program segments, make sure
2714 that we allocated enough space. This happens when SIZEOF_HEADERS
2715 is used in a linker script. */
2716 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2717 if (alloc != 0 && count > alloc)
2719 ((*_bfd_error_handler)
2720 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2721 bfd_get_filename (abfd), alloc, count));
2722 bfd_set_error (bfd_error_bad_value);
2729 phdrs = ((Elf_Internal_Phdr *)
2730 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2734 off = bed->s->sizeof_ehdr;
2735 off += alloc * bed->s->sizeof_phdr;
2742 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2749 /* If elf_segment_map is not from map_sections_to_segments, the
2750 sections may not be correctly ordered. */
2752 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2755 p->p_type = m->p_type;
2756 p->p_flags = m->p_flags;
2758 if (p->p_type == PT_LOAD
2760 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2762 if ((abfd->flags & D_PAGED) != 0)
2763 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2766 bfd_size_type align;
2769 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2771 bfd_size_type secalign;
2773 secalign = bfd_get_section_alignment (abfd, *secpp);
2774 if (secalign > align)
2778 off += (m->sections[0]->vma - off) % (1 << align);
2785 p->p_vaddr = m->sections[0]->vma;
2787 if (m->p_paddr_valid)
2788 p->p_paddr = m->p_paddr;
2789 else if (m->count == 0)
2792 p->p_paddr = m->sections[0]->lma;
2794 if (p->p_type == PT_LOAD
2795 && (abfd->flags & D_PAGED) != 0)
2796 p->p_align = bed->maxpagesize;
2797 else if (m->count == 0)
2798 p->p_align = bed->s->file_align;
2806 if (m->includes_filehdr)
2808 if (! m->p_flags_valid)
2811 p->p_filesz = bed->s->sizeof_ehdr;
2812 p->p_memsz = bed->s->sizeof_ehdr;
2815 BFD_ASSERT (p->p_type == PT_LOAD);
2817 if (p->p_vaddr < (bfd_vma) off)
2819 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2820 bfd_get_filename (abfd));
2821 bfd_set_error (bfd_error_bad_value);
2826 if (! m->p_paddr_valid)
2829 if (p->p_type == PT_LOAD)
2831 filehdr_vaddr = p->p_vaddr;
2832 filehdr_paddr = p->p_paddr;
2836 if (m->includes_phdrs)
2838 if (! m->p_flags_valid)
2841 if (m->includes_filehdr)
2843 if (p->p_type == PT_LOAD)
2845 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2846 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2851 p->p_offset = bed->s->sizeof_ehdr;
2855 BFD_ASSERT (p->p_type == PT_LOAD);
2856 p->p_vaddr -= off - p->p_offset;
2857 if (! m->p_paddr_valid)
2858 p->p_paddr -= off - p->p_offset;
2861 if (p->p_type == PT_LOAD)
2863 phdrs_vaddr = p->p_vaddr;
2864 phdrs_paddr = p->p_paddr;
2867 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2870 p->p_filesz += alloc * bed->s->sizeof_phdr;
2871 p->p_memsz += alloc * bed->s->sizeof_phdr;
2874 if (p->p_type == PT_LOAD
2875 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2877 if (! m->includes_filehdr && ! m->includes_phdrs)
2883 adjust = off - (p->p_offset + p->p_filesz);
2884 p->p_filesz += adjust;
2885 p->p_memsz += adjust;
2891 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2895 bfd_size_type align;
2899 align = 1 << bfd_get_section_alignment (abfd, sec);
2901 /* The section may have artificial alignment forced by a
2902 link script. Notice this case by the gap between the
2903 cumulative phdr vma and the section's vma. */
2904 if (p->p_vaddr + p->p_memsz < sec->vma)
2906 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2908 p->p_memsz += adjust;
2911 if ((flags & SEC_LOAD) != 0)
2912 p->p_filesz += adjust;
2915 if (p->p_type == PT_LOAD)
2917 bfd_signed_vma adjust;
2919 if ((flags & SEC_LOAD) != 0)
2921 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2925 else if ((flags & SEC_ALLOC) != 0)
2927 /* The section VMA must equal the file position
2928 modulo the page size. FIXME: I'm not sure if
2929 this adjustment is really necessary. We used to
2930 not have the SEC_LOAD case just above, and then
2931 this was necessary, but now I'm not sure. */
2932 if ((abfd->flags & D_PAGED) != 0)
2933 adjust = (sec->vma - voff) % bed->maxpagesize;
2935 adjust = (sec->vma - voff) % align;
2944 (* _bfd_error_handler)
2945 (_("Error: First section in segment (%s) starts at 0x%x"),
2946 bfd_section_name (abfd, sec), sec->lma);
2947 (* _bfd_error_handler)
2948 (_(" whereas segment starts at 0x%x"),
2953 p->p_memsz += adjust;
2956 if ((flags & SEC_LOAD) != 0)
2957 p->p_filesz += adjust;
2962 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2963 used in a linker script we may have a section with
2964 SEC_LOAD clear but which is supposed to have
2966 if ((flags & SEC_LOAD) != 0
2967 || (flags & SEC_HAS_CONTENTS) != 0)
2968 off += sec->_raw_size;
2970 if ((flags & SEC_ALLOC) != 0)
2971 voff += sec->_raw_size;
2974 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2976 /* The actual "note" segment has i == 0.
2977 This is the one that actually contains everything. */
2981 p->p_filesz = sec->_raw_size;
2982 off += sec->_raw_size;
2987 /* Fake sections -- don't need to be written. */
2990 flags = sec->flags = 0;
2997 p->p_memsz += sec->_raw_size;
2999 if ((flags & SEC_LOAD) != 0)
3000 p->p_filesz += sec->_raw_size;
3002 if (align > p->p_align
3003 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3007 if (! m->p_flags_valid)
3010 if ((flags & SEC_CODE) != 0)
3012 if ((flags & SEC_READONLY) == 0)
3018 /* Now that we have set the section file positions, we can set up
3019 the file positions for the non PT_LOAD segments. */
3020 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3024 if (p->p_type != PT_LOAD && m->count > 0)
3026 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3027 p->p_offset = m->sections[0]->filepos;
3031 if (m->includes_filehdr)
3033 p->p_vaddr = filehdr_vaddr;
3034 if (! m->p_paddr_valid)
3035 p->p_paddr = filehdr_paddr;
3037 else if (m->includes_phdrs)
3039 p->p_vaddr = phdrs_vaddr;
3040 if (! m->p_paddr_valid)
3041 p->p_paddr = phdrs_paddr;
3046 /* Clear out any program headers we allocated but did not use. */
3047 for (; count < alloc; count++, p++)
3049 memset (p, 0, sizeof *p);
3050 p->p_type = PT_NULL;
3053 elf_tdata (abfd)->phdr = phdrs;
3055 elf_tdata (abfd)->next_file_pos = off;
3057 /* Write out the program headers. */
3058 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3059 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3065 /* Get the size of the program header.
3067 If this is called by the linker before any of the section VMA's are set, it
3068 can't calculate the correct value for a strange memory layout. This only
3069 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3070 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3071 data segment (exclusive of .interp and .dynamic).
3073 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3074 will be two segments. */
3076 static bfd_size_type
3077 get_program_header_size (abfd)
3082 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3084 /* We can't return a different result each time we're called. */
3085 if (elf_tdata (abfd)->program_header_size != 0)
3086 return elf_tdata (abfd)->program_header_size;
3088 if (elf_tdata (abfd)->segment_map != NULL)
3090 struct elf_segment_map *m;
3093 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3095 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3096 return elf_tdata (abfd)->program_header_size;
3099 /* Assume we will need exactly two PT_LOAD segments: one for text
3100 and one for data. */
3103 s = bfd_get_section_by_name (abfd, ".interp");
3104 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3106 /* If we have a loadable interpreter section, we need a
3107 PT_INTERP segment. In this case, assume we also need a
3108 PT_PHDR segment, although that may not be true for all
3113 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3115 /* We need a PT_DYNAMIC segment. */
3119 for (s = abfd->sections; s != NULL; s = s->next)
3121 if ((s->flags & SEC_LOAD) != 0
3122 && strncmp (s->name, ".note", 5) == 0)
3124 /* We need a PT_NOTE segment. */
3129 /* Let the backend count up any program headers it might need. */
3130 if (bed->elf_backend_additional_program_headers)
3134 a = (*bed->elf_backend_additional_program_headers) (abfd);
3140 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3141 return elf_tdata (abfd)->program_header_size;
3144 /* Work out the file positions of all the sections. This is called by
3145 _bfd_elf_compute_section_file_positions. All the section sizes and
3146 VMAs must be known before this is called.
3148 We do not consider reloc sections at this point, unless they form
3149 part of the loadable image. Reloc sections are assigned file
3150 positions in assign_file_positions_for_relocs, which is called by
3151 write_object_contents and final_link.
3153 We also don't set the positions of the .symtab and .strtab here. */
3156 assign_file_positions_except_relocs (abfd)
3159 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3160 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3161 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3163 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3165 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3166 && bfd_get_format (abfd) != bfd_core)
3168 Elf_Internal_Shdr **hdrpp;
3171 /* Start after the ELF header. */
3172 off = i_ehdrp->e_ehsize;
3174 /* We are not creating an executable, which means that we are
3175 not creating a program header, and that the actual order of
3176 the sections in the file is unimportant. */
3177 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3179 Elf_Internal_Shdr *hdr;
3182 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3184 hdr->sh_offset = -1;
3187 if (i == tdata->symtab_section
3188 || i == tdata->strtab_section)
3190 hdr->sh_offset = -1;
3194 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3200 Elf_Internal_Shdr **hdrpp;
3202 /* Assign file positions for the loaded sections based on the
3203 assignment of sections to segments. */
3204 if (! assign_file_positions_for_segments (abfd))
3207 /* Assign file positions for the other sections. */
3209 off = elf_tdata (abfd)->next_file_pos;
3210 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3212 Elf_Internal_Shdr *hdr;
3215 if (hdr->bfd_section != NULL
3216 && hdr->bfd_section->filepos != 0)
3217 hdr->sh_offset = hdr->bfd_section->filepos;
3218 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3220 ((*_bfd_error_handler)
3221 (_("%s: warning: allocated section `%s' not in segment"),
3222 bfd_get_filename (abfd),
3223 (hdr->bfd_section == NULL
3225 : hdr->bfd_section->name)));
3226 if ((abfd->flags & D_PAGED) != 0)
3227 off += (hdr->sh_addr - off) % bed->maxpagesize;
3229 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3230 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3233 else if (hdr->sh_type == SHT_REL
3234 || hdr->sh_type == SHT_RELA
3235 || hdr == i_shdrpp[tdata->symtab_section]
3236 || hdr == i_shdrpp[tdata->strtab_section])
3237 hdr->sh_offset = -1;
3239 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3243 /* Place the section headers. */
3244 off = align_file_position (off, bed->s->file_align);
3245 i_ehdrp->e_shoff = off;
3246 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3248 elf_tdata (abfd)->next_file_pos = off;
3257 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3258 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3259 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3261 struct bfd_strtab_hash *shstrtab;
3262 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3264 i_ehdrp = elf_elfheader (abfd);
3265 i_shdrp = elf_elfsections (abfd);
3267 shstrtab = _bfd_elf_stringtab_init ();
3268 if (shstrtab == NULL)
3271 elf_shstrtab (abfd) = shstrtab;
3273 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3274 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3275 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3276 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3278 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3279 i_ehdrp->e_ident[EI_DATA] =
3280 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3281 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3283 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3284 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3286 for (count = EI_PAD; count < EI_NIDENT; count++)
3287 i_ehdrp->e_ident[count] = 0;
3289 if ((abfd->flags & DYNAMIC) != 0)
3290 i_ehdrp->e_type = ET_DYN;
3291 else if ((abfd->flags & EXEC_P) != 0)
3292 i_ehdrp->e_type = ET_EXEC;
3293 else if (bfd_get_format (abfd) == bfd_core)
3294 i_ehdrp->e_type = ET_CORE;
3296 i_ehdrp->e_type = ET_REL;
3298 switch (bfd_get_arch (abfd))
3300 case bfd_arch_unknown:
3301 i_ehdrp->e_machine = EM_NONE;
3303 case bfd_arch_sparc:
3304 if (bfd_get_arch_size (abfd) == 64)
3305 i_ehdrp->e_machine = EM_SPARCV9;
3307 i_ehdrp->e_machine = EM_SPARC;
3310 i_ehdrp->e_machine = EM_S370;
3313 if (bfd_get_arch_size (abfd) == 64)
3314 i_ehdrp->e_machine = EM_X86_64;
3316 i_ehdrp->e_machine = EM_386;
3319 i_ehdrp->e_machine = EM_IA_64;
3321 case bfd_arch_m68hc11:
3322 i_ehdrp->e_machine = EM_68HC11;
3324 case bfd_arch_m68hc12:
3325 i_ehdrp->e_machine = EM_68HC12;
3328 i_ehdrp->e_machine = EM_S390;
3331 i_ehdrp->e_machine = EM_68K;
3334 i_ehdrp->e_machine = EM_88K;
3337 i_ehdrp->e_machine = EM_860;
3340 i_ehdrp->e_machine = EM_960;
3342 case bfd_arch_mips: /* MIPS Rxxxx */
3343 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3346 i_ehdrp->e_machine = EM_PARISC;
3348 case bfd_arch_powerpc:
3349 i_ehdrp->e_machine = EM_PPC;
3351 case bfd_arch_alpha:
3352 i_ehdrp->e_machine = EM_ALPHA;
3355 i_ehdrp->e_machine = EM_SH;
3358 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3361 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3364 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3366 case bfd_arch_mcore:
3367 i_ehdrp->e_machine = EM_MCORE;
3370 i_ehdrp->e_machine = EM_AVR;
3373 switch (bfd_get_mach (abfd))
3376 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3380 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3383 i_ehdrp->e_machine = EM_ARM;
3386 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3388 case bfd_arch_mn10200:
3389 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3391 case bfd_arch_mn10300:
3392 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3395 i_ehdrp->e_machine = EM_PJ;
3398 i_ehdrp->e_machine = EM_CRIS;
3400 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3402 i_ehdrp->e_machine = EM_NONE;
3404 i_ehdrp->e_version = bed->s->ev_current;
3405 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3407 /* No program header, for now. */
3408 i_ehdrp->e_phoff = 0;
3409 i_ehdrp->e_phentsize = 0;
3410 i_ehdrp->e_phnum = 0;
3412 /* Each bfd section is section header entry. */
3413 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3414 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3416 /* If we're building an executable, we'll need a program header table. */
3417 if (abfd->flags & EXEC_P)
3419 /* It all happens later. */
3421 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3423 /* elf_build_phdrs() returns a (NULL-terminated) array of
3424 Elf_Internal_Phdrs. */
3425 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3426 i_ehdrp->e_phoff = outbase;
3427 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3432 i_ehdrp->e_phentsize = 0;
3434 i_ehdrp->e_phoff = 0;
3437 elf_tdata (abfd)->symtab_hdr.sh_name =
3438 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3439 elf_tdata (abfd)->strtab_hdr.sh_name =
3440 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3441 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3442 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3443 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3444 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3445 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3451 /* Assign file positions for all the reloc sections which are not part
3452 of the loadable file image. */
3455 _bfd_elf_assign_file_positions_for_relocs (abfd)
3460 Elf_Internal_Shdr **shdrpp;
3462 off = elf_tdata (abfd)->next_file_pos;
3464 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3465 i < elf_elfheader (abfd)->e_shnum;
3468 Elf_Internal_Shdr *shdrp;
3471 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3472 && shdrp->sh_offset == -1)
3473 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3476 elf_tdata (abfd)->next_file_pos = off;
3480 _bfd_elf_write_object_contents (abfd)
3483 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3484 Elf_Internal_Ehdr *i_ehdrp;
3485 Elf_Internal_Shdr **i_shdrp;
3489 if (! abfd->output_has_begun
3490 && ! _bfd_elf_compute_section_file_positions
3491 (abfd, (struct bfd_link_info *) NULL))
3494 i_shdrp = elf_elfsections (abfd);
3495 i_ehdrp = elf_elfheader (abfd);
3498 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3502 _bfd_elf_assign_file_positions_for_relocs (abfd);
3504 /* After writing the headers, we need to write the sections too... */
3505 for (count = 1; count < i_ehdrp->e_shnum; count++)
3507 if (bed->elf_backend_section_processing)
3508 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3509 if (i_shdrp[count]->contents)
3511 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3512 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3514 != i_shdrp[count]->sh_size))
3519 /* Write out the section header names. */
3520 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3521 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3524 if (bed->elf_backend_final_write_processing)
3525 (*bed->elf_backend_final_write_processing) (abfd,
3526 elf_tdata (abfd)->linker);
3528 return bed->s->write_shdrs_and_ehdr (abfd);
3532 _bfd_elf_write_corefile_contents (abfd)
3535 /* Hopefully this can be done just like an object file. */
3536 return _bfd_elf_write_object_contents (abfd);
3539 /* Given a section, search the header to find them. */
3542 _bfd_elf_section_from_bfd_section (abfd, asect)
3546 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3547 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3549 Elf_Internal_Shdr *hdr;
3550 int maxindex = elf_elfheader (abfd)->e_shnum;
3552 for (index = 0; index < maxindex; index++)
3554 hdr = i_shdrp[index];
3555 if (hdr->bfd_section == asect)
3559 if (bed->elf_backend_section_from_bfd_section)
3561 for (index = 0; index < maxindex; index++)
3565 hdr = i_shdrp[index];
3567 if ((*bed->elf_backend_section_from_bfd_section)
3568 (abfd, hdr, asect, &retval))
3573 if (bfd_is_abs_section (asect))
3575 if (bfd_is_com_section (asect))
3577 if (bfd_is_und_section (asect))
3580 bfd_set_error (bfd_error_nonrepresentable_section);
3585 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3589 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3591 asymbol **asym_ptr_ptr;
3593 asymbol *asym_ptr = *asym_ptr_ptr;
3595 flagword flags = asym_ptr->flags;
3597 /* When gas creates relocations against local labels, it creates its
3598 own symbol for the section, but does put the symbol into the
3599 symbol chain, so udata is 0. When the linker is generating
3600 relocatable output, this section symbol may be for one of the
3601 input sections rather than the output section. */
3602 if (asym_ptr->udata.i == 0
3603 && (flags & BSF_SECTION_SYM)
3604 && asym_ptr->section)
3608 if (asym_ptr->section->output_section != NULL)
3609 indx = asym_ptr->section->output_section->index;
3611 indx = asym_ptr->section->index;
3612 if (elf_section_syms (abfd)[indx])
3613 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3616 idx = asym_ptr->udata.i;
3620 /* This case can occur when using --strip-symbol on a symbol
3621 which is used in a relocation entry. */
3622 (*_bfd_error_handler)
3623 (_("%s: symbol `%s' required but not present"),
3624 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3625 bfd_set_error (bfd_error_no_symbols);
3632 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3633 (long) asym_ptr, asym_ptr->name, idx, flags,
3634 elf_symbol_flags (flags));
3642 /* Copy private BFD data. This copies any program header information. */
3645 copy_private_bfd_data (ibfd, obfd)
3649 Elf_Internal_Ehdr * iehdr;
3650 struct elf_segment_map * map;
3651 struct elf_segment_map * map_first;
3652 struct elf_segment_map ** pointer_to_map;
3653 Elf_Internal_Phdr * segment;
3656 unsigned int num_segments;
3657 boolean phdr_included = false;
3658 bfd_vma maxpagesize;
3659 struct elf_segment_map * phdr_adjust_seg = NULL;
3660 unsigned int phdr_adjust_num = 0;
3662 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3663 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3666 if (elf_tdata (ibfd)->phdr == NULL)
3669 iehdr = elf_elfheader (ibfd);
3672 pointer_to_map = &map_first;
3674 num_segments = elf_elfheader (ibfd)->e_phnum;
3675 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3677 /* Returns the end address of the segment + 1. */
3678 #define SEGMENT_END(segment, start) \
3679 (start + (segment->p_memsz > segment->p_filesz \
3680 ? segment->p_memsz : segment->p_filesz))
3682 /* Returns true if the given section is contained within
3683 the given segment. VMA addresses are compared. */
3684 #define IS_CONTAINED_BY_VMA(section, segment) \
3685 (section->vma >= segment->p_vaddr \
3686 && (section->vma + section->_raw_size) \
3687 <= (SEGMENT_END (segment, segment->p_vaddr)))
3689 /* Returns true if the given section is contained within
3690 the given segment. LMA addresses are compared. */
3691 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3692 (section->lma >= base \
3693 && (section->lma + section->_raw_size) \
3694 <= SEGMENT_END (segment, base))
3696 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3697 #define IS_COREFILE_NOTE(p, s) \
3698 (p->p_type == PT_NOTE \
3699 && bfd_get_format (ibfd) == bfd_core \
3700 && s->vma == 0 && s->lma == 0 \
3701 && (bfd_vma) s->filepos >= p->p_offset \
3702 && (bfd_vma) s->filepos + s->_raw_size \
3703 <= p->p_offset + p->p_filesz)
3705 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3706 linker, which generates a PT_INTERP section with p_vaddr and
3707 p_memsz set to 0. */
3708 #define IS_SOLARIS_PT_INTERP(p, s) \
3710 && p->p_filesz > 0 \
3711 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3712 && s->_raw_size > 0 \
3713 && (bfd_vma) s->filepos >= p->p_offset \
3714 && ((bfd_vma) s->filepos + s->_raw_size \
3715 <= p->p_offset + p->p_filesz))
3717 /* Decide if the given section should be included in the given segment.
3718 A section will be included if:
3719 1. It is within the address space of the segment,
3720 2. It is an allocated segment,
3721 3. There is an output section associated with it,
3722 4. The section has not already been allocated to a previous segment. */
3723 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3724 ((((IS_CONTAINED_BY_VMA (section, segment) \
3725 || IS_SOLARIS_PT_INTERP (segment, section)) \
3726 && (section->flags & SEC_ALLOC) != 0) \
3727 || IS_COREFILE_NOTE (segment, section)) \
3728 && section->output_section != NULL \
3729 && section->segment_mark == false)
3731 /* Returns true iff seg1 starts after the end of seg2. */
3732 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3733 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3735 /* Returns true iff seg1 and seg2 overlap. */
3736 #define SEGMENT_OVERLAPS(seg1, seg2) \
3737 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3739 /* Initialise the segment mark field. */
3740 for (section = ibfd->sections; section != NULL; section = section->next)
3741 section->segment_mark = false;
3743 /* Scan through the segments specified in the program header
3744 of the input BFD. For this first scan we look for overlaps
3745 in the loadable segments. These can be created by wierd
3746 parameters to objcopy. */
3747 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3752 Elf_Internal_Phdr *segment2;
3754 if (segment->p_type != PT_LOAD)
3757 /* Determine if this segment overlaps any previous segments. */
3758 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3760 bfd_signed_vma extra_length;
3762 if (segment2->p_type != PT_LOAD
3763 || ! SEGMENT_OVERLAPS (segment, segment2))
3766 /* Merge the two segments together. */
3767 if (segment2->p_vaddr < segment->p_vaddr)
3769 /* Extend SEGMENT2 to include SEGMENT and then delete
3772 SEGMENT_END (segment, segment->p_vaddr)
3773 - SEGMENT_END (segment2, segment2->p_vaddr);
3775 if (extra_length > 0)
3777 segment2->p_memsz += extra_length;
3778 segment2->p_filesz += extra_length;
3781 segment->p_type = PT_NULL;
3783 /* Since we have deleted P we must restart the outer loop. */
3785 segment = elf_tdata (ibfd)->phdr;
3790 /* Extend SEGMENT to include SEGMENT2 and then delete
3793 SEGMENT_END (segment2, segment2->p_vaddr)
3794 - SEGMENT_END (segment, segment->p_vaddr);
3796 if (extra_length > 0)
3798 segment->p_memsz += extra_length;
3799 segment->p_filesz += extra_length;
3802 segment2->p_type = PT_NULL;
3807 /* The second scan attempts to assign sections to segments. */
3808 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3812 unsigned int section_count;
3813 asection ** sections;
3814 asection * output_section;
3816 bfd_vma matching_lma;
3817 bfd_vma suggested_lma;
3820 if (segment->p_type == PT_NULL)
3823 /* Compute how many sections might be placed into this segment. */
3825 for (section = ibfd->sections; section != NULL; section = section->next)
3826 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3829 /* Allocate a segment map big enough to contain all of the
3830 sections we have selected. */
3831 map = ((struct elf_segment_map *)
3833 (sizeof (struct elf_segment_map)
3834 + ((size_t) section_count - 1) * sizeof (asection *))));
3838 /* Initialise the fields of the segment map. Default to
3839 using the physical address of the segment in the input BFD. */
3841 map->p_type = segment->p_type;
3842 map->p_flags = segment->p_flags;
3843 map->p_flags_valid = 1;
3844 map->p_paddr = segment->p_paddr;
3845 map->p_paddr_valid = 1;
3847 /* Determine if this segment contains the ELF file header
3848 and if it contains the program headers themselves. */
3849 map->includes_filehdr = (segment->p_offset == 0
3850 && segment->p_filesz >= iehdr->e_ehsize);
3852 map->includes_phdrs = 0;
3854 if (! phdr_included || segment->p_type != PT_LOAD)
3856 map->includes_phdrs =
3857 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3858 && (segment->p_offset + segment->p_filesz
3859 >= ((bfd_vma) iehdr->e_phoff
3860 + iehdr->e_phnum * iehdr->e_phentsize)));
3862 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3863 phdr_included = true;
3866 if (section_count == 0)
3868 /* Special segments, such as the PT_PHDR segment, may contain
3869 no sections, but ordinary, loadable segments should contain
3871 if (segment->p_type == PT_LOAD)
3873 (_("%s: warning: Empty loadable segment detected\n"),
3874 bfd_get_filename (ibfd));
3877 *pointer_to_map = map;
3878 pointer_to_map = &map->next;
3883 /* Now scan the sections in the input BFD again and attempt
3884 to add their corresponding output sections to the segment map.
3885 The problem here is how to handle an output section which has
3886 been moved (ie had its LMA changed). There are four possibilities:
3888 1. None of the sections have been moved.
3889 In this case we can continue to use the segment LMA from the
3892 2. All of the sections have been moved by the same amount.
3893 In this case we can change the segment's LMA to match the LMA
3894 of the first section.
3896 3. Some of the sections have been moved, others have not.
3897 In this case those sections which have not been moved can be
3898 placed in the current segment which will have to have its size,
3899 and possibly its LMA changed, and a new segment or segments will
3900 have to be created to contain the other sections.
3902 4. The sections have been moved, but not be the same amount.
3903 In this case we can change the segment's LMA to match the LMA
3904 of the first section and we will have to create a new segment
3905 or segments to contain the other sections.
3907 In order to save time, we allocate an array to hold the section
3908 pointers that we are interested in. As these sections get assigned
3909 to a segment, they are removed from this array. */
3911 sections = (asection **) bfd_malloc
3912 (sizeof (asection *) * section_count);
3913 if (sections == NULL)
3916 /* Step One: Scan for segment vs section LMA conflicts.
3917 Also add the sections to the section array allocated above.
3918 Also add the sections to the current segment. In the common
3919 case, where the sections have not been moved, this means that
3920 we have completely filled the segment, and there is nothing
3926 for (j = 0, section = ibfd->sections;
3928 section = section->next)
3930 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3932 output_section = section->output_section;
3934 sections[j ++] = section;
3936 /* The Solaris native linker always sets p_paddr to 0.
3937 We try to catch that case here, and set it to the
3939 if (segment->p_paddr == 0
3940 && segment->p_vaddr != 0
3942 && output_section->lma != 0
3943 && (output_section->vma == (segment->p_vaddr
3944 + (map->includes_filehdr
3947 + (map->includes_phdrs
3948 ? iehdr->e_phnum * iehdr->e_phentsize
3950 map->p_paddr = segment->p_vaddr;
3952 /* Match up the physical address of the segment with the
3953 LMA address of the output section. */
3954 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3955 || IS_COREFILE_NOTE (segment, section))
3957 if (matching_lma == 0)
3958 matching_lma = output_section->lma;
3960 /* We assume that if the section fits within the segment
3961 then it does not overlap any other section within that
3963 map->sections[isec ++] = output_section;
3965 else if (suggested_lma == 0)
3966 suggested_lma = output_section->lma;
3970 BFD_ASSERT (j == section_count);
3972 /* Step Two: Adjust the physical address of the current segment,
3974 if (isec == section_count)
3976 /* All of the sections fitted within the segment as currently
3977 specified. This is the default case. Add the segment to
3978 the list of built segments and carry on to process the next
3979 program header in the input BFD. */
3980 map->count = section_count;
3981 *pointer_to_map = map;
3982 pointer_to_map = &map->next;
3989 if (matching_lma != 0)
3991 /* At least one section fits inside the current segment.
3992 Keep it, but modify its physical address to match the
3993 LMA of the first section that fitted. */
3994 map->p_paddr = matching_lma;
3998 /* None of the sections fitted inside the current segment.
3999 Change the current segment's physical address to match
4000 the LMA of the first section. */
4001 map->p_paddr = suggested_lma;
4004 /* Offset the segment physical address from the lma
4005 to allow for space taken up by elf headers. */
4006 if (map->includes_filehdr)
4007 map->p_paddr -= iehdr->e_ehsize;
4009 if (map->includes_phdrs)
4011 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4013 /* iehdr->e_phnum is just an estimate of the number
4014 of program headers that we will need. Make a note
4015 here of the number we used and the segment we chose
4016 to hold these headers, so that we can adjust the
4017 offset when we know the correct value. */
4018 phdr_adjust_num = iehdr->e_phnum;
4019 phdr_adjust_seg = map;
4023 /* Step Three: Loop over the sections again, this time assigning
4024 those that fit to the current segment and remvoing them from the
4025 sections array; but making sure not to leave large gaps. Once all
4026 possible sections have been assigned to the current segment it is
4027 added to the list of built segments and if sections still remain
4028 to be assigned, a new segment is constructed before repeating
4036 /* Fill the current segment with sections that fit. */
4037 for (j = 0; j < section_count; j++)
4039 section = sections[j];
4041 if (section == NULL)
4044 output_section = section->output_section;
4046 BFD_ASSERT (output_section != NULL);
4048 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4049 || IS_COREFILE_NOTE (segment, section))
4051 if (map->count == 0)
4053 /* If the first section in a segment does not start at
4054 the beginning of the segment, then something is
4056 if (output_section->lma !=
4058 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4059 + (map->includes_phdrs
4060 ? iehdr->e_phnum * iehdr->e_phentsize
4066 asection * prev_sec;
4068 prev_sec = map->sections[map->count - 1];
4070 /* If the gap between the end of the previous section
4071 and the start of this section is more than
4072 maxpagesize then we need to start a new segment. */
4073 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4074 < BFD_ALIGN (output_section->lma, maxpagesize))
4075 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4077 if (suggested_lma == 0)
4078 suggested_lma = output_section->lma;
4084 map->sections[map->count++] = output_section;
4087 section->segment_mark = true;
4089 else if (suggested_lma == 0)
4090 suggested_lma = output_section->lma;
4093 BFD_ASSERT (map->count > 0);
4095 /* Add the current segment to the list of built segments. */
4096 *pointer_to_map = map;
4097 pointer_to_map = &map->next;
4099 if (isec < section_count)
4101 /* We still have not allocated all of the sections to
4102 segments. Create a new segment here, initialise it
4103 and carry on looping. */
4104 map = ((struct elf_segment_map *)
4106 (sizeof (struct elf_segment_map)
4107 + ((size_t) section_count - 1)
4108 * sizeof (asection *))));
4112 /* Initialise the fields of the segment map. Set the physical
4113 physical address to the LMA of the first section that has
4114 not yet been assigned. */
4116 map->p_type = segment->p_type;
4117 map->p_flags = segment->p_flags;
4118 map->p_flags_valid = 1;
4119 map->p_paddr = suggested_lma;
4120 map->p_paddr_valid = 1;
4121 map->includes_filehdr = 0;
4122 map->includes_phdrs = 0;
4125 while (isec < section_count);
4130 /* The Solaris linker creates program headers in which all the
4131 p_paddr fields are zero. When we try to objcopy or strip such a
4132 file, we get confused. Check for this case, and if we find it
4133 reset the p_paddr_valid fields. */
4134 for (map = map_first; map != NULL; map = map->next)
4135 if (map->p_paddr != 0)
4139 for (map = map_first; map != NULL; map = map->next)
4140 map->p_paddr_valid = 0;
4143 elf_tdata (obfd)->segment_map = map_first;
4145 /* If we had to estimate the number of program headers that were
4146 going to be needed, then check our estimate know and adjust
4147 the offset if necessary. */
4148 if (phdr_adjust_seg != NULL)
4152 for (count = 0, map = map_first; map != NULL; map = map->next)
4155 if (count > phdr_adjust_num)
4156 phdr_adjust_seg->p_paddr
4157 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4161 /* Final Step: Sort the segments into ascending order of physical
4163 if (map_first != NULL)
4165 struct elf_segment_map *prev;
4168 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4170 /* Yes I know - its a bubble sort.... */
4171 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4173 /* Swap map and map->next. */
4174 prev->next = map->next;
4175 map->next = map->next->next;
4176 prev->next->next = map;
4186 #undef IS_CONTAINED_BY_VMA
4187 #undef IS_CONTAINED_BY_LMA
4188 #undef IS_COREFILE_NOTE
4189 #undef IS_SOLARIS_PT_INTERP
4190 #undef INCLUDE_SECTION_IN_SEGMENT
4191 #undef SEGMENT_AFTER_SEGMENT
4192 #undef SEGMENT_OVERLAPS
4196 /* Copy private section information. This copies over the entsize
4197 field, and sometimes the info field. */
4200 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4206 Elf_Internal_Shdr *ihdr, *ohdr;
4208 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4209 || obfd->xvec->flavour != bfd_target_elf_flavour)
4212 /* Copy over private BFD data if it has not already been copied.
4213 This must be done here, rather than in the copy_private_bfd_data
4214 entry point, because the latter is called after the section
4215 contents have been set, which means that the program headers have
4216 already been worked out. */
4217 if (elf_tdata (obfd)->segment_map == NULL
4218 && elf_tdata (ibfd)->phdr != NULL)
4222 /* Only set up the segments if there are no more SEC_ALLOC
4223 sections. FIXME: This won't do the right thing if objcopy is
4224 used to remove the last SEC_ALLOC section, since objcopy
4225 won't call this routine in that case. */
4226 for (s = isec->next; s != NULL; s = s->next)
4227 if ((s->flags & SEC_ALLOC) != 0)
4231 if (! copy_private_bfd_data (ibfd, obfd))
4236 ihdr = &elf_section_data (isec)->this_hdr;
4237 ohdr = &elf_section_data (osec)->this_hdr;
4239 ohdr->sh_entsize = ihdr->sh_entsize;
4241 if (ihdr->sh_type == SHT_SYMTAB
4242 || ihdr->sh_type == SHT_DYNSYM
4243 || ihdr->sh_type == SHT_GNU_verneed
4244 || ihdr->sh_type == SHT_GNU_verdef)
4245 ohdr->sh_info = ihdr->sh_info;
4247 elf_section_data (osec)->use_rela_p
4248 = elf_section_data (isec)->use_rela_p;
4253 /* Copy private symbol information. If this symbol is in a section
4254 which we did not map into a BFD section, try to map the section
4255 index correctly. We use special macro definitions for the mapped
4256 section indices; these definitions are interpreted by the
4257 swap_out_syms function. */
4259 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4260 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4261 #define MAP_STRTAB (SHN_LORESERVE - 3)
4262 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4265 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4271 elf_symbol_type *isym, *osym;
4273 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4274 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4277 isym = elf_symbol_from (ibfd, isymarg);
4278 osym = elf_symbol_from (obfd, osymarg);
4282 && bfd_is_abs_section (isym->symbol.section))
4286 shndx = isym->internal_elf_sym.st_shndx;
4287 if (shndx == elf_onesymtab (ibfd))
4288 shndx = MAP_ONESYMTAB;
4289 else if (shndx == elf_dynsymtab (ibfd))
4290 shndx = MAP_DYNSYMTAB;
4291 else if (shndx == elf_tdata (ibfd)->strtab_section)
4293 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4294 shndx = MAP_SHSTRTAB;
4295 osym->internal_elf_sym.st_shndx = shndx;
4301 /* Swap out the symbols. */
4304 swap_out_syms (abfd, sttp, relocatable_p)
4306 struct bfd_strtab_hash **sttp;
4309 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4311 if (!elf_map_symbols (abfd))
4314 /* Dump out the symtabs. */
4316 int symcount = bfd_get_symcount (abfd);
4317 asymbol **syms = bfd_get_outsymbols (abfd);
4318 struct bfd_strtab_hash *stt;
4319 Elf_Internal_Shdr *symtab_hdr;
4320 Elf_Internal_Shdr *symstrtab_hdr;
4321 char *outbound_syms;
4324 stt = _bfd_elf_stringtab_init ();
4328 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4329 symtab_hdr->sh_type = SHT_SYMTAB;
4330 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4331 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4332 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4333 symtab_hdr->sh_addralign = bed->s->file_align;
4335 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4336 symstrtab_hdr->sh_type = SHT_STRTAB;
4338 outbound_syms = bfd_alloc (abfd,
4339 (1 + symcount) * bed->s->sizeof_sym);
4340 if (outbound_syms == NULL)
4342 symtab_hdr->contents = (PTR) outbound_syms;
4344 /* now generate the data (for "contents") */
4346 /* Fill in zeroth symbol and swap it out. */
4347 Elf_Internal_Sym sym;
4353 sym.st_shndx = SHN_UNDEF;
4354 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4355 outbound_syms += bed->s->sizeof_sym;
4357 for (idx = 0; idx < symcount; idx++)
4359 Elf_Internal_Sym sym;
4360 bfd_vma value = syms[idx]->value;
4361 elf_symbol_type *type_ptr;
4362 flagword flags = syms[idx]->flags;
4365 if ((flags & BSF_SECTION_SYM) != 0)
4367 /* Section symbols have no name. */
4372 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4375 if (sym.st_name == (unsigned long) -1)
4379 type_ptr = elf_symbol_from (abfd, syms[idx]);
4381 if ((flags & BSF_SECTION_SYM) == 0
4382 && bfd_is_com_section (syms[idx]->section))
4384 /* ELF common symbols put the alignment into the `value' field,
4385 and the size into the `size' field. This is backwards from
4386 how BFD handles it, so reverse it here. */
4387 sym.st_size = value;
4388 if (type_ptr == NULL
4389 || type_ptr->internal_elf_sym.st_value == 0)
4390 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4392 sym.st_value = type_ptr->internal_elf_sym.st_value;
4393 sym.st_shndx = _bfd_elf_section_from_bfd_section
4394 (abfd, syms[idx]->section);
4398 asection *sec = syms[idx]->section;
4401 if (sec->output_section)
4403 value += sec->output_offset;
4404 sec = sec->output_section;
4406 /* Don't add in the section vma for relocatable output. */
4407 if (! relocatable_p)
4409 sym.st_value = value;
4410 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4412 if (bfd_is_abs_section (sec)
4414 && type_ptr->internal_elf_sym.st_shndx != 0)
4416 /* This symbol is in a real ELF section which we did
4417 not create as a BFD section. Undo the mapping done
4418 by copy_private_symbol_data. */
4419 shndx = type_ptr->internal_elf_sym.st_shndx;
4423 shndx = elf_onesymtab (abfd);
4426 shndx = elf_dynsymtab (abfd);
4429 shndx = elf_tdata (abfd)->strtab_section;
4432 shndx = elf_tdata (abfd)->shstrtab_section;
4440 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4446 /* Writing this would be a hell of a lot easier if
4447 we had some decent documentation on bfd, and
4448 knew what to expect of the library, and what to
4449 demand of applications. For example, it
4450 appears that `objcopy' might not set the
4451 section of a symbol to be a section that is
4452 actually in the output file. */
4453 sec2 = bfd_get_section_by_name (abfd, sec->name);
4454 BFD_ASSERT (sec2 != 0);
4455 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4456 BFD_ASSERT (shndx != -1);
4460 sym.st_shndx = shndx;
4463 if ((flags & BSF_FUNCTION) != 0)
4465 else if ((flags & BSF_OBJECT) != 0)
4470 /* Processor-specific types */
4471 if (type_ptr != NULL
4472 && bed->elf_backend_get_symbol_type)
4473 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4475 if (flags & BSF_SECTION_SYM)
4476 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4477 else if (bfd_is_com_section (syms[idx]->section))
4478 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4479 else if (bfd_is_und_section (syms[idx]->section))
4480 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4484 else if (flags & BSF_FILE)
4485 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4488 int bind = STB_LOCAL;
4490 if (flags & BSF_LOCAL)
4492 else if (flags & BSF_WEAK)
4494 else if (flags & BSF_GLOBAL)
4497 sym.st_info = ELF_ST_INFO (bind, type);
4500 if (type_ptr != NULL)
4501 sym.st_other = type_ptr->internal_elf_sym.st_other;
4505 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4506 outbound_syms += bed->s->sizeof_sym;
4510 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4511 symstrtab_hdr->sh_type = SHT_STRTAB;
4513 symstrtab_hdr->sh_flags = 0;
4514 symstrtab_hdr->sh_addr = 0;
4515 symstrtab_hdr->sh_entsize = 0;
4516 symstrtab_hdr->sh_link = 0;
4517 symstrtab_hdr->sh_info = 0;
4518 symstrtab_hdr->sh_addralign = 1;
4524 /* Return the number of bytes required to hold the symtab vector.
4526 Note that we base it on the count plus 1, since we will null terminate
4527 the vector allocated based on this size. However, the ELF symbol table
4528 always has a dummy entry as symbol #0, so it ends up even. */
4531 _bfd_elf_get_symtab_upper_bound (abfd)
4536 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4538 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4539 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4545 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4550 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4552 if (elf_dynsymtab (abfd) == 0)
4554 bfd_set_error (bfd_error_invalid_operation);
4558 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4559 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4565 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4566 bfd *abfd ATTRIBUTE_UNUSED;
4569 return (asect->reloc_count + 1) * sizeof (arelent *);
4572 /* Canonicalize the relocs. */
4575 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4584 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4590 tblptr = section->relocation;
4591 for (i = 0; i < section->reloc_count; i++)
4592 *relptr++ = tblptr++;
4596 return section->reloc_count;
4600 _bfd_elf_get_symtab (abfd, alocation)
4602 asymbol **alocation;
4604 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4605 (abfd, alocation, false);
4608 bfd_get_symcount (abfd) = symcount;
4613 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4615 asymbol **alocation;
4617 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4618 (abfd, alocation, true);
4621 /* Return the size required for the dynamic reloc entries. Any
4622 section that was actually installed in the BFD, and has type
4623 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4624 considered to be a dynamic reloc section. */
4627 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4633 if (elf_dynsymtab (abfd) == 0)
4635 bfd_set_error (bfd_error_invalid_operation);
4639 ret = sizeof (arelent *);
4640 for (s = abfd->sections; s != NULL; s = s->next)
4641 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4642 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4643 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4644 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4645 * sizeof (arelent *));
4650 /* Canonicalize the dynamic relocation entries. Note that we return
4651 the dynamic relocations as a single block, although they are
4652 actually associated with particular sections; the interface, which
4653 was designed for SunOS style shared libraries, expects that there
4654 is only one set of dynamic relocs. Any section that was actually
4655 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4656 the dynamic symbol table, is considered to be a dynamic reloc
4660 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4665 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4669 if (elf_dynsymtab (abfd) == 0)
4671 bfd_set_error (bfd_error_invalid_operation);
4675 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4677 for (s = abfd->sections; s != NULL; s = s->next)
4679 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4680 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4681 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4686 if (! (*slurp_relocs) (abfd, s, syms, true))
4688 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4690 for (i = 0; i < count; i++)
4701 /* Read in the version information. */
4704 _bfd_elf_slurp_version_tables (abfd)
4707 bfd_byte *contents = NULL;
4709 if (elf_dynverdef (abfd) != 0)
4711 Elf_Internal_Shdr *hdr;
4712 Elf_External_Verdef *everdef;
4713 Elf_Internal_Verdef *iverdef;
4714 Elf_Internal_Verdef *iverdefarr;
4715 Elf_Internal_Verdef iverdefmem;
4719 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4721 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4722 if (contents == NULL)
4724 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4725 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4728 /* We know the number of entries in the section but not the maximum
4729 index. Therefore we have to run through all entries and find
4731 everdef = (Elf_External_Verdef *) contents;
4733 for (i = 0; i < hdr->sh_info; ++i)
4735 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4737 if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4738 maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4740 everdef = ((Elf_External_Verdef *)
4741 ((bfd_byte *) everdef + iverdefmem.vd_next));
4744 elf_tdata (abfd)->verdef =
4745 ((Elf_Internal_Verdef *)
4746 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4747 if (elf_tdata (abfd)->verdef == NULL)
4750 elf_tdata (abfd)->cverdefs = maxidx;
4752 everdef = (Elf_External_Verdef *) contents;
4753 iverdefarr = elf_tdata (abfd)->verdef;
4754 for (i = 0; i < hdr->sh_info; i++)
4756 Elf_External_Verdaux *everdaux;
4757 Elf_Internal_Verdaux *iverdaux;
4760 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4762 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4763 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4765 iverdef->vd_bfd = abfd;
4767 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4770 * sizeof (Elf_Internal_Verdaux))));
4771 if (iverdef->vd_auxptr == NULL)
4774 everdaux = ((Elf_External_Verdaux *)
4775 ((bfd_byte *) everdef + iverdef->vd_aux));
4776 iverdaux = iverdef->vd_auxptr;
4777 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4779 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4781 iverdaux->vda_nodename =
4782 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4783 iverdaux->vda_name);
4784 if (iverdaux->vda_nodename == NULL)
4787 if (j + 1 < iverdef->vd_cnt)
4788 iverdaux->vda_nextptr = iverdaux + 1;
4790 iverdaux->vda_nextptr = NULL;
4792 everdaux = ((Elf_External_Verdaux *)
4793 ((bfd_byte *) everdaux + iverdaux->vda_next));
4796 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4798 if (i + 1 < hdr->sh_info)
4799 iverdef->vd_nextdef = iverdef + 1;
4801 iverdef->vd_nextdef = NULL;
4803 everdef = ((Elf_External_Verdef *)
4804 ((bfd_byte *) everdef + iverdef->vd_next));
4811 if (elf_dynverref (abfd) != 0)
4813 Elf_Internal_Shdr *hdr;
4814 Elf_External_Verneed *everneed;
4815 Elf_Internal_Verneed *iverneed;
4818 hdr = &elf_tdata (abfd)->dynverref_hdr;
4820 elf_tdata (abfd)->verref =
4821 ((Elf_Internal_Verneed *)
4822 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4823 if (elf_tdata (abfd)->verref == NULL)
4826 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4828 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4829 if (contents == NULL)
4831 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4832 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4835 everneed = (Elf_External_Verneed *) contents;
4836 iverneed = elf_tdata (abfd)->verref;
4837 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4839 Elf_External_Vernaux *evernaux;
4840 Elf_Internal_Vernaux *ivernaux;
4843 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4845 iverneed->vn_bfd = abfd;
4847 iverneed->vn_filename =
4848 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4850 if (iverneed->vn_filename == NULL)
4853 iverneed->vn_auxptr =
4854 ((Elf_Internal_Vernaux *)
4856 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4858 evernaux = ((Elf_External_Vernaux *)
4859 ((bfd_byte *) everneed + iverneed->vn_aux));
4860 ivernaux = iverneed->vn_auxptr;
4861 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4863 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4865 ivernaux->vna_nodename =
4866 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4867 ivernaux->vna_name);
4868 if (ivernaux->vna_nodename == NULL)
4871 if (j + 1 < iverneed->vn_cnt)
4872 ivernaux->vna_nextptr = ivernaux + 1;
4874 ivernaux->vna_nextptr = NULL;
4876 evernaux = ((Elf_External_Vernaux *)
4877 ((bfd_byte *) evernaux + ivernaux->vna_next));
4880 if (i + 1 < hdr->sh_info)
4881 iverneed->vn_nextref = iverneed + 1;
4883 iverneed->vn_nextref = NULL;
4885 everneed = ((Elf_External_Verneed *)
4886 ((bfd_byte *) everneed + iverneed->vn_next));
4896 if (contents == NULL)
4902 _bfd_elf_make_empty_symbol (abfd)
4905 elf_symbol_type *newsym;
4907 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4912 newsym->symbol.the_bfd = abfd;
4913 return &newsym->symbol;
4918 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4919 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4923 bfd_symbol_info (symbol, ret);
4926 /* Return whether a symbol name implies a local symbol. Most targets
4927 use this function for the is_local_label_name entry point, but some
4931 _bfd_elf_is_local_label_name (abfd, name)
4932 bfd *abfd ATTRIBUTE_UNUSED;
4935 /* Normal local symbols start with ``.L''. */
4936 if (name[0] == '.' && name[1] == 'L')
4939 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4940 DWARF debugging symbols starting with ``..''. */
4941 if (name[0] == '.' && name[1] == '.')
4944 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4945 emitting DWARF debugging output. I suspect this is actually a
4946 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4947 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4948 underscore to be emitted on some ELF targets). For ease of use,
4949 we treat such symbols as local. */
4950 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4957 _bfd_elf_get_lineno (ignore_abfd, symbol)
4958 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4959 asymbol *symbol ATTRIBUTE_UNUSED;
4966 _bfd_elf_set_arch_mach (abfd, arch, machine)
4968 enum bfd_architecture arch;
4969 unsigned long machine;
4971 /* If this isn't the right architecture for this backend, and this
4972 isn't the generic backend, fail. */
4973 if (arch != get_elf_backend_data (abfd)->arch
4974 && arch != bfd_arch_unknown
4975 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4978 return bfd_default_set_arch_mach (abfd, arch, machine);
4981 /* Find the function to a particular section and offset,
4982 for error reporting. */
4985 elf_find_function (abfd, section, symbols, offset,
4986 filename_ptr, functionname_ptr)
4987 bfd *abfd ATTRIBUTE_UNUSED;
4991 CONST char **filename_ptr;
4992 CONST char **functionname_ptr;
4994 const char *filename;
5003 for (p = symbols; *p != NULL; p++)
5007 q = (elf_symbol_type *) *p;
5009 if (bfd_get_section (&q->symbol) != section)
5012 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5017 filename = bfd_asymbol_name (&q->symbol);
5021 if (q->symbol.section == section
5022 && q->symbol.value >= low_func
5023 && q->symbol.value <= offset)
5025 func = (asymbol *) q;
5026 low_func = q->symbol.value;
5036 *filename_ptr = filename;
5037 if (functionname_ptr)
5038 *functionname_ptr = bfd_asymbol_name (func);
5043 /* Find the nearest line to a particular section and offset,
5044 for error reporting. */
5047 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5048 filename_ptr, functionname_ptr, line_ptr)
5053 CONST char **filename_ptr;
5054 CONST char **functionname_ptr;
5055 unsigned int *line_ptr;
5059 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5060 filename_ptr, functionname_ptr,
5063 if (!*functionname_ptr)
5064 elf_find_function (abfd, section, symbols, offset,
5065 *filename_ptr ? NULL : filename_ptr,
5071 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5072 filename_ptr, functionname_ptr,
5074 &elf_tdata (abfd)->dwarf2_find_line_info))
5076 if (!*functionname_ptr)
5077 elf_find_function (abfd, section, symbols, offset,
5078 *filename_ptr ? NULL : filename_ptr,
5084 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5085 &found, filename_ptr,
5086 functionname_ptr, line_ptr,
5087 &elf_tdata (abfd)->line_info))
5092 if (symbols == NULL)
5095 if (! elf_find_function (abfd, section, symbols, offset,
5096 filename_ptr, functionname_ptr))
5104 _bfd_elf_sizeof_headers (abfd, reloc)
5110 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5112 ret += get_program_header_size (abfd);
5117 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5122 bfd_size_type count;
5124 Elf_Internal_Shdr *hdr;
5126 if (! abfd->output_has_begun
5127 && ! _bfd_elf_compute_section_file_positions
5128 (abfd, (struct bfd_link_info *) NULL))
5131 hdr = &elf_section_data (section)->this_hdr;
5133 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5135 if (bfd_write (location, 1, count, abfd) != count)
5142 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5143 bfd *abfd ATTRIBUTE_UNUSED;
5144 arelent *cache_ptr ATTRIBUTE_UNUSED;
5145 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5152 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5155 Elf_Internal_Rel *dst;
5161 /* Try to convert a non-ELF reloc into an ELF one. */
5164 _bfd_elf_validate_reloc (abfd, areloc)
5168 /* Check whether we really have an ELF howto. */
5170 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5172 bfd_reloc_code_real_type code;
5173 reloc_howto_type *howto;
5175 /* Alien reloc: Try to determine its type to replace it with an
5176 equivalent ELF reloc. */
5178 if (areloc->howto->pc_relative)
5180 switch (areloc->howto->bitsize)
5183 code = BFD_RELOC_8_PCREL;
5186 code = BFD_RELOC_12_PCREL;
5189 code = BFD_RELOC_16_PCREL;
5192 code = BFD_RELOC_24_PCREL;
5195 code = BFD_RELOC_32_PCREL;
5198 code = BFD_RELOC_64_PCREL;
5204 howto = bfd_reloc_type_lookup (abfd, code);
5206 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5208 if (howto->pcrel_offset)
5209 areloc->addend += areloc->address;
5211 areloc->addend -= areloc->address; /* addend is unsigned!! */
5216 switch (areloc->howto->bitsize)
5222 code = BFD_RELOC_14;
5225 code = BFD_RELOC_16;
5228 code = BFD_RELOC_26;
5231 code = BFD_RELOC_32;
5234 code = BFD_RELOC_64;
5240 howto = bfd_reloc_type_lookup (abfd, code);
5244 areloc->howto = howto;
5252 (*_bfd_error_handler)
5253 (_("%s: unsupported relocation type %s"),
5254 bfd_get_filename (abfd), areloc->howto->name);
5255 bfd_set_error (bfd_error_bad_value);
5260 _bfd_elf_close_and_cleanup (abfd)
5263 if (bfd_get_format (abfd) == bfd_object)
5265 if (elf_shstrtab (abfd) != NULL)
5266 _bfd_stringtab_free (elf_shstrtab (abfd));
5269 return _bfd_generic_close_and_cleanup (abfd);
5272 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5273 in the relocation's offset. Thus we cannot allow any sort of sanity
5274 range-checking to interfere. There is nothing else to do in processing
5277 bfd_reloc_status_type
5278 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5279 bfd *abfd ATTRIBUTE_UNUSED;
5280 arelent *re ATTRIBUTE_UNUSED;
5281 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5282 PTR data ATTRIBUTE_UNUSED;
5283 asection *is ATTRIBUTE_UNUSED;
5284 bfd *obfd ATTRIBUTE_UNUSED;
5285 char **errmsg ATTRIBUTE_UNUSED;
5287 return bfd_reloc_ok;
5290 /* Elf core file support. Much of this only works on native
5291 toolchains, since we rely on knowing the
5292 machine-dependent procfs structure in order to pick
5293 out details about the corefile. */
5295 #ifdef HAVE_SYS_PROCFS_H
5296 # include <sys/procfs.h>
5299 /* Define offsetof for those systems which lack it. */
5302 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5305 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5308 elfcore_make_pid (abfd)
5311 return ((elf_tdata (abfd)->core_lwpid << 16)
5312 + (elf_tdata (abfd)->core_pid));
5315 /* If there isn't a section called NAME, make one, using
5316 data from SECT. Note, this function will generate a
5317 reference to NAME, so you shouldn't deallocate or
5321 elfcore_maybe_make_sect (abfd, name, sect)
5328 if (bfd_get_section_by_name (abfd, name) != NULL)
5331 sect2 = bfd_make_section (abfd, name);
5335 sect2->_raw_size = sect->_raw_size;
5336 sect2->filepos = sect->filepos;
5337 sect2->flags = sect->flags;
5338 sect2->alignment_power = sect->alignment_power;
5342 /* prstatus_t exists on:
5344 linux 2.[01] + glibc
5348 #if defined (HAVE_PRSTATUS_T)
5350 elfcore_grok_prstatus (abfd, note)
5352 Elf_Internal_Note *note;
5360 if (note->descsz == sizeof (prstatus_t))
5364 raw_size = sizeof (prstat.pr_reg);
5365 offset = offsetof (prstatus_t, pr_reg);
5366 memcpy (&prstat, note->descdata, sizeof (prstat));
5368 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5369 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5371 /* pr_who exists on:
5374 pr_who doesn't exist on:
5377 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5378 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5381 #if defined (HAVE_PRSTATUS32_T)
5382 else if (note->descsz == sizeof (prstatus32_t))
5384 /* 64-bit host, 32-bit corefile */
5385 prstatus32_t prstat;
5387 raw_size = sizeof (prstat.pr_reg);
5388 offset = offsetof (prstatus32_t, pr_reg);
5389 memcpy (&prstat, note->descdata, sizeof (prstat));
5391 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5392 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5394 /* pr_who exists on:
5397 pr_who doesn't exist on:
5400 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5401 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5404 #endif /* HAVE_PRSTATUS32_T */
5407 /* Fail - we don't know how to handle any other
5408 note size (ie. data object type). */
5412 /* Make a ".reg/999" section. */
5414 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5415 name = bfd_alloc (abfd, strlen (buf) + 1);
5420 sect = bfd_make_section (abfd, name);
5424 sect->_raw_size = raw_size;
5425 sect->filepos = note->descpos + offset;
5427 sect->flags = SEC_HAS_CONTENTS;
5428 sect->alignment_power = 2;
5430 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5435 #endif /* defined (HAVE_PRSTATUS_T) */
5437 /* Create a pseudosection containing the exact contents of NOTE. This
5438 actually creates up to two pseudosections:
5439 - For the single-threaded case, a section named NAME, unless
5440 such a section already exists.
5441 - For the multi-threaded case, a section named "NAME/PID", where
5442 PID is elfcore_make_pid (abfd).
5443 Both pseudosections have identical contents: the contents of NOTE. */
5446 elfcore_make_note_pseudosection (abfd, name, note)
5449 Elf_Internal_Note *note;
5452 char *threaded_name;
5455 /* Build the section name. */
5457 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5458 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5459 if (threaded_name == NULL)
5461 strcpy (threaded_name, buf);
5463 sect = bfd_make_section (abfd, threaded_name);
5466 sect->_raw_size = note->descsz;
5467 sect->filepos = note->descpos;
5468 sect->flags = SEC_HAS_CONTENTS;
5469 sect->alignment_power = 2;
5471 if (! elfcore_maybe_make_sect (abfd, name, sect))
5477 /* There isn't a consistent prfpregset_t across platforms,
5478 but it doesn't matter, because we don't have to pick this
5479 data structure apart. */
5482 elfcore_grok_prfpreg (abfd, note)
5484 Elf_Internal_Note *note;
5486 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5489 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5490 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5494 elfcore_grok_prxfpreg (abfd, note)
5496 Elf_Internal_Note *note;
5498 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5501 #if defined (HAVE_PRPSINFO_T)
5502 typedef prpsinfo_t elfcore_psinfo_t;
5503 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5504 typedef prpsinfo32_t elfcore_psinfo32_t;
5508 #if defined (HAVE_PSINFO_T)
5509 typedef psinfo_t elfcore_psinfo_t;
5510 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5511 typedef psinfo32_t elfcore_psinfo32_t;
5515 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5517 /* return a malloc'ed copy of a string at START which is at
5518 most MAX bytes long, possibly without a terminating '\0'.
5519 the copy will always have a terminating '\0'. */
5522 elfcore_strndup (abfd, start, max)
5528 char *end = memchr (start, '\0', max);
5536 dup = bfd_alloc (abfd, len + 1);
5540 memcpy (dup, start, len);
5547 elfcore_grok_psinfo (abfd, note)
5549 Elf_Internal_Note *note;
5551 if (note->descsz == sizeof (elfcore_psinfo_t))
5553 elfcore_psinfo_t psinfo;
5555 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5557 elf_tdata (abfd)->core_program
5558 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5560 elf_tdata (abfd)->core_command
5561 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5563 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5564 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5566 /* 64-bit host, 32-bit corefile */
5567 elfcore_psinfo32_t psinfo;
5569 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5571 elf_tdata (abfd)->core_program
5572 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5574 elf_tdata (abfd)->core_command
5575 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5581 /* Fail - we don't know how to handle any other
5582 note size (ie. data object type). */
5586 /* Note that for some reason, a spurious space is tacked
5587 onto the end of the args in some (at least one anyway)
5588 implementations, so strip it off if it exists. */
5591 char *command = elf_tdata (abfd)->core_command;
5592 int n = strlen (command);
5594 if (0 < n && command[n - 1] == ' ')
5595 command[n - 1] = '\0';
5600 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5602 #if defined (HAVE_PSTATUS_T)
5604 elfcore_grok_pstatus (abfd, note)
5606 Elf_Internal_Note *note;
5608 if (note->descsz == sizeof (pstatus_t)
5609 #if defined (HAVE_PXSTATUS_T)
5610 || note->descsz == sizeof (pxstatus_t)
5616 memcpy (&pstat, note->descdata, sizeof (pstat));
5618 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5620 #if defined (HAVE_PSTATUS32_T)
5621 else if (note->descsz == sizeof (pstatus32_t))
5623 /* 64-bit host, 32-bit corefile */
5626 memcpy (&pstat, note->descdata, sizeof (pstat));
5628 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5631 /* Could grab some more details from the "representative"
5632 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5633 NT_LWPSTATUS note, presumably. */
5637 #endif /* defined (HAVE_PSTATUS_T) */
5639 #if defined (HAVE_LWPSTATUS_T)
5641 elfcore_grok_lwpstatus (abfd, note)
5643 Elf_Internal_Note *note;
5645 lwpstatus_t lwpstat;
5650 if (note->descsz != sizeof (lwpstat)
5651 #if defined (HAVE_LWPXSTATUS_T)
5652 && note->descsz != sizeof (lwpxstatus_t)
5657 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5659 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5660 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5662 /* Make a ".reg/999" section. */
5664 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5665 name = bfd_alloc (abfd, strlen (buf) + 1);
5670 sect = bfd_make_section (abfd, name);
5674 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5675 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5676 sect->filepos = note->descpos
5677 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5680 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5681 sect->_raw_size = sizeof (lwpstat.pr_reg);
5682 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5685 sect->flags = SEC_HAS_CONTENTS;
5686 sect->alignment_power = 2;
5688 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5691 /* Make a ".reg2/999" section */
5693 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5694 name = bfd_alloc (abfd, strlen (buf) + 1);
5699 sect = bfd_make_section (abfd, name);
5703 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5704 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5705 sect->filepos = note->descpos
5706 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5709 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5710 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5711 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5714 sect->flags = SEC_HAS_CONTENTS;
5715 sect->alignment_power = 2;
5717 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5722 #endif /* defined (HAVE_LWPSTATUS_T) */
5724 #if defined (HAVE_WIN32_PSTATUS_T)
5726 elfcore_grok_win32pstatus (abfd, note)
5728 Elf_Internal_Note *note;
5733 win32_pstatus_t pstatus;
5735 if (note->descsz < sizeof (pstatus))
5738 memcpy (&pstatus, note->descdata, note->descsz);
5740 switch (pstatus.data_type)
5742 case NOTE_INFO_PROCESS:
5743 /* FIXME: need to add ->core_command. */
5744 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5745 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5748 case NOTE_INFO_THREAD:
5749 /* Make a ".reg/999" section. */
5750 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5752 name = bfd_alloc (abfd, strlen (buf) + 1);
5758 sect = bfd_make_section (abfd, name);
5762 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5763 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5764 data.thread_info.thread_context);
5765 sect->flags = SEC_HAS_CONTENTS;
5766 sect->alignment_power = 2;
5768 if (pstatus.data.thread_info.is_active_thread)
5769 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5773 case NOTE_INFO_MODULE:
5774 /* Make a ".module/xxxxxxxx" section. */
5775 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5777 name = bfd_alloc (abfd, strlen (buf) + 1);
5783 sect = bfd_make_section (abfd, name);
5788 sect->_raw_size = note->descsz;
5789 sect->filepos = note->descpos;
5790 sect->flags = SEC_HAS_CONTENTS;
5791 sect->alignment_power = 2;
5800 #endif /* HAVE_WIN32_PSTATUS_T */
5803 elfcore_grok_note (abfd, note)
5805 Elf_Internal_Note *note;
5812 #if defined (HAVE_PRSTATUS_T)
5814 return elfcore_grok_prstatus (abfd, note);
5817 #if defined (HAVE_PSTATUS_T)
5819 return elfcore_grok_pstatus (abfd, note);
5822 #if defined (HAVE_LWPSTATUS_T)
5824 return elfcore_grok_lwpstatus (abfd, note);
5827 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5828 return elfcore_grok_prfpreg (abfd, note);
5830 #if defined (HAVE_WIN32_PSTATUS_T)
5831 case NT_WIN32PSTATUS:
5832 return elfcore_grok_win32pstatus (abfd, note);
5835 case NT_PRXFPREG: /* Linux SSE extension */
5836 if (note->namesz == 5
5837 && ! strcmp (note->namedata, "LINUX"))
5838 return elfcore_grok_prxfpreg (abfd, note);
5842 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5845 return elfcore_grok_psinfo (abfd, note);
5851 elfcore_read_notes (abfd, offset, size)
5862 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5865 buf = bfd_malloc ((size_t) size);
5869 if (bfd_read (buf, size, 1, abfd) != size)
5877 while (p < buf + size)
5879 /* FIXME: bad alignment assumption. */
5880 Elf_External_Note *xnp = (Elf_External_Note *) p;
5881 Elf_Internal_Note in;
5883 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5885 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5886 in.namedata = xnp->name;
5888 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5889 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5890 in.descpos = offset + (in.descdata - buf);
5892 if (! elfcore_grok_note (abfd, &in))
5895 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5902 /* FIXME: This function is now unnecessary. Callers can just call
5903 bfd_section_from_phdr directly. */
5906 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5908 Elf_Internal_Phdr* phdr;
5911 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5917 /* Providing external access to the ELF program header table. */
5919 /* Return an upper bound on the number of bytes required to store a
5920 copy of ABFD's program header table entries. Return -1 if an error
5921 occurs; bfd_get_error will return an appropriate code. */
5924 bfd_get_elf_phdr_upper_bound (abfd)
5927 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5929 bfd_set_error (bfd_error_wrong_format);
5933 return (elf_elfheader (abfd)->e_phnum
5934 * sizeof (Elf_Internal_Phdr));
5937 /* Copy ABFD's program header table entries to *PHDRS. The entries
5938 will be stored as an array of Elf_Internal_Phdr structures, as
5939 defined in include/elf/internal.h. To find out how large the
5940 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5942 Return the number of program header table entries read, or -1 if an
5943 error occurs; bfd_get_error will return an appropriate code. */
5946 bfd_get_elf_phdrs (abfd, phdrs)
5952 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5954 bfd_set_error (bfd_error_wrong_format);
5958 num_phdrs = elf_elfheader (abfd)->e_phnum;
5959 memcpy (phdrs, elf_tdata (abfd)->phdr,
5960 num_phdrs * sizeof (Elf_Internal_Phdr));