Skip to content

Commit 26d7b64

Browse files
committed
Auto merge of #52993 - alexcrichton:fix-some-vis, r=michaelwoerister
rustc: Tweak visibility of some lang items This commit tweaks the linker-level visibility of some lang items that rustc uses and defines. Notably this means that `#[panic_implementation]` and `#[alloc_error_handler]` functions are never marked as `internal`. It's up to the linker to eliminate these, not rustc. Additionally `#[global_allocator]` generated symbols are no longer forced to `Default` visibility (fully exported), but rather they're relaxed to `Hidden` visibility). This symbols are *not* needed across DLL boundaries, only as a local implementation detail of the compiler-injected allocator symbols, so `Hidden` should suffice. Closes #51342 Closes #52795
2 parents 3f4f18f + 7c58ab6 commit 26d7b64

File tree

9 files changed

+345
-152
lines changed

9 files changed

+345
-152
lines changed

src/librustc/middle/reachable.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// makes all other generics or inline functions that it references
1616
// reachable as well.
1717

18-
use hir::CodegenFnAttrs;
18+
use hir::{CodegenFnAttrs, CodegenFnAttrFlags};
1919
use hir::map as hir_map;
2020
use hir::def::Def;
2121
use hir::def_id::{DefId, CrateNum};
@@ -28,7 +28,6 @@ use util::nodemap::{NodeSet, FxHashSet};
2828

2929
use rustc_target::spec::abi::Abi;
3030
use syntax::ast;
31-
use syntax::attr;
3231
use hir;
3332
use hir::def_id::LOCAL_CRATE;
3433
use hir::intravisit::{Visitor, NestedVisitorMap};
@@ -359,8 +358,12 @@ struct CollectPrivateImplItemsVisitor<'a, 'tcx: 'a> {
359358
impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx> {
360359
fn visit_item(&mut self, item: &hir::Item) {
361360
// Anything which has custom linkage gets thrown on the worklist no
362-
// matter where it is in the crate.
363-
if attr::contains_name(&item.attrs, "linkage") {
361+
// matter where it is in the crate, along with "special std symbols"
362+
// which are currently akin to allocator symbols.
363+
let def_id = self.tcx.hir.local_def_id(item.id);
364+
let codegen_attrs = self.tcx.codegen_fn_attrs(def_id);
365+
if codegen_attrs.linkage.is_some() ||
366+
codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
364367
self.worklist.push(item.id);
365368
}
366369

src/librustc_allocator/expand.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_errors;
1313
use syntax::{
1414
ast::{
1515
self, Arg, Attribute, Crate, Expr, FnHeader, Generics, Ident, Item, ItemKind,
16-
LitKind, Mac, Mod, Mutability, StrStyle, Ty, TyKind, Unsafety, VisibilityKind,
16+
Mac, Mod, Mutability, Ty, TyKind, Unsafety, VisibilityKind,
1717
},
1818
attr,
1919
codemap::{
@@ -236,17 +236,12 @@ impl<'a> AllocFnFactory<'a> {
236236
}
237237

238238
fn attrs(&self) -> Vec<Attribute> {
239-
let key = Symbol::intern("linkage");
240-
let value = LitKind::Str(Symbol::intern("external"), StrStyle::Cooked);
241-
let linkage = self.cx.meta_name_value(self.span, key, value);
242-
243239
let no_mangle = Symbol::intern("no_mangle");
244240
let no_mangle = self.cx.meta_word(self.span, no_mangle);
245241

246242
let special = Symbol::intern("rustc_std_internal_symbol");
247243
let special = self.cx.meta_word(self.span, special);
248244
vec![
249-
self.cx.attribute(self.span, linkage),
250245
self.cx.attribute(self.span, no_mangle),
251246
self.cx.attribute(self.span, special),
252247
]

src/librustc_codegen_llvm/allocator.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind
6767
if tcx.sess.target.target.options.default_hidden_visibility {
6868
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
6969
}
70-
if tcx.sess.target.target.options.requires_uwtable {
71-
attributes::emit_uwtable(llfn, true);
72-
}
70+
if tcx.sess.target.target.options.requires_uwtable {
71+
attributes::emit_uwtable(llfn, true);
72+
}
7373

7474
let callee = CString::new(kind.fn_name(method.name)).unwrap();
7575
let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
7676
callee.as_ptr(),
7777
ty);
78+
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
7879

7980
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx,
8081
llfn,

src/librustc_codegen_llvm/base.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,28 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
809809
rx,
810810
codegen_units.len());
811811

812-
// Codegen an allocator shim, if any
813-
let allocator_module = if let Some(kind) = *tcx.sess.allocator_kind.get() {
812+
// Codegen an allocator shim, if necessary.
813+
//
814+
// If the crate doesn't have an `allocator_kind` set then there's definitely
815+
// no shim to generate. Otherwise we also check our dependency graph for all
816+
// our output crate types. If anything there looks like its a `Dynamic`
817+
// linkage, then it's already got an allocator shim and we'll be using that
818+
// one instead. If nothing exists then it's our job to generate the
819+
// allocator!
820+
let any_dynamic_crate = tcx.sess.dependency_formats.borrow()
821+
.iter()
822+
.any(|(_, list)| {
823+
use rustc::middle::dependency_format::Linkage;
824+
list.iter().any(|linkage| {
825+
match linkage {
826+
Linkage::Dynamic => true,
827+
_ => false,
828+
}
829+
})
830+
});
831+
let allocator_module = if any_dynamic_crate {
832+
None
833+
} else if let Some(kind) = *tcx.sess.allocator_kind.get() {
814834
unsafe {
815835
let llmod_id = "allocator";
816836
let modules = ModuleLlvm::new(tcx.sess, llmod_id);

0 commit comments

Comments
 (0)