annotate python/elffile.py @ 118:db8aafe00d27

Added elf file read scripts
author Windel Bouwman
date Sat, 12 Jan 2013 09:39:23 +0100
parents
children bbf4c9b138d4
rev   line source
118
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
1 import struct
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
2
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
3 classes = {1:'ELF32', 2:'ELF64'}
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
4
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
5 class ElfFile:
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
6 def __init__(self, f):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
7 self.f = f
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
8 self.parse()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
9 def parse(self):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
10 self.parseHeader()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
11 self.parseSections()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
12 self.parsePrograms()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
13 @property
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
14 def ElfClass(self):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
15 return classes[self.ei_class]
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
16 def parseHeader(self):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
17 # Parse identifier
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
18 e_ident = self.f.read(16)
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
19 ELFMAGIC = bytes([0x7f]) + b'ELF'
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
20 assert e_ident[0:4] == ELFMAGIC
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
21 self.ei_class = e_ident[4]
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
22 print('ELF class: ', self.ElfClass)
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
23 assert self.ei_class in classes.keys()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
24 self.ei_data = e_ident[5]
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
25 print('ei_data:', self.ei_data)
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
26 ei_version = e_ident[6]
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
27 assert ei_version == 1
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
28
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
29 # rest of the ELF header:
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
30 e_type = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
31 e_machine = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
32 e_version = self.readWord()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
33 e_entry = self.readAddr()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
34 e_phoff = self.readOff()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
35 e_shoff = self.readOff()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
36 e_flags = self.readWord()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
37 e_ehsize = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
38 e_phentsize = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
39 e_phnum = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
40 e_shentsize = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
41 e_shnum = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
42 e_shstrndx = self.readHalf()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
43
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
44 if self.ei_class == 1:
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
45 assert e_ehsize == 52
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
46 elif self.ei_class == 2:
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
47 assert e_ehsize == 64
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
48 print('program header size', e_phentsize, e_phnum)
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
49 print('section header size', e_shentsize, e_shnum, e_shstrndx)
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
50 def readFmt(self, fmt):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
51 size = struct.calcsize(fmt)
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
52 return struct.unpack(fmt, self.f.read(size))
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
53 def readHalf(self):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
54 return self.readFmt('<H')[0]
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
55 def readWord(self):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
56 return self.readFmt('<I')[0]
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
57 def readAddr(self):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
58 if self.ei_class == 1:
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
59 fmt = '<I'
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
60 elif self.ei_class == 2:
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
61 fmt = '<Q'
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
62 else:
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
63 raise Exception()
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
64 return self.readFmt(fmt)[0]
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
65 readOff = readAddr
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
66
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
67 def printProgramHeaders(self):
db8aafe00d27 Added elf file read scripts
Windel Bouwman
parents:
diff changeset
68 pass