Java Code Examples for ghidra.program.model.listing.Program#getListing()

The following examples show how to use ghidra.program.model.listing.Program#getListing() . 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: ProgramXmlMgr.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createDefaultTree(Program program, XmlProgramOptions options) {

		if (options.isAddToProgram()) {
			return;
		}

		Listing listing = program.getListing();
		if (listing.getTreeNames().length == 0) {
			try {
				listing.createRootModule(TreeManager.DEFAULT_TREE_NAME);
			}
			catch (DuplicateNameException e) {
				// shouldn't happen since we checked the tree names above
				Msg.debug(this, "Unable to create default module", e);
			}
		}

	}
 
Example 2
Source File: AddSingleReferenceInSwitchTable.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
  public void run() throws Exception {
  	
  	Program program = currentProgram;
  	Listing listing = program.getListing(); 
  	
  	// Ask for base address 
  	//  (equals the pc when program hits the switch table, 
  	//   which equals the address of the "add pc, .." instruction + 4)
  	Address pc = askAddress("Address", "Enter switch base address (hex, don't use 0x)");

  	// Get current data value
  	Data data = listing.getDefinedDataAt(currentAddress);
  	long currVal = NumericUtilities.parseHexLong(data.getValue().toString().substring(2));
  	
// Calculate referenced addr
Address refAddr = pc.add(2 * currVal);
	
// Add reference
println("Adding ref " + refAddr.toString() + " to address " + data.getAddressString(false, true));
data.addValueReference(refAddr, RefType.DATA);

  }
 
Example 3
Source File: AnalyzerBase.java    From gotools with MIT License 5 votes vote down vote up
/**
 * Creates a new defined Data object at the given address.
 *
 * @param address  the address at which to create a new Data object.
 * @param datatype the Data Type that describes the type of Data object to
 *                 create.
 * @return the newly created Data object
 */
final Data createData(Program p, Address address, DataType datatype) throws Exception {
  Listing listing = p.getListing();
  Data d = listing.getDefinedDataAt(address);
  if (d != null) {
    if (d.getDataType().isEquivalent(datatype)) {
      return d;
    }
    throw new CodeUnitInsertionException("Data conflict at address " + address);
  }
  return listing.createData(address, datatype);
}
 
Example 4
Source File: MultEntSubModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a <CODE>MultEntSubModel</CODE> for a program.
 * @param program program to create blocks from.
 * @param includeExternals external blocks will be included if true
 */
public MultEntSubModel(Program program, boolean includeExternals) {
	this.program = program;
	this.includeExternals = includeExternals;
	listing = program.getListing();
	foundMSubs = new AddressObjectMap();
}
 
Example 5
Source File: OverlapCodeSubModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a <CODE>OverlapCodeSubModel</CODE> subroutine on a program.
 * @param program program to create blocks from.
 * @param includeExternals external blocks will be included if true
 */
public OverlapCodeSubModel(Program program, boolean includeExternals) {
    this.program = program;
    listing = program.getListing();
    foundOSubs = new CodeBlockCache();
    modelM = new MultEntSubModel(program, includeExternals);
}
 
Example 6
Source File: FunctionBodyFunctionExtentGenerator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public List<CodeUnit> calculateExtent(Function func) {
	ArrayList<CodeUnit> units = new ArrayList<CodeUnit>();

	final AddressSetView body = func.getBody();
	final Program program = func.getProgram();
	final Listing listing = program.getListing();

	InstructionIterator codeUnitIterator = listing.getInstructions(body, true);
	for (Instruction codeUnit : codeUnitIterator) {
		units.add(codeUnit);
	}

	return units;
}
 
Example 7
Source File: AddressFormatModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Set the program.  This formatter is dependent upon listing from program.
 * There are two cases where this depedency only appears.  All Formatters that
 * are added as a view to Memory Viewer are created via a Factory within their
 * respective Formatter Plugin.
 */
public void setProgram(Program program) {
	if (program == null) {
		listing = null;
		memory = null;
	}
	else {
		listing = program.getListing();
		memory = program.getMemory();
	}
}
 
Example 8
Source File: ExternalEntryFunctionAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Called when a function has been added.
 * Looks at address for call reference
 */
public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log) {

	Listing listing = program.getListing();

	AddressSet funcStarts = new AddressSet();

	monitor.setMessage("Finding External Entry Functions");
	//
	// add entry points
	//
	AddressIterator entryIter = program.getSymbolTable().getExternalEntryPointIterator();
	while (entryIter.hasNext() && !monitor.isCancelled()) {
		Address entry = entryIter.next();
		if (set.contains(entry) && listing.getInstructionAt(entry) != null) {
			funcStarts.addRange(entry, entry);
		}
	}

	// remove any addresses that are already functions
	SymbolIterator iter =
		program.getSymbolTable().getSymbols(funcStarts, SymbolType.FUNCTION, true);
	AddressSet alreadyFunctionSet = new AddressSet();
	while (iter.hasNext() && !monitor.isCancelled()) {
		Symbol element = iter.next();
		alreadyFunctionSet.addRange(element.getAddress(), element.getAddress());
	}
	funcStarts.delete(alreadyFunctionSet);

	if (monitor.isCancelled()) {
		return false;
	}
	AutoAnalysisManager amgr = AutoAnalysisManager.getAnalysisManager(program);
	amgr.createFunction(funcStarts, false);

	return true;
}
 
Example 9
Source File: RTTI1DataType.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid(Program program, Address startAddress,
		DataValidationOptions validationOptions) {

	Memory memory = program.getMemory();
	Listing listing = program.getListing();

	if (!memory.contains(startAddress)) {
		return false;
	}

	Address endAddress = startAddress.add(LENGTH - 1);
	try {
		MSDataTypeUtils.getBytes(memory, startAddress, LENGTH);
	}
	catch (InvalidDataTypeException e) {
		return false; // Couldn't get enough bytes from memory for an RTTI1.
	}

	if (!validationOptions.shouldIgnoreInstructions() &&
		containsInstruction(listing, startAddress, endAddress)) {
		return false;
	}

	if (!validationOptions.shouldIgnoreDefinedData() &&
		containsDefinedData(listing, startAddress, endAddress)) {
		return false;
	}

	// RTTI1 should start on 4 byte boundary.
	if (startAddress.getOffset() % 4 != 0) {
		return false;
	}

	return validateComponents(program, startAddress, validationOptions);
}
 
Example 10
Source File: RenameTreeCmd.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @see ghidra.framework.cmd.Command#applyTo(ghidra.framework.model.DomainObject)
 */
public boolean applyTo(DomainObject obj) {
	program = (Program)obj;
	Listing listing = program.getListing();
	try {
		listing.renameTree(oldName, newName);
		return true;
	} catch (DuplicateNameException e) {
		statusMsg = e.getMessage();
	}
	return false;
}
 
Example 11
Source File: MakeFuncsAtLabelsScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {

	// find all the sections of memory marked as executable
	Program prog = currentProgram;
	AddressSetView execMemSet = prog.getMemory().getExecuteSet();
	SymbolTable sm = currentProgram.getSymbolTable();
	SymbolIterator textLabels = sm.getPrimarySymbolIterator(execMemSet, true);
	Listing listing = prog.getListing();
	for (Symbol symbol : textLabels) {
		if (symbol.getSource() == SourceType.IMPORTED &&
			(symbol.getSymbolType() == SymbolType.LABEL)) {
			if (!this.isRunningHeadless()) {
				printf("%s %s", symbol.getAddress().toString(), symbol.toString());
			}
			Address labelAddress = symbol.getAddress();
			//don't declare to be functions if the symbol starts with .L or LAB
			if (symbol.toString().startsWith(".L") || symbol.toString().startsWith("LAB")) {
				continue;
			}
			if (listing.isUndefined(labelAddress, labelAddress)) {
				if (!this.isRunningHeadless()) {
					printf("Undefined: %s", labelAddress.toString());
				}
				boolean result = disassemble(labelAddress);
				if (result == false) {
					printf("Disassembly failure at %s", labelAddress.toString());
					continue; //must be data
				}
			}
			createFunction(labelAddress, symbol.toString());
		}
	}
}
 
Example 12
Source File: CommentTypeFilterAddressIterator.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a new CommentTypeFilterAddressIterator
 * @param it an address iterator whose items are tested for the comment type.
 * @param commentType the type of comment to search for.
 */
public CommentTypeFilterAddressIterator(Program program, AddressIterator it, int commentType) {
	this.listing = program.getListing();
	this.it = it;
	this.commentType = commentType;
}
 
Example 13
Source File: RTTI3DataType.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValid(Program program, Address startAddress,
		DataValidationOptions validationOptions) {

	Memory memory = program.getMemory();
	if (!memory.contains(startAddress)) {
		return false;
	}

	// RTTI4 should start on a 4 byte boundary.
	if (startAddress.getOffset() % 4 != 0) {
		return false;
	}

	Listing listing = program.getListing();
	Address endAddress = startAddress.add(LENGTH - 1);
	try {
		MSDataTypeUtils.getBytes(memory, startAddress, LENGTH);
	}
	catch (InvalidDataTypeException e) {
		return false; // Couldn't get enough bytes from memory for an RTTI3.
	}

	if (!validationOptions.shouldIgnoreInstructions() &&
		containsInstruction(listing, startAddress, endAddress)) {
		return false;
	}

	if (!validationOptions.shouldIgnoreDefinedData() &&
		containsDefinedData(listing, startAddress, endAddress)) {
		return false;
	}

	// First 8 bytes is 2 dword numeric values.

	// Next four bytes after 2 dwords should be number of RTTI1 pointers in RTTI2.
	long rtti1Count = getRtti1Count(memory, startAddress);
	if (rtti1Count < 0 || rtti1Count > MAX_RTTI_1_COUNT) { // For now assume we shouldn't be seeing more than 1000 pointers in RTTI2.
		return false;
	}

	boolean validateReferredToData = validationOptions.shouldValidateReferredToData();

	// Last component should refer to RTTI2.
	Address rtti2Address = getRtti2Address(memory, startAddress);
	RTTI2DataType rtti2 = new RTTI2DataType(rtti1Count, program.getDataTypeManager());
	if (rtti2Address == null ||
		(validateReferredToData && !rtti2.isValid(program, rtti2Address, validationOptions))) {
		return false;
	}

	return true;
}
 
Example 14
Source File: RTTI2DataType.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValid(Program program, Address startAddress,
		DataValidationOptions validationOptions) {

	Memory memory = program.getMemory();
	Listing listing = program.getListing();

	if (!memory.contains(startAddress)) {
		return false;
	}

	// Each entry is a 4 byte value.
	long numEntries = (rtti1Count != 0) ? rtti1Count
			: getNumEntries(program, startAddress, validationOptions);
	if (numEntries == 0) {
		return false;
	}
	long length = numEntries * ENTRY_SIZE;
	Address endAddress = startAddress.add(length - 1);
	if (!validRefData(memory, startAddress)) {
		return false;
	}

	if (!validationOptions.shouldIgnoreInstructions() &&
		containsInstruction(listing, startAddress, endAddress)) {
		return false;
	}

	if (!validationOptions.shouldIgnoreDefinedData() &&
		containsDefinedData(listing, startAddress, endAddress)) {
		return false;
	}

	boolean validateReferredToData = validationOptions.shouldValidateReferredToData();

	Address addr = startAddress;
	for (int ordinal = 0; ordinal < numEntries && addr != null &&
		validRefData(memory, addr); ordinal++) {

		// Each component is either a direct reference or an image base offset.
		Address rtti1Address = getReferencedAddress(program, addr);
		if (rtti1Address == null || (validateReferredToData &&
			!rtti1.isValid(program, rtti1Address, validationOptions))) {
			return false;
		}

		try {
			addr = addr.add(ENTRY_SIZE); // Add the data type size.
		}
		catch (AddressOutOfBoundsException e) {
			if (ordinal < (rtti1Count - 1)) {
				return false; // Didn't get all the entries.
			}
			break;
		}
	}

	return true;
}
 
Example 15
Source File: RTTI4DataType.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValid(Program program, Address startAddress,
		DataValidationOptions validationOptions) {

	Memory memory = program.getMemory();
	if (!memory.contains(startAddress)) {
		return false;
	}

	// RTTI4 should start on a 4 byte boundary.
	if (startAddress.getOffset() % 4 != 0) {
		return false;
	}

	Listing listing = program.getListing();
	Address endAddress = startAddress.add(LENGTH - 1);
	try {
		MSDataTypeUtils.getBytes(memory, startAddress, LENGTH);
	}
	catch (InvalidDataTypeException e) {
		return false; // Couldn't get enough bytes from memory for an RTTI4.
	}

	if (!validationOptions.shouldIgnoreInstructions() &&
		containsInstruction(listing, startAddress, endAddress)) {
		return false;
	}

	if (!validationOptions.shouldIgnoreDefinedData() &&
		containsDefinedData(listing, startAddress, endAddress)) {
		return false;
	}

	// First 12 bytes is 3 dword numeric values.

	boolean validateReferredToData = validationOptions.shouldValidateReferredToData();

	// Fourth component should refer to RTTI0.
	Address rtti0CompAddress = startAddress.add(RTTI_0_OFFSET);
	Address rtti0Address = getReferencedAddress(program, rtti0CompAddress);
	if (rtti0Address == null ||
		(validateReferredToData && !rtti0.isValid(program, rtti0Address, validationOptions))) {
		return false;
	}

	// Last component should refer to RTTI3.
	Address rtti3CompAddress = startAddress.add(RTTI_3_OFFSET);
	Address rtti3Address = getReferencedAddress(program, rtti3CompAddress);
	if (rtti3Address == null ||
		(validateReferredToData && !rtti3.isValid(program, rtti3Address, validationOptions))) {
		return false;
	}

	return true;
}