Java Code Examples for ghidra.program.model.mem.Memory#setInt()

The following examples show how to use ghidra.program.model.mem.Memory#setInt() . 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: RelocationFixupHandler.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected boolean process32BitRelocation(Program program, Relocation relocation,
		Address oldImageBase, Address newImageBase) throws MemoryAccessException,
		CodeUnitInsertionException {
	long diff = newImageBase.subtract(oldImageBase);

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

	InstructionStasher instructionStasher = new InstructionStasher(program, address);

	memory.setInt(address, newValue);

	instructionStasher.restore();

	return true;
}
 
Example 2
Source File: PIC30_ElfRelocationHandler.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void relocate(ElfRelocationContext elfRelocationContext, ElfRelocation relocation, Address relocationAddress)
		throws MemoryAccessException, NotFoundException {

	int type = relocation.getType();
	if (type == R_PIC30_NONE) {
		return;
	}

	Program program = elfRelocationContext.getProgram();
	Memory memory = program.getMemory();

	int symbolIndex = relocation.getSymbolIndex();

	int addend = (int) relocation.getAddend();

	if (symbolIndex == 0) {// TODO
		return;
	}

	long relocWordOffset = (int) relocationAddress.getAddressableWordOffset();

	ElfSymbol sym = elfRelocationContext.getSymbol(symbolIndex);
	int symbolValue = (int) elfRelocationContext.getSymbolValue(sym); // word offset

	int oldValue = memory.getInt(relocationAddress);
	short oldShortValue = memory.getShort(relocationAddress);

	int newValue;

	ElfHeader elf = elfRelocationContext.getElfHeader();
	if (elf.e_machine() == ElfConstants.EM_DSPIC30F) {
		switch (type) {
		case R_PIC30_16: // 2
			newValue = (symbolValue + addend + oldShortValue) & 0xffff;
			memory.setShort(relocationAddress, (short) newValue);
			break;
		case R_PIC30_32: // 3
			newValue = symbolValue + addend + oldValue;
			memory.setInt(relocationAddress, newValue);
			break;
		case R_PIC30_FILE_REG_WORD_WITH_DST: // 7
			int reloc = symbolValue >> 1;
			reloc += addend;
			reloc += oldValue >> 4;
			reloc &= 0x7fff;
			newValue = (reloc << 4) | (oldValue & ~0x7fff0);
			memory.setInt(relocationAddress, newValue);
			break;
		case R_PIC30_WORD: // 8
		case R_PIC30_WORD_TBLOFFSET: // 0x15
			reloc = symbolValue;
			reloc += addend;
			reloc += oldValue >> 4;
			reloc &= 0xffff;
			newValue = (reloc << 4) | (oldValue & ~0x0ffff0);
			memory.setInt(relocationAddress, newValue);
			break;
		case R_PIC30_WORD_TBLPAGE: // 0x18
			reloc = symbolValue >> 16;
			reloc += addend;
			reloc += oldValue >> 4;
			reloc &= 0xffff;
			if (isEDSVariant(elfRelocationContext)) {
				reloc |= 0x100;
			}
			newValue = (reloc << 4) | (oldValue & ~0x0ffff0);
			memory.setInt(relocationAddress, newValue);
			break;
		case R_PIC30_PCREL_BRANCH: // 0x1c
			newValue = (int) (symbolValue - relocWordOffset + oldShortValue - 2);
			newValue >>>= 1;
			memory.setShort(relocationAddress, (short) (newValue & 0xffff));
			break;
		default:
			String symbolName = sym.getNameAsString();
			markAsUnhandled(program, relocationAddress, type, symbolIndex, symbolName,
					elfRelocationContext.getLog());
			break;
		}
	}
}
 
Example 3
Source File: SPARC_ElfRelocationHandler.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void relocate(ElfRelocationContext elfRelocationContext, ElfRelocation relocation,
		Address relocationAddress) throws MemoryAccessException, NotFoundException {

	ElfHeader elf = elfRelocationContext.getElfHeader();
	if (elf.e_machine() != ElfConstants.EM_SPARC &&
		elf.e_machine() != ElfConstants.EM_SPARC32PLUS) {
		return;
	}

	Program program = elfRelocationContext.getProgram();
	Memory memory = program.getMemory();

	int type = relocation.getType();
	if (type == SPARC_ElfRelocationConstants.R_SPARC_NONE) {
		return;
	}

	int symbolIndex = relocation.getSymbolIndex();

	long addend = relocation.getAddend(); // will be 0 for REL case

	long offset = (int) relocationAddress.getOffset();

	ElfSymbol sym = elfRelocationContext.getSymbol(symbolIndex);
	String symbolName = sym != null ? sym.getNameAsString() : null;

	long symbolValue = elfRelocationContext.getSymbolValue(sym);

	int oldValue = memory.getInt(relocationAddress);
	int newValue = 0;

	switch (type) {
		case SPARC_ElfRelocationConstants.R_SPARC_DISP32:
			newValue = (int) (symbolValue + addend - offset);
			memory.setInt(relocationAddress, oldValue | newValue);
			break;
		case SPARC_ElfRelocationConstants.R_SPARC_WDISP30:
			newValue = (int) (symbolValue + addend - offset) >>> 2;
			memory.setInt(relocationAddress, oldValue | newValue);
			break;
		case SPARC_ElfRelocationConstants.R_SPARC_HI22:
			newValue = ((int) symbolValue + (int) addend) >>> 10;
			memory.setInt(relocationAddress, oldValue | newValue);
			break;
		case SPARC_ElfRelocationConstants.R_SPARC_LO10:
			newValue = ((int) symbolValue + (int) addend) & 0x3FF;
			memory.setInt(relocationAddress, oldValue | newValue);
			break;
		case SPARC_ElfRelocationConstants.R_SPARC_JMP_SLOT:
			// should copy address of symbol in EXTERNAL block
		case SPARC_ElfRelocationConstants.R_SPARC_32:
			newValue = (int) symbolValue + (int) addend;
			memory.setInt(relocationAddress, newValue);
			break;
		// we punt on this because it's not linked yet!
		case SPARC_ElfRelocationConstants.R_SPARC_GLOB_DAT:
			newValue = (int) symbolValue;
			memory.setInt(relocationAddress, newValue);
			break;
		case SPARC_ElfRelocationConstants.R_SPARC_RELATIVE:
			newValue = (int) elf.getImageBase() + (int) addend;
			memory.setInt(relocationAddress, newValue);
			break;
		case SPARC_ElfRelocationConstants.R_SPARC_UA32:
			newValue = (int) symbolValue + (int) addend;
			memory.setInt(relocationAddress, newValue);
			break;
		case SPARC_ElfRelocationConstants.R_SPARC_COPY:
			markAsWarning(program, relocationAddress, "R_SPARC_COPY", symbolName, symbolIndex,
				"Runtime copy not supported", elfRelocationContext.getLog());
			break;
		default:
			markAsUnhandled(program, relocationAddress, type, symbolIndex, symbolName,
				elfRelocationContext.getLog());
			break;
	}
}