Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0450aab

Browse files
author
Denis Davydov
committedJan 25, 2021
build.rs: Build fixes for Centos 7
1 parent b9a4c2d commit 0450aab

File tree

6 files changed

+83
-13
lines changed

6 files changed

+83
-13
lines changed
 

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ edition = "2018"
88
bitflags = "1"
99

1010
[build-dependencies]
11-
bindgen = "0.54"
11+
bindgen = "0.56"
1212
cc = "1.0"
1313
shlex = "0.1"

‎build.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ const OPAQUE_TYPES: &[&str] = &[
5353
// and https://github.com/rust-lang/rust-bindgen/issues/1538
5454
"desc_struct",
5555
"xregs_state",
56+
"xsave_struct",
57+
];
58+
const CLANG_ARGS_BLACKLIST: [&'static str; 5] = [
59+
"-maccumulate-outgoing-args",
60+
"-mpreferred-stack-boundary=3",
61+
"-mindirect-branch=thunk-extern",
62+
"-mindirect-branch-register",
63+
"-fconserve-stack",
5664
];
5765

5866
fn handle_kernel_version_cfg(bindings_path: &PathBuf) {
@@ -86,10 +94,25 @@ fn handle_kernel_version_cfg(bindings_path: &PathBuf) {
8694
}
8795
if major >= 4 {
8896
// We don't currently support anything older than 4.4
89-
for x in 4..=if major > 4 { 20 } else { minor } {
97+
for x in 1..=if major > 4 { 20 } else { minor } {
9098
println!("cargo:rustc-cfg=kernel_4_{}_0_or_greater", x);
9199
}
92100
}
101+
if major >= 3 {
102+
for x in 1..=if major > 3 { 19 } else { minor } {
103+
println!("cargo:rustc-cfg=kernel_3_{}_0_or_greater", x);
104+
}
105+
}
106+
}
107+
108+
fn handle_kernel_rhel_kabi(fs_ops_path: &PathBuf) {
109+
let f = BufReader::new(fs::File::open(fs_ops_path).unwrap());
110+
for line in f.lines() {
111+
if line.unwrap().contains("RH_KABI_REPLACE") {
112+
println!("cargo:rustc-cfg=kernel_rh_kabi");
113+
break;
114+
}
115+
}
93116
}
94117

95118
fn handle_kernel_symbols_cfg(symvers_path: &PathBuf) {
@@ -153,6 +176,12 @@ fn main() {
153176

154177
builder = builder.clang_arg(format!("--target={}", target));
155178
for arg in kernel_args.iter() {
179+
if CLANG_ARGS_BLACKLIST.contains(&arg.as_str()) {
180+
continue;
181+
}
182+
if arg.as_str() == "-DOS_CENTOS" {
183+
println!("cargo:rustc-cfg=os_centos")
184+
}
156185
builder = builder.clang_arg(arg.clone());
157186
}
158187

@@ -179,16 +208,26 @@ fn main() {
179208
.expect("Couldn't write bindings!");
180209

181210
handle_kernel_version_cfg(&out_path.join("bindings.rs"));
211+
handle_kernel_rhel_kabi(
212+
&PathBuf::from(&kernel_dir)
213+
.parent()
214+
.unwrap()
215+
.join("source/include/linux/fs.h"),
216+
);
182217
handle_kernel_symbols_cfg(&PathBuf::from(&kernel_dir).join("Module.symvers"));
183218

184219
let mut builder = cc::Build::new();
185-
builder.compiler(env::var("CC").unwrap_or_else(|_| "clang".to_string()));
220+
let compiler = env::var("CC").unwrap_or_else(|_| "clang".to_string());
221+
builder.compiler(&compiler);
186222
builder.target(&target);
187223
builder.warnings(false);
188224
println!("cargo:rerun-if-changed=src/helpers.c");
189225
builder.file("src/helpers.c");
190226
for arg in kernel_args.iter() {
191227
builder.flag(&arg);
192228
}
229+
if &compiler == "gcc" {
230+
builder.flag("-fno-pie");
231+
}
193232
builder.compile("helpers");
194233
}

‎hello-world/Kbuild

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CARGO ?= cargo
66
export c_flags
77

88
$(src)/target/x86_64-linux-kernel/debug/libhello_world.a: cargo_will_determine_dependencies
9-
cd $(src); $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel
9+
cd $(src); $(CARGO) +nightly build -Z build-std=core,alloc --target=x86_64-linux-kernel
1010

1111
.PHONY: cargo_will_determine_dependencies
1212

‎hello-world/Makefile

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
export KDIR ?= /lib/modules/$(shell uname -r)/build
22

3+
CONFIG_CC_IS_CLANG := y
34
CLANG ?= clang
45
ifeq ($(origin CC),default)
56
CC := ${CLANG}
67
endif
78

9+
HEADER_SEMVER ?= $(shell uname -r | grep -oG "[0-9]\+\.[0-9]\+\.[0-9]\+" | head -1)
10+
ifeq ($(shell echo $(HEADER_SEMVER) 4.4.0 | tr " " "\n" | sort -V | head -1), $(HEADER_SEMVER))
11+
CC := gcc
12+
CONFIG_CC_IS_CLANG := n
13+
endif
14+
15+
ifneq (,$(wildcard /etc/centos-release))
16+
KCPPFLAGS += -DOS_CENTOS
17+
endif
18+
819
all:
9-
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) CONFIG_CC_IS_CLANG=y
20+
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) KCPPFLAGS=$(KCPPFLAGS) CONFIG_CC_IS_CLANG=$(CONFIG_CC_IS_CLANG)
1021

1122
clean:
1223
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) clean

