1
Fork 0

Support TLS access into dylibs on Windows

This commit is contained in:
John Kåre Alsaker 2023-02-03 09:04:12 +01:00
parent 51c93553d4
commit 0d89c6a2d4
23 changed files with 207 additions and 47 deletions

View file

@ -190,7 +190,8 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::query::TyCtxtAt;
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
use rustc_middle::ty::{
self, GenericParamDefKind, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, VtblEntry,
self, GenericParamDefKind, Instance, InstanceDef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt,
VtblEntry,
};
use rustc_middle::{middle::codegen_fn_attrs::CodegenFnAttrFlags, mir::visit::TyContext};
use rustc_session::config::EntryFnType;
@ -462,6 +463,16 @@ fn collect_items_rec<'tcx>(
collect_miri(tcx, id, &mut neighbors);
}
}
if tcx.needs_thread_local_shim(def_id) {
neighbors.push(respan(
starting_point.span,
MonoItem::Fn(Instance {
def: InstanceDef::ThreadLocalShim(def_id),
substs: InternalSubsts::empty(),
}),
));
}
}
MonoItem::Fn(instance) => {
// Sanity check whether this ended up being collected accidentally
@ -962,6 +973,9 @@ fn visit_instance_use<'tcx>(
bug!("{:?} being reified", instance);
}
}
ty::InstanceDef::ThreadLocalShim(..) => {
bug!("{:?} being reified", instance);
}
ty::InstanceDef::DropGlue(_, None) => {
// Don't need to emit noop drop glue if we are calling directly.
if !is_direct_call {
@ -1210,11 +1224,9 @@ impl<'v> RootCollector<'_, 'v> {
self.output.push(dummy_spanned(MonoItem::GlobalAsm(id)));
}
DefKind::Static(..) => {
debug!(
"RootCollector: ItemKind::Static({})",
self.tcx.def_path_str(id.owner_id.to_def_id())
);
self.output.push(dummy_spanned(MonoItem::Static(id.owner_id.to_def_id())));
let def_id = id.owner_id.to_def_id();
debug!("RootCollector: ItemKind::Static({})", self.tcx.def_path_str(def_id));
self.output.push(dummy_spanned(MonoItem::Static(def_id)));
}
DefKind::Const => {
// const items only generate mono items if they are

View file

@ -279,6 +279,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
| ty::InstanceDef::DropGlue(..)
| ty::InstanceDef::Virtual(..)
| ty::InstanceDef::CloneShim(..)
| ty::InstanceDef::ThreadLocalShim(..)
| ty::InstanceDef::FnPtrAddrShim(..) => return None,
};
@ -392,6 +393,19 @@ fn mono_item_linkage_and_visibility<'tcx>(
type CguNameCache = FxHashMap<(DefId, bool), Symbol>;
fn static_visibility<'tcx>(
tcx: TyCtxt<'tcx>,
can_be_internalized: &mut bool,
def_id: DefId,
) -> Visibility {
if tcx.is_reachable_non_generic(def_id) {
*can_be_internalized = false;
default_visibility(tcx, def_id, false)
} else {
Visibility::Hidden
}
}
fn mono_item_visibility<'tcx>(
tcx: TyCtxt<'tcx>,
mono_item: &MonoItem<'tcx>,
@ -403,21 +417,9 @@ fn mono_item_visibility<'tcx>(
MonoItem::Fn(instance) => instance,
// Misc handling for generics and such, but otherwise:
MonoItem::Static(def_id) => {
return if tcx.is_reachable_non_generic(*def_id) {
*can_be_internalized = false;
default_visibility(tcx, *def_id, false)
} else {
Visibility::Hidden
};
}
MonoItem::Static(def_id) => return static_visibility(tcx, can_be_internalized, *def_id),
MonoItem::GlobalAsm(item_id) => {
return if tcx.is_reachable_non_generic(item_id.owner_id) {
*can_be_internalized = false;
default_visibility(tcx, item_id.owner_id.to_def_id(), false)
} else {
Visibility::Hidden
};
return static_visibility(tcx, can_be_internalized, item_id.owner_id.to_def_id());
}
};
@ -425,6 +427,11 @@ fn mono_item_visibility<'tcx>(
InstanceDef::Item(def) => def.did,
InstanceDef::DropGlue(def_id, Some(_)) => def_id,
// We match the visiblity of statics here
InstanceDef::ThreadLocalShim(def_id) => {
return static_visibility(tcx, can_be_internalized, def_id);
}
// These are all compiler glue and such, never exported, always hidden.
InstanceDef::VTableShim(..)
| InstanceDef::ReifyShim(..)