sysfs_btf.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. /* See scripts/link-vmlinux.sh, gen_btf() func for details */
  11. extern char __weak __start_BTF[];
  12. extern char __weak __stop_BTF[];
  13. static ssize_t
  14. btf_vmlinux_read(struct file *file, struct kobject *kobj,
  15. struct bin_attribute *bin_attr,
  16. char *buf, loff_t off, size_t len)
  17. {
  18. memcpy(buf, __start_BTF + off, len);
  19. return len;
  20. }
  21. static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
  22. .attr = { .name = "vmlinux", .mode = 0444, },
  23. .read = btf_vmlinux_read,
  24. };
  25. static struct kobject *btf_kobj;
  26. static int __init btf_vmlinux_init(void)
  27. {
  28. bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
  29. if (!__start_BTF || bin_attr_btf_vmlinux.size == 0)
  30. return 0;
  31. btf_kobj = kobject_create_and_add("btf", kernel_kobj);
  32. if (!btf_kobj)
  33. return -ENOMEM;
  34. return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux);
  35. }
  36. subsys_initcall(btf_vmlinux_init);