1

I am trying to decrease the binary size by overriding default __terminate_handler. Normally, when compiling with GCC, this piece of code:

[[noreturn]] void terminate() noexcept
{
    while (true)
        ;
}

namespace __cxxabiv1
{
std::terminate_handler __terminate_handler = terminate;
}

Makes the default __terminate_handler overridden and the binary size decreases by 30-50 kB, depending on the release/debug build type.

When compiling with Clang I use -nostdlib linker flag, thus I have to link the standard libraries explicitly, by putting -lstdc++ -lgcc as the command line options. This means that I get an error:

ld.lld: error: duplicate symbol: __cxxabiv1::__terminate_handler
-> defined in armgnutoolchain-src/arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(eh_term_handler.o)
-> defined in custom_terminate.cpp.obj

Both, custom code and libstdc++ define __cxxabiv1::__terminate_handler (_ZN10__cxxabiv119__terminate_handlerE) as a symbol initialized in data section. Ideally, I would like the symbol to be defined weak in the libstdc++.

Is there a way to ignore the symbol defined in libstdc++?

K. Koovalsky
  • 596
  • 4
  • 17
  • 1
    [OT]: `while (true);` is UB. – Jarod42 Nov 29 '21 at 11:08
  • It is ugly, but maybe it is possible to modify libstdc++ by adding your `__terminate_handler` before the one in the library (I guess it should be possible with `ar`, but I have not checked the details). The linker should then choose your version and ignore the other. See e.g. https://stackoverflow.com/questions/23079997/override-weak-symbols-in-static-library. – nielsen Nov 30 '21 at 00:33

0 Answers0