X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=test%2FScripts%2Fmacho-dump;h=8c9fa59c601060815b6a56c14fd40e31b789aae5;hb=ab3d00e5350fd4c097e2a5b077da7584692029a7;hp=d37b545b2ab12633cc329045bf861b2961ffed99;hpb=8906ff1b9dfde28f1ff00706643ca10843b26e01;p=oota-llvm.git diff --git a/test/Scripts/macho-dump b/test/Scripts/macho-dump index d37b545b2ab..8c9fa59c601 100755 --- a/test/Scripts/macho-dump +++ b/test/Scripts/macho-dump @@ -4,6 +4,8 @@ import struct import sys import StringIO +import common_dump + class Reader: def __init__(self, path): if path == '-': @@ -12,12 +14,10 @@ class Reader: else: self.file = open(path,'rb') self.isLSB = None + self.is64Bit = None self.string_table = None - def setLSB(self, isLSB): - self.isLSB = bool(isLSB) - def tell(self): return self.file.tell() @@ -37,7 +37,16 @@ class Reader: return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0] def read32(self): - return struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0] + # Force to 32-bit, if possible; otherwise these might be long ints on a + # big-endian platform. FIXME: Why??? + Value = struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0] + return int(Value) + + def read64(self): + Value = struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0] + if Value == int(Value): + Value = int(Value) + return Value def registerStringTable(self, strings): if self.string_table is not None: @@ -57,9 +66,13 @@ def dumpmacho(path, opts): magic = f.read(4) if magic == '\xFE\xED\xFA\xCE': - f.setLSB(False) + f.isLSB, f.is64Bit = False, False elif magic == '\xCE\xFA\xED\xFE': - f.setLSB(True) + f.isLSB, f.is64Bit = True, False + elif magic == '\xFE\xED\xFA\xCF': + f.isLSB, f.is64Bit = False, True + elif magic == '\xCF\xFA\xED\xFE': + f.isLSB, f.is64Bit = True, True else: raise ValueError,"Not a Mach-O object file: %r (bad magic)" % path @@ -76,6 +89,9 @@ def dumpmacho(path, opts): print "('flag', %r)" % f.read32() + if f.is64Bit: + print "('reserved', %r)" % f.read32() + start = f.tell() print "('load_commands', [" @@ -84,7 +100,8 @@ def dumpmacho(path, opts): print "])" if f.tell() - start != loadCommandsSize: - raise ValueError,"%s: warning: invalid load commands size: %r" % (sys.argv[0], loadCommandsSize) + raise ValueError,"%s: warning: invalid load commands size: %r" % ( + sys.argv[0], loadCommandsSize) def dumpLoadCommand(f, i, opts): start = f.tell() @@ -96,25 +113,38 @@ def dumpLoadCommand(f, i, opts): print " ('size', %r)" % cmdSize if cmd == 1: - dumpSegmentLoadCommand32(f, opts) + dumpSegmentLoadCommand(f, opts, False) elif cmd == 2: dumpSymtabCommand(f, opts) elif cmd == 11: dumpDysymtabCommand(f, opts) + elif cmd == 25: + dumpSegmentLoadCommand(f, opts, True) + elif cmd == 27: + import uuid + print " ('uuid', %s)" % uuid.UUID(bytes=f.read(16)) else: - print >>sys.stderr,"%s: warning: unknown load command: %r" % (sys.argv[0], cmd) + print >>sys.stderr,"%s: warning: unknown load command: %r" % ( + sys.argv[0], cmd) f.read(cmdSize - 8) print " )," if f.tell() - start != cmdSize: - raise ValueError,"%s: warning: invalid load command size: %r" % (sys.argv[0], cmdSize) + raise ValueError,"%s: warning: invalid load command size: %r" % ( + sys.argv[0], cmdSize) -def dumpSegmentLoadCommand32(f, opts): +def dumpSegmentLoadCommand(f, opts, is64Bit): print " ('segment_name', %r)" % f.read(16) - print " ('vm_addr', %r)" % f.read32() - print " ('vm_size', %r)" % f.read32() - print " ('file_offset', %r)" % f.read32() - print " ('file_size', %r)" % f.read32() + if is64Bit: + print " ('vm_addr', %r)" % f.read64() + print " ('vm_size', %r)" % f.read64() + print " ('file_offset', %r)" % f.read64() + print " ('file_size', %r)" % f.read64() + else: + print " ('vm_addr', %r)" % f.read32() + print " ('vm_size', %r)" % f.read32() + print " ('file_offset', %r)" % f.read32() + print " ('file_size', %r)" % f.read32() print " ('maxprot', %r)" % f.read32() print " ('initprot', %r)" % f.read32() numSections = f.read32() @@ -123,7 +153,7 @@ def dumpSegmentLoadCommand32(f, opts): print " ('sections', [" for i in range(numSections): - dumpSection32(f, i, opts) + dumpSection(f, i, opts, is64Bit) print " ])" def dumpSymtabCommand(f, opts): @@ -159,11 +189,15 @@ def dumpNlist32(f, i, opts): n_type = f.read8() print " ('n_type', %#x)" % n_type n_sect = f.read8() - print " ('n_type', %r)" % n_sect + print " ('n_sect', %r)" % n_sect n_desc = f.read16() print " ('n_desc', %r)" % n_desc - n_value = f.read32() - print " ('n_value', %r)" % n_value + if f.is64Bit: + n_value = f.read64() + print " ('n_value', %r)" % n_value + else: + n_value = f.read32() + print " ('n_value', %r)" % n_value print " ('_string', %r)" % f.getString(n_strx) print " )," @@ -195,30 +229,59 @@ def dumpDysymtabCommand(f, opts): print " ('_indirect_symbols', [" for i in range(nindirectsyms): print " # Indirect Symbol %r" % i - print " (('symbol_index', %r),)," % f.read32() + print " (('symbol_index', %#x),)," % f.read32() print " ])" f.seek(prev_pos) -def dumpSection32(f, i, opts): +def dumpSection(f, i, opts, is64Bit): print " # Section %r" % i print " (('section_name', %r)" % f.read(16) print " ('segment_name', %r)" % f.read(16) - print " ('address', %r)" % f.read32() - print " ('size', %r)" % f.read32() - print " ('offset', %r)" % f.read32() - print " ('alignment', %r)" % f.read32() - print " ('reloc_offset', %r)" % f.read32() - print " ('num_reloc', %r)" % f.read32() + if is64Bit: + print " ('address', %r)" % f.read64() + size = f.read64() + print " ('size', %r)" % size + else: + print " ('address', %r)" % f.read32() + size = f.read32() + print " ('size', %r)" % size + offset = f.read32() + print " ('offset', %r)" % offset + print " ('alignment', %r)" % f.read32() + reloc_offset = f.read32() + print " ('reloc_offset', %r)" % reloc_offset + num_reloc = f.read32() + print " ('num_reloc', %r)" % num_reloc print " ('flags', %#x)" % f.read32() print " ('reserved1', %r)" % f.read32() print " ('reserved2', %r)" % f.read32() + if is64Bit: + print " ('reserved3', %r)" % f.read32() print " )," + + prev_pos = f.tell() + + f.seek(reloc_offset) + print " ('_relocations', [" + for i in range(num_reloc): + print " # Relocation %r" % i + print " (('word-0', %#x)," % f.read32() + print " ('word-1', %#x))," % f.read32() + print " ])" + + if opts.dumpSectionData: + f.seek(offset) + print " ('_section_data', '%s')" % common_dump.dataToHex(f.read(size)) + + f.seek(prev_pos) def 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: