Closed AE1020 closed 2 months ago
Without -Clink-dead-code=yes
rustc will pass --gc-section
to the linker. This will cause every symbol not reachable from an exported symbol to be omitted. In case of executables only main is exported unless -Z export-executable-symbols=yes
is used.
Possible if #[no_mangle] caused functions to be considered live roots, as the comment suggests
If this is not true, should the comment be updated?
The comment is about the functions that will be codegened, not about those that end up in the final executable after whatever logic the linker uses to determine if functions should be kept.
Triage: Seems like -Z export-executable-symbols=yes
is the right solution here? Closing.
I'm attempting to add instrumentation functions to rustc, to be called from the LLDB debug console. Here's a simple example of trying to add a C-style function and a Rust function to a hello world program:
I built this with
rustc -g hello.rs
. Then with a breakpoint in main, at the LLDB debug console I tried:I had seen suggestions online that adding
#[no_mangle]
should be enough to get a function into the root set. That's echoed in comments at time of writing in the rust compiler sources at compiler/rustc_monomorphize/src/collector.rs:The functions would be included in the link if I instead built with
rustc -g hello.rs -Clink-dead-code
:Unfortunately... there are issues (e.g. #77529) with using
-Clink-dead-code
when building some codebases, and that includes building the rust compiler itself.If I try building rustc with
export RUSTFLAGS="-Clink-dead-code"
, this error stops the build (probably there would be others):(Coincidentally (?), that's the file with the comment about #[no_mangle] that I cite above.)
What I'm actually looking for is a way to add these root functions callable from the debugger to the rustc compiler...
Possible if #[no_mangle] caused functions to be considered live roots, as the comment suggests
Possible if there was a way to build rustc using the
RUSTFLAGS="-Clink-dead-code"
Perhaps there's another other trick or #[...] setting which could affect being included in the link?
(A workaround is to hack in spurious calls reachable from main() somewhere to the instrumentation functions.)
Meta