X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc_codegen_utils%2Fsymbol_names.rs;h=bd714f5c6b715b354effd7335faebd6ca228819b;hb=7bc1665730002a5368b4384613758c7ddc77db09;hp=c52c6cfa83c917c64ce7d3b1902927805d6f4218;hpb=1b0367146a55d245373e135deac6978a1095224e;p=rust.git diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index c52c6cfa83c..bd714f5c6b7 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -88,12 +88,12 @@ //! DefPaths which are much more robust in the face of changes to the code base. use rustc::hir::def_id::LOCAL_CRATE; -use rustc::hir::Node; use rustc::hir::CodegenFnAttrFlags; +use rustc::hir::Node; +use rustc::mir::mono::{InstantiationMode, MonoItem}; use rustc::session::config::SymbolManglingVersion; use rustc::ty::query::Providers; -use rustc::ty::{self, TyCtxt, Instance}; -use rustc::mir::mono::{MonoItem, InstantiationMode}; +use rustc::ty::{self, Instance, TyCtxt}; use syntax_pos::symbol::Symbol; @@ -104,9 +104,7 @@ pub fn provide(providers: &mut Providers<'_>) { *providers = Providers { - symbol_name: |tcx, instance| ty::SymbolName { - name: symbol_name(tcx, instance), - }, + symbol_name: |tcx, instance| ty::SymbolName { name: symbol_name(tcx, instance) }, ..*providers }; @@ -142,12 +140,32 @@ fn symbol_name(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Symbol { }; let attrs = tcx.codegen_fn_attrs(def_id); + + // Foreign items by default use no mangling for their symbol name. There's a + // few exceptions to this rule though: + // + // * This can be overridden with the `#[link_name]` attribute + // + // * On the wasm32 targets there is a bug (or feature) in LLD [1] where the + // same-named symbol when imported from different wasm modules will get + // hooked up incorectly. As a result foreign symbols, on the wasm target, + // with a wasm import module, get mangled. Additionally our codegen will + // deduplicate symbols based purely on the symbol name, but for wasm this + // isn't quite right because the same-named symbol on wasm can come from + // different modules. For these reasons if `#[link(wasm_import_module)]` + // is present we mangle everything on wasm because the demangled form will + // show up in the `wasm-import-name` custom attribute in LLVM IR. + // + // [1]: https://bugs.llvm.org/show_bug.cgi?id=44316 if is_foreign { - if let Some(name) = attrs.link_name { - return name; + if tcx.sess.target.target.arch != "wasm32" + || !tcx.wasm_import_module_map(def_id.krate).contains_key(&def_id) + { + if let Some(name) = attrs.link_name { + return name; + } + return tcx.item_name(def_id); } - // Don't mangle foreign items. - return tcx.item_name(def_id); } if let Some(name) = attrs.export_name {