2010-09-09 15:00:41 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import StringIO
|
|
|
|
|
2010-09-11 15:25:58 +00:00
|
|
|
import common_dump
|
|
|
|
|
2010-09-09 15:00:41 +00:00
|
|
|
class Reader:
|
|
|
|
def __init__(self, path):
|
|
|
|
if path == "-":
|
|
|
|
# Snarf all the data so we can seek.
|
|
|
|
self.file = StringIO.StringIO(sys.stdin.read())
|
|
|
|
else:
|
|
|
|
self.file = open(path, "rb")
|
|
|
|
self.isLSB = None
|
|
|
|
self.is64Bit = None
|
2012-06-27 22:48:25 +00:00
|
|
|
self.isN64 = False
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
def seek(self, pos):
|
|
|
|
self.file.seek(pos)
|
|
|
|
|
|
|
|
def read(self, N):
|
|
|
|
data = self.file.read(N)
|
|
|
|
if len(data) != N:
|
|
|
|
raise ValueError, "Out of data!"
|
|
|
|
return data
|
|
|
|
|
|
|
|
def read8(self):
|
2011-08-04 17:00:11 +00:00
|
|
|
return (ord(self.read(1)), 8)
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
def read16(self):
|
2011-08-04 17:00:11 +00:00
|
|
|
return (struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0], 16)
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
def read32(self):
|
2011-08-04 17:00:11 +00:00
|
|
|
return (struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0], 32)
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
def read64(self):
|
2011-08-04 17:00:11 +00:00
|
|
|
return (struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0], 64)
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
def readWord(self):
|
|
|
|
if self.is64Bit:
|
|
|
|
return self.read64()
|
|
|
|
else:
|
|
|
|
return self.read32()
|
|
|
|
|
|
|
|
class StringTable:
|
|
|
|
def __init__(self, strings):
|
|
|
|
self.string_table = strings
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
end = self.string_table.index('\x00', index)
|
|
|
|
return self.string_table[index:end]
|
|
|
|
|
|
|
|
class Section:
|
|
|
|
def __init__(self, f):
|
|
|
|
self.sh_name = f.read32()
|
|
|
|
self.sh_type = f.read32()
|
|
|
|
self.sh_flags = f.readWord()
|
|
|
|
self.sh_addr = f.readWord()
|
|
|
|
self.sh_offset = f.readWord()
|
|
|
|
self.sh_size = f.readWord()
|
|
|
|
self.sh_link = f.read32()
|
|
|
|
self.sh_info = f.read32()
|
|
|
|
self.sh_addralign = f.readWord()
|
|
|
|
self.sh_entsize = f.readWord()
|
|
|
|
|
|
|
|
def dump(self, shstrtab, f, strtab, dumpdata):
|
2011-08-04 17:00:11 +00:00
|
|
|
print " (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name[0]]
|
2010-10-19 17:39:10 +00:00
|
|
|
print " ('sh_type', %s)" % common_dump.HexDump(self.sh_type)
|
|
|
|
print " ('sh_flags', %s)" % common_dump.HexDump(self.sh_flags)
|
|
|
|
print " ('sh_addr', %s)" % common_dump.HexDump(self.sh_addr)
|
|
|
|
print " ('sh_offset', %s)" % common_dump.HexDump(self.sh_offset)
|
|
|
|
print " ('sh_size', %s)" % common_dump.HexDump(self.sh_size)
|
|
|
|
print " ('sh_link', %s)" % common_dump.HexDump(self.sh_link)
|
|
|
|
print " ('sh_info', %s)" % common_dump.HexDump(self.sh_info)
|
|
|
|
print " ('sh_addralign', %s)" % common_dump.HexDump(self.sh_addralign)
|
|
|
|
print " ('sh_entsize', %s)" % common_dump.HexDump(self.sh_entsize)
|
2011-08-04 17:00:11 +00:00
|
|
|
if self.sh_type[0] == 2: # SHT_SYMTAB
|
2010-09-09 15:00:41 +00:00
|
|
|
print " ('_symbols', ["
|
|
|
|
dumpSymtab(f, self, strtab)
|
|
|
|
print " ])"
|
2011-08-04 17:00:11 +00:00
|
|
|
elif self.sh_type[0] == 4 or self.sh_type[0] == 9: # SHT_RELA / SHT_REL
|
2010-09-09 15:00:41 +00:00
|
|
|
print " ('_relocations', ["
|
2011-08-04 17:00:11 +00:00
|
|
|
dumpRel(f, self, self.sh_type[0] == 4)
|
2010-09-09 15:00:41 +00:00
|
|
|
print " ])"
|
|
|
|
elif dumpdata:
|
2011-08-04 17:00:11 +00:00
|
|
|
f.seek(self.sh_offset[0])
|
2010-12-16 00:15:10 +00:00
|
|
|
if self.sh_type != 8: # != SHT_NOBITS
|
2011-08-04 17:00:11 +00:00
|
|
|
data = f.read(self.sh_size[0])
|
2010-12-16 00:15:10 +00:00
|
|
|
print " ('_section_data', '%s')" % common_dump.dataToHex(data)
|
|
|
|
else:
|
|
|
|
print " ('_section_data', '')"
|
2010-09-09 15:00:41 +00:00
|
|
|
print " ),"
|
|
|
|
|
|
|
|
def dumpSymtab(f, section, strtab):
|
2011-08-04 17:00:11 +00:00
|
|
|
entries = section.sh_size[0] // section.sh_entsize[0]
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
for index in range(entries):
|
2011-08-04 17:00:11 +00:00
|
|
|
f.seek(section.sh_offset[0] + index * section.sh_entsize[0])
|
2011-08-04 14:27:46 +00:00
|
|
|
print " # Symbol %s" % index
|
2010-09-09 15:00:41 +00:00
|
|
|
name = f.read32()
|
2011-08-04 17:00:11 +00:00
|
|
|
print " (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name[0]]
|
2010-09-09 15:00:41 +00:00
|
|
|
if not f.is64Bit:
|
2010-10-19 17:39:10 +00:00
|
|
|
print " ('st_value', %s)" % common_dump.HexDump(f.read32())
|
|
|
|
print " ('st_size', %s)" % common_dump.HexDump(f.read32())
|
2011-08-04 17:00:11 +00:00
|
|
|
st_info = f.read8()[0]
|
2011-08-04 15:10:35 +00:00
|
|
|
st_bind = (st_info >> 4, 4)
|
|
|
|
st_type = (st_info & 0xf, 4)
|
2011-08-04 17:00:11 +00:00
|
|
|
print " ('st_bind', %s)" % common_dump.HexDump(st_bind)
|
|
|
|
print " ('st_type', %s)" % common_dump.HexDump(st_type)
|
|
|
|
print " ('st_other', %s)" % common_dump.HexDump(f.read8())
|
|
|
|
print " ('st_shndx', %s)" % common_dump.HexDump(f.read16())
|
2010-09-09 15:00:41 +00:00
|
|
|
if f.is64Bit:
|
2011-08-04 17:00:11 +00:00
|
|
|
print " ('st_value', %s)" % common_dump.HexDump(f.read64())
|
|
|
|
print " ('st_size', %s)" % common_dump.HexDump(f.read64())
|
2010-09-09 15:00:41 +00:00
|
|
|
print " ),"
|
|
|
|
|
|
|
|
def dumpRel(f, section, dumprela = False):
|
2011-08-04 17:00:11 +00:00
|
|
|
entries = section.sh_size[0] // section.sh_entsize[0]
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
for index in range(entries):
|
2011-08-04 17:00:11 +00:00
|
|
|
f.seek(section.sh_offset[0] + index * section.sh_entsize[0])
|
2011-08-04 14:01:03 +00:00
|
|
|
print " # Relocation %s" % index
|
2010-10-19 17:39:10 +00:00
|
|
|
print " (('r_offset', %s)" % common_dump.HexDump(f.readWord())
|
2012-06-27 22:48:25 +00:00
|
|
|
|
|
|
|
if f.isN64:
|
|
|
|
r_sym = f.read32()
|
|
|
|
r_ssym = f.read8()
|
|
|
|
r_type3 = f.read8()
|
|
|
|
r_type2 = f.read8()
|
|
|
|
r_type = f.read8()
|
|
|
|
print " ('r_sym', %s)" % common_dump.HexDump(r_sym)
|
|
|
|
print " ('r_ssym', %s)" % common_dump.HexDump(r_ssym)
|
|
|
|
print " ('r_type3', %s)" % common_dump.HexDump(r_type3)
|
|
|
|
print " ('r_type2', %s)" % common_dump.HexDump(r_type2)
|
|
|
|
print " ('r_type', %s)" % common_dump.HexDump(r_type)
|
2010-09-09 15:00:41 +00:00
|
|
|
else:
|
2012-06-27 22:48:25 +00:00
|
|
|
r_info = f.readWord()[0]
|
|
|
|
if f.is64Bit:
|
|
|
|
r_sym = (r_info >> 32, 32)
|
|
|
|
r_type = (r_info & 0xffffffff, 32)
|
|
|
|
else:
|
|
|
|
r_sym = (r_info >> 8, 24)
|
|
|
|
r_type = (r_info & 0xff, 8)
|
|
|
|
print " ('r_sym', %s)" % common_dump.HexDump(r_sym)
|
|
|
|
print " ('r_type', %s)" % common_dump.HexDump(r_type)
|
2010-09-09 15:00:41 +00:00
|
|
|
if dumprela:
|
2011-08-04 17:00:11 +00:00
|
|
|
print " ('r_addend', %s)" % common_dump.HexDump(f.readWord())
|
2010-09-09 15:00:41 +00:00
|
|
|
print " ),"
|
|
|
|
|
|
|
|
def dumpELF(path, opts):
|
|
|
|
f = Reader(path)
|
|
|
|
|
|
|
|
magic = f.read(4)
|
|
|
|
assert magic == '\x7FELF'
|
|
|
|
|
|
|
|
fileclass = f.read8()
|
2011-08-04 17:00:11 +00:00
|
|
|
if fileclass[0] == 1: # ELFCLASS32
|
2010-09-09 15:00:41 +00:00
|
|
|
f.is64Bit = False
|
2011-08-04 17:00:11 +00:00
|
|
|
elif fileclass[0] == 2: # ELFCLASS64
|
2010-09-09 15:00:41 +00:00
|
|
|
f.is64Bit = True
|
|
|
|
else:
|
2010-10-19 17:39:10 +00:00
|
|
|
raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
|
|
|
|
print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
byteordering = f.read8()
|
2011-08-04 17:00:11 +00:00
|
|
|
if byteordering[0] == 1: # ELFDATA2LSB
|
2010-09-09 15:00:41 +00:00
|
|
|
f.isLSB = True
|
2011-08-04 17:00:11 +00:00
|
|
|
elif byteordering[0] == 2: # ELFDATA2MSB
|
2010-09-09 15:00:41 +00:00
|
|
|
f.isLSB = False
|
|
|
|
else:
|
2010-10-19 17:39:10 +00:00
|
|
|
raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
|
|
|
|
print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
|
2010-09-09 15:00:41 +00:00
|
|
|
|
2010-10-19 17:39:10 +00:00
|
|
|
print "('e_indent[EI_VERSION]', %s)" % common_dump.HexDump(f.read8())
|
|
|
|
print "('e_indent[EI_OSABI]', %s)" % common_dump.HexDump(f.read8())
|
|
|
|
print "('e_indent[EI_ABIVERSION]', %s)" % common_dump.HexDump(f.read8())
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
f.seek(16) # Seek to end of e_ident.
|
|
|
|
|
2010-10-19 17:39:10 +00:00
|
|
|
print "('e_type', %s)" % common_dump.HexDump(f.read16())
|
2012-06-27 22:48:25 +00:00
|
|
|
|
|
|
|
# Does any other architecture use N64?
|
|
|
|
e_machine = f.read16()
|
|
|
|
if e_machine[0] == 0x0008 and f.is64Bit: # EM_MIPS && 64 bit
|
|
|
|
f.isN64 = True
|
|
|
|
|
|
|
|
print "('e_machine', %s)" % common_dump.HexDump(e_machine)
|
2010-10-19 17:39:10 +00:00
|
|
|
print "('e_version', %s)" % common_dump.HexDump(f.read32())
|
|
|
|
print "('e_entry', %s)" % common_dump.HexDump(f.readWord())
|
|
|
|
print "('e_phoff', %s)" % common_dump.HexDump(f.readWord())
|
2010-09-09 15:00:41 +00:00
|
|
|
e_shoff = f.readWord()
|
2010-10-19 17:39:10 +00:00
|
|
|
print "('e_shoff', %s)" % common_dump.HexDump(e_shoff)
|
|
|
|
print "('e_flags', %s)" % common_dump.HexDump(f.read32())
|
|
|
|
print "('e_ehsize', %s)" % common_dump.HexDump(f.read16())
|
|
|
|
print "('e_phentsize', %s)" % common_dump.HexDump(f.read16())
|
|
|
|
print "('e_phnum', %s)" % common_dump.HexDump(f.read16())
|
2010-09-09 15:00:41 +00:00
|
|
|
e_shentsize = f.read16()
|
2010-10-19 17:39:10 +00:00
|
|
|
print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
|
2010-09-09 15:00:41 +00:00
|
|
|
e_shnum = f.read16()
|
2010-10-19 17:39:10 +00:00
|
|
|
print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
|
2010-09-09 15:00:41 +00:00
|
|
|
e_shstrndx = f.read16()
|
2010-10-19 17:39:10 +00:00
|
|
|
print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
# Read all section headers
|
|
|
|
sections = []
|
2011-08-04 17:00:11 +00:00
|
|
|
for index in range(e_shnum[0]):
|
|
|
|
f.seek(e_shoff[0] + index * e_shentsize[0])
|
2010-09-09 15:00:41 +00:00
|
|
|
s = Section(f)
|
|
|
|
sections.append(s)
|
|
|
|
|
|
|
|
# Read .shstrtab so we can resolve section names
|
2011-08-04 17:00:11 +00:00
|
|
|
f.seek(sections[e_shstrndx[0]].sh_offset[0])
|
|
|
|
shstrtab = StringTable(f.read(sections[e_shstrndx[0]].sh_size[0]))
|
2010-09-09 15:00:41 +00:00
|
|
|
|
|
|
|
# Get the symbol string table
|
|
|
|
strtab = None
|
|
|
|
for section in sections:
|
2011-08-04 17:00:11 +00:00
|
|
|
if shstrtab[section.sh_name[0]] == ".strtab":
|
|
|
|
f.seek(section.sh_offset[0])
|
|
|
|
strtab = StringTable(f.read(section.sh_size[0]))
|
2010-09-09 15:00:41 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
print "('_sections', ["
|
2011-08-04 17:00:11 +00:00
|
|
|
for index in range(e_shnum[0]):
|
2011-08-04 13:39:15 +00:00
|
|
|
print " # Section %s" % index
|
2010-09-09 15:00:41 +00:00
|
|
|
sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
|
|
|
|
print "])"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from optparse import OptionParser, OptionGroup
|
|
|
|
parser = OptionParser("usage: %prog [options] {files}")
|
|
|
|
parser.add_option("", "--dump-section-data", dest="dumpSectionData",
|
|
|
|
help="Dump the contents of sections",
|
|
|
|
action="store_true", default=False)
|
|
|
|
(opts, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if not args:
|
|
|
|
args.append('-')
|
|
|
|
|
|
|
|
for arg in args:
|
|
|
|
dumpELF(arg, opts)
|