Equate testing in the decompiler

This commit is contained in:
caheckman 2020-11-05 13:26:55 -05:00
parent f758942dd7
commit 1c3314eaeb
2 changed files with 70 additions and 0 deletions

View file

@ -23,6 +23,7 @@ import org.junit.Test;
import docking.widgets.fieldpanel.field.Field; import docking.widgets.fieldpanel.field.Field;
import docking.widgets.fieldpanel.support.FieldLocation; import docking.widgets.fieldpanel.support.FieldLocation;
import ghidra.app.cmd.equate.SetEquateCmd;
import ghidra.app.cmd.function.CreateFunctionCmd; import ghidra.app.cmd.function.CreateFunctionCmd;
import ghidra.app.cmd.function.DeleteFunctionCmd; import ghidra.app.cmd.function.DeleteFunctionCmd;
import ghidra.app.cmd.label.RenameLabelCmd; import ghidra.app.cmd.label.RenameLabelCmd;
@ -134,6 +135,13 @@ public class HighSymbolTest extends AbstractDecompilerTest {
waitForDecompiler(); waitForDecompiler();
} }
private void applyEquate(String equateName, Address addr, long equateValue) {
modifyProgram(p -> {
SetEquateCmd cmd = new SetEquateCmd(equateName, addr, 0, equateValue);
cmd.applyTo(program);
});
}
private void renameExisting(HighSymbol highSymbol, ClangToken tokenAtCursor, String newName) { private void renameExisting(HighSymbol highSymbol, ClangToken tokenAtCursor, String newName) {
SymbolEntry oldEntry = highSymbol.getFirstWholeMap(); SymbolEntry oldEntry = highSymbol.getFirstWholeMap();
long oldId = highSymbol.getId(); long oldId = highSymbol.getId();
@ -401,4 +409,62 @@ public class HighSymbolTest extends AbstractDecompilerTest {
assertTrue(highSymbol.isNameLocked()); assertTrue(highSymbol.isNameLocked());
assertTrue(highSymbol.isTypeLocked()); assertTrue(highSymbol.isTypeLocked());
} }
@Test
public void testHighSymbol_convert() {
Address subAddr = addr(0x10015ac);
String equateName = "00000000000000000000000001010011b";
int equateValue = 0x53;
applyEquate(equateName, subAddr, equateValue);
decompile("10015ac");
ClangTextField line = getLineContaining("if (param");
FieldLocation loc = loc(line.getLineNumber(), 20);
ClangToken token = line.getToken(loc);
assertTrue(token.getText().equals("0b01010011"));
HighVariable variable = token.getHighVariable();
assertTrue(variable instanceof HighConstant);
HighSymbol highSymbol = variable.getSymbol();
assertTrue(highSymbol instanceof EquateSymbol);
EquateSymbol eqSymbol = (EquateSymbol) highSymbol;
assertEquals(eqSymbol.getConvert(), EquateSymbol.FORMAT_BIN);
assertEquals(eqSymbol.getValue(), equateValue);
}
@Test
public void testHighSymbol_dualequates() {
// Two equates on the same value at different locations
// One a convert, and one a label
Address convAddr = addr(0x100165a);
String convName = "141";
int convValue = 0x8d;
applyEquate(convName, convAddr, convValue);
Address eqAddr = addr(0x10015f1);
String eqName = "BIGEQUATE";
int eqValue = 0x8d;
applyEquate(eqName, eqAddr, eqValue);
decompile("10015ac");
ClangTextField line = getLineContaining(",DAT_010056a8");
FieldLocation loc = loc(line.getLineNumber(), 23);
ClangToken token = line.getToken(loc);
assertTrue(token.getText().equals("141"));
HighVariable variable = token.getHighVariable();
assertTrue(variable instanceof HighConstant);
HighSymbol highSymbol = variable.getSymbol();
assertTrue(highSymbol instanceof EquateSymbol);
EquateSymbol eqSymbol = (EquateSymbol) highSymbol;
assertEquals(eqSymbol.getConvert(), EquateSymbol.FORMAT_DEC);
assertEquals(eqSymbol.getValue(), convValue);
line = getLineContaining("DAT_010056a8 = ");
loc = loc(line.getLineNumber(), 39);
token = line.getToken(loc);
assertTrue(token.getText().equals(eqName));
variable = token.getHighVariable();
assertTrue(variable instanceof HighConstant);
highSymbol = variable.getSymbol();
assertTrue(highSymbol instanceof EquateSymbol);
eqSymbol = (EquateSymbol) highSymbol;
assertEquals(eqSymbol.getConvert(), 0);
assertEquals(eqSymbol.getValue(), eqValue);
}
} }

View file

@ -59,6 +59,10 @@ public class EquateSymbol extends HighSymbol {
public long getValue() { return value; } public long getValue() { return value; }
public int getConvert() {
return convert;
}
@Override @Override
public void restoreXML(XmlPullParser parser) throws PcodeXMLException { public void restoreXML(XmlPullParser parser) throws PcodeXMLException {
XmlElement symel = parser.start("equatesymbol"); XmlElement symel = parser.start("equatesymbol");