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 5524ebfcd6SJosh Poimboeufgrep0() { 5624ebfcd6SJosh Poimboeuf command grep "$@" || true 5724ebfcd6SJosh Poimboeuf} 5824ebfcd6SJosh Poimboeuf 5924ebfcd6SJosh Poimboeufstatus() { 6024ebfcd6SJosh Poimboeuf echo "$*" 6124ebfcd6SJosh Poimboeuf} 6224ebfcd6SJosh Poimboeuf 6324ebfcd6SJosh Poimboeufwarn() { 6424ebfcd6SJosh Poimboeuf echo "error: $SCRIPT: $*" >&2 6524ebfcd6SJosh Poimboeuf} 6624ebfcd6SJosh Poimboeuf 6724ebfcd6SJosh Poimboeufdie() { 6824ebfcd6SJosh Poimboeuf warn "$@" 6924ebfcd6SJosh Poimboeuf exit 1 7024ebfcd6SJosh Poimboeuf} 7124ebfcd6SJosh Poimboeuf 7224ebfcd6SJosh Poimboeufdeclare -a STASHED_FILES 7324ebfcd6SJosh Poimboeuf 7424ebfcd6SJosh Poimboeufstash_file() { 7524ebfcd6SJosh Poimboeuf local file="$1" 7624ebfcd6SJosh Poimboeuf local rel_file="${file#"$SRC"/}" 7724ebfcd6SJosh Poimboeuf 7824ebfcd6SJosh Poimboeuf [[ ! -e "$file" ]] && die "no file to stash: $file" 7924ebfcd6SJosh Poimboeuf 8024ebfcd6SJosh Poimboeuf mkdir -p "$STASH_DIR/$(dirname "$rel_file")" 8124ebfcd6SJosh Poimboeuf cp -f "$file" "$STASH_DIR/$rel_file" 8224ebfcd6SJosh Poimboeuf 8324ebfcd6SJosh Poimboeuf STASHED_FILES+=("$rel_file") 8424ebfcd6SJosh Poimboeuf} 8524ebfcd6SJosh Poimboeuf 8624ebfcd6SJosh Poimboeufrestore_files() { 8724ebfcd6SJosh Poimboeuf local file 8824ebfcd6SJosh Poimboeuf 8924ebfcd6SJosh Poimboeuf for file in "${STASHED_FILES[@]}"; do 9024ebfcd6SJosh Poimboeuf mv -f "$STASH_DIR/$file" "$SRC/$file" || warn "can't restore file: $file" 9124ebfcd6SJosh Poimboeuf done 9224ebfcd6SJosh Poimboeuf 9324ebfcd6SJosh Poimboeuf STASHED_FILES=() 9424ebfcd6SJosh Poimboeuf} 9524ebfcd6SJosh Poimboeuf 9624ebfcd6SJosh Poimboeufcleanup() { 9724ebfcd6SJosh Poimboeuf set +o nounset 98*d36a7343SJoe Lawrence revert_patches 9924ebfcd6SJosh Poimboeuf restore_files 10024ebfcd6SJosh Poimboeuf [[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR" 10124ebfcd6SJosh Poimboeuf return 0 10224ebfcd6SJosh Poimboeuf} 10324ebfcd6SJosh Poimboeuf 10424ebfcd6SJosh Poimboeuftrap_err() { 10524ebfcd6SJosh Poimboeuf warn "line ${BASH_LINENO[0]}: '$BASH_COMMAND'" 10624ebfcd6SJosh Poimboeuf} 10724ebfcd6SJosh Poimboeuf 10824ebfcd6SJosh Poimboeuftrap cleanup EXIT INT TERM HUP 10924ebfcd6SJosh Poimboeuftrap trap_err ERR 11024ebfcd6SJosh Poimboeuf 11124ebfcd6SJosh Poimboeuf__usage() { 11224ebfcd6SJosh Poimboeuf cat <<EOF 11324ebfcd6SJosh PoimboeufUsage: $SCRIPT [OPTIONS] PATCH_FILE(s) 11424ebfcd6SJosh PoimboeufGenerate a livepatch module. 11524ebfcd6SJosh Poimboeuf 11624ebfcd6SJosh PoimboeufOptions: 11778be9facSJosh Poimboeuf -f, --show-first-changed Show address of first changed instruction 11824ebfcd6SJosh Poimboeuf -j, --jobs=<jobs> Build jobs to run simultaneously [default: $JOBS] 11924ebfcd6SJosh Poimboeuf -o, --output=<file.ko> Output file [default: livepatch-<patch-name>.ko] 12024ebfcd6SJosh Poimboeuf --no-replace Disable livepatch atomic replace 12124ebfcd6SJosh Poimboeuf -v, --verbose Pass V=1 to kernel/module builds 12224ebfcd6SJosh Poimboeuf 12324ebfcd6SJosh PoimboeufAdvanced Options: 1242c2f0b86SJosh Poimboeuf -d, --debug Show symbol/reloc cloning decisions 12524ebfcd6SJosh Poimboeuf -S, --short-circuit=STEP Start at build step (requires prior --keep-tmp) 12624ebfcd6SJosh Poimboeuf 1|orig Build original kernel (default) 12724ebfcd6SJosh Poimboeuf 2|patched Build patched kernel 12824ebfcd6SJosh Poimboeuf 3|diff Diff objects 12924ebfcd6SJosh Poimboeuf 4|kmod Build patch module 13024ebfcd6SJosh Poimboeuf -T, --keep-tmp Preserve tmp dir on exit 13124ebfcd6SJosh Poimboeuf 13224ebfcd6SJosh PoimboeufEOF 13324ebfcd6SJosh Poimboeuf} 13424ebfcd6SJosh Poimboeuf 13524ebfcd6SJosh Poimboeufusage() { 13624ebfcd6SJosh Poimboeuf __usage >&2 13724ebfcd6SJosh Poimboeuf} 13824ebfcd6SJosh Poimboeuf 13924ebfcd6SJosh Poimboeufprocess_args() { 14024ebfcd6SJosh Poimboeuf local keep_tmp=0 14124ebfcd6SJosh Poimboeuf local short 14224ebfcd6SJosh Poimboeuf local long 14324ebfcd6SJosh Poimboeuf local args 14424ebfcd6SJosh Poimboeuf 14578be9facSJosh Poimboeuf short="hfj:o:vdS:T" 14678be9facSJosh Poimboeuf long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,short-circuit:,keep-tmp" 14724ebfcd6SJosh Poimboeuf 14824ebfcd6SJosh Poimboeuf args=$(getopt --options "$short" --longoptions "$long" -- "$@") || { 14924ebfcd6SJosh Poimboeuf echo; usage; exit 15024ebfcd6SJosh Poimboeuf } 15124ebfcd6SJosh Poimboeuf eval set -- "$args" 15224ebfcd6SJosh Poimboeuf 15324ebfcd6SJosh Poimboeuf while true; do 15424ebfcd6SJosh Poimboeuf case "$1" in 15524ebfcd6SJosh Poimboeuf -h | --help) 15624ebfcd6SJosh Poimboeuf usage 15724ebfcd6SJosh Poimboeuf exit 0 15824ebfcd6SJosh Poimboeuf ;; 15978be9facSJosh Poimboeuf -f | --show-first-changed) 16078be9facSJosh Poimboeuf DIFF_CHECKSUM=1 16178be9facSJosh Poimboeuf shift 16278be9facSJosh Poimboeuf ;; 16324ebfcd6SJosh Poimboeuf -j | --jobs) 16424ebfcd6SJosh Poimboeuf JOBS="$2" 16524ebfcd6SJosh Poimboeuf shift 2 16624ebfcd6SJosh Poimboeuf ;; 16724ebfcd6SJosh Poimboeuf -o | --output) 16824ebfcd6SJosh Poimboeuf [[ "$2" != *.ko ]] && die "output filename should end with .ko" 16924ebfcd6SJosh Poimboeuf OUTFILE="$2" 17024ebfcd6SJosh Poimboeuf NAME="$(basename "$OUTFILE")" 17124ebfcd6SJosh Poimboeuf NAME="${NAME%.ko}" 17224ebfcd6SJosh Poimboeuf NAME="$(module_name_string "$NAME")" 17324ebfcd6SJosh Poimboeuf shift 2 17424ebfcd6SJosh Poimboeuf ;; 17524ebfcd6SJosh Poimboeuf --no-replace) 17624ebfcd6SJosh Poimboeuf REPLACE=0 17724ebfcd6SJosh Poimboeuf shift 17824ebfcd6SJosh Poimboeuf ;; 17924ebfcd6SJosh Poimboeuf -v | --verbose) 18024ebfcd6SJosh Poimboeuf VERBOSE="V=1" 18124ebfcd6SJosh Poimboeuf shift 18224ebfcd6SJosh Poimboeuf ;; 1832c2f0b86SJosh Poimboeuf -d | --debug) 1842c2f0b86SJosh Poimboeuf DEBUG_CLONE=1 1852c2f0b86SJosh Poimboeuf keep_tmp=1 1862c2f0b86SJosh Poimboeuf shift 1872c2f0b86SJosh Poimboeuf ;; 18824ebfcd6SJosh Poimboeuf -S | --short-circuit) 18924ebfcd6SJosh Poimboeuf [[ ! -d "$TMP_DIR" ]] && die "--short-circuit requires preserved klp-tmp dir" 19024ebfcd6SJosh Poimboeuf keep_tmp=1 19124ebfcd6SJosh Poimboeuf case "$2" in 19224ebfcd6SJosh Poimboeuf 1 | orig) SHORT_CIRCUIT=1; ;; 19324ebfcd6SJosh Poimboeuf 2 | patched) SHORT_CIRCUIT=2; ;; 19424ebfcd6SJosh Poimboeuf 3 | diff) SHORT_CIRCUIT=3; ;; 19524ebfcd6SJosh Poimboeuf 4 | mod) SHORT_CIRCUIT=4; ;; 19624ebfcd6SJosh Poimboeuf *) die "invalid short-circuit step '$2'" ;; 19724ebfcd6SJosh Poimboeuf esac 19824ebfcd6SJosh Poimboeuf shift 2 19924ebfcd6SJosh Poimboeuf ;; 20024ebfcd6SJosh Poimboeuf -T | --keep-tmp) 20124ebfcd6SJosh Poimboeuf keep_tmp=1 20224ebfcd6SJosh Poimboeuf shift 20324ebfcd6SJosh Poimboeuf ;; 20424ebfcd6SJosh Poimboeuf --) 20524ebfcd6SJosh Poimboeuf shift 20624ebfcd6SJosh Poimboeuf break 20724ebfcd6SJosh Poimboeuf ;; 20824ebfcd6SJosh Poimboeuf *) 20924ebfcd6SJosh Poimboeuf usage 21024ebfcd6SJosh Poimboeuf exit 1 21124ebfcd6SJosh Poimboeuf ;; 21224ebfcd6SJosh Poimboeuf esac 21324ebfcd6SJosh Poimboeuf done 21424ebfcd6SJosh Poimboeuf 21524ebfcd6SJosh Poimboeuf if [[ $# -eq 0 ]]; then 21624ebfcd6SJosh Poimboeuf usage 21724ebfcd6SJosh Poimboeuf exit 1 21824ebfcd6SJosh Poimboeuf fi 21924ebfcd6SJosh Poimboeuf 22024ebfcd6SJosh Poimboeuf KEEP_TMP="$keep_tmp" 22124ebfcd6SJosh Poimboeuf PATCHES=("$@") 22224ebfcd6SJosh Poimboeuf} 22324ebfcd6SJosh Poimboeuf 22424ebfcd6SJosh Poimboeuf# temporarily disable xtrace for especially verbose code 22524ebfcd6SJosh Poimboeufxtrace_save() { 22624ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set +x 22724ebfcd6SJosh Poimboeuf return 0 22824ebfcd6SJosh Poimboeuf} 22924ebfcd6SJosh Poimboeuf 23024ebfcd6SJosh Poimboeufxtrace_restore() { 23124ebfcd6SJosh Poimboeuf [[ -v XTRACE ]] && set -x 23224ebfcd6SJosh Poimboeuf return 0 23324ebfcd6SJosh Poimboeuf} 23424ebfcd6SJosh Poimboeuf 23524ebfcd6SJosh Poimboeufvalidate_config() { 23624ebfcd6SJosh Poimboeuf xtrace_save "reading .config" 23724ebfcd6SJosh Poimboeuf source "$CONFIG" || die "no .config file in $(dirname "$CONFIG")" 23824ebfcd6SJosh Poimboeuf xtrace_restore 23924ebfcd6SJosh Poimboeuf 24024ebfcd6SJosh Poimboeuf [[ -v CONFIG_LIVEPATCH ]] || \ 24124ebfcd6SJosh Poimboeuf die "CONFIG_LIVEPATCH not enabled" 24224ebfcd6SJosh Poimboeuf 24324ebfcd6SJosh Poimboeuf [[ -v CONFIG_KLP_BUILD ]] || \ 24424ebfcd6SJosh Poimboeuf die "CONFIG_KLP_BUILD not enabled" 24524ebfcd6SJosh Poimboeuf 24624ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_LATENT_ENTROPY ]] && \ 24724ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_LATENT_ENTROPY' not supported" 24824ebfcd6SJosh Poimboeuf 24924ebfcd6SJosh Poimboeuf [[ -v CONFIG_GCC_PLUGIN_RANDSTRUCT ]] && \ 25024ebfcd6SJosh Poimboeuf die "kernel option 'CONFIG_GCC_PLUGIN_RANDSTRUCT' not supported" 25124ebfcd6SJosh Poimboeuf 252a8ff29f0SJosh Poimboeuf [[ -v CONFIG_AS_IS_LLVM ]] && \ 253a8ff29f0SJosh Poimboeuf [[ "$CONFIG_AS_VERSION" -lt 200000 ]] && \ 254a8ff29f0SJosh Poimboeuf die "Clang assembler version < 20 not supported" 255a8ff29f0SJosh Poimboeuf 25624ebfcd6SJosh Poimboeuf return 0 25724ebfcd6SJosh Poimboeuf} 25824ebfcd6SJosh Poimboeuf 25924ebfcd6SJosh Poimboeuf# Only allow alphanumerics and '_' and '-' in the module name. Everything else 26024ebfcd6SJosh Poimboeuf# is replaced with '-'. Also truncate to 55 chars so the full name + NUL 26124ebfcd6SJosh Poimboeuf# terminator fits in the kernel's 56-byte module name array. 26224ebfcd6SJosh Poimboeufmodule_name_string() { 26324ebfcd6SJosh Poimboeuf echo "${1//[^a-zA-Z0-9_-]/-}" | cut -c 1-55 26424ebfcd6SJosh Poimboeuf} 26524ebfcd6SJosh Poimboeuf 26624ebfcd6SJosh Poimboeuf# If the module name wasn't specified on the cmdline with --output, give it a 26724ebfcd6SJosh Poimboeuf# name based on the patch name. 26824ebfcd6SJosh Poimboeufset_module_name() { 26924ebfcd6SJosh Poimboeuf [[ -v NAME ]] && return 0 27024ebfcd6SJosh Poimboeuf 27124ebfcd6SJosh Poimboeuf if [[ "${#PATCHES[@]}" -eq 1 ]]; then 27224ebfcd6SJosh Poimboeuf NAME="$(basename "${PATCHES[0]}")" 27324ebfcd6SJosh Poimboeuf NAME="${NAME%.*}" 27424ebfcd6SJosh Poimboeuf else 27524ebfcd6SJosh Poimboeuf NAME="patch" 27624ebfcd6SJosh Poimboeuf fi 27724ebfcd6SJosh Poimboeuf 27824ebfcd6SJosh Poimboeuf NAME="livepatch-$NAME" 27924ebfcd6SJosh Poimboeuf NAME="$(module_name_string "$NAME")" 28024ebfcd6SJosh Poimboeuf 28124ebfcd6SJosh Poimboeuf OUTFILE="$NAME.ko" 28224ebfcd6SJosh Poimboeuf} 28324ebfcd6SJosh Poimboeuf 28424ebfcd6SJosh Poimboeuf# Hardcode the value printed by the localversion script to prevent patch 285*d36a7343SJoe Lawrence# application from appending it with '+' due to a dirty working tree. 28624ebfcd6SJosh Poimboeufset_kernelversion() { 28724ebfcd6SJosh Poimboeuf local file="$SRC/scripts/setlocalversion" 28824ebfcd6SJosh Poimboeuf local localversion 28924ebfcd6SJosh Poimboeuf 29024ebfcd6SJosh Poimboeuf stash_file "$file" 29124ebfcd6SJosh Poimboeuf 29224ebfcd6SJosh Poimboeuf localversion="$(cd "$SRC" && make --no-print-directory kernelversion)" 29324ebfcd6SJosh Poimboeuf localversion="$(cd "$SRC" && KERNELVERSION="$localversion" ./scripts/setlocalversion)" 29424ebfcd6SJosh Poimboeuf [[ -z "$localversion" ]] && die "setlocalversion failed" 29524ebfcd6SJosh Poimboeuf 29624ebfcd6SJosh Poimboeuf sed -i "2i echo $localversion; exit 0" scripts/setlocalversion 29724ebfcd6SJosh Poimboeuf} 29824ebfcd6SJosh Poimboeuf 299757bd10fSJoe Lawrenceget_patch_input_files() { 300757bd10fSJoe Lawrence local patch="$1" 301757bd10fSJoe Lawrence 302757bd10fSJoe Lawrence grep0 -E '^--- ' "$patch" \ 303*d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 304757bd10fSJoe Lawrence | gawk '{print $2}' \ 305757bd10fSJoe Lawrence | sed 's|^[^/]*/||' \ 306757bd10fSJoe Lawrence | sort -u 307757bd10fSJoe Lawrence} 308757bd10fSJoe Lawrence 309757bd10fSJoe Lawrenceget_patch_output_files() { 310757bd10fSJoe Lawrence local patch="$1" 311757bd10fSJoe Lawrence 312757bd10fSJoe Lawrence grep0 -E '^\+\+\+ ' "$patch" \ 313*d36a7343SJoe Lawrence | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 314757bd10fSJoe Lawrence | gawk '{print $2}' \ 315757bd10fSJoe Lawrence | sed 's|^[^/]*/||' \ 316757bd10fSJoe Lawrence | sort -u 317757bd10fSJoe Lawrence} 318757bd10fSJoe Lawrence 31924ebfcd6SJosh Poimboeufget_patch_files() { 32024ebfcd6SJosh Poimboeuf local patch="$1" 32124ebfcd6SJosh Poimboeuf 322757bd10fSJoe Lawrence { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \ 32324ebfcd6SJosh Poimboeuf | sort -u 32424ebfcd6SJosh Poimboeuf} 32524ebfcd6SJosh Poimboeuf 32624ebfcd6SJosh Poimboeufcheck_unsupported_patches() { 32724ebfcd6SJosh Poimboeuf local patch 32824ebfcd6SJosh Poimboeuf 32924ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 33024ebfcd6SJosh Poimboeuf local files=() 33124ebfcd6SJosh Poimboeuf 33224ebfcd6SJosh Poimboeuf get_patch_files "$patch" | mapfile -t files 33324ebfcd6SJosh Poimboeuf 33424ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 33524ebfcd6SJosh Poimboeuf case "$file" in 33624ebfcd6SJosh Poimboeuf lib/*|*.S) 33724ebfcd6SJosh Poimboeuf die "unsupported patch to $file" 33824ebfcd6SJosh Poimboeuf ;; 33924ebfcd6SJosh Poimboeuf esac 34024ebfcd6SJosh Poimboeuf done 34124ebfcd6SJosh Poimboeuf done 34224ebfcd6SJosh Poimboeuf} 34324ebfcd6SJosh Poimboeuf 34424ebfcd6SJosh Poimboeufapply_patch() { 34524ebfcd6SJosh Poimboeuf local patch="$1" 34624ebfcd6SJosh Poimboeuf 34724ebfcd6SJosh Poimboeuf [[ ! -f "$patch" ]] && die "$patch doesn't exist" 348*d36a7343SJoe Lawrence patch -d "$SRC" -p1 --dry-run --silent --no-backup-if-mismatch -r /dev/null < "$patch" 349*d36a7343SJoe Lawrence patch -d "$SRC" -p1 --silent --no-backup-if-mismatch -r /dev/null < "$patch" 35024ebfcd6SJosh Poimboeuf 35124ebfcd6SJosh Poimboeuf APPLIED_PATCHES+=("$patch") 35224ebfcd6SJosh Poimboeuf} 35324ebfcd6SJosh Poimboeuf 35424ebfcd6SJosh Poimboeufrevert_patch() { 35524ebfcd6SJosh Poimboeuf local patch="$1" 35624ebfcd6SJosh Poimboeuf local tmp=() 35724ebfcd6SJosh Poimboeuf 358*d36a7343SJoe Lawrence patch -d "$SRC" -p1 -R --silent --no-backup-if-mismatch -r /dev/null < "$patch" 35924ebfcd6SJosh Poimboeuf 36024ebfcd6SJosh Poimboeuf for p in "${APPLIED_PATCHES[@]}"; do 36124ebfcd6SJosh Poimboeuf [[ "$p" == "$patch" ]] && continue 36224ebfcd6SJosh Poimboeuf tmp+=("$p") 36324ebfcd6SJosh Poimboeuf done 36424ebfcd6SJosh Poimboeuf 36524ebfcd6SJosh Poimboeuf APPLIED_PATCHES=("${tmp[@]}") 36624ebfcd6SJosh Poimboeuf} 36724ebfcd6SJosh Poimboeuf 36824ebfcd6SJosh Poimboeufapply_patches() { 36924ebfcd6SJosh Poimboeuf local patch 37024ebfcd6SJosh Poimboeuf 37124ebfcd6SJosh Poimboeuf for patch in "${PATCHES[@]}"; do 37224ebfcd6SJosh Poimboeuf apply_patch "$patch" 37324ebfcd6SJosh Poimboeuf done 37424ebfcd6SJosh Poimboeuf} 37524ebfcd6SJosh Poimboeuf 37624ebfcd6SJosh Poimboeufrevert_patches() { 37724ebfcd6SJosh Poimboeuf local patches=("${APPLIED_PATCHES[@]}") 37824ebfcd6SJosh Poimboeuf 37924ebfcd6SJosh Poimboeuf for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do 380*d36a7343SJoe Lawrence revert_patch "${patches[$i]}" 38124ebfcd6SJosh Poimboeuf done 38224ebfcd6SJosh Poimboeuf 38324ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 38424ebfcd6SJosh Poimboeuf} 38524ebfcd6SJosh Poimboeuf 38624ebfcd6SJosh Poimboeufvalidate_patches() { 38724ebfcd6SJosh Poimboeuf check_unsupported_patches 38824ebfcd6SJosh Poimboeuf apply_patches 38924ebfcd6SJosh Poimboeuf revert_patches 39024ebfcd6SJosh Poimboeuf} 39124ebfcd6SJosh Poimboeuf 39224ebfcd6SJosh Poimboeufdo_init() { 39324ebfcd6SJosh Poimboeuf # We're not yet smart enough to handle anything other than in-tree 39424ebfcd6SJosh Poimboeuf # builds in pwd. 39524ebfcd6SJosh Poimboeuf [[ ! "$SRC" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory" 39624ebfcd6SJosh Poimboeuf [[ ! "$OBJ" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory" 39724ebfcd6SJosh Poimboeuf 39824ebfcd6SJosh Poimboeuf (( SHORT_CIRCUIT <= 1 )) && rm -rf "$TMP_DIR" 39924ebfcd6SJosh Poimboeuf mkdir -p "$TMP_DIR" 40024ebfcd6SJosh Poimboeuf 40124ebfcd6SJosh Poimboeuf APPLIED_PATCHES=() 40224ebfcd6SJosh Poimboeuf 40324ebfcd6SJosh Poimboeuf [[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines" 404*d36a7343SJoe Lawrence command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)" 40524ebfcd6SJosh Poimboeuf 40624ebfcd6SJosh Poimboeuf validate_config 40724ebfcd6SJosh Poimboeuf set_module_name 40824ebfcd6SJosh Poimboeuf set_kernelversion 40924ebfcd6SJosh Poimboeuf} 41024ebfcd6SJosh Poimboeuf 41124ebfcd6SJosh Poimboeuf# Refresh the patch hunk headers, specifically the line numbers and counts. 41224ebfcd6SJosh Poimboeufrefresh_patch() { 41324ebfcd6SJosh Poimboeuf local patch="$1" 41424ebfcd6SJosh Poimboeuf local tmpdir="$PATCH_TMP_DIR" 415757bd10fSJoe Lawrence local input_files=() 416757bd10fSJoe Lawrence local output_files=() 41724ebfcd6SJosh Poimboeuf 41824ebfcd6SJosh Poimboeuf rm -rf "$tmpdir" 41924ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/a" 42024ebfcd6SJosh Poimboeuf mkdir -p "$tmpdir/b" 42124ebfcd6SJosh Poimboeuf 42224ebfcd6SJosh Poimboeuf # Get all source files affected by the patch 423757bd10fSJoe Lawrence get_patch_input_files "$patch" | mapfile -t input_files 424757bd10fSJoe Lawrence get_patch_output_files "$patch" | mapfile -t output_files 42524ebfcd6SJosh Poimboeuf 42624ebfcd6SJosh Poimboeuf # Copy orig source files to 'a' 427757bd10fSJoe Lawrence ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) 42824ebfcd6SJosh Poimboeuf 42924ebfcd6SJosh Poimboeuf # Copy patched source files to 'b' 430*d36a7343SJoe Lawrence apply_patch "$patch" 431757bd10fSJoe Lawrence ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) 432*d36a7343SJoe Lawrence revert_patch "$patch" 43324ebfcd6SJosh Poimboeuf 43424ebfcd6SJosh Poimboeuf # Diff 'a' and 'b' to make a clean patch 435*d36a7343SJoe Lawrence ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true 43624ebfcd6SJosh Poimboeuf} 43724ebfcd6SJosh Poimboeuf 43824ebfcd6SJosh Poimboeuf# Copy the patches to a temporary directory, fix their lines so as not to 43924ebfcd6SJosh Poimboeuf# affect the __LINE__ macro for otherwise unchanged functions further down the 44024ebfcd6SJosh Poimboeuf# file, and update $PATCHES to point to the fixed patches. 44124ebfcd6SJosh Poimboeuffix_patches() { 44224ebfcd6SJosh Poimboeuf local idx 44324ebfcd6SJosh Poimboeuf local i 44424ebfcd6SJosh Poimboeuf 44524ebfcd6SJosh Poimboeuf rm -f "$TMP_DIR"/*.patch 44624ebfcd6SJosh Poimboeuf 44724ebfcd6SJosh Poimboeuf idx=0001 44824ebfcd6SJosh Poimboeuf for i in "${!PATCHES[@]}"; do 44924ebfcd6SJosh Poimboeuf local old_patch="${PATCHES[$i]}" 45024ebfcd6SJosh Poimboeuf local tmp_patch="$TMP_DIR/tmp.patch" 45124ebfcd6SJosh Poimboeuf local patch="${PATCHES[$i]}" 45224ebfcd6SJosh Poimboeuf local new_patch 45324ebfcd6SJosh Poimboeuf 45424ebfcd6SJosh Poimboeuf new_patch="$TMP_DIR/$idx-fixed-$(basename "$patch")" 45524ebfcd6SJosh Poimboeuf 45624ebfcd6SJosh Poimboeuf cp -f "$old_patch" "$tmp_patch" 45724ebfcd6SJosh Poimboeuf refresh_patch "$tmp_patch" 458*d36a7343SJoe Lawrence "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" 45924ebfcd6SJosh Poimboeuf 46024ebfcd6SJosh Poimboeuf PATCHES[i]="$new_patch" 46124ebfcd6SJosh Poimboeuf 46224ebfcd6SJosh Poimboeuf rm -f "$tmp_patch" 46324ebfcd6SJosh Poimboeuf idx=$(printf "%04d" $(( 10#$idx + 1 ))) 46424ebfcd6SJosh Poimboeuf done 46524ebfcd6SJosh Poimboeuf} 46624ebfcd6SJosh Poimboeuf 46724ebfcd6SJosh Poimboeufclean_kernel() { 46824ebfcd6SJosh Poimboeuf local cmd=() 46924ebfcd6SJosh Poimboeuf 47024ebfcd6SJosh Poimboeuf cmd=("make") 47124ebfcd6SJosh Poimboeuf cmd+=("--silent") 47224ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 47324ebfcd6SJosh Poimboeuf cmd+=("clean") 47424ebfcd6SJosh Poimboeuf 47524ebfcd6SJosh Poimboeuf ( 47624ebfcd6SJosh Poimboeuf cd "$SRC" 47724ebfcd6SJosh Poimboeuf "${cmd[@]}" 47824ebfcd6SJosh Poimboeuf ) 47924ebfcd6SJosh Poimboeuf} 48024ebfcd6SJosh Poimboeuf 48124ebfcd6SJosh Poimboeufbuild_kernel() { 48224ebfcd6SJosh Poimboeuf local log="$TMP_DIR/build.log" 4832092007aSJosh Poimboeuf local objtool_args=() 48424ebfcd6SJosh Poimboeuf local cmd=() 48524ebfcd6SJosh Poimboeuf 4862092007aSJosh Poimboeuf objtool_args=("--checksum") 4872092007aSJosh Poimboeuf 48824ebfcd6SJosh Poimboeuf cmd=("make") 48924ebfcd6SJosh Poimboeuf 49024ebfcd6SJosh Poimboeuf # When a patch to a kernel module references a newly created unexported 49124ebfcd6SJosh Poimboeuf # symbol which lives in vmlinux or another kernel module, the patched 49224ebfcd6SJosh Poimboeuf # kernel build fails with the following error: 49324ebfcd6SJosh Poimboeuf # 49424ebfcd6SJosh Poimboeuf # ERROR: modpost: "klp_string" [fs/xfs/xfs.ko] undefined! 49524ebfcd6SJosh Poimboeuf # 49624ebfcd6SJosh Poimboeuf # The undefined symbols are working as designed in that case. They get 49724ebfcd6SJosh Poimboeuf # resolved later when the livepatch module build link pulls all the 49824ebfcd6SJosh Poimboeuf # disparate objects together into the same kernel module. 49924ebfcd6SJosh Poimboeuf # 50024ebfcd6SJosh Poimboeuf # It would be good to have a way to tell modpost to skip checking for 50124ebfcd6SJosh Poimboeuf # undefined symbols altogether. For now, just convert the error to a 50224ebfcd6SJosh Poimboeuf # warning with KBUILD_MODPOST_WARN, and grep out the warning to avoid 50324ebfcd6SJosh Poimboeuf # confusing the user. 50424ebfcd6SJosh Poimboeuf # 50524ebfcd6SJosh Poimboeuf cmd+=("KBUILD_MODPOST_WARN=1") 50624ebfcd6SJosh Poimboeuf 50724ebfcd6SJosh Poimboeuf cmd+=("$VERBOSE") 50824ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 50924ebfcd6SJosh Poimboeuf cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") 5102092007aSJosh Poimboeuf cmd+=("OBJTOOL_ARGS=${objtool_args[*]}") 51124ebfcd6SJosh Poimboeuf cmd+=("vmlinux") 51224ebfcd6SJosh Poimboeuf cmd+=("modules") 51324ebfcd6SJosh Poimboeuf 51424ebfcd6SJosh Poimboeuf ( 51524ebfcd6SJosh Poimboeuf cd "$SRC" 51624ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 51724ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 51824ebfcd6SJosh Poimboeuf 2> >(tee -a "$log" | grep0 -v "modpost.*undefined!" >&2) 51924ebfcd6SJosh Poimboeuf ) 52024ebfcd6SJosh Poimboeuf} 52124ebfcd6SJosh Poimboeuf 52224ebfcd6SJosh Poimboeuffind_objects() { 52324ebfcd6SJosh Poimboeuf local opts=("$@") 52424ebfcd6SJosh Poimboeuf 52524ebfcd6SJosh Poimboeuf # Find root-level vmlinux.o and non-root-level .ko files, 52624ebfcd6SJosh Poimboeuf # excluding klp-tmp/ and .git/ 52724ebfcd6SJosh Poimboeuf find "$OBJ" \( -path "$TMP_DIR" -o -path "$OBJ/.git" -o -regex "$OBJ/[^/][^/]*\.ko" \) -prune -o \ 52824ebfcd6SJosh Poimboeuf -type f "${opts[@]}" \ 52924ebfcd6SJosh Poimboeuf \( -name "*.ko" -o -path "$OBJ/vmlinux.o" \) \ 53024ebfcd6SJosh Poimboeuf -printf '%P\n' 53124ebfcd6SJosh Poimboeuf} 53224ebfcd6SJosh Poimboeuf 53324ebfcd6SJosh Poimboeuf# Copy all .o archives to $ORIG_DIR 53424ebfcd6SJosh Poimboeufcopy_orig_objects() { 53524ebfcd6SJosh Poimboeuf local files=() 53624ebfcd6SJosh Poimboeuf 53724ebfcd6SJosh Poimboeuf rm -rf "$ORIG_DIR" 53824ebfcd6SJosh Poimboeuf mkdir -p "$ORIG_DIR" 53924ebfcd6SJosh Poimboeuf 54024ebfcd6SJosh Poimboeuf find_objects | mapfile -t files 54124ebfcd6SJosh Poimboeuf 54224ebfcd6SJosh Poimboeuf xtrace_save "copying orig objects" 54324ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 54424ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 54524ebfcd6SJosh Poimboeuf local file="$OBJ/$rel_file" 54624ebfcd6SJosh Poimboeuf local file_dir="$(dirname "$file")" 54724ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 54824ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 54924ebfcd6SJosh Poimboeuf 55024ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 55124ebfcd6SJosh Poimboeuf 55224ebfcd6SJosh Poimboeuf mkdir -p "$orig_dir" 55324ebfcd6SJosh Poimboeuf cp -f "$file" "$orig_dir" 55424ebfcd6SJosh Poimboeuf done 55524ebfcd6SJosh Poimboeuf xtrace_restore 55624ebfcd6SJosh Poimboeuf 55724ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$ORIG_DIR" 55824ebfcd6SJosh Poimboeuf touch "$TIMESTAMP" 55924ebfcd6SJosh Poimboeuf} 56024ebfcd6SJosh Poimboeuf 56124ebfcd6SJosh Poimboeuf# Copy all changed objects to $PATCHED_DIR 56224ebfcd6SJosh Poimboeufcopy_patched_objects() { 56324ebfcd6SJosh Poimboeuf local files=() 56424ebfcd6SJosh Poimboeuf local opts=() 56524ebfcd6SJosh Poimboeuf local found=0 56624ebfcd6SJosh Poimboeuf 56724ebfcd6SJosh Poimboeuf rm -rf "$PATCHED_DIR" 56824ebfcd6SJosh Poimboeuf mkdir -p "$PATCHED_DIR" 56924ebfcd6SJosh Poimboeuf 57024ebfcd6SJosh Poimboeuf # Note this doesn't work with some configs, thus the 'cmp' below. 57124ebfcd6SJosh Poimboeuf opts=("-newer") 57224ebfcd6SJosh Poimboeuf opts+=("$TIMESTAMP") 57324ebfcd6SJosh Poimboeuf 57424ebfcd6SJosh Poimboeuf find_objects "${opts[@]}" | mapfile -t files 57524ebfcd6SJosh Poimboeuf 57624ebfcd6SJosh Poimboeuf xtrace_save "copying changed objects" 57724ebfcd6SJosh Poimboeuf for _file in "${files[@]}"; do 57824ebfcd6SJosh Poimboeuf local rel_file="${_file/.ko/.o}" 57924ebfcd6SJosh Poimboeuf local file="$OBJ/$rel_file" 58024ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 58124ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 58224ebfcd6SJosh Poimboeuf local patched_dir="$(dirname "$patched_file")" 58324ebfcd6SJosh Poimboeuf 58424ebfcd6SJosh Poimboeuf [[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file" 58524ebfcd6SJosh Poimboeuf 58624ebfcd6SJosh Poimboeuf cmp -s "$orig_file" "$file" && continue 58724ebfcd6SJosh Poimboeuf 58824ebfcd6SJosh Poimboeuf mkdir -p "$patched_dir" 58924ebfcd6SJosh Poimboeuf cp -f "$file" "$patched_dir" 59024ebfcd6SJosh Poimboeuf found=1 59124ebfcd6SJosh Poimboeuf done 59224ebfcd6SJosh Poimboeuf xtrace_restore 59324ebfcd6SJosh Poimboeuf 59424ebfcd6SJosh Poimboeuf (( found == 0 )) && die "no changes detected" 59524ebfcd6SJosh Poimboeuf 59624ebfcd6SJosh Poimboeuf mv -f "$TMP_DIR/build.log" "$PATCHED_DIR" 59724ebfcd6SJosh Poimboeuf} 59824ebfcd6SJosh Poimboeuf 59924ebfcd6SJosh Poimboeuf# Diff changed objects, writing output object to $DIFF_DIR 60024ebfcd6SJosh Poimboeufdiff_objects() { 60124ebfcd6SJosh Poimboeuf local log="$KLP_DIFF_LOG" 60224ebfcd6SJosh Poimboeuf local files=() 6032c2f0b86SJosh Poimboeuf local opts=() 60424ebfcd6SJosh Poimboeuf 60524ebfcd6SJosh Poimboeuf rm -rf "$DIFF_DIR" 60624ebfcd6SJosh Poimboeuf mkdir -p "$DIFF_DIR" 60724ebfcd6SJosh Poimboeuf 60824ebfcd6SJosh Poimboeuf find "$PATCHED_DIR" -type f -name "*.o" | mapfile -t files 60924ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 61024ebfcd6SJosh Poimboeuf 6112c2f0b86SJosh Poimboeuf [[ -v DEBUG_CLONE ]] && opts=("--debug") 6122c2f0b86SJosh Poimboeuf 61324ebfcd6SJosh Poimboeuf # Diff all changed objects 61424ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 61524ebfcd6SJosh Poimboeuf local rel_file="${file#"$PATCHED_DIR"/}" 61624ebfcd6SJosh Poimboeuf local orig_file="$rel_file" 61724ebfcd6SJosh Poimboeuf local patched_file="$PATCHED_DIR/$rel_file" 61824ebfcd6SJosh Poimboeuf local out_file="$DIFF_DIR/$rel_file" 61978be9facSJosh Poimboeuf local filter=() 62024ebfcd6SJosh Poimboeuf local cmd=() 62124ebfcd6SJosh Poimboeuf 62224ebfcd6SJosh Poimboeuf mkdir -p "$(dirname "$out_file")" 62324ebfcd6SJosh Poimboeuf 62424ebfcd6SJosh Poimboeuf cmd=("$SRC/tools/objtool/objtool") 62524ebfcd6SJosh Poimboeuf cmd+=("klp") 62624ebfcd6SJosh Poimboeuf cmd+=("diff") 6272c2f0b86SJosh Poimboeuf (( ${#opts[@]} > 0 )) && cmd+=("${opts[@]}") 62824ebfcd6SJosh Poimboeuf cmd+=("$orig_file") 62924ebfcd6SJosh Poimboeuf cmd+=("$patched_file") 63024ebfcd6SJosh Poimboeuf cmd+=("$out_file") 63124ebfcd6SJosh Poimboeuf 63278be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 63378be9facSJosh Poimboeuf filter=("grep0") 63478be9facSJosh Poimboeuf filter+=("-Ev") 63578be9facSJosh Poimboeuf filter+=("DEBUG: .*checksum: ") 63678be9facSJosh Poimboeuf else 63778be9facSJosh Poimboeuf filter=("cat") 63878be9facSJosh Poimboeuf fi 63978be9facSJosh Poimboeuf 64024ebfcd6SJosh Poimboeuf ( 64124ebfcd6SJosh Poimboeuf cd "$ORIG_DIR" 64224ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 64324ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 64478be9facSJosh Poimboeuf 2> >(tee -a "$log" | "${filter[@]}" >&2) || \ 64524ebfcd6SJosh Poimboeuf die "objtool klp diff failed" 64624ebfcd6SJosh Poimboeuf ) 64724ebfcd6SJosh Poimboeuf done 64824ebfcd6SJosh Poimboeuf} 64924ebfcd6SJosh Poimboeuf 65078be9facSJosh Poimboeuf# For each changed object, run objtool with --debug-checksum to get the 65178be9facSJosh Poimboeuf# per-instruction checksums, and then diff those to find the first changed 65278be9facSJosh Poimboeuf# instruction for each function. 65378be9facSJosh Poimboeufdiff_checksums() { 65478be9facSJosh Poimboeuf local orig_log="$ORIG_DIR/checksum.log" 65578be9facSJosh Poimboeuf local patched_log="$PATCHED_DIR/checksum.log" 65678be9facSJosh Poimboeuf local -A funcs 65778be9facSJosh Poimboeuf local cmd=() 65878be9facSJosh Poimboeuf local line 65978be9facSJosh Poimboeuf local file 66078be9facSJosh Poimboeuf local func 66178be9facSJosh Poimboeuf 66278be9facSJosh Poimboeuf gawk '/\.o: changed function: / { 66378be9facSJosh Poimboeuf sub(/:$/, "", $1) 66478be9facSJosh Poimboeuf print $1, $NF 66578be9facSJosh Poimboeuf }' "$KLP_DIFF_LOG" | mapfile -t lines 66678be9facSJosh Poimboeuf 66778be9facSJosh Poimboeuf for line in "${lines[@]}"; do 66878be9facSJosh Poimboeuf read -r file func <<< "$line" 66978be9facSJosh Poimboeuf if [[ ! -v funcs["$file"] ]]; then 67078be9facSJosh Poimboeuf funcs["$file"]="$func" 67178be9facSJosh Poimboeuf else 67278be9facSJosh Poimboeuf funcs["$file"]+=" $func" 67378be9facSJosh Poimboeuf fi 67478be9facSJosh Poimboeuf done 67578be9facSJosh Poimboeuf 67678be9facSJosh Poimboeuf cmd=("$SRC/tools/objtool/objtool") 67778be9facSJosh Poimboeuf cmd+=("--checksum") 67878be9facSJosh Poimboeuf cmd+=("--link") 67978be9facSJosh Poimboeuf cmd+=("--dry-run") 68078be9facSJosh Poimboeuf 68178be9facSJosh Poimboeuf for file in "${!funcs[@]}"; do 68278be9facSJosh Poimboeuf local opt="--debug-checksum=${funcs[$file]// /,}" 68378be9facSJosh Poimboeuf 68478be9facSJosh Poimboeuf ( 68578be9facSJosh Poimboeuf cd "$ORIG_DIR" 68678be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$orig_log" || \ 68778be9facSJosh Poimboeuf ( cat "$orig_log" >&2; die "objtool --debug-checksum failed" ) 68878be9facSJosh Poimboeuf 68978be9facSJosh Poimboeuf cd "$PATCHED_DIR" 69078be9facSJosh Poimboeuf "${cmd[@]}" "$opt" "$file" &> "$patched_log" || \ 69178be9facSJosh Poimboeuf ( cat "$patched_log" >&2; die "objtool --debug-checksum failed" ) 69278be9facSJosh Poimboeuf ) 69378be9facSJosh Poimboeuf 69478be9facSJosh Poimboeuf for func in ${funcs[$file]}; do 69578be9facSJosh Poimboeuf diff <( grep0 -E "^DEBUG: .*checksum: $func " "$orig_log" | sed "s|$ORIG_DIR/||") \ 69678be9facSJosh Poimboeuf <( grep0 -E "^DEBUG: .*checksum: $func " "$patched_log" | sed "s|$PATCHED_DIR/||") \ 69778be9facSJosh Poimboeuf | gawk '/^< DEBUG: / { 69878be9facSJosh Poimboeuf gsub(/:/, "") 69978be9facSJosh Poimboeuf printf "%s: %s: %s\n", $3, $5, $6 70078be9facSJosh Poimboeuf exit 70178be9facSJosh Poimboeuf }' || true 70278be9facSJosh Poimboeuf done 70378be9facSJosh Poimboeuf done 70478be9facSJosh Poimboeuf} 70578be9facSJosh Poimboeuf 70624ebfcd6SJosh Poimboeuf# Build and post-process livepatch module in $KMOD_DIR 70724ebfcd6SJosh Poimboeufbuild_patch_module() { 70824ebfcd6SJosh Poimboeuf local makefile="$KMOD_DIR/Kbuild" 70924ebfcd6SJosh Poimboeuf local log="$KMOD_DIR/build.log" 71024ebfcd6SJosh Poimboeuf local kmod_file 71124ebfcd6SJosh Poimboeuf local cflags=() 71224ebfcd6SJosh Poimboeuf local files=() 71324ebfcd6SJosh Poimboeuf local cmd=() 71424ebfcd6SJosh Poimboeuf 71524ebfcd6SJosh Poimboeuf rm -rf "$KMOD_DIR" 71624ebfcd6SJosh Poimboeuf mkdir -p "$KMOD_DIR" 71724ebfcd6SJosh Poimboeuf 71824ebfcd6SJosh Poimboeuf cp -f "$SRC/scripts/livepatch/init.c" "$KMOD_DIR" 71924ebfcd6SJosh Poimboeuf 72024ebfcd6SJosh Poimboeuf echo "obj-m := $NAME.o" > "$makefile" 72124ebfcd6SJosh Poimboeuf echo -n "$NAME-y := init.o" >> "$makefile" 72224ebfcd6SJosh Poimboeuf 72324ebfcd6SJosh Poimboeuf find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files 72424ebfcd6SJosh Poimboeuf [[ ${#files[@]} -eq 0 ]] && die "no changes detected" 72524ebfcd6SJosh Poimboeuf 72624ebfcd6SJosh Poimboeuf for file in "${files[@]}"; do 72724ebfcd6SJosh Poimboeuf local rel_file="${file#"$DIFF_DIR"/}" 72824ebfcd6SJosh Poimboeuf local orig_file="$ORIG_DIR/$rel_file" 72924ebfcd6SJosh Poimboeuf local orig_dir="$(dirname "$orig_file")" 73024ebfcd6SJosh Poimboeuf local kmod_file="$KMOD_DIR/$rel_file" 73124ebfcd6SJosh Poimboeuf local kmod_dir="$(dirname "$kmod_file")" 73278c268f3SJosh Poimboeuf local cmd_file="$kmod_dir/.$(basename "$file").cmd" 73324ebfcd6SJosh Poimboeuf 73424ebfcd6SJosh Poimboeuf mkdir -p "$kmod_dir" 73524ebfcd6SJosh Poimboeuf cp -f "$file" "$kmod_dir" 73624ebfcd6SJosh Poimboeuf 73724ebfcd6SJosh Poimboeuf # Tell kbuild this is a prebuilt object 73824ebfcd6SJosh Poimboeuf cp -f "$file" "${kmod_file}_shipped" 73924ebfcd6SJosh Poimboeuf 74078c268f3SJosh Poimboeuf # Make modpost happy 74178c268f3SJosh Poimboeuf touch "$cmd_file" 74278c268f3SJosh Poimboeuf 74324ebfcd6SJosh Poimboeuf echo -n " $rel_file" >> "$makefile" 74424ebfcd6SJosh Poimboeuf done 74524ebfcd6SJosh Poimboeuf 74624ebfcd6SJosh Poimboeuf echo >> "$makefile" 74724ebfcd6SJosh Poimboeuf 74824ebfcd6SJosh Poimboeuf cflags=("-ffunction-sections") 74924ebfcd6SJosh Poimboeuf cflags+=("-fdata-sections") 75024ebfcd6SJosh Poimboeuf [[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE") 75124ebfcd6SJosh Poimboeuf 75224ebfcd6SJosh Poimboeuf cmd=("make") 75324ebfcd6SJosh Poimboeuf cmd+=("$VERBOSE") 75424ebfcd6SJosh Poimboeuf cmd+=("-j$JOBS") 75524ebfcd6SJosh Poimboeuf cmd+=("--directory=.") 75624ebfcd6SJosh Poimboeuf cmd+=("M=$KMOD_DIR") 75724ebfcd6SJosh Poimboeuf cmd+=("KCFLAGS=${cflags[*]}") 75824ebfcd6SJosh Poimboeuf 75924ebfcd6SJosh Poimboeuf # Build a "normal" kernel module with init.c and the diffed objects 76024ebfcd6SJosh Poimboeuf ( 76124ebfcd6SJosh Poimboeuf cd "$SRC" 76224ebfcd6SJosh Poimboeuf "${cmd[@]}" \ 76324ebfcd6SJosh Poimboeuf 1> >(tee -a "$log") \ 76424ebfcd6SJosh Poimboeuf 2> >(tee -a "$log" >&2) 76524ebfcd6SJosh Poimboeuf ) 76624ebfcd6SJosh Poimboeuf 76724ebfcd6SJosh Poimboeuf kmod_file="$KMOD_DIR/$NAME.ko" 76824ebfcd6SJosh Poimboeuf 76924ebfcd6SJosh Poimboeuf # Save off the intermediate binary for debugging 77024ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$kmod_file.orig" 77124ebfcd6SJosh Poimboeuf 77224ebfcd6SJosh Poimboeuf # Work around issue where slight .config change makes corrupt BTF 77324ebfcd6SJosh Poimboeuf objcopy --remove-section=.BTF "$kmod_file" 77424ebfcd6SJosh Poimboeuf 77524ebfcd6SJosh Poimboeuf # Fix (and work around) linker wreckage for klp syms / relocs 77624ebfcd6SJosh Poimboeuf "$SRC/tools/objtool/objtool" klp post-link "$kmod_file" || die "objtool klp post-link failed" 77724ebfcd6SJosh Poimboeuf 77824ebfcd6SJosh Poimboeuf cp -f "$kmod_file" "$OUTFILE" 77924ebfcd6SJosh Poimboeuf} 78024ebfcd6SJosh Poimboeuf 78124ebfcd6SJosh Poimboeuf 78224ebfcd6SJosh Poimboeuf################################################################################ 78324ebfcd6SJosh Poimboeuf 78424ebfcd6SJosh Poimboeufprocess_args "$@" 78524ebfcd6SJosh Poimboeufdo_init 78624ebfcd6SJosh Poimboeuf 78724ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 1 )); then 78824ebfcd6SJosh Poimboeuf status "Validating patch(es)" 78924ebfcd6SJosh Poimboeuf validate_patches 79024ebfcd6SJosh Poimboeuf status "Building original kernel" 79124ebfcd6SJosh Poimboeuf clean_kernel 79224ebfcd6SJosh Poimboeuf build_kernel 79324ebfcd6SJosh Poimboeuf status "Copying original object files" 79424ebfcd6SJosh Poimboeuf copy_orig_objects 79524ebfcd6SJosh Poimboeuffi 79624ebfcd6SJosh Poimboeuf 79724ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 2 )); then 79824ebfcd6SJosh Poimboeuf status "Fixing patch(es)" 79924ebfcd6SJosh Poimboeuf fix_patches 80024ebfcd6SJosh Poimboeuf apply_patches 80124ebfcd6SJosh Poimboeuf status "Building patched kernel" 80224ebfcd6SJosh Poimboeuf build_kernel 80324ebfcd6SJosh Poimboeuf revert_patches 80424ebfcd6SJosh Poimboeuf status "Copying patched object files" 80524ebfcd6SJosh Poimboeuf copy_patched_objects 80624ebfcd6SJosh Poimboeuffi 80724ebfcd6SJosh Poimboeuf 80824ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 3 )); then 80924ebfcd6SJosh Poimboeuf status "Diffing objects" 81024ebfcd6SJosh Poimboeuf diff_objects 81178be9facSJosh Poimboeuf if [[ -v DIFF_CHECKSUM ]]; then 81278be9facSJosh Poimboeuf status "Finding first changed instructions" 81378be9facSJosh Poimboeuf diff_checksums 81478be9facSJosh Poimboeuf fi 81524ebfcd6SJosh Poimboeuffi 81624ebfcd6SJosh Poimboeuf 81724ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 4 )); then 81824ebfcd6SJosh Poimboeuf status "Building patch module: $OUTFILE" 81924ebfcd6SJosh Poimboeuf build_patch_module 82024ebfcd6SJosh Poimboeuffi 82124ebfcd6SJosh Poimboeuf 82224ebfcd6SJosh Poimboeufstatus "SUCCESS" 823