Skip to content

Commit e2cf52a

Browse files
committed
[Swift] Initial demangling support
1 parent 6f51ac9 commit e2cf52a

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

Cargo.lock

Lines changed: 9 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" }
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
mod demangler;
2+
3+
use crate::demangler::SwiftDemangler;
14
use binaryninja::add_optional_plugin_dependency;
5+
use binaryninja::demangle::Demangler;
6+
use demangler::SwiftDemangler;
27

38
#[no_mangle]
49
#[allow(non_snake_case)]
@@ -12,5 +17,7 @@ pub extern "C" fn CorePluginDependencies() {
1217
pub extern "C" fn CorePluginInit() -> bool {
1318
binaryninja::tracing_init!("Plugin.Swift");
1419

20+
Demangler::register("Swift", SwiftDemangler);
21+
1522
true
1623
}

0 commit comments

Comments
 (0)