Skip to content

Introduce operand offset (C++ and Java) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Ghidra/Features/Decompiler/src/decompile/cpp/pcodeparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern int pcodeerror(const char *str );
UserOpSymbol *useropsym;
LabelSymbol *labelsym;
StartSymbol *startsym;
OffsetSymbol *offsetsym;
EndSymbol *endsym;
Next2Symbol *next2sym;
OperandSymbol *operandsym;
Expand Down Expand Up @@ -78,6 +79,7 @@ extern int pcodeerror(const char *str );
%token <varsym> VARSYM
%token <operandsym> OPERANDSYM
%token <startsym> STARTSYM
%token <offsetsym> OFFSETSYM
%token <endsym> ENDSYM
%token <next2sym> NEXT2SYM
%token <labelsym> LABELSYM
Expand Down Expand Up @@ -225,6 +227,7 @@ label: '<' LABELSYM '>' { $$ = $2; }
specificsymbol: VARSYM { $$ = $1; }
| OPERANDSYM { $$ = $1; }
| STARTSYM { $$ = $1; }
| OFFSETSYM { $$ = $1; }
| ENDSYM { $$ = $1; }
| NEXT2SYM { $$ = $1; }
;
Expand Down Expand Up @@ -752,6 +755,9 @@ int4 PcodeSnippet::lex(void)
case SleighSymbol::start_symbol:
yylval.startsym = (StartSymbol *)sym;
return STARTSYM;
case SleighSymbol::offset_symbol:
yylval.offsetsym = (OffsetSymbol *)sym;
return OFFSETSYM;
case SleighSymbol::end_symbol:
yylval.endsym = (EndSymbol *)sym;
return ENDSYM;
Expand Down
8 changes: 8 additions & 0 deletions Ghidra/Features/Decompiler/src/decompile/cpp/semantics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ uintb ConstTpl::fix(const ParserWalker &walker) const
switch(type) {
case j_start:
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
case j_offset:
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
case j_next:
return walker.getNaddr().getOffset(); // Fill in next address placeholder with real address
case j_next2:
Expand Down Expand Up @@ -350,6 +352,9 @@ void ConstTpl::saveXml(ostream &s) const
case j_start:
s << "start\"/>";
break;
case j_offset:
s << "operand_offset\"/>";
break;
case j_next:
s << "next\"/>";
break;
Expand Down Expand Up @@ -408,6 +413,9 @@ void ConstTpl::restoreXml(const Element *el,const AddrSpaceManager *manage)
else if (typestring=="start") {
type = j_start;
}
else if (typestring=="operand_offset") {
type = j_offset;
}
else if (typestring=="next") {
type = j_next;
}
Expand Down
2 changes: 1 addition & 1 deletion Ghidra/Features/Decompiler/src/decompile/cpp/semantics.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ConstTpl {
public:
enum const_type { real=0, handle=1, j_start=2, j_next=3, j_next2=4, j_curspace=5,
j_curspace_size=6, spaceid=7, j_relative=8,
j_flowref=9, j_flowref_size=10, j_flowdest=11, j_flowdest_size=12 };
j_flowref=9, j_flowref_size=10, j_flowdest=11, j_flowdest_size=12, j_offset=13 };
enum v_field { v_space=0, v_offset=1, v_size=2, v_offset_plus=3 };
private:
const_type type;
Expand Down
2 changes: 2 additions & 0 deletions Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1817,6 +1817,8 @@ void SleighCompile::predefinedSymbols(void)
symtab.addSymbol(spacesym);
StartSymbol *startsym = new StartSymbol("inst_start",getConstantSpace());
symtab.addSymbol(startsym);
OffsetSymbol *offsetsym = new OffsetSymbol("operand_offset",getConstantSpace());
symtab.addSymbol(offsetsym);
EndSymbol *endsym = new EndSymbol("inst_next",getConstantSpace());
symtab.addSymbol(endsym);
Next2Symbol *next2sym = new Next2Symbol("inst_next2",getConstantSpace());
Expand Down
4 changes: 4 additions & 0 deletions Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extern int sleigherror(const char *str );
LabelSymbol *labelsym;
SubtableSymbol *subtablesym;
StartSymbol *startsym;
OffsetSymbol *offsetsym;
EndSymbol *endsym;
Next2Symbol *next2sym;
OperandSymbol *operandsym;
Expand Down Expand Up @@ -123,6 +124,7 @@ extern int sleigherror(const char *str );
%token <varlistsym> VARLISTSYM
%token <operandsym> OPERANDSYM
%token <startsym> STARTSYM
%token <offsetsym> OFFSETSYM
%token <endsym> ENDSYM
%token <next2sym> NEXT2SYM
%token <macrosym> MACROSYM
Expand Down Expand Up @@ -504,6 +506,7 @@ specificsymbol: VARSYM { $$ = $1; }
| SPECSYM { $$ = $1; }
| OPERANDSYM { $$ = $1; }
| STARTSYM { $$ = $1; }
| OFFSETSYM { $$ = $1; }
| ENDSYM { $$ = $1; }
| NEXT2SYM { $$ = $1; }
;
Expand Down Expand Up @@ -579,6 +582,7 @@ anysymbol: SPACESYM { $$ = $1; }
| VARLISTSYM { $$ = $1; }
| OPERANDSYM { $$ = $1; }
| STARTSYM { $$ = $1; }
| OFFSETSYM { $$ = $1; }
| ENDSYM { $$ = $1; }
| NEXT2SYM { $$ = $1; }
| BITSYM { $$ = $1; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ PatternExpression *PatternExpression::restoreExpression(const Element *el,Transl
res = new OperandValue();
else if (nm == "start_exp")
res = new StartInstructionValue();
else if (nm == "offset_exp")
res = new OperandOffsetValue();
else if (nm == "end_exp")
res = new EndInstructionValue();
else if (nm == "plus_exp")
Expand Down
14 changes: 14 additions & 0 deletions Ghidra/Features/Decompiler/src/decompile/cpp/slghpatexpress.hh
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ public:
virtual void saveXml(ostream &s) const { s << "<start_exp/>"; }
virtual void restoreXml(const Element *el,Translate *trans) {}
};

class OperandOffsetValue : public PatternValue {
public:
OperandOffsetValue(void) {}
virtual intb getValue(ParserWalker &walker) const {
return (intb)walker.getOffset(-1);
}
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
virtual intb minValue(void) const { return (intb)0; }
virtual intb maxValue(void) const { return (intb)0; }
virtual void saveXml(ostream &s) const { s << "<offset_exp/>"; }
virtual void restoreXml(const Element *el,Translate *trans) {}
};

class EndInstructionValue : public PatternValue {
public:
Expand Down
3 changes: 3 additions & 0 deletions Ghidra/Features/Decompiler/src/decompile/cpp/slghscan.l
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ int4 find_symbol(void) {
case SleighSymbol::start_symbol:
sleighlval.startsym = (StartSymbol *)sym;
return STARTSYM;
case SleighSymbol::offset_symbol:
sleighlval.offsetsym = (OffsetSymbol *)sym;
return OFFSETSYM;
case SleighSymbol::end_symbol:
sleighlval.endsym = (EndSymbol *)sym;
return ENDSYM;
Expand Down
66 changes: 66 additions & 0 deletions Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ void SymbolTable::restoreSymbolHeader(const Element *el)
sym = new OperandSymbol();
else if (el->getName() == "start_sym_head")
sym = new StartSymbol();
else if (el->getName() == "offset_sym_head")
sym = new OffsetSymbol();
else if (el->getName() == "end_sym_head")
sym = new EndSymbol();
else if (el->getName() == "next2_sym_head")
Expand Down Expand Up @@ -1196,6 +1198,70 @@ void StartSymbol::restoreXml(const Element *el,SleighBase *trans)
patexp->layClaim();
}

OffsetSymbol::OffsetSymbol(const string &nm,AddrSpace *cspc) : SpecificSymbol(nm)

{
const_space = cspc;
patexp = new OperandOffsetValue();
patexp->layClaim();
}

OffsetSymbol::~OffsetSymbol(void)

{
if (patexp != (PatternExpression *)0)
PatternExpression::release(patexp);
}

VarnodeTpl *OffsetSymbol::getVarnode(void) const

{ // Returns current operand offset as a constant
ConstTpl spc(const_space);
ConstTpl off(ConstTpl::j_offset);
ConstTpl sz_zero;
return new VarnodeTpl(spc,off,sz_zero);
}

void OffsetSymbol::getFixedHandle(FixedHandle &hand,ParserWalker &walker) const

{
hand.space = walker.getCurSpace();
hand.offset_space = (AddrSpace *)0;
hand.offset_offset = walker.getAddr().getOffset(); // Get starting address of instruction
hand.size = hand.space->getAddrSize();
}

void OffsetSymbol::print(ostream &s,ParserWalker &walker) const

{
intb val = (intb) walker.getAddr().getOffset();
s << "0x" << std::hex << val << std::dec;
}

void OffsetSymbol::saveXml(ostream &s) const

{
s << "<offset_sym";
SleighSymbol::saveXmlHeader(s);
s << "/>\n";
}

void OffsetSymbol::saveXmlHeader(ostream &s) const

{
s << "<offset_sym_head";
SleighSymbol::saveXmlHeader(s);
s << "/>\n";
}

void OffsetSymbol::restoreXml(const Element *el,SleighBase *trans)

{
const_space = trans->getConstantSpace();
patexp = new OperandOffsetValue();
patexp->layClaim();
}

EndSymbol::EndSymbol(const string &nm,AddrSpace *cspc) : SpecificSymbol(nm)

{
Expand Down
19 changes: 18 additions & 1 deletion Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SleighSymbol {
public:
enum symbol_type { space_symbol, token_symbol, userop_symbol, value_symbol, valuemap_symbol,
name_symbol, varnode_symbol, varnodelist_symbol, operand_symbol,
start_symbol, end_symbol, next2_symbol, subtable_symbol, macro_symbol, section_symbol,
start_symbol, offset_symbol, end_symbol, next2_symbol, subtable_symbol, macro_symbol, section_symbol,
bitrange_symbol, context_symbol, epsilon_symbol, label_symbol,
dummy_symbol };
private:
Expand Down Expand Up @@ -376,6 +376,23 @@ public:
virtual void restoreXml(const Element *el,SleighBase *trans);
};

class OffsetSymbol : public SpecificSymbol {
AddrSpace *const_space;
PatternExpression *patexp;
public:
OffsetSymbol(void) { patexp = (PatternExpression *)0; } // For use with restoreXml
OffsetSymbol(const string &nm,AddrSpace *cspc);
virtual ~OffsetSymbol(void);
virtual VarnodeTpl *getVarnode(void) const;
virtual PatternExpression *getPatternExpression(void) const { return patexp; }
virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
virtual void print(ostream &s,ParserWalker &walker) const;
virtual symbol_type getType(void) const { return offset_symbol; }
virtual void saveXml(ostream &s) const;
virtual void saveXmlHeader(ostream &s) const;
virtual void restoreXml(const Element *el,SleighBase *trans);
};

class EndSymbol : public SpecificSymbol {
AddrSpace *const_space;
PatternExpression *patexp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ specific_symbol[String purpose] returns [SpecificSymbol symbol]
if (sym == null) {
unknownSymbolError($s.getText(), find($s), "start, end, next2, operand, epsilon, or varnode", purpose);
} else if(sym.getType() != symbol_type.start_symbol
&& sym.getType() != symbol_type.offset_symbol
&& sym.getType() != symbol_type.end_symbol
&& sym.getType() != symbol_type.next2_symbol
&& sym.getType() != symbol_type.operand_symbol
Expand Down Expand Up @@ -839,6 +840,7 @@ pattern_symbol[String purpose] returns [PatternExpression expr]
}
$expr = os.getPatternExpression();
} else if(sym.getType() == symbol_type.start_symbol
|| sym.getType() == symbol_type.offset_symbol
|| sym.getType() == symbol_type.end_symbol
|| sym.getType() == symbol_type.next2_symbol
|| sym.getType() == symbol_type.epsilon_symbol
Expand Down Expand Up @@ -872,6 +874,7 @@ pattern_symbol2[String purpose] returns [PatternExpression expr]
if (sym == null) {
unknownSymbolError($s.getText(), find($s), "start, end, next2, operand, epsilon, or varnode", purpose);
} else if(sym.getType() == symbol_type.start_symbol
|| sym.getType() == symbol_type.offset_symbol
|| sym.getType() == symbol_type.end_symbol
|| sym.getType() == symbol_type.next2_symbol
|| sym.getType() == symbol_type.operand_symbol
Expand Down Expand Up @@ -943,6 +946,7 @@ cstatement[VectorSTL<ContextChange> r]
|| sym.getType() == symbol_type.name_symbol
|| sym.getType() == symbol_type.varnodelist_symbol
|| sym.getType() == symbol_type.start_symbol
|| sym.getType() == symbol_type.offset_symbol
|| sym.getType() == symbol_type.end_symbol
|| sym.getType() == symbol_type.next2_symbol
|| sym.getType() == symbol_type.operand_symbol
Expand Down Expand Up @@ -1170,6 +1174,7 @@ assignment returns [VectorSTL<OpTpl> value]
if (sym == null) {
$value = pcode.newOutput(find(id), false, e, $id.getText());
} else if(sym.getType() != symbol_type.start_symbol
&& sym.getType() != symbol_type.offset_symbol
&& sym.getType() != symbol_type.end_symbol
&& sym.getType() != symbol_type.next2_symbol
&& sym.getType() != symbol_type.operand_symbol
Expand Down Expand Up @@ -1486,6 +1491,7 @@ expr_apply returns [Object value]
pcode.reportError(find($t), "macro invocation not allowed as expression");
}
} else if(sym.getType() == symbol_type.start_symbol
|| sym.getType() == symbol_type.offset_symbol
|| sym.getType() == symbol_type.end_symbol
|| sym.getType() == symbol_type.next2_symbol
|| sym.getType() == symbol_type.operand_symbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ else if (sym instanceof VarnodeSymbol) {
else if (sym instanceof StartSymbol) {
// Ignore. We handle inst_start in semantic processing
}
else if (sym instanceof OffsetSymbol) {
// Ignore. We handle inst_start in semantic processing
}
else if (sym instanceof EndSymbol) {
// Ignore. We handle inst_next in semantic processing
}
Expand Down
Loading