Ghidra is a software reverse engineering (SRE) framework
GP-2350 Corrected processing of ELF REL type relocations for R_ARM_JUMP24, R_ARM_CALL and R_ARM_PLT32
ghidra151255066a43f
diff --git a/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationConstants.java b/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationConstants.javaindex 74e5505ccf..7e79867c3e 100644--- a/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationConstants.java+++ b/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationConstants.java@@ -71,7 +71,7 @@ public class ARM_ElfRelocationConstants {/** GOT(S) + A - GOT_ORG */public static final int R_ARM_GOT_BREL = 26;/** ((S + A) | T) - P */- public static final int R_ARM_GOT_PLT32 = 27;+ public static final int R_ARM_PLT32 = 27;/** ((S + A) | T) - P */public static final int R_ARM_CALL = 28;/** ((S + A) | T) - P */diff --git a/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationHandler.java b/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationHandler.javaindex d52541f83a..9089b9a626 100644--- a/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationHandler.java+++ b/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ARM_ElfRelocationHandler.java@@ -84,7 +84,7 @@ public class ARM_ElfRelocationHandler extends ElfRelocationHandler {case ARM_ElfRelocationConstants.R_ARM_PC24: { // Target class: ARM Instructionint oldValue = memory.getInt(relocationAddress, instructionBigEndian);if (elfRelocationContext.extractAddend()) {- addend = (oldValue << 8 >> 6); // extract addend and sign-extend with *4 factor+ addend = (oldValue << 8) >> 6; // extract addend and sign-extend with *4 factor}newValue = (int) (symbolValue + addend);newValue -= (offset + elfRelocationContext.getPcBias(false));@@ -217,7 +217,7 @@ public class ARM_ElfRelocationHandler extends ElfRelocationHandler {case ARM_ElfRelocationConstants.R_ARM_THM_PC8: { // Target class: Thumb16 Instructionshort oldValue = memory.getShort(relocationAddress, instructionBigEndian);newValue = (int) (symbolValue + addend);- newValue -= (offset + 4); // PC relative, PC will be 4 bytes past inst start+ newValue -= (offset + elfRelocationContext.getPcBias(true));newValue = newValue >> 1;short sValue = (short) ((oldValue & 0xff00) | (newValue & 0x00ff));memory.setShort(relocationAddress, sValue, instructionBigEndian);@@ -313,8 +313,11 @@ public class ARM_ElfRelocationHandler extends ElfRelocationHandler {case ARM_ElfRelocationConstants.R_ARM_JUMP24: // Target class: ARM Instructioncase ARM_ElfRelocationConstants.R_ARM_CALL:- case ARM_ElfRelocationConstants.R_ARM_GOT_PLT32:+ case ARM_ElfRelocationConstants.R_ARM_PLT32:int oldValue = memory.getInt(relocationAddress, instructionBigEndian);+ if (elfRelocationContext.extractAddend()) {+ addend = (oldValue << 8) >> 6; // extract addend and sign-extend with *4 factor+ }newValue = (int) (symbolValue + addend);newValue -= (offset + elfRelocationContext.getPcBias(false));diff --git a/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfArmRelocationFixupHandler.java b/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfArmRelocationFixupHandler.javaindex f2840e3067..d7481d13f5 100644--- a/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfArmRelocationFixupHandler.java+++ b/Ghidra/Processors/ARM/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfArmRelocationFixupHandler.java@@ -32,13 +32,15 @@ public class ElfArmRelocationFixupHandler extends RelocationFixupHandler {Address newImageBase) throws MemoryAccessException, CodeUnitInsertionException {switch (relocation.getType()) {+// TODO: This over simplified relocation fixup is flawed and does not properly+// handle post-import image base change for supported relocationscase ARM_ElfRelocationConstants.R_ARM_NONE:case ARM_ElfRelocationConstants.R_ARM_ABS32:case ARM_ElfRelocationConstants.R_ARM_REL32:case ARM_ElfRelocationConstants.R_ARM_GLOB_DAT:// case ARM_ElfRelocationConstants.R_ARM_JUMP_SLOT:case ARM_ElfRelocationConstants.R_ARM_RELATIVE:- case ARM_ElfRelocationConstants.R_ARM_GOT_PLT32:+ case ARM_ElfRelocationConstants.R_ARM_PLT32:case ARM_ElfRelocationConstants.R_ARM_CALL:case ARM_ElfRelocationConstants.R_ARM_JUMP24:case ARM_ElfRelocationConstants.R_ARM_THM_JUMP24: