LLVM 20.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 = 0xffff ///< Max # of struct/union/enum members or func args
94};
95
96enum TypeKinds : uint8_t {
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-15: vlen (e.g. # of struct's members)
113 /// Bits 16-23: unused
114 /// Bits 24-28: kind (e.g. int, ptr, array...etc)
115 /// Bits 29-30: unused
116 /// Bit 31: kind_flag, currently used by
117 /// struct, union and fwd
119
120 /// "Size" is used by INT, ENUM, STRUCT and UNION.
121 /// "Size" tells the size of the type it is describing.
122 ///
123 /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
124 /// FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
125 /// "Type" is a type_id referring to another type.
126 union {
129 };
130
131 uint32_t getKind() const { return Info >> 24 & 0x1f; }
132 uint32_t getVlen() const { return Info & 0xffff; }
133};
134
135// For some specific BTF_KIND, "struct CommonType" is immediately
136// followed by extra data.
137
138// BTF_KIND_INT is followed by a u32 and the following
139// is the 32 bits arrangement:
140// BTF_INT_ENCODING(VAL) : (((VAL) & 0x0f000000) >> 24)
141// BTF_INT_OFFSET(VAL) : (((VAL & 0x00ff0000)) >> 16)
142// BTF_INT_BITS(VAL) : ((VAL) & 0x000000ff)
143
144/// Attributes stored in the INT_ENCODING.
145enum : uint8_t {
146 INT_SIGNED = (1 << 0),
147 INT_CHAR = (1 << 1),
148 INT_BOOL = (1 << 2)
150
151/// BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
152/// The exact number of btf_enum is stored in the vlen (of the
153/// info in "struct CommonType").
154struct BTFEnum {
155 uint32_t NameOff; ///< Enum name offset in the string table
156 int32_t Val; ///< Enum member value
157};
158
159/// BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
160/// The exact number of BTFEnum64 is stored in the vlen (of the
161/// info in "struct CommonType").
162struct BTFEnum64 {
163 uint32_t NameOff; ///< Enum name offset in the string table
164 uint32_t Val_Lo32; ///< Enum member lo32 value
165 uint32_t Val_Hi32; ///< Enum member hi32 value
166};
167
168/// BTF_KIND_ARRAY is followed by one "struct BTFArray".
169struct BTFArray {
170 uint32_t ElemType; ///< Element type
171 uint32_t IndexType; ///< Index type
172 uint32_t Nelems; ///< Number of elements for this array
173};
174
175/// BTF_KIND_STRUCT and BTF_KIND_UNION are followed
176/// by multiple "struct BTFMember". The exact number
177/// of BTFMember is stored in the vlen (of the info in
178/// "struct CommonType").
179///
180/// If the struct/union contains any bitfield member,
181/// the Offset below represents BitOffset (bits 0 - 23)
182/// and BitFieldSize(bits 24 - 31) with BitFieldSize = 0
183/// for non bitfield members. Otherwise, the Offset
184/// represents the BitOffset.
185struct BTFMember {
186 uint32_t NameOff; ///< Member name offset in the string table
187 uint32_t Type; ///< Member type
188 uint32_t Offset; ///< BitOffset or BitFieldSize+BitOffset
189};
190
191/// BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
192/// The exist number of BTFParam is stored in the vlen (of the info
193/// in "struct CommonType").
194struct BTFParam {
197};
198
199/// BTF_KIND_FUNC can be global, static or extern.
200enum : uint8_t {
204};
205
206/// Variable scoping information.
207enum : uint8_t {
208 VAR_STATIC = 0, ///< Linkage: InternalLinkage
209 VAR_GLOBAL_ALLOCATED = 1, ///< Linkage: ExternalLinkage
210 VAR_GLOBAL_EXTERNAL = 2, ///< Linkage: ExternalLinkage
211};
212
213/// BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar".
214/// The exist number of BTFDataSec is stored in the vlen (of the info
215/// in "struct CommonType").
217 uint32_t Type; ///< A BTF_KIND_VAR type
218 uint32_t Offset; ///< In-section offset
219 uint32_t Size; ///< Occupied memory size
220};
221
222/// The .BTF.ext section header definition.
223struct ExtHeader {
225 uint8_t Version;
226 uint8_t Flags;
228
229 uint32_t FuncInfoOff; ///< Offset of func info section
230 uint32_t FuncInfoLen; ///< Length of func info section
231 uint32_t LineInfoOff; ///< Offset of line info section
232 uint32_t LineInfoLen; ///< Length of line info section
233 uint32_t FieldRelocOff; ///< Offset of offset reloc section
234 uint32_t FieldRelocLen; ///< Length of offset reloc section
235};
236
237/// Specifying one function info.
239 uint32_t InsnOffset; ///< Byte offset in the section
240 uint32_t TypeId; ///< Type id referring to .BTF type section
241};
242
243/// Specifying function info's in one section.
245 uint32_t SecNameOff; ///< Section name index in the .BTF string table
246 uint32_t NumFuncInfo; ///< Number of func info's in this section
247};
248
249/// Specifying one line info.
251 uint32_t InsnOffset; ///< Byte offset in this section
252 uint32_t FileNameOff; ///< File name index in the .BTF string table
253 uint32_t LineOff; ///< Line index in the .BTF string table
254 uint32_t LineCol; ///< Line num: line_col >> 10,
255 /// col num: line_col & 0x3ff
256 uint32_t getLine() const { return LineCol >> 10; }
257 uint32_t getCol() const { return LineCol & 0x3ff; }
258};
259
260/// Specifying line info's in one section.
262 uint32_t SecNameOff; ///< Section name index in the .BTF string table
263 uint32_t NumLineInfo; ///< Number of line info's in this section
264};
265
266/// Specifying one offset relocation.
268 uint32_t InsnOffset; ///< Byte offset in this section
269 uint32_t TypeID; ///< TypeID for the relocation
270 uint32_t OffsetNameOff; ///< The string to traverse types
271 uint32_t RelocKind; ///< What to patch the instruction
272};
273
274/// Specifying offset relocation's in one section.
276 uint32_t SecNameOff; ///< Section name index in the .BTF string table
277 uint32_t NumFieldReloc; ///< Number of offset reloc's in this section
278};
279
280/// CO-RE relocation kind codes used in .BTF.ext section.
296};
297
298// Define a number of sub-types for CommonType, each with:
299// - An accessor for a relevant "tail" information (data fields that
300// follow the CommonType record in binary format).
301// - A classof() definition based on CommonType::getKind() value to
302// allow use with dyn_cast<>() function.
303
304// For CommonType sub-types that are followed by a single entry of
305// some type in the binary format.
306#define BTF_DEFINE_TAIL(Type, Accessor) \
307 const Type &Accessor() const { return *getTrailingObjects<Type>(); }
308
309// For CommonType sub-types that are followed by CommonType::getVlen()
310// number of entries of some type in the binary format.
311#define BTF_DEFINE_TAIL_ARR(Type, Accessor) \
312 ArrayRef<Type> Accessor() const { \
313 return ArrayRef<Type>(getTrailingObjects<Type>(), getVlen()); \
314 }
315
316struct ArrayType final : CommonType,
317 private TrailingObjects<ArrayType, BTFArray> {
320
321 static bool classof(const CommonType *V) {
322 return V->getKind() == BTF_KIND_ARRAY;
323 }
324};
325
326struct StructType final : CommonType,
327 private TrailingObjects<StructType, BTFMember> {
330
331 static bool classof(const CommonType *V) {
332 return V->getKind() == BTF_KIND_STRUCT || V->getKind() == BTF_KIND_UNION;
333 }
334};
335
336struct EnumType final : CommonType, private TrailingObjects<EnumType, BTFEnum> {
339
340 static bool classof(const CommonType *V) {
341 return V->getKind() == BTF_KIND_ENUM;
342 }
343};
344
345struct Enum64Type final : CommonType,
346 private TrailingObjects<Enum64Type, BTFEnum64> {
349
350 static bool classof(const CommonType *V) {
351 return V->getKind() == BTF_KIND_ENUM64;
352 }
353};
354
355#undef BTF_DEFINE_TAIL
356#undef BTF_DEFINE_TAIL_ARR
357
358} // End namespace BTF.
359} // End namespace llvm.
360
361#endif
aarch64 promote const
#define BTF_DEFINE_TAIL(Type, Accessor)
Definition: BTF.h:306
#define BTF_DEFINE_TAIL_ARR(Type, Accessor)
Definition: BTF.h:311
Mark the given Function as meaning that it cannot be changed in any way mark any values that are used as this function s parameters or by its return values(according to Uses) live as well. void DeadArgumentEliminationPass
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...
See the file comment for details on the usage of the TrailingObjects type.
@ 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
TypeKinds
Definition: BTF.h:96
@ VERSION
Definition: BTF.h:57
@ MAGIC
Definition: BTF.h:57
constexpr uint32_t ENUM_SIGNED_FLAG
Definition: BTF.h:103
@ VAR_GLOBAL_ALLOCATED
Linkage: ExternalLinkage.
Definition: BTF.h:209
@ VAR_STATIC
Linkage: InternalLinkage.
Definition: BTF.h:208
@ VAR_GLOBAL_EXTERNAL
Linkage: ExternalLinkage.
Definition: BTF.h:210
@ INT_CHAR
Definition: BTF.h:147
@ INT_SIGNED
Definition: BTF.h:146
@ INT_BOOL
Definition: BTF.h:148
constexpr uint32_t FWD_UNION_FLAG
Definition: BTF.h:102
@ MAX_VLEN
Max # of struct/union/enum members or func args.
Definition: BTF.h:93
@ FUNC_STATIC
Definition: BTF.h:201
@ FUNC_EXTERN
Definition: BTF.h:203
@ FUNC_GLOBAL
Definition: BTF.h:202
PatchableRelocKind
CO-RE relocation kind codes used in .BTF.ext section.
Definition: BTF.h:281
@ FIELD_RSHIFT_U64
Definition: BTF.h:287
@ ENUM_VALUE
Definition: BTF.h:293
@ FIELD_SIGNEDNESS
Definition: BTF.h:285
@ FIELD_BYTE_OFFSET
Definition: BTF.h:282
@ FIELD_BYTE_SIZE
Definition: BTF.h:283
@ ENUM_VALUE_EXISTENCE
Definition: BTF.h:292
@ MAX_FIELD_RELOC_KIND
Definition: BTF.h:295
@ BTF_TYPE_ID_REMOTE
Definition: BTF.h:289
@ TYPE_EXISTENCE
Definition: BTF.h:290
@ BTF_TYPE_ID_LOCAL
Definition: BTF.h:288
@ FIELD_LSHIFT_U64
Definition: BTF.h:286
@ TYPE_MATCH
Definition: BTF.h:294
@ TYPE_SIZE
Definition: BTF.h:291
@ FIELD_EXISTENCE
Definition: BTF.h:284
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static bool classof(const CommonType *V)
Definition: BTF.h:321
friend TrailingObjects
Definition: BTF.h:318
Specifying one offset relocation.
Definition: BTF.h:267
uint32_t InsnOffset
Byte offset in this section.
Definition: BTF.h:268
uint32_t OffsetNameOff
The string to traverse types.
Definition: BTF.h:270
uint32_t RelocKind
What to patch the instruction.
Definition: BTF.h:271
uint32_t TypeID
TypeID for the relocation.
Definition: BTF.h:269
Specifying one function info.
Definition: BTF.h:238
uint32_t InsnOffset
Byte offset in the section.
Definition: BTF.h:239
uint32_t TypeId
Type id referring to .BTF type section.
Definition: BTF.h:240
Specifying one line info.
Definition: BTF.h:250
uint32_t getLine() const
Definition: BTF.h:256
uint32_t LineCol
Line num: line_col >> 10, col num: line_col & 0x3ff.
Definition: BTF.h:254
uint32_t getCol() const
Definition: BTF.h:257
uint32_t FileNameOff
File name index in the .BTF string table.
Definition: BTF.h:252
uint32_t InsnOffset
Byte offset in this section.
Definition: BTF.h:251
uint32_t LineOff
Line index in the .BTF string table.
Definition: BTF.h:253
BTF_KIND_ARRAY is followed by one "struct BTFArray".
Definition: BTF.h:169
uint32_t Nelems
Number of elements for this array.
Definition: BTF.h:172
uint32_t IndexType
Index type.
Definition: BTF.h:171
uint32_t ElemType
Element type.
Definition: BTF.h:170
BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar".
Definition: BTF.h:216
uint32_t Size
Occupied memory size.
Definition: BTF.h:219
uint32_t Type
A BTF_KIND_VAR type.
Definition: BTF.h:217
uint32_t Offset
In-section offset.
Definition: BTF.h:218
BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
Definition: BTF.h:162
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:163
uint32_t Val_Hi32
Enum member hi32 value.
Definition: BTF.h:165
uint32_t Val_Lo32
Enum member lo32 value.
Definition: BTF.h:164
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
Definition: BTF.h:154
int32_t Val
Enum member value.
Definition: BTF.h:156
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:155
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
Definition: BTF.h:185
uint32_t NameOff
Member name offset in the string table.
Definition: BTF.h:186
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
Definition: BTF.h:188
uint32_t Type
Member type.
Definition: BTF.h:187
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
Definition: BTF.h:194
uint32_t Type
Definition: BTF.h:196
uint32_t NameOff
Definition: BTF.h:195
The BTF common type definition.
Definition: BTF.h:107
uint32_t Type
Definition: BTF.h:128
uint32_t Size
Definition: BTF.h:127
uint32_t NameOff
Type name offset in the string table.
Definition: BTF.h:109
uint32_t Info
"Info" bits arrangement: Bits 0-15: vlen (e.g.
Definition: BTF.h:118
uint32_t getKind() const
Definition: BTF.h:131
uint32_t getVlen() const
Definition: BTF.h:132
static bool classof(const CommonType *V)
Definition: BTF.h:350
friend TrailingObjects
Definition: BTF.h:347
static bool classof(const CommonType *V)
Definition: BTF.h:340
friend TrailingObjects
Definition: BTF.h:337
The .BTF.ext section header definition.
Definition: BTF.h:223
uint32_t FieldRelocLen
Length of offset reloc section.
Definition: BTF.h:234
uint32_t LineInfoLen
Length of line info section.
Definition: BTF.h:232
uint8_t Flags
Definition: BTF.h:226
uint8_t Version
Definition: BTF.h:225
uint16_t Magic
Definition: BTF.h:224
uint32_t FuncInfoOff
Offset of func info section.
Definition: BTF.h:229
uint32_t FuncInfoLen
Length of func info section.
Definition: BTF.h:230
uint32_t FieldRelocOff
Offset of offset reloc section.
Definition: BTF.h:233
uint32_t LineInfoOff
Offset of line info section.
Definition: BTF.h:231
uint32_t HdrLen
Definition: BTF.h:227
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:275
uint32_t NumFieldReloc
Number of offset reloc's in this section.
Definition: BTF.h:277
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition: BTF.h:276
Specifying function info's in one section.
Definition: BTF.h:244
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition: BTF.h:245
uint32_t NumFuncInfo
Number of func info's in this section.
Definition: BTF.h:246
Specifying line info's in one section.
Definition: BTF.h:261
uint32_t NumLineInfo
Number of line info's in this section.
Definition: BTF.h:263
uint32_t SecNameOff
Section name index in the .BTF string table.
Definition: BTF.h:262
friend TrailingObjects
Definition: BTF.h:328
static bool classof(const CommonType *V)
Definition: BTF.h:331