Guest User

Untitled

a guest
Aug 25th, 2019
68
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ for file in *.cpp *.h *.bat ; do echo "--------------------- $file -----------------" ; cat "$file" ; echo "" ; done
  2. --------------------- A.cpp -----------------
  3. #include "stuff.h"
  4.  
  5. printFunc_t print=nullptr;
  6.  
  7. extern "C" void loadA( printFunc_t func ) {
  8.         print = func;
  9.     (*print)("A: loading libB\n");
  10.  
  11.         auto B = dlopen("./libB.dll", RTLD_LAZY);
  12.     if ( CheckDlError() != 0 )
  13.                 return;
  14.  
  15.     (*print)("A: getting libB::loadB()\n");
  16.     auto loadB = reinterpret_cast< loaderFunc_t >( dlsym( B, "loadB" ) );
  17.  
  18.     if ( CheckDlError() != 0 )
  19.                 return;
  20.  
  21.         (*print)("A: calling libB::loadB()\n");
  22.     (*loadB)( func );
  23.  
  24.         (*print)("A: closing libB\n");
  25.         dlclose(B);
  26.         int a = 0;
  27. }
  28.  
  29. --------------------- B.cpp -----------------
  30. #include "stuff.h"
  31.  
  32. extern "C" void loadB( printFunc_t func ) {
  33.     (*func)("B: I am loaded\n");
  34. }
  35.  
  36. --------------------- stub.cpp -----------------
  37. #include "stuff.h"
  38.  
  39. void Print( const char * msg ) {
  40.         printf("%s", msg);
  41.         fflush(stdout);
  42. }
  43.  
  44. int main() {
  45.     Print("stub: loading libA\n");
  46.         auto A = dlopen("./libA.dll", RTLD_LAZY);
  47.     if ( CheckDlError() != 0 )
  48.                 return -1;
  49.  
  50.     Print("stub: getting libA::loadA()\n");
  51.     auto loadA = reinterpret_cast< loaderFunc_t >( dlsym( A, "loadA" ) );
  52.     if ( CheckDlError() != 0 )
  53.                 return -1;
  54.  
  55.         Print("stub: calling libA::loadA()\n");
  56.         (*loadA)( &Print );
  57.  
  58.         Print("stub: closing libA\n");
  59.         dlclose(A);
  60. }
  61.  
  62. --------------------- stuff.h -----------------
  63. #include <cstdio>
  64. #include <cassert>
  65.  
  66. #ifdef _WIN32
  67.     #include "windows.h"
  68.     #define dlopen( file, flag ) LoadLibrary( file )
  69.     #define dlsym( module, resource ) reinterpret_cast<void*>( GetProcAddress( module, resource ) )
  70.     #define dlclose( module ) FreeLibrary( module )
  71. #elif defined(__linux)
  72.     #include <dlfcn.h>
  73. #endif
  74.  
  75. bool CheckDlError( void );
  76. inline bool CheckDlError( void ) {
  77.     #ifdef _WIN32
  78.         return GetLastError() == 0;
  79.     #elif defined(__linux)
  80.         const char *error = dlerror();
  81.         if ( error )  {
  82.             printf("Error linking library: %s\n", error);
  83.             return true;
  84.         }
  85.     #endif
  86.     return false;
  87. }
  88.  
  89. typedef void ( *printFunc_t )( const char* msg );
  90. typedef void ( *loaderFunc_t )( printFunc_t );
  91. typedef void ( *voidFunc_t )( void );
  92.  
  93. --------------------- build.bat -----------------
  94. rm A.dll B.dll stub.exe 2>>/dev/null
  95.  
  96. g++ B.cpp -shared -g -ggdb -Wl,--export-all-symbols -o libB.dll
  97. g++ A.cpp -shared -g -ggdb -Wl,--export-all-symbols -o libA.dll
  98. g++ stub.cpp -g -ggdb -Wl,--export-all-symbols -o stub
RAW Paste Data