133965Sjdp/* ELF executable support for BFD. 278828Sobrien Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3218822Sdim 2001, 2002, 2003, 2004, 2005, 2006, 2007 4218822Sdim Free Software Foundation, Inc. 533965Sjdp 633965Sjdp Written by Fred Fish @ Cygnus Support, from information published 733965Sjdp in "UNIX System V Release 4, Programmers Guide: ANSI C and 833965Sjdp Programming Support Tools". Sufficient support for gdb. 933965Sjdp 1033965Sjdp Rewritten by Mark Eichin @ Cygnus Support, from information 1133965Sjdp published in "System V Application Binary Interface", chapters 4 1233965Sjdp and 5, as well as the various "Processor Supplement" documents 1333965Sjdp derived from it. Added support for assembler and other object file 1433965Sjdp utilities. Further work done by Ken Raeburn (Cygnus Support), Michael 1533965Sjdp Meissner (Open Software Foundation), and Peter Hoogenboom (University 1633965Sjdp of Utah) to finish and extend this. 1733965Sjdp 1833965SjdpThis file is part of BFD, the Binary File Descriptor library. 1933965Sjdp 2033965SjdpThis program is free software; you can redistribute it and/or modify 2133965Sjdpit under the terms of the GNU General Public License as published by 2233965Sjdpthe Free Software Foundation; either version 2 of the License, or 2333965Sjdp(at your option) any later version. 2433965Sjdp 2533965SjdpThis program is distributed in the hope that it will be useful, 2633965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of 2733965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2833965SjdpGNU General Public License for more details. 2933965Sjdp 3033965SjdpYou should have received a copy of the GNU General Public License 3133965Sjdpalong with this program; if not, write to the Free Software 32218822SdimFoundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 3333965Sjdp 3433965Sjdp/* Problems and other issues to resolve. 3533965Sjdp 3633965Sjdp (1) BFD expects there to be some fixed number of "sections" in 37218822Sdim the object file. I.E. there is a "section_count" variable in the 3833965Sjdp bfd structure which contains the number of sections. However, ELF 3933965Sjdp supports multiple "views" of a file. In particular, with current 4033965Sjdp implementations, executable files typically have two tables, a 4133965Sjdp program header table and a section header table, both of which 4233965Sjdp partition the executable. 4333965Sjdp 4433965Sjdp In ELF-speak, the "linking view" of the file uses the section header 4533965Sjdp table to access "sections" within the file, and the "execution view" 4633965Sjdp uses the program header table to access "segments" within the file. 4733965Sjdp "Segments" typically may contain all the data from one or more 4833965Sjdp "sections". 4933965Sjdp 5033965Sjdp Note that the section header table is optional in ELF executables, 5133965Sjdp but it is this information that is most useful to gdb. If the 5233965Sjdp section header table is missing, then gdb should probably try 5333965Sjdp to make do with the program header table. (FIXME) 5433965Sjdp 5533965Sjdp (2) The code in this file is compiled twice, once in 32-bit mode and 5633965Sjdp once in 64-bit mode. More of it should be made size-independent 5733965Sjdp and moved into elf.c. 5833965Sjdp 5933965Sjdp (3) ELF section symbols are handled rather sloppily now. This should 6033965Sjdp be cleaned up, and ELF section symbols reconciled with BFD section 6133965Sjdp symbols. 6233965Sjdp 6333965Sjdp (4) We need a published spec for 64-bit ELF. We've got some stuff here 6433965Sjdp that we're using for SPARC V9 64-bit chips, but don't assume that 6533965Sjdp it's cast in stone. 6633965Sjdp */ 6733965Sjdp 68218822Sdim#include "sysdep.h" 6933965Sjdp#include "bfd.h" 7089857Sobrien#include "libiberty.h" 7133965Sjdp#include "bfdlink.h" 7233965Sjdp#include "libbfd.h" 7333965Sjdp#include "elf-bfd.h" 7433965Sjdp 7533965Sjdp/* Renaming structures, typedefs, macros and functions to be size-specific. */ 7633965Sjdp#define Elf_External_Ehdr NAME(Elf,External_Ehdr) 7733965Sjdp#define Elf_External_Sym NAME(Elf,External_Sym) 7833965Sjdp#define Elf_External_Shdr NAME(Elf,External_Shdr) 7933965Sjdp#define Elf_External_Phdr NAME(Elf,External_Phdr) 8033965Sjdp#define Elf_External_Rel NAME(Elf,External_Rel) 8133965Sjdp#define Elf_External_Rela NAME(Elf,External_Rela) 8233965Sjdp#define Elf_External_Dyn NAME(Elf,External_Dyn) 8333965Sjdp 8433965Sjdp#define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command) 8533965Sjdp#define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal) 8633965Sjdp#define elf_core_file_matches_executable_p \ 8733965Sjdp NAME(bfd_elf,core_file_matches_executable_p) 8833965Sjdp#define elf_object_p NAME(bfd_elf,object_p) 8933965Sjdp#define elf_core_file_p NAME(bfd_elf,core_file_p) 9033965Sjdp#define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound) 9133965Sjdp#define elf_get_dynamic_symtab_upper_bound \ 9233965Sjdp NAME(bfd_elf,get_dynamic_symtab_upper_bound) 9333965Sjdp#define elf_swap_reloc_in NAME(bfd_elf,swap_reloc_in) 9433965Sjdp#define elf_swap_reloca_in NAME(bfd_elf,swap_reloca_in) 9533965Sjdp#define elf_swap_reloc_out NAME(bfd_elf,swap_reloc_out) 9633965Sjdp#define elf_swap_reloca_out NAME(bfd_elf,swap_reloca_out) 9733965Sjdp#define elf_swap_symbol_in NAME(bfd_elf,swap_symbol_in) 9833965Sjdp#define elf_swap_symbol_out NAME(bfd_elf,swap_symbol_out) 9933965Sjdp#define elf_swap_phdr_in NAME(bfd_elf,swap_phdr_in) 10033965Sjdp#define elf_swap_phdr_out NAME(bfd_elf,swap_phdr_out) 10133965Sjdp#define elf_swap_dyn_in NAME(bfd_elf,swap_dyn_in) 10233965Sjdp#define elf_swap_dyn_out NAME(bfd_elf,swap_dyn_out) 10333965Sjdp#define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound) 10433965Sjdp#define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc) 10533965Sjdp#define elf_slurp_symbol_table NAME(bfd_elf,slurp_symbol_table) 106130561Sobrien#define elf_canonicalize_symtab NAME(bfd_elf,canonicalize_symtab) 10733965Sjdp#define elf_canonicalize_dynamic_symtab \ 10833965Sjdp NAME(bfd_elf,canonicalize_dynamic_symtab) 109218822Sdim#define elf_get_synthetic_symtab \ 110218822Sdim NAME(bfd_elf,get_synthetic_symtab) 11133965Sjdp#define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol) 11233965Sjdp#define elf_get_symbol_info NAME(bfd_elf,get_symbol_info) 11333965Sjdp#define elf_get_lineno NAME(bfd_elf,get_lineno) 11433965Sjdp#define elf_set_arch_mach NAME(bfd_elf,set_arch_mach) 11533965Sjdp#define elf_find_nearest_line NAME(bfd_elf,find_nearest_line) 11633965Sjdp#define elf_sizeof_headers NAME(bfd_elf,sizeof_headers) 11733965Sjdp#define elf_set_section_contents NAME(bfd_elf,set_section_contents) 11833965Sjdp#define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto) 11933965Sjdp#define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel) 12033965Sjdp#define elf_find_section NAME(bfd_elf,find_section) 12133965Sjdp#define elf_write_shdrs_and_ehdr NAME(bfd_elf,write_shdrs_and_ehdr) 12233965Sjdp#define elf_write_out_phdrs NAME(bfd_elf,write_out_phdrs) 12377298Sobrien#define elf_write_relocs NAME(bfd_elf,write_relocs) 12477298Sobrien#define elf_slurp_reloc_table NAME(bfd_elf,slurp_reloc_table) 12533965Sjdp 12633965Sjdp#if ARCH_SIZE == 64 12733965Sjdp#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y) 12833965Sjdp#define ELF_R_SYM(X) ELF64_R_SYM(X) 12933965Sjdp#define ELF_R_TYPE(X) ELF64_R_TYPE(X) 13033965Sjdp#define ELFCLASS ELFCLASS64 13133965Sjdp#define FILE_ALIGN 8 13233965Sjdp#define LOG_FILE_ALIGN 3 13333965Sjdp#endif 13433965Sjdp#if ARCH_SIZE == 32 13533965Sjdp#define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y) 13633965Sjdp#define ELF_R_SYM(X) ELF32_R_SYM(X) 13733965Sjdp#define ELF_R_TYPE(X) ELF32_R_TYPE(X) 13833965Sjdp#define ELFCLASS ELFCLASS32 13933965Sjdp#define FILE_ALIGN 4 14033965Sjdp#define LOG_FILE_ALIGN 2 14133965Sjdp#endif 14233965Sjdp 143218822Sdim#if DEBUG & 2 144130561Sobrienstatic void elf_debug_section (int, Elf_Internal_Shdr *); 145218822Sdim#endif 146218822Sdim#if DEBUG & 1 147130561Sobrienstatic void elf_debug_file (Elf_Internal_Ehdr *); 14833965Sjdp#endif 14933965Sjdp 15033965Sjdp/* Structure swapping routines */ 15133965Sjdp 15233965Sjdp/* Should perhaps use put_offset, put_word, etc. For now, the two versions 15333965Sjdp can be handled by explicitly specifying 32 bits or "the long type". */ 15433965Sjdp#if ARCH_SIZE == 64 15589857Sobrien#define H_PUT_WORD H_PUT_64 15689857Sobrien#define H_PUT_SIGNED_WORD H_PUT_S64 15789857Sobrien#define H_GET_WORD H_GET_64 15889857Sobrien#define H_GET_SIGNED_WORD H_GET_S64 15933965Sjdp#endif 16033965Sjdp#if ARCH_SIZE == 32 16189857Sobrien#define H_PUT_WORD H_PUT_32 16289857Sobrien#define H_PUT_SIGNED_WORD H_PUT_S32 16389857Sobrien#define H_GET_WORD H_GET_32 16489857Sobrien#define H_GET_SIGNED_WORD H_GET_S32 16533965Sjdp#endif 16633965Sjdp 16733965Sjdp/* Translate an ELF symbol in external format into an ELF symbol in internal 16877298Sobrien format. */ 16933965Sjdp 170218822Sdimbfd_boolean 171130561Sobrienelf_swap_symbol_in (bfd *abfd, 172130561Sobrien const void *psrc, 173130561Sobrien const void *pshn, 174130561Sobrien Elf_Internal_Sym *dst) 17533965Sjdp{ 176130561Sobrien const Elf_External_Sym *src = psrc; 177130561Sobrien const Elf_External_Sym_Shndx *shndx = pshn; 17860484Sobrien int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma; 17960484Sobrien 18089857Sobrien dst->st_name = H_GET_32 (abfd, src->st_name); 18160484Sobrien if (signed_vma) 18289857Sobrien dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value); 18360484Sobrien else 18489857Sobrien dst->st_value = H_GET_WORD (abfd, src->st_value); 18589857Sobrien dst->st_size = H_GET_WORD (abfd, src->st_size); 18689857Sobrien dst->st_info = H_GET_8 (abfd, src->st_info); 18789857Sobrien dst->st_other = H_GET_8 (abfd, src->st_other); 18889857Sobrien dst->st_shndx = H_GET_16 (abfd, src->st_shndx); 18989857Sobrien if (dst->st_shndx == SHN_XINDEX) 19089857Sobrien { 19189857Sobrien if (shndx == NULL) 192218822Sdim return FALSE; 19389857Sobrien dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx); 19489857Sobrien } 195218822Sdim return TRUE; 19633965Sjdp} 19733965Sjdp 19833965Sjdp/* Translate an ELF symbol in internal format into an ELF symbol in external 19977298Sobrien format. */ 20033965Sjdp 20133965Sjdpvoid 202130561Sobrienelf_swap_symbol_out (bfd *abfd, 203130561Sobrien const Elf_Internal_Sym *src, 204130561Sobrien void *cdst, 205130561Sobrien void *shndx) 20633965Sjdp{ 20789857Sobrien unsigned int tmp; 208130561Sobrien Elf_External_Sym *dst = cdst; 20989857Sobrien H_PUT_32 (abfd, src->st_name, dst->st_name); 21089857Sobrien H_PUT_WORD (abfd, src->st_value, dst->st_value); 21189857Sobrien H_PUT_WORD (abfd, src->st_size, dst->st_size); 21289857Sobrien H_PUT_8 (abfd, src->st_info, dst->st_info); 21389857Sobrien H_PUT_8 (abfd, src->st_other, dst->st_other); 21489857Sobrien tmp = src->st_shndx; 21589857Sobrien if (tmp > SHN_HIRESERVE) 21689857Sobrien { 21789857Sobrien if (shndx == NULL) 21889857Sobrien abort (); 21989857Sobrien H_PUT_32 (abfd, tmp, shndx); 22089857Sobrien tmp = SHN_XINDEX; 22189857Sobrien } 22289857Sobrien H_PUT_16 (abfd, tmp, dst->st_shndx); 22333965Sjdp} 22433965Sjdp 22533965Sjdp/* Translate an ELF file header in external format into an ELF file header in 22677298Sobrien internal format. */ 22733965Sjdp 22833965Sjdpstatic void 229130561Sobrienelf_swap_ehdr_in (bfd *abfd, 230130561Sobrien const Elf_External_Ehdr *src, 231130561Sobrien Elf_Internal_Ehdr *dst) 23233965Sjdp{ 23377298Sobrien int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma; 23433965Sjdp memcpy (dst->e_ident, src->e_ident, EI_NIDENT); 23589857Sobrien dst->e_type = H_GET_16 (abfd, src->e_type); 23689857Sobrien dst->e_machine = H_GET_16 (abfd, src->e_machine); 23789857Sobrien dst->e_version = H_GET_32 (abfd, src->e_version); 23877298Sobrien if (signed_vma) 23989857Sobrien dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry); 24077298Sobrien else 24189857Sobrien dst->e_entry = H_GET_WORD (abfd, src->e_entry); 24289857Sobrien dst->e_phoff = H_GET_WORD (abfd, src->e_phoff); 24389857Sobrien dst->e_shoff = H_GET_WORD (abfd, src->e_shoff); 24489857Sobrien dst->e_flags = H_GET_32 (abfd, src->e_flags); 24589857Sobrien dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize); 24689857Sobrien dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize); 24789857Sobrien dst->e_phnum = H_GET_16 (abfd, src->e_phnum); 24889857Sobrien dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize); 24989857Sobrien dst->e_shnum = H_GET_16 (abfd, src->e_shnum); 25089857Sobrien dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx); 25133965Sjdp} 25233965Sjdp 25333965Sjdp/* Translate an ELF file header in internal format into an ELF file header in 25477298Sobrien external format. */ 25533965Sjdp 25633965Sjdpstatic void 257130561Sobrienelf_swap_ehdr_out (bfd *abfd, 258130561Sobrien const Elf_Internal_Ehdr *src, 259130561Sobrien Elf_External_Ehdr *dst) 26033965Sjdp{ 26189857Sobrien unsigned int tmp; 26277298Sobrien int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma; 26333965Sjdp memcpy (dst->e_ident, src->e_ident, EI_NIDENT); 26477298Sobrien /* note that all elements of dst are *arrays of unsigned char* already... */ 26589857Sobrien H_PUT_16 (abfd, src->e_type, dst->e_type); 26689857Sobrien H_PUT_16 (abfd, src->e_machine, dst->e_machine); 26789857Sobrien H_PUT_32 (abfd, src->e_version, dst->e_version); 26877298Sobrien if (signed_vma) 26989857Sobrien H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry); 27077298Sobrien else 27189857Sobrien H_PUT_WORD (abfd, src->e_entry, dst->e_entry); 27289857Sobrien H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff); 27389857Sobrien H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff); 27489857Sobrien H_PUT_32 (abfd, src->e_flags, dst->e_flags); 27589857Sobrien H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize); 27689857Sobrien H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize); 27789857Sobrien H_PUT_16 (abfd, src->e_phnum, dst->e_phnum); 27889857Sobrien H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize); 27989857Sobrien tmp = src->e_shnum; 28089857Sobrien if (tmp >= SHN_LORESERVE) 28189857Sobrien tmp = SHN_UNDEF; 28289857Sobrien H_PUT_16 (abfd, tmp, dst->e_shnum); 28389857Sobrien tmp = src->e_shstrndx; 28489857Sobrien if (tmp >= SHN_LORESERVE) 28589857Sobrien tmp = SHN_XINDEX; 28689857Sobrien H_PUT_16 (abfd, tmp, dst->e_shstrndx); 28733965Sjdp} 28833965Sjdp 28933965Sjdp/* Translate an ELF section header table entry in external format into an 29077298Sobrien ELF section header table entry in internal format. */ 29133965Sjdp 29233965Sjdpstatic void 293130561Sobrienelf_swap_shdr_in (bfd *abfd, 294130561Sobrien const Elf_External_Shdr *src, 295130561Sobrien Elf_Internal_Shdr *dst) 29633965Sjdp{ 29760484Sobrien int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma; 29860484Sobrien 29989857Sobrien dst->sh_name = H_GET_32 (abfd, src->sh_name); 30089857Sobrien dst->sh_type = H_GET_32 (abfd, src->sh_type); 30189857Sobrien dst->sh_flags = H_GET_WORD (abfd, src->sh_flags); 30260484Sobrien if (signed_vma) 30389857Sobrien dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr); 30460484Sobrien else 30589857Sobrien dst->sh_addr = H_GET_WORD (abfd, src->sh_addr); 30689857Sobrien dst->sh_offset = H_GET_WORD (abfd, src->sh_offset); 30789857Sobrien dst->sh_size = H_GET_WORD (abfd, src->sh_size); 30889857Sobrien dst->sh_link = H_GET_32 (abfd, src->sh_link); 30989857Sobrien dst->sh_info = H_GET_32 (abfd, src->sh_info); 31089857Sobrien dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign); 31189857Sobrien dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize); 31233965Sjdp dst->bfd_section = NULL; 31333965Sjdp dst->contents = NULL; 31433965Sjdp} 31533965Sjdp 31633965Sjdp/* Translate an ELF section header table entry in internal format into an 31777298Sobrien ELF section header table entry in external format. */ 31833965Sjdp 31933965Sjdpstatic void 320130561Sobrienelf_swap_shdr_out (bfd *abfd, 321130561Sobrien const Elf_Internal_Shdr *src, 322130561Sobrien Elf_External_Shdr *dst) 32333965Sjdp{ 32477298Sobrien /* note that all elements of dst are *arrays of unsigned char* already... */ 32589857Sobrien H_PUT_32 (abfd, src->sh_name, dst->sh_name); 32689857Sobrien H_PUT_32 (abfd, src->sh_type, dst->sh_type); 32789857Sobrien H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags); 32889857Sobrien H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr); 32989857Sobrien H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset); 33089857Sobrien H_PUT_WORD (abfd, src->sh_size, dst->sh_size); 33189857Sobrien H_PUT_32 (abfd, src->sh_link, dst->sh_link); 33289857Sobrien H_PUT_32 (abfd, src->sh_info, dst->sh_info); 33389857Sobrien H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign); 33489857Sobrien H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize); 33533965Sjdp} 33633965Sjdp 33733965Sjdp/* Translate an ELF program header table entry in external format into an 33877298Sobrien ELF program header table entry in internal format. */ 33933965Sjdp 34033965Sjdpvoid 341130561Sobrienelf_swap_phdr_in (bfd *abfd, 342130561Sobrien const Elf_External_Phdr *src, 343130561Sobrien Elf_Internal_Phdr *dst) 34433965Sjdp{ 34560484Sobrien int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma; 34660484Sobrien 34789857Sobrien dst->p_type = H_GET_32 (abfd, src->p_type); 34889857Sobrien dst->p_flags = H_GET_32 (abfd, src->p_flags); 34989857Sobrien dst->p_offset = H_GET_WORD (abfd, src->p_offset); 35060484Sobrien if (signed_vma) 35160484Sobrien { 35289857Sobrien dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr); 35389857Sobrien dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr); 35460484Sobrien } 35560484Sobrien else 35660484Sobrien { 35789857Sobrien dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr); 35889857Sobrien dst->p_paddr = H_GET_WORD (abfd, src->p_paddr); 35960484Sobrien } 36089857Sobrien dst->p_filesz = H_GET_WORD (abfd, src->p_filesz); 36189857Sobrien dst->p_memsz = H_GET_WORD (abfd, src->p_memsz); 36289857Sobrien dst->p_align = H_GET_WORD (abfd, src->p_align); 36333965Sjdp} 36433965Sjdp 36533965Sjdpvoid 366130561Sobrienelf_swap_phdr_out (bfd *abfd, 367130561Sobrien const Elf_Internal_Phdr *src, 368130561Sobrien Elf_External_Phdr *dst) 36933965Sjdp{ 37077298Sobrien /* note that all elements of dst are *arrays of unsigned char* already... */ 37189857Sobrien H_PUT_32 (abfd, src->p_type, dst->p_type); 37289857Sobrien H_PUT_WORD (abfd, src->p_offset, dst->p_offset); 37389857Sobrien H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr); 37489857Sobrien H_PUT_WORD (abfd, src->p_paddr, dst->p_paddr); 37589857Sobrien H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz); 37689857Sobrien H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz); 37789857Sobrien H_PUT_32 (abfd, src->p_flags, dst->p_flags); 37889857Sobrien H_PUT_WORD (abfd, src->p_align, dst->p_align); 37933965Sjdp} 38033965Sjdp 38177298Sobrien/* Translate an ELF reloc from external format to internal format. */ 382130561Sobrienvoid 383130561Sobrienelf_swap_reloc_in (bfd *abfd, 384130561Sobrien const bfd_byte *s, 385130561Sobrien Elf_Internal_Rela *dst) 38633965Sjdp{ 387130561Sobrien const Elf_External_Rel *src = (const Elf_External_Rel *) s; 38889857Sobrien dst->r_offset = H_GET_WORD (abfd, src->r_offset); 38989857Sobrien dst->r_info = H_GET_WORD (abfd, src->r_info); 390130561Sobrien dst->r_addend = 0; 39133965Sjdp} 39233965Sjdp 393130561Sobrienvoid 394130561Sobrienelf_swap_reloca_in (bfd *abfd, 395130561Sobrien const bfd_byte *s, 396130561Sobrien Elf_Internal_Rela *dst) 39733965Sjdp{ 398130561Sobrien const Elf_External_Rela *src = (const Elf_External_Rela *) s; 39989857Sobrien dst->r_offset = H_GET_WORD (abfd, src->r_offset); 40089857Sobrien dst->r_info = H_GET_WORD (abfd, src->r_info); 40189857Sobrien dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend); 40233965Sjdp} 40333965Sjdp 40477298Sobrien/* Translate an ELF reloc from internal format to external format. */ 405130561Sobrienvoid 406130561Sobrienelf_swap_reloc_out (bfd *abfd, 407130561Sobrien const Elf_Internal_Rela *src, 408130561Sobrien bfd_byte *d) 40933965Sjdp{ 410130561Sobrien Elf_External_Rel *dst = (Elf_External_Rel *) d; 41189857Sobrien H_PUT_WORD (abfd, src->r_offset, dst->r_offset); 41289857Sobrien H_PUT_WORD (abfd, src->r_info, dst->r_info); 41333965Sjdp} 41433965Sjdp 415130561Sobrienvoid 416130561Sobrienelf_swap_reloca_out (bfd *abfd, 417130561Sobrien const Elf_Internal_Rela *src, 418130561Sobrien bfd_byte *d) 41933965Sjdp{ 420130561Sobrien Elf_External_Rela *dst = (Elf_External_Rela *) d; 42189857Sobrien H_PUT_WORD (abfd, src->r_offset, dst->r_offset); 42289857Sobrien H_PUT_WORD (abfd, src->r_info, dst->r_info); 42389857Sobrien H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend); 42433965Sjdp} 42533965Sjdp 426130561Sobrienvoid 427130561Sobrienelf_swap_dyn_in (bfd *abfd, 428130561Sobrien const void *p, 429130561Sobrien Elf_Internal_Dyn *dst) 43033965Sjdp{ 431130561Sobrien const Elf_External_Dyn *src = p; 43233965Sjdp 43389857Sobrien dst->d_tag = H_GET_WORD (abfd, src->d_tag); 43489857Sobrien dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val); 43533965Sjdp} 43633965Sjdp 437130561Sobrienvoid 438130561Sobrienelf_swap_dyn_out (bfd *abfd, 439130561Sobrien const Elf_Internal_Dyn *src, 440130561Sobrien void *p) 44133965Sjdp{ 442130561Sobrien Elf_External_Dyn *dst = p; 44360484Sobrien 44489857Sobrien H_PUT_WORD (abfd, src->d_tag, dst->d_tag); 44589857Sobrien H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val); 44633965Sjdp} 44733965Sjdp 44833965Sjdp/* ELF .o/exec file reading */ 44933965Sjdp 45033965Sjdp/* Begin processing a given object. 45133965Sjdp 45233965Sjdp First we validate the file by reading in the ELF header and checking 45333965Sjdp the magic number. */ 45433965Sjdp 455130561Sobrienstatic inline bfd_boolean 456130561Sobrienelf_file_p (Elf_External_Ehdr *x_ehdrp) 45733965Sjdp{ 45833965Sjdp return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0) 45933965Sjdp && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1) 46033965Sjdp && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2) 46133965Sjdp && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3)); 46233965Sjdp} 46333965Sjdp 464218822Sdim/* Determines if a given section index is valid. */ 465218822Sdim 466218822Sdimstatic inline bfd_boolean 467218822Sdimvalid_section_index_p (unsigned index, unsigned num_sections) 468218822Sdim{ 469218822Sdim /* Note: We allow SHN_UNDEF as a valid section index. */ 470218822Sdim if (index < SHN_LORESERVE || index > SHN_HIRESERVE) 471218822Sdim return index < num_sections; 472218822Sdim 473218822Sdim /* We disallow the use of reserved indcies, except for those 474218822Sdim with OS or Application specific meaning. The test make use 475218822Sdim of the knowledge that: 476218822Sdim SHN_LORESERVE == SHN_LOPROC 477218822Sdim and 478218822Sdim SHN_HIPROC == SHN_LOOS - 1 */ 479218822Sdim /* XXX - Should we allow SHN_XINDEX as a valid index here ? */ 480218822Sdim return (index >= SHN_LOPROC && index <= SHN_HIOS); 481218822Sdim} 482218822Sdim 48333965Sjdp/* Check to see if the file associated with ABFD matches the target vector 48433965Sjdp that ABFD points to. 48533965Sjdp 48633965Sjdp Note that we may be called several times with the same ABFD, but different 48733965Sjdp target vectors, most of which will not match. We have to avoid leaving 48833965Sjdp any side effects in ABFD, or any data it points to (like tdata), if the 48933965Sjdp file does not match the target vector. */ 49033965Sjdp 49133965Sjdpconst bfd_target * 492130561Sobrienelf_object_p (bfd *abfd) 49333965Sjdp{ 49433965Sjdp Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ 49533965Sjdp Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 49633965Sjdp Elf_External_Shdr x_shdr; /* Section header table entry, external form */ 49789857Sobrien Elf_Internal_Shdr i_shdr; 49889857Sobrien Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */ 49933965Sjdp unsigned int shindex; 500130561Sobrien const struct elf_backend_data *ebd; 50189857Sobrien struct bfd_preserve preserve; 50233965Sjdp asection *s; 50389857Sobrien bfd_size_type amt; 504218822Sdim const bfd_target *target; 505218822Sdim const bfd_target * const *target_ptr; 50633965Sjdp 507130561Sobrien preserve.marker = NULL; 50877298Sobrien 50933965Sjdp /* Read in the ELF header in external format. */ 51033965Sjdp 511130561Sobrien if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr)) 51233965Sjdp { 51333965Sjdp if (bfd_get_error () != bfd_error_system_call) 51433965Sjdp goto got_wrong_format_error; 51533965Sjdp else 51633965Sjdp goto got_no_match; 51733965Sjdp } 51833965Sjdp 51933965Sjdp /* Now check to see if we have a valid ELF file, and one that BFD can 52033965Sjdp make use of. The magic number must match, the address size ('class') 52133965Sjdp and byte-swapping must match our XVEC entry, and it must have a 52233965Sjdp section header table (FIXME: See comments re sections at top of this 52377298Sobrien file). */ 52433965Sjdp 525104834Sobrien if (! elf_file_p (&x_ehdr) 526104834Sobrien || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT 527104834Sobrien || x_ehdr.e_ident[EI_CLASS] != ELFCLASS) 52833965Sjdp goto got_wrong_format_error; 52933965Sjdp 53033965Sjdp /* Check that file's byte order matches xvec's */ 53133965Sjdp switch (x_ehdr.e_ident[EI_DATA]) 53233965Sjdp { 53333965Sjdp case ELFDATA2MSB: /* Big-endian */ 53433965Sjdp if (! bfd_header_big_endian (abfd)) 53533965Sjdp goto got_wrong_format_error; 53633965Sjdp break; 53733965Sjdp case ELFDATA2LSB: /* Little-endian */ 53833965Sjdp if (! bfd_header_little_endian (abfd)) 53933965Sjdp goto got_wrong_format_error; 54033965Sjdp break; 54133965Sjdp case ELFDATANONE: /* No data encoding specified */ 54233965Sjdp default: /* Unknown data encoding specified */ 54333965Sjdp goto got_wrong_format_error; 54433965Sjdp } 54533965Sjdp 546130561Sobrien if (!bfd_preserve_save (abfd, &preserve)) 547130561Sobrien goto got_no_match; 548130561Sobrien 549218822Sdim target = abfd->xvec; 550218822Sdim 55133965Sjdp /* Allocate an instance of the elf_obj_tdata structure and hook it up to 55233965Sjdp the tdata pointer in the bfd. */ 55333965Sjdp 554218822Sdim if (! (*target->_bfd_set_format[bfd_object]) (abfd)) 55533965Sjdp goto got_no_match; 556130561Sobrien preserve.marker = elf_tdata (abfd); 55733965Sjdp 55833965Sjdp /* Now that we know the byte order, swap in the rest of the header */ 55933965Sjdp i_ehdrp = elf_elfheader (abfd); 56033965Sjdp elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); 56133965Sjdp#if DEBUG & 1 56233965Sjdp elf_debug_file (i_ehdrp); 56333965Sjdp#endif 56433965Sjdp 56560484Sobrien /* Reject ET_CORE (header indicates core file, not object file) */ 56660484Sobrien if (i_ehdrp->e_type == ET_CORE) 56760484Sobrien goto got_wrong_format_error; 56860484Sobrien 56989857Sobrien /* If this is a relocatable file and there is no section header 57089857Sobrien table, then we're hosed. */ 57189857Sobrien if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_type == ET_REL) 57233965Sjdp goto got_wrong_format_error; 57333965Sjdp 574218822Sdim /* As a simple sanity check, verify that what BFD thinks is the 57533965Sjdp size of each section header table entry actually matches the size 57689857Sobrien recorded in the file, but only if there are any sections. */ 57789857Sobrien if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0) 57833965Sjdp goto got_wrong_format_error; 57933965Sjdp 580104834Sobrien /* Further sanity check. */ 581104834Sobrien if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_shnum != 0) 582104834Sobrien goto got_wrong_format_error; 583104834Sobrien 58433965Sjdp ebd = get_elf_backend_data (abfd); 58533965Sjdp 58633965Sjdp /* Check that the ELF e_machine field matches what this particular 58733965Sjdp BFD format expects. */ 58833965Sjdp if (ebd->elf_machine_code != i_ehdrp->e_machine 589130561Sobrien && (ebd->elf_machine_alt1 == 0 590130561Sobrien || i_ehdrp->e_machine != ebd->elf_machine_alt1) 591130561Sobrien && (ebd->elf_machine_alt2 == 0 592130561Sobrien || i_ehdrp->e_machine != ebd->elf_machine_alt2)) 59333965Sjdp { 59433965Sjdp if (ebd->elf_machine_code != EM_NONE) 59533965Sjdp goto got_wrong_format_error; 59633965Sjdp 59733965Sjdp /* This is the generic ELF target. Let it match any ELF target 59833965Sjdp for which we do not have a specific backend. */ 59933965Sjdp for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) 60033965Sjdp { 601130561Sobrien const struct elf_backend_data *back; 60233965Sjdp 60333965Sjdp if ((*target_ptr)->flavour != bfd_target_elf_flavour) 60433965Sjdp continue; 605130561Sobrien back = (const struct elf_backend_data *) (*target_ptr)->backend_data; 60633965Sjdp if (back->elf_machine_code == i_ehdrp->e_machine 60733965Sjdp || (back->elf_machine_alt1 != 0 60833965Sjdp && back->elf_machine_alt1 == i_ehdrp->e_machine) 60933965Sjdp || (back->elf_machine_alt2 != 0 61033965Sjdp && back->elf_machine_alt2 == i_ehdrp->e_machine)) 61133965Sjdp { 61233965Sjdp /* target_ptr is an ELF backend which matches this 61333965Sjdp object file, so reject the generic ELF target. */ 61433965Sjdp goto got_wrong_format_error; 61533965Sjdp } 61633965Sjdp } 61733965Sjdp } 61833965Sjdp 61933965Sjdp if (i_ehdrp->e_type == ET_EXEC) 62033965Sjdp abfd->flags |= EXEC_P; 62133965Sjdp else if (i_ehdrp->e_type == ET_DYN) 62233965Sjdp abfd->flags |= DYNAMIC; 62333965Sjdp 62433965Sjdp if (i_ehdrp->e_phnum > 0) 62533965Sjdp abfd->flags |= D_PAGED; 62633965Sjdp 62733965Sjdp if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)) 62860484Sobrien { 62960484Sobrien /* It's OK if this fails for the generic target. */ 63060484Sobrien if (ebd->elf_machine_code != EM_NONE) 63160484Sobrien goto got_no_match; 63260484Sobrien } 63333965Sjdp 634218822Sdim if (ebd->elf_machine_code != EM_NONE 635218822Sdim && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi) 636218822Sdim { 637218822Sdim if (ebd->elf_osabi != ELFOSABI_NONE) 638218822Sdim goto got_wrong_format_error; 63933965Sjdp 640218822Sdim /* This is an ELFOSABI_NONE ELF target. Let it match any ELF 641218822Sdim target of the compatible machine for which we do not have a 642218822Sdim backend with matching ELFOSABI. */ 643218822Sdim for (target_ptr = bfd_target_vector; 644218822Sdim *target_ptr != NULL; 645218822Sdim target_ptr++) 646218822Sdim { 647218822Sdim const struct elf_backend_data *back; 648218822Sdim 649218822Sdim /* Skip this target and targets with incompatible byte 650218822Sdim order. */ 651218822Sdim if (*target_ptr == target 652218822Sdim || (*target_ptr)->flavour != bfd_target_elf_flavour 653218822Sdim || (*target_ptr)->byteorder != target->byteorder 654218822Sdim || ((*target_ptr)->header_byteorder 655218822Sdim != target->header_byteorder)) 656218822Sdim continue; 657218822Sdim 658218822Sdim back = (const struct elf_backend_data *) (*target_ptr)->backend_data; 659218822Sdim if (back->elf_osabi == i_ehdrp->e_ident[EI_OSABI] 660218822Sdim && (back->elf_machine_code == i_ehdrp->e_machine 661218822Sdim || (back->elf_machine_alt1 != 0 662218822Sdim && back->elf_machine_alt1 == i_ehdrp->e_machine) 663218822Sdim || (back->elf_machine_alt2 != 0 664218822Sdim && back->elf_machine_alt2 == i_ehdrp->e_machine))) 665218822Sdim { 666218822Sdim /* target_ptr is an ELF backend which matches this 667218822Sdim object file, so reject the ELFOSABI_NONE ELF target. */ 668218822Sdim goto got_wrong_format_error; 669218822Sdim } 670218822Sdim } 671218822Sdim } 672218822Sdim 673104834Sobrien if (i_ehdrp->e_shoff != 0) 674104834Sobrien { 675218822Sdim bfd_signed_vma where = i_ehdrp->e_shoff; 676218822Sdim 677218822Sdim if (where != (file_ptr) where) 678218822Sdim goto got_wrong_format_error; 679218822Sdim 680104834Sobrien /* Seek to the section header table in the file. */ 681218822Sdim if (bfd_seek (abfd, (file_ptr) where, SEEK_SET) != 0) 682104834Sobrien goto got_no_match; 68389857Sobrien 684104834Sobrien /* Read the first section header at index 0, and convert to internal 685104834Sobrien form. */ 686130561Sobrien if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)) 687104834Sobrien goto got_no_match; 688104834Sobrien elf_swap_shdr_in (abfd, &x_shdr, &i_shdr); 68989857Sobrien 690104834Sobrien /* If the section count is zero, the actual count is in the first 691104834Sobrien section header. */ 692104834Sobrien if (i_ehdrp->e_shnum == SHN_UNDEF) 693218822Sdim { 694218822Sdim i_ehdrp->e_shnum = i_shdr.sh_size; 695218822Sdim if (i_ehdrp->e_shnum != i_shdr.sh_size 696218822Sdim || i_ehdrp->e_shnum == 0) 697218822Sdim goto got_wrong_format_error; 698218822Sdim } 69989857Sobrien 700104834Sobrien /* And similarly for the string table index. */ 701104834Sobrien if (i_ehdrp->e_shstrndx == SHN_XINDEX) 702218822Sdim { 703218822Sdim i_ehdrp->e_shstrndx = i_shdr.sh_link; 704218822Sdim if (i_ehdrp->e_shstrndx != i_shdr.sh_link) 705218822Sdim goto got_wrong_format_error; 706218822Sdim } 707218822Sdim 708218822Sdim /* Sanity check that we can read all of the section headers. 709218822Sdim It ought to be good enough to just read the last one. */ 710218822Sdim if (i_ehdrp->e_shnum != 1) 711218822Sdim { 712218822Sdim /* Check that we don't have a totally silly number of sections. */ 713218822Sdim if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr) 714218822Sdim || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr)) 715218822Sdim goto got_wrong_format_error; 716218822Sdim 717218822Sdim where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr); 718218822Sdim if (where != (file_ptr) where) 719218822Sdim goto got_wrong_format_error; 720218822Sdim if ((bfd_size_type) where <= i_ehdrp->e_shoff) 721218822Sdim goto got_wrong_format_error; 722218822Sdim 723218822Sdim if (bfd_seek (abfd, (file_ptr) where, SEEK_SET) != 0) 724218822Sdim goto got_no_match; 725218822Sdim if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)) 726218822Sdim goto got_no_match; 727218822Sdim 728218822Sdim /* Back to where we were. */ 729218822Sdim where = i_ehdrp->e_shoff + sizeof (x_shdr); 730218822Sdim if (bfd_seek (abfd, (file_ptr) where, SEEK_SET) != 0) 731218822Sdim goto got_no_match; 732218822Sdim } 733104834Sobrien } 73489857Sobrien 73589857Sobrien /* Allocate space for a copy of the section header table in 73689857Sobrien internal form. */ 73789857Sobrien if (i_ehdrp->e_shnum != 0) 73833965Sjdp { 73989857Sobrien Elf_Internal_Shdr *shdrp; 74089857Sobrien unsigned int num_sec; 74189857Sobrien 74289857Sobrien amt = sizeof (*i_shdrp) * i_ehdrp->e_shnum; 743130561Sobrien i_shdrp = bfd_alloc (abfd, amt); 74489857Sobrien if (!i_shdrp) 74533965Sjdp goto got_no_match; 74689857Sobrien num_sec = i_ehdrp->e_shnum; 74789857Sobrien if (num_sec > SHN_LORESERVE) 74889857Sobrien num_sec += SHN_HIRESERVE + 1 - SHN_LORESERVE; 74989857Sobrien elf_numsections (abfd) = num_sec; 75089857Sobrien amt = sizeof (i_shdrp) * num_sec; 751130561Sobrien elf_elfsections (abfd) = bfd_alloc (abfd, amt); 75289857Sobrien if (!elf_elfsections (abfd)) 75389857Sobrien goto got_no_match; 75433965Sjdp 75589857Sobrien memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp)); 75689857Sobrien shdrp = i_shdrp; 75789857Sobrien shindex = 0; 75889857Sobrien if (num_sec > SHN_LORESERVE) 75989857Sobrien { 76089857Sobrien for ( ; shindex < SHN_LORESERVE; shindex++) 76189857Sobrien elf_elfsections (abfd)[shindex] = shdrp++; 76289857Sobrien for ( ; shindex < SHN_HIRESERVE + 1; shindex++) 76389857Sobrien elf_elfsections (abfd)[shindex] = i_shdrp; 76489857Sobrien } 76589857Sobrien for ( ; shindex < num_sec; shindex++) 76689857Sobrien elf_elfsections (abfd)[shindex] = shdrp++; 76789857Sobrien 76889857Sobrien /* Read in the rest of the section header table and convert it 76989857Sobrien to internal form. */ 77089857Sobrien for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++) 77189857Sobrien { 772130561Sobrien if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)) 77389857Sobrien goto got_no_match; 77489857Sobrien elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex); 77589857Sobrien 776218822Sdim /* Sanity check sh_link and sh_info. */ 777218822Sdim if (! valid_section_index_p (i_shdrp[shindex].sh_link, num_sec)) 778218822Sdim goto got_wrong_format_error; 779218822Sdim 780218822Sdim if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK) 781218822Sdim || i_shdrp[shindex].sh_type == SHT_RELA 782218822Sdim || i_shdrp[shindex].sh_type == SHT_REL) 783218822Sdim && ! valid_section_index_p (i_shdrp[shindex].sh_info, num_sec)) 784218822Sdim goto got_wrong_format_error; 785218822Sdim 78689857Sobrien /* If the section is loaded, but not page aligned, clear 78789857Sobrien D_PAGED. */ 78889857Sobrien if (i_shdrp[shindex].sh_size != 0 78989857Sobrien && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0 79089857Sobrien && i_shdrp[shindex].sh_type != SHT_NOBITS 79189857Sobrien && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset) 792218822Sdim % ebd->minpagesize) 79389857Sobrien != 0)) 79489857Sobrien abfd->flags &= ~D_PAGED; 79589857Sobrien } 79633965Sjdp } 79789857Sobrien 798218822Sdim /* A further sanity check. */ 799218822Sdim if (i_ehdrp->e_shnum != 0) 80033965Sjdp { 801218822Sdim if (! valid_section_index_p (i_ehdrp->e_shstrndx, elf_numsections (abfd))) 802218822Sdim { 803218822Sdim /* PR 2257: 804218822Sdim We used to just goto got_wrong_format_error here 805218822Sdim but there are binaries in existance for which this test 806218822Sdim will prevent the binutils from working with them at all. 807218822Sdim So we are kind, and reset the string index value to 0 808218822Sdim so that at least some processing can be done. */ 809218822Sdim i_ehdrp->e_shstrndx = SHN_UNDEF; 810218822Sdim _bfd_error_handler (_("warning: %s has a corrupt string table index - ignoring"), abfd->filename); 811218822Sdim } 81233965Sjdp } 813218822Sdim else if (i_ehdrp->e_shstrndx != SHN_UNDEF) 814218822Sdim goto got_wrong_format_error; 81533965Sjdp 81633965Sjdp /* Read in the program headers. */ 81733965Sjdp if (i_ehdrp->e_phnum == 0) 81833965Sjdp elf_tdata (abfd)->phdr = NULL; 81933965Sjdp else 82033965Sjdp { 82133965Sjdp Elf_Internal_Phdr *i_phdr; 82233965Sjdp unsigned int i; 82333965Sjdp 82489857Sobrien amt = i_ehdrp->e_phnum * sizeof (Elf_Internal_Phdr); 825130561Sobrien elf_tdata (abfd)->phdr = bfd_alloc (abfd, amt); 82633965Sjdp if (elf_tdata (abfd)->phdr == NULL) 82733965Sjdp goto got_no_match; 82889857Sobrien if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0) 82933965Sjdp goto got_no_match; 83033965Sjdp i_phdr = elf_tdata (abfd)->phdr; 83133965Sjdp for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++) 83233965Sjdp { 83333965Sjdp Elf_External_Phdr x_phdr; 83433965Sjdp 835130561Sobrien if (bfd_bread (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr) 83633965Sjdp goto got_no_match; 83733965Sjdp elf_swap_phdr_in (abfd, &x_phdr, i_phdr); 83833965Sjdp } 83933965Sjdp } 84033965Sjdp 841218822Sdim if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff != 0) 84289857Sobrien { 84389857Sobrien unsigned int num_sec; 84433965Sjdp 84589857Sobrien /* Once all of the section headers have been read and converted, we 84689857Sobrien can start processing them. Note that the first section header is 84789857Sobrien a dummy placeholder entry, so we ignore it. */ 84889857Sobrien num_sec = elf_numsections (abfd); 84989857Sobrien for (shindex = 1; shindex < num_sec; shindex++) 85089857Sobrien { 85189857Sobrien if (! bfd_section_from_shdr (abfd, shindex)) 85289857Sobrien goto got_no_match; 85389857Sobrien if (shindex == SHN_LORESERVE - 1) 85489857Sobrien shindex += SHN_HIRESERVE + 1 - SHN_LORESERVE; 85589857Sobrien } 856218822Sdim 857218822Sdim /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER. */ 858218822Sdim if (! _bfd_elf_setup_sections (abfd)) 859218822Sdim goto got_wrong_format_error; 86033965Sjdp } 86133965Sjdp 86233965Sjdp /* Let the backend double check the format and override global 86333965Sjdp information. */ 86433965Sjdp if (ebd->elf_backend_object_p) 86533965Sjdp { 866104834Sobrien if (! (*ebd->elf_backend_object_p) (abfd)) 86733965Sjdp goto got_wrong_format_error; 86833965Sjdp } 86933965Sjdp 870218822Sdim /* Remember the entry point specified in the ELF file header. */ 871218822Sdim bfd_set_start_address (abfd, i_ehdrp->e_entry); 872218822Sdim 87333965Sjdp /* If we have created any reloc sections that are associated with 87433965Sjdp debugging sections, mark the reloc sections as debugging as well. */ 87533965Sjdp for (s = abfd->sections; s != NULL; s = s->next) 87633965Sjdp { 87733965Sjdp if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL 87833965Sjdp || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 87933965Sjdp && elf_section_data (s)->this_hdr.sh_info > 0) 88033965Sjdp { 88133965Sjdp unsigned long targ_index; 88233965Sjdp asection *targ_sec; 88333965Sjdp 88433965Sjdp targ_index = elf_section_data (s)->this_hdr.sh_info; 88533965Sjdp targ_sec = bfd_section_from_elf_index (abfd, targ_index); 88633965Sjdp if (targ_sec != NULL 88733965Sjdp && (targ_sec->flags & SEC_DEBUGGING) != 0) 88833965Sjdp s->flags |= SEC_DEBUGGING; 88933965Sjdp } 89033965Sjdp } 89133965Sjdp 892130561Sobrien bfd_preserve_finish (abfd, &preserve); 893218822Sdim return target; 89433965Sjdp 89560484Sobrien got_wrong_format_error: 89677298Sobrien /* There is way too much undoing of half-known state here. The caller, 89777298Sobrien bfd_check_format_matches, really shouldn't iterate on live bfd's to 89877298Sobrien check match/no-match like it does. We have to rely on that a call to 89977298Sobrien bfd_default_set_arch_mach with the previously known mach, undoes what 90077298Sobrien was done by the first bfd_default_set_arch_mach (with mach 0) here. 90177298Sobrien For this to work, only elf-data and the mach may be changed by the 90277298Sobrien target-specific elf_backend_object_p function. Note that saving the 90377298Sobrien whole bfd here and restoring it would be even worse; the first thing 90477298Sobrien you notice is that the cached bfd file position gets out of sync. */ 90533965Sjdp bfd_set_error (bfd_error_wrong_format); 90689857Sobrien 90760484Sobrien got_no_match: 908130561Sobrien if (preserve.marker != NULL) 909130561Sobrien bfd_preserve_restore (abfd, &preserve); 91089857Sobrien return NULL; 91133965Sjdp} 91233965Sjdp 91333965Sjdp/* ELF .o/exec file writing */ 91433965Sjdp 91533965Sjdp/* Write out the relocs. */ 91633965Sjdp 91777298Sobrienvoid 918130561Sobrienelf_write_relocs (bfd *abfd, asection *sec, void *data) 91933965Sjdp{ 920130561Sobrien bfd_boolean *failedp = data; 92133965Sjdp Elf_Internal_Shdr *rela_hdr; 922130561Sobrien bfd_vma addr_offset; 923130561Sobrien void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 924130561Sobrien size_t extsize; 925130561Sobrien bfd_byte *dst_rela; 92633965Sjdp unsigned int idx; 927130561Sobrien asymbol *last_sym; 928130561Sobrien int last_sym_idx; 92933965Sjdp 93033965Sjdp /* If we have already failed, don't do anything. */ 93133965Sjdp if (*failedp) 93233965Sjdp return; 93333965Sjdp 93433965Sjdp if ((sec->flags & SEC_RELOC) == 0) 93533965Sjdp return; 93633965Sjdp 93733965Sjdp /* The linker backend writes the relocs out itself, and sets the 93833965Sjdp reloc_count field to zero to inhibit writing them here. Also, 93933965Sjdp sometimes the SEC_RELOC flag gets set even when there aren't any 94033965Sjdp relocs. */ 94133965Sjdp if (sec->reloc_count == 0) 94233965Sjdp return; 94333965Sjdp 944218822Sdim /* If we have opened an existing file for update, reloc_count may be 945218822Sdim set even though we are not linking. In that case we have nothing 946218822Sdim to do. */ 947218822Sdim if (sec->orelocation == NULL) 948218822Sdim return; 949218822Sdim 95033965Sjdp rela_hdr = &elf_section_data (sec)->rel_hdr; 95133965Sjdp 95233965Sjdp rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count; 953130561Sobrien rela_hdr->contents = bfd_alloc (abfd, rela_hdr->sh_size); 95433965Sjdp if (rela_hdr->contents == NULL) 95533965Sjdp { 956130561Sobrien *failedp = TRUE; 95733965Sjdp return; 95833965Sjdp } 95933965Sjdp 96060484Sobrien /* Figure out whether the relocations are RELA or REL relocations. */ 96160484Sobrien if (rela_hdr->sh_type == SHT_RELA) 962130561Sobrien { 963130561Sobrien swap_out = elf_swap_reloca_out; 964130561Sobrien extsize = sizeof (Elf_External_Rela); 965130561Sobrien } 96660484Sobrien else if (rela_hdr->sh_type == SHT_REL) 967130561Sobrien { 968130561Sobrien swap_out = elf_swap_reloc_out; 969130561Sobrien extsize = sizeof (Elf_External_Rel); 970130561Sobrien } 97160484Sobrien else 97260484Sobrien /* Every relocation section should be either an SHT_RELA or an 97360484Sobrien SHT_REL section. */ 97460484Sobrien abort (); 97560484Sobrien 976130561Sobrien /* The address of an ELF reloc is section relative for an object 977130561Sobrien file, and absolute for an executable file or shared library. 978130561Sobrien The address of a BFD reloc is always section relative. */ 979130561Sobrien addr_offset = 0; 980130561Sobrien if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 981130561Sobrien addr_offset = sec->vma; 982130561Sobrien 98377298Sobrien /* orelocation has the data, reloc_count has the count... */ 984130561Sobrien last_sym = 0; 985130561Sobrien last_sym_idx = 0; 986130561Sobrien dst_rela = rela_hdr->contents; 987130561Sobrien 988130561Sobrien for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize) 98933965Sjdp { 990130561Sobrien Elf_Internal_Rela src_rela; 991130561Sobrien arelent *ptr; 992130561Sobrien asymbol *sym; 993130561Sobrien int n; 99433965Sjdp 995130561Sobrien ptr = sec->orelocation[idx]; 996130561Sobrien sym = *ptr->sym_ptr_ptr; 997130561Sobrien if (sym == last_sym) 998130561Sobrien n = last_sym_idx; 999130561Sobrien else if (bfd_is_abs_section (sym->section) && sym->value == 0) 1000130561Sobrien n = STN_UNDEF; 1001130561Sobrien else 100233965Sjdp { 1003130561Sobrien last_sym = sym; 1004130561Sobrien n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym); 1005130561Sobrien if (n < 0) 100633965Sjdp { 1007130561Sobrien *failedp = TRUE; 100833965Sjdp return; 100933965Sjdp } 1010130561Sobrien last_sym_idx = n; 101133965Sjdp } 101233965Sjdp 1013130561Sobrien if ((*ptr->sym_ptr_ptr)->the_bfd != NULL 1014130561Sobrien && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec 1015130561Sobrien && ! _bfd_elf_validate_reloc (abfd, ptr)) 101633965Sjdp { 1017130561Sobrien *failedp = TRUE; 1018130561Sobrien return; 1019130561Sobrien } 102033965Sjdp 1021130561Sobrien src_rela.r_offset = ptr->address + addr_offset; 1022130561Sobrien src_rela.r_info = ELF_R_INFO (n, ptr->howto->type); 1023130561Sobrien src_rela.r_addend = ptr->addend; 1024130561Sobrien (*swap_out) (abfd, &src_rela, dst_rela); 102533965Sjdp } 102633965Sjdp} 102733965Sjdp 102833965Sjdp/* Write out the program headers. */ 102933965Sjdp 103033965Sjdpint 1031130561Sobrienelf_write_out_phdrs (bfd *abfd, 1032130561Sobrien const Elf_Internal_Phdr *phdr, 1033130561Sobrien unsigned int count) 103433965Sjdp{ 103533965Sjdp while (count--) 103633965Sjdp { 103733965Sjdp Elf_External_Phdr extphdr; 103833965Sjdp elf_swap_phdr_out (abfd, phdr, &extphdr); 1039130561Sobrien if (bfd_bwrite (&extphdr, sizeof (Elf_External_Phdr), abfd) 1040130561Sobrien != sizeof (Elf_External_Phdr)) 104133965Sjdp return -1; 104233965Sjdp phdr++; 104333965Sjdp } 104433965Sjdp return 0; 104533965Sjdp} 104633965Sjdp 104733965Sjdp/* Write out the section headers and the ELF file header. */ 104833965Sjdp 1049130561Sobrienbfd_boolean 1050130561Sobrienelf_write_shdrs_and_ehdr (bfd *abfd) 105133965Sjdp{ 105233965Sjdp Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ 105333965Sjdp Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 105433965Sjdp Elf_External_Shdr *x_shdrp; /* Section header table, external form */ 105533965Sjdp Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ 105633965Sjdp unsigned int count; 105789857Sobrien bfd_size_type amt; 105833965Sjdp 105933965Sjdp i_ehdrp = elf_elfheader (abfd); 106033965Sjdp i_shdrp = elf_elfsections (abfd); 106133965Sjdp 106277298Sobrien /* swap the header before spitting it out... */ 106333965Sjdp 106433965Sjdp#if DEBUG & 1 106533965Sjdp elf_debug_file (i_ehdrp); 106633965Sjdp#endif 106733965Sjdp elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr); 106889857Sobrien amt = sizeof (x_ehdr); 106933965Sjdp if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 1070130561Sobrien || bfd_bwrite (&x_ehdr, amt, abfd) != amt) 1071130561Sobrien return FALSE; 107233965Sjdp 107389857Sobrien /* Some fields in the first section header handle overflow of ehdr 107489857Sobrien fields. */ 107589857Sobrien if (i_ehdrp->e_shnum >= SHN_LORESERVE) 107689857Sobrien i_shdrp[0]->sh_size = i_ehdrp->e_shnum; 107789857Sobrien if (i_ehdrp->e_shstrndx >= SHN_LORESERVE) 107889857Sobrien i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx; 107989857Sobrien 108077298Sobrien /* at this point we've concocted all the ELF sections... */ 108189857Sobrien amt = i_ehdrp->e_shnum; 108289857Sobrien amt *= sizeof (*x_shdrp); 1083130561Sobrien x_shdrp = bfd_alloc (abfd, amt); 108433965Sjdp if (!x_shdrp) 1085130561Sobrien return FALSE; 108633965Sjdp 108789857Sobrien for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++) 108833965Sjdp { 108933965Sjdp#if DEBUG & 2 109089857Sobrien elf_debug_section (count, *i_shdrp); 109133965Sjdp#endif 109289857Sobrien elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count); 1093130561Sobrien 109489857Sobrien if (count == SHN_LORESERVE - 1) 109589857Sobrien i_shdrp += SHN_HIRESERVE + 1 - SHN_LORESERVE; 109633965Sjdp } 109733965Sjdp if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0 1098130561Sobrien || bfd_bwrite (x_shdrp, amt, abfd) != amt) 1099130561Sobrien return FALSE; 110033965Sjdp 110177298Sobrien /* need to dump the string table too... */ 110233965Sjdp 1103130561Sobrien return TRUE; 110433965Sjdp} 110533965Sjdp 110633965Sjdplong 1107130561Sobrienelf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic) 110833965Sjdp{ 110933965Sjdp Elf_Internal_Shdr *hdr; 111033965Sjdp Elf_Internal_Shdr *verhdr; 111138889Sjdp unsigned long symcount; /* Number of external ELF symbols */ 111233965Sjdp elf_symbol_type *sym; /* Pointer to current bfd symbol */ 111333965Sjdp elf_symbol_type *symbase; /* Buffer for generated bfd symbols */ 1114104834Sobrien Elf_Internal_Sym *isym; 1115104834Sobrien Elf_Internal_Sym *isymend; 1116104834Sobrien Elf_Internal_Sym *isymbuf = NULL; 1117104834Sobrien Elf_External_Versym *xver; 1118104834Sobrien Elf_External_Versym *xverbuf = NULL; 1119130561Sobrien const struct elf_backend_data *ebd; 112089857Sobrien bfd_size_type amt; 112133965Sjdp 112233965Sjdp /* Read each raw ELF symbol, converting from external ELF form to 112333965Sjdp internal ELF form, and then using the information to create a 112433965Sjdp canonical bfd symbol table entry. 112533965Sjdp 112633965Sjdp Note that we allocate the initial bfd canonical symbol buffer 112733965Sjdp based on a one-to-one mapping of the ELF symbols to canonical 112833965Sjdp symbols. We actually use all the ELF symbols, so there will be no 112933965Sjdp space left over at the end. When we have all the symbols, we 113077298Sobrien build the caller's pointer vector. */ 113133965Sjdp 113233965Sjdp if (! dynamic) 113333965Sjdp { 113433965Sjdp hdr = &elf_tdata (abfd)->symtab_hdr; 113533965Sjdp verhdr = NULL; 113633965Sjdp } 113733965Sjdp else 113833965Sjdp { 113933965Sjdp hdr = &elf_tdata (abfd)->dynsymtab_hdr; 114033965Sjdp if (elf_dynversym (abfd) == 0) 114133965Sjdp verhdr = NULL; 114233965Sjdp else 114333965Sjdp verhdr = &elf_tdata (abfd)->dynversym_hdr; 114433965Sjdp if ((elf_tdata (abfd)->dynverdef_section != 0 114533965Sjdp && elf_tdata (abfd)->verdef == NULL) 114633965Sjdp || (elf_tdata (abfd)->dynverref_section != 0 114733965Sjdp && elf_tdata (abfd)->verref == NULL)) 114833965Sjdp { 1149218822Sdim if (!_bfd_elf_slurp_version_tables (abfd, FALSE)) 115033965Sjdp return -1; 115133965Sjdp } 115233965Sjdp } 115333965Sjdp 1154104834Sobrien ebd = get_elf_backend_data (abfd); 115533965Sjdp symcount = hdr->sh_size / sizeof (Elf_External_Sym); 115633965Sjdp if (symcount == 0) 115733965Sjdp sym = symbase = NULL; 115833965Sjdp else 115933965Sjdp { 1160104834Sobrien isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0, 1161104834Sobrien NULL, NULL, NULL); 1162104834Sobrien if (isymbuf == NULL) 1163104834Sobrien return -1; 116433965Sjdp 116589857Sobrien amt = symcount; 116689857Sobrien amt *= sizeof (elf_symbol_type); 1167130561Sobrien symbase = bfd_zalloc (abfd, amt); 116833965Sjdp if (symbase == (elf_symbol_type *) NULL) 116989857Sobrien goto error_return; 117033965Sjdp 117133965Sjdp /* Read the raw ELF version symbol information. */ 117233965Sjdp if (verhdr != NULL 117333965Sjdp && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount) 117433965Sjdp { 117533965Sjdp (*_bfd_error_handler) 117660484Sobrien (_("%s: version count (%ld) does not match symbol count (%ld)"), 117733965Sjdp abfd->filename, 117833965Sjdp (long) (verhdr->sh_size / sizeof (Elf_External_Versym)), 117933965Sjdp symcount); 118033965Sjdp 118133965Sjdp /* Slurp in the symbols without the version information, 1182218822Sdim since that is more helpful than just quitting. */ 118333965Sjdp verhdr = NULL; 118433965Sjdp } 118533965Sjdp 118633965Sjdp if (verhdr != NULL) 118733965Sjdp { 118833965Sjdp if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0) 118933965Sjdp goto error_return; 119033965Sjdp 1191130561Sobrien xverbuf = bfd_malloc (verhdr->sh_size); 1192104834Sobrien if (xverbuf == NULL && verhdr->sh_size != 0) 119333965Sjdp goto error_return; 119433965Sjdp 1195130561Sobrien if (bfd_bread (xverbuf, verhdr->sh_size, abfd) != verhdr->sh_size) 119633965Sjdp goto error_return; 119733965Sjdp } 119833965Sjdp 119933965Sjdp /* Skip first symbol, which is a null dummy. */ 1200104834Sobrien xver = xverbuf; 1201104834Sobrien if (xver != NULL) 1202104834Sobrien ++xver; 1203104834Sobrien isymend = isymbuf + symcount; 1204104834Sobrien for (isym = isymbuf + 1, sym = symbase; isym < isymend; isym++, sym++) 120533965Sjdp { 1206104834Sobrien memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym)); 120733965Sjdp sym->symbol.the_bfd = abfd; 120833965Sjdp 1209218822Sdim sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL); 121033965Sjdp 1211104834Sobrien sym->symbol.value = isym->st_value; 121233965Sjdp 1213104834Sobrien if (isym->st_shndx == SHN_UNDEF) 121433965Sjdp { 121589857Sobrien sym->symbol.section = bfd_und_section_ptr; 121689857Sobrien } 1217104834Sobrien else if (isym->st_shndx < SHN_LORESERVE 1218104834Sobrien || isym->st_shndx > SHN_HIRESERVE) 121989857Sobrien { 1220130561Sobrien sym->symbol.section = bfd_section_from_elf_index (abfd, 1221130561Sobrien isym->st_shndx); 122233965Sjdp if (sym->symbol.section == NULL) 122333965Sjdp { 122433965Sjdp /* This symbol is in a section for which we did not 122533965Sjdp create a BFD section. Just use bfd_abs_section, 122633965Sjdp although it is wrong. FIXME. */ 122733965Sjdp sym->symbol.section = bfd_abs_section_ptr; 122833965Sjdp } 122933965Sjdp } 1230104834Sobrien else if (isym->st_shndx == SHN_ABS) 123133965Sjdp { 123233965Sjdp sym->symbol.section = bfd_abs_section_ptr; 123333965Sjdp } 1234104834Sobrien else if (isym->st_shndx == SHN_COMMON) 123533965Sjdp { 123633965Sjdp sym->symbol.section = bfd_com_section_ptr; 123733965Sjdp /* Elf puts the alignment into the `value' field, and 123833965Sjdp the size into the `size' field. BFD wants to see the 123933965Sjdp size in the value field, and doesn't care (at the 124033965Sjdp moment) about the alignment. */ 1241104834Sobrien sym->symbol.value = isym->st_size; 124233965Sjdp } 124333965Sjdp else 124433965Sjdp sym->symbol.section = bfd_abs_section_ptr; 124533965Sjdp 1246130561Sobrien /* If this is a relocatable file, then the symbol value is 1247218822Sdim already section relative. */ 124833965Sjdp if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 124933965Sjdp sym->symbol.value -= sym->symbol.section->vma; 125033965Sjdp 1251104834Sobrien switch (ELF_ST_BIND (isym->st_info)) 125233965Sjdp { 125333965Sjdp case STB_LOCAL: 125433965Sjdp sym->symbol.flags |= BSF_LOCAL; 125533965Sjdp break; 125633965Sjdp case STB_GLOBAL: 1257104834Sobrien if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON) 125833965Sjdp sym->symbol.flags |= BSF_GLOBAL; 125933965Sjdp break; 126033965Sjdp case STB_WEAK: 126133965Sjdp sym->symbol.flags |= BSF_WEAK; 126233965Sjdp break; 126333965Sjdp } 126433965Sjdp 1265104834Sobrien switch (ELF_ST_TYPE (isym->st_info)) 126633965Sjdp { 126733965Sjdp case STT_SECTION: 126833965Sjdp sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING; 126933965Sjdp break; 127033965Sjdp case STT_FILE: 127133965Sjdp sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING; 127233965Sjdp break; 127333965Sjdp case STT_FUNC: 127433965Sjdp sym->symbol.flags |= BSF_FUNCTION; 127533965Sjdp break; 127633965Sjdp case STT_OBJECT: 127733965Sjdp sym->symbol.flags |= BSF_OBJECT; 127833965Sjdp break; 1279218822Sdim case STT_TLS: 1280218822Sdim sym->symbol.flags |= BSF_THREAD_LOCAL; 1281218822Sdim break; 1282218822Sdim case STT_RELC: 1283218822Sdim sym->symbol.flags |= BSF_RELC; 1284218822Sdim break; 1285218822Sdim case STT_SRELC: 1286218822Sdim sym->symbol.flags |= BSF_SRELC; 1287218822Sdim break; 128833965Sjdp } 128933965Sjdp 129033965Sjdp if (dynamic) 129133965Sjdp sym->symbol.flags |= BSF_DYNAMIC; 129233965Sjdp 1293104834Sobrien if (xver != NULL) 129433965Sjdp { 129533965Sjdp Elf_Internal_Versym iversym; 129633965Sjdp 1297104834Sobrien _bfd_elf_swap_versym_in (abfd, xver, &iversym); 129833965Sjdp sym->version = iversym.vs_vers; 1299104834Sobrien xver++; 130033965Sjdp } 130133965Sjdp 130233965Sjdp /* Do some backend-specific processing on this symbol. */ 1303104834Sobrien if (ebd->elf_backend_symbol_processing) 1304104834Sobrien (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol); 130533965Sjdp } 130633965Sjdp } 130733965Sjdp 130833965Sjdp /* Do some backend-specific processing on this symbol table. */ 1309104834Sobrien if (ebd->elf_backend_symbol_table_processing) 1310104834Sobrien (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount); 131133965Sjdp 131233965Sjdp /* We rely on the zalloc to clear out the final symbol entry. */ 131333965Sjdp 131433965Sjdp symcount = sym - symbase; 131533965Sjdp 131633965Sjdp /* Fill in the user's symbol pointer vector if needed. */ 131733965Sjdp if (symptrs) 131833965Sjdp { 131933965Sjdp long l = symcount; 132033965Sjdp 132133965Sjdp sym = symbase; 132233965Sjdp while (l-- > 0) 132333965Sjdp { 132433965Sjdp *symptrs++ = &sym->symbol; 132533965Sjdp sym++; 132633965Sjdp } 132733965Sjdp *symptrs = 0; /* Final null pointer */ 132833965Sjdp } 132933965Sjdp 1330104834Sobrien if (xverbuf != NULL) 1331104834Sobrien free (xverbuf); 1332104834Sobrien if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf) 1333104834Sobrien free (isymbuf); 133433965Sjdp return symcount; 133589857Sobrien 133633965Sjdperror_return: 1337104834Sobrien if (xverbuf != NULL) 1338104834Sobrien free (xverbuf); 1339104834Sobrien if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf) 1340104834Sobrien free (isymbuf); 134133965Sjdp return -1; 134233965Sjdp} 134333965Sjdp 1344218822Sdim/* Read relocations for ASECT from REL_HDR. There are RELOC_COUNT of 134560484Sobrien them. */ 134633965Sjdp 1347130561Sobrienstatic bfd_boolean 1348130561Sobrienelf_slurp_reloc_table_from_section (bfd *abfd, 1349130561Sobrien asection *asect, 1350130561Sobrien Elf_Internal_Shdr *rel_hdr, 1351130561Sobrien bfd_size_type reloc_count, 1352130561Sobrien arelent *relents, 1353130561Sobrien asymbol **symbols, 1354130561Sobrien bfd_boolean dynamic) 135533965Sjdp{ 1356130561Sobrien const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 1357130561Sobrien void *allocated = NULL; 135833965Sjdp bfd_byte *native_relocs; 135933965Sjdp arelent *relent; 136033965Sjdp unsigned int i; 136133965Sjdp int entsize; 1362104834Sobrien unsigned int symcount; 136333965Sjdp 1364130561Sobrien allocated = bfd_malloc (rel_hdr->sh_size); 136533965Sjdp if (allocated == NULL) 136633965Sjdp goto error_return; 136733965Sjdp 136833965Sjdp if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0 136989857Sobrien || (bfd_bread (allocated, rel_hdr->sh_size, abfd) 137033965Sjdp != rel_hdr->sh_size)) 137133965Sjdp goto error_return; 137233965Sjdp 1373130561Sobrien native_relocs = allocated; 137433965Sjdp 137533965Sjdp entsize = rel_hdr->sh_entsize; 137633965Sjdp BFD_ASSERT (entsize == sizeof (Elf_External_Rel) 137733965Sjdp || entsize == sizeof (Elf_External_Rela)); 137833965Sjdp 1379104834Sobrien if (dynamic) 1380104834Sobrien symcount = bfd_get_dynamic_symcount (abfd); 1381104834Sobrien else 1382104834Sobrien symcount = bfd_get_symcount (abfd); 1383104834Sobrien 138433965Sjdp for (i = 0, relent = relents; 138533965Sjdp i < reloc_count; 138633965Sjdp i++, relent++, native_relocs += entsize) 138733965Sjdp { 138833965Sjdp Elf_Internal_Rela rela; 138933965Sjdp 139033965Sjdp if (entsize == sizeof (Elf_External_Rela)) 1391130561Sobrien elf_swap_reloca_in (abfd, native_relocs, &rela); 139233965Sjdp else 1393130561Sobrien elf_swap_reloc_in (abfd, native_relocs, &rela); 139433965Sjdp 139533965Sjdp /* The address of an ELF reloc is section relative for an object 139633965Sjdp file, and absolute for an executable file or shared library. 139733965Sjdp The address of a normal BFD reloc is always section relative, 139833965Sjdp and the address of a dynamic reloc is absolute.. */ 139933965Sjdp if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic) 140033965Sjdp relent->address = rela.r_offset; 140133965Sjdp else 140233965Sjdp relent->address = rela.r_offset - asect->vma; 140333965Sjdp 140433965Sjdp if (ELF_R_SYM (rela.r_info) == 0) 140533965Sjdp relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; 1406104834Sobrien else if (ELF_R_SYM (rela.r_info) > symcount) 1407104834Sobrien { 1408104834Sobrien (*_bfd_error_handler) 1409104834Sobrien (_("%s(%s): relocation %d has invalid symbol index %ld"), 1410104834Sobrien abfd->filename, asect->name, i, ELF_R_SYM (rela.r_info)); 1411104834Sobrien relent->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; 1412104834Sobrien } 141333965Sjdp else 141433965Sjdp { 1415218822Sdim asymbol **ps; 141633965Sjdp 141733965Sjdp ps = symbols + ELF_R_SYM (rela.r_info) - 1; 141833965Sjdp 1419218822Sdim relent->sym_ptr_ptr = ps; 142033965Sjdp } 142133965Sjdp 142233965Sjdp relent->addend = rela.r_addend; 142333965Sjdp 1424130561Sobrien if ((entsize == sizeof (Elf_External_Rela) 1425130561Sobrien && ebd->elf_info_to_howto != NULL) 1426130561Sobrien || ebd->elf_info_to_howto_rel == NULL) 142733965Sjdp (*ebd->elf_info_to_howto) (abfd, relent, &rela); 142833965Sjdp else 1429130561Sobrien (*ebd->elf_info_to_howto_rel) (abfd, relent, &rela); 143033965Sjdp } 143133965Sjdp 143233965Sjdp if (allocated != NULL) 143333965Sjdp free (allocated); 143433965Sjdp 1435130561Sobrien return TRUE; 143633965Sjdp 143733965Sjdp error_return: 143833965Sjdp if (allocated != NULL) 143933965Sjdp free (allocated); 1440130561Sobrien return FALSE; 144133965Sjdp} 144233965Sjdp 144360484Sobrien/* Read in and swap the external relocs. */ 144460484Sobrien 1445130561Sobrienbfd_boolean 1446130561Sobrienelf_slurp_reloc_table (bfd *abfd, 1447130561Sobrien asection *asect, 1448130561Sobrien asymbol **symbols, 1449130561Sobrien bfd_boolean dynamic) 145060484Sobrien{ 145160484Sobrien struct bfd_elf_section_data * const d = elf_section_data (asect); 145260484Sobrien Elf_Internal_Shdr *rel_hdr; 145360484Sobrien Elf_Internal_Shdr *rel_hdr2; 145460484Sobrien bfd_size_type reloc_count; 145560484Sobrien bfd_size_type reloc_count2; 145660484Sobrien arelent *relents; 145789857Sobrien bfd_size_type amt; 145860484Sobrien 145960484Sobrien if (asect->relocation != NULL) 1460130561Sobrien return TRUE; 146160484Sobrien 146260484Sobrien if (! dynamic) 146360484Sobrien { 146460484Sobrien if ((asect->flags & SEC_RELOC) == 0 146560484Sobrien || asect->reloc_count == 0) 1466130561Sobrien return TRUE; 146760484Sobrien 146860484Sobrien rel_hdr = &d->rel_hdr; 146978828Sobrien reloc_count = NUM_SHDR_ENTRIES (rel_hdr); 147060484Sobrien rel_hdr2 = d->rel_hdr2; 147178828Sobrien reloc_count2 = (rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0); 147260484Sobrien 147360484Sobrien BFD_ASSERT (asect->reloc_count == reloc_count + reloc_count2); 147460484Sobrien BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset 147560484Sobrien || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset)); 147660484Sobrien 147760484Sobrien } 147860484Sobrien else 147960484Sobrien { 148060484Sobrien /* Note that ASECT->RELOC_COUNT tends not to be accurate in this 148160484Sobrien case because relocations against this section may use the 148260484Sobrien dynamic symbol table, and in that case bfd_section_from_shdr 148360484Sobrien in elf.c does not update the RELOC_COUNT. */ 1484218822Sdim if (asect->size == 0) 1485130561Sobrien return TRUE; 148660484Sobrien 148760484Sobrien rel_hdr = &d->this_hdr; 148878828Sobrien reloc_count = NUM_SHDR_ENTRIES (rel_hdr); 148960484Sobrien rel_hdr2 = NULL; 149060484Sobrien reloc_count2 = 0; 149160484Sobrien } 149260484Sobrien 149389857Sobrien amt = (reloc_count + reloc_count2) * sizeof (arelent); 1494130561Sobrien relents = bfd_alloc (abfd, amt); 149560484Sobrien if (relents == NULL) 1496130561Sobrien return FALSE; 149760484Sobrien 149860484Sobrien if (!elf_slurp_reloc_table_from_section (abfd, asect, 149960484Sobrien rel_hdr, reloc_count, 150060484Sobrien relents, 150160484Sobrien symbols, dynamic)) 1502130561Sobrien return FALSE; 150377298Sobrien 150477298Sobrien if (rel_hdr2 150560484Sobrien && !elf_slurp_reloc_table_from_section (abfd, asect, 150660484Sobrien rel_hdr2, reloc_count2, 150760484Sobrien relents + reloc_count, 150860484Sobrien symbols, dynamic)) 1509130561Sobrien return FALSE; 151060484Sobrien 151160484Sobrien asect->relocation = relents; 1512130561Sobrien return TRUE; 151360484Sobrien} 151460484Sobrien 1515218822Sdim#if DEBUG & 2 151633965Sjdpstatic void 1517130561Sobrienelf_debug_section (int num, Elf_Internal_Shdr *hdr) 151833965Sjdp{ 151933965Sjdp fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, 152033965Sjdp hdr->bfd_section != NULL ? hdr->bfd_section->name : "", 152133965Sjdp (long) hdr); 152233965Sjdp fprintf (stderr, 152333965Sjdp "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n", 152433965Sjdp (long) hdr->sh_name, 152533965Sjdp (long) hdr->sh_type, 152633965Sjdp (long) hdr->sh_flags); 152733965Sjdp fprintf (stderr, 152833965Sjdp "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n", 152933965Sjdp (long) hdr->sh_addr, 153033965Sjdp (long) hdr->sh_offset, 153133965Sjdp (long) hdr->sh_size); 153233965Sjdp fprintf (stderr, 153333965Sjdp "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n", 153433965Sjdp (long) hdr->sh_link, 153533965Sjdp (long) hdr->sh_info, 153633965Sjdp (long) hdr->sh_addralign); 153733965Sjdp fprintf (stderr, "sh_entsize = %ld\n", 153833965Sjdp (long) hdr->sh_entsize); 153933965Sjdp fflush (stderr); 154033965Sjdp} 1541218822Sdim#endif 154233965Sjdp 1543218822Sdim#if DEBUG & 1 154433965Sjdpstatic void 1545130561Sobrienelf_debug_file (Elf_Internal_Ehdr *ehdrp) 154633965Sjdp{ 154733965Sjdp fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry); 154833965Sjdp fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff); 154933965Sjdp fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum); 155033965Sjdp fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize); 155133965Sjdp fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff); 155233965Sjdp fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum); 155333965Sjdp fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize); 155433965Sjdp} 155533965Sjdp#endif 155633965Sjdp 1557130561Sobrien/* Create a new BFD as if by bfd_openr. Rather than opening a file, 1558130561Sobrien reconstruct an ELF file by reading the segments out of remote memory 1559130561Sobrien based on the ELF file header at EHDR_VMA and the ELF program headers it 1560130561Sobrien points to. If not null, *LOADBASEP is filled in with the difference 1561130561Sobrien between the VMAs from which the segments were read, and the VMAs the 1562130561Sobrien file headers (and hence BFD's idea of each section's VMA) put them at. 1563130561Sobrien 1564130561Sobrien The function TARGET_READ_MEMORY is called to copy LEN bytes from the 1565130561Sobrien remote memory at target address VMA into the local buffer at MYADDR; it 1566130561Sobrien should return zero on success or an `errno' code on failure. TEMPL must 1567130561Sobrien be a BFD for a target with the word size and byte order found in the 1568130561Sobrien remote memory. */ 1569130561Sobrien 1570130561Sobrienbfd * 1571130561SobrienNAME(_bfd_elf,bfd_from_remote_memory) 1572130561Sobrien (bfd *templ, 1573130561Sobrien bfd_vma ehdr_vma, 1574130561Sobrien bfd_vma *loadbasep, 1575218822Sdim int (*target_read_memory) (bfd_vma, bfd_byte *, int)) 1576130561Sobrien{ 1577130561Sobrien Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ 1578130561Sobrien Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */ 1579130561Sobrien Elf_External_Phdr *x_phdrs; 1580130561Sobrien Elf_Internal_Phdr *i_phdrs, *last_phdr; 1581130561Sobrien bfd *nbfd; 1582130561Sobrien struct bfd_in_memory *bim; 1583130561Sobrien int contents_size; 1584218822Sdim bfd_byte *contents; 1585130561Sobrien int err; 1586130561Sobrien unsigned int i; 1587130561Sobrien bfd_vma loadbase; 1588130561Sobrien 1589130561Sobrien /* Read in the ELF header in external format. */ 1590218822Sdim err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr); 1591130561Sobrien if (err) 1592130561Sobrien { 1593130561Sobrien bfd_set_error (bfd_error_system_call); 1594130561Sobrien errno = err; 1595130561Sobrien return NULL; 1596130561Sobrien } 1597130561Sobrien 1598130561Sobrien /* Now check to see if we have a valid ELF file, and one that BFD can 1599130561Sobrien make use of. The magic number must match, the address size ('class') 1600130561Sobrien and byte-swapping must match our XVEC entry. */ 1601130561Sobrien 1602130561Sobrien if (! elf_file_p (&x_ehdr) 1603130561Sobrien || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT 1604130561Sobrien || x_ehdr.e_ident[EI_CLASS] != ELFCLASS) 1605130561Sobrien { 1606130561Sobrien bfd_set_error (bfd_error_wrong_format); 1607130561Sobrien return NULL; 1608130561Sobrien } 1609130561Sobrien 1610130561Sobrien /* Check that file's byte order matches xvec's */ 1611130561Sobrien switch (x_ehdr.e_ident[EI_DATA]) 1612130561Sobrien { 1613130561Sobrien case ELFDATA2MSB: /* Big-endian */ 1614130561Sobrien if (! bfd_header_big_endian (templ)) 1615130561Sobrien { 1616130561Sobrien bfd_set_error (bfd_error_wrong_format); 1617130561Sobrien return NULL; 1618130561Sobrien } 1619130561Sobrien break; 1620130561Sobrien case ELFDATA2LSB: /* Little-endian */ 1621130561Sobrien if (! bfd_header_little_endian (templ)) 1622130561Sobrien { 1623130561Sobrien bfd_set_error (bfd_error_wrong_format); 1624130561Sobrien return NULL; 1625130561Sobrien } 1626130561Sobrien break; 1627130561Sobrien case ELFDATANONE: /* No data encoding specified */ 1628130561Sobrien default: /* Unknown data encoding specified */ 1629130561Sobrien bfd_set_error (bfd_error_wrong_format); 1630130561Sobrien return NULL; 1631130561Sobrien } 1632130561Sobrien 1633130561Sobrien elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr); 1634130561Sobrien 1635130561Sobrien /* The file header tells where to find the program headers. 1636130561Sobrien These are what we use to actually choose what to read. */ 1637130561Sobrien 1638130561Sobrien if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0) 1639130561Sobrien { 1640130561Sobrien bfd_set_error (bfd_error_wrong_format); 1641130561Sobrien return NULL; 1642130561Sobrien } 1643130561Sobrien 1644130561Sobrien x_phdrs = bfd_malloc (i_ehdr.e_phnum * (sizeof *x_phdrs + sizeof *i_phdrs)); 1645130561Sobrien if (x_phdrs == NULL) 1646130561Sobrien { 1647130561Sobrien bfd_set_error (bfd_error_no_memory); 1648130561Sobrien return NULL; 1649130561Sobrien } 1650218822Sdim err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs, 1651130561Sobrien i_ehdr.e_phnum * sizeof x_phdrs[0]); 1652130561Sobrien if (err) 1653130561Sobrien { 1654130561Sobrien free (x_phdrs); 1655130561Sobrien bfd_set_error (bfd_error_system_call); 1656130561Sobrien errno = err; 1657130561Sobrien return NULL; 1658130561Sobrien } 1659130561Sobrien i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum]; 1660130561Sobrien 1661130561Sobrien contents_size = 0; 1662130561Sobrien last_phdr = NULL; 1663130561Sobrien loadbase = ehdr_vma; 1664130561Sobrien for (i = 0; i < i_ehdr.e_phnum; ++i) 1665130561Sobrien { 1666130561Sobrien elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]); 1667218822Sdim /* IA-64 vDSO may have two mappings for one segment, where one mapping 1668218822Sdim is executable only, and one is read only. We must not use the 1669218822Sdim executable one. */ 1670218822Sdim if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R)) 1671130561Sobrien { 1672130561Sobrien bfd_vma segment_end; 1673130561Sobrien segment_end = (i_phdrs[i].p_offset + i_phdrs[i].p_filesz 1674130561Sobrien + i_phdrs[i].p_align - 1) & -i_phdrs[i].p_align; 1675130561Sobrien if (segment_end > (bfd_vma) contents_size) 1676130561Sobrien contents_size = segment_end; 1677130561Sobrien 1678130561Sobrien if ((i_phdrs[i].p_offset & -i_phdrs[i].p_align) == 0) 1679130561Sobrien loadbase = ehdr_vma - (i_phdrs[i].p_vaddr & -i_phdrs[i].p_align); 1680130561Sobrien 1681130561Sobrien last_phdr = &i_phdrs[i]; 1682130561Sobrien } 1683130561Sobrien } 1684130561Sobrien if (last_phdr == NULL) 1685130561Sobrien { 1686130561Sobrien /* There were no PT_LOAD segments, so we don't have anything to read. */ 1687130561Sobrien free (x_phdrs); 1688130561Sobrien bfd_set_error (bfd_error_wrong_format); 1689130561Sobrien return NULL; 1690130561Sobrien } 1691130561Sobrien 1692130561Sobrien /* Trim the last segment so we don't bother with zeros in the last page 1693130561Sobrien that are off the end of the file. However, if the extra bit in that 1694130561Sobrien page includes the section headers, keep them. */ 1695130561Sobrien if ((bfd_vma) contents_size > last_phdr->p_offset + last_phdr->p_filesz 1696130561Sobrien && (bfd_vma) contents_size >= (i_ehdr.e_shoff 1697130561Sobrien + i_ehdr.e_shnum * i_ehdr.e_shentsize)) 1698130561Sobrien { 1699130561Sobrien contents_size = last_phdr->p_offset + last_phdr->p_filesz; 1700130561Sobrien if ((bfd_vma) contents_size < (i_ehdr.e_shoff 1701130561Sobrien + i_ehdr.e_shnum * i_ehdr.e_shentsize)) 1702130561Sobrien contents_size = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize; 1703130561Sobrien } 1704130561Sobrien else 1705130561Sobrien contents_size = last_phdr->p_offset + last_phdr->p_filesz; 1706130561Sobrien 1707130561Sobrien /* Now we know the size of the whole image we want read in. */ 1708130561Sobrien contents = bfd_zmalloc (contents_size); 1709130561Sobrien if (contents == NULL) 1710130561Sobrien { 1711130561Sobrien free (x_phdrs); 1712130561Sobrien bfd_set_error (bfd_error_no_memory); 1713130561Sobrien return NULL; 1714130561Sobrien } 1715130561Sobrien 1716130561Sobrien for (i = 0; i < i_ehdr.e_phnum; ++i) 1717218822Sdim /* IA-64 vDSO may have two mappings for one segment, where one mapping 1718218822Sdim is executable only, and one is read only. We must not use the 1719218822Sdim executable one. */ 1720218822Sdim if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R)) 1721130561Sobrien { 1722130561Sobrien bfd_vma start = i_phdrs[i].p_offset & -i_phdrs[i].p_align; 1723130561Sobrien bfd_vma end = (i_phdrs[i].p_offset + i_phdrs[i].p_filesz 1724130561Sobrien + i_phdrs[i].p_align - 1) & -i_phdrs[i].p_align; 1725130561Sobrien if (end > (bfd_vma) contents_size) 1726130561Sobrien end = contents_size; 1727130561Sobrien err = target_read_memory ((loadbase + i_phdrs[i].p_vaddr) 1728130561Sobrien & -i_phdrs[i].p_align, 1729130561Sobrien contents + start, end - start); 1730130561Sobrien if (err) 1731130561Sobrien { 1732130561Sobrien free (x_phdrs); 1733130561Sobrien free (contents); 1734130561Sobrien bfd_set_error (bfd_error_system_call); 1735130561Sobrien errno = err; 1736130561Sobrien return NULL; 1737130561Sobrien } 1738130561Sobrien } 1739130561Sobrien free (x_phdrs); 1740130561Sobrien 1741130561Sobrien /* If the segments visible in memory didn't include the section headers, 1742130561Sobrien then clear them from the file header. */ 1743130561Sobrien if ((bfd_vma) contents_size < (i_ehdr.e_shoff 1744130561Sobrien + i_ehdr.e_shnum * i_ehdr.e_shentsize)) 1745130561Sobrien { 1746130561Sobrien memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff); 1747130561Sobrien memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum); 1748130561Sobrien memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx); 1749130561Sobrien } 1750130561Sobrien 1751130561Sobrien /* This will normally have been in the first PT_LOAD segment. But it 1752130561Sobrien conceivably could be missing, and we might have just changed it. */ 1753130561Sobrien memcpy (contents, &x_ehdr, sizeof x_ehdr); 1754130561Sobrien 1755130561Sobrien /* Now we have a memory image of the ELF file contents. Make a BFD. */ 1756130561Sobrien bim = bfd_malloc (sizeof (struct bfd_in_memory)); 1757130561Sobrien if (bim == NULL) 1758130561Sobrien { 1759130561Sobrien free (contents); 1760130561Sobrien bfd_set_error (bfd_error_no_memory); 1761130561Sobrien return NULL; 1762130561Sobrien } 1763130561Sobrien nbfd = _bfd_new_bfd (); 1764130561Sobrien if (nbfd == NULL) 1765130561Sobrien { 1766130561Sobrien free (bim); 1767130561Sobrien free (contents); 1768130561Sobrien bfd_set_error (bfd_error_no_memory); 1769130561Sobrien return NULL; 1770130561Sobrien } 1771130561Sobrien nbfd->filename = "<in-memory>"; 1772130561Sobrien nbfd->xvec = templ->xvec; 1773130561Sobrien bim->size = contents_size; 1774130561Sobrien bim->buffer = contents; 1775130561Sobrien nbfd->iostream = bim; 1776130561Sobrien nbfd->flags = BFD_IN_MEMORY; 1777130561Sobrien nbfd->direction = read_direction; 1778130561Sobrien nbfd->mtime = time (NULL); 1779130561Sobrien nbfd->mtime_set = TRUE; 1780130561Sobrien 1781130561Sobrien if (loadbasep) 1782130561Sobrien *loadbasep = loadbase; 1783130561Sobrien return nbfd; 1784130561Sobrien} 1785130561Sobrien 178633965Sjdp#include "elfcore.h" 178733965Sjdp 178833965Sjdp/* Size-dependent data and functions. */ 178933965Sjdpconst struct elf_size_info NAME(_bfd_elf,size_info) = { 179033965Sjdp sizeof (Elf_External_Ehdr), 179133965Sjdp sizeof (Elf_External_Phdr), 179233965Sjdp sizeof (Elf_External_Shdr), 179333965Sjdp sizeof (Elf_External_Rel), 179433965Sjdp sizeof (Elf_External_Rela), 179533965Sjdp sizeof (Elf_External_Sym), 179633965Sjdp sizeof (Elf_External_Dyn), 179733965Sjdp sizeof (Elf_External_Note), 179877298Sobrien 4, 179960484Sobrien 1, 1800130561Sobrien ARCH_SIZE, LOG_FILE_ALIGN, 180133965Sjdp ELFCLASS, EV_CURRENT, 180233965Sjdp elf_write_out_phdrs, 180333965Sjdp elf_write_shdrs_and_ehdr, 180477298Sobrien elf_write_relocs, 1805104834Sobrien elf_swap_symbol_in, 180633965Sjdp elf_swap_symbol_out, 180733965Sjdp elf_slurp_reloc_table, 180833965Sjdp elf_slurp_symbol_table, 180960484Sobrien elf_swap_dyn_in, 181060484Sobrien elf_swap_dyn_out, 1811130561Sobrien elf_swap_reloc_in, 1812130561Sobrien elf_swap_reloc_out, 1813130561Sobrien elf_swap_reloca_in, 1814130561Sobrien elf_swap_reloca_out 181533965Sjdp}; 1816