Skip to content

Commit fefc69a

Browse files
committed
Synthesis object file for #[used] and exported symbols
1 parent 08b7029 commit fefc69a

File tree

7 files changed

+182
-8
lines changed

7 files changed

+182
-8
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+69
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::{ErrorGuaranteed, Handler};
77
use rustc_fs_util::fix_windows_verbatim_for_gcc;
88
use rustc_hir::def_id::CrateNum;
99
use rustc_middle::middle::dependency_format::Linkage;
10+
use rustc_middle::middle::exported_symbols::SymbolExportKind;
1011
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip};
1112
use rustc_session::config::{OutputFilenames, OutputType, PrintRequest, SplitDwarfKind};
1213
use rustc_session::cstore::DllImport;
@@ -1654,6 +1655,67 @@ fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor
16541655
}
16551656
}
16561657

1658+
/// Add a synthetic object file that contains reference to all symbols that we want to expose to
1659+
/// the linker.
1660+
///
1661+
/// Background: we implement rlibs as static library (archives). Linkers treat archives
1662+
/// differently from object files: all object files participate in linking, while archives will
1663+
/// only participate in linking if they can satisfy at least one undefined reference (version
1664+
/// scripts doesn't count). This causes `#[no_mangle]` or `#[used]` items to be ignored by the
1665+
/// linker, and since they never participate in the linking, using `KEEP` in the linker scripts
1666+
/// can't keep them either. This causes #47384.
1667+
///
1668+
/// To keep them around, we could use `--whole-archive` and equivalents to force rlib to
1669+
/// participate in linking like object files, but this proves to be expensive (#93791). Therefore
1670+
/// we instead just introduce an undefined reference to them. This could be done by `-u` command
1671+
/// line option to the linker or `EXTERN(...)` in linker scripts, however they does not only
1672+
/// introduce an undefined reference, but also make them the GC roots, preventing `--gc-sections`
1673+
/// from removing them, and this is especially problematic for embedded programming where every
1674+
/// byte counts.
1675+
///
1676+
/// This method creates a synthetic object file, which contains undefined references to all symbols
1677+
/// that are necessary for the linking. They are only present in symbol table but not actually
1678+
/// used in any sections, so the linker will therefore pick relevant rlibs for linking, but
1679+
/// unused `#[no_mangle]` or `#[used]` can still be discard by GC sections.
1680+
fn add_linked_symbol_object(
1681+
cmd: &mut dyn Linker,
1682+
sess: &Session,
1683+
tmpdir: &Path,
1684+
symbols: &[(String, SymbolExportKind)],
1685+
) {
1686+
if symbols.is_empty() {
1687+
return;
1688+
}
1689+
1690+
let Some(mut file) = super::metadata::create_object_file(sess) else {
1691+
return;
1692+
};
1693+
1694+
for (sym, kind) in symbols.iter() {
1695+
file.add_symbol(object::write::Symbol {
1696+
name: sym.clone().into(),
1697+
value: 0,
1698+
size: 0,
1699+
kind: match kind {
1700+
SymbolExportKind::Text => object::SymbolKind::Text,
1701+
SymbolExportKind::Data => object::SymbolKind::Data,
1702+
SymbolExportKind::Tls => object::SymbolKind::Tls,
1703+
},
1704+
scope: object::SymbolScope::Unknown,
1705+
weak: false,
1706+
section: object::write::SymbolSection::Undefined,
1707+
flags: object::SymbolFlags::None,
1708+
});
1709+
}
1710+
1711+
let path = tmpdir.join("symbols.o");
1712+
let result = std::fs::write(&path, file.write().unwrap());
1713+
if let Err(e) = result {
1714+
sess.fatal(&format!("failed to write {}: {}", path.display(), e));
1715+
}
1716+
cmd.add_object(&path);
1717+
}
1718+
16571719
/// Add object files containing code from the current crate.
16581720
fn add_local_crate_regular_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) {
16591721
for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) {
@@ -1797,6 +1859,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
17971859
// Sanitizer libraries.
17981860
add_sanitizer_libraries(sess, crate_type, cmd);
17991861

1862+
add_linked_symbol_object(
1863+
cmd,
1864+
sess,
1865+
tmpdir,
1866+
&codegen_results.crate_info.linked_symbols[&crate_type],
1867+
);
1868+
18001869
// Object code from the current crate.
18011870
// Take careful note of the ordering of the arguments we pass to the linker
18021871
// here. Linkers will assume that things on the left depend on things to the

compiler/rustc_codegen_ssa/src/back/linker.rs

+46
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::{env, mem, str};
1212

1313
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
1414
use rustc_middle::middle::dependency_format::Linkage;
15+
use rustc_middle::middle::exported_symbols::SymbolExportKind;
1516
use rustc_middle::ty::TyCtxt;
1617
use rustc_serialize::{json, Encoder};
1718
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
@@ -1557,6 +1558,51 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St
15571558
symbols
15581559
}
15591560

