https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826
Yuri Gribov <tetra2005 at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |tetra2005 at gmail dot com --- Comment #3 from Yuri Gribov <tetra2005 at gmail dot com> --- This is a very funny bug but not related to GCC per se. Firstly, let's consider a miminal repro: __attribute__((constructor)) static void some_init() { dlsym(RTLD_DEFAULT, "anything"); } (segfaults just as well). Under -O0 this produces a normal call: call dlsym@PLT ... ret but with -O2 GCC is clever enough to tail-call-optimize it to a plain jump: jmp dlsym@PLT Now dlsym (and other dl-functions) secretly take shadow parameter - return address on stack: void * __dlsym (void *handle, const char *name DL_CALLER_DECL) { ... struct dlsym_args args; args.who = DL_CALLER; args.handle = handle; args.name = name; (from dlsym.c). As in our case return address is missing, args.who argument is missing which causes segfault during symbol resolution (dynamic linker is lame on checks).