Use the builtin startswith() string method instead of implementing it manually. --- tools/checkstack.py | 14 +++++++------- tools/layoutrom.py | 11 +++++------ 2 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/tools/checkstack.py b/tools/checkstack.py index 2fbc160..428c296 100755 --- a/tools/checkstack.py +++ b/tools/checkstack.py @@ -140,10 +140,10 @@ def calc():
im = re_usestack.match(insn) if im is not None: - if insn[:5] == 'pushl' or insn[:6] == 'pushfl': + if insn.startswith('pushl') or insn.startswith('pushfl'): stackusage += 4 continue - elif insn[:5] == 'pushw' or insn[:6] == 'pushfw': + elif insn.startswith('pushw') or insn.startswith('pushfw'): stackusage += 2 continue stackusage += int(im.group('num'), 16) @@ -158,13 +158,13 @@ def calc(): insnaddr = m.group('insnaddr') calladdr = m.group('calladdr') if calladdr is None: - if insn[:6] == 'lcallw': + if insn.startswith('lcallw'): noteCall(cur, subfuncs, insnaddr, -1, stackusage + 4) noteYield(cur, stackusage + 4) - elif insn[:3] == 'int': + elif insn.startswith('int'): noteCall(cur, subfuncs, insnaddr, -1, stackusage + 6) noteYield(cur, stackusage + 6) - elif insn[:3] == 'sti': + elif insn.startswith('sti'): noteYield(cur, stackusage) else: # misc instruction @@ -176,10 +176,10 @@ def calc(): if '+' in ref: # Inter-function jump. pass - elif insn[:1] == 'j': + elif insn.startswith('j'): # Tail call noteCall(cur, subfuncs, insnaddr, calladdr, 0) - elif insn[:5] == 'calll': + elif insn.startswith('calll'): noteCall(cur, subfuncs, insnaddr, calladdr, stackusage + 4) else: print "unknown call", ref diff --git a/tools/layoutrom.py b/tools/layoutrom.py index 5f8c368..7603f38 100755 --- a/tools/layoutrom.py +++ b/tools/layoutrom.py @@ -59,10 +59,9 @@ def getSectionsStart(sections, endaddr, minalign=1):
# Return the subset of sections with a given name prefix def getSectionsPrefix(sections, prefix): - lp = len(prefix) out = [] for size, align, name in sections: - if name[:lp] == prefix: + if name.startswith(prefix): out.append((size, align, name)) return out
@@ -79,7 +78,7 @@ def fitSections(sections, fillsections): fixedsections = [] for sectioninfo in sections: size, align, name = sectioninfo - if name[:11] == '.fixedaddr.': + if name.startswith('.fixedaddr.'): addr = int(name[11:], 16) fixedsections.append((addr, sectioninfo)) if align != 1: @@ -295,7 +294,7 @@ PHDRS # Find and keep the section associated with a symbol (if available). def keepsymbol(symbol, infos, pos, callerpos=None): addr, section = infos[pos][1].get(symbol, (None, None)) - if section is None or '*' in section or section[:9] == '.discard.': + if section is None or '*' in section or section.startswith('.discard.'): return -1 if callerpos is not None and symbol not in infos[callerpos][4]: # This symbol reference is a cross section reference (an xref). @@ -340,7 +339,7 @@ def gc(info16, info32seg, info32flat): (info32flat[0], info32flat[1], info32flat[2], [], {})) # Start by keeping sections that are globally visible. for size, align, section in info16[0]: - if section[:11] == '.fixedaddr.' or '.export.' in section: + if section.startswith('.fixedaddr.') or '.export.' in section: keepsection(section, infos) keepsymbol('post32', infos, 0, 2) # Return sections found. @@ -372,7 +371,7 @@ def parseObjDump(file): if line == 'SYMBOL TABLE:': state = 'symbol' continue - if line[:24] == 'RELOCATION RECORDS FOR [': + if line.startswith('RELOCATION RECORDS FOR ['): state = 'reloc' relocsection = line[24:-2] continue