Skip to content

Commit

Permalink
Improve getRxLines method (not tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoroninaD committed Nov 5, 2017
1 parent 7707cb8 commit 309456b
Showing 1 changed file with 114 additions and 3 deletions.
117 changes: 114 additions & 3 deletions parse.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import re, arm_translate, utils, switcher
conditions = ['eq','ne','cs','hs','cc','lo','mi','pl','vs','vc','hi','ls','ge','lt','gt','le','al']


def getRxLines(lines, line, table, sp_subbed, sub_ind):
def getRxLines1(lines, line, table, sp_subbed, sub_ind):
#line = add_sp_to_reg[0].group()
c = switcher.getNumber(line)
if c is None:
Expand Down Expand Up @@ -77,6 +78,115 @@ def getRxLines(lines, line, table, sp_subbed, sub_ind):
return to_write


def getRxLines(lines, line, table, sp_subbed, sub_ind):
llll = []
def getJumpIndex(line):
try:
jmpAddr = tmp[commonJMPPattern.search(line).end():].strip().split(';')[0]
except:
aaa=1
jmpAddr = re.sub('[a-z]+_', '0x', jmpAddr).lower()
try:
int(jmpAddr, 16)
except:
return None
try_ind = [l for l in lines if l.addr == jmpAddr]
if len(try_ind)==0:
return None
return lines.index(try_ind[0])


def handleLine(index,line):
llll.append((index,line))
c = switcher.getNumber(line)
if c is None:
return -1
regsPattern = re.compile('r11|r10|r12|r[0-9]', re.IGNORECASE)
reg = switcher.searchPattern(regsPattern, line).group()
useRegPattern = re.compile\
('.*(ldr|str)(b|h|sb|sh|sw|d)?(.w)?.*\[{0}(,\s?#\-?(0x)?[0-9a-f]+.*\])?'.format(reg),
re.IGNORECASE)
clearRegPattern = re.compile \
('.*(mov(eq|ne)?|(v)?ldr(b|h|sb|sh|sw)?|add)(.w)?\s{0},\s?.*'.format(reg),
re.IGNORECASE)
simpleJMPPattern = re.compile('\sbl?s?(\.w)?\s', re.IGNORECASE)
regJumpPattern = re.compile('\sbl?x\s', re.IGNORECASE)
conditionJumpPattern = re.compile('\s((cbz)|(b(l)?({0})))\s'.format('|'.join(conditions)),re.IGNORECASE)
commonJMPPattern = re.compile('\s((cbn?z)|(bl?s?({0})?))(\.w)?\s'.format('|'.join(conditions)),re.IGNORECASE)
#определяем строку, с которой будем искать строки вида [reg, #d]
start_ind = list(lines).index(line)


ways, old_ways = [start_ind], [start_ind]
while(len(ways)>0):
i = ways[0]
end_reg = switcher.searchInLines(clearRegPattern, lines[i + 1:])
# определяем строку, ДО которой будем искать строки вида [reg, #d] (mov затирает sp)
end_ind = list(lines).index(end_reg[0].group()) if len(end_reg) > 0 else len(lines)
for l in lines[i:end_ind]:
tmp = l.line
if useRegPattern.search(tmp):
handleLine(i,l)
if simpleJMPPattern.search(tmp):
index = getJumpIndex(tmp)
if index is not None and index not in old_ways:
old_ways.append(index)
ways.append(index)
break
if conditionJumpPattern.search(tmp):
try:
index = getJumpIndex(tmp)
except:
index = getJumpIndex(tmp)
if index is None:
break
if index not in old_ways:
old_ways.append(index)
ways.append(index)
ways.remove(i)

# предполагаем, что изменение sp происходит только в начале и конце функции todo
# если sp еще не отнят, тогда не нужно учитывать a



to_write = []
# Ищем строки вида [reg, #d]
# use_reg = list(filter(None,
# [re.search('.*(ldr|str)(b|h|sb|sh|sw|d)?(.w)?.*\[{0}(, #\-?[0-9]+\])?'.format(reg), line) for
# line in lines[start_ind:end_ind]]))

# ... [rx]

#use_reg = switcher.searchInLines(useRegPattern, lines[start_ind:end_ind])


#todo если будет str rx, [sp, #] и уже добавлен в to_write по sp, будет перезаписано?
for item in llll:
start_ind, l = item[0],item[1]
if start_ind < sub_ind:
sp_subbed = 0
pattern = re.compile('v?(ldr|str)(b|h|sb|sh|d)?(.w)?',re.IGNORECASE)
instr = switcher.searchPattern(pattern, l).group()
d = switcher.getNumber(l)
if d is None:
return -1
n = c + d - sp_subbed
if n >= 0: # todo а что если будет sub rx, sp?
pattern = re.compile('(\s+r10|r11|r12|sp|lr|pc|r[0-9]|((d|s)(([1-2][0-9])|3[0-1]|[0-9]))),', re.IGNORECASE)
rx = switcher.searchPattern(pattern, l).group().strip().replace(',', '')
offset = changeOffset(n, d, table)
new_instr_code = arm_translate.makeLdrOrStr(instr, l.bytes, rx, reg, offset, l.thumb, l.line)
to_write.append((l.addr, len(l.bytes) // 2, utils.toLittleEndian(new_instr_code)))

# str rx, [...]
strRegPattern = re.compile('.*str(b|h|sb|sh|sw|d)?(.w)?\s{0}.*'.format(reg), re.IGNORECASE)
str_reg = switcher.searchInLines(strRegPattern, lines[start_ind:end_ind])
if len(str_reg) > 0:
return -1
return to_write


def getAllSpLinesForLow(lines, table):
to_write = []

Expand Down Expand Up @@ -149,10 +259,11 @@ def getAllSpLinesForLow(lines, table):
#code, is_thumb = utils.getCodeFromLine(l.group())
offset = changeOffset(b-a, b, table)
new_instr_code = arm_translate.\
makeLdrOrStr(instr,l.bytes , rx, 'sp', offset, l.thumb, l.line)
makeLdrOrStr(instr,l.bytes , rx.lower(), 'sp', offset, l.thumb, l.line)
# to_write ... [sp, #b + new_regs_count*4]
to_write.append((l.addr,
len(l.bytes) // 2, utils.toLittleEndian(new_instr_code)))
len(l.bytes) // 2, utils.toLittleEndian(new_instr_code)))


#ищем строки вида add rx, sp, (#c) - должна быть одна ? todo
#add_sp_to_reg = list(filter(None, [re.search('.*(add(.w)?|mov)\s*(r[0-9]|r10|r11|r12), sp(, #[1-9]+)?.*', line) for line in lines]))
Expand Down

0 comments on commit 309456b

Please sign in to comment.