xref: /linux/Documentation/bpf/btf.rst (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1=====================
2BPF Type Format (BTF)
3=====================
4
51. Introduction
6===============
7
8BTF (BPF Type Format) is the metadata format which encodes the debug info
9related to BPF program/map. The name BTF was used initially to describe data
10types. The BTF was later extended to include function info for defined
11subroutines, and line info for source/line information.
12
13The debug info is used for map pretty print, function signature, etc. The
14function signature enables better bpf program/function kernel symbol. The line
15info helps generate source annotated translated byte code, jited code and
16verifier log.
17
18The BTF specification contains two parts,
19  * BTF kernel API
20  * BTF ELF file format
21
22The kernel API is the contract between user space and kernel. The kernel
23verifies the BTF info before using it. The ELF file format is a user space
24contract between ELF file and libbpf loader.
25
26The type and string sections are part of the BTF kernel API, describing the
27debug info (mostly types related) referenced by the bpf program. These two
28sections are discussed in details in :ref:`BTF_Type_String`.
29
30.. _BTF_Type_String:
31
322. BTF Type and String Encoding
33===============================
34
35The file ``include/uapi/linux/btf.h`` provides high-level definition of how
36types/strings are encoded.
37
38The beginning of data blob must be::
39
40    struct btf_header {
41        __u16   magic;
42        __u8    version;
43        __u8    flags;
44        __u32   hdr_len;
45
46        /* All offsets are in bytes relative to the end of this header */
47        __u32   type_off;       /* offset of type section       */
48        __u32   type_len;       /* length of type section       */
49        __u32   str_off;        /* offset of string section     */
50        __u32   str_len;        /* length of string section     */
51    };
52
53The magic is ``0xeB9F``, which has different encoding for big and little
54endian systems, and can be used to test whether BTF is generated for big- or
55little-endian target. The ``btf_header`` is designed to be extensible with
56``hdr_len`` equal to ``sizeof(struct btf_header)`` when a data blob is
57generated.
58
592.1 String Encoding
60-------------------
61
62The first string in the string section must be a null string. The rest of
63string table is a concatenation of other null-terminated strings.
64
652.2 Type Encoding
66-----------------
67
68The type id ``0`` is reserved for ``void`` type. The type section is parsed
69sequentially and type id is assigned to each recognized type starting from id
70``1``. Currently, the following types are supported::
71
72    #define BTF_KIND_INT            1       /* Integer      */
73    #define BTF_KIND_PTR            2       /* Pointer      */
74    #define BTF_KIND_ARRAY          3       /* Array        */
75    #define BTF_KIND_STRUCT         4       /* Struct       */
76    #define BTF_KIND_UNION          5       /* Union        */
77    #define BTF_KIND_ENUM           6       /* Enumeration up to 32-bit values */
78    #define BTF_KIND_FWD            7       /* Forward      */
79    #define BTF_KIND_TYPEDEF        8       /* Typedef      */
80    #define BTF_KIND_VOLATILE       9       /* Volatile     */
81    #define BTF_KIND_CONST          10      /* Const        */
82    #define BTF_KIND_RESTRICT       11      /* Restrict     */
83    #define BTF_KIND_FUNC           12      /* Function     */
84    #define BTF_KIND_FUNC_PROTO     13      /* Function Proto       */
85    #define BTF_KIND_VAR            14      /* Variable     */
86    #define BTF_KIND_DATASEC        15      /* Section      */
87    #define BTF_KIND_FLOAT          16      /* Floating point       */
88    #define BTF_KIND_DECL_TAG       17      /* Decl Tag     */
89    #define BTF_KIND_TYPE_TAG       18      /* Type Tag     */
90    #define BTF_KIND_ENUM64         19      /* Enumeration up to 64-bit values */
91    #define BTF_KIND_LOC_PARAM      20      /* Location description (register, const etc) */
92    #define BTF_KIND_LOC_PROTO      21      /* Set of location parameters for site */
93    #define BTF_KIND_LOCSEC         22      /* Section with site descriptions */
94
95Note that the type section encodes debug info, not just pure types.
96``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
97
98Each type contains the following common data::
99
100    struct btf_type {
101        __u32 name_off;
102        /* "info" bits arrangement
103         * bits  0-23: vlen (e.g. # of struct's members)
104         * bits 24-30: kind (e.g. int, ptr, array...etc)
105         * bit     31: kind_flag, currently used by
106         *             struct, union, enum, fwd, enum64,
107         *             decl_tag and type_tag
108         */
109        __u32 info;
110        /* "size" is used by INT, ENUM, STRUCT, UNION, ENUM64 and
111         * LOC_PARAM.
112         * "size" tells the size of the type it is describing.
113         *
114         * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
115         * FUNC, FUNC_PROTO, DECL_TAG and TYPE_TAG. It is unused by
116         * LOC_PROTO and LOCSEC.
117         * "type" is a type_id referring to another type.
118         */
119        union {
120                __u32 size;
121                __u32 type;
122        };
123    };
124
125For certain kinds, the common data are followed by kind-specific data. The
126``name_off`` in ``struct btf_type`` specifies the offset in the string table.
127The following sections detail encoding of each kind.
128
1292.2.1 BTF_KIND_INT
130~~~~~~~~~~~~~~~~~~
131
132``struct btf_type`` encoding requirement:
133 * ``name_off``: any valid offset
134 * ``info.kind_flag``: 0
135 * ``info.kind``: BTF_KIND_INT
136 * ``info.vlen``: 0
137 * ``size``: the size of the int type in bytes.
138
139``btf_type`` is followed by a ``u32`` with the following bits arrangement::
140
141  #define BTF_INT_ENCODING(VAL)   (((VAL) & 0x0f000000) >> 24)
142  #define BTF_INT_OFFSET(VAL)     (((VAL) & 0x00ff0000) >> 16)
143  #define BTF_INT_BITS(VAL)       ((VAL)  & 0x000000ff)
144
145The ``BTF_INT_ENCODING`` has the following attributes::
146
147  #define BTF_INT_SIGNED  (1 << 0)
148  #define BTF_INT_CHAR    (1 << 1)
149  #define BTF_INT_BOOL    (1 << 2)
150
151The ``BTF_INT_ENCODING()`` provides extra information: signedness, char, or
152bool, for the int type. The char and bool encoding are mostly useful for
153pretty print. At most one encoding can be specified for the int type.
154
155The ``BTF_INT_BITS()`` specifies the number of actual bits held by this int
156type. For example, a 4-bit bitfield encodes ``BTF_INT_BITS()`` equals to 4.
157The ``btf_type.size * 8`` must be equal to or greater than ``BTF_INT_BITS()``
158for the type. The maximum value of ``BTF_INT_BITS()`` is 128.
159
160The ``BTF_INT_OFFSET()`` specifies the starting bit offset to calculate values
161for this int. For example, a bitfield struct member has:
162
163 * btf member bit offset 100 from the start of the structure,
164 * btf member pointing to an int type,
165 * the int type has ``BTF_INT_OFFSET() = 2`` and ``BTF_INT_BITS() = 4``
166
167Then in the struct memory layout, this member will occupy ``4`` bits starting
168from bits ``100 + 2 = 102``.
169
170Alternatively, the bitfield struct member can be the following to access the
171same bits as the above:
172
173 * btf member bit offset 102,
174 * btf member pointing to an int type,
175 * the int type has ``BTF_INT_OFFSET() = 0`` and ``BTF_INT_BITS() = 4``
176
177The original intention of ``BTF_INT_OFFSET()`` is to provide flexibility of
178bitfield encoding. Currently, both llvm and pahole generate
179``BTF_INT_OFFSET() = 0`` for all int types.
180
1812.2.2 BTF_KIND_PTR
182~~~~~~~~~~~~~~~~~~
183
184``struct btf_type`` encoding requirement:
185  * ``name_off``: 0
186  * ``info.kind_flag``: 0
187  * ``info.kind``: BTF_KIND_PTR
188  * ``info.vlen``: 0
189  * ``type``: the pointee type of the pointer
190
191No additional type data follow ``btf_type``.
192
1932.2.3 BTF_KIND_ARRAY
194~~~~~~~~~~~~~~~~~~~~
195
196``struct btf_type`` encoding requirement:
197  * ``name_off``: 0
198  * ``info.kind_flag``: 0
199  * ``info.kind``: BTF_KIND_ARRAY
200  * ``info.vlen``: 0
201  * ``size/type``: 0, not used
202
203``btf_type`` is followed by one ``struct btf_array``::
204
205    struct btf_array {
206        __u32   type;
207        __u32   index_type;
208        __u32   nelems;
209    };
210
211The ``struct btf_array`` encoding:
212  * ``type``: the element type
213  * ``index_type``: the index type
214  * ``nelems``: the number of elements for this array (``0`` is also allowed).
215
216The ``index_type`` can be any regular int type (``u8``, ``u16``, ``u32``,
217``u64``, ``unsigned __int128``). The original design of including
218``index_type`` follows DWARF, which has an ``index_type`` for its array type.
219Currently in BTF, beyond type verification, the ``index_type`` is not used.
220
221The ``struct btf_array`` allows chaining through element type to represent
222multidimensional arrays. For example, for ``int a[5][6]``, the following type
223information illustrates the chaining:
224
225  * [1]: int
226  * [2]: array, ``btf_array.type = [1]``, ``btf_array.nelems = 6``
227  * [3]: array, ``btf_array.type = [2]``, ``btf_array.nelems = 5``
228
229Currently, both pahole and llvm collapse multidimensional array into
230one-dimensional array, e.g., for ``a[5][6]``, the ``btf_array.nelems`` is
231equal to ``30``. This is because the original use case is map pretty print
232where the whole array is dumped out so one-dimensional array is enough. As
233more BTF usage is explored, pahole and llvm can be changed to generate proper
234chained representation for multidimensional arrays.
235
2362.2.4 BTF_KIND_STRUCT
237~~~~~~~~~~~~~~~~~~~~~
2382.2.5 BTF_KIND_UNION
239~~~~~~~~~~~~~~~~~~~~
240
241``struct btf_type`` encoding requirement:
242  * ``name_off``: 0 or offset to a valid C identifier
243  * ``info.kind_flag``: 0 or 1
244  * ``info.kind``: BTF_KIND_STRUCT or BTF_KIND_UNION
245  * ``info.vlen``: the number of struct/union members
246  * ``info.size``: the size of the struct/union in bytes
247
248``btf_type`` is followed by ``info.vlen`` number of ``struct btf_member``.::
249
250    struct btf_member {
251        __u32   name_off;
252        __u32   type;
253        __u32   offset;
254    };
255
256``struct btf_member`` encoding:
257  * ``name_off``: offset to a valid C identifier
258  * ``type``: the member type
259  * ``offset``: <see below>
260
261If the type info ``kind_flag`` is not set, the offset contains only bit offset
262of the member. Note that the base type of the bitfield can only be int or enum
263type. If the bitfield size is 32, the base type can be either int or enum
264type. If the bitfield size is not 32, the base type must be int, and int type
265``BTF_INT_BITS()`` encodes the bitfield size.
266
267If the ``kind_flag`` is set, the ``btf_member.offset`` contains both member
268bitfield size and bit offset. The bitfield size and bit offset are calculated
269as below.::
270
271  #define BTF_MEMBER_BITFIELD_SIZE(val)   ((val) >> 24)
272  #define BTF_MEMBER_BIT_OFFSET(val)      ((val) & 0xffffff)
273
274In this case, if the base type is an int type, it must be a regular int type:
275
276  * ``BTF_INT_OFFSET()`` must be 0.
277  * ``BTF_INT_BITS()`` must be equal to ``{1,2,4,8,16} * 8``.
278
279Commit 9d5f9f701b18 introduced ``kind_flag`` and explains why both modes
280exist.
281
2822.2.6 BTF_KIND_ENUM
283~~~~~~~~~~~~~~~~~~~
284
285``struct btf_type`` encoding requirement:
286  * ``name_off``: 0 or offset to a valid C identifier
287  * ``info.kind_flag``: 0 for unsigned, 1 for signed
288  * ``info.kind``: BTF_KIND_ENUM
289  * ``info.vlen``: number of enum values
290  * ``size``: 1/2/4/8
291
292``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum``.::
293
294    struct btf_enum {
295        __u32   name_off;
296        __s32   val;
297    };
298
299The ``btf_enum`` encoding:
300  * ``name_off``: offset to a valid C identifier
301  * ``val``: any value
302
303If the original enum value is signed and the size is less than 4,
304that value will be sign extended into 4 bytes. If the size is 8,
305the value will be truncated into 4 bytes.
306
3072.2.7 BTF_KIND_FWD
308~~~~~~~~~~~~~~~~~~
309
310``struct btf_type`` encoding requirement:
311  * ``name_off``: offset to a valid C identifier
312  * ``info.kind_flag``: 0 for struct, 1 for union
313  * ``info.kind``: BTF_KIND_FWD
314  * ``info.vlen``: 0
315  * ``type``: 0
316
317No additional type data follow ``btf_type``.
318
3192.2.8 BTF_KIND_TYPEDEF
320~~~~~~~~~~~~~~~~~~~~~~
321
322``struct btf_type`` encoding requirement:
323  * ``name_off``: offset to a valid C identifier
324  * ``info.kind_flag``: 0
325  * ``info.kind``: BTF_KIND_TYPEDEF
326  * ``info.vlen``: 0
327  * ``type``: the type which can be referred by name at ``name_off``
328
329No additional type data follow ``btf_type``.
330
3312.2.9 BTF_KIND_VOLATILE
332~~~~~~~~~~~~~~~~~~~~~~~
333
334``struct btf_type`` encoding requirement:
335  * ``name_off``: 0
336  * ``info.kind_flag``: 0
337  * ``info.kind``: BTF_KIND_VOLATILE
338  * ``info.vlen``: 0
339  * ``type``: the type with ``volatile`` qualifier
340
341No additional type data follow ``btf_type``.
342
3432.2.10 BTF_KIND_CONST
344~~~~~~~~~~~~~~~~~~~~~
345
346``struct btf_type`` encoding requirement:
347  * ``name_off``: 0
348  * ``info.kind_flag``: 0
349  * ``info.kind``: BTF_KIND_CONST
350  * ``info.vlen``: 0
351  * ``type``: the type with ``const`` qualifier
352
353No additional type data follow ``btf_type``.
354
3552.2.11 BTF_KIND_RESTRICT
356~~~~~~~~~~~~~~~~~~~~~~~~
357
358``struct btf_type`` encoding requirement:
359  * ``name_off``: 0
360  * ``info.kind_flag``: 0
361  * ``info.kind``: BTF_KIND_RESTRICT
362  * ``info.vlen``: 0
363  * ``type``: the type with ``restrict`` qualifier
364
365No additional type data follow ``btf_type``.
366
3672.2.12 BTF_KIND_FUNC
368~~~~~~~~~~~~~~~~~~~~
369
370``struct btf_type`` encoding requirement:
371  * ``name_off``: offset to a valid C identifier
372  * ``info.kind_flag``: 0
373  * ``info.kind``: BTF_KIND_FUNC
374  * ``info.vlen``: linkage information (BTF_FUNC_STATIC, BTF_FUNC_GLOBAL
375                   or BTF_FUNC_EXTERN - see :ref:`BTF_Function_Linkage_Constants`)
376  * ``type``: a BTF_KIND_FUNC_PROTO type
377
378No additional type data follow ``btf_type``.
379
380A BTF_KIND_FUNC defines not a type, but a subprogram (function) whose
381signature is defined by ``type``. The subprogram is thus an instance of that
382type. The BTF_KIND_FUNC may in turn be referenced by a func_info in the
383:ref:`BTF_Ext_Section` (ELF) or in the arguments to :ref:`BPF_Prog_Load`
384(ABI).
385
386Currently, only linkage values of BTF_FUNC_STATIC and BTF_FUNC_GLOBAL are
387supported in the kernel.
388
3892.2.13 BTF_KIND_FUNC_PROTO
390~~~~~~~~~~~~~~~~~~~~~~~~~~
391
392``struct btf_type`` encoding requirement:
393  * ``name_off``: 0
394  * ``info.kind_flag``: 0
395  * ``info.kind``: BTF_KIND_FUNC_PROTO
396  * ``info.vlen``: # of parameters
397  * ``type``: the return type
398
399``btf_type`` is followed by ``info.vlen`` number of ``struct btf_param``.::
400
401    struct btf_param {
402        __u32   name_off;
403        __u32   type;
404    };
405
406If a BTF_KIND_FUNC_PROTO type is referred by a BTF_KIND_FUNC type, then
407``btf_param.name_off`` must point to a valid C identifier except for the
408possible last argument representing the variable argument. The btf_param.type
409refers to parameter type.
410
411If the function has variable arguments, the last parameter is encoded with
412``name_off = 0`` and ``type = 0``.
413
4142.2.14 BTF_KIND_VAR
415~~~~~~~~~~~~~~~~~~~
416
417``struct btf_type`` encoding requirement:
418  * ``name_off``: offset to a valid C identifier
419  * ``info.kind_flag``: 0
420  * ``info.kind``: BTF_KIND_VAR
421  * ``info.vlen``: 0
422  * ``type``: the type of the variable
423
424``btf_type`` is followed by a single ``struct btf_var`` with the
425following data::
426
427    struct btf_var {
428        __u32   linkage;
429    };
430
431``btf_var.linkage`` may take the values: BTF_VAR_STATIC, BTF_VAR_GLOBAL_ALLOCATED or BTF_VAR_GLOBAL_EXTERN -
432see :ref:`BTF_Var_Linkage_Constants`.
433
434Not all type of global variables are supported by LLVM at this point.
435The following is currently available:
436
437  * static variables with or without section attributes
438  * global variables with section attributes
439
440The latter is for future extraction of map key/value type id's from a
441map definition.
442
4432.2.15 BTF_KIND_DATASEC
444~~~~~~~~~~~~~~~~~~~~~~~
445
446``struct btf_type`` encoding requirement:
447  * ``name_off``: offset to a valid name associated with a variable or
448                  one of .data/.bss/.rodata
449  * ``info.kind_flag``: 0
450  * ``info.kind``: BTF_KIND_DATASEC
451  * ``info.vlen``: # of variables
452  * ``size``: total section size in bytes (0 at compilation time, patched
453              to actual size by BPF loaders such as libbpf)
454
455``btf_type`` is followed by ``info.vlen`` number of ``struct btf_var_secinfo``.::
456
457    struct btf_var_secinfo {
458        __u32   type;
459        __u32   offset;
460        __u32   size;
461    };
462
463``struct btf_var_secinfo`` encoding:
464  * ``type``: the type of the BTF_KIND_VAR variable
465  * ``offset``: the in-section offset of the variable
466  * ``size``: the size of the variable in bytes
467
4682.2.16 BTF_KIND_FLOAT
469~~~~~~~~~~~~~~~~~~~~~
470
471``struct btf_type`` encoding requirement:
472 * ``name_off``: any valid offset
473 * ``info.kind_flag``: 0
474 * ``info.kind``: BTF_KIND_FLOAT
475 * ``info.vlen``: 0
476 * ``size``: the size of the float type in bytes: 2, 4, 8, 12 or 16.
477
478No additional type data follow ``btf_type``.
479
4802.2.17 BTF_KIND_DECL_TAG
481~~~~~~~~~~~~~~~~~~~~~~~~
482
483``struct btf_type`` encoding requirement:
484 * ``name_off``: offset to a non-empty string
485 * ``info.kind_flag``: 0 or 1
486 * ``info.kind``: BTF_KIND_DECL_TAG
487 * ``info.vlen``: 0
488 * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef``
489
490``btf_type`` is followed by ``struct btf_decl_tag``.::
491
492    struct btf_decl_tag {
493        __u32   component_idx;
494    };
495
496The ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``.
497For ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``.
498For the other three types, if the btf_decl_tag attribute is
499applied to the ``struct``, ``union`` or ``func`` itself,
500``btf_decl_tag.component_idx`` must be ``-1``. Otherwise,
501the attribute is applied to a ``struct``/``union`` member or
502a ``func`` argument, and ``btf_decl_tag.component_idx`` should be a
503valid index (starting from 0) pointing to a member or an argument.
504
505If ``info.kind_flag`` is 0, then this is a normal decl tag, and the
506``name_off`` encodes btf_decl_tag attribute string.
507
508If ``info.kind_flag`` is 1, then the decl tag represents an arbitrary
509__attribute__. In this case, ``name_off`` encodes a string
510representing the attribute-list of the attribute specifier. For
511example, for an ``__attribute__((aligned(4)))`` the string's contents
512is ``aligned(4)``.
513
5142.2.18 BTF_KIND_TYPE_TAG
515~~~~~~~~~~~~~~~~~~~~~~~~
516
517``struct btf_type`` encoding requirement:
518 * ``name_off``: offset to a non-empty string
519 * ``info.kind_flag``: 0 or 1
520 * ``info.kind``: BTF_KIND_TYPE_TAG
521 * ``info.vlen``: 0
522 * ``type``: the type with ``btf_type_tag`` attribute
523
524Currently, ``BTF_KIND_TYPE_TAG`` is only emitted for pointer types.
525It has the following btf type chain:
526::
527
528  ptr -> [type_tag]*
529      -> [const | volatile | restrict | typedef]*
530      -> base_type
531
532Basically, a pointer type points to zero or more
533type_tag, then zero or more const/volatile/restrict/typedef
534and finally the base type. The base type is one of
535int, ptr, array, struct, union, enum, func_proto and float types.
536
537Similarly to decl tags, if the ``info.kind_flag`` is 0, then this is a
538normal type tag, and the ``name_off`` encodes btf_type_tag attribute
539string.
540
541If ``info.kind_flag`` is 1, then the type tag represents an arbitrary
542__attribute__, and the ``name_off`` encodes a string representing the
543attribute-list of the attribute specifier.
544
5452.2.19 BTF_KIND_ENUM64
546~~~~~~~~~~~~~~~~~~~~~~
547
548``struct btf_type`` encoding requirement:
549  * ``name_off``: 0 or offset to a valid C identifier
550  * ``info.kind_flag``: 0 for unsigned, 1 for signed
551  * ``info.kind``: BTF_KIND_ENUM64
552  * ``info.vlen``: number of enum values
553  * ``size``: 1/2/4/8
554
555``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
556
557    struct btf_enum64 {
558        __u32   name_off;
559        __u32   val_lo32;
560        __u32   val_hi32;
561    };
562
563The ``btf_enum64`` encoding:
564  * ``name_off``: offset to a valid C identifier
565  * ``val_lo32``: lower 32-bit value for a 64-bit value
566  * ``val_hi32``: high 32-bit value for a 64-bit value
567
568If the original enum value is signed and the size is less than 8,
569that value will be sign extended into 8 bytes.
570
5712.2.20 BTF_KIND_LOC_PARAM
572~~~~~~~~~~~~~~~~~~~~~~~~~~
573
574``struct btf_type`` encoding requirement:
575  * ``name_off``: 0
576  * ``info.kind_flag``: 0
577  * ``info.kind``: BTF_KIND_LOC_PARAM
578  * ``info.vlen``: number of 32-bit location value words
579  * ``size``: size in bytes of the represented parameter: 1 through 16
580
581``btf_type`` is followed by a ``struct btf_loc_param`` and ``info.vlen``
582number of 32-bit value words.::
583
584    struct btf_loc_param {
585        __u32 flags;
586        __u32 values[];
587    };
588
589The ``flags`` field describes how to interpret ``values``:
590
591  * ``BTF_LOC_PARAM_CONST`` describes a constant; the value is stored in
592    low-word, high-word order when it requires 64 bits.
593  * ``BTF_LOC_PARAM_ADDR | BTF_LOC_PARAM_CONST`` describes an address offset
594    relative to the runtime base address of the kernel or module image.
595  * ``BTF_LOC_PARAM_REG`` with one word describes a register number; with two
596    words it describes a multi-register parameter.
597  * ``BTF_LOC_PARAM_REG | BTF_LOC_PARAM_OFFSET`` describes an address held in
598    a register plus an offset. Adding ``BTF_LOC_PARAM_DEREF`` dereferences
599    that address. ``BTF_LOC_PARAM_REG | BTF_LOC_PARAM_DEREF`` with one word
600    dereferences the value held in the register.
601  * ``BTF_LOC_PARAM_SIGNED`` makes a constant or offset signed. A constant's
602    signed width is ``size``. For a register-relative offset, its signed
603    width is the number of offset value words times 32 bits.
604
6052.2.21 BTF_KIND_LOC_PROTO
606~~~~~~~~~~~~~~~~~~~~~~~~~~
607
608``struct btf_type`` encoding requirement:
609  * ``name_off``: 0
610  * ``info.kind_flag``: 0
611  * ``info.kind``: BTF_KIND_LOC_PROTO
612  * ``info.vlen``: number of function parameter locations
613  * ``type``: 0
614
615``btf_type`` is followed by ``info.vlen`` number of ``__u32`` BTF type IDs.
616Each entry corresponds to a function parameter at an inline site. An entry is
617either 0, meaning that no location information is available, or the type ID
618of a ``BTF_KIND_LOC_PARAM``.
619
6202.2.22 BTF_KIND_LOCSEC
621~~~~~~~~~~~~~~~~~~~~~~
622
623``struct btf_type`` encoding requirement:
624  * ``name_off``: offset to a valid ELF section name
625  * ``info.kind_flag``: 0
626  * ``info.kind``: BTF_KIND_LOCSEC
627  * ``info.vlen``: number of inline sites in the section
628  * ``type``: 0
629
630``btf_type`` is followed by ``info.vlen`` number of ``struct btf_loc``.::
631
632    struct btf_loc {
633        __u32 func;
634        __u32 loc_proto;
635        __u32 offset;
636    };
637
638The ``func`` field is the non-zero type ID of the ``BTF_KIND_FUNC`` being
639described. ``loc_proto`` is the non-zero type ID of the associated
640``BTF_KIND_LOC_PROTO``. ``offset`` is the location address offset relative to
641the runtime base address of the ELF section associated with the LOCSEC.
642For example, a LOCSEC named ``inline.text`` contains records for ``.text``
643whose offsets are relative to the runtime base of that section.
644
6452.3 Constant Values
646-------------------
647
648.. _BTF_Function_Linkage_Constants:
649
6502.3.1 Function Linkage Constant Values
651~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
652.. table:: Function Linkage Values and Meanings
653
654  ===================  =====  ===========
655  kind                 value  description
656  ===================  =====  ===========
657  ``BTF_FUNC_STATIC``  0x0    definition of subprogram not visible outside containing compilation unit
658  ``BTF_FUNC_GLOBAL``  0x1    definition of subprogram visible outside containing compilation unit
659  ``BTF_FUNC_EXTERN``  0x2    declaration of a subprogram whose definition is outside the containing compilation unit
660  ===================  =====  ===========
661
662
663.. _BTF_Var_Linkage_Constants:
664
6652.3.2 Variable Linkage Constant Values
666~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
667.. table:: Variable Linkage Values and Meanings
668
669  ============================  =====  ===========
670  kind                          value  description
671  ============================  =====  ===========
672  ``BTF_VAR_STATIC``            0x0    definition of global variable not visible outside containing compilation unit
673  ``BTF_VAR_GLOBAL_ALLOCATED``  0x1    definition of global variable visible outside containing compilation unit
674  ``BTF_VAR_GLOBAL_EXTERN``     0x2    declaration of global variable whose definition is outside the containing compilation unit
675  ============================  =====  ===========
676
6773. BTF Kernel API
678=================
679
680The following bpf syscall command involves BTF:
681   * BPF_BTF_LOAD: load a blob of BTF data into kernel
682   * BPF_MAP_CREATE: map creation with btf key and value type info.
683   * BPF_PROG_LOAD: prog load with btf function and line info.
684   * BPF_BTF_GET_FD_BY_ID: get a btf fd
685   * BPF_OBJ_GET_INFO_BY_FD: btf, func_info, line_info
686     and other btf related info are returned.
687
688The workflow typically looks like:
689::
690
691  Application:
692      BPF_BTF_LOAD
693          |
694          v
695      BPF_MAP_CREATE and BPF_PROG_LOAD
696          |
697          V
698      ......
699
700  Introspection tool:
701      ......
702      BPF_{PROG,MAP}_GET_NEXT_ID (get prog/map id's)
703          |
704          V
705      BPF_{PROG,MAP}_GET_FD_BY_ID (get a prog/map fd)
706          |
707          V
708      BPF_OBJ_GET_INFO_BY_FD (get bpf_prog_info/bpf_map_info with btf_id)
709          |                                     |
710          V                                     |
711      BPF_BTF_GET_FD_BY_ID (get btf_fd)         |
712          |                                     |
713          V                                     |
714      BPF_OBJ_GET_INFO_BY_FD (get btf)          |
715          |                                     |
716          V                                     V
717      pretty print types, dump func signatures and line info, etc.
718
719
7203.1 BPF_BTF_LOAD
721----------------
722
723Load a blob of BTF data into kernel. A blob of data, described in
724:ref:`BTF_Type_String`, can be directly loaded into the kernel. A ``btf_fd``
725is returned to a userspace.
726
7273.2 BPF_MAP_CREATE
728------------------
729
730A map can be created with ``btf_fd`` and specified key/value type id.::
731
732    __u32   btf_fd;         /* fd pointing to a BTF type data */
733    __u32   btf_key_type_id;        /* BTF type_id of the key */
734    __u32   btf_value_type_id;      /* BTF type_id of the value */
735
736In libbpf, the map can be defined with extra annotation like below:
737::
738
739    struct {
740        __uint(type, BPF_MAP_TYPE_ARRAY);
741        __type(key, int);
742        __type(value, struct ipv_counts);
743        __uint(max_entries, 4);
744    } btf_map SEC(".maps");
745
746During ELF parsing, libbpf is able to extract key/value type_id's and assign
747them to BPF_MAP_CREATE attributes automatically.
748
749.. _BPF_Prog_Load:
750
7513.3 BPF_PROG_LOAD
752-----------------
753
754During prog_load, func_info and line_info can be passed to kernel with proper
755values for the following attributes:
756::
757
758    __u32           insn_cnt;
759    __aligned_u64   insns;
760    ......
761    __u32           prog_btf_fd;    /* fd pointing to BTF type data */
762    __u32           func_info_rec_size;     /* userspace bpf_func_info size */
763    __aligned_u64   func_info;      /* func info */
764    __u32           func_info_cnt;  /* number of bpf_func_info records */
765    __u32           line_info_rec_size;     /* userspace bpf_line_info size */
766    __aligned_u64   line_info;      /* line info */
767    __u32           line_info_cnt;  /* number of bpf_line_info records */
768
769The func_info and line_info are an array of below, respectively.::
770
771    struct bpf_func_info {
772        __u32   insn_off; /* [0, insn_cnt - 1] */
773        __u32   type_id;  /* pointing to a BTF_KIND_FUNC type */
774    };
775    struct bpf_line_info {
776        __u32   insn_off; /* [0, insn_cnt - 1] */
777        __u32   file_name_off; /* offset to string table for the filename */
778        __u32   line_off; /* offset to string table for the source line */
779        __u32   line_col; /* line number and column number */
780    };
781
782func_info_rec_size is the size of each func_info record, and
783line_info_rec_size is the size of each line_info record. Passing the record
784size to kernel make it possible to extend the record itself in the future.
785
786Below are requirements for func_info:
787  * func_info[0].insn_off must be 0.
788  * the func_info insn_off is in strictly increasing order and matches
789    bpf func boundaries.
790
791Below are requirements for line_info:
792  * the first insn in each func must have a line_info record pointing to it.
793  * the line_info insn_off is in strictly increasing order.
794
795For line_info, the line number and column number are defined as below:
796::
797
798    #define BPF_LINE_INFO_LINE_NUM(line_col)        ((line_col) >> 10)
799    #define BPF_LINE_INFO_LINE_COL(line_col)        ((line_col) & 0x3ff)
800
8013.4 BPF_{PROG,MAP}_GET_NEXT_ID
802------------------------------
803
804In kernel, every loaded program, map or btf has a unique id. The id won't
805change during the lifetime of a program, map, or btf.
806
807The bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID returns all id's, one for
808each command, to user space, for bpf program or maps, respectively, so an
809inspection tool can inspect all programs and maps.
810
8113.5 BPF_{PROG,MAP}_GET_FD_BY_ID
812-------------------------------
813
814An introspection tool cannot use id to get details about program or maps.
815A file descriptor needs to be obtained first for reference-counting purpose.
816
8173.6 BPF_OBJ_GET_INFO_BY_FD
818--------------------------
819
820Once a program/map fd is acquired, an introspection tool can get the detailed
821information from kernel about this fd, some of which are BTF-related. For
822example, ``bpf_map_info`` returns ``btf_id`` and key/value type ids.
823``bpf_prog_info`` returns ``btf_id``, func_info, and line info for translated
824bpf byte codes, and jited_line_info.
825
8263.7 BPF_BTF_GET_FD_BY_ID
827------------------------
828
829With ``btf_id`` obtained in ``bpf_map_info`` and ``bpf_prog_info``, bpf
830syscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd. Then, with
831command BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally loaded into the
832kernel with BPF_BTF_LOAD, can be retrieved.
833
834With the btf blob, ``bpf_map_info``, and ``bpf_prog_info``, an introspection
835tool has full btf knowledge and is able to pretty print map key/values, dump
836func signatures and line info, along with byte/jit codes.
837
8384. ELF File Format Interface
839============================
840
8414.1 .BTF section
842----------------
843
844The .BTF section contains type and string data. The format of this section is
845same as the one describe in :ref:`BTF_Type_String`.
846
847.. _BTF_Ext_Section:
848
8494.2 .BTF.ext section
850--------------------
851
852The .BTF.ext section encodes func_info, line_info and CO-RE relocations
853which needs loader manipulation before loading into the kernel.
854
855The specification for .BTF.ext section is defined at ``tools/lib/bpf/btf.h``
856and ``tools/lib/bpf/btf.c``.
857
858The current header of .BTF.ext section::
859
860    struct btf_ext_header {
861        __u16   magic;
862        __u8    version;
863        __u8    flags;
864        __u32   hdr_len;
865
866        /* All offsets are in bytes relative to the end of this header */
867        __u32   func_info_off;
868        __u32   func_info_len;
869        __u32   line_info_off;
870        __u32   line_info_len;
871
872        /* optional part of .BTF.ext header */
873        __u32   core_relo_off;
874        __u32   core_relo_len;
875    };
876
877It is very similar to .BTF section. Instead of type/string section, it
878contains func_info, line_info and core_relo sub-sections.
879See :ref:`BPF_Prog_Load` for details about func_info and line_info
880record format.
881
882The func_info is organized as below.::
883
884     func_info_rec_size              /* __u32 value */
885     btf_ext_info_sec for section #1 /* func_info for section #1 */
886     btf_ext_info_sec for section #2 /* func_info for section #2 */
887     ...
888
889``func_info_rec_size`` specifies the size of ``bpf_func_info`` structure when
890.BTF.ext is generated. ``btf_ext_info_sec``, defined below, is a collection of
891func_info for each specific ELF section.::
892
893     struct btf_ext_info_sec {
894        __u32   sec_name_off; /* offset to section name */
895        __u32   num_info;
896        /* Followed by num_info * record_size number of bytes */
897        __u8    data[0];
898     };
899
900Here, num_info must be greater than 0.
901
902The line_info is organized as below.::
903
904     line_info_rec_size              /* __u32 value */
905     btf_ext_info_sec for section #1 /* line_info for section #1 */
906     btf_ext_info_sec for section #2 /* line_info for section #2 */
907     ...
908
909``line_info_rec_size`` specifies the size of ``bpf_line_info`` structure when
910.BTF.ext is generated.
911
912The interpretation of ``bpf_func_info->insn_off`` and
913``bpf_line_info->insn_off`` is different between kernel API and ELF API. For
914kernel API, the ``insn_off`` is the instruction offset in the unit of ``struct
915bpf_insn``. For ELF API, the ``insn_off`` is the byte offset from the
916beginning of section (``btf_ext_info_sec->sec_name_off``).
917
918The core_relo is organized as below.::
919
920     core_relo_rec_size              /* __u32 value */
921     btf_ext_info_sec for section #1 /* core_relo for section #1 */
922     btf_ext_info_sec for section #2 /* core_relo for section #2 */
923
924``core_relo_rec_size`` specifies the size of ``bpf_core_relo``
925structure when .BTF.ext is generated. All ``bpf_core_relo`` structures
926within a single ``btf_ext_info_sec`` describe relocations applied to
927section named by ``btf_ext_info_sec->sec_name_off``.
928
929See :ref:`Documentation/bpf/llvm_reloc.rst <btf-co-re-relocations>`
930for more information on CO-RE relocations.
931
9324.3 .BTF_ids section
933--------------------
934
935The .BTF_ids section encodes BTF ID values that are used within the kernel.
936
937This section is created during the kernel compilation with the help of
938macros defined in ``include/linux/btf_ids.h`` header file. Kernel code can
939use them to create lists and sets (sorted lists) of BTF ID values.
940
941The ``BTF_ID_LIST`` and ``BTF_ID`` macros define unsorted list of BTF ID values,
942with following syntax::
943
944  BTF_ID_LIST(list)
945  BTF_ID(type1, name1)
946  BTF_ID(type2, name2)
947
948resulting in following layout in .BTF_ids section::
949
950  __BTF_ID__type1__name1__1:
951  .zero 4
952  __BTF_ID__type2__name2__2:
953  .zero 4
954
955The ``u32 list[];`` variable is defined to access the list.
956
957The ``BTF_ID_UNUSED`` macro defines 4 zero bytes. It's used when we
958want to define unused entry in BTF_ID_LIST, like::
959
960      BTF_ID_LIST(bpf_skb_output_btf_ids)
961      BTF_ID(struct, sk_buff)
962      BTF_ID_UNUSED
963      BTF_ID(struct, task_struct)
964
965The ``BTF_SET_START/END`` macros pair defines sorted list of BTF ID values
966and their count, with following syntax::
967
968  BTF_SET_START(set)
969  BTF_ID(type1, name1)
970  BTF_ID(type2, name2)
971  BTF_SET_END(set)
972
973resulting in following layout in .BTF_ids section::
974
975  __BTF_ID__set__set:
976  .zero 4
977  __BTF_ID__type1__name1__3:
978  .zero 4
979  __BTF_ID__type2__name2__4:
980  .zero 4
981
982The ``struct btf_id_set set;`` variable is defined to access the list.
983
984The ``typeX`` name can be one of following::
985
986   struct, union, typedef, func
987
988and is used as a filter when resolving the BTF ID value.
989
990All the BTF ID lists and sets are compiled in the .BTF_ids section and
991resolved during the linking phase of kernel build by ``resolve_btfids`` tool.
992
9934.4 .BTF.base section
994---------------------
995Split BTF - where the .BTF section only contains types not in the associated
996base .BTF section - is an extremely efficient way to encode type information
997for kernel modules, since they generally consist of a few module-specific
998types along with a large set of shared kernel types. The former are encoded
999in split BTF, while the latter are encoded in base BTF, resulting in more
1000compact representations. A type in split BTF that refers to a type in
1001base BTF refers to it using its base BTF ID, and split BTF IDs start
1002at last_base_BTF_ID + 1.
1003
1004The downside of this approach however is that this makes the split BTF
1005somewhat brittle - when the base BTF changes, base BTF ID references are
1006no longer valid and the split BTF itself becomes useless. The role of the
1007.BTF.base section is to make split BTF more resilient for cases where
1008the base BTF may change, as is the case for kernel modules not built every
1009time the kernel is for example. .BTF.base contains named base types; INTs,
1010FLOATs, STRUCTs, UNIONs, ENUM[64]s and FWDs. INTs and FLOATs are fully
1011described in .BTF.base sections, while composite types like structs
1012and unions are not fully defined - the .BTF.base type simply serves as
1013a description of the type the split BTF referred to, so structs/unions
1014have 0 members in the .BTF.base section. ENUM[64]s are similarly recorded
1015with 0 members. Any other types are added to the split BTF. This
1016distillation process then leaves us with a .BTF.base section with
1017such minimal descriptions of base types and .BTF split section which refers
1018to those base types. Later, we can relocate the split BTF using both the
1019information stored in the .BTF.base section and the new .BTF base; the type
1020information in the .BTF.base section allows us to update the split BTF
1021references to point at the corresponding new base BTF IDs.
1022
1023BTF relocation happens on kernel module load when a kernel module has a
1024.BTF.base section, and libbpf also provides a btf__relocate() API to
1025accomplish this.
1026
1027As an example consider the following base BTF::
1028
1029      [1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
1030      [2] STRUCT 'foo' size=8 vlen=2
1031              'f1' type_id=1 bits_offset=0
1032              'f2' type_id=1 bits_offset=32
1033
1034...and associated split BTF::
1035
1036      [3] PTR '(anon)' type_id=2
1037
1038i.e. split BTF describes a pointer to struct foo { int f1; int f2 };
1039
1040.BTF.base will consist of::
1041
1042      [1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
1043      [2] STRUCT 'foo' size=8 vlen=0
1044
1045If we relocate the split BTF later using the following new base BTF::
1046
1047      [1] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
1048      [2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
1049      [3] STRUCT 'foo' size=8 vlen=2
1050              'f1' type_id=2 bits_offset=0
1051              'f2' type_id=2 bits_offset=32
1052
1053...we can use our .BTF.base description to know that the split BTF reference
1054is to struct foo, and relocation results in new split BTF::
1055
1056      [4] PTR '(anon)' type_id=3
1057
1058Note that we had to update BTF ID and start BTF ID for the split BTF.
1059
1060So we see how .BTF.base plays the role of facilitating later relocation,
1061leading to more resilient split BTF.
1062
1063.BTF.base sections will be generated automatically for out-of-tree kernel module
1064builds - i.e. where KBUILD_EXTMOD is set (as it would be for "make M=path/2/mod"
1065cases). .BTF.base generation requires pahole support for the "distilled_base"
1066BTF feature; this is available in pahole v1.28 and later.
1067
10685. Using BTF
1069============
1070
10715.1 bpftool map pretty print
1072----------------------------
1073
1074With BTF, the map key/value can be printed based on fields rather than simply
1075raw bytes. This is especially valuable for large structure or if your data
1076structure has bitfields. For example, for the following map,::
1077
1078      enum A { A1, A2, A3, A4, A5 };
1079      typedef enum A ___A;
1080      struct tmp_t {
1081           char a1:4;
1082           int  a2:4;
1083           int  :4;
1084           __u32 a3:4;
1085           int b;
1086           ___A b1:4;
1087           enum A b2:4;
1088      };
1089      struct {
1090           __uint(type, BPF_MAP_TYPE_ARRAY);
1091           __type(key, int);
1092           __type(value, struct tmp_t);
1093           __uint(max_entries, 1);
1094      } tmpmap SEC(".maps");
1095
1096bpftool is able to pretty print like below:
1097::
1098
1099      [{
1100            "key": 0,
1101            "value": {
1102                "a1": 0x2,
1103                "a2": 0x4,
1104                "a3": 0x6,
1105                "b": 7,
1106                "b1": 0x8,
1107                "b2": 0xa
1108            }
1109        }
1110      ]
1111
11125.2 bpftool prog dump
1113---------------------
1114
1115The following is an example showing how func_info and line_info can help prog
1116dump with better kernel symbol names, function prototypes and line
1117information.::
1118
1119    $ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
1120    [...]
1121    int test_long_fname_2(struct dummy_tracepoint_args * arg):
1122    bpf_prog_44a040bf25481309_test_long_fname_2:
1123    ; static int test_long_fname_2(struct dummy_tracepoint_args *arg)
1124       0:   push   %rbp
1125       1:   mov    %rsp,%rbp
1126       4:   sub    $0x30,%rsp
1127       b:   sub    $0x28,%rbp
1128       f:   mov    %rbx,0x0(%rbp)
1129      13:   mov    %r13,0x8(%rbp)
1130      17:   mov    %r14,0x10(%rbp)
1131      1b:   mov    %r15,0x18(%rbp)
1132      1f:   xor    %eax,%eax
1133      21:   mov    %rax,0x20(%rbp)
1134      25:   xor    %esi,%esi
1135    ; int key = 0;
1136      27:   mov    %esi,-0x4(%rbp)
1137    ; if (!arg->sock)
1138      2a:   mov    0x8(%rdi),%rdi
1139    ; if (!arg->sock)
1140      2e:   cmp    $0x0,%rdi
1141      32:   je     0x0000000000000070
1142      34:   mov    %rbp,%rsi
1143    ; counts = bpf_map_lookup_elem(&btf_map, &key);
1144    [...]
1145
11465.3 Verifier Log
1147----------------
1148
1149The following is an example of how line_info can help debugging verification
1150failure.::
1151
1152       /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c
1153        * is modified as below.
1154        */
1155       data = (void *)(long)xdp->data;
1156       data_end = (void *)(long)xdp->data_end;
1157       /*
1158       if (data + 4 > data_end)
1159               return XDP_DROP;
1160       */
1161       *(u32 *)data = dst->dst;
1162
1163    $ bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp
1164        ; data = (void *)(long)xdp->data;
1165        224: (79) r2 = *(u64 *)(r10 -112)
1166        225: (61) r2 = *(u32 *)(r2 +0)
1167        ; *(u32 *)data = dst->dst;
1168        226: (63) *(u32 *)(r2 +0) = r1
1169        invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0)
1170        R2 offset is outside of the packet
1171
11726. BTF Generation
1173=================
1174
1175You need latest pahole
1176
1177  https://git.kernel.org/pub/scm/devel/pahole/pahole.git/
1178
1179or llvm (8.0 or later). The pahole acts as a dwarf2btf converter. It doesn't
1180support .BTF.ext and btf BTF_KIND_FUNC type yet. For example,::
1181
1182      -bash-4.4$ cat t.c
1183      struct t {
1184        int a:2;
1185        int b:3;
1186        int c:2;
1187      } g;
1188      -bash-4.4$ gcc -c -O2 -g t.c
1189      -bash-4.4$ pahole -JV t.o
1190      File t.o:
1191      [1] STRUCT t kind_flag=1 size=4 vlen=3
1192              a type_id=2 bitfield_size=2 bits_offset=0
1193              b type_id=2 bitfield_size=3 bits_offset=2
1194              c type_id=2 bitfield_size=2 bits_offset=5
1195      [2] INT int size=4 bit_offset=0 nr_bits=32 encoding=SIGNED
1196
1197The llvm is able to generate .BTF and .BTF.ext directly with -g for bpf target
1198only. The assembly code (-S) is able to show the BTF encoding in assembly
1199format.::
1200
1201    -bash-4.4$ cat t2.c
1202    typedef int __int32;
1203    struct t2 {
1204      int a2;
1205      int (*f2)(char q1, __int32 q2, ...);
1206      int (*f3)();
1207    } g2;
1208    int main() { return 0; }
1209    int test() { return 0; }
1210    -bash-4.4$ clang -c -g -O2 --target=bpf t2.c
1211    -bash-4.4$ readelf -S t2.o
1212      ......
1213      [ 8] .BTF              PROGBITS         0000000000000000  00000247
1214           000000000000016e  0000000000000000           0     0     1
1215      [ 9] .BTF.ext          PROGBITS         0000000000000000  000003b5
1216           0000000000000060  0000000000000000           0     0     1
1217      [10] .rel.BTF.ext      REL              0000000000000000  000007e0
1218           0000000000000040  0000000000000010          16     9     8
1219      ......
1220    -bash-4.4$ clang -S -g -O2 --target=bpf t2.c
1221    -bash-4.4$ cat t2.s
1222      ......
1223            .section        .BTF,"",@progbits
1224            .short  60319                   # 0xeb9f
1225            .byte   1
1226            .byte   0
1227            .long   24
1228            .long   0
1229            .long   220
1230            .long   220
1231            .long   122
1232            .long   0                       # BTF_KIND_FUNC_PROTO(id = 1)
1233            .long   218103808               # 0xd000000
1234            .long   2
1235            .long   83                      # BTF_KIND_INT(id = 2)
1236            .long   16777216                # 0x1000000
1237            .long   4
1238            .long   16777248                # 0x1000020
1239      ......
1240            .byte   0                       # string offset=0
1241            .ascii  ".text"                 # string offset=1
1242            .byte   0
1243            .ascii  "/home/yhs/tmp-pahole/t2.c" # string offset=7
1244            .byte   0
1245            .ascii  "int main() { return 0; }" # string offset=33
1246            .byte   0
1247            .ascii  "int test() { return 0; }" # string offset=58
1248            .byte   0
1249            .ascii  "int"                   # string offset=83
1250      ......
1251            .section        .BTF.ext,"",@progbits
1252            .short  60319                   # 0xeb9f
1253            .byte   1
1254            .byte   0
1255            .long   24
1256            .long   0
1257            .long   28
1258            .long   28
1259            .long   44
1260            .long   8                       # FuncInfo
1261            .long   1                       # FuncInfo section string offset=1
1262            .long   2
1263            .long   .Lfunc_begin0
1264            .long   3
1265            .long   .Lfunc_begin1
1266            .long   5
1267            .long   16                      # LineInfo
1268            .long   1                       # LineInfo section string offset=1
1269            .long   2
1270            .long   .Ltmp0
1271            .long   7
1272            .long   33
1273            .long   7182                    # Line 7 Col 14
1274            .long   .Ltmp3
1275            .long   7
1276            .long   58
1277            .long   8206                    # Line 8 Col 14
1278
12797. Testing
1280==========
1281
1282The kernel BPF selftest `tools/testing/selftests/bpf/prog_tests/btf.c`_
1283provides an extensive set of BTF-related tests.
1284
1285.. Links
1286.. _tools/testing/selftests/bpf/prog_tests/btf.c:
1287   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/btf.c
1288