global {con,de}structors, atexit & friends
Ulrich Drepper
drepper@cygnus.com
Fri Jul 24 18:07:00 GMT 1998
Jason Merrill and I've spend yesterday quite some time discussing
these problems. The conclusion: the standard is is weak when it
demands that atexit calls from constructors make the registered
function be called before the destructor for the current object but
after the one of the next object. I.e.,
Global constructors:
1. Obj1 constructor runs and calls atexit for function FOO
2. Obj2 constructor runs
Global destructors:
1. Obj2 destructor runs
2. Registered function FOO runs
3. Obj1 destructor runs
This definition enforces the use of atexit for running global
destructors (or at least, it forces that the atexit implementation and
the functions running the global destructor coordinate very closely).
The first (using atexit for all destructors) is possible with the
simple minded model the C++ WG had. But it breaks with dynamically
loaded objects. Beside this, registering the possibly many global
destructors is slow. The second solution isn't possible without
access to the sources for the system.
Beside this, the current gcc already has the atexit problem. For
function local objects we already use atexit. Mike Stump added this
hack in response to a PR we got but this change made only this
situation working. It isn't really correct.
So what to?
We came up with two plans:
- let gcc by default ignore the standard requirement of running atexit()
registered functions like in the example above. I.e., stay with the
current approach of using __DTOR_LIST__ which is faster and (I'm sure
about this) will in 99% of all cases not lead to problems with programs.
To make dynamically loaded objects working we'll have to remove the
atexit() use for function static objects. See below for a description
of the solution we came up with.
- provide a gcc option which select the standard obeying use of atexit().
This means that on systems on which we cannot influence the atexit()
implementation (or where the atexit() implementation does not have the
necessary features) you will not be able to compile C++ code with global
or function local objects as shared objects. Period.
For systems where we can change the atexit() implementation we'll add
some hacks which allow the destructors to remove function hooks from
the atexit() list. I.e., when a dynamic module is unloaded the registered
functions from this module get removed form the list.
Given the user these possibilities we a) provide those who don't want
to follow blindly the standard the possibility to have fast starting
programs, and b) those who want to have 100% compliant programs the
free choice (though maybe only on Free Systems).
Solutions for handling function static destructors.
The problem here is that the destructors for the objects must be
called in the reverse order as the constructors did, as usual. But
the order is determined dynamically. Whenever a function is called
the first time the constructor for the static object is run and what
function is run first cannot be determined (generally) by the
compiler. Mike's solution was to fall back on atexit which solves the
ordering (partly at least). But atexit() shouldn't be used as said
above.
We came up with a solution which should solve this problem and which
also should be fast.
Currently each global destructor function is made available in the
__DTOR_LIST__ by something similar to
.section .dtor
.long address_of_destr
.previous
I.e., the list is constructed by the linker by accumulating all the
entries to this section. For destructors for function static objects
nothing like this happens.
What we suggest is now that the addresses of these destructors are
also added to a list and the function starting the functions on the
__DTOR_LIST__ first starts the functions on this new list, let's call
it __DYNAMIC_DTOR_LIST__.
Since the __DYNAMIC_DTOR_LIST__ cannot be build at link time it is
located in the data section. But we can allocate the room for it
using the same methods we use for the global destructors:
.section .dtor.dynamic
.long 0
.previous
This will make sure that for every destructor of a function static
object there is a slot in this array. When now the corresponding
constructor is run it has to register the destructor. But now instead
of using atexit() it puts it in the __DYNAMIC_DTOR_LIST__. This can
be implemented (thread-) safely by treating the __DYNAMIC_DTOR_LIST__
as a stack. We'll have to provide a global variable which acts as a
stack pointer. Then the only task of the constructor is to get the
current stack pointer, fill in the destructor address and increment
the pointer. Using the instructions to atomically increment a value
we can implement this thread-safe. On dumb architectures we'll maybe
have to use spinlocks but this is a minor problem.
The only problem with this approach we have seen is that the
__DYNAMIC_DTOR_LIST__ and the stack pointer variable must be global or
we add a global function which registers a new destructor (preferably
the later will be used). In some situations this could lead to
problems. E.g., if no export list control is performed on ELF systems
you would get the wrong result if a destructor in a dynamically loaded
object must be registered. Due to the name lookup rules the
registration function in the main problem (if it exists) is found
instead of the function in the shared objects. This could be
prevented using -Bsymbolic but this might have other unwanted side
effects.
But today most ELF binutils (including GNU binutils) provide the
possibility to control the exported symbols of a shared objects. This
also means that for symbols which are global inside the shared object
the PLT is not used. Instead the linker inserts direct calls to the
functions called and we once again get the correct result.
Because of this I think we don't really have any severe problems with
this approach. Function static objects are fortunately rare and if
they are used in shared objects we'll have possiblities to make it
work.
Non-ELF binary formats shouldn't have any problems, they are too dumb
to allow such sophisticated lookup schemes.
That was long. The questions are now:
- do you agree?
- do we miss something?
- anybody volunteering to implement this?
--
---------------. drepper at gnu.org ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com `------------------------
More information about the Gcc
mailing list