Skip to content

Commit ea26029

Browse files
committed
Remove implicit #[no_mangle] for #[rustc_std_internal_symbol]
1 parent 1535ecb commit ea26029

File tree

12 files changed

+90
-44
lines changed

12 files changed

+90
-44
lines changed

compiler/rustc_codegen_cranelift/src/allocator.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast::expand::allocator::{
77
};
88
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
99
use rustc_session::config::OomStrategy;
10+
use rustc_symbol_mangling::mangle_internal_symbol;
1011

1112
use crate::prelude::*;
1213

@@ -18,6 +19,7 @@ pub(crate) fn codegen(
1819
) -> bool {
1920
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
2021
codegen_inner(
22+
tcx,
2123
module,
2224
unwind_context,
2325
kind,
@@ -28,6 +30,7 @@ pub(crate) fn codegen(
2830
}
2931

3032
fn codegen_inner(
33+
tcx: TyCtxt<'_>,
3134
module: &mut impl Module,
3235
unwind_context: &mut UnwindContext,
3336
kind: AllocatorKind,
@@ -69,8 +72,8 @@ fn codegen_inner(
6972
module,
7073
unwind_context,
7174
sig,
72-
&global_fn_name(method.name),
73-
&default_fn_name(method.name),
75+
&mangle_internal_symbol(tcx, &global_fn_name(method.name)),
76+
&mangle_internal_symbol(tcx, &default_fn_name(method.name)),
7477
);
7578
}
7679
}
@@ -84,19 +87,32 @@ fn codegen_inner(
8487
module,
8588
unwind_context,
8689
sig,
87-
"__rust_alloc_error_handler",
88-
&alloc_error_handler_name(alloc_error_handler_kind),
90+
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
91+
&mangle_internal_symbol(tcx, &alloc_error_handler_name(alloc_error_handler_kind)),
8992
);
9093

91-
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
94+
let data_id = module
95+
.declare_data(
96+
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
97+
Linkage::Export,
98+
false,
99+
false,
100+
)
101+
.unwrap();
92102
let mut data = DataDescription::new();
93103
data.set_align(1);
94104
let val = oom_strategy.should_panic();
95105
data.define(Box::new([val]));
96106
module.define_data(data_id, &data).unwrap();
97107

98-
let data_id =
99-
module.declare_data(NO_ALLOC_SHIM_IS_UNSTABLE, Linkage::Export, false, false).unwrap();
108+
let data_id = module
109+
.declare_data(
110+
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
111+
Linkage::Export,
112+
false,
113+
false,
114+
)
115+
.unwrap();
100116
let mut data = DataDescription::new();
101117
data.set_align(1);
102118
data.define(Box::new([0]));

compiler/rustc_codegen_cranelift/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern crate rustc_metadata;
2727
extern crate rustc_monomorphize;
2828
extern crate rustc_session;
2929
extern crate rustc_span;
30+
extern crate rustc_symbol_mangling;
3031
extern crate rustc_target;
3132

3233
// This prevents duplicating functions and statics that are already part of the host rustc process.

compiler/rustc_codegen_gcc/src/allocator.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_ast::expand::allocator::{
88
use rustc_middle::bug;
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::config::OomStrategy;
11+
use rustc_symbol_mangling::mangle_internal_symbol;
1112

1213
use crate::GccContext;
1314

@@ -51,8 +52,8 @@ pub(crate) unsafe fn codegen(
5152
panic!("invalid allocator output")
5253
}
5354
};
54-
let from_name = global_fn_name(method.name);
55-
let to_name = default_fn_name(method.name);
55+
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
56+
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
5657

5758
create_wrapper_function(tcx, context, &from_name, &to_name, &types, output);
5859
}
@@ -62,19 +63,19 @@ pub(crate) unsafe fn codegen(
6263
create_wrapper_function(
6364
tcx,
6465
context,
65-
"__rust_alloc_error_handler",
66-
alloc_error_handler_name(alloc_error_handler_kind),
66+
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
67+
&mangle_internal_symbol(tcx, &alloc_error_handler_name(alloc_error_handler_kind)),
6768
&[usize, usize],
6869
None,
6970
);
7071

71-
let name = OomStrategy::SYMBOL.to_string();
72+
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
7273
let global = context.new_global(None, GlobalKind::Exported, i8, name);
7374
let value = tcx.sess.opts.unstable_opts.oom.should_panic();
7475
let value = context.new_rvalue_from_int(i8, value as i32);
7576
global.global_set_initializer_rvalue(value);
7677

77-
let name = NO_ALLOC_SHIM_IS_UNSTABLE.to_string();
78+
let name = mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE);
7879
let global = context.new_global(None, GlobalKind::Exported, i8, name);
7980
let value = context.new_rvalue_from_int(i8, 0);
8081
global.global_set_initializer_rvalue(value);

compiler/rustc_codegen_llvm/src/allocator.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast::expand::allocator::{
77
use rustc_middle::bug;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::config::{DebugInfo, OomStrategy};
10+
use rustc_symbol_mangling::mangle_internal_symbol;
1011

1112
use crate::debuginfo;
1213
use crate::llvm::{self, Context, False, Module, True, Type};
@@ -54,8 +55,8 @@ pub(crate) unsafe fn codegen(
5455
}
5556
};
5657

