| 1 |
|
| 2 |
# Crate Features |
| 3 |
|
| 4 |
| Cargo feature | Description | |
| 5 |
| --- | --- | |
| 6 |
| `priority` | Enable support for the priority parameter. | |
| 7 |
| `proc_macro` | Enable support for the proc-macro `#[ctor]` attribute. The declarative form (`ctor!(...)`) is always available. It is recommended that crates re-exporting the `ctor` macro disable this feature and only use the declarative form. | |
| 8 |
| `std` | Enable support for the standard library. | |
| 9 |
|
| 10 |
# Macro Attributes |
| 11 |
|
| 12 |
<table><tr><th>Attribute</th><th>Description</th></tr> |
| 13 |
<tr><td><code>anonymous</code></td><td> |
| 14 |
|
| 15 |
Do not give the constructor a name in the generated code (allows for |
| 16 |
multiple constructors with the same name). Equivalent to wrapping the |
| 17 |
constructor in an anonymous const (i.e.: `const _ = { ... };`). |
| 18 |
|
| 19 |
|
| 20 |
</td></tr> |
| 21 |
<tr><td><code>body(link_section = ".text.startup")</code></td><td> |
| 22 |
|
| 23 |
Place the constructor body in a custom link section. By default, this |
| 24 |
uses the appropriate platform-specific link section. |
| 25 |
|
| 26 |
Co-locating startup functions may improve performance by allowing the binary |
| 27 |
to page them in and out of memory together. |
| 28 |
|
| 29 |
|
| 30 |
</td></tr> |
| 31 |
<tr><td><code>crate_path = ::path::to::ctor::crate</code></td><td> |
| 32 |
|
| 33 |
The path to the `ctor` crate containing the support macros. If you |
| 34 |
re-export `ctor` items as part of your crate, you can use this to |
| 35 |
redirect the macro’s output to the correct crate. |
| 36 |
|
| 37 |
Using the declarative [`ctor!`][c] form is |
| 38 |
preferred over this parameter. |
| 39 |
|
| 40 |
[c]: crate::declarative::ctor! |
| 41 |
|
| 42 |
|
| 43 |
</td></tr> |
| 44 |
<tr><td><code>export_name_prefix = "ctor_"</code></td><td> |
| 45 |
|
| 46 |
Specify a custom export name prefix for the constructor function. |
| 47 |
|
| 48 |
If specified, an export with the given prefix will be generated in the form: |
| 49 |
|
| 50 |
`<prefix><priority>_<unique_id>` |
| 51 |
|
| 52 |
|
| 53 |
</td></tr> |
| 54 |
<tr><td><code>link_section = ".ctors"</code></td><td> |
| 55 |
|
| 56 |
Place the constructor function pointer in a custom link section. By |
| 57 |
default, this uses the appropriate platform-specific link section. |
| 58 |
|
| 59 |
|
| 60 |
</td></tr> |
| 61 |
<tr><td><code>naked</code></td><td> |
| 62 |
|
| 63 |
Use the least-possibly mangled version of the linker invocation for this |
| 64 |
constructor. This is not recommended for general use as it may prevent |
| 65 |
authors of binary crates from having low-level control over the order of |
| 66 |
initialization. |
| 67 |
|
| 68 |
There are no guarantees about the order of execution of constructors |
| 69 |
with this attribute, just that it will be called at some point before |
| 70 |
`main`. |
| 71 |
|
| 72 |
`naked` constructors are always executed directly by the underlying C |
| 73 |
library and/or dynamic loader. |
| 74 |
|
| 75 |
`naked` cannot be used with the `priority` attribute. |
| 76 |
|
| 77 |
|
| 78 |
</td></tr> |
| 79 |
<tr><td><code>priority = N | early | late</code></td><td> |
| 80 |
|
| 81 |
The priority of the constructor. Higher-`N`-priority constructors are |
| 82 |
run last. `N` must be between 0 and 999 inclusive for ordering |
| 83 |
guarantees (`N` >= 1000 ordering is platform-defined). |
| 84 |
|
| 85 |
Priority is specified as numeric value, string literal, or the |
| 86 |
identifiers `early`, `default`, or `late`. The integer value will be |
| 87 |
clamped to a platform-defined range (typically 0-65535), while string |
| 88 |
priorities are passed through unprocessed. |
| 89 |
|
| 90 |
Most platforms reserve the numeric values range of 0..100 for their own |
| 91 |
internal use and it may not be safe to access platform services (`libc` |
| 92 |
or other) in constructors with those priorities. |
| 93 |
|
| 94 |
Priority is applied as follows: |
| 95 |
|
| 96 |
- `N` is run in increasing order, from `0 <= N <= 999`. |
| 97 |
- `early` is run at a priority level where it is safe to access the C |
| 98 |
runtime. This is equivalent to a priority of 101 on most platforms. |
| 99 |
- `default` is the default, and is run after `early`. This is |
| 100 |
equivalent to a priority of 500. |
| 101 |
- `late` is run last, and will be positioned to run after most |
| 102 |
constructors, even outside the range `0 <= N <= 999`. The equivalent |
| 103 |
priority is platform-defined. |
| 104 |
- `main` is run, for binary targets. |
| 105 |
|
| 106 |
Ordering with explicit priority values outside of `0 <= N <= 999` is |
| 107 |
platform-defined with respect to the list above, however platforms will |
| 108 |
order constructors within a given priority range in ascending order |
| 109 |
(i.e.: 10000 will run before 20000). |
| 110 |
|
| 111 |
|
| 112 |
</td></tr> |
| 113 |
<tr><td><code>unsafe</code></td><td> |
| 114 |
|
| 115 |
|
| 116 |
Marks a ctor as unsafe. Required. |
| 117 |
|
| 118 |
The `ctor` crate rejects `#[ctor]` without marking the item unsafe; |
| 119 |
that error can be suppressed by passing |
| 120 |
`RUSTFLAGS="--cfg linktime_no_fail_on_missing_unsafe"` to Cargo. |
| 121 |
|
| 122 |
|
| 123 |
</td></tr> |
| 124 |
<tr><td><code>used(linker)</code></td><td> |
| 125 |
|
| 126 |
|
| 127 |
Mark generated function pointers `used(linker)`. Requires nightly |
| 128 |
for the nightly-only feature `feature(used_with_arg)` (see |
| 129 |
<https://github.com/rust-lang/rust/issues/93798>). |
| 130 |
|
| 131 |
This can be made the default by using the `cfg` flag |
| 132 |
`linktime_used_linker` (`RUSTFLAGS="--cfg linktime_used_linker"`). |
| 133 |
|
| 134 |
For a crate using this macro to function correctly with and without |
| 135 |
this flag, it is recommended to add the following line to the top of |
| 136 |
lib.rs in the crate root: |
| 137 |
|
| 138 |
`#![cfg_attr(linktime_used_linker, feature(used_with_arg))]` |
| 139 |
|
| 140 |
|
| 141 |
</td></tr> |
| 142 |
</table> |
| 143 |
|
| 144 |
# Defaults |
| 145 |
|
| 146 |
## `body_link_section` |
| 147 |
|
| 148 |
```rust |
| 149 |
# #[cfg(false)] { |
| 150 |
#[cfg(target_os = "linux")] |
| 151 |
# const _: () = { let |
| 152 |
body_link_section = ".text.startup" |
| 153 |
# ; }; |
| 154 |
|
| 155 |
#[cfg(target_os = "android")] |
| 156 |
# const _: () = { let |
| 157 |
body_link_section = ".text.startup" |
| 158 |
# ; }; |
| 159 |
|
| 160 |
#[cfg(target_os = "freebsd")] |
| 161 |
# const _: () = { let |
| 162 |
body_link_section = ".text.startup" |
| 163 |
# ; }; |
| 164 |
|
| 165 |
#[cfg(all(target_os = "windows", any(target_env = "gnu", target_env = "msvc")))] |
| 166 |
# const _: () = { let |
| 167 |
body_link_section = ".text$A" |
| 168 |
# ; }; |
| 169 |
|
| 170 |
#[cfg(all(target_os = "windows", not(any(target_env = "gnu", target_env = "msvc"))))] |
| 171 |
# const _: () = { let |
| 172 |
body_link_section = ".text.startup" |
| 173 |
# ; }; |
| 174 |
|
| 175 |
#[cfg(target_vendor = "apple")] |
| 176 |
# const _: () = { let |
| 177 |
body_link_section = "__TEXT,__text_startup,regular,pure_instructions" |
| 178 |
# ; }; |
| 179 |
|
| 180 |
// default |
| 181 |
body_link_section = () |
| 182 |
# } |
| 183 |
``` |
| 184 |
|
| 185 |
## `export_name_prefix` |
| 186 |
|
| 187 |
```rust |
| 188 |
# #[cfg(false)] { |
| 189 |
#[cfg(target_os = "aix")] |
| 190 |
# const _: () = { let |
| 191 |
export_name_prefix = "__sinit" |
| 192 |
# ; }; |
| 193 |
|
| 194 |
// default |
| 195 |
export_name_prefix = () |
| 196 |
# } |
| 197 |
``` |
| 198 |
|
| 199 |
## `link_section` |
| 200 |
|
| 201 |
```rust |
| 202 |
# #[cfg(false)] { |
| 203 |
#[cfg(target_vendor = "apple")] |
| 204 |
# const _: () = { let |
| 205 |
link_section = "__DATA,__mod_init_func,mod_init_funcs" |
| 206 |
# ; }; |
| 207 |
|
| 208 |
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", |
| 209 |
target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly", |
| 210 |
target_os = "illumos", target_os = "haiku", target_os = "vxworks", target_os = |
| 211 |
"nto", target_family = "wasm"))] |
| 212 |
# const _: () = { let |
| 213 |
link_section = ".init_array" |
| 214 |
# ; }; |
| 215 |
|
| 216 |
#[cfg(target_os = "none")] |
| 217 |
# const _: () = { let |
| 218 |
link_section = ".init_array" |
| 219 |
# ; }; |
| 220 |
|
| 221 |
#[cfg(target_arch = "xtensa")] |
| 222 |
# const _: () = { let |
| 223 |
link_section = ".ctors" |
| 224 |
# ; }; |
| 225 |
|
| 226 |
#[cfg(all(target_os = "windows", any(target_env = "gnu", target_env = "msvc")))] |
| 227 |
# const _: () = { let |
| 228 |
link_section = ".CRT$XCU" |
| 229 |
# ; }; |
| 230 |
|
| 231 |
#[cfg(all(target_os = "windows", not(any(target_env = "gnu", target_env = "msvc"))))] |
| 232 |
# const _: () = { let |
| 233 |
link_section = ".ctors" |
| 234 |
# ; }; |
| 235 |
|
| 236 |
#[cfg(target_os = "uefi")] |
| 237 |
# const _: () = { let |
| 238 |
link_section = ".init_array" |
| 239 |
# ; }; |
| 240 |
|
| 241 |
#[cfg(target_os = "aix")] |
| 242 |
# const _: () = { let |
| 243 |
link_section = () |
| 244 |
# ; }; |
| 245 |
|
| 246 |
// default |
| 247 |
link_section = ".init_array" |
| 248 |
# } |
| 249 |
``` |
| 250 |
|
| 251 |
## `priority` |
| 252 |
|
| 253 |
```rust |
| 254 |
# #[cfg(false)] { |
| 255 |
#[cfg(feature = "priority")] |
| 256 |
# const _: () = { let |
| 257 |
priority = default |
| 258 |
# ; }; |
| 259 |
|
| 260 |
// default |
| 261 |
priority = () |
| 262 |
# } |
| 263 |
``` |
| 264 |
|
| 265 |
## `r#unsafe` |
| 266 |
|
| 267 |
```rust |
| 268 |
# #[cfg(false)] { |
| 269 |
#[cfg(linktime_no_fail_on_missing_unsafe)] |
| 270 |
# const _: () = { let |
| 271 |
r#unsafe = (no_fail_on_missing_unsafe) |
| 272 |
# ; }; |
| 273 |
|
| 274 |
// default |
| 275 |
r#unsafe = () |
| 276 |
# } |
| 277 |
``` |
| 278 |
|
| 279 |
## `used_linker` |
| 280 |
|
| 281 |
```rust |
| 282 |
# #[cfg(false)] { |
| 283 |
#[cfg(linktime_used_linker)] |
| 284 |
# const _: () = { let |
| 285 |
used_linker = used_linker |
| 286 |
# ; }; |
| 287 |
|
| 288 |
// default |
| 289 |
used_linker = () |
| 290 |
# } |
| 291 |
``` |