3

我正在用 C 编写一个插件应用程序,我正在使用 dlopen/dlsym 动态加载某些函数的“实现”。例如,我有以下指向函数的指针

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

我使用以下代码加载实现:

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();

“插件”通过以下方式定义 cti_create

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

所以它定义了一个整数参数,但一切正常,没有错误。问题是:用 dlsym 加载符号时是否可以进行一些参数类型验证?如何强制加载的符号具有我期望的签名?

4

1 回答 1

2

如果函数是 C 函数,则在使用 dlsym 加载时不能进行任何参数类型验证——图像中没有任何内容可以定义参数(或返回类型)。如果您使用的是 C++(并且没有声明符号具有extern "C"链接),那么类型检查将嵌入到实际的符号名称中。话虽如此,在调用时,dlsym()您必须传入损坏的 C++ 名称,而不是“cti_create”。

于 2014-03-10T13:49:24.793 回答