Skip to content

Commit bc49d30

Browse files
authored
Rollup merge of #143387 - dpaoliello:shouldpanicfn, r=bjorn3
Make __rust_alloc_error_handler_should_panic a function Fixes rust-lang/rust#143253 `__rust_alloc_error_handler_should_panic` is a static but was being exported as a function. For most targets this doesn't matter, but Arm64EC Windows uses different decorations for exported variables vs functions, hence it fails to link when `-Z oom=abort` is enabled. We've had issues in the past with statics like this (see rust-lang/rust#141061) but the tldr; is that Arm64EC needs symbols correctly exported as either a function or data, and data MUST and MUST ONLY be marked `dllimport` when the symbol is being imported from another binary, which is non-trivial to calculate for these compiler-generated statics. So, instead, the easiest thing to do is to make `__rust_alloc_error_handler_should_panic` a function instead. Since `__rust_alloc_error_handler_should_panic` isn't involved in any linking shenanigans, I've marked it as `AlwaysInline` with the hopes that the various backends will see that it is just returning a constant and perform the same optimizations as the previous implementation. r? `@bjorn3`
2 parents 775d902 + 7a5658a commit bc49d30

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

src/allocator.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,34 @@ fn codegen_inner(
8484
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
8585
);
8686

87-
let data_id = module
88-
.declare_data(
89-
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
90-
Linkage::Export,
91-
false,
92-
false,
93-
)
94-
.unwrap();
95-
let mut data = DataDescription::new();
96-
data.set_align(1);
97-
let val = oom_strategy.should_panic();
98-
data.define(Box::new([val]));
99-
module.define_data(data_id, &data).unwrap();
87+
{
88+
let sig = Signature {
89+
call_conv: module.target_config().default_call_conv,
90+
params: vec![],
91+
returns: vec![AbiParam::new(types::I8)],
92+
};
93+
let func_id = module
94+
.declare_function(
95+
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
96+
Linkage::Export,
97+
&sig,
98+
)
99+
.unwrap();
100+
let mut ctx = Context::new();
101+
ctx.func.signature = sig;
102+
{
103+
let mut func_ctx = FunctionBuilderContext::new();
104+
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
105+
106+
let block = bcx.create_block();
107+
bcx.switch_to_block(block);
108+
let value = bcx.ins().iconst(types::I8, oom_strategy.should_panic() as i64);
109+
bcx.ins().return_(&[value]);
110+
bcx.seal_all_blocks();
111+
bcx.finalize();
112+
}
113+
module.define_function(func_id, &mut ctx).unwrap();
114+
}
100115

101116
{
102117
let sig = Signature {

0 commit comments

Comments
 (0)