bpf: Support llvm-objcopy and llvm-objdump for vmlinux BTF
Subsystems (4)
PATCH v5 1/1 bpf: Support llvm-objcopy and llvm-objdump for vmlinux BTF
Simplify gen_btf logic to make it work with llvm-objcopy and llvm-objdump. The existing 'file format' and 'architecture' parsing logic is brittle and does not work with llvm-objcopy/llvm-objdump.
.BTF in .tmp_vmlinux.btf is non-SHF_ALLOC. Add the SHF_ALLOC flag and rename .BTF to BTF so that C code can reference the section via linker synthesized __start_BTF and __stop_BTF. This fixes a small problem that previous .BTF had the SHF_WRITE flag. Additionally, objcopy -I binary synthesized symbols _binary__btf_vmlinux_bin_start and _binary__btf_vmlinux_bin_start (not used elsewhere) are replaced with more common __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.
--- kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 17 ++++++----------- 3 files changed, 15 insertions(+), 22 deletions(-)
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 +6 -11
@@ -133,17 +133,12 @@ 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} + # Extract .BTF, add SHF_ALLOC, rename to BTF so that we can reference + # it via linker synthesized __start_BTF and __stop_BTF. Change e_type + # to ET_REL so that it can be used to link final vmlinux. + ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ + --rename-section .BTF=BTF ${1} ${2} 2>/dev/null && \ + 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 3 lines · Fangrui Song
Simplify
gen_btflogic to make it work with llvm-objcopy and llvm-objdump. The existing 'file format' and 'architecture' parsing logic is brittle and does not work with llvm-objcopy/llvm-objdump.
Thanks, it all makes sense and looks much better/portable (too much dependence on binutils :-).
I've left a bunch of questions/nits below.
Quoted 9 lines
.BTF in .tmp_vmlinux.btf is non-SHF_ALLOC. Add the
SHF_ALLOCflag and rename .BTF to BTF so that C code can reference the section via linker synthesized__start_BTFand__stop_BTF. This fixes a small problem that previous .BTF had theSHF_WRITEflag. Additionally,objcopy -I binarysynthesized symbols_binary__btf_vmlinux_bin_startand_binary__btf_vmlinux_bin_start(not used elsewhere) are replaced with more common__start_BTFand__stop_BTF.
Quoted 7 lines
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.
Please keep small changelog here, for example:
v5:
* rebased on top of bpfnext
Btw, I tried to pull and test it and failed:
$ curl -LO https://lore.kernel.org/bpf/[email protected]/raw % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 9627 100 9627 0 0 47191 0 --:--:-- --:--:-- --:--:-- 47191 $ git am raw warning: Patch sent with format=flowed; space at the end of lines might be lost. Applying: bpf: Support llvm-objcopy and llvm-objdump for vmlinux BTF error: corrupt patch at line 20 Patch failed at 0001 bpf: Support llvm-objcopy and llvm-objdump for vmlinux BTF hint: Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort". $ git describe v5.6-rc5-1621-g230021539e8c
Are you sure it's on top of the bpf-next? Or am I doing something wrong?
Quoted 74 lines
Fixes df786c9b9476 ("bpf: Force .BTF section start to zero when dumping from vmlinux")Cc Stanislav Fomichev <[email protected]>Cc Nick Desaulniers <[email protected]>Reported-by Nathan Chancellor <[email protected]>Signed-off-by Fangrui Song <[email protected]>--- kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 17 ++++++----------- 3 files changed, 15 insertions(+), 22 deletions(-)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 +0
-0
scripts/link-vmlinux.sh gen_btf() · line 140
Quoted 8 lines
scripts/link-vmlinux.sh +0
-6@@ -133,17 +133,12 @@ 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
We still have the following in gen_btf that you need to remove:
local bin_arch
local bin_format
local bin_file
scripts/link-vmlinux.sh gen_btf() · line 141
Quoted 11 lines
scripts/link-vmlinux.sh +6
-5- ${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} + # Extract .BTF, add SHF_ALLOC, rename to BTF so that we can reference + # it via linker synthesized __start_BTF and __stop_BTF. Change e_type + # to ET_REL so that it can be used to link final vmlinux. + ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ + --rename-section .BTF=BTF ${1} ${2} 2>/dev/null && \ + printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
We have 'set -e' so && can be omitted. Maybe it will be a bit
more clear:
# Extract .BTF, add SHF_ALLOC, rename to BTF so that we can reference
# it via linker synthesized __start_BTF and __stop_BTF.
${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
--rename-section .BTF=BTF ${1} ${2} 2>/dev/null
# Change e_type to ET_REL (0x01) so that it can be used to link final
# vmlinux.
printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
^^^ maybe also point out that this is required for llvm's ldd.
I know you point it out in the commit message, but still nice
to have as a comment here.Quoted 4 lines
scripts/link-vmlinux.sh +0
-0} # Create ${2} .o file with all symbols from the ${1} object fileSignature · 1 line
2.25.1.481.gfbce0eb801-goog
Quoted 20 lines · Stanislav Fomichev
Quoted 3 lines · Fangrui Song
Simplify
gen_btflogic to make it work with llvm-objcopy and llvm-objdump. The existing 'file format' and 'architecture' parsing logic is brittle and does not work with llvm-objcopy/llvm-objdump.Thanks, it all makes sense and looks much better/portable (too much dependence on binutils :-).
I've left a bunch of questions/nits below.Quoted 12 lines
.BTF in .tmp_vmlinux.btf is non-SHF_ALLOC. Add the
SHF_ALLOCflag and rename .BTF to BTF so that C code can reference the section via linker synthesized__start_BTFand__stop_BTF. This fixes a small problem that previous .BTF had theSHF_WRITEflag. Additionally,objcopy -I binarysynthesized symbols_binary__btf_vmlinux_bin_startand_binary__btf_vmlinux_bin_start(not used elsewhere) are replaced with more common__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?"
Quoted 8 lines
Quoted 4 lines
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.Please keep small changelog here, for example:
v5:
* rebased on top of bpfnext
Thanks for the tip. Add them at the bottom?
Quoted 19 lines
Btw, I tried to pull and test it and failed:
$ curl -LO https://lore.kernel.org/bpf/[email protected]/raw % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 9627 100 9627 0 0 47191 0 --:--:-- --:--:-- --:--:-- 47191 $ git am raw warning: Patch sent with format=flowed; space at the end of lines might be lost. Applying: bpf: Support llvm-objcopy and llvm-objdump for vmlinux BTF error: corrupt patch at line 20 Patch failed at 0001 bpf: Support llvm-objcopy and llvm-objdump for vmlinux BTF hint: Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort". $ git describe v5.6-rc5-1621-g230021539e8c
Quoted 1 line
Are you sure it's on top of the bpf-next? Or am I doing something wrong?
It can be my fault when messing with various patches.. Will be more cautious.
Quoted 78 lines
Quoted 78 lines
Fixes df786c9b9476 ("bpf: Force .BTF section start to zero when dumping from vmlinux")Cc Stanislav Fomichev <[email protected]>Cc Nick Desaulniers <[email protected]>Reported-by Nathan Chancellor <[email protected]>Signed-off-by Fangrui Song <[email protected]>--- kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 17 ++++++----------- 3 files changed, 15 insertions(+), 22 deletions(-)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 +0
-2@@ -133,17 +133,12 @@ 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 | \
Quoted 8 lines
scripts/link-vmlinux.sh gen_btf() · line 141
Quoted 4 lines
scripts/link-vmlinux.sh +0
-4- cut -d, -f1 | cut -d' ' -f2) - bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \ - awk '{print $4}') - bin_file=.btf.vmlinux.binWe still have the following in
gen_btfthat you need to remove:
localbin_arch
localbin_format
localbin_file
Thanks. Will delete them.
Quoted 5 lines
Quoted 5 lines
scripts/link-vmlinux.sh +0
-5- ${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}
Quoted 8 lines
scripts/link-vmlinux.sh gen_btf() · line 141
Quoted 6 lines
scripts/link-vmlinux.sh +6
-0+ # Extract .BTF, add SHF_ALLOC, rename to BTF so that we can reference + # it via linker synthesized __start_BTF and __stop_BTF. Change e_type + # to ET_REL so that it can be used to link final vmlinux. + ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ + --rename-section .BTF=BTF ${1} ${2} 2>/dev/null && \ + printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=noneWe have 'set -e' so && can be omitted. Maybe it will be a bit more clear:
Agree.
Quoted 5 lines
# Extract .BTF, add
SHF_ALLOC, rename to BTF so that we can reference # it via linker synthesized__start_BTFand__stop_BTF. ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
--rename-section .BTF=BTF ${1} ${2} 2>/dev/null
Quoted 7 lines
# Change
e_typetoET_REL(0x01) so that it can be used to link final # vmlinux.
printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none^^^ maybe also point out that this is required for llvm's ldd. I know you point it out in the commit message, but still nice to have as a comment here.
Will mention lld in v6.
Quoted 4 lines
Quoted 4 lines
scripts/link-vmlinux.sh +0
-1} # Create ${2} .o file with all symbols from the ${1} object file --2.25.1.481.gfbce0eb801-goog
Quoted 8 lines · Fangrui Song
Quoted 4 lines · Stanislav Fomichev
Please keep small changelog here, for example:
v5:
* rebased on top of bpfnextThanks for the tip. Add them at the bottom?
"Below the fold" see this patch I just sent out:
https://lore.kernel.org/lkml/[email protected]/T/#u
grep "Changes"
$ git format-patch -v2 HEAD~ $ vim 0001-...patch <manually add changelog "below the fold">
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 11 lines · Nick Desaulniers
Quoted 8 lines · Fangrui Song
Quoted 4 lines · Stanislav Fomichev
Please keep small changelog here, for example:
v5:
* rebased on top of bpfnextThanks for the tip. Add them at the bottom?
Quoted 7 lines
"Below the fold" see this patch I just sent out:
https://lore.kernel.org/lkml/[email protected]/T/#u
grep "Changes"$ git format-patch -v2 HEAD~ $ vim 0001-...patch <manually add changelog "below the fold">
BPF subtree prefers the changelog in the commit body, not the comments (iow, before ---).
Add them at the end of you message, see, for example:
https://lore.kernel.org/bpf/[email protected]/T/
Quoted 3 lines
--
Thanks,
~Nick Desaulniers
Quoted 18 lines · Stanislav Fomichev
Quoted 15 lines · Nick Desaulniers
Quoted 8 lines · Fangrui Song
Quoted 4 lines · Stanislav Fomichev
Please keep small changelog here, for example:
v5:
* rebased on top of bpfnextThanks for the tip. Add them at the bottom?
"Below the fold" see this patch I just sent out:
https://lore.kernel.org/lkml/[email protected]/T/#u
grep "Changes"
Quoted 7 lines
Quoted 3 lines
$ git format-patch -v2 HEAD~ $ vim 0001-...patch <manually add changelog "below the fold">BPF subtree prefers the changelog in the commit body, not the comments (iow, before ---).
Add them at the end of you message, see, for example:
https://lore.kernel.org/bpf/[email protected]/T/
Sigh, every maintainer is a special snowflake. In our tree, you're only allowed to commit on Thursdays under a blood moon. /s
But thanks for the note.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Daniel Borkmann
maintainerQuoted 24 lines · Nick Desaulniers
Quoted 15 lines · Stanislav Fomichev
Quoted 13 lines · Nick Desaulniers
Quoted 7 lines · Fangrui Song
Quoted 4 lines · Stanislav Fomichev
Please keep small changelog here, for example:
v5:
* rebased on top of bpfnextThanks for the tip. Add them at the bottom?
"Below the fold" see this patch I just sent out:
https://lore.kernel.org/lkml/[email protected]/T/#u
grep "Changes"Quoted 7 lines
Quoted 3 lines
$ git format-patch -v2 HEAD~ $ vim 0001-...patch <manually add changelog "below the fold">BPF subtree prefers the changelog in the commit body, not the comments (iow, before ---).
Add them at the end of you message, see, for example:
https://lore.kernel.org/bpf/[email protected]/T/
Quoted 4 lines
Sigh, every maintainer is a special snowflake. In our tree, you're only allowed to commit on Thursdays under a blood moon. /s
But thanks for the note.
For every commit into bpf/bpf-next we automatically put a 'Link:' tag pointing to the lore.kernel.org/bpf long-term archive, so there is always an easy way to retrieve additional information or discussions ([0] as one example). So whether you put it above or below the '---' line I personally don't mind either way. All crucial information that helps a reader later on to understand /why/ we ended up exactly with the current change should be part of the main commit log though.
Quoted 11 lines · Fangrui Song
Simplify
gen_btflogic to make it work with llvm-objcopy and llvm-objdump. The existing 'file format' and 'architecture' parsing logic is brittle and does not work with llvm-objcopy/llvm-objdump..BTF in .tmp_vmlinux.btf is non-SHF_ALLOC. Add the
SHF_ALLOCflag and rename .BTF to BTF so that C code can reference the section via linker synthesized__start_BTFand__stop_BTF. This fixes a small problem that previous .BTF had theSHF_WRITEflag. Additionally,objcopy -I binarysynthesized symbols_binary__btf_vmlinux_bin_startand_binary__btf_vmlinux_bin_start(not used elsewhere) are replaced with more common__start_BTFand__stop_BTF.
I'm glad to see the name change benefit here. Just reducing the number of execs in this path is only worth this change, IMO. Going from 2 objdump and 2 objcopy calls to a single objcopy is very nice. :)
Quoted 8 lines
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.
Nice ELF trick. :)
Quoted 7 lines
Fixes df786c9b9476 ("bpf: Force .BTF section start to zero when dumping from vmlinux")Cc Stanislav Fomichev <[email protected]>Cc Nick Desaulniers <[email protected]>Reported-by Nathan Chancellor <[email protected]>Signed-off-by Fangrui Song <[email protected]>
-Kees
Quoted 91 lines
--- kernel/bpf/btf.c | 9 ++++----- kernel/bpf/sysfs_btf.c | 11 +++++------ scripts/link-vmlinux.sh | 17 ++++++----------- 3 files changed, 15 insertions(+), 22 deletions(-)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 +6
-11@@ -133,17 +133,12 @@ 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} + # Extract .BTF, add SHF_ALLOC, rename to BTF so that we can reference + # it via linker synthesized __start_BTF and __stop_BTF. Change e_type + # to ET_REL so that it can be used to link final vmlinux. + ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ + --rename-section .BTF=BTF ${1} ${2} 2>/dev/null && \ + 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