LLVM  15.0.0git
BTF.h
Go to the documentation of this file.
1 //===-- BTF.h --------------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the layout of .BTF and .BTF.ext ELF sections.
11 ///
12 /// The binary layout for .BTF section:
13 /// struct Header
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.
17 ///
18 /// The binary layout for .BTF.ext section:
19 /// struct ExtHeader
20 /// FuncInfo, LineInfo, FieldReloc and ExternReloc subsections
21 /// The FuncInfo subsection is defined as below:
22 /// BTFFuncInfo Size
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
27 /// ...
28 /// The LineInfo subsection is defined as below:
29 /// BPFLineInfo Size
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
34 /// ...
35 /// The FieldReloc subsection is defined as below:
36 /// BPFFieldReloc Size
37 /// struct SecFieldReloc for ELF section #1
38 /// A number of struct BPFFieldReloc for ELF section #1
39 /// struct SecFieldReloc for ELF section #2
40 /// A number of struct BPFFieldReloc for ELF section #2
41 /// ...
42 ///
43 /// The section formats are also defined at
44 /// https://github.com/torvalds/linux/blob/master/include/uapi/linux/btf.h
45 ///
46 //===----------------------------------------------------------------------===//
47 
48 #ifndef LLVM_LIB_TARGET_BPF_BTF_H
49 #define LLVM_LIB_TARGET_BPF_BTF_H
50 
51 namespace llvm {
52 namespace BTF {
53 
54 enum : uint32_t { MAGIC = 0xeB9F, VERSION = 1 };
55 
56 /// Sizes in bytes of various things in the BTF format.
57 enum {
58  HeaderSize = 24,
73 };
74 
75 /// The .BTF section header definition.
76 struct Header {
77  uint16_t Magic; ///< Magic value
78  uint8_t Version; ///< Version number
79  uint8_t Flags; ///< Extra flags
80  uint32_t HdrLen; ///< Length of this header
81 
82  /// All offsets are in bytes relative to the end of this header.
83  uint32_t TypeOff; ///< Offset of type section
84  uint32_t TypeLen; ///< Length of type section
85  uint32_t StrOff; ///< Offset of string section
86  uint32_t StrLen; ///< Length of string section
87 };
88 
89 enum : uint32_t {
90  MAX_VLEN = 0xffff ///< Max # of struct/union/enum members or func args
91 };
92 
93 enum TypeKinds : uint8_t {
94 #define HANDLE_BTF_KIND(ID, NAME) BTF_KIND_##NAME = ID,
95 #include "BTF.def"
96 };
97 
98 /// The BTF common type definition. Different kinds may have
99 /// additional information after this structure data.
100 struct CommonType {
101  /// Type name offset in the string table.
103 
104  /// "Info" bits arrangement:
105  /// Bits 0-15: vlen (e.g. # of struct's members)
106  /// Bits 16-23: unused
107  /// Bits 24-27: kind (e.g. int, ptr, array...etc)
108  /// Bits 28-30: unused
109  /// Bit 31: kind_flag, currently used by
110  /// struct, union and fwd
112 
113  /// "Size" is used by INT, ENUM, STRUCT and UNION.
114  /// "Size" tells the size of the type it is describing.
115  ///
116  /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
117  /// FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
118  /// "Type" is a type_id referring to another type.
119  union {
122  };
123 };
124 
125 // For some specific BTF_KIND, "struct CommonType" is immediately
126 // followed by extra data.
127 
128 // BTF_KIND_INT is followed by a u32 and the following
129 // is the 32 bits arrangement:
130 // BTF_INT_ENCODING(VAL) : (((VAL) & 0x0f000000) >> 24)
131 // BTF_INT_OFFSET(VAL) : (((VAL & 0x00ff0000)) >> 16)
132 // BTF_INT_BITS(VAL) : ((VAL) & 0x000000ff)
133 
134 /// Attributes stored in the INT_ENCODING.
135 enum : uint8_t {
136  INT_SIGNED = (1 << 0),
137  INT_CHAR = (1 << 1),
138  INT_BOOL = (1 << 2)
139 };
140 
141 /// BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
142 /// The exact number of btf_enum is stored in the vlen (of the
143 /// info in "struct CommonType").
144 struct BTFEnum {
145  uint32_t NameOff; ///< Enum name offset in the string table
146  int32_t Val; ///< Enum member value
147 };
148 
149 /// BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
150 /// The exact number of BTFEnum64 is stored in the vlen (of the
151 /// info in "struct CommonType").
152 struct BTFEnum64 {
153  uint32_t NameOff; ///< Enum name offset in the string table
154  uint32_t Val_Lo32; ///< Enum member lo32 value
155  uint32_t Val_Hi32; ///< Enum member hi32 value
156 };
157 
158 /// BTF_KIND_ARRAY is followed by one "struct BTFArray".
159 struct BTFArray {
160  uint32_t ElemType; ///< Element type
161  uint32_t IndexType; ///< Index type
162  uint32_t Nelems; ///< Number of elements for this array
163 };
164 
165 /// BTF_KIND_STRUCT and BTF_KIND_UNION are followed
166 /// by multiple "struct BTFMember". The exact number
167 /// of BTFMember is stored in the vlen (of the info in
168 /// "struct CommonType").
169 ///
170 /// If the struct/union contains any bitfield member,
171 /// the Offset below represents BitOffset (bits 0 - 23)
172 /// and BitFieldSize(bits 24 - 31) with BitFieldSize = 0
173 /// for non bitfield members. Otherwise, the Offset
174 /// represents the BitOffset.
175 struct BTFMember {
176  uint32_t NameOff; ///< Member name offset in the string table
177  uint32_t Type; ///< Member type
178  uint32_t Offset; ///< BitOffset or BitFieldSize+BitOffset
179 };
180 
181 /// BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
182 /// The exist number of BTFParam is stored in the vlen (of the info
183 /// in "struct CommonType").
184 struct BTFParam {
187 };
188 
189 /// BTF_KIND_FUNC can be global, static or extern.
190 enum : uint8_t {
194 };
195 
196 /// Variable scoping information.
197 enum : uint8_t {
198  VAR_STATIC = 0, ///< Linkage: InternalLinkage
199  VAR_GLOBAL_ALLOCATED = 1, ///< Linkage: ExternalLinkage
200  VAR_GLOBAL_EXTERNAL = 2, ///< Linkage: ExternalLinkage
201 };
202 
203 /// BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar".
204 /// The exist number of BTFDataSec is stored in the vlen (of the info
205 /// in "struct CommonType").
206 struct BTFDataSec {
207  uint32_t Type; ///< A BTF_KIND_VAR type
208  uint32_t Offset; ///< In-section offset
209  uint32_t Size; ///< Occupied memory size
210 };
211 
212 /// The .BTF.ext section header definition.
213 struct ExtHeader {
215  uint8_t Version;
216  uint8_t Flags;
218 
219  uint32_t FuncInfoOff; ///< Offset of func info section
220  uint32_t FuncInfoLen; ///< Length of func info section
221  uint32_t LineInfoOff; ///< Offset of line info section
222  uint32_t LineInfoLen; ///< Length of line info section
223  uint32_t FieldRelocOff; ///< Offset of offset reloc section
224  uint32_t FieldRelocLen; ///< Length of offset reloc section
225 };
226 
227 /// Specifying one function info.
228 struct BPFFuncInfo {
229  uint32_t InsnOffset; ///< Byte offset in the section
230  uint32_t TypeId; ///< Type id referring to .BTF type section
231 };
232 
233 /// Specifying function info's in one section.
234 struct SecFuncInfo {
235  uint32_t SecNameOff; ///< Section name index in the .BTF string table
236  uint32_t NumFuncInfo; ///< Number of func info's in this section
237 };
238 
239 /// Specifying one line info.
240 struct BPFLineInfo {
241  uint32_t InsnOffset; ///< Byte offset in this section
242  uint32_t FileNameOff; ///< File name index in the .BTF string table
243  uint32_t LineOff; ///< Line index in the .BTF string table
244  uint32_t LineCol; ///< Line num: line_col >> 10,
245  /// col num: line_col & 0x3ff
246 };
247 
248 /// Specifying line info's in one section.
249 struct SecLineInfo {
250  uint32_t SecNameOff; ///< Section name index in the .BTF string table
251  uint32_t NumLineInfo; ///< Number of line info's in this section
252 };
253 
254 /// Specifying one offset relocation.
256  uint32_t InsnOffset; ///< Byte offset in this section
257  uint32_t TypeID; ///< TypeID for the relocation
258  uint32_t OffsetNameOff; ///< The string to traverse types
259  uint32_t RelocKind; ///< What to patch the instruction
260 };
261 
262 /// Specifying offset relocation's in one section.
264  uint32_t SecNameOff; ///< Section name index in the .BTF string table
265  uint32_t NumFieldReloc; ///< Number of offset reloc's in this section
266 };
267 
268 } // End namespace BTF.
269 } // End namespace llvm.
270 
271 #endif
llvm::BTF::BTFMember
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
Definition: BTF.h:175
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::BTF::BTFDataSec::Type
uint32_t Type
A BTF_KIND_VAR type.
Definition: BTF.h:207
llvm::BTF::BPFFieldRelocSize
@ BPFFieldRelocSize
Definition: BTF.h:72
llvm::BTF::BTFEnum64::Val_Hi32
uint32_t Val_Hi32
Enum member hi32 value.
Definition: BTF.h:155
llvm::BTF::BTFEnum
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
Definition: BTF.h:144
llvm::BTF::BPFFieldReloc::TypeID
uint32_t TypeID
TypeID for the relocation.
Definition: BTF.h:257
llvm::BTF::HeaderSize
@ HeaderSize
Definition: BTF.h:58
llvm::BTF::FUNC_EXTERN
@ FUNC_EXTERN
Definition: BTF.h:193
llvm::BTF::VAR_STATIC
@ VAR_STATIC
Linkage: InternalLinkage.
Definition: BTF.h:198
llvm::BTF::Header::TypeLen
uint32_t TypeLen
Length of type section.
Definition: BTF.h:84
llvm::BTF::FUNC_GLOBAL
@ FUNC_GLOBAL
Definition: BTF.h:192
llvm::BTF::SecLineInfo
Specifying line info's in one section.
Definition: BTF.h:249
llvm::BTF::ExtHeader::Flags
uint8_t Flags
Definition: BTF.h:216
llvm::BTF::SecFieldReloc
Specifying offset relocation's in one section.
Definition: BTF.h:263
llvm::BTF::INT_CHAR
@ INT_CHAR
Definition: BTF.h:137
llvm::BTF::BTFMember::Type
uint32_t Type
Member type.
Definition: BTF.h:177
llvm::BTF::SecLineInfo::NumLineInfo
uint32_t NumLineInfo
Number of line info's in this section.
Definition: BTF.h:251
llvm::BTF::VAR_GLOBAL_EXTERNAL
@ VAR_GLOBAL_EXTERNAL
Linkage: ExternalLinkage.
Definition: BTF.h:200
llvm::BTF::SecFieldRelocSize
@ SecFieldRelocSize
Definition: BTF.h:69
llvm::BTF::BTFDataSec::Size
uint32_t Size
Occupied memory size.
Definition: BTF.h:209
llvm::BTF::ExtHeader::FuncInfoOff
uint32_t FuncInfoOff
Offset of func info section.
Definition: BTF.h:219
llvm::BTF::BTFArraySize
@ BTFArraySize
Definition: BTF.h:61
llvm::BTF::BPFLineInfoSize
@ BPFLineInfoSize
Definition: BTF.h:71
llvm::BTF::BTFParam::NameOff
uint32_t NameOff
Definition: BTF.h:185
llvm::BTF::SecFuncInfoSize
@ SecFuncInfoSize
Definition: BTF.h:67
llvm::BTF::BTFArray::Nelems
uint32_t Nelems
Number of elements for this array.
Definition: BTF.h:162
llvm::BTF::SecFuncInfo::SecNameOff
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition: BTF.h:235
llvm::BTF::SecLineInfo::SecNameOff
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition: BTF.h:250
llvm::BTF::ExtHeader::FieldRelocLen
uint32_t FieldRelocLen
Length of offset reloc section.
Definition: BTF.h:224
llvm::BTF::SecFuncInfo
Specifying function info's in one section.
Definition: BTF.h:234
llvm::BTF::Header::HdrLen
uint32_t HdrLen
Length of this header.
Definition: BTF.h:80
llvm::BTF::BPFLineInfo
Specifying one line info.
Definition: BTF.h:240
llvm::BTF::TypeKinds
TypeKinds
Definition: BTF.h:93
llvm::BTF::BPFFieldReloc::OffsetNameOff
uint32_t OffsetNameOff
The string to traverse types.
Definition: BTF.h:258
llvm::BTF::BTFMember::Offset
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
Definition: BTF.h:178
llvm::BTF::BPFFuncInfo::TypeId
uint32_t TypeId
Type id referring to .BTF type section.
Definition: BTF.h:230
llvm::BTF::ExtHeader::Magic
uint16_t Magic
Definition: BTF.h:214
llvm::BTF::BTFParamSize
@ BTFParamSize
Definition: BTF.h:65
llvm::BTF::Header::Flags
uint8_t Flags
Extra flags.
Definition: BTF.h:79
llvm::BTF::BPFFuncInfoSize
@ BPFFuncInfoSize
Definition: BTF.h:70
llvm::BTF::ExtHeader::FuncInfoLen
uint32_t FuncInfoLen
Length of func info section.
Definition: BTF.h:220
llvm::BTF::BPFLineInfo::FileNameOff
uint32_t FileNameOff
File name index in the .BTF string table.
Definition: BTF.h:242
llvm::BTF::BTFMember::NameOff
uint32_t NameOff
Member name offset in the string table.
Definition: BTF.h:176
llvm::BTF::CommonTypeSize
@ CommonTypeSize
Definition: BTF.h:60
llvm::BTF::ExtHeader::HdrLen
uint32_t HdrLen
Definition: BTF.h:217
llvm::BTF::Header::Magic
uint16_t Magic
Magic value.
Definition: BTF.h:77
llvm::BTF::VAR_GLOBAL_ALLOCATED
@ VAR_GLOBAL_ALLOCATED
Linkage: ExternalLinkage.
Definition: BTF.h:199
llvm::BTF::MAX_VLEN
@ MAX_VLEN
Max # of struct/union/enum members or func args.
Definition: BTF.h:90
llvm::BTF::SecFieldReloc::NumFieldReloc
uint32_t NumFieldReloc
Number of offset reloc's in this section.
Definition: BTF.h:265
llvm::BTF::ExtHeader::Version
uint8_t Version
Definition: BTF.h:215
llvm::BTF::ExtHeader::LineInfoOff
uint32_t LineInfoOff
Offset of line info section.
Definition: BTF.h:221
llvm::BTF::BTFEnum64Size
@ BTFEnum64Size
Definition: BTF.h:63
llvm::BTF::BTFParam
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
Definition: BTF.h:184
llvm::BTF::BTFArray::IndexType
uint32_t IndexType
Index type.
Definition: BTF.h:161
llvm::BTF::BPFLineInfo::LineOff
uint32_t LineOff
Line index in the .BTF string table.
Definition: BTF.h:243
llvm::BTF::INT_SIGNED
@ INT_SIGNED
Definition: BTF.h:136
llvm::BTF::BTFParam::Type
uint32_t Type
Definition: BTF.h:186
llvm::BTF::MAGIC
@ MAGIC
Definition: BTF.h:54
llvm::BTF::BPFFieldReloc::InsnOffset
uint32_t InsnOffset
Byte offset in this section.
Definition: BTF.h:256
llvm::BTF::CommonType
The BTF common type definition.
Definition: BTF.h:100
llvm::BTF::ExtHeader::FieldRelocOff
uint32_t FieldRelocOff
Offset of offset reloc section.
Definition: BTF.h:223
llvm::BTF::BTFEnum64::Val_Lo32
uint32_t Val_Lo32
Enum member lo32 value.
Definition: BTF.h:154
llvm::BTF::ExtHeader
The .BTF.ext section header definition.
Definition: BTF.h:213
llvm::BTF::BPFFuncInfo::InsnOffset
uint32_t InsnOffset
Byte offset in the section.
Definition: BTF.h:229
llvm::BTF::Header::StrOff
uint32_t StrOff
Offset of string section.
Definition: BTF.h:85
uint32_t
llvm::BTF::BTFEnum64
BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
Definition: BTF.h:152
llvm::BTF::BPFFieldReloc
Specifying one offset relocation.
Definition: BTF.h:255
llvm::BTF::BTFDataSec
BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar".
Definition: BTF.h:206
llvm::BTF::BTFEnum::Val
int32_t Val
Enum member value.
Definition: BTF.h:146
llvm::BTF::BTFArray::ElemType
uint32_t ElemType
Element type.
Definition: BTF.h:160
llvm::BTF::BTFEnum::NameOff
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:145
llvm::BTF::BPFFieldReloc::RelocKind
uint32_t RelocKind
What to patch the instruction.
Definition: BTF.h:259
llvm::BTF::CommonType::Info
uint32_t Info
"Info" bits arrangement: Bits 0-15: vlen (e.g.
Definition: BTF.h:111
llvm::BTF::BTFDataSec::Offset
uint32_t Offset
In-section offset.
Definition: BTF.h:208
llvm::BTF::BTFEnum64::NameOff
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:153
uint16_t
llvm::BTF::Header::TypeOff
uint32_t TypeOff
All offsets are in bytes relative to the end of this header.
Definition: BTF.h:83
llvm::BTF::Header::Version
uint8_t Version
Version number.
Definition: BTF.h:78
llvm::BTF::VERSION
@ VERSION
Definition: BTF.h:54
llvm::BTF::BPFFuncInfo
Specifying one function info.
Definition: BTF.h:228
llvm::BTF::ExtHeaderSize
@ ExtHeaderSize
Definition: BTF.h:59
llvm::BTF::Header::StrLen
uint32_t StrLen
Length of string section.
Definition: BTF.h:86
llvm::BTF::BTFDataSecVarSize
@ BTFDataSecVarSize
Definition: BTF.h:66
llvm::BTF::Header
The .BTF section header definition.
Definition: BTF.h:76
llvm::BTF::SecLineInfoSize
@ SecLineInfoSize
Definition: BTF.h:68
llvm::BTF::FUNC_STATIC
@ FUNC_STATIC
Definition: BTF.h:191
llvm::BTF::SecFuncInfo::NumFuncInfo
uint32_t NumFuncInfo
Number of func info's in this section.
Definition: BTF.h:236
llvm::BTF::INT_BOOL
@ INT_BOOL
Definition: BTF.h:138
llvm::BTF::CommonType::NameOff
uint32_t NameOff
Type name offset in the string table.
Definition: BTF.h:102
llvm::BTF::BTFEnumSize
@ BTFEnumSize
Definition: BTF.h:62
llvm::BTF::SecFieldReloc::SecNameOff
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition: BTF.h:264
llvm::BTF::CommonType::Type
uint32_t Type
Definition: BTF.h:121
llvm::BTF::BPFLineInfo::InsnOffset
uint32_t InsnOffset
Byte offset in this section.
Definition: BTF.h:241
llvm::BTF::ExtHeader::LineInfoLen
uint32_t LineInfoLen
Length of line info section.
Definition: BTF.h:222
llvm::BTF::BTFMemberSize
@ BTFMemberSize
Definition: BTF.h:64
llvm::BTF::CommonType::Size
uint32_t Size
Definition: BTF.h:120
llvm::BTF::BPFLineInfo::LineCol
uint32_t LineCol
Line num: line_col >> 10, col num: line_col & 0x3ff.
Definition: BTF.h:244
llvm::BTF::BTFArray
BTF_KIND_ARRAY is followed by one "struct BTFArray".
Definition: BTF.h:159