views:

363

answers:

1

Does anyone know why a library initialized within dlopen() would initialize a static variable owned by the main program. Both the main program and shared library have a copy of the static variable, but for some reason the shared library re-initializes the main program's copy of the static variable and destructs it, causing a segfault when the main program attempts to destruct it.

Is this a case of bad name mangling in the symbol table?

A: 

This is a case where the runtime linker only wants a single active copy of a symbol in a process. If both a shared object and the executable have a copy of the symbol, the runtime linker will resolve all references to one of those.

What you can do to solve this problem is to use symbol reduction using the version command of the link editor when building the shared object. Make sure the symbol for the static variable is not global and you will get the behavior you are looking for.

R Samuel Klatchko
Hrm, I seem to be having a problem implementing the version script and passing it to the linker. It is complaining about a syntax error. Here is the script file I used: "{ global: local: *; }" Any ideas? I looked for examples but they all seem to fail the syntax check. Or is it better to put the __asm__ directive directly in the source?
Paul
@Paul - my scripts look like `VERSION { { global: ...; local *; }; }` and then I just use the filename of that file on linker command line.
R Samuel Klatchko
@Samuel - Thanks a bunch, after getting the version script completed and adding a couple of symbols to the global context it now works!
Paul

related questions