Skip to content

Ghidra is a software reverse engineering (SRE) framework

help for Ghidra Bundles, update scripting help, @imports->@importpackage

Jason P. Leasure0af17e4b08ad
diff --git a/Ghidra/Features/Base/certification.manifest b/Ghidra/Features/Base/certification.manifest
index 8afcc61691..98ad1f984a 100644
--- a/Ghidra/Features/Base/certification.manifest
+++ b/Ghidra/Features/Base/certification.manifest
@@ -437,6 +437,7 @@ src/main/help/help/topics/GhidraScriptMgrPlugin/images/text_list_bullets.png||FA
src/main/help/help/topics/GhidraScriptMgrPlugin/images/text_lowercase.png||FAMFAMFAM Icons - CC 2.5|||famfamfam silk icon set|END|
src/main/help/help/topics/GhidraScriptMgrPlugin/images/textfield_rename.png||FAMFAMFAM Icons - CC 2.5|||famfamfam silk icon set|END|
src/main/help/help/topics/GhidraScriptMgrPlugin/images/undo.png||GHIDRA||||END|
+src/main/help/help/topics/BundleManager/BundleManager.htm||GHIDRA||||END|
src/main/help/help/topics/GhidraServer/GhidraServer.htm||GHIDRA||||END|
src/main/help/help/topics/Glossary/glossary.htm||GHIDRA||||END|
src/main/help/help/topics/Glossary/images/BigEndian.png||GHIDRA||reviewed||END|
diff --git a/Ghidra/Features/Base/src/main/help/help/TOC_Source.xml b/Ghidra/Features/Base/src/main/help/help/TOC_Source.xml
index d154fd594e..9a27608027 100644
--- a/Ghidra/Features/Base/src/main/help/help/TOC_Source.xml
+++ b/Ghidra/Features/Base/src/main/help/help/TOC_Source.xml
@@ -354,9 +354,10 @@
<tocdef id="Create a New Script" sortgroup="k" text="Create a New Script" target="help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm#New" />
<tocdef id="Refresh Script List" sortgroup="l" text="Refresh Script List" target="help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm#Refresh" />
<tocdef id="Add and Remove Script Directories" sortgroup="m" text="Add and Remove Script Directories" target="help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm#Script_Directories" />
- <tocdef id="Ghidra Script Development" sortgroup="n" text="Ghidra Script Development" target="help/topics/GhidraScriptMgrPlugin/ScriptDevelopment.htm" />
</tocdef>
<tocdef id="Console" sortgroup="e" text="Console" target="help/topics/ConsolePlugin/console.html" />
+ <tocdef id="Ghidra Script Development" sortgroup="f" text="Ghidra Script Development" target="help/topics/GhidraScriptMgrPlugin/ScriptDevelopment.htm" />
+ <tocdef id="Ghidra Bundles" sortgroup="g" text="Ghidra Bundles" target="help/topics/BundleManager/BundleManager.htm" />
</tocdef>
<tocdef id="Program Validator"
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/BundleManager/BundleManager.htm b/Ghidra/Features/Base/src/main/help/help/topics/BundleManager/BundleManager.htm
new file mode 100644
index 0000000000..706d336d6e
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/help/help/topics/BundleManager/BundleManager.htm
@@ -0,0 +1,151 @@
+<!doctype HTML public "-//W3C//DTD HTML 4.0 Frameset//EN">
+<html>
+ <head>
+ <title>Ghidra Bundles</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
+ <link rel="stylesheet" type="text/css" href="../../shared/Frontpage.css">
+ </head>
+ <body lang="EN-US">
+ <h1>Ghidra Bundles</h1>
+
+ <h2>Dynamic modules</h2>
+ <p> Scripting brings a poweful form of dynamic extensibilty to Ghidra,
+ where Java source code is (re)compiled, loaded, and run without exiting
+ Ghidra. When a script grows large or requires external dependencies, it
+ might be worth the effort think of how to split up code into sensible
+ pieces, or <em>modules</em>.</p>
+
+ <p>To support modularity while preserving the dynamic nature of scripts,
+ Ghidra uses <a href="https://www.osgi.org">OSGi</a>. Without delving too
+ much into terminology, the key things to know are </p>
+ <ol style="padding-left:5em">
+ <li>The unit of modularity in OSGi is the <em>bundle</em>. Bundles are
+ <em>mostly independent</em> components with well-defined,
+ collaborations.
+ </li>
+
+ <li>Concretely, a bundle is a Jar file with extra metadata in its
+ manifest file that tells the framework what it uses and what it
+ provides.</li>
+
+ <li>Bundles can <em>export</em> packages for use by other bundles.
+ Exported packages can have versions.</li>
+
+ <li>Bundles can <em>import</em> packages from other bundles. Imports can
+ be constrained by version.</li>
+
+ <li>Ghidra can compile source directories to bundles. We refer to these
+ as <a href="BundleManager.htm#source_bundles"><em>source bundles</em></a>.
+
+ <li>The entire Ghidra API is part of the "system bundle", so hosted
+ bundles have complete access.</li>
+
+ </ol>
+
+
+ <h2><a name="source_bundles"/>Source bundles</h2>
+ <p>When a directory is added to the Bundle Manager, it is treated as a
+ <em>source bundle</em> and of its Java contents will be compiled to <br>
+ &nbsp;&nbsp;&nbsp;&nbsp;<code>&lt;user home&gt;/.ghidra/.ghidra-&lt;version&gt;/osgi/compiled-bundles/&lt;hash&gt;</code>, <br>
+ where <code>&lt;hash&gt;</code> is a hash of the source bundle location.</p>
+ <h3>exploded bundles</h3>
+ <p>
+ Each subdirectory of <code>compiled-bundles/</code> is an <em>exploded jar</em> -- by re-compressing it,
+ we get a standard Jar bundle:
+ </p>
+ <code>
+ <pre>
+ jar cMf mybundle.jar -C $HOME/.ghidra/.ghidra_&lt;version&gt;/osgi/compiled-bundles/&lt;hash&gt; .
+ </pre>
+ </code>
+ <p>
+ <code>mybundle.jar</code> could then be shared in binary form.
+ </p>
+
+ <h3>generated files</h3>
+ <p>If there is no manifest in the source directory, Ghidra generates one
+ using <a href="https://bnd.bndtools.org/">bndlib</a> so that:
+ </p>
+ <ul style="padding-left:5em">
+ <li>Every package is exported unless it contains <code>private</code> or <code>internal</code> in its name.</li>
+ <li>Currently active bundles satisfying <code>@importpackage</code> requirments made available for import.</li>
+ <li>Every package from Ghidra core is available for import. (Ghidra's core API is part of the system bundle)</li>
+ </ul>
+ <p>
+ If no bundle activator is present, a stub is created and referenced in the generated manifest.
+ </p>
+
+ <h2><a name="dependency"/>Dependency</h2>
+ <p>Two basic types of code dependency are available when developing with
+ OSGi, intra-bundle and inter-bundle.</p>
+
+ <h3>intra-bundle (<em>compile time</em>) dependency</h3>
+ <p>Classes within a bundle, e.g. source files in a source bundle, can rely
+ one another with Java's usual package <code>import</code>.</p>
+ <p>This kind of dependency is resolved at compile time -- if a class isn't
+ imported or present, compilation will fail!</p>
+
+ <h3>inter-bundle (<em>run time</em>) dependency</h3>
+ <p>To make use of code from <em>other</em> bundles, a bundle must declare its
+ requirements. When a bundle is activated, the framework attempts to
+ <em>resolve</em> its declared dependencies against active bundles. The
+ first match, in the order those bundles were activated, will be "wired" to
+ the dependent.</p>
+
+ <p>Note: OSGi bundle dependency is very similar to Java 9 modules, except
+ that Java 9 modules provide <em>load time</em> resolution.</p>
+
+ <p>Via the Jar manifest, there are multiple ways to configure the
+ requirements of a bundle. We'll describe only one, the
+ <code>Import-Package</code> attribute, because Ghidra provides a shortcut
+ which we'll describe afterwards.</p>
+
+ <h4><code>Import-Package</code> in the manifest</h4>
+ <p>You can find more detail at https://osgi.org for more detail.</p>
+ <p>These examples should demonstrated the most common usage. Note: the
+ file <code>META-INF/MANIFEST.MF</code> should have only <em>one</em>
+ <code>Import-Package</code> entry:</p>
+ <code>
+ <pre>
+ Import-Package:com.example.stuff
+ Import-Package:com.example.stuff;version=1.2.3
+ Import-Package:com.example.stuff;version="[1.2.0,1.3)"
+ Import-Package:com.example.stuff,org.example.otherstuff
+ Import-Package:com.example.stuff;version="[1.2.0,1.3)",org.example.otherstuff
+ Import-Package:com.example.stuff;version="[1.2.0,1.3)",org.example.otherstuff;version=2.3.4
+ </pre>
+ </code>
+
+ <h4><code>@importpackage</code> in source</h4>
+ <p>If a source bundle includes a file <code>META-INF/MANIFEST.MF</code>, it
+ will be used as the bundle's manifest. If not, a manifest will be
+ generated, see <a href="#source_bundles">above</a>.</p>
+
+ <p>Ghidra provides a shortcut to populate <code>Import-Package</code>, the
+ <code>@importpackage</code> metadata tag. The <code>@importpackage</code>
+ values from all source files in a source bundle are appended to the
+ generated manifest <code>Import-Package</code> attribute.</p>
+
+
+ <h2><a name="AddBundles"/><a name="RemoveBundles"/>Adding and removing bundles from the manager</h2>
+ <p>Adding a directory to the bundle manager and then enabling it makes any
+ contained scripts visible in the script manager.</p>
+ <p>Removing a bundle deactives, disables, and removes it from the list.</p>
+
+ <h2><a name="ActivateBundles"/><a name="DeactivateBundles"/>Activating and deactivating bundles</h2>
+ <p>In order to make a bundle's contents available to other bundles, it must
+ be activated. This corresponds the OSGi bundle <code>ACTIVE</code> state.
+ Ghidra will compile source bundles prior to installing and activating.</p>
+
+ <p>On deactivation, any dependents of a bundle are stopped first, then the
+ bundle itself is stopped</p>
+
+ <h2><a name="CleanBundles"/>Cleaning bundles</h2>
+ <p>When Ghidra builds a source bundle, the result is cached to a
+ subdirectory of <br>
+ &nbsp;&nbsp;&nbsp;&nbsp;<code>&lt;user home&gt;/.ghidra/.ghidra-&lt;version&gt;/osgi/compiled-bundles/&lt;hash&gt;</code>. <br>
+ These cached files speed up subsequent builds. A <em>clean</em>
+ deactivates then wipes the subdirectory of the selected bundle.</p>
+
+ </body>
+</html>
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm
index cca4bb5094..fa5099cd8b 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm
@@ -271,16 +271,20 @@
</P>
</BLOCKQUOTE>
- <H3 align="left"><A name="Script_Directories"></A>Script Directories&nbsp;<IMG src=
+ <H3 align="left"><A name="Script_Directories"></A>Script Directories / Bundle Manager&nbsp;<IMG src=
"images/text_list_bullets.png" alt=""></H3>
<BLOCKQUOTE>
- <P align="left">Allows you to add and remove directories to search for scripts. The default
- directories are your home directory and the various system directories (e.g.,
- <tt>$GHIDRA_HOME/Features/Base/ghidra_scripts</tt>). You
- can save directories, but ignore them in the Script Manager dialog by selecting/deselecting
- the "Use" column checkbox.<BR>
- </P>
+ <P align="left">Allows you to add and remove directories to search for
+ scripts and bundle dependencies for use by scripts. The default
+ directories are your home directory and the various system directories
+ (e.g., <tt>$GHIDRA_HOME/Features/Base/ghidra_scripts</tt>). You can
+ save directories, but ignore them in the Script Manager dialog by
+ selecting/deselecting the "Enable" column checkbox. </P>
+
+ <P align="left">For more information on Ghidra's dynamic module support, see
+ <a href="../BundleManager/BundleManager.htm">Ghidra Bundles</A>.</p>
+ <BR>
<DIV align="center">
<IMG src="images/Script_Dirs.png" alt="">
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/ScriptDevelopment.htm b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/ScriptDevelopment.htm
index a4aa8f82ee..0a3e5f3afc 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/ScriptDevelopment.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/ScriptDevelopment.htm
@@ -48,7 +48,7 @@
This is highly recommended if you plan to do more than simple edits to scripts.</P>
</BLOCKQUOTE>
- <H2><A name="meta_data">Meta-data</H2>
+ <H2><A name="meta_data" />Meta-data</H2>
<BLOCKQUOTE>
<P>The scripting framework supports special meta-data comments. This comment is treated
@@ -84,6 +84,28 @@
</P>
</BLOCKQUOTE>
+ <P><B><CODE>@importpackage</CODE></B></P>
+
+ <BLOCKQUOTE>
+ <P>This tag is a convenience for declaring <A href="../BundleManager/BundleManager.htm#dependency">bundle dependencies</A>. The tag's
+ contents syntax is that of the <a href="https://osgi.org/specification/osgi.core/7.0.0/framework.module.html#framework.module.importpackage"><CODE>Import-Package</CODE> attribute</a>
+ in an OSGi bundle manifest.
+ </P>
+
+ <P>For example:</P>
+
+ <BLOCKQUOTE>
+ <P><TT>
+ @importpackage com.example.library<BR>
+ @importpackage org.apache.commons.collections.properties;version=4.4<BR>
+ @importpackage org.ghidra.analysis;version="[1.1,2)"<BR>
+ @importpackage com.example.library1,com.example.library2<BR>
+ </TT></P>
+ </BLOCKQUOTE>
+
+ </BLOCKQUOTE>
+
+
<P><B><CODE>@keybinding</CODE></B></P>
<BLOCKQUOTE>
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java
index 89ee83dd0d..4c506ac1db 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java
@@ -37,6 +37,7 @@ import ghidra.app.services.ConsoleService;
import ghidra.framework.plugintool.ComponentProviderAdapter;
import ghidra.framework.plugintool.PluginTool;
import ghidra.framework.preferences.Preferences;
+import ghidra.util.HelpLocation;
import ghidra.util.exception.CancelledException;
import ghidra.util.filechooser.GhidraFileChooserModel;
import ghidra.util.filechooser.GhidraFileFilter;
@@ -62,7 +63,10 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
static final String BUNDLE_LIST_GROUP = "1bundle list group";
public BundleStatusComponentProvider(PluginTool tool, String owner, BundleHost bundleHost) {
- super(tool, "Bundle Status Component", owner);
+ super(tool, "BundleManager", owner);
+ setHelpLocation(new HelpLocation("BundleManager", "BundleManager"));
+ setTitle("Bundle Manager");
+
this.bundleHost = bundleHost;
this.bundleStatusTableModel = new BundleStatusTableModel(this, bundleHost);
@@ -119,10 +123,9 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
// to allow custom cell renderers
bundleStatusTable.setAutoCreateColumnsFromModel(false);
- int skinnyWidth = 50;
-
TableColumn column;
+ int skinnyWidth = 60;
//
column = bundleStatusTable.getColumnModel().getColumn(
bundleStatusTableModel.enabledColumn.index);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java
index 85a6534365..e612fac2e7 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java
@@ -142,7 +142,7 @@ public class GhidraSourceBundle extends GhidraBundle {
private String parseImps(ResourceFile javaSource) {
// XXX don't use @imports, use an annotation
- return GhidraScriptUtil.newScriptInfo(javaSource).getImports();
+ return GhidraScriptUtil.newScriptInfo(javaSource).getImportPackage();
}
/**
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
index ca84b4b4d3..5db19973c8 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
@@ -353,7 +353,7 @@ class GhidraScriptActionManager {
refreshAction.setEnabled(true);
plugin.getTool().addLocalAction(provider, refreshAction);
- bundleStatusAction = new DockingAction("Bundle Status", plugin.getName()) {
+ bundleStatusAction = new DockingAction("Script Directories", plugin.getName()) {
@Override
public void actionPerformed(ActionContext context) {
provider.showBundleStatusComponent();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/ScriptInfo.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/ScriptInfo.java
index 2bb59ec05c..2c18083f5e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/ScriptInfo.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/ScriptInfo.java
@@ -45,14 +45,14 @@ public class ScriptInfo {
*/
public static final String DELIMITTER = ".";
- public static final String AT_AUTHOR = "@author";
- public static final String AT_CATEGORY = "@category";
- public static final String AT_KEYBINDING = "@keybinding";
- public static final String AT_MENUPATH = "@menupath";
- public static final String AT_TOOLBAR = "@toolbar";
+ static final String AT_AUTHOR = "@author";
+ static final String AT_CATEGORY = "@category";
+ static final String AT_KEYBINDING = "@keybinding";
+ static final String AT_MENUPATH = "@menupath";
+ static final String AT_TOOLBAR = "@toolbar";
// omit from METADATA to avoid pre-populating in new scripts
- public static final String AT_IMPORTS = "@imports";
+ private static final String AT_IMPORTPACKAGE = "@importpackage";
public static final String[] METADATA =
{ AT_AUTHOR, AT_CATEGORY, AT_KEYBINDING, AT_MENUPATH, AT_TOOLBAR, };
@@ -71,7 +71,7 @@ public class ScriptInfo {
private String[] menupath = new String[0];
private String toolbar;
private ImageIcon toolbarImage;
- private String imports;
+ private String importpackage;
/**
* Constructs a new script.
@@ -96,7 +96,7 @@ public class ScriptInfo {
menupath = new String[0];
toolbar = null;
toolbarImage = null;
- imports = null;
+ importpackage = null;
keybindingErrorMessage = null;
}
@@ -277,8 +277,8 @@ public class ScriptInfo {
else if (line.startsWith(AT_TOOLBAR)) {
toolbar = getTagValue(AT_TOOLBAR, line);
}
- else if (line.startsWith(AT_IMPORTS)) {
- imports = getTagValue(AT_IMPORTS, line);
+ else if (line.startsWith(AT_IMPORTPACKAGE)) {
+ importpackage = getTagValue(AT_IMPORTPACKAGE, line);
}
}
catch (Exception e) {
@@ -440,9 +440,9 @@ public class ScriptInfo {
* Returns the script imports
* @return the script imports
*/
- public String getImports() {
+ public String getImportPackage() {
parseHeader();
- return imports;
+ return importpackage;
}
/**
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/BundleHostTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/BundleHostTest.java
index cc453e31eb..e091ea3bdf 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/BundleHostTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/BundleHostTest.java
@@ -301,7 +301,7 @@ public class BundleHostTest extends AbstractGhidraHeadlessIntegrationTest {
pushNewBundle();
// @imports tag is only parsed from classes in default package
addClass(
- "//@imports lib\n"
+ "//@importpackage lib\n"
,
"import lib.Library;\n"
,
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java
index 3bbc990083..5142c63347 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java
@@ -276,7 +276,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
// Tests that the user can add an additional script path directory and choose that one
// to use
//
- DockingActionIf bundleStatusAction = getAction(plugin, "Bundle Status");
+ DockingActionIf bundleStatusAction = getAction(plugin, "Script Directories");
performAction(bundleStatusAction, false);
waitForSwing();
diff --git a/Ghidra/Features/Python/src/main/help/help/TOC_Source.xml b/Ghidra/Features/Python/src/main/help/help/TOC_Source.xml
index 5069c64d9c..acdd831d0b 100644
--- a/Ghidra/Features/Python/src/main/help/help/TOC_Source.xml
+++ b/Ghidra/Features/Python/src/main/help/help/TOC_Source.xml
@@ -3,7 +3,7 @@
<tocroot>
<tocref id="Ghidra Functionality">
<tocref id="Scripting">
- <tocdef id="Python Interpreter" sortgroup="f" text="Python Interpreter" target="help/topics/Python/interpreter.html" />
+ <tocdef id="Python Interpreter" sortgroup="z" text="Python Interpreter" target="help/topics/Python/interpreter.html" />
</tocref>
</tocref>
-</tocroot>
\ No newline at end of file
+</tocroot>