xref: /openbmc/linux/tools/lib/bpf/btf_dump.c (revision cefd754d)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 
3 /*
4  * BTF-to-C type converter.
5  *
6  * Copyright (c) 2019 Facebook
7  */
8 
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <linux/err.h>
15 #include <linux/btf.h>
16 #include <linux/kernel.h>
17 #include "btf.h"
18 #include "hashmap.h"
19 #include "libbpf.h"
20 #include "libbpf_internal.h"
21 
22 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
23 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
24 
25 static const char *pfx(int lvl)
26 {
27 	return lvl >= PREFIX_CNT ? PREFIXES : &PREFIXES[PREFIX_CNT - lvl];
28 }
29 
30 enum btf_dump_type_order_state {
31 	NOT_ORDERED,
32 	ORDERING,
33 	ORDERED,
34 };
35 
36 enum btf_dump_type_emit_state {
37 	NOT_EMITTED,
38 	EMITTING,
39 	EMITTED,
40 };
41 
42 /* per-type auxiliary state */
43 struct btf_dump_type_aux_state {
44 	/* topological sorting state */
45 	enum btf_dump_type_order_state order_state: 2;
46 	/* emitting state used to determine the need for forward declaration */
47 	enum btf_dump_type_emit_state emit_state: 2;
48 	/* whether forward declaration was already emitted */
49 	__u8 fwd_emitted: 1;
50 	/* whether unique non-duplicate name was already assigned */
51 	__u8 name_resolved: 1;
52 	/* whether type is referenced from any other type */
53 	__u8 referenced: 1;
54 };
55 
56 struct btf_dump {
57 	const struct btf *btf;
58 	const struct btf_ext *btf_ext;
59 	btf_dump_printf_fn_t printf_fn;
60 	struct btf_dump_opts opts;
61 	int ptr_sz;
62 	bool strip_mods;
63 	int last_id;
64 
65 	/* per-type auxiliary state */
66 	struct btf_dump_type_aux_state *type_states;
67 	size_t type_states_cap;
68 	/* per-type optional cached unique name, must be freed, if present */
69 	const char **cached_names;
70 	size_t cached_names_cap;
71 
72 	/* topo-sorted list of dependent type definitions */
73 	__u32 *emit_queue;
74 	int emit_queue_cap;
75 	int emit_queue_cnt;
76 
77 	/*
78 	 * stack of type declarations (e.g., chain of modifiers, arrays,
79 	 * funcs, etc)
80 	 */
81 	__u32 *decl_stack;
82 	int decl_stack_cap;
83 	int decl_stack_cnt;
84 
85 	/* maps struct/union/enum name to a number of name occurrences */
86 	struct hashmap *type_names;
87 	/*
88 	 * maps typedef identifiers and enum value names to a number of such
89 	 * name occurrences
90 	 */
91 	struct hashmap *ident_names;
92 };
93 
94 static size_t str_hash_fn(const void *key, void *ctx)
95 {
96 	return str_hash(key);
97 }
98 
99 static bool str_equal_fn(const void *a, const void *b, void *ctx)
100 {
101 	return strcmp(a, b) == 0;
102 }
103 
104 static const char *btf_name_of(const struct btf_dump *d, __u32 name_off)
105 {
106 	return btf__name_by_offset(d->btf, name_off);
107 }
108 
109 static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)
110 {
111 	va_list args;
112 
113 	va_start(args, fmt);
114 	d->printf_fn(d->opts.ctx, fmt, args);
115 	va_end(args);
116 }
117 
118 static int btf_dump_mark_referenced(struct btf_dump *d);
119 static int btf_dump_resize(struct btf_dump *d);
120 
121 struct btf_dump *btf_dump__new(const struct btf *btf,
122 			       const struct btf_ext *btf_ext,
123 			       const struct btf_dump_opts *opts,
124 			       btf_dump_printf_fn_t printf_fn)
125 {
126 	struct btf_dump *d;
127 	int err;
128 
129 	d = calloc(1, sizeof(struct btf_dump));
130 	if (!d)
131 		return ERR_PTR(-ENOMEM);
132 
133 	d->btf = btf;
134 	d->btf_ext = btf_ext;
135 	d->printf_fn = printf_fn;
136 	d->opts.ctx = opts ? opts->ctx : NULL;
137 	d->ptr_sz = btf__pointer_size(btf) ? : sizeof(void *);
138 
139 	d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
140 	if (IS_ERR(d->type_names)) {
141 		err = PTR_ERR(d->type_names);
142 		d->type_names = NULL;
143 		goto err;
144 	}
145 	d->ident_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
146 	if (IS_ERR(d->ident_names)) {
147 		err = PTR_ERR(d->ident_names);
148 		d->ident_names = NULL;
149 		goto err;
150 	}
151 
152 	err = btf_dump_resize(d);
153 	if (err)
154 		goto err;
155 
156 	return d;
157 err:
158 	btf_dump__free(d);
159 	return ERR_PTR(err);
160 }
161 
162 static int btf_dump_resize(struct btf_dump *d)
163 {
164 	int err, last_id = btf__get_nr_types(d->btf);
165 
166 	if (last_id <= d->last_id)
167 		return 0;
168 
169 	if (btf_ensure_mem((void **)&d->type_states, &d->type_states_cap,
170 			   sizeof(*d->type_states), last_id + 1))
171 		return -ENOMEM;
172 	if (btf_ensure_mem((void **)&d->cached_names, &d->cached_names_cap,
173 			   sizeof(*d->cached_names), last_id + 1))
174 		return -ENOMEM;
175 
176 	if (d->last_id == 0) {
177 		/* VOID is special */
178 		d->type_states[0].order_state = ORDERED;
179 		d->type_states[0].emit_state = EMITTED;
180 	}
181 
182 	/* eagerly determine referenced types for anon enums */
183 	err = btf_dump_mark_referenced(d);
184 	if (err)
185 		return err;
186 
187 	d->last_id = last_id;
188 	return 0;
189 }
190 
191 void btf_dump__free(struct btf_dump *d)
192 {
193 	int i;
194 
195 	if (IS_ERR_OR_NULL(d))
196 		return;
197 
198 	free(d->type_states);
199 	if (d->cached_names) {
200 		/* any set cached name is owned by us and should be freed */
201 		for (i = 0; i <= d->last_id; i++) {
202 			if (d->cached_names[i])
203 				free((void *)d->cached_names[i]);
204 		}
205 	}
206 	free(d->cached_names);
207 	free(d->emit_queue);
208 	free(d->decl_stack);
209 	hashmap__free(d->type_names);
210 	hashmap__free(d->ident_names);
211 
212 	free(d);
213 }
214 
215 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr);
216 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id);
217 
218 /*
219  * Dump BTF type in a compilable C syntax, including all the necessary
220  * dependent types, necessary for compilation. If some of the dependent types
221  * were already emitted as part of previous btf_dump__dump_type() invocation
222  * for another type, they won't be emitted again. This API allows callers to
223  * filter out BTF types according to user-defined criterias and emitted only
224  * minimal subset of types, necessary to compile everything. Full struct/union
225  * definitions will still be emitted, even if the only usage is through
226  * pointer and could be satisfied with just a forward declaration.
227  *
228  * Dumping is done in two high-level passes:
229  *   1. Topologically sort type definitions to satisfy C rules of compilation.
230  *   2. Emit type definitions in C syntax.
231  *
232  * Returns 0 on success; <0, otherwise.
233  */
234 int btf_dump__dump_type(struct btf_dump *d, __u32 id)
235 {
236 	int err, i;
237 
238 	if (id > btf__get_nr_types(d->btf))
239 		return -EINVAL;
240 
241 	err = btf_dump_resize(d);
242 	if (err)
243 		return err;
244 
245 	d->emit_queue_cnt = 0;
246 	err = btf_dump_order_type(d, id, false);
247 	if (err < 0)
248 		return err;
249 
250 	for (i = 0; i < d->emit_queue_cnt; i++)
251 		btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
252 
253 	return 0;
254 }
255 
256 /*
257  * Mark all types that are referenced from any other type. This is used to
258  * determine top-level anonymous enums that need to be emitted as an
259  * independent type declarations.
260  * Anonymous enums come in two flavors: either embedded in a struct's field
261  * definition, in which case they have to be declared inline as part of field
262  * type declaration; or as a top-level anonymous enum, typically used for
263  * declaring global constants. It's impossible to distinguish between two
264  * without knowning whether given enum type was referenced from other type:
265  * top-level anonymous enum won't be referenced by anything, while embedded
266  * one will.
267  */
268 static int btf_dump_mark_referenced(struct btf_dump *d)
269 {
270 	int i, j, n = btf__get_nr_types(d->btf);
271 	const struct btf_type *t;
272 	__u16 vlen;
273 
274 	for (i = d->last_id + 1; i <= n; i++) {
275 		t = btf__type_by_id(d->btf, i);
276 		vlen = btf_vlen(t);
277 
278 		switch (btf_kind(t)) {
279 		case BTF_KIND_INT:
280 		case BTF_KIND_ENUM:
281 		case BTF_KIND_FWD:
282 			break;
283 
284 		case BTF_KIND_VOLATILE:
285 		case BTF_KIND_CONST:
286 		case BTF_KIND_RESTRICT:
287 		case BTF_KIND_PTR:
288 		case BTF_KIND_TYPEDEF:
289 		case BTF_KIND_FUNC:
290 		case BTF_KIND_VAR:
291 			d->type_states[t->type].referenced = 1;
292 			break;
293 
294 		case BTF_KIND_ARRAY: {
295 			const struct btf_array *a = btf_array(t);
296 
297 			d->type_states[a->index_type].referenced = 1;
298 			d->type_states[a->type].referenced = 1;
299 			break;
300 		}
301 		case BTF_KIND_STRUCT:
302 		case BTF_KIND_UNION: {
303 			const struct btf_member *m = btf_members(t);
304 
305 			for (j = 0; j < vlen; j++, m++)
306 				d->type_states[m->type].referenced = 1;
307 			break;
308 		}
309 		case BTF_KIND_FUNC_PROTO: {
310 			const struct btf_param *p = btf_params(t);
311 
312 			for (j = 0; j < vlen; j++, p++)
313 				d->type_states[p->type].referenced = 1;
314 			break;
315 		}
316 		case BTF_KIND_DATASEC: {
317 			const struct btf_var_secinfo *v = btf_var_secinfos(t);
318 
319 			for (j = 0; j < vlen; j++, v++)
320 				d->type_states[v->type].referenced = 1;
321 			break;
322 		}
323 		default:
324 			return -EINVAL;
325 		}
326 	}
327 	return 0;
328 }
329 
330 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
331 {
332 	__u32 *new_queue;
333 	size_t new_cap;
334 
335 	if (d->emit_queue_cnt >= d->emit_queue_cap) {
336 		new_cap = max(16, d->emit_queue_cap * 3 / 2);
337 		new_queue = libbpf_reallocarray(d->emit_queue, new_cap, sizeof(new_queue[0]));
338 		if (!new_queue)
339 			return -ENOMEM;
340 		d->emit_queue = new_queue;
341 		d->emit_queue_cap = new_cap;
342 	}
343 
344 	d->emit_queue[d->emit_queue_cnt++] = id;
345 	return 0;
346 }
347 
348 /*
349  * Determine order of emitting dependent types and specified type to satisfy
350  * C compilation rules.  This is done through topological sorting with an
351  * additional complication which comes from C rules. The main idea for C is
352  * that if some type is "embedded" into a struct/union, it's size needs to be
353  * known at the time of definition of containing type. E.g., for:
354  *
355  *	struct A {};
356  *	struct B { struct A x; }
357  *
358  * struct A *HAS* to be defined before struct B, because it's "embedded",
359  * i.e., it is part of struct B layout. But in the following case:
360  *
361  *	struct A;
362  *	struct B { struct A *x; }
363  *	struct A {};
364  *
365  * it's enough to just have a forward declaration of struct A at the time of
366  * struct B definition, as struct B has a pointer to struct A, so the size of
367  * field x is known without knowing struct A size: it's sizeof(void *).
368  *
369  * Unfortunately, there are some trickier cases we need to handle, e.g.:
370  *
371  *	struct A {}; // if this was forward-declaration: compilation error
372  *	struct B {
373  *		struct { // anonymous struct
374  *			struct A y;
375  *		} *x;
376  *	};
377  *
378  * In this case, struct B's field x is a pointer, so it's size is known
379  * regardless of the size of (anonymous) struct it points to. But because this
380  * struct is anonymous and thus defined inline inside struct B, *and* it
381  * embeds struct A, compiler requires full definition of struct A to be known
382  * before struct B can be defined. This creates a transitive dependency
383  * between struct A and struct B. If struct A was forward-declared before
384  * struct B definition and fully defined after struct B definition, that would
385  * trigger compilation error.
386  *
387  * All this means that while we are doing topological sorting on BTF type
388  * graph, we need to determine relationships between different types (graph
389  * nodes):
390  *   - weak link (relationship) between X and Y, if Y *CAN* be
391  *   forward-declared at the point of X definition;
392  *   - strong link, if Y *HAS* to be fully-defined before X can be defined.
393  *
394  * The rule is as follows. Given a chain of BTF types from X to Y, if there is
395  * BTF_KIND_PTR type in the chain and at least one non-anonymous type
396  * Z (excluding X, including Y), then link is weak. Otherwise, it's strong.
397  * Weak/strong relationship is determined recursively during DFS traversal and
398  * is returned as a result from btf_dump_order_type().
399  *
400  * btf_dump_order_type() is trying to avoid unnecessary forward declarations,
401  * but it is not guaranteeing that no extraneous forward declarations will be
402  * emitted.
403  *
404  * To avoid extra work, algorithm marks some of BTF types as ORDERED, when
405  * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT,
406  * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the
407  * entire graph path, so depending where from one came to that BTF type, it
408  * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM,
409  * once they are processed, there is no need to do it again, so they are
410  * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces
411  * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But
412  * in any case, once those are processed, no need to do it again, as the
413  * result won't change.
414  *
415  * Returns:
416  *   - 1, if type is part of strong link (so there is strong topological
417  *   ordering requirements);
418  *   - 0, if type is part of weak link (so can be satisfied through forward
419  *   declaration);
420  *   - <0, on error (e.g., unsatisfiable type loop detected).
421  */
422 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
423 {
424 	/*
425 	 * Order state is used to detect strong link cycles, but only for BTF
426 	 * kinds that are or could be an independent definition (i.e.,
427 	 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays,
428 	 * func_protos, modifiers are just means to get to these definitions.
429 	 * Int/void don't need definitions, they are assumed to be always
430 	 * properly defined.  We also ignore datasec, var, and funcs for now.
431 	 * So for all non-defining kinds, we never even set ordering state,
432 	 * for defining kinds we set ORDERING and subsequently ORDERED if it
433 	 * forms a strong link.
434 	 */
435 	struct btf_dump_type_aux_state *tstate = &d->type_states[id];
436 	const struct btf_type *t;
437 	__u16 vlen;
438 	int err, i;
439 
440 	/* return true, letting typedefs know that it's ok to be emitted */
441 	if (tstate->order_state == ORDERED)
442 		return 1;
443 
444 	t = btf__type_by_id(d->btf, id);
445 
446 	if (tstate->order_state == ORDERING) {
447 		/* type loop, but resolvable through fwd declaration */
448 		if (btf_is_composite(t) && through_ptr && t->name_off != 0)
449 			return 0;
450 		pr_warn("unsatisfiable type cycle, id:[%u]\n", id);
451 		return -ELOOP;
452 	}
453 
454 	switch (btf_kind(t)) {
455 	case BTF_KIND_INT:
456 		tstate->order_state = ORDERED;
457 		return 0;
458 
459 	case BTF_KIND_PTR:
460 		err = btf_dump_order_type(d, t->type, true);
461 		tstate->order_state = ORDERED;
462 		return err;
463 
464 	case BTF_KIND_ARRAY:
465 		return btf_dump_order_type(d, btf_array(t)->type, through_ptr);
466 
467 	case BTF_KIND_STRUCT:
468 	case BTF_KIND_UNION: {
469 		const struct btf_member *m = btf_members(t);
470 		/*
471 		 * struct/union is part of strong link, only if it's embedded
472 		 * (so no ptr in a path) or it's anonymous (so has to be
473 		 * defined inline, even if declared through ptr)
474 		 */
475 		if (through_ptr && t->name_off != 0)
476 			return 0;
477 
478 		tstate->order_state = ORDERING;
479 
480 		vlen = btf_vlen(t);
481 		for (i = 0; i < vlen; i++, m++) {
482 			err = btf_dump_order_type(d, m->type, false);
483 			if (err < 0)
484 				return err;
485 		}
486 
487 		if (t->name_off != 0) {
488 			err = btf_dump_add_emit_queue_id(d, id);
489 			if (err < 0)
490 				return err;
491 		}
492 
493 		tstate->order_state = ORDERED;
494 		return 1;
495 	}
496 	case BTF_KIND_ENUM:
497 	case BTF_KIND_FWD:
498 		/*
499 		 * non-anonymous or non-referenced enums are top-level
500 		 * declarations and should be emitted. Same logic can be
501 		 * applied to FWDs, it won't hurt anyways.
502 		 */
503 		if (t->name_off != 0 || !tstate->referenced) {
504 			err = btf_dump_add_emit_queue_id(d, id);
505 			if (err)
506 				return err;
507 		}
508 		tstate->order_state = ORDERED;
509 		return 1;
510 
511 	case BTF_KIND_TYPEDEF: {
512 		int is_strong;
513 
514 		is_strong = btf_dump_order_type(d, t->type, through_ptr);
515 		if (is_strong < 0)
516 			return is_strong;
517 
518 		/* typedef is similar to struct/union w.r.t. fwd-decls */
519 		if (through_ptr && !is_strong)
520 			return 0;
521 
522 		/* typedef is always a named definition */
523 		err = btf_dump_add_emit_queue_id(d, id);
524 		if (err)
525 			return err;
526 
527 		d->type_states[id].order_state = ORDERED;
528 		return 1;
529 	}
530 	case BTF_KIND_VOLATILE:
531 	case BTF_KIND_CONST:
532 	case BTF_KIND_RESTRICT:
533 		return btf_dump_order_type(d, t->type, through_ptr);
534 
535 	case BTF_KIND_FUNC_PROTO: {
536 		const struct btf_param *p = btf_params(t);
537 		bool is_strong;
538 
539 		err = btf_dump_order_type(d, t->type, through_ptr);
540 		if (err < 0)
541 			return err;
542 		is_strong = err > 0;
543 
544 		vlen = btf_vlen(t);
545 		for (i = 0; i < vlen; i++, p++) {
546 			err = btf_dump_order_type(d, p->type, through_ptr);
547 			if (err < 0)
548 				return err;
549 			if (err > 0)
550 				is_strong = true;
551 		}
552 		return is_strong;
553 	}
554 	case BTF_KIND_FUNC:
555 	case BTF_KIND_VAR:
556 	case BTF_KIND_DATASEC:
557 		d->type_states[id].order_state = ORDERED;
558 		return 0;
559 
560 	default:
561 		return -EINVAL;
562 	}
563 }
564 
565 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
566 					  const struct btf_type *t);
567 
568 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
569 				     const struct btf_type *t);
570 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id,
571 				     const struct btf_type *t, int lvl);
572 
573 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
574 				   const struct btf_type *t);
575 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
576 				   const struct btf_type *t, int lvl);
577 
578 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
579 				  const struct btf_type *t);
580 
581 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
582 				      const struct btf_type *t, int lvl);
583 
584 /* a local view into a shared stack */
585 struct id_stack {
586 	const __u32 *ids;
587 	int cnt;
588 };
589 
590 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
591 				    const char *fname, int lvl);
592 static void btf_dump_emit_type_chain(struct btf_dump *d,
593 				     struct id_stack *decl_stack,
594 				     const char *fname, int lvl);
595 
596 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
597 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
598 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
599 				 const char *orig_name);
600 
601 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
602 {
603 	const struct btf_type *t = btf__type_by_id(d->btf, id);
604 
605 	/* __builtin_va_list is a compiler built-in, which causes compilation
606 	 * errors, when compiling w/ different compiler, then used to compile
607 	 * original code (e.g., GCC to compile kernel, Clang to use generated
608 	 * C header from BTF). As it is built-in, it should be already defined
609 	 * properly internally in compiler.
610 	 */
611 	if (t->name_off == 0)
612 		return false;
613 	return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0;
614 }
615 
616 /*
617  * Emit C-syntax definitions of types from chains of BTF types.
618  *
619  * High-level handling of determining necessary forward declarations are handled
620  * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type
621  * declarations/definitions in C syntax  are handled by a combo of
622  * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to
623  * corresponding btf_dump_emit_*_{def,fwd}() functions.
624  *
625  * We also keep track of "containing struct/union type ID" to determine when
626  * we reference it from inside and thus can avoid emitting unnecessary forward
627  * declaration.
628  *
629  * This algorithm is designed in such a way, that even if some error occurs
630  * (either technical, e.g., out of memory, or logical, i.e., malformed BTF
631  * that doesn't comply to C rules completely), algorithm will try to proceed
632  * and produce as much meaningful output as possible.
633  */
634 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
635 {
636 	struct btf_dump_type_aux_state *tstate = &d->type_states[id];
637 	bool top_level_def = cont_id == 0;
638 	const struct btf_type *t;
639 	__u16 kind;
640 
641 	if (tstate->emit_state == EMITTED)
642 		return;
643 
644 	t = btf__type_by_id(d->btf, id);
645 	kind = btf_kind(t);
646 
647 	if (tstate->emit_state == EMITTING) {
648 		if (tstate->fwd_emitted)
649 			return;
650 
651 		switch (kind) {
652 		case BTF_KIND_STRUCT:
653 		case BTF_KIND_UNION:
654 			/*
655 			 * if we are referencing a struct/union that we are
656 			 * part of - then no need for fwd declaration
657 			 */
658 			if (id == cont_id)
659 				return;
660 			if (t->name_off == 0) {
661 				pr_warn("anonymous struct/union loop, id:[%u]\n",
662 					id);
663 				return;
664 			}
665 			btf_dump_emit_struct_fwd(d, id, t);
666 			btf_dump_printf(d, ";\n\n");
667 			tstate->fwd_emitted = 1;
668 			break;
669 		case BTF_KIND_TYPEDEF:
670 			/*
671 			 * for typedef fwd_emitted means typedef definition
672 			 * was emitted, but it can be used only for "weak"
673 			 * references through pointer only, not for embedding
674 			 */
675 			if (!btf_dump_is_blacklisted(d, id)) {
676 				btf_dump_emit_typedef_def(d, id, t, 0);
677 				btf_dump_printf(d, ";\n\n");
678 			}
679 			tstate->fwd_emitted = 1;
680 			break;
681 		default:
682 			break;
683 		}
684 
685 		return;
686 	}
687 
688 	switch (kind) {
689 	case BTF_KIND_INT:
690 		/* Emit type alias definitions if necessary */
691 		btf_dump_emit_missing_aliases(d, id, t);
692 
693 		tstate->emit_state = EMITTED;
694 		break;
695 	case BTF_KIND_ENUM:
696 		if (top_level_def) {
697 			btf_dump_emit_enum_def(d, id, t, 0);
698 			btf_dump_printf(d, ";\n\n");
699 		}
700 		tstate->emit_state = EMITTED;
701 		break;
702 	case BTF_KIND_PTR:
703 	case BTF_KIND_VOLATILE:
704 	case BTF_KIND_CONST:
705 	case BTF_KIND_RESTRICT:
706 		btf_dump_emit_type(d, t->type, cont_id);
707 		break;
708 	case BTF_KIND_ARRAY:
709 		btf_dump_emit_type(d, btf_array(t)->type, cont_id);
710 		break;
711 	case BTF_KIND_FWD:
712 		btf_dump_emit_fwd_def(d, id, t);
713 		btf_dump_printf(d, ";\n\n");
714 		tstate->emit_state = EMITTED;
715 		break;
716 	case BTF_KIND_TYPEDEF:
717 		tstate->emit_state = EMITTING;
718 		btf_dump_emit_type(d, t->type, id);
719 		/*
720 		 * typedef can server as both definition and forward
721 		 * declaration; at this stage someone depends on
722 		 * typedef as a forward declaration (refers to it
723 		 * through pointer), so unless we already did it,
724 		 * emit typedef as a forward declaration
725 		 */
726 		if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
727 			btf_dump_emit_typedef_def(d, id, t, 0);
728 			btf_dump_printf(d, ";\n\n");
729 		}
730 		tstate->emit_state = EMITTED;
731 		break;
732 	case BTF_KIND_STRUCT:
733 	case BTF_KIND_UNION:
734 		tstate->emit_state = EMITTING;
735 		/* if it's a top-level struct/union definition or struct/union
736 		 * is anonymous, then in C we'll be emitting all fields and
737 		 * their types (as opposed to just `struct X`), so we need to
738 		 * make sure that all types, referenced from struct/union
739 		 * members have necessary forward-declarations, where
740 		 * applicable
741 		 */
742 		if (top_level_def || t->name_off == 0) {
743 			const struct btf_member *m = btf_members(t);
744 			__u16 vlen = btf_vlen(t);
745 			int i, new_cont_id;
746 
747 			new_cont_id = t->name_off == 0 ? cont_id : id;
748 			for (i = 0; i < vlen; i++, m++)
749 				btf_dump_emit_type(d, m->type, new_cont_id);
750 		} else if (!tstate->fwd_emitted && id != cont_id) {
751 			btf_dump_emit_struct_fwd(d, id, t);
752 			btf_dump_printf(d, ";\n\n");
753 			tstate->fwd_emitted = 1;
754 		}
755 
756 		if (top_level_def) {
757 			btf_dump_emit_struct_def(d, id, t, 0);
758 			btf_dump_printf(d, ";\n\n");
759 			tstate->emit_state = EMITTED;
760 		} else {
761 			tstate->emit_state = NOT_EMITTED;
762 		}
763 		break;
764 	case BTF_KIND_FUNC_PROTO: {
765 		const struct btf_param *p = btf_params(t);
766 		__u16 vlen = btf_vlen(t);
767 		int i;
768 
769 		btf_dump_emit_type(d, t->type, cont_id);
770 		for (i = 0; i < vlen; i++, p++)
771 			btf_dump_emit_type(d, p->type, cont_id);
772 
773 		break;
774 	}
775 	default:
776 		break;
777 	}
778 }
779 
780 static bool btf_is_struct_packed(const struct btf *btf, __u32 id,
781 				 const struct btf_type *t)
782 {
783 	const struct btf_member *m;
784 	int align, i, bit_sz;
785 	__u16 vlen;
786 
787 	align = btf__align_of(btf, id);
788 	/* size of a non-packed struct has to be a multiple of its alignment*/
789 	if (align && t->size % align)
790 		return true;
791 
792 	m = btf_members(t);
793 	vlen = btf_vlen(t);
794 	/* all non-bitfield fields have to be naturally aligned */
795 	for (i = 0; i < vlen; i++, m++) {
796 		align = btf__align_of(btf, m->type);
797 		bit_sz = btf_member_bitfield_size(t, i);
798 		if (align && bit_sz == 0 && m->offset % (8 * align) != 0)
799 			return true;
800 	}
801 
802 	/*
803 	 * if original struct was marked as packed, but its layout is
804 	 * naturally aligned, we'll detect that it's not packed
805 	 */
806 	return false;
807 }
808 
809 static int chip_away_bits(int total, int at_most)
810 {
811 	return total % at_most ? : at_most;
812 }
813 
814 static void btf_dump_emit_bit_padding(const struct btf_dump *d,
815 				      int cur_off, int m_off, int m_bit_sz,
816 				      int align, int lvl)
817 {
818 	int off_diff = m_off - cur_off;
819 	int ptr_bits = d->ptr_sz * 8;
820 
821 	if (off_diff <= 0)
822 		/* no gap */
823 		return;
824 	if (m_bit_sz == 0 && off_diff < align * 8)
825 		/* natural padding will take care of a gap */
826 		return;
827 
828 	while (off_diff > 0) {
829 		const char *pad_type;
830 		int pad_bits;
831 
832 		if (ptr_bits > 32 && off_diff > 32) {
833 			pad_type = "long";
834 			pad_bits = chip_away_bits(off_diff, ptr_bits);
835 		} else if (off_diff > 16) {
836 			pad_type = "int";
837 			pad_bits = chip_away_bits(off_diff, 32);
838 		} else if (off_diff > 8) {
839 			pad_type = "short";
840 			pad_bits = chip_away_bits(off_diff, 16);
841 		} else {
842 			pad_type = "char";
843 			pad_bits = chip_away_bits(off_diff, 8);
844 		}
845 		btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
846 		off_diff -= pad_bits;
847 	}
848 }
849 
850 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
851 				     const struct btf_type *t)
852 {
853 	btf_dump_printf(d, "%s %s",
854 			btf_is_struct(t) ? "struct" : "union",
855 			btf_dump_type_name(d, id));
856 }
857 
858 static void btf_dump_emit_struct_def(struct btf_dump *d,
859 				     __u32 id,
860 				     const struct btf_type *t,
861 				     int lvl)
862 {
863 	const struct btf_member *m = btf_members(t);
864 	bool is_struct = btf_is_struct(t);
865 	int align, i, packed, off = 0;
866 	__u16 vlen = btf_vlen(t);
867 
868 	packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
869 
870 	btf_dump_printf(d, "%s%s%s {",
871 			is_struct ? "struct" : "union",
872 			t->name_off ? " " : "",
873 			btf_dump_type_name(d, id));
874 
875 	for (i = 0; i < vlen; i++, m++) {
876 		const char *fname;
877 		int m_off, m_sz;
878 
879 		fname = btf_name_of(d, m->name_off);
880 		m_sz = btf_member_bitfield_size(t, i);
881 		m_off = btf_member_bit_offset(t, i);
882 		align = packed ? 1 : btf__align_of(d->btf, m->type);
883 
884 		btf_dump_emit_bit_padding(d, off, m_off, m_sz, align, lvl + 1);
885 		btf_dump_printf(d, "\n%s", pfx(lvl + 1));
886 		btf_dump_emit_type_decl(d, m->type, fname, lvl + 1);
887 
888 		if (m_sz) {
889 			btf_dump_printf(d, ": %d", m_sz);
890 			off = m_off + m_sz;
891 		} else {
892 			m_sz = max((__s64)0, btf__resolve_size(d->btf, m->type));
893 			off = m_off + m_sz * 8;
894 		}
895 		btf_dump_printf(d, ";");
896 	}
897 
898 	/* pad at the end, if necessary */
899 	if (is_struct) {
900 		align = packed ? 1 : btf__align_of(d->btf, id);
901 		btf_dump_emit_bit_padding(d, off, t->size * 8, 0, align,
902 					  lvl + 1);
903 	}
904 
905 	if (vlen)
906 		btf_dump_printf(d, "\n");
907 	btf_dump_printf(d, "%s}", pfx(lvl));
908 	if (packed)
909 		btf_dump_printf(d, " __attribute__((packed))");
910 }
911 
912 static const char *missing_base_types[][2] = {
913 	/*
914 	 * GCC emits typedefs to its internal __PolyX_t types when compiling Arm
915 	 * SIMD intrinsics. Alias them to standard base types.
916 	 */
917 	{ "__Poly8_t",		"unsigned char" },
918 	{ "__Poly16_t",		"unsigned short" },
919 	{ "__Poly64_t",		"unsigned long long" },
920 	{ "__Poly128_t",	"unsigned __int128" },
921 };
922 
923 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
924 					  const struct btf_type *t)
925 {
926 	const char *name = btf_dump_type_name(d, id);
927 	int i;
928 
929 	for (i = 0; i < ARRAY_SIZE(missing_base_types); i++) {
930 		if (strcmp(name, missing_base_types[i][0]) == 0) {
931 			btf_dump_printf(d, "typedef %s %s;\n\n",
932 					missing_base_types[i][1], name);
933 			break;
934 		}
935 	}
936 }
937 
938 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
939 				   const struct btf_type *t)
940 {
941 	btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id));
942 }
943 
944 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
945 				   const struct btf_type *t,
946 				   int lvl)
947 {
948 	const struct btf_enum *v = btf_enum(t);
949 	__u16 vlen = btf_vlen(t);
950 	const char *name;
951 	size_t dup_cnt;
952 	int i;
953 
954 	btf_dump_printf(d, "enum%s%s",
955 			t->name_off ? " " : "",
956 			btf_dump_type_name(d, id));
957 
958 	if (vlen) {
959 		btf_dump_printf(d, " {");
960 		for (i = 0; i < vlen; i++, v++) {
961 			name = btf_name_of(d, v->name_off);
962 			/* enumerators share namespace with typedef idents */
963 			dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
964 			if (dup_cnt > 1) {
965 				btf_dump_printf(d, "\n%s%s___%zu = %u,",
966 						pfx(lvl + 1), name, dup_cnt,
967 						(__u32)v->val);
968 			} else {
969 				btf_dump_printf(d, "\n%s%s = %u,",
970 						pfx(lvl + 1), name,
971 						(__u32)v->val);
972 			}
973 		}
974 		btf_dump_printf(d, "\n%s}", pfx(lvl));
975 	}
976 }
977 
978 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
979 				  const struct btf_type *t)
980 {
981 	const char *name = btf_dump_type_name(d, id);
982 
983 	if (btf_kflag(t))
984 		btf_dump_printf(d, "union %s", name);
985 	else
986 		btf_dump_printf(d, "struct %s", name);
987 }
988 
989 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
990 				     const struct btf_type *t, int lvl)
991 {
992 	const char *name = btf_dump_ident_name(d, id);
993 
994 	/*
995 	 * Old GCC versions are emitting invalid typedef for __gnuc_va_list
996 	 * pointing to VOID. This generates warnings from btf_dump() and
997 	 * results in uncompilable header file, so we are fixing it up here
998 	 * with valid typedef into __builtin_va_list.
999 	 */
1000 	if (t->type == 0 && strcmp(name, "__gnuc_va_list") == 0) {
1001 		btf_dump_printf(d, "typedef __builtin_va_list __gnuc_va_list");
1002 		return;
1003 	}
1004 
1005 	btf_dump_printf(d, "typedef ");
1006 	btf_dump_emit_type_decl(d, t->type, name, lvl);
1007 }
1008 
1009 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
1010 {
1011 	__u32 *new_stack;
1012 	size_t new_cap;
1013 
1014 	if (d->decl_stack_cnt >= d->decl_stack_cap) {
1015 		new_cap = max(16, d->decl_stack_cap * 3 / 2);
1016 		new_stack = libbpf_reallocarray(d->decl_stack, new_cap, sizeof(new_stack[0]));
1017 		if (!new_stack)
1018 			return -ENOMEM;
1019 		d->decl_stack = new_stack;
1020 		d->decl_stack_cap = new_cap;
1021 	}
1022 
1023 	d->decl_stack[d->decl_stack_cnt++] = id;
1024 
1025 	return 0;
1026 }
1027 
1028 /*
1029  * Emit type declaration (e.g., field type declaration in a struct or argument
1030  * declaration in function prototype) in correct C syntax.
1031  *
1032  * For most types it's trivial, but there are few quirky type declaration
1033  * cases worth mentioning:
1034  *   - function prototypes (especially nesting of function prototypes);
1035  *   - arrays;
1036  *   - const/volatile/restrict for pointers vs other types.
1037  *
1038  * For a good discussion of *PARSING* C syntax (as a human), see
1039  * Peter van der Linden's "Expert C Programming: Deep C Secrets",
1040  * Ch.3 "Unscrambling Declarations in C".
1041  *
1042  * It won't help with BTF to C conversion much, though, as it's an opposite
1043  * problem. So we came up with this algorithm in reverse to van der Linden's
1044  * parsing algorithm. It goes from structured BTF representation of type
1045  * declaration to a valid compilable C syntax.
1046  *
1047  * For instance, consider this C typedef:
1048  *	typedef const int * const * arr[10] arr_t;
1049  * It will be represented in BTF with this chain of BTF types:
1050  *	[typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int]
1051  *
1052  * Notice how [const] modifier always goes before type it modifies in BTF type
1053  * graph, but in C syntax, const/volatile/restrict modifiers are written to
1054  * the right of pointers, but to the left of other types. There are also other
1055  * quirks, like function pointers, arrays of them, functions returning other
1056  * functions, etc.
1057  *
1058  * We handle that by pushing all the types to a stack, until we hit "terminal"
1059  * type (int/enum/struct/union/fwd). Then depending on the kind of a type on
1060  * top of a stack, modifiers are handled differently. Array/function pointers
1061  * have also wildly different syntax and how nesting of them are done. See
1062  * code for authoritative definition.
1063  *
1064  * To avoid allocating new stack for each independent chain of BTF types, we
1065  * share one bigger stack, with each chain working only on its own local view
1066  * of a stack frame. Some care is required to "pop" stack frames after
1067  * processing type declaration chain.
1068  */
1069 int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
1070 			     const struct btf_dump_emit_type_decl_opts *opts)
1071 {
1072 	const char *fname;
1073 	int lvl, err;
1074 
1075 	if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
1076 		return -EINVAL;
1077 
1078 	err = btf_dump_resize(d);
1079 	if (err)
1080 		return -EINVAL;
1081 
1082 	fname = OPTS_GET(opts, field_name, "");
1083 	lvl = OPTS_GET(opts, indent_level, 0);
1084 	d->strip_mods = OPTS_GET(opts, strip_mods, false);
1085 	btf_dump_emit_type_decl(d, id, fname, lvl);
1086 	d->strip_mods = false;
1087 	return 0;
1088 }
1089 
1090 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
1091 				    const char *fname, int lvl)
1092 {
1093 	struct id_stack decl_stack;
1094 	const struct btf_type *t;
1095 	int err, stack_start;
1096 
1097 	stack_start = d->decl_stack_cnt;
1098 	for (;;) {
1099 		t = btf__type_by_id(d->btf, id);
1100 		if (d->strip_mods && btf_is_mod(t))
1101 			goto skip_mod;
1102 
1103 		err = btf_dump_push_decl_stack_id(d, id);
1104 		if (err < 0) {
1105 			/*
1106 			 * if we don't have enough memory for entire type decl
1107 			 * chain, restore stack, emit warning, and try to
1108 			 * proceed nevertheless
1109 			 */
1110 			pr_warn("not enough memory for decl stack:%d", err);
1111 			d->decl_stack_cnt = stack_start;
1112 			return;
1113 		}
1114 skip_mod:
1115 		/* VOID */
1116 		if (id == 0)
1117 			break;
1118 
1119 		switch (btf_kind(t)) {
1120 		case BTF_KIND_PTR:
1121 		case BTF_KIND_VOLATILE:
1122 		case BTF_KIND_CONST:
1123 		case BTF_KIND_RESTRICT:
1124 		case BTF_KIND_FUNC_PROTO:
1125 			id = t->type;
1126 			break;
1127 		case BTF_KIND_ARRAY:
1128 			id = btf_array(t)->type;
1129 			break;
1130 		case BTF_KIND_INT:
1131 		case BTF_KIND_ENUM:
1132 		case BTF_KIND_FWD:
1133 		case BTF_KIND_STRUCT:
1134 		case BTF_KIND_UNION:
1135 		case BTF_KIND_TYPEDEF:
1136 			goto done;
1137 		default:
1138 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1139 				btf_kind(t), id);
1140 			goto done;
1141 		}
1142 	}
1143 done:
1144 	/*
1145 	 * We might be inside a chain of declarations (e.g., array of function
1146 	 * pointers returning anonymous (so inlined) structs, having another
1147 	 * array field). Each of those needs its own "stack frame" to handle
1148 	 * emitting of declarations. Those stack frames are non-overlapping
1149 	 * portions of shared btf_dump->decl_stack. To make it a bit nicer to
1150 	 * handle this set of nested stacks, we create a view corresponding to
1151 	 * our own "stack frame" and work with it as an independent stack.
1152 	 * We'll need to clean up after emit_type_chain() returns, though.
1153 	 */
1154 	decl_stack.ids = d->decl_stack + stack_start;
1155 	decl_stack.cnt = d->decl_stack_cnt - stack_start;
1156 	btf_dump_emit_type_chain(d, &decl_stack, fname, lvl);
1157 	/*
1158 	 * emit_type_chain() guarantees that it will pop its entire decl_stack
1159 	 * frame before returning. But it works with a read-only view into
1160 	 * decl_stack, so it doesn't actually pop anything from the
1161 	 * perspective of shared btf_dump->decl_stack, per se. We need to
1162 	 * reset decl_stack state to how it was before us to avoid it growing
1163 	 * all the time.
1164 	 */
1165 	d->decl_stack_cnt = stack_start;
1166 }
1167 
1168 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
1169 {
1170 	const struct btf_type *t;
1171 	__u32 id;
1172 
1173 	while (decl_stack->cnt) {
1174 		id = decl_stack->ids[decl_stack->cnt - 1];
1175 		t = btf__type_by_id(d->btf, id);
1176 
1177 		switch (btf_kind(t)) {
1178 		case BTF_KIND_VOLATILE:
1179 			btf_dump_printf(d, "volatile ");
1180 			break;
1181 		case BTF_KIND_CONST:
1182 			btf_dump_printf(d, "const ");
1183 			break;
1184 		case BTF_KIND_RESTRICT:
1185 			btf_dump_printf(d, "restrict ");
1186 			break;
1187 		default:
1188 			return;
1189 		}
1190 		decl_stack->cnt--;
1191 	}
1192 }
1193 
1194 static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
1195 {
1196 	const struct btf_type *t;
1197 	__u32 id;
1198 
1199 	while (decl_stack->cnt) {
1200 		id = decl_stack->ids[decl_stack->cnt - 1];
1201 		t = btf__type_by_id(d->btf, id);
1202 		if (!btf_is_mod(t))
1203 			return;
1204 		decl_stack->cnt--;
1205 	}
1206 }
1207 
1208 static void btf_dump_emit_name(const struct btf_dump *d,
1209 			       const char *name, bool last_was_ptr)
1210 {
1211 	bool separate = name[0] && !last_was_ptr;
1212 
1213 	btf_dump_printf(d, "%s%s", separate ? " " : "", name);
1214 }
1215 
1216 static void btf_dump_emit_type_chain(struct btf_dump *d,
1217 				     struct id_stack *decls,
1218 				     const char *fname, int lvl)
1219 {
1220 	/*
1221 	 * last_was_ptr is used to determine if we need to separate pointer
1222 	 * asterisk (*) from previous part of type signature with space, so
1223 	 * that we get `int ***`, instead of `int * * *`. We default to true
1224 	 * for cases where we have single pointer in a chain. E.g., in ptr ->
1225 	 * func_proto case. func_proto will start a new emit_type_chain call
1226 	 * with just ptr, which should be emitted as (*) or (*<fname>), so we
1227 	 * don't want to prepend space for that last pointer.
1228 	 */
1229 	bool last_was_ptr = true;
1230 	const struct btf_type *t;
1231 	const char *name;
1232 	__u16 kind;
1233 	__u32 id;
1234 
1235 	while (decls->cnt) {
1236 		id = decls->ids[--decls->cnt];
1237 		if (id == 0) {
1238 			/* VOID is a special snowflake */
1239 			btf_dump_emit_mods(d, decls);
1240 			btf_dump_printf(d, "void");
1241 			last_was_ptr = false;
1242 			continue;
1243 		}
1244 
1245 		t = btf__type_by_id(d->btf, id);
1246 		kind = btf_kind(t);
1247 
1248 		switch (kind) {
1249 		case BTF_KIND_INT:
1250 			btf_dump_emit_mods(d, decls);
1251 			name = btf_name_of(d, t->name_off);
1252 			btf_dump_printf(d, "%s", name);
1253 			break;
1254 		case BTF_KIND_STRUCT:
1255 		case BTF_KIND_UNION:
1256 			btf_dump_emit_mods(d, decls);
1257 			/* inline anonymous struct/union */
1258 			if (t->name_off == 0)
1259 				btf_dump_emit_struct_def(d, id, t, lvl);
1260 			else
1261 				btf_dump_emit_struct_fwd(d, id, t);
1262 			break;
1263 		case BTF_KIND_ENUM:
1264 			btf_dump_emit_mods(d, decls);
1265 			/* inline anonymous enum */
1266 			if (t->name_off == 0)
1267 				btf_dump_emit_enum_def(d, id, t, lvl);
1268 			else
1269 				btf_dump_emit_enum_fwd(d, id, t);
1270 			break;
1271 		case BTF_KIND_FWD:
1272 			btf_dump_emit_mods(d, decls);
1273 			btf_dump_emit_fwd_def(d, id, t);
1274 			break;
1275 		case BTF_KIND_TYPEDEF:
1276 			btf_dump_emit_mods(d, decls);
1277 			btf_dump_printf(d, "%s", btf_dump_ident_name(d, id));
1278 			break;
1279 		case BTF_KIND_PTR:
1280 			btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *");
1281 			break;
1282 		case BTF_KIND_VOLATILE:
1283 			btf_dump_printf(d, " volatile");
1284 			break;
1285 		case BTF_KIND_CONST:
1286 			btf_dump_printf(d, " const");
1287 			break;
1288 		case BTF_KIND_RESTRICT:
1289 			btf_dump_printf(d, " restrict");
1290 			break;
1291 		case BTF_KIND_ARRAY: {
1292 			const struct btf_array *a = btf_array(t);
1293 			const struct btf_type *next_t;
1294 			__u32 next_id;
1295 			bool multidim;
1296 			/*
1297 			 * GCC has a bug
1298 			 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354)
1299 			 * which causes it to emit extra const/volatile
1300 			 * modifiers for an array, if array's element type has
1301 			 * const/volatile modifiers. Clang doesn't do that.
1302 			 * In general, it doesn't seem very meaningful to have
1303 			 * a const/volatile modifier for array, so we are
1304 			 * going to silently skip them here.
1305 			 */
1306 			btf_dump_drop_mods(d, decls);
1307 
1308 			if (decls->cnt == 0) {
1309 				btf_dump_emit_name(d, fname, last_was_ptr);
1310 				btf_dump_printf(d, "[%u]", a->nelems);
1311 				return;
1312 			}
1313 
1314 			next_id = decls->ids[decls->cnt - 1];
1315 			next_t = btf__type_by_id(d->btf, next_id);
1316 			multidim = btf_is_array(next_t);
1317 			/* we need space if we have named non-pointer */
1318 			if (fname[0] && !last_was_ptr)
1319 				btf_dump_printf(d, " ");
1320 			/* no parentheses for multi-dimensional array */
1321 			if (!multidim)
1322 				btf_dump_printf(d, "(");
1323 			btf_dump_emit_type_chain(d, decls, fname, lvl);
1324 			if (!multidim)
1325 				btf_dump_printf(d, ")");
1326 			btf_dump_printf(d, "[%u]", a->nelems);
1327 			return;
1328 		}
1329 		case BTF_KIND_FUNC_PROTO: {
1330 			const struct btf_param *p = btf_params(t);
1331 			__u16 vlen = btf_vlen(t);
1332 			int i;
1333 
1334 			/*
1335 			 * GCC emits extra volatile qualifier for
1336 			 * __attribute__((noreturn)) function pointers. Clang
1337 			 * doesn't do it. It's a GCC quirk for backwards
1338 			 * compatibility with code written for GCC <2.5. So,
1339 			 * similarly to extra qualifiers for array, just drop
1340 			 * them, instead of handling them.
1341 			 */
1342 			btf_dump_drop_mods(d, decls);
1343 			if (decls->cnt) {
1344 				btf_dump_printf(d, " (");
1345 				btf_dump_emit_type_chain(d, decls, fname, lvl);
1346 				btf_dump_printf(d, ")");
1347 			} else {
1348 				btf_dump_emit_name(d, fname, last_was_ptr);
1349 			}
1350 			btf_dump_printf(d, "(");
1351 			/*
1352 			 * Clang for BPF target generates func_proto with no
1353 			 * args as a func_proto with a single void arg (e.g.,
1354 			 * `int (*f)(void)` vs just `int (*f)()`). We are
1355 			 * going to pretend there are no args for such case.
1356 			 */
1357 			if (vlen == 1 && p->type == 0) {
1358 				btf_dump_printf(d, ")");
1359 				return;
1360 			}
1361 
1362 			for (i = 0; i < vlen; i++, p++) {
1363 				if (i > 0)
1364 					btf_dump_printf(d, ", ");
1365 
1366 				/* last arg of type void is vararg */
1367 				if (i == vlen - 1 && p->type == 0) {
1368 					btf_dump_printf(d, "...");
1369 					break;
1370 				}
1371 
1372 				name = btf_name_of(d, p->name_off);
1373 				btf_dump_emit_type_decl(d, p->type, name, lvl);
1374 			}
1375 
1376 			btf_dump_printf(d, ")");
1377 			return;
1378 		}
1379 		default:
1380 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1381 				kind, id);
1382 			return;
1383 		}
1384 
1385 		last_was_ptr = kind == BTF_KIND_PTR;
1386 	}
1387 
1388 	btf_dump_emit_name(d, fname, last_was_ptr);
1389 }
1390 
1391 /* return number of duplicates (occurrences) of a given name */
1392 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
1393 				 const char *orig_name)
1394 {
1395 	size_t dup_cnt = 0;
1396 
1397 	hashmap__find(name_map, orig_name, (void **)&dup_cnt);
1398 	dup_cnt++;
1399 	hashmap__set(name_map, orig_name, (void *)dup_cnt, NULL, NULL);
1400 
1401 	return dup_cnt;
1402 }
1403 
1404 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
1405 					 struct hashmap *name_map)
1406 {
1407 	struct btf_dump_type_aux_state *s = &d->type_states[id];
1408 	const struct btf_type *t = btf__type_by_id(d->btf, id);
1409 	const char *orig_name = btf_name_of(d, t->name_off);
1410 	const char **cached_name = &d->cached_names[id];
1411 	size_t dup_cnt;
1412 
1413 	if (t->name_off == 0)
1414 		return "";
1415 
1416 	if (s->name_resolved)
1417 		return *cached_name ? *cached_name : orig_name;
1418 
1419 	dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
1420 	if (dup_cnt > 1) {
1421 		const size_t max_len = 256;
1422 		char new_name[max_len];
1423 
1424 		snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
1425 		*cached_name = strdup(new_name);
1426 	}
1427 
1428 	s->name_resolved = 1;
1429 	return *cached_name ? *cached_name : orig_name;
1430 }
1431 
1432 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id)
1433 {
1434 	return btf_dump_resolve_name(d, id, d->type_names);
1435 }
1436 
1437 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id)
1438 {
1439 	return btf_dump_resolve_name(d, id, d->ident_names);
1440 }
1441