@henols/c64-re-tools 2.6.0-rc.7 → 2.6.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/skills/c64-docs/SKILL.md +1 -1
- package/skills/c64-docs/scripts/docs.ts +6 -6
- package/skills/c64-knowledge/SKILL.md +3 -3
- package/skills/c64-knowledge/scripts/knowledge.ts +2 -0
- package/skills/c64-knowledge/scripts/lib/listing.ts +18 -3
- package/skills/c64-reverse-engineering/SKILL.md +1 -1
- package/skills/c64-source/SKILL.md +7 -7
- package/skills/c64-source/scripts/lib/source.ts +10 -0
- package/skills/c64-source/scripts/source.ts +32 -27
- package/skills/c64-static-analysis/SKILL.md +5 -3
- package/skills/c64-static-analysis/scripts/lib/instructions.ts +22 -20
- package/skills/c64-static-analysis/scripts/static-analysis.ts +16 -16
- package/skills/c64-testing/SKILL.md +2 -1
- package/skills/c64-testing/references/recording-format.md +17 -3
- package/skills/c64-testing/references/scenario-format.md +2 -3
- package/skills/c64-testing/scripts/lib/diff-files.ts +4 -18
- package/skills/c64-testing/scripts/lib/play.ts +39 -41
- package/skills/c64-testing/scripts/lib/recording.ts +21 -29
- package/skills/c64-testing/scripts/lib/runs.ts +2 -2
- package/skills/c64-testing/scripts/lib/scenario.ts +3 -5
- package/skills/c64-unpacker/SKILL.md +2 -1
- package/skills/c64-unpacker/scripts/lib/loads.ts +2 -2
- package/skills/emulator/SKILL.md +2 -5
- package/src/c64-keys.ts +98 -0
- package/src/cli/main.ts +1 -1
- package/src/host/vice/session.ts +82 -8
- package/src/host-client/steps.ts +17 -17
- package/src/host-client/tools.ts +1 -1
- package/src/host-client/vice-session.ts +7 -1
- package/src/knowledge/read.ts +25 -1
- package/src/mcp/inputs.ts +12 -0
- package/src/mcp/server.ts +1 -0
- package/src/mcp/tools/input.ts +38 -5
- package/src/native/c64run.ts +1 -1
- package/src/native/discover.ts +3 -3
- package/src/native/ghidra/analyze.ts +74 -63
- package/src/native/ghidra/c64rt-wasm.ts +4 -0
- package/src/native/ghidra/language/c64rt_6510.sla +0 -0
- package/src/native/ghidra/pcode.ts +222 -0
- package/src/native/ghidra/walk.ts +205 -0
- package/src/native/ghidra/wasm.ts +98 -0
- package/src/native/processes.ts +4 -4
- package/src/native/types.ts +2 -2
- package/src/project.ts +4 -3
- package/src/protocol/vice.ts +47 -0
- package/src/script.ts +1 -1
- package/src/vsf.ts +98 -0
- package/tools/c64run.c +9 -12
- package/tools/install-container.sh +1 -2
- package/src/native/ghidra/index.ts +0 -170
- package/src/native/ghidra/scripts/C64Export.java +0 -274
- package/src/native/ghidra/scripts/C64Prepare.java +0 -66
- package/tools/install-ghidra.sh +0 -40
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
// Runs after Ghidra's analysis. Arguments: <request.json> <out.json>.
|
|
2
|
-
//
|
|
3
|
-
// Writes the structural result for the loaded image: functions with the
|
|
4
|
-
// source of their names, code and defined-data regions, memory references
|
|
5
|
-
// from instructions, every instruction with its bytes, mnemonic, operand
|
|
6
|
-
// text and flow targets, and the requested decompilations within their bounds.
|
|
7
|
-
//@category c64-re-tools
|
|
8
|
-
|
|
9
|
-
import java.io.FileReader;
|
|
10
|
-
import java.io.FileWriter;
|
|
11
|
-
import java.util.LinkedHashSet;
|
|
12
|
-
import java.util.Set;
|
|
13
|
-
|
|
14
|
-
import com.google.gson.Gson;
|
|
15
|
-
import com.google.gson.JsonArray;
|
|
16
|
-
import com.google.gson.JsonElement;
|
|
17
|
-
import com.google.gson.JsonObject;
|
|
18
|
-
|
|
19
|
-
import ghidra.app.decompiler.DecompInterface;
|
|
20
|
-
import ghidra.app.decompiler.DecompileResults;
|
|
21
|
-
import ghidra.app.script.GhidraScript;
|
|
22
|
-
import ghidra.program.model.address.Address;
|
|
23
|
-
import ghidra.program.model.address.AddressSet;
|
|
24
|
-
import ghidra.program.model.listing.CodeUnit;
|
|
25
|
-
import ghidra.program.model.listing.Data;
|
|
26
|
-
import ghidra.program.model.listing.Function;
|
|
27
|
-
import ghidra.program.model.listing.Instruction;
|
|
28
|
-
import ghidra.program.model.symbol.RefType;
|
|
29
|
-
import ghidra.program.model.symbol.Reference;
|
|
30
|
-
import ghidra.program.model.symbol.SourceType;
|
|
31
|
-
|
|
32
|
-
public class C64Export extends GhidraScript {
|
|
33
|
-
private static final java.util.regex.Pattern NAME = java.util.regex.Pattern.compile("^\\.?[A-Za-z_][A-Za-z0-9_]{0,63}$");
|
|
34
|
-
/** The longest reason that the result check accepts. */
|
|
35
|
-
private static final int MAX_REASON = 1000;
|
|
36
|
-
/** The longest time for one decompilation, in seconds. */
|
|
37
|
-
private static final int MAX_DECOMPILE_SECONDS = 60;
|
|
38
|
-
|
|
39
|
-
@Override
|
|
40
|
-
public void run() throws Exception {
|
|
41
|
-
String[] args = getScriptArgs();
|
|
42
|
-
JsonObject request;
|
|
43
|
-
try (FileReader reader = new FileReader(args[0])) {
|
|
44
|
-
request = new Gson().fromJson(reader, JsonObject.class);
|
|
45
|
-
}
|
|
46
|
-
long start = request.get("start").getAsLong();
|
|
47
|
-
long end = request.get("end").getAsLong();
|
|
48
|
-
AddressSet coverage = new AddressSet(toAddr(start), toAddr(end));
|
|
49
|
-
|
|
50
|
-
JsonObject out = new JsonObject();
|
|
51
|
-
JsonArray coverageOut = new JsonArray();
|
|
52
|
-
coverageOut.add(range(start, end));
|
|
53
|
-
out.add("coverage", coverageOut);
|
|
54
|
-
out.add("functions", functions(coverage));
|
|
55
|
-
out.add("regions", regions(coverage));
|
|
56
|
-
out.add("references", references(coverage));
|
|
57
|
-
out.add("instructions", instructions(coverage));
|
|
58
|
-
JsonArray notDecompiled = new JsonArray();
|
|
59
|
-
out.add("decompilations", decompile(request, notDecompiled));
|
|
60
|
-
out.add("notDecompiled", notDecompiled);
|
|
61
|
-
JsonObject completeness = new JsonObject();
|
|
62
|
-
completeness.addProperty("functions", true);
|
|
63
|
-
completeness.addProperty("regions", true);
|
|
64
|
-
completeness.addProperty("references", true);
|
|
65
|
-
out.add("completeness", completeness);
|
|
66
|
-
try (FileWriter writer = new FileWriter(args[1])) {
|
|
67
|
-
new Gson().toJson(out, writer);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private static JsonObject range(long start, long end) {
|
|
72
|
-
JsonObject range = new JsonObject();
|
|
73
|
-
range.addProperty("start", start);
|
|
74
|
-
range.addProperty("end", end);
|
|
75
|
-
return range;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private JsonArray functions(AddressSet coverage) {
|
|
79
|
-
JsonArray functions = new JsonArray();
|
|
80
|
-
for (Function function : currentProgram.getFunctionManager().getFunctions(coverage, true)) {
|
|
81
|
-
String name = function.getName();
|
|
82
|
-
SourceType source = function.getSymbol().getSource();
|
|
83
|
-
// Only the seeds carry user-defined names in this disposable project.
|
|
84
|
-
String nameSource = source == SourceType.USER_DEFINED ? "seed" : source == SourceType.DEFAULT ? "generated" : "native";
|
|
85
|
-
if (!NAME.matcher(name).matches()) {
|
|
86
|
-
// A name that this script makes is generated, whatever name it replaces.
|
|
87
|
-
name = String.format("FUN_%04x", function.getEntryPoint().getOffset());
|
|
88
|
-
nameSource = "generated";
|
|
89
|
-
}
|
|
90
|
-
JsonObject item = new JsonObject();
|
|
91
|
-
item.addProperty("entry", function.getEntryPoint().getOffset());
|
|
92
|
-
item.addProperty("name", name);
|
|
93
|
-
item.addProperty("nameSource", nameSource);
|
|
94
|
-
functions.add(item);
|
|
95
|
-
}
|
|
96
|
-
return functions;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Instructions are code and defined data is data; undefined bytes are not classified. */
|
|
100
|
-
private JsonArray regions(AddressSet coverage) {
|
|
101
|
-
JsonArray regions = new JsonArray();
|
|
102
|
-
String kind = null;
|
|
103
|
-
long first = 0;
|
|
104
|
-
long last = -2;
|
|
105
|
-
for (CodeUnit unit : currentProgram.getListing().getCodeUnits(coverage, true)) {
|
|
106
|
-
String unitKind = unit instanceof Instruction ? "code" : (unit instanceof Data && ((Data) unit).isDefined()) ? "data" : null;
|
|
107
|
-
long unitStart = unit.getMinAddress().getOffset();
|
|
108
|
-
long unitEnd = Math.min(unit.getMaxAddress().getOffset(), coverage.getMaxAddress().getOffset());
|
|
109
|
-
if (unitKind != null && unitKind.equals(kind) && unitStart == last + 1) {
|
|
110
|
-
last = unitEnd;
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
if (kind != null) {
|
|
114
|
-
JsonObject region = range(first, last);
|
|
115
|
-
region.addProperty("classification", kind);
|
|
116
|
-
regions.add(region);
|
|
117
|
-
}
|
|
118
|
-
kind = unitKind;
|
|
119
|
-
first = unitStart;
|
|
120
|
-
last = unitEnd;
|
|
121
|
-
}
|
|
122
|
-
if (kind != null) {
|
|
123
|
-
JsonObject region = range(first, last);
|
|
124
|
-
region.addProperty("classification", kind);
|
|
125
|
-
regions.add(region);
|
|
126
|
-
}
|
|
127
|
-
return regions;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
private JsonArray references(AddressSet coverage) {
|
|
131
|
-
Set<String> seen = new LinkedHashSet<>();
|
|
132
|
-
JsonArray references = new JsonArray();
|
|
133
|
-
for (Instruction instruction : currentProgram.getListing().getInstructions(coverage, true)) {
|
|
134
|
-
for (Reference reference : instruction.getReferencesFrom()) {
|
|
135
|
-
RefType type = reference.getReferenceType();
|
|
136
|
-
Address to = reference.getToAddress();
|
|
137
|
-
if (!reference.isMemoryReference() || !to.isMemoryAddress() || type == RefType.FALL_THROUGH) {
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
long from = instruction.getMinAddress().getOffset();
|
|
141
|
-
if (type.isCall()) {
|
|
142
|
-
add(references, seen, from, to.getOffset(), "call");
|
|
143
|
-
} else if (type.isJump() || type.isFlow()) {
|
|
144
|
-
add(references, seen, from, to.getOffset(), "jump");
|
|
145
|
-
} else {
|
|
146
|
-
if (type.isRead()) {
|
|
147
|
-
add(references, seen, from, to.getOffset(), "read");
|
|
148
|
-
}
|
|
149
|
-
if (type.isWrite()) {
|
|
150
|
-
add(references, seen, from, to.getOffset(), "write");
|
|
151
|
-
}
|
|
152
|
-
if (!type.isRead() && !type.isWrite()) {
|
|
153
|
-
add(references, seen, from, to.getOffset(), "reference");
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return references;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Every instruction in the coverage, in address order, as Ghidra decoded it with the 6510 language. */
|
|
162
|
-
private JsonArray instructions(AddressSet coverage) throws Exception {
|
|
163
|
-
JsonArray instructions = new JsonArray();
|
|
164
|
-
for (Instruction instruction : currentProgram.getListing().getInstructions(coverage, true)) {
|
|
165
|
-
JsonObject item = new JsonObject();
|
|
166
|
-
item.addProperty("address", instruction.getMinAddress().getOffset());
|
|
167
|
-
StringBuilder bytes = new StringBuilder();
|
|
168
|
-
for (byte value : instruction.getBytes()) {
|
|
169
|
-
bytes.append(String.format("%02x", value & 0xff));
|
|
170
|
-
}
|
|
171
|
-
item.addProperty("bytes", bytes.toString());
|
|
172
|
-
item.addProperty("mnemonic", instruction.getMnemonicString().toLowerCase());
|
|
173
|
-
StringBuilder operands = new StringBuilder();
|
|
174
|
-
for (int index = 0; index < instruction.getNumOperands(); index++) {
|
|
175
|
-
if (index > 0) {
|
|
176
|
-
operands.append(",");
|
|
177
|
-
}
|
|
178
|
-
operands.append(instruction.getDefaultOperandRepresentation(index));
|
|
179
|
-
}
|
|
180
|
-
item.addProperty("operands", operands.toString());
|
|
181
|
-
JsonArray flows = new JsonArray();
|
|
182
|
-
for (Address flow : instruction.getFlows()) {
|
|
183
|
-
flows.add(flow.getOffset());
|
|
184
|
-
}
|
|
185
|
-
item.add("flows", flows);
|
|
186
|
-
instructions.add(item);
|
|
187
|
-
}
|
|
188
|
-
return instructions;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
private static void add(JsonArray references, Set<String> seen, long from, long to, String type) {
|
|
192
|
-
if (!seen.add(from + ":" + to + ":" + type)) {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
JsonObject reference = new JsonObject();
|
|
196
|
-
reference.addProperty("from", from);
|
|
197
|
-
reference.addProperty("to", to);
|
|
198
|
-
reference.addProperty("type", type);
|
|
199
|
-
references.add(reference);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** A requested routine without decompiled code, and the decompiler's reason. */
|
|
203
|
-
private static void notDecompiled(JsonArray list, long entry, String reason) {
|
|
204
|
-
JsonObject item = new JsonObject();
|
|
205
|
-
item.addProperty("entry", entry);
|
|
206
|
-
String text = reason == null || reason.isBlank() ? "the decompiler gave no reason" : reason.strip();
|
|
207
|
-
item.addProperty("reason", text.length() > MAX_REASON ? text.substring(0, MAX_REASON) : text);
|
|
208
|
-
list.add(item);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private JsonArray decompile(JsonObject request, JsonArray failures) {
|
|
212
|
-
JsonArray decompilations = new JsonArray();
|
|
213
|
-
JsonArray entries = request.getAsJsonArray("decompile");
|
|
214
|
-
if (entries.size() == 0) {
|
|
215
|
-
return decompilations;
|
|
216
|
-
}
|
|
217
|
-
int perFunction = request.get("maxChars").getAsInt();
|
|
218
|
-
int remaining = request.get("maxTotalChars").getAsInt();
|
|
219
|
-
// The time that is left for the decompilations, as a clock time in milliseconds.
|
|
220
|
-
long deadline = request.get("decompileDeadline").getAsLong();
|
|
221
|
-
int routinesLeft = entries.size();
|
|
222
|
-
DecompInterface decompiler = new DecompInterface();
|
|
223
|
-
try {
|
|
224
|
-
// Ghidra ships no native decompiler for some platforms (macOS): it then does not start.
|
|
225
|
-
if (!decompiler.openProgram(currentProgram)) {
|
|
226
|
-
String reason = "the Ghidra decompiler did not start: " + decompiler.getLastMessage();
|
|
227
|
-
for (JsonElement element : entries) {
|
|
228
|
-
notDecompiled(failures, element.getAsLong(), reason);
|
|
229
|
-
}
|
|
230
|
-
return decompilations;
|
|
231
|
-
}
|
|
232
|
-
for (JsonElement element : entries) {
|
|
233
|
-
// Each routine gets an equal part of the time that is left, so every routine gets a turn.
|
|
234
|
-
long secondsLeft = (deadline - System.currentTimeMillis()) / 1000;
|
|
235
|
-
int seconds = (int) Math.min(MAX_DECOMPILE_SECONDS, secondsLeft / routinesLeft);
|
|
236
|
-
routinesLeft--;
|
|
237
|
-
Function function = getFunctionAt(toAddr(element.getAsLong()));
|
|
238
|
-
if (function == null) {
|
|
239
|
-
notDecompiled(failures, element.getAsLong(), "no function starts at this address");
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
if (remaining <= 0) {
|
|
243
|
-
notDecompiled(failures, element.getAsLong(), "the decompiled text of the other routines used the whole limit");
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
if (seconds < 1) {
|
|
247
|
-
notDecompiled(failures, element.getAsLong(), "the time for the Ghidra run ended before this routine");
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
DecompileResults results = decompiler.decompileFunction(function, seconds, monitor);
|
|
251
|
-
if (!results.decompileCompleted() || results.getDecompiledFunction() == null) {
|
|
252
|
-
notDecompiled(failures, element.getAsLong(), results.getErrorMessage());
|
|
253
|
-
continue;
|
|
254
|
-
}
|
|
255
|
-
String text = results.getDecompiledFunction().getC();
|
|
256
|
-
int limit = Math.min(perFunction, remaining);
|
|
257
|
-
boolean truncated = text.length() > limit;
|
|
258
|
-
if (truncated) {
|
|
259
|
-
text = text.substring(0, limit);
|
|
260
|
-
}
|
|
261
|
-
remaining -= text.length();
|
|
262
|
-
JsonObject item = new JsonObject();
|
|
263
|
-
item.addProperty("entry", function.getEntryPoint().getOffset());
|
|
264
|
-
item.addProperty("text", text);
|
|
265
|
-
item.addProperty("truncated", truncated);
|
|
266
|
-
decompilations.add(item);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
finally {
|
|
270
|
-
decompiler.dispose();
|
|
271
|
-
}
|
|
272
|
-
return decompilations;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
// Runs before Ghidra's analysis. Arguments: <seeds.json>.
|
|
2
|
-
//
|
|
3
|
-
// Fills the rest of the 64 KiB address space with uninitialized memory, so
|
|
4
|
-
// references to I/O and other RAM resolve, then applies the knowledge seeds:
|
|
5
|
-
// labels (user-defined names), data ranges (defined bytes that analysis does
|
|
6
|
-
// not disassemble), entry points (disassembled functions) and code starts
|
|
7
|
-
// (disassembled code without a function: a branch target is no routine).
|
|
8
|
-
//@category c64-re-tools
|
|
9
|
-
|
|
10
|
-
import java.io.FileReader;
|
|
11
|
-
|
|
12
|
-
import com.google.gson.Gson;
|
|
13
|
-
import com.google.gson.JsonElement;
|
|
14
|
-
import com.google.gson.JsonObject;
|
|
15
|
-
|
|
16
|
-
import ghidra.app.script.GhidraScript;
|
|
17
|
-
import ghidra.program.model.address.Address;
|
|
18
|
-
import ghidra.program.model.data.ArrayDataType;
|
|
19
|
-
import ghidra.program.model.data.ByteDataType;
|
|
20
|
-
import ghidra.program.model.mem.Memory;
|
|
21
|
-
import ghidra.program.model.symbol.SourceType;
|
|
22
|
-
|
|
23
|
-
public class C64Prepare extends GhidraScript {
|
|
24
|
-
@Override
|
|
25
|
-
public void run() throws Exception {
|
|
26
|
-
JsonObject seeds;
|
|
27
|
-
try (FileReader reader = new FileReader(getScriptArgs()[0])) {
|
|
28
|
-
seeds = new Gson().fromJson(reader, JsonObject.class);
|
|
29
|
-
}
|
|
30
|
-
long start = seeds.get("start").getAsLong();
|
|
31
|
-
long end = seeds.get("end").getAsLong();
|
|
32
|
-
Memory memory = currentProgram.getMemory();
|
|
33
|
-
if (start > 0) {
|
|
34
|
-
memory.createUninitializedBlock("RAM_LOW", toAddr(0), start, false);
|
|
35
|
-
}
|
|
36
|
-
if (end < 0xffff) {
|
|
37
|
-
memory.createUninitializedBlock("RAM_HIGH", toAddr(end + 1), 0xffff - end, false);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
for (JsonElement element : seeds.getAsJsonArray("labels")) {
|
|
41
|
-
JsonObject label = element.getAsJsonObject();
|
|
42
|
-
createLabel(toAddr(label.get("address").getAsLong()), label.get("name").getAsString(), true, SourceType.USER_DEFINED);
|
|
43
|
-
}
|
|
44
|
-
for (JsonElement element : seeds.getAsJsonArray("dataRanges")) {
|
|
45
|
-
JsonObject range = element.getAsJsonObject();
|
|
46
|
-
Address first = toAddr(range.get("start").getAsLong());
|
|
47
|
-
Address last = toAddr(range.get("end").getAsLong());
|
|
48
|
-
int length = (int) (last.getOffset() - first.getOffset() + 1);
|
|
49
|
-
currentProgram.getListing().clearCodeUnits(first, last, false);
|
|
50
|
-
currentProgram.getListing().createData(first, new ArrayDataType(ByteDataType.dataType, length, 1));
|
|
51
|
-
}
|
|
52
|
-
for (JsonElement element : seeds.getAsJsonArray("entryPoints")) {
|
|
53
|
-
Address entry = toAddr(element.getAsLong());
|
|
54
|
-
addEntryPoint(entry);
|
|
55
|
-
disassemble(entry);
|
|
56
|
-
if (getFunctionAt(entry) == null) {
|
|
57
|
-
createFunction(entry, null);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (seeds.has("codeStarts")) {
|
|
61
|
-
for (JsonElement element : seeds.getAsJsonArray("codeStarts")) {
|
|
62
|
-
disassemble(toAddr(element.getAsLong()));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
package/tools/install-ghidra.sh
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# Installs Ghidra in /opt/ghidra_<version>_PUBLIC and points /opt/ghidra at
|
|
3
|
-
# it, so that C64RT_GHIDRA=/opt/ghidra stays the same when the version
|
|
4
|
-
# changes. Removes the Ghidra versions that it installed before. Ghidra needs
|
|
5
|
-
# Java 21. install-container.sh runs it as root.
|
|
6
|
-
|
|
7
|
-
set -Eeuo pipefail
|
|
8
|
-
|
|
9
|
-
GHIDRA_VERSION="12.1.3"
|
|
10
|
-
GHIDRA_ZIP="ghidra_${GHIDRA_VERSION}_PUBLIC_20260817.zip"
|
|
11
|
-
GHIDRA_URL="https://github.com/NationalSecurityAgency/ghidra/releases/download/Ghidra_${GHIDRA_VERSION}_build/${GHIDRA_ZIP}"
|
|
12
|
-
GHIDRA_SHA256="93a5d11a9ad510622acaaf908c556a7b9b764d338e78a7567f3689bf5081fd54"
|
|
13
|
-
DEST="/opt"
|
|
14
|
-
GHIDRA_DIR="${DEST}/ghidra_${GHIDRA_VERSION}_PUBLIC"
|
|
15
|
-
|
|
16
|
-
fail() {
|
|
17
|
-
printf 'Ghidra: %s\n' "$*" >&2
|
|
18
|
-
exit 1
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
[[ "$(id -u)" -eq 0 ]] || fail "run as root."
|
|
22
|
-
|
|
23
|
-
if [[ ! -d "${GHIDRA_DIR}" ]]; then
|
|
24
|
-
BUILD_DIR="$(mktemp -d -t ghidra-XXXXXXXX)"
|
|
25
|
-
trap 'rm -rf "${BUILD_DIR}"' EXIT
|
|
26
|
-
curl -fsSL --retry 3 -o "${BUILD_DIR}/${GHIDRA_ZIP}" "${GHIDRA_URL}"
|
|
27
|
-
echo "${GHIDRA_SHA256} ${BUILD_DIR}/${GHIDRA_ZIP}" | sha256sum -c - > /dev/null || fail "the archive has the wrong checksum."
|
|
28
|
-
unzip -q "${BUILD_DIR}/${GHIDRA_ZIP}" -d "${BUILD_DIR}/unpacked"
|
|
29
|
-
# Ghidra ships its decompiler only for x86-64; on arm64 its own Gradle wrapper builds it.
|
|
30
|
-
if [[ "$(uname -m)" == aarch64 || "$(uname -m)" == arm64 ]]; then
|
|
31
|
-
(cd "${BUILD_DIR}/unpacked/ghidra_${GHIDRA_VERSION}_PUBLIC/support/gradle" && GRADLE_USER_HOME="${BUILD_DIR}/gradle" ./gradlew --no-daemon buildNatives)
|
|
32
|
-
fi
|
|
33
|
-
mv "${BUILD_DIR}/unpacked/ghidra_${GHIDRA_VERSION}_PUBLIC" "${GHIDRA_DIR}"
|
|
34
|
-
fi
|
|
35
|
-
|
|
36
|
-
ln -sfn "${GHIDRA_DIR}" "${DEST}/ghidra"
|
|
37
|
-
for old in "${DEST}"/ghidra_*_PUBLIC; do
|
|
38
|
-
[[ "${old}" == "${GHIDRA_DIR}" || ! -d "${old}" ]] || rm -rf "${old}"
|
|
39
|
-
done
|
|
40
|
-
echo "Ghidra ${GHIDRA_VERSION} is installed in ${GHIDRA_DIR}, and ${DEST}/ghidra points at it."
|