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
3477 return ERR_PTR(err);3477 return ERR_PTR(err);
3478}3478}
3479 3479
3480extern char __weak _binary__btf_vmlinux_bin_start[];3480extern char __weak __start_BTF[];
3481extern char __weak _binary__btf_vmlinux_bin_end[];3481extern char __weak __stop_BTF[];
3482extern struct btf *btf_vmlinux;3482extern 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)
kernel/bpf/sysfs_btf.c+5−6
@@ -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 */
12extern char __weak _binary__btf_vmlinux_bin_start[];12extern char __weak __start_BTF[];
13extern char __weak _binary__btf_vmlinux_bin_end[];13extern char __weak __stop_BTF[];
14 14
15static ssize_t15static ssize_t
16btf_vmlinux_read(struct file *file, struct kobject *kobj,16btf_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
31static int __init btf_vmlinux_init(void)31static 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}

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