tsgates / rust.ko Goto Github PK
View Code? Open in Web Editor NEWA minimal Linux kernel module written in rust.
A minimal Linux kernel module written in rust.
It seems like there have been a bit of changes, so that e.g. #![feature(libc)]
and #![feature(core)]
aren't readily available any more. Are you able to compile this code yourself w/ rustc 1.2 or newer versions?
When I run make I get the error No rule to make target 'modules'.
What am I doing wrong?
this might not be an issue, but i'm really surprized to see that you ignore the length of the string slice in main.rs while not doing anything about null terminators. Or does fixup add a null terminator to the &'static str somehow?
I'm using linux kernel 4.20.6 and the following clang and rust versions[0]. The make command succeds for me, but I get the following warning. Seems like the module compiles successfully but the linking to rust library is broken.
WARNING: "rust_main" [/usr/src/rust.ko/build/hello.ko] undefined!
Let me know if more information is needed to debug this issue.
[0]:
[root@host rust.ko]# rustc --version
rustc 1.35.0-nightly (70f130954 2019-04-16)
[root@host rust.ko]# clang --version
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/rh/llvm-toolset-7/root/usr/bin
The project doesn't get compiled out of the box with rustc 1.20.0-nightly (bf0a9e0b4 2017-07-10)
. Instead of the forked version of bindgen (which doesn't get compiled), I used the original one which generates a kernel binding that uses std
crate everywhere. A snippet looks like:
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
impl <T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::std::marker::PhantomData)
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T { ::std::mem::transmute(self) }
#[inline]
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
::std::mem::transmute(self)
}
#[inline]
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
::std::slice::from_raw_parts(self.as_ptr(), len)
}
#[inline]
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
}
}
While compiling, I get the following errors (a lot of similar ones because of use of std
crate).
error[E0433]: failed to resolve. Could not find `marker` in `std`
--> std/src/os/kernel.rs:5:38
|
5 | pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Could not find `marker` in `std`
I wonder how the original project was even compiled. Any idea how can I get around the issue?
I'm getting the clang error, and failed to build from source.
bluet@Titania:~/workspace/rust.ko$ make Compiling linux-std v0.1.0 (file:///home/bluet/workspace/rust.ko/std) error: failed to run custom build command for
linux-std v0.1.0 (file:///home/bluet/workspace/rust.ko/std) Process didn't exit successfully:
/home/bluet/workspace/rust.ko/target/debug/build/linux-std-b8b7ab4d3d9bbe98/build-script-build` (exit code: 101)
--- stdout
Working directory: /usr/src/linux-headers-3.19.0-58-generic
LLVM arguments: -Wp,-MD,/home/bluet/workspace/rust.ko/build/.libhello.a.d -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/4.8/include -I./arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -Iubuntu/include -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -m64 -DKBUILD_STR(s)=#s -Werror=implicit-int -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -Werror=strict-prototypes -DCONFIG_X86_X32_ABI -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0 -Wframe-larger-than=1024 -fstack-protector -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -DKBUILD_BASENAME=KBUILD_STR(libhello) -pg -DMODULE -DCC_USING_FENTRY -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -Dfalse=__false -Dtrue=__true -Du64=__u64 /home/bluet/workspace/rust.ko/target/x86_64-unknown-none-gnu/debug/build/linux-std-b8b7ab4d3d9bbe98/out/kernel-include.h
--- stderr
error: error: unknown argument: '-fno-delete-null-pointer-checks'
warning: warning: unknown warning option '-Wno-unused-but-set-variable'; did you mean '-Wno-unused-const-variable'? [-Wunknown-warning-option]
warning: include/asm-generic/int-ll64.h:25:28: warning: redefinition of typedef '__u64' is a C11 feature [-Wtypedef-redefinition]
thread '
RUST_BACKTRACE=1
for a backtrace.
make[2]: *** [/home/bluet/workspace/rust.ko/build/libhello.a] Error 101
make[1]: *** [module/home/bluet/workspace/rust.ko/build] Error 2
make: *** [all] Error 2
My env
bluet@Titania:/workspace/rust.ko$ uname -a14.04.1-Ubuntu SMP Fri Mar 18 19:05:43 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Linux Titania 3.19.0-58-generic #64
bluet@Titania:/workspace/rust.ko$ clang -v/workspace/rust.ko$ rustc -V
Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.8.4
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.9.3
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8.4
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8.4
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
bluet@Titania:
rustc 1.9.0-nightly (b678600ac 2016-03-29)
bluet@Titania:~/workspace/rust.ko$ cargo -V
cargo 0.10.0-nightly (d38f03b 2016-03-29)
`
$ rustup run nightly make KERNEL_BUILD_PATH=/lib/modules/4.17.11-arch1/build/ CARGO=/home/frol/.cargo/bin/xargo
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --sysroot /home/frol/.xargo -Z force-unstable-if-unmarked --target x86_64-unknown-none-gnu --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro` (exit code: 1)
--- stderr
error: Error loading target specification: Field target-c-int-width in target specification is required
|
= help: Use `--print target-list` for a list of built-in targets
I have updated the x86_64-unknown-none-gnu.json
file with the version from rust-barebones-kernel
, but still cannot get through error:
$ rustup run nightly make KERNEL_BUILD_PATH=/lib/modules/4.17.11-arch1/build/ CARGO=/home/frol/.cargo/bin/xargo
Compiling core v0.1.0 (https://github.com/phil-opp/nightly-libcore.git#54571deb)
Compiling kernel32-sys v0.2.2
Compiling log v0.4.3
Compiling clang-sys v0.5.4
error: Error loading target specification: Could not find specification for target "x86_64-unknown-none-gnu"
|
= help: Use `--print target-list` for a list of built-in targets
error: Could not compile `core`.
warning: build failed, waiting for other jobs to finish...
error: build failed
make[2]: *** [/mnt/storage/projects/linux-kernel-basecamp/rust.ko/build/Makefile:46: /mnt/storage/projects/linux-kernel-basecamp/rust.ko/build/libhello.a] Error 101
make[1]: *** [Makefile:1571: _module_/mnt/storage/projects/linux-kernel-basecamp/rust.ko/build] Error 2
make: *** [Makefile:36: all] Error 2
Can anyone help me?
Hi, I'm really excited about the newest patch, with bindgen support, but I'm getting errors when compiling it.
ld -m elf_x86_64 --gc-sections --entry=init_module --require-defined=cleanup_module -r -o /home/tbelaire/Code/rust.ko/build/hello.o /home/tbelaire/Code/rust.ko/build/src/module.o /home/tbelaire/Code/rust.ko/build/libhello.a
ld: unrecognized option '--require-defined=cleanup_module'
$ ld --version
GNU ld (GNU Binutils for Ubuntu) 2.24
Copyright 2013 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
My binutils package is up to date, should I be using a llvm linker or something?
Oh, and I needed to add in
"-fno-delete-null-pointer-checks",
to the CLANG_ARGS_BLACKLIST
, but that's fine.
There's no trivial solution. This article is a good discussion of the problem. I thought I'd mention it in case you want to update this, or in case someone else is debugging mysterious memory corruption.
I've been forced to remove the --gc-sections line, as it removes functions, including _Unwind_Resume
, causing errors at link time.
This is only apparent if you've started using something from the stdlib that can panic, as otherwise there are no references to _Unwind_Resume
, but for my rot13 branch, I have been unable to build with --gc-sections
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.