Skip to content

Commit 607ae4a

Browse files
committed
Add operand_offset to sleigh Java
Signed-off-by: Klaus Kämpf <[email protected]>
1 parent 17c875e commit 607ae4a

File tree

15 files changed

+373
-1
lines changed

15 files changed

+373
-1
lines changed

Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g

+6
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ specific_symbol[String purpose] returns [SpecificSymbol symbol]
342342
if (sym == null) {
343343
unknownSymbolError($s.getText(), find($s), "start, end, next2, operand, epsilon, or varnode", purpose);
344344
} else if(sym.getType() != symbol_type.start_symbol
345+
&& sym.getType() != symbol_type.offset_symbol
345346
&& sym.getType() != symbol_type.end_symbol
346347
&& sym.getType() != symbol_type.next2_symbol
347348
&& sym.getType() != symbol_type.operand_symbol
@@ -839,6 +840,7 @@ pattern_symbol[String purpose] returns [PatternExpression expr]
839840
}
840841
$expr = os.getPatternExpression();
841842
} else if(sym.getType() == symbol_type.start_symbol
843+
|| sym.getType() == symbol_type.offset_symbol
842844
|| sym.getType() == symbol_type.end_symbol
843845
|| sym.getType() == symbol_type.next2_symbol
844846
|| sym.getType() == symbol_type.epsilon_symbol
@@ -872,6 +874,7 @@ pattern_symbol2[String purpose] returns [PatternExpression expr]
872874
if (sym == null) {
873875
unknownSymbolError($s.getText(), find($s), "start, end, next2, operand, epsilon, or varnode", purpose);
874876
} else if(sym.getType() == symbol_type.start_symbol
877+
|| sym.getType() == symbol_type.offset_symbol
875878
|| sym.getType() == symbol_type.end_symbol
876879
|| sym.getType() == symbol_type.next2_symbol
877880
|| sym.getType() == symbol_type.operand_symbol
@@ -943,6 +946,7 @@ cstatement[VectorSTL<ContextChange> r]
943946
|| sym.getType() == symbol_type.name_symbol
944947
|| sym.getType() == symbol_type.varnodelist_symbol
945948
|| sym.getType() == symbol_type.start_symbol
949+
|| sym.getType() == symbol_type.offset_symbol
946950
|| sym.getType() == symbol_type.end_symbol
947951
|| sym.getType() == symbol_type.next2_symbol
948952
|| sym.getType() == symbol_type.operand_symbol
@@ -1170,6 +1174,7 @@ assignment returns [VectorSTL<OpTpl> value]
11701174
if (sym == null) {
11711175
$value = pcode.newOutput(find(id), false, e, $id.getText());
11721176
} else if(sym.getType() != symbol_type.start_symbol
1177+
&& sym.getType() != symbol_type.offset_symbol
11731178
&& sym.getType() != symbol_type.end_symbol
11741179
&& sym.getType() != symbol_type.next2_symbol
11751180
&& sym.getType() != symbol_type.operand_symbol
@@ -1486,6 +1491,7 @@ expr_apply returns [Object value]
14861491
pcode.reportError(find($t), "macro invocation not allowed as expression");
14871492
}
14881493
} else if(sym.getType() == symbol_type.start_symbol
1494+
|| sym.getType() == symbol_type.offset_symbol
14891495
|| sym.getType() == symbol_type.end_symbol
14901496
|| sym.getType() == symbol_type.next2_symbol
14911497
|| sym.getType() == symbol_type.operand_symbol

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssemblerBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ else if (sym instanceof VarnodeSymbol) {
570570
else if (sym instanceof StartSymbol) {
571571
// Ignore. We handle inst_start in semantic processing
572572
}
573+
else if (sym instanceof OffsetSymbol) {
574+
// Ignore. We handle inst_start in semantic processing
575+
}
573576
else if (sym instanceof EndSymbol) {
574577
// Ignore. We handle inst_next in semantic processing
575578
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/*
17+
* Created on Feb 8, 2005
18+
*
19+
*/
20+
package ghidra.app.plugin.processors.sleigh.expression;
21+
22+
import ghidra.app.plugin.processors.sleigh.ParserWalker;
23+
import ghidra.app.plugin.processors.sleigh.SleighLanguage;
24+
import ghidra.program.model.address.Address;
25+
import ghidra.program.model.mem.MemoryAccessException;
26+
import ghidra.xml.XmlPullParser;
27+
28+
/**
29+
*
30+
*
31+
* The offset value of the current instructions address
32+
*/
33+
public class OffsetInstructionValue extends PatternValue {
34+
private static final int HASH = "[operand_offset]".hashCode();
35+
36+
@Override
37+
public int hashCode() {
38+
return HASH;
39+
}
40+
41+
@Override
42+
public boolean equals(Object obj) {
43+
return obj instanceof OffsetInstructionValue;
44+
}
45+
46+
/* (non-Javadoc)
47+
* @see ghidra.app.plugin.processors.sleigh.expression.PatternValue#minValue()
48+
*/
49+
@Override
50+
public long minValue() {
51+
return 0;
52+
}
53+
54+
/* (non-Javadoc)
55+
* @see ghidra.app.plugin.processors.sleigh.expression.PatternValue#maxValue()
56+
*/
57+
@Override
58+
public long maxValue() {
59+
return 0;
60+
}
61+
62+
/* (non-Javadoc)
63+
* @see ghidra.app.plugin.processors.sleigh.expression.PatternExpression#getValue(ghidra.app.plugin.processors.sleigh.ParserWalker)
64+
*/
65+
@Override
66+
public long getValue(ParserWalker walker) throws MemoryAccessException {
67+
return walker.getOffset(-1);
68+
}
69+
70+
/* (non-Javadoc)
71+
* @see ghidra.app.plugin.processors.sleigh.PatternExpression#restoreXml(org.jdom.Element)
72+
*/
73+
@Override
74+
public void restoreXml(XmlPullParser parser, SleighLanguage lang) {
75+
parser.discardSubTree("offset_exp");
76+
// Nothing to do
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "[operand_offset]";
82+
}
83+
}

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/processors/sleigh/expression/PatternExpression.java

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ else if (nm.equals("operand_exp"))
4949
res = new OperandValue();
5050
else if (nm.equals("start_exp"))
5151
res = new StartInstructionValue();
52+
else if (nm.equals("offset_exp"))
53+
res = new OffsetInstructionValue();
5254
else if (nm.equals("end_exp"))
5355
res = new EndInstructionValue();
5456
else if (nm.equals("next2_exp"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* ###
2+
* IP: GHIDRA
3+
* REVIEWED: YES
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
/*
18+
* Created on Feb 8, 2005
19+
*
20+
*/
21+
package ghidra.app.plugin.processors.sleigh.symbol;
22+
23+
import ghidra.app.plugin.processors.sleigh.*;
24+
import ghidra.app.plugin.processors.sleigh.expression.*;
25+
import ghidra.program.model.mem.*;
26+
import ghidra.xml.*;
27+
28+
import java.util.*;
29+
30+
/**
31+
*
32+
*
33+
* TripleSymbol with semantic value equal to offset of instruction's
34+
* current address
35+
*/
36+
public class OffsetSymbol extends SpecificSymbol {
37+
38+
private PatternExpression patexp;
39+
40+
/* (non-Javadoc)
41+
* @see ghidra.app.plugin.processors.sleigh.symbol.TripleSymbol#getPatternExpression()
42+
*/
43+
@Override
44+
public PatternExpression getPatternExpression() {
45+
return patexp;
46+
}
47+
48+
/* (non-Javadoc)
49+
* @see ghidra.app.plugin.processors.sleigh.symbol.TripleSymbol#getFixedHandle(ghidra.app.plugin.processors.sleigh.FixedHandle, ghidra.app.plugin.processors.sleigh.ParserWalker)
50+
*/
51+
@Override
52+
public void getFixedHandle(FixedHandle hand, ParserWalker walker) {
53+
hand.space = walker.getCurSpace();
54+
hand.offset_space = null;
55+
hand.offset_offset = walker.getAddr().getOffset();
56+
hand.size = hand.space.getPointerSize();
57+
}
58+
59+
/* (non-Javadoc)
60+
* @see ghidra.app.plugin.processors.sleigh.symbol.TripleSymbol#print(ghidra.app.plugin.processors.sleigh.ParserWalker)
61+
*/
62+
@Override
63+
public String print(ParserWalker walker) throws MemoryAccessException {
64+
long val = walker.getAddr().getOffset();
65+
return "0x" + Long.toHexString(val);
66+
}
67+
68+
@Override
69+
public void printList(ParserWalker walker, ArrayList<Object> list) {
70+
list.add(walker.getParentHandle());
71+
}
72+
/* (non-Javadoc)
73+
* @see ghidra.app.plugin.processors.sleigh.symbol.Symbol#restoreXml(org.jdom.Element, ghidra.app.plugin.processors.sleigh.SleighLanguage)
74+
*/
75+
@Override
76+
public void restoreXml(XmlPullParser parser, SleighLanguage sleigh) {
77+
XmlElement element = parser.start("offset_sym");
78+
patexp = new OffsetInstructionValue();
79+
parser.end(element);
80+
}
81+
82+
}

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/processors/sleigh/symbol/SymbolTable.java

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ else if (el.getName().equals("operand_sym_head"))
150150
sym = new OperandSymbol();
151151
else if (el.getName().equals("start_sym_head"))
152152
sym = new StartSymbol();
153+
else if (el.getName().equals("offset_sym_head"))
154+
sym = new OffsetSymbol();
153155
else if (el.getName().equals("end_sym_head"))
154156
sym = new EndSymbol();
155157
else if (el.getName().equals("next2_sym_head"))

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/semantics/ConstTpl.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public enum const_type {
4646
j_flowref,
4747
j_flowref_size,
4848
j_flowdest,
49-
j_flowdest_size
49+
j_flowdest_size,
50+
j_offset
5051
}
5152

5253
public enum v_field {
@@ -297,6 +298,9 @@ public void saveXml(PrintStream s) {
297298
case j_start:
298299
s.append("start\"/>");
299300
break;
301+
case j_offset:
302+
s.append("offset\"/>");
303+
break;
300304
case j_next:
301305
s.append("next\"/>");
302306
break;
@@ -351,6 +355,9 @@ else if (typestring.equals("handle")) {
351355
else if (typestring.equals("start")) {
352356
type = const_type.j_start;
353357
}
358+
else if (typestring.equals("offset")) {
359+
type = const_type.j_offset;
360+
}
354361
else if (typestring.equals("next")) {
355362
type = const_type.j_next;
356363
}

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/SleighCompile.java

+2
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ private void predefinedSymbols() {
287287
symtab.addSymbol(spacesym);
288288
StartSymbol startsym = new StartSymbol(location, "inst_start", getConstantSpace());
289289
symtab.addSymbol(startsym);
290+
OffsetSymbol offsetsym = new OffsetSymbol(location, "operand_offset", getConstantSpace());
291+
symtab.addSymbol(offsetsym);
290292
EndSymbol endsym = new EndSymbol(location, "inst_next", getConstantSpace());
291293
symtab.addSymbol(endsym);
292294
Next2Symbol next2sym = new Next2Symbol(location, "inst_next2", getConstantSpace());

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/Yylval.java

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Yylval {
3030
VarnodeListSymbol varlistsym;
3131
OperandSymbol operandsym;
3232
StartSymbol startsym;
33+
OffsetSymbol offsetsym;
3334
EndSymbol endsym;
3435
Next2Symbol next2sym;
3536
SubtableSymbol subtablesym;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* ###
2+
* IP: GHIDRA
3+
* REVIEWED: YES
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package ghidra.pcodeCPort.slghpatexpress;
18+
19+
import generic.stl.VectorSTL;
20+
import ghidra.app.plugin.processors.sleigh.ParserWalker;
21+
import ghidra.pcodeCPort.translate.Translate;
22+
import ghidra.sleigh.grammar.Location;
23+
24+
import java.io.PrintStream;
25+
26+
import org.jdom.Element;
27+
28+
public class OffsetInstructionValue extends PatternValue {
29+
30+
public OffsetInstructionValue(Location location) {
31+
super(location);
32+
}
33+
34+
public long getValue(ParserWalker pos) {
35+
return pos.getOffset(-1);
36+
}
37+
38+
@Override
39+
public TokenPattern genMinPattern(VectorSTL<TokenPattern> ops) {
40+
return new TokenPattern(location);
41+
}
42+
43+
@Override
44+
public TokenPattern genPattern(long val) {
45+
return new TokenPattern(location);
46+
}
47+
48+
@Override
49+
public long minValue() {
50+
return 0;
51+
}
52+
53+
@Override
54+
public long maxValue() {
55+
return 0;
56+
}
57+
58+
@Override
59+
public void saveXml(PrintStream s) {
60+
s.append("<offset_exp/>");
61+
}
62+
63+
@Override
64+
public void restoreXml(Element el, Translate trans) {
65+
}
66+
67+
}

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slghpatexpress/PatternExpression.java

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ else if (nm.equals("operand_exp")) {
8484
else if (nm.equals("start_exp")) {
8585
res = new StartInstructionValue(null);
8686
}
87+
else if (nm.equals("offset_exp")) {
88+
res = new OffsetInstructionValue(null);
89+
}
8790
else if (nm.equals("end_exp")) {
8891
res = new EndInstructionValue(null);
8992
}

0 commit comments

Comments
 (0)