Skip to content

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

Closed
@pocesar

Description

@pocesar

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

jonas-schievink commented on Apr 19, 2019

@jonas-schievink
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

pocesar commented on Apr 19, 2019

@pocesar
Author

I've already tried that and doesn't work on the program that is using std... unless I'm missing something obvious

jonas-schievink

jonas-schievink commented on Apr 19, 2019

@jonas-schievink
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

pocesar commented on Apr 19, 2019

@pocesar
Author
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

jonas-schievink commented on Apr 19, 2019

@jonas-schievink
Contributor

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

Julian-Wollersberger

Julian-Wollersberger commented on Apr 20, 2019

@Julian-Wollersberger
Contributor

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

pocesar

pocesar commented on Apr 21, 2019

@pocesar
Author

@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

sfackler commented on Apr 21, 2019

@sfackler
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

added
I-heavyIssue: Problems and improvements with respect to binary size of generated code.
on Apr 21, 2019
NickeZ

NickeZ commented on Nov 28, 2019

@NickeZ

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

jyn514 commented on May 19, 2021

@jyn514
Member

Duplicate of #54981.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-heavyIssue: Problems and improvements with respect to binary size of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pocesar@NickeZ@sfackler@jonas-schievink@jyn514

        Issue actions

          no way to not ship panic strings inside the compiled binary · Issue #60105 · rust-lang/rust