Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no way to not ship panic strings inside the compiled binary #60105

Closed
pocesar opened this issue Apr 19, 2019 · 10 comments
Closed

no way to not ship panic strings inside the compiled binary #60105

pocesar opened this issue Apr 19, 2019 · 10 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-heavy Issue: Problems and improvements with respect to binary size of generated code.

Comments

@pocesar
Copy link

pocesar commented Apr 19, 2019

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

@jonas-schievink
Copy link
Contributor

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
Copy link
Author

pocesar commented 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
Copy link
Contributor

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
Copy link
Author

pocesar commented Apr 19, 2019

error: duplicate lang item in crate `panic_abort`: `panic_impl`.
  |
  = note: first defined in crate `std`.

error: aborting due to previous error

error: Could not compile `bin`.

To learn more, run the command again with --verbose.

@jonas-schievink
Copy link
Contributor

Ah, I misunderstood what you meant. Looks like that RFC indeed only targets #![no_std].

@Julian-Wollersberger
Copy link
Contributor

Linux has a strip command that discards symbols from object files. Maybe there is an equivalent under Winows.

@pocesar
Copy link
Author

pocesar commented 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 2MB

maybe the default panic implementation should use #[cfg(...)] to literally omit the strings on

$crate::panicking::panic(&($msg, file!(), line!(), __rust_unstable_column!()))
like #[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
Copy link
Member

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

@jonas-schievink jonas-schievink added the I-heavy Issue: Problems and improvements with respect to binary size of generated code. label Apr 21, 2019
@jonas-schievink jonas-schievink added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 20, 2019
@NickeZ
Copy link

NickeZ commented 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:

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    #[cfg(debug_assertions)]
    print_debug!(0, "Error: {}", info);
    #[cfg(not(debug_assertions))]
    print_debug!(0, "Error");
    loop {}
}

@jyn514
Copy link
Member

jyn514 commented May 19, 2021

Duplicate of #54981.

@jyn514 jyn514 closed this as completed May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-heavy Issue: Problems and improvements with respect to binary size of generated code.
Projects
None yet
Development

No branches or pull requests

6 participants