Skip to content

Commit 5a1c8e8

Browse files
committed
Rewrite implementation of #[alloc_error_handler]
The new implementation doesn't use weak lang items and instead changes `#[alloc_error_handler]` to an attribute macro just like `#[global_allocator]`. The attribute will generate the `__rg_oom` function which is called by the compiler-generated `__rust_alloc_error_handler`. If no `__rg_oom` function is defined in any crate then the compiler shim will call `__rdl_oom` in the alloc crate which will simply panic. This also fixes link errors with `-C link-dead-code` with `default_alloc_error_handler`: `__rg_oom` was previously defined in the alloc crate and would attempt to reference the `oom` lang item, even if it didn't exist. This worked as long as `__rg_oom` was excluded from linking since it was not called. This is a prerequisite for the stabilization of `default_alloc_error_handler` (rust-lang#102318).
1 parent a1d984c commit 5a1c8e8

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

src/allocator.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::symbol::sym;
77

88
use crate::GccContext;
99

10-
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
10+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) {
1111
let context = &mods.context;
1212
let usize =
1313
match tcx.sess.target.pointer_width {
@@ -90,14 +90,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
9090
.collect();
9191
let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
9292

93-
let kind =
94-
if has_alloc_error_handler {
95-
AllocatorKind::Global
96-
}
97-
else {
98-
AllocatorKind::Default
99-
};
100-
let callee = kind.fn_name(sym::oom);
93+
let callee = alloc_error_handler_kind.fn_name(sym::oom);
10194
let args: Vec<_> = types.iter().enumerate()
10295
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
10396
.collect();

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ impl CodegenBackend for GccCodegenBackend {
153153
}
154154

155155
impl ExtraBackendMethods for GccCodegenBackend {
156-
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) -> Self::Module {
156+
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) -> Self::Module {
157157
let mut mods = GccContext {
158158
context: Context::default(),
159159
};
160-
unsafe { allocator::codegen(tcx, &mut mods, module_name, kind, has_alloc_error_handler); }
160+
unsafe { allocator::codegen(tcx, &mut mods, module_name, kind, alloc_error_handler_kind); }
161161
mods
162162
}
163163

0 commit comments

Comments
 (0)