gccrs: Implement #[used] attribute for static variable retention.
GCC Rust now fully supports the #[used] attribute, ensuring static variables are retained by the compiler and linker as intended.
This commit completes the implementation of the #[used] attribute in GCC Rust. It ensures that static variables marked with #[used] are not garbage collected by the compiler or linker, mirroring the behavior of C’s used and retain attributes. The #[used(linker)] variant specifically instructs the linker to preserve the static item, even if it appears unused.
In Details
gccrs: This commit integrates the full functionality of the #[used] attribute into the GCC Rust backend. The CompileItem::visit method now calls handle_used_attr to correctly process this attribute. This ensures that static variables marked with #[used] or #[used(linker)] are not optimized away, providing behavior analogous to C's used and retain semantics. This is critical for low-level usage where variables might be referenced externally.
- #[used] attribute
- A Rust attribute that prevents the compiler or linker from removing a static variable, even if it appears unused. This is crucial for embedded systems and kernel development where such variables might be accessed indirectly or by external tools.
- static variable
- A variable with static storage duration, meaning it exists for the entire lifetime of the program. In Rust, these are often declared using
staticorstatic mut. - linker
- A program that combines one or more object files generated by a compiler into a single executable file, resolving symbols and references between them.