__dso_handle
is a "guard" that is used to identify dynamic shared objects during global destruction.
Thom Chiovoloni wrote a concise description of __dso_handle
while debugging issues related to it:
__dso_handle
is a magic symbol that has a different value in every .so
(it's a hidden symbol provided by the loader). It's used ... to let the runtime ("the runtime" = libc + loader + ...) know which shared library the thread local dtor applies to, which allows the runtime to avoid some cases where it would call a function pointer coming from an unloaded module.
Realistically, you should stop reading here. If you're trying to defeat object identification by messing with __dso_handle
, something is likely very wrong.
However, since you asked where it is defined: the answer is complex. To surface the location of its definition (for GCC), use iostream
in a C++ file, and, after that, do extern int __dso_handle;
. That should surface the location of the declaration due to a type conflict (see this forum thread for a source).
Sometimes, it is defined manually.
Sometimes, it is defined/supplied by the "runtime" installed by the compiler (in practice, the CRT is usually just a bunch of binary header/entry-point-management code, and some exit guards/handlers). In GCC (not sure if other compilers support this; if so, it'll be in their sources):
Often, it is defined in the stdlib:
Further reading: