History

bpf: Support llvm-objcopy for vmlinux BTF

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.

Fangrui Song committed 6 years ago

Commit 90ceddc1 parent 483d7a35 files · +34 −31Browse files

Showing the changes to kernel (2 of 5 files). Show all

Unified · Split

2 files
  1. kernel/bpf/btf.c +4 −5
  2. kernel/bpf/sysfs_btf.c +5 −6
kernel/bpf/btf.c+4−5
@@ -3477,8 +3477,8 @@ errout: · 3476 unmodified lines
34773477 return ERR_PTR(err);
34783478 }
34793479
3480−extern char __weak _binary__btf_vmlinux_bin_start[];
3481−extern char __weak _binary__btf_vmlinux_bin_end[];
3480+extern char __weak __start_BTF[];
3481+extern char __weak __stop_BTF[];
34823482 extern struct btf *btf_vmlinux;
34833483
34843484 #define BPF_MAP_TYPE(_id, _ops)
@@ -3605,9 +3605,8 @@ struct btf *btf_parse_vmlinux(void) · 120 unmodified lines
36053605 }
36063606 env->btf = btf;
36073607
3608− btf->data = _binary__btf_vmlinux_bin_start;
3609− btf->data_size = _binary__btf_vmlinux_bin_end -
3610− _binary__btf_vmlinux_bin_start;
3608+ btf->data = __start_BTF;
3609+ btf->data_size = __stop_BTF - __start_BTF;
36113610
36123611 err = btf_parse_hdr(env);
36133612 if (err)
kernel/bpf/sysfs_btf.c+5−6
@@ -9,15 +9,15 @@ · 8 unmodified lines
99 #include <linux/sysfs.h>
1010
1111 /* See scripts/link-vmlinux.sh, gen_btf() func for details */
12−extern char __weak _binary__btf_vmlinux_bin_start[];
13−extern char __weak _binary__btf_vmlinux_bin_end[];
12+extern char __weak __start_BTF[];
13+extern char __weak __stop_BTF[];
1414
1515 static ssize_t
1616 btf_vmlinux_read(struct file *file, struct kobject *kobj,
1717 struct bin_attribute *bin_attr,
1818 char *buf, loff_t off, size_t len)
1919 {
20− memcpy(buf, _binary__btf_vmlinux_bin_start + off, len);
20+ memcpy(buf, __start_BTF + off, len);
2121 return len;
2222 }
2323
@@ -30,15 +30,14 @@ static struct kobject *btf_kobj; · 6 unmodified lines
3030
3131 static int __init btf_vmlinux_init(void)
3232 {
33− if (!_binary__btf_vmlinux_bin_start)
33+ if (!__start_BTF)
3434 return 0;
3535
3636 btf_kobj = kobject_create_and_add("btf", kernel_kobj);
3737 if (!btf_kobj)
3838 return -ENOMEM;
3939
40− bin_attr_btf_vmlinux.size = _binary__btf_vmlinux_bin_end -
41− _binary__btf_vmlinux_bin_start;
40+ bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
4241
4342 return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux);
4443 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t