无法构建简单的 Hello world 内核模块

Par*_*h K 7 kernel gcc 18.04

我刚刚开始编写内核模块。我指的是本指南

我完全按照指南中的说明编写了一个简单的 Hello world 程序。

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */

int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");

    /* 
     * A non 0 return means init_module failed; module can't be loaded. 
     */
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下 makefile 编译它:

obj-m += hello-1.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run Code Online (Sandbox Code Playgroud)

我收到错误:

error: code model kernel does not support PIC mode
Run Code Online (Sandbox Code Playgroud)

我查了一下这个论坛和其他人,发现了以下内容

  1. 试图构建 Linux 内核但没有成功的人。这里
  2. 这让我找到了这个补丁。但我不知道如何使用它。
  3. 然后我尝试使用 gcc-5(只是看看会发生什么。它仍然不起作用。
  4. 我基本上以这个人的身份经历了所有步骤。我尝试添加-fno-pie,然后是那个家伙遵循的其他步骤。

$ uname -a
Linux cristopher 4.15.0-43-generic #46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

根据错误链接,这已在gcc. 但我认为它还没有在我使用的稳定版本中。

我可以想到这个问题的多种解决方案。1. 安装上述(2)中的补丁。2. 使用旧版本gcc,这不是问题。3. 在内核 makefile 中使用更多额外的标志。(虽然不确定这一点。)


我不知道如何做上述任何事情。任何帮助,将不胜感激。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:    18.04
Codename:   bionic
Run Code Online (Sandbox Code Playgroud)

PS:我最近从 16.04 升级到 18.04。它以前工作正常。


包括make all.

$ make all
make -C /lib/modules/4.15.0-43-generic/build M=/media/parth/F/Parth/programs/dev modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-43-generic'
  CC [M]  /media/parth/F/Parth/programs/dev/hello-1.o
/media/parth/F/Parth/programs/dev/hello-1.c:1:0: error: code model kernel does not support PIC mode
 #include <linux/module.h> /* Needed by all modules */
 ^
scripts/Makefile.build:339: recipe for target '/media/parth/F/Parth/programs/dev/hello-1.o' failed
make[2]: *** [/media/parth/F/Parth/programs/dev/hello-1.o] Error 1
Makefile:1551: recipe for target '_module_/media/parth/F/Parth/programs/dev' failed
make[1]: *** [_module_/media/parth/F/Parth/programs/dev] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-43-generic'
Makefile:7: recipe for target 'all' failed
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)

好的,我不确定如何或为什么,但原始错误不再出现。但是有一个新的错误:

In file included from ./include/linux/list.h:5:0,
                 from ./include/linux/module.h:9,
                 from /media/parth/F/Parth/programs/dev/hello-1.c:1:
./include/linux/types.h:17:9: error: unknown type name ‘__kernel_ino_t’
 typedef __kernel_ino_t  ino_t;
         ^~~~~~~~~~~~~~
./include/linux/types.h:18:9: error: unknown type name ‘__kernel_mode_t’
 typedef __kernel_mode_t  mode_t;
         ^~~~~~~~~~~~~~~
./include/linux/types.h:21:9: error: unknown type name ‘__kernel_off_t’
 typedef __kernel_off_t  off_t;
         ^~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

等等等等等等....

这一切似乎都源于此:

In file included from ./include/linux/list.h:9:0,
                 from ./include/linux/module.h:9,
                 from /media/parth/F/Parth/programs/dev/hello-1.c:1:
./include/linux/kernel.h:6:10: fatal error: stdarg.h: No such file or directory
 #include <stdarg.h>
          ^~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我尝试-fno-pie在我的系统的以下几行(大约第 650 行)中从内核构建 makefile 中删除。并得到了原来的错误信息。

KBUILD_CFLAGS   += $(call cc-option,-fno-PIE)
KBUILD_AFLAGS   += $(call cc-option,-fno-PIE)
Run Code Online (Sandbox Code Playgroud)

好的,我更新了我的系统,在thisthisthis链接的发布文件生效后。似乎他们已经修复了错误。或者,是否有条款规定您在执行 UBUNTU 的全新安装后无法升级到最新的软件包?

不管怎样,还是想知道更多……