xref: /linux/tools/bpf/bpftool/gen.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Facebook */
3 
4 #ifndef _GNU_SOURCE
5 #define _GNU_SOURCE
6 #endif
7 #include <ctype.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <libgen.h>
11 #include <linux/err.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <bpf/bpf.h>
17 #include <bpf/libbpf.h>
18 #include <bpf/libbpf_internal.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <bpf/btf.h>
23 
24 #include "json_writer.h"
25 #include "main.h"
26 
27 #define MAX_OBJ_NAME_LEN 64
28 
29 static void sanitize_identifier(char *name)
30 {
31 	int i;
32 
33 	for (i = 0; name[i]; i++)
34 		if (!isalnum(name[i]) && name[i] != '_')
35 			name[i] = '_';
36 }
37 
38 static bool str_has_prefix(const char *str, const char *prefix)
39 {
40 	return strncmp(str, prefix, strlen(prefix)) == 0;
41 }
42 
43 static bool str_has_suffix(const char *str, const char *suffix)
44 {
45 	size_t i, n1 = strlen(str), n2 = strlen(suffix);
46 
47 	if (n1 < n2)
48 		return false;
49 
50 	for (i = 0; i < n2; i++) {
51 		if (str[n1 - i - 1] != suffix[n2 - i - 1])
52 			return false;
53 	}
54 
55 	return true;
56 }
57 
58 static const struct btf_type *
59 resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
60 {
61 	const struct btf_type *t;
62 
63 	t = skip_mods_and_typedefs(btf, id, NULL);
64 	if (!btf_is_ptr(t))
65 		return NULL;
66 
67 	t = skip_mods_and_typedefs(btf, t->type, res_id);
68 
69 	return btf_is_func_proto(t) ? t : NULL;
70 }
71 
72 static void get_obj_name(char *name, const char *file)
73 {
74 	char file_copy[PATH_MAX];
75 
76 	/* Using basename() POSIX version to be more portable. */
77 	strncpy(file_copy, file, PATH_MAX - 1)[PATH_MAX - 1] = '\0';
78 	strncpy(name, basename(file_copy), MAX_OBJ_NAME_LEN - 1)[MAX_OBJ_NAME_LEN - 1] = '\0';
79 	if (str_has_suffix(name, ".o"))
80 		name[strlen(name) - 2] = '\0';
81 	sanitize_identifier(name);
82 }
83 
84 static void get_header_guard(char *guard, const char *obj_name, const char *suffix)
85 {
86 	int i;
87 
88 	sprintf(guard, "__%s_%s__", obj_name, suffix);
89 	for (i = 0; guard[i]; i++)
90 		guard[i] = toupper(guard[i]);
91 }
92 
93 static bool get_map_ident(const struct bpf_map *map, char *buf, size_t buf_sz)
94 {
95 	static const char *sfxs[] = { ".data", ".rodata", ".bss", ".kconfig" };
96 	const char *name = bpf_map__name(map);
97 	int i, n;
98 
99 	if (!bpf_map__is_internal(map)) {
100 		snprintf(buf, buf_sz, "%s", name);
101 		return true;
102 	}
103 
104 	for  (i = 0, n = ARRAY_SIZE(sfxs); i < n; i++) {
105 		const char *sfx = sfxs[i], *p;
106 
107 		p = strstr(name, sfx);
108 		if (p) {
109 			snprintf(buf, buf_sz, "%s", p + 1);
110 			sanitize_identifier(buf);
111 			return true;
112 		}
113 	}
114 
115 	return false;
116 }
117 
118 static bool get_datasec_ident(const char *sec_name, char *buf, size_t buf_sz)
119 {
120 	static const char *pfxs[] = { ".data", ".rodata", ".bss", ".kconfig" };
121 	int i, n;
122 
123 	/* recognize hard coded LLVM section name */
124 	if (strcmp(sec_name, ".addr_space.1") == 0) {
125 		/* this is the name to use in skeleton */
126 		snprintf(buf, buf_sz, "arena");
127 		return true;
128 	}
129 	for  (i = 0, n = ARRAY_SIZE(pfxs); i < n; i++) {
130 		const char *pfx = pfxs[i];
131 
132 		if (str_has_prefix(sec_name, pfx)) {
133 			snprintf(buf, buf_sz, "%s", sec_name + 1);
134 			sanitize_identifier(buf);
135 			return true;
136 		}
137 	}
138 
139 	return false;
140 }
141 
142 static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args)
143 {
144 	vprintf(fmt, args);
145 }
146 
147 static int codegen_datasec_def(struct bpf_object *obj,
148 			       struct btf *btf,
149 			       struct btf_dump *d,
150 			       const struct btf_type *sec,
151 			       const char *obj_name)
152 {
153 	const char *sec_name = btf__name_by_offset(btf, sec->name_off);
154 	const struct btf_var_secinfo *sec_var = btf_var_secinfos(sec);
155 	int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec);
156 	char var_ident[256], sec_ident[256];
157 	bool strip_mods = false;
158 
159 	if (!get_datasec_ident(sec_name, sec_ident, sizeof(sec_ident)))
160 		return 0;
161 
162 	if (strcmp(sec_name, ".kconfig") != 0)
163 		strip_mods = true;
164 
165 	printf("	struct %s__%s {\n", obj_name, sec_ident);
166 	for (i = 0; i < vlen; i++, sec_var++) {
167 		const struct btf_type *var = btf__type_by_id(btf, sec_var->type);
168 		const char *var_name = btf__name_by_offset(btf, var->name_off);
169 		DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts,
170 			.field_name = var_ident,
171 			.indent_level = 2,
172 			.strip_mods = strip_mods,
173 		);
174 		int need_off = sec_var->offset, align_off, align;
175 		__u32 var_type_id = var->type;
176 
177 		/* static variables are not exposed through BPF skeleton */
178 		if (btf_var(var)->linkage == BTF_VAR_STATIC)
179 			continue;
180 
181 		if (off > need_off) {
182 			p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n",
183 			      sec_name, i, need_off, off);
184 			return -EINVAL;
185 		}
186 
187 		align = btf__align_of(btf, var->type);
188 		if (align <= 0) {
189 			p_err("Failed to determine alignment of variable '%s': %d",
190 			      var_name, align);
191 			return -EINVAL;
192 		}
193 		/* Assume 32-bit architectures when generating data section
194 		 * struct memory layout. Given bpftool can't know which target
195 		 * host architecture it's emitting skeleton for, we need to be
196 		 * conservative and assume 32-bit one to ensure enough padding
197 		 * bytes are generated for pointer and long types. This will
198 		 * still work correctly for 64-bit architectures, because in
199 		 * the worst case we'll generate unnecessary padding field,
200 		 * which on 64-bit architectures is not strictly necessary and
201 		 * would be handled by natural 8-byte alignment. But it still
202 		 * will be a correct memory layout, based on recorded offsets
203 		 * in BTF.
204 		 */
205 		if (align > 4)
206 			align = 4;
207 
208 		align_off = (off + align - 1) / align * align;
209 		if (align_off != need_off) {
210 			printf("\t\tchar __pad%d[%d];\n",
211 			       pad_cnt, need_off - off);
212 			pad_cnt++;
213 		}
214 
215 		/* sanitize variable name, e.g., for static vars inside
216 		 * a function, it's name is '<function name>.<variable name>',
217 		 * which we'll turn into a '<function name>_<variable name>'
218 		 */
219 		var_ident[0] = '\0';
220 		strncat(var_ident, var_name, sizeof(var_ident) - 1);
221 		sanitize_identifier(var_ident);
222 
223 		printf("\t\t");
224 		err = btf_dump__emit_type_decl(d, var_type_id, &opts);
225 		if (err)
226 			return err;
227 		printf(";\n");
228 
229 		off = sec_var->offset + sec_var->size;
230 	}
231 	printf("	} *%s;\n", sec_ident);
232 	return 0;
233 }
234 
235 static const struct btf_type *find_type_for_map(struct btf *btf, const char *map_ident)
236 {
237 	int n = btf__type_cnt(btf), i;
238 	char sec_ident[256];
239 
240 	for (i = 1; i < n; i++) {
241 		const struct btf_type *t = btf__type_by_id(btf, i);
242 		const char *name;
243 
244 		if (!btf_is_datasec(t))
245 			continue;
246 
247 		name = btf__str_by_offset(btf, t->name_off);
248 		if (!get_datasec_ident(name, sec_ident, sizeof(sec_ident)))
249 			continue;
250 
251 		if (strcmp(sec_ident, map_ident) == 0)
252 			return t;
253 	}
254 	return NULL;
255 }
256 
257 static bool is_mmapable_map(const struct bpf_map *map, char *buf, size_t sz)
258 {
259 	size_t tmp_sz;
260 
261 	if (bpf_map__type(map) == BPF_MAP_TYPE_ARENA && bpf_map__initial_value(map, &tmp_sz)) {
262 		snprintf(buf, sz, "arena");
263 		return true;
264 	}
265 
266 	if (!bpf_map__is_internal(map) || !(bpf_map__map_flags(map) & BPF_F_MMAPABLE))
267 		return false;
268 
269 	if (!get_map_ident(map, buf, sz))
270 		return false;
271 
272 	return true;
273 }
274 
275 static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
276 {
277 	struct btf *btf = bpf_object__btf(obj);
278 	struct btf_dump *d;
279 	struct bpf_map *map;
280 	const struct btf_type *sec;
281 	char map_ident[256];
282 	int err = 0;
283 
284 	d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL);
285 	if (!d)
286 		return -errno;
287 
288 	bpf_object__for_each_map(map, obj) {
289 		/* only generate definitions for memory-mapped internal maps */
290 		if (!is_mmapable_map(map, map_ident, sizeof(map_ident)))
291 			continue;
292 
293 		sec = find_type_for_map(btf, map_ident);
294 
295 		/* In some cases (e.g., sections like .rodata.cst16 containing
296 		 * compiler allocated string constants only) there will be
297 		 * special internal maps with no corresponding DATASEC BTF
298 		 * type. In such case, generate empty structs for each such
299 		 * map. It will still be memory-mapped and its contents
300 		 * accessible from user-space through BPF skeleton.
301 		 */
302 		if (!sec) {
303 			printf("	struct %s__%s {\n", obj_name, map_ident);
304 			printf("	} *%s;\n", map_ident);
305 		} else {
306 			err = codegen_datasec_def(obj, btf, d, sec, obj_name);
307 			if (err)
308 				goto out;
309 		}
310 	}
311 
312 
313 out:
314 	btf_dump__free(d);
315 	return err;
316 }
317 
318 static bool btf_is_ptr_to_func_proto(const struct btf *btf,
319 				     const struct btf_type *v)
320 {
321 	return btf_is_ptr(v) && btf_is_func_proto(btf__type_by_id(btf, v->type));
322 }
323 
324 static int codegen_subskel_datasecs(struct bpf_object *obj, const char *obj_name)
325 {
326 	struct btf *btf = bpf_object__btf(obj);
327 	struct btf_dump *d;
328 	struct bpf_map *map;
329 	const struct btf_type *sec, *var;
330 	const struct btf_var_secinfo *sec_var;
331 	int i, err = 0, vlen;
332 	char map_ident[256], sec_ident[256];
333 	bool strip_mods = false, needs_typeof = false;
334 	const char *sec_name, *var_name;
335 	__u32 var_type_id;
336 
337 	d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL);
338 	if (!d)
339 		return -errno;
340 
341 	bpf_object__for_each_map(map, obj) {
342 		/* only generate definitions for memory-mapped internal maps */
343 		if (!is_mmapable_map(map, map_ident, sizeof(map_ident)))
344 			continue;
345 
346 		sec = find_type_for_map(btf, map_ident);
347 		if (!sec)
348 			continue;
349 
350 		sec_name = btf__name_by_offset(btf, sec->name_off);
351 		if (!get_datasec_ident(sec_name, sec_ident, sizeof(sec_ident)))
352 			continue;
353 
354 		strip_mods = strcmp(sec_name, ".kconfig") != 0;
355 		printf("	struct %s__%s {\n", obj_name, sec_ident);
356 
357 		sec_var = btf_var_secinfos(sec);
358 		vlen = btf_vlen(sec);
359 		for (i = 0; i < vlen; i++, sec_var++) {
360 			DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts,
361 				.indent_level = 2,
362 				.strip_mods = strip_mods,
363 				/* we'll print the name separately */
364 				.field_name = "",
365 			);
366 
367 			var = btf__type_by_id(btf, sec_var->type);
368 			var_name = btf__name_by_offset(btf, var->name_off);
369 			var_type_id = var->type;
370 
371 			/* static variables are not exposed through BPF skeleton */
372 			if (btf_var(var)->linkage == BTF_VAR_STATIC)
373 				continue;
374 
375 			/* The datasec member has KIND_VAR but we want the
376 			 * underlying type of the variable (e.g. KIND_INT).
377 			 */
378 			var = skip_mods_and_typedefs(btf, var->type, NULL);
379 
380 			printf("\t\t");
381 			/* Func and array members require special handling.
382 			 * Instead of producing `typename *var`, they produce
383 			 * `typeof(typename) *var`. This allows us to keep a
384 			 * similar syntax where the identifier is just prefixed
385 			 * by *, allowing us to ignore C declaration minutiae.
386 			 */
387 			needs_typeof = btf_is_array(var) || btf_is_ptr_to_func_proto(btf, var);
388 			if (needs_typeof)
389 				printf("__typeof__(");
390 
391 			err = btf_dump__emit_type_decl(d, var_type_id, &opts);
392 			if (err)
393 				goto out;
394 
395 			if (needs_typeof)
396 				printf(")");
397 
398 			printf(" *%s;\n", var_name);
399 		}
400 		printf("	} %s;\n", sec_ident);
401 	}
402 
403 out:
404 	btf_dump__free(d);
405 	return err;
406 }
407 
408 static void codegen(const char *template, ...)
409 {
410 	const char *src, *end;
411 	int skip_tabs = 0, n;
412 	char *s, *dst;
413 	va_list args;
414 	char c;
415 
416 	n = strlen(template);
417 	s = malloc(n + 1);
418 	if (!s)
419 		exit(-1);
420 	src = template;
421 	dst = s;
422 
423 	/* find out "baseline" indentation to skip */
424 	while ((c = *src++)) {
425 		if (c == '\t') {
426 			skip_tabs++;
427 		} else if (c == '\n') {
428 			break;
429 		} else {
430 			p_err("unrecognized character at pos %td in template '%s': '%c'",
431 			      src - template - 1, template, c);
432 			free(s);
433 			exit(-1);
434 		}
435 	}
436 
437 	while (*src) {
438 		/* skip baseline indentation tabs */
439 		for (n = skip_tabs; n > 0; n--, src++) {
440 			if (*src != '\t') {
441 				p_err("not enough tabs at pos %td in template '%s'",
442 				      src - template - 1, template);
443 				free(s);
444 				exit(-1);
445 			}
446 		}
447 		/* trim trailing whitespace */
448 		end = strchrnul(src, '\n');
449 		for (n = end - src; n > 0 && isspace(src[n - 1]); n--)
450 			;
451 		memcpy(dst, src, n);
452 		dst += n;
453 		if (*end)
454 			*dst++ = '\n';
455 		src = *end ? end + 1 : end;
456 	}
457 	*dst++ = '\0';
458 
459 	/* print out using adjusted template */
460 	va_start(args, template);
461 	n = vprintf(s, args);
462 	va_end(args);
463 
464 	free(s);
465 }
466 
467 static void print_hex(const char *data, int data_sz)
468 {
469 	int i, len;
470 
471 	for (i = 0, len = 0; i < data_sz; i++) {
472 		int w = data[i] ? 4 : 2;
473 
474 		len += w;
475 		if (len > 78) {
476 			printf("\\\n");
477 			len = w;
478 		}
479 		if (!data[i])
480 			printf("\\0");
481 		else
482 			printf("\\x%02x", (unsigned char)data[i]);
483 	}
484 }
485 
486 static size_t bpf_map_mmap_sz(const struct bpf_map *map)
487 {
488 	long page_sz = sysconf(_SC_PAGE_SIZE);
489 	size_t map_sz;
490 
491 	map_sz = (size_t)roundup(bpf_map__value_size(map), 8) * bpf_map__max_entries(map);
492 	map_sz = roundup(map_sz, page_sz);
493 	return map_sz;
494 }
495 
496 /* Emit type size asserts for all top-level fields in memory-mapped internal maps. */
497 static void codegen_asserts(struct bpf_object *obj, const char *obj_name)
498 {
499 	struct btf *btf = bpf_object__btf(obj);
500 	struct bpf_map *map;
501 	struct btf_var_secinfo *sec_var;
502 	int i, vlen;
503 	const struct btf_type *sec;
504 	char map_ident[256], var_ident[256];
505 
506 	if (!btf)
507 		return;
508 
509 	codegen("\
510 		\n\
511 		__attribute__((unused)) static void			    \n\
512 		%1$s__assert(struct %1$s *s __attribute__((unused)))	    \n\
513 		{							    \n\
514 		#ifdef __cplusplus					    \n\
515 		#define _Static_assert static_assert			    \n\
516 		#endif							    \n\
517 		", obj_name);
518 
519 	bpf_object__for_each_map(map, obj) {
520 		if (!is_mmapable_map(map, map_ident, sizeof(map_ident)))
521 			continue;
522 
523 		sec = find_type_for_map(btf, map_ident);
524 		if (!sec) {
525 			/* best effort, couldn't find the type for this map */
526 			continue;
527 		}
528 
529 		sec_var = btf_var_secinfos(sec);
530 		vlen =  btf_vlen(sec);
531 
532 		for (i = 0; i < vlen; i++, sec_var++) {
533 			const struct btf_type *var = btf__type_by_id(btf, sec_var->type);
534 			const char *var_name = btf__name_by_offset(btf, var->name_off);
535 			long var_size;
536 
537 			/* static variables are not exposed through BPF skeleton */
538 			if (btf_var(var)->linkage == BTF_VAR_STATIC)
539 				continue;
540 
541 			var_size = btf__resolve_size(btf, var->type);
542 			if (var_size < 0)
543 				continue;
544 
545 			var_ident[0] = '\0';
546 			strncat(var_ident, var_name, sizeof(var_ident) - 1);
547 			sanitize_identifier(var_ident);
548 
549 			printf("\t_Static_assert(sizeof(s->%s->%s) == %ld, \"unexpected size of '%s'\");\n",
550 			       map_ident, var_ident, var_size, var_ident);
551 		}
552 	}
553 	codegen("\
554 		\n\
555 		#ifdef __cplusplus					    \n\
556 		#undef _Static_assert					    \n\
557 		#endif							    \n\
558 		}							    \n\
559 		");
560 }
561 
562 static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
563 {
564 	struct bpf_program *prog;
565 
566 	bpf_object__for_each_program(prog, obj) {
567 		const char *tp_name;
568 
569 		codegen("\
570 			\n\
571 			\n\
572 			static inline int					    \n\
573 			%1$s__%2$s__attach(struct %1$s *skel)			    \n\
574 			{							    \n\
575 				int prog_fd = skel->progs.%2$s.prog_fd;		    \n\
576 			", obj_name, bpf_program__name(prog));
577 
578 		switch (bpf_program__type(prog)) {
579 		case BPF_PROG_TYPE_RAW_TRACEPOINT:
580 			tp_name = strchr(bpf_program__section_name(prog), '/') + 1;
581 			printf("\tint fd = skel_raw_tracepoint_open(\"%s\", prog_fd);\n", tp_name);
582 			break;
583 		case BPF_PROG_TYPE_TRACING:
584 		case BPF_PROG_TYPE_LSM:
585 			if (bpf_program__expected_attach_type(prog) == BPF_TRACE_ITER)
586 				printf("\tint fd = skel_link_create(prog_fd, 0, BPF_TRACE_ITER);\n");
587 			else
588 				printf("\tint fd = skel_raw_tracepoint_open(NULL, prog_fd);\n");
589 			break;
590 		default:
591 			printf("\tint fd = ((void)prog_fd, 0); /* auto-attach not supported */\n");
592 			break;
593 		}
594 		codegen("\
595 			\n\
596 										    \n\
597 				if (fd > 0)					    \n\
598 					skel->links.%1$s_fd = fd;		    \n\
599 				return fd;					    \n\
600 			}							    \n\
601 			", bpf_program__name(prog));
602 	}
603 
604 	codegen("\
605 		\n\
606 									    \n\
607 		static inline int					    \n\
608 		%1$s__attach(struct %1$s *skel)				    \n\
609 		{							    \n\
610 			int ret = 0;					    \n\
611 									    \n\
612 		", obj_name);
613 
614 	bpf_object__for_each_program(prog, obj) {
615 		codegen("\
616 			\n\
617 				ret = ret < 0 ? ret : %1$s__%2$s__attach(skel);   \n\
618 			", obj_name, bpf_program__name(prog));
619 	}
620 
621 	codegen("\
622 		\n\
623 			return ret < 0 ? ret : 0;			    \n\
624 		}							    \n\
625 									    \n\
626 		static inline void					    \n\
627 		%1$s__detach(struct %1$s *skel)				    \n\
628 		{							    \n\
629 		", obj_name);
630 
631 	bpf_object__for_each_program(prog, obj) {
632 		codegen("\
633 			\n\
634 				skel_closenz(skel->links.%1$s_fd);	    \n\
635 			", bpf_program__name(prog));
636 	}
637 
638 	codegen("\
639 		\n\
640 		}							    \n\
641 		");
642 }
643 
644 static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
645 {
646 	struct bpf_program *prog;
647 	struct bpf_map *map;
648 	char ident[256];
649 
650 	codegen("\
651 		\n\
652 		static void						    \n\
653 		%1$s__destroy(struct %1$s *skel)			    \n\
654 		{							    \n\
655 			if (!skel)					    \n\
656 				return;					    \n\
657 			%1$s__detach(skel);				    \n\
658 		",
659 		obj_name);
660 
661 	bpf_object__for_each_program(prog, obj) {
662 		codegen("\
663 			\n\
664 				skel_closenz(skel->progs.%1$s.prog_fd);	    \n\
665 			", bpf_program__name(prog));
666 	}
667 
668 	bpf_object__for_each_map(map, obj) {
669 		if (!get_map_ident(map, ident, sizeof(ident)))
670 			continue;
671 		if (bpf_map__is_internal(map) &&
672 		    (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
673 			printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
674 			       ident, bpf_map_mmap_sz(map));
675 		codegen("\
676 			\n\
677 				skel_closenz(skel->maps.%1$s.map_fd);	    \n\
678 			", ident);
679 	}
680 	codegen("\
681 		\n\
682 			skel_free(skel);				    \n\
683 		}							    \n\
684 		",
685 		obj_name);
686 }
687 
688 static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *header_guard)
689 {
690 	DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
691 	struct bpf_map *map;
692 	char ident[256];
693 	int err = 0;
694 
695 	err = bpf_object__gen_loader(obj, &opts);
696 	if (err)
697 		return err;
698 
699 	err = bpf_object__load(obj);
700 	if (err) {
701 		p_err("failed to load object file");
702 		goto out;
703 	}
704 	/* If there was no error during load then gen_loader_opts
705 	 * are populated with the loader program.
706 	 */
707 
708 	/* finish generating 'struct skel' */
709 	codegen("\
710 		\n\
711 		};							    \n\
712 		", obj_name);
713 
714 
715 	codegen_attach_detach(obj, obj_name);
716 
717 	codegen_destroy(obj, obj_name);
718 
719 	codegen("\
720 		\n\
721 		static inline struct %1$s *				    \n\
722 		%1$s__open(void)					    \n\
723 		{							    \n\
724 			struct %1$s *skel;				    \n\
725 									    \n\
726 			skel = skel_alloc(sizeof(*skel));		    \n\
727 			if (!skel)					    \n\
728 				goto cleanup;				    \n\
729 			skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
730 		",
731 		obj_name, opts.data_sz);
732 	bpf_object__for_each_map(map, obj) {
733 		const void *mmap_data = NULL;
734 		size_t mmap_size = 0;
735 
736 		if (!is_mmapable_map(map, ident, sizeof(ident)))
737 			continue;
738 
739 		codegen("\
740 		\n\
741 			{						    \n\
742 				static const char data[] __attribute__((__aligned__(8))) = \"\\\n\
743 		");
744 		mmap_data = bpf_map__initial_value(map, &mmap_size);
745 		print_hex(mmap_data, mmap_size);
746 		codegen("\
747 		\n\
748 		\";							    \n\
749 									    \n\
750 				skel->%1$s = skel_prep_map_data((void *)data, %2$zd,\n\
751 								sizeof(data) - 1);\n\
752 				if (!skel->%1$s)			    \n\
753 					goto cleanup;			    \n\
754 				skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\
755 			}						    \n\
756 			", ident, bpf_map_mmap_sz(map));
757 	}
758 	codegen("\
759 		\n\
760 			return skel;					    \n\
761 		cleanup:						    \n\
762 			%1$s__destroy(skel);				    \n\
763 			return NULL;					    \n\
764 		}							    \n\
765 									    \n\
766 		static inline int					    \n\
767 		%1$s__load(struct %1$s *skel)				    \n\
768 		{							    \n\
769 			struct bpf_load_and_run_opts opts = {};		    \n\
770 			int err;					    \n\
771 			static const char opts_data[] __attribute__((__aligned__(8))) = \"\\\n\
772 		",
773 		obj_name);
774 	print_hex(opts.data, opts.data_sz);
775 	codegen("\
776 		\n\
777 		\";							    \n\
778 			static const char opts_insn[] __attribute__((__aligned__(8))) = \"\\\n\
779 		");
780 	print_hex(opts.insns, opts.insns_sz);
781 	codegen("\
782 		\n\
783 		\";							    \n\
784 									    \n\
785 			opts.ctx = (struct bpf_loader_ctx *)skel;	    \n\
786 			opts.data_sz = sizeof(opts_data) - 1;		    \n\
787 			opts.data = (void *)opts_data;			    \n\
788 			opts.insns_sz = sizeof(opts_insn) - 1;		    \n\
789 			opts.insns = (void *)opts_insn;			    \n\
790 									    \n\
791 			err = bpf_load_and_run(&opts);			    \n\
792 			if (err < 0)					    \n\
793 				return err;				    \n\
794 		");
795 	bpf_object__for_each_map(map, obj) {
796 		const char *mmap_flags;
797 
798 		if (!is_mmapable_map(map, ident, sizeof(ident)))
799 			continue;
800 
801 		if (bpf_map__map_flags(map) & BPF_F_RDONLY_PROG)
802 			mmap_flags = "PROT_READ";
803 		else
804 			mmap_flags = "PROT_READ | PROT_WRITE";
805 
806 		codegen("\
807 		\n\
808 			skel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value,  \n\
809 							%2$zd, %3$s, skel->maps.%1$s.map_fd);\n\
810 			if (!skel->%1$s)				    \n\
811 				return -ENOMEM;				    \n\
812 			",
813 		       ident, bpf_map_mmap_sz(map), mmap_flags);
814 	}
815 	codegen("\
816 		\n\
817 			return 0;					    \n\
818 		}							    \n\
819 									    \n\
820 		static inline struct %1$s *				    \n\
821 		%1$s__open_and_load(void)				    \n\
822 		{							    \n\
823 			struct %1$s *skel;				    \n\
824 									    \n\
825 			skel = %1$s__open();				    \n\
826 			if (!skel)					    \n\
827 				return NULL;				    \n\
828 			if (%1$s__load(skel)) {				    \n\
829 				%1$s__destroy(skel);			    \n\
830 				return NULL;				    \n\
831 			}						    \n\
832 			return skel;					    \n\
833 		}							    \n\
834 									    \n\
835 		", obj_name);
836 
837 	codegen_asserts(obj, obj_name);
838 
839 	codegen("\
840 		\n\
841 									    \n\
842 		#endif /* %s */						    \n\
843 		",
844 		header_guard);
845 	err = 0;
846 out:
847 	return err;
848 }
849 
850 static void
851 codegen_maps_skeleton(struct bpf_object *obj, size_t map_cnt, bool mmaped)
852 {
853 	struct bpf_map *map;
854 	char ident[256];
855 	size_t i;
856 
857 	if (!map_cnt)
858 		return;
859 
860 	codegen("\
861 		\n\
862 									\n\
863 			/* maps */				    \n\
864 			s->map_cnt = %zu;			    \n\
865 			s->map_skel_sz = sizeof(*s->maps);	    \n\
866 			s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);\n\
867 			if (!s->maps) {				    \n\
868 				err = -ENOMEM;			    \n\
869 				goto err;			    \n\
870 			}					    \n\
871 		",
872 		map_cnt
873 	);
874 	i = 0;
875 	bpf_object__for_each_map(map, obj) {
876 		if (!get_map_ident(map, ident, sizeof(ident)))
877 			continue;
878 
879 		codegen("\
880 			\n\
881 									\n\
882 				s->maps[%zu].name = \"%s\";	    \n\
883 				s->maps[%zu].map = &obj->maps.%s;   \n\
884 			",
885 			i, bpf_map__name(map), i, ident);
886 		/* memory-mapped internal maps */
887 		if (mmaped && is_mmapable_map(map, ident, sizeof(ident))) {
888 			printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n",
889 				i, ident);
890 		}
891 		i++;
892 	}
893 }
894 
895 static void
896 codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_links)
897 {
898 	struct bpf_program *prog;
899 	int i;
900 
901 	if (!prog_cnt)
902 		return;
903 
904 	codegen("\
905 		\n\
906 									\n\
907 			/* programs */				    \n\
908 			s->prog_cnt = %zu;			    \n\
909 			s->prog_skel_sz = sizeof(*s->progs);	    \n\
910 			s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);\n\
911 			if (!s->progs) {			    \n\
912 				err = -ENOMEM;			    \n\
913 				goto err;			    \n\
914 			}					    \n\
915 		",
916 		prog_cnt
917 	);
918 	i = 0;
919 	bpf_object__for_each_program(prog, obj) {
920 		codegen("\
921 			\n\
922 									\n\
923 				s->progs[%1$zu].name = \"%2$s\";    \n\
924 				s->progs[%1$zu].prog = &obj->progs.%2$s;\n\
925 			",
926 			i, bpf_program__name(prog));
927 
928 		if (populate_links) {
929 			codegen("\
930 				\n\
931 					s->progs[%1$zu].link = &obj->links.%2$s;\n\
932 				",
933 				i, bpf_program__name(prog));
934 		}
935 		i++;
936 	}
937 }
938 
939 static int walk_st_ops_shadow_vars(struct btf *btf, const char *ident,
940 				   const struct btf_type *map_type, __u32 map_type_id)
941 {
942 	LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts, .indent_level = 3);
943 	const struct btf_type *member_type;
944 	__u32 offset, next_offset = 0;
945 	const struct btf_member *m;
946 	struct btf_dump *d = NULL;
947 	const char *member_name;
948 	__u32 member_type_id;
949 	int i, err = 0, n;
950 	int size;
951 
952 	d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL);
953 	if (!d)
954 		return -errno;
955 
956 	n = btf_vlen(map_type);
957 	for (i = 0, m = btf_members(map_type); i < n; i++, m++) {
958 		member_type = skip_mods_and_typedefs(btf, m->type, &member_type_id);
959 		member_name = btf__name_by_offset(btf, m->name_off);
960 
961 		offset = m->offset / 8;
962 		if (next_offset < offset)
963 			printf("\t\t\tchar __padding_%d[%d];\n", i, offset - next_offset);
964 
965 		switch (btf_kind(member_type)) {
966 		case BTF_KIND_INT:
967 		case BTF_KIND_FLOAT:
968 		case BTF_KIND_ENUM:
969 		case BTF_KIND_ENUM64:
970 			/* scalar type */
971 			printf("\t\t\t");
972 			opts.field_name = member_name;
973 			err = btf_dump__emit_type_decl(d, member_type_id, &opts);
974 			if (err) {
975 				p_err("Failed to emit type declaration for %s: %d", member_name, err);
976 				goto out;
977 			}
978 			printf(";\n");
979 
980 			size = btf__resolve_size(btf, member_type_id);
981 			if (size < 0) {
982 				p_err("Failed to resolve size of %s: %d\n", member_name, size);
983 				err = size;
984 				goto out;
985 			}
986 
987 			next_offset = offset + size;
988 			break;
989 
990 		case BTF_KIND_PTR:
991 			if (resolve_func_ptr(btf, m->type, NULL)) {
992 				/* Function pointer */
993 				printf("\t\t\tstruct bpf_program *%s;\n", member_name);
994 
995 				next_offset = offset + sizeof(void *);
996 				break;
997 			}
998 			/* All pointer types are unsupported except for
999 			 * function pointers.
1000 			 */
1001 			fallthrough;
1002 
1003 		default:
1004 			/* Unsupported types
1005 			 *
1006 			 * Types other than scalar types and function
1007 			 * pointers are currently not supported in order to
1008 			 * prevent conflicts in the generated code caused
1009 			 * by multiple definitions. For instance, if the
1010 			 * struct type FOO is used in a struct_ops map,
1011 			 * bpftool has to generate definitions for FOO,
1012 			 * which may result in conflicts if FOO is defined
1013 			 * in different skeleton files.
1014 			 */
1015 			size = btf__resolve_size(btf, member_type_id);
1016 			if (size < 0) {
1017 				p_err("Failed to resolve size of %s: %d\n", member_name, size);
1018 				err = size;
1019 				goto out;
1020 			}
1021 			printf("\t\t\tchar __unsupported_%d[%d];\n", i, size);
1022 
1023 			next_offset = offset + size;
1024 			break;
1025 		}
1026 	}
1027 
1028 	/* Cannot fail since it must be a struct type */
1029 	size = btf__resolve_size(btf, map_type_id);
1030 	if (next_offset < (__u32)size)
1031 		printf("\t\t\tchar __padding_end[%d];\n", size - next_offset);
1032 
1033 out:
1034 	btf_dump__free(d);
1035 
1036 	return err;
1037 }
1038 
1039 /* Generate the pointer of the shadow type for a struct_ops map.
1040  *
1041  * This function adds a pointer of the shadow type for a struct_ops map.
1042  * The members of a struct_ops map can be exported through a pointer to a
1043  * shadow type. The user can access these members through the pointer.
1044  *
1045  * A shadow type includes not all members, only members of some types.
1046  * They are scalar types and function pointers. The function pointers are
1047  * translated to the pointer of the struct bpf_program. The scalar types
1048  * are translated to the original type without any modifiers.
1049  *
1050  * Unsupported types will be translated to a char array to occupy the same
1051  * space as the original field, being renamed as __unsupported_*.  The user
1052  * should treat these fields as opaque data.
1053  */
1054 static int gen_st_ops_shadow_type(const char *obj_name, struct btf *btf, const char *ident,
1055 				  const struct bpf_map *map)
1056 {
1057 	const struct btf_type *map_type;
1058 	const char *type_name;
1059 	__u32 map_type_id;
1060 	int err;
1061 
1062 	map_type_id = bpf_map__btf_value_type_id(map);
1063 	if (map_type_id == 0)
1064 		return -EINVAL;
1065 	map_type = btf__type_by_id(btf, map_type_id);
1066 	if (!map_type)
1067 		return -EINVAL;
1068 
1069 	type_name = btf__name_by_offset(btf, map_type->name_off);
1070 
1071 	printf("\t\tstruct %s__%s__%s {\n", obj_name, ident, type_name);
1072 
1073 	err = walk_st_ops_shadow_vars(btf, ident, map_type, map_type_id);
1074 	if (err)
1075 		return err;
1076 
1077 	printf("\t\t} *%s;\n", ident);
1078 
1079 	return 0;
1080 }
1081 
1082 static int gen_st_ops_shadow(const char *obj_name, struct btf *btf, struct bpf_object *obj)
1083 {
1084 	int err, st_ops_cnt = 0;
1085 	struct bpf_map *map;
1086 	char ident[256];
1087 
1088 	if (!btf)
1089 		return 0;
1090 
1091 	/* Generate the pointers to shadow types of
1092 	 * struct_ops maps.
1093 	 */
1094 	bpf_object__for_each_map(map, obj) {
1095 		if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
1096 			continue;
1097 		if (!get_map_ident(map, ident, sizeof(ident)))
1098 			continue;
1099 
1100 		if (st_ops_cnt == 0) /* first struct_ops map */
1101 			printf("\tstruct {\n");
1102 		st_ops_cnt++;
1103 
1104 		err = gen_st_ops_shadow_type(obj_name, btf, ident, map);
1105 		if (err)
1106 			return err;
1107 	}
1108 
1109 	if (st_ops_cnt)
1110 		printf("\t} struct_ops;\n");
1111 
1112 	return 0;
1113 }
1114 
1115 /* Generate the code to initialize the pointers of shadow types. */
1116 static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
1117 {
1118 	struct bpf_map *map;
1119 	char ident[256];
1120 
1121 	if (!btf)
1122 		return;
1123 
1124 	/* Initialize the pointers to_ops shadow types of
1125 	 * struct_ops maps.
1126 	 */
1127 	bpf_object__for_each_map(map, obj) {
1128 		if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
1129 			continue;
1130 		if (!get_map_ident(map, ident, sizeof(ident)))
1131 			continue;
1132 		codegen("\
1133 			\n\
1134 				obj->struct_ops.%1$s = (__typeof__(obj->struct_ops.%1$s))\n\
1135 					bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
1136 			\n\
1137 			", ident);
1138 	}
1139 }
1140 
1141 static int do_skeleton(int argc, char **argv)
1142 {
1143 	char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
1144 	size_t map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz;
1145 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
1146 	char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data;
1147 	struct bpf_object *obj = NULL;
1148 	const char *file;
1149 	char ident[256];
1150 	struct bpf_program *prog;
1151 	int fd, err = -1;
1152 	struct bpf_map *map;
1153 	struct btf *btf;
1154 	struct stat st;
1155 
1156 	if (!REQ_ARGS(1)) {
1157 		usage();
1158 		return -1;
1159 	}
1160 	file = GET_ARG();
1161 
1162 	while (argc) {
1163 		if (!REQ_ARGS(2))
1164 			return -1;
1165 
1166 		if (is_prefix(*argv, "name")) {
1167 			NEXT_ARG();
1168 
1169 			if (obj_name[0] != '\0') {
1170 				p_err("object name already specified");
1171 				return -1;
1172 			}
1173 
1174 			strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1);
1175 			obj_name[MAX_OBJ_NAME_LEN - 1] = '\0';
1176 		} else {
1177 			p_err("unknown arg %s", *argv);
1178 			return -1;
1179 		}
1180 
1181 		NEXT_ARG();
1182 	}
1183 
1184 	if (argc) {
1185 		p_err("extra unknown arguments");
1186 		return -1;
1187 	}
1188 
1189 	if (stat(file, &st)) {
1190 		p_err("failed to stat() %s: %s", file, strerror(errno));
1191 		return -1;
1192 	}
1193 	file_sz = st.st_size;
1194 	mmap_sz = roundup(file_sz, sysconf(_SC_PAGE_SIZE));
1195 	fd = open(file, O_RDONLY);
1196 	if (fd < 0) {
1197 		p_err("failed to open() %s: %s", file, strerror(errno));
1198 		return -1;
1199 	}
1200 	obj_data = mmap(NULL, mmap_sz, PROT_READ, MAP_PRIVATE, fd, 0);
1201 	if (obj_data == MAP_FAILED) {
1202 		obj_data = NULL;
1203 		p_err("failed to mmap() %s: %s", file, strerror(errno));
1204 		goto out;
1205 	}
1206 	if (obj_name[0] == '\0')
1207 		get_obj_name(obj_name, file);
1208 	opts.object_name = obj_name;
1209 	if (verifier_logs)
1210 		/* log_level1 + log_level2 + stats, but not stable UAPI */
1211 		opts.kernel_log_level = 1 + 2 + 4;
1212 	obj = bpf_object__open_mem(obj_data, file_sz, &opts);
1213 	if (!obj) {
1214 		char err_buf[256];
1215 
1216 		err = -errno;
1217 		libbpf_strerror(err, err_buf, sizeof(err_buf));
1218 		p_err("failed to open BPF object file: %s", err_buf);
1219 		goto out;
1220 	}
1221 
1222 	bpf_object__for_each_map(map, obj) {
1223 		if (!get_map_ident(map, ident, sizeof(ident))) {
1224 			p_err("ignoring unrecognized internal map '%s'...",
1225 			      bpf_map__name(map));
1226 			continue;
1227 		}
1228 		map_cnt++;
1229 	}
1230 	bpf_object__for_each_program(prog, obj) {
1231 		prog_cnt++;
1232 	}
1233 
1234 	get_header_guard(header_guard, obj_name, "SKEL_H");
1235 	if (use_loader) {
1236 		codegen("\
1237 		\n\
1238 		/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */   \n\
1239 		/* THIS FILE IS AUTOGENERATED BY BPFTOOL! */		    \n\
1240 		#ifndef %2$s						    \n\
1241 		#define %2$s						    \n\
1242 									    \n\
1243 		#include <bpf/skel_internal.h>				    \n\
1244 									    \n\
1245 		struct %1$s {						    \n\
1246 			struct bpf_loader_ctx ctx;			    \n\
1247 		",
1248 		obj_name, header_guard
1249 		);
1250 	} else {
1251 		codegen("\
1252 		\n\
1253 		/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */   \n\
1254 									    \n\
1255 		/* THIS FILE IS AUTOGENERATED BY BPFTOOL! */		    \n\
1256 		#ifndef %2$s						    \n\
1257 		#define %2$s						    \n\
1258 									    \n\
1259 		#include <errno.h>					    \n\
1260 		#include <stdlib.h>					    \n\
1261 		#include <bpf/libbpf.h>					    \n\
1262 									    \n\
1263 		struct %1$s {						    \n\
1264 			struct bpf_object_skeleton *skeleton;		    \n\
1265 			struct bpf_object *obj;				    \n\
1266 		",
1267 		obj_name, header_guard
1268 		);
1269 	}
1270 
1271 	if (map_cnt) {
1272 		printf("\tstruct {\n");
1273 		bpf_object__for_each_map(map, obj) {
1274 			if (!get_map_ident(map, ident, sizeof(ident)))
1275 				continue;
1276 			if (use_loader)
1277 				printf("\t\tstruct bpf_map_desc %s;\n", ident);
1278 			else
1279 				printf("\t\tstruct bpf_map *%s;\n", ident);
1280 		}
1281 		printf("\t} maps;\n");
1282 	}
1283 
1284 	btf = bpf_object__btf(obj);
1285 	err = gen_st_ops_shadow(obj_name, btf, obj);
1286 	if (err)
1287 		goto out;
1288 
1289 	if (prog_cnt) {
1290 		printf("\tstruct {\n");
1291 		bpf_object__for_each_program(prog, obj) {
1292 			if (use_loader)
1293 				printf("\t\tstruct bpf_prog_desc %s;\n",
1294 				       bpf_program__name(prog));
1295 			else
1296 				printf("\t\tstruct bpf_program *%s;\n",
1297 				       bpf_program__name(prog));
1298 		}
1299 		printf("\t} progs;\n");
1300 		printf("\tstruct {\n");
1301 		bpf_object__for_each_program(prog, obj) {
1302 			if (use_loader)
1303 				printf("\t\tint %s_fd;\n",
1304 				       bpf_program__name(prog));
1305 			else
1306 				printf("\t\tstruct bpf_link *%s;\n",
1307 				       bpf_program__name(prog));
1308 		}
1309 		printf("\t} links;\n");
1310 	}
1311 
1312 	if (btf) {
1313 		err = codegen_datasecs(obj, obj_name);
1314 		if (err)
1315 			goto out;
1316 	}
1317 	if (use_loader) {
1318 		err = gen_trace(obj, obj_name, header_guard);
1319 		goto out;
1320 	}
1321 
1322 	codegen("\
1323 		\n\
1324 									    \n\
1325 		#ifdef __cplusplus					    \n\
1326 			static inline struct %1$s *open(const struct bpf_object_open_opts *opts = nullptr);\n\
1327 			static inline struct %1$s *open_and_load();	    \n\
1328 			static inline int load(struct %1$s *skel);	    \n\
1329 			static inline int attach(struct %1$s *skel);	    \n\
1330 			static inline void detach(struct %1$s *skel);	    \n\
1331 			static inline void destroy(struct %1$s *skel);	    \n\
1332 			static inline const void *elf_bytes(size_t *sz);    \n\
1333 		#endif /* __cplusplus */				    \n\
1334 		};							    \n\
1335 									    \n\
1336 		static void						    \n\
1337 		%1$s__destroy(struct %1$s *obj)				    \n\
1338 		{							    \n\
1339 			if (!obj)					    \n\
1340 				return;					    \n\
1341 			if (obj->skeleton)				    \n\
1342 				bpf_object__destroy_skeleton(obj->skeleton);\n\
1343 			free(obj);					    \n\
1344 		}							    \n\
1345 									    \n\
1346 		static inline int					    \n\
1347 		%1$s__create_skeleton(struct %1$s *obj);		    \n\
1348 									    \n\
1349 		static inline struct %1$s *				    \n\
1350 		%1$s__open_opts(const struct bpf_object_open_opts *opts)    \n\
1351 		{							    \n\
1352 			struct %1$s *obj;				    \n\
1353 			int err;					    \n\
1354 									    \n\
1355 			obj = (struct %1$s *)calloc(1, sizeof(*obj));	    \n\
1356 			if (!obj) {					    \n\
1357 				errno = ENOMEM;				    \n\
1358 				return NULL;				    \n\
1359 			}						    \n\
1360 									    \n\
1361 			err = %1$s__create_skeleton(obj);		    \n\
1362 			if (err)					    \n\
1363 				goto err_out;				    \n\
1364 									    \n\
1365 			err = bpf_object__open_skeleton(obj->skeleton, opts);\n\
1366 			if (err)					    \n\
1367 				goto err_out;				    \n\
1368 									    \n\
1369 		", obj_name);
1370 
1371 	gen_st_ops_shadow_init(btf, obj);
1372 
1373 	codegen("\
1374 		\n\
1375 			return obj;					    \n\
1376 		err_out:						    \n\
1377 			%1$s__destroy(obj);				    \n\
1378 			errno = -err;					    \n\
1379 			return NULL;					    \n\
1380 		}							    \n\
1381 									    \n\
1382 		static inline struct %1$s *				    \n\
1383 		%1$s__open(void)					    \n\
1384 		{							    \n\
1385 			return %1$s__open_opts(NULL);			    \n\
1386 		}							    \n\
1387 									    \n\
1388 		static inline int					    \n\
1389 		%1$s__load(struct %1$s *obj)				    \n\
1390 		{							    \n\
1391 			return bpf_object__load_skeleton(obj->skeleton);    \n\
1392 		}							    \n\
1393 									    \n\
1394 		static inline struct %1$s *				    \n\
1395 		%1$s__open_and_load(void)				    \n\
1396 		{							    \n\
1397 			struct %1$s *obj;				    \n\
1398 			int err;					    \n\
1399 									    \n\
1400 			obj = %1$s__open();				    \n\
1401 			if (!obj)					    \n\
1402 				return NULL;				    \n\
1403 			err = %1$s__load(obj);				    \n\
1404 			if (err) {					    \n\
1405 				%1$s__destroy(obj);			    \n\
1406 				errno = -err;				    \n\
1407 				return NULL;				    \n\
1408 			}						    \n\
1409 			return obj;					    \n\
1410 		}							    \n\
1411 									    \n\
1412 		static inline int					    \n\
1413 		%1$s__attach(struct %1$s *obj)				    \n\
1414 		{							    \n\
1415 			return bpf_object__attach_skeleton(obj->skeleton);  \n\
1416 		}							    \n\
1417 									    \n\
1418 		static inline void					    \n\
1419 		%1$s__detach(struct %1$s *obj)				    \n\
1420 		{							    \n\
1421 			bpf_object__detach_skeleton(obj->skeleton);	    \n\
1422 		}							    \n\
1423 		",
1424 		obj_name
1425 	);
1426 
1427 	codegen("\
1428 		\n\
1429 									    \n\
1430 		static inline const void *%1$s__elf_bytes(size_t *sz);	    \n\
1431 									    \n\
1432 		static inline int					    \n\
1433 		%1$s__create_skeleton(struct %1$s *obj)			    \n\
1434 		{							    \n\
1435 			struct bpf_object_skeleton *s;			    \n\
1436 			int err;					    \n\
1437 									    \n\
1438 			s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
1439 			if (!s)	{					    \n\
1440 				err = -ENOMEM;				    \n\
1441 				goto err;				    \n\
1442 			}						    \n\
1443 									    \n\
1444 			s->sz = sizeof(*s);				    \n\
1445 			s->name = \"%1$s\";				    \n\
1446 			s->obj = &obj->obj;				    \n\
1447 		",
1448 		obj_name
1449 	);
1450 
1451 	codegen_maps_skeleton(obj, map_cnt, true /*mmaped*/);
1452 	codegen_progs_skeleton(obj, prog_cnt, true /*populate_links*/);
1453 
1454 	codegen("\
1455 		\n\
1456 									    \n\
1457 			s->data = %1$s__elf_bytes(&s->data_sz);		    \n\
1458 									    \n\
1459 			obj->skeleton = s;				    \n\
1460 			return 0;					    \n\
1461 		err:							    \n\
1462 			bpf_object__destroy_skeleton(s);		    \n\
1463 			return err;					    \n\
1464 		}							    \n\
1465 									    \n\
1466 		static inline const void *%1$s__elf_bytes(size_t *sz)	    \n\
1467 		{							    \n\
1468 			static const char data[] __attribute__((__aligned__(8))) = \"\\\n\
1469 		",
1470 		obj_name
1471 	);
1472 
1473 	/* embed contents of BPF object file */
1474 	print_hex(obj_data, file_sz);
1475 
1476 	codegen("\
1477 		\n\
1478 		\";							    \n\
1479 									    \n\
1480 			*sz = sizeof(data) - 1;				    \n\
1481 			return (const void *)data;			    \n\
1482 		}							    \n\
1483 									    \n\
1484 		#ifdef __cplusplus					    \n\
1485 		struct %1$s *%1$s::open(const struct bpf_object_open_opts *opts) { return %1$s__open_opts(opts); }\n\
1486 		struct %1$s *%1$s::open_and_load() { return %1$s__open_and_load(); }	\n\
1487 		int %1$s::load(struct %1$s *skel) { return %1$s__load(skel); }		\n\
1488 		int %1$s::attach(struct %1$s *skel) { return %1$s__attach(skel); }	\n\
1489 		void %1$s::detach(struct %1$s *skel) { %1$s__detach(skel); }		\n\
1490 		void %1$s::destroy(struct %1$s *skel) { %1$s__destroy(skel); }		\n\
1491 		const void *%1$s::elf_bytes(size_t *sz) { return %1$s__elf_bytes(sz); } \n\
1492 		#endif /* __cplusplus */				    \n\
1493 									    \n\
1494 		",
1495 		obj_name);
1496 
1497 	codegen_asserts(obj, obj_name);
1498 
1499 	codegen("\
1500 		\n\
1501 									    \n\
1502 		#endif /* %1$s */					    \n\
1503 		",
1504 		header_guard);
1505 	err = 0;
1506 out:
1507 	bpf_object__close(obj);
1508 	if (obj_data)
1509 		munmap(obj_data, mmap_sz);
1510 	close(fd);
1511 	return err;
1512 }
1513 
1514 /* Subskeletons are like skeletons, except they don't own the bpf_object,
1515  * associated maps, links, etc. Instead, they know about the existence of
1516  * variables, maps, programs and are able to find their locations
1517  * _at runtime_ from an already loaded bpf_object.
1518  *
1519  * This allows for library-like BPF objects to have userspace counterparts
1520  * with access to their own items without having to know anything about the
1521  * final BPF object that the library was linked into.
1522  */
1523 static int do_subskeleton(int argc, char **argv)
1524 {
1525 	char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SUBSKEL_H__")];
1526 	size_t i, len, file_sz, map_cnt = 0, prog_cnt = 0, mmap_sz, var_cnt = 0, var_idx = 0;
1527 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
1528 	char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data;
1529 	struct bpf_object *obj = NULL;
1530 	const char *file, *var_name;
1531 	char ident[256];
1532 	int fd, err = -1, map_type_id;
1533 	const struct bpf_map *map;
1534 	struct bpf_program *prog;
1535 	struct btf *btf;
1536 	const struct btf_type *map_type, *var_type;
1537 	const struct btf_var_secinfo *var;
1538 	struct stat st;
1539 
1540 	if (!REQ_ARGS(1)) {
1541 		usage();
1542 		return -1;
1543 	}
1544 	file = GET_ARG();
1545 
1546 	while (argc) {
1547 		if (!REQ_ARGS(2))
1548 			return -1;
1549 
1550 		if (is_prefix(*argv, "name")) {
1551 			NEXT_ARG();
1552 
1553 			if (obj_name[0] != '\0') {
1554 				p_err("object name already specified");
1555 				return -1;
1556 			}
1557 
1558 			strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1);
1559 			obj_name[MAX_OBJ_NAME_LEN - 1] = '\0';
1560 		} else {
1561 			p_err("unknown arg %s", *argv);
1562 			return -1;
1563 		}
1564 
1565 		NEXT_ARG();
1566 	}
1567 
1568 	if (argc) {
1569 		p_err("extra unknown arguments");
1570 		return -1;
1571 	}
1572 
1573 	if (use_loader) {
1574 		p_err("cannot use loader for subskeletons");
1575 		return -1;
1576 	}
1577 
1578 	if (stat(file, &st)) {
1579 		p_err("failed to stat() %s: %s", file, strerror(errno));
1580 		return -1;
1581 	}
1582 	file_sz = st.st_size;
1583 	mmap_sz = roundup(file_sz, sysconf(_SC_PAGE_SIZE));
1584 	fd = open(file, O_RDONLY);
1585 	if (fd < 0) {
1586 		p_err("failed to open() %s: %s", file, strerror(errno));
1587 		return -1;
1588 	}
1589 	obj_data = mmap(NULL, mmap_sz, PROT_READ, MAP_PRIVATE, fd, 0);
1590 	if (obj_data == MAP_FAILED) {
1591 		obj_data = NULL;
1592 		p_err("failed to mmap() %s: %s", file, strerror(errno));
1593 		goto out;
1594 	}
1595 	if (obj_name[0] == '\0')
1596 		get_obj_name(obj_name, file);
1597 
1598 	/* The empty object name allows us to use bpf_map__name and produce
1599 	 * ELF section names out of it. (".data" instead of "obj.data")
1600 	 */
1601 	opts.object_name = "";
1602 	obj = bpf_object__open_mem(obj_data, file_sz, &opts);
1603 	if (!obj) {
1604 		char err_buf[256];
1605 
1606 		libbpf_strerror(errno, err_buf, sizeof(err_buf));
1607 		p_err("failed to open BPF object file: %s", err_buf);
1608 		obj = NULL;
1609 		goto out;
1610 	}
1611 
1612 	btf = bpf_object__btf(obj);
1613 	if (!btf) {
1614 		err = -1;
1615 		p_err("need btf type information for %s", obj_name);
1616 		goto out;
1617 	}
1618 
1619 	bpf_object__for_each_program(prog, obj) {
1620 		prog_cnt++;
1621 	}
1622 
1623 	/* First, count how many variables we have to find.
1624 	 * We need this in advance so the subskel can allocate the right
1625 	 * amount of storage.
1626 	 */
1627 	bpf_object__for_each_map(map, obj) {
1628 		if (!get_map_ident(map, ident, sizeof(ident)))
1629 			continue;
1630 
1631 		/* Also count all maps that have a name */
1632 		map_cnt++;
1633 
1634 		if (!is_mmapable_map(map, ident, sizeof(ident)))
1635 			continue;
1636 
1637 		map_type_id = bpf_map__btf_value_type_id(map);
1638 		if (map_type_id <= 0) {
1639 			err = map_type_id;
1640 			goto out;
1641 		}
1642 		map_type = btf__type_by_id(btf, map_type_id);
1643 
1644 		var = btf_var_secinfos(map_type);
1645 		len = btf_vlen(map_type);
1646 		for (i = 0; i < len; i++, var++) {
1647 			var_type = btf__type_by_id(btf, var->type);
1648 
1649 			if (btf_var(var_type)->linkage == BTF_VAR_STATIC)
1650 				continue;
1651 
1652 			var_cnt++;
1653 		}
1654 	}
1655 
1656 	get_header_guard(header_guard, obj_name, "SUBSKEL_H");
1657 	codegen("\
1658 	\n\
1659 	/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */	    \n\
1660 									    \n\
1661 	/* THIS FILE IS AUTOGENERATED! */				    \n\
1662 	#ifndef %2$s							    \n\
1663 	#define %2$s							    \n\
1664 									    \n\
1665 	#include <errno.h>						    \n\
1666 	#include <stdlib.h>						    \n\
1667 	#include <bpf/libbpf.h>						    \n\
1668 									    \n\
1669 	struct %1$s {							    \n\
1670 		struct bpf_object *obj;					    \n\
1671 		struct bpf_object_subskeleton *subskel;			    \n\
1672 	", obj_name, header_guard);
1673 
1674 	if (map_cnt) {
1675 		printf("\tstruct {\n");
1676 		bpf_object__for_each_map(map, obj) {
1677 			if (!get_map_ident(map, ident, sizeof(ident)))
1678 				continue;
1679 			printf("\t\tstruct bpf_map *%s;\n", ident);
1680 		}
1681 		printf("\t} maps;\n");
1682 	}
1683 
1684 	err = gen_st_ops_shadow(obj_name, btf, obj);
1685 	if (err)
1686 		goto out;
1687 
1688 	if (prog_cnt) {
1689 		printf("\tstruct {\n");
1690 		bpf_object__for_each_program(prog, obj) {
1691 			printf("\t\tstruct bpf_program *%s;\n",
1692 				bpf_program__name(prog));
1693 		}
1694 		printf("\t} progs;\n");
1695 	}
1696 
1697 	err = codegen_subskel_datasecs(obj, obj_name);
1698 	if (err)
1699 		goto out;
1700 
1701 	/* emit code that will allocate enough storage for all symbols */
1702 	codegen("\
1703 		\n\
1704 									    \n\
1705 		#ifdef __cplusplus					    \n\
1706 			static inline struct %1$s *open(const struct bpf_object *src);\n\
1707 			static inline void destroy(struct %1$s *skel);	    \n\
1708 		#endif /* __cplusplus */				    \n\
1709 		};							    \n\
1710 									    \n\
1711 		static inline void					    \n\
1712 		%1$s__destroy(struct %1$s *skel)			    \n\
1713 		{							    \n\
1714 			if (!skel)					    \n\
1715 				return;					    \n\
1716 			if (skel->subskel)				    \n\
1717 				bpf_object__destroy_subskeleton(skel->subskel);\n\
1718 			free(skel);					    \n\
1719 		}							    \n\
1720 									    \n\
1721 		static inline struct %1$s *				    \n\
1722 		%1$s__open(const struct bpf_object *src)		    \n\
1723 		{							    \n\
1724 			struct %1$s *obj;				    \n\
1725 			struct bpf_object_subskeleton *s;		    \n\
1726 			int err;					    \n\
1727 									    \n\
1728 			obj = (struct %1$s *)calloc(1, sizeof(*obj));	    \n\
1729 			if (!obj) {					    \n\
1730 				err = -ENOMEM;				    \n\
1731 				goto err;				    \n\
1732 			}						    \n\
1733 			s = (struct bpf_object_subskeleton *)calloc(1, sizeof(*s));\n\
1734 			if (!s) {					    \n\
1735 				err = -ENOMEM;				    \n\
1736 				goto err;				    \n\
1737 			}						    \n\
1738 			s->sz = sizeof(*s);				    \n\
1739 			s->obj = src;					    \n\
1740 			s->var_skel_sz = sizeof(*s->vars);		    \n\
1741 			obj->subskel = s;				    \n\
1742 									    \n\
1743 			/* vars */					    \n\
1744 			s->var_cnt = %2$d;				    \n\
1745 			s->vars = (struct bpf_var_skeleton *)calloc(%2$d, sizeof(*s->vars));\n\
1746 			if (!s->vars) {					    \n\
1747 				err = -ENOMEM;				    \n\
1748 				goto err;				    \n\
1749 			}						    \n\
1750 		",
1751 		obj_name, var_cnt
1752 	);
1753 
1754 	/* walk through each symbol and emit the runtime representation */
1755 	bpf_object__for_each_map(map, obj) {
1756 		if (!is_mmapable_map(map, ident, sizeof(ident)))
1757 			continue;
1758 
1759 		map_type_id = bpf_map__btf_value_type_id(map);
1760 		if (map_type_id <= 0)
1761 			/* skip over internal maps with no type*/
1762 			continue;
1763 
1764 		map_type = btf__type_by_id(btf, map_type_id);
1765 		var = btf_var_secinfos(map_type);
1766 		len = btf_vlen(map_type);
1767 		for (i = 0; i < len; i++, var++) {
1768 			var_type = btf__type_by_id(btf, var->type);
1769 			var_name = btf__name_by_offset(btf, var_type->name_off);
1770 
1771 			if (btf_var(var_type)->linkage == BTF_VAR_STATIC)
1772 				continue;
1773 
1774 			/* Note that we use the dot prefix in .data as the
1775 			 * field access operator i.e. maps%s becomes maps.data
1776 			 */
1777 			codegen("\
1778 			\n\
1779 									    \n\
1780 				s->vars[%3$d].name = \"%1$s\";		    \n\
1781 				s->vars[%3$d].map = &obj->maps.%2$s;	    \n\
1782 				s->vars[%3$d].addr = (void **) &obj->%2$s.%1$s;\n\
1783 			", var_name, ident, var_idx);
1784 
1785 			var_idx++;
1786 		}
1787 	}
1788 
1789 	codegen_maps_skeleton(obj, map_cnt, false /*mmaped*/);
1790 	codegen_progs_skeleton(obj, prog_cnt, false /*links*/);
1791 
1792 	codegen("\
1793 		\n\
1794 									    \n\
1795 			err = bpf_object__open_subskeleton(s);		    \n\
1796 			if (err)					    \n\
1797 				goto err;				    \n\
1798 									    \n\
1799 		");
1800 
1801 	gen_st_ops_shadow_init(btf, obj);
1802 
1803 	codegen("\
1804 		\n\
1805 			return obj;					    \n\
1806 		err:							    \n\
1807 			%1$s__destroy(obj);				    \n\
1808 			errno = -err;					    \n\
1809 			return NULL;					    \n\
1810 		}							    \n\
1811 									    \n\
1812 		#ifdef __cplusplus					    \n\
1813 		struct %1$s *%1$s::open(const struct bpf_object *src) { return %1$s__open(src); }\n\
1814 		void %1$s::destroy(struct %1$s *skel) { %1$s__destroy(skel); }\n\
1815 		#endif /* __cplusplus */				    \n\
1816 									    \n\
1817 		#endif /* %2$s */					    \n\
1818 		",
1819 		obj_name, header_guard);
1820 	err = 0;
1821 out:
1822 	bpf_object__close(obj);
1823 	if (obj_data)
1824 		munmap(obj_data, mmap_sz);
1825 	close(fd);
1826 	return err;
1827 }
1828 
1829 static int do_object(int argc, char **argv)
1830 {
1831 	struct bpf_linker *linker;
1832 	const char *output_file, *file;
1833 	int err = 0;
1834 
1835 	if (!REQ_ARGS(2)) {
1836 		usage();
1837 		return -1;
1838 	}
1839 
1840 	output_file = GET_ARG();
1841 
1842 	linker = bpf_linker__new(output_file, NULL);
1843 	if (!linker) {
1844 		p_err("failed to create BPF linker instance");
1845 		return -1;
1846 	}
1847 
1848 	while (argc) {
1849 		file = GET_ARG();
1850 
1851 		err = bpf_linker__add_file(linker, file, NULL);
1852 		if (err) {
1853 			p_err("failed to link '%s': %s (%d)", file, strerror(errno), errno);
1854 			goto out;
1855 		}
1856 	}
1857 
1858 	err = bpf_linker__finalize(linker);
1859 	if (err) {
1860 		p_err("failed to finalize ELF file: %s (%d)", strerror(errno), errno);
1861 		goto out;
1862 	}
1863 
1864 	err = 0;
1865 out:
1866 	bpf_linker__free(linker);
1867 	return err;
1868 }
1869 
1870 static int do_help(int argc, char **argv)
1871 {
1872 	if (json_output) {
1873 		jsonw_null(json_wtr);
1874 		return 0;
1875 	}
1876 
1877 	fprintf(stderr,
1878 		"Usage: %1$s %2$s object OUTPUT_FILE INPUT_FILE [INPUT_FILE...]\n"
1879 		"       %1$s %2$s skeleton FILE [name OBJECT_NAME]\n"
1880 		"       %1$s %2$s subskeleton FILE [name OBJECT_NAME]\n"
1881 		"       %1$s %2$s min_core_btf INPUT OUTPUT OBJECT [OBJECT...]\n"
1882 		"       %1$s %2$s help\n"
1883 		"\n"
1884 		"       " HELP_SPEC_OPTIONS " |\n"
1885 		"                    {-L|--use-loader} }\n"
1886 		"",
1887 		bin_name, "gen");
1888 
1889 	return 0;
1890 }
1891 
1892 static int btf_save_raw(const struct btf *btf, const char *path)
1893 {
1894 	const void *data;
1895 	FILE *f = NULL;
1896 	__u32 data_sz;
1897 	int err = 0;
1898 
1899 	data = btf__raw_data(btf, &data_sz);
1900 	if (!data)
1901 		return -ENOMEM;
1902 
1903 	f = fopen(path, "wb");
1904 	if (!f)
1905 		return -errno;
1906 
1907 	if (fwrite(data, 1, data_sz, f) != data_sz)
1908 		err = -errno;
1909 
1910 	fclose(f);
1911 	return err;
1912 }
1913 
1914 struct btfgen_info {
1915 	struct btf *src_btf;
1916 	struct btf *marked_btf; /* btf structure used to mark used types */
1917 };
1918 
1919 static size_t btfgen_hash_fn(long key, void *ctx)
1920 {
1921 	return key;
1922 }
1923 
1924 static bool btfgen_equal_fn(long k1, long k2, void *ctx)
1925 {
1926 	return k1 == k2;
1927 }
1928 
1929 static void btfgen_free_info(struct btfgen_info *info)
1930 {
1931 	if (!info)
1932 		return;
1933 
1934 	btf__free(info->src_btf);
1935 	btf__free(info->marked_btf);
1936 
1937 	free(info);
1938 }
1939 
1940 static struct btfgen_info *
1941 btfgen_new_info(const char *targ_btf_path)
1942 {
1943 	struct btfgen_info *info;
1944 	int err;
1945 
1946 	info = calloc(1, sizeof(*info));
1947 	if (!info)
1948 		return NULL;
1949 
1950 	info->src_btf = btf__parse(targ_btf_path, NULL);
1951 	if (!info->src_btf) {
1952 		err = -errno;
1953 		p_err("failed parsing '%s' BTF file: %s", targ_btf_path, strerror(errno));
1954 		goto err_out;
1955 	}
1956 
1957 	info->marked_btf = btf__parse(targ_btf_path, NULL);
1958 	if (!info->marked_btf) {
1959 		err = -errno;
1960 		p_err("failed parsing '%s' BTF file: %s", targ_btf_path, strerror(errno));
1961 		goto err_out;
1962 	}
1963 
1964 	return info;
1965 
1966 err_out:
1967 	btfgen_free_info(info);
1968 	errno = -err;
1969 	return NULL;
1970 }
1971 
1972 #define MARKED UINT32_MAX
1973 
1974 static void btfgen_mark_member(struct btfgen_info *info, int type_id, int idx)
1975 {
1976 	const struct btf_type *t = btf__type_by_id(info->marked_btf, type_id);
1977 	struct btf_member *m = btf_members(t) + idx;
1978 
1979 	m->name_off = MARKED;
1980 }
1981 
1982 static int
1983 btfgen_mark_type(struct btfgen_info *info, unsigned int type_id, bool follow_pointers)
1984 {
1985 	const struct btf_type *btf_type = btf__type_by_id(info->src_btf, type_id);
1986 	struct btf_type *cloned_type;
1987 	struct btf_param *param;
1988 	struct btf_array *array;
1989 	int err, i;
1990 
1991 	if (type_id == 0)
1992 		return 0;
1993 
1994 	/* mark type on cloned BTF as used */
1995 	cloned_type = (struct btf_type *) btf__type_by_id(info->marked_btf, type_id);
1996 	cloned_type->name_off = MARKED;
1997 
1998 	/* recursively mark other types needed by it */
1999 	switch (btf_kind(btf_type)) {
2000 	case BTF_KIND_UNKN:
2001 	case BTF_KIND_INT:
2002 	case BTF_KIND_FLOAT:
2003 	case BTF_KIND_ENUM:
2004 	case BTF_KIND_ENUM64:
2005 	case BTF_KIND_STRUCT:
2006 	case BTF_KIND_UNION:
2007 		break;
2008 	case BTF_KIND_PTR:
2009 		if (follow_pointers) {
2010 			err = btfgen_mark_type(info, btf_type->type, follow_pointers);
2011 			if (err)
2012 				return err;
2013 		}
2014 		break;
2015 	case BTF_KIND_CONST:
2016 	case BTF_KIND_RESTRICT:
2017 	case BTF_KIND_VOLATILE:
2018 	case BTF_KIND_TYPEDEF:
2019 		err = btfgen_mark_type(info, btf_type->type, follow_pointers);
2020 		if (err)
2021 			return err;
2022 		break;
2023 	case BTF_KIND_ARRAY:
2024 		array = btf_array(btf_type);
2025 
2026 		/* mark array type */
2027 		err = btfgen_mark_type(info, array->type, follow_pointers);
2028 		/* mark array's index type */
2029 		err = err ? : btfgen_mark_type(info, array->index_type, follow_pointers);
2030 		if (err)
2031 			return err;
2032 		break;
2033 	case BTF_KIND_FUNC_PROTO:
2034 		/* mark ret type */
2035 		err = btfgen_mark_type(info, btf_type->type, follow_pointers);
2036 		if (err)
2037 			return err;
2038 
2039 		/* mark parameters types */
2040 		param = btf_params(btf_type);
2041 		for (i = 0; i < btf_vlen(btf_type); i++) {
2042 			err = btfgen_mark_type(info, param->type, follow_pointers);
2043 			if (err)
2044 				return err;
2045 			param++;
2046 		}
2047 		break;
2048 	/* tells if some other type needs to be handled */
2049 	default:
2050 		p_err("unsupported kind: %s (%d)", btf_kind_str(btf_type), type_id);
2051 		return -EINVAL;
2052 	}
2053 
2054 	return 0;
2055 }
2056 
2057 static int btfgen_record_field_relo(struct btfgen_info *info, struct bpf_core_spec *targ_spec)
2058 {
2059 	struct btf *btf = info->src_btf;
2060 	const struct btf_type *btf_type;
2061 	struct btf_member *btf_member;
2062 	struct btf_array *array;
2063 	unsigned int type_id = targ_spec->root_type_id;
2064 	int idx, err;
2065 
2066 	/* mark root type */
2067 	btf_type = btf__type_by_id(btf, type_id);
2068 	err = btfgen_mark_type(info, type_id, false);
2069 	if (err)
2070 		return err;
2071 
2072 	/* mark types for complex types (arrays, unions, structures) */
2073 	for (int i = 1; i < targ_spec->raw_len; i++) {
2074 		/* skip typedefs and mods */
2075 		while (btf_is_mod(btf_type) || btf_is_typedef(btf_type)) {
2076 			type_id = btf_type->type;
2077 			btf_type = btf__type_by_id(btf, type_id);
2078 		}
2079 
2080 		switch (btf_kind(btf_type)) {
2081 		case BTF_KIND_STRUCT:
2082 		case BTF_KIND_UNION:
2083 			idx = targ_spec->raw_spec[i];
2084 			btf_member = btf_members(btf_type) + idx;
2085 
2086 			/* mark member */
2087 			btfgen_mark_member(info, type_id, idx);
2088 
2089 			/* mark member's type */
2090 			type_id = btf_member->type;
2091 			btf_type = btf__type_by_id(btf, type_id);
2092 			err = btfgen_mark_type(info, type_id, false);
2093 			if (err)
2094 				return err;
2095 			break;
2096 		case BTF_KIND_ARRAY:
2097 			array = btf_array(btf_type);
2098 			type_id = array->type;
2099 			btf_type = btf__type_by_id(btf, type_id);
2100 			break;
2101 		default:
2102 			p_err("unsupported kind: %s (%d)",
2103 			      btf_kind_str(btf_type), btf_type->type);
2104 			return -EINVAL;
2105 		}
2106 	}
2107 
2108 	return 0;
2109 }
2110 
2111 /* Mark types, members, and member types. Compared to btfgen_record_field_relo,
2112  * this function does not rely on the target spec for inferring members, but
2113  * uses the associated BTF.
2114  *
2115  * The `behind_ptr` argument is used to stop marking of composite types reached
2116  * through a pointer. This way, we can keep BTF size in check while providing
2117  * reasonable match semantics.
2118  */
2119 static int btfgen_mark_type_match(struct btfgen_info *info, __u32 type_id, bool behind_ptr)
2120 {
2121 	const struct btf_type *btf_type;
2122 	struct btf *btf = info->src_btf;
2123 	struct btf_type *cloned_type;
2124 	int i, err;
2125 
2126 	if (type_id == 0)
2127 		return 0;
2128 
2129 	btf_type = btf__type_by_id(btf, type_id);
2130 	/* mark type on cloned BTF as used */
2131 	cloned_type = (struct btf_type *)btf__type_by_id(info->marked_btf, type_id);
2132 	cloned_type->name_off = MARKED;
2133 
2134 	switch (btf_kind(btf_type)) {
2135 	case BTF_KIND_UNKN:
2136 	case BTF_KIND_INT:
2137 	case BTF_KIND_FLOAT:
2138 	case BTF_KIND_ENUM:
2139 	case BTF_KIND_ENUM64:
2140 		break;
2141 	case BTF_KIND_STRUCT:
2142 	case BTF_KIND_UNION: {
2143 		struct btf_member *m = btf_members(btf_type);
2144 		__u16 vlen = btf_vlen(btf_type);
2145 
2146 		if (behind_ptr)
2147 			break;
2148 
2149 		for (i = 0; i < vlen; i++, m++) {
2150 			/* mark member */
2151 			btfgen_mark_member(info, type_id, i);
2152 
2153 			/* mark member's type */
2154 			err = btfgen_mark_type_match(info, m->type, false);
2155 			if (err)
2156 				return err;
2157 		}
2158 		break;
2159 	}
2160 	case BTF_KIND_CONST:
2161 	case BTF_KIND_FWD:
2162 	case BTF_KIND_RESTRICT:
2163 	case BTF_KIND_TYPEDEF:
2164 	case BTF_KIND_VOLATILE:
2165 		return btfgen_mark_type_match(info, btf_type->type, behind_ptr);
2166 	case BTF_KIND_PTR:
2167 		return btfgen_mark_type_match(info, btf_type->type, true);
2168 	case BTF_KIND_ARRAY: {
2169 		struct btf_array *array;
2170 
2171 		array = btf_array(btf_type);
2172 		/* mark array type */
2173 		err = btfgen_mark_type_match(info, array->type, false);
2174 		/* mark array's index type */
2175 		err = err ? : btfgen_mark_type_match(info, array->index_type, false);
2176 		if (err)
2177 			return err;
2178 		break;
2179 	}
2180 	case BTF_KIND_FUNC_PROTO: {
2181 		__u16 vlen = btf_vlen(btf_type);
2182 		struct btf_param *param;
2183 
2184 		/* mark ret type */
2185 		err = btfgen_mark_type_match(info, btf_type->type, false);
2186 		if (err)
2187 			return err;
2188 
2189 		/* mark parameters types */
2190 		param = btf_params(btf_type);
2191 		for (i = 0; i < vlen; i++) {
2192 			err = btfgen_mark_type_match(info, param->type, false);
2193 			if (err)
2194 				return err;
2195 			param++;
2196 		}
2197 		break;
2198 	}
2199 	/* tells if some other type needs to be handled */
2200 	default:
2201 		p_err("unsupported kind: %s (%d)", btf_kind_str(btf_type), type_id);
2202 		return -EINVAL;
2203 	}
2204 
2205 	return 0;
2206 }
2207 
2208 /* Mark types, members, and member types. Compared to btfgen_record_field_relo,
2209  * this function does not rely on the target spec for inferring members, but
2210  * uses the associated BTF.
2211  */
2212 static int btfgen_record_type_match_relo(struct btfgen_info *info, struct bpf_core_spec *targ_spec)
2213 {
2214 	return btfgen_mark_type_match(info, targ_spec->root_type_id, false);
2215 }
2216 
2217 static int btfgen_record_type_relo(struct btfgen_info *info, struct bpf_core_spec *targ_spec)
2218 {
2219 	return btfgen_mark_type(info, targ_spec->root_type_id, true);
2220 }
2221 
2222 static int btfgen_record_enumval_relo(struct btfgen_info *info, struct bpf_core_spec *targ_spec)
2223 {
2224 	return btfgen_mark_type(info, targ_spec->root_type_id, false);
2225 }
2226 
2227 static int btfgen_record_reloc(struct btfgen_info *info, struct bpf_core_spec *res)
2228 {
2229 	switch (res->relo_kind) {
2230 	case BPF_CORE_FIELD_BYTE_OFFSET:
2231 	case BPF_CORE_FIELD_BYTE_SIZE:
2232 	case BPF_CORE_FIELD_EXISTS:
2233 	case BPF_CORE_FIELD_SIGNED:
2234 	case BPF_CORE_FIELD_LSHIFT_U64:
2235 	case BPF_CORE_FIELD_RSHIFT_U64:
2236 		return btfgen_record_field_relo(info, res);
2237 	case BPF_CORE_TYPE_ID_LOCAL: /* BPF_CORE_TYPE_ID_LOCAL doesn't require kernel BTF */
2238 		return 0;
2239 	case BPF_CORE_TYPE_ID_TARGET:
2240 	case BPF_CORE_TYPE_EXISTS:
2241 	case BPF_CORE_TYPE_SIZE:
2242 		return btfgen_record_type_relo(info, res);
2243 	case BPF_CORE_TYPE_MATCHES:
2244 		return btfgen_record_type_match_relo(info, res);
2245 	case BPF_CORE_ENUMVAL_EXISTS:
2246 	case BPF_CORE_ENUMVAL_VALUE:
2247 		return btfgen_record_enumval_relo(info, res);
2248 	default:
2249 		return -EINVAL;
2250 	}
2251 }
2252 
2253 static struct bpf_core_cand_list *
2254 btfgen_find_cands(const struct btf *local_btf, const struct btf *targ_btf, __u32 local_id)
2255 {
2256 	const struct btf_type *local_type;
2257 	struct bpf_core_cand_list *cands = NULL;
2258 	struct bpf_core_cand local_cand = {};
2259 	size_t local_essent_len;
2260 	const char *local_name;
2261 	int err;
2262 
2263 	local_cand.btf = local_btf;
2264 	local_cand.id = local_id;
2265 
2266 	local_type = btf__type_by_id(local_btf, local_id);
2267 	if (!local_type) {
2268 		err = -EINVAL;
2269 		goto err_out;
2270 	}
2271 
2272 	local_name = btf__name_by_offset(local_btf, local_type->name_off);
2273 	if (!local_name) {
2274 		err = -EINVAL;
2275 		goto err_out;
2276 	}
2277 	local_essent_len = bpf_core_essential_name_len(local_name);
2278 
2279 	cands = calloc(1, sizeof(*cands));
2280 	if (!cands)
2281 		return NULL;
2282 
2283 	err = bpf_core_add_cands(&local_cand, local_essent_len, targ_btf, "vmlinux", 1, cands);
2284 	if (err)
2285 		goto err_out;
2286 
2287 	return cands;
2288 
2289 err_out:
2290 	bpf_core_free_cands(cands);
2291 	errno = -err;
2292 	return NULL;
2293 }
2294 
2295 /* Record relocation information for a single BPF object */
2296 static int btfgen_record_obj(struct btfgen_info *info, const char *obj_path)
2297 {
2298 	const struct btf_ext_info_sec *sec;
2299 	const struct bpf_core_relo *relo;
2300 	const struct btf_ext_info *seg;
2301 	struct hashmap_entry *entry;
2302 	struct hashmap *cand_cache = NULL;
2303 	struct btf_ext *btf_ext = NULL;
2304 	unsigned int relo_idx;
2305 	struct btf *btf = NULL;
2306 	size_t i;
2307 	int err;
2308 
2309 	btf = btf__parse(obj_path, &btf_ext);
2310 	if (!btf) {
2311 		err = -errno;
2312 		p_err("failed to parse BPF object '%s': %s", obj_path, strerror(errno));
2313 		return err;
2314 	}
2315 
2316 	if (!btf_ext) {
2317 		p_err("failed to parse BPF object '%s': section %s not found",
2318 		      obj_path, BTF_EXT_ELF_SEC);
2319 		err = -EINVAL;
2320 		goto out;
2321 	}
2322 
2323 	if (btf_ext->core_relo_info.len == 0) {
2324 		err = 0;
2325 		goto out;
2326 	}
2327 
2328 	cand_cache = hashmap__new(btfgen_hash_fn, btfgen_equal_fn, NULL);
2329 	if (IS_ERR(cand_cache)) {
2330 		err = PTR_ERR(cand_cache);
2331 		goto out;
2332 	}
2333 
2334 	seg = &btf_ext->core_relo_info;
2335 	for_each_btf_ext_sec(seg, sec) {
2336 		for_each_btf_ext_rec(seg, sec, relo_idx, relo) {
2337 			struct bpf_core_spec specs_scratch[3] = {};
2338 			struct bpf_core_relo_res targ_res = {};
2339 			struct bpf_core_cand_list *cands = NULL;
2340 			const char *sec_name = btf__name_by_offset(btf, sec->sec_name_off);
2341 
2342 			if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
2343 			    !hashmap__find(cand_cache, relo->type_id, &cands)) {
2344 				cands = btfgen_find_cands(btf, info->src_btf, relo->type_id);
2345 				if (!cands) {
2346 					err = -errno;
2347 					goto out;
2348 				}
2349 
2350 				err = hashmap__set(cand_cache, relo->type_id, cands,
2351 						   NULL, NULL);
2352 				if (err)
2353 					goto out;
2354 			}
2355 
2356 			err = bpf_core_calc_relo_insn(sec_name, relo, relo_idx, btf, cands,
2357 						      specs_scratch, &targ_res);
2358 			if (err)
2359 				goto out;
2360 
2361 			/* specs_scratch[2] is the target spec */
2362 			err = btfgen_record_reloc(info, &specs_scratch[2]);
2363 			if (err)
2364 				goto out;
2365 		}
2366 	}
2367 
2368 out:
2369 	btf__free(btf);
2370 	btf_ext__free(btf_ext);
2371 
2372 	if (!IS_ERR_OR_NULL(cand_cache)) {
2373 		hashmap__for_each_entry(cand_cache, entry, i) {
2374 			bpf_core_free_cands(entry->pvalue);
2375 		}
2376 		hashmap__free(cand_cache);
2377 	}
2378 
2379 	return err;
2380 }
2381 
2382 static int btfgen_remap_id(__u32 *type_id, void *ctx)
2383 {
2384 	unsigned int *ids = ctx;
2385 
2386 	*type_id = ids[*type_id];
2387 
2388 	return 0;
2389 }
2390 
2391 /* Generate BTF from relocation information previously recorded */
2392 static struct btf *btfgen_get_btf(struct btfgen_info *info)
2393 {
2394 	struct btf *btf_new = NULL;
2395 	unsigned int *ids = NULL;
2396 	unsigned int i, n = btf__type_cnt(info->marked_btf);
2397 	int err = 0;
2398 
2399 	btf_new = btf__new_empty();
2400 	if (!btf_new) {
2401 		err = -errno;
2402 		goto err_out;
2403 	}
2404 
2405 	ids = calloc(n, sizeof(*ids));
2406 	if (!ids) {
2407 		err = -errno;
2408 		goto err_out;
2409 	}
2410 
2411 	/* first pass: add all marked types to btf_new and add their new ids to the ids map */
2412 	for (i = 1; i < n; i++) {
2413 		const struct btf_type *cloned_type, *type;
2414 		const char *name;
2415 		int new_id;
2416 
2417 		cloned_type = btf__type_by_id(info->marked_btf, i);
2418 
2419 		if (cloned_type->name_off != MARKED)
2420 			continue;
2421 
2422 		type = btf__type_by_id(info->src_btf, i);
2423 
2424 		/* add members for struct and union */
2425 		if (btf_is_composite(type)) {
2426 			struct btf_member *cloned_m, *m;
2427 			unsigned short vlen;
2428 			int idx_src;
2429 
2430 			name = btf__str_by_offset(info->src_btf, type->name_off);
2431 
2432 			if (btf_is_struct(type))
2433 				err = btf__add_struct(btf_new, name, type->size);
2434 			else
2435 				err = btf__add_union(btf_new, name, type->size);
2436 
2437 			if (err < 0)
2438 				goto err_out;
2439 			new_id = err;
2440 
2441 			cloned_m = btf_members(cloned_type);
2442 			m = btf_members(type);
2443 			vlen = btf_vlen(cloned_type);
2444 			for (idx_src = 0; idx_src < vlen; idx_src++, cloned_m++, m++) {
2445 				/* add only members that are marked as used */
2446 				if (cloned_m->name_off != MARKED)
2447 					continue;
2448 
2449 				name = btf__str_by_offset(info->src_btf, m->name_off);
2450 				err = btf__add_field(btf_new, name, m->type,
2451 						     btf_member_bit_offset(cloned_type, idx_src),
2452 						     btf_member_bitfield_size(cloned_type, idx_src));
2453 				if (err < 0)
2454 					goto err_out;
2455 			}
2456 		} else {
2457 			err = btf__add_type(btf_new, info->src_btf, type);
2458 			if (err < 0)
2459 				goto err_out;
2460 			new_id = err;
2461 		}
2462 
2463 		/* add ID mapping */
2464 		ids[i] = new_id;
2465 	}
2466 
2467 	/* second pass: fix up type ids */
2468 	for (i = 1; i < btf__type_cnt(btf_new); i++) {
2469 		struct btf_type *btf_type = (struct btf_type *) btf__type_by_id(btf_new, i);
2470 
2471 		err = btf_type_visit_type_ids(btf_type, btfgen_remap_id, ids);
2472 		if (err)
2473 			goto err_out;
2474 	}
2475 
2476 	free(ids);
2477 	return btf_new;
2478 
2479 err_out:
2480 	btf__free(btf_new);
2481 	free(ids);
2482 	errno = -err;
2483 	return NULL;
2484 }
2485 
2486 /* Create minimized BTF file for a set of BPF objects.
2487  *
2488  * The BTFGen algorithm is divided in two main parts: (1) collect the
2489  * BTF types that are involved in relocations and (2) generate the BTF
2490  * object using the collected types.
2491  *
2492  * In order to collect the types involved in the relocations, we parse
2493  * the BTF and BTF.ext sections of the BPF objects and use
2494  * bpf_core_calc_relo_insn() to get the target specification, this
2495  * indicates how the types and fields are used in a relocation.
2496  *
2497  * Types are recorded in different ways according to the kind of the
2498  * relocation. For field-based relocations only the members that are
2499  * actually used are saved in order to reduce the size of the generated
2500  * BTF file. For type-based relocations empty struct / unions are
2501  * generated and for enum-based relocations the whole type is saved.
2502  *
2503  * The second part of the algorithm generates the BTF object. It creates
2504  * an empty BTF object and fills it with the types recorded in the
2505  * previous step. This function takes care of only adding the structure
2506  * and union members that were marked as used and it also fixes up the
2507  * type IDs on the generated BTF object.
2508  */
2509 static int minimize_btf(const char *src_btf, const char *dst_btf, const char *objspaths[])
2510 {
2511 	struct btfgen_info *info;
2512 	struct btf *btf_new = NULL;
2513 	int err, i;
2514 
2515 	info = btfgen_new_info(src_btf);
2516 	if (!info) {
2517 		err = -errno;
2518 		p_err("failed to allocate info structure: %s", strerror(errno));
2519 		goto out;
2520 	}
2521 
2522 	for (i = 0; objspaths[i] != NULL; i++) {
2523 		err = btfgen_record_obj(info, objspaths[i]);
2524 		if (err) {
2525 			p_err("error recording relocations for %s: %s", objspaths[i],
2526 			      strerror(errno));
2527 			goto out;
2528 		}
2529 	}
2530 
2531 	btf_new = btfgen_get_btf(info);
2532 	if (!btf_new) {
2533 		err = -errno;
2534 		p_err("error generating BTF: %s", strerror(errno));
2535 		goto out;
2536 	}
2537 
2538 	err = btf_save_raw(btf_new, dst_btf);
2539 	if (err) {
2540 		p_err("error saving btf file: %s", strerror(errno));
2541 		goto out;
2542 	}
2543 
2544 out:
2545 	btf__free(btf_new);
2546 	btfgen_free_info(info);
2547 
2548 	return err;
2549 }
2550 
2551 static int do_min_core_btf(int argc, char **argv)
2552 {
2553 	const char *input, *output, **objs;
2554 	int i, err;
2555 
2556 	if (!REQ_ARGS(3)) {
2557 		usage();
2558 		return -1;
2559 	}
2560 
2561 	input = GET_ARG();
2562 	output = GET_ARG();
2563 
2564 	objs = (const char **) calloc(argc + 1, sizeof(*objs));
2565 	if (!objs) {
2566 		p_err("failed to allocate array for object names");
2567 		return -ENOMEM;
2568 	}
2569 
2570 	i = 0;
2571 	while (argc)
2572 		objs[i++] = GET_ARG();
2573 
2574 	err = minimize_btf(input, output, objs);
2575 	free(objs);
2576 	return err;
2577 }
2578 
2579 static const struct cmd cmds[] = {
2580 	{ "object",		do_object },
2581 	{ "skeleton",		do_skeleton },
2582 	{ "subskeleton",	do_subskeleton },
2583 	{ "min_core_btf",	do_min_core_btf},
2584 	{ "help",		do_help },
2585 	{ 0 }
2586 };
2587 
2588 int do_gen(int argc, char **argv)
2589 {
2590 	return cmd_select(cmds, argc, argv, do_help);
2591 }
2592