Skip to content

Commit 6917db6

Browse files
committed
Only add an automatic SONAME for Rust dylibs
(cherry picked from commit f46057b)
1 parent 6a3b69c commit 6917db6

File tree

3 files changed

+80
-21
lines changed

3 files changed

+80
-21
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ fn add_order_independent_options(
24932493
}
24942494
}
24952495

2496-
cmd.set_output_kind(link_output_kind, out_filename);
2496+
cmd.set_output_kind(link_output_kind, crate_type, out_filename);
24972497

24982498
add_relro_args(cmd, sess);
24992499

compiler/rustc_codegen_ssa/src/back/linker.rs

+69-14
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,12 @@ pub trait Linker {
266266
fn is_cc(&self) -> bool {
267267
false
268268
}
269-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
269+
fn set_output_kind(
270+
&mut self,
271+
output_kind: LinkOutputKind,
272+
crate_type: CrateType,
273+
out_filename: &Path,
274+
);
270275
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
271276
bug!("dylib linked with unsupported linker")
272277
}
@@ -387,7 +392,7 @@ impl<'a> GccLinker<'a> {
387392
]);
388393
}
389394

390-
fn build_dylib(&mut self, out_filename: &Path) {
395+
fn build_dylib(&mut self, crate_type: CrateType, out_filename: &Path) {
391396
// On mac we need to tell the linker to let this library be rpathed
392397
if self.sess.target.is_like_osx {
393398
if !self.is_ld {
@@ -418,7 +423,7 @@ impl<'a> GccLinker<'a> {
418423
let mut out_implib = OsString::from("--out-implib=");
419424
out_implib.push(out_filename.with_file_name(implib_name));
420425
self.link_arg(out_implib);
421-
} else {
426+
} else if crate_type == CrateType::Dylib {
422427
// When dylibs are linked by a full path this value will get into `DT_NEEDED`
423428
// instead of the full path, so the library can be later found in some other
424429
// location than that specific path.
@@ -465,7 +470,12 @@ impl<'a> Linker for GccLinker<'a> {
465470
!self.is_ld
466471
}
467472

468-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
473+
fn set_output_kind(
474+
&mut self,
475+
output_kind: LinkOutputKind,
476+
crate_type: CrateType,
477+
out_filename: &Path,
478+
) {
469479
match output_kind {
470480
LinkOutputKind::DynamicNoPicExe => {
471481
if !self.is_ld && self.is_gnu {
@@ -500,10 +510,10 @@ impl<'a> Linker for GccLinker<'a> {
500510
self.link_args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
501511
}
502512
}
503-
LinkOutputKind::DynamicDylib => self.build_dylib(out_filename),
513+
LinkOutputKind::DynamicDylib => self.build_dylib(crate_type, out_filename),
504514
LinkOutputKind::StaticDylib => {
505515
self.link_or_cc_arg("-static");
506-
self.build_dylib(out_filename);
516+
self.build_dylib(crate_type, out_filename);
507517
}
508518
LinkOutputKind::WasiReactorExe => {
509519
self.link_args(&["--entry", "_initialize"]);
@@ -859,7 +869,12 @@ impl<'a> Linker for MsvcLinker<'a> {
859869
&mut self.cmd
860870
}
861871

862-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
872+
fn set_output_kind(
873+
&mut self,
874+
output_kind: LinkOutputKind,
875+
_crate_type: CrateType,
876+
out_filename: &Path,
877+
) {
863878
match output_kind {
864879
LinkOutputKind::DynamicNoPicExe
865880
| LinkOutputKind::DynamicPicExe
@@ -1111,7 +1126,13 @@ impl<'a> Linker for EmLinker<'a> {
11111126
true
11121127
}
11131128

1114-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1129+
fn set_output_kind(
1130+
&mut self,
1131+
_output_kind: LinkOutputKind,
1132+
_crate_type: CrateType,
1133+
_out_filename: &Path,
1134+
) {
1135+
}
11151136

11161137
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
11171138
// Emscripten always links statically
@@ -1260,7 +1281,12 @@ impl<'a> Linker for WasmLd<'a> {
12601281
&mut self.cmd
12611282
}
12621283

1263-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, _out_filename: &Path) {
1284+
fn set_output_kind(
1285+
&mut self,
1286+
output_kind: LinkOutputKind,
1287+
_crate_type: CrateType,
1288+
_out_filename: &Path,
1289+
) {
12641290
match output_kind {
12651291
LinkOutputKind::DynamicNoPicExe
12661292
| LinkOutputKind::DynamicPicExe
@@ -1409,7 +1435,13 @@ impl<'a> Linker for L4Bender<'a> {
14091435
&mut self.cmd
14101436
}
14111437

1412-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1438+
fn set_output_kind(
1439+
&mut self,
1440+
_output_kind: LinkOutputKind,
1441+
_crate_type: CrateType,
1442+
_out_filename: &Path,
1443+
) {
1444+
}
14131445

14141446
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
14151447
self.hint_static();
@@ -1556,7 +1588,12 @@ impl<'a> Linker for AixLinker<'a> {
15561588
&mut self.cmd
15571589
}
15581590

1559-
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
1591+
fn set_output_kind(
1592+
&mut self,
1593+
output_kind: LinkOutputKind,
1594+
_crate_type: CrateType,
1595+
out_filename: &Path,
1596+
) {
15601597
match output_kind {
15611598
LinkOutputKind::DynamicDylib => {
15621599
self.hint_dynamic();
@@ -1763,7 +1800,13 @@ impl<'a> Linker for PtxLinker<'a> {
17631800
&mut self.cmd
17641801
}
17651802

1766-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1803+
fn set_output_kind(
1804+
&mut self,
1805+
_output_kind: LinkOutputKind,
1806+
_crate_type: CrateType,
1807+
_out_filename: &Path,
1808+
) {
1809+
}
17671810

17681811
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
17691812
panic!("staticlibs not supported")
@@ -1829,7 +1872,13 @@ impl<'a> Linker for LlbcLinker<'a> {
18291872
&mut self.cmd
18301873
}
18311874

1832-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1875+
fn set_output_kind(
1876+
&mut self,
1877+
_output_kind: LinkOutputKind,
1878+
_crate_type: CrateType,
1879+
_out_filename: &Path,
1880+
) {
1881+
}
18331882

18341883
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
18351884
panic!("staticlibs not supported")
@@ -1900,7 +1949,13 @@ impl<'a> Linker for BpfLinker<'a> {
19001949
&mut self.cmd
19011950
}
19021951

1903-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1952+
fn set_output_kind(
1953+
&mut self,
1954+
_output_kind: LinkOutputKind,
1955+
_crate_type: CrateType,
1956+
_out_filename: &Path,
1957+
) {
1958+
}
19041959

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

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)