xref: /linux/Documentation/bpf/btf.rst (revision 06d07429858317ded2db7986113a9e0129cd599b)
1ffcf7ce9SYonghong Song=====================
2ffcf7ce9SYonghong SongBPF Type Format (BTF)
3ffcf7ce9SYonghong Song=====================
4ffcf7ce9SYonghong Song
5ffcf7ce9SYonghong Song1. Introduction
63ff36bffSDave Tucker===============
7ffcf7ce9SYonghong Song
89ab5305dSAndrii NakryikoBTF (BPF Type Format) is the metadata format which encodes the debug info
99ab5305dSAndrii Nakryikorelated to BPF program/map. The name BTF was used initially to describe data
109ab5305dSAndrii Nakryikotypes. The BTF was later extended to include function info for defined
119ab5305dSAndrii Nakryikosubroutines, and line info for source/line information.
12ffcf7ce9SYonghong Song
139ab5305dSAndrii NakryikoThe debug info is used for map pretty print, function signature, etc. The
149ab5305dSAndrii Nakryikofunction signature enables better bpf program/function kernel symbol. The line
159ab5305dSAndrii Nakryikoinfo helps generate source annotated translated byte code, jited code and
169ab5305dSAndrii Nakryikoverifier log.
17ffcf7ce9SYonghong Song
18ffcf7ce9SYonghong SongThe BTF specification contains two parts,
19ffcf7ce9SYonghong Song  * BTF kernel API
20ffcf7ce9SYonghong Song  * BTF ELF file format
21ffcf7ce9SYonghong Song
229ab5305dSAndrii NakryikoThe kernel API is the contract between user space and kernel. The kernel
239ab5305dSAndrii Nakryikoverifies the BTF info before using it. The ELF file format is a user space
249ab5305dSAndrii Nakryikocontract between ELF file and libbpf loader.
25ffcf7ce9SYonghong Song
269ab5305dSAndrii NakryikoThe type and string sections are part of the BTF kernel API, describing the
279ab5305dSAndrii Nakryikodebug info (mostly types related) referenced by the bpf program. These two
289ab5305dSAndrii Nakryikosections are discussed in details in :ref:`BTF_Type_String`.
29ffcf7ce9SYonghong Song
30ffcf7ce9SYonghong Song.. _BTF_Type_String:
31ffcf7ce9SYonghong Song
32ffcf7ce9SYonghong Song2. BTF Type and String Encoding
333ff36bffSDave Tucker===============================
34ffcf7ce9SYonghong Song
359ab5305dSAndrii NakryikoThe file ``include/uapi/linux/btf.h`` provides high-level definition of how
369ab5305dSAndrii Nakryikotypes/strings are encoded.
37ffcf7ce9SYonghong Song
38ffcf7ce9SYonghong SongThe beginning of data blob must be::
39ffcf7ce9SYonghong Song
40ffcf7ce9SYonghong Song    struct btf_header {
41ffcf7ce9SYonghong Song        __u16   magic;
42ffcf7ce9SYonghong Song        __u8    version;
43ffcf7ce9SYonghong Song        __u8    flags;
44ffcf7ce9SYonghong Song        __u32   hdr_len;
45ffcf7ce9SYonghong Song
46ffcf7ce9SYonghong Song        /* All offsets are in bytes relative to the end of this header */
47ffcf7ce9SYonghong Song        __u32   type_off;       /* offset of type section       */
48ffcf7ce9SYonghong Song        __u32   type_len;       /* length of type section       */
49ffcf7ce9SYonghong Song        __u32   str_off;        /* offset of string section     */
50ffcf7ce9SYonghong Song        __u32   str_len;        /* length of string section     */
51ffcf7ce9SYonghong Song    };
52ffcf7ce9SYonghong Song
53ffcf7ce9SYonghong SongThe magic is ``0xeB9F``, which has different encoding for big and little
549ab5305dSAndrii Nakryikoendian systems, and can be used to test whether BTF is generated for big- or
559ab5305dSAndrii Nakryikolittle-endian target. The ``btf_header`` is designed to be extensible with
569ab5305dSAndrii Nakryiko``hdr_len`` equal to ``sizeof(struct btf_header)`` when a data blob is
579ab5305dSAndrii Nakryikogenerated.
58ffcf7ce9SYonghong Song
59ffcf7ce9SYonghong Song2.1 String Encoding
603ff36bffSDave Tucker-------------------
61ffcf7ce9SYonghong Song
629ab5305dSAndrii NakryikoThe first string in the string section must be a null string. The rest of
639ab5305dSAndrii Nakryikostring table is a concatenation of other null-terminated strings.
64ffcf7ce9SYonghong Song
65ffcf7ce9SYonghong Song2.2 Type Encoding
663ff36bffSDave Tucker-----------------
67ffcf7ce9SYonghong Song
689ab5305dSAndrii NakryikoThe type id ``0`` is reserved for ``void`` type. The type section is parsed
699ab5305dSAndrii Nakryikosequentially and type id is assigned to each recognized type starting from id
709ab5305dSAndrii Nakryiko``1``. Currently, the following types are supported::
71ffcf7ce9SYonghong Song
72ffcf7ce9SYonghong Song    #define BTF_KIND_INT            1       /* Integer      */
73ffcf7ce9SYonghong Song    #define BTF_KIND_PTR            2       /* Pointer      */
74ffcf7ce9SYonghong Song    #define BTF_KIND_ARRAY          3       /* Array        */
75ffcf7ce9SYonghong Song    #define BTF_KIND_STRUCT         4       /* Struct       */
76ffcf7ce9SYonghong Song    #define BTF_KIND_UNION          5       /* Union        */
7761dbd598SYonghong Song    #define BTF_KIND_ENUM           6       /* Enumeration up to 32-bit values */
78ffcf7ce9SYonghong Song    #define BTF_KIND_FWD            7       /* Forward      */
79ffcf7ce9SYonghong Song    #define BTF_KIND_TYPEDEF        8       /* Typedef      */
80ffcf7ce9SYonghong Song    #define BTF_KIND_VOLATILE       9       /* Volatile     */
81ffcf7ce9SYonghong Song    #define BTF_KIND_CONST          10      /* Const        */
82ffcf7ce9SYonghong Song    #define BTF_KIND_RESTRICT       11      /* Restrict     */
83ffcf7ce9SYonghong Song    #define BTF_KIND_FUNC           12      /* Function     */
84ffcf7ce9SYonghong Song    #define BTF_KIND_FUNC_PROTO     13      /* Function Proto       */
85f063c889SDaniel Borkmann    #define BTF_KIND_VAR            14      /* Variable     */
86f063c889SDaniel Borkmann    #define BTF_KIND_DATASEC        15      /* Section      */
876be6a0baSIlya Leoshkevich    #define BTF_KIND_FLOAT          16      /* Floating point       */
88223f903eSYonghong Song    #define BTF_KIND_DECL_TAG       17      /* Decl Tag     */
89d52f5c63SYonghong Song    #define BTF_KIND_TYPE_TAG       18      /* Type Tag     */
9061dbd598SYonghong Song    #define BTF_KIND_ENUM64         19      /* Enumeration up to 64-bit values */
91ffcf7ce9SYonghong Song
92ffcf7ce9SYonghong SongNote that the type section encodes debug info, not just pure types.
93ffcf7ce9SYonghong Song``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
94ffcf7ce9SYonghong Song
95ffcf7ce9SYonghong SongEach type contains the following common data::
96ffcf7ce9SYonghong Song
97ffcf7ce9SYonghong Song    struct btf_type {
98ffcf7ce9SYonghong Song        __u32 name_off;
99ffcf7ce9SYonghong Song        /* "info" bits arrangement
100ffcf7ce9SYonghong Song         * bits  0-15: vlen (e.g. # of struct's members)
101ffcf7ce9SYonghong Song         * bits 16-23: unused
1026be6a0baSIlya Leoshkevich         * bits 24-28: kind (e.g. int, ptr, array...etc)
1036be6a0baSIlya Leoshkevich         * bits 29-30: unused
104ffcf7ce9SYonghong Song         * bit     31: kind_flag, currently used by
10561dbd598SYonghong Song         *             struct, union, fwd, enum and enum64.
106ffcf7ce9SYonghong Song         */
107ffcf7ce9SYonghong Song        __u32 info;
10861dbd598SYonghong Song        /* "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.
109ffcf7ce9SYonghong Song         * "size" tells the size of the type it is describing.
110ffcf7ce9SYonghong Song         *
111ffcf7ce9SYonghong Song         * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
112d52f5c63SYonghong Song         * FUNC, FUNC_PROTO, DECL_TAG and TYPE_TAG.
113ffcf7ce9SYonghong Song         * "type" is a type_id referring to another type.
114ffcf7ce9SYonghong Song         */
115ffcf7ce9SYonghong Song        union {
116ffcf7ce9SYonghong Song                __u32 size;
117ffcf7ce9SYonghong Song                __u32 type;
118ffcf7ce9SYonghong Song        };
119ffcf7ce9SYonghong Song    };
120ffcf7ce9SYonghong Song
1219ab5305dSAndrii NakryikoFor certain kinds, the common data are followed by kind-specific data. The
1229ab5305dSAndrii Nakryiko``name_off`` in ``struct btf_type`` specifies the offset in the string table.
1239ab5305dSAndrii NakryikoThe following sections detail encoding of each kind.
124ffcf7ce9SYonghong Song
125ffcf7ce9SYonghong Song2.2.1 BTF_KIND_INT
126ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~
127ffcf7ce9SYonghong Song
128ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
129ffcf7ce9SYonghong Song * ``name_off``: any valid offset
130ffcf7ce9SYonghong Song * ``info.kind_flag``: 0
131ffcf7ce9SYonghong Song * ``info.kind``: BTF_KIND_INT
132ffcf7ce9SYonghong Song * ``info.vlen``: 0
133ffcf7ce9SYonghong Song * ``size``: the size of the int type in bytes.
134ffcf7ce9SYonghong Song
1355efc529fSAndrii Nakryiko``btf_type`` is followed by a ``u32`` with the following bits arrangement::
136ffcf7ce9SYonghong Song
137ffcf7ce9SYonghong Song  #define BTF_INT_ENCODING(VAL)   (((VAL) & 0x0f000000) >> 24)
138948dc8c9SGary Lin  #define BTF_INT_OFFSET(VAL)     (((VAL) & 0x00ff0000) >> 16)
139ffcf7ce9SYonghong Song  #define BTF_INT_BITS(VAL)       ((VAL)  & 0x000000ff)
140ffcf7ce9SYonghong Song
141ffcf7ce9SYonghong SongThe ``BTF_INT_ENCODING`` has the following attributes::
142ffcf7ce9SYonghong Song
143ffcf7ce9SYonghong Song  #define BTF_INT_SIGNED  (1 << 0)
144ffcf7ce9SYonghong Song  #define BTF_INT_CHAR    (1 << 1)
145ffcf7ce9SYonghong Song  #define BTF_INT_BOOL    (1 << 2)
146ffcf7ce9SYonghong Song
1479ab5305dSAndrii NakryikoThe ``BTF_INT_ENCODING()`` provides extra information: signedness, char, or
1489ab5305dSAndrii Nakryikobool, for the int type. The char and bool encoding are mostly useful for
1499ab5305dSAndrii Nakryikopretty print. At most one encoding can be specified for the int type.
150ffcf7ce9SYonghong Song
1519ab5305dSAndrii NakryikoThe ``BTF_INT_BITS()`` specifies the number of actual bits held by this int
1529ab5305dSAndrii Nakryikotype. For example, a 4-bit bitfield encodes ``BTF_INT_BITS()`` equals to 4.
1539ab5305dSAndrii NakryikoThe ``btf_type.size * 8`` must be equal to or greater than ``BTF_INT_BITS()``
1549ab5305dSAndrii Nakryikofor the type. The maximum value of ``BTF_INT_BITS()`` is 128.
155ffcf7ce9SYonghong Song
1569ab5305dSAndrii NakryikoThe ``BTF_INT_OFFSET()`` specifies the starting bit offset to calculate values
157f52c97d9SJesper Dangaard Brouerfor this int. For example, a bitfield struct member has:
158d857a3ffSMauro Carvalho Chehab
159f52c97d9SJesper Dangaard Brouer * btf member bit offset 100 from the start of the structure,
160f52c97d9SJesper Dangaard Brouer * btf member pointing to an int type,
161f52c97d9SJesper Dangaard Brouer * the int type has ``BTF_INT_OFFSET() = 2`` and ``BTF_INT_BITS() = 4``
162ffcf7ce9SYonghong Song
1639ab5305dSAndrii NakryikoThen in the struct memory layout, this member will occupy ``4`` bits starting
1649ab5305dSAndrii Nakryikofrom bits ``100 + 2 = 102``.
165ffcf7ce9SYonghong Song
1669ab5305dSAndrii NakryikoAlternatively, the bitfield struct member can be the following to access the
1679ab5305dSAndrii Nakryikosame bits as the above:
168d857a3ffSMauro Carvalho Chehab
169ffcf7ce9SYonghong Song * btf member bit offset 102,
170ffcf7ce9SYonghong Song * btf member pointing to an int type,
171ffcf7ce9SYonghong Song * the int type has ``BTF_INT_OFFSET() = 0`` and ``BTF_INT_BITS() = 4``
172ffcf7ce9SYonghong Song
1739ab5305dSAndrii NakryikoThe original intention of ``BTF_INT_OFFSET()`` is to provide flexibility of
1749ab5305dSAndrii Nakryikobitfield encoding. Currently, both llvm and pahole generate
1759ab5305dSAndrii Nakryiko``BTF_INT_OFFSET() = 0`` for all int types.
176ffcf7ce9SYonghong Song
177ffcf7ce9SYonghong Song2.2.2 BTF_KIND_PTR
178ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~
179ffcf7ce9SYonghong Song
180ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
181ffcf7ce9SYonghong Song  * ``name_off``: 0
182ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
183ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_PTR
184ffcf7ce9SYonghong Song  * ``info.vlen``: 0
185ffcf7ce9SYonghong Song  * ``type``: the pointee type of the pointer
186ffcf7ce9SYonghong Song
187ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
188ffcf7ce9SYonghong Song
189ffcf7ce9SYonghong Song2.2.3 BTF_KIND_ARRAY
190ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~
191ffcf7ce9SYonghong Song
192ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
193ffcf7ce9SYonghong Song  * ``name_off``: 0
194ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
195ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_ARRAY
196ffcf7ce9SYonghong Song  * ``info.vlen``: 0
197ffcf7ce9SYonghong Song  * ``size/type``: 0, not used
198ffcf7ce9SYonghong Song
1995efc529fSAndrii Nakryiko``btf_type`` is followed by one ``struct btf_array``::
200ffcf7ce9SYonghong Song
201ffcf7ce9SYonghong Song    struct btf_array {
202ffcf7ce9SYonghong Song        __u32   type;
203ffcf7ce9SYonghong Song        __u32   index_type;
204ffcf7ce9SYonghong Song        __u32   nelems;
205ffcf7ce9SYonghong Song    };
206ffcf7ce9SYonghong Song
207ffcf7ce9SYonghong SongThe ``struct btf_array`` encoding:
208ffcf7ce9SYonghong Song  * ``type``: the element type
209ffcf7ce9SYonghong Song  * ``index_type``: the index type
210ffcf7ce9SYonghong Song  * ``nelems``: the number of elements for this array (``0`` is also allowed).
211ffcf7ce9SYonghong Song
2129ab5305dSAndrii NakryikoThe ``index_type`` can be any regular int type (``u8``, ``u16``, ``u32``,
2139ab5305dSAndrii Nakryiko``u64``, ``unsigned __int128``). The original design of including
2149ab5305dSAndrii Nakryiko``index_type`` follows DWARF, which has an ``index_type`` for its array type.
215ffcf7ce9SYonghong SongCurrently in BTF, beyond type verification, the ``index_type`` is not used.
216ffcf7ce9SYonghong Song
217ffcf7ce9SYonghong SongThe ``struct btf_array`` allows chaining through element type to represent
2189ab5305dSAndrii Nakryikomultidimensional arrays. For example, for ``int a[5][6]``, the following type
2199ab5305dSAndrii Nakryikoinformation illustrates the chaining:
220ffcf7ce9SYonghong Song
221ffcf7ce9SYonghong Song  * [1]: int
222ffcf7ce9SYonghong Song  * [2]: array, ``btf_array.type = [1]``, ``btf_array.nelems = 6``
223ffcf7ce9SYonghong Song  * [3]: array, ``btf_array.type = [2]``, ``btf_array.nelems = 5``
224ffcf7ce9SYonghong Song
2259ab5305dSAndrii NakryikoCurrently, both pahole and llvm collapse multidimensional array into
2269ab5305dSAndrii Nakryikoone-dimensional array, e.g., for ``a[5][6]``, the ``btf_array.nelems`` is
2279ab5305dSAndrii Nakryikoequal to ``30``. This is because the original use case is map pretty print
2289ab5305dSAndrii Nakryikowhere the whole array is dumped out so one-dimensional array is enough. As
2299ab5305dSAndrii Nakryikomore BTF usage is explored, pahole and llvm can be changed to generate proper
2309ab5305dSAndrii Nakryikochained representation for multidimensional arrays.
231ffcf7ce9SYonghong Song
232ffcf7ce9SYonghong Song2.2.4 BTF_KIND_STRUCT
233ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~
234ffcf7ce9SYonghong Song2.2.5 BTF_KIND_UNION
235ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~
236ffcf7ce9SYonghong Song
237ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
238ffcf7ce9SYonghong Song  * ``name_off``: 0 or offset to a valid C identifier
239ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0 or 1
240ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_STRUCT or BTF_KIND_UNION
241ffcf7ce9SYonghong Song  * ``info.vlen``: the number of struct/union members
242ffcf7ce9SYonghong Song  * ``info.size``: the size of the struct/union in bytes
243ffcf7ce9SYonghong Song
244ffcf7ce9SYonghong Song``btf_type`` is followed by ``info.vlen`` number of ``struct btf_member``.::
245ffcf7ce9SYonghong Song
246ffcf7ce9SYonghong Song    struct btf_member {
247ffcf7ce9SYonghong Song        __u32   name_off;
248ffcf7ce9SYonghong Song        __u32   type;
249ffcf7ce9SYonghong Song        __u32   offset;
250ffcf7ce9SYonghong Song    };
251ffcf7ce9SYonghong Song
252ffcf7ce9SYonghong Song``struct btf_member`` encoding:
253ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
254ffcf7ce9SYonghong Song  * ``type``: the member type
255ffcf7ce9SYonghong Song  * ``offset``: <see below>
256ffcf7ce9SYonghong Song
2579ab5305dSAndrii NakryikoIf the type info ``kind_flag`` is not set, the offset contains only bit offset
2589ab5305dSAndrii Nakryikoof the member. Note that the base type of the bitfield can only be int or enum
2599ab5305dSAndrii Nakryikotype. If the bitfield size is 32, the base type can be either int or enum
2609ab5305dSAndrii Nakryikotype. If the bitfield size is not 32, the base type must be int, and int type
2619ab5305dSAndrii Nakryiko``BTF_INT_BITS()`` encodes the bitfield size.
262ffcf7ce9SYonghong Song
2639ab5305dSAndrii NakryikoIf the ``kind_flag`` is set, the ``btf_member.offset`` contains both member
2649ab5305dSAndrii Nakryikobitfield size and bit offset. The bitfield size and bit offset are calculated
2659ab5305dSAndrii Nakryikoas below.::
266ffcf7ce9SYonghong Song
267ffcf7ce9SYonghong Song  #define BTF_MEMBER_BITFIELD_SIZE(val)   ((val) >> 24)
268ffcf7ce9SYonghong Song  #define BTF_MEMBER_BIT_OFFSET(val)      ((val) & 0xffffff)
269ffcf7ce9SYonghong Song
2709ab5305dSAndrii NakryikoIn this case, if the base type is an int type, it must be a regular int type:
271ffcf7ce9SYonghong Song
272ffcf7ce9SYonghong Song  * ``BTF_INT_OFFSET()`` must be 0.
273ffcf7ce9SYonghong Song  * ``BTF_INT_BITS()`` must be equal to ``{1,2,4,8,16} * 8``.
274ffcf7ce9SYonghong Song
275*86b17aafSVegard NossumCommit 9d5f9f701b18 introduced ``kind_flag`` and explains why both modes
276*86b17aafSVegard Nossumexist.
277ffcf7ce9SYonghong Song
278ffcf7ce9SYonghong Song2.2.6 BTF_KIND_ENUM
279ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~
280ffcf7ce9SYonghong Song
281ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
282ffcf7ce9SYonghong Song  * ``name_off``: 0 or offset to a valid C identifier
28361dbd598SYonghong Song  * ``info.kind_flag``: 0 for unsigned, 1 for signed
284ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_ENUM
285ffcf7ce9SYonghong Song  * ``info.vlen``: number of enum values
28661dbd598SYonghong Song  * ``size``: 1/2/4/8
287ffcf7ce9SYonghong Song
288ffcf7ce9SYonghong Song``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum``.::
289ffcf7ce9SYonghong Song
290ffcf7ce9SYonghong Song    struct btf_enum {
291ffcf7ce9SYonghong Song        __u32   name_off;
292ffcf7ce9SYonghong Song        __s32   val;
293ffcf7ce9SYonghong Song    };
294ffcf7ce9SYonghong Song
295ffcf7ce9SYonghong SongThe ``btf_enum`` encoding:
296ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
297ffcf7ce9SYonghong Song  * ``val``: any value
298ffcf7ce9SYonghong Song
29961dbd598SYonghong SongIf the original enum value is signed and the size is less than 4,
30061dbd598SYonghong Songthat value will be sign extended into 4 bytes. If the size is 8,
30161dbd598SYonghong Songthe value will be truncated into 4 bytes.
30261dbd598SYonghong Song
303ffcf7ce9SYonghong Song2.2.7 BTF_KIND_FWD
304ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~
305ffcf7ce9SYonghong Song
306ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
307ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
308ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0 for struct, 1 for union
309ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_FWD
310ffcf7ce9SYonghong Song  * ``info.vlen``: 0
311ffcf7ce9SYonghong Song  * ``type``: 0
312ffcf7ce9SYonghong Song
313ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
314ffcf7ce9SYonghong Song
315ffcf7ce9SYonghong Song2.2.8 BTF_KIND_TYPEDEF
316ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~
317ffcf7ce9SYonghong Song
318ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
319ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
320ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
321ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_TYPEDEF
322ffcf7ce9SYonghong Song  * ``info.vlen``: 0
323ffcf7ce9SYonghong Song  * ``type``: the type which can be referred by name at ``name_off``
324ffcf7ce9SYonghong Song
325ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
326ffcf7ce9SYonghong Song
327ffcf7ce9SYonghong Song2.2.9 BTF_KIND_VOLATILE
328ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~
329ffcf7ce9SYonghong Song
330ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
331ffcf7ce9SYonghong Song  * ``name_off``: 0
332ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
333ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_VOLATILE
334ffcf7ce9SYonghong Song  * ``info.vlen``: 0
335ffcf7ce9SYonghong Song  * ``type``: the type with ``volatile`` qualifier
336ffcf7ce9SYonghong Song
337ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
338ffcf7ce9SYonghong Song
339ffcf7ce9SYonghong Song2.2.10 BTF_KIND_CONST
340ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~
341ffcf7ce9SYonghong Song
342ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
343ffcf7ce9SYonghong Song  * ``name_off``: 0
344ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
345ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_CONST
346ffcf7ce9SYonghong Song  * ``info.vlen``: 0
347ffcf7ce9SYonghong Song  * ``type``: the type with ``const`` qualifier
348ffcf7ce9SYonghong Song
349ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
350ffcf7ce9SYonghong Song
351ffcf7ce9SYonghong Song2.2.11 BTF_KIND_RESTRICT
352ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~
353ffcf7ce9SYonghong Song
354ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
355ffcf7ce9SYonghong Song  * ``name_off``: 0
356ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
357ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_RESTRICT
358ffcf7ce9SYonghong Song  * ``info.vlen``: 0
359ffcf7ce9SYonghong Song  * ``type``: the type with ``restrict`` qualifier
360ffcf7ce9SYonghong Song
361ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
362ffcf7ce9SYonghong Song
363ffcf7ce9SYonghong Song2.2.12 BTF_KIND_FUNC
364ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~
365ffcf7ce9SYonghong Song
366ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
367ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
368ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
369ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_FUNC
370e5e23424SIndu Bhagat  * ``info.vlen``: linkage information (BTF_FUNC_STATIC, BTF_FUNC_GLOBAL
371e5e23424SIndu Bhagat                   or BTF_FUNC_EXTERN)
372ffcf7ce9SYonghong Song  * ``type``: a BTF_KIND_FUNC_PROTO type
373ffcf7ce9SYonghong Song
374ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
375ffcf7ce9SYonghong Song
3765efc529fSAndrii NakryikoA BTF_KIND_FUNC defines not a type, but a subprogram (function) whose
3779ab5305dSAndrii Nakryikosignature is defined by ``type``. The subprogram is thus an instance of that
3789ab5305dSAndrii Nakryikotype. The BTF_KIND_FUNC may in turn be referenced by a func_info in the
3799ab5305dSAndrii Nakryiko:ref:`BTF_Ext_Section` (ELF) or in the arguments to :ref:`BPF_Prog_Load`
3809ab5305dSAndrii Nakryiko(ABI).
381ffcf7ce9SYonghong Song
382e5e23424SIndu BhagatCurrently, only linkage values of BTF_FUNC_STATIC and BTF_FUNC_GLOBAL are
383e5e23424SIndu Bhagatsupported in the kernel.
384e5e23424SIndu Bhagat
385ffcf7ce9SYonghong Song2.2.13 BTF_KIND_FUNC_PROTO
386ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~~~
387ffcf7ce9SYonghong Song
388ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
389ffcf7ce9SYonghong Song  * ``name_off``: 0
390ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
391ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_FUNC_PROTO
392ffcf7ce9SYonghong Song  * ``info.vlen``: # of parameters
393ffcf7ce9SYonghong Song  * ``type``: the return type
394ffcf7ce9SYonghong Song
395ffcf7ce9SYonghong Song``btf_type`` is followed by ``info.vlen`` number of ``struct btf_param``.::
396ffcf7ce9SYonghong Song
397ffcf7ce9SYonghong Song    struct btf_param {
398ffcf7ce9SYonghong Song        __u32   name_off;
399ffcf7ce9SYonghong Song        __u32   type;
400ffcf7ce9SYonghong Song    };
401ffcf7ce9SYonghong Song
4029ab5305dSAndrii NakryikoIf a BTF_KIND_FUNC_PROTO type is referred by a BTF_KIND_FUNC type, then
4039ab5305dSAndrii Nakryiko``btf_param.name_off`` must point to a valid C identifier except for the
4049ab5305dSAndrii Nakryikopossible last argument representing the variable argument. The btf_param.type
4059ab5305dSAndrii Nakryikorefers to parameter type.
406ffcf7ce9SYonghong Song
4079ab5305dSAndrii NakryikoIf the function has variable arguments, the last parameter is encoded with
4089ab5305dSAndrii Nakryiko``name_off = 0`` and ``type = 0``.
409ffcf7ce9SYonghong Song
410f063c889SDaniel Borkmann2.2.14 BTF_KIND_VAR
411f063c889SDaniel Borkmann~~~~~~~~~~~~~~~~~~~
412f063c889SDaniel Borkmann
413f063c889SDaniel Borkmann``struct btf_type`` encoding requirement:
414f063c889SDaniel Borkmann  * ``name_off``: offset to a valid C identifier
415f063c889SDaniel Borkmann  * ``info.kind_flag``: 0
416f063c889SDaniel Borkmann  * ``info.kind``: BTF_KIND_VAR
417f063c889SDaniel Borkmann  * ``info.vlen``: 0
418f063c889SDaniel Borkmann  * ``type``: the type of the variable
419f063c889SDaniel Borkmann
420f063c889SDaniel Borkmann``btf_type`` is followed by a single ``struct btf_variable`` with the
421f063c889SDaniel Borkmannfollowing data::
422f063c889SDaniel Borkmann
423f063c889SDaniel Borkmann    struct btf_var {
424f063c889SDaniel Borkmann        __u32   linkage;
425f063c889SDaniel Borkmann    };
426f063c889SDaniel Borkmann
427f063c889SDaniel Borkmann``struct btf_var`` encoding:
428f063c889SDaniel Borkmann  * ``linkage``: currently only static variable 0, or globally allocated
429f063c889SDaniel Borkmann                 variable in ELF sections 1
430f063c889SDaniel Borkmann
431f063c889SDaniel BorkmannNot all type of global variables are supported by LLVM at this point.
432f063c889SDaniel BorkmannThe following is currently available:
433f063c889SDaniel Borkmann
434f063c889SDaniel Borkmann  * static variables with or without section attributes
435f063c889SDaniel Borkmann  * global variables with section attributes
436f063c889SDaniel Borkmann
437f063c889SDaniel BorkmannThe latter is for future extraction of map key/value type id's from a
438f063c889SDaniel Borkmannmap definition.
439f063c889SDaniel Borkmann
440f063c889SDaniel Borkmann2.2.15 BTF_KIND_DATASEC
441f063c889SDaniel Borkmann~~~~~~~~~~~~~~~~~~~~~~~
442f063c889SDaniel Borkmann
443f063c889SDaniel Borkmann``struct btf_type`` encoding requirement:
444f063c889SDaniel Borkmann  * ``name_off``: offset to a valid name associated with a variable or
445f063c889SDaniel Borkmann                  one of .data/.bss/.rodata
446f063c889SDaniel Borkmann  * ``info.kind_flag``: 0
447f063c889SDaniel Borkmann  * ``info.kind``: BTF_KIND_DATASEC
448f063c889SDaniel Borkmann  * ``info.vlen``: # of variables
449f063c889SDaniel Borkmann  * ``size``: total section size in bytes (0 at compilation time, patched
450f063c889SDaniel Borkmann              to actual size by BPF loaders such as libbpf)
451f063c889SDaniel Borkmann
452f063c889SDaniel Borkmann``btf_type`` is followed by ``info.vlen`` number of ``struct btf_var_secinfo``.::
453f063c889SDaniel Borkmann
454f063c889SDaniel Borkmann    struct btf_var_secinfo {
455f063c889SDaniel Borkmann        __u32   type;
456f063c889SDaniel Borkmann        __u32   offset;
457f063c889SDaniel Borkmann        __u32   size;
458f063c889SDaniel Borkmann    };
459f063c889SDaniel Borkmann
460f063c889SDaniel Borkmann``struct btf_var_secinfo`` encoding:
461f063c889SDaniel Borkmann  * ``type``: the type of the BTF_KIND_VAR variable
462f063c889SDaniel Borkmann  * ``offset``: the in-section offset of the variable
463f063c889SDaniel Borkmann  * ``size``: the size of the variable in bytes
464f063c889SDaniel Borkmann
4656be6a0baSIlya Leoshkevich2.2.16 BTF_KIND_FLOAT
4666be6a0baSIlya Leoshkevich~~~~~~~~~~~~~~~~~~~~~
4676be6a0baSIlya Leoshkevich
4686be6a0baSIlya Leoshkevich``struct btf_type`` encoding requirement:
4696be6a0baSIlya Leoshkevich * ``name_off``: any valid offset
4706be6a0baSIlya Leoshkevich * ``info.kind_flag``: 0
4716be6a0baSIlya Leoshkevich * ``info.kind``: BTF_KIND_FLOAT
4726be6a0baSIlya Leoshkevich * ``info.vlen``: 0
4736be6a0baSIlya Leoshkevich * ``size``: the size of the float type in bytes: 2, 4, 8, 12 or 16.
4746be6a0baSIlya Leoshkevich
4756be6a0baSIlya LeoshkevichNo additional type data follow ``btf_type``.
4766be6a0baSIlya Leoshkevich
477223f903eSYonghong Song2.2.17 BTF_KIND_DECL_TAG
478223f903eSYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~
47948f5a6c4SYonghong Song
48048f5a6c4SYonghong Song``struct btf_type`` encoding requirement:
48148f5a6c4SYonghong Song * ``name_off``: offset to a non-empty string
48248f5a6c4SYonghong Song * ``info.kind_flag``: 0
483223f903eSYonghong Song * ``info.kind``: BTF_KIND_DECL_TAG
48448f5a6c4SYonghong Song * ``info.vlen``: 0
4855a867134SYonghong Song * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef``
48648f5a6c4SYonghong Song
487223f903eSYonghong Song``btf_type`` is followed by ``struct btf_decl_tag``.::
48848f5a6c4SYonghong Song
489223f903eSYonghong Song    struct btf_decl_tag {
49048f5a6c4SYonghong Song        __u32   component_idx;
49148f5a6c4SYonghong Song    };
49248f5a6c4SYonghong Song
493223f903eSYonghong SongThe ``name_off`` encodes btf_decl_tag attribute string.
4945a867134SYonghong SongThe ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``.
4955a867134SYonghong SongFor ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``.
496223f903eSYonghong SongFor the other three types, if the btf_decl_tag attribute is
49748f5a6c4SYonghong Songapplied to the ``struct``, ``union`` or ``func`` itself,
498223f903eSYonghong Song``btf_decl_tag.component_idx`` must be ``-1``. Otherwise,
49948f5a6c4SYonghong Songthe attribute is applied to a ``struct``/``union`` member or
500223f903eSYonghong Songa ``func`` argument, and ``btf_decl_tag.component_idx`` should be a
50148f5a6c4SYonghong Songvalid index (starting from 0) pointing to a member or an argument.
50248f5a6c4SYonghong Song
50361dbd598SYonghong Song2.2.18 BTF_KIND_TYPE_TAG
504d52f5c63SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~
505d52f5c63SYonghong Song
506d52f5c63SYonghong Song``struct btf_type`` encoding requirement:
507d52f5c63SYonghong Song * ``name_off``: offset to a non-empty string
508d52f5c63SYonghong Song * ``info.kind_flag``: 0
509d52f5c63SYonghong Song * ``info.kind``: BTF_KIND_TYPE_TAG
510d52f5c63SYonghong Song * ``info.vlen``: 0
511d52f5c63SYonghong Song * ``type``: the type with ``btf_type_tag`` attribute
512d52f5c63SYonghong Song
513b7290384SYonghong SongCurrently, ``BTF_KIND_TYPE_TAG`` is only emitted for pointer types.
514b7290384SYonghong SongIt has the following btf type chain:
515b7290384SYonghong Song::
516b7290384SYonghong Song
517b7290384SYonghong Song  ptr -> [type_tag]*
518b7290384SYonghong Song      -> [const | volatile | restrict | typedef]*
519b7290384SYonghong Song      -> base_type
520b7290384SYonghong Song
521b7290384SYonghong SongBasically, a pointer type points to zero or more
522b7290384SYonghong Songtype_tag, then zero or more const/volatile/restrict/typedef
523b7290384SYonghong Songand finally the base type. The base type is one of
524b7290384SYonghong Songint, ptr, array, struct, union, enum, func_proto and float types.
525b7290384SYonghong Song
52661dbd598SYonghong Song2.2.19 BTF_KIND_ENUM64
52761dbd598SYonghong Song~~~~~~~~~~~~~~~~~~~~~~
52861dbd598SYonghong Song
52961dbd598SYonghong Song``struct btf_type`` encoding requirement:
53061dbd598SYonghong Song  * ``name_off``: 0 or offset to a valid C identifier
53161dbd598SYonghong Song  * ``info.kind_flag``: 0 for unsigned, 1 for signed
53261dbd598SYonghong Song  * ``info.kind``: BTF_KIND_ENUM64
53361dbd598SYonghong Song  * ``info.vlen``: number of enum values
53461dbd598SYonghong Song  * ``size``: 1/2/4/8
53561dbd598SYonghong Song
53661dbd598SYonghong Song``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
53761dbd598SYonghong Song
53861dbd598SYonghong Song    struct btf_enum64 {
53961dbd598SYonghong Song        __u32   name_off;
54061dbd598SYonghong Song        __u32   val_lo32;
54161dbd598SYonghong Song        __u32   val_hi32;
54261dbd598SYonghong Song    };
54361dbd598SYonghong Song
54461dbd598SYonghong SongThe ``btf_enum64`` encoding:
54561dbd598SYonghong Song  * ``name_off``: offset to a valid C identifier
54661dbd598SYonghong Song  * ``val_lo32``: lower 32-bit value for a 64-bit value
54761dbd598SYonghong Song  * ``val_hi32``: high 32-bit value for a 64-bit value
54861dbd598SYonghong Song
54961dbd598SYonghong SongIf the original enum value is signed and the size is less than 8,
55061dbd598SYonghong Songthat value will be sign extended into 8 bytes.
55161dbd598SYonghong Song
552ffcf7ce9SYonghong Song3. BTF Kernel API
5533ff36bffSDave Tucker=================
554ffcf7ce9SYonghong Song
555ffcf7ce9SYonghong SongThe following bpf syscall command involves BTF:
556ffcf7ce9SYonghong Song   * BPF_BTF_LOAD: load a blob of BTF data into kernel
557ffcf7ce9SYonghong Song   * BPF_MAP_CREATE: map creation with btf key and value type info.
558ffcf7ce9SYonghong Song   * BPF_PROG_LOAD: prog load with btf function and line info.
559ffcf7ce9SYonghong Song   * BPF_BTF_GET_FD_BY_ID: get a btf fd
560ffcf7ce9SYonghong Song   * BPF_OBJ_GET_INFO_BY_FD: btf, func_info, line_info
561ffcf7ce9SYonghong Song     and other btf related info are returned.
562ffcf7ce9SYonghong Song
563ffcf7ce9SYonghong SongThe workflow typically looks like:
564ffcf7ce9SYonghong Song::
565ffcf7ce9SYonghong Song
566ffcf7ce9SYonghong Song  Application:
567ffcf7ce9SYonghong Song      BPF_BTF_LOAD
568ffcf7ce9SYonghong Song          |
569ffcf7ce9SYonghong Song          v
570ffcf7ce9SYonghong Song      BPF_MAP_CREATE and BPF_PROG_LOAD
571ffcf7ce9SYonghong Song          |
572ffcf7ce9SYonghong Song          V
573ffcf7ce9SYonghong Song      ......
574ffcf7ce9SYonghong Song
575ffcf7ce9SYonghong Song  Introspection tool:
576ffcf7ce9SYonghong Song      ......
577ffcf7ce9SYonghong Song      BPF_{PROG,MAP}_GET_NEXT_ID (get prog/map id's)
578ffcf7ce9SYonghong Song          |
579ffcf7ce9SYonghong Song          V
580ffcf7ce9SYonghong Song      BPF_{PROG,MAP}_GET_FD_BY_ID (get a prog/map fd)
581ffcf7ce9SYonghong Song          |
582ffcf7ce9SYonghong Song          V
583ffcf7ce9SYonghong Song      BPF_OBJ_GET_INFO_BY_FD (get bpf_prog_info/bpf_map_info with btf_id)
584ffcf7ce9SYonghong Song          |                                     |
585ffcf7ce9SYonghong Song          V                                     |
586ffcf7ce9SYonghong Song      BPF_BTF_GET_FD_BY_ID (get btf_fd)         |
587ffcf7ce9SYonghong Song          |                                     |
588ffcf7ce9SYonghong Song          V                                     |
589ffcf7ce9SYonghong Song      BPF_OBJ_GET_INFO_BY_FD (get btf)          |
590ffcf7ce9SYonghong Song          |                                     |
591ffcf7ce9SYonghong Song          V                                     V
592ffcf7ce9SYonghong Song      pretty print types, dump func signatures and line info, etc.
593ffcf7ce9SYonghong Song
594ffcf7ce9SYonghong Song
595ffcf7ce9SYonghong Song3.1 BPF_BTF_LOAD
5963ff36bffSDave Tucker----------------
597ffcf7ce9SYonghong Song
5989ab5305dSAndrii NakryikoLoad a blob of BTF data into kernel. A blob of data, described in
5999ab5305dSAndrii Nakryiko:ref:`BTF_Type_String`, can be directly loaded into the kernel. A ``btf_fd``
6009ab5305dSAndrii Nakryikois returned to a userspace.
601ffcf7ce9SYonghong Song
602ffcf7ce9SYonghong Song3.2 BPF_MAP_CREATE
6033ff36bffSDave Tucker------------------
604ffcf7ce9SYonghong Song
605ffcf7ce9SYonghong SongA map can be created with ``btf_fd`` and specified key/value type id.::
606ffcf7ce9SYonghong Song
607ffcf7ce9SYonghong Song    __u32   btf_fd;         /* fd pointing to a BTF type data */
608ffcf7ce9SYonghong Song    __u32   btf_key_type_id;        /* BTF type_id of the key */
609ffcf7ce9SYonghong Song    __u32   btf_value_type_id;      /* BTF type_id of the value */
610ffcf7ce9SYonghong Song
611ffcf7ce9SYonghong SongIn libbpf, the map can be defined with extra annotation like below:
612ffcf7ce9SYonghong Song::
613ffcf7ce9SYonghong Song
61496c85308SAndrii Nakryiko    struct {
61596c85308SAndrii Nakryiko        __uint(type, BPF_MAP_TYPE_ARRAY);
61696c85308SAndrii Nakryiko        __type(key, int);
61796c85308SAndrii Nakryiko        __type(value, struct ipv_counts);
61896c85308SAndrii Nakryiko        __uint(max_entries, 4);
61996c85308SAndrii Nakryiko    } btf_map SEC(".maps");
620ffcf7ce9SYonghong Song
62196c85308SAndrii NakryikoDuring ELF parsing, libbpf is able to extract key/value type_id's and assign
62296c85308SAndrii Nakryikothem to BPF_MAP_CREATE attributes automatically.
623ffcf7ce9SYonghong Song
624ffcf7ce9SYonghong Song.. _BPF_Prog_Load:
625ffcf7ce9SYonghong Song
626ffcf7ce9SYonghong Song3.3 BPF_PROG_LOAD
6273ff36bffSDave Tucker-----------------
628ffcf7ce9SYonghong Song
6299ab5305dSAndrii NakryikoDuring prog_load, func_info and line_info can be passed to kernel with proper
6309ab5305dSAndrii Nakryikovalues for the following attributes:
631ffcf7ce9SYonghong Song::
632ffcf7ce9SYonghong Song
633ffcf7ce9SYonghong Song    __u32           insn_cnt;
634ffcf7ce9SYonghong Song    __aligned_u64   insns;
635ffcf7ce9SYonghong Song    ......
636ffcf7ce9SYonghong Song    __u32           prog_btf_fd;    /* fd pointing to BTF type data */
637ffcf7ce9SYonghong Song    __u32           func_info_rec_size;     /* userspace bpf_func_info size */
638ffcf7ce9SYonghong Song    __aligned_u64   func_info;      /* func info */
639ffcf7ce9SYonghong Song    __u32           func_info_cnt;  /* number of bpf_func_info records */
640ffcf7ce9SYonghong Song    __u32           line_info_rec_size;     /* userspace bpf_line_info size */
641ffcf7ce9SYonghong Song    __aligned_u64   line_info;      /* line info */
642ffcf7ce9SYonghong Song    __u32           line_info_cnt;  /* number of bpf_line_info records */
643ffcf7ce9SYonghong Song
644ffcf7ce9SYonghong SongThe func_info and line_info are an array of below, respectively.::
645ffcf7ce9SYonghong Song
646ffcf7ce9SYonghong Song    struct bpf_func_info {
647ffcf7ce9SYonghong Song        __u32   insn_off; /* [0, insn_cnt - 1] */
648ffcf7ce9SYonghong Song        __u32   type_id;  /* pointing to a BTF_KIND_FUNC type */
649ffcf7ce9SYonghong Song    };
650ffcf7ce9SYonghong Song    struct bpf_line_info {
651ffcf7ce9SYonghong Song        __u32   insn_off; /* [0, insn_cnt - 1] */
652ffcf7ce9SYonghong Song        __u32   file_name_off; /* offset to string table for the filename */
653ffcf7ce9SYonghong Song        __u32   line_off; /* offset to string table for the source line */
654ffcf7ce9SYonghong Song        __u32   line_col; /* line number and column number */
655ffcf7ce9SYonghong Song    };
656ffcf7ce9SYonghong Song
6579ab5305dSAndrii Nakryikofunc_info_rec_size is the size of each func_info record, and
6589ab5305dSAndrii Nakryikoline_info_rec_size is the size of each line_info record. Passing the record
6599ab5305dSAndrii Nakryikosize to kernel make it possible to extend the record itself in the future.
660ffcf7ce9SYonghong Song
661ffcf7ce9SYonghong SongBelow are requirements for func_info:
662ffcf7ce9SYonghong Song  * func_info[0].insn_off must be 0.
663ffcf7ce9SYonghong Song  * the func_info insn_off is in strictly increasing order and matches
664ffcf7ce9SYonghong Song    bpf func boundaries.
665ffcf7ce9SYonghong Song
666ffcf7ce9SYonghong SongBelow are requirements for line_info:
6675efc529fSAndrii Nakryiko  * the first insn in each func must have a line_info record pointing to it.
668ffcf7ce9SYonghong Song  * the line_info insn_off is in strictly increasing order.
669ffcf7ce9SYonghong Song
670ffcf7ce9SYonghong SongFor line_info, the line number and column number are defined as below:
671ffcf7ce9SYonghong Song::
672ffcf7ce9SYonghong Song
673ffcf7ce9SYonghong Song    #define BPF_LINE_INFO_LINE_NUM(line_col)        ((line_col) >> 10)
674ffcf7ce9SYonghong Song    #define BPF_LINE_INFO_LINE_COL(line_col)        ((line_col) & 0x3ff)
675ffcf7ce9SYonghong Song
676ffcf7ce9SYonghong Song3.4 BPF_{PROG,MAP}_GET_NEXT_ID
6773ff36bffSDave Tucker------------------------------
678ffcf7ce9SYonghong Song
6799ab5305dSAndrii NakryikoIn kernel, every loaded program, map or btf has a unique id. The id won't
6809ab5305dSAndrii Nakryikochange during the lifetime of a program, map, or btf.
681ffcf7ce9SYonghong Song
6829ab5305dSAndrii NakryikoThe bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID returns all id's, one for
6839ab5305dSAndrii Nakryikoeach command, to user space, for bpf program or maps, respectively, so an
6849ab5305dSAndrii Nakryikoinspection tool can inspect all programs and maps.
685ffcf7ce9SYonghong Song
686ffcf7ce9SYonghong Song3.5 BPF_{PROG,MAP}_GET_FD_BY_ID
6873ff36bffSDave Tucker-------------------------------
688ffcf7ce9SYonghong Song
6895efc529fSAndrii NakryikoAn introspection tool cannot use id to get details about program or maps.
6905efc529fSAndrii NakryikoA file descriptor needs to be obtained first for reference-counting purpose.
691ffcf7ce9SYonghong Song
692ffcf7ce9SYonghong Song3.6 BPF_OBJ_GET_INFO_BY_FD
6933ff36bffSDave Tucker--------------------------
694ffcf7ce9SYonghong Song
6959ab5305dSAndrii NakryikoOnce a program/map fd is acquired, an introspection tool can get the detailed
6969ab5305dSAndrii Nakryikoinformation from kernel about this fd, some of which are BTF-related. For
6979ab5305dSAndrii Nakryikoexample, ``bpf_map_info`` returns ``btf_id`` and key/value type ids.
6989ab5305dSAndrii Nakryiko``bpf_prog_info`` returns ``btf_id``, func_info, and line info for translated
6999ab5305dSAndrii Nakryikobpf byte codes, and jited_line_info.
700ffcf7ce9SYonghong Song
701ffcf7ce9SYonghong Song3.7 BPF_BTF_GET_FD_BY_ID
7023ff36bffSDave Tucker------------------------
703ffcf7ce9SYonghong Song
7049ab5305dSAndrii NakryikoWith ``btf_id`` obtained in ``bpf_map_info`` and ``bpf_prog_info``, bpf
7059ab5305dSAndrii Nakryikosyscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd. Then, with
7069ab5305dSAndrii Nakryikocommand BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally loaded into the
7079ab5305dSAndrii Nakryikokernel with BPF_BTF_LOAD, can be retrieved.
708ffcf7ce9SYonghong Song
7095efc529fSAndrii NakryikoWith the btf blob, ``bpf_map_info``, and ``bpf_prog_info``, an introspection
7109ab5305dSAndrii Nakryikotool has full btf knowledge and is able to pretty print map key/values, dump
7119ab5305dSAndrii Nakryikofunc signatures and line info, along with byte/jit codes.
712ffcf7ce9SYonghong Song
713ffcf7ce9SYonghong Song4. ELF File Format Interface
7143ff36bffSDave Tucker============================
715ffcf7ce9SYonghong Song
716ffcf7ce9SYonghong Song4.1 .BTF section
7173ff36bffSDave Tucker----------------
718ffcf7ce9SYonghong Song
7199ab5305dSAndrii NakryikoThe .BTF section contains type and string data. The format of this section is
7209ab5305dSAndrii Nakryikosame as the one describe in :ref:`BTF_Type_String`.
721ffcf7ce9SYonghong Song
722ffcf7ce9SYonghong Song.. _BTF_Ext_Section:
723ffcf7ce9SYonghong Song
724ffcf7ce9SYonghong Song4.2 .BTF.ext section
7253ff36bffSDave Tucker--------------------
726ffcf7ce9SYonghong Song
727be4033d3SEduard ZingermanThe .BTF.ext section encodes func_info, line_info and CO-RE relocations
728be4033d3SEduard Zingermanwhich needs loader manipulation before loading into the kernel.
729ffcf7ce9SYonghong Song
7309ab5305dSAndrii NakryikoThe specification for .BTF.ext section is defined at ``tools/lib/bpf/btf.h``
7319ab5305dSAndrii Nakryikoand ``tools/lib/bpf/btf.c``.
732ffcf7ce9SYonghong Song
733ffcf7ce9SYonghong SongThe current header of .BTF.ext section::
734ffcf7ce9SYonghong Song
735ffcf7ce9SYonghong Song    struct btf_ext_header {
736ffcf7ce9SYonghong Song        __u16   magic;
737ffcf7ce9SYonghong Song        __u8    version;
738ffcf7ce9SYonghong Song        __u8    flags;
739ffcf7ce9SYonghong Song        __u32   hdr_len;
740ffcf7ce9SYonghong Song
741ffcf7ce9SYonghong Song        /* All offsets are in bytes relative to the end of this header */
742ffcf7ce9SYonghong Song        __u32   func_info_off;
743ffcf7ce9SYonghong Song        __u32   func_info_len;
744ffcf7ce9SYonghong Song        __u32   line_info_off;
745ffcf7ce9SYonghong Song        __u32   line_info_len;
746be4033d3SEduard Zingerman
747be4033d3SEduard Zingerman        /* optional part of .BTF.ext header */
748be4033d3SEduard Zingerman        __u32   core_relo_off;
749be4033d3SEduard Zingerman        __u32   core_relo_len;
750ffcf7ce9SYonghong Song    };
751ffcf7ce9SYonghong Song
7529ab5305dSAndrii NakryikoIt is very similar to .BTF section. Instead of type/string section, it
753be4033d3SEduard Zingermancontains func_info, line_info and core_relo sub-sections.
754be4033d3SEduard ZingermanSee :ref:`BPF_Prog_Load` for details about func_info and line_info
755be4033d3SEduard Zingermanrecord format.
756ffcf7ce9SYonghong Song
757ffcf7ce9SYonghong SongThe func_info is organized as below.::
758ffcf7ce9SYonghong Song
759be4033d3SEduard Zingerman     func_info_rec_size              /* __u32 value */
760ffcf7ce9SYonghong Song     btf_ext_info_sec for section #1 /* func_info for section #1 */
761ffcf7ce9SYonghong Song     btf_ext_info_sec for section #2 /* func_info for section #2 */
762ffcf7ce9SYonghong Song     ...
763ffcf7ce9SYonghong Song
7649ab5305dSAndrii Nakryiko``func_info_rec_size`` specifies the size of ``bpf_func_info`` structure when
7659ab5305dSAndrii Nakryiko.BTF.ext is generated. ``btf_ext_info_sec``, defined below, is a collection of
7669ab5305dSAndrii Nakryikofunc_info for each specific ELF section.::
767ffcf7ce9SYonghong Song
768ffcf7ce9SYonghong Song     struct btf_ext_info_sec {
769ffcf7ce9SYonghong Song        __u32   sec_name_off; /* offset to section name */
770ffcf7ce9SYonghong Song        __u32   num_info;
771ffcf7ce9SYonghong Song        /* Followed by num_info * record_size number of bytes */
772ffcf7ce9SYonghong Song        __u8    data[0];
773ffcf7ce9SYonghong Song     };
774ffcf7ce9SYonghong Song
775ffcf7ce9SYonghong SongHere, num_info must be greater than 0.
776ffcf7ce9SYonghong Song
777ffcf7ce9SYonghong SongThe line_info is organized as below.::
778ffcf7ce9SYonghong Song
779be4033d3SEduard Zingerman     line_info_rec_size              /* __u32 value */
780ffcf7ce9SYonghong Song     btf_ext_info_sec for section #1 /* line_info for section #1 */
781ffcf7ce9SYonghong Song     btf_ext_info_sec for section #2 /* line_info for section #2 */
782ffcf7ce9SYonghong Song     ...
783ffcf7ce9SYonghong Song
7849ab5305dSAndrii Nakryiko``line_info_rec_size`` specifies the size of ``bpf_line_info`` structure when
7859ab5305dSAndrii Nakryiko.BTF.ext is generated.
786ffcf7ce9SYonghong Song
787ffcf7ce9SYonghong SongThe interpretation of ``bpf_func_info->insn_off`` and
7889ab5305dSAndrii Nakryiko``bpf_line_info->insn_off`` is different between kernel API and ELF API. For
7899ab5305dSAndrii Nakryikokernel API, the ``insn_off`` is the instruction offset in the unit of ``struct
7909ab5305dSAndrii Nakryikobpf_insn``. For ELF API, the ``insn_off`` is the byte offset from the
7919ab5305dSAndrii Nakryikobeginning of section (``btf_ext_info_sec->sec_name_off``).
792ffcf7ce9SYonghong Song
793be4033d3SEduard ZingermanThe core_relo is organized as below.::
794be4033d3SEduard Zingerman
795be4033d3SEduard Zingerman     core_relo_rec_size              /* __u32 value */
796be4033d3SEduard Zingerman     btf_ext_info_sec for section #1 /* core_relo for section #1 */
797be4033d3SEduard Zingerman     btf_ext_info_sec for section #2 /* core_relo for section #2 */
798be4033d3SEduard Zingerman
799be4033d3SEduard Zingerman``core_relo_rec_size`` specifies the size of ``bpf_core_relo``
800be4033d3SEduard Zingermanstructure when .BTF.ext is generated. All ``bpf_core_relo`` structures
801be4033d3SEduard Zingermanwithin a single ``btf_ext_info_sec`` describe relocations applied to
802be4033d3SEduard Zingermansection named by ``btf_ext_info_sec->sec_name_off``.
803be4033d3SEduard Zingerman
8043888fa13SEduard ZingermanSee :ref:`Documentation/bpf/llvm_reloc.rst <btf-co-re-relocations>`
805be4033d3SEduard Zingermanfor more information on CO-RE relocations.
806be4033d3SEduard Zingerman
807232ce4beSJiri Olsa4.2 .BTF_ids section
8083ff36bffSDave Tucker--------------------
809232ce4beSJiri Olsa
810232ce4beSJiri OlsaThe .BTF_ids section encodes BTF ID values that are used within the kernel.
811232ce4beSJiri Olsa
812232ce4beSJiri OlsaThis section is created during the kernel compilation with the help of
813232ce4beSJiri Olsamacros defined in ``include/linux/btf_ids.h`` header file. Kernel code can
814232ce4beSJiri Olsause them to create lists and sets (sorted lists) of BTF ID values.
815232ce4beSJiri Olsa
816232ce4beSJiri OlsaThe ``BTF_ID_LIST`` and ``BTF_ID`` macros define unsorted list of BTF ID values,
817232ce4beSJiri Olsawith following syntax::
818232ce4beSJiri Olsa
819232ce4beSJiri Olsa  BTF_ID_LIST(list)
820232ce4beSJiri Olsa  BTF_ID(type1, name1)
821232ce4beSJiri Olsa  BTF_ID(type2, name2)
822232ce4beSJiri Olsa
823232ce4beSJiri Olsaresulting in following layout in .BTF_ids section::
824232ce4beSJiri Olsa
825232ce4beSJiri Olsa  __BTF_ID__type1__name1__1:
826232ce4beSJiri Olsa  .zero 4
827232ce4beSJiri Olsa  __BTF_ID__type2__name2__2:
828232ce4beSJiri Olsa  .zero 4
829232ce4beSJiri Olsa
830232ce4beSJiri OlsaThe ``u32 list[];`` variable is defined to access the list.
831232ce4beSJiri Olsa
832232ce4beSJiri OlsaThe ``BTF_ID_UNUSED`` macro defines 4 zero bytes. It's used when we
833232ce4beSJiri Olsawant to define unused entry in BTF_ID_LIST, like::
834232ce4beSJiri Olsa
835232ce4beSJiri Olsa      BTF_ID_LIST(bpf_skb_output_btf_ids)
836232ce4beSJiri Olsa      BTF_ID(struct, sk_buff)
837232ce4beSJiri Olsa      BTF_ID_UNUSED
838232ce4beSJiri Olsa      BTF_ID(struct, task_struct)
839232ce4beSJiri Olsa
84068a26bc7SJiri OlsaThe ``BTF_SET_START/END`` macros pair defines sorted list of BTF ID values
84168a26bc7SJiri Olsaand their count, with following syntax::
84268a26bc7SJiri Olsa
84368a26bc7SJiri Olsa  BTF_SET_START(set)
84468a26bc7SJiri Olsa  BTF_ID(type1, name1)
84568a26bc7SJiri Olsa  BTF_ID(type2, name2)
84668a26bc7SJiri Olsa  BTF_SET_END(set)
84768a26bc7SJiri Olsa
84868a26bc7SJiri Olsaresulting in following layout in .BTF_ids section::
84968a26bc7SJiri Olsa
85068a26bc7SJiri Olsa  __BTF_ID__set__set:
85168a26bc7SJiri Olsa  .zero 4
85268a26bc7SJiri Olsa  __BTF_ID__type1__name1__3:
85368a26bc7SJiri Olsa  .zero 4
85468a26bc7SJiri Olsa  __BTF_ID__type2__name2__4:
85568a26bc7SJiri Olsa  .zero 4
85668a26bc7SJiri Olsa
85768a26bc7SJiri OlsaThe ``struct btf_id_set set;`` variable is defined to access the list.
85868a26bc7SJiri Olsa
85968a26bc7SJiri OlsaThe ``typeX`` name can be one of following::
86068a26bc7SJiri Olsa
86168a26bc7SJiri Olsa   struct, union, typedef, func
86268a26bc7SJiri Olsa
86368a26bc7SJiri Olsaand is used as a filter when resolving the BTF ID value.
86468a26bc7SJiri Olsa
865232ce4beSJiri OlsaAll the BTF ID lists and sets are compiled in the .BTF_ids section and
866232ce4beSJiri Olsaresolved during the linking phase of kernel build by ``resolve_btfids`` tool.
867232ce4beSJiri Olsa
868ffcf7ce9SYonghong Song5. Using BTF
8693ff36bffSDave Tucker============
870ffcf7ce9SYonghong Song
871ffcf7ce9SYonghong Song5.1 bpftool map pretty print
8723ff36bffSDave Tucker----------------------------
873ffcf7ce9SYonghong Song
8749ab5305dSAndrii NakryikoWith BTF, the map key/value can be printed based on fields rather than simply
8759ab5305dSAndrii Nakryikoraw bytes. This is especially valuable for large structure or if your data
8769ab5305dSAndrii Nakryikostructure has bitfields. For example, for the following map,::
877ffcf7ce9SYonghong Song
878ffcf7ce9SYonghong Song      enum A { A1, A2, A3, A4, A5 };
879ffcf7ce9SYonghong Song      typedef enum A ___A;
880ffcf7ce9SYonghong Song      struct tmp_t {
881ffcf7ce9SYonghong Song           char a1:4;
882ffcf7ce9SYonghong Song           int  a2:4;
883ffcf7ce9SYonghong Song           int  :4;
884ffcf7ce9SYonghong Song           __u32 a3:4;
885ffcf7ce9SYonghong Song           int b;
886ffcf7ce9SYonghong Song           ___A b1:4;
887ffcf7ce9SYonghong Song           enum A b2:4;
888ffcf7ce9SYonghong Song      };
88996c85308SAndrii Nakryiko      struct {
89096c85308SAndrii Nakryiko           __uint(type, BPF_MAP_TYPE_ARRAY);
89196c85308SAndrii Nakryiko           __type(key, int);
89296c85308SAndrii Nakryiko           __type(value, struct tmp_t);
89396c85308SAndrii Nakryiko           __uint(max_entries, 1);
89496c85308SAndrii Nakryiko      } tmpmap SEC(".maps");
895ffcf7ce9SYonghong Song
896ffcf7ce9SYonghong Songbpftool is able to pretty print like below:
897ffcf7ce9SYonghong Song::
898ffcf7ce9SYonghong Song
899ffcf7ce9SYonghong Song      [{
900ffcf7ce9SYonghong Song            "key": 0,
901ffcf7ce9SYonghong Song            "value": {
902ffcf7ce9SYonghong Song                "a1": 0x2,
903ffcf7ce9SYonghong Song                "a2": 0x4,
904ffcf7ce9SYonghong Song                "a3": 0x6,
905ffcf7ce9SYonghong Song                "b": 7,
906ffcf7ce9SYonghong Song                "b1": 0x8,
907ffcf7ce9SYonghong Song                "b2": 0xa
908ffcf7ce9SYonghong Song            }
909ffcf7ce9SYonghong Song        }
910ffcf7ce9SYonghong Song      ]
911ffcf7ce9SYonghong Song
912ffcf7ce9SYonghong Song5.2 bpftool prog dump
9133ff36bffSDave Tucker---------------------
914ffcf7ce9SYonghong Song
9159ab5305dSAndrii NakryikoThe following is an example showing how func_info and line_info can help prog
9169ab5305dSAndrii Nakryikodump with better kernel symbol names, function prototypes and line
9179ab5305dSAndrii Nakryikoinformation.::
918ffcf7ce9SYonghong Song
919ffcf7ce9SYonghong Song    $ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
920ffcf7ce9SYonghong Song    [...]
921ffcf7ce9SYonghong Song    int test_long_fname_2(struct dummy_tracepoint_args * arg):
922ffcf7ce9SYonghong Song    bpf_prog_44a040bf25481309_test_long_fname_2:
923ffcf7ce9SYonghong Song    ; static int test_long_fname_2(struct dummy_tracepoint_args *arg)
924ffcf7ce9SYonghong Song       0:   push   %rbp
925ffcf7ce9SYonghong Song       1:   mov    %rsp,%rbp
926ffcf7ce9SYonghong Song       4:   sub    $0x30,%rsp
927ffcf7ce9SYonghong Song       b:   sub    $0x28,%rbp
928ffcf7ce9SYonghong Song       f:   mov    %rbx,0x0(%rbp)
929ffcf7ce9SYonghong Song      13:   mov    %r13,0x8(%rbp)
930ffcf7ce9SYonghong Song      17:   mov    %r14,0x10(%rbp)
931ffcf7ce9SYonghong Song      1b:   mov    %r15,0x18(%rbp)
932ffcf7ce9SYonghong Song      1f:   xor    %eax,%eax
933ffcf7ce9SYonghong Song      21:   mov    %rax,0x20(%rbp)
934ffcf7ce9SYonghong Song      25:   xor    %esi,%esi
935ffcf7ce9SYonghong Song    ; int key = 0;
936ffcf7ce9SYonghong Song      27:   mov    %esi,-0x4(%rbp)
937ffcf7ce9SYonghong Song    ; if (!arg->sock)
938ffcf7ce9SYonghong Song      2a:   mov    0x8(%rdi),%rdi
939ffcf7ce9SYonghong Song    ; if (!arg->sock)
940ffcf7ce9SYonghong Song      2e:   cmp    $0x0,%rdi
941ffcf7ce9SYonghong Song      32:   je     0x0000000000000070
942ffcf7ce9SYonghong Song      34:   mov    %rbp,%rsi
943ffcf7ce9SYonghong Song    ; counts = bpf_map_lookup_elem(&btf_map, &key);
944ffcf7ce9SYonghong Song    [...]
945ffcf7ce9SYonghong Song
9465efc529fSAndrii Nakryiko5.3 Verifier Log
9473ff36bffSDave Tucker----------------
948ffcf7ce9SYonghong Song
9499ab5305dSAndrii NakryikoThe following is an example of how line_info can help debugging verification
9509ab5305dSAndrii Nakryikofailure.::
951ffcf7ce9SYonghong Song
952ffcf7ce9SYonghong Song       /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c
953ffcf7ce9SYonghong Song        * is modified as below.
954ffcf7ce9SYonghong Song        */
955ffcf7ce9SYonghong Song       data = (void *)(long)xdp->data;
956ffcf7ce9SYonghong Song       data_end = (void *)(long)xdp->data_end;
957ffcf7ce9SYonghong Song       /*
958ffcf7ce9SYonghong Song       if (data + 4 > data_end)
959ffcf7ce9SYonghong Song               return XDP_DROP;
960ffcf7ce9SYonghong Song       */
961ffcf7ce9SYonghong Song       *(u32 *)data = dst->dst;
962ffcf7ce9SYonghong Song
963ffcf7ce9SYonghong Song    $ bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp
964ffcf7ce9SYonghong Song        ; data = (void *)(long)xdp->data;
965ffcf7ce9SYonghong Song        224: (79) r2 = *(u64 *)(r10 -112)
966ffcf7ce9SYonghong Song        225: (61) r2 = *(u32 *)(r2 +0)
967ffcf7ce9SYonghong Song        ; *(u32 *)data = dst->dst;
968ffcf7ce9SYonghong Song        226: (63) *(u32 *)(r2 +0) = r1
969ffcf7ce9SYonghong Song        invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0)
970ffcf7ce9SYonghong Song        R2 offset is outside of the packet
971ffcf7ce9SYonghong Song
972ffcf7ce9SYonghong Song6. BTF Generation
9733ff36bffSDave Tucker=================
974ffcf7ce9SYonghong Song
975ffcf7ce9SYonghong SongYou need latest pahole
976ffcf7ce9SYonghong Song
977ffcf7ce9SYonghong Song  https://git.kernel.org/pub/scm/devel/pahole/pahole.git/
978ffcf7ce9SYonghong Song
9799ab5305dSAndrii Nakryikoor llvm (8.0 or later). The pahole acts as a dwarf2btf converter. It doesn't
9809ab5305dSAndrii Nakryikosupport .BTF.ext and btf BTF_KIND_FUNC type yet. For example,::
981ffcf7ce9SYonghong Song
982ffcf7ce9SYonghong Song      -bash-4.4$ cat t.c
983ffcf7ce9SYonghong Song      struct t {
984ffcf7ce9SYonghong Song        int a:2;
985ffcf7ce9SYonghong Song        int b:3;
986ffcf7ce9SYonghong Song        int c:2;
987ffcf7ce9SYonghong Song      } g;
988ffcf7ce9SYonghong Song      -bash-4.4$ gcc -c -O2 -g t.c
989ffcf7ce9SYonghong Song      -bash-4.4$ pahole -JV t.o
990ffcf7ce9SYonghong Song      File t.o:
991ffcf7ce9SYonghong Song      [1] STRUCT t kind_flag=1 size=4 vlen=3
992ffcf7ce9SYonghong Song              a type_id=2 bitfield_size=2 bits_offset=0
993ffcf7ce9SYonghong Song              b type_id=2 bitfield_size=3 bits_offset=2
994ffcf7ce9SYonghong Song              c type_id=2 bitfield_size=2 bits_offset=5
995ffcf7ce9SYonghong Song      [2] INT int size=4 bit_offset=0 nr_bits=32 encoding=SIGNED
996ffcf7ce9SYonghong Song
9979ab5305dSAndrii NakryikoThe llvm is able to generate .BTF and .BTF.ext directly with -g for bpf target
9989ab5305dSAndrii Nakryikoonly. The assembly code (-S) is able to show the BTF encoding in assembly
9999ab5305dSAndrii Nakryikoformat.::
1000ffcf7ce9SYonghong Song
1001ffcf7ce9SYonghong Song    -bash-4.4$ cat t2.c
1002ffcf7ce9SYonghong Song    typedef int __int32;
1003ffcf7ce9SYonghong Song    struct t2 {
1004ffcf7ce9SYonghong Song      int a2;
1005ffcf7ce9SYonghong Song      int (*f2)(char q1, __int32 q2, ...);
1006ffcf7ce9SYonghong Song      int (*f3)();
1007ffcf7ce9SYonghong Song    } g2;
1008ffcf7ce9SYonghong Song    int main() { return 0; }
1009ffcf7ce9SYonghong Song    int test() { return 0; }
1010bbaf1ff0SFangrui Song    -bash-4.4$ clang -c -g -O2 --target=bpf t2.c
1011ffcf7ce9SYonghong Song    -bash-4.4$ readelf -S t2.o
1012ffcf7ce9SYonghong Song      ......
1013ffcf7ce9SYonghong Song      [ 8] .BTF              PROGBITS         0000000000000000  00000247
1014ffcf7ce9SYonghong Song           000000000000016e  0000000000000000           0     0     1
1015ffcf7ce9SYonghong Song      [ 9] .BTF.ext          PROGBITS         0000000000000000  000003b5
1016ffcf7ce9SYonghong Song           0000000000000060  0000000000000000           0     0     1
1017ffcf7ce9SYonghong Song      [10] .rel.BTF.ext      REL              0000000000000000  000007e0
1018ffcf7ce9SYonghong Song           0000000000000040  0000000000000010          16     9     8
1019ffcf7ce9SYonghong Song      ......
1020bbaf1ff0SFangrui Song    -bash-4.4$ clang -S -g -O2 --target=bpf t2.c
1021ffcf7ce9SYonghong Song    -bash-4.4$ cat t2.s
1022ffcf7ce9SYonghong Song      ......
1023ffcf7ce9SYonghong Song            .section        .BTF,"",@progbits
1024ffcf7ce9SYonghong Song            .short  60319                   # 0xeb9f
1025ffcf7ce9SYonghong Song            .byte   1
1026ffcf7ce9SYonghong Song            .byte   0
1027ffcf7ce9SYonghong Song            .long   24
1028ffcf7ce9SYonghong Song            .long   0
1029ffcf7ce9SYonghong Song            .long   220
1030ffcf7ce9SYonghong Song            .long   220
1031ffcf7ce9SYonghong Song            .long   122
1032ffcf7ce9SYonghong Song            .long   0                       # BTF_KIND_FUNC_PROTO(id = 1)
1033ffcf7ce9SYonghong Song            .long   218103808               # 0xd000000
1034ffcf7ce9SYonghong Song            .long   2
1035ffcf7ce9SYonghong Song            .long   83                      # BTF_KIND_INT(id = 2)
1036ffcf7ce9SYonghong Song            .long   16777216                # 0x1000000
1037ffcf7ce9SYonghong Song            .long   4
1038ffcf7ce9SYonghong Song            .long   16777248                # 0x1000020
1039ffcf7ce9SYonghong Song      ......
1040ffcf7ce9SYonghong Song            .byte   0                       # string offset=0
1041ffcf7ce9SYonghong Song            .ascii  ".text"                 # string offset=1
1042ffcf7ce9SYonghong Song            .byte   0
1043ffcf7ce9SYonghong Song            .ascii  "/home/yhs/tmp-pahole/t2.c" # string offset=7
1044ffcf7ce9SYonghong Song            .byte   0
1045ffcf7ce9SYonghong Song            .ascii  "int main() { return 0; }" # string offset=33
1046ffcf7ce9SYonghong Song            .byte   0
1047ffcf7ce9SYonghong Song            .ascii  "int test() { return 0; }" # string offset=58
1048ffcf7ce9SYonghong Song            .byte   0
1049ffcf7ce9SYonghong Song            .ascii  "int"                   # string offset=83
1050ffcf7ce9SYonghong Song      ......
1051ffcf7ce9SYonghong Song            .section        .BTF.ext,"",@progbits
1052ffcf7ce9SYonghong Song            .short  60319                   # 0xeb9f
1053ffcf7ce9SYonghong Song            .byte   1
1054ffcf7ce9SYonghong Song            .byte   0
1055ffcf7ce9SYonghong Song            .long   24
1056ffcf7ce9SYonghong Song            .long   0
1057ffcf7ce9SYonghong Song            .long   28
1058ffcf7ce9SYonghong Song            .long   28
1059ffcf7ce9SYonghong Song            .long   44
1060ffcf7ce9SYonghong Song            .long   8                       # FuncInfo
1061ffcf7ce9SYonghong Song            .long   1                       # FuncInfo section string offset=1
1062ffcf7ce9SYonghong Song            .long   2
1063ffcf7ce9SYonghong Song            .long   .Lfunc_begin0
1064ffcf7ce9SYonghong Song            .long   3
1065ffcf7ce9SYonghong Song            .long   .Lfunc_begin1
1066ffcf7ce9SYonghong Song            .long   5
1067ffcf7ce9SYonghong Song            .long   16                      # LineInfo
1068ffcf7ce9SYonghong Song            .long   1                       # LineInfo section string offset=1
1069ffcf7ce9SYonghong Song            .long   2
1070ffcf7ce9SYonghong Song            .long   .Ltmp0
1071ffcf7ce9SYonghong Song            .long   7
1072ffcf7ce9SYonghong Song            .long   33
1073ffcf7ce9SYonghong Song            .long   7182                    # Line 7 Col 14
1074ffcf7ce9SYonghong Song            .long   .Ltmp3
1075ffcf7ce9SYonghong Song            .long   7
1076ffcf7ce9SYonghong Song            .long   58
1077ffcf7ce9SYonghong Song            .long   8206                    # Line 8 Col 14
1078ffcf7ce9SYonghong Song
1079ffcf7ce9SYonghong Song7. Testing
10803ff36bffSDave Tucker==========
1081ffcf7ce9SYonghong Song
1082b74344cbSRong TaoThe kernel BPF selftest `tools/testing/selftests/bpf/prog_tests/btf.c`_
1083b74344cbSRong Taoprovides an extensive set of BTF-related tests.
1084b74344cbSRong Tao
1085b74344cbSRong Tao.. Links
1086b74344cbSRong Tao.. _tools/testing/selftests/bpf/prog_tests/btf.c:
1087b74344cbSRong Tao   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/btf.c
1088