Skip to content

Commit cfe0cff

Browse files
authored
Rollup merge of rust-lang#130960 - cuviper:cdylib-soname, r=petrochenkov
Only add an automatic SONAME for Rust dylibs rust-lang#126094 added an automatic relative `SONAME` to all dynamic libraries, but it was really only needed for Rust `--crate-type="dylib"`. In Fedora, it was a surprise to see `SONAME` on `"cdylib"` libraries like Python modules, especially because that generates an undesirable RPM `Provides`. We can instead add a `SONAME` just for Rust dylibs by passing the crate-type argument farther. Ref: https://bugzilla.redhat.com/show_bug.cgi?id=2314879
2 parents f33fa3f + f46057b commit cfe0cff

File tree

3 files changed

+80
-21
lines changed

3 files changed

+80
-21
lines changed

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,7 @@ fn add_order_independent_options(
24902490
}
24912491
}
24922492

2493-
cmd.set_output_kind(link_output_kind, out_filename);
2493+
cmd.set_output_kind(link_output_kind, crate_type, out_filename);
24942494

24952495
add_relro_args(cmd, sess);
24962496

Diff for: compiler/rustc_codegen_ssa/src/back/linker.rs

+69-14
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ pub(crate) trait Linker {
275275
fn is_cc(&self) -> bool {
276276
false
277277
}
278-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
278+
fn set_output_kind(
279+
&mut self,
280+
output_kind: LinkOutputKind,
281+
crate_type: CrateType,
282+
out_filename: &Path,
283+
);
279284
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
280285
bug!("dylib linked with unsupported linker")
281286
}
@@ -396,7 +401,7 @@ impl<'a> GccLinker<'a> {
396401
]);
397402
}
398403

