-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Description
In current implementation #93798 of the #[used(linker)] work fine if the symbol placed in same module or in the child module. This has been discussed and fixed here: #47384. But if the #[used(linker)] marks symbol from another crate, this won't work.
I tried this code:
main.rs:
#![feature(used_with_arg)]
extern "C" {
#[link_name = "__start_my_cool_section"]
static START: u32;
#[link_name = "__stop_my_cool_section"]
static STOP: u32;
}
#[used(linker)]
#[link_section = "my_cool_section"]
pub static TEST: [u32; 0] = [];
fn main() {
bar::bar(); // <--- try to comment this
let a = unsafe{ &START as *const _ as usize};
let b = unsafe{ &STOP as *const _ as usize};
println!("{a} {b} b-a={}", b-a);
}Cargo.toml of the main crate:
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
bar = { path = "bar" }lib.rs of the bar crate:
#![feature(used_with_arg)]
#[used(linker)]
#[link_section = "my_cool_section"]
pub static TEST: [u32; 10] = [42; 10];
pub fn bar(){
println!("-- bar --");
}
So, as well as #47384 we may view contents of my_cool_section section:
$ objdump -s -j"my_cool_section" ./foo
./foo: file format elf64-x86-64
Contents of section my_cool_section:
41128 2a000000 2a000000 2a000000 2a000000 *...*...*...*...
41138 2a000000 2a000000 2a000000 2a000000 *...*...*...*...
41148 2a000000 2a000000 *...*... ```
Comment out bar::bar() line of the main.rs and recompile. Then bar::TEST no longer exist in the output and no my_cool_section section in the compiled foo:
$ objdump -s -j"my_cool_section" ./foo
./foo: file format elf64-x86-64
objdump: section 'my_cool_section' mentioned in a -j option, but not found in any input file
If you do not use START/STOP variables (but call bar::bar()) of the example, then there is no my_cool_section section either. Probably I understand that this is not correct too.
As far as I understand, if this is not fixed, then some crates (linkme, inventory...) will not work correctly in some cases.
Meta
$ rustc +nightly --version --verbose
rustc 1.63.0-nightly (dc80ca78b 2022-06-21)
binary: rustc
commit-hash: dc80ca78b6ec2b6bba02560470347433bcd0bb3c
commit-date: 2022-06-21
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5