Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-2963 - fix DWARF to handle programs with changed base address.

dev747368ced97b941c16
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.java
index 3166867a17..e1f5e5dd1c 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.java
@@ -854,7 +854,8 @@ public class DIEAggregate {
public long getLowPC(long defaultValue) {
DWARFNumericAttribute attr =
getAttribute(DWARFAttribute.DW_AT_low_pc, DWARFNumericAttribute.class);
- return (attr != null) ? attr.getUnsignedValue() : defaultValue;
+ return (attr != null) ? attr.getUnsignedValue() + getProgram().getProgramBaseAddressFixup()
+ : defaultValue;
}
/**
@@ -872,14 +873,15 @@ public class DIEAggregate {
// if the DWARF attr was a DW_FORM_addr, it doesn't need fixing up
if (high.form == DWARFForm.DW_FORM_addr) {
- return highVal.getUnsignedValue();
+ return highVal.getUnsignedValue() + getProgram().getProgramBaseAddressFixup();
}
// else it was a DW_FORM_data value and is relative to the lowPC value
DWARFNumericAttribute low =
getAttribute(DWARFAttribute.DW_AT_low_pc, DWARFNumericAttribute.class);
if (low != null && highVal.getUnsignedValue() > 0) {
- return low.getUnsignedValue() + highVal.getUnsignedValue() - 1;
+ return low.getUnsignedValue() + getProgram().getProgramBaseAddressFixup() +
+ highVal.getUnsignedValue() - 1;
}
}
throw new IOException("Bad/unsupported DW_AT_high_pc attribute value or type");
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java
index 9aa4f9e101..99406a1f79 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java
@@ -34,6 +34,7 @@ import ghidra.program.model.data.Enum;
import ghidra.program.model.lang.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.listing.Function.FunctionUpdateType;
+import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.pcode.Varnode;
import ghidra.program.model.symbol.*;
import ghidra.program.model.util.CodeUnitInsertionException;
@@ -437,7 +438,7 @@ public class DWARFFunctionImporter {
dvar.dni = dvar.dni.replaceType(null /*nothing matches static global var*/);
if (res != 0) {
// If the expression evaluated to a static address other than '0'
- Address staticVariableAddresss = toAddr(res);
+ Address staticVariableAddresss = toAddr(res + prog.getProgramBaseAddressFixup());
if (variablesProcesesed.contains(staticVariableAddresss)) {
return null;
}
@@ -445,7 +446,7 @@ public class DWARFFunctionImporter {
boolean external = diea.getBool(DWARFAttribute.DW_AT_external, false);
DataType storageType = dwarfDTM.getStorageDataType(diea.getTypeRef(), dvar.type);
- outputGlobal(staticVariableAddresss, storageType, external,
+ outputGlobal(staticVariableAddresss, dvar.type, storageType, external,
DWARFSourceInfo.create(diea), dvar.dni);
}
else {
@@ -749,8 +750,14 @@ public class DWARFFunctionImporter {
return false;
}
- private Data createVariable(Address address, DataType dataType, DWARFNameInfo dni) {
+ private Data createVariable(Address address, DataType origDataType, DataType dataType,
+ DWARFNameInfo dni) {
try {
+ MemoryBlock block = currentProgram.getMemory().getBlock(address);
+ if (dataType.getLength() <= 0 && !block.isInitialized()) {
+ // fall back to the original data type, which shouldn't have any dynamic sized (ie. string) data types
+ dataType = origDataType;
+ }
if (dataType.getLength() < 0) {
Data result = DataUtilities.createData(currentProgram, address, dataType, -1, false,
ClearDataMode.CLEAR_ALL_UNDEFINED_CONFLICT_DATA);
@@ -777,8 +784,8 @@ public class DWARFFunctionImporter {
return null;
}
- private void outputGlobal(Address address, DataType baseDataType, boolean external,
- DWARFSourceInfo sourceInfo, DWARFNameInfo dni) {
+ private void outputGlobal(Address address, DataType origDataType, DataType baseDataType,
+ boolean external, DWARFSourceInfo sourceInfo, DWARFNameInfo dni) {
Namespace namespace = dni.getParentNamespace(currentProgram);
@@ -796,7 +803,7 @@ public class DWARFFunctionImporter {
setExternalEntryPoint(external, address);
- Data varData = createVariable(address, baseDataType, dni);
+ Data varData = createVariable(address, origDataType, baseDataType, dni);
importSummary.globalVarsAdded++;
if (sourceInfo != null) {
@@ -1096,7 +1103,7 @@ public class DWARFFunctionImporter {
}
}
- private Function createFunction(DWARFFunction dfunc) throws DuplicateNameException {
+ private Function createFunction(DWARFFunction dfunc) {
try {
// create a new symbol if one does not exist (symbol table will figure this out)
SymbolTable symbolTable = currentProgram.getSymbolTable();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java
index a8a6397f6e..bd43b25aed 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java
@@ -97,6 +97,7 @@ public class DWARFProgram implements Closeable {
private int totalDIECount = -1;
private int totalAggregateCount;
private boolean foundCrossCURefs = false;
+ private long programBaseAddressFixup;
private int maxDNICacheSize = 50;
private FixedSizeHashMap<Long, DWARFNameInfo> dniCache =
@@ -184,6 +185,11 @@ public class DWARFProgram implements Closeable {
this.importOptions = importOptions;
this.nameLengthCutoffSize = Math.max(MIN_NAME_LENGTH_CUTOFF,
Math.min(importOptions.getNameLengthCutoff(), MAX_NAME_LENGTH_CUTOFF));
+ Long oib = ElfLoader.getElfOriginalImageBase(program);
+ if (oib != null && oib.longValue() != program.getImageBase().getOffset()) {
+ this.programBaseAddressFixup = program.getImageBase().getOffset() - oib.longValue();
+ }
+
monitor.setMessage("Reading DWARF debug string table");
this.debugStrings = StringTable.readStringTable(
sectionProvider.getSectionAsByteProvider(DWARFSectionNames.DEBUG_STR));
@@ -897,4 +903,16 @@ public class DWARFProgram implements Closeable {
public void setNameLengthCutoff(int nameLenCutoff) {
this.nameLengthCutoffSize = nameLenCutoff;
}
+
+ /**
+ * A fixup value that needs to be applied to static addresses of the program.
+ * <p>
+ * This value is necessary if the program's built-in base address is overridden at import time.
+ * <p>
+ * @return long value to add to static addresses discovered in DWARF to make it agree with
+ * Ghidra's imported program.
+ */
+ public long getProgramBaseAddressFixup() {
+ return programBaseAddressFixup;
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java
index 4b0f60c406..61928a6c43 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java
@@ -28,12 +28,12 @@ import ghidra.app.util.bin.format.elf.ElfHeader;
import ghidra.app.util.importer.*;
import ghidra.framework.model.DomainFolder;
import ghidra.framework.model.DomainObject;
+import ghidra.framework.options.Options;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.lang.Endian;
import ghidra.program.model.listing.Program;
import ghidra.program.util.ELFExternalSymbolResolver;
-import ghidra.util.Msg;
-import ghidra.util.StringUtilities;
+import ghidra.util.*;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitor;
@@ -53,6 +53,18 @@ public class ElfLoader extends AbstractLibrarySupportLoader {
public final static String ELF_REQUIRED_LIBRARY_PROPERTY_PREFIX = "ELF Required Library ["; // followed by "#]"
public final static String ELF_SOURCE_FILE_PROPERTY_PREFIX = "ELF Source File ["; // followed by "#]"
+ /**
+ * Getter for the {@link #ELF_ORIGINAL_IMAGE_BASE_PROPERTY} property.
+ *
+ * @param program Ghidra program that has the property to get
+ * @return Long value of the original image base, or null if the property is not present
+ */
+ public static Long getElfOriginalImageBase(Program program) {
+ Options props = program.getOptions(Program.PROGRAM_INFO);
+ String oibStr = props.getString(ElfLoader.ELF_ORIGINAL_IMAGE_BASE_PROPERTY, null);
+ return (oibStr != null) ? NumericUtilities.parseHexLong(oibStr) : null;
+ }
+
public ElfLoader() {
}