fix(core): Reject non-ELF targets in mod_init! and mod_fini! macros

This commit is contained in:
Jade Ellis committed 2026-11-21 18:04:50 +00:00
1 parent 306be392ba
commit 9744a6e332
1 file changed
+30 -4
+30 -4
View File
@@ -22,11 +22,24 @@ macro_rules! mod_dtor {
#[macro_export]
macro_rules! mod_init {
($body:block) => {
#[cfg(not(all(target_family = "unix", not(target_vendor = "apple"))))]
compile_error!("conduwuit_mods (mod_init!) requires an ELF Unix target");
#[used]
#[cfg_attr(target_family = "unix", unsafe(link_section = ".init_array"))]
// SAFETY: `.init_array` is the standard ELF constructor pointer table;
// the dynamic loader invokes `_mod_init` before `main()` or on `dlopen`.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".init_array")
)]
static MOD_INIT: unsafe extern "C" fn() = { _mod_init };
#[cfg_attr(target_family = "unix", unsafe(link_section = ".text.startup"))]
// SAFETY: `.text.startup` is the standard ELF text subsection for
// initialization functions.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".text.startup")
)]
unsafe extern "C" fn _mod_init() -> () $body
};
}
@@ -34,11 +47,24 @@ macro_rules! mod_init {
#[macro_export]
macro_rules! mod_fini {
($body:block) => {
#[cfg(not(all(target_family = "unix", not(target_vendor = "apple"))))]
compile_error!("conduwuit_mods (mod_fini!) requires an ELF Unix target");
#[used]
#[cfg_attr(target_family = "unix", unsafe(link_section = ".fini_array"))]
// SAFETY: `.fini_array` is the standard ELF destructor pointer table;
// the dynamic loader invokes `_mod_fini` on `dlclose` or process teardown.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".fini_array")
)]
static MOD_FINI: unsafe extern "C" fn() = { _mod_fini };
#[cfg_attr(target_family = "unix", unsafe(link_section = ".text.startup"))]
// SAFETY: `.text.exit` / `.text.startup` is a standard ELF text
// subsection for teardown/initialization functions.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".text.startup")
)]
unsafe extern "C" fn _mod_fini() -> () $body
};
}