From fdda6b672eddb333750453d3b7d6175f4bb5e904 Mon Sep 17 00:00:00 2001 From: Ryan Kurtz Date: Fri, 23 Sep 2022 05:15:36 -0400 Subject: [PATCH] GP-2604: More load library options --- .../help/topics/ImporterPlugin/importer.htm | 46 ++-- .../ghidra/app/util/OptionsEditorPanel.java | 36 ++- .../opinion/AbstractLibrarySupportLoader.java | 218 +++++++++++++----- .../opinion/AbstractOrdinalSupportLoader.java | 20 +- .../util/opinion/AbstractProgramLoader.java | 73 +++--- .../ghidra/app/util/opinion/BinaryLoader.java | 6 +- .../ghidra/app/util/opinion/ElfLoader.java | 12 +- .../app/util/opinion/IntelHexLoader.java | 6 +- .../app/util/opinion/MotorolaHexLoader.java | 6 +- .../ghidra/app/util/opinion/XmlLoader.java | 6 +- .../ghidra/app/util/opinion/ApkLoader.java | 14 +- .../Common/support/analyzeHeadlessREADME.html | 9 + 12 files changed, 294 insertions(+), 158 deletions(-) diff --git a/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm b/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm index 534d1bca12..85bebfc22d 100644 --- a/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm +++ b/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm @@ -29,38 +29,27 @@
@@ -281,8 +270,22 @@ those symbols will remain at the address they were originally placed. If the option is off, the symbols will move with the image base or the memory block.

+ +

Link Existing Project Libraries

-

Load Local Libraries

+
+

Searches the project for existing library programs and creates external references to + them.

+
+ +

Project Library Search Folder

+ +
+

The project folder that will get searched for existing library programs. If left + empty, the folder that the main program is being imported to will be searched.

+
+ +

Load Local Libraries From Disk

Searches the executable's directory to recursively resolve the external libraries used @@ -292,7 +295,7 @@ in these programs will be resolved.

-

Load System Libraries

+

Load System Libraries From Disk

Searches a user-defined path list to recursively resolve the external libraries used @@ -309,6 +312,13 @@

Specifies how many levels deep the depth-first library dependency tree will be traversed when loading local or system libraries.

+ +

Library Destination Folder

+ +
+

The project folder where newly loaded library programs will get created. If left + empty, they will get created in the same folder as the main program being imported.

+

COFF Options

diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/OptionsEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/OptionsEditorPanel.java index f375b942da..9c77b048be 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/OptionsEditorPanel.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/OptionsEditorPanel.java @@ -28,12 +28,15 @@ import javax.swing.event.DocumentListener; import org.apache.commons.collections4.map.LazyMap; import docking.DockingWindowManager; +import docking.options.editor.ButtonPanelFactory; import docking.widgets.checkbox.GCheckBox; import docking.widgets.combobox.GComboBox; import docking.widgets.label.GLabel; import docking.widgets.textfield.IntegerTextField; import ghidra.app.util.opinion.AbstractLibrarySupportLoader; import ghidra.app.util.opinion.LibraryPathsDialog; +import ghidra.framework.main.DataTreeDialog; +import ghidra.framework.model.DomainFolder; import ghidra.program.model.address.*; import ghidra.util.exception.AssertException; import ghidra.util.layout.*; @@ -43,7 +46,7 @@ import ghidra.util.layout.*; * in a list of Options and generates editors for each of them on th fly. */ public class OptionsEditorPanel extends JPanel { - private static final int MAX_PER_COLUMN = 10; + private static final int MAX_PER_COLUMN = 11; private static final int MAX_BOOLEANS_WITH_SELECT_ALL = 5; private int columns; private AddressFactoryService addressFactoryService; @@ -177,9 +180,13 @@ public class OptionsEditorPanel extends JPanel { public Component getEditorComponent(Option option) { - //special case for load library paths + // Special cases for library link/load options + if (option.getName().equals(AbstractLibrarySupportLoader.LINK_SEARCH_FOLDER_OPTION_NAME) || + option.getName().equals(AbstractLibrarySupportLoader.LIBRARY_DEST_FOLDER_OPTION_NAME)) { + return buildProjectFolderEditor(option); + } if (option.getName().equals(AbstractLibrarySupportLoader.SYSTEM_LIBRARY_OPTION_NAME)) { - return buildLoadLibraryPathsEditor(option); + return buildPathsEditor(option); } Component customEditorComponent = option.getCustomEditorComponent(); @@ -216,7 +223,7 @@ public class OptionsEditorPanel extends JPanel { } } - private Component buildLoadLibraryPathsEditor(Option option) { + private Component buildPathsEditor(Option option) { JPanel panel = new JPanel(new BorderLayout()); JButton button = new JButton("Edit Paths"); button.addActionListener( @@ -233,6 +240,27 @@ public class OptionsEditorPanel extends JPanel { return panel; } + private Component buildProjectFolderEditor(Option option) { + JPanel panel = new JPanel(new BorderLayout()); + JTextField textField = new JTextField(); + textField.setEditable(false); + JButton button = ButtonPanelFactory.createButton(ButtonPanelFactory.BROWSE_TYPE); + button.addActionListener(e -> { + DataTreeDialog dataTreeDialog = new DataTreeDialog(this, + "Choose a project folder", DataTreeDialog.CHOOSE_FOLDER); + dataTreeDialog.setSelectedFolder(null); + dataTreeDialog.showComponent(); + DomainFolder folder = dataTreeDialog.getDomainFolder(); + if (folder != null) { + textField.setText(folder.getPathname()); + option.setValue(folder.getPathname()); + } + }); + panel.add(textField, BorderLayout.CENTER); + panel.add(button, BorderLayout.EAST); + return panel; + } + private Component getAddressSpaceEditorComponent(Option option) { JComboBox combo = new GComboBox<>(); AddressFactory addressFactory = addressFactoryService.getAddressFactory(); diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java index aa48a41f2d..c07f075e62 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java @@ -50,15 +50,24 @@ import utilities.util.FileUtilities; */ public abstract class AbstractLibrarySupportLoader extends AbstractProgramLoader { - public static final String LOCAL_LIBRARY_OPTION_NAME = "Load Local Libraries"; + public static final String LINK_EXISTING_OPTION_NAME = "Link Existing Project Libraries"; + static final boolean LINK_EXISTING_OPTION_DEFAULT = true; + + public static final String LINK_SEARCH_FOLDER_OPTION_NAME = "Project Library Search Folder"; + static final String LINK_SEARCH_FOLDER_OPTION_DEFAULT = ""; + + public static final String LOCAL_LIBRARY_OPTION_NAME = "Load Local Libraries From Disk"; static final boolean LOCAL_LIBRARY_OPTION_DEFAULT = false; - public static final String SYSTEM_LIBRARY_OPTION_NAME = "Load System Libraries"; + public static final String SYSTEM_LIBRARY_OPTION_NAME = "Load System Libraries From Disk"; static final boolean SYSTEM_LIBRARY_OPTION_DEFAULT = false; public static final String DEPTH_OPTION_NAME = "Recursive Library Load Depth"; static final int DEPTH_OPTION_DEFAULT = 1; + public static final String LIBRARY_DEST_FOLDER_OPTION_NAME = "Library Destination Folder"; + static final String LIBRARY_DEST_FOLDER_OPTION_DEFAULT = ""; + /** * Loads bytes in a particular format into the given {@link Program}. * @@ -76,11 +85,11 @@ public abstract class AbstractLibrarySupportLoader extends AbstractProgramLoader throws CancelledException, IOException; @Override - protected List loadProgram(ByteProvider provider, String programName, + protected List loadProgram(ByteProvider provider, String programName, DomainFolder programFolder, LoadSpec loadSpec, List
  • -loader-applyLabels <true|false>
  • -loader-anchorLabels <true|false>
  • +
  • -loader-linkExistingProjectLibraries <true|false>
  • +
  • -loader-projectLibrarySearchFolder <project path>
  • -loader-loadLocalLibraries <true|false>
  • -loader-loadSystemLibraries <true|false>
  • -loader-libraryLoadDepth <depth>
  • +
  • -loader-libraryDestinationFolder <project path>
  • -loader-applyRelocations <true|false>
  • -loader-imagebase <imagebase3>
  • -loader-dataImageBase <dataImageBase4>
  • @@ -602,9 +605,12 @@ The Headless Analyzer uses the command-line parameters discussed below. See
  • -loader-applyLabels <true|false>
  • -loader-anchorLabels <true|false>
  • +
  • -loader-linkExistingProjectLibraries <true|false>
  • +
  • -loader-projectLibrarySearchFolder <project path>
  • -loader-loadLocalLibraries <true|false>
  • -loader-loadSystemLibraries <true|false>
  • -loader-libraryLoadDepth <depth>
  • +
  • -loader-libraryDestinationFolder <project path>
  • -loader-ordinalLookup <true|false>
  • -loader-parseCliHeaders <true|false>
  • @@ -612,9 +618,12 @@ The Headless Analyzer uses the command-line parameters discussed below. See
  • -loader-applyLabels <true|false>
  • -loader-anchorLabels <true|false>
  • +
  • -loader-linkExistingProjectLibraries <true|false>
  • +
  • -loader-projectLibrarySearchFolder <project path>
  • -loader-loadLocalLibraries <true|false>
  • -loader-loadSystemLibraries <true|false>
  • -loader-libraryLoadDepth <depth>
  • +
  • -loader-libraryDestinationFolder <project path>
  • -loader-addChainedFixupsRelocations <true|false>