Skip to content

Commit 7177afd

Browse files
committed
Add all rustc_std_internal_symbol to symbols.o
rustc_std_internal_symbol is meant to call functions from crates where there is no direct dependency on said crate. As they either have to be added to symbols.o or rustc has to introduce an implicit dependency on them to avoid linker errors. The latter is done for some things like the panic runtime, but adding these symbols to symbols.o allows removing those implicit dependencies.
1 parent 1973872 commit 7177afd

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,7 @@ pub(crate) fn linked_symbols(
18291829
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
18301830
if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum)
18311831
|| info.used
1832+
|| info.rustc_std_internal_symbol
18321833
{
18331834
symbols.push((
18341835
symbol_export::linking_symbol_name_for_instance_in_crate(tcx, symbol, cnum),

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+17
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
131131
used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED)
132132
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
133133
|| used,
134+
rustc_std_internal_symbol: codegen_attrs
135+
.flags
136+
.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL),
134137
};
135138
(def_id.to_def_id(), info)
136139
})
@@ -143,6 +146,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
143146
level: SymbolExportLevel::C,
144147
kind: SymbolExportKind::Data,
145148
used: false,
149+
rustc_std_internal_symbol: false,
146150
},
147151
);
148152
}
@@ -191,6 +195,7 @@ fn exported_symbols_provider_local<'tcx>(
191195
level: info.level,
192196
kind: SymbolExportKind::Text,
193197
used: info.used,
198+
rustc_std_internal_symbol: info.rustc_std_internal_symbol,
194199
},
195200
)
196201
})
@@ -207,6 +212,7 @@ fn exported_symbols_provider_local<'tcx>(
207212
level: SymbolExportLevel::C,
208213
kind: SymbolExportKind::Text,
209214
used: false,
215+
rustc_std_internal_symbol: false,
210216
},
211217
));
212218
}
@@ -229,6 +235,7 @@ fn exported_symbols_provider_local<'tcx>(
229235
level: SymbolExportLevel::Rust,
230236
kind: SymbolExportKind::Text,
231237
used: false,
238+
rustc_std_internal_symbol: true,
232239
},
233240
));
234241
}
@@ -243,6 +250,7 @@ fn exported_symbols_provider_local<'tcx>(
243250
level: SymbolExportLevel::Rust,
244251
kind: SymbolExportKind::Data,
245252
used: false,
253+
rustc_std_internal_symbol: true,
246254
},
247255
))
248256
}
@@ -262,6 +270,7 @@ fn exported_symbols_provider_local<'tcx>(
262270
level: SymbolExportLevel::C,
263271
kind: SymbolExportKind::Data,
264272
used: false,
273+
rustc_std_internal_symbol: false,
265274
},
266275
)
267276
}));
@@ -287,6 +296,7 @@ fn exported_symbols_provider_local<'tcx>(
287296
level: SymbolExportLevel::C,
288297
kind: SymbolExportKind::Data,
289298
used: false,
299+
rustc_std_internal_symbol: false,
290300
},
291301
)
292302
}));
@@ -304,6 +314,7 @@ fn exported_symbols_provider_local<'tcx>(
304314
level: SymbolExportLevel::C,
305315
kind: SymbolExportKind::Data,
306316
used: true,
317+
rustc_std_internal_symbol: false,
307318
},
308319
));
309320
}
@@ -379,6 +390,8 @@ fn exported_symbols_provider_local<'tcx>(
379390
}
380391
}
381392

393+
// Note: These all set rustc_std_internal_symbol to false as generic functions must not
394+
// be marked with this attribute and we are only handling generic functions here.
382395
match *mono_item {
383396
MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
384397
let has_generics = args.non_erasable_generics().next().is_some();
@@ -394,6 +407,7 @@ fn exported_symbols_provider_local<'tcx>(
394407
level: SymbolExportLevel::Rust,
395408
kind: SymbolExportKind::Text,
396409
used: false,
410+
rustc_std_internal_symbol: false,
397411
},
398412
));
399413
}
@@ -416,6 +430,7 @@ fn exported_symbols_provider_local<'tcx>(
416430
level: SymbolExportLevel::Rust,
417431
kind: SymbolExportKind::Text,
418432
used: false,
433+
rustc_std_internal_symbol: false,
419434
},
420435
));
421436
}
@@ -432,6 +447,7 @@ fn exported_symbols_provider_local<'tcx>(
432447
level: SymbolExportLevel::Rust,
433448
kind: SymbolExportKind::Text,
434449
used: false,
450+
rustc_std_internal_symbol: false,
435451
},
436452
));
437453
}
@@ -442,6 +458,7 @@ fn exported_symbols_provider_local<'tcx>(
442458
level: SymbolExportLevel::Rust,
443459
kind: SymbolExportKind::Text,
444460
used: false,
461+
rustc_std_internal_symbol: false,
445462
},
446463
));
447464
}

compiler/rustc_codegen_ssa/src/base.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::{Duration, Instant};
66
use itertools::Itertools;
77
use rustc_abi::FIRST_VARIANT;
88
use rustc_ast as ast;
9-
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, AllocatorKind, global_fn_name};
9+
use rustc_ast::expand::allocator::AllocatorKind;
1010
use rustc_attr_parsing::OptimizeAttr;
1111
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1212
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
@@ -1074,26 +1074,6 @@ impl CrateInfo {
10741074
.collect::<Vec<_>>();
10751075
symbols.sort_unstable_by(|a, b| a.0.cmp(&b.0));
10761076
linked_symbols.extend(symbols);
1077-
if tcx.allocator_kind(()).is_some() {
1078-
// At least one crate needs a global allocator. This crate may be placed
1079-
// after the crate that defines it in the linker order, in which case some
1080-
// linkers return an error. By adding the global allocator shim methods to
1081-
// the linked_symbols list, linking the generated symbols.o will ensure that
1082-
// circular dependencies involving the global allocator don't lead to linker
1083-
// errors.
1084-
linked_symbols.extend(ALLOCATOR_METHODS.iter().map(|method| {
1085-
(
1086-
format!(
1087-
"{prefix}{}",
1088-
mangle_internal_symbol(
1089-
tcx,
1090-
global_fn_name(method.name).as_str()
1091-
)
1092-
),
1093-
SymbolExportKind::Text,
1094-
)
1095-
}));
1096-
}
10971077
});
10981078
}
10991079

compiler/rustc_middle/src/middle/exported_symbols.rs

+3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ pub enum SymbolExportKind {
3535
pub struct SymbolExportInfo {
3636
pub level: SymbolExportLevel,
3737
pub kind: SymbolExportKind,
38+
/// Was the symbol marked as `#[used(compiler)]` or `#[used(linker)]`?
3839
pub used: bool,
40+
/// Was the symbol marked as `#[rustc_std_internal_symbol]`?
41+
pub rustc_std_internal_symbol: bool,
3942
}
4043

4144
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]

0 commit comments

Comments
 (0)