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>
16 #include <linux/kernel.h>
20 #include "libbpf_internal.h"
22 /* make sure libbpf doesn't use kernel-only integer typedefs */
23 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
25 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
26 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
28 static const char *pfx(int lvl)
30 return lvl >= PREFIX_CNT ? PREFIXES : &PREFIXES[PREFIX_CNT - lvl];
33 enum btf_dump_type_order_state {
39 enum btf_dump_type_emit_state {
45 /* per-type auxiliary state */
46 struct btf_dump_type_aux_state {
47 /* topological sorting state */
48 enum btf_dump_type_order_state order_state: 2;
49 /* emitting state used to determine the need for forward declaration */
50 enum btf_dump_type_emit_state emit_state: 2;
51 /* whether forward declaration was already emitted */
53 /* whether unique non-duplicate name was already assigned */
54 __u8 name_resolved: 1;
55 /* whether type is referenced from any other type */
60 const struct btf *btf;
61 const struct btf_ext *btf_ext;
62 btf_dump_printf_fn_t printf_fn;
63 struct btf_dump_opts opts;
67 /* per-type auxiliary state */
68 struct btf_dump_type_aux_state *type_states;
69 /* per-type optional cached unique name, must be freed, if present */
70 const char **cached_names;
72 /* topo-sorted list of dependent type definitions */
78 * stack of type declarations (e.g., chain of modifiers, arrays,
85 /* maps struct/union/enum name to a number of name occurrences */
86 struct hashmap *type_names;
88 * maps typedef identifiers and enum value names to a number of such
91 struct hashmap *ident_names;
94 static size_t str_hash_fn(const void *key, void *ctx)
106 static bool str_equal_fn(const void *a, const void *b, void *ctx)
108 return strcmp(a, b) == 0;
111 static const char *btf_name_of(const struct btf_dump *d, __u32 name_off)
113 return btf__name_by_offset(d->btf, name_off);
116 static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)
121 d->printf_fn(d->opts.ctx, fmt, args);
125 static int btf_dump_mark_referenced(struct btf_dump *d);
127 struct btf_dump *btf_dump__new(const struct btf *btf,
128 const struct btf_ext *btf_ext,
129 const struct btf_dump_opts *opts,
130 btf_dump_printf_fn_t printf_fn)
135 d = calloc(1, sizeof(struct btf_dump));
137 return ERR_PTR(-ENOMEM);
140 d->btf_ext = btf_ext;
141 d->printf_fn = printf_fn;
142 d->opts.ctx = opts ? opts->ctx : NULL;
143 d->ptr_sz = btf__pointer_size(btf) ? : sizeof(void *);
145 d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
146 if (IS_ERR(d->type_names)) {
147 err = PTR_ERR(d->type_names);
148 d->type_names = NULL;
151 d->ident_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
152 if (IS_ERR(d->ident_names)) {
153 err = PTR_ERR(d->ident_names);
154 d->ident_names = NULL;
157 d->type_states = calloc(1 + btf__get_nr_types(d->btf),
158 sizeof(d->type_states[0]));
159 if (!d->type_states) {
163 d->cached_names = calloc(1 + btf__get_nr_types(d->btf),
164 sizeof(d->cached_names[0]));
165 if (!d->cached_names) {
170 /* VOID is special */
171 d->type_states[0].order_state = ORDERED;
172 d->type_states[0].emit_state = EMITTED;
174 /* eagerly determine referenced types for anon enums */
175 err = btf_dump_mark_referenced(d);
185 void btf_dump__free(struct btf_dump *d)
189 if (IS_ERR_OR_NULL(d))
192 free(d->type_states);
193 if (d->cached_names) {
194 /* any set cached name is owned by us and should be freed */
195 for (i = 0, cnt = btf__get_nr_types(d->btf); i <= cnt; i++) {
196 if (d->cached_names[i])
197 free((void *)d->cached_names[i]);
200 free(d->cached_names);
203 hashmap__free(d->type_names);
204 hashmap__free(d->ident_names);
209 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr);
210 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id);
213 * Dump BTF type in a compilable C syntax, including all the necessary
214 * dependent types, necessary for compilation. If some of the dependent types
215 * were already emitted as part of previous btf_dump__dump_type() invocation
216 * for another type, they won't be emitted again. This API allows callers to
217 * filter out BTF types according to user-defined criterias and emitted only
218 * minimal subset of types, necessary to compile everything. Full struct/union
219 * definitions will still be emitted, even if the only usage is through
220 * pointer and could be satisfied with just a forward declaration.
222 * Dumping is done in two high-level passes:
223 * 1. Topologically sort type definitions to satisfy C rules of compilation.
224 * 2. Emit type definitions in C syntax.
226 * Returns 0 on success; <0, otherwise.
228 int btf_dump__dump_type(struct btf_dump *d, __u32 id)
232 if (id > btf__get_nr_types(d->btf))
235 d->emit_queue_cnt = 0;
236 err = btf_dump_order_type(d, id, false);
240 for (i = 0; i < d->emit_queue_cnt; i++)
241 btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
247 * Mark all types that are referenced from any other type. This is used to
248 * determine top-level anonymous enums that need to be emitted as an
249 * independent type declarations.
250 * Anonymous enums come in two flavors: either embedded in a struct's field
251 * definition, in which case they have to be declared inline as part of field
252 * type declaration; or as a top-level anonymous enum, typically used for
253 * declaring global constants. It's impossible to distinguish between two
254 * without knowning whether given enum type was referenced from other type:
255 * top-level anonymous enum won't be referenced by anything, while embedded
258 static int btf_dump_mark_referenced(struct btf_dump *d)
260 int i, j, n = btf__get_nr_types(d->btf);
261 const struct btf_type *t;
264 for (i = 1; i <= n; i++) {
265 t = btf__type_by_id(d->btf, i);
268 switch (btf_kind(t)) {
274 case BTF_KIND_VOLATILE:
276 case BTF_KIND_RESTRICT:
278 case BTF_KIND_TYPEDEF:
281 d->type_states[t->type].referenced = 1;
284 case BTF_KIND_ARRAY: {
285 const struct btf_array *a = btf_array(t);
287 d->type_states[a->index_type].referenced = 1;
288 d->type_states[a->type].referenced = 1;
291 case BTF_KIND_STRUCT:
292 case BTF_KIND_UNION: {
293 const struct btf_member *m = btf_members(t);
295 for (j = 0; j < vlen; j++, m++)
296 d->type_states[m->type].referenced = 1;
299 case BTF_KIND_FUNC_PROTO: {
300 const struct btf_param *p = btf_params(t);
302 for (j = 0; j < vlen; j++, p++)
303 d->type_states[p->type].referenced = 1;
306 case BTF_KIND_DATASEC: {
307 const struct btf_var_secinfo *v = btf_var_secinfos(t);
309 for (j = 0; j < vlen; j++, v++)
310 d->type_states[v->type].referenced = 1;
319 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
324 if (d->emit_queue_cnt >= d->emit_queue_cap) {
325 new_cap = max(16, d->emit_queue_cap * 3 / 2);
326 new_queue = realloc(d->emit_queue,
327 new_cap * sizeof(new_queue[0]));
330 d->emit_queue = new_queue;
331 d->emit_queue_cap = new_cap;
334 d->emit_queue[d->emit_queue_cnt++] = id;
339 * Determine order of emitting dependent types and specified type to satisfy
340 * C compilation rules. This is done through topological sorting with an
341 * additional complication which comes from C rules. The main idea for C is
342 * that if some type is "embedded" into a struct/union, it's size needs to be
343 * known at the time of definition of containing type. E.g., for:
346 * struct B { struct A x; }
348 * struct A *HAS* to be defined before struct B, because it's "embedded",
349 * i.e., it is part of struct B layout. But in the following case:
352 * struct B { struct A *x; }
355 * it's enough to just have a forward declaration of struct A at the time of
356 * struct B definition, as struct B has a pointer to struct A, so the size of
357 * field x is known without knowing struct A size: it's sizeof(void *).
359 * Unfortunately, there are some trickier cases we need to handle, e.g.:
361 * struct A {}; // if this was forward-declaration: compilation error
363 * struct { // anonymous struct
368 * In this case, struct B's field x is a pointer, so it's size is known
369 * regardless of the size of (anonymous) struct it points to. But because this
370 * struct is anonymous and thus defined inline inside struct B, *and* it
371 * embeds struct A, compiler requires full definition of struct A to be known
372 * before struct B can be defined. This creates a transitive dependency
373 * between struct A and struct B. If struct A was forward-declared before
374 * struct B definition and fully defined after struct B definition, that would
375 * trigger compilation error.
377 * All this means that while we are doing topological sorting on BTF type
378 * graph, we need to determine relationships between different types (graph
380 * - weak link (relationship) between X and Y, if Y *CAN* be
381 * forward-declared at the point of X definition;
382 * - strong link, if Y *HAS* to be fully-defined before X can be defined.
384 * The rule is as follows. Given a chain of BTF types from X to Y, if there is
385 * BTF_KIND_PTR type in the chain and at least one non-anonymous type
386 * Z (excluding X, including Y), then link is weak. Otherwise, it's strong.
387 * Weak/strong relationship is determined recursively during DFS traversal and
388 * is returned as a result from btf_dump_order_type().
390 * btf_dump_order_type() is trying to avoid unnecessary forward declarations,
391 * but it is not guaranteeing that no extraneous forward declarations will be
394 * To avoid extra work, algorithm marks some of BTF types as ORDERED, when
395 * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT,
396 * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the
397 * entire graph path, so depending where from one came to that BTF type, it
398 * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM,
399 * once they are processed, there is no need to do it again, so they are
400 * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces
401 * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But
402 * in any case, once those are processed, no need to do it again, as the
403 * result won't change.
406 * - 1, if type is part of strong link (so there is strong topological
407 * ordering requirements);
408 * - 0, if type is part of weak link (so can be satisfied through forward
410 * - <0, on error (e.g., unsatisfiable type loop detected).
412 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
415 * Order state is used to detect strong link cycles, but only for BTF
416 * kinds that are or could be an independent definition (i.e.,
417 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays,
418 * func_protos, modifiers are just means to get to these definitions.
419 * Int/void don't need definitions, they are assumed to be always
420 * properly defined. We also ignore datasec, var, and funcs for now.
421 * So for all non-defining kinds, we never even set ordering state,
422 * for defining kinds we set ORDERING and subsequently ORDERED if it
423 * forms a strong link.
425 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
426 const struct btf_type *t;
430 /* return true, letting typedefs know that it's ok to be emitted */
431 if (tstate->order_state == ORDERED)
434 t = btf__type_by_id(d->btf, id);
436 if (tstate->order_state == ORDERING) {
437 /* type loop, but resolvable through fwd declaration */
438 if (btf_is_composite(t) && through_ptr && t->name_off != 0)
440 pr_warn("unsatisfiable type cycle, id:[%u]\n", id);
444 switch (btf_kind(t)) {
446 tstate->order_state = ORDERED;
450 err = btf_dump_order_type(d, t->type, true);
451 tstate->order_state = ORDERED;
455 return btf_dump_order_type(d, btf_array(t)->type, through_ptr);
457 case BTF_KIND_STRUCT:
458 case BTF_KIND_UNION: {
459 const struct btf_member *m = btf_members(t);
461 * struct/union is part of strong link, only if it's embedded
462 * (so no ptr in a path) or it's anonymous (so has to be
463 * defined inline, even if declared through ptr)
465 if (through_ptr && t->name_off != 0)
468 tstate->order_state = ORDERING;
471 for (i = 0; i < vlen; i++, m++) {
472 err = btf_dump_order_type(d, m->type, false);
477 if (t->name_off != 0) {
478 err = btf_dump_add_emit_queue_id(d, id);
483 tstate->order_state = ORDERED;
489 * non-anonymous or non-referenced enums are top-level
490 * declarations and should be emitted. Same logic can be
491 * applied to FWDs, it won't hurt anyways.
493 if (t->name_off != 0 || !tstate->referenced) {
494 err = btf_dump_add_emit_queue_id(d, id);
498 tstate->order_state = ORDERED;
501 case BTF_KIND_TYPEDEF: {
504 is_strong = btf_dump_order_type(d, t->type, through_ptr);
508 /* typedef is similar to struct/union w.r.t. fwd-decls */
509 if (through_ptr && !is_strong)
512 /* typedef is always a named definition */
513 err = btf_dump_add_emit_queue_id(d, id);
517 d->type_states[id].order_state = ORDERED;
520 case BTF_KIND_VOLATILE:
522 case BTF_KIND_RESTRICT:
523 return btf_dump_order_type(d, t->type, through_ptr);
525 case BTF_KIND_FUNC_PROTO: {
526 const struct btf_param *p = btf_params(t);
529 err = btf_dump_order_type(d, t->type, through_ptr);
535 for (i = 0; i < vlen; i++, p++) {
536 err = btf_dump_order_type(d, p->type, through_ptr);
546 case BTF_KIND_DATASEC:
547 d->type_states[id].order_state = ORDERED;
555 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
556 const struct btf_type *t);
558 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
559 const struct btf_type *t);
560 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id,
561 const struct btf_type *t, int lvl);
563 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
564 const struct btf_type *t);
565 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
566 const struct btf_type *t, int lvl);
568 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
569 const struct btf_type *t);
571 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
572 const struct btf_type *t, int lvl);
574 /* a local view into a shared stack */
580 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
581 const char *fname, int lvl);
582 static void btf_dump_emit_type_chain(struct btf_dump *d,
583 struct id_stack *decl_stack,
584 const char *fname, int lvl);
586 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
587 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
588 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
589 const char *orig_name);
591 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
593 const struct btf_type *t = btf__type_by_id(d->btf, id);
595 /* __builtin_va_list is a compiler built-in, which causes compilation
596 * errors, when compiling w/ different compiler, then used to compile
597 * original code (e.g., GCC to compile kernel, Clang to use generated
598 * C header from BTF). As it is built-in, it should be already defined
599 * properly internally in compiler.
601 if (t->name_off == 0)
603 return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0;
607 * Emit C-syntax definitions of types from chains of BTF types.
609 * High-level handling of determining necessary forward declarations are handled
610 * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type
611 * declarations/definitions in C syntax are handled by a combo of
612 * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to
613 * corresponding btf_dump_emit_*_{def,fwd}() functions.
615 * We also keep track of "containing struct/union type ID" to determine when
616 * we reference it from inside and thus can avoid emitting unnecessary forward
619 * This algorithm is designed in such a way, that even if some error occurs
620 * (either technical, e.g., out of memory, or logical, i.e., malformed BTF
621 * that doesn't comply to C rules completely), algorithm will try to proceed
622 * and produce as much meaningful output as possible.
624 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
626 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
627 bool top_level_def = cont_id == 0;
628 const struct btf_type *t;
631 if (tstate->emit_state == EMITTED)
634 t = btf__type_by_id(d->btf, id);
637 if (tstate->emit_state == EMITTING) {
638 if (tstate->fwd_emitted)
642 case BTF_KIND_STRUCT:
645 * if we are referencing a struct/union that we are
646 * part of - then no need for fwd declaration
650 if (t->name_off == 0) {
651 pr_warn("anonymous struct/union loop, id:[%u]\n",
655 btf_dump_emit_struct_fwd(d, id, t);
656 btf_dump_printf(d, ";\n\n");
657 tstate->fwd_emitted = 1;
659 case BTF_KIND_TYPEDEF:
661 * for typedef fwd_emitted means typedef definition
662 * was emitted, but it can be used only for "weak"
663 * references through pointer only, not for embedding
665 if (!btf_dump_is_blacklisted(d, id)) {
666 btf_dump_emit_typedef_def(d, id, t, 0);
667 btf_dump_printf(d, ";\n\n");
669 tstate->fwd_emitted = 1;
680 /* Emit type alias definitions if necessary */
681 btf_dump_emit_missing_aliases(d, id, t);
683 tstate->emit_state = EMITTED;
687 btf_dump_emit_enum_def(d, id, t, 0);
688 btf_dump_printf(d, ";\n\n");
690 tstate->emit_state = EMITTED;
693 case BTF_KIND_VOLATILE:
695 case BTF_KIND_RESTRICT:
696 btf_dump_emit_type(d, t->type, cont_id);
699 btf_dump_emit_type(d, btf_array(t)->type, cont_id);
702 btf_dump_emit_fwd_def(d, id, t);
703 btf_dump_printf(d, ";\n\n");
704 tstate->emit_state = EMITTED;
706 case BTF_KIND_TYPEDEF:
707 tstate->emit_state = EMITTING;
708 btf_dump_emit_type(d, t->type, id);
710 * typedef can server as both definition and forward
711 * declaration; at this stage someone depends on
712 * typedef as a forward declaration (refers to it
713 * through pointer), so unless we already did it,
714 * emit typedef as a forward declaration
716 if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
717 btf_dump_emit_typedef_def(d, id, t, 0);
718 btf_dump_printf(d, ";\n\n");
720 tstate->emit_state = EMITTED;
722 case BTF_KIND_STRUCT:
724 tstate->emit_state = EMITTING;
725 /* if it's a top-level struct/union definition or struct/union
726 * is anonymous, then in C we'll be emitting all fields and
727 * their types (as opposed to just `struct X`), so we need to
728 * make sure that all types, referenced from struct/union
729 * members have necessary forward-declarations, where
732 if (top_level_def || t->name_off == 0) {
733 const struct btf_member *m = btf_members(t);
734 __u16 vlen = btf_vlen(t);
737 new_cont_id = t->name_off == 0 ? cont_id : id;
738 for (i = 0; i < vlen; i++, m++)
739 btf_dump_emit_type(d, m->type, new_cont_id);
740 } else if (!tstate->fwd_emitted && id != cont_id) {
741 btf_dump_emit_struct_fwd(d, id, t);
742 btf_dump_printf(d, ";\n\n");
743 tstate->fwd_emitted = 1;
747 btf_dump_emit_struct_def(d, id, t, 0);
748 btf_dump_printf(d, ";\n\n");
749 tstate->emit_state = EMITTED;
751 tstate->emit_state = NOT_EMITTED;
754 case BTF_KIND_FUNC_PROTO: {
755 const struct btf_param *p = btf_params(t);
756 __u16 vlen = btf_vlen(t);
759 btf_dump_emit_type(d, t->type, cont_id);
760 for (i = 0; i < vlen; i++, p++)
761 btf_dump_emit_type(d, p->type, cont_id);
770 static bool btf_is_struct_packed(const struct btf *btf, __u32 id,
771 const struct btf_type *t)
773 const struct btf_member *m;
774 int align, i, bit_sz;
777 align = btf__align_of(btf, id);
778 /* size of a non-packed struct has to be a multiple of its alignment*/
779 if (align && t->size % align)
784 /* all non-bitfield fields have to be naturally aligned */
785 for (i = 0; i < vlen; i++, m++) {
786 align = btf__align_of(btf, m->type);
787 bit_sz = btf_member_bitfield_size(t, i);
788 if (align && bit_sz == 0 && m->offset % (8 * align) != 0)
793 * if original struct was marked as packed, but its layout is
794 * naturally aligned, we'll detect that it's not packed
799 static int chip_away_bits(int total, int at_most)
801 return total % at_most ? : at_most;
804 static void btf_dump_emit_bit_padding(const struct btf_dump *d,
805 int cur_off, int m_off, int m_bit_sz,
808 int off_diff = m_off - cur_off;
809 int ptr_bits = d->ptr_sz * 8;
814 if (m_bit_sz == 0 && off_diff < align * 8)
815 /* natural padding will take care of a gap */
818 while (off_diff > 0) {
819 const char *pad_type;
822 if (ptr_bits > 32 && off_diff > 32) {
824 pad_bits = chip_away_bits(off_diff, ptr_bits);
825 } else if (off_diff > 16) {
827 pad_bits = chip_away_bits(off_diff, 32);
828 } else if (off_diff > 8) {
830 pad_bits = chip_away_bits(off_diff, 16);
833 pad_bits = chip_away_bits(off_diff, 8);
835 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
836 off_diff -= pad_bits;
840 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
841 const struct btf_type *t)
843 btf_dump_printf(d, "%s %s",
844 btf_is_struct(t) ? "struct" : "union",
845 btf_dump_type_name(d, id));
848 static void btf_dump_emit_struct_def(struct btf_dump *d,
850 const struct btf_type *t,
853 const struct btf_member *m = btf_members(t);
854 bool is_struct = btf_is_struct(t);
855 int align, i, packed, off = 0;
856 __u16 vlen = btf_vlen(t);
858 packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
860 btf_dump_printf(d, "%s%s%s {",
861 is_struct ? "struct" : "union",
862 t->name_off ? " " : "",
863 btf_dump_type_name(d, id));
865 for (i = 0; i < vlen; i++, m++) {
869 fname = btf_name_of(d, m->name_off);
870 m_sz = btf_member_bitfield_size(t, i);
871 m_off = btf_member_bit_offset(t, i);
872 align = packed ? 1 : btf__align_of(d->btf, m->type);
874 btf_dump_emit_bit_padding(d, off, m_off, m_sz, align, lvl + 1);
875 btf_dump_printf(d, "\n%s", pfx(lvl + 1));
876 btf_dump_emit_type_decl(d, m->type, fname, lvl + 1);
879 btf_dump_printf(d, ": %d", m_sz);
882 m_sz = max(0LL, btf__resolve_size(d->btf, m->type));
883 off = m_off + m_sz * 8;
885 btf_dump_printf(d, ";");
888 /* pad at the end, if necessary */
890 align = packed ? 1 : btf__align_of(d->btf, id);
891 btf_dump_emit_bit_padding(d, off, t->size * 8, 0, align,
896 btf_dump_printf(d, "\n");
897 btf_dump_printf(d, "%s}", pfx(lvl));
899 btf_dump_printf(d, " __attribute__((packed))");
902 static const char *missing_base_types[][2] = {
904 * GCC emits typedefs to its internal __PolyX_t types when compiling Arm
905 * SIMD intrinsics. Alias them to standard base types.
907 { "__Poly8_t", "unsigned char" },
908 { "__Poly16_t", "unsigned short" },
909 { "__Poly64_t", "unsigned long long" },
910 { "__Poly128_t", "unsigned __int128" },
913 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
914 const struct btf_type *t)
916 const char *name = btf_dump_type_name(d, id);
919 for (i = 0; i < ARRAY_SIZE(missing_base_types); i++) {
920 if (strcmp(name, missing_base_types[i][0]) == 0) {
921 btf_dump_printf(d, "typedef %s %s;\n\n",
922 missing_base_types[i][1], name);
928 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
929 const struct btf_type *t)
931 btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id));
934 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
935 const struct btf_type *t,
938 const struct btf_enum *v = btf_enum(t);
939 __u16 vlen = btf_vlen(t);
944 btf_dump_printf(d, "enum%s%s",
945 t->name_off ? " " : "",
946 btf_dump_type_name(d, id));
949 btf_dump_printf(d, " {");
950 for (i = 0; i < vlen; i++, v++) {
951 name = btf_name_of(d, v->name_off);
952 /* enumerators share namespace with typedef idents */
953 dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
955 btf_dump_printf(d, "\n%s%s___%zu = %u,",
956 pfx(lvl + 1), name, dup_cnt,
959 btf_dump_printf(d, "\n%s%s = %u,",
964 btf_dump_printf(d, "\n%s}", pfx(lvl));
968 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
969 const struct btf_type *t)
971 const char *name = btf_dump_type_name(d, id);
974 btf_dump_printf(d, "union %s", name);
976 btf_dump_printf(d, "struct %s", name);
979 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
980 const struct btf_type *t, int lvl)
982 const char *name = btf_dump_ident_name(d, id);
985 * Old GCC versions are emitting invalid typedef for __gnuc_va_list
986 * pointing to VOID. This generates warnings from btf_dump() and
987 * results in uncompilable header file, so we are fixing it up here
988 * with valid typedef into __builtin_va_list.
990 if (t->type == 0 && strcmp(name, "__gnuc_va_list") == 0) {
991 btf_dump_printf(d, "typedef __builtin_va_list __gnuc_va_list");
995 btf_dump_printf(d, "typedef ");
996 btf_dump_emit_type_decl(d, t->type, name, lvl);
999 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
1004 if (d->decl_stack_cnt >= d->decl_stack_cap) {
1005 new_cap = max(16, d->decl_stack_cap * 3 / 2);
1006 new_stack = realloc(d->decl_stack,
1007 new_cap * sizeof(new_stack[0]));
1010 d->decl_stack = new_stack;
1011 d->decl_stack_cap = new_cap;
1014 d->decl_stack[d->decl_stack_cnt++] = id;
1020 * Emit type declaration (e.g., field type declaration in a struct or argument
1021 * declaration in function prototype) in correct C syntax.
1023 * For most types it's trivial, but there are few quirky type declaration
1024 * cases worth mentioning:
1025 * - function prototypes (especially nesting of function prototypes);
1027 * - const/volatile/restrict for pointers vs other types.
1029 * For a good discussion of *PARSING* C syntax (as a human), see
1030 * Peter van der Linden's "Expert C Programming: Deep C Secrets",
1031 * Ch.3 "Unscrambling Declarations in C".
1033 * It won't help with BTF to C conversion much, though, as it's an opposite
1034 * problem. So we came up with this algorithm in reverse to van der Linden's
1035 * parsing algorithm. It goes from structured BTF representation of type
1036 * declaration to a valid compilable C syntax.
1038 * For instance, consider this C typedef:
1039 * typedef const int * const * arr[10] arr_t;
1040 * It will be represented in BTF with this chain of BTF types:
1041 * [typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int]
1043 * Notice how [const] modifier always goes before type it modifies in BTF type
1044 * graph, but in C syntax, const/volatile/restrict modifiers are written to
1045 * the right of pointers, but to the left of other types. There are also other
1046 * quirks, like function pointers, arrays of them, functions returning other
1049 * We handle that by pushing all the types to a stack, until we hit "terminal"
1050 * type (int/enum/struct/union/fwd). Then depending on the kind of a type on
1051 * top of a stack, modifiers are handled differently. Array/function pointers
1052 * have also wildly different syntax and how nesting of them are done. See
1053 * code for authoritative definition.
1055 * To avoid allocating new stack for each independent chain of BTF types, we
1056 * share one bigger stack, with each chain working only on its own local view
1057 * of a stack frame. Some care is required to "pop" stack frames after
1058 * processing type declaration chain.
1060 int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
1061 const struct btf_dump_emit_type_decl_opts *opts)
1066 if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
1069 fname = OPTS_GET(opts, field_name, "");
1070 lvl = OPTS_GET(opts, indent_level, 0);
1071 d->strip_mods = OPTS_GET(opts, strip_mods, false);
1072 btf_dump_emit_type_decl(d, id, fname, lvl);
1073 d->strip_mods = false;
1077 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
1078 const char *fname, int lvl)
1080 struct id_stack decl_stack;
1081 const struct btf_type *t;
1082 int err, stack_start;
1084 stack_start = d->decl_stack_cnt;
1086 t = btf__type_by_id(d->btf, id);
1087 if (d->strip_mods && btf_is_mod(t))
1090 err = btf_dump_push_decl_stack_id(d, id);
1093 * if we don't have enough memory for entire type decl
1094 * chain, restore stack, emit warning, and try to
1095 * proceed nevertheless
1097 pr_warn("not enough memory for decl stack:%d", err);
1098 d->decl_stack_cnt = stack_start;
1106 switch (btf_kind(t)) {
1108 case BTF_KIND_VOLATILE:
1109 case BTF_KIND_CONST:
1110 case BTF_KIND_RESTRICT:
1111 case BTF_KIND_FUNC_PROTO:
1114 case BTF_KIND_ARRAY:
1115 id = btf_array(t)->type;
1120 case BTF_KIND_STRUCT:
1121 case BTF_KIND_UNION:
1122 case BTF_KIND_TYPEDEF:
1125 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1132 * We might be inside a chain of declarations (e.g., array of function
1133 * pointers returning anonymous (so inlined) structs, having another
1134 * array field). Each of those needs its own "stack frame" to handle
1135 * emitting of declarations. Those stack frames are non-overlapping
1136 * portions of shared btf_dump->decl_stack. To make it a bit nicer to
1137 * handle this set of nested stacks, we create a view corresponding to
1138 * our own "stack frame" and work with it as an independent stack.
1139 * We'll need to clean up after emit_type_chain() returns, though.
1141 decl_stack.ids = d->decl_stack + stack_start;
1142 decl_stack.cnt = d->decl_stack_cnt - stack_start;
1143 btf_dump_emit_type_chain(d, &decl_stack, fname, lvl);
1145 * emit_type_chain() guarantees that it will pop its entire decl_stack
1146 * frame before returning. But it works with a read-only view into
1147 * decl_stack, so it doesn't actually pop anything from the
1148 * perspective of shared btf_dump->decl_stack, per se. We need to
1149 * reset decl_stack state to how it was before us to avoid it growing
1152 d->decl_stack_cnt = stack_start;
1155 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
1157 const struct btf_type *t;
1160 while (decl_stack->cnt) {
1161 id = decl_stack->ids[decl_stack->cnt - 1];
1162 t = btf__type_by_id(d->btf, id);
1164 switch (btf_kind(t)) {
1165 case BTF_KIND_VOLATILE:
1166 btf_dump_printf(d, "volatile ");
1168 case BTF_KIND_CONST:
1169 btf_dump_printf(d, "const ");
1171 case BTF_KIND_RESTRICT:
1172 btf_dump_printf(d, "restrict ");
1181 static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
1183 const struct btf_type *t;
1186 while (decl_stack->cnt) {
1187 id = decl_stack->ids[decl_stack->cnt - 1];
1188 t = btf__type_by_id(d->btf, id);
1195 static void btf_dump_emit_name(const struct btf_dump *d,
1196 const char *name, bool last_was_ptr)
1198 bool separate = name[0] && !last_was_ptr;
1200 btf_dump_printf(d, "%s%s", separate ? " " : "", name);
1203 static void btf_dump_emit_type_chain(struct btf_dump *d,
1204 struct id_stack *decls,
1205 const char *fname, int lvl)
1208 * last_was_ptr is used to determine if we need to separate pointer
1209 * asterisk (*) from previous part of type signature with space, so
1210 * that we get `int ***`, instead of `int * * *`. We default to true
1211 * for cases where we have single pointer in a chain. E.g., in ptr ->
1212 * func_proto case. func_proto will start a new emit_type_chain call
1213 * with just ptr, which should be emitted as (*) or (*<fname>), so we
1214 * don't want to prepend space for that last pointer.
1216 bool last_was_ptr = true;
1217 const struct btf_type *t;
1222 while (decls->cnt) {
1223 id = decls->ids[--decls->cnt];
1225 /* VOID is a special snowflake */
1226 btf_dump_emit_mods(d, decls);
1227 btf_dump_printf(d, "void");
1228 last_was_ptr = false;
1232 t = btf__type_by_id(d->btf, id);
1237 btf_dump_emit_mods(d, decls);
1238 name = btf_name_of(d, t->name_off);
1239 btf_dump_printf(d, "%s", name);
1241 case BTF_KIND_STRUCT:
1242 case BTF_KIND_UNION:
1243 btf_dump_emit_mods(d, decls);
1244 /* inline anonymous struct/union */
1245 if (t->name_off == 0)
1246 btf_dump_emit_struct_def(d, id, t, lvl);
1248 btf_dump_emit_struct_fwd(d, id, t);
1251 btf_dump_emit_mods(d, decls);
1252 /* inline anonymous enum */
1253 if (t->name_off == 0)
1254 btf_dump_emit_enum_def(d, id, t, lvl);
1256 btf_dump_emit_enum_fwd(d, id, t);
1259 btf_dump_emit_mods(d, decls);
1260 btf_dump_emit_fwd_def(d, id, t);
1262 case BTF_KIND_TYPEDEF:
1263 btf_dump_emit_mods(d, decls);
1264 btf_dump_printf(d, "%s", btf_dump_ident_name(d, id));
1267 btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *");
1269 case BTF_KIND_VOLATILE:
1270 btf_dump_printf(d, " volatile");
1272 case BTF_KIND_CONST:
1273 btf_dump_printf(d, " const");
1275 case BTF_KIND_RESTRICT:
1276 btf_dump_printf(d, " restrict");
1278 case BTF_KIND_ARRAY: {
1279 const struct btf_array *a = btf_array(t);
1280 const struct btf_type *next_t;
1285 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354)
1286 * which causes it to emit extra const/volatile
1287 * modifiers for an array, if array's element type has
1288 * const/volatile modifiers. Clang doesn't do that.
1289 * In general, it doesn't seem very meaningful to have
1290 * a const/volatile modifier for array, so we are
1291 * going to silently skip them here.
1293 btf_dump_drop_mods(d, decls);
1295 if (decls->cnt == 0) {
1296 btf_dump_emit_name(d, fname, last_was_ptr);
1297 btf_dump_printf(d, "[%u]", a->nelems);
1301 next_id = decls->ids[decls->cnt - 1];
1302 next_t = btf__type_by_id(d->btf, next_id);
1303 multidim = btf_is_array(next_t);
1304 /* we need space if we have named non-pointer */
1305 if (fname[0] && !last_was_ptr)
1306 btf_dump_printf(d, " ");
1307 /* no parentheses for multi-dimensional array */
1309 btf_dump_printf(d, "(");
1310 btf_dump_emit_type_chain(d, decls, fname, lvl);
1312 btf_dump_printf(d, ")");
1313 btf_dump_printf(d, "[%u]", a->nelems);
1316 case BTF_KIND_FUNC_PROTO: {
1317 const struct btf_param *p = btf_params(t);
1318 __u16 vlen = btf_vlen(t);
1322 * GCC emits extra volatile qualifier for
1323 * __attribute__((noreturn)) function pointers. Clang
1324 * doesn't do it. It's a GCC quirk for backwards
1325 * compatibility with code written for GCC <2.5. So,
1326 * similarly to extra qualifiers for array, just drop
1327 * them, instead of handling them.
1329 btf_dump_drop_mods(d, decls);
1331 btf_dump_printf(d, " (");
1332 btf_dump_emit_type_chain(d, decls, fname, lvl);
1333 btf_dump_printf(d, ")");
1335 btf_dump_emit_name(d, fname, last_was_ptr);
1337 btf_dump_printf(d, "(");
1339 * Clang for BPF target generates func_proto with no
1340 * args as a func_proto with a single void arg (e.g.,
1341 * `int (*f)(void)` vs just `int (*f)()`). We are
1342 * going to pretend there are no args for such case.
1344 if (vlen == 1 && p->type == 0) {
1345 btf_dump_printf(d, ")");
1349 for (i = 0; i < vlen; i++, p++) {
1351 btf_dump_printf(d, ", ");
1353 /* last arg of type void is vararg */
1354 if (i == vlen - 1 && p->type == 0) {
1355 btf_dump_printf(d, "...");
1359 name = btf_name_of(d, p->name_off);
1360 btf_dump_emit_type_decl(d, p->type, name, lvl);
1363 btf_dump_printf(d, ")");
1367 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1372 last_was_ptr = kind == BTF_KIND_PTR;
1375 btf_dump_emit_name(d, fname, last_was_ptr);
1378 /* return number of duplicates (occurrences) of a given name */
1379 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
1380 const char *orig_name)
1384 hashmap__find(name_map, orig_name, (void **)&dup_cnt);
1386 hashmap__set(name_map, orig_name, (void *)dup_cnt, NULL, NULL);
1391 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
1392 struct hashmap *name_map)
1394 struct btf_dump_type_aux_state *s = &d->type_states[id];
1395 const struct btf_type *t = btf__type_by_id(d->btf, id);
1396 const char *orig_name = btf_name_of(d, t->name_off);
1397 const char **cached_name = &d->cached_names[id];
1400 if (t->name_off == 0)
1403 if (s->name_resolved)
1404 return *cached_name ? *cached_name : orig_name;
1406 dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
1408 const size_t max_len = 256;
1409 char new_name[max_len];
1411 snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
1412 *cached_name = strdup(new_name);
1415 s->name_resolved = 1;
1416 return *cached_name ? *cached_name : orig_name;
1419 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id)
1421 return btf_dump_resolve_name(d, id, d->type_names);
1424 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id)
1426 return btf_dump_resolve_name(d, id, d->ident_names);