1 //===-- BTF.h --------------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// This file contains the layout of .BTF and .BTF.ext ELF sections. 12 /// 13 /// The binary layout for .BTF section: 14 /// struct Header 15 /// Type and Str subsections 16 /// The Type subsection is a collection of types with type id starting with 1. 17 /// The Str subsection is simply a collection of strings. 18 /// 19 /// The binary layout for .BTF.ext section: 20 /// struct ExtHeader 21 /// FuncInfo and LineInfo subsections 22 /// The FuncInfo subsection is defined as below: 23 /// BTFFuncInfo Size 24 /// struct SecFuncInfo for ELF section #1 25 /// A number of struct BPFFuncInfo for ELF section #1 26 /// struct SecFuncInfo for ELF section #2 27 /// A number of struct BPFFuncInfo for ELF section #2 28 /// ... 29 /// The LineInfo subsection is defined as below: 30 /// BPFLineInfo Size 31 /// struct SecLineInfo for ELF section #1 32 /// A number of struct BPFLineInfo for ELF section #1 33 /// struct SecLineInfo for ELF section #2 34 /// A number of struct BPFLineInfo for ELF section #2 35 /// ... 36 /// 37 /// The section formats are also defined at 38 /// https://github.com/torvalds/linux/blob/master/include/uapi/linux/btf.h 39 /// 40 //===----------------------------------------------------------------------===// 41 42 #ifndef LLVM_LIB_TARGET_BPF_BTF_H 43 #define LLVM_LIB_TARGET_BPF_BTF_H 44 45 namespace llvm { 46 namespace BTF { 47 48 enum : uint32_t { MAGIC = 0xeB9F, VERSION = 1 }; 49 50 /// Sizes in bytes of various things in the BTF format. 51 enum { 52 HeaderSize = 24, 53 ExtHeaderSize = 24, 54 CommonTypeSize = 12, 55 BTFArraySize = 12, 56 BTFEnumSize = 8, 57 BTFMemberSize = 12, 58 BTFParamSize = 8, 59 SecFuncInfoSize = 8, 60 SecLineInfoSize = 8, 61 BPFFuncInfoSize = 8, 62 BPFLineInfoSize = 16 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. 92 uint32_t NameOff; 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 101 uint32_t Info; 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 and FUNC_PROTO. 108 /// "Type" is a type_id referring to another type. 109 union { 110 uint32_t Size; 111 uint32_t Type; 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 { INT_SIGNED = (1 << 0), INT_CHAR = (1 << 1), INT_BOOL = (1 << 2) }; 126 127 /// BTF_KIND_ENUM is followed by multiple "struct BTFEnum". 128 /// The exact number of btf_enum is stored in the vlen (of the 129 /// info in "struct CommonType"). 130 struct BTFEnum { 131 uint32_t NameOff; ///< Enum name offset in the string table 132 int32_t Val; ///< Enum member value 133 }; 134 135 /// BTF_KIND_ARRAY is followed by one "struct BTFArray". 136 struct BTFArray { 137 uint32_t ElemType; ///< Element type 138 uint32_t IndexType; ///< Index type 139 uint32_t Nelems; ///< Number of elements for this array 140 }; 141 142 /// BTF_KIND_STRUCT and BTF_KIND_UNION are followed 143 /// by multiple "struct BTFMember". The exact number 144 /// of BTFMember is stored in the vlen (of the info in 145 /// "struct CommonType"). 146 /// 147 /// If the struct/union contains any bitfield member, 148 /// the Offset below represents BitOffset (bits 0 - 23) 149 /// and BitFieldSize(bits 24 - 31) with BitFieldSize = 0 150 /// for non bitfield members. Otherwise, the Offset 151 /// represents the BitOffset. 152 struct BTFMember { 153 uint32_t NameOff; ///< Member name offset in the string table 154 uint32_t Type; ///< Member type 155 uint32_t Offset; ///< BitOffset or BitFieldSize+BitOffset 156 }; 157 158 /// BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam". 159 /// The exist number of BTFParam is stored in the vlen (of the info 160 /// in "struct CommonType"). 161 struct BTFParam { 162 uint32_t NameOff; 163 uint32_t Type; 164 }; 165 166 /// The .BTF.ext section header definition. 167 struct ExtHeader { 168 uint16_t Magic; 169 uint8_t Version; 170 uint8_t Flags; 171 uint32_t HdrLen; 172 173 uint32_t FuncInfoOff; ///< Offset of func info section 174 uint32_t FuncInfoLen; ///< Length of func info section 175 uint32_t LineInfoOff; ///< Offset of line info section 176 uint32_t LineInfoLen; ///< Length of line info section 177 }; 178 179 /// Specifying one function info. 180 struct BPFFuncInfo { 181 uint32_t InsnOffset; ///< Byte offset in the section 182 uint32_t TypeId; ///< Type id referring to .BTF type section 183 }; 184 185 /// Specifying function info's in one section. 186 struct SecFuncInfo { 187 uint32_t SecNameOff; ///< Section name index in the .BTF string table 188 uint32_t NumFuncInfo; ///< Number of func info's in this section 189 }; 190 191 /// Specifying one line info. 192 struct BPFLineInfo { 193 uint32_t InsnOffset; ///< Byte offset in this section 194 uint32_t FileNameOff; ///< File name index in the .BTF string table 195 uint32_t LineOff; ///< Line index in the .BTF string table 196 uint32_t LineCol; ///< Line num: line_col >> 10, 197 /// col num: line_col & 0x3ff 198 }; 199 200 /// Specifying line info's in one section. 201 struct SecLineInfo { 202 uint32_t SecNameOff; ///< Section name index in the .BTF string tble 203 uint32_t NumLineInfo; ///< Number of line info's in this section 204 }; 205 206 } // End namespace BTF. 207 } // End namespace llvm. 208 209 #endif 210