Skip to content

Commit 67b7b7f

Browse files
committed
Support #[alloc_error_handler] without the allocator shim
Currently it is possible to avoid linking the allocator shim when __rust_no_alloc_shim_is_unstable_v2 is defined when linking rlibs directly as some build systems need. However this requires liballoc to be compiled with --cfg no_global_oom_handling, which places huge restrictions on what functions you can call and makes it impossible to use libstd. Or alternatively you have to define __rust_alloc_error_handler and (when using libstd) __rust_alloc_error_handler_should_panic using #[rustc_std_internal_symbol]. With this commit you can either use libstd and define __rust_alloc_error_handler_should_panic or not use libstd and use #[alloc_error_handler] instead. Both options are still unstable though. Eventually the alloc_error_handler may either be removed entirely (though the PR for that has been stale for years now) or we may start using weak symbols for it instead. For the latter case this commit is a prerequisite anyway.
1 parent 0e4df74 commit 67b7b7f

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/allocator.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
55
use rustc_ast::expand::allocator::{
6-
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
7-
alloc_error_handler_name, default_fn_name, global_fn_name,
6+
ALLOC_ERROR_HANDLER, ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
7+
default_fn_name, global_fn_name,
88
};
99
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
1010
use rustc_session::config::OomStrategy;
@@ -72,17 +72,19 @@ fn codegen_inner(
7272
}
7373
}
7474

75-
let sig = Signature {
76-
call_conv: module.target_config().default_call_conv,
77-
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
78-
returns: vec![],
79-
};
80-
crate::common::create_wrapper_function(
81-
module,
82-
sig,
83-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
84-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
85-
);
75+
if alloc_error_handler_kind == AllocatorKind::Default {
76+
let sig = Signature {
77+
call_conv: module.target_config().default_call_conv,
78+
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
79+
returns: vec![],
80+
};
81+
crate::common::create_wrapper_function(
82+
module,
83+
sig,
84+
&mangle_internal_symbol(tcx, &global_fn_name(ALLOC_ERROR_HANDLER)),
85+
&mangle_internal_symbol(tcx, &default_fn_name(ALLOC_ERROR_HANDLER)),
86+
);
87+
}
8688

8789
{
8890
let sig = Signature {

0 commit comments

Comments
 (0)