BTF.h 8.7 KB

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