btf.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /* Copyright (c) 2018 Facebook */
  3. /*! \file */
  4. #ifndef __LIBBPF_BTF_H
  5. #define __LIBBPF_BTF_H
  6. #include <stdarg.h>
  7. #include <stdbool.h>
  8. #include <linux/btf.h>
  9. #include <linux/types.h>
  10. #include "libbpf_common.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #define BTF_ELF_SEC ".BTF"
  15. #define BTF_EXT_ELF_SEC ".BTF.ext"
  16. #define BTF_BASE_ELF_SEC ".BTF.base"
  17. #define MAPS_ELF_SEC ".maps"
  18. struct btf;
  19. struct btf_ext;
  20. struct btf_type;
  21. struct bpf_object;
  22. enum btf_endianness {
  23. BTF_LITTLE_ENDIAN = 0,
  24. BTF_BIG_ENDIAN = 1,
  25. };
  26. /**
  27. * @brief **btf__free()** frees all data of a BTF object
  28. * @param btf BTF object to free
  29. */
  30. LIBBPF_API void btf__free(struct btf *btf);
  31. /**
  32. * @brief **btf__new()** creates a new instance of a BTF object from the raw
  33. * bytes of an ELF's BTF section
  34. * @param data raw bytes
  35. * @param size number of bytes passed in `data`
  36. * @return new BTF object instance which has to be eventually freed with
  37. * **btf__free()**
  38. *
  39. * On error, error-code-encoded-as-pointer is returned, not a NULL. To extract
  40. * error code from such a pointer `libbpf_get_error()` should be used. If
  41. * `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is
  42. * returned on error instead. In both cases thread-local `errno` variable is
  43. * always set to error code as well.
  44. */
  45. LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
  46. /**
  47. * @brief **btf__new_split()** create a new instance of a BTF object from the
  48. * provided raw data bytes. It takes another BTF instance, **base_btf**, which
  49. * serves as a base BTF, which is extended by types in a newly created BTF
  50. * instance
  51. * @param data raw bytes
  52. * @param size length of raw bytes
  53. * @param base_btf the base BTF object
  54. * @return new BTF object instance which has to be eventually freed with
  55. * **btf__free()**
  56. *
  57. * If *base_btf* is NULL, `btf__new_split()` is equivalent to `btf__new()` and
  58. * creates non-split BTF.
  59. *
  60. * On error, error-code-encoded-as-pointer is returned, not a NULL. To extract
  61. * error code from such a pointer `libbpf_get_error()` should be used. If
  62. * `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is
  63. * returned on error instead. In both cases thread-local `errno` variable is
  64. * always set to error code as well.
  65. */
  66. LIBBPF_API struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf);
  67. /**
  68. * @brief **btf__new_empty()** creates an empty BTF object. Use
  69. * `btf__add_*()` to populate such BTF object.
  70. * @return new BTF object instance which has to be eventually freed with
  71. * **btf__free()**
  72. *
  73. * On error, error-code-encoded-as-pointer is returned, not a NULL. To extract
  74. * error code from such a pointer `libbpf_get_error()` should be used. If
  75. * `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is
  76. * returned on error instead. In both cases thread-local `errno` variable is
  77. * always set to error code as well.
  78. */
  79. LIBBPF_API struct btf *btf__new_empty(void);
  80. /**
  81. * @brief **btf__new_empty_split()** creates an unpopulated BTF object from an
  82. * ELF BTF section except with a base BTF on top of which split BTF should be
  83. * based
  84. * @param base_btf base BTF object
  85. * @return new BTF object instance which has to be eventually freed with
  86. * **btf__free()**
  87. *
  88. * If *base_btf* is NULL, `btf__new_empty_split()` is equivalent to
  89. * `btf__new_empty()` and creates non-split BTF.
  90. *
  91. * On error, error-code-encoded-as-pointer is returned, not a NULL. To extract
  92. * error code from such a pointer `libbpf_get_error()` should be used. If
  93. * `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is
  94. * returned on error instead. In both cases thread-local `errno` variable is
  95. * always set to error code as well.
  96. */
  97. LIBBPF_API struct btf *btf__new_empty_split(struct btf *base_btf);
  98. /**
  99. * @brief **btf__distill_base()** creates new versions of the split BTF
  100. * *src_btf* and its base BTF. The new base BTF will only contain the types
  101. * needed to improve robustness of the split BTF to small changes in base BTF.
  102. * When that split BTF is loaded against a (possibly changed) base, this
  103. * distilled base BTF will help update references to that (possibly changed)
  104. * base BTF.
  105. * @param src_btf source split BTF object
  106. * @param new_base_btf pointer to where the new base BTF object pointer will be stored
  107. * @param new_split_btf pointer to where the new split BTF object pointer will be stored
  108. * @return 0 on success; negative error code, otherwise
  109. *
  110. * Both the new split and its associated new base BTF must be freed by
  111. * the caller.
  112. *
  113. * If successful, 0 is returned and **new_base_btf** and **new_split_btf**
  114. * will point at new base/split BTF. Both the new split and its associated
  115. * new base BTF must be freed by the caller.
  116. *
  117. * A negative value is returned on error and the thread-local `errno` variable
  118. * is set to the error code as well.
  119. */
  120. LIBBPF_API int btf__distill_base(const struct btf *src_btf, struct btf **new_base_btf,
  121. struct btf **new_split_btf);
  122. LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
  123. LIBBPF_API struct btf *btf__parse_split(const char *path, struct btf *base_btf);
  124. LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
  125. LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf);
  126. LIBBPF_API struct btf *btf__parse_raw(const char *path);
  127. LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf);
  128. LIBBPF_API struct btf *btf__load_vmlinux_btf(void);
  129. LIBBPF_API struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf);
  130. LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id);
  131. LIBBPF_API struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf);
  132. LIBBPF_API int btf__load_into_kernel(struct btf *btf);
  133. LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
  134. const char *type_name);
  135. LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
  136. const char *type_name, __u32 kind);
  137. LIBBPF_API __u32 btf__type_cnt(const struct btf *btf);
  138. LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf);
  139. LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
  140. __u32 id);
  141. LIBBPF_API size_t btf__pointer_size(const struct btf *btf);
  142. LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz);
  143. LIBBPF_API enum btf_endianness btf__endianness(const struct btf *btf);
  144. LIBBPF_API int btf__set_endianness(struct btf *btf, enum btf_endianness endian);
  145. LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
  146. LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
  147. LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
  148. LIBBPF_API int btf__fd(const struct btf *btf);
  149. LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
  150. LIBBPF_API const void *btf__raw_data(const struct btf *btf, __u32 *size);
  151. LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
  152. LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
  153. LIBBPF_API struct btf_ext *btf_ext__new(const __u8 *data, __u32 size);
  154. LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
  155. LIBBPF_API const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size);
  156. LIBBPF_API enum btf_endianness btf_ext__endianness(const struct btf_ext *btf_ext);
  157. LIBBPF_API int btf_ext__set_endianness(struct btf_ext *btf_ext,
  158. enum btf_endianness endian);
  159. LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
  160. LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
  161. LIBBPF_API int btf__add_type(struct btf *btf, const struct btf *src_btf,
  162. const struct btf_type *src_type);
  163. /**
  164. * @brief **btf__add_btf()** appends all the BTF types from *src_btf* into *btf*
  165. * @param btf BTF object which all the BTF types and strings are added to
  166. * @param src_btf BTF object which all BTF types and referenced strings are copied from
  167. * @return BTF type ID of the first appended BTF type, or negative error code
  168. *
  169. * **btf__add_btf()** can be used to simply and efficiently append the entire
  170. * contents of one BTF object to another one. All the BTF type data is copied
  171. * over, all referenced type IDs are adjusted by adding a necessary ID offset.
  172. * Only strings referenced from BTF types are copied over and deduplicated, so
  173. * if there were some unused strings in *src_btf*, those won't be copied over,
  174. * which is consistent with the general string deduplication semantics of BTF
  175. * writing APIs.
  176. *
  177. * If any error is encountered during this process, the contents of *btf* is
  178. * left intact, which means that **btf__add_btf()** follows the transactional
  179. * semantics and the operation as a whole is all-or-nothing.
  180. *
  181. * *src_btf* has to be non-split BTF, as of now copying types from split BTF
  182. * is not supported and will result in -ENOTSUP error code returned.
  183. */
  184. LIBBPF_API int btf__add_btf(struct btf *btf, const struct btf *src_btf);
  185. LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding);
  186. LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);
  187. LIBBPF_API int btf__add_ptr(struct btf *btf, int ref_type_id);
  188. LIBBPF_API int btf__add_array(struct btf *btf,
  189. int index_type_id, int elem_type_id, __u32 nr_elems);
  190. /* struct/union construction APIs */
  191. LIBBPF_API int btf__add_struct(struct btf *btf, const char *name, __u32 sz);
  192. LIBBPF_API int btf__add_union(struct btf *btf, const char *name, __u32 sz);
  193. LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_id,
  194. __u32 bit_offset, __u32 bit_size);
  195. /* enum construction APIs */
  196. LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz);
  197. LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value);
  198. LIBBPF_API int btf__add_enum64(struct btf *btf, const char *name, __u32 bytes_sz, bool is_signed);
  199. LIBBPF_API int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value);
  200. enum btf_fwd_kind {
  201. BTF_FWD_STRUCT = 0,
  202. BTF_FWD_UNION = 1,
  203. BTF_FWD_ENUM = 2,
  204. };
  205. LIBBPF_API int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind);
  206. LIBBPF_API int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id);
  207. LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id);
  208. LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id);
  209. LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id);
  210. LIBBPF_API int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id);
  211. LIBBPF_API int btf__add_type_attr(struct btf *btf, const char *value, int ref_type_id);
  212. /* func and func_proto construction APIs */
  213. LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
  214. enum btf_func_linkage linkage, int proto_type_id);
  215. LIBBPF_API int btf__add_func_proto(struct btf *btf, int ret_type_id);
  216. LIBBPF_API int btf__add_func_param(struct btf *btf, const char *name, int type_id);
  217. /* var & datasec construction APIs */
  218. LIBBPF_API int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id);
  219. LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz);
  220. LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
  221. __u32 offset, __u32 byte_sz);
  222. /* tag construction API */
  223. LIBBPF_API int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id,
  224. int component_idx);
  225. LIBBPF_API int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id,
  226. int component_idx);
  227. struct btf_dedup_opts {
  228. size_t sz;
  229. /* optional .BTF.ext info to dedup along the main BTF info */
  230. struct btf_ext *btf_ext;
  231. /* force hash collisions (used for testing) */
  232. bool force_collisions;
  233. size_t :0;
  234. };
  235. #define btf_dedup_opts__last_field force_collisions
  236. LIBBPF_API int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts);
  237. /**
  238. * @brief **btf__relocate()** will check the split BTF *btf* for references
  239. * to base BTF kinds, and verify those references are compatible with
  240. * *base_btf*; if they are, *btf* is adjusted such that is re-parented to
  241. * *base_btf* and type ids and strings are adjusted to accommodate this.
  242. * @param btf split BTF object to relocate
  243. * @param base_btf base BTF object
  244. * @return 0 on success; negative error code, otherwise
  245. *
  246. * If successful, 0 is returned and **btf** now has **base_btf** as its
  247. * base.
  248. *
  249. * A negative value is returned on error and the thread-local `errno` variable
  250. * is set to the error code as well.
  251. */
  252. LIBBPF_API int btf__relocate(struct btf *btf, const struct btf *base_btf);
  253. struct btf_permute_opts {
  254. size_t sz;
  255. /* optional .BTF.ext info along the main BTF info */
  256. struct btf_ext *btf_ext;
  257. size_t :0;
  258. };
  259. #define btf_permute_opts__last_field btf_ext
  260. /**
  261. * @brief **btf__permute()** rearranges BTF types in-place according to a specified ID mapping
  262. * @param btf BTF object to permute
  263. * @param id_map Array mapping original type IDs to new IDs
  264. * @param id_map_cnt Number of elements in @id_map
  265. * @param opts Optional parameters, including BTF extension data for reference updates
  266. * @return 0 on success, negative error code on failure
  267. *
  268. * **btf__permute()** reorders BTF types based on the provided @id_map array,
  269. * updating all internal type references to maintain consistency. The function
  270. * operates in-place, modifying the BTF object directly.
  271. *
  272. * For **base BTF**:
  273. * - @id_map must include all types from ID 0 to `btf__type_cnt(btf) - 1`
  274. * - @id_map_cnt must be `btf__type_cnt(btf)`
  275. * - Mapping is defined as `id_map[original_id] = new_id`
  276. * - `id_map[0]` must be 0 (void type cannot be moved)
  277. *
  278. * For **split BTF**:
  279. * - @id_map must include only split types (types added on top of the base BTF)
  280. * - @id_map_cnt must be `btf__type_cnt(btf) - btf__type_cnt(btf__base_btf(btf))`
  281. * - Mapping is defined as `id_map[original_id - start_id] = new_id`
  282. * - `start_id` equals `btf__type_cnt(btf__base_btf(btf))`
  283. *
  284. * After permutation, all type references within the BTF data and optional
  285. * BTF extension (if provided via @opts) are updated automatically.
  286. *
  287. * On error, returns a negative error code and sets errno:
  288. * - `-EINVAL`: Invalid parameters or invalid ID mapping
  289. * - `-ENOMEM`: Memory allocation failure
  290. */
  291. LIBBPF_API int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
  292. const struct btf_permute_opts *opts);
  293. struct btf_dump;
  294. struct btf_dump_opts {
  295. size_t sz;
  296. };
  297. #define btf_dump_opts__last_field sz
  298. typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
  299. LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
  300. btf_dump_printf_fn_t printf_fn,
  301. void *ctx,
  302. const struct btf_dump_opts *opts);
  303. LIBBPF_API void btf_dump__free(struct btf_dump *d);
  304. LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
  305. struct btf_dump_emit_type_decl_opts {
  306. /* size of this struct, for forward/backward compatibility */
  307. size_t sz;
  308. /* optional field name for type declaration, e.g.:
  309. * - struct my_struct <FNAME>
  310. * - void (*<FNAME>)(int)
  311. * - char (*<FNAME>)[123]
  312. */
  313. const char *field_name;
  314. /* extra indentation level (in number of tabs) to emit for multi-line
  315. * type declarations (e.g., anonymous struct); applies for lines
  316. * starting from the second one (first line is assumed to have
  317. * necessary indentation already
  318. */
  319. int indent_level;
  320. /* strip all the const/volatile/restrict mods */
  321. bool strip_mods;
  322. size_t :0;
  323. };
  324. #define btf_dump_emit_type_decl_opts__last_field strip_mods
  325. LIBBPF_API int
  326. btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
  327. const struct btf_dump_emit_type_decl_opts *opts);
  328. struct btf_dump_type_data_opts {
  329. /* size of this struct, for forward/backward compatibility */
  330. size_t sz;
  331. const char *indent_str;
  332. int indent_level;
  333. /* below match "show" flags for bpf_show_snprintf() */
  334. bool compact; /* no newlines/indentation */
  335. bool skip_names; /* skip member/type names */
  336. bool emit_zeroes; /* show 0-valued fields */
  337. bool emit_strings; /* print char arrays as strings */
  338. size_t :0;
  339. };
  340. #define btf_dump_type_data_opts__last_field emit_strings
  341. LIBBPF_API int
  342. btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
  343. const void *data, size_t data_sz,
  344. const struct btf_dump_type_data_opts *opts);
  345. /*
  346. * A set of helpers for easier BTF types handling.
  347. *
  348. * The inline functions below rely on constants from the kernel headers which
  349. * may not be available for applications including this header file. To avoid
  350. * compilation errors, we define all the constants here that were added after
  351. * the initial introduction of the BTF_KIND* constants.
  352. */
  353. #ifndef BTF_KIND_FUNC
  354. #define BTF_KIND_FUNC 12 /* Function */
  355. #define BTF_KIND_FUNC_PROTO 13 /* Function Proto */
  356. #endif
  357. #ifndef BTF_KIND_VAR
  358. #define BTF_KIND_VAR 14 /* Variable */
  359. #define BTF_KIND_DATASEC 15 /* Section */
  360. #endif
  361. #ifndef BTF_KIND_FLOAT
  362. #define BTF_KIND_FLOAT 16 /* Floating point */
  363. #endif
  364. /* The kernel header switched to enums, so the following were never #defined */
  365. #define BTF_KIND_DECL_TAG 17 /* Decl Tag */
  366. #define BTF_KIND_TYPE_TAG 18 /* Type Tag */
  367. #define BTF_KIND_ENUM64 19 /* Enum for up-to 64bit values */
  368. static inline __u16 btf_kind(const struct btf_type *t)
  369. {
  370. return BTF_INFO_KIND(t->info);
  371. }
  372. static inline __u16 btf_vlen(const struct btf_type *t)
  373. {
  374. return BTF_INFO_VLEN(t->info);
  375. }
  376. static inline bool btf_kflag(const struct btf_type *t)
  377. {
  378. return BTF_INFO_KFLAG(t->info);
  379. }
  380. static inline bool btf_is_void(const struct btf_type *t)
  381. {
  382. return btf_kind(t) == BTF_KIND_UNKN;
  383. }
  384. static inline bool btf_is_int(const struct btf_type *t)
  385. {
  386. return btf_kind(t) == BTF_KIND_INT;
  387. }
  388. static inline bool btf_is_ptr(const struct btf_type *t)
  389. {
  390. return btf_kind(t) == BTF_KIND_PTR;
  391. }
  392. static inline bool btf_is_array(const struct btf_type *t)
  393. {
  394. return btf_kind(t) == BTF_KIND_ARRAY;
  395. }
  396. static inline bool btf_is_struct(const struct btf_type *t)
  397. {
  398. return btf_kind(t) == BTF_KIND_STRUCT;
  399. }
  400. static inline bool btf_is_union(const struct btf_type *t)
  401. {
  402. return btf_kind(t) == BTF_KIND_UNION;
  403. }
  404. static inline bool btf_is_composite(const struct btf_type *t)
  405. {
  406. __u16 kind = btf_kind(t);
  407. return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
  408. }
  409. static inline bool btf_is_enum(const struct btf_type *t)
  410. {
  411. return btf_kind(t) == BTF_KIND_ENUM;
  412. }
  413. static inline bool btf_is_enum64(const struct btf_type *t)
  414. {
  415. return btf_kind(t) == BTF_KIND_ENUM64;
  416. }
  417. static inline bool btf_is_fwd(const struct btf_type *t)
  418. {
  419. return btf_kind(t) == BTF_KIND_FWD;
  420. }
  421. static inline bool btf_is_typedef(const struct btf_type *t)
  422. {
  423. return btf_kind(t) == BTF_KIND_TYPEDEF;
  424. }
  425. static inline bool btf_is_volatile(const struct btf_type *t)
  426. {
  427. return btf_kind(t) == BTF_KIND_VOLATILE;
  428. }
  429. static inline bool btf_is_const(const struct btf_type *t)
  430. {
  431. return btf_kind(t) == BTF_KIND_CONST;
  432. }
  433. static inline bool btf_is_restrict(const struct btf_type *t)
  434. {
  435. return btf_kind(t) == BTF_KIND_RESTRICT;
  436. }
  437. static inline bool btf_is_mod(const struct btf_type *t)
  438. {
  439. __u16 kind = btf_kind(t);
  440. return kind == BTF_KIND_VOLATILE ||
  441. kind == BTF_KIND_CONST ||
  442. kind == BTF_KIND_RESTRICT ||
  443. kind == BTF_KIND_TYPE_TAG;
  444. }
  445. static inline bool btf_is_func(const struct btf_type *t)
  446. {
  447. return btf_kind(t) == BTF_KIND_FUNC;
  448. }
  449. static inline bool btf_is_func_proto(const struct btf_type *t)
  450. {
  451. return btf_kind(t) == BTF_KIND_FUNC_PROTO;
  452. }
  453. static inline bool btf_is_var(const struct btf_type *t)
  454. {
  455. return btf_kind(t) == BTF_KIND_VAR;
  456. }
  457. static inline bool btf_is_datasec(const struct btf_type *t)
  458. {
  459. return btf_kind(t) == BTF_KIND_DATASEC;
  460. }
  461. static inline bool btf_is_float(const struct btf_type *t)
  462. {
  463. return btf_kind(t) == BTF_KIND_FLOAT;
  464. }
  465. static inline bool btf_is_decl_tag(const struct btf_type *t)
  466. {
  467. return btf_kind(t) == BTF_KIND_DECL_TAG;
  468. }
  469. static inline bool btf_is_type_tag(const struct btf_type *t)
  470. {
  471. return btf_kind(t) == BTF_KIND_TYPE_TAG;
  472. }
  473. static inline bool btf_is_any_enum(const struct btf_type *t)
  474. {
  475. return btf_is_enum(t) || btf_is_enum64(t);
  476. }
  477. static inline bool btf_kind_core_compat(const struct btf_type *t1,
  478. const struct btf_type *t2)
  479. {
  480. return btf_kind(t1) == btf_kind(t2) ||
  481. (btf_is_any_enum(t1) && btf_is_any_enum(t2));
  482. }
  483. static inline __u8 btf_int_encoding(const struct btf_type *t)
  484. {
  485. return BTF_INT_ENCODING(*(__u32 *)(t + 1));
  486. }
  487. static inline __u8 btf_int_offset(const struct btf_type *t)
  488. {
  489. return BTF_INT_OFFSET(*(__u32 *)(t + 1));
  490. }
  491. static inline __u8 btf_int_bits(const struct btf_type *t)
  492. {
  493. return BTF_INT_BITS(*(__u32 *)(t + 1));
  494. }
  495. static inline struct btf_array *btf_array(const struct btf_type *t)
  496. {
  497. return (struct btf_array *)(t + 1);
  498. }
  499. static inline struct btf_enum *btf_enum(const struct btf_type *t)
  500. {
  501. return (struct btf_enum *)(t + 1);
  502. }
  503. struct btf_enum64;
  504. static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
  505. {
  506. return (struct btf_enum64 *)(t + 1);
  507. }
  508. static inline __u64 btf_enum64_value(const struct btf_enum64 *e)
  509. {
  510. /* struct btf_enum64 is introduced in Linux 6.0, which is very
  511. * bleeding-edge. Here we are avoiding relying on struct btf_enum64
  512. * definition coming from kernel UAPI headers to support wider range
  513. * of system-wide kernel headers.
  514. *
  515. * Given this header can be also included from C++ applications, that
  516. * further restricts C tricks we can use (like using compatible
  517. * anonymous struct). So just treat struct btf_enum64 as
  518. * a three-element array of u32 and access second (lo32) and third
  519. * (hi32) elements directly.
  520. *
  521. * For reference, here is a struct btf_enum64 definition:
  522. *
  523. * const struct btf_enum64 {
  524. * __u32 name_off;
  525. * __u32 val_lo32;
  526. * __u32 val_hi32;
  527. * };
  528. */
  529. const __u32 *e64 = (const __u32 *)e;
  530. return ((__u64)e64[2] << 32) | e64[1];
  531. }
  532. static inline struct btf_member *btf_members(const struct btf_type *t)
  533. {
  534. return (struct btf_member *)(t + 1);
  535. }
  536. /* Get bit offset of a member with specified index. */
  537. static inline __u32 btf_member_bit_offset(const struct btf_type *t,
  538. __u32 member_idx)
  539. {
  540. const struct btf_member *m = btf_members(t) + member_idx;
  541. bool kflag = btf_kflag(t);
  542. return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
  543. }
  544. /*
  545. * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or
  546. * BTF_KIND_UNION. If member is not a bitfield, zero is returned.
  547. */
  548. static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
  549. __u32 member_idx)
  550. {
  551. const struct btf_member *m = btf_members(t) + member_idx;
  552. bool kflag = btf_kflag(t);
  553. return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
  554. }
  555. static inline struct btf_param *btf_params(const struct btf_type *t)
  556. {
  557. return (struct btf_param *)(t + 1);
  558. }
  559. static inline struct btf_var *btf_var(const struct btf_type *t)
  560. {
  561. return (struct btf_var *)(t + 1);
  562. }
  563. static inline struct btf_var_secinfo *
  564. btf_var_secinfos(const struct btf_type *t)
  565. {
  566. return (struct btf_var_secinfo *)(t + 1);
  567. }
  568. struct btf_decl_tag;
  569. static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)
  570. {
  571. return (struct btf_decl_tag *)(t + 1);
  572. }
  573. #ifdef __cplusplus
  574. } /* extern "C" */
  575. #endif
  576. #endif /* __LIBBPF_BTF_H */