dlopen / dlsym with as little linking as possible
Posted
by johannes
on Stack Overflow
See other posts from Stack Overflow
or by johannes
Published on 2010-04-10T17:35:22Z
Indexed on
2010/04/10
17:43 UTC
Read the original article
Hit count: 425
I have an application which can make use of plugins which are loaded at runtime using dlopen. Each of the plugins defines a function toretrieve the plugin information which is defined using a common structure. Something like that:
struct plugin {
char *name;
char *app_version;
int app_verion_id;
char *plugin_version;
int plugin_version_id;
/* ... */
};
struct plugin p = { "sample plugin",APP_VERION,APP_VERSION_ID,"1.2.3",10203 };
struct plugin *get_plugin() {
return &p;
}
This works well and plugins can be loaded. Now i want to build a small tool to read these properties without linking the whole application. For doing that I have some code like this:
void *handle;
struct plugin *plugin;
struct plugin *(get_plugin*)();
handle = dlopen(filename, RTLD_LAZY);
if (!handle) { /*...return; ...*/ }
get_plugin = dlym(handle, "get_plugin");
if (!get_plugin) { /*...return; ...*/ }
plugin = get_plugin();
printf("Plugin: %s\n", plugin->name);
This works nice for simple plugins. The issue is that many plugins reference further symbols from the application, which are resolved even though RTLD_LAZY was set. (like global variables from the application which are used to initialize plugin-global things) So the dlopen() call fails with an error like fatal: relocation error: file sample_plugin.so: symbol application_some_symbol: referenced symbol not found
. As I just want to have access to the single simple structure I was wondering how I can prevent the linker from doing that much of his work.
© Stack Overflow or respective owner