Introduction
A .ko file (“kernel object”) is a loadable module for the
Linux kernel: a driver, file system or other
feature that the kernel can load while running instead of having it built in. Modules
are installed under /lib/modules/ followed by the kernel version, and
tools such as modprobe load them along with the modules they depend
on.
A module only loads into the kernel version it was built for, unless the kernel was
built with symbol versioning and the symbols it uses match. Distributions often store
modules compressed, as .ko.xz, .ko.zst or
.ko.gz.
Structure
A kernel module is an ELF relocatable object file,
the same kind as a .o file, which the kernel links into itself when it
loads the module. A few sections are special to modules:
.modinfoholds text entries such aslicense=GPL,description=,depends=andvermagic=, the kernel version and options the module was built for.modinfoprints them..gnu.linkonce.this_moduleholds the module’sstruct module, with its name and its init and exit functions.__versions, when symbol versioning is on, lists a checksum for each kernel symbol the module uses.
A signed module has its signature appended after the ELF data, followed by the text
~Module signature appended~ and a newline at the very end of the
file.
Every module section, every .modinfo key the build writes, the
vermagic string and the __versions entries are on the
module sections page, and the appended
signature block on signature. The ELF
structures themselves are on the ELF pages.
Not described
Known, not yet covered
- Reference: the layouts of
struct module,kernel_paramandksymtabas the kernel source defines them, and compressed modules (.ko.xz,.ko.zst); see sections. - Viewer: a
.koopens in the ELF viewer as a relocatable object: header, sections, symbols and relocations are decoded..modinfois listed as itskey=valuestrings (checked againstllvm-readelf -p .modinfoon the real module);.gnu.linkonce.this_module,__param,__versionsand the module signature are shown as bytes, not decoded. The only real module on this Mac is a 2008 ARM module (Linux 2.6.24); no x86-64 or signed module was checked, and the viewer has not been tried in a browser.
Also used for
- Executable and Linkable Format – Programs, libraries and object files on Linux and Unix systems.
Links
Software
- kmod – the module tools
modprobe,insmod,rmmod,lsmodandmodinfo(Linux) - GNU Binutils – GNU’s binary tools, with
readelfandobjdump(Linux, macOS, Windows)
Reference
- Building External Modules – Linux kernel documentation on building modules
- Kernel module signing facility – Linux kernel documentation on signed modules