Skip to content

Commit c8410d4

Browse files
authored
Rollup merge of rust-lang#108017 - chbaker0:fix-105967, r=chbaker0
Add `--no-undefined-version` link flag and fix associated breakage LLVM upstream sets `--no-undefined-version` by default in lld: https://reviews.llvm.org/D135402. Due to a bug in how version scripts are generated, this breaks the `dylib` output type for most crates. See rust-lang#105967 (comment) for details. This PR adds the flag to gcc flavor linkers in anticipation of this LLVM change rolling in, and patches `rustc` to not attempt to export `__rust_*` allocator symbols when they weren't generated. Fixes rust-lang#105967
2 parents 0f3a0dc + cb41803 commit c8410d4

File tree

4 files changed

+38
-40
lines changed

4 files changed

+38
-40
lines changed

compiler/rustc_codegen_cranelift/src/allocator.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::prelude::*;
55

66
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
7+
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
78
use rustc_session::config::OomStrategy;
89
use rustc_span::symbol::sym;
910

@@ -13,24 +14,15 @@ pub(crate) fn codegen(
1314
module: &mut impl Module,
1415
unwind_context: &mut UnwindContext,
1516
) -> bool {
16-
let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
17-
use rustc_middle::middle::dependency_format::Linkage;
18-
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
19-
});
20-
if any_dynamic_crate {
21-
false
22-
} else if let Some(kind) = tcx.allocator_kind(()) {
23-
codegen_inner(
24-
module,
25-
unwind_context,
26-
kind,
27-
tcx.alloc_error_handler_kind(()).unwrap(),
28-
tcx.sess.opts.unstable_opts.oom,
29-
);
30-
true
31-
} else {
32-
false
33-
}
17+
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
18+
codegen_inner(
19+
module,
20+
unwind_context,
21+
kind,
22+
tcx.alloc_error_handler_kind(()).unwrap(),
23+
tcx.sess.opts.unstable_opts.oom,
24+
);
25+
true
3426
}
3527

3628
fn codegen_inner(

compiler/rustc_codegen_ssa/src/back/linker.rs

+1
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ impl<'a> Linker for GccLinker<'a> {
720720
let mut arg = OsString::from("--version-script=");
721721
arg.push(path);
722722
self.linker_arg(arg);
723+
self.linker_arg("--no-undefined-version");
723724
}
724725
}
725726
}

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::base::allocator_kind_for_codegen;
2+
13
use std::collections::hash_map::Entry::*;
24

35
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
@@ -200,7 +202,8 @@ fn exported_symbols_provider_local(
200202
));
201203
}
202204

203-
if tcx.allocator_kind(()).is_some() {
205+
// Mark allocator shim symbols as exported only if they were generated.
206+
if allocator_kind_for_codegen(tcx).is_some() {
204207
for symbol_name in ALLOCATOR_METHODS
205208
.iter()
206209
.map(|method| format!("__rust_{}", method.name))

compiler/rustc_codegen_ssa/src/base.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::mir::place::PlaceRef;
1313
use crate::traits::*;
1414
use crate::{CachedModuleCodegen, CompiledModule, CrateInfo, MemFlags, ModuleCodegen, ModuleKind};
1515

16+
use rustc_ast::expand::allocator::AllocatorKind;
1617
use rustc_attr as attr;
1718
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1819
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
@@ -545,6 +546,23 @@ pub fn collect_debugger_visualizers_transitive(
545546
.collect::<BTreeSet<_>>()
546547
}
547548

549+
/// Decide allocator kind to codegen. If `Some(_)` this will be the same as
550+
/// `tcx.allocator_kind`, but it may be `None` in more cases (e.g. if using
551+
/// allocator definitions from a dylib dependency).
552+
pub fn allocator_kind_for_codegen(tcx: TyCtxt<'_>) -> Option<AllocatorKind> {
553+
// If the crate doesn't have an `allocator_kind` set then there's definitely
554+
// no shim to generate. Otherwise we also check our dependency graph for all
555+
// our output crate types. If anything there looks like its a `Dynamic`
556+
// linkage, then it's already got an allocator shim and we'll be using that
557+
// one instead. If nothing exists then it's our job to generate the
558+
// allocator!
559+
let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
560+
use rustc_middle::middle::dependency_format::Linkage;
561+
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
562+
});
563+
if any_dynamic_crate { None } else { tcx.allocator_kind(()) }
564+
}
565+
548566
pub fn codegen_crate<B: ExtraBackendMethods>(
549567
backend: B,
550568
tcx: TyCtxt<'_>,
@@ -615,20 +633,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
615633
);
616634

617635
// Codegen an allocator shim, if necessary.
618-
//
619-
// If the crate doesn't have an `allocator_kind` set then there's definitely
620-
// no shim to generate. Otherwise we also check our dependency graph for all
621-
// our output crate types. If anything there looks like its a `Dynamic`
622-
// linkage, then it's already got an allocator shim and we'll be using that
623-
// one instead. If nothing exists then it's our job to generate the
624-
// allocator!
625-
let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
626-
use rustc_middle::middle::dependency_format::Linkage;
627-
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
628-
});
629-
let allocator_module = if any_dynamic_crate {
630-
None
631-
} else if let Some(kind) = tcx.allocator_kind(()) {
636+
if let Some(kind) = allocator_kind_for_codegen(tcx) {
632637
let llmod_id =
633638
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
634639
let module_llvm = tcx.sess.time("write_allocator_module", || {
@@ -642,13 +647,10 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
642647
)
643648
});
644649

645-
Some(ModuleCodegen { name: llmod_id, module_llvm, kind: ModuleKind::Allocator })
646-
} else {
647-
None
648-
};
649-
650-
if let Some(allocator_module) = allocator_module {
651-
ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, allocator_module);
650+
ongoing_codegen.submit_pre_codegened_module_to_llvm(
651+
tcx,
652+
ModuleCodegen { name: llmod_id, module_llvm, kind: ModuleKind::Allocator },
653+
);
652654
}
653655

654656
// For better throughput during parallel processing by LLVM, we used to sort

0 commit comments

Comments
 (0)