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