-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlist_xrefs.cpp
More file actions
86 lines (73 loc) · 2.74 KB
/
Copy pathlist_xrefs.cpp
File metadata and controls
86 lines (73 loc) · 2.74 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
// 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/.
// list_xrefs: Demonstrate cross-reference extraction via the local backend.
//
// Usage: list_xrefs <binary_path> [ghidra_root] [arch]
//
// Demonstrates: CreateLocalClient -> OpenProgram -> ListXrefs.
// Decompiles all discovered functions, then extracts call and data xrefs
// from the decompiler's pcode analysis.
//
// If ghidra_root is omitted, uses embedded processor specs (no Ghidra needed).
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include "libghidra/ghidra.hpp"
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0]
<< " <binary_path> [ghidra_root] [arch]\n";
return 1;
}
const std::string binary_path = argv[1];
const std::string ghidra_root = (argc >= 3) ? argv[2] : "";
const std::string arch = (argc >= 4) ? argv[3] : "";
auto client = ghidra::local({
.ghidra_root = ghidra_root,
.default_arch = arch,
});
// Open the binary
ghidra::OpenRequest req;
req.program_path = binary_path;
auto open_result = client->OpenProgram(req);
if (!open_result.ok()) {
std::cerr << "Failed to load binary: " << open_result.status.message << "\n";
return 1;
}
std::cout << "Loaded: " << open_result.value->program_name << "\n\n";
// List all functions first
auto funcs = client->ListFunctions(0, INT64_MAX, 0, 0);
if (!funcs.ok()) {
std::cerr << "ListFunctions failed: " << funcs.status.message << "\n";
return 1;
}
std::cout << "Found " << funcs.value->functions.size() << " functions\n\n";
// Get xrefs across the entire address space
auto xrefs = client->ListXrefs(0, INT64_MAX, 0, 0);
if (!xrefs.ok()) {
std::cerr << "ListXrefs failed: " << xrefs.status.message << "\n";
return 1;
}
std::cout << "Cross-references (" << xrefs.value->xrefs.size() << " total):\n";
std::cout << std::string(72, '-') << "\n";
std::cout << std::left << std::setw(18) << "FROM"
<< std::setw(18) << "TO"
<< std::setw(20) << "TYPE"
<< "FLAGS\n";
std::cout << std::string(72, '-') << "\n";
for (const auto& x : xrefs.value->xrefs) {
std::cout << "0x" << std::hex << std::setfill('0') << std::setw(12) << x.from_address
<< " 0x" << std::setw(12) << x.to_address
<< " " << std::setfill(' ') << std::left << std::setw(20) << x.ref_type;
if (x.is_flow) std::cout << "flow ";
if (x.is_memory) std::cout << "mem ";
std::cout << "\n";
}
return 0;
}