Java Code Examples for ghidra.program.model.data.DataType#DEFAULT

The following examples show how to use ghidra.program.model.data.DataType#DEFAULT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VariableSymbolDB.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public DataType getDataType() {
	DataType dt = symbolMgr.getDataType(getSymbolData1());
	if (dt == null) {
		VariableStorage storage = getVariableStorage();
		if (storage == null) {
			dt = DataType.DEFAULT;
		}
		else if (storage.isVoidStorage()) {
			dt = DataType.VOID;
		}
		else {
			dt = Undefined.getUndefinedDataType(storage.size());
		}
	}
	return dt;
}
 
Example 2
Source File: PreviewTableCellData.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private String getCodeUnitPreview(CodeUnitFormat format) {
	Address addr = location.getAddress();
	if (addr.isExternalAddress()) {
		Symbol s = program.getSymbolTable().getPrimarySymbol(addr);
		if (s != null) {
			ExternalLocation extLoc = program.getExternalManager().getExternalLocation(s);
			DataType dt = extLoc.getDataType();
			if (dt == null) {
				dt = DataType.DEFAULT;
			}
			return dt.getMnemonic(dt.getDefaultSettings());
		}
	}

	CodeUnit cu = program.getListing().getCodeUnitAt(addr);
	return getFormatedCodeUnitPreview(cu);
}
 
Example 3
Source File: ExternalFunctionMerger.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private ExternalLocation replaceFunction(ExternalLocation toExternalLocation,
		ExternalLocation fromExternalLocation, ProgramMerge programMerge, TaskMonitor monitor)
		throws CancelledException, UnsupportedOperationException {

	Function fromFunction = fromExternalLocation.getFunction();
	Function toFunction = toExternalLocation.getFunction();
	if (fromFunction == null) {
		if (toFunction == null) {
			return toExternalLocation;
		}
		DataType dataType = getResultDataType(toExternalLocation);
		Namespace namespace = toExternalLocation.getParentNameSpace();
		String label = toExternalLocation.getLabel();
		// Need to remove the function.
		Symbol toSymbol = toExternalLocation.getSymbol();
		Address addr = toSymbol.getAddress();
		// The location is no longer valid so get the location and restore the data type.
		ExternalManager externalManager = programMerge.getResultProgram().getExternalManager();
		toSymbol.delete(); // This should remove the function and it becomes a label.
		SymbolTable symbolTable = programMerge.getResultProgram().getSymbolTable();
		Symbol symbol = symbolTable.getSymbol(label, addr, namespace);
		ExternalLocation newLocation = externalManager.getExternalLocation(symbol);
		if (newLocation != null && dataType != null && dataType != DataType.DEFAULT) {
			newLocation.setDataType(dataType); // Restore the datatype
		}
		return newLocation;
	}
	if (toFunction == null) {
		toFunction = toExternalLocation.createFunction();
	}
	if (ProgramDiff.equivalentFunctions(fromFunction, toFunction)) {
		return toExternalLocation;
	}
	programMerge.replaceExternalFunction(toFunction, fromFunction, monitor);
	return toFunction.getExternalLocation();
}
 
Example 4
Source File: ClipboardPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Program createDefaultProgram() throws Exception {
	ProgramBuilder builder = new ProgramBuilder("default", ProgramBuilder._TOY, this);

	builder.createMemory("test", "0x01001050", 20000);

	builder.setBytes("0x01001050",
		"0e 5e f4 77 33 58 f4 77 91 45 f4 77 88 7c f4 77 8d 70 f5 77 05 62 f4 77 f0 a3 " +
			"f4 77 09 56 f4 77 10 17 f4 77 f7 29 f6 77 02 59 f4 77");

	builder.createMemoryReference("0x01002cc0", "0x01002cf0", RefType.DATA,
		SourceType.USER_DEFINED);
	builder.createMemoryReference("0x01002d04", "0x01002d0f", RefType.DATA,
		SourceType.USER_DEFINED);

	DataType dt = DataType.DEFAULT;
	Parameter p = new ParameterImpl(null, dt, builder.getProgram());
	builder.createEmptyFunction("ghidra", "0x01002cf5", 1, dt, p);
	builder.createEmptyFunction("sscanf", "0x0100415a", 1, dt, p);

	builder.createComment("0x0100415a",
		"\n;|||||||||||||||||||| FUNCTION ||||||||||||||||||||||||||||||||||||||||||||||||||\n ",
		CodeUnit.PLATE_COMMENT);

	builder.setBytes("0x0100418c", "ff 15 08 10 00 01");
	builder.disassemble("0x0100418c", 6);

	return builder.getProgram();
}
 
Example 5
Source File: ExternalFunctionMerger.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private DataType getResultDataType(DataType fromDataType) {
	if (fromDataType == null) {
		return null;
	}
	if (fromDataType == DataType.DEFAULT) {
		return fromDataType;
	}
	DataTypeManager latestDTM = programs[LATEST].getDataTypeManager();
	DataTypeManager myDTM = programs[MY].getDataTypeManager();
	DataTypeManager originalDTM = programs[ORIGINAL].getDataTypeManager();
	DataTypeManager fromDataTypeManager = fromDataType.getDataTypeManager();
	if (fromDataTypeManager == latestDTM) {
		long latestID = latestDTM.getID(fromDataType);
		DataType latestResultDT = getResultDataType(latestID, programs[LATEST]);
		if (latestResultDT != null) {
			return latestResultDT;
		}
	}
	if (fromDataTypeManager == myDTM) {
		long myID = myDTM.getID(fromDataType);
		DataType myResultDT = getResultDataType(myID, programs[MY]);
		if (myResultDT != null) {
			return myResultDT;
		}
	}
	if (fromDataTypeManager == originalDTM) {
		long originalID = originalDTM.getID(fromDataType);
		DataType originalResultDT = getResultDataType(originalID, programs[ORIGINAL]);
		if (originalResultDT != null) {
			return originalResultDT;
		}
	}
	return fromDataType;
}
 
Example 6
Source File: PreviewTableCellData.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getAddressPreview() {
	if (address.isExternalAddress()) {
		Symbol s = program.getSymbolTable().getPrimarySymbol(address);
		if (s != null) {
			ExternalLocation loc = program.getExternalManager().getExternalLocation(s);
			DataType dt = loc.getDataType();
			if (dt == null) {
				dt = DataType.DEFAULT;
			}
			return dt.getMnemonic(dt.getDefaultSettings());
		}
	}

	return null;
}
 
Example 7
Source File: DataSettingsDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void applyCommonSettings() {

		// TODO: Use task since this could be big and slow

		InteriorSelection interiorSelection = selection.getInteriorSelection();
		if (interiorSelection == null) {
			CodeUnitIterator codeUnits = program.getListing().getCodeUnits(selection, true);
			while (codeUnits.hasNext()) {
				// TODO: check monitor
				CodeUnit cu = codeUnits.next();
				if ((cu instanceof Data) && ((Data) cu).isDefined()) {
					applySettingsToData((Data) cu);
				}
			}
			return;
		}

		int[] from = interiorSelection.getFrom().getComponentPath();
		int[] to = interiorSelection.getTo().getComponentPath();

		Data dataComp = DataPlugin.getDataUnit(program, selection.getMinAddress(), from);
		if (dataComp == null) {
			return;
		}
		Data parent = dataComp.getParent();
		int fromIndex = from[from.length - 1];
		int toIndex = to[to.length - 1];
		for (int i = fromIndex; i <= toIndex; i++) {
			dataComp = parent.getComponent(i);
			if (dataComp == null) {
				break;
			}
			DataType dt = dataComp.getDataType();
			if (dt == DataType.DEFAULT) {
				continue;
			}
			applySettingsToData(dataComp);
		}
	}
 
Example 8
Source File: HighExternalSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the external reference symbol given a name, the symbol Address, and a
 * resolving Address.
 * @param nm is the given name
 * @param addr is the symbol Address
 * @param resolveAddr is the resolve Address
 * @param dtmanage is a PcodeDataTypeManager for facilitating XML marshaling
 */
