Skip to content

Commit

Permalink
rustc: Switch start_fn to hidden visibility
Browse files Browse the repository at this point in the history
This'll avoid exporting a symbol from binaries unnecessarily and should help the
linker clean things up if it can.
  • Loading branch information
alexcrichton committed Dec 26, 2017
1 parent 81e375d commit 2cdd1c4
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/librustc_mir/monomorphize/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,35 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let codegen_unit = codegen_units.entry(codegen_unit_name.clone())
.or_insert_with(make_codegen_unit);

let mut can_be_internalized = true;
let (linkage, visibility) = match trans_item.explicit_linkage(tcx) {
Some(explicit_linkage) => (explicit_linkage, Visibility::Default),
None => {
match trans_item {
MonoItem::Fn(ref instance) => {
let visibility = match instance.def {
InstanceDef::Item(def_id) => {
// If we encounter the lang start item, we set the visibility to
// default.
// The `start_fn` lang item is actually a
// monomorphized instance of a function in the
// standard library, used for the `main`
// function. We don't want to export it so we
// tag it with `Hidden` visibility but this
// symbol is only referenced from the actual
// `main` symbol which we unfortunately don't
// know anything about during
// partitioning/collection. As a result we
// forcibly keep this symbol out of the
// `internalization_candidates` set.
//
// FIXME: eventually we don't want to always
// force this symbol to have hidden
// visibility, it should indeed be a candidate
// for internalization, but we have to
// understand that it's referenced from the
// `main` symbol we'll generate later.
if tcx.lang_items().start_fn() == Some(def_id) {
Visibility::Default
can_be_internalized = false;
Visibility::Hidden
} else if def_id.is_local() {
if tcx.is_exported_symbol(def_id) {
Visibility::Default
Expand Down Expand Up @@ -350,7 +368,7 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
}
};
if visibility == Visibility::Hidden {
if visibility == Visibility::Hidden && can_be_internalized {
internalization_candidates.insert(trans_item);
}

Expand Down

0 comments on commit 2cdd1c4

Please sign in to comment.