@@ -36,9 +36,9 @@ pub use crate::types::{CStr, Mode};
3636///
3737/// linux_kernel_module::kernel_module!(
3838/// MyKernelModule,
39- /// author: "Fish in a Barrel Contributors",
40- /// description: "My very own kernel module!",
41- /// license: "GPL"
39+ /// author: b "Fish in a Barrel Contributors",
40+ /// description: b "My very own kernel module!",
41+ /// license: b "GPL"
4242/// );
4343#[ macro_export]
4444macro_rules! kernel_module {
@@ -72,13 +72,43 @@ macro_rules! kernel_module {
7272 ) *
7373 } ;
7474
75- ( @attribute $name: ident, $value: expr) => {
75+ // TODO: The modinfo attributes below depend on the compiler placing
76+ // the variables in order in the .modinfo section, so that you end up
77+ // with b"key=value\0" in order in the section. This is a reasonably
78+ // standard trick in C, but I'm not sure that rustc guarantees it.
79+ //
80+ // Ideally we'd be able to use concat_bytes! + stringify_bytes! +
81+ // some way of turning a string literal (or at least a string
82+ // literal token) into a bytes literal, and get a single static
83+ // [u8; * N] with the whole thing, but those don't really exist yet.
84+ // Most of the alternatives (e.g. .as_bytes() as a const fn) give
85+ // you a pointer, not an array, which isn't right.
86+
87+ ( @attribute author, $value: expr) => {
88+ #[ link_section = ".modinfo" ]
89+ pub static AUTHOR_KEY : [ u8 ; 7 ] = * b"author=" ;
90+ #[ link_section = ".modinfo" ]
91+ pub static AUTHOR_VALUE : [ u8 ; $value. len( ) ] = * $value;
92+ #[ link_section = ".modinfo" ]
93+ pub static AUTHOR_NUL : [ u8 ; 1 ] = * b"\0 " ;
94+ } ;
95+
96+ ( @attribute description, $value: expr) => {
97+ #[ link_section = ".modinfo" ]
98+ pub static DESCRIPTION_KEY : [ u8 ; 12 ] = * b"description=" ;
99+ #[ link_section = ".modinfo" ]
100+ pub static DESCRIPTION_VALUE : [ u8 ; $value. len( ) ] = * $value;
101+ #[ link_section = ".modinfo" ]
102+ pub static DESCRIPTION_NUL : [ u8 ; 1 ] = * b"\0 " ;
103+ } ;
104+
105+ ( @attribute license, $value: expr) => {
106+ #[ link_section = ".modinfo" ]
107+ pub static LICENSE_KEY : [ u8 ; 8 ] = * b"license=" ;
108+ #[ link_section = ".modinfo" ]
109+ pub static LICENSE_VALUE : [ u8 ; $value. len( ) ] = * $value;
76110 #[ link_section = ".modinfo" ]
77- #[ allow( non_upper_case_globals) ]
78- // TODO: Generate a name the same way the kernel's `__MODULE_INFO` does.
79- // TODO: This needs to be a `[u8; _]`, since the kernel defines this as a `const char []`.
80- // See https://github.com/rust-lang/rfcs/pull/2545
81- pub static $name: & ' static [ u8 ] = concat!( stringify!( $name) , "=" , $value, '\0' ) . as_bytes( ) ;
111+ pub static LICENSE_NUL : [ u8 ; 1 ] = * b"\0 " ;
82112 } ;
83113}
84114
0 commit comments