public HighExternalSymbol(String nm, Address addr, Address resolveAddr,
		PcodeDataTypeManager dtmanage) {
	super(0, nm, DataType.DEFAULT, true, true, dtmanage);
	resolveAddress = resolveAddr;
	VariableStorage store;
	try {
		store = new VariableStorage(getProgram(), addr, 1);
	}
	catch (InvalidInputException e) {
		store = VariableStorage.UNASSIGNED_STORAGE;
	}
	MappedEntry entry = new MappedEntry(this, store, null);
	addMapEntry(entry);
}
 
Example 9
Source File: CopyPasteFunctionInfoTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Program buildTaskman(String name) throws Exception {
	ToyProgramBuilder builder = new ToyProgramBuilder(name, true, ProgramBuilder._TOY);
	builder.createMemory("test1", "0x01001000", 0x8000);
	builder.createFunction("0x1006420");
	builder.createEntryPoint("0x1006420", "entry");
	builder.createFunction("0x1004700");
	builder.createComment("0x1006420", "FUNCTION", CodeUnit.PLATE_COMMENT);
	DataType dt = DataType.DEFAULT;
	Parameter p = new ParameterImpl(null, dt, builder.getProgram());
	builder.createEmptyFunction("BOB", "0x1004260", 1, dt, p, p, p, p, p, p, p, p, p, p, p, p,
		p);
	return builder.getProgram();
}
 
Example 10
Source File: EquateSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public EquateSymbol(long uniqueId, String nm, long val, HighFunction func, Address addr,
		long hash) {
	super(uniqueId, nm, DataType.DEFAULT, func);
	category = 1;
	value = val;
	convert = FORMAT_DEFAULT;
	DynamicEntry entry = new DynamicEntry(this, addr, hash);
	addMapEntry(entry);
}
 
Example 11
Source File: HighFunctionShellSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the function shell given a name and address
 * @param id is an id to associate with the new symbol
 * @param nm is the given name
 * @param addr is the given address
 * @param manage is PcodeDataTypeManager to facilitate XML marshaling
 */
public HighFunctionShellSymbol(long id, String nm, Address addr, PcodeDataTypeManager manage) {
	super(id, nm, DataType.DEFAULT, true, true, manage);
	VariableStorage store;
	try {
		store = new VariableStorage(getProgram(), addr, 1);
	}
	catch (InvalidInputException e) {
		store = VariableStorage.UNASSIGNED_STORAGE;
	}
	MappedEntry entry = new MappedEntry(this, store, null);
	addMapEntry(entry);
}
 
Example 12
Source File: HighLabelSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the label given a name and address
 * @param nm is the given name
 * @param addr is the given Address
 * @param dtmanage is a PcodeDataManager to facilitate XML marshaling
 */
public HighLabelSymbol(String nm, Address addr, PcodeDataTypeManager dtmanage) {
	super(0, nm, DataType.DEFAULT, true, true, dtmanage);
	VariableStorage store;
	try {
		store = new VariableStorage(getProgram(), addr, 1);
	}
	catch (InvalidInputException e) {
		store = VariableStorage.UNASSIGNED_STORAGE;
	}
	MappedEntry entry = new MappedEntry(this, store, null);
	addMapEntry(entry);
}
 
Example 13
Source File: GlobalSymbolMap.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Create a HighSymbol based on the id of the underlying Ghidra Symbol. The Symbol
 * is looked up in the SymbolTable and then a HighSymbol is created with the name and
 * dataType associated with the Symbol. If a Symbol cannot be found, null is returned.
 * @param id is the database id of the CodeSymbol
 * @param dataType is the recovered data-type of the symbol
 * @param sz is the size in bytes of the desired symbol
 * @return the CodeSymbol wrapped as a HighSymbol or null
 */
