dlopen

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

shared object can't find symbols in main binary, C++

拟墨画扇 提交于 2019-11-29 08:11:42
问题 I'm experimenting with making a kind of plugin architecture for a program I wrote, and at my first attempt I'm having a problem. Is it possible to access symbols from the main executable from within the shared object? I thought the following would be fine: testlib.cpp: void foo(); void bar() __attribute__((constructor)); void bar(){ foo(); } testexe.cpp: #include <iostream> #include <dlfcn.h> using namespace std; void foo() { cout << "dynamic library loaded" << endl; } int main() { cout <<

dlopen - Undefined symbol error

空扰寡人 提交于 2019-11-29 07:07:16
I'm using dlopen to load a shared library at run time dlopen("SharedLibarary1.so", RTLD_NOW | RTLD_GLOBAL); In that shared object I refer to a const char* defined in another shared library "SharedLibarary2.so". The Executable, and both the libraries are built using -rdynamic. But I still get the run time error when using dlopen: "/usr/lib/SharedLibarary1.so: undefined symbol" and points to the mangled const char* has the undefined symbol. Whith GDB "info share" I can see that the second library is not loaded at the point of the error. How ever that problem goes away if I do a dlopen on the

Using dlopen() on an executable

為{幸葍}努か 提交于 2019-11-29 03:42:01
I need to call a function from another program. If the other program were a library, I could simply use dlopen and dlsym to get a handle to the function. Unfortunately, the other program is a Unix Executable, and building it as a library is not an option. Trying dlopen() on the executable gives this error message: dlopen([...]/testprogram, 1): no suitable image found. Did find: [...]/testprogram: can't map This isn't surprising, as dlopen is meant for use with libraries, not executables. Is there any way to get dlopen and dlsym to work with executables? If not, is there an alternative way of

Returning a shared library symbol table

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 02:29:39
问题 For instance: void* sdl_library = dlopen("libSDL.so", RTLD_LAZY); void* initializer = dlsym(sdl_library,"SDL_Init"); Assuming no errors, initializer will point to the function SD_Init in the shared library libSDK.so. However this requires knowing the symbol "SDL_Init" exists. Is it possibly to query a library for all its symbols? Eg, in this case it would return SDL_Init, the function pointer, and any other symbols exported by libSDL.so. 回答1: There is no libc function to do that. However, you

Get loaded address of a ELF binary, dlopen is not working as expected

眉间皱痕 提交于 2019-11-29 01:36:21
问题 I'm trying to get the loaded address of an ELF binary, but dlopen doesn't work as expected: void *elf = (char *)dlopen (0, RTLD_NOW); printf ("%p\n", elf); sleep (100); It prints 0xb772d918 , but from what /proc/1510/maps tells, it doesn't point to the loaded address of the dlfn binary, but the ld-2.15.so , 08048000-08049000 r-xp 00000000 fc:00 1379 /root/dlfn 08049000-0804a000 r--p 00000000 fc:00 1379 /root/dlfn 0804a000-0804b000 rw-p 00001000 fc:00 1379 /root/dlfn b7550000-b7552000 rw-p

How would a loaded library function call a symbol in the main application?

a 夏天 提交于 2019-11-28 23:15:38
问题 When loaded a shared library is opened via the function dlopen() , is there a way for it to call functions in main program? 回答1: Code of dlo.c (the lib): #include <stdio.h> // function is defined in main program void callb(void); void test(void) { printf("here, in lib\n"); callb(); } Compile with gcc -shared -olibdlo.so dlo.c Here the code of the main program (copied from dlopen manpage, and adjusted): #include <stdio.h> #include <stdlib.h> #include <dlfcn.h> void callb(void) { printf("here,

Multiple instances of singleton across shared libraries on Linux

扶醉桌前 提交于 2019-11-28 15:59:21
My question, as the title mentioned, is obvious, and I describe the scenario in details. There is a class named singleton implemented by singleton pattern as following, in file singleton.h: /* * singleton.h * * Created on: 2011-12-24 * Author: bourneli */ #ifndef SINGLETON_H_ #define SINGLETON_H_ class singleton { private: singleton() {num = -1;} static singleton* pInstance; public: static singleton& instance() { if (NULL == pInstance) { pInstance = new singleton(); } return *pInstance; } public: int num; }; singleton* singleton::pInstance = NULL; #endif /* SINGLETON_H_ */ then, there is a

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(); };

linux dlopen: can a library be “notified” when it is loaded?

大城市里の小女人 提交于 2019-11-28 08:27:12
Is there a way for a shared library to be "notified" when it is loaded? In other words, let's say I use dlopen on a shared library, is there a function that is automatically called (if present) on the shared library (e.g. main?) Libraries should export initialization and cleanup routines using the gcc __attribute__((constructor)) and __attribute__((destructor)) function attributes. See the gcc info pages for information on these. Constructor routines are executed before dlopen returns (or before main() is started if the library is loaded at load time). Destructor routines are executed before