mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 02:09:44 +02:00
GT-3481 - Gnu Demangler - Checkpoint 2 - Most docs and tests done;
analyzer test remains
This commit is contained in:
parent
b774ecb2d6
commit
b4ce3012d7
55 changed files with 854 additions and 1283 deletions
|
@ -19,17 +19,10 @@
|
|||
|
||||
//
|
||||
//@category Examples.Demangler
|
||||
import java.io.*;
|
||||
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.app.util.demangler.DemangledObject;
|
||||
import ghidra.app.util.demangler.DemanglerOptions;
|
||||
import ghidra.app.util.demangler.gnu.GnuDemanglerNativeProcess;
|
||||
import ghidra.app.util.demangler.gnu.GnuDemanglerParser;
|
||||
import ghidra.app.util.opinion.ElfLoader;
|
||||
import ghidra.app.util.opinion.MachoLoader;
|
||||
import ghidra.framework.*;
|
||||
import ghidra.program.model.lang.CompilerSpec;
|
||||
import ghidra.app.util.demangler.gnu.GnuDemangler;
|
||||
import ghidra.app.util.demangler.gnu.GnuDemanglerOptions;
|
||||
import ghidra.program.model.symbol.Symbol;
|
||||
|
||||
public class DemangleElfWithOptionScript extends GhidraScript {
|
||||
|
@ -37,8 +30,9 @@ public class DemangleElfWithOptionScript extends GhidraScript {
|
|||
@Override
|
||||
protected void run() throws Exception {
|
||||
|
||||
String executableFormat = currentProgram.getExecutableFormat();
|
||||
if (!canDemangle(executableFormat)) {
|
||||
GnuDemangler demangler = new GnuDemangler();
|
||||
if (!demangler.canDemangle(currentProgram)) {
|
||||
String executableFormat = currentProgram.getExecutableFormat();
|
||||
println("Cannot use the elf demangling options for executable format: " +
|
||||
executableFormat);
|
||||
return;
|
||||
|
@ -55,81 +49,22 @@ public class DemangleElfWithOptionScript extends GhidraScript {
|
|||
|
||||
String mangled = symbol.getName();
|
||||
|
||||
Process process = createProcess(executableFormat);
|
||||
GnuDemanglerOptions options = new GnuDemanglerOptions();
|
||||
options.setDoDisassembly(false);
|
||||
options.setDemanglerApplicationArguments("-s auto");
|
||||
|
||||
InputStream in = process.getInputStream();
|
||||
OutputStream out = process.getOutputStream();
|
||||
/*
|
||||
// for older formats use the deprecated demangler
|
||||
options.setDemanglerName(GnuDemanglerOptions.GNU_DEMANGLER_V2_24);
|
||||
options.setDemanglerApplicationArguments("-s arm");
|
||||
*/
|
||||
|
||||
BufferedReader input = new BufferedReader(new InputStreamReader(in));
|
||||
PrintWriter output = new PrintWriter(out);
|
||||
|
||||
output.println(mangled);
|
||||
output.flush();
|
||||
String demangled = input.readLine();
|
||||
println("demangled: " + demangled);
|
||||
|
||||
GnuDemanglerParser parser = new GnuDemanglerParser(null);
|
||||
DemangledObject demangledObject = parser.parse(mangled, demangled);
|
||||
DemangledObject demangledObject = demangler.demangle(mangled, options);
|
||||
if (demangledObject == null) {
|
||||
println("Could not demangle: " + mangled);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO change to GnuDemanglerOptions
|
||||
DemanglerOptions options = new DemanglerOptions();
|
||||
options.setDoDisassembly(false);
|
||||
options.setApplySignature(true);
|
||||
options.setDemangleOnlyKnownPatterns(true);
|
||||
|
||||
if (!demangledObject.applyTo(currentProgram, currentAddress, options, monitor)) {
|
||||
println("Failed to apply demangled data for " + mangled);
|
||||
}
|
||||
println("Succesfully demangled " + mangled + " to " + demangled);
|
||||
}
|
||||
|
||||
private boolean canDemangle(String executableFormat) {
|
||||
|
||||
//check if language is GCC - this is not altogether correct !
|
||||
// Objective-C and other non-GCC based symbols may be handled improperly
|
||||
|
||||
if (isELF(executableFormat) || isMacho(executableFormat)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
CompilerSpec compilerSpec = currentProgram.getCompilerSpec();
|
||||
if (compilerSpec.getCompilerSpecID()
|
||||
.getIdAsString()
|
||||
.toLowerCase()
|
||||
.indexOf("windows") == -1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isELF(String executableFormat) {
|
||||
return executableFormat != null && executableFormat.indexOf(ElfLoader.ELF_NAME) != -1;
|
||||
}
|
||||
|
||||
private boolean isMacho(String executableFormat) {
|
||||
return executableFormat != null && executableFormat.indexOf(MachoLoader.MACH_O_NAME) != -1;
|
||||
}
|
||||
|
||||
/// TODO this is here because we did not support program arguments. replace this code
|
||||
private Process createProcess(String executableName) throws Exception {
|
||||
|
||||
String demanglerName = GnuDemanglerNativeProcess.DEMANGLER_GNU;
|
||||
OperatingSystem OS = Platform.CURRENT_PLATFORM.getOperatingSystem();
|
||||
String demanglerExe =
|
||||
(OS == OperatingSystem.WINDOWS) ? demanglerName + ".exe" : demanglerName;
|
||||
File commandPath = Application.getOSFile("GnuDemangler", demanglerExe);
|
||||
|
||||
//
|
||||
// This is where special options are to be passed. Put your own here as necessary.
|
||||
//
|
||||
String[] command = new String[] { commandPath.getAbsolutePath(), "-s", "arm" };
|
||||
|
||||
Process process = Runtime.getRuntime().exec(command);
|
||||
|
||||
return process;
|
||||
println("Succesfully demangled " + mangled + " to " + demangledObject);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue