Skip to content

Commit 6e5f718

Browse files
committed
Remove implicit #[no_mangle] for #[rustc_std_internal_symbol]
1 parent 9eee105 commit 6e5f718

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
@@ -26,6 +26,7 @@ extern crate rustc_index;
2626
extern crate rustc_metadata;
2727
extern crate rustc_session;
2828
extern crate rustc_span;
29+
extern crate rustc_symbol_mangling;
2930
extern crate rustc_target;
3031
#[macro_use]
3132
extern crate tracing;

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
#[cfg(feature = "master")]
@@ -53,8 +54,8 @@ pub(crate) unsafe fn codegen(
5354
panic!("invalid allocator output")
5455
}
5556
};
56-
let from_name = global_fn_name(method.name);
57-
let to_name = default_fn_name(method.name);
57+
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
58+
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
5859

5960
create_wrapper_function(tcx, context, &from_name, &to_name, &types, output);
6061
}
@@ -64,13 +65,13 @@ pub(crate) unsafe fn codegen(
6465
create_wrapper_function(
6566
tcx,
6667
context,
67-
"__rust_alloc_error_handler",
68-
alloc_error_handler_name(alloc_error_handler_kind),
68+
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
69+
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
6970
&[usize, usize],
7071
None,
7172
);
7273

73-
let name = OomStrategy::SYMBOL.to_string();
74+
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
7475
let global = context.new_global(None, GlobalKind::Exported, i8, name);
7576
#[cfg(feature = "master")]
7677
global.add_attribute(VarAttribute::Visibility(symbol_visibility_to_gcc(
@@ -80,7 +81,7 @@ pub(crate) unsafe fn codegen(
8081
let value = context.new_rvalue_from_int(i8, value as i32);
8182
global.global_set_initializer_rvalue(value);
8283

83-
let name = NO_ALLOC_SHIM_IS_UNSTABLE.to_string();
84+
let name = mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE);
8485
let global = context.new_global(None, GlobalKind::Exported, i8, name);
8586
#[cfg(feature = "master")]
8687
global.add_attribute(VarAttribute::Visibility(symbol_visibility_to_gcc(

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ extern crate rustc_metadata;
5252
extern crate rustc_middle;
5353
extern crate rustc_session;
5454
extern crate rustc_span;
55+
extern crate rustc_symbol_mangling;
5556
extern crate rustc_target;
5657

5758
// 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
@@ -27,6 +27,7 @@ use rustc_session::config::{
2727
};
2828
use rustc_span::source_map::Spanned;
2929
use rustc_span::{DUMMY_SP, Span};
30+
use rustc_symbol_mangling::mangle_internal_symbol;
3031
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
3132
use smallvec::SmallVec;
3233

@@ -1177,7 +1178,7 @@ impl<'ll> CodegenCx<'ll, '_> {
11771178
Some(def_id) => self.get_static(def_id),
11781179
_ => {
11791180
let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
1180-
self.declare_global("rust_eh_catch_typeinfo", ty)
1181+
self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
11811182
}
11821183
};
11831184
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

@@ -814,7 +815,10 @@ fn codegen_msvc_try<'ll>(
814815
let type_name = bx.const_bytes(b"rust_panic\0");
815816
let type_info =
816817
bx.const_struct(&[type_info_vtable, bx.const_null(bx.type_ptr()), type_name], false);
817-
let tydesc = bx.declare_global("__rust_panic_type_info", bx.val_ty(type_info));
818+
let tydesc = bx.declare_global(
819+
&mangle_internal_symbol(bx.tcx, "__rust_panic_type_info"),
820+
bx.val_ty(type_info),
821+
);
818822

819823
llvm::set_linkage(tydesc, llvm::Linkage::LinkOnceODRLinkage);
820824
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
@@ -25,6 +25,7 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2525
use rustc_session::Session;
2626
use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
2727
use rustc_span::{DUMMY_SP, Symbol, sym};
28+
use rustc_symbol_mangling::mangle_internal_symbol;
2829
use rustc_trait_selection::infer::{BoundRegionConversionTime, TyCtxtInferExt};
2930
use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
3031
use tracing::{debug, info};
@@ -992,7 +993,12 @@ impl CrateInfo {
992993
.for_each(|(_, linked_symbols)| {
993994
let mut symbols = missing_weak_lang_items
994995
.iter()
995-
.map(|item| (format!("{prefix}{item}"), SymbolExportKind::Text))
996+
.map(|item| {
997+
(
998+
format!("{prefix}{}", mangle_internal_symbol(tcx, item.as_str())),
999+
SymbolExportKind::Text,
1000+
)
1001+
})
9961002
.collect::<Vec<_>>();
9971003
symbols.sort_unstable_by(|a, b| a.0.cmp(&b.0));
9981004
linked_symbols.extend(symbols);
@@ -1005,7 +1011,13 @@ impl CrateInfo {
10051011
// errors.
10061012
linked_symbols.extend(ALLOCATOR_METHODS.iter().map(|method| {
10071013
(
1008-
format!("{prefix}{}", global_fn_name(method.name).as_str()),
1014+
format!(
1015+
"{prefix}{}",
1016+
mangle_internal_symbol(
1017+
tcx,
1018+
global_fn_name(method.name).as_str()
1019+
)
1020+
),
10091021
SymbolExportKind::Text,
10101022
)
10111023
}));

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -682,25 +682,19 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
682682
// strippable by the linker.
683683
//
684684
// Additionally weak lang items have predetermined symbol names.
685-
if WEAK_LANG_ITEMS.iter().any(|&l| tcx.lang_items().get(l) == Some(did.to_def_id())) {
686-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
687-
}
688685
if let Some((name, _)) = lang_items::extract(attrs)
689686
&& let Some(lang_item) = LangItem::from_name(name)
690-
&& let Some(link_name) = lang_item.link_name()
691687
{
692-
codegen_fn_attrs.export_name = Some(link_name);
693-
codegen_fn_attrs.link_name = Some(link_name);
688+
if WEAK_LANG_ITEMS.iter().any(|&l| l == lang_item) {
689+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
690+
}
691+
if let Some(link_name) = lang_item.link_name() {
692+
codegen_fn_attrs.export_name = Some(link_name);
693+
codegen_fn_attrs.link_name = Some(link_name);
694+
}
694695
}
695696
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
696697

697-
// Internal symbols to the standard library all have no_mangle semantics in
698-
// that they have defined symbol names present in the function name. This
699-
// also applies to weak symbols where they all have known symbol names.
700-
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
701-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
702-
}
703-
704698
// Any linkage to LLVM intrinsics for now forcibly marks them all as never
705699
// unwinds since LLVM sometimes can't handle codegen which `invoke`s
706700
// intrinsic functions.

0 commit comments

Comments
 (0)