Cross Compiling Linux Kernel
Light technical post revealing the tip of the iceberg of Linux kernel programming. Read on to learn about cross compilation tool chains, and how to cross compile the Linux kernel.
Recently I’ve been kernel developing in C, and I’ve found it quite interesting to delve into the deeps of the Linux kernel. This post is for beginners who might be struggling to understand how to get started with setting up the basics before they can write kernel programs.
About cross compilation tool chains
A tool chain is most simply a set of tools used for compiling, linking, debugging, testing for a specific platform. Usually we have our most basic tools, GCC, a linker, GDB debugger. Example tool chain for ARM architecture:
Downloading cross compilation tool chain for ARM
We’ll be downloading the gnu tool chain for x86_64 Linux hosted machine
# Download and extract the contents of the toolchain
wget https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-a/10.3-2021.07/binrel/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu.tar.xz
tar -xvf gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu.tar.xz
# Add the toolchain binaries to your path
cwd=`pwd`
export PATH=$PATH:$cwd/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin
# Permenantly add it to the path (this example using zsh as the shell type)
echo PATH=$PATH:$cwd/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin >> ~/.zshrcARM tool-chains downloads link for reference: https://developer.arm.com/downloads/-/gnu-a
Compiling the Linux kernel:
Get Yan Naigebaver’s stories in your inbox
Join Medium for free to get updates from this writer.
Download the Linux kernel source code
# Download the linux kernel source code
# version = x.yz , major would be x
# wget https://cdn.kernel.org/pub/linux/kernel/v<major>.x/linux-<version>.tar.xz
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.15.tar.xz
tar -xvf linux-6.15.tar.xzBuilding the kernel
# Setting those two variables indicates to use the arm64 architecture,
# and compile with our toolchain
ARCH=arm64
CROSS_COMPILE=aarch64-none-linux-gnu-
# mrproper cleans config and garbage from last compilation tries
make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} mrproper
# defconfig creates default configuration file for the compilation process
make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} defconfig
# -j $(nproc) means using all CPU threads available for the compilation
make -j $(nproc) ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} allThe compilation process might take a while based on the computing speed of the machine.
Common problem when compiling the kernel:
I’ve encountered the following error, usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x10): multiple definition of 'yylloc'; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here
To solve this issue, run the following and compile again
# Replace all occurences of YYLTYPE yylloc with extern in the source code
grep -Rl . -e "^YYLTYPE yylloc" | \
xargs -I {} sed -i s/"^YYLTYPE yylloc"/"extern YYLTYPE yylloc"/g {}Reference link for the fix: https://github.com/BPI-SINOVOIP/BPI-M4-bsp/issues/4
Congratulations now you have a compiled Linux kernel suitable for the ARM64 architecture!
Cheers!








