65

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?

beparas
  • 1,927
  • 7
  • 24
  • 30
  • 2
    I think .ko are 2.6 kernel object files whereas .o are 2.4. Read more about it here: http://tldp.org/HOWTO/Module-HOWTO/linuxversions.html – smichak May 07 '12 at 05:19

3 Answers3

65

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.

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
gby
  • 14,900
  • 40
  • 57
21

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 .

Raulp
  • 7,758
  • 20
  • 93
  • 155
0

.ko is the extension of a module file and .o is the extension of an object file.

An object file is created from compiling source files, with .c and .h extensions.

A module file is created by linking relevant object files and necessary dependencies.

The module file is a loadable kernel module (LKM) that can be dynamically loaded and unloaded to a running kernel without restarting the computer. The object files serve as the building blocks for creating the module file.

Here is a line from the makefile of the scsi_mod module:

drivers/scsi/Makefile:scsi_mod-y += scsi.o hosts.o scsi_ioctl.o

The above line has been taken from the Makefile that is located under drivers/scsi location.

scsi_mod is the module file that is created by linking the following object files: scsi.o, hosts.o and scsi_ioctl.o

Asif
  • 664
  • 1
  • 7
  • 14