elfcode.h

来自「基于4个mips核的noc设计」· C头文件 代码 · 共 1,567 行 · 第 1/4 页

H
1,567
字号
/* ELF executable support for BFD.   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,   2001 Free Software Foundation, Inc.   Written by Fred Fish @ Cygnus Support, from information published   in "UNIX System V Release 4, Programmers Guide: ANSI C and   Programming Support Tools".  Sufficient support for gdb.   Rewritten by Mark Eichin @ Cygnus Support, from information   published in "System V Application Binary Interface", chapters 4   and 5, as well as the various "Processor Supplement" documents   derived from it. Added support for assembler and other object file   utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael   Meissner (Open Software Foundation), and Peter Hoogenboom (University   of Utah) to finish and extend this.This file is part of BFD, the Binary File Descriptor library.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  *//* Problems and other issues to resolve.   (1)	BFD expects there to be some fixed number of "sections" in        the object file.  I.E. there is a "section_count" variable in the	bfd structure which contains the number of sections.  However, ELF	supports multiple "views" of a file.  In particular, with current	implementations, executable files typically have two tables, a	program header table and a section header table, both of which	partition the executable.	In ELF-speak, the "linking view" of the file uses the section header	table to access "sections" within the file, and the "execution view"	uses the program header table to access "segments" within the file.	"Segments" typically may contain all the data from one or more	"sections".	Note that the section header table is optional in ELF executables,	but it is this information that is most useful to gdb.  If the	section header table is missing, then gdb should probably try	to make do with the program header table.  (FIXME)   (2)  The code in this file is compiled twice, once in 32-bit mode and	once in 64-bit mode.  More of it should be made size-independent	and moved into elf.c.   (3)	ELF section symbols are handled rather sloppily now.  This should	be cleaned up, and ELF section symbols reconciled with BFD section	symbols.   (4)  We need a published spec for 64-bit ELF.  We've got some stuff here	that we're using for SPARC V9 64-bit chips, but don't assume that	it's cast in stone. */#include "bfd.h"#include "sysdep.h"#include "bfdlink.h"#include "libbfd.h"#include "elf-bfd.h"/* Renaming structures, typedefs, macros and functions to be size-specific.  */#define Elf_External_Ehdr	NAME(Elf,External_Ehdr)#define Elf_External_Sym	NAME(Elf,External_Sym)#define Elf_External_Shdr	NAME(Elf,External_Shdr)#define Elf_External_Phdr	NAME(Elf,External_Phdr)#define Elf_External_Rel	NAME(Elf,External_Rel)#define Elf_External_Rela	NAME(Elf,External_Rela)#define Elf_External_Dyn	NAME(Elf,External_Dyn)#define elf_core_file_failing_command	NAME(bfd_elf,core_file_failing_command)#define elf_core_file_failing_signal	NAME(bfd_elf,core_file_failing_signal)#define elf_core_file_matches_executable_p \  NAME(bfd_elf,core_file_matches_executable_p)#define elf_object_p			NAME(bfd_elf,object_p)#define elf_core_file_p			NAME(bfd_elf,core_file_p)#define elf_get_symtab_upper_bound	NAME(bfd_elf,get_symtab_upper_bound)#define elf_get_dynamic_symtab_upper_bound \  NAME(bfd_elf,get_dynamic_symtab_upper_bound)#define elf_swap_reloc_in		NAME(bfd_elf,swap_reloc_in)#define elf_swap_reloca_in		NAME(bfd_elf,swap_reloca_in)#define elf_swap_reloc_out		NAME(bfd_elf,swap_reloc_out)#define elf_swap_reloca_out		NAME(bfd_elf,swap_reloca_out)#define elf_swap_symbol_in		NAME(bfd_elf,swap_symbol_in)#define elf_swap_symbol_out		NAME(bfd_elf,swap_symbol_out)#define elf_swap_phdr_in		NAME(bfd_elf,swap_phdr_in)#define elf_swap_phdr_out		NAME(bfd_elf,swap_phdr_out)#define elf_swap_dyn_in			NAME(bfd_elf,swap_dyn_in)#define elf_swap_dyn_out		NAME(bfd_elf,swap_dyn_out)#define elf_get_reloc_upper_bound	NAME(bfd_elf,get_reloc_upper_bound)#define elf_canonicalize_reloc		NAME(bfd_elf,canonicalize_reloc)#define elf_slurp_symbol_table		NAME(bfd_elf,slurp_symbol_table)#define elf_get_symtab			NAME(bfd_elf,get_symtab)#define elf_canonicalize_dynamic_symtab \  NAME(bfd_elf,canonicalize_dynamic_symtab)#define elf_make_empty_symbol		NAME(bfd_elf,make_empty_symbol)#define elf_get_symbol_info		NAME(bfd_elf,get_symbol_info)#define elf_get_lineno			NAME(bfd_elf,get_lineno)#define elf_set_arch_mach		NAME(bfd_elf,set_arch_mach)#define elf_find_nearest_line		NAME(bfd_elf,find_nearest_line)#define elf_sizeof_headers		NAME(bfd_elf,sizeof_headers)#define elf_set_section_contents	NAME(bfd_elf,set_section_contents)#define elf_no_info_to_howto		NAME(bfd_elf,no_info_to_howto)#define elf_no_info_to_howto_rel	NAME(bfd_elf,no_info_to_howto_rel)#define elf_find_section		NAME(bfd_elf,find_section)#define elf_bfd_link_add_symbols	NAME(bfd_elf,bfd_link_add_symbols)#define elf_add_dynamic_entry		NAME(bfd_elf,add_dynamic_entry)#define elf_write_shdrs_and_ehdr	NAME(bfd_elf,write_shdrs_and_ehdr)#define elf_write_out_phdrs		NAME(bfd_elf,write_out_phdrs)#define elf_write_relocs		NAME(bfd_elf,write_relocs)#define elf_slurp_reloc_table		NAME(bfd_elf,slurp_reloc_table)#define elf_link_create_dynamic_sections \  NAME(bfd_elf,link_create_dynamic_sections)#define elf_link_record_dynamic_symbol  _bfd_elf_link_record_dynamic_symbol#define elf_bfd_final_link		NAME(bfd_elf,bfd_final_link)#define elf_create_pointer_linker_section NAME(bfd_elf,create_pointer_linker_section)#define elf_finish_pointer_linker_section NAME(bfd_elf,finish_pointer_linker_section)#define elf_gc_sections			NAME(_bfd_elf,gc_sections)#define elf_gc_common_finalize_got_offsets \  NAME(_bfd_elf,gc_common_finalize_got_offsets)#define elf_gc_common_final_link	NAME(_bfd_elf,gc_common_final_link)#define elf_gc_record_vtinherit		NAME(_bfd_elf,gc_record_vtinherit)#define elf_gc_record_vtentry		NAME(_bfd_elf,gc_record_vtentry)#define elf_link_record_local_dynamic_symbol \  NAME(_bfd_elf,link_record_local_dynamic_symbol)#if ARCH_SIZE == 64#define ELF_R_INFO(X,Y)	ELF64_R_INFO(X,Y)#define ELF_R_SYM(X)	ELF64_R_SYM(X)#define ELF_R_TYPE(X)	ELF64_R_TYPE(X)#define ELFCLASS	ELFCLASS64#define FILE_ALIGN	8#define LOG_FILE_ALIGN	3#endif#if ARCH_SIZE == 32#define ELF_R_INFO(X,Y)	ELF32_R_INFO(X,Y)#define ELF_R_SYM(X)	ELF32_R_SYM(X)#define ELF_R_TYPE(X)	ELF32_R_TYPE(X)#define ELFCLASS	ELFCLASS32#define FILE_ALIGN	4#define LOG_FILE_ALIGN	2#endif/* Static functions */static void elf_swap_ehdr_in  PARAMS ((bfd *, const Elf_External_Ehdr *, Elf_Internal_Ehdr *));static void elf_swap_ehdr_out  PARAMS ((bfd *, const Elf_Internal_Ehdr *, Elf_External_Ehdr *));static void elf_swap_shdr_in  PARAMS ((bfd *, const Elf_External_Shdr *, Elf_Internal_Shdr *));static void elf_swap_shdr_out  PARAMS ((bfd *, const Elf_Internal_Shdr *, Elf_External_Shdr *));#define elf_stringtab_init _bfd_elf_stringtab_init#define section_from_elf_index bfd_section_from_elf_indexstatic boolean elf_slurp_reloc_table_from_section  PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, bfd_size_type,	   arelent *, asymbol **, boolean));static boolean elf_file_p PARAMS ((Elf_External_Ehdr *));#ifdef DEBUGstatic void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *));static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));static char *elf_symbol_flags PARAMS ((flagword));#endif/* Structure swapping routines *//* Should perhaps use put_offset, put_word, etc.  For now, the two versions   can be handled by explicitly specifying 32 bits or "the long type".  */#if ARCH_SIZE == 64#define put_word	bfd_h_put_64#define put_signed_word	bfd_h_put_signed_64#define get_word	bfd_h_get_64#define get_signed_word	bfd_h_get_signed_64#endif#if ARCH_SIZE == 32#define put_word	bfd_h_put_32#define put_signed_word	bfd_h_put_signed_32#define get_word	bfd_h_get_32#define get_signed_word	bfd_h_get_signed_32#endif/* Translate an ELF symbol in external format into an ELF symbol in internal   format.  */voidelf_swap_symbol_in (abfd, src, dst)     bfd *abfd;     const Elf_External_Sym *src;     Elf_Internal_Sym *dst;{  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;  dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);  if (signed_vma)    dst->st_value = get_signed_word (abfd, (bfd_byte *) src->st_value);  else    dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);  dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);  dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);  dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);  dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);}/* Translate an ELF symbol in internal format into an ELF symbol in external   format.  */voidelf_swap_symbol_out (abfd, src, cdst)     bfd *abfd;     const Elf_Internal_Sym *src;     PTR cdst;{  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;  bfd_h_put_32 (abfd, src->st_name, dst->st_name);  put_word (abfd, src->st_value, dst->st_value);  put_word (abfd, src->st_size, dst->st_size);  bfd_h_put_8 (abfd, src->st_info, dst->st_info);  bfd_h_put_8 (abfd, src->st_other, dst->st_other);  bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);}/* Translate an ELF file header in external format into an ELF file header in   internal format.  */static voidelf_swap_ehdr_in (abfd, src, dst)     bfd *abfd;     const Elf_External_Ehdr *src;     Elf_Internal_Ehdr *dst;{  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);  dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);  dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);  dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);  if (signed_vma)    dst->e_entry = get_signed_word (abfd, (bfd_byte *) src->e_entry);  else    dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);  dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);  dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);  dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);  dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);  dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);  dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);  dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);  dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);  dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);}/* Translate an ELF file header in internal format into an ELF file header in   external format.  */static voidelf_swap_ehdr_out (abfd, src, dst)     bfd *abfd;     const Elf_Internal_Ehdr *src;     Elf_External_Ehdr *dst;{  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);  /* note that all elements of dst are *arrays of unsigned char* already...  */  bfd_h_put_16 (abfd, src->e_type, dst->e_type);  bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);  bfd_h_put_32 (abfd, src->e_version, dst->e_version);  if (signed_vma)    put_signed_word (abfd, src->e_entry, dst->e_entry);  else    put_word (abfd, src->e_entry, dst->e_entry);  put_word (abfd, src->e_phoff, dst->e_phoff);  put_word (abfd, src->e_shoff, dst->e_shoff);  bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);  bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);  bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);  bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);  bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);  bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);  bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);}/* Translate an ELF section header table entry in external format into an   ELF section header table entry in internal format.  */static voidelf_swap_shdr_in (abfd, src, dst)     bfd *abfd;     const Elf_External_Shdr *src;     Elf_Internal_Shdr *dst;{  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;  dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);  dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);  dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);  if (signed_vma)    dst->sh_addr = get_signed_word (abfd, (bfd_byte *) src->sh_addr);  else    dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);  dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);  dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);  dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);  dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);  dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);  dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);  dst->bfd_section = NULL;  dst->contents = NULL;}/* Translate an ELF section header table entry in internal format into an   ELF section header table entry in external format.  */static voidelf_swap_shdr_out (abfd, src, dst)     bfd *abfd;     const Elf_Internal_Shdr *src;     Elf_External_Shdr *dst;{  /* note that all elements of dst are *arrays of unsigned char* already...  */  bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);  bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);  put_word (abfd, src->sh_flags, dst->sh_flags);  put_word (abfd, src->sh_addr, dst->sh_addr);  put_word (abfd, src->sh_offset, dst->sh_offset);  put_word (abfd, src->sh_size, dst->sh_size);  bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);  bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);  put_word (abfd, src->sh_addralign, dst->sh_addralign);  put_word (abfd, src->sh_entsize, dst->sh_entsize);}/* Translate an ELF program header table entry in external format into an   ELF program header table entry in internal format.  */voidelf_swap_phdr_in (abfd, src, dst)     bfd *abfd;     const Elf_External_Phdr *src;     Elf_Internal_Phdr *dst;{  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;  dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);  dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);  dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);  if (signed_vma)    {      dst->p_vaddr = get_signed_word (abfd, (bfd_byte *) src->p_vaddr);      dst->p_paddr = get_signed_word (abfd, (bfd_byte *) src->p_paddr);    }  else    {      dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);      dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);    }  dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);  dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);  dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);}voidelf_swap_phdr_out (abfd, src, dst)     bfd *abfd;     const Elf_Internal_Phdr *src;     Elf_External_Phdr *dst;{  /* note that all elements of dst are *arrays of unsigned char* already...  */  bfd_h_put_32 (abfd, src->p_type, dst->p_type);  put_word (abfd, src->p_offset, dst->p_offset);  put_word (abfd, src->p_vaddr, dst->p_vaddr);  put_word (abfd, src->p_paddr, dst->p_paddr);  put_word (abfd, src->p_filesz, dst->p_filesz);  put_word (abfd, src->p_memsz, dst->p_memsz);  bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);  put_word (abfd, src->p_align, dst->p_align);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?