BTF.h 8.3 KB

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