• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
pfx(int lvl)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 
str_hash_fn(long key,void * ctx)121 static size_t str_hash_fn(long key, void *ctx)
122 {
123 	return str_hash((void *)key);
124 }
125 
str_equal_fn(long a,long b,void * ctx)126 static bool str_equal_fn(long a, long b, void *ctx)
127 {
128 	return strcmp((void *)a, (void *)b) == 0;
129 }
130 
btf_name_of(const struct btf_dump * d,__u32 name_off)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 
btf_dump_printf(const struct btf_dump * d,const char * fmt,...)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 
btf_dump__new(const struct btf * btf,btf_dump_printf_fn_t printf_fn,void * ctx,const struct btf_dump_opts * opts)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 
btf_dump_resize(struct btf_dump * d)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 
btf_dump_free_names(struct hashmap * map)223 static void btf_dump_free_names(struct hashmap *map)
224 {
225 	size_t bkt;
226 	struct hashmap_entry *cur;
227 	if (!map)
228 		return;
229 
230 	hashmap__for_each_entry(map, cur, bkt)
231 		free((void *)cur->pkey);
232 
233 	hashmap__free(map);
234 }
235 
btf_dump__free(struct btf_dump * d)236 void btf_dump__free(struct btf_dump *d)
237 {
238 	int i;
239 
240 	if (IS_ERR_OR_NULL(d))
241 		return;
242 
243 	free(d->type_states);
244 	if (d->cached_names) {
245 		/* any set cached name is owned by us and should be freed */
246 		for (i = 0; i <= d->last_id; i++) {
247 			if (d->cached_names[i])
248 				free((void *)d->cached_names[i]);
249 		}
250 	}
251 	free(d->cached_names);
252 	free(d->emit_queue);
253 	free(d->decl_stack);
254 	btf_dump_free_names(d->type_names);
255 	btf_dump_free_names(d->ident_names);
256 
257 	free(d);
258 }
259 
260 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr);
261 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id);
262 
263 /*
264  * Dump BTF type in a compilable C syntax, including all the necessary
265  * dependent types, necessary for compilation. If some of the dependent types
266  * were already emitted as part of previous btf_dump__dump_type() invocation
267  * for another type, they won't be emitted again. This API allows callers to
268  * filter out BTF types according to user-defined criterias and emitted only
269  * minimal subset of types, necessary to compile everything. Full struct/union
270  * definitions will still be emitted, even if the only usage is through
271  * pointer and could be satisfied with just a forward declaration.
272  *
273  * Dumping is done in two high-level passes:
274  *   1. Topologically sort type definitions to satisfy C rules of compilation.
275  *   2. Emit type definitions in C syntax.
276  *
277  * Returns 0 on success; <0, otherwise.
278  */
btf_dump__dump_type(struct btf_dump * d,__u32 id)279 int btf_dump__dump_type(struct btf_dump *d, __u32 id)
280 {
281 	int err, i;
282 
283 	if (id >= btf__type_cnt(d->btf))
284 		return libbpf_err(-EINVAL);
285 
286 	err = btf_dump_resize(d);
287 	if (err)
288 		return libbpf_err(err);
289 
290 	d->emit_queue_cnt = 0;
291 	err = btf_dump_order_type(d, id, false);
292 	if (err < 0)
293 		return libbpf_err(err);
294 
295 	for (i = 0; i < d->emit_queue_cnt; i++)
296 		btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
297 
298 	return 0;
299 }
300 
301 /*
302  * Mark all types that are referenced from any other type. This is used to
303  * determine top-level anonymous enums that need to be emitted as an
304  * independent type declarations.
305  * Anonymous enums come in two flavors: either embedded in a struct's field
306  * definition, in which case they have to be declared inline as part of field
307  * type declaration; or as a top-level anonymous enum, typically used for
308  * declaring global constants. It's impossible to distinguish between two
309  * without knowning whether given enum type was referenced from other type:
310  * top-level anonymous enum won't be referenced by anything, while embedded
311  * one will.
312  */
btf_dump_mark_referenced(struct btf_dump * d)313 static int btf_dump_mark_referenced(struct btf_dump *d)
314 {
315 	int i, j, n = btf__type_cnt(d->btf);
316 	const struct btf_type *t;
317 	__u16 vlen;
318 
319 	for (i = d->last_id + 1; i < n; i++) {
320 		t = btf__type_by_id(d->btf, i);
321 		vlen = btf_vlen(t);
322 
323 		switch (btf_kind(t)) {
324 		case BTF_KIND_INT:
325 		case BTF_KIND_ENUM:
326 		case BTF_KIND_ENUM64:
327 		case BTF_KIND_FWD:
328 		case BTF_KIND_FLOAT:
329 			break;
330 
331 		case BTF_KIND_VOLATILE:
332 		case BTF_KIND_CONST:
333 		case BTF_KIND_RESTRICT:
334 		case BTF_KIND_PTR:
335 		case BTF_KIND_TYPEDEF:
336 		case BTF_KIND_FUNC:
337 		case BTF_KIND_VAR:
338 		case BTF_KIND_DECL_TAG:
339 		case BTF_KIND_TYPE_TAG:
340 			d->type_states[t->type].referenced = 1;
341 			break;
342 
343 		case BTF_KIND_ARRAY: {
344 			const struct btf_array *a = btf_array(t);
345 
346 			d->type_states[a->index_type].referenced = 1;
347 			d->type_states[a->type].referenced = 1;
348 			break;
349 		}
350 		case BTF_KIND_STRUCT:
351 		case BTF_KIND_UNION: {
352 			const struct btf_member *m = btf_members(t);
353 
354 			for (j = 0; j < vlen; j++, m++)
355 				d->type_states[m->type].referenced = 1;
356 			break;
357 		}
358 		case BTF_KIND_FUNC_PROTO: {
359 			const struct btf_param *p = btf_params(t);
360 
361 			for (j = 0; j < vlen; j++, p++)
362 				d->type_states[p->type].referenced = 1;
363 			break;
364 		}
365 		case BTF_KIND_DATASEC: {
366 			const struct btf_var_secinfo *v = btf_var_secinfos(t);
367 
368 			for (j = 0; j < vlen; j++, v++)
369 				d->type_states[v->type].referenced = 1;
370 			break;
371 		}
372 		default:
373 			return -EINVAL;
374 		}
375 	}
376 	return 0;
377 }
378 
btf_dump_add_emit_queue_id(struct btf_dump * d,__u32 id)379 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
380 {
381 	__u32 *new_queue;
382 	size_t new_cap;
383 
384 	if (d->emit_queue_cnt >= d->emit_queue_cap) {
385 		new_cap = max(16, d->emit_queue_cap * 3 / 2);
386 		new_queue = libbpf_reallocarray(d->emit_queue, new_cap, sizeof(new_queue[0]));
387 		if (!new_queue)
388 			return -ENOMEM;
389 		d->emit_queue = new_queue;
390 		d->emit_queue_cap = new_cap;
391 	}
392 
393 	d->emit_queue[d->emit_queue_cnt++] = id;
394 	return 0;
395 }
396 
397 /*
398  * Determine order of emitting dependent types and specified type to satisfy
399  * C compilation rules.  This is done through topological sorting with an
400  * additional complication which comes from C rules. The main idea for C is
401  * that if some type is "embedded" into a struct/union, it's size needs to be
402  * known at the time of definition of containing type. E.g., for:
403  *
404  *	struct A {};
405  *	struct B { struct A x; }
406  *
407  * struct A *HAS* to be defined before struct B, because it's "embedded",
408  * i.e., it is part of struct B layout. But in the following case:
409  *
410  *	struct A;
411  *	struct B { struct A *x; }
412  *	struct A {};
413  *
414  * it's enough to just have a forward declaration of struct A at the time of
415  * struct B definition, as struct B has a pointer to struct A, so the size of
416  * field x is known without knowing struct A size: it's sizeof(void *).
417  *
418  * Unfortunately, there are some trickier cases we need to handle, e.g.:
419  *
420  *	struct A {}; // if this was forward-declaration: compilation error
421  *	struct B {
422  *		struct { // anonymous struct
423  *			struct A y;
424  *		} *x;
425  *	};
426  *
427  * In this case, struct B's field x is a pointer, so it's size is known
428  * regardless of the size of (anonymous) struct it points to. But because this
429  * struct is anonymous and thus defined inline inside struct B, *and* it
430  * embeds struct A, compiler requires full definition of struct A to be known
431  * before struct B can be defined. This creates a transitive dependency
432  * between struct A and struct B. If struct A was forward-declared before
433  * struct B definition and fully defined after struct B definition, that would
434  * trigger compilation error.
435  *
436  * All this means that while we are doing topological sorting on BTF type
437  * graph, we need to determine relationships between different types (graph
438  * nodes):
439  *   - weak link (relationship) between X and Y, if Y *CAN* be
440  *   forward-declared at the point of X definition;
441  *   - strong link, if Y *HAS* to be fully-defined before X can be defined.
442  *
443  * The rule is as follows. Given a chain of BTF types from X to Y, if there is
444  * BTF_KIND_PTR type in the chain and at least one non-anonymous type
445  * Z (excluding X, including Y), then link is weak. Otherwise, it's strong.
446  * Weak/strong relationship is determined recursively during DFS traversal and
447  * is returned as a result from btf_dump_order_type().
448  *
449  * btf_dump_order_type() is trying to avoid unnecessary forward declarations,
450  * but it is not guaranteeing that no extraneous forward declarations will be
451  * emitted.
452  *
453  * To avoid extra work, algorithm marks some of BTF types as ORDERED, when
454  * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT,
455  * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the
456  * entire graph path, so depending where from one came to that BTF type, it
457  * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM,
458  * once they are processed, there is no need to do it again, so they are
459  * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces
460  * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But
461  * in any case, once those are processed, no need to do it again, as the
462  * result won't change.
463  *
464  * Returns:
465  *   - 1, if type is part of strong link (so there is strong topological
466  *   ordering requirements);
467  *   - 0, if type is part of weak link (so can be satisfied through forward
468  *   declaration);
469  *   - <0, on error (e.g., unsatisfiable type loop detected).
470  */
btf_dump_order_type(struct btf_dump * d,__u32 id,bool through_ptr)471 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
472 {
473 	/*
474 	 * Order state is used to detect strong link cycles, but only for BTF
475 	 * kinds that are or could be an independent definition (i.e.,
476 	 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays,
477 	 * func_protos, modifiers are just means to get to these definitions.
478 	 * Int/void don't need definitions, they are assumed to be always
479 	 * properly defined.  We also ignore datasec, var, and funcs for now.
480 	 * So for all non-defining kinds, we never even set ordering state,
481 	 * for defining kinds we set ORDERING and subsequently ORDERED if it
482 	 * forms a strong link.
483 	 */
484 	struct btf_dump_type_aux_state *tstate = &d->type_states[id];
485 	const struct btf_type *t;
486 	__u16 vlen;
487 	int err, i;
488 
489 	/* return true, letting typedefs know that it's ok to be emitted */
490 	if (tstate->order_state == ORDERED)
491 		return 1;
492 
493 	t = btf__type_by_id(d->btf, id);
494 
495 	if (tstate->order_state == ORDERING) {
496 		/* type loop, but resolvable through fwd declaration */
497 		if (btf_is_composite(t) && through_ptr && t->name_off != 0)
498 			return 0;
499 		pr_warn("unsatisfiable type cycle, id:[%u]\n", id);
500 		return -ELOOP;
501 	}
502 
503 	switch (btf_kind(t)) {
504 	case BTF_KIND_INT:
505 	case BTF_KIND_FLOAT:
506 		tstate->order_state = ORDERED;
507 		return 0;
508 
509 	case BTF_KIND_PTR:
510 		err = btf_dump_order_type(d, t->type, true);
511 		tstate->order_state = ORDERED;
512 		return err;
513 
514 	case BTF_KIND_ARRAY:
515 		return btf_dump_order_type(d, btf_array(t)->type, false);
516 
517 	case BTF_KIND_STRUCT:
518 	case BTF_KIND_UNION: {
519 		const struct btf_member *m = btf_members(t);
520 		/*
521 		 * struct/union is part of strong link, only if it's embedded
522 		 * (so no ptr in a path) or it's anonymous (so has to be
523 		 * defined inline, even if declared through ptr)
524 		 */
525 		if (through_ptr && t->name_off != 0)
526 			return 0;
527 
528 		tstate->order_state = ORDERING;
529 
530 		vlen = btf_vlen(t);
531 		for (i = 0; i < vlen; i++, m++) {
532 			err = btf_dump_order_type(d, m->type, false);
533 			if (err < 0)
534 				return err;
535 		}
536 
537 		if (t->name_off != 0) {
538 			err = btf_dump_add_emit_queue_id(d, id);
539 			if (err < 0)
540 				return err;
541 		}
542 
543 		tstate->order_state = ORDERED;
544 		return 1;
545 	}
546 	case BTF_KIND_ENUM:
547 	case BTF_KIND_ENUM64:
548 	case BTF_KIND_FWD:
549 		/*
550 		 * non-anonymous or non-referenced enums are top-level
551 		 * declarations and should be emitted. Same logic can be
552 		 * applied to FWDs, it won't hurt anyways.
553 		 */
554 		if (t->name_off != 0 || !tstate->referenced) {
555 			err = btf_dump_add_emit_queue_id(d, id);
556 			if (err)
557 				return err;
558 		}
559 		tstate->order_state = ORDERED;
560 		return 1;
561 
562 	case BTF_KIND_TYPEDEF: {
563 		int is_strong;
564 
565 		is_strong = btf_dump_order_type(d, t->type, through_ptr);
566 		if (is_strong < 0)
567 			return is_strong;
568 
569 		/* typedef is similar to struct/union w.r.t. fwd-decls */
570 		if (through_ptr && !is_strong)
571 			return 0;
572 
573 		/* typedef is always a named definition */
574 		err = btf_dump_add_emit_queue_id(d, id);
575 		if (err)
576 			return err;
577 
578 		d->type_states[id].order_state = ORDERED;
579 		return 1;
580 	}
581 	case BTF_KIND_VOLATILE:
582 	case BTF_KIND_CONST:
583 	case BTF_KIND_RESTRICT:
584 	case BTF_KIND_TYPE_TAG:
585 		return btf_dump_order_type(d, t->type, through_ptr);
586 
587 	case BTF_KIND_FUNC_PROTO: {
588 		const struct btf_param *p = btf_params(t);
589 		bool is_strong;
590 
591 		err = btf_dump_order_type(d, t->type, through_ptr);
592 		if (err < 0)
593 			return err;
594 		is_strong = err > 0;
595 
596 		vlen = btf_vlen(t);
597 		for (i = 0; i < vlen; i++, p++) {
598 			err = btf_dump_order_type(d, p->type, through_ptr);
599 			if (err < 0)
600 				return err;
601 			if (err > 0)
602 				is_strong = true;
603 		}
604 		return is_strong;
605 	}
606 	case BTF_KIND_FUNC:
607 	case BTF_KIND_VAR:
608 	case BTF_KIND_DATASEC:
609 	case BTF_KIND_DECL_TAG:
610 		d->type_states[id].order_state = ORDERED;
611 		return 0;
612 
613 	default:
614 		return -EINVAL;
615 	}
616 }
617 
618 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
619 					  const struct btf_type *t);
620 
621 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
622 				     const struct btf_type *t);
623 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id,
624 				     const struct btf_type *t, int lvl);
625 
626 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
627 				   const struct btf_type *t);
628 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
629 				   const struct btf_type *t, int lvl);
630 
631 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
632 				  const struct btf_type *t);
633 
634 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
635 				      const struct btf_type *t, int lvl);
636 
637 /* a local view into a shared stack */
638 struct id_stack {
639 	const __u32 *ids;
640 	int cnt;
641 };
642 
643 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
644 				    const char *fname, int lvl);
645 static void btf_dump_emit_type_chain(struct btf_dump *d,
646 				     struct id_stack *decl_stack,
647 				     const char *fname, int lvl);
648 
649 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
650 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
651 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
652 				 const char *orig_name);
653 
btf_dump_is_blacklisted(struct btf_dump * d,__u32 id)654 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
655 {
656 	const struct btf_type *t = btf__type_by_id(d->btf, id);
657 
658 	/* __builtin_va_list is a compiler built-in, which causes compilation
659 	 * errors, when compiling w/ different compiler, then used to compile
660 	 * original code (e.g., GCC to compile kernel, Clang to use generated
661 	 * C header from BTF). As it is built-in, it should be already defined
662 	 * properly internally in compiler.
663 	 */
664 	if (t->name_off == 0)
665 		return false;
666 	return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0;
667 }
668 
669 /*
670  * Emit C-syntax definitions of types from chains of BTF types.
671  *
672  * High-level handling of determining necessary forward declarations are handled
673  * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type
674  * declarations/definitions in C syntax  are handled by a combo of
675  * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to
676  * corresponding btf_dump_emit_*_{def,fwd}() functions.
677  *
678  * We also keep track of "containing struct/union type ID" to determine when
679  * we reference it from inside and thus can avoid emitting unnecessary forward
680  * declaration.
681  *
682  * This algorithm is designed in such a way, that even if some error occurs
683  * (either technical, e.g., out of memory, or logical, i.e., malformed BTF
684  * that doesn't comply to C rules completely), algorithm will try to proceed
685  * and produce as much meaningful output as possible.
686  */
btf_dump_emit_type(struct btf_dump * d,__u32 id,__u32 cont_id)687 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
688 {
689 	struct btf_dump_type_aux_state *tstate = &d->type_states[id];
690 	bool top_level_def = cont_id == 0;
691 	const struct btf_type *t;
692 	__u16 kind;
693 
694 	if (tstate->emit_state == EMITTED)
695 		return;
696 
697 	t = btf__type_by_id(d->btf, id);
698 	kind = btf_kind(t);
699 
700 	if (tstate->emit_state == EMITTING) {
701 		if (tstate->fwd_emitted)
702 			return;
703 
704 		switch (kind) {
705 		case BTF_KIND_STRUCT:
706 		case BTF_KIND_UNION:
707 			/*
708 			 * if we are referencing a struct/union that we are
709 			 * part of - then no need for fwd declaration
710 			 */
711 			if (id == cont_id)
712 				return;
713 			if (t->name_off == 0) {
714 				pr_warn("anonymous struct/union loop, id:[%u]\n",
715 					id);
716 				return;
717 			}
718 			btf_dump_emit_struct_fwd(d, id, t);
719 			btf_dump_printf(d, ";\n\n");
720 			tstate->fwd_emitted = 1;
721 			break;
722 		case BTF_KIND_TYPEDEF:
723 			/*
724 			 * for typedef fwd_emitted means typedef definition
725 			 * was emitted, but it can be used only for "weak"
726 			 * references through pointer only, not for embedding
727 			 */
728 			if (!btf_dump_is_blacklisted(d, id)) {
729 				btf_dump_emit_typedef_def(d, id, t, 0);
730 				btf_dump_printf(d, ";\n\n");
731 			}
732 			tstate->fwd_emitted = 1;
733 			break;
734 		default:
735 			break;
736 		}
737 
738 		return;
739 	}
740 
741 	switch (kind) {
742 	case BTF_KIND_INT:
743 		/* Emit type alias definitions if necessary */
744 		btf_dump_emit_missing_aliases(d, id, t);
745 
746 		tstate->emit_state = EMITTED;
747 		break;
748 	case BTF_KIND_ENUM:
749 	case BTF_KIND_ENUM64:
750 		if (top_level_def) {
751 			btf_dump_emit_enum_def(d, id, t, 0);
752 			btf_dump_printf(d, ";\n\n");
753 		}
754 		tstate->emit_state = EMITTED;
755 		break;
756 	case BTF_KIND_PTR:
757 	case BTF_KIND_VOLATILE:
758 	case BTF_KIND_CONST:
759 	case BTF_KIND_RESTRICT:
760 	case BTF_KIND_TYPE_TAG:
761 		btf_dump_emit_type(d, t->type, cont_id);
762 		break;
763 	case BTF_KIND_ARRAY:
764 		btf_dump_emit_type(d, btf_array(t)->type, cont_id);
765 		break;
766 	case BTF_KIND_FWD:
767 		btf_dump_emit_fwd_def(d, id, t);
768 		btf_dump_printf(d, ";\n\n");
769 		tstate->emit_state = EMITTED;
770 		break;
771 	case BTF_KIND_TYPEDEF:
772 		tstate->emit_state = EMITTING;
773 		btf_dump_emit_type(d, t->type, id);
774 		/*
775 		 * typedef can server as both definition and forward
776 		 * declaration; at this stage someone depends on
777 		 * typedef as a forward declaration (refers to it
778 		 * through pointer), so unless we already did it,
779 		 * emit typedef as a forward declaration
780 		 */
781 		if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
782 			btf_dump_emit_typedef_def(d, id, t, 0);
783 			btf_dump_printf(d, ";\n\n");
784 		}
785 		tstate->emit_state = EMITTED;
786 		break;
787 	case BTF_KIND_STRUCT:
788 	case BTF_KIND_UNION:
789 		tstate->emit_state = EMITTING;
790 		/* if it's a top-level struct/union definition or struct/union
791 		 * is anonymous, then in C we'll be emitting all fields and
792 		 * their types (as opposed to just `struct X`), so we need to
793 		 * make sure that all types, referenced from struct/union
794 		 * members have necessary forward-declarations, where
795 		 * applicable
796 		 */
797 		if (top_level_def || t->name_off == 0) {
798 			const struct btf_member *m = btf_members(t);
799 			__u16 vlen = btf_vlen(t);
800 			int i, new_cont_id;
801 
802 			new_cont_id = t->name_off == 0 ? cont_id : id;
803 			for (i = 0; i < vlen; i++, m++)
804 				btf_dump_emit_type(d, m->type, new_cont_id);
805 		} else if (!tstate->fwd_emitted && id != cont_id) {
806 			btf_dump_emit_struct_fwd(d, id, t);
807 			btf_dump_printf(d, ";\n\n");
808 			tstate->fwd_emitted = 1;
809 		}
810 
811 		if (top_level_def) {
812 			btf_dump_emit_struct_def(d, id, t, 0);
813 			btf_dump_printf(d, ";\n\n");
814 			tstate->emit_state = EMITTED;
815 		} else {
816 			tstate->emit_state = NOT_EMITTED;
817 		}
818 		break;
819 	case BTF_KIND_FUNC_PROTO: {
820 		const struct btf_param *p = btf_params(t);
821 		__u16 n = btf_vlen(t);
822 		int i;
823 
824 		btf_dump_emit_type(d, t->type, cont_id);
825 		for (i = 0; i < n; i++, p++)
826 			btf_dump_emit_type(d, p->type, cont_id);
827 
828 		break;
829 	}
830 	default:
831 		break;
832 	}
833 }
834 
btf_is_struct_packed(const struct btf * btf,__u32 id,const struct btf_type * t)835 static bool btf_is_struct_packed(const struct btf *btf, __u32 id,
836 				 const struct btf_type *t)
837 {
838 	const struct btf_member *m;
839 	int max_align = 1, align, i, bit_sz;
840 	__u16 vlen;
841 
842 	m = btf_members(t);
843 	vlen = btf_vlen(t);
844 	/* all non-bitfield fields have to be naturally aligned */
845 	for (i = 0; i < vlen; i++, m++) {
846 		align = btf__align_of(btf, m->type);
847 		bit_sz = btf_member_bitfield_size(t, i);
848 		if (align && bit_sz == 0 && m->offset % (8 * align) != 0)
849 			return true;
850 		max_align = max(align, max_align);
851 	}
852 	/* size of a non-packed struct has to be a multiple of its alignment */
853 	if (t->size % max_align != 0)
854 		return true;
855 	/*
856 	 * if original struct was marked as packed, but its layout is
857 	 * naturally aligned, we'll detect that it's not packed
858 	 */
859 	return false;
860 }
861 
btf_dump_emit_bit_padding(const struct btf_dump * d,int cur_off,int next_off,int next_align,bool in_bitfield,int lvl)862 static void btf_dump_emit_bit_padding(const struct btf_dump *d,
863 				      int cur_off, int next_off, int next_align,
864 				      bool in_bitfield, int lvl)
865 {
866 	const struct {
867 		const char *name;
868 		int bits;
869 	} pads[] = {
870 		{"long", d->ptr_sz * 8}, {"int", 32}, {"short", 16}, {"char", 8}
871 	};
872 	int new_off, pad_bits, bits, i;
873 	const char *pad_type;
874 
875 	if (cur_off >= next_off)
876 		return; /* no gap */
877 
878 	/* For filling out padding we want to take advantage of
879 	 * natural alignment rules to minimize unnecessary explicit
880 	 * padding. First, we find the largest type (among long, int,
881 	 * short, or char) that can be used to force naturally aligned
882 	 * boundary. Once determined, we'll use such type to fill in
883 	 * the remaining padding gap. In some cases we can rely on
884 	 * compiler filling some gaps, but sometimes we need to force
885 	 * alignment to close natural alignment with markers like
886 	 * `long: 0` (this is always the case for bitfields).  Note
887 	 * that even if struct itself has, let's say 4-byte alignment
888 	 * (i.e., it only uses up to int-aligned types), using `long:
889 	 * X;` explicit padding doesn't actually change struct's
890 	 * overall alignment requirements, but compiler does take into
891 	 * account that type's (long, in this example) natural
892 	 * alignment requirements when adding implicit padding. We use
893 	 * this fact heavily and don't worry about ruining correct
894 	 * struct alignment requirement.
895 	 */
896 	for (i = 0; i < ARRAY_SIZE(pads); i++) {
897 		pad_bits = pads[i].bits;
898 		pad_type = pads[i].name;
899 
900 		new_off = roundup(cur_off, pad_bits);
901 		if (new_off <= next_off)
902 			break;
903 	}
904 
905 	if (new_off > cur_off && new_off <= next_off) {
906 		/* We need explicit `<type>: 0` aligning mark if next
907 		 * field is right on alignment offset and its
908 		 * alignment requirement is less strict than <type>'s
909 		 * alignment (so compiler won't naturally align to the
910 		 * offset we expect), or if subsequent `<type>: X`,
911 		 * will actually completely fit in the remaining hole,
912 		 * making compiler basically ignore `<type>: X`
913 		 * completely.
914 		 */
915 		if (in_bitfield ||
916 		    (new_off == next_off && roundup(cur_off, next_align * 8) != new_off) ||
917 		    (new_off != next_off && next_off - new_off <= new_off - cur_off))
918 			/* but for bitfields we'll emit explicit bit count */
919 			btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type,
920 					in_bitfield ? new_off - cur_off : 0);
921 		cur_off = new_off;
922 	}
923 
924 	/* Now we know we start at naturally aligned offset for a chosen
925 	 * padding type (long, int, short, or char), and so the rest is just
926 	 * a straightforward filling of remaining padding gap with full
927 	 * `<type>: sizeof(<type>);` markers, except for the last one, which
928 	 * might need smaller than sizeof(<type>) padding.
929 	 */
930 	while (cur_off != next_off) {
931 		bits = min(next_off - cur_off, pad_bits);
932 		if (bits == pad_bits) {
933 			btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
934 			cur_off += bits;
935 			continue;
936 		}
937 		/* For the remainder padding that doesn't cover entire
938 		 * pad_type bit length, we pick the smallest necessary type.
939 		 * This is pure aesthetics, we could have just used `long`,
940 		 * but having smallest necessary one communicates better the
941 		 * scale of the padding gap.
942 		 */
943 		for (i = ARRAY_SIZE(pads) - 1; i >= 0; i--) {
944 			pad_type = pads[i].name;
945 			pad_bits = pads[i].bits;
946 			if (pad_bits < bits)
947 				continue;
948 
949 			btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, bits);
950 			cur_off += bits;
951 			break;
952 		}
953 	}
954 }
955 
btf_dump_emit_struct_fwd(struct btf_dump * d,__u32 id,const struct btf_type * t)956 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
957 				     const struct btf_type *t)
958 {
959 	btf_dump_printf(d, "%s%s%s",
960 			btf_is_struct(t) ? "struct" : "union",
961 			t->name_off ? " " : "",
962 			btf_dump_type_name(d, id));
963 }
964 
btf_dump_emit_struct_def(struct btf_dump * d,__u32 id,const struct btf_type * t,int lvl)965 static void btf_dump_emit_struct_def(struct btf_dump *d,
966 				     __u32 id,
967 				     const struct btf_type *t,
968 				     int lvl)
969 {
970 	const struct btf_member *m = btf_members(t);
971 	bool is_struct = btf_is_struct(t);
972 	bool packed, prev_bitfield = false;
973 	int align, i, off = 0;
974 	__u16 vlen = btf_vlen(t);
975 
976 	align = btf__align_of(d->btf, id);
977 	packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
978 
979 	btf_dump_printf(d, "%s%s%s {",
980 			is_struct ? "struct" : "union",
981 			t->name_off ? " " : "",
982 			btf_dump_type_name(d, id));
983 
984 	for (i = 0; i < vlen; i++, m++) {
985 		const char *fname;
986 		int m_off, m_sz, m_align;
987 		bool in_bitfield;
988 
989 		fname = btf_name_of(d, m->name_off);
990 		m_sz = btf_member_bitfield_size(t, i);
991 		m_off = btf_member_bit_offset(t, i);
992 		m_align = packed ? 1 : btf__align_of(d->btf, m->type);
993 
994 		in_bitfield = prev_bitfield && m_sz != 0;
995 
996 		btf_dump_emit_bit_padding(d, off, m_off, m_align, in_bitfield, lvl + 1);
997 		btf_dump_printf(d, "\n%s", pfx(lvl + 1));
998 		btf_dump_emit_type_decl(d, m->type, fname, lvl + 1);
999 
1000 		if (m_sz) {
1001 			btf_dump_printf(d, ": %d", m_sz);
1002 			off = m_off + m_sz;
1003 			prev_bitfield = true;
1004 		} else {
1005 			m_sz = max((__s64)0, btf__resolve_size(d->btf, m->type));
1006 			off = m_off + m_sz * 8;
1007 			prev_bitfield = false;
1008 		}
1009 
1010 		btf_dump_printf(d, ";");
1011 	}
1012 
1013 	/* pad at the end, if necessary */
1014 	if (is_struct)
1015 		btf_dump_emit_bit_padding(d, off, t->size * 8, align, false, lvl + 1);
1016 
1017 	/*
1018 	 * Keep `struct empty {}` on a single line,
1019 	 * only print newline when there are regular or padding fields.
1020 	 */
1021 	if (vlen || t->size) {
1022 		btf_dump_printf(d, "\n");
1023 		btf_dump_printf(d, "%s}", pfx(lvl));
1024 	} else {
1025 		btf_dump_printf(d, "}");
1026 	}
1027 	if (packed)
1028 		btf_dump_printf(d, " __attribute__((packed))");
1029 }
1030 
1031 static const char *missing_base_types[][2] = {
1032 	/*
1033 	 * GCC emits typedefs to its internal __PolyX_t types when compiling Arm
1034 	 * SIMD intrinsics. Alias them to standard base types.
1035 	 */
1036 	{ "__Poly8_t",		"unsigned char" },
1037 	{ "__Poly16_t",		"unsigned short" },
1038 	{ "__Poly64_t",		"unsigned long long" },
1039 	{ "__Poly128_t",	"unsigned __int128" },
1040 };
1041 
btf_dump_emit_missing_aliases(struct btf_dump * d,__u32 id,const struct btf_type * t)1042 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
1043 					  const struct btf_type *t)
1044 {
1045 	const char *name = btf_dump_type_name(d, id);
1046 	int i;
1047 
1048 	for (i = 0; i < ARRAY_SIZE(missing_base_types); i++) {
1049 		if (strcmp(name, missing_base_types[i][0]) == 0) {
1050 			btf_dump_printf(d, "typedef %s %s;\n\n",
1051 					missing_base_types[i][1], name);
1052 			break;
1053 		}
1054 	}
1055 }
1056 
btf_dump_emit_enum_fwd(struct btf_dump * d,__u32 id,const struct btf_type * t)1057 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
1058 				   const struct btf_type *t)
1059 {
1060 	btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id));
1061 }
1062 
btf_dump_emit_enum32_val(struct btf_dump * d,const struct btf_type * t,int lvl,__u16 vlen)1063 static void btf_dump_emit_enum32_val(struct btf_dump *d,
1064 				     const struct btf_type *t,
1065 				     int lvl, __u16 vlen)
1066 {
1067 	const struct btf_enum *v = btf_enum(t);
1068 	bool is_signed = btf_kflag(t);
1069 	const char *fmt_str;
1070 	const char *name;
1071 	size_t dup_cnt;
1072 	int i;
1073 
1074 	for (i = 0; i < vlen; i++, v++) {
1075 		name = btf_name_of(d, v->name_off);
1076 		/* enumerators share namespace with typedef idents */
1077 		dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1078 		if (dup_cnt > 1) {
1079 			fmt_str = is_signed ? "\n%s%s___%zd = %d," : "\n%s%s___%zd = %u,";
1080 			btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, dup_cnt, v->val);
1081 		} else {
1082 			fmt_str = is_signed ? "\n%s%s = %d," : "\n%s%s = %u,";
1083 			btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, v->val);
1084 		}
1085 	}
1086 }
1087 
btf_dump_emit_enum64_val(struct btf_dump * d,const struct btf_type * t,int lvl,__u16 vlen)1088 static void btf_dump_emit_enum64_val(struct btf_dump *d,
1089 				     const struct btf_type *t,
1090 				     int lvl, __u16 vlen)
1091 {
1092 	const struct btf_enum64 *v = btf_enum64(t);
1093 	bool is_signed = btf_kflag(t);
1094 	const char *fmt_str;
1095 	const char *name;
1096 	size_t dup_cnt;
1097 	__u64 val;
1098 	int i;
1099 
1100 	for (i = 0; i < vlen; i++, v++) {
1101 		name = btf_name_of(d, v->name_off);
1102 		dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1103 		val = btf_enum64_value(v);
1104 		if (dup_cnt > 1) {
1105 			fmt_str = is_signed ? "\n%s%s___%zd = %lldLL,"
1106 					    : "\n%s%s___%zd = %lluULL,";
1107 			btf_dump_printf(d, fmt_str,
1108 					pfx(lvl + 1), name, dup_cnt,
1109 					(unsigned long long)val);
1110 		} else {
1111 			fmt_str = is_signed ? "\n%s%s = %lldLL,"
1112 					    : "\n%s%s = %lluULL,";
1113 			btf_dump_printf(d, fmt_str,
1114 					pfx(lvl + 1), name,
1115 					(unsigned long long)val);
1116 		}
1117 	}
1118 }
btf_dump_emit_enum_def(struct btf_dump * d,__u32 id,const struct btf_type * t,int lvl)1119 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
1120 				   const struct btf_type *t,
1121 				   int lvl)
1122 {
1123 	__u16 vlen = btf_vlen(t);
1124 
1125 	btf_dump_printf(d, "enum%s%s",
1126 			t->name_off ? " " : "",
1127 			btf_dump_type_name(d, id));
1128 
1129 	if (!vlen)
1130 		return;
1131 
1132 	btf_dump_printf(d, " {");
1133 	if (btf_is_enum(t))
1134 		btf_dump_emit_enum32_val(d, t, lvl, vlen);
1135 	else
1136 		btf_dump_emit_enum64_val(d, t, lvl, vlen);
1137 	btf_dump_printf(d, "\n%s}", pfx(lvl));
1138 
1139 	/* special case enums with special sizes */
1140 	if (t->size == 1) {
1141 		/* one-byte enums can be forced with mode(byte) attribute */
1142 		btf_dump_printf(d, " __attribute__((mode(byte)))");
1143 	} else if (t->size == 8 && d->ptr_sz == 8) {
1144 		/* enum can be 8-byte sized if one of the enumerator values
1145 		 * doesn't fit in 32-bit integer, or by adding mode(word)
1146 		 * attribute (but probably only on 64-bit architectures); do
1147 		 * our best here to try to satisfy the contract without adding
1148 		 * unnecessary attributes
1149 		 */
1150 		bool needs_word_mode;
1151 
1152 		if (btf_is_enum(t)) {
1153 			/* enum can't represent 64-bit values, so we need word mode */
1154 			needs_word_mode = true;
1155 		} else {
1156 			/* enum64 needs mode(word) if none of its values has
1157 			 * non-zero upper 32-bits (which means that all values
1158 			 * fit in 32-bit integers and won't cause compiler to
1159 			 * bump enum to be 64-bit naturally
1160 			 */
1161 			int i;
1162 
1163 			needs_word_mode = true;
1164 			for (i = 0; i < vlen; i++) {
1165 				if (btf_enum64(t)[i].val_hi32 != 0) {
1166 					needs_word_mode = false;
1167 					break;
1168 				}
1169 			}
1170 		}
1171 		if (needs_word_mode)
1172 			btf_dump_printf(d, " __attribute__((mode(word)))");
1173 	}
1174 
1175 }
1176 
btf_dump_emit_fwd_def(struct btf_dump * d,__u32 id,const struct btf_type * t)1177 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
1178 				  const struct btf_type *t)
1179 {
1180 	const char *name = btf_dump_type_name(d, id);
1181 
1182 	if (btf_kflag(t))
1183 		btf_dump_printf(d, "union %s", name);
1184 	else
1185 		btf_dump_printf(d, "struct %s", name);
1186 }
1187 
btf_dump_emit_typedef_def(struct btf_dump * d,__u32 id,const struct btf_type * t,int lvl)1188 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
1189 				     const struct btf_type *t, int lvl)
1190 {
1191 	const char *name = btf_dump_ident_name(d, id);
1192 
1193 	/*
1194 	 * Old GCC versions are emitting invalid typedef for __gnuc_va_list
1195 	 * pointing to VOID. This generates warnings from btf_dump() and
1196 	 * results in uncompilable header file, so we are fixing it up here
1197 	 * with valid typedef into __builtin_va_list.
1198 	 */
1199 	if (t->type == 0 && strcmp(name, "__gnuc_va_list") == 0) {
1200 		btf_dump_printf(d, "typedef __builtin_va_list __gnuc_va_list");
1201 		return;
1202 	}
1203 
1204 	btf_dump_printf(d, "typedef ");
1205 	btf_dump_emit_type_decl(d, t->type, name, lvl);
1206 }
1207 
btf_dump_push_decl_stack_id(struct btf_dump * d,__u32 id)1208 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
1209 {
1210 	__u32 *new_stack;
1211 	size_t new_cap;
1212 
1213 	if (d->decl_stack_cnt >= d->decl_stack_cap) {
1214 		new_cap = max(16, d->decl_stack_cap * 3 / 2);
1215 		new_stack = libbpf_reallocarray(d->decl_stack, new_cap, sizeof(new_stack[0]));
1216 		if (!new_stack)
1217 			return -ENOMEM;
1218 		d->decl_stack = new_stack;
1219 		d->decl_stack_cap = new_cap;
1220 	}
1221 
1222 	d->decl_stack[d->decl_stack_cnt++] = id;
1223 
1224 	return 0;
1225 }
1226 
1227 /*
1228  * Emit type declaration (e.g., field type declaration in a struct or argument
1229  * declaration in function prototype) in correct C syntax.
1230  *
1231  * For most types it's trivial, but there are few quirky type declaration
1232  * cases worth mentioning:
1233  *   - function prototypes (especially nesting of function prototypes);
1234  *   - arrays;
1235  *   - const/volatile/restrict for pointers vs other types.
1236  *
1237  * For a good discussion of *PARSING* C syntax (as a human), see
1238  * Peter van der Linden's "Expert C Programming: Deep C Secrets",
1239  * Ch.3 "Unscrambling Declarations in C".
1240  *
1241  * It won't help with BTF to C conversion much, though, as it's an opposite
1242  * problem. So we came up with this algorithm in reverse to van der Linden's
1243  * parsing algorithm. It goes from structured BTF representation of type
1244  * declaration to a valid compilable C syntax.
1245  *
1246  * For instance, consider this C typedef:
1247  *	typedef const int * const * arr[10] arr_t;
1248  * It will be represented in BTF with this chain of BTF types:
1249  *	[typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int]
1250  *
1251  * Notice how [const] modifier always goes before type it modifies in BTF type
1252  * graph, but in C syntax, const/volatile/restrict modifiers are written to
1253  * the right of pointers, but to the left of other types. There are also other
1254  * quirks, like function pointers, arrays of them, functions returning other
1255  * functions, etc.
1256  *
1257  * We handle that by pushing all the types to a stack, until we hit "terminal"
1258  * type (int/enum/struct/union/fwd). Then depending on the kind of a type on
1259  * top of a stack, modifiers are handled differently. Array/function pointers
1260  * have also wildly different syntax and how nesting of them are done. See
1261  * code for authoritative definition.
1262  *
1263  * To avoid allocating new stack for each independent chain of BTF types, we
1264  * share one bigger stack, with each chain working only on its own local view
1265  * of a stack frame. Some care is required to "pop" stack frames after
1266  * processing type declaration chain.
1267  */
btf_dump__emit_type_decl(struct btf_dump * d,__u32 id,const struct btf_dump_emit_type_decl_opts * opts)1268 int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
1269 			     const struct btf_dump_emit_type_decl_opts *opts)
1270 {
1271 	const char *fname;
1272 	int lvl, err;
1273 
1274 	if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
1275 		return libbpf_err(-EINVAL);
1276 
1277 	err = btf_dump_resize(d);
1278 	if (err)
1279 		return libbpf_err(err);
1280 
1281 	fname = OPTS_GET(opts, field_name, "");
1282 	lvl = OPTS_GET(opts, indent_level, 0);
1283 	d->strip_mods = OPTS_GET(opts, strip_mods, false);
1284 	btf_dump_emit_type_decl(d, id, fname, lvl);
1285 	d->strip_mods = false;
1286 	return 0;
1287 }
1288 
btf_dump_emit_type_decl(struct btf_dump * d,__u32 id,const char * fname,int lvl)1289 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
1290 				    const char *fname, int lvl)
1291 {
1292 	struct id_stack decl_stack;
1293 	const struct btf_type *t;
1294 	int err, stack_start;
1295 
1296 	stack_start = d->decl_stack_cnt;
1297 	for (;;) {
1298 		t = btf__type_by_id(d->btf, id);
1299 		if (d->strip_mods && btf_is_mod(t))
1300 			goto skip_mod;
1301 
1302 		err = btf_dump_push_decl_stack_id(d, id);
1303 		if (err < 0) {
1304 			/*
1305 			 * if we don't have enough memory for entire type decl
1306 			 * chain, restore stack, emit warning, and try to
1307 			 * proceed nevertheless
1308 			 */
1309 			pr_warn("not enough memory for decl stack:%d", err);
1310 			d->decl_stack_cnt = stack_start;
1311 			return;
1312 		}
1313 skip_mod:
1314 		/* VOID */
1315 		if (id == 0)
1316 			break;
1317 
1318 		switch (btf_kind(t)) {
1319 		case BTF_KIND_PTR:
1320 		case BTF_KIND_VOLATILE:
1321 		case BTF_KIND_CONST:
1322 		case BTF_KIND_RESTRICT:
1323 		case BTF_KIND_FUNC_PROTO:
1324 		case BTF_KIND_TYPE_TAG:
1325 			id = t->type;
1326 			break;
1327 		case BTF_KIND_ARRAY:
1328 			id = btf_array(t)->type;
1329 			break;
1330 		case BTF_KIND_INT:
1331 		case BTF_KIND_ENUM:
1332 		case BTF_KIND_ENUM64:
1333 		case BTF_KIND_FWD:
1334 		case BTF_KIND_STRUCT:
1335 		case BTF_KIND_UNION:
1336 		case BTF_KIND_TYPEDEF:
1337 		case BTF_KIND_FLOAT:
1338 			goto done;
1339 		default:
1340 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1341 				btf_kind(t), id);
1342 			goto done;
1343 		}
1344 	}
1345 done:
1346 	/*
1347 	 * We might be inside a chain of declarations (e.g., array of function
1348 	 * pointers returning anonymous (so inlined) structs, having another
1349 	 * array field). Each of those needs its own "stack frame" to handle
1350 	 * emitting of declarations. Those stack frames are non-overlapping
1351 	 * portions of shared btf_dump->decl_stack. To make it a bit nicer to
1352 	 * handle this set of nested stacks, we create a view corresponding to
1353 	 * our own "stack frame" and work with it as an independent stack.
1354 	 * We'll need to clean up after emit_type_chain() returns, though.
1355 	 */
1356 	decl_stack.ids = d->decl_stack + stack_start;
1357 	decl_stack.cnt = d->decl_stack_cnt - stack_start;
1358 	btf_dump_emit_type_chain(d, &decl_stack, fname, lvl);
1359 	/*
1360 	 * emit_type_chain() guarantees that it will pop its entire decl_stack
1361 	 * frame before returning. But it works with a read-only view into
1362 	 * decl_stack, so it doesn't actually pop anything from the
1363 	 * perspective of shared btf_dump->decl_stack, per se. We need to
1364 	 * reset decl_stack state to how it was before us to avoid it growing
1365 	 * all the time.
1366 	 */
1367 	d->decl_stack_cnt = stack_start;
1368 }
1369 
btf_dump_emit_mods(struct btf_dump * d,struct id_stack * decl_stack)1370 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
1371 {
1372 	const struct btf_type *t;
1373 	__u32 id;
1374 
1375 	while (decl_stack->cnt) {
1376 		id = decl_stack->ids[decl_stack->cnt - 1];
1377 		t = btf__type_by_id(d->btf, id);
1378 
1379 		switch (btf_kind(t)) {
1380 		case BTF_KIND_VOLATILE:
1381 			btf_dump_printf(d, "volatile ");
1382 			break;
1383 		case BTF_KIND_CONST:
1384 			btf_dump_printf(d, "const ");
1385 			break;
1386 		case BTF_KIND_RESTRICT:
1387 			btf_dump_printf(d, "restrict ");
1388 			break;
1389 		default:
1390 			return;
1391 		}
1392 		decl_stack->cnt--;
1393 	}
1394 }
1395 
btf_dump_drop_mods(struct btf_dump * d,struct id_stack * decl_stack)1396 static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
1397 {
1398 	const struct btf_type *t;
1399 	__u32 id;
1400 
1401 	while (decl_stack->cnt) {
1402 		id = decl_stack->ids[decl_stack->cnt - 1];
1403 		t = btf__type_by_id(d->btf, id);
1404 		if (!btf_is_mod(t))
1405 			return;
1406 		decl_stack->cnt--;
1407 	}
1408 }
1409 
btf_dump_emit_name(const struct btf_dump * d,const char * name,bool last_was_ptr)1410 static void btf_dump_emit_name(const struct btf_dump *d,
1411 			       const char *name, bool last_was_ptr)
1412 {
1413 	bool separate = name[0] && !last_was_ptr;
1414 
1415 	btf_dump_printf(d, "%s%s", separate ? " " : "", name);
1416 }
1417 
btf_dump_emit_type_chain(struct btf_dump * d,struct id_stack * decls,const char * fname,int lvl)1418 static void btf_dump_emit_type_chain(struct btf_dump *d,
1419 				     struct id_stack *decls,
1420 				     const char *fname, int lvl)
1421 {
1422 	/*
1423 	 * last_was_ptr is used to determine if we need to separate pointer
1424 	 * asterisk (*) from previous part of type signature with space, so
1425 	 * that we get `int ***`, instead of `int * * *`. We default to true
1426 	 * for cases where we have single pointer in a chain. E.g., in ptr ->
1427 	 * func_proto case. func_proto will start a new emit_type_chain call
1428 	 * with just ptr, which should be emitted as (*) or (*<fname>), so we
1429 	 * don't want to prepend space for that last pointer.
1430 	 */
1431 	bool last_was_ptr = true;
1432 	const struct btf_type *t;
1433 	const char *name;
1434 	__u16 kind;
1435 	__u32 id;
1436 
1437 	while (decls->cnt) {
1438 		id = decls->ids[--decls->cnt];
1439 		if (id == 0) {
1440 			/* VOID is a special snowflake */
1441 			btf_dump_emit_mods(d, decls);
1442 			btf_dump_printf(d, "void");
1443 			last_was_ptr = false;
1444 			continue;
1445 		}
1446 
1447 		t = btf__type_by_id(d->btf, id);
1448 		kind = btf_kind(t);
1449 
1450 		switch (kind) {
1451 		case BTF_KIND_INT:
1452 		case BTF_KIND_FLOAT:
1453 			btf_dump_emit_mods(d, decls);
1454 			name = btf_name_of(d, t->name_off);
1455 			btf_dump_printf(d, "%s", name);
1456 			break;
1457 		case BTF_KIND_STRUCT:
1458 		case BTF_KIND_UNION:
1459 			btf_dump_emit_mods(d, decls);
1460 			/* inline anonymous struct/union */
1461 			if (t->name_off == 0 && !d->skip_anon_defs)
1462 				btf_dump_emit_struct_def(d, id, t, lvl);
1463 			else
1464 				btf_dump_emit_struct_fwd(d, id, t);
1465 			break;
1466 		case BTF_KIND_ENUM:
1467 		case BTF_KIND_ENUM64:
1468 			btf_dump_emit_mods(d, decls);
1469 			/* inline anonymous enum */
1470 			if (t->name_off == 0 && !d->skip_anon_defs)
1471 				btf_dump_emit_enum_def(d, id, t, lvl);
1472 			else
1473 				btf_dump_emit_enum_fwd(d, id, t);
1474 			break;
1475 		case BTF_KIND_FWD:
1476 			btf_dump_emit_mods(d, decls);
1477 			btf_dump_emit_fwd_def(d, id, t);
1478 			break;
1479 		case BTF_KIND_TYPEDEF:
1480 			btf_dump_emit_mods(d, decls);
1481 			btf_dump_printf(d, "%s", btf_dump_ident_name(d, id));
1482 			break;
1483 		case BTF_KIND_PTR:
1484 			btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *");
1485 			break;
1486 		case BTF_KIND_VOLATILE:
1487 			btf_dump_printf(d, " volatile");
1488 			break;
1489 		case BTF_KIND_CONST:
1490 			btf_dump_printf(d, " const");
1491 			break;
1492 		case BTF_KIND_RESTRICT:
1493 			btf_dump_printf(d, " restrict");
1494 			break;
1495 		case BTF_KIND_TYPE_TAG:
1496 			btf_dump_emit_mods(d, decls);
1497 			name = btf_name_of(d, t->name_off);
1498 			btf_dump_printf(d, " __attribute__((btf_type_tag(\"%s\")))", name);
1499 			break;
1500 		case BTF_KIND_ARRAY: {
1501 			const struct btf_array *a = btf_array(t);
1502 			const struct btf_type *next_t;
1503 			__u32 next_id;
1504 			bool multidim;
1505 			/*
1506 			 * GCC has a bug
1507 			 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354)
1508 			 * which causes it to emit extra const/volatile
1509 			 * modifiers for an array, if array's element type has
1510 			 * const/volatile modifiers. Clang doesn't do that.
1511 			 * In general, it doesn't seem very meaningful to have
1512 			 * a const/volatile modifier for array, so we are
1513 			 * going to silently skip them here.
1514 			 */
1515 			btf_dump_drop_mods(d, decls);
1516 
1517 			if (decls->cnt == 0) {
1518 				btf_dump_emit_name(d, fname, last_was_ptr);
1519 				btf_dump_printf(d, "[%u]", a->nelems);
1520 				return;
1521 			}
1522 
1523 			next_id = decls->ids[decls->cnt - 1];
1524 			next_t = btf__type_by_id(d->btf, next_id);
1525 			multidim = btf_is_array(next_t);
1526 			/* we need space if we have named non-pointer */
1527 			if (fname[0] && !last_was_ptr)
1528 				btf_dump_printf(d, " ");
1529 			/* no parentheses for multi-dimensional array */
1530 			if (!multidim)
1531 				btf_dump_printf(d, "(");
1532 			btf_dump_emit_type_chain(d, decls, fname, lvl);
1533 			if (!multidim)
1534 				btf_dump_printf(d, ")");
1535 			btf_dump_printf(d, "[%u]", a->nelems);
1536 			return;
1537 		}
1538 		case BTF_KIND_FUNC_PROTO: {
1539 			const struct btf_param *p = btf_params(t);
1540 			__u16 vlen = btf_vlen(t);
1541 			int i;
1542 
1543 			/*
1544 			 * GCC emits extra volatile qualifier for
1545 			 * __attribute__((noreturn)) function pointers. Clang
1546 			 * doesn't do it. It's a GCC quirk for backwards
1547 			 * compatibility with code written for GCC <2.5. So,
1548 			 * similarly to extra qualifiers for array, just drop
1549 			 * them, instead of handling them.
1550 			 */
1551 			btf_dump_drop_mods(d, decls);
1552 			if (decls->cnt) {
1553 				btf_dump_printf(d, " (");
1554 				btf_dump_emit_type_chain(d, decls, fname, lvl);
1555 				btf_dump_printf(d, ")");
1556 			} else {
1557 				btf_dump_emit_name(d, fname, last_was_ptr);
1558 			}
1559 			btf_dump_printf(d, "(");
1560 			/*
1561 			 * Clang for BPF target generates func_proto with no
1562 			 * args as a func_proto with a single void arg (e.g.,
1563 			 * `int (*f)(void)` vs just `int (*f)()`). We are
1564 			 * going to pretend there are no args for such case.
1565 			 */
1566 			if (vlen == 1 && p->type == 0) {
1567 				btf_dump_printf(d, ")");
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) */
btf_dump_emit_type_cast(struct btf_dump * d,__u32 id,bool top_level)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 */
btf_dump_name_dups(struct btf_dump * d,struct hashmap * name_map,const char * orig_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 
btf_dump_resolve_name(struct btf_dump * d,__u32 id,struct hashmap * name_map)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 
btf_dump_type_name(struct btf_dump * d,__u32 id)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 
btf_dump_ident_name(struct btf_dump * d,__u32 id)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 
btf_dump_data_newline(struct btf_dump * d)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 
btf_dump_data_delim(struct btf_dump * d)1713 static const char *btf_dump_data_delim(struct btf_dump *d)
1714 {
1715 	return d->typed_dump->depth == 0 ? "" : ",";
1716 }
1717 
btf_dump_data_pfx(struct btf_dump * d)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 
btf_dump_unsupported_data(struct btf_dump * d,const struct btf_type * t,__u32 id)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 
btf_dump_get_bitfield_value(struct btf_dump * d,const struct btf_type * t,const void * data,__u8 bits_offset,__u8 bit_sz,__u64 * value)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 
btf_dump_bitfield_check_zero(struct btf_dump * d,const struct btf_type * t,const void * data,__u8 bits_offset,__u8 bit_sz)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 
btf_dump_bitfield_data(struct btf_dump * d,const struct btf_type * t,const void * data,__u8 bits_offset,__u8 bit_sz)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 */
btf_dump_base_type_check_zero(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)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 
ptr_is_aligned(const struct btf * btf,__u32 type_id,const void * data)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 
btf_dump_int_data(struct btf_dump * d,const struct btf_type * t,__u32 type_id,const void * data,__u8 bits_offset)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 
btf_dump_float_data(struct btf_dump * d,const struct btf_type * t,__u32 type_id,const void * data)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 
btf_dump_var_data(struct btf_dump * d,const struct btf_type * v,__u32 id,const void * data)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 
btf_dump_array_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)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 
btf_dump_struct_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)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 
btf_dump_ptr_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)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 
btf_dump_get_enum_value(struct btf_dump * d,const struct btf_type * t,const void * data,__u32 id,__s64 * value)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 
btf_dump_enum_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)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 
btf_dump_datasec_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)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. */
btf_dump_type_data_check_overflow(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data,__u8 bits_offset,__u8 bit_sz)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 
btf_dump_type_data_check_zero(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data,__u8 bits_offset,__u8 bit_sz)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. */
btf_dump_dump_type_data(struct btf_dump * d,const char * fname,const struct btf_type * t,__u32 id,const void * data,__u8 bits_offset,__u8 bit_sz)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 
btf_dump__dump_type_data(struct btf_dump * d,__u32 id,const void * data,size_t data_sz,const struct btf_dump_type_data_opts * opts)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