some structs filled in
authorebenders <devnull@localhost>
Wed, 7 Sep 2011 12:21:07 +0000 (15:21 +0300)
committerebenders <devnull@localhost>
Wed, 7 Sep 2011 12:21:07 +0000 (15:21 +0300)
elftools/elf/enums.py
elftools/elf/flags.py [new file with mode: 0644]
elftools/elf/structs.py
z.py

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