Tuesday, May 13, 2008

Module Compiling Makefile Changes from 2.4 to 2.6

2.4 method of compiling a module:
 KERNELDIR = /usr/src/linux
CFLAGS = -D__KERNEL__ -DMODULE -I$(KERNELDIR)/include -O

all: module.o
And done. To add on to this, somthing more is required in 2.6

In 2.6, all loadable modules now need to go through a linking step. The link brings in init/vermagic.o from the kernel source tree; this object creates a special section in the loadable module describing the environment in which it was built. It includes the compiler version used, whether the kernel was built for SMP, whether kernel preemption is enabled, the architecture which was compiled for, and, of course, the kernel version. A difference in any of these parameters can render a module incompatible with a given running kernel; rather than fail in mysterious ways, the new module loader opts to detect these compatibilities and refuse to load the module.

New way:

obj-m :=module.o
module-objs := file1.o file2.o

To get the make command call the kernel build system to read this makefile, add:

make -C /path/to/source SUBDIRS=$PWD modules

The makefile is read in two passes, in the first time it will simply invoke the kernel build system, while the actual work will get done in the second pass.


Reference: http://lwn.net/Articles/21823/

No comments: