Skip to content

scripts: verify rust/libmacros.so ELF architecture compatibility - #872

Open
thomas7475 wants to merge 1 commit into
koverstreet:masterfrom
thomas7475:test-script
Open

thomas7475 wants to merge 1 commit into
koverstreet:masterfrom
thomas7475:test-script

Conversation

@thomas7475

Copy link
Copy Markdown

Summary

Adds an ELF architecture compatibility check for rust/libmacros.so to the DKMS Rust availability script (rust-is-available-dkms.sh).

Problem

When building out-of-tree Rust modules via DKMS, rustc dynamically loads procedural macro shared libraries (rust/libmacros.so) directly into its host process at compile time via dlopen().

If kernel headers or prebuilt kernel artifacts were cross-compiled on a different host architecture (e.g., compiled on x86_64 and installed on an aarch64 target system), host rustc cannot load the foreign .so. This causes DKMS builds to fail deep inside rustc with cryptic dynamic linking or crate loading errors (E0463).

This test catches architecture mismatches early and gracefully falls back to building the C-only module, logging the exact cause in the DKMS build log.

Implementation Details

  1. get_elf_arch(): Reads the first 20 bytes of an ELF header in a single POSIX od call, extracting ELF class, endianness, and e_machine (x86_64, aarch64, arm, riscv, loongarch64, etc.).
  2. get_host_arch(): Reuses get_elf_arch() on native host binaries ($HOSTRUSTC, $RUSTC, $CC, or /bin/sh). This guarantees 1:1 string parity between the running toolchain and libmacros.so, preventing false positives on 32-bit platforms (arm vs armv7l) or in chroot/container environments.
  3. Mismatch Fallback: If a mismatch is found ($host_arch != $macro_arch), skip() is triggered with a clear diagnostic:

    bcachefs: building without Rust — host architecture (aarch64) does not match rust/libmacros.so (x86_64); native kernel headers required

@thomas7475
thomas7475 force-pushed the test-script branch 3 times, most recently from 8828c85 to afce2b8 Compare August 23, 2026 20:04
@thomas7475
thomas7475 force-pushed the test-script branch 2 times, most recently from 55a3d52 to 8673013 Compare August 31, 2026 14:27
When building the bcachefs DKMS module on a system where Rust is enabled,
rustc dynamically loads the procedural macro shared library
(rust/libmacros.so) directly into its host process at compile time.

If kernel headers or prebuilt kernel artifacts were compiled on a host
with a different architecture (e.g. cross-compiled on x86_64 and installed
on an aarch64 system), rustc on the target host cannot load the alien
libmacros.so. This causes the DKMS build to fall back to the C-only module.

Add an ELF architecture compatibility check to rust-is-available-dkms.sh:

- Add helper function elf_arch_name() to map ELF class, endianness, and
  e_machine bytes to human-readable architecture names (x86_64,
  aarch64, arm, riscv, etc.).
- Inspect the ELF header of a native host binary ($HOSTRUSTC, $RUSTC, or
  /bin/sh) using od and compare its class, endianness, and machine type
  against rust/libmacros.so.
- If a mismatch is found, trigger skip() with a clear diagnostic:
  "host architecture ($host_arch) does not match rust/libmacros.so ($macro_arch); native kernel headers required"

This allows DKMS to catch cross-architecture header mismatches early and
gracefully fall back to building the C-only bcachefs module while
logging the exact reason in the build log.

# Conflicts:
#	fs/scripts/rust-is-available-dkms.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant