Don't print a counter in hex.
[oota-llvm.git] / test / Scripts / elf-dump
index 1db4c49cc6b5ef037f3ec62d46a44e2066accaba..9794bfd9d8dc93b6d667249a400e19677a4c4660 100755 (executable)
@@ -34,27 +34,15 @@ class Reader:
     def read32(self):
         return struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
 
-    def read32S(self):
-        return struct.unpack('><'[self.isLSB] + 'i', self.read(4))[0]
-
     def read64(self):
         return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
 
-    def read64S(self):
-        return struct.unpack('><'[self.isLSB] + 'q', self.read(8))[0]
-
     def readWord(self):
         if self.is64Bit:
             return self.read64()
         else:
             return self.read32()
 
-    def readWordS(self):
-        if self.is64Bit:
-            return self.read64S()
-        else:
-            return self.read32S()
-
 class StringTable:
     def __init__(self, strings):
        self.string_table = strings
@@ -77,16 +65,16 @@ class Section:
         self.sh_entsize = f.readWord()
 
     def dump(self, shstrtab, f, strtab, dumpdata):
-        print "  (('sh_name', %d) # %r" % (self.sh_name, shstrtab[self.sh_name])
-        print "   ('sh_type', %d)" % self.sh_type
-        print "   ('sh_flags', %d)" % self.sh_flags
-        print "   ('sh_addr', %d)" % self.sh_addr
-        print "   ('sh_offset', %d)" % self.sh_offset
-        print "   ('sh_size', %d)" % self.sh_size
-        print "   ('sh_link', %d)" % self.sh_link
-        print "   ('sh_info', %d)" % self.sh_info
-        print "   ('sh_addralign', %d)" % self.sh_addralign
-        print "   ('sh_entsize', %d)" % self.sh_entsize
+        print "  (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name]
+        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)
         if self.sh_type == 2: # SHT_SYMTAB
             print "   ('_symbols', ["
             dumpSymtab(f, self, strtab)
@@ -97,8 +85,11 @@ class Section:
             print "   ])"
         elif dumpdata:
             f.seek(self.sh_offset)
-            data = f.read(self.sh_size)
-            print "   ('_section_data', '%s')" % common_dump.dataToHex(data)
+            if self.sh_type != 8: # != SHT_NOBITS
+                data = f.read(self.sh_size)
+                print "   ('_section_data', '%s')" % common_dump.dataToHex(data)
+            else:
+                print "   ('_section_data', '')" 
         print "  ),"
 
 def dumpSymtab(f, section, strtab):
@@ -106,20 +97,20 @@ def dumpSymtab(f, section, strtab):
 
     for index in range(entries):
         f.seek(section.sh_offset + index * section.sh_entsize)
-        print "    # Symbol %d" % index
+        print "    # Symbol %s" % common_dump.HexDump(index)
         name = f.read32()
-        print "    (('st_name', %d) # %r" % (name, strtab[name])
+        print "    (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name]
         if not f.is64Bit:
-            print "     ('st_value', %d)" % f.read32()
-            print "     ('st_size', %d)" % f.read32()
+            print "     ('st_value', %s)" % common_dump.HexDump(f.read32())
+            print "     ('st_size', %s)" % common_dump.HexDump(f.read32())
         st_info = f.read8()
-        print "     ('st_bind', %d)" % (st_info >> 4)
-        print "     ('st_type', %d)" % (st_info & 0xf)
-        print "     ('st_other', %d)" % f.read8()
-        print "     ('st_shndx', %d)" % f.read16()
+        print "     ('st_bind', %s)" % common_dump.HexDump((st_info >> 4))
+        print "     ('st_type', %s)" % common_dump.HexDump((st_info & 0xf))
+        print "     ('st_other', %s)" % common_dump.HexDump(f.read8())
+        print "     ('st_shndx', %s)" % common_dump.HexDump(f.read16())
         if f.is64Bit:
-            print "     ('st_value', %d)" % f.read64()
-            print "     ('st_size', %d)" % f.read64()
+            print "     ('st_value', %s)" % common_dump.HexDump(f.read64(), 64)
+            print "     ('st_size', %s)" % common_dump.HexDump(f.read64(), 64)
         print "    ),"
 
 def dumpRel(f, section, dumprela = False):
