Skip to content

Commit 17c875e

Browse files
committed
Introduce operand_offset
Signed-off-by: Klaus Kämpf <[email protected]>
1 parent 72abe9e commit 17c875e

File tree

10 files changed

+124
-2
lines changed

10 files changed

+124
-2
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern int pcodeerror(const char *str );
3939
UserOpSymbol *useropsym;
4040
LabelSymbol *labelsym;
4141
StartSymbol *startsym;
42+
OffsetSymbol *offsetsym;
4243
EndSymbol *endsym;
4344
Next2Symbol *next2sym;
4445
OperandSymbol *operandsym;
@@ -78,6 +79,7 @@ extern int pcodeerror(const char *str );
7879
%token <varsym> VARSYM
7980
%token <operandsym> OPERANDSYM
8081
%token <startsym> STARTSYM
82+
%token <offsetsym> OFFSETSYM
8183
%token <endsym> ENDSYM
8284
%token <next2sym> NEXT2SYM
8385
%token <labelsym> LABELSYM
@@ -225,6 +227,7 @@ label: '<' LABELSYM '>' { $$ = $2; }
225227
specificsymbol: VARSYM { $$ = $1; }
226228
| OPERANDSYM { $$ = $1; }
227229
| STARTSYM { $$ = $1; }
230+
| OFFSETSYM { $$ = $1; }
228231
| ENDSYM { $$ = $1; }
229232
| NEXT2SYM { $$ = $1; }
230233
;
@@ -752,6 +755,9 @@ int4 PcodeSnippet::lex(void)
752755
case SleighSymbol::start_symbol:
753756
yylval.startsym = (StartSymbol *)sym;
754757
return STARTSYM;
758+
case SleighSymbol::offset_symbol:
759+
yylval.offsetsym = (OffsetSymbol *)sym;
760+
return OFFSETSYM;
755761
case SleighSymbol::end_symbol:
756762
yylval.endsym = (EndSymbol *)sym;
757763
return ENDSYM;

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

