https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61882
Pekka S <p...@gcc-bugzilla.mail.kapsi.fi> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |p...@gcc-bugzilla.mail.kaps | |i.fi --- Comment #2 from Pekka S <p...@gcc-bugzilla.mail.kapsi.fi> --- This is problem is still present on GCC 13 to a certain degree. For an example, the following construct is not possible, as `weak_template' is essentially always considered being present unless using -O0. Note that `weak' behaves as expected even it is not extern "C". If weak address is sourced from GOT (PIC/PIE) and the symbol is undefined, the program will likely crash, as the address is zero (typically optimized as an unconditional indirect branch). Purely static builds are affected as well, but as there is no indirect branch, the non-existing branch is likely replaced by a no-operation during linking and possibly goes unnoticed for simple constructs like these. [[gnu::weak]] extern void weak(); template <typename T> [[gnu::weak]] extern void weak_template(); void call () { auto f0 = weak; auto f1 = weak_template<int>; if (f0) f0(); if (f1) f1(); /* NB: else statement would be always ignored here. */ // indirect_call(f1); /* see below. */ } It is possible to overcome this issue by not calling the acquired address directly but passing it first to another function. This helper function must not be inlined / optimized (which might require additional tricks documented in the manual and so forth). [[gnu::noipa]] static void indirect_call (auto f) { if (f) f(); }