Skip to content

Commit f04d1e8

Browse files
MaskRaygregkh
authored andcommitted
bpf: Support llvm-objcopy for vmlinux BTF
commit 90ceddc upstream. 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 <[email protected]> Signed-off-by: Fangrui Song <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Stanislav Fomichev <[email protected]> Tested-by: Andrii Nakryiko <[email protected]> Reviewed-by: Stanislav Fomichev <[email protected]> Reviewed-by: Kees Cook <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Michael Ellerman <[email protected]> (powerpc) Link: ClangBuiltLinux/linux#871 Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Maria Teguiani <[email protected]> Tested-by: Matthias Maennich <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3a577e6 commit f04d1e8

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

arch/powerpc/kernel/vmlinux.lds.S

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,6 @@ SECTIONS
326326
*(.branch_lt)
327327
}
328328

329-
#ifdef CONFIG_DEBUG_INFO_BTF
330-
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) {
331-
*(.BTF)
332-
}
333-
#endif
334-
335329
.opd : AT(ADDR(.opd) - LOAD_OFFSET) {
336330
__start_opd = .;
337331
KEEP(*(.opd))

include/asm-generic/vmlinux.lds.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,12 @@
496496
__start___modver = .; \
497497
KEEP(*(__modver)) \
498498
__stop___modver = .; \
499-
. = ALIGN((align)); \
500-
__end_rodata = .; \
501499
} \
502-
. = ALIGN((align));
500+
\
501+
BTF \
502+
\
503+
. = ALIGN((align)); \
504+
__end_rodata = .;
503505

504506
/* RODATA & RO_DATA provided for backward compatibility.
505507
* All archs are supposed to use RO_DATA() */
@@ -588,6 +590,20 @@
588590
__stop___ex_table = .; \
589591
}
590592

593+
/*
594+
* .BTF
595+
*/
596+
#ifdef CONFIG_DEBUG_INFO_BTF
597+
#define BTF \
598+
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \
599+
__start_BTF = .; \
600+
*(.BTF) \
601+
__stop_BTF = .; \
602+
}
603+
#else
604+
#define BTF
605+
#endif
606+
591607
/*
592608
* Init task
593609
*/

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)