Ghidra is a software reverse engineering (SRE) framework
GP-4152 msp430 ELF relocation fixes
Jamese7f5a8a5a181
diff --git a/Ghidra/Processors/TI_MSP430/data/languages/TI430X.sinc b/Ghidra/Processors/TI_MSP430/data/languages/TI430X.sincindex 80054727b0..cc526bd116 100644--- a/Ghidra/Processors/TI_MSP430/data/languages/TI430X.sinc+++ b/Ghidra/Processors/TI_MSP430/data/languages/TI430X.sinc@@ -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);diff --git a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430X_ElfRelocationConstants.java b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430X_ElfRelocationConstants.javasimilarity index 96%rename from Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430X_ElfRelocationConstants.javarename to Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430X_ElfRelocationConstants.javaindex 7278758b13..e9acd105b7 100644--- a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430X_ElfRelocationConstants.java+++ b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430X_ElfRelocationConstants.java@@ -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 namespublic 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 onlypublic static final int R_MSP430X_SUB_ULEB128 = 23; // GNU only- private TI_MSP430X_ElfRelocationConstants() {+ private MSP430X_ElfRelocationConstants() {//class not for instantiation}diff --git a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430X_ElfRelocationHandler.java b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430X_ElfRelocationHandler.javasimilarity index 79%rename from Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430X_ElfRelocationHandler.javarename to Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430X_ElfRelocationHandler.javaindex eaca68a4e8..0597dfb6a1 100644--- a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430X_ElfRelocationHandler.java+++ b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430X_ElfRelocationHandler.java@@ -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 {@Overridepublic 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);diff --git a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430_ElfRelocationConstants.java b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430_ElfRelocationConstants.javasimilarity index 96%rename from Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430_ElfRelocationConstants.javarename to Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430_ElfRelocationConstants.javaindex 9864cdf3a4..5f506fa320 100644--- a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430_ElfRelocationConstants.java+++ b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430_ElfRelocationConstants.java@@ -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;diff --git a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430_ElfRelocationHandler.java b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430_ElfRelocationHandler.javasimilarity index 88%rename from Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430_ElfRelocationHandler.javarename to Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430_ElfRelocationHandler.javaindex ac779f23f3..a28090de4d 100644--- a/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/TI_MSP430_ElfRelocationHandler.java+++ b/Ghidra/Processors/TI_MSP430/src/main/java/ghidra/app/util/bin/format/elf/relocation/MSP430_ElfRelocationHandler.java@@ -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 {@Overridepublic 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;