Skip to content

Commit aafcf3b

Browse files
committed
Auto merge of #148413 - Noratrieb:mangle-rust-eh-personality, r=<try>
Mangle personality symbol try-job: x86_64-msvc-1 try-job: i686-msvc-1 try-job: x86_64-mingw-1 try-job: test-various try-job: armhf-gnu try-job: aarch64-apple
2 parents 8afe9ff + f0f210f commit aafcf3b

File tree

23 files changed

+117
-43
lines changed

23 files changed

+117
-43
lines changed

compiler/rustc_codegen_cranelift/src/debuginfo/emit.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ impl WriterRelocate {
9393
// HACK rust_eh_personality is likely not defined in the same crate,
9494
// so get_finalized_function won't work. Use the rust_eh_personality
9595
// of cg_clif itself, which is likely ABI compatible.
96-
if jit_module.declarations().get_function_decl(func_id).name.as_deref()
97-
== Some("rust_eh_personality")
96+
if jit_module
97+
.declarations()
98+
.get_function_decl(func_id)
99+
.name
100+
.is_some_and(|name| name.ends_with("rust_eh_personality"))
98101
{
99102
unsafe extern "C" {
100103
fn rust_eh_personality() -> !;

compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) struct UnwindContext {
2525
}
2626

2727
impl UnwindContext {
28-
pub(crate) fn new(module: &mut dyn Module, pic_eh_frame: bool) -> Self {
28+
pub(crate) fn new(module: &mut dyn Module, tcx: TyCtxt<'_>, pic_eh_frame: bool) -> Self {
2929
let endian = match module.isa().endianness() {
3030
Endianness::Little => RunTimeEndian::Little,
3131
Endianness::Big => RunTimeEndian::Big,
@@ -70,7 +70,7 @@ impl UnwindContext {
7070
// FIXME use eh_personality lang item instead
7171
let personality = module
7272
.declare_function(
73-
"rust_eh_personality",
73+
&rustc_symbol_mangling::mangle_internal_symbol(tcx, "rust_eh_personality"),
7474
Linkage::Import,
7575
&Signature {
7676
params: vec![

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ fn produce_final_output_artifacts(
322322
// These are used in linking steps and will be cleaned up afterward.
323323
}
324324

325-
fn make_module(sess: &Session, name: String) -> UnwindModule<ObjectModule> {
326-
let isa = crate::build_isa(sess, false);
325+
fn make_module(tcx: TyCtxt<'_>, name: String) -> UnwindModule<ObjectModule> {
326+
let isa = crate::build_isa(tcx.sess, false);
327327

328328
let mut builder =
329329
ObjectBuilder::new(isa, name + ".o", cranelift_module::default_libcall_names()).unwrap();
@@ -333,12 +333,13 @@ fn make_module(sess: &Session, name: String) -> UnwindModule<ObjectModule> {
333333
// explicitly disable it on MinGW as rustc already disables it by default on MinGW and as such
334334
// isn't tested. If rustc enables it in the future on MinGW, we can re-enable it too once it has
335335
// been on MinGW.
336-
let default_function_sections = sess.target.function_sections && !sess.target.is_like_windows;
336+
let default_function_sections =
337+
tcx.sess.target.function_sections && !tcx.sess.target.is_like_windows;
337338
builder.per_function_section(
338-
sess.opts.unstable_opts.function_sections.unwrap_or(default_function_sections),
339+
tcx.sess.opts.unstable_opts.function_sections.unwrap_or(default_function_sections),
339340
);
340341

341-
UnwindModule::new(ObjectModule::new(builder), true)
342+
UnwindModule::new(ObjectModule::new(builder), tcx, true)
342343
}
343344

344345
fn emit_cgu(
@@ -579,7 +580,7 @@ fn module_codegen(
579580
ConcurrencyLimiterToken,
580581
),
581582
) -> OngoingModuleCodegen {
582-
let mut module = make_module(tcx.sess, cgu_name.as_str().to_string());
583+
let mut module = make_module(tcx, cgu_name.as_str().to_string());
583584

584585
let (mut debug_context, codegened_functions, mut global_asm) =
585586
codegen_cgu_content(tcx, &mut module, cgu_name);
@@ -643,7 +644,7 @@ fn module_codegen(
643644
}
644645

645646
fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
646-
let mut allocator_module = make_module(tcx.sess, "allocator_shim".to_string());
647+
let mut allocator_module = make_module(tcx, "allocator_shim".to_string());
647648
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
648649

649650
if created_alloc_shim {

compiler/rustc_codegen_cranelift/src/unwind_module.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cranelift_module::{
77
ModuleReloc, ModuleResult,
88
};
99
use cranelift_object::{ObjectModule, ObjectProduct};
10+
use rustc_middle::ty::TyCtxt;
1011

1112
use crate::UnwindContext;
1213

@@ -17,8 +18,8 @@ pub(crate) struct UnwindModule<T> {
1718
}
1819

1920
impl<T: Module> UnwindModule<T> {
20-
pub(crate) fn new(mut module: T, pic_eh_frame: bool) -> Self {
21-
let unwind_context = UnwindContext::new(&mut module, pic_eh_frame);
21+
pub(crate) fn new(mut module: T, tcx: TyCtxt<'_>, pic_eh_frame: bool) -> Self {
22+
let unwind_context = UnwindContext::new(&mut module, tcx, pic_eh_frame);
2223
UnwindModule { module, unwind_context }
2324
}
2425
}

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,13 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
457457
self.declare_fn(symbol_name, fn_abi)
458458
}
459459
_ => {
460+
let rust_name;
460461
let name = if wants_msvc_seh(self.sess()) {
461462
"__CxxFrameHandler3"
462463
} else {
463-
"rust_eh_personality"
464+
rust_name =
465+
rustc_symbol_mangling::mangle_internal_symbol(tcx, "rust_eh_personality");
466+
&rust_name
464467
};
465468
self.declare_func(name, self.type_i32(), &[], true)
466469
}

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ impl CodegenBackend for GccCodegenBackend {
257257
let lto_supported = gccjit::is_lto_supported();
258258
LTO_SUPPORTED.store(lto_supported, Ordering::SeqCst);
259259
self.lto_supported.store(lto_supported, Ordering::SeqCst);
260-
261-
gccjit::set_global_personality_function_name(b"rust_eh_personality\0");
262260
}
263261

264262
#[cfg(not(feature = "master"))]
@@ -291,6 +289,8 @@ impl CodegenBackend for GccCodegenBackend {
291289
}
292290

293291
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
292+
#[cfg(feature = "master")]
293+
self.set_personality_function(tcx);
294294
let target_cpu = target_cpu(tcx.sess);
295295
let res = codegen_crate(self.clone(), tcx, target_cpu.to_string());
296296

@@ -314,6 +314,18 @@ impl CodegenBackend for GccCodegenBackend {
314314
}
315315
}
316316

317+
impl GccCodegenBackend {
318+
#[cfg(feature = "master")]
319+
fn set_personality_function(&self, tcx: TyCtxt<'_>) {
320+
let personality_symbol =
321+
CString::new(rustc_symbol_mangling::mangle_internal_symbol(tcx, "rust_eh_personality"))
322+
.unwrap();
323+
// FIXME: Change gccjit to store an owned string internally (https://github.com/rust-lang/rust/pull/148413#discussion_r2749777937)
324+
let personality_symbol = Box::leak(personality_symbol.into_boxed_c_str());
325+
gccjit::set_global_personality_function_name(personality_symbol.to_bytes_with_nul());
326+
}
327+
}
328+
317329
fn new_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> {
318330
let context = Context::default();
319331
if matches!(tcx.sess.target.arch, Arch::X86 | Arch::X86_64) {

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn prepare_lto(
7878
symbols_below_threshold.push(c"__llvm_profile_counter_bias".to_owned());
7979

8080
// LTO seems to discard this otherwise under certain circumstances.
81-
symbols_below_threshold.push(c"rust_eh_personality".to_owned());
81+
symbols_below_threshold.push(CString::new(cgcx.rust_eh_personality_symbol.clone()).unwrap());
8282

8383
// If we're performing LTO for the entire crate graph, then for each of our
8484
// upstream dependencies, find the corresponding rlib and load the bitcode

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,17 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
901901
DUMMY_SP,
902902
)),
903903
_ => {
904-
let name = name.unwrap_or("rust_eh_personality");
904+
let mangled_symbol;
905+
let name = match name {
906+
Some(name) => name,
907+
None => {
908+
mangled_symbol = rustc_symbol_mangling::mangle_internal_symbol(
909+
tcx,
910+
"rust_eh_personality",
911+
);
912+
mangled_symbol.as_str()
913+
}
914+
};
905915
if let Some(llfn) = self.get_declared_value(name) {
906916
llfn
907917
} else {

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ impl CodegenBackend for LlvmCodegenBackend {
346346
llvm::LLVMRustLLVMHasZstdCompression()
347347
}
348348

349+
fn can_mangle_eh_personality(&self) -> bool {
350+
// https://github.com/llvm/llvm-project/pull/166095
351+
llvm_util::get_version() > (22, 0, 0)
352+
}
353+
349354
fn target_config(&self, sess: &Session) -> TargetConfig {
350355
target_config(sess)
351356
}

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
348348
pub split_debuginfo: rustc_target::spec::SplitDebuginfo,
349349
pub split_dwarf_kind: rustc_session::config::SplitDwarfKind,
350350
pub pointer_size: Size,
351+
pub rust_eh_personality_symbol: String,
351352

352353
/// LLVM optimizations for which we want to print remarks.
353354
pub remark: Passes,
@@ -1328,6 +1329,10 @@ fn start_executing_work<B: ExtraBackendMethods>(
13281329
target_is_like_darwin: tcx.sess.target.is_like_darwin,
13291330
target_is_like_aix: tcx.sess.target.is_like_aix,
13301331
target_is_like_gpu: tcx.sess.target.is_like_gpu,
1332+
rust_eh_personality_symbol: rustc_symbol_mangling::mangle_internal_symbol(
1333+
tcx,
1334+
"rust_eh_personality",
1335+
),
13311336
split_debuginfo: tcx.sess.split_debuginfo(),
13321337
split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind,
13331338
parallel: backend.supports_parallel() && !sess.opts.unstable_opts.no_parallel_backend,

0 commit comments

Comments
 (0)