Question

i'm using libltdl in order to dynamically load plugin libraries. Been following this documentation, and after i call this

lt_dlhandle lt_dlopen (const char *filename)

i need to know what symbols are defined in this library. I need the list of symbols to pass it to

void * lt_dlsym (lt_dlhandle handle, const char *name)

Which requires a symbol name as an argument.

What is the way to get the lists of loadable exported symbols in my plugin?

Was it helpful?

Solution

Like said Matthieu M. in his comment, there is no native way to get a list of loaded symbols from a dynamic lib.

However, I usually use this workaround, which is to make your plugin declare the symbols in a container, and then to retrieve this container from your main program.

plugin.h

#include <set>
#include <string>

// call this method from your main program to get list of symbols:
const std::set<std::string> & getSymbols();

void MySymbol01();
bool MySymbol02(int arg1, char arg2);

plugin.c

#include "plugin.h"

class SymbolDeclarator {
    public:
    static std::set<std::string> symbols;
    SymbolDeclarator(const std::string & symbol) {
        symbols.insert(symbol);
    }
};

const std::set<std::string> & getSymbols() {
    return SymbolDeclarator::symbols;
}

#define SYMBOL(RETURN, NAME) \
    static const SymbolDeclarator Declarator##NAME(#NAME); \
    RETURN NAME

SYMBOL(void, MySymbol01)() {
    // write your code here
}

SYMBOL(bool, MySymbol02)(int arg1, char arg2) {
    // write your code here
}

I only see 2 issues with this solution:

  1. to have a non-const static variable: symbols declared in plugin.c -> non thread-safe.
  2. to have code executed before the main(), which is hardly debuggable.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top