gpt4 book ai didi

linux - 警告构建使用导出符号的内核模块

转载 作者:IT王子 更新时间:2023-10-29 00:18:53 27 4
gpt4 key购买 nike

我有两个内核模块(比如 modA 和 modB)。 modA 使用 EXPORT_SYMBOL(symA) 导出一个符号,modB 使用它。我有 modA 的 header modA.h:

...
extern void symA(int param);
...

modB.c 中:

#include "modA.h"
...
static int __init modB_init(void)
{
symA(10);
}
...

如果我的 insmod modB 一切正常,我的 modB 在内核中正确链接并且函数 symA 被正确调用。然而,当我构建 modB 时,编译器会发出警告:symA is undefined。 LKM 是可重定位的 ELF,那么为什么编译器会发出此警告?如何删除?

最佳答案

此问题(以及在这种情况下如何正确编译)在 http://www.kernel.org/doc/Documentation/kbuild/modules.txt 中进行了解释

Sometimes, an external module uses exported symbols from another external module. kbuild needs to have full knowledge of all symbols to avoid spitting out warnings about undefined symbols. Three solutions exist for this situation.

NOTE: The method with a top-level kbuild file is recommended but may be impractical in certain situations.

Use a top-level kbuild file If you have two modules, foo.ko and bar.ko, where foo.ko needs symbols from bar.ko, you can use a common top-level kbuild file so both modules are compiled in the same build. Consider the following directory layout:

  ./foo/ <= contains foo.ko       ./bar/ <= contains bar.ko

The top-level kbuild file would then look like:

#./Kbuild (or ./Makefile): obj-y := foo/ bar/

And executing

$ make -C $KDIR M=$PWD

will then do the expected and compile both modules with full

knowledge of symbols from either module.

Use an extra Module.symvers file When an external module is built, a Module.symvers file is generated containing all exported symbols which are not defined in the kernel. To get access to symbols from bar.ko, copy the Module.symvers file from the compilation of bar.ko to the directory where foo.ko is built. During the module build, kbuild will read the Module.symvers file in the directory of the external module, and when the build is finished, a new Module.symvers file is created containing the sum of all symbols defined and not part of the kernel.

Use "make" variable KBUILD_EXTRA_SYMBOLS If it is impractical to copy Module.symvers from another module, you can assign a space separated list of files to KBUILD_EXTRA_SYMBOLS in your build file. These files will be loaded by modpost during the initialization of its symbol tables.

关于linux - 警告构建使用导出符号的内核模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9499687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com