GT-3227 - review fixes

This commit is contained in:
dragonmacher 2019-10-15 15:45:48 -04:00
parent 665a83d6c0
commit c4b6058c31
4 changed files with 185 additions and 279 deletions

View file

@ -17,9 +17,6 @@ package ghidra.program.database.symbol;
import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import db.*;
import db.util.ErrorHandler;
@ -27,14 +24,12 @@ import ghidra.program.database.*;
import ghidra.program.database.map.AddressKeyAddressIterator;
import ghidra.program.database.map.AddressMap;
import ghidra.program.model.address.*;
import ghidra.program.model.data.Enum;
import ghidra.program.model.listing.*;
import ghidra.program.model.scalar.Scalar;
import ghidra.program.model.symbol.Equate;
import ghidra.program.model.symbol.EquateTable;
import ghidra.program.util.ChangeManager;
import ghidra.program.util.EquateInfo;
import ghidra.util.*;
import ghidra.util.Lock;
import ghidra.util.UniversalID;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
@ -114,112 +109,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
program.dbError(e);
}
@Override
public void applyEnum(AddressSetView addresses, Enum enoom, TaskMonitor monitor,
boolean shouldDoOnSubOps) throws CancelledException {
if (addresses == null) {
throw new IllegalArgumentException("Can't apply Enum over null addresses");
}
if (enoom == null) {
throw new IllegalArgumentException("Data Type is null");
}
Consumer<Instruction> applyEquates = instruction -> {
if (monitor.isCancelled()) {
return;
}
for (int opIndex = 0; opIndex < instruction.getNumOperands(); opIndex++) {
if (!shouldDoOnSubOps) {
// Only apply equates to scalars that are not contained in sub operands.
Scalar scalar = instruction.getScalar(opIndex);
maybeCreateEquateOnScalar(enoom, instruction, opIndex, scalar);
}
else {
// Apply equates to scalars in the sub operands as well.
List<?> subOperands = instruction.getDefaultOperandRepresentationList(opIndex);
for (Object subOp : subOperands) {
maybeCreateEquateOnScalar(enoom, instruction, opIndex, subOp);
}
}
}
};
Listing listing = program.getListing();
InstructionIterator it = listing.getInstructions(addresses, true);
Stream<Instruction> instructions = StreamSupport.stream(it.spliterator(), false);
try {
lock.acquire();
instructions.forEach(applyEquates);
}
finally {
lock.release();
}
}
private void maybeCreateEquateOnScalar(Enum enoom, Instruction instruction, int opIndex,
Object operandRepresentation) {
if (!(operandRepresentation instanceof Scalar)) {
return;
}
Scalar scalar = (Scalar) operandRepresentation;
int enoomLength = enoom.getLength();
boolean anyValuesMatch = Arrays.stream(enoom.getValues()).anyMatch(enumValue -> {
return scalar.equals(new Scalar(enoomLength * 8, enumValue, scalar.isSigned()));
});
if (!anyValuesMatch) {
return;
}
if (program.getDataTypeManager().findDataTypeForID(enoom.getUniversalID()) == null) {
enoom = (Enum) program.getDataTypeManager().addDataType(enoom, null);
}
Address addr = instruction.getAddress();
removeUnusedEquates(opIndex, scalar, addr);
long value = scalar.getValue();
String equateName = EquateManager.formatNameForEquate(enoom.getUniversalID(), value);
Equate equate = getOrCreateEquate(equateName, value);
equate.addReference(addr, opIndex);
}
private void removeUnusedEquates(int opIndex, Scalar scalar, Address addr) {
Equate existingEquate = getEquate(addr, opIndex, scalar.getValue());
if (existingEquate != null) {
if (existingEquate.getReferenceCount() <= 1) {
removeEquate(existingEquate.getName());
}
}
}
private Equate getOrCreateEquate(String name, long value) {
Equate equate = getEquate(name);
if (equate != null) {
return equate;
}
try {
equate = createEquate(name, value);
}
catch (DuplicateNameException | InvalidInputException e) {
// These should not happen:
// Duplicate will not happen since we checked for the existence first; Invalid
// can't happen since we built the name ourselves (we are assuming)
Msg.error(this, "Unexpected error creating equate", e); // just in case
}
return equate;
}
@Override
public Equate createEquate(String name, long value)
throws DuplicateNameException, InvalidInputException {

View file

@ -21,9 +21,9 @@ import java.util.Iterator;
import java.util.List;
import ghidra.program.model.address.*;
import ghidra.program.model.data.Enum;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
/**
* EquateTable manages all equates for program. An equate defines a relationship
* between a scalar value and a string whereby the scalar may be represented by
@ -32,40 +32,25 @@ import ghidra.util.task.TaskMonitor;
*/
public interface EquateTable {
/**
* Creates equates and/or adds references for scalars
* in the given address set using the given data type.
* The data type given must be an enumeration data type.
* @param addrSet the address set to use.
* @param dataType the data type to use.
* @param monitor task monitor to cancel the remove operation
* @param shouldDoOnSubOps true if the enum should be applied inside sub-operands as well.
*
* @throws CancelledException if the operation is cancelled
* @throws IllegalArgumentException if the dataType is null or not an enum.
* Creates a new equate
* @param name the name to associate with the given value.
* @param value the value to associate with the given name.
* @return the equate
* @exception DuplicateNameException thrown if name is already in use
* as an equate.
* @throws InvalidInputException if name contains blank characters,
* is zero length, or is null
*/
public void applyEnum(AddressSetView addrSet, Enum dataType, TaskMonitor monitor,
boolean shouldDoOnSubOps) throws CancelledException;
public Equate createEquate(String name, long value)
throws DuplicateNameException, InvalidInputException;
/**
* Creates a new equate
* @param name the name to associate with the given value.
* @param value the value to associate with the given name.
* @exception DuplicateNameException thrown if name is already in use
* as an equate.
* @throws InvalidInputException if name contains blank characters,
* is zero length, or is null
*/
public Equate createEquate(String name,long value)
throws DuplicateNameException, InvalidInputException;
/**
* Removes the equate from the program.
* @param name the name of the equate to remove.
* @return true if the equate existed, false otherwise.
*/
/**
* Removes the equate from the program.
* @param name the name of the equate to remove.
* @return true if the equate existed, false otherwise.
*/
public boolean removeEquate(String name);
/**
@ -75,24 +60,25 @@ public interface EquateTable {
* @param monitor task monitor to cancel the remove operation
* @throws CancelledException if the operation was cancelled.
*/
public void deleteAddressRange(Address start, Address end, TaskMonitor monitor) throws CancelledException;
public void deleteAddressRange(Address start, Address end, TaskMonitor monitor)
throws CancelledException;
/**
* Returns the equate with the given name, null if no such equate
* exists.
* @param name the of the equate to be retrieved.
*/
public Equate getEquate(String name);
/**
* Returns the equate with the given name, null if no such equate exists
* @param name the of the equate to be retrieved
* @return the equate
*/
public Equate getEquate(String name);
/**
* Returns the first equate found that is associated with the given
* value at the given reference address and operand position;
* @param reference address where the equate is used.
* @param opndPosition the operand index of the operand where the equate is used.
* @param value the value where the equate is used.
/**
* Returns the first equate found that is associated with the given
* value at the given reference address and operand position;
* @param reference address where the equate is used.
* @param opndPosition the operand index of the operand where the equate is used.
* @param value the value where the equate is used.
* @return the equate or null if there is no such equate.
*/
public Equate getEquate(Address reference, int opndPosition, long value);
*/
public Equate getEquate(Address reference, int opndPosition, long value);
/**
* Returns the equates (one for each scalar) at the given reference address
@ -102,7 +88,7 @@ public interface EquateTable {
* @return the list of equates or empty list if there is no such equate.
*/
public List<Equate> getEquates(Address reference, int opndPosition);
/**
* Returns the equates (one for each scalar and opIndex) at the given reference address.
* For an instruction a given operand can have multiple scalars.
@ -111,39 +97,42 @@ public interface EquateTable {
*/
public List<Equate> getEquates(Address reference);
/**
* Returns an address iterator over all the addresses where
* equates have been set.
*/
public AddressIterator getEquateAddresses();
/**
* Returns all equates defined for value.
* @param value the value to get all equates for.
*/
public List<Equate> getEquates(long value);
/**
* Returns an iterator over all equates.
*/
/**
* Returns an address iterator over all the addresses where
* equates have been set.
* @return the iterator
*/
public AddressIterator getEquateAddresses();
/**
* Returns all equates defined for value.
* @param value the value to get all equates for.
* @return the equates
*/
public List<Equate> getEquates(long value);
/**
* Returns an iterator over all equates.
* @return the iterator
*/
public Iterator<Equate> getEquates();
/**
* Return an address iterator over each address with an
* equate reference starting at the start address.
*
* @param start start address
* @return an AddressIterator over addresses with defined equate references
*/
public AddressIterator getEquateAddresses(Address start);
/**
* Return an address iterator over each address with an
* equate reference starting at the start address.
*
* @param start start address
* @return an AddressIterator over addresses with defined equate references
*/
public AddressIterator getEquateAddresses(Address start);
/**
* Return an address iterator over each address with an
* equate reference that is in the specified address set.
*
* @param asv the address set
* @return AddressIterator over addresses with defined equate references
*/
public AddressIterator getEquateAddresses(AddressSetView asv);
/**
* Return an address iterator over each address with an
* equate reference that is in the specified address set.
*
* @param asv the address set
* @return AddressIterator over addresses with defined equate references
*/
public AddressIterator getEquateAddresses(AddressSetView asv);
}