xref: /linux/Documentation/livepatch/module-elf-format.rst (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
13b812eccSJessica Yu===========================
23b812eccSJessica YuLivepatch module Elf format
33b812eccSJessica Yu===========================
43b812eccSJessica Yu
53b812eccSJessica YuThis document outlines the Elf format requirements that livepatch modules must follow.
63b812eccSJessica Yu
789e33ea7SMauro Carvalho Chehab
889e33ea7SMauro Carvalho Chehab.. Table of Contents
989e33ea7SMauro Carvalho Chehab
10*7af6fbddSMark Brown.. contents:: :local:
11*7af6fbddSMark Brown
123b812eccSJessica Yu
13d9defe44SPetr Mladek1. Background and motivation
14d9defe44SPetr Mladek============================
153b812eccSJessica Yu
163b812eccSJessica YuFormerly, livepatch required separate architecture-specific code to write
173b812eccSJessica Yurelocations. However, arch-specific code to write relocations already
183b812eccSJessica Yuexists in the module loader, so this former approach produced redundant
193b812eccSJessica Yucode. So, instead of duplicating code and re-implementing what the module
203b812eccSJessica Yuloader can already do, livepatch leverages existing code in the module
213b812eccSJessica Yuloader to perform the all the arch-specific relocation work. Specifically,
223b812eccSJessica Yulivepatch reuses the apply_relocate_add() function in the module loader to
233b812eccSJessica Yuwrite relocations. The patch module Elf format described in this document
243b812eccSJessica Yuenables livepatch to be able to do this. The hope is that this will make
253b812eccSJessica Yulivepatch more easily portable to other architectures and reduce the amount
263b812eccSJessica Yuof arch-specific code required to port livepatch to a particular
273b812eccSJessica Yuarchitecture.
283b812eccSJessica Yu
293b812eccSJessica YuSince apply_relocate_add() requires access to a module's section header
303b812eccSJessica Yutable, symbol table, and relocation section indices, Elf information is
315ad75fcdSJessica Yupreserved for livepatch modules (see section 5). Livepatch manages its own
323b812eccSJessica Yurelocation sections and symbols, which are described in this document. The
333b812eccSJessica YuElf constants used to mark livepatch symbols and relocation sections were
343b812eccSJessica Yuselected from OS-specific ranges according to the definitions from glibc.
353b812eccSJessica Yu
36d9defe44SPetr MladekWhy does livepatch need to write its own relocations?
37d9defe44SPetr Mladek-----------------------------------------------------
383b812eccSJessica YuA typical livepatch module contains patched versions of functions that can
393b812eccSJessica Yureference non-exported global symbols and non-included local symbols.
403b812eccSJessica YuRelocations referencing these types of symbols cannot be left in as-is
413b812eccSJessica Yusince the kernel module loader cannot resolve them and will therefore
423b812eccSJessica Yureject the livepatch module. Furthermore, we cannot apply relocations that
433b812eccSJessica Yuaffect modules not yet loaded at patch module load time (e.g. a patch to a
443b812eccSJessica Yudriver that is not loaded). Formerly, livepatch solved this problem by
453b812eccSJessica Yuembedding special "dynrela" (dynamic rela) sections in the resulting patch
463b812eccSJessica Yumodule Elf output. Using these dynrela sections, livepatch could resolve
473b812eccSJessica Yusymbols while taking into account its scope and what module the symbol
483b812eccSJessica Yubelongs to, and then manually apply the dynamic relocations. However this
493b812eccSJessica Yuapproach required livepatch to supply arch-specific code in order to write
503b812eccSJessica Yuthese relocations. In the new format, livepatch manages its own SHT_RELA
513b812eccSJessica Yurelocation sections in place of dynrela sections, and the symbols that the
523b812eccSJessica Yurelas reference are special livepatch symbols (see section 2 and 3). The
533b812eccSJessica Yuarch-specific livepatch relocation code is replaced by a call to
543b812eccSJessica Yuapply_relocate_add().
553b812eccSJessica Yu
56d9defe44SPetr Mladek2. Livepatch modinfo field
57d9defe44SPetr Mladek==========================
583b812eccSJessica Yu
593b812eccSJessica YuLivepatch modules are required to have the "livepatch" modinfo attribute.
603b812eccSJessica YuSee the sample livepatch module in samples/livepatch/ for how this is done.
613b812eccSJessica Yu
623b812eccSJessica YuLivepatch modules can be identified by users by using the 'modinfo' command
633b812eccSJessica Yuand looking for the presence of the "livepatch" field. This field is also
643b812eccSJessica Yuused by the kernel module loader to identify livepatch modules.
653b812eccSJessica Yu
66d9defe44SPetr MladekExample:
67d9defe44SPetr Mladek--------
68d9defe44SPetr Mladek
69d9defe44SPetr Mladek**Modinfo output:**
7089e33ea7SMauro Carvalho Chehab
7189e33ea7SMauro Carvalho Chehab::
7289e33ea7SMauro Carvalho Chehab
733b812eccSJessica Yu	% modinfo livepatch-meminfo.ko
743b812eccSJessica Yu	filename:		livepatch-meminfo.ko
753b812eccSJessica Yu	livepatch:		Y
763b812eccSJessica Yu	license:		GPL
773b812eccSJessica Yu	depends:
783b812eccSJessica Yu	vermagic:		4.3.0+ SMP mod_unload
793b812eccSJessica Yu
80d9defe44SPetr Mladek3. Livepatch relocation sections
81d9defe44SPetr Mladek================================
823b812eccSJessica Yu
833b812eccSJessica YuA livepatch module manages its own Elf relocation sections to apply
843b812eccSJessica Yurelocations to modules as well as to the kernel (vmlinux) at the
853b812eccSJessica Yuappropriate time. For example, if a patch module patches a driver that is
863b812eccSJessica Yunot currently loaded, livepatch will apply the corresponding livepatch
873b812eccSJessica Yurelocation section(s) to the driver once it loads.
883b812eccSJessica Yu
893b812eccSJessica YuEach "object" (e.g. vmlinux, or a module) within a patch module may have
903b812eccSJessica Yumultiple livepatch relocation sections associated with it (e.g. patches to
913b812eccSJessica Yumultiple functions within the same object). There is a 1-1 correspondence
923b812eccSJessica Yubetween a livepatch relocation section and the target section (usually the
933b812eccSJessica Yutext section of a function) to which the relocation(s) apply. It is
943b812eccSJessica Yualso possible for a livepatch module to have no livepatch relocation
953b812eccSJessica Yusections, as in the case of the sample livepatch module (see
963b812eccSJessica Yusamples/livepatch).
973b812eccSJessica Yu
985ad75fcdSJessica YuSince Elf information is preserved for livepatch modules (see Section 5), a
993b812eccSJessica Yulivepatch relocation section can be applied simply by passing in the
1003b812eccSJessica Yuappropriate section index to apply_relocate_add(), which then uses it to
1013b812eccSJessica Yuaccess the relocation section and apply the relocations.
1023b812eccSJessica Yu
1033b812eccSJessica YuEvery symbol referenced by a rela in a livepatch relocation section is a
1043b812eccSJessica Yulivepatch symbol. These must be resolved before livepatch can call
1053b812eccSJessica Yuapply_relocate_add(). See Section 3 for more information.
1063b812eccSJessica Yu
107d9defe44SPetr Mladek3.1 Livepatch relocation section format
108d9defe44SPetr Mladek=======================================
1093b812eccSJessica Yu
1103b812eccSJessica YuLivepatch relocation sections must be marked with the SHF_RELA_LIVEPATCH
1113b812eccSJessica Yusection flag. See include/uapi/linux/elf.h for the definition. The module
1123b812eccSJessica Yuloader recognizes this flag and will avoid applying those relocation sections
1133b812eccSJessica Yuat patch module load time. These sections must also be marked with SHF_ALLOC,
1143b812eccSJessica Yuso that the module loader doesn't discard them on module load (i.e. they will
1153b812eccSJessica Yube copied into memory along with the other SHF_ALLOC sections).
1163b812eccSJessica Yu
11789e33ea7SMauro Carvalho ChehabThe name of a livepatch relocation section must conform to the following
11889e33ea7SMauro Carvalho Chehabformat::
1193b812eccSJessica Yu
1203b812eccSJessica Yu  .klp.rela.objname.section_name
1213b812eccSJessica Yu  ^        ^^     ^ ^          ^
1223b812eccSJessica Yu  |________||_____| |__________|
1233b812eccSJessica Yu     [A]      [B]        [C]
1243b812eccSJessica Yu
125d9defe44SPetr Mladek[A]
126d9defe44SPetr Mladek  The relocation section name is prefixed with the string ".klp.rela."
1273b812eccSJessica Yu
128d9defe44SPetr Mladek[B]
129d9defe44SPetr Mladek  The name of the object (i.e. "vmlinux" or name of module) to
130d9defe44SPetr Mladek  which the relocation section belongs follows immediately after the prefix.
131d9defe44SPetr Mladek
132d9defe44SPetr Mladek[C]
133d9defe44SPetr Mladek  The actual name of the section to which this relocation section applies.
134d9defe44SPetr Mladek
135d9defe44SPetr MladekExamples:
136d9defe44SPetr Mladek---------
137d9defe44SPetr Mladek
138d9defe44SPetr Mladek**Livepatch relocation section names:**
139d9defe44SPetr Mladek
140d9defe44SPetr Mladek::
141d9defe44SPetr Mladek
1423b812eccSJessica Yu  .klp.rela.ext4.text.ext4_attr_store
1433b812eccSJessica Yu  .klp.rela.vmlinux.text.cmdline_proc_show
1443b812eccSJessica Yu
145d9defe44SPetr Mladek**`readelf --sections` output for a patch
146d9defe44SPetr Mladekmodule that patches vmlinux and modules 9p, btrfs, ext4:**
14789e33ea7SMauro Carvalho Chehab
14889e33ea7SMauro Carvalho Chehab::
14989e33ea7SMauro Carvalho Chehab
1503b812eccSJessica Yu  Section Headers:
1513b812eccSJessica Yu  [Nr] Name                          Type                    Address          Off    Size   ES Flg Lk Inf Al
1523b812eccSJessica Yu  [ snip ]
1533b812eccSJessica Yu  [29] .klp.rela.9p.text.caches.show RELA                    0000000000000000 002d58 0000c0 18 AIo 64   9  8
1543b812eccSJessica Yu  [30] .klp.rela.btrfs.text.btrfs.feature.attr.show RELA     0000000000000000 002e18 000060 18 AIo 64  11  8
1553b812eccSJessica Yu  [ snip ]
1563b812eccSJessica Yu  [34] .klp.rela.ext4.text.ext4.attr.store RELA              0000000000000000 002fd8 0000d8 18 AIo 64  13  8
1573b812eccSJessica Yu  [35] .klp.rela.ext4.text.ext4.attr.show RELA               0000000000000000 0030b0 000150 18 AIo 64  15  8
1583b812eccSJessica Yu  [36] .klp.rela.vmlinux.text.cmdline.proc.show RELA         0000000000000000 003200 000018 18 AIo 64  17  8
1593b812eccSJessica Yu  [37] .klp.rela.vmlinux.text.meminfo.proc.show RELA         0000000000000000 003218 0000f0 18 AIo 64  19  8
1603b812eccSJessica Yu  [ snip ]                                       ^                                             ^
1613b812eccSJessica Yu                                                 |                                             |
1623b812eccSJessica Yu                                                [*]                                           [*]
163d9defe44SPetr Mladek
164d9defe44SPetr Mladek[*]
165d9defe44SPetr Mladek  Livepatch relocation sections are SHT_RELA sections but with a few special
1663b812eccSJessica Yu  characteristics. Notice that they are marked SHF_ALLOC ("A") so that they will
1673b812eccSJessica Yu  not be discarded when the module is loaded into memory, as well as with the
1683b812eccSJessica Yu  SHF_RELA_LIVEPATCH flag ("o" - for OS-specific).
1693b812eccSJessica Yu
170d9defe44SPetr Mladek**`readelf --relocs` output for a patch module:**
17189e33ea7SMauro Carvalho Chehab
17289e33ea7SMauro Carvalho Chehab::
17389e33ea7SMauro Carvalho Chehab
1743b812eccSJessica Yu  Relocation section '.klp.rela.btrfs.text.btrfs_feature_attr_show' at offset 0x2ba0 contains 4 entries:
1753b812eccSJessica Yu      Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
1763b812eccSJessica Yu  000000000000001f  0000005e00000002 R_X86_64_PC32          0000000000000000 .klp.sym.vmlinux.printk,0 - 4
1773b812eccSJessica Yu  0000000000000028  0000003d0000000b R_X86_64_32S           0000000000000000 .klp.sym.btrfs.btrfs_ktype,0 + 0
1783b812eccSJessica Yu  0000000000000036  0000003b00000002 R_X86_64_PC32          0000000000000000 .klp.sym.btrfs.can_modify_feature.isra.3,0 - 4
1793b812eccSJessica Yu  000000000000004c  0000004900000002 R_X86_64_PC32          0000000000000000 .klp.sym.vmlinux.snprintf,0 - 4
1803b812eccSJessica Yu  [ snip ]                                                                   ^
1813b812eccSJessica Yu                                                                             |
1823b812eccSJessica Yu                                                                            [*]
1833b812eccSJessica Yu
184d9defe44SPetr Mladek[*]
185d9defe44SPetr Mladek  Every symbol referenced by a relocation is a livepatch symbol.
1863b812eccSJessica Yu
187d9defe44SPetr Mladek4. Livepatch symbols
188d9defe44SPetr Mladek====================
189d9defe44SPetr Mladek
1903b812eccSJessica YuLivepatch symbols are symbols referred to by livepatch relocation sections.
1913b812eccSJessica YuThese are symbols accessed from new versions of functions for patched
1923b812eccSJessica Yuobjects, whose addresses cannot be resolved by the module loader (because
1933b812eccSJessica Yuthey are local or unexported global syms). Since the module loader only
1943b812eccSJessica Yuresolves exported syms, and not every symbol referenced by the new patched
1953b812eccSJessica Yufunctions is exported, livepatch symbols were introduced. They are used
1963b812eccSJessica Yualso in cases where we cannot immediately know the address of a symbol when
1973b812eccSJessica Yua patch module loads. For example, this is the case when livepatch patches
1983b812eccSJessica Yua module that is not loaded yet. In this case, the relevant livepatch
1993b812eccSJessica Yusymbols are resolved simply when the target module loads. In any case, for
2003b812eccSJessica Yuany livepatch relocation section, all livepatch symbols referenced by that
2013b812eccSJessica Yusection must be resolved before livepatch can call apply_relocate_add() for
2023b812eccSJessica Yuthat reloc section.
2033b812eccSJessica Yu
2043b812eccSJessica YuLivepatch symbols must be marked with SHN_LIVEPATCH so that the module
2053b812eccSJessica Yuloader can identify and ignore them. Livepatch modules keep these symbols
2063b812eccSJessica Yuin their symbol tables, and the symbol table is made accessible through
2073b812eccSJessica Yumodule->symtab.
2083b812eccSJessica Yu
209d9defe44SPetr Mladek4.1 A livepatch module's symbol table
210d9defe44SPetr Mladek=====================================
2113b812eccSJessica YuNormally, a stripped down copy of a module's symbol table (containing only
2123b812eccSJessica Yu"core" symbols) is made available through module->symtab (See layout_symtab()
2133b812eccSJessica Yuin kernel/module.c). For livepatch modules, the symbol table copied into memory
2143b812eccSJessica Yuon module load must be exactly the same as the symbol table produced when the
2153b812eccSJessica Yupatch module was compiled. This is because the relocations in each livepatch
2163b812eccSJessica Yurelocation section refer to their respective symbols with their symbol indices,
2173b812eccSJessica Yuand the original symbol indices (and thus the symtab ordering) must be
2183b812eccSJessica Yupreserved in order for apply_relocate_add() to find the right symbol.
2193b812eccSJessica Yu
22089e33ea7SMauro Carvalho ChehabFor example, take this particular rela from a livepatch module:::
22189e33ea7SMauro Carvalho Chehab
2223b812eccSJessica Yu  Relocation section '.klp.rela.btrfs.text.btrfs_feature_attr_show' at offset 0x2ba0 contains 4 entries:
2233b812eccSJessica Yu      Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
2243b812eccSJessica Yu  000000000000001f  0000005e00000002 R_X86_64_PC32          0000000000000000 .klp.sym.vmlinux.printk,0 - 4
2253b812eccSJessica Yu
2263b812eccSJessica Yu  This rela refers to the symbol '.klp.sym.vmlinux.printk,0', and the symbol index is encoded
2273b812eccSJessica Yu  in 'Info'. Here its symbol index is 0x5e, which is 94 in decimal, which refers to the
2283b812eccSJessica Yu  symbol index 94.
2293b812eccSJessica Yu  And in this patch module's corresponding symbol table, symbol index 94 refers to that very symbol:
2303b812eccSJessica Yu  [ snip ]
2313b812eccSJessica Yu  94: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.printk,0
2323b812eccSJessica Yu  [ snip ]
2333b812eccSJessica Yu
234d9defe44SPetr Mladek4.2 Livepatch symbol format
235d9defe44SPetr Mladek===========================
2363b812eccSJessica Yu
2373b812eccSJessica YuLivepatch symbols must have their section index marked as SHN_LIVEPATCH, so
2383b812eccSJessica Yuthat the module loader can identify them and not attempt to resolve them.
2393b812eccSJessica YuSee include/uapi/linux/elf.h for the actual definitions.
2403b812eccSJessica Yu
24189e33ea7SMauro Carvalho ChehabLivepatch symbol names must conform to the following format::
2423b812eccSJessica Yu
2433b812eccSJessica Yu  .klp.sym.objname.symbol_name,sympos
2443b812eccSJessica Yu  ^       ^^     ^ ^         ^ ^
2453b812eccSJessica Yu  |_______||_____| |_________| |
2463b812eccSJessica Yu     [A]     [B]       [C]    [D]
2473b812eccSJessica Yu
248d9defe44SPetr Mladek[A]
249d9defe44SPetr Mladek  The symbol name is prefixed with the string ".klp.sym."
250d9defe44SPetr Mladek
251d9defe44SPetr Mladek[B]
252d9defe44SPetr Mladek  The name of the object (i.e. "vmlinux" or name of module) to
2533b812eccSJessica Yu  which the symbol belongs follows immediately after the prefix.
254d9defe44SPetr Mladek
255d9defe44SPetr Mladek[C]
256d9defe44SPetr Mladek  The actual name of the symbol.
257d9defe44SPetr Mladek
258d9defe44SPetr Mladek[D]
259d9defe44SPetr Mladek  The position of the symbol in the object (as according to kallsyms)
2603b812eccSJessica Yu  This is used to differentiate duplicate symbols within the same
2613b812eccSJessica Yu  object. The symbol position is expressed numerically (0, 1, 2...).
2623b812eccSJessica Yu  The symbol position of a unique symbol is 0.
2633b812eccSJessica Yu
264d9defe44SPetr MladekExamples:
265d9defe44SPetr Mladek---------
266d9defe44SPetr Mladek
267d9defe44SPetr Mladek**Livepatch symbol names:**
26889e33ea7SMauro Carvalho Chehab
26989e33ea7SMauro Carvalho Chehab::
27089e33ea7SMauro Carvalho Chehab
2713b812eccSJessica Yu	.klp.sym.vmlinux.snprintf,0
2723b812eccSJessica Yu	.klp.sym.vmlinux.printk,0
2733b812eccSJessica Yu	.klp.sym.btrfs.btrfs_ktype,0
2743b812eccSJessica Yu
275d9defe44SPetr Mladek**`readelf --symbols` output for a patch module:**
27689e33ea7SMauro Carvalho Chehab
27789e33ea7SMauro Carvalho Chehab::
27889e33ea7SMauro Carvalho Chehab
2793b812eccSJessica Yu  Symbol table '.symtab' contains 127 entries:
2803b812eccSJessica Yu     Num:    Value          Size Type    Bind   Vis     Ndx         Name
2813b812eccSJessica Yu     [ snip ]
2823b812eccSJessica Yu      73: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.snprintf,0
2833b812eccSJessica Yu      74: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.capable,0
2843b812eccSJessica Yu      75: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.find_next_bit,0
2853b812eccSJessica Yu      76: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.si_swapinfo,0
2863b812eccSJessica Yu    [ snip ]                                               ^
2873b812eccSJessica Yu                                                           |
2883b812eccSJessica Yu                                                          [*]
289d9defe44SPetr Mladek
290d9defe44SPetr Mladek[*]
291d9defe44SPetr Mladek  Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
2923b812eccSJessica Yu  "OS" means OS-specific.
2933b812eccSJessica Yu
2941d05334dSPeter Zijlstra5. Symbol table and Elf section access
295d9defe44SPetr Mladek======================================
2963b812eccSJessica YuA livepatch module's symbol table is accessible through module->symtab.
2973b812eccSJessica Yu
2983b812eccSJessica YuSince apply_relocate_add() requires access to a module's section headers,
2993b812eccSJessica Yusymbol table, and relocation section indices, Elf information is preserved for
3003b812eccSJessica Yulivepatch modules and is made accessible by the module loader through
3013b812eccSJessica Yumodule->klp_info, which is a klp_modinfo struct. When a livepatch module loads,
30289e33ea7SMauro Carvalho Chehabthis struct is filled in by the module loader. Its fields are documented below::
3033b812eccSJessica Yu
3043b812eccSJessica Yu	struct klp_modinfo {
3053b812eccSJessica Yu		Elf_Ehdr hdr; /* Elf header */
3063b812eccSJessica Yu		Elf_Shdr *sechdrs; /* Section header table */
3073b812eccSJessica Yu		char *secstrings; /* String table for the section headers */
3083b812eccSJessica Yu		unsigned int symndx; /* The symbol table section index */
3093b812eccSJessica Yu	};
310