+# 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
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
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
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