|
| 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 | +} |
0 commit comments