Java源码示例:ghidra.program.model.address.AddressSpace

示例1
@Override
public PcodeOp[] getPcode(Program program, InjectContext con) {
	if (con.inputlist.size() != 2) {
		return null;			// Error
	}
	// The first Varnode must be a constant specifying the number of parameters
	int numParams = (int) con.inputlist.get(0).getOffset();
	// The second Varnode must be the first register to be moved
	long fromOffset = con.inputlist.get(1).getOffset();
	// Base of designated input registers
	long toOffset = InjectPayloadDexParameters.INPUT_REGISTER_START;
	AddressSpace registerSpace = program.getAddressFactory().getAddressSpace("register");
	PcodeOp[] resOps = new PcodeOp[numParams];
	for (int i = 0; i < numParams; ++i) {
		Address fromAddr = registerSpace.getAddress(fromOffset);
		Address toAddr = registerSpace.getAddress(toOffset);
		fromOffset += 4;
		toOffset += 4;
		PcodeOp op = new PcodeOp(con.baseAddr, i, PcodeOp.COPY);
		op.setInput(new Varnode(fromAddr, 4), 0);
		op.setOutput(new Varnode(toAddr, 4));
		resOps[i] = op;
	}
	return resOps;
}
 
示例2
private void formatMemoryInput(Program program, VarnodeTpl input0, VarnodeTpl input1,
		List<AttributedString> lineList) {
	if (!input0.getSpace().isConstSpace() || input0.getOffset().getType() != ConstTpl.REAL) {
		throw new RuntimeException("Expected constant input[0] for LOAD/STORE pcode op");
	}
	int id = (int) input0.getOffset().getReal();
	AddressSpace space = program.getAddressFactory().getAddressSpace(id);
	String spaceName;
	if (space == null) {
		Msg.error(PcodeFormatter.class, "Address space id not found: " + id);
		spaceName = "unknown";
	}
	else {
		spaceName = space.getName();
	}
	lineList.add(new AttributedString(spaceName, Color.BLUE, metrics));
	lineList.add(LEFT_PAREN);
	formatVarnodeTpl(program, -1, 1, input1, lineList);
	lineList.add(RIGHT_PAREN);
}
 
示例3
@Test
public void testCreateOverlayBlock2() throws Exception {

	model.setBlockName(".test");
	model.setStartAddress(getAddr(0x01001000));
	model.setLength(100);
	model.setBlockType(MemoryBlockType.DEFAULT);
	model.setOverlay(true);
	model.setInitializedType(InitializedType.INITIALIZED_FROM_VALUE);
	model.setInitialValue(0xa);
	assertTrue(model.execute());
	MemoryBlock block = null;
	AddressSpace[] spaces = program.getAddressFactory().getAddressSpaces();
	AddressSpace ovSpace = null;
	for (AddressSpace space : spaces) {
		if (space.isOverlaySpace()) {
			ovSpace = space;
			Address blockAddr = space.getAddress(0x1001000);
			block = program.getMemory().getBlock(blockAddr);
			break;
		}
	}
	assertNotNull(block);
	assertEquals((byte) 0xa, block.getByte(ovSpace.getAddress(0x1001000)));
}
 
示例4
private void updateLastAddress(Address startAddress) {
	if (startAddress == null) {
		return;
	}

	if (forward) {
		if (startAddress.getOffset() > 0) {
			lastAddress = startAddress.subtract(1);
		}
	}
	else {
		// don't add past the address range
		AddressSpace addressSpace = startAddress.getAddressSpace();
		Address maxAddress = addressSpace.getMaxAddress();
		long maxOffset = maxAddress.getOffset();
		long startOffset = startAddress.getOffset();
		long result = startOffset + 1;
		if (result > startOffset && result < maxOffset) {
			lastAddress = startAddress.add(1);
		}
	}
}
 
示例5
private void markupDebugCodeView(Program program, boolean isBinary,
		MessageLog log, AddressSpace space) throws DuplicateNameException, IOException {
	DebugCodeView dcv = parser.getDebugCodeView();
	if (dcv != null) {
		Address dataAddr = getDataAddress(dcv.getDebugDirectory(), isBinary, space, ntHeader);
		if (dataAddr != null) {
			PdbInfoIface pdbInfo = dcv.getPdbInfo();
			if (pdbInfo != null) {
				setPlateComment(program, dataAddr, "CodeView PDB Info");
				PeUtils.createData(program, dataAddr, pdbInfo.toDataType(), log);
			}
			PdbInfoDotNetIface dotNetPdbInfo = dcv.getDotNetPdbInfo();
			if (dotNetPdbInfo != null) {
				setPlateComment(program, dataAddr, ".NET PDB Info");
				PeUtils.createData(program, dataAddr, dotNetPdbInfo.toDataType(), log);
			}
		}
	}
}
 
示例6
/**
 * Adds existing overlay spaces to the factory.
 * @param factory the factory to add overlay spaces to
 * @throws IOException
 */
void initializeOverlaySpaces(ProgramAddressFactory factory) throws IOException {
	Table table = db.getTable(TABLE_NAME);
	if (table != null) {
		RecordIterator it = table.iterator();
		while (it.hasNext()) {
			Record rec = it.next();
			String spaceName = rec.getString(OV_SPACE_NAME_COL);
			String templateSpaceName = rec.getString(OV_SPACE_BASE_COL);
			long minOffset = rec.getLongValue(OV_MIN_OFFSET_COL);
			long maxOffset = rec.getLongValue(OV_MAX_OFFSET_COL);
			AddressSpace space = factory.getAddressSpace(templateSpaceName);
			try {
				OverlayAddressSpace sp =
					factory.addOverlayAddressSpace(spaceName, space, minOffset, maxOffset);
				sp.setDatabaseKey(rec.getKey());
			}
			catch (DuplicateNameException e) {
				throw new AssertException(
					"Should not have duplicateNameException when recreating factory");
			}
		}
	}
}
 
示例7
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
		TaskMonitor monitor, MessageLog log) throws IOException {

	if (!prog.getExecutableFormat().equals(PeLoader.PE_NAME)) {
		throw new IOException("Program must be a " + PeLoader.PE_NAME);
	}

	SymbolTable symtab = prog.getSymbolTable();
	AddressSpace space = prog.getAddressFactory().getDefaultAddressSpace();
	List<MapExport> exports = parseExports(provider, log);
	int successCount = 0;
	for (MapExport exp : exports) {
		try {
			symtab.createLabel(space.getAddress(exp.addr), exp.name,
				SourceType.IMPORTED).setPrimary();
			successCount++;
		}
		catch (InvalidInputException e) {
			log.appendMsg(e.getMessage());
		}
	}
	log.appendMsg("Added " + successCount + " symbols.");
}
 
示例8
/**
 * @see ghidra.program.database.symbol.SymbolDatabaseAdapter#getMaxSymbolAddress(ghidra.program.model.address.AddressSpace)
 */
@Override
Address getMaxSymbolAddress(AddressSpace space) throws IOException {
	if (space.isMemorySpace()) {
		AddressIndexKeyIterator addressKeyIterator =
			new AddressIndexKeyIterator(symbolTable, SYMBOL_ADDR_COL, addrMap,
				space.getMinAddress(), space.getMaxAddress(), false);
		if (addressKeyIterator.hasNext()) {
			return addrMap.decodeAddress(addressKeyIterator.next());
		}
	}
	else {
		LongField max = new LongField(addrMap.getKey(space.getMaxAddress(), false));
		DBFieldIterator iterator =
			symbolTable.indexFieldIterator(null, max, false, SYMBOL_ADDR_COL);
		if (iterator.hasPrevious()) {
			LongField val = (LongField) iterator.previous();
			Address addr = addrMap.decodeAddress(val.getLongValue());
			if (space.equals(addr.getAddressSpace())) {
				return addr;
			}
		}
	}
	return null;
}
 
示例9
protected void buildRangelistXML(StringBuilder res) {
	if (pcaddr == null || pcaddr.isExternalAddress()) {
		res.append("<rangelist/>");
		return;
	}
	res.append("<rangelist>");
	AddressSpace space = pcaddr.getAddressSpace();
	long off;
	if (space.isOverlaySpace()) {
		space = space.getPhysicalSpace();
		off = space.getAddress(pcaddr.getOffset()).getUnsignedOffset();
	}
	else {
		off = pcaddr.getUnsignedOffset();
	}
	res.append("<range");
	SpecXmlUtils.encodeStringAttribute(res, "space", space.getName());
	SpecXmlUtils.encodeUnsignedIntegerAttribute(res, "first", off);
	SpecXmlUtils.encodeUnsignedIntegerAttribute(res, "last", off);
	res.append("/>");
	res.append("</rangelist>\n");
}
 
示例10
private boolean validLocationAddress() {

		AddressSpace locationAddressSpace = extAddressInputWidget.getAddressSpace();
		if (locationAddressSpace != null) {
			if (extAddressInputWidget.hasInput()) {
				Address locationAddress = extAddressInputWidget.getAddress();
				if (locationAddress == null) {
					showInputErr("Invalid address specified, " + locationAddressSpace.getName() +
						" offset must be in range: " +
						locationAddressSpace.getMinAddress().toString(false) + " to " +
						locationAddressSpace.getMaxAddress().toString(false));
					return false;
				}
			}
		}
		return true;
	}
 
示例11
@Override
public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
		NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
		DataTypeConflictException, IOException {

	if (!isBinary) {//certificates are never mapped into running program...
		return;
	}

	monitor.setMessage(program.getName()+": security data...");

	AddressSpace space = program.getAddressFactory().getDefaultAddressSpace();
	Address addr = space.getAddress(virtualAddress);//NOTE: virtualAddress is only a binary offset inside file!!!

	createDirectoryBookmark(program, addr);

	program.getListing().clearCodeUnits(addr, addr, false);

	for (SecurityCertificate cert : certificates) {
		DataType dt = cert.toDataType();
		program.getListing().createData(addr, dt);
		addr = addr.add(dt.getLength());
	}
}
 
示例12
/**
 * Calculate the Address for every method in the list and add an entry to -methodMap-
 * @param defaultAddressSpace is the AddressSpace all encoded offsets are relative to
 * @param methodList is the list of encoded methods
 */
private void installMethodList(AddressSpace defaultAddressSpace,
		List<EncodedMethod> methodList) {
	for (EncodedMethod encodedMethod : methodList) {
		Address methodAddress = defaultAddressSpace.getAddress(
			DexUtil.METHOD_ADDRESS + encodedMethod.getCodeOffset());
		methodMap.put(methodAddress, encodedMethod);
	}
}
 
示例13
@Test
   public void testRegisterAddress() {
	AddressSpace regSpace = p.getAddressFactory().getRegisterSpace();
	Address a = regSpace.getAddress(0);
	long key = addrMap.getKey(a, false);
	assertEquals(0x3000000000000000l, key);
	Address b = addrMap.decodeAddress(key);
	assertEquals(a, b);

	a = regSpace.getAddress(10);
	key = addrMap.getKey(a, false);
	assertEquals(0x300000000000000al, key);
	b = addrMap.decodeAddress(key);
	assertEquals(a, b);
}
 
示例14
@Override
public AddressSpace getPreferredSegmentAddressSpace(ElfLoadHelper elfLoadHelper,
		ElfProgramHeader elfProgramHeader) {
	Language language = elfLoadHelper.getProgram().getLanguage();
	if (isDataLoad(elfProgramHeader)) {
		return language.getDefaultDataSpace();
	}
	return language.getDefaultSpace();
}
 
示例15
/**
 * Get the preferred load address space for an allocated section.   The OTHER space
 * is reserved and should not be returned by this method.
 * @param elfLoadHelper load helper object
 * @param elfSectionHeader elf section header
 * @return preferred load address space
 */
public AddressSpace getPreferredSectionAddressSpace(ElfLoadHelper elfLoadHelper,
		ElfSectionHeader elfSectionHeader) {
	Program program = elfLoadHelper.getProgram();
	if (elfSectionHeader.isExecutable()) {
		return program.getAddressFactory().getDefaultAddressSpace();
	}
	// segment is not marked execute, use the data space by default
	return program.getLanguage().getDefaultDataSpace();
}
 
