Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How linux compiles kernel modules in kernel 2.6

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how linux compiles kernel modules in kernel 2.6. what is introduced in this article is very detailed and has a certain reference value. Interested friends must read it!

The method of compiling kernel modules is slightly different from that of compiling general applications. We will find that Makefile exists in the directory of the kernel source tree. That is, these Makefile are organized at different levels. In previous kernel versions, compiling kernel modules was troublesome and required us to make a lot of changes to these Makefile. The kernel of 2.6 adopts "kbuild" compilation system, which simplifies these problems. For kbuild, refer to / Documentation/kbuild/modules.txt in the kernel source tree.

Before compiling, the source file must be required. These source files can be placed in the kernel source tree or anywhere outside the kernel source tree. Depending on the directory where the source file exists, there are two compilation methods: in and outside the source tree.

Compile the module in the source tree

The source code of the official kernel module is organized by module (driver) type. We can see subdirectories such as char, usb, block and so on in the drivers directory of the kernel source tree. Then when we add files to the kernel source tree, we also follow these classifications. The rules of classification should be grasped flexibly.

Let's take the previous simple module "hello, world" as an example to see how to compile kernel modules in the kernel source tree.

1, do not create new subdirectories

(1) first edit a c source program named hello.c in the drivers directory in the kernel source tree.

(2) modify the Makefile file of the drivers directory and add: obj-m + = hello.o

(3) recompile the kernel (return to the root of the source tree and run $sudo make).

In this way, there are several more files in the drivers directory: hello.mod.c, hello.mod.o, hello.o, hello.ko. Hello.ko is the compiled module.

2, new subdirectory

If you have more source files, you can create a new subdirectory in the drivers directory. Let's take hello and world as examples:

(1) create a new helo subdirectory in the drivers directory of the kernel source tree, and put hello.c in the hello directory.

(2) modify the Makefile file of the drivers directory and add: obj-m + = hello/

(3) create a new Makefile file in the hello directory with the following contents: obj-m + = hello.o

(4) recompile the kernel (return to the root of the source tree and run $sudo make).

In this way, the newly generated module file is located in the hello directory.

If you compile the kernel module in the kernel source tree, if you don't create a new subdirectory, you only need to modify the Makefile of the current directory, otherwise you should create a new Makefile in the new subdirectory to specify the compilation options, and modify the Makefile of the upper directory so that kbuild can enter the new subdirectory.

Compile the module outside the source tree

Or take the above hello, world as an example. There is a hello.c in the current directory:

(1) first, create a new Makefile in the directory where the module code is located, with the following contents:

Obj-m: = hello.o

(2) call the make command like this:

$sudo make-C / usr/local/src/kernel/linux-2.6.16.20 SUBDIRS=$PWD modules

Here / usr/local/src/kernel/linux-2.6.16.20 is the directory where the kernel source tree is located.

-C means that make is required to change to the directory specified by-C. SUBDIRS (you can also use M instead of SUBDIRS) returns make to the current directory before compiling the kernel module.

The whole compilation process actually executes the Makefile of the kernel source tree specified by-C and specifies the directory of the kernel source files you want to compile through SUBDIR.

Simplify command line input

It is troublesome to enter these parameters each time you call make, so you can rewrite Makefile to simplify:

Obj-m + = hello.oall: make-C / lib/modules/$ (shell uname-r) / build PWD $(PWD) modulesclean: make-C / lib/modules/$ (shell uname-r) / build Mouse $(PWD) clean

In this way, you can do the above work simply by calling $sudo make in the current directory. Calling $sudo make clean will delete all newly generated files.

The above Makefile determines where the kernel source tree is located: if we go to the / lib/modules directory first, we will see some directories with the name of the kernel version, in which there is a build file, which is a symbolic link to the kernel source tree. So how do you determine which kernel version of the directory to enter? This can be determined by $uname-r, which indicates the version of the kernel currently running.

You can also further simplify the Makefile:

Obj-m: = hello.o KERNELDIR? = / lib/modules/$ (shell uname-r) / build PWD: = $(shell pwd) default: $(MAKE)-C $(KERNELDIR) KERNELDIR $(PWD) modules clean: $(MAKE)-C $(KERNELDIR) Mouse $(PWD) clean

This eliminates the need to specify the directory of the kernel code tree again and again in Makefile.

In the above example, only all the code in one file is discussed. If the code is distributed in multiple source files, such as file1.c, file2.c, generate hello.ko. Makefile should be written like this:

Obj-m: = hello.o

Hello-objs: = file1.o file2.o

Note that although our goal is to generate a .ko file, it is written as .o in Makefile!

These are all the contents of the article "how linux compiles kernel modules in kernel 2.6". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report