@@ -127,17 +118,22 @@ def dumpRel(f, section, dumprela = False):
 
     for index in range(entries):
         f.seek(section.sh_offset + index * section.sh_entsize)
-        print "    # Relocation %d" % index
-        print "    (('r_offset', %d)" % f.readWord()
+        print "    # Relocation %s" % common_dump.HexDump(index)
+        print "    (('r_offset', %s)" % common_dump.HexDump(f.readWord())
         r_info = f.readWord()
         if f.is64Bit:
-            print "     ('r_sym', %d)" % (r_info >> 32)
-            print "     ('r_type', %d)" % (r_info & 0xffffffff)
+            print "     ('r_sym', %s)" % common_dump.HexDump((r_info >> 32))
+            print "     ('r_type', %s)" % common_dump.HexDump((r_info & 0xffffffff))
         else:
-            print "     ('r_sym', %d)" % (r_info >> 8)
-            print "     ('r_type', %d)" % (r_info & 0xff)
+            print "     ('r_sym', %s)" % common_dump.HexDump((r_info >> 8))
+            print "     ('r_type', %s)" % common_dump.HexDump((r_info & 0xff))
         if dumprela:
-            print "     ('r_addend', %d)" % f.readWordS()
+            val = f.readWord()
+            if f.is64Bit:
+                numBits = 64
+            else:
+                numBits = 32
+            print "     ('r_addend', %s)" % common_dump.HexDump(val, numBits)
         print "    ),"
 
 def dumpELF(path, opts):
@@ -152,8 +148,8 @@ def dumpELF(path, opts):
     elif fileclass == 2: # ELFCLASS64
         f.is64Bit = True
     else:
-        raise ValueError, "Unknown file class %d" % fileclass
-    print "('e_indent[EI_CLASS]', %d)" % fileclass
+        raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
+    print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
 
     byteordering = f.read8()
     if byteordering == 1: # ELFDATA2LSB
@@ -161,32 +157,32 @@ def dumpELF(path, opts):
     elif byteordering == 2: # ELFDATA2MSB
         f.isLSB = False
     else:
-        raise ValueError, "Unknown byte ordering %d" % byteordering
-    print "('e_indent[EI_DATA]', %d)" % byteordering
+        raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
+    print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
 
-    print "('e_indent[EI_VERSION]', %d)" % f.read8()
-    print "('e_indent[EI_OSABI]', %d)" % f.read8()
-    print "('e_indent[EI_ABIVERSION]', %d)" % f.read8()
+    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())
 
     f.seek(16) # Seek to end of e_ident.
 
-    print "('e_type', %d)" % f.read16()
-    print "('e_machine', %d)" % f.read16()
-    print "('e_version', %d)" % f.read32()
-    print "('e_entry', %d)" % f.readWord()
-    print "('e_phoff', %d)" % f.readWord()
+    print "('e_type', %s)" % common_dump.HexDump(f.read16())
+    print "('e_machine', %s)" % common_dump.HexDump(f.read16())
+    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())
     e_shoff = f.readWord()
-    print "('e_shoff', %d)" % e_shoff
-    print "('e_flags', %d)" % f.read32()
-    print "('e_ehsize', %d)" % f.read16()
-    print "('e_phentsize', %d)" % f.read16()
-    print "('e_phnum', %d)" % f.read16()
+    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())
     e_shentsize = f.read16()
-    print "('e_shentsize', %d)" % e_shentsize
+    print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
     e_shnum = f.read16()
-    print "('e_shnum', %d)" % e_shnum
+    print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
     e_shstrndx = f.read16()
-    print "('e_shstrndx', %d)" % e_shstrndx
+    print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
 
     # Read all section headers
     sections = []
@@ -209,7 +205,7 @@ def dumpELF(path, opts):
 
     print "('_sections', ["
     for index in range(e_shnum):
-        print "  # Section %d" % index
+        print "  # Section %s" % index
         sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
     print "])"