Makefile for a basic kernel module - linux

The following Makefile aims to create a basic Module in the kernel 2.6. And so, I really would like if somebody explain to me the command lines in this 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
One further question : Being in a 2.6 kernel, should I replace hello-1.o by hello-1.ko?

The first line tells the kernel makefile that hello-1.o should be used to build a module. The kernel makefile will handle the extension itself; you should not change it.
The third and fifth lines invoke the kernel makefile to build/clean a module, passing it the directory it should look for source files in.
The rest is all standard makefile boilerplate.
See Documentation/kbuild/modules.txt in the kernel documentation for more details.

Related

How to compile Linux kernel module without kernel Makefile?

I have an ARM Linux device, but I don't have the Makefile of the kernel to build a kernel module.
I have GCC cross compiler to this arch.
How can I compile a kernel module without the Makefile of the kernel?
How can I compile kernel module without have the Makefile of the kernel?
You can't. You need the kernel Makefile in order to compile a module, along with a pre-built kernel source tree. On most distributions, you can obtain the kernel source for building modules through packages like linux-headers-xxx where xxx should be the output of uname -r.
For example, on Debian or Ubuntu:
sudo apt-get install linux-headers-$(uanme -r)
You will then find the files needed for building modules at /lib/modules/$(uname -r)/build, and you can build a module with a Makefile like this:
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m := mymodule.o # same name as the .c file of your module
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
What this does is basically invoking the kernel Makefile telling it to build the module in the current directory.
Also, I am not sure why you say that you are on an ARM Linux device, and you have a cross compiler. If you are on the device itself, you shouldn't need a cross compiler at all. If you are on a different device, then you'll need to use the appropriate uname -r for the target device in order to get the right sources and in order to build. You might need to do this by hand since the output of uname -r isn't always helpful.
You'll also need to secify the architecture and cross compilation toolchain prefix in the module Makefile, for example:
default:
$(MAKE) -C $(KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- modules

Kernel Makefiel Link against Module

I am currently trying to write some Kernel Module code.
I am using the functions of an existing Kernel Module.
The hearders are included in my .c file but I dont know how to link my code with the Kernel Module properly
in normal userspace Makefiles I would know but not how to do this with Kernel Modules
Any suggestions?
Here's my Makefile
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
INC := -I/usr/src/kernels/$(shell uname -r)/include
obj-m := scif.o
all:
$(MAKE) V=1 -C $(KERNELDIR) M=$(PWD) modules
clean:
make -C $(KERNELDIR) M=$(PWD) clean
You do not 'link' code between modules. Beside including the header files, a module can call only functions that are exported (with EXPORT_SYMBOL or EXPORT_SYMBOL_GPL ..etc) either in the main Kernel or in other modules. Also, make sure that module dependencies in modeules.dep are correct or the module will complain when it is loaded.

Device Driver. Make file,External module

I'm trying to cross compile a helloworld kernel (2.6.x) module for ARM architecture on my intel x86 host.
The tool chain for ARM is located at: /home/vivek/ti-sdk-am335x-evm-05.07.00.00/linux-devkit/bin
The kernel source is located at: /home/vivek/Arago
The hellow.c and Makefile are located on Desktop on /home/vivek/Desktop/hellodriver
I have given the path for cross compiler as /home/vivek/ti-sdk-am335x-evm-05.07.00.00/linux-devkit/bin
My Makefile is follows:-
export ARCH=arm
export CROSS_COMPILE=arm-arago-linux-gnueabi-
obj-m =Hello.o
KDIR =/home/vivek/Arago
PWD = $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
On executing make I am getting
vivek#ubuntu:~/Desktop/hellodriver$ make
make: Nothing to be done for `default'.
What am I doing wrong?
There is mistake in your Makefile. As your source code name is hellow.c, But in your Makefile its Hello.o.
So change your obj-m =Hello.o to obj-m =hellow.o
And one more thing Etan Reisner said above make sure you using Tab for command in Makefile.

Cross-compiling external linux module: M=`pwd`directory not found

I'm compiling an external module against a complete kernel tree using CentOS. I've succesfully modified, cross-compiled, built and booted this kernel on a Beaglebone. I'm using the linaro tools (arm-gnueabihf-). I'm using the same typical Makefile structure to compile the module I've ever used since 2.6 came out. When running the make rule:
make ARCH=arm CROSS_COMPILE=${CC} -C $(KDIR) M=`pwd` modules
where CC and KDIR are properly set for my toolchain and my kernel src tree, kbuild properly changes dir to the -C (kernel src) dir, but when coming back (i.e. processing M) I get the following error:
scripts/Makefile.build:44: /home/foo/bar/
Makefile: No such file or directory
where /home/foo/bar is actually my current working directory, i.e. the proper outcome of pwd
Directly writing my pwd path in M= or using a make var instead of invoking pwd yields the same result (not a syntax problem)
Any idea?
Just try this ,as i dont know how is your make file.
create Makefile in /home/foo/bar
obj-m += hello.o
KERNELDIR=/home/vinay.hunachyal/build/clfs/sources/linux-3.9
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
make -C $(KERNELDIR) M=$(PWD) clean
In above Makefile edit KERNELDIR=(path-to-kernel-source)
also change obj-m +=(your module)
After this just build your module using
make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-

Redirecting output of linux module build

I'd like to redirect the output of my module build to segregate the artifacts from the source.
My makefile looks like:
obj-m += hello-1.o
all:
make ARCH=arm CROSS_COMPILE=arm-eabi- -C /work/TI-Android-ICS-4.0.3_AM37x_3.0.0/kernel M=$(PWD) modules
clean:
make ARCH=arm CROSS_COMPILE=arm-eabi- -C /work/TI-Android-ICS-4.0.3_AM37x_3.0.0/kernel M=$(PWD) clean
This works correctly, except that the module output happens to be in my source directory. I tried adding O={path to my output dirctory} in each line, but then it failed to build with something like...
ERROR: Kernel configuration is invalid.
include/generated/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.
WARNING: Symbol version dump
/work/development/linux/driver/blah/Module.symvers
is missing; modules will have no dependencies and modversions.
I assume this stems from the fact that there is some output file from the kernel build that's used in the module build, and changing the output directory with "O=" collides with that.
Is there a method for accomplishing this using the existing build infrastructure?
Looking at the documentation for the module system, it doesn't look good. Perhaps you can copy .config into your build directory and do a make oldconfig && make modules-prepare with O= set.
Alternatively, what happens if you run make from another directory?
/somewhere/else$ make -C /path/to/kernel ARCH=arm CROSS_COMPILE=arm-eabi- M=/your/module/dir modules
This Makefile will solve your query
obj-m += hello-1.o
all:
make ARCH=arm CROSS_COMPILE=arm-eabi- -C /work/TI-Android-ICS-4.0.3_AM37x_3.0.0/kernel M=$(PWD) modules_install INSTALL_MOD_PATH=<output directory for modules>
clean:
make ARCH=arm CROSS_COMPILE=arm-eabi- -C /work/TI-Android-ICS-4.0.3_AM37x_3.0.0/kernel M=$(PWD) clean

Resources