Skip to contentSkip to conversation

RFC: vmlinux BTF from clang -gbtf, stock lld and a libbpf tool

DISCUSSION Jose E. Marchesi 2 participants 2 messages Nov 27 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

[Fixing email adresses of David Faust and Bruce McCullough and Nick
Alcock]

Quoted 358 lines

Alexei asked in llvm/llvm-project#183915 [1][2] for native-vs-pahole results to come here, and suggested dropping BTF dedup from lld in favour of a stock link plus a libbpf-based kernel tool. I closed #183915 and tested that route on saved x86-64 kernel objects.

Two headline findings up front:

1. On those saved objects, unmodified ld.lld plus a libbpf prototype reproduced the patched linker's decoded vmlinux BTF (bpftool raw dumps match) and byte-identical .BTF_ids, though not byte-identical .BTF and without booting a host-tool image.
2. On the booted native kernel from the earlier patched-lld run, a verifier negative test (flex_array_unaligned_step in verifier_btf_array_access) unexpectedly loads across all three test_progs flavours because struct vring_used is only a FWD in native BTF. Static checks also show shifted-register FUNC prototypes (e.g. __add_to_kill) and two duplicate-ID warnings in resolve_btfids that fail CONFIG_WERROR=y.

The compiler side is LLVM #183929 (-gbtf for non-BPF ELF targets); its
Discourse RFC [3] drew a key question from LLVM DebugInfo maintainers:
LLVM debug info is best-effort, whereas the verifier and trampolines treat vmlinux BTF as trusted safety and ABI metadata. Before reshaping
#183929 or proposing kbuild patches, I'd like feedback on where that trust boundary sits, how functions and DATASECs should be handled, and
what GCC/Clang/linker contract makes sense.

Prior art: Arnaldo outlined GCC -gbtf, linker dedup and pahole fixups at LSF/MM/BPF 2025 [4]; Nick and Bruce have GNU ld BTF dedup work in progress [5]; Ihor's resolve_btfids / gen-btf.sh series [6] moved BTF post-processing into the tree; and pahole 1.32 now ships opt-in true_signature [7]. Whatever comes out of this should work for GCC objects too.

Setup and compiler behaviour
----------------------------

Three separate comparisons were run (revisions, config, lists and tool
source are in the gist linked below):

