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

Control visibility of dylib symbols #18541

Closed
mprobinson opened this issue Nov 2, 2014 · 6 comments
Closed

Control visibility of dylib symbols #18541

mprobinson opened this issue Nov 2, 2014 · 6 comments

Comments

@mprobinson
Copy link
Contributor

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.

@alexchandel
Copy link

dynamic linking is evil anyways

@mprobinson
Copy link
Contributor Author

I'm writing to an existing C API so I have no choice but to use it.

@alexchandel
Copy link

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.

@mprobinson
Copy link
Contributor Author

I'm producing one for consumption by C code, and it's not possible for me to have all consumers statically link.

@mprobinson
Copy link
Contributor Author

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 rustc --opt-level 3 lib.rs:

$ ls -hs liblib.so
1.5M liblib.so

Compiling with rustc --opt-level 3 -C link-args="-Wl,-version-script=version.script -Wl,-gc-sections" lib.rs, and version.script containing only get_len in the global section:

$ ls -hs liblib.so 
328K liblib.so

GCC supports -fvisibility=hidden and __attribute__ ((visibility ("default"))) for this, could Rust support something similar?

@mprobinson
Copy link
Contributor Author

The correct solution is to enable dylib LTO, so closing in favor of #18863.

lnicola pushed a commit to lnicola/rust that referenced this issue Dec 11, 2024
…eric-args

feat: Complete diagnostics in ty lowering groundwork and serve a first diagnostic 🎉
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants