Skip to content

Commit 44edb33

Browse files
committed
Use the Windows API to demangle MSVC symbols on Windows
1 parent fbdaa89 commit 44edb33

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Cargo.lock

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

objdiff-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ rabbitizer = { version = "2.0.0-alpha.4", default-features = false, features = [
162162
cpp_demangle = { version = "0.4", default-features = false, features = ["alloc"], optional = true }
163163
iced-x86 = { version = "1.21", default-features = false, features = ["decoder", "intel", "gas", "masm", "nasm", "exhaustive_enums", "no_std"], optional = true }
164164
msvc-demangler = { version = "0.11", optional = true }
165+
windows-sys = { version = "0.59", features = ["Win32_System_Diagnostics_Debug"] }
165166

166167
# arm
167168
unarm = { version = "1.9", optional = true }

objdiff-core/src/arch/x86.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,36 @@ impl Arch for ArchX86 {
304304

305305
fn demangle(&self, name: &str) -> Option<String> {
306306
if name.starts_with('?') {
307-
msvc_demangler::demangle(name, msvc_demangler::DemangleFlags::llvm()).ok()
308-
} else {
309-
cpp_demangle::Symbol::new(name)
310-
.ok()
311-
.and_then(|s| s.demangle(&cpp_demangle::DemangleOptions::default()).ok())
307+
#[cfg(target_os = "windows")]
308+
{
309+
use std::ffi::{CString, CStr};
310+
use windows_sys::Win32::System::Diagnostics::Debug::UnDecorateSymbolName;
311+
312+
let cstr = CString::new(name).ok()?;
313+
let mut buffer = vec![0u8; 1024];
314+
315+
unsafe {
316+
let len = UnDecorateSymbolName(
317+
cstr.as_ptr() as *const u8,
318+
buffer.as_mut_ptr(),
319+
buffer.len() as u32,
320+
0, // UNDNAME_COMPLETE
321+
);
322+
if len > 0 {
323+
let result = CStr::from_ptr(buffer.as_ptr() as *const i8)
324+
.to_str()
325+
.ok()?
326+
.to_string();
327+
return Some(result);
328+
}
329+
}
330+
}
331+
return msvc_demangler::demangle(name, msvc_demangler::DemangleFlags::llvm()).ok();
312332
}
333+
334+
cpp_demangle::Symbol::new(name)
335+
.ok()
336+
.and_then(|s| s.demangle(&cpp_demangle::DemangleOptions::default()).ok())
313337
}
314338

315339
fn reloc_name(&self, flags: RelocationFlags) -> Option<&'static str> {

0 commit comments

Comments
 (0)