Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-4158 adjust how DWARF handles Rust

dev747368d9a900702e83
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java
index 941a76de47..770d4f07b1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java
@@ -17,6 +17,7 @@ package ghidra.app.plugin.core.analysis;
import java.io.IOException;
+import ghidra.app.plugin.core.analysis.rust.RustUtilities;
import ghidra.app.services.*;
import ghidra.app.util.bin.format.dwarf4.DWARFException;
import ghidra.app.util.bin.format.dwarf4.DWARFPreconditionException;
@@ -99,6 +100,11 @@ public class DWARFAnalyzer extends AbstractAnalyzer {
importOptions.setPreloadAllDIEs(true);
}
+ if (RustUtilities.isRustProgram(program)) {
+ Msg.info(this, "DWARF: Enabling DIE preload for Rust binary");
+ importOptions.setPreloadAllDIEs(true);
+ }
+
try {
try (DWARFProgram prog = new DWARFProgram(program, importOptions, monitor, dsp)) {
if (prog.getRegisterMappings() == null && importOptions.isImportFuncs()) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/RustUtilities.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/RustUtilities.java
index add3b4ba8e..08e5e29f13 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/RustUtilities.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/RustUtilities.java
@@ -58,6 +58,18 @@ public class RustUtilities {
return false;
}
+ /**
+ * Returns true if the given program has earlier been tagged as having a Rust compiler by
+ * the loader.
+ *
+ * @param program {@link Program}
+ * @return boolean true if program's compiler property includes rust
+ */
+ public static boolean isRustProgram(Program program) {
+ String name = program.getCompiler();
+ return name != null && name.contains(RustConstants.RUST_COMPILER);
+ }
+
public static int addExtensions(Program program, TaskMonitor monitor, String subPath)
throws IOException {
Processor processor = program.getLanguageCompilerSpecPair().getLanguage().getProcessor();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/demangler/RustDemangler.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/demangler/RustDemangler.java
index 322763a8d7..2deb5abb2c 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/demangler/RustDemangler.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/rust/demangler/RustDemangler.java
@@ -15,7 +15,7 @@
*/
package ghidra.app.plugin.core.analysis.rust.demangler;
-import ghidra.app.plugin.core.analysis.rust.RustConstants;
+import ghidra.app.plugin.core.analysis.rust.RustUtilities;
import ghidra.app.util.demangler.*;
import ghidra.program.model.lang.CompilerSpec;
import ghidra.program.model.listing.Program;
@@ -36,8 +36,7 @@ public class RustDemangler implements Demangler {
@Override
public boolean canDemangle(Program program) {
- String name = program.getCompiler();
- return name != null && name.contains(RustConstants.RUST_COMPILER);
+ return RustUtilities.isRustProgram(program);
}
@Override
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/funcfixup/RustDWARFFunctionFixup.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/funcfixup/RustDWARFFunctionFixup.java
index e117f8d914..98e0f3e8db 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/funcfixup/RustDWARFFunctionFixup.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/funcfixup/RustDWARFFunctionFixup.java
@@ -15,28 +15,51 @@
*/
package ghidra.app.util.bin.format.dwarf4.funcfixup;
+import java.io.IOException;
+
import ghidra.app.util.bin.format.dwarf4.DIEAggregate;
import ghidra.app.util.bin.format.dwarf4.DWARFException;
import ghidra.app.util.bin.format.dwarf4.encoding.DWARFSourceLanguage;
import ghidra.app.util.bin.format.dwarf4.next.DWARFFunction;
+import ghidra.app.util.bin.format.dwarf4.next.DWARFFunction.CommitMode;
+import ghidra.program.database.data.ProgramBasedDataTypeManagerDB;
+import ghidra.program.model.lang.CompilerSpec;
+import ghidra.program.model.listing.Program;
import ghidra.util.classfinder.ExtensionPointProperties;
+import ghidra.util.exception.InvalidInputException;
/**
- * Prevent functions in a Rust compile unit from incorrectly being locked down to an empty signature.
+ * Adjust functions in a Rust compile unit to use Rust calling convention, ignore any information
+ * about parameter storage locations.
*/
@ExtensionPointProperties(priority = DWARFFunctionFixup.PRIORITY_NORMAL_EARLY)
public class RustDWARFFunctionFixup implements DWARFFunctionFixup {
+ private String rustCC;
@Override
public void fixupDWARFFunction(DWARFFunction dfunc) throws DWARFException {
DIEAggregate diea = dfunc.diea;
int cuLang = diea.getCompilationUnit().getCompileUnit().getLanguage();
- if (cuLang == DWARFSourceLanguage.DW_LANG_Rust && dfunc.params.isEmpty()) {
- // if there were no defined parameters and the language is Rust, don't force an
- // empty param signature. Rust language emit dwarf info without types (signatures)
- // when used without -g.
- throw new DWARFException("Rust empty param list" /* string doesnt matter */);
+ if (cuLang == DWARFSourceLanguage.DW_LANG_Rust) {
+ dfunc.callingConventionName = getRustCC(dfunc.getProgram().getGhidraProgram());
+ dfunc.signatureCommitMode = CommitMode.FORMAL;
}
+
}
+ private String getRustCC(Program program) throws DWARFException {
+ if (rustCC == null) {
+ rustCC = CompilerSpec.CALLING_CONVENTION_rustcall;
+ try {
+ // NOTE: this has a side effect of ensuring the rust cc is present in the program
+ ProgramBasedDataTypeManagerDB dtm =
+ (ProgramBasedDataTypeManagerDB) program.getDataTypeManager();
+ dtm.getCallingConventionID(CompilerSpec.CALLING_CONVENTION_rustcall, false);
+ }
+ catch (InvalidInputException | IOException e) {
+ throw new DWARFException("Unable to get Rust calling convention");
+ }
+ }
+ return rustCC;
+ }
}
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 9905f1a9c9..332911d4c7 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
@@ -479,6 +479,14 @@ public class DWARFProgram implements Closeable {
String workingName = ensureSafeNameLength(name);
workingName = GoSymbolName.fixGolangSpecialSymbolnameChars(workingName);
+ if (diea.getCompilationUnit()
+ .getCompileUnit()
+ .getLanguage() == DWARFSourceLanguage.DW_LANG_Rust &&
+ workingName.startsWith("{impl#") && parentDNI != null) {
+ // if matches a Rust {impl#NN} name, skip it and re-use the parent name
+ return parentDNI;
+ }
+
DWARFNameInfo result =
parentDNI.createChild(origName, workingName, DWARFUtil.getSymbolTypeFromDIE(diea));
return result;