The crate is part of the linktime project.
| crate | docs | version |
|---|---|---|
linktime |
||
ctor |
||
dtor |
||
link-section |
dtor
Shutdown functions for Rust (like __attribute__((destructor)) in C/C++) for
Linux, macOS, Windows, mobile (iOS/Android), WASM, BSD/BSD-likes and many other
platforms.
use dtor;
use *;
Examples
Print a message at shutdown time.
Platform Support
| Platform | Link Section | at_binary_exit | at_module_exit |
|---|---|---|---|
| Linux | .fini_array |
Yes (atexit) |
Yes (__cxa_atexit) |
| MacOS | .mod_term_func 🍎 |
Yes (atexit) |
Yes (__cxa_atexit) |
| Windows | .CRT$XPU 🪟 |
No | Yes (atexit) |
| AIX | No 🔵 | Yes | Yes |
| Other POSIX-like platforms | .fini_array/.dtors |
Yes (atexit) |
Yes (__cxa_atexit) |
Notes:
- 🍎 Not recommended. Apple platforms no longer call
mod_term_funcfunctions. - 🪟 Not recommended. Windows platforms may not reliably call functions in link sections, unless a binary is built with a static CRT.
- 🔵 Link sections are not supported on AIX, but the
platform calls functions with the prefix
__sinitand__stermat startup and shutdown respectively.
Shutdown Method (#[dtor(method = ...)])
The #[dtor] macro supports multiple registration strategies via
#[dtor(method = ...)]. The best choice is platform-dependent:
#[dtor](no method specified): Use the platform's most reliable method:at_module_exiton Windows and Apple platforms, andlinkeron others.unload: Run on module unload (library unload or process exit) using the platform's default unload method.term: Run on process termination only using the platform's default termination method. Not recommended: code may be unloaded before the dtor runs.at_module_exit: Register using__cxa_atexit(non-Windows) oratexit(Windows) so the dtor runs when the module unloads.at_binary_exit: Register to run at process exit (unsupported on Windows).linker: Register using the platform's linker mechanism (link_sectionon all platforms with the exception ofexport_name_prefixon AIX). Unsupported on Apple platforms.
Default:
- Apple and Windows default to
at_module_exit - Most other platforms default to
linker
Examples:
use dtor;
/// Use `at_module_exit` on all platforms
use dtor;
/// Use `link_section` with a section name of `.dtors` on most platforms,
/// and `export_name_prefix` on AIX.
///
/// Platform note: this will fail to compile on Apple platforms.
Warnings
Rust's philosophy is that nothing happens before or after main and this library
explicitly subverts that. The code that runs in the ctor and dtor functions
should be careful to limit itself to libc functions and code that does not
rely on Rust's stdlib services.
See ::life_before_main for more information.
Under the Hood
The #[dtor] macro effectively creates a constructor that calls libc::atexit
with the provided function, i.e. roughly equivalent to:
Crate Features
| Cargo feature | Description |
|---|---|
proc_macro |
Enable support for the proc-macro #[dtor] attribute. The declarative form (dtor!(...)) is always available. It is recommended that crates re-exporting the dtor macro disable this feature and only use the declarative form. |
std |
Enable support for the standard library. |
Macro Attributes
Make the ctor function anonymous.
Specify a custom crate path for the dtor crate. Used when re-exporting the dtor macro.
Specify a custom export name prefix for the constructor function.
If specified, an export with the given prefix will be generated in the form:
<prefix>_<unique_id>
Place the initialization function pointer in a custom link section.
Specify a custom export name prefix for the destructor function.
If specified, an export with the given prefix will be generated in the form:
<prefix>_<unique_id>
Place the destructor function pointer in a custom link section.
Specify the dtor method.
term: Run the dtor on binary termination using the platform's default_term_method. Not recommended as code may be unloaded before the dtor is called.unload: Run the dtor on module unload (library or binary) using the platform's default_unload_method.at_module_exit: Run the dtor using the platform'sat_module_exit(__cxa_atexiton all platforms other than Windows,atexiton Windows).at_binary_exit: Run the dtor using the platform'sat_binary_exit(unsupported on Windows platforms).linker: Register the dtor using the platform's link_section or export_name_prefix (unsupported on Apple platforms).
Marks a dtor as unsafe. Required.
The ctor crate will warn if there is no unsafe flag in the ctor
annotation. This warning for a missing unsafe keyword can be hidden
by passing RUSTFLAGS="--cfg linktime_no_fail_on_missing_unsafe" to
Cargo.
Mark generated functions pointers used(linker). Requires nightly
for the nightly-only feature feature(used_with_arg) (see
https://github.com/rust-lang/rust/issues/93798).
The can be made the default by using the cfg flag
linktime_used_linker (RUSTFLAGS="--cfg linktime_used_linker").
For a crate using this macro to function correctly with and without this flag, it is recommended to add the following line to the top of lib.rs in the crate root:
#![cfg_attr(linktime_used_linker, feature(used_with_arg))]
Defaults
ctor_export_name_prefix
ctor_export_name_prefix = "__sinit80000000"
// default
ctor_export_name_prefix =
ctor_link_section
ctor_link_section = "__DATA,__mod_init_func,mod_init_funcs"
ctor_link_section = ".init_array"
ctor_link_section = ".init_array"
ctor_link_section = ".ctors"
ctor_link_section = ".CRT$XCU"
ctor_link_section = ".ctors"
ctor_link_section =
// default
ctor_link_section =
default_term_method
default_term_method = at_module_exit
// default
default_term_method = at_binary_exit
default_unload_method
// default
default_unload_method = at_module_exit
export_name_prefix
export_name_prefix = "__sterm80000000"
// default
export_name_prefix =
link_section
link_section = "__DATA,__mod_term_func,mod_term_funcs"
link_section = ".fini_array"
link_section = ".fini_array"
link_section = ".dtors"
link_section = ".CRT$XPU"
link_section = ".dtors"
link_section =
// default
link_section =
method
method = at_module_exit
method = at_module_exit
// default
method = linker
r#unsafe
r#unsafe =
// default
r#unsafe =
used_linker
used_linker = used_linker
// default
used_linker =