Description
Already have panic = "abort"
in cargo, and it doesn't seem to leave the panic!() strings out of the compiled binary, even on --release
build. I don't care about panic strings, I just want a slim generated exe that fails if something goes wrong.
tokio
, crossbeam
and chrono
are adding a lot of useless strings in the .rdata
on Windows, for example. also opt-level = "s"
(and "z"
)
tried to use panic::set_hook
as well, it didn't do anything. tried to make the long path names using remap-path-prefix
but it seemed to have no effect (the paths are still full absolute paths to current user folder/.cargo/registry). doesn't matter if I use windows paths (eg: C:\Users or Unix C:/Users) and besides the #40552 that's two years old, but a minor issue when you need to rely on std and size optimizations.
also tried a lot of other stuff (like link-arg=-s
, debuginfo=0
, no-landing-pads
, etc), while the generated exe is smaller than a full blown cargo build --release
, so it's really an unexpected output from the compiler
Activity
jonas-schievink commentedon Apr 19, 2019
You can do this by using a custom panic implementation like panic-abort which doesn't use the strings. Also see RFC 2070 which introduced this feature and is currently the best source of information on how to use it.
pocesar commentedon Apr 19, 2019
I've already tried that and doesn't work on the program that is using std... unless I'm missing something obvious
jonas-schievink commentedon Apr 19, 2019
Can you provide a reproduction for that? It definitely works on embedded
no_std
code, but it should also work with libstd-using code.pocesar commentedon Apr 19, 2019
jonas-schievink commentedon Apr 19, 2019
Ah, I misunderstood what you meant. Looks like that RFC indeed only targets
#![no_std]
.Julian-Wollersberger commentedon Apr 20, 2019
Linux has a
strip
command that discards symbols from object files. Maybe there is an equivalent under Winows.pocesar commentedon Apr 21, 2019
@Julius-Beides actually, opt-level either Z or S does that, if you also pass
-Cdebuginfo=0
along with-Zstrip-debuginfo-if-disabled=yes
, but doesn't get rid of those hardcoded panics. without it the generated binary is almost 2MBmaybe the default panic implementation should use
#[cfg(...)]
to literally omit the strings onrust/src/libcore/macros.rs
Line 12 in 9ebf478
#[cfg(not(panic="abort"))]
(but this doesn't work, of course) before the call to panic fn, since even assert_* macros panic with strings. I usually do that for debug strings, with a#[cfg(debug_assertions)]] println!()
(when I actually do care about the information, during profile.dev / test)sfackler commentedon Apr 21, 2019
You can do this by building your own std (xargo can help with this) with this Cargo feature enabled: https://github.com/rust-lang/rust/blob/master/src/libstd/Cargo.toml#L60
NickeZ commentedon Nov 28, 2019
Did you ever solve this issue? It is easy to make the strings deterministic that are from the crate you are actually compiling. But there are strings that point to dependencies in
/home/<user>/.cargo
which I need to get rid of.I compile with
opt-level=z
and my panic implementation does not reference the strings:jyn514 commentedon May 19, 2021
Duplicate of #54981.