Simplify gen_btf logic to make it work with llvm-objcopy. The existing 'file format' and 'architecture' parsing logic is brittle and does not work with llvm-objcopy/llvm-objdump. 'file format' output of llvm-objdump>=11 will match GNU objdump, but 'architecture' (bfdarch) may not. .BTF in .tmp_vmlinux.btf is non-SHF_ALLOC. Add the SHF_ALLOC flag because it is part of vmlinux image used for introspection. C code can reference the section via linker script defined __start_BTF and __stop_BTF. This fixes a small problem that previous .BTF had the SHF_WRITE flag (objcopy -I binary -O elf* synthesized .data). Additionally, `objcopy -I binary` synthesized symbols _binary__btf_vmlinux_bin_start and _binary__btf_vmlinux_bin_stop (not used elsewhere) are replaced with more commonplace __start_BTF and __stop_BTF. Add 2>/dev/null because GNU objcopy (but not llvm-objcopy) warns "empty loadable segment detected at vaddr=0xffffffff81000000, is this intentional?" We use a dd command to change the e_type field in the ELF header from ET_EXEC to ET_REL so that lld will accept .btf.vmlinux.bin.o. Accepting ET_EXEC as an input file is an extremely rare GNU ld feature that lld does not intend to support, because this is error-prone. The output section description .BTF in include/asm-generic/vmlinux.lds.h avoids potential subtle orphan section placement issues and suppresses --orphan-handling=warn warnings.
Showing the changes to kernel (2 of 5 files). Show all
Unified · Split
| @@ -3477,8 +3477,8 @@ errout: · 3476 unmodified lines | |||
| 3477 | return ERR_PTR(err); | 3477 | return ERR_PTR(err); |
| 3478 | } | 3478 | } |
| 3479 | 3479 | ||
| 3480 | extern char __weak _binary__btf_vmlinux_bin_start[]; | 3480 | extern char __weak __start_BTF[]; |
| 3481 | extern char __weak _binary__btf_vmlinux_bin_end[]; | 3481 | extern char __weak __stop_BTF[]; |
| 3482 | extern struct btf *btf_vmlinux; | 3482 | extern struct btf *btf_vmlinux; |
| 3483 | 3483 | ||
| 3484 | #define BPF_MAP_TYPE(_id, _ops) | 3484 | #define BPF_MAP_TYPE(_id, _ops) |
| @@ -3605,9 +3605,8 @@ struct btf *btf_parse_vmlinux(void) · 120 unmodified lines | |||
| 3605 | } | 3605 | } |
| 3606 | env->btf = btf; | 3606 | env->btf = btf; |
| 3607 | 3607 | ||
| 3608 | btf->data = _binary__btf_vmlinux_bin_start; | 3608 | btf->data = __start_BTF; |
| 3609 | btf->data_size = _binary__btf_vmlinux_bin_end - | 3609 | btf->data_size = __stop_BTF - __start_BTF; |
| 3610 | _binary__btf_vmlinux_bin_start; | ||
| 3611 | 3610 | ||
| 3612 | err = btf_parse_hdr(env); | 3611 | err = btf_parse_hdr(env); |
| 3613 | if (err) | 3612 | if (err) |
| @@ -9,15 +9,15 @@ · 8 unmodified lines | |||
| 9 | #include <linux/sysfs.h> | 9 | #include <linux/sysfs.h> |
| 10 | 10 | ||
| 11 | /* See scripts/link-vmlinux.sh, gen_btf() func for details */ | 11 | /* See scripts/link-vmlinux.sh, gen_btf() func for details */ |
| 12 | extern char __weak _binary__btf_vmlinux_bin_start[]; | 12 | extern char __weak __start_BTF[]; |
| 13 | extern char __weak _binary__btf_vmlinux_bin_end[]; | 13 | extern char __weak __stop_BTF[]; |
| 14 | 14 | ||
| 15 | static ssize_t | 15 | static ssize_t |
| 16 | btf_vmlinux_read(struct file *file, struct kobject *kobj, | 16 | btf_vmlinux_read(struct file *file, struct kobject *kobj, |
| 17 | struct bin_attribute *bin_attr, | 17 | struct bin_attribute *bin_attr, |
| 18 | char *buf, loff_t off, size_t len) | 18 | char *buf, loff_t off, size_t len) |
| 19 | { | 19 | { |
| 20 | memcpy(buf, _binary__btf_vmlinux_bin_start + off, len); | 20 | memcpy(buf, __start_BTF + off, len); |
| 21 | return len; | 21 | return len; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| @@ -30,15 +30,14 @@ static struct kobject *btf_kobj; · 6 unmodified lines | |||
| 30 | 30 | ||
| 31 | static int __init btf_vmlinux_init(void) | 31 | static int __init btf_vmlinux_init(void) |
| 32 | { | 32 | { |
| 33 | if (!_binary__btf_vmlinux_bin_start) | 33 | if (!__start_BTF) |
| 34 | return 0; | 34 | return 0; |
| 35 | 35 | ||
| 36 | btf_kobj = kobject_create_and_add("btf", kernel_kobj); | 36 | btf_kobj = kobject_create_and_add("btf", kernel_kobj); |
| 37 | if (!btf_kobj) | 37 | if (!btf_kobj) |
| 38 | return -ENOMEM; | 38 | return -ENOMEM; |
| 39 | 39 | ||
| 40 | bin_attr_btf_vmlinux.size = _binary__btf_vmlinux_bin_end - | 40 | bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF; |
| 41 | _binary__btf_vmlinux_bin_start; | ||
| 42 | 41 | ||
| 43 | return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux); | 42 | return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux); |
| 44 | } | 43 | } |