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