1 //===-- BTF.h --------------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file contains the layout of .BTF and .BTF.ext ELF sections.
12 /// The binary layout for .BTF section:
14 /// Type and Str subsections
15 /// The Type subsection is a collection of types with type id starting with 1.
16 /// The Str subsection is simply a collection of strings.
18 /// The binary layout for .BTF.ext section:
20 /// FuncInfo and LineInfo subsections
21 /// The FuncInfo subsection is defined as below:
23 /// struct SecFuncInfo for ELF section #1
24 /// A number of struct BPFFuncInfo for ELF section #1
25 /// struct SecFuncInfo for ELF section #2
26 /// A number of struct BPFFuncInfo for ELF section #2
28 /// The LineInfo subsection is defined as below:
30 /// struct SecLineInfo for ELF section #1
31 /// A number of struct BPFLineInfo for ELF section #1
32 /// struct SecLineInfo for ELF section #2
33 /// A number of struct BPFLineInfo for ELF section #2
36 /// The section formats are also defined at
37 /// https://github.com/torvalds/linux/blob/master/include/uapi/linux/btf.h
39 //===----------------------------------------------------------------------===//
41 #ifndef LLVM_LIB_TARGET_BPF_BTF_H
42 #define LLVM_LIB_TARGET_BPF_BTF_H
47 enum : uint32_t { MAGIC
= 0xeB9F, VERSION
= 1 };
49 /// Sizes in bytes of various things in the BTF format.
64 /// The .BTF section header definition.
66 uint16_t Magic
; ///< Magic value
67 uint8_t Version
; ///< Version number
68 uint8_t Flags
; ///< Extra flags
69 uint32_t HdrLen
; ///< Length of this header
71 /// All offsets are in bytes relative to the end of this header.
72 uint32_t TypeOff
; ///< Offset of type section
73 uint32_t TypeLen
; ///< Length of type section
74 uint32_t StrOff
; ///< Offset of string section
75 uint32_t StrLen
; ///< Length of string section
79 MAX_VLEN
= 0xffff ///< Max # of struct/union/enum members or func args
82 enum TypeKinds
: uint8_t {
83 #define HANDLE_BTF_KIND(ID, NAME) BTF_KIND_##NAME = ID,
87 /// The BTF common type definition. Different kinds may have
88 /// additional information after this structure data.
90 /// Type name offset in the string table.
93 /// "Info" bits arrangement:
94 /// Bits 0-15: vlen (e.g. # of struct's members)
95 /// Bits 16-23: unused
96 /// Bits 24-27: kind (e.g. int, ptr, array...etc)
97 /// Bits 28-30: unused
98 /// Bit 31: kind_flag, currently used by
99 /// struct, union and fwd
102 /// "Size" is used by INT, ENUM, STRUCT and UNION.
103 /// "Size" tells the size of the type it is describing.
105 /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
106 /// FUNC and FUNC_PROTO.
107 /// "Type" is a type_id referring to another type.
114 // For some specific BTF_KIND, "struct CommonType" is immediately
115 // followed by extra data.
117 // BTF_KIND_INT is followed by a u32 and the following
118 // is the 32 bits arrangement:
119 // BTF_INT_ENCODING(VAL) : (((VAL) & 0x0f000000) >> 24)
120 // BTF_INT_OFFSET(VAL) : (((VAL & 0x00ff0000)) >> 16)
121 // BTF_INT_BITS(VAL) : ((VAL) & 0x000000ff)
123 /// Attributes stored in the INT_ENCODING.
124 enum : uint8_t { INT_SIGNED
= (1 << 0), INT_CHAR
= (1 << 1), INT_BOOL
= (1 << 2) };
126 /// BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
127 /// The exact number of btf_enum is stored in the vlen (of the
128 /// info in "struct CommonType").
130 uint32_t NameOff
; ///< Enum name offset in the string table
131 int32_t Val
; ///< Enum member value
134 /// BTF_KIND_ARRAY is followed by one "struct BTFArray".
136 uint32_t ElemType
; ///< Element type
137 uint32_t IndexType
; ///< Index type
138 uint32_t Nelems
; ///< Number of elements for this array
141 /// BTF_KIND_STRUCT and BTF_KIND_UNION are followed
142 /// by multiple "struct BTFMember". The exact number
143 /// of BTFMember is stored in the vlen (of the info in
144 /// "struct CommonType").
146 /// If the struct/union contains any bitfield member,
147 /// the Offset below represents BitOffset (bits 0 - 23)
148 /// and BitFieldSize(bits 24 - 31) with BitFieldSize = 0
149 /// for non bitfield members. Otherwise, the Offset
150 /// represents the BitOffset.
152 uint32_t NameOff
; ///< Member name offset in the string table
153 uint32_t Type
; ///< Member type
154 uint32_t Offset
; ///< BitOffset or BitFieldSize+BitOffset
157 /// BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
158 /// The exist number of BTFParam is stored in the vlen (of the info
159 /// in "struct CommonType").
165 /// The .BTF.ext section header definition.
172 uint32_t FuncInfoOff
; ///< Offset of func info section
173 uint32_t FuncInfoLen
; ///< Length of func info section
174 uint32_t LineInfoOff
; ///< Offset of line info section
175 uint32_t LineInfoLen
; ///< Length of line info section
178 /// Specifying one function info.
180 uint32_t InsnOffset
; ///< Byte offset in the section
181 uint32_t TypeId
; ///< Type id referring to .BTF type section
184 /// Specifying function info's in one section.
186 uint32_t SecNameOff
; ///< Section name index in the .BTF string table
187 uint32_t NumFuncInfo
; ///< Number of func info's in this section
190 /// Specifying one line info.
192 uint32_t InsnOffset
; ///< Byte offset in this section
193 uint32_t FileNameOff
; ///< File name index in the .BTF string table
194 uint32_t LineOff
; ///< Line index in the .BTF string table
195 uint32_t LineCol
; ///< Line num: line_col >> 10,
196 /// col num: line_col & 0x3ff
199 /// Specifying line info's in one section.
201 uint32_t SecNameOff
; ///< Section name index in the .BTF string tble
202 uint32_t NumLineInfo
; ///< Number of line info's in this section
205 } // End namespace BTF.
206 } // End namespace llvm.