GP-4152 msp430 ELF relocation fixes

This commit is contained in:
James 2024-01-08 21:52:20 +00:00 committed by ghidra1
parent 325c48e904
commit e7f5a8a5a1
5 changed files with 47 additions and 37 deletions

View File

@ -3258,7 +3258,6 @@ macro wzero(full, word)
<top>
# Operation Flags...
build repeat_carry;
$(CARRY) = $(CARRY) * ctx_zc;
$(OVERFLOW) = ((XRREG_B_AS_DEST != 0x0) && ($(CARRY) == 0x1)); # V Flag
# Operation...
tmp:1 = $(CARRY);

View File

@ -18,7 +18,7 @@ package ghidra.app.util.bin.format.elf.relocation;
/**
* class for elf_msp430x_reloc_type (from binutils source)
*/
public class TI_MSP430X_ElfRelocationConstants {
public class MSP430X_ElfRelocationConstants {
// Note: some of the msp430x relocation types have "msp430" (without the x)
// in their names
public static final int R_MSP430_NONE = 0; // No calculation
@ -46,7 +46,7 @@ public class TI_MSP430X_ElfRelocationConstants {
public static final int R_MSP430X_SET_ULEB128 = 22; // GNU only
public static final int R_MSP430X_SUB_ULEB128 = 23; // GNU only
private TI_MSP430X_ElfRelocationConstants() {
private MSP430X_ElfRelocationConstants() {
//class not for instantiation
}

View File

@ -25,7 +25,7 @@ import ghidra.program.model.reloc.Relocation.Status;
import ghidra.program.model.reloc.RelocationResult;
import ghidra.util.exception.NotFoundException;
public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
public class MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
@Override
public boolean canRelocate(ElfHeader elf) {
@ -38,7 +38,7 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
ElfRelocation relocation, Address relocationAddress)
throws MemoryAccessException, NotFoundException {
int type = relocation.getType();
if (type == TI_MSP430X_ElfRelocationConstants.R_MSP430_NONE) {
if (type == MSP430X_ElfRelocationConstants.R_MSP430_NONE) {
return RelocationResult.SKIPPED;
}
@ -53,29 +53,29 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
int byteLength = 0;
switch (type) {
case TI_MSP430X_ElfRelocationConstants.R_MSP430_ABS32:
case MSP430X_ElfRelocationConstants.R_MSP430_ABS32:
int newIntValue = (int) (symbolValue + addend);
memory.setInt(relocationAddress, newIntValue);
byteLength = 4;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430_ABS16:
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_ABS16:
case MSP430X_ElfRelocationConstants.R_MSP430_ABS16:
case MSP430X_ElfRelocationConstants.R_MSP430X_ABS16:
short newShortValue = (short) (symbolValue + addend);
memory.setShort(relocationAddress, newShortValue);
byteLength = 2;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430_ABS8:
case MSP430X_ElfRelocationConstants.R_MSP430_ABS8:
byte newByteValue = (byte) (symbolValue + addend);
memory.setByte(relocationAddress, newByteValue);
byteLength = 1;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430_PCR16:
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_PCR16:
case MSP430X_ElfRelocationConstants.R_MSP430_PCR16:
case MSP430X_ElfRelocationConstants.R_MSP430X_PCR16:
newShortValue = (short) (symbolValue + addend - offset);
memory.setShort(relocationAddress, newShortValue);
byteLength = 2;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_EXT_SRC:
case MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_EXT_SRC:
newIntValue = (int) (symbolValue + addend - offset - 4);
memory.setShort(relocationAddress.add(4), (short) (newIntValue));
newIntValue >>= 16;
@ -84,9 +84,9 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
highWord &= 0xff0f;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 3;
byteLength = 6;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_EXT_DST:
case MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_EXT_DST:
newIntValue = (int) (symbolValue + addend - offset - 4);
memory.setShort(relocationAddress.add(4), (short) (newIntValue));
newIntValue >>= 16;
@ -95,9 +95,9 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
highWord &= 0xfff0;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 3;
byteLength = 6;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_EXT_ODST:
case MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_EXT_ODST:
newIntValue = (int) (symbolValue + addend - offset - 4);
memory.setShort(relocationAddress.add(6), (short) (newIntValue));
newIntValue >>= 16;
@ -106,9 +106,9 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
highWord &= 0xfff0;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 4;
byteLength = 8;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_EXT_SRC:
case MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_EXT_SRC:
newIntValue = (int) (symbolValue + addend);
memory.setShort(relocationAddress.add(4), (short) (newIntValue));
newIntValue >>= 16;
@ -117,9 +117,9 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
highWord &= 0xff0f;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 3;
byteLength = 6;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_EXT_DST:
case MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_EXT_DST:
newIntValue = (int) (symbolValue + addend);
memory.setShort(relocationAddress.add(4), (short) (newIntValue));
newIntValue >>= 16;
@ -128,9 +128,9 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
highWord &= 0xfff0;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 3;
byteLength = 6;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_EXT_ODST:
case MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_EXT_ODST:
newIntValue = (int) (symbolValue + addend);
memory.setShort(relocationAddress.add(6), (short) (newIntValue));
newIntValue >>= 16;
@ -139,9 +139,20 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
highWord &= 0xfff0;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 8;
break;
case MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_ADR_SRC:
newIntValue = (int) (symbolValue + addend);
memory.setShort(relocationAddress.add(2), (short) (newIntValue & 0xffff));
newIntValue >>= 16;
newIntValue &= 0xf;
highWord = memory.getShort(relocationAddress);
highWord &= 0xf0ff;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 4;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_ADR_DST:
case MSP430X_ElfRelocationConstants.R_MSP430X_ABS20_ADR_DST:
newIntValue = (int) (symbolValue + addend);
memory.setShort(relocationAddress.add(2), (short) (newIntValue & 0xffff));
newIntValue >>= 16;
@ -150,20 +161,20 @@ public class TI_MSP430X_ElfRelocationHandler extends ElfRelocationHandler {
highWord &= 0xfff0;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 3;
byteLength = 4;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_CALL:
case MSP430X_ElfRelocationConstants.R_MSP430X_PCR20_CALL:
newIntValue = (int) (symbolValue + addend - offset);
memory.setShort(relocationAddress.add(2), (short) (newIntValue & 0xffff));
memory.setShort(relocationAddress.add(2), (short) newIntValue);
newIntValue >>= 16;
newIntValue &= 0xf;
highWord = memory.getShort(relocationAddress);
highWord &= 0xfff0;
highWord |= newIntValue;
memory.setShort(relocationAddress, highWord);
byteLength = 3;
byteLength = 4;
break;
case TI_MSP430X_ElfRelocationConstants.R_MSP430X_10_PCREL:
case MSP430X_ElfRelocationConstants.R_MSP430X_10_PCREL:
short oldShortValue = memory.getShort(relocationAddress);
oldShortValue &= 0xfc00;
newShortValue = (short) (symbolValue + addend - offset - 2);

View File

@ -18,7 +18,7 @@ package ghidra.app.util.bin.format.elf.relocation;
/**
* class for elf_msp430_reloc_type (from binutils source)
*/
public class TI_MSP430_ElfRelocationConstants {
public class MSP430_ElfRelocationConstants {
public static final int R_MSP430_NONE = 0;
public static final int R_MSP430_32 = 1;
public static final int R_MSP430_10_PCREL = 2;

View File

@ -25,7 +25,7 @@ import ghidra.program.model.reloc.Relocation.Status;
import ghidra.program.model.reloc.RelocationResult;
import ghidra.util.exception.NotFoundException;
public class TI_MSP430_ElfRelocationHandler extends ElfRelocationHandler {
public class MSP430_ElfRelocationHandler extends ElfRelocationHandler {
@Override
public boolean canRelocate(ElfHeader elf) {
@ -38,7 +38,7 @@ public class TI_MSP430_ElfRelocationHandler extends ElfRelocationHandler {
ElfRelocation relocation, Address relocationAddress)
throws MemoryAccessException, NotFoundException {
int type = relocation.getType();
if (type == TI_MSP430_ElfRelocationConstants.R_MSP430_NONE) {
if (type == MSP430_ElfRelocationConstants.R_MSP430_NONE) {
return RelocationResult.SKIPPED;
}
@ -54,12 +54,12 @@ public class TI_MSP430_ElfRelocationHandler extends ElfRelocationHandler {
int byteLength = 0;
switch (type) {
case TI_MSP430_ElfRelocationConstants.R_MSP430_32:
case MSP430_ElfRelocationConstants.R_MSP430_32:
int newIntValue = (int) (symbolValue + addend);
memory.setInt(relocationAddress, newIntValue);
byteLength = 4;
break;
case TI_MSP430_ElfRelocationConstants.R_MSP430_10_PCREL:
case MSP430_ElfRelocationConstants.R_MSP430_10_PCREL:
short oldShortValue = memory.getShort(relocationAddress);
oldShortValue &= 0xfc00;
short newShortValue = (short) (symbolValue + addend - offset - 2);
@ -69,25 +69,25 @@ public class TI_MSP430_ElfRelocationHandler extends ElfRelocationHandler {
memory.setShort(relocationAddress, newShortValue);
byteLength = 2;
break;
case TI_MSP430_ElfRelocationConstants.R_MSP430_16:
case MSP430_ElfRelocationConstants.R_MSP430_16:
newShortValue = (short) (symbolValue + addend);
memory.setShort(relocationAddress, newShortValue);
byteLength = 2;
break;
//case TI_MSP430_ElfRelocationConstants.R_MSP430_16_PCREL:
case TI_MSP430_ElfRelocationConstants.R_MSP430_16_BYTE:
case MSP430_ElfRelocationConstants.R_MSP430_16_BYTE:
newShortValue = (short) (symbolValue + addend);
memory.setShort(relocationAddress, newShortValue);
byteLength = 2;
break;
case TI_MSP430_ElfRelocationConstants.R_MSP430_16_PCREL_BYTE:
case MSP430_ElfRelocationConstants.R_MSP430_16_PCREL_BYTE:
newShortValue = (short) (symbolValue + addend - offset);
memory.setShort(relocationAddress, newShortValue);
byteLength = 2;
break;
//case TI_MSP430_ElfRelocationConstants.R_MSP430_2X_PCREL:
//case TI_MSP430_ElfRelocationConstants.R_MSP430_RL_PCREL:
case TI_MSP430_ElfRelocationConstants.R_MSP430_8:
case MSP430_ElfRelocationConstants.R_MSP430_8:
byte newByteValue = (byte) (symbolValue + addend);
memory.setByte(relocationAddress, newByteValue);
byteLength = 1;