使用 GCC 命令行从 .c 文件构建 .ko 文件

Build .ko file from .c file using GCC command line

作为一个学习练习,我正在尝试从一个 c 文件构建一个内核目标文件,该文件在使用 modprobe 加载时只会执行 printk("hello, world")。我的内核版本是4.15.

假设我有这个示例代码:

int printk(const char *fmt, ...);

struct platform_device;

struct platform_driver {
    int (*probe)(struct platform_device *);
    int (*remove)(struct platform_device *);
};

static struct platform_driver my_driver = {
    .probe = my_driver_probe,
    .remove = my_driver_remove
};

module_platform_driver(my_driver);

static int my_driver_probe(struct platform_device *pdev) {
        printk("my driver loaded\n");
    return 0;
}

static int my_driver_remove(struct platform_device *pdev) {
        printk("my driver removed\n");
    return 0;
}

我如何使用 gcc 编译它以创建一个可以用 modprobe 加载的有效 my_driver.ko 文件?

I'm trying to build a kernel object file from a c file which would only do printk("hello, world")

对此类模块的简短 google 搜索为我提供了以下示例:https://github.com/maK-/SimplestLKM https://github.com/moutoum/linux-kernel-module-hello-world https://github.com/ichergui/hello-world https://github.com/carloscdias/hello-world-linux-module .

How do I compile it using gcc to create a valid my_driver.ko file that can be loaded with modprobe?

第一个项目在 hello.c 中保存了以下 hello world 模块的源代码:

#include<linux/init.h>
#include<linux/module.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("maK");

static int hello_init(void){
    printk(KERN_ALERT "Hello world!\n");
    return 0;
}

static void hello_exit(void){
    printk(KERN_ALERT "Goodbye cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

以下是使用 V=1 编译第一个项目的会话记录:

$ git clone https://github.com/maK-/SimplestLKM
Cloning into 'SimplestLKM'...
remote: Enumerating objects: 15, done.
remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 15
Receiving objects: 100% (15/15), 13.47 KiB | 431.00 KiB/s, done.
Resolving deltas: 100% (4/4), done.
$ cd SimplestLKM/
$ make V=1
make -C /lib/modules/5.10.72-1-lts/build M=/dev/shm/.1000.home.tmp.dir/SimplestLKM modules
make[1]: Entering directory '/usr/lib/modules/5.10.72-1-lts/build'
test -e include/generated/autoconf.h -a -e include/config/auto.conf || (        \
echo >&2;                           \
echo >&2 "  ERROR: Kernel configuration is invalid.";       \
echo >&2 "         include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 "         Run 'make oldconfig && make prepare' on kernel src to fix it.";  \
echo >&2 ;                          \
/bin/false)
make -f ./scripts/Makefile.build obj=/dev/shm/.1000.home.tmp.dir/SimplestLKM \
single-build= \
need-builtin=1 need-modorder=1
  gcc -Wp,-MMD,/dev/shm/.1000.home.tmp.dir/SimplestLKM/.hello.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN  -DMODULE  -DKBUILD_BASENAME='"hello"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.c
   ./tools/objtool/objtool orc generate  --module --no-fp --retpoline --uaccess /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o
  { echo  /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o;  echo; } > /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod
  {   echo /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.ko; :; } | awk '!x[[=11=]]++' - > /dev/shm/.1000.home.tmp.dir/SimplestLKM/modules.order
make -f ./scripts/Makefile.modpost
  sed 's/ko$/o/' /dev/shm/.1000.home.tmp.dir/SimplestLKM/modules.order | scripts/mod/modpost  -a   -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/Module.symvers -e -i Module.symvers  -N -T -
make -f ./scripts/Makefile.modfinal
  gcc -Wp,-MMD,/dev/shm/.1000.home.tmp.dir/SimplestLKM/.hello.mod.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN  -DMODULE  -DKBUILD_BASENAME='"hello.mod"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.c
  ld -r -m elf_x86_64 --build-id=sha1  -T scripts/module.lds -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.ko /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o;  true
make[1]: Leaving directory '/usr/lib/modules/5.10.72-1-lts/build'

由此我们知道我们还需要生成的 hello.mod.c。 modpost program生成的本模块的内容如下:

#include <linux/module.h>
#define INCLUDE_VERMAGIC
#include <linux/build-salt.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>

BUILD_SALT;

MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);

__visible struct module __this_module
__section(".gnu.linkonce.this_module") = {
    .name = KBUILD_MODNAME,
    .init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
    .exit = cleanup_module,
#endif
    .arch = MODULE_ARCH_INIT,
};

#ifdef CONFIG_RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif

MODULE_INFO(depends, "");


MODULE_INFO(srcversion, "C7C2D304485DDC1C93263AE");

有了这两个文件,我们可以用gcc编译它们,先编译成hello.ohello.mod.o,然后再用ld组合。所以这些是在我的系统上编译模块所需的编译器标志:

( cd /usr/lib/modules/5.10.72-1-lts/build/ && gcc -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN  -DMODULE  -DKBUILD_BASENAME='"hello"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.c )

( cd /usr/lib/modules/5.10.72-1-lts/build/ && gcc -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN  -DMODULE  -DKBUILD_BASENAME='"hello"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.c )

( cd /usr/lib/modules/5.10.72-1-lts/build/ && ld -r -m elf_x86_64 --build-id=sha1  -T scripts/module.lds -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.ko /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o )

之后我们可以 insmod hello.ko 并在 dmesg 中使用 CRIT 打印 hello world。

what are the compiler flags to use in order to do it.

以与上述相同的方式,您可以在您的系统上找出传递给编译器的选项