Skip to content

Commit 891d0ca

Browse files
theihoranakryiko
authored andcommitted
Add scripts to generate fresh vmlinux.h
Add script/gen-vmlinux-headers.sh (adapted from retsnoop [1]), that can build a Linux Kernel for various architectures and generate corresponding vmlinux.h header. Add kconfigs directory with common and per-arch configurations that can be used to produce vmlinux.h In gen-vmlinux-header.sh if an explicit config is not specified, append kconfigs/config.common and kconfigs/config.${ARCH} to build the Linux Kernel. [1] https://github.com/anakryiko/retsnoop/blob/master/scripts/gen-vmlinux-headers.sh Signed-off-by: Ihor Solodrai <[email protected]>
1 parent 83a228c commit 891d0ca

File tree

9 files changed

+144
-0
lines changed

9 files changed

+144
-0
lines changed

kconfigs/config.aarch64

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_64BIT=y

kconfigs/config.common

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CONFIG_BPF=y
2+
CONFIG_BPF_EVENTS=y
3+
CONFIG_BPF_JIT=y
4+
CONFIG_BPF_SYSCALL=y
5+
CONFIG_DEBUG_INFO=y
6+
CONFIG_DEBUG_INFO_BTF=y
7+
CONFIG_DEBUG_INFO_DWARF4=y
8+
CONFIG_FPROBE=y
9+
CONFIG_FTRACE=y
10+
CONFIG_FUNCTION_TRACER=y
11+
CONFIG_KPROBES=y
12+
CONFIG_KPROBE_EVENTS=y
13+
CONFIG_MMU=y
14+
CONFIG_MODULES=y
15+
CONFIG_PERF_EVENTS=y
16+
CONFIG_TRACEPOINTS=y
17+
CONFIG_TRACING=y
18+
CONFIG_UPROBE_EVENTS=y

kconfigs/config.loongarch64

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_64BIT=y

kconfigs/config.ppc64le

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_64BIT=y

kconfigs/config.riscv64

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_64BIT=y

kconfigs/config.s390x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_64BIT=y

kconfigs/config.x86_64

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_64BIT=y

scripts/gen-vmlinux-header.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
3+
usage () {
4+
echo "USAGE: ./gen-vmlinux-headers.sh <arch> [<linux-repo-path> <kconfig-path>]"
5+
exit 1
6+
}
7+
8+
set -eu
9+
10+
source $(dirname "$0")/helpers.sh
11+
12+
WORKSPACE=$(pwd)
13+
BUILD_DIR="${BUILD_DIR:-$WORKSPACE/build}"
14+
GCC_VERSION=${GCC_VERSION:-"13"}
15+
OUTPUT_DIR="${OUTPUT_DIR:-$WORKSPACE/vmlinux.h}"
16+
17+
TARGET_ARCH=$1
18+
LINUX_REPO=${2:-$WORKSPACE/linux}
19+
KCONFIG=${3:-}
20+
21+
if [ -z "${TARGET_ARCH}" ]; then
22+
echo "Error: Target architecture is not set"
23+
usage
24+
fi
25+
26+
if [ ! -d "${LINUX_REPO}" ]; then
27+
echo "Error: Linux repo path is not found, LINUX_REPO=$LINUX_REPO"
28+
usage
29+
fi
30+
31+
if [ ! -f "${KCONFIG}" ]; then
32+
KCONFIG=$(mktemp /tmp/kconfig.XXXX)
33+
cp $WORKSPACE/kconfigs/config.common $KCONFIG
34+
arch_conf="$WORKSPACE/kconfigs/config.${TARGET_ARCH}"
35+
if [ -f "$arch_conf" ]; then
36+
cat "$arch_conf" >> $KCONFIG
37+
fi
38+
echo "==== Using KCONFIG=$KCONFIG for kernel build"
39+
cat $KCONFIG
40+
echo "==== end of KCONFIG=$KCONFIG"
41+
fi
42+
43+
mkdir -p "$BUILD_DIR"
44+
LINUX_REPO=$(realpath "$LINUX_REPO")
45+
KCONFIG=$(realpath "$KCONFIG")
46+
47+
# Install the cross-compiler
48+
if [ "${TARGET_ARCH}" != "x86_64" ]; then
49+
compiler_id=$(arch_compiler_id "$TARGET_ARCH")
50+
sudo apt install -y \
51+
"gcc-${GCC_VERSION}-${compiler_id}" \
52+
"binutils-${compiler_id}"
53+
sudo update-alternatives --install \
54+
/usr/bin/${compiler_id}-gcc \
55+
${compiler_id}-gcc \
56+
/usr/bin/${compiler_id}-gcc-${GCC_VERSION} 100
57+
sudo update-alternatives --set \
58+
${compiler_id}-gcc \
59+
/usr/bin/${compiler_id}-gcc-${GCC_VERSION}
60+
fi
61+
62+
build_arch(){
63+
local arch="$1"
64+
local kbuild_output="$(realpath $BUILD_DIR/$arch)"
65+
mkdir -p "$kbuild_output"
66+
local arch_slug=$(arch_slug "$arch")
67+
local compiler_id=$(arch_compiler_id "$arch")
68+
69+
echo "Building $arch ($arch_slug) into $kbuild_output..."
70+
(
71+
cd "$LINUX_REPO"
72+
make O="$kbuild_output" \
73+
ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \
74+
tinyconfig
75+
"$LINUX_REPO/scripts/kconfig/merge_config.sh" -m \
76+
-O "$kbuild_output" \
77+
"$kbuild_output/.config" "${KCONFIG}"
78+
make O="$kbuild_output" \
79+
ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \
80+
olddefconfig
81+
82+
make O="$kbuild_output" \
83+
ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \
84+
-j$(nproc) all
85+
86+
mkdir -p "$OUTPUT_DIR/$arch"
87+
bpftool btf dump file "$kbuild_output/vmlinux" format c \
88+
> "$OUTPUT_DIR/$arch/vmlinux.h"
89+
)
90+
}
91+
92+
build_arch $TARGET_ARCH

scripts/helpers.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
arch_slug() {
4+
printf "$arch" \
5+
| sed 's/x86_64/x86/' \
6+
| sed 's/i686/x86/' \
7+
| sed 's/aarch64/arm64/' \
8+
| sed 's/ppc64le/powerpc/' \
9+
| sed 's/riscv64/riscv/' \
10+
| sed 's/s390x/s390/' \
11+
| sed 's/loongarch64/loongarch/'
12+
}
13+
14+
arch_compiler_id() {
15+
local arch="$1"
16+
case "$arch" in
17+
arm)
18+
echo "arm-linux-gnueabi"
19+
;;
20+
ppc64le)
21+
echo "powerpc64le-linux-gnu"
22+
;;
23+
*)
24+
echo "${arch}-linux-gnu"
25+
;;
26+
esac
27+
}
28+

0 commit comments

Comments
 (0)