public HighSymbol populateSymbol(long id, DataType dataType, int sz) {
	if ((id >> 56) == (HighSymbol.ID_BASE >> 56)) {
		return null;		// This is an internal id, not a database key
	}
	Symbol symbol = symbolTable.getSymbol(id);
	if (symbol == null) {
		return null;
	}
	HighSymbol highSym = null;
	if (symbol instanceof CodeSymbol) {
		if (dataType == null) {
			Object dataObj = symbol.getObject();
			if (dataObj instanceof Data) {
				dataType = ((Data) dataObj).getDataType();
				sz = dataType.getLength();
			}
			else {
				dataType = DataType.DEFAULT;
				sz = 1;
			}
		}
		highSym = new HighCodeSymbol((CodeSymbol) symbol, dataType, sz, func);
	}
	else if (symbol instanceof FunctionSymbol) {
		highSym = new HighFunctionShellSymbol(id, symbol.getName(), symbol.getAddress(),
			func.getDataTypeManager());
	}
	else {
		return null;
	}
	insertSymbol(highSym, symbol.getAddress());
	return highSym;
}
 
Example 14
Source File: HighFunctionSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct given an Address, size, and decompiler function model for the symbol.
 * The Address is typically the entry point of the function but may be different
 * if the function is getting mapped from elsewhere (i.e. the EXTERNAL space). The size
 * is given in bytes but generally isn't the true size of the function. The size needs to
 * make the symbol just big enough to absorb any off-cut Address queries.
 * @param addr is the starting Address of the symbol
 * @param size is the pseudo-size of the function
 * @param function is the decompiler model of the function
 */
public HighFunctionSymbol(Address addr, int size, HighFunction function) {
	super(function.getID(), "", DataType.DEFAULT, function);
	VariableStorage store;
	try {
		store = new VariableStorage(getProgram(), addr, size);
	}
	catch (InvalidInputException e) {
		store = VariableStorage.UNASSIGNED_STORAGE;
	}
	MappedEntry entry = new MappedEntry(this, store, null);
	addMapEntry(entry);
}
 
Example 15
Source File: OldFunctionManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
DataType getDataType(long dataTypeId) {
	DataType dataType = dataManager.getDataType(dataTypeId);
	if (dataType == null || dataType.isDeleted()) {
		return DataType.DEFAULT;
	}
	if (dataType.getLength() > 0) {
		// Return variable - although it could be too big
		return dataType;
	}
	if (dataType instanceof Pointer) {
		return program.getDataTypeManager().getPointer(((Pointer) dataType).getDataType());
	}
	return program.getDataTypeManager().getPointer(dataType);
}
 
Example 16
Source File: OldFunctionDataDB.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @see ghidra.program.model.listing.Function#getReturnType()
 */
public synchronized DataType getReturnType() {
	long typeId = functionRecord.getLongValue(OldFunctionDBAdapter.RETURN_DATA_TYPE_ID_COL);
	DataType dt = functionManager.getDataType(typeId);
	if (dt == null) {
		dt = DataType.DEFAULT;
	}
	return dt;
}
 
