GT-3112 - PDB Universal - applicator/analyzer (112 squashed commits)

This commit is contained in:
ghizard
2020-09-01 10:48:01 -04:00
parent e1efbe228e
commit a1d7ed2cb2
85 changed files with 27376 additions and 43 deletions

View File

@@ -0,0 +1,108 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Text-dump all data types from the user-specified DataTypeManager to the user-specified file.
//
//@category Data Types
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import docking.widgets.OptionDialog;
import ghidra.app.script.GhidraScript;
import ghidra.app.services.DataTypeManagerService;
import ghidra.app.util.ToolTipUtils;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.data.*;
import ghidra.util.HTMLUtilities;
import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
public class DeveloperDumpAllTypesScript extends GhidraScript {
@Override
protected void run() throws Exception, CancelledException {
DataTypeManager manager = userChooseDataTypeManager();
if (manager == null) {
return;
}
File dumpFile = askFile("Choose an output file", "OK");
if (dumpFile == null) {
Msg.info(this, "Canceled execution due to no output file");
return;
}
if (dumpFile.exists()) {
if (!askYesNo("Confirm Overwrite", "Overwrite file: " + dumpFile.getName())) {
Msg.info(this, "Operation canceled");
return;
}
}
FileWriter fileWriter = new FileWriter(dumpFile);
String message = "Outputting DataTypes from: " + manager.getName();
Iterator<DataType> allDataTypes = manager.getAllDataTypes();
while (allDataTypes.hasNext()) {
monitor.checkCanceled();
DataType dataType = allDataTypes.next();
DataTypePath dataTypePath = dataType.getDataTypePath();
String pathString = dataTypePath.toString();
String htmlString = ToolTipUtils.getToolTipText(dataType);
String plainString = HTMLUtilities.fromHTML(htmlString);
fileWriter.append(pathString);
fileWriter.append("\n");
fileWriter.append(plainString);
fileWriter.append("\n");
fileWriter.append("------------------------------------------------------------\n");
}
fileWriter.close();
message = "Results located in: " + dumpFile.getAbsoluteFile();
monitor.setMessage(message);
Msg.info(this, message);
}
private DataTypeManager userChooseDataTypeManager() {
PluginTool tool = state.getTool();
DataTypeManagerService service = tool.getService(DataTypeManagerService.class);
DataTypeManager[] dataTypeManagers = service.getDataTypeManagers();
String names[] = new String[dataTypeManagers.length];
String initialDtmChoice = names[0];
try {
initialDtmChoice = currentProgram.getDataTypeManager().getName();
}
catch (Exception e) {
// Ignore... assuming no program or dtm.
}
for (int i = 0; i < dataTypeManagers.length; i++) {
names[i] = dataTypeManagers[i].getName();
}
String userChoice =
OptionDialog.showInputChoiceDialog(null, "Choose a Data Type Manager or Cancel",
"Choose", names, initialDtmChoice, OptionDialog.CANCEL_OPTION);
if (userChoice == null) {
return null;
}
for (int i = 0; i < dataTypeManagers.length; i++) {
if (names[i].contentEquals(userChoice)) {
return dataTypeManagers[i];
}
}
return null; // should not reach this line.
}
}

View File

