This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Global constructors


Tiago Stein D'Agostini wrote:
But I need to make it to call the global constructors (a call to it just before call to main). Unfortunatelly I am not able to put a call to __do_global_ctors_aux (LD indicates an undefined reference) even the __do_global_ctors_aux being there at the object file.

You aren't supposed to call this function. You are only supposed to call __do_global_ctors. __do_global_ctors then calls __do_global_ctors_aux.


There are multiple ways to support static constructors. You can configure this for you port which ever way you would like it. The best bet is to probably pick an existing target and model your port after it.

For a normal ELF target, the .init section contains code that is executed by the program loader when the code is loaded into memory, before calling main. The .fini section contains code that is executed by the program loader after the program exits. The addresses of static constructor/destructor functions are put in the .ctors/.dtors sections respectively, and the .init/.fini sections just have code to call each function in turn until it gets to the end of the section. You need crti.o/crtn.o files that provide the function prologues/epilogues for the .init/.fini sections. Gcc provides the rest of the code that goes in the .init/.fini sections via crtbegin.o/crtend.o.

If your loader doesn't automatically run the .init/.fini sections, which is common in embedded systems, then you can put code in the crt0.o file to run them. In your crti.o file, put labels at the start of the .init/.fini sections. You can then call the function in the .init section before calling main. You can then call atexit to register the function in the .fini section to be called when the program exits.

If this seems like too much work, you can tell gcc not to use .init/.fini sections, in which case you need an explicit call to __do_global_ctors someplace. You can do this yourself in the crt0.o startup code, or you can get gcc to emit a call to __main in the main function which does it for you.
--
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]