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 scripts (1 of 5 files). Show all

Unified · Split

scripts/link-vmlinux.sh+10−14
@@ -113,9 +113,6 @@ vmlinux_link() · 112 unmodified lines
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() · 11 unmodified lines
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