A .ko file is an ELF relocatable object
(ET_REL); its header, sections, symbols and relocations follow the
ELF reference. What makes it a module is a set
of sections written by the kernel build: the build tool modpost generates a
.mod.c file for each module that is compiled and linked into it. The
kernel does not publish a separate file specification; this page is drawn from the
kernel’s headers, modpost and its documentation (see
sources).
Module sections
| Section | Contents |
|---|---|
.modinfo | Key–value strings about the module. Must have contents (a NOBITS .modinfo is an error). |
.gnu.linkonce.this_module | The module’s struct module, __this_module. |
__versions | Symbol CRCs of imported symbols (basic modversions). |
__version_ext_crcs, __version_ext_names | Extended modversions tables. |
__param | One struct kernel_param per module parameter. |
__modver | A version attribute, for code built into the kernel rather than as a module. |
__ksymtab, __kflagstab, __ksymtab_strings | Symbols the module exports; names and namespaces in __ksymtab_strings. |
__kcrctab | CRCs of exported symbols, with CONFIG_MODVERSIONS. |
.export_symbol | Export records checked by modpost; labels start __export_symbol_. |
.no_trim_symbol | Symbols to keep when unused exports are trimmed. |
The layouts of struct kernel_param, __ksymtab and
struct module are kernel-internal and change between kernel versions;
they are not specified as a file format.
.modinfo
A series of strings, each key=value followed by a
zero byte, with no padding (alignment 1). Each comes from a MODULE_INFO(tag,
info) in the source or in the generated .mod.c; values may not
contain zero bytes.
| Key | Written by | Value |
|---|---|---|
license | MODULE_LICENSE | Licence. Accepted free licences: GPL, GPL v2, GPL and additional rights, Dual BSD/GPL, Dual MIT/GPL, Dual MPL/GPL; Proprietary marks a non-free module. |
author | MODULE_AUTHOR | Author. |
description | MODULE_DESCRIPTION | Description. |
alias | MODULE_ALIAS | Another name the module can be loaded by. |
softdep | MODULE_SOFTDEP | Soft dependency. |
firmware | MODULE_FIRMWARE | A firmware file the module may load. |
import_ns | MODULE_IMPORT_NS | A symbol namespace the module imports. |
version | MODULE_VERSION | The module’s own version. |
parm | MODULE_PARM_DESC | param:description |
parmtype | __MODULE_PARM_TYPE | param:type |
name | modpost | The module name (KBUILD_MODNAME). |
depends | modpost | Comma-separated names of the modules that provide symbols it uses. |
intree | modpost | Y for a module built inside the kernel tree. |
staging | modpost | Y for a module under drivers/staging. |
test | modpost | Y for a module under tools/testing. |
srcversion | modpost | A checksum of the module’s source, when enabled. |
vermagic | kernel build (not confirmed which file writes it; vermagic.h may only be included from the kernel’s module loader or a .mod.c) | The version magic string. |
vermagic
VERMAGIC_STRING is the kernel release followed by a space and then, each
only if the option is on:
| Text | Option |
|---|---|
SMP | CONFIG_SMP |
preempt_rt or preempt | CONFIG_PREEMPT_RT, CONFIG_PREEMPT_BUILD |
mod_unload | CONFIG_MODULE_UNLOAD |
modversions | CONFIG_MODVERSIONS |
| architecture text | MODULE_ARCH_VERMAGIC, from the architecture’s asm/vermagic.h |
RANDSTRUCT_seed hash | structure layout randomisation |
For example vermagic=6.8.0-45-generic SMP preempt mod_unload modversions
would describe a kernel with those options (an illustration built from the rules
above).
.gnu.linkonce.this_module
Holds the symbol __this_module, a struct module. The
generated .mod.c sets only name (the module name),
init (init_module, if the module has one), exit
(cleanup_module, if it has one and CONFIG_MODULE_UNLOAD is
on) and arch; the rest is zero. The offsets of these fields depend on the
kernel version and configuration, so the section can only be decoded with that
kernel’s headers.
__versions
With CONFIG_MODVERSIONS each exported symbol has a CRC computed over its
full prototype. With basic modversions (the default), a module lists, for every
kernel symbol it imports, the CRC it was built against; the kernel refuses to load the
module if a CRC differs. The section is an array of:
| Field | Size (64-bit) | Size (32-bit) | Meaning |
|---|---|---|---|
unsigned long crc | 8 | 4 | CRC of the symbol |
char name[MODULE_NAME_LEN] | 56 | 60 | Symbol name, zero-terminated |
MODULE_NAME_LEN is 64 − sizeof(unsigned long), so each
entry is 64 bytes and names are limited to 55 or 59 characters. The symbol
module_layout is included when modversions is on. Values are in the
target’s byte order.
Extended modversions
With CONFIG_EXTENDED_MODVERSIONS (needed for Rust together with
modversions), longer names are possible: __version_ext_crcs is an array
of 32-bit CRCs and __version_ext_names the matching names, each ending in
a zero byte, in the same order.
Module.symvers
Not part of a .ko, but written by the same build: a text file with one
line per exported symbol and five tab-separated columns: CRC, symbol, module, export
type and namespace (which may be empty). The CRC is 0x00000000 without
CONFIG_MODVERSIONS.
Sources
- scripts/mod/modpost.c – the generated
.mod.c:name,intree,staging,test,depends,srcversion,__this_module,__versions, the extended tables, checked sections - include/linux/module.h –
struct modversion_info, theMODULE_macros and licence strings - include/linux/moduleparam.h –
MODULE_INFOand.modinfo,parm,parmtype,__MODULE_NAME_LEN,__param - include/linux/vermagic.h –
VERMAGIC_STRING - Building External Modules – modversions,
__ksymtab,__kcrctab,__versions, extended modversions,Module.symvers