I'm developing some C++ modules for an engineering software called MATLAB.
I have a very annoying problem that prevents C++ exceptions to work properly.
This is the setting:
0) GNU/Linux 2.6, gcc 4.0.2, ld 2.16.1
1) Modules (MEX-files) are loaded dynamically by MATLAB by dlopen()
2) All MEX-files include a common dynamic shared library (libvl.so)
3) The shared library throws instances of a certain class VL::Exception
which are (or should) be intercepted by the MEX-files.
Both the MEX-files and libvl.so correctly export typeinfo symbols for
(a MEX file)
nm -C -D mex/hwcaminfo.mexglx | grep Exception
000048f0 V typeinfo for VL::Exception
00002fbc V typeinfo name for VL::Exception
(the shared library libvl.so)
nm -C -D lib/libvl.so | grep Exception
000104c0 V typeinfo for VL::Exception
000104c8 V typeinfo name for VL::Exception
- when the FIRST MEX-file is loaded (by MATLAB calling dlopen()),
libvl.so is loaded as well
- the weak typeinfo symbols for VL::Exception are correctly merged
- throws of VL::Exception from libvl.so are correctly intercepted by
the MEX file
- when a SECOND MEX-file is loaded (dlopen() again) libvl.so is already loaded
- the weak typeinfo symbol for VL::Exception in the second MEX-file is
NOT merged (why?)
- throws of VL::Esception from libvl.so terminate() the second MEX-file
I managed to let it work as follows:
- I set LD_PRELOAD=libvl.so BEFORE launching MATLAB
- in which case all weak typeinfo symbols get merged consistently for
all calls of dlopen() as libvl.so gets loaded first
This issue apparently affects only WEAK symbols. Any strong symbol
defined in libvl.so is consistently merged across different MEX-files,
no matter which is the pattern of dlopen() calls, implying that symbols
in libvl.so have GLOBAL visibility once the library is loaded.
Is there any way to get the correct behavior WITHOUT using LD_PRELOAD ?