Re: [RFC PATCH v1 11/11] scripts: Add eh_frame_min.py .eh_frame coverage and size analyzer

From
bot+bpf-ci@kernel.org
Date
2026-11-23 00:39:56
Also in
bpf, kvmarm, linux-arch, linux-efi, linux-kbuild, linux-kselftest, linux-perf-users, linux-riscv, lkml, rust-for-linux
Reply to
Ian Rogers, 2026-11-22
scripts: Add eh_frame_min.py .eh_frame coverage and size analyzer
TL;DR:
- eh_frame_min.py may abort with a traceback on a valid truncated-looking CFA
  expression, skipping later files
- i386 DRAP registers look wrong, skewing coverage
- combined savings figure for .o/.ko compares unlike sizes
- dead variable and stale comment (nits)
quoted hunk ↗ jump to hunk
diff --git a/scripts/eh_frame_min.py b/scripts/eh_frame_min.py
new file mode 100755
--- /dev/null
+++ b/scripts/eh_frame_min.py
[ ... ]
quoted hunk ↗ jump to hunk
+# DWARF register numbers per ELF e_machine (drap = transient stack-realign scratch regs)
+ARCH_REGS = {
+    62: {"name": "x86_64", "sp": 7, "fp": 6, "ra": 16, "drap": {1, 2, 5, 10, 13}},  # EM_X86_64
+    3:  {"name": "i386",   "sp": 4, "fp": 5, "ra": 8,  "drap": {1, 2}},              # EM_386
In i386 DWARF numbering 1 is %ecx and 2 is %edx.  GCC's i386 DRAP register
is %ecx, or %edi (DWARF 7) when the function emits tail calls, needs a
static chain, or uses regparm 3 as the i386 kernel does.  %edx is never the
DRAP register.  The set {1, 2} looks like the x86_64 numbers reused without
renumbering.

When a stack-realigning function uses %edi as DRAP, the script sees
"DW_CFA_def_cfa: r7 ofs 0", records "def_cfa non-SP/FP reg=7" and counts
the FDE as unsupported, and --minify leaves it out.  The same prologue
using %ecx is counted as supported.

The kernel interpreter (cfi_set_cfa_reg() in kernel/unwind/dwarf_cfi.c) and
scripts/min_eh_frame.c accept either register, so the reported coverage for
i386 inputs differs from what the minifier would keep.  Should the i386
entry be {1, 7}?

[ ... ]
+    has_indirect = False
This isn't a bug, but parse_cie() sets has_indirect to True in the 'L', 'P'
and 'R' augmentation branches and nothing ever reads it.  The returned dict
derives the flag the caller uses independently:

    "has_indirect_fde": bool(fde_enc & DW_EH_PE_indirect),

Could the variable and its assignments be removed?  The "offset", "version",
"aug" and "lsda_enc" entries of the same dict also appear to have no
consumer in analyze_elf() or decode_and_minify_insns().

[ ... ]
+    # Stream of abstract events: ("adv", delta_units) or ("op", raw_bytes, new_row_snapshot)
Is the comment accurate?  emit_op() appends only 2-tuples:

    events.append(("op", raw_bytes))

and the rest of decode_and_minify_insns() relies on that form, for example
the "for kind, payload in events:" loop and the restore_state elision that
compares against ("op", bytes([DW_CFA_remember_state])).  Row snapshots are
kept in state_stack.  Should the comment describe ("op", raw_bytes)?

[ ... ]
quoted hunk ↗ jump to hunk
+        elif op == DW_CFA_def_cfa_expression:
+            expr_len, pos = read_uleb128(insns, pos, end)
+            expr = insns[pos : pos + expr_len]
+            pos += expr_len
+            # Fixed DRAP / sigframe pattern: DW_OP_breg(FP|SP) <sleb128>, DW_OP_deref
+            if (len(expr) >= 3 and expr[-1] == 0x06 and
+                expr[0] in (0x70 + fp_reg, 0x70 + sp_reg)):
+                soff, epos = read_sleb128(expr, 1, len(expr) - 1)
Can this read_sleb128() call raise an uncaught ValueError?  It raises
"Truncated SLEB128" when no terminating byte appears before the end.  A
valid expression whose SLEB128 operand ends in 0x06 with no DW_OP_deref
after it hits this, for example on x86_64:

    DW_CFA_def_cfa_expression 3: DW_OP_breg7 0x80 0x06

Here expr[0] == 0x70 + sp_reg and expr[-1] == 0x06, so the guard passes and
read_sleb128(expr, 1, 2) reads 0x80 with the continuation bit set and runs
off the end.

Nothing catches the exception in decode_and_minify_insns(), analyze_elf() or
main(), so the script aborts with a traceback, later files on the command
line are not analyzed, and --minify writes no output.

The kernel parser treats the same bytes as an unsupported FDE
(cfi_parse_indirect_cfa_expr() returns -EINVAL), and scripts/min_eh_frame.c
does likewise.  Should the analyzer append the opcode name to reasons and
mark the FDE unsupported instead?  The script is aimed at arbitrary
user-space binaries and shared libraries, so the input is not controlled.

[ ... ]
+    min_eh_hdr_size = 12 + supported_fdes * 8
[ ... ]
quoted hunk ↗ jump to hunk
+    orig_total = eh_size + eh_hdr_size
+    min_total = len(min_eh_stream) + min_eh_hdr_size
+    savings_pct = (100.0 * (1.0 - len(min_eh_stream) / eh_size)) if eh_size else 0.0
+    total_savings_pct = (100.0 * (1.0 - min_total / orig_total)) if orig_total else 0.0
[ ... ]
+    print(f"  Minified + .eh_frame_hdr: {min_total:10,d} B  (-{total_savings_pct:.1f}% combined)")
Is the combined figure comparing like with like?  The minified side adds a
simulated .eh_frame_hdr of 12 + 8 bytes per supported FDE, while the
original side uses the input's real .eh_frame_hdr.  Relocatable inputs (.o
and .ko, as in the commit message examples) have no .eh_frame_hdr, so
eh_hdr_size is 0 and only the minified side is charged for the lookup table.

Since minification barely shrinks .eh_frame, total_savings_pct comes out
around -20% for every object or module.

The sign also looks wrong, because the format string prints a literal '-'
before the value, so a negative percentage renders as something like
"(--24.0% combined)".

Could the original side simulate 12 + 8 * total_fdes when the input has no
.eh_frame_hdr, or could the simulated header be skipped when eh_hdr_size is
0, with the sign taken from the value?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45087188604
Thread (26 messages) 26 messages, 3 authors, last reply 1d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help