dlopen

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

typeinfo, shared libraries and dlopen() without RTLD_GLOBAL

随声附和 提交于 2020-01-31 03:34:45
问题 I'm having some trouble with exceptions not functioning correctly (or at least, as I would hope; I know there are issues with this) across shared libraries when loaded using dlopen . I include some simplified example code here. The actual situation is myapp =Matlab, myext1 =mexglx matlab extension, mylib is a shared library of my code between the two extensions ( myext1 , myext2 ) mylib.h struct Foo { Foo(int a); m_a; } void throwFoo(); mylib.cpp #include "mylib.h" Foo::Foo(int a): m_a(a) {}

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;

glibc : Test if lib as DF_1_NODELETE flag or if lib has unique symbol

别来无恙 提交于 2020-01-24 09:58:09
问题 I'm using dlopen / dlclose to load lib with glibc 2.21. Is there a C++ call to check a lib as a DF_1_NODELETE flag set ? readelf seems to be able to do it. or at least if a lib has unique symbol defined in it ? nm is definitely able to do it. ideally i would like something like : CloseLib( libHandle lib) { if( checkIfLibIsClosable(lib) ) { dlclose(lib) } } This is in order to avoid calling dlclose on lib with DF_1_NODELETE flag, as calling it will fails with an assert error : Inconsistency

glibc : Test if lib as DF_1_NODELETE flag or if lib has unique symbol

南笙酒味 提交于 2020-01-24 09:58:05
问题 I'm using dlopen / dlclose to load lib with glibc 2.21. Is there a C++ call to check a lib as a DF_1_NODELETE flag set ? readelf seems to be able to do it. or at least if a lib has unique symbol defined in it ? nm is definitely able to do it. ideally i would like something like : CloseLib( libHandle lib) { if( checkIfLibIsClosable(lib) ) { dlclose(lib) } } This is in order to avoid calling dlclose on lib with DF_1_NODELETE flag, as calling it will fails with an assert error : Inconsistency

access dlopen flags

家住魔仙堡 提交于 2020-01-23 03:21:15
问题 I'm inside a shared object (code) loaded with dlopen. I want to know the flags given to the loading call. I do not have access to the loader (code) - e.g. it may be a script interpreter - but I have to create subsequent dlopen calls with the same flags. How can I do this? 回答1: I think this is NOT possible without the aid of debuggers. From the latest glibc code , here is the source code of dlopen void * dlopen (const char *file, int mode) { return __dlopen (file, mode, RETURN_ADDRESS (0)); }

dlopen a dynamic library from a static library linux C++

混江龙づ霸主 提交于 2020-01-13 06:53:09
问题 I've a linux application that links against a static library (.a) and that library uses the dlopen function to load dynamic libraries (.so) If I compile the static library as dynamic and link it to the application, the dlopen it will work as expected, but if I use it as described above it won't. Can't a static library uses the dlopen function to load shared libraries? Thanks. 回答1: There should be no problem with what you're trying to do: app.c: #include "staticlib.h" #include "stdio.h" int

Dynamic Linking Loader in Linux

谁说胖子不能爱 提交于 2020-01-10 23:24:11
The four functions dlopen (), dlsym (), dlclose (), dlerror () implement the interface to the dynamic linking loader. #include < dlfcn.h > void *dlopen(const char * filename , int flag ); char *dlerror(void); void *dlsym(void * handle , const char * symbol ); int dlclose(void * handle ); dlerror dlerror () 返回一个适合程序员阅读的字符串,描述在最近一次的 dlopen (), dlsym (), dlclose ()调用中出现的错误。如果没有错误返回NULL。 dlopen dlopen接收一C-style字符串,返回一个对dynamic library的handle.如果文件名为NULL,返回到main program的handle。如果filename中包含"/",就会将其理解为一个(相对或者绝对)路径名。否则dynamic linker按照下面的顺序搜索library: 如果calling program的可执行文件包含DT_RPATH tag,并且不包含DT

How to do runtime binding based on CPU capabilities on linux

[亡魂溺海] 提交于 2020-01-09 19:16:00
问题 Is it possible to have a linux library (e.g. "libloader.so") load another library to resolve any external symbols? I've got a whole bunch of code that gets conditionally compiled for the SIMD level to be supported ( SSE2, AVX, AVX2 ). This works fine if the build platform is the same as the runtime platform. But it hinders reuse across different processor generations. One thought is to have executable which calls function link to libloader.so that does not directly implement function . Rather

How to do runtime binding based on CPU capabilities on linux

痴心易碎 提交于 2020-01-09 19:15:29
问题 Is it possible to have a linux library (e.g. "libloader.so") load another library to resolve any external symbols? I've got a whole bunch of code that gets conditionally compiled for the SIMD level to be supported ( SSE2, AVX, AVX2 ). This works fine if the build platform is the same as the runtime platform. But it hinders reuse across different processor generations. One thought is to have executable which calls function link to libloader.so that does not directly implement function . Rather