Ghidra is a software reverse engineering (SRE) framework
GP-4118 Corrected failure caused by bad ELF GOT allocation during x86-64 object module import (Closes #5961)
ghidra19b53f5801008
diff --git a/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationContext.java b/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationContext.javaindex c34eb7bc93..8ca1f6dadb 100644--- a/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationContext.java+++ b/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationContext.java@@ -87,7 +87,7 @@ class X86_64_ElfRelocationContext extends ElfRelocationContext {// NOTE: GOT allocation calculation assumes all GOT entries correspond to a specific// symbol and not a computed offset. This assumption may need to be revised based upon// uses of getGotEntryAddress method- Set<Object> uniqueSymbolValues = new HashSet<>();+ Set<Long> uniqueSymbolValues = new HashSet<>();for (ElfRelocationTable rt : getElfHeader().getRelocationTables()) {ElfSymbolTable st = rt.getAssociatedSymbolTable();if (st == null) {@@ -102,9 +102,11 @@ class X86_64_ElfRelocationContext extends ElfRelocationContext {if (elfSymbol == null) {continue;}- long value = elfSymbol.getValue();- Object uniqueValue = value == 0 ? elfSymbol.getNameAsString() : Long.valueOf(value);- uniqueSymbolValues.add(uniqueValue);+ long symbolValue = getSymbolValue(elfSymbol);+ if (!uniqueSymbolValues.add(symbolValue)) {+ System.out.println("Duplicate sym value 0x" + Long.toHexString(symbolValue) ++ " for " + elfSymbol.getNameAsString());+ }}}return Math.max(8, uniqueSymbolValues.size() * 8);@@ -113,13 +115,13 @@ class X86_64_ElfRelocationContext extends ElfRelocationContext {private boolean requiresGotEntry(ElfRelocation r) {switch (r.getType()) {case X86_64_ElfRelocationConstants.R_X86_64_GOTPCREL:- case X86_64_ElfRelocationConstants.R_X86_64_GOTOFF64:- case X86_64_ElfRelocationConstants.R_X86_64_GOTPC32:- case X86_64_ElfRelocationConstants.R_X86_64_GOT64:+// case X86_64_ElfRelocationConstants.R_X86_64_GOTOFF64:+// case X86_64_ElfRelocationConstants.R_X86_64_GOTPC32:+// case X86_64_ElfRelocationConstants.R_X86_64_GOT64:case X86_64_ElfRelocationConstants.R_X86_64_GOTPCREL64:- case X86_64_ElfRelocationConstants.R_X86_64_GOTPC64:- case X86_64_ElfRelocationConstants.R_X86_64_GOTPCRELX:- case X86_64_ElfRelocationConstants.R_X86_64_REX_GOTPCRELX:+// case X86_64_ElfRelocationConstants.R_X86_64_GOTPC64:+// case X86_64_ElfRelocationConstants.R_X86_64_GOTPCRELX:+// case X86_64_ElfRelocationConstants.R_X86_64_REX_GOTPCRELX:return true;default:return false;@@ -134,8 +136,8 @@ class X86_64_ElfRelocationContext extends ElfRelocationContext {ElfSymbol gotElfSymbol = findGotElfSymbol();if (gotElfSymbol == null && !getElfHeader().isRelocatable()) {- loadHelper.log(- "GOT allocatiom failed. " + ElfConstants.GOT_SYMBOL_NAME + " not defined");+ loadHelper+ .log("GOT allocatiom failed. " + ElfConstants.GOT_SYMBOL_NAME + " not defined");return null;}@@ -144,9 +146,9 @@ class X86_64_ElfRelocationContext extends ElfRelocationContext {}int alignment = getLoadAdapter().getLinkageBlockAlignment();- allocatedGotLimits =- getLoadHelper().allocateLinkageBlock(alignment, computeRequiredGotSize(),- ElfRelocationHandler.GOT_BLOCK_NAME);+ int gotSize = computeRequiredGotSize();+ allocatedGotLimits = getLoadHelper().allocateLinkageBlock(alignment, gotSize,+ ElfRelocationHandler.GOT_BLOCK_NAME);if (allocatedGotLimits != null &&allocatedGotLimits.getMinAddress().getOffset() < Integer.MAX_VALUE) {// GOT must fall within first 32-bit segment@@ -207,10 +209,11 @@ class X86_64_ElfRelocationContext extends ElfRelocationContext {/*** Get or allocate a GOT entry for the specified symbolValue.* NOTE: This is restricted to object modules only which do not of a GOT.- * @param symbolValue symbol value+ * @param elfSymbol ELF symbol* @return GOT entry address or null if unable to allocate*/- public Address getGotEntryAddress(long symbolValue) {+ public Address getGotEntryAddress(ElfSymbol elfSymbol) {+ long symbolValue = getSymbolValue(elfSymbol);Address addr = null;if (gotMap != null) {addr = gotMap.get(symbolValue);@@ -242,13 +245,7 @@ class X86_64_ElfRelocationContext extends ElfRelocationContext {: LittleEndianDataConverter.INSTANCE;for (long symbolValue : gotMap.keySet()) {Address addr = gotMap.get(symbolValue);- byte[] bytes;- if (program.getDefaultPointerSize() == 4) {- bytes = converter.getBytes((int) symbolValue);- }- else {- bytes = converter.getBytes(symbolValue);- }+ byte[] bytes = converter.getBytes(symbolValue); // 8-byte pointer valueblock.putBytes(addr, bytes);loadHelper.createData(addr, PointerDataType.dataType);}diff --git a/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationHandler.java b/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationHandler.javaindex 2992c45601..c480a8ff01 100644--- a/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationHandler.java+++ b/Ghidra/Processors/x86/src/main/java/ghidra/app/util/bin/format/elf/relocation/X86_64_ElfRelocationHandler.java@@ -44,8 +44,10 @@ public class X86_64_ElfRelocationHandler extends ElfRelocationHandler {return new X86_64_ElfRelocationContext(this, loadHelper, symbolMap);}+ @Overridepublic RelocationResult relocate(ElfRelocationContext elfRelocationContext,- ElfRelocation relocation, Address relocationAddress) throws MemoryAccessException, NotFoundException {+ ElfRelocation relocation, Address relocationAddress)+ throws MemoryAccessException, NotFoundException {ElfHeader elf = elfRelocationContext.getElfHeader();if (elf.e_machine() != ElfConstants.EM_X86_64) {@@ -89,8 +91,8 @@ public class X86_64_ElfRelocationHandler extends ElfRelocationHandler {value = symbolValue + addend;memory.setLong(relocationAddress, value);if (symbolIndex != 0 && addend != 0 && !sym.isSection()) {- warnExternalOffsetRelocation(program, relocationAddress,- symbolAddr, symbolName, addend, elfRelocationContext.getLog());+ warnExternalOffsetRelocation(program, relocationAddress, symbolAddr, symbolName,+ addend, elfRelocationContext.getLog());applyComponentOffsetPointer(program, relocationAddress, addend);}break;@@ -205,7 +207,6 @@ public class X86_64_ElfRelocationHandler extends ElfRelocationHandler {}break;-case X86_64_ElfRelocationConstants.R_X86_64_GOTPCRELX:case X86_64_ElfRelocationConstants.R_X86_64_REX_GOTPCRELX:@@ -234,7 +235,8 @@ public class X86_64_ElfRelocationHandler extends ElfRelocationHandler {if (modRM == (byte) 0x25) { // check for indirect JMP op// convert to direct JMP op// must compensate for shorter instruction by appending NOP- elfRelocationContext.getLoadHelper().addArtificialRelocTableEntry(opAddr, 2);+ elfRelocationContext.getLoadHelper()+ .addArtificialRelocTableEntry(opAddr, 2);memory.setByte(opAddr, (byte) 0xe9); // direct JMP opmemory.setByte(relocationAddress.add(3), (byte) 0x90); // append NOPdirectValueAddr = modRMAddr;@@ -243,7 +245,8 @@ public class X86_64_ElfRelocationHandler extends ElfRelocationHandler {else if (modRM == (byte) 0x15) { // check for indirect CALL instruction// convert to direct CALL instruction// use of addr32 prefix allows use of single instruction- elfRelocationContext.getLoadHelper().addArtificialRelocTableEntry(opAddr, 2);+ elfRelocationContext.getLoadHelper()+ .addArtificialRelocTableEntry(opAddr, 2);memory.setByte(opAddr, (byte) 0x67); // addr32 prefixmemory.setByte(modRMAddr, (byte) 0xe8); // direct CALL opdirectValueAddr = relocationAddress;@@ -260,7 +263,7 @@ public class X86_64_ElfRelocationHandler extends ElfRelocationHandler {// Let R_X86_64_GOTPCREL case handle as simple GOTPCREL relocation.case X86_64_ElfRelocationConstants.R_X86_64_GOTPCREL:- Address symbolGotAddress = x86RelocationContext.getGotEntryAddress(symbolValue);+ Address symbolGotAddress = x86RelocationContext.getGotEntryAddress(sym);if (symbolGotAddress == null) {markAsError(program, relocationAddress, type, symbolName,"GOT allocation failure", elfRelocationContext.getLog());@@ -272,7 +275,7 @@ public class X86_64_ElfRelocationHandler extends ElfRelocationHandler {break;case X86_64_ElfRelocationConstants.R_X86_64_GOTPCREL64:- symbolGotAddress = x86RelocationContext.getGotEntryAddress(symbolValue);+ symbolGotAddress = x86RelocationContext.getGotEntryAddress(sym);if (symbolGotAddress == null) {markAsError(program, relocationAddress, "R_X86_64_GOTPCREL64", symbolName,"GOT allocation failure", elfRelocationContext.getLog());