- Add pseudo instructions tLDRpci_pic and t2LDRpci_pic which does a pc-relative
[oota-llvm.git] / test / Scripts / macho-dump
index e844197a6ccda1ae5a51d3681ba4351eab487f65..5b9943ada2ca1ea8e22e50299e6e5e8d36c2b382 100755 (executable)
@@ -2,31 +2,58 @@
 
 import struct
 import sys
+import StringIO
 
 class Reader:
    def __init__(self, path):
       if path == '-':
-         self.file = sys.stdin
+         # 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.pos = 0
+
+      self.string_table = None
 
    def setLSB(self, isLSB):
       self.isLSB = bool(isLSB)
 
    def tell(self):
-      return self.pos
+      return self.file.tell()
+
+   def seek(self, pos):
+      self.file.seek(pos)
 
    def read(self, N):
       data = self.file.read(N)
-      self.pos += len(data)
       if len(data) != N:
          raise ValueError,"Out of data!"
       return data
 
+   def read8(self):
+      return ord(self.read(1))
+
+   def read16(self):
+      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 registerStringTable(self, strings):
+      if self.string_table is not None:
+         raise ValueError,"%s: warning: multiple string tables" % sys.argv[0]
+
+      self.string_table = strings
+
+   def getString(self, index):
+      if self.string_table is None:
+         raise ValueError,"%s: warning: no string table registered" % sys.argv[0]
+      
+      end = self.string_table.index('\x00', index)
+      return self.string_table[index:end]
 
 def dumpmacho(path, opts):
    f = Reader(path)
@@ -60,7 +87,7 @@ def dumpmacho(path, opts):
    print "])"
 
    if f.tell() - start != loadCommandsSize:
-      raise ValueError,"%s: warning: invalid load commands size: %r" % (sys.argv, loadCommandsSize)
+      raise ValueError,"%s: warning: invalid load commands size: %r" % (sys.argv[0], loadCommandsSize)
 
 def dumpLoadCommand(f, i, opts):
    start = f.tell()
@@ -77,13 +104,16 @@ def dumpLoadCommand(f, i, opts):
       dumpSymtabCommand(f, opts)
    elif cmd == 11:
       dumpDysymtabCommand(f, opts)
+   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)
       f.read(cmdSize - 8)
    print " ),"
 
    if f.tell() - start != cmdSize:
-      raise ValueError,"%s: warning: invalid load command size: %r" % (sys.argv, cmdSize)
+      raise ValueError,"%s: warning: invalid load command size: %r" % (sys.argv[0], cmdSize)
 
 def dumpSegmentLoadCommand32(f, opts):
    print "  ('segment_name', %r)" % f.read(16) 
@@ -103,10 +133,45 @@ def dumpSegmentLoadCommand32(f, opts):
    print "  ])"
 
 def dumpSymtabCommand(f, opts):
-   print "  ('symoff', %r)" % f.read32()
-   print "  ('nsyms', %r)" % f.read32()
-   print "  ('stroff', %r)" % f.read32()
-   print "  ('strsize', %r)" % f.read32()
+   symoff = f.read32()
+   print "  ('symoff', %r)" % symoff
+   nsyms = f.read32()
+   print "  ('nsyms', %r)" % nsyms
+   stroff = f.read32()
+   print "  ('stroff', %r)" % stroff
+   strsize = f.read32()
+   print "  ('strsize', %r)" % strsize
+
+   prev_pos = f.tell()
+
+   f.seek(stroff)
+   string_data = f.read(strsize)
+   print "  ('_string_data', %r)" % string_data
+
+   f.registerStringTable(string_data)
+
+   f.seek(symoff)
+   print "  ('_symbols', ["
+   for i in range(nsyms):
+      dumpNlist32(f, i, opts)
+   print "  ])"
+      
+   f.seek(prev_pos)
+
+def dumpNlist32(f, i, opts):
+   print "    # Symbol %r" % i
+   n_strx = f.read32()
+   print "   (('n_strx', %r)" % n_strx
+   n_type = f.read8()
+   print "    ('n_type', %#x)" % n_type
+   n_sect = f.read8()
+   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
+   print "    ('_string', %r)" % f.getString(n_strx)
+   print "   ),"
 
 def dumpDysymtabCommand(f, opts):   
    print "  ('ilocalsym', %r)" % f.read32()
@@ -121,32 +186,67 @@ def dumpDysymtabCommand(f, opts):
    print "  ('nmodtab', %r)" % f.read32()
    print "  ('extrefsymoff', %r)" % f.read32()
    print "  ('nextrefsyms', %r)" % f.read32()
-   print "  ('indirectsymoff', %r)" % f.read32()
-   print "  ('nindirectsyms', %r)" % f.read32()
+   indirectsymoff = f.read32()
+   print "  ('indirectsymoff', %r)" % indirectsymoff
+   nindirectsyms = f.read32()
+   print "  ('nindirectsyms', %r)" % nindirectsyms
    print "  ('extreloff', %r)" % f.read32()
    print "  ('nextrel', %r)" % f.read32()
    print "  ('locreloff', %r)" % f.read32()
    print "  ('nlocrel', %r)" % f.read32()
 
+   prev_pos = f.tell()
+
+   f.seek(indirectsymoff)
+   print "  ('_indirect_symbols', ["
+   for i in range(nindirectsyms):
+      print "    # Indirect Symbol %r" % i
+      print "    (('symbol_index', %#x),)," % f.read32()
+   print "  ])"
+      
+   f.seek(prev_pos)
+
 def dumpSection32(f, i, opts):
    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()
+   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()
    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', %r)" % 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: