Why does my attiny binary include core::fmt code?

For a quite minimal embedded project with an attiny85, I was surprised that the binary contains some huge functions that don't seem to be used in my code at all, like:

<<core::fmt::builders::PadAdapter as core::fmt::Write>::write_str::h47ed86973795e0d0>
<core::char::methods::<impl char>::encode_utf8::h654557cbf67e1bf5>:

That takes my binary beyond the 8k size limit with almost no production code at all :confused:

I see that attiny-hal depends on ufmt, but shouldn't LTO remove unused code? I have lto enabled:

panic = "abort"
lto = true
codegen-units = 1
opt-level = "s"
debug-assertions = false

My code doesn't use any string handling or utf-8 at all. Could you help me to remove that code, please?

What's the code?

encode_utf8 is probably from string.push(char) or collect::<String>().

PadAdapter is used by {:?} formatting string of structs. It's usually from assert! or unwrap().

panic = "abort" unfortunately doesn't remove all panic machinery, it still prints error before aborting. Try panic_immediate_abort unstable feature:


Perfect! This, along with strip=true brought it down to 1369 bytes :slight_smile: Thank you!


I wonder what that means for an ATtiny processor. To where? Using what hardware?

then probably panics again when stderr doesn't work :slight_smile:

1 Like

The actual behaviour for a panic is determined by the #[panic_handler] attribute.

@kornel was describing what the default is the implementation provided by std. This calls the panic hook that was set with std::panic::set_hook() (the default implementation just prints to stderr) and then begins unwinding the stack.

In a #![no_std] application like this, the OP would have had to either provide their own #[panic_handler] function or link to a crate that provides one. There are loads of implementations to choose from, ranging from implementations that just loop infinitely or reset the microcontroller or print the panic message via ITM/semihosting or whatever.

1 Like

No, it aborts, though that might change in the future.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.