Skip to content

Commit 7aa5bd4

Browse files
committed
[Swift] Initial demangling support
1 parent a49c29b commit 7aa5bd4

5 files changed

Lines changed: 52 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/workflow_swift/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ binaryninja = { workspace = true }
1616
binaryninjacore-sys.workspace = true
1717
tracing = "0.1"
1818
thiserror = "2.0"
19+
swift-demangler = { git = "https://github.com/Vector35/swift-demangler.git" }

plugins/workflow_swift/demo/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ binaryninja = { workspace = true, features = ["demo"] }
1717
binaryninjacore-sys.workspace = true
1818
tracing = "0.1"
1919
thiserror = "2.0"
20+
swift-demangler = { git = "https://github.com/Vector35/swift-demangler.git" }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use binaryninja::architecture::CoreArchitecture;
2+
use binaryninja::binary_view::BinaryView;
3+
use binaryninja::demangle::CustomDemangler;
4+
use binaryninja::rc::Ref;
5+
use binaryninja::types::{QualifiedName, Type};
6+
7+
pub struct SwiftDemangler;
8+
9+
impl CustomDemangler for SwiftDemangler {
10+
fn is_mangled_string(&self, name: &str) -> bool {
11+
name.starts_with("$s")
12+
|| name.starts_with("_$s")
13+
|| name.starts_with("$S")
14+
|| name.starts_with("_$S")
15+
|| name.starts_with("$e")
16+
|| name.starts_with("_$e")
17+
|| name.starts_with("_T")
18+
}
19+
20+
fn demangle(
21+
&self,
22+
_arch: &CoreArchitecture,
23+
name: &str,
24+
_view: Option<Ref<BinaryView>>,
25+
) -> Option<(QualifiedName, Option<Ref<Type>>)> {
26+
let ctx = swift_demangler::Context::new();
27+
let symbol = swift_demangler::Symbol::parse(&ctx, name)?;
28+
// Use the canonical demangled form from the parsed node tree.
29+
// This matches what `xcrun swift-demangle` produces.
30+
// TODO: Use the structured Symbol API to also reconstruct BN Types.
31+
let demangled = symbol.display();
32+
Some((QualifiedName::from(demangled), None))
33+
}
34+
}

plugins/workflow_swift/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
mod demangler;
2+
13
use binaryninja::add_optional_plugin_dependency;
4+
use binaryninja::demangle::Demangler;
5+
use demangler::SwiftDemangler;
26

37
#[no_mangle]
48
#[allow(non_snake_case)]
@@ -12,5 +16,7 @@ pub extern "C" fn CorePluginDependencies() {
1216
pub extern "C" fn CorePluginInit() -> bool {
1317
binaryninja::tracing_init!("Plugin.Swift");
1418

19+
Demangler::register("Swift", SwiftDemangler);
20+
1521
true
1622
}

0 commit comments

Comments
 (0)