2024-04-23 19:50:20 +00:00
|
|
|
// 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
|
2024-04-24 19:04:52 +00:00
|
|
|
void *module = dlopen("$ORIGIN/linux_plugin.so", RTLD_NOW);
|
2024-04-23 19:50:20 +00:00
|
|
|
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();
|
|
|
|
}
|