Skip to content
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

Add used_linker feature #281

Merged
merged 1 commit into from
May 30, 2023
Merged

Add used_linker feature #281

merged 1 commit into from
May 30, 2023

Conversation

mmastrac
Copy link
Owner

@mmastrac mmastrac commented May 30, 2023

Add a feature used_linker that uses #[used(linker)], an unstable +nightly feature that might fix some of the ctor pain.

This may fix #280, #206 and related bugs.

@mmastrac mmastrac merged commit 5be16f5 into master May 30, 2023
@stepancheg
Copy link

stepancheg commented Jun 1, 2023

The issue still reproduces with this example unfortunately: #220

(There's something odd that feature is not enabled for proc macro dependency even if selected explicitly, but I patched ctor crate to include is unconditionally, and the problem still exists.)

In that repo:

cargo expand -p foo bar
mod bar {
    use crate::COUNTER;
    use std::sync::atomic::{Ordering, Ordering::SeqCst};
    use ctor::ctor;
    extern fn init() {
        COUNTER.store(COUNTER.load(SeqCst) + 1, Ordering::SeqCst)
    }
    #[used(linker)]
    #[allow(non_upper_case_globals)]
    #[doc(hidden)]
    #[link_section = "__DATA,__mod_init_func"]
    static init___rust_ctor___ctor: unsafe extern "C" fn() = {
        unsafe extern "C" fn init___rust_ctor___ctor() {
            init()
        }
        init___rust_ctor___ctor
    };
}

and yet

cargo +nightly run
The value of COUNTER is 2.
Why not 3?

macOS 13.4, Apple M1, rustc 1.72.0-nightly (871b59520 2023-05-31)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ctor not running for statically linked C library
2 participants