#!/bin/bash
# The boot kernel is built with CONFIG_DEBUG_INFO_BTF_MODULES=y and WITHOUT CONFIG_MODULE_ALLOW_BTF_MISMATCH.
# Modules built in a slightly different build of the same tree then fail to load ("failed to validate module BTF: -22",
# insmod: Invalid argument). Removing the .BTF section (and the appended signature, which goes away when the ELF is
# rewritten) fixes it; the kernel does not force module signatures.
# usage: strip-module-btf.sh <modules/kernel dir containing *.ko.zst> [extra-module.ko ...]
set -euo pipefail
K="$1"; shift
OBJCOPY=${OBJCOPY:-llvm-objcopy-19}
strip_one() { f="$1"; raw="${f%.zst}"; zstd -dqf "$f" -o "$raw" && "$OBJCOPY" --remove-section=.BTF "$raw" && zstd -q -f -3 "$raw" -o "$f" && rm -f "$raw"; }
export -f strip_one; export OBJCOPY
find "$K" -name '*.ko.zst' -print0 | xargs -0 -n1 -P"$(nproc)" bash -c 'strip_one "$0"'
echo "stripped $(find "$K" -name '*.ko.zst' | wc -l) modules under $K"
