dlsym

How to correctly assign a pointer returned by dlsym into a variable of function pointer type?

眉间皱痕 提交于 2019-11-29 11:47:18
问题 I am trying to use dlopen() and dlsym() in my code and compile it with gcc . Here is the first file. /* main.c */ #include <dlfcn.h> int main() { void *handle = dlopen("./foo.so", RTLD_NOW); if (handle) { void (*func)() = dlsym(handle, "func"); func(); } return 0; } Here is the second file. /* foo.c */ #include <stdio.h> void func() { printf("hello, world\n"); } Here is how I compile and run the code. $ gcc -std=c99 -pedantic -Wall -Wextra -shared -fPIC -o foo.so foo.c $ gcc -std=c99

dynamic_cast fails when used with dlopen/dlsym

眉间皱痕 提交于 2019-11-28 09:10:34
Intro Let me apologise upfront for the long question. It is as short as I could make it, which is, unfortunately, not very short. Setup I have defined two interfaces, A and B: class A // An interface { public: virtual ~A() {} virtual void whatever_A()=0; }; class B // Another interface { public: virtual ~B() {} virtual void whatever_B()=0; }; Then, I have a shared library "testc" constructing objects of class C, implementing both A and B, and then passing out pointers to their A-interface: class C: public A, public B { public: C(); ~C(); virtual void whatever_A(); virtual void whatever_B(); };

Alternatives to dlsym() and dlopen() in C++

大憨熊 提交于 2019-11-28 03:51:59
I have an application a part of which uses shared libraries. These libraries are linked at compile time. At Runtime the loader expects the shared object to be in the LD_LIBRARY_PATH , if not found the entire application crashes with error "unable to load shared libraries".Note that there is no guarantee that client would be having the library, in that case I want the application to leave a suitable error message also the independent part should work correctly. For this purpose I am using dlsym() and dlopen() to use the API in the shared library. The problem with this is if I have a lot of

What is Linux utility to mangle a C++ symbol name?

南楼画角 提交于 2019-11-27 23:24:50
I have c++filt command to demangle a symbol, what is the tool to do the opposite and mangle a symbol name? This would be useful if I were to want to call dlsym() on a mangled C++ function name. I'd rather not hard code the name mangling in the code since it could change over time due to new complier versions or new compiler brands being used or at present due to compiling for multiple platforms. Is there a programatic way to get the string that represents a C++ function at runtime so that the code is compiler independent? One way to possibly do this would be to call a utility at compile time

Function interposition in Linux without dlsym

陌路散爱 提交于 2019-11-27 14:10:45
I'm currently working on a project where I need to track the usage of several system calls and low-level functions like mmap , brk , sbrk . So far, I've been doing this using function interposition: I write a wrapper function with the same name as the function I'm replacing ( mmap for example), and I load it in a program by setting the LD_PRELOAD environment variable. I call the real function through a pointer that I load with dlsym . Unfortunately, one of the functions I want to wrap, sbrk , is used internally by dlsym , so the program crashes when I try to load the symbol. sbrk is not a

dynamic_cast fails when used with dlopen/dlsym

房东的猫 提交于 2019-11-27 02:42:53
问题 Intro Let me apologise upfront for the long question. It is as short as I could make it, which is, unfortunately, not very short. Setup I have defined two interfaces, A and B: class A // An interface { public: virtual ~A() {} virtual void whatever_A()=0; }; class B // Another interface { public: virtual ~B() {} virtual void whatever_B()=0; }; Then, I have a shared library "testc" constructing objects of class C, implementing both A and B, and then passing out pointers to their A-interface:

Function interposition in Linux without dlsym

拥有回忆 提交于 2019-11-26 16:39:21
问题 I'm currently working on a project where I need to track the usage of several system calls and low-level functions like mmap , brk , sbrk . So far, I've been doing this using function interposition: I write a wrapper function with the same name as the function I'm replacing ( mmap for example), and I load it in a program by setting the LD_PRELOAD environment variable. I call the real function through a pointer that I load with dlsym . Unfortunately, one of the functions I want to wrap, sbrk ,