Create aout class.

This commit is contained in:
Krystian Bacławski
2013-05-25 17:08:00 +02:00
parent 140ae616aa
commit b8aee8e3a3
2 changed files with 93 additions and 67 deletions
+4 -67
View File
@@ -1,73 +1,10 @@
#!/usr/bin/env python2.7 -B
import os
import struct
import sys
from objtools import aout, util
from objtools import aout
if __name__ == '__main__':
for path in sys.argv[1:]:
with open(path) as data:
print 'Parsing {0} of size {1} bytes.'.format(
repr(path), os.stat(path)[6])
header = aout.Header.decode(data.read(32))
text_seg = data.read(header.text)
data_seg = data.read(header.data)
text_reloc = data.read(header.trsize)
data_reloc = data.read(header.drsize)
symbols = data.read(header.syms)
str_size = struct.unpack('>I', data.read(4))[0]
strings = data.read()
if str_size != len(strings):
print 'Warning: wrong size of string table!'
str_map = {}
str_table = []
s = 0
while True:
e = strings.find('\0', s)
if e == -1:
str_map[s + 4] = strings[s:]
str_table.append(strings[s:])
break
str_map[s + 4] = strings[s:e]
str_table.append(strings[s:e])
s = e + 1
print header
print ''
if text_seg:
print 'Text:'
util.hexdump(text_seg)
print ''
if data_seg:
print 'Data:'
util.hexdump(data_seg)
print ''
print 'Symbols:'
for i in range(0, len(symbols), 12):
symbol = aout.SymbolInfo.decode(symbols[i:i + 12])
print ' ', symbol.as_string(str_map)
print ''
if text_reloc:
print 'Text relocations:'
for i in range(0, len(text_reloc), 8):
reloc = aout.RelocInfo.decode(text_reloc[i:i + 8])
print ' ', reloc.as_string(str_table)
print ''
if data_reloc:
print 'Data relocations:'
for i in range(0, len(data_reloc), 8):
reloc = aout.RelocInfo.decode(data_reloc[i:i + 8])
print ' ', reloc.as_string(str_table)
print ''
obj = aout.Aout()
obj.read(path)
obj.dump()
+89
View File
@@ -1,5 +1,7 @@
from collections import namedtuple
import os
import struct
from util import hexdump
class Header(namedtuple('Header', ('mid', 'magic', 'text', 'data', 'bss',
@@ -99,3 +101,90 @@ class SymbolInfo(namedtuple('SymbolInfo', ('strx', 'type', 'other', 'desc',
return '{3:08x} {5} {0:<5} {2:04x} {1:02x} {6:02x} {4}'.format(
self.type_str, self.other, self.desc, self.value, symbol,
visibility, self.type)
class Aout(object):
def __init__(self):
self._path = None
self._header = None
self._text = ''
self._data = ''
self._str_map = {}
self._str_table = []
self._symbols = []
self._text_relocs = []
self._data_relocs = []
def addString(self, offset, text):
self._str_map[offset] = text
self._str_table.append(text)
def read(self, path):
self._path = path
with open(path) as data:
print 'Reading {0} of size {1} bytes.'.format(
repr(path), os.stat(path)[6])
self._header = Header.decode(data.read(32))
self._text = data.read(self._header.text)
self._data = data.read(self._header.data)
text_reloc = data.read(self._header.trsize)
data_reloc = data.read(self._header.drsize)
symbols = data.read(self._header.syms)
str_size = struct.unpack('>I', data.read(4))[0]
strings = data.read()
if str_size != len(strings):
print 'Warning: wrong size of string table!'
s = 0
while True:
e = strings.find('\0', s)
if e == -1:
self.addString(s + 4, strings[s:])
break
else:
self.addString(s + 4, strings[s:e])
s = e + 1
for i in range(0, len(symbols), 12):
self._symbols.append(SymbolInfo.decode(symbols[i:i + 12]))
for i in range(0, len(text_reloc), 8):
self._text_relocs.append(RelocInfo.decode(text_reloc[i:i + 8]))
for i in range(0, len(data_reloc), 8):
self._data_relocs.append(RelocInfo.decode(data_reloc[i:i + 8]))
def dump(self):
print self._header
print ''
if self._text:
print 'Text:'
hexdump(self._text)
print ''
if self._data:
print 'Data:'
hexdump(self._data)
print ''
print 'Symbols:'
for symbol in self._symbols:
print ' ', symbol.as_string(self._str_map)
print ''
if self._text_relocs:
print 'Text relocations:'
for reloc in self._text_relocs:
print ' ', reloc.as_string(self._str_table)
print ''
if self._data_relocs:
print 'Data relocations:'
for reloc in self._text_relocs:
print ' ', reloc.as_string(self._str_table)
print ''