xref: /openbmc/linux/tools/lib/bpf/btf_dump.c (revision d5a9588c)
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 emit valid empty args (void) syntax for
1563 			 * such case. Similarly and conveniently, valid
1564 			 * no args case can be special-cased here as well.
1565 			 */
1566 			if (vlen == 0 || (vlen == 1 && p->type == 0)) {
1567 				btf_dump_printf(d, "void)");
1568 				return;
1569 			}
1570 
1571 			for (i = 0; i < vlen; i++, p++) {
1572 				if (i > 0)
1573 					btf_dump_printf(d, ", ");
1574 
1575 				/* last arg of type void is vararg */
1576 				if (i == vlen - 1 && p->type == 0) {
1577 					btf_dump_printf(d, "...");
1578 					break;
1579 				}
1580 
1581 				name = btf_name_of(d, p->name_off);
1582 				btf_dump_emit_type_decl(d, p->type, name, lvl);
1583 			}
1584 
1585 			btf_dump_printf(d, ")");
1586 			return;
1587 		}
1588 		default:
1589 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1590 				kind, id);
1591 			return;
1592 		}
1593 
1594 		last_was_ptr = kind == BTF_KIND_PTR;
1595 	}
1596 
1597 	btf_dump_emit_name(d, fname, last_was_ptr);
1598 }
1599 
1600 /* show type name as (type_name) */
1601 static void btf_dump_emit_type_cast(struct btf_dump *d, __u32 id,
1602 				    bool top_level)
1603 {
1604 	const struct btf_type *t;
1605 
1606 	/* for array members, we don't bother emitting type name for each
1607 	 * member to avoid the redundancy of
1608 	 * .name = (char[4])[(char)'f',(char)'o',(char)'o',]
1609 	 */
1610 	if (d->typed_dump->is_array_member)
1611 		return;
1612 
1613 	/* avoid type name specification for variable/section; it will be done
1614 	 * for the associated variable value(s).
1615 	 */
1616 	t = btf__type_by_id(d->btf, id);
1617 	if (btf_is_var(t) || btf_is_datasec(t))
1618 		return;
1619 
1620 	if (top_level)
1621 		btf_dump_printf(d, "(");
1622 
1623 	d->skip_anon_defs = true;
1624 	d->strip_mods = true;
1625 	btf_dump_emit_type_decl(d, id, "", 0);
1626 	d->strip_mods = false;
1627 	d->skip_anon_defs = false;
1628 
1629 	if (top_level)
1630 		btf_dump_printf(d, ")");
1631 }
1632 
1633 /* return number of duplicates (occurrences) of a given name */
1634 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
1635 				 const char *orig_name)
1636 {
1637 	char *old_name, *new_name;
1638 	size_t dup_cnt = 0;
1639 	int err;
1640 
1641 	new_name = strdup(orig_name);
1642 	if (!new_name)
1643 		return 1;
1644 
1645 	(void)hashmap__find(name_map, orig_name, &dup_cnt);
1646 	dup_cnt++;
1647 
1648 	err = hashmap__set(name_map, new_name, dup_cnt, &old_name, NULL);
1649 	if (err)
1650 		free(new_name);
1651 
1652 	free(old_name);
1653 
1654 	return dup_cnt;
1655 }
1656 
1657 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
1658 					 struct hashmap *name_map)
1659 {
1660 	struct btf_dump_type_aux_state *s = &d->type_states[id];
1661 	const struct btf_type *t = btf__type_by_id(d->btf, id);
1662 	const char *orig_name = btf_name_of(d, t->name_off);
1663 	const char **cached_name = &d->cached_names[id];
1664 	size_t dup_cnt;
1665 
1666 	if (t->name_off == 0)
1667 		return "";
1668 
1669 	if (s->name_resolved)
1670 		return *cached_name ? *cached_name : orig_name;
1671 
1672 	if (btf_is_fwd(t) || (btf_is_enum(t) && btf_vlen(t) == 0)) {
1673 		s->name_resolved = 1;
1674 		return orig_name;
1675 	}
1676 
1677 	dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
1678 	if (dup_cnt > 1) {
1679 		const size_t max_len = 256;
1680 		char new_name[max_len];
1681 
1682 		snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
1683 		*cached_name = strdup(new_name);
1684 	}
1685 
1686 	s->name_resolved = 1;
1687 	return *cached_name ? *cached_name : orig_name;
1688 }
1689 
1690 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id)
1691 {
1692 	return btf_dump_resolve_name(d, id, d->type_names);
1693 }
1694 
1695 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id)
1696 {
1697 	return btf_dump_resolve_name(d, id, d->ident_names);
1698 }
1699 
1700 static int btf_dump_dump_type_data(struct btf_dump *d,
1701 				   const char *fname,
1702 				   const struct btf_type *t,
1703 				   __u32 id,
1704 				   const void *data,
1705 				   __u8 bits_offset,
1706 				   __u8 bit_sz);
1707 
1708 static const char *btf_dump_data_newline(struct btf_dump *d)
1709 {
1710 	return d->typed_dump->compact || d->typed_dump->depth == 0 ? "" : "\n";
1711 }
1712 
1713 static const char *btf_dump_data_delim(struct btf_dump *d)
1714 {
1715 	return d->typed_dump->depth == 0 ? "" : ",";
1716 }
1717 
1718 static void btf_dump_data_pfx(struct btf_dump *d)
1719 {
1720 	int i, lvl = d->typed_dump->indent_lvl + d->typed_dump->depth;
1721 
1722 	if (d->typed_dump->compact)
1723 		return;
1724 
1725 	for (i = 0; i < lvl; i++)
1726 		btf_dump_printf(d, "%s", d->typed_dump->indent_str);
1727 }
1728 
1729 /* A macro is used here as btf_type_value[s]() appends format specifiers
1730  * to the format specifier passed in; these do the work of appending
1731  * delimiters etc while the caller simply has to specify the type values
1732  * in the format specifier + value(s).
1733  */
1734 #define btf_dump_type_values(d, fmt, ...)				\
1735 	btf_dump_printf(d, fmt "%s%s",					\
1736 			##__VA_ARGS__,					\
1737 			btf_dump_data_delim(d),				\
1738 			btf_dump_data_newline(d))
1739 
1740 static int btf_dump_unsupported_data(struct btf_dump *d,
1741 				     const struct btf_type *t,
1742 				     __u32 id)
1743 {
1744 	btf_dump_printf(d, "<unsupported kind:%u>", btf_kind(t));
1745 	return -ENOTSUP;
1746 }
1747 
1748 static int btf_dump_get_bitfield_value(struct btf_dump *d,
1749 				       const struct btf_type *t,
1750 				       const void *data,
1751 				       __u8 bits_offset,
1752 				       __u8 bit_sz,
1753 				       __u64 *value)
1754 {
1755 	__u16 left_shift_bits, right_shift_bits;
1756 	const __u8 *bytes = data;
1757 	__u8 nr_copy_bits;
1758 	__u64 num = 0;
1759 	int i;
1760 
1761 	/* Maximum supported bitfield size is 64 bits */
1762 	if (t->size > 8) {
1763 		pr_warn("unexpected bitfield size %d\n", t->size);
1764 		return -EINVAL;
1765 	}
1766 
1767 	/* Bitfield value retrieval is done in two steps; first relevant bytes are
1768 	 * stored in num, then we left/right shift num to eliminate irrelevant bits.
1769 	 */
1770 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1771 	for (i = t->size - 1; i >= 0; i--)
1772 		num = num * 256 + bytes[i];
1773 	nr_copy_bits = bit_sz + bits_offset;
1774 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1775 	for (i = 0; i < t->size; i++)
1776 		num = num * 256 + bytes[i];
1777 	nr_copy_bits = t->size * 8 - bits_offset;
1778 #else
1779 # error "Unrecognized __BYTE_ORDER__"
1780 #endif
1781 	left_shift_bits = 64 - nr_copy_bits;
1782 	right_shift_bits = 64 - bit_sz;
1783 
1784 	*value = (num << left_shift_bits) >> right_shift_bits;
1785 
1786 	return 0;
1787 }
1788 
1789 static int btf_dump_bitfield_check_zero(struct btf_dump *d,
1790 					const struct btf_type *t,
1791 					const void *data,
1792 					__u8 bits_offset,
1793 					__u8 bit_sz)
1794 {
1795 	__u64 check_num;
1796 	int err;
1797 
1798 	err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &check_num);
1799 	if (err)
1800 		return err;
1801 	if (check_num == 0)
1802 		return -ENODATA;
1803 	return 0;
1804 }
1805 
1806 static int btf_dump_bitfield_data(struct btf_dump *d,
1807 				  const struct btf_type *t,
1808 				  const void *data,
1809 				  __u8 bits_offset,
1810 				  __u8 bit_sz)
1811 {
1812 	__u64 print_num;
1813 	int err;
1814 
1815 	err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &print_num);
1816 	if (err)
1817 		return err;
1818 
1819 	btf_dump_type_values(d, "0x%llx", (unsigned long long)print_num);
1820 
1821 	return 0;
1822 }
1823 
1824 /* ints, floats and ptrs */
1825 static int btf_dump_base_type_check_zero(struct btf_dump *d,
1826 					 const struct btf_type *t,
1827 					 __u32 id,
1828 					 const void *data)
1829 {
1830 	static __u8 bytecmp[16] = {};
1831 	int nr_bytes;
1832 
1833 	/* For pointer types, pointer size is not defined on a per-type basis.
1834 	 * On dump creation however, we store the pointer size.
1835 	 */
1836 	if (btf_kind(t) == BTF_KIND_PTR)
1837 		nr_bytes = d->ptr_sz;
1838 	else
1839 		nr_bytes = t->size;
1840 
1841 	if (nr_bytes < 1 || nr_bytes > 16) {
1842 		pr_warn("unexpected size %d for id [%u]\n", nr_bytes, id);
1843 		return -EINVAL;
1844 	}
1845 
1846 	if (memcmp(data, bytecmp, nr_bytes) == 0)
1847 		return -ENODATA;
1848 	return 0;
1849 }
1850 
1851 static bool ptr_is_aligned(const struct btf *btf, __u32 type_id,
1852 			   const void *data)
1853 {
1854 	int alignment = btf__align_of(btf, type_id);
1855 
1856 	if (alignment == 0)
1857 		return false;
1858 
1859 	return ((uintptr_t)data) % alignment == 0;
1860 }
1861 
1862 static int btf_dump_int_data(struct btf_dump *d,
1863 			     const struct btf_type *t,
1864 			     __u32 type_id,
1865 			     const void *data,
1866 			     __u8 bits_offset)
1867 {
1868 	__u8 encoding = btf_int_encoding(t);
1869 	bool sign = encoding & BTF_INT_SIGNED;
1870 	char buf[16] __attribute__((aligned(16)));
1871 	int sz = t->size;
1872 
1873 	if (sz == 0 || sz > sizeof(buf)) {
1874 		pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1875 		return -EINVAL;
1876 	}
1877 
1878 	/* handle packed int data - accesses of integers not aligned on
1879 	 * int boundaries can cause problems on some platforms.
1880 	 */
1881 	if (!ptr_is_aligned(d->btf, type_id, data)) {
1882 		memcpy(buf, data, sz);
1883 		data = buf;
1884 	}
1885 
1886 	switch (sz) {
1887 	case 16: {
1888 		const __u64 *ints = data;
1889 		__u64 lsi, msi;
1890 
1891 		/* avoid use of __int128 as some 32-bit platforms do not
1892 		 * support it.
1893 		 */
1894 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1895 		lsi = ints[0];
1896 		msi = ints[1];
1897 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1898 		lsi = ints[1];
1899 		msi = ints[0];
1900 #else
1901 # error "Unrecognized __BYTE_ORDER__"
1902 #endif
1903 		if (msi == 0)
1904 			btf_dump_type_values(d, "0x%llx", (unsigned long long)lsi);
1905 		else
1906 			btf_dump_type_values(d, "0x%llx%016llx", (unsigned long long)msi,
1907 					     (unsigned long long)lsi);
1908 		break;
1909 	}
1910 	case 8:
1911 		if (sign)
1912 			btf_dump_type_values(d, "%lld", *(long long *)data);
1913 		else
1914 			btf_dump_type_values(d, "%llu", *(unsigned long long *)data);
1915 		break;
1916 	case 4:
1917 		if (sign)
1918 			btf_dump_type_values(d, "%d", *(__s32 *)data);
1919 		else
1920 			btf_dump_type_values(d, "%u", *(__u32 *)data);
1921 		break;
1922 	case 2:
1923 		if (sign)
1924 			btf_dump_type_values(d, "%d", *(__s16 *)data);
1925 		else
1926 			btf_dump_type_values(d, "%u", *(__u16 *)data);
1927 		break;
1928 	case 1:
1929 		if (d->typed_dump->is_array_char) {
1930 			/* check for null terminator */
1931 			if (d->typed_dump->is_array_terminated)
1932 				break;
1933 			if (*(char *)data == '\0') {
1934 				d->typed_dump->is_array_terminated = true;
1935 				break;
1936 			}
1937 			if (isprint(*(char *)data)) {
1938 				btf_dump_type_values(d, "'%c'", *(char *)data);
1939 				break;
1940 			}
1941 		}
1942 		if (sign)
1943 			btf_dump_type_values(d, "%d", *(__s8 *)data);
1944 		else
1945 			btf_dump_type_values(d, "%u", *(__u8 *)data);
1946 		break;
1947 	default:
1948 		pr_warn("unexpected sz %d for id [%u]\n", sz, type_id);
1949 		return -EINVAL;
1950 	}
1951 	return 0;
1952 }
1953 
1954 union float_data {
1955 	long double ld;
1956 	double d;
1957 	float f;
1958 };
1959 
1960 static int btf_dump_float_data(struct btf_dump *d,
1961 			       const struct btf_type *t,
1962 			       __u32 type_id,
1963 			       const void *data)
1964 {
1965 	const union float_data *flp = data;
1966 	union float_data fl;
1967 	int sz = t->size;
1968 
1969 	/* handle unaligned data; copy to local union */
1970 	if (!ptr_is_aligned(d->btf, type_id, data)) {
1971 		memcpy(&fl, data, sz);
1972 		flp = &fl;
1973 	}
1974 
1975 	switch (sz) {
1976 	case 16:
1977 		btf_dump_type_values(d, "%Lf", flp->ld);
1978 		break;
1979 	case 8:
1980 		btf_dump_type_values(d, "%lf", flp->d);
1981 		break;
1982 	case 4:
1983 		btf_dump_type_values(d, "%f", flp->f);
1984 		break;
1985 	default:
1986 		pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1987 		return -EINVAL;
1988 	}
1989 	return 0;
1990 }
1991 
1992 static int btf_dump_var_data(struct btf_dump *d,
1993 			     const struct btf_type *v,
1994 			     __u32 id,
1995 			     const void *data)
1996 {
1997 	enum btf_func_linkage linkage = btf_var(v)->linkage;
1998 	const struct btf_type *t;
1999 	const char *l;
2000 	__u32 type_id;
2001 
2002 	switch (linkage) {
2003 	case BTF_FUNC_STATIC:
2004 		l = "static ";
2005 		break;
2006 	case BTF_FUNC_EXTERN:
2007 		l = "extern ";
2008 		break;
2009 	case BTF_FUNC_GLOBAL:
2010 	default:
2011 		l = "";
2012 		break;
2013 	}
2014 
2015 	/* format of output here is [linkage] [type] [varname] = (type)value,
2016 	 * for example "static int cpu_profile_flip = (int)1"
2017 	 */
2018 	btf_dump_printf(d, "%s", l);
2019 	type_id = v->type;
2020 	t = btf__type_by_id(d->btf, type_id);
2021 	btf_dump_emit_type_cast(d, type_id, false);
2022 	btf_dump_printf(d, " %s = ", btf_name_of(d, v->name_off));
2023 	return btf_dump_dump_type_data(d, NULL, t, type_id, data, 0, 0);
2024 }
2025 
2026 static int btf_dump_array_data(struct btf_dump *d,
2027 			       const struct btf_type *t,
2028 			       __u32 id,
2029 			       const void *data)
2030 {
2031 	const struct btf_array *array = btf_array(t);
2032 	const struct btf_type *elem_type;
2033 	__u32 i, elem_type_id;
2034 	__s64 elem_size;
2035 	bool is_array_member;
2036 
2037 	elem_type_id = array->type;
2038 	elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2039 	elem_size = btf__resolve_size(d->btf, elem_type_id);
2040 	if (elem_size <= 0) {
2041 		pr_warn("unexpected elem size %zd for array type [%u]\n",
2042 			(ssize_t)elem_size, id);
2043 		return -EINVAL;
2044 	}
2045 
2046 	if (btf_is_int(elem_type)) {
2047 		/*
2048 		 * BTF_INT_CHAR encoding never seems to be set for
2049 		 * char arrays, so if size is 1 and element is
2050 		 * printable as a char, we'll do that.
2051 		 */
2052 		if (elem_size == 1)
2053 			d->typed_dump->is_array_char = true;
2054 	}
2055 
2056 	/* note that we increment depth before calling btf_dump_print() below;
2057 	 * this is intentional.  btf_dump_data_newline() will not print a
2058 	 * newline for depth 0 (since this leaves us with trailing newlines
2059 	 * at the end of typed display), so depth is incremented first.
2060 	 * For similar reasons, we decrement depth before showing the closing
2061 	 * parenthesis.
2062 	 */
2063 	d->typed_dump->depth++;
2064 	btf_dump_printf(d, "[%s", btf_dump_data_newline(d));
2065 
2066 	/* may be a multidimensional array, so store current "is array member"
2067 	 * status so we can restore it correctly later.
2068 	 */
2069 	is_array_member = d->typed_dump->is_array_member;
2070 	d->typed_dump->is_array_member = true;
2071 	for (i = 0; i < array->nelems; i++, data += elem_size) {
2072 		if (d->typed_dump->is_array_terminated)
2073 			break;
2074 		btf_dump_dump_type_data(d, NULL, elem_type, elem_type_id, data, 0, 0);
2075 	}
2076 	d->typed_dump->is_array_member = is_array_member;
2077 	d->typed_dump->depth--;
2078 	btf_dump_data_pfx(d);
2079 	btf_dump_type_values(d, "]");
2080 
2081 	return 0;
2082 }
2083 
2084 static int btf_dump_struct_data(struct btf_dump *d,
2085 				const struct btf_type *t,
2086 				__u32 id,
2087 				const void *data)
2088 {
2089 	const struct btf_member *m = btf_members(t);
2090 	__u16 n = btf_vlen(t);
2091 	int i, err = 0;
2092 
2093 	/* note that we increment depth before calling btf_dump_print() below;
2094 	 * this is intentional.  btf_dump_data_newline() will not print a
2095 	 * newline for depth 0 (since this leaves us with trailing newlines
2096 	 * at the end of typed display), so depth is incremented first.
2097 	 * For similar reasons, we decrement depth before showing the closing
2098 	 * parenthesis.
2099 	 */
2100 	d->typed_dump->depth++;
2101 	btf_dump_printf(d, "{%s", btf_dump_data_newline(d));
2102 
2103 	for (i = 0; i < n; i++, m++) {
2104 		const struct btf_type *mtype;
2105 		const char *mname;
2106 		__u32 moffset;
2107 		__u8 bit_sz;
2108 
2109 		mtype = btf__type_by_id(d->btf, m->type);
2110 		mname = btf_name_of(d, m->name_off);
2111 		moffset = btf_member_bit_offset(t, i);
2112 
2113 		bit_sz = btf_member_bitfield_size(t, i);
2114 		err = btf_dump_dump_type_data(d, mname, mtype, m->type, data + moffset / 8,
2115 					      moffset % 8, bit_sz);
2116 		if (err < 0)
2117 			return err;
2118 	}
2119 	d->typed_dump->depth--;
2120 	btf_dump_data_pfx(d);
2121 	btf_dump_type_values(d, "}");
2122 	return err;
2123 }
2124 
2125 union ptr_data {
2126 	unsigned int p;
2127 	unsigned long long lp;
2128 };
2129 
2130 static int btf_dump_ptr_data(struct btf_dump *d,
2131 			      const struct btf_type *t,
2132 			      __u32 id,
2133 			      const void *data)
2134 {
2135 	if (ptr_is_aligned(d->btf, id, data) && d->ptr_sz == sizeof(void *)) {
2136 		btf_dump_type_values(d, "%p", *(void **)data);
2137 	} else {
2138 		union ptr_data pt;
2139 
2140 		memcpy(&pt, data, d->ptr_sz);
2141 		if (d->ptr_sz == 4)
2142 			btf_dump_type_values(d, "0x%x", pt.p);
2143 		else
2144 			btf_dump_type_values(d, "0x%llx", pt.lp);
2145 	}
2146 	return 0;
2147 }
2148 
2149 static int btf_dump_get_enum_value(struct btf_dump *d,
2150 				   const struct btf_type *t,
2151 				   const void *data,
2152 				   __u32 id,
2153 				   __s64 *value)
2154 {
2155 	bool is_signed = btf_kflag(t);
2156 
2157 	if (!ptr_is_aligned(d->btf, id, data)) {
2158 		__u64 val;
2159 		int err;
2160 
2161 		err = btf_dump_get_bitfield_value(d, t, data, 0, 0, &val);
2162 		if (err)
2163 			return err;
2164 		*value = (__s64)val;
2165 		return 0;
2166 	}
2167 
2168 	switch (t->size) {
2169 	case 8:
2170 		*value = *(__s64 *)data;
2171 		return 0;
2172 	case 4:
2173 		*value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;
2174 		return 0;
2175 	case 2:
2176 		*value = is_signed ? *(__s16 *)data : *(__u16 *)data;
2177 		return 0;
2178 	case 1:
2179 		*value = is_signed ? *(__s8 *)data : *(__u8 *)data;
2180 		return 0;
2181 	default:
2182 		pr_warn("unexpected size %d for enum, id:[%u]\n", t->size, id);
2183 		return -EINVAL;
2184 	}
2185 }
2186 
2187 static int btf_dump_enum_data(struct btf_dump *d,
2188 			      const struct btf_type *t,
2189 			      __u32 id,
2190 			      const void *data)
2191 {
2192 	bool is_signed;
2193 	__s64 value;
2194 	int i, err;
2195 
2196 	err = btf_dump_get_enum_value(d, t, data, id, &value);
2197 	if (err)
2198 		return err;
2199 
2200 	is_signed = btf_kflag(t);
2201 	if (btf_is_enum(t)) {
2202 		const struct btf_enum *e;
2203 
2204 		for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) {
2205 			if (value != e->val)
2206 				continue;
2207 			btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2208 			return 0;
2209 		}
2210 
2211 		btf_dump_type_values(d, is_signed ? "%d" : "%u", value);
2212 	} else {
2213 		const struct btf_enum64 *e;
2214 
2215 		for (i = 0, e = btf_enum64(t); i < btf_vlen(t); i++, e++) {
2216 			if (value != btf_enum64_value(e))
2217 				continue;
2218 			btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2219 			return 0;
2220 		}
2221 
2222 		btf_dump_type_values(d, is_signed ? "%lldLL" : "%lluULL",
2223 				     (unsigned long long)value);
2224 	}
2225 	return 0;
2226 }
2227 
2228 static int btf_dump_datasec_data(struct btf_dump *d,
2229 				 const struct btf_type *t,
2230 				 __u32 id,
2231 				 const void *data)
2232 {
2233 	const struct btf_var_secinfo *vsi;
2234 	const struct btf_type *var;
2235 	__u32 i;
2236 	int err;
2237 
2238 	btf_dump_type_values(d, "SEC(\"%s\") ", btf_name_of(d, t->name_off));
2239 
2240 	for (i = 0, vsi = btf_var_secinfos(t); i < btf_vlen(t); i++, vsi++) {
2241 		var = btf__type_by_id(d->btf, vsi->type);
2242 		err = btf_dump_dump_type_data(d, NULL, var, vsi->type, data + vsi->offset, 0, 0);
2243 		if (err < 0)
2244 			return err;
2245 		btf_dump_printf(d, ";");
2246 	}
2247 	return 0;
2248 }
2249 
2250 /* return size of type, or if base type overflows, return -E2BIG. */
2251 static int btf_dump_type_data_check_overflow(struct btf_dump *d,
2252 					     const struct btf_type *t,
2253 					     __u32 id,
2254 					     const void *data,
2255 					     __u8 bits_offset,
2256 					     __u8 bit_sz)
2257 {
2258 	__s64 size;
2259 
2260 	if (bit_sz) {
2261 		/* bits_offset is at most 7. bit_sz is at most 128. */
2262 		__u8 nr_bytes = (bits_offset + bit_sz + 7) / 8;
2263 
2264 		/* When bit_sz is non zero, it is called from
2265 		 * btf_dump_struct_data() where it only cares about
2266 		 * negative error value.
2267 		 * Return nr_bytes in success case to make it
2268 		 * consistent as the regular integer case below.
2269 		 */
2270 		return data + nr_bytes > d->typed_dump->data_end ? -E2BIG : nr_bytes;
2271 	}
2272 
2273 	size = btf__resolve_size(d->btf, id);
2274 
2275 	if (size < 0 || size >= INT_MAX) {
2276 		pr_warn("unexpected size [%zu] for id [%u]\n",
2277 			(size_t)size, id);
2278 		return -EINVAL;
2279 	}
2280 
2281 	/* Only do overflow checking for base types; we do not want to
2282 	 * avoid showing part of a struct, union or array, even if we
2283 	 * do not have enough data to show the full object.  By
2284 	 * restricting overflow checking to base types we can ensure
2285 	 * that partial display succeeds, while avoiding overflowing
2286 	 * and using bogus data for display.
2287 	 */
2288 	t = skip_mods_and_typedefs(d->btf, id, NULL);
2289 	if (!t) {
2290 		pr_warn("unexpected error skipping mods/typedefs for id [%u]\n",
2291 			id);
2292 		return -EINVAL;
2293 	}
2294 
2295 	switch (btf_kind(t)) {
2296 	case BTF_KIND_INT:
2297 	case BTF_KIND_FLOAT:
2298 	case BTF_KIND_PTR:
2299 	case BTF_KIND_ENUM:
2300 	case BTF_KIND_ENUM64:
2301 		if (data + bits_offset / 8 + size > d->typed_dump->data_end)
2302 			return -E2BIG;
2303 		break;
2304 	default:
2305 		break;
2306 	}
2307 	return (int)size;
2308 }
2309 
2310 static int btf_dump_type_data_check_zero(struct btf_dump *d,
2311 					 const struct btf_type *t,
2312 					 __u32 id,
2313 					 const void *data,
2314 					 __u8 bits_offset,
2315 					 __u8 bit_sz)
2316 {
2317 	__s64 value;
2318 	int i, err;
2319 
2320 	/* toplevel exceptions; we show zero values if
2321 	 * - we ask for them (emit_zeros)
2322 	 * - if we are at top-level so we see "struct empty { }"
2323 	 * - or if we are an array member and the array is non-empty and
2324 	 *   not a char array; we don't want to be in a situation where we
2325 	 *   have an integer array 0, 1, 0, 1 and only show non-zero values.
2326 	 *   If the array contains zeroes only, or is a char array starting
2327 	 *   with a '\0', the array-level check_zero() will prevent showing it;
2328 	 *   we are concerned with determining zero value at the array member
2329 	 *   level here.
2330 	 */
2331 	if (d->typed_dump->emit_zeroes || d->typed_dump->depth == 0 ||
2332 	    (d->typed_dump->is_array_member &&
2333 	     !d->typed_dump->is_array_char))
2334 		return 0;
2335 
2336 	t = skip_mods_and_typedefs(d->btf, id, NULL);
2337 
2338 	switch (btf_kind(t)) {
2339 	case BTF_KIND_INT:
2340 		if (bit_sz)
2341 			return btf_dump_bitfield_check_zero(d, t, data, bits_offset, bit_sz);
2342 		return btf_dump_base_type_check_zero(d, t, id, data);
2343 	case BTF_KIND_FLOAT:
2344 	case BTF_KIND_PTR:
2345 		return btf_dump_base_type_check_zero(d, t, id, data);
2346 	case BTF_KIND_ARRAY: {
2347 		const struct btf_array *array = btf_array(t);
2348 		const struct btf_type *elem_type;
2349 		__u32 elem_type_id, elem_size;
2350 		bool ischar;
2351 
2352 		elem_type_id = array->type;
2353 		elem_size = btf__resolve_size(d->btf, elem_type_id);
2354 		elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2355 
2356 		ischar = btf_is_int(elem_type) && elem_size == 1;
2357 
2358 		/* check all elements; if _any_ element is nonzero, all
2359 		 * of array is displayed.  We make an exception however
2360 		 * for char arrays where the first element is 0; these
2361 		 * are considered zeroed also, even if later elements are
2362 		 * non-zero because the string is terminated.
2363 		 */
2364 		for (i = 0; i < array->nelems; i++) {
2365 			if (i == 0 && ischar && *(char *)data == 0)
2366 				return -ENODATA;
2367 			err = btf_dump_type_data_check_zero(d, elem_type,
2368 							    elem_type_id,
2369 							    data +
2370 							    (i * elem_size),
2371 							    bits_offset, 0);
2372 			if (err != -ENODATA)
2373 				return err;
2374 		}
2375 		return -ENODATA;
2376 	}
2377 	case BTF_KIND_STRUCT:
2378 	case BTF_KIND_UNION: {
2379 		const struct btf_member *m = btf_members(t);
2380 		__u16 n = btf_vlen(t);
2381 
2382 		/* if any struct/union member is non-zero, the struct/union
2383 		 * is considered non-zero and dumped.
2384 		 */
2385 		for (i = 0; i < n; i++, m++) {
2386 			const struct btf_type *mtype;
2387 			__u32 moffset;
2388 
2389 			mtype = btf__type_by_id(d->btf, m->type);
2390 			moffset = btf_member_bit_offset(t, i);
2391 
2392 			/* btf_int_bits() does not store member bitfield size;
2393 			 * bitfield size needs to be stored here so int display
2394 			 * of member can retrieve it.
2395 			 */
2396 			bit_sz = btf_member_bitfield_size(t, i);
2397 			err = btf_dump_type_data_check_zero(d, mtype, m->type, data + moffset / 8,
2398 							    moffset % 8, bit_sz);
2399 			if (err != ENODATA)
2400 				return err;
2401 		}
2402 		return -ENODATA;
2403 	}
2404 	case BTF_KIND_ENUM:
2405 	case BTF_KIND_ENUM64:
2406 		err = btf_dump_get_enum_value(d, t, data, id, &value);
2407 		if (err)
2408 			return err;
2409 		if (value == 0)
2410 			return -ENODATA;
2411 		return 0;
2412 	default:
2413 		return 0;
2414 	}
2415 }
2416 
2417 /* returns size of data dumped, or error. */
2418 static int btf_dump_dump_type_data(struct btf_dump *d,
2419 				   const char *fname,
2420 				   const struct btf_type *t,
2421 				   __u32 id,
2422 				   const void *data,
2423 				   __u8 bits_offset,
2424 				   __u8 bit_sz)
2425 {
2426 	int size, err = 0;
2427 
2428 	size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset, bit_sz);
2429 	if (size < 0)
2430 		return size;
2431 	err = btf_dump_type_data_check_zero(d, t, id, data, bits_offset, bit_sz);
2432 	if (err) {
2433 		/* zeroed data is expected and not an error, so simply skip
2434 		 * dumping such data.  Record other errors however.
2435 		 */
2436 		if (err == -ENODATA)
2437 			return size;
2438 		return err;
2439 	}
2440 	btf_dump_data_pfx(d);
2441 
2442 	if (!d->typed_dump->skip_names) {
2443 		if (fname && strlen(fname) > 0)
2444 			btf_dump_printf(d, ".%s = ", fname);
2445 		btf_dump_emit_type_cast(d, id, true);
2446 	}
2447 
2448 	t = skip_mods_and_typedefs(d->btf, id, NULL);
2449 
2450 	switch (btf_kind(t)) {
2451 	case BTF_KIND_UNKN:
2452 	case BTF_KIND_FWD:
2453 	case BTF_KIND_FUNC:
2454 	case BTF_KIND_FUNC_PROTO:
2455 	case BTF_KIND_DECL_TAG:
2456 		err = btf_dump_unsupported_data(d, t, id);
2457 		break;
2458 	case BTF_KIND_INT:
2459 		if (bit_sz)
2460 			err = btf_dump_bitfield_data(d, t, data, bits_offset, bit_sz);
2461 		else
2462 			err = btf_dump_int_data(d, t, id, data, bits_offset);
2463 		break;
2464 	case BTF_KIND_FLOAT:
2465 		err = btf_dump_float_data(d, t, id, data);
2466 		break;
2467 	case BTF_KIND_PTR:
2468 		err = btf_dump_ptr_data(d, t, id, data);
2469 		break;
2470 	case BTF_KIND_ARRAY:
2471 		err = btf_dump_array_data(d, t, id, data);
2472 		break;
2473 	case BTF_KIND_STRUCT:
2474 	case BTF_KIND_UNION:
2475 		err = btf_dump_struct_data(d, t, id, data);
2476 		break;
2477 	case BTF_KIND_ENUM:
2478 	case BTF_KIND_ENUM64:
2479 		/* handle bitfield and int enum values */
2480 		if (bit_sz) {
2481 			__u64 print_num;
2482 			__s64 enum_val;
2483 
2484 			err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz,
2485 							  &print_num);
2486 			if (err)
2487 				break;
2488 			enum_val = (__s64)print_num;
2489 			err = btf_dump_enum_data(d, t, id, &enum_val);
2490 		} else
2491 			err = btf_dump_enum_data(d, t, id, data);
2492 		break;
2493 	case BTF_KIND_VAR:
2494 		err = btf_dump_var_data(d, t, id, data);
2495 		break;
2496 	case BTF_KIND_DATASEC:
2497 		err = btf_dump_datasec_data(d, t, id, data);
2498 		break;
2499 	default:
2500 		pr_warn("unexpected kind [%u] for id [%u]\n",
2501 			BTF_INFO_KIND(t->info), id);
2502 		return -EINVAL;
2503 	}
2504 	if (err < 0)
2505 		return err;
2506 	return size;
2507 }
2508 
2509 int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
2510 			     const void *data, size_t data_sz,
2511 			     const struct btf_dump_type_data_opts *opts)
2512 {
2513 	struct btf_dump_data typed_dump = {};
2514 	const struct btf_type *t;
2515 	int ret;
2516 
2517 	if (!OPTS_VALID(opts, btf_dump_type_data_opts))
2518 		return libbpf_err(-EINVAL);
2519 
2520 	t = btf__type_by_id(d->btf, id);
2521 	if (!t)
2522 		return libbpf_err(-ENOENT);
2523 
2524 	d->typed_dump = &typed_dump;
2525 	d->typed_dump->data_end = data + data_sz;
2526 	d->typed_dump->indent_lvl = OPTS_GET(opts, indent_level, 0);
2527 
2528 	/* default indent string is a tab */
2529 	if (!OPTS_GET(opts, indent_str, NULL))
2530 		d->typed_dump->indent_str[0] = '\t';
2531 	else
2532 		libbpf_strlcpy(d->typed_dump->indent_str, opts->indent_str,
2533 			       sizeof(d->typed_dump->indent_str));
2534 
2535 	d->typed_dump->compact = OPTS_GET(opts, compact, false);
2536 	d->typed_dump->skip_names = OPTS_GET(opts, skip_names, false);
2537 	d->typed_dump->emit_zeroes = OPTS_GET(opts, emit_zeroes, false);
2538 
2539 	ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);
2540 
2541 	d->typed_dump = NULL;
2542 
2543 	return libbpf_err(ret);
2544 }
2545