Skip to content

Commit cb7d9de

Browse files
nbdd0121ojeda
authored andcommitted
rust: compiler_builtins: make stubs non-global
Currently we define a number of stubs for compiler-builtin intrinsics that compiled libcore generates. The defined stubs are weak so they will not conflict with genuine implementation of these intrinsics, but their effect is global and will cause non-libcore code that accidently generate these intrinsics calls compile and bug on runtime. Instead of defining a stub that can affect all code, this patch uses objcopy's `--redefine-sym` flag to redirect these calls (from libcore only) to a prefixed version (e.g. redirect `__multi3` to `__rust_multi3`), so we can define panciking stubs that are only visible to libcore. This patch was previously discussed on GitHub [1]. This approach was also independently proposed by Nick Desaulniers in [2]. Link: Rust-for-Linux/linux#779 [1] Link: https://lore.kernel.org/lkml/CAKwvOdkc0Qhwu=gfe1+H23TnAa6jnO6A3ZCO687dH6mSrATmDA@mail.gmail.com/ Suggested-by: Nick Desaulniers <[email protected]> Acked-by: Nick Desaulniers <[email protected]> Signed-off-by: Gary Guo <[email protected]> Reviewed-by: Wei Liu <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 8909a80 commit cb7d9de

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

rust/Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,22 @@ rust-analyzer:
360360
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
361361
$(RUST_LIB_SRC) > $(objtree)/rust-project.json
362362

363+
redirect-intrinsics = \
364+
__eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \
365+
__unorddf2 \
366+
__muloti4 __multi3 \
367+
__udivmodti4 __udivti3 __umodti3
368+
369+
ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
370+
# These intrinsics are defined for ARM64 and RISCV64
371+
redirect-intrinsics += \
372+
__ashrti3 \
373+
__ashlti3 __lshrti3
374+
endif
375+
363376
$(obj)/core.o: private skip_clippy = 1
364377
$(obj)/core.o: private skip_flags = -Dunreachable_pub
378+
$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
365379
$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
366380
$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs $(obj)/target.json FORCE
367381
$(call if_changed_dep,rustc_library)

rust/compiler_builtins.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ macro_rules! define_panicking_intrinsics(
2828
($reason: tt, { $($ident: ident, )* }) => {
2929
$(
3030
#[doc(hidden)]
31-
#[no_mangle]
31+
#[export_name = concat!("__rust", stringify!($ident))]
3232
pub extern "C" fn $ident() {
3333
panic!($reason);
3434
}
@@ -61,3 +61,6 @@ define_panicking_intrinsics!("`u128` should not be used", {
6161
__udivti3,
6262
__umodti3,
6363
});
64+
65+
// NOTE: if you are adding a new intrinsic here, you should also add it to
66+
// `redirect-intrinsics` in `rust/Makefile`.

0 commit comments

Comments
 (0)