@@ -140,7 +140,12 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
140140// The third parameter is for env vars, used on windows to set up the
141141// path for MSVC to find its DLLs, and gcc to find its bundled
142142// toolchain
143- fn get_linker ( sess : & Session , linker : & Path , flavor : LinkerFlavor ) -> Command {
143+ fn get_linker (
144+ sess : & Session ,
145+ linker : & Path ,
146+ flavor : LinkerFlavor ,
147+ self_contained : bool ,
148+ ) -> Command {
144149 let msvc_tool = windows_registry:: find_tool ( & sess. opts . target_triple . triple ( ) , "link.exe" ) ;
145150
146151 // If our linker looks like a batch script on Windows then to execute this
@@ -199,7 +204,7 @@ fn get_linker(sess: &Session, linker: &Path, flavor: LinkerFlavor) -> Command {
199204
200205 // The compiler's sysroot often has some bundled tools, so add it to the
201206 // PATH for the child.
202- let mut new_path = sess. host_filesearch ( PathKind :: All ) . get_tools_search_paths ( ) ;
207+ let mut new_path = sess. host_filesearch ( PathKind :: All ) . get_tools_search_paths ( self_contained ) ;
203208 let mut msvc_changed_path = false ;
204209 if sess. target . target . options . is_like_msvc {
205210 if let Some ( ref tool) = msvc_tool {
@@ -551,19 +556,25 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
551556 "Linker does not support -static-pie command line option. Retrying with -static instead."
552557 ) ;
553558 // Mirror `add_(pre,post)_link_objects` to replace CRT objects.
554- let fallback = crt_objects_fallback ( sess, crate_type) ;
559+ let self_contained = crt_objects_fallback ( sess, crate_type) ;
555560 let opts = & sess. target . target . options ;
556- let pre_objects =
557- if fallback { & opts. pre_link_objects_fallback } else { & opts. pre_link_objects } ;
558- let post_objects =
559- if fallback { & opts. post_link_objects_fallback } else { & opts. post_link_objects } ;
561+ let pre_objects = if self_contained {
562+ & opts. pre_link_objects_fallback
563+ } else {
564+ & opts. pre_link_objects
565+ } ;
566+ let post_objects = if self_contained {
567+ & opts. post_link_objects_fallback
568+ } else {
569+ & opts. post_link_objects
570+ } ;
560571 let get_objects = |objects : & CrtObjects , kind| {
561572 objects
562573 . get ( & kind)
563574 . iter ( )
564575 . copied ( )
565576 . flatten ( )
566- . map ( |obj| get_object_file_path ( sess, obj) . into_os_string ( ) )
577+ . map ( |obj| get_object_file_path ( sess, obj, self_contained ) . into_os_string ( ) )
567578 . collect :: < Vec < _ > > ( )
568579 } ;
569580 let pre_objects_static_pie = get_objects ( pre_objects, LinkOutputKind :: StaticPicExe ) ;
@@ -1066,9 +1077,11 @@ fn get_crt_libs_path(sess: &Session) -> Option<PathBuf> {
10661077 }
10671078}
10681079
1069- fn get_object_file_path ( sess : & Session , name : & str ) -> PathBuf {
1080+ fn get_object_file_path ( sess : & Session , name : & str , self_contained : bool ) -> PathBuf {
10701081 // prefer system {,dll}crt2.o libs, see get_crt_libs_path comment for more details
1071- if sess. target . target . llvm_target . contains ( "windows-gnu" ) {
1082+ if sess. opts . debugging_opts . link_self_contained . is_none ( )
1083+ && sess. target . target . llvm_target . contains ( "windows-gnu" )
1084+ {
10721085 if let Some ( compiler_libs_path) = get_crt_libs_path ( sess) {
10731086 let file_path = compiler_libs_path. join ( name) ;
10741087 if file_path. exists ( ) {
@@ -1081,9 +1094,12 @@ fn get_object_file_path(sess: &Session, name: &str) -> PathBuf {
10811094 if file_path. exists ( ) {
10821095 return file_path;
10831096 }
1084- let file_path = fs. get_selfcontained_lib_path ( ) . join ( name) ;
1085- if file_path. exists ( ) {
1086- return file_path;
1097+ // Special directory with objects used only in self-contained linkage mode
1098+ if self_contained {
1099+ let file_path = fs. get_self_contained_lib_path ( ) . join ( name) ;
1100+ if file_path. exists ( ) {
1101+ return file_path;
1102+ }
10871103 }
10881104 for search_path in fs. search_paths ( ) {
10891105 let file_path = search_path. dir . join ( name) ;
@@ -1268,6 +1284,10 @@ fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
12681284/// Whether we link to our own CRT objects instead of relying on gcc to pull them.
12691285/// We only provide such support for a very limited number of targets.
12701286fn crt_objects_fallback ( sess : & Session , crate_type : CrateType ) -> bool {
1287+ if let Some ( self_contained) = sess. opts . debugging_opts . link_self_contained {
1288+ return self_contained;
1289+ }
1290+
12711291 match sess. target . target . options . crt_objects_fallback {
12721292 // FIXME: Find a better heuristic for "native musl toolchain is available",
12731293 // based on host and linker path, for example.
@@ -1287,12 +1307,13 @@ fn add_pre_link_objects(
12871307 cmd : & mut dyn Linker ,
12881308 sess : & Session ,
12891309 link_output_kind : LinkOutputKind ,
1290- fallback : bool ,
1310+ self_contained : bool ,
12911311) {
12921312 let opts = & sess. target . target . options ;
1293- let objects = if fallback { & opts. pre_link_objects_fallback } else { & opts. pre_link_objects } ;
1313+ let objects =
1314+ if self_contained { & opts. pre_link_objects_fallback } else { & opts. pre_link_objects } ;
12941315 for obj in objects. get ( & link_output_kind) . iter ( ) . copied ( ) . flatten ( ) {
1295- cmd. add_object ( & get_object_file_path ( sess, obj) ) ;
1316+ cmd. add_object ( & get_object_file_path ( sess, obj, self_contained ) ) ;
12961317 }
12971318}
12981319
@@ -1301,12 +1322,13 @@ fn add_post_link_objects(
13011322 cmd : & mut dyn Linker ,
13021323 sess : & Session ,
13031324 link_output_kind : LinkOutputKind ,
1304- fallback : bool ,
1325+ self_contained : bool ,
13051326) {
13061327 let opts = & sess. target . target . options ;
1307- let objects = if fallback { & opts. post_link_objects_fallback } else { & opts. post_link_objects } ;
1328+ let objects =
1329+ if self_contained { & opts. post_link_objects_fallback } else { & opts. post_link_objects } ;
13081330 for obj in objects. get ( & link_output_kind) . iter ( ) . copied ( ) . flatten ( ) {
1309- cmd. add_object ( & get_object_file_path ( sess, obj) ) ;
1331+ cmd. add_object ( & get_object_file_path ( sess, obj, self_contained ) ) ;
13101332 }
13111333}
13121334
@@ -1468,9 +1490,12 @@ fn link_local_crate_native_libs_and_dependent_crate_libs<'a, B: ArchiveBuilder<'
14681490}
14691491
14701492/// Add sysroot and other globally set directories to the directory search list.
1471- fn add_library_search_dirs ( cmd : & mut dyn Linker , sess : & Session ) {
1493+ fn add_library_search_dirs ( cmd : & mut dyn Linker , sess : & Session , self_contained : bool ) {
14721494 // Prefer system mingw-w64 libs, see get_crt_libs_path comment for more details.
1473- if cfg ! ( windows) && sess. target . target . llvm_target . contains ( "windows-gnu" ) {
1495+ if sess. opts . debugging_opts . link_self_contained . is_none ( )
1496+ && cfg ! ( windows)
1497+ && sess. target . target . llvm_target . contains ( "windows-gnu" )
1498+ {
14741499 if let Some ( compiler_libs_path) = get_crt_libs_path ( sess) {
14751500 cmd. include_path ( & compiler_libs_path) ;
14761501 }
@@ -1481,8 +1506,11 @@ fn add_library_search_dirs(cmd: &mut dyn Linker, sess: &Session) {
14811506 let lib_path = sess. target_filesearch ( PathKind :: All ) . get_lib_path ( ) ;
14821507 cmd. include_path ( & fix_windows_verbatim_for_gcc ( & lib_path) ) ;
14831508
1484- let lib_path = sess. target_filesearch ( PathKind :: All ) . get_selfcontained_lib_path ( ) ;
1485- cmd. include_path ( & fix_windows_verbatim_for_gcc ( & lib_path) ) ;
1509+ // Special directory with libraries used only in self-contained linkage mode
1510+ if self_contained {
1511+ let lib_path = sess. target_filesearch ( PathKind :: All ) . get_self_contained_lib_path ( ) ;
1512+ cmd. include_path ( & fix_windows_verbatim_for_gcc ( & lib_path) ) ;
1513+ }
14861514}
14871515
14881516/// Add options making relocation sections in the produced ELF files read-only
@@ -1545,13 +1573,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
15451573 codegen_results : & CodegenResults ,
15461574 target_cpu : & str ,
15471575) -> Command {
1548- let base_cmd = get_linker ( sess, path, flavor) ;
1576+ let crt_objects_fallback = crt_objects_fallback ( sess, crate_type) ;
1577+ let base_cmd = get_linker ( sess, path, flavor, crt_objects_fallback) ;
15491578 // FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
15501579 // to the linker args construction.
15511580 assert ! ( base_cmd. get_args( ) . is_empty( ) || sess. target. target. target_vendor == "uwp" ) ;
15521581 let cmd = & mut * codegen_results. linker_info . to_linker ( base_cmd, & sess, flavor, target_cpu) ;
15531582 let link_output_kind = link_output_kind ( sess, crate_type) ;
1554- let crt_objects_fallback = crt_objects_fallback ( sess, crate_type) ;
15551583
15561584 // NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
15571585 add_pre_link_args ( cmd, sess, flavor) ;
@@ -1597,7 +1625,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
15971625
15981626 // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
15991627 // FIXME: Order-dependent, at least relatively to other args adding searh directories.
1600- add_library_search_dirs ( cmd, sess) ;
1628+ add_library_search_dirs ( cmd, sess, crt_objects_fallback ) ;
16011629
16021630 // OBJECT-FILES-YES
16031631 add_local_crate_regular_objects ( cmd, codegen_results) ;
0 commit comments