xref: /openbmc/linux/tools/lib/bpf/btf_dump.c (revision 2fa5ebe3)
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 <ctype.h>
14 #include <endian.h>
15 #include <errno.h>
16 #include <limits.h>
17 #include <linux/err.h>
18 #include <linux/btf.h>
19 #include <linux/kernel.h>
20 #include "btf.h"
21 #include "hashmap.h"
22 #include "libbpf.h"
23 #include "libbpf_internal.h"
24 
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;
27 
28 static const char *pfx(int lvl)
29 {
30 	return lvl >= PREFIX_CNT ? PREFIXES : &PREFIXES[PREFIX_CNT - lvl];
31 }
32 
33 enum btf_dump_type_order_state {
34 	NOT_ORDERED,
35 	ORDERING,
36 	ORDERED,
37 };
38 
39 enum btf_dump_type_emit_state {
40 	NOT_EMITTED,
41 	EMITTING,
42 	EMITTED,
43 };
44 
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 */
52 	__u8 fwd_emitted: 1;
53 	/* whether unique non-duplicate name was already assigned */
54 	__u8 name_resolved: 1;
55 	/* whether type is referenced from any other type */
56 	__u8 referenced: 1;
57 };
58 
59 /* indent string length; one indent string is added for each indent level */
60 #define BTF_DATA_INDENT_STR_LEN			32
61 
62 /*
63  * Common internal data for BTF type data dump operations.
64  */
65 struct btf_dump_data {
66 	const void *data_end;		/* end of valid data to show */
67 	bool compact;
68 	bool skip_names;
69 	bool emit_zeroes;
70 	__u8 indent_lvl;	/* base indent level */
71 	char indent_str[BTF_DATA_INDENT_STR_LEN];
72 	/* below are used during iteration */
73 	int depth;
74 	bool is_array_member;
75 	bool is_array_terminated;
76 	bool is_array_char;
77 };
78 
79 struct btf_dump {
80 	const struct btf *btf;
81 	btf_dump_printf_fn_t printf_fn;
82 	void *cb_ctx;
83 	int ptr_sz;
84 	bool strip_mods;
85 	bool skip_anon_defs;
86 	int last_id;
87 
88 	/* per-type auxiliary state */
89 	struct btf_dump_type_aux_state *type_states;
90 	size_t type_states_cap;
91 	/* per-type optional cached unique name, must be freed, if present */
92 	const char **cached_names;
93 	size_t cached_names_cap;
94 
95 	/* topo-sorted list of dependent type definitions */
96 	__u32 *emit_queue;
97 	int emit_queue_cap;
98 	int emit_queue_cnt;
99 
100 	/*
101 	 * stack of type declarations (e.g., chain of modifiers, arrays,
102 	 * funcs, etc)
103 	 */
104 	__u32 *decl_stack;
105 	int decl_stack_cap;
106 	int decl_stack_cnt;
107 
108 	/* maps struct/union/enum name to a number of name occurrences */
109 	struct hashmap *type_names;
110 	/*
111 	 * maps typedef identifiers and enum value names to a number of such
112 	 * name occurrences
113 	 */
114 	struct hashmap *ident_names;
115 	/*
116 	 * data for typed display; allocated if needed.
117 	 */
118 	struct btf_dump_data *typed_dump;
119 };
120 
121 static size_t str_hash_fn(long key, void *ctx)
122 {
123 	return str_hash((void *)key);
124 }
125 
126 static bool str_equal_fn(long a, long b, void *ctx)
127 {
128 	return strcmp((void *)a, (void *)b) == 0;
129 }
130 
131 static const char *btf_name_of(const struct btf_dump *d, __u32 name_off)
132 {
133 	return btf__name_by_offset(d->btf, name_off);
134 }
135 
136 static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)
137 {
138 	va_list args;
139 
140 	va_start(args, fmt);
141 	d->printf_fn(d->cb_ctx, fmt, args);
142 	va_end(args);
143 }
144 
145 static int btf_dump_mark_referenced(struct btf_dump *d);
146 static int btf_dump_resize(struct btf_dump *d);
147 
148 struct btf_dump *btf_dump__new(const struct btf *btf,
149 			       btf_dump_printf_fn_t printf_fn,
150 			       void *ctx,
151 			       const struct btf_dump_opts *opts)
152 {
153 	struct btf_dump *d;
154 	int err;
155 
156 	if (!OPTS_VALID(opts, btf_dump_opts))
157 		return libbpf_err_ptr(-EINVAL);
158 
159 	if (!printf_fn)
160 		return libbpf_err_ptr(-EINVAL);
161 
162 	d = calloc(1, sizeof(struct btf_dump));
163 	if (!d)
164 		return libbpf_err_ptr(-ENOMEM);
165 
166 	d->btf = btf;
167 	d->printf_fn = printf_fn;
168 	d->cb_ctx = ctx;
169 	d->ptr_sz = btf__pointer_size(btf) ? : sizeof(void *);
170 
171 	d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
172 	if (IS_ERR(d->type_names)) {
173 		err = PTR_ERR(d->type_names);
174 		d->type_names = NULL;
175 		goto err;
176 	}
177 	d->ident_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
178 	if (IS_ERR(d->ident_names)) {
179 		err = PTR_ERR(d->ident_names);
180 		d->ident_names = NULL;
181 		goto err;
182 	}
183 
184 	err = btf_dump_resize(d);
185 	if (err)
186 		goto err;
187 
188 	return d;
189 err:
190 	btf_dump__free(d);
191 	return libbpf_err_ptr(err);
192 }
193 
194 static int btf_dump_resize(struct btf_dump *d)
195 {
196 	int err, last_id = btf__type_cnt(d->btf) - 1;
197 
198 	if (last_id <= d->last_id)
199 		return 0;
200 
201 	if (libbpf_ensure_mem((void **)&d->type_states, &d->type_states_cap,
202 			      sizeof(*d->type_states), last_id + 1))
203 		return -ENOMEM;
204 	if (libbpf_ensure_mem((void **)&d->cached_names, &d->cached_names_cap,
205 			      sizeof(*d->cached_names), last_id + 1))
206 		return -ENOMEM;
207 
208 	if (d->last_id == 0) {
209 		/* VOID is special */
210 		d->type_states[0].order_state = ORDERED;
211 		d->type_states[0].emit_state = EMITTED;
212 	}
213 
214 	/* eagerly determine referenced types for anon enums */
215 	err = btf_dump_mark_referenced(d);
216 	if (err)
217 		return err;
218 
219 	d->last_id = last_id;
220 	return 0;
221 }
222 
223 static void btf_dump_free_names(struct hashmap *map)
224 {
225 	size_t bkt;
226 	struct hashmap_entry *cur;
227 
228 	hashmap__for_each_entry(map, cur, bkt)
229 		free((void *)cur->pkey);
230 
231 	hashmap__free(map);
232 }
233 
234 void btf_dump__free(struct btf_dump *d)
235 {
236 	int i;
237 
238 	if (IS_ERR_OR_NULL(d))
239 		return;
240 
241 	free(d->type_states);
242 	if (d->cached_names) {
243 		/* any set cached name is owned by us and should be freed */
244 		for (i = 0; i <= d->last_id; i++) {
245 			if (d->cached_names[i])
246 				free((void *)d->cached_names[i]);
247 		}
248 	}
249 	free(d->cached_names);
250 	free(d->emit_queue);
251 	free(d->decl_stack);
252 	btf_dump_free_names(d->type_names);
253 	btf_dump_free_names(d->ident_names);
254 
255 	free(d);
256 }
257 
258 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr);
259 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id);
260 
261 /*
262  * Dump BTF type in a compilable C syntax, including all the necessary
263  * dependent types, necessary for compilation. If some of the dependent types
264  * were already emitted as part of previous btf_dump__dump_type() invocation
265  * for another type, they won't be emitted again. This API allows callers to
266  * filter out BTF types according to user-defined criterias and emitted only
267  * minimal subset of types, necessary to compile everything. Full struct/union
268  * definitions will still be emitted, even if the only usage is through
269  * pointer and could be satisfied with just a forward declaration.
270  *
271  * Dumping is done in two high-level passes:
272  *   1. Topologically sort type definitions to satisfy C rules of compilation.
273  *   2. Emit type definitions in C syntax.
274  *
275  * Returns 0 on success; <0, otherwise.
276  */
277 int btf_dump__dump_type(struct btf_dump *d, __u32 id)
278 {
279 	int err, i;
280 
281 	if (id >= btf__type_cnt(d->btf))
282 		return libbpf_err(-EINVAL);
283 
284 	err = btf_dump_resize(d);
285 	if (err)
286 		return libbpf_err(err);
287 
288 	d->emit_queue_cnt = 0;
289 	err = btf_dump_order_type(d, id, false);
290 	if (err < 0)
291 		return libbpf_err(err);
292 
293 	for (i = 0; i < d->emit_queue_cnt; i++)
294 		btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
295 
296 	return 0;
297 }
298 
299 /*
300  * Mark all types that are referenced from any other type. This is used to
301  * determine top-level anonymous enums that need to be emitted as an
302  * independent type declarations.
303  * Anonymous enums come in two flavors: either embedded in a struct's field
304  * definition, in which case they have to be declared inline as part of field
305  * type declaration; or as a top-level anonymous enum, typically used for
306  * declaring global constants. It's impossible to distinguish between two
307  * without knowning whether given enum type was referenced from other type:
308  * top-level anonymous enum won't be referenced by anything, while embedded
309  * one will.
310  */
311 static int btf_dump_mark_referenced(struct btf_dump *d)
312 {
313 	int i, j, n = btf__type_cnt(d->btf);
314 	const struct btf_type *t;
315 	__u16 vlen;
316 
317 	for (i = d->last_id + 1; i < n; i++) {
318 		t = btf__type_by_id(d->btf, i);
319 		vlen = btf_vlen(t);
320 
321 		switch (btf_kind(t)) {
322 		case BTF_KIND_INT:
323 		case BTF_KIND_ENUM:
324 		case BTF_KIND_ENUM64:
325 		case BTF_KIND_FWD:
326 		case BTF_KIND_FLOAT:
327 			break;
328 
329 		case BTF_KIND_VOLATILE:
330 		case BTF_KIND_CONST:
331 		case BTF_KIND_RESTRICT:
332 		case BTF_KIND_PTR:
333 		case BTF_KIND_TYPEDEF:
334 		case BTF_KIND_FUNC:
335 		case BTF_KIND_VAR:
336 		case BTF_KIND_DECL_TAG:
337 		case BTF_KIND_TYPE_TAG:
338 			d->type_states[t->type].referenced = 1;
339 			break;
340 
341 		case BTF_KIND_ARRAY: {
342 			const struct btf_array *a = btf_array(t);
343 
344 			d->type_states[a->index_type].referenced = 1;
345 			d->type_states[a->type].referenced = 1;
346 			break;
347 		}
348 		case BTF_KIND_STRUCT:
349 		case BTF_KIND_UNION: {
350 			const struct btf_member *m = btf_members(t);
351 
352 			for (j = 0; j < vlen; j++, m++)
353 				d->type_states[m->type].referenced = 1;
354 			break;
355 		}
356 		case BTF_KIND_FUNC_PROTO: {
357 			const struct btf_param *p = btf_params(t);
358 
359 			for (j = 0; j < vlen; j++, p++)
360 				d->type_states[p->type].referenced = 1;
361 			break;
362 		}
363 		case BTF_KIND_DATASEC: {
364 			const struct btf_var_secinfo *v = btf_var_secinfos(t);
365 
366 			for (j = 0; j < vlen; j++, v++)
367 				d->type_states[v->type].referenced = 1;
368 			break;
369 		}
370 		default:
371 			return -EINVAL;
372 		}
373 	}
374 	return 0;
375 }
376 
377 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
378 {
379 	__u32 *new_queue;
380 	size_t new_cap;
381 
382 	if (d->emit_queue_cnt >= d->emit_queue_cap) {
383 		new_cap = max(16, d->emit_queue_cap * 3 / 2);
384 		new_queue = libbpf_reallocarray(d->emit_queue, new_cap, sizeof(new_queue[0]));
385 		if (!new_queue)
386 			return -ENOMEM;
387 		d->emit_queue = new_queue;
388 		d->emit_queue_cap = new_cap;
389 	}
390 
391 	d->emit_queue[d->emit_queue_cnt++] = id;
392 	return 0;
393 }
394 
395 /*
396  * Determine order of emitting dependent types and specified type to satisfy
397  * C compilation rules.  This is done through topological sorting with an
398  * additional complication which comes from C rules. The main idea for C is
399  * that if some type is "embedded" into a struct/union, it's size needs to be
400  * known at the time of definition of containing type. E.g., for:
401  *
402  *	struct A {};
403  *	struct B { struct A x; }
404  *
405  * struct A *HAS* to be defined before struct B, because it's "embedded",
406  * i.e., it is part of struct B layout. But in the following case:
407  *
408  *	struct A;
409  *	struct B { struct A *x; }
410  *	struct A {};
411  *
412  * it's enough to just have a forward declaration of struct A at the time of
413  * struct B definition, as struct B has a pointer to struct A, so the size of
414  * field x is known without knowing struct A size: it's sizeof(void *).
415  *
416  * Unfortunately, there are some trickier cases we need to handle, e.g.:
417  *
418  *	struct A {}; // if this was forward-declaration: compilation error
419  *	struct B {
420  *		struct { // anonymous struct
421  *			struct A y;
422  *		} *x;
423  *	};
424  *
425  * In this case, struct B's field x is a pointer, so it's size is known
426  * regardless of the size of (anonymous) struct it points to. But because this
427  * struct is anonymous and thus defined inline inside struct B, *and* it
428  * embeds struct A, compiler requires full definition of struct A to be known
429  * before struct B can be defined. This creates a transitive dependency
430  * between struct A and struct B. If struct A was forward-declared before
431  * struct B definition and fully defined after struct B definition, that would
432  * trigger compilation error.
433  *
434  * All this means that while we are doing topological sorting on BTF type
435  * graph, we need to determine relationships between different types (graph
436  * nodes):
437  *   - weak link (relationship) between X and Y, if Y *CAN* be
438  *   forward-declared at the point of X definition;
439  *   - strong link, if Y *HAS* to be fully-defined before X can be defined.
440  *
441  * The rule is as follows. Given a chain of BTF types from X to Y, if there is
442  * BTF_KIND_PTR type in the chain and at least one non-anonymous type
443  * Z (excluding X, including Y), then link is weak. Otherwise, it's strong.
444  * Weak/strong relationship is determined recursively during DFS traversal and
445  * is returned as a result from btf_dump_order_type().
446  *
447  * btf_dump_order_type() is trying to avoid unnecessary forward declarations,
448  * but it is not guaranteeing that no extraneous forward declarations will be
449  * emitted.
450  *
451  * To avoid extra work, algorithm marks some of BTF types as ORDERED, when
452  * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT,
453  * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the
454  * entire graph path, so depending where from one came to that BTF type, it
455  * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM,
456  * once they are processed, there is no need to do it again, so they are
457  * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces
458  * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But
459  * in any case, once those are processed, no need to do it again, as the
460  * result won't change.
461  *
462  * Returns:
463  *   - 1, if type is part of strong link (so there is strong topological
464  *   ordering requirements);
465  *   - 0, if type is part of weak link (so can be satisfied through forward
466  *   declaration);
467  *   - <0, on error (e.g., unsatisfiable type loop detected).
468  */
469 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
470 {
471 	/*
472 	 * Order state is used to detect strong link cycles, but only for BTF
473 	 * kinds that are or could be an independent definition (i.e.,
474 	 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays,
475 	 * func_protos, modifiers are just means to get to these definitions.
476 	 * Int/void don't need definitions, they are assumed to be always
477 	 * properly defined.  We also ignore datasec, var, and funcs for now.
478 	 * So for all non-defining kinds, we never even set ordering state,
479 	 * for defining kinds we set ORDERING and subsequently ORDERED if it
480 	 * forms a strong link.
481 	 */
482 	struct btf_dump_type_aux_state *tstate = &d->type_states[id];
483 	const struct btf_type *t;
484 	__u16 vlen;
485 	int err, i;
486 
487 	/* return true, letting typedefs know that it's ok to be emitted */
488 	if (tstate->order_state == ORDERED)
489 		return 1;
490 
491 	t = btf__type_by_id(d->btf, id);
492 
493 	if (tstate->order_state == ORDERING) {
494 		/* type loop, but resolvable through fwd declaration */
495 		if (btf_is_composite(t) && through_ptr && t->name_off != 0)
496 			return 0;
497 		pr_warn("unsatisfiable type cycle, id:[%u]\n", id);
498 		return -ELOOP;
499 	}
500 
501 	switch (btf_kind(t)) {
502 	case BTF_KIND_INT:
503 	case BTF_KIND_FLOAT:
504 		tstate->order_state = ORDERED;
505 		return 0;
506 
507 	case BTF_KIND_PTR:
508 		err = btf_dump_order_type(d, t->type, true);
509 		tstate->order_state = ORDERED;
510 		return err;
511 
512 	case BTF_KIND_ARRAY:
513 		return btf_dump_order_type(d, btf_array(t)->type, false);
514 
515 	case BTF_KIND_STRUCT:
516 	case BTF_KIND_UNION: {
517 		const struct btf_member *m = btf_members(t);
518 		/*
519 		 * struct/union is part of strong link, only if it's embedded
520 		 * (so no ptr in a path) or it's anonymous (so has to be
521 		 * defined inline, even if declared through ptr)
522 		 */
523 		if (through_ptr && t->name_off != 0)
524 			return 0;
525 
526 		tstate->order_state = ORDERING;
527 
528 		vlen = btf_vlen(t);
529 		for (i = 0; i < vlen; i++, m++) {
530 			err = btf_dump_order_type(d, m->type, false);
531 			if (err < 0)
532 				return err;
533 		}
534 
535 		if (t->name_off != 0) {
536 			err = btf_dump_add_emit_queue_id(d, id);
537 			if (err < 0)
538 				return err;
539 		}
540 
541 		tstate->order_state = ORDERED;
542 		return 1;
543 	}
544 	case BTF_KIND_ENUM:
545 	case BTF_KIND_ENUM64:
546 	case BTF_KIND_FWD:
547 		/*
548 		 * non-anonymous or non-referenced enums are top-level
549 		 * declarations and should be emitted. Same logic can be
550 		 * applied to FWDs, it won't hurt anyways.
551 		 */
552 		if (t->name_off != 0 || !tstate->referenced) {
553 			err = btf_dump_add_emit_queue_id(d, id);
554 			if (err)
555 				return err;
556 		}
557 		tstate->order_state = ORDERED;
558 		return 1;
559 
560 	case BTF_KIND_TYPEDEF: {
561 		int is_strong;
562 
563 		is_strong = btf_dump_order_type(d, t->type, through_ptr);
564 		if (is_strong < 0)
565 			return is_strong;
566 
567 		/* typedef is similar to struct/union w.r.t. fwd-decls */
568 		if (through_ptr && !is_strong)
569 			return 0;
570 
571 		/* typedef is always a named definition */
572 		err = btf_dump_add_emit_queue_id(d, id);
573 		if (err)
574 			return err;
575 
576 		d->type_states[id].order_state = ORDERED;
577 		return 1;
578 	}
579 	case BTF_KIND_VOLATILE:
580 	case BTF_KIND_CONST:
581 	case BTF_KIND_RESTRICT:
582 	case BTF_KIND_TYPE_TAG:
583 		return btf_dump_order_type(d, t->type, through_ptr);
584 
585 	case BTF_KIND_FUNC_PROTO: {
586 		const struct btf_param *p = btf_params(t);
587 		bool is_strong;
588 
589 		err = btf_dump_order_type(d, t->type, through_ptr);
590 		if (err < 0)
591 			return err;
592 		is_strong = err > 0;
593 
594 		vlen = btf_vlen(t);
595 		for (i = 0; i < vlen; i++, p++) {
596 			err = btf_dump_order_type(d, p->type, through_ptr);
597 			if (err < 0)
598 				return err;
599 			if (err > 0)
600 				is_strong = true;
601 		}
602 		return is_strong;
603 	}
604 	case BTF_KIND_FUNC:
605 	case BTF_KIND_VAR:
606 	case BTF_KIND_DATASEC:
607 	case BTF_KIND_DECL_TAG:
608 		d->type_states[id].order_state = ORDERED;
609 		return 0;
610 
611 	default:
612 		return -EINVAL;
613 	}
614 }
615 
616 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
617 					  const struct btf_type *t);
618 
619 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
620 				     const struct btf_type *t);
621 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id,
622 				     const struct btf_type *t, int lvl);
623 
624 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
625 				   const struct btf_type *t);
626 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
627 				   const struct btf_type *t, int lvl);
628 
629 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
630 				  const struct btf_type *t);
631 
632 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
633 				      const struct btf_type *t, int lvl);
634 
635 /* a local view into a shared stack */
636 struct id_stack {
637 	const __u32 *ids;
638 	int cnt;
639 };
640 
641 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
642 				    const char *fname, int lvl);
643 static void btf_dump_emit_type_chain(struct btf_dump *d,
644 				     struct id_stack *decl_stack,
645 				     const char *fname, int lvl);
646 
647 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
648 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
649 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
650 				 const char *orig_name);
651 
652 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
653 {
654 	const struct btf_type *t = btf__type_by_id(d->btf, id);
655 
656 	/* __builtin_va_list is a compiler built-in, which causes compilation
657 	 * errors, when compiling w/ different compiler, then used to compile
658 	 * original code (e.g., GCC to compile kernel, Clang to use generated
659 	 * C header from BTF). As it is built-in, it should be already defined
660 	 * properly internally in compiler.
661 	 */
662 	if (t->name_off == 0)
663 		return false;
664 	return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0;
665 }
666 
667 /*
668  * Emit C-syntax definitions of types from chains of BTF types.
669  *
670  * High-level handling of determining necessary forward declarations are handled
671  * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type
672  * declarations/definitions in C syntax  are handled by a combo of
673  * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to
674  * corresponding btf_dump_emit_*_{def,fwd}() functions.
675  *
676  * We also keep track of "containing struct/union type ID" to determine when
677  * we reference it from inside and thus can avoid emitting unnecessary forward
678  * declaration.
679  *
680  * This algorithm is designed in such a way, that even if some error occurs
681  * (either technical, e.g., out of memory, or logical, i.e., malformed BTF
682  * that doesn't comply to C rules completely), algorithm will try to proceed
683  * and produce as much meaningful output as possible.
684  */
685 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
686 {
687 	struct btf_dump_type_aux_state *tstate = &d->type_states[id];
688 	bool top_level_def = cont_id == 0;
689 	const struct btf_type *t;
690 	__u16 kind;
691 
692 	if (tstate->emit_state == EMITTED)
693 		return;
694 
695 	t = btf__type_by_id(d->btf, id);
696 	kind = btf_kind(t);
697 
698 	if (tstate->emit_state == EMITTING) {
699 		if (tstate->fwd_emitted)
700 			return;
701 
702 		switch (kind) {
703 		case BTF_KIND_STRUCT:
704 		case BTF_KIND_UNION:
705 			/*
706 			 * if we are referencing a struct/union that we are
707 			 * part of - then no need for fwd declaration
708 			 */
709 			if (id == cont_id)
710 				return;
711 			if (t->name_off == 0) {
712 				pr_warn("anonymous struct/union loop, id:[%u]\n",
713 					id);
714 				return;
715 			}
716 			btf_dump_emit_struct_fwd(d, id, t);
717 			btf_dump_printf(d, ";\n\n");
718 			tstate->fwd_emitted = 1;
719 			break;
720 		case BTF_KIND_TYPEDEF:
721 			/*
722 			 * for typedef fwd_emitted means typedef definition
723 			 * was emitted, but it can be used only for "weak"
724 			 * references through pointer only, not for embedding
725 			 */
726 			if (!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->fwd_emitted = 1;
731 			break;
732 		default:
733 			break;
734 		}
735 
736 		return;
737 	}
738 
739 	switch (kind) {
740 	case BTF_KIND_INT:
741 		/* Emit type alias definitions if necessary */
742 		btf_dump_emit_missing_aliases(d, id, t);
743 
744 		tstate->emit_state = EMITTED;
745 		break;
746 	case BTF_KIND_ENUM:
747 	case BTF_KIND_ENUM64:
748 		if (top_level_def) {
749 			btf_dump_emit_enum_def(d, id, t, 0);
750 			btf_dump_printf(d, ";\n\n");
751 		}
752 		tstate->emit_state = EMITTED;
753 		break;
754 	case BTF_KIND_PTR:
755 	case BTF_KIND_VOLATILE:
756 	case BTF_KIND_CONST:
757 	case BTF_KIND_RESTRICT:
758 	case BTF_KIND_TYPE_TAG:
759 		btf_dump_emit_type(d, t->type, cont_id);
760 		break;
761 	case BTF_KIND_ARRAY:
762 		btf_dump_emit_type(d, btf_array(t)->type, cont_id);
763 		break;
764 	case BTF_KIND_FWD:
765 		btf_dump_emit_fwd_def(d, id, t);
766 		btf_dump_printf(d, ";\n\n");
767 		tstate->emit_state = EMITTED;
768 		break;
769 	case BTF_KIND_TYPEDEF:
770 		tstate->emit_state = EMITTING;
771 		btf_dump_emit_type(d, t->type, id);
772 		/*
773 		 * typedef can server as both definition and forward
774 		 * declaration; at this stage someone depends on
775 		 * typedef as a forward declaration (refers to it
776 		 * through pointer), so unless we already did it,
777 		 * emit typedef as a forward declaration
778 		 */
779 		if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
780 			btf_dump_emit_typedef_def(d, id, t, 0);
781 			btf_dump_printf(d, ";\n\n");
782 		}
783 		tstate->emit_state = EMITTED;
784 		break;
785 	case BTF_KIND_STRUCT:
786 	case BTF_KIND_UNION:
787 		tstate->emit_state = EMITTING;
788 		/* if it's a top-level struct/union definition or struct/union
789 		 * is anonymous, then in C we'll be emitting all fields and
790 		 * their types (as opposed to just `struct X`), so we need to
791 		 * make sure that all types, referenced from struct/union
792 		 * members have necessary forward-declarations, where
793 		 * applicable
794 		 */
795 		if (top_level_def || t->name_off == 0) {
796 			const struct btf_member *m = btf_members(t);
797 			__u16 vlen = btf_vlen(t);
798 			int i, new_cont_id;
799 
800 			new_cont_id = t->name_off == 0 ? cont_id : id;
801 			for (i = 0; i < vlen; i++, m++)
802 				btf_dump_emit_type(d, m->type, new_cont_id);
803 		} else if (!tstate->fwd_emitted && id != cont_id) {
804 			btf_dump_emit_struct_fwd(d, id, t);
805 			btf_dump_printf(d, ";\n\n");
806 			tstate->fwd_emitted = 1;
807 		}
808 
809 		if (top_level_def) {
810 			btf_dump_emit_struct_def(d, id, t, 0);
811 			btf_dump_printf(d, ";\n\n");
812 			tstate->emit_state = EMITTED;
813 		} else {
814 			tstate->emit_state = NOT_EMITTED;
815 		}
816 		break;
817 	case BTF_KIND_FUNC_PROTO: {
818 		const struct btf_param *p = btf_params(t);
819 		__u16 n = btf_vlen(t);
820 		int i;
821 
822 		btf_dump_emit_type(d, t->type, cont_id);
823 		for (i = 0; i < n; i++, p++)
824 			btf_dump_emit_type(d, p->type, cont_id);
825 
826 		break;
827 	}
828 	default:
829 		break;
830 	}
831 }
832 
833 static bool btf_is_struct_packed(const struct btf *btf, __u32 id,
834 				 const struct btf_type *t)
835 {
836 	const struct btf_member *m;
837 	int max_align = 1, align, i, bit_sz;
838 	__u16 vlen;
839 
840 	m = btf_members(t);
841 	vlen = btf_vlen(t);
842 	/* all non-bitfield fields have to be naturally aligned */
843 	for (i = 0; i < vlen; i++, m++) {
844 		align = btf__align_of(btf, m->type);
845 		bit_sz = btf_member_bitfield_size(t, i);
846 		if (align && bit_sz == 0 && m->offset % (8 * align) != 0)
847 			return true;
848 		max_align = max(align, max_align);
849 	}
850 	/* size of a non-packed struct has to be a multiple of its alignment */
851 	if (t->size % max_align != 0)
852 		return true;
853 	/*
854 	 * if original struct was marked as packed, but its layout is
855 	 * naturally aligned, we'll detect that it's not packed
856 	 */
857 	return false;
858 }
859 
860 static void btf_dump_emit_bit_padding(const struct btf_dump *d,
861 				      int cur_off, int next_off, int next_align,
862 				      bool in_bitfield, int lvl)
863 {
864 	const struct {
865 		const char *name;
866 		int bits;
867 	} pads[] = {
868 		{"long", d->ptr_sz * 8}, {"int", 32}, {"short", 16}, {"char", 8}
869 	};
870 	int new_off, pad_bits, bits, i;
871 	const char *pad_type;
872 
873 	if (cur_off >= next_off)
874 		return; /* no gap */
875 
876 	/* For filling out padding we want to take advantage of
877 	 * natural alignment rules to minimize unnecessary explicit
878 	 * padding. First, we find the largest type (among long, int,
879 	 * short, or char) that can be used to force naturally aligned
880 	 * boundary. Once determined, we'll use such type to fill in
881 	 * the remaining padding gap. In some cases we can rely on
882 	 * compiler filling some gaps, but sometimes we need to force
883 	 * alignment to close natural alignment with markers like
884 	 * `long: 0` (this is always the case for bitfields).  Note
885 	 * that even if struct itself has, let's say 4-byte alignment
886 	 * (i.e., it only uses up to int-aligned types), using `long:
887 	 * X;` explicit padding doesn't actually change struct's
888 	 * overall alignment requirements, but compiler does take into
889 	 * account that type's (long, in this example) natural
890 	 * alignment requirements when adding implicit padding. We use
891 	 * this fact heavily and don't worry about ruining correct
892 	 * struct alignment requirement.
893 	 */
894 	for (i = 0; i < ARRAY_SIZE(pads); i++) {
895 		pad_bits = pads[i].bits;
896 		pad_type = pads[i].name;
897 
898 		new_off = roundup(cur_off, pad_bits);
899 		if (new_off <= next_off)
900 			break;
901 	}
902 
903 	if (new_off > cur_off && new_off <= next_off) {
904 		/* We need explicit `<type>: 0` aligning mark if next
905 		 * field is right on alignment offset and its
906 		 * alignment requirement is less strict than <type>'s
907 		 * alignment (so compiler won't naturally align to the
908 		 * offset we expect), or if subsequent `<type>: X`,
909 		 * will actually completely fit in the remaining hole,
910 		 * making compiler basically ignore `<type>: X`
911 		 * completely.
912 		 */
913 		if (in_bitfield ||
914 		    (new_off == next_off && roundup(cur_off, next_align * 8) != new_off) ||
915 		    (new_off != next_off && next_off - new_off <= new_off - cur_off))
916 			/* but for bitfields we'll emit explicit bit count */
917 			btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type,
918 					in_bitfield ? new_off - cur_off : 0);
919 		cur_off = new_off;
920 	}
921 
922 	/* Now we know we start at naturally aligned offset for a chosen
923 	 * padding type (long, int, short, or char), and so the rest is just
924 	 * a straightforward filling of remaining padding gap with full
925 	 * `<type>: sizeof(<type>);` markers, except for the last one, which
926 	 * might need smaller than sizeof(<type>) padding.
927 	 */
928 	while (cur_off != next_off) {
929 		bits = min(next_off - cur_off, pad_bits);
930 		if (bits == pad_bits) {
931 			btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
932 			cur_off += bits;
933 			continue;
934 		}
935 		/* For the remainder padding that doesn't cover entire
936 		 * pad_type bit length, we pick the smallest necessary type.
937 		 * This is pure aesthetics, we could have just used `long`,
938 		 * but having smallest necessary one communicates better the
939 		 * scale of the padding gap.
940 		 */
941 		for (i = ARRAY_SIZE(pads) - 1; i >= 0; i--) {
942 			pad_type = pads[i].name;
943 			pad_bits = pads[i].bits;
944 			if (pad_bits < bits)
945 				continue;
946 
947 			btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, bits);
948 			cur_off += bits;
949 			break;
950 		}
951 	}
952 }
953 
954 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
955 				     const struct btf_type *t)
956 {
957 	btf_dump_printf(d, "%s%s%s",
958 			btf_is_struct(t) ? "struct" : "union",
959 			t->name_off ? " " : "",
960 			btf_dump_type_name(d, id));
961 }
962 
963 static void btf_dump_emit_struct_def(struct btf_dump *d,
964 				     __u32 id,
965 				     const struct btf_type *t,
966 				     int lvl)
967 {
968 	const struct btf_member *m = btf_members(t);
969 	bool is_struct = btf_is_struct(t);
970 	bool packed, prev_bitfield = false;
971 	int align, i, off = 0;
972 	__u16 vlen = btf_vlen(t);
973 
974 	align = btf__align_of(d->btf, id);
975 	packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
976 
977 	btf_dump_printf(d, "%s%s%s {",
978 			is_struct ? "struct" : "union",
979 			t->name_off ? " " : "",
980 			btf_dump_type_name(d, id));
981 
982 	for (i = 0; i < vlen; i++, m++) {
983 		const char *fname;
984 		int m_off, m_sz, m_align;
985 		bool in_bitfield;
986 
987 		fname = btf_name_of(d, m->name_off);
988 		m_sz = btf_member_bitfield_size(t, i);
989 		m_off = btf_member_bit_offset(t, i);
990 		m_align = packed ? 1 : btf__align_of(d->btf, m->type);
991 
992 		in_bitfield = prev_bitfield && m_sz != 0;
993 
994 		btf_dump_emit_bit_padding(d, off, m_off, m_align, in_bitfield, lvl + 1);
995 		btf_dump_printf(d, "\n%s", pfx(lvl + 1));
996 		btf_dump_emit_type_decl(d, m->type, fname, lvl + 1);
997 
998 		if (m_sz) {
999 			btf_dump_printf(d, ": %d", m_sz);
1000 			off = m_off + m_sz;
1001 			prev_bitfield = true;
1002 		} else {
1003 			m_sz = max((__s64)0, btf__resolve_size(d->btf, m->type));
1004 			off = m_off + m_sz * 8;
1005 			prev_bitfield = false;
1006 		}
1007 
1008 		btf_dump_printf(d, ";");
1009 	}
1010 
1011 	/* pad at the end, if necessary */
1012 	if (is_struct)
1013 		btf_dump_emit_bit_padding(d, off, t->size * 8, align, false, lvl + 1);
1014 
1015 	/*
1016 	 * Keep `struct empty {}` on a single line,
1017 	 * only print newline when there are regular or padding fields.
1018 	 */
1019 	if (vlen || t->size) {
1020 		btf_dump_printf(d, "\n");
1021 		btf_dump_printf(d, "%s}", pfx(lvl));
1022 	} else {
1023 		btf_dump_printf(d, "}");
1024 	}
1025 	if (packed)
1026 		btf_dump_printf(d, " __attribute__((packed))");
1027 }
1028 
1029 static const char *missing_base_types[][2] = {
1030 	/*
1031 	 * GCC emits typedefs to its internal __PolyX_t types when compiling Arm
1032 	 * SIMD intrinsics. Alias them to standard base types.
1033 	 */
1034 	{ "__Poly8_t",		"unsigned char" },
1035 	{ "__Poly16_t",		"unsigned short" },
1036 	{ "__Poly64_t",		"unsigned long long" },
1037 	{ "__Poly128_t",	"unsigned __int128" },
1038 };
1039 
1040 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
1041 					  const struct btf_type *t)
1042 {
1043 	const char *name = btf_dump_type_name(d, id);
1044 	int i;
1045 
1046 	for (i = 0; i < ARRAY_SIZE(missing_base_types); i++) {
1047 		if (strcmp(name, missing_base_types[i][0]) == 0) {
1048 			btf_dump_printf(d, "typedef %s %s;\n\n",
1049 					missing_base_types[i][1], name);
1050 			break;
1051 		}
1052 	}
1053 }
1054 
1055 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
1056 				   const struct btf_type *t)
1057 {
1058 	btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id));
1059 }
1060 
1061 static void btf_dump_emit_enum32_val(struct btf_dump *d,
1062 				     const struct btf_type *t,
1063 				     int lvl, __u16 vlen)
1064 {
1065 	const struct btf_enum *v = btf_enum(t);
1066 	bool is_signed = btf_kflag(t);
1067 	const char *fmt_str;
1068 	const char *name;
1069 	size_t dup_cnt;
1070 	int i;
1071 
1072 	for (i = 0; i < vlen; i++, v++) {
1073 		name = btf_name_of(d, v->name_off);
1074 		/* enumerators share namespace with typedef idents */
1075 		dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1076 		if (dup_cnt > 1) {
1077 			fmt_str = is_signed ? "\n%s%s___%zd = %d," : "\n%s%s___%zd = %u,";
1078 			btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, dup_cnt, v->val);
1079 		} else {
1080 			fmt_str = is_signed ? "\n%s%s = %d," : "\n%s%s = %u,";
1081 			btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, v->val);
1082 		}
1083 	}
1084 }
1085 
1086 static void btf_dump_emit_enum64_val(struct btf_dump *d,
1087 				     const struct btf_type *t,
1088 				     int lvl, __u16 vlen)
1089 {
1090 	const struct btf_enum64 *v = btf_enum64(t);
1091 	bool is_signed = btf_kflag(t);
1092 	const char *fmt_str;
1093 	const char *name;
1094 	size_t dup_cnt;
1095 	__u64 val;
1096 	int i;
1097 
1098 	for (i = 0; i < vlen; i++, v++) {
1099 		name = btf_name_of(d, v->name_off);
1100 		dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1101 		val = btf_enum64_value(v);
1102 		if (dup_cnt > 1) {
1103 			fmt_str = is_signed ? "\n%s%s___%zd = %lldLL,"
1104 					    : "\n%s%s___%zd = %lluULL,";
1105 			btf_dump_printf(d, fmt_str,
1106 					pfx(lvl + 1), name, dup_cnt,
1107 					(unsigned long long)val);
1108 		} else {
1109 			fmt_str = is_signed ? "\n%s%s = %lldLL,"
1110 					    : "\n%s%s = %lluULL,";
1111 			btf_dump_printf(d, fmt_str,
1112 					pfx(lvl + 1), name,
1113 					(unsigned long long)val);
1114 		}
1115 	}
1116 }
1117 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
1118 				   const struct btf_type *t,
1119 				   int lvl)
1120 {
1121 	__u16 vlen = btf_vlen(t);
1122 
1123 	btf_dump_printf(d, "enum%s%s",
1124 			t->name_off ? " " : "",
1125 			btf_dump_type_name(d, id));
1126 
1127 	if (!vlen)
1128 		return;
1129 
1130 	btf_dump_printf(d, " {");
1131 	if (btf_is_enum(t))
1132 		btf_dump_emit_enum32_val(d, t, lvl, vlen);
1133 	else
1134 		btf_dump_emit_enum64_val(d, t, lvl, vlen);
1135 	btf_dump_printf(d, "\n%s}", pfx(lvl));
1136 
1137 	/* special case enums with special sizes */
1138 	if (t->size == 1) {
1139 		/* one-byte enums can be forced with mode(byte) attribute */
1140 		btf_dump_printf(d, " __attribute__((mode(byte)))");
1141 	} else if (t->size == 8 && d->ptr_sz == 8) {
1142 		/* enum can be 8-byte sized if one of the enumerator values
1143 		 * doesn't fit in 32-bit integer, or by adding mode(word)
1144 		 * attribute (but probably only on 64-bit architectures); do
1145 		 * our best here to try to satisfy the contract without adding
1146 		 * unnecessary attributes
1147 		 */
1148 		bool needs_word_mode;
1149 
1150 		if (btf_is_enum(t)) {
1151 			/* enum can't represent 64-bit values, so we need word mode */
1152 			needs_word_mode = true;
1153 		} else {
1154 			/* enum64 needs mode(word) if none of its values has
1155 			 * non-zero upper 32-bits (which means that all values
1156 			 * fit in 32-bit integers and won't cause compiler to
1157 			 * bump enum to be 64-bit naturally
1158 			 */
1159 			int i;
1160 
1161 			needs_word_mode = true;
1162 			for (i = 0; i < vlen; i++) {
1163 				if (btf_enum64(t)[i].val_hi32 != 0) {
1164 					needs_word_mode = false;
1165 					break;
1166 				}
1167 			}
1168 		}
1169 		if (needs_word_mode)
1170 			btf_dump_printf(d, " __attribute__((mode(word)))");
1171 	}
1172 
1173 }
1174 
1175 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
1176 				  const struct btf_type *t)
1177 {
1178 	const char *name = btf_dump_type_name(d, id);
1179 
1180 	if (btf_kflag(t))
1181 		btf_dump_printf(d, "union %s", name);
1182 	else
1183 		btf_dump_printf(d, "struct %s", name);
1184 }
1185 
1186 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
1187 				     const struct btf_type *t, int lvl)
1188 {
1189 	const char *name = btf_dump_ident_name(d, id);
1190 
1191 	/*
1192 	 * Old GCC versions are emitting invalid typedef for __gnuc_va_list
1193 	 * pointing to VOID. This generates warnings from btf_dump() and
1194 	 * results in uncompilable header file, so we are fixing it up here
1195 	 * with valid typedef into __builtin_va_list.
1196 	 */
1197 	if (t->type == 0 && strcmp(name, "__gnuc_va_list") == 0) {
1198 		btf_dump_printf(d, "typedef __builtin_va_list __gnuc_va_list");
1199 		return;
1200 	}
1201 
1202 	btf_dump_printf(d, "typedef ");
1203 	btf_dump_emit_type_decl(d, t->type, name, lvl);
1204 }
1205 
1206 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
1207 {
1208 	__u32 *new_stack;
1209 	size_t new_cap;
1210 
1211 	if (d->decl_stack_cnt >= d->decl_stack_cap) {
1212 		new_cap = max(16, d->decl_stack_cap * 3 / 2);
1213 		new_stack = libbpf_reallocarray(d->decl_stack, new_cap, sizeof(new_stack[0]));
1214 		if (!new_stack)
1215 			return -ENOMEM;
1216 		d->decl_stack = new_stack;
1217 		d->decl_stack_cap = new_cap;
1218 	}
1219 
1220 	d->decl_stack[d->decl_stack_cnt++] = id;
1221 
1222 	return 0;
1223 }
1224 
1225 /*
1226  * Emit type declaration (e.g., field type declaration in a struct or argument
1227  * declaration in function prototype) in correct C syntax.
1228  *
1229  * For most types it's trivial, but there are few quirky type declaration
1230  * cases worth mentioning:
1231  *   - function prototypes (especially nesting of function prototypes);
1232  *   - arrays;
1233  *   - const/volatile/restrict for pointers vs other types.
1234  *
1235  * For a good discussion of *PARSING* C syntax (as a human), see
1236  * Peter van der Linden's "Expert C Programming: Deep C Secrets",
1237  * Ch.3 "Unscrambling Declarations in C".
1238  *
1239  * It won't help with BTF to C conversion much, though, as it's an opposite
1240  * problem. So we came up with this algorithm in reverse to van der Linden's
1241  * parsing algorithm. It goes from structured BTF representation of type
1242  * declaration to a valid compilable C syntax.
1243  *
1244  * For instance, consider this C typedef:
1245  *	typedef const int * const * arr[10] arr_t;
1246  * It will be represented in BTF with this chain of BTF types:
1247  *	[typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int]
1248  *
1249  * Notice how [const] modifier always goes before type it modifies in BTF type
1250  * graph, but in C syntax, const/volatile/restrict modifiers are written to
1251  * the right of pointers, but to the left of other types. There are also other
1252  * quirks, like function pointers, arrays of them, functions returning other
1253  * functions, etc.
1254  *
1255  * We handle that by pushing all the types to a stack, until we hit "terminal"
1256  * type (int/enum/struct/union/fwd). Then depending on the kind of a type on
1257  * top of a stack, modifiers are handled differently. Array/function pointers
1258  * have also wildly different syntax and how nesting of them are done. See
1259  * code for authoritative definition.
1260  *
1261  * To avoid allocating new stack for each independent chain of BTF types, we
1262  * share one bigger stack, with each chain working only on its own local view
1263  * of a stack frame. Some care is required to "pop" stack frames after
1264  * processing type declaration chain.
1265  */
1266 int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
1267 			     const struct btf_dump_emit_type_decl_opts *opts)
1268 {
1269 	const char *fname;
1270 	int lvl, err;
1271 
1272 	if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
1273 		return libbpf_err(-EINVAL);
1274 
1275 	err = btf_dump_resize(d);
1276 	if (err)
1277 		return libbpf_err(err);
1278 
1279 	fname = OPTS_GET(opts, field_name, "");
1280 	lvl = OPTS_GET(opts, indent_level, 0);
1281 	d->strip_mods = OPTS_GET(opts, strip_mods, false);
1282 	btf_dump_emit_type_decl(d, id, fname, lvl);
1283 	d->strip_mods = false;
1284 	return 0;
1285 }
1286 
1287 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
1288 				    const char *fname, int lvl)
1289 {
1290 	struct id_stack decl_stack;
1291 	const struct btf_type *t;
1292 	int err, stack_start;
1293 
1294 	stack_start = d->decl_stack_cnt;
1295 	for (;;) {
1296 		t = btf__type_by_id(d->btf, id);
1297 		if (d->strip_mods && btf_is_mod(t))
1298 			goto skip_mod;
1299 
1300 		err = btf_dump_push_decl_stack_id(d, id);
1301 		if (err < 0) {
1302 			/*
1303 			 * if we don't have enough memory for entire type decl
1304 			 * chain, restore stack, emit warning, and try to
1305 			 * proceed nevertheless
1306 			 */
1307 			pr_warn("not enough memory for decl stack:%d", err);
1308 			d->decl_stack_cnt = stack_start;
1309 			return;
1310 		}
1311 skip_mod:
1312 		/* VOID */
1313 		if (id == 0)
1314 			break;
1315 
1316 		switch (btf_kind(t)) {
1317 		case BTF_KIND_PTR:
1318 		case BTF_KIND_VOLATILE:
1319 		case BTF_KIND_CONST:
1320 		case BTF_KIND_RESTRICT:
1321 		case BTF_KIND_FUNC_PROTO:
1322 		case BTF_KIND_TYPE_TAG:
1323 			id = t->type;
1324 			break;
1325 		case BTF_KIND_ARRAY:
1326 			id = btf_array(t)->type;
1327 			break;
1328 		case BTF_KIND_INT:
1329 		case BTF_KIND_ENUM:
1330 		case BTF_KIND_ENUM64:
1331 		case BTF_KIND_FWD:
1332 		case BTF_KIND_STRUCT:
1333 		case BTF_KIND_UNION:
1334 		case BTF_KIND_TYPEDEF:
1335 		case BTF_KIND_FLOAT:
1336 			goto done;
1337 		default:
1338 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1339 				btf_kind(t), id);
1340 			goto done;
1341 		}
1342 	}
1343 done:
1344 	/*
1345 	 * We might be inside a chain of declarations (e.g., array of function
1346 	 * pointers returning anonymous (so inlined) structs, having another
1347 	 * array field). Each of those needs its own "stack frame" to handle
1348 	 * emitting of declarations. Those stack frames are non-overlapping
1349 	 * portions of shared btf_dump->decl_stack. To make it a bit nicer to
1350 	 * handle this set of nested stacks, we create a view corresponding to
1351 	 * our own "stack frame" and work with it as an independent stack.
1352 	 * We'll need to clean up after emit_type_chain() returns, though.
1353 	 */
1354 	decl_stack.ids = d->decl_stack + stack_start;
1355 	decl_stack.cnt = d->decl_stack_cnt - stack_start;
1356 	btf_dump_emit_type_chain(d, &decl_stack, fname, lvl);
1357 	/*
1358 	 * emit_type_chain() guarantees that it will pop its entire decl_stack
1359 	 * frame before returning. But it works with a read-only view into
1360 	 * decl_stack, so it doesn't actually pop anything from the
1361 	 * perspective of shared btf_dump->decl_stack, per se. We need to
1362 	 * reset decl_stack state to how it was before us to avoid it growing
1363 	 * all the time.
1364 	 */
1365 	d->decl_stack_cnt = stack_start;
1366 }
1367 
1368 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
1369 {
1370 	const struct btf_type *t;
1371 	__u32 id;
1372 
1373 	while (decl_stack->cnt) {
1374 		id = decl_stack->ids[decl_stack->cnt - 1];
1375 		t = btf__type_by_id(d->btf, id);
1376 
1377 		switch (btf_kind(t)) {
1378 		case BTF_KIND_VOLATILE:
1379 			btf_dump_printf(d, "volatile ");
1380 			break;
1381 		case BTF_KIND_CONST:
1382 			btf_dump_printf(d, "const ");
1383 			break;
1384 		case BTF_KIND_RESTRICT:
1385 			btf_dump_printf(d, "restrict ");
1386 			break;
1387 		default:
1388 			return;
1389 		}
1390 		decl_stack->cnt--;
1391 	}
1392 }
1393 
1394 static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
1395 {
1396 	const struct btf_type *t;
1397 	__u32 id;
1398 
1399 	while (decl_stack->cnt) {
1400 		id = decl_stack->ids[decl_stack->cnt - 1];
1401 		t = btf__type_by_id(d->btf, id);
1402 		if (!btf_is_mod(t))
1403 			return;
1404 		decl_stack->cnt--;
1405 	}
1406 }
1407 
1408 static void btf_dump_emit_name(const struct btf_dump *d,
1409 			       const char *name, bool last_was_ptr)
1410 {
1411 	bool separate = name[0] && !last_was_ptr;
1412 
1413 	btf_dump_printf(d, "%s%s", separate ? " " : "", name);
1414 }
1415 
1416 static void btf_dump_emit_type_chain(struct btf_dump *d,
1417 				     struct id_stack *decls,
1418 				     const char *fname, int lvl)
1419 {
1420 	/*
1421 	 * last_was_ptr is used to determine if we need to separate pointer
1422 	 * asterisk (*) from previous part of type signature with space, so
1423 	 * that we get `int ***`, instead of `int * * *`. We default to true
1424 	 * for cases where we have single pointer in a chain. E.g., in ptr ->
1425 	 * func_proto case. func_proto will start a new emit_type_chain call
1426 	 * with just ptr, which should be emitted as (*) or (*<fname>), so we
1427 	 * don't want to prepend space for that last pointer.
1428 	 */
1429 	bool last_was_ptr = true;
1430 	const struct btf_type *t;
1431 	const char *name;
1432 	__u16 kind;
1433 	__u32 id;
1434 
1435 	while (decls->cnt) {
1436 		id = decls->ids[--decls->cnt];
1437 		if (id == 0) {
1438 			/* VOID is a special snowflake */
1439 			btf_dump_emit_mods(d, decls);
1440 			btf_dump_printf(d, "void");
1441 			last_was_ptr = false;
1442 			continue;
1443 		}
1444 
1445 		t = btf__type_by_id(d->btf, id);
1446 		kind = btf_kind(t);
1447 
1448 		switch (kind) {
1449 		case BTF_KIND_INT:
1450 		case BTF_KIND_FLOAT:
1451 			btf_dump_emit_mods(d, decls);
1452 			name = btf_name_of(d, t->name_off);
1453 			btf_dump_printf(d, "%s", name);
1454 			break;
1455 		case BTF_KIND_STRUCT:
1456 		case BTF_KIND_UNION:
1457 			btf_dump_emit_mods(d, decls);
1458 			/* inline anonymous struct/union */
1459 			if (t->name_off == 0 && !d->skip_anon_defs)
1460 				btf_dump_emit_struct_def(d, id, t, lvl);
1461 			else
1462 				btf_dump_emit_struct_fwd(d, id, t);
1463 			break;
1464 		case BTF_KIND_ENUM:
1465 		case BTF_KIND_ENUM64:
1466 			btf_dump_emit_mods(d, decls);
1467 			/* inline anonymous enum */
1468 			if (t->name_off == 0 && !d->skip_anon_defs)
1469 				btf_dump_emit_enum_def(d, id, t, lvl);
1470 			else
1471 				btf_dump_emit_enum_fwd(d, id, t);
1472 			break;
1473 		case BTF_KIND_FWD:
1474 			btf_dump_emit_mods(d, decls);
1475 			btf_dump_emit_fwd_def(d, id, t);
1476 			break;
1477 		case BTF_KIND_TYPEDEF:
1478 			btf_dump_emit_mods(d, decls);
1479 			btf_dump_printf(d, "%s", btf_dump_ident_name(d, id));
1480 			break;
1481 		case BTF_KIND_PTR:
1482 			btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *");
1483 			break;
1484 		case BTF_KIND_VOLATILE:
1485 			btf_dump_printf(d, " volatile");
1486 			break;
1487 		case BTF_KIND_CONST:
1488 			btf_dump_printf(d, " const");
1489 			break;
1490 		case BTF_KIND_RESTRICT:
1491 			btf_dump_printf(d, " restrict");
1492 			break;
1493 		case BTF_KIND_TYPE_TAG:
1494 			btf_dump_emit_mods(d, decls);
1495 			name = btf_name_of(d, t->name_off);
1496 			btf_dump_printf(d, " __attribute__((btf_type_tag(\"%s\")))", name);
1497 			break;
1498 		case BTF_KIND_ARRAY: {
1499 			const struct btf_array *a = btf_array(t);
1500 			const struct btf_type *next_t;
1501 			__u32 next_id;
1502 			bool multidim;
1503 			/*
1504 			 * GCC has a bug
1505 			 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354)
1506 			 * which causes it to emit extra const/volatile
1507 			 * modifiers for an array, if array's element type has
1508 			 * const/volatile modifiers. Clang doesn't do that.
1509 			 * In general, it doesn't seem very meaningful to have
1510 			 * a const/volatile modifier for array, so we are
1511 			 * going to silently skip them here.
1512 			 */
1513 			btf_dump_drop_mods(d, decls);
1514 
1515 			if (decls->cnt == 0) {
1516 				btf_dump_emit_name(d, fname, last_was_ptr);
1517 				btf_dump_printf(d, "[%u]", a->nelems);
1518 				return;
1519 			}
1520 
1521 			next_id = decls->ids[decls->cnt - 1];
1522 			next_t = btf__type_by_id(d->btf, next_id);
1523 			multidim = btf_is_array(next_t);
1524 			/* we need space if we have named non-pointer */
1525 			if (fname[0] && !last_was_ptr)
1526 				btf_dump_printf(d, " ");
1527 			/* no parentheses for multi-dimensional array */
1528 			if (!multidim)
1529 				btf_dump_printf(d, "(");
1530 			btf_dump_emit_type_chain(d, decls, fname, lvl);
1531 			if (!multidim)
1532 				btf_dump_printf(d, ")");
1533 			btf_dump_printf(d, "[%u]", a->nelems);
1534 			return;
1535 		}
1536 		case BTF_KIND_FUNC_PROTO: {
1537 			const struct btf_param *p = btf_params(t);
1538 			__u16 vlen = btf_vlen(t);
1539 			int i;
1540 
1541 			/*
1542 			 * GCC emits extra volatile qualifier for
1543 			 * __attribute__((noreturn)) function pointers. Clang
1544 			 * doesn't do it. It's a GCC quirk for backwards
1545 			 * compatibility with code written for GCC <2.5. So,
1546 			 * similarly to extra qualifiers for array, just drop
1547 			 * them, instead of handling them.
1548 			 */
1549 			btf_dump_drop_mods(d, decls);
1550 			if (decls->cnt) {
1551 				btf_dump_printf(d, " (");
1552 				btf_dump_emit_type_chain(d, decls, fname, lvl);
1553 				btf_dump_printf(d, ")");
1554 			} else {
1555 				btf_dump_emit_name(d, fname, last_was_ptr);
1556 			}
1557 			btf_dump_printf(d, "(");
1558 			/*
1559 			 * Clang for BPF target generates func_proto with no
1560 			 * args as a func_proto with a single void arg (e.g.,
1561 			 * `int (*f)(void)` vs just `int (*f)()`). We are
1562 			 * going to pretend there are no args for such case.
1563 			 */
1564 			if (vlen == 1 && p->type == 0) {
1565 				btf_dump_printf(d, ")");
1566 				return;
1567 			}
1568 
1569 			for (i = 0; i < vlen; i++, p++) {
1570 				if (i > 0)
1571 					btf_dump_printf(d, ", ");
1572 
1573 				/* last arg of type void is vararg */
1574 				if (i == vlen - 1 && p->type == 0) {
1575 					btf_dump_printf(d, "...");
1576 					break;
1577 				}
1578 
1579 				name = btf_name_of(d, p->name_off);
1580 				btf_dump_emit_type_decl(d, p->type, name, lvl);
1581 			}
1582 
1583 			btf_dump_printf(d, ")");
1584 			return;
1585 		}
1586 		default:
1587 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1588 				kind, id);
1589 			return;
1590 		}
1591 
1592 		last_was_ptr = kind == BTF_KIND_PTR;
1593 	}
1594 
1595 	btf_dump_emit_name(d, fname, last_was_ptr);
1596 }
1597 
1598 /* show type name as (type_name) */
1599 static void btf_dump_emit_type_cast(struct btf_dump *d, __u32 id,
1600 				    bool top_level)
1601 {
1602 	const struct btf_type *t;
1603 
1604 	/* for array members, we don't bother emitting type name for each
1605 	 * member to avoid the redundancy of
1606 	 * .name = (char[4])[(char)'f',(char)'o',(char)'o',]
1607 	 */
1608 	if (d->typed_dump->is_array_member)
1609 		return;
1610 
1611 	/* avoid type name specification for variable/section; it will be done
1612 	 * for the associated variable value(s).
1613 	 */
1614 	t = btf__type_by_id(d->btf, id);
1615 	if (btf_is_var(t) || btf_is_datasec(t))
1616 		return;
1617 
1618 	if (top_level)
1619 		btf_dump_printf(d, "(");
1620 
1621 	d->skip_anon_defs = true;
1622 	d->strip_mods = true;
1623 	btf_dump_emit_type_decl(d, id, "", 0);
1624 	d->strip_mods = false;
1625 	d->skip_anon_defs = false;
1626 
1627 	if (top_level)
1628 		btf_dump_printf(d, ")");
1629 }
1630 
1631 /* return number of duplicates (occurrences) of a given name */
1632 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
1633 				 const char *orig_name)
1634 {
1635 	char *old_name, *new_name;
1636 	size_t dup_cnt = 0;
1637 	int err;
1638 
1639 	new_name = strdup(orig_name);
1640 	if (!new_name)
1641 		return 1;
1642 
1643 	(void)hashmap__find(name_map, orig_name, &dup_cnt);
1644 	dup_cnt++;
1645 
1646 	err = hashmap__set(name_map, new_name, dup_cnt, &old_name, NULL);
1647 	if (err)
1648 		free(new_name);
1649 
1650 	free(old_name);
1651 
1652 	return dup_cnt;
1653 }
1654 
1655 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
1656 					 struct hashmap *name_map)
1657 {
1658 	struct btf_dump_type_aux_state *s = &d->type_states[id];
1659 	const struct btf_type *t = btf__type_by_id(d->btf, id);
1660 	const char *orig_name = btf_name_of(d, t->name_off);
1661 	const char **cached_name = &d->cached_names[id];
1662 	size_t dup_cnt;
1663 
1664 	if (t->name_off == 0)
1665 		return "";
1666 
1667 	if (s->name_resolved)
1668 		return *cached_name ? *cached_name : orig_name;
1669 
1670 	if (btf_is_fwd(t) || (btf_is_enum(t) && btf_vlen(t) == 0)) {
1671 		s->name_resolved = 1;
1672 		return orig_name;
1673 	}
1674 
1675 	dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
1676 	if (dup_cnt > 1) {
1677 		const size_t max_len = 256;
1678 		char new_name[max_len];
1679 
1680 		snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
1681 		*cached_name = strdup(new_name);
1682 	}
1683 
1684 	s->name_resolved = 1;
1685 	return *cached_name ? *cached_name : orig_name;
1686 }
1687 
1688 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id)
1689 {
1690 	return btf_dump_resolve_name(d, id, d->type_names);
1691 }
1692 
1693 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id)
1694 {
1695 	return btf_dump_resolve_name(d, id, d->ident_names);
1696 }
1697 
1698 static int btf_dump_dump_type_data(struct btf_dump *d,
1699 				   const char *fname,
1700 				   const struct btf_type *t,
1701 				   __u32 id,
1702 				   const void *data,
1703 				   __u8 bits_offset,
1704 				   __u8 bit_sz);
1705 
1706 static const char *btf_dump_data_newline(struct btf_dump *d)
1707 {
1708 	return d->typed_dump->compact || d->typed_dump->depth == 0 ? "" : "\n";
1709 }
1710 
1711 static const char *btf_dump_data_delim(struct btf_dump *d)
1712 {
1713 	return d->typed_dump->depth == 0 ? "" : ",";
1714 }
1715 
1716 static void btf_dump_data_pfx(struct btf_dump *d)
1717 {
1718 	int i, lvl = d->typed_dump->indent_lvl + d->typed_dump->depth;
1719 
1720 	if (d->typed_dump->compact)
1721 		return;
1722 
1723 	for (i = 0; i < lvl; i++)
1724 		btf_dump_printf(d, "%s", d->typed_dump->indent_str);
1725 }
1726 
1727 /* A macro is used here as btf_type_value[s]() appends format specifiers
1728  * to the format specifier passed in; these do the work of appending
1729  * delimiters etc while the caller simply has to specify the type values
1730  * in the format specifier + value(s).
1731  */
1732 #define btf_dump_type_values(d, fmt, ...)				\
1733 	btf_dump_printf(d, fmt "%s%s",					\
1734 			##__VA_ARGS__,					\
1735 			btf_dump_data_delim(d),				\
1736 			btf_dump_data_newline(d))
1737 
1738 static int btf_dump_unsupported_data(struct btf_dump *d,
1739 				     const struct btf_type *t,
1740 				     __u32 id)
1741 {
1742 	btf_dump_printf(d, "<unsupported kind:%u>", btf_kind(t));
1743 	return -ENOTSUP;
1744 }
1745 
1746 static int btf_dump_get_bitfield_value(struct btf_dump *d,
1747 				       const struct btf_type *t,
1748 				       const void *data,
1749 				       __u8 bits_offset,
1750 				       __u8 bit_sz,
1751 				       __u64 *value)
1752 {
1753 	__u16 left_shift_bits, right_shift_bits;
1754 	const __u8 *bytes = data;
1755 	__u8 nr_copy_bits;
1756 	__u64 num = 0;
1757 	int i;
1758 
1759 	/* Maximum supported bitfield size is 64 bits */
1760 	if (t->size > 8) {
1761 		pr_warn("unexpected bitfield size %d\n", t->size);
1762 		return -EINVAL;
1763 	}
1764 
1765 	/* Bitfield value retrieval is done in two steps; first relevant bytes are
1766 	 * stored in num, then we left/right shift num to eliminate irrelevant bits.
1767 	 */
1768 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1769 	for (i = t->size - 1; i >= 0; i--)
1770 		num = num * 256 + bytes[i];
1771 	nr_copy_bits = bit_sz + bits_offset;
1772 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1773 	for (i = 0; i < t->size; i++)
1774 		num = num * 256 + bytes[i];
1775 	nr_copy_bits = t->size * 8 - bits_offset;
1776 #else
1777 # error "Unrecognized __BYTE_ORDER__"
1778 #endif
1779 	left_shift_bits = 64 - nr_copy_bits;
1780 	right_shift_bits = 64 - bit_sz;
1781 
1782 	*value = (num << left_shift_bits) >> right_shift_bits;
1783 
1784 	return 0;
1785 }
1786 
1787 static int btf_dump_bitfield_check_zero(struct btf_dump *d,
1788 					const struct btf_type *t,
1789 					const void *data,
1790 					__u8 bits_offset,
1791 					__u8 bit_sz)
1792 {
1793 	__u64 check_num;
1794 	int err;
1795 
1796 	err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &check_num);
1797 	if (err)
1798 		return err;
1799 	if (check_num == 0)
1800 		return -ENODATA;
1801 	return 0;
1802 }
1803 
1804 static int btf_dump_bitfield_data(struct btf_dump *d,
1805 				  const struct btf_type *t,
1806 				  const void *data,
1807 				  __u8 bits_offset,
1808 				  __u8 bit_sz)
1809 {
1810 	__u64 print_num;
1811 	int err;
1812 
1813 	err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &print_num);
1814 	if (err)
1815 		return err;
1816 
1817 	btf_dump_type_values(d, "0x%llx", (unsigned long long)print_num);
1818 
1819 	return 0;
1820 }
1821 
1822 /* ints, floats and ptrs */
1823 static int btf_dump_base_type_check_zero(struct btf_dump *d,
1824 					 const struct btf_type *t,
1825 					 __u32 id,
1826 					 const void *data)
1827 {
1828 	static __u8 bytecmp[16] = {};
1829 	int nr_bytes;
1830 
1831 	/* For pointer types, pointer size is not defined on a per-type basis.
1832 	 * On dump creation however, we store the pointer size.
1833 	 */
1834 	if (btf_kind(t) == BTF_KIND_PTR)
1835 		nr_bytes = d->ptr_sz;
1836 	else
1837 		nr_bytes = t->size;
1838 
1839 	if (nr_bytes < 1 || nr_bytes > 16) {
1840 		pr_warn("unexpected size %d for id [%u]\n", nr_bytes, id);
1841 		return -EINVAL;
1842 	}
1843 
1844 	if (memcmp(data, bytecmp, nr_bytes) == 0)
1845 		return -ENODATA;
1846 	return 0;
1847 }
1848 
1849 static bool ptr_is_aligned(const struct btf *btf, __u32 type_id,
1850 			   const void *data)
1851 {
1852 	int alignment = btf__align_of(btf, type_id);
1853 
1854 	if (alignment == 0)
1855 		return false;
1856 
1857 	return ((uintptr_t)data) % alignment == 0;
1858 }
1859 
1860 static int btf_dump_int_data(struct btf_dump *d,
1861 			     const struct btf_type *t,
1862 			     __u32 type_id,
1863 			     const void *data,
1864 			     __u8 bits_offset)
1865 {
1866 	__u8 encoding = btf_int_encoding(t);
1867 	bool sign = encoding & BTF_INT_SIGNED;
1868 	char buf[16] __attribute__((aligned(16)));
1869 	int sz = t->size;
1870 
1871 	if (sz == 0 || sz > sizeof(buf)) {
1872 		pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1873 		return -EINVAL;
1874 	}
1875 
1876 	/* handle packed int data - accesses of integers not aligned on
1877 	 * int boundaries can cause problems on some platforms.
1878 	 */
1879 	if (!ptr_is_aligned(d->btf, type_id, data)) {
1880 		memcpy(buf, data, sz);
1881 		data = buf;
1882 	}
1883 
1884 	switch (sz) {
1885 	case 16: {
1886 		const __u64 *ints = data;
1887 		__u64 lsi, msi;
1888 
1889 		/* avoid use of __int128 as some 32-bit platforms do not
1890 		 * support it.
1891 		 */
1892 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1893 		lsi = ints[0];
1894 		msi = ints[1];
1895 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1896 		lsi = ints[1];
1897 		msi = ints[0];
1898 #else
1899 # error "Unrecognized __BYTE_ORDER__"
1900 #endif
1901 		if (msi == 0)
1902 			btf_dump_type_values(d, "0x%llx", (unsigned long long)lsi);
1903 		else
1904 			btf_dump_type_values(d, "0x%llx%016llx", (unsigned long long)msi,
1905 					     (unsigned long long)lsi);
1906 		break;
1907 	}
1908 	case 8:
1909 		if (sign)
1910 			btf_dump_type_values(d, "%lld", *(long long *)data);
1911 		else
1912 			btf_dump_type_values(d, "%llu", *(unsigned long long *)data);
1913 		break;
1914 	case 4:
1915 		if (sign)
1916 			btf_dump_type_values(d, "%d", *(__s32 *)data);
1917 		else
1918 			btf_dump_type_values(d, "%u", *(__u32 *)data);
1919 		break;
1920 	case 2:
1921 		if (sign)
1922 			btf_dump_type_values(d, "%d", *(__s16 *)data);
1923 		else
1924 			btf_dump_type_values(d, "%u", *(__u16 *)data);
1925 		break;
1926 	case 1:
1927 		if (d->typed_dump->is_array_char) {
1928 			/* check for null terminator */
1929 			if (d->typed_dump->is_array_terminated)
1930 				break;
1931 			if (*(char *)data == '\0') {
1932 				d->typed_dump->is_array_terminated = true;
1933 				break;
1934 			}
1935 			if (isprint(*(char *)data)) {
1936 				btf_dump_type_values(d, "'%c'", *(char *)data);
1937 				break;
1938 			}
1939 		}
1940 		if (sign)
1941 			btf_dump_type_values(d, "%d", *(__s8 *)data);
1942 		else
1943 			btf_dump_type_values(d, "%u", *(__u8 *)data);
1944 		break;
1945 	default:
1946 		pr_warn("unexpected sz %d for id [%u]\n", sz, type_id);
1947 		return -EINVAL;
1948 	}
1949 	return 0;
1950 }
1951 
1952 union float_data {
1953 	long double ld;
1954 	double d;
1955 	float f;
1956 };
1957 
1958 static int btf_dump_float_data(struct btf_dump *d,
1959 			       const struct btf_type *t,
1960 			       __u32 type_id,
1961 			       const void *data)
1962 {
1963 	const union float_data *flp = data;
1964 	union float_data fl;
1965 	int sz = t->size;
1966 
1967 	/* handle unaligned data; copy to local union */
1968 	if (!ptr_is_aligned(d->btf, type_id, data)) {
1969 		memcpy(&fl, data, sz);
1970 		flp = &fl;
1971 	}
1972 
1973 	switch (sz) {
1974 	case 16:
1975 		btf_dump_type_values(d, "%Lf", flp->ld);
1976 		break;
1977 	case 8:
1978 		btf_dump_type_values(d, "%lf", flp->d);
1979 		break;
1980 	case 4:
1981 		btf_dump_type_values(d, "%f", flp->f);
1982 		break;
1983 	default:
1984 		pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1985 		return -EINVAL;
1986 	}
1987 	return 0;
1988 }
1989 
1990 static int btf_dump_var_data(struct btf_dump *d,
1991 			     const struct btf_type *v,
1992 			     __u32 id,
1993 			     const void *data)
1994 {
1995 	enum btf_func_linkage linkage = btf_var(v)->linkage;
1996 	const struct btf_type *t;
1997 	const char *l;
1998 	__u32 type_id;
1999 
2000 	switch (linkage) {
2001 	case BTF_FUNC_STATIC:
2002 		l = "static ";
2003 		break;
2004 	case BTF_FUNC_EXTERN:
2005 		l = "extern ";
2006 		break;
2007 	case BTF_FUNC_GLOBAL:
2008 	default:
2009 		l = "";
2010 		break;
2011 	}
2012 
2013 	/* format of output here is [linkage] [type] [varname] = (type)value,
2014 	 * for example "static int cpu_profile_flip = (int)1"
2015 	 */
2016 	btf_dump_printf(d, "%s", l);
2017 	type_id = v->type;
2018 	t = btf__type_by_id(d->btf, type_id);
2019 	btf_dump_emit_type_cast(d, type_id, false);
2020 	btf_dump_printf(d, " %s = ", btf_name_of(d, v->name_off));
2021 	return btf_dump_dump_type_data(d, NULL, t, type_id, data, 0, 0);
2022 }
2023 
2024 static int btf_dump_array_data(struct btf_dump *d,
2025 			       const struct btf_type *t,
2026 			       __u32 id,
2027 			       const void *data)
2028 {
2029 	const struct btf_array *array = btf_array(t);
2030 	const struct btf_type *elem_type;
2031 	__u32 i, elem_type_id;
2032 	__s64 elem_size;
2033 	bool is_array_member;
2034 
2035 	elem_type_id = array->type;
2036 	elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2037 	elem_size = btf__resolve_size(d->btf, elem_type_id);
2038 	if (elem_size <= 0) {
2039 		pr_warn("unexpected elem size %zd for array type [%u]\n",
2040 			(ssize_t)elem_size, id);
2041 		return -EINVAL;
2042 	}
2043 
2044 	if (btf_is_int(elem_type)) {
2045 		/*
2046 		 * BTF_INT_CHAR encoding never seems to be set for
2047 		 * char arrays, so if size is 1 and element is
2048 		 * printable as a char, we'll do that.
2049 		 */
2050 		if (elem_size == 1)
2051 			d->typed_dump->is_array_char = true;
2052 	}
2053 
2054 	/* note that we increment depth before calling btf_dump_print() below;
2055 	 * this is intentional.  btf_dump_data_newline() will not print a
2056 	 * newline for depth 0 (since this leaves us with trailing newlines
2057 	 * at the end of typed display), so depth is incremented first.
2058 	 * For similar reasons, we decrement depth before showing the closing
2059 	 * parenthesis.
2060 	 */
2061 	d->typed_dump->depth++;
2062 	btf_dump_printf(d, "[%s", btf_dump_data_newline(d));
2063 
2064 	/* may be a multidimensional array, so store current "is array member"
2065 	 * status so we can restore it correctly later.
2066 	 */
2067 	is_array_member = d->typed_dump->is_array_member;
2068 	d->typed_dump->is_array_member = true;
2069 	for (i = 0; i < array->nelems; i++, data += elem_size) {
2070 		if (d->typed_dump->is_array_terminated)
2071 			break;
2072 		btf_dump_dump_type_data(d, NULL, elem_type, elem_type_id, data, 0, 0);
2073 	}
2074 	d->typed_dump->is_array_member = is_array_member;
2075 	d->typed_dump->depth--;
2076 	btf_dump_data_pfx(d);
2077 	btf_dump_type_values(d, "]");
2078 
2079 	return 0;
2080 }
2081 
2082 static int btf_dump_struct_data(struct btf_dump *d,
2083 				const struct btf_type *t,
2084 				__u32 id,
2085 				const void *data)
2086 {
2087 	const struct btf_member *m = btf_members(t);
2088 	__u16 n = btf_vlen(t);
2089 	int i, err = 0;
2090 
2091 	/* note that we increment depth before calling btf_dump_print() below;
2092 	 * this is intentional.  btf_dump_data_newline() will not print a
2093 	 * newline for depth 0 (since this leaves us with trailing newlines
2094 	 * at the end of typed display), so depth is incremented first.
2095 	 * For similar reasons, we decrement depth before showing the closing
2096 	 * parenthesis.
2097 	 */
2098 	d->typed_dump->depth++;
2099 	btf_dump_printf(d, "{%s", btf_dump_data_newline(d));
2100 
2101 	for (i = 0; i < n; i++, m++) {
2102 		const struct btf_type *mtype;
2103 		const char *mname;
2104 		__u32 moffset;
2105 		__u8 bit_sz;
2106 
2107 		mtype = btf__type_by_id(d->btf, m->type);
2108 		mname = btf_name_of(d, m->name_off);
2109 		moffset = btf_member_bit_offset(t, i);
2110 
2111 		bit_sz = btf_member_bitfield_size(t, i);
2112 		err = btf_dump_dump_type_data(d, mname, mtype, m->type, data + moffset / 8,
2113 					      moffset % 8, bit_sz);
2114 		if (err < 0)
2115 			return err;
2116 	}
2117 	d->typed_dump->depth--;
2118 	btf_dump_data_pfx(d);
2119 	btf_dump_type_values(d, "}");
2120 	return err;
2121 }
2122 
2123 union ptr_data {
2124 	unsigned int p;
2125 	unsigned long long lp;
2126 };
2127 
2128 static int btf_dump_ptr_data(struct btf_dump *d,
2129 			      const struct btf_type *t,
2130 			      __u32 id,
2131 			      const void *data)
2132 {
2133 	if (ptr_is_aligned(d->btf, id, data) && d->ptr_sz == sizeof(void *)) {
2134 		btf_dump_type_values(d, "%p", *(void **)data);
2135 	} else {
2136 		union ptr_data pt;
2137 
2138 		memcpy(&pt, data, d->ptr_sz);
2139 		if (d->ptr_sz == 4)
2140 			btf_dump_type_values(d, "0x%x", pt.p);
2141 		else
2142 			btf_dump_type_values(d, "0x%llx", pt.lp);
2143 	}
2144 	return 0;
2145 }
2146 
2147 static int btf_dump_get_enum_value(struct btf_dump *d,
2148 				   const struct btf_type *t,
2149 				   const void *data,
2150 				   __u32 id,
2151 				   __s64 *value)
2152 {
2153 	bool is_signed = btf_kflag(t);
2154 
2155 	if (!ptr_is_aligned(d->btf, id, data)) {
2156 		__u64 val;
2157 		int err;
2158 
2159 		err = btf_dump_get_bitfield_value(d, t, data, 0, 0, &val);
2160 		if (err)
2161 			return err;
2162 		*value = (__s64)val;
2163 		return 0;
2164 	}
2165 
2166 	switch (t->size) {
2167 	case 8:
2168 		*value = *(__s64 *)data;
2169 		return 0;
2170 	case 4:
2171 		*value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;
2172 		return 0;
2173 	case 2:
2174 		*value = is_signed ? *(__s16 *)data : *(__u16 *)data;
2175 		return 0;
2176 	case 1:
2177 		*value = is_signed ? *(__s8 *)data : *(__u8 *)data;
2178 		return 0;
2179 	default:
2180 		pr_warn("unexpected size %d for enum, id:[%u]\n", t->size, id);
2181 		return -EINVAL;
2182 	}
2183 }
2184 
2185 static int btf_dump_enum_data(struct btf_dump *d,
2186 			      const struct btf_type *t,
2187 			      __u32 id,
2188 			      const void *data)
2189 {
2190 	bool is_signed;
2191 	__s64 value;
2192 	int i, err;
2193 
2194 	err = btf_dump_get_enum_value(d, t, data, id, &value);
2195 	if (err)
2196 		return err;
2197 
2198 	is_signed = btf_kflag(t);
2199 	if (btf_is_enum(t)) {
2200 		const struct btf_enum *e;
2201 
2202 		for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) {
2203 			if (value != e->val)
2204 				continue;
2205 			btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2206 			return 0;
2207 		}
2208 
2209 		btf_dump_type_values(d, is_signed ? "%d" : "%u", value);
2210 	} else {
2211 		const struct btf_enum64 *e;
2212 
2213 		for (i = 0, e = btf_enum64(t); i < btf_vlen(t); i++, e++) {
2214 			if (value != btf_enum64_value(e))
2215 				continue;
2216 			btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2217 			return 0;
2218 		}
2219 
2220 		btf_dump_type_values(d, is_signed ? "%lldLL" : "%lluULL",
2221 				     (unsigned long long)value);
2222 	}
2223 	return 0;
2224 }
2225 
2226 static int btf_dump_datasec_data(struct btf_dump *d,
2227 				 const struct btf_type *t,
2228 				 __u32 id,
2229 				 const void *data)
2230 {
2231 	const struct btf_var_secinfo *vsi;
2232 	const struct btf_type *var;
2233 	__u32 i;
2234 	int err;
2235 
2236 	btf_dump_type_values(d, "SEC(\"%s\") ", btf_name_of(d, t->name_off));
2237 
2238 	for (i = 0, vsi = btf_var_secinfos(t); i < btf_vlen(t); i++, vsi++) {
2239 		var = btf__type_by_id(d->btf, vsi->type);
2240 		err = btf_dump_dump_type_data(d, NULL, var, vsi->type, data + vsi->offset, 0, 0);
2241 		if (err < 0)
2242 			return err;
2243 		btf_dump_printf(d, ";");
2244 	}
2245 	return 0;
2246 }
2247 
2248 /* return size of type, or if base type overflows, return -E2BIG. */
2249 static int btf_dump_type_data_check_overflow(struct btf_dump *d,
2250 					     const struct btf_type *t,
2251 					     __u32 id,
2252 					     const void *data,
2253 					     __u8 bits_offset)
2254 {
2255 	__s64 size = btf__resolve_size(d->btf, id);
2256 
2257 	if (size < 0 || size >= INT_MAX) {
2258 		pr_warn("unexpected size [%zu] for id [%u]\n",
2259 			(size_t)size, id);
2260 		return -EINVAL;
2261 	}
2262 
2263 	/* Only do overflow checking for base types; we do not want to
2264 	 * avoid showing part of a struct, union or array, even if we
2265 	 * do not have enough data to show the full object.  By
2266 	 * restricting overflow checking to base types we can ensure
2267 	 * that partial display succeeds, while avoiding overflowing
2268 	 * and using bogus data for display.
2269 	 */
2270 	t = skip_mods_and_typedefs(d->btf, id, NULL);
2271 	if (!t) {
2272 		pr_warn("unexpected error skipping mods/typedefs for id [%u]\n",
2273 			id);
2274 		return -EINVAL;
2275 	}
2276 
2277 	switch (btf_kind(t)) {
2278 	case BTF_KIND_INT:
2279 	case BTF_KIND_FLOAT:
2280 	case BTF_KIND_PTR:
2281 	case BTF_KIND_ENUM:
2282 	case BTF_KIND_ENUM64:
2283 		if (data + bits_offset / 8 + size > d->typed_dump->data_end)
2284 			return -E2BIG;
2285 		break;
2286 	default:
2287 		break;
2288 	}
2289 	return (int)size;
2290 }
2291 
2292 static int btf_dump_type_data_check_zero(struct btf_dump *d,
2293 					 const struct btf_type *t,
2294 					 __u32 id,
2295 					 const void *data,
2296 					 __u8 bits_offset,
2297 					 __u8 bit_sz)
2298 {
2299 	__s64 value;
2300 	int i, err;
2301 
2302 	/* toplevel exceptions; we show zero values if
2303 	 * - we ask for them (emit_zeros)
2304 	 * - if we are at top-level so we see "struct empty { }"
2305 	 * - or if we are an array member and the array is non-empty and
2306 	 *   not a char array; we don't want to be in a situation where we
2307 	 *   have an integer array 0, 1, 0, 1 and only show non-zero values.
2308 	 *   If the array contains zeroes only, or is a char array starting
2309 	 *   with a '\0', the array-level check_zero() will prevent showing it;
2310 	 *   we are concerned with determining zero value at the array member
2311 	 *   level here.
2312 	 */
2313 	if (d->typed_dump->emit_zeroes || d->typed_dump->depth == 0 ||
2314 	    (d->typed_dump->is_array_member &&
2315 	     !d->typed_dump->is_array_char))
2316 		return 0;
2317 
2318 	t = skip_mods_and_typedefs(d->btf, id, NULL);
2319 
2320 	switch (btf_kind(t)) {
2321 	case BTF_KIND_INT:
2322 		if (bit_sz)
2323 			return btf_dump_bitfield_check_zero(d, t, data, bits_offset, bit_sz);
2324 		return btf_dump_base_type_check_zero(d, t, id, data);
2325 	case BTF_KIND_FLOAT:
2326 	case BTF_KIND_PTR:
2327 		return btf_dump_base_type_check_zero(d, t, id, data);
2328 	case BTF_KIND_ARRAY: {
2329 		const struct btf_array *array = btf_array(t);
2330 		const struct btf_type *elem_type;
2331 		__u32 elem_type_id, elem_size;
2332 		bool ischar;
2333 
2334 		elem_type_id = array->type;
2335 		elem_size = btf__resolve_size(d->btf, elem_type_id);
2336 		elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2337 
2338 		ischar = btf_is_int(elem_type) && elem_size == 1;
2339 
2340 		/* check all elements; if _any_ element is nonzero, all
2341 		 * of array is displayed.  We make an exception however
2342 		 * for char arrays where the first element is 0; these
2343 		 * are considered zeroed also, even if later elements are
2344 		 * non-zero because the string is terminated.
2345 		 */
2346 		for (i = 0; i < array->nelems; i++) {
2347 			if (i == 0 && ischar && *(char *)data == 0)
2348 				return -ENODATA;
2349 			err = btf_dump_type_data_check_zero(d, elem_type,
2350 							    elem_type_id,
2351 							    data +
2352 							    (i * elem_size),
2353 							    bits_offset, 0);
2354 			if (err != -ENODATA)
2355 				return err;
2356 		}
2357 		return -ENODATA;
2358 	}
2359 	case BTF_KIND_STRUCT:
2360 	case BTF_KIND_UNION: {
2361 		const struct btf_member *m = btf_members(t);
2362 		__u16 n = btf_vlen(t);
2363 
2364 		/* if any struct/union member is non-zero, the struct/union
2365 		 * is considered non-zero and dumped.
2366 		 */
2367 		for (i = 0; i < n; i++, m++) {
2368 			const struct btf_type *mtype;
2369 			__u32 moffset;
2370 
2371 			mtype = btf__type_by_id(d->btf, m->type);
2372 			moffset = btf_member_bit_offset(t, i);
2373 
2374 			/* btf_int_bits() does not store member bitfield size;
2375 			 * bitfield size needs to be stored here so int display
2376 			 * of member can retrieve it.
2377 			 */
2378 			bit_sz = btf_member_bitfield_size(t, i);
2379 			err = btf_dump_type_data_check_zero(d, mtype, m->type, data + moffset / 8,
2380 							    moffset % 8, bit_sz);
2381 			if (err != ENODATA)
2382 				return err;
2383 		}
2384 		return -ENODATA;
2385 	}
2386 	case BTF_KIND_ENUM:
2387 	case BTF_KIND_ENUM64:
2388 		err = btf_dump_get_enum_value(d, t, data, id, &value);
2389 		if (err)
2390 			return err;
2391 		if (value == 0)
2392 			return -ENODATA;
2393 		return 0;
2394 	default:
2395 		return 0;
2396 	}
2397 }
2398 
2399 /* returns size of data dumped, or error. */
2400 static int btf_dump_dump_type_data(struct btf_dump *d,
2401 				   const char *fname,
2402 				   const struct btf_type *t,
2403 				   __u32 id,
2404 				   const void *data,
2405 				   __u8 bits_offset,
2406 				   __u8 bit_sz)
2407 {
2408 	int size, err = 0;
2409 
2410 	size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset);
2411 	if (size < 0)
2412 		return size;
2413 	err = btf_dump_type_data_check_zero(d, t, id, data, bits_offset, bit_sz);
2414 	if (err) {
2415 		/* zeroed data is expected and not an error, so simply skip
2416 		 * dumping such data.  Record other errors however.
2417 		 */
2418 		if (err == -ENODATA)
2419 			return size;
2420 		return err;
2421 	}
2422 	btf_dump_data_pfx(d);
2423 
2424 	if (!d->typed_dump->skip_names) {
2425 		if (fname && strlen(fname) > 0)
2426 			btf_dump_printf(d, ".%s = ", fname);
2427 		btf_dump_emit_type_cast(d, id, true);
2428 	}
2429 
2430 	t = skip_mods_and_typedefs(d->btf, id, NULL);
2431 
2432 	switch (btf_kind(t)) {
2433 	case BTF_KIND_UNKN:
2434 	case BTF_KIND_FWD:
2435 	case BTF_KIND_FUNC:
2436 	case BTF_KIND_FUNC_PROTO:
2437 	case BTF_KIND_DECL_TAG:
2438 		err = btf_dump_unsupported_data(d, t, id);
2439 		break;
2440 	case BTF_KIND_INT:
2441 		if (bit_sz)
2442 			err = btf_dump_bitfield_data(d, t, data, bits_offset, bit_sz);
2443 		else
2444 			err = btf_dump_int_data(d, t, id, data, bits_offset);
2445 		break;
2446 	case BTF_KIND_FLOAT:
2447 		err = btf_dump_float_data(d, t, id, data);
2448 		break;
2449 	case BTF_KIND_PTR:
2450 		err = btf_dump_ptr_data(d, t, id, data);
2451 		break;
2452 	case BTF_KIND_ARRAY:
2453 		err = btf_dump_array_data(d, t, id, data);
2454 		break;
2455 	case BTF_KIND_STRUCT:
2456 	case BTF_KIND_UNION:
2457 		err = btf_dump_struct_data(d, t, id, data);
2458 		break;
2459 	case BTF_KIND_ENUM:
2460 	case BTF_KIND_ENUM64:
2461 		/* handle bitfield and int enum values */
2462 		if (bit_sz) {
2463 			__u64 print_num;
2464 			__s64 enum_val;
2465 
2466 			err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz,
2467 							  &print_num);
2468 			if (err)
2469 				break;
2470 			enum_val = (__s64)print_num;
2471 			err = btf_dump_enum_data(d, t, id, &enum_val);
2472 		} else
2473 			err = btf_dump_enum_data(d, t, id, data);
2474 		break;
2475 	case BTF_KIND_VAR:
2476 		err = btf_dump_var_data(d, t, id, data);
2477 		break;
2478 	case BTF_KIND_DATASEC:
2479 		err = btf_dump_datasec_data(d, t, id, data);
2480 		break;
2481 	default:
2482 		pr_warn("unexpected kind [%u] for id [%u]\n",
2483 			BTF_INFO_KIND(t->info), id);
2484 		return -EINVAL;
2485 	}
2486 	if (err < 0)
2487 		return err;
2488 	return size;
2489 }
2490 
2491 int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
2492 			     const void *data, size_t data_sz,
2493 			     const struct btf_dump_type_data_opts *opts)
2494 {
2495 	struct btf_dump_data typed_dump = {};
2496 	const struct btf_type *t;
2497 	int ret;
2498 
2499 	if (!OPTS_VALID(opts, btf_dump_type_data_opts))
2500 		return libbpf_err(-EINVAL);
2501 
2502 	t = btf__type_by_id(d->btf, id);
2503 	if (!t)
2504 		return libbpf_err(-ENOENT);
2505 
2506 	d->typed_dump = &typed_dump;
2507 	d->typed_dump->data_end = data + data_sz;
2508 	d->typed_dump->indent_lvl = OPTS_GET(opts, indent_level, 0);
2509 
2510 	/* default indent string is a tab */
2511 	if (!OPTS_GET(opts, indent_str, NULL))
2512 		d->typed_dump->indent_str[0] = '\t';
2513 	else
2514 		libbpf_strlcpy(d->typed_dump->indent_str, opts->indent_str,
2515 			       sizeof(d->typed_dump->indent_str));
2516 
2517 	d->typed_dump->compact = OPTS_GET(opts, compact, false);
2518 	d->typed_dump->skip_names = OPTS_GET(opts, skip_names, false);
2519 	d->typed_dump->emit_zeroes = OPTS_GET(opts, emit_zeroes, false);
2520 
2521 	ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);
2522 
2523 	d->typed_dump = NULL;
2524 
2525 	return libbpf_err(ret);
2526 }
2527