Example 17
Source File: ExternalFunctionMerger.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
	 * Method called for merging an external that was added in LATEST and one that was added in MY.
	 * @param externalLocations RESULT, LATEST, and MY locations and ORIGINAL is null.
	 * @param monitor task monitor for progress and cancelling.
	 * @throws CancelledException if user cancels the merge.
	 */
	private void mergeLatestAndMyForAddConflict(ExternalLocation[] externalLocations,
			TaskMonitor monitor) throws CancelledException {
		// Merge LATEST and MY into the RESULT external.
		// This is a merge of LATEST and MY added externals, so every difference is a conflict.
		int latestMyChanges =
			getBasicExternalDiffs(externalLocations[LATEST], externalLocations[MY]);

		int detailConflictFlags =
			latestMyChanges & (EXTERNAL_NAMESPACE | EXTERNAL_LABEL | EXTERNAL_ADDRESS);
//			(EXTERNAL_NAMESPACE | EXTERNAL_LABEL | EXTERNAL_SOURCE_TYPE | EXTERNAL_ADDRESS);
		if ((detailConflictFlags & EXTERNAL_ADDRESS) != 0) {
			Address latestAddress = externalLocations[LATEST].getAddress();
			Address myAddress = externalLocations[MY].getAddress();
			if (latestAddress != null && myAddress == null) {
				// Want to set the external to LATEST address, which it already is, so nothing to do.
				detailConflictFlags &= ~EXTERNAL_ADDRESS; // Turn off the ADDRESS conflict flag.
			}
			else if (latestAddress == null && myAddress != null) {
				// Set the external to MY address.
				mergeExternalDetail(EXTERNAL_ADDRESS, externalLocations[RESULT],
					externalLocations[MY], monitor);
				detailConflictFlags &= ~EXTERNAL_ADDRESS; // Turn off the ADDRESS conflict flag.
			}
		}
		if (detailConflictFlags != 0) {
			saveExternalDetailConflict(externalLocations, detailConflictFlags);
		}

		// Check Data Type vs Function and for just data type differences.
		Address myExternalAddress = externalLocations[MY].getExternalSpaceAddress();

		DataType latestDataType = getResultDataType(externalLocations[LATEST]);
		DataType myDataType = getResultDataType(externalLocations[MY]);
		boolean latestHasDataType =
			(latestDataType != null) && (latestDataType != DataType.DEFAULT);
		boolean myHasDataType = (myDataType != null) && (myDataType != DataType.DEFAULT);
		boolean differentDataTypes =
			latestHasDataType && myHasDataType && (latestMyChanges & EXTERNAL_DATA_TYPE) != 0;
		boolean latestIsFunction = externalLocations[LATEST].isFunction();
		boolean myIsFunction = externalLocations[MY].isFunction();
		boolean differentFunctions =
			latestIsFunction && myIsFunction && (latestMyChanges & EXTERNAL_FUNCTION) != 0;
		// If the data types are different, then did a change to one conflict with a
		// data type or function change to the other?
		if (differentDataTypes) {
			// Conflict: Both set the data type differently.
			saveExternalDataTypeConflict(myExternalAddress);
		}
		if (differentFunctions) {
			// Conflict: Both created different functions.
			determineDetailedFunctionConflicts(externalLocations, monitor);
		}
		if ((latestIsFunction && !latestHasDataType && !myIsFunction && myHasDataType) ||
			(myIsFunction && !myHasDataType && !latestIsFunction && latestHasDataType)) {
			// Conflict: LATEST is function and MY is label with data type
			//   - or -  MY is function and LATEST is label with data type.
			saveExternalFunctionVersusDataTypeConflict(myExternalAddress);
		}
		else {
			if (myHasDataType && !latestHasDataType) {
				// Auto Merge My data type change.
				replaceExternalDataType(externalLocations[RESULT], externalLocations[MY], monitor);
			}
			if (myIsFunction && !latestIsFunction) {
				// Auto Merge MY external function changes.
				replaceFunction(externalLocations[RESULT], externalLocations[MY], getMergeMy(),
					monitor);
			}
		}
	}
 
Example 18
Source File: DataSettingsDialog.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void accumulateInteriorSettingsDefinitions(InteriorSelection interiorSelection,
		TaskMonitor monitor) {

	List<Class<? extends SettingsDefinition>> defClasses = null;
	List<SettingsDefinition> defs = null;

	int[] from = interiorSelection.getFrom().getComponentPath();
	int[] to = interiorSelection.getTo().getComponentPath();

	Data dataComp = DataPlugin.getDataUnit(program, selection.getMinAddress(), from);
	if (dataComp == null || from.length != to.length) {
		return;
	}
	Data parent = dataComp.getParent();
	int fromIndex = from[from.length - 1];
	int toIndex = to[to.length - 1];
	for (int i = fromIndex; i <= toIndex; i++) {
		dataComp = parent.getComponent(i);
		if (dataComp == null) {
			break;
		}
		DataType dt = dataComp.getDataType();
		if (dt == DataType.DEFAULT) {
			continue;
		}
		SettingsDefinition[] settingsDefinitions = dt.getSettingsDefinitions();
		if (settingsDefinitions.length == 0) {
			return;
		}
		if (defClasses == null) {
			defClasses = new ArrayList<>();
			defs = new ArrayList<>();
			for (SettingsDefinition def : settingsDefinitions) {
				defs.add(def);
				defClasses.add(def.getClass());
			}
		}
		else {
			removeMissingDefinitions(defClasses, defs, settingsDefinitions);
		}
	}
	defsArray = new SettingsDefinition[defs.size()];
	defs.toArray(defsArray);
}
 
