mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Auto merge of #95604 - nbdd0121:used2, r=petrochenkov
Generate synthetic object file to ensure all exported and used symbols participate in the linking Fix #50007 and #47384 This is the synthetic object file approach that I described in https://github.com/rust-lang/rust/pull/95363#issuecomment-1079932354, allowing all exported and used symbols to be linked while still allowing them to be GCed. Related #93791, #95363 r? `@petrochenkov` cc `@carbotaniuman`
This commit is contained in:
commit
18b53cefdf
@ -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) || info.used {
|
||||
Some(CString::new(name.as_str()).unwrap())
|
||||
} else {
|
||||
None
|
||||
|
@ -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;
|
||||
@ -1655,6 +1656,73 @@ 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;
|
||||
};
|
||||
|
||||
// NOTE(nbdd0121): MSVC will hang if the input object file contains no sections,
|
||||
// so add an empty section.
|
||||
if file.format() == object::BinaryFormat::Coff {
|
||||
file.add_section(Vec::new(), ".text".into(), object::SectionKind::Text);
|
||||
}
|
||||
|
||||
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()) {
|
||||
@ -1795,6 +1863,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
|
||||
// Pre-link CRT objects.
|
||||
add_pre_link_objects(cmd, sess, link_output_kind, crt_objects_fallback);
|
||||
|
||||
add_linked_symbol_object(
|
||||
cmd,
|
||||
sess,
|
||||
tmpdir,
|
||||
&codegen_results.crate_info.linked_symbols[&crate_type],
|
||||
);
|
||||
|
||||
// Sanitizer libraries.
|
||||
add_sanitizer_libraries(sess, crate_type, cmd);
|
||||
|
||||
|
@ -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::{ExportedSymbol, SymbolExportInfo, SymbolExportKind};
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_serialize::{json, Encoder};
|
||||
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
|
||||
@ -1518,22 +1519,13 @@ impl<'a> L4Bender<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
|
||||
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
|
||||
return exports.iter().map(ToString::to_string).collect();
|
||||
}
|
||||
|
||||
let mut symbols = Vec::new();
|
||||
|
||||
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
|
||||
for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() {
|
||||
if level.is_below_threshold(export_threshold) {
|
||||
symbols.push(symbol_export::symbol_name_for_instance_in_crate(
|
||||
tcx,
|
||||
symbol,
|
||||
LOCAL_CRATE,
|
||||
));
|
||||
}
|
||||
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() {
|
||||
callback(symbol, info, LOCAL_CRATE);
|
||||
}
|
||||
|
||||
let formats = tcx.dependency_formats(());
|
||||
@ -1543,16 +1535,52 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St
|
||||
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, level) in tcx.exported_symbols(cnum).iter() {
|
||||
if !level.is_below_threshold(export_threshold) {
|
||||
continue;
|
||||
}
|
||||
|
||||
symbols.push(symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum));
|
||||
for &(symbol, info) in tcx.exported_symbols(cnum).iter() {
|
||||
callback(symbol, info, cnum);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
|
||||
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
|
||||
return exports.iter().map(ToString::to_string).collect();
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
pub(crate) fn linked_symbols(
|
||||
tcx: TyCtxt<'_>,
|
||||
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_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, cnum),
|
||||
info.kind,
|
||||
));
|
||||
}
|
||||
});
|
||||
|
||||
symbols
|
||||
}
|
||||
|
@ -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<write::Object<'static>> {
|
||||
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
|
||||
let endianness = match sess.target.options.endian {
|
||||
Endian::Little => Endianness::Little,
|
||||
Endian::Big => Endianness::Big,
|
||||
|
@ -7,7 +7,7 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
|
||||
use rustc_hir::Node;
|
||||
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
||||
use rustc_middle::middle::exported_symbols::{
|
||||
metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
|
||||
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
|
||||
};
|
||||
use rustc_middle::ty::query::{ExternProviders, Providers};
|
||||
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
|
||||
@ -40,7 +40,7 @@ pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
|
||||
}
|
||||
}
|
||||
|
||||
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportLevel> {
|
||||
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportInfo> {
|
||||
assert_eq!(cnum, LOCAL_CRATE);
|
||||
|
||||
if !tcx.sess.opts.output_types.should_codegen() {
|
||||
@ -103,36 +103,51 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
|
||||
}
|
||||
})
|
||||
.map(|def_id| {
|
||||
let export_level = if special_runtime_crate {
|
||||
let (export_level, used) = if special_runtime_crate {
|
||||
let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name;
|
||||
// We can probably do better here by just ensuring that
|
||||
// it has hidden visibility rather than public
|
||||
// visibility, as this is primarily here to ensure it's
|
||||
// not stripped during LTO.
|
||||
//
|
||||
// In general though we won't link right if these
|
||||
// symbols are stripped, and LTO currently strips them.
|
||||
match name {
|
||||
// We won't link right if these symbols are stripped during LTO.
|
||||
let used = match name {
|
||||
"rust_eh_personality"
|
||||
| "rust_eh_register_frames"
|
||||
| "rust_eh_unregister_frames" =>
|
||||
SymbolExportLevel::C,
|
||||
_ => SymbolExportLevel::Rust,
|
||||
}
|
||||
| "rust_eh_unregister_frames" => true,
|
||||
_ => false,
|
||||
};
|
||||
(SymbolExportLevel::Rust, used)
|
||||
} else {
|
||||
symbol_export_level(tcx, def_id.to_def_id())
|
||||
(symbol_export_level(tcx, def_id.to_def_id()), false)
|
||||
};
|
||||
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())),
|
||||
export_level
|
||||
);
|
||||
(def_id.to_def_id(), export_level)
|
||||
(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) || used,
|
||||
})
|
||||
})
|
||||
.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,
|
||||
kind: SymbolExportKind::Data,
|
||||
used: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
reachable_non_generics
|
||||
@ -141,8 +156,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
|
||||
}
|
||||
@ -155,7 +170,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() {
|
||||
@ -165,13 +180,20 @@ 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,
|
||||
kind: SymbolExportKind::Text,
|
||||
used: false,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
if tcx.allocator_kind(()).is_some() {
|
||||
@ -179,7 +201,14 @@ 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,
|
||||
kind: SymbolExportKind::Text,
|
||||
used: false,
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,17 +221,39 @@ 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,
|
||||
kind: SymbolExportKind::Data,
|
||||
used: false,
|
||||
},
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
if tcx.sess.opts.debugging_opts.sanitizer.contains(SanitizerSet::MEMORY) {
|
||||
// Similar to profiling, preserve weak msan symbol during LTO.
|
||||
const MSAN_WEAK_SYMBOLS: [&str; 2] = ["__msan_track_origins", "__msan_keep_going"];
|
||||
let mut msan_weak_symbols = Vec::new();
|
||||
|
||||
symbols.extend(MSAN_WEAK_SYMBOLS.iter().map(|sym| {
|
||||
// Similar to profiling, preserve weak msan symbol during LTO.
|
||||
if tcx.sess.opts.debugging_opts.sanitizer_recover.contains(SanitizerSet::MEMORY) {
|
||||
msan_weak_symbols.push("__msan_keep_going");
|
||||
}
|
||||
|
||||
if tcx.sess.opts.debugging_opts.sanitizer_memory_track_origins != 0 {
|
||||
msan_weak_symbols.push("__msan_track_origins");
|
||||
}
|
||||
|
||||
symbols.extend(msan_weak_symbols.into_iter().map(|sym| {
|
||||
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
|
||||
(exported_symbol, SymbolExportLevel::C)
|
||||
(
|
||||
exported_symbol,
|
||||
SymbolExportInfo {
|
||||
level: SymbolExportLevel::C,
|
||||
kind: SymbolExportKind::Data,
|
||||
used: false,
|
||||
},
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
@ -210,7 +261,14 @@ 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,
|
||||
kind: SymbolExportKind::Data,
|
||||
used: false,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
if tcx.sess.opts.share_generics() && tcx.local_crate_exports_generics() {
|
||||
@ -243,7 +301,14 @@ 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,
|
||||
kind: SymbolExportKind::Text,
|
||||
used: false,
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
MonoItem::Fn(Instance { def: InstanceDef::DropGlue(_, Some(ty)), substs }) => {
|
||||
@ -252,7 +317,14 @@ 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,
|
||||
kind: SymbolExportKind::Text,
|
||||
used: false,
|
||||
},
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
// Any other symbols don't qualify for sharing
|
||||
|
@ -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<B> = Arc<
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
|
||||
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportInfo)>>>;
|
||||
|
||||
/// Additional resources used by optimize_and_codegen (not module specific)
|
||||
#[derive(Clone)]
|
||||
|
@ -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,
|
||||
|
@ -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_serialize::{opaque, Decodable, Decoder, Encoder};
|
||||
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
|
||||
@ -141,6 +142,7 @@ impl From<&cstore::NativeLib> for NativeLib {
|
||||
pub struct CrateInfo {
|
||||
pub target_cpu: String,
|
||||
pub exported_symbols: FxHashMap<CrateType, Vec<String>>,
|
||||
pub linked_symbols: FxHashMap<CrateType, Vec<(String, SymbolExportKind)>>,
|
||||
pub local_crate_name: Symbol,
|
||||
pub compiler_builtins: Option<CrateNum>,
|
||||
pub profiler_runtime: Option<CrateNum>,
|
||||
|
@ -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;
|
||||
@ -1429,7 +1429,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)))
|
||||
}
|
||||
|
||||
|
@ -190,9 +190,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
|
||||
}
|
||||
|
@ -22,7 +22,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;
|
||||
@ -1874,8 +1874,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.
|
||||
|
@ -14,7 +14,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;
|
||||
@ -220,7 +220,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,
|
||||
|
@ -21,6 +21,23 @@ 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)]
|
||||
pub enum ExportedSymbol<'tcx> {
|
||||
NonGeneric(DefId),
|
||||
|
@ -1359,7 +1359,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<SymbolExportLevel> {
|
||||
-> DefIdMap<SymbolExportInfo> {
|
||||
storage(ArenaCacheSelector<'tcx>)
|
||||
desc { "looking up the exported symbols of a crate" }
|
||||
separate_provide_extern
|
||||
@ -1675,7 +1675,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
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -5,7 +5,7 @@ use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::{DefId, 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,
|
||||
}
|
||||
}
|
||||
|
@ -333,6 +333,11 @@ impl CollectPrivateImplItemsVisitor<'_, '_> {
|
||||
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)
|
||||
{
|
||||
self.worklist.push(def_id);
|
||||
}
|
||||
|
@ -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") || arg.ends_with("\\symbols.o") {
|
||||
"symbols.o"
|
||||
} else {
|
||||
&*arg
|
||||
};
|
||||
out.push_str(&format!("{}: {}\n", arg, hash(&contents)));
|
||||
}
|
||||
|
||||
|
@ -54,9 +54,12 @@ all:
|
||||
# Check that a Rust dylib does not export generics if -Zshare-generics=no
|
||||
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "0" ]
|
||||
|
||||
# FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
|
||||
ifndef IS_WINDOWS
|
||||
# Check that an executable does not export any dynamic symbols
|
||||
[ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "0" ]
|
||||
[ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_rust_function_from_exe)" -eq "0" ]
|
||||
endif
|
||||
|
||||
|
||||
# Check the combined case, where we generate a cdylib and an rlib in the same
|
||||
@ -91,6 +94,8 @@ all:
|
||||
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rlib)" -eq "1" ]
|
||||
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "1" ]
|
||||
|
||||
ifndef IS_WINDOWS
|
||||
# Check that an executable does not export any dynamic symbols
|
||||
[ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "0" ]
|
||||
[ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_rust_function_from_exe)" -eq "0" ]
|
||||
endif
|
||||
|
12
src/test/run-make/issue-47384/Makefile
Normal file
12
src/test/run-make/issue-47384/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
-include ../../run-make-fulldeps/tools.mk
|
||||
|
||||
# only-linux
|
||||
# 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
|
12
src/test/run-make/issue-47384/lib.rs
Normal file
12
src/test/run-make/issue-47384/lib.rs
Normal file
@ -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
|
||||
}
|
||||
}
|
7
src/test/run-make/issue-47384/linker.ld
Normal file
7
src/test/run-make/issue-47384/linker.ld
Normal file
@ -0,0 +1,7 @@
|
||||
SECTIONS
|
||||
{
|
||||
.static : ALIGN(4)
|
||||
{
|
||||
KEEP(*(.rodata.STATIC));
|
||||
}
|
||||
}
|
1
src/test/run-make/issue-47384/main.rs
Normal file
1
src/test/run-make/issue-47384/main.rs
Normal file
@ -0,0 +1 @@
|
||||
extern crate lib;
|
Loading…
Reference in New Issue
Block a user