mr4th-dynamic-linking/linux_concrete/linux_main.c

39 lines
1019 B
C

// declare base_func; no mark -> load-time imported symbol
void base_func(void);
// setup plugin_func as a function pointer so that it can be linked at run time
typedef void PLUGIN_Func(void);
PLUGIN_Func *plugin_func = 0;
// need to include for dlopen and dlsym
#include <dlfcn.h>
// helper macro for dlsym that handles the casting of function pointers
#define GET_PROC(var,mod,name) *((void**)(&(var))) = dlsym((mod),(name))
int main(){
// functions linked by load-time linking can be called directly without further setup
base_func();
// to call a function with run-time linking, we must manually load and link it
void *module = dlopen("./linux_plugin.so", RTLD_NOW);
if (module != 0){
GET_PROC(plugin_func, module, "plugin_func");
}
// calls to plugin_func only work after the "plugin" loaded successfully
if (plugin_func != 0){
plugin_func();
}
// demonstration: the data structures contained in the 'base' are not duplicated in each binary
base_func();
}