Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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