-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Control visibility of dylib symbols #18541
Comments
dynamic linking is evil anyways |
I'm writing to an existing C API so I have no choice but to use it. |
You're linking against a dylib, or you're producing one for consumption by other C libraries? If possible, just have all your consumers statically link against your library. In #14344, the linker unintentionally optimizes away unused symbols. It's interesting that here, it's not removing any. |
I'm producing one for consumption by C code, and it's not possible for me to have all consumers statically link. |
As an example of the problem, consider this code: #![no_std]
#![feature(lang_items, globs)]
#![crate_type = "dylib"]
extern crate core;
extern crate collections;
use core::prelude::*;
use collections::string::String;
#[no_mangle]
pub extern "C" fn get_len() -> uint {
let mut s = String::from_str("Test");
s.push('_'); // Do enough work to prevent LLVM optimizing it out
s.len()
}
#[lang = "stack_exhausted"] extern fn stack_exhausted() { }
#[lang = "eh_personality"] extern fn eh_personality() { }
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop { } } Compiling with
Compiling with
GCC supports |
The correct solution is to enable dylib LTO, so closing in favor of #18863. |
…eric-args feat: Complete diagnostics in ty lowering groundwork and serve a first diagnostic 🎉
Compiling a dylib on Linux, I expected only extern fns in the root crate to be visible. Instead every public symbol in every crate I used was visible, and no dead code was eliminated, resulting in a very bloated dylib.
I was able to fix this by creating a linker version script with only the intended public symbols in the global section and passing linker arguments:
-C link-args="-Wl,-version-script=version.script -Wl,-gc-sections"
This works but it requires extra manual effort maintaining the version script and it is not cross-platform. It would be nice if Rust supported this natively.
The text was updated successfully, but these errors were encountered: