Java Code Examples for ghidra.program.model.address.Address#getNewAddress()

The following examples show how to use ghidra.program.model.address.Address#getNewAddress() . 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: DynamicSymbolTableCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void markupTOC(MachHeader header, FlatProgramAPI api, Address baseAddress,
		ProgramModule parentModule, TaskMonitor monitor) throws Exception {
	if (getTableOfContentsSize() == 0) {
		return;
	}
	Address tocStartAddr = baseAddress.getNewAddress(getTableOfContentsOffset());
	long offset = 0;
	for (TableOfContents toc : tocList) {
		if (monitor.isCancelled()) {
			return;
		}
		Address tocAddr = tocStartAddr.add(offset);
		api.setPlateComment(tocAddr,
			"Module: " + moduleList.get(toc.getModuleIndex()).getModuleName() + '\n' +
				"Symbol: " + header.getFirstLoadCommand(SymbolTableCommand.class).getSymbolAt(
					toc.getSymbolIndex()).getString());
		DataType tocDT = toc.toDataType();
		api.createData(tocAddr, tocDT);
		offset += tocDT.getLength();
	}
	api.createFragment(parentModule, "TOC", tocStartAddr, offset);
}
 
Example 2
Source File: RunPathCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
			int length = getCommandSize() - path.getOffset();
			api.createAsciiString(address.add(path.getOffset()), length);
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName());
	}
}
 
Example 3
Source File: DynamicSymbolTableCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void markupExternalRelocations(FlatProgramAPI api, Address baseAddress,
		ProgramModule parentModule, TaskMonitor monitor) throws Exception {
	if (getExternalRelocationSize() == 0) {
		return;
	}
	Address relocStartAddr = baseAddress.getNewAddress(getExternalRelocationOffset());
	long offset = 0;
	for (RelocationInfo reloc : externalRelocations) {
		if (monitor.isCancelled()) {
			return;
		}
		DataType relocDT = reloc.toDataType();
		Address relocAddr = relocStartAddr.add(offset);
		api.createData(relocAddr, relocDT);
		api.setPlateComment(relocAddr, reloc.toString());
		offset += relocDT.getLength();
	}
	api.createFragment(parentModule, "EXTERNAL_RELOCATIONS", relocStartAddr, offset);
}
 
Example 4
Source File: DynamicLinkerCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
			int length = getCommandSize() - name.getOffset();
			api.createAsciiString(address.add(name.getOffset()), length);
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName());
	}
}
 
Example 5
Source File: BuildVersionCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {

	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName());
	}
}
 
Example 6
Source File: EntryPointCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {

	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName());
	}

}
 
Example 7
Source File: GenericRefernenceBaseRelocationFixupHandler.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private boolean handleGenerically64(Program program, Relocation relocation,
		Address oldImageBase, Address newImageBase) throws MemoryAccessException,
		CodeUnitInsertionException {
	long diff = newImageBase.subtract(oldImageBase);

	Address address = relocation.getAddress();
	Memory memory = program.getMemory();
	long value = memory.getLong(address);
	long newValue = value + diff;

	Address candiateRelocationValue = newImageBase.getNewAddress(newValue);
	if (hasMatchingReference(program, address, candiateRelocationValue)) {
		return process64BitRelocation(program, relocation, oldImageBase, newImageBase);
	}

	return false;
}
 
Example 8
Source File: GenericRefernenceBaseRelocationFixupHandler.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private boolean handleGenerically32(Program program, Relocation relocation,
		Address oldImageBase, Address newImageBase) throws MemoryAccessException,
		CodeUnitInsertionException {

	long diff = newImageBase.subtract(oldImageBase);

	Address address = relocation.getAddress();
	Memory memory = program.getMemory();
	long value = memory.getInt(address) & 0xffffffff;
	int newValue = (int) (value + diff);
	Address candiateRelocationValue = newImageBase.getNewAddress(newValue);
	if (hasMatchingReference(program, address, candiateRelocationValue)) {
		return process32BitRelocation(program, relocation, oldImageBase, newImageBase);
	}
	return false;
}
 
Example 9
Source File: SourceVersionCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {

	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName());
	}

}
 
Example 10
Source File: DynamicSymbolTableCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void makupIndirectSymbolTable(MachHeader header, FlatProgramAPI api,
		Address baseAddress, ProgramModule parentModule, TaskMonitor monitor) throws Exception {
	int SIZEOF_DWORD = 4;
	if (getIndirectSymbolTableSize() == 0) {
		return;
	}
	Address start = baseAddress.getNewAddress(getIndirectSymbolTableOffset());
	long length = getIndirectSymbolTableSize() * SIZEOF_DWORD;

	api.createFragment(parentModule, "INDIRECT_SYMBOLS", start, length);

	for (int i = 0; i < indirectSymbols.length; ++i) {
		if (monitor.isCancelled()) {
			return;
		}
		Address addr = start.add(i * SIZEOF_DWORD);
		NList symbol = header.getFirstLoadCommand(SymbolTableCommand.class).getSymbolAt(
			indirectSymbols[i]);
		if (symbol != null) {
			api.setEOLComment(addr, symbol.getString());
		}
	}

	api.createDwords(start, getIndirectSymbolTableSize());
}
 
Example 11
Source File: PreboundDynamicLibraryCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address addr = baseAddress.getNewAddress(getStartIndex());
			api.createData(addr, toDataType());

			int nameLen = getCommandSize() - name.getOffset();
			Address nameAddr = addr.add(name.getOffset());
			api.createAsciiString(nameAddr, nameLen);
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName() + " - " + e.getMessage());
	}
}
 
Example 12
Source File: UnsupportedLoadCommand.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	if (isBinary) {
		try {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
		}
		catch (Exception e) {
			log.appendMsg("Unable to create " + getCommandName() + " - " + e.getMessage());
		}
	}
}
 
Example 13
Source File: PrebindChecksumCommand.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address addr = baseAddress.getNewAddress(getStartIndex());
			api.createData(addr, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName() + " - " + e.getMessage());
	}
}
 
Example 14
Source File: ThreadCommand.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address addr = baseAddress.getNewAddress(getStartIndex());
			api.createData(addr, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName() + " - " + e.getMessage());
	}
}
 
Example 15
Source File: VariableImpl.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Varnode resizeStackVarnode(Varnode varnode, int newVarnodeSize,
		VariableStorage curStorage, int newSize, DataType type) throws InvalidInputException {

	Address curAddr = varnode.getAddress();
	int stackOffset = (int) curAddr.getOffset();
	int newStackOffset = stackOffset;

	int newEndStackOffset = newStackOffset + newVarnodeSize - 1;
	if (newStackOffset < 0 && newEndStackOffset >= 0) {
		throw new InvalidInputException(
			"Data type does not fit within variable stack constraints");
	}

	return new Varnode(curAddr.getNewAddress(newStackOffset), newVarnodeSize);
}
 
Example 16
Source File: DynamicSymbolTableCommand.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void markupModules(MachHeader header, FlatProgramAPI api, Address baseAddress,
		ProgramModule parentModule, TaskMonitor monitor) throws Exception {
	if (getModuleTableSize() == 0) {
		return;
	}
	SymbolTableCommand symtabCommand = header.getFirstLoadCommand(SymbolTableCommand.class);
	Address moduleStartAddr = baseAddress.getNewAddress(getModuleTableOffset());
	long offset = 0;
	int id = 0;
	for (DynamicLibraryModule module : moduleList) {
		if (monitor.isCancelled()) {
			return;
		}
		DataType moduleDT = module.toDataType();
		Address moduleAddr = moduleStartAddr.add(offset);
		Data moduleData = api.createData(moduleAddr, moduleDT);

		Address stringAddr = baseAddress.getNewAddress(
			symtabCommand.getStringTableOffset() + module.getModuleNameIndex());

		api.createMemoryReference(moduleData, stringAddr, RefType.DATA);
		api.createAsciiString(stringAddr);
		api.setPlateComment(moduleAddr,
			"0x" + Integer.toHexString(id++) + " - " + module.getModuleName());

		offset += moduleDT.getLength();
	}
	api.createFragment(parentModule, "MODULES", moduleStartAddr, offset);
}
 
Example 17
Source File: PseudoDisassembler.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * In order to check a location to see if it disassembles from an address reference, the
 * address is checked for low-bit code switch behavior.  If it does switch, the context
 * is changed.
 * 
 * @param procContext context to change
 * @param addr destination address that will be disassembled (possible pseudo disassembled)
 * @return the correct disassembly location if the address needed to be adjusted.
 */

public Address setTargeContextForDisassembly(PseudoDisassemblerContext procContext,
		Address addr) {
	Register lowBitCodeMode = program.getRegister(LOW_BIT_CODE_MODE_REGISTER_NAME);
	if (lowBitCodeMode == null) {
		return addr;
	}
	long offset = addr.getOffset();
	if ((offset & 1) == 1) {
		addr = addr.getNewAddress(addr.getOffset() & ~0x1);
		procContext.setValue(lowBitCodeMode, addr, BigInteger.ONE);
	}
	return addr.getNewAddress(addr.getOffset() & ~0x1);
}
 
