Skip to content

no_mangle/used static is only present in output when in reachable module #47384

Closed
@phil-opp

Description

@phil-opp

Statics that are marked no_mangle and/or used only get to the linker if they are in a reachable module of a reachable crate. The static itself does not need to be used in code, only some function in the same module. This previously worked in our project, so this seems like a regression.

Example

The issue is best explained with a small example crate named bug_test, which can be found here:

src/lib.rs:

pub mod foo {
    #[no_mangle]
    #[used]
    pub static STATIC: [u32; 10] = [1; 10];

    pub fn hello() {}
}

pub fn bar() {
    foo::hello(); // STATIC not present if commented out
}

src/main.rs:

extern crate bug_test;

fn main() {
    bug_test::bar(); // STATIC not present if commented out 
}

Linker script linker.ld:

SECTIONS
{
    .static : ALIGN(4)
    {
        KEEP(*(.rodata.STATIC));
    }
}

Build using:

RUSTFLAGS='-Z pre-link-args=-Tlinker.ld' cargo build

Show contents of .static section:

> objdump -s -j".static" target/debug/bug_test

target/debug/bug_test:     file format elf64-x86-64

Contents of section .static:
 3354c 01000000 01000000 01000000 01000000  ................
 3355c 01000000 01000000 01000000 01000000  ................
 3356c 01000000 01000000                    ........  

Comment out one of the STATIC not present if commented out lines and recompile. Then STATIC no longer exist in the output and the .static section is empty:

> objdump -s -j".static" target/debug/bug_test

target/debug/bug_test:     file format elf64-x86-64

objdump: section '.static' mentioned in a -j option, but not found in any input file

Versions

> rustc --version
rustc 1.27.0-nightly (ac3c2288f 2018-04-18)
> cargo --version
cargo 1.26.0-nightly (008c36908 2018-04-13)
> ld --version
GNU ld (GNU Binutils for Ubuntu) 2.26.1

Edit: Added the used attribute.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-incr-compArea: Incremental compilationA-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions