22DWARF module versioning
33=======================
44
5- 1. Introduction
6- ===============
5+ Introduction
6+ ============
77
88When CONFIG_MODVERSIONS is enabled, symbol versions for modules
99are typically calculated from preprocessed source code using the
@@ -14,8 +14,8 @@ selected, **gendwarfksyms** is used instead to calculate symbol versions
1414from the DWARF debugging information, which contains the necessary
1515details about the final module ABI.
1616
17- 1.1. Usage
18- ==========
17+ Usage
18+ -----
1919
2020gendwarfksyms accepts a list of object files on the command line, and a
2121list of symbol names (one per line) in standard input::
@@ -33,8 +33,8 @@ list of symbol names (one per line) in standard input::
3333 -h, --help Print this message
3434
3535
36- 2. Type information availability
37- ================================
36+ Type information availability
37+ =============================
3838
3939While symbols are typically exported in the same translation unit (TU)
4040where they're defined, it's also perfectly fine for a TU to export
@@ -56,8 +56,8 @@ type for calculating symbol versions even if the symbol is defined
5656elsewhere. The name of the symbol pointer is expected to start with
5757`__gendwarfksyms_ptr_ `, followed by the name of the exported symbol.
5858
59- 3. Symtypes output format
60- =========================
59+ Symtypes output format
60+ ======================
6161
6262Similarly to genksyms, gendwarfksyms supports writing a symtypes
6363file for each processed object that contain types for exported
@@ -85,8 +85,8 @@ produces C-style type strings, gendwarfksyms uses the same simple parsed
8585DWARF format produced by **--dump-dies **, but with type references
8686instead of fully expanded strings.
8787
88- 4. Maintaining a stable kABI
89- ============================
88+ Maintaining a stable kABI
89+ =========================
9090
9191Distribution maintainers often need the ability to make ABI compatible
9292changes to kernel data structures due to LTS updates or backports. Using
@@ -104,8 +104,8 @@ for source code annotation. Note that as these features are only used to
104104transform the inputs for symbol versioning, the user is responsible for
105105ensuring that their changes actually won't break the ABI.
106106
107- 4.1. kABI rules
108- ===============
107+ kABI rules
108+ ----------
109109
110110kABI rules allow distributions to fine-tune certain parts
111111of gendwarfksyms output and thus control how symbol
@@ -125,22 +125,25 @@ the rules. The fields are as follows:
125125 qualified name of the DWARF Debugging Information Entry (DIE).
126126- `value `: Provides rule-specific data.
127127
128- The following helper macro , for example, can be used to specify rules
128+ The following helper macros , for example, can be used to specify rules
129129in the source code::
130130
131- #define __KABI_RULE (hint, target, value) \
132- static const char __PASTE(__gendwarfksyms_rule_, \
131+ #define ___KABI_RULE (hint, target, value) \
132+ static const char __PASTE(__gendwarfksyms_rule_, \
133133 __COUNTER__)[] __used __aligned(1) \
134134 __section(".discard.gendwarfksyms.kabi_rules") = \
135- "1\0" #hint "\0" #target "\0" #value
135+ "1\0" #hint "\0" target "\0" value
136+
137+ #define __KABI_RULE(hint, target, value) \
138+ ___KABI_RULE(hint, #target, #value)
136139
137140
138141Currently, only the rules discussed in this section are supported, but
139142the format is extensible enough to allow further rules to be added as
140143need arises.
141144
142- 4.1.1. Managing definition visibility
143- =====================================
145+ Managing definition visibility
146+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144147
145148A declaration can change into a full definition when additional includes
146149are pulled into the translation unit. This changes the versions of any
@@ -168,8 +171,8 @@ Example usage::
168171
169172 KABI_DECLONLY(s);
170173
171- 4.1.2. Adding enumerators
172- =========================
174+ Adding enumerators
175+ ~~~~~~~~~~~~~~~~~~
173176
174177For enums, all enumerators and their values are included in calculating
175178symbol versions, which becomes a problem if we later need to add more
@@ -223,8 +226,89 @@ Example usage::
223226 KABI_ENUMERATOR_IGNORE(e, C);
224227 KABI_ENUMERATOR_VALUE(e, LAST, 2);
225228
226- 4.3. Adding structure members
227- =============================
229+ Managing structure size changes
230+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231+
232+ A data structure can be partially opaque to modules if its allocation is
233+ handled by the core kernel, and modules only need to access some of its
234+ members. In this situation, it's possible to append new members to the
235+ structure without breaking the ABI, as long as the layout for the original
236+ members remains unchanged.
237+
238+ To append new members, we can hide them from symbol versioning as
239+ described in section :ref: `Hiding members <hiding_members >`, but we can't
240+ hide the increase in structure size. The `byte_size ` rule allows us to
241+ override the structure size used for symbol versioning.
242+
243+ The rule fields are expected to be as follows:
244+
245+ - `type `: "byte_size"
246+ - `target `: The fully qualified name of the target data structure
247+ (as shown in **--dump-dies ** output).
248+ - `value `: A positive decimal number indicating the structure size
249+ in bytes.
250+
251+ Using the `__KABI_RULE ` macro, this rule can be defined as::
252+
253+ #define KABI_BYTE_SIZE(fqn, value) \
254+ __KABI_RULE(byte_size, fqn, value)
255+
256+ Example usage::
257+
258+ struct s {
259+ /* Unchanged original members */
260+ unsigned long a;
261+ void *p;
262+
263+ /* Appended new members */
264+ KABI_IGNORE(0, unsigned long n);
265+ };
266+
267+ KABI_BYTE_SIZE(s, 16);
268+
269+ Overriding type strings
270+ ~~~~~~~~~~~~~~~~~~~~~~~
271+
272+ In rare situations where distributions must make significant changes to
273+ otherwise opaque data structures that have inadvertently been included
274+ in the published ABI, keeping symbol versions stable using the more
275+ targeted kABI rules can become tedious. The `type_string ` rule allows us
276+ to override the full type string for a type or a symbol, and even add
277+ types for versioning that no longer exist in the kernel.
278+
279+ The rule fields are expected to be as follows:
280+
281+ - `type `: "type_string"
282+ - `target `: The fully qualified name of the target data structure
283+ (as shown in **--dump-dies ** output) or symbol.
284+ - `value `: A valid type string (as shown in **--symtypes **) output)
285+ to use instead of the real type.
286+
287+ Using the `__KABI_RULE ` macro, this rule can be defined as::
288+
289+ #define KABI_TYPE_STRING(type, str) \
290+ ___KABI_RULE("type_string", type, str)
291+
292+ Example usage::
293+
294+ /* Override type for a structure */
295+ KABI_TYPE_STRING("s#s",
296+ "structure_type s { "
297+ "member base_type int byte_size(4) "
298+ "encoding(5) n "
299+ "data_member_location(0) "
300+ "} byte_size(8)");
301+
302+ /* Override type for a symbol */
303+ KABI_TYPE_STRING("my_symbol", "variable s#s");
304+
305+ The `type_string ` rule should be used only as a last resort if maintaining
306+ a stable symbol versions cannot be reasonably achieved using other
307+ means. Overriding a type string increases the risk of actual ABI breakages
308+ going unnoticed as it hides all changes to the type.
309+
310+ Adding structure members
311+ ------------------------
228312
229313Perhaps the most common ABI compatible change is adding a member to a
230314kernel data structure. When changes to a structure are anticipated,
@@ -237,8 +321,8 @@ natural method. This section describes gendwarfksyms support for using
237321reserved space in data structures and hiding members that don't change
238322the ABI when calculating symbol versions.
239323
240- 4.3.1. Reserving space and replacing members
241- ============================================
324+ Reserving space and replacing members
325+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
242326
243327Space is typically reserved for later use by appending integer types, or
244328arrays, to the end of the data structure, but any type can be used. Each
@@ -276,8 +360,10 @@ The examples include `KABI_(RESERVE|USE|REPLACE)*` macros that help
276360simplify the process and also ensure the replacement member is correctly
277361aligned and its size won't exceed the reserved space.
278362
279- 4.3.2. Hiding members
280- =====================
363+ .. _hiding_members :
364+
365+ Hiding members
366+ ~~~~~~~~~~~~~~
281367
282368Predicting which structures will require changes during the support
283369timeframe isn't always possible, in which case one might have to resort
@@ -305,4 +391,5 @@ member to a union where one of the fields has a name starting with
305391 unsigned long b;
306392 };
307393
308- With **--stable **, both versions produce the same symbol version.
394+ With **--stable **, both versions produce the same symbol version. The
395+ examples include a `KABI_IGNORE ` macro to simplify the code.
0 commit comments