Skip to content

Commit eea99b4

Browse files
committed
Mangle #[rustc_std_internal_symbol] to include the rustc version unless #[no_mangle] is used
1 parent 483bc03 commit eea99b4

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ mod v0;
112112
pub mod errors;
113113
pub mod test;
114114

115+
pub use v0::mangle_internal_symbol;
116+
115117
/// This function computes the symbol name for the given `instance` and the
116118
/// given instantiating crate. That is, if you know that instance X is
117119
/// instantiated in crate Y, this is the symbol name this instance would have.
@@ -183,6 +185,39 @@ fn compute_symbol_name<'tcx>(
183185
CodegenFnAttrs::EMPTY
184186
};
185187

188+
if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
189+
// Items marked as #[rustc_std_internal_symbol] need to have a fixed
190+
// symbol name because it is used to import items from another crate
191+
// without a direct dependency. As such it is not possible to look up
192+
// the mangled name for the `Instance` from the crate metadata of the
193+
// defining crate.
194+
// Weak lang items automatically get #[rustc_std_internal_symbol]
195+
// applied by the code computing the CodegenFnAttrs.
196+
// We are mangling all #[rustc_std_internal_symbol] items that don't
197+
// also have #[no_mangle] as a combination of the rustc version and the
198+
// unmangled linkage name. This is to ensure that if we link against a
199+
// staticlib compiled by a different rustc version, we don't get symbol
200+
// conflicts or even UB due to a different implementation/ABI. Rust
201+
// staticlibs currently export all symbols, including those that are
202+
// hidden in cdylibs.
203+
// We are using the v0 symbol mangling scheme here as we need to be
204+
// consistent across all crates and in some contexts the legacy symbol
205+
// mangling scheme can't be used. For example both the GCC backend and
206+
// Rust-for-Linux don't support some of the characters used by the
207+
// legacy symbol mangling scheme.
208+
let name = if tcx.is_foreign_item(def_id) {
209+
if let Some(name) = attrs.link_name { name } else { tcx.item_name(def_id) }
210+
} else {
211+
if let Some(name) = attrs.export_name { name } else { tcx.item_name(def_id) }
212+
};
213+
214+
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
215+
return name.to_string();
216+
} else {
217+
return v0::mangle_internal_symbol(tcx, name.as_str());
218+
}
219+
}
220+
186221
// Foreign items by default use no mangling for their symbol name. There's a
187222
// few exceptions to this rule though:
188223
//
@@ -198,6 +233,8 @@ fn compute_symbol_name<'tcx>(
198233
// is present we mangle everything on wasm because the demangled form will
199234
// show up in the `wasm-import-name` custom attribute in LLVM IR.
200235
//
236+
// * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
237+
// both for exports and imports through foreign items. This is handled above.
201238
// [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
202239
if tcx.is_foreign_item(def_id)
203240
&& (!tcx.sess.target.is_like_wasm

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::fmt::Write;
2+
use std::hash::Hasher;
23
use std::iter;
34
use std::ops::Range;
45

56
use rustc_abi::{ExternAbi, Integer};
67
use rustc_data_structures::base_n::ToBaseN;
78
use rustc_data_structures::fx::FxHashMap;
89
use rustc_data_structures::intern::Interned;
10+
use rustc_data_structures::stable_hasher::{Hash64, StableHasher};
911
use rustc_hir as hir;
1012
use rustc_hir::def::CtorKind;
1113
use rustc_hir::def_id::{CrateNum, DefId};
@@ -70,6 +72,45 @@ pub(super) fn mangle<'tcx>(
7072
std::mem::take(&mut cx.out)
7173
}
7274

75+
pub fn mangle_internal_symbol<'tcx>(tcx: TyCtxt<'tcx>, item_name: &str) -> String {
76+
let prefix = "_R";
77+
let mut cx: SymbolMangler<'_> = SymbolMangler {
78+
tcx,
79+
start_offset: prefix.len(),
80+
paths: FxHashMap::default(),
81+
types: FxHashMap::default(),
82+
consts: FxHashMap::default(),
83+
binders: vec![],
84+
out: String::from(prefix),
85+
};
86+
87+
cx.path_append_ns(
88+
|cx| {
89+
cx.push("C");
90+
cx.push_disambiguator({
91+
let mut hasher = StableHasher::new();
92+
// Incorporate the rustc version to ensure #[rustc_std_internal_symbol] functions
93+
// get a different symbol name depending on the rustc version.
94+
//
95+
// RUSTC_FORCE_RUSTC_VERSION is ignored here as otherwise different we would get an
96+
// abi incompatibility with the standard library.
97+
hasher.write(tcx.sess.cfg_version.as_bytes());
98+
99+
let hash: Hash64 = hasher.finish();
100+
hash.as_u64()
101+
});
102+
cx.push_ident("__rustc");
103+
Ok(())
104+
},
105+
'v',
106+
0,
107+
item_name,
108+
)
109+
.unwrap();
110+
111+
std::mem::take(&mut cx.out)
112+
}
113+
73114
pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
74115
tcx: TyCtxt<'tcx>,
75116
trait_ref: ty::PolyExistentialTraitRef<'tcx>,

0 commit comments

Comments
 (0)