124ebfcd6SJosh Poimboeuf#!/bin/bash 224ebfcd6SJosh Poimboeuf# SPDX-License-Identifier: GPL-2.0 324ebfcd6SJosh Poimboeuf# 424ebfcd6SJosh Poimboeuf# Build a livepatch module 524ebfcd6SJosh Poimboeuf 624ebfcd6SJosh Poimboeuf# shellcheck disable=SC1090,SC2155 724ebfcd6SJosh Poimboeuf 824ebfcd6SJosh Poimboeufif (( BASH_VERSINFO[0] < 4 || \ 924ebfcd6SJosh Poimboeuf (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 4) )); then 1024ebfcd6SJosh Poimboeuf echo "error: this script requires bash 4.4+" >&2 1124ebfcd6SJosh Poimboeuf exit 1 1224ebfcd6SJosh Poimboeuffi 1324ebfcd6SJosh Poimboeuf 1424ebfcd6SJosh Poimboeufset -o errexit 1524ebfcd6SJosh Poimboeufset -o errtrace 1624ebfcd6SJosh Poimboeufset -o pipefail 1724ebfcd6SJosh Poimboeufset -o nounset 1824ebfcd6SJosh Poimboeuf 1924ebfcd6SJosh Poimboeuf# Allow doing 'cmd | mapfile -t array' instead of 'mapfile -t array < <(cmd)'. 2024ebfcd6SJosh Poimboeuf# This helps keep execution in pipes so pipefail+errexit can catch errors. 2124ebfcd6SJosh Poimboeufshopt -s lastpipe 2224ebfcd6SJosh Poimboeuf 2378be9facSJosh Poimboeufunset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP XTRACE 2424ebfcd6SJosh Poimboeuf 2524ebfcd6SJosh PoimboeufREPLACE=1 2624ebfcd6SJosh PoimboeufSHORT_CIRCUIT=0 2724ebfcd6SJosh PoimboeufJOBS="$(getconf _NPROCESSORS_ONLN)" 2824ebfcd6SJosh PoimboeufVERBOSE="-s" 2924ebfcd6SJosh Poimboeufshopt -o xtrace | grep -q 'on' && XTRACE=1 3024ebfcd6SJosh Poimboeuf 3124ebfcd6SJosh Poimboeuf# Avoid removing the previous $TMP_DIR until args have been fully processed. 3224ebfcd6SJosh PoimboeufKEEP_TMP=1 3324ebfcd6SJosh Poimboeuf 3424ebfcd6SJosh PoimboeufSCRIPT="$(basename "$0")" 3524ebfcd6SJosh PoimboeufSCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 3624ebfcd6SJosh PoimboeufFIX_PATCH_LINES="$SCRIPT_DIR/fix-patch-lines" 3724ebfcd6SJosh Poimboeuf 3824ebfcd6SJosh PoimboeufSRC="$(pwd)" 3924ebfcd6SJosh PoimboeufOBJ="$(pwd)" 4024ebfcd6SJosh Poimboeuf 4124ebfcd6SJosh PoimboeufCONFIG="$OBJ/.config" 4224ebfcd6SJosh PoimboeufTMP_DIR="$OBJ/klp-tmp" 4324ebfcd6SJosh Poimboeuf 4424ebfcd6SJosh PoimboeufORIG_DIR="$TMP_DIR/orig" 4524ebfcd6SJosh PoimboeufPATCHED_DIR="$TMP_DIR/patched" 4624ebfcd6SJosh PoimboeufDIFF_DIR="$TMP_DIR/diff" 4724ebfcd6SJosh PoimboeufKMOD_DIR="$TMP_DIR/kmod" 4824ebfcd6SJosh Poimboeuf 4924ebfcd6SJosh PoimboeufSTASH_DIR="$TMP_DIR/stash" 5024ebfcd6SJosh PoimboeufTIMESTAMP="$TMP_DIR/timestamp" 5124ebfcd6SJosh PoimboeufPATCH_TMP_DIR="$TMP_DIR/tmp" 5224ebfcd6SJosh Poimboeuf 5324ebfcd6SJosh PoimboeufKLP_DIFF_LOG="$DIFF_DIR/diff.log" 5424ebfcd6SJosh Poimboeuf 55*1fbc9b85SJoe Lawrence# Terminal output colors 56*1fbc9b85SJoe Lawrenceread -r COLOR_RESET COLOR_BOLD COLOR_ERROR COLOR_WARN <<< "" 57*1fbc9b85SJoe Lawrenceif [[ -t 1 && -t 2 ]]; then 58*1fbc9b85SJoe Lawrence COLOR_RESET="\033[0m" 59*1fbc9b85SJoe Lawrence COLOR_BOLD="\033[1m" 60*1fbc9b85SJoe Lawrence COLOR_ERROR="\033[0;31m" 61*1fbc9b85SJoe Lawrence COLOR_WARN="\033[0;33m" 62*1fbc9b85SJoe Lawrencefi 63*1fbc9b85SJoe Lawrence 6424ebfcd6SJosh Poimboeufgrep0() { 65b4a53519SJoe Lawrence # shellcheck disable=SC2317 6624ebfcd6SJosh Poimboeuf command grep "$@" || true 6724ebfcd6SJosh Poimboeuf} 6824ebfcd6SJosh Poimboeuf 69e4dbf706SJoe Lawrence# Because pipefail is enabled, the grep0 helper should be used instead of 70e4dbf706SJoe Lawrence# grep, otherwise a failed match can propagate to an error. 71e4dbf706SJoe Lawrencegrep() { 72e4dbf706SJoe Lawrence echo "error: $SCRIPT: use grep0 or 'command grep' instead of bare grep" >&2 73e4dbf706SJoe Lawrence exit 1 74e4dbf706SJoe Lawrence} 75e4dbf706SJoe Lawrence 7624ebfcd6SJosh Poimboeufstatus() { 77*1fbc9b85SJoe Lawrence echo -e "${COLOR_BOLD}$*${COLOR_RESET}" 7824ebfcd6SJosh Poimboeuf} 7924ebfcd6SJosh Poimboeuf 8024ebfcd6SJosh Poimboeufwarn() { 81*1fbc9b85SJoe Lawrence echo -e "${COLOR_WARN}warning${COLOR_RESET}: $SCRIPT: $*" >&2 8224ebfcd6SJosh Poimboeuf} 8324ebfcd6SJosh Poimboeuf 8424ebfcd6SJosh Poimboeufdie() { 85*1fbc9b85SJoe Lawrence echo -e "${COLOR_ERROR}error${COLOR_RESET}: $SCRIPT: $*" >&2 8624ebfcd6SJosh Poimboeuf exit 1 8724ebfcd6SJosh Poimboeuf} 8824ebfcd6SJosh Poimboeuf 8924ebfcd6SJosh Poimboeufdeclare -a STASHED_FILES 9024ebfcd6SJosh Poimboeuf 9124ebfcd6SJosh Poimboeufstash_file() { 9224ebfcd6SJosh Poimboeuf local file="$1" 9324ebfcd6SJosh Poimboeuf local rel_file="${file#"$SRC"/}" 9424ebfcd6SJosh Poimboeuf 9524ebfcd6SJosh Poimboeuf [[ ! -e "$file" ]] && die "no file to stash: $file" 9624ebfcd6SJosh Poimboeuf 9724ebfcd6SJosh Poimboeuf mkdir -p "$STASH_DIR/$(dirname "$rel_file")" 9824ebfcd6SJosh Poimboeuf cp -f "$file" "$STASH_DIR/$rel_file" 9924ebfcd6SJosh Poimboeuf 10024ebfcd6SJosh Poimboeuf STASHED_FILES+=("$rel_file") 10124ebfcd6SJosh Poimboeuf} 10224ebfcd6SJosh Poimboeuf 10324ebfcd6SJosh Poimboeufrestore_files() { 10424ebfcd6SJosh Poimboeuf local file 10524ebfcd6SJosh Poimboeuf 10624ebfcd6SJosh Poimboeuf for file in "${STASHED_FILES[@]}"; do 10724ebfcd6SJosh Poimboeuf mv -f "$STASH_DIR/$file" "$SRC/$file" || warn "can't restore file: $file" 10824ebfcd6SJosh Poimboeuf done 10924ebfcd6SJosh Poimboeuf 11024ebfcd6SJosh Poimboeuf STASHED_FILES=() 11124ebfcd6SJosh Poimboeuf} 11224ebfcd6SJosh Poimboeuf 11324ebfcd6SJosh Poimboeufcleanup() { 11424ebfcd6SJosh Poimboeuf set +o nounset 115d36a7343SJoe Lawrence revert_patches 11624ebfcd6SJosh Poimboeuf restore_files 11724ebfcd6SJosh Poimboeuf [[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR" 11824ebfcd6SJosh Poimboeuf return 0 11924ebfcd6SJosh Poimboeuf} 12024ebfcd6SJosh Poimboeuf 12124ebfcd6SJosh Poimboeuftrap_err() { 122*1fbc9b85SJoe Lawrence die "line ${BASH_LINENO[0]}: '$BASH_COMMAND'" 12324ebfcd6SJosh Poimboeuf} 12424ebfcd6SJosh Poimboeuf 12524ebfcd6SJosh Poimboeuftrap cleanup EXIT INT TERM HUP 12624ebfcd6SJosh Poimboeuftrap trap_err ERR 12724ebfcd6SJosh Poimboeuf 12824ebfcd6SJosh Poimboeuf__usage() { 12924ebfcd6SJosh Poimboeuf cat <<EOF 13024ebfcd6SJosh PoimboeufUsage: $SCRIPT [OPTIONS] PATCH_FILE(s) 13124ebfcd6SJosh PoimboeufGenerate a livepatch module. 13224ebfcd6SJosh Poimboeuf 13324ebfcd6SJosh PoimboeufOptions: 13478be9facSJosh Poimboeuf -f, --show-first-changed Show address of first changed instruction 13524ebfcd6SJosh Poimboeuf -j, --jobs=<jobs> Build jobs to run simultaneously [default: $JOBS] 13624ebfcd6SJosh Poimboeuf -o, --output=<file.ko> Output file [default: livepatch-<patch-name>.ko] 13724ebfcd6SJosh Poimboeuf --no-replace Disable livepatch atomic replace 13824ebfcd6SJosh Poimboeuf -v, --verbose Pass V=1 to kernel/module builds 13924ebfcd6SJosh Poimboeuf 14024ebfcd6SJosh PoimboeufAdvanced Options: 1412c2f0b86SJosh Poimboeuf -d, --debug Show symbol/reloc cloning decisions 14224ebfcd6SJosh Poimboeuf -S, --short-circuit=STEP Start at build step (requires prior --keep-tmp) 14324ebfcd6SJosh Poimboeuf 1|orig Build original kernel (default) 14424ebfcd6SJosh Poimboeuf 2|patched Build patched kernel 14524ebfcd6SJosh Poimboeuf 3|diff Diff objects 14624ebfcd6SJosh Poimboeuf 4|kmod Build patch module 14724ebfcd6SJosh Poimboeuf -T, --keep-tmp Preserve tmp dir on exit 14824ebfcd6SJosh Poimboeuf 14924ebfcd6SJosh PoimboeufEOF 15024ebfcd6SJosh Poimboeuf} 15124ebfcd6SJosh Poimboeuf 15224ebfcd6SJosh Poimboeufusage() { 15324ebfcd6SJosh Poimboeuf __usage >&2 15424ebfcd6SJosh Poimboeuf} 15524ebfcd6SJosh Poimboeuf 15624ebfcd6SJosh Poimboeufprocess_args() { 15724ebfcd6SJosh Poimboeuf local keep_tmp=0 15824ebfcd6SJosh Poimboeuf local short 15924ebfcd6SJosh Poimboeuf local long 16024ebfcd6SJosh Poimboeuf local args 16124ebfcd6SJosh Poimboeuf 16278be9facSJosh Poimboeuf short="hfj:o:vdS:T" 16378be9facSJosh Poimboeuf long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,short-circuit:,keep-tmp" 16424ebfcd6SJosh Poimboeuf 16524ebfcd6SJosh Poimboeuf args=$(getopt --options "$short" --longoptions "$long" -- "$@") || { 16624ebfcd6SJosh Poimboeuf echo; usage; exit 16724ebfcd6SJosh Poimboeuf } 16824ebfcd6SJosh Poimboeuf eval set -- "$args" 16924ebfcd6SJosh Poimboeuf 17024ebfcd6SJosh Poimboeuf while true; do 17124ebfcd6SJosh Poimboeuf case "$1" in 17224ebfcd6SJosh Poimboeuf -h | --help) 17324ebfcd6SJosh Poimboeuf usage 17424ebfcd6SJosh Poimboeuf exit 0 17524ebfcd6SJosh Poimboeuf ;; 17678be9facSJosh Poimboeuf -f | --show-first-changed) 17778be9facSJosh Poimboeuf DIFF_CHECKSUM=1 17878be9facSJosh Poimboeuf shift 17978be9facSJosh Poimboeuf ;; 18024ebfcd6SJosh Poimboeuf -j | --jobs) 18124ebfcd6SJosh Poimboeuf JOBS="$2" 18224ebfcd6SJosh Poimboeuf shift 2 18324ebfcd6SJosh Poimboeuf ;; 18424ebfcd6SJosh Poimboeuf -o | --output) 18524ebfcd6SJosh Poimboeuf [[ "$2" != *.ko ]] && die "output filename should end with .ko" 18624ebfcd6SJosh Poimboeuf OUTFILE="$2" 18724ebfcd6SJosh Poimboeuf NAME="$(basename "$OUTFILE")" 18824ebfcd6SJosh Poimboeuf NAME="${NAME%.ko}" 18924ebfcd6SJosh Poimboeuf NAME="$(module_name_string "$NAME")" 19024ebfcd6SJosh Poimboeuf shift 2 19124ebfcd6SJosh Poimboeuf ;; 19224ebfcd6SJosh Poimboeuf --no-replace) 19324ebfcd6SJosh Poimboeuf REPLACE=0 19424ebfcd6SJosh Poimboeuf shift 19524ebfcd6SJosh Poimboeuf ;; 19624ebfcd6SJosh Poimboeuf -v | --verbose) 19724ebfcd6SJosh Poimboeuf VERBOSE="V=1" 19824ebfcd6SJosh Poimboeuf shift 19924ebfcd6SJosh Poimboeuf ;; 2002c2f0b86SJosh Poimboeuf -d | --debug) 2012c2f0b86SJosh Poimboeuf DEBUG_CLONE=1 2022c2f0b86SJosh Poimboeuf keep_tmp=1 2032c2f0b86SJosh Poimboeuf shift 2042c2f0b86SJosh Poimboeuf ;; 20524ebfcd6SJosh Poimboeuf -S | --short-circuit) 20624ebfcd6SJosh Poimboeuf [[ ! -d "$TMP_DIR" ]] && die "--short-circuit requires preserved klp-tmp dir" 20724ebfcd6SJosh Poimboeuf keep_tmp=1 20824ebfcd6SJosh Poimboeuf case "$2" in 20924ebfcd6SJosh Poimboeuf 1 | orig) SHORT_CIRCUIT=1; ;; 21024ebfcd6SJosh Poimboeuf 2 | patched) SHORT_CIRCUIT=2; ;; 21124ebfcd6SJosh Poimboeuf 3 | diff) SHORT_CIRCUIT=3; ;; 21224ebfcd6SJosh Poimboeuf 4 | mod) SHORT_CIRCUIT=4; ;; 21324ebfcd6SJosh Poimboeuf *) die "invalid short-circuit step '$2'" ;; 21424ebfcd6SJosh Poimboeuf esac 21524ebfcd6SJosh Poimboeuf shift 2 21624ebfcd6SJosh Poimboeuf ;; 21724ebfcd6SJosh Poimboeuf -T | --keep-tmp) 21824ebfcd6SJosh Poimboeuf keep_tmp=1 21924ebfcd6SJosh Poimboeuf shift 22024ebfcd6SJosh Poimboeuf ;; 22124ebfcd6SJosh Poimboeuf --) 22224ebfcd6SJosh Poimboeuf shift 22324ebfcd6SJosh Poimboeuf break 22424ebfcd6SJosh Poimboeuf ;; 22524ebfcd6SJosh Poimboeuf *) 22624ebfcd6SJosh Poimboeuf usage 22724ebfcd6SJosh Poimboeuf exit 1 22824ebfcd6SJosh Poimboeuf ;; 22924ebfcd6SJosh Poimboeuf esac 23024ebfcd6SJosh Poimboeuf done 23124ebfcd6SJosh Poimboeuf 232e506ad21SJoe Lawrence if [[ $# -eq 0 ]] && (( SHORT_CIRCUIT <= 2 )); then 23324ebfcd6SJosh Poimboeuf usage 23424ebfcd6SJosh Poimboeuf exit 1 23524ebfcd6SJosh Poimboeuf fi 23624ebfcd6SJosh Poimboeuf 23724ebfcd6SJosh Poimboeuf KEEP_TMP="$keep_tmp" 23824ebfcd6SJosh Poimboeuf PATCHES=("$@") 23924ebfcd6SJosh Poimboeuf} 24024ebfcd6SJosh Poimboeuf 24124ebfcd6SJosh Poimboeuf# temporarily disable xtrace for especially verbose code 24224ebfcd6SJosh Poimboeufxtrace_save() { 24324ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set +x 24424ebfcd6SJosh Poimboeuf return 0 24524ebfcd6SJosh Poimboeuf} 24624ebfcd6SJosh Poimboeuf 24724ebfcd6SJosh Poimboeufxtrace_restore() { 24824ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set -x 24924ebfcd6SJosh Poimboeuf return 0 25024ebfcd6SJosh Poimboeuf} 25124ebfcd6SJosh Poimboeuf 25224ebfcd6SJosh Poimboeufvalidate_config() { 25324ebfcd6SJosh Poimboeuf xtrace_save "reading .config" 25424ebfcd6SJosh Poimboeuf source "$CONFIG" || die "no .config file in $(dirname "$CONFIG")" 25524ebfcd6SJosh Poimboeuf xtrace_restore 25624ebfcd6SJosh Poimboeuf 25724ebfcd6SJosh Poimboeuf [[ -v CONFIG_LIVEPATCH ]] || \ 25824ebfcd6SJosh Poimboeuf die "CONFIG_LIVEPATCH not enabled" 25924ebfcd6SJosh Poimboeuf 26024ebfcd6SJosh Poimboeuf [[ -v CONFIG_KLP_BUILD ]] || \ 26124ebfcd6SJosh Poimboeuf die "CONFIG_KLP_BUILD not enabled" 26224ebfcd6SJosh Poimboeuf 26324ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_LATENT_ENTROPY ]] && \ 26424ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_LATENT_ENTROPY' not supported" 26524ebfcd6SJosh Poimboeuf 26624ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_RANDSTRUCT ]] && \ 26724ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_RANDSTRUCT' not supported" 26824ebfcd6SJosh Poimboeuf 269a8ff29f0SJosh Poimboeuf [[ -v CONFIG_AS_IS_LLVM ]] && \ 270a8ff29f0SJosh Poimboeuf [[ "$CONFIG_AS_VERSION" -lt 200000 ]] && \ 271a8ff29f0SJosh Poimboeuf die "Clang assembler version < 20 not supported" 272a8ff29f0SJosh Poimboeuf 27324ebfcd6SJosh Poimboeuf return 0 27424ebfcd6SJosh Poimboeuf} 27524ebfcd6SJosh Poimboeuf 27624ebfcd6SJosh Poimboeuf# Only allow alphanumerics and '_' and '-' in the module name. Everything else 27724ebfcd6SJosh Poimboeuf# is replaced with '-'. Also truncate to 55 chars so the full name + NUL 27824ebfcd6SJosh Poimboeuf# terminator fits in the kernel's 56-byte module name array. 27924ebfcd6SJosh Poimboeufmodule_name_string() { 28024ebfcd6SJosh Poimboeuf echo "${1//[^a-zA-Z0-9_-]/-}" | cut -c 1-55 28124ebfcd6SJosh Poimboeuf} 28224ebfcd6SJosh Poimboeuf 28324ebfcd6SJosh Poimboeuf# If the module name wasn't specified on the cmdline with --output, give it a 28424ebfcd6SJosh Poimboeuf# name based on the patch name. 28524ebfcd6SJosh Poimboeufset_module_name() { 28624ebfcd6SJosh Poimboeuf [[ -v NAME ]] && return 0 28724ebfcd6SJosh Poimboeuf 28824ebfcd6SJosh Poimboeuf if [[ "${#PATCHES[@]}" -eq 1 ]]; then 28924ebfcd6SJosh Poimboeuf NAME="$(basename "${PATCHES[0]}")" 29024ebfcd6SJosh Poimboeuf NAME="${NAME%.*}" 29124ebfcd6SJosh Poimboeuf else 29224ebfcd6SJosh Poimboeuf NAME="patch" 29324ebfcd6SJosh Poimboeuf fi 29424ebfcd6SJosh Poimboeuf 29524ebfcd6SJosh Poimboeuf NAME="livepatch-$NAME" 29624ebfcd6SJosh Poimboeuf NAME="$(module_name_string "$NAME")" 29724ebfcd6SJosh Poimboeuf 29824ebfcd6SJosh Poimboeuf OUTFILE="$NAME.ko" 29924ebfcd6SJosh Poimboeuf} 30024ebfcd6SJosh Poimboeuf 30124ebfcd6SJosh Poimboeuf# Hardcode the value printed by the localversion script to prevent patch 302d36a7343SJoe Lawrence# application from appending it with '+' due to a dirty working tree. 30324ebfcd6SJosh Poimboeufset_kernelversion() { 30424ebfcd6SJosh Poimboeuf local file="$SRC/scripts/setlocalversion" 30524ebfcd6SJosh Poimboeuf local localversion 30624ebfcd6SJosh Poimboeuf 30724ebfcd6SJosh Poimboeuf stash_file "$file" 30824ebfcd6SJosh Poimboeuf 30924ebfcd6SJosh Poimboeuf localversion="$(cd "$SRC" && make --no-print-directory kernelversion)" 31024ebfcd6SJosh Poimboeuf localversion="$(cd "$SRC" && KERNELVERSION="$localversion" ./scripts/setlocalversion)" 31124ebfcd6SJosh Poimboeuf [[ -z "$localversion" ]] && die "setlocalversion failed" 31224ebfcd6SJosh Poimboeuf 31324ebfcd6SJosh Poimboeuf sed -i "2i echo $localversion; exit 0" scripts/setlocalversion 31424ebfcd6SJosh Poimboeuf} 31524ebfcd6SJosh Poimboeuf 316757bd10fSJoe Lawrenceget_patch_input_files() { 317757bd10fSJoe Lawrence local patch="$1" 318757bd10fSJoe Lawrence 319757bd10fSJoe Lawrence grep0 -E '^--- ' "$patch" \ 320d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 321757bd10fSJoe Lawrence | gawk '{print $2}' \ 322757bd10fSJoe Lawrence | sed 's|^[^/]*/||' \ 323757bd10fSJoe Lawrence | sort -u 324757bd10fSJoe Lawrence} 325757bd10fSJoe Lawrence 326757bd10fSJoe Lawrenceget_patch_output_files() { 327757bd10fSJoe Lawrence local patch="$1" 328757bd10fSJoe Lawrence 329757bd10fSJoe Lawrence grep0 -E '^\+\+\+ ' "$patch" \ 330d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 331757bd10fSJoe Lawrence | gawk '{print $2}' \ 332757bd10fSJoe Lawrence | sed 's|^[^/]*/||' \ 333757bd10fSJoe Lawrence | sort -u 334757bd10fSJoe Lawrence} 335757bd10fSJoe Lawrence 33624ebfcd6SJosh Poimboeufget_patch_files() { 33724ebfcd6SJosh Poimboeuf local patch="$1" 33824ebfcd6SJosh Poimboeuf 339757bd10fSJoe Lawrence { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \ 34024ebfcd6SJosh Poimboeuf | sort -u 34124ebfcd6SJosh Poimboeuf} 34224ebfcd6SJosh Poimboeuf 34324ebfcd6SJosh Poimboeufcheck_unsupported_patches() { 34424ebfcd6SJosh Poimboeuf local patch 34524ebfcd6SJosh Poimboeuf 34624ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 34724ebfcd6SJosh Poimboeuf local files=() 34824ebfcd6SJosh Poimboeuf 34924ebfcd6SJosh Poimboeuf get_patch_files "$patch" | mapfile -t files 35024ebfcd6SJosh Poimboeuf 35124ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 35224ebfcd6SJosh Poimboeuf case "$file" in 35324ebfcd6SJosh Poimboeuf lib/*|*.S) 354b41d8b7dSJoe Lawrence die "${patch}: unsupported patch to $file" 35524ebfcd6SJosh Poimboeuf ;; 35624ebfcd6SJosh Poimboeuf esac 35724ebfcd6SJosh Poimboeuf done 35824ebfcd6SJosh Poimboeuf done 35924ebfcd6SJosh Poimboeuf} 36024ebfcd6SJosh Poimboeuf 36124ebfcd6SJosh Poimboeufapply_patch() { 36224ebfcd6SJosh Poimboeuf local patch="$1" 36324ebfcd6SJosh Poimboeuf 36424ebfcd6SJosh Poimboeuf [[ ! -f "$patch" ]] && die "$patch doesn't exist" 365d36a7343SJoe Lawrence patch -d "$SRC" -p1 --dry-run --silent --no-backup-if-mismatch -r /dev/null < "$patch" 366d36a7343SJoe Lawrence patch -d "$SRC" -p1 --silent --no-backup-if-mismatch -r /dev/null < "$patch" 36724ebfcd6SJosh Poimboeuf 36824ebfcd6SJosh Poimboeuf APPLIED_PATCHES+=("$patch") 36924ebfcd6SJosh Poimboeuf} 37024ebfcd6SJosh Poimboeuf 37124ebfcd6SJosh Poimboeufrevert_patch() { 37224ebfcd6SJosh Poimboeuf local patch="$1" 37324ebfcd6SJosh Poimboeuf local tmp=() 37424ebfcd6SJosh Poimboeuf 375d36a7343SJoe Lawrence patch -d "$SRC" -p1 -R --silent --no-backup-if-mismatch -r /dev/null < "$patch" 37624ebfcd6SJosh Poimboeuf 37724ebfcd6SJosh Poimboeuf for p in "${APPLIED_PATCHES[@]}"; do 37824ebfcd6SJosh Poimboeuf [[ "$p" == "$patch" ]] && continue 37924ebfcd6SJosh Poimboeuf tmp+=("$p") 38024ebfcd6SJosh Poimboeuf done 38124ebfcd6SJosh Poimboeuf 38224ebfcd6SJosh Poimboeuf APPLIED_PATCHES=("${tmp[@]}") 38324ebfcd6SJosh Poimboeuf} 38424ebfcd6SJosh Poimboeuf 38524ebfcd6SJosh Poimboeufapply_patches() { 38624ebfcd6SJosh Poimboeuf local patch 38724ebfcd6SJosh Poimboeuf 38824ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 38924ebfcd6SJosh Poimboeuf apply_patch "$patch" 39024ebfcd6SJosh Poimboeuf done 39124ebfcd6SJosh Poimboeuf} 39224ebfcd6SJosh Poimboeuf 39324ebfcd6SJosh Poimboeufrevert_patches() { 39424ebfcd6SJosh Poimboeuf local patches=("${APPLIED_PATCHES[@]}") 39524ebfcd6SJosh Poimboeuf 39624ebfcd6SJosh Poimboeuf for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do 397d36a7343SJoe Lawrence revert_patch "${patches[$i]}" 39824ebfcd6SJosh Poimboeuf done 39924ebfcd6SJosh Poimboeuf 40024ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 40124ebfcd6SJosh Poimboeuf} 40224ebfcd6SJosh Poimboeuf 40324ebfcd6SJosh Poimboeufvalidate_patches() { 40424ebfcd6SJosh Poimboeuf check_unsupported_patches 40524ebfcd6SJosh Poimboeuf apply_patches 40624ebfcd6SJosh Poimboeuf revert_patches 40724ebfcd6SJosh Poimboeuf} 40824ebfcd6SJosh Poimboeuf 40924ebfcd6SJosh Poimboeufdo_init() { 41024ebfcd6SJosh Poimboeuf # We're not yet smart enough to handle anything other than in-tree 41124ebfcd6SJosh Poimboeuf # builds in pwd. 41224ebfcd6SJosh Poimboeuf [[ ! "$SRC" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory" 41324ebfcd6SJosh Poimboeuf [[ ! "$OBJ" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory" 41424ebfcd6SJosh Poimboeuf 41524ebfcd6SJosh Poimboeuf (( SHORT_CIRCUIT <= 1 )) && rm -rf "$TMP_DIR" 41624ebfcd6SJosh Poimboeuf mkdir -p "$TMP_DIR" 41724ebfcd6SJosh Poimboeuf 41824ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 41924ebfcd6SJosh Poimboeuf 42024ebfcd6SJosh Poimboeuf [[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines" 421d36a7343SJoe Lawrence command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)" 42224ebfcd6SJosh Poimboeuf 42324ebfcd6SJosh Poimboeuf validate_config 42424ebfcd6SJosh Poimboeuf set_module_name 42524ebfcd6SJosh Poimboeuf set_kernelversion 42624ebfcd6SJosh Poimboeuf} 42724ebfcd6SJosh Poimboeuf 42824ebfcd6SJosh Poimboeuf# Refresh the patch hunk headers, specifically the line numbers and counts. 42924ebfcd6SJosh Poimboeufrefresh_patch() { 43024ebfcd6SJosh Poimboeuf local patch="$1" 43124ebfcd6SJosh Poimboeuf local tmpdir="$PATCH_TMP_DIR" 432757bd10fSJoe Lawrence local input_files=() 433757bd10fSJoe Lawrence local output_files=() 43424ebfcd6SJosh Poimboeuf 43524ebfcd6SJosh Poimboeuf rm -rf "$tmpdir" 43624ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/a" 43724ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/b" 43824ebfcd6SJosh Poimboeuf 43924ebfcd6SJosh Poimboeuf # Get all source files affected by the patch 440757bd10fSJoe Lawrence get_patch_input_files "$patch" | mapfile -t input_files 441757bd10fSJoe Lawrence get_patch_output_files "$patch" | mapfile -t output_files 44224ebfcd6SJosh Poimboeuf 44324ebfcd6SJosh Poimboeuf # Copy orig source files to 'a' 444757bd10fSJoe Lawrence ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) 44524ebfcd6SJosh Poimboeuf 44624ebfcd6SJosh Poimboeuf # Copy patched source files to 'b' 447d36a7343SJoe Lawrence apply_patch "$patch" 448757bd10fSJoe Lawrence ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) 449d36a7343SJoe Lawrence revert_patch "$patch" 45024ebfcd6SJosh Poimboeuf 45124ebfcd6SJosh Poimboeuf # Diff 'a' and 'b' to make a clean patch 452d36a7343SJoe Lawrence ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true 45324ebfcd6SJosh Poimboeuf} 45424ebfcd6SJosh Poimboeuf 45524ebfcd6SJosh Poimboeuf# Copy the patches to a temporary directory, fix their lines so as not to 45624ebfcd6SJosh Poimboeuf# affect the __LINE__ macro for otherwise unchanged functions further down the 45724ebfcd6SJosh Poimboeuf# file, and update $PATCHES to point to the fixed patches. 45824ebfcd6SJosh Poimboeuffix_patches() { 45924ebfcd6SJosh Poimboeuf local idx 46024ebfcd6SJosh Poimboeuf local i 46124ebfcd6SJosh Poimboeuf 46224ebfcd6SJosh Poimboeuf rm -f "$TMP_DIR"/*.patch 46324ebfcd6SJosh Poimboeuf 46424ebfcd6SJosh Poimboeuf idx=0001 46524ebfcd6SJosh Poimboeuf for i in "${!PATCHES[@]}"; do 46624ebfcd6SJosh Poimboeuf local old_patch="${PATCHES[$i]}" 46724ebfcd6SJosh Poimboeuf local tmp_patch="$TMP_DIR/tmp.patch" 46824ebfcd6SJosh Poimboeuf local patch="${PATCHES[$i]}" 46924ebfcd6SJosh Poimboeuf local new_patch 47024ebfcd6SJosh Poimboeuf 47124ebfcd6SJosh Poimboeuf new_patch="$TMP_DIR/$idx-fixed-$(basename "$patch")" 47224ebfcd6SJosh Poimboeuf 47324ebfcd6SJosh Poimboeuf cp -f "$old_patch" "$tmp_patch" 47424ebfcd6SJosh Poimboeuf refresh_patch "$tmp_patch" 475d36a7343SJoe Lawrence "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" 47624ebfcd6SJosh Poimboeuf 47724ebfcd6SJosh Poimboeuf PATCHES[i]="$new_patch" 47824ebfcd6SJosh Poimboeuf 47924ebfcd6SJosh Poimboeuf rm -f "$tmp_patch" 48024ebfcd6SJosh Poimboeuf idx=$(printf "%04d" $(( 10#$idx + 1 ))) 48124ebfcd6SJosh Poimboeuf done 48224ebfcd6SJosh Poimboeuf} 48324ebfcd6SJosh Poimboeuf 48424ebfcd6SJosh Poimboeufclean_kernel() { 48524ebfcd6SJosh Poimboeuf local cmd=() 48624ebfcd6SJosh Poimboeuf 48724ebfcd6SJosh Poimboeuf cmd=("make") 48824ebfcd6SJosh Poimboeuf cmd+=("--silent") 48924ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 49024ebfcd6SJosh Poimboeuf cmd+=("clean") 49124ebfcd6SJosh Poimboeuf 49224ebfcd6SJosh Poimboeuf ( 49324ebfcd6SJosh Poimboeuf cd "$SRC" 49424ebfcd6SJosh Poimboeuf "${cmd[@]}" 49524ebfcd6SJosh Poimboeuf ) 49624ebfcd6SJosh Poimboeuf} 49724ebfcd6SJosh Poimboeuf 49824ebfcd6SJosh Poimboeufbuild_kernel() { 499b41d8b7dSJoe Lawrence local build="$1" 50024ebfcd6SJosh Poimboeuf local log="$TMP_DIR/build.log" 5012092007aSJosh Poimboeuf local objtool_args=() 50224ebfcd6SJosh Poimboeuf local cmd=() 50324ebfcd6SJosh Poimboeuf 5042092007aSJosh Poimboeuf objtool_args=("--checksum") 5052092007aSJosh Poimboeuf 50624ebfcd6SJosh Poimboeuf cmd=("make") 50724ebfcd6SJosh Poimboeuf 50824ebfcd6SJosh Poimboeuf # When a patch to a kernel module references a newly created unexported 50924ebfcd6SJosh Poimboeuf # symbol which lives in vmlinux or another kernel module, the patched 51024ebfcd6SJosh Poimboeuf # kernel build fails with the following error: 51124ebfcd6SJosh Poimboeuf # 51224ebfcd6SJosh Poimboeuf # ERROR: modpost: "klp_string" [fs/xfs/xfs.ko] undefined! 51324ebfcd6SJosh Poimboeuf # 51424ebfcd6SJosh Poimboeuf # The undefined symbols are working as designed in that case. They get 51524ebfcd6SJosh Poimboeuf # resolved later when the livepatch module build link pulls all the 51624ebfcd6SJosh Poimboeuf # disparate objects together into the same kernel module. 51724ebfcd6SJosh Poimboeuf # 51824ebfcd6SJosh Poimboeuf # It would be good to have a way to tell modpost to skip checking for 51924ebfcd6SJosh Poimboeuf # undefined symbols altogether. For now, just convert the error to a 52024ebfcd6SJosh Poimboeuf # warning with KBUILD_MODPOST_WARN, and grep out the warning to avoid 52124ebfcd6SJosh Poimboeuf # confusing the user. 52224ebfcd6SJosh Poimboeuf # 52324ebfcd6SJosh Poimboeuf cmd+=("KBUILD_MODPOST_WARN=1") 52424ebfcd6SJosh Poimboeuf 52524ebfcd6SJosh Poimboeuf cmd+=("$VERBOSE") 52624ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 52724ebfcd6SJosh Poimboeuf cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") 5282092007aSJosh Poimboeuf cmd+=("OBJTOOL_ARGS=${objtool_args[*]}") 52924ebfcd6SJosh Poimboeuf cmd+=("vmlinux") 53024ebfcd6SJosh Poimboeuf cmd+=("modules") 53124ebfcd6SJosh Poimboeuf 53224ebfcd6SJosh Poimboeuf ( 53324ebfcd6SJosh Poimboeuf cd "$SRC" 53424ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 53524ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 53624ebfcd6SJosh Poimboeuf 2> >(tee -a "$log" | grep0 -v "modpost.*undefined!" >&2) 537b41d8b7dSJoe Lawrence ) || die "$build kernel build failed" 53824ebfcd6SJosh Poimboeuf} 53924ebfcd6SJosh Poimboeuf 54024ebfcd6SJosh Poimboeuffind_objects() { 54124ebfcd6SJosh Poimboeuf local opts=("$@") 54224ebfcd6SJosh Poimboeuf 54324ebfcd6SJosh Poimboeuf # Find root-level vmlinux.o and non-root-level .ko files, 54424ebfcd6SJosh Poimboeuf # excluding klp-tmp/ and .git/ 54524ebfcd6SJosh Poimboeuf find "$OBJ" \( -path "$TMP_DIR" -o -path "$OBJ/.git" -o -regex "$OBJ/[^/][^/]*\.ko" \) -prune -o \ 54624ebfcd6SJosh Poimboeuf -type f "${opts[@]}" \ 54724ebfcd6SJosh Poimboeuf \( -name "*.ko" -o -path "$OBJ/vmlinux.o" \) \ 54824ebfcd6SJosh Poimboeuf -printf '%P\n' 54924ebfcd6SJosh Poimboeuf} 55024ebfcd6SJosh Poimboeuf 55124ebfcd6SJosh Poimboeuf# Copy all .o archives to $ORIG_DIR 55224ebfcd6SJosh Poimboeufcopy_orig_objects() { 55324ebfcd6SJosh Poimboeuf local files=() 55424ebfcd6SJosh Poimboeuf 55524ebfcd6SJosh Poimboeuf rm -rf "$ORIG_DIR" 55624ebfcd6SJosh Poimboeuf mkdir -p "$ORIG_DIR" 55724ebfcd6SJosh Poimboeuf 55824ebfcd6SJosh Poimboeuf find_objects | mapfile -t files 55924ebfcd6SJosh Poimboeuf 56024ebfcd6SJosh Poimboeuf xtrace_save "copying orig objects" 56124ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 56224ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 56324ebfcd6SJosh Poimboeuf local file="$OBJ/$rel_file" 56424ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 56524ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 56624ebfcd6SJosh Poimboeuf 56724ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 56824ebfcd6SJosh Poimboeuf 56924ebfcd6SJosh Poimboeuf mkdir -p "$orig_dir" 57024ebfcd6SJosh Poimboeuf cp -f "$file" "$orig_dir" 57124ebfcd6SJosh Poimboeuf done 57224ebfcd6SJosh Poimboeuf xtrace_restore 57324ebfcd6SJosh Poimboeuf 57424ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$ORIG_DIR" 57524ebfcd6SJosh Poimboeuf touch "$TIMESTAMP" 57624ebfcd6SJosh Poimboeuf} 57724ebfcd6SJosh Poimboeuf 57824ebfcd6SJosh Poimboeuf# Copy all changed objects to $PATCHED_DIR 57924ebfcd6SJosh Poimboeufcopy_patched_objects() { 58024ebfcd6SJosh Poimboeuf local files=() 58124ebfcd6SJosh Poimboeuf local opts=() 58224ebfcd6SJosh Poimboeuf local found=0 58324ebfcd6SJosh Poimboeuf 58424ebfcd6SJosh Poimboeuf rm -rf "$PATCHED_DIR" 58524ebfcd6SJosh Poimboeuf mkdir -p "$PATCHED_DIR" 58624ebfcd6SJosh Poimboeuf 58724ebfcd6SJosh Poimboeuf # Note this doesn't work with some configs, thus the 'cmp' below. 58824ebfcd6SJosh Poimboeuf opts=("-newer") 58924ebfcd6SJosh Poimboeuf opts+=("$TIMESTAMP") 59024ebfcd6SJosh Poimboeuf 59124ebfcd6SJosh Poimboeuf find_objects "${opts[@]}" | mapfile -t files 59224ebfcd6SJosh Poimboeuf 59324ebfcd6SJosh Poimboeuf xtrace_save "copying changed objects" 59424ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 59524ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 59624ebfcd6SJosh Poimboeuf local file="$OBJ/$rel_file" 59724ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 59824ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 59924ebfcd6SJosh Poimboeuf local patched_dir="$(dirname "$patched_file")" 60024ebfcd6SJosh Poimboeuf 60124ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 60224ebfcd6SJosh Poimboeuf 60324ebfcd6SJosh Poimboeuf cmp -s "$orig_file" "$file" && continue 60424ebfcd6SJosh Poimboeuf 60524ebfcd6SJosh Poimboeuf mkdir -p "$patched_dir" 60624ebfcd6SJosh Poimboeuf cp -f "$file" "$patched_dir" 60724ebfcd6SJosh Poimboeuf found=1 60824ebfcd6SJosh Poimboeuf done 60924ebfcd6SJosh Poimboeuf xtrace_restore 61024ebfcd6SJosh Poimboeuf 61124ebfcd6SJosh Poimboeuf (( found == 0 )) && die "no changes detected" 61224ebfcd6SJosh Poimboeuf 61324ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$PATCHED_DIR" 61424ebfcd6SJosh Poimboeuf} 61524ebfcd6SJosh Poimboeuf 61624ebfcd6SJosh Poimboeuf# Diff changed objects, writing output object to $DIFF_DIR 61724ebfcd6SJosh Poimboeufdiff_objects() { 61824ebfcd6SJosh Poimboeuf local log="$KLP_DIFF_LOG" 61924ebfcd6SJosh Poimboeuf local files=() 6202c2f0b86SJosh Poimboeuf local opts=() 62124ebfcd6SJosh Poimboeuf 62224ebfcd6SJosh Poimboeuf rm -rf "$DIFF_DIR" 62324ebfcd6SJosh Poimboeuf mkdir -p "$DIFF_DIR" 62424ebfcd6SJosh Poimboeuf 62524ebfcd6SJosh Poimboeuf find "$PATCHED_DIR" -type f -name "*.o" | mapfile -t files 62624ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 62724ebfcd6SJosh Poimboeuf 6282c2f0b86SJosh Poimboeuf [[ -v DEBUG_CLONE ]] && opts=("--debug") 6292c2f0b86SJosh Poimboeuf 63024ebfcd6SJosh Poimboeuf # Diff all changed objects 63124ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 63224ebfcd6SJosh Poimboeuf local rel_file="${file#"$PATCHED_DIR"/}" 63324ebfcd6SJosh Poimboeuf local orig_file="$rel_file" 63424ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 63524ebfcd6SJosh Poimboeuf local out_file="$DIFF_DIR/$rel_file" 63678be9facSJosh Poimboeuf local filter=() 63724ebfcd6SJosh Poimboeuf local cmd=() 63824ebfcd6SJosh Poimboeuf 63924ebfcd6SJosh Poimboeuf mkdir -p "$(dirname "$out_file")" 64024ebfcd6SJosh Poimboeuf 64124ebfcd6SJosh Poimboeuf cmd=("$SRC/tools/objtool/objtool") 64224ebfcd6SJosh Poimboeuf cmd+=("klp") 64324ebfcd6SJosh Poimboeuf cmd+=("diff") 6442c2f0b86SJosh Poimboeuf (( ${#opts[@]} > 0 )) && cmd+=("${opts[@]}") 64524ebfcd6SJosh Poimboeuf cmd+=("$orig_file") 64624ebfcd6SJosh Poimboeuf cmd+=("$patched_file") 64724ebfcd6SJosh Poimboeuf cmd+=("$out_file") 64824ebfcd6SJosh Poimboeuf 64978be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 65078be9facSJosh Poimboeuf filter=("grep0") 65178be9facSJosh Poimboeuf filter+=("-Ev") 65278be9facSJosh Poimboeuf filter+=("DEBUG: .*checksum: ") 65378be9facSJosh Poimboeuf else 65478be9facSJosh Poimboeuf filter=("cat") 65578be9facSJosh Poimboeuf fi 65678be9facSJosh Poimboeuf 65724ebfcd6SJosh Poimboeuf ( 65824ebfcd6SJosh Poimboeuf cd "$ORIG_DIR" 65924ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 66024ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 66178be9facSJosh Poimboeuf 2> >(tee -a "$log" | "${filter[@]}" >&2) || \ 66224ebfcd6SJosh Poimboeuf die "objtool klp diff failed" 66324ebfcd6SJosh Poimboeuf ) 66424ebfcd6SJosh Poimboeuf done 66524ebfcd6SJosh Poimboeuf} 66624ebfcd6SJosh Poimboeuf 66778be9facSJosh Poimboeuf# For each changed object, run objtool with --debug-checksum to get the 66878be9facSJosh Poimboeuf# per-instruction checksums, and then diff those to find the first changed 66978be9facSJosh Poimboeuf# instruction for each function. 67078be9facSJosh Poimboeufdiff_checksums() { 67178be9facSJosh Poimboeuf local orig_log="$ORIG_DIR/checksum.log" 67278be9facSJosh Poimboeuf local patched_log="$PATCHED_DIR/checksum.log" 67378be9facSJosh Poimboeuf local -A funcs 67478be9facSJosh Poimboeuf local cmd=() 67578be9facSJosh Poimboeuf local line 67678be9facSJosh Poimboeuf local file 67778be9facSJosh Poimboeuf local func 67878be9facSJosh Poimboeuf 67978be9facSJosh Poimboeuf gawk '/\.o: changed function: / { 68078be9facSJosh Poimboeuf sub(/:$/, "", $1) 68178be9facSJosh Poimboeuf print $1, $NF 68278be9facSJosh Poimboeuf }' "$KLP_DIFF_LOG" | mapfile -t lines 68378be9facSJosh Poimboeuf 68478be9facSJosh Poimboeuf for line in "${lines[@]}"; do 68578be9facSJosh Poimboeuf read -r file func <<< "$line" 68678be9facSJosh Poimboeuf if [[ ! -v funcs["$file"] ]]; then 68778be9facSJosh Poimboeuf funcs["$file"]="$func" 68878be9facSJosh Poimboeuf else 68978be9facSJosh Poimboeuf funcs["$file"]+=" $func" 69078be9facSJosh Poimboeuf fi 69178be9facSJosh Poimboeuf done 69278be9facSJosh Poimboeuf 69378be9facSJosh Poimboeuf cmd=("$SRC/tools/objtool/objtool") 69478be9facSJosh Poimboeuf cmd+=("--checksum") 69578be9facSJosh Poimboeuf cmd+=("--link") 69678be9facSJosh Poimboeuf cmd+=("--dry-run") 69778be9facSJosh Poimboeuf 69878be9facSJosh Poimboeuf for file in "${!funcs[@]}"; do 69978be9facSJosh Poimboeuf local opt="--debug-checksum=${funcs[$file]// /,}" 70078be9facSJosh Poimboeuf 70178be9facSJosh Poimboeuf ( 70278be9facSJosh Poimboeuf cd "$ORIG_DIR" 70378be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$orig_log" || \ 70478be9facSJosh Poimboeuf ( cat "$orig_log" >&2; die "objtool --debug-checksum failed" ) 70578be9facSJosh Poimboeuf 70678be9facSJosh Poimboeuf cd "$PATCHED_DIR" 70778be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$patched_log" || \ 70878be9facSJosh Poimboeuf ( cat "$patched_log" >&2; die "objtool --debug-checksum failed" ) 70978be9facSJosh Poimboeuf ) 71078be9facSJosh Poimboeuf 71178be9facSJosh Poimboeuf for func in ${funcs[$file]}; do 71278be9facSJosh Poimboeuf diff <( grep0 -E "^DEBUG: .*checksum: $func " "$orig_log" | sed "s|$ORIG_DIR/||") \ 71378be9facSJosh Poimboeuf <( grep0 -E "^DEBUG: .*checksum: $func " "$patched_log" | sed "s|$PATCHED_DIR/||") \ 71478be9facSJosh Poimboeuf | gawk '/^< DEBUG: / { 71578be9facSJosh Poimboeuf gsub(/:/, "") 71678be9facSJosh Poimboeuf printf "%s: %s: %s\n", $3, $5, $6 71778be9facSJosh Poimboeuf exit 71878be9facSJosh Poimboeuf }' || true 71978be9facSJosh Poimboeuf done 72078be9facSJosh Poimboeuf done 72178be9facSJosh Poimboeuf} 72278be9facSJosh Poimboeuf 72324ebfcd6SJosh Poimboeuf# Build and post-process livepatch module in $KMOD_DIR 72424ebfcd6SJosh Poimboeufbuild_patch_module() { 72524ebfcd6SJosh Poimboeuf local makefile="$KMOD_DIR/Kbuild" 72624ebfcd6SJosh Poimboeuf local log="$KMOD_DIR/build.log" 72724ebfcd6SJosh Poimboeuf local kmod_file 72824ebfcd6SJosh Poimboeuf local cflags=() 72924ebfcd6SJosh Poimboeuf local files=() 73024ebfcd6SJosh Poimboeuf local cmd=() 73124ebfcd6SJosh Poimboeuf 73224ebfcd6SJosh Poimboeuf rm -rf "$KMOD_DIR" 73324ebfcd6SJosh Poimboeuf mkdir -p "$KMOD_DIR" 73424ebfcd6SJosh Poimboeuf 73524ebfcd6SJosh Poimboeuf cp -f "$SRC/scripts/livepatch/init.c" "$KMOD_DIR" 73624ebfcd6SJosh Poimboeuf 73724ebfcd6SJosh Poimboeuf echo "obj-m := $NAME.o" > "$makefile" 73824ebfcd6SJosh Poimboeuf echo -n "$NAME-y := init.o" >> "$makefile" 73924ebfcd6SJosh Poimboeuf 74024ebfcd6SJosh Poimboeuf find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files 74124ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 74224ebfcd6SJosh Poimboeuf 74324ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 74424ebfcd6SJosh Poimboeuf local rel_file="${file#"$DIFF_DIR"/}" 74524ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 74624ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 74724ebfcd6SJosh Poimboeuf local kmod_file="$KMOD_DIR/$rel_file" 74824ebfcd6SJosh Poimboeuf local kmod_dir="$(dirname "$kmod_file")" 74978c268f3SJosh Poimboeuf local cmd_file="$kmod_dir/.$(basename "$file").cmd" 75024ebfcd6SJosh Poimboeuf 75124ebfcd6SJosh Poimboeuf mkdir -p "$kmod_dir" 75224ebfcd6SJosh Poimboeuf cp -f "$file" "$kmod_dir" 75324ebfcd6SJosh Poimboeuf 75424ebfcd6SJosh Poimboeuf # Tell kbuild this is a prebuilt object 75524ebfcd6SJosh Poimboeuf cp -f "$file" "${kmod_file}_shipped" 75624ebfcd6SJosh Poimboeuf 75778c268f3SJosh Poimboeuf # Make modpost happy 75878c268f3SJosh Poimboeuf touch "$cmd_file" 75978c268f3SJosh Poimboeuf 76024ebfcd6SJosh Poimboeuf echo -n " $rel_file" >> "$makefile" 76124ebfcd6SJosh Poimboeuf done 76224ebfcd6SJosh Poimboeuf 76324ebfcd6SJosh Poimboeuf echo >> "$makefile" 76424ebfcd6SJosh Poimboeuf 76524ebfcd6SJosh Poimboeuf cflags=("-ffunction-sections") 76624ebfcd6SJosh Poimboeuf cflags+=("-fdata-sections") 76724ebfcd6SJosh Poimboeuf [[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE") 76824ebfcd6SJosh Poimboeuf 76924ebfcd6SJosh Poimboeuf cmd=("make") 77024ebfcd6SJosh Poimboeuf cmd+=("$VERBOSE") 77124ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 77224ebfcd6SJosh Poimboeuf cmd+=("--directory=.") 77324ebfcd6SJosh Poimboeuf cmd+=("M=$KMOD_DIR") 77424ebfcd6SJosh Poimboeuf cmd+=("KCFLAGS=${cflags[*]}") 77524ebfcd6SJosh Poimboeuf 77624ebfcd6SJosh Poimboeuf # Build a "normal" kernel module with init.c and the diffed objects 77724ebfcd6SJosh Poimboeuf ( 77824ebfcd6SJosh Poimboeuf cd "$SRC" 77924ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 78024ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 78124ebfcd6SJosh Poimboeuf 2> >(tee -a "$log" >&2) 78224ebfcd6SJosh Poimboeuf ) 78324ebfcd6SJosh Poimboeuf 78424ebfcd6SJosh Poimboeuf kmod_file="$KMOD_DIR/$NAME.ko" 78524ebfcd6SJosh Poimboeuf 78624ebfcd6SJosh Poimboeuf # Save off the intermediate binary for debugging 78724ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$kmod_file.orig" 78824ebfcd6SJosh Poimboeuf 78924ebfcd6SJosh Poimboeuf # Work around issue where slight .config change makes corrupt BTF 79024ebfcd6SJosh Poimboeuf objcopy --remove-section=.BTF "$kmod_file" 79124ebfcd6SJosh Poimboeuf 79224ebfcd6SJosh Poimboeuf # Fix (and work around) linker wreckage for klp syms / relocs 79324ebfcd6SJosh Poimboeuf "$SRC/tools/objtool/objtool" klp post-link "$kmod_file" || die "objtool klp post-link failed" 79424ebfcd6SJosh Poimboeuf 79524ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$OUTFILE" 79624ebfcd6SJosh Poimboeuf} 79724ebfcd6SJosh Poimboeuf 79824ebfcd6SJosh Poimboeuf 79924ebfcd6SJosh Poimboeuf################################################################################ 80024ebfcd6SJosh Poimboeuf 80124ebfcd6SJosh Poimboeufprocess_args "$@" 80224ebfcd6SJosh Poimboeufdo_init 80324ebfcd6SJosh Poimboeuf 804e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 2 )); then 80524ebfcd6SJosh Poimboeuf status "Validating patch(es)" 80624ebfcd6SJosh Poimboeuf validate_patches 807e506ad21SJoe Lawrencefi 808e506ad21SJoe Lawrence 809e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 1 )); then 81024ebfcd6SJosh Poimboeuf status "Building original kernel" 81124ebfcd6SJosh Poimboeuf clean_kernel 812b41d8b7dSJoe Lawrence build_kernel "original" 81324ebfcd6SJosh Poimboeuf status "Copying original object files" 81424ebfcd6SJosh Poimboeuf copy_orig_objects 81524ebfcd6SJosh Poimboeuffi 81624ebfcd6SJosh Poimboeuf 81724ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 2 )); then 81824ebfcd6SJosh Poimboeuf status "Fixing patch(es)" 81924ebfcd6SJosh Poimboeuf fix_patches 82024ebfcd6SJosh Poimboeuf apply_patches 82124ebfcd6SJosh Poimboeuf status "Building patched kernel" 822b41d8b7dSJoe Lawrence build_kernel "patched" 82324ebfcd6SJosh Poimboeuf revert_patches 82424ebfcd6SJosh Poimboeuf status "Copying patched object files" 82524ebfcd6SJosh Poimboeuf copy_patched_objects 82624ebfcd6SJosh Poimboeuffi 82724ebfcd6SJosh Poimboeuf 82824ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 3 )); then 82924ebfcd6SJosh Poimboeuf status "Diffing objects" 83024ebfcd6SJosh Poimboeuf diff_objects 83178be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 83278be9facSJosh Poimboeuf status "Finding first changed instructions" 83378be9facSJosh Poimboeuf diff_checksums 83478be9facSJosh Poimboeuf fi 83524ebfcd6SJosh Poimboeuffi 83624ebfcd6SJosh Poimboeuf 83724ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 4 )); then 83824ebfcd6SJosh Poimboeuf status "Building patch module: $OUTFILE" 83924ebfcd6SJosh Poimboeuf build_patch_module 84024ebfcd6SJosh Poimboeuffi 84124ebfcd6SJosh Poimboeuf 84224ebfcd6SJosh Poimboeufstatus "SUCCESS" 843