-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking issue for the #[used]
attribute
#40289
Comments
cc #41628 |
Is there anything besides the task of creating an RFC that blocks stabilization of this feature? This feature is a cornerstone in a library I wrote and use, and I'd love to be able to use the library on a stable toolchain. |
I have opened an RFC to stabilize this: rust-lang/rfcs#2386 |
There is still an open bug where an |
Needs to be done before stabilizing this: |
check that #[used] is used only on statics this attribute has no effect on other items. This makes the implementation match what's described in the RFC. cc rust-lang#40289 r? @nagisa
That's not a problem with #![feature(used)]
mod foo {
// private and non-reachable
#[no_mangle]
#[used]
static STATIC: [u32; 10] = [1; 10];
} $ cargo build
$ nm -C target/debug/libfoo.rlib
foo-d351a64747b10042.18cd33xhzunvqw3h.rcgu.o:
0000000000000000 r STATIC
$ cargo build --release
$ nm -C target/release/libfoo.rlib
foo-f1e69902e2c50980.foo0-f8ffab837addfc521cde53270051d9b7.rs.rcgu.o:
0000000000000000 r STATIC You are not getting the behavior you want for other reasons, mainly due to link-time visibility: you want Also To make your example work I would rewrite it like this: // foo/src/lib.rs
#![feature(used)]
#![no_std] // not required but I'm using thumbv7m-none-eabi as the example target
pub mod foo {
// `STATIC` needs to be an *exported* symbol; they only way to do that right now is to make the
// item *both* public and reachable
#[doc(hidden)] // don't show this in the API docs
#[export_name = "STATIC"] // or you could use `no_mangle` and rename the item, same thing
#[used] // required or the compiler will drop this symbol when optimizations are enabled
pub static __HIDDEN: [u32; 10] = [1; 10];
} // src/main.rs
#![feature(lang_items)]
#![no_std]
#![no_main]
extern crate foo;
#[lang = "panic_fmt"]
fn panic_fmt() {}
$ cargo rustc --target thumbv7m-none-eabi -- -C linker=arm-none-eabi-ld -Z linker-flavor=ld -C link-arg=-Tlink.x
$ # note exported symbol: "R"
$ arm-none-eabi-nm -C target/thumbv7m-none-eabi/debug/deps/libfoo-739fb2098c2fa823.rlib
foo-739fb2098c2fa823.18cd33xhzunvqw3h.rcgu.o:
00000000 N
00000041 N
00000050 N
00000066 N
0000006a N
00000073 N
00000077 N
00000080 N
00000000 R STATIC
$ arm-none-eabi-objdump -Cd -j.static target/thumbv7m-none-eabi/debug/bar
target/thumbv7m-none-eabi/debug/bar: file format elf32-littlearm
Disassembly of section .static:
00000000 <STATIC>:
0: 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 ................
10: 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 ................
20: 01 00 00 00 01 00 00 00 ........ I think Visibility is important too: if |
@japaric Thanks so much for the detailed explanation! The behavior makes sense to me now and I agree that this has nothing to do with the |
This is required because otherwise the linker finishes as soon as all unresolved symbols are found. If it stumbles upon one of them by accident (because it looked at the file to find other symbols), the symbol is kept. If not, it is discarded. The `EXTERN` declaration tells the linker that it should look for the passed symbol. This also explains why our workaround worked: If the EXCEPTIONS static is alone in a separate module, it wasn't found because the linker didn't look at the module file to find other symbols. For more details, see rust-lang/rust#40289 (comment)
The stabilization RFC is now merged. Anyone writing up the stabilization PR? |
|
@rfcbot fcp merge I propose that we stabilize this feature. The #[used] attribute, which can only be applied to static variables, forces the compiler to keep the variable in the output object file (.o, .rlib, etc.) even if the variable is not used, or referenced, by any other item in the crate. See the latest RFC here. |
Trying again w/ a lang tag... @rfcbot fcp merge |
Team member @cramertj has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
stabilize #[used] closes #40289 RFC for stabilization: rust-lang/rfcs#2386 r? @Centril Where should this be documented? Currently the documentation is in the unstable book
Note: See https://reviews.llvm.org/D97448 for the See: |
Argh, why did GCC do that? Apparently there's still some active debate about it: https://gcc.gnu.org/pipermail/gcc-patches/2021-February/565418.html I can sort of understand why one might want |
Added in PR #39987.
RFC for stabilization: rust-lang/rfcs#2386 (merged)
This is an experimental feature and requires an RFC before it goes through the stabilization process.
Usage
Preserves
static
variables up to the object file level. This feature doesn't affect link time garbage collections of unused symbols / sections.The text was updated successfully, but these errors were encountered: