Skip to content

Commit 98bf29d

Browse files
authored
Rollup merge of rust-lang#84254 - jclulow:illumos-link-order, r=petrochenkov
illumos should put libc last in library search order Under some conditions, the toolchain will produce a sequence of linker arguments that result in a NEEDED list that puts libc before libgcc_s; e.g., [0] NEEDED 0x2046ba libc.so.1 [1] NEEDED 0x204723 libm.so.2 [2] NEEDED 0x204736 libsocket.so.1 [3] NEEDED 0x20478b libumem.so.1 [4] NEEDED 0x204763 libgcc_s.so.1 Both libc and libgcc_s provide an unwinder implementation, but libgcc_s provides some extra symbols upon which Rust directly depends. If libc is first in the NEEDED list we will find some of those symbols in libc but others in libgcc_s, resulting in undefined behaviour as the two implementations do not use compatible interior data structures. This solution is not perfect, but is the simplest way to produce correct binaries on illumos for now.
2 parents e5f83d2 + 31c2ad0 commit 98bf29d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+8
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ impl<'a> Linker for GccLinker<'a> {
340340
}
341341

342342
fn link_dylib(&mut self, lib: Symbol, verbatim: bool, as_needed: bool) {
343+
if self.sess.target.os == "illumos" && lib.as_str() == "c" {
344+
// libc will be added via late_link_args on illumos so that it will
345+
// appear last in the library search order.
346+
// FIXME: This should be replaced by a more complete and generic
347+
// mechanism for controlling the order of library arguments passed
348+
// to the linker.
349+
return;
350+
}
343351
if !as_needed {
344352
if self.sess.target.is_like_osx {
345353
// FIXME(81490): ld64 doesn't support these flags but macOS 11

compiler/rustc_target/src/spec/illumos_base.rs

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ pub fn opts() -> TargetOptions {
66
late_link_args.insert(
77
LinkerFlavor::Gcc,
88
vec![
9+
// The illumos libc contains a stack unwinding implementation, as
10+
// does libgcc_s. The latter implementation includes several
11+
// additional symbols that are not always in base libc. To force
12+
// the consistent use of just one unwinder, we ensure libc appears
13+
// after libgcc_s in the NEEDED list for the resultant binary by
14+
// ignoring any attempts to add it as a dynamic dependency until the
15+
// very end.
16+
// FIXME: This should be replaced by a more complete and generic
17+
// mechanism for controlling the order of library arguments passed
18+
// to the linker.
19+
"-lc".to_string(),
920
// LLVM will insert calls to the stack protector functions
1021
// "__stack_chk_fail" and "__stack_chk_guard" into code in native
1122
// object files. Some platforms include these symbols directly in

0 commit comments

Comments
 (0)