4

I am porting a DLL from Windows to Linux (OS X actually). I used this StackOverflow article to do that change.

i.e. I've ported the Windows "bool DllMain()" to the Linux way:

__attribute__((constructor)) void dllLoad();

__attribute__((destructor)) void dllUnload();

... but both are void return types. I need to be able to do the same as Windows and return FALSE if a condition isn't met in the constructor so that the dlopen() fails and the .so doesn't load.

How do I get the calling dlopen() to fail?

Community
  • 1
  • 1
Edward
  • 71
  • 1
  • 5

2 Answers2

3

The answer is that it isn't possible. As stated above you can't error out in the constructor - be it an exception or exit()

Edward
  • 71
  • 1
  • 5
1

You need to approach this differently.

If you are dynamically loading a library, than you must also be using GetProcAddress() and dlsym() to actually do anything with it. dlsym() is your path forward here.

You clearly control the plugin's code, since otherwise you couldn't even add these APIs to it. So, all your 'dllmain' on either platform needs to do is set some global 'is valid' state information. You then simply call a known API like "ModuleIsValid()" whose entire job is to simply read that state info & return true/false. If it returns false, you close the library and report failure.

user3726672
  • 187
  • 2