Example 19
Source File: ExternalFunctionMerger.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void determineExternalDataTypeConflict(ExternalLocation[] externalLocations,
		int latestMyChanges, int originalLatestChanges, int originalMyChanges,
		TaskMonitor monitor) throws CancelledException {

	Address myExternalAddress = externalLocations[MY].getExternalSpaceAddress();

	DataType latestDataType = getResultDataType(externalLocations[LATEST]);
	DataType myDataType = getResultDataType(externalLocations[MY]);
	boolean latestHasDataType =
		(latestDataType != null) && (latestDataType != DataType.DEFAULT);
	boolean myHasDataType = (myDataType != null) && (myDataType != DataType.DEFAULT);
	boolean differentDataTypes = (latestMyChanges & EXTERNAL_DATA_TYPE) != 0;
	boolean latestChangedDataType = (originalLatestChanges & EXTERNAL_DATA_TYPE) != 0;
	boolean myChangedDataType = (originalMyChanges & EXTERNAL_DATA_TYPE) != 0;
	boolean latestIsFunction = externalLocations[LATEST].isFunction();
	boolean myIsFunction = externalLocations[MY].isFunction();
	boolean latestChangedFunction = (originalLatestChanges & EXTERNAL_FUNCTION) != 0;
	boolean myChangedFunction = (originalMyChanges & EXTERNAL_FUNCTION) != 0;
	// If the data types are different, then did a change to one conflict with a
	// data type or function change to the other?
	if (differentDataTypes) {
		if (myChangedDataType) {
			if (latestChangedDataType) {
				// Both changed the data type differently.
				saveExternalDataTypeConflict(myExternalAddress);
			}
			else if (myHasDataType && latestChangedFunction && latestIsFunction) {
				// MY Data Type conflicts with LATEST function.
				saveExternalFunctionVersusDataTypeConflict(myExternalAddress);
			}
			else {
				// Auto Merge My data type change.
				replaceExternalDataType(externalLocations[RESULT], externalLocations[MY],
					monitor);
			}
		}
		else if (latestChangedDataType) {
			if (myChangedDataType) {
				// Can't get here. This CONFLICT is handled by "if (myChangedDataType)" above.
				throw new AssertException("Shouldn't be here!");
			}
			else if (latestHasDataType && myChangedFunction && myIsFunction) {
				// LATEST data type conflicts with MY function.
				saveExternalFunctionVersusDataTypeConflict(myExternalAddress);
			}
			// Otherwise, RESULT should already have the LATEST data type.
		}
	}
}
 
Example 20
Source File: GlobalSymbolMap.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Create a HighSymbol corresponding to an underlying Data object. The name of the symbol is
 * generated dynamically. A symbol is always returned unless the address is invalid,
 * in which case null is returned.
 * @param id is the id to associate with the new symbol
 * @param addr is the address of the Data object
 * @param dataType is the recovered data-type of the symbol
 * @param sz is the size in bytes of the symbol
 * @return the new HighSymbol or null
 */
public HighCodeSymbol newSymbol(long id, Address addr, DataType dataType, int sz) {
	if (dataType == null) {
		dataType = DataType.DEFAULT;
		sz = 1;
	}
	HighCodeSymbol symbol = new HighCodeSymbol(id, addr, dataType, sz, func);
	insertSymbol(symbol, addr);
	return symbol;
}