Skip to content

Ghidra is a software reverse engineering (SRE) framework

update help documentation

Jason P. Leasure9c4523858a18
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
index 51b94f217f..ff0d52cd73 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/BundleManager/BundleManager.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/BundleManager/BundleManager.htm
@@ -9,24 +9,28 @@
<h1>Ghidra Bundles</h1>
<h2>Dynamic modules</h2>
- <p> Scripting brings a poweful form of dynamic extensibilty to Ghidra,
+ <p> Scripting brings a powerful 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>
+ might be worth the effort to split up code into <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:8em">
<li>The unit of modularity in OSGi is the <em>bundle</em>. Bundles are
- <em>mostly independent</em> components with well-defined
- collaborations.
+ <em>mostly independent</em> components with declared imports and exports.
</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>
+ manifest file that tells the framework what it uses/imports and what it
+ provides/exports.
+ <br/>
+ Bundles are Jars, but Ghidra will automatically compile source
+ directories to bundles. We refer to source directories intended to be
+ used in OSGi as <a href="BundleManager.htm#sourceBundles"> <em>source
+ bundles</em></a>. Each Ghidra script directory is a seperate
+ source bundle.</li>
<li>Bundles can <em>export</em> Java packages for use by other bundles,
and packages can have version numbers assigned.</li>
@@ -34,36 +38,32 @@
<li>Bundles can <em>import</em> packages from other bundles, and each
import can be constrained by version number or range.</li>
- <li>Bundles are Jars, but Ghidra will automatically compile source
- directories to bundles. We refer to source directories intended to be
- used in OSGi as <a href="BundleManager.htm#sourceBundles"> <em>source
- bundles</em></a>. <em>In particular</em> Ghidra script directories
- are source bundles.
+ <li>The entire Ghidra API is part of the "system bundle", meaning its packages don't need
+ need to be explicitly imported.
+ </li>
- <li>The entire Ghidra API is part of the "system bundle", so hosted
- bundles have complete access.</li>
+ <li>A bundle can provide an <em>activator</em> class whose start and stop methods
+ are called when the bundle is activated/deactivated.
+ </li>
</ol>
<h2><a name="sourceBundles"/>Source bundles</h2>
<p>When a directory is added to the Bundle Manager, it is treated as a
- <em>source bundle</em>. When enabled, its Java contents are 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>
+ <em>source bundle</em>. When enabled, its Java contents are compiled to
+ <pre>
+ &lt;user home&gt;/.ghidra/.ghidra-&lt;version&gt;/osgi/compiled-bundles/&lt;hash&gt;/
+ </pre>
+ where <code>&lt;hash&gt;</code> is a hash of the source bundle path. These compiled artifacts are then loaded
+ by the OSGi framework.</p>
<h3>exploded bundles</h3>
<p>
- Each subdirectory of <code>compiled-bundles/</code> is an <em>exploded
- jar</em> -- by compressing it, we get a standard Jar bundle:
- </p>
- <code>
- <pre>
+ Each such subdirectory of <code>compiled-bundles/</code> is an <em>exploded
+ jar</em> -- by compressing it, we get a standard OSGi Jar bundle:</p>
+ <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>
+ </pre>
<h3><a name="generated_files"/>generated files</h3>
<p>If there is no manifest in the source directory, Ghidra generates one
@@ -71,9 +71,9 @@
</p>
<ul style="padding-left:8em">
<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>
+ <li>Packages listed in <code>@importpackage</code> meta-data are imported from active bundles.</li>
</ul>
+ <p>Note: <em>import</em> and <em>export</em> here refer to inter-bundle dependency, see <a href="#inter-bundle-dependency">below</a></p>
<p>
If no bundle activator is present, a stub is created and referenced in the generated manifest.
</p>
@@ -82,57 +82,128 @@
<p>Two 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>
+ compile time -- if a class isn't imported or present, compilation will fail.</p>
- <h3>inter-bundle (<em>run time</em>) dependency</h3>
+ <p>In the following example, <code>IntraBundleExampleScript</code> depends on <code>mylib.MyLibrary</code> without
+ any special dependency declaration since they are both defined in the same source bundle,
+ <code>my_ghidra_scripts</code>:</p>
+ <pre>
+ my_ghidra_scripts/mylib/MyLibrary.java:
+ package mylib;
+
+ public class MyLibrary {
+ public void doStuff() {
+ // ...
+ }
+ }
+
+ my_ghidra_scripts/IntraBundleExampleScript.java
+ // Intra-bundle dependency example.
+ //@category Examples
+
+ import ghidra.app.script.GhidraScript;
+ import mylib.MyLibrary;
+
+ public class IntraBundleExampleScript extends GhidraScript {
+ @Override
+ public void run() throws Exception {
+ new MyLibrary().doStuff();
+ }
+ }
+ </pre>
+
+ <h3><a name="inter-bundle-dependency" />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
+ dependencies. 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
+ first match, in the order of bundle activation, 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>Note: OSGi bundle dependency is very similar to Java 9 modules. The key difference is that
+ Java 9 modules provide <em>load time</em> resolution, while OSGi provides <em>run 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>@importpackage</code> in a script</h4>
+ <p>Ghidra's <code>@importpacakge</code> meta-data tag provides a convenient way to declare inter-bundle
+ dependencies directly in a script. Set the value to a comma separated list of packages
+ to import from other bundles.</p>
- <h4><code>Import-Package</code> in the manifest</h4>
- <p>You can find a high level description of OSGi at <a href="https://osgi.org">https://osgi.org</a> and details
- <a href="https://osgi.org/specification/osgi.core/7.0.0/framework.module.html#framework.module.importpackage">
- here
- </a>.
-
- <p>Examples of the syntax for common cases are below:</p>
- <code>
+ <p>In the example below, the class <code>InterBundleExampleScript</code> is implemented in the bundle
+ <code>my_ghidra_scripts</code> and uses a class, <code>yourlib.YourLibrary</code>, defined in another bundle,
+ <code>your_ghidra_scripts</code>.</p>
<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
+ your_ghidra_scripts/yourlib/YourLibrary.java:
+ package yourlib;
+
+ public class YourLibrary {
+ public void doOtherStuff() {
+ // ...
+ }
+ }
+
+
+ my_ghidra_scripts/InterBundleExampleScript.java
+ // Inter-bundle dependency example.
+ //@category Examples
+ //@importpackage yourlib
+
+ import ghidra.app.script.GhidraScript;
+ import yourlib.YourLibrary;
+
+ public class InterBundleExampleScript extends GhidraScript {
+ @Override
+ public void run() throws Exception {
+ new YourLibrary().doOtherStuff();
+ }
+ }
</pre>
- </code>
- <p>Note: the file <code>META-INF/MANIFEST.MF</code> should have only <em>one</em>
- <code>Import-Package</code> entry.</p>
+ <h4><code>Import-Package</code> in the manifest</h4>
+ <p>The underlying OSGi mechanism for declaring inter-bundle dependency is via the manifest.</p>
+
+ <p>You can find detailed descriptions of manifest attributes used by OSGi, like <code>Import-Package</code>, here
+ <a href="https://osgi.org/specification/osgi.core/7.0.0/framework.module.html#framework.module.importpackage">
+ https://osgi.org/specification/osgi.core/7.0.0/framework.module.html#framework.module.importpackage</a>.
+ <p>If a Ghidra source bundle has no manifest, Ghidra <a href="#generated_files">generates one</a>.</p>
- <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="#generated_files">above</a>.</p>
+ <p>For example, the <code>@importpackage yourlib</code> in the previous example corresponds to the `yourlib` entry
+ in the `Import-Package` line of the manifest generated for <code>my_ghidra_scripts</code>:
+ </p>
+ <pre>
+ &lt;user home&gt;/.ghidra/.ghidra-&lt;version&gt;/osgi/compiled-bundles/ab12cd89/META-INF/MANIFEST.MF:
+ Manifest-Version: 1.0
+ Bundle-ManifestVersion: 2
+ Export-Package: mylib
+ Import-Package: ghidra.app.script,yourlib,ghidra.app.plugin.core.osgi
+ Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=11))"
+ Bundle-SymbolicName: ab12cd89
+ Bundle-Version: 1.0
+ Bundle-Name: ab12cd89
+ Bundle-Activator: GeneratedActivator
+ </pre>
+ The manifest generated for <code>your_ghidra_scripts</code> is as follows:
+ <pre>
+ &lt;user home&gt;/.ghidra/.ghidra-&lt;version&gt;/osgi/compiled-bundles/ef34ab56/META-INF/MANIFEST.MF:
+ Manifest-Version: 1.0
+ Bundle-ManifestVersion: 2
+ Export-Package: yourlib
+ Import-Package: ghidra.app.plugin.core.osgi
+ Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=11))"
+ Bundle-SymbolicName: ef34ab56
+ Bundle-Version: 1.0
+ Bundle-Name: ef34ab56
+ Bundle-Activator: GeneratedActivator
+ </pre>
+ <p>Notice the <code>Export-Package</code> line generated for <code>your_ghidra_scripts</code>. The generated
+ manifest exports all packages not including <code>private</code> or <code>internal</code>.</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 the root of a source bundle are appended to the generated manifest's
- <code>Import-Package</code> attribute.</p>
+ <p>For full control, users can include a manifest with their bundle's source,
+ e.g. <code>my_ghidra_scripts/META-INF/MANIFEST.MF</code>. </p>
<h2><a name="EnableBundles"/><a name="DisableBundles"/>Enabling and disabling bundles</h2>
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 a162814a7c..253d719d27 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
@@ -87,8 +87,9 @@
<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 content of this tag is formatted as the <a
+ <P>This tag is used to declare <A href="../BundleManager/BundleManager.htm#inter-bundle-dependency">
+ inter-bundle dependencies</A> as a comma separated list of Java packages. The complete 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>