Skip to content

Commit 61a8112

Browse files
committed
Remove implicit #[no_mangle] for #[rustc_std_internal_symbol]
1 parent eea99b4 commit 61a8112

File tree

17 files changed

+93
-47
lines changed

17 files changed

+93
-47
lines changed

compiler/rustc_codegen_cranelift/src/allocator.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ 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

1314
/// Returns whether an allocator shim was created
1415
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
1516
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
1617
codegen_inner(
18+
tcx,
1719
module,
1820
kind,
1921
tcx.alloc_error_handler_kind(()).unwrap(),
@@ -23,6 +25,7 @@ pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
2325
}
2426

2527
fn codegen_inner(
28+
tcx: TyCtxt<'_>,
2629
module: &mut dyn Module,
2730
kind: AllocatorKind,
2831
alloc_error_handler_kind: AllocatorKind,
@@ -62,8 +65,8 @@ fn codegen_inner(
6265
crate::common::create_wrapper_function(
6366
module,
6467
sig,
65-
&global_fn_name(method.name),
66-
&default_fn_name(method.name),
68+
&mangle_internal_symbol(tcx, &global_fn_name(method.name)),
69+
&mangle_internal_symbol(tcx, &default_fn_name(method.name)),
6770
);
6871
}
6972
}
@@ -76,19 +79,32 @@ fn codegen_inner(
7679
crate::common::create_wrapper_function(
7780
module,
7881
sig,
79-
"__rust_alloc_error_handler",
80-
&alloc_error_handler_name(alloc_error_handler_kind),
82+
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
83+
&mangle_internal_symbol(tcx, &alloc_error_handler_name(alloc_error_handler_kind)),
8184
);
8285

83-
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
86+
let data_id = module
87+
.declare_data(
88+
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
89+
Linkage::Export,
90+
false,
91+
false,
92+
)
93+
.unwrap();
8494
let mut data = DataDescription::new();
8595
data.set_align(1);
8696
let val = oom_strategy.should_panic();
8797
data.define(Box::new([val]));
8898
module.define_data(data_id, &data).unwrap();
8999

90-
let data_id =
91-
module.declare_data(NO_ALLOC_SHIM_IS_UNSTABLE, Linkage::Export, false, false).unwrap();
100+
let data_id = module
101+
.declare_data(
102+
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
103+
Linkage::Export,
104+
false,
105+
false,
106+
)
107+
.unwrap();
92108
let mut data = DataDescription::new();
93109
data.set_align(1);
94110
data.define(Box::new([0]));

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern crate rustc_index;
2727
extern crate rustc_metadata;
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

Lines changed: 7 additions & 6 deletions
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_gcc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern crate rustc_metadata;
5050
extern crate rustc_middle;
5151
extern crate rustc_session;
5252
extern crate rustc_span;
53+
extern crate rustc_symbol_mangling;
5354
extern crate rustc_target;
5455

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

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::expand::allocator::{
66
use rustc_middle::bug;
77
use rustc_middle::ty::TyCtxt;
88
use rustc_session::config::{DebugInfo, OomStrategy};
9+
use rustc_symbol_mangling::mangle_internal_symbol;
910

1011
use crate::common::AsCCharPtr;
1112
use crate::llvm::{self, Context, False, Module, True, Type};
@@ -55,8 +56,8 @@ pub(crate) unsafe fn codegen(
5556
}
5657
};
5758

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

6162
create_wrapper_function(tcx, llcx, llmod, &from_name, &to_name, &args, output, false);
6263
}
@@ -67,23 +68,23 @@ pub(crate) unsafe fn codegen(
6768
tcx,
6869
llcx,
6970
llmod,
70-
"__rust_alloc_error_handler",
71-
alloc_error_handler_name(alloc_error_handler_kind),
71+
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
72+
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
7273
&[usize, usize], // size, align
7374
None,
7475
true,
7576
);
7677

7778
unsafe {
7879
// __rust_alloc_error_handler_should_panic
79-
let name = OomStrategy::SYMBOL;
80+
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
8081
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_c_char_ptr(), name.len(), i8);
8182
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
8283
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
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_c_char_ptr(), name.len(), i8);
8889
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
8990
let llval = llvm::LLVMConstInt(i8, 0, False);

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_session::config::{
2525
};
2626
use rustc_span::source_map::Spanned;
2727
use rustc_span::{DUMMY_SP, Span};
28+
use rustc_symbol_mangling::mangle_internal_symbol;
2829
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
2930
use smallvec::SmallVec;
3031

@@ -1109,7 +1110,7 @@ impl<'ll> CodegenCx<'ll, '_> {
11091110
Some(def_id) => self.get_static(def_id),
11101111
_ => {
11111112
let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
1112-
self.declare_global("rust_eh_catch_typeinfo", ty)
1113+
self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
11131114
}
11141115
};
11151116
self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, LayoutOf};
1414
use rustc_middle::ty::{self, GenericArgsRef, Ty};
1515
use rustc_middle::{bug, span_bug};
1616
use rustc_span::{Span, Symbol, sym};
17+
use rustc_symbol_mangling::mangle_internal_symbol;
1718
use rustc_target::spec::{HasTargetSpec, PanicStrategy};
1819
use tracing::debug;
1920

