1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
34 /* For sparc64-cross-sparc32. */
43 static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
45 static boolean map_sections_to_segments PARAMS ((bfd *));
46 static int elf_sort_sections PARAMS ((const PTR, const PTR));
47 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
48 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
49 static boolean prep_headers PARAMS ((bfd *));
50 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
51 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
52 static char *elf_read PARAMS ((bfd *, long, unsigned int));
53 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
54 static boolean assign_section_numbers PARAMS ((bfd *));
55 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
56 static boolean elf_map_symbols PARAMS ((bfd *));
57 static bfd_size_type get_program_header_size PARAMS ((bfd *));
58 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (abfd, src, dst)
69 const Elf_External_Verdef *src;
70 Elf_Internal_Verdef *dst;
72 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
73 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
74 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
75 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
76 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
77 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
78 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (abfd, src, dst)
86 const Elf_Internal_Verdef *src;
87 Elf_External_Verdef *dst;
89 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
90 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
91 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
92 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
93 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
94 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
95 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
98 /* Swap in a Verdaux structure. */
101 _bfd_elf_swap_verdaux_in (abfd, src, dst)
103 const Elf_External_Verdaux *src;
104 Elf_Internal_Verdaux *dst;
106 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
107 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
110 /* Swap out a Verdaux structure. */
113 _bfd_elf_swap_verdaux_out (abfd, src, dst)
115 const Elf_Internal_Verdaux *src;
116 Elf_External_Verdaux *dst;
118 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
119 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
122 /* Swap in a Verneed structure. */
125 _bfd_elf_swap_verneed_in (abfd, src, dst)
127 const Elf_External_Verneed *src;
128 Elf_Internal_Verneed *dst;
130 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
131 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
132 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
133 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
134 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
137 /* Swap out a Verneed structure. */
140 _bfd_elf_swap_verneed_out (abfd, src, dst)
142 const Elf_Internal_Verneed *src;
143 Elf_External_Verneed *dst;
145 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
146 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
147 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
148 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
149 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
152 /* Swap in a Vernaux structure. */
155 _bfd_elf_swap_vernaux_in (abfd, src, dst)
157 const Elf_External_Vernaux *src;
158 Elf_Internal_Vernaux *dst;
160 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
161 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
162 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
163 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
164 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
167 /* Swap out a Vernaux structure. */
170 _bfd_elf_swap_vernaux_out (abfd, src, dst)
172 const Elf_Internal_Vernaux *src;
173 Elf_External_Vernaux *dst;
175 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
176 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
177 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
178 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
179 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
182 /* Swap in a Versym structure. */
185 _bfd_elf_swap_versym_in (abfd, src, dst)
187 const Elf_External_Versym *src;
188 Elf_Internal_Versym *dst;
190 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
193 /* Swap out a Versym structure. */
196 _bfd_elf_swap_versym_out (abfd, src, dst)
198 const Elf_Internal_Versym *src;
199 Elf_External_Versym *dst;
201 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
204 /* Standard ELF hash function. Do not change this function; you will
205 cause invalid hash tables to be generated. */
208 bfd_elf_hash (namearg)
211 const unsigned char *name = (const unsigned char *) namearg;
216 while ((ch = *name++) != '\0')
219 if ((g = (h & 0xf0000000)) != 0)
222 /* The ELF ABI says `h &= ~g', but this is equivalent in
223 this case and on some machines one insn instead of two. */
230 /* Read a specified number of bytes at a specified offset in an ELF
231 file, into a newly allocated buffer, and return a pointer to the
235 elf_read (abfd, offset, size)
242 if ((buf = bfd_alloc (abfd, size)) == NULL)
244 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
246 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
248 if (bfd_get_error () != bfd_error_system_call)
249 bfd_set_error (bfd_error_file_truncated);
256 bfd_elf_mkobject (abfd)
259 /* this just does initialization */
260 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
261 elf_tdata (abfd) = (struct elf_obj_tdata *)
262 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
263 if (elf_tdata (abfd) == 0)
265 /* since everything is done at close time, do we need any
272 bfd_elf_mkcorefile (abfd)
275 /* I think this can be done just like an object file. */
276 return bfd_elf_mkobject (abfd);
280 bfd_elf_get_str_section (abfd, shindex)
282 unsigned int shindex;
284 Elf_Internal_Shdr **i_shdrp;
285 char *shstrtab = NULL;
287 unsigned int shstrtabsize;
289 i_shdrp = elf_elfsections (abfd);
290 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
293 shstrtab = (char *) i_shdrp[shindex]->contents;
294 if (shstrtab == NULL)
296 /* No cached one, attempt to read, and cache what we read. */
297 offset = i_shdrp[shindex]->sh_offset;
298 shstrtabsize = i_shdrp[shindex]->sh_size;
299 shstrtab = elf_read (abfd, offset, shstrtabsize);
300 i_shdrp[shindex]->contents = (PTR) shstrtab;
306 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
308 unsigned int shindex;
309 unsigned int strindex;
311 Elf_Internal_Shdr *hdr;
316 hdr = elf_elfsections (abfd)[shindex];
318 if (hdr->contents == NULL
319 && bfd_elf_get_str_section (abfd, shindex) == NULL)
322 if (strindex >= hdr->sh_size)
324 (*_bfd_error_handler)
325 (_("%s: invalid string offset %u >= %lu for section `%s'"),
326 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
327 ((shindex == elf_elfheader(abfd)->e_shstrndx
328 && strindex == hdr->sh_name)
330 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
334 return ((char *) hdr->contents) + strindex;
337 /* Make a BFD section from an ELF section. We store a pointer to the
338 BFD section in the bfd_section field of the header. */
341 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
343 Elf_Internal_Shdr *hdr;
349 if (hdr->bfd_section != NULL)
351 BFD_ASSERT (strcmp (name,
352 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
356 newsect = bfd_make_section_anyway (abfd, name);
360 newsect->filepos = hdr->sh_offset;
362 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
363 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
364 || ! bfd_set_section_alignment (abfd, newsect,
365 bfd_log2 (hdr->sh_addralign)))
368 flags = SEC_NO_FLAGS;
369 if (hdr->sh_type != SHT_NOBITS)
370 flags |= SEC_HAS_CONTENTS;
371 if ((hdr->sh_flags & SHF_ALLOC) != 0)
374 if (hdr->sh_type != SHT_NOBITS)
377 if ((hdr->sh_flags & SHF_WRITE) == 0)
378 flags |= SEC_READONLY;
379 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
381 else if ((flags & SEC_LOAD) != 0)
384 /* The debugging sections appear to be recognized only by name, not
387 const char * debug_sec_names [] =
396 for (i = sizeof (debug_sec_names) / sizeof (debug_sec_names[0]); i--;)
397 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
401 flags |= SEC_DEBUGGING;
404 /* As a GNU extension, if the name begins with .gnu.linkonce, we
405 only link a single copy of the section. This is used to support
406 g++. g++ will emit each template expansion in its own section.
407 The symbols will be defined as weak, so that multiple definitions
408 are permitted. The GNU linker extension is to actually discard
409 all but one of the sections. */
410 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
411 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
413 if (! bfd_set_section_flags (abfd, newsect, flags))
416 if ((flags & SEC_ALLOC) != 0)
418 Elf_Internal_Phdr *phdr;
421 /* Look through the phdrs to see if we need to adjust the lma.
422 If all the p_paddr fields are zero, we ignore them, since
423 some ELF linkers produce such output. */
424 phdr = elf_tdata (abfd)->phdr;
425 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
427 if (phdr->p_paddr != 0)
430 if (i < elf_elfheader (abfd)->e_phnum)
432 phdr = elf_tdata (abfd)->phdr;
433 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
435 if (phdr->p_type == PT_LOAD
436 && phdr->p_vaddr != phdr->p_paddr
437 && phdr->p_vaddr <= hdr->sh_addr
438 && (phdr->p_vaddr + phdr->p_memsz
439 >= hdr->sh_addr + hdr->sh_size)
440 && ((flags & SEC_LOAD) == 0
441 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
442 && (phdr->p_offset + phdr->p_filesz
443 >= hdr->sh_offset + hdr->sh_size))))
445 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
452 hdr->bfd_section = newsect;
453 elf_section_data (newsect)->this_hdr = *hdr;
463 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
466 Helper functions for GDB to locate the string tables.
467 Since BFD hides string tables from callers, GDB needs to use an
468 internal hook to find them. Sun's .stabstr, in particular,
469 isn't even pointed to by the .stab section, so ordinary
470 mechanisms wouldn't work to find it, even if we had some.
473 struct elf_internal_shdr *
474 bfd_elf_find_section (abfd, name)
478 Elf_Internal_Shdr **i_shdrp;
483 i_shdrp = elf_elfsections (abfd);
486 shstrtab = bfd_elf_get_str_section
487 (abfd, elf_elfheader (abfd)->e_shstrndx);
488 if (shstrtab != NULL)
490 max = elf_elfheader (abfd)->e_shnum;
491 for (i = 1; i < max; i++)
492 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
499 const char *const bfd_elf_section_type_names[] = {
500 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
501 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
502 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
505 /* ELF relocs are against symbols. If we are producing relocateable
506 output, and the reloc is against an external symbol, and nothing
507 has given us any additional addend, the resulting reloc will also
508 be against the same symbol. In such a case, we don't want to
509 change anything about the way the reloc is handled, since it will
510 all be done at final link time. Rather than put special case code
511 into bfd_perform_relocation, all the reloc types use this howto
512 function. It just short circuits the reloc if producing
513 relocateable output against an external symbol. */
516 bfd_reloc_status_type
517 bfd_elf_generic_reloc (abfd,
524 bfd *abfd ATTRIBUTE_UNUSED;
525 arelent *reloc_entry;
527 PTR data ATTRIBUTE_UNUSED;
528 asection *input_section;
530 char **error_message ATTRIBUTE_UNUSED;
532 if (output_bfd != (bfd *) NULL
533 && (symbol->flags & BSF_SECTION_SYM) == 0
534 && (! reloc_entry->howto->partial_inplace
535 || reloc_entry->addend == 0))
537 reloc_entry->address += input_section->output_offset;
541 return bfd_reloc_continue;
544 /* Print out the program headers. */
547 _bfd_elf_print_private_bfd_data (abfd, farg)
551 FILE *f = (FILE *) farg;
552 Elf_Internal_Phdr *p;
554 bfd_byte *dynbuf = NULL;
556 p = elf_tdata (abfd)->phdr;
561 fprintf (f, _("\nProgram Header:\n"));
562 c = elf_elfheader (abfd)->e_phnum;
563 for (i = 0; i < c; i++, p++)
570 case PT_NULL: s = "NULL"; break;
571 case PT_LOAD: s = "LOAD"; break;
572 case PT_DYNAMIC: s = "DYNAMIC"; break;
573 case PT_INTERP: s = "INTERP"; break;
574 case PT_NOTE: s = "NOTE"; break;
575 case PT_SHLIB: s = "SHLIB"; break;
576 case PT_PHDR: s = "PHDR"; break;
577 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
579 fprintf (f, "%8s off 0x", s);
580 fprintf_vma (f, p->p_offset);
581 fprintf (f, " vaddr 0x");
582 fprintf_vma (f, p->p_vaddr);
583 fprintf (f, " paddr 0x");
584 fprintf_vma (f, p->p_paddr);
585 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
586 fprintf (f, " filesz 0x");
587 fprintf_vma (f, p->p_filesz);
588 fprintf (f, " memsz 0x");
589 fprintf_vma (f, p->p_memsz);
590 fprintf (f, " flags %c%c%c",
591 (p->p_flags & PF_R) != 0 ? 'r' : '-',
592 (p->p_flags & PF_W) != 0 ? 'w' : '-',
593 (p->p_flags & PF_X) != 0 ? 'x' : '-');
594 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
595 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
600 s = bfd_get_section_by_name (abfd, ".dynamic");
605 bfd_byte *extdyn, *extdynend;
607 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
609 fprintf (f, _("\nDynamic Section:\n"));
611 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
614 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
618 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
621 link = elf_elfsections (abfd)[elfsec]->sh_link;
623 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
624 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
627 extdynend = extdyn + s->_raw_size;
628 for (; extdyn < extdynend; extdyn += extdynsize)
630 Elf_Internal_Dyn dyn;
635 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
637 if (dyn.d_tag == DT_NULL)
644 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
648 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
649 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
650 case DT_PLTGOT: name = "PLTGOT"; break;
651 case DT_HASH: name = "HASH"; break;
652 case DT_STRTAB: name = "STRTAB"; break;
653 case DT_SYMTAB: name = "SYMTAB"; break;
654 case DT_RELA: name = "RELA"; break;
655 case DT_RELASZ: name = "RELASZ"; break;
656 case DT_RELAENT: name = "RELAENT"; break;
657 case DT_STRSZ: name = "STRSZ"; break;
658 case DT_SYMENT: name = "SYMENT"; break;
659 case DT_INIT: name = "INIT"; break;
660 case DT_FINI: name = "FINI"; break;
661 case DT_SONAME: name = "SONAME"; stringp = true; break;
662 case DT_RPATH: name = "RPATH"; stringp = true; break;
663 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
664 case DT_REL: name = "REL"; break;
665 case DT_RELSZ: name = "RELSZ"; break;
666 case DT_RELENT: name = "RELENT"; break;
667 case DT_PLTREL: name = "PLTREL"; break;
668 case DT_DEBUG: name = "DEBUG"; break;
669 case DT_TEXTREL: name = "TEXTREL"; break;
670 case DT_JMPREL: name = "JMPREL"; break;
671 case DT_BIND_NOW: name = "BIND_NOW"; break;
672 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
673 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
674 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
675 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
676 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
677 case DT_FLAGS: name = "FLAGS"; break;
678 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
679 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
680 case DT_CHECKSUM: name = "CHECKSUM"; break;
681 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
682 case DT_MOVEENT: name = "MOVEENT"; break;
683 case DT_MOVESZ: name = "MOVESZ"; break;
684 case DT_FEATURE: name = "FEATURE"; break;
685 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
686 case DT_SYMINSZ: name = "SYMINSZ"; break;
687 case DT_SYMINENT: name = "SYMINENT"; break;
688 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
689 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
690 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
691 case DT_PLTPAD: name = "PLTPAD"; break;
692 case DT_MOVETAB: name = "MOVETAB"; break;
693 case DT_SYMINFO: name = "SYMINFO"; break;
694 case DT_RELACOUNT: name = "RELACOUNT"; break;
695 case DT_RELCOUNT: name = "RELCOUNT"; break;
696 case DT_FLAGS_1: name = "FLAGS_1"; break;
697 case DT_VERSYM: name = "VERSYM"; break;
698 case DT_VERDEF: name = "VERDEF"; break;
699 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
700 case DT_VERNEED: name = "VERNEED"; break;
701 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
702 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
703 case DT_USED: name = "USED"; break;
704 case DT_FILTER: name = "FILTER"; stringp = true; break;
707 fprintf (f, " %-11s ", name);
709 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
714 string = bfd_elf_string_from_elf_section (abfd, link,
718 fprintf (f, "%s", string);
727 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
728 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
730 if (! _bfd_elf_slurp_version_tables (abfd))
734 if (elf_dynverdef (abfd) != 0)
736 Elf_Internal_Verdef *t;
738 fprintf (f, _("\nVersion definitions:\n"));
739 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
741 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
742 t->vd_flags, t->vd_hash, t->vd_nodename);
743 if (t->vd_auxptr->vda_nextptr != NULL)
745 Elf_Internal_Verdaux *a;
748 for (a = t->vd_auxptr->vda_nextptr;
751 fprintf (f, "%s ", a->vda_nodename);
757 if (elf_dynverref (abfd) != 0)
759 Elf_Internal_Verneed *t;
761 fprintf (f, _("\nVersion References:\n"));
762 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
764 Elf_Internal_Vernaux *a;
766 fprintf (f, _(" required from %s:\n"), t->vn_filename);
767 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
768 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
769 a->vna_flags, a->vna_other, a->vna_nodename);
781 /* Display ELF-specific fields of a symbol. */
784 bfd_elf_print_symbol (abfd, filep, symbol, how)
788 bfd_print_symbol_type how;
790 FILE *file = (FILE *) filep;
793 case bfd_print_symbol_name:
794 fprintf (file, "%s", symbol->name);
796 case bfd_print_symbol_more:
797 fprintf (file, "elf ");
798 fprintf_vma (file, symbol->value);
799 fprintf (file, " %lx", (long) symbol->flags);
801 case bfd_print_symbol_all:
803 CONST char *section_name;
804 CONST char *name = NULL;
805 struct elf_backend_data *bed;
806 unsigned char st_other;
808 section_name = symbol->section ? symbol->section->name : "(*none*)";
810 bed = get_elf_backend_data (abfd);
811 if (bed->elf_backend_print_symbol_all)
812 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
817 bfd_print_symbol_vandf ((PTR) file, symbol);
820 fprintf (file, " %s\t", section_name);
821 /* Print the "other" value for a symbol. For common symbols,
822 we've already printed the size; now print the alignment.
823 For other symbols, we have no specified alignment, and
824 we've printed the address; now print the size. */
826 (bfd_is_com_section (symbol->section)
827 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
828 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
830 /* If we have version information, print it. */
831 if (elf_tdata (abfd)->dynversym_section != 0
832 && (elf_tdata (abfd)->dynverdef_section != 0
833 || elf_tdata (abfd)->dynverref_section != 0))
836 const char *version_string;
838 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
842 else if (vernum == 1)
843 version_string = "Base";
844 else if (vernum <= elf_tdata (abfd)->cverdefs)
846 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
849 Elf_Internal_Verneed *t;
852 for (t = elf_tdata (abfd)->verref;
856 Elf_Internal_Vernaux *a;
858 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
860 if (a->vna_other == vernum)
862 version_string = a->vna_nodename;
869 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
870 fprintf (file, " %-11s", version_string);
875 fprintf (file, " (%s)", version_string);
876 for (i = 10 - strlen (version_string); i > 0; --i)
881 /* If the st_other field is not zero, print it. */
882 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
887 case STV_INTERNAL: fprintf (file, " .internal"); break;
888 case STV_HIDDEN: fprintf (file, " .hidden"); break;
889 case STV_PROTECTED: fprintf (file, " .protected"); break;
891 /* Some other non-defined flags are also present, so print
893 fprintf (file, " 0x%02x", (unsigned int) st_other);
896 fprintf (file, " %s", name);
902 /* Create an entry in an ELF linker hash table. */
904 struct bfd_hash_entry *
905 _bfd_elf_link_hash_newfunc (entry, table, string)
906 struct bfd_hash_entry *entry;
907 struct bfd_hash_table *table;
910 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
912 /* Allocate the structure if it has not already been allocated by a
914 if (ret == (struct elf_link_hash_entry *) NULL)
915 ret = ((struct elf_link_hash_entry *)
916 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
917 if (ret == (struct elf_link_hash_entry *) NULL)
918 return (struct bfd_hash_entry *) ret;
920 /* Call the allocation method of the superclass. */
921 ret = ((struct elf_link_hash_entry *)
922 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
924 if (ret != (struct elf_link_hash_entry *) NULL)
926 /* Set local fields. */
930 ret->dynstr_index = 0;
932 ret->got.offset = (bfd_vma) -1;
933 ret->plt.offset = (bfd_vma) -1;
934 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
935 ret->verinfo.verdef = NULL;
936 ret->vtable_entries_used = NULL;
937 ret->vtable_entries_size = 0;
938 ret->vtable_parent = NULL;
939 ret->type = STT_NOTYPE;
941 /* Assume that we have been called by a non-ELF symbol reader.
942 This flag is then reset by the code which reads an ELF input
943 file. This ensures that a symbol created by a non-ELF symbol
944 reader will have the flag set correctly. */
945 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
948 return (struct bfd_hash_entry *) ret;
951 /* Copy data from an indirect symbol to its direct symbol, hiding the
952 old indirect symbol. */
955 _bfd_elf_link_hash_copy_indirect (dir, ind)
956 struct elf_link_hash_entry *dir, *ind;
958 /* Copy down any references that we may have already seen to the
959 symbol which just became indirect. */
961 dir->elf_link_hash_flags |=
962 (ind->elf_link_hash_flags
963 & (ELF_LINK_HASH_REF_DYNAMIC
964 | ELF_LINK_HASH_REF_REGULAR
965 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
966 | ELF_LINK_NON_GOT_REF));
968 /* Copy over the global and procedure linkage table offset entries.
969 These may have been already set up by a check_relocs routine. */
970 if (dir->got.offset == (bfd_vma) -1)
972 dir->got.offset = ind->got.offset;
973 ind->got.offset = (bfd_vma) -1;
975 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
977 if (dir->plt.offset == (bfd_vma) -1)
979 dir->plt.offset = ind->plt.offset;
980 ind->plt.offset = (bfd_vma) -1;
982 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
984 if (dir->dynindx == -1)
986 dir->dynindx = ind->dynindx;
987 dir->dynstr_index = ind->dynstr_index;
989 ind->dynstr_index = 0;
991 BFD_ASSERT (ind->dynindx == -1);
995 _bfd_elf_link_hash_hide_symbol (info, h)
996 struct bfd_link_info *info ATTRIBUTE_UNUSED;
997 struct elf_link_hash_entry *h;
999 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1001 h->plt.offset = (bfd_vma) -1;
1004 /* Initialize an ELF linker hash table. */
1007 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1008 struct elf_link_hash_table *table;
1010 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1011 struct bfd_hash_table *,
1014 table->dynamic_sections_created = false;
1015 table->dynobj = NULL;
1016 /* The first dynamic symbol is a dummy. */
1017 table->dynsymcount = 1;
1018 table->dynstr = NULL;
1019 table->bucketcount = 0;
1020 table->needed = NULL;
1021 table->runpath = NULL;
1023 table->stab_info = NULL;
1024 table->dynlocal = NULL;
1025 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1028 /* Create an ELF linker hash table. */
1030 struct bfd_link_hash_table *
1031 _bfd_elf_link_hash_table_create (abfd)
1034 struct elf_link_hash_table *ret;
1036 ret = ((struct elf_link_hash_table *)
1037 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1038 if (ret == (struct elf_link_hash_table *) NULL)
1041 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1043 bfd_release (abfd, ret);
1050 /* This is a hook for the ELF emulation code in the generic linker to
1051 tell the backend linker what file name to use for the DT_NEEDED
1052 entry for a dynamic object. The generic linker passes name as an
1053 empty string to indicate that no DT_NEEDED entry should be made. */
1056 bfd_elf_set_dt_needed_name (abfd, name)
1060 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1061 && bfd_get_format (abfd) == bfd_object)
1062 elf_dt_name (abfd) = name;
1066 bfd_elf_set_dt_needed_soname (abfd, name)
1070 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1071 && bfd_get_format (abfd) == bfd_object)
1072 elf_dt_soname (abfd) = name;
1075 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1076 the linker ELF emulation code. */
1078 struct bfd_link_needed_list *
1079 bfd_elf_get_needed_list (abfd, info)
1080 bfd *abfd ATTRIBUTE_UNUSED;
1081 struct bfd_link_info *info;
1083 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1085 return elf_hash_table (info)->needed;
1088 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1089 hook for the linker ELF emulation code. */
1091 struct bfd_link_needed_list *
1092 bfd_elf_get_runpath_list (abfd, info)
1093 bfd *abfd ATTRIBUTE_UNUSED;
1094 struct bfd_link_info *info;
1096 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1098 return elf_hash_table (info)->runpath;
1101 /* Get the name actually used for a dynamic object for a link. This
1102 is the SONAME entry if there is one. Otherwise, it is the string
1103 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1106 bfd_elf_get_dt_soname (abfd)
1109 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1110 && bfd_get_format (abfd) == bfd_object)
1111 return elf_dt_name (abfd);
1115 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1116 the ELF linker emulation code. */
1119 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1121 struct bfd_link_needed_list **pneeded;
1124 bfd_byte *dynbuf = NULL;
1127 bfd_byte *extdyn, *extdynend;
1129 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1133 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1134 || bfd_get_format (abfd) != bfd_object)
1137 s = bfd_get_section_by_name (abfd, ".dynamic");
1138 if (s == NULL || s->_raw_size == 0)
1141 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1145 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1149 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1153 link = elf_elfsections (abfd)[elfsec]->sh_link;
1155 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1156 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1159 extdynend = extdyn + s->_raw_size;
1160 for (; extdyn < extdynend; extdyn += extdynsize)
1162 Elf_Internal_Dyn dyn;
1164 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1166 if (dyn.d_tag == DT_NULL)
1169 if (dyn.d_tag == DT_NEEDED)
1172 struct bfd_link_needed_list *l;
1174 string = bfd_elf_string_from_elf_section (abfd, link,
1179 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1200 /* Allocate an ELF string table--force the first byte to be zero. */
1202 struct bfd_strtab_hash *
1203 _bfd_elf_stringtab_init ()
1205 struct bfd_strtab_hash *ret;
1207 ret = _bfd_stringtab_init ();
1212 loc = _bfd_stringtab_add (ret, "", true, false);
1213 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1214 if (loc == (bfd_size_type) -1)
1216 _bfd_stringtab_free (ret);
1223 /* ELF .o/exec file reading */
1225 /* Create a new bfd section from an ELF section header. */
1228 bfd_section_from_shdr (abfd, shindex)
1230 unsigned int shindex;
1232 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1233 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1234 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1237 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1239 switch (hdr->sh_type)
1242 /* Inactive section. Throw it away. */
1245 case SHT_PROGBITS: /* Normal section with contents. */
1246 case SHT_DYNAMIC: /* Dynamic linking information. */
1247 case SHT_NOBITS: /* .bss section. */
1248 case SHT_HASH: /* .hash section. */
1249 case SHT_NOTE: /* .note section. */
1250 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1252 case SHT_SYMTAB: /* A symbol table */
1253 if (elf_onesymtab (abfd) == shindex)
1256 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1257 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1258 elf_onesymtab (abfd) = shindex;
1259 elf_tdata (abfd)->symtab_hdr = *hdr;
1260 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1261 abfd->flags |= HAS_SYMS;
1263 /* Sometimes a shared object will map in the symbol table. If
1264 SHF_ALLOC is set, and this is a shared object, then we also
1265 treat this section as a BFD section. We can not base the
1266 decision purely on SHF_ALLOC, because that flag is sometimes
1267 set in a relocateable object file, which would confuse the
1269 if ((hdr->sh_flags & SHF_ALLOC) != 0
1270 && (abfd->flags & DYNAMIC) != 0
1271 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1276 case SHT_DYNSYM: /* A dynamic symbol table */
1277 if (elf_dynsymtab (abfd) == shindex)
1280 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1281 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1282 elf_dynsymtab (abfd) = shindex;
1283 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1284 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1285 abfd->flags |= HAS_SYMS;
1287 /* Besides being a symbol table, we also treat this as a regular
1288 section, so that objcopy can handle it. */
1289 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1291 case SHT_STRTAB: /* A string table */
1292 if (hdr->bfd_section != NULL)
1294 if (ehdr->e_shstrndx == shindex)
1296 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1297 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1303 for (i = 1; i < ehdr->e_shnum; i++)
1305 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1306 if (hdr2->sh_link == shindex)
1308 if (! bfd_section_from_shdr (abfd, i))
1310 if (elf_onesymtab (abfd) == i)
1312 elf_tdata (abfd)->strtab_hdr = *hdr;
1313 elf_elfsections (abfd)[shindex] =
1314 &elf_tdata (abfd)->strtab_hdr;
1317 if (elf_dynsymtab (abfd) == i)
1319 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1320 elf_elfsections (abfd)[shindex] = hdr =
1321 &elf_tdata (abfd)->dynstrtab_hdr;
1322 /* We also treat this as a regular section, so
1323 that objcopy can handle it. */
1326 #if 0 /* Not handling other string tables specially right now. */
1327 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1328 /* We have a strtab for some random other section. */
1329 newsect = (asection *) hdr2->bfd_section;
1332 hdr->bfd_section = newsect;
1333 hdr2 = &elf_section_data (newsect)->str_hdr;
1335 elf_elfsections (abfd)[shindex] = hdr2;
1341 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1345 /* *These* do a lot of work -- but build no sections! */
1347 asection *target_sect;
1348 Elf_Internal_Shdr *hdr2;
1350 /* Check for a bogus link to avoid crashing. */
1351 if (hdr->sh_link >= ehdr->e_shnum)
1353 ((*_bfd_error_handler)
1354 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1355 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1356 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1359 /* For some incomprehensible reason Oracle distributes
1360 libraries for Solaris in which some of the objects have
1361 bogus sh_link fields. It would be nice if we could just
1362 reject them, but, unfortunately, some people need to use
1363 them. We scan through the section headers; if we find only
1364 one suitable symbol table, we clobber the sh_link to point
1365 to it. I hope this doesn't break anything. */
1366 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1367 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1373 for (scan = 1; scan < ehdr->e_shnum; scan++)
1375 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1376 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1387 hdr->sh_link = found;
1390 /* Get the symbol table. */
1391 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1392 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1395 /* If this reloc section does not use the main symbol table we
1396 don't treat it as a reloc section. BFD can't adequately
1397 represent such a section, so at least for now, we don't
1398 try. We just present it as a normal section. We also
1399 can't use it as a reloc section if it points to the null
1401 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1402 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1404 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1406 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1407 if (target_sect == NULL)
1410 if ((target_sect->flags & SEC_RELOC) == 0
1411 || target_sect->reloc_count == 0)
1412 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1415 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1416 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1417 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1420 elf_elfsections (abfd)[shindex] = hdr2;
1421 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1422 target_sect->flags |= SEC_RELOC;
1423 target_sect->relocation = NULL;
1424 target_sect->rel_filepos = hdr->sh_offset;
1425 /* In the section to which the relocations apply, mark whether
1426 its relocations are of the REL or RELA variety. */
1427 if (hdr->sh_size != 0)
1428 elf_section_data (target_sect)->use_rela_p
1429 = (hdr->sh_type == SHT_RELA);
1430 abfd->flags |= HAS_RELOC;
1435 case SHT_GNU_verdef:
1436 elf_dynverdef (abfd) = shindex;
1437 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1438 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1441 case SHT_GNU_versym:
1442 elf_dynversym (abfd) = shindex;
1443 elf_tdata (abfd)->dynversym_hdr = *hdr;
1444 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1447 case SHT_GNU_verneed:
1448 elf_dynverref (abfd) = shindex;
1449 elf_tdata (abfd)->dynverref_hdr = *hdr;
1450 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1457 /* Check for any processor-specific section types. */
1459 if (bed->elf_backend_section_from_shdr)
1460 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1468 /* Given an ELF section number, retrieve the corresponding BFD
1472 bfd_section_from_elf_index (abfd, index)
1476 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1477 if (index >= elf_elfheader (abfd)->e_shnum)
1479 return elf_elfsections (abfd)[index]->bfd_section;
1483 _bfd_elf_new_section_hook (abfd, sec)
1487 struct bfd_elf_section_data *sdata;
1489 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1492 sec->used_by_bfd = (PTR) sdata;
1494 /* Indicate whether or not this section should use RELA relocations. */
1496 = get_elf_backend_data (abfd)->default_use_rela_p;
1501 /* Create a new bfd section from an ELF program header.
1503 Since program segments have no names, we generate a synthetic name
1504 of the form segment<NUM>, where NUM is generally the index in the
1505 program header table. For segments that are split (see below) we
1506 generate the names segment<NUM>a and segment<NUM>b.
1508 Note that some program segments may have a file size that is different than
1509 (less than) the memory size. All this means is that at execution the
1510 system must allocate the amount of memory specified by the memory size,
1511 but only initialize it with the first "file size" bytes read from the
1512 file. This would occur for example, with program segments consisting
1513 of combined data+bss.
1515 To handle the above situation, this routine generates TWO bfd sections
1516 for the single program segment. The first has the length specified by
1517 the file size of the segment, and the second has the length specified
1518 by the difference between the two sizes. In effect, the segment is split
1519 into it's initialized and uninitialized parts.
1524 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1526 Elf_Internal_Phdr *hdr;
1528 const char *typename;
1535 split = ((hdr->p_memsz > 0)
1536 && (hdr->p_filesz > 0)
1537 && (hdr->p_memsz > hdr->p_filesz));
1538 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1539 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1542 strcpy (name, namebuf);
1543 newsect = bfd_make_section (abfd, name);
1544 if (newsect == NULL)
1546 newsect->vma = hdr->p_vaddr;
1547 newsect->lma = hdr->p_paddr;
1548 newsect->_raw_size = hdr->p_filesz;
1549 newsect->filepos = hdr->p_offset;
1550 newsect->flags |= SEC_HAS_CONTENTS;
1551 if (hdr->p_type == PT_LOAD)
1553 newsect->flags |= SEC_ALLOC;
1554 newsect->flags |= SEC_LOAD;
1555 if (hdr->p_flags & PF_X)
1557 /* FIXME: all we known is that it has execute PERMISSION,
1559 newsect->flags |= SEC_CODE;
1562 if (!(hdr->p_flags & PF_W))
1564 newsect->flags |= SEC_READONLY;
1569 sprintf (namebuf, "%s%db", typename, index);
1570 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1573 strcpy (name, namebuf);
1574 newsect = bfd_make_section (abfd, name);
1575 if (newsect == NULL)
1577 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1578 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1579 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1580 if (hdr->p_type == PT_LOAD)
1582 newsect->flags |= SEC_ALLOC;
1583 if (hdr->p_flags & PF_X)
1584 newsect->flags |= SEC_CODE;
1586 if (!(hdr->p_flags & PF_W))
1587 newsect->flags |= SEC_READONLY;
1594 bfd_section_from_phdr (abfd, hdr, index)
1596 Elf_Internal_Phdr *hdr;
1599 struct elf_backend_data *bed;
1601 switch (hdr->p_type)
1604 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1607 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1610 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1613 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1616 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1618 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1623 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1626 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1629 /* Check for any processor-specific program segment types.
1630 If no handler for them, default to making "segment" sections. */
1631 bed = get_elf_backend_data (abfd);
1632 if (bed->elf_backend_section_from_phdr)
1633 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1635 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1639 /* Initialize REL_HDR, the section-header for new section, containing
1640 relocations against ASECT. If USE_RELA_P is true, we use RELA
1641 relocations; otherwise, we use REL relocations. */
1644 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1646 Elf_Internal_Shdr *rel_hdr;
1651 struct elf_backend_data *bed;
1653 bed = get_elf_backend_data (abfd);
1654 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1657 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1659 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1661 if (rel_hdr->sh_name == (unsigned int) -1)
1663 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1664 rel_hdr->sh_entsize = (use_rela_p
1665 ? bed->s->sizeof_rela
1666 : bed->s->sizeof_rel);
1667 rel_hdr->sh_addralign = bed->s->file_align;
1668 rel_hdr->sh_flags = 0;
1669 rel_hdr->sh_addr = 0;
1670 rel_hdr->sh_size = 0;
1671 rel_hdr->sh_offset = 0;
1676 /* Set up an ELF internal section header for a section. */
1680 elf_fake_sections (abfd, asect, failedptrarg)
1685 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1686 boolean *failedptr = (boolean *) failedptrarg;
1687 Elf_Internal_Shdr *this_hdr;
1691 /* We already failed; just get out of the bfd_map_over_sections
1696 this_hdr = &elf_section_data (asect)->this_hdr;
1698 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1701 if (this_hdr->sh_name == (unsigned long) -1)
1707 this_hdr->sh_flags = 0;
1709 if ((asect->flags & SEC_ALLOC) != 0
1710 || asect->user_set_vma)
1711 this_hdr->sh_addr = asect->vma;
1713 this_hdr->sh_addr = 0;
1715 this_hdr->sh_offset = 0;
1716 this_hdr->sh_size = asect->_raw_size;
1717 this_hdr->sh_link = 0;
1718 this_hdr->sh_addralign = 1 << asect->alignment_power;
1719 /* The sh_entsize and sh_info fields may have been set already by
1720 copy_private_section_data. */
1722 this_hdr->bfd_section = asect;
1723 this_hdr->contents = NULL;
1725 /* FIXME: This should not be based on section names. */
1726 if (strcmp (asect->name, ".dynstr") == 0)
1727 this_hdr->sh_type = SHT_STRTAB;
1728 else if (strcmp (asect->name, ".hash") == 0)
1730 this_hdr->sh_type = SHT_HASH;
1731 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1733 else if (strcmp (asect->name, ".dynsym") == 0)
1735 this_hdr->sh_type = SHT_DYNSYM;
1736 this_hdr->sh_entsize = bed->s->sizeof_sym;
1738 else if (strcmp (asect->name, ".dynamic") == 0)
1740 this_hdr->sh_type = SHT_DYNAMIC;
1741 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1743 else if (strncmp (asect->name, ".rela", 5) == 0
1744 && get_elf_backend_data (abfd)->may_use_rela_p)
1746 this_hdr->sh_type = SHT_RELA;
1747 this_hdr->sh_entsize = bed->s->sizeof_rela;
1749 else if (strncmp (asect->name, ".rel", 4) == 0
1750 && get_elf_backend_data (abfd)->may_use_rel_p)
1752 this_hdr->sh_type = SHT_REL;
1753 this_hdr->sh_entsize = bed->s->sizeof_rel;
1755 else if (strncmp (asect->name, ".note", 5) == 0)
1756 this_hdr->sh_type = SHT_NOTE;
1757 else if (strncmp (asect->name, ".stab", 5) == 0
1758 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1759 this_hdr->sh_type = SHT_STRTAB;
1760 else if (strcmp (asect->name, ".gnu.version") == 0)
1762 this_hdr->sh_type = SHT_GNU_versym;
1763 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1765 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1767 this_hdr->sh_type = SHT_GNU_verdef;
1768 this_hdr->sh_entsize = 0;
1769 /* objcopy or strip will copy over sh_info, but may not set
1770 cverdefs. The linker will set cverdefs, but sh_info will be
1772 if (this_hdr->sh_info == 0)
1773 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1775 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1776 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1778 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1780 this_hdr->sh_type = SHT_GNU_verneed;
1781 this_hdr->sh_entsize = 0;
1782 /* objcopy or strip will copy over sh_info, but may not set
1783 cverrefs. The linker will set cverrefs, but sh_info will be
1785 if (this_hdr->sh_info == 0)
1786 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1788 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1789 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1791 else if ((asect->flags & SEC_ALLOC) != 0
1792 && (asect->flags & SEC_LOAD) != 0)
1793 this_hdr->sh_type = SHT_PROGBITS;
1794 else if ((asect->flags & SEC_ALLOC) != 0
1795 && ((asect->flags & SEC_LOAD) == 0))
1796 this_hdr->sh_type = SHT_NOBITS;
1800 this_hdr->sh_type = SHT_PROGBITS;
1803 if ((asect->flags & SEC_ALLOC) != 0)
1804 this_hdr->sh_flags |= SHF_ALLOC;
1805 if ((asect->flags & SEC_READONLY) == 0)
1806 this_hdr->sh_flags |= SHF_WRITE;
1807 if ((asect->flags & SEC_CODE) != 0)
1808 this_hdr->sh_flags |= SHF_EXECINSTR;
1810 /* Check for processor-specific section types. */
1811 if (bed->elf_backend_fake_sections)
1812 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1814 /* If the section has relocs, set up a section header for the
1815 SHT_REL[A] section. If two relocation sections are required for
1816 this section, it is up to the processor-specific back-end to
1817 create the other. */
1818 if ((asect->flags & SEC_RELOC) != 0
1819 && !_bfd_elf_init_reloc_shdr (abfd,
1820 &elf_section_data (asect)->rel_hdr,
1822 elf_section_data (asect)->use_rela_p))
1826 /* Assign all ELF section numbers. The dummy first section is handled here
1827 too. The link/info pointers for the standard section types are filled
1828 in here too, while we're at it. */
1831 assign_section_numbers (abfd)
1834 struct elf_obj_tdata *t = elf_tdata (abfd);
1836 unsigned int section_number;
1837 Elf_Internal_Shdr **i_shdrp;
1841 for (sec = abfd->sections; sec; sec = sec->next)
1843 struct bfd_elf_section_data *d = elf_section_data (sec);
1845 d->this_idx = section_number++;
1846 if ((sec->flags & SEC_RELOC) == 0)
1849 d->rel_idx = section_number++;
1852 d->rel_idx2 = section_number++;
1857 t->shstrtab_section = section_number++;
1858 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1859 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1861 if (bfd_get_symcount (abfd) > 0)
1863 t->symtab_section = section_number++;
1864 t->strtab_section = section_number++;
1867 elf_elfheader (abfd)->e_shnum = section_number;
1869 /* Set up the list of section header pointers, in agreement with the
1871 i_shdrp = ((Elf_Internal_Shdr **)
1872 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1873 if (i_shdrp == NULL)
1876 i_shdrp[0] = ((Elf_Internal_Shdr *)
1877 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1878 if (i_shdrp[0] == NULL)
1880 bfd_release (abfd, i_shdrp);
1883 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1885 elf_elfsections (abfd) = i_shdrp;
1887 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1888 if (bfd_get_symcount (abfd) > 0)
1890 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1891 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1892 t->symtab_hdr.sh_link = t->strtab_section;
1894 for (sec = abfd->sections; sec; sec = sec->next)
1896 struct bfd_elf_section_data *d = elf_section_data (sec);
1900 i_shdrp[d->this_idx] = &d->this_hdr;
1901 if (d->rel_idx != 0)
1902 i_shdrp[d->rel_idx] = &d->rel_hdr;
1903 if (d->rel_idx2 != 0)
1904 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1906 /* Fill in the sh_link and sh_info fields while we're at it. */
1908 /* sh_link of a reloc section is the section index of the symbol
1909 table. sh_info is the section index of the section to which
1910 the relocation entries apply. */
1911 if (d->rel_idx != 0)
1913 d->rel_hdr.sh_link = t->symtab_section;
1914 d->rel_hdr.sh_info = d->this_idx;
1916 if (d->rel_idx2 != 0)
1918 d->rel_hdr2->sh_link = t->symtab_section;
1919 d->rel_hdr2->sh_info = d->this_idx;
1922 switch (d->this_hdr.sh_type)
1926 /* A reloc section which we are treating as a normal BFD
1927 section. sh_link is the section index of the symbol
1928 table. sh_info is the section index of the section to
1929 which the relocation entries apply. We assume that an
1930 allocated reloc section uses the dynamic symbol table.
1931 FIXME: How can we be sure? */
1932 s = bfd_get_section_by_name (abfd, ".dynsym");
1934 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1936 /* We look up the section the relocs apply to by name. */
1938 if (d->this_hdr.sh_type == SHT_REL)
1942 s = bfd_get_section_by_name (abfd, name);
1944 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1948 /* We assume that a section named .stab*str is a stabs
1949 string section. We look for a section with the same name
1950 but without the trailing ``str'', and set its sh_link
1951 field to point to this section. */
1952 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1953 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1958 len = strlen (sec->name);
1959 alc = (char *) bfd_malloc (len - 2);
1962 strncpy (alc, sec->name, len - 3);
1963 alc[len - 3] = '\0';
1964 s = bfd_get_section_by_name (abfd, alc);
1968 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1970 /* This is a .stab section. */
1971 elf_section_data (s)->this_hdr.sh_entsize =
1972 4 + 2 * bfd_get_arch_size (abfd) / 8;
1979 case SHT_GNU_verneed:
1980 case SHT_GNU_verdef:
1981 /* sh_link is the section header index of the string table
1982 used for the dynamic entries, or the symbol table, or the
1984 s = bfd_get_section_by_name (abfd, ".dynstr");
1986 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1990 case SHT_GNU_versym:
1991 /* sh_link is the section header index of the symbol table
1992 this hash table or version table is for. */
1993 s = bfd_get_section_by_name (abfd, ".dynsym");
1995 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2003 /* Map symbol from it's internal number to the external number, moving
2004 all local symbols to be at the head of the list. */
2007 sym_is_global (abfd, sym)
2011 /* If the backend has a special mapping, use it. */
2012 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2013 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2016 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2017 || bfd_is_und_section (bfd_get_section (sym))
2018 || bfd_is_com_section (bfd_get_section (sym)));
2022 elf_map_symbols (abfd)
2025 int symcount = bfd_get_symcount (abfd);
2026 asymbol **syms = bfd_get_outsymbols (abfd);
2027 asymbol **sect_syms;
2029 int num_globals = 0;
2030 int num_locals2 = 0;
2031 int num_globals2 = 0;
2033 int num_sections = 0;
2040 fprintf (stderr, "elf_map_symbols\n");
2044 /* Add a section symbol for each BFD section. FIXME: Is this really
2046 for (asect = abfd->sections; asect; asect = asect->next)
2048 if (max_index < asect->index)
2049 max_index = asect->index;
2053 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2054 if (sect_syms == NULL)
2056 elf_section_syms (abfd) = sect_syms;
2058 for (idx = 0; idx < symcount; idx++)
2062 if ((sym->flags & BSF_SECTION_SYM) != 0
2069 if (sec->owner != NULL)
2071 if (sec->owner != abfd)
2073 if (sec->output_offset != 0)
2076 sec = sec->output_section;
2078 /* Empty sections in the input files may have had a section
2079 symbol created for them. (See the comment near the end of
2080 _bfd_generic_link_output_symbols in linker.c). If the linker
2081 script discards such sections then we will reach this point.
2082 Since we know that we cannot avoid this case, we detect it
2083 and skip the abort and the assignment to the sect_syms array.
2084 To reproduce this particular case try running the linker
2085 testsuite test ld-scripts/weak.exp for an ELF port that uses
2086 the generic linker. */
2087 if (sec->owner == NULL)
2090 BFD_ASSERT (sec->owner == abfd);
2092 sect_syms[sec->index] = syms[idx];
2097 for (asect = abfd->sections; asect; asect = asect->next)
2099 if (sect_syms[asect->index] != NULL)
2102 sym = bfd_make_empty_symbol (abfd);
2105 sym->the_bfd = abfd;
2106 sym->name = asect->name;
2108 /* Set the flags to 0 to indicate that this one was newly added. */
2110 sym->section = asect;
2111 sect_syms[asect->index] = sym;
2115 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2116 asect->name, (long) asect->vma, asect->index, (long) asect);
2120 /* Classify all of the symbols. */
2121 for (idx = 0; idx < symcount; idx++)
2123 if (!sym_is_global (abfd, syms[idx]))
2128 for (asect = abfd->sections; asect; asect = asect->next)
2130 if (sect_syms[asect->index] != NULL
2131 && sect_syms[asect->index]->flags == 0)
2133 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2134 if (!sym_is_global (abfd, sect_syms[asect->index]))
2138 sect_syms[asect->index]->flags = 0;
2142 /* Now sort the symbols so the local symbols are first. */
2143 new_syms = ((asymbol **)
2145 (num_locals + num_globals) * sizeof (asymbol *)));
2146 if (new_syms == NULL)
2149 for (idx = 0; idx < symcount; idx++)
2151 asymbol *sym = syms[idx];
2154 if (!sym_is_global (abfd, sym))
2157 i = num_locals + num_globals2++;
2159 sym->udata.i = i + 1;
2161 for (asect = abfd->sections; asect; asect = asect->next)
2163 if (sect_syms[asect->index] != NULL
2164 && sect_syms[asect->index]->flags == 0)
2166 asymbol *sym = sect_syms[asect->index];
2169 sym->flags = BSF_SECTION_SYM;
2170 if (!sym_is_global (abfd, sym))
2173 i = num_locals + num_globals2++;
2175 sym->udata.i = i + 1;
2179 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2181 elf_num_locals (abfd) = num_locals;
2182 elf_num_globals (abfd) = num_globals;
2186 /* Align to the maximum file alignment that could be required for any
2187 ELF data structure. */
2189 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2190 static INLINE file_ptr
2191 align_file_position (off, align)
2195 return (off + align - 1) & ~(align - 1);
2198 /* Assign a file position to a section, optionally aligning to the
2199 required section alignment. */
2202 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2203 Elf_Internal_Shdr *i_shdrp;
2211 al = i_shdrp->sh_addralign;
2213 offset = BFD_ALIGN (offset, al);
2215 i_shdrp->sh_offset = offset;
2216 if (i_shdrp->bfd_section != NULL)
2217 i_shdrp->bfd_section->filepos = offset;
2218 if (i_shdrp->sh_type != SHT_NOBITS)
2219 offset += i_shdrp->sh_size;
2223 /* Compute the file positions we are going to put the sections at, and
2224 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2225 is not NULL, this is being called by the ELF backend linker. */
2228 _bfd_elf_compute_section_file_positions (abfd, link_info)
2230 struct bfd_link_info *link_info;
2232 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2234 struct bfd_strtab_hash *strtab;
2235 Elf_Internal_Shdr *shstrtab_hdr;
2237 if (abfd->output_has_begun)
2240 /* Do any elf backend specific processing first. */
2241 if (bed->elf_backend_begin_write_processing)
2242 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2244 if (! prep_headers (abfd))
2247 /* Post process the headers if necessary. */
2248 if (bed->elf_backend_post_process_headers)
2249 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2252 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2256 if (!assign_section_numbers (abfd))
2259 /* The backend linker builds symbol table information itself. */
2260 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2262 /* Non-zero if doing a relocatable link. */
2263 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2265 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2269 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2270 /* sh_name was set in prep_headers. */
2271 shstrtab_hdr->sh_type = SHT_STRTAB;
2272 shstrtab_hdr->sh_flags = 0;
2273 shstrtab_hdr->sh_addr = 0;
2274 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2275 shstrtab_hdr->sh_entsize = 0;
2276 shstrtab_hdr->sh_link = 0;
2277 shstrtab_hdr->sh_info = 0;
2278 /* sh_offset is set in assign_file_positions_except_relocs. */
2279 shstrtab_hdr->sh_addralign = 1;
2281 if (!assign_file_positions_except_relocs (abfd))
2284 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2287 Elf_Internal_Shdr *hdr;
2289 off = elf_tdata (abfd)->next_file_pos;
2291 hdr = &elf_tdata (abfd)->symtab_hdr;
2292 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2294 hdr = &elf_tdata (abfd)->strtab_hdr;
2295 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2297 elf_tdata (abfd)->next_file_pos = off;
2299 /* Now that we know where the .strtab section goes, write it
2301 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2302 || ! _bfd_stringtab_emit (abfd, strtab))
2304 _bfd_stringtab_free (strtab);
2307 abfd->output_has_begun = true;
2312 /* Create a mapping from a set of sections to a program segment. */
2314 static INLINE struct elf_segment_map *
2315 make_mapping (abfd, sections, from, to, phdr)
2317 asection **sections;
2322 struct elf_segment_map *m;
2326 m = ((struct elf_segment_map *)
2328 (sizeof (struct elf_segment_map)
2329 + (to - from - 1) * sizeof (asection *))));
2333 m->p_type = PT_LOAD;
2334 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2335 m->sections[i - from] = *hdrpp;
2336 m->count = to - from;
2338 if (from == 0 && phdr)
2340 /* Include the headers in the first PT_LOAD segment. */
2341 m->includes_filehdr = 1;
2342 m->includes_phdrs = 1;
2348 /* Set up a mapping from BFD sections to program segments. */
2351 map_sections_to_segments (abfd)
2354 asection **sections = NULL;
2358 struct elf_segment_map *mfirst;
2359 struct elf_segment_map **pm;
2360 struct elf_segment_map *m;
2362 unsigned int phdr_index;
2363 bfd_vma maxpagesize;
2365 boolean phdr_in_segment = true;
2369 if (elf_tdata (abfd)->segment_map != NULL)
2372 if (bfd_count_sections (abfd) == 0)
2375 /* Select the allocated sections, and sort them. */
2377 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2378 * sizeof (asection *));
2379 if (sections == NULL)
2383 for (s = abfd->sections; s != NULL; s = s->next)
2385 if ((s->flags & SEC_ALLOC) != 0)
2391 BFD_ASSERT (i <= bfd_count_sections (abfd));
2394 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2396 /* Build the mapping. */
2401 /* If we have a .interp section, then create a PT_PHDR segment for
2402 the program headers and a PT_INTERP segment for the .interp
2404 s = bfd_get_section_by_name (abfd, ".interp");
2405 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2407 m = ((struct elf_segment_map *)
2408 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2412 m->p_type = PT_PHDR;
2413 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2414 m->p_flags = PF_R | PF_X;
2415 m->p_flags_valid = 1;
2416 m->includes_phdrs = 1;
2421 m = ((struct elf_segment_map *)
2422 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2426 m->p_type = PT_INTERP;
2434 /* Look through the sections. We put sections in the same program
2435 segment when the start of the second section can be placed within
2436 a few bytes of the end of the first section. */
2439 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2441 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2443 && (dynsec->flags & SEC_LOAD) == 0)
2446 /* Deal with -Ttext or something similar such that the first section
2447 is not adjacent to the program headers. This is an
2448 approximation, since at this point we don't know exactly how many
2449 program headers we will need. */
2452 bfd_size_type phdr_size;
2454 phdr_size = elf_tdata (abfd)->program_header_size;
2456 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2457 if ((abfd->flags & D_PAGED) == 0
2458 || sections[0]->lma < phdr_size
2459 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2460 phdr_in_segment = false;
2463 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2466 boolean new_segment;
2470 /* See if this section and the last one will fit in the same
2473 if (last_hdr == NULL)
2475 /* If we don't have a segment yet, then we don't need a new
2476 one (we build the last one after this loop). */
2477 new_segment = false;
2479 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2481 /* If this section has a different relation between the
2482 virtual address and the load address, then we need a new
2486 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2487 < BFD_ALIGN (hdr->lma, maxpagesize))
2489 /* If putting this section in this segment would force us to
2490 skip a page in the segment, then we need a new segment. */
2493 else if ((last_hdr->flags & SEC_LOAD) == 0
2494 && (hdr->flags & SEC_LOAD) != 0)
2496 /* We don't want to put a loadable section after a
2497 nonloadable section in the same segment. */
2500 else if ((abfd->flags & D_PAGED) == 0)
2502 /* If the file is not demand paged, which means that we
2503 don't require the sections to be correctly aligned in the
2504 file, then there is no other reason for a new segment. */
2505 new_segment = false;
2508 && (hdr->flags & SEC_READONLY) == 0
2509 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2512 /* We don't want to put a writable section in a read only
2513 segment, unless they are on the same page in memory
2514 anyhow. We already know that the last section does not
2515 bring us past the current section on the page, so the
2516 only case in which the new section is not on the same
2517 page as the previous section is when the previous section
2518 ends precisely on a page boundary. */
2523 /* Otherwise, we can use the same segment. */
2524 new_segment = false;
2529 if ((hdr->flags & SEC_READONLY) == 0)
2535 /* We need a new program segment. We must create a new program
2536 header holding all the sections from phdr_index until hdr. */
2538 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2545 if ((hdr->flags & SEC_READONLY) == 0)
2552 phdr_in_segment = false;
2555 /* Create a final PT_LOAD program segment. */
2556 if (last_hdr != NULL)
2558 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2566 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2569 m = ((struct elf_segment_map *)
2570 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2574 m->p_type = PT_DYNAMIC;
2576 m->sections[0] = dynsec;
2582 /* For each loadable .note section, add a PT_NOTE segment. We don't
2583 use bfd_get_section_by_name, because if we link together
2584 nonloadable .note sections and loadable .note sections, we will
2585 generate two .note sections in the output file. FIXME: Using
2586 names for section types is bogus anyhow. */
2587 for (s = abfd->sections; s != NULL; s = s->next)
2589 if ((s->flags & SEC_LOAD) != 0
2590 && strncmp (s->name, ".note", 5) == 0)
2592 m = ((struct elf_segment_map *)
2593 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2597 m->p_type = PT_NOTE;
2609 elf_tdata (abfd)->segment_map = mfirst;
2613 if (sections != NULL)
2618 /* Sort sections by address. */
2621 elf_sort_sections (arg1, arg2)
2625 const asection *sec1 = *(const asection **) arg1;
2626 const asection *sec2 = *(const asection **) arg2;
2628 /* Sort by LMA first, since this is the address used to
2629 place the section into a segment. */
2630 if (sec1->lma < sec2->lma)
2632 else if (sec1->lma > sec2->lma)
2635 /* Then sort by VMA. Normally the LMA and the VMA will be
2636 the same, and this will do nothing. */
2637 if (sec1->vma < sec2->vma)
2639 else if (sec1->vma > sec2->vma)
2642 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2644 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2649 return sec1->target_index - sec2->target_index;
2659 /* Sort by size, to put zero sized sections before others at the
2662 if (sec1->_raw_size < sec2->_raw_size)
2664 if (sec1->_raw_size > sec2->_raw_size)
2667 return sec1->target_index - sec2->target_index;
2670 /* Assign file positions to the sections based on the mapping from
2671 sections to segments. This function also sets up some fields in
2672 the file header, and writes out the program headers. */
2675 assign_file_positions_for_segments (abfd)
2678 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2680 struct elf_segment_map *m;
2682 Elf_Internal_Phdr *phdrs;
2684 bfd_vma filehdr_vaddr, filehdr_paddr;
2685 bfd_vma phdrs_vaddr, phdrs_paddr;
2686 Elf_Internal_Phdr *p;
2688 if (elf_tdata (abfd)->segment_map == NULL)
2690 if (! map_sections_to_segments (abfd))
2694 if (bed->elf_backend_modify_segment_map)
2696 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2701 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2704 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2705 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2706 elf_elfheader (abfd)->e_phnum = count;
2711 /* If we already counted the number of program segments, make sure
2712 that we allocated enough space. This happens when SIZEOF_HEADERS
2713 is used in a linker script. */
2714 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2715 if (alloc != 0 && count > alloc)
2717 ((*_bfd_error_handler)
2718 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2719 bfd_get_filename (abfd), alloc, count));
2720 bfd_set_error (bfd_error_bad_value);
2727 phdrs = ((Elf_Internal_Phdr *)
2728 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2732 off = bed->s->sizeof_ehdr;
2733 off += alloc * bed->s->sizeof_phdr;
2740 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2747 /* If elf_segment_map is not from map_sections_to_segments, the
2748 sections may not be correctly ordered. */
2750 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2753 p->p_type = m->p_type;
2754 p->p_flags = m->p_flags;
2756 if (p->p_type == PT_LOAD
2758 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2760 if ((abfd->flags & D_PAGED) != 0)
2761 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2764 bfd_size_type align;
2767 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2769 bfd_size_type secalign;
2771 secalign = bfd_get_section_alignment (abfd, *secpp);
2772 if (secalign > align)
2776 off += (m->sections[0]->vma - off) % (1 << align);
2783 p->p_vaddr = m->sections[0]->vma;
2785 if (m->p_paddr_valid)
2786 p->p_paddr = m->p_paddr;
2787 else if (m->count == 0)
2790 p->p_paddr = m->sections[0]->lma;
2792 if (p->p_type == PT_LOAD
2793 && (abfd->flags & D_PAGED) != 0)
2794 p->p_align = bed->maxpagesize;
2795 else if (m->count == 0)
2796 p->p_align = bed->s->file_align;
2804 if (m->includes_filehdr)
2806 if (! m->p_flags_valid)
2809 p->p_filesz = bed->s->sizeof_ehdr;
2810 p->p_memsz = bed->s->sizeof_ehdr;
2813 BFD_ASSERT (p->p_type == PT_LOAD);
2815 if (p->p_vaddr < (bfd_vma) off)
2817 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2818 bfd_get_filename (abfd));
2819 bfd_set_error (bfd_error_bad_value);
2824 if (! m->p_paddr_valid)
2827 if (p->p_type == PT_LOAD)
2829 filehdr_vaddr = p->p_vaddr;
2830 filehdr_paddr = p->p_paddr;
2834 if (m->includes_phdrs)
2836 if (! m->p_flags_valid)
2839 if (m->includes_filehdr)
2841 if (p->p_type == PT_LOAD)
2843 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2844 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2849 p->p_offset = bed->s->sizeof_ehdr;
2853 BFD_ASSERT (p->p_type == PT_LOAD);
2854 p->p_vaddr -= off - p->p_offset;
2855 if (! m->p_paddr_valid)
2856 p->p_paddr -= off - p->p_offset;
2859 if (p->p_type == PT_LOAD)
2861 phdrs_vaddr = p->p_vaddr;
2862 phdrs_paddr = p->p_paddr;
2865 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2868 p->p_filesz += alloc * bed->s->sizeof_phdr;
2869 p->p_memsz += alloc * bed->s->sizeof_phdr;
2872 if (p->p_type == PT_LOAD
2873 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2875 if (! m->includes_filehdr && ! m->includes_phdrs)
2881 adjust = off - (p->p_offset + p->p_filesz);
2882 p->p_filesz += adjust;
2883 p->p_memsz += adjust;
2889 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2893 bfd_size_type align;
2897 align = 1 << bfd_get_section_alignment (abfd, sec);
2899 /* The section may have artificial alignment forced by a
2900 link script. Notice this case by the gap between the
2901 cumulative phdr vma and the section's vma. */
2902 if (p->p_vaddr + p->p_memsz < sec->vma)
2904 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2906 p->p_memsz += adjust;
2909 if ((flags & SEC_LOAD) != 0)
2910 p->p_filesz += adjust;
2913 if (p->p_type == PT_LOAD)
2915 bfd_signed_vma adjust;
2917 if ((flags & SEC_LOAD) != 0)
2919 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2923 else if ((flags & SEC_ALLOC) != 0)
2925 /* The section VMA must equal the file position
2926 modulo the page size. FIXME: I'm not sure if
2927 this adjustment is really necessary. We used to
2928 not have the SEC_LOAD case just above, and then
2929 this was necessary, but now I'm not sure. */
2930 if ((abfd->flags & D_PAGED) != 0)
2931 adjust = (sec->vma - voff) % bed->maxpagesize;
2933 adjust = (sec->vma - voff) % align;
2942 (* _bfd_error_handler)
2943 (_("Error: First section in segment (%s) starts at 0x%x"),
2944 bfd_section_name (abfd, sec), sec->lma);
2945 (* _bfd_error_handler)
2946 (_(" whereas segment starts at 0x%x"),
2951 p->p_memsz += adjust;
2954 if ((flags & SEC_LOAD) != 0)
2955 p->p_filesz += adjust;
2960 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2961 used in a linker script we may have a section with
2962 SEC_LOAD clear but which is supposed to have
2964 if ((flags & SEC_LOAD) != 0
2965 || (flags & SEC_HAS_CONTENTS) != 0)
2966 off += sec->_raw_size;
2968 if ((flags & SEC_ALLOC) != 0)
2969 voff += sec->_raw_size;
2972 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2974 /* The actual "note" segment has i == 0.
2975 This is the one that actually contains everything. */
2979 p->p_filesz = sec->_raw_size;
2980 off += sec->_raw_size;
2985 /* Fake sections -- don't need to be written. */
2988 flags = sec->flags = 0;
2995 p->p_memsz += sec->_raw_size;
2997 if ((flags & SEC_LOAD) != 0)
2998 p->p_filesz += sec->_raw_size;
3000 if (align > p->p_align
3001 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3005 if (! m->p_flags_valid)
3008 if ((flags & SEC_CODE) != 0)
3010 if ((flags & SEC_READONLY) == 0)
3016 /* Now that we have set the section file positions, we can set up
3017 the file positions for the non PT_LOAD segments. */
3018 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3022 if (p->p_type != PT_LOAD && m->count > 0)
3024 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3025 p->p_offset = m->sections[0]->filepos;
3029 if (m->includes_filehdr)
3031 p->p_vaddr = filehdr_vaddr;
3032 if (! m->p_paddr_valid)
3033 p->p_paddr = filehdr_paddr;
3035 else if (m->includes_phdrs)
3037 p->p_vaddr = phdrs_vaddr;
3038 if (! m->p_paddr_valid)
3039 p->p_paddr = phdrs_paddr;
3044 /* Clear out any program headers we allocated but did not use. */
3045 for (; count < alloc; count++, p++)
3047 memset (p, 0, sizeof *p);
3048 p->p_type = PT_NULL;
3051 elf_tdata (abfd)->phdr = phdrs;
3053 elf_tdata (abfd)->next_file_pos = off;
3055 /* Write out the program headers. */
3056 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3057 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3063 /* Get the size of the program header.
3065 If this is called by the linker before any of the section VMA's are set, it
3066 can't calculate the correct value for a strange memory layout. This only
3067 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3068 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3069 data segment (exclusive of .interp and .dynamic).
3071 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3072 will be two segments. */
3074 static bfd_size_type
3075 get_program_header_size (abfd)
3080 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3082 /* We can't return a different result each time we're called. */
3083 if (elf_tdata (abfd)->program_header_size != 0)
3084 return elf_tdata (abfd)->program_header_size;
3086 if (elf_tdata (abfd)->segment_map != NULL)
3088 struct elf_segment_map *m;
3091 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3093 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3094 return elf_tdata (abfd)->program_header_size;
3097 /* Assume we will need exactly two PT_LOAD segments: one for text
3098 and one for data. */
3101 s = bfd_get_section_by_name (abfd, ".interp");
3102 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3104 /* If we have a loadable interpreter section, we need a
3105 PT_INTERP segment. In this case, assume we also need a
3106 PT_PHDR segment, although that may not be true for all
3111 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3113 /* We need a PT_DYNAMIC segment. */
3117 for (s = abfd->sections; s != NULL; s = s->next)
3119 if ((s->flags & SEC_LOAD) != 0
3120 && strncmp (s->name, ".note", 5) == 0)
3122 /* We need a PT_NOTE segment. */
3127 /* Let the backend count up any program headers it might need. */
3128 if (bed->elf_backend_additional_program_headers)
3132 a = (*bed->elf_backend_additional_program_headers) (abfd);
3138 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3139 return elf_tdata (abfd)->program_header_size;
3142 /* Work out the file positions of all the sections. This is called by
3143 _bfd_elf_compute_section_file_positions. All the section sizes and
3144 VMAs must be known before this is called.
3146 We do not consider reloc sections at this point, unless they form
3147 part of the loadable image. Reloc sections are assigned file
3148 positions in assign_file_positions_for_relocs, which is called by
3149 write_object_contents and final_link.
3151 We also don't set the positions of the .symtab and .strtab here. */
3154 assign_file_positions_except_relocs (abfd)
3157 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3158 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3159 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3161 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3163 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3164 && bfd_get_format (abfd) != bfd_core)
3166 Elf_Internal_Shdr **hdrpp;
3169 /* Start after the ELF header. */
3170 off = i_ehdrp->e_ehsize;
3172 /* We are not creating an executable, which means that we are
3173 not creating a program header, and that the actual order of
3174 the sections in the file is unimportant. */
3175 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3177 Elf_Internal_Shdr *hdr;
3180 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3182 hdr->sh_offset = -1;
3185 if (i == tdata->symtab_section
3186 || i == tdata->strtab_section)
3188 hdr->sh_offset = -1;
3192 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3198 Elf_Internal_Shdr **hdrpp;
3200 /* Assign file positions for the loaded sections based on the
3201 assignment of sections to segments. */
3202 if (! assign_file_positions_for_segments (abfd))
3205 /* Assign file positions for the other sections. */
3207 off = elf_tdata (abfd)->next_file_pos;
3208 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3210 Elf_Internal_Shdr *hdr;
3213 if (hdr->bfd_section != NULL
3214 && hdr->bfd_section->filepos != 0)
3215 hdr->sh_offset = hdr->bfd_section->filepos;
3216 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3218 ((*_bfd_error_handler)
3219 (_("%s: warning: allocated section `%s' not in segment"),
3220 bfd_get_filename (abfd),
3221 (hdr->bfd_section == NULL
3223 : hdr->bfd_section->name)));
3224 if ((abfd->flags & D_PAGED) != 0)
3225 off += (hdr->sh_addr - off) % bed->maxpagesize;
3227 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3228 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3231 else if (hdr->sh_type == SHT_REL
3232 || hdr->sh_type == SHT_RELA
3233 || hdr == i_shdrpp[tdata->symtab_section]
3234 || hdr == i_shdrpp[tdata->strtab_section])
3235 hdr->sh_offset = -1;
3237 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3241 /* Place the section headers. */
3242 off = align_file_position (off, bed->s->file_align);
3243 i_ehdrp->e_shoff = off;
3244 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3246 elf_tdata (abfd)->next_file_pos = off;
3255 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3256 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3257 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3259 struct bfd_strtab_hash *shstrtab;
3260 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3262 i_ehdrp = elf_elfheader (abfd);
3263 i_shdrp = elf_elfsections (abfd);
3265 shstrtab = _bfd_elf_stringtab_init ();
3266 if (shstrtab == NULL)
3269 elf_shstrtab (abfd) = shstrtab;
3271 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3272 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3273 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3274 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3276 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3277 i_ehdrp->e_ident[EI_DATA] =
3278 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3279 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3281 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3282 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3284 for (count = EI_PAD; count < EI_NIDENT; count++)
3285 i_ehdrp->e_ident[count] = 0;
3287 if ((abfd->flags & DYNAMIC) != 0)
3288 i_ehdrp->e_type = ET_DYN;
3289 else if ((abfd->flags & EXEC_P) != 0)
3290 i_ehdrp->e_type = ET_EXEC;
3291 else if (bfd_get_format (abfd) == bfd_core)
3292 i_ehdrp->e_type = ET_CORE;
3294 i_ehdrp->e_type = ET_REL;
3296 switch (bfd_get_arch (abfd))
3298 case bfd_arch_unknown:
3299 i_ehdrp->e_machine = EM_NONE;
3301 case bfd_arch_sparc:
3302 if (bfd_get_arch_size (abfd) == 64)
3303 i_ehdrp->e_machine = EM_SPARCV9;
3305 i_ehdrp->e_machine = EM_SPARC;
3308 i_ehdrp->e_machine = EM_S370;
3311 i_ehdrp->e_machine = EM_386;
3314 i_ehdrp->e_machine = EM_IA_64;
3316 case bfd_arch_m68hc11:
3317 i_ehdrp->e_machine = EM_68HC11;
3319 case bfd_arch_m68hc12:
3320 i_ehdrp->e_machine = EM_68HC12;
3323 i_ehdrp->e_machine = EM_68K;
3326 i_ehdrp->e_machine = EM_88K;
3329 i_ehdrp->e_machine = EM_860;
3332 i_ehdrp->e_machine = EM_960;
3334 case bfd_arch_mips: /* MIPS Rxxxx */
3335 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3338 i_ehdrp->e_machine = EM_PARISC;
3340 case bfd_arch_powerpc:
3341 i_ehdrp->e_machine = EM_PPC;
3343 case bfd_arch_alpha:
3344 i_ehdrp->e_machine = EM_ALPHA;
3347 i_ehdrp->e_machine = EM_SH;
3350 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3353 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3356 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3358 case bfd_arch_mcore:
3359 i_ehdrp->e_machine = EM_MCORE;
3362 i_ehdrp->e_machine = EM_AVR;
3365 switch (bfd_get_mach (abfd))
3368 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3372 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3375 i_ehdrp->e_machine = EM_ARM;
3378 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3380 case bfd_arch_mn10200:
3381 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3383 case bfd_arch_mn10300:
3384 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3387 i_ehdrp->e_machine = EM_PJ;
3390 i_ehdrp->e_machine = EM_CRIS;
3392 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3394 i_ehdrp->e_machine = EM_NONE;
3396 i_ehdrp->e_version = bed->s->ev_current;
3397 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3399 /* no program header, for now. */
3400 i_ehdrp->e_phoff = 0;
3401 i_ehdrp->e_phentsize = 0;
3402 i_ehdrp->e_phnum = 0;
3404 /* each bfd section is section header entry */
3405 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3406 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3408 /* if we're building an executable, we'll need a program header table */
3409 if (abfd->flags & EXEC_P)
3411 /* it all happens later */
3413 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3415 /* elf_build_phdrs() returns a (NULL-terminated) array of
3416 Elf_Internal_Phdrs */
3417 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3418 i_ehdrp->e_phoff = outbase;
3419 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3424 i_ehdrp->e_phentsize = 0;
3426 i_ehdrp->e_phoff = 0;
3429 elf_tdata (abfd)->symtab_hdr.sh_name =
3430 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3431 elf_tdata (abfd)->strtab_hdr.sh_name =
3432 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3433 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3434 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3435 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3436 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3437 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3443 /* Assign file positions for all the reloc sections which are not part
3444 of the loadable file image. */
3447 _bfd_elf_assign_file_positions_for_relocs (abfd)
3452 Elf_Internal_Shdr **shdrpp;
3454 off = elf_tdata (abfd)->next_file_pos;
3456 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3457 i < elf_elfheader (abfd)->e_shnum;
3460 Elf_Internal_Shdr *shdrp;
3463 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3464 && shdrp->sh_offset == -1)
3465 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3468 elf_tdata (abfd)->next_file_pos = off;
3472 _bfd_elf_write_object_contents (abfd)
3475 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3476 Elf_Internal_Ehdr *i_ehdrp;
3477 Elf_Internal_Shdr **i_shdrp;
3481 if (! abfd->output_has_begun
3482 && ! _bfd_elf_compute_section_file_positions
3483 (abfd, (struct bfd_link_info *) NULL))
3486 i_shdrp = elf_elfsections (abfd);
3487 i_ehdrp = elf_elfheader (abfd);
3490 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3494 _bfd_elf_assign_file_positions_for_relocs (abfd);
3496 /* After writing the headers, we need to write the sections too... */
3497 for (count = 1; count < i_ehdrp->e_shnum; count++)
3499 if (bed->elf_backend_section_processing)
3500 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3501 if (i_shdrp[count]->contents)
3503 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3504 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3506 != i_shdrp[count]->sh_size))
3511 /* Write out the section header names. */
3512 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3513 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3516 if (bed->elf_backend_final_write_processing)
3517 (*bed->elf_backend_final_write_processing) (abfd,
3518 elf_tdata (abfd)->linker);
3520 return bed->s->write_shdrs_and_ehdr (abfd);
3524 _bfd_elf_write_corefile_contents (abfd)
3527 /* Hopefully this can be done just like an object file. */
3528 return _bfd_elf_write_object_contents (abfd);
3530 /* given a section, search the header to find them... */
3532 _bfd_elf_section_from_bfd_section (abfd, asect)
3536 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3537 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3539 Elf_Internal_Shdr *hdr;
3540 int maxindex = elf_elfheader (abfd)->e_shnum;
3542 for (index = 0; index < maxindex; index++)
3544 hdr = i_shdrp[index];
3545 if (hdr->bfd_section == asect)
3549 if (bed->elf_backend_section_from_bfd_section)
3551 for (index = 0; index < maxindex; index++)
3555 hdr = i_shdrp[index];
3557 if ((*bed->elf_backend_section_from_bfd_section)
3558 (abfd, hdr, asect, &retval))
3563 if (bfd_is_abs_section (asect))
3565 if (bfd_is_com_section (asect))
3567 if (bfd_is_und_section (asect))
3570 bfd_set_error (bfd_error_nonrepresentable_section);
3575 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3579 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3581 asymbol **asym_ptr_ptr;
3583 asymbol *asym_ptr = *asym_ptr_ptr;
3585 flagword flags = asym_ptr->flags;
3587 /* When gas creates relocations against local labels, it creates its
3588 own symbol for the section, but does put the symbol into the
3589 symbol chain, so udata is 0. When the linker is generating
3590 relocatable output, this section symbol may be for one of the
3591 input sections rather than the output section. */
3592 if (asym_ptr->udata.i == 0
3593 && (flags & BSF_SECTION_SYM)
3594 && asym_ptr->section)
3598 if (asym_ptr->section->output_section != NULL)
3599 indx = asym_ptr->section->output_section->index;
3601 indx = asym_ptr->section->index;
3602 if (elf_section_syms (abfd)[indx])
3603 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3606 idx = asym_ptr->udata.i;
3610 /* This case can occur when using --strip-symbol on a symbol
3611 which is used in a relocation entry. */
3612 (*_bfd_error_handler)
3613 (_("%s: symbol `%s' required but not present"),
3614 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3615 bfd_set_error (bfd_error_no_symbols);
3622 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3623 (long) asym_ptr, asym_ptr->name, idx, flags,
3624 elf_symbol_flags (flags));
3632 /* Copy private BFD data. This copies any program header information. */
3635 copy_private_bfd_data (ibfd, obfd)
3639 Elf_Internal_Ehdr * iehdr;
3640 struct elf_segment_map * map;
3641 struct elf_segment_map * map_first;
3642 struct elf_segment_map ** pointer_to_map;
3643 Elf_Internal_Phdr * segment;
3646 unsigned int num_segments;
3647 boolean phdr_included = false;
3648 bfd_vma maxpagesize;
3649 struct elf_segment_map * phdr_adjust_seg = NULL;
3650 unsigned int phdr_adjust_num = 0;
3652 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3653 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3656 if (elf_tdata (ibfd)->phdr == NULL)
3659 iehdr = elf_elfheader (ibfd);
3662 pointer_to_map = & map_first;
3664 num_segments = elf_elfheader (ibfd)->e_phnum;
3665 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3667 /* Returns the end address of the segment + 1. */
3668 #define SEGMENT_END(segment, start) \
3669 (start + (segment->p_memsz > segment->p_filesz \
3670 ? segment->p_memsz : segment->p_filesz))
3672 /* Returns true if the given section is contained within
3673 the given segment. VMA addresses are compared. */
3674 #define IS_CONTAINED_BY_VMA(section, segment) \
3675 (section->vma >= segment->p_vaddr \
3676 && (section->vma + section->_raw_size) \
3677 <= (SEGMENT_END (segment, segment->p_vaddr)))
3679 /* Returns true if the given section is contained within
3680 the given segment. LMA addresses are compared. */
3681 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3682 (section->lma >= base \
3683 && (section->lma + section->_raw_size) \
3684 <= SEGMENT_END (segment, base))
3686 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3687 #define IS_COREFILE_NOTE(p, s) \
3688 (p->p_type == PT_NOTE \
3689 && bfd_get_format (ibfd) == bfd_core \
3690 && s->vma == 0 && s->lma == 0 \
3691 && (bfd_vma) s->filepos >= p->p_offset \
3692 && (bfd_vma) s->filepos + s->_raw_size \
3693 <= p->p_offset + p->p_filesz)
3695 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3696 linker, which generates a PT_INTERP section with p_vaddr and
3697 p_memsz set to 0. */
3698 #define IS_SOLARIS_PT_INTERP(p, s) \
3700 && p->p_filesz > 0 \
3701 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3702 && s->_raw_size > 0 \
3703 && (bfd_vma) s->filepos >= p->p_offset \
3704 && ((bfd_vma) s->filepos + s->_raw_size \
3705 <= p->p_offset + p->p_filesz))
3707 /* Decide if the given section should be included in the given segment.
3708 A section will be included if:
3709 1. It is within the address space of the segment,
3710 2. It is an allocated segment,
3711 3. There is an output section associated with it,
3712 4. The section has not already been allocated to a previous segment. */
3713 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3714 ((((IS_CONTAINED_BY_VMA (section, segment) \
3715 || IS_SOLARIS_PT_INTERP (segment, section)) \
3716 && (section->flags & SEC_ALLOC) != 0) \
3717 || IS_COREFILE_NOTE (segment, section)) \
3718 && section->output_section != NULL \
3719 && section->segment_mark == false)
3721 /* Returns true iff seg1 starts after the end of seg2. */
3722 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3723 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3725 /* Returns true iff seg1 and seg2 overlap. */
3726 #define SEGMENT_OVERLAPS(seg1, seg2) \
3727 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3729 /* Initialise the segment mark field. */
3730 for (section = ibfd->sections; section != NULL; section = section->next)
3731 section->segment_mark = false;
3733 /* Scan through the segments specified in the program header
3734 of the input BFD. For this first scan we look for overlaps
3735 in the loadable segments. These can be created by wierd
3736 parameters to objcopy. */
3737 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3742 Elf_Internal_Phdr * segment2;
3744 if (segment->p_type != PT_LOAD)
3747 /* Determine if this segment overlaps any previous segments. */
3748 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j ++, segment2 ++)
3750 bfd_signed_vma extra_length;
3752 if (segment2->p_type != PT_LOAD
3753 || ! SEGMENT_OVERLAPS (segment, segment2))
3756 /* Merge the two segments together. */
3757 if (segment2->p_vaddr < segment->p_vaddr)
3759 /* Extend SEGMENT2 to include SEGMENT and then delete SEGMENT. */
3761 SEGMENT_END (segment, segment->p_vaddr)
3762 - SEGMENT_END (segment2, segment2->p_vaddr);
3764 if (extra_length > 0)
3766 segment2->p_memsz += extra_length;
3767 segment2->p_filesz += extra_length;
3770 segment->p_type = PT_NULL;
3772 /* Since we have deleted P we must restart the outer loop. */
3774 segment = elf_tdata (ibfd)->phdr;
3779 /* Extend SEGMENT to include SEGMENT2 and then delete SEGMENT2. */
3781 SEGMENT_END (segment2, segment2->p_vaddr)
3782 - SEGMENT_END (segment, segment->p_vaddr);
3784 if (extra_length > 0)
3786 segment->p_memsz += extra_length;
3787 segment->p_filesz += extra_length;
3790 segment2->p_type = PT_NULL;
3795 /* The second scan attempts to assign sections to segments. */
3796 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3800 unsigned int section_count;
3801 asection ** sections;
3802 asection * output_section;
3804 bfd_vma matching_lma;
3805 bfd_vma suggested_lma;
3808 if (segment->p_type == PT_NULL)
3811 /* Compute how many sections might be placed into this segment. */
3813 for (section = ibfd->sections; section != NULL; section = section->next)
3814 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3817 /* Allocate a segment map big enough to contain all of the
3818 sections we have selected. */
3819 map = ((struct elf_segment_map *)
3821 (sizeof (struct elf_segment_map)
3822 + ((size_t) section_count - 1) * sizeof (asection *))));
3826 /* Initialise the fields of the segment map. Default to
3827 using the physical address of the segment in the input BFD. */
3829 map->p_type = segment->p_type;
3830 map->p_flags = segment->p_flags;
3831 map->p_flags_valid = 1;
3832 map->p_paddr = segment->p_paddr;
3833 map->p_paddr_valid = 1;
3835 /* Determine if this segment contains the ELF file header
3836 and if it contains the program headers themselves. */
3837 map->includes_filehdr = (segment->p_offset == 0
3838 && segment->p_filesz >= iehdr->e_ehsize);
3840 map->includes_phdrs = 0;
3842 if (! phdr_included || segment->p_type != PT_LOAD)
3844 map->includes_phdrs =
3845 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3846 && (segment->p_offset + segment->p_filesz
3847 >= ((bfd_vma) iehdr->e_phoff
3848 + iehdr->e_phnum * iehdr->e_phentsize)));
3850 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3851 phdr_included = true;
3854 if (section_count == 0)
3856 /* Special segments, such as the PT_PHDR segment, may contain
3857 no sections, but ordinary, loadable segments should contain
3859 if (segment->p_type == PT_LOAD)
3861 (_("%s: warning: Empty loadable segment detected\n"),
3862 bfd_get_filename (ibfd));
3865 * pointer_to_map = map;
3866 pointer_to_map = & map->next;
3871 /* Now scan the sections in the input BFD again and attempt
3872 to add their corresponding output sections to the segment map.
3873 The problem here is how to handle an output section which has
3874 been moved (ie had its LMA changed). There are four possibilities:
3876 1. None of the sections have been moved.
3877 In this case we can continue to use the segment LMA from the
3880 2. All of the sections have been moved by the same amount.
3881 In this case we can change the segment's LMA to match the LMA
3882 of the first section.
3884 3. Some of the sections have been moved, others have not.
3885 In this case those sections which have not been moved can be
3886 placed in the current segment which will have to have its size,
3887 and possibly its LMA changed, and a new segment or segments will
3888 have to be created to contain the other sections.
3890 4. The sections have been moved, but not be the same amount.
3891 In this case we can change the segment's LMA to match the LMA
3892 of the first section and we will have to create a new segment
3893 or segments to contain the other sections.
3895 In order to save time, we allocate an array to hold the section
3896 pointers that we are interested in. As these sections get assigned
3897 to a segment, they are removed from this array. */
3899 sections = (asection **) bfd_malloc
3900 (sizeof (asection *) * section_count);
3901 if (sections == NULL)
3904 /* Step One: Scan for segment vs section LMA conflicts.
3905 Also add the sections to the section array allocated above.
3906 Also add the sections to the current segment. In the common
3907 case, where the sections have not been moved, this means that
3908 we have completely filled the segment, and there is nothing
3914 for (j = 0, section = ibfd->sections;
3916 section = section->next)
3918 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3920 output_section = section->output_section;
3922 sections[j ++] = section;
3924 /* The Solaris native linker always sets p_paddr to 0.
3925 We try to catch that case here, and set it to the
3927 if (segment->p_paddr == 0
3928 && segment->p_vaddr != 0
3930 && output_section->lma != 0
3931 && (output_section->vma == (segment->p_vaddr
3932 + (map->includes_filehdr
3935 + (map->includes_phdrs
3936 ? iehdr->e_phnum * iehdr->e_phentsize
3938 map->p_paddr = segment->p_vaddr;
3940 /* Match up the physical address of the segment with the
3941 LMA address of the output section. */
3942 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3943 || IS_COREFILE_NOTE (segment, section))
3945 if (matching_lma == 0)
3946 matching_lma = output_section->lma;
3948 /* We assume that if the section fits within the segment
3949 then it does not overlap any other section within that
3951 map->sections[isec ++] = output_section;
3953 else if (suggested_lma == 0)
3954 suggested_lma = output_section->lma;
3958 BFD_ASSERT (j == section_count);
3960 /* Step Two: Adjust the physical address of the current segment,
3962 if (isec == section_count)
3964 /* All of the sections fitted within the segment as currently
3965 specified. This is the default case. Add the segment to
3966 the list of built segments and carry on to process the next
3967 program header in the input BFD. */
3968 map->count = section_count;
3969 * pointer_to_map = map;
3970 pointer_to_map = & map->next;
3977 if (matching_lma != 0)
3979 /* At least one section fits inside the current segment.
3980 Keep it, but modify its physical address to match the
3981 LMA of the first section that fitted. */
3982 map->p_paddr = matching_lma;
3986 /* None of the sections fitted inside the current segment.
3987 Change the current segment's physical address to match
3988 the LMA of the first section. */
3989 map->p_paddr = suggested_lma;
3992 /* Offset the segment physical address from the lma
3993 to allow for space taken up by elf headers. */
3994 if (map->includes_filehdr)
3995 map->p_paddr -= iehdr->e_ehsize;
3997 if (map->includes_phdrs)
3999 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4001 /* iehdr->e_phnum is just an estimate of the number
4002 of program headers that we will need. Make a note
4003 here of the number we used and the segment we chose
4004 to hold these headers, so that we can adjust the
4005 offset when we know the correct value. */
4006 phdr_adjust_num = iehdr->e_phnum;
4007 phdr_adjust_seg = map;
4011 /* Step Three: Loop over the sections again, this time assigning
4012 those that fit to the current segment and remvoing them from the
4013 sections array; but making sure not to leave large gaps. Once all
4014 possible sections have been assigned to the current segment it is
4015 added to the list of built segments and if sections still remain
4016 to be assigned, a new segment is constructed before repeating
4024 /* Fill the current segment with sections that fit. */
4025 for (j = 0; j < section_count; j++)
4027 section = sections[j];
4029 if (section == NULL)
4032 output_section = section->output_section;
4034 BFD_ASSERT (output_section != NULL);
4036 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4037 || IS_COREFILE_NOTE (segment, section))
4039 if (map->count == 0)
4041 /* If the first section in a segment does not start at
4042 the beginning of the segment, then something is
4044 if (output_section->lma !=
4046 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4047 + (map->includes_phdrs
4048 ? iehdr->e_phnum * iehdr->e_phentsize
4054 asection * prev_sec;
4056 prev_sec = map->sections[map->count - 1];
4058 /* If the gap between the end of the previous section
4059 and the start of this section is more than
4060 maxpagesize then we need to start a new segment. */
4061 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4062 < BFD_ALIGN (output_section->lma, maxpagesize))
4063 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4065 if (suggested_lma == 0)
4066 suggested_lma = output_section->lma;
4072 map->sections[map->count++] = output_section;
4075 section->segment_mark = true;
4077 else if (suggested_lma == 0)
4078 suggested_lma = output_section->lma;
4081 BFD_ASSERT (map->count > 0);
4083 /* Add the current segment to the list of built segments. */
4084 * pointer_to_map = map;
4085 pointer_to_map = & map->next;
4087 if (isec < section_count)
4089 /* We still have not allocated all of the sections to
4090 segments. Create a new segment here, initialise it
4091 and carry on looping. */
4092 map = ((struct elf_segment_map *)
4094 (sizeof (struct elf_segment_map)
4095 + ((size_t) section_count - 1)
4096 * sizeof (asection *))));
4100 /* Initialise the fields of the segment map. Set the physical
4101 physical address to the LMA of the first section that has
4102 not yet been assigned. */
4104 map->p_type = segment->p_type;
4105 map->p_flags = segment->p_flags;
4106 map->p_flags_valid = 1;
4107 map->p_paddr = suggested_lma;
4108 map->p_paddr_valid = 1;
4109 map->includes_filehdr = 0;
4110 map->includes_phdrs = 0;
4113 while (isec < section_count);
4118 /* The Solaris linker creates program headers in which all the
4119 p_paddr fields are zero. When we try to objcopy or strip such a
4120 file, we get confused. Check for this case, and if we find it
4121 reset the p_paddr_valid fields. */
4122 for (map = map_first; map != NULL; map = map->next)
4123 if (map->p_paddr != 0)
4127 for (map = map_first; map != NULL; map = map->next)
4128 map->p_paddr_valid = 0;
4131 elf_tdata (obfd)->segment_map = map_first;
4133 /* If we had to estimate the number of program headers that were
4134 going to be needed, then check our estimate know and adjust
4135 the offset if necessary. */
4136 if (phdr_adjust_seg != NULL)
4140 for (count = 0, map = map_first; map != NULL; map = map->next)
4143 if (count > phdr_adjust_num)
4144 phdr_adjust_seg->p_paddr
4145 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4149 /* Final Step: Sort the segments into ascending order of physical address. */
4150 if (map_first != NULL)
4152 struct elf_segment_map * prev;
4155 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4157 /* Yes I know - its a bubble sort.... */
4158 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4160 /* Swap map and map->next. */
4161 prev->next = map->next;
4162 map->next = map->next->next;
4163 prev->next->next = map;
4173 #undef IS_CONTAINED_BY_VMA
4174 #undef IS_CONTAINED_BY_LMA
4175 #undef IS_COREFILE_NOTE
4176 #undef IS_SOLARIS_PT_INTERP
4177 #undef INCLUDE_SECTION_IN_SEGMENT
4178 #undef SEGMENT_AFTER_SEGMENT
4179 #undef SEGMENT_OVERLAPS
4183 /* Copy private section information. This copies over the entsize
4184 field, and sometimes the info field. */
4187 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4193 Elf_Internal_Shdr *ihdr, *ohdr;
4195 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4196 || obfd->xvec->flavour != bfd_target_elf_flavour)
4199 /* Copy over private BFD data if it has not already been copied.
4200 This must be done here, rather than in the copy_private_bfd_data
4201 entry point, because the latter is called after the section
4202 contents have been set, which means that the program headers have
4203 already been worked out. */
4204 if (elf_tdata (obfd)->segment_map == NULL
4205 && elf_tdata (ibfd)->phdr != NULL)
4209 /* Only set up the segments if there are no more SEC_ALLOC
4210 sections. FIXME: This won't do the right thing if objcopy is
4211 used to remove the last SEC_ALLOC section, since objcopy
4212 won't call this routine in that case. */
4213 for (s = isec->next; s != NULL; s = s->next)
4214 if ((s->flags & SEC_ALLOC) != 0)
4218 if (! copy_private_bfd_data (ibfd, obfd))
4223 ihdr = &elf_section_data (isec)->this_hdr;
4224 ohdr = &elf_section_data (osec)->this_hdr;
4226 ohdr->sh_entsize = ihdr->sh_entsize;
4228 if (ihdr->sh_type == SHT_SYMTAB
4229 || ihdr->sh_type == SHT_DYNSYM
4230 || ihdr->sh_type == SHT_GNU_verneed
4231 || ihdr->sh_type == SHT_GNU_verdef)
4232 ohdr->sh_info = ihdr->sh_info;
4234 elf_section_data (osec)->use_rela_p
4235 = elf_section_data (isec)->use_rela_p;
4240 /* Copy private symbol information. If this symbol is in a section
4241 which we did not map into a BFD section, try to map the section
4242 index correctly. We use special macro definitions for the mapped
4243 section indices; these definitions are interpreted by the
4244 swap_out_syms function. */
4246 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4247 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4248 #define MAP_STRTAB (SHN_LORESERVE - 3)
4249 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4252 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4258 elf_symbol_type *isym, *osym;
4260 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4261 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4264 isym = elf_symbol_from (ibfd, isymarg);
4265 osym = elf_symbol_from (obfd, osymarg);
4269 && bfd_is_abs_section (isym->symbol.section))
4273 shndx = isym->internal_elf_sym.st_shndx;
4274 if (shndx == elf_onesymtab (ibfd))
4275 shndx = MAP_ONESYMTAB;
4276 else if (shndx == elf_dynsymtab (ibfd))
4277 shndx = MAP_DYNSYMTAB;
4278 else if (shndx == elf_tdata (ibfd)->strtab_section)
4280 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4281 shndx = MAP_SHSTRTAB;
4282 osym->internal_elf_sym.st_shndx = shndx;
4288 /* Swap out the symbols. */
4291 swap_out_syms (abfd, sttp, relocatable_p)
4293 struct bfd_strtab_hash **sttp;
4296 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4298 if (!elf_map_symbols (abfd))
4301 /* Dump out the symtabs. */
4303 int symcount = bfd_get_symcount (abfd);
4304 asymbol **syms = bfd_get_outsymbols (abfd);
4305 struct bfd_strtab_hash *stt;
4306 Elf_Internal_Shdr *symtab_hdr;
4307 Elf_Internal_Shdr *symstrtab_hdr;
4308 char *outbound_syms;
4311 stt = _bfd_elf_stringtab_init ();
4315 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4316 symtab_hdr->sh_type = SHT_SYMTAB;
4317 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4318 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4319 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4320 symtab_hdr->sh_addralign = bed->s->file_align;
4322 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4323 symstrtab_hdr->sh_type = SHT_STRTAB;
4325 outbound_syms = bfd_alloc (abfd,
4326 (1 + symcount) * bed->s->sizeof_sym);
4327 if (outbound_syms == NULL)
4329 symtab_hdr->contents = (PTR) outbound_syms;
4331 /* now generate the data (for "contents") */
4333 /* Fill in zeroth symbol and swap it out. */
4334 Elf_Internal_Sym sym;
4340 sym.st_shndx = SHN_UNDEF;
4341 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4342 outbound_syms += bed->s->sizeof_sym;
4344 for (idx = 0; idx < symcount; idx++)
4346 Elf_Internal_Sym sym;
4347 bfd_vma value = syms[idx]->value;
4348 elf_symbol_type *type_ptr;
4349 flagword flags = syms[idx]->flags;
4352 if (flags & BSF_SECTION_SYM)
4353 /* Section symbols have no names. */
4357 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4360 if (sym.st_name == (unsigned long) -1)
4364 type_ptr = elf_symbol_from (abfd, syms[idx]);
4366 if ((flags & BSF_SECTION_SYM) == 0
4367 && bfd_is_com_section (syms[idx]->section))
4369 /* ELF common symbols put the alignment into the `value' field,
4370 and the size into the `size' field. This is backwards from
4371 how BFD handles it, so reverse it here. */
4372 sym.st_size = value;
4373 if (type_ptr == NULL
4374 || type_ptr->internal_elf_sym.st_value == 0)
4375 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4377 sym.st_value = type_ptr->internal_elf_sym.st_value;
4378 sym.st_shndx = _bfd_elf_section_from_bfd_section
4379 (abfd, syms[idx]->section);
4383 asection *sec = syms[idx]->section;
4386 if (sec->output_section)
4388 value += sec->output_offset;
4389 sec = sec->output_section;
4391 /* Don't add in the section vma for relocatable output. */
4392 if (! relocatable_p)
4394 sym.st_value = value;
4395 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4397 if (bfd_is_abs_section (sec)
4399 && type_ptr->internal_elf_sym.st_shndx != 0)
4401 /* This symbol is in a real ELF section which we did
4402 not create as a BFD section. Undo the mapping done
4403 by copy_private_symbol_data. */
4404 shndx = type_ptr->internal_elf_sym.st_shndx;
4408 shndx = elf_onesymtab (abfd);
4411 shndx = elf_dynsymtab (abfd);
4414 shndx = elf_tdata (abfd)->strtab_section;
4417 shndx = elf_tdata (abfd)->shstrtab_section;
4425 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4431 /* Writing this would be a hell of a lot easier if
4432 we had some decent documentation on bfd, and
4433 knew what to expect of the library, and what to
4434 demand of applications. For example, it
4435 appears that `objcopy' might not set the
4436 section of a symbol to be a section that is
4437 actually in the output file. */
4438 sec2 = bfd_get_section_by_name (abfd, sec->name);
4439 BFD_ASSERT (sec2 != 0);
4440 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4441 BFD_ASSERT (shndx != -1);
4445 sym.st_shndx = shndx;
4448 if ((flags & BSF_FUNCTION) != 0)
4450 else if ((flags & BSF_OBJECT) != 0)
4455 /* Processor-specific types */
4456 if (type_ptr != NULL
4457 && bed->elf_backend_get_symbol_type)
4458 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4460 if (flags & BSF_SECTION_SYM)
4461 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4462 else if (bfd_is_com_section (syms[idx]->section))
4463 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4464 else if (bfd_is_und_section (syms[idx]->section))
4465 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4469 else if (flags & BSF_FILE)
4470 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4473 int bind = STB_LOCAL;
4475 if (flags & BSF_LOCAL)
4477 else if (flags & BSF_WEAK)
4479 else if (flags & BSF_GLOBAL)
4482 sym.st_info = ELF_ST_INFO (bind, type);
4485 if (type_ptr != NULL)
4486 sym.st_other = type_ptr->internal_elf_sym.st_other;
4490 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4491 outbound_syms += bed->s->sizeof_sym;
4495 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4496 symstrtab_hdr->sh_type = SHT_STRTAB;
4498 symstrtab_hdr->sh_flags = 0;
4499 symstrtab_hdr->sh_addr = 0;
4500 symstrtab_hdr->sh_entsize = 0;
4501 symstrtab_hdr->sh_link = 0;
4502 symstrtab_hdr->sh_info = 0;
4503 symstrtab_hdr->sh_addralign = 1;
4509 /* Return the number of bytes required to hold the symtab vector.
4511 Note that we base it on the count plus 1, since we will null terminate
4512 the vector allocated based on this size. However, the ELF symbol table
4513 always has a dummy entry as symbol #0, so it ends up even. */
4516 _bfd_elf_get_symtab_upper_bound (abfd)
4521 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4523 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4524 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4530 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4535 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4537 if (elf_dynsymtab (abfd) == 0)
4539 bfd_set_error (bfd_error_invalid_operation);
4543 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4544 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4550 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4551 bfd *abfd ATTRIBUTE_UNUSED;
4554 return (asect->reloc_count + 1) * sizeof (arelent *);
4557 /* Canonicalize the relocs. */
4560 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4569 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4575 tblptr = section->relocation;
4576 for (i = 0; i < section->reloc_count; i++)
4577 *relptr++ = tblptr++;
4581 return section->reloc_count;
4585 _bfd_elf_get_symtab (abfd, alocation)
4587 asymbol **alocation;
4589 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4590 (abfd, alocation, false);
4593 bfd_get_symcount (abfd) = symcount;
4598 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4600 asymbol **alocation;
4602 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4603 (abfd, alocation, true);
4606 /* Return the size required for the dynamic reloc entries. Any
4607 section that was actually installed in the BFD, and has type
4608 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4609 considered to be a dynamic reloc section. */
4612 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4618 if (elf_dynsymtab (abfd) == 0)
4620 bfd_set_error (bfd_error_invalid_operation);
4624 ret = sizeof (arelent *);
4625 for (s = abfd->sections; s != NULL; s = s->next)
4626 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4627 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4628 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4629 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4630 * sizeof (arelent *));
4635 /* Canonicalize the dynamic relocation entries. Note that we return
4636 the dynamic relocations as a single block, although they are
4637 actually associated with particular sections; the interface, which
4638 was designed for SunOS style shared libraries, expects that there
4639 is only one set of dynamic relocs. Any section that was actually
4640 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4641 the dynamic symbol table, is considered to be a dynamic reloc
4645 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4650 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4654 if (elf_dynsymtab (abfd) == 0)
4656 bfd_set_error (bfd_error_invalid_operation);
4660 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4662 for (s = abfd->sections; s != NULL; s = s->next)
4664 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4665 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4666 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4671 if (! (*slurp_relocs) (abfd, s, syms, true))
4673 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4675 for (i = 0; i < count; i++)
4686 /* Read in the version information. */
4689 _bfd_elf_slurp_version_tables (abfd)
4692 bfd_byte *contents = NULL;
4694 if (elf_dynverdef (abfd) != 0)
4696 Elf_Internal_Shdr *hdr;
4697 Elf_External_Verdef *everdef;
4698 Elf_Internal_Verdef *iverdef;
4699 Elf_Internal_Verdef *iverdefarr;
4700 Elf_Internal_Verdef iverdefmem;
4702 unsigned int maxidx;
4704 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4706 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4707 if (contents == NULL)
4709 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4710 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4713 /* We know the number of entries in the section but not the maximum
4714 index. Therefore we have to run through all entries and find
4716 everdef = (Elf_External_Verdef *) contents;
4718 for (i = 0; i < hdr->sh_info; ++i)
4720 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4722 if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4723 maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4725 everdef = ((Elf_External_Verdef *)
4726 ((bfd_byte *) everdef + iverdefmem.vd_next));
4729 elf_tdata (abfd)->verdef =
4730 ((Elf_Internal_Verdef *)
4731 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4732 if (elf_tdata (abfd)->verdef == NULL)
4735 elf_tdata (abfd)->cverdefs = maxidx;
4737 everdef = (Elf_External_Verdef *) contents;
4738 iverdefarr = elf_tdata (abfd)->verdef;
4739 for (i = 0; i < hdr->sh_info; i++)
4741 Elf_External_Verdaux *everdaux;
4742 Elf_Internal_Verdaux *iverdaux;
4745 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4747 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4748 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4750 iverdef->vd_bfd = abfd;
4752 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4755 * sizeof (Elf_Internal_Verdaux))));
4756 if (iverdef->vd_auxptr == NULL)
4759 everdaux = ((Elf_External_Verdaux *)
4760 ((bfd_byte *) everdef + iverdef->vd_aux));
4761 iverdaux = iverdef->vd_auxptr;
4762 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4764 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4766 iverdaux->vda_nodename =
4767 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4768 iverdaux->vda_name);
4769 if (iverdaux->vda_nodename == NULL)
4772 if (j + 1 < iverdef->vd_cnt)
4773 iverdaux->vda_nextptr = iverdaux + 1;
4775 iverdaux->vda_nextptr = NULL;
4777 everdaux = ((Elf_External_Verdaux *)
4778 ((bfd_byte *) everdaux + iverdaux->vda_next));
4781 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4783 if (i + 1 < hdr->sh_info)
4784 iverdef->vd_nextdef = iverdef + 1;
4786 iverdef->vd_nextdef = NULL;
4788 everdef = ((Elf_External_Verdef *)
4789 ((bfd_byte *) everdef + iverdef->vd_next));
4796 if (elf_dynverref (abfd) != 0)
4798 Elf_Internal_Shdr *hdr;
4799 Elf_External_Verneed *everneed;
4800 Elf_Internal_Verneed *iverneed;
4803 hdr = &elf_tdata (abfd)->dynverref_hdr;
4805 elf_tdata (abfd)->verref =
4806 ((Elf_Internal_Verneed *)
4807 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4808 if (elf_tdata (abfd)->verref == NULL)
4811 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4813 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4814 if (contents == NULL)
4816 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4817 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4820 everneed = (Elf_External_Verneed *) contents;
4821 iverneed = elf_tdata (abfd)->verref;
4822 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4824 Elf_External_Vernaux *evernaux;
4825 Elf_Internal_Vernaux *ivernaux;
4828 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4830 iverneed->vn_bfd = abfd;
4832 iverneed->vn_filename =
4833 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4835 if (iverneed->vn_filename == NULL)
4838 iverneed->vn_auxptr =
4839 ((Elf_Internal_Vernaux *)
4841 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4843 evernaux = ((Elf_External_Vernaux *)
4844 ((bfd_byte *) everneed + iverneed->vn_aux));
4845 ivernaux = iverneed->vn_auxptr;
4846 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4848 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4850 ivernaux->vna_nodename =
4851 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4852 ivernaux->vna_name);
4853 if (ivernaux->vna_nodename == NULL)
4856 if (j + 1 < iverneed->vn_cnt)
4857 ivernaux->vna_nextptr = ivernaux + 1;
4859 ivernaux->vna_nextptr = NULL;
4861 evernaux = ((Elf_External_Vernaux *)
4862 ((bfd_byte *) evernaux + ivernaux->vna_next));
4865 if (i + 1 < hdr->sh_info)
4866 iverneed->vn_nextref = iverneed + 1;
4868 iverneed->vn_nextref = NULL;
4870 everneed = ((Elf_External_Verneed *)
4871 ((bfd_byte *) everneed + iverneed->vn_next));
4881 if (contents == NULL)
4887 _bfd_elf_make_empty_symbol (abfd)
4890 elf_symbol_type *newsym;
4892 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4897 newsym->symbol.the_bfd = abfd;
4898 return &newsym->symbol;
4903 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4904 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4908 bfd_symbol_info (symbol, ret);
4911 /* Return whether a symbol name implies a local symbol. Most targets
4912 use this function for the is_local_label_name entry point, but some
4916 _bfd_elf_is_local_label_name (abfd, name)
4917 bfd *abfd ATTRIBUTE_UNUSED;
4920 /* Normal local symbols start with ``.L''. */
4921 if (name[0] == '.' && name[1] == 'L')
4924 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4925 DWARF debugging symbols starting with ``..''. */
4926 if (name[0] == '.' && name[1] == '.')
4929 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4930 emitting DWARF debugging output. I suspect this is actually a
4931 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4932 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4933 underscore to be emitted on some ELF targets). For ease of use,
4934 we treat such symbols as local. */
4935 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4942 _bfd_elf_get_lineno (ignore_abfd, symbol)
4943 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4944 asymbol *symbol ATTRIBUTE_UNUSED;
4951 _bfd_elf_set_arch_mach (abfd, arch, machine)
4953 enum bfd_architecture arch;
4954 unsigned long machine;
4956 /* If this isn't the right architecture for this backend, and this
4957 isn't the generic backend, fail. */
4958 if (arch != get_elf_backend_data (abfd)->arch
4959 && arch != bfd_arch_unknown
4960 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4963 return bfd_default_set_arch_mach (abfd, arch, machine);
4966 /* Find the nearest line to a particular section and offset, for error
4970 _bfd_elf_find_nearest_line (abfd,
4981 CONST char **filename_ptr;
4982 CONST char **functionname_ptr;
4983 unsigned int *line_ptr;
4986 const char *filename;
4991 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4992 filename_ptr, functionname_ptr,
4996 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4997 filename_ptr, functionname_ptr,
5001 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5002 &found, filename_ptr,
5003 functionname_ptr, line_ptr,
5004 &elf_tdata (abfd)->line_info))
5009 if (symbols == NULL)
5016 for (p = symbols; *p != NULL; p++)
5020 q = (elf_symbol_type *) *p;
5022 if (bfd_get_section (&q->symbol) != section)
5025 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5030 filename = bfd_asymbol_name (&q->symbol);
5034 if (q->symbol.section == section
5035 && q->symbol.value >= low_func
5036 && q->symbol.value <= offset)
5038 func = (asymbol *) q;
5039 low_func = q->symbol.value;
5048 *filename_ptr = filename;
5049 *functionname_ptr = bfd_asymbol_name (func);
5055 _bfd_elf_sizeof_headers (abfd, reloc)
5061 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5063 ret += get_program_header_size (abfd);
5068 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5073 bfd_size_type count;
5075 Elf_Internal_Shdr *hdr;
5077 if (! abfd->output_has_begun
5078 && ! _bfd_elf_compute_section_file_positions
5079 (abfd, (struct bfd_link_info *) NULL))
5082 hdr = &elf_section_data (section)->this_hdr;
5084 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5086 if (bfd_write (location, 1, count, abfd) != count)
5093 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5094 bfd *abfd ATTRIBUTE_UNUSED;
5095 arelent *cache_ptr ATTRIBUTE_UNUSED;
5096 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5103 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5106 Elf_Internal_Rel *dst;
5112 /* Try to convert a non-ELF reloc into an ELF one. */
5115 _bfd_elf_validate_reloc (abfd, areloc)
5119 /* Check whether we really have an ELF howto. */
5121 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5123 bfd_reloc_code_real_type code;
5124 reloc_howto_type *howto;
5126 /* Alien reloc: Try to determine its type to replace it with an
5127 equivalent ELF reloc. */
5129 if (areloc->howto->pc_relative)
5131 switch (areloc->howto->bitsize)
5134 code = BFD_RELOC_8_PCREL;
5137 code = BFD_RELOC_12_PCREL;
5140 code = BFD_RELOC_16_PCREL;
5143 code = BFD_RELOC_24_PCREL;
5146 code = BFD_RELOC_32_PCREL;
5149 code = BFD_RELOC_64_PCREL;
5155 howto = bfd_reloc_type_lookup (abfd, code);
5157 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5159 if (howto->pcrel_offset)
5160 areloc->addend += areloc->address;
5162 areloc->addend -= areloc->address; /* addend is unsigned!! */
5167 switch (areloc->howto->bitsize)
5173 code = BFD_RELOC_14;
5176 code = BFD_RELOC_16;
5179 code = BFD_RELOC_26;
5182 code = BFD_RELOC_32;
5185 code = BFD_RELOC_64;
5191 howto = bfd_reloc_type_lookup (abfd, code);
5195 areloc->howto = howto;
5203 (*_bfd_error_handler)
5204 (_("%s: unsupported relocation type %s"),
5205 bfd_get_filename (abfd), areloc->howto->name);
5206 bfd_set_error (bfd_error_bad_value);
5211 _bfd_elf_close_and_cleanup (abfd)
5214 if (bfd_get_format (abfd) == bfd_object)
5216 if (elf_shstrtab (abfd) != NULL)
5217 _bfd_stringtab_free (elf_shstrtab (abfd));
5220 return _bfd_generic_close_and_cleanup (abfd);
5223 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5224 in the relocation's offset. Thus we cannot allow any sort of sanity
5225 range-checking to interfere. There is nothing else to do in processing
5228 bfd_reloc_status_type
5229 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5230 bfd *abfd ATTRIBUTE_UNUSED;
5231 arelent *re ATTRIBUTE_UNUSED;
5232 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5233 PTR data ATTRIBUTE_UNUSED;
5234 asection *is ATTRIBUTE_UNUSED;
5235 bfd *obfd ATTRIBUTE_UNUSED;
5236 char **errmsg ATTRIBUTE_UNUSED;
5238 return bfd_reloc_ok;
5242 /* Elf core file support. Much of this only works on native
5243 toolchains, since we rely on knowing the
5244 machine-dependent procfs structure in order to pick
5245 out details about the corefile. */
5247 #ifdef HAVE_SYS_PROCFS_H
5248 # include <sys/procfs.h>
5252 /* Define offsetof for those systems which lack it. */
5255 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5259 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5262 elfcore_make_pid (abfd)
5265 return ((elf_tdata (abfd)->core_lwpid << 16)
5266 + (elf_tdata (abfd)->core_pid));
5270 /* If there isn't a section called NAME, make one, using
5271 data from SECT. Note, this function will generate a
5272 reference to NAME, so you shouldn't deallocate or
5276 elfcore_maybe_make_sect (abfd, name, sect)
5283 if (bfd_get_section_by_name (abfd, name) != NULL)
5286 sect2 = bfd_make_section (abfd, name);
5290 sect2->_raw_size = sect->_raw_size;
5291 sect2->filepos = sect->filepos;
5292 sect2->flags = sect->flags;
5293 sect2->alignment_power = sect->alignment_power;
5298 /* prstatus_t exists on:
5300 linux 2.[01] + glibc
5304 #if defined (HAVE_PRSTATUS_T)
5306 elfcore_grok_prstatus (abfd, note)
5308 Elf_Internal_Note* note;
5316 if (note->descsz == sizeof (prstatus_t))
5320 raw_size = sizeof (prstat.pr_reg);
5321 offset = offsetof (prstatus_t, pr_reg);
5322 memcpy (&prstat, note->descdata, sizeof (prstat));
5324 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5325 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5327 /* pr_who exists on:
5330 pr_who doesn't exist on:
5333 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5334 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5337 #if defined (HAVE_PRSTATUS32_T)
5338 else if (note->descsz == sizeof (prstatus32_t))
5340 /* 64-bit host, 32-bit corefile */
5341 prstatus32_t prstat;
5343 raw_size = sizeof (prstat.pr_reg);
5344 offset = offsetof (prstatus32_t, pr_reg);
5345 memcpy (&prstat, note->descdata, sizeof (prstat));
5347 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5348 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5350 /* pr_who exists on:
5353 pr_who doesn't exist on:
5356 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5357 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5360 #endif /* HAVE_PRSTATUS32_T */
5363 /* Fail - we don't know how to handle any other
5364 note size (ie. data object type). */
5368 /* Make a ".reg/999" section. */
5370 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5371 name = bfd_alloc (abfd, strlen (buf) + 1);
5376 sect = bfd_make_section (abfd, name);
5380 sect->_raw_size = raw_size;
5381 sect->filepos = note->descpos + offset;
5383 sect->flags = SEC_HAS_CONTENTS;
5384 sect->alignment_power = 2;
5386 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5391 #endif /* defined (HAVE_PRSTATUS_T) */
5394 /* Create a pseudosection containing the exact contents of NOTE. This
5395 actually creates up to two pseudosections:
5396 - For the single-threaded case, a section named NAME, unless
5397 such a section already exists.
5398 - For the multi-threaded case, a section named "NAME/PID", where
5399 PID is elfcore_make_pid (abfd).
5400 Both pseudosections have identical contents: the contents of NOTE. */
5403 elfcore_make_note_pseudosection (abfd, name, note)
5406 Elf_Internal_Note* note;
5409 char *threaded_name;
5412 /* Build the section name. */
5414 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5415 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5416 if (threaded_name == NULL)
5418 strcpy (threaded_name, buf);
5420 sect = bfd_make_section (abfd, threaded_name);
5423 sect->_raw_size = note->descsz;
5424 sect->filepos = note->descpos;
5425 sect->flags = SEC_HAS_CONTENTS;
5426 sect->alignment_power = 2;
5428 if (! elfcore_maybe_make_sect (abfd, name, sect))
5435 /* There isn't a consistent prfpregset_t across platforms,
5436 but it doesn't matter, because we don't have to pick this
5437 data structure apart. */
5439 elfcore_grok_prfpreg (abfd, note)
5441 Elf_Internal_Note* note;
5443 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5447 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5448 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5451 elfcore_grok_prxfpreg (abfd, note)
5453 Elf_Internal_Note* note;
5455 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5459 #if defined (HAVE_PRPSINFO_T)
5460 typedef prpsinfo_t elfcore_psinfo_t;
5461 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5462 typedef prpsinfo32_t elfcore_psinfo32_t;
5466 #if defined (HAVE_PSINFO_T)
5467 typedef psinfo_t elfcore_psinfo_t;
5468 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5469 typedef psinfo32_t elfcore_psinfo32_t;
5474 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5476 /* return a malloc'ed copy of a string at START which is at
5477 most MAX bytes long, possibly without a terminating '\0'.
5478 the copy will always have a terminating '\0'. */
5481 elfcore_strndup (abfd, start, max)
5487 char* end = memchr (start, '\0', max);
5495 dup = bfd_alloc (abfd, len + 1);
5499 memcpy (dup, start, len);
5506 elfcore_grok_psinfo (abfd, note)
5508 Elf_Internal_Note* note;
5510 if (note->descsz == sizeof (elfcore_psinfo_t))
5512 elfcore_psinfo_t psinfo;
5514 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5516 elf_tdata (abfd)->core_program
5517 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5519 elf_tdata (abfd)->core_command
5520 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5522 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5523 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5525 /* 64-bit host, 32-bit corefile */
5526 elfcore_psinfo32_t psinfo;
5528 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5530 elf_tdata (abfd)->core_program
5531 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5533 elf_tdata (abfd)->core_command
5534 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5540 /* Fail - we don't know how to handle any other
5541 note size (ie. data object type). */
5545 /* Note that for some reason, a spurious space is tacked
5546 onto the end of the args in some (at least one anyway)
5547 implementations, so strip it off if it exists. */
5550 char* command = elf_tdata (abfd)->core_command;
5551 int n = strlen (command);
5553 if (0 < n && command[n - 1] == ' ')
5554 command[n - 1] = '\0';
5559 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5562 #if defined (HAVE_PSTATUS_T)
5564 elfcore_grok_pstatus (abfd, note)
5566 Elf_Internal_Note* note;
5568 if (note->descsz == sizeof (pstatus_t))
5572 memcpy (&pstat, note->descdata, sizeof (pstat));
5574 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5576 #if defined (HAVE_PSTATUS32_T)
5577 else if (note->descsz == sizeof (pstatus32_t))
5579 /* 64-bit host, 32-bit corefile */
5582 memcpy (&pstat, note->descdata, sizeof (pstat));
5584 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5587 /* Could grab some more details from the "representative"
5588 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5589 NT_LWPSTATUS note, presumably. */
5593 #endif /* defined (HAVE_PSTATUS_T) */
5596 #if defined (HAVE_LWPSTATUS_T)
5598 elfcore_grok_lwpstatus (abfd, note)
5600 Elf_Internal_Note* note;
5602 lwpstatus_t lwpstat;
5607 if (note->descsz != sizeof (lwpstat))
5610 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5612 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5613 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5615 /* Make a ".reg/999" section. */
5617 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5618 name = bfd_alloc (abfd, strlen (buf) + 1);
5623 sect = bfd_make_section (abfd, name);
5627 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5628 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5629 sect->filepos = note->descpos
5630 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5633 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5634 sect->_raw_size = sizeof (lwpstat.pr_reg);
5635 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5638 sect->flags = SEC_HAS_CONTENTS;
5639 sect->alignment_power = 2;
5641 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5644 /* Make a ".reg2/999" section */
5646 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5647 name = bfd_alloc (abfd, strlen (buf) + 1);
5652 sect = bfd_make_section (abfd, name);
5656 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5657 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5658 sect->filepos = note->descpos
5659 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5662 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5663 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5664 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5667 sect->flags = SEC_HAS_CONTENTS;
5668 sect->alignment_power = 2;
5670 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5675 #endif /* defined (HAVE_LWPSTATUS_T) */
5677 #if defined (HAVE_WIN32_PSTATUS_T)
5679 elfcore_grok_win32pstatus (abfd, note)
5681 Elf_Internal_Note * note;
5686 win32_pstatus_t pstatus;
5688 if (note->descsz < sizeof (pstatus))
5691 memcpy (& pstatus, note->descdata, note->descsz);
5693 switch (pstatus.data_type)
5695 case NOTE_INFO_PROCESS:
5696 /* FIXME: need to add ->core_command. */
5697 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5698 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5701 case NOTE_INFO_THREAD:
5702 /* Make a ".reg/999" section. */
5703 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5705 name = bfd_alloc (abfd, strlen (buf) + 1);
5711 sect = bfd_make_section (abfd, name);
5715 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5716 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5717 data.thread_info.thread_context);
5718 sect->flags = SEC_HAS_CONTENTS;
5719 sect->alignment_power = 2;
5721 if (pstatus.data.thread_info.is_active_thread)
5722 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5726 case NOTE_INFO_MODULE:
5727 /* Make a ".module/xxxxxxxx" section. */
5728 sprintf (buf, ".module/%08x" , pstatus.data.module_info.base_address);
5730 name = bfd_alloc (abfd, strlen (buf) + 1);
5736 sect = bfd_make_section (abfd, name);
5741 sect->_raw_size = note->descsz;
5742 sect->filepos = note->descpos;
5743 sect->flags = SEC_HAS_CONTENTS;
5744 sect->alignment_power = 2;
5753 #endif /* HAVE_WIN32_PSTATUS_T */
5756 elfcore_grok_note (abfd, note)
5758 Elf_Internal_Note* note;
5765 #if defined (HAVE_PRSTATUS_T)
5767 return elfcore_grok_prstatus (abfd, note);
5770 #if defined (HAVE_PSTATUS_T)
5772 return elfcore_grok_pstatus (abfd, note);
5775 #if defined (HAVE_LWPSTATUS_T)
5777 return elfcore_grok_lwpstatus (abfd, note);
5780 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5781 return elfcore_grok_prfpreg (abfd, note);
5783 #if defined (HAVE_WIN32_PSTATUS_T)
5784 case NT_WIN32PSTATUS:
5785 return elfcore_grok_win32pstatus (abfd, note);
5788 case NT_PRXFPREG: /* Linux SSE extension */
5789 if (note->namesz == 5
5790 && ! strcmp (note->namedata, "LINUX"))
5791 return elfcore_grok_prxfpreg (abfd, note);
5795 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5798 return elfcore_grok_psinfo (abfd, note);
5805 elfcore_read_notes (abfd, offset, size)
5816 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5819 buf = bfd_malloc ((size_t) size);
5823 if (bfd_read (buf, size, 1, abfd) != size)
5831 while (p < buf + size)
5833 /* FIXME: bad alignment assumption. */
5834 Elf_External_Note* xnp = (Elf_External_Note*) p;
5835 Elf_Internal_Note in;
5837 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5839 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5840 in.namedata = xnp->name;
5842 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5843 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5844 in.descpos = offset + (in.descdata - buf);
5846 if (! elfcore_grok_note (abfd, &in))
5849 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5857 /* FIXME: This function is now unnecessary. Callers can just call
5858 bfd_section_from_phdr directly. */
5861 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5863 Elf_Internal_Phdr* phdr;
5866 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5874 /* Providing external access to the ELF program header table. */
5876 /* Return an upper bound on the number of bytes required to store a
5877 copy of ABFD's program header table entries. Return -1 if an error
5878 occurs; bfd_get_error will return an appropriate code. */
5880 bfd_get_elf_phdr_upper_bound (abfd)
5883 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5885 bfd_set_error (bfd_error_wrong_format);
5889 return (elf_elfheader (abfd)->e_phnum
5890 * sizeof (Elf_Internal_Phdr));
5894 /* Copy ABFD's program header table entries to *PHDRS. The entries
5895 will be stored as an array of Elf_Internal_Phdr structures, as
5896 defined in include/elf/internal.h. To find out how large the
5897 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5899 Return the number of program header table entries read, or -1 if an
5900 error occurs; bfd_get_error will return an appropriate code. */
5902 bfd_get_elf_phdrs (abfd, phdrs)
5908 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5910 bfd_set_error (bfd_error_wrong_format);
5914 num_phdrs = elf_elfheader (abfd)->e_phnum;
5915 memcpy (phdrs, elf_tdata (abfd)->phdr,
5916 num_phdrs * sizeof (Elf_Internal_Phdr));