سؤال

I'm writing a plug-in application in C and I'm using dlopen/dlsym to load dynamically the "implementation" of some functions. For example I have the following pointer to a function

struct cti_t* (*create)() = 0;

And I load the implementation using the following code:

plugin_handle = dlopen ("xxx.so", RTLD_NOW);
//error checking

create = dlsym(plugin_handle, "cti_create");
//error checking

//call create for the specific implenetation
struct cti_t *dev = create();

The "plug-in" defines the cti_create in the following way

struct cti_t* cti_create(int i) {
   printf("Creating device lcl");
       //do somenthing with i
   return &lcl_cti;
}

So it defines one integer parameter, but everything works without errors. The question is: is it possible to make some argument type verification when the symbol is loaded with dlsym? How can I enforce that the symbols loaded have the signature I expect?

هل كانت مفيدة؟

المحلول

You cannot make any argument type verification when loading with dlsym if the function is a C function -- there's nothing in the image to define the arguments (or the return type). If you were working with C++ (and did not declare the symbol to have extern "C" linkage), then the type checking would be embedded in the actual symbol name. That being said, at the time of calling dlsym() you would have to pass in the mangled C++ name, not "cti_create".

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top