xref: /linux/scripts/livepatch/klp-build (revision 029223d301620bc4e1086696047b0d5d6eba5edd)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Build a livepatch module
5
6# shellcheck disable=SC1090,SC2155,SC2164
7
8if (( BASH_VERSINFO[0]  < 4 || \
9     (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 4) )); then
10		echo "error: this script requires bash 4.4+" >&2
11	exit 1
12fi
13
14set -o errtrace
15set -o pipefail
16set -o nounset
17
18# Allow doing 'cmd | mapfile -t array' instead of 'mapfile -t array < <(cmd)'.
19# This helps keep execution in pipes so pipefail+ERR trap can catch errors.
20shopt -s lastpipe
21
22unset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP VERBOSE XTRACE
23
24REPLACE=1
25SHORT_CIRCUIT=0
26JOBS="$(getconf _NPROCESSORS_ONLN)"
27shopt -o xtrace | grep -q 'on' && XTRACE=1
28
29# Avoid removing the previous $TMP_DIR until args have been fully processed.
30KEEP_TMP=1
31
32SCRIPT="$(basename "$0")"
33SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
34FIX_PATCH_LINES="$SCRIPT_DIR/fix-patch-lines"
35
36OBJTOOL="$PWD/tools/objtool/objtool"
37CONFIG="$PWD/.config"
38TMP_DIR="$PWD/klp-tmp"
39
40ORIG_DIR="$TMP_DIR/1-orig"
41PATCHED_DIR="$TMP_DIR/2-patched"
42ORIG_CSUM_DIR="$TMP_DIR/3-checksum-orig"
43PATCHED_CSUM_DIR="$TMP_DIR/3-checksum-patched"
44DIFF_DIR="$TMP_DIR/4-diff"
45KMOD_DIR="$TMP_DIR/5-kmod"
46
47STASH_DIR="$TMP_DIR/stash"
48TIMESTAMP="$TMP_DIR/timestamp"
49PATCH_TMP_DIR="$TMP_DIR/tmp"
50
51KLP_DIFF_LOG="$DIFF_DIR/diff.log"
52
53# Terminal output colors
54read -r COLOR_RESET COLOR_BOLD COLOR_ERROR COLOR_WARN <<< ""
55if [[ -t 1 && -t 2 ]]; then
56	COLOR_RESET="\033[0m"
57	COLOR_BOLD="\033[1m"
58	COLOR_ERROR="\033[0;31m"
59	COLOR_WARN="\033[0;33m"
60fi
61
62grep0() {
63	# shellcheck disable=SC2317
64	command grep "$@" || true
65}
66
67# Because pipefail is enabled, the grep0 helper should be used instead of
68# grep, otherwise a failed match can propagate to an error.
69grep() {
70	echo "error: $SCRIPT: use grep0 or 'command grep' instead of bare grep" >&2
71	exit 1
72}
73
74status() {
75	echo -e "${COLOR_BOLD}$*${COLOR_RESET}"
76}
77
78warn() {
79	echo -e "${COLOR_WARN}warning${COLOR_RESET}: $SCRIPT: $*" >&2
80}
81
82die() {
83	echo -e "${COLOR_ERROR}error${COLOR_RESET}: $SCRIPT: $*" >&2
84	exit 1
85}
86
87declare -a STASHED_FILES
88
89stash_file() {
90	local file="$1"
91	local rel_file="${file#"$PWD"/}"
92
93	[[ ! -e "$file" ]] && die "no file to stash: $file"
94
95	mkdir -p "$STASH_DIR/$(dirname "$rel_file")"
96	cp -f "$file" "$STASH_DIR/$rel_file"
97
98	STASHED_FILES+=("$rel_file")
99}
100
101restore_files() {
102	local file
103
104	for file in "${STASHED_FILES[@]}"; do
105		mv -f "$STASH_DIR/$file" "$PWD/$file" || warn "can't restore file: $file"
106	done
107
108	STASHED_FILES=()
109}
110
111cleanup() {
112	set +o nounset
113	revert_patches
114	restore_files
115	[[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR"
116	return 0
117}
118
119trap_err() {
120	die "line ${BASH_LINENO[0]}: '$BASH_COMMAND'"
121}
122
123trap cleanup  EXIT INT TERM HUP
124trap trap_err ERR
125
126__usage() {
127	cat <<EOF
128Usage: $SCRIPT [OPTIONS] PATCH_FILE(s)
129Generate a livepatch module.
130
131Options:
132   -f, --show-first-changed	Show address of first changed instruction
133   -j, --jobs=<jobs>		Build jobs to run simultaneously [default: $JOBS]
134   -o, --output=<file.ko>	Output file [default: livepatch-<patch-name>.ko]
135       --no-replace		Disable livepatch atomic replace
136   -v, --verbose		Pass V=1 to kernel/module builds
137
138Advanced Options:
139   -d, --debug			Show symbol/reloc cloning decisions
140   -S, --short-circuit=STEP	Start at build step (requires prior --keep-tmp)
141				   1|orig		Build original kernel (default)
142				   2|patched		Build patched kernel
143				   3|checksum		Generate checksums
144				   4|diff		Diff objects
145				   5|kmod		Build patch module
146   -T, --keep-tmp		Preserve tmp dir on exit
147
148EOF
149}
150
151usage() {
152	__usage >&2
153}
154
155process_args() {
156	local keep_tmp=0
157	local short
158	local long
159	local args
160	local patch
161
162	short="hfj:o:vdS:T"
163	long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,short-circuit:,keep-tmp"
164
165	args=$(getopt --options "$short" --longoptions "$long" -- "$@") || {
166		echo; usage; exit
167	}
168	eval set -- "$args"
169
170	while true; do
171		case "$1" in
172			-h | --help)
173				usage
174				exit 0
175				;;
176			-f | --show-first-changed)
177				DIFF_CHECKSUM=1
178				shift
179				;;
180			-j | --jobs)
181				JOBS="$2"
182				shift 2
183				;;
184			-o | --output)
185				[[ "$2" != *.ko ]] && die "output filename should end with .ko"
186				OUTFILE="$2"
187				NAME="$(basename "$OUTFILE")"
188				NAME="${NAME%.ko}"
189				NAME="$(module_name_string "$NAME")"
190				shift 2
191				;;
192			--no-replace)
193				REPLACE=0
194				shift
195				;;
196			-v | --verbose)
197				VERBOSE=1
198				shift
199				;;
200			-d | --debug)
201				DEBUG_CLONE=1
202				keep_tmp=1
203				shift
204				;;
205			-S | --short-circuit)
206				[[ ! -d "$TMP_DIR" ]] && die "--short-circuit requires preserved klp-tmp dir"
207				keep_tmp=1
208				case "$2" in
209					1 | orig)		SHORT_CIRCUIT=1; ;;
210					2 | patched)		SHORT_CIRCUIT=2; ;;
211					3 | checksum)		SHORT_CIRCUIT=3; ;;
212					4 | diff)		SHORT_CIRCUIT=4; ;;
213					5 | kmod)		SHORT_CIRCUIT=5; ;;
214					*)		die "invalid short-circuit step '$2'" ;;
215				esac
216				shift 2
217				;;
218			-T | --keep-tmp)
219				keep_tmp=1
220				shift
221				;;
222			--)
223				shift
224				break
225				;;
226			*)
227				usage
228				exit 1
229				;;
230		esac
231	done
232
233	if [[ $# -eq 0 ]] && (( SHORT_CIRCUIT <= 2 )); then
234		usage
235		exit 1
236	fi
237
238	KEEP_TMP="$keep_tmp"
239	PATCHES=("$@")
240
241	for patch in "${PATCHES[@]}"; do
242		[[ -f "$patch" ]] || die "$patch doesn't exist"
243	done
244}
245
246# temporarily disable xtrace for especially verbose code
247xtrace_save() {
248	[[ -v XTRACE ]] && set +x
249	return 0
250}
251
252xtrace_restore() {
253	[[ -v XTRACE ]] && set -x
254	return 0
255}
256
257validate_config() {
258	xtrace_save "reading .config"
259	source "$CONFIG" || die "no .config file in $(dirname "$CONFIG")"
260	xtrace_restore
261
262	[[ -v CONFIG_LIVEPATCH ]] ||			\
263		die "CONFIG_LIVEPATCH not enabled"
264
265	[[ -v CONFIG_KLP_BUILD ]] ||			\
266		die "CONFIG_KLP_BUILD not enabled"
267
268	[[ -v CONFIG_GCC_PLUGIN_LATENT_ENTROPY ]] &&	\
269		die "kernel option 'CONFIG_GCC_PLUGIN_LATENT_ENTROPY' not supported"
270
271	[[ -v CONFIG_GCC_PLUGIN_RANDSTRUCT ]] &&	\
272		die "kernel option 'CONFIG_GCC_PLUGIN_RANDSTRUCT' not supported"
273
274	[[ -v CONFIG_LD_DEAD_CODE_DATA_ELIMINATION ]] &&		\
275		die "kernel option 'CONFIG_LD_DEAD_CODE_DATA_ELIMINATION' not supported"
276
277	[[ -v CONFIG_AS_IS_LLVM ]] &&				\
278		[[ "$CONFIG_AS_VERSION" -lt 200000 ]] &&	\
279		die "Clang assembler version < 20 not supported"
280
281	[[ -x "$OBJTOOL" ]] && "$OBJTOOL" klp 2>&1 | command grep -q "not implemented" && \
282		die "objtool not built with KLP support; install xxhash-devel/libxxhash-dev (version >= 0.8) and recompile"
283
284	return 0
285}
286
287# Only allow alphanumerics and '_' and '-' in the module name.  Everything else
288# is replaced with '-'.  Also truncate to 55 chars so the full name + NUL
289# terminator fits in the kernel's 56-byte module name array.
290module_name_string() {
291	echo "${1//[^a-zA-Z0-9_-]/-}" | cut -c 1-55
292}
293
294# If the module name wasn't specified on the cmdline with --output, give it a
295# name based on the patch name.
296set_module_name() {
297	[[ -v NAME ]] && return 0
298
299	if [[ "${#PATCHES[@]}" -eq 1 ]]; then
300		NAME="$(basename "${PATCHES[0]}")"
301		NAME="${NAME%.*}"
302	else
303		NAME="patch"
304	fi
305
306	NAME="livepatch-$NAME"
307	NAME="$(module_name_string "$NAME")"
308
309	OUTFILE="$NAME.ko"
310}
311
312# Hardcode the value printed by the localversion script to prevent patch
313# application from appending it with '+' due to a dirty working tree.
314set_kernelversion() {
315	local file="$PWD/scripts/setlocalversion"
316	local kernelrelease
317
318	stash_file "$file"
319
320	if [[ -n "$(make -s listnewconfig 2>/dev/null)" ]]; then
321		die ".config mismatch, check your .config or run 'make olddefconfig'"
322	fi
323	make syncconfig &>/dev/null || die "make syncconfig failed"
324
325	kernelrelease="$(make -s kernelrelease)"
326	[[ -z "$kernelrelease" ]] && die "failed to get kernel version"
327
328	sed -i "2i echo $kernelrelease; exit 0" scripts/setlocalversion
329}
330
331get_patch_input_files() {
332	local patch="$1"
333
334	grep0 -E '^--- ' "$patch"				\
335		| grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \
336		| gawk '{print $2}'				\
337		| sed 's|^[^/]*/||'				\
338		| sort -u
339}
340
341get_patch_output_files() {
342	local patch="$1"
343
344	grep0 -E '^\+\+\+ ' "$patch"				\
345		| grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \
346		| gawk '{print $2}'				\
347		| sed 's|^[^/]*/||'				\
348		| sort -u
349}
350
351get_patch_files() {
352	local patch="$1"
353
354	{ get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \
355		| sort -u
356}
357
358check_unsupported_patches() {
359	local patch
360
361	for patch in "${PATCHES[@]}"; do
362		local files=()
363
364		get_patch_files "$patch" | mapfile -t files
365
366		for file in "${files[@]}"; do
367			case "$file" in
368				lib/*|*/vdso/*|*/realmode/rm/*|*.S)
369					die "${patch}: unsupported patch to $file"
370					;;
371			esac
372		done
373	done
374}
375
376apply_patch() {
377	local patch="$1"
378	shift
379	local extra_args=("$@")
380	local drift_regex="with fuzz|offset [0-9]+ line"
381	local output
382	local status
383
384	[[ ! -f "$patch" ]] && die "$patch doesn't exist"
385	status=0
386	output=$(patch -p1 --dry-run --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" < "$patch" 2>&1) || status=$?
387	if [[ "$status" -ne 0 ]]; then
388		echo "$output" >&2
389		die "$patch did not apply"
390	elif [[ "$output" =~ $drift_regex ]]; then
391		[[ -v VERBOSE ]] && echo "$output" >&2
392		warn "${patch} applied with fuzz"
393	fi
394
395	APPLIED_PATCHES+=("$patch")
396	patch -p1 --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" --silent < "$patch"
397}
398
399revert_patch() {
400	local patch="$1"
401	local tmp=()
402
403	patch -p1 -R --force --no-backup-if-mismatch -r /dev/null &> /dev/null < "$patch" || true
404
405	for p in "${APPLIED_PATCHES[@]}"; do
406		[[ "$p" == "$patch" ]] && continue
407		tmp+=("$p")
408	done
409
410	APPLIED_PATCHES=("${tmp[@]}")
411}
412
413apply_patches() {
414	local extra_args=("$@")
415	local patch
416
417	for patch in "${PATCHES[@]}"; do
418		apply_patch "$patch" "${extra_args[@]}"
419	done
420}
421
422revert_patches() {
423	local patches=("${APPLIED_PATCHES[@]}")
424
425	for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do
426		revert_patch "${patches[$i]}"
427	done
428
429	APPLIED_PATCHES=()
430}
431
432validate_patches() {
433	check_unsupported_patches
434	apply_patches
435	revert_patches
436}
437
438do_init() {
439	# We're not yet smart enough to handle anything other than in-tree
440	# builds in pwd.
441	[[ ! "$PWD" -ef "$SCRIPT_DIR/../.." ]] && die "please run from the kernel root directory"
442
443	if (( SHORT_CIRCUIT >= 2 )); then
444		[[ -f "$ORIG_DIR/.complete" ]] || die "-S $SHORT_CIRCUIT requires completed $ORIG_DIR"
445	fi
446	if (( SHORT_CIRCUIT >= 3 )); then
447		[[ -f "$PATCHED_DIR/.complete" ]] || die "-S $SHORT_CIRCUIT requires completed $PATCHED_DIR"
448	fi
449	if (( SHORT_CIRCUIT >= 4 )); then
450		[[ -f "$ORIG_CSUM_DIR/.complete" ]] || die "-S $SHORT_CIRCUIT requires completed $ORIG_CSUM_DIR"
451		[[ -f "$PATCHED_CSUM_DIR/.complete" ]] || die "-S $SHORT_CIRCUIT requires completed $PATCHED_CSUM_DIR"
452	fi
453	if (( SHORT_CIRCUIT >= 5 )); then
454		[[ -f "$DIFF_DIR/.complete" ]] || die "-S $SHORT_CIRCUIT requires completed $DIFF_DIR"
455	fi
456
457	(( SHORT_CIRCUIT <= 1 )) && rm -rf "$TMP_DIR"
458	mkdir -p "$TMP_DIR"
459
460	APPLIED_PATCHES=()
461
462	[[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines"
463	command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)"
464
465	validate_config
466	set_module_name
467	set_kernelversion
468}
469
470# Refresh the patch hunk headers, specifically the line numbers and counts.
471refresh_patch() {
472	local patch="$1"
473	local tmpdir="$PATCH_TMP_DIR"
474	local input_files=()
475	local output_files=()
476
477	rm -rf "$tmpdir"
478	mkdir -p "$tmpdir/a"
479	mkdir -p "$tmpdir/b"
480
481	# Get all source files affected by the patch
482	get_patch_input_files "$patch" | mapfile -t input_files
483	get_patch_output_files "$patch" | mapfile -t output_files
484
485	# Copy orig source files to 'a'
486	echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a"
487
488	# Copy patched source files to 'b'
489	apply_patch "$patch" "--silent"
490	echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b"
491	revert_patch "$patch"
492
493	# Diff 'a' and 'b' to make a clean patch
494	( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true
495}
496
497# Copy the patches to a temporary directory, fix their lines so as not to
498# affect the __LINE__ macro for otherwise unchanged functions further down the
499# file, and update $PATCHES to point to the fixed patches.
500fix_patches() {
501	local idx
502	local i
503
504	rm -f "$TMP_DIR"/*.patch
505
506	idx=0001
507	for i in "${!PATCHES[@]}"; do
508		local old_patch="${PATCHES[$i]}"
509		local tmp_patch="$TMP_DIR/tmp.patch"
510		local patch="${PATCHES[$i]}"
511		local new_patch
512
513		new_patch="$TMP_DIR/$idx-fixed-$(basename "$patch")"
514
515		cp -f "$old_patch" "$tmp_patch"
516		refresh_patch "$tmp_patch"
517		"$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch"
518
519		PATCHES[i]="$new_patch"
520
521		rm -f "$tmp_patch"
522		idx=$(printf "%04d" $(( 10#$idx + 1 )))
523	done
524}
525
526clean_kernel() {
527	local cmd=()
528
529	cmd=("make")
530	cmd+=("--silent")
531	cmd+=("-j$JOBS")
532	cmd+=("clean")
533
534	"${cmd[@]}"
535}
536
537build_kernel() {
538	local build="$1"
539	local log="$TMP_DIR/build.log"
540	local cmd=()
541
542	cmd=("make")
543
544	# When a patch to a kernel module references a newly created unexported
545	# symbol which lives in vmlinux or another kernel module, the patched
546	# kernel build fails with the following error:
547	#
548	#   ERROR: modpost: "klp_string" [fs/xfs/xfs.ko] undefined!
549	#
550	# The undefined symbols are working as designed in that case.  They get
551	# resolved later when the livepatch module build link pulls all the
552	# disparate objects together into the same kernel module.
553	#
554	# It would be good to have a way to tell modpost to skip checking for
555	# undefined symbols altogether.  For now, just convert the error to a
556	# warning with KBUILD_MODPOST_WARN, and grep out the warning to avoid
557	# confusing the user.
558	#
559	cmd+=("KBUILD_MODPOST_WARN=1")
560
561	cmd+=("KLP_SYMIDS=1")
562
563	if [[ -v VERBOSE ]]; then
564		cmd+=("V=1")
565	else
566		cmd+=("-s")
567	fi
568	cmd+=("-j$JOBS")
569	cmd+=("KCFLAGS=-ffunction-sections -fdata-sections")
570	cmd+=("vmlinux")
571	cmd+=("modules")
572
573	"${cmd[@]}"							\
574		1> >(tee -a "$log")					\
575		2> >(tee -a "$log" | grep0 -v "modpost.*undefined!" >&2) \
576		|| die "$build kernel build failed"
577}
578
579find_objects() {
580	local opts=("$@")
581
582	# Find root-level vmlinux.o and non-root-level .ko files,
583	# excluding klp-tmp/ and hidden directories.
584	find "$PWD" -mindepth 1 \
585		    \( -path "$TMP_DIR" -o -name ".*" -o -regex "$PWD/[^/][^/]*\.ko" \) -prune -o \
586		    -type f "${opts[@]}"				\
587		    \( -name "*.ko" -o -path "$PWD/vmlinux.o" \)	\
588		    -printf '%P\n'
589}
590
591# Copy all .o archives to $ORIG_DIR
592copy_orig_objects() {
593	local files=()
594
595	rm -rf "$ORIG_DIR"
596	mkdir -p "$ORIG_DIR"
597
598	find_objects | mapfile -t files
599
600	xtrace_save "copying original objects"
601	for _file in "${files[@]}"; do
602		local rel_file="${_file/.ko/.o}"
603		local file="$PWD/$rel_file"
604		local orig_file="$ORIG_DIR/$rel_file"
605		local orig_dir="$(dirname "$orig_file")"
606
607		[[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file"
608
609		mkdir -p "$orig_dir"
610		cp -f "$file" "$orig_dir"
611	done
612	xtrace_restore
613
614	mv -f "$TMP_DIR/build.log" "$ORIG_DIR"
615	touch "$TIMESTAMP"
616	touch "$ORIG_DIR/.complete"
617}
618
619# Copy all changed objects to $PATCHED_DIR
620copy_patched_objects() {
621	local files=()
622	local opts=()
623	local found=0
624
625	rm -rf "$PATCHED_DIR"
626	mkdir -p "$PATCHED_DIR"
627
628	# Note this doesn't work with some configs, thus the 'cmp' below.
629	opts=("-newer")
630	opts+=("$TIMESTAMP")
631
632	find_objects "${opts[@]}" | mapfile -t files
633
634	xtrace_save "copying changed objects"
635	for _file in "${files[@]}"; do
636		local rel_file="${_file/.ko/.o}"
637		local file="$PWD/$rel_file"
638		local orig_file="$ORIG_DIR/$rel_file"
639		local patched_file="$PATCHED_DIR/$rel_file"
640		local patched_dir="$(dirname "$patched_file")"
641
642		[[ ! -f "$file" ]] && die "missing $(basename "$file") for $_file"
643
644		cmp -s "$orig_file" "$file" && continue
645
646		mkdir -p "$patched_dir"
647		cp -f "$file" "$patched_dir"
648		found=1
649	done
650	xtrace_restore
651
652	(( found == 0 )) && die "no changes detected"
653
654	mv -f "$TMP_DIR/build.log" "$PATCHED_DIR"
655	touch "$PATCHED_DIR/.complete"
656}
657
658# Copy .o files to a separate directory and run "objtool klp checksum" on each
659# copy.  The checksums are written to a .discard.sym_checksum section.
660#
661# If match_dir is given, only process files which also exist there.
662generate_checksums() {
663	local src_dir="$1"
664	local dest_dir="$2"
665	local match_dir="${3:-}"
666	local files=()
667	local file
668
669	rm -rf "$dest_dir"
670	mkdir -p "$dest_dir"
671
672	find "$src_dir" -type f -name "*.o" | mapfile -t files
673	for file in "${files[@]}"; do
674		local rel="${file#"$src_dir"/}"
675		local dest="$dest_dir/$rel"
676
677		[[ -n "$match_dir" && ! -f "$match_dir/$rel" ]] && continue
678
679		mkdir -p "$(dirname "$dest")"
680		cp -f "$file" "$dest"
681		"$OBJTOOL" klp checksum "$dest"
682	done
683
684	touch "$dest_dir/.complete"
685}
686
687# Diff changed objects, writing output object to $DIFF_DIR
688diff_objects() {
689	local log="$KLP_DIFF_LOG"
690	local files=()
691	local opts=()
692
693	rm -rf "$DIFF_DIR"
694	mkdir -p "$DIFF_DIR"
695
696	find "$PATCHED_CSUM_DIR" -type f -name "*.o" | mapfile -t files
697	[[ ${#files[@]} -eq 0 ]] && die "no changes detected"
698
699	[[ -v DEBUG_CLONE ]] && opts=("--debug")
700
701	# Diff all changed objects
702	for file in "${files[@]}"; do
703		local rel_file="${file#"$PATCHED_CSUM_DIR"/}"
704		local orig_file="$rel_file"
705		local patched_file="$PATCHED_CSUM_DIR/$rel_file"
706		local out_file="$DIFF_DIR/$rel_file"
707		local filter=()
708		local cmd=()
709
710		mkdir -p "$(dirname "$out_file")"
711
712		cmd=("$OBJTOOL")
713		cmd+=("klp")
714		cmd+=("diff")
715		(( ${#opts[@]} > 0 )) && cmd+=("${opts[@]}")
716		cmd+=("$orig_file")
717		cmd+=("$patched_file")
718		cmd+=("$out_file")
719
720		if [[ -v DIFF_CHECKSUM ]]; then
721			filter=("grep0")
722			filter+=("-Ev")
723			filter+=("DEBUG: .*checksum: ")
724		else
725			filter=("cat")
726		fi
727
728		(
729			cd "$ORIG_CSUM_DIR"
730			[[ -v VERBOSE ]] && echo "cd $ORIG_CSUM_DIR && ${cmd[*]}"
731			"${cmd[@]}"							\
732				1> >(tee -a "$log")					\
733				2> >(tee -a "$log" | "${filter[@]}" >&2) ||		\
734				die "objtool klp diff failed"
735		)
736	done
737
738	touch "$DIFF_DIR/.complete"
739}
740
741# For each changed object, run "objtool klp checksum" with --debug-checksum to
742# get the per-instruction checksums, and then diff those to find the first
743# changed instruction for each function.
744diff_checksums() {
745	local orig_log="$ORIG_DIR/checksum.log"
746	local patched_log="$PATCHED_DIR/checksum.log"
747	local -A funcs
748	local cmd=()
749	local line
750	local file
751	local func
752
753	gawk '/\.o: changed function: / {
754		sub(/:$/, "", $1)
755		print $1, $NF
756	}' "$KLP_DIFF_LOG" | mapfile -t lines
757
758	for line in "${lines[@]}"; do
759		read -r file func <<< "$line"
760		if [[ ! -v funcs["$file"] ]]; then
761			funcs["$file"]="$func"
762		else
763			funcs["$file"]+=" $func"
764		fi
765	done
766
767	cmd=("$OBJTOOL")
768	cmd+=("klp" "checksum")
769	cmd+=("--dry-run")
770
771	for file in "${!funcs[@]}"; do
772		local opt="--debug-checksum=${funcs[$file]// /,}"
773
774		(
775			cd "$ORIG_DIR"
776			"${cmd[@]}" "$opt" "$file" &> "$orig_log" || \
777				( cat "$orig_log" >&2; die "objtool klp checksum failed" )
778
779			cd "$PATCHED_DIR"
780			"${cmd[@]}" "$opt" "$file" &> "$patched_log" ||	\
781				( cat "$patched_log" >&2; die "objtool klp checksum failed" )
782		)
783
784		for func in ${funcs[$file]}; do
785			local -a orig patched
786			paste <(grep0 -E "^DEBUG: .*checksum: $func " "$orig_log") \
787			      <(grep0 -E "^DEBUG: .*checksum: $func " "$patched_log") |
788			while IFS= read -r line; do
789				read -ra orig <<< "${line%%$'\t'*}"
790				read -ra patched <<< "${line#*$'\t'}"
791
792				if [[ ${#patched[@]} -eq 0 ]]; then
793					printf "%s: %s: %s (removed)\n" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}"
794					break
795				elif [[ ${#orig[@]} -eq 0 ]]; then
796					printf "%s: %s: %s (added)\n" "${patched[1]%:}" "${patched[3]}" "${patched[-2]}"
797					break
798				fi
799
800				[[ "${orig[-1]}" == "${patched[-1]}" ]] && continue
801
802				printf "%s: %s: %s" "${orig[1]%:}" "${orig[3]}" "${orig[-2]}"
803				[[ "${orig[-2]}" != "${patched[-2]}" ]] && \
804					printf " (patched: %s)" "${patched[-2]}"
805				printf "\n"
806				break
807			done || true
808		done
809	done
810}
811
812# Build and post-process livepatch module in $KMOD_DIR
813build_patch_module() {
814	local makefile="$KMOD_DIR/Kbuild"
815	local log="$KMOD_DIR/build.log"
816	local kmod_file
817	local cflags=()
818	local files=()
819	local cmd=()
820
821	rm -rf "$KMOD_DIR"
822	mkdir -p "$KMOD_DIR"
823
824	cp -f "$SCRIPT_DIR/init.c" "$KMOD_DIR"
825
826	echo "obj-m := $NAME.o" > "$makefile"
827	echo -n "$NAME-y := init.o" >> "$makefile"
828
829	find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files
830	[[ ${#files[@]} -eq 0 ]] && die "no changes detected"
831
832	for file in "${files[@]}"; do
833		local rel_file="${file#"$DIFF_DIR"/}"
834		local orig_file="$ORIG_DIR/$rel_file"
835		local orig_dir="$(dirname "$orig_file")"
836		local kmod_file="$KMOD_DIR/$rel_file"
837		local kmod_dir="$(dirname "$kmod_file")"
838		local cmd_file="$kmod_dir/.$(basename "$file").cmd"
839
840		mkdir -p "$kmod_dir"
841		cp -f "$file" "$kmod_dir"
842
843		# Tell kbuild this is a prebuilt object
844		cp -f "$file" "${kmod_file}_shipped"
845
846		# Make modpost happy
847		touch "$cmd_file"
848
849		echo -n " $rel_file" >> "$makefile"
850	done
851
852	echo >> "$makefile"
853
854	cflags=("-ffunction-sections")
855	cflags+=("-fdata-sections")
856	[[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE")
857
858	cmd=("make")
859	if [[ -v VERBOSE ]]; then
860		cmd+=("V=1")
861	else
862		cmd+=("-s")
863	fi
864	cmd+=("-j$JOBS")
865	cmd+=("--directory=.")
866	cmd+=("M=$KMOD_DIR")
867	cmd+=("KCFLAGS=${cflags[*]}")
868
869	# Build a "normal" kernel module with init.c and the diffed objects
870	"${cmd[@]}"							\
871		1> >(tee -a "$log")					\
872		2> >(tee -a "$log" >&2)
873
874	kmod_file="$KMOD_DIR/$NAME.ko"
875
876	# Save off the intermediate binary for debugging
877	cp -f "$kmod_file" "$kmod_file.orig"
878
879	# Work around issue where slight .config change makes corrupt BTF
880	objcopy --remove-section=.BTF "$kmod_file"
881
882	# Fix (and work around) linker wreckage for klp syms / relocs
883	"$OBJTOOL" klp post-link "$kmod_file" || die "objtool klp post-link failed"
884
885	cp -f "$kmod_file" "$OUTFILE"
886}
887
888
889################################################################################
890
891process_args "$@"
892do_init
893
894if (( SHORT_CIRCUIT <= 2 )); then
895	status "Validating patch(es)"
896	validate_patches
897fi
898
899if (( SHORT_CIRCUIT <= 1 )); then
900	status "Building original kernel"
901	clean_kernel
902	build_kernel "original"
903	status "Copying original object files"
904	copy_orig_objects
905fi
906
907if (( SHORT_CIRCUIT <= 2 )); then
908	status "Fixing patch(es)"
909	fix_patches
910	apply_patches "--silent"
911	status "Building patched kernel"
912	build_kernel "patched"
913	revert_patches
914	status "Copying patched object files"
915	copy_patched_objects
916fi
917
918if (( SHORT_CIRCUIT <= 3 )); then
919	status "Generating original checksums"
920	generate_checksums "$ORIG_DIR" "$ORIG_CSUM_DIR" "$PATCHED_DIR"
921	status "Generating patched checksums"
922	generate_checksums "$PATCHED_DIR" "$PATCHED_CSUM_DIR"
923fi
924
925if (( SHORT_CIRCUIT <= 4 )); then
926	status "Diffing objects"
927	diff_objects
928	if [[ -v DIFF_CHECKSUM ]]; then
929		status "Finding first changed instructions"
930		diff_checksums
931	fi
932fi
933
934if (( SHORT_CIRCUIT <= 5 )); then
935	status "Building patch module: $OUTFILE"
936	build_patch_module
937fi
938
939status "SUCCESS"
940