Skip to content

Commit 4f0dcf6

Browse files
committed
Use weak linkage for the alloc error handler too
1 parent 4e63d6e commit 4f0dcf6

File tree

18 files changed

+56
-341
lines changed

18 files changed

+56
-341
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";
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
@@ -1,9 +1,7 @@
11
//! Allocator shim
22
// Adapted from rustc
33

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

28-
fn codegen_inner(
29-
tcx: TyCtxt<'_>,
30-
module: &mut dyn Module,
31-
alloc_error_handler_kind: AllocatorKind,
32-
oom_strategy: OomStrategy,
33-
) {
34-
let usize_ty = module.target_config().pointer_type();
35-
36-
let sig = Signature {
37-
call_conv: module.target_config().default_call_conv,
38-
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
39-
returns: vec![],
40-
};
41-
crate::common::create_wrapper_function(
42-
module,
43-
sig,
44-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
45-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
46-
);
47-
48-
let data_id = module
49-
.declare_data(
50-
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
51-
Linkage::Export,
52-
false,
53-
false,
54-
)
55-
.unwrap();
21+
fn codegen_inner(tcx: TyCtxt<'_>, module: &mut dyn Module, oom_strategy: OomStrategy) {
22+
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
5623
let mut data = DataDescription::new();
5724
data.set_align(1);
5825
let val = oom_strategy.should_panic();
Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use gccjit::{Context, FunctionType, GlobalKind, ToRValue, Type};
1+
use gccjit::GlobalKind;
22
#[cfg(feature = "master")]
3-
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;
3+
use gccjit::VarAttribute;
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-
&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")]
@@ -57,71 +33,3 @@ pub(crate) unsafe fn codegen(
5733
let value = context.new_rvalue_from_int(i8, 0);
5834
global.global_set_initializer_rvalue(value);
5935
}
60-
61-
fn create_wrapper_function(
62-
tcx: TyCtxt<'_>,
63-
context: &Context<'_>,
64-
from_name: &str,
65-
to_name: &str,
66-
types: &[Type<'_>],
67-
output: Option<Type<'_>>,
68-
) {
69-
let void = context.new_type::<()>();
70-
71-
let args: Vec<_> = types
72-
.iter()
73-
.enumerate()
74-
.map(|(index, typ)| context.new_parameter(None, *typ, format!("param{}", index)))
75-
.collect();
76-
let func = context.new_function(
77-
None,
78-
FunctionType::Exported,
79-
output.unwrap_or(void),
80-
&args,
81-
from_name,
82-
false,
83-
);
84-
85-
#[cfg(feature = "master")]
86-
func.add_attribute(FnAttribute::Visibility(symbol_visibility_to_gcc(
87-
tcx.sess.default_visibility(),
88-
)));
89-
90-
if tcx.sess.must_emit_unwind_tables() {
91-
// TODO(antoyo): emit unwind tables.
92-
}
93-
94-
let args: Vec<_> = types
95-
.iter()
96-
.enumerate()
97-
.map(|(index, typ)| context.new_parameter(None, *typ, format!("param{}", index)))
98-
.collect();
99-
let callee = context.new_function(
100-
None,
101-
FunctionType::Extern,
102-
output.unwrap_or(void),
103-
&args,
104-
to_name,
105-
false,
106-
);
107-
#[cfg(feature = "master")]
108-
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
109-
110-
let block = func.new_block("entry");
111-
112-
let args = args
113-
.iter()
114-
.enumerate()
115-
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
116-
.collect::<Vec<_>>();
117-
let ret = context.new_call(None, callee, &args);
118-
//llvm::LLVMSetTailCall(ret, True);
119-
if output.is_some() {
120-
block.end_with_return(None, ret);
121-
} else {
122-
block.end_with_void_return(None);
123-
}
124-
125-
// TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
126-
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
127-
}

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ use back::lto::{ThinBuffer, ThinData};
9393
use gccjit::{CType, Context, OptimizationLevel};
9494
#[cfg(feature = "master")]
9595
use gccjit::{TargetInfo, Version};
96-
use rustc_ast::expand::allocator::AllocatorKind;
9796
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
9897
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
9998
use rustc_codegen_ssa::back::write::{
@@ -274,12 +273,7 @@ fn new_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> {
274273
}
275274

276275
impl ExtraBackendMethods for GccCodegenBackend {
277-
fn codegen_allocator(
278-
&self,
279-
tcx: TyCtxt<'_>,
280-
module_name: &str,
281-
alloc_error_handler_kind: AllocatorKind,
282-
) -> Self::Module {
276+
fn codegen_allocator(&self, tcx: TyCtxt<'_>, module_name: &str) -> Self::Module {
283277
let mut mods = GccContext {
284278
context: Arc::new(SyncContext::new(new_context(tcx))),
285279
relocation_model: tcx.sess.relocation_model(),
@@ -288,7 +282,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
288282
};
289283

290284
unsafe {
291-
allocator::codegen(tcx, &mut mods, module_name, alloc_error_handler_kind);
285+
allocator::codegen(tcx, &mut mods, module_name);
292286
}
293287
mods
294288
}
Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
1-
use libc::c_uint;
2-
use rustc_ast::expand::allocator::{
3-
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
4-
};
1+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
52
use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
6-
use rustc_middle::bug;
73
use rustc_middle::ty::TyCtxt;
84
use rustc_session::config::{DebugInfo, OomStrategy};
95
use rustc_symbol_mangling::mangle_internal_symbol;
106

11-
use crate::builder::SBuilder;
12-
use crate::declare::declare_simple_fn;
13-
use crate::llvm::{self, False, True, Type};
14-
use crate::{SimpleCx, attributes, debuginfo};
7+
use crate::llvm::{self, False};
8+
use crate::{SimpleCx, debuginfo};
159

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-
};
10+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, cx: SimpleCx<'_>, module_name: &str) {
2811
let i8 = cx.type_i8();
2912

30-
// rust alloc error handler
31-
create_wrapper_function(
32-
tcx,
33-
&cx,
34-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
35-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
36-
&[usize, usize], // size, align
37-
None,
38-
true,
39-
);
40-
4113
unsafe {
4214
// __rust_alloc_error_handler_should_panic
4315
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
@@ -60,67 +32,3 @@ pub(crate) unsafe fn codegen(
6032
dbg_cx.finalize(tcx.sess);
6133
}
6234
}
63-
64-
fn create_wrapper_function(
65-
tcx: TyCtxt<'_>,
66-
cx: &SimpleCx<'_>,
67-
from_name: &str,
68-
to_name: &str,
69-
args: &[&Type],
70-
output: Option<&Type>,
71-
no_return: bool,
72-
) {
73-
let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void()));
74-
let llfn = declare_simple_fn(
75-
&cx,
76-
from_name,
77-
llvm::CallConv::CCallConv,
78-
llvm::UnnamedAddr::Global,
79-
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
80-
ty,
81-
);
82-
let no_return = if no_return {
83-
// -> ! DIFlagNoReturn
84-
let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);
85-
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
86-
Some(no_return)
87-
} else {
88-
None
89-
};
90-
91-
if tcx.sess.must_emit_unwind_tables() {
92-
let uwtable =
93-
attributes::uwtable_attr(cx.llcx, tcx.sess.opts.unstable_opts.use_sync_unwind);
94-
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
95-
}
96-
97-
let callee = declare_simple_fn(
98-
&cx,
99-
to_name,
100-
llvm::CallConv::CCallConv,
101-
llvm::UnnamedAddr::Global,
102-
llvm::Visibility::Hidden,
103-
ty,
104-
);
105-
if let Some(no_return) = no_return {
106-
// -> ! DIFlagNoReturn
107-
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
108-
}
109-
llvm::set_visibility(callee, llvm::Visibility::Hidden);
110-
111-
let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
112-
113-
let mut bx = SBuilder::build(&cx, llbb);
114-
let args = args
115-
.iter()
116-
.enumerate()
117-
.map(|(i, _)| llvm::get_param(llfn, i as c_uint))
118-
.collect::<Vec<_>>();
119-
let ret = bx.call(ty, callee, &args, None);
120-
llvm::LLVMSetTailCall(ret, True);
121-
if output.is_some() {
122-
bx.ret(ret);
123-
} else {
124-
bx.ret_void()
125-
}
126-
}

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_llvm/src/llvm/ffi.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,6 @@ unsafe extern "C" {
11801180
pub(crate) fn LLVMSetThreadLocalMode(GlobalVar: &Value, Mode: ThreadLocalMode);
11811181
pub(crate) fn LLVMIsGlobalConstant(GlobalVar: &Value) -> Bool;
11821182
pub(crate) fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
1183-
pub(crate) safe fn LLVMSetTailCall(CallInst: &Value, IsTailCall: Bool);
11841183

11851184
// Operations on attributes
11861185
pub(crate) fn LLVMCreateStringAttribute(

0 commit comments

Comments
 (0)