Skip to content

#[link(kind = "static", name = "c")] segfaults on run #124043

Closed as not planned
Closed as not planned

Description

@GuillaumeGomez

While working on #123974, I had a failure when running doctests of the unstable book. Rustdoc generates this code:

#![allow(unused_extern_crates)]
#![allow(internal_features)]
#![feature(test)]
#![feature(rustc_attrs)]
#![feature(coverage_attribute)]
#![feature(link_arg_attribute)]

#![allow(unused)]
extern crate test;
mod __doctest_0 {

fn main()  {
    #[link(kind = "link-arg", name = "--start-group")]
#[link(kind = "static", name = "c")] // Causing the segfault
#[link(kind = "static", name = "gcc")]
#[link(kind = "link-arg", name = "--end-group")]
extern "C" {}
}
#[rustc_test_marker = "/home/imperio/rust/rust/src/doc/unstable-book/src/language-features/link-arg-attribute.md - The_tracking_issue_for_this_feature_is__ (line 11)"]
pub const TEST: test::TestDescAndFn = test::TestDescAndFn {
    desc: test::TestDesc {
        name: test::StaticTestName("/home/imperio/rust/rust/src/doc/unstable-book/src/language-features/link-arg-attribute.md - The_tracking_issue_for_this_feature_is__ (line 11)"),
        ignore: false,
        ignore_message: None,
        source_file: "_home_imperio_rust_rust_src_doc_unstable_book_src_language_features_link_arg_attribute_md",
        start_line: 11,
        start_col: 0,
        end_line: 0,
        end_col: 0,
        compile_fail: false,
        no_run: true,
        should_panic: test::ShouldPanic::No,
        test_type: test::TestType::UnitTest,
    },
    testfn: test::StaticTestFn(
        #[coverage(off)]
        || test::assert_test_result(Ok::<(), String>(())),
    )
};
}

#[rustc_main]
#[coverage(off)]
fn main() {
    test::test_main(&["rustdoctest".to_string(),], vec![__doctest_0::TEST], None);
}

If you compile and run this program (on linux x86_64), it'll segfault. Commenting #[link(kind = "static", name = "c")] will make it run without problem.

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 16, 2024
  2. Noratrieb commented on Apr 17, 2024

    @Noratrieb
    Member

    I'd just like to interject for a moment. What you're refering to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.
    No but if you're on GNU/Linux then I do expect that to end badly, because glibc is not supposed to be linked statically.
    I also don't know how rustc deals with having several linkages to "c" (as the standard library also links to it) but probably not very well either.
    it would be interesting to know where it segfaults, but this certainly isn't supposed to work :D.

  3. added
    A-linkageArea: linking into static, shared libraries and binaries
    O-linuxOperating system: Linux
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 17, 2024
  4. bjorn3 commented on Apr 17, 2024

    @bjorn3
    Member

    You have to use -Ctarget-feature=+crt-static to statically link libc. Otherwise you will still be producing a dynamically linked executable.

  5. bjorn3 commented on Oct 8, 2026

    @bjorn3
    Member

    In addition to the above statically linking glibc is poorly supported by glibc itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.O-linuxOperating system: LinuxT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions