From c982300b7dd050b44ce564c788e557dc1cc1e26e Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Wed, 12 Feb 2025 15:11:54 -0700 Subject: [PATCH] . --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 6 +++++- README.md | 16 +++++++++++++++- tests-static-main/Cargo.toml | 14 ++++++++++++++ tests-static-main/README.md | 16 ++++++++++++++++ tests-static-main/src/main.rs | 5 +++++ tests-static/Cargo.toml | 14 ++++++++++++++ tests-static/src/lib.rs | 6 ++++++ tests-wasm/src/lib.rs | 2 +- tests/Cargo.toml | 2 +- 10 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 tests-static-main/Cargo.toml create mode 100644 tests-static-main/README.md create mode 100644 tests-static-main/src/main.rs create mode 100644 tests-static/Cargo.toml create mode 100644 tests-static/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9dea49e..cda46c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,6 +165,23 @@ dependencies = [ "libc-print", ] +[[package]] +name = "tests-static" +version = "0.1.0" +dependencies = [ + "ctor", + "libc-print", +] + +[[package]] +name = "tests-static-main" +version = "0.1.0" +dependencies = [ + "ctor", + "libc-print", + "tests-static", +] + [[package]] name = "tests-wasm" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index d1622e6..6c95924 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,7 @@ [workspace] -members = [ "ctor", "codegen", "dtor", "tests", "tests-wasm" ] +members = [ "ctor", "codegen", "dtor", "tests", "tests-wasm", "tests-static", "tests-static-main" ] resolver = "2" + + +[profile.dev] +codegen-units = 1 \ No newline at end of file diff --git a/README.md b/README.md index 8776091..4ecaf80 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ [![docs.rs](https://docs.rs/ctor/badge.svg)](https://docs.rs/ctor) [![crates.io](https://img.shields.io/crates/v/ctor.svg)](https://crates.io/crates/ctor) -Module initialization/teardown functions for Rust (like `__attribute__((constructor))` in C/C++) for Linux, OSX, FreeBSD, NetBSD, Illumos, OpenBSD, DragonFlyBSD, Android, iOS, and Windows. +Module initialization/teardown functions for Rust (like +`__attribute__((constructor))` in C/C++) for Linux, OSX, FreeBSD, NetBSD, +Illumos, OpenBSD, DragonFlyBSD, Android, iOS, WASM, and Windows. This library currently requires **Rust > 1.31.0** at a minimum for the procedural macro support. @@ -25,6 +27,18 @@ startup/shutdown respectively. This library supports WASM targets, but note that only a single `#[ctor]` function is supported due to platform limitations. +## Linking + +The `#[ctor]` and `#[dtor]` macros make use of linker sections to ensure that a +function is run at startup time. + +Because of various platform linking issues, you may need to ensure that you +reference a symbol in each module that provides a `#[ctor]` or `#[dtor]`. If the +functions do not run as expected, `use other_crate` may be sufficient to fix +things. + +See https://github.com/rust-lang/rust/issues/133491 for more information. + ## Warnings Rust's philosophy is that nothing happens before or after main and diff --git a/tests-static-main/Cargo.toml b/tests-static-main/Cargo.toml new file mode 100644 index 0000000..9e88b3e --- /dev/null +++ b/tests-static-main/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "tests-static-main" +version = "0.1.0" +edition = "2021" +publish = false + +[features] +import = [] +used_linker = ["ctor/used_linker", "tests-static/used_linker"] + +[dependencies] +ctor = { path = "../ctor" } +libc-print = "0.1.15" +tests-static = { path = "../tests-static" } diff --git a/tests-static-main/README.md b/tests-static-main/README.md new file mode 100644 index 0000000..52e5f3e --- /dev/null +++ b/tests-static-main/README.md @@ -0,0 +1,16 @@ +# Static linking example + +Example showing how static linking may be complicated. Without the `import` feature, +the `#[ctor]` function will not be run. + +``` +cargo run -p tests-static-main --features=import +``` + +On MacOS, you may need to link with `rust-lld`: + +``` +RUSTFLAGS="-Clinker=rust-lld" cargo run -p tests-static-main --features=import +``` + +See https://github.com/rust-lang/rust/issues/133491 for more information. diff --git a/tests-static-main/src/main.rs b/tests-static-main/src/main.rs new file mode 100644 index 0000000..3eaf6be --- /dev/null +++ b/tests-static-main/src/main.rs @@ -0,0 +1,5 @@ +#[allow(unused)] +#[cfg(feature = "import")] +use tests_static; + +fn main() {} diff --git a/tests-static/Cargo.toml b/tests-static/Cargo.toml new file mode 100644 index 0000000..0031fe0 --- /dev/null +++ b/tests-static/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "tests-static" +version = "0.1.0" +edition = "2021" +publish = false + +[features] +used_linker = ["ctor/used_linker"] + +[lib] + +[dependencies] +ctor = { path = "../ctor" } +libc-print = "0.1.15" diff --git a/tests-static/src/lib.rs b/tests-static/src/lib.rs new file mode 100644 index 0000000..915e94d --- /dev/null +++ b/tests-static/src/lib.rs @@ -0,0 +1,6 @@ +#![cfg_attr(feature = "used_linker", feature(used_with_arg))] + +#[ctor::ctor] +pub fn foo() { + libc_print::libc_println!("foo"); +} diff --git a/tests-wasm/src/lib.rs b/tests-wasm/src/lib.rs index 0d7cd9f..a22bf48 100644 --- a/tests-wasm/src/lib.rs +++ b/tests-wasm/src/lib.rs @@ -1,4 +1,4 @@ -#[cfg(target_family="wasm")] +#[cfg(target_family = "wasm")] #[ctor::ctor] unsafe fn hello() { use wasm_bindgen::prelude::wasm_bindgen; diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 000ce27..1763c12 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -21,4 +21,4 @@ crate-type = ["cdylib"] [[example]] name = "dylib_load" -path = "src/dylib_load.rs" \ No newline at end of file +path = "src/dylib_load.rs"