bpf: Support llvm-objcopy for vmlinux BTF
Subsystems (6)
PATCH v6 1/1 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.
v6:
- drop llvm-objdump from the title. We don't run objdump now
- delete unused local variables: bin_arch, bin_format and bin_file
- mention in the comment that lld does not allow an ET_EXEC input
- rename BTF back to .BTF . The section name is assumed by bpftool
- add output section description to include/asm-generic/vmlinux.lds.h
- mention cb0cc635c7a9 ("powerpc: Include .BTF section")
v5:
- rebase on top of bpf-next/master
- rename .BTF to BTF
--- arch/powerpc/kernel/vmlinux.lds.S | 6 ------ include/asm-generic/vmlinux.lds.h | 15 +++++++++++++++ kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 24 ++++++++++-------------- 5 files changed, 34 insertions(+), 31 deletions(-)
arch/powerpc/kernel/vmlinux.lds.S +0 -6
@@ -303,12 +303,6 @@ SECTIONS *(.branch_lt) } -#ifdef CONFIG_DEBUG_INFO_BTF - .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { - *(.BTF) - } -#endif - .opd : AT(ADDR(.opd) - LOAD_OFFSET) { __start_opd = .; KEEP(*(.opd))
include/asm-generic/vmlinux.lds.h +15 -0
@@ -535,6 +535,7 @@ \RO_EXCEPTION_TABLE \ NOTES \ +BTF \ \ . = ALIGN((align)); \ __end_rodata = .;
@@ -621,6 +622,20 @@ __stop___ex_table = .; \ } +/* +* .BTF +*/ +#ifdef CONFIG_DEBUG_INFO_BTF +#define BTF \ + .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \ + __start_BTF = .; \ + *(.BTF) \ + __stop_BTF = .; \ + } +#else +#define BTF +#endif +/* * Init task */
kernel/bpf/btf.c +4 -5
@@ -3477,8 +3477,8 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,return ERR_PTR (err); } -extern char __weak _binary__btf_vmlinux_bin_start[]; -extern char __weak _binary__btf_vmlinux_bin_end[]; +extern char __weak __start_BTF[]; +extern char __weak __stop_BTF[];extern struct btf *btf_vmlinux;#define BPF_MAP_TYPE (_id, _ops)
@@ -3605,9 +3605,8 @@ struct btf *btf_parse_vmlinux(void) } env->btf = btf; - btf->data = _binary__btf_vmlinux_bin_start; - btf->data_size = _binary__btf_vmlinux_bin_end - - _binary__btf_vmlinux_bin_start; + btf->data = __start_BTF; + btf->data_size = __stop_BTF - __start_BTF; err =btf_parse_hdr (env);if (err)
kernel/bpf/sysfs_btf.c +5 -6
@@ -9,15 +9,15 @@#include <linux/sysfs.h> /* See scripts/link-vmlinux.sh, gen_btf() func for details */ -extern char __weak _binary__btf_vmlinux_bin_start[]; -extern char __weak _binary__btf_vmlinux_bin_end[]; +extern char __weak __start_BTF[]; +extern char __weak __stop_BTF[];static ssize_t btf_vmlinux_read (struct file *file,struct kobject *kobj,struct bin_attribute *bin_attr,char *buf,loff_t off,size_t len) { -memcpy (buf, _binary__btf_vmlinux_bin_start + off, len); +memcpy (buf, __start_BTF + off, len);return len; }
@@ -30,15 +30,14 @@ static struct kobject *btf_kobj;static int __init btf_vmlinux_init (void ) { -if (!_binary__btf_vmlinux_bin_start) +if (!__start_BTF)return 0 ; btf_kobj =kobject_create_and_add ("btf" , kernel_kobj);if (!btf_kobj)return -ENOMEM ; - bin_attr_btf_vmlinux.size = _binary__btf_vmlinux_bin_end - - _binary__btf_vmlinux_bin_start; + bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;return sysfs_create_bin_file (btf_kobj, &bin_attr_btf_vmlinux); }
scripts/link-vmlinux.sh +10 -14
@@ -113,9 +113,6 @@ vmlinux_link() gen_btf() { local pahole_ver - local bin_arch - local bin_format - local bin_file if ! [ -x "$(command -v ${PAHOLE})" ]; then echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
@@ -133,17 +130,16 @@ gen_btf() info "BTF" ${2} LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1} - # dump .BTF section into raw binary file to link with final vmlinux - bin_arch=$(LANG=C ${OBJDUMP} -f ${1} | grep architecture | \ - cut -d, -f1 | cut -d' ' -f2) - bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \ - awk '{print $4}') - bin_file=.btf.vmlinux.bin - ${OBJCOPY} --change-section-address .BTF=0 \ - --set-section-flags .BTF=alloc -O binary \ - --only-section=.BTF ${1} $bin_file - ${OBJCOPY} -I binary -O ${bin_format} -B ${bin_arch} \ - --rename-section .data=.BTF $bin_file ${2} + # Create ${2} which contains just .BTF section but no symbols. Add + # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all + # deletes all symbols including __start_BTF and __stop_BTF, which will + # be redefined in the linker script. Add 2>/dev/null to suppress GNU + # objcopy warnings: "empty loadable segment detected at ..." + ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ + --strip-all ${1} ${2} 2>/dev/null + # Change e_type to ET_REL so that it can be used to link final vmlinux. + # Unlike GNU ld, lld does not allow an ET_EXEC input. + printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none } # Create ${2} .o file with all symbols from the ${1} object file
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
Quoted 58 lines · Fangrui Song
Simplify
gen_btflogic 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_ALLOCflag because it is part of vmlinux image used for introspection. C code can reference the section via linker script defined__start_BTFand__stop_BTF. This fixes a small problem that previous .BTF had theSHF_WRITEflag (objcopy -I binary -O elf* synthesized .data).Additionally,
objcopy -I binarysynthesized symbols_binary__btf_vmlinux_bin_startand_binary__btf_vmlinux_bin_stop(not used elsewhere) are replaced with more commonplace__start_BTFand__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_typefield in the ELF header fromET_EXECtoET_RELso that lld will accept .btf.vmlinux.bin.o. AcceptingET_EXECas 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.havoids potential subtle orphan section placement issues and suppresses --orphan-handling=warn warnings.v6:
- drop llvm-objdump from the title. We don't run objdump now
- delete unused local variables:bin_arch,bin_formatandbin_file
- mention in the comment that lld does not allow anET_EXECinput
- rename BTF back to .BTF . The section name is assumed by bpftool
- add output section description toinclude/asm-generic/vmlinux.lds.h
- mentioncb0cc635c7a9("powerpc: Include .BTF section")v5:
- rebase on top of bpf-next/master
- rename .BTF to BTFFixes df786c9b9476 ("bpf: Force .BTF section start to zero when dumping from vmlinux")Fixes cb0cc635c7a9 ("powerpc: Include .BTF section")Signed-off-by Fangrui Song <[email protected]>Reported-by Nathan Chancellor <[email protected]>Reviewed-by Stanislav Fomichev <[email protected]>Tested-by Stanislav Fomichev <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Michael Ellerman <[email protected]>Cc Naveen N. Rao <[email protected]>Cc Nick Desaulniers <[email protected]>
Quoted 1 line
---
Thanks for detailed commit message and comments in the script, that's very helpful. Looks good to me, I've tested with my local setup and everything works across bpftool, selftests and my private BTF tool, which doesn't use libbpf.
Quoted 6 lines
arch/powerpc/kernel/vmlinux.lds.S | 6 ------ include/asm-generic/vmlinux.lds.h | 15 +++++++++++++++ kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 24 ++++++++++-------------- 5 files changed, 34 insertions(+), 31 deletions(-)
[...]
Quoted 37 lines · Fangrui Song
Simplify
gen_btflogic 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_ALLOCflag because it is part of vmlinux image used for introspection. C code can reference the section via linker script defined__start_BTFand__stop_BTF. This fixes a small problem that previous .BTF had theSHF_WRITEflag (objcopy -I binary -O elf* synthesized .data).Additionally,
objcopy -I binarysynthesized symbols_binary__btf_vmlinux_bin_startand_binary__btf_vmlinux_bin_stop(not used elsewhere) are replaced with more commonplace__start_BTFand__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_typefield in the ELF header fromET_EXECtoET_RELso that lld will accept .btf.vmlinux.bin.o. AcceptingET_EXECas 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.havoids potential subtle orphan section placement issues and suppresses --orphan-handling=warn warnings.v6:
- drop llvm-objdump from the title. We don't run objdump now
- delete unused local variables:bin_arch,bin_formatandbin_file
- mention in the comment that lld does not allow anET_EXECinput
- rename BTF back to .BTF . The section name is assumed by bpftool
- add output section description toinclude/asm-generic/vmlinux.lds.h
- mentioncb0cc635c7a9("powerpc: Include .BTF section")
Quoted 8 lines
v5:
- rebase on top of bpf-next/master
- rename .BTF to BTFFixes df786c9b9476 ("bpf: Force .BTF section start to zero when dumping from vmlinux")Fixes cb0cc635c7a9 ("powerpc: Include .BTF section")Signed-off-by Fangrui Song <[email protected]>
-Kees
Quoted 183 lines
Reported-by Nathan Chancellor <[email protected]>Reviewed-by Stanislav Fomichev <[email protected]>Tested-by Stanislav Fomichev <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Michael Ellerman <[email protected]>Cc Naveen N. Rao <[email protected]>Cc Nick Desaulniers <[email protected]>--- arch/powerpc/kernel/vmlinux.lds.S | 6 ------ include/asm-generic/vmlinux.lds.h | 15 +++++++++++++++ kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 24 ++++++++++-------------- 5 files changed, 34 insertions(+), 31 deletions(-)arch/powerpc/kernel/vmlinux.lds.S +0
-6@@ -303,12 +303,6 @@ SECTIONS *(.branch_lt) } -#ifdef CONFIG_DEBUG_INFO_BTF - .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { - *(.BTF) - } -#endif - .opd : AT(ADDR(.opd) - LOAD_OFFSET) { __start_opd = .; KEEP(*(.opd))include/asm-generic/vmlinux.lds.h +15
-0@@ -535,6 +535,7 @@ \RO_EXCEPTION_TABLE \ NOTES \ +BTF \ \ . = ALIGN((align)); \ __end_rodata = .;@@ -621,6 +622,20 @@ __stop___ex_table = .; \ } +/* +* .BTF +*/ +#ifdef CONFIG_DEBUG_INFO_BTF +#define BTF \ + .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \ + __start_BTF = .; \ + *(.BTF) \ + __stop_BTF = .; \ + } +#else +#define BTF +#endif +/* * Init task */ kernel/bpf/btf.c +4
-5@@ -3477,8 +3477,8 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,return ERR_PTR (err); } -extern char __weak _binary__btf_vmlinux_bin_start[]; -extern char __weak _binary__btf_vmlinux_bin_end[]; +extern char __weak __start_BTF[]; +extern char __weak __stop_BTF[];extern struct btf *btf_vmlinux;#define BPF_MAP_TYPE (_id, _ops)@@ -3605,9 +3605,8 @@ struct btf *btf_parse_vmlinux(void) } env->btf = btf; - btf->data = _binary__btf_vmlinux_bin_start; - btf->data_size = _binary__btf_vmlinux_bin_end - - _binary__btf_vmlinux_bin_start; + btf->data = __start_BTF; + btf->data_size = __stop_BTF - __start_BTF; err =btf_parse_hdr (env);if (err)kernel/bpf/sysfs_btf.c +5
-6@@ -9,15 +9,15 @@#include <linux/sysfs.h> /* See scripts/link-vmlinux.sh, gen_btf() func for details */ -extern char __weak _binary__btf_vmlinux_bin_start[]; -extern char __weak _binary__btf_vmlinux_bin_end[]; +extern char __weak __start_BTF[]; +extern char __weak __stop_BTF[];static ssize_t btf_vmlinux_read (struct file *file,struct kobject *kobj,struct bin_attribute *bin_attr,char *buf,loff_t off,size_t len) { -memcpy (buf, _binary__btf_vmlinux_bin_start + off, len); +memcpy (buf, __start_BTF + off, len);return len; }@@ -30,15 +30,14 @@ static struct kobject *btf_kobj;static int __init btf_vmlinux_init (void ) { -if (!_binary__btf_vmlinux_bin_start) +if (!__start_BTF)return 0 ; btf_kobj =kobject_create_and_add ("btf" , kernel_kobj);if (!btf_kobj)return -ENOMEM ; - bin_attr_btf_vmlinux.size = _binary__btf_vmlinux_bin_end - - _binary__btf_vmlinux_bin_start; + bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;return sysfs_create_bin_file (btf_kobj, &bin_attr_btf_vmlinux); }scripts/link-vmlinux.sh +10
-14@@ -113,9 +113,6 @@ vmlinux_link() gen_btf() { local pahole_ver - local bin_arch - local bin_format - local bin_file if ! [ -x "$(command -v ${PAHOLE})" ]; then echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"@@ -133,17 +130,16 @@ gen_btf() info "BTF" ${2} LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1} - # dump .BTF section into raw binary file to link with final vmlinux - bin_arch=$(LANG=C ${OBJDUMP} -f ${1} | grep architecture | \ - cut -d, -f1 | cut -d' ' -f2) - bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \ - awk '{print $4}') - bin_file=.btf.vmlinux.bin - ${OBJCOPY} --change-section-address .BTF=0 \ - --set-section-flags .BTF=alloc -O binary \ - --only-section=.BTF ${1} $bin_file - ${OBJCOPY} -I binary -O ${bin_format} -B ${bin_arch} \ - --rename-section .data=.BTF $bin_file ${2} + # Create ${2} which contains just .BTF section but no symbols. Add + # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all + # deletes all symbols including __start_BTF and __stop_BTF, which will + # be redefined in the linker script. Add 2>/dev/null to suppress GNU + # objcopy warnings: "empty loadable segment detected at ..." + ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ + --strip-all ${1} ${2} 2>/dev/null + # Change e_type to ET_REL so that it can be used to link final vmlinux. + # Unlike GNU ld, lld does not allow an ET_EXEC input. + printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none } # Create ${2} .o file with all symbols from the ${1} object fileSignature · 1 line
2.25.1.481.gfbce0eb801-goog
Signature · 1 line
Kees Cook
Quoted 70 lines · Fangrui Song
Simplify
gen_btflogic 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_ALLOCflag because it is part of vmlinux image used for introspection. C code can reference the section via linker script defined__start_BTFand__stop_BTF. This fixes a small problem that previous .BTF had theSHF_WRITEflag (objcopy -I binary -O elf* synthesized .data).Additionally,
objcopy -I binarysynthesized symbols_binary__btf_vmlinux_bin_startand_binary__btf_vmlinux_bin_stop(not used elsewhere) are replaced with more commonplace__start_BTFand__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_typefield in the ELF header fromET_EXECtoET_RELso that lld will accept .btf.vmlinux.bin.o. AcceptingET_EXECas 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.havoids potential subtle orphan section placement issues and suppresses --orphan-handling=warn warnings.v6:
- drop llvm-objdump from the title. We don't run objdump now
- delete unused local variables:bin_arch,bin_formatandbin_file
- mention in the comment that lld does not allow anET_EXECinput
- rename BTF back to .BTF . The section name is assumed by bpftool
- add output section description toinclude/asm-generic/vmlinux.lds.h
- mentioncb0cc635c7a9("powerpc: Include .BTF section")v5:
- rebase on top of bpf-next/master
- rename .BTF to BTFFixes df786c9b9476 ("bpf: Force .BTF section start to zero when dumping from vmlinux")Fixes cb0cc635c7a9 ("powerpc: Include .BTF section")Signed-off-by Fangrui Song <[email protected]>Reported-by Nathan Chancellor <[email protected]>Reviewed-by Stanislav Fomichev <[email protected]>Tested-by Stanislav Fomichev <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Michael Ellerman <[email protected]>Cc Naveen N. Rao <[email protected]>Cc Nick Desaulniers <[email protected]>--- arch/powerpc/kernel/vmlinux.lds.S | 6 ------ include/asm-generic/vmlinux.lds.h | 15 +++++++++++++++ kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 24 ++++++++++-------------- 5 files changed, 34 insertions(+), 31 deletions(-)arch/powerpc/kernel/vmlinux.lds.S +0
-0
arch/powerpc/kernel/vmlinux.lds.S SECTIONS · line 311
Quoted 9 lines
arch/powerpc/kernel/vmlinux.lds.S +0
-6@@ -303,12 +303,6 @@ SECTIONS *(.branch_lt) } -#ifdef CONFIG_DEBUG_INFO_BTF - .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { - *(.BTF) - } -#endif -
I wasn't able to easily test as I don't have bpf-next/master handy.
But this part of the change looks fine to me.
cheers
Quoted 145 lines
include/asm-generic/vmlinux.lds.h +15
-0@@ -535,6 +535,7 @@ \RO_EXCEPTION_TABLE \ NOTES \ +BTF \ \ . = ALIGN((align)); \ __end_rodata = .;@@ -621,6 +622,20 @@ __stop___ex_table = .; \ } +/* +* .BTF +*/ +#ifdef CONFIG_DEBUG_INFO_BTF +#define BTF \ + .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \ + __start_BTF = .; \ + *(.BTF) \ + __stop_BTF = .; \ + } +#else +#define BTF +#endif +/* * Init task */ kernel/bpf/btf.c +4
-5@@ -3477,8 +3477,8 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,return ERR_PTR (err); } -extern char __weak _binary__btf_vmlinux_bin_start[]; -extern char __weak _binary__btf_vmlinux_bin_end[]; +extern char __weak __start_BTF[]; +extern char __weak __stop_BTF[];extern struct btf *btf_vmlinux;#define BPF_MAP_TYPE (_id, _ops)@@ -3605,9 +3605,8 @@ struct btf *btf_parse_vmlinux(void) } env->btf = btf; - btf->data = _binary__btf_vmlinux_bin_start; - btf->data_size = _binary__btf_vmlinux_bin_end - - _binary__btf_vmlinux_bin_start; + btf->data = __start_BTF; + btf->data_size = __stop_BTF - __start_BTF; err =btf_parse_hdr (env);if (err)kernel/bpf/sysfs_btf.c +5
-6@@ -9,15 +9,15 @@#include <linux/sysfs.h> /* See scripts/link-vmlinux.sh, gen_btf() func for details */ -extern char __weak _binary__btf_vmlinux_bin_start[]; -extern char __weak _binary__btf_vmlinux_bin_end[]; +extern char __weak __start_BTF[]; +extern char __weak __stop_BTF[];static ssize_t btf_vmlinux_read (struct file *file,struct kobject *kobj,struct bin_attribute *bin_attr,char *buf,loff_t off,size_t len) { -memcpy (buf, _binary__btf_vmlinux_bin_start + off, len); +memcpy (buf, __start_BTF + off, len);return len; }@@ -30,15 +30,14 @@ static struct kobject *btf_kobj;static int __init btf_vmlinux_init (void ) { -if (!_binary__btf_vmlinux_bin_start) +if (!__start_BTF)return 0 ; btf_kobj =kobject_create_and_add ("btf" , kernel_kobj);if (!btf_kobj)return -ENOMEM ; - bin_attr_btf_vmlinux.size = _binary__btf_vmlinux_bin_end - - _binary__btf_vmlinux_bin_start; + bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;return sysfs_create_bin_file (btf_kobj, &bin_attr_btf_vmlinux); }scripts/link-vmlinux.sh +10
-14@@ -113,9 +113,6 @@ vmlinux_link() gen_btf() { local pahole_ver - local bin_arch - local bin_format - local bin_file if ! [ -x "$(command -v ${PAHOLE})" ]; then echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"@@ -133,17 +130,16 @@ gen_btf() info "BTF" ${2} LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1} - # dump .BTF section into raw binary file to link with final vmlinux - bin_arch=$(LANG=C ${OBJDUMP} -f ${1} | grep architecture | \ - cut -d, -f1 | cut -d' ' -f2) - bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \ - awk '{print $4}') - bin_file=.btf.vmlinux.bin - ${OBJCOPY} --change-section-address .BTF=0 \ - --set-section-flags .BTF=alloc -O binary \ - --only-section=.BTF ${1} $bin_file - ${OBJCOPY} -I binary -O ${bin_format} -B ${bin_arch} \ - --rename-section .data=.BTF $bin_file ${2} + # Create ${2} which contains just .BTF section but no symbols. Add + # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all + # deletes all symbols including __start_BTF and __stop_BTF, which will + # be redefined in the linker script. Add 2>/dev/null to suppress GNU + # objcopy warnings: "empty loadable segment detected at ..." + ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ + --strip-all ${1} ${2} 2>/dev/null + # Change e_type to ET_REL so that it can be used to link final vmlinux. + # Unlike GNU ld, lld does not allow an ET_EXEC input. + printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none } # Create ${2} .o file with all symbols from the ${1} object fileSignature · 1 line
2.25.1.481.gfbce0eb801-goog
Quoted 42 lines · Michael Ellerman
Quoted 41 lines · Fangrui Song
Simplify
gen_btflogic 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_ALLOCflag because it is part of vmlinux image used for introspection. C code can reference the section via linker script defined__start_BTFand__stop_BTF. This fixes a small problem that previous .BTF had theSHF_WRITEflag (objcopy -I binary -O elf* synthesized .data).Additionally,
objcopy -I binarysynthesized symbols_binary__btf_vmlinux_bin_startand_binary__btf_vmlinux_bin_stop(not used elsewhere) are replaced with more commonplace__start_BTFand__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_typefield in the ELF header fromET_EXECtoET_RELso that lld will accept .btf.vmlinux.bin.o. AcceptingET_EXECas 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.havoids potential subtle orphan section placement issues and suppresses --orphan-handling=warn warnings.v6:
- drop llvm-objdump from the title. We don't run objdump now
- delete unused local variables:bin_arch,bin_formatandbin_file
- mention in the comment that lld does not allow anET_EXECinput
- rename BTF back to .BTF . The section name is assumed by bpftool
- add output section description toinclude/asm-generic/vmlinux.lds.h
- mentioncb0cc635c7a9("powerpc: Include .BTF section")v5:
- rebase on top of bpf-next/master
- rename .BTF to BTF
Quoted 16 lines
Quoted 8 lines
Fixes df786c9b9476 ("bpf: Force .BTF section start to zero when dumping from vmlinux")Fixes cb0cc635c7a9 ("powerpc: Include .BTF section")Signed-off-by Fangrui Song <[email protected]>Reported-by Nathan Chancellor <[email protected]>Reviewed-by Stanislav Fomichev <[email protected]>Tested-by Stanislav Fomichev <[email protected]>Cc Alexei Starovoitov <[email protected]>Quoted 8 lines
Cc Andrii Nakryiko <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Michael Ellerman <[email protected]>Cc Naveen N. Rao <[email protected]>Cc Nick Desaulniers <[email protected]>
Applied, thanks everyone!