399-
fn build_dylib(&mut self, out_filename: &Path) {
404+
fn build_dylib(&mut self, crate_type: CrateType, out_filename: &Path) {
400405
// On mac we need to tell the linker to let this library be rpathed
401406
if self.sess.target.is_like_osx {
402407
if !self.is_ld {
@@ -427,7 +432,7 @@ impl<'a> GccLinker<'a> {
427432
let mut out_implib = OsString::from("--out-implib=");
428433
out_implib.push(out_filename.with_file_name(implib_name));
429434
self.link_arg(out_implib);
430-
} else {
435+
} else if crate_type == CrateType::Dylib {
431436
// When dylibs are linked by a full path this value will get into `DT_NEEDED`
432437
// instead of the full path, so the library can be later found in some other
433438
// location than that specific path.
@@ -474,7 +479,12 @@ impl<'a> Linker for GccLinker<'a> {
474479
!self.is_ld
475480
}
476481

477-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
482+
fn set_output_kind(
483+
&mut self,
484+
output_kind: LinkOutputKind,
485+
crate_type: CrateType,
486+
out_filename: &Path,
487+
) {
478488
match output_kind {
479489
LinkOutputKind::DynamicNoPicExe => {
480490
if !self.is_ld && self.is_gnu {
@@ -509,10 +519,10 @@ impl<'a> Linker for GccLinker<'a> {
509519
self.link_args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
510520
}
511521
}
512-
LinkOutputKind::DynamicDylib => self.build_dylib(out_filename),
522+
LinkOutputKind::DynamicDylib => self.build_dylib(crate_type, out_filename),
513523
LinkOutputKind::StaticDylib => {
514524
self.link_or_cc_arg("-static");
515-
self.build_dylib(out_filename);
525+
self.build_dylib(crate_type, out_filename);
516526
}
517527
LinkOutputKind::WasiReactorExe => {
518528
self.link_args(&["--entry", "_initialize"]);
@@ -866,7 +876,12 @@ impl<'a> Linker for MsvcLinker<'a> {
866876
&mut self.cmd
867877
}
868878

869-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
879+
fn set_output_kind(
880+
&mut self,
881+
output_kind: LinkOutputKind,
882+
_crate_type: CrateType,
883+
out_filename: &Path,
884+
) {
870885
match output_kind {
871886
LinkOutputKind::DynamicNoPicExe
872887
| LinkOutputKind::DynamicPicExe
@@ -1124,7 +1139,13 @@ impl<'a> Linker for EmLinker<'a> {
11241139
true
11251140
}
11261141

1127-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1142+
fn set_output_kind(
1143+
&mut self,
1144+
_output_kind: LinkOutputKind,
1145+
_crate_type: CrateType,
1146+
_out_filename: &Path,
1147+
) {
1148+
}
11281149

11291150
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
11301151
// Emscripten always links statically
@@ -1273,7 +1294,12 @@ impl<'a> Linker for WasmLd<'a> {
12731294
&mut self.cmd
12741295
}
12751296

1276-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, _out_filename: &Path) {
1297+
fn set_output_kind(
1298+
&mut self,
1299+
output_kind: LinkOutputKind,
1300+
_crate_type: CrateType,
1301+
_out_filename: &Path,
1302+
) {
12771303
match output_kind {
12781304
LinkOutputKind::DynamicNoPicExe
12791305
| LinkOutputKind::DynamicPicExe
@@ -1422,7 +1448,13 @@ impl<'a> Linker for L4Bender<'a> {
14221448
&mut self.cmd
14231449
}
14241450

1425-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1451+
fn set_output_kind(
1452+
&mut self,
1453+
_output_kind: LinkOutputKind,
1454+
_crate_type: CrateType,
1455+
_out_filename: &Path,
1456+
) {
1457+
}
14261458

14271459
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
14281460
self.hint_static();
@@ -1568,7 +1600,12 @@ impl<'a> Linker for AixLinker<'a> {
15681600
&mut self.cmd
15691601
}
15701602

1571-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
1603+
fn set_output_kind(
1604+
&mut self,
1605+
output_kind: LinkOutputKind,
1606+
_crate_type: CrateType,
1607+
out_filename: &Path,
1608+
) {
15721609
match output_kind {
15731610
LinkOutputKind::DynamicDylib => {
15741611
self.hint_dynamic();
@@ -1775,7 +1812,13 @@ impl<'a> Linker for PtxLinker<'a> {
17751812
&mut self.cmd
17761813
}
17771814

1778-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1815+
fn set_output_kind(
1816+
&mut self,
1817+
_output_kind: LinkOutputKind,
1818+
_crate_type: CrateType,
1819+
_out_filename: &Path,
1820+
) {
1821+
}
17791822

17801823
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
17811824
panic!("staticlibs not supported")
@@ -1841,7 +1884,13 @@ impl<'a> Linker for LlbcLinker<'a> {
18411884
&mut self.cmd
18421885
}
18431886

1844-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1887+
fn set_output_kind(
1888+
&mut self,
1889+
_output_kind: LinkOutputKind,
1890+
_crate_type: CrateType,
1891+
_out_filename: &Path,
1892+
) {
1893+
}
18451894

18461895
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
18471896
panic!("staticlibs not supported")
@@ -1912,7 +1961,13 @@ impl<'a> Linker for BpfLinker<'a> {
19121961
&mut self.cmd
19131962
}
19141963

1915-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1964+
fn set_output_kind(
1965+
&mut self,
1966+
_output_kind: LinkOutputKind,
1967+
_crate_type: CrateType,
1968+
_out_filename: &Path,
1969+
) {
1970+
}
19161971

19171972
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
19181973
panic!("staticlibs not supported")

Diff for: tests/run-make/dylib-soname/rmake.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
use run_make_support::{cmd, run_in_tmpdir, rustc};
88

99
fn main() {
10+
let check = |ty: &str| {
11+
rustc().crate_name("foo").crate_type(ty).input("foo.rs").run();
12+
cmd("readelf").arg("-d").arg("libfoo.so").run()
13+
};
1014
run_in_tmpdir(|| {
11-
rustc().crate_name("foo").crate_type("dylib").input("foo.rs").run();
12-
cmd("readelf")
13-
.arg("-d")
14-
.arg("libfoo.so")
15-
.run()
16-
.assert_stdout_contains("Library soname: [libfoo.so]");
15+
// Rust dylibs should get a relative SONAME
16+
check("dylib").assert_stdout_contains("Library soname: [libfoo.so]");
17+
});
18+
run_in_tmpdir(|| {
19+
// C dylibs should not implicitly get any SONAME
20+
check("cdylib").assert_stdout_not_contains("Library soname:");
1721
});
1822
}

0 commit comments

Comments
 (0)