xref: /linux/kernel/bpf/sysfs_btf.c (revision 2ab002c755bfa88777e3f2db884d531f3010736c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Provide kernel BTF information for introspection and use by eBPF tools.
4  */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/kobject.h>
8 #include <linux/init.h>
9 #include <linux/sysfs.h>
10 
11 /* See scripts/link-vmlinux.sh, gen_btf() func for details */
12 extern char __start_BTF[];
13 extern char __stop_BTF[];
14 
15 static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
16 	.attr = { .name = "vmlinux", .mode = 0444, },
17 	.read_new = sysfs_bin_attr_simple_read,
18 };
19 
20 struct kobject *btf_kobj;
21 
22 static int __init btf_vmlinux_init(void)
23 {
24 	bin_attr_btf_vmlinux.private = __start_BTF;
25 	bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
26 
27 	if (bin_attr_btf_vmlinux.size == 0)
28 		return 0;
29 
30 	btf_kobj = kobject_create_and_add("btf", kernel_kobj);
31 	if (!btf_kobj)
32 		return -ENOMEM;
33 
34 	return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux);
35 }
36 
37 subsys_initcall(btf_vmlinux_init);
38