2

I am struggling to get my code down to minimal bare bones size! I am using a STM32F0 with only 32k flash and need a good part of the flash for data storage. My code is already at approx 20k flash size! Some of this is due to use of the STM32 HAL functions which I can account for and optimise later if needed.

However, my biggest consumer of flash is all the implicitly included library routines. I can not seem to remove these functions. They are not called anywhere in my code or any HAL code. Functions such as _malloc_r (1.3k Bytes), and __vfiprintf_r (3kB) and many others are using a large part of my flash. I think these are all the libc functions. I do not use these and would like them gone!

Does anybody know how to remove these?

I have tried different optimisation levels and linker options but no luck so far. I have tried -nostdlib and --specs=nosys.specs with no change. If I remove my file with definitions for functions such as _exit I get a linker error suggesting that the library is still included and needs these. Also linker map confirms presence of a lot of unwanted functions!

Any suggestions?

3
  • 2
    Have you tried calling the linker ld explicitly instead of through the gcc frontend program? Commented Aug 24, 2017 at 9:29
  • 2
    Add -v to see how ld is called. -nostartfiles might be relevant. Commented Aug 24, 2017 at 9:39
  • 2
    perhaps you should be using -ffreestanding? Commented Aug 24, 2017 at 10:18

2 Answers 2

3

When you are wondering about what takes space, or why functions and libraries have been linked in, generate a map file with cross references - something like "-Wl,-Map=program.map,--cref". Look at the file with a text editor, and you can see why a function like malloc has been included.

Sign up to request clarification or add additional context in comments.

Comments

2

Solved... Some of my code included and called assert. The moment I removed assert calls my code size more than halved! I instead used the STM32 HALs assert_param macro that is a light weight assert that just redirects to a user defined function.

It would be helpful if someone could explain to me how gcc decides to include library functions when assert is called? I see that assert.h declares an external function __assert_func. How does the linker know to reference it from a library rather than just say "undefined reference to __asert_func"?

7 Comments

As a rule of thumb, never use assert() in the production release. And as another rule of thumb, never use assert() in embedded systems. Roll out your own error reporting system for the given product, you're likely going to need one anyway, to report product-specific errors.
@Lundin asserts in STM32 are designed in a way #define assert_param(condition) and for production you can simply make empty define. Otherwise I agree with you.
Try to use static assertions as much as possible. They have no runtime cost, and won't need to be disabled for release builds.
The second half of this answer would probably be another good question to ask
@Lundin I would add another one - void HAL or HAL like libraries.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.