cranelift-object: Support marking data object as used
Feature
See title.
Benefit
This is required for implementing #[used(linker)] in cg_clif. This attribute is equivalent to attribute((used)) in C. And on all non-ELF platforms it is the default. when #[used] is used. On ELF platforms it may become the default in the future.
Implementation
Add a flag to DataDescription and then set the right symbol flag for the given object file format.
Note that #[used(linker)] is __attribute__((used,retain)), not __attribute__((used)), which is just #[used(compiler)].
| Compiler | Linker | |
|---|---|---|
| GCC C | __attribute__((used)) |
__attribute__((retain)) (note that you need both) |
| Rust | #[used(compiler)] |
#[used(linker)] |
| LLVM | @llvm.compiler.used |
@llvm.used |
| ELF | - | SHT_GNU_RETAIN ("R" in Assembly) |
https://github.com/bytecodealliance/wasmtime/pull/11206 implements this on the Cranelift side and https://github.com/rust-lang/rustc_codegen_cranelift/tree/used_static has the cg_clif changes.