示例16
@Override
public void evaluate(Emulate emu, Varnode outputVarnode, Varnode[] inputs) {
	int numArgs = inputs.length - 1;
	if (numArgs != 3) throw new LowlevelError(this.getClass().getName() + ": requires 3 inputs (FCX, LCX, PCXI), got " + numArgs);

	MemoryState memoryState = emu.getMemoryState();

	// compute new EA
	BigInteger FCXvalue = memoryState.getBigInteger(FCX);
	BigInteger PCXIvalue = memoryState.getBigInteger(PCXI);
	
	// read the value at FCX, if get nothing, then assume just increment the FCX to get to new node.
	
	// EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0};
	long ea = PCXIvalue.longValue();
	ea = ((ea & 0xffff0000) << 12) | ((ea & 0xffff) << 6);
	
	Address EA_addr = emu.getExecuteAddress().getNewAddress(ea);
	AddressSpace addressSpace = emu.getExecuteAddress().getAddressSpace();
	
	// read the bytes
	byte[] inBytes = new byte[4*16];
	memoryState.getChunk(inBytes, addressSpace, EA_addr.getOffset(), 4*16, true);
	//	{PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12], A[13], A[14], A[15], D[12], D[13], D[14], D[15] = M(EA,16 * word)};
	int index = 0;
	index += copyArrayToRegister(PCXI, PCXI.getBitLength()/8, memoryState, inBytes, index);
	index += copyArrayToRegister(PSW, PSW.getBitLength()/8, memoryState, inBytes, index);
	index += copyArrayToRegister(a10, 2 * a10.getBitLength()/8, memoryState, inBytes, index);
	index += copyArrayToRegister(d8, 4 * d8.getBitLength()/8, memoryState, inBytes, index);
	index += copyArrayToRegister(a12, 4 * a12.getBitLength()/8, memoryState, inBytes, index);
	index += copyArrayToRegister(d12, 4 * d12.getBitLength()/8, memoryState, inBytes, index);	
		
	// M(EA, word) = FCX;
	memoryState.setValue(EA_addr.getAddressSpace(), EA_addr.getOffset(), 4, FCXvalue);

	// FCX[19:0] = new_FCX[19:0];
	FCXvalue = FCXvalue.andNot(BigInteger.valueOf(0x000fffff)).or(PCXIvalue.and(BigInteger.valueOf(0x000fffff)));
	memoryState.setValue(FCX, FCXvalue);
}
 
示例17
private int copyArrayToRegister(Register reg, int len, MemoryState memoryState, byte[] inBytes, int i) {
	byte[] vBytes = new byte[len];
	AddressSpace spc = reg.getAddressSpace();
	System.arraycopy(inBytes, i, vBytes, 0, vBytes.length);
	memoryState.setChunk(vBytes, spc, reg.getOffset(), vBytes.length);
	return len;
}
 
示例18
private void markupThunk(Program program, 
					boolean isBinary, 
					AddressSpace space, 
					DelayImportDescriptor descriptor,
					long ptr,
					List<ThunkData> thunks,
					TaskMonitor monitor,
					MessageLog log) {

	DataType dt = ntHeader.getOptionalHeader().is64bit() 
			? (DataType)QWORD 
			: (DataType)DWORD;
	
	long thunkPtr = va(ptr, isBinary);
	if (!descriptor.isUsingRVA()) {
		thunkPtr -= ntHeader.getOptionalHeader().getImageBase();
	}

	for (ThunkData thunk : thunks) {
		if (monitor.isCancelled()) {
			return;
		}
		Address thunkAddress = space.getAddress(thunkPtr);
		PeUtils.createData(program, thunkAddress, dt, log);
		setEolComment(program, thunkAddress, thunk.getStructName());
		thunkPtr += thunk.getStructSize();
	}
}
 
示例19
private String getAddressString(CodeUnit cu) {
	Address addr = cu.getMinAddress();
	AddressSpace space = addr.getAddressSpace();
	if (displayBlockName) {
		String text = addr.toString(false, padZeros ? 16 : minHexDigits);
		MemoryBlock block = cu.getProgram().getMemory().getBlock(addr);
		if (block != null) {
			return block.getName() + ":" + text;
		}
	}
	return addr.toString(space.showSpaceName(), padZeros ? 16 : minHexDigits);
}
 
示例20
private static int getOffsetUnitSize(Language language, CoffSectionHeader section) {
	// Assumes all offset utilize a consistent unit size based upon the code space.
	// Keep notes here when this offset characterization is violated and a new one established.
	//   TMS320C55x appears to use byte offsets (unit sizes: code=1 data=2)
	AddressSpace codeSpace = language.getDefaultSpace(); // presumed code space
	if (section == null || !section.isExplicitlyByteAligned()) {
		return codeSpace.getAddressableUnitSize();
	}
	return 1;
}
 
示例21
@Test
   public void testStackAddressNegative() {
	AddressSpace stackSpace = p.getAddressFactory().getStackSpace();
	Address a = stackSpace.getAddress(-1);
	long key = addrMap.getKey(a, false);
	assertEquals(0x40000000ffffffffl, key);
	Address b = addrMap.decodeAddress(key);
	assertEquals(a, b);

}
 
示例22
@Override
public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
		NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
		DataTypeConflictException, IOException {

	monitor.setMessage(program.getName()+": TLS...");
	Address addr = PeUtils.getMarkupAddress(program, isBinary, ntHeader, virtualAddress);
	if (!program.getMemory().contains(addr)) {
		return;
	}
	createDirectoryBookmark(program, addr);
	PeUtils.createData(program, addr, tls.toDataType(), log);

	// Markup TLS callback functions
	if (tls.getAddressOfCallBacks() != 0) {
		AddressSpace space = program.getImageBase().getAddressSpace();
		DataType pointerDataType = PointerDataType.dataType.clone(program.getDataTypeManager());
		try {
			for (int i = 0; i < 20; i++) { // cap # of TLS callbacks as a precaution (1 is the norm)
				Address nextCallbackPtrAddr = space.getAddress(
					tls.getAddressOfCallBacks() + i * pointerDataType.getLength());
				Address nextCallbackAddr = PointerDataType.getAddressValue(
					new DumbMemBufferImpl(program.getMemory(), nextCallbackPtrAddr),
					pointerDataType.getLength(), space);
				if (nextCallbackAddr.getOffset() == 0) {
					break;
				}
				PeUtils.createData(program, nextCallbackPtrAddr, pointerDataType, log);
				program.getSymbolTable().createLabel(nextCallbackAddr, "tls_callback_" + i,
					SourceType.IMPORTED);
				program.getSymbolTable().addExternalEntryPoint(nextCallbackAddr);
			}
		}
		catch (InvalidInputException e) {
			log.appendMsg("TLS", "Failed to markup TLS callback functions: " + e.getMessage());
		}
	}
}
 
示例23
@Override
public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
		NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException {

   	monitor.setMessage(program.getName()+": bound import(s)...");
	Address addr = PeUtils.getMarkupAddress(program, isBinary, ntHeader, virtualAddress);
	if (!program.getMemory().contains(addr)) {
		return;
	}
	createDirectoryBookmark(program, addr);

	AddressSpace space = program.getAddressFactory().getDefaultAddressSpace();

	for (BoundImportDescriptor descriptor : descriptors) {
           if (monitor.isCancelled()) {
               return;
           }
           DataType dt = descriptor.toDataType();
		PeUtils.createData(program, addr, dt, log);
           addr = addr.add(dt.getLength());

           long namePtr = descriptor.getOffsetModuleName()+virtualAddress;
           Address nameAddr = space.getAddress(va(namePtr, isBinary));
           createTerminatedString(program, nameAddr, false, log);
       }

	BoundImportDescriptor terminator = new BoundImportDescriptor();
	PeUtils.createData(program, addr, terminator.toDataType(), log);
   }
 
示例24
@Override
public int getChunk(byte[] res, AddressSpace spc, long off, int size,
		boolean stopOnUnintialized) {
	int readLen = super.getChunk(res, spc, off, size, stopOnUnintialized);
	if (filterEnabled && filter != null) {
		filterEnabled = false;
		try {
			filter.filterRead(spc, off, readLen, res);
		}
		finally {
			filterEnabled = true;
		}
	}
	return readLen;
}
 
示例25
@Override
public void setChunk(byte[] res, AddressSpace spc, long off, int size) {
	super.setChunk(res, spc, off, size);
	if (filterEnabled && filter != null) {
		filterEnabled = false;
		try {
			filter.filterWrite(spc, off, size, res);
		}
		finally {
			filterEnabled = true;
		}
	}
}
 
示例26
private void markupDebigMisc(Program program, boolean isBinary,
		MessageLog log, AddressSpace space) throws DuplicateNameException {
	DebugMisc dm = parser.getDebugMisc();
	if (dm != null) {
		Address dataAddr = getDataAddress(dm.getDebugDirectory(), isBinary, space, ntHeader);
		if (dataAddr != null) {
			setPlateComment(program, dataAddr, "Misc Debug Info");
			PeUtils.createData(program, dataAddr, dm.toDataType(), log);
		}
	}
}
 
示例27
@Test
public void testCreateStringOverUndefined() throws Exception {

	Program testProgram = buildProgram("stringOverUndefinedTest", false);

	Listing listing = testProgram.getListing();
	AddressSpace space = testProgram.getLanguage().getAddressFactory().getDefaultAddressSpace();
	Address addr = addr(space, 0x11000);

	// Create undefined data type at start and middle of string
	listing.createData(addr, Undefined.getUndefinedDataType(1));
	listing.createData(addr.add(3), Undefined.getUndefinedDataType(1));
	Data data = listing.getDefinedDataAt(addr);
	assertNotNull(data);

	// Run StringAnalyzer
	AutoAnalysisManager manager = AutoAnalysisManager.getAnalysisManager(testProgram);
	StringsAnalyzer analyzer = new StringsAnalyzer();
	analyzer.added(testProgram, null, monitor, manager.getMessageLog());

	// Make sure our string got created, despite the undefined being there
	data = listing.getDefinedDataAt(addr);
	assertNotNull(data);
	String type = data.getDataType().getName().toLowerCase();
	assertTrue("Data at address " + addr + " should be a type of string instead of " + type,
		type.contains("string"));
}
 
示例28
/**
 * Format VarnodeTpl in a manner consistent with Varnode.toString().
 * @param space address space
 * @param offset offset of type ConstTpl.REAL
 * @param size size of type ConstTpl.REAL
 * @param lineList
 */
private void formatRaw(AddressSpace space, ConstTpl offset, ConstTpl size,
		List<AttributedString> lineList) {
	// same format as the Varnode.toString
	String str =
		"(" + space.getName() + ", 0x" + Long.toHexString(offset.getReal()) + ", " +
			size.getReal() + ")";
	lineList.add(new AttributedString(str, Color.BLUE, metrics));
}
 
示例29
/**
 * Any MemoryBank that has been registered with this MemoryState can be retrieved via this
 * method if the MemoryBank's associated address space is known.
 * @param spc is the address space of the desired MemoryBank
 * @return the MemoryBank or null if no bank is associated with spc.
 */
public final MemoryBank getMemoryBank(AddressSpace spc) {
	int index = spc.getUnique();
	if (index >= memspace.size())
		return null;
	return memspace.get(index);
}
 
示例30
/**
 * This is the main interface for reading values from the MemoryState.
 * If there is no registered MemoryBank for the desired address space, or
 * if there is some other error, an exception is thrown.
 * @param spc is the address space being queried
 * @param off is the offset of the value being queried
 * @param size is the number of bytes to query
 * @param signed true if signed value should be returned, false for unsigned value
 * @return the queried unsigned value
 */
public final BigInteger getBigInteger(AddressSpace spc, long off, int size, boolean signed) {
	if (spc.isConstantSpace()) {
		if (!signed && off < 0) {
			return new BigInteger(1, Utils.longToBytes(off, 8, true));
		}
		return BigInteger.valueOf(off);
	}
	byte[] bytes = new byte[size];
	getChunk(bytes, spc, off, size, false);
	return Utils.bytesToBigInteger(bytes, size, language.isBigEndian(), signed);
}