* also create other stuff in struct constructor
authorebenders <devnull@localhost>
Wed, 7 Sep 2011 12:36:11 +0000 (15:36 +0300)
committerebenders <devnull@localhost>
Wed, 7 Sep 2011 12:36:11 +0000 (15:36 +0300)
* sample for getting ELF's string table contents

elftools/elf/structs.py
z.py

index bfaca070184c89cf47d460eeb4f522775268eb9c..090a8a9d0e709c5a8ec406014669dd7860e4a3fe 100644 (file)
@@ -36,6 +36,8 @@ class ELFStructs(object):
             self.Elf_sxword = SBInt32 if self.elfclass == 32 else SBInt64\r
         \r
         self._create_ehdr()\r
+        self._create_shdr()\r
+        self._create_sym()\r
     \r
     def _create_ehdr(self):
         self.Elf_Ehdr = Struct('Elf_Ehdr',\r
diff --git a/z.py b/z.py
index b52b969060af8e3eb246e7e24c1c03981b04eeeb..aa4672eef7e0a8314547a6fa65b72779e4bbe1a4 100644 (file)
--- a/z.py
+++ b/z.py
@@ -1,10 +1,30 @@
+import sys\r
 from elftools.elf.structs import ELFStructs\r
 \r
+# read a little-endian, 64-bit file\r
 es = ELFStructs(True, 64)\r
 \r
-\r
 stream = open('binfiles/z.elf', 'rb')\r
-print es.Elf_Ehdr.parse_stream(stream)\r
+eheader = es.Elf_Ehdr.parse_stream(stream)\r
+\r
+print eheader\r
+\r
+shtable_offset = eheader.e_shoff\r
+strtable_section_offset = shtable_offset + eheader.e_shstrndx * eheader.e_shentsize\r
+\r
+# get to the section header for the sh string table\r
+print strtable_section_offset\r
+stream.seek(strtable_section_offset)\r
+sheader = es.Elf_Shdr.parse_stream(stream)\r
+print sheader\r
+\r
+# yay, looks correct!!\r
+stream.seek(sheader.sh_offset)\r
+buf = stream.read(sheader.sh_size)\r
+for c in buf:\r
+    sys.stdout.write('%02X' % ord(c))\r
+\r
+\r
 \r
 \r
 #~ print es.Elf_Ehdr\r