From 44edb3383d94af74efc0b0a5d9a6fcfc25e38652 Mon Sep 17 00:00:00 2001 From: ZivDero Date: Mon, 8 Sep 2025 16:43:28 +0300 Subject: [PATCH 1/4] Use the Windows API to demangle MSVC symbols on Windows --- Cargo.lock | 1 + objdiff-core/Cargo.toml | 1 + objdiff-core/src/arch/x86.rs | 34 +++++++++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 306b957..39c4e63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3507,6 +3507,7 @@ dependencies = [ "typed-path", "unarm", "winapi", + "windows-sys 0.59.0", "yaxpeax-arch", "yaxpeax-arm", ] diff --git a/objdiff-core/Cargo.toml b/objdiff-core/Cargo.toml index 52acf71..6358327 100644 --- a/objdiff-core/Cargo.toml +++ b/objdiff-core/Cargo.toml @@ -162,6 +162,7 @@ rabbitizer = { version = "2.0.0-alpha.4", default-features = false, features = [ cpp_demangle = { version = "0.4", default-features = false, features = ["alloc"], optional = true } iced-x86 = { version = "1.21", default-features = false, features = ["decoder", "intel", "gas", "masm", "nasm", "exhaustive_enums", "no_std"], optional = true } msvc-demangler = { version = "0.11", optional = true } +windows-sys = { version = "0.59", features = ["Win32_System_Diagnostics_Debug"] } # arm unarm = { version = "1.9", optional = true } diff --git a/objdiff-core/src/arch/x86.rs b/objdiff-core/src/arch/x86.rs index 7b0bf58..26a55af 100644 --- a/objdiff-core/src/arch/x86.rs +++ b/objdiff-core/src/arch/x86.rs @@ -304,12 +304,36 @@ impl Arch for ArchX86 { fn demangle(&self, name: &str) -> Option { if name.starts_with('?') { - msvc_demangler::demangle(name, msvc_demangler::DemangleFlags::llvm()).ok() - } else { - cpp_demangle::Symbol::new(name) - .ok() - .and_then(|s| s.demangle(&cpp_demangle::DemangleOptions::default()).ok()) + #[cfg(target_os = "windows")] + { + use std::ffi::{CString, CStr}; + use windows_sys::Win32::System::Diagnostics::Debug::UnDecorateSymbolName; + + let cstr = CString::new(name).ok()?; + let mut buffer = vec![0u8; 1024]; + + unsafe { + let len = UnDecorateSymbolName( + cstr.as_ptr() as *const u8, + buffer.as_mut_ptr(), + buffer.len() as u32, + 0, // UNDNAME_COMPLETE + ); + if len > 0 { + let result = CStr::from_ptr(buffer.as_ptr() as *const i8) + .to_str() + .ok()? + .to_string(); + return Some(result); + } + } + } + return msvc_demangler::demangle(name, msvc_demangler::DemangleFlags::llvm()).ok(); } + + cpp_demangle::Symbol::new(name) + .ok() + .and_then(|s| s.demangle(&cpp_demangle::DemangleOptions::default()).ok()) } fn reloc_name(&self, flags: RelocationFlags) -> Option<&'static str> { From bd93b8dc41f41d42a2349c9b1ce9f26c9ceebb08 Mon Sep 17 00:00:00 2001 From: ZivDero Date: Mon, 8 Sep 2025 16:54:19 +0300 Subject: [PATCH 2/4] Adjust formatting --- objdiff-core/src/arch/x86.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/objdiff-core/src/arch/x86.rs b/objdiff-core/src/arch/x86.rs index 26a55af..464be87 100644 --- a/objdiff-core/src/arch/x86.rs +++ b/objdiff-core/src/arch/x86.rs @@ -306,7 +306,7 @@ impl Arch for ArchX86 { if name.starts_with('?') { #[cfg(target_os = "windows")] { - use std::ffi::{CString, CStr}; + use std::ffi::{CStr, CString}; use windows_sys::Win32::System::Diagnostics::Debug::UnDecorateSymbolName; let cstr = CString::new(name).ok()?; @@ -320,10 +320,8 @@ impl Arch for ArchX86 { 0, // UNDNAME_COMPLETE ); if len > 0 { - let result = CStr::from_ptr(buffer.as_ptr() as *const i8) - .to_str() - .ok()? - .to_string(); + let result = + CStr::from_ptr(buffer.as_ptr() as *const i8).to_str().ok()?.to_string(); return Some(result); } } From 5cf05536d0a1deaeb96cd1558f541d5e4af84a4d Mon Sep 17 00:00:00 2001 From: ZivDero Date: Mon, 8 Sep 2025 17:43:22 +0300 Subject: [PATCH 3/4] Fix wrong types causing errors --- objdiff-core/src/arch/x86.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/objdiff-core/src/arch/x86.rs b/objdiff-core/src/arch/x86.rs index 464be87..25595c1 100644 --- a/objdiff-core/src/arch/x86.rs +++ b/objdiff-core/src/arch/x86.rs @@ -314,8 +314,8 @@ impl Arch for ArchX86 { unsafe { let len = UnDecorateSymbolName( - cstr.as_ptr() as *const u8, - buffer.as_mut_ptr(), + cstr.as_ptr() as windows_sys::core::PCSTR, + buffer.as_mut_ptr() as windows_sys::core::PSTR, buffer.len() as u32, 0, // UNDNAME_COMPLETE ); From 8487bef5cad8679a67310420ac7298e2a0ba70fb Mon Sep 17 00:00:00 2001 From: ZivDero Date: Tue, 9 Sep 2025 05:04:47 +0300 Subject: [PATCH 4/4] Attempt to get wasm to build --- Cargo.lock | 19 +++++++++++++++++-- objdiff-core/Cargo.toml | 2 +- objdiff-core/src/arch/x86.rs | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 39c4e63..0d7178e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3507,7 +3507,7 @@ dependencies = [ "typed-path", "unarm", "winapi", - "windows-sys 0.59.0", + "windows-sys 0.61.0", "yaxpeax-arch", "yaxpeax-arm", ] @@ -6282,6 +6282,12 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" +[[package]] +name = "windows-link" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45e46c0661abb7180e7b9c281db115305d49ca1709ab8242adf09666d2173c65" + [[package]] name = "windows-result" version = "0.2.0" @@ -6361,6 +6367,15 @@ dependencies = [ "windows-targets 0.53.3", ] +[[package]] +name = "windows-sys" +version = "0.61.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e201184e40b2ede64bc2ea34968b28e33622acdbbf37104f0e4a33f7abe657aa" +dependencies = [ + "windows-link 0.2.0", +] + [[package]] name = "windows-targets" version = "0.42.2" @@ -6413,7 +6428,7 @@ version = "0.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5fe6031c4041849d7c496a8ded650796e7b6ecc19df1a431c1a363342e5dc91" dependencies = [ - "windows-link", + "windows-link 0.1.3", "windows_aarch64_gnullvm 0.53.0", "windows_aarch64_msvc 0.53.0", "windows_i686_gnu 0.53.0", diff --git a/objdiff-core/Cargo.toml b/objdiff-core/Cargo.toml index 6358327..2f8998e 100644 --- a/objdiff-core/Cargo.toml +++ b/objdiff-core/Cargo.toml @@ -162,7 +162,6 @@ rabbitizer = { version = "2.0.0-alpha.4", default-features = false, features = [ cpp_demangle = { version = "0.4", default-features = false, features = ["alloc"], optional = true } iced-x86 = { version = "1.21", default-features = false, features = ["decoder", "intel", "gas", "masm", "nasm", "exhaustive_enums", "no_std"], optional = true } msvc-demangler = { version = "0.11", optional = true } -windows-sys = { version = "0.59", features = ["Win32_System_Diagnostics_Debug"] } # arm unarm = { version = "1.9", optional = true } @@ -182,6 +181,7 @@ encoding_rs = { version = "0.8.35", optional = true } [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", optional = true, features = ["winbase"] } +windows-sys = { version = "0.61", optional = true, features = ["Win32_System_Diagnostics_Debug"] } # For Linux static binaries, use rustls [target.'cfg(target_os = "linux")'.dependencies] diff --git a/objdiff-core/src/arch/x86.rs b/objdiff-core/src/arch/x86.rs index 25595c1..70df4d8 100644 --- a/objdiff-core/src/arch/x86.rs +++ b/objdiff-core/src/arch/x86.rs @@ -307,6 +307,7 @@ impl Arch for ArchX86 { #[cfg(target_os = "windows")] { use std::ffi::{CStr, CString}; + use windows_sys::Win32::System::Diagnostics::Debug::UnDecorateSymbolName; let cstr = CString::new(name).ok()?;