Reading an unrelated question that exposes a pattern that looked similar to yours, I think I found the origin of your strange code.
Most probably this code comes from an expansion of thread-related C++11 functionality, in particular stuff that uses __gthread_active_p()
to check if the pthread
library was actually linked in.
Most code that has to potentially deal with threading issues in libstdc++ (but has reasonable fallbacks for when no threading support is required) is littered with
if(__gthread_active_p())
which in turn generally boils down to checking if the symbol __pthread_key_create
is defined to something different than NULL
. The trick here is that such a function is declared with __attribute__ ((weak))
, so, if no definition is provided (i.e. if the pthread
library is not linked in), instead of complaining, the linker just resolves its references to NULL.
So, what you are seeing in your code are the checks that the compiler left in to do thread-related stuff (say, acquiring a mutex that protects some shared resources). The pre-linking code is probably something like:
mov eax,__pthread_key_create
test rax,rax
jz .skip_mutex_init
call __pthread_init_some_mutex
.skip_mutex_init:
mov rax, 0xffffffffffffff60
sub rbx,r15
add QWORD PTR fs:[rax],rbx`
where you see all the __pthread
symbols resolved to NULL
pointers. Of course the resulting code is quite dumb - the compiler couldn't know if the linker was going to be invoked with -lpthread
, and the linker just performs dumb substitutions - it's too late to run the optimizer again (unless LTO is enabled, but that's an entirely different game).
You can see yourself similar patterns playing with the compiler explorer: try enable/disable linking the full binary (the button on the right with 11010 caption), and adding/removing -pthread
on the command line.
Now, I didn't manage to reproduce exactly your result, but probably that comes from a different version of libstdc++
being used, or different thread-aware components being used in your code (I just tried with std::mutex
and std::shared_ptr
).