Merge remote-tracking branch 'origin/GP-369_LostEquates' into Ghidra_9.2

This commit is contained in:
ghidra1 2020-11-05 14:22:28 -05:00
commit 4ba468828f
4 changed files with 88 additions and 34 deletions

View file

@ -5509,6 +5509,7 @@ int4 RuleEqual2Zero::applyOp(PcodeOp *op,Funcdata &data)
if (vn2->isConstant()) {
Address val(vn2->getSpace(),uintb_negate(vn2->getOffset()-1,vn2->getSize()));
unnegvn = data.newVarnode(vn2->getSize(),val);
unnegvn->copySymbolIfValid(vn2); // Propagate any markup
posvn = vn;
}
else {

View file

@ -23,6 +23,7 @@ import org.junit.Test;
import docking.widgets.fieldpanel.field.Field;
import docking.widgets.fieldpanel.support.FieldLocation;
import ghidra.app.cmd.equate.SetEquateCmd;
import ghidra.app.cmd.function.CreateFunctionCmd;
import ghidra.app.cmd.function.DeleteFunctionCmd;
import ghidra.app.cmd.label.RenameLabelCmd;
@ -134,6 +135,13 @@ public class HighSymbolTest extends AbstractDecompilerTest {
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) {
SymbolEntry oldEntry = highSymbol.getFirstWholeMap();
long oldId = highSymbol.getId();
@ -401,4 +409,62 @@ public class HighSymbolTest extends AbstractDecompilerTest {
assertTrue(highSymbol.isNameLocked());
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 int getConvert() {
return convert;
}
@Override
public void restoreXML(XmlPullParser parser) throws PcodeXMLException {
XmlElement symel = parser.start("equatesymbol");

View file

@ -476,12 +476,9 @@ public class LocalSymbolMap {
symbolMap.put(uniqueId, sym);
}
private void newEquateSymbol(long uniqueId, String nm, long val, long hash, Address addr,
TreeMap<String, HighSymbol> constantSymbolMap) {
HighSymbol eqSymbol = constantSymbolMap.get(nm);
if (eqSymbol != null) {
return; // New reference to same symbol
}
private EquateSymbol newEquateSymbol(long uniqueId, String nm, long val, long hash,
Address addr) {
EquateSymbol eqSymbol;
if (uniqueId == 0) {
uniqueId = getNextId();
}
@ -494,7 +491,7 @@ public class LocalSymbolMap {
eqSymbol = new EquateSymbol(uniqueId, conv, val, func, addr, hash);
}
//Do NOT setTypeLock
constantSymbolMap.put(nm, eqSymbol);
return eqSymbol;
}
/**
@ -502,7 +499,6 @@ public class LocalSymbolMap {
* @param dbFunction is the function to pull equates for
*/
private void grabEquates(Function dbFunction) {
TreeMap<String, HighSymbol> constantSymbolMap = null;
// Find named constants via Equates
Program program = dbFunction.getProgram();
EquateTable equateTable = program.getEquateTable();
@ -516,34 +512,21 @@ public class LocalSymbolMap {
continue;
}
long hash[] = DynamicHash.calcConstantHash(instr, eq.getValue());
for (long element : hash) {
if (constantSymbolMap == null) {
constantSymbolMap = new TreeMap<String, HighSymbol>();
}
newEquateSymbol(0, eq.getDisplayName(), eq.getValue(), element, defAddr,
constantSymbolMap);
}
}
if (hash.length == 0) {
continue;
}
Arrays.sort(hash); // Sort in preparation for deduping
String displayName = eq.getDisplayName();
long eqValue = eq.getValue();
// TODO: Find typed constants via DataTypeReferences
// -- for each datatype reference within the scope of the function
// MappedVarKey key = new MappedVarKey(AddressSpace.HASH_SPACE.getAddress(hash),defAddr);
// DynamicSymbol sym = constantSymbolMap.get(key);
// String name = sym != null ? sym.getName() : null;
// sym = new DynamicSymbol(name, dt, dt.getLength(), hash, defAddr, func, 0); // format??
// if (name != null) {
// sym.setTypeLock(true);
// }
// sym.setTypeLock(true);
// sym.setReadOnly(true);
//
// Add constant dynamic symbols to map
if (constantSymbolMap != null) {
for (HighSymbol sym : constantSymbolMap.values()) {
long id = getNextId();
symbolMap.put(id, sym);
EquateSymbol eqSymbol;
for (int i = 0; i < hash.length; ++i) {
if (i != 0 && hash[i - 1] == hash[i]) {
continue; // Found a duplicate, skip it
}
eqSymbol = newEquateSymbol(0, displayName, eqValue, hash[i], defAddr);
symbolMap.put(eqSymbol.getId(), eqSymbol);
}
}
}
}