Skip to content

Commit dd930a3

Browse files
Amanieuantoyo
authored andcommitted
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 4c0a6e6 commit dd930a3

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
@@ -9,7 +9,7 @@ use rustc_span::symbol::sym;
99

1010
use crate::GccContext;
1111

12-
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
12+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) {
1313
let context = &mods.context;
1414
let usize =
1515
match tcx.sess.target.pointer_width {
@@ -99,14 +99,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
9999
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
100100
}
101101

102-
let kind =
103-
if has_alloc_error_handler {
104-
AllocatorKind::Global
105-
}
106-
else {
107-
AllocatorKind::Default
108-
};
109-
let callee = kind.fn_name(sym::oom);
102+
let callee = alloc_error_handler_kind.fn_name(sym::oom);
110103
let args: Vec<_> = types.iter().enumerate()
111104
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
112105
.collect();

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ impl CodegenBackend for GccCodegenBackend {
155155
}
156156

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

0 commit comments

Comments
 (0)