Fixing r116753 r116756 r116777

The failures in r116753 r116756 were caused by a python issue -
Python likes to append 'L' suffix to stringified numbers if the number
is larger than a machine int. Unfortunately, this causes a divergence of
behavior between 32 and 64 bit python versions.

I re-crafted elf-dump/common_dump to take care of these issues by:

1. always printing 0x (makes for easy sed/regex)
2. always print fixed length (exactly 2 + numBits/4 digits long)
   by mod ((2^numBits) - 1)
3. left-padded with '0'

There is a residual common routine that is also used by
macho-dump (dataToHex) , so I left the 'section_data' test values alone.




git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@116823 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jason W Kim
2010-10-19 17:39:10 +00:00
parent 081c34b725
commit f7d5278fb3
33 changed files with 617 additions and 582 deletions

View File

@ -11,3 +11,36 @@ def dataToHex(d):
hex_byte += ' '
bytes.append(hex_byte)
return ''.join(bytes).strip()
def dataToHexUnified(d):
""" Convert the raw data in 'd' to an hex string with a space every 4 bytes.
Each 4byte number is prefixed with 0x for easy sed/rx
Fixme: convert all MC tests to use this routine instead of the above
"""
bytes = []
for i,c in enumerate(d):
byte = ord(c)
hex_byte = hex(byte)[2:]
if byte <= 0xf:
hex_byte = '0' + hex_byte
if i % 4 == 0:
hex_byte = '0x' + hex_byte
if i % 4 == 3:
hex_byte += ' '
bytes.append(hex_byte)
return ''.join(bytes).strip()
def HexDump(val, numBits=32):
"""
1. do not print 'L'
2. Handle negatives and large numbers by mod (2^numBits)
3. print fixed length, prepend with zeros.
Length is exactly 2+(numBits/4)
4. Do print 0x Why?
so that they can be easily distinguished using sed/rx
"""
val = val & (( 1 << numBits) - 1)
newFmt = "0x%0" + "%d" % (numBits / 4) + "x"
return newFmt % val

View File

@ -6,6 +6,8 @@ import StringIO
import common_dump
FormatOutput=hex
class Reader:
def __init__(self, path):
if path == "-":
@ -77,16 +79,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)
@ -106,20 +108,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())
print " ('st_size', %s)" % common_dump.HexDump(f.read64())
print " ),"
def dumpRel(f, section, dumprela = False):
@ -127,17 +129,17 @@ 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()
print " ('r_addend', %s)" % common_dump.HexDump(f.readWordS())
print " ),"
def dumpELF(path, opts):
@ -152,8 +154,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 +163,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 +211,7 @@ def dumpELF(path, opts):
print "('_sections', ["
for index in range(e_shnum):
print " # Section %d" % index
print " # Section %s" % common_dump.HexDump(index)
sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
print "])"