Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3026 - added support for ELF MIPS R_MIPS_GPREL32 relocation and corrected local R_MIPS_GPREL16 relocation.

ghidra15cde1bd48d04
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java
index 76a397ef39..055a088ccd 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java
@@ -1056,6 +1056,12 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {
AddressRange range = new AddressRangeImpl(alignedAddress, freeRange.getMaxAddress());
if (size > 0) {
+ long rangeLen = range.getLength();
+ if (rangeLen < 0 || rangeLen > size) {
+ // reduce size of allocation range
+ range = new AddressRangeImpl(range.getMinAddress(),
+ range.getMinAddress().add(size - 1));
+ }
// keep track if allocations other than the EXTERNAL block allocation
allocatedRegions.add(range);
}
diff --git a/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/extend/MIPS_ElfExtension.java b/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/extend/MIPS_ElfExtension.java
index adae4377f9..06d724837c 100644
--- a/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/extend/MIPS_ElfExtension.java
+++ b/Ghidra/Processors/MIPS/src/main/java/ghidra/app/util/bin/format/elf/extend/MIPS_ElfExtension.java
@@ -20,8 +20,7 @@ import java.math.BigInteger;
import ghidra.app.util.bin.format.elf.*;
import ghidra.app.util.bin.format.elf.ElfDynamicType.ElfDynamicValueType;
import ghidra.app.util.bin.format.elf.relocation.MIPS_Elf64Relocation;
-import ghidra.program.model.address.Address;
-import ghidra.program.model.address.AddressOutOfBoundsException;
+import ghidra.program.model.address.*;
import ghidra.program.model.data.*;
import ghidra.program.model.lang.Language;
import ghidra.program.model.lang.Register;
@@ -39,8 +38,10 @@ public class MIPS_ElfExtension extends ElfExtension {
private static final String MIPS_STUBS_SECTION_NAME = ".MIPS.stubs";
// GP value reflected by symbol address
- // TODO: In the future symbol could be created in the constant space
+ public static final String MIPS_GP_DISP_SYMBOL_NAME = "_gp_disp"; // relocation GP marker symbol
+ public static final String MIPS_GP_GNU_LOCAL_SYMBOL_NAME = "__gnu_local_gp";
public static final String MIPS_GP_VALUE_SYMBOL = "_mips_gp_value";
+ public static final String MIPS_GP0_VALUE_SYMBOL = "_mips_gp0_value";
// Elf Program Header Extensions
public static final ElfProgramHeaderType PT_MIPS_REGINFO = new ElfProgramHeaderType(0x70000000,
@@ -540,8 +541,6 @@ public class MIPS_ElfExtension extends ElfExtension {
odkHeader.add(WordDataType.dataType, "section", null);
odkHeader.add(DWordDataType.dataType, "info", null);
- Structure odkRegInfo = buildRegInfoStructure(elf64, true);
-
Memory memory = elfLoadHelper.getProgram().getMemory();
long limit = 0;
MemoryBlock block = memory.getBlock(mipsOptionsAddr);
@@ -577,9 +576,7 @@ public class MIPS_ElfExtension extends ElfExtension {
switch (kind) {
case ODK_REGINFO:
- Data regInfoData = elfLoadHelper.createData(nextOptionAddr, odkRegInfo);
- // TODO: need better understanding of ri_gp_value
- // TODO: adjust gp for section relocation and apply to section context somehow (need for relocation processing?)
+ processMipsRegInfo(elfLoadHelper, nextOptionAddr);
break;
default:
@@ -594,12 +591,9 @@ public class MIPS_ElfExtension extends ElfExtension {
catch (AddressOutOfBoundsException | MemoryAccessException e) {
// ignore
}
-
- elfLoadHelper.log(
- "WARNING: .MIPS.options section has not been processed (not yet implemented)");
}
- private Structure buildRegInfoStructure(boolean elf64, boolean mipsOptions) {
+ private Structure buildRegInfoStructure(boolean elf64) {
String prefix = elf64 ? "Elf64" : "Elf32";
@@ -640,11 +634,11 @@ public class MIPS_ElfExtension extends ElfExtension {
Structure regInfoStruct =
new StructureDataType(new CategoryPath("/ELF"), prefix + "_RegInfo_MIPS", 0);
regInfoStruct.add(gprMask, "ri_gprmask", null);
- if (mipsOptions && elf64) {
+ if (elf64) {
regInfoStruct.add(DWordDataType.dataType, "ri_pad", null);
}
regInfoStruct.add(new ArrayDataType(DWordDataType.dataType, 4, 4));
- if (mipsOptions & elf64) {
+ if (elf64) {
regInfoStruct.add(QWordDataType.dataType, "ri_gp_value", null);
}
else {
@@ -655,18 +649,24 @@ public class MIPS_ElfExtension extends ElfExtension {
private void processMipsRegInfo(ElfLoadHelper elfLoadHelper, Address regInfoAddr) {
- Structure regInfoStruct =
- buildRegInfoStructure(elfLoadHelper.getElfHeader().is64Bit(), false);
+ // NOTES: assumes only one gp0 value
+
+ boolean is64bit = elfLoadHelper.getElfHeader().is64Bit();
+ Structure regInfoStruct = buildRegInfoStructure(is64bit);
Data data = elfLoadHelper.createData(regInfoAddr, regInfoStruct);
- Data gpValueComponent = data.getComponent(5); // gp_value
+ Data gpValueComponent = data.getComponent(is64bit ? 3 : 2); // ri_gp_value value -> gp0
if (gpValueComponent != null) {
try {
- Scalar gpValue = gpValueComponent.getScalar(0);
- Address gpAddr = elfLoadHelper.getDefaultAddress(gpValue.getUnsignedValue());
- elfLoadHelper.createSymbol(gpAddr, MIPS_GP_VALUE_SYMBOL, false, false, null);
- elfLoadHelper.log(
- MIPS_GP_VALUE_SYMBOL + "=0x" + Long.toHexString(gpValue.getUnsignedValue()));
+ // Create gp0 symbol in default space which represents a constant value (pinned)
+ Scalar gp0Value = gpValueComponent.getScalar(0);
+ long gp0 = gp0Value.getUnsignedValue();
+ AddressSpace defaultSpace =
+ elfLoadHelper.getProgram().getAddressFactory().getDefaultAddressSpace();
+ Address gpAddr = defaultSpace.getAddress(gp0);
+ elfLoadHelper.createSymbol(gpAddr, MIPS_GP0_VALUE_SYMBOL, false, false,
+ null).setPinned(true);
+ elfLoadHelper.log(MIPS_GP0_VALUE_SYMBOL + "=0x" + Long.toHexString(gp0));
}
catch (InvalidInputException e) {
// ignore
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 87707a60ad..51500fd5fc 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
@@ -37,8 +37,6 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
// private static final int DTP_OFFSET = 0x8000;
// private static final String GOT_SYMBOL_NAME = "_GLOBAL_OFFSET_TABLE_";
- private static final String GP_DISP_SYMBOL_NAME = "_gp_disp";
- private static final String GP_GNU_LOCAL_SYMBOL_NAME = "__gnu_local_gp";
@Override
public boolean canRelocate(ElfHeader elf) {
@@ -159,13 +157,14 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
mipsRelocationContext.savedAddend = 0;
boolean isGpDisp = false;
- if (GP_DISP_SYMBOL_NAME.equals(symbolName)) {
+ if (MIPS_ElfExtension.MIPS_GP_DISP_SYMBOL_NAME.equals(symbolName)) {
isGpDisp = true;
}
- else if (GP_GNU_LOCAL_SYMBOL_NAME.equals(symbolName)) {
+ else if (MIPS_ElfExtension.MIPS_GP_GNU_LOCAL_SYMBOL_NAME.equals(symbolName)) {
// TODO: GP based relocation not yet supported - need to evaluate an example
markAsError(program, relocationAddress, Integer.toString(relocType), symbolName,
- GP_GNU_LOCAL_SYMBOL_NAME + " relocation not yet supported", log);
+ MIPS_ElfExtension.MIPS_GP_GNU_LOCAL_SYMBOL_NAME + " relocation not yet supported",
+ log);
if (saveValue) {
mipsRelocationContext.savedAddendHasError = true;
}
@@ -563,24 +562,47 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
break;
case MIPS_ElfRelocationConstants.R_MIPS_GPREL16:
+ case MIPS_ElfRelocationConstants.R_MIPS_GPREL32:
case MIPS_ElfRelocationConstants.R_MIPS16_GPREL:
case MIPS_ElfRelocationConstants.R_MICROMIPS_GPREL16:
case MIPS_ElfRelocationConstants.R_MICROMIPS_GPREL7_S2:
case MIPS_ElfRelocationConstants.R_MIPS_LITERAL:
case MIPS_ElfRelocationConstants.R_MICROMIPS_LITERAL:
if (mipsRelocationContext.extractAddend()) {
- if (relocType == MIPS_ElfRelocationConstants.R_MICROMIPS_GPREL7_S2) {
- addend = (oldValue & 0x7f0000) >> 14;
+ if (relocType == MIPS_ElfRelocationConstants.R_MIPS_GPREL32) {
+ addend = oldValue;
}
else {
addend = oldValue & 0xffff;
+ if (relocType == MIPS_ElfRelocationConstants.R_MICROMIPS_GPREL7_S2) {
+ addend <<= 2;
+ }
+ addend = signExtend((int) addend, 16);
+ }
+ }
+
+ long gp0 = 0;
+ if (elfSymbol.isLocal() &&
+ (relocType == MIPS_ElfRelocationConstants.R_MIPS_GPREL16 ||
+ relocType == MIPS_ElfRelocationConstants.R_MIPS_GPREL32)) {
+ gp0 = mipsRelocationContext.getGP0Value();
+ if (gp0 == -1) {
+ markAsError(mipsRelocationContext.getProgram(), relocationAddress,
+ Integer.toString(relocType), symbolName,
+ "Failed to perform local GP0-based relocation (requires .reginfo data)",
+ mipsRelocationContext.getLog());
+ if (saveValue) {
+ mipsRelocationContext.savedAddendHasError = true;
+ }
+ return;
+ }
+ if (gp0 == 0) {
+ gp0 = mipsRelocationContext.getImageBaseWordAdjustmentOffset();
}
- addend = signExtend((int) addend, 16);
}
long gp = mipsRelocationContext.getGPValue();
if (gp == -1) {
- // Unhandled GOT/GP case
markAsError(mipsRelocationContext.getProgram(), relocationAddress,
Integer.toString(relocType), symbolName,
"Failed to perform GP-based relocation", mipsRelocationContext.getLog());
@@ -590,24 +612,12 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
return;
}
- if (elfSymbol.isSection()) {
- // TODO: this computation has been completely fudged to get the desired results.
- // It is unclear why the addend is not needed or the 0x10 factor. This could
- // be specific to the test sample and may break for others. This may reflect
- // the offset from the start of the function which could easily vary and may break.
- value = (int) (offset - gp - 0x10); // value appears to be functionStart (t9) - gp, but we don't know where the function start is
- }
- else {
- value = (int) (symbolValue + addend - gp);
- }
+ value = (int) (symbolValue + addend - gp + gp0);
+
+ int mask =
+ relocType == MIPS_ElfRelocationConstants.R_MIPS_GPREL32 ? 0xffffffff : 0xffff;
+ newValue = (oldValue & ~mask) | (value & mask);
- // TODO: unsure if local symbol needs additional gp adjustment
- if (relocType == MIPS_ElfRelocationConstants.R_MICROMIPS_GPREL7_S2) {
- newValue = (oldValue & ~0x7f0000) | ((value & 0x7f) << 16);
- }
- else {
- newValue = (oldValue & ~0xffff) | (value & 0xffff);
- }
writeNewValue = true;
break;
@@ -1072,8 +1082,6 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
*/
public long getGPValue() {
- // TODO: 64-bit should really use .MIPS.options REGINFO.ri_gp_value in some capacity
-
long gp = getAdjustedGPValue();
if (gp == -1) {
@@ -1087,7 +1095,7 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
if (sectionGotAddress == Address.NO_ADDRESS) {
return -1;
}
- // gp if defined as 0x7ff0 byte offset into the global offset table
+ // gp is defined as 0x7ff0 byte offset into the global offset table
return sectionGotAddress.getOffset() + 0x7ff0;
}
@@ -1181,12 +1189,9 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
/**
* Get the GP value
- * @param mipsRelocationContext
* @return adjusted GP value or -1 if _mips_gp_value symbol not defined.
*/
- long getAdjustedGPValue(/* reloc_object */) {
-
- // TODO: should we try using reginfo and gp_value information if needed
+ long getAdjustedGPValue() {
// TODO: this is a simplified use of GP and could be incorrect when multiple GPs exist
@@ -1198,6 +1203,20 @@ public class MIPS_ElfRelocationHandler extends ElfRelocationHandler {
return symbol.getAddress().getOffset();
}
+ /**
+ * Get the GP0 value (from .reginfo and generated symbol)
+ * @param mipsRelocationContext
+ * @return adjusted GP0 value or -1 if _mips_gp0_value symbol not defined.
+ */
+ long getGP0Value() {
+ Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(program,
+ MIPS_ElfExtension.MIPS_GP0_VALUE_SYMBOL, err -> getLog().error("MIPS_ELF", err));
+ if (symbol == null) {
+ return -1;
+ }
+ return symbol.getAddress().getOffset();
+ }
+
@Override
public long getSymbolValue(ElfSymbol symbol) {
if ("__gnu_local_gp".equals(symbol.getNameAsString())) {