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