dlopen

dlopen on new binary with same name returns old handle

假如想象 提交于 2019-12-01 13:48:21
I'm using dlopen to load dynamically generated code. The program calls the compiler on the code and generates a .so file which is then loaded by the program to extend itself. The problem is that if I use the same name for the generated code, the dlopen returns a handle to the old object, not the new one. The code looks like: …generate code into test.cpp system("gcc <args> test.cpp -o test.so"); void *handle = dlopen("test.so"); void *sym = dlsym(handle, "run"); (*sym)(); dlclose(handle); …Do other work …generate different code to test.cpp system("gcc <args> test.cpp -o test.so"); void *handle

PostgreSQL如何实现跨平台代码

笑着哭i 提交于 2019-12-01 11:29:25
我们知道,PostgreSQL可以支持几乎(这个词似乎可以不要)所有主流平台,平台间尤其Windows与*nix之间的API差异巨大,PG是怎么做到的呢,用一个简单的例子解释。 前边我写怎么在Windows下编译mysql_fdw提到过的修改: #include "dynloader.h" mysql_dll_handle = dlopen(_MYSQL_LIBNAME, RTLD_LAZY | RTLD_DEEPBIND); 改为 mysql_dll_handle = dlopen(_MYSQL_LIBNAME, 1); 更正规的写法是 #if defined(__APPLE__) || defined(__FreeBSD__) mysql_dll_handle = dlopen(_MYSQL_LIBNAME, RTLD_LAZY); #elif defined WIN32 mysql_dll_handle = pg_dlopen(_MYSQL_LIBNAME, 1); #else mysql_dll_handle = dlopen(_MYSQL_LIBNAME, RTLD_LAZY | RTLD_DEEPBIND); #endif 这里并没有修改原有两行,只是为展示应该怎么写,模块代码的跨平台性才会更好些。 dynloader.h在编译前会根据平台指向正确的头文件

Static Vs Dynamic libraries

让人想犯罪 __ 提交于 2019-12-01 11:01:30
I have read about static and dynamic libraries. My question is little specifie dlopen dlclose : Benifit of dlopen is we can start the EXE with out loading the necessary libraries at the begining. Only when we need we will load the libratries and unload it from the memory. This is behaviour of dynamic linking libaries. My question is if i link the library libUtlities ld -o EXE main.o -lUtilities When I run the EXE, libUtlities will be loaded to the memory before I first use there functionalities which i observed in dbx (Solaris debugger) But will not contribute to the size of the EXE. 1.So is

Static Vs Dynamic libraries

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:09:36
问题 I have read about static and dynamic libraries. My question is little specifie dlopen dlclose : Benifit of dlopen is we can start the EXE with out loading the necessary libraries at the begining. Only when we need we will load the libratries and unload it from the memory. This is behaviour of dynamic linking libaries. My question is if i link the library libUtlities ld -o EXE main.o -lUtilities When I run the EXE, libUtlities will be loaded to the memory before I first use there

Fixing undefined reference to dlopen and dlcose

走远了吗. 提交于 2019-12-01 07:29:57
问题 I have created simple c++ application. I can compile it and t works fine. But now I need to load library dynamically and I have added dlfnc.h to my project and added some more code: #include <iostream> #include <dlfcn.h> void *mylib; int eret; using namespace std; int main() { mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY); eret = dlclose(mylib); cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! return 0; } Compiling: myname@ubuntu:~/workspace/LinuxGcc/src$ g++ LinuxGcc.cpp

Static Class Variables in Dynamic Library and Main Program [duplicate]

五迷三道 提交于 2019-11-30 22:38:08
This question already has an answer here: Main Program and Shared Library initializes same static variable in __static_initialization_and_destruction_0 1 answer I am working on a project that has a class 'A' that contains a static stl container class. This class is included in both my main program and a .so file. The class uses the default(implicit, not declared) constructor/destructor. The main program loads the .so file using dlopen() and in its destructor, calls dlclose(). The program crashes after main exits when glibc calls the destructor for the static class member variable. The problem

Compile C program using dlopen and dlsym with -fPIC

一笑奈何 提交于 2019-11-30 21:29:59
I am having a problem about a wrong symbol resolution. My main program loads a shared library with dlopen and a symbol from it with dlsym. Both the program and the library are written in C. Library code int a(int b) { return b+1; } int c(int d) { return a(d)+1; } In order to make it work on a 64-bit machine, -fPIC is passed to gcc when compiling. The program is: #include <dlfcn.h> #include <stdio.h> int (*a)(int b); int (*c)(int d); int main() { void* lib=dlopen("./libtest.so",RTLD_LAZY); a=dlsym(lib,"a"); c=dlsym(lib,"c"); int d = c(6); int b = a(5); printf("b is %d d is %d\n",b,d); return 0;

Static Class Variables in Dynamic Library and Main Program [duplicate]

末鹿安然 提交于 2019-11-30 18:00:24
问题 This question already has an answer here : Main Program and Shared Library initializes same static variable in __static_initialization_and_destruction_0 (1 answer) Closed last year . I am working on a project that has a class 'A' that contains a static stl container class. This class is included in both my main program and a .so file. The class uses the default(implicit, not declared) constructor/destructor. The main program loads the .so file using dlopen() and in its destructor, calls

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

陌路散爱 提交于 2019-11-30 17:47:40
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 using to check if the library was unloaded: dlclose(handles[name]); do { void *handle = dlopen

Compile C program using dlopen and dlsym with -fPIC

拜拜、爱过 提交于 2019-11-30 17:11:05
问题 I am having a problem about a wrong symbol resolution. My main program loads a shared library with dlopen and a symbol from it with dlsym. Both the program and the library are written in C. Library code int a(int b) { return b+1; } int c(int d) { return a(d)+1; } In order to make it work on a 64-bit machine, -fPIC is passed to gcc when compiling. The program is: #include <dlfcn.h> #include <stdio.h> int (*a)(int b); int (*c)(int d); int main() { void* lib=dlopen("./libtest.so",RTLD_LAZY); a