From a867b8dd1cb48146d24883a26c5fd961c160d84e Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Wed, 9 Feb 2022 00:02:51 +0000 Subject: [PATCH 1/8] Add a test for issue 47384 --- src/test/run-make/issue-47384/Makefile | 12 ++++++++++++ src/test/run-make/issue-47384/lib.rs | 12 ++++++++++++ src/test/run-make/issue-47384/linker.ld | 7 +++++++ src/test/run-make/issue-47384/main.rs | 1 + 4 files changed, 32 insertions(+) create mode 100644 src/test/run-make/issue-47384/Makefile create mode 100644 src/test/run-make/issue-47384/lib.rs create mode 100644 src/test/run-make/issue-47384/linker.ld create mode 100644 src/test/run-make/issue-47384/main.rs diff --git a/src/test/run-make/issue-47384/Makefile b/src/test/run-make/issue-47384/Makefile new file mode 100644 index 0000000000000..d3d48966b8547 --- /dev/null +++ b/src/test/run-make/issue-47384/Makefile @@ -0,0 +1,12 @@ +-include ../../run-make-fulldeps/tools.mk + +# ignore-windows +# ignore-cross-compile + +all: main.rs + $(RUSTC) --crate-type lib lib.rs + $(RUSTC) --crate-type cdylib -Clink-args="-Tlinker.ld" main.rs + # Ensure `#[used]` and `KEEP`-ed section is there + objdump -s -j".static" $(TMPDIR)/libmain.so + # Ensure `#[no_mangle]` symbol is there + nm $(TMPDIR)/libmain.so | $(CGREP) bar diff --git a/src/test/run-make/issue-47384/lib.rs b/src/test/run-make/issue-47384/lib.rs new file mode 100644 index 0000000000000..99508bcdaf314 --- /dev/null +++ b/src/test/run-make/issue-47384/lib.rs @@ -0,0 +1,12 @@ +mod foo { + #[link_section = ".rodata.STATIC"] + #[used] + static STATIC: [u32; 10] = [1; 10]; +} + +mod bar { + #[no_mangle] + extern "C" fn bar() -> i32 { + 0 + } +} diff --git a/src/test/run-make/issue-47384/linker.ld b/src/test/run-make/issue-47384/linker.ld new file mode 100644 index 0000000000000..2e70acab3f496 --- /dev/null +++ b/src/test/run-make/issue-47384/linker.ld @@ -0,0 +1,7 @@ +SECTIONS +{ + .static : ALIGN(4) + { + KEEP(*(.rodata.STATIC)); + } +} diff --git a/src/test/run-make/issue-47384/main.rs b/src/test/run-make/issue-47384/main.rs new file mode 100644 index 0000000000000..02572632517ec --- /dev/null +++ b/src/test/run-make/issue-47384/main.rs @@ -0,0 +1 @@ +extern crate lib; From aa8413cd26f1eab00d781893c8e426dbc564eaf1 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 2 Apr 2022 22:27:33 +0100 Subject: [PATCH 2/8] Add `SymbolExportInfo` This is currently a wrapper to `SymbolExportLevel` but it allows later addition of extra information. --- compiler/rustc_codegen_llvm/src/back/lto.rs | 6 +- compiler/rustc_codegen_ssa/src/back/linker.rs | 8 +-- .../src/back/symbol_export.rs | 60 ++++++++++++++----- compiler/rustc_codegen_ssa/src/back/write.rs | 4 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 4 +- .../src/rmeta/decoder/cstore_impl.rs | 4 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 6 +- compiler/rustc_metadata/src/rmeta/mod.rs | 4 +- .../src/middle/exported_symbols.rs | 7 +++ compiler/rustc_middle/src/query/mod.rs | 4 +- compiler/rustc_middle/src/ty/query.rs | 2 +- .../src/partitioning/default.rs | 4 +- 12 files changed, 75 insertions(+), 38 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 0f5b1c08ec2dc..6bc242b46e043 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -16,7 +16,7 @@ use rustc_errors::{FatalError, Handler}; use rustc_hir::def_id::LOCAL_CRATE; use rustc_middle::bug; use rustc_middle::dep_graph::WorkProduct; -use rustc_middle::middle::exported_symbols::SymbolExportLevel; +use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel}; use rustc_session::cgu_reuse_tracker::CguReuse; use rustc_session::config::{self, CrateType, Lto}; use tracing::{debug, info}; @@ -55,8 +55,8 @@ fn prepare_lto( Lto::No => panic!("didn't request LTO but we're doing LTO"), }; - let symbol_filter = &|&(ref name, level): &(String, SymbolExportLevel)| { - if level.is_below_threshold(export_threshold) { + let symbol_filter = &|&(ref name, info): &(String, SymbolExportInfo)| { + if info.level.is_below_threshold(export_threshold) { Some(CString::new(name.as_str()).unwrap()) } else { None diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 3a66bfafaf3f4..a90b8a62c623a 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -1526,8 +1526,8 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec, crate_type: CrateType) -> Vec SymbolExportLevel { } } -fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap { +fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap { assert_eq!(cnum, LOCAL_CRATE); if !tcx.sess.opts.output_types.should_codegen() { @@ -129,12 +129,17 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap< tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())), export_level ); - (def_id.to_def_id(), export_level) + (def_id.to_def_id(), SymbolExportInfo { + level: export_level, + }) }) .collect(); if let Some(id) = tcx.proc_macro_decls_static(()) { - reachable_non_generics.insert(id.to_def_id(), SymbolExportLevel::C); + reachable_non_generics.insert( + id.to_def_id(), + SymbolExportInfo { level: SymbolExportLevel::C }, + ); } reachable_non_generics @@ -143,8 +148,8 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap< fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: DefId) -> bool { let export_threshold = threshold(tcx); - if let Some(&level) = tcx.reachable_non_generics(def_id.krate).get(&def_id) { - level.is_below_threshold(export_threshold) + if let Some(&info) = tcx.reachable_non_generics(def_id.krate).get(&def_id) { + info.level.is_below_threshold(export_threshold) } else { false } @@ -157,7 +162,7 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b fn exported_symbols_provider_local<'tcx>( tcx: TyCtxt<'tcx>, cnum: CrateNum, -) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] { +) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] { assert_eq!(cnum, LOCAL_CRATE); if !tcx.sess.opts.output_types.should_codegen() { @@ -167,13 +172,16 @@ fn exported_symbols_provider_local<'tcx>( let mut symbols: Vec<_> = tcx .reachable_non_generics(LOCAL_CRATE) .iter() - .map(|(&def_id, &level)| (ExportedSymbol::NonGeneric(def_id), level)) + .map(|(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info)) .collect(); if tcx.entry_fn(()).is_some() { let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, "main")); - symbols.push((exported_symbol, SymbolExportLevel::C)); + symbols.push(( + exported_symbol, + SymbolExportInfo { level: SymbolExportLevel::C }, + )); } if tcx.allocator_kind(()).is_some() { @@ -181,7 +189,10 @@ fn exported_symbols_provider_local<'tcx>( let symbol_name = format!("__rust_{}", method.name); let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name)); - symbols.push((exported_symbol, SymbolExportLevel::Rust)); + symbols.push(( + exported_symbol, + SymbolExportInfo { level: SymbolExportLevel::Rust }, + )); } } @@ -194,7 +205,10 @@ fn exported_symbols_provider_local<'tcx>( symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| { let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym)); - (exported_symbol, SymbolExportLevel::C) + ( + exported_symbol, + SymbolExportInfo { level: SymbolExportLevel::C }, + ) })); } @@ -204,7 +218,10 @@ fn exported_symbols_provider_local<'tcx>( symbols.extend(MSAN_WEAK_SYMBOLS.iter().map(|sym| { let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym)); - (exported_symbol, SymbolExportLevel::C) + ( + exported_symbol, + SymbolExportInfo { level: SymbolExportLevel::C }, + ) })); } @@ -212,7 +229,10 @@ fn exported_symbols_provider_local<'tcx>( let symbol_name = metadata_symbol_name(tcx); let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name)); - symbols.push((exported_symbol, SymbolExportLevel::Rust)); + symbols.push(( + exported_symbol, + SymbolExportInfo { level: SymbolExportLevel::Rust }, + )); } if tcx.sess.opts.share_generics() && tcx.local_crate_exports_generics() { @@ -245,7 +265,12 @@ fn exported_symbols_provider_local<'tcx>( MonoItem::Fn(Instance { def: InstanceDef::Item(def), substs }) => { if substs.non_erasable_generics().next().is_some() { let symbol = ExportedSymbol::Generic(def.did, substs); - symbols.push((symbol, SymbolExportLevel::Rust)); + symbols.push(( + symbol, + SymbolExportInfo { + level: SymbolExportLevel::Rust, + }, + )); } } MonoItem::Fn(Instance { def: InstanceDef::DropGlue(_, Some(ty)), substs }) => { @@ -254,7 +279,12 @@ fn exported_symbols_provider_local<'tcx>( substs.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)) ); - symbols.push((ExportedSymbol::DropGlue(ty), SymbolExportLevel::Rust)); + symbols.push(( + ExportedSymbol::DropGlue(ty), + SymbolExportInfo { + level: SymbolExportLevel::Rust, + }, + )); } _ => { // Any other symbols don't qualify for sharing diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index d4eaf6389dfe9..7a6e9bfd2b7c6 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -23,7 +23,7 @@ use rustc_incremental::{ }; use rustc_metadata::EncodedMetadata; use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; -use rustc_middle::middle::exported_symbols::SymbolExportLevel; +use rustc_middle::middle::exported_symbols::SymbolExportInfo; use rustc_middle::ty::TyCtxt; use rustc_session::cgu_reuse_tracker::CguReuseTracker; use rustc_session::config::{self, CrateType, Lto, OutputFilenames, OutputType}; @@ -304,7 +304,7 @@ pub type TargetMachineFactoryFn = Arc< + Sync, >; -pub type ExportedSymbols = FxHashMap>>; +pub type ExportedSymbols = FxHashMap>>; /// Additional resources used by optimize_and_codegen (not module specific) #[derive(Clone)] diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 046322a42d85b..d7a4547bcc606 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -22,7 +22,7 @@ use rustc_hir::lang_items; use rustc_index::vec::{Idx, IndexVec}; use rustc_middle::arena::ArenaAllocatable; use rustc_middle::metadata::ModChild; -use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; +use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; use rustc_middle::middle::stability::DeprecationEntry; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::thir; @@ -1405,7 +1405,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { fn exported_symbols( self, tcx: TyCtxt<'tcx>, - ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] { + ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] { tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx))) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index cd3a1d72d41d2..1a7c458b6a267 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -188,9 +188,9 @@ provide! { <'tcx> tcx, def_id, other, cdata, let reachable_non_generics = tcx .exported_symbols(cdata.cnum) .iter() - .filter_map(|&(exported_symbol, export_level)| { + .filter_map(|&(exported_symbol, export_info)| { if let ExportedSymbol::NonGeneric(def_id) = exported_symbol { - Some((def_id, export_level)) + Some((def_id, export_info)) } else { None } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 6c758b8e5b633..9c94dca9ecde2 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -21,7 +21,7 @@ use rustc_index::vec::Idx; use rustc_middle::hir::nested_filter; use rustc_middle::middle::dependency_format::Linkage; use rustc_middle::middle::exported_symbols::{ - metadata_symbol_name, ExportedSymbol, SymbolExportLevel, + metadata_symbol_name, ExportedSymbol, SymbolExportInfo, }; use rustc_middle::mir::interpret; use rustc_middle::thir; @@ -1844,8 +1844,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // definition (as that's not defined in this crate). fn encode_exported_symbols( &mut self, - exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportLevel)], - ) -> Lazy<[(ExportedSymbol<'tcx>, SymbolExportLevel)]> { + exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)], + ) -> Lazy<[(ExportedSymbol<'tcx>, SymbolExportInfo)]> { empty_proc_macro!(self); // The metadata symbol name is special. It should not show up in // downstream crates. diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 15e8693d71282..c0b7a787b80e0 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -13,7 +13,7 @@ use rustc_hir::definitions::DefKey; use rustc_hir::lang_items; use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec}; use rustc_middle::metadata::ModChild; -use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; +use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; use rustc_middle::mir; use rustc_middle::thir; use rustc_middle::ty::fast_reject::SimplifiedType; @@ -218,7 +218,7 @@ crate struct CrateRoot<'tcx> { tables: LazyTables<'tcx>, - exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]), + exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportInfo)]), syntax_contexts: SyntaxContextTable, expn_data: ExpnDataTable, diff --git a/compiler/rustc_middle/src/middle/exported_symbols.rs b/compiler/rustc_middle/src/middle/exported_symbols.rs index 5ea78e087f845..a605e6dfdc1d3 100644 --- a/compiler/rustc_middle/src/middle/exported_symbols.rs +++ b/compiler/rustc_middle/src/middle/exported_symbols.rs @@ -21,6 +21,13 @@ impl SymbolExportLevel { } } +/// The `SymbolExportInfo` of a symbols specifies symbol-related information +/// that is relevant to code generation and linking. +#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] +pub struct SymbolExportInfo { + pub level: SymbolExportLevel, +} + #[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] pub enum ExportedSymbol<'tcx> { NonGeneric(DefId), diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 89761bf4e27a0..97f1d96404b5e 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1355,7 +1355,7 @@ rustc_queries! { // Does not include external symbols that don't have a corresponding DefId, // like the compiler-generated `main` function and so on. query reachable_non_generics(_: CrateNum) - -> DefIdMap { + -> DefIdMap { storage(ArenaCacheSelector<'tcx>) desc { "looking up the exported symbols of a crate" } separate_provide_extern @@ -1672,7 +1672,7 @@ rustc_queries! { /// correspond to a publicly visible symbol in `cnum` machine code. /// - The `exported_symbols` sets of different crates do not intersect. query exported_symbols(_: CrateNum) - -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] { + -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] { desc { "exported_symbols" } separate_provide_extern } diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 9e48c569c253a..6ff061a820ebf 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -3,7 +3,7 @@ use crate::infer::canonical::{self, Canonical}; use crate::lint::LintLevelMap; use crate::metadata::ModChild; use crate::middle::codegen_fn_attrs::CodegenFnAttrs; -use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; +use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; use crate::middle::lib_features::LibFeatures; use crate::middle::privacy::AccessLevels; use crate::middle::region; diff --git a/compiler/rustc_monomorphize/src/partitioning/default.rs b/compiler/rustc_monomorphize/src/partitioning/default.rs index c4ffb19f87a91..02f3efa695c34 100644 --- a/compiler/rustc_monomorphize/src/partitioning/default.rs +++ b/compiler/rustc_monomorphize/src/partitioning/default.rs @@ -5,7 +5,7 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::DefPathDataName; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; -use rustc_middle::middle::exported_symbols::SymbolExportLevel; +use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel}; use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, Linkage, Visibility}; use rustc_middle::mir::mono::{InstantiationMode, MonoItem}; use rustc_middle::ty::print::characteristic_def_id_of_type; @@ -554,7 +554,7 @@ fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibilit // C-export level items remain at `Default`, all other internal // items become `Hidden`. match tcx.reachable_non_generics(id.krate).get(&id) { - Some(SymbolExportLevel::C) => Visibility::Default, + Some(SymbolExportInfo { level: SymbolExportLevel::C, .. }) => Visibility::Default, _ => Visibility::Hidden, } } From 08b70292737b6da048ae15b3333b8b5eb1c83b67 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 2 Apr 2022 22:29:59 +0100 Subject: [PATCH 3/8] Make `#[used]` considered reachable --- compiler/rustc_passes/src/reachable.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index b520e5d04eab9..55745248c9389 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -332,6 +332,8 @@ impl CollectPrivateImplItemsVisitor<'_, '_> { // which are currently akin to allocator symbols. let codegen_attrs = self.tcx.codegen_fn_attrs(def_id); if codegen_attrs.contains_extern_indicator() + || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED) + || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) || codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) { self.worklist.push(def_id); From fefc69ab15cdeae2242a2ea4d7d09a73217a98a6 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 2 Apr 2022 22:54:51 +0100 Subject: [PATCH 4/8] Synthesis object file for `#[used]` and exported symbols --- compiler/rustc_codegen_ssa/src/back/link.rs | 69 +++++++++++++++++++ compiler/rustc_codegen_ssa/src/back/linker.rs | 46 +++++++++++++ .../rustc_codegen_ssa/src/back/metadata.rs | 2 +- .../src/back/symbol_export.rs | 54 +++++++++++++-- compiler/rustc_codegen_ssa/src/base.rs | 7 ++ compiler/rustc_codegen_ssa/src/lib.rs | 2 + .../src/middle/exported_symbols.rs | 10 +++ 7 files changed, 182 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 548ae0e411da7..a88f861311861 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -7,6 +7,7 @@ use rustc_errors::{ErrorGuaranteed, Handler}; use rustc_fs_util::fix_windows_verbatim_for_gcc; use rustc_hir::def_id::CrateNum; use rustc_middle::middle::dependency_format::Linkage; +use rustc_middle::middle::exported_symbols::SymbolExportKind; use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip}; use rustc_session::config::{OutputFilenames, OutputType, PrintRequest, SplitDwarfKind}; use rustc_session::cstore::DllImport; @@ -1654,6 +1655,67 @@ fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor } } +/// Add a synthetic object file that contains reference to all symbols that we want to expose to +/// the linker. +/// +/// Background: we implement rlibs as static library (archives). Linkers treat archives +/// differently from object files: all object files participate in linking, while archives will +/// only participate in linking if they can satisfy at least one undefined reference (version +/// scripts doesn't count). This causes `#[no_mangle]` or `#[used]` items to be ignored by the +/// linker, and since they never participate in the linking, using `KEEP` in the linker scripts +/// can't keep them either. This causes #47384. +/// +/// To keep them around, we could use `--whole-archive` and equivalents to force rlib to +/// participate in linking like object files, but this proves to be expensive (#93791). Therefore +/// we instead just introduce an undefined reference to them. This could be done by `-u` command +/// line option to the linker or `EXTERN(...)` in linker scripts, however they does not only +/// introduce an undefined reference, but also make them the GC roots, preventing `--gc-sections` +/// from removing them, and this is especially problematic for embedded programming where every +/// byte counts. +/// +/// This method creates a synthetic object file, which contains undefined references to all symbols +/// that are necessary for the linking. They are only present in symbol table but not actually +/// used in any sections, so the linker will therefore pick relevant rlibs for linking, but +/// unused `#[no_mangle]` or `#[used]` can still be discard by GC sections. +fn add_linked_symbol_object( + cmd: &mut dyn Linker, + sess: &Session, + tmpdir: &Path, + symbols: &[(String, SymbolExportKind)], +) { + if symbols.is_empty() { + return; + } + + let Some(mut file) = super::metadata::create_object_file(sess) else { + return; + }; + + for (sym, kind) in symbols.iter() { + file.add_symbol(object::write::Symbol { + name: sym.clone().into(), + value: 0, + size: 0, + kind: match kind { + SymbolExportKind::Text => object::SymbolKind::Text, + SymbolExportKind::Data => object::SymbolKind::Data, + SymbolExportKind::Tls => object::SymbolKind::Tls, + }, + scope: object::SymbolScope::Unknown, + weak: false, + section: object::write::SymbolSection::Undefined, + flags: object::SymbolFlags::None, + }); + } + + let path = tmpdir.join("symbols.o"); + let result = std::fs::write(&path, file.write().unwrap()); + if let Err(e) = result { + sess.fatal(&format!("failed to write {}: {}", path.display(), e)); + } + cmd.add_object(&path); +} + /// Add object files containing code from the current crate. fn add_local_crate_regular_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) { 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>>( // Sanitizer libraries. add_sanitizer_libraries(sess, crate_type, cmd); + add_linked_symbol_object( + cmd, + sess, + tmpdir, + &codegen_results.crate_info.linked_symbols[&crate_type], + ); + // Object code from the current crate. // Take careful note of the ordering of the arguments we pass to the linker // here. Linkers will assume that things on the left depend on things to the diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index a90b8a62c623a..044dd9556fed0 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -12,6 +12,7 @@ use std::{env, mem, str}; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_middle::middle::dependency_format::Linkage; +use rustc_middle::middle::exported_symbols::SymbolExportKind; use rustc_middle::ty::TyCtxt; use rustc_serialize::{json, Encoder}; 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, + crate_type: CrateType, +) -> Vec<(String, SymbolExportKind)> { + match crate_type { + CrateType::Executable | CrateType::Cdylib => (), + CrateType::Staticlib | CrateType::ProcMacro | CrateType::Rlib | CrateType::Dylib => { + return Vec::new(); + } + } + + let mut symbols = Vec::new(); + + let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); + for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() { + if info.level.is_below_threshold(export_threshold) || info.used { + symbols.push(( + symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, LOCAL_CRATE), + info.kind, + )); + } + } + + let formats = tcx.dependency_formats(()); + let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap(); + + for (index, dep_format) in deps.iter().enumerate() { + let cnum = CrateNum::new(index + 1); + // For each dependency that we are linking to statically ... + if *dep_format == Linkage::Static { + // ... we add its symbol list to our export list. + for &(symbol, info) in tcx.exported_symbols(cnum).iter() { + if info.level.is_below_threshold(export_threshold) || info.used { + symbols.push(( + symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum), + info.kind, + )); + } + } + } + } + + symbols +} + /// Much simplified and explicit CLI for the NVPTX linker. The linker operates /// with bitcode and uses LLVM backend to generate a PTX assembly. pub struct PtxLinker<'a> { diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index c52269805c46f..2e42272805682 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -94,7 +94,7 @@ fn search_for_metadata<'a>( .map_err(|e| format!("failed to read {} section in '{}': {}", section, path.display(), e)) } -fn create_object_file(sess: &Session) -> Option> { +pub(crate) fn create_object_file(sess: &Session) -> Option> { let endianness = match sess.target.options.endian { Endian::Little => Endianness::Little, Endian::Big => Endianness::Big, diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index e14ebe3ae48f9..56159cc2e08c5 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -9,7 +9,7 @@ use rustc_hir::Node; use rustc_index::vec::IndexVec; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::middle::exported_symbols::{ - metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportLevel, + metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel, }; use rustc_middle::ty::query::{ExternProviders, Providers}; use rustc_middle::ty::subst::{GenericArgKind, SubstsRef}; @@ -124,6 +124,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap< } else { symbol_export_level(tcx, def_id.to_def_id()) }; + let codegen_attrs = tcx.codegen_fn_attrs(def_id.to_def_id()); debug!( "EXPORTED SYMBOL (local): {} ({:?})", tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())), @@ -131,6 +132,17 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap< ); (def_id.to_def_id(), SymbolExportInfo { level: export_level, + kind: if tcx.is_static(def_id.to_def_id()) { + if codegen_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) { + SymbolExportKind::Tls + } else { + SymbolExportKind::Data + } + } else { + SymbolExportKind::Text + }, + used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED) + || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER), }) }) .collect(); @@ -138,7 +150,11 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap< if let Some(id) = tcx.proc_macro_decls_static(()) { reachable_non_generics.insert( id.to_def_id(), - SymbolExportInfo { level: SymbolExportLevel::C }, + SymbolExportInfo { + level: SymbolExportLevel::C, + kind: SymbolExportKind::Data, + used: false, + }, ); } @@ -180,7 +196,11 @@ fn exported_symbols_provider_local<'tcx>( symbols.push(( exported_symbol, - SymbolExportInfo { level: SymbolExportLevel::C }, + SymbolExportInfo { + level: SymbolExportLevel::C, + kind: SymbolExportKind::Text, + used: false, + }, )); } @@ -191,7 +211,11 @@ fn exported_symbols_provider_local<'tcx>( symbols.push(( exported_symbol, - SymbolExportInfo { level: SymbolExportLevel::Rust }, + SymbolExportInfo { + level: SymbolExportLevel::Rust, + kind: SymbolExportKind::Text, + used: false, + }, )); } } @@ -207,7 +231,11 @@ fn exported_symbols_provider_local<'tcx>( let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym)); ( exported_symbol, - SymbolExportInfo { level: SymbolExportLevel::C }, + SymbolExportInfo { + level: SymbolExportLevel::C, + kind: SymbolExportKind::Data, + used: false, + }, ) })); } @@ -220,7 +248,11 @@ fn exported_symbols_provider_local<'tcx>( let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym)); ( exported_symbol, - SymbolExportInfo { level: SymbolExportLevel::C }, + SymbolExportInfo { + level: SymbolExportLevel::C, + kind: SymbolExportKind::Data, + used: false, + }, ) })); } @@ -231,7 +263,11 @@ fn exported_symbols_provider_local<'tcx>( symbols.push(( exported_symbol, - SymbolExportInfo { level: SymbolExportLevel::Rust }, + SymbolExportInfo { + level: SymbolExportLevel::Rust, + kind: SymbolExportKind::Data, + used: false, + }, )); } @@ -269,6 +305,8 @@ fn exported_symbols_provider_local<'tcx>( symbol, SymbolExportInfo { level: SymbolExportLevel::Rust, + kind: SymbolExportKind::Text, + used: false, }, )); } @@ -283,6 +321,8 @@ fn exported_symbols_provider_local<'tcx>( ExportedSymbol::DropGlue(ty), SymbolExportInfo { level: SymbolExportLevel::Rust, + kind: SymbolExportKind::Text, + used: false, }, )); } diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 010560248054e..5a1d020884278 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -801,6 +801,12 @@ impl CrateInfo { .iter() .map(|&c| (c, crate::back::linker::exported_symbols(tcx, c))) .collect(); + let linked_symbols = tcx + .sess + .crate_types() + .iter() + .map(|&c| (c, crate::back::linker::linked_symbols(tcx, c))) + .collect(); let local_crate_name = tcx.crate_name(LOCAL_CRATE); let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID); let subsystem = tcx.sess.first_attr_value_str_by_name(crate_attrs, sym::windows_subsystem); @@ -834,6 +840,7 @@ impl CrateInfo { let mut info = CrateInfo { target_cpu, exported_symbols, + linked_symbols, local_crate_name, compiler_builtins: None, profiler_runtime: None, diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 6cf6be79a8628..f3d5bb4bc624a 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -28,6 +28,7 @@ use rustc_hir::def_id::CrateNum; use rustc_hir::LangItem; use rustc_middle::dep_graph::WorkProduct; use rustc_middle::middle::dependency_format::Dependencies; +use rustc_middle::middle::exported_symbols::SymbolExportKind; use rustc_middle::ty::query::{ExternProviders, Providers}; use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT}; use rustc_session::cstore::{self, CrateSource}; @@ -140,6 +141,7 @@ impl From<&cstore::NativeLib> for NativeLib { pub struct CrateInfo { pub target_cpu: String, pub exported_symbols: FxHashMap>, + pub linked_symbols: FxHashMap>, pub local_crate_name: Symbol, pub compiler_builtins: Option, pub profiler_runtime: Option, diff --git a/compiler/rustc_middle/src/middle/exported_symbols.rs b/compiler/rustc_middle/src/middle/exported_symbols.rs index a605e6dfdc1d3..631fd09ec4cf6 100644 --- a/compiler/rustc_middle/src/middle/exported_symbols.rs +++ b/compiler/rustc_middle/src/middle/exported_symbols.rs @@ -21,11 +21,21 @@ impl SymbolExportLevel { } } +/// Kind of exported symbols. +#[derive(Eq, PartialEq, Debug, Copy, Clone, Encodable, Decodable, HashStable)] +pub enum SymbolExportKind { + Text, + Data, + Tls, +} + /// The `SymbolExportInfo` of a symbols specifies symbol-related information /// that is relevant to code generation and linking. #[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] pub struct SymbolExportInfo { pub level: SymbolExportLevel, + pub kind: SymbolExportKind, + pub used: bool, } #[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] From 3730fe3210c44e32fabd6ad5138fd7239a967240 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 2 Apr 2022 23:53:40 +0100 Subject: [PATCH 5/8] Ignore paths in temporary dir in reproducible build test --- src/test/run-make-fulldeps/reproducible-build/linker.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/run-make-fulldeps/reproducible-build/linker.rs b/src/test/run-make-fulldeps/reproducible-build/linker.rs index 998d1f328596c..b58614c8b4e1f 100644 --- a/src/test/run-make-fulldeps/reproducible-build/linker.rs +++ b/src/test/run-make-fulldeps/reproducible-build/linker.rs @@ -25,6 +25,12 @@ fn main() { let mut contents = Vec::new(); File::open(path).unwrap().read_to_end(&mut contents).unwrap(); + // This file is produced during linking in a temporary directory. + let arg = if arg.ends_with("/symbols.o") { + "symbols.o" + } else { + &*arg + }; out.push_str(&format!("{}: {}\n", arg, hash(&contents))); } From 460054c193e21a548cf8e0c7a86a3faf02fc96f7 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 9 Apr 2022 22:36:25 +0100 Subject: [PATCH 6/8] Add a note to reachable.rs about `#[used]` --- compiler/rustc_passes/src/reachable.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 55745248c9389..b65e334261325 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -332,9 +332,12 @@ impl CollectPrivateImplItemsVisitor<'_, '_> { // which are currently akin to allocator symbols. let codegen_attrs = self.tcx.codegen_fn_attrs(def_id); if codegen_attrs.contains_extern_indicator() + || codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) + // FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by + // `linked_symbols` in cg_ssa. They won't be exported in binary or cdylib due to their + // `SymbolExportLevel::Rust` export level but may end up being exported in dylibs. || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED) || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) - || codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) { self.worklist.push(def_id); } From 98cd818d785f38b59ec874e13e7b83f6521553c7 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sun, 10 Apr 2022 02:16:12 +0100 Subject: [PATCH 7/8] Refactor exported_symbols and linked_symbols for code reuse --- compiler/rustc_codegen_ssa/src/back/linker.rs | 70 +++++++------------ 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 044dd9556fed0..fce340232500f 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -12,7 +12,7 @@ use std::{env, mem, str}; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_middle::middle::dependency_format::Linkage; -use rustc_middle::middle::exported_symbols::SymbolExportKind; +use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportKind}; use rustc_middle::ty::TyCtxt; use rustc_serialize::{json, Encoder}; use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip}; @@ -1519,22 +1519,13 @@ impl<'a> L4Bender<'a> { } } -pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec { - if let Some(ref exports) = tcx.sess.target.override_export_symbols { - return exports.clone(); - } - - let mut symbols = Vec::new(); - - let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); +fn for_each_exported_symbols_include_dep<'tcx>( + tcx: TyCtxt<'tcx>, + crate_type: CrateType, + mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum), +) { for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() { - if info.level.is_below_threshold(export_threshold) { - symbols.push(symbol_export::symbol_name_for_instance_in_crate( - tcx, - symbol, - LOCAL_CRATE, - )); - } + callback(symbol, info, LOCAL_CRATE); } let formats = tcx.dependency_formats(()); @@ -1544,16 +1535,26 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec, crate_type: CrateType) -> Vec { + if let Some(ref exports) = tcx.sess.target.override_export_symbols { + return exports.clone(); + } + + let mut symbols = Vec::new(); + + let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); + for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| { + if info.level.is_below_threshold(export_threshold) { + symbols.push(symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum)); + } + }); symbols } @@ -1572,33 +1573,14 @@ pub(crate) fn linked_symbols( let mut symbols = Vec::new(); let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); - for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() { + for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| { if info.level.is_below_threshold(export_threshold) || info.used { symbols.push(( - symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, LOCAL_CRATE), + symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum), info.kind, )); } - } - - let formats = tcx.dependency_formats(()); - let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap(); - - for (index, dep_format) in deps.iter().enumerate() { - let cnum = CrateNum::new(index + 1); - // For each dependency that we are linking to statically ... - if *dep_format == Linkage::Static { - // ... we add its symbol list to our export list. - for &(symbol, info) in tcx.exported_symbols(cnum).iter() { - if info.level.is_below_threshold(export_threshold) || info.used { - symbols.push(( - symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum), - info.kind, - )); - } - } - } - } + }); symbols } From 73e354906b7c67853cc4e579191b8b52405bf8e7 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Fri, 15 Apr 2022 23:53:58 +0100 Subject: [PATCH 8/8] Fix tests --- compiler/rustc_codegen_ssa/src/back/link.rs | 6 +++--- src/test/run-make/issue-47384/Makefile | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index a88f861311861..45ae5ffc5131e 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1856,9 +1856,6 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( // Pre-link CRT objects. add_pre_link_objects(cmd, sess, link_output_kind, crt_objects_fallback); - // Sanitizer libraries. - add_sanitizer_libraries(sess, crate_type, cmd); - add_linked_symbol_object( cmd, sess, @@ -1866,6 +1863,9 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( &codegen_results.crate_info.linked_symbols[&crate_type], ); + // Sanitizer libraries. + add_sanitizer_libraries(sess, crate_type, cmd); + // Object code from the current crate. // Take careful note of the ordering of the arguments we pass to the linker // here. Linkers will assume that things on the left depend on things to the diff --git a/src/test/run-make/issue-47384/Makefile b/src/test/run-make/issue-47384/Makefile index d3d48966b8547..f10365f8c8811 100644 --- a/src/test/run-make/issue-47384/Makefile +++ b/src/test/run-make/issue-47384/Makefile @@ -1,6 +1,6 @@ -include ../../run-make-fulldeps/tools.mk -# ignore-windows +# only-linux # ignore-cross-compile all: main.rs