Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-4180 revised ELF section symbol extension support for MIPS

ghidra182c2716ba4b2
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.java
index 5f317e56ee..1e77400ce7 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.java
@@ -530,4 +530,29 @@ public class ElfLoadAdapter {
// no additional options
}
+ /**
+ * Get the section-relative offset for the specified ELF symbol which is bound to
+ * the specified section. If the symbol has an absolute symbol value/offset this method
+ * should return null.
+ * <p>
+ * For Harvard Architectures it may be necessary to adjust offset if section was mapped
+ * to a non-default data space.
+ * <p>
+ * The default behavior is to return {@link ElfSymbol#getValue()} if {@link ElfHeader#isRelocatable()}
+ * is true.
+ *
+ * @param section ELF section header which is specified by the ELF symbol
+ * @param sectionBase memory address where section has been loaded. Could be within overlay
+ * space if load conflict occured.
+ * @param elfSymbol ELF symbol
+ * @return section relative symbol offset or null if symbol value offset is absolute
+ */
+ public Long getSectionSymbolRelativeOffset(ElfSectionHeader section, Address sectionBase,
+ ElfSymbol elfSymbol) {
+ if (section.getElfHeader().isRelocatable()) {
+ return elfSymbol.getValue();
+ }
+ return null;
+ }
+
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.java
index ac86d5c57f..dd248ec24f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.java
@@ -99,8 +99,8 @@ public class ElfRelocationContext {
int symbolIndex = relocation.getSymbolIndex();
ElfSymbol sym = getSymbol(symbolIndex);
if (sym == null) {
- ElfRelocationHandler.markAsUnhandled(program, relocationAddress, relocation.getType(),
- symbolIndex, "index " + symbolIndex, getLog());
+ ElfRelocationHandler.markAsError(program, relocationAddress, relocation.getType(), null,
+ "invalid symbol index (" + symbolIndex + ")", getLog());
return RelocationResult.FAILURE;
}
if (sym.isTLS()) {
@@ -142,11 +142,12 @@ public class ElfRelocationContext {
if (!StringUtils.isBlank(symName)) {
nameMsg = " to: " + symName;
}
- program.getBookmarkManager().setBookmark(relocationAddress, BookmarkType.ERROR,
- "Relocation", "No handler to process ELF Relocation" + nameMsg);
+ program.getBookmarkManager()
+ .setBookmark(relocationAddress, BookmarkType.ERROR, "Relocation",
+ "No handler to process ELF Relocation" + nameMsg);
- loadHelper.log("WARNING: At " + relocationAddress +
- " no handler to process ELF Relocation" + nameMsg);
+ loadHelper.log(
+ "WARNING: At " + relocationAddress + " no handler to process ELF Relocation" + nameMsg);
}
/**
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 868caec25e..ee1e2a5a0d 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
@@ -1362,11 +1362,18 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {
private void allocateUndefinedSymbolData(HashMap<Address, Integer> dataAllocationMap) {
for (Address addr : dataAllocationMap.keySet()) {
+
+ MemoryBlock block = memory.getBlock(addr);
+ if (block == null) {
+ continue;
+ }
+
// Create undefined data for each data/object symbol
Integer symbolSize = dataAllocationMap.get(addr);
if (symbolSize != null) {
try {
- createUndefined(addr, symbolSize);
+ DataType undefined = Undefined.getUndefinedDataType(symbolSize);
+ listing.createData(addr, undefined);
}
catch (CodeUnitInsertionException e) {
// ignore conflicts which can be caused by other markup
@@ -1793,13 +1800,22 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {
symbolSpace = symSectionBase.getAddressSpace();
- if (elf.isRelocatable()) {
+ Long relOffset = loadAdapter.getSectionSymbolRelativeOffset(symSection,
+ symSectionBase, elfSymbol);
+ if (relOffset != null) {
// Section relative symbol - ensure that symbol remains in
// overlay space even if beyond bounds of associated block
- // Note: don't use symOffset variable since it may have been
- // adjusted for image base
- return symSectionBase.addWrapSpace(elfSymbol.getValue() *
- symSectionBase.getAddressSpace().getAddressableUnitSize());
+ try {
+ return symSectionBase.addNoWrap(
+ relOffset * symSectionBase.getAddressSpace().getAddressableUnitSize());
+ }
+ catch (AddressOverflowException e) {
+ log("Unable to place symbol within section (address overflow): " +
+ elfSymbol.getFormattedName() + " - value=0x" +
+ Long.toHexString(elfSymbol.getValue()) + ", section=" +
+ symSection.getNameAsString());
+ return null;
+ }
}
}
@@ -3550,16 +3566,6 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {
return StringUtilities.pad("" + value, ' ', 4);
}
- private Data createUndefined(Address addr, int size) throws CodeUnitInsertionException {
-
- MemoryBlock block = memory.getBlock(addr);
- if (block == null) {
- return null;
- }
- DataType undefined = Undefined.getUndefinedDataType(size);
- return listing.createData(addr, undefined);
- }
-
@Override
public Symbol createSymbol(Address addr, String name, boolean isPrimary, boolean pinAbsolute,
Namespace namespace) throws InvalidInputException {
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 a84833d103..4ba4dc4160 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
@@ -37,7 +37,7 @@ import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
public class MIPS_ElfExtension extends ElfExtension {
-
+
private static final String MIPS_STUBS_SECTION_NAME = ".MIPS.stubs";
// GP value reflected by symbol address
@@ -46,6 +46,9 @@ public class MIPS_ElfExtension extends ElfExtension {
public static final String MIPS_GP_VALUE_SYMBOL = "_mips_gp_value";
public static final String MIPS_GP0_VALUE_SYMBOL = "_mips_gp0_value";
+ // Elf Header - File Type
+ public static final short ET_MIPS_PSP_PRX = (short) 0xffa0;
+
// Elf Program Header Extensions
public static final ElfProgramHeaderType PT_MIPS_REGINFO = new ElfProgramHeaderType(0x70000000,
"PT_MIPS_REGINFO", "Register usage information. Identifies one .reginfo section");
@@ -56,6 +59,18 @@ public class MIPS_ElfExtension extends ElfExtension {
public static final ElfProgramHeaderType PT_MIPS_ABIFLAGS =
new ElfProgramHeaderType(0x70000003, "PT_MIPS_ABIFLAGS", "Records ABI related flags");
+ // PT_MIPS_PSPREL1 relocation format not supported (does not link to symbol table):
+ // type = r_info & 0xf, readwrite = (r_info >> 8) & 0xff, relative = (r_info >> 16) & 0xff
+ // Uses Elf32_Rel but relocation processing differs from standard MIPS relocation handler
+ // see https://github.com/hrydgard/ppsspp/blob/master/Core/ELF/ElfReader.cpp
+ public static final ElfProgramHeaderType PT_MIPS_PSPREL1 =
+ new ElfProgramHeaderType(0x700000a0, "PT_MIPS_PSPREL1", "PSP relocation table");
+
+ // PT_MIPS_PSPREL2 relocation format not supported (does not link to symbol table, non-standard format):
+ // see https://github.com/hrydgard/ppsspp/blob/master/Core/ELF/ElfReader.cpp
+ public static final ElfProgramHeaderType PT_MIPS_PSPREL2 =
+ new ElfProgramHeaderType(0x700000a1, "PT_MIPS_PSPREL2", "PSP relocation table");
+
// Elf Section Header Extensions
public static final ElfSectionHeaderType SHT_MIPS_LIBLIST =
new ElfSectionHeaderType(0x70000000, "SHT_MIPS_LIBLIST",
@@ -145,6 +160,12 @@ public class MIPS_ElfExtension extends ElfExtension {
public static final ElfSectionHeaderType SHT_MIPS_ABIFLAGS =
new ElfSectionHeaderType(0x7000002a, "SHT_MIPS_ABIFLAGS", "ABI related flags section");
+ // SHT_MIPS_PSPREL relocation format not supported (does not link to symbol table, uses Elf32_Rel):
+ // type = r_info & 0xf, readwrite = (r_info >> 8) & 0xff, relative = (r_info >> 16) & 0xff
+ // see https://github.com/hrydgard/ppsspp/blob/master/Core/ELF/ElfReader.cpp
+ public static final ElfSectionHeaderType SHT_MIPS_PSPREL =
+ new ElfSectionHeaderType(0x700000a0, "SHT_MIPS_PSPREL", "PSP relocation table"); // relocations not supported
+
// Elf Dynamic Type Extensions
public static final ElfDynamicType DT_MIPS_RLD_VERSION =
new ElfDynamicType(0x70000001, "DT_MIPS_RLD_VERSION",
@@ -325,8 +346,9 @@ public class MIPS_ElfExtension extends ElfExtension {
if ((functionAddress.getOffset() & 1) != 0) {
functionAddress = functionAddress.previous(); // align address
try {
- program.getProgramContext().setValue(isaModeRegister, functionAddress,
- functionAddress, BigInteger.ONE);
+ program.getProgramContext()
+ .setValue(isaModeRegister, functionAddress, functionAddress,
+ BigInteger.ONE);
}
catch (ContextChangeException e) {
// ignore since should not be instructions at time of import
@@ -344,16 +366,18 @@ public class MIPS_ElfExtension extends ElfExtension {
}
short sectionIndex = elfSymbol.getSectionHeaderIndex();
- if (sectionIndex == SHN_MIPS_ACOMMON || sectionIndex == SHN_MIPS_TEXT || sectionIndex == SHN_MIPS_DATA) {
+ if (sectionIndex == SHN_MIPS_ACOMMON || sectionIndex == SHN_MIPS_TEXT ||
+ sectionIndex == SHN_MIPS_DATA) {
// NOTE: logic assumes no memory conflict occured during section loading
- AddressSpace defaultSpace = elfLoadHelper.getProgram().getAddressFactory().getDefaultAddressSpace();
- return defaultSpace.getAddress(elfSymbol.getValue() + elfLoadHelper.getImageBaseWordAdjustmentOffset());
+ AddressSpace defaultSpace =
+ elfLoadHelper.getProgram().getAddressFactory().getDefaultAddressSpace();
+ return defaultSpace.getAddress(
+ elfSymbol.getValue() + elfLoadHelper.getImageBaseWordAdjustmentOffset());
}
return null;
}
-
@Override
public Address evaluateElfSymbol(ElfLoadHelper elfLoadHelper, ElfSymbol elfSymbol,
Address address, boolean isExternal) {
@@ -404,8 +428,8 @@ public class MIPS_ElfExtension extends ElfExtension {
if (enableISA) {
try {
- program.getProgramContext().setValue(isaModeRegister, address, address,
- BigInteger.ONE);
+ program.getProgramContext()
+ .setValue(isaModeRegister, address, address, BigInteger.ONE);
}
catch (ContextChangeException e) {
// ignore since should not be instructions at time of import
@@ -683,7 +707,6 @@ public class MIPS_ElfExtension extends ElfExtension {
// NOTES: assumes only one gp0 value
-
AtomicBoolean multipleGp0 = new AtomicBoolean(false);
Symbol gp0Sym = SymbolUtilities.getLabelOrFunctionSymbol(elfLoadHelper.getProgram(),
MIPS_GP0_VALUE_SYMBOL, msg -> multipleGp0.set(true));
@@ -703,16 +726,15 @@ public class MIPS_ElfExtension extends ElfExtension {
long gp0 = gp0Value.getUnsignedValue();
if (multipleGp0.get() || otherGp0Value != null) {
if (multipleGp0.get() || gp0 != otherGp0Value) {
- elfLoadHelper.log(
- "Multiple gp0 values defined (not supported): 0x" +
- Long.toHexString(gp0));
+ elfLoadHelper.log("Multiple gp0 values defined (not supported): 0x" +
+ Long.toHexString(gp0));
}
return;
}
Address gpAddr = defaultSpace.getAddress(gp0);
- elfLoadHelper.createSymbol(gpAddr, MIPS_GP0_VALUE_SYMBOL, false, false,
- null).setPinned(true);
+ 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) {
@@ -822,7 +844,8 @@ public class MIPS_ElfExtension extends ElfExtension {
}
}
- private void fixupMipsGot(ElfLoadHelper elfLoadHelper, TaskMonitor monitor) throws CancelledException {
+ private void fixupMipsGot(ElfLoadHelper elfLoadHelper, TaskMonitor monitor)
+ throws CancelledException {
ElfHeader elfHeader = elfLoadHelper.getElfHeader();
ElfDynamicTable dynamicTable = elfHeader.getDynamicTable();
@@ -877,8 +900,7 @@ public class MIPS_ElfExtension extends ElfExtension {
}
private Address adjustTableEntryIfNonZero(Address tableBaseAddr, int entryIndex,
- long adjustment, ElfLoadHelper elfLoadHelper)
- throws MemoryAccessException {
+ long adjustment, ElfLoadHelper elfLoadHelper) throws MemoryAccessException {
boolean is64Bit = elfLoadHelper.getElfHeader().is64Bit();
Memory memory = elfLoadHelper.getProgram().getMemory();
Address tableEntryAddr;
@@ -939,4 +961,19 @@ public class MIPS_ElfExtension extends ElfExtension {
return super.getRelocationClass(elfHeader);
}
+ @Override
+ public Long getSectionSymbolRelativeOffset(ElfSectionHeader symSection, Address symSectionBase,
+ ElfSymbol elfSymbol) {
+
+ // NOTE: PSP PRX files should really be wired to ElfHeader.isRelocatable(), however we do
+ // not support the associated relocation tables so we do offer the image base option
+ // during import. If image base should be changed we leave that to a user script to change
+ // the image base and process the relocation tables as needed.
+ if (symSection.getElfHeader().e_type() == ET_MIPS_PSP_PRX) {
+ return elfSymbol.getValue();
+ }
+
+ return super.getSectionSymbolRelativeOffset(symSection, symSectionBase, elfSymbol);
+ }
+
}