diff --git a/.travis.yml b/.travis.yml index 8deb01c5..a66a2c12 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ matrix: include: - - dist: trusty - dist: xenial language: rust @@ -34,3 +33,4 @@ after_failure: notifications: email: false + diff --git a/build.rs b/build.rs index 82491ffb..f5c45d5d 100644 --- a/build.rs +++ b/build.rs @@ -38,6 +38,7 @@ const OPAQUE_TYPES: &[&str] = &[ // This needs to be opaque because it's both packed and aligned, which rustc // doesn't support yet. See https://github.com/rust-lang/rust/issues/59154 // and https://github.com/rust-lang/rust-bindgen/issues/1538 + "desc_struct", "xregs_state", ]; @@ -59,6 +60,7 @@ fn main() { ) .unwrap(); + builder = builder.clang_arg("--target=x86_64-linux-kernel-module"); for arg in shlex::split(&output).unwrap() { builder = builder.clang_arg(arg.to_string()); } @@ -88,6 +90,7 @@ fn main() { let mut builder = cc::Build::new(); println!("cargo:rerun-if-env-changed=CLANG"); builder.compiler(env::var("CLANG").unwrap_or("clang".to_string())); + builder.target("x86_64-linux-kernel-module"); builder.warnings(false); builder.file("src/helpers.c"); for arg in shlex::split(&output).unwrap() { diff --git a/hello-world/Makefile b/hello-world/Makefile index fccc18c3..21e4fbde 100644 --- a/hello-world/Makefile +++ b/hello-world/Makefile @@ -1,6 +1,11 @@ +ifneq ($(KERNELRELEASE),) obj-m := helloworld.o -helloworld-objs := target/x86_64-linux-kernel-module/debug/libhello_world.a +helloworld-objs := libhello_world.o EXTRA_LDFLAGS += --entry=init_module + +$(M)/libhello_world.o: target/x86_64-linux-kernel-module/debug/libhello_world.a + $(LD) -r -o $@ --whole-archive $^ +else KDIR ?= /lib/modules/$(shell uname -r)/build all: @@ -8,3 +13,4 @@ all: clean: $(MAKE) -C $(KDIR) M=$(CURDIR) clean +endif diff --git a/hello-world/src/lib.rs b/hello-world/src/lib.rs index 2413fe0a..1799c1ff 100644 --- a/hello-world/src/lib.rs +++ b/hello-world/src/lib.rs @@ -1,9 +1,9 @@ #![no_std] -#![feature(alloc)] +#![feature(alloc, const_str_as_bytes)] extern crate alloc; use alloc::borrow::ToOwned; -use alloc::String; +use alloc::string::String; #[macro_use] extern crate linux_kernel_module; @@ -27,6 +27,7 @@ impl Drop for HelloWorldModule { println!("Goodbye kernel module!"); } } + kernel_module!( HelloWorldModule, author: "Alex Gaynor and Geoffrey Thomas", diff --git a/src/allocator.rs b/src/allocator.rs index ee17f353..45efb943 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -22,6 +22,6 @@ unsafe impl GlobalAlloc for KernelAllocator { } #[alloc_error_handler] -extern "C" fn oom(_layout: Layout) -> ! { +fn oom(_layout: Layout) -> ! { panic!("Out of memory!"); } diff --git a/src/lib.rs b/src/lib.rs index c06448c6..a46ae77f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,9 +60,9 @@ macro_rules! kernel_module { #[link_section = ".modinfo"] #[allow(non_upper_case_globals)] // TODO: Generate a name the same way the kernel's `__MODULE_INFO` does. - // TODO: This needs to be a `&'static [u8]`, since the kernel defines this as a - // `const char []`. - pub static $name: &'static str = concat!(stringify!($name), "=", $value); + // TODO: This needs to be a `[u8; _]`, since the kernel defines this as a `const char []`. + // See https://github.com/rust-lang/rfcs/pull/2545 + pub static $name: &'static [u8] = concat!(stringify!($name), "=", $value, '\0').as_bytes(); }; } diff --git a/tests/Makefile b/tests/Makefile index acfd9592..8a2f876b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,11 @@ +ifneq ($(KERNELRELEASE),) obj-m := testmodule.o -testmodule-objs := $(TEST_LIBRARY) +testmodule-objs := $(TEST_LIBRARY_OBJECT) EXTRA_LDFLAGS += --entry=init_module + +$(M)/$(TEST_LIBRARY_OBJECT): target/x86_64-linux-kernel-module/debug/$(TEST_LIBRARY_ARCHIVE) + $(LD) -r -o $@ --whole-archive $^ +else KDIR ?= /lib/modules/$(shell uname -r)/build all: @@ -8,3 +13,4 @@ all: clean: $(MAKE) -C $(KDIR) M=$(CURDIR) clean +endif diff --git a/tests/printk/src/lib.rs b/tests/printk/src/lib.rs index 3db1517a..dc0d8f4d 100644 --- a/tests/printk/src/lib.rs +++ b/tests/printk/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![feature(const_str_as_bytes)] #[macro_use] extern crate linux_kernel_module; diff --git a/tests/run_tests.py b/tests/run_tests.py index f8fae900..681f6388 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -46,14 +46,13 @@ def main(): path ) ) + library_path, _ = os.path.splitext(os.path.basename(module)) + library_object = library_path + ".o" + library_archive = library_path + ".a" run( "make", "-C", BASE_DIR, - "TEST_LIBRARY={}".format( - os.path.join( - "target/x86_64-linux-kernel-module/debug/", - os.path.basename(module) - ) - ), + "TEST_LIBRARY_OBJECT={}".format(library_object), + "TEST_LIBRARY_ARCHIVE={}".format(library_archive), ) run( "rustc", diff --git a/tests/sysctl/src/lib.rs b/tests/sysctl/src/lib.rs index 45fbc871..84e9a052 100644 --- a/tests/sysctl/src/lib.rs +++ b/tests/sysctl/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![feature(const_str_as_bytes)] use core::sync::atomic::AtomicBool; diff --git a/x86_64-linux-kernel-module.json b/x86_64-linux-kernel-module.json index 93159324..fd5353af 100644 --- a/x86_64-linux-kernel-module.json +++ b/x86_64-linux-kernel-module.json @@ -24,6 +24,7 @@ }, "relocation-model": "static", "relro-level": "full", + "needs-plt": true, "target-c-int-width": "32", "target-endian": "little", "target-family": "unix",