|
1 | 1 | # Application Binary Interface (ABI)
|
2 | 2 |
|
3 |
| -This section documents (or will document) features that affect the ABI of a Rust program / binary, |
4 |
| -rlib, dylib, etc. A (likely incomplete) list of such features is shown below: |
| 3 | +This section documents features that affect the ABI of a Rust program / binary, rlib, dylib, etc. A |
| 4 | +list of such features is shown below: |
5 | 5 |
|
6 |
| -- #[used] |
7 |
| -- #[no_mangle] |
8 |
| -- #[link_section] |
9 |
| -- extern "$ABI" fn |
| 6 | +- `#[export_name]` |
| 7 | +- `#[link_section]` |
| 8 | +- `#[no_mangle]` |
| 9 | +- `#[used]` |
| 10 | +- `extern "$ABI" fn` |
| 11 | + |
| 12 | +## `#[used]` |
| 13 | + |
| 14 | +The `#[used]` attribute can only be applied to `static` variables. This attribute forces the |
| 15 | +compiler to keep the variable in the output object file (.o, .rlib, etc.) even if the variable is |
| 16 | +not used, or referenced, by any other item in the crate. |
| 17 | + |
| 18 | +Below is an example that shows under what conditions the compiler keeps a `static` variable in the |
| 19 | +output object file. |
| 20 | + |
| 21 | +``` rust |
| 22 | +// foo.rs |
| 23 | + |
| 24 | +#![feature(used)] |
| 25 | + |
| 26 | +// kept because of #[used] |
| 27 | +#[used] |
| 28 | +static FOO: u32 = 0; |
| 29 | + |
| 30 | +// removed because it's unused |
| 31 | +#[allow(dead_code)] |
| 32 | +static BAR: u32 = 0; |
| 33 | + |
| 34 | +// kept because it's referenced by a public, reachable function |
| 35 | +pub static BAZ: u32 = 0; |
| 36 | + |
| 37 | +pub static QUUX: u32 = 0; |
| 38 | + |
| 39 | +pub fn quux() -> &'static u32 { |
| 40 | + &QUUX |
| 41 | +} |
| 42 | + |
| 43 | +// removed because it's referenced by a private, unused (dead) function |
| 44 | +static CORGE: u32 = 0; |
| 45 | + |
| 46 | +#[allow(dead_code)] |
| 47 | +fn corge() -> &'static u32 { |
| 48 | + &CORGE |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +``` console |
| 53 | +$ rustc -O --emit=obj --crate-type=rlib foo.rs |
| 54 | + |
| 55 | +$ nm -C foo.o |
| 56 | +0000000000000000 R foo::BAZ |
| 57 | +0000000000000000 r foo::FOO |
| 58 | +0000000000000000 R foo::QUUX |
| 59 | +0000000000000000 T foo::quux |
| 60 | +``` |
0 commit comments