1

I studied and tested the following URLS closely, Calling function in shared library (Linux) get Segmentation Fault as well as https://git.xeserv.us/Quora/dlsym-example/src/master/testprog.c. My question is how to map dlsym to a non-static C++ member function similar to this C function example. If I substitute C function syntax for a non-static C++ member function , I get a no-op.

   void *lib;              /* void pointer named lib */
   void (*sayHello)(void); /* void pointer named sayHello */
   lib = dlopen("helloWorld.so", RTLD_NOW);

   *(void **) (&sayHello) = dlsym(lib,"_ZN19CDataTransferServer10InitializeEi13CC_SAMPLETYPEjjj");

Any help is appreciated.

Community
  • 1
  • 1
Frank
  • 1,406
  • 2
  • 16
  • 42
  • 2
    C++ has no ABI (Application Binary Interface) and linkage is compiler (even compiler version) dependent. –  Jan 04 '16 at 18:16
  • @Basilev, The compiler tag is g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4. – Frank Jan 04 '16 at 18:28
  • @Dieter Lücking, Is C++ name mangling relevant to this purpose or not? – Frank Jan 04 '16 at 18:30
  • @richpe, mangling is absolutely relevant. Please edit your post to include your failed C++ attempt, instead of irrelevant C one. – Basilevs Jan 04 '16 at 18:33
  • Related: [Wrong arguments position in function imported with dlsym](http://stackoverflow.com/q/16820055) – Basilevs Jan 04 '16 at 18:48
  • Possible duplicate of [What is Linux utility to mangle a C++ symbol name?](http://stackoverflow.com/questions/11335624/what-is-linux-utility-to-mangle-a-c-symbol-name) – Basilevs Jan 04 '16 at 18:50
  • @richpe your latest edit intrduces obviously incorrect cast of function pointer. I can derive from mangled name of function you use, that it accepts arguments (at least two) and you are trying to assign it to a pointer to function that accepts no arguments. – Basilevs Jan 04 '16 at 19:59

1 Answers1

1

Consider loading a method by its mangled name extracted with

readelf -Ws libmy.so

or

nm -g libmy.o
Basilevs
  • 22,440
  • 15
  • 57
  • 102
  • Thank you for the great answer. Do I need to include any header files which resolve the mangled names such as:_ZN19CDataTransferServer10InitializeEi13CC_SAMPLETYPEjjj? – Frank Jan 04 '16 at 18:57
  • You will need a definition of DataTransferServer to have a typed pointer to its method. – Basilevs Jan 04 '16 at 18:58
  • I just included DataServer.h and compiled with g++ -g -DLINUX -I /home/vend/developmentMono/SmartCamXi_Hybrid/Include test.cpp -ldl and got the following compiler errors: test.cpp:33:48: error: ‘_ZN19CDataTransferServer10InitializeEi13CC_SAMPLETYPEjjj’ was not declared in this scope *(bool **)(&sayHello) = (bool *)dlsym(handle,_ZN19CDataTransferServer10InitializeEi13CC_SAMPLETYPEjjj); Please suggest a way to workaround these errors. Thanks. – Frank Jan 04 '16 at 19:16
  • I was just informed you cannot access a c++ class by using dlopen and dlsym since they are C functions. Instead a better way is described in this URL, http://stackoverflow.com/questions/496664/c-dynamic-shared-library-on-linux. Thank you for all of your help. – Frank Jan 04 '16 at 19:48
  • You should provide a string literal, not a symbol as argument of dlsym. every C++ method is a C function with a mangled name (at least in GCC world). – Basilevs Jan 04 '16 at 19:55