LLVM 24.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#include "llvm/ADT/ArrayRef.h"
53
54namespace llvm {
55namespace BTF {
56
57enum : uint32_t { MAGIC = 0xeB9F, VERSION = 1 };
58
59/// Sizes in bytes of various things in the BTF format.
60enum {
76};
77
78/// The .BTF section header definition.
79struct Header {
80 uint16_t Magic; ///< Magic value
81 uint8_t Version; ///< Version number
82 uint8_t Flags; ///< Extra flags
83 uint32_t HdrLen; ///< Length of this header
84
85 /// All offsets are in bytes relative to the end of this header.
86 uint32_t TypeOff; ///< Offset of type section
87 uint32_t TypeLen; ///< Length of type section
88 uint32_t StrOff; ///< Offset of string section
89 uint32_t StrLen; ///< Length of string section
90};
91
92enum : uint32_t {
93 MAX_VLEN = 0xffffff ///< Max # of struct/union/enum members or func args
94};
95
97#define HANDLE_BTF_KIND(ID, NAME) BTF_KIND_##NAME = ID,
98#include "BTF.def"
99};
100
101// Constants for CommonType::Info field.
102constexpr uint32_t FWD_UNION_FLAG = 1u << 31;
103constexpr uint32_t ENUM_SIGNED_FLAG = 1u << 31;
104
105/// The BTF common type definition. Different kinds may have
106/// additional information after this structure data.
108 /// Type name offset in the string table.
110
111 /// "Info" bits arrangement:
112 /// Bits 0-23: vlen (e.g. # of struct's members)
113 /// Bits 24-30: kind (e.g. int, ptr, array...etc)
114 /// Bit 31: kind_flag, currently used by
115 /// struct, union, enum, fwd, enum64,
116 /// decl_tag and type_tag
118
119 /// "Size" is used by INT, ENUM, STRUCT and UNION.
120 /// "Size" tells the size of the type it is describing.
121 ///
122 /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
123 /// FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
124 /// "Type" is a type_id referring to another type.
125 union {
128 };
129
130 uint32_t getKind() const { return Info >> 24 & 0x7f; }
131 uint32_t getVlen() const { return Info & 0xffffff; }
132};
133
134// For some specific BTF_KIND, "struct CommonType" is immediately
135// followed by extra data.
136
137// BTF_KIND_INT is followed by a u32 and the following
138// is the 32 bits arrangement:
139// BTF_INT_ENCODING(VAL) : (((VAL) & 0x0f000000) >> 24)
140// BTF_INT_OFFSET(VAL) : (((VAL & 0x00ff0000)) >> 16)
141// BTF_INT_BITS(VAL) : ((VAL) & 0x000000ff)
142
143/// Attributes stored in the INT_ENCODING.
144enum : uint8_t {
145 INT_SIGNED = (1 << 0),
146 INT_CHAR = (1 << 1),
147 INT_BOOL = (1 << 2)
148};
149
150/// BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
151/// The exact number of btf_enum is stored in the vlen (of the
152/// info in "struct CommonType").
153struct BTFEnum {
154 uint32_t NameOff; ///< Enum name offset in the string table
155 int32_t Val; ///< Enum member value
156};
157
158/// BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
159/// The exact number of BTFEnum64 is stored in the vlen (of the
160/// info in "struct CommonType").
161struct BTFEnum64 {
162 uint32_t NameOff; ///< Enum name offset in the string table
163 uint32_t Val_Lo32; ///< Enum member lo32 value
164 uint32_t Val_Hi32; ///< Enum member hi32 value
165};
166
167/// BTF_KIND_ARRAY is followed by one "struct BTFArray".
168struct BTFArray {
169 uint32_t ElemType; ///< Element type
170 uint32_t IndexType; ///< Index type
171 uint32_t Nelems; ///< Number of elements for this array
172};
173
174/// BTF_KIND_STRUCT and BTF_KIND_UNION are followed
175/// by multiple "struct BTFMember". The exact number
176/// of BTFMember is stored in the vlen (of the info in
177/// "struct CommonType").
178///
179/// If the struct/union contains any bitfield member,
180/// the Offset below represents BitOffset (bits 0 - 23)
181/// and BitFieldSize(bits 24 - 31) with BitFieldSize = 0
182/// for non bitfield members. Otherwise, the Offset
183/// represents the BitOffset.
184struct BTFMember {
185 uint32_t NameOff; ///< Member name offset in the string table
186 uint32_t Type; ///< Member type
187 uint32_t Offset; ///< BitOffset or BitFieldSize+BitOffset
188};
189
190/// BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
191/// The exist number of BTFParam is stored in the vlen (of the info
192/// in "struct CommonType").
197
198/// BTF_KIND_FUNC can be global, static or extern.
199enum : uint8_t {
203};
204
205/// Variable scoping information.
206enum : uint8_t {
207 VAR_STATIC = 0, ///< Linkage: InternalLinkage
208 VAR_GLOBAL_ALLOCATED = 1, ///< Linkage: ExternalLinkage
209 VAR_GLOBAL_EXTERNAL = 2, ///< Linkage: ExternalLinkage
210};
211
212/// BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar".
213/// The exist number of BTFDataSec is stored in the vlen (of the info
214/// in "struct CommonType").
216 uint32_t Type; ///< A BTF_KIND_VAR type
217 uint32_t Offset; ///< In-section offset
218 uint32_t Size; ///< Occupied memory size
219};
220
221/// The .BTF.ext section header definition.
222struct ExtHeader {
227
228 uint32_t FuncInfoOff; ///< Offset of func info section
229 uint32_t FuncInfoLen; ///< Length of func info section
230 uint32_t LineInfoOff; ///< Offset of line info section
231 uint32_t LineInfoLen; ///< Length of line info section
232 uint32_t FieldRelocOff; ///< Offset of offset reloc section
233 uint32_t FieldRelocLen; ///< Length of offset reloc section
234};
235
236/// Specifying one function info.
238 uint32_t InsnOffset; ///< Byte offset in the section
239 uint32_t TypeId; ///< Type id referring to .BTF type section
240};
241
242/// Specifying function info's in one section.
244 uint32_t SecNameOff; ///< Section name index in the .BTF string table
245 uint32_t NumFuncInfo; ///< Number of func info's in this section
246};
247
248/// Specifying one line info.
250 uint32_t InsnOffset; ///< Byte offset in this section
251 uint32_t FileNameOff; ///< File name index in the .BTF string table
252 uint32_t LineOff; ///< Line index in the .BTF string table
253 uint32_t LineCol; ///< Line num: line_col >> 10,
254 /// col num: line_col & 0x3ff
255 uint32_t getLine() const { return LineCol >> 10; }
256 uint32_t getCol() const { return LineCol & 0x3ff; }
257};
258
259/// Specifying line info's in one section.
261 uint32_t SecNameOff; ///< Section name index in the .BTF string table
262 uint32_t NumLineInfo; ///< Number of line info's in this section
263};
264
265/// Specifying one offset relocation.
267 uint32_t InsnOffset; ///< Byte offset in this section
268 uint32_t TypeID; ///< TypeID for the relocation
269 uint32_t OffsetNameOff; ///< The string to traverse types
270 uint32_t RelocKind; ///< What to patch the instruction
271};
272
273/// Specifying offset relocation's in one section.
275 uint32_t SecNameOff; ///< Section name index in the .BTF string table
276 uint32_t NumFieldReloc; ///< Number of offset reloc's in this section
277};
278
279/// CO-RE relocation kind codes used in .BTF.ext section.
296
297// Define a number of sub-types for CommonType, each with:
298// - An accessor for a relevant "tail" information (data fields that
299// follow the CommonType record in binary format).
300// - A classof() definition based on CommonType::getKind() value to
301// allow use with dyn_cast<>() function.
302
303// For CommonType sub-types that are followed by a single entry of
304// some type in the binary format.
305#define BTF_DEFINE_TAIL(Type, Accessor) \
306 const Type &Accessor() const { return *getTrailingObjects(); }
307
308// For CommonType sub-types that are followed by CommonType::getVlen()
309// number of entries of some type in the binary format.
310#define BTF_DEFINE_TAIL_ARR(Type, Accessor) \
311 ArrayRef<Type> Accessor() const { return getTrailingObjects(getVlen()); }
312
313struct ArrayType final : CommonType,
314 private TrailingObjects<ArrayType, BTFArray> {
317
318 static bool classof(const CommonType *V) {
319 return V->getKind() == BTF_KIND_ARRAY;
320 }
321};
322
323struct StructType final : CommonType,
324 private TrailingObjects<StructType, BTFMember> {
327
328 static bool classof(const CommonType *V) {
329 return V->getKind() == BTF_KIND_STRUCT || V->getKind() == BTF_KIND_UNION;
330 }
331};
332
333struct EnumType final : CommonType, private TrailingObjects<EnumType, BTFEnum> {
336
337 static bool classof(const CommonType *V) {
338 return V->getKind() == BTF_KIND_ENUM;
339 }
340};
341
342struct Enum64Type final : CommonType,
343 private TrailingObjects<Enum64Type, BTFEnum64> {
346
347 static bool classof(const CommonType *V) {
348 return V->getKind() == BTF_KIND_ENUM64;
349 }
350};
351
352#undef BTF_DEFINE_TAIL
353#undef BTF_DEFINE_TAIL_ARR
354
355} // End namespace BTF.
356} // End namespace llvm.
357
358#endif
aarch64 promote const
#define BTF_DEFINE_TAIL(Type, Accessor)
Definition BTF.h:305
#define BTF_DEFINE_TAIL_ARR(Type, Accessor)
Definition BTF.h:310
static std::array< T, N > getArray(const MachOObjectFile &O, const void *Ptr)
This header defines support for implementing classes that have some trailing object (or arrays of obj...
TypeKinds
Definition BTF.h:96
@ INT_CHAR
Definition BTF.h:146
@ INT_SIGNED
Definition BTF.h:145
@ INT_BOOL
Definition BTF.h:147
constexpr uint32_t ENUM_SIGNED_FLAG
Definition BTF.h:103
@ VAR_GLOBAL_ALLOCATED
Linkage: ExternalLinkage.
Definition BTF.h:208
@ VAR_STATIC
Linkage: InternalLinkage.
Definition BTF.h:207
@ VAR_GLOBAL_EXTERNAL
Linkage: ExternalLinkage.
Definition BTF.h:209
@ VERSION
Definition BTF.h:57
@ MAGIC
Definition BTF.h:57
@ BTFEnum64Size
Definition BTF.h:66
@ BTFArraySize
Definition BTF.h:64
@ BPFFuncInfoSize
Definition BTF.h:73
@ BTFMemberSize
Definition BTF.h:67
@ BTFEnumSize
Definition BTF.h:65
@ HeaderSize
Definition BTF.h:61
@ ExtHeaderSize
Definition BTF.h:62
@ SecLineInfoSize
Definition BTF.h:71
@ SecFieldRelocSize
Definition BTF.h:72
@ BPFLineInfoSize
Definition BTF.h:74
@ SecFuncInfoSize
Definition BTF.h:70
@ BTFParamSize
Definition BTF.h:68
@ BPFFieldRelocSize
Definition BTF.h:75
@ CommonTypeSize
Definition BTF.h:63
@ BTFDataSecVarSize
Definition BTF.h:69
@ MAX_VLEN
Max # of struct/union/enum members or func args.
Definition BTF.h:93
constexpr uint32_t FWD_UNION_FLAG
Definition BTF.h:102
PatchableRelocKind
CO-RE relocation kind codes used in .BTF.ext section.
Definition BTF.h:280
@ FIELD_RSHIFT_U64
Definition BTF.h:286
@ ENUM_VALUE
Definition BTF.h:292
@ FIELD_SIGNEDNESS
Definition BTF.h:284
@ FIELD_BYTE_OFFSET
Definition BTF.h:281
@ FIELD_BYTE_SIZE
Definition BTF.h:282
@ ENUM_VALUE_EXISTENCE
Definition BTF.h:291
@ MAX_FIELD_RELOC_KIND
Definition BTF.h:294
@ BTF_TYPE_ID_REMOTE
Definition BTF.h:288
@ TYPE_EXISTENCE
Definition BTF.h:289
@ BTF_TYPE_ID_LOCAL
Definition BTF.h:287
@ FIELD_LSHIFT_U64
Definition BTF.h:285
@ TYPE_MATCH
Definition BTF.h:293
@ TYPE_SIZE
Definition BTF.h:290
@ FIELD_EXISTENCE
Definition BTF.h:283
@ FUNC_STATIC
Definition BTF.h:200
@ FUNC_EXTERN
Definition BTF.h:202
@ FUNC_GLOBAL
Definition BTF.h:201
This is an optimization pass for GlobalISel generic memory operations.
static bool classof(const CommonType *V)
Definition BTF.h:318
friend TrailingObjects
Definition BTF.h:315
Specifying one offset relocation.
Definition BTF.h:266
uint32_t InsnOffset
Byte offset in this section.
Definition BTF.h:267
uint32_t OffsetNameOff
The string to traverse types.
Definition BTF.h:269
uint32_t RelocKind
What to patch the instruction.
Definition BTF.h:270
uint32_t TypeID
TypeID for the relocation.
Definition BTF.h:268
Specifying one function info.
Definition BTF.h:237
uint32_t InsnOffset
Byte offset in the section.
Definition BTF.h:238
uint32_t TypeId
Type id referring to .BTF type section.
Definition BTF.h:239
Specifying one line info.
Definition BTF.h:249
uint32_t getLine() const
Definition BTF.h:255
uint32_t LineCol
Line num: line_col >> 10, col num: line_col & 0x3ff.
Definition BTF.h:253
uint32_t getCol() const
Definition BTF.h:256
uint32_t FileNameOff
File name index in the .BTF string table.
Definition BTF.h:251
uint32_t InsnOffset
Byte offset in this section.
Definition BTF.h:250
uint32_t LineOff
Line index in the .BTF string table.
Definition BTF.h:252
BTF_KIND_ARRAY is followed by one "struct BTFArray".
Definition BTF.h:168
uint32_t Nelems
Number of elements for this array.
Definition BTF.h:171
uint32_t IndexType
Index type.
Definition BTF.h:170
uint32_t ElemType
Element type.
Definition BTF.h:169
BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar".
Definition BTF.h:215
uint32_t Size
Occupied memory size.
Definition BTF.h:218
uint32_t Type
A BTF_KIND_VAR type.
Definition BTF.h:216
uint32_t Offset
In-section offset.
Definition BTF.h:217
BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
Definition BTF.h:161
uint32_t NameOff
Enum name offset in the string table.
Definition BTF.h:162
uint32_t Val_Hi32
Enum member hi32 value.
Definition BTF.h:164
uint32_t Val_Lo32
Enum member lo32 value.
Definition BTF.h:163
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
Definition BTF.h:153
int32_t Val
Enum member value.
Definition BTF.h:155
uint32_t NameOff
Enum name offset in the string table.
Definition BTF.h:154
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
Definition BTF.h:184
uint32_t NameOff
Member name offset in the string table.
Definition BTF.h:185
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
Definition BTF.h:187
uint32_t Type
Member type.
Definition BTF.h:186
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
Definition BTF.h:193
uint32_t Type
Definition BTF.h:195
uint32_t NameOff
Definition BTF.h:194
The BTF common type definition.
Definition BTF.h:107
uint32_t Type
Definition BTF.h:127
uint32_t Size
Definition BTF.h:126
uint32_t NameOff
Type name offset in the string table.
Definition BTF.h:109
uint32_t Info
"Info" bits arrangement: Bits 0-23: vlen (e.g.
Definition BTF.h:117
uint32_t getKind() const
Definition BTF.h:130
uint32_t getVlen() const
Definition BTF.h:131
static bool classof(const CommonType *V)
Definition BTF.h:347
friend TrailingObjects
Definition BTF.h:344
static bool classof(const CommonType *V)
Definition BTF.h:337
friend TrailingObjects
Definition BTF.h:334
The .BTF.ext section header definition.
Definition BTF.h:222
uint32_t FieldRelocLen
Length of offset reloc section.
Definition BTF.h:233
uint32_t LineInfoLen
Length of line info section.
Definition BTF.h:231
uint8_t Flags
Definition BTF.h:225
uint8_t Version
Definition BTF.h:224
uint16_t Magic
Definition BTF.h:223
uint32_t FuncInfoOff
Offset of func info section.
Definition BTF.h:228
uint32_t FuncInfoLen
Length of func info section.
Definition BTF.h:229
uint32_t FieldRelocOff
Offset of offset reloc section.
Definition BTF.h:232
uint32_t LineInfoOff
Offset of line info section.
Definition BTF.h:230
uint32_t HdrLen
Definition BTF.h:226
The .BTF section header definition.
Definition BTF.h:79
uint32_t TypeOff
All offsets are in bytes relative to the end of this header.
Definition BTF.h:86
uint32_t StrOff
Offset of string section.
Definition BTF.h:88
uint32_t TypeLen
Length of type section.
Definition BTF.h:87
uint8_t Flags
Extra flags.
Definition BTF.h:82
uint8_t Version
Version number.
Definition BTF.h:81
uint32_t HdrLen
Length of this header.
Definition BTF.h:83
uint16_t Magic
Magic value.
Definition BTF.h:80
uint32_t StrLen
Length of string section.
Definition BTF.h:89
Specifying offset relocation's in one section.
Definition BTF.h:274
uint32_t NumFieldReloc
Number of offset reloc's in this section.
Definition BTF.h:276
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition BTF.h:275
Specifying function info's in one section.
Definition BTF.h:243
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition BTF.h:244
uint32_t NumFuncInfo
Number of func info's in this section.
Definition BTF.h:245
Specifying line info's in one section.
Definition BTF.h:260
uint32_t NumLineInfo
Number of line info's in this section.
Definition BTF.h:262
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition BTF.h:261
friend TrailingObjects
Definition BTF.h:325
static bool classof(const CommonType *V)
Definition BTF.h:328