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
56}56}
57 57
58# Link of vmlinux58# Link of vmlinux
59# ${1} - optional extra .o files59# ${1} - output file
60# ${2} - output file60# ${@:2} - optional extra .o files
61vmlinux_link()61vmlinux_link()
62{62{
63 local lds="${objtree}/${KBUILD_LDS}"63 local lds="${objtree}/${KBUILD_LDS}"
@@ -70,9 +70,9 @@ vmlinux_link() · 6 unmodified lines
70 --start-group \70 --start-group \
71 ${KBUILD_VMLINUX_LIBS} \71 ${KBUILD_VMLINUX_LIBS} \
72 --end-group \72 --end-group \
73 ${1}"73 ${@:2}"
74 74
75 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \75 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${1} \
76 -T ${lds} ${objects}76 -T ${lds} ${objects}
77 else77 else
78 objects="-Wl,--whole-archive \78 objects="-Wl,--whole-archive \
@@ -81,9 +81,9 @@ vmlinux_link() · 2 unmodified lines
81 -Wl,--start-group \81 -Wl,--start-group \
82 ${KBUILD_VMLINUX_LIBS} \82 ${KBUILD_VMLINUX_LIBS} \
83 -Wl,--end-group \83 -Wl,--end-group \
84 ${1}"84 ${@:2}"
85 85
86 ${CC} ${CFLAGS_vmlinux} -o ${2} \86 ${CC} ${CFLAGS_vmlinux} -o ${1} \
87 -Wl,-T,${lds} \87 -Wl,-T,${lds} \
88 ${objects} \88 ${objects} \
89 -lutil -lrt -lpthread89 -lutil -lrt -lpthread
@@ -92,23 +92,34 @@ vmlinux_link() · 2 unmodified lines
92}92}
93 93
94# generate .BTF typeinfo from DWARF debuginfo94# generate .BTF typeinfo from DWARF debuginfo
95# ${1} - vmlinux image
96# ${2} - file to dump raw BTF data into
95gen_btf()97gen_btf()
96{98{
97 local pahole_ver;99 local pahole_ver
100 local bin_arch
98 101
99 if ! [ -x "$(command -v ${PAHOLE})" ]; then102 if ! [ -x "$(command -v ${PAHOLE})" ]; then
100 info "BTF" "${1}: pahole (${PAHOLE}) is not available"103 info "BTF" "${1}: pahole (${PAHOLE}) is not available"
101 return 0104 return 1
102 fi105 fi
103 106
104 pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')107 pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
105 if [ "${pahole_ver}" -lt "113" ]; then108 if [ "${pahole_ver}" -lt "113" ]; then
106 info "BTF" "${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.13"109 info "BTF" "${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.13"
107 return 0110 return 1
108 fi111 fi
109 112
110 info "BTF" ${1}113 info "BTF" ${2}
114 vmlinux_link ${1}
111 LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}115 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}
112}123}
113 124
114# Create ${2} .o file with all symbols from the ${1} object file125# Create ${2} .o file with all symbols from the ${1} object file
@@ -153,6 +164,7 @@ sortextable() · 38 unmodified lines
153# Delete output files in case of error164# Delete output files in case of error
154cleanup()165cleanup()
155{166{
167 rm -f .btf.*
156 rm -f .tmp_System.map168 rm -f .tmp_System.map
157 rm -f .tmp_kallsyms*169 rm -f .tmp_kallsyms*
158 rm -f .tmp_vmlinux*170 rm -f .tmp_vmlinux*
@@ -215,6 +227,13 @@ ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o · 56 unmodified lines
215info MODINFO modules.builtin.modinfo227info MODINFO modules.builtin.modinfo
216${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo228${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
217 229
230btf_kernel_bin_o=""
231if [ -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
235fi
236
218kallsymso=""237kallsymso=""
219kallsyms_vmlinux=""238kallsyms_vmlinux=""
220if [ -n "${CONFIG_KALLSYMS}" ]; then239if [ -n "${CONFIG_KALLSYMS}" ]; then
@@ -246,11 +265,11 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then · 25 unmodified lines
246 kallsyms_vmlinux=.tmp_vmlinux2265 kallsyms_vmlinux=.tmp_vmlinux2
247 266
248 # step 1267 # step 1
249 vmlinux_link "" .tmp_vmlinux1268 vmlinux_link .tmp_vmlinux1 ${btf_kernel_bin_o}
250 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o269 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
251 270
252 # step 2271 # step 2
253 vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2272 vmlinux_link .tmp_vmlinux2 .tmp_kallsyms1.o ${btf_kernel_bin_o}
254 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o273 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
255 274
256 # step 3275 # step 3
@@ -261,18 +280,13 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then · 4 unmodified lines
261 kallsymso=.tmp_kallsyms3.o280 kallsymso=.tmp_kallsyms3.o
262 kallsyms_vmlinux=.tmp_vmlinux3281 kallsyms_vmlinux=.tmp_vmlinux3
263 282
264 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3283 vmlinux_link .tmp_vmlinux3 .tmp_kallsyms2.o ${btf_kernel_bin_o}
265
266 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o284 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
267 fi285 fi
268fi286fi
269 287
270info LD vmlinux288info LD vmlinux
271vmlinux_link "${kallsymso}" vmlinux289vmlinux_link vmlinux "${kallsymso}" "${btf_kernel_bin_o}"
272
273if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
274 gen_btf vmlinux
275fi
276 290
277if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then291if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
278 info SORTEX vmlinux292 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