Skip to content

Commit 87864f3

Browse files
committed
Use weak linkage for the alloc error handler too
1 parent 82cb98f commit 87864f3

File tree

17 files changed

+40
-187
lines changed

17 files changed

+40
-187
lines changed

compiler/rustc_ast/src/expand/allocator.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ pub fn global_fn_name(base: Symbol) -> String {
1111
format!("__rust_{base}")
1212
}
1313

14-
pub fn alloc_error_handler_name(alloc_error_handler_kind: AllocatorKind) -> &'static str {
15-
match alloc_error_handler_kind {
16-
AllocatorKind::Global => "__rg_oom",
17-
AllocatorKind::Default => "__rdl_oom",
18-
}
19-
}
20-
2114
pub const NO_ALLOC_SHIM_IS_UNSTABLE: &str = "__rust_no_alloc_shim_is_unstable_v2";
2215

2316
pub enum AllocatorTy {

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) fn expand(
5656
}
5757

5858
// #[rustc_std_internal_symbol]
59-
// unsafe fn __rg_oom(size: usize, align: usize) -> ! {
59+
// unsafe fn __rust_alloc_error_handler(size: usize, align: usize) -> ! {
6060
// handler(core::alloc::Layout::from_size_align_unchecked(size, align))
6161
// }
6262
fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span) -> Stmt {
@@ -85,7 +85,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
8585
let kind = ItemKind::Fn(Box::new(Fn {
8686
defaultness: ast::Defaultness::Final,
8787
sig,
88-
ident: Ident::from_str_and_span("__rg_oom", span),
88+
ident: Ident::from_str_and_span("__rust_alloc_error_handler", span),
8989
generics: Generics::default(),
9090
contract: None,
9191
body,

compiler/rustc_codegen_cranelift/src/allocator.rs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// Adapted from rustc
33

44
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
5-
use rustc_ast::expand::allocator::{
6-
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
7-
};
5+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
86
use rustc_codegen_ssa::base::needs_allocator_shim;
97
use rustc_session::config::OomStrategy;
108
use rustc_symbol_mangling::mangle_internal_symbol;
@@ -14,46 +12,15 @@ use crate::prelude::*;
1412
/// Returns whether an allocator shim was created
1513
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
1614
if needs_allocator_shim(tcx) {
17-
codegen_inner(
18-
tcx,
19-
module,
20-
tcx.alloc_error_handler_kind(()).unwrap(),
21-
tcx.sess.opts.unstable_opts.oom,
22-
);
15+
codegen_inner(tcx, module, tcx.sess.opts.unstable_opts.oom);
2316
true
2417
} else {
2518
false
2619
}
2720
}
2821

29-
fn codegen_inner(
30-
tcx: TyCtxt<'_>,
31-
module: &mut dyn Module,
32-
alloc_error_handler_kind: AllocatorKind,
33-
oom_strategy: OomStrategy,
34-
) {
35-
let usize_ty = module.target_config().pointer_type();
36-
37-
let sig = Signature {
38-
call_conv: module.target_config().default_call_conv,
39-
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
40-
returns: vec![],
41-
};
42-
crate::common::create_wrapper_function(
43-
module,
44-
sig,
45-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
46-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
47-
);
48-
49-
let data_id = module
50-
.declare_data(
51-
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
52-
Linkage::Export,
53-
false,
54-
false,
55-
)
56-
.unwrap();
22+
fn codegen_inner(tcx: TyCtxt<'_>, module: &mut dyn Module, oom_strategy: OomStrategy) {
23+
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
5724
let mut data = DataDescription::new();
5825
data.set_align(1);
5926
let val = oom_strategy.should_panic();

compiler/rustc_codegen_gcc/src/allocator.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use gccjit::{Context, FunctionType, GlobalKind, ToRValue, Type};
22
#[cfg(feature = "master")]
33
use gccjit::{FnAttribute, VarAttribute};
4-
use rustc_ast::expand::allocator::{
5-
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
6-
};
7-
use rustc_middle::bug;
4+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
85
use rustc_middle::ty::TyCtxt;
96
use rustc_session::config::OomStrategy;
107
use rustc_symbol_mangling::mangle_internal_symbol;
@@ -13,31 +10,10 @@ use crate::GccContext;
1310
#[cfg(feature = "master")]
1411
use crate::base::symbol_visibility_to_gcc;
1512

16-
pub(crate) unsafe fn codegen(
17-
tcx: TyCtxt<'_>,
18-
mods: &mut GccContext,
19-
_module_name: &str,
20-
alloc_error_handler_kind: AllocatorKind,
21-
) {
13+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str) {
2214
let context = &mods.context;
23-
let usize = match tcx.sess.target.pointer_width {
24-
16 => context.new_type::<u16>(),
25-
32 => context.new_type::<u32>(),
26-
64 => context.new_type::<u64>(),
27-
tws => bug!("Unsupported target word size for int: {}", tws),
28-
};
2915
let i8 = context.new_type::<i8>();
3016

31-
// FIXME(bjorn3): Add noreturn attribute
32-
create_wrapper_function(
33-
tcx,
34-
context,
35-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
36-
Some(&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind))),
37-
&[usize, usize],
38-
None,
39-
);
40-
4117
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
4218
let global = context.new_global(None, GlobalKind::Exported, i8, name);
4319
#[cfg(feature = "master")]

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ use back::lto::{ThinBuffer, ThinData};
9595
use gccjit::{CType, Context, OptimizationLevel};
9696
#[cfg(feature = "master")]
9797
use gccjit::{TargetInfo, Version};
98-
use rustc_ast::expand::allocator::AllocatorKind;
9998
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
10099
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
101100
use rustc_codegen_ssa::back::write::{
@@ -277,12 +276,7 @@ fn new_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> {
277276
}
278277

279278
impl ExtraBackendMethods for GccCodegenBackend {
280-
fn codegen_allocator(
281-
&self,
282-
tcx: TyCtxt<'_>,
283-
module_name: &str,
284-
alloc_error_handler_kind: AllocatorKind,
285-
) -> Self::Module {
279+
fn codegen_allocator(&self, tcx: TyCtxt<'_>, module_name: &str) -> Self::Module {
286280
let mut mods = GccContext {
287281
context: Arc::new(SyncContext::new(new_context(tcx))),
288282
relocation_model: tcx.sess.relocation_model(),
@@ -291,7 +285,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
291285
};
292286

293287
unsafe {
294-
allocator::codegen(tcx, &mut mods, module_name, alloc_error_handler_kind);
288+
allocator::codegen(tcx, &mut mods, module_name);
295289
}
296290
mods
297291
}

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use libc::c_uint;
2-
use rustc_ast::expand::allocator::{
3-
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
4-
};
2+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
53
use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
6-
use rustc_middle::bug;
74
use rustc_middle::ty::TyCtxt;
85
use rustc_session::config::{DebugInfo, OomStrategy};
96
use rustc_symbol_mangling::mangle_internal_symbol;
@@ -13,31 +10,9 @@ use crate::declare::declare_simple_fn;
1310
use crate::llvm::{self, False, True, Type};
1411
use crate::{SimpleCx, attributes, debuginfo};
1512

16-
pub(crate) unsafe fn codegen(
17-
tcx: TyCtxt<'_>,
18-
cx: SimpleCx<'_>,
19-
module_name: &str,
20-
alloc_error_handler_kind: AllocatorKind,
21-
) {
22-
let usize = match tcx.sess.target.pointer_width {
23-
16 => cx.type_i16(),
24-
32 => cx.type_i32(),
25-
64 => cx.type_i64(),
26-
tws => bug!("Unsupported target word size for int: {}", tws),
27-
};
13+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, cx: SimpleCx<'_>, module_name: &str) {
2814
let i8 = cx.type_i8();
2915

30-
// rust alloc error handler
31-
create_wrapper_function(
32-
tcx,
33-
&cx,
34-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
35-
Some(&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind))),
36-
&[usize, usize], // size, align
37-
None,
38-
true,
39-
);
40-
4116
unsafe {
4217
// __rust_alloc_error_handler_should_panic
4318
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use back::write::{create_informational_target_machine, create_target_machine};
2828
use context::SimpleCx;
2929
use errors::{AutoDiffWithoutLTO, ParseTargetMachineConfig};
3030
use llvm_util::target_config;
31-
use rustc_ast::expand::allocator::AllocatorKind;
3231
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
3332
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
3433
use rustc_codegen_ssa::back::write::{
@@ -104,17 +103,12 @@ impl Drop for TimeTraceProfiler {
104103
}
105104

106105
impl ExtraBackendMethods for LlvmCodegenBackend {
107-
fn codegen_allocator<'tcx>(
108-
&self,
109-
tcx: TyCtxt<'tcx>,
110-
module_name: &str,
111-
alloc_error_handler_kind: AllocatorKind,
112-
) -> ModuleLlvm {
106+
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str) -> ModuleLlvm {
113107
let module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
114108
let cx =
115109
SimpleCx::new(module_llvm.llmod(), &module_llvm.llcx, tcx.data_layout.pointer_size);
116110
unsafe {
117-
allocator::codegen(tcx, cx, module_name, alloc_error_handler_kind);
111+
allocator::codegen(tcx, cx, module_name);
118112
}
119113
module_llvm
120114
}

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ fn exported_symbols_provider_local<'tcx>(
214214
// Mark allocator shim symbols as exported only if they were generated.
215215
if needs_allocator_shim(tcx) {
216216
for symbol_name in [
217-
mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
218217
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
219218
mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
220219
] {

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -708,15 +708,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
708708
if needs_allocator_shim(tcx) {
709709
let llmod_id =
710710
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
711-
let module_llvm = tcx.sess.time("write_allocator_module", || {
712-
backend.codegen_allocator(
713-
tcx,
714-
&llmod_id,
715-
// If allocator_kind is Some then alloc_error_handler_kind must
716-
// also be Some.
717-
tcx.alloc_error_handler_kind(()).unwrap(),
718-
)
719-
});
711+
let module_llvm =
712+
tcx.sess.time("write_allocator_module", || backend.codegen_allocator(tcx, &llmod_id));
720713

721714
ongoing_codegen.wait_for_signal_to_codegen_item();
722715
ongoing_codegen.check_for_errors(tcx.sess);

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::any::Any;
22
use std::hash::Hash;
33

4-
use rustc_ast::expand::allocator::AllocatorKind;
54
use rustc_data_structures::fx::FxIndexMap;
65
use rustc_data_structures::sync::{DynSend, DynSync};
76
use rustc_metadata::EncodedMetadata;
@@ -103,12 +102,7 @@ pub trait CodegenBackend {
103102
pub trait ExtraBackendMethods:
104103
CodegenBackend + WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
105104
{
106-
fn codegen_allocator<'tcx>(
107-
&self,
108-
tcx: TyCtxt<'tcx>,
109-
module_name: &str,
110-
alloc_error_handler_kind: AllocatorKind,
111-
) -> Self::Module;
105+
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str) -> Self::Module;
112106

113107
/// This generates the codegen unit and returns it along with
114108
/// a `u64` giving an estimate of the unit's processing cost.

0 commit comments

Comments
 (0)