Linux内核模块中定义的功能可用于内核代码吗?

问题描述 投票:-1回答:1

我可以在模块上使用内核代码的功能,也可以使用EXPORT_SYMBOL使用其他代码。

相反,我想在内核代码中使用EXPORT_SYMBOL使用内核模块的功能。

我对此有什么选择吗?

linux function kernel kernel-module
1个回答
0
投票

加载内核内核时,加载程序应解析每个符号(内核内核使用的(函数)。

因为加载内核内核时内​​核模块不可用,所以内核内核不能直接使用模块中定义的符号。

但是,内核内核可以具有pointer,可以在加载模块时通过模块的代码对其进行初始化。可以将其视为某种registration过程:

foo.h中

// Header used by both kernel core and modules

// Register 'foo' function.
void register_foo(int (*foo)(void));

foo.c的

// compiled as a part of the kernel core
#include <foo.h>

// pointer to the registered function
static int (*foo_pointer)(void) = NULL;

// Implement the function provided by the header.
void register_foo(int (*foo)(void))
{
  foo_pointer = foo;
}
// make registration function available for the module.
EXPORT_SYMBOL(register_foo);

// Calls foo function.
int call_foo(void)
{
  if (foo_pointer)
    return foo_pointer(); // call the registered function by pointer.
  else
    return 0; // in case no function is registered.
}

的module.c

// compiled as a module
#include <foo.h>

// Implement function
int foo(void)
{
  return 1;
}

int __init module_init(void)
{
   // register the function.
   register_foo(&foo);
   // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.