Ghidra is a software reverse engineering (SRE) framework
GP-8 Additional support for BitrangeSymbol
caheckman35f20dd20db0
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.ccindex b308e1b710..b8735099af 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.cc@@ -287,6 +287,7 @@ void SymbolTable::purge(void)case SleighSymbol::token_symbol:case SleighSymbol::epsilon_symbol:case SleighSymbol::section_symbol:+ case SleighSymbol::bitrange_symbol:break;case SleighSymbol::macro_symbol:{ // Delete macro's local symbolsdiff --git a/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g b/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.gindex 8935b88f24..4ff7d50664 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g+++ b/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g@@ -1168,7 +1168,13 @@ assignment returns [VectorSTL<OpTpl> value]| ^(t=OP_ASSIGN ^(OP_IDENTIFIER id=.) e=expr) {SleighSymbol sym = pcode.findSymbol($id.getText());if (sym == null) {- $value = pcode.newOutput(find(id), false, e, $id.getText());+ $value = pcode.newOutput(find(id), false, e, $id.getText());+ } else if (sym instanceof BitrangeSymbol) {+ BitrangeSymbol bitSym = (BitrangeSymbol)sym;+ VarnodeSymbol parent = bitSym.getParentSymbol();+ $value = pcode.assignBitRange(find(t), parent.getVarnode(),+ bitSym.getBitOffset(),+ bitSym.numBits(),e);} else if(sym.getType() != symbol_type.start_symbol&& sym.getType() != symbol_type.end_symbol&& sym.getType() != symbol_type.next2_symbol@@ -1450,7 +1456,7 @@ expr returns [ExprTree value]| s=sizedstar { $value = pcode.createLoad(s.first.location, s.first, s.second); }| a=expr_apply { $value = (ExprTree) $a.value; }- | v=varnode { $value = new ExprTree(v.location, v); }+ | v=varnode_or_bitsym["expression"] { $value = $v.value; }| b=bitrange { $value = $b.value; }| i=integer { $value = new ExprTree(i.location, new VarnodeTpl(i.location, new ConstTpl(pcode.getConstantSpace()),new ConstTpl(ConstTpl.const_type.real, $i.value.longValue()),@@ -1463,6 +1469,30 @@ expr returns [ExprTree value]};+varnode_or_bitsym[String purpose] returns [ExprTree value]+ : ^(t=OP_IDENTIFIER s=.) {+ SleighSymbol sym = pcode.findSymbol($s.getText());+ if (sym == null) {+ unknownSymbolError($s.getText(), find($s), "varnode or bitrange symbol", purpose);+ } else if (sym instanceof BitrangeSymbol) {+ BitrangeSymbol bitSym = (BitrangeSymbol)sym;+ $value = pcode.createBitRange(find(t), bitSym.getParentSymbol(),+ bitSym.getBitOffset(),+ bitSym.numBits());+ } else if (sym instanceof SpecificSymbol) {+ VarnodeTpl vTemp = ((SpecificSymbol)sym).getVarnode();+ $value = new ExprTree(vTemp.location, vTemp);+ } else {+ undeclaredSymbolError(sym, find($s), purpose);+ }+ }+ | v=varnode_adorned { $value = new ExprTree($v.value.location,$v.value); }+ | t=OP_WILDCARD {+ wildcardError($t, purpose);+ $value = null;+ }+ ;+expr_apply returns [Object value]@after {$code_block::stmtLocation = find(x);@@ -1517,9 +1547,8 @@ expr_operands returns [VectorSTL<ExprTree> value]: (e=expr { value.push_back(e); })*;-varnode returns [VarnodeTpl value]- : ss=specific_symbol["varnode reference"] { $value = ss.getVarnode(); }- | ^(t=OP_TRUNCATION_SIZE n=integer m=integer) {+varnode_adorned returns [VarnodeTpl value]+ : ^(t=OP_TRUNCATION_SIZE n=integer m=integer) {if ($m.value.longValue() > 8) {reportError(find(t), "Constant varnode size must not exceed 8 (" +$n.value.longValue() + ":" + $m.value.longValue() + ")");@@ -1532,6 +1561,11 @@ varnode returns [VarnodeTpl value]| ^(OP_ADDRESS_OF v=varnode) { $value = pcode.addressOf(v, 0); };+varnode returns [VarnodeTpl value]+ : ss=specific_symbol["varnode reference"] { $value = ss.getVarnode(); }+ | v=varnode_adorned { $value = $v.value; }+ ;+qstring returns [String value]: ^(OP_QSTRING s=.) { $value = $s.getText(); };diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slghsymbol/SymbolTable.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slghsymbol/SymbolTable.javaindex 76f476a8d2..33fbb65e91 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slghsymbol/SymbolTable.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slghsymbol/SymbolTable.java@@ -30,8 +30,8 @@ import ghidra.sleigh.grammar.Location;public class SymbolTable {- private VectorSTL<SleighSymbol> symbollist = new VectorSTL<SleighSymbol>();- private VectorSTL<SymbolScope> table = new VectorSTL<SymbolScope>();+ private VectorSTL<SleighSymbol> symbollist = new VectorSTL<>();+ private VectorSTL<SymbolScope> table = new VectorSTL<>();private SymbolScope curscope;public SymbolTable() {@@ -51,7 +51,7 @@ public class SymbolTable {}public VectorSTL<SleighSymbol> getUnsoughtSymbols() {- VectorSTL<SleighSymbol> result = new VectorSTL<SleighSymbol>();+ VectorSTL<SleighSymbol> result = new VectorSTL<>();IteratorSTL<SleighSymbol> siter;for (siter = symbollist.begin(); !siter.isEnd(); siter.increment()) {SleighSymbol sleighSymbol = siter.get();@@ -333,10 +333,12 @@ public class SymbolTable {case token_symbol:case epsilon_symbol:case section_symbol:+ case bitrange_symbol:break;case macro_symbol: { // Delete macro's local symbolsMacroSymbol macro = (MacroSymbol) sym;- for (int macroIndex = 0; macroIndex < macro.getNumOperands(); ++macroIndex) {+ for (int macroIndex = 0; macroIndex < macro+ .getNumOperands(); ++macroIndex) {SleighSymbol opersym = macro.getOperand(macroIndex);table.get(opersym.scopeid).removeSymbol(opersym);symbollist.set(opersym.id, null);@@ -349,11 +351,13 @@ public class SymbolTable {if (subsym.getPattern() != null) {continue;}- for (int subtableIndex = 0; subtableIndex < subsym.getNumConstructors(); ++subtableIndex) { // Go thru+ for (int subtableIndex = 0; subtableIndex < subsym+ .getNumConstructors(); ++subtableIndex) { // Go thru// each// constructorConstructor con = subsym.getConstructor(subtableIndex);- for (int operandIndex = 0; operandIndex < con.getNumOperands(); ++operandIndex) { // Go thru each operand+ for (int operandIndex = 0; operandIndex < con+ .getNumOperands(); ++operandIndex) { // Go thru each operandOperandSymbol oper = con.getOperand(operandIndex);table.get(oper.scopeid).removeSymbol(oper);symbollist.set(oper.id, null);@@ -382,8 +386,8 @@ public class SymbolTable {// Renumber all the scopes and symbols// so that there are no gapsprivate void renumber() {- VectorSTL<SymbolScope> newtable = new VectorSTL<SymbolScope>();- VectorSTL<SleighSymbol> newsymbol = new VectorSTL<SleighSymbol>();+ VectorSTL<SymbolScope> newtable = new VectorSTL<>();+ VectorSTL<SleighSymbol> newsymbol = new VectorSTL<>();// First renumber the scopesSymbolScope scope = null;