Skip to content

Commit 0fb3dcb

Browse files
committed
rust: rework & improve robustness of is_rust_module.sh
This also simplifies the file and makes it work under any POSIX shell. Link: Rust-for-Linux#657 Reported-by: Nathan Chancellor <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 9246d81 commit 0fb3dcb

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

rust/macros/module.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
503503
/// Used by the printing macros, e.g. [`info!`].
504504
const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
505505
506+
/// The \"Rust loadable module\" mark, for `scripts/is_rust_module.sh`.
507+
//
508+
// This may be best done another way later on, e.g. as a new modinfo
509+
// key or a new section. For the moment, keep it simple.
510+
#[cfg(MODULE)]
511+
#[doc(hidden)]
512+
#[used]
513+
static __IS_RUST_MODULE: () = ();
514+
506515
static mut __MOD: Option<{type_}> = None;
507516
508517
// SAFETY: `__this_module` is constructed by the kernel at load time and will not be freed until the module is unloaded.

scripts/is_rust_module.sh

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
#!/bin/bash
1+
#!/bin/sh
22
# SPDX-License-Identifier: GPL-2.0
33
#
4-
# is_rust_module.sh MOD.ko
4+
# is_rust_module.sh module.ko
55
#
6-
# Returns 0 if MOD.ko is a rust module, 1 otherwise.
6+
# Returns `0` if `module.ko` is a Rust module, `1` otherwise.
77

88
set -e
9-
module="$*"
109

11-
while IFS= read -r line
12-
do
13-
# Any symbol beginning with "_R" is a v0 mangled rust symbol
14-
if [[ $line =~ ^[0-9a-fA-F]+[[:space:]]+[uUtTrR][[:space:]]+_R[^[:space:]]+$ ]]; then
15-
exit 0
16-
fi
17-
done < <(${NM} "$module")
18-
19-
exit 1
10+
# Using the `16_` prefix ensures other symbols with the same substring
11+
# are not picked up (even if it would be unlikely). The last part is
12+
# used just in case LLVM decides to use the `.` suffix.
13+
${NM} "$*" | grep -qE '^[0-9a-fA-F]+ r _R[^[:space:]]+16___IS_RUST_MODULE[^[:space:]]*$'

0 commit comments

Comments
 (0)