@@ -0,0 +1,101 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Script for ghizard developer investigating template and ICF squashing.
//
//@category PDB
import java.io.*;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.symbol.*;
import ghidra.util.Msg;
import mdemangler.*;
import mdemangler.object.MDObjectCPP;
public class DumpAllSymbolsDemangledScript extends GhidraScript {
@Override
public void run() throws Exception {
File dumpFile = askFile("Choose an output file", "OK");
if (dumpFile == null) {
Msg.info(this, "Canceled execution due to no output file");
return;
}
if (dumpFile.exists()) {
if (!askYesNo("Confirm Overwrite", "Overwrite file: " + dumpFile.getName())) {
Msg.info(this, "Operation canceled");
return;
}
}
FileWriter fileWriter = new FileWriter(dumpFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
SymbolTable st = currentProgram.getSymbolTable();
SymbolIterator it = st.getDefinedSymbols();
while (it.hasNext() && !monitor.isCancelled()) {
Symbol s = it.next();
if (s.getSource() == SourceType.DEFAULT) {
continue;
}
Address addr = s.getAddress();
if (addr.isExternalAddress() || Address.NO_ADDRESS.equals(addr)) {
continue;
}
String name = s.getName();
String demangled = getDemangledString(name);
if (demangled != null && !demangled.isBlank()) {
name = demangled;
}
bufferedWriter.append(addr + " " + name + "\n");
}
bufferedWriter.close();
}
/**
* Gets a demangled string for the indicated mangled string.
* @param mangledString the mangled string to be decoded
* @return the associated demangled string
*/
private static String getDemangledString(String mangledString) {
MDMangGhidra demangler = new MDMangGhidra();
try {
MDParsableItem parsableItem = demangler.demangle(mangledString, true);
if (parsableItem instanceof MDObjectCPP) {
MDObjectCPP mdObject = (MDObjectCPP) parsableItem;
return mdObject.getQualifiedName().toString();
}
// if (parsableItem instanceof MDFunctionType) {
// MDFunctionType functionType = (MDFunctionType) parsableItem;
// return functionType.getName();
// }
// if (parsableItem instanceof MDDataType) {
// MDDataType dataType = (MDDataType) parsableItem;
// return dataType.getName();
// }
return parsableItem.toString();
}
catch (MDException e) {
// Couldn't demangle.
Msg.info(null, e.getMessage());
return null;
}
}
}

View File

@@ -0,0 +1,88 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Dump PDB information from AbstractPdb for PDB developer use.
//
//@category PDB
import java.io.*;
import ghidra.app.script.GhidraScript;
import ghidra.app.util.bin.format.pdb2.pdbreader.*;
import ghidra.util.Msg;
public class PdbDeveloperDumpScript extends GhidraScript {
@Override
protected void run() throws Exception {
File pdbFile = askFile("Choose a PDB file", "OK");
if (pdbFile == null) {
Msg.info(this, "Canceled execution due to no input file");
return;
}
if (!pdbFile.exists()) {
String message = pdbFile.getAbsolutePath() + " is not a valid file.";
Msg.info(this, message);
popup(message);
return;
}
String pdbFileName = pdbFile.getAbsolutePath();
if (!pdbFileName.endsWith(".pdb") && !pdbFileName.endsWith(".PDB")) {
String message = "Aborting: Expected input file to have extension of type .pdb (got '" +
pdbFileName + "').";
Msg.info(this, message);
popup(message);
return;
}
File dumpFile = askFile("Choose an output file", "OK");
if (dumpFile == null) {
Msg.info(this, "Canceled execution due to no output file");
return;
}
if (dumpFile.exists()) {
if (!askYesNo("Confirm Overwrite", "Overwrite file: " + dumpFile.getName())) {
Msg.info(this, "Operation canceled");
return;
}
}
String message = "Processing PDB Dump of: " + pdbFileName;
monitor.setMessage(message);
Msg.info(this, message);
try (AbstractPdb pdb = PdbParser.parse(pdbFileName, new PdbReaderOptions(), monitor)) {
pdb.deserialize(monitor);
FileWriter fileWriter = new FileWriter(dumpFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
outputHeaderMessage(bufferedWriter, pdbFileName);
pdb.dumpDirectory(bufferedWriter);
pdb.dumpSubStreams(bufferedWriter);
bufferedWriter.close();
}
catch (IOException ioe) {
Msg.info(this, ioe.getMessage());
popup(ioe.getMessage());
}
message = "Results located in: " + dumpFile.getAbsoluteFile();
monitor.setMessage(message);
Msg.info(this, message);
}
private void outputHeaderMessage(BufferedWriter bufferedWriter, String name) throws Exception {
bufferedWriter.append(getClass().getSimpleName() + " dump of: " + name +
"\nWARNING: FORMAT SUBJECT TO CHANGE WITHOUT NOTICE--DO NOT PARSE\n\n");
}
}