delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/08/13/01:53:51

From: khan AT xraylith DOT wisc DOT edu (Mumit Khan)
Subject: Re: Global constructors in DLL ?
13 Aug 1998 01:53:51 -0700 :
Message-ID: <9808121659.AA04752.cygnus.gnu-win32@modi.xraylith.wisc.edu>
References: <004c01bdc58c$8a3ba560$fa173185 AT gbird0 DOT fu DOT is DOT saga-u DOT ac DOT jp>
To: "Colin Peters" <colin AT fu DOT is DOT saga-u DOT ac DOT jp>
Cc: "Edwin Essenius" <edwin AT bitbybit-is DOT nl>,
"GNU-win32" <gnu-win32 AT cygnus DOT com>, scox AT cygnus DOT com,
cygwin32-developers AT cygnus DOT com

"Colin Peters" <colin AT fu DOT is DOT saga-u DOT ac DOT jp> writes:
> Edwin Essenius <edwin AT bitbybit-is DOT nl> wrote:
> 
> >I am using cywin B19.2, with the sergey patches an trying to build a
> >C++ DLL. Except for the Global constructors all seems to work.
> >Global variables in the DLL are initialized, but the global constructors
> >are not called when it is declared in a DLL.
> 
> 
> If you have written or obtained your own startup code for the DLL (what the
> entry point is set to on the link line, or DllMain possibly) then you should
> check that you are calling __main in the startup code on DLL_PROCESS_ATTACH.
> __do_global_dtors should be called on DLL_PROCESS_DETACH. Also make sure to
> disable atexit inside the DLL (mainly for the call to __main) or the main
> process may attempt to call your destructors when it exits, which could be
> after the DLL has been detached.

Colin's right on the mark as to the cause of the global constructors not
working correctly. Here's the change to gcc sources that causes this
problem:

  Mon Mar 30 13:11:05 1998  Stan Cox  <scox AT cygnus DOT com>

	* libgcc2.c: (__main, __do_global_dtors, __do_global_ctors):
	For __CYGWIN32__ use the versions in winsup/dcrt0.cc.

The problem is that the current default DLL startup in winsup calls 
*its* definition of __do_global_ctors, which doesn't call GCC generated 
C++ global initializers (cf: __CTOR__LIST__ and __DTOR_LIST__), and hence 
your C++ globals aren't being initialized.

I get around it by using my own DLL startup, but that's a path I won't
recommend since you might be missing things that the winsup DLL startup
does beyond just initializing globals.

Stan, could you please take a look at this problem? The easiest way is to
simply copy the libgcc2.c version of DO_GLOBAL_CTORS_BODY (gbl-ctors.h)
into winsup's __do_global_ctors (and similarly for __do_global_dtors).

Regards,
Mumit

ps: I'm copying this message to cygwin32-developers list as well, since this 
has the potential to be another "cygwin32 comes with broken C++" story.

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019