Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:39

0001 //===-- BTF.h --------------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file contains the layout of .BTF and .BTF.ext ELF sections.
0011 ///
0012 /// The binary layout for .BTF section:
0013 ///   struct Header
0014 ///   Type and Str subsections
0015 /// The Type subsection is a collection of types with type id starting with 1.
0016 /// The Str subsection is simply a collection of strings.
0017 ///
0018 /// The binary layout for .BTF.ext section:
0019 ///   struct ExtHeader
0020 ///   FuncInfo, LineInfo, FieldReloc and ExternReloc subsections
0021 /// The FuncInfo subsection is defined as below:
0022 ///   BTFFuncInfo Size
0023 ///   struct SecFuncInfo for ELF section #1
0024 ///   A number of struct BPFFuncInfo for ELF section #1
0025 ///   struct SecFuncInfo for ELF section #2
0026 ///   A number of struct BPFFuncInfo for ELF section #2
0027 ///   ...
0028 /// The LineInfo subsection is defined as below:
0029 ///   BPFLineInfo Size
0030 ///   struct SecLineInfo for ELF section #1
0031 ///   A number of struct BPFLineInfo for ELF section #1
0032 ///   struct SecLineInfo for ELF section #2
0033 ///   A number of struct BPFLineInfo for ELF section #2
0034 ///   ...
0035 /// The FieldReloc subsection is defined as below:
0036 ///   BPFFieldReloc Size
0037 ///   struct SecFieldReloc for ELF section #1
0038 ///   A number of struct BPFFieldReloc for ELF section #1
0039 ///   struct SecFieldReloc for ELF section #2
0040 ///   A number of struct BPFFieldReloc for ELF section #2
0041 ///   ...
0042 ///
0043 /// The section formats are also defined at
0044 ///    https://github.com/torvalds/linux/blob/master/include/uapi/linux/btf.h
0045 ///
0046 //===----------------------------------------------------------------------===//
0047 
0048 #ifndef LLVM_LIB_TARGET_BPF_BTF_H
0049 #define LLVM_LIB_TARGET_BPF_BTF_H
0050 
0051 #include "llvm/ADT/ArrayRef.h"
0052 #include "llvm/Support/TrailingObjects.h"
0053 
0054 namespace llvm {
0055 namespace BTF {
0056 
0057 enum : uint32_t { MAGIC = 0xeB9F, VERSION = 1 };
0058 
0059 /// Sizes in bytes of various things in the BTF format.
0060 enum {
0061   HeaderSize = 24,
0062   ExtHeaderSize = 32,
0063   CommonTypeSize = 12,
0064   BTFArraySize = 12,
0065   BTFEnumSize = 8,
0066   BTFEnum64Size = 12,
0067   BTFMemberSize = 12,
0068   BTFParamSize = 8,
0069   BTFDataSecVarSize = 12,
0070   SecFuncInfoSize = 8,
0071   SecLineInfoSize = 8,
0072   SecFieldRelocSize = 8,
0073   BPFFuncInfoSize = 8,
0074   BPFLineInfoSize = 16,
0075   BPFFieldRelocSize = 16,
0076 };
0077 
0078 /// The .BTF section header definition.
0079 struct Header {
0080   uint16_t Magic;  ///< Magic value
0081   uint8_t Version; ///< Version number
0082   uint8_t Flags;   ///< Extra flags
0083   uint32_t HdrLen; ///< Length of this header
0084 
0085   /// All offsets are in bytes relative to the end of this header.
0086   uint32_t TypeOff; ///< Offset of type section
0087   uint32_t TypeLen; ///< Length of type section
0088   uint32_t StrOff;  ///< Offset of string section
0089   uint32_t StrLen;  ///< Length of string section
0090 };
0091 
0092 enum : uint32_t {
0093   MAX_VLEN = 0xffff ///< Max # of struct/union/enum members or func args
0094 };
0095 
0096 enum TypeKinds : uint8_t {
0097 #define HANDLE_BTF_KIND(ID, NAME) BTF_KIND_##NAME = ID,
0098 #include "BTF.def"
0099 };
0100 
0101 // Constants for CommonType::Info field.
0102 constexpr uint32_t FWD_UNION_FLAG = 1u << 31;
0103 constexpr uint32_t ENUM_SIGNED_FLAG = 1u << 31;
0104 
0105 /// The BTF common type definition. Different kinds may have
0106 /// additional information after this structure data.
0107 struct CommonType {
0108   /// Type name offset in the string table.
0109   uint32_t NameOff;
0110 
0111   /// "Info" bits arrangement:
0112   /// Bits  0-15: vlen (e.g. # of struct's members)
0113   /// Bits 16-23: unused
0114   /// Bits 24-28: kind (e.g. int, ptr, array...etc)
0115   /// Bits 29-30: unused
0116   /// Bit     31: kind_flag, currently used by
0117   ///             struct, union and fwd
0118   uint32_t Info;
0119 
0120   /// "Size" is used by INT, ENUM, STRUCT and UNION.
0121   /// "Size" tells the size of the type it is describing.
0122   ///
0123   /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
0124   /// FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
0125   /// "Type" is a type_id referring to another type.
0126   union {
0127     uint32_t Size;
0128     uint32_t Type;
0129   };
0130 
0131   uint32_t getKind() const { return Info >> 24 & 0x1f; }
0132   uint32_t getVlen() const { return Info & 0xffff; }
0133 };
0134 
0135 // For some specific BTF_KIND, "struct CommonType" is immediately
0136 // followed by extra data.
0137 
0138 // BTF_KIND_INT is followed by a u32 and the following
0139 // is the 32 bits arrangement:
0140 // BTF_INT_ENCODING(VAL) : (((VAL) & 0x0f000000) >> 24)
0141 // BTF_INT_OFFSET(VAL) : (((VAL & 0x00ff0000)) >> 16)
0142 // BTF_INT_BITS(VAL) : ((VAL) & 0x000000ff)
0143 
0144 /// Attributes stored in the INT_ENCODING.
0145 enum : uint8_t {
0146   INT_SIGNED = (1 << 0),
0147   INT_CHAR = (1 << 1),
0148   INT_BOOL = (1 << 2)
0149 };
0150 
0151 /// BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
0152 /// The exact number of btf_enum is stored in the vlen (of the
0153 /// info in "struct CommonType").
0154 struct BTFEnum {
0155   uint32_t NameOff; ///< Enum name offset in the string table
0156   int32_t Val;      ///< Enum member value
0157 };
0158 
0159 /// BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
0160 /// The exact number of BTFEnum64 is stored in the vlen (of the
0161 /// info in "struct CommonType").
0162 struct BTFEnum64 {
0163   uint32_t NameOff;  ///< Enum name offset in the string table
0164   uint32_t Val_Lo32; ///< Enum member lo32 value
0165   uint32_t Val_Hi32; ///< Enum member hi32 value
0166 };
0167 
0168 /// BTF_KIND_ARRAY is followed by one "struct BTFArray".
0169 struct BTFArray {
0170   uint32_t ElemType;  ///< Element type
0171   uint32_t IndexType; ///< Index type
0172   uint32_t Nelems;    ///< Number of elements for this array
0173 };
0174 
0175 /// BTF_KIND_STRUCT and BTF_KIND_UNION are followed
0176 /// by multiple "struct BTFMember".  The exact number
0177 /// of BTFMember is stored in the vlen (of the info in
0178 /// "struct CommonType").
0179 ///
0180 /// If the struct/union contains any bitfield member,
0181 /// the Offset below represents BitOffset (bits 0 - 23)
0182 /// and BitFieldSize(bits 24 - 31) with BitFieldSize = 0
0183 /// for non bitfield members. Otherwise, the Offset
0184 /// represents the BitOffset.
0185 struct BTFMember {
0186   uint32_t NameOff; ///< Member name offset in the string table
0187   uint32_t Type;    ///< Member type
0188   uint32_t Offset;  ///< BitOffset or BitFieldSize+BitOffset
0189 };
0190 
0191 /// BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
0192 /// The exist number of BTFParam is stored in the vlen (of the info
0193 /// in "struct CommonType").
0194 struct BTFParam {
0195   uint32_t NameOff;
0196   uint32_t Type;
0197 };
0198 
0199 /// BTF_KIND_FUNC can be global, static or extern.
0200 enum : uint8_t {
0201   FUNC_STATIC = 0,
0202   FUNC_GLOBAL = 1,
0203   FUNC_EXTERN = 2,
0204 };
0205 
0206 /// Variable scoping information.
0207 enum : uint8_t {
0208   VAR_STATIC = 0,           ///< Linkage: InternalLinkage
0209   VAR_GLOBAL_ALLOCATED = 1, ///< Linkage: ExternalLinkage
0210   VAR_GLOBAL_EXTERNAL = 2,  ///< Linkage: ExternalLinkage
0211 };
0212 
0213 /// BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar".
0214 /// The exist number of BTFDataSec is stored in the vlen (of the info
0215 /// in "struct CommonType").
0216 struct BTFDataSec {
0217   uint32_t Type;   ///< A BTF_KIND_VAR type
0218   uint32_t Offset; ///< In-section offset
0219   uint32_t Size;   ///< Occupied memory size
0220 };
0221 
0222 /// The .BTF.ext section header definition.
0223 struct ExtHeader {
0224   uint16_t Magic;
0225   uint8_t Version;
0226   uint8_t Flags;
0227   uint32_t HdrLen;
0228 
0229   uint32_t FuncInfoOff;   ///< Offset of func info section
0230   uint32_t FuncInfoLen;   ///< Length of func info section
0231   uint32_t LineInfoOff;   ///< Offset of line info section
0232   uint32_t LineInfoLen;   ///< Length of line info section
0233   uint32_t FieldRelocOff; ///< Offset of offset reloc section
0234   uint32_t FieldRelocLen; ///< Length of offset reloc section
0235 };
0236 
0237 /// Specifying one function info.
0238 struct BPFFuncInfo {
0239   uint32_t InsnOffset; ///< Byte offset in the section
0240   uint32_t TypeId;     ///< Type id referring to .BTF type section
0241 };
0242 
0243 /// Specifying function info's in one section.
0244 struct SecFuncInfo {
0245   uint32_t SecNameOff;  ///< Section name index in the .BTF string table
0246   uint32_t NumFuncInfo; ///< Number of func info's in this section
0247 };
0248 
0249 /// Specifying one line info.
0250 struct BPFLineInfo {
0251   uint32_t InsnOffset;  ///< Byte offset in this section
0252   uint32_t FileNameOff; ///< File name index in the .BTF string table
0253   uint32_t LineOff;     ///< Line index in the .BTF string table
0254   uint32_t LineCol;     ///< Line num: line_col >> 10,
0255                         ///  col num: line_col & 0x3ff
0256   uint32_t getLine() const { return LineCol >> 10; }
0257   uint32_t getCol() const { return LineCol & 0x3ff; }
0258 };
0259 
0260 /// Specifying line info's in one section.
0261 struct SecLineInfo {
0262   uint32_t SecNameOff;  ///< Section name index in the .BTF string table
0263   uint32_t NumLineInfo; ///< Number of line info's in this section
0264 };
0265 
0266 /// Specifying one offset relocation.
0267 struct BPFFieldReloc {
0268   uint32_t InsnOffset;    ///< Byte offset in this section
0269   uint32_t TypeID;        ///< TypeID for the relocation
0270   uint32_t OffsetNameOff; ///< The string to traverse types
0271   uint32_t RelocKind;     ///< What to patch the instruction
0272 };
0273 
0274 /// Specifying offset relocation's in one section.
0275 struct SecFieldReloc {
0276   uint32_t SecNameOff;    ///< Section name index in the .BTF string table
0277   uint32_t NumFieldReloc; ///< Number of offset reloc's in this section
0278 };
0279 
0280 /// CO-RE relocation kind codes used in .BTF.ext section.
0281 enum PatchableRelocKind : uint32_t {
0282   FIELD_BYTE_OFFSET = 0,
0283   FIELD_BYTE_SIZE,
0284   FIELD_EXISTENCE,
0285   FIELD_SIGNEDNESS,
0286   FIELD_LSHIFT_U64,
0287   FIELD_RSHIFT_U64,
0288   BTF_TYPE_ID_LOCAL,
0289   BTF_TYPE_ID_REMOTE,
0290   TYPE_EXISTENCE,
0291   TYPE_SIZE,
0292   ENUM_VALUE_EXISTENCE,
0293   ENUM_VALUE,
0294   TYPE_MATCH,
0295   MAX_FIELD_RELOC_KIND,
0296 };
0297 
0298 // Define a number of sub-types for CommonType, each with:
0299 // - An accessor for a relevant "tail" information (data fields that
0300 //   follow the CommonType record in binary format).
0301 // - A classof() definition based on CommonType::getKind() value to
0302 //   allow use with dyn_cast<>() function.
0303 
0304 // For CommonType sub-types that are followed by a single entry of
0305 // some type in the binary format.
0306 #define BTF_DEFINE_TAIL(Type, Accessor)                                        \
0307   const Type &Accessor() const { return *getTrailingObjects<Type>(); }
0308 
0309 // For CommonType sub-types that are followed by CommonType::getVlen()
0310 // number of entries of some type in the binary format.
0311 #define BTF_DEFINE_TAIL_ARR(Type, Accessor)                                    \
0312   ArrayRef<Type> Accessor() const {                                            \
0313     return ArrayRef<Type>(getTrailingObjects<Type>(), getVlen());              \
0314   }
0315 
0316 struct ArrayType final : CommonType,
0317                          private TrailingObjects<ArrayType, BTFArray> {
0318   friend TrailingObjects;
0319   BTF_DEFINE_TAIL(BTFArray, getArray)
0320 
0321   static bool classof(const CommonType *V) {
0322     return V->getKind() == BTF_KIND_ARRAY;
0323   }
0324 };
0325 
0326 struct StructType final : CommonType,
0327                           private TrailingObjects<StructType, BTFMember> {
0328   friend TrailingObjects;
0329   BTF_DEFINE_TAIL_ARR(BTFMember, members)
0330 
0331   static bool classof(const CommonType *V) {
0332     return V->getKind() == BTF_KIND_STRUCT || V->getKind() == BTF_KIND_UNION;
0333   }
0334 };
0335 
0336 struct EnumType final : CommonType, private TrailingObjects<EnumType, BTFEnum> {
0337   friend TrailingObjects;
0338   BTF_DEFINE_TAIL_ARR(BTFEnum, values)
0339 
0340   static bool classof(const CommonType *V) {
0341     return V->getKind() == BTF_KIND_ENUM;
0342   }
0343 };
0344 
0345 struct Enum64Type final : CommonType,
0346                           private TrailingObjects<Enum64Type, BTFEnum64> {
0347   friend TrailingObjects;
0348   BTF_DEFINE_TAIL_ARR(BTFEnum64, values)
0349 
0350   static bool classof(const CommonType *V) {
0351     return V->getKind() == BTF_KIND_ENUM64;
0352   }
0353 };
0354 
0355 #undef BTF_DEFINE_TAIL
0356 #undef BTF_DEFINE_TAIL_ARR
0357 
0358 } // End namespace BTF.
0359 } // End namespace llvm.
0360 
0361 #endif