Skip to content

Commit b21cbfd

Browse files
committed
Fold LinkerInfo into CrateInfo
1 parent b4a12f9 commit b21cbfd

File tree

4 files changed

+48
-86
lines changed

4 files changed

+48
-86
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
17181718
path,
17191719
flavor,
17201720
crt_objects_fallback,
1721-
&codegen_results.crate_info.linker_info,
1721+
&codegen_results.crate_info.target_cpu,
17221722
);
17231723
let link_output_kind = link_output_kind(sess, crate_type);
17241724

@@ -1729,7 +1729,11 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
17291729
// dynamic library.
17301730
// Must be passed before any libraries to prevent the symbols to export from being thrown away,
17311731
// at least on some platforms (e.g. windows-gnu).
1732-
cmd.export_symbols(tmpdir, crate_type);
1732+
cmd.export_symbols(
1733+
tmpdir,
1734+
crate_type,
1735+
&codegen_results.crate_info.exported_symbols[&crate_type],
1736+
);
17331737

17341738
// Can be used for adding custom CRT objects or overriding order-dependent options above.
17351739
// FIXME: In practice built-in target specs use this for arbitrary order-independent options,
@@ -1901,10 +1905,10 @@ fn add_order_independent_options(
19011905
if flavor == LinkerFlavor::PtxLinker {
19021906
// Provide the linker with fallback to internal `target-cpu`.
19031907
cmd.arg("--fallback-arch");
1904-
cmd.arg(&codegen_results.crate_info.linker_info.target_cpu);
1908+
cmd.arg(&codegen_results.crate_info.target_cpu);
19051909
} else if flavor == LinkerFlavor::BpfLinker {
19061910
cmd.arg("--cpu");
1907-
cmd.arg(&codegen_results.crate_info.linker_info.target_cpu);
1911+
cmd.arg(&codegen_results.crate_info.target_cpu);
19081912
cmd.arg("--cpu-features");
19091913
cmd.arg(match &sess.opts.cg.target_feature {
19101914
feat if !feat.is_empty() => feat,

compiler/rustc_codegen_ssa/src/back/linker.rs

+29-77
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::io::{self, BufWriter};
1010
use std::path::{Path, PathBuf};
1111
use std::{env, mem, str};
1212

13-
use rustc_data_structures::fx::FxHashMap;
1413
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
1514
use rustc_middle::middle::dependency_format::Linkage;
1615
use rustc_middle::ty::TyCtxt;
@@ -36,28 +35,6 @@ pub fn disable_localization(linker: &mut Command) {
3635
linker.env("VSLANG", "1033");
3736
}
3837

39-
/// For all the linkers we support, and information they might
40-
/// need out of the shared crate context before we get rid of it.
41-
#[derive(Debug, Encodable, Decodable)]
42-
pub struct LinkerInfo {
43-
pub(super) target_cpu: String,
44-
exports: FxHashMap<CrateType, Vec<String>>,
45-
}
46-
47-
impl LinkerInfo {
48-
pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> LinkerInfo {
49-
LinkerInfo {
50-
target_cpu,
51-
exports: tcx
52-
.sess
53-
.crate_types()
54-
.iter()
55-
.map(|&c| (c, exported_symbols(tcx, c)))
56-
.collect(),
57-
}
58-
}
59-
}
60-
6138
// The third parameter is for env vars, used on windows to set up the
6239
// path for MSVC to find its DLLs, and gcc to find its bundled
6340
// toolchain
@@ -66,7 +43,7 @@ pub fn get_linker<'a>(
6643
linker: &Path,
6744
flavor: LinkerFlavor,
6845
self_contained: bool,
69-
info: &'a LinkerInfo,
46+
target_cpu: &'a str,
7047
) -> Box<dyn Linker + 'a> {
7148
let msvc_tool = windows_registry::find_tool(&sess.opts.target_triple.triple(), "link.exe");
7249

@@ -153,40 +130,26 @@ pub fn get_linker<'a>(
153130

154131
match flavor {
155132
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => {
156-
Box::new(MsvcLinker { cmd, sess, exports: &info.exports }) as Box<dyn Linker>
133+
Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>
157134
}
158-
LinkerFlavor::Em => {
159-
Box::new(EmLinker { cmd, sess, exports: &info.exports }) as Box<dyn Linker>
135+
LinkerFlavor::Em => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
136+
LinkerFlavor::Gcc => {
137+
Box::new(GccLinker { cmd, sess, target_cpu, hinted_static: false, is_ld: false })
138+
as Box<dyn Linker>
160139
}
161-
LinkerFlavor::Gcc => Box::new(GccLinker {
162-
cmd,
163-
sess,
164-
exports: &info.exports,
165-
target_cpu: &info.target_cpu,
166-
hinted_static: false,
167-
is_ld: false,
168-
}) as Box<dyn Linker>,
169140

170141
LinkerFlavor::Lld(LldFlavor::Ld)
171142
| LinkerFlavor::Lld(LldFlavor::Ld64)
172-
| LinkerFlavor::Ld => Box::new(GccLinker {
173-
cmd,
174-
sess,
175-
exports: &info.exports,
176-
target_cpu: &info.target_cpu,
177-
hinted_static: false,
178-
is_ld: true,
179-
}) as Box<dyn Linker>,
180-
181-
LinkerFlavor::Lld(LldFlavor::Wasm) => {
182-
Box::new(WasmLd::new(cmd, sess, &info.exports)) as Box<dyn Linker>
143+
| LinkerFlavor::Ld => {
144+
Box::new(GccLinker { cmd, sess, target_cpu, hinted_static: false, is_ld: true })
145+
as Box<dyn Linker>
183146
}
184147

148+
LinkerFlavor::Lld(LldFlavor::Wasm) => Box::new(WasmLd::new(cmd, sess)) as Box<dyn Linker>,
149+
185150
LinkerFlavor::PtxLinker => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
186151

187-
LinkerFlavor::BpfLinker => {
188-
Box::new(BpfLinker { cmd, sess, exports: &info.exports }) as Box<dyn Linker>
189-
}
152+
LinkerFlavor::BpfLinker => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
190153
}
191154
}
192155

@@ -222,7 +185,7 @@ pub trait Linker {
222185
fn debuginfo(&mut self, strip: Strip);
223186
fn no_crt_objects(&mut self);
224187
fn no_default_libraries(&mut self);
225-
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
188+
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]);
226189
fn subsystem(&mut self, subsystem: &str);
227190
fn group_start(&mut self);
228191
fn group_end(&mut self);
@@ -250,7 +213,6 @@ impl dyn Linker + '_ {
250213
pub struct GccLinker<'a> {
251214
cmd: Command,
252215
sess: &'a Session,
253-
exports: &'a FxHashMap<CrateType, Vec<String>>,
254216
target_cpu: &'a str,
255217
hinted_static: bool, // Keeps track of the current hinting mode.
256218
// Link as ld
@@ -655,7 +617,7 @@ impl<'a> Linker for GccLinker<'a> {
655617
}
656618
}
657619

658-
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
620+
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
659621
// Symbol visibility in object files typically takes care of this.
660622
if crate_type == CrateType::Executable && self.sess.target.override_export_symbols.is_none()
661623
{
@@ -684,7 +646,7 @@ impl<'a> Linker for GccLinker<'a> {
684646
// Write a plain, newline-separated list of symbols
685647
let res: io::Result<()> = try {
686648
let mut f = BufWriter::new(File::create(&path)?);
687-
for sym in self.exports[&crate_type].iter() {
649+
for sym in symbols {
688650
debug!(" _{}", sym);
689651
writeln!(f, "_{}", sym)?;
690652
}
@@ -699,7 +661,7 @@ impl<'a> Linker for GccLinker<'a> {
699661
// .def file similar to MSVC one but without LIBRARY section
700662
// because LD doesn't like when it's empty
701663
writeln!(f, "EXPORTS")?;
702-
for symbol in self.exports[&crate_type].iter() {
664+
for symbol in symbols {
703665
debug!(" _{}", symbol);
704666
writeln!(f, " {}", symbol)?;
705667
}
@@ -712,9 +674,9 @@ impl<'a> Linker for GccLinker<'a> {
712674
let res: io::Result<()> = try {
713675
let mut f = BufWriter::new(File::create(&path)?);
714676
writeln!(f, "{{")?;
715-
if !self.exports[&crate_type].is_empty() {
677+
if !symbols.is_empty() {
716678
writeln!(f, " global:")?;
717-
for sym in self.exports[&crate_type].iter() {
679+
for sym in symbols {
718680
debug!(" {};", sym);
719681
writeln!(f, " {};", sym)?;
720682
}
@@ -814,7 +776,6 @@ impl<'a> Linker for GccLinker<'a> {
814776
pub struct MsvcLinker<'a> {
815777
cmd: Command,
816778
sess: &'a Session,
817-
exports: &'a FxHashMap<CrateType, Vec<String>>,
818779
}
819780

820781
impl<'a> Linker for MsvcLinker<'a> {
@@ -988,7 +949,7 @@ impl<'a> Linker for MsvcLinker<'a> {
988949
// crates. Upstream rlibs may be linked statically to this dynamic library,
989950
// in which case they may continue to transitively be used and hence need
990951
// their symbols exported.
991-
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
952+
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
992953
// Symbol visibility takes care of this typically
993954
if crate_type == CrateType::Executable {
994955
return;
@@ -1002,7 +963,7 @@ impl<'a> Linker for MsvcLinker<'a> {
1002963
// straight to exports.
1003964
writeln!(f, "LIBRARY")?;
1004965
writeln!(f, "EXPORTS")?;
1005-
for symbol in self.exports[&crate_type].iter() {
966+
for symbol in symbols {
1006967
debug!(" _{}", symbol);
1007968
writeln!(f, " {}", symbol)?;
1008969
}
@@ -1055,7 +1016,6 @@ impl<'a> Linker for MsvcLinker<'a> {
10551016
pub struct EmLinker<'a> {
10561017
cmd: Command,
10571018
sess: &'a Session,
1058-
exports: &'a FxHashMap<CrateType, Vec<String>>,
10591019
}
10601020

10611021
impl<'a> Linker for EmLinker<'a> {
@@ -1167,9 +1127,7 @@ impl<'a> Linker for EmLinker<'a> {
11671127
self.cmd.args(&["-s", "DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]"]);
11681128
}
11691129

1170-
fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
1171-
let symbols = &self.exports[&crate_type];
1172-
1130+
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
11731131
debug!("EXPORTED SYMBOLS:");
11741132

11751133
self.cmd.arg("-s");
@@ -1211,15 +1169,10 @@ impl<'a> Linker for EmLinker<'a> {
12111169
pub struct WasmLd<'a> {
12121170
cmd: Command,
12131171
sess: &'a Session,
1214-
exports: &'a FxHashMap<CrateType, Vec<String>>,
12151172
}
12161173

12171174
impl<'a> WasmLd<'a> {
1218-
fn new(
1219-
mut cmd: Command,
1220-
sess: &'a Session,
1221-
exports: &'a FxHashMap<CrateType, Vec<String>>,
1222-
) -> WasmLd<'a> {
1175+
fn new(mut cmd: Command, sess: &'a Session) -> WasmLd<'a> {
12231176
// If the atomics feature is enabled for wasm then we need a whole bunch
12241177
// of flags:
12251178
//
@@ -1252,7 +1205,7 @@ impl<'a> WasmLd<'a> {
12521205
cmd.arg("--export=__tls_align");
12531206
cmd.arg("--export=__tls_base");
12541207
}
1255-
WasmLd { cmd, sess, exports }
1208+
WasmLd { cmd, sess }
12561209
}
12571210
}
12581211

@@ -1368,8 +1321,8 @@ impl<'a> Linker for WasmLd<'a> {
13681321

13691322
fn no_default_libraries(&mut self) {}
13701323

1371-
fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
1372-
for sym in self.exports[&crate_type].iter() {
1324+
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
1325+
for sym in symbols {
13731326
self.cmd.arg("--export").arg(&sym);
13741327
}
13751328

@@ -1392,7 +1345,7 @@ impl<'a> Linker for WasmLd<'a> {
13921345
}
13931346
}
13941347

1395-
fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
1348+
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
13961349
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
13971350
return exports.clone();
13981351
}
@@ -1521,7 +1474,7 @@ impl<'a> Linker for PtxLinker<'a> {
15211474

15221475
fn control_flow_guard(&mut self) {}
15231476

1524-
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType) {}
1477+
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, _symbols: &[String]) {}
15251478

15261479
fn subsystem(&mut self, _subsystem: &str) {}
15271480

@@ -1535,7 +1488,6 @@ impl<'a> Linker for PtxLinker<'a> {
15351488
pub struct BpfLinker<'a> {
15361489
cmd: Command,
15371490
sess: &'a Session,
1538-
exports: &'a FxHashMap<CrateType, Vec<String>>,
15391491
}
15401492

15411493
impl<'a> Linker for BpfLinker<'a> {
@@ -1622,11 +1574,11 @@ impl<'a> Linker for BpfLinker<'a> {
16221574

16231575
fn control_flow_guard(&mut self) {}
16241576

1625-
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
1577+
fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
16261578
let path = tmpdir.join("symbols");
16271579
let res: io::Result<()> = try {
16281580
let mut f = BufWriter::new(File::create(&path)?);
1629-
for sym in self.exports[&crate_type].iter() {
1581+
for sym in symbols {
16301582
writeln!(f, "{}", sym)?;
16311583
}
16321584
};

compiler/rustc_codegen_ssa/src/base.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::back::linker::LinkerInfo;
21
use crate::back::write::{
32
compute_per_cgu_lto_type, start_async_codegen, submit_codegened_module_to_llvm,
43
submit_post_lto_module_to_llvm, submit_pre_lto_module_to_llvm, ComputedLtoType, OngoingCodegen,
@@ -756,7 +755,12 @@ impl<B: ExtraBackendMethods> Drop for AbortCodegenOnDrop<B> {
756755

757756
impl CrateInfo {
758757
pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> CrateInfo {
759-
let linker_info = LinkerInfo::new(tcx, target_cpu);
758+
let exported_symbols = tcx
759+
.sess
760+
.crate_types()
761+
.iter()
762+
.map(|&c| (c, crate::back::linker::exported_symbols(tcx, c)))
763+
.collect();
760764
let local_crate_name = tcx.crate_name(LOCAL_CRATE);
761765
let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
762766
let subsystem = tcx.sess.first_attr_value_str_by_name(crate_attrs, sym::windows_subsystem);
@@ -772,7 +776,8 @@ impl CrateInfo {
772776
});
773777

774778
let mut info = CrateInfo {
775-
linker_info,
779+
target_cpu,
780+
exported_symbols,
776781
local_crate_name,
777782
compiler_builtins: None,
778783
profiler_runtime: None,

compiler/rustc_codegen_ssa/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_middle::dep_graph::WorkProduct;
2727
use rustc_middle::middle::cstore::{self, CrateSource};
2828
use rustc_middle::middle::dependency_format::Dependencies;
2929
use rustc_middle::ty::query::Providers;
30-
use rustc_session::config::{OutputFilenames, OutputType, RUST_CGU_EXT};
30+
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
3131
use rustc_session::utils::NativeLibKind;
3232
use rustc_span::symbol::Symbol;
3333
use std::path::{Path, PathBuf};
@@ -135,7 +135,8 @@ impl From<&cstore::NativeLib> for NativeLib {
135135
/// and the corresponding properties without referencing information outside of a `CrateInfo`.
136136
#[derive(Debug, Encodable, Decodable)]
137137
pub struct CrateInfo {
138-
pub linker_info: back::linker::LinkerInfo,
138+
pub target_cpu: String,
139+
pub exported_symbols: FxHashMap<CrateType, Vec<String>>,
139140
pub local_crate_name: Symbol,
140141
pub compiler_builtins: Option<CrateNum>,
141142
pub profiler_runtime: Option<CrateNum>,

0 commit comments

Comments
 (0)