- Run B (booted x86-64 non-LTO kernels, QEMU TCG, no KVM): local branch
58b6bf74fe on bpf-next 32b66b94c, identical .config (no LTO, no IBT, CONFIG_WERROR=n, DWARF enabled, existing pahole Kconfig dependency retained; CONFIG_DEBUG_INFO_BTF_INLINE is rejected on the native path). Native vmlinux C objects used combined LLVM 0737facd716f (main 3bfae959fb6f + #183915 + #183929) with -gbtf and no pahole for vmlinux; control used DWARF and pahole 1.31 with this config's flags. Modules used pahole --btf_base in both. The native arm also compiled a local __used bpf_ctx_convert anchor in kernel/bpf/btf.c so complete program context types were retained, and selftests omitted callx_rodata_elem_addr (LLVM BPF MC assert #244366). Original Run B built separate selftest payloads; the coverage rerun used the same baseline selftest initramfs against both saved kernels.

- Stock-link replay (not booted): saved Run B native objects relinked with unmodified ld.lld 3bfae959fb6f and a 329-line libbpf prototype.

- Run A (selftest compiler check): one pahole kernel, selftests/bpf built with stock clang 3bfae959fb6f vs #183929 clang 76d97ce620bf.

Compiler-flag gap vs GCC (verified on GCC 15.2 x86-64): GCC -gbtf alone
emits .BTF without DWARF (both require -g -gbtf), emits no .BTF.ext on
x86-64, and controls pointee pruning with -gprune-btf/-gno-prune-btf (full pointees by default on x86-64). #183929 currently forces DWARF on
and emits .BTF.ext (~49 MB and 3.1M relocations in the archive replay,
all discarded at the executable link).

Saved-object replay (experimental instrument, not a kbuild API)
---------------------------------------------------------------

1. Stock ld.lld -r on the .tmp_vmlinux1 inputs (vmlinux.a plus .vmlinux.export.o, init/version-timestamp.o and .tmp_vmlinux0.kallsyms.o), with -Map, keeping all 3,602 .BTF blobs and their .rela.BTF. Using archive-only vmlinux.o missed 3,324 .modinfo entries from .vmlinux.export.o and picked weak linux_banner/init_uts_ns over the strong definitions in init/version-timestamp.o; ET_REL offsets also don't give final section placement.

2. Stock first executable link (.tmp_vmlinux1) with -Map and a discard
script dropping .BTF, .BTF.ext and their relocations. Keeping .BTF in this link fails on x86-64 because #183929's BTFKindDataSec calls emitLabelReference(sym, 4) (non-section-relative), emitting absolute R_X86_64_32 (S+A) instead of a section-relative offset; lld rightly rejects high-half kernel VAs. That is a producer-side encoding problem for ordinary final links, not a linker quirk.

3. The host prototype strips input DATASECs and extern FUNCs, appends blobs via libbpf, places surviving non-empty/non-overlapping entries from the -r relocations and two lld text maps, emits one DATASEC per non-empty output section and runs btf__dedup(). It is an x86-64
prototype: it hardcodes lld map prefixes, pairs repeated input sections by ordinal/size, uses name/size/bytes fallback for 167 SHF_MERGE entries, drops 14 overlaps and 27,422 script discards, and retains all 96,069 VAR records even when 68,570 entries are placed.

4. resolve_btfids runs unchanged (doing its own fixups, finalize_btf ()
dedup/sort and ID patching).

Single-sample replay timings (not a controlled kbuild benchmark):
2.97M -> 276k types before resolve_btfids; dedup 0.45 s, tool 8.4 s and
~555 MiB peak RSS. The extra augmented -r link took 2:57 (with 177k major page faults; archive-only -r took 1.55 s, stock exec 1.76 s, patched exec 6.65 s).

Results
-------

- Stock-link replay: for the augmented replay, post-resolver bpftool raw dumps compare equal to the patched-lld kernel and .BTF_ids is byte-identical (15,568 bytes) with no unresolved IDs. Raw .BTF differs in bytes (13,554,061 vs 13,554,053, 32- vs 24-byte header and string table). Both logs emit two duplicate-ID warnings (bpf_lsm_inode_init_security and bpf_lsm_mmap_file), which would fail CONFIG_WERROR=y (--fatal_warnings). Augmented sections were embedded and extracted cleanly, but no host-tool image was booted.

- Boot and selftests (Run B, patched-lld vs pahole kernel): both boot and expose /sys/kernel/btf/vmlinux. Original Run B ran only plain test_progs; observed rows matched (OK/FAIL/SKIP 2557/382/113, including subtests) up to a shared userspace segfault in tailcalls (exit 139). bpf_testmod.ko failed with -EINVAL on both, causing failures and skips and limiting module/kfunc coverage. The -cpuv4 and -no_alu32 runners didn't start there. With tailcalls excluded and the shared baseline selftest payload, all 2,099 missing flavour/group invocations ran on both kernels. Five initial diffs did not persist as native-only failures on retry (timer_async_cancel failed 2/7 native vs 1/7 pahole; tracing_multi_bench_attach passed on both on retry). The sole persistent native-only failure is verifier_btf_array_access/flex_array_unaligned_step (see vring_used below).

- Size and per-CPU filter: unfiltered native .BTF is 13.6 MB (96,069 VAR records, 68,570 DATASEC entries in 15 sections) vs 8.0 MB (7,984,202 B) pahole. Filtering the host prototype on the saved objects to keep only .data..percpu VARs and DATASEC entries gives 8.00 MB (8,003,505 B) pre-resolver (0.29 s dedup, 4.8 s runtime, 544,072 KiB process RSS high-water after dedup), 180,130 types, and 369/369 exact matches against both the final shipped ELF (zero stale offsets) and pahole's (name, offset, size). Post-resolver kfunc and bpf_fastcall decl-tag sets match (raw TYPE_TAG counts are 1,059 vs 1,061; raw FUNC linkage is 41,566 static / 39,089 global native vs 78,798 static / 0 global pahole).

- Run A: no reproducible regression attributable to #183929 (initial
run: 10,204/1,604/375 vs 10,204/1,603/375 across binary, test and subtest rows, 5 diffs, shared tailcalls crash; a separate 954-group rerun matched on subtests, with 4 initial group diffs settling to timer_async_cancel 3/7 stock vs 0/7 PR on identical normalized timer.bpf.o).

