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]

dlopen/dlclose with static variables


Hi,

I have problems with static variables in shared libraries. Have a look at the source at the end of this mail. I build it with

g++ -ldl -o main main.cxx
gcc -shared -o libso.so so.cxx

with a gcc 3.0.3 or with a gcc 2.95.2. Letting it run gives the following output:

opening shared library
calling symbol 0x40016920
Ctor 0x40017c00
leaving main
Segmentation fault (core dumped)

When I move the static object to the global scope (out of foo() ), the object gets only destructed once within dlclose(). The difference seems to be, that in the case shown below, a call to atexit() is generated by the compiler while in the latter case this is not done (proving with nm libso.so | grep atexit). I would consider the first behaviour to be bug.

Comments ? Any way to workaround this with different compiler/linker switches ?

Joerg Budischewski
[Open|Star]Office developer

--

so.cxx:
#include <stdio.h>

class A
{
public:
A(){fprintf( stderr, "Ctor %p\n" , this );}
~A(){fprintf( stderr, "Dtor %p\n" , this );}
};

extern "C" void foo()
{
static A a;
}


main.cxx:
#include <stdio.h>
#include <dlfcn.h>

typedef void ( * func ) ( void );

int main( int argc , char *argv[] )
{
void * p;
fprintf( stderr, "opening shared library\n" );
p = dlopen( "libso.so" , RTLD_NOW );
if( p )
{
func f = (func) dlsym( p , "foo" );
if( f )
{
fprintf( stderr, "calling symbol %p\n" , f );
f();
}
dlclose( p );
fprintf( stderr, "leaving main\n" );
}
else
{
fprintf( stderr, "error: %s\n" , dlerror() );
}
return 0;
}


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