btf.rst 41 KB

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