Skip to content

Commit 2cdd1c4

Browse files
committed
rustc: Switch start_fn to hidden visibility
This'll avoid exporting a symbol from binaries unnecessarily and should help the linker clean things up if it can.
1 parent 81e375d commit 2cdd1c4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/librustc_mir/monomorphize/partitioning.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,35 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
305305
let codegen_unit = codegen_units.entry(codegen_unit_name.clone())
306306
.or_insert_with(make_codegen_unit);
307307

308+
let mut can_be_internalized = true;
308309
let (linkage, visibility) = match trans_item.explicit_linkage(tcx) {
309310
Some(explicit_linkage) => (explicit_linkage, Visibility::Default),
310311
None => {
311312
match trans_item {
312313
MonoItem::Fn(ref instance) => {
313314
let visibility = match instance.def {
314315
InstanceDef::Item(def_id) => {
315-
// If we encounter the lang start item, we set the visibility to
316-
// default.
316+
// The `start_fn` lang item is actually a
317+
// monomorphized instance of a function in the
318+
// standard library, used for the `main`
319+
// function. We don't want to export it so we
320+
// tag it with `Hidden` visibility but this
321+
// symbol is only referenced from the actual
322+
// `main` symbol which we unfortunately don't
323+
// know anything about during
324+
// partitioning/collection. As a result we
325+
// forcibly keep this symbol out of the
326+
// `internalization_candidates` set.
327+
//
328+
// FIXME: eventually we don't want to always
329+
// force this symbol to have hidden
330+
// visibility, it should indeed be a candidate
331+
// for internalization, but we have to
332+
// understand that it's referenced from the
333+
// `main` symbol we'll generate later.
317334
if tcx.lang_items().start_fn() == Some(def_id) {
318-
Visibility::Default
335+
can_be_internalized = false;
336+
Visibility::Hidden
319337
} else if def_id.is_local() {
320338
if tcx.is_exported_symbol(def_id) {
321339
Visibility::Default
@@ -350,7 +368,7 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
350368
}
351369
}
352370
};
353-
if visibility == Visibility::Hidden {
371+
if visibility == Visibility::Hidden && can_be_internalized {
354372
internalization_candidates.insert(trans_item);
355373
}
356374

0 commit comments

Comments
 (0)