Skip to content

Commit 096dd7c

Browse files
committed
Forbid rustc from using -f{function,data}-sections (fixes #20 panic)
Some kernel configs were panicking at the Rust example driver initialization. The `__MOD` static value had a bogus value, which meant that trying to initialize it was dropping the object that was, supposedly, there. The memory corruption happened during rootfs unpacking, which explains why it only happened in some configs (like in CI) and why it also didn't happen if there was an early error during unpacking. That memory corruption, in turn, was caused because the `__MOD` symbol was being placed after the end of the kernel reserve. That happened due to the kernel's linker script not supporting unique sections per symbol for dead code data elimination -- yet. Some arches do, but until we can rely on that, we need to disable their generation in rustc's side for the moment. Since we discussed to have the target spec on our side, and since `-Z function-sections=false` was added just a month ago, I went with the spec route. Other symbols were being placed in unexpected places, which should be fixed now too. Signed-off-by: Miguel Ojeda <[email protected]>
1 parent fe8d5f6 commit 096dd7c

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,9 @@ KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
510510
KBUILD_CPPFLAGS := -D__KERNEL__
511511
KBUILD_RUSTCFLAGS :=
512512
# TODO: a simple way to update `Cargo.lock` when we add a new driver
513-
# TODO: another option is using explicit target specs, e.g.
514-
# `--target=$(srctree)/arch/$(SRCARCH)/rust-target-spec.json`
515513
KBUILD_CARGOFLAGS := $(CARGO_VERBOSE) --locked \
516514
-Z build-std=core,alloc -Z unstable-options \
517-
--out-dir=out --target=x86_64-linux-kernel
515+
--out-dir=out --target=$(PWD)/$(srctree)/arch/$(SRCARCH)/rust/target.json
518516
KBUILD_AFLAGS_KERNEL :=
519517
KBUILD_CFLAGS_KERNEL :=
520518
KBUILD_RUSTCFLAGS_KERNEL :=

arch/x86/rust/target.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"arch": "x86_64",
3+
"code-model": "kernel",
4+
"cpu": "x86-64",
5+
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
6+
"disable-redzone": true,
7+
"eliminate-frame-pointer": false,
8+
"env": "gnu",
9+
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-3dnow,-3dnowa,-avx,-avx2,+soft-float",
10+
"function-sections": false,
11+
"is-builtin": true,
12+
"linker-flavor": "gcc",
13+
"linker-is-gnu": true,
14+
"llvm-target": "x86_64-elf",
15+
"max-atomic-width": 64,
16+
"needs-plt": true,
17+
"os": "none",
18+
"panic-strategy": "abort",
19+
"position-independent-executables": true,
20+
"pre-link-args": {
21+
"gcc": [
22+
"-Wl,--as-needed",
23+
"-Wl,-z,noexecstack",
24+
"-m64"
25+
]
26+
},
27+
"relocation-model": "static",
28+
"relro-level": "full",
29+
"stack-probes": true,
30+
"target-c-int-width": "32",
31+
"target-endian": "little",
32+
"target-pointer-width": "64",
33+
"vendor": "unknown"
34+
}

rust/kernel/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ fn main() {
9696

9797
let kernel_args = prepare_cflags(&cflags, &kernel_dir);
9898

99-
let target = env::var("TARGET").unwrap();
99+
// TODO: pass the proper triple to bindgen
100+
let target = "x86_64-linux-kernel";
100101

101102
let mut builder = bindgen::Builder::default()
102103
.use_core()

0 commit comments

Comments
 (0)