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

LTO preserves too many extern symbols #34985

Closed
alexcrichton opened this issue Jul 23, 2016 · 5 comments
Closed

LTO preserves too many extern symbols #34985

alexcrichton opened this issue Jul 23, 2016 · 5 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@alexcrichton
Copy link
Member

Right now the standard library ends up exporting a bunch of extern symbols from liblibc:

$ echo '#[no_mangle] pub extern fn foo() { panic!() }' > foo.rs
$ echo 'extern void foo(); int main() { foo(); return 0; }' > foo.c
$ rustc foo.rs --crate-type staticlib -C lto
$ gcc foo.c libfoo.a
$ nm -g ./a.out | grep libc
000000000049e310 T _ZN4libc4unix6notbsd11WEXITSTATUS17hdf8e9951afe1c101E
000000000049e530 T _ZN4libc4unix6notbsd5linux7CPU_SET17hb5ba54c4f04e4ad5E
000000000049e510 T _ZN4libc4unix6notbsd5linux8CPU_ZERO17hc3389841ca5535cfE
000000000049e630 T _ZN4libc4unix6notbsd5linux9CPU_EQUAL17h6aa5067ba806ec07E
000000000049e5b0 T _ZN4libc4unix6notbsd5linux9CPU_ISSET17h25c9bc3ffd366857E
000000000049e170 T _ZN4libc4unix6notbsd6FD_CLR17h82439e27c061361cE
000000000049e250 T _ZN4libc4unix6notbsd6FD_SET17h6f0001978b943490E
000000000049e2c0 T _ZN4libc4unix6notbsd7FD_ZERO17h157d16767431163aE
000000000049e1e0 T _ZN4libc4unix6notbsd8FD_ISSET17h9bb2d4903026e75fE
000000000049e320 T _ZN4libc4unix6notbsd8WTERMSIG17h140bc8346daf707fE
000000000049e2f0 T _ZN4libc4unix6notbsd9WIFEXITED17h9b79d0f36c3cdb42E

These are all defined in the libc crate, but LTO should be stripping them out.

I believe the reason that this is happening is we attempt to preserve extern symbols from upstream crates (as they may be part of the ABI), but we don't check for #[no_mangle] (which none of these functions have).

I think the relevant clause is here which ends up calling this function coming over to this function. Although there's a clause which looks like it checks for this, I think we're falling through too quickly and we may wish to change that.

Note that this is mostly just a hunch though, some more investigation may be required! For an aspiring trans hacker though I don't think this should be too hard and I'd be willing to help out!

@alexcrichton
Copy link
Member Author

@alexcrichton alexcrichton added A-linkage Area: linking into static, shared libraries and binaries E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Jul 23, 2016
@wesleywiser
Copy link
Member

I'd like to give this a shot if that's ok. What's the best way to test a fix? Running the above test case?

@alexcrichton
Copy link
Member Author

@wesleywiser sure! I'm not sure there's a great test case we can check in for this, but to manually test it yeah the commands in the description should be good enough.

@wesleywiser
Copy link
Member

Ok cool! I believe your hunch was correct. The code was balling out too soon if the function's ABI wasn't Rust. Removing the early return and running the above commands no longer shows those symbols. Is there any other tests I should run before opening a PR? (The check-stage2 target passes on OS X.)

@alexcrichton
Copy link
Member Author

Awesome! I'd run a full make check just to be sure, but if that passes it's probably good to go

wesleywiser added a commit to wesleywiser/rust that referenced this issue Jul 27, 2016
Previously, all extern symbols were exported even when performing LTO.
Now, we only export symbols that are also marked #[no_mangle].

Fixes rust-lang#34985
bors added a commit that referenced this issue Jul 30, 2016
Only export #[no_mangle] extern symbols during LTO

Fixes #34985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

No branches or pull requests

2 participants