This is the mail archive of the gcc-help@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: dlopen() not initializing modules?


There is the -init and -fini flags you can use with the linker. It
should be able to run specific functions when the shared object is
loaded, you should be able to initialize and destroy your objects that
way. I think you have to use it with the -Xlinker option (such as
-Xlinker -init -Xlinker init_function_name).

Gokhan


"Charles Y. Kim" wrote:
> 
> So does anyone know if there's a magic linker flag to initialize static
> globals in shared objects using GCC3.0.4 under HP-UX11?
> 
> Take the following super simple case:
> 
>         class myClass {
>         public:
>                 int x;
>                 myClass() {
>                         x = 5;
>                 }
>         };
> 
>         myClass test;
> 
>         extern "C" void run()
>         {
>                 cout << test.x << endl;
>         }
> 
> I dlopen() the file that contains this code, use dlsym to get a pointer to
> run(), and then run the function.  The myClass constructor is never run so
> when I print out test.x I get 0 instead of 5!
> 
> How do I fix this?
> 
> Thanks.
> 
> - Charles
> 
>  -----Original Message-----
> From:   Charles Y. Kim [mailto:ckim@telcontar.com]
> Sent:   Monday, June 03, 2002 10:32 AM
> To:     'gcc-help@gcc.gnu.org'
> Subject:        dlopen() not initializing modules?
> 
> Hi all,
> 
> When I dlopen() a shared object, it looks like the module initializations
> aren't being performed and the constructors for globals aren't getting
> called (I verified this with a small test program).
> 
> Does anyone know how I might fix this?
> 
> Here's my test code:
> #include <map>
> #include <string>
> #include <stdio.h>
> #include <iostream.h>
> 
> class myMap {
> public:
>   void add(int x, int y) {
>     m[x] = y;
>   }
>   myMap() {
>     puts("Constructor run");
>   }
> private:
>   std::map<int, int> m;
> };
> 
> //myMap m2;
> 
> extern "C" int run() {
>   myMap m2;
>   m2.add(1, 2);
> }
> 
> This shared object is being opened like this:
> int main() {
>   void *handle;
>   void (*function)();
>   handle = dlopen("./maptest.so", RTLD_LAZY);
>   char *err;
>   err = dlerror();
>   if(err != NULL)
>     cout << "File Load failure: " << err << endl;
>   function = (void(*)())dlsym(handle, "run");
>   err = dlerror();
>   if(err != NULL)
>     cout << "Load failure: " << err << endl;
>   else {
>     for (int i = 0; i < 1000000000; i++) {
>       cout << "Count: " << i << endl;
>       function();
>     }
>   }
> }
> 
> I'm dlopen()ing this file built as a shared object.  When I declare a myMap
> in global, the constructor is never run, and I end up with a segmentation
> fault/core dump.
> 
> Anyone have any ideas?  I'm running GCC3.0.4 on HP-UX11i.
> 
> Thank in advance.
> 
> - Charles

-- 
Gokhan Kisacikoglu    				  Centropolis Effects, LLC
Senior Technical Director			  10950 W Washington Blvd
kisa@centropolisfx.com  phone: 310.204.7300 x263  Culver City, CA, 90232
	"The deadlines on the calender are closer than they appear."


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