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

#[link_section] does not work in libraries on release #85045

Open
AsafFisher opened this issue May 7, 2021 · 5 comments
Open

#[link_section] does not work in libraries on release #85045

AsafFisher opened this issue May 7, 2021 · 5 comments
Labels
A-codegen Area: Code generation A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@AsafFisher
Copy link

AsafFisher commented May 7, 2021

I tried this code while compiling for release:
bin.rs

use elfredo::extended_data;


fn main() {
    print!("{}", do_stuff_with_extended_data());
}

lib.rs

#[link_section = ".extended"]
#[used]
#[no_mangle]
pub static extended_data: u8 = 3;

fn do_stuff_with_extended_data() -> &str{
// Uses extended_data
}

I expected to see this happen (compiled with debug):

asaffisher@ubuntu:/mnt/hgfs/elfredo/target/debug$ readelf -S test_echo_patch_elf | grep extend
  [18] .extended         PROGBITS         000000000003cec8  0003cec8

Instead, this happened(compiled in release):

asaffisher@ubuntu:/mnt/hgfs/elfredo/target/release$ readelf -S test_echo_patch_elf | grep extend
asaffisher@ubuntu:/mnt/hgfs/elfredo/target/release$

Meta

rustc --version --verbose:

rustc 1.53.0-nightly (42816d61e 2021-04-24)
binary: rustc
commit-hash: 42816d61ead7e46d462df997958ccfd514f8c21c
commit-date: 2021-04-24
host: x86_64-unknown-linux-gnu
release: 1.53.0-nightly
LLVM version: 12.0.0
@AsafFisher AsafFisher added the C-bug Category: This is a bug. label May 7, 2021
@AsafFisher AsafFisher changed the title #[link_section] does not work in libraries on release #[link_section] does not work in libraries on release May 7, 2021
@nagisa
Copy link
Member

nagisa commented May 7, 2021

#[used] can only ensure that the symbols get to the linker, but not that the linker won't remove them. Can you verify that this is not the linker removing your otherwise unused sections?

@AsafFisher
Copy link
Author

AsafFisher commented May 7, 2021

The section is obviously used, see the println!
I added the #[used]just for the memes...
Also this behavior does not happen on debug.

@nagisa
Copy link
Member

nagisa commented May 7, 2021

It is used at a source level, but there's nothing preventing optimizations from inlining the value of the (immutable) static into the printing routines. At which point the static can become otherwise unused. FWIW I'm not able to reproduce this, regardless:

# rustc lib.rs --crate-type lib -C opt-level=3 --crate-name=elfredo
# readelf -S libelfredo.rlib | grep .extended
  [ 5] .extended         PROGBITS         0000000000000000  0000077c
# rustc main.rs --crate-type bin --extern elfredo=libelfredo.rlib -Copt-level=3 --edition=2018
# readelf -S main | grep extended
  [17] .extended         PROGBITS         0000000000036cc8  00036cc8

The only recommendation I have is the same as before. Verify that it isn't the linker removing this section. Perhaps the easiest way would be to utilize a linker script with KEEP directive or an equivalent inside it.

@AsafFisher
Copy link
Author

AsafFisher commented May 7, 2021

You are right its an optimization thing. But I still think it is an unwanted behavior.
Why would one ever want to both make sure a variable is allocated at a certain section and to have such an optimization... I think that when you use #[link_section = ".extended"] This behavior is unwanted.

Note that I changed the example

@fmease fmease added A-linkage Area: linking into static, shared libraries and binaries A-codegen Area: Code generation C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage-legacy labels Jan 25, 2024
@bjorn3
Copy link
Member

bjorn3 commented Jan 25, 2024

Since #91504 on nightly we now have #[used(linker)] behind the used_with_arg feature. #[used] behaves identical to #[used(compiler)]. Note that the linker actually needs to have support for this or it may be discarded anyway. At https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable LLVM does however suggest that #[used(compiler)] should not be exposed to source languages, unlike what rust does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants