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

The following examples show how to use ghidra.program.model.mem.Memory#setShort() . 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: 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;
		}
	}
}