Differences from pahole
-----------------------

Types. In raw BTF, 132 STRUCT/UNION definitions present in pahole are FWD-only in native. In the normalized C-dump comparison (23,109 exact of 23,216 native and 23,490 pahole declaration rows), only 39 show up in fwd_vs_definition.txt; the other 93 (including vring_used) were paired as "exact" against bpftool's synthetic forward declarations and
filed under extras.txt (111 pahole rows = 93 missing definitions + 18 extra FWDs; 2 native FWDs). Walking pahole's BTF from those 132 roots reaches 149 of 178 pahole-only rows (e.g. acpi_gpio_params via acpi_gpio_mapping).

The cause confirmed per-object in libahci.o (ahci_cmd_hdr) and e1000_hw.o (e1000_shadow_ram) is clang's deferred pointee traversal, which emits a FWD even when the CU's DWARF has the full definition. That has two concrete failures here:

- Verifier false acceptance: struct vring_used is FWD-only in native BTF (ID 271968), so flex_array_unaligned_step in verifier_btf_array_access (which expects rejection of an unaligned variable offset into vring_used's flexible ring[] array) gets unexpected_load_success in all three test_progs flavours and on confirmation, while pahole rejects it as expected.
- Wrong linked type: e1000_hw.o emits only a FWD for e1000_shadow_ram,
and in the merged BTF e1000's member points at e1000e's different same-name struct (different field name).

Pruning pointees is a retention policy, not a BTF-format limit (GCC exposes -gprune-btf/-gno-prune-btf). Remaining C-dump diffs: 51 pairs differing in nested vs flat array shape (e.g. u8 data[4][13] vs [52]; input DWARF not traced); ima_hooks NONE vs NONE___2 dumper spelling; two anonymous enums paired with pahole typedefs; and 29 unreached pahole-only / 11 non-enum native-only rows not yet traced.

Functions. Native has 80,655 FUNC records under 80,024 names vs pahole
1.31's 78,798 (1,227 native-only names, 1 pahole-only name, and 600 names with multiple native records: 17 differing only in linkage, 484 only in parameter names -- including 447 shared single-address syscalls -- and 99 differing in arity or types). The one pahole-only name is native_save_fl: an assembly STT_FUNC in arch/x86/kernel/irqflags.S that pahole joins by name to an inline DWARF
signature, which clang's machine-function emitter omits.

Replaying pahole 1.31's flags on the control DWARF classifies the 1,227
native-only names by inference as:

- 780 ambiguous ELF addresses (625), inconsistent DWARF prototypes (60), or both (95, e.g. add_grec in igmp.c vs mcast.c, 5 vs 6 params);
- 36 with no matching STT_FUNC in .tmp_vmlinux1;
- 411 attributed by exclusion to the unexpected-register check (which can also drop valid ABIs such as ZSTD_findFrameSizeInfo's hidden struct-return pointer).

For __add_to_kill, native BTF lists (tsk, p, vma, to_kill, addr), while
DWARF and entry disassembly put to_kill and addr in the 3rd and 4th argument registers. It has an ftrace site, so an fentry attach using that prototype would read the wrong registers if admitted (attachment was not tested). Counting by name with a concrete native DW_CC_nocall DIE catches 339 of the 411, misses 72, and also marks 601 names present
in pahole 1.31 -- including kcore_update_ram, whose return value is discarded in disassembly while pahole still emits int(void).

In a detached check on the saved baseline .tmp_vmlinux1, pahole 1.32 emits 78,435 FUNCs with default flags and 79,077 (+642) with true_signature, recovering 343 of the 1,227 native-only names (284 unexpected-reg, 59 inconsistent) -- including __add_to_kill with its true 4-parameter ABI (tsk, p, to_kill, addr), while kcore_update_ram stays int(void).

Filtering in a host tool using ELF + BTF can address some ambiguous and missing-symbol cases if per-object provenance is kept (while preserving kfuncs and single-address parameter-name variants like weak
sys_ni.o stubs), but ELF + source BTF alone cannot prove parameter registers or dropped returns.

Lists, config, host.c, pahole 1.32 notes and coverage diffs:

https://gist.github.com/caniko/24da79feb8f405ada030bb5ee18d27ed/
a0ba0b01951d6d2447e6490deba16badb36bda38

Known problems
--------------

- Final-ELF layout drift: against the shipped patched-lld vmlinux, both
unfiltered native graphs match 65,951/68,570 DATASEC entries, with 2,618 stale .rodata offsets (non-uniform deltas after kallsyms), a stale .rodata section size (4,361,141 vs 10,128,309 bytes), and one first+final name mismatch (bringup_idt_table vs __pi_bringup_idt_table
from x86 .pi.o prefixing). The per-CPU-only filter avoided all of these in this build (369/369 exact on final vmlinux); keeping other sections requires refreshing placement across link-vmlinux.sh passes without breaking kallsyms convergence or .BTF_ids.

- GCC 15.2 -gbtf fixtures deliberately write zero DATASEC offsets with
no relocations outside CO-RE, so host.c cannot place GCC VARs.

- Modules still get BTF from pahole --btf_base. One existing split-BTF
bpf_testmod.ko decodes identically in userspace bpftool against both native bases, but native split-BTF generation, .BTF.base distillation and module .BTF_ids patching are untested, loading against a stock-link replay image was not tested, and Run B's load attempts for bpf_testmod.ko failed with -EINVAL on both kernels.

- No LTO, IBT, GNU ld, GCC kernel, Rust, section-GC/COMDAT-loser or non-x86 tests yet.

Questions
---------

0. Trust boundary: if compiler BTF is best-effort debug output (per LLVM DebugInfo feedback [3]), what independent rule lets the kernel promote it to trusted verifier/trampoline input -- and should an incomplete FWD like struct vring_used independently make the verifier fail closed?

1. Functions and ABI: what evidence must bind a FUNC and its argument /
return ABI to the prevailing body, and what must be omitted when that evidence is absent? Given pahole 1.32's true_signature, is there a post-lowering compiler fact worth standardizing separately from ordinary debug metadata?

2. Variable scope: is the tested per-CPU-only policy the right initial
scope for native vmlinux BTF, or is there a concrete consumer justifying final-layout placement for other sections (pahole 1.31 already has opt-in global_var; cf. Brennan's 2022 proposal [8])?

3. Kbuild and linker inputs: can the minimum policy work from ordinary
kbuild artifacts without an extra full -r link, lld-specific text -Map parsing, or a final-layout/ID cycle? If not, what stable provenance interface should linkers provide, and where should the merge step live (tools/bpf vs resolve_btfids)?

4. Cross-compiler ET_REL contract and flags (GCC + Clang + linkers):
(a) What common ET_REL representation should identify each DATASEC entry's defining symbol/section and distinguish real zero offsets from discards and losing definitions (instead of Clang's absolute R_X86_64_32)?
(b) For GCC maintainers (asked on the LLVM RFC [3]): is GCC's behaviour -- -gbtf omitting DWARF unless -g is also passed, no .BTF.ext on non-BPF, and -gprune-btf controlling pointee pruning -- still the intended model for Clang to match?

Until there is agreement on the compiler/kernel contract, I'll hold off
on ABI-signature changes and focus #183929 on matching GCC's -gbtf / -gprune-btf / non-BPF .BTF.ext behaviour cleanly without target-triple
checks.

[1] https://github.com/llvm/llvm-project/pull/183915#
issuecomment-4409372304
[2] https://github.com/llvm/llvm-project/pull/183915#
pullrequestreview-5960724788
[3] https://discourse.llvm.org/t/
rfc-btf-debug-info-for-non-bpf-elf-targets-gbtf/92909
[4] https://lwn.net/Articles/1016243/
[5] https://lpc.events/event/20/contributions/2395/attachments/2141/
4739/2026-lpc.pdf
[6] https://lore.kernel.org/bpf/
[email protected]/
[7] https://lore.kernel.org/bpf/
[email protected]/
[8] https://lore.kernel.org/bpf/
[email protected]/

Thanks,
Can

Note: Claude Code and OpenCode were used to help run artifact analyses,
fact-check numbers against saved dumps, and review this draft.

#2 lore raw

On 27 Nov 2026, Jose E. Marchesi uttered the following:

Quoted 2 lines

[Fixing email adresses of David Faust and Bruce McCullough and Nick
Alcock]

Thanks!

Quoted 19 lines
Quoted 19 lines

Alexei asked in llvm/llvm-project#183915 [1][2] for native-vs-pahole results to come here, and suggested dropping BTF dedup from lld in favour of a stock link plus a libbpf-based kernel tool. I closed #183915 and tested that route on saved x86-64 kernel objects.

Two headline findings up front:

1. On those saved objects, unmodified ld.lld plus a libbpf prototype reproduced the patched linker's decoded vmlinux BTF (bpftool raw dumps match) and byte-identical .BTF_ids, though not byte-identical .BTF and without booting a host-tool image.
2. On the booted native kernel from the earlier patched-lld run, a verifier negative test (flex_array_unaligned_step in verifier_btf_array_access) unexpectedly loads across all three test_progs flavours because struct vring_used is only a FWD in native BTF. Static checks also show shifted-register FUNC prototypes (e.g. __add_to_kill) and two duplicate-ID warnings in resolve_btfids that fail CONFIG_WERROR=y.

Quoted 2 lines
Quoted 2 lines

The compiler side is LLVM #183929 (-gbtf for non-BPF ELF targets); its

Ooh! I thought I was going to have to implement this. :)

Quoted 7 lines
Quoted 7 lines

Discourse RFC [3] drew a key question from LLVM DebugInfo maintainers:
LLVM debug info is best-effort, whereas the verifier and trampolines treat vmlinux BTF as trusted safety and ABI metadata. Before reshaping #183929 or proposing kbuild patches, I'd like feedback on where that trust boundary sits, how functions and DATASECs should be handled, and what GCC/Clang/linker contract makes sense.

I'm quite interested in this too, given my past, ah, arguments over the possibility of structs with conflicting definitions in different TUs, the mess pahole makes of them (because it assumes they don't exist, and accepts the first available struct with a given name it sees), and their occasional appearance in the kernel. We've even had cases where setting the wrong kconfig options can cause wildly different duplicate definitions of things like task_struct, though having structs this important with the wrong definitions causes the BTF self-tests to fail disastrously so hopefully nobody would actually ship a kernel that turned those on.

