From 705d8b27b69a5617bc29a400df0df8d53315c3b1 Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Mon, 16 Sep 2024 09:35:35 -0700 Subject: [PATCH] work in progress --- win32/dll/Cargo.toml | 22 ++++++++++++++++++++ win32/dll/advapi32.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ win32/dll/build.rs | 20 ++++++++++++++++++ win32/dll/lib.rs | 1 + 4 files changed, 91 insertions(+) create mode 100644 win32/dll/Cargo.toml create mode 100644 win32/dll/advapi32.rs create mode 100644 win32/dll/build.rs create mode 100644 win32/dll/lib.rs diff --git a/win32/dll/Cargo.toml b/win32/dll/Cargo.toml new file mode 100644 index 00000000..7df25c92 --- /dev/null +++ b/win32/dll/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "dll" +version = "0.1.0" +build = "build.rs" +edition = "2021" + +[workspace] +resolver = "2" + +[[example]] +name = "advapi32" +crate-type = ["cdylib"] +path = "advapi32.rs" + +[lib] +path = "lib.rs" + +[profile.release] +panic = "abort" +# Disabling debug appears to do nothing on Windows :( +debug = false +strip = true diff --git a/win32/dll/advapi32.rs b/win32/dll/advapi32.rs new file mode 100644 index 00000000..3e2b4ad1 --- /dev/null +++ b/win32/dll/advapi32.rs @@ -0,0 +1,48 @@ +#![no_std] +#![no_main] + +#[panic_handler] +fn panic(_: &::core::panic::PanicInfo) -> ! { + loop {} +} + +#[link( + name = "retrowin32", + kind = "raw-dylib", + import_name_type = "undecorated" +)] +extern "stdcall" { + fn retrowin32_syscall(); +} + +#[no_mangle] +pub unsafe extern "stdcall" fn RegCloseKey(_: u32) { + retrowin32_syscall(); +} + +#[no_mangle] +pub unsafe extern "stdcall" fn RegOpenKey(_: u32) { + retrowin32_syscall(); +} + +// Have to wrap the pointers in a struct to impl Sync. +// Have to impl Sync to use in a static. +// The other option is a "static mut" but that creates a .data section we don't otherwise need. +#[repr(transparent)] +pub struct VTableEntry(*const fn()); +unsafe impl Sync for VTableEntry {} + +#[no_mangle] +pub static vtab: [VTableEntry; 4] = [ + VTableEntry(core::ptr::null()), + VTableEntry(RegCloseKey as _), + VTableEntry(core::ptr::null()), + VTableEntry(RegOpenKey as _), +]; + +// core::arch::global_asm!( +// ".globl _IDirectSound", +// "_IDirectSound:", +// ".long _RegCloseKey", +// ".long 0", +// ); diff --git a/win32/dll/build.rs b/win32/dll/build.rs new file mode 100644 index 00000000..c6f8543a --- /dev/null +++ b/win32/dll/build.rs @@ -0,0 +1,20 @@ +#[cfg(target_family = "unix")] +fn main() { + println!("cargo:rerun-if-env-changed=XWIN"); + if let Ok(xwin) = std::env::var("XWIN") { + for dir in ["crt/lib/x86", "sdk/lib/ucrt/x86", "sdk/lib/um/x86"] { + println!(r"cargo:rustc-link-search={xwin}/{dir}"); + } + } + + println!("cargo::rustc-link-arg=/Brepro"); + println!("cargo::rustc-link-arg=/noentry"); + println!("cargo::rustc-link-arg=/nodefaultlib"); + // This doesn't help, we need -Zmerge-functions=disabled + // println!("cargo::rustc-link-arg=/OPT:NOICF"); + // This doesn't disable debug info, turns out /Brepro causes it + // println!("cargo::rustc-link-arg=/DEBUG:NONE"); +} + +#[cfg(target_family = "windows")] +fn main() {} diff --git a/win32/dll/lib.rs b/win32/dll/lib.rs new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/win32/dll/lib.rs @@ -0,0 +1 @@ +