BTF.h revision 360784
152419Sjulian//===-- BTF.h --------------------------------------------------*- C++ -*-===// 252419Sjulian// 352419Sjulian// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 452419Sjulian// See https://llvm.org/LICENSE.txt for license information. 552419Sjulian// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 652419Sjulian// 752419Sjulian//===----------------------------------------------------------------------===// 852419Sjulian/// 952419Sjulian/// \file 1052419Sjulian/// This file contains the layout of .BTF and .BTF.ext ELF sections. 1152419Sjulian/// 1252419Sjulian/// The binary layout for .BTF section: 1352419Sjulian/// struct Header 1452419Sjulian/// Type and Str subsections 1552419Sjulian/// The Type subsection is a collection of types with type id starting with 1. 1652419Sjulian/// The Str subsection is simply a collection of strings. 1752419Sjulian/// 1852419Sjulian/// The binary layout for .BTF.ext section: 1952419Sjulian/// struct ExtHeader 2052419Sjulian/// FuncInfo, LineInfo, FieldReloc and ExternReloc subsections 2152419Sjulian/// The FuncInfo subsection is defined as below: 2252419Sjulian/// BTFFuncInfo Size 2352419Sjulian/// struct SecFuncInfo for ELF section #1 2452419Sjulian/// A number of struct BPFFuncInfo for ELF section #1 2552419Sjulian/// struct SecFuncInfo for ELF section #2 2652419Sjulian/// A number of struct BPFFuncInfo for ELF section #2 2752419Sjulian/// ... 2852419Sjulian/// The LineInfo subsection is defined as below: 2952419Sjulian/// BPFLineInfo Size 3052419Sjulian/// struct SecLineInfo for ELF section #1 3152419Sjulian/// A number of struct BPFLineInfo for ELF section #1 3252419Sjulian/// struct SecLineInfo for ELF section #2 3352419Sjulian/// A number of struct BPFLineInfo for ELF section #2 3452419Sjulian/// ... 3552419Sjulian/// The FieldReloc subsection is defined as below: 3652419Sjulian/// BPFFieldReloc Size 3767506Sjulian/// struct SecFieldReloc for ELF section #1 3852419Sjulian/// A number of struct BPFFieldReloc for ELF section #1 3952419Sjulian/// struct SecFieldReloc for ELF section #2 4052752Sjulian/// A number of struct BPFFieldReloc for ELF section #2 4152419Sjulian/// ... 4252419Sjulian/// 4352419Sjulian/// The section formats are also defined at 4452419Sjulian/// https://github.com/torvalds/linux/blob/master/include/uapi/linux/btf.h 4552419Sjulian/// 4652419Sjulian//===----------------------------------------------------------------------===// 4752419Sjulian 4852419Sjulian#ifndef LLVM_LIB_TARGET_BPF_BTF_H 4952419Sjulian#define LLVM_LIB_TARGET_BPF_BTF_H 5052419Sjulian 5152419Sjuliannamespace llvm { 5252419Sjuliannamespace BTF { 5352419Sjulian 5452419Sjulianenum : uint32_t { MAGIC = 0xeB9F, VERSION = 1 }; 5552419Sjulian 5652419Sjulian/// Sizes in bytes of various things in the BTF format. 5752419Sjulianenum { 5852419Sjulian HeaderSize = 24, 5952419Sjulian ExtHeaderSize = 32, 6052419Sjulian CommonTypeSize = 12, 6152419Sjulian BTFArraySize = 12, 6252419Sjulian BTFEnumSize = 8, 6352419Sjulian BTFMemberSize = 12, 6453393Sarchie BTFParamSize = 8, 6552419Sjulian BTFDataSecVarSize = 12, 6652419Sjulian SecFuncInfoSize = 8, 6752419Sjulian SecLineInfoSize = 8, 6853393Sarchie SecFieldRelocSize = 8, 6952419Sjulian BPFFuncInfoSize = 8, 7052419Sjulian BPFLineInfoSize = 16, 7152752Sjulian BPFFieldRelocSize = 16, 7252752Sjulian}; 7352752Sjulian 7452752Sjulian/// The .BTF section header definition. 7552752Sjulianstruct Header { 7652752Sjulian uint16_t Magic; ///< Magic value 7752419Sjulian uint8_t Version; ///< Version number 7852419Sjulian uint8_t Flags; ///< Extra flags 7952419Sjulian uint32_t HdrLen; ///< Length of this header 8052419Sjulian 8152419Sjulian /// All offsets are in bytes relative to the end of this header. 8252419Sjulian uint32_t TypeOff; ///< Offset of type section 8352419Sjulian uint32_t TypeLen; ///< Length of type section 8452419Sjulian uint32_t StrOff; ///< Offset of string section 8552419Sjulian uint32_t StrLen; ///< Length of string section 8652419Sjulian}; 8752419Sjulian 8852419Sjulianenum : uint32_t { 8952419Sjulian MAX_VLEN = 0xffff ///< Max # of struct/union/enum members or func args 9053913Sarchie}; 9153913Sarchie 9252419Sjulianenum TypeKinds : uint8_t { 9352419Sjulian#define HANDLE_BTF_KIND(ID, NAME) BTF_KIND_##NAME = ID, 9452419Sjulian#include "BTF.def" 9552419Sjulian}; 9652419Sjulian 9752419Sjulian/// The BTF common type definition. Different kinds may have 9852419Sjulian/// additional information after this structure data. 9952419Sjulianstruct CommonType { 10052419Sjulian /// Type name offset in the string table. 10152419Sjulian uint32_t NameOff; 10252419Sjulian 10352419Sjulian /// "Info" bits arrangement: 10452419Sjulian /// Bits 0-15: vlen (e.g. # of struct's members) 10552419Sjulian /// Bits 16-23: unused 10652419Sjulian /// Bits 24-27: kind (e.g. int, ptr, array...etc) 10752419Sjulian /// Bits 28-30: unused 10852419Sjulian /// Bit 31: kind_flag, currently used by 10952419Sjulian /// struct, union and fwd 11068876Sdwmalone uint32_t Info; 11152419Sjulian 11252419Sjulian /// "Size" is used by INT, ENUM, STRUCT and UNION. 11352419Sjulian /// "Size" tells the size of the type it is describing. 11452419Sjulian /// 11552419Sjulian /// "Type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, 11652419Sjulian /// FUNC, FUNC_PROTO and VAR. 11752419Sjulian /// "Type" is a type_id referring to another type. 11852419Sjulian union { 11952419Sjulian uint32_t Size; 12052419Sjulian uint32_t Type; 12152419Sjulian }; 12252419Sjulian}; 12352419Sjulian 12452419Sjulian// For some specific BTF_KIND, "struct CommonType" is immediately 12552419Sjulian// followed by extra data. 12652419Sjulian 12752419Sjulian// BTF_KIND_INT is followed by a u32 and the following 12852419Sjulian// is the 32 bits arrangement: 12952419Sjulian// BTF_INT_ENCODING(VAL) : (((VAL) & 0x0f000000) >> 24) 13052419Sjulian// BTF_INT_OFFSET(VAL) : (((VAL & 0x00ff0000)) >> 16) 13152419Sjulian// BTF_INT_BITS(VAL) : ((VAL) & 0x000000ff) 13252419Sjulian 13352419Sjulian/// Attributes stored in the INT_ENCODING. 13452419Sjulianenum : uint8_t { 13552419Sjulian INT_SIGNED = (1 << 0), 13652419Sjulian INT_CHAR = (1 << 1), 13752419Sjulian INT_BOOL = (1 << 2) 13852419Sjulian}; 13952419Sjulian 14052419Sjulian/// BTF_KIND_ENUM is followed by multiple "struct BTFEnum". 14152419Sjulian/// The exact number of btf_enum is stored in the vlen (of the 14252419Sjulian/// info in "struct CommonType"). 14352419Sjulianstruct BTFEnum { 14452419Sjulian uint32_t NameOff; ///< Enum name offset in the string table 14552419Sjulian int32_t Val; ///< Enum member value 14652419Sjulian}; 14752419Sjulian 14852419Sjulian/// BTF_KIND_ARRAY is followed by one "struct BTFArray". 14952419Sjulianstruct BTFArray { 15052419Sjulian uint32_t ElemType; ///< Element type 15159728Sjulian uint32_t IndexType; ///< Index type 15252419Sjulian uint32_t Nelems; ///< Number of elements for this array 15369922Sjulian}; 15469922Sjulian 15569922Sjulian/// BTF_KIND_STRUCT and BTF_KIND_UNION are followed 15669922Sjulian/// by multiple "struct BTFMember". The exact number 15769922Sjulian/// of BTFMember is stored in the vlen (of the info in 15869922Sjulian/// "struct CommonType"). 15969922Sjulian/// 16069922Sjulian/// If the struct/union contains any bitfield member, 16169922Sjulian/// the Offset below represents BitOffset (bits 0 - 23) 16269922Sjulian/// and BitFieldSize(bits 24 - 31) with BitFieldSize = 0 16369922Sjulian/// for non bitfield members. Otherwise, the Offset 16469922Sjulian/// represents the BitOffset. 16569922Sjulianstruct BTFMember { 16669922Sjulian uint32_t NameOff; ///< Member name offset in the string table 16769922Sjulian uint32_t Type; ///< Member type 16869922Sjulian uint32_t Offset; ///< BitOffset or BitFieldSize+BitOffset 16952419Sjulian}; 17052419Sjulian 17152419Sjulian/// BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam". 17252419Sjulian/// The exist number of BTFParam is stored in the vlen (of the info 17352419Sjulian/// in "struct CommonType"). 17452419Sjulianstruct BTFParam { 17552419Sjulian uint32_t NameOff; 17652419Sjulian uint32_t Type; 17752419Sjulian}; 17852419Sjulian 17952419Sjulian/// BTF_KIND_FUNC can be global, static or extern. 18059728Sjulianenum : uint8_t { 18169922Sjulian FUNC_STATIC = 0, 18252419Sjulian FUNC_GLOBAL = 1, 18352419Sjulian FUNC_EXTERN = 2, 18452419Sjulian}; 18552419Sjulian 18652419Sjulian/// Variable scoping information. 18752419Sjulianenum : uint8_t { 18852419Sjulian VAR_STATIC = 0, ///< Linkage: InternalLinkage 18952419Sjulian VAR_GLOBAL_ALLOCATED = 1, ///< Linkage: ExternalLinkage 19052539Sjulian VAR_GLOBAL_EXTERNAL = 2, ///< Linkage: ExternalLinkage 19152539Sjulian}; 19252419Sjulian 19352419Sjulian/// BTF_KIND_DATASEC are followed by multiple "struct BTFDataSecVar". 19452419Sjulian/// The exist number of BTFDataSec is stored in the vlen (of the info 19552419Sjulian/// in "struct CommonType"). 19652419Sjulianstruct BTFDataSec { 19752419Sjulian uint32_t Type; ///< A BTF_KIND_VAR type 19852419Sjulian uint32_t Offset; ///< In-section offset 19952419Sjulian uint32_t Size; ///< Occupied memory size 20052419Sjulian}; 20152419Sjulian 20252419Sjulian/// The .BTF.ext section header definition. 20352419Sjulianstruct ExtHeader { 20452419Sjulian uint16_t Magic; 20552419Sjulian uint8_t Version; 20652419Sjulian uint8_t Flags; 20752419Sjulian uint32_t HdrLen; 20852419Sjulian 20952419Sjulian uint32_t FuncInfoOff; ///< Offset of func info section 21052419Sjulian uint32_t FuncInfoLen; ///< Length of func info section 21152419Sjulian uint32_t LineInfoOff; ///< Offset of line info section 21252419Sjulian uint32_t LineInfoLen; ///< Length of line info section 21352419Sjulian uint32_t FieldRelocOff; ///< Offset of offset reloc section 21452419Sjulian uint32_t FieldRelocLen; ///< Length of offset reloc section 21552419Sjulian}; 21652419Sjulian 21752419Sjulian/// Specifying one function info. 21852419Sjulianstruct BPFFuncInfo { 21952419Sjulian uint32_t InsnOffset; ///< Byte offset in the section 22052419Sjulian uint32_t TypeId; ///< Type id referring to .BTF type section 22152419Sjulian}; 22252419Sjulian 22352419Sjulian/// Specifying function info's in one section. 22452419Sjulianstruct SecFuncInfo { 22552419Sjulian uint32_t SecNameOff; ///< Section name index in the .BTF string table 22652419Sjulian uint32_t NumFuncInfo; ///< Number of func info's in this section 22752419Sjulian}; 22852419Sjulian 22952419Sjulian/// Specifying one line info. 23052419Sjulianstruct BPFLineInfo { 23152419Sjulian uint32_t InsnOffset; ///< Byte offset in this section 23252419Sjulian uint32_t FileNameOff; ///< File name index in the .BTF string table 23352419Sjulian uint32_t LineOff; ///< Line index in the .BTF string table 23452419Sjulian uint32_t LineCol; ///< Line num: line_col >> 10, 23552419Sjulian /// col num: line_col & 0x3ff 23652419Sjulian}; 23752419Sjulian 23852419Sjulian/// Specifying line info's in one section. 23952419Sjulianstruct SecLineInfo { 24052419Sjulian uint32_t SecNameOff; ///< Section name index in the .BTF string table 24152419Sjulian uint32_t NumLineInfo; ///< Number of line info's in this section 24252419Sjulian}; 24352419Sjulian 24452419Sjulian/// Specifying one offset relocation. 24552419Sjulianstruct BPFFieldReloc { 24652419Sjulian uint32_t InsnOffset; ///< Byte offset in this section 24752419Sjulian uint32_t TypeID; ///< TypeID for the relocation 24852419Sjulian uint32_t OffsetNameOff; ///< The string to traverse types 24952419Sjulian uint32_t RelocKind; ///< What to patch the instruction 25052419Sjulian}; 25152419Sjulian 25252419Sjulian/// Specifying offset relocation's in one section. 253struct SecFieldReloc { 254 uint32_t SecNameOff; ///< Section name index in the .BTF string table 255 uint32_t NumFieldReloc; ///< Number of offset reloc's in this section 256}; 257 258} // End namespace BTF. 259} // End namespace llvm. 260 261#endif 262