Services and Modeling for Embedded Software Development
Embecosm divider strip
Prev  Next

5.2.5.  Constructor and Destructor Handling

GCC may require constructors to be initialized at start up and destructors to be called on exit. This behavior is captured in the GCC functions __do_global_ctors and __do_global_dtors. There is some complexity associated with this functionality, since there may be separate lists for the main code and shared libraries that are dynamically loaded.

It is usual to wrap this functionality in two functions, init and fini, which are placed in their own sections, .init and .fini. The .init section is loaded before all other text sections and the .fini section after all other text sections.

The start up code should call init to handle any constructors.

        l.jal   init

The fini function is passed to the library function _atexit to ensure it is called on a normal exit.

        l.movhi r3,hi(fini)
        l.jal   _atexit
        l.ori   r3,r3,lo(fini)          /* Delay slot */
Embecosm divider strip