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