I successfully built this trivial LKM with gcc
but the resulting binary is of size 70kB.
#include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("GPL"); static int __init LinuxKernelModule_init(void) { printk("LinuxKernelModule: Hello, world!n"); return 0; } static void __exit LinuxKernelModule_exit(void) { printk("LinuxKernelModule: Goodbye, world!n"); } module_init(LinuxKernelModule_init); module_exit(LinuxKernelModule_exit);
What CFLAGS and make
arguments would you suggest to make it smaller?
Advertisement
Answer
The standard Linux kernel is compiled in an optimal way already unless it’s a debug version or some very specific options are turned on. So the simplest Makefile for an external kernel module which doesn’t add any compiler of linker options on top of those used to build the kernel should be sufficient and should produce an optimal kernel module.
For this trivial LKM (let’s call it lkm.c
) the simplest Makefile below produces a kernel module that is 3986 bytes in size on my system. gcc
version is 7.4.0
, ldd
version is 2.27
, the kernel is 4.15.0-66-generic
, the distro is Ubuntu.
KERNEL = /lib/modules/$(shell uname -r)/build obj-m += lkm.o all: ${MAKE} -C ${KERNEL} M=$(PWD) modules clean: ${MAKE} -C ${KERNEL} M=$(PWD) clean