.o和.ko文件的区别。

时间:2022-06-09 03:47:18

I am writing simple Linux module mod.c. When I compile mod.c file, it creates two output file mod.o and mod.ko. So I just want to know, What is the difference between mod.o and mod.ko file?

我正在编写简单的Linux模块mod.c。当我编译mod.c文件时,它会创建两个输出文件mod.o和mod.ko。所以我想知道,mod.o和。ko文件的区别是什么?

2 个解决方案

#1


36  

The short answer is that the .ko file is your object file linked with some kernel automatically generated data structures that are needed by the kernel.

简短的回答是,.ko文件是您的对象文件,它与内核所需要的一些内核自动生成的数据结构相关联。

The .o file is the object file of your module - the result of compiling your C file. The kernel build system then automatically creates another C file with some data structures describing the kernel module (named your_module_kmod.c), compile this C file into another object file and links your object file and the object file it built together to create the .ko file.

o文件是模块的对象文件——编译C文件的结果。然后,内核构建系统会自动创建另一个C文件,其中包含一些描述内核模块的数据结构(命名为your_module_kmod.c),将这个C文件编译成另一个对象文件,并将对象文件和它构建的对象文件链接起来,创建.ko文件。

The dynamic linker in the kernel that is in charge of loading kernel modules, expects to find the data structure the kernel put in the kmod object in the .ko file and will not be able to load your kernel module without them.

内核中负责加载内核模块的动态链接器,期望找到内核在.ko文件中放入kmod对象的数据结构,如果没有它们,将无法加载内核模块。

#2


13  

Before Linux 2.6, a user space program would interpret the ELF object (.o) file and do all the work of linking it to the running kernel, generating a finished binary image. The program would pass that image to the kernel and the kernel would do little more than stick it in memory. In Linux 2.6, the kernel does the linking. A user space program passes the contents of the ELF object file directly to the kernel. For this to work, the ELF object image must contain additional information. To identify this particular kind of ELF object file, we name the file with suffix ".ko" ("kernel object") instead of ".o" For example, the serial device driver that in Linux 2.4 lived in the file serial.o in Linux 2.6 lives in the file serial.ko.

在Linux 2.6之前,用户空间程序将解释ELF对象(.o)文件,并完成所有将其链接到运行内核的工作,生成一个已完成的二进制映像。这个程序会把这个图像传递给内核,内核只会把它留在内存中。在Linux 2.6中,内核执行链接。用户空间程序将ELF对象文件的内容直接传递给内核。为此,ELF对象映像必须包含其他信息。为了识别这种特殊类型的ELF对象文件,我们用后缀“来命名文件”。(“内核对象”)而不是“。例如,在Linux 2.4中,串行设备驱动程序生活在文件序列中。o在Linux 2.6中,生活在文件系列中。

from http://tldp.org/HOWTO/Module-HOWTO/linuxversions.html .

从http://tldp.org/HOWTO/Module-HOWTO/linuxversions.html。

#1


36  

The short answer is that the .ko file is your object file linked with some kernel automatically generated data structures that are needed by the kernel.

简短的回答是,.ko文件是您的对象文件,它与内核所需要的一些内核自动生成的数据结构相关联。

The .o file is the object file of your module - the result of compiling your C file. The kernel build system then automatically creates another C file with some data structures describing the kernel module (named your_module_kmod.c), compile this C file into another object file and links your object file and the object file it built together to create the .ko file.

o文件是模块的对象文件——编译C文件的结果。然后,内核构建系统会自动创建另一个C文件,其中包含一些描述内核模块的数据结构(命名为your_module_kmod.c),将这个C文件编译成另一个对象文件,并将对象文件和它构建的对象文件链接起来,创建.ko文件。

The dynamic linker in the kernel that is in charge of loading kernel modules, expects to find the data structure the kernel put in the kmod object in the .ko file and will not be able to load your kernel module without them.

内核中负责加载内核模块的动态链接器,期望找到内核在.ko文件中放入kmod对象的数据结构,如果没有它们,将无法加载内核模块。

#2


13  

Before Linux 2.6, a user space program would interpret the ELF object (.o) file and do all the work of linking it to the running kernel, generating a finished binary image. The program would pass that image to the kernel and the kernel would do little more than stick it in memory. In Linux 2.6, the kernel does the linking. A user space program passes the contents of the ELF object file directly to the kernel. For this to work, the ELF object image must contain additional information. To identify this particular kind of ELF object file, we name the file with suffix ".ko" ("kernel object") instead of ".o" For example, the serial device driver that in Linux 2.4 lived in the file serial.o in Linux 2.6 lives in the file serial.ko.

在Linux 2.6之前,用户空间程序将解释ELF对象(.o)文件,并完成所有将其链接到运行内核的工作,生成一个已完成的二进制映像。这个程序会把这个图像传递给内核,内核只会把它留在内存中。在Linux 2.6中,内核执行链接。用户空间程序将ELF对象文件的内容直接传递给内核。为此,ELF对象映像必须包含其他信息。为了识别这种特殊类型的ELF对象文件,我们用后缀“来命名文件”。(“内核对象”)而不是“。例如,在Linux 2.4中,串行设备驱动程序生活在文件序列中。o在Linux 2.6中,生活在文件系列中。

from http://tldp.org/HOWTO/Module-HOWTO/linuxversions.html .

从http://tldp.org/HOWTO/Module-HOWTO/linuxversions.html。