dlsym

dlsym() workaround return type

六月ゝ 毕业季﹏ 提交于 2019-12-20 03:32:43
问题 The man page of dlsym() lists *(void **) (&cosine) = dlsym(handle, "cos"); as a workaround for casting the return value of dlsym() . What's the meaning of *(void **) (&cosine) here? I understand cosine is a function pointer defined previously, but I'm not sure why an ampersand & is needed before the name (an error without & ). Moreover, I don't figure out why the pointer of void * ( void ** ) is again used with * . 回答1: Let's unwrap it a bit at a time: &cosine This takes a pointer to the

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

泪湿孤枕 提交于 2019-12-17 17:32:17
问题 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

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

无人久伴 提交于 2019-12-17 16:30:59
问题 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

Referencing global symbols from shared library loaded with dlopen

余生长醉 提交于 2019-12-13 04:35:51
问题 I have a shared library which I want to access symbols from the main program. For example: main.c #include <stdio.h> void bar(void) { puts("bar"); } extern void foo(void); int main(void) { foo(); return 0; } foo.c #include <stdio.h> extern void bar(void); void foo(void) { puts("foo"); bar(); } I compile and run like: gcc -c -fpic foo.c gcc -shared -o libfoo.so foo.o gcc -L$(pwd) -o test main.c -lfoo ./test And I get the output I expect: foo bar However, I must use dlopen() and dlsym() because

Attempt to call JNI_CreateJavaVM from libart.so fails

孤者浪人 提交于 2019-12-11 14:52:01
问题 I am working on a Xamarin.Android app with a C++ part. Now I need to call directly into Android Java interfaces from the C++ library. I copied the code from Caleb Fenton's detailed and very helpful blog post which uses the JNI to call from C++ to Java . But I can't get the pointer to the JVM in the same way that he does it. (By the way, I am mostly a C# programmer, so it's entirely possible that I've made an elementary mistake in C++). In the header file: #pragma once class MyJniClass { /

Wrong arguments position in function imported with dlsym

一曲冷凌霜 提交于 2019-12-11 02:22:28
问题 I have strange issue. When I invoking imported method with arguments from shared library, in those method I have wrong arguments. It's like: x = 1; y = 2; z = 3; (*method)(x,y,z); In method I have: void method(int x, int y, int z){ // x = 2, y = 3, z = 32432423 - something like this } Here how I do import: QVector<int> (*interpolateValue)( int, int, int ); libHandle = dlopen( "plugins/libinterpolate.so", RTLD_LAZY ); *(void **)(&interpolateValue) = dlsym( libHandle, "

How can i intercept dlsym calls using LD_PRELOAD?

拟墨画扇 提交于 2019-12-10 15:45:45
问题 I want to intercept application's calls to dlsym, i've tried declaring inside the .so that i'm preloading dlsym , and using dlsym itself to get it's real address, but that for quite obvious reasons didn't work. Is there a way easier than taking process' memory maps, and using libelf to find the real location of dlsym inside loaded libdl.so? 回答1: I stumbled across the same problem with hdante's answer as the commenter: calling __libc_dlsym() directly crashes with a segfault. After reading some

How to detect at runtime whether symbols are stripped?

隐身守侯 提交于 2019-12-09 09:50:07
问题 In my C++ program, how can I detect programmatically at runtime whether symbols have been stripped via the 'strip' gnu development tool on Linux? I'd like a function definition which returns true if stripped, otherwise false. Would using dlsym() on "main()" work to detect this reliably? 回答1: I know the file command can tell the difference, so you could possibly look at its source to see what mechanism it uses. 回答2: From a comment left for another answer: A stripped ELF will lack a .symtab

How to access (dynamically allocated) Fortran arrays in C

五迷三道 提交于 2019-12-09 05:05:57
问题 My main question is why arrays do such weird things and whether there is any way at all to do the following in a "clean" way. I currently have a C program foo.c interfacing a Fortran program bar.f90 via dlopen/dlsym , roughly like in the code below: foo.c: #include <dlfcn.h> #include <stdio.h> int main() { int i, k = 4; double arr[k]; char * e; void * bar = dlopen("Code/Test/bar.so", RTLD_NOW | RTLD_LOCAL); void (*allocArray)(int*); *(void **)(&allocArray) = dlsym(bar, "__bar_MOD_allocarray")

Factory Pattern in C++: generating explicit createInstance()-Method automatically

家住魔仙堡 提交于 2019-12-09 02:14:31
i have the problem in writing a C++ framework, that users should have less overhead than possible to use it. Users can publish their work to the frameworks by creating a shared library that contains a class, which is derived by a frameworks' BaseClass and implementing an extern "C" createInstance()-method in order to return an instance its' derived class. So the framework can access the user class by calling the createInstance-Method through the shared library with dlsym(). class BaseClass{} class UserClass : public BaseClass{} extern "C"{ BaseClass* UserXcreateInstance(){ return new UserClass