124ebfcd6SJosh Poimboeuf#!/bin/bash 224ebfcd6SJosh Poimboeuf# SPDX-License-Identifier: GPL-2.0 324ebfcd6SJosh Poimboeuf# 424ebfcd6SJosh Poimboeuf# Build a livepatch module 524ebfcd6SJosh Poimboeuf 6946d3510SJosh Poimboeuf# shellcheck disable=SC1090,SC2155,SC2164 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 errtrace 1524ebfcd6SJosh Poimboeufset -o pipefail 1624ebfcd6SJosh Poimboeufset -o nounset 1724ebfcd6SJosh Poimboeuf 1824ebfcd6SJosh Poimboeuf# Allow doing 'cmd | mapfile -t array' instead of 'mapfile -t array < <(cmd)'. 19946d3510SJosh Poimboeuf# This helps keep execution in pipes so pipefail+ERR trap can catch errors. 2024ebfcd6SJosh Poimboeufshopt -s lastpipe 2124ebfcd6SJosh Poimboeuf 2278be9facSJosh Poimboeufunset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP XTRACE 2324ebfcd6SJosh Poimboeuf 2424ebfcd6SJosh PoimboeufREPLACE=1 2524ebfcd6SJosh PoimboeufSHORT_CIRCUIT=0 2624ebfcd6SJosh PoimboeufJOBS="$(getconf _NPROCESSORS_ONLN)" 2724ebfcd6SJosh PoimboeufVERBOSE="-s" 2824ebfcd6SJosh Poimboeufshopt -o xtrace | grep -q 'on' && XTRACE=1 2924ebfcd6SJosh Poimboeuf 3024ebfcd6SJosh Poimboeuf# Avoid removing the previous $TMP_DIR until args have been fully processed. 3124ebfcd6SJosh PoimboeufKEEP_TMP=1 3224ebfcd6SJosh Poimboeuf 3324ebfcd6SJosh PoimboeufSCRIPT="$(basename "$0")" 3424ebfcd6SJosh PoimboeufSCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 3524ebfcd6SJosh PoimboeufFIX_PATCH_LINES="$SCRIPT_DIR/fix-patch-lines" 3624ebfcd6SJosh Poimboeuf 3724ebfcd6SJosh PoimboeufSRC="$(pwd)" 3824ebfcd6SJosh PoimboeufOBJ="$(pwd)" 3924ebfcd6SJosh Poimboeuf 4024ebfcd6SJosh PoimboeufCONFIG="$OBJ/.config" 4124ebfcd6SJosh PoimboeufTMP_DIR="$OBJ/klp-tmp" 4224ebfcd6SJosh Poimboeuf 4324ebfcd6SJosh PoimboeufORIG_DIR="$TMP_DIR/orig" 4424ebfcd6SJosh PoimboeufPATCHED_DIR="$TMP_DIR/patched" 4524ebfcd6SJosh PoimboeufDIFF_DIR="$TMP_DIR/diff" 4624ebfcd6SJosh PoimboeufKMOD_DIR="$TMP_DIR/kmod" 4724ebfcd6SJosh Poimboeuf 4824ebfcd6SJosh PoimboeufSTASH_DIR="$TMP_DIR/stash" 4924ebfcd6SJosh PoimboeufTIMESTAMP="$TMP_DIR/timestamp" 5024ebfcd6SJosh PoimboeufPATCH_TMP_DIR="$TMP_DIR/tmp" 5124ebfcd6SJosh Poimboeuf 5224ebfcd6SJosh PoimboeufKLP_DIFF_LOG="$DIFF_DIR/diff.log" 5324ebfcd6SJosh Poimboeuf 541fbc9b85SJoe Lawrence# Terminal output colors 551fbc9b85SJoe Lawrenceread -r COLOR_RESET COLOR_BOLD COLOR_ERROR COLOR_WARN <<< "" 561fbc9b85SJoe Lawrenceif [[ -t 1 && -t 2 ]]; then 571fbc9b85SJoe Lawrence COLOR_RESET="\033[0m" 581fbc9b85SJoe Lawrence COLOR_BOLD="\033[1m" 591fbc9b85SJoe Lawrence COLOR_ERROR="\033[0;31m" 601fbc9b85SJoe Lawrence COLOR_WARN="\033[0;33m" 611fbc9b85SJoe Lawrencefi 621fbc9b85SJoe Lawrence 6324ebfcd6SJosh Poimboeufgrep0() { 64b4a53519SJoe Lawrence # shellcheck disable=SC2317 6524ebfcd6SJosh Poimboeuf command grep "$@" || true 6624ebfcd6SJosh Poimboeuf} 6724ebfcd6SJosh Poimboeuf 68e4dbf706SJoe Lawrence# Because pipefail is enabled, the grep0 helper should be used instead of 69e4dbf706SJoe Lawrence# grep, otherwise a failed match can propagate to an error. 70e4dbf706SJoe Lawrencegrep() { 71e4dbf706SJoe Lawrence echo "error: $SCRIPT: use grep0 or 'command grep' instead of bare grep" >&2 72e4dbf706SJoe Lawrence exit 1 73e4dbf706SJoe Lawrence} 74e4dbf706SJoe Lawrence 7524ebfcd6SJosh Poimboeufstatus() { 761fbc9b85SJoe Lawrence echo -e "${COLOR_BOLD}$*${COLOR_RESET}" 7724ebfcd6SJosh Poimboeuf} 7824ebfcd6SJosh Poimboeuf 7924ebfcd6SJosh Poimboeufwarn() { 801fbc9b85SJoe Lawrence echo -e "${COLOR_WARN}warning${COLOR_RESET}: $SCRIPT: $*" >&2 8124ebfcd6SJosh Poimboeuf} 8224ebfcd6SJosh Poimboeuf 8324ebfcd6SJosh Poimboeufdie() { 841fbc9b85SJoe Lawrence echo -e "${COLOR_ERROR}error${COLOR_RESET}: $SCRIPT: $*" >&2 8524ebfcd6SJosh Poimboeuf exit 1 8624ebfcd6SJosh Poimboeuf} 8724ebfcd6SJosh Poimboeuf 8824ebfcd6SJosh Poimboeufdeclare -a STASHED_FILES 8924ebfcd6SJosh Poimboeuf 9024ebfcd6SJosh Poimboeufstash_file() { 9124ebfcd6SJosh Poimboeuf local file="$1" 9224ebfcd6SJosh Poimboeuf local rel_file="${file#"$SRC"/}" 9324ebfcd6SJosh Poimboeuf 9424ebfcd6SJosh Poimboeuf [[ ! -e "$file" ]] && die "no file to stash: $file" 9524ebfcd6SJosh Poimboeuf 9624ebfcd6SJosh Poimboeuf mkdir -p "$STASH_DIR/$(dirname "$rel_file")" 9724ebfcd6SJosh Poimboeuf cp -f "$file" "$STASH_DIR/$rel_file" 9824ebfcd6SJosh Poimboeuf 9924ebfcd6SJosh Poimboeuf STASHED_FILES+=("$rel_file") 10024ebfcd6SJosh Poimboeuf} 10124ebfcd6SJosh Poimboeuf 10224ebfcd6SJosh Poimboeufrestore_files() { 10324ebfcd6SJosh Poimboeuf local file 10424ebfcd6SJosh Poimboeuf 10524ebfcd6SJosh Poimboeuf for file in "${STASHED_FILES[@]}"; do 10624ebfcd6SJosh Poimboeuf mv -f "$STASH_DIR/$file" "$SRC/$file" || warn "can't restore file: $file" 10724ebfcd6SJosh Poimboeuf done 10824ebfcd6SJosh Poimboeuf 10924ebfcd6SJosh Poimboeuf STASHED_FILES=() 11024ebfcd6SJosh Poimboeuf} 11124ebfcd6SJosh Poimboeuf 11224ebfcd6SJosh Poimboeufcleanup() { 11324ebfcd6SJosh Poimboeuf set +o nounset 114d36a7343SJoe Lawrence revert_patches 11524ebfcd6SJosh Poimboeuf restore_files 11624ebfcd6SJosh Poimboeuf [[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR" 11724ebfcd6SJosh Poimboeuf return 0 11824ebfcd6SJosh Poimboeuf} 11924ebfcd6SJosh Poimboeuf 12024ebfcd6SJosh Poimboeuftrap_err() { 1211fbc9b85SJoe Lawrence die "line ${BASH_LINENO[0]}: '$BASH_COMMAND'" 12224ebfcd6SJosh Poimboeuf} 12324ebfcd6SJosh Poimboeuf 12424ebfcd6SJosh Poimboeuftrap cleanup EXIT INT TERM HUP 12524ebfcd6SJosh Poimboeuftrap trap_err ERR 12624ebfcd6SJosh Poimboeuf 12724ebfcd6SJosh Poimboeuf__usage() { 12824ebfcd6SJosh Poimboeuf cat <<EOF 12924ebfcd6SJosh PoimboeufUsage: $SCRIPT [OPTIONS] PATCH_FILE(s) 13024ebfcd6SJosh PoimboeufGenerate a livepatch module. 13124ebfcd6SJosh Poimboeuf 13224ebfcd6SJosh PoimboeufOptions: 13378be9facSJosh Poimboeuf -f, --show-first-changed Show address of first changed instruction 13424ebfcd6SJosh Poimboeuf -j, --jobs=<jobs> Build jobs to run simultaneously [default: $JOBS] 13524ebfcd6SJosh Poimboeuf -o, --output=<file.ko> Output file [default: livepatch-<patch-name>.ko] 13624ebfcd6SJosh Poimboeuf --no-replace Disable livepatch atomic replace 13724ebfcd6SJosh Poimboeuf -v, --verbose Pass V=1 to kernel/module builds 13824ebfcd6SJosh Poimboeuf 13924ebfcd6SJosh PoimboeufAdvanced Options: 1402c2f0b86SJosh Poimboeuf -d, --debug Show symbol/reloc cloning decisions 14124ebfcd6SJosh Poimboeuf -S, --short-circuit=STEP Start at build step (requires prior --keep-tmp) 14224ebfcd6SJosh Poimboeuf 1|orig Build original kernel (default) 14324ebfcd6SJosh Poimboeuf 2|patched Build patched kernel 14424ebfcd6SJosh Poimboeuf 3|diff Diff objects 14524ebfcd6SJosh Poimboeuf 4|kmod Build patch module 14624ebfcd6SJosh Poimboeuf -T, --keep-tmp Preserve tmp dir on exit 14724ebfcd6SJosh Poimboeuf 14824ebfcd6SJosh PoimboeufEOF 14924ebfcd6SJosh Poimboeuf} 15024ebfcd6SJosh Poimboeuf 15124ebfcd6SJosh Poimboeufusage() { 15224ebfcd6SJosh Poimboeuf __usage >&2 15324ebfcd6SJosh Poimboeuf} 15424ebfcd6SJosh Poimboeuf 15524ebfcd6SJosh Poimboeufprocess_args() { 15624ebfcd6SJosh Poimboeuf local keep_tmp=0 15724ebfcd6SJosh Poimboeuf local short 15824ebfcd6SJosh Poimboeuf local long 15924ebfcd6SJosh Poimboeuf local args 160*b3ece301SJosh Poimboeuf local patch 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=("$@") 239*b3ece301SJosh Poimboeuf 240*b3ece301SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 241*b3ece301SJosh Poimboeuf [[ -f "$patch" ]] || die "$patch doesn't exist" 242*b3ece301SJosh Poimboeuf done 24324ebfcd6SJosh Poimboeuf} 24424ebfcd6SJosh Poimboeuf 24524ebfcd6SJosh Poimboeuf# temporarily disable xtrace for especially verbose code 24624ebfcd6SJosh Poimboeufxtrace_save() { 24724ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set +x 24824ebfcd6SJosh Poimboeuf return 0 24924ebfcd6SJosh Poimboeuf} 25024ebfcd6SJosh Poimboeuf 25124ebfcd6SJosh Poimboeufxtrace_restore() { 25224ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set -x 25324ebfcd6SJosh Poimboeuf return 0 25424ebfcd6SJosh Poimboeuf} 25524ebfcd6SJosh Poimboeuf 25624ebfcd6SJosh Poimboeufvalidate_config() { 25724ebfcd6SJosh Poimboeuf xtrace_save "reading .config" 25824ebfcd6SJosh Poimboeuf source "$CONFIG" || die "no .config file in $(dirname "$CONFIG")" 25924ebfcd6SJosh Poimboeuf xtrace_restore 26024ebfcd6SJosh Poimboeuf 26124ebfcd6SJosh Poimboeuf [[ -v CONFIG_LIVEPATCH ]] || \ 26224ebfcd6SJosh Poimboeuf die "CONFIG_LIVEPATCH not enabled" 26324ebfcd6SJosh Poimboeuf 26424ebfcd6SJosh Poimboeuf [[ -v CONFIG_KLP_BUILD ]] || \ 26524ebfcd6SJosh Poimboeuf die "CONFIG_KLP_BUILD not enabled" 26624ebfcd6SJosh Poimboeuf 26724ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_LATENT_ENTROPY ]] && \ 26824ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_LATENT_ENTROPY' not supported" 26924ebfcd6SJosh Poimboeuf 27024ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_RANDSTRUCT ]] && \ 27124ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_RANDSTRUCT' not supported" 27224ebfcd6SJosh Poimboeuf 273a8ff29f0SJosh Poimboeuf [[ -v CONFIG_AS_IS_LLVM ]] && \ 274a8ff29f0SJosh Poimboeuf [[ "$CONFIG_AS_VERSION" -lt 200000 ]] && \ 275a8ff29f0SJosh Poimboeuf die "Clang assembler version < 20 not supported" 276a8ff29f0SJosh Poimboeuf 27724ebfcd6SJosh Poimboeuf return 0 27824ebfcd6SJosh Poimboeuf} 27924ebfcd6SJosh Poimboeuf 28024ebfcd6SJosh Poimboeuf# Only allow alphanumerics and '_' and '-' in the module name. Everything else 28124ebfcd6SJosh Poimboeuf# is replaced with '-'. Also truncate to 55 chars so the full name + NUL 28224ebfcd6SJosh Poimboeuf# terminator fits in the kernel's 56-byte module name array. 28324ebfcd6SJosh Poimboeufmodule_name_string() { 28424ebfcd6SJosh Poimboeuf echo "${1//[^a-zA-Z0-9_-]/-}" | cut -c 1-55 28524ebfcd6SJosh Poimboeuf} 28624ebfcd6SJosh Poimboeuf 28724ebfcd6SJosh Poimboeuf# If the module name wasn't specified on the cmdline with --output, give it a 28824ebfcd6SJosh Poimboeuf# name based on the patch name. 28924ebfcd6SJosh Poimboeufset_module_name() { 29024ebfcd6SJosh Poimboeuf [[ -v NAME ]] && return 0 29124ebfcd6SJosh Poimboeuf 29224ebfcd6SJosh Poimboeuf if [[ "${#PATCHES[@]}" -eq 1 ]]; then 29324ebfcd6SJosh Poimboeuf NAME="$(basename "${PATCHES[0]}")" 29424ebfcd6SJosh Poimboeuf NAME="${NAME%.*}" 29524ebfcd6SJosh Poimboeuf else 29624ebfcd6SJosh Poimboeuf NAME="patch" 29724ebfcd6SJosh Poimboeuf fi 29824ebfcd6SJosh Poimboeuf 29924ebfcd6SJosh Poimboeuf NAME="livepatch-$NAME" 30024ebfcd6SJosh Poimboeuf NAME="$(module_name_string "$NAME")" 30124ebfcd6SJosh Poimboeuf 30224ebfcd6SJosh Poimboeuf OUTFILE="$NAME.ko" 30324ebfcd6SJosh Poimboeuf} 30424ebfcd6SJosh Poimboeuf 30524ebfcd6SJosh Poimboeuf# Hardcode the value printed by the localversion script to prevent patch 306d36a7343SJoe Lawrence# application from appending it with '+' due to a dirty working tree. 30724ebfcd6SJosh Poimboeufset_kernelversion() { 30824ebfcd6SJosh Poimboeuf local file="$SRC/scripts/setlocalversion" 3096f93f7b0SJosh Poimboeuf local kernelrelease 31024ebfcd6SJosh Poimboeuf 31124ebfcd6SJosh Poimboeuf stash_file "$file" 31224ebfcd6SJosh Poimboeuf 313cc39ccceSJosh Poimboeuf if [[ -n "$(make -s listnewconfig 2>/dev/null)" ]]; then 314cc39ccceSJosh Poimboeuf die ".config mismatch, check your .config or run 'make olddefconfig'" 315cc39ccceSJosh Poimboeuf fi 316cc39ccceSJosh Poimboeuf make syncconfig &>/dev/null || die "make syncconfig failed" 317cc39ccceSJosh Poimboeuf 318cc39ccceSJosh Poimboeuf kernelrelease="$(make -s kernelrelease)" 3196f93f7b0SJosh Poimboeuf [[ -z "$kernelrelease" ]] && die "failed to get kernel version" 32024ebfcd6SJosh Poimboeuf 3216f93f7b0SJosh Poimboeuf sed -i "2i echo $kernelrelease; exit 0" scripts/setlocalversion 32224ebfcd6SJosh Poimboeuf} 32324ebfcd6SJosh Poimboeuf 324757bd10fSJoe Lawrenceget_patch_input_files() { 32524ebfcd6SJosh Poimboeuf local patch="$1" 32624ebfcd6SJosh Poimboeuf 327757bd10fSJoe Lawrence grep0 -E '^--- ' "$patch" \ 328d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 32924ebfcd6SJosh Poimboeuf | gawk '{print $2}' \ 33024ebfcd6SJosh Poimboeuf | sed 's|^[^/]*/||' \ 33124ebfcd6SJosh Poimboeuf | sort -u 33224ebfcd6SJosh Poimboeuf} 33324ebfcd6SJosh Poimboeuf 334757bd10fSJoe Lawrenceget_patch_output_files() { 33524ebfcd6SJosh Poimboeuf local patch="$1" 33624ebfcd6SJosh Poimboeuf 337757bd10fSJoe Lawrence grep0 -E '^\+\+\+ ' "$patch" \ 338d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 339757bd10fSJoe Lawrence | gawk '{print $2}' \ 340757bd10fSJoe Lawrence | sed 's|^[^/]*/||' \ 341757bd10fSJoe Lawrence | sort -u 342757bd10fSJoe Lawrence} 34324ebfcd6SJosh Poimboeuf 34424ebfcd6SJosh Poimboeufget_patch_files() { 34524ebfcd6SJosh Poimboeuf local patch="$1" 34624ebfcd6SJosh Poimboeuf 347757bd10fSJoe Lawrence { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \ 34824ebfcd6SJosh Poimboeuf | sort -u 34924ebfcd6SJosh Poimboeuf} 35024ebfcd6SJosh Poimboeuf 35124ebfcd6SJosh Poimboeufcheck_unsupported_patches() { 35224ebfcd6SJosh Poimboeuf local patch 35324ebfcd6SJosh Poimboeuf 35424ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 35524ebfcd6SJosh Poimboeuf local files=() 35624ebfcd6SJosh Poimboeuf 35724ebfcd6SJosh Poimboeuf get_patch_files "$patch" | mapfile -t files 35824ebfcd6SJosh Poimboeuf 35924ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 36024ebfcd6SJosh Poimboeuf case "$file" in 36124ebfcd6SJosh Poimboeuf lib/*|*.S) 362b41d8b7dSJoe Lawrence die "${patch}: unsupported patch to $file" 36324ebfcd6SJosh Poimboeuf ;; 36424ebfcd6SJosh Poimboeuf esac 36524ebfcd6SJosh Poimboeuf done 36624ebfcd6SJosh Poimboeuf done 36724ebfcd6SJosh Poimboeuf} 36824ebfcd6SJosh Poimboeuf 36924ebfcd6SJosh Poimboeufapply_patch() { 37024ebfcd6SJosh Poimboeuf local patch="$1" 37124ebfcd6SJosh Poimboeuf shift 37224ebfcd6SJosh Poimboeuf local extra_args=("$@") 37351a0b7c4SJoe Lawrence local drift_regex="with fuzz|offset [0-9]+ line" 37451a0b7c4SJoe Lawrence local output 37551a0b7c4SJoe Lawrence local status 37624ebfcd6SJosh Poimboeuf 37724ebfcd6SJosh Poimboeuf [[ ! -f "$patch" ]] && die "$patch doesn't exist" 37851a0b7c4SJoe Lawrence status=0 37951a0b7c4SJoe Lawrence output=$(patch -d "$SRC" -p1 --dry-run --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" < "$patch" 2>&1) || status=$? 38051a0b7c4SJoe Lawrence if [[ "$status" -ne 0 ]]; then 38151a0b7c4SJoe Lawrence echo "$output" >&2 38251a0b7c4SJoe Lawrence die "$patch did not apply" 38351a0b7c4SJoe Lawrence elif [[ "$output" =~ $drift_regex ]]; then 38451a0b7c4SJoe Lawrence echo "$output" >&2 38551a0b7c4SJoe Lawrence warn "${patch} applied with fuzz" 38651a0b7c4SJoe Lawrence fi 38724ebfcd6SJosh Poimboeuf 38851a0b7c4SJoe Lawrence patch -d "$SRC" -p1 --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" --silent < "$patch" 38924ebfcd6SJosh Poimboeuf APPLIED_PATCHES+=("$patch") 39024ebfcd6SJosh Poimboeuf} 39124ebfcd6SJosh Poimboeuf 39224ebfcd6SJosh Poimboeufrevert_patch() { 39324ebfcd6SJosh Poimboeuf local patch="$1" 39424ebfcd6SJosh Poimboeuf local tmp=() 39524ebfcd6SJosh Poimboeuf 396d36a7343SJoe Lawrence patch -d "$SRC" -p1 -R --silent --no-backup-if-mismatch -r /dev/null < "$patch" 39724ebfcd6SJosh Poimboeuf 39824ebfcd6SJosh Poimboeuf for p in "${APPLIED_PATCHES[@]}"; do 39924ebfcd6SJosh Poimboeuf [[ "$p" == "$patch" ]] && continue 40024ebfcd6SJosh Poimboeuf tmp+=("$p") 40124ebfcd6SJosh Poimboeuf done 40224ebfcd6SJosh Poimboeuf 40324ebfcd6SJosh Poimboeuf APPLIED_PATCHES=("${tmp[@]}") 40424ebfcd6SJosh Poimboeuf} 40524ebfcd6SJosh Poimboeuf 40624ebfcd6SJosh Poimboeufapply_patches() { 40751a0b7c4SJoe Lawrence local extra_args=("$@") 40824ebfcd6SJosh Poimboeuf local patch 40924ebfcd6SJosh Poimboeuf 41024ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 41151a0b7c4SJoe Lawrence apply_patch "$patch" "${extra_args[@]}" 41224ebfcd6SJosh Poimboeuf done 41324ebfcd6SJosh Poimboeuf} 41424ebfcd6SJosh Poimboeuf 41524ebfcd6SJosh Poimboeufrevert_patches() { 41624ebfcd6SJosh Poimboeuf local patches=("${APPLIED_PATCHES[@]}") 41724ebfcd6SJosh Poimboeuf 41824ebfcd6SJosh Poimboeuf for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do 419d36a7343SJoe Lawrence revert_patch "${patches[$i]}" 42024ebfcd6SJosh Poimboeuf done 42124ebfcd6SJosh Poimboeuf 42224ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 42324ebfcd6SJosh Poimboeuf} 42424ebfcd6SJosh Poimboeuf 42524ebfcd6SJosh Poimboeufvalidate_patches() { 42624ebfcd6SJosh Poimboeuf check_unsupported_patches 42724ebfcd6SJosh Poimboeuf apply_patches 42824ebfcd6SJosh Poimboeuf revert_patches 42924ebfcd6SJosh Poimboeuf} 43024ebfcd6SJosh Poimboeuf 43124ebfcd6SJosh Poimboeufdo_init() { 43224ebfcd6SJosh Poimboeuf # We're not yet smart enough to handle anything other than in-tree 43324ebfcd6SJosh Poimboeuf # builds in pwd. 43424ebfcd6SJosh Poimboeuf [[ ! "$SRC" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory" 43524ebfcd6SJosh Poimboeuf [[ ! "$OBJ" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory" 43624ebfcd6SJosh Poimboeuf 43724ebfcd6SJosh Poimboeuf (( SHORT_CIRCUIT <= 1 )) && rm -rf "$TMP_DIR" 43824ebfcd6SJosh Poimboeuf mkdir -p "$TMP_DIR" 43924ebfcd6SJosh Poimboeuf 44024ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 44124ebfcd6SJosh Poimboeuf 44224ebfcd6SJosh Poimboeuf [[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines" 443d36a7343SJoe Lawrence command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)" 44424ebfcd6SJosh Poimboeuf 44524ebfcd6SJosh Poimboeuf validate_config 44624ebfcd6SJosh Poimboeuf set_module_name 44724ebfcd6SJosh Poimboeuf set_kernelversion 44824ebfcd6SJosh Poimboeuf} 44924ebfcd6SJosh Poimboeuf 45024ebfcd6SJosh Poimboeuf# Refresh the patch hunk headers, specifically the line numbers and counts. 45124ebfcd6SJosh Poimboeufrefresh_patch() { 45224ebfcd6SJosh Poimboeuf local patch="$1" 45324ebfcd6SJosh Poimboeuf local tmpdir="$PATCH_TMP_DIR" 454757bd10fSJoe Lawrence local input_files=() 455757bd10fSJoe Lawrence local output_files=() 45624ebfcd6SJosh Poimboeuf 45724ebfcd6SJosh Poimboeuf rm -rf "$tmpdir" 45824ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/a" 45924ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/b" 46024ebfcd6SJosh Poimboeuf 46124ebfcd6SJosh Poimboeuf # Get all source files affected by the patch 462757bd10fSJoe Lawrence get_patch_input_files "$patch" | mapfile -t input_files 463757bd10fSJoe Lawrence get_patch_output_files "$patch" | mapfile -t output_files 46424ebfcd6SJosh Poimboeuf 46524ebfcd6SJosh Poimboeuf # Copy orig source files to 'a' 466757bd10fSJoe Lawrence ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) 46724ebfcd6SJosh Poimboeuf 46824ebfcd6SJosh Poimboeuf # Copy patched source files to 'b' 46951a0b7c4SJoe Lawrence apply_patch "$patch" "--silent" 470757bd10fSJoe Lawrence ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) 471d36a7343SJoe Lawrence revert_patch "$patch" 47224ebfcd6SJosh Poimboeuf 47324ebfcd6SJosh Poimboeuf # Diff 'a' and 'b' to make a clean patch 474d36a7343SJoe Lawrence ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true 47524ebfcd6SJosh Poimboeuf} 47624ebfcd6SJosh Poimboeuf 47724ebfcd6SJosh Poimboeuf# Copy the patches to a temporary directory, fix their lines so as not to 47824ebfcd6SJosh Poimboeuf# affect the __LINE__ macro for otherwise unchanged functions further down the 47924ebfcd6SJosh Poimboeuf# file, and update $PATCHES to point to the fixed patches. 48024ebfcd6SJosh Poimboeuffix_patches() { 48124ebfcd6SJosh Poimboeuf local idx 48224ebfcd6SJosh Poimboeuf local i 48324ebfcd6SJosh Poimboeuf 48424ebfcd6SJosh Poimboeuf rm -f "$TMP_DIR"/*.patch 48524ebfcd6SJosh Poimboeuf 48624ebfcd6SJosh Poimboeuf idx=0001 48724ebfcd6SJosh Poimboeuf for i in "${!PATCHES[@]}"; do 48824ebfcd6SJosh Poimboeuf local old_patch="${PATCHES[$i]}" 48924ebfcd6SJosh Poimboeuf local tmp_patch="$TMP_DIR/tmp.patch" 49024ebfcd6SJosh Poimboeuf local patch="${PATCHES[$i]}" 49124ebfcd6SJosh Poimboeuf local new_patch 49224ebfcd6SJosh Poimboeuf 49324ebfcd6SJosh Poimboeuf new_patch="$TMP_DIR/$idx-fixed-$(basename "$patch")" 49424ebfcd6SJosh Poimboeuf 49524ebfcd6SJosh Poimboeuf cp -f "$old_patch" "$tmp_patch" 49624ebfcd6SJosh Poimboeuf refresh_patch "$tmp_patch" 497d36a7343SJoe Lawrence "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" 49824ebfcd6SJosh Poimboeuf 49924ebfcd6SJosh Poimboeuf PATCHES[i]="$new_patch" 50024ebfcd6SJosh Poimboeuf 50124ebfcd6SJosh Poimboeuf rm -f "$tmp_patch" 50224ebfcd6SJosh Poimboeuf idx=$(printf "%04d" $(( 10#$idx + 1 ))) 50324ebfcd6SJosh Poimboeuf done 50424ebfcd6SJosh Poimboeuf} 50524ebfcd6SJosh Poimboeuf 50624ebfcd6SJosh Poimboeufclean_kernel() { 50724ebfcd6SJosh Poimboeuf local cmd=() 50824ebfcd6SJosh Poimboeuf 50924ebfcd6SJosh Poimboeuf cmd=("make") 51024ebfcd6SJosh Poimboeuf cmd+=("--silent") 51124ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 51224ebfcd6SJosh Poimboeuf cmd+=("clean") 51324ebfcd6SJosh Poimboeuf 51424ebfcd6SJosh Poimboeuf ( 51524ebfcd6SJosh Poimboeuf cd "$SRC" 51624ebfcd6SJosh Poimboeuf "${cmd[@]}" 51724ebfcd6SJosh Poimboeuf ) 51824ebfcd6SJosh Poimboeuf} 51924ebfcd6SJosh Poimboeuf 52024ebfcd6SJosh Poimboeufbuild_kernel() { 521b41d8b7dSJoe Lawrence local build="$1" 52224ebfcd6SJosh Poimboeuf local log="$TMP_DIR/build.log" 5232092007aSJosh Poimboeuf local objtool_args=() 52424ebfcd6SJosh Poimboeuf local cmd=() 52524ebfcd6SJosh Poimboeuf 5262092007aSJosh Poimboeuf objtool_args=("--checksum") 5272092007aSJosh Poimboeuf 52824ebfcd6SJosh Poimboeuf cmd=("make") 52924ebfcd6SJosh Poimboeuf 53024ebfcd6SJosh Poimboeuf # When a patch to a kernel module references a newly created unexported 53124ebfcd6SJosh Poimboeuf # symbol which lives in vmlinux or another kernel module, the patched 53224ebfcd6SJosh Poimboeuf # kernel build fails with the following error: 53324ebfcd6SJosh Poimboeuf # 53424ebfcd6SJosh Poimboeuf # ERROR: modpost: "klp_string" [fs/xfs/xfs.ko] undefined! 53524ebfcd6SJosh Poimboeuf # 53624ebfcd6SJosh Poimboeuf # The undefined symbols are working as designed in that case. They get 53724ebfcd6SJosh Poimboeuf # resolved later when the livepatch module build link pulls all the 53824ebfcd6SJosh Poimboeuf # disparate objects together into the same kernel module. 53924ebfcd6SJosh Poimboeuf # 54024ebfcd6SJosh Poimboeuf # It would be good to have a way to tell modpost to skip checking for 54124ebfcd6SJosh Poimboeuf # undefined symbols altogether. For now, just convert the error to a 54224ebfcd6SJosh Poimboeuf # warning with KBUILD_MODPOST_WARN, and grep out the warning to avoid 54324ebfcd6SJosh Poimboeuf # confusing the user. 54424ebfcd6SJosh Poimboeuf # 54524ebfcd6SJosh Poimboeuf cmd+=("KBUILD_MODPOST_WARN=1") 54624ebfcd6SJosh Poimboeuf 54724ebfcd6SJosh Poimboeuf cmd+=("$VERBOSE") 54824ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 54924ebfcd6SJosh Poimboeuf cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") 5502092007aSJosh Poimboeuf cmd+=("OBJTOOL_ARGS=${objtool_args[*]}") 55124ebfcd6SJosh Poimboeuf cmd+=("vmlinux") 55224ebfcd6SJosh Poimboeuf cmd+=("modules") 55324ebfcd6SJosh Poimboeuf 55424ebfcd6SJosh Poimboeuf ( 55524ebfcd6SJosh Poimboeuf cd "$SRC" 55624ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 55724ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 55824ebfcd6SJosh Poimboeuf 2> >(tee -a "$log" | grep0 -v "modpost.*undefined!" >&2) 559b41d8b7dSJoe Lawrence ) || die "$build kernel build failed" 56024ebfcd6SJosh Poimboeuf} 56124ebfcd6SJosh Poimboeuf 56224ebfcd6SJosh Poimboeuffind_objects() { 56324ebfcd6SJosh Poimboeuf local opts=("$@") 56424ebfcd6SJosh Poimboeuf 56524ebfcd6SJosh Poimboeuf # Find root-level vmlinux.o and non-root-level .ko files, 56624ebfcd6SJosh Poimboeuf # excluding klp-tmp/ and .git/ 56724ebfcd6SJosh Poimboeuf find "$OBJ" \( -path "$TMP_DIR" -o -path "$OBJ/.git" -o -regex "$OBJ/[^/][^/]*\.ko" \) -prune -o \ 56824ebfcd6SJosh Poimboeuf -type f "${opts[@]}" \ 56924ebfcd6SJosh Poimboeuf \( -name "*.ko" -o -path "$OBJ/vmlinux.o" \) \ 57024ebfcd6SJosh Poimboeuf -printf '%P\n' 57124ebfcd6SJosh Poimboeuf} 57224ebfcd6SJosh Poimboeuf 57324ebfcd6SJosh Poimboeuf# Copy all .o archives to $ORIG_DIR 57424ebfcd6SJosh Poimboeufcopy_orig_objects() { 57524ebfcd6SJosh Poimboeuf local files=() 57624ebfcd6SJosh Poimboeuf 57724ebfcd6SJosh Poimboeuf rm -rf "$ORIG_DIR" 57824ebfcd6SJosh Poimboeuf mkdir -p "$ORIG_DIR" 57924ebfcd6SJosh Poimboeuf 58024ebfcd6SJosh Poimboeuf find_objects | mapfile -t files 58124ebfcd6SJosh Poimboeuf 58224ebfcd6SJosh Poimboeuf xtrace_save "copying orig objects" 58324ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 58424ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 58524ebfcd6SJosh Poimboeuf local file="$OBJ/$rel_file" 58624ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 58724ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 58824ebfcd6SJosh Poimboeuf 58924ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 59024ebfcd6SJosh Poimboeuf 59124ebfcd6SJosh Poimboeuf mkdir -p "$orig_dir" 59224ebfcd6SJosh Poimboeuf cp -f "$file" "$orig_dir" 59324ebfcd6SJosh Poimboeuf done 59424ebfcd6SJosh Poimboeuf xtrace_restore 59524ebfcd6SJosh Poimboeuf 59624ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$ORIG_DIR" 59724ebfcd6SJosh Poimboeuf touch "$TIMESTAMP" 59824ebfcd6SJosh Poimboeuf} 59924ebfcd6SJosh Poimboeuf 60024ebfcd6SJosh Poimboeuf# Copy all changed objects to $PATCHED_DIR 60124ebfcd6SJosh Poimboeufcopy_patched_objects() { 60224ebfcd6SJosh Poimboeuf local files=() 60324ebfcd6SJosh Poimboeuf local opts=() 60424ebfcd6SJosh Poimboeuf local found=0 60524ebfcd6SJosh Poimboeuf 60624ebfcd6SJosh Poimboeuf rm -rf "$PATCHED_DIR" 60724ebfcd6SJosh Poimboeuf mkdir -p "$PATCHED_DIR" 60824ebfcd6SJosh Poimboeuf 60924ebfcd6SJosh Poimboeuf # Note this doesn't work with some configs, thus the 'cmp' below. 61024ebfcd6SJosh Poimboeuf opts=("-newer") 61124ebfcd6SJosh Poimboeuf opts+=("$TIMESTAMP") 61224ebfcd6SJosh Poimboeuf 61324ebfcd6SJosh Poimboeuf find_objects "${opts[@]}" | mapfile -t files 61424ebfcd6SJosh Poimboeuf 61524ebfcd6SJosh Poimboeuf xtrace_save "copying changed objects" 61624ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 61724ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 61824ebfcd6SJosh Poimboeuf local file="$OBJ/$rel_file" 61924ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 62024ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 62124ebfcd6SJosh Poimboeuf local patched_dir="$(dirname "$patched_file")" 62224ebfcd6SJosh Poimboeuf 62324ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 62424ebfcd6SJosh Poimboeuf 62524ebfcd6SJosh Poimboeuf cmp -s "$orig_file" "$file" && continue 62624ebfcd6SJosh Poimboeuf 62724ebfcd6SJosh Poimboeuf mkdir -p "$patched_dir" 62824ebfcd6SJosh Poimboeuf cp -f "$file" "$patched_dir" 62924ebfcd6SJosh Poimboeuf found=1 63024ebfcd6SJosh Poimboeuf done 63124ebfcd6SJosh Poimboeuf xtrace_restore 63224ebfcd6SJosh Poimboeuf 63324ebfcd6SJosh Poimboeuf (( found == 0 )) && die "no changes detected" 63424ebfcd6SJosh Poimboeuf 63524ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$PATCHED_DIR" 63624ebfcd6SJosh Poimboeuf} 63724ebfcd6SJosh Poimboeuf 63824ebfcd6SJosh Poimboeuf# Diff changed objects, writing output object to $DIFF_DIR 63924ebfcd6SJosh Poimboeufdiff_objects() { 64024ebfcd6SJosh Poimboeuf local log="$KLP_DIFF_LOG" 64124ebfcd6SJosh Poimboeuf local files=() 6422c2f0b86SJosh Poimboeuf local opts=() 64324ebfcd6SJosh Poimboeuf 64424ebfcd6SJosh Poimboeuf rm -rf "$DIFF_DIR" 64524ebfcd6SJosh Poimboeuf mkdir -p "$DIFF_DIR" 64624ebfcd6SJosh Poimboeuf 64724ebfcd6SJosh Poimboeuf find "$PATCHED_DIR" -type f -name "*.o" | mapfile -t files 64824ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 64924ebfcd6SJosh Poimboeuf 6502c2f0b86SJosh Poimboeuf [[ -v DEBUG_CLONE ]] && opts=("--debug") 6512c2f0b86SJosh Poimboeuf 65224ebfcd6SJosh Poimboeuf # Diff all changed objects 65324ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 65424ebfcd6SJosh Poimboeuf local rel_file="${file#"$PATCHED_DIR"/}" 65524ebfcd6SJosh Poimboeuf local orig_file="$rel_file" 65624ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 65724ebfcd6SJosh Poimboeuf local out_file="$DIFF_DIR/$rel_file" 65878be9facSJosh Poimboeuf local filter=() 65924ebfcd6SJosh Poimboeuf local cmd=() 66024ebfcd6SJosh Poimboeuf 66124ebfcd6SJosh Poimboeuf mkdir -p "$(dirname "$out_file")" 66224ebfcd6SJosh Poimboeuf 66324ebfcd6SJosh Poimboeuf cmd=("$SRC/tools/objtool/objtool") 66424ebfcd6SJosh Poimboeuf cmd+=("klp") 66524ebfcd6SJosh Poimboeuf cmd+=("diff") 6662c2f0b86SJosh Poimboeuf (( ${#opts[@]} > 0 )) && cmd+=("${opts[@]}") 66724ebfcd6SJosh Poimboeuf cmd+=("$orig_file") 66824ebfcd6SJosh Poimboeuf cmd+=("$patched_file") 66924ebfcd6SJosh Poimboeuf cmd+=("$out_file") 67024ebfcd6SJosh Poimboeuf 67178be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 67278be9facSJosh Poimboeuf filter=("grep0") 67378be9facSJosh Poimboeuf filter+=("-Ev") 67478be9facSJosh Poimboeuf filter+=("DEBUG: .*checksum: ") 67578be9facSJosh Poimboeuf else 67678be9facSJosh Poimboeuf filter=("cat") 67778be9facSJosh Poimboeuf fi 67878be9facSJosh Poimboeuf 67924ebfcd6SJosh Poimboeuf ( 68024ebfcd6SJosh Poimboeuf cd "$ORIG_DIR" 68124ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 68224ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 68378be9facSJosh Poimboeuf 2> >(tee -a "$log" | "${filter[@]}" >&2) || \ 68424ebfcd6SJosh Poimboeuf die "objtool klp diff failed" 68524ebfcd6SJosh Poimboeuf ) 68624ebfcd6SJosh Poimboeuf done 68724ebfcd6SJosh Poimboeuf} 68824ebfcd6SJosh Poimboeuf 68978be9facSJosh Poimboeuf# For each changed object, run objtool with --debug-checksum to get the 69078be9facSJosh Poimboeuf# per-instruction checksums, and then diff those to find the first changed 69178be9facSJosh Poimboeuf# instruction for each function. 69278be9facSJosh Poimboeufdiff_checksums() { 69378be9facSJosh Poimboeuf local orig_log="$ORIG_DIR/checksum.log" 69478be9facSJosh Poimboeuf local patched_log="$PATCHED_DIR/checksum.log" 69578be9facSJosh Poimboeuf local -A funcs 69678be9facSJosh Poimboeuf local cmd=() 69778be9facSJosh Poimboeuf local line 69878be9facSJosh Poimboeuf local file 69978be9facSJosh Poimboeuf local func 70078be9facSJosh Poimboeuf 70178be9facSJosh Poimboeuf gawk '/\.o: changed function: / { 70278be9facSJosh Poimboeuf sub(/:$/, "", $1) 70378be9facSJosh Poimboeuf print $1, $NF 70478be9facSJosh Poimboeuf }' "$KLP_DIFF_LOG" | mapfile -t lines 70578be9facSJosh Poimboeuf 70678be9facSJosh Poimboeuf for line in "${lines[@]}"; do 70778be9facSJosh Poimboeuf read -r file func <<< "$line" 70878be9facSJosh Poimboeuf if [[ ! -v funcs["$file"] ]]; then 70978be9facSJosh Poimboeuf funcs["$file"]="$func" 71078be9facSJosh Poimboeuf else 71178be9facSJosh Poimboeuf funcs["$file"]+=" $func" 71278be9facSJosh Poimboeuf fi 71378be9facSJosh Poimboeuf done 71478be9facSJosh Poimboeuf 71578be9facSJosh Poimboeuf cmd=("$SRC/tools/objtool/objtool") 71678be9facSJosh Poimboeuf cmd+=("--checksum") 71778be9facSJosh Poimboeuf cmd+=("--link") 71878be9facSJosh Poimboeuf cmd+=("--dry-run") 71978be9facSJosh Poimboeuf 72078be9facSJosh Poimboeuf for file in "${!funcs[@]}"; do 72178be9facSJosh Poimboeuf local opt="--debug-checksum=${funcs[$file]// /,}" 72278be9facSJosh Poimboeuf 72378be9facSJosh Poimboeuf ( 72478be9facSJosh Poimboeuf cd "$ORIG_DIR" 72578be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$orig_log" || \ 72678be9facSJosh Poimboeuf ( cat "$orig_log" >&2; die "objtool --debug-checksum failed" ) 72778be9facSJosh Poimboeuf 72878be9facSJosh Poimboeuf cd "$PATCHED_DIR" 72978be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$patched_log" || \ 73078be9facSJosh Poimboeuf ( cat "$patched_log" >&2; die "objtool --debug-checksum failed" ) 73178be9facSJosh Poimboeuf ) 73278be9facSJosh Poimboeuf 73378be9facSJosh Poimboeuf for func in ${funcs[$file]}; do 734ba77fe55SJosh Poimboeuf local -a orig patched 735ba77fe55SJosh Poimboeuf paste <(grep0 -E "^DEBUG: .*checksum: $func " "$orig_log") \ 736ba77fe55SJosh Poimboeuf <(grep0 -E "^DEBUG: .*checksum: $func " "$patched_log") | 737ba77fe55SJosh Poimboeuf while IFS= read -r line; do 738ba77fe55SJosh Poimboeuf read -ra orig <<< "${line%%$'\t'*}" 739ba77fe55SJosh Poimboeuf read -ra patched <<< "${line#*$'\t'}" 740ba77fe55SJosh Poimboeuf 741ba77fe55SJosh Poimboeuf if [[ ${#patched[@]} -eq 0 ]]; then 742ba77fe55SJosh Poimboeuf printf "%s: %s: %s (removed)\n" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}" 743ba77fe55SJosh Poimboeuf break 744ba77fe55SJosh Poimboeuf elif [[ ${#orig[@]} -eq 0 ]]; then 745ba77fe55SJosh Poimboeuf printf "%s: %s: %s (added)\n" "${patched[1]%:}" "${patched[3]}" "${patched[-2]}" 746ba77fe55SJosh Poimboeuf break 747ba77fe55SJosh Poimboeuf fi 748ba77fe55SJosh Poimboeuf 749ba77fe55SJosh Poimboeuf [[ "${orig[-1]}" == "${patched[-1]}" ]] && continue 750ba77fe55SJosh Poimboeuf 751ba77fe55SJosh Poimboeuf printf "%s: %s: %s" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}" 752ba77fe55SJosh Poimboeuf [[ "${orig[-2]}" != "${patched[-2]}" ]] && \ 753ba77fe55SJosh Poimboeuf printf " (patched: %s)" "${patched[-2]}" 754ba77fe55SJosh Poimboeuf printf "\n" 755ba77fe55SJosh Poimboeuf break 756ba77fe55SJosh Poimboeuf done || true 75778be9facSJosh Poimboeuf done 75878be9facSJosh Poimboeuf done 75978be9facSJosh Poimboeuf} 76078be9facSJosh Poimboeuf 76124ebfcd6SJosh Poimboeuf# Build and post-process livepatch module in $KMOD_DIR 76224ebfcd6SJosh Poimboeufbuild_patch_module() { 76324ebfcd6SJosh Poimboeuf local makefile="$KMOD_DIR/Kbuild" 76424ebfcd6SJosh Poimboeuf local log="$KMOD_DIR/build.log" 76524ebfcd6SJosh Poimboeuf local kmod_file 76624ebfcd6SJosh Poimboeuf local cflags=() 76724ebfcd6SJosh Poimboeuf local files=() 76824ebfcd6SJosh Poimboeuf local cmd=() 76924ebfcd6SJosh Poimboeuf 77024ebfcd6SJosh Poimboeuf rm -rf "$KMOD_DIR" 77124ebfcd6SJosh Poimboeuf mkdir -p "$KMOD_DIR" 77224ebfcd6SJosh Poimboeuf 77324ebfcd6SJosh Poimboeuf cp -f "$SRC/scripts/livepatch/init.c" "$KMOD_DIR" 77424ebfcd6SJosh Poimboeuf 77524ebfcd6SJosh Poimboeuf echo "obj-m := $NAME.o" > "$makefile" 77624ebfcd6SJosh Poimboeuf echo -n "$NAME-y := init.o" >> "$makefile" 77724ebfcd6SJosh Poimboeuf 77824ebfcd6SJosh Poimboeuf find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files 77924ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 78024ebfcd6SJosh Poimboeuf 78124ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 78224ebfcd6SJosh Poimboeuf local rel_file="${file#"$DIFF_DIR"/}" 78324ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 78424ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 78524ebfcd6SJosh Poimboeuf local kmod_file="$KMOD_DIR/$rel_file" 78624ebfcd6SJosh Poimboeuf local kmod_dir="$(dirname "$kmod_file")" 78778c268f3SJosh Poimboeuf local cmd_file="$kmod_dir/.$(basename "$file").cmd" 78824ebfcd6SJosh Poimboeuf 78924ebfcd6SJosh Poimboeuf mkdir -p "$kmod_dir" 79024ebfcd6SJosh Poimboeuf cp -f "$file" "$kmod_dir" 79124ebfcd6SJosh Poimboeuf 79224ebfcd6SJosh Poimboeuf # Tell kbuild this is a prebuilt object 79324ebfcd6SJosh Poimboeuf cp -f "$file" "${kmod_file}_shipped" 79424ebfcd6SJosh Poimboeuf 79578c268f3SJosh Poimboeuf # Make modpost happy 79678c268f3SJosh Poimboeuf touch "$cmd_file" 79778c268f3SJosh Poimboeuf 79824ebfcd6SJosh Poimboeuf echo -n " $rel_file" >> "$makefile" 79924ebfcd6SJosh Poimboeuf done 80024ebfcd6SJosh Poimboeuf 80124ebfcd6SJosh Poimboeuf echo >> "$makefile" 80224ebfcd6SJosh Poimboeuf 80324ebfcd6SJosh Poimboeuf cflags=("-ffunction-sections") 80424ebfcd6SJosh Poimboeuf cflags+=("-fdata-sections") 80524ebfcd6SJosh Poimboeuf [[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE") 80624ebfcd6SJosh Poimboeuf 80724ebfcd6SJosh Poimboeuf cmd=("make") 80824ebfcd6SJosh Poimboeuf cmd+=("$VERBOSE") 80924ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 81024ebfcd6SJosh Poimboeuf cmd+=("--directory=.") 81124ebfcd6SJosh Poimboeuf cmd+=("M=$KMOD_DIR") 81224ebfcd6SJosh Poimboeuf cmd+=("KCFLAGS=${cflags[*]}") 81324ebfcd6SJosh Poimboeuf 81424ebfcd6SJosh Poimboeuf # Build a "normal" kernel module with init.c and the diffed objects 81524ebfcd6SJosh Poimboeuf ( 81624ebfcd6SJosh Poimboeuf cd "$SRC" 81724ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 81824ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 81924ebfcd6SJosh Poimboeuf 2> >(tee -a "$log" >&2) 82024ebfcd6SJosh Poimboeuf ) 82124ebfcd6SJosh Poimboeuf 82224ebfcd6SJosh Poimboeuf kmod_file="$KMOD_DIR/$NAME.ko" 82324ebfcd6SJosh Poimboeuf 82424ebfcd6SJosh Poimboeuf # Save off the intermediate binary for debugging 82524ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$kmod_file.orig" 82624ebfcd6SJosh Poimboeuf 82724ebfcd6SJosh Poimboeuf # Work around issue where slight .config change makes corrupt BTF 82824ebfcd6SJosh Poimboeuf objcopy --remove-section=.BTF "$kmod_file" 82924ebfcd6SJosh Poimboeuf 83024ebfcd6SJosh Poimboeuf # Fix (and work around) linker wreckage for klp syms / relocs 83124ebfcd6SJosh Poimboeuf "$SRC/tools/objtool/objtool" klp post-link "$kmod_file" || die "objtool klp post-link failed" 83224ebfcd6SJosh Poimboeuf 83324ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$OUTFILE" 83424ebfcd6SJosh Poimboeuf} 83524ebfcd6SJosh Poimboeuf 83624ebfcd6SJosh Poimboeuf 83724ebfcd6SJosh Poimboeuf################################################################################ 83824ebfcd6SJosh Poimboeuf 83924ebfcd6SJosh Poimboeufprocess_args "$@" 84024ebfcd6SJosh Poimboeufdo_init 84124ebfcd6SJosh Poimboeuf 842e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 2 )); then 84324ebfcd6SJosh Poimboeuf status "Validating patch(es)" 84424ebfcd6SJosh Poimboeuf validate_patches 845e506ad21SJoe Lawrencefi 846e506ad21SJoe Lawrence 847e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 1 )); then 84824ebfcd6SJosh Poimboeuf status "Building original kernel" 84924ebfcd6SJosh Poimboeuf clean_kernel 850b41d8b7dSJoe Lawrence build_kernel "original" 85124ebfcd6SJosh Poimboeuf status "Copying original object files" 85224ebfcd6SJosh Poimboeuf copy_orig_objects 85324ebfcd6SJosh Poimboeuffi 85424ebfcd6SJosh Poimboeuf 85524ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 2 )); then 85624ebfcd6SJosh Poimboeuf status "Fixing patch(es)" 85724ebfcd6SJosh Poimboeuf fix_patches 85851a0b7c4SJoe Lawrence apply_patches "--silent" 85924ebfcd6SJosh Poimboeuf status "Building patched kernel" 860b41d8b7dSJoe Lawrence build_kernel "patched" 86124ebfcd6SJosh Poimboeuf revert_patches 86224ebfcd6SJosh Poimboeuf status "Copying patched object files" 86324ebfcd6SJosh Poimboeuf copy_patched_objects 86424ebfcd6SJosh Poimboeuffi 86524ebfcd6SJosh Poimboeuf 86624ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 3 )); then 86724ebfcd6SJosh Poimboeuf status "Diffing objects" 86824ebfcd6SJosh Poimboeuf diff_objects 86978be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 87078be9facSJosh Poimboeuf status "Finding first changed instructions" 87178be9facSJosh Poimboeuf diff_checksums 87278be9facSJosh Poimboeuf fi 87324ebfcd6SJosh Poimboeuffi 87424ebfcd6SJosh Poimboeuf 87524ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 4 )); then 87624ebfcd6SJosh Poimboeuf status "Building patch module: $OUTFILE" 87724ebfcd6SJosh Poimboeuf build_patch_module 87824ebfcd6SJosh Poimboeuffi 87924ebfcd6SJosh Poimboeuf 88024ebfcd6SJosh Poimboeufstatus "SUCCESS" 881