+8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ uintb ConstTpl::fix(const ParserWalker &walker) const
121121
switch(type) {
122122
case j_start:
123123
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
124+
case j_offset:
125+
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
124126
case j_next:
125127
return walker.getNaddr().getOffset(); // Fill in next address placeholder with real address
126128
case j_next2:
@@ -350,6 +352,9 @@ void ConstTpl::saveXml(ostream &s) const
350352
case j_start:
351353
s << "start\"/>";
352354
break;
355+
case j_offset:
356+
s << "operand_offset\"/>";
357+
break;
353358
case j_next:
354359
s << "next\"/>";
355360
break;
@@ -408,6 +413,9 @@ void ConstTpl::restoreXml(const Element *el,const AddrSpaceManager *manage)
408413
else if (typestring=="start") {
409414
type = j_start;
410415
}
416+
else if (typestring=="operand_offset") {
417+
type = j_offset;
418+
}
411419
else if (typestring=="next") {
412420
type = j_next;
413421
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ConstTpl {
3434
public:
3535
enum const_type { real=0, handle=1, j_start=2, j_next=3, j_next2=4, j_curspace=5,
3636
j_curspace_size=6, spaceid=7, j_relative=8,
37-
j_flowref=9, j_flowref_size=10, j_flowdest=11, j_flowdest_size=12 };
37+
j_flowref=9, j_flowref_size=10, j_flowdest=11, j_flowdest_size=12, j_offset=13 };
3838
enum v_field { v_space=0, v_offset=1, v_size=2, v_offset_plus=3 };
3939
private:
4040
const_type type;

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

+2
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,8 @@ void SleighCompile::predefinedSymbols(void)
18171817
symtab.addSymbol(spacesym);
18181818
StartSymbol *startsym = new StartSymbol("inst_start",getConstantSpace());
18191819
symtab.addSymbol(startsym);
1820+
OffsetSymbol *offsetsym = new OffsetSymbol("operand_offset",getConstantSpace());
1821+
symtab.addSymbol(offsetsym);
18201822
EndSymbol *endsym = new EndSymbol("inst_next",getConstantSpace());
18211823
symtab.addSymbol(endsym);
18221824
Next2Symbol *next2sym = new Next2Symbol("inst_next2",getConstantSpace());

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

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ extern int sleigherror(const char *str );
5959
LabelSymbol *labelsym;
6060
SubtableSymbol *subtablesym;
6161
StartSymbol *startsym;
62+
OffsetSymbol *offsetsym;
6263
EndSymbol *endsym;
6364
Next2Symbol *next2sym;
6465
OperandSymbol *operandsym;
@@ -123,6 +124,7 @@ extern int sleigherror(const char *str );
123124
%token <varlistsym> VARLISTSYM
124125
%token <operandsym> OPERANDSYM
125126
%token <startsym> STARTSYM
127+
%token <offsetsym> OFFSETSYM
126128
%token <endsym> ENDSYM
127129
%token <next2sym> NEXT2SYM
128130
%token <macrosym> MACROSYM
@@ -504,6 +506,7 @@ specificsymbol: VARSYM { $$ = $1; }
504506
| SPECSYM { $$ = $1; }
505507
| OPERANDSYM { $$ = $1; }
506508
| STARTSYM { $$ = $1; }
509+
| OFFSETSYM { $$ = $1; }
507510
| ENDSYM { $$ = $1; }
508511
| NEXT2SYM { $$ = $1; }
509512
;
@@ -579,6 +582,7 @@ anysymbol: SPACESYM { $$ = $1; }
579582
| VARLISTSYM { $$ = $1; }
580583
| OPERANDSYM { $$ = $1; }
581584
| STARTSYM { $$ = $1; }
585+
| OFFSETSYM { $$ = $1; }
582586
| ENDSYM { $$ = $1; }
583587
| NEXT2SYM { $$ = $1; }
584588
| BITSYM { $$ = $1; }

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

+2
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ PatternExpression *PatternExpression::restoreExpression(const Element *el,Transl
478478
res = new OperandValue();
479479
else if (nm == "start_exp")
480480
res = new StartInstructionValue();
481+
else if (nm == "offset_exp")
482+
res = new OperandOffsetValue();
481483
else if (nm == "end_exp")
482484
res = new EndInstructionValue();
483485
else if (nm == "plus_exp")

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

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

157171
class EndInstructionValue : public PatternValue {
158172
public:

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

+3
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ int4 find_symbol(void) {
431431
case SleighSymbol::start_symbol:
432432
sleighlval.startsym = (StartSymbol *)sym;
433433
return STARTSYM;
434+
case SleighSymbol::offset_symbol:
435+
sleighlval.offsetsym = (OffsetSymbol *)sym;
436+
return OFFSETSYM;
434437
case SleighSymbol::end_symbol:
435438
sleighlval.endsym = (EndSymbol *)sym;
436439
return ENDSYM;

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

+66
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ void SymbolTable::restoreSymbolHeader(const Element *el)
254254
sym = new OperandSymbol();
255255
else if (el->getName() == "start_sym_head")
256256
sym = new StartSymbol();
257+
else if (el->getName() == "offset_sym_head")
258+
sym = new OffsetSymbol();
257259
else if (el->getName() == "end_sym_head")
258260
sym = new EndSymbol();
259261
else if (el->getName() == "next2_sym_head")
@@ -1196,6 +1198,70 @@ void StartSymbol::restoreXml(const Element *el,SleighBase *trans)
11961198
patexp->layClaim();
11971199
}
11981200

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

12011267
{

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SleighSymbol {
2727
public:
2828
enum symbol_type { space_symbol, token_symbol, userop_symbol, value_symbol, valuemap_symbol,
2929
name_symbol, varnode_symbol, varnodelist_symbol, operand_symbol,
30-
start_symbol, end_symbol, next2_symbol, subtable_symbol, macro_symbol, section_symbol,
30+
start_symbol, offset_symbol, end_symbol, next2_symbol, subtable_symbol, macro_symbol, section_symbol,
3131
bitrange_symbol, context_symbol, epsilon_symbol, label_symbol,
3232
dummy_symbol };
3333
private:
@@ -376,6 +376,23 @@ public:
376376
virtual void restoreXml(const Element *el,SleighBase *trans);
377377
};
378378

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

0 commit comments

Comments
 (0)