英文:
Prefix getting added to license information during Linux Kernel module build
问题
我试图从多个源文件构建单个Linux内核模块。我正在使用以下Makefile:
obj-m := mymodule.o
mymodule-y := mymodule_a.o mymodule_b.o mymodule_c.o
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
然而,即使构建成功,modinfo的输出也不如预期。具体来说,许可证、作者、参数等都有一个前缀(在这种情况下是“mymodule.”):
mymodule.description: mymodule driver
mymodule.author: myname
mymodule.license: GPL
我期望的是:
description: mymodule driver
author: myname
license: GPL
我注意到,当使用内核版本5.19构建模块时,不会出现这个前缀问题。然而,当使用内核版本5.4构建时,问题会出现。我尝试了几种方法(不生成中间的.o文件,直接添加到obj-m等),但都没有解决这个问题。
有没有人知道为什么会在特定的内核版本上添加这个前缀,以及如何防止这种情况发生?
英文:
I am trying to build a single Linux Kernel module from multiple source files. I am using the following Makefile:
obj-m := mymodule.o
mymodule-y := mymodule_a.o mymodule_b.o mymodule_c.o
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
However, even when the build is successful, the output from modinfo isn't as expected. Specifically, the license, author, parameters etc. all have a prefix ("mymodule." in this case):
mymodule.description: mymodule driver
mymodule.author: myname
mymodule.license: GPL
What I expect is:
description: mymodule driver
author: myname
license: GPL
I've noticed that this prefixing issue does not occur when building the module with Kernel version 5.19. However, when building with Kernel version 5.4, the issue does manifest. I've tried several methods (not generating intermediate .o files, adding directly to obj-m etc.), but none have resolved the issue.
Does anyone know why this prefix is being added on specific Kernel versions, and how to prevent this?
答案1
得分: 0
问题通过向编译器添加-fno-pic
标志来解决。这是通过向Makefile添加EXTRA_CFLAGS+=-fno-pic
来完成的。
通常内核模块不需要使用位置无关代码(PIC)。禁用PIC解决了我遇到的modinfo前缀问题。
英文:
The issue was resolved by adding the -fno-pic
flag to the compiler. This was done by adding EXTRA_CFLAGS+=-fno-pic
to the Makefile.
Position Independent Code (PIC) is typically not needed for kernel modules. Disabling PIC resolved the modinfo prefixing issue I was facing.
答案2
得分: 0
我遇到了同样的问题,并试图找出根本原因。
#ifdef MODULE
#define MODULE_PARAM_PREFIX /* 空 */
#define __MODULE_INFO_PREFIX /* 空 */
#else
#define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
#define __MODULE_INFO_PREFIX KBUILD_MODNAME "."
#endif
#define __MODULE_INFO(tag, name, info) \
static const char __UNIQUE_ID(name)[] \
__used __section(".modinfo") __aligned(1) \
= __MODULE_INFO_PREFIX __stringify(tag) "=" info
显然,当未定义`MODULE`时,`MODULE_INFO`会扩展为带有模块名称前缀的标签。
通过在Makefile中添加`V=1`,我可以观察到gcc的命令行。我的模块在编译过程中没有`-DMODULE`参数,而一个示例的hello-world模块确实有`-DMODULE`。
在我的情况下,我设置了`KBUILD_CFLAGS_MODULE=-Ixxxxxx`来提供额外的包含路径。预期的用法是`CFLAGS_MODULE=xxx`。覆盖`KBUILD_CFLAGS_MODULE`会导致`-DMODULE`丢失。
英文:
I ran into same problem and I tried to figure out the root cause.
#ifdef MODULE
#define MODULE_PARAM_PREFIX /* empty */
#define __MODULE_INFO_PREFIX /* empty */
#else
#define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
#define __MODULE_INFO_PREFIX KBUILD_MODNAME "."
#endif
#define __MODULE_INFO(tag, name, info) \
static const char __UNIQUE_ID(name)[] \
__used __section(".modinfo") __aligned(1) \
= __MODULE_INFO_PREFIX __stringify(tag) "=" info
Apparently, when MODULE
is not defined, MODULE_INFO
would expand to a tag with module name prefix.
By appending V=1
to Makefile, I can observe gcc command lines. My module does not have -DMODULE
argument during compilation while a hello-world example do has -DMODULE
.
In my case, I set KBUILD_CFLAGS_MODULE=-Ixxxxxx
to provide additional include path. The expected usage is CFLAGS_MODULE=xxx
. Overrides KBUILD_CFLAGS_MODULE
makes -DMODULE
lost.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论