‎src/file_operations.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,24 @@ impl<T: FileOperations> FileOperationsVtable<T> {
159159
} else {
160160
None
161161
},
162-
162+
#[cfg(not(kernel_4_1_0_or_greater))]
163+
aio_read: None,
164+
#[cfg(not(kernel_4_1_0_or_greater))]
165+
aio_write: None,
166+
#[cfg(not(kernel_3_11_0_or_greater))]
167+
readdir: None,
163168
#[cfg(not(kernel_4_9_0_or_greater))]
164169
aio_fsync: None,
165170
check_flags: None,
166-
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater)))]
171+
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater), not(os_centos)))]
167172
clone_file_range: None,
168173
compat_ioctl: None,
169174
#[cfg(kernel_4_5_0_or_greater)]
170175
copy_file_range: None,
171-
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater)))]
176+
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater), not(os_centos)))]
172177
dedupe_file_range: None,
173178
fallocate: None,
174-
#[cfg(kernel_4_19_0_or_greater)]
179+
#[cfg(any(kernel_4_19_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))]
175180
fadvise: None,
176181
fasync: None,
177182
flock: None,
@@ -181,26 +186,40 @@ impl<T: FileOperations> FileOperationsVtable<T> {
181186
iterate: None,
182187
#[cfg(kernel_4_7_0_or_greater)]
183188
iterate_shared: None,
184-
#[cfg(kernel_5_1_0_or_greater)]
189+
#[cfg(any(kernel_5_1_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))]
185190
iopoll: None,
186191
lock: None,
187192
mmap: None,
188193
#[cfg(kernel_4_15_0_or_greater)]
189194
mmap_supported_flags: 0,
190195
owner: ptr::null_mut(),
191196
poll: None,
197+
#[cfg(kernel_3_16_0_or_greater)]
192198
read_iter: None,
193-
#[cfg(kernel_4_20_0_or_greater)]
199+
#[cfg(any(kernel_4_20_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))]
194200
remap_file_range: None,
195201
sendpage: None,
196202
#[cfg(kernel_aufs_setfl)]
197203
setfl: None,
204+
#[cfg(kernel_rh_kabi)]
205+
__bindgen_anon_1: bindings::file_operations__bindgen_ty_1 { setlease: None },
206+
#[cfg(not(kernel_rh_kabi))]
198207
setlease: None,
199208
show_fdinfo: None,
200209
splice_read: None,
201210
splice_write: None,
202211
unlocked_ioctl: None,
212+
#[cfg(kernel_3_16_0_or_greater)]
203213
write_iter: None,
214+
215+
#[cfg(all(os_centos, kernel_4_18_0_or_greater))]
216+
rh_reserved1: 0,
217+
#[cfg(all(os_centos, kernel_4_18_0_or_greater))]
218+
rh_reserved2: 0,
219+
#[cfg(all(os_centos, kernel_4_18_0_or_greater))]
220+
rh_reserved3: 0,
221+
#[cfg(all(os_centos, kernel_4_18_0_or_greater))]
222+
rh_reserved4: 0,
204223
};
205224
}
206225

‎src/helpers.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
#include <linux/uaccess.h>
44
#include <linux/version.h>
55

6-
76
void bug_helper(void)
87
{
98
BUG();
109
}
1110

1211
int access_ok_helper(const void __user *addr, unsigned long n)
1312
{
14-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) /* v5.0-rc1~46 */
13+
#if defined(OS_CENTOS) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
14+
return access_ok(addr, n);
15+
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) /* v5.0-rc1~46 */
1516
return access_ok(addr, n);
1617
#else
1718
return access_ok(0, addr, n);

0 commit comments

Comments
 (0)
This repository has been archived.