From e52b6b54728afa0a469a427ffd4eed3c4b5aa2de Mon Sep 17 00:00:00 2001 From: malaterre Date: Mon, 19 Sep 2005 18:54:10 +0000 Subject: [PATCH] ENH: Adding an exe that seg fault on AMD64/gcc. --- Testing/CMakeLists.txt | 14 +++++++ Testing/dynmodule.cxx | 39 ++++++++++++++++++ Testing/loadmodule.cxx | 93 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 Testing/dynmodule.cxx create mode 100644 Testing/loadmodule.cxx diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt index 47f897bf..994e37be 100644 --- a/Testing/CMakeLists.txt +++ b/Testing/CMakeLists.txt @@ -181,3 +181,17 @@ FILE(WRITE "${GDCM_BINARY_DIR}/gdcmDataImages.h" FILE(WRITE "${GDCM_BINARY_DIR}/gdcmDataSeqImages.h" "const char * const gdcmDataSeqImages[] = { ${GDCM_DATA_SEQ_IMAGES}\n0 };\n" ) + +#----------------------------------------------------------------------------- +# Trying to solve a bizarre problem when loading shared lib if gdcm is static +# Therefore I disable the test when building SHAREDLIBS +IF(UNIX AND NOT GDCM_BUILD_SHARED_LIBS) + ADD_LIBRARY(dynmodule MODULE dynmodule.cxx) + TARGET_LINK_LIBRARIES(dynmodule gdcm) + ADD_EXECUTABLE(loadmodule loadmodule.cxx ) + TARGET_LINK_LIBRARIES(loadmodule gdcm -ldl) + ADD_TEST(loadmodule ${CXX_TEST_PATH}/loadmodule) +ENDIF(UNIX AND NOT GDCM_BUILD_SHARED_LIBS) + + + diff --git a/Testing/dynmodule.cxx b/Testing/dynmodule.cxx new file mode 100644 index 00000000..2c9a664d --- /dev/null +++ b/Testing/dynmodule.cxx @@ -0,0 +1,39 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: dynmodule.cxx,v $ + Language: C++ + Date: $Date: 2005/09/19 18:54:10 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +#include "gdcmFile.h" + +#ifdef _WIN32 +# define __ELX_DLLEXPORT __declspec(dllexport) +#else +# define __ELX_DLLEXPORT +#endif + +extern "C" __ELX_DLLEXPORT void afunc(void) +{ + std::cerr << "Hello, we are in the dll-function afunc!" << std::endl; + + // Create an instance of a gdcm class. This makes sure + // that the problematic gdcm-library is linked. + gdcm::File *file = new gdcm::File(); + + std::cerr << "End of function afunc" << std::endl; + +} + + diff --git a/Testing/loadmodule.cxx b/Testing/loadmodule.cxx new file mode 100644 index 00000000..2cf7de47 --- /dev/null +++ b/Testing/loadmodule.cxx @@ -0,0 +1,93 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: loadmodule.cxx,v $ + Language: C++ + Date: $Date: 2005/09/19 18:54:10 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +/* For some bizarre reason global symbol, mainly std::string + * are creating seg fault when loading a shared lib, build on top + * of a static gdcm lib */ + +#include "gdcmFile.h" +#include + +int main(int, char* []) +{ + //typedef itk::DynamicLoader dl; + typedef void* lh; + + // the type of function stored in the dynamic lib + typedef void (*fnptr)(void); + + //This function gives a segmentation fault. + //std::cerr << "Last error before opening: " << dl::LastError() << std::endl; + + //Fill in here /libadll.so" + std::string libname = GDCM_LIBRARY_OUTPUT_PATH "/libdynmodule.so"; + + //open the dynamic lib + //lh handle = dl::OpenLibrary( libname.c_str() ); + lh handle = dlopen(libname.c_str(), RTLD_LAZY); + std::cerr << "address of handle: " << handle << std::endl; + if (!handle) + { + std::cerr << "Ooops cannot open lib:" << libname << std::endl; + return 1; + } + + //std::cerr << "Error after opening: " << dl::LastError() << std::endl; + + + std::cerr << "Getting function address from lib" << std::endl; + void * aptr = dlsym(handle, "afunc"); + + if (aptr == 0) + { + std::cerr << "error in finding function" << std::endl; + //const char *er = dlerror(); + //std::cerr << "dl says: " << dl::LastError() << std::endl; + + std::cerr << "Closing libs" << std::endl; + //dl::CloseLibrary(handle); + dlclose(handle); + //std::cerr << "dl says: " << dl::LastError() << std::endl; + + std::cerr << "Exiting" << std::endl; + return 1; + } + + + std::cerr << "Casting function address from lib" << std::endl; + fnptr afuncptr = (fnptr)(aptr); + + std::cerr << "Calling function from lib" << std::endl; + afuncptr(); + + // Create an instance of the GDCMImageIO class. This makes sure + // that the problematic gdcm-library is linked. + //itk::GDCMImageIO::Pointer gdcmio = itk::GDCMImageIO::New(); + gdcm::File *file = new gdcm::File(); + + std::cerr << "Closing libs" << std::endl; + //dl::CloseLibrary(handle); + dlclose(handle); + + //std::cerr << "dl says: " << dl::LastError() << std::endl; + + std::cerr << "Exiting" << std::endl; + return 0; + +} + + -- 2.45.1