Skip to content

Ghidra is a software reverse engineering (SRE) framework

Recurse case ordering into nested switches

caheckmana4e360a3e5e5
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/block.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/block.cc
index c1cdbb2725..9b6d8d87e5 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/block.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/block.cc
@@ -1254,12 +1254,13 @@ FlowBlock *BlockGraph::nextFlowAfter(const FlowBlock *bl) const
return nextbl;
}
-void BlockGraph::orderSwitchCases(void) const
+void BlockGraph::finalizePrinting(const Funcdata &data) const
{
+ // Recurse into all the substructures
vector<FlowBlock *>::const_iterator iter;
for(iter=list.begin();iter!=list.end();++iter)
- (*iter)->orderSwitchCases();
+ (*iter)->finalizePrinting(data);
}
void BlockGraph::saveXmlBody(ostream &s) const
@@ -3082,9 +3083,12 @@ void BlockSwitch::grabCaseBasic(FlowBlock *switchbl,const vector<FlowBlock *> &c
}
}
-void BlockSwitch::orderSwitchCases(void) const
+void BlockSwitch::finalizePrinting(const Funcdata &data) const
{
+ BlockGraph::finalizePrinting(data); // Make sure to still recurse
+ // We need to order the cases based on the label
+ // First populate the label and depth fields of the CaseOrder objects
for(int4 i=0;i<caseblocks.size();++i) { // Construct the depth parameter, to sort fall-thru cases
CaseOrder &curcase( caseblocks[i] );
int4 j = curcase.chain;
@@ -3113,7 +3117,7 @@ void BlockSwitch::orderSwitchCases(void) const
else
curcase.label = 0; // Should never happen
}
- // Order case statements based on case labels
+ // Do actual sort of the cases based on label
stable_sort(caseblocks.begin(),caseblocks.end(),CaseOrder::compare);
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/block.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/block.hh
index 053f67f38c..30f867a35e 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/block.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/block.hh
@@ -170,7 +170,7 @@ public:
virtual void flipInPlaceExecute(void);
virtual bool isComplex(void) const { return true; } ///< Is \b this too complex to be a condition (BlockCondition)
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
- virtual void orderSwitchCases(void) const {} ///< Order \e case components of any contained BlockSwitch
+ virtual void finalizePrinting(const Funcdata &data) const {} ///< Make any final configurations necessary to print the block
virtual void saveXmlHeader(ostream &s) const; ///< Save basic information as XML attributes
virtual void restoreXmlHeader(const Element *el); ///< Restore basic information for XML attributes
virtual void saveXmlBody(ostream &s) const {} ///< Save detail about components to an XML stream
@@ -296,7 +296,7 @@ public:
virtual void printRaw(ostream &s) const;
virtual void emit(PrintLanguage *lng) const { lng->emitBlockGraph(this); }
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
- virtual void orderSwitchCases(void) const;
+ virtual void finalizePrinting(const Funcdata &data) const;
virtual void saveXmlBody(ostream &s) const;
virtual void restoreXmlBody(List::const_iterator &iter,List::const_iterator enditer,BlockMap &resolver);
void restoreXml(const Element *el,const AddrSpaceManager *m); ///< Restore \b this BlockGraph from an XML stream
@@ -674,7 +674,7 @@ public:
virtual void printHeader(ostream &s) const;
virtual void emit(PrintLanguage *lng) const { lng->emitBlockSwitch(this); }
virtual FlowBlock *nextFlowAfter(const FlowBlock *bl) const;
- virtual void orderSwitchCases(void) const;
+ virtual void finalizePrinting(const Funcdata &data) const;
};
/// \brief Helper class for resolving cross-references while deserializing BlockGraph objects
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/blockaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/blockaction.cc
index c157b39508..86489e4e94 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/blockaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/blockaction.cc
@@ -2170,7 +2170,7 @@ int4 ActionFinalStructure::apply(Funcdata &data)
BlockGraph &graph(data.getStructure());
graph.orderBlocks();
- graph.orderSwitchCases();
+ graph.finalizePrinting(data);
graph.scopeBreak(-1,-1); // Put in \e break statements
graph.markUnstructured(); // Put in \e gotos
graph.markLabelBumpUp(false); // Fix up labeling