From: ebenders Date: Wed, 7 Sep 2011 12:21:07 +0000 (+0300) Subject: some structs filled in X-Git-Tag: v0.10~143 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=e790d0db44e5a59e806390e59a603c852f00b367;p=pyelftools.git some structs filled in --- diff --git a/elftools/elf/enums.py b/elftools/elf/enums.py index e69de29..0b76c52 100644 --- a/elftools/elf/enums.py +++ b/elftools/elf/enums.py @@ -0,0 +1,84 @@ +# Mappings of enum names<->values to be inserted into construct's Enum adapter +# + +# e_ident[EI_CLASS] in the ELF header +ENUM_EI_CLASS = dict( + ELFCLASSNONE=0, + ELFCLASS32=1, + ELFCLASS64=2 +) + +# e_ident[EI_DATA] in the ELF header +ENUM_EI_DATA = dict( + ELFDATANONE=0, + ELFDATA2LSB=1, + ELFDATA2MSB=2 +) + +# e_version in the ELF header +ENUM_E_VERSION = dict( + EV_NONE=0, + EV_CURRENT=1 +) + +# e_type in the ELF header +ENUM_E_TYPE = dict( + ET_NONE=0, + ET_REL=1, + ET_EXEC=2, + ET_DYN=3, + ET_CORE=4, + ET_LOPROC=0xff00, + ET_HIPROC=0xffff, + _default_='PROC_SPECIFIC', +) + +# e_machine in the ELF header +# (this list is currently somewhat partial...) +ENUM_E_MACHINE = dict( + EM_NONE=0, + EM_M32=1, + EM_SPARC=2, + EM_386=3, + EM_68K=4, + EM_88K=5, + EM_860=7, + EM_MIPS=8, + EM_S370=9, + EM_MIPS_RS4_BE=10, + EM_IA_64=50, + EM_X86_64=62, + EM_AVR=83, + _default_='RESERVED', +) + +# sh_type in the section header +ENUM_SH_TYPE = dict( + SHT_NULL=0, + SHT_PROGBITS=1, + SHT_SYMTAB=2, + SHT_STRTAB=3, + SHT_RELA=4, + SHT_HASH=5, + SHT_DYNAMIC=6, + SHT_NOTE=7, + SHT_NOBITS=8, + SHT_REL=9, + SHT_SHLIB=10, + SHT_DYNSYM=11, + SHT_INIT_ARRAY=14, + SHT_FINI_ARRAY=15, + SHT_PREINIT_ARRAY=16, + SHT_GROUP=17, + SHT_SYMTAB_SHNDX=18, + SHT_NUM=19, + SHT_LOOS=0x60000000, + SHT_HIOS=0x6fffffff, + SHT_LOPROC=0x70000000, + SHT_HIPROC=0x7fffffff, + SHT_LOUSER=0x80000000, + SHT_HIUSER=0xffffffff, + SHT_AMD64_UNWIND=0x70000001, + _default_='RESERVED', +) + diff --git a/elftools/elf/flags.py b/elftools/elf/flags.py new file mode 100644 index 0000000..32a1ed3 --- /dev/null +++ b/elftools/elf/flags.py @@ -0,0 +1,16 @@ +# Flag values, placed into classes for namespacing + +class SH_FLAGS(object): + SHF_WRITE=0x1 + SHF_ALLOC=0x2 + SHF_EXECINSTR=0x4 + SHF_MERGE=0x10 + SHF_STRINGS=0x20 + SHF_INFO_LINK=0x40 + SHF_LINK_ORDER=0x80 + SHF_OS_NONCONFORMING=0x100 + SHF_GROUP=0x200 + SHF_TLS=0x400 + SHF_MASKOS=0x0ff00000 + SHF_MASKPROC=0xf0000000 + diff --git a/elftools/elf/structs.py b/elftools/elf/structs.py index 87725a8..bfaca07 100644 --- a/elftools/elf/structs.py +++ b/elftools/elf/structs.py @@ -2,9 +2,11 @@ from ..construct import ( UBInt8, UBInt16, UBInt32, UBInt64, ULInt8, ULInt16, ULInt32, ULInt64, SBInt32, SLInt32, SBInt64, SLInt64, - Struct, Array, + Struct, Array, Enum, Padding, ) +from .enums import * + class ELFStructs(object): def __init__(self, little_endian=True, elfclass=32): @@ -21,8 +23,8 @@ class ELFStructs(object): self.Elf_addr = ULInt32 if self.elfclass == 32 else ULInt64 self.Elf_offset = self.Elf_addr self.Elf_sword = SLInt32 - self.Elf_xword = ULInt64 - self.Elf_sxword = SLInt64 + self.Elf_xword = ULInt32 if self.elfclass == 32 else ULInt64 + self.Elf_sxword = SLInt32 if self.elfclass == 32 else SLInt64 else: self.Elf_byte = UBInt8 self.Elf_half = UBInt16 @@ -30,8 +32,68 @@ class ELFStructs(object): self.Elf_addr = UBInt32 if self.elfclass == 32 else UBInt64 self.Elf_offset = self.Elf_addr self.Elf_sword = SBInt32 - self.Elf_xword = UBInt64 - self.Elf_sxword = SBInt64 + self.Elf_xword = UBInt32 if self.elfclass == 32 else UBInt64 + self.Elf_sxword = SBInt32 if self.elfclass == 32 else SBInt64 + self._create_ehdr() + + def _create_ehdr(self): self.Elf_Ehdr = Struct('Elf_Ehdr', - \ No newline at end of file + Struct('e_ident', + Array(4, self.Elf_byte('EI_MAG')), + Enum(self.Elf_byte('EI_CLASS'), **ENUM_EI_CLASS), + Enum(self.Elf_byte('EI_DATA'), **ENUM_EI_DATA), + Enum(self.Elf_byte('EI_VERSION'), **ENUM_E_VERSION), + Padding(9) + ), + Enum(self.Elf_half('e_type'), **ENUM_E_TYPE), + Enum(self.Elf_half('e_machine'), **ENUM_E_MACHINE), + Enum(self.Elf_word('e_version'), **ENUM_E_VERSION), + self.Elf_addr('e_entry'), + self.Elf_offset('e_phoff'), + self.Elf_offset('e_shoff'), + self.Elf_word('e_flags'), + self.Elf_half('e_ehsize'), + self.Elf_half('e_phentsize'), + self.Elf_half('e_phnum'), + self.Elf_half('e_shentsize'), + self.Elf_half('e_shnum'), + self.Elf_half('e_shstrndx'), + ) + + def _create_shdr(self): + self.Elf_Shdr = Struct('Elf_Shdr', + self.Elf_word('sh_name'), + Enum(self.Elf_word('sh_type'), **ENUM_SH_TYPE), + self.Elf_xword('sh_flags'), + self.Elf_addr('sh_addr'), + self.Elf_offset('sh_offset'), + self.Elf_xword('sh_size'), + self.Elf_word('sh_link'), + self.Elf_word('sh_info'), + self.Elf_xword('sh_addralign'), + self.Elf_xword('sh_entsize'), + ) + + def _create_sym(self): + if self.elfclass == 32: + self.Elf_Sym = Struct('Elf_Sym', + self.Elf_word('st_name'), + self.Elf_addr('st_value'), + self.Elf_word('st_size'), + self.Elf_byte('st_info'), + self.Elf_byte('st_other'), + self.Elf_half('st_shndx'), + ) + else: + self.Elf_Sym = Struct('Elf_Sym', + self.Elf_word('st_name'), + self.Elf_byte('st_info'), + self.Elf_byte('st_other'), + self.Elf_half('st_shndx'), + self.Elf_addr('st_value'), + self.Elf_xword('st_size'), + ) + + + diff --git a/z.py b/z.py index 22d4d03..b52b969 100644 --- a/z.py +++ b/z.py @@ -1,2 +1,10 @@ from elftools.elf.structs import ELFStructs +es = ELFStructs(True, 64) + + +stream = open('binfiles/z.elf', 'rb') +print es.Elf_Ehdr.parse_stream(stream) + + +#~ print es.Elf_Ehdr