xref: /openbmc/linux/Documentation/bpf/btf.rst (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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
2759ab5305dSAndrii NakryikoThe following kernel patch introduced ``kind_flag`` and explained why both
2769ab5305dSAndrii Nakryikomodes exist:
277ffcf7ce9SYonghong Song
278ffcf7ce9SYonghong Song  https://github.com/torvalds/linux/commit/9d5f9f701b1891466fb3dbb1806ad97716f95cc3#diff-fa650a64fdd3968396883d2fe8215ff3
279ffcf7ce9SYonghong Song
280ffcf7ce9SYonghong Song2.2.6 BTF_KIND_ENUM
281ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~
282ffcf7ce9SYonghong Song
283ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
284ffcf7ce9SYonghong Song  * ``name_off``: 0 or offset to a valid C identifier
28561dbd598SYonghong Song  * ``info.kind_flag``: 0 for unsigned, 1 for signed
286ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_ENUM
287ffcf7ce9SYonghong Song  * ``info.vlen``: number of enum values
28861dbd598SYonghong Song  * ``size``: 1/2/4/8
289ffcf7ce9SYonghong Song
290ffcf7ce9SYonghong Song``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum``.::
291ffcf7ce9SYonghong Song
292ffcf7ce9SYonghong Song    struct btf_enum {
293ffcf7ce9SYonghong Song        __u32   name_off;
294ffcf7ce9SYonghong Song        __s32   val;
295ffcf7ce9SYonghong Song    };
296ffcf7ce9SYonghong Song
297ffcf7ce9SYonghong SongThe ``btf_enum`` encoding:
298ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
299ffcf7ce9SYonghong Song  * ``val``: any value
300ffcf7ce9SYonghong Song
30161dbd598SYonghong SongIf the original enum value is signed and the size is less than 4,
30261dbd598SYonghong Songthat value will be sign extended into 4 bytes. If the size is 8,
30361dbd598SYonghong Songthe value will be truncated into 4 bytes.
30461dbd598SYonghong Song
305ffcf7ce9SYonghong Song2.2.7 BTF_KIND_FWD
306ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~
307ffcf7ce9SYonghong Song
308ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
309ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
310ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0 for struct, 1 for union
311ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_FWD
312ffcf7ce9SYonghong Song  * ``info.vlen``: 0
313ffcf7ce9SYonghong Song  * ``type``: 0
314ffcf7ce9SYonghong Song
315ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
316ffcf7ce9SYonghong Song
317ffcf7ce9SYonghong Song2.2.8 BTF_KIND_TYPEDEF
318ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~
319ffcf7ce9SYonghong Song
320ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
321ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
322ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
323ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_TYPEDEF
324ffcf7ce9SYonghong Song  * ``info.vlen``: 0
325ffcf7ce9SYonghong Song  * ``type``: the type which can be referred by name at ``name_off``
326ffcf7ce9SYonghong Song
327ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
328ffcf7ce9SYonghong Song
329ffcf7ce9SYonghong Song2.2.9 BTF_KIND_VOLATILE
330ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~
331ffcf7ce9SYonghong Song
332ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
333ffcf7ce9SYonghong Song  * ``name_off``: 0
334ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
335ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_VOLATILE
336ffcf7ce9SYonghong Song  * ``info.vlen``: 0
337ffcf7ce9SYonghong Song  * ``type``: the type with ``volatile`` qualifier
338ffcf7ce9SYonghong Song
339ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
340ffcf7ce9SYonghong Song
341ffcf7ce9SYonghong Song2.2.10 BTF_KIND_CONST
342ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~
343ffcf7ce9SYonghong Song
344ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
345ffcf7ce9SYonghong Song  * ``name_off``: 0
346ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
347ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_CONST
348ffcf7ce9SYonghong Song  * ``info.vlen``: 0
349ffcf7ce9SYonghong Song  * ``type``: the type with ``const`` qualifier
350ffcf7ce9SYonghong Song
351ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
352ffcf7ce9SYonghong Song
353ffcf7ce9SYonghong Song2.2.11 BTF_KIND_RESTRICT
354ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~
355ffcf7ce9SYonghong Song
356ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
357ffcf7ce9SYonghong Song  * ``name_off``: 0
358ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
359ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_RESTRICT
360ffcf7ce9SYonghong Song  * ``info.vlen``: 0
361ffcf7ce9SYonghong Song  * ``type``: the type with ``restrict`` qualifier
362ffcf7ce9SYonghong Song
363ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
364ffcf7ce9SYonghong Song
365ffcf7ce9SYonghong Song2.2.12 BTF_KIND_FUNC
366ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~
367ffcf7ce9SYonghong Song
368ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
369ffcf7ce9SYonghong Song  * ``name_off``: offset to a valid C identifier
370ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
371ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_FUNC
372e5e23424SIndu Bhagat  * ``info.vlen``: linkage information (BTF_FUNC_STATIC, BTF_FUNC_GLOBAL
373e5e23424SIndu Bhagat                   or BTF_FUNC_EXTERN)
374ffcf7ce9SYonghong Song  * ``type``: a BTF_KIND_FUNC_PROTO type
375ffcf7ce9SYonghong Song
376ffcf7ce9SYonghong SongNo additional type data follow ``btf_type``.
377ffcf7ce9SYonghong Song
3785efc529fSAndrii NakryikoA BTF_KIND_FUNC defines not a type, but a subprogram (function) whose
3799ab5305dSAndrii Nakryikosignature is defined by ``type``. The subprogram is thus an instance of that
3809ab5305dSAndrii Nakryikotype. The BTF_KIND_FUNC may in turn be referenced by a func_info in the
3819ab5305dSAndrii Nakryiko:ref:`BTF_Ext_Section` (ELF) or in the arguments to :ref:`BPF_Prog_Load`
3829ab5305dSAndrii Nakryiko(ABI).
383ffcf7ce9SYonghong Song
384e5e23424SIndu BhagatCurrently, only linkage values of BTF_FUNC_STATIC and BTF_FUNC_GLOBAL are
385e5e23424SIndu Bhagatsupported in the kernel.
386e5e23424SIndu Bhagat
387ffcf7ce9SYonghong Song2.2.13 BTF_KIND_FUNC_PROTO
388ffcf7ce9SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~~~
389ffcf7ce9SYonghong Song
390ffcf7ce9SYonghong Song``struct btf_type`` encoding requirement:
391ffcf7ce9SYonghong Song  * ``name_off``: 0
392ffcf7ce9SYonghong Song  * ``info.kind_flag``: 0
393ffcf7ce9SYonghong Song  * ``info.kind``: BTF_KIND_FUNC_PROTO
394ffcf7ce9SYonghong Song  * ``info.vlen``: # of parameters
395ffcf7ce9SYonghong Song  * ``type``: the return type
396ffcf7ce9SYonghong Song
397ffcf7ce9SYonghong Song``btf_type`` is followed by ``info.vlen`` number of ``struct btf_param``.::
398ffcf7ce9SYonghong Song
399ffcf7ce9SYonghong Song    struct btf_param {
400ffcf7ce9SYonghong Song        __u32   name_off;
401ffcf7ce9SYonghong Song        __u32   type;
402ffcf7ce9SYonghong Song    };
403ffcf7ce9SYonghong Song
4049ab5305dSAndrii NakryikoIf a BTF_KIND_FUNC_PROTO type is referred by a BTF_KIND_FUNC type, then
4059ab5305dSAndrii Nakryiko``btf_param.name_off`` must point to a valid C identifier except for the
4069ab5305dSAndrii Nakryikopossible last argument representing the variable argument. The btf_param.type
4079ab5305dSAndrii Nakryikorefers to parameter type.
408ffcf7ce9SYonghong Song
4099ab5305dSAndrii NakryikoIf the function has variable arguments, the last parameter is encoded with
4109ab5305dSAndrii Nakryiko``name_off = 0`` and ``type = 0``.
411ffcf7ce9SYonghong Song
412f063c889SDaniel Borkmann2.2.14 BTF_KIND_VAR
413f063c889SDaniel Borkmann~~~~~~~~~~~~~~~~~~~
414f063c889SDaniel Borkmann
415f063c889SDaniel Borkmann``struct btf_type`` encoding requirement:
416f063c889SDaniel Borkmann  * ``name_off``: offset to a valid C identifier
417f063c889SDaniel Borkmann  * ``info.kind_flag``: 0
418f063c889SDaniel Borkmann  * ``info.kind``: BTF_KIND_VAR
419f063c889SDaniel Borkmann  * ``info.vlen``: 0
420f063c889SDaniel Borkmann  * ``type``: the type of the variable
421f063c889SDaniel Borkmann
422f063c889SDaniel Borkmann``btf_type`` is followed by a single ``struct btf_variable`` with the
423f063c889SDaniel Borkmannfollowing data::
424f063c889SDaniel Borkmann
425f063c889SDaniel Borkmann    struct btf_var {
426f063c889SDaniel Borkmann        __u32   linkage;
427f063c889SDaniel Borkmann    };
428f063c889SDaniel Borkmann
429f063c889SDaniel Borkmann``struct btf_var`` encoding:
430f063c889SDaniel Borkmann  * ``linkage``: currently only static variable 0, or globally allocated
431f063c889SDaniel Borkmann                 variable in ELF sections 1
432f063c889SDaniel Borkmann
433f063c889SDaniel BorkmannNot all type of global variables are supported by LLVM at this point.
434f063c889SDaniel BorkmannThe following is currently available:
435f063c889SDaniel Borkmann
436f063c889SDaniel Borkmann  * static variables with or without section attributes
437f063c889SDaniel Borkmann  * global variables with section attributes
438f063c889SDaniel Borkmann
439f063c889SDaniel BorkmannThe latter is for future extraction of map key/value type id's from a
440f063c889SDaniel Borkmannmap definition.
441f063c889SDaniel Borkmann
442f063c889SDaniel Borkmann2.2.15 BTF_KIND_DATASEC
443f063c889SDaniel Borkmann~~~~~~~~~~~~~~~~~~~~~~~
444f063c889SDaniel Borkmann
445f063c889SDaniel Borkmann``struct btf_type`` encoding requirement:
446f063c889SDaniel Borkmann  * ``name_off``: offset to a valid name associated with a variable or
447f063c889SDaniel Borkmann                  one of .data/.bss/.rodata
448f063c889SDaniel Borkmann  * ``info.kind_flag``: 0
449f063c889SDaniel Borkmann  * ``info.kind``: BTF_KIND_DATASEC
450f063c889SDaniel Borkmann  * ``info.vlen``: # of variables
451f063c889SDaniel Borkmann  * ``size``: total section size in bytes (0 at compilation time, patched
452f063c889SDaniel Borkmann              to actual size by BPF loaders such as libbpf)
453f063c889SDaniel Borkmann
454f063c889SDaniel Borkmann``btf_type`` is followed by ``info.vlen`` number of ``struct btf_var_secinfo``.::
455f063c889SDaniel Borkmann
456f063c889SDaniel Borkmann    struct btf_var_secinfo {
457f063c889SDaniel Borkmann        __u32   type;
458f063c889SDaniel Borkmann        __u32   offset;
459f063c889SDaniel Borkmann        __u32   size;
460f063c889SDaniel Borkmann    };
461f063c889SDaniel Borkmann
462f063c889SDaniel Borkmann``struct btf_var_secinfo`` encoding:
463f063c889SDaniel Borkmann  * ``type``: the type of the BTF_KIND_VAR variable
464f063c889SDaniel Borkmann  * ``offset``: the in-section offset of the variable
465f063c889SDaniel Borkmann  * ``size``: the size of the variable in bytes
466f063c889SDaniel Borkmann
4676be6a0baSIlya Leoshkevich2.2.16 BTF_KIND_FLOAT
4686be6a0baSIlya Leoshkevich~~~~~~~~~~~~~~~~~~~~~
4696be6a0baSIlya Leoshkevich
4706be6a0baSIlya Leoshkevich``struct btf_type`` encoding requirement:
4716be6a0baSIlya Leoshkevich * ``name_off``: any valid offset
4726be6a0baSIlya Leoshkevich * ``info.kind_flag``: 0
4736be6a0baSIlya Leoshkevich * ``info.kind``: BTF_KIND_FLOAT
4746be6a0baSIlya Leoshkevich * ``info.vlen``: 0
4756be6a0baSIlya Leoshkevich * ``size``: the size of the float type in bytes: 2, 4, 8, 12 or 16.
4766be6a0baSIlya Leoshkevich
4776be6a0baSIlya LeoshkevichNo additional type data follow ``btf_type``.
4786be6a0baSIlya Leoshkevich
479223f903eSYonghong Song2.2.17 BTF_KIND_DECL_TAG
480223f903eSYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~
48148f5a6c4SYonghong Song
48248f5a6c4SYonghong Song``struct btf_type`` encoding requirement:
48348f5a6c4SYonghong Song * ``name_off``: offset to a non-empty string
48448f5a6c4SYonghong Song * ``info.kind_flag``: 0
485223f903eSYonghong Song * ``info.kind``: BTF_KIND_DECL_TAG
48648f5a6c4SYonghong Song * ``info.vlen``: 0
4875a867134SYonghong Song * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef``
48848f5a6c4SYonghong Song
489223f903eSYonghong Song``btf_type`` is followed by ``struct btf_decl_tag``.::
49048f5a6c4SYonghong Song
491223f903eSYonghong Song    struct btf_decl_tag {
49248f5a6c4SYonghong Song        __u32   component_idx;
49348f5a6c4SYonghong Song    };
49448f5a6c4SYonghong Song
495223f903eSYonghong SongThe ``name_off`` encodes btf_decl_tag attribute string.
4965a867134SYonghong SongThe ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``.
4975a867134SYonghong SongFor ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``.
498223f903eSYonghong SongFor the other three types, if the btf_decl_tag attribute is
49948f5a6c4SYonghong Songapplied to the ``struct``, ``union`` or ``func`` itself,
500223f903eSYonghong Song``btf_decl_tag.component_idx`` must be ``-1``. Otherwise,
50148f5a6c4SYonghong Songthe attribute is applied to a ``struct``/``union`` member or
502223f903eSYonghong Songa ``func`` argument, and ``btf_decl_tag.component_idx`` should be a
50348f5a6c4SYonghong Songvalid index (starting from 0) pointing to a member or an argument.
50448f5a6c4SYonghong Song
50561dbd598SYonghong Song2.2.18 BTF_KIND_TYPE_TAG
506d52f5c63SYonghong Song~~~~~~~~~~~~~~~~~~~~~~~~
507d52f5c63SYonghong Song
508d52f5c63SYonghong Song``struct btf_type`` encoding requirement:
509d52f5c63SYonghong Song * ``name_off``: offset to a non-empty string
510d52f5c63SYonghong Song * ``info.kind_flag``: 0
511d52f5c63SYonghong Song * ``info.kind``: BTF_KIND_TYPE_TAG
512d52f5c63SYonghong Song * ``info.vlen``: 0
513d52f5c63SYonghong Song * ``type``: the type with ``btf_type_tag`` attribute
514d52f5c63SYonghong Song
515b7290384SYonghong SongCurrently, ``BTF_KIND_TYPE_TAG`` is only emitted for pointer types.
516b7290384SYonghong SongIt has the following btf type chain:
517b7290384SYonghong Song::
518b7290384SYonghong Song
519b7290384SYonghong Song  ptr -> [type_tag]*
520b7290384SYonghong Song      -> [const | volatile | restrict | typedef]*
521b7290384SYonghong Song      -> base_type
522b7290384SYonghong Song
523b7290384SYonghong SongBasically, a pointer type points to zero or more
524b7290384SYonghong Songtype_tag, then zero or more const/volatile/restrict/typedef
525b7290384SYonghong Songand finally the base type. The base type is one of
526b7290384SYonghong Songint, ptr, array, struct, union, enum, func_proto and float types.
527b7290384SYonghong Song
52861dbd598SYonghong Song2.2.19 BTF_KIND_ENUM64
52961dbd598SYonghong Song~~~~~~~~~~~~~~~~~~~~~~
53061dbd598SYonghong Song
53161dbd598SYonghong Song``struct btf_type`` encoding requirement:
53261dbd598SYonghong Song  * ``name_off``: 0 or offset to a valid C identifier
53361dbd598SYonghong Song  * ``info.kind_flag``: 0 for unsigned, 1 for signed
53461dbd598SYonghong Song  * ``info.kind``: BTF_KIND_ENUM64
53561dbd598SYonghong Song  * ``info.vlen``: number of enum values
53661dbd598SYonghong Song  * ``size``: 1/2/4/8
53761dbd598SYonghong Song
53861dbd598SYonghong Song``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
53961dbd598SYonghong Song
54061dbd598SYonghong Song    struct btf_enum64 {
54161dbd598SYonghong Song        __u32   name_off;
54261dbd598SYonghong Song        __u32   val_lo32;
54361dbd598SYonghong Song        __u32   val_hi32;
54461dbd598SYonghong Song    };
54561dbd598SYonghong Song
54661dbd598SYonghong SongThe ``btf_enum64`` encoding:
54761dbd598SYonghong Song  * ``name_off``: offset to a valid C identifier
54861dbd598SYonghong Song  * ``val_lo32``: lower 32-bit value for a 64-bit value
54961dbd598SYonghong Song  * ``val_hi32``: high 32-bit value for a 64-bit value
55061dbd598SYonghong Song
55161dbd598SYonghong SongIf the original enum value is signed and the size is less than 8,
55261dbd598SYonghong Songthat value will be sign extended into 8 bytes.
55361dbd598SYonghong Song
554ffcf7ce9SYonghong Song3. BTF Kernel API
5553ff36bffSDave Tucker=================
556ffcf7ce9SYonghong Song
557ffcf7ce9SYonghong SongThe following bpf syscall command involves BTF:
558ffcf7ce9SYonghong Song   * BPF_BTF_LOAD: load a blob of BTF data into kernel
559ffcf7ce9SYonghong Song   * BPF_MAP_CREATE: map creation with btf key and value type info.
560ffcf7ce9SYonghong Song   * BPF_PROG_LOAD: prog load with btf function and line info.
561ffcf7ce9SYonghong Song   * BPF_BTF_GET_FD_BY_ID: get a btf fd
562ffcf7ce9SYonghong Song   * BPF_OBJ_GET_INFO_BY_FD: btf, func_info, line_info
563ffcf7ce9SYonghong Song     and other btf related info are returned.
564ffcf7ce9SYonghong Song
565ffcf7ce9SYonghong SongThe workflow typically looks like:
566ffcf7ce9SYonghong Song::
567ffcf7ce9SYonghong Song
568ffcf7ce9SYonghong Song  Application:
569ffcf7ce9SYonghong Song      BPF_BTF_LOAD
570ffcf7ce9SYonghong Song          |
571ffcf7ce9SYonghong Song          v
572ffcf7ce9SYonghong Song      BPF_MAP_CREATE and BPF_PROG_LOAD
573ffcf7ce9SYonghong Song          |
574ffcf7ce9SYonghong Song          V
575ffcf7ce9SYonghong Song      ......
576ffcf7ce9SYonghong Song
577ffcf7ce9SYonghong Song  Introspection tool:
578ffcf7ce9SYonghong Song      ......
579ffcf7ce9SYonghong Song      BPF_{PROG,MAP}_GET_NEXT_ID (get prog/map id's)
580ffcf7ce9SYonghong Song          |
581ffcf7ce9SYonghong Song          V
582ffcf7ce9SYonghong Song      BPF_{PROG,MAP}_GET_FD_BY_ID (get a prog/map fd)
583ffcf7ce9SYonghong Song          |
584ffcf7ce9SYonghong Song          V
585ffcf7ce9SYonghong Song      BPF_OBJ_GET_INFO_BY_FD (get bpf_prog_info/bpf_map_info with btf_id)
586ffcf7ce9SYonghong Song          |                                     |
587ffcf7ce9SYonghong Song          V                                     |
588ffcf7ce9SYonghong Song      BPF_BTF_GET_FD_BY_ID (get btf_fd)         |
589ffcf7ce9SYonghong Song          |                                     |
590ffcf7ce9SYonghong Song          V                                     |
591ffcf7ce9SYonghong Song      BPF_OBJ_GET_INFO_BY_FD (get btf)          |
592ffcf7ce9SYonghong Song          |                                     |
593ffcf7ce9SYonghong Song          V                                     V
594ffcf7ce9SYonghong Song      pretty print types, dump func signatures and line info, etc.
595ffcf7ce9SYonghong Song
596ffcf7ce9SYonghong Song
597ffcf7ce9SYonghong Song3.1 BPF_BTF_LOAD
5983ff36bffSDave Tucker----------------
599ffcf7ce9SYonghong Song
6009ab5305dSAndrii NakryikoLoad a blob of BTF data into kernel. A blob of data, described in
6019ab5305dSAndrii Nakryiko:ref:`BTF_Type_String`, can be directly loaded into the kernel. A ``btf_fd``
6029ab5305dSAndrii Nakryikois returned to a userspace.
603ffcf7ce9SYonghong Song
604ffcf7ce9SYonghong Song3.2 BPF_MAP_CREATE
6053ff36bffSDave Tucker------------------
606ffcf7ce9SYonghong Song
607ffcf7ce9SYonghong SongA map can be created with ``btf_fd`` and specified key/value type id.::
608ffcf7ce9SYonghong Song
609ffcf7ce9SYonghong Song    __u32   btf_fd;         /* fd pointing to a BTF type data */
610ffcf7ce9SYonghong Song    __u32   btf_key_type_id;        /* BTF type_id of the key */
611ffcf7ce9SYonghong Song    __u32   btf_value_type_id;      /* BTF type_id of the value */
612ffcf7ce9SYonghong Song
613ffcf7ce9SYonghong SongIn libbpf, the map can be defined with extra annotation like below:
614ffcf7ce9SYonghong Song::
615ffcf7ce9SYonghong Song
61696c85308SAndrii Nakryiko    struct {
61796c85308SAndrii Nakryiko        __uint(type, BPF_MAP_TYPE_ARRAY);
61896c85308SAndrii Nakryiko        __type(key, int);
61996c85308SAndrii Nakryiko        __type(value, struct ipv_counts);
62096c85308SAndrii Nakryiko        __uint(max_entries, 4);
62196c85308SAndrii Nakryiko    } btf_map SEC(".maps");
622ffcf7ce9SYonghong Song
62396c85308SAndrii NakryikoDuring ELF parsing, libbpf is able to extract key/value type_id's and assign
62496c85308SAndrii Nakryikothem to BPF_MAP_CREATE attributes automatically.
625ffcf7ce9SYonghong Song
626ffcf7ce9SYonghong Song.. _BPF_Prog_Load:
627ffcf7ce9SYonghong Song
628ffcf7ce9SYonghong Song3.3 BPF_PROG_LOAD
6293ff36bffSDave Tucker-----------------
630ffcf7ce9SYonghong Song
6319ab5305dSAndrii NakryikoDuring prog_load, func_info and line_info can be passed to kernel with proper
6329ab5305dSAndrii Nakryikovalues for the following attributes:
633ffcf7ce9SYonghong Song::
634ffcf7ce9SYonghong Song
635ffcf7ce9SYonghong Song    __u32           insn_cnt;
636ffcf7ce9SYonghong Song    __aligned_u64   insns;
637ffcf7ce9SYonghong Song    ......
638ffcf7ce9SYonghong Song    __u32           prog_btf_fd;    /* fd pointing to BTF type data */
639ffcf7ce9SYonghong Song    __u32           func_info_rec_size;     /* userspace bpf_func_info size */
640ffcf7ce9SYonghong Song    __aligned_u64   func_info;      /* func info */
641ffcf7ce9SYonghong Song    __u32           func_info_cnt;  /* number of bpf_func_info records */
642ffcf7ce9SYonghong Song    __u32           line_info_rec_size;     /* userspace bpf_line_info size */
643ffcf7ce9SYonghong Song    __aligned_u64   line_info;      /* line info */
644ffcf7ce9SYonghong Song    __u32           line_info_cnt;  /* number of bpf_line_info records */
645ffcf7ce9SYonghong Song
646ffcf7ce9SYonghong SongThe func_info and line_info are an array of below, respectively.::
647ffcf7ce9SYonghong Song
648ffcf7ce9SYonghong Song    struct bpf_func_info {
649ffcf7ce9SYonghong Song        __u32   insn_off; /* [0, insn_cnt - 1] */
650ffcf7ce9SYonghong Song        __u32   type_id;  /* pointing to a BTF_KIND_FUNC type */
651ffcf7ce9SYonghong Song    };
652ffcf7ce9SYonghong Song    struct bpf_line_info {
653ffcf7ce9SYonghong Song        __u32   insn_off; /* [0, insn_cnt - 1] */
654ffcf7ce9SYonghong Song        __u32   file_name_off; /* offset to string table for the filename */
655ffcf7ce9SYonghong Song        __u32   line_off; /* offset to string table for the source line */
656ffcf7ce9SYonghong Song        __u32   line_col; /* line number and column number */
657ffcf7ce9SYonghong Song    };
658ffcf7ce9SYonghong Song
6599ab5305dSAndrii Nakryikofunc_info_rec_size is the size of each func_info record, and
6609ab5305dSAndrii Nakryikoline_info_rec_size is the size of each line_info record. Passing the record
6619ab5305dSAndrii Nakryikosize to kernel make it possible to extend the record itself in the future.
662ffcf7ce9SYonghong Song
663ffcf7ce9SYonghong SongBelow are requirements for func_info:
664ffcf7ce9SYonghong Song  * func_info[0].insn_off must be 0.
665ffcf7ce9SYonghong Song  * the func_info insn_off is in strictly increasing order and matches
666ffcf7ce9SYonghong Song    bpf func boundaries.
667ffcf7ce9SYonghong Song
668ffcf7ce9SYonghong SongBelow are requirements for line_info:
6695efc529fSAndrii Nakryiko  * the first insn in each func must have a line_info record pointing to it.
670ffcf7ce9SYonghong Song  * the line_info insn_off is in strictly increasing order.
671ffcf7ce9SYonghong Song
672ffcf7ce9SYonghong SongFor line_info, the line number and column number are defined as below:
673ffcf7ce9SYonghong Song::
674ffcf7ce9SYonghong Song
675ffcf7ce9SYonghong Song    #define BPF_LINE_INFO_LINE_NUM(line_col)        ((line_col) >> 10)
676ffcf7ce9SYonghong Song    #define BPF_LINE_INFO_LINE_COL(line_col)        ((line_col) & 0x3ff)
677ffcf7ce9SYonghong Song
678ffcf7ce9SYonghong Song3.4 BPF_{PROG,MAP}_GET_NEXT_ID
6793ff36bffSDave Tucker------------------------------
680ffcf7ce9SYonghong Song
6819ab5305dSAndrii NakryikoIn kernel, every loaded program, map or btf has a unique id. The id won't
6829ab5305dSAndrii Nakryikochange during the lifetime of a program, map, or btf.
683ffcf7ce9SYonghong Song
6849ab5305dSAndrii NakryikoThe bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID returns all id's, one for
6859ab5305dSAndrii Nakryikoeach command, to user space, for bpf program or maps, respectively, so an
6869ab5305dSAndrii Nakryikoinspection tool can inspect all programs and maps.
687ffcf7ce9SYonghong Song
688ffcf7ce9SYonghong Song3.5 BPF_{PROG,MAP}_GET_FD_BY_ID
6893ff36bffSDave Tucker-------------------------------
690ffcf7ce9SYonghong Song
6915efc529fSAndrii NakryikoAn introspection tool cannot use id to get details about program or maps.
6925efc529fSAndrii NakryikoA file descriptor needs to be obtained first for reference-counting purpose.
693ffcf7ce9SYonghong Song
694ffcf7ce9SYonghong Song3.6 BPF_OBJ_GET_INFO_BY_FD
6953ff36bffSDave Tucker--------------------------
696ffcf7ce9SYonghong Song
6979ab5305dSAndrii NakryikoOnce a program/map fd is acquired, an introspection tool can get the detailed
6989ab5305dSAndrii Nakryikoinformation from kernel about this fd, some of which are BTF-related. For
6999ab5305dSAndrii Nakryikoexample, ``bpf_map_info`` returns ``btf_id`` and key/value type ids.
7009ab5305dSAndrii Nakryiko``bpf_prog_info`` returns ``btf_id``, func_info, and line info for translated
7019ab5305dSAndrii Nakryikobpf byte codes, and jited_line_info.
702ffcf7ce9SYonghong Song
703ffcf7ce9SYonghong Song3.7 BPF_BTF_GET_FD_BY_ID
7043ff36bffSDave Tucker------------------------
705ffcf7ce9SYonghong Song
7069ab5305dSAndrii NakryikoWith ``btf_id`` obtained in ``bpf_map_info`` and ``bpf_prog_info``, bpf
7079ab5305dSAndrii Nakryikosyscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd. Then, with
7089ab5305dSAndrii Nakryikocommand BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally loaded into the
7099ab5305dSAndrii Nakryikokernel with BPF_BTF_LOAD, can be retrieved.
710ffcf7ce9SYonghong Song
7115efc529fSAndrii NakryikoWith the btf blob, ``bpf_map_info``, and ``bpf_prog_info``, an introspection
7129ab5305dSAndrii Nakryikotool has full btf knowledge and is able to pretty print map key/values, dump
7139ab5305dSAndrii Nakryikofunc signatures and line info, along with byte/jit codes.
714ffcf7ce9SYonghong Song
715ffcf7ce9SYonghong Song4. ELF File Format Interface
7163ff36bffSDave Tucker============================
717ffcf7ce9SYonghong Song
718ffcf7ce9SYonghong Song4.1 .BTF section
7193ff36bffSDave Tucker----------------
720ffcf7ce9SYonghong Song
7219ab5305dSAndrii NakryikoThe .BTF section contains type and string data. The format of this section is
7229ab5305dSAndrii Nakryikosame as the one describe in :ref:`BTF_Type_String`.
723ffcf7ce9SYonghong Song
724ffcf7ce9SYonghong Song.. _BTF_Ext_Section:
725ffcf7ce9SYonghong Song
726ffcf7ce9SYonghong Song4.2 .BTF.ext section
7273ff36bffSDave Tucker--------------------
728ffcf7ce9SYonghong Song
729be4033d3SEduard ZingermanThe .BTF.ext section encodes func_info, line_info and CO-RE relocations
730be4033d3SEduard Zingermanwhich needs loader manipulation before loading into the kernel.
731ffcf7ce9SYonghong Song
7329ab5305dSAndrii NakryikoThe specification for .BTF.ext section is defined at ``tools/lib/bpf/btf.h``
7339ab5305dSAndrii Nakryikoand ``tools/lib/bpf/btf.c``.
734ffcf7ce9SYonghong Song
735ffcf7ce9SYonghong SongThe current header of .BTF.ext section::
736ffcf7ce9SYonghong Song
737ffcf7ce9SYonghong Song    struct btf_ext_header {
738ffcf7ce9SYonghong Song        __u16   magic;
739ffcf7ce9SYonghong Song        __u8    version;
740ffcf7ce9SYonghong Song        __u8    flags;
741ffcf7ce9SYonghong Song        __u32   hdr_len;
742ffcf7ce9SYonghong Song
743ffcf7ce9SYonghong Song        /* All offsets are in bytes relative to the end of this header */
744ffcf7ce9SYonghong Song        __u32   func_info_off;
745ffcf7ce9SYonghong Song        __u32   func_info_len;
746ffcf7ce9SYonghong Song        __u32   line_info_off;
747ffcf7ce9SYonghong Song        __u32   line_info_len;
748be4033d3SEduard Zingerman
749be4033d3SEduard Zingerman        /* optional part of .BTF.ext header */
750be4033d3SEduard Zingerman        __u32   core_relo_off;
751be4033d3SEduard Zingerman        __u32   core_relo_len;
752ffcf7ce9SYonghong Song    };
753ffcf7ce9SYonghong Song
7549ab5305dSAndrii NakryikoIt is very similar to .BTF section. Instead of type/string section, it
755be4033d3SEduard Zingermancontains func_info, line_info and core_relo sub-sections.
756be4033d3SEduard ZingermanSee :ref:`BPF_Prog_Load` for details about func_info and line_info
757be4033d3SEduard Zingermanrecord format.
758ffcf7ce9SYonghong Song
759ffcf7ce9SYonghong SongThe func_info is organized as below.::
760ffcf7ce9SYonghong Song
761be4033d3SEduard Zingerman     func_info_rec_size              /* __u32 value */
762ffcf7ce9SYonghong Song     btf_ext_info_sec for section #1 /* func_info for section #1 */
763ffcf7ce9SYonghong Song     btf_ext_info_sec for section #2 /* func_info for section #2 */
764ffcf7ce9SYonghong Song     ...
765ffcf7ce9SYonghong Song
7669ab5305dSAndrii Nakryiko``func_info_rec_size`` specifies the size of ``bpf_func_info`` structure when
7679ab5305dSAndrii Nakryiko.BTF.ext is generated. ``btf_ext_info_sec``, defined below, is a collection of
7689ab5305dSAndrii Nakryikofunc_info for each specific ELF section.::
769ffcf7ce9SYonghong Song
770ffcf7ce9SYonghong Song     struct btf_ext_info_sec {
771ffcf7ce9SYonghong Song        __u32   sec_name_off; /* offset to section name */
772ffcf7ce9SYonghong Song        __u32   num_info;
773ffcf7ce9SYonghong Song        /* Followed by num_info * record_size number of bytes */
774ffcf7ce9SYonghong Song        __u8    data[0];
775ffcf7ce9SYonghong Song     };
776ffcf7ce9SYonghong Song
777ffcf7ce9SYonghong SongHere, num_info must be greater than 0.
778ffcf7ce9SYonghong Song
779ffcf7ce9SYonghong SongThe line_info is organized as below.::
780ffcf7ce9SYonghong Song
781be4033d3SEduard Zingerman     line_info_rec_size              /* __u32 value */
782ffcf7ce9SYonghong Song     btf_ext_info_sec for section #1 /* line_info for section #1 */
783ffcf7ce9SYonghong Song     btf_ext_info_sec for section #2 /* line_info for section #2 */
784ffcf7ce9SYonghong Song     ...
785ffcf7ce9SYonghong Song
7869ab5305dSAndrii Nakryiko``line_info_rec_size`` specifies the size of ``bpf_line_info`` structure when
7879ab5305dSAndrii Nakryiko.BTF.ext is generated.
788ffcf7ce9SYonghong Song
789ffcf7ce9SYonghong SongThe interpretation of ``bpf_func_info->insn_off`` and
7909ab5305dSAndrii Nakryiko``bpf_line_info->insn_off`` is different between kernel API and ELF API. For
7919ab5305dSAndrii Nakryikokernel API, the ``insn_off`` is the instruction offset in the unit of ``struct
7929ab5305dSAndrii Nakryikobpf_insn``. For ELF API, the ``insn_off`` is the byte offset from the
7939ab5305dSAndrii Nakryikobeginning of section (``btf_ext_info_sec->sec_name_off``).
794ffcf7ce9SYonghong Song
795be4033d3SEduard ZingermanThe core_relo is organized as below.::
796be4033d3SEduard Zingerman
797be4033d3SEduard Zingerman     core_relo_rec_size              /* __u32 value */
798be4033d3SEduard Zingerman     btf_ext_info_sec for section #1 /* core_relo for section #1 */
799be4033d3SEduard Zingerman     btf_ext_info_sec for section #2 /* core_relo for section #2 */
800be4033d3SEduard Zingerman
801be4033d3SEduard Zingerman``core_relo_rec_size`` specifies the size of ``bpf_core_relo``
802be4033d3SEduard Zingermanstructure when .BTF.ext is generated. All ``bpf_core_relo`` structures
803be4033d3SEduard Zingermanwithin a single ``btf_ext_info_sec`` describe relocations applied to
804be4033d3SEduard Zingermansection named by ``btf_ext_info_sec->sec_name_off``.
805be4033d3SEduard Zingerman
806*3888fa13SEduard ZingermanSee :ref:`Documentation/bpf/llvm_reloc.rst <btf-co-re-relocations>`
807be4033d3SEduard Zingermanfor more information on CO-RE relocations.
808be4033d3SEduard Zingerman
809232ce4beSJiri Olsa4.2 .BTF_ids section
8103ff36bffSDave Tucker--------------------
811232ce4beSJiri Olsa
812232ce4beSJiri OlsaThe .BTF_ids section encodes BTF ID values that are used within the kernel.
813232ce4beSJiri Olsa
814232ce4beSJiri OlsaThis section is created during the kernel compilation with the help of
815232ce4beSJiri Olsamacros defined in ``include/linux/btf_ids.h`` header file. Kernel code can
816232ce4beSJiri Olsause them to create lists and sets (sorted lists) of BTF ID values.
817232ce4beSJiri Olsa
818232ce4beSJiri OlsaThe ``BTF_ID_LIST`` and ``BTF_ID`` macros define unsorted list of BTF ID values,
819232ce4beSJiri Olsawith following syntax::
820232ce4beSJiri Olsa
821232ce4beSJiri Olsa  BTF_ID_LIST(list)
822232ce4beSJiri Olsa  BTF_ID(type1, name1)
823232ce4beSJiri Olsa  BTF_ID(type2, name2)
824232ce4beSJiri Olsa
825232ce4beSJiri Olsaresulting in following layout in .BTF_ids section::
826232ce4beSJiri Olsa
827232ce4beSJiri Olsa  __BTF_ID__type1__name1__1:
828232ce4beSJiri Olsa  .zero 4
829232ce4beSJiri Olsa  __BTF_ID__type2__name2__2:
830232ce4beSJiri Olsa  .zero 4
831232ce4beSJiri Olsa
832232ce4beSJiri OlsaThe ``u32 list[];`` variable is defined to access the list.
833232ce4beSJiri Olsa
834232ce4beSJiri OlsaThe ``BTF_ID_UNUSED`` macro defines 4 zero bytes. It's used when we
835232ce4beSJiri Olsawant to define unused entry in BTF_ID_LIST, like::
836232ce4beSJiri Olsa
837232ce4beSJiri Olsa      BTF_ID_LIST(bpf_skb_output_btf_ids)
838232ce4beSJiri Olsa      BTF_ID(struct, sk_buff)
839232ce4beSJiri Olsa      BTF_ID_UNUSED
840232ce4beSJiri Olsa      BTF_ID(struct, task_struct)
841232ce4beSJiri Olsa
84268a26bc7SJiri OlsaThe ``BTF_SET_START/END`` macros pair defines sorted list of BTF ID values
84368a26bc7SJiri Olsaand their count, with following syntax::
84468a26bc7SJiri Olsa
84568a26bc7SJiri Olsa  BTF_SET_START(set)
84668a26bc7SJiri Olsa  BTF_ID(type1, name1)
84768a26bc7SJiri Olsa  BTF_ID(type2, name2)
84868a26bc7SJiri Olsa  BTF_SET_END(set)
84968a26bc7SJiri Olsa
85068a26bc7SJiri Olsaresulting in following layout in .BTF_ids section::
85168a26bc7SJiri Olsa
85268a26bc7SJiri Olsa  __BTF_ID__set__set:
85368a26bc7SJiri Olsa  .zero 4
85468a26bc7SJiri Olsa  __BTF_ID__type1__name1__3:
85568a26bc7SJiri Olsa  .zero 4
85668a26bc7SJiri Olsa  __BTF_ID__type2__name2__4:
85768a26bc7SJiri Olsa  .zero 4
85868a26bc7SJiri Olsa
85968a26bc7SJiri OlsaThe ``struct btf_id_set set;`` variable is defined to access the list.
86068a26bc7SJiri Olsa
86168a26bc7SJiri OlsaThe ``typeX`` name can be one of following::
86268a26bc7SJiri Olsa
86368a26bc7SJiri Olsa   struct, union, typedef, func
86468a26bc7SJiri Olsa
86568a26bc7SJiri Olsaand is used as a filter when resolving the BTF ID value.
86668a26bc7SJiri Olsa
867232ce4beSJiri OlsaAll the BTF ID lists and sets are compiled in the .BTF_ids section and
868232ce4beSJiri Olsaresolved during the linking phase of kernel build by ``resolve_btfids`` tool.
869232ce4beSJiri Olsa
870ffcf7ce9SYonghong Song5. Using BTF
8713ff36bffSDave Tucker============
872ffcf7ce9SYonghong Song
873ffcf7ce9SYonghong Song5.1 bpftool map pretty print
8743ff36bffSDave Tucker----------------------------
875ffcf7ce9SYonghong Song
8769ab5305dSAndrii NakryikoWith BTF, the map key/value can be printed based on fields rather than simply
8779ab5305dSAndrii Nakryikoraw bytes. This is especially valuable for large structure or if your data
8789ab5305dSAndrii Nakryikostructure has bitfields. For example, for the following map,::
879ffcf7ce9SYonghong Song
880ffcf7ce9SYonghong Song      enum A { A1, A2, A3, A4, A5 };
881ffcf7ce9SYonghong Song      typedef enum A ___A;
882ffcf7ce9SYonghong Song      struct tmp_t {
883ffcf7ce9SYonghong Song           char a1:4;
884ffcf7ce9SYonghong Song           int  a2:4;
885ffcf7ce9SYonghong Song           int  :4;
886ffcf7ce9SYonghong Song           __u32 a3:4;
887ffcf7ce9SYonghong Song           int b;
888ffcf7ce9SYonghong Song           ___A b1:4;
889ffcf7ce9SYonghong Song           enum A b2:4;
890ffcf7ce9SYonghong Song      };
89196c85308SAndrii Nakryiko      struct {
89296c85308SAndrii Nakryiko           __uint(type, BPF_MAP_TYPE_ARRAY);
89396c85308SAndrii Nakryiko           __type(key, int);
89496c85308SAndrii Nakryiko           __type(value, struct tmp_t);
89596c85308SAndrii Nakryiko           __uint(max_entries, 1);
89696c85308SAndrii Nakryiko      } tmpmap SEC(".maps");
897ffcf7ce9SYonghong Song
898ffcf7ce9SYonghong Songbpftool is able to pretty print like below:
899ffcf7ce9SYonghong Song::
900ffcf7ce9SYonghong Song
901ffcf7ce9SYonghong Song      [{
902ffcf7ce9SYonghong Song            "key": 0,
903ffcf7ce9SYonghong Song            "value": {
904ffcf7ce9SYonghong Song                "a1": 0x2,
905ffcf7ce9SYonghong Song                "a2": 0x4,
906ffcf7ce9SYonghong Song                "a3": 0x6,
907ffcf7ce9SYonghong Song                "b": 7,
908ffcf7ce9SYonghong Song                "b1": 0x8,
909ffcf7ce9SYonghong Song                "b2": 0xa
910ffcf7ce9SYonghong Song            }
911ffcf7ce9SYonghong Song        }
912ffcf7ce9SYonghong Song      ]
913ffcf7ce9SYonghong Song
914ffcf7ce9SYonghong Song5.2 bpftool prog dump
9153ff36bffSDave Tucker---------------------
916ffcf7ce9SYonghong Song
9179ab5305dSAndrii NakryikoThe following is an example showing how func_info and line_info can help prog
9189ab5305dSAndrii Nakryikodump with better kernel symbol names, function prototypes and line
9199ab5305dSAndrii Nakryikoinformation.::
920ffcf7ce9SYonghong Song
921ffcf7ce9SYonghong Song    $ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
922ffcf7ce9SYonghong Song    [...]
923ffcf7ce9SYonghong Song    int test_long_fname_2(struct dummy_tracepoint_args * arg):
924ffcf7ce9SYonghong Song    bpf_prog_44a040bf25481309_test_long_fname_2:
925ffcf7ce9SYonghong Song    ; static int test_long_fname_2(struct dummy_tracepoint_args *arg)
926ffcf7ce9SYonghong Song       0:   push   %rbp
927ffcf7ce9SYonghong Song       1:   mov    %rsp,%rbp
928ffcf7ce9SYonghong Song       4:   sub    $0x30,%rsp
929ffcf7ce9SYonghong Song       b:   sub    $0x28,%rbp
930ffcf7ce9SYonghong Song       f:   mov    %rbx,0x0(%rbp)
931ffcf7ce9SYonghong Song      13:   mov    %r13,0x8(%rbp)
932ffcf7ce9SYonghong Song      17:   mov    %r14,0x10(%rbp)
933ffcf7ce9SYonghong Song      1b:   mov    %r15,0x18(%rbp)
934ffcf7ce9SYonghong Song      1f:   xor    %eax,%eax
935ffcf7ce9SYonghong Song      21:   mov    %rax,0x20(%rbp)
936ffcf7ce9SYonghong Song      25:   xor    %esi,%esi
937ffcf7ce9SYonghong Song    ; int key = 0;
938ffcf7ce9SYonghong Song      27:   mov    %esi,-0x4(%rbp)
939ffcf7ce9SYonghong Song    ; if (!arg->sock)
940ffcf7ce9SYonghong Song      2a:   mov    0x8(%rdi),%rdi
941ffcf7ce9SYonghong Song    ; if (!arg->sock)
942ffcf7ce9SYonghong Song      2e:   cmp    $0x0,%rdi
943ffcf7ce9SYonghong Song      32:   je     0x0000000000000070
944ffcf7ce9SYonghong Song      34:   mov    %rbp,%rsi
945ffcf7ce9SYonghong Song    ; counts = bpf_map_lookup_elem(&btf_map, &key);
946ffcf7ce9SYonghong Song    [...]
947ffcf7ce9SYonghong Song
9485efc529fSAndrii Nakryiko5.3 Verifier Log
9493ff36bffSDave Tucker----------------
950ffcf7ce9SYonghong Song
9519ab5305dSAndrii NakryikoThe following is an example of how line_info can help debugging verification
9529ab5305dSAndrii Nakryikofailure.::
953ffcf7ce9SYonghong Song
954ffcf7ce9SYonghong Song       /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c
955ffcf7ce9SYonghong Song        * is modified as below.
956ffcf7ce9SYonghong Song        */
957ffcf7ce9SYonghong Song       data = (void *)(long)xdp->data;
958ffcf7ce9SYonghong Song       data_end = (void *)(long)xdp->data_end;
959ffcf7ce9SYonghong Song       /*
960ffcf7ce9SYonghong Song       if (data + 4 > data_end)
961ffcf7ce9SYonghong Song               return XDP_DROP;
962ffcf7ce9SYonghong Song       */
963ffcf7ce9SYonghong Song       *(u32 *)data = dst->dst;
964ffcf7ce9SYonghong Song
965ffcf7ce9SYonghong Song    $ bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp
966ffcf7ce9SYonghong Song        ; data = (void *)(long)xdp->data;
967ffcf7ce9SYonghong Song        224: (79) r2 = *(u64 *)(r10 -112)
968ffcf7ce9SYonghong Song        225: (61) r2 = *(u32 *)(r2 +0)
969ffcf7ce9SYonghong Song        ; *(u32 *)data = dst->dst;
970ffcf7ce9SYonghong Song        226: (63) *(u32 *)(r2 +0) = r1
971ffcf7ce9SYonghong Song        invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0)
972ffcf7ce9SYonghong Song        R2 offset is outside of the packet
973ffcf7ce9SYonghong Song
974ffcf7ce9SYonghong Song6. BTF Generation
9753ff36bffSDave Tucker=================
976ffcf7ce9SYonghong Song
977ffcf7ce9SYonghong SongYou need latest pahole
978ffcf7ce9SYonghong Song
979ffcf7ce9SYonghong Song  https://git.kernel.org/pub/scm/devel/pahole/pahole.git/
980ffcf7ce9SYonghong Song
9819ab5305dSAndrii Nakryikoor llvm (8.0 or later). The pahole acts as a dwarf2btf converter. It doesn't
9829ab5305dSAndrii Nakryikosupport .BTF.ext and btf BTF_KIND_FUNC type yet. For example,::
983ffcf7ce9SYonghong Song
984ffcf7ce9SYonghong Song      -bash-4.4$ cat t.c
985ffcf7ce9SYonghong Song      struct t {
986ffcf7ce9SYonghong Song        int a:2;
987ffcf7ce9SYonghong Song        int b:3;
988ffcf7ce9SYonghong Song        int c:2;
989ffcf7ce9SYonghong Song      } g;
990ffcf7ce9SYonghong Song      -bash-4.4$ gcc -c -O2 -g t.c
991ffcf7ce9SYonghong Song      -bash-4.4$ pahole -JV t.o
992ffcf7ce9SYonghong Song      File t.o:
993ffcf7ce9SYonghong Song      [1] STRUCT t kind_flag=1 size=4 vlen=3
994ffcf7ce9SYonghong Song              a type_id=2 bitfield_size=2 bits_offset=0
995ffcf7ce9SYonghong Song              b type_id=2 bitfield_size=3 bits_offset=2
996ffcf7ce9SYonghong Song              c type_id=2 bitfield_size=2 bits_offset=5
997ffcf7ce9SYonghong Song      [2] INT int size=4 bit_offset=0 nr_bits=32 encoding=SIGNED
998ffcf7ce9SYonghong Song
9999ab5305dSAndrii NakryikoThe llvm is able to generate .BTF and .BTF.ext directly with -g for bpf target
10009ab5305dSAndrii Nakryikoonly. The assembly code (-S) is able to show the BTF encoding in assembly
10019ab5305dSAndrii Nakryikoformat.::
1002ffcf7ce9SYonghong Song
1003ffcf7ce9SYonghong Song    -bash-4.4$ cat t2.c
1004ffcf7ce9SYonghong Song    typedef int __int32;
1005ffcf7ce9SYonghong Song    struct t2 {
1006ffcf7ce9SYonghong Song      int a2;
1007ffcf7ce9SYonghong Song      int (*f2)(char q1, __int32 q2, ...);
1008ffcf7ce9SYonghong Song      int (*f3)();
1009ffcf7ce9SYonghong Song    } g2;
1010ffcf7ce9SYonghong Song    int main() { return 0; }
1011ffcf7ce9SYonghong Song    int test() { return 0; }
1012bbaf1ff0SFangrui Song    -bash-4.4$ clang -c -g -O2 --target=bpf t2.c
1013ffcf7ce9SYonghong Song    -bash-4.4$ readelf -S t2.o
1014ffcf7ce9SYonghong Song      ......
1015ffcf7ce9SYonghong Song      [ 8] .BTF              PROGBITS         0000000000000000  00000247
1016ffcf7ce9SYonghong Song           000000000000016e  0000000000000000           0     0     1
1017ffcf7ce9SYonghong Song      [ 9] .BTF.ext          PROGBITS         0000000000000000  000003b5
1018ffcf7ce9SYonghong Song           0000000000000060  0000000000000000           0     0     1
1019ffcf7ce9SYonghong Song      [10] .rel.BTF.ext      REL              0000000000000000  000007e0
1020ffcf7ce9SYonghong Song           0000000000000040  0000000000000010          16     9     8
1021ffcf7ce9SYonghong Song      ......
1022bbaf1ff0SFangrui Song    -bash-4.4$ clang -S -g -O2 --target=bpf t2.c
1023ffcf7ce9SYonghong Song    -bash-4.4$ cat t2.s
1024ffcf7ce9SYonghong Song      ......
1025ffcf7ce9SYonghong Song            .section        .BTF,"",@progbits
1026ffcf7ce9SYonghong Song            .short  60319                   # 0xeb9f
1027ffcf7ce9SYonghong Song            .byte   1
1028ffcf7ce9SYonghong Song            .byte   0
1029ffcf7ce9SYonghong Song            .long   24
1030ffcf7ce9SYonghong Song            .long   0
1031ffcf7ce9SYonghong Song            .long   220
1032ffcf7ce9SYonghong Song            .long   220
1033ffcf7ce9SYonghong Song            .long   122
1034ffcf7ce9SYonghong Song            .long   0                       # BTF_KIND_FUNC_PROTO(id = 1)
1035ffcf7ce9SYonghong Song            .long   218103808               # 0xd000000
1036ffcf7ce9SYonghong Song            .long   2
1037ffcf7ce9SYonghong Song            .long   83                      # BTF_KIND_INT(id = 2)
1038ffcf7ce9SYonghong Song            .long   16777216                # 0x1000000
1039ffcf7ce9SYonghong Song            .long   4
1040ffcf7ce9SYonghong Song            .long   16777248                # 0x1000020
1041ffcf7ce9SYonghong Song      ......
1042ffcf7ce9SYonghong Song            .byte   0                       # string offset=0
1043ffcf7ce9SYonghong Song            .ascii  ".text"                 # string offset=1
1044ffcf7ce9SYonghong Song            .byte   0
1045ffcf7ce9SYonghong Song            .ascii  "/home/yhs/tmp-pahole/t2.c" # string offset=7
1046ffcf7ce9SYonghong Song            .byte   0
1047ffcf7ce9SYonghong Song            .ascii  "int main() { return 0; }" # string offset=33
1048ffcf7ce9SYonghong Song            .byte   0
1049ffcf7ce9SYonghong Song            .ascii  "int test() { return 0; }" # string offset=58
1050ffcf7ce9SYonghong Song            .byte   0
1051ffcf7ce9SYonghong Song            .ascii  "int"                   # string offset=83
1052ffcf7ce9SYonghong Song      ......
1053ffcf7ce9SYonghong Song            .section        .BTF.ext,"",@progbits
1054ffcf7ce9SYonghong Song            .short  60319                   # 0xeb9f
1055ffcf7ce9SYonghong Song            .byte   1
1056ffcf7ce9SYonghong Song            .byte   0
1057ffcf7ce9SYonghong Song            .long   24
1058ffcf7ce9SYonghong Song            .long   0
1059ffcf7ce9SYonghong Song            .long   28
1060ffcf7ce9SYonghong Song            .long   28
1061ffcf7ce9SYonghong Song            .long   44
1062ffcf7ce9SYonghong Song            .long   8                       # FuncInfo
1063ffcf7ce9SYonghong Song            .long   1                       # FuncInfo section string offset=1
1064ffcf7ce9SYonghong Song            .long   2
1065ffcf7ce9SYonghong Song            .long   .Lfunc_begin0
1066ffcf7ce9SYonghong Song            .long   3
1067ffcf7ce9SYonghong Song            .long   .Lfunc_begin1
1068ffcf7ce9SYonghong Song            .long   5
1069ffcf7ce9SYonghong Song            .long   16                      # LineInfo
1070ffcf7ce9SYonghong Song            .long   1                       # LineInfo section string offset=1
1071ffcf7ce9SYonghong Song            .long   2
1072ffcf7ce9SYonghong Song            .long   .Ltmp0
1073ffcf7ce9SYonghong Song            .long   7
1074ffcf7ce9SYonghong Song            .long   33
1075ffcf7ce9SYonghong Song            .long   7182                    # Line 7 Col 14
1076ffcf7ce9SYonghong Song            .long   .Ltmp3
1077ffcf7ce9SYonghong Song            .long   7
1078ffcf7ce9SYonghong Song            .long   58
1079ffcf7ce9SYonghong Song            .long   8206                    # Line 8 Col 14
1080ffcf7ce9SYonghong Song
1081ffcf7ce9SYonghong Song7. Testing
10823ff36bffSDave Tucker==========
1083ffcf7ce9SYonghong Song
1084b74344cbSRong TaoThe kernel BPF selftest `tools/testing/selftests/bpf/prog_tests/btf.c`_
1085b74344cbSRong Taoprovides an extensive set of BTF-related tests.
1086b74344cbSRong Tao
1087b74344cbSRong Tao.. Links
1088b74344cbSRong Tao.. _tools/testing/selftests/bpf/prog_tests/btf.c:
1089b74344cbSRong Tao   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/btf.c
1090