C / C ++ Linux equivalent "bool DllMain ()" - but I need to return dlopen () failure

I am transferring DLLs from Windows to Linux (actually on OS X). I have used this https://stackoverflow.com/a/166272/ ... to make these changes.

i.e. I ported Windows "bool DllMain ()" to the Linux path:

__attribute__((constructor)) void dllLoad(); __attribute__((destructor)) void dllUnload(); 

... but both types are invalid. I need to be able to do the same thing as Windows and return FALSE if the condition is not met in the constructor, so dlopen () fails and .so does not load.

How can I call dlopen () to fail?

+4
source share
2 answers

The answer is that this is not possible. As stated above, you cannot make mistakes in the constructor - whether it be an exception or an exit ()

+3
source

You need to approach differently.

If you are dynamically loading the library, you should also use GetProcAddress () and dlsym () to actually do anything with it. dlsym () is your way forward.

You clearly control the plugin code, because otherwise you could not even add these APIs to it. Thus, all of your dllmain on any of these platforms should set some global "valid" status information. Then you simply call the well-known API like "ModuleIsValid ()", whose task is to simply read this status information and return true / false. If it returns false, you close the library and refuse the report.

+1
source

All Articles