linux/tools/lib/bpf/btf.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
   2/* Copyright (c) 2018 Facebook */
   3
   4#ifndef __LIBBPF_BTF_H
   5#define __LIBBPF_BTF_H
   6
   7#include <stdarg.h>
   8#include <stdbool.h>
   9#include <linux/btf.h>
  10#include <linux/types.h>
  11
  12#include "libbpf_common.h"
  13
  14#ifdef __cplusplus
  15extern "C" {
  16#endif
  17
  18#define BTF_ELF_SEC ".BTF"
  19#define BTF_EXT_ELF_SEC ".BTF.ext"
  20#define MAPS_ELF_SEC ".maps"
  21
  22struct btf;
  23struct btf_ext;
  24struct btf_type;
  25
  26struct bpf_object;
  27
  28enum btf_endianness {
  29        BTF_LITTLE_ENDIAN = 0,
  30        BTF_BIG_ENDIAN = 1,
  31};
  32
  33LIBBPF_API void btf__free(struct btf *btf);
  34
  35LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
  36LIBBPF_API struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf);
  37LIBBPF_API struct btf *btf__new_empty(void);
  38LIBBPF_API struct btf *btf__new_empty_split(struct btf *base_btf);
  39
  40LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
  41LIBBPF_API struct btf *btf__parse_split(const char *path, struct btf *base_btf);
  42LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
  43LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf);
  44LIBBPF_API struct btf *btf__parse_raw(const char *path);
  45LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf);
  46
  47LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
  48LIBBPF_API int btf__load(struct btf *btf);
  49LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
  50                                   const char *type_name);
  51LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
  52                                        const char *type_name, __u32 kind);
  53LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
  54LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf);
  55LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
  56                                                  __u32 id);
  57LIBBPF_API size_t btf__pointer_size(const struct btf *btf);
  58LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz);
  59LIBBPF_API enum btf_endianness btf__endianness(const struct btf *btf);
  60LIBBPF_API int btf__set_endianness(struct btf *btf, enum btf_endianness endian);
  61LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
  62LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
  63LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
  64LIBBPF_API int btf__fd(const struct btf *btf);
  65LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
  66LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
  67LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
  68LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
  69LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
  70LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
  71                                    __u32 expected_key_size,
  72                                    __u32 expected_value_size,
  73                                    __u32 *key_type_id, __u32 *value_type_id);
  74
  75LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size);
  76LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
  77LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
  78                                             __u32 *size);
  79LIBBPF_API LIBBPF_DEPRECATED("btf_ext__reloc_func_info was never meant as a public API and has wrong assumptions embedded in it; it will be removed in the future libbpf versions")
  80int btf_ext__reloc_func_info(const struct btf *btf,
  81                             const struct btf_ext *btf_ext,
  82                             const char *sec_name, __u32 insns_cnt,
  83                             void **func_info, __u32 *cnt);
  84LIBBPF_API LIBBPF_DEPRECATED("btf_ext__reloc_line_info was never meant as a public API and has wrong assumptions embedded in it; it will be removed in the future libbpf versions")
  85int btf_ext__reloc_line_info(const struct btf *btf,
  86                             const struct btf_ext *btf_ext,
  87                             const char *sec_name, __u32 insns_cnt,
  88                             void **line_info, __u32 *cnt);
  89LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
  90LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
  91
  92LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
  93
  94LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
  95LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
  96
  97LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding);
  98LIBBPF_API int btf__add_ptr(struct btf *btf, int ref_type_id);
  99LIBBPF_API int btf__add_array(struct btf *btf,
 100                              int index_type_id, int elem_type_id, __u32 nr_elems);
 101/* struct/union construction APIs */
 102LIBBPF_API int btf__add_struct(struct btf *btf, const char *name, __u32 sz);
 103LIBBPF_API int btf__add_union(struct btf *btf, const char *name, __u32 sz);
 104LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_id,
 105                              __u32 bit_offset, __u32 bit_size);
 106
 107/* enum construction APIs */
 108LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz);
 109LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value);
 110
 111enum btf_fwd_kind {
 112        BTF_FWD_STRUCT = 0,
 113        BTF_FWD_UNION = 1,
 114        BTF_FWD_ENUM = 2,
 115};
 116
 117LIBBPF_API int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind);
 118LIBBPF_API int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id);
 119LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id);
 120LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id);
 121LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id);
 122
 123/* func and func_proto construction APIs */
 124LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
 125                             enum btf_func_linkage linkage, int proto_type_id);
 126LIBBPF_API int btf__add_func_proto(struct btf *btf, int ret_type_id);
 127LIBBPF_API int btf__add_func_param(struct btf *btf, const char *name, int type_id);
 128
 129/* var & datasec construction APIs */
 130LIBBPF_API int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id);
 131LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz);
 132LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
 133                                         __u32 offset, __u32 byte_sz);
 134
 135struct btf_dedup_opts {
 136        unsigned int dedup_table_size;
 137        bool dont_resolve_fwds;
 138};
 139
 140LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
 141                          const struct btf_dedup_opts *opts);
 142
 143struct btf_dump;
 144
 145struct btf_dump_opts {
 146        void *ctx;
 147};
 148
 149typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
 150
 151LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
 152                                          const struct btf_ext *btf_ext,
 153                                          const struct btf_dump_opts *opts,
 154                                          btf_dump_printf_fn_t printf_fn);
 155LIBBPF_API void btf_dump__free(struct btf_dump *d);
 156
 157LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
 158
 159struct btf_dump_emit_type_decl_opts {
 160        /* size of this struct, for forward/backward compatiblity */
 161        size_t sz;
 162        /* optional field name for type declaration, e.g.:
 163         * - struct my_struct <FNAME>
 164         * - void (*<FNAME>)(int)
 165         * - char (*<FNAME>)[123]
 166         */
 167        const char *field_name;
 168        /* extra indentation level (in number of tabs) to emit for multi-line
 169         * type declarations (e.g., anonymous struct); applies for lines
 170         * starting from the second one (first line is assumed to have
 171         * necessary indentation already
 172         */
 173        int indent_level;
 174        /* strip all the const/volatile/restrict mods */
 175        bool strip_mods;
 176};
 177#define btf_dump_emit_type_decl_opts__last_field strip_mods
 178
 179LIBBPF_API int
 180btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
 181                         const struct btf_dump_emit_type_decl_opts *opts);
 182
 183/*
 184 * A set of helpers for easier BTF types handling
 185 */
 186static inline __u16 btf_kind(const struct btf_type *t)
 187{
 188        return BTF_INFO_KIND(t->info);
 189}
 190
 191static inline __u16 btf_vlen(const struct btf_type *t)
 192{
 193        return BTF_INFO_VLEN(t->info);
 194}
 195
 196static inline bool btf_kflag(const struct btf_type *t)
 197{
 198        return BTF_INFO_KFLAG(t->info);
 199}
 200
 201static inline bool btf_is_void(const struct btf_type *t)
 202{
 203        return btf_kind(t) == BTF_KIND_UNKN;
 204}
 205
 206static inline bool btf_is_int(const struct btf_type *t)
 207{
 208        return btf_kind(t) == BTF_KIND_INT;
 209}
 210
 211static inline bool btf_is_ptr(const struct btf_type *t)
 212{
 213        return btf_kind(t) == BTF_KIND_PTR;
 214}
 215
 216static inline bool btf_is_array(const struct btf_type *t)
 217{
 218        return btf_kind(t) == BTF_KIND_ARRAY;
 219}
 220
 221static inline bool btf_is_struct(const struct btf_type *t)
 222{
 223        return btf_kind(t) == BTF_KIND_STRUCT;
 224}
 225
 226static inline bool btf_is_union(const struct btf_type *t)
 227{
 228        return btf_kind(t) == BTF_KIND_UNION;
 229}
 230
 231static inline bool btf_is_composite(const struct btf_type *t)
 232{
 233        __u16 kind = btf_kind(t);
 234
 235        return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
 236}
 237
 238static inline bool btf_is_enum(const struct btf_type *t)
 239{
 240        return btf_kind(t) == BTF_KIND_ENUM;
 241}
 242
 243static inline bool btf_is_fwd(const struct btf_type *t)
 244{
 245        return btf_kind(t) == BTF_KIND_FWD;
 246}
 247
 248static inline bool btf_is_typedef(const struct btf_type *t)
 249{
 250        return btf_kind(t) == BTF_KIND_TYPEDEF;
 251}
 252
 253static inline bool btf_is_volatile(const struct btf_type *t)
 254{
 255        return btf_kind(t) == BTF_KIND_VOLATILE;
 256}
 257
 258static inline bool btf_is_const(const struct btf_type *t)
 259{
 260        return btf_kind(t) == BTF_KIND_CONST;
 261}
 262
 263static inline bool btf_is_restrict(const struct btf_type *t)
 264{
 265        return btf_kind(t) == BTF_KIND_RESTRICT;
 266}
 267
 268static inline bool btf_is_mod(const struct btf_type *t)
 269{
 270        __u16 kind = btf_kind(t);
 271
 272        return kind == BTF_KIND_VOLATILE ||
 273               kind == BTF_KIND_CONST ||
 274               kind == BTF_KIND_RESTRICT;
 275}
 276
 277static inline bool btf_is_func(const struct btf_type *t)
 278{
 279        return btf_kind(t) == BTF_KIND_FUNC;
 280}
 281
 282static inline bool btf_is_func_proto(const struct btf_type *t)
 283{
 284        return btf_kind(t) == BTF_KIND_FUNC_PROTO;
 285}
 286
 287static inline bool btf_is_var(const struct btf_type *t)
 288{
 289        return btf_kind(t) == BTF_KIND_VAR;
 290}
 291
 292static inline bool btf_is_datasec(const struct btf_type *t)
 293{
 294        return btf_kind(t) == BTF_KIND_DATASEC;
 295}
 296
 297static inline __u8 btf_int_encoding(const struct btf_type *t)
 298{
 299        return BTF_INT_ENCODING(*(__u32 *)(t + 1));
 300}
 301
 302static inline __u8 btf_int_offset(const struct btf_type *t)
 303{
 304        return BTF_INT_OFFSET(*(__u32 *)(t + 1));
 305}
 306
 307static inline __u8 btf_int_bits(const struct btf_type *t)
 308{
 309        return BTF_INT_BITS(*(__u32 *)(t + 1));
 310}
 311
 312static inline struct btf_array *btf_array(const struct btf_type *t)
 313{
 314        return (struct btf_array *)(t + 1);
 315}
 316
 317static inline struct btf_enum *btf_enum(const struct btf_type *t)
 318{
 319        return (struct btf_enum *)(t + 1);
 320}
 321
 322static inline struct btf_member *btf_members(const struct btf_type *t)
 323{
 324        return (struct btf_member *)(t + 1);
 325}
 326
 327/* Get bit offset of a member with specified index. */
 328static inline __u32 btf_member_bit_offset(const struct btf_type *t,
 329                                          __u32 member_idx)
 330{
 331        const struct btf_member *m = btf_members(t) + member_idx;
 332        bool kflag = btf_kflag(t);
 333
 334        return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
 335}
 336/*
 337 * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or
 338 * BTF_KIND_UNION. If member is not a bitfield, zero is returned.
 339 */
 340static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
 341                                             __u32 member_idx)
 342{
 343        const struct btf_member *m = btf_members(t) + member_idx;
 344        bool kflag = btf_kflag(t);
 345
 346        return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
 347}
 348
 349static inline struct btf_param *btf_params(const struct btf_type *t)
 350{
 351        return (struct btf_param *)(t + 1);
 352}
 353
 354static inline struct btf_var *btf_var(const struct btf_type *t)
 355{
 356        return (struct btf_var *)(t + 1);
 357}
 358
 359static inline struct btf_var_secinfo *
 360btf_var_secinfos(const struct btf_type *t)
 361{
 362        return (struct btf_var_secinfo *)(t + 1);
 363}
 364
 365#ifdef __cplusplus
 366} /* extern "C" */
 367#endif
 368
 369#endif /* __LIBBPF_BTF_H */
 370