1561+
pub(crate) fn linked_symbols(
1562+
tcx: TyCtxt<'_>,
1563+
crate_type: CrateType,
1564+
) -> Vec<(String, SymbolExportKind)> {
1565+
match crate_type {
1566+
CrateType::Executable | CrateType::Cdylib => (),
1567+
CrateType::Staticlib | CrateType::ProcMacro | CrateType::Rlib | CrateType::Dylib => {
1568+
return Vec::new();
1569+
}
1570+
}
1571+
1572+
let mut symbols = Vec::new();
1573+
1574+
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1575+
for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() {
1576+
if info.level.is_below_threshold(export_threshold) || info.used {
1577+
symbols.push((
1578+
symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, LOCAL_CRATE),
1579+
info.kind,
1580+
));
1581+
}
1582+
}
1583+
1584+
let formats = tcx.dependency_formats(());
1585+
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();
1586+
1587+
for (index, dep_format) in deps.iter().enumerate() {
1588+
let cnum = CrateNum::new(index + 1);
1589+
// For each dependency that we are linking to statically ...
1590+
if *dep_format == Linkage::Static {
1591+
// ... we add its symbol list to our export list.
1592+
for &(symbol, info) in tcx.exported_symbols(cnum).iter() {
1593+
if info.level.is_below_threshold(export_threshold) || info.used {
1594+
symbols.push((
1595+
symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum),
1596+
info.kind,
1597+
));
1598+
}
1599+
}
1600+
}
1601+
}
1602+
1603+
symbols
1604+
}
1605+
15601606
/// Much simplified and explicit CLI for the NVPTX linker. The linker operates
15611607
/// with bitcode and uses LLVM backend to generate a PTX assembly.
15621608
pub struct PtxLinker<'a> {

compiler/rustc_codegen_ssa/src/back/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn search_for_metadata<'a>(
9494
.map_err(|e| format!("failed to read {} section in '{}': {}", section, path.display(), e))
9595
}
9696

97-
fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
97+
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
9898
let endianness = match sess.target.options.endian {
9999
Endian::Little => Endianness::Little,
100100
Endian::Big => Endianness::Big,

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+47-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::Node;
99
use rustc_index::vec::IndexVec;
1010
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1111
use rustc_middle::middle::exported_symbols::{
12-
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportLevel,
12+
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
1313
};
1414
use rustc_middle::ty::query::{ExternProviders, Providers};
1515
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
@@ -124,21 +124,37 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
124124
} else {
125125
symbol_export_level(tcx, def_id.to_def_id())
126126
};
127+
let codegen_attrs = tcx.codegen_fn_attrs(def_id.to_def_id());
127128
debug!(
128129
"EXPORTED SYMBOL (local): {} ({:?})",
129130
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
130131
export_level
131132
);
132133
(def_id.to_def_id(), SymbolExportInfo {
133134
level: export_level,
135+
kind: if tcx.is_static(def_id.to_def_id()) {
136+
if codegen_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
137+
SymbolExportKind::Tls
138+
} else {
139+
SymbolExportKind::Data
140+
}
141+
} else {
142+
SymbolExportKind::Text
143+
},
144+
used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED)
145+
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER),
134146
})
135147
})
136148
.collect();
137149

138150
if let Some(id) = tcx.proc_macro_decls_static(()) {
139151
reachable_non_generics.insert(
140152
id.to_def_id(),
141-
SymbolExportInfo { level: SymbolExportLevel::C },
153+
SymbolExportInfo {
154+
level: SymbolExportLevel::C,
155+
kind: SymbolExportKind::Data,
156+
used: false,
157+
},
142158
);
143159
}
144160

@@ -180,7 +196,11 @@ fn exported_symbols_provider_local<'tcx>(
180196

181197
symbols.push((
182198
exported_symbol,
183-
SymbolExportInfo { level: SymbolExportLevel::C },
199+
SymbolExportInfo {
200+
level: SymbolExportLevel::C,
201+
kind: SymbolExportKind::Text,
202+
used: false,
203+
},
184204
));
185205
}
186206

@@ -191,7 +211,11 @@ fn exported_symbols_provider_local<'tcx>(
191211