(I am willing to believe that there is an intent that no such structures exist in the kernel and thus the file format need not accommodate them, though CTF, being meant for userspace, has to be more flexible. I stil don't believe that they don't *appear*, because I've seen them, and there is no obvious means to keep them from reappearing accidentally. A deduplicator is of course the perfect place to implement guards against such things appearing in built kernels, but it has to be able to spot them first...)

Quoted 2 lines
Quoted 2 lines

true_signature [7]. Whatever comes out of this should work for GCC objects too.

I intend to build CTFv4 support for LLVM on top of it, too, following which BTF-compatible (CTFv4) support in LLVM-compiled userspace should be practical, modulo bugs.

Quoted 1 line
Quoted 1 line

Saved-object replay (experimental instrument, not a kbuild API)

This is really neat :)

Quoted 11 lines
Quoted 11 lines

Types. In raw BTF, 132 STRUCT/UNION definitions present in pahole are FWD-only in native. In the normalized C-dump comparison (23,109 exact of 23,216 native and 23,490 pahole declaration rows), only 39 show up in fwd_vs_definition.txt; the other 93 (including vring_used) were paired as "exact" against bpftool's synthetic forward declarations and
filed under extras.txt (111 pahole rows = 93 missing definitions + 18 extra FWDs; 2 native FWDs). Walking pahole's BTF from those 132 roots reaches 149 of 178 pahole-only rows (e.g. acpi_gpio_params via acpi_gpio_mapping).

Quoted 3 lines
Quoted 3 lines

The cause confirmed per-object in libahci.o (ahci_cmd_hdr) and e1000_hw.o (e1000_shadow_ram) is clang's deferred pointee traversal, which emits a FWD even when the CU's DWARF has the full definition.

libctf fuses such things together when it sees them (addition of structs/unions to a dict when a forward already exists mutates the forward into a struct/union; addition of a forward when a struct/union already exists just returns the struct/union; the deduplicator also avoids emitting forwards unless no concrate definition is visible, and by the time it emits anything it's already seen everything there is to see.)

This sort of thing may not be practical in your model though.