Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2678 corrected ELF R_MIPS_REL32 relocation processing for 64-bit architecture

ghidra1a089e27cde46
diff --git a/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/relocation/MIPS_ElfRelocationHandler.java b/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/relocation/MIPS_ElfRelocationHandler.java
index b77b0ad4f9..7bfd75681c 100644
--- a/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/relocation/MIPS_ElfRelocationHandler.java
+++ b/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/relocation/MIPS_ElfRelocationHandler.java
@@ -411,21 +411,33 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
}
case MIPS_ElfRelocationConstants.R_MIPS_32:
value = (int) symbolValue;
- int intAddend = (int) (mipsRelocationContext.extractAddend() ? oldValue : addend);
- value += intAddend;
+ int intAddend;
+ if (mipsRelocationContext.extractAddend()) {
+ intAddend = elf.is64Bit() ? (int) memory.getLong(relocationAddress)
+ : memory.getInt(relocationAddress);
+ }
+ else {
+ intAddend = (int) addend;
+ }
- newValue = value;
- writeNewValue = true;
+ newValue = value + intAddend;
+ long newValueBig = Integer.toUnsignedLong(newValue);
- if (symbolIndex != 0 && intAddend != 0 && !saveValue) {
- // If not continuing with compound relocation perform fixup so
- // we can create offset-pointer now.
- // NOTE: this may not handle all combound relocation cases
- memory.setInt(relocationAddress, newValue);
- writeNewValue = false;
- warnExternalOffsetRelocation(program, relocationAddress,
- symbolAddr, symbolName, intAddend, mipsRelocationContext.getLog());
- if (elf.is32Bit()) {
+ if (saveValue) {
+ mipsRelocationContext.savedAddend = newValueBig;
+ }
+ else {
+ if (elf.is64Bit()) {
+ memory.setLong(relocationAddress, newValueBig);
+ }
+ else {
+ memory.setInt(relocationAddress, newValue);
+ }
+ if (symbolIndex != 0 && intAddend != 0 && !saveValue) {
+ // If not continuing with compound relocation (64-bit only)
+ // perform fixup so we can create offset-pointer now.
+ warnExternalOffsetRelocation(program, relocationAddress,
+ symbolAddr, symbolName, intAddend, mipsRelocationContext.getLog());
applyComponentOffsetPointer(program, relocationAddress, intAddend);
}
}
@@ -451,7 +463,7 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
case MIPS_ElfRelocationConstants.R_MIPS_PC16:
newValue =
mipsRelocationContext.extractAddend() ? (oldValue & 0xffff) << 2 : (int) addend;
- long newValueBig = signExtend(newValue, 18);
+ newValueBig = signExtend(newValue, 18);
newValueBig += symbolValue - offset;
value = (int) newValueBig;