192212
symbols.push((
193213
exported_symbol,
194-
SymbolExportInfo { level: SymbolExportLevel::Rust },
214+
SymbolExportInfo {
215+
level: SymbolExportLevel::Rust,
216+
kind: SymbolExportKind::Text,
217+
used: false,
218+
},
195219
));
196220
}
197221
}
@@ -207,7 +231,11 @@ fn exported_symbols_provider_local<'tcx>(
207231
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
208232
(
209233
exported_symbol,
210-
SymbolExportInfo { level: SymbolExportLevel::C },
234+
SymbolExportInfo {
235+
level: SymbolExportLevel::C,
236+
kind: SymbolExportKind::Data,
237+
used: false,
238+
},
211239
)
212240
}));
213241
}
@@ -220,7 +248,11 @@ fn exported_symbols_provider_local<'tcx>(
220248
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
221249
(
222250
exported_symbol,
223-
SymbolExportInfo { level: SymbolExportLevel::C },
251+
SymbolExportInfo {
252+
level: SymbolExportLevel::C,
253+
kind: SymbolExportKind::Data,
254+
used: false,
255+
},
224256
)
225257
}));
226258
}
@@ -231,7 +263,11 @@ fn exported_symbols_provider_local<'tcx>(
231263

232264
symbols.push((
233265
exported_symbol,
234-
SymbolExportInfo { level: SymbolExportLevel::Rust },
266+
SymbolExportInfo {
267+
level: SymbolExportLevel::Rust,
268+
kind: SymbolExportKind::Data,
269+
used: false,
270+
},
235271
));
236272
}
237273

@@ -269,6 +305,8 @@ fn exported_symbols_provider_local<'tcx>(
269305
symbol,
270306
SymbolExportInfo {
271307
level: SymbolExportLevel::Rust,
308+
kind: SymbolExportKind::Text,
309+
used: false,
272310
},
273311
));
274312
}
@@ -283,6 +321,8 @@ fn exported_symbols_provider_local<'tcx>(
283321
ExportedSymbol::DropGlue(ty),
284322
SymbolExportInfo {
285323
level: SymbolExportLevel::Rust,
324+
kind: SymbolExportKind::Text,
325+
used: false,
286326
},
287327
));
288328
}

compiler/rustc_codegen_ssa/src/base.rs

+7
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,12 @@ impl CrateInfo {
801801
.iter()
802802
.map(|&c| (c, crate::back::linker::exported_symbols(tcx, c)))
803803
.collect();
804+
let linked_symbols = tcx
805+
.sess
806+
.crate_types()
807+
.iter()
808+
.map(|&c| (c, crate::back::linker::linked_symbols(tcx, c)))
809+
.collect();
804810
let local_crate_name = tcx.crate_name(LOCAL_CRATE);
805811
let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
806812
let subsystem = tcx.sess.first_attr_value_str_by_name(crate_attrs, sym::windows_subsystem);
@@ -834,6 +840,7 @@ impl CrateInfo {
834840
let mut info = CrateInfo {
835841
target_cpu,
836842
exported_symbols,
843+
linked_symbols,
837844
local_crate_name,
838845
compiler_builtins: None,
839846
profiler_runtime: None,

compiler/rustc_codegen_ssa/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_hir::def_id::CrateNum;
2828
use rustc_hir::LangItem;
2929
use rustc_middle::dep_graph::WorkProduct;
3030
use rustc_middle::middle::dependency_format::Dependencies;
31+
use rustc_middle::middle::exported_symbols::SymbolExportKind;
3132
use rustc_middle::ty::query::{ExternProviders, Providers};
3233
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
3334
use rustc_session::cstore::{self, CrateSource};
@@ -140,6 +141,7 @@ impl From<&cstore::NativeLib> for NativeLib {
140141
pub struct CrateInfo {
141142
pub target_cpu: String,
142143
pub exported_symbols: FxHashMap<CrateType, Vec<String>>,
144+
pub linked_symbols: FxHashMap<CrateType, Vec<(String, SymbolExportKind)>>,
143145
pub local_crate_name: Symbol,
144146
pub compiler_builtins: Option<CrateNum>,
145147
pub profiler_runtime: Option<CrateNum>,

compiler/rustc_middle/src/middle/exported_symbols.rs

+10
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ impl SymbolExportLevel {
2121
}
2222
}
2323

24+
/// Kind of exported symbols.
25+
#[derive(Eq, PartialEq, Debug, Copy, Clone, Encodable, Decodable, HashStable)]
26+
pub enum SymbolExportKind {
27+
Text,
28+
Data,
29+
Tls,
30+
}
31+
2432
/// The `SymbolExportInfo` of a symbols specifies symbol-related information
2533
/// that is relevant to code generation and linking.
2634
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
2735
pub struct SymbolExportInfo {
2836
pub level: SymbolExportLevel,
37+
pub kind: SymbolExportKind,
38+
pub used: bool,
2939
}
3040

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

0 commit comments

Comments
 (0)