@@ -7,30 +7,18 @@ This document outlines the Elf format requirements that livepatch modules must f
77
88.. Table of Contents
99
10- 0. Background and motivation
11- 1. Livepatch modinfo field
12- 2. Livepatch relocation sections
13- 2.1 What are livepatch relocation sections?
14- 2.2 Livepatch relocation section format
15- 2.2.1 Required flags
16- 2.2.2 Required name format
17- 2.2.3 Example livepatch relocation section names
18- 2.2.4 Example `readelf --sections` output
19- 2.2.5 Example `readelf --relocs` output
20- 3. Livepatch symbols
21- 3.1 What are livepatch symbols?
22- 3.2 A livepatch module's symbol table
23- 3.3 Livepatch symbol format
24- 3.3.1 Required flags
25- 3.3.2 Required name format
26- 3.3.3 Example livepatch symbol names
27- 3.3.4 Example `readelf --symbols` output
28- 4. Architecture-specific sections
29- 5. Symbol table and Elf section access
30-
31- ----------------------------
32- 0. Background and motivation
33- ----------------------------
10+ 1. Background and motivation
11+ 2. Livepatch modinfo field
12+ 3. Livepatch relocation sections
13+ 3.1 Livepatch relocation section format
14+ 4. Livepatch symbols
15+ 4.1 A livepatch module's symbol table
16+ 4.2 Livepatch symbol format
17+ 5. Architecture-specific sections
18+ 6. Symbol table and Elf section access
19+
20+ 1. Background and motivation
21+ ============================
3422
3523Formerly, livepatch required separate architecture-specific code to write
3624relocations. However, arch-specific code to write relocations already
@@ -52,8 +40,8 @@ relocation sections and symbols, which are described in this document. The
5240Elf constants used to mark livepatch symbols and relocation sections were
5341selected from OS-specific ranges according to the definitions from glibc.
5442
55- 0.1 Why does livepatch need to write its own relocations?
56- ---------------------------------------------------------
43+ Why does livepatch need to write its own relocations?
44+ -----------------------------------------------------
5745A typical livepatch module contains patched versions of functions that can
5846reference non-exported global symbols and non-included local symbols.
5947Relocations referencing these types of symbols cannot be left in as-is
@@ -72,13 +60,8 @@ relas reference are special livepatch symbols (see section 2 and 3). The
7260arch-specific livepatch relocation code is replaced by a call to
7361apply_relocate_add().
7462
75- ================================
76- PATCH MODULE FORMAT REQUIREMENTS
77- ================================
78-
79- --------------------------
80- 1. Livepatch modinfo field
81- --------------------------
63+ 2. Livepatch modinfo field
64+ ==========================
8265
8366Livepatch modules are required to have the "livepatch" modinfo attribute.
8467See the sample livepatch module in samples/livepatch/ for how this is done.
@@ -87,8 +70,10 @@ Livepatch modules can be identified by users by using the 'modinfo' command
8770and looking for the presence of the "livepatch" field. This field is also
8871used by the kernel module loader to identify livepatch modules.
8972
90- Example modinfo output:
91- -----------------------
73+ Example:
74+ --------
75+
76+ **Modinfo output: **
9277
9378::
9479
@@ -99,13 +84,9 @@ Example modinfo output:
9984 depends:
10085 vermagic: 4.3.0+ SMP mod_unload
10186
102- --------------------------------
103- 2. Livepatch relocation sections
104- --------------------------------
87+ 3. Livepatch relocation sections
88+ ================================
10589
106- -------------------------------------------
107- 2.1 What are livepatch relocation sections?
108- -------------------------------------------
10990A livepatch module manages its own Elf relocation sections to apply
11091relocations to modules as well as to the kernel (vmlinux) at the
11192appropriate time. For example, if a patch module patches a driver that is
@@ -130,21 +111,16 @@ Every symbol referenced by a rela in a livepatch relocation section is a
130111livepatch symbol. These must be resolved before livepatch can call
131112apply_relocate_add(). See Section 3 for more information.
132113
133- ---------------------------------------
134- 2.2 Livepatch relocation section format
135- ---------------------------------------
114+ 3.1 Livepatch relocation section format
115+ =======================================
136116
137- 2.2.1 Required flags
138- --------------------
139117Livepatch relocation sections must be marked with the SHF_RELA_LIVEPATCH
140118section flag. See include/uapi/linux/elf.h for the definition. The module
141119loader recognizes this flag and will avoid applying those relocation sections
142120at patch module load time. These sections must also be marked with SHF_ALLOC,
143121so that the module loader doesn't discard them on module load (i.e. they will
144122be copied into memory along with the other SHF_ALLOC sections).
145123
146- 2.2.2 Required name format
147- --------------------------
148124The name of a livepatch relocation section must conform to the following
149125format::
150126
@@ -153,19 +129,28 @@ format::
153129 |________||_____| |__________|
154130 [A] [B] [C]
155131
156- [A] The relocation section name is prefixed with the string ".klp.rela."
157- [B] The name of the object (i.e. "vmlinux" or name of module) to
158- which the relocation section belongs follows immediately after the prefix.
159- [C] The actual name of the section to which this relocation section applies.
132+ [A]
133+ The relocation section name is prefixed with the string ".klp.rela."
160134
161- 2.2.3 Example livepatch relocation section names:
162- -------------------------------------------------
163- .klp.rela.ext4.text.ext4_attr_store
164- .klp.rela.vmlinux.text.cmdline_proc_show
135+ [B]
136+ The name of the object (i.e. "vmlinux" or name of module) to
137+ which the relocation section belongs follows immediately after the prefix.
165138
166- 2.2.4 Example `readelf --sections ` output for a patch
167- module that patches vmlinux and modules 9p, btrfs, ext4:
168- --------------------------------------------------------
139+ [C]
140+ The actual name of the section to which this relocation section applies.
141+
142+ Examples:
143+ ---------
144+
145+ **Livepatch relocation section names: **
146+
147+ ::
148+
149+ .klp.rela.ext4.text.ext4_attr_store
150+ .klp.rela.vmlinux.text.cmdline_proc_show
151+
152+ **`readelf --sections` output for a patch
153+ module that patches vmlinux and modules 9p, btrfs, ext4: **
169154
170155::
171156
@@ -182,13 +167,14 @@ module that patches vmlinux and modules 9p, btrfs, ext4:
182167 [ snip ] ^ ^
183168 | |
184169 [*] [*]
185- [*] Livepatch relocation sections are SHT_RELA sections but with a few special
170+
171+ [*]
172+ Livepatch relocation sections are SHT_RELA sections but with a few special
186173 characteristics. Notice that they are marked SHF_ALLOC ("A") so that they will
187174 not be discarded when the module is loaded into memory, as well as with the
188175 SHF_RELA_LIVEPATCH flag ("o" - for OS-specific).
189176
190- 2.2.5 Example `readelf --relocs ` output for a patch module:
191- -----------------------------------------------------------
177+ **`readelf --relocs` output for a patch module: **
192178
193179::
194180
@@ -200,16 +186,14 @@ module that patches vmlinux and modules 9p, btrfs, ext4:
200186 000000000000004c 0000004900000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.snprintf,0 - 4
201187 [ snip ] ^
202188 |
203- [*]
204- [*] Every symbol referenced by a relocation is a livepatch symbol.
189+ [*]
190+
191+ [*]
192+ Every symbol referenced by a relocation is a livepatch symbol.
205193
206- --------------------
207- 3. Livepatch symbols
208- --------------------
194+ 4. Livepatch symbols
195+ ====================
209196
210- -------------------------------
211- 3.1 What are livepatch symbols?
212- -------------------------------
213197Livepatch symbols are symbols referred to by livepatch relocation sections.
214198These are symbols accessed from new versions of functions for patched
215199objects, whose addresses cannot be resolved by the module loader (because
@@ -229,9 +213,8 @@ loader can identify and ignore them. Livepatch modules keep these symbols
229213in their symbol tables, and the symbol table is made accessible through
230214module->symtab.
231215
232- -------------------------------------
233- 3.2 A livepatch module's symbol table
234- -------------------------------------
216+ 4.1 A livepatch module's symbol table
217+ =====================================
235218Normally, a stripped down copy of a module's symbol table (containing only
236219"core" symbols) is made available through module->symtab (See layout_symtab()
237220in kernel/module.c). For livepatch modules, the symbol table copied into memory
@@ -255,45 +238,48 @@ For example, take this particular rela from a livepatch module:::
255238 94: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.printk,0
256239 [ snip ]
257240
258- ---------------------------
259- 3.3 Livepatch symbol format
260- ---------------------------
241+ 4.2 Livepatch symbol format
242+ ===========================
261243
262- 3.3.1 Required flags
263- --------------------
264244Livepatch symbols must have their section index marked as SHN_LIVEPATCH, so
265245that the module loader can identify them and not attempt to resolve them.
266246See include/uapi/linux/elf.h for the actual definitions.
267247
268- 3.3.2 Required name format
269- --------------------------
270248Livepatch symbol names must conform to the following format::
271249
272250 .klp.sym.objname.symbol_name,sympos
273251 ^ ^^ ^ ^ ^ ^
274252 |_______||_____| |_________| |
275253 [A] [B] [C] [D]
276254
277- [A] The symbol name is prefixed with the string ".klp.sym."
278- [B] The name of the object (i.e. "vmlinux" or name of module) to
279- which the symbol belongs follows immediately after the prefix.
280- [C] The actual name of the symbol.
281- [D] The position of the symbol in the object (as according to kallsyms)
282- This is used to differentiate duplicate symbols within the same
283- object. The symbol position is expressed numerically (0, 1, 2...).
284- The symbol position of a unique symbol is 0.
255+ [A]
256+ The symbol name is prefixed with the string ".klp.sym."
257+
258+ [B]
259+ The name of the object (i.e. "vmlinux" or name of module) to
260+ which the symbol belongs follows immediately after the prefix.
285261
286- 3.3.3 Example livepatch symbol names:
287- -------------------------------------
262+ [C]
263+ The actual name of the symbol.
264+
265+ [D]
266+ The position of the symbol in the object (as according to kallsyms)
267+ This is used to differentiate duplicate symbols within the same
268+ object. The symbol position is expressed numerically (0, 1, 2...).
269+ The symbol position of a unique symbol is 0.
270+
271+ Examples:
272+ ---------
273+
274+ **Livepatch symbol names: **
288275
289276::
290277
291278 .klp.sym.vmlinux.snprintf,0
292279 .klp.sym.vmlinux.printk,0
293280 .klp.sym.btrfs.btrfs_ktype,0
294281
295- 3.3.4 Example `readelf --symbols ` output for a patch module:
296- ------------------------------------------------------------
282+ **`readelf --symbols` output for a patch module: **
297283
298284::
299285
@@ -307,12 +293,13 @@ Livepatch symbol names must conform to the following format::
307293 [ snip ] ^
308294 |
309295 [*]
310- [*] Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
311- "OS" means OS-specific.
312296
313- ---------------------------------
314- 4. Architecture-specific sections
315- ---------------------------------
297+ [*]
298+ Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
299+ "OS" means OS-specific.
300+
301+ 5. Architecture-specific sections
302+ =================================
316303Architectures may override arch_klp_init_object_loaded() to perform
317304additional arch-specific tasks when a target module loads, such as applying
318305arch-specific sections. On x86 for example, we must apply per-object
@@ -321,9 +308,8 @@ These sections must be prefixed with ".klp.arch.$objname." so that they can
321308be easily identified when iterating through a patch module's Elf sections
322309(See arch/x86/kernel/livepatch.c for a complete example).
323310
324- --------------------------------------
325- 5. Symbol table and Elf section access
326- --------------------------------------
311+ 6. Symbol table and Elf section access
312+ ======================================
327313A livepatch module's symbol table is accessible through module->symtab.
328314
329315Since apply_relocate_add() requires access to a module's section headers,
0 commit comments