cbacktracelibgcc

How to detect if libgcc has been loaded


I want to use GNU libc's backtrace() and backtrace_symbols_fd() functions in a signal handler. However, the backtrace docs page tells me to make sure that libgcc is loaded already in that case.

But how to make sure that libgcc is loaded?

I could try to call some simple functions from libgcc, but it seems that they could simply be builtin in the compiler already and not trigger loading of libgcc. I also tried to use something like dlopen("/lib/gcc/x86_64-linux-gnu/12/libgcc_s.so", RTLD_NOW); on Debian 12, but this does not work, either (with random other shared lib it does work, though). Linking libgcc statically is apparently also not a good option. The solution should be fairly generic and not tied to, e.g., Ubuntu only.

Besides my searching effort, I haven't found any good solution for this case. Any tips or hints would be highly appreciated.


Solution

  • If you want a function inside libgcc that you can call to ensure the library is loaded, an obvious choice is backtrace itself. Just call it once and throw away the result.