Back to index

cell-binutils  2.17cvs20070401
elfcode.h
Go to the documentation of this file.
00001 /* ELF executable support for BFD.
00002    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
00003    2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
00004 
00005    Written by Fred Fish @ Cygnus Support, from information published
00006    in "UNIX System V Release 4, Programmers Guide: ANSI C and
00007    Programming Support Tools".  Sufficient support for gdb.
00008 
00009    Rewritten by Mark Eichin @ Cygnus Support, from information
00010    published in "System V Application Binary Interface", chapters 4
00011    and 5, as well as the various "Processor Supplement" documents
00012    derived from it. Added support for assembler and other object file
00013    utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael
00014    Meissner (Open Software Foundation), and Peter Hoogenboom (University
00015    of Utah) to finish and extend this.
00016 
00017 This file is part of BFD, the Binary File Descriptor library.
00018 
00019 This program is free software; you can redistribute it and/or modify
00020 it under the terms of the GNU General Public License as published by
00021 the Free Software Foundation; either version 2 of the License, or
00022 (at your option) any later version.
00023 
00024 This program is distributed in the hope that it will be useful,
00025 but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 GNU General Public License for more details.
00028 
00029 You should have received a copy of the GNU General Public License
00030 along with this program; if not, write to the Free Software
00031 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00032 
00033 /* Problems and other issues to resolve.
00034 
00035    (1) BFD expects there to be some fixed number of "sections" in
00036        the object file.  I.E. there is a "section_count" variable in the
00037        bfd structure which contains the number of sections.  However, ELF
00038        supports multiple "views" of a file.  In particular, with current
00039        implementations, executable files typically have two tables, a
00040        program header table and a section header table, both of which
00041        partition the executable.
00042 
00043        In ELF-speak, the "linking view" of the file uses the section header
00044        table to access "sections" within the file, and the "execution view"
00045        uses the program header table to access "segments" within the file.
00046        "Segments" typically may contain all the data from one or more
00047        "sections".
00048 
00049        Note that the section header table is optional in ELF executables,
00050        but it is this information that is most useful to gdb.  If the
00051        section header table is missing, then gdb should probably try
00052        to make do with the program header table.  (FIXME)
00053 
00054    (2)  The code in this file is compiled twice, once in 32-bit mode and
00055        once in 64-bit mode.  More of it should be made size-independent
00056        and moved into elf.c.
00057 
00058    (3) ELF section symbols are handled rather sloppily now.  This should
00059        be cleaned up, and ELF section symbols reconciled with BFD section
00060        symbols.
00061 
00062    (4)  We need a published spec for 64-bit ELF.  We've got some stuff here
00063        that we're using for SPARC V9 64-bit chips, but don't assume that
00064        it's cast in stone.
00065  */
00066 
00067 #include "bfd.h"
00068 #include "sysdep.h"
00069 #include "libiberty.h"
00070 #include "bfdlink.h"
00071 #include "libbfd.h"
00072 #include "elf-bfd.h"
00073 
00074 /* Renaming structures, typedefs, macros and functions to be size-specific.  */
00075 #define Elf_External_Ehdr   NAME(Elf,External_Ehdr)
00076 #define Elf_External_Sym    NAME(Elf,External_Sym)
00077 #define Elf_External_Shdr   NAME(Elf,External_Shdr)
00078 #define Elf_External_Phdr   NAME(Elf,External_Phdr)
00079 #define Elf_External_Rel    NAME(Elf,External_Rel)
00080 #define Elf_External_Rela   NAME(Elf,External_Rela)
00081 #define Elf_External_Dyn    NAME(Elf,External_Dyn)
00082 
00083 #define elf_core_file_failing_command     NAME(bfd_elf,core_file_failing_command)
00084 #define elf_core_file_failing_signal      NAME(bfd_elf,core_file_failing_signal)
00085 #define elf_core_file_matches_executable_p \
00086   NAME(bfd_elf,core_file_matches_executable_p)
00087 #define elf_object_p               NAME(bfd_elf,object_p)
00088 #define elf_core_file_p                   NAME(bfd_elf,core_file_p)
00089 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
00090 #define elf_get_dynamic_symtab_upper_bound \
00091   NAME(bfd_elf,get_dynamic_symtab_upper_bound)
00092 #define elf_swap_reloc_in          NAME(bfd_elf,swap_reloc_in)
00093 #define elf_swap_reloca_in         NAME(bfd_elf,swap_reloca_in)
00094 #define elf_swap_reloc_out         NAME(bfd_elf,swap_reloc_out)
00095 #define elf_swap_reloca_out        NAME(bfd_elf,swap_reloca_out)
00096 #define elf_swap_symbol_in         NAME(bfd_elf,swap_symbol_in)
00097 #define elf_swap_symbol_out        NAME(bfd_elf,swap_symbol_out)
00098 #define elf_swap_phdr_in           NAME(bfd_elf,swap_phdr_in)
00099 #define elf_swap_phdr_out          NAME(bfd_elf,swap_phdr_out)
00100 #define elf_swap_dyn_in                   NAME(bfd_elf,swap_dyn_in)
00101 #define elf_swap_dyn_out           NAME(bfd_elf,swap_dyn_out)
00102 #define elf_get_reloc_upper_bound  NAME(bfd_elf,get_reloc_upper_bound)
00103 #define elf_canonicalize_reloc            NAME(bfd_elf,canonicalize_reloc)
00104 #define elf_slurp_symbol_table            NAME(bfd_elf,slurp_symbol_table)
00105 #define elf_canonicalize_symtab           NAME(bfd_elf,canonicalize_symtab)
00106 #define elf_canonicalize_dynamic_symtab \
00107   NAME(bfd_elf,canonicalize_dynamic_symtab)
00108 #define elf_get_synthetic_symtab \
00109   NAME(bfd_elf,get_synthetic_symtab)
00110 #define elf_make_empty_symbol             NAME(bfd_elf,make_empty_symbol)
00111 #define elf_get_symbol_info        NAME(bfd_elf,get_symbol_info)
00112 #define elf_get_lineno                    NAME(bfd_elf,get_lineno)
00113 #define elf_set_arch_mach          NAME(bfd_elf,set_arch_mach)
00114 #define elf_find_nearest_line             NAME(bfd_elf,find_nearest_line)
00115 #define elf_sizeof_headers         NAME(bfd_elf,sizeof_headers)
00116 #define elf_set_section_contents   NAME(bfd_elf,set_section_contents)
00117 #define elf_no_info_to_howto              NAME(bfd_elf,no_info_to_howto)
00118 #define elf_no_info_to_howto_rel   NAME(bfd_elf,no_info_to_howto_rel)
00119 #define elf_find_section           NAME(bfd_elf,find_section)
00120 #define elf_write_shdrs_and_ehdr   NAME(bfd_elf,write_shdrs_and_ehdr)
00121 #define elf_write_out_phdrs        NAME(bfd_elf,write_out_phdrs)
00122 #define elf_write_relocs           NAME(bfd_elf,write_relocs)
00123 #define elf_slurp_reloc_table             NAME(bfd_elf,slurp_reloc_table)
00124 
00125 #if ARCH_SIZE == 64
00126 #define ELF_R_INFO(X,Y)     ELF64_R_INFO(X,Y)
00127 #define ELF_R_SYM(X) ELF64_R_SYM(X)
00128 #define ELF_R_TYPE(X)       ELF64_R_TYPE(X)
00129 #define ELFCLASS     ELFCLASS64
00130 #define FILE_ALIGN   8
00131 #define LOG_FILE_ALIGN      3
00132 #endif
00133 #if ARCH_SIZE == 32
00134 #define ELF_R_INFO(X,Y)     ELF32_R_INFO(X,Y)
00135 #define ELF_R_SYM(X) ELF32_R_SYM(X)
00136 #define ELF_R_TYPE(X)       ELF32_R_TYPE(X)
00137 #define ELFCLASS     ELFCLASS32
00138 #define FILE_ALIGN   4
00139 #define LOG_FILE_ALIGN      2
00140 #endif
00141 
00142 #if DEBUG & 2
00143 static void elf_debug_section (int, Elf_Internal_Shdr *);
00144 #endif
00145 #if DEBUG & 1
00146 static void elf_debug_file (Elf_Internal_Ehdr *);
00147 #endif
00148 
00149 /* Structure swapping routines */
00150 
00151 /* Should perhaps use put_offset, put_word, etc.  For now, the two versions
00152    can be handled by explicitly specifying 32 bits or "the long type".  */
00153 #if ARCH_SIZE == 64
00154 #define H_PUT_WORD          H_PUT_64
00155 #define H_PUT_SIGNED_WORD   H_PUT_S64
00156 #define H_GET_WORD          H_GET_64
00157 #define H_GET_SIGNED_WORD   H_GET_S64
00158 #endif
00159 #if ARCH_SIZE == 32
00160 #define H_PUT_WORD          H_PUT_32
00161 #define H_PUT_SIGNED_WORD   H_PUT_S32
00162 #define H_GET_WORD          H_GET_32
00163 #define H_GET_SIGNED_WORD   H_GET_S32
00164 #endif
00165 
00166 /* Translate an ELF symbol in external format into an ELF symbol in internal
00167    format.  */
00168 
00169 bfd_boolean
00170 elf_swap_symbol_in (bfd *abfd,
00171                   const void *psrc,
00172                   const void *pshn,
00173                   Elf_Internal_Sym *dst)
00174 {
00175   const Elf_External_Sym *src = psrc;
00176   const Elf_External_Sym_Shndx *shndx = pshn;
00177   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
00178 
00179   dst->st_name = H_GET_32 (abfd, src->st_name);
00180   if (signed_vma)
00181     dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
00182   else
00183     dst->st_value = H_GET_WORD (abfd, src->st_value);
00184   dst->st_size = H_GET_WORD (abfd, src->st_size);
00185   dst->st_info = H_GET_8 (abfd, src->st_info);
00186   dst->st_other = H_GET_8 (abfd, src->st_other);
00187   dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
00188   if (dst->st_shndx == SHN_XINDEX)
00189     {
00190       if (shndx == NULL)
00191        return FALSE;
00192       dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
00193     }
00194   return TRUE;
00195 }
00196 
00197 /* Translate an ELF symbol in internal format into an ELF symbol in external
00198    format.  */
00199 
00200 void
00201 elf_swap_symbol_out (bfd *abfd,
00202                    const Elf_Internal_Sym *src,
00203                    void *cdst,
00204                    void *shndx)
00205 {
00206   unsigned int tmp;
00207   Elf_External_Sym *dst = cdst;
00208   H_PUT_32 (abfd, src->st_name, dst->st_name);
00209   H_PUT_WORD (abfd, src->st_value, dst->st_value);
00210   H_PUT_WORD (abfd, src->st_size, dst->st_size);
00211   H_PUT_8 (abfd, src->st_info, dst->st_info);
00212   H_PUT_8 (abfd, src->st_other, dst->st_other);
00213   tmp = src->st_shndx;
00214   if (tmp > SHN_HIRESERVE)
00215     {
00216       if (shndx == NULL)
00217        abort ();
00218       H_PUT_32 (abfd, tmp, shndx);
00219       tmp = SHN_XINDEX;
00220     }
00221   H_PUT_16 (abfd, tmp, dst->st_shndx);
00222 }
00223 
00224 /* Translate an ELF file header in external format into an ELF file header in
00225    internal format.  */
00226 
00227 static void
00228 elf_swap_ehdr_in (bfd *abfd,
00229                 const Elf_External_Ehdr *src,
00230                 Elf_Internal_Ehdr *dst)
00231 {
00232   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
00233   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
00234   dst->e_type = H_GET_16 (abfd, src->e_type);
00235   dst->e_machine = H_GET_16 (abfd, src->e_machine);
00236   dst->e_version = H_GET_32 (abfd, src->e_version);
00237   if (signed_vma)
00238     dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
00239   else
00240     dst->e_entry = H_GET_WORD (abfd, src->e_entry);
00241   dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
00242   dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
00243   dst->e_flags = H_GET_32 (abfd, src->e_flags);
00244   dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
00245   dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
00246   dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
00247   dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
00248   dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
00249   dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
00250 }
00251 
00252 /* Translate an ELF file header in internal format into an ELF file header in
00253    external format.  */
00254 
00255 static void
00256 elf_swap_ehdr_out (bfd *abfd,
00257                  const Elf_Internal_Ehdr *src,
00258                  Elf_External_Ehdr *dst)
00259 {
00260   unsigned int tmp;
00261   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
00262   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
00263   /* note that all elements of dst are *arrays of unsigned char* already...  */
00264   H_PUT_16 (abfd, src->e_type, dst->e_type);
00265   H_PUT_16 (abfd, src->e_machine, dst->e_machine);
00266   H_PUT_32 (abfd, src->e_version, dst->e_version);
00267   if (signed_vma)
00268     H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
00269   else
00270     H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
00271   H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
00272   H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
00273   H_PUT_32 (abfd, src->e_flags, dst->e_flags);
00274   H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
00275   H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
00276   H_PUT_16 (abfd, src->e_phnum, dst->e_phnum);
00277   H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
00278   tmp = src->e_shnum;
00279   if (tmp >= SHN_LORESERVE)
00280     tmp = SHN_UNDEF;
00281   H_PUT_16 (abfd, tmp, dst->e_shnum);
00282   tmp = src->e_shstrndx;
00283   if (tmp >= SHN_LORESERVE)
00284     tmp = SHN_XINDEX;
00285   H_PUT_16 (abfd, tmp, dst->e_shstrndx);
00286 }
00287 
00288 /* Translate an ELF section header table entry in external format into an
00289    ELF section header table entry in internal format.  */
00290 
00291 static void
00292 elf_swap_shdr_in (bfd *abfd,
00293                 const Elf_External_Shdr *src,
00294                 Elf_Internal_Shdr *dst)
00295 {
00296   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
00297 
00298   dst->sh_name = H_GET_32 (abfd, src->sh_name);
00299   dst->sh_type = H_GET_32 (abfd, src->sh_type);
00300   dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
00301   if (signed_vma)
00302     dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
00303   else
00304     dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
00305   dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
00306   dst->sh_size = H_GET_WORD (abfd, src->sh_size);
00307   dst->sh_link = H_GET_32 (abfd, src->sh_link);
00308   dst->sh_info = H_GET_32 (abfd, src->sh_info);
00309   dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
00310   dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
00311   dst->bfd_section = NULL;
00312   dst->contents = NULL;
00313 }
00314 
00315 /* Translate an ELF section header table entry in internal format into an
00316    ELF section header table entry in external format.  */
00317 
00318 static void
00319 elf_swap_shdr_out (bfd *abfd,
00320                  const Elf_Internal_Shdr *src,
00321                  Elf_External_Shdr *dst)
00322 {
00323   /* note that all elements of dst are *arrays of unsigned char* already...  */
00324   H_PUT_32 (abfd, src->sh_name, dst->sh_name);
00325   H_PUT_32 (abfd, src->sh_type, dst->sh_type);
00326   H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
00327   H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
00328   H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
00329   H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
00330   H_PUT_32 (abfd, src->sh_link, dst->sh_link);
00331   H_PUT_32 (abfd, src->sh_info, dst->sh_info);
00332   H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
00333   H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
00334 }
00335 
00336 /* Translate an ELF program header table entry in external format into an
00337    ELF program header table entry in internal format.  */
00338 
00339 void
00340 elf_swap_phdr_in (bfd *abfd,
00341                 const Elf_External_Phdr *src,
00342                 Elf_Internal_Phdr *dst)
00343 {
00344   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
00345 
00346   dst->p_type = H_GET_32 (abfd, src->p_type);
00347   dst->p_flags = H_GET_32 (abfd, src->p_flags);
00348   dst->p_offset = H_GET_WORD (abfd, src->p_offset);
00349   if (signed_vma)
00350     {
00351       dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
00352       dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
00353     }
00354   else
00355     {
00356       dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
00357       dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
00358     }
00359   dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
00360   dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
00361   dst->p_align = H_GET_WORD (abfd, src->p_align);
00362 }
00363 
00364 void
00365 elf_swap_phdr_out (bfd *abfd,
00366                  const Elf_Internal_Phdr *src,
00367                  Elf_External_Phdr *dst)
00368 {
00369   /* note that all elements of dst are *arrays of unsigned char* already...  */
00370   H_PUT_32 (abfd, src->p_type, dst->p_type);
00371   H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
00372   H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
00373   H_PUT_WORD (abfd, src->p_paddr, dst->p_paddr);
00374   H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
00375   H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
00376   H_PUT_32 (abfd, src->p_flags, dst->p_flags);
00377   H_PUT_WORD (abfd, src->p_align, dst->p_align);
00378 }
00379 
00380 /* Translate an ELF reloc from external format to internal format.  */
00381 void
00382 elf_swap_reloc_in (bfd *abfd,
00383                  const bfd_byte *s,
00384                  Elf_Internal_Rela *dst)
00385 {
00386   const Elf_External_Rel *src = (const Elf_External_Rel *) s;
00387   dst->r_offset = H_GET_WORD (abfd, src->r_offset);
00388   dst->r_info = H_GET_WORD (abfd, src->r_info);
00389   dst->r_addend = 0;
00390 }
00391 
00392 void
00393 elf_swap_reloca_in (bfd *abfd,
00394                   const bfd_byte *s,
00395                   Elf_Internal_Rela *dst)
00396 {
00397   const Elf_External_Rela *src = (const Elf_External_Rela *) s;
00398   dst->r_offset = H_GET_WORD (abfd, src->r_offset);
00399   dst->r_info = H_GET_WORD (abfd, src->r_info);
00400   dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
00401 }
00402 
00403 /* Translate an ELF reloc from internal format to external format.  */
00404 void
00405 elf_swap_reloc_out (bfd *abfd,
00406                   const Elf_Internal_Rela *src,
00407                   bfd_byte *d)
00408 {
00409   Elf_External_Rel *dst = (Elf_External_Rel *) d;
00410   H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
00411   H_PUT_WORD (abfd, src->r_info, dst->r_info);
00412 }
00413 
00414 void
00415 elf_swap_reloca_out (bfd *abfd,
00416                    const Elf_Internal_Rela *src,
00417                    bfd_byte *d)
00418 {
00419   Elf_External_Rela *dst = (Elf_External_Rela *) d;
00420   H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
00421   H_PUT_WORD (abfd, src->r_info, dst->r_info);
00422   H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
00423 }
00424 
00425 void
00426 elf_swap_dyn_in (bfd *abfd,
00427                const void *p,
00428                Elf_Internal_Dyn *dst)
00429 {
00430   const Elf_External_Dyn *src = p;
00431 
00432   dst->d_tag = H_GET_WORD (abfd, src->d_tag);
00433   dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
00434 }
00435 
00436 void
00437 elf_swap_dyn_out (bfd *abfd,
00438                 const Elf_Internal_Dyn *src,
00439                 void *p)
00440 {
00441   Elf_External_Dyn *dst = p;
00442 
00443   H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
00444   H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
00445 }
00446 
00447 /* ELF .o/exec file reading */
00448 
00449 /* Begin processing a given object.
00450 
00451    First we validate the file by reading in the ELF header and checking
00452    the magic number.  */
00453 
00454 static inline bfd_boolean
00455 elf_file_p (Elf_External_Ehdr *x_ehdrp)
00456 {
00457   return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
00458          && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
00459          && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
00460          && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
00461 }
00462 
00463 /* Determines if a given section index is valid.  */
00464 
00465 static inline bfd_boolean
00466 valid_section_index_p (unsigned index, unsigned num_sections)
00467 {
00468   /* Note: We allow SHN_UNDEF as a valid section index.  */
00469   if (index < SHN_LORESERVE || index > SHN_HIRESERVE)
00470     return index < num_sections;
00471   
00472   /* We disallow the use of reserved indcies, except for those
00473      with OS or Application specific meaning.  The test make use
00474      of the knowledge that:
00475        SHN_LORESERVE == SHN_LOPROC
00476      and
00477        SHN_HIPROC == SHN_LOOS - 1  */
00478   /* XXX - Should we allow SHN_XINDEX as a valid index here ?  */
00479   return (index >= SHN_LOPROC && index <= SHN_HIOS);
00480 }
00481 
00482 /* Check to see if the file associated with ABFD matches the target vector
00483    that ABFD points to.
00484 
00485    Note that we may be called several times with the same ABFD, but different
00486    target vectors, most of which will not match.  We have to avoid leaving
00487    any side effects in ABFD, or any data it points to (like tdata), if the
00488    file does not match the target vector.  */
00489 
00490 const bfd_target *
00491 elf_object_p (bfd *abfd)
00492 {
00493   Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
00494   Elf_Internal_Ehdr *i_ehdrp;      /* Elf file header, internal form */
00495   Elf_External_Shdr x_shdr; /* Section header table entry, external form */
00496   Elf_Internal_Shdr i_shdr;
00497   Elf_Internal_Shdr *i_shdrp;      /* Section header table, internal form */
00498   unsigned int shindex;
00499   const struct elf_backend_data *ebd;
00500   struct bfd_preserve preserve;
00501   asection *s;
00502   bfd_size_type amt;
00503   const bfd_target *target;
00504   const bfd_target * const *target_ptr;
00505 
00506   preserve.marker = NULL;
00507 
00508   /* Read in the ELF header in external format.  */
00509 
00510   if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
00511     {
00512       if (bfd_get_error () != bfd_error_system_call)
00513        goto got_wrong_format_error;
00514       else
00515        goto got_no_match;
00516     }
00517 
00518   /* Now check to see if we have a valid ELF file, and one that BFD can
00519      make use of.  The magic number must match, the address size ('class')
00520      and byte-swapping must match our XVEC entry, and it must have a
00521      section header table (FIXME: See comments re sections at top of this
00522      file).  */
00523 
00524   if (! elf_file_p (&x_ehdr)
00525       || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
00526       || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
00527     goto got_wrong_format_error;
00528 
00529   /* Check that file's byte order matches xvec's */
00530   switch (x_ehdr.e_ident[EI_DATA])
00531     {
00532     case ELFDATA2MSB:              /* Big-endian */
00533       if (! bfd_header_big_endian (abfd))
00534        goto got_wrong_format_error;
00535       break;
00536     case ELFDATA2LSB:              /* Little-endian */
00537       if (! bfd_header_little_endian (abfd))
00538        goto got_wrong_format_error;
00539       break;
00540     case ELFDATANONE:              /* No data encoding specified */
00541     default:                /* Unknown data encoding specified */
00542       goto got_wrong_format_error;
00543     }
00544 
00545   if (!bfd_preserve_save (abfd, &preserve))
00546     goto got_no_match;
00547 
00548   target = abfd->xvec;
00549 
00550   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
00551      the tdata pointer in the bfd.  */
00552 
00553   if (! (*target->_bfd_set_format[bfd_object]) (abfd))
00554     goto got_no_match;
00555   preserve.marker = elf_tdata (abfd);
00556 
00557   /* Now that we know the byte order, swap in the rest of the header */
00558   i_ehdrp = elf_elfheader (abfd);
00559   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
00560 #if DEBUG & 1
00561   elf_debug_file (i_ehdrp);
00562 #endif
00563 
00564   /* Reject ET_CORE (header indicates core file, not object file) */
00565   if (i_ehdrp->e_type == ET_CORE)
00566     goto got_wrong_format_error;
00567 
00568   /* If this is a relocatable file and there is no section header
00569      table, then we're hosed.  */
00570   if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_type == ET_REL)
00571     goto got_wrong_format_error;
00572 
00573   /* As a simple sanity check, verify that what BFD thinks is the
00574      size of each section header table entry actually matches the size
00575      recorded in the file, but only if there are any sections.  */
00576   if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
00577     goto got_wrong_format_error;
00578 
00579   /* Further sanity check.  */
00580   if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_shnum != 0)
00581     goto got_wrong_format_error;
00582 
00583   ebd = get_elf_backend_data (abfd);
00584 
00585   /* Check that the ELF e_machine field matches what this particular
00586      BFD format expects.  */
00587   if (ebd->elf_machine_code != i_ehdrp->e_machine
00588       && (ebd->elf_machine_alt1 == 0
00589          || i_ehdrp->e_machine != ebd->elf_machine_alt1)
00590       && (ebd->elf_machine_alt2 == 0
00591          || i_ehdrp->e_machine != ebd->elf_machine_alt2))
00592     {
00593       if (ebd->elf_machine_code != EM_NONE)
00594        goto got_wrong_format_error;
00595 
00596       /* This is the generic ELF target.  Let it match any ELF target
00597         for which we do not have a specific backend.  */
00598       for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
00599        {
00600          const struct elf_backend_data *back;
00601 
00602          if ((*target_ptr)->flavour != bfd_target_elf_flavour)
00603            continue;
00604          back = (const struct elf_backend_data *) (*target_ptr)->backend_data;
00605          if (back->elf_machine_code == i_ehdrp->e_machine
00606              || (back->elf_machine_alt1 != 0
00607                 && back->elf_machine_alt1 == i_ehdrp->e_machine)
00608              || (back->elf_machine_alt2 != 0
00609                 && back->elf_machine_alt2 == i_ehdrp->e_machine))
00610            {
00611              /* target_ptr is an ELF backend which matches this
00612                object file, so reject the generic ELF target.  */
00613              goto got_wrong_format_error;
00614            }
00615        }
00616     }
00617 
00618   if (i_ehdrp->e_type == ET_EXEC)
00619     abfd->flags |= EXEC_P;
00620   else if (i_ehdrp->e_type == ET_DYN)
00621     abfd->flags |= DYNAMIC;
00622 
00623   if (i_ehdrp->e_phnum > 0)
00624     abfd->flags |= D_PAGED;
00625 
00626   if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
00627     {
00628       /* It's OK if this fails for the generic target.  */
00629       if (ebd->elf_machine_code != EM_NONE)
00630        goto got_no_match;
00631     }
00632 
00633   if (ebd->elf_machine_code != EM_NONE
00634       && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
00635     {
00636       if (ebd->elf_osabi != ELFOSABI_NONE)
00637        goto got_wrong_format_error;
00638 
00639       /* This is an ELFOSABI_NONE ELF target.  Let it match any ELF
00640         target of the compatible machine for which we do not have a
00641         backend with matching ELFOSABI.  */
00642       for (target_ptr = bfd_target_vector;
00643           *target_ptr != NULL;
00644           target_ptr++)
00645        {
00646          const struct elf_backend_data *back;
00647 
00648          /* Skip this target and targets with incompatible byte
00649             order.  */
00650          if (*target_ptr == target
00651              || (*target_ptr)->flavour != bfd_target_elf_flavour
00652              || (*target_ptr)->byteorder != target->byteorder
00653              || ((*target_ptr)->header_byteorder
00654                 != target->header_byteorder))
00655            continue;
00656 
00657          back = (const struct elf_backend_data *) (*target_ptr)->backend_data;
00658          if (back->elf_osabi == i_ehdrp->e_ident[EI_OSABI]
00659              && (back->elf_machine_code == i_ehdrp->e_machine
00660                 || (back->elf_machine_alt1 != 0
00661                     && back->elf_machine_alt1 == i_ehdrp->e_machine)
00662                 || (back->elf_machine_alt2 != 0
00663                     && back->elf_machine_alt2 == i_ehdrp->e_machine)))
00664            {
00665              /* target_ptr is an ELF backend which matches this
00666                object file, so reject the ELFOSABI_NONE ELF target.  */
00667              goto got_wrong_format_error;
00668            }
00669        }
00670     }
00671 
00672   if (i_ehdrp->e_shoff != 0)
00673     {
00674       bfd_signed_vma where = i_ehdrp->e_shoff;
00675 
00676       if (where != (file_ptr) where)
00677        goto got_wrong_format_error;
00678 
00679       /* Seek to the section header table in the file.  */
00680       if (bfd_seek (abfd, (file_ptr) where, SEEK_SET) != 0)
00681        goto got_no_match;
00682 
00683       /* Read the first section header at index 0, and convert to internal
00684         form.  */
00685       if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
00686        goto got_no_match;
00687       elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
00688 
00689       /* If the section count is zero, the actual count is in the first
00690         section header.  */
00691       if (i_ehdrp->e_shnum == SHN_UNDEF)
00692        {
00693          i_ehdrp->e_shnum = i_shdr.sh_size;
00694          if (i_ehdrp->e_shnum != i_shdr.sh_size
00695              || i_ehdrp->e_shnum == 0)
00696            goto got_wrong_format_error;
00697        }
00698 
00699       /* And similarly for the string table index.  */
00700       if (i_ehdrp->e_shstrndx == SHN_XINDEX)
00701        {
00702          i_ehdrp->e_shstrndx = i_shdr.sh_link;
00703          if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
00704            goto got_wrong_format_error;
00705        }
00706 
00707       /* Sanity check that we can read all of the section headers.
00708         It ought to be good enough to just read the last one.  */
00709       if (i_ehdrp->e_shnum != 1)
00710        {
00711          /* Check that we don't have a totally silly number of sections.  */
00712          if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
00713              || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
00714            goto got_wrong_format_error;
00715 
00716          where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
00717          if (where != (file_ptr) where)
00718            goto got_wrong_format_error;
00719          if ((bfd_size_type) where <= i_ehdrp->e_shoff)
00720            goto got_wrong_format_error;
00721 
00722          if (bfd_seek (abfd, (file_ptr) where, SEEK_SET) != 0)
00723            goto got_no_match;
00724          if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
00725            goto got_no_match;
00726 
00727          /* Back to where we were.  */
00728          where = i_ehdrp->e_shoff + sizeof (x_shdr);
00729          if (bfd_seek (abfd, (file_ptr) where, SEEK_SET) != 0)
00730            goto got_no_match;
00731        }
00732     }
00733 
00734   /* Allocate space for a copy of the section header table in
00735      internal form.  */
00736   if (i_ehdrp->e_shnum != 0)
00737     {
00738       Elf_Internal_Shdr *shdrp;
00739       unsigned int num_sec;
00740 
00741       amt = sizeof (*i_shdrp) * i_ehdrp->e_shnum;
00742       i_shdrp = bfd_alloc (abfd, amt);
00743       if (!i_shdrp)
00744        goto got_no_match;
00745       num_sec = i_ehdrp->e_shnum;
00746       if (num_sec > SHN_LORESERVE)
00747        num_sec += SHN_HIRESERVE + 1 - SHN_LORESERVE;
00748       elf_numsections (abfd) = num_sec;
00749       amt = sizeof (i_shdrp) * num_sec;
00750       elf_elfsections (abfd) = bfd_alloc (abfd, amt);
00751       if (!elf_elfsections (abfd))
00752        goto got_no_match;
00753 
00754       memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
00755       shdrp = i_shdrp;
00756       shindex = 0;
00757       if (num_sec > SHN_LORESERVE)
00758        {
00759          for ( ; shindex < SHN_LORESERVE; shindex++)
00760            elf_elfsections (abfd)[shindex] = shdrp++;
00761          for ( ; shindex < SHN_HIRESERVE + 1; shindex++)
00762            elf_elfsections (abfd)[shindex] = i_shdrp;
00763        }
00764       for ( ; shindex < num_sec; shindex++)
00765        elf_elfsections (abfd)[shindex] = shdrp++;
00766 
00767       /* Read in the rest of the section header table and convert it
00768         to internal form.  */
00769       for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
00770        {
00771          if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
00772            goto got_no_match;
00773          elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
00774 
00775          /* Sanity check sh_link and sh_info.  */
00776          if (! valid_section_index_p (i_shdrp[shindex].sh_link, num_sec))
00777            goto got_wrong_format_error;
00778 
00779          if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
00780               || i_shdrp[shindex].sh_type == SHT_RELA
00781               || i_shdrp[shindex].sh_type == SHT_REL)
00782              && ! valid_section_index_p (i_shdrp[shindex].sh_info, num_sec))
00783            goto got_wrong_format_error;
00784 
00785          /* If the section is loaded, but not page aligned, clear
00786             D_PAGED.  */
00787          if (i_shdrp[shindex].sh_size != 0
00788              && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
00789              && i_shdrp[shindex].sh_type != SHT_NOBITS
00790              && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
00791                  % ebd->minpagesize)
00792                 != 0))
00793            abfd->flags &= ~D_PAGED;
00794        }
00795     }
00796 
00797   /* A further sanity check.  */
00798   if (i_ehdrp->e_shnum != 0)
00799     {
00800       if (! valid_section_index_p (i_ehdrp->e_shstrndx, elf_numsections (abfd)))
00801        {
00802          /* PR 2257:
00803             We used to just goto got_wrong_format_error here
00804             but there are binaries in existance for which this test
00805             will prevent the binutils from working with them at all.
00806             So we are kind, and reset the string index value to 0
00807             so that at least some processing can be done.  */
00808          i_ehdrp->e_shstrndx = SHN_UNDEF;
00809          _bfd_error_handler (_("warning: %s has a corrupt string table index - ignoring"), abfd->filename);
00810        }
00811     }
00812   else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
00813     goto got_wrong_format_error;
00814 
00815   /* Read in the program headers.  */
00816   if (i_ehdrp->e_phnum == 0)
00817     elf_tdata (abfd)->phdr = NULL;
00818   else
00819     {
00820       Elf_Internal_Phdr *i_phdr;
00821       unsigned int i;
00822 
00823       amt = i_ehdrp->e_phnum * sizeof (Elf_Internal_Phdr);
00824       elf_tdata (abfd)->phdr = bfd_alloc (abfd, amt);
00825       if (elf_tdata (abfd)->phdr == NULL)
00826        goto got_no_match;
00827       if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
00828        goto got_no_match;
00829       i_phdr = elf_tdata (abfd)->phdr;
00830       for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
00831        {
00832          Elf_External_Phdr x_phdr;
00833 
00834          if (bfd_bread (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
00835            goto got_no_match;
00836          elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
00837        }
00838     }
00839 
00840   if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff != 0)
00841     {
00842       unsigned int num_sec;
00843 
00844       /* Once all of the section headers have been read and converted, we
00845         can start processing them.  Note that the first section header is
00846         a dummy placeholder entry, so we ignore it.  */
00847       num_sec = elf_numsections (abfd);
00848       for (shindex = 1; shindex < num_sec; shindex++)
00849        {
00850          if (! bfd_section_from_shdr (abfd, shindex))
00851            goto got_no_match;
00852          if (shindex == SHN_LORESERVE - 1)
00853            shindex += SHN_HIRESERVE + 1 - SHN_LORESERVE;
00854        }
00855 
00856       /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
00857       if (! _bfd_elf_setup_sections (abfd))
00858        goto got_wrong_format_error;
00859     }
00860 
00861   /* Let the backend double check the format and override global
00862      information.  */
00863   if (ebd->elf_backend_object_p)
00864     {
00865       if (! (*ebd->elf_backend_object_p) (abfd))
00866        goto got_wrong_format_error;
00867     }
00868 
00869   /* Remember the entry point specified in the ELF file header.  */
00870   bfd_set_start_address (abfd, i_ehdrp->e_entry);
00871 
00872   /* If we have created any reloc sections that are associated with
00873      debugging sections, mark the reloc sections as debugging as well.  */
00874   for (s = abfd->sections; s != NULL; s = s->next)
00875     {
00876       if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
00877           || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
00878          && elf_section_data (s)->this_hdr.sh_info > 0)
00879        {
00880          unsigned long targ_index;
00881          asection *targ_sec;
00882 
00883          targ_index = elf_section_data (s)->this_hdr.sh_info;
00884          targ_sec = bfd_section_from_elf_index (abfd, targ_index);
00885          if (targ_sec != NULL
00886              && (targ_sec->flags & SEC_DEBUGGING) != 0)
00887            s->flags |= SEC_DEBUGGING;
00888        }
00889     }
00890 
00891   bfd_preserve_finish (abfd, &preserve);
00892   return target;
00893 
00894  got_wrong_format_error:
00895   /* There is way too much undoing of half-known state here.  The caller,
00896      bfd_check_format_matches, really shouldn't iterate on live bfd's to
00897      check match/no-match like it does.  We have to rely on that a call to
00898      bfd_default_set_arch_mach with the previously known mach, undoes what
00899      was done by the first bfd_default_set_arch_mach (with mach 0) here.
00900      For this to work, only elf-data and the mach may be changed by the
00901      target-specific elf_backend_object_p function.  Note that saving the
00902      whole bfd here and restoring it would be even worse; the first thing
00903      you notice is that the cached bfd file position gets out of sync.  */
00904   bfd_set_error (bfd_error_wrong_format);
00905 
00906  got_no_match:
00907   if (preserve.marker != NULL)
00908     bfd_preserve_restore (abfd, &preserve);
00909   return NULL;
00910 }
00911 
00912 /* ELF .o/exec file writing */
00913 
00914 /* Write out the relocs.  */
00915 
00916 void
00917 elf_write_relocs (bfd *abfd, asection *sec, void *data)
00918 {
00919   bfd_boolean *failedp = data;
00920   Elf_Internal_Shdr *rela_hdr;
00921   bfd_vma addr_offset;
00922   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
00923   size_t extsize;
00924   bfd_byte *dst_rela;
00925   unsigned int idx;
00926   asymbol *last_sym;
00927   int last_sym_idx;
00928 
00929   /* If we have already failed, don't do anything.  */
00930   if (*failedp)
00931     return;
00932 
00933   if ((sec->flags & SEC_RELOC) == 0)
00934     return;
00935 
00936   /* The linker backend writes the relocs out itself, and sets the
00937      reloc_count field to zero to inhibit writing them here.  Also,
00938      sometimes the SEC_RELOC flag gets set even when there aren't any
00939      relocs.  */
00940   if (sec->reloc_count == 0)
00941     return;
00942 
00943   /* If we have opened an existing file for update, reloc_count may be
00944      set even though we are not linking.  In that case we have nothing
00945      to do.  */
00946   if (sec->orelocation == NULL)
00947     return;
00948 
00949   rela_hdr = &elf_section_data (sec)->rel_hdr;
00950 
00951   rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
00952   rela_hdr->contents = bfd_alloc (abfd, rela_hdr->sh_size);
00953   if (rela_hdr->contents == NULL)
00954     {
00955       *failedp = TRUE;
00956       return;
00957     }
00958 
00959   /* Figure out whether the relocations are RELA or REL relocations.  */
00960   if (rela_hdr->sh_type == SHT_RELA)
00961     {
00962       swap_out = elf_swap_reloca_out;
00963       extsize = sizeof (Elf_External_Rela);
00964     }
00965   else if (rela_hdr->sh_type == SHT_REL)
00966     {
00967       swap_out = elf_swap_reloc_out;
00968       extsize = sizeof (Elf_External_Rel);
00969     }
00970   else
00971     /* Every relocation section should be either an SHT_RELA or an
00972        SHT_REL section.  */
00973     abort ();
00974 
00975   /* The address of an ELF reloc is section relative for an object
00976      file, and absolute for an executable file or shared library.
00977      The address of a BFD reloc is always section relative.  */
00978   addr_offset = 0;
00979   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
00980     addr_offset = sec->vma;
00981 
00982   /* orelocation has the data, reloc_count has the count...  */
00983   last_sym = 0;
00984   last_sym_idx = 0;
00985   dst_rela = rela_hdr->contents;
00986 
00987   for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
00988     {
00989       Elf_Internal_Rela src_rela;
00990       arelent *ptr;
00991       asymbol *sym;
00992       int n;
00993 
00994       ptr = sec->orelocation[idx];
00995       sym = *ptr->sym_ptr_ptr;
00996       if (sym == last_sym)
00997        n = last_sym_idx;
00998       else if (bfd_is_abs_section (sym->section) && sym->value == 0)
00999        n = STN_UNDEF;
01000       else
01001        {
01002          last_sym = sym;
01003          n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
01004          if (n < 0)
01005            {
01006              *failedp = TRUE;
01007              return;
01008            }
01009          last_sym_idx = n;
01010        }
01011 
01012       if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
01013          && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
01014          && ! _bfd_elf_validate_reloc (abfd, ptr))
01015        {
01016          *failedp = TRUE;
01017          return;
01018        }
01019 
01020       src_rela.r_offset = ptr->address + addr_offset;
01021       src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
01022       src_rela.r_addend = ptr->addend;
01023       (*swap_out) (abfd, &src_rela, dst_rela);
01024     }
01025 }
01026 
01027 /* Write out the program headers.  */
01028 
01029 int
01030 elf_write_out_phdrs (bfd *abfd,
01031                    const Elf_Internal_Phdr *phdr,
01032                    unsigned int count)
01033 {
01034   while (count--)
01035     {
01036       Elf_External_Phdr extphdr;
01037       elf_swap_phdr_out (abfd, phdr, &extphdr);
01038       if (bfd_bwrite (&extphdr, sizeof (Elf_External_Phdr), abfd)
01039          != sizeof (Elf_External_Phdr))
01040        return -1;
01041       phdr++;
01042     }
01043   return 0;
01044 }
01045 
01046 /* Write out the section headers and the ELF file header.  */
01047 
01048 bfd_boolean
01049 elf_write_shdrs_and_ehdr (bfd *abfd)
01050 {
01051   Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
01052   Elf_Internal_Ehdr *i_ehdrp;      /* Elf file header, internal form */
01053   Elf_External_Shdr *x_shdrp;      /* Section header table, external form */
01054   Elf_Internal_Shdr **i_shdrp;     /* Section header table, internal form */
01055   unsigned int count;
01056   bfd_size_type amt;
01057 
01058   i_ehdrp = elf_elfheader (abfd);
01059   i_shdrp = elf_elfsections (abfd);
01060 
01061   /* swap the header before spitting it out...  */
01062 
01063 #if DEBUG & 1
01064   elf_debug_file (i_ehdrp);
01065 #endif
01066   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
01067   amt = sizeof (x_ehdr);
01068   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
01069       || bfd_bwrite (&x_ehdr, amt, abfd) != amt)
01070     return FALSE;
01071 
01072   /* Some fields in the first section header handle overflow of ehdr
01073      fields.  */
01074   if (i_ehdrp->e_shnum >= SHN_LORESERVE)
01075     i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
01076   if (i_ehdrp->e_shstrndx >= SHN_LORESERVE)
01077     i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
01078 
01079   /* at this point we've concocted all the ELF sections...  */
01080   amt = i_ehdrp->e_shnum;
01081   amt *= sizeof (*x_shdrp);
01082   x_shdrp = bfd_alloc (abfd, amt);
01083   if (!x_shdrp)
01084     return FALSE;
01085 
01086   for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
01087     {
01088 #if DEBUG & 2
01089       elf_debug_section (count, *i_shdrp);
01090 #endif
01091       elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
01092 
01093       if (count == SHN_LORESERVE - 1)
01094        i_shdrp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
01095     }
01096   if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
01097       || bfd_bwrite (x_shdrp, amt, abfd) != amt)
01098     return FALSE;
01099 
01100   /* need to dump the string table too...  */
01101 
01102   return TRUE;
01103 }
01104 
01105 long
01106 elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic)
01107 {
01108   Elf_Internal_Shdr *hdr;
01109   Elf_Internal_Shdr *verhdr;
01110   unsigned long symcount;   /* Number of external ELF symbols */
01111   elf_symbol_type *sym;            /* Pointer to current bfd symbol */
01112   elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
01113   Elf_Internal_Sym *isym;
01114   Elf_Internal_Sym *isymend;
01115   Elf_Internal_Sym *isymbuf = NULL;
01116   Elf_External_Versym *xver;
01117   Elf_External_Versym *xverbuf = NULL;
01118   const struct elf_backend_data *ebd;
01119   bfd_size_type amt;
01120 
01121   /* Read each raw ELF symbol, converting from external ELF form to
01122      internal ELF form, and then using the information to create a
01123      canonical bfd symbol table entry.
01124 
01125      Note that we allocate the initial bfd canonical symbol buffer
01126      based on a one-to-one mapping of the ELF symbols to canonical
01127      symbols.  We actually use all the ELF symbols, so there will be no
01128      space left over at the end.  When we have all the symbols, we
01129      build the caller's pointer vector.  */
01130 
01131   if (! dynamic)
01132     {
01133       hdr = &elf_tdata (abfd)->symtab_hdr;
01134       verhdr = NULL;
01135     }
01136   else
01137     {
01138       hdr = &elf_tdata (abfd)->dynsymtab_hdr;
01139       if (elf_dynversym (abfd) == 0)
01140        verhdr = NULL;
01141       else
01142        verhdr = &elf_tdata (abfd)->dynversym_hdr;
01143       if ((elf_tdata (abfd)->dynverdef_section != 0
01144           && elf_tdata (abfd)->verdef == NULL)
01145          || (elf_tdata (abfd)->dynverref_section != 0
01146              && elf_tdata (abfd)->verref == NULL))
01147        {
01148          if (!_bfd_elf_slurp_version_tables (abfd, FALSE))
01149            return -1;
01150        }
01151     }
01152 
01153   ebd = get_elf_backend_data (abfd);
01154   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
01155   if (symcount == 0)
01156     sym = symbase = NULL;
01157   else
01158     {
01159       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
01160                                   NULL, NULL, NULL);
01161       if (isymbuf == NULL)
01162        return -1;
01163 
01164       amt = symcount;
01165       amt *= sizeof (elf_symbol_type);
01166       symbase = bfd_zalloc (abfd, amt);
01167       if (symbase == (elf_symbol_type *) NULL)
01168        goto error_return;
01169 
01170       /* Read the raw ELF version symbol information.  */
01171       if (verhdr != NULL
01172          && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
01173        {
01174          (*_bfd_error_handler)
01175            (_("%s: version count (%ld) does not match symbol count (%ld)"),
01176             abfd->filename,
01177             (long) (verhdr->sh_size / sizeof (Elf_External_Versym)),
01178             symcount);
01179 
01180          /* Slurp in the symbols without the version information,
01181             since that is more helpful than just quitting.  */
01182          verhdr = NULL;
01183        }
01184 
01185       if (verhdr != NULL)
01186        {
01187          if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
01188            goto error_return;
01189 
01190          xverbuf = bfd_malloc (verhdr->sh_size);
01191          if (xverbuf == NULL && verhdr->sh_size != 0)
01192            goto error_return;
01193 
01194          if (bfd_bread (xverbuf, verhdr->sh_size, abfd) != verhdr->sh_size)
01195            goto error_return;
01196        }
01197 
01198       /* Skip first symbol, which is a null dummy.  */
01199       xver = xverbuf;
01200       if (xver != NULL)
01201        ++xver;
01202       isymend = isymbuf + symcount;
01203       for (isym = isymbuf + 1, sym = symbase; isym < isymend; isym++, sym++)
01204        {
01205          memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
01206          sym->symbol.the_bfd = abfd;
01207 
01208          sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
01209 
01210          sym->symbol.value = isym->st_value;
01211 
01212          if (isym->st_shndx == SHN_UNDEF)
01213            {
01214              sym->symbol.section = bfd_und_section_ptr;
01215            }
01216          else if (isym->st_shndx < SHN_LORESERVE
01217                  || isym->st_shndx > SHN_HIRESERVE)
01218            {
01219              sym->symbol.section = bfd_section_from_elf_index (abfd,
01220                                                         isym->st_shndx);
01221              if (sym->symbol.section == NULL)
01222               {
01223                 /* This symbol is in a section for which we did not
01224                    create a BFD section.  Just use bfd_abs_section,
01225                    although it is wrong.  FIXME.  */
01226                 sym->symbol.section = bfd_abs_section_ptr;
01227               }
01228            }
01229          else if (isym->st_shndx == SHN_ABS)
01230            {
01231              sym->symbol.section = bfd_abs_section_ptr;
01232            }
01233          else if (isym->st_shndx == SHN_COMMON)
01234            {
01235              sym->symbol.section = bfd_com_section_ptr;
01236              /* Elf puts the alignment into the `value' field, and
01237                the size into the `size' field.  BFD wants to see the
01238                size in the value field, and doesn't care (at the
01239                moment) about the alignment.  */
01240              sym->symbol.value = isym->st_size;
01241            }
01242          else
01243            sym->symbol.section = bfd_abs_section_ptr;
01244 
01245          /* If this is a relocatable file, then the symbol value is
01246             already section relative.  */
01247          if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
01248            sym->symbol.value -= sym->symbol.section->vma;
01249 
01250          switch (ELF_ST_BIND (isym->st_info))
01251            {
01252            case STB_LOCAL:
01253              sym->symbol.flags |= BSF_LOCAL;
01254              break;
01255            case STB_GLOBAL:
01256              if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
01257               sym->symbol.flags |= BSF_GLOBAL;
01258              break;
01259            case STB_WEAK:
01260              sym->symbol.flags |= BSF_WEAK;
01261              break;
01262            }
01263 
01264          switch (ELF_ST_TYPE (isym->st_info))
01265            {
01266            case STT_SECTION:
01267              sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
01268              break;
01269            case STT_FILE:
01270              sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
01271              break;
01272            case STT_FUNC:
01273              sym->symbol.flags |= BSF_FUNCTION;
01274              break;
01275            case STT_OBJECT:
01276              sym->symbol.flags |= BSF_OBJECT;
01277              break;
01278            case STT_TLS:
01279              sym->symbol.flags |= BSF_THREAD_LOCAL;
01280              break;
01281            case STT_RELC:
01282              sym->symbol.flags |= BSF_RELC;
01283              break;
01284            case STT_SRELC:
01285              sym->symbol.flags |= BSF_SRELC;
01286              break;
01287            }
01288 
01289          if (dynamic)
01290            sym->symbol.flags |= BSF_DYNAMIC;
01291 
01292          if (xver != NULL)
01293            {
01294              Elf_Internal_Versym iversym;
01295 
01296              _bfd_elf_swap_versym_in (abfd, xver, &iversym);
01297              sym->version = iversym.vs_vers;
01298              xver++;
01299            }
01300 
01301          /* Do some backend-specific processing on this symbol.  */
01302          if (ebd->elf_backend_symbol_processing)
01303            (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
01304        }
01305     }
01306 
01307   /* Do some backend-specific processing on this symbol table.  */
01308   if (ebd->elf_backend_symbol_table_processing)
01309     (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
01310 
01311   /* We rely on the zalloc to clear out the final symbol entry.  */
01312 
01313   symcount = sym - symbase;
01314 
01315   /* Fill in the user's symbol pointer vector if needed.  */
01316   if (symptrs)
01317     {
01318       long l = symcount;
01319 
01320       sym = symbase;
01321       while (l-- > 0)
01322        {
01323          *symptrs++ = &sym->symbol;
01324          sym++;
01325        }
01326       *symptrs = 0;         /* Final null pointer */
01327     }
01328 
01329   if (xverbuf != NULL)
01330     free (xverbuf);
01331   if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
01332     free (isymbuf);
01333   return symcount;
01334 
01335 error_return:
01336   if (xverbuf != NULL)
01337     free (xverbuf);
01338   if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
01339     free (isymbuf);
01340   return -1;
01341 }
01342 
01343 /* Read relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
01344    them.  */
01345 
01346 static bfd_boolean
01347 elf_slurp_reloc_table_from_section (bfd *abfd,
01348                                 asection *asect,
01349                                 Elf_Internal_Shdr *rel_hdr,
01350                                 bfd_size_type reloc_count,
01351                                 arelent *relents,
01352                                 asymbol **symbols,
01353                                 bfd_boolean dynamic)
01354 {
01355   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
01356   void *allocated = NULL;
01357   bfd_byte *native_relocs;
01358   arelent *relent;
01359   unsigned int i;
01360   int entsize;
01361   unsigned int symcount;
01362 
01363   allocated = bfd_malloc (rel_hdr->sh_size);
01364   if (allocated == NULL)
01365     goto error_return;
01366 
01367   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
01368       || (bfd_bread (allocated, rel_hdr->sh_size, abfd)
01369          != rel_hdr->sh_size))
01370     goto error_return;
01371 
01372   native_relocs = allocated;
01373 
01374   entsize = rel_hdr->sh_entsize;
01375   BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
01376              || entsize == sizeof (Elf_External_Rela));
01377 
01378   if (dynamic)
01379     symcount = bfd_get_dynamic_symcount (abfd);
01380   else
01381     symcount = bfd_get_symcount (abfd);
01382 
01383   for (i = 0, relent = relents;
01384        i < reloc_count;
01385        i++, relent++, native_relocs += entsize)
01386     {
01387       Elf_Internal_Rela rela;
01388 
01389       if (entsize == sizeof (Elf_External_Rela))
01390        elf_swap_reloca_in (abfd, native_relocs, &rela);
01391       else
01392        elf_swap_reloc_in (abfd, native_relocs, &rela);
01393 
01394       /* The address of an ELF reloc is section relative for an object
01395         file, and absolute for an executable file or shared library.
01396         The address of a normal BFD reloc is always section relative,
01397         and the address of a dynamic reloc is absolute..  */
01398       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
01399        relent->address = rela.r_offset;
01400       else
01401        relent->address = rela.r_offset - asect->vma;
01402 
01403       if (ELF_R_SYM (rela.r_info) == 0)
01404        relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
01405       else if (ELF_R_SYM (rela.r_info) > symcount)
01406        {
01407          (*_bfd_error_handler)
01408            (_("%s(%s): relocation %d has invalid symbol index %ld"),
01409             abfd->filename, asect->name, i, ELF_R_SYM (rela.r_info));
01410          relent->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
01411        }
01412       else
01413        {
01414          asymbol **ps;
01415 
01416          ps = symbols + ELF_R_SYM (rela.r_info) - 1;
01417 
01418          relent->sym_ptr_ptr = ps;
01419        }
01420 
01421       relent->addend = rela.r_addend;
01422 
01423       if ((entsize == sizeof (Elf_External_Rela)
01424           && ebd->elf_info_to_howto != NULL)
01425          || ebd->elf_info_to_howto_rel == NULL)
01426        (*ebd->elf_info_to_howto) (abfd, relent, &rela);
01427       else
01428        (*ebd->elf_info_to_howto_rel) (abfd, relent, &rela);
01429     }
01430 
01431   if (allocated != NULL)
01432     free (allocated);
01433 
01434   return TRUE;
01435 
01436  error_return:
01437   if (allocated != NULL)
01438     free (allocated);
01439   return FALSE;
01440 }
01441 
01442 /* Read in and swap the external relocs.  */
01443 
01444 bfd_boolean
01445 elf_slurp_reloc_table (bfd *abfd,
01446                      asection *asect,
01447                      asymbol **symbols,
01448                      bfd_boolean dynamic)
01449 {
01450   struct bfd_elf_section_data * const d = elf_section_data (asect);
01451   Elf_Internal_Shdr *rel_hdr;
01452   Elf_Internal_Shdr *rel_hdr2;
01453   bfd_size_type reloc_count;
01454   bfd_size_type reloc_count2;
01455   arelent *relents;
01456   bfd_size_type amt;
01457 
01458   if (asect->relocation != NULL)
01459     return TRUE;
01460 
01461   if (! dynamic)
01462     {
01463       if ((asect->flags & SEC_RELOC) == 0
01464          || asect->reloc_count == 0)
01465        return TRUE;
01466 
01467       rel_hdr = &d->rel_hdr;
01468       reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
01469       rel_hdr2 = d->rel_hdr2;
01470       reloc_count2 = (rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0);
01471 
01472       BFD_ASSERT (asect->reloc_count == reloc_count + reloc_count2);
01473       BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
01474                 || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
01475 
01476     }
01477   else
01478     {
01479       /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
01480         case because relocations against this section may use the
01481         dynamic symbol table, and in that case bfd_section_from_shdr
01482         in elf.c does not update the RELOC_COUNT.  */
01483       if (asect->size == 0)
01484        return TRUE;
01485 
01486       rel_hdr = &d->this_hdr;
01487       reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
01488       rel_hdr2 = NULL;
01489       reloc_count2 = 0;
01490     }
01491 
01492   amt = (reloc_count + reloc_count2) * sizeof (arelent);
01493   relents = bfd_alloc (abfd, amt);
01494   if (relents == NULL)
01495     return FALSE;
01496 
01497   if (!elf_slurp_reloc_table_from_section (abfd, asect,
01498                                       rel_hdr, reloc_count,
01499                                       relents,
01500                                       symbols, dynamic))
01501     return FALSE;
01502 
01503   if (rel_hdr2
01504       && !elf_slurp_reloc_table_from_section (abfd, asect,
01505                                          rel_hdr2, reloc_count2,
01506                                          relents + reloc_count,
01507                                          symbols, dynamic))
01508     return FALSE;
01509 
01510   asect->relocation = relents;
01511   return TRUE;
01512 }
01513 
01514 #if DEBUG & 2
01515 static void
01516 elf_debug_section (int num, Elf_Internal_Shdr *hdr)
01517 {
01518   fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
01519           hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
01520           (long) hdr);
01521   fprintf (stderr,
01522           "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
01523           (long) hdr->sh_name,
01524           (long) hdr->sh_type,
01525           (long) hdr->sh_flags);
01526   fprintf (stderr,
01527           "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
01528           (long) hdr->sh_addr,
01529           (long) hdr->sh_offset,
01530           (long) hdr->sh_size);
01531   fprintf (stderr,
01532           "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
01533           (long) hdr->sh_link,
01534           (long) hdr->sh_info,
01535           (long) hdr->sh_addralign);
01536   fprintf (stderr, "sh_entsize   = %ld\n",
01537           (long) hdr->sh_entsize);
01538   fflush (stderr);
01539 }
01540 #endif
01541 
01542 #if DEBUG & 1
01543 static void
01544 elf_debug_file (Elf_Internal_Ehdr *ehdrp)
01545 {
01546   fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
01547   fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
01548   fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
01549   fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
01550   fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
01551   fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
01552   fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
01553 }
01554 #endif
01555 
01556 /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
01557    reconstruct an ELF file by reading the segments out of remote memory
01558    based on the ELF file header at EHDR_VMA and the ELF program headers it
01559    points to.  If not null, *LOADBASEP is filled in with the difference
01560    between the VMAs from which the segments were read, and the VMAs the
01561    file headers (and hence BFD's idea of each section's VMA) put them at.
01562 
01563    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
01564    remote memory at target address VMA into the local buffer at MYADDR; it
01565    should return zero on success or an `errno' code on failure.  TEMPL must
01566    be a BFD for a target with the word size and byte order found in the
01567    remote memory.  */
01568 
01569 bfd *
01570 NAME(_bfd_elf,bfd_from_remote_memory)
01571   (bfd *templ,
01572    bfd_vma ehdr_vma,
01573    bfd_vma *loadbasep,
01574    int (*target_read_memory) (bfd_vma, bfd_byte *, int))
01575 {
01576   Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
01577   Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
01578   Elf_External_Phdr *x_phdrs;
01579   Elf_Internal_Phdr *i_phdrs, *last_phdr;
01580   bfd *nbfd;
01581   struct bfd_in_memory *bim;
01582   int contents_size;
01583   bfd_byte *contents;
01584   int err;
01585   unsigned int i;
01586   bfd_vma loadbase;
01587 
01588   /* Read in the ELF header in external format.  */
01589   err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
01590   if (err)
01591     {
01592       bfd_set_error (bfd_error_system_call);
01593       errno = err;
01594       return NULL;
01595     }
01596 
01597   /* Now check to see if we have a valid ELF file, and one that BFD can
01598      make use of.  The magic number must match, the address size ('class')
01599      and byte-swapping must match our XVEC entry.  */
01600 
01601   if (! elf_file_p (&x_ehdr)
01602       || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
01603       || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
01604     {
01605       bfd_set_error (bfd_error_wrong_format);
01606       return NULL;
01607     }
01608 
01609   /* Check that file's byte order matches xvec's */
01610   switch (x_ehdr.e_ident[EI_DATA])
01611     {
01612     case ELFDATA2MSB:              /* Big-endian */
01613       if (! bfd_header_big_endian (templ))
01614        {
01615          bfd_set_error (bfd_error_wrong_format);
01616          return NULL;
01617        }
01618       break;
01619     case ELFDATA2LSB:              /* Little-endian */
01620       if (! bfd_header_little_endian (templ))
01621        {
01622          bfd_set_error (bfd_error_wrong_format);
01623          return NULL;
01624        }
01625       break;
01626     case ELFDATANONE:              /* No data encoding specified */
01627     default:                /* Unknown data encoding specified */
01628       bfd_set_error (bfd_error_wrong_format);
01629       return NULL;
01630     }
01631 
01632   elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr);
01633 
01634   /* The file header tells where to find the program headers.
01635      These are what we use to actually choose what to read.  */
01636 
01637   if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
01638     {
01639       bfd_set_error (bfd_error_wrong_format);
01640       return NULL;
01641     }
01642 
01643   x_phdrs = bfd_malloc (i_ehdr.e_phnum * (sizeof *x_phdrs + sizeof *i_phdrs));
01644   if (x_phdrs == NULL)
01645     {
01646       bfd_set_error (bfd_error_no_memory);
01647       return NULL;
01648     }
01649   err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs,
01650                          i_ehdr.e_phnum * sizeof x_phdrs[0]);
01651   if (err)
01652     {
01653       free (x_phdrs);
01654       bfd_set_error (bfd_error_system_call);
01655       errno = err;
01656       return NULL;
01657     }
01658   i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum];
01659 
01660   contents_size = 0;
01661   last_phdr = NULL;
01662   loadbase = ehdr_vma;
01663   for (i = 0; i < i_ehdr.e_phnum; ++i)
01664     {
01665       elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
01666       /* IA-64 vDSO may have two mappings for one segment, where one mapping
01667         is executable only, and one is read only.  We must not use the
01668         executable one.  */
01669       if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
01670        {
01671          bfd_vma segment_end;
01672          segment_end = (i_phdrs[i].p_offset + i_phdrs[i].p_filesz
01673                       + i_phdrs[i].p_align - 1) & -i_phdrs[i].p_align;
01674          if (segment_end > (bfd_vma) contents_size)
01675            contents_size = segment_end;
01676 
01677          if ((i_phdrs[i].p_offset & -i_phdrs[i].p_align) == 0)
01678            loadbase = ehdr_vma - (i_phdrs[i].p_vaddr & -i_phdrs[i].p_align);
01679 
01680          last_phdr = &i_phdrs[i];
01681        }
01682     }
01683   if (last_phdr == NULL)
01684     {
01685       /* There were no PT_LOAD segments, so we don't have anything to read.  */
01686       free (x_phdrs);
01687       bfd_set_error (bfd_error_wrong_format);
01688       return NULL;
01689     }
01690 
01691   /* Trim the last segment so we don't bother with zeros in the last page
01692      that are off the end of the file.  However, if the extra bit in that
01693      page includes the section headers, keep them.  */
01694   if ((bfd_vma) contents_size > last_phdr->p_offset + last_phdr->p_filesz
01695       && (bfd_vma) contents_size >= (i_ehdr.e_shoff
01696                                  + i_ehdr.e_shnum * i_ehdr.e_shentsize))
01697     {
01698       contents_size = last_phdr->p_offset + last_phdr->p_filesz;
01699       if ((bfd_vma) contents_size < (i_ehdr.e_shoff
01700                                  + i_ehdr.e_shnum * i_ehdr.e_shentsize))
01701        contents_size = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
01702     }
01703   else
01704     contents_size = last_phdr->p_offset + last_phdr->p_filesz;
01705 
01706   /* Now we know the size of the whole image we want read in.  */
01707   contents = bfd_zmalloc (contents_size);
01708   if (contents == NULL)
01709     {
01710       free (x_phdrs);
01711       bfd_set_error (bfd_error_no_memory);
01712       return NULL;
01713     }
01714 
01715   for (i = 0; i < i_ehdr.e_phnum; ++i)
01716     /* IA-64 vDSO may have two mappings for one segment, where one mapping
01717        is executable only, and one is read only.  We must not use the
01718        executable one.  */
01719     if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
01720       {
01721        bfd_vma start = i_phdrs[i].p_offset & -i_phdrs[i].p_align;
01722        bfd_vma end = (i_phdrs[i].p_offset + i_phdrs[i].p_filesz
01723                      + i_phdrs[i].p_align - 1) & -i_phdrs[i].p_align;
01724        if (end > (bfd_vma) contents_size)
01725          end = contents_size;
01726        err = target_read_memory ((loadbase + i_phdrs[i].p_vaddr)
01727                               & -i_phdrs[i].p_align,
01728                               contents + start, end - start);
01729        if (err)
01730          {
01731            free (x_phdrs);
01732            free (contents);
01733            bfd_set_error (bfd_error_system_call);
01734            errno = err;
01735            return NULL;
01736          }
01737       }
01738   free (x_phdrs);
01739 
01740   /* If the segments visible in memory didn't include the section headers,
01741      then clear them from the file header.  */
01742   if ((bfd_vma) contents_size < (i_ehdr.e_shoff
01743                              + i_ehdr.e_shnum * i_ehdr.e_shentsize))
01744     {
01745       memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
01746       memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);
01747       memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx);
01748     }
01749 
01750   /* This will normally have been in the first PT_LOAD segment.  But it
01751      conceivably could be missing, and we might have just changed it.  */
01752   memcpy (contents, &x_ehdr, sizeof x_ehdr);
01753 
01754   /* Now we have a memory image of the ELF file contents.  Make a BFD.  */
01755   bim = bfd_malloc (sizeof (struct bfd_in_memory));
01756   if (bim == NULL)
01757     {
01758       free (contents);
01759       bfd_set_error (bfd_error_no_memory);
01760       return NULL;
01761     }
01762   nbfd = _bfd_new_bfd ();
01763   if (nbfd == NULL)
01764     {
01765       free (bim);
01766       free (contents);
01767       bfd_set_error (bfd_error_no_memory);
01768       return NULL;
01769     }
01770   nbfd->filename = "<in-memory>";
01771   nbfd->xvec = templ->xvec;
01772   bim->size = contents_size;
01773   bim->buffer = contents;
01774   nbfd->iostream = bim;
01775   nbfd->flags = BFD_IN_MEMORY;
01776   nbfd->direction = read_direction;
01777   nbfd->mtime = time (NULL);
01778   nbfd->mtime_set = TRUE;
01779 
01780   if (loadbasep)
01781     *loadbasep = loadbase;
01782   return nbfd;
01783 }
01784 
01785 #include "elfcore.h"
01786 
01787 /* Size-dependent data and functions.  */
01788 const struct elf_size_info NAME(_bfd_elf,size_info) = {
01789   sizeof (Elf_External_Ehdr),
01790   sizeof (Elf_External_Phdr),
01791   sizeof (Elf_External_Shdr),
01792   sizeof (Elf_External_Rel),
01793   sizeof (Elf_External_Rela),
01794   sizeof (Elf_External_Sym),
01795   sizeof (Elf_External_Dyn),
01796   sizeof (Elf_External_Note),
01797   4,
01798   1,
01799   ARCH_SIZE, LOG_FILE_ALIGN,
01800   ELFCLASS, EV_CURRENT,
01801   elf_write_out_phdrs,
01802   elf_write_shdrs_and_ehdr,
01803   elf_write_relocs,
01804   elf_swap_symbol_in,
01805   elf_swap_symbol_out,
01806   elf_slurp_reloc_table,
01807   elf_slurp_symbol_table,
01808   elf_swap_dyn_in,
01809   elf_swap_dyn_out,
01810   elf_swap_reloc_in,
01811   elf_swap_reloc_out,
01812   elf_swap_reloca_in,
01813   elf_swap_reloca_out
01814 };