c++gccg++embedded

How to prevent C++ name demangling functions from being included in binary


I am developing firmware for an embedded application on ARM Cortex-M in C++ with GCC 7.3.1 in C++14 mode. There's only 64k of flash available, and my binary wouldn't fit. Looking at the map file I see that there's a function called __gcclibcxx_demangle_callback in the binary that takes up 27k flash space. I understand that this is related to C++ name demangling. The linker doesn't discard this symbol although I'm definitely not doing any name demangling in my code. I'm using STL here and there though. How can I eliminate this function to gain some flash space?

I've tried passing -ffreestanding -fno-exceptions -nostartfiles -fno-rtti to the compiler and the linker. Passing -nostdlib causes the linking to fail even if I pass -lc -lg -lgcc -lstdc++.


Solution

  • @Russ Schultz's comment got me thinking. Very probably it really was some STL code that used name demangling, and it would have been surprising if there was no other libstdc++ that was more suitable for embedded systems. Apparently, the GNU Arm Embedded Toolchain provides alternate versions of libstdc++. One is the "nano" version which can be easily used by passing -specs=nano.specs to the linker. This reduces the size of the resulting binary drastically (115k to 45k), and roughly looking at the contents, it really includes only necessary symbols. Almost no code bloat apart from some C++ vtables which are tiny anyway.