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