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