@@ -943,8 +943,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
943
943
Linkage :: NotLinked |
944
944
Linkage :: IncludedFromDylib => { }
945
945
Linkage :: Static => {
946
- add_static_crate ( cmd, sess, tmpdir, crate_type,
947
- & src. rlib . unwrap ( ) . 0 , sess. cstore . is_no_builtins ( cnum) )
946
+ add_static_crate ( cmd, sess, tmpdir, crate_type, cnum) ;
948
947
}
949
948
Linkage :: Dynamic => {
950
949
add_dynamic_crate ( cmd, sess, & src. dylib . unwrap ( ) . 0 )
@@ -956,9 +955,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
956
955
// was already "included" in a dylib (e.g. `libstd` when `-C prefer-dynamic`
957
956
// is used)
958
957
if let Some ( cnum) = compiler_builtins {
959
- let src = sess. cstore . used_crate_source ( cnum) ;
960
- add_static_crate ( cmd, sess, tmpdir, crate_type,
961
- & src. rlib . unwrap ( ) . 0 , sess. cstore . is_no_builtins ( cnum) ) ;
958
+ add_static_crate ( cmd, sess, tmpdir, crate_type, cnum) ;
962
959
}
963
960
964
961
// Converts a library file-stem into a cc -l argument
@@ -1006,8 +1003,9 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
1006
1003
sess : & Session ,
1007
1004
tmpdir : & Path ,
1008
1005
crate_type : config:: CrateType ,
1009
- cratepath : & Path ,
1010
- is_a_no_builtins_crate : bool ) {
1006
+ cnum : ast:: CrateNum ) {
1007
+ let src = sess. cstore . used_crate_source ( cnum) ;
1008
+ let cratepath = & src. rlib . unwrap ( ) . 0 ;
1011
1009
if !sess. lto ( ) && crate_type != config:: CrateTypeDylib {
1012
1010
cmd. link_rlib ( & fix_windows_verbatim_for_gcc ( cratepath) ) ;
1013
1011
return
@@ -1031,7 +1029,13 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
1031
1029
}
1032
1030
let canonical = f. replace ( "-" , "_" ) ;
1033
1031
let canonical_name = name. replace ( "-" , "_" ) ;
1034
- if sess. lto ( ) && !is_a_no_builtins_crate &&
1032
+
1033
+ // If we're performing LTO and this is a rust-generated object
1034
+ // file, then we don't need the object file as it's part of the
1035
+ // LTO module. Note that `#![no_builtins]` is excluded from LTO,
1036
+ // though, so we let that object file slide.
1037
+ if sess. lto ( ) &&
1038
+ !sess. cstore . is_no_builtins ( cnum) &&
1035
1039
canonical. starts_with ( & canonical_name) &&
1036
1040
canonical. ends_with ( ".o" ) {
1037
1041
let num = & f[ name. len ( ) ..f. len ( ) - 2 ] ;
@@ -1043,13 +1047,23 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
1043
1047
any_objects = true ;
1044
1048
}
1045
1049
1046
- if any_objects {
1047
- archive. build ( ) ;
1048
- if crate_type == config:: CrateTypeDylib {
1049
- cmd. link_whole_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
1050
- } else {
1051
- cmd. link_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
1052
- }
1050
+ if !any_objects {
1051
+ return
1052
+ }
1053
+ archive. build ( ) ;
1054
+
1055
+ // If we're creating a dylib, then we need to include the
1056
+ // whole of each object in our archive into that artifact. This is
1057
+ // because a `dylib` can be reused as an intermediate artifact.
1058
+ //
1059
+ // Note, though, that we don't want to include the whole of a
1060
+ // compiler-builtins crate (e.g. compiler-rt) because it'll get
1061
+ // repeatedly linked anyway.
1062
+ if crate_type == config:: CrateTypeDylib &&
1063
+ !sess. cstore . is_compiler_builtins ( cnum) {
1064
+ cmd. link_whole_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
1065
+ } else {
1066
+ cmd. link_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
1053
1067
}
1054
1068
} ) ;
1055
1069
}
0 commit comments