57-
let from_name = global_fn_name(method.name);
58-
let to_name = default_fn_name(method.name);
58+
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
59+
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
5960

6061
create_wrapper_function(tcx, llcx, llmod, &from_name, &to_name, &args, output, false);
6162
}
@@ -66,15 +67,15 @@ pub(crate) unsafe fn codegen(
6667
tcx,
6768
llcx,
6869
llmod,
69-
"__rust_alloc_error_handler",
70-
alloc_error_handler_name(alloc_error_handler_kind),
70+
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
71+
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
7172
&[usize, usize], // size, align
7273
None,
7374
true,
7475
);
7576

7677
// __rust_alloc_error_handler_should_panic
77-
let name = OomStrategy::SYMBOL;
78+
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
7879
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
7980
if tcx.sess.default_hidden_visibility() {
8081
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
@@ -83,7 +84,7 @@ pub(crate) unsafe fn codegen(
8384
let llval = llvm::LLVMConstInt(i8, val as u64, False);
8485
llvm::LLVMSetInitializer(ll_g, llval);
8586

86-
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
87+
let name = mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE);
8788
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
8889
if tcx.sess.default_hidden_visibility() {
8990
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);

compiler/rustc_codegen_llvm/src/intrinsic.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf};
1919
use rustc_middle::ty::{self, GenericArgsRef, Ty};
2020
use rustc_middle::{bug, span_bug};
2121
use rustc_span::{sym, Span, Symbol};
22+
use rustc_symbol_mangling::mangle_internal_symbol;
2223
use rustc_target::abi::{self, Align, Float, HasDataLayout, Primitive, Size};
2324
use rustc_target::spec::{HasTargetSpec, PanicStrategy};
2425
use tracing::debug;
@@ -737,7 +738,10 @@ fn codegen_msvc_try<'ll>(
737738
let type_name = bx.const_bytes(b"rust_panic\0");
738739
let type_info =
739740
bx.const_struct(&[type_info_vtable, bx.const_null(bx.type_ptr()), type_name], false);
740-
let tydesc = bx.declare_global("__rust_panic_type_info", bx.val_ty(type_info));
741+
let tydesc = bx.declare_global(
742+
&mangle_internal_symbol(bx.tcx, "__rust_panic_type_info"),
743+
bx.val_ty(type_info),
744+
);
741745
unsafe {
742746
llvm::LLVMRustSetLinkage(tydesc, llvm::Linkage::LinkOnceODRLinkage);
743747
llvm::SetUniqueComdat(bx.llmod, tydesc);

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::base::allocator_kind_for_codegen;
22

33
use std::collections::hash_map::Entry::*;
44

5-
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE};
5+
use rustc_ast::expand::allocator::{global_fn_name, ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE};
66
use rustc_data_structures::unord::UnordMap;
77
use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
@@ -17,6 +17,7 @@ use rustc_middle::ty::{self, SymbolName, TyCtxt};
1717
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
1818
use rustc_middle::util::Providers;
1919
use rustc_session::config::{CrateType, OomStrategy};
20+
use rustc_symbol_mangling::mangle_internal_symbol;
2021
use rustc_target::spec::{SanitizerSet, TlsModel};
2122
use tracing::debug;
2223

