Difference between .o and .ko file

后端 未结 2 1801
情书的邮戳
情书的邮戳 2021-01-30 10:20

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 i

相关标签:
2条回答
  • 2021-01-30 11:00

    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.

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

    0 讨论(0)
  • 2021-01-30 11:05

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题