可以延迟导出符号的加载吗?

时间:2013-06-10 19:22:18

标签: linux-kernel kernel-module kernel

我正在使用一些Linux内核模块,并且有一个与循环加载问题相关的问题。

模块A首先加载并导出许多符号,供模块B或C使用。然后,模块B或C随后被加载,并且符号存在供其使用。

但是,我现在发现模块A需要来自模块B或C的符号,但仅在运行时期间,而不需要初始化模块。所以当A加载时,它发现该符号尚不存在。我甚至在模块A中将符号标记为extern,但这也不起作用。

是否可以在加载模块A后延迟加载符号,但在加载B或C之前它仍然不存在?

2 个答案:

答案 0 :(得分:5)

这种情况通常使用回调来解决。

假设模块A导出函数以注册/取消注册回调。 B和/或C使用这些函数并向A提供适当的回调。当需要时,A检查回调是否已设置并调用它们。

像这样(没有错误处理和锁定以简化):

/* Module A */
struct a_ops /* Better to define struct a_ops in a header file */
{
    void (*needed_func)(void);
    void (*another_needed_func)(void);
}; 
...
struct a_ops ops = {
    .needed_func = NULL;
    .another_needed_func = NULL;
};
...
int a_register_needed_funcs(struct a_ops *a_ops)
{
    ops.needed_func = a_ops->needed_func;
    ops.another_needed_func = a_ops->another_needed_func;
}
EXPORT_SYMBOL(a_register_needed_funcs);

void a_unregister_needed_funcs()
{
    ops.needed_func = NULL;
    ops.another_needed_func = NULL;
}
EXPORT_SYMBOL(a_unregister_needed_funcs);

...
/* Call the specified callbacks when needed: */
void do_something(void)
{
    if (ops.needed_func != NULL) {
        ops.needed_func();
    }
    else {
            /* the callback is not set, handle this: report error, ignore it or
             * do something else */
            ...
    }
}
...

/* Modules B and C */
/* Their code #includes the file where struct a_ops is defined. 
 * The module registers the callbacks, for example, in its init function
 * and unregister in exit function. */
...
static void func(void)
{
    ...
}

static void another_func(void)
{
    ...
}

struct a_ops my_funcs = {
    .needed_func = func;
    .another_needed_func = another_func;
};

int __init my_module_init(void)
{
    ...
    result = a_register_needed_funcs(&my_funcs);
    ... 
}
void __exit my_module_exit(void)
{
    ...
    a_unregister_needed_funcs();
    ...
}

这类似于文件操作和内核中的许多其他回调操作。假设用户想要读取由自定义驱动程序维护的字符设备。内核本身(确切地说是VFS)接收请求但不能自己处理它。它将请求转发到已为该设备注册其文件操作回调的自定义驱动程序。反过来,驱动程序使用内核导出的函数,如cdev_add()

答案 1 :(得分:1)

如果您知道符号的类型/原型,请尝试使用kallsyms_lookup_name()在运行时获取指向所需符号的指针,而不是作为外部符号链接到它(意味着让加载器查找它你在加载时)。可以使用您最喜欢的搜索引擎找到示例。