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 module files are compiled to the kernel and independently compiled into modules

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

Share

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

This article mainly introduces "how Linux module files are compiled into the kernel and independently compiled into modules". In daily operations, I believe many people have doubts about how Linux module files are compiled into the kernel and independently compiled into modules. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how Linux module files are compiled into the kernel and independently compiled into modules". Next, please follow the editor to study!

1. Kernel directory

The source code of the Linux kernel is very large, increasing with the development of the version. It uses a directory tree structure and organizes configuration and compilation using Makefile.

For the first time in the Linux kernel, read carefully the readme file in the top-level directory, which is an overview of the Linux kernel and instructions for compilation commands. Readme's description focuses on general-purpose platforms such as X86, and there may be some special instructions for some special architectures.

The Makefile of the top-level directory is the core file for the compilation of the whole kernel configuration, which is responsible for organizing the compilation and management of the subdirectories of the directory tree, and can also set the architecture and version number.

There are many subdirectories at the top of the kernel source code, which organize and store various kernel subsystems or files. The specific catalog description is shown in the table below.

two。 Compilation tool

1.make mrproper: clear the configuration files and object files generated by the kernel, etc., usually used when compiling for the first time

two。 Import default configuration information (in kernel root)

A) make xxx_deconfig b) cp arch/arm/configs/xx_deconfig .config generates the default configuration file

3. Configuration command

Make xxxxconfig modify configuration file make xconfig (graphical interface qt library) make menuconfig (commonly used libncurses library) sudo apt-get install libncurses5-dev make config (simplified)

4. Compile the kernel

Make uImage-generate kernel image / arch/arm/boot/uImage

5. Compile device tree

Make dtbs-generate device tree file / arch/arm/boot/dtb/xxxxxx.dtb

6. Compile and generate module files

Make modules-select the configuration value as M's code to compile and generate module files. (.ko) under the corresponding source directory. 3. Kernel compilation

Now many Linux-based product development, usually manufacturers will provide integrated development environment SDK. Builroot makes it easier for us to build the environment, but as beginners we still need to know how to compile kernel source code independently.

0) prerequisites

The cross-compilation tool chain must be installed first. For the installation of the cross-compilation tool chain, please refer to "linux Environment Building-ubuntu16.04 installation"

We are using arm-none-linux-gnueabi-gcc here.

1) download kernel source code

Download address: https://mirrors.edge.kernel.org/pub/linux/kernel/

We download the Linux-3.14 kernel (which can be a later version) to the / home/peng directory.

Or directly click on the link below https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.14.10.tar.xz

Unpack the package and enter the kernel source directory as follows:

$tar xvf linux-3.14.tar.xz $cd linux-3.142) modify the Makefile under the root of the kernel directory tree to indicate the cross compiler: $vim Makefile

Find ARCH and CROSS_COMPILE, and modify:

ARCH? = (SUBARCH) CROSS_COMPILE? = $(CONFIG_CROSS_COMPILE: "%" =%)

For

ARCH? = arm CROSS_COMPILE? = arm-none-linux-gnueabi-

3) configure the kernel to generate .config files:

Import default configuration

$make exynos_defconfig

Here we assume that the kernel to be compiled will eventually run on Samsung's board. The name of soc is exynos. Samsung has actually placed its own configuration file in. / arch/arm/configs/exynos_defconfig.

Executing this command will eventually generate a .config file under the kernel root.

We rely entirely on this file to compile the kernel. This file is some of the kernel module macro definitions and parameter settings required by the exynos development board, these values are an initial configuration given by the manufacturer. In the actual project development, we need to re-migrate the corresponding driver module based on this configuration file.

4) configure kernel module

Enter the kernel configuration command to select the kernel options as follows:

$make menuconfig

After the command is executed successfully, you will see the interface shown in the following figure. In fact, we have seen the interface of the same function in figure 1.5, which is also the kernel option configuration interface, but that interface can only be executed under X-window.

Where:

1. Submenu->

Indicates that there is a submenu. Press enter to enter the submenu.

two。 Square brackets [] there are parentheses before each option, some brackets, some angle brackets, and some parentheses.

[] indicates that there are only two options for this option, with either empty or "*" in square brackets

Use the spacebar to make a choice.

3. Angle brackets

When choosing the appropriate configuration, there are three options, which represent the following meanings.

● *: compile this feature into the kernel. ● null: this feature is not compiled into the kernel. ● M: compile this feature into a module that can be dynamically inserted into the kernel as needed.

4. The module configures parentheses () and the contents of parentheses require you to select one of the options provided.

If you are using make xconfig, you can use the mouse to select the corresponding options. If you are using make menuconfig, you need to use the enter key to select.

In the process of compiling the kernel, the trouble is to configure this step. Developers who are new to the Linux kernel often don't know how to choose these options.

In fact, when configuring, most options can use their default values, and only a few need to be selected according to the different needs of the user.

The principle of selection is to compile some functional codes that are far away from other parts of the kernel and are not frequently used into loadable modules, which helps to reduce the length of the kernel and reduce the memory consumed by the kernel. simplify the impact on the kernel when the corresponding environment changes; do not select the functions that are not needed; some functional codes that are closely related to the kernel and are often used are compiled directly into the kernel.

5) compile the kernel:

