-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdecompile_project.cpp
More file actions
104 lines (91 loc) · 3.47 KB
/
Copy pathdecompile_project.cpp
File metadata and controls
104 lines (91 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2024-2026 Elias Bachaalany
// SPDX-License-Identifier: MPL-2.0
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// decompile_project: Open a Ghidra project (.gpr) and decompile functions via IClient.
//
// Usage: decompile_project <gpr_path> <binary_path> [function_name] [ghidra_root]
//
// If [function_name] is given, decompiles that specific function.
// Otherwise lists all functions and decompiles the first 3.
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include "libghidra/ghidra.hpp"
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0]
<< " <gpr_path> <binary_path> [function_name] [ghidra_root]\n";
return 1;
}
const std::string gpr_path = argv[1];
const std::string binary_path = argv[2];
const std::string target_func = (argc >= 4) ? argv[3] : "";
const std::string ghidra_root = (argc >= 5) ? argv[4] : "";
auto client = ghidra::local({
.ghidra_root = ghidra_root,
});
// --- Open via project path ---
std::cout << "Opening project: " << gpr_path << "\n";
ghidra::OpenRequest req;
req.project_path = gpr_path;
req.program_path = binary_path;
auto open_result = client->OpenProgram(req);
if (!open_result.ok()) {
std::cerr << "Failed: " << open_result.status.message << "\n";
return 1;
}
std::cout << "Binary loaded and project names applied.\n\n";
// --- List functions ---
auto funcs_result = client->ListFunctions(0, 0, 10000, 0);
if (!funcs_result.ok()) {
std::cerr << "ListFunctions failed: " << funcs_result.status.message << "\n";
return 1;
}
auto& funcs = funcs_result.value->functions;
std::cout << funcs.size() << " functions found.\n\n";
if (!target_func.empty()) {
// Find and decompile specific function
auto it = std::find_if(funcs.begin(), funcs.end(),
[&](const ghidra::Function& f) { return f.name == target_func; });
if (it == funcs.end()) {
std::cerr << "Function '" << target_func << "' not found\n";
return 1;
}
std::cout << "=== " << it->name << " @ 0x" << std::hex << it->entry_address
<< std::dec << " ===\n";
auto d = client->GetDecompilation(it->entry_address, 30000);
if (d.ok() && d.value->decompilation &&
!d.value->decompilation->pseudocode.empty()) {
std::cout << d.value->decompilation->pseudocode << "\n";
} else {
std::cerr << "Decompilation failed\n";
return 1;
}
} else {
// List all, decompile first 3
for (const auto& f : funcs) {
std::cout << " 0x" << std::hex << std::setfill('0') << std::setw(8)
<< f.entry_address << std::dec << " "
<< (f.name.empty() ? "(unnamed)" : f.name) << "\n";
}
int count = std::min(static_cast<int>(funcs.size()), 3);
for (int i = 0; i < count; i++) {
std::cout << "\n=== " << funcs[i].name << " @ 0x" << std::hex
<< funcs[i].entry_address << std::dec << " ===\n";
auto d = client->GetDecompilation(funcs[i].entry_address, 30000);
if (d.ok() && d.value->decompilation &&
!d.value->decompilation->pseudocode.empty()) {
std::cout << d.value->decompilation->pseudocode << "\n";
} else {
std::cerr << " (failed)\n";
}
}
}
return 0;
}