Skip to content

Commit

Permalink
kmod: auto detect whether to apply hack
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Jul 25, 2024
1 parent 1832fa8 commit 65e5faa
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ clean:

out/.options.%:
$(mkdir_p)
rm out/.options.* || :
rm -f out/.options.*
touch $@

bpf/vmlinux/system.h:
Expand All @@ -121,7 +121,7 @@ out/mimic: $(mimic_obj)
$(mkdir_p)
$(CC) $(CFLAGS) $(mimic_obj) -o $@ $(LDFLAGS) $(mimic_link_libs)

out/mimic.ko: .FORCE
out/mimic.ko: .FORCE build-tools
$(mkdir_p)
$(MAKE) -C kmod
cp kmod/mimic.ko $@
Expand Down
2 changes: 1 addition & 1 deletion kmod/AKMBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ built_modules="$modname.ko"

build() {
cp -r "$srcdir"/* "$builddir"
make $MAKEFLAGS -C "$builddir" KERNEL_UNAME="$kernel_ver" DISTRO_HACK=1
make $MAKEFLAGS -C "$builddir" KERNEL_UNAME="$kernel_ver"
}
48 changes: 32 additions & 16 deletions kmod/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ccflags-y := -D_MIMIC_KMOD
KERNEL_UNAME ?= $(shell uname -r)
SYSTEM_BUILD_DIR := /lib/modules/$(KERNEL_UNAME)/build

# Notes on distro hack:
# Notes on hack:
#
# - Debian does not ship vmlinux nor resolve_btfids in
# linux-headers like Arch Linux does, so we need to refer to externally
Expand All @@ -20,33 +20,49 @@ SYSTEM_BUILD_DIR := /lib/modules/$(KERNEL_UNAME)/build
# Other distributions lacking components necessary to build kernel module BTF
# can also use the hack in packaging.

ifdef DISTRO_HACK
ifneq ($(wildcard $(SYSTEM_BUILD_DIR)/vmlinux),)
vmlinux_exists := 1
else
vmlinux_exists := 0
endif
ifneq ($(wildcard $(SYSTEM_BUILD_DIR)/tools/bpf/resolve_btfids/resolve_btfids),)
resolve_btfids_exists := 1
else
resolve_btfids_exists := 0
endif

ifeq ($(vmlinux_exists)$(resolve_btfids_exists),11)
build_dir := $(SYSTEM_BUILD_DIR)
else
enable_hack := 1
build_dir := build

VMLINUX_TO_BTF ?= /usr/lib/mimic/vmlinux-to-btf
RESOLVE_BTFIDS ?= /usr/lib/mimic/resolve_btfids

export EXTRACT_VMLINUX ?= /usr/lib/mimic/extract-vmlinux
export EXTRACT_BTF ?= /usr/lib/mimic/extract-btf
path_extension := PATH="$$PATH:/usr/lib/mimic:$(CURDIR)/../out:$(CURDIR)/../tools"
VMLINUX_TO_BTF ?= $(path_extension) vmlinux-to-btf
export EXTRACT_VMLINUX ?= $(path_extension) extract-vmlinux
export EXTRACT_BTF ?= $(path_extension) extract-btf
export VMLINUX_SUFFIX ?= -$(KERNEL_UNAME)

else
build_dir := $(SYSTEM_BUILD_DIR)
endif

all:
ifdef DISTRO_HACK
cp -rL $(SYSTEM_BUILD_DIR) $(build_dir) || true
$(VMLINUX_TO_BTF) $(build_dir)
if [ ! -f $(build_dir)/tools/bpf/resolve_btfids/resolve_btfids ]; then \
install -Dm755 $(RESOLVE_BTFIDS) $(build_dir)/tools/bpf/resolve_btfids/resolve_btfids; \
fi
ifdef enable_hack
-cp -rL $(SYSTEM_BUILD_DIR) $(build_dir)
ifeq ($(vmlinux_exists),0)
$(VMLINUX_TO_BTF) >$(build_dir)/vmlinux
endif
ifeq ($(resolve_btfids_exists),0)
ifeq ($(wildcard $(RESOLVE_BTFIDS)),)
@echo "ERROR: `resolve_btfids` not found. Please compile it from Linux kernel source and re-run make with RESOLVE_BTFIDS=<path>."
@exit 1
endif
install -Dm755 $(RESOLVE_BTFIDS) $(build_dir)/tools/bpf/resolve_btfids/resolve_btfids
endif # resolve_btfids_exists
endif # enable_hack
$(MAKE) -C $(build_dir) M=$(CURDIR) modules

clean:
ifdef DISTRO_HACK
rm -rf $(build_dir)
endif
rm -rf build
[ ! -d $(SYSTEM_BUILD_DIR) ] || $(MAKE) -C $(SYSTEM_BUILD_DIR) M=$(CURDIR) clean
2 changes: 1 addition & 1 deletion kmod/dkms.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PACKAGE_NAME="mimic"
PACKAGE_VERSION="0.4.2"
MAKE[0]="make KERNEL_UNAME=$kernelver DISTRO_HACK=1"
MAKE[0]="make KERNEL_UNAME=$kernelver"
CLEAN="make clean"
BUILT_MODULE_NAME[0]="mimic"
DEST_MODULE_LOCATION[0]="/kernel/drivers/misc"
Expand Down
2 changes: 1 addition & 1 deletion tools/vendor-vmlinux-h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ do_vendor() {
)

BOOT_DIR="out/$debian_arch/boot" VMLINUX_SUFFIX="-${uname_r_ver}-${flavor}" \
tools/vmlinux-to-btf "out/$debian_arch" "$endianness"
tools/vmlinux-to-btf "$endianness" >"out/$debian_arch/vmlinux"

mkdir -p bpf/vmlinux
cat >"bpf/vmlinux/$arch.h" <<EOF
Expand Down
37 changes: 17 additions & 20 deletions tools/vmlinux-to-btf
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,30 @@
#
# See kmod/Makefile for usage with automatic kernel building tool e.g. DKMS.

module_dir="$1"
endianness="$2"
endianness="$1"
: "${VMLINUX_SUFFIX:=-$(uname -r)}"
: "${EXTRACT_VMLINUX:="tools/extract-vmlinux"}"
: "${EXTRACT_BTF:="out/extract-btf"}"
: "${BOOT_DIR:="/boot"}"

if [ ! -f "$module_dir/vmlinux" ]; then
if [ -f "$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX" ]; then
"$EXTRACT_VMLINUX" "$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX" >"$module_dir/vmlinux"
[ $? -eq 0 ] || "$EXTRACT_BTF" "$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX" "$endianness" >"$module_dir/vmlinux"
if [ -f "$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX" ]; then
"$EXTRACT_VMLINUX" "$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX"
[ $? -eq 0 ] || "$EXTRACT_BTF" "$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX" "$endianness"
if [ $? -ne 0 ]; then
>&2 echo "ERROR: cannot extract BTF from '$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX'"
exit 1
fi
elif [ -f "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" ]; then
if readelf -h "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" >/dev/null 2>&1; then
cp "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" "$module_dir/vmlinux"
else
"$EXTRACT_BTF" "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" "$endianness"
if [ $? -ne 0 ]; then
>&2 echo "ERROR: cannot extract BTF from '$BOOT_DIR/vmlinuz$VMLINUX_SUFFIX'"
>&2 echo "ERROR: cannot extract BTF from '$BOOT_DIR/vmlinux$VMLINUX_SUFFIX'"
exit 1
fi
elif [ -f "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" ]; then
if readelf -h "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" >/dev/null 2>/dev/null; then
cp "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" "$module_dir/vmlinux"
else
"$EXTRACT_BTF" "$BOOT_DIR/vmlinux$VMLINUX_SUFFIX" "$endianness" >"$module_dir/vmlinux"
if [ $? -ne 0 ]; then
>&2 echo "ERROR: cannot extract BTF from '$BOOT_DIR/vmlinux$VMLINUX_SUFFIX'"
exit 1
fi
fi
else
>&2 echo "ERROR: no boot image found"
exit 1
fi
else
>&2 echo "ERROR: no boot image found"
exit 1
fi

0 comments on commit 65e5faa

Please sign in to comment.