Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/bindiffhelper/BinDiffHelperPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import ghidra.app.plugin.PluginCategoryNames;
import ghidra.app.plugin.ProgramPlugin;
import ghidra.app.services.CodeViewerService;
import ghidra.app.util.Option;
import ghidra.app.util.exporter.Exporter;
import ghidra.framework.model.DomainFile;
import ghidra.framework.plugintool.PluginInfo;
Expand Down Expand Up @@ -63,12 +64,14 @@ public class BinDiffHelperPlugin extends ProgramPlugin {
Exporter binExportExporter;
String binDiffBinary;
String diffCommand;
boolean enableNamespace;
protected String defaultBinPath;
protected String defaultDiffCommand;
Program program;

public final static String BDBINPROPERTY = "de.ubfx.bindiffhelper.bindiffbinary";
public final static String DIFFCOMMAND = "de.ubfx.bindiffhelper.diffCommand";
public final static String ENABLENAMESPACE = "de.ubfx.bindiffhelper.enableNamespace";
/**
* Plugin constructor.
*
Expand Down Expand Up @@ -113,6 +116,7 @@ public BinDiffHelperPlugin(PluginTool tool) {

binDiffBinary = Preferences.getProperty(BDBINPROPERTY, defaultBinPath);
diffCommand = Preferences.getProperty(DIFFCOMMAND, defaultDiffCommand);
enableNamespace = Boolean.parseBoolean(Preferences.getProperty(ENABLENAMESPACE, "false"));

provider = new BinDiffHelperProvider(this, this.getCurrentProgram());
provider.setTitle("BinDiffHelper");
Expand All @@ -130,6 +134,14 @@ File binExportDomainFile(DomainFile df)
if (dof instanceof Program)
{
out = File.createTempFile(df.getName() + "_bdh", ".BinExport");

if (enableNamespace) {
binExportExporter.setOptions(
List.of(
new Option("Prepend Namespace to Function Names", true)
)
);
}

if (binExportExporter.export(out, dof, null, TaskMonitor.DUMMY) == false)
{
Expand Down Expand Up @@ -286,6 +298,12 @@ public boolean updateBinDiffBinary() throws IOException

return true;
}

public void updateEnableNamespace(boolean enable)
{
enableNamespace = enable;
Preferences.setProperty(ENABLENAMESPACE, Boolean.toString(enable));
}

public void updateDiffCommand(String cmd)
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/bindiffhelper/BinDiffHelperProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected void doDiffWork() {
Set<Long> secFnSet = secondary.beFile.getFunctionAddressSet();
Set<Long> commonSecFnSet = new TreeSet<Long>();

ctm = new ComparisonTableModel();
ctm = new ComparisonTableModel(plugin.enableNamespace);
try {
Statement stmt = conn.createStatement();

Expand Down
22 changes: 19 additions & 3 deletions src/main/java/bindiffhelper/ComparisonTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ public class ComparisonTableModel extends AbstractTableModel {

private List<Entry> data;

public ComparisonTableModel() {
private boolean showNamespace = false;

public ComparisonTableModel(boolean showNamespace) {
data = new ArrayList<Entry>();
this.showNamespace = showNamespace;
}

public void addEntry(Entry e) {
Expand Down Expand Up @@ -54,8 +57,21 @@ public Object getValueAt(int row, int col) {
return "0x" + Long.toHexString(e.primaryAddress.getUnsignedOffset());
return "";
case 2:
if (e.primaryFunctionSymbol != null)
return e.primaryFunctionSymbol.getName();
if (e.primaryFunctionSymbol != null){
if (showNamespace) {
var functionNameComponents = new ArrayList<String>();
var parentNamespace = e.primaryFunctionSymbol.getParentNamespace();
while (parentNamespace != null && !"Global".equals(parentNamespace.getName())) {
functionNameComponents.add(0, parentNamespace.getName());
parentNamespace = parentNamespace.getParentNamespace();
}
// Add the name of the function as the last component.
functionNameComponents.add(e.primaryFunctionSymbol.getName());
return String.join("::", functionNameComponents);
} else {
return e.primaryFunctionSymbol.getName();
}
}
return "No Symbol";
case 3:
if (e.primaryFunctionNameDb != null)
Expand Down
47 changes: 46 additions & 1 deletion src/main/java/bindiffhelper/ImportFunctionNamesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import docking.action.DockingAction;
import docking.action.MenuData;
import docking.action.ToolBarData;
import ghidra.program.model.listing.Program;
import ghidra.program.model.symbol.Namespace;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.symbol.SymbolTable;
import ghidra.util.HTMLUtilities;
import ghidra.util.Msg;
import resources.ResourceManager;
Expand All @@ -32,6 +35,31 @@ protected boolean shouldImportEntry(ComparisonTableModel.Entry e) {
return false;
}

private Namespace getOrCreateNamespace(Program program, String namespacePath) throws Exception {

SymbolTable symbolTable = program.getSymbolTable();
Namespace currentNamespace = program.getGlobalNamespace();

if (namespacePath == null || namespacePath.isEmpty()) {
return currentNamespace;
}

String[] namespaces = namespacePath.split("::");
for (String namespaceName : namespaces) {
ghidra.program.model.symbol.Namespace nextNamespace = symbolTable.getNamespace(
namespaceName, currentNamespace);

if (nextNamespace == null) {
nextNamespace = symbolTable.createNameSpace(
currentNamespace, namespaceName, SourceType.IMPORTED);
}

currentNamespace = nextNamespace;
}

return currentNamespace;
}

@Override
public void actionPerformed(ActionContext arg0) {
int trans = plugin.program.startTransaction("Rename functions");
Expand All @@ -46,7 +74,24 @@ public void actionPerformed(ActionContext arg0) {
Exception exp = null;
try {
transformation = e.primaryFunctionSymbol.getName() + " -> " + e.secondaryFunctionName;
e.primaryFunctionSymbol.setName(e.secondaryFunctionName, SourceType.IMPORTED);

if (e.secondaryFunctionName.contains("::")) {
String[] parts = e.secondaryFunctionName.split("::");
String methodName = parts[parts.length - 1];

StringBuilder namespacePath = new StringBuilder();
for (int i = 0; i < parts.length - 1; i++) {
if (i > 0) namespacePath.append("::");
namespacePath.append(parts[i]);
}

Namespace namespace = getOrCreateNamespace(plugin.program, namespacePath.toString());

e.primaryFunctionSymbol.setNameAndNamespace(methodName, namespace, SourceType.IMPORTED);
} else {
e.primaryFunctionSymbol.setName(e.secondaryFunctionName, SourceType.IMPORTED);
}

e.do_import = false;
} catch (Exception ex) {
exp = ex;
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/bindiffhelper/SettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;

import docking.DialogComponentProvider;
import docking.widgets.filechooser.GhidraFileChooserPanel;
Expand All @@ -18,6 +14,7 @@ public class SettingsDialog extends DialogComponentProvider {
protected BinDiffHelperPlugin plugin;
protected GhidraFileChooserPanel fileChooserPanel;
private JTextField customTextField;
private JCheckBox enableNamespaceCheckBox;

public SettingsDialog(BinDiffHelperPlugin plugin) {
super("Settings");
Expand Down Expand Up @@ -51,7 +48,18 @@ public SettingsDialog(BinDiffHelperPlugin plugin) {
diffCommandTextField.add(customTextField, BorderLayout.CENTER);

diffCommandPanel.add(diffCommandTextField, BorderLayout.CENTER);
panel.add(diffCommandPanel, BorderLayout.SOUTH);

JPanel namespacePanel = new JPanel(new BorderLayout());
namespacePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
enableNamespaceCheckBox = new JCheckBox("Enable export Namespace");
enableNamespaceCheckBox.setSelected(plugin.enableNamespace);
namespacePanel.add(enableNamespaceCheckBox, BorderLayout.CENTER);

JPanel southContainer = new JPanel();
southContainer.setLayout(new BoxLayout(southContainer, BoxLayout.Y_AXIS));
southContainer.add(diffCommandPanel);
southContainer.add(namespacePanel);
panel.add(southContainer, BorderLayout.SOUTH);

addWorkPanel(panel);

Expand All @@ -76,6 +84,7 @@ protected void okCallback() {
}

plugin.updateDiffCommand(customTextField.getText());
plugin.updateEnableNamespace(enableNamespaceCheckBox.isSelected());
plugin.provider.generateWarnings();
}
}
Loading