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