dlsym

I can call a function imported with dlsym() with a wrong signature, why?

故事扮演 提交于 2020-08-09 09:36:59
问题 host.cpp has: int main (void) { void * th = dlopen("./p1.so", RTLD_LAZY); void * fu = dlsym(th, "fu"); ((void(*)(int, const char*)) fu)(2, "rofl"); return 0; } And p1.cpp has: #include <iostream> extern "C" bool fu (float * lol) { std::cout << "fuuuuuuuu!!!\n"; return true; } (I intentionally left errors checks out) When executing host, “fuuuuuuuu!!!” is printed correctly, even though I typecasted the void pointer to the symbol with a completely different function signature. Why did this

Referencing global symbols from shared library loaded with dlopen

坚强是说给别人听的谎言 提交于 2020-02-15 18:01:54
问题 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

Referencing global symbols from shared library loaded with dlopen

别说谁变了你拦得住时间么 提交于 2020-02-15 17:57:07
问题 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

Defining interface of abstract class in shared library

允我心安 提交于 2020-01-24 17:23:36
问题 Say I have a abstract base class defined like so: interface.hpp #ifndef INTERFACE_HPP #define INTERFACE_HPP 1 class interface{ public: virtual void func() = 0; }; #endif // INTERFACE_HPP Then I compile a translation unit test.cpp into a shared object test.so : test.cpp #include "interface.hpp" #include <iostream> class test_interface: public interface{ public: void func(){std::cout << "test_interface::func() called\n";} }; extern "C" interface &get_interface(){ static test_interface test;

Setting my lib for LD_PRELOAD makes some processes produce loader errors

点点圈 提交于 2020-01-12 03:14:06
问题 I get the following error when I try to run a script I have only execution access for: uname: symbol lookup error: /home/dumindara/random/sotest/a.out: undefined symbol: dlsym This is after I have set LD_PRELOAD environment variable to /home/dumindara/random/sotest/a.out . a.out has a test malloc function, and calls dlsym internally. I don't get this problem when running ls . Most processes do give this error. Why does this happen and what can I do to make it work? 回答1: I assume that your a

Unable to get stat with dlsym

我们两清 提交于 2020-01-04 05:48:25
问题 I'm trying to write a hook for stat / lstat / fstat but I can't seem to get the original version from dlsym . I'm using the following code to obtain the original pointers. orig_stat = dlsym(RTLD_NEXT, "stat"); orig_lstat = dlsym(RTLD_NEXT, "lstat"); orig_fstat = dlsym(RTLD_NEXT, "fstat"); However, all three variables are set to null and calling dlerror also returns null . I'm creating the shared object with: clang fakestat.c -shared -fPIC -ldl -o fakestat.so and using my library by running a

Load named unexported symbols with dlsym?

半城伤御伤魂 提交于 2020-01-04 04:34:28
问题 Is it possible to load a named unexported symbol from a framework using dlsym ? The symbol I'm trying to import has a name by which it is referred to within the framework. It is a function I need to call. I'm trying to do it the usual dlopen + dlsym way, but when I try to load a symbol that isn't exported, dlsym returns a NULL pointer. 回答1: dlsym can only load functions that are listed in the symbol table. You can list the symbol table by running nm on the framework in question. You can

Propagating exceptions through dlsym cython

烂漫一生 提交于 2020-01-02 15:06:56
问题 I am unable to propagate exceptions through dlsym. I use dlsym to load a cythonized python file. I made a minimal working example below so you can try it yourself: I have a pyx file, c_fun.pyx, which I compile to a C file using Cython. Then I'm using dlsym to load in the so file in another program, say use_fun.c++. You can use ./compile.sh to compile the files. On executing ./test, the program crashes with a segmentation fault. #c_fun.pyx: cdef public double _myfunction(double x) except*: a=1

Casting when using dlsym()

£可爱£侵袭症+ 提交于 2020-01-02 02:43:06
问题 I'm using dlsym() in C and I have a question whether the return value of dlsym() should be explicitly cast or if it is implicitly cast correctly. Here is the function: double (*(compile)(void))(double x, double y) { if (system("scan-build clang -fPIC -shared -g -Wall -Werror -pedantic " "-std=c11 -O0 -lm foo.c -o foo.so") != 0) { exit(EXIT_FAILURE); } void *handle; handle = dlopen("./foo.so", RTLD_LAZY); if (!handle) { printf("Failed to load foo.so: %s\n", dlerror()); exit(EXIT_FAILURE); }

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

為{幸葍}努か 提交于 2019-12-23 03:21:34
问题 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{}