From 71fc8bfef774108d9e4af4122eb7830081d66c46 Mon Sep 17 00:00:00 2001 From: Artur K Date: Sat, 17 Mar 2012 23:42:46 +0100 Subject: [PATCH] Added JumpTable class, and simplified LongJCond23/22 by using Assignment class as an parameter. --- include/Procedure.h | 9 +++++++++ src/Procedure.cpp | 25 +++++++++++++++++++++++++ src/proplong.cpp | 21 +++++++++++---------- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/include/Procedure.h b/include/Procedure.h index 9c86e4d..8a62af8 100644 --- a/include/Procedure.h +++ b/include/Procedure.h @@ -77,6 +77,15 @@ struct Assignment COND_EXPR *lhs; COND_EXPR *rhs; }; +struct JumpTable +{ + uint32_t start; + uint32_t finish; + bool valid() {return start { diff --git a/src/Procedure.cpp b/src/Procedure.cpp index 1a9d7af..db39e60 100644 --- a/src/Procedure.cpp +++ b/src/Procedure.cpp @@ -5,3 +5,28 @@ //{ // return &m_type; //} + +/* Does some heuristic pruning. Looks for ptrs. into the table + * and for addresses that don't appear to point to valid code. +*/ +void JumpTable::pruneEntries(uint16_t cs) +{ + PROG *prg(Project::get()->binary()); + for (uint32_t i = start; i < finish; i += 2) + { + uint32_t target = cs + LH(&prg->Image[i]); + if (target < finish && target >= start) + finish = target; + else if (target >= (uint32_t)prg->cbImage) + finish = i; + } + ICODE _Icode; // used as scan input + for (uint32_t i = start; i < finish; i += 2) + { + uint32_t target = cs + LH(&prg->Image[i]); + /* Be wary of 00 00 as code - it's probably data */ + if (! (prg->Image[target] || prg->Image[target+1]) || scan(target, _Icode)) + finish = i; + } + +} diff --git a/src/proplong.cpp b/src/proplong.cpp index 34bf367..a40a03c 100644 --- a/src/proplong.cpp +++ b/src/proplong.cpp @@ -83,7 +83,7 @@ static boolT isLong22 (iICODE pIcode, iICODE pEnd, iICODE &off) * @return number of ICODEs to skip */ -static int longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode, int arc, iICODE atOffset) +static int longJCond23 (Assignment &asgn, iICODE pIcode, int arc, iICODE atOffset) { BB * pbb, * obb1, * obb2, * tbb; int skipped_insn=0; @@ -142,8 +142,8 @@ static int longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode, int arc, iICODE atOffset1(atOffset),next1(++iICODE(pIcode)); advance(atOffset1,1); /* Create new HLI_JCOND and condition */ - lhs = COND_EXPR::boolOp (lhs, rhs, condOpJCond[atOffset1->ll()->getOpcode()-iJB]); - next1->setJCond(lhs); + asgn.lhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, condOpJCond[atOffset1->ll()->getOpcode()-iJB]); + next1->setJCond(asgn.lhs); next1->copyDU(*pIcode, eUSE, eUSE); next1->du.use |= atOffset->du.use; @@ -167,7 +167,7 @@ static int longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode, int arc, * the new edges for the remaining nodes. * @return number of ICODE's to skip */ -static int longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode,iICODE pEnd) +static int longJCond22 (Assignment &asgn, iICODE pIcode,iICODE pEnd) { BB * pbb, * obb1, * tbb; @@ -177,8 +177,8 @@ static int longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode,iICODE pEn iICODE icodes[] = { pIcode++,pIcode++,pIcode++,pIcode++ }; /* Form conditional expression */ - lhs = COND_EXPR::boolOp (lhs, rhs, condOpJCond[icodes[3]->ll()->getOpcode() - iJB]); - icodes[1]->setJCond(lhs); + asgn.lhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, condOpJCond[icodes[3]->ll()->getOpcode() - iJB]); + icodes[1]->setJCond(asgn.lhs); icodes[1]->copyDU (*icodes[0], eUSE, eUSE); icodes[1]->du.use |= icodes[2]->du.use; @@ -283,7 +283,7 @@ void Function::propLongStk (int i, const ID &pLocId) { if ( checkLongEq (pLocId.id.longStkId, pIcode, i, this, asgn, *l23->ll()) ) { - advance(pIcode,longJCond23 (asgn.rhs, asgn.lhs, pIcode, arc, l23)); + advance(pIcode,longJCond23 (asgn, pIcode, arc, l23)); } } @@ -293,7 +293,7 @@ void Function::propLongStk (int i, const ID &pLocId) { if ( checkLongEq (pLocId.id.longStkId, pIcode, i, this,asgn, *l23->ll()) ) { - advance(pIcode,longJCond22 (asgn.rhs, asgn.lhs, pIcode,pEnd)); + advance(pIcode,longJCond22 (asgn, pIcode,pEnd)); } } } @@ -460,7 +460,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be if (checkLongRegEq (pLocId.id.longId, pIcode, loc_ident_idx, this, asgn, *long_loc->ll())) { // reduce the advance by 1 here (loop increases) ? - advance(pIcode,longJCond23 (asgn.rhs, asgn.lhs, pIcode, arc, long_loc)); + advance(pIcode,longJCond23 (asgn, pIcode, arc, long_loc)); } } @@ -470,7 +470,8 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be { if (checkLongRegEq (pLocId.id.longId, pIcode, loc_ident_idx, this, asgn, *long_loc->ll()) ) { - advance(pIcode,longJCond22 (asgn.rhs, asgn.lhs, pIcode,pEnd) - 1); + // TODO: verify that removing -1 does not change anything ! + advance(pIcode,longJCond22 (asgn, pIcode,pEnd)); } }