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

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

Closed
Ecco opened this issue Feb 10, 2016 · 4 comments
Closed

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

Ecco opened this issue Feb 10, 2016 · 4 comments
Labels
A-codegen Area: Code generation

Comments

@Ecco
Copy link

Ecco commented Feb 10, 2016

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.

@japaric
Copy link
Member

japaric commented Feb 10, 2016

rust-lang/rfcs#1459 proposes a #[used]/#[preserve] that would force the compiler to preserve the symbol even when it's unused.

@alexcrichton
Copy link
Member

The relevant logic for this I believe is here and may just be a bit too eager. There's a few other locations that we check for "extern fn or #[no_mangle] static", and we may just need to add it here as well.

It would probably be good to sort this out more thoroughly though as it seems like this keeps coming up!

@alexcrichton alexcrichton added the A-codegen Area: Code generation label Feb 11, 2016
@JinShil
Copy link

JinShil commented Oct 23, 2016

It would probably be good to sort this out more thoroughly though as it seems like this keeps coming up!

Yep, I just ran into this too. The only workaround I could find was to add a #[linkage = "external"] attribute.
See also rust-lang/rfcs#1002

@Mark-Simulacrum
Copy link
Member

Without optimizations:

$ nm test.0.o
0000000000000000 R FOO
0000000000000000 T main
                 U _ZN3std2rt10lang_start17he6ada39161c356c2E
0000000000000000 t _ZN4test4main17hf4d700377696cbb0E

With optimizations:

$ nm test.0.o
0000000000000000 R FOO
0000000000000000 T main
                 U _ZN3std2rt10lang_start17he6ada39161c356c2E
0000000000000000 t _ZN4test4main17hf4d700377696cbb0E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation
Projects
None yet
Development

No branches or pull requests

5 participants