Skip to content

Commit f9a8788

Browse files
committed
Introduce operand_offset
Signed-off-by: Klaus Kämpf <[email protected]>
1 parent 9c724c1 commit f9a8788

File tree

10 files changed

+125
-3
lines changed

10 files changed

+125
-3
lines changed

Ghidra/Features/Decompiler/src/decompile/cpp/pcodeparse.y

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
UserOpSymbol *useropsym;
3838
LabelSymbol *labelsym;
3939
StartSymbol *startsym;
40+
OffsetSymbol *offsetsym;
4041
EndSymbol *endsym;
4142
OperandSymbol *operandsym;
4243
VarnodeSymbol *varsym;
@@ -75,6 +76,7 @@
7576
%token <varsym> VARSYM
7677
%token <operandsym> OPERANDSYM
7778
%token <startsym> STARTSYM
79+
%token <offsetsym> OFFSETSYM
7880
%token <endsym> ENDSYM
7981
%token <labelsym> LABELSYM
8082

@@ -220,6 +222,7 @@ label: '<' LABELSYM '>' { $$ = $2; }
220222
specificsymbol: VARSYM { $$ = $1; }
221223
| OPERANDSYM { $$ = $1; }
222224
| STARTSYM { $$ = $1; }
225+
| OFFSETSYM { $$ = $1; }
223226
| ENDSYM { $$ = $1; }
224227
;
225228
paramlist: /* EMPTY */ { $$ = new vector<ExprTree *>; }
@@ -635,7 +638,7 @@ void PcodeLexer::initialize(istream *t)
635638
}
636639
}
637640

638-
uint4 PcodeSnippet::allocateTemp(void)
641+
uintb PcodeSnippet::allocateTemp(void)
639642

640643
{ // Allocate a variable in the unique space and return the offset
641644
uint4 res = tempbase;
@@ -749,6 +752,9 @@ int4 PcodeSnippet::lex(void)
749752
case SleighSymbol::start_symbol:
750753
yylval.startsym = (StartSymbol *)sym;
751754
return STARTSYM;
755+
case SleighSymbol::offset_symbol:
756+
yylval.offsetsym = (OffsetSymbol *)sym;
757+
return OFFSETSYM;
752758
case SleighSymbol::end_symbol:
753759
yylval.endsym = (EndSymbol *)sym;
754760
return ENDSYM;

Ghidra/Features/Decompiler/src/decompile/cpp/semantics.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ uintb ConstTpl::fix(const ParserWalker &walker) const
119119
switch(type) {
120120
case j_start:
121121
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
122+
case j_offset:
123+
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
122124
case j_next:
123125
return walker.getNaddr().getOffset(); // Fill in next address placeholder with real address
124126
case j_flowref:
@@ -346,6 +348,9 @@ void ConstTpl::saveXml(ostream &s) const
346348
case j_start:
347349
s << "start\"/>";
348350
break;
351+
case j_offset:
352+
s << "operand_offset\"/>";
353+
break;
349354
case j_next:
350355
s << "next\"/>";
351356
break;
@@ -401,6 +406,9 @@ void ConstTpl::restoreXml(const Element *el,const AddrSpaceManager *manage)
401406
else if (typestring=="start") {
402407
type = j_start;
403408
}
409+
else if (typestring=="operand_offset") {
410+
type = j_offset;
411+
}
404412
else if (typestring=="next") {
405413
type = j_next;
406414
}

Ghidra/Features/Decompiler/src/decompile/cpp/semantics.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ConstTpl {
3232
public:
3333
enum const_type { real=0, handle=1, j_start=2, j_next=3, j_curspace=4,
3434
j_curspace_size=5, spaceid=6, j_relative=7,
35-
j_flowref=8, j_flowref_size=9, j_flowdest=10, j_flowdest_size=11 };
35+
j_flowref=8, j_flowref_size=9, j_flowdest=10, j_flowdest_size=11, j_offset=12 };
3636
enum v_field { v_space=0, v_offset=1, v_size=2, v_offset_plus=3 };
3737
private:
3838
const_type type;

Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,8 @@ void SleighCompile::predefinedSymbols(void)
18111811
symtab.addSymbol(spacesym);
18121812
StartSymbol *startsym = new StartSymbol("inst_start",getConstantSpace());
18131813
symtab.addSymbol(startsym);
1814+
OffsetSymbol *offsetsym = new OffsetSymbol("operand_offset",getConstantSpace());
1815+
symtab.addSymbol(offsetsym);
18141816
EndSymbol *endsym = new EndSymbol("inst_next",getConstantSpace());
18151817
symtab.addSymbol(endsym);
18161818
EpsilonSymbol *epsilon = new EpsilonSymbol("epsilon",getConstantSpace());

Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.y

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
LabelSymbol *labelsym;
5858
SubtableSymbol *subtablesym;
5959
StartSymbol *startsym;
60+
OffsetSymbol *offsetsym;
6061
EndSymbol *endsym;
6162
OperandSymbol *operandsym;
6263
VarnodeListSymbol *varlistsym;
@@ -120,6 +121,7 @@
120121
%token <varlistsym> VARLISTSYM
121122
%token <operandsym> OPERANDSYM
122123
%token <startsym> STARTSYM
124+
%token <offsetsym> OFFSETSYM
123125
%token <endsym> ENDSYM
124126
%token <macrosym> MACROSYM
125127
%token <labelsym> LABELSYM
@@ -498,6 +500,7 @@ specificsymbol: VARSYM { $$ = $1; }
498500
| SPECSYM { $$ = $1; }
499501
| OPERANDSYM { $$ = $1; }
500502
| STARTSYM { $$ = $1; }
503+
| OFFSETSYM { $$ = $1; }
501504
| ENDSYM { $$ = $1; }
502505
;
503506
charstring: CHAR { $$ = new string; (*$$) += $1; }
@@ -572,6 +575,7 @@ anysymbol: SPACESYM { $$ = $1; }
572575
| VARLISTSYM { $$ = $1; }
573576
| OPERANDSYM { $$ = $1; }
574577
| STARTSYM { $$ = $1; }
578+
| OFFSETSYM { $$ = $1; }
575579
| ENDSYM { $$ = $1; }
576580
| BITSYM { $$ = $1; }
577581
;

Ghidra/Features/Decompiler/src/decompile/cpp/slghpatexpress.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ PatternExpression *PatternExpression::restoreExpression(const Element *el,Transl
476476
res = new OperandValue();
477477
else if (nm == "start_exp")
478478
res = new StartInstructionValue();
479+
else if (nm == "offset_exp")
480+
res = new OperandOffsetValue();
479481
else if (nm == "end_exp")
480482
res = new EndInstructionValue();
481483
else if (nm == "plus_exp")

Ghidra/Features/Decompiler/src/decompile/cpp/slghpatexpress.hh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ public:
151151
virtual void saveXml(ostream &s) const { s << "<start_exp/>"; }
152152
virtual void restoreXml(const Element *el,Translate *trans) {}
153153
};
154+
155+
class OperandOffsetValue : public PatternValue {
156+
public:
157+
OperandOffsetValue(void) {}
158+
virtual intb getValue(ParserWalker &walker) const {
159+
return (intb)walker.getOffset(-1);
160+
}
161+
virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
162+
virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
163+
virtual intb minValue(void) const { return (intb)0; }
164+
virtual intb maxValue(void) const { return (intb)0; }
165+
virtual void saveXml(ostream &s) const { s << "<offset_exp/>"; }
166+
virtual void restoreXml(const Element *el,Translate *trans) {}
167+
};
154168

155169
class EndInstructionValue : public PatternValue {
156170
public:

Ghidra/Features/Decompiler/src/decompile/cpp/slghscan.l

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ int4 find_symbol(void) {
426426
case SleighSymbol::start_symbol:
427427
yylval.startsym = (StartSymbol *)sym;
428428
return STARTSYM;
429+
case SleighSymbol::offset_symbol:
430+
yylval.offsetsym = (OffsetSymbol *)sym;
431+
return OFFSETSYM;
429432
case SleighSymbol::end_symbol:
430433
yylval.endsym = (EndSymbol *)sym;
431434
return ENDSYM;

Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ void SymbolTable::restoreSymbolHeader(const Element *el)
250250
sym = new OperandSymbol();
251251
else if (el->getName() == "start_sym_head")
252252
sym = new StartSymbol();
253+
else if (el->getName() == "offset_sym_head")
254+
sym = new OffsetSymbol();
253255
else if (el->getName() == "end_sym_head")
254256
sym = new EndSymbol();
255257
else if (el->getName() == "subtable_sym_head")
@@ -1190,6 +1192,70 @@ void StartSymbol::restoreXml(const Element *el,SleighBase *trans)
11901192
patexp->layClaim();
11911193
}
11921194

1195+
OffsetSymbol::OffsetSymbol(const string &nm,AddrSpace *cspc) : SpecificSymbol(nm)
1196+
1197+
{
1198+
const_space = cspc;
1199+
patexp = new OperandOffsetValue();
1200+
patexp->layClaim();
1201+
}
1202+
1203+
OffsetSymbol::~OffsetSymbol(void)
1204+
1205+
{
1206+
if (patexp != (PatternExpression *)0)
1207+
PatternExpression::release(patexp);
1208+
}
1209+
1210+
VarnodeTpl *OffsetSymbol::getVarnode(void) const
1211+
1212+
{ // Returns current operand offset as a constant
1213+
ConstTpl spc(const_space);
1214+
ConstTpl off(ConstTpl::j_offset);
1215+
ConstTpl sz_zero;
1216+
return new VarnodeTpl(spc,off,sz_zero);
1217+
}
1218+
1219+
void OffsetSymbol::getFixedHandle(FixedHandle &hand,ParserWalker &walker) const
1220+
1221+
{
1222+
hand.space = walker.getCurSpace();
1223+
hand.offset_space = (AddrSpace *)0;
1224+
hand.offset_offset = walker.getAddr().getOffset(); // Get starting address of instruction
1225+
hand.size = hand.space->getAddrSize();
1226+
}
1227+
1228+
void OffsetSymbol::print(ostream &s,ParserWalker &walker) const
1229+
1230+
{
1231+
intb val = (intb) walker.getAddr().getOffset();
1232+
s << "0x" << std::hex << val << std::dec;
1233+
}
1234+
1235+
void OffsetSymbol::saveXml(ostream &s) const
1236+
1237+
{
1238+
s << "<offset_sym";
1239+
SleighSymbol::saveXmlHeader(s);
1240+
s << "/>\n";
1241+
}
1242+
1243+
void OffsetSymbol::saveXmlHeader(ostream &s) const
1244+
1245+
{
1246+
s << "<offset_sym_head";
1247+
SleighSymbol::saveXmlHeader(s);
1248+
s << "/>\n";
1249+
}
1250+
1251+
void OffsetSymbol::restoreXml(const Element *el,SleighBase *trans)
1252+
1253+
{
1254+
const_space = trans->getConstantSpace();
1255+
patexp = new OperandOffsetValue();
1256+
patexp->layClaim();
1257+
}
1258+
11931259
EndSymbol::EndSymbol(const string &nm,AddrSpace *cspc) : SpecificSymbol(nm)
11941260

11951261
{

Ghidra/Features/Decompiler/src/decompile/cpp/slghsymbol.hh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SleighSymbol {
2525
public:
2626
enum symbol_type { space_symbol, token_symbol, userop_symbol, value_symbol, valuemap_symbol,
2727
name_symbol, varnode_symbol, varnodelist_symbol, operand_symbol,
28-
start_symbol, end_symbol, subtable_symbol, macro_symbol, section_symbol,
28+
start_symbol, offset_symbol, end_symbol, subtable_symbol, macro_symbol, section_symbol,
2929
bitrange_symbol, context_symbol, epsilon_symbol, label_symbol,
3030
dummy_symbol };
3131
private:
@@ -374,6 +374,23 @@ public:
374374
virtual void restoreXml(const Element *el,SleighBase *trans);
375375
};
376376

377+
class OffsetSymbol : public SpecificSymbol {
378+
AddrSpace *const_space;
379+
PatternExpression *patexp;
380+
public:
381+
OffsetSymbol(void) { patexp = (PatternExpression *)0; } // For use with restoreXml
382+
OffsetSymbol(const string &nm,AddrSpace *cspc);
383+
virtual ~OffsetSymbol(void);
384+
virtual VarnodeTpl *getVarnode(void) const;
385+
virtual PatternExpression *getPatternExpression(void) const { return patexp; }
386+
virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
387+
virtual void print(ostream &s,ParserWalker &walker) const;
388+
virtual symbol_type getType(void) const { return offset_symbol; }
389+
virtual void saveXml(ostream &s) const;
390+
virtual void saveXmlHeader(ostream &s) const;
391+
virtual void restoreXml(const Element *el,SleighBase *trans);
392+
};
393+
377394
class EndSymbol : public SpecificSymbol {
378395
AddrSpace *const_space;
379396
PatternExpression *patexp;

0 commit comments

Comments
 (0)