This is the mail archive of the gcc-bugs@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]

[Bug c++/11137] New: Linux shared library constructors not called unless there's one global object


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11137

           Summary: Linux shared library constructors not called unless
                    there's one global object
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P2
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jcalcote@novell.com
                CC: gcc-bugs@gcc.gnu.org

Linux shared library constructor and destructor routines defined by the use of 
the __attribute__((constructor)) and __attribute__((destructor)) keywords are 
not called in C++ programs when the library is loaded/unloaded unless there is 
at least one global object in the module. Global constructors and destructors 
appears to work correctly in C programs. Only C++ programs exhibit this 
strange behaviour. I use the following hack to get my constructors to be 
called - I place this statement in my init/exit code:

static struct Stupid { Stupid() {} ~Stupid() {} } stupid;

which causes Stupid() to be executed at library load time, and then adds a 
call to my __attribute__((constructor)). 

Having disassembled the code to my module by using objdump, I can see that the 
routine that calls global object constructors is not injected unless there's 
at least one global object. Since calls to __attribute__((constructor)) 
routines are placed at the end of this compiler generated code, no such calls 
will be made if this code is not generated. The cause of generation of this 
routine appears to be the existance of a global C++ object, but not the 
existence of __attribute__((constuctor)) routines.


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