Ghidra is a software reverse engineering (SRE) framework
RuleDoubleArithShift
caheckman173190305630
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex 6971ebb4fd..b2ac97b7c2 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -4478,6 +4478,7 @@ void universal_action(Architecture *conf)actprop->addRule( new RuleAndCompare("analysis") );actprop->addRule( new RuleDoubleSub("analysis") );actprop->addRule( new RuleDoubleShift("analysis") );+ actprop->addRule( new RuleDoubleArithShift("analysis") );actprop->addRule( new RuleConcatShift("analysis") );actprop->addRule( new RuleLeftRight("analysis") );actprop->addRule( new RuleShiftCompare("analysis") );diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex 594d894668..dc4123c3fb 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@ -1819,6 +1819,40 @@ int4 RuleDoubleShift::applyOp(PcodeOp *op,Funcdata &data)return 1;}+/// \class RuleDoubleArithShift+/// \brief Simply two sequential INT_SRIGHT: `(x s>> #c) s>> #d => x s>> saturate(#c + #d)`+///+/// Optimized division optimization in particular can produce a sequence of signed right shifts.+/// The shift amounts add up to the point where the sign bit has saturated the entire result.+void RuleDoubleArithShift::getOpList(vector<uint4> &oplist) const++{+ oplist.push_back(CPUI_INT_SRIGHT);+}++int4 RuleDoubleArithShift::applyOp(PcodeOp *op,Funcdata &data)++{+ Varnode *constD = op->getIn(1);+ if (!constD->isConstant()) return 0;+ Varnode *shiftin = op->getIn(0);+ if (!shiftin->isWritten()) return 0;+ PcodeOp *shift2op = shiftin->getDef();+ if (shift2op->code() != CPUI_INT_SRIGHT) return 0;+ Varnode *constC = shift2op->getIn(1);+ if (!constC->isConstant()) return 0;+ Varnode *inVn = shift2op->getIn(0);+ if (inVn->isFree()) return 0;+ int4 max = op->getOut()->getSize() * 8 - 1; // This is maximum possible shift.+ int4 sa = (int4)constC->getOffset() + (int4)constD->getOffset();+ if (sa <= 0) return 0; // Something is wrong+ if (sa > max)+ sa = max; // Shift amount has saturated+ data.opSetInput(op, inVn, 0);+ data.opSetInput(op, data.newConstant(4, sa),1);+ return 1;+}+/// \class RuleConcatShift/// \brief Simplify INT_RIGHT canceling PIECE: `concat(V,W) >> c => zext(V)`///diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hhindex f75ace6b16..b00a46edd8 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh@@ -345,6 +345,16 @@ public:virtual void getOpList(vector<uint4> &oplist) const;virtual int4 applyOp(PcodeOp *op,Funcdata &data);};+class RuleDoubleArithShift : public Rule {+public:+ RuleDoubleArithShift(const string &g) : Rule(g, 0, "doublearithshift") {} ///< Constructor+ virtual Rule *clone(const ActionGroupList &grouplist) const {+ if (!grouplist.contains(getGroup())) return (Rule *)0;+ return new RuleDoubleArithShift(getGroup());+ }+ virtual void getOpList(vector<uint4> &oplist) const;+ virtual int4 applyOp(PcodeOp *op,Funcdata &data);+};class RuleConcatShift : public Rule {public:RuleConcatShift(const string &g) : Rule(g, 0, "concatshift") {} ///< Constructor