btf.h 20 KB

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