Skip to content

change stdlib circular dependencies handling #100404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix CI failures on windows and aarch64-linux
  • Loading branch information
petrochenkov committed Sep 6, 2022
commit c34047cbd5222c239bdaaf306a5962d881908007
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,14 @@ fn add_upstream_rust_crates<'a>(
bundle: Some(false),
whole_archive: Some(false) | None,
} => {
// HACK/FIXME: Fixup a circular dependency between libgcc and libc
// with glibc. This logic should be moved to the libc crate.
if sess.target.os == "linux"
&& sess.target.env == "gnu"
&& name == "c"
{
cmd.link_staticlib("gcc", false);
}
cmd.link_staticlib(name, lib.verbatim.unwrap_or(false));
}
NativeLibKind::LinkArg => {
Expand Down
48 changes: 28 additions & 20 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,26 +849,34 @@ impl CrateInfo {

// Handle circular dependencies in the standard library.
// See comment before `add_linked_symbol_object` function for the details.
let missing_weak_lang_items: FxHashSet<&Symbol> = info
.used_crates
.iter()
.flat_map(|cnum| {
tcx.missing_lang_items(*cnum)
.iter()
.filter(|l| lang_items::required(tcx, **l))
.filter_map(|item| WEAK_ITEMS_SYMBOLS.get(item))
})
.collect();
info.linked_symbols
.iter_mut()
.filter(|(crate_type, _)| !matches!(crate_type, CrateType::Rlib | CrateType::Staticlib))
.for_each(|(_, linked_symbols)| {
linked_symbols.extend(
missing_weak_lang_items
// With msvc-like linkers it's both unnecessary (they support circular dependencies),
// and causes linking issues (when weak lang item symbols are "privatized" by LTO).
let target = &tcx.sess.target;
if !target.is_like_msvc {
let missing_weak_lang_items: FxHashSet<&Symbol> = info
.used_crates
.iter()
.flat_map(|cnum| {
tcx.missing_lang_items(*cnum)
.iter()
.map(|item| (item.to_string(), SymbolExportKind::Text)),
)
});
.filter(|l| lang_items::required(tcx, **l))
.filter_map(|item| WEAK_ITEMS_SYMBOLS.get(item))
})
.collect();
let prefix = if target.is_like_windows && target.arch == "x86" { "_" } else { "" };
info.linked_symbols
.iter_mut()
.filter(|(crate_type, _)| {
!matches!(crate_type, CrateType::Rlib | CrateType::Staticlib)
})
.for_each(|(_, linked_symbols)| {
linked_symbols.extend(
missing_weak_lang_items
.iter()
.map(|item| (format!("{prefix}{item}"), SymbolExportKind::Text)),
)
});
}

let embed_visualizers = tcx.sess.crate_types().iter().any(|&crate_type| match crate_type {
CrateType::Executable | CrateType::Dylib | CrateType::Cdylib => {
Expand All @@ -888,7 +896,7 @@ impl CrateInfo {
}
});

if tcx.sess.target.is_like_msvc && embed_visualizers {
if target.is_like_msvc && embed_visualizers {
info.natvis_debugger_visualizers =
collect_debugger_visualizers_transitive(tcx, DebuggerVisualizerType::Natvis);
}
Expand Down