History

btf: expose BTF info through sysfs

Make .BTF section allocated and expose its contents through sysfs. /sys/kernel/btf directory is created to contain all the BTFs present inside kernel. Currently there is only kernel's main BTF, represented as /sys/kernel/btf/kernel file. Once kernel modules' BTFs are supported, each module will expose its BTF as /sys/kernel/btf/<module-name> file. Current approach relies on a few pieces coming together: 1. pahole is used to take almost final vmlinux image (modulo .BTF and kallsyms) and generate .BTF section by converting DWARF info into BTF. This section is not allocated and not mapped to any segment, though, so is not yet accessible from inside kernel at runtime. 2. objcopy dumps .BTF contents into binary file and subsequently convert binary file into linkable object file with automatically generated symbols _binary__btf_kernel_bin_start and _binary__btf_kernel_bin_end, pointing to start and end, respectively, of BTF raw data. 3. final vmlinux image is generated by linking this object file (and kallsyms, if necessary). sysfs_btf.c then creates /sys/kernel/btf/kernel file and exposes embedded BTF contents through it. This allows, e.g., libbpf and bpftool access BTF info at well-known location, without resorting to searching for vmlinux image on disk (location of which is not standardized and vmlinux image might not be even available in some scenarios, e.g., inside qemu during testing). Alternative approach using .incbin assembler directive to embed BTF contents directly was attempted but didn't work, because sysfs_proc.o is not re-compiled during link-vmlinux.sh stage. This is required, though, to update embedded BTF data (initially empty data is embedded, then pahole generates BTF info and we need to regenerate sysfs_btf.o with updated contents, but it's too late at that point). If BTF couldn't be generated due to missing or too old pahole, sysfs_btf.c handles that gracefully by detecting that _binary__btf_kernel_bin_start (weak symbol) is 0 and not creating /sys/kernel/btf at all. v2->v3: - added Documentation/ABI/testing/sysfs-kernel-btf (Greg K-H); - created proper kobject (btf_kobj) for btf directory (Greg K-H); - undo v2 change of reusing vmlinux, as it causes extra kallsyms pass due to initially missing __binary__btf_kernel_bin_{start/end} symbols; v1->v2: - allow kallsyms stage to re-use vmlinux generated by gen_btf();

Andrii Nakryiko committed 7 years ago

Commit 341dfcf1 parent a664a834 files · +104 −19Browse files

Showing the changes to scripts (1 of 4 files). Show all

Unified · Split

scripts/link-vmlinux.sh+33−19
@@ -56,8 +56,8 @@ modpost_link() · 55 unmodified lines
5656 }
5757
5858 # Link of vmlinux
59−# ${1} - optional extra .o files
60−# ${2} - output file
59+# ${1} - output file
60+# ${@:2} - optional extra .o files
6161 vmlinux_link()
6262 {
6363 local lds="${objtree}/${KBUILD_LDS}"
@@ -70,9 +70,9 @@ vmlinux_link() · 6 unmodified lines
7070 --start-group \
7171 ${KBUILD_VMLINUX_LIBS} \
7272 --end-group \
73− ${1}"
73+ ${@:2}"
7474
75− ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
75+ ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${1} \
7676 -T ${lds} ${objects}
7777 else
7878 objects="-Wl,--whole-archive \
@@ -81,9 +81,9 @@ vmlinux_link() · 2 unmodified lines
8181 -Wl,--start-group \
8282 ${KBUILD_VMLINUX_LIBS} \
8383 -Wl,--end-group \
84− ${1}"
84+ ${@:2}"
8585
86− ${CC} ${CFLAGS_vmlinux} -o ${2} \
86+ ${CC} ${CFLAGS_vmlinux} -o ${1} \
8787 -Wl,-T,${lds} \
8888 ${objects} \
8989 -lutil -lrt -lpthread
@@ -92,23 +92,34 @@ vmlinux_link() · 2 unmodified lines
9292 }
9393
9494 # generate .BTF typeinfo from DWARF debuginfo
95+# ${1} - vmlinux image
96+# ${2} - file to dump raw BTF data into
9597 gen_btf()
9698 {
97− local pahole_ver;
99+ local pahole_ver
100+ local bin_arch
98101
99102 if ! [ -x "$(command -v ${PAHOLE})" ]; then
100103 info "BTF" "${1}: pahole (${PAHOLE}) is not available"
101− return 0
104+ return 1
102105 fi
103106
104107 pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
105108 if [ "${pahole_ver}" -lt "113" ]; then
106109 info "BTF" "${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.13"
107− return 0
110+ return 1
108111 fi
109112
110− info "BTF" ${1}
113+ info "BTF" ${2}
114+ vmlinux_link ${1}
111115 LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
116+
117+ # dump .BTF section into raw binary file to link with final vmlinux
118+ bin_arch=$(${OBJDUMP} -f ${1} | grep architecture | \
119+ cut -d, -f1 | cut -d' ' -f2)
120+ ${OBJCOPY} --dump-section .BTF=.btf.kernel.bin ${1} 2>/dev/null
121+ ${OBJCOPY} -I binary -O ${CONFIG_OUTPUT_FORMAT} -B ${bin_arch} \
122+ --rename-section .data=.BTF .btf.kernel.bin ${2}
112123 }
113124
114125 # Create ${2} .o file with all symbols from the ${1} object file
@@ -153,6 +164,7 @@ sortextable() · 38 unmodified lines
153164 # Delete output files in case of error
154165 cleanup()
155166 {
167+ rm -f .btf.*
156168 rm -f .tmp_System.map
157169 rm -f .tmp_kallsyms*
158170 rm -f .tmp_vmlinux*
@@ -215,6 +227,13 @@ ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o · 56 unmodified lines
215227 info MODINFO modules.builtin.modinfo
216228 ${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
217229
230+btf_kernel_bin_o=""
231+if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
232+ if gen_btf .tmp_vmlinux.btf .btf.kernel.bin.o ; then
233+ btf_kernel_bin_o=.btf.kernel.bin.o
234+ fi
235+fi
236+
218237 kallsymso=""
219238 kallsyms_vmlinux=""
220239 if [ -n "${CONFIG_KALLSYMS}" ]; then
@@ -246,11 +265,11 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then · 25 unmodified lines
246265 kallsyms_vmlinux=.tmp_vmlinux2
247266
248267 # step 1
249− vmlinux_link "" .tmp_vmlinux1
268+ vmlinux_link .tmp_vmlinux1 ${btf_kernel_bin_o}
250269 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
251270
252271 # step 2
253− vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
272+ vmlinux_link .tmp_vmlinux2 .tmp_kallsyms1.o ${btf_kernel_bin_o}
254273 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
255274
256275 # step 3
@@ -261,18 +280,13 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then · 4 unmodified lines
261280 kallsymso=.tmp_kallsyms3.o
262281 kallsyms_vmlinux=.tmp_vmlinux3
263282
264− vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
265−
283+ vmlinux_link .tmp_vmlinux3 .tmp_kallsyms2.o ${btf_kernel_bin_o}
266284 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
267285 fi
268286 fi
269287
270288 info LD vmlinux
271−vmlinux_link "${kallsymso}" vmlinux
272−
273−if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
274− gen_btf vmlinux
275−fi
289+vmlinux_link vmlinux "${kallsymso}" "${btf_kernel_bin_o}"
276290
277291 if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
278292 info SORTEX vmlinux

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t