@@ -783,7 +784,10 @@ fn codegen_msvc_try<'ll>(
783784
let type_name = bx.const_bytes(b"rust_panic\0");
784785
let type_info =
785786
bx.const_struct(&[type_info_vtable, bx.const_null(bx.type_ptr()), type_name], false);
786-
let tydesc = bx.declare_global("__rust_panic_type_info", bx.val_ty(type_info));
787+
let tydesc = bx.declare_global(
788+
&mangle_internal_symbol(bx.tcx, "__rust_panic_type_info"),
789+
bx.val_ty(type_info),
790+
);
787791

788792
llvm::set_linkage(tydesc, llvm::Linkage::LinkOnceODRLinkage);
789793
if bx.cx.tcx.sess.target.supports_comdat() {

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::hash_map::Entry::*;
22

3-
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE};
3+
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE, global_fn_name};
44
use rustc_data_structures::unord::UnordMap;
55
use rustc_hir::def::DefKind;
66
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId};
@@ -13,6 +13,7 @@ use rustc_middle::query::LocalCrate;
1313
use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Instance, SymbolName, TyCtxt};
1414
use rustc_middle::util::Providers;
1515
use rustc_session::config::{CrateType, OomStrategy};
16+
use rustc_symbol_mangling::mangle_internal_symbol;
1617
use rustc_target::spec::{SanitizerSet, TlsModel};
1718
use tracing::debug;
1819

@@ -209,8 +210,11 @@ fn exported_symbols_provider_local(
209210
if allocator_kind_for_codegen(tcx).is_some() {
210211
for symbol_name in ALLOCATOR_METHODS
211212
.iter()
212-
.map(|method| format!("__rust_{}", method.name))
213-
.chain(["__rust_alloc_error_handler".to_string(), OomStrategy::SYMBOL.to_string()])
213+
.map(|method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
214+
.chain([
215+
mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
216+
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
217+
])
214218
{
215219
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
216220

@@ -221,8 +225,10 @@ fn exported_symbols_provider_local(
221225
}));
222226
}
223227

224-
let exported_symbol =
225-
ExportedSymbol::NoDefId(SymbolName::new(tcx, NO_ALLOC_SHIM_IS_UNSTABLE));
228+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(
229+
tcx,
230+
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
231+
));
226232
symbols.push((exported_symbol, SymbolExportInfo {
227233
level: SymbolExportLevel::Rust,
228234
kind: SymbolExportKind::Data,

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_session::Session;
2727
use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
2828
use rustc_span::symbol::sym;
2929
use rustc_span::{DUMMY_SP, Symbol};
30+
use rustc_symbol_mangling::mangle_internal_symbol;
3031
use rustc_trait_selection::infer::at::ToTrace;
3132
use rustc_trait_selection::infer::{BoundRegionConversionTime, TyCtxtInferExt};
3233
use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
@@ -982,7 +983,12 @@ impl CrateInfo {
982983
.for_each(|(_, linked_symbols)| {
983984
let mut symbols = missing_weak_lang_items
984985
.iter()
985-
.map(|item| (format!("{prefix}{item}"), SymbolExportKind::Text))
986+
.map(|item| {
987+
(
988+
format!("{prefix}{}", mangle_internal_symbol(tcx, item.as_str())),
989+
SymbolExportKind::Text,
990+
)
991+
})
986992
.collect::<Vec<_>>();
987993
symbols.sort_unstable_by(|a, b| a.0.cmp(&b.0));
988994
linked_symbols.extend(symbols);
@@ -995,7 +1001,13 @@ impl CrateInfo {
9951001
// errors.
9961002
linked_symbols.extend(ALLOCATOR_METHODS.iter().map(|method| {
9971003
(
998-
format!("{prefix}{}", global_fn_name(method.name).as_str()),
1004+
format!(
1005+
"{prefix}{}",
1006+
mangle_internal_symbol(
1007+
tcx,
1008+
global_fn_name(method.name).as_str()
1009+
)
1010+
),
9991011
SymbolExportKind::Text,
10001012
)
10011013
}));

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -635,25 +635,19 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
635635
// strippable by the linker.
636636
//
637637
// Additionally weak lang items have predetermined symbol names.
638-
if WEAK_LANG_ITEMS.iter().any(|&l| tcx.lang_items().get(l) == Some(did.to_def_id())) {
639-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
640-
}
641638
if let Some((name, _)) = lang_items::extract(attrs)
642639
&& let Some(lang_item) = LangItem::from_name(name)
643-
&& let Some(link_name) = lang_item.link_name()
644640
{
645-
codegen_fn_attrs.export_name = Some(link_name);
646-
codegen_fn_attrs.link_name = Some(link_name);
641+
if WEAK_LANG_ITEMS.iter().any(|&l| l == lang_item) {
642+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
643+
}
644+
if let Some(link_name) = lang_item.link_name() {
645+
codegen_fn_attrs.export_name = Some(link_name);
646+
codegen_fn_attrs.link_name = Some(link_name);
647+
}
647648
}
648649
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
649650

650-
// Internal symbols to the standard library all have no_mangle semantics in
651-
// that they have defined symbol names present in the function name. This
652-
// also applies to weak symbols where they all have known symbol names.
653-
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
654-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
655-
}
656-
657651
// Any linkage to LLVM intrinsics for now forcibly marks them all as never
658652
// unwinds since LLVM sometimes can't handle codegen which `invoke`s
659653
// intrinsic functions.

0 commit comments

Comments
 (0)