Example 18
Source File: HCS12X_ElfExtension.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Address evaluateElfSymbol(ElfLoadHelper elfLoadHelper, ElfSymbol elfSymbol,
		Address address, boolean isExternal) {

	if (isExternal) {
		return address;
	}

	String symName = elfSymbol.getNameAsString();

	long laddr = address.getOffset();

	laddr = hcs12TranslatePagedAddress(laddr);

	Address mappedAddr = address.getNewAddress(laddr);

	return mappedAddr;
}
 
Example 19
Source File: StringParameterPropagator.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Analyze a functions references
 * @param constUse 
 */
public void analyzeFunction(HashMap<Address, FuncInfo> constUse,
		DecompInterface decompInterface, Program prog, Function f,
		HashSet<Address> stringLocationSet) {
	if (f == null) {
		return;
	}

	if (!decompileFunction(f, decompInterface)) {
		return;
	}
	Address entry = f.getEntryPoint();

	Iterator<PcodeOpAST> ops = hfunction.getPcodeOps();
	while (ops.hasNext() && !monitor.isCancelled()) {
		PcodeOpAST pcodeOpAST = ops.next();
		// System.out.println(pcodeOpAST);
		if (pcodeOpAST.getOpcode() != PcodeOp.CALL) {
			continue;
		}
		Varnode calledFunc = pcodeOpAST.getInput(0);

		if (calledFunc == null || !calledFunc.isAddress()) {
			continue;
		}
		Address calledFuncAddr = calledFunc.getAddress();

		// rifle through parameters
		int numParams = pcodeOpAST.getNumInputs();
		for (int i = 1; i < numParams; i++) {
			Varnode parm = pcodeOpAST.getInput(i);  // 1st param is the call dest
			if (parm == null) {
				continue;
			}

			// follow back to a const if possible
			ArrayList<PcodeOp> localDefUseList = new ArrayList<PcodeOp>();

			// check out the constUse list to see if we fished out a constant.  Don't follow out of function
			// see if it is a constant
			if (parm.isConstant()) {
				// then this is a resource id
				// lookup the resource and create a reference
				long value = parm.getOffset();
				// TODO: not so fast, if there is a defUseList, must apply it to get the real constant USED!
				try {
					value = applyDefUseList(value, localDefUseList);
					// constUse.put(calledFuncAddr, i);
				}
				catch (InvalidInputException exc) {
					// Do nothing
				}

				long mask =
					0xffffffffffffffffL >>> ((8 - entry.getAddressSpace().getPointerSize()) * 8);
				Address possibleAddr = entry.getNewAddress(mask & value);
				if (stringLocationSet.contains(possibleAddr)) {
					markStringParam(constUse, possibleAddr, calledFuncAddr, i - 1,
						numParams - 1);
				}
			}
			if (parm.isAddress()) {
				if (stringLocationSet.contains(parm.getAddress())) {
					markStringParam(constUse, parm.getAddress(), calledFuncAddr, i - 1,
						numParams - 1);
				}
			}
		}
	}
}
 
Example 20
Source File: SymbolTableCommand.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	if (isBinary) {
		try {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());

			if (getStringTableSize() > 0) {
				Address stringTableStart = baseAddress.getNewAddress(getStringTableOffset());
				api.createFragment(parentModule, "string_table", stringTableStart,
					getStringTableSize());
			}

			int symbolIndex = 0;
			Address symbolStartAddr = baseAddress.getNewAddress(getSymbolOffset());
			long offset = 0;
			for (NList symbol : symbols) {
				if (monitor.isCancelled()) {
					return;
				}

				DataType symbolDT = symbol.toDataType();
				Address symbolAddr = symbolStartAddr.add(offset);
				Data symbolData = api.createData(symbolAddr, symbolDT);

				Address stringAddress = baseAddress.getNewAddress(
					getStringTableOffset() + symbol.getStringTableIndex());
				Data stringData = api.createAsciiString(stringAddress);
				String string = (String) stringData.getValue();

				Reference ref =
					api.createMemoryReference(symbolData, stringAddress, RefType.DATA);
				api.setReferencePrimary(ref, false);

				api.setPlateComment(symbolAddr,
					string + "\n" + "Index:           0x" + Integer.toHexString(symbolIndex) +
						"\n" + "Value:           0x" + Long.toHexString(symbol.getValue()) +
						"\n" + "Description:     0x" +
						Long.toHexString(symbol.getDescription() & 0xffff) + "\n" +
						"Library Ordinal: 0x" +
						Long.toHexString(symbol.getLibraryOrdinal() & 0xff));

				offset += symbolDT.getLength();
				++symbolIndex;
			}

			if (getNumberOfSymbols() > 0) {
				api.createFragment(parentModule, "symbols", symbolStartAddr, offset);
			}
		}
		catch (Exception e) {
			log.appendMsg("Unable to create " + getCommandName() + " - " + e.getMessage());
		}
	}
}