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 <linux/btf.h> 9 #include <linux/types.h> 10 11 #include "libbpf_common.h" 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 #define BTF_ELF_SEC ".BTF" 18 #define BTF_EXT_ELF_SEC ".BTF.ext" 19 #define MAPS_ELF_SEC ".maps" 20 21 struct btf; 22 struct btf_ext; 23 struct btf_type; 24 25 struct bpf_object; 26 27 LIBBPF_API void btf__free(struct btf *btf); 28 LIBBPF_API struct btf *btf__new(const void *data, __u32 size); 29 LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext); 30 LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext); 31 LIBBPF_API struct btf *btf__parse_raw(const char *path); 32 LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf); 33 LIBBPF_API int btf__load(struct btf *btf); 34 LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, 35 const char *type_name); 36 LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf, 37 const char *type_name, __u32 kind); 38 LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf); 39 LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf, 40 __u32 id); 41 LIBBPF_API size_t btf__pointer_size(const struct btf *btf); 42 LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz); 43 LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); 44 LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); 45 LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id); 46 LIBBPF_API int btf__fd(const struct btf *btf); 47 LIBBPF_API void btf__set_fd(struct btf *btf, int fd); 48 LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size); 49 LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); 50 LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); 51 LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, 52 __u32 expected_key_size, 53 __u32 expected_value_size, 54 __u32 *key_type_id, __u32 *value_type_id); 55 56 LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size); 57 LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext); 58 LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, 59 __u32 *size); 60 LIBBPF_API int btf_ext__reloc_func_info(const struct btf *btf, 61 const struct btf_ext *btf_ext, 62 const char *sec_name, __u32 insns_cnt, 63 void **func_info, __u32 *cnt); 64 LIBBPF_API int btf_ext__reloc_line_info(const struct btf *btf, 65 const struct btf_ext *btf_ext, 66 const char *sec_name, __u32 insns_cnt, 67 void **line_info, __u32 *cnt); 68 LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext); 69 LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext); 70 71 LIBBPF_API struct btf *libbpf_find_kernel_btf(void); 72 73 struct btf_dedup_opts { 74 unsigned int dedup_table_size; 75 bool dont_resolve_fwds; 76 }; 77 78 LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext, 79 const struct btf_dedup_opts *opts); 80 81 struct btf_dump; 82 83 struct btf_dump_opts { 84 void *ctx; 85 }; 86 87 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args); 88 89 LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf, 90 const struct btf_ext *btf_ext, 91 const struct btf_dump_opts *opts, 92 btf_dump_printf_fn_t printf_fn); 93 LIBBPF_API void btf_dump__free(struct btf_dump *d); 94 95 LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id); 96 97 struct btf_dump_emit_type_decl_opts { 98 /* size of this struct, for forward/backward compatiblity */ 99 size_t sz; 100 /* optional field name for type declaration, e.g.: 101 * - struct my_struct <FNAME> 102 * - void (*<FNAME>)(int) 103 * - char (*<FNAME>)[123] 104 */ 105 const char *field_name; 106 /* extra indentation level (in number of tabs) to emit for multi-line 107 * type declarations (e.g., anonymous struct); applies for lines 108 * starting from the second one (first line is assumed to have 109 * necessary indentation already 110 */ 111 int indent_level; 112 /* strip all the const/volatile/restrict mods */ 113 bool strip_mods; 114 }; 115 #define btf_dump_emit_type_decl_opts__last_field strip_mods 116 117 LIBBPF_API int 118 btf_dump__emit_type_decl(struct btf_dump *d, __u32 id, 119 const struct btf_dump_emit_type_decl_opts *opts); 120 121 /* 122 * A set of helpers for easier BTF types handling 123 */ 124 static inline __u16 btf_kind(const struct btf_type *t) 125 { 126 return BTF_INFO_KIND(t->info); 127 } 128 129 static inline __u16 btf_vlen(const struct btf_type *t) 130 { 131 return BTF_INFO_VLEN(t->info); 132 } 133 134 static inline bool btf_kflag(const struct btf_type *t) 135 { 136 return BTF_INFO_KFLAG(t->info); 137 } 138 139 static inline bool btf_is_void(const struct btf_type *t) 140 { 141 return btf_kind(t) == BTF_KIND_UNKN; 142 } 143 144 static inline bool btf_is_int(const struct btf_type *t) 145 { 146 return btf_kind(t) == BTF_KIND_INT; 147 } 148 149 static inline bool btf_is_ptr(const struct btf_type *t) 150 { 151 return btf_kind(t) == BTF_KIND_PTR; 152 } 153 154 static inline bool btf_is_array(const struct btf_type *t) 155 { 156 return btf_kind(t) == BTF_KIND_ARRAY; 157 } 158 159 static inline bool btf_is_struct(const struct btf_type *t) 160 { 161 return btf_kind(t) == BTF_KIND_STRUCT; 162 } 163 164 static inline bool btf_is_union(const struct btf_type *t) 165 { 166 return btf_kind(t) == BTF_KIND_UNION; 167 } 168 169 static inline bool btf_is_composite(const struct btf_type *t) 170 { 171 __u16 kind = btf_kind(t); 172 173 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 174 } 175 176 static inline bool btf_is_enum(const struct btf_type *t) 177 { 178 return btf_kind(t) == BTF_KIND_ENUM; 179 } 180 181 static inline bool btf_is_fwd(const struct btf_type *t) 182 { 183 return btf_kind(t) == BTF_KIND_FWD; 184 } 185 186 static inline bool btf_is_typedef(const struct btf_type *t) 187 { 188 return btf_kind(t) == BTF_KIND_TYPEDEF; 189 } 190 191 static inline bool btf_is_volatile(const struct btf_type *t) 192 { 193 return btf_kind(t) == BTF_KIND_VOLATILE; 194 } 195 196 static inline bool btf_is_const(const struct btf_type *t) 197 { 198 return btf_kind(t) == BTF_KIND_CONST; 199 } 200 201 static inline bool btf_is_restrict(const struct btf_type *t) 202 { 203 return btf_kind(t) == BTF_KIND_RESTRICT; 204 } 205 206 static inline bool btf_is_mod(const struct btf_type *t) 207 { 208 __u16 kind = btf_kind(t); 209 210 return kind == BTF_KIND_VOLATILE || 211 kind == BTF_KIND_CONST || 212 kind == BTF_KIND_RESTRICT; 213 } 214 215 static inline bool btf_is_func(const struct btf_type *t) 216 { 217 return btf_kind(t) == BTF_KIND_FUNC; 218 } 219 220 static inline bool btf_is_func_proto(const struct btf_type *t) 221 { 222 return btf_kind(t) == BTF_KIND_FUNC_PROTO; 223 } 224 225 static inline bool btf_is_var(const struct btf_type *t) 226 { 227 return btf_kind(t) == BTF_KIND_VAR; 228 } 229 230 static inline bool btf_is_datasec(const struct btf_type *t) 231 { 232 return btf_kind(t) == BTF_KIND_DATASEC; 233 } 234 235 static inline __u8 btf_int_encoding(const struct btf_type *t) 236 { 237 return BTF_INT_ENCODING(*(__u32 *)(t + 1)); 238 } 239 240 static inline __u8 btf_int_offset(const struct btf_type *t) 241 { 242 return BTF_INT_OFFSET(*(__u32 *)(t + 1)); 243 } 244 245 static inline __u8 btf_int_bits(const struct btf_type *t) 246 { 247 return BTF_INT_BITS(*(__u32 *)(t + 1)); 248 } 249 250 static inline struct btf_array *btf_array(const struct btf_type *t) 251 { 252 return (struct btf_array *)(t + 1); 253 } 254 255 static inline struct btf_enum *btf_enum(const struct btf_type *t) 256 { 257 return (struct btf_enum *)(t + 1); 258 } 259 260 static inline struct btf_member *btf_members(const struct btf_type *t) 261 { 262 return (struct btf_member *)(t + 1); 263 } 264 265 /* Get bit offset of a member with specified index. */ 266 static inline __u32 btf_member_bit_offset(const struct btf_type *t, 267 __u32 member_idx) 268 { 269 const struct btf_member *m = btf_members(t) + member_idx; 270 bool kflag = btf_kflag(t); 271 272 return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset; 273 } 274 /* 275 * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or 276 * BTF_KIND_UNION. If member is not a bitfield, zero is returned. 277 */ 278 static inline __u32 btf_member_bitfield_size(const struct btf_type *t, 279 __u32 member_idx) 280 { 281 const struct btf_member *m = btf_members(t) + member_idx; 282 bool kflag = btf_kflag(t); 283 284 return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0; 285 } 286 287 static inline struct btf_param *btf_params(const struct btf_type *t) 288 { 289 return (struct btf_param *)(t + 1); 290 } 291 292 static inline struct btf_var *btf_var(const struct btf_type *t) 293 { 294 return (struct btf_var *)(t + 1); 295 } 296 297 static inline struct btf_var_secinfo * 298 btf_var_secinfos(const struct btf_type *t) 299 { 300 return (struct btf_var_secinfo *)(t + 1); 301 } 302 303 #ifdef __cplusplus 304 } /* extern "C" */ 305 #endif 306 307 #endif /* __LIBBPF_BTF_H */ 308