Skip to content

Commit 90ceddc

Browse files
MaskRayborkmann
authored andcommitted
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. Fixes: df786c9 ("bpf: Force .BTF section start to zero when dumping from vmlinux") Fixes: cb0cc63 ("powerpc: Include .BTF section") Reported-by: Nathan Chancellor <natechancellor@gmail.com> Signed-off-by: Fangrui Song <maskray@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Tested-by: Stanislav Fomichev <sdf@google.com> Tested-by: Andrii Nakryiko <andriin@fb.com> Reviewed-by: Stanislav Fomichev <sdf@google.com> Reviewed-by: Kees Cook <keescook@chromium.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc) Link: ClangBuiltLinux/linux#871 Link: https://lore.kernel.org/bpf/20200318222746.173648-1-maskray@google.com
1 parent 483d7a3 commit 90ceddc

File tree

5 files changed

+34
-31
lines changed

5 files changed

+34
-31
lines changed

arch/powerpc/kernel/vmlinux.lds.S

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,6 @@ SECTIONS
303303
*(.branch_lt)
304304
}
305305

306-
#ifdef CONFIG_DEBUG_INFO_BTF
307-
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) {
308-
*(.BTF)
309-
}
310-
#endif
311-
312306
.opd : AT(ADDR(.opd) - LOAD_OFFSET) {
313307
__start_opd = .;
314308
KEEP(*(.opd))

include/asm-generic/vmlinux.lds.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@
535535
\
536536
RO_EXCEPTION_TABLE \
537537
NOTES \
538+
BTF \
538539
\
539540
. = ALIGN((align)); \
540541
__end_rodata = .;
@@ -621,6 +622,20 @@
621622
__stop___ex_table = .; \
622623
}
623624

625+
/*
626+
* .BTF
627+
*/
628+
#ifdef CONFIG_DEBUG_INFO_BTF
629+
#define BTF \
630+
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \
631+
__start_BTF = .; \
632+
*(.BTF) \
633+
__stop_BTF = .; \
634+
}
635+
#else
636+
#define BTF
637+
#endif
638+
624639
/*
625640
* Init task
626641
*/

kernel/bpf/btf.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,8 +3477,8 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
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)
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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
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;
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
}

scripts/link-vmlinux.sh

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ vmlinux_link()
113113
gen_btf()
114114
{
115115
local pahole_ver
116-
local bin_arch
117-
local bin_format
118-
local bin_file
119116

120117
if ! [ -x "$(command -v ${PAHOLE})" ]; then
121118
echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
@@ -133,17 +130,16 @@ gen_btf()
133130
info "BTF" ${2}
134131
LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
135132

136-
# dump .BTF section into raw binary file to link with final vmlinux
137-
bin_arch=$(LANG=C ${OBJDUMP} -f ${1} | grep architecture | \
138-
cut -d, -f1 | cut -d' ' -f2)
139-
bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \
140-
awk '{print $4}')
141-
bin_file=.btf.vmlinux.bin
142-
${OBJCOPY} --change-section-address .BTF=0 \
143-
--set-section-flags .BTF=alloc -O binary \
144-
--only-section=.BTF ${1} $bin_file
145-
${OBJCOPY} -I binary -O ${bin_format} -B ${bin_arch} \
146-
--rename-section .data=.BTF $bin_file ${2}
133+
# Create ${2} which contains just .BTF section but no symbols. Add
134+
# SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
135+
# deletes all symbols including __start_BTF and __stop_BTF, which will
136+
# be redefined in the linker script. Add 2>/dev/null to suppress GNU
137+
# objcopy warnings: "empty loadable segment detected at ..."
138+
${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
139+
--strip-all ${1} ${2} 2>/dev/null
140+
# Change e_type to ET_REL so that it can be used to link final vmlinux.
141+
# Unlike GNU ld, lld does not allow an ET_EXEC input.
142+
printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
147143
}
148144

149145
# Create ${2} .o file with all symbols from the ${1} object file

0 commit comments

Comments
 (0)