Skip to content

Commit cfe0cff

Browse files
authored
Rollup merge of #130960 - cuviper:cdylib-soname, r=petrochenkov
Only add an automatic SONAME for Rust dylibs #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

3 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎compiler/rustc_codegen_ssa/src/back/link.rs‎

Lines changed: 1 addition & 1 deletion
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

‎compiler/rustc_codegen_ssa/src/back/linker.rs‎

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ pub(crate) trait Linker {
275275
fnis_cc(&self) -> bool{
276276
false
277277
}
278-
fnset_output_kind(&mutself,output_kind:LinkOutputKind,out_filename:&Path);
278+
fnset_output_kind(
279+
&mutself,
280+
output_kind:LinkOutputKind,
281+
crate_type:CrateType,
282+
out_filename:&Path,
283+
);
279284
fnlink_dylib_by_name(&mutself,_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-
fnbuild_dylib(&mutself,out_filename:&Path){
404+
fnbuild_dylib(&mutself,crate_type:CrateType,out_filename:&Path){
400405
// On mac we need to tell the linker to let this library be rpathed
401406
ifself.sess.target.is_like_osx{
402407
if !self.is_ld{
@@ -427,7 +432,7 @@ impl<'a> GccLinker<'a> {
427432
letmut 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+
}elseif 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-
fnset_output_kind(&mutself,output_kind:LinkOutputKind,out_filename:&Path){
482+
fnset_output_kind(
483+
&mutself,
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
&mutself.cmd
867877
}
868878

869-
fnset_output_kind(&mutself,output_kind:LinkOutputKind,out_filename:&Path){
879+
fnset_output_kind(
880+
&mutself,
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-
fnset_output_kind(&mutself,_output_kind:LinkOutputKind,_out_filename:&Path){}
1142+
fnset_output_kind(
1143+
&mutself,
1144+
_output_kind:LinkOutputKind,
1145+
_crate_type:CrateType,
1146+
_out_filename:&Path,
1147+
){
1148+
}
11281149

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

1276-
fnset_output_kind(&mutself,output_kind:LinkOutputKind,_out_filename:&Path){
1297+
fnset_output_kind(
1298+
&mutself,
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
&mutself.cmd
14231449
}
14241450

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

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

1571-
fnset_output_kind(&mutself,output_kind:LinkOutputKind,out_filename:&Path){
1603+
fnset_output_kind(
1604+
&mutself,
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
&mutself.cmd
17761813
}
17771814

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

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

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

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

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

19171972
fnlink_staticlib_by_name(&mutself,_name:&str,_verbatim:bool,_whole_archive:bool){
19181973
panic!("staticlibs not supported")

‎tests/run-make/dylib-soname/rmake.rs‎

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

99
fnmain(){
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)