diff --git a/Makefile b/Makefile index 757663a..549c91f 100644 --- a/Makefile +++ b/Makefile @@ -99,7 +99,7 @@ clean: out/.options.%: $(mkdir_p) - rm out/.options.* || : + rm -f out/.options.* touch $@ bpf/vmlinux/system.h: @@ -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 $@ diff --git a/kmod/AKMBUILD b/kmod/AKMBUILD index ed7ca4d..e45f421 100644 --- a/kmod/AKMBUILD +++ b/kmod/AKMBUILD @@ -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" } diff --git a/kmod/Makefile b/kmod/Makefile index 72f44aa..e15e2d2 100644 --- a/kmod/Makefile +++ b/kmod/Makefile @@ -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 @@ -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=." + @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 diff --git a/kmod/dkms.conf b/kmod/dkms.conf index 1e351a2..1319ada 100644 --- a/kmod/dkms.conf +++ b/kmod/dkms.conf @@ -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" diff --git a/tools/vendor-vmlinux-h b/tools/vendor-vmlinux-h index fef3afc..8695057 100755 --- a/tools/vendor-vmlinux-h +++ b/tools/vendor-vmlinux-h @@ -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" <"$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