@@ -220,8 +221,11 @@ fn exported_symbols_provider_local(
220221
if allocator_kind_for_codegen(tcx).is_some() {
221222
for symbol_name in ALLOCATOR_METHODS
222223
.iter()
223-
.map(|method| format!("__rust_{}", method.name))
224-
.chain(["__rust_alloc_error_handler".to_string(), OomStrategy::SYMBOL.to_string()])
224+
.map(|method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
225+
.chain([
226+
mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
227+
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
228+
])
225229
{
226230
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
227231

@@ -235,8 +239,10 @@ fn exported_symbols_provider_local(
235239
));
236240
}
237241

238-
let exported_symbol =
239-
ExportedSymbol::NoDefId(SymbolName::new(tcx, NO_ALLOC_SHIM_IS_UNSTABLE));
242+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(
243+
tcx,
244+
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
245+
));
240246
symbols.push((
241247
exported_symbol,
242248
SymbolExportInfo {

compiler/rustc_codegen_ssa/src/base.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
3838
use rustc_session::Session;
3939
use rustc_span::symbol::sym;
4040
use rustc_span::Symbol;
41+
use rustc_symbol_mangling::mangle_internal_symbol;
4142
use rustc_target::abi::FIRST_VARIANT;
4243

4344
use std::cmp;
@@ -925,7 +926,19 @@ impl CrateInfo {
925926
.for_each(|(_, linked_symbols)| {
926927
let mut symbols = missing_weak_lang_items
927928
.iter()
928-
.map(|item| (format!("{prefix}{item}"), SymbolExportKind::Text))
929+
.map(|item| {
930+
(
931+
format!(
932+
"{prefix}{}",
933+
if *item == sym::rust_eh_personality {
934+
"rust_eh_personality".to_owned()
935+
} else {
936+
mangle_internal_symbol(tcx, item.as_str())
937+
}
938+
),
939+
SymbolExportKind::Text,
940+
)
941+
})
929942
.collect::<Vec<_>>();
930943
symbols.sort_unstable_by(|a, b| a.0.cmp(&b.0));
931944
linked_symbols.extend(symbols);
@@ -938,7 +951,13 @@ impl CrateInfo {
938951
// errors.
939952
linked_symbols.extend(ALLOCATOR_METHODS.iter().map(|method| {
940953
(
941-
format!("{prefix}{}", global_fn_name(method.name).as_str()),
954+
format!(
955+
"{prefix}{}",
956+
mangle_internal_symbol(
957+
tcx,
958+
global_fn_name(method.name).as_str()
959+
)
960+
),
942961
SymbolExportKind::Text,
943962
)
944963
}));

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -652,25 +652,23 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
652652
// strippable by the linker.
653653
//
654654
// Additionally weak lang items have predetermined symbol names.
655-
if WEAK_LANG_ITEMS.iter().any(|&l| tcx.lang_items().get(l) == Some(did.to_def_id())) {
656-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
657-
}
658655
if let Some((name, _)) = lang_items::extract(attrs)
659656
&& let Some(lang_item) = LangItem::from_name(name)
660-
&& let Some(link_name) = lang_item.link_name()
661657
{
662-
codegen_fn_attrs.export_name = Some(link_name);
663-
codegen_fn_attrs.link_name = Some(link_name);
658+
if WEAK_LANG_ITEMS.iter().any(|&l| l == lang_item) {
659+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
660+
}
661+
if lang_item == LangItem::EhPersonality {
662+
// rust_eh_personality must not be mangled as LLVM hard-codes the name.
663+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
664+
}
665+
if let Some(link_name) = lang_item.link_name() {
666+
codegen_fn_attrs.export_name = Some(link_name);
667+
codegen_fn_attrs.link_name = Some(link_name);
668+
}
664669
}
665670
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
666671

667-
// Internal symbols to the standard library all have no_mangle semantics in
668-
// that they have defined symbol names present in the function name. This
669-
// also applies to weak symbols where they all have known symbol names.
670-
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
671-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
672-
}
673-
674672
// Any linkage to LLVM intrinsics for now forcibly marks them all as never
675673
// unwinds since LLVM sometimes can't handle codegen which `invoke`s
676674
// intrinsic functions.

tests/codegen/alloc-optimisation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
pub fn alloc_test(data: u32) {
66
// CHECK-LABEL: @alloc_test
77
// CHECK-NEXT: start:
8-
// CHECK-NEXT: {{.*}} load volatile i8, ptr @__rust_no_alloc_shim_is_unstable, align 1
8+
// CHECK-NEXT: {{.*}} load volatile i8, ptr @{{.*}}__rust_no_alloc_shim_is_unstable, align 1
99
// CHECK-NEXT: ret void
1010
let x = Box::new(data);
1111
drop(x);

tests/codegen/box-uninit-bytes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ pub fn box_lotsa_padding() -> Box<LotsaPadding> {
4141

4242
// Hide the `allocalign` attribute in the declaration of __rust_alloc
4343
// from the CHECK-NOT above, and also verify the attributes got set reasonably.
44-
// CHECK: declare {{(dso_local )?}}noalias noundef ptr @__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+}} allocalign noundef) unnamed_addr [[RUST_ALLOC_ATTRS:#[0-9]+]]
44+
// CHECK: declare {{(dso_local )?}}noalias noundef ptr @{{.*}}__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+}} allocalign noundef) unnamed_addr [[RUST_ALLOC_ATTRS:#[0-9]+]]
4545

4646
// CHECK-DAG: attributes [[RUST_ALLOC_ATTRS]] = { {{.*}} allockind("alloc,uninitialized,aligned") allocsize(0) {{(uwtable )?}}"alloc-family"="__rust_alloc" {{.*}} }

tests/codegen/dealloc-no-unwind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Drop for A {
1818
#[no_mangle]
1919
pub fn a(a: Box<i32>) {
2020
// CHECK-LABEL: define{{.*}}void @a
21-
// CHECK: call void @__rust_dealloc
21+
// CHECK: call void @{{.*}}__rust_dealloc
2222
// CHECK-NEXT: call void @foo
2323
let _a = A;
2424
drop(a);

tests/codegen/vec-optimizes-away.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
pub fn sum_me() -> i32 {
66
// CHECK-LABEL: @sum_me
77
// CHECK-NEXT: {{^.*:$}}
8-
// CHECK-NEXT: {{.*}} load volatile i8, ptr @__rust_no_alloc_shim_is_unstable, align 1
8+
// CHECK-NEXT: {{.*}} load volatile i8, ptr @{{.*}}__rust_no_alloc_shim_is_unstable, align 1
99
// CHECK-NEXT: ret i32 6
1010
vec![1, 2, 3].iter().sum::<i32>()
1111
}

0 commit comments

Comments
 (0)