crt{begin,end}.o and __do_global_{c,d}tors_aux

Jim Wilson wilson@tuliptree.org
Sat Jun 7 06:28:00 GMT 2003


Alan Modra wrote:
> Can someone tell me why on ELF system, crtbegin.o defines
> __do_global_dtors_aux and crtend.o defines __do_global_ctors_aux, and
> not the other way around?  Is it just so that the calls from _fini and
> _init respectively are to static functions?

There are some comments in crtstuff.c that explains some of this stuff. 
crtstuff.c is the file from which all crtbegin/crtend files are derived.

ctors and dtors have to be run in opposite orders.  ctors are run from 
the end (CTOR_END).  dtors are run from the beginning (DTOR_LIST). 
CTOR_END is defined in crtend, so do_global_ctors_aux is put in crtend.
Similarly, DTOR_LIST is defined in crtbegin, so do_global_dtors_aux is 
put in crtbegin.  This makes sure that there is no confusion about which 
CTOR_END/DTOR_LIST symbol is being used by these functions.  This is per 
my reading of the comments.  I'm not saying that things should work this 
way, that just seems to be the reason for it according to the comments.

> The reason I'm asking is that this arrangement means that the calls go
> from one end of the app to the other, which isn't ideal for targets
> having limited branch range.

Yes, that is annoying.  For IA-64, the first branches out of range are 
always the ones in crtbegin/crtend.

Jim





More information about the Gcc mailing list