Skip to content

Commit 6f1a70d

Browse files
Allow for re-using hidden monomorphizations on platforms that don't support Rust dylibs.
1 parent 3d282ff commit 6f1a70d

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/librustc/ty/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
14951495

14961496
#[inline]
14971497
pub fn local_crate_exports_generics(self) -> bool {
1498+
debug_assert!(self.share_generics());
1499+
14981500
self.sess.crate_types.borrow().iter().any(|crate_type| {
14991501
match crate_type {
15001502
CrateTypeExecutable |

src/librustc_trans/back/symbol_export.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,37 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
233233
use rustc::mir::mono::{Linkage, Visibility, MonoItem};
234234
use rustc::ty::InstanceDef;
235235

236+
// Normally, we require that shared monomorphizations are not hidden,
237+
// because if we want to re-use a monomorphization from a Rust dylib, it
238+
// needs to be exported.
239+
// However, on platforms that don't allow for Rust dylibs, having
240+
// external linkage is enough for monomorphization to be linked to.
241+
let need_visibility = tcx.sess.target.target.options.dynamic_linking &&
242+
!tcx.sess.target.target.options.only_cdylib;
243+
236244
let (_, cgus) = tcx.collect_and_partition_translation_items(LOCAL_CRATE);
237245

238246
for (mono_item, &(linkage, visibility)) in cgus.iter()
239247
.flat_map(|cgu| cgu.items().iter()) {
240-
if linkage == Linkage::External && visibility == Visibility::Default {
241-
if let &MonoItem::Fn(Instance {
242-
def: InstanceDef::Item(def_id),
243-
substs,
244-
}) = mono_item {
245-
if substs.types().next().is_some() {
246-
symbols.push((ExportedSymbol::Generic(def_id, substs),
247-
SymbolExportLevel::Rust));
248-
}
248+
if linkage != Linkage::External {
249+
// We can only re-use things with external linkage, otherwise
250+
// we'll get a linker error
251+
continue
252+
}
253+
254+
if need_visibility && visibility == Visibility::Hidden {
255+
// If we potentially share things from Rust dylibs, they must
256+
// not be hidden
257+
continue
258+
}
259+
260+
if let &MonoItem::Fn(Instance {
261+
def: InstanceDef::Item(def_id),
262+
substs,
263+
}) = mono_item {
264+
if substs.types().next().is_some() {
265+
symbols.push((ExportedSymbol::Generic(def_id, substs),
266+
SymbolExportLevel::Rust));
249267
}
250268
}
251269
}

0 commit comments

Comments
 (0)