Skip to content

Commit e88e2cc

Browse files
liu-song-6joe-lawrence
authored andcommitted
kpatch-build: Enable cross compiling with env TARGET_ARCH
llvm/clang supports cross compiling of the kernel. To build livepatch in the same cross compile environment, enable specifying TARGET_ARCH for kpatch-build. For example, in a x86_64 host, we can build livepatch for aarch64 kernel with: TARGET=aarch64 kpatch-build ... Signed-off-by: Song Liu <song@kernel.org> Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com> [removed whitespace changes]
1 parent a04e6e9 commit e88e2cc

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

doc/patch-author-guide.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Table of contents
2929
- [Exported symbol versioning](#exported-symbol-versioning)
3030
- [System calls](#system-calls)
3131
- [Symbol Namespaces](#symbol-namespaces)
32+
- [Cross Compile](#cross-compile)
33+
3234

3335
Patch analysis
3436
--------------
@@ -964,3 +966,25 @@ ERROR: modpost: module livepatch-test uses symbol dma_buf_export from namespace
964966
```
965967
To manually import the required namespace, add the MODULE_IMPORT_NS() macro to
966968
the patch source. For example: `MODULE_IMPORT_NS("DMA_BUF")`
969+
970+
Cross Compile
971+
-------------
972+
973+
It is recommended to build the livepatch in the same environment (compiler/library/etc.) as
974+
the target kernel. When the target kernel was cross compiled for a different architecture,
975+
it is recommended to cross compile the livepatch.
976+
977+
There are two options to cross compile a livepatch.
978+
979+
To specify a separate set of cross compilers,
980+
we can set the `CROSS_COMPILE` environment variable. For example, to use `aarch64-gcc` and `aarch64-ld`,
981+
we can run kpatch-build as
982+
```
983+
CROSS_COMPILE=aarch64- kpatch-build ...
984+
```
985+
986+
llvm/clang supports cross compile with the same binaries. To specify a cross compile target, we can
987+
use the TARGET_ARCH environment variable, for example:
988+
```
989+
TARGET_ARCH=aarch64 kpatch-build ...
990+
```

kpatch-build/kpatch-build

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ set -o pipefail
4040
BASE="$PWD"
4141
SCRIPTDIR="$(readlink -f "$(dirname "$(type -p "$0")")")"
4242
ARCH="$(uname -m)"
43+
TARGET_ARCH="${TARGET_ARCH:-$ARCH}"
4344
CPUS="$(getconf _NPROCESSORS_ONLN)"
4445
CACHEDIR="${CACHEDIR:-$HOME/.kpatch}"
4546
KERNEL_SRCDIR="$CACHEDIR/src"
@@ -390,7 +391,7 @@ find_special_section_data() {
390391
check[e]=true # exception_table_entry
391392

392393
# Arch-specific features
393-
case "$ARCH" in
394+
case "$TARGET_ARCH" in
394395
"x86_64")
395396
check[a]=true # alt_instr
396397
kernel_version_gte 5.10.0 && check[s]=true # static_call_site
@@ -722,6 +723,15 @@ print_supported_distro(){
722723
fi
723724
}
724725

726+
# Used in "make ARCH=xxx" when making the kernel.
727+
# Match "aarch64" to "arm64", while keep everything else the same.
728+
kernel_make_arch() {
729+
if [[ "$1" == "aarch64" ]]; then
730+
echo "arm64"
731+
else
732+
echo "$1"
733+
fi
734+
}
725735

726736
usage() {
727737
echo "usage: $(basename "$0") [options] <patch1 ... patchN>" >&2
@@ -1299,6 +1309,11 @@ else
12991309
MAKEVARS+=("LD=${KPATCH_CC_PREFIX}${LD}")
13001310
fi
13011311

1312+
if [[ "$ARCH" != "$TARGET_ARCH" ]]; then
1313+
KARCH=$(kernel_make_arch "$TARGET_ARCH")
1314+
MAKEVARS+=("ARCH=$KARCH")
1315+
fi
1316+
13021317

13031318
# $TARGETS used as list, no quotes.
13041319
# shellcheck disable=SC2086
@@ -1489,7 +1504,12 @@ fi
14891504
cd "$TEMPDIR/output" || die
14901505
# $KPATCH_LDFLAGS and result of find used as list, no quotes.
14911506
# shellcheck disable=SC2086,SC2046
1492-
"$LD" -r $KPATCH_LDFLAGS -o ../patch/tmp_output.o $(find . -name "*.o") 2>&1 | logger || die
1507+
if [[ "$ARCH" != "$TARGET_ARCH" ]]; then
1508+
# if cross compiling, use LLD to link
1509+
"$LLD" -r $KPATCH_LDFLAGS -o ../patch/tmp_output.o $(find . -name "*.o") 2>&1 | logger || die
1510+
else
1511+
"$LD" -r $KPATCH_LDFLAGS -o ../patch/tmp_output.o $(find . -name "*.o") 2>&1 | logger || die
1512+
fi
14931513

14941514
if [[ "$USE_KLP" -eq 1 ]]; then
14951515
cp -f "$TEMPDIR"/patch/tmp_output.o "$TEMPDIR"/patch/output.o || die

0 commit comments

Comments
 (0)