Root@ubuntu:/home/peng/linux-3.14# make uImage

UImage

If there are no changes according to the default configuration, the compiled system will generate a uImage file in the arch/arm/boot directory that has just been generated.

6) download the Linux kernel

Because different boards correspond to different versions of uboot, the uboot commands for downloading programs will also be different. I won't discuss verification for the time being.

4. Compilation of independent drivers 1. Compiled into an independent module

Suppose we have the following driver to compile into a separate ko file that can be loaded into the development board

Hello.c

# include # include / / # include # include static int major = 237; static int minor = 0; static dev_t devno; struct device * class_dev = NULL; struct class * cls; static int hello_open (struct inode * inode, struct file * filep) {printk ("hello_open ()\ n"); return 0;} static int hello_release (struct inode * inode, struct file * filep) {printk ("hello_release ()\ n") Return 0;} # define KMAX_LEN 32 char kbuf [KMAX _ LEN+1] = "kernel"; / / read (fd,buff,40); static ssize_t hello_read (struct file * filep, char _ user * buf, size_t size, loff_t * pos) {int error; if (size > strlen (kbuf)) {size = strlen (kbuf);} if (copy_to_user (buf,kbuf, size)) {error =-EFAULT; return error } return size;} / / write (fd,buff,40); static ssize_t hello_write (struct file * filep, const char _ user * buf, size_t size, loff_t * pos) {int error; if (size > KMAX_LEN) {size = KMAX_LEN;} memset (kbuf,0,sizeof (kbuf)); if (copy_from_user (kbuf, buf, size)) {error =-EFAULT; return error } printk ("% s\ n", kbuf); return size;} static struct file_operations hello_ops = {.open = hello_open, .release = hello_release, .read = hello_read, .write = hello_write,}; static int hello_init (void) {int result; printk ("hello_init\ n"); result = register_chrdev (major, "hello", & hello_ops); if (result)

< 0) { printk("register_chrdev fail \n"); return result; } cls = class_create(THIS_MODULE, "hellocls"); if (IS_ERR(cls)) { printk(KERN_ERR "class_create() failed for cls\n"); result = PTR_ERR(cls); goto out_err_1; } devno = MKDEV(major, minor); class_dev = device_create(cls, NULL, devno, NULL, "hellodev"); if (IS_ERR(class_dev)) { result = PTR_ERR(class_dev); goto out_err_2; } return 0; out_err_2: class_destroy(cls); out_err_1: unregister_chrdev(major,"hello"); return result; } static void hello_exit(void) { printk("hello_exit \n"); device_destroy(cls, devno); class_destroy(cls); unregister_chrdev(major,"hello"); return; } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); //proc/devices 注意我们需要编写Makefile如下: ifneq ($(KERNELRELEASE),) obj-m:=hello.o else KDIR :=/home/peng/linux-3.14 PWD :=$(shell pwd) all: make -C $(KDIR) M=$(PWD) modules clean: rm -f *.ko *.o *.mod.o *.symvers *.cmd *.mod.c *.order endif 关于Makefile的详解,大家可以参考我们之前的文章 《手把手教Linux驱动1-模块化编程》 其中内核路径: KDIR :=/home/peng/linux-3.14 必须是我们刚才编译过的内核源码根目录。 编译时,程序可以放到其他目录下: 用file命令查看文件属性,是基于ARM的。该模块文件就是与前面编译的内核配套的驱动模块,如果开发板的内核版本与上面编译的版本号一致,那么该模块文件就可以在开发板上insmod。 2. 编译到内核 步骤: 1)拷贝文件 如果要将刚才的驱动程序直接编译到内核,那么我们必须把hello.c拷贝到内核的某个目录下。 字符设备可以考虑放到以下目录: linux-3.14/drivers/char 2)修改Makefile root@ubuntu:/home/peng/linux-3.14/drivers/char# vim Makefile 修改如下: 该行内容是根据宏CONFIG_HELLO来决定是否编译hello.c这个文件。 3)修改Kconfig 7 HELLO 取前面步骤CONFIG_HELLO下划线后面的字符串 8 tristate 表示该模块最终有3个选项 空 * M 9 表示该模块依赖的模块,如果ARCH_EXYNOS4模块没有被选中,那么HELLO模块也不会被编译到内核 10 帮助信息 4) 重新配置 执行 make menuconfig 进入配置页面, 输入 / 可以根据关键字查找模块所在位置。 我们添加的模块文件的位置: 根据路径 ->

Device Drivers-> Character devices

Find the module configuration path we just had

Here are angle brackets because the property we set is tristate

When you move to Help, you can see the help we filled in earlier.

We can press the spacebar to set it to * and compile it into the kernel.

Select Save

Then click Exit twice to exit.

5) recompile the kernel

Root@ubuntu:/home/peng/linux-3.14# make uImage

In this way, our module is compiled into the newly generated kernel module file.

3. Supplement

The ultimate goal of the previous section is to generate the definition information CONFIG_HELLO=y and save it to the .config file in the kernel root directory.

In fact, if we do not modify the Kconfig, it is also possible to add this macro definition to .config.

That's all for today. What are you waiting for? Hurry up and start practicing.

The virtual machine used in this paper is called cross-compilation tool, as well as source code.

At this point, the study of "how Linux module files are compiled into the kernel and independently compiled into modules" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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