Skip to content

Symbols are removed from object file when optimizations are enabled #31531

@Ecco

Description

@Ecco

Consider this very simple sample code, referred to as test.rs hereinafter:

fn main() {
}

#[no_mangle]
pub static FOO: i32 = 1;

When compiling it without optimizations, we get the following result:

$ rustc test.rs -C linker=/bin/false
$ nm test.0.o
0000000000000040 s _FOO
                 U __ZN2rt10lang_start20hacfbaccc148e05fdU4xE
0000000000000000 t __ZN4main20h82df1d8932225dfbeaaE
0000000000000010 T _main

If we enable optimizations (like cargo build does) we get this:

$ rustc test.rs -C opt-level=3 -C linker=/bin/false
$ nm test.0.o
                 U __ZN2rt10lang_start20hacfbaccc148e05fdU4xE
0000000000000000 t __ZN4main20h82df1d8932225dfbeaaE
0000000000000010 T _main

My understanding is that it is not the compiler's job to trim this (apparently) unused symbol from the object file, but the linker's. Indeed, one could imagine that the FOO symbol might be expected by another object file that would be linked into the final executable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions