Skip to content

Commit

Permalink
rust: remove __aeabi_uldivmod stub
Browse files Browse the repository at this point in the history
Rust's libcore genuinely needs 64-bit division for formatting, but we
can't change these to use kernel's division routine; so `__aeabi_uldivmod`
calls will be generated.

Instead of defining a stub that can affect all code, use objcopy to redirect
these calls and forward them to `div64_u64_rem`.

Signed-off-by: Gary Guo <gary@garyguo.net>
  • Loading branch information
nbdd0121 committed May 26, 2022
1 parent 9a87c30 commit 6805b2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions rust/Makefile
Expand Up @@ -370,6 +370,7 @@ rust-analyzer:

$(obj)/core.o: private skip_clippy = 1
$(obj)/core.o: private skip_flags = -Dunreachable_pub
$(obj)/core.o: private rustc_objcopy = --redefine-sym __aeabi_uldivmod=rust_aeabi_uldivmod
$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs $(obj)/target.json FORCE
$(call if_changed_dep,rustc_library)
Expand Down
25 changes: 22 additions & 3 deletions rust/compiler_builtins.rs
Expand Up @@ -20,6 +20,7 @@
//! [`compiler-rt`]: https://compiler-rt.llvm.org/

#![feature(compiler_builtins)]
#![feature(naked_functions)]
#![compiler_builtins]
#![no_builtins]
#![no_std]
Expand Down Expand Up @@ -51,6 +52,24 @@ define_panicking_intrinsics!("`u128` should not be used", {
});

#[cfg(target_arch = "arm")]
define_panicking_intrinsics!("`u64` division/modulo should not be used", {
__aeabi_uldivmod,
});
#[doc(hidden)]
#[naked]
#[no_mangle]
pub extern "C" fn rust_aeabi_uldivmod() {
// Adapted from compiler-rt:
// https://github.com/llvm/llvm-project/blob/a80e65e00ada7a9c16acf17a5fd40b4f12ced3a8/compiler-rt/lib/builtins/arm/aeabi_uldivmod.S
unsafe {
core::arch::asm!(
"push {{r6, lr}}",
"sub sp, sp, #16",
"add r6, sp, #8",
"str r6, [sp]",
"bl div64_u64_rem",
"ldr r2, [sp, #8]",
"ldr r3, [sp, #12]",
"add sp, sp, #16",
"pop {{r6, pc}}",
options(noreturn)
);
}
}

0 comments on commit 6805b2a

Please sign in to comment.