Skip to content

Fix epicsFindSymbol() on OSX and WIN32

Heinz Junkes requested to merge github/fork/mdavidsaver/fix-epicsFindSymbol into 7.0

Created by: mdavidsaver

I've found that epicsFindSymbol() seems not to work on OSX and WIN32. The WIN32 is more complicated, and outside of my usual experience, so I'm going through a PR.

The fix on OSX is easy to explain. dlsym(0, name) errors because 0 is not a valid handle. On Linux (glibc anyway) RTDL_GLOBAL is zero, but on OSX this seems not to be the case, so the existing code probably never worked.

GetProcAddress(0, name), while undocumented, seems to behave like GetProcAddress(GetModuleHandle(NULL), name) and only searches for symbols exported from the executable file. This may be sufficient in some static linking situations, but isn't very useful in DLL builds.

There does not seem to be a direct analog to RTDL_GLOBAL for WIN32, but the same effect can be achieved with EnumProcessModules() to enumerate all loaded modules, this includes the executable file, and all DLLs loaded implicitly and explicitly.

I've chosen to use the K32EnumProcessModules() variant, which doesn't require linking against psapi.dll, but requires Windows 7 or later. This could be relaxed if it is desirable.

I'm also included a change so that the WIN32 version of epicsLoadError() uses thread local storage.

Merge request reports

Loading