-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
#[used] attribute #39987
Merged
Merged
#[used] attribute #39987
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4c7e277
add an #[used] attribute
bc1bd8a
add tracking issue and feature-gate and run-make tests
c759eea
fix location of the emitted object file
c1635d7
cast the #[used] static to *i8
ecddad6
don't test for the absence of BAR in the rmake test
bbe5411
document the implementation a bit more
763beff
add documentation to the unstable book
7d25e76
add link to issue number, ignore snippet that requires custom linking
f4f79c3
ignore the .init_array doctest
98037ca
don't pass -C to nm
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
# `used` | ||
|
||
The tracking issue for this feature is: 40289. | ||
|
||
------------------------ | ||
|
||
The `#[used]` attribute can be applied to `static` variables to prevent the Rust | ||
compiler from optimizing them away even if they appear to be unused by the crate | ||
(appear to be "dead code"). | ||
|
||
``` rust | ||
#![feature(used)] | ||
|
||
#[used] | ||
static FOO: i32 = 1; | ||
|
||
static BAR: i32 = 2; | ||
|
||
fn main() {} | ||
``` | ||
|
||
If you compile this program into an object file, you'll see that `FOO` makes it | ||
to the object file but `BAR` doesn't. Neither static variable is used by the | ||
program. | ||
|
||
``` text | ||
$ rustc -C opt-level=3 --emit=obj used.rs | ||
|
||
$ nm -C used.o | ||
0000000000000000 T main | ||
U std::rt::lang_start | ||
0000000000000000 r used::FOO | ||
0000000000000000 t used::main | ||
``` | ||
|
||
Note that the *linker* knows nothing about the `#[used]` attribute and will | ||
remove `#[used]` symbols if they are not referenced by other parts of the | ||
program: | ||
|
||
``` text | ||
$ rustc -C opt-level=3 used.rs | ||
|
||
$ nm -C used | grep FOO | ||
``` | ||
|
||
"This doesn't sound too useful then!" you may think but keep reading. | ||
|
||
To preserve the symbols all the way to the final binary, you'll need the | ||
cooperation of the linker. Here's one example: | ||
|
||
The ELF standard defines two special sections, `.init_array` and | ||
`.pre_init_array`, that may contain function pointers which will be executed | ||
*before* the `main` function is invoked. The linker will preserve symbols placed | ||
in these sections (at least when linking programs that target the `*-*-linux-*` | ||
targets). | ||
|
||
``` rust | ||
#![feature(used)] | ||
|
||
extern "C" fn before_main() { | ||
println!("Hello, world!"); | ||
} | ||
|
||
#[link_section = ".init_array"] | ||
#[used] | ||
static INIT_ARRAY: [extern "C" fn(); 1] = [before_main]; | ||
|
||
fn main() {} | ||
``` | ||
|
||
So, `#[used]` and `#[link_section]` can be combined to obtain "life before | ||
main". | ||
|
||
``` text | ||
$ rustc -C opt-level=3 before-main.rs | ||
|
||
$ ./before-main | ||
Hello, world! | ||
``` | ||
|
||
Another example: ARM Cortex-M microcontrollers need their reset handler, a | ||
pointer to the function that will executed right after the microcontroller is | ||
turned on, to be placed near the start of their FLASH memory to boot properly. | ||
|
||
This condition can be met using `#[used]` and `#[link_section]` plus a linker | ||
script. | ||
|
||
``` rust | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you want an
|
||
#![feature(lang_items)] | ||
#![feature(used)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
extern "C" fn reset_handler() -> ! { | ||
loop {} | ||
} | ||
|
||
#[link_section = ".reset_handler"] | ||
#[used] | ||
static RESET_HANDLER: extern "C" fn() -> ! = reset_handler; | ||
|
||
#[lang = "panic_fmt"] | ||
fn panic_fmt() {} | ||
``` | ||
|
||
``` text | ||
MEMORY | ||
{ | ||
FLASH : ORIGIN = 0x08000000, LENGTH = 128K | ||
RAM : ORIGIN = 0x20000000, LENGTH = 20K | ||
} | ||
|
||
SECTIONS | ||
{ | ||
.text ORIGIN(FLASH) : | ||
{ | ||
/* Vector table */ | ||
LONG(ORIGIN(RAM) + LENGTH(RAM)); /* initial SP value */ | ||
KEEP(*(.reset_handler)); | ||
|
||
/* Omitted: The rest of the vector table */ | ||
|
||
*(.text.*); | ||
} > FLASH | ||
|
||
/DISCARD/ : | ||
{ | ||
/* Unused unwinding stuff */ | ||
*(.ARM.exidx.*) | ||
} | ||
} | ||
``` | ||
|
||
``` text | ||
$ xargo rustc --target thumbv7m-none-eabi --release -- \ | ||
-C link-arg=-Tlink.x -C link-arg=-nostartfiles | ||
|
||
$ arm-none-eabi-objdump -Cd target/thumbv7m-none-eabi/release/app | ||
./target/thumbv7m-none-eabi/release/app: file format elf32-littlearm | ||
|
||
|
||
Disassembly of section .text: | ||
|
||
08000000 <app::RESET_HANDLER-0x4>: | ||
8000000: 20005000 .word 0x20005000 | ||
|
||
08000004 <app::RESET_HANDLER>: | ||
8000004: 08000009 .... | ||
|
||
08000008 <app::reset_handler>: | ||
8000008: e7fe b.n 8000008 <app::reset_handler> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#[used] | ||
fn foo() {} | ||
//~^^ ERROR the `#[used]` attribute is an experimental feature | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
-include ../tools.mk | ||
|
||
ifdef IS_WINDOWS | ||
# Do nothing on MSVC. | ||
all: | ||
exit 0 | ||
else | ||
all: | ||
$(RUSTC) -C opt-level=3 --emit=obj used.rs | ||
nm -C $(TMPDIR)/used.o | grep FOO | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![crate_type = "lib"] | ||
#![feature(used)] | ||
|
||
#[used] | ||
static FOO: u32 = 0; | ||
|
||
static BAR: u32 = 0; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: it would be nice is this was a clickable link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in
[40289](https://github.com/rust-lang/rust/issues/40289)