Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) struct UnwindContext {
}

impl UnwindContext {
pub(crate) fn new(module: &mut dyn Module, pic_eh_frame: bool) -> Self {
pub(crate) fn new(module: &mut dyn Module, eh_personality: &str, pic_eh_frame: bool) -> Self {
let endian = match module.isa().endianness() {
Endianness::Little => RunTimeEndian::Little,
Endianness::Big => RunTimeEndian::Big,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl UnwindContext {
// FIXME use eh_personality lang item instead
let personality = module
.declare_function(
"rust_eh_personality",
eh_personality,
Linkage::Import,
&Signature {
params: vec![
Expand All @@ -89,7 +89,7 @@ impl UnwindContext {
// Use indirection here to support PIC the case where rust_eh_personality is defined in
// another DSO.
let personality_ref = module
.declare_data("DW.ref.rust_eh_personality", Linkage::Local, false, false)
.declare_data(&format!("DW.ref.{eh_personality}"), Linkage::Local, false, false)
.unwrap();

let mut personality_ref_data = DataDescription::new();
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ fn make_module(tcx: TyCtxt<'_>, cgu_name: &str) -> AotModule {
tcx.sess.opts.unstable_opts.function_sections.unwrap_or(default_function_sections),
);

let module = UnwindModule::new(ObjectModule::new(builder), true);
let module = UnwindModule::new(
ObjectModule::new(builder),
&rustc_symbol_mangling::eh_personality_symbol(tcx),
true,
);

let producer = crate::debuginfo::producer(tcx.sess);
let global_asm_config = GlobalAsmConfig::new(tcx.sess);
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_codegen_cranelift/src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ fn create_jit_module(
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info.clone()));
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);
let mut jit_module = UnwindModule::new(
JITModule::new(jit_builder),
&rustc_symbol_mangling::eh_personality_symbol(tcx),
false,
);

let cx = DebugContext::new(tcx, jit_module.isa(), false, "dummy_cgu_name");

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/unwind_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub(crate) struct UnwindModule<T> {
}

impl<T: Module> UnwindModule<T> {
pub(crate) fn new(mut module: T, pic_eh_frame: bool) -> Self {
let unwind_context = UnwindContext::new(&mut module, pic_eh_frame);
pub(crate) fn new(mut module: T, eh_personality: &str, pic_eh_frame: bool) -> Self {
let unwind_context = UnwindContext::new(&mut module, eh_personality, pic_eh_frame);
UnwindModule { module, unwind_context }
}
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,12 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
self.declare_fn(symbol_name, fn_abi)
}
_ => {
let rust_name;
let name = if wants_msvc_seh(&self.sess().target) {
"__CxxFrameHandler3"
} else {
"rust_eh_personality"
rust_name = rustc_symbol_mangling::eh_personality_symbol(tcx);
&rust_name
};
self.declare_func(name, self.type_i32(), &[], true)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn declare_raw_fn<'gcc>(
cx.functions.borrow_mut().insert(name.to_string(), func);

#[cfg(feature = "master")]
if name == "rust_eh_personality" {
if name.ends_with("rust_eh_personality") {
// NOTE: GCC will sometimes change the personality function set on a function from
// rust_eh_personality to __gcc_personality_v0 as an optimization.
// As such, we need to create a weak alias from __gcc_personality_v0 to
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ impl GccCodegenBackend {
fn config(&self) -> &BackendConfig {
self.config.as_ref().expect("target info not initialized")
}

#[cfg(feature = "master")]
fn set_personality_function(&self, tcx: TyCtxt<'_>) {
let personality_symbol = rustc_symbol_mangling::eh_personality_symbol(tcx);
gccjit::set_global_personality_function_name(
&CString::new(personality_symbol).expect("symbol name shouldn't contain NUL"),
);
}
}

fn load_libgccjit_if_needed(libgccjit_target_lib_file: &Path) {
Expand Down Expand Up @@ -236,8 +244,6 @@ impl CodegenBackend for GccCodegenBackend {
target_info: Arc::new(IntoDynSyncSend(context.get_target_info())),
lto_supported: gccjit::is_lto_supported(),
});

gccjit::set_global_personality_function_name(c"rust_eh_personality");
}

#[cfg(not(feature = "master"))]
Expand Down Expand Up @@ -273,6 +279,8 @@ impl CodegenBackend for GccCodegenBackend {
}

fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
#[cfg(feature = "master")]
self.set_personality_function(tcx);
Box::new(codegen_crate(self.clone(), tcx))
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn prepare_lto(
symbols_below_threshold.push(c"__llvm_profile_counter_bias".to_owned());

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

// If we're performing LTO for the entire crate graph, then for each of our
// upstream dependencies, find the corresponding rlib and load the bitcode
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,14 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
tcx.sess.pointer_authentication_functions(),
),
_ => {
let name = name.unwrap_or("rust_eh_personality");
let mangled_symbol;
let name = match name {
Some(name) => name,
None => {
mangled_symbol = rustc_symbol_mangling::eh_personality_symbol(tcx);
mangled_symbol.as_str()
}
};
if let Some(llfn) = self.get_declared_value(name) {
llfn
} else {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ pub struct CodegenContext {
pub split_debuginfo: rustc_target::spec::SplitDebuginfo,
pub split_dwarf_kind: rustc_session::config::SplitDwarfKind,
pub pointer_size: Size,
pub rust_eh_personality_symbol: String,

/// LLVM optimizations for which we want to print remarks.
pub remark: Passes,
Expand Down Expand Up @@ -1284,6 +1285,7 @@ fn start_executing_work<B: WriteBackendMethods>(
target_is_like_darwin: tcx.sess.target.is_like_darwin,
target_is_like_aix: tcx.sess.target.is_like_aix,
target_is_like_gpu: tcx.sess.target.is_like_gpu,
rust_eh_personality_symbol: rustc_symbol_mangling::eh_personality_symbol(tcx),
split_debuginfo: tcx.sess.split_debuginfo(),
split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind,
parallel,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod v0;

pub mod test;

pub use v0::{mangle_cgu, mangle_internal_symbol};
pub use v0::{eh_personality_symbol, mangle_cgu, mangle_internal_symbol};

/// Offload kernels need custom v0 symbol treatment because the host
/// and device compilation passes run with different `stable_crate_id`s
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ pub fn mangle_cgu<'tcx>(tcx: TyCtxt<'tcx>, krate: CrateNum, cgu_name: Either<u64
std::mem::take(&mut p.out)
}

pub fn eh_personality_symbol<'tcx>(tcx: TyCtxt<'tcx>) -> String {
mangle_internal_symbol(tcx, "rust_eh_personality")
}

pub fn mangle_internal_symbol<'tcx>(tcx: TyCtxt<'tcx>, item_name: &str) -> String {
match item_name {
// rust_eh_personality must not be renamed as LLVM hard-codes the name
"rust_eh_personality" => return item_name.to_owned(),
// Apple availability symbols need to not be mangled to be usable by
// C/Objective-C code.
"__isPlatformVersionAtLeast" | "__isOSVersionAtLeast" => return item_name.to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen-llvm/gdb_debug_script_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn panic_handler(_: &core::panic::PanicInfo) -> ! {
loop {}
}

#[no_mangle]
#[lang = "eh_personality"]
extern "C" fn rust_eh_personality() {
loop {}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/run-make/no-alloc-shim/foo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(lang_items)]
#![feature(rustc_attrs)]
#![no_std]
#![no_main]
Expand All @@ -11,7 +12,7 @@ fn panic_handler(_: &core::panic::PanicInfo) -> ! {
loop {}
}

#[no_mangle]
#[lang = "eh_personality"]
extern "C" fn rust_eh_personality(
_version: i32,
_actions: i32,
Expand Down
46 changes: 22 additions & 24 deletions tests/run-make/symbols-all-mangled/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,31 @@ fn main() {
symbols_check(&exe_name);
}

fn is_symbol_ok(sym: &str) -> bool {
let sym = strip_underscore_if_apple(sym);
// There are debuginfo helper symbols that get prefixed with DW.ref.
let sym = sym.strip_prefix("DW.ref.").unwrap_or(sym);

if sym.starts_with("_ZN") || sym.starts_with("_R") {
return true; // Correctly mangled
}

if sym.contains(".llvm.") {
// Starting in LLVM 21 we get various implementation-detail functions which
// contain .llvm. that are not a problem.
return true;
}

false
}

fn symbols_check_archive(path: &str) {
let binary_data = rfs::read(path);
let file = object::read::archive::ArchiveFile::parse(&*binary_data).unwrap();
for symbol in file.symbols().unwrap().unwrap() {
let symbol = symbol.unwrap();
let name = strip_underscore_if_apple(std::str::from_utf8(symbol.name()).unwrap());
if name.starts_with("_ZN") || name.starts_with("_R") {
let name = std::str::from_utf8(symbol.name()).unwrap();
if is_symbol_ok(name) {
continue; // Correctly mangled
}

Expand All @@ -35,16 +53,6 @@ fn symbols_check_archive(path: &str) {
continue; // All compiler-builtins symbols must remain unmangled
}

if name.contains("rust_eh_personality") {
continue; // Unfortunately LLVM doesn't allow us to mangle this symbol
}

if name.contains(".llvm.") {
// Starting in LLVM 21 we get various implementation-detail functions which
// contain .llvm. that are not a problem.
continue;
}

panic!("Unmangled symbol found in {path}: {name}");
}
}
Expand All @@ -59,8 +67,8 @@ fn symbols_check(path: &str) {
if symbol.is_weak() {
continue; // Likely an intrinsic from compiler-builtins
}
let name = strip_underscore_if_apple(symbol.name().unwrap());
if name.starts_with("_ZN") || name.starts_with("_R") {
let name = symbol.name().unwrap();
if is_symbol_ok(name) {
continue; // Correctly mangled
}

Expand All @@ -71,16 +79,6 @@ fn symbols_check(path: &str) {
continue;
}

if name.contains("rust_eh_personality") {
continue; // Unfortunately LLVM doesn't allow us to mangle this symbol
}

if name.contains(".llvm.") {
// Starting in LLVM 21 we get various implementation-detail functions which
// contain .llvm. that are not a problem.
continue;
}

panic!("Unmangled symbol found in {path}: {name}");
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/linkage-attr/raw-dylib/elf/glibc-x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//@ ignore-backends: gcc

#![allow(incomplete_features)]
#![feature(lang_items)]
#![feature(raw_dylib_elf)]
#![no_std]
#![no_main]
Expand Down Expand Up @@ -69,7 +70,7 @@ fn panic_handler(_: &core::panic::PanicInfo<'_>) -> ! {
exit(1);
}

#[unsafe(no_mangle)]
#[lang = "eh_personality"]
extern "C" fn rust_eh_personality(
_version: i32,
_actions: i32,
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/panic-runtime/auxiliary/panic-runtime-abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//@ no-prefer-dynamic

#![feature(panic_runtime)]
#![feature(rustc_attrs)]
#![crate_type = "rlib"]

#![no_std]
#![panic_runtime]

Expand All @@ -13,5 +13,5 @@ pub extern "C" fn __rust_maybe_catch_panic() {}
#[no_mangle]
pub extern "C" fn __rust_start_panic() {}

#[no_mangle]
#[rustc_std_internal_symbol]
pub extern "C" fn rust_eh_personality() {}
4 changes: 2 additions & 2 deletions tests/ui/panic-runtime/auxiliary/panic-runtime-unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//@ no-prefer-dynamic

#![feature(panic_runtime)]
#![feature(rustc_attrs)]
#![crate_type = "rlib"]

#![no_std]
#![panic_runtime]

Expand All @@ -13,5 +13,5 @@ pub extern "C" fn __rust_maybe_catch_panic() {}
#[no_mangle]
pub extern "C" fn __rust_start_panic() {}

#[no_mangle]
#[rustc_std_internal_symbol]
pub extern "C" fn rust_eh_personality() {}
4 changes: 2 additions & 2 deletions tests/ui/panic-runtime/auxiliary/panic-runtime-unwind2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//@ no-prefer-dynamic

#![feature(panic_runtime)]
#![feature(rustc_attrs)]
#![crate_type = "rlib"]

#![no_std]
#![panic_runtime]

Expand All @@ -13,5 +13,5 @@ pub extern "C" fn __rust_maybe_catch_panic() {}
#[no_mangle]
pub extern "C" fn __rust_start_panic() {}

#[no_mangle]
#[rustc_std_internal_symbol]
pub extern "C" fn rust_eh_personality() {}
3 changes: 2 additions & 1 deletion tests/ui/panic-runtime/incompatible-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//@ compile-flags: --crate-type=lib -Ccodegen-units=1
#![no_std]
#![panic_runtime]
#![feature(lang_items)]
#![feature(panic_runtime)]
#![feature(rustc_attrs)]

Expand All @@ -20,7 +21,7 @@ pub fn test(_: DropMe) {
unreachable!();
}

#[rustc_std_internal_symbol]
#[lang = "eh_personality"]
pub unsafe extern "C" fn rust_eh_personality(
_version: i32,
_actions: i32,
Expand Down
1 change: 0 additions & 1 deletion tests/ui/sanitizer/dataflow-abilist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,4 @@ fun:_ZN4core*=uninstrumented
fun:_R*4core*=uninstrumented
fun:_ZN3std*=uninstrumented
fun:_R*3std*=uninstrumented
fun:rust_eh_personality=uninstrumented
fun:_R*__rustc*=uninstrumented
Loading