dlopen

dlclose() doesn't work with factory function & complex static in function?

此生再无相见时 提交于 2019-11-30 17:04:41
I'm making a simple plugin framework in which I'd like to be able to dlopen() a shared library (i.e. plugin), inspect and use whatever factory functions is provides and eventually dlclose() it, leaving no trace. My factory system is trivial, with a single exported function that returns a pointer to a common Base class. To check the plugin has been unloaded properly, I have a static object whose destructor sets a bool from the main program. Here's the main program: // dltest.cpp follows. Compile with g++ -std=c++0x dltest.cpp -o dltest -ldl #include <dlfcn.h> #include <iostream> using namespace

dlsym/dlopen with runtime arguments

懵懂的女人 提交于 2019-11-30 13:04:40
问题 I am trying to do something like the following enum types {None, Bool, Short, Char, Integer, Double, Long, Ptr}; int main(int argc, char ** args) { enum types params[10] = {0}; void* triangle = dlopen("./foo.so", RTLD_LAZY); void * fun = dlsym(triangle, ars[1]); <<pseudo code>> } Where pseudo code is something like fun = {} for param in params: if param == None: fun += void if param == Bool: fun += Boolean if param == Integer: fun += int ... returnVal = fun.pop() funSignature = returnval + "

Cannot load any more object with static TLS

南楼画角 提交于 2019-11-30 09:00:07
I have an application that use dlopen() to load additional modules. The application and modules are built on Ubuntu 12.04 x86_64 using gcc 4.6 but for i386 arch. The binaries are then copied to another machine with exactly same OS and work fine. However if they are copied to Ubuntu 12.04 i386 then some (but not all) modules fail to load with the following message: dlopen: cannot load any more object with static TLS I would suspect that this is caused by the usage of __thread variables. However such variables are not used in the loaded modules - only in the loader module itself. Can someone

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

你。 提交于 2019-11-30 08:12:46
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 -pedantic -Wall -Wextra -ldl -o main main.c main.c: In function ‘main’: main.c:10:26: warning: ISO C forbids

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

 ̄綄美尐妖づ 提交于 2019-11-30 06:55:52
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 << "attempting to load" << endl; void* ret = dlopen("./testlib.so", RTLD_LAZY); if(ret == NULL) cout <<

Returning a shared library symbol table

巧了我就是萌 提交于 2019-11-30 04:58:03
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. jweyrich There is no libc function to do that. However, you can write one yourself (though the code is somewhat involved). On Linux, dlopen() in fact returns

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

前提是你 提交于 2019-11-30 04:13:20
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 00000000 00:00 0 b7552000-b76f5000 r-xp 00000000 fc:00 9275 /lib/i386-linux-gnu/libc-2.15.so b76f5000

dlclose doesn't really unload shared object, no matter how many times it is called

为君一笑 提交于 2019-11-30 01:54:34
问题 My program uses dlopen to load a shared object and later dlclose to unload it. Sometimes this shared object is loaded once again. I noticed static variables are not re-initialized (something which is crucial to my program) so I added a test ( dlopen with RTLD_NOLOAD ) after dlclose to see if the library is really unloaded. Sure enough, it was still in memory. I then tried calling dlclose repeatedly until the library is really unloaded, but what I got was an infinite loop. This is the code I'm

Get functions names in a shared library programatically

喜欢而已 提交于 2019-11-29 13:19:27
Can I get list of all functions names from a shared library (Linux only) programmatically when I am using dl_open() ? I want something like this: std::vector<std::string> list_all_functions(void *dl) { //... what can I do here? } int main() { void * dl = dl_open("./mylib.so", RTLD_NOW); auto functions = list_all_functions(dl); //... dl_close(dl); return 0; } Example library (mylib.so) Header (.h): extern "C" { int sum (int a, int b); } Source (.c): int sum (int a, int b) { return a + b; } Dirty hack that I know: use nm or objdump utility There is no libc function to do that. However, you can

Cannot load any more object with static TLS

允我心安 提交于 2019-11-29 12:35:03
问题 I have an application that use dlopen() to load additional modules. The application and modules are built on Ubuntu 12.04 x86_64 using gcc 4.6 but for i386 arch. The binaries are then copied to another machine with exactly same OS and work fine. However if they are copied to Ubuntu 12.04 i386 then some (but not all) modules fail to load with the following message: dlopen: cannot load any more object with static TLS I would suspect that this is caused by the usage of __thread variables.