From bc60be0dc815b46e4b011eb6a93fed4697f273da Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Mon, 18 Jan 2021 12:17:13 +0300 Subject: [PATCH] build.rs: Build fixes for Centos 7 --- Cargo.toml | 2 +- build.rs | 27 +++++++++++++++++++++++++-- hello-world/Kbuild | 2 +- hello-world/Makefile | 13 ++++++++++++- src/file_operations.rs | 31 +++++++++++++++++++++++++------ src/helpers.c | 5 +++-- 6 files changed, 67 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5bc4b0d8..9df03b47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,6 @@ edition = "2018" bitflags = "1" [build-dependencies] -bindgen = "0.54" +bindgen = "0.56" cc = "1.0" shlex = "0.1" diff --git a/build.rs b/build.rs index 9358ad4c..b7050916 100644 --- a/build.rs +++ b/build.rs @@ -53,6 +53,14 @@ const OPAQUE_TYPES: &[&str] = &[ // and https://github.com/rust-lang/rust-bindgen/issues/1538 "desc_struct", "xregs_state", + "xsave_struct", +]; +const CLANG_ARGS_BLACKLIST: [&'static str; 5] = [ + "-maccumulate-outgoing-args", + "-mpreferred-stack-boundary=3", + "-mindirect-branch=thunk-extern", + "-mindirect-branch-register", + "-fconserve-stack", ]; fn handle_kernel_version_cfg(bindings_path: &PathBuf) { @@ -86,10 +94,15 @@ fn handle_kernel_version_cfg(bindings_path: &PathBuf) { } if major >= 4 { // We don't currently support anything older than 4.4 - for x in 4..=if major > 4 { 20 } else { minor } { + for x in 1..=if major > 4 { 20 } else { minor } { println!("cargo:rustc-cfg=kernel_4_{}_0_or_greater", x); } } + if major >= 3 { + for x in 1..=if major > 3 { 19 } else { minor } { + println!("cargo:rustc-cfg=kernel_3_{}_0_or_greater", x); + } + } } fn handle_kernel_symbols_cfg(symvers_path: &PathBuf) { @@ -153,6 +166,12 @@ fn main() { builder = builder.clang_arg(format!("--target={}", target)); for arg in kernel_args.iter() { + if CLANG_ARGS_BLACKLIST.contains(&arg.as_str()) { + continue; + } + if arg.as_str() == "-DOS_CENTOS" { + println!("cargo:rustc-cfg=os_centos") + } builder = builder.clang_arg(arg.clone()); } @@ -182,7 +201,8 @@ fn main() { handle_kernel_symbols_cfg(&PathBuf::from(&kernel_dir).join("Module.symvers")); let mut builder = cc::Build::new(); - builder.compiler(env::var("CC").unwrap_or_else(|_| "clang".to_string())); + let compiler = env::var("CC").unwrap_or_else(|_| "clang".to_string()); + builder.compiler(&compiler); builder.target(&target); builder.warnings(false); println!("cargo:rerun-if-changed=src/helpers.c"); @@ -190,5 +210,8 @@ fn main() { for arg in kernel_args.iter() { builder.flag(&arg); } + if &compiler == "gcc" { + builder.flag("-fno-pie"); + } builder.compile("helpers"); } diff --git a/hello-world/Kbuild b/hello-world/Kbuild index 81d6dc88..de7cf5ff 100644 --- a/hello-world/Kbuild +++ b/hello-world/Kbuild @@ -6,7 +6,7 @@ CARGO ?= cargo export c_flags $(src)/target/x86_64-linux-kernel/debug/libhello_world.a: cargo_will_determine_dependencies - cd $(src); $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel + cd $(src); $(CARGO) +nightly build -Z build-std=core,alloc --target=x86_64-linux-kernel .PHONY: cargo_will_determine_dependencies diff --git a/hello-world/Makefile b/hello-world/Makefile index ecbce40d..ccb062f2 100644 --- a/hello-world/Makefile +++ b/hello-world/Makefile @@ -1,12 +1,23 @@ export KDIR ?= /lib/modules/$(shell uname -r)/build +CONFIG_CC_IS_CLANG := y CLANG ?= clang ifeq ($(origin CC),default) CC := ${CLANG} endif +HEADER_SEMVER ?= $(shell uname -r | grep -oG "[0-9]\+\.[0-9]\+\.[0-9]\+" | head -1) +ifeq ($(shell echo $(HEADER_SEMVER) 4.4.0 | tr " " "\n" | sort -V | head -1), $(HEADER_SEMVER)) +CC := gcc +CONFIG_CC_IS_CLANG := n +endif + +ifneq (,$(wildcard /etc/centos-release)) +KCPPFLAGS += -DOS_CENTOS +endif + all: - $(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) CONFIG_CC_IS_CLANG=y + $(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) KCPPFLAGS=$(KCPPFLAGS) CONFIG_CC_IS_CLANG=$(CONFIG_CC_IS_CLANG) clean: $(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) clean diff --git a/src/file_operations.rs b/src/file_operations.rs index 1f7d85cc..1be4d88f 100644 --- a/src/file_operations.rs +++ b/src/file_operations.rs @@ -159,19 +159,24 @@ impl FileOperationsVtable { } else { None }, - + #[cfg(not(kernel_4_1_0_or_greater))] + aio_read: None, + #[cfg(not(kernel_4_1_0_or_greater))] + aio_write: None, + #[cfg(not(kernel_3_11_0_or_greater))] + readdir: None, #[cfg(not(kernel_4_9_0_or_greater))] aio_fsync: None, check_flags: None, - #[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater)))] + #[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater), not(os_centos)))] clone_file_range: None, compat_ioctl: None, #[cfg(kernel_4_5_0_or_greater)] copy_file_range: None, - #[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater)))] + #[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater), not(os_centos)))] dedupe_file_range: None, fallocate: None, - #[cfg(kernel_4_19_0_or_greater)] + #[cfg(any(kernel_4_19_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))] fadvise: None, fasync: None, flock: None, @@ -181,7 +186,7 @@ impl FileOperationsVtable { iterate: None, #[cfg(kernel_4_7_0_or_greater)] iterate_shared: None, - #[cfg(kernel_5_1_0_or_greater)] + #[cfg(any(kernel_5_1_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))] iopoll: None, lock: None, mmap: None, @@ -189,18 +194,32 @@ impl FileOperationsVtable { mmap_supported_flags: 0, owner: ptr::null_mut(), poll: None, + #[cfg(kernel_3_16_0_or_greater)] read_iter: None, - #[cfg(kernel_4_20_0_or_greater)] + #[cfg(any(kernel_4_20_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))] remap_file_range: None, sendpage: None, #[cfg(kernel_aufs_setfl)] setfl: None, + #[cfg(all(not(kernel_4_18_0_or_greater), os_centos))] + __bindgen_anon_1: bindings::file_operations__bindgen_ty_1 { setlease: None }, + #[cfg(any(kernel_4_18_0_or_greater, not(os_centos)))] setlease: None, show_fdinfo: None, splice_read: None, splice_write: None, unlocked_ioctl: None, + #[cfg(kernel_3_16_0_or_greater)] write_iter: None, + + #[cfg(all(os_centos, kernel_4_18_0_or_greater))] + rh_reserved1: 0, + #[cfg(all(os_centos, kernel_4_18_0_or_greater))] + rh_reserved2: 0, + #[cfg(all(os_centos, kernel_4_18_0_or_greater))] + rh_reserved3: 0, + #[cfg(all(os_centos, kernel_4_18_0_or_greater))] + rh_reserved4: 0, }; } diff --git a/src/helpers.c b/src/helpers.c index cf2ecd19..8b5a6a6a 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -3,7 +3,6 @@ #include #include - void bug_helper(void) { BUG(); @@ -11,7 +10,9 @@ void bug_helper(void) int access_ok_helper(const void __user *addr, unsigned long n) { -#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) /* v5.0-rc1~46 */ +#if defined(OS_CENTOS) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0) + return access_ok(addr, n); +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) /* v5.0-rc1~46 */ return access_ok(addr, n); #else return access_ok(0, addr, n);