summary refs log tree commit diff stats
path: root/rust/util/src/module.rs
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2025-09-08 12:49:50 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2025-09-17 19:00:57 +0200
commit59869b4d58854190f09a79c5392d60fdc0b55d45 (patch)
tree8cccf2a861742b83b6018d29e9ad1dcf48bfd985 /rust/util/src/module.rs
parent1e70d83de4d502759d6e68fbac301c1243088385 (diff)
downloadfocaccia-qemu-59869b4d58854190f09a79c5392d60fdc0b55d45.tar.gz
focaccia-qemu-59869b4d58854190f09a79c5392d60fdc0b55d45.zip
rust: split "util" crate
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20250827104147.717203-7-marcandre.lureau@redhat.com
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'rust/util/src/module.rs')
-rw-r--r--rust/util/src/module.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/rust/util/src/module.rs b/rust/util/src/module.rs
new file mode 100644
index 0000000000..06c45fc142
--- /dev/null
+++ b/rust/util/src/module.rs
@@ -0,0 +1,43 @@
+// Copyright 2024, Linaro Limited
+// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+//! Macro to register blocks of code that run as QEMU starts up.
+
+#[macro_export]
+macro_rules! module_init {
+    ($type:ident => $body:block) => {
+        const _: () = {
+            #[used]
+            #[cfg_attr(
+                not(any(target_vendor = "apple", target_os = "windows")),
+                link_section = ".init_array"
+            )]
+            #[cfg_attr(target_vendor = "apple", link_section = "__DATA,__mod_init_func")]
+            #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
+            pub static LOAD_MODULE: extern "C" fn() = {
+                extern "C" fn init_fn() {
+                    $body
+                }
+
+                extern "C" fn ctor_fn() {
+                    unsafe {
+                        $crate::bindings::register_module_init(
+                            Some(init_fn),
+                            $crate::bindings::module_init_type::$type,
+                        );
+                    }
+                }
+
+                ctor_fn
+            };
+        };
+    };
+
+    // shortcut because it's quite common that $body needs unsafe {}
+    ($type:ident => unsafe $body:block) => {
+        ::util::module_init! {
+            $type => { unsafe { $body } }
+        }
+    };
+}