From ec1e088e65b3d72d9c89c2a7c49176b2fc094982 Mon Sep 17 00:00:00 2001 From: ebenders Date: Wed, 7 Sep 2011 15:36:11 +0300 Subject: [PATCH] * also create other stuff in struct constructor * sample for getting ELF's string table contents --- elftools/elf/structs.py | 2 ++ z.py | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/elftools/elf/structs.py b/elftools/elf/structs.py index bfaca07..090a8a9 100644 --- a/elftools/elf/structs.py +++ b/elftools/elf/structs.py @@ -36,6 +36,8 @@ class ELFStructs(object): self.Elf_sxword = SBInt32 if self.elfclass == 32 else SBInt64 self._create_ehdr() + self._create_shdr() + self._create_sym() def _create_ehdr(self): self.Elf_Ehdr = Struct('Elf_Ehdr', diff --git a/z.py b/z.py index b52b969..aa4672e 100644 --- a/z.py +++ b/z.py @@ -1,10 +1,30 @@ +import sys from elftools.elf.structs import ELFStructs +# read a little-endian, 64-bit file es = ELFStructs(True, 64) - stream = open('binfiles/z.elf', 'rb') -print es.Elf_Ehdr.parse_stream(stream) +eheader = es.Elf_Ehdr.parse_stream(stream) + +print eheader + +shtable_offset = eheader.e_shoff +strtable_section_offset = shtable_offset + eheader.e_shstrndx * eheader.e_shentsize + +# get to the section header for the sh string table +print strtable_section_offset +stream.seek(strtable_section_offset) +sheader = es.Elf_Shdr.parse_stream(stream) +print sheader + +# yay, looks correct!! +stream.seek(sheader.sh_offset) +buf = stream.read(sheader.sh_size) +for c in buf: + sys.stdout.write('%02X' % ord(c)) + + #~ print es.Elf_Ehdr -- 2.30.2