libgcc2.c: destructor calling order

Guido Flohr gufl0000@stud.uni-sb.de
Mon Jan 31 11:56:00 GMT 2000


Hi,

I've had a look at libgcc2.c from gcc 2.9.5 for someone who is doing a
gcc port to FreeMiNT.  FreeMiNT uses by default a GNU ld which produces 
(sort of an) a.out format (no init section, not ELF!), using sets to grok
with constructors/destructors.

The gcc docs say that destructors have to be called in the reverse of the
corresponding constructors (I'm not a C++ expert and don't know what the
C++ standard currently says about that).

However, if I have two source files foobar.cc and baz.cc:

// foobar.cc
class foo;
class bar;
... Implementation ...
foo obj1;
bar obj2;
EOF

// baz.cc
class baz;
... Implementation ...
baz obj3;
EOF

I get this order:

foo::foo ()
bar::bar ()
baz::baz ()
main ()
bar::~bar ()
foo::~foo ()
baz::~baz ()

Within one object file the compiler itself arranges for the correct order
of the destructors, but the linker doesn't do the same for multiple object
files.  If this order is really wrong then you should maybe modify
__do_global_dtors in libgcc2.c to walk through __DTOR_LIST__ in reverse
order by default.

I hope this is really a bug or at least suspiciuous and I haven't just
stolen your time. ;-)

Ciao

Guido

P.S.: I'm using GNU ld 2.9.1.  Any a.out BFD backend should reproduce this
behavior.  The MiNT backend is not yet "officially" integrated in libbfd
but the relevant parts here are standard a.out.
--
http://stud.uni-sb.de/~gufl0000/ 
mailto:guido@freemint.de


More information about the Gcc-bugs mailing list