Skip to content

Commit ecc24c7

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

File tree

15 files changed

+378
-1
lines changed

15 files changed

+378
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ specific_symbol[String purpose] returns [SpecificSymbol symbol]
351351
if (sym == null) {
352352
unknownSymbolError($s.getText(), find($s), "start, end, operand, epsilon, or varnode", purpose);
353353
} else if(sym.getType() != symbol_type.start_symbol
354+
&& sym.getType() != symbol_type.offset_symbol
354355
&& sym.getType() != symbol_type.end_symbol
355356
&& sym.getType() != symbol_type.operand_symbol
356357
&& sym.getType() != symbol_type.epsilon_symbol
@@ -847,6 +848,7 @@ pattern_symbol[String purpose] returns [PatternExpression expr]
847848
}
848849
$expr = os.getPatternExpression();
849850
} else if(sym.getType() == symbol_type.start_symbol
851+
|| sym.getType() == symbol_type.offset_symbol
850852
|| sym.getType() == symbol_type.end_symbol
851853
|| sym.getType() == symbol_type.epsilon_symbol
852854
|| sym.getType() == symbol_type.varnode_symbol) {
@@ -879,6 +881,7 @@ pattern_symbol2[String purpose] returns [PatternExpression expr]
879881
if (sym == null) {
880882
unknownSymbolError($s.getText(), find($s), "start, end, operand, epsilon, or varnode", purpose);
881883
} else if(sym.getType() == symbol_type.start_symbol
884+
|| sym.getType() == symbol_type.offset_symbol
882885
|| sym.getType() == symbol_type.end_symbol
883886
|| sym.getType() == symbol_type.operand_symbol
884887
|| sym.getType() == symbol_type.epsilon_symbol
@@ -949,6 +952,7 @@ cstatement[VectorSTL<ContextChange> r]
949952
|| sym.getType() == symbol_type.name_symbol
950953
|| sym.getType() == symbol_type.varnodelist_symbol
951954
|| sym.getType() == symbol_type.start_symbol
955+
|| sym.getType() == symbol_type.offset_symbol
952956
|| sym.getType() == symbol_type.end_symbol
953957
|| sym.getType() == symbol_type.operand_symbol
954958
|| sym.getType() == symbol_type.epsilon_symbol
@@ -1175,6 +1179,7 @@ assignment returns [VectorSTL<OpTpl> value]
11751179
if (sym == null) {
11761180
$value = pcode.newOutput(find(id), false, e, $id.getText());
11771181
} else if(sym.getType() != symbol_type.start_symbol
1182+
&& sym.getType() != symbol_type.offset_symbol
11781183
&& sym.getType() != symbol_type.end_symbol
11791184
&& sym.getType() != symbol_type.operand_symbol
11801185
&& sym.getType() != symbol_type.epsilon_symbol
@@ -1488,6 +1493,7 @@ expr_apply returns [Object value]
14881493
pcode.reportError(find($t), "macro invocation not allowed as expression");
14891494
}
14901495
} else if(sym.getType() == symbol_type.start_symbol
1496+
|| sym.getType() == symbol_type.offset_symbol
14911497
|| sym.getType() == symbol_type.end_symbol
14921498
|| sym.getType() == symbol_type.operand_symbol
14931499
|| sym.getType() == symbol_type.epsilon_symbol

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ else if (sym instanceof VarnodeSymbol) {
377377
else if (sym instanceof StartSymbol) {
378378
// Ignore. We handle inst_start in semantic processing
379379
}
380+
else if (sym instanceof OffsetSymbol) {
381+
// Ignore. We handle inst_start in semantic processing
382+
}
380383
else if (sym instanceof EndSymbol) {
381384
// Ignore. We handle inst_next in semantic processing
382385
}
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

Lines changed: 2 additions & 0 deletions
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("plus_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

Lines changed: 2 additions & 0 deletions
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("subtable_sym_head"))

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public enum const_type {
4848
j_flowref,
4949
j_flowref_size,
5050
j_flowdest,
51-
j_flowdest_size
51+
j_flowdest_size,
52+
j_offset
5253
}
5354

5455
public enum v_field {
@@ -205,6 +206,8 @@ public long fix(ParserWalker walker) {
205206
switch (type) {
206207
case j_start:
207208
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
209+
case j_offset:
210+
return walker.getAddr().getOffset(); // Fill in starting address placeholder with real address
208211
case j_next:
209212
return walker.getNaddr().getOffset(); // Fill in next address placeholder with real address
210213
case j_curspace_size:
@@ -431,6 +434,9 @@ public void saveXml(PrintStream s) {
431434
case j_start:
432435
s.append("start\"/>");
433436
break;
437+
case j_offset:
438+
s.append("offset\"/>");
439+
break;
434440
case j_next:
435441
s.append("next\"/>");
436442
break;
@@ -482,6 +488,9 @@ else if (typestring.equals("handle")) {
482488
else if (typestring.equals("start")) {
483489
type = const_type.j_start;
484490
}
491+
else if (typestring.equals("offset")) {
492+
type = const_type.j_offset;
493+
}
485494
else if (typestring.equals("next")) {
486495
type = const_type.j_next;
487496
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private void initializeSymbols() {
8787

8888
Location internalLoc = Location.INTERNALLY_DEFINED;
8989
symbolMap.put("inst_start", new StartSymbol(internalLoc, "inst_start", getConstantSpace()));
90+
symbolMap.put("operand_offset", new OffsetSymbol(internalLoc, "operand_offset", getConstantSpace()));
9091
symbolMap.put("inst_next", new EndSymbol(internalLoc, "inst_next", getConstantSpace()));
9192
symbolMap.put("inst_ref", new FlowRefSymbol(internalLoc, "inst_ref", getConstantSpace()));
9293
symbolMap.put("inst_dest",

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

Lines changed: 2 additions & 0 deletions
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
EpsilonSymbol epsilon = new EpsilonSymbol(location, "epsilon", getConstantSpace());

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Yylval {
3131
VarnodeListSymbol varlistsym;
3232
OperandSymbol operandsym;
3333
StartSymbol startsym;
34+
OffsetSymbol offsetsym;
3435
EndSymbol endsym;
3536
SubtableSymbol subtablesym;
3637
MacroSymbol macrosym;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.pcodeCPort.context.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+
@Override
35+
public long getValue(ParserWalker pos) {
36+
return pos.getOffset(-1);
37+
}
38+
39+
@Override
40+
public TokenPattern genMinPattern(VectorSTL<TokenPattern> ops) {
41+
return new TokenPattern(location);
42+
}
43+
44+
@Override
45+
public TokenPattern genPattern(long val) {
46+
return new TokenPattern(location);
47+
}
48+
49+
@Override
50+
public long minValue() {
51+
return 0;
52+
}
53+
54+
@Override
55+
public long maxValue() {
56+
return 0;
57+
}
58+
59+
@Override
60+
public void saveXml(PrintStream s) {
61+
s.append("<offset_exp/>");
62+
}
63+
64+
@Override
65+
public void restoreXml(Element el, Translate trans) {
66+
}
67+
68+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ else if (nm.equals("operand_exp")) {
8888
else if (nm.equals("start_exp")) {
8989
res = new StartInstructionValue(null);
9090
}
91+
else if (nm.equals("offset_exp")) {
92+
res = new OffsetInstructionValue(null);
93+
}
9194
else if (nm.equals("end_exp")) {
9295
res = new EndInstructionValue(null);
9396
}

0 commit comments

Comments
 (0)