Skip to content

Test for static linking failure #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests-static-main/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
16 changes: 16 additions & 0 deletions tests-static-main/README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions tests-static-main/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[allow(unused)]
#[cfg(feature = "import")]
use tests_static;

fn main() {}
14 changes: 14 additions & 0 deletions tests-static/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions tests-static/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg_attr(feature = "used_linker", feature(used_with_arg))]

#[ctor::ctor]
pub fn foo() {
libc_print::libc_println!("foo");
}
2 changes: 1 addition & 1 deletion tests-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(target_family="wasm")]
#[cfg(target_family = "wasm")]
#[ctor::ctor]
unsafe fn hello() {
use wasm_bindgen::prelude::wasm_bindgen;
Expand Down
2 changes: 1 addition & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ crate-type = ["cdylib"]

[[example]]
name = "dylib_load"
path = "src/dylib_load.rs"
path = "src/dylib_load.rs"