1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * BTF-to-C type converter.
6 * Copyright (c) 2019 Facebook
14 #include <linux/err.h>
15 #include <linux/btf.h>
19 #include "libbpf_internal.h"
21 /* make sure libbpf doesn't use kernel-only integer typedefs */
22 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
24 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
25 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
27 static const char *pfx(int lvl)
29 return lvl >= PREFIX_CNT ? PREFIXES : &PREFIXES[PREFIX_CNT - lvl];
32 enum btf_dump_type_order_state {
38 enum btf_dump_type_emit_state {
44 /* per-type auxiliary state */
45 struct btf_dump_type_aux_state {
46 /* topological sorting state */
47 enum btf_dump_type_order_state order_state: 2;
48 /* emitting state used to determine the need for forward declaration */
49 enum btf_dump_type_emit_state emit_state: 2;
50 /* whether forward declaration was already emitted */
52 /* whether unique non-duplicate name was already assigned */
53 __u8 name_resolved: 1;
54 /* whether type is referenced from any other type */
59 const struct btf *btf;
60 const struct btf_ext *btf_ext;
61 btf_dump_printf_fn_t printf_fn;
62 struct btf_dump_opts opts;
64 /* per-type auxiliary state */
65 struct btf_dump_type_aux_state *type_states;
66 /* per-type optional cached unique name, must be freed, if present */
67 const char **cached_names;
69 /* topo-sorted list of dependent type definitions */
75 * stack of type declarations (e.g., chain of modifiers, arrays,
82 /* maps struct/union/enum name to a number of name occurrences */
83 struct hashmap *type_names;
85 * maps typedef identifiers and enum value names to a number of such
88 struct hashmap *ident_names;
91 static size_t str_hash_fn(const void *key, void *ctx)
103 static bool str_equal_fn(const void *a, const void *b, void *ctx)
105 return strcmp(a, b) == 0;
108 static const char *btf_name_of(const struct btf_dump *d, __u32 name_off)
110 return btf__name_by_offset(d->btf, name_off);
113 static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)
118 d->printf_fn(d->opts.ctx, fmt, args);
122 static int btf_dump_mark_referenced(struct btf_dump *d);
124 struct btf_dump *btf_dump__new(const struct btf *btf,
125 const struct btf_ext *btf_ext,
126 const struct btf_dump_opts *opts,
127 btf_dump_printf_fn_t printf_fn)
132 d = calloc(1, sizeof(struct btf_dump));
134 return ERR_PTR(-ENOMEM);
137 d->btf_ext = btf_ext;
138 d->printf_fn = printf_fn;
139 d->opts.ctx = opts ? opts->ctx : NULL;
141 d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
142 if (IS_ERR(d->type_names)) {
143 err = PTR_ERR(d->type_names);
144 d->type_names = NULL;
147 d->ident_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
148 if (IS_ERR(d->ident_names)) {
149 err = PTR_ERR(d->ident_names);
150 d->ident_names = NULL;
153 d->type_states = calloc(1 + btf__get_nr_types(d->btf),
154 sizeof(d->type_states[0]));
155 if (!d->type_states) {
159 d->cached_names = calloc(1 + btf__get_nr_types(d->btf),
160 sizeof(d->cached_names[0]));
161 if (!d->cached_names) {
166 /* VOID is special */
167 d->type_states[0].order_state = ORDERED;
168 d->type_states[0].emit_state = EMITTED;
170 /* eagerly determine referenced types for anon enums */
171 err = btf_dump_mark_referenced(d);
181 void btf_dump__free(struct btf_dump *d)
188 free(d->type_states);
189 if (d->cached_names) {
190 /* any set cached name is owned by us and should be freed */
191 for (i = 0, cnt = btf__get_nr_types(d->btf); i <= cnt; i++) {
192 if (d->cached_names[i])
193 free((void *)d->cached_names[i]);
196 free(d->cached_names);
199 hashmap__free(d->type_names);
200 hashmap__free(d->ident_names);
205 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr);
206 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id);
209 * Dump BTF type in a compilable C syntax, including all the necessary
210 * dependent types, necessary for compilation. If some of the dependent types
211 * were already emitted as part of previous btf_dump__dump_type() invocation
212 * for another type, they won't be emitted again. This API allows callers to
213 * filter out BTF types according to user-defined criterias and emitted only
214 * minimal subset of types, necessary to compile everything. Full struct/union
215 * definitions will still be emitted, even if the only usage is through
216 * pointer and could be satisfied with just a forward declaration.
218 * Dumping is done in two high-level passes:
219 * 1. Topologically sort type definitions to satisfy C rules of compilation.
220 * 2. Emit type definitions in C syntax.
222 * Returns 0 on success; <0, otherwise.
224 int btf_dump__dump_type(struct btf_dump *d, __u32 id)
228 if (id > btf__get_nr_types(d->btf))
231 d->emit_queue_cnt = 0;
232 err = btf_dump_order_type(d, id, false);
236 for (i = 0; i < d->emit_queue_cnt; i++)
237 btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
243 * Mark all types that are referenced from any other type. This is used to
244 * determine top-level anonymous enums that need to be emitted as an
245 * independent type declarations.
246 * Anonymous enums come in two flavors: either embedded in a struct's field
247 * definition, in which case they have to be declared inline as part of field
248 * type declaration; or as a top-level anonymous enum, typically used for
249 * declaring global constants. It's impossible to distinguish between two
250 * without knowning whether given enum type was referenced from other type:
251 * top-level anonymous enum won't be referenced by anything, while embedded
254 static int btf_dump_mark_referenced(struct btf_dump *d)
256 int i, j, n = btf__get_nr_types(d->btf);
257 const struct btf_type *t;
260 for (i = 1; i <= n; i++) {
261 t = btf__type_by_id(d->btf, i);
264 switch (btf_kind(t)) {
270 case BTF_KIND_VOLATILE:
272 case BTF_KIND_RESTRICT:
274 case BTF_KIND_TYPEDEF:
277 d->type_states[t->type].referenced = 1;
280 case BTF_KIND_ARRAY: {
281 const struct btf_array *a = btf_array(t);
283 d->type_states[a->index_type].referenced = 1;
284 d->type_states[a->type].referenced = 1;
287 case BTF_KIND_STRUCT:
288 case BTF_KIND_UNION: {
289 const struct btf_member *m = btf_members(t);
291 for (j = 0; j < vlen; j++, m++)
292 d->type_states[m->type].referenced = 1;
295 case BTF_KIND_FUNC_PROTO: {
296 const struct btf_param *p = btf_params(t);
298 for (j = 0; j < vlen; j++, p++)
299 d->type_states[p->type].referenced = 1;
302 case BTF_KIND_DATASEC: {
303 const struct btf_var_secinfo *v = btf_var_secinfos(t);
305 for (j = 0; j < vlen; j++, v++)
306 d->type_states[v->type].referenced = 1;
315 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
320 if (d->emit_queue_cnt >= d->emit_queue_cap) {
321 new_cap = max(16, d->emit_queue_cap * 3 / 2);
322 new_queue = realloc(d->emit_queue,
323 new_cap * sizeof(new_queue[0]));
326 d->emit_queue = new_queue;
327 d->emit_queue_cap = new_cap;
330 d->emit_queue[d->emit_queue_cnt++] = id;
335 * Determine order of emitting dependent types and specified type to satisfy
336 * C compilation rules. This is done through topological sorting with an
337 * additional complication which comes from C rules. The main idea for C is
338 * that if some type is "embedded" into a struct/union, it's size needs to be
339 * known at the time of definition of containing type. E.g., for:
342 * struct B { struct A x; }
344 * struct A *HAS* to be defined before struct B, because it's "embedded",
345 * i.e., it is part of struct B layout. But in the following case:
348 * struct B { struct A *x; }
351 * it's enough to just have a forward declaration of struct A at the time of
352 * struct B definition, as struct B has a pointer to struct A, so the size of
353 * field x is known without knowing struct A size: it's sizeof(void *).
355 * Unfortunately, there are some trickier cases we need to handle, e.g.:
357 * struct A {}; // if this was forward-declaration: compilation error
359 * struct { // anonymous struct
364 * In this case, struct B's field x is a pointer, so it's size is known
365 * regardless of the size of (anonymous) struct it points to. But because this
366 * struct is anonymous and thus defined inline inside struct B, *and* it
367 * embeds struct A, compiler requires full definition of struct A to be known
368 * before struct B can be defined. This creates a transitive dependency
369 * between struct A and struct B. If struct A was forward-declared before
370 * struct B definition and fully defined after struct B definition, that would
371 * trigger compilation error.
373 * All this means that while we are doing topological sorting on BTF type
374 * graph, we need to determine relationships between different types (graph
376 * - weak link (relationship) between X and Y, if Y *CAN* be
377 * forward-declared at the point of X definition;
378 * - strong link, if Y *HAS* to be fully-defined before X can be defined.
380 * The rule is as follows. Given a chain of BTF types from X to Y, if there is
381 * BTF_KIND_PTR type in the chain and at least one non-anonymous type
382 * Z (excluding X, including Y), then link is weak. Otherwise, it's strong.
383 * Weak/strong relationship is determined recursively during DFS traversal and
384 * is returned as a result from btf_dump_order_type().
386 * btf_dump_order_type() is trying to avoid unnecessary forward declarations,
387 * but it is not guaranteeing that no extraneous forward declarations will be
390 * To avoid extra work, algorithm marks some of BTF types as ORDERED, when
391 * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT,
392 * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the
393 * entire graph path, so depending where from one came to that BTF type, it
394 * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM,
395 * once they are processed, there is no need to do it again, so they are
396 * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces
397 * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But
398 * in any case, once those are processed, no need to do it again, as the
399 * result won't change.
402 * - 1, if type is part of strong link (so there is strong topological
403 * ordering requirements);
404 * - 0, if type is part of weak link (so can be satisfied through forward
406 * - <0, on error (e.g., unsatisfiable type loop detected).
408 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
411 * Order state is used to detect strong link cycles, but only for BTF
412 * kinds that are or could be an independent definition (i.e.,
413 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays,
414 * func_protos, modifiers are just means to get to these definitions.
415 * Int/void don't need definitions, they are assumed to be always
416 * properly defined. We also ignore datasec, var, and funcs for now.
417 * So for all non-defining kinds, we never even set ordering state,
418 * for defining kinds we set ORDERING and subsequently ORDERED if it
419 * forms a strong link.
421 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
422 const struct btf_type *t;
426 /* return true, letting typedefs know that it's ok to be emitted */
427 if (tstate->order_state == ORDERED)
430 t = btf__type_by_id(d->btf, id);
432 if (tstate->order_state == ORDERING) {
433 /* type loop, but resolvable through fwd declaration */
434 if (btf_is_composite(t) && through_ptr && t->name_off != 0)
436 pr_warn("unsatisfiable type cycle, id:[%u]\n", id);
440 switch (btf_kind(t)) {
442 tstate->order_state = ORDERED;
446 err = btf_dump_order_type(d, t->type, true);
447 tstate->order_state = ORDERED;
451 return btf_dump_order_type(d, btf_array(t)->type, through_ptr);
453 case BTF_KIND_STRUCT:
454 case BTF_KIND_UNION: {
455 const struct btf_member *m = btf_members(t);
457 * struct/union is part of strong link, only if it's embedded
458 * (so no ptr in a path) or it's anonymous (so has to be
459 * defined inline, even if declared through ptr)
461 if (through_ptr && t->name_off != 0)
464 tstate->order_state = ORDERING;
467 for (i = 0; i < vlen; i++, m++) {
468 err = btf_dump_order_type(d, m->type, false);
473 if (t->name_off != 0) {
474 err = btf_dump_add_emit_queue_id(d, id);
479 tstate->order_state = ORDERED;
485 * non-anonymous or non-referenced enums are top-level
486 * declarations and should be emitted. Same logic can be
487 * applied to FWDs, it won't hurt anyways.
489 if (t->name_off != 0 || !tstate->referenced) {
490 err = btf_dump_add_emit_queue_id(d, id);
494 tstate->order_state = ORDERED;
497 case BTF_KIND_TYPEDEF: {
500 is_strong = btf_dump_order_type(d, t->type, through_ptr);
504 /* typedef is similar to struct/union w.r.t. fwd-decls */
505 if (through_ptr && !is_strong)
508 /* typedef is always a named definition */
509 err = btf_dump_add_emit_queue_id(d, id);
513 d->type_states[id].order_state = ORDERED;
516 case BTF_KIND_VOLATILE:
518 case BTF_KIND_RESTRICT:
519 return btf_dump_order_type(d, t->type, through_ptr);
521 case BTF_KIND_FUNC_PROTO: {
522 const struct btf_param *p = btf_params(t);
525 err = btf_dump_order_type(d, t->type, through_ptr);
531 for (i = 0; i < vlen; i++, p++) {
532 err = btf_dump_order_type(d, p->type, through_ptr);
542 case BTF_KIND_DATASEC:
543 d->type_states[id].order_state = ORDERED;
551 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
552 const struct btf_type *t);
553 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id,
554 const struct btf_type *t, int lvl);
556 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
557 const struct btf_type *t);
558 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
559 const struct btf_type *t, int lvl);
561 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
562 const struct btf_type *t);
564 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
565 const struct btf_type *t, int lvl);
567 /* a local view into a shared stack */
573 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
574 const char *fname, int lvl);
575 static void btf_dump_emit_type_chain(struct btf_dump *d,
576 struct id_stack *decl_stack,
577 const char *fname, int lvl);
579 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
580 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
581 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
582 const char *orig_name);
584 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
586 const struct btf_type *t = btf__type_by_id(d->btf, id);
588 /* __builtin_va_list is a compiler built-in, which causes compilation
589 * errors, when compiling w/ different compiler, then used to compile
590 * original code (e.g., GCC to compile kernel, Clang to use generated
591 * C header from BTF). As it is built-in, it should be already defined
592 * properly internally in compiler.
594 if (t->name_off == 0)
596 return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0;
600 * Emit C-syntax definitions of types from chains of BTF types.
602 * High-level handling of determining necessary forward declarations are handled
603 * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type
604 * declarations/definitions in C syntax are handled by a combo of
605 * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to
606 * corresponding btf_dump_emit_*_{def,fwd}() functions.
608 * We also keep track of "containing struct/union type ID" to determine when
609 * we reference it from inside and thus can avoid emitting unnecessary forward
612 * This algorithm is designed in such a way, that even if some error occurs
613 * (either technical, e.g., out of memory, or logical, i.e., malformed BTF
614 * that doesn't comply to C rules completely), algorithm will try to proceed
615 * and produce as much meaningful output as possible.
617 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
619 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
620 bool top_level_def = cont_id == 0;
621 const struct btf_type *t;
624 if (tstate->emit_state == EMITTED)
627 t = btf__type_by_id(d->btf, id);
630 if (tstate->emit_state == EMITTING) {
631 if (tstate->fwd_emitted)
635 case BTF_KIND_STRUCT:
638 * if we are referencing a struct/union that we are
639 * part of - then no need for fwd declaration
643 if (t->name_off == 0) {
644 pr_warn("anonymous struct/union loop, id:[%u]\n",
648 btf_dump_emit_struct_fwd(d, id, t);
649 btf_dump_printf(d, ";\n\n");
650 tstate->fwd_emitted = 1;
652 case BTF_KIND_TYPEDEF:
654 * for typedef fwd_emitted means typedef definition
655 * was emitted, but it can be used only for "weak"
656 * references through pointer only, not for embedding
658 if (!btf_dump_is_blacklisted(d, id)) {
659 btf_dump_emit_typedef_def(d, id, t, 0);
660 btf_dump_printf(d, ";\n\n");
662 tstate->fwd_emitted = 1;
673 tstate->emit_state = EMITTED;
677 btf_dump_emit_enum_def(d, id, t, 0);
678 btf_dump_printf(d, ";\n\n");
680 tstate->emit_state = EMITTED;
683 case BTF_KIND_VOLATILE:
685 case BTF_KIND_RESTRICT:
686 btf_dump_emit_type(d, t->type, cont_id);
689 btf_dump_emit_type(d, btf_array(t)->type, cont_id);
692 btf_dump_emit_fwd_def(d, id, t);
693 btf_dump_printf(d, ";\n\n");
694 tstate->emit_state = EMITTED;
696 case BTF_KIND_TYPEDEF:
697 tstate->emit_state = EMITTING;
698 btf_dump_emit_type(d, t->type, id);
700 * typedef can server as both definition and forward
701 * declaration; at this stage someone depends on
702 * typedef as a forward declaration (refers to it
703 * through pointer), so unless we already did it,
704 * emit typedef as a forward declaration
706 if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
707 btf_dump_emit_typedef_def(d, id, t, 0);
708 btf_dump_printf(d, ";\n\n");
710 tstate->emit_state = EMITTED;
712 case BTF_KIND_STRUCT:
714 tstate->emit_state = EMITTING;
715 /* if it's a top-level struct/union definition or struct/union
716 * is anonymous, then in C we'll be emitting all fields and
717 * their types (as opposed to just `struct X`), so we need to
718 * make sure that all types, referenced from struct/union
719 * members have necessary forward-declarations, where
722 if (top_level_def || t->name_off == 0) {
723 const struct btf_member *m = btf_members(t);
724 __u16 vlen = btf_vlen(t);
727 new_cont_id = t->name_off == 0 ? cont_id : id;
728 for (i = 0; i < vlen; i++, m++)
729 btf_dump_emit_type(d, m->type, new_cont_id);
730 } else if (!tstate->fwd_emitted && id != cont_id) {
731 btf_dump_emit_struct_fwd(d, id, t);
732 btf_dump_printf(d, ";\n\n");
733 tstate->fwd_emitted = 1;
737 btf_dump_emit_struct_def(d, id, t, 0);
738 btf_dump_printf(d, ";\n\n");
739 tstate->emit_state = EMITTED;
741 tstate->emit_state = NOT_EMITTED;
744 case BTF_KIND_FUNC_PROTO: {
745 const struct btf_param *p = btf_params(t);
746 __u16 vlen = btf_vlen(t);
749 btf_dump_emit_type(d, t->type, cont_id);
750 for (i = 0; i < vlen; i++, p++)
751 btf_dump_emit_type(d, p->type, cont_id);
760 static bool btf_is_struct_packed(const struct btf *btf, __u32 id,
761 const struct btf_type *t)
763 const struct btf_member *m;
764 int align, i, bit_sz;
767 align = btf__align_of(btf, id);
768 /* size of a non-packed struct has to be a multiple of its alignment*/
769 if (align && t->size % align)
774 /* all non-bitfield fields have to be naturally aligned */
775 for (i = 0; i < vlen; i++, m++) {
776 align = btf__align_of(btf, m->type);
777 bit_sz = btf_member_bitfield_size(t, i);
778 if (align && bit_sz == 0 && m->offset % (8 * align) != 0)
783 * if original struct was marked as packed, but its layout is
784 * naturally aligned, we'll detect that it's not packed
789 static int chip_away_bits(int total, int at_most)
791 return total % at_most ? : at_most;
794 static void btf_dump_emit_bit_padding(const struct btf_dump *d,
795 int cur_off, int m_off, int m_bit_sz,
798 int off_diff = m_off - cur_off;
799 int ptr_bits = sizeof(void *) * 8;
804 if (m_bit_sz == 0 && off_diff < align * 8)
805 /* natural padding will take care of a gap */
808 while (off_diff > 0) {
809 const char *pad_type;
812 if (ptr_bits > 32 && off_diff > 32) {
814 pad_bits = chip_away_bits(off_diff, ptr_bits);
815 } else if (off_diff > 16) {
817 pad_bits = chip_away_bits(off_diff, 32);
818 } else if (off_diff > 8) {
820 pad_bits = chip_away_bits(off_diff, 16);
823 pad_bits = chip_away_bits(off_diff, 8);
825 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
826 off_diff -= pad_bits;
830 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
831 const struct btf_type *t)
833 btf_dump_printf(d, "%s %s",
834 btf_is_struct(t) ? "struct" : "union",
835 btf_dump_type_name(d, id));
838 static void btf_dump_emit_struct_def(struct btf_dump *d,
840 const struct btf_type *t,
843 const struct btf_member *m = btf_members(t);
844 bool is_struct = btf_is_struct(t);
845 int align, i, packed, off = 0;
846 __u16 vlen = btf_vlen(t);
848 packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
850 btf_dump_printf(d, "%s%s%s {",
851 is_struct ? "struct" : "union",
852 t->name_off ? " " : "",
853 btf_dump_type_name(d, id));
855 for (i = 0; i < vlen; i++, m++) {
859 fname = btf_name_of(d, m->name_off);
860 m_sz = btf_member_bitfield_size(t, i);
861 m_off = btf_member_bit_offset(t, i);
862 align = packed ? 1 : btf__align_of(d->btf, m->type);
864 btf_dump_emit_bit_padding(d, off, m_off, m_sz, align, lvl + 1);
865 btf_dump_printf(d, "\n%s", pfx(lvl + 1));
866 btf_dump_emit_type_decl(d, m->type, fname, lvl + 1);
869 btf_dump_printf(d, ": %d", m_sz);
872 m_sz = max(0, btf__resolve_size(d->btf, m->type));
873 off = m_off + m_sz * 8;
875 btf_dump_printf(d, ";");
878 /* pad at the end, if necessary */
880 align = packed ? 1 : btf__align_of(d->btf, id);
881 btf_dump_emit_bit_padding(d, off, t->size * 8, 0, align,
886 btf_dump_printf(d, "\n");
887 btf_dump_printf(d, "%s}", pfx(lvl));
889 btf_dump_printf(d, " __attribute__((packed))");
892 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
893 const struct btf_type *t)
895 btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id));
898 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
899 const struct btf_type *t,
902 const struct btf_enum *v = btf_enum(t);
903 __u16 vlen = btf_vlen(t);
908 btf_dump_printf(d, "enum%s%s",
909 t->name_off ? " " : "",
910 btf_dump_type_name(d, id));
913 btf_dump_printf(d, " {");
914 for (i = 0; i < vlen; i++, v++) {
915 name = btf_name_of(d, v->name_off);
916 /* enumerators share namespace with typedef idents */
917 dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
919 btf_dump_printf(d, "\n%s%s___%zu = %d,",
920 pfx(lvl + 1), name, dup_cnt,
923 btf_dump_printf(d, "\n%s%s = %d,",
928 btf_dump_printf(d, "\n%s}", pfx(lvl));
932 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
933 const struct btf_type *t)
935 const char *name = btf_dump_type_name(d, id);
938 btf_dump_printf(d, "union %s", name);
940 btf_dump_printf(d, "struct %s", name);
943 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
944 const struct btf_type *t, int lvl)
946 const char *name = btf_dump_ident_name(d, id);
949 * Old GCC versions are emitting invalid typedef for __gnuc_va_list
950 * pointing to VOID. This generates warnings from btf_dump() and
951 * results in uncompilable header file, so we are fixing it up here
952 * with valid typedef into __builtin_va_list.
954 if (t->type == 0 && strcmp(name, "__gnuc_va_list") == 0) {
955 btf_dump_printf(d, "typedef __builtin_va_list __gnuc_va_list");
959 btf_dump_printf(d, "typedef ");
960 btf_dump_emit_type_decl(d, t->type, name, lvl);
963 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
968 if (d->decl_stack_cnt >= d->decl_stack_cap) {
969 new_cap = max(16, d->decl_stack_cap * 3 / 2);
970 new_stack = realloc(d->decl_stack,
971 new_cap * sizeof(new_stack[0]));
974 d->decl_stack = new_stack;
975 d->decl_stack_cap = new_cap;
978 d->decl_stack[d->decl_stack_cnt++] = id;
984 * Emit type declaration (e.g., field type declaration in a struct or argument
985 * declaration in function prototype) in correct C syntax.
987 * For most types it's trivial, but there are few quirky type declaration
988 * cases worth mentioning:
989 * - function prototypes (especially nesting of function prototypes);
991 * - const/volatile/restrict for pointers vs other types.
993 * For a good discussion of *PARSING* C syntax (as a human), see
994 * Peter van der Linden's "Expert C Programming: Deep C Secrets",
995 * Ch.3 "Unscrambling Declarations in C".
997 * It won't help with BTF to C conversion much, though, as it's an opposite
998 * problem. So we came up with this algorithm in reverse to van der Linden's
999 * parsing algorithm. It goes from structured BTF representation of type
1000 * declaration to a valid compilable C syntax.
1002 * For instance, consider this C typedef:
1003 * typedef const int * const * arr[10] arr_t;
1004 * It will be represented in BTF with this chain of BTF types:
1005 * [typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int]
1007 * Notice how [const] modifier always goes before type it modifies in BTF type
1008 * graph, but in C syntax, const/volatile/restrict modifiers are written to
1009 * the right of pointers, but to the left of other types. There are also other
1010 * quirks, like function pointers, arrays of them, functions returning other
1013 * We handle that by pushing all the types to a stack, until we hit "terminal"
1014 * type (int/enum/struct/union/fwd). Then depending on the kind of a type on
1015 * top of a stack, modifiers are handled differently. Array/function pointers
1016 * have also wildly different syntax and how nesting of them are done. See
1017 * code for authoritative definition.
1019 * To avoid allocating new stack for each independent chain of BTF types, we
1020 * share one bigger stack, with each chain working only on its own local view
1021 * of a stack frame. Some care is required to "pop" stack frames after
1022 * processing type declaration chain.
1024 int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
1025 const struct btf_dump_emit_type_decl_opts *opts)
1030 if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
1033 fname = OPTS_GET(opts, field_name, "");
1034 lvl = OPTS_GET(opts, indent_level, 0);
1035 btf_dump_emit_type_decl(d, id, fname, lvl);
1039 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
1040 const char *fname, int lvl)
1042 struct id_stack decl_stack;
1043 const struct btf_type *t;
1044 int err, stack_start;
1046 stack_start = d->decl_stack_cnt;
1048 err = btf_dump_push_decl_stack_id(d, id);
1051 * if we don't have enough memory for entire type decl
1052 * chain, restore stack, emit warning, and try to
1053 * proceed nevertheless
1055 pr_warn("not enough memory for decl stack:%d", err);
1056 d->decl_stack_cnt = stack_start;
1064 t = btf__type_by_id(d->btf, id);
1065 switch (btf_kind(t)) {
1067 case BTF_KIND_VOLATILE:
1068 case BTF_KIND_CONST:
1069 case BTF_KIND_RESTRICT:
1070 case BTF_KIND_FUNC_PROTO:
1073 case BTF_KIND_ARRAY:
1074 id = btf_array(t)->type;
1079 case BTF_KIND_STRUCT:
1080 case BTF_KIND_UNION:
1081 case BTF_KIND_TYPEDEF:
1084 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1091 * We might be inside a chain of declarations (e.g., array of function
1092 * pointers returning anonymous (so inlined) structs, having another
1093 * array field). Each of those needs its own "stack frame" to handle
1094 * emitting of declarations. Those stack frames are non-overlapping
1095 * portions of shared btf_dump->decl_stack. To make it a bit nicer to
1096 * handle this set of nested stacks, we create a view corresponding to
1097 * our own "stack frame" and work with it as an independent stack.
1098 * We'll need to clean up after emit_type_chain() returns, though.
1100 decl_stack.ids = d->decl_stack + stack_start;
1101 decl_stack.cnt = d->decl_stack_cnt - stack_start;
1102 btf_dump_emit_type_chain(d, &decl_stack, fname, lvl);
1104 * emit_type_chain() guarantees that it will pop its entire decl_stack
1105 * frame before returning. But it works with a read-only view into
1106 * decl_stack, so it doesn't actually pop anything from the
1107 * perspective of shared btf_dump->decl_stack, per se. We need to
1108 * reset decl_stack state to how it was before us to avoid it growing
1111 d->decl_stack_cnt = stack_start;
1114 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
1116 const struct btf_type *t;
1119 while (decl_stack->cnt) {
1120 id = decl_stack->ids[decl_stack->cnt - 1];
1121 t = btf__type_by_id(d->btf, id);
1123 switch (btf_kind(t)) {
1124 case BTF_KIND_VOLATILE:
1125 btf_dump_printf(d, "volatile ");
1127 case BTF_KIND_CONST:
1128 btf_dump_printf(d, "const ");
1130 case BTF_KIND_RESTRICT:
1131 btf_dump_printf(d, "restrict ");
1140 static void btf_dump_emit_name(const struct btf_dump *d,
1141 const char *name, bool last_was_ptr)
1143 bool separate = name[0] && !last_was_ptr;
1145 btf_dump_printf(d, "%s%s", separate ? " " : "", name);
1148 static void btf_dump_emit_type_chain(struct btf_dump *d,
1149 struct id_stack *decls,
1150 const char *fname, int lvl)
1153 * last_was_ptr is used to determine if we need to separate pointer
1154 * asterisk (*) from previous part of type signature with space, so
1155 * that we get `int ***`, instead of `int * * *`. We default to true
1156 * for cases where we have single pointer in a chain. E.g., in ptr ->
1157 * func_proto case. func_proto will start a new emit_type_chain call
1158 * with just ptr, which should be emitted as (*) or (*<fname>), so we
1159 * don't want to prepend space for that last pointer.
1161 bool last_was_ptr = true;
1162 const struct btf_type *t;
1167 while (decls->cnt) {
1168 id = decls->ids[--decls->cnt];
1170 /* VOID is a special snowflake */
1171 btf_dump_emit_mods(d, decls);
1172 btf_dump_printf(d, "void");
1173 last_was_ptr = false;
1177 t = btf__type_by_id(d->btf, id);
1182 btf_dump_emit_mods(d, decls);
1183 name = btf_name_of(d, t->name_off);
1184 btf_dump_printf(d, "%s", name);
1186 case BTF_KIND_STRUCT:
1187 case BTF_KIND_UNION:
1188 btf_dump_emit_mods(d, decls);
1189 /* inline anonymous struct/union */
1190 if (t->name_off == 0)
1191 btf_dump_emit_struct_def(d, id, t, lvl);
1193 btf_dump_emit_struct_fwd(d, id, t);
1196 btf_dump_emit_mods(d, decls);
1197 /* inline anonymous enum */
1198 if (t->name_off == 0)
1199 btf_dump_emit_enum_def(d, id, t, lvl);
1201 btf_dump_emit_enum_fwd(d, id, t);
1204 btf_dump_emit_mods(d, decls);
1205 btf_dump_emit_fwd_def(d, id, t);
1207 case BTF_KIND_TYPEDEF:
1208 btf_dump_emit_mods(d, decls);
1209 btf_dump_printf(d, "%s", btf_dump_ident_name(d, id));
1212 btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *");
1214 case BTF_KIND_VOLATILE:
1215 btf_dump_printf(d, " volatile");
1217 case BTF_KIND_CONST:
1218 btf_dump_printf(d, " const");
1220 case BTF_KIND_RESTRICT:
1221 btf_dump_printf(d, " restrict");
1223 case BTF_KIND_ARRAY: {
1224 const struct btf_array *a = btf_array(t);
1225 const struct btf_type *next_t;
1230 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354)
1231 * which causes it to emit extra const/volatile
1232 * modifiers for an array, if array's element type has
1233 * const/volatile modifiers. Clang doesn't do that.
1234 * In general, it doesn't seem very meaningful to have
1235 * a const/volatile modifier for array, so we are
1236 * going to silently skip them here.
1238 while (decls->cnt) {
1239 next_id = decls->ids[decls->cnt - 1];
1240 next_t = btf__type_by_id(d->btf, next_id);
1241 if (btf_is_mod(next_t))
1247 if (decls->cnt == 0) {
1248 btf_dump_emit_name(d, fname, last_was_ptr);
1249 btf_dump_printf(d, "[%u]", a->nelems);
1253 next_id = decls->ids[decls->cnt - 1];
1254 next_t = btf__type_by_id(d->btf, next_id);
1255 multidim = btf_is_array(next_t);
1256 /* we need space if we have named non-pointer */
1257 if (fname[0] && !last_was_ptr)
1258 btf_dump_printf(d, " ");
1259 /* no parentheses for multi-dimensional array */
1261 btf_dump_printf(d, "(");
1262 btf_dump_emit_type_chain(d, decls, fname, lvl);
1264 btf_dump_printf(d, ")");
1265 btf_dump_printf(d, "[%u]", a->nelems);
1268 case BTF_KIND_FUNC_PROTO: {
1269 const struct btf_param *p = btf_params(t);
1270 __u16 vlen = btf_vlen(t);
1273 btf_dump_emit_mods(d, decls);
1275 btf_dump_printf(d, " (");
1276 btf_dump_emit_type_chain(d, decls, fname, lvl);
1277 btf_dump_printf(d, ")");
1279 btf_dump_emit_name(d, fname, last_was_ptr);
1281 btf_dump_printf(d, "(");
1283 * Clang for BPF target generates func_proto with no
1284 * args as a func_proto with a single void arg (e.g.,
1285 * `int (*f)(void)` vs just `int (*f)()`). We are
1286 * going to pretend there are no args for such case.
1288 if (vlen == 1 && p->type == 0) {
1289 btf_dump_printf(d, ")");
1293 for (i = 0; i < vlen; i++, p++) {
1295 btf_dump_printf(d, ", ");
1297 /* last arg of type void is vararg */
1298 if (i == vlen - 1 && p->type == 0) {
1299 btf_dump_printf(d, "...");
1303 name = btf_name_of(d, p->name_off);
1304 btf_dump_emit_type_decl(d, p->type, name, lvl);
1307 btf_dump_printf(d, ")");
1311 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1316 last_was_ptr = kind == BTF_KIND_PTR;
1319 btf_dump_emit_name(d, fname, last_was_ptr);
1322 /* return number of duplicates (occurrences) of a given name */
1323 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
1324 const char *orig_name)
1328 hashmap__find(name_map, orig_name, (void **)&dup_cnt);
1330 hashmap__set(name_map, orig_name, (void *)dup_cnt, NULL, NULL);
1335 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
1336 struct hashmap *name_map)
1338 struct btf_dump_type_aux_state *s = &d->type_states[id];
1339 const struct btf_type *t = btf__type_by_id(d->btf, id);
1340 const char *orig_name = btf_name_of(d, t->name_off);
1341 const char **cached_name = &d->cached_names[id];
1344 if (t->name_off == 0)
1347 if (s->name_resolved)
1348 return *cached_name ? *cached_name : orig_name;
1350 dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
1352 const size_t max_len = 256;
1353 char new_name[max_len];
1355 snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
1356 *cached_name = strdup(new_name);
1359 s->name_resolved = 1;
1360 return *cached_name ? *cached_name : orig_name;
1363 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id)
1365 return btf_dump_resolve_name(d, id, d->type_names);
1368 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id)
1370 return btf_dump_resolve_name(d, id, d->ident_names);