xref: /linux/scripts/livepatch/klp-build (revision df0d7bb04a27e48a0fb5fd32223f5ab248876cab)
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
3624ebfcd6SJosh PoimboeufSRC="$(pwd)"
3724ebfcd6SJosh PoimboeufOBJ="$(pwd)"
3824ebfcd6SJosh Poimboeuf
3924ebfcd6SJosh PoimboeufCONFIG="$OBJ/.config"
4024ebfcd6SJosh PoimboeufTMP_DIR="$OBJ/klp-tmp"
4124ebfcd6SJosh Poimboeuf
4224ebfcd6SJosh PoimboeufORIG_DIR="$TMP_DIR/orig"
4324ebfcd6SJosh PoimboeufPATCHED_DIR="$TMP_DIR/patched"
4424ebfcd6SJosh PoimboeufDIFF_DIR="$TMP_DIR/diff"
4524ebfcd6SJosh PoimboeufKMOD_DIR="$TMP_DIR/kmod"
4624ebfcd6SJosh Poimboeuf
4724ebfcd6SJosh PoimboeufSTASH_DIR="$TMP_DIR/stash"
4824ebfcd6SJosh PoimboeufTIMESTAMP="$TMP_DIR/timestamp"
4924ebfcd6SJosh PoimboeufPATCH_TMP_DIR="$TMP_DIR/tmp"
5024ebfcd6SJosh Poimboeuf
5124ebfcd6SJosh PoimboeufKLP_DIFF_LOG="$DIFF_DIR/diff.log"
5224ebfcd6SJosh Poimboeuf
531fbc9b85SJoe Lawrence# Terminal output colors
541fbc9b85SJoe Lawrenceread -r COLOR_RESET COLOR_BOLD COLOR_ERROR COLOR_WARN <<< ""
551fbc9b85SJoe Lawrenceif [[ -t 1 && -t 2 ]]; then
561fbc9b85SJoe Lawrence	COLOR_RESET="\033[0m"
571fbc9b85SJoe Lawrence	COLOR_BOLD="\033[1m"
581fbc9b85SJoe Lawrence	COLOR_ERROR="\033[0;31m"
591fbc9b85SJoe Lawrence	COLOR_WARN="\033[0;33m"
601fbc9b85SJoe Lawrencefi
611fbc9b85SJoe Lawrence
6224ebfcd6SJosh Poimboeufgrep0() {
63b4a53519SJoe Lawrence	# shellcheck disable=SC2317
6424ebfcd6SJosh Poimboeuf	command grep "$@" || true
6524ebfcd6SJosh Poimboeuf}
6624ebfcd6SJosh Poimboeuf
67e4dbf706SJoe Lawrence# Because pipefail is enabled, the grep0 helper should be used instead of
68e4dbf706SJoe Lawrence# grep, otherwise a failed match can propagate to an error.
69e4dbf706SJoe Lawrencegrep() {
70e4dbf706SJoe Lawrence	echo "error: $SCRIPT: use grep0 or 'command grep' instead of bare grep" >&2
71e4dbf706SJoe Lawrence	exit 1
72e4dbf706SJoe Lawrence}
73e4dbf706SJoe Lawrence
7424ebfcd6SJosh Poimboeufstatus() {
751fbc9b85SJoe Lawrence	echo -e "${COLOR_BOLD}$*${COLOR_RESET}"
7624ebfcd6SJosh Poimboeuf}
7724ebfcd6SJosh Poimboeuf
7824ebfcd6SJosh Poimboeufwarn() {
791fbc9b85SJoe Lawrence	echo -e "${COLOR_WARN}warning${COLOR_RESET}: $SCRIPT: $*" >&2
8024ebfcd6SJosh Poimboeuf}
8124ebfcd6SJosh Poimboeuf
8224ebfcd6SJosh Poimboeufdie() {
831fbc9b85SJoe Lawrence	echo -e "${COLOR_ERROR}error${COLOR_RESET}: $SCRIPT: $*" >&2
8424ebfcd6SJosh Poimboeuf	exit 1
8524ebfcd6SJosh Poimboeuf}
8624ebfcd6SJosh Poimboeuf
8724ebfcd6SJosh Poimboeufdeclare -a STASHED_FILES
8824ebfcd6SJosh Poimboeuf
8924ebfcd6SJosh Poimboeufstash_file() {
9024ebfcd6SJosh Poimboeuf	local file="$1"
9124ebfcd6SJosh Poimboeuf	local rel_file="${file#"$SRC"/}"
9224ebfcd6SJosh Poimboeuf
9324ebfcd6SJosh Poimboeuf	[[ ! -e "$file" ]] && die "no file to stash: $file"
9424ebfcd6SJosh Poimboeuf
9524ebfcd6SJosh Poimboeuf	mkdir -p "$STASH_DIR/$(dirname "$rel_file")"
9624ebfcd6SJosh Poimboeuf	cp -f "$file" "$STASH_DIR/$rel_file"
9724ebfcd6SJosh Poimboeuf
9824ebfcd6SJosh Poimboeuf	STASHED_FILES+=("$rel_file")
9924ebfcd6SJosh Poimboeuf}
10024ebfcd6SJosh Poimboeuf
10124ebfcd6SJosh Poimboeufrestore_files() {
10224ebfcd6SJosh Poimboeuf	local file
10324ebfcd6SJosh Poimboeuf
10424ebfcd6SJosh Poimboeuf	for file in "${STASHED_FILES[@]}"; do
10524ebfcd6SJosh Poimboeuf		mv -f "$STASH_DIR/$file" "$SRC/$file" || warn "can't restore file: $file"
10624ebfcd6SJosh Poimboeuf	done
10724ebfcd6SJosh Poimboeuf
10824ebfcd6SJosh Poimboeuf	STASHED_FILES=()
10924ebfcd6SJosh Poimboeuf}
11024ebfcd6SJosh Poimboeuf
11124ebfcd6SJosh Poimboeufcleanup() {
11224ebfcd6SJosh Poimboeuf	set +o nounset
113d36a7343SJoe Lawrence	revert_patches
11424ebfcd6SJosh Poimboeuf	restore_files
11524ebfcd6SJosh Poimboeuf	[[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR"
11624ebfcd6SJosh Poimboeuf	return 0
11724ebfcd6SJosh Poimboeuf}
11824ebfcd6SJosh Poimboeuf
11924ebfcd6SJosh Poimboeuftrap_err() {
1201fbc9b85SJoe Lawrence	die "line ${BASH_LINENO[0]}: '$BASH_COMMAND'"
12124ebfcd6SJosh Poimboeuf}
12224ebfcd6SJosh Poimboeuf
12324ebfcd6SJosh Poimboeuftrap cleanup  EXIT INT TERM HUP
12424ebfcd6SJosh Poimboeuftrap trap_err ERR
12524ebfcd6SJosh Poimboeuf
12624ebfcd6SJosh Poimboeuf__usage() {
12724ebfcd6SJosh Poimboeuf	cat <<EOF
12824ebfcd6SJosh PoimboeufUsage: $SCRIPT [OPTIONS] PATCH_FILE(s)
12924ebfcd6SJosh PoimboeufGenerate a livepatch module.
13024ebfcd6SJosh Poimboeuf
13124ebfcd6SJosh PoimboeufOptions:
13278be9facSJosh Poimboeuf   -f, --show-first-changed	Show address of first changed instruction
13324ebfcd6SJosh Poimboeuf   -j, --jobs=<jobs>		Build jobs to run simultaneously [default: $JOBS]
13424ebfcd6SJosh Poimboeuf   -o, --output=<file.ko>	Output file [default: livepatch-<patch-name>.ko]
13524ebfcd6SJosh Poimboeuf       --no-replace		Disable livepatch atomic replace
13624ebfcd6SJosh Poimboeuf   -v, --verbose		Pass V=1 to kernel/module builds
13724ebfcd6SJosh Poimboeuf
13824ebfcd6SJosh PoimboeufAdvanced Options:
1392c2f0b86SJosh Poimboeuf   -d, --debug			Show symbol/reloc cloning decisions
14024ebfcd6SJosh Poimboeuf   -S, --short-circuit=STEP	Start at build step (requires prior --keep-tmp)
14124ebfcd6SJosh Poimboeuf				   1|orig	Build original kernel (default)
14224ebfcd6SJosh Poimboeuf				   2|patched	Build patched kernel
14324ebfcd6SJosh Poimboeuf				   3|diff	Diff objects
14424ebfcd6SJosh Poimboeuf				   4|kmod	Build patch module
14524ebfcd6SJosh Poimboeuf   -T, --keep-tmp		Preserve tmp dir on exit
14624ebfcd6SJosh Poimboeuf
14724ebfcd6SJosh PoimboeufEOF
14824ebfcd6SJosh Poimboeuf}
14924ebfcd6SJosh Poimboeuf
15024ebfcd6SJosh Poimboeufusage() {
15124ebfcd6SJosh Poimboeuf	__usage >&2
15224ebfcd6SJosh Poimboeuf}
15324ebfcd6SJosh Poimboeuf
15424ebfcd6SJosh Poimboeufprocess_args() {
15524ebfcd6SJosh Poimboeuf	local keep_tmp=0
15624ebfcd6SJosh Poimboeuf	local short
15724ebfcd6SJosh Poimboeuf	local long
15824ebfcd6SJosh Poimboeuf	local args
159b3ece301SJosh Poimboeuf	local patch
16024ebfcd6SJosh Poimboeuf
16178be9facSJosh Poimboeuf	short="hfj:o:vdS:T"
16278be9facSJosh Poimboeuf	long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,short-circuit:,keep-tmp"
16324ebfcd6SJosh Poimboeuf
16424ebfcd6SJosh Poimboeuf	args=$(getopt --options "$short" --longoptions "$long" -- "$@") || {
16524ebfcd6SJosh Poimboeuf		echo; usage; exit
16624ebfcd6SJosh Poimboeuf	}
16724ebfcd6SJosh Poimboeuf	eval set -- "$args"
16824ebfcd6SJosh Poimboeuf
16924ebfcd6SJosh Poimboeuf	while true; do
17024ebfcd6SJosh Poimboeuf		case "$1" in
17124ebfcd6SJosh Poimboeuf			-h | --help)
17224ebfcd6SJosh Poimboeuf				usage
17324ebfcd6SJosh Poimboeuf				exit 0
17424ebfcd6SJosh Poimboeuf				;;
17578be9facSJosh Poimboeuf			-f | --show-first-changed)
17678be9facSJosh Poimboeuf				DIFF_CHECKSUM=1
17778be9facSJosh Poimboeuf				shift
17878be9facSJosh Poimboeuf				;;
17924ebfcd6SJosh Poimboeuf			-j | --jobs)
18024ebfcd6SJosh Poimboeuf				JOBS="$2"
18124ebfcd6SJosh Poimboeuf				shift 2
18224ebfcd6SJosh Poimboeuf				;;
18324ebfcd6SJosh Poimboeuf			-o | --output)
18424ebfcd6SJosh Poimboeuf				[[ "$2" != *.ko ]] && die "output filename should end with .ko"
18524ebfcd6SJosh Poimboeuf				OUTFILE="$2"
18624ebfcd6SJosh Poimboeuf				NAME="$(basename "$OUTFILE")"
18724ebfcd6SJosh Poimboeuf				NAME="${NAME%.ko}"
18824ebfcd6SJosh Poimboeuf				NAME="$(module_name_string "$NAME")"
18924ebfcd6SJosh Poimboeuf				shift 2
19024ebfcd6SJosh Poimboeuf				;;
19124ebfcd6SJosh Poimboeuf			--no-replace)
19224ebfcd6SJosh Poimboeuf				REPLACE=0
19324ebfcd6SJosh Poimboeuf				shift
19424ebfcd6SJosh Poimboeuf				;;
19524ebfcd6SJosh Poimboeuf			-v | --verbose)
19696524543SJosh Poimboeuf				VERBOSE=1
19724ebfcd6SJosh Poimboeuf				shift
19824ebfcd6SJosh Poimboeuf				;;
1992c2f0b86SJosh Poimboeuf			-d | --debug)
2002c2f0b86SJosh Poimboeuf				DEBUG_CLONE=1
2012c2f0b86SJosh Poimboeuf				keep_tmp=1
2022c2f0b86SJosh Poimboeuf				shift
2032c2f0b86SJosh Poimboeuf				;;
20424ebfcd6SJosh Poimboeuf			-S | --short-circuit)
20524ebfcd6SJosh Poimboeuf				[[ ! -d "$TMP_DIR" ]] && die "--short-circuit requires preserved klp-tmp dir"
20624ebfcd6SJosh Poimboeuf				keep_tmp=1
20724ebfcd6SJosh Poimboeuf				case "$2" in
20824ebfcd6SJosh Poimboeuf					1 | orig)	SHORT_CIRCUIT=1; ;;
20924ebfcd6SJosh Poimboeuf					2 | patched)	SHORT_CIRCUIT=2; ;;
21024ebfcd6SJosh Poimboeuf					3 | diff)	SHORT_CIRCUIT=3; ;;
21124ebfcd6SJosh Poimboeuf					4 | mod)	SHORT_CIRCUIT=4; ;;
21224ebfcd6SJosh Poimboeuf					*)		die "invalid short-circuit step '$2'" ;;
21324ebfcd6SJosh Poimboeuf				esac
21424ebfcd6SJosh Poimboeuf				shift 2
21524ebfcd6SJosh Poimboeuf				;;
21624ebfcd6SJosh Poimboeuf			-T | --keep-tmp)
21724ebfcd6SJosh Poimboeuf				keep_tmp=1
21824ebfcd6SJosh Poimboeuf				shift
21924ebfcd6SJosh Poimboeuf				;;
22024ebfcd6SJosh Poimboeuf			--)
22124ebfcd6SJosh Poimboeuf				shift
22224ebfcd6SJosh Poimboeuf				break
22324ebfcd6SJosh Poimboeuf				;;
22424ebfcd6SJosh Poimboeuf			*)
22524ebfcd6SJosh Poimboeuf				usage
22624ebfcd6SJosh Poimboeuf				exit 1
22724ebfcd6SJosh Poimboeuf				;;
22824ebfcd6SJosh Poimboeuf		esac
22924ebfcd6SJosh Poimboeuf	done
23024ebfcd6SJosh Poimboeuf
231e506ad21SJoe Lawrence	if [[ $# -eq 0 ]] && (( SHORT_CIRCUIT <= 2 )); then
23224ebfcd6SJosh Poimboeuf		usage
23324ebfcd6SJosh Poimboeuf		exit 1
23424ebfcd6SJosh Poimboeuf	fi
23524ebfcd6SJosh Poimboeuf
23624ebfcd6SJosh Poimboeuf	KEEP_TMP="$keep_tmp"
23724ebfcd6SJosh Poimboeuf	PATCHES=("$@")
238b3ece301SJosh Poimboeuf
239b3ece301SJosh Poimboeuf	for patch in "${PATCHES[@]}"; do
240b3ece301SJosh Poimboeuf		[[ -f "$patch" ]] || die "$patch doesn't exist"
241b3ece301SJosh Poimboeuf	done
24224ebfcd6SJosh Poimboeuf}
24324ebfcd6SJosh Poimboeuf
24424ebfcd6SJosh Poimboeuf# temporarily disable xtrace for especially verbose code
24524ebfcd6SJosh Poimboeufxtrace_save() {
24624ebfcd6SJosh Poimboeuf	[[ -v XTRACE ]] && set +x
24724ebfcd6SJosh Poimboeuf	return 0
24824ebfcd6SJosh Poimboeuf}
24924ebfcd6SJosh Poimboeuf
25024ebfcd6SJosh Poimboeufxtrace_restore() {
25124ebfcd6SJosh Poimboeuf	[[ -v XTRACE ]] && set -x
25224ebfcd6SJosh Poimboeuf	return 0
25324ebfcd6SJosh Poimboeuf}
25424ebfcd6SJosh Poimboeuf
25524ebfcd6SJosh Poimboeufvalidate_config() {
25624ebfcd6SJosh Poimboeuf	xtrace_save "reading .config"
25724ebfcd6SJosh Poimboeuf	source "$CONFIG" || die "no .config file in $(dirname "$CONFIG")"
25824ebfcd6SJosh Poimboeuf	xtrace_restore
25924ebfcd6SJosh Poimboeuf
26024ebfcd6SJosh Poimboeuf	[[ -v CONFIG_LIVEPATCH ]] ||			\
26124ebfcd6SJosh Poimboeuf		die "CONFIG_LIVEPATCH not enabled"
26224ebfcd6SJosh Poimboeuf
26324ebfcd6SJosh Poimboeuf	[[ -v CONFIG_KLP_BUILD ]] ||			\
26424ebfcd6SJosh Poimboeuf		die "CONFIG_KLP_BUILD not enabled"
26524ebfcd6SJosh Poimboeuf
26624ebfcd6SJosh Poimboeuf	[[ -v CONFIG_GCC_PLUGIN_LATENT_ENTROPY ]] &&	\
26724ebfcd6SJosh Poimboeuf		die "kernel option 'CONFIG_GCC_PLUGIN_LATENT_ENTROPY' not supported"
26824ebfcd6SJosh Poimboeuf
26924ebfcd6SJosh Poimboeuf	[[ -v CONFIG_GCC_PLUGIN_RANDSTRUCT ]] &&	\
27024ebfcd6SJosh Poimboeuf		die "kernel option 'CONFIG_GCC_PLUGIN_RANDSTRUCT' not supported"
27124ebfcd6SJosh Poimboeuf
272a8ff29f0SJosh Poimboeuf	[[ -v CONFIG_AS_IS_LLVM ]] &&				\
273a8ff29f0SJosh Poimboeuf		[[ "$CONFIG_AS_VERSION" -lt 200000 ]] &&	\
274a8ff29f0SJosh Poimboeuf		die "Clang assembler version < 20 not supported"
275a8ff29f0SJosh Poimboeuf
27624ebfcd6SJosh Poimboeuf	return 0
27724ebfcd6SJosh Poimboeuf}
27824ebfcd6SJosh Poimboeuf
27924ebfcd6SJosh Poimboeuf# Only allow alphanumerics and '_' and '-' in the module name.  Everything else
28024ebfcd6SJosh Poimboeuf# is replaced with '-'.  Also truncate to 55 chars so the full name + NUL
28124ebfcd6SJosh Poimboeuf# terminator fits in the kernel's 56-byte module name array.
28224ebfcd6SJosh Poimboeufmodule_name_string() {
28324ebfcd6SJosh Poimboeuf	echo "${1//[^a-zA-Z0-9_-]/-}" | cut -c 1-55
28424ebfcd6SJosh Poimboeuf}
28524ebfcd6SJosh Poimboeuf
28624ebfcd6SJosh Poimboeuf# If the module name wasn't specified on the cmdline with --output, give it a
28724ebfcd6SJosh Poimboeuf# name based on the patch name.
28824ebfcd6SJosh Poimboeufset_module_name() {
28924ebfcd6SJosh Poimboeuf	[[ -v NAME ]] && return 0
29024ebfcd6SJosh Poimboeuf
29124ebfcd6SJosh Poimboeuf	if [[ "${#PATCHES[@]}" -eq 1 ]]; then
29224ebfcd6SJosh Poimboeuf		NAME="$(basename "${PATCHES[0]}")"
29324ebfcd6SJosh Poimboeuf		NAME="${NAME%.*}"
29424ebfcd6SJosh Poimboeuf	else
29524ebfcd6SJosh Poimboeuf		NAME="patch"
29624ebfcd6SJosh Poimboeuf	fi
29724ebfcd6SJosh Poimboeuf
29824ebfcd6SJosh Poimboeuf	NAME="livepatch-$NAME"
29924ebfcd6SJosh Poimboeuf	NAME="$(module_name_string "$NAME")"
30024ebfcd6SJosh Poimboeuf
30124ebfcd6SJosh Poimboeuf	OUTFILE="$NAME.ko"
30224ebfcd6SJosh Poimboeuf}
30324ebfcd6SJosh Poimboeuf
30424ebfcd6SJosh Poimboeuf# Hardcode the value printed by the localversion script to prevent patch
305d36a7343SJoe Lawrence# application from appending it with '+' due to a dirty working tree.
30624ebfcd6SJosh Poimboeufset_kernelversion() {
30724ebfcd6SJosh Poimboeuf	local file="$SRC/scripts/setlocalversion"
3086f93f7b0SJosh Poimboeuf	local kernelrelease
30924ebfcd6SJosh Poimboeuf
31024ebfcd6SJosh Poimboeuf	stash_file "$file"
31124ebfcd6SJosh Poimboeuf
312cc39ccceSJosh Poimboeuf	if [[ -n "$(make -s listnewconfig 2>/dev/null)" ]]; then
313cc39ccceSJosh Poimboeuf		die ".config mismatch, check your .config or run 'make olddefconfig'"
314cc39ccceSJosh Poimboeuf	fi
315cc39ccceSJosh Poimboeuf	make syncconfig &>/dev/null || die "make syncconfig failed"
316cc39ccceSJosh Poimboeuf
317cc39ccceSJosh Poimboeuf	kernelrelease="$(make -s kernelrelease)"
3186f93f7b0SJosh Poimboeuf	[[ -z "$kernelrelease" ]] && die "failed to get kernel version"
31924ebfcd6SJosh Poimboeuf
3206f93f7b0SJosh Poimboeuf	sed -i "2i echo $kernelrelease; exit 0" scripts/setlocalversion
32124ebfcd6SJosh Poimboeuf}
32224ebfcd6SJosh Poimboeuf
323757bd10fSJoe Lawrenceget_patch_input_files() {
32424ebfcd6SJosh Poimboeuf	local patch="$1"
32524ebfcd6SJosh Poimboeuf
326757bd10fSJoe Lawrence	grep0 -E '^--- ' "$patch"				\
327d36a7343SJoe Lawrence		| grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \
32824ebfcd6SJosh Poimboeuf		| gawk '{print $2}'				\
32924ebfcd6SJosh Poimboeuf		| sed 's|^[^/]*/||'				\
33024ebfcd6SJosh Poimboeuf		| sort -u
33124ebfcd6SJosh Poimboeuf}
33224ebfcd6SJosh Poimboeuf
333757bd10fSJoe Lawrenceget_patch_output_files() {
33424ebfcd6SJosh Poimboeuf	local patch="$1"
33524ebfcd6SJosh Poimboeuf
336757bd10fSJoe Lawrence	grep0 -E '^\+\+\+ ' "$patch"				\
337d36a7343SJoe Lawrence		| grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \
338757bd10fSJoe Lawrence		| gawk '{print $2}'				\
339757bd10fSJoe Lawrence		| sed 's|^[^/]*/||'				\
340757bd10fSJoe Lawrence		| sort -u
341757bd10fSJoe Lawrence}
34224ebfcd6SJosh Poimboeuf
34324ebfcd6SJosh Poimboeufget_patch_files() {
34424ebfcd6SJosh Poimboeuf	local patch="$1"
34524ebfcd6SJosh Poimboeuf
346757bd10fSJoe Lawrence	{ get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \
34724ebfcd6SJosh Poimboeuf		| sort -u
34824ebfcd6SJosh Poimboeuf}
34924ebfcd6SJosh Poimboeuf
35024ebfcd6SJosh Poimboeufcheck_unsupported_patches() {
35124ebfcd6SJosh Poimboeuf	local patch
35224ebfcd6SJosh Poimboeuf
35324ebfcd6SJosh Poimboeuf	for patch in "${PATCHES[@]}"; do
35424ebfcd6SJosh Poimboeuf		local files=()
35524ebfcd6SJosh Poimboeuf
35624ebfcd6SJosh Poimboeuf		get_patch_files "$patch" | mapfile -t files
35724ebfcd6SJosh Poimboeuf
35824ebfcd6SJosh Poimboeuf		for file in "${files[@]}"; do
35924ebfcd6SJosh Poimboeuf			case "$file" in
360*df0d7bb0SJosh Poimboeuf				lib/*|*/vdso/*|*/realmode/rm/*|*.S)
361b41d8b7dSJoe Lawrence					die "${patch}: unsupported patch to $file"
36224ebfcd6SJosh Poimboeuf					;;
36324ebfcd6SJosh Poimboeuf			esac
36424ebfcd6SJosh Poimboeuf		done
36524ebfcd6SJosh Poimboeuf	done
36624ebfcd6SJosh Poimboeuf}
36724ebfcd6SJosh Poimboeuf
36824ebfcd6SJosh Poimboeufapply_patch() {
36924ebfcd6SJosh Poimboeuf	local patch="$1"
37024ebfcd6SJosh Poimboeuf	shift
37124ebfcd6SJosh Poimboeuf	local extra_args=("$@")
37251a0b7c4SJoe Lawrence	local drift_regex="with fuzz|offset [0-9]+ line"
37351a0b7c4SJoe Lawrence	local output
37451a0b7c4SJoe Lawrence	local status
37524ebfcd6SJosh Poimboeuf
37624ebfcd6SJosh Poimboeuf	[[ ! -f "$patch" ]] && die "$patch doesn't exist"
37751a0b7c4SJoe Lawrence	status=0
37851a0b7c4SJoe Lawrence	output=$(patch -d "$SRC" -p1 --dry-run --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" < "$patch" 2>&1) || status=$?
37951a0b7c4SJoe Lawrence	if [[ "$status" -ne 0 ]]; then
38051a0b7c4SJoe Lawrence		echo "$output" >&2
38151a0b7c4SJoe Lawrence		die "$patch did not apply"
38251a0b7c4SJoe Lawrence	elif [[ "$output" =~ $drift_regex ]]; then
38396524543SJosh Poimboeuf		[[ -v VERBOSE ]] && echo "$output" >&2
38451a0b7c4SJoe Lawrence		warn "${patch} applied with fuzz"
38551a0b7c4SJoe Lawrence	fi
38624ebfcd6SJosh Poimboeuf
38724ebfcd6SJosh Poimboeuf	APPLIED_PATCHES+=("$patch")
388f3048888SJosh Poimboeuf	patch -d "$SRC" -p1 --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" --silent < "$patch"
38924ebfcd6SJosh Poimboeuf}
39024ebfcd6SJosh Poimboeuf
39124ebfcd6SJosh Poimboeufrevert_patch() {
39224ebfcd6SJosh Poimboeuf	local patch="$1"
39324ebfcd6SJosh Poimboeuf	local tmp=()
39424ebfcd6SJosh Poimboeuf
395f3048888SJosh Poimboeuf	patch -d "$SRC" -p1 -R --force --no-backup-if-mismatch -r /dev/null &> /dev/null < "$patch" || true
39624ebfcd6SJosh Poimboeuf
39724ebfcd6SJosh Poimboeuf	for p in "${APPLIED_PATCHES[@]}"; do
39824ebfcd6SJosh Poimboeuf		[[ "$p" == "$patch" ]] && continue
39924ebfcd6SJosh Poimboeuf		tmp+=("$p")
40024ebfcd6SJosh Poimboeuf	done
40124ebfcd6SJosh Poimboeuf
40224ebfcd6SJosh Poimboeuf	APPLIED_PATCHES=("${tmp[@]}")
40324ebfcd6SJosh Poimboeuf}
40424ebfcd6SJosh Poimboeuf
40524ebfcd6SJosh Poimboeufapply_patches() {
40651a0b7c4SJoe Lawrence	local extra_args=("$@")
40724ebfcd6SJosh Poimboeuf	local patch
40824ebfcd6SJosh Poimboeuf
40924ebfcd6SJosh Poimboeuf	for patch in "${PATCHES[@]}"; do
41051a0b7c4SJoe Lawrence		apply_patch "$patch" "${extra_args[@]}"
41124ebfcd6SJosh Poimboeuf	done
41224ebfcd6SJosh Poimboeuf}
41324ebfcd6SJosh Poimboeuf
41424ebfcd6SJosh Poimboeufrevert_patches() {
41524ebfcd6SJosh Poimboeuf	local patches=("${APPLIED_PATCHES[@]}")
41624ebfcd6SJosh Poimboeuf
41724ebfcd6SJosh Poimboeuf	for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do
418d36a7343SJoe Lawrence		revert_patch "${patches[$i]}"
41924ebfcd6SJosh Poimboeuf	done
42024ebfcd6SJosh Poimboeuf
42124ebfcd6SJosh Poimboeuf	APPLIED_PATCHES=()
42224ebfcd6SJosh Poimboeuf}
42324ebfcd6SJosh Poimboeuf
42424ebfcd6SJosh Poimboeufvalidate_patches() {
42524ebfcd6SJosh Poimboeuf	check_unsupported_patches
42624ebfcd6SJosh Poimboeuf	apply_patches
42724ebfcd6SJosh Poimboeuf	revert_patches
42824ebfcd6SJosh Poimboeuf}
42924ebfcd6SJosh Poimboeuf
43024ebfcd6SJosh Poimboeufdo_init() {
43124ebfcd6SJosh Poimboeuf	# We're not yet smart enough to handle anything other than in-tree
43224ebfcd6SJosh Poimboeuf	# builds in pwd.
43324ebfcd6SJosh Poimboeuf	[[ ! "$SRC" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory"
43424ebfcd6SJosh Poimboeuf	[[ ! "$OBJ" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory"
43524ebfcd6SJosh Poimboeuf
43624ebfcd6SJosh Poimboeuf	(( SHORT_CIRCUIT <= 1 )) && rm -rf "$TMP_DIR"
43724ebfcd6SJosh Poimboeuf	mkdir -p "$TMP_DIR"
43824ebfcd6SJosh Poimboeuf
43924ebfcd6SJosh Poimboeuf	APPLIED_PATCHES=()
44024ebfcd6SJosh Poimboeuf
44124ebfcd6SJosh Poimboeuf	[[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines"
442d36a7343SJoe Lawrence	command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)"
44324ebfcd6SJosh Poimboeuf
44424ebfcd6SJosh Poimboeuf	validate_config
44524ebfcd6SJosh Poimboeuf	set_module_name
44624ebfcd6SJosh Poimboeuf	set_kernelversion
44724ebfcd6SJosh Poimboeuf}
44824ebfcd6SJosh Poimboeuf
44924ebfcd6SJosh Poimboeuf# Refresh the patch hunk headers, specifically the line numbers and counts.
45024ebfcd6SJosh Poimboeufrefresh_patch() {
45124ebfcd6SJosh Poimboeuf	local patch="$1"
45224ebfcd6SJosh Poimboeuf	local tmpdir="$PATCH_TMP_DIR"
453757bd10fSJoe Lawrence	local input_files=()
454757bd10fSJoe Lawrence	local output_files=()
45524ebfcd6SJosh Poimboeuf
45624ebfcd6SJosh Poimboeuf	rm -rf "$tmpdir"
45724ebfcd6SJosh Poimboeuf	mkdir -p "$tmpdir/a"
45824ebfcd6SJosh Poimboeuf	mkdir -p "$tmpdir/b"
45924ebfcd6SJosh Poimboeuf
46024ebfcd6SJosh Poimboeuf	# Get all source files affected by the patch
461757bd10fSJoe Lawrence	get_patch_input_files "$patch" | mapfile -t input_files
462757bd10fSJoe Lawrence	get_patch_output_files "$patch" | mapfile -t output_files
46324ebfcd6SJosh Poimboeuf
46424ebfcd6SJosh Poimboeuf	# Copy orig source files to 'a'
465757bd10fSJoe Lawrence	( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" )
46624ebfcd6SJosh Poimboeuf
46724ebfcd6SJosh Poimboeuf	# Copy patched source files to 'b'
46851a0b7c4SJoe Lawrence	apply_patch "$patch" "--silent"
469757bd10fSJoe Lawrence	( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" )
470d36a7343SJoe Lawrence	revert_patch "$patch"
47124ebfcd6SJosh Poimboeuf
47224ebfcd6SJosh Poimboeuf	# Diff 'a' and 'b' to make a clean patch
473d36a7343SJoe Lawrence	( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true
47424ebfcd6SJosh Poimboeuf}
47524ebfcd6SJosh Poimboeuf
47624ebfcd6SJosh Poimboeuf# Copy the patches to a temporary directory, fix their lines so as not to
47724ebfcd6SJosh Poimboeuf# affect the __LINE__ macro for otherwise unchanged functions further down the
47824ebfcd6SJosh Poimboeuf# file, and update $PATCHES to point to the fixed patches.
47924ebfcd6SJosh Poimboeuffix_patches() {
48024ebfcd6SJosh Poimboeuf	local idx
48124ebfcd6SJosh Poimboeuf	local i
48224ebfcd6SJosh Poimboeuf
48324ebfcd6SJosh Poimboeuf	rm -f "$TMP_DIR"/*.patch
48424ebfcd6SJosh Poimboeuf
48524ebfcd6SJosh Poimboeuf	idx=0001
48624ebfcd6SJosh Poimboeuf	for i in "${!PATCHES[@]}"; do
48724ebfcd6SJosh Poimboeuf		local old_patch="${PATCHES[$i]}"
48824ebfcd6SJosh Poimboeuf		local tmp_patch="$TMP_DIR/tmp.patch"
48924ebfcd6SJosh Poimboeuf		local patch="${PATCHES[$i]}"
49024ebfcd6SJosh Poimboeuf		local new_patch
49124ebfcd6SJosh Poimboeuf
49224ebfcd6SJosh Poimboeuf		new_patch="$TMP_DIR/$idx-fixed-$(basename "$patch")"
49324ebfcd6SJosh Poimboeuf
49424ebfcd6SJosh Poimboeuf		cp -f "$old_patch" "$tmp_patch"
49524ebfcd6SJosh Poimboeuf		refresh_patch "$tmp_patch"
496d36a7343SJoe Lawrence		"$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch"
49724ebfcd6SJosh Poimboeuf
49824ebfcd6SJosh Poimboeuf		PATCHES[i]="$new_patch"
49924ebfcd6SJosh Poimboeuf
50024ebfcd6SJosh Poimboeuf		rm -f "$tmp_patch"
50124ebfcd6SJosh Poimboeuf		idx=$(printf "%04d" $(( 10#$idx + 1 )))
50224ebfcd6SJosh Poimboeuf	done
50324ebfcd6SJosh Poimboeuf}
50424ebfcd6SJosh Poimboeuf
50524ebfcd6SJosh Poimboeufclean_kernel() {
50624ebfcd6SJosh Poimboeuf	local cmd=()
50724ebfcd6SJosh Poimboeuf
50824ebfcd6SJosh Poimboeuf	cmd=("make")
50924ebfcd6SJosh Poimboeuf	cmd+=("--silent")
51024ebfcd6SJosh Poimboeuf	cmd+=("-j$JOBS")
51124ebfcd6SJosh Poimboeuf	cmd+=("clean")
51224ebfcd6SJosh Poimboeuf
51324ebfcd6SJosh Poimboeuf	(
51424ebfcd6SJosh Poimboeuf		cd "$SRC"
51524ebfcd6SJosh Poimboeuf		"${cmd[@]}"
51624ebfcd6SJosh Poimboeuf	)
51724ebfcd6SJosh Poimboeuf}
51824ebfcd6SJosh Poimboeuf
51924ebfcd6SJosh Poimboeufbuild_kernel() {
520b41d8b7dSJoe Lawrence	local build="$1"
52124ebfcd6SJosh Poimboeuf	local log="$TMP_DIR/build.log"
5222092007aSJosh Poimboeuf	local objtool_args=()
52324ebfcd6SJosh Poimboeuf	local cmd=()
52424ebfcd6SJosh Poimboeuf
5252092007aSJosh Poimboeuf	objtool_args=("--checksum")
5262092007aSJosh Poimboeuf
52724ebfcd6SJosh Poimboeuf	cmd=("make")
52824ebfcd6SJosh Poimboeuf
52924ebfcd6SJosh Poimboeuf	# When a patch to a kernel module references a newly created unexported
53024ebfcd6SJosh Poimboeuf	# symbol which lives in vmlinux or another kernel module, the patched
53124ebfcd6SJosh Poimboeuf	# kernel build fails with the following error:
53224ebfcd6SJosh Poimboeuf	#
53324ebfcd6SJosh Poimboeuf	#   ERROR: modpost: "klp_string" [fs/xfs/xfs.ko] undefined!
53424ebfcd6SJosh Poimboeuf	#
53524ebfcd6SJosh Poimboeuf	# The undefined symbols are working as designed in that case.  They get
53624ebfcd6SJosh Poimboeuf	# resolved later when the livepatch module build link pulls all the
53724ebfcd6SJosh Poimboeuf	# disparate objects together into the same kernel module.
53824ebfcd6SJosh Poimboeuf	#
53924ebfcd6SJosh Poimboeuf	# It would be good to have a way to tell modpost to skip checking for
54024ebfcd6SJosh Poimboeuf	# undefined symbols altogether.  For now, just convert the error to a
54124ebfcd6SJosh Poimboeuf	# warning with KBUILD_MODPOST_WARN, and grep out the warning to avoid
54224ebfcd6SJosh Poimboeuf	# confusing the user.
54324ebfcd6SJosh Poimboeuf	#
54424ebfcd6SJosh Poimboeuf	cmd+=("KBUILD_MODPOST_WARN=1")
54524ebfcd6SJosh Poimboeuf
54696524543SJosh Poimboeuf	if [[ -v VERBOSE ]]; then
54796524543SJosh Poimboeuf		cmd+=("V=1")
54896524543SJosh Poimboeuf	else
54996524543SJosh Poimboeuf		cmd+=("-s")
55096524543SJosh Poimboeuf	fi
55124ebfcd6SJosh Poimboeuf	cmd+=("-j$JOBS")
55224ebfcd6SJosh Poimboeuf	cmd+=("KCFLAGS=-ffunction-sections -fdata-sections")
5532092007aSJosh Poimboeuf	cmd+=("OBJTOOL_ARGS=${objtool_args[*]}")
55424ebfcd6SJosh Poimboeuf	cmd+=("vmlinux")
55524ebfcd6SJosh Poimboeuf	cmd+=("modules")
55624ebfcd6SJosh Poimboeuf
55724ebfcd6SJosh Poimboeuf	(
55824ebfcd6SJosh Poimboeuf		cd "$SRC"
55924ebfcd6SJosh Poimboeuf		"${cmd[@]}"							\
56024ebfcd6SJosh Poimboeuf			1> >(tee -a "$log")					\
56124ebfcd6SJosh Poimboeuf			2> >(tee -a "$log" | grep0 -v "modpost.*undefined!" >&2)
562b41d8b7dSJoe Lawrence	) || die "$build kernel build failed"
56324ebfcd6SJosh Poimboeuf}
56424ebfcd6SJosh Poimboeuf
56524ebfcd6SJosh Poimboeuffind_objects() {
56624ebfcd6SJosh Poimboeuf	local opts=("$@")
56724ebfcd6SJosh Poimboeuf
56824ebfcd6SJosh Poimboeuf	# Find root-level vmlinux.o and non-root-level .ko files,
56924ebfcd6SJosh Poimboeuf	# excluding klp-tmp/ and .git/
57024ebfcd6SJosh Poimboeuf	find "$OBJ" \( -path "$TMP_DIR" -o -path "$OBJ/.git" -o	-regex "$OBJ/[^/][^/]*\.ko" \) -prune -o \
57124ebfcd6SJosh Poimboeuf		    -type f "${opts[@]}"				\
57224ebfcd6SJosh Poimboeuf		    \( -name "*.ko" -o -path "$OBJ/vmlinux.o" \)	\
57324ebfcd6SJosh Poimboeuf		    -printf '%P\n'
57424ebfcd6SJosh Poimboeuf}
57524ebfcd6SJosh Poimboeuf
57624ebfcd6SJosh Poimboeuf# Copy all .o archives to $ORIG_DIR
57724ebfcd6SJosh Poimboeufcopy_orig_objects() {
57824ebfcd6SJosh Poimboeuf	local files=()
57924ebfcd6SJosh Poimboeuf
58024ebfcd6SJosh Poimboeuf	rm -rf "$ORIG_DIR"
58124ebfcd6SJosh Poimboeuf	mkdir -p "$ORIG_DIR"
58224ebfcd6SJosh Poimboeuf
58324ebfcd6SJosh Poimboeuf	find_objects | mapfile -t files
58424ebfcd6SJosh Poimboeuf
58524ebfcd6SJosh Poimboeuf	xtrace_save "copying orig objects"
58624ebfcd6SJosh Poimboeuf	for _file in "${files[@]}"; do
58724ebfcd6SJosh Poimboeuf		local rel_file="${_file/.ko/.o}"
58824ebfcd6SJosh Poimboeuf		local file="$OBJ/$rel_file"
58924ebfcd6SJosh Poimboeuf		local orig_file="$ORIG_DIR/$rel_file"
59024ebfcd6SJosh Poimboeuf		local orig_dir="$(dirname "$orig_file")"
59124ebfcd6SJosh Poimboeuf
59224ebfcd6SJosh Poimboeuf		[[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file"
59324ebfcd6SJosh Poimboeuf
59424ebfcd6SJosh Poimboeuf		mkdir -p "$orig_dir"
59524ebfcd6SJosh Poimboeuf		cp -f "$file" "$orig_dir"
59624ebfcd6SJosh Poimboeuf	done
59724ebfcd6SJosh Poimboeuf	xtrace_restore
59824ebfcd6SJosh Poimboeuf
59924ebfcd6SJosh Poimboeuf	mv -f "$TMP_DIR/build.log" "$ORIG_DIR"
60024ebfcd6SJosh Poimboeuf	touch "$TIMESTAMP"
60124ebfcd6SJosh Poimboeuf}
60224ebfcd6SJosh Poimboeuf
60324ebfcd6SJosh Poimboeuf# Copy all changed objects to $PATCHED_DIR
60424ebfcd6SJosh Poimboeufcopy_patched_objects() {
60524ebfcd6SJosh Poimboeuf	local files=()
60624ebfcd6SJosh Poimboeuf	local opts=()
60724ebfcd6SJosh Poimboeuf	local found=0
60824ebfcd6SJosh Poimboeuf
60924ebfcd6SJosh Poimboeuf	rm -rf "$PATCHED_DIR"
61024ebfcd6SJosh Poimboeuf	mkdir -p "$PATCHED_DIR"
61124ebfcd6SJosh Poimboeuf
61224ebfcd6SJosh Poimboeuf	# Note this doesn't work with some configs, thus the 'cmp' below.
61324ebfcd6SJosh Poimboeuf	opts=("-newer")
61424ebfcd6SJosh Poimboeuf	opts+=("$TIMESTAMP")
61524ebfcd6SJosh Poimboeuf
61624ebfcd6SJosh Poimboeuf	find_objects "${opts[@]}" | mapfile -t files
61724ebfcd6SJosh Poimboeuf
61824ebfcd6SJosh Poimboeuf	xtrace_save "copying changed objects"
61924ebfcd6SJosh Poimboeuf	for _file in "${files[@]}"; do
62024ebfcd6SJosh Poimboeuf		local rel_file="${_file/.ko/.o}"
62124ebfcd6SJosh Poimboeuf		local file="$OBJ/$rel_file"
62224ebfcd6SJosh Poimboeuf		local orig_file="$ORIG_DIR/$rel_file"
62324ebfcd6SJosh Poimboeuf		local patched_file="$PATCHED_DIR/$rel_file"
62424ebfcd6SJosh Poimboeuf		local patched_dir="$(dirname "$patched_file")"
62524ebfcd6SJosh Poimboeuf
62624ebfcd6SJosh Poimboeuf		[[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file"
62724ebfcd6SJosh Poimboeuf
62824ebfcd6SJosh Poimboeuf		cmp -s "$orig_file" "$file" && continue
62924ebfcd6SJosh Poimboeuf
63024ebfcd6SJosh Poimboeuf		mkdir -p "$patched_dir"
63124ebfcd6SJosh Poimboeuf		cp -f "$file" "$patched_dir"
63224ebfcd6SJosh Poimboeuf		found=1
63324ebfcd6SJosh Poimboeuf	done
63424ebfcd6SJosh Poimboeuf	xtrace_restore
63524ebfcd6SJosh Poimboeuf
63624ebfcd6SJosh Poimboeuf	(( found == 0 )) && die "no changes detected"
63724ebfcd6SJosh Poimboeuf
63824ebfcd6SJosh Poimboeuf	mv -f "$TMP_DIR/build.log" "$PATCHED_DIR"
63924ebfcd6SJosh Poimboeuf}
64024ebfcd6SJosh Poimboeuf
64124ebfcd6SJosh Poimboeuf# Diff changed objects, writing output object to $DIFF_DIR
64224ebfcd6SJosh Poimboeufdiff_objects() {
64324ebfcd6SJosh Poimboeuf	local log="$KLP_DIFF_LOG"
64424ebfcd6SJosh Poimboeuf	local files=()
6452c2f0b86SJosh Poimboeuf	local opts=()
64624ebfcd6SJosh Poimboeuf
64724ebfcd6SJosh Poimboeuf	rm -rf "$DIFF_DIR"
64824ebfcd6SJosh Poimboeuf	mkdir -p "$DIFF_DIR"
64924ebfcd6SJosh Poimboeuf
65024ebfcd6SJosh Poimboeuf	find "$PATCHED_DIR" -type f -name "*.o" | mapfile -t files
65124ebfcd6SJosh Poimboeuf	[[ ${#files[@]} -eq 0 ]] && die "no changes detected"
65224ebfcd6SJosh Poimboeuf
6532c2f0b86SJosh Poimboeuf	[[ -v DEBUG_CLONE ]] && opts=("--debug")
6542c2f0b86SJosh Poimboeuf
65524ebfcd6SJosh Poimboeuf	# Diff all changed objects
65624ebfcd6SJosh Poimboeuf	for file in "${files[@]}"; do
65724ebfcd6SJosh Poimboeuf		local rel_file="${file#"$PATCHED_DIR"/}"
65824ebfcd6SJosh Poimboeuf		local orig_file="$rel_file"
65924ebfcd6SJosh Poimboeuf		local patched_file="$PATCHED_DIR/$rel_file"
66024ebfcd6SJosh Poimboeuf		local out_file="$DIFF_DIR/$rel_file"
66178be9facSJosh Poimboeuf		local filter=()
66224ebfcd6SJosh Poimboeuf		local cmd=()
66324ebfcd6SJosh Poimboeuf
66424ebfcd6SJosh Poimboeuf		mkdir -p "$(dirname "$out_file")"
66524ebfcd6SJosh Poimboeuf
66624ebfcd6SJosh Poimboeuf		cmd=("$SRC/tools/objtool/objtool")
66724ebfcd6SJosh Poimboeuf		cmd+=("klp")
66824ebfcd6SJosh Poimboeuf		cmd+=("diff")
6692c2f0b86SJosh Poimboeuf		(( ${#opts[@]} > 0 )) && cmd+=("${opts[@]}")
67024ebfcd6SJosh Poimboeuf		cmd+=("$orig_file")
67124ebfcd6SJosh Poimboeuf		cmd+=("$patched_file")
67224ebfcd6SJosh Poimboeuf		cmd+=("$out_file")
67324ebfcd6SJosh Poimboeuf
67478be9facSJosh Poimboeuf		if [[ -v DIFF_CHECKSUM ]]; then
67578be9facSJosh Poimboeuf			filter=("grep0")
67678be9facSJosh Poimboeuf			filter+=("-Ev")
67778be9facSJosh Poimboeuf			filter+=("DEBUG: .*checksum: ")
67878be9facSJosh Poimboeuf		else
67978be9facSJosh Poimboeuf			filter=("cat")
68078be9facSJosh Poimboeuf		fi
68178be9facSJosh Poimboeuf
68224ebfcd6SJosh Poimboeuf		(
68324ebfcd6SJosh Poimboeuf			cd "$ORIG_DIR"
68424ebfcd6SJosh Poimboeuf			"${cmd[@]}"							\
68524ebfcd6SJosh Poimboeuf				1> >(tee -a "$log")					\
68678be9facSJosh Poimboeuf				2> >(tee -a "$log" | "${filter[@]}" >&2) ||		\
68724ebfcd6SJosh Poimboeuf				die "objtool klp diff failed"
68824ebfcd6SJosh Poimboeuf		)
68924ebfcd6SJosh Poimboeuf	done
69024ebfcd6SJosh Poimboeuf}
69124ebfcd6SJosh Poimboeuf
69278be9facSJosh Poimboeuf# For each changed object, run objtool with --debug-checksum to get the
69378be9facSJosh Poimboeuf# per-instruction checksums, and then diff those to find the first changed
69478be9facSJosh Poimboeuf# instruction for each function.
69578be9facSJosh Poimboeufdiff_checksums() {
69678be9facSJosh Poimboeuf	local orig_log="$ORIG_DIR/checksum.log"
69778be9facSJosh Poimboeuf	local patched_log="$PATCHED_DIR/checksum.log"
69878be9facSJosh Poimboeuf	local -A funcs
69978be9facSJosh Poimboeuf	local cmd=()
70078be9facSJosh Poimboeuf	local line
70178be9facSJosh Poimboeuf	local file
70278be9facSJosh Poimboeuf	local func
70378be9facSJosh Poimboeuf
70478be9facSJosh Poimboeuf	gawk '/\.o: changed function: / {
70578be9facSJosh Poimboeuf		sub(/:$/, "", $1)
70678be9facSJosh Poimboeuf		print $1, $NF
70778be9facSJosh Poimboeuf	}' "$KLP_DIFF_LOG" | mapfile -t lines
70878be9facSJosh Poimboeuf
70978be9facSJosh Poimboeuf	for line in "${lines[@]}"; do
71078be9facSJosh Poimboeuf		read -r file func <<< "$line"
71178be9facSJosh Poimboeuf		if [[ ! -v funcs["$file"] ]]; then
71278be9facSJosh Poimboeuf			funcs["$file"]="$func"
71378be9facSJosh Poimboeuf		else
71478be9facSJosh Poimboeuf			funcs["$file"]+=" $func"
71578be9facSJosh Poimboeuf		fi
71678be9facSJosh Poimboeuf	done
71778be9facSJosh Poimboeuf
71878be9facSJosh Poimboeuf	cmd=("$SRC/tools/objtool/objtool")
71978be9facSJosh Poimboeuf	cmd+=("--checksum")
72078be9facSJosh Poimboeuf	cmd+=("--link")
72178be9facSJosh Poimboeuf	cmd+=("--dry-run")
72278be9facSJosh Poimboeuf
72378be9facSJosh Poimboeuf	for file in "${!funcs[@]}"; do
72478be9facSJosh Poimboeuf		local opt="--debug-checksum=${funcs[$file]// /,}"
72578be9facSJosh Poimboeuf
72678be9facSJosh Poimboeuf		(
72778be9facSJosh Poimboeuf			cd "$ORIG_DIR"
72878be9facSJosh Poimboeuf			"${cmd[@]}" "$opt" "$file" &> "$orig_log" || \
72978be9facSJosh Poimboeuf				( cat "$orig_log" >&2; die "objtool --debug-checksum failed" )
73078be9facSJosh Poimboeuf
73178be9facSJosh Poimboeuf			cd "$PATCHED_DIR"
73278be9facSJosh Poimboeuf			"${cmd[@]}" "$opt" "$file" &> "$patched_log" ||	\
73378be9facSJosh Poimboeuf				( cat "$patched_log" >&2; die "objtool --debug-checksum failed" )
73478be9facSJosh Poimboeuf		)
73578be9facSJosh Poimboeuf
73678be9facSJosh Poimboeuf		for func in ${funcs[$file]}; do
737ba77fe55SJosh Poimboeuf			local -a orig patched
738ba77fe55SJosh Poimboeuf			paste <(grep0 -E "^DEBUG: .*checksum: $func " "$orig_log") \
739ba77fe55SJosh Poimboeuf			      <(grep0 -E "^DEBUG: .*checksum: $func " "$patched_log") |
740ba77fe55SJosh Poimboeuf			while IFS= read -r line; do
741ba77fe55SJosh Poimboeuf				read -ra orig <<< "${line%%$'\t'*}"
742ba77fe55SJosh Poimboeuf				read -ra patched <<< "${line#*$'\t'}"
743ba77fe55SJosh Poimboeuf
744ba77fe55SJosh Poimboeuf				if [[ ${#patched[@]} -eq 0 ]]; then
745ba77fe55SJosh Poimboeuf					printf "%s: %s: %s (removed)\n" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}"
746ba77fe55SJosh Poimboeuf					break
747ba77fe55SJosh Poimboeuf				elif [[ ${#orig[@]} -eq 0 ]]; then
748ba77fe55SJosh Poimboeuf					printf "%s: %s: %s (added)\n" "${patched[1]%:}" "${patched[3]}" "${patched[-2]}"
749ba77fe55SJosh Poimboeuf					break
750ba77fe55SJosh Poimboeuf				fi
751ba77fe55SJosh Poimboeuf
752ba77fe55SJosh Poimboeuf				[[ "${orig[-1]}" == "${patched[-1]}" ]] && continue
753ba77fe55SJosh Poimboeuf
754ba77fe55SJosh Poimboeuf				printf "%s: %s: %s" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}"
755ba77fe55SJosh Poimboeuf				[[ "${orig[-2]}" != "${patched[-2]}" ]] && \
756ba77fe55SJosh Poimboeuf					printf " (patched: %s)" "${patched[-2]}"
757ba77fe55SJosh Poimboeuf				printf "\n"
758ba77fe55SJosh Poimboeuf				break
759ba77fe55SJosh Poimboeuf			done || true
76078be9facSJosh Poimboeuf		done
76178be9facSJosh Poimboeuf	done
76278be9facSJosh Poimboeuf}
76378be9facSJosh Poimboeuf
76424ebfcd6SJosh Poimboeuf# Build and post-process livepatch module in $KMOD_DIR
76524ebfcd6SJosh Poimboeufbuild_patch_module() {
76624ebfcd6SJosh Poimboeuf	local makefile="$KMOD_DIR/Kbuild"
76724ebfcd6SJosh Poimboeuf	local log="$KMOD_DIR/build.log"
76824ebfcd6SJosh Poimboeuf	local kmod_file
76924ebfcd6SJosh Poimboeuf	local cflags=()
77024ebfcd6SJosh Poimboeuf	local files=()
77124ebfcd6SJosh Poimboeuf	local cmd=()
77224ebfcd6SJosh Poimboeuf
77324ebfcd6SJosh Poimboeuf	rm -rf "$KMOD_DIR"
77424ebfcd6SJosh Poimboeuf	mkdir -p "$KMOD_DIR"
77524ebfcd6SJosh Poimboeuf
77624ebfcd6SJosh Poimboeuf	cp -f "$SRC/scripts/livepatch/init.c" "$KMOD_DIR"
77724ebfcd6SJosh Poimboeuf
77824ebfcd6SJosh Poimboeuf	echo "obj-m := $NAME.o" > "$makefile"
77924ebfcd6SJosh Poimboeuf	echo -n "$NAME-y := init.o" >> "$makefile"
78024ebfcd6SJosh Poimboeuf
78124ebfcd6SJosh Poimboeuf	find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files
78224ebfcd6SJosh Poimboeuf	[[ ${#files[@]} -eq 0 ]] && die "no changes detected"
78324ebfcd6SJosh Poimboeuf
78424ebfcd6SJosh Poimboeuf	for file in "${files[@]}"; do
78524ebfcd6SJosh Poimboeuf		local rel_file="${file#"$DIFF_DIR"/}"
78624ebfcd6SJosh Poimboeuf		local orig_file="$ORIG_DIR/$rel_file"
78724ebfcd6SJosh Poimboeuf		local orig_dir="$(dirname "$orig_file")"
78824ebfcd6SJosh Poimboeuf		local kmod_file="$KMOD_DIR/$rel_file"
78924ebfcd6SJosh Poimboeuf		local kmod_dir="$(dirname "$kmod_file")"
79078c268f3SJosh Poimboeuf		local cmd_file="$kmod_dir/.$(basename "$file").cmd"
79124ebfcd6SJosh Poimboeuf
79224ebfcd6SJosh Poimboeuf		mkdir -p "$kmod_dir"
79324ebfcd6SJosh Poimboeuf		cp -f "$file" "$kmod_dir"
79424ebfcd6SJosh Poimboeuf
79524ebfcd6SJosh Poimboeuf		# Tell kbuild this is a prebuilt object
79624ebfcd6SJosh Poimboeuf		cp -f "$file" "${kmod_file}_shipped"
79724ebfcd6SJosh Poimboeuf
79878c268f3SJosh Poimboeuf		# Make modpost happy
79978c268f3SJosh Poimboeuf		touch "$cmd_file"
80078c268f3SJosh Poimboeuf
80124ebfcd6SJosh Poimboeuf		echo -n " $rel_file" >> "$makefile"
80224ebfcd6SJosh Poimboeuf	done
80324ebfcd6SJosh Poimboeuf
80424ebfcd6SJosh Poimboeuf	echo >> "$makefile"
80524ebfcd6SJosh Poimboeuf
80624ebfcd6SJosh Poimboeuf	cflags=("-ffunction-sections")
80724ebfcd6SJosh Poimboeuf	cflags+=("-fdata-sections")
80824ebfcd6SJosh Poimboeuf	[[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE")
80924ebfcd6SJosh Poimboeuf
81024ebfcd6SJosh Poimboeuf	cmd=("make")
81196524543SJosh Poimboeuf	if [[ -v VERBOSE ]]; then
81296524543SJosh Poimboeuf		cmd+=("V=1")
81396524543SJosh Poimboeuf	else
81496524543SJosh Poimboeuf		cmd+=("-s")
81596524543SJosh Poimboeuf	fi
81624ebfcd6SJosh Poimboeuf	cmd+=("-j$JOBS")
81724ebfcd6SJosh Poimboeuf	cmd+=("--directory=.")
81824ebfcd6SJosh Poimboeuf	cmd+=("M=$KMOD_DIR")
81924ebfcd6SJosh Poimboeuf	cmd+=("KCFLAGS=${cflags[*]}")
82024ebfcd6SJosh Poimboeuf
82124ebfcd6SJosh Poimboeuf	# Build a "normal" kernel module with init.c and the diffed objects
82224ebfcd6SJosh Poimboeuf	(
82324ebfcd6SJosh Poimboeuf		cd "$SRC"
82424ebfcd6SJosh Poimboeuf		"${cmd[@]}"							\
82524ebfcd6SJosh Poimboeuf			1> >(tee -a "$log")					\
82624ebfcd6SJosh Poimboeuf			2> >(tee -a "$log" >&2)
82724ebfcd6SJosh Poimboeuf	)
82824ebfcd6SJosh Poimboeuf
82924ebfcd6SJosh Poimboeuf	kmod_file="$KMOD_DIR/$NAME.ko"
83024ebfcd6SJosh Poimboeuf
83124ebfcd6SJosh Poimboeuf	# Save off the intermediate binary for debugging
83224ebfcd6SJosh Poimboeuf	cp -f "$kmod_file" "$kmod_file.orig"
83324ebfcd6SJosh Poimboeuf
83424ebfcd6SJosh Poimboeuf	# Work around issue where slight .config change makes corrupt BTF
83524ebfcd6SJosh Poimboeuf	objcopy --remove-section=.BTF "$kmod_file"
83624ebfcd6SJosh Poimboeuf
83724ebfcd6SJosh Poimboeuf	# Fix (and work around) linker wreckage for klp syms / relocs
83824ebfcd6SJosh Poimboeuf	"$SRC/tools/objtool/objtool" klp post-link "$kmod_file" || die "objtool klp post-link failed"
83924ebfcd6SJosh Poimboeuf
84024ebfcd6SJosh Poimboeuf	cp -f "$kmod_file" "$OUTFILE"
84124ebfcd6SJosh Poimboeuf}
84224ebfcd6SJosh Poimboeuf
84324ebfcd6SJosh Poimboeuf
84424ebfcd6SJosh Poimboeuf################################################################################
84524ebfcd6SJosh Poimboeuf
84624ebfcd6SJosh Poimboeufprocess_args "$@"
84724ebfcd6SJosh Poimboeufdo_init
84824ebfcd6SJosh Poimboeuf
849e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 2 )); then
85024ebfcd6SJosh Poimboeuf	status "Validating patch(es)"
85124ebfcd6SJosh Poimboeuf	validate_patches
852e506ad21SJoe Lawrencefi
853e506ad21SJoe Lawrence
854e506ad21SJoe Lawrenceif (( SHORT_CIRCUIT <= 1 )); then
85524ebfcd6SJosh Poimboeuf	status "Building original kernel"
85624ebfcd6SJosh Poimboeuf	clean_kernel
857b41d8b7dSJoe Lawrence	build_kernel "original"
85824ebfcd6SJosh Poimboeuf	status "Copying original object files"
85924ebfcd6SJosh Poimboeuf	copy_orig_objects
86024ebfcd6SJosh Poimboeuffi
86124ebfcd6SJosh Poimboeuf
86224ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 2 )); then
86324ebfcd6SJosh Poimboeuf	status "Fixing patch(es)"
86424ebfcd6SJosh Poimboeuf	fix_patches
86551a0b7c4SJoe Lawrence	apply_patches "--silent"
86624ebfcd6SJosh Poimboeuf	status "Building patched kernel"
867b41d8b7dSJoe Lawrence	build_kernel "patched"
86824ebfcd6SJosh Poimboeuf	revert_patches
86924ebfcd6SJosh Poimboeuf	status "Copying patched object files"
87024ebfcd6SJosh Poimboeuf	copy_patched_objects
87124ebfcd6SJosh Poimboeuffi
87224ebfcd6SJosh Poimboeuf
87324ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 3 )); then
87424ebfcd6SJosh Poimboeuf	status "Diffing objects"
87524ebfcd6SJosh Poimboeuf	diff_objects
87678be9facSJosh Poimboeuf	if [[ -v DIFF_CHECKSUM ]]; then
87778be9facSJosh Poimboeuf		status "Finding first changed instructions"
87878be9facSJosh Poimboeuf		diff_checksums
87978be9facSJosh Poimboeuf	fi
88024ebfcd6SJosh Poimboeuffi
88124ebfcd6SJosh Poimboeuf
88224ebfcd6SJosh Poimboeufif (( SHORT_CIRCUIT <= 4 )); then
88324ebfcd6SJosh Poimboeuf	status "Building patch module: $OUTFILE"
88424ebfcd6SJosh Poimboeuf	build_patch_module
88524ebfcd6SJosh Poimboeuffi
88624ebfcd6SJosh Poimboeuf
88724ebfcd6SJosh Poimboeufstatus "SUCCESS"
888