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 2296524543SJosh Poimboeufunset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP VERBOSE XTRACE 2324ebfcd6SJosh Poimboeuf 2424ebfcd6SJosh PoimboeufREPLACE=1 2524ebfcd6SJosh PoimboeufSHORT_CIRCUIT=0 2624ebfcd6SJosh PoimboeufJOBS="$(getconf _NPROCESSORS_ONLN)" 2724ebfcd6SJosh Poimboeufshopt -o xtrace | grep -q 'on' && XTRACE=1 2824ebfcd6SJosh Poimboeuf 2924ebfcd6SJosh Poimboeuf# Avoid removing the previous $TMP_DIR until args have been fully processed. 3024ebfcd6SJosh PoimboeufKEEP_TMP=1 3124ebfcd6SJosh Poimboeuf 3224ebfcd6SJosh PoimboeufSCRIPT="$(basename "$0")" 3324ebfcd6SJosh PoimboeufSCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 3424ebfcd6SJosh PoimboeufFIX_PATCH_LINES="$SCRIPT_DIR/fix-patch-lines" 3524ebfcd6SJosh Poimboeuf 36*b6480aaeSJosh PoimboeufOBJTOOL="$PWD/tools/objtool/objtool" 37*b6480aaeSJosh PoimboeufCONFIG="$PWD/.config" 38*b6480aaeSJosh PoimboeufTMP_DIR="$PWD/klp-tmp" 3924ebfcd6SJosh Poimboeuf 4024ebfcd6SJosh PoimboeufORIG_DIR="$TMP_DIR/orig" 4124ebfcd6SJosh PoimboeufPATCHED_DIR="$TMP_DIR/patched" 4224ebfcd6SJosh PoimboeufDIFF_DIR="$TMP_DIR/diff" 4324ebfcd6SJosh PoimboeufKMOD_DIR="$TMP_DIR/kmod" 4424ebfcd6SJosh Poimboeuf 4524ebfcd6SJosh PoimboeufSTASH_DIR="$TMP_DIR/stash" 4624ebfcd6SJosh PoimboeufTIMESTAMP="$TMP_DIR/timestamp" 4724ebfcd6SJosh PoimboeufPATCH_TMP_DIR="$TMP_DIR/tmp" 4824ebfcd6SJosh Poimboeuf 4924ebfcd6SJosh PoimboeufKLP_DIFF_LOG="$DIFF_DIR/diff.log" 5024ebfcd6SJosh Poimboeuf 511fbc9b85SJoe Lawrence# Terminal output colors 521fbc9b85SJoe Lawrenceread -r COLOR_RESET COLOR_BOLD COLOR_ERROR COLOR_WARN <<< "" 531fbc9b85SJoe Lawrenceif [[ -t 1 && -t 2 ]]; then 541fbc9b85SJoe Lawrence COLOR_RESET="\033[0m" 551fbc9b85SJoe Lawrence COLOR_BOLD="\033[1m" 561fbc9b85SJoe Lawrence COLOR_ERROR="\033[0;31m" 571fbc9b85SJoe Lawrence COLOR_WARN="\033[0;33m" 581fbc9b85SJoe Lawrencefi 591fbc9b85SJoe Lawrence 6024ebfcd6SJosh Poimboeufgrep0() { 61b4a53519SJoe Lawrence # shellcheck disable=SC2317 6224ebfcd6SJosh Poimboeuf command grep "$@" || true 6324ebfcd6SJosh Poimboeuf} 6424ebfcd6SJosh Poimboeuf 65e4dbf706SJoe Lawrence# Because pipefail is enabled, the grep0 helper should be used instead of 66e4dbf706SJoe Lawrence# grep, otherwise a failed match can propagate to an error. 67e4dbf706SJoe Lawrencegrep() { 68e4dbf706SJoe Lawrence echo "error: $SCRIPT: use grep0 or 'command grep' instead of bare grep" >&2 69e4dbf706SJoe Lawrence exit 1 70e4dbf706SJoe Lawrence} 71e4dbf706SJoe Lawrence 7224ebfcd6SJosh Poimboeufstatus() { 731fbc9b85SJoe Lawrence echo -e "${COLOR_BOLD}$*${COLOR_RESET}" 7424ebfcd6SJosh Poimboeuf} 7524ebfcd6SJosh Poimboeuf 7624ebfcd6SJosh Poimboeufwarn() { 771fbc9b85SJoe Lawrence echo -e "${COLOR_WARN}warning${COLOR_RESET}: $SCRIPT: $*" >&2 7824ebfcd6SJosh Poimboeuf} 7924ebfcd6SJosh Poimboeuf 8024ebfcd6SJosh Poimboeufdie() { 811fbc9b85SJoe Lawrence echo -e "${COLOR_ERROR}error${COLOR_RESET}: $SCRIPT: $*" >&2 8224ebfcd6SJosh Poimboeuf exit 1 8324ebfcd6SJosh Poimboeuf} 8424ebfcd6SJosh Poimboeuf 8524ebfcd6SJosh Poimboeufdeclare -a STASHED_FILES 8624ebfcd6SJosh Poimboeuf 8724ebfcd6SJosh Poimboeufstash_file() { 8824ebfcd6SJosh Poimboeuf local file="$1" 89*b6480aaeSJosh Poimboeuf local rel_file="${file#"$PWD"/}" 9024ebfcd6SJosh Poimboeuf 9124ebfcd6SJosh Poimboeuf [[ ! -e "$file" ]] && die "no file to stash: $file" 9224ebfcd6SJosh Poimboeuf 9324ebfcd6SJosh Poimboeuf mkdir -p "$STASH_DIR/$(dirname "$rel_file")" 9424ebfcd6SJosh Poimboeuf cp -f "$file" "$STASH_DIR/$rel_file" 9524ebfcd6SJosh Poimboeuf 9624ebfcd6SJosh Poimboeuf STASHED_FILES+=("$rel_file") 9724ebfcd6SJosh Poimboeuf} 9824ebfcd6SJosh Poimboeuf 9924ebfcd6SJosh Poimboeufrestore_files() { 10024ebfcd6SJosh Poimboeuf local file 10124ebfcd6SJosh Poimboeuf 10224ebfcd6SJosh Poimboeuf for file in "${STASHED_FILES[@]}"; do 103*b6480aaeSJosh Poimboeuf mv -f "$STASH_DIR/$file" "$PWD/$file" || warn "can't restore file: $file" 10424ebfcd6SJosh Poimboeuf done 10524ebfcd6SJosh Poimboeuf 10624ebfcd6SJosh Poimboeuf STASHED_FILES=() 10724ebfcd6SJosh Poimboeuf} 10824ebfcd6SJosh Poimboeuf 10924ebfcd6SJosh Poimboeufcleanup() { 11024ebfcd6SJosh Poimboeuf set +o nounset 111d36a7343SJoe Lawrence revert_patches 11224ebfcd6SJosh Poimboeuf restore_files 11324ebfcd6SJosh Poimboeuf [[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR" 11424ebfcd6SJosh Poimboeuf return 0 11524ebfcd6SJosh Poimboeuf} 11624ebfcd6SJosh Poimboeuf 11724ebfcd6SJosh Poimboeuftrap_err() { 1181fbc9b85SJoe Lawrence die "line ${BASH_LINENO[0]}: '$BASH_COMMAND'" 11924ebfcd6SJosh Poimboeuf} 12024ebfcd6SJosh Poimboeuf 12124ebfcd6SJosh Poimboeuftrap cleanup EXIT INT TERM HUP 12224ebfcd6SJosh Poimboeuftrap trap_err ERR 12324ebfcd6SJosh Poimboeuf 12424ebfcd6SJosh Poimboeuf__usage() { 12524ebfcd6SJosh Poimboeuf cat <<EOF 12624ebfcd6SJosh PoimboeufUsage: $SCRIPT [OPTIONS] PATCH_FILE(s) 12724ebfcd6SJosh PoimboeufGenerate a livepatch module. 12824ebfcd6SJosh Poimboeuf 12924ebfcd6SJosh PoimboeufOptions: 13078be9facSJosh Poimboeuf -f, --show-first-changed Show address of first changed instruction 13124ebfcd6SJosh Poimboeuf -j, --jobs=<jobs> Build jobs to run simultaneously [default: $JOBS] 13224ebfcd6SJosh Poimboeuf -o, --output=<file.ko> Output file [default: livepatch-<patch-name>.ko] 13324ebfcd6SJosh Poimboeuf --no-replace Disable livepatch atomic replace 13424ebfcd6SJosh Poimboeuf -v, --verbose Pass V=1 to kernel/module builds 13524ebfcd6SJosh Poimboeuf 13624ebfcd6SJosh PoimboeufAdvanced Options: 1372c2f0b86SJosh Poimboeuf -d, --debug Show symbol/reloc cloning decisions 13824ebfcd6SJosh Poimboeuf -S, --short-circuit=STEP Start at build step (requires prior --keep-tmp) 13924ebfcd6SJosh Poimboeuf 1|orig Build original kernel (default) 14024ebfcd6SJosh Poimboeuf 2|patched Build patched kernel 14124ebfcd6SJosh Poimboeuf 3|diff Diff objects 14224ebfcd6SJosh Poimboeuf 4|kmod Build patch module 14324ebfcd6SJosh Poimboeuf -T, --keep-tmp Preserve tmp dir on exit 14424ebfcd6SJosh Poimboeuf 14524ebfcd6SJosh PoimboeufEOF 14624ebfcd6SJosh Poimboeuf} 14724ebfcd6SJosh Poimboeuf 14824ebfcd6SJosh Poimboeufusage() { 14924ebfcd6SJosh Poimboeuf __usage >&2 15024ebfcd6SJosh Poimboeuf} 15124ebfcd6SJosh Poimboeuf 15224ebfcd6SJosh Poimboeufprocess_args() { 15324ebfcd6SJosh Poimboeuf local keep_tmp=0 15424ebfcd6SJosh Poimboeuf local short 15524ebfcd6SJosh Poimboeuf local long 15624ebfcd6SJosh Poimboeuf local args 157b3ece301SJosh Poimboeuf local patch 15824ebfcd6SJosh Poimboeuf 15978be9facSJosh Poimboeuf short="hfj:o:vdS:T" 16078be9facSJosh Poimboeuf long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,short-circuit:,keep-tmp" 16124ebfcd6SJosh Poimboeuf 16224ebfcd6SJosh Poimboeuf args=$(getopt --options "$short" --longoptions "$long" -- "$@") || { 16324ebfcd6SJosh Poimboeuf echo; usage; exit 16424ebfcd6SJosh Poimboeuf } 16524ebfcd6SJosh Poimboeuf eval set -- "$args" 16624ebfcd6SJosh Poimboeuf 16724ebfcd6SJosh Poimboeuf while true; do 16824ebfcd6SJosh Poimboeuf case "$1" in 16924ebfcd6SJosh Poimboeuf -h | --help) 17024ebfcd6SJosh Poimboeuf usage 17124ebfcd6SJosh Poimboeuf exit 0 17224ebfcd6SJosh Poimboeuf ;; 17378be9facSJosh Poimboeuf -f | --show-first-changed) 17478be9facSJosh Poimboeuf DIFF_CHECKSUM=1 17578be9facSJosh Poimboeuf shift 17678be9facSJosh Poimboeuf ;; 17724ebfcd6SJosh Poimboeuf -j | --jobs) 17824ebfcd6SJosh Poimboeuf JOBS="$2" 17924ebfcd6SJosh Poimboeuf shift 2 18024ebfcd6SJosh Poimboeuf ;; 18124ebfcd6SJosh Poimboeuf -o | --output) 18224ebfcd6SJosh Poimboeuf [[ "$2" != *.ko ]] && die "output filename should end with .ko" 18324ebfcd6SJosh Poimboeuf OUTFILE="$2" 18424ebfcd6SJosh Poimboeuf NAME="$(basename "$OUTFILE")" 18524ebfcd6SJosh Poimboeuf NAME="${NAME%.ko}" 18624ebfcd6SJosh Poimboeuf NAME="$(module_name_string "$NAME")" 18724ebfcd6SJosh Poimboeuf shift 2 18824ebfcd6SJosh Poimboeuf ;; 18924ebfcd6SJosh Poimboeuf --no-replace) 19024ebfcd6SJosh Poimboeuf REPLACE=0 19124ebfcd6SJosh Poimboeuf shift 19224ebfcd6SJosh Poimboeuf ;; 19324ebfcd6SJosh Poimboeuf -v | --verbose) 19496524543SJosh Poimboeuf VERBOSE=1 19524ebfcd6SJosh Poimboeuf shift 19624ebfcd6SJosh Poimboeuf ;; 1972c2f0b86SJosh Poimboeuf -d | --debug) 1982c2f0b86SJosh Poimboeuf DEBUG_CLONE=1 1992c2f0b86SJosh Poimboeuf keep_tmp=1 2002c2f0b86SJosh Poimboeuf shift 2012c2f0b86SJosh Poimboeuf ;; 20224ebfcd6SJosh Poimboeuf -S | --short-circuit) 20324ebfcd6SJosh Poimboeuf [[ ! -d "$TMP_DIR" ]] && die "--short-circuit requires preserved klp-tmp dir" 20424ebfcd6SJosh Poimboeuf keep_tmp=1 20524ebfcd6SJosh Poimboeuf case "$2" in 20624ebfcd6SJosh Poimboeuf 1 | orig) SHORT_CIRCUIT=1; ;; 20724ebfcd6SJosh Poimboeuf 2 | patched) SHORT_CIRCUIT=2; ;; 20824ebfcd6SJosh Poimboeuf 3 | diff) SHORT_CIRCUIT=3; ;; 20924ebfcd6SJosh Poimboeuf 4 | mod) SHORT_CIRCUIT=4; ;; 21024ebfcd6SJosh Poimboeuf *) die "invalid short-circuit step '$2'" ;; 21124ebfcd6SJosh Poimboeuf esac 21224ebfcd6SJosh Poimboeuf shift 2 21324ebfcd6SJosh Poimboeuf ;; 21424ebfcd6SJosh Poimboeuf -T | --keep-tmp) 21524ebfcd6SJosh Poimboeuf keep_tmp=1 21624ebfcd6SJosh Poimboeuf shift 21724ebfcd6SJosh Poimboeuf ;; 21824ebfcd6SJosh Poimboeuf --) 21924ebfcd6SJosh Poimboeuf shift 22024ebfcd6SJosh Poimboeuf break 22124ebfcd6SJosh Poimboeuf ;; 22224ebfcd6SJosh Poimboeuf *) 22324ebfcd6SJosh Poimboeuf usage 22424ebfcd6SJosh Poimboeuf exit 1 22524ebfcd6SJosh Poimboeuf ;; 22624ebfcd6SJosh Poimboeuf esac 22724ebfcd6SJosh Poimboeuf done 22824ebfcd6SJosh Poimboeuf 229e506ad21SJoe Lawrence if [[ $# -eq 0 ]] && (( SHORT_CIRCUIT <= 2 )); then 23024ebfcd6SJosh Poimboeuf usage 23124ebfcd6SJosh Poimboeuf exit 1 23224ebfcd6SJosh Poimboeuf fi 23324ebfcd6SJosh Poimboeuf 23424ebfcd6SJosh Poimboeuf KEEP_TMP="$keep_tmp" 23524ebfcd6SJosh Poimboeuf PATCHES=("$@") 236b3ece301SJosh Poimboeuf 237b3ece301SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 238b3ece301SJosh Poimboeuf [[ -f "$patch" ]] || die "$patch doesn't exist" 239b3ece301SJosh Poimboeuf done 24024ebfcd6SJosh Poimboeuf} 24124ebfcd6SJosh Poimboeuf 24224ebfcd6SJosh Poimboeuf# temporarily disable xtrace for especially verbose code 24324ebfcd6SJosh Poimboeufxtrace_save() { 24424ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set +x 24524ebfcd6SJosh Poimboeuf return 0 24624ebfcd6SJosh Poimboeuf} 24724ebfcd6SJosh Poimboeuf 24824ebfcd6SJosh Poimboeufxtrace_restore() { 24924ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set -x 25024ebfcd6SJosh Poimboeuf return 0 25124ebfcd6SJosh Poimboeuf} 25224ebfcd6SJosh Poimboeuf 25324ebfcd6SJosh Poimboeufvalidate_config() { 25424ebfcd6SJosh Poimboeuf xtrace_save "reading .config" 25524ebfcd6SJosh Poimboeuf source "$CONFIG" || die "no .config file in $(dirname "$CONFIG")" 25624ebfcd6SJosh Poimboeuf xtrace_restore 25724ebfcd6SJosh Poimboeuf 25824ebfcd6SJosh Poimboeuf [[ -v CONFIG_LIVEPATCH ]] || \ 25924ebfcd6SJosh Poimboeuf die "CONFIG_LIVEPATCH not enabled" 26024ebfcd6SJosh Poimboeuf 26124ebfcd6SJosh Poimboeuf [[ -v CONFIG_KLP_BUILD ]] || \ 26224ebfcd6SJosh Poimboeuf die "CONFIG_KLP_BUILD not enabled" 26324ebfcd6SJosh Poimboeuf 26424ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_LATENT_ENTROPY ]] && \ 26524ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_LATENT_ENTROPY' not supported" 26624ebfcd6SJosh Poimboeuf 26724ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_RANDSTRUCT ]] && \ 26824ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_RANDSTRUCT' not supported" 26924ebfcd6SJosh Poimboeuf 270a8ff29f0SJosh Poimboeuf [[ -v CONFIG_AS_IS_LLVM ]] && \ 271a8ff29f0SJosh Poimboeuf [[ "$CONFIG_AS_VERSION" -lt 200000 ]] && \ 272a8ff29f0SJosh Poimboeuf die "Clang assembler version < 20 not supported" 273a8ff29f0SJosh Poimboeuf 27424ebfcd6SJosh Poimboeuf return 0 27524ebfcd6SJosh Poimboeuf} 27624ebfcd6SJosh Poimboeuf 27724ebfcd6SJosh Poimboeuf# Only allow alphanumerics and '_' and '-' in the module name. Everything else 27824ebfcd6SJosh Poimboeuf# is replaced with '-'. Also truncate to 55 chars so the full name + NUL 27924ebfcd6SJosh Poimboeuf# terminator fits in the kernel's 56-byte module name array. 28024ebfcd6SJosh Poimboeufmodule_name_string() { 28124ebfcd6SJosh Poimboeuf echo "${1//[^a-zA-Z0-9_-]/-}" | cut -c 1-55 28224ebfcd6SJosh Poimboeuf} 28324ebfcd6SJosh Poimboeuf 28424ebfcd6SJosh Poimboeuf# If the module name wasn't specified on the cmdline with --output, give it a 28524ebfcd6SJosh Poimboeuf# name based on the patch name. 28624ebfcd6SJosh Poimboeufset_module_name() { 28724ebfcd6SJosh Poimboeuf [[ -v NAME ]] && return 0 28824ebfcd6SJosh Poimboeuf 28924ebfcd6SJosh Poimboeuf if [[ "${#PATCHES[@]}" -eq 1 ]]; then 29024ebfcd6SJosh Poimboeuf NAME="$(basename "${PATCHES[0]}")" 29124ebfcd6SJosh Poimboeuf NAME="${NAME%.*}" 29224ebfcd6SJosh Poimboeuf else 29324ebfcd6SJosh Poimboeuf NAME="patch" 29424ebfcd6SJosh Poimboeuf fi 29524ebfcd6SJosh Poimboeuf 29624ebfcd6SJosh Poimboeuf NAME="livepatch-$NAME" 29724ebfcd6SJosh Poimboeuf NAME="$(module_name_string "$NAME")" 29824ebfcd6SJosh Poimboeuf 29924ebfcd6SJosh Poimboeuf OUTFILE="$NAME.ko" 30024ebfcd6SJosh Poimboeuf} 30124ebfcd6SJosh Poimboeuf 30224ebfcd6SJosh Poimboeuf# Hardcode the value printed by the localversion script to prevent patch 303d36a7343SJoe Lawrence# application from appending it with '+' due to a dirty working tree. 30424ebfcd6SJosh Poimboeufset_kernelversion() { 305*b6480aaeSJosh Poimboeuf local file="$PWD/scripts/setlocalversion" 3066f93f7b0SJosh Poimboeuf local kernelrelease 30724ebfcd6SJosh Poimboeuf 30824ebfcd6SJosh Poimboeuf stash_file "$file" 30924ebfcd6SJosh Poimboeuf 310cc39ccceSJosh Poimboeuf if [[ -n "$(make -s listnewconfig 2>/dev/null)" ]]; then 311cc39ccceSJosh Poimboeuf die ".config mismatch, check your .config or run 'make olddefconfig'" 312cc39ccceSJosh Poimboeuf fi 313cc39ccceSJosh Poimboeuf make syncconfig &>/dev/null || die "make syncconfig failed" 314cc39ccceSJosh Poimboeuf 315cc39ccceSJosh Poimboeuf kernelrelease="$(make -s kernelrelease)" 3166f93f7b0SJosh Poimboeuf [[ -z "$kernelrelease" ]] && die "failed to get kernel version" 31724ebfcd6SJosh Poimboeuf 3186f93f7b0SJosh Poimboeuf sed -i "2i echo $kernelrelease; exit 0" scripts/setlocalversion 31924ebfcd6SJosh Poimboeuf} 32024ebfcd6SJosh Poimboeuf 321757bd10fSJoe Lawrenceget_patch_input_files() { 32224ebfcd6SJosh Poimboeuf local patch="$1" 32324ebfcd6SJosh Poimboeuf 324757bd10fSJoe Lawrence grep0 -E '^--- ' "$patch" \ 325d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 32624ebfcd6SJosh Poimboeuf | gawk '{print $2}' \ 32724ebfcd6SJosh Poimboeuf | sed 's|^[^/]*/||' \ 32824ebfcd6SJosh Poimboeuf | sort -u 32924ebfcd6SJosh Poimboeuf} 33024ebfcd6SJosh Poimboeuf 331757bd10fSJoe Lawrenceget_patch_output_files() { 33224ebfcd6SJosh Poimboeuf local patch="$1" 33324ebfcd6SJosh Poimboeuf 334757bd10fSJoe Lawrence grep0 -E '^\+\+\+ ' "$patch" \ 335d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 336757bd10fSJoe Lawrence | gawk '{print $2}' \ 337757bd10fSJoe Lawrence | sed 's|^[^/]*/||' \ 338757bd10fSJoe Lawrence | sort -u 339757bd10fSJoe Lawrence} 34024ebfcd6SJosh Poimboeuf 34124ebfcd6SJosh Poimboeufget_patch_files() { 34224ebfcd6SJosh Poimboeuf local patch="$1" 34324ebfcd6SJosh Poimboeuf 344757bd10fSJoe Lawrence { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \ 34524ebfcd6SJosh Poimboeuf | sort -u 34624ebfcd6SJosh Poimboeuf} 34724ebfcd6SJosh Poimboeuf 34824ebfcd6SJosh Poimboeufcheck_unsupported_patches() { 34924ebfcd6SJosh Poimboeuf local patch 35024ebfcd6SJosh Poimboeuf 35124ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 35224ebfcd6SJosh Poimboeuf local files=() 35324ebfcd6SJosh Poimboeuf 35424ebfcd6SJosh Poimboeuf get_patch_files "$patch" | mapfile -t files 35524ebfcd6SJosh Poimboeuf 35624ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 35724ebfcd6SJosh Poimboeuf case "$file" in 358df0d7bb0SJosh Poimboeuf lib/*|*/vdso/*|*/realmode/rm/*|*.S) 359b41d8b7dSJoe Lawrence die "${patch}: unsupported patch to $file" 36024ebfcd6SJosh Poimboeuf ;; 36124ebfcd6SJosh Poimboeuf esac 36224ebfcd6SJosh Poimboeuf done 36324ebfcd6SJosh Poimboeuf done 36424ebfcd6SJosh Poimboeuf} 36524ebfcd6SJosh Poimboeuf 36624ebfcd6SJosh Poimboeufapply_patch() { 36724ebfcd6SJosh Poimboeuf local patch="$1" 36824ebfcd6SJosh Poimboeuf shift 36924ebfcd6SJosh Poimboeuf local extra_args=("$@") 37051a0b7c4SJoe Lawrence local drift_regex="with fuzz|offset [0-9]+ line" 37151a0b7c4SJoe Lawrence local output 37251a0b7c4SJoe Lawrence local status 37324ebfcd6SJosh Poimboeuf 37424ebfcd6SJosh Poimboeuf [[ ! -f "$patch" ]] && die "$patch doesn't exist" 37551a0b7c4SJoe Lawrence status=0 376*b6480aaeSJosh Poimboeuf output=$(patch -p1 --dry-run --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" < "$patch" 2>&1) || status=$? 37751a0b7c4SJoe Lawrence if [[ "$status" -ne 0 ]]; then 37851a0b7c4SJoe Lawrence echo "$output" >&2 37951a0b7c4SJoe Lawrence die "$patch did not apply" 38051a0b7c4SJoe Lawrence elif [[ "$output" =~ $drift_regex ]]; then 38196524543SJosh Poimboeuf [[ -v VERBOSE ]] && echo "$output" >&2 38251a0b7c4SJoe Lawrence warn "${patch} applied with fuzz" 38351a0b7c4SJoe Lawrence fi 38424ebfcd6SJosh Poimboeuf 38524ebfcd6SJosh Poimboeuf APPLIED_PATCHES+=("$patch") 386*b6480aaeSJosh Poimboeuf patch -p1 --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" --silent < "$patch" 38724ebfcd6SJosh Poimboeuf} 38824ebfcd6SJosh Poimboeuf 38924ebfcd6SJosh Poimboeufrevert_patch() { 39024ebfcd6SJosh Poimboeuf local patch="$1" 39124ebfcd6SJosh Poimboeuf local tmp=() 39224ebfcd6SJosh Poimboeuf 393*b6480aaeSJosh Poimboeuf patch -p1 -R --force --no-backup-if-mismatch -r /dev/null &> /dev/null < "$patch" || true 39424ebfcd6SJosh Poimboeuf 39524ebfcd6SJosh Poimboeuf for p in "${APPLIED_PATCHES[@]}"; do 39624ebfcd6SJosh Poimboeuf [[ "$p" == "$patch" ]] && continue 39724ebfcd6SJosh Poimboeuf tmp+=("$p") 39824ebfcd6SJosh Poimboeuf done 39924ebfcd6SJosh Poimboeuf 40024ebfcd6SJosh Poimboeuf APPLIED_PATCHES=("${tmp[@]}") 40124ebfcd6SJosh Poimboeuf} 40224ebfcd6SJosh Poimboeuf 40324ebfcd6SJosh Poimboeufapply_patches() { 40451a0b7c4SJoe Lawrence local extra_args=("$@") 40524ebfcd6SJosh Poimboeuf local patch 40624ebfcd6SJosh Poimboeuf 40724ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 40851a0b7c4SJoe Lawrence apply_patch "$patch" "${extra_args[@]}" 40924ebfcd6SJosh Poimboeuf done 41024ebfcd6SJosh Poimboeuf} 41124ebfcd6SJosh Poimboeuf 41224ebfcd6SJosh Poimboeufrevert_patches() { 41324ebfcd6SJosh Poimboeuf local patches=("${APPLIED_PATCHES[@]}") 41424ebfcd6SJosh Poimboeuf 41524ebfcd6SJosh Poimboeuf for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do 416d36a7343SJoe Lawrence revert_patch "${patches[$i]}" 41724ebfcd6SJosh Poimboeuf done 41824ebfcd6SJosh Poimboeuf 41924ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 42024ebfcd6SJosh Poimboeuf} 42124ebfcd6SJosh Poimboeuf 42224ebfcd6SJosh Poimboeufvalidate_patches() { 42324ebfcd6SJosh Poimboeuf check_unsupported_patches 42424ebfcd6SJosh Poimboeuf apply_patches 42524ebfcd6SJosh Poimboeuf revert_patches 42624ebfcd6SJosh Poimboeuf} 42724ebfcd6SJosh Poimboeuf 42824ebfcd6SJosh Poimboeufdo_init() { 42924ebfcd6SJosh Poimboeuf # We're not yet smart enough to handle anything other than in-tree 43024ebfcd6SJosh Poimboeuf # builds in pwd. 431*b6480aaeSJosh Poimboeuf [[ ! "$PWD" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory" 43224ebfcd6SJosh Poimboeuf 43324ebfcd6SJosh Poimboeuf (( SHORT_CIRCUIT <= 1 )) && rm -rf "$TMP_DIR" 43424ebfcd6SJosh Poimboeuf mkdir -p "$TMP_DIR" 43524ebfcd6SJosh Poimboeuf 43624ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 43724ebfcd6SJosh Poimboeuf 43824ebfcd6SJosh Poimboeuf [[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines" 439d36a7343SJoe Lawrence command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)" 44024ebfcd6SJosh Poimboeuf 44124ebfcd6SJosh Poimboeuf validate_config 44224ebfcd6SJosh Poimboeuf set_module_name 44324ebfcd6SJosh Poimboeuf set_kernelversion 44424ebfcd6SJosh Poimboeuf} 44524ebfcd6SJosh Poimboeuf 44624ebfcd6SJosh Poimboeuf# Refresh the patch hunk headers, specifically the line numbers and counts. 44724ebfcd6SJosh Poimboeufrefresh_patch() { 44824ebfcd6SJosh Poimboeuf local patch="$1" 44924ebfcd6SJosh Poimboeuf local tmpdir="$PATCH_TMP_DIR" 450757bd10fSJoe Lawrence local input_files=() 451757bd10fSJoe Lawrence local output_files=() 45224ebfcd6SJosh Poimboeuf 45324ebfcd6SJosh Poimboeuf rm -rf "$tmpdir" 45424ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/a" 45524ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/b" 45624ebfcd6SJosh Poimboeuf 45724ebfcd6SJosh Poimboeuf # Get all source files affected by the patch 458757bd10fSJoe Lawrence get_patch_input_files "$patch" | mapfile -t input_files 459757bd10fSJoe Lawrence get_patch_output_files "$patch" | mapfile -t output_files 46024ebfcd6SJosh Poimboeuf 46124ebfcd6SJosh Poimboeuf # Copy orig source files to 'a' 462*b6480aaeSJosh Poimboeuf echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" 46324ebfcd6SJosh Poimboeuf 46424ebfcd6SJosh Poimboeuf # Copy patched source files to 'b' 46551a0b7c4SJoe Lawrence apply_patch "$patch" "--silent" 466*b6480aaeSJosh Poimboeuf echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" 467d36a7343SJoe Lawrence revert_patch "$patch" 46824ebfcd6SJosh Poimboeuf 46924ebfcd6SJosh Poimboeuf # Diff 'a' and 'b' to make a clean patch 470d36a7343SJoe Lawrence ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true 47124ebfcd6SJosh Poimboeuf} 47224ebfcd6SJosh Poimboeuf 47324ebfcd6SJosh Poimboeuf# Copy the patches to a temporary directory, fix their lines so as not to 47424ebfcd6SJosh Poimboeuf# affect the __LINE__ macro for otherwise unchanged functions further down the 47524ebfcd6SJosh Poimboeuf# file, and update $PATCHES to point to the fixed patches. 47624ebfcd6SJosh Poimboeuffix_patches() { 47724ebfcd6SJosh Poimboeuf local idx 47824ebfcd6SJosh Poimboeuf local i 47924ebfcd6SJosh Poimboeuf 48024ebfcd6SJosh Poimboeuf rm -f "$TMP_DIR"/*.patch 48124ebfcd6SJosh Poimboeuf 48224ebfcd6SJosh Poimboeuf idx=0001 48324ebfcd6SJosh Poimboeuf for i in "${!PATCHES[@]}"; do 48424ebfcd6SJosh Poimboeuf local old_patch="${PATCHES[$i]}" 48524ebfcd6SJosh Poimboeuf local tmp_patch="$TMP_DIR/tmp.patch" 48624ebfcd6SJosh Poimboeuf local patch="${PATCHES[$i]}" 48724ebfcd6SJosh Poimboeuf local new_patch 48824ebfcd6SJosh Poimboeuf 48924ebfcd6SJosh Poimboeuf new_patch="$TMP_DIR/$idx-fixed-$(basename "$patch")" 49024ebfcd6SJosh Poimboeuf 49124ebfcd6SJosh Poimboeuf cp -f "$old_patch" "$tmp_patch" 49224ebfcd6SJosh Poimboeuf refresh_patch "$tmp_patch" 493d36a7343SJoe Lawrence "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" 49424ebfcd6SJosh Poimboeuf 49524ebfcd6SJosh Poimboeuf PATCHES[i]="$new_patch" 49624ebfcd6SJosh Poimboeuf 49724ebfcd6SJosh Poimboeuf rm -f "$tmp_patch" 49824ebfcd6SJosh Poimboeuf idx=$(printf "%04d" $(( 10#$idx + 1 ))) 49924ebfcd6SJosh Poimboeuf done 50024ebfcd6SJosh Poimboeuf} 50124ebfcd6SJosh Poimboeuf 50224ebfcd6SJosh Poimboeufclean_kernel() { 50324ebfcd6SJosh Poimboeuf local cmd=() 50424ebfcd6SJosh Poimboeuf 50524ebfcd6SJosh Poimboeuf cmd=("make") 50624ebfcd6SJosh Poimboeuf cmd+=("--silent") 50724ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 50824ebfcd6SJosh Poimboeuf cmd+=("clean") 50924ebfcd6SJosh Poimboeuf 51024ebfcd6SJosh Poimboeuf "${cmd[@]}" 51124ebfcd6SJosh Poimboeuf} 51224ebfcd6SJosh Poimboeuf 51324ebfcd6SJosh Poimboeufbuild_kernel() { 514b41d8b7dSJoe Lawrence local build="$1" 51524ebfcd6SJosh Poimboeuf local log="$TMP_DIR/build.log" 5162092007aSJosh Poimboeuf local objtool_args=() 51724ebfcd6SJosh Poimboeuf local cmd=() 51824ebfcd6SJosh Poimboeuf 5192092007aSJosh Poimboeuf objtool_args=("--checksum") 5202092007aSJosh Poimboeuf 52124ebfcd6SJosh Poimboeuf cmd=("make") 52224ebfcd6SJosh Poimboeuf 52324ebfcd6SJosh Poimboeuf # When a patch to a kernel module references a newly created unexported 52424ebfcd6SJosh Poimboeuf # symbol which lives in vmlinux or another kernel module, the patched 52524ebfcd6SJosh Poimboeuf # kernel build fails with the following error: 52624ebfcd6SJosh Poimboeuf # 52724ebfcd6SJosh Poimboeuf # ERROR: modpost: "klp_string" [fs/xfs/xfs.ko] undefined! 52824ebfcd6SJosh Poimboeuf # 52924ebfcd6SJosh Poimboeuf # The undefined symbols are working as designed in that case. They get 53024ebfcd6SJosh Poimboeuf # resolved later when the livepatch module build link pulls all the 53124ebfcd6SJosh Poimboeuf # disparate objects together into the same kernel module. 53224ebfcd6SJosh Poimboeuf # 53324ebfcd6SJosh Poimboeuf # It would be good to have a way to tell modpost to skip checking for 53424ebfcd6SJosh Poimboeuf # undefined symbols altogether. For now, just convert the error to a 53524ebfcd6SJosh Poimboeuf # warning with KBUILD_MODPOST_WARN, and grep out the warning to avoid 53624ebfcd6SJosh Poimboeuf # confusing the user. 53724ebfcd6SJosh Poimboeuf # 53824ebfcd6SJosh Poimboeuf cmd+=("KBUILD_MODPOST_WARN=1") 53924ebfcd6SJosh Poimboeuf 54096524543SJosh Poimboeuf if [[ -v VERBOSE ]]; then 54196524543SJosh Poimboeuf cmd+=("V=1") 54296524543SJosh Poimboeuf else 54396524543SJosh Poimboeuf cmd+=("-s") 54496524543SJosh Poimboeuf fi 54524ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 54624ebfcd6SJosh Poimboeuf cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") 5472092007aSJosh Poimboeuf cmd+=("OBJTOOL_ARGS=${objtool_args[*]}") 54824ebfcd6SJosh Poimboeuf cmd+=("vmlinux") 54924ebfcd6SJosh Poimboeuf cmd+=("modules") 55024ebfcd6SJosh Poimboeuf 55124ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 55224ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 553*b6480aaeSJosh Poimboeuf 2> >(tee -a "$log" | grep0 -v "modpost.*undefined!" >&2) \ 554*b6480aaeSJosh Poimboeuf || die "$build kernel build failed" 55524ebfcd6SJosh Poimboeuf} 55624ebfcd6SJosh Poimboeuf 55724ebfcd6SJosh Poimboeuffind_objects() { 55824ebfcd6SJosh Poimboeuf local opts=("$@") 55924ebfcd6SJosh Poimboeuf 56024ebfcd6SJosh Poimboeuf # Find root-level vmlinux.o and non-root-level .ko files, 56124ebfcd6SJosh Poimboeuf # excluding klp-tmp/ and .git/ 562*b6480aaeSJosh Poimboeuf find "$PWD" \( -path "$TMP_DIR" -o -path "$PWD/.git" -o -regex "$PWD/[^/][^/]*\.ko" \) -prune -o \ 56324ebfcd6SJosh Poimboeuf -type f "${opts[@]}" \ 564*b6480aaeSJosh Poimboeuf \( -name "*.ko" -o -path "$PWD/vmlinux.o" \) \ 56524ebfcd6SJosh Poimboeuf -printf '%P\n' 56624ebfcd6SJosh Poimboeuf} 56724ebfcd6SJosh Poimboeuf 56824ebfcd6SJosh Poimboeuf# Copy all .o archives to $ORIG_DIR 56924ebfcd6SJosh Poimboeufcopy_orig_objects() { 57024ebfcd6SJosh Poimboeuf local files=() 57124ebfcd6SJosh Poimboeuf 57224ebfcd6SJosh Poimboeuf rm -rf "$ORIG_DIR" 57324ebfcd6SJosh Poimboeuf mkdir -p "$ORIG_DIR" 57424ebfcd6SJosh Poimboeuf 57524ebfcd6SJosh Poimboeuf find_objects | mapfile -t files 57624ebfcd6SJosh Poimboeuf 57724ebfcd6SJosh Poimboeuf xtrace_save "copying orig objects" 57824ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 57924ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 580*b6480aaeSJosh Poimboeuf local file="$PWD/$rel_file" 58124ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 58224ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 58324ebfcd6SJosh Poimboeuf 58424ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 58524ebfcd6SJosh Poimboeuf 58624ebfcd6SJosh Poimboeuf mkdir -p "$orig_dir" 58724ebfcd6SJosh Poimboeuf cp -f "$file" "$orig_dir" 58824ebfcd6SJosh Poimboeuf done 58924ebfcd6SJosh Poimboeuf xtrace_restore 59024ebfcd6SJosh Poimboeuf 59124ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$ORIG_DIR" 59224ebfcd6SJosh Poimboeuf touch "$TIMESTAMP" 59324ebfcd6SJosh Poimboeuf} 59424ebfcd6SJosh Poimboeuf 59524ebfcd6SJosh Poimboeuf# Copy all changed objects to $PATCHED_DIR 59624ebfcd6SJosh Poimboeufcopy_patched_objects() { 59724ebfcd6SJosh Poimboeuf local files=() 59824ebfcd6SJosh Poimboeuf local opts=() 59924ebfcd6SJosh Poimboeuf local found=0 60024ebfcd6SJosh Poimboeuf 60124ebfcd6SJosh Poimboeuf rm -rf "$PATCHED_DIR" 60224ebfcd6SJosh Poimboeuf mkdir -p "$PATCHED_DIR" 60324ebfcd6SJosh Poimboeuf 60424ebfcd6SJosh Poimboeuf # Note this doesn't work with some configs, thus the 'cmp' below. 60524ebfcd6SJosh Poimboeuf opts=("-newer") 60624ebfcd6SJosh Poimboeuf opts+=("$TIMESTAMP") 60724ebfcd6SJosh Poimboeuf 60824ebfcd6SJosh Poimboeuf find_objects "${opts[@]}" | mapfile -t files 60924ebfcd6SJosh Poimboeuf 61024ebfcd6SJosh Poimboeuf xtrace_save "copying changed objects" 61124ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 61224ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 613*b6480aaeSJosh Poimboeuf local file="$PWD/$rel_file" 61424ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 61524ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 61624ebfcd6SJosh Poimboeuf local patched_dir="$(dirname "$patched_file")" 61724ebfcd6SJosh Poimboeuf 61824ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 61924ebfcd6SJosh Poimboeuf 62024ebfcd6SJosh Poimboeuf cmp -s "$orig_file" "$file" && continue 62124ebfcd6SJosh Poimboeuf 62224ebfcd6SJosh Poimboeuf mkdir -p "$patched_dir" 62324ebfcd6SJosh Poimboeuf cp -f "$file" "$patched_dir" 62424ebfcd6SJosh Poimboeuf found=1 62524ebfcd6SJosh Poimboeuf done 62624ebfcd6SJosh Poimboeuf xtrace_restore 62724ebfcd6SJosh Poimboeuf 62824ebfcd6SJosh Poimboeuf (( found == 0 )) && die "no changes detected" 62924ebfcd6SJosh Poimboeuf 63024ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$PATCHED_DIR" 63124ebfcd6SJosh Poimboeuf} 63224ebfcd6SJosh Poimboeuf 63324ebfcd6SJosh Poimboeuf# Diff changed objects, writing output object to $DIFF_DIR 63424ebfcd6SJosh Poimboeufdiff_objects() { 63524ebfcd6SJosh Poimboeuf local log="$KLP_DIFF_LOG" 63624ebfcd6SJosh Poimboeuf local files=() 6372c2f0b86SJosh Poimboeuf local opts=() 63824ebfcd6SJosh Poimboeuf 63924ebfcd6SJosh Poimboeuf rm -rf "$DIFF_DIR" 64024ebfcd6SJosh Poimboeuf mkdir -p "$DIFF_DIR" 64124ebfcd6SJosh Poimboeuf 64224ebfcd6SJosh Poimboeuf find "$PATCHED_DIR" -type f -name "*.o" | mapfile -t files 64324ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 64424ebfcd6SJosh Poimboeuf 6452c2f0b86SJosh Poimboeuf [[ -v DEBUG_CLONE ]] && opts=("--debug") 6462c2f0b86SJosh Poimboeuf 64724ebfcd6SJosh Poimboeuf # Diff all changed objects 64824ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 64924ebfcd6SJosh Poimboeuf local rel_file="${file#"$PATCHED_DIR"/}" 65024ebfcd6SJosh Poimboeuf local orig_file="$rel_file" 65124ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 65224ebfcd6SJosh Poimboeuf local out_file="$DIFF_DIR/$rel_file" 65378be9facSJosh Poimboeuf local filter=() 65424ebfcd6SJosh Poimboeuf local cmd=() 65524ebfcd6SJosh Poimboeuf 65624ebfcd6SJosh Poimboeuf mkdir -p "$(dirname "$out_file")" 65724ebfcd6SJosh Poimboeuf 658*b6480aaeSJosh Poimboeuf cmd=("$OBJTOOL") 65924ebfcd6SJosh Poimboeuf cmd+=("klp") 66024ebfcd6SJosh Poimboeuf cmd+=("diff") 6612c2f0b86SJosh Poimboeuf (( ${#opts[@]} > 0 )) && cmd+=("${opts[@]}") 66224ebfcd6SJosh Poimboeuf cmd+=("$orig_file") 66324ebfcd6SJosh Poimboeuf cmd+=("$patched_file") 66424ebfcd6SJosh Poimboeuf cmd+=("$out_file") 66524ebfcd6SJosh Poimboeuf 66678be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 66778be9facSJosh Poimboeuf filter=("grep0") 66878be9facSJosh Poimboeuf filter+=("-Ev") 66978be9facSJosh Poimboeuf filter+=("DEBUG: .*checksum: ") 67078be9facSJosh Poimboeuf else 67178be9facSJosh Poimboeuf filter=("cat") 67278be9facSJosh Poimboeuf fi 67378be9facSJosh Poimboeuf 67424ebfcd6SJosh Poimboeuf ( 67524ebfcd6SJosh Poimboeuf cd "$ORIG_DIR" 676e950d2a1SJosh Poimboeuf [[ -v VERBOSE ]] && echo "cd $ORIG_DIR && ${cmd[*]}" 67724ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 67824ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 67978be9facSJosh Poimboeuf 2> >(tee -a "$log" | "${filter[@]}" >&2) || \ 68024ebfcd6SJosh Poimboeuf die "objtool klp diff failed" 68124ebfcd6SJosh Poimboeuf ) 68224ebfcd6SJosh Poimboeuf done 68324ebfcd6SJosh Poimboeuf} 68424ebfcd6SJosh Poimboeuf 68578be9facSJosh Poimboeuf# For each changed object, run objtool with --debug-checksum to get the 68678be9facSJosh Poimboeuf# per-instruction checksums, and then diff those to find the first changed 68778be9facSJosh Poimboeuf# instruction for each function. 68878be9facSJosh Poimboeufdiff_checksums() { 68978be9facSJosh Poimboeuf local orig_log="$ORIG_DIR/checksum.log" 69078be9facSJosh Poimboeuf local patched_log="$PATCHED_DIR/checksum.log" 69178be9facSJosh Poimboeuf local -A funcs 69278be9facSJosh Poimboeuf local cmd=() 69378be9facSJosh Poimboeuf local line 69478be9facSJosh Poimboeuf local file 69578be9facSJosh Poimboeuf local func 69678be9facSJosh Poimboeuf 69778be9facSJosh Poimboeuf gawk '/\.o: changed function: / { 69878be9facSJosh Poimboeuf sub(/:$/, "", $1) 69978be9facSJosh Poimboeuf print $1, $NF 70078be9facSJosh Poimboeuf }' "$KLP_DIFF_LOG" | mapfile -t lines 70178be9facSJosh Poimboeuf 70278be9facSJosh Poimboeuf for line in "${lines[@]}"; do 70378be9facSJosh Poimboeuf read -r file func <<< "$line" 70478be9facSJosh Poimboeuf if [[ ! -v funcs["$file"] ]]; then 70578be9facSJosh Poimboeuf funcs["$file"]="$func" 70678be9facSJosh Poimboeuf else 70778be9facSJosh Poimboeuf funcs["$file"]+=" $func" 70878be9facSJosh Poimboeuf fi 70978be9facSJosh Poimboeuf done 71078be9facSJosh Poimboeuf 711*b6480aaeSJosh Poimboeuf cmd=("$OBJTOOL") 71278be9facSJosh Poimboeuf cmd+=("--checksum") 71378be9facSJosh Poimboeuf cmd+=("--link") 71478be9facSJosh Poimboeuf cmd+=("--dry-run") 71578be9facSJosh Poimboeuf 71678be9facSJosh Poimboeuf for file in "${!funcs[@]}"; do 71778be9facSJosh Poimboeuf local opt="--debug-checksum=${funcs[$file]// /,}" 71878be9facSJosh Poimboeuf 71978be9facSJosh Poimboeuf ( 72078be9facSJosh Poimboeuf cd "$ORIG_DIR" 72178be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$orig_log" || \ 72278be9facSJosh Poimboeuf ( cat "$orig_log" >&2; die "objtool --debug-checksum failed" ) 72378be9facSJosh Poimboeuf 72478be9facSJosh Poimboeuf cd "$PATCHED_DIR" 72578be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$patched_log" || \ 72678be9facSJosh Poimboeuf ( cat "$patched_log" >&2; die "objtool --debug-checksum failed" ) 72778be9facSJosh Poimboeuf ) 72878be9facSJosh Poimboeuf 72978be9facSJosh Poimboeuf for func in ${funcs[$file]}; do 730ba77fe55SJosh Poimboeuf local -a orig patched 731ba77fe55SJosh Poimboeuf paste <(grep0 -E "^DEBUG: .*checksum: $func " "$orig_log") \ 732ba77fe55SJosh Poimboeuf <(grep0 -E "^DEBUG: .*checksum: $func " "$patched_log") | 733ba77fe55SJosh Poimboeuf while IFS= read -r line; do 734ba77fe55SJosh Poimboeuf read -ra orig <<< "${line%%$'\t'*}" 735ba77fe55SJosh Poimboeuf read -ra patched <<< "${line#*$'\t'}" 736ba77fe55SJosh Poimboeuf 737ba77fe55SJosh Poimboeuf if [[ ${#patched[@]} -eq 0 ]]; then 738ba77fe55SJosh Poimboeuf printf "%s: %s: %s (removed)\n" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}" 739ba77fe55SJosh Poimboeuf break 740ba77fe55SJosh Poimboeuf elif [[ ${#orig[@]} -eq 0 ]]; then 741ba77fe55SJosh Poimboeuf printf "%s: %s: %s (added)\n" "${patched[1]%:}" "${patched[3]}" "${patched[-2]}" 742ba77fe55SJosh Poimboeuf break 743ba77fe55SJosh Poimboeuf fi 744ba77fe55SJosh Poimboeuf 745ba77fe55SJosh Poimboeuf [[ "${orig[-1]}" == "${patched[-1]}" ]] && continue 746ba77fe55SJosh Poimboeuf 747ba77fe55SJosh Poimboeuf printf "%s: %s: %s" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}" 748ba77fe55SJosh Poimboeuf [[ "${orig[-2]}" != "${patched[-2]}" ]] && \ 749ba77fe55SJosh Poimboeuf printf " (patched: %s)" "${patched[-2]}" 750ba77fe55SJosh Poimboeuf printf "\n" 751ba77fe55SJosh Poimboeuf break 752ba77fe55SJosh Poimboeuf done || true 75378be9facSJosh Poimboeuf done 75478be9facSJosh Poimboeuf done 75578be9facSJosh Poimboeuf} 75678be9facSJosh Poimboeuf 75724ebfcd6SJosh Poimboeuf# Build and post-process livepatch module in $KMOD_DIR 75824ebfcd6SJosh Poimboeufbuild_patch_module() { 75924ebfcd6SJosh Poimboeuf local makefile="$KMOD_DIR/Kbuild" 76024ebfcd6SJosh Poimboeuf local log="$KMOD_DIR/build.log" 76124ebfcd6SJosh Poimboeuf local kmod_file 76224ebfcd6SJosh Poimboeuf local cflags=() 76324ebfcd6SJosh Poimboeuf local files=() 76424ebfcd6SJosh Poimboeuf local cmd=() 76524ebfcd6SJosh Poimboeuf 76624ebfcd6SJosh Poimboeuf rm -rf "$KMOD_DIR" 76724ebfcd6SJosh Poimboeuf mkdir -p "$KMOD_DIR" 76824ebfcd6SJosh Poimboeuf 769*b6480aaeSJosh Poimboeuf cp -f "$SCRIPT_DIR/init.c" "$KMOD_DIR" 77024ebfcd6SJosh Poimboeuf 77124ebfcd6SJosh Poimboeuf echo "obj-m := $NAME.o" > "$makefile" 77224ebfcd6SJosh Poimboeuf echo -n "$NAME-y := init.o" >> "$makefile" 77324ebfcd6SJosh Poimboeuf 77424ebfcd6SJosh Poimboeuf find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files 77524ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 77624ebfcd6SJosh Poimboeuf 77724ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 77824ebfcd6SJosh Poimboeuf local rel_file="${file#"$DIFF_DIR"/}" 77924ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 78024ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 78124ebfcd6SJosh Poimboeuf local kmod_file="$KMOD_DIR/$rel_file" 78224ebfcd6SJosh Poimboeuf local kmod_dir="$(dirname "$kmod_file")" 78378c268f3SJosh Poimboeuf local cmd_file="$kmod_dir/.$(basename "$file").cmd" 78424ebfcd6SJosh Poimboeuf 78524ebfcd6SJosh Poimboeuf mkdir -p "$kmod_dir" 78624ebfcd6SJosh Poimboeuf cp -f "$file" "$kmod_dir" 78724ebfcd6SJosh Poimboeuf 78824ebfcd6SJosh Poimboeuf # Tell kbuild this is a prebuilt object 78924ebfcd6SJosh Poimboeuf cp -f "$file" "${kmod_file}_shipped" 79024ebfcd6SJosh Poimboeuf 79178c268f3SJosh Poimboeuf # Make modpost happy 79278c268f3SJosh Poimboeuf touch "$cmd_file" 79378c268f3SJosh Poimboeuf 79424ebfcd6SJosh Poimboeuf echo -n " $rel_file" >> "$makefile" 79524ebfcd6SJosh Poimboeuf done 79624ebfcd6SJosh Poimboeuf 79724ebfcd6SJosh Poimboeuf echo >> "$makefile" 79824ebfcd6SJosh Poimboeuf 79924ebfcd6SJosh Poimboeuf cflags=("-ffunction-sections") 80024ebfcd6SJosh Poimboeuf cflags+=("-fdata-sections") 80124ebfcd6SJosh Poimboeuf [[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE") 80224ebfcd6SJosh Poimboeuf 80324ebfcd6SJosh Poimboeuf cmd=("make") 80496524543SJosh Poimboeuf if [[ -v VERBOSE ]]; then 80596524543SJosh Poimboeuf cmd+=("V=1") 80696524543SJosh Poimboeuf else 80796524543SJosh Poimboeuf cmd+=("-s") 80896524543SJosh Poimboeuf fi 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 "${cmd[@]}" \ 81624ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 81724ebfcd6SJosh Poimboeuf 2> >(tee -a "$log" >&2) 81824ebfcd6SJosh Poimboeuf 81924ebfcd6SJosh Poimboeuf kmod_file="$KMOD_DIR/$NAME.ko" 82024ebfcd6SJosh Poimboeuf 82124ebfcd6SJosh Poimboeuf # Save off the intermediate binary for debugging 82224ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$kmod_file.orig" 82324ebfcd6SJosh Poimboeuf 82424ebfcd6SJosh Poimboeuf # Work around issue where slight .config change makes corrupt BTF 82524ebfcd6SJosh Poimboeuf objcopy --remove-section=.BTF "$kmod_file" 82624ebfcd6SJosh Poimboeuf 82724ebfcd6SJosh Poimboeuf # Fix (and work around) linker wreckage for klp syms / relocs 828*b6480aaeSJosh Poimboeuf "$OBJTOOL" klp post-link "$kmod_file" || die "objtool klp post-link failed" 82924ebfcd6SJosh Poimboeuf 83024ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$OUTFILE" 83124ebfcd6SJosh Poimboeuf} 83224ebfcd6SJosh Poimboeuf 83324ebfcd6SJosh Poimboeuf 83424ebfcd6SJosh Poimboeuf################################################################################ 83524ebfcd6SJosh Poimboeuf 83624ebfcd6SJosh Poimboeufprocess_args "$@" 83724ebfcd6SJosh Poimboeufdo_init 83824ebfcd6SJosh Poimboeuf 839e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 2 )); then 84024ebfcd6SJosh Poimboeuf status "Validating patch(es)" 84124ebfcd6SJosh Poimboeuf validate_patches 842e506ad21SJoe Lawrencefi 843e506ad21SJoe Lawrence 844e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 1 )); then 84524ebfcd6SJosh Poimboeuf status "Building original kernel" 84624ebfcd6SJosh Poimboeuf clean_kernel 847b41d8b7dSJoe Lawrence build_kernel "original" 84824ebfcd6SJosh Poimboeuf status "Copying original object files" 84924ebfcd6SJosh Poimboeuf copy_orig_objects 85024ebfcd6SJosh Poimboeuffi 85124ebfcd6SJosh Poimboeuf 85224ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 2 )); then 85324ebfcd6SJosh Poimboeuf status "Fixing patch(es)" 85424ebfcd6SJosh Poimboeuf fix_patches 85551a0b7c4SJoe Lawrence apply_patches "--silent" 85624ebfcd6SJosh Poimboeuf status "Building patched kernel" 857b41d8b7dSJoe Lawrence build_kernel "patched" 85824ebfcd6SJosh Poimboeuf revert_patches 85924ebfcd6SJosh Poimboeuf status "Copying patched object files" 86024ebfcd6SJosh Poimboeuf copy_patched_objects 86124ebfcd6SJosh Poimboeuffi 86224ebfcd6SJosh Poimboeuf 86324ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 3 )); then 86424ebfcd6SJosh Poimboeuf status "Diffing objects" 86524ebfcd6SJosh Poimboeuf diff_objects 86678be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 86778be9facSJosh Poimboeuf status "Finding first changed instructions" 86878be9facSJosh Poimboeuf diff_checksums 86978be9facSJosh Poimboeuf fi 87024ebfcd6SJosh Poimboeuffi 87124ebfcd6SJosh Poimboeuf 87224ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 4 )); then 87324ebfcd6SJosh Poimboeuf status "Building patch module: $OUTFILE" 87424ebfcd6SJosh Poimboeuf build_patch_module 87524ebfcd6SJosh Poimboeuffi 87624ebfcd6SJosh Poimboeuf 87724ebfcd6SJosh Poimboeufstatus "SUCCESS" 878