btf.rst 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. =====================
  2. BPF Type Format (BTF)
  3. =====================
  4. 1. Introduction
  5. ===============
  6. BTF (BPF Type Format) is the metadata format which encodes the debug info
  7. related to BPF program/map. The name BTF was used initially to describe data
  8. types. The BTF was later extended to include function info for defined
  9. subroutines, and line info for source/line information.
  10. The debug info is used for map pretty print, function signature, etc. The
  11. function signature enables better bpf program/function kernel symbol. The line
  12. info helps generate source annotated translated byte code, jited code and
  13. verifier log.
  14. The BTF specification contains two parts,
  15. * BTF kernel API
  16. * BTF ELF file format
  17. The kernel API is the contract between user space and kernel. The kernel
  18. verifies the BTF info before using it. The ELF file format is a user space
  19. contract between ELF file and libbpf loader.
  20. The type and string sections are part of the BTF kernel API, describing the
  21. debug info (mostly types related) referenced by the bpf program. These two
  22. sections are discussed in details in :ref:`BTF_Type_String`.
  23. .. _BTF_Type_String:
  24. 2. BTF Type and String Encoding
  25. ===============================
  26. The file ``include/uapi/linux/btf.h`` provides high-level definition of how
  27. types/strings are encoded.
  28. The beginning of data blob must be::
  29. struct btf_header {
  30. __u16 magic;
  31. __u8 version;
  32. __u8 flags;
  33. __u32 hdr_len;
  34. /* All offsets are in bytes relative to the end of this header */
  35. __u32 type_off; /* offset of type section */
  36. __u32 type_len; /* length of type section */
  37. __u32 str_off; /* offset of string section */
  38. __u32 str_len; /* length of string section */
  39. };
  40. The magic is ``0xeB9F``, which has different encoding for big and little
  41. endian systems, and can be used to test whether BTF is generated for big- or
  42. little-endian target. The ``btf_header`` is designed to be extensible with
  43. ``hdr_len`` equal to ``sizeof(struct btf_header)`` when a data blob is
  44. generated.
  45. 2.1 String Encoding
  46. -------------------
  47. The first string in the string section must be a null string. The rest of
  48. string table is a concatenation of other null-terminated strings.
  49. 2.2 Type Encoding
  50. -----------------
  51. The type id ``0`` is reserved for ``void`` type. The type section is parsed
  52. sequentially and type id is assigned to each recognized type starting from id
  53. ``1``. Currently, the following types are supported::
  54. #define BTF_KIND_INT 1 /* Integer */
  55. #define BTF_KIND_PTR 2 /* Pointer */
  56. #define BTF_KIND_ARRAY 3 /* Array */
  57. #define BTF_KIND_STRUCT 4 /* Struct */
  58. #define BTF_KIND_UNION 5 /* Union */
  59. #define BTF_KIND_ENUM 6 /* Enumeration up to 32-bit values */
  60. #define BTF_KIND_FWD 7 /* Forward */
  61. #define BTF_KIND_TYPEDEF 8 /* Typedef */
  62. #define BTF_KIND_VOLATILE 9 /* Volatile */
  63. #define BTF_KIND_CONST 10 /* Const */
  64. #define BTF_KIND_RESTRICT 11 /* Restrict */
  65. #define BTF_KIND_FUNC 12 /* Function */
  66. #define BTF_KIND_FUNC_PROTO 13 /* Function Proto */
  67. #define BTF_KIND_VAR 14 /* Variable */
  68. #define BTF_KIND_DATASEC 15 /* Section */
  69. #define BTF_KIND_FLOAT 16 /* Floating point */
  70. #define BTF_KIND_DECL_TAG 17 /* Decl Tag */
  71. #define BTF_KIND_TYPE_TAG 18 /* Type Tag */
  72. #define BTF_KIND_ENUM64 19 /* Enumeration up to 64-bit values */
  73. Note that the type section encodes debug info, not just pure types.
  74. ``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
  75. Each type contains the following common data::
  76. struct btf_type {
  77. __u32 name_off;
  78. /* "info" bits arrangement
  79. * bits 0-15: vlen (e.g. # of struct's members)
  80. * bits 16-23: unused
  81. * bits 24-28: kind (e.g. int, ptr, array...etc)
  82. * bits 29-30: unused
  83. * bit 31: kind_flag, currently used by
  84. * struct, union, fwd, enum and enum64.
  85. */
  86. __u32 info;
  87. /* "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.
  88. * "size" tells the size of the type it is describing.
  89. *
  90. * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
  91. * FUNC, FUNC_PROTO, DECL_TAG and TYPE_TAG.
  92. * "type" is a type_id referring to another type.
  93. */
  94. union {
  95. __u32 size;
  96. __u32 type;
  97. };
  98. };
  99. For certain kinds, the common data are followed by kind-specific data. The
  100. ``name_off`` in ``struct btf_type`` specifies the offset in the string table.
  101. The following sections detail encoding of each kind.
  102. 2.2.1 BTF_KIND_INT
  103. ~~~~~~~~~~~~~~~~~~
  104. ``struct btf_type`` encoding requirement:
  105. * ``name_off``: any valid offset
  106. * ``info.kind_flag``: 0
  107. * ``info.kind``: BTF_KIND_INT
  108. * ``info.vlen``: 0
  109. * ``size``: the size of the int type in bytes.
  110. ``btf_type`` is followed by a ``u32`` with the following bits arrangement::
  111. #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24)
  112. #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16)
  113. #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff)
  114. The ``BTF_INT_ENCODING`` has the following attributes::
  115. #define BTF_INT_SIGNED (1 << 0)
  116. #define BTF_INT_CHAR (1 << 1)
  117. #define BTF_INT_BOOL (1 << 2)
  118. The ``BTF_INT_ENCODING()`` provides extra information: signedness, char, or
  119. bool, for the int type. The char and bool encoding are mostly useful for
  120. pretty print. At most one encoding can be specified for the int type.
  121. The ``BTF_INT_BITS()`` specifies the number of actual bits held by this int
  122. type. For example, a 4-bit bitfield encodes ``BTF_INT_BITS()`` equals to 4.
  123. The ``btf_type.size * 8`` must be equal to or greater than ``BTF_INT_BITS()``
  124. for the type. The maximum value of ``BTF_INT_BITS()`` is 128.
  125. The ``BTF_INT_OFFSET()`` specifies the starting bit offset to calculate values
  126. for this int. For example, a bitfield struct member has:
  127. * btf member bit offset 100 from the start of the structure,
  128. * btf member pointing to an int type,
  129. * the int type has ``BTF_INT_OFFSET() = 2`` and ``BTF_INT_BITS() = 4``
  130. Then in the struct memory layout, this member will occupy ``4`` bits starting
  131. from bits ``100 + 2 = 102``.
  132. Alternatively, the bitfield struct member can be the following to access the
  133. same bits as the above:
  134. * btf member bit offset 102,
  135. * btf member pointing to an int type,
  136. * the int type has ``BTF_INT_OFFSET() = 0`` and ``BTF_INT_BITS() = 4``
  137. The original intention of ``BTF_INT_OFFSET()`` is to provide flexibility of
  138. bitfield encoding. Currently, both llvm and pahole generate
  139. ``BTF_INT_OFFSET() = 0`` for all int types.
  140. 2.2.2 BTF_KIND_PTR
  141. ~~~~~~~~~~~~~~~~~~
  142. ``struct btf_type`` encoding requirement:
  143. * ``name_off``: 0
  144. * ``info.kind_flag``: 0
  145. * ``info.kind``: BTF_KIND_PTR
  146. * ``info.vlen``: 0
  147. * ``type``: the pointee type of the pointer
  148. No additional type data follow ``btf_type``.
  149. 2.2.3 BTF_KIND_ARRAY
  150. ~~~~~~~~~~~~~~~~~~~~
  151. ``struct btf_type`` encoding requirement:
  152. * ``name_off``: 0
  153. * ``info.kind_flag``: 0
  154. * ``info.kind``: BTF_KIND_ARRAY
  155. * ``info.vlen``: 0
  156. * ``size/type``: 0, not used
  157. ``btf_type`` is followed by one ``struct btf_array``::
  158. struct btf_array {
  159. __u32 type;
  160. __u32 index_type;
  161. __u32 nelems;
  162. };
  163. The ``struct btf_array`` encoding:
  164. * ``type``: the element type
  165. * ``index_type``: the index type
  166. * ``nelems``: the number of elements for this array (``0`` is also allowed).
  167. The ``index_type`` can be any regular int type (``u8``, ``u16``, ``u32``,
  168. ``u64``, ``unsigned __int128``). The original design of including
  169. ``index_type`` follows DWARF, which has an ``index_type`` for its array type.
  170. Currently in BTF, beyond type verification, the ``index_type`` is not used.
  171. The ``struct btf_array`` allows chaining through element type to represent
  172. multidimensional arrays. For example, for ``int a[5][6]``, the following type
  173. information illustrates the chaining:
  174. * [1]: int
  175. * [2]: array, ``btf_array.type = [1]``, ``btf_array.nelems = 6``
  176. * [3]: array, ``btf_array.type = [2]``, ``btf_array.nelems = 5``
  177. Currently, both pahole and llvm collapse multidimensional array into
  178. one-dimensional array, e.g., for ``a[5][6]``, the ``btf_array.nelems`` is
  179. equal to ``30``. This is because the original use case is map pretty print
  180. where the whole array is dumped out so one-dimensional array is enough. As
  181. more BTF usage is explored, pahole and llvm can be changed to generate proper
  182. chained representation for multidimensional arrays.
  183. 2.2.4 BTF_KIND_STRUCT
  184. ~~~~~~~~~~~~~~~~~~~~~
  185. 2.2.5 BTF_KIND_UNION
  186. ~~~~~~~~~~~~~~~~~~~~
  187. ``struct btf_type`` encoding requirement:
  188. * ``name_off``: 0 or offset to a valid C identifier
  189. * ``info.kind_flag``: 0 or 1
  190. * ``info.kind``: BTF_KIND_STRUCT or BTF_KIND_UNION
  191. * ``info.vlen``: the number of struct/union members
  192. * ``info.size``: the size of the struct/union in bytes
  193. ``btf_type`` is followed by ``info.vlen`` number of ``struct btf_member``.::
  194. struct btf_member {
  195. __u32 name_off;
  196. __u32 type;
  197. __u32 offset;
  198. };
  199. ``struct btf_member`` encoding:
  200. * ``name_off``: offset to a valid C identifier
  201. * ``type``: the member type
  202. * ``offset``: <see below>
  203. If the type info ``kind_flag`` is not set, the offset contains only bit offset
  204. of the member. Note that the base type of the bitfield can only be int or enum
  205. type. If the bitfield size is 32, the base type can be either int or enum
  206. type. If the bitfield size is not 32, the base type must be int, and int type
  207. ``BTF_INT_BITS()`` encodes the bitfield size.
  208. If the ``kind_flag`` is set, the ``btf_member.offset`` contains both member
  209. bitfield size and bit offset. The bitfield size and bit offset are calculated
  210. as below.::
  211. #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24)
  212. #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff)
  213. In this case, if the base type is an int type, it must be a regular int type:
  214. * ``BTF_INT_OFFSET()`` must be 0.
  215. * ``BTF_INT_BITS()`` must be equal to ``{1,2,4,8,16} * 8``.
  216. Commit 9d5f9f701b18 introduced ``kind_flag`` and explains why both modes
  217. exist.
  218. 2.2.6 BTF_KIND_ENUM
  219. ~~~~~~~~~~~~~~~~~~~
  220. ``struct btf_type`` encoding requirement:
  221. * ``name_off``: 0 or offset to a valid C identifier
  222. * ``info.kind_flag``: 0 for unsigned, 1 for signed
  223. * ``info.kind``: BTF_KIND_ENUM
  224. * ``info.vlen``: number of enum values
  225. * ``size``: 1/2/4/8
  226. ``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum``.::
  227. struct btf_enum {
  228. __u32 name_off;
  229. __s32 val;
  230. };
  231. The ``btf_enum`` encoding:
  232. * ``name_off``: offset to a valid C identifier
  233. * ``val``: any value
  234. If the original enum value is signed and the size is less than 4,
  235. that value will be sign extended into 4 bytes. If the size is 8,
  236. the value will be truncated into 4 bytes.
  237. 2.2.7 BTF_KIND_FWD
  238. ~~~~~~~~~~~~~~~~~~
  239. ``struct btf_type`` encoding requirement:
  240. * ``name_off``: offset to a valid C identifier
  241. * ``info.kind_flag``: 0 for struct, 1 for union
  242. * ``info.kind``: BTF_KIND_FWD
  243. * ``info.vlen``: 0
  244. * ``type``: 0
  245. No additional type data follow ``btf_type``.
  246. 2.2.8 BTF_KIND_TYPEDEF
  247. ~~~~~~~~~~~~~~~~~~~~~~
  248. ``struct btf_type`` encoding requirement:
  249. * ``name_off``: offset to a valid C identifier
  250. * ``info.kind_flag``: 0
  251. * ``info.kind``: BTF_KIND_TYPEDEF
  252. * ``info.vlen``: 0
  253. * ``type``: the type which can be referred by name at ``name_off``
  254. No additional type data follow ``btf_type``.
  255. 2.2.9 BTF_KIND_VOLATILE
  256. ~~~~~~~~~~~~~~~~~~~~~~~
  257. ``struct btf_type`` encoding requirement:
  258. * ``name_off``: 0
  259. * ``info.kind_flag``: 0
  260. * ``info.kind``: BTF_KIND_VOLATILE
  261. * ``info.vlen``: 0
  262. * ``type``: the type with ``volatile`` qualifier
  263. No additional type data follow ``btf_type``.
  264. 2.2.10 BTF_KIND_CONST
  265. ~~~~~~~~~~~~~~~~~~~~~
  266. ``struct btf_type`` encoding requirement:
  267. * ``name_off``: 0
  268. * ``info.kind_flag``: 0
  269. * ``info.kind``: BTF_KIND_CONST
  270. * ``info.vlen``: 0
  271. * ``type``: the type with ``const`` qualifier
  272. No additional type data follow ``btf_type``.
  273. 2.2.11 BTF_KIND_RESTRICT
  274. ~~~~~~~~~~~~~~~~~~~~~~~~
  275. ``struct btf_type`` encoding requirement:
  276. * ``name_off``: 0
  277. * ``info.kind_flag``: 0
  278. * ``info.kind``: BTF_KIND_RESTRICT
  279. * ``info.vlen``: 0
  280. * ``type``: the type with ``restrict`` qualifier
  281. No additional type data follow ``btf_type``.
  282. 2.2.12 BTF_KIND_FUNC
  283. ~~~~~~~~~~~~~~~~~~~~
  284. ``struct btf_type`` encoding requirement:
  285. * ``name_off``: offset to a valid C identifier
  286. * ``info.kind_flag``: 0
  287. * ``info.kind``: BTF_KIND_FUNC
  288. * ``info.vlen``: linkage information (BTF_FUNC_STATIC, BTF_FUNC_GLOBAL
  289. or BTF_FUNC_EXTERN - see :ref:`BTF_Function_Linkage_Constants`)
  290. * ``type``: a BTF_KIND_FUNC_PROTO type
  291. No additional type data follow ``btf_type``.
  292. A BTF_KIND_FUNC defines not a type, but a subprogram (function) whose
  293. signature is defined by ``type``. The subprogram is thus an instance of that
  294. type. The BTF_KIND_FUNC may in turn be referenced by a func_info in the
  295. :ref:`BTF_Ext_Section` (ELF) or in the arguments to :ref:`BPF_Prog_Load`
  296. (ABI).
  297. Currently, only linkage values of BTF_FUNC_STATIC and BTF_FUNC_GLOBAL are
  298. supported in the kernel.
  299. 2.2.13 BTF_KIND_FUNC_PROTO
  300. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  301. ``struct btf_type`` encoding requirement:
  302. * ``name_off``: 0
  303. * ``info.kind_flag``: 0
  304. * ``info.kind``: BTF_KIND_FUNC_PROTO
  305. * ``info.vlen``: # of parameters
  306. * ``type``: the return type
  307. ``btf_type`` is followed by ``info.vlen`` number of ``struct btf_param``.::
  308. struct btf_param {
  309. __u32 name_off;
  310. __u32 type;
  311. };
  312. If a BTF_KIND_FUNC_PROTO type is referred by a BTF_KIND_FUNC type, then
  313. ``btf_param.name_off`` must point to a valid C identifier except for the
  314. possible last argument representing the variable argument. The btf_param.type
  315. refers to parameter type.
  316. If the function has variable arguments, the last parameter is encoded with
  317. ``name_off = 0`` and ``type = 0``.
  318. 2.2.14 BTF_KIND_VAR
  319. ~~~~~~~~~~~~~~~~~~~
  320. ``struct btf_type`` encoding requirement:
  321. * ``name_off``: offset to a valid C identifier
  322. * ``info.kind_flag``: 0
  323. * ``info.kind``: BTF_KIND_VAR
  324. * ``info.vlen``: 0
  325. * ``type``: the type of the variable
  326. ``btf_type`` is followed by a single ``struct btf_variable`` with the
  327. following data::
  328. struct btf_var {
  329. __u32 linkage;
  330. };
  331. ``btf_var.linkage`` may take the values: BTF_VAR_STATIC, BTF_VAR_GLOBAL_ALLOCATED or BTF_VAR_GLOBAL_EXTERN -
  332. see :ref:`BTF_Var_Linkage_Constants`.
  333. Not all type of global variables are supported by LLVM at this point.
  334. The following is currently available:
  335. * static variables with or without section attributes
  336. * global variables with section attributes
  337. The latter is for future extraction of map key/value type id's from a
  338. map definition.
  339. 2.2.15 BTF_KIND_DATASEC
  340. ~~~~~~~~~~~~~~~~~~~~~~~
  341. ``struct btf_type`` encoding requirement:
  342. * ``name_off``: offset to a valid name associated with a variable or
  343. one of .data/.bss/.rodata
  344. * ``info.kind_flag``: 0
  345. * ``info.kind``: BTF_KIND_DATASEC
  346. * ``info.vlen``: # of variables
  347. * ``size``: total section size in bytes (0 at compilation time, patched
  348. to actual size by BPF loaders such as libbpf)
  349. ``btf_type`` is followed by ``info.vlen`` number of ``struct btf_var_secinfo``.::
  350. struct btf_var_secinfo {
  351. __u32 type;
  352. __u32 offset;
  353. __u32 size;
  354. };
  355. ``struct btf_var_secinfo`` encoding:
  356. * ``type``: the type of the BTF_KIND_VAR variable
  357. * ``offset``: the in-section offset of the variable
  358. * ``size``: the size of the variable in bytes
  359. 2.2.16 BTF_KIND_FLOAT
  360. ~~~~~~~~~~~~~~~~~~~~~
  361. ``struct btf_type`` encoding requirement:
  362. * ``name_off``: any valid offset
  363. * ``info.kind_flag``: 0
  364. * ``info.kind``: BTF_KIND_FLOAT
  365. * ``info.vlen``: 0
  366. * ``size``: the size of the float type in bytes: 2, 4, 8, 12 or 16.
  367. No additional type data follow ``btf_type``.
  368. 2.2.17 BTF_KIND_DECL_TAG
  369. ~~~~~~~~~~~~~~~~~~~~~~~~
  370. ``struct btf_type`` encoding requirement:
  371. * ``name_off``: offset to a non-empty string
  372. * ``info.kind_flag``: 0
  373. * ``info.kind``: BTF_KIND_DECL_TAG
  374. * ``info.vlen``: 0
  375. * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef``
  376. ``btf_type`` is followed by ``struct btf_decl_tag``.::
  377. struct btf_decl_tag {
  378. __u32 component_idx;
  379. };
  380. The ``name_off`` encodes btf_decl_tag attribute string.
  381. The ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``.
  382. For ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``.
  383. For the other three types, if the btf_decl_tag attribute is
  384. applied to the ``struct``, ``union`` or ``func`` itself,
  385. ``btf_decl_tag.component_idx`` must be ``-1``. Otherwise,
  386. the attribute is applied to a ``struct``/``union`` member or
  387. a ``func`` argument, and ``btf_decl_tag.component_idx`` should be a
  388. valid index (starting from 0) pointing to a member or an argument.
  389. 2.2.18 BTF_KIND_TYPE_TAG
  390. ~~~~~~~~~~~~~~~~~~~~~~~~
  391. ``struct btf_type`` encoding requirement:
  392. * ``name_off``: offset to a non-empty string
  393. * ``info.kind_flag``: 0
  394. * ``info.kind``: BTF_KIND_TYPE_TAG
  395. * ``info.vlen``: 0
  396. * ``type``: the type with ``btf_type_tag`` attribute
  397. Currently, ``BTF_KIND_TYPE_TAG`` is only emitted for pointer types.
  398. It has the following btf type chain:
  399. ::
  400. ptr -> [type_tag]*
  401. -> [const | volatile | restrict | typedef]*
  402. -> base_type
  403. Basically, a pointer type points to zero or more
  404. type_tag, then zero or more const/volatile/restrict/typedef
  405. and finally the base type. The base type is one of
  406. int, ptr, array, struct, union, enum, func_proto and float types.
  407. 2.2.19 BTF_KIND_ENUM64
  408. ~~~~~~~~~~~~~~~~~~~~~~
  409. ``struct btf_type`` encoding requirement:
  410. * ``name_off``: 0 or offset to a valid C identifier
  411. * ``info.kind_flag``: 0 for unsigned, 1 for signed
  412. * ``info.kind``: BTF_KIND_ENUM64
  413. * ``info.vlen``: number of enum values
  414. * ``size``: 1/2/4/8
  415. ``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
  416. struct btf_enum64 {
  417. __u32 name_off;
  418. __u32 val_lo32;
  419. __u32 val_hi32;
  420. };
  421. The ``btf_enum64`` encoding:
  422. * ``name_off``: offset to a valid C identifier
  423. * ``val_lo32``: lower 32-bit value for a 64-bit value
  424. * ``val_hi32``: high 32-bit value for a 64-bit value
  425. If the original enum value is signed and the size is less than 8,
  426. that value will be sign extended into 8 bytes.
  427. 2.3 Constant Values
  428. -------------------
  429. .. _BTF_Function_Linkage_Constants:
  430. 2.3.1 Function Linkage Constant Values
  431. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  432. .. table:: Function Linkage Values and Meanings
  433. =================== ===== ===========
  434. kind value description
  435. =================== ===== ===========
  436. ``BTF_FUNC_STATIC`` 0x0 definition of subprogram not visible outside containing compilation unit
  437. ``BTF_FUNC_GLOBAL`` 0x1 definition of subprogram visible outside containing compilation unit
  438. ``BTF_FUNC_EXTERN`` 0x2 declaration of a subprogram whose definition is outside the containing compilation unit
  439. =================== ===== ===========
  440. .. _BTF_Var_Linkage_Constants:
  441. 2.3.2 Variable Linkage Constant Values
  442. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  443. .. table:: Variable Linkage Values and Meanings
  444. ============================ ===== ===========
  445. kind value description
  446. ============================ ===== ===========
  447. ``BTF_VAR_STATIC`` 0x0 definition of global variable not visible outside containing compilation unit
  448. ``BTF_VAR_GLOBAL_ALLOCATED`` 0x1 definition of global variable visible outside containing compilation unit
  449. ``BTF_VAR_GLOBAL_EXTERN`` 0x2 declaration of global variable whose definition is outside the containing compilation unit
  450. ============================ ===== ===========
  451. 3. BTF Kernel API
  452. =================
  453. The following bpf syscall command involves BTF:
  454. * BPF_BTF_LOAD: load a blob of BTF data into kernel
  455. * BPF_MAP_CREATE: map creation with btf key and value type info.
  456. * BPF_PROG_LOAD: prog load with btf function and line info.
  457. * BPF_BTF_GET_FD_BY_ID: get a btf fd
  458. * BPF_OBJ_GET_INFO_BY_FD: btf, func_info, line_info
  459. and other btf related info are returned.
  460. The workflow typically looks like:
  461. ::
  462. Application:
  463. BPF_BTF_LOAD
  464. |
  465. v
  466. BPF_MAP_CREATE and BPF_PROG_LOAD
  467. |
  468. V
  469. ......
  470. Introspection tool:
  471. ......
  472. BPF_{PROG,MAP}_GET_NEXT_ID (get prog/map id's)
  473. |
  474. V
  475. BPF_{PROG,MAP}_GET_FD_BY_ID (get a prog/map fd)
  476. |
  477. V
  478. BPF_OBJ_GET_INFO_BY_FD (get bpf_prog_info/bpf_map_info with btf_id)
  479. | |
  480. V |
  481. BPF_BTF_GET_FD_BY_ID (get btf_fd) |
  482. | |
  483. V |
  484. BPF_OBJ_GET_INFO_BY_FD (get btf) |
  485. | |
  486. V V
  487. pretty print types, dump func signatures and line info, etc.
  488. 3.1 BPF_BTF_LOAD
  489. ----------------
  490. Load a blob of BTF data into kernel. A blob of data, described in
  491. :ref:`BTF_Type_String`, can be directly loaded into the kernel. A ``btf_fd``
  492. is returned to a userspace.
  493. 3.2 BPF_MAP_CREATE
  494. ------------------
  495. A map can be created with ``btf_fd`` and specified key/value type id.::
  496. __u32 btf_fd; /* fd pointing to a BTF type data */
  497. __u32 btf_key_type_id; /* BTF type_id of the key */
  498. __u32 btf_value_type_id; /* BTF type_id of the value */
  499. In libbpf, the map can be defined with extra annotation like below:
  500. ::
  501. struct {
  502. __uint(type, BPF_MAP_TYPE_ARRAY);
  503. __type(key, int);
  504. __type(value, struct ipv_counts);
  505. __uint(max_entries, 4);
  506. } btf_map SEC(".maps");
  507. During ELF parsing, libbpf is able to extract key/value type_id's and assign
  508. them to BPF_MAP_CREATE attributes automatically.
  509. .. _BPF_Prog_Load:
  510. 3.3 BPF_PROG_LOAD
  511. -----------------
  512. During prog_load, func_info and line_info can be passed to kernel with proper
  513. values for the following attributes:
  514. ::
  515. __u32 insn_cnt;
  516. __aligned_u64 insns;
  517. ......
  518. __u32 prog_btf_fd; /* fd pointing to BTF type data */
  519. __u32 func_info_rec_size; /* userspace bpf_func_info size */
  520. __aligned_u64 func_info; /* func info */
  521. __u32 func_info_cnt; /* number of bpf_func_info records */
  522. __u32 line_info_rec_size; /* userspace bpf_line_info size */
  523. __aligned_u64 line_info; /* line info */
  524. __u32 line_info_cnt; /* number of bpf_line_info records */
  525. The func_info and line_info are an array of below, respectively.::
  526. struct bpf_func_info {
  527. __u32 insn_off; /* [0, insn_cnt - 1] */
  528. __u32 type_id; /* pointing to a BTF_KIND_FUNC type */
  529. };
  530. struct bpf_line_info {
  531. __u32 insn_off; /* [0, insn_cnt - 1] */
  532. __u32 file_name_off; /* offset to string table for the filename */
  533. __u32 line_off; /* offset to string table for the source line */
  534. __u32 line_col; /* line number and column number */
  535. };
  536. func_info_rec_size is the size of each func_info record, and
  537. line_info_rec_size is the size of each line_info record. Passing the record
  538. size to kernel make it possible to extend the record itself in the future.
  539. Below are requirements for func_info:
  540. * func_info[0].insn_off must be 0.
  541. * the func_info insn_off is in strictly increasing order and matches
  542. bpf func boundaries.
  543. Below are requirements for line_info:
  544. * the first insn in each func must have a line_info record pointing to it.
  545. * the line_info insn_off is in strictly increasing order.
  546. For line_info, the line number and column number are defined as below:
  547. ::
  548. #define BPF_LINE_INFO_LINE_NUM(line_col) ((line_col) >> 10)
  549. #define BPF_LINE_INFO_LINE_COL(line_col) ((line_col) & 0x3ff)
  550. 3.4 BPF_{PROG,MAP}_GET_NEXT_ID
  551. ------------------------------
  552. In kernel, every loaded program, map or btf has a unique id. The id won't
  553. change during the lifetime of a program, map, or btf.
  554. The bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID returns all id's, one for
  555. each command, to user space, for bpf program or maps, respectively, so an
  556. inspection tool can inspect all programs and maps.
  557. 3.5 BPF_{PROG,MAP}_GET_FD_BY_ID
  558. -------------------------------
  559. An introspection tool cannot use id to get details about program or maps.
  560. A file descriptor needs to be obtained first for reference-counting purpose.
  561. 3.6 BPF_OBJ_GET_INFO_BY_FD
  562. --------------------------
  563. Once a program/map fd is acquired, an introspection tool can get the detailed
  564. information from kernel about this fd, some of which are BTF-related. For
  565. example, ``bpf_map_info`` returns ``btf_id`` and key/value type ids.
  566. ``bpf_prog_info`` returns ``btf_id``, func_info, and line info for translated
  567. bpf byte codes, and jited_line_info.
  568. 3.7 BPF_BTF_GET_FD_BY_ID
  569. ------------------------
  570. With ``btf_id`` obtained in ``bpf_map_info`` and ``bpf_prog_info``, bpf
  571. syscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd. Then, with
  572. command BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally loaded into the
  573. kernel with BPF_BTF_LOAD, can be retrieved.
  574. With the btf blob, ``bpf_map_info``, and ``bpf_prog_info``, an introspection
  575. tool has full btf knowledge and is able to pretty print map key/values, dump
  576. func signatures and line info, along with byte/jit codes.
  577. 4. ELF File Format Interface
  578. ============================
  579. 4.1 .BTF section
  580. ----------------
  581. The .BTF section contains type and string data. The format of this section is
  582. same as the one describe in :ref:`BTF_Type_String`.
  583. .. _BTF_Ext_Section:
  584. 4.2 .BTF.ext section
  585. --------------------
  586. The .BTF.ext section encodes func_info, line_info and CO-RE relocations
  587. which needs loader manipulation before loading into the kernel.
  588. The specification for .BTF.ext section is defined at ``tools/lib/bpf/btf.h``
  589. and ``tools/lib/bpf/btf.c``.
  590. The current header of .BTF.ext section::
  591. struct btf_ext_header {
  592. __u16 magic;
  593. __u8 version;
  594. __u8 flags;
  595. __u32 hdr_len;
  596. /* All offsets are in bytes relative to the end of this header */
  597. __u32 func_info_off;
  598. __u32 func_info_len;
  599. __u32 line_info_off;
  600. __u32 line_info_len;
  601. /* optional part of .BTF.ext header */
  602. __u32 core_relo_off;
  603. __u32 core_relo_len;
  604. };
  605. It is very similar to .BTF section. Instead of type/string section, it
  606. contains func_info, line_info and core_relo sub-sections.
  607. See :ref:`BPF_Prog_Load` for details about func_info and line_info
  608. record format.
  609. The func_info is organized as below.::
  610. func_info_rec_size /* __u32 value */
  611. btf_ext_info_sec for section #1 /* func_info for section #1 */
  612. btf_ext_info_sec for section #2 /* func_info for section #2 */
  613. ...
  614. ``func_info_rec_size`` specifies the size of ``bpf_func_info`` structure when
  615. .BTF.ext is generated. ``btf_ext_info_sec``, defined below, is a collection of
  616. func_info for each specific ELF section.::
  617. struct btf_ext_info_sec {
  618. __u32 sec_name_off; /* offset to section name */
  619. __u32 num_info;
  620. /* Followed by num_info * record_size number of bytes */
  621. __u8 data[0];
  622. };
  623. Here, num_info must be greater than 0.
  624. The line_info is organized as below.::
  625. line_info_rec_size /* __u32 value */
  626. btf_ext_info_sec for section #1 /* line_info for section #1 */
  627. btf_ext_info_sec for section #2 /* line_info for section #2 */
  628. ...
  629. ``line_info_rec_size`` specifies the size of ``bpf_line_info`` structure when
  630. .BTF.ext is generated.
  631. The interpretation of ``bpf_func_info->insn_off`` and
  632. ``bpf_line_info->insn_off`` is different between kernel API and ELF API. For
  633. kernel API, the ``insn_off`` is the instruction offset in the unit of ``struct
  634. bpf_insn``. For ELF API, the ``insn_off`` is the byte offset from the
  635. beginning of section (``btf_ext_info_sec->sec_name_off``).
  636. The core_relo is organized as below.::
  637. core_relo_rec_size /* __u32 value */
  638. btf_ext_info_sec for section #1 /* core_relo for section #1 */
  639. btf_ext_info_sec for section #2 /* core_relo for section #2 */
  640. ``core_relo_rec_size`` specifies the size of ``bpf_core_relo``
  641. structure when .BTF.ext is generated. All ``bpf_core_relo`` structures
  642. within a single ``btf_ext_info_sec`` describe relocations applied to
  643. section named by ``btf_ext_info_sec->sec_name_off``.
  644. See :ref:`Documentation/bpf/llvm_reloc.rst <btf-co-re-relocations>`
  645. for more information on CO-RE relocations.
  646. 4.2 .BTF_ids section
  647. --------------------
  648. The .BTF_ids section encodes BTF ID values that are used within the kernel.
  649. This section is created during the kernel compilation with the help of
  650. macros defined in ``include/linux/btf_ids.h`` header file. Kernel code can
  651. use them to create lists and sets (sorted lists) of BTF ID values.
  652. The ``BTF_ID_LIST`` and ``BTF_ID`` macros define unsorted list of BTF ID values,
  653. with following syntax::
  654. BTF_ID_LIST(list)
  655. BTF_ID(type1, name1)
  656. BTF_ID(type2, name2)
  657. resulting in following layout in .BTF_ids section::
  658. __BTF_ID__type1__name1__1:
  659. .zero 4
  660. __BTF_ID__type2__name2__2:
  661. .zero 4
  662. The ``u32 list[];`` variable is defined to access the list.
  663. The ``BTF_ID_UNUSED`` macro defines 4 zero bytes. It's used when we
  664. want to define unused entry in BTF_ID_LIST, like::
  665. BTF_ID_LIST(bpf_skb_output_btf_ids)
  666. BTF_ID(struct, sk_buff)
  667. BTF_ID_UNUSED
  668. BTF_ID(struct, task_struct)
  669. The ``BTF_SET_START/END`` macros pair defines sorted list of BTF ID values
  670. and their count, with following syntax::
  671. BTF_SET_START(set)
  672. BTF_ID(type1, name1)
  673. BTF_ID(type2, name2)
  674. BTF_SET_END(set)
  675. resulting in following layout in .BTF_ids section::
  676. __BTF_ID__set__set:
  677. .zero 4
  678. __BTF_ID__type1__name1__3:
  679. .zero 4
  680. __BTF_ID__type2__name2__4:
  681. .zero 4
  682. The ``struct btf_id_set set;`` variable is defined to access the list.
  683. The ``typeX`` name can be one of following::
  684. struct, union, typedef, func
  685. and is used as a filter when resolving the BTF ID value.
  686. All the BTF ID lists and sets are compiled in the .BTF_ids section and
  687. resolved during the linking phase of kernel build by ``resolve_btfids`` tool.
  688. 5. Using BTF
  689. ============
  690. 5.1 bpftool map pretty print
  691. ----------------------------
  692. With BTF, the map key/value can be printed based on fields rather than simply
  693. raw bytes. This is especially valuable for large structure or if your data
  694. structure has bitfields. For example, for the following map,::
  695. enum A { A1, A2, A3, A4, A5 };
  696. typedef enum A ___A;
  697. struct tmp_t {
  698. char a1:4;
  699. int a2:4;
  700. int :4;
  701. __u32 a3:4;
  702. int b;
  703. ___A b1:4;
  704. enum A b2:4;
  705. };
  706. struct {
  707. __uint(type, BPF_MAP_TYPE_ARRAY);
  708. __type(key, int);
  709. __type(value, struct tmp_t);
  710. __uint(max_entries, 1);
  711. } tmpmap SEC(".maps");
  712. bpftool is able to pretty print like below:
  713. ::
  714. [{
  715. "key": 0,
  716. "value": {
  717. "a1": 0x2,
  718. "a2": 0x4,
  719. "a3": 0x6,
  720. "b": 7,
  721. "b1": 0x8,
  722. "b2": 0xa
  723. }
  724. }
  725. ]
  726. 5.2 bpftool prog dump
  727. ---------------------
  728. The following is an example showing how func_info and line_info can help prog
  729. dump with better kernel symbol names, function prototypes and line
  730. information.::
  731. $ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
  732. [...]
  733. int test_long_fname_2(struct dummy_tracepoint_args * arg):
  734. bpf_prog_44a040bf25481309_test_long_fname_2:
  735. ; static int test_long_fname_2(struct dummy_tracepoint_args *arg)
  736. 0: push %rbp
  737. 1: mov %rsp,%rbp
  738. 4: sub $0x30,%rsp
  739. b: sub $0x28,%rbp
  740. f: mov %rbx,0x0(%rbp)
  741. 13: mov %r13,0x8(%rbp)
  742. 17: mov %r14,0x10(%rbp)
  743. 1b: mov %r15,0x18(%rbp)
  744. 1f: xor %eax,%eax
  745. 21: mov %rax,0x20(%rbp)
  746. 25: xor %esi,%esi
  747. ; int key = 0;
  748. 27: mov %esi,-0x4(%rbp)
  749. ; if (!arg->sock)
  750. 2a: mov 0x8(%rdi),%rdi
  751. ; if (!arg->sock)
  752. 2e: cmp $0x0,%rdi
  753. 32: je 0x0000000000000070
  754. 34: mov %rbp,%rsi
  755. ; counts = bpf_map_lookup_elem(&btf_map, &key);
  756. [...]
  757. 5.3 Verifier Log
  758. ----------------
  759. The following is an example of how line_info can help debugging verification
  760. failure.::
  761. /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c
  762. * is modified as below.
  763. */
  764. data = (void *)(long)xdp->data;
  765. data_end = (void *)(long)xdp->data_end;
  766. /*
  767. if (data + 4 > data_end)
  768. return XDP_DROP;
  769. */
  770. *(u32 *)data = dst->dst;
  771. $ bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp
  772. ; data = (void *)(long)xdp->data;
  773. 224: (79) r2 = *(u64 *)(r10 -112)
  774. 225: (61) r2 = *(u32 *)(r2 +0)
  775. ; *(u32 *)data = dst->dst;
  776. 226: (63) *(u32 *)(r2 +0) = r1
  777. invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0)
  778. R2 offset is outside of the packet
  779. 6. BTF Generation
  780. =================
  781. You need latest pahole
  782. https://git.kernel.org/pub/scm/devel/pahole/pahole.git/
  783. or llvm (8.0 or later). The pahole acts as a dwarf2btf converter. It doesn't
  784. support .BTF.ext and btf BTF_KIND_FUNC type yet. For example,::
  785. -bash-4.4$ cat t.c
  786. struct t {
  787. int a:2;
  788. int b:3;
  789. int c:2;
  790. } g;
  791. -bash-4.4$ gcc -c -O2 -g t.c
  792. -bash-4.4$ pahole -JV t.o
  793. File t.o:
  794. [1] STRUCT t kind_flag=1 size=4 vlen=3
  795. a type_id=2 bitfield_size=2 bits_offset=0
  796. b type_id=2 bitfield_size=3 bits_offset=2
  797. c type_id=2 bitfield_size=2 bits_offset=5
  798. [2] INT int size=4 bit_offset=0 nr_bits=32 encoding=SIGNED
  799. The llvm is able to generate .BTF and .BTF.ext directly with -g for bpf target
  800. only. The assembly code (-S) is able to show the BTF encoding in assembly
  801. format.::
  802. -bash-4.4$ cat t2.c
  803. typedef int __int32;
  804. struct t2 {
  805. int a2;
  806. int (*f2)(char q1, __int32 q2, ...);
  807. int (*f3)();
  808. } g2;
  809. int main() { return 0; }
  810. int test() { return 0; }
  811. -bash-4.4$ clang -c -g -O2 --target=bpf t2.c
  812. -bash-4.4$ readelf -S t2.o
  813. ......
  814. [ 8] .BTF PROGBITS 0000000000000000 00000247
  815. 000000000000016e 0000000000000000 0 0 1
  816. [ 9] .BTF.ext PROGBITS 0000000000000000 000003b5
  817. 0000000000000060 0000000000000000 0 0 1
  818. [10] .rel.BTF.ext REL 0000000000000000 000007e0
  819. 0000000000000040 0000000000000010 16 9 8
  820. ......
  821. -bash-4.4$ clang -S -g -O2 --target=bpf t2.c
  822. -bash-4.4$ cat t2.s
  823. ......
  824. .section .BTF,"",@progbits
  825. .short 60319 # 0xeb9f
  826. .byte 1
  827. .byte 0
  828. .long 24
  829. .long 0
  830. .long 220
  831. .long 220
  832. .long 122
  833. .long 0 # BTF_KIND_FUNC_PROTO(id = 1)
  834. .long 218103808 # 0xd000000
  835. .long 2
  836. .long 83 # BTF_KIND_INT(id = 2)
  837. .long 16777216 # 0x1000000
  838. .long 4
  839. .long 16777248 # 0x1000020
  840. ......
  841. .byte 0 # string offset=0
  842. .ascii ".text" # string offset=1
  843. .byte 0
  844. .ascii "/home/yhs/tmp-pahole/t2.c" # string offset=7
  845. .byte 0
  846. .ascii "int main() { return 0; }" # string offset=33
  847. .byte 0
  848. .ascii "int test() { return 0; }" # string offset=58
  849. .byte 0
  850. .ascii "int" # string offset=83
  851. ......
  852. .section .BTF.ext,"",@progbits
  853. .short 60319 # 0xeb9f
  854. .byte 1
  855. .byte 0
  856. .long 24
  857. .long 0
  858. .long 28
  859. .long 28
  860. .long 44
  861. .long 8 # FuncInfo
  862. .long 1 # FuncInfo section string offset=1
  863. .long 2
  864. .long .Lfunc_begin0
  865. .long 3
  866. .long .Lfunc_begin1
  867. .long 5
  868. .long 16 # LineInfo
  869. .long 1 # LineInfo section string offset=1
  870. .long 2
  871. .long .Ltmp0
  872. .long 7
  873. .long 33
  874. .long 7182 # Line 7 Col 14
  875. .long .Ltmp3
  876. .long 7
  877. .long 58
  878. .long 8206 # Line 8 Col 14
  879. 7. Testing
  880. ==========
  881. The kernel BPF selftest `tools/testing/selftests/bpf/prog_tests/btf.c`_
  882. provides an extensive set of BTF-related tests.
  883. .. Links
  884. .. _tools/testing/selftests/bpf/prog_tests/btf.c:
  885. https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/btf.c