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 tools/import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ python -m pip install -r requirements.txt
```

> By default, all .hpp files are backed up to the tmp\backup\ folder. When the .sarif file has not changed between calls of this script, use `-ClearCache $false` to speed up the conversion.

## Usage with a .gzf

`ghidra-skink` can read the program straight from a Ghidra `.gzf` with pyghidra, so the SARIF export is not needed.
It generates the same header files in about half a minute instead of about ten minutes.

0. Install pyghidra (also used by `gzftosarif.py`), and set `GHIDRA_INSTALL_DIR` to your Ghidra installation:

```pwsh
python -m pip install pyghidra
```

1. Export the program from Ghidra as a .gzf (File > Export Program..., format "Ghidra Zip File").
2. Execute the following from PowerShell:

```pwsh
& .\tools\import\reimport-ghidra.ps1 -OverwriteAll $true -Gzf "path/to/gzf/file.gzf"
```
20 changes: 11 additions & 9 deletions tools/import/ghidra-importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from skink.export.enums.enumfamilies import EnumFamily, collect_enum_families
import pathlib
from skink.export.styles.style3.exporter import Exporter, BinaryContext, TransformationRules, FileRules
from skink.utils.unique import unique

from skink.export.context import DEFAULT, Context
ctx: Context = DEFAULT.copy() # type: ignore
Expand All @@ -29,6 +30,7 @@
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--sarif", required=False, default="Stronghold Crusader.exe.sarif")
parser.add_argument("--gzf", required=False, default=None, help="Read the program from this .gzf with pyghidra instead of from --sarif")
parser.add_argument("--clear-cache", required=False, action=argparse.BooleanOptionalAction, default=False)
parser.add_argument("--output-dir", required=False, default='src')
parser.add_argument("--verbose", default=False, action=argparse.BooleanOptionalAction)
Expand All @@ -48,9 +50,14 @@
print(args)
exit(0)

project = Project(args.sarif,
cache_objects=args.cache,
cache_path=".cache" if args.cache else None)
if args.gzf:
# Read the program straight from the gzf with pyghidra, no SARIF export and no cache needed
logging.log(logging.INFO, f"reading program from: {args.gzf}")
project = Project.from_gzf(args.gzf)
else:
project = Project(args.sarif,
cache_objects=args.cache,
cache_path=".cache" if args.cache else None)

# TODO: permit_overwrite = False
logging.log(logging.INFO, "processing all symbol results")
Expand All @@ -63,12 +70,7 @@
logging.log(logging.INFO, "processing all symbol results: finished")

logging.log(logging.INFO, "finding all by location")
objs: List[BasicResult] = []
objs_i = 0
for obj in project.find_all_by_location(location="/_HoldStrong", recursive=True, lookup_lsymbols=True):
if obj not in objs:
objs_i += 1
objs.append(obj)
objs: List[BasicResult] = unique(project.find_all_by_location(location="/_HoldStrong", recursive=True, lookup_lsymbols=True))
logging.log(logging.INFO, "finding all by location: finished")


Expand Down
15 changes: 11 additions & 4 deletions tools/import/reimport-ghidra.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
param(
[Parameter(Mandatory = $false)]
[string]$Sarif = "Stronghold Crusader.exe.sarif",
# Read the program straight from a .gzf with pyghidra (no SARIF export, no cache)
[string]$Gzf = "",
[string]$OutputDir = "src",
[bool]$ClearCache = $true,
[bool]$OverwriteAll = $true,
Expand All @@ -22,8 +24,13 @@ if ($Interactive) {

$pyArgs += "$ScriptPath"

$pyArgs += "--sarif"
$pyArgs += $Sarif
if ($Gzf) {
$pyArgs += "--gzf"
$pyArgs += $Gzf
} else {
$pyArgs += "--sarif"
$pyArgs += $Sarif
}

$pyArgs += "--output-dir"
$pyArgs += "$OutputDir"
Expand Down Expand Up @@ -63,12 +70,12 @@ if ($DryRun) {
$hppFiles | Remove-Item
}

if($GenerateSarif) {
if($GenerateSarif -and -not $Gzf) {
Write-Output "Generating sarif file from gzf"
& python .\tools\import\gzftosarif.py --sarif $Sarif
}

Write-Output "Converting sarif to .hpp files"
Write-Output "Converting $(if ($Gzf) { $Gzf } else { $Sarif }) to .hpp files"
# Execute the export
& python $pyArgs

Expand Down
Loading