Mathieu Lacage mathieu.lacage@gmail.com
Wed Feb 10 08:59:00 GMT 2010


While trying to debug the intialization and shutdown of my code, I
stumbled upon cxa_atexit and cxa_finalize: I tried to grep a bit the
gcc and glibc codebases to understand who calls them and with which
arguments but I am a bit lost so, help would be welcome. I can see how
each bit and piece of the puzzle works but the global picture somewhat
escapes me.

1) I can't figure out who inserts the calls to __cxa_finalize/atexit.
I suspect that it's gcc but I can't see which piece of gcc code does
this. I thought that it would be the gcc/crtstuff.c file but it
includes only a call to __cxa_finalize from the .fini section and only
when CRTSTUFF_O is  defined and this does not appear to be defined on
my platform. (fedora11 x86_64). So, who makes sure that these
functions are called during .init/.fini ?

2) I can't figure out how the code comes up with the third argument to
__cxa_atexit and the first argument to __cxa_finalize. I expected some
kind of per-binary global variable (say, __dsa_handle) for
__cxa_finalize which would be set by the code calling __cxa_atexit but
I can't see this symbol defined anywhere in my binaries. Of course, I
can't see how __cxa_atexit comes up with this third argument. Is the
__dso_handle really expected to be a the void *handle associated by
the dynamic linker to this binary or is it merely expected to be a
unique address specific to this binary ?

3) why is this scheme used instead of just using atexit+the normal
constructor/destructor things on a per-binary basis ?

Mathieu Lacage <mathieu.lacage@gmail.com>

More information about the Gcc-help mailing list