diff --git a/compiler/rustc_target/src/spec/windows_gnu_base.rs b/compiler/rustc_target/src/spec/windows_gnu_base.rs index b01a17fb80eba..35a52896f6fa6 100644 --- a/compiler/rustc_target/src/spec/windows_gnu_base.rs +++ b/compiler/rustc_target/src/spec/windows_gnu_base.rs @@ -16,13 +16,34 @@ pub fn opts() -> TargetOptions { ], ); + let mut late_link_args = LinkArgs::new(); let mut late_link_args_dynamic = LinkArgs::new(); let mut late_link_args_static = LinkArgs::new(); + // Order of `late_link_args*` was found through trial and error to work with various + // mingw-w64 versions (not tested on the CI). It's expected to change from time to time. + let mingw_libs = vec![ + "-lmsvcrt".to_string(), + "-lmingwex".to_string(), + "-lmingw32".to_string(), + "-lgcc".to_string(), // alas, mingw* libraries above depend on libgcc + // mingw's msvcrt is a weird hybrid import library and static library. + // And it seems that the linker fails to use import symbols from msvcrt + // that are required from functions in msvcrt in certain cases. For example + // `_fmode` that is used by an implementation of `__p__fmode` in x86_64. + // The library is purposely listed twice to fix that. + // + // See https://github.com/rust-lang/rust/pull/47483 for some more details. + "-lmsvcrt".to_string(), + "-luser32".to_string(), + "-lkernel32".to_string(), + ]; + late_link_args.insert(LinkerFlavor::Gcc, mingw_libs.clone()); + late_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), mingw_libs); let dynamic_unwind_libs = vec![ // If any of our crates are dynamically linked then we need to use // the shared libgcc_s-dw2-1.dll. This is required to support // unwinding across DLL boundaries. - "-l:libunwind.dll.a".to_string(), + "-lgcc_s".to_string(), ]; late_link_args_dynamic.insert(LinkerFlavor::Gcc, dynamic_unwind_libs.clone()); late_link_args_dynamic.insert(LinkerFlavor::Lld(LldFlavor::Ld), dynamic_unwind_libs); @@ -32,7 +53,8 @@ pub fn opts() -> TargetOptions { // binaries to be redistributed without the libgcc_s-dw2-1.dll // dependency, but unfortunately break unwinding across DLL // boundaries when unwinding across FFI boundaries. - "-l:libunwind.a".to_string(), + "-lgcc_eh".to_string(), + "-l:libpthread.a".to_string(), ]; late_link_args_static.insert(LinkerFlavor::Gcc, static_unwind_libs.clone()); late_link_args_static.insert(LinkerFlavor::Lld(LldFlavor::Ld), static_unwind_libs); @@ -42,8 +64,7 @@ pub fn opts() -> TargetOptions { env: "gnu".to_string(), vendor: "pc".to_string(), // FIXME(#13846) this should be enabled for windows - function_sections: true, - no_default_libraries: false, + function_sections: false, linker: Some("gcc".to_string()), dynamic_linking: true, executables: true, @@ -59,6 +80,7 @@ pub fn opts() -> TargetOptions { pre_link_objects_fallback: crt_objects::pre_mingw_fallback(), post_link_objects_fallback: crt_objects::post_mingw_fallback(), crt_objects_fallback: Some(CrtObjectsFallback::Mingw), + late_link_args, late_link_args_dynamic, late_link_args_static, abi_return_struct_as_int: true, diff --git a/missing-libs-hack/libgcc.a b/missing-libs-hack/libgcc.a deleted file mode 100644 index b2bc12372f5a8..0000000000000 Binary files a/missing-libs-hack/libgcc.a and /dev/null differ diff --git a/missing-libs-hack/libgcc_eh.a b/missing-libs-hack/libgcc_eh.a deleted file mode 100644 index fa89eccc23b0a..0000000000000 Binary files a/missing-libs-hack/libgcc_eh.a and /dev/null differ diff --git a/missing-libs-hack/libgcc_s.a b/missing-libs-hack/libgcc_s.a deleted file mode 100644 index 8ead01a4c4d6c..0000000000000 Binary files a/missing-libs-hack/libgcc_s.a and /dev/null differ diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index 3bb6df8484f3d..c2f7cf83ee16f 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -470,11 +470,12 @@ def download_toolchain(self, stage0=True, rustc_channel=None): with output(self.rustc_stamp(stage0)) as rust_stamp: rust_stamp.write(key) - gcc_libs_hack = '{}/missing-libs-hack'.format(self.rust_root) - gcc_libs_hack_dest = '{}/rustlib/{}/lib'.format(lib_dir, self.build) - shutil.copy(gcc_libs_hack + '/libgcc.a', gcc_libs_hack_dest) - shutil.copy(gcc_libs_hack + '/libgcc_eh.a', gcc_libs_hack_dest) - shutil.copy(gcc_libs_hack + '/libgcc_s.a', gcc_libs_hack_dest) + #gcc_libs_hack = '{}/missing-libs-hack'.format(self.rust_root) + #gcc_libs_hack_dest = '{}/rustlib/{}/lib'.format(lib_dir, self.build) + #shutil.copy(gcc_libs_hack + '/libgcc.a', gcc_libs_hack_dest) + #shutil.copy(gcc_libs_hack + '/libgcc_eh.a', gcc_libs_hack_dest) + #shutil.copy(gcc_libs_hack + '/libgcc_s.a', gcc_libs_hack_dest) + #shutil.copy(gcc_libs_hack + '/libstdc++.a', gcc_libs_hack_dest) if self.rustfmt() and self.rustfmt().startswith(bin_root) and ( not os.path.exists(self.rustfmt()) diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index bd93b07d7b80d..b425dcbf759f4 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -184,7 +184,7 @@ fn make_win_dist( "libm.a", "libmingw32.a", "libmingwex.a", - //"libstdc++.a", + "libstdc++.a", "libiconv.a", "libmoldname.a", "libpthread.a", diff --git a/x86_64-pc-windows-gnu.json b/x86_64-pc-windows-gnu.json new file mode 100644 index 0000000000000..df082f7cfb9f5 --- /dev/null +++ b/x86_64-pc-windows-gnu.json @@ -0,0 +1,137 @@ +{ + "abi-return-struct-as-int": true, + "allows-weak-linkage": false, + "arch": "x86_64", + "crt-objects-fallback": "mingw", + "data-layout": "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128", + "dll-prefix": "", + "dll-suffix": ".dll", + "dynamic-linking": true, + "eh-frame-header": false, + "emit-debug-gdb-scripts": false, + "env": "gnu", + "exe-suffix": ".exe", + "executables": true, + "is-builtin": false, + "is-like-windows": true, + "late-link-args-dynamic": { + "gcc": [ + "-l:libunwind.dll.a" + ], + "ld.lld": [ + "-l:libunwind.dll.a" + ] + }, + "late-link-args-static": { + "gcc": [ + "-l:libunwind.a" + ], + "ld.lld": [ + "-l:libunwind.a" + ] + }, + "linker": "x86_64-w64-mingw32-clang", + "llvm-target": "x86_64-pc-windows-gnu", + "max-atomic-width": 64, + "no-default-libraries": false, + "os": "windows", + "post-link-objects": { + "dynamic-dylib": [ + "rsend.o" + ], + "dynamic-nopic-exe": [ + "rsend.o" + ], + "dynamic-pic-exe": [ + "rsend.o" + ], + "static-dylib": [ + "rsend.o" + ], + "static-nopic-exe": [ + "rsend.o" + ], + "static-pic-exe": [ + "rsend.o" + ] + }, + "post-link-objects-fallback": { + "dynamic-dylib": [ + "rsend.o" + ], + "dynamic-nopic-exe": [ + "rsend.o" + ], + "dynamic-pic-exe": [ + "rsend.o" + ], + "static-dylib": [ + "rsend.o" + ], + "static-nopic-exe": [ + "rsend.o" + ], + "static-pic-exe": [ + "rsend.o" + ] + }, + "pre-link-args": { + "gcc": [ + "-fno-use-linker-plugin", + "-Wl,--dynamicbase", + "-Wl,--disable-auto-image-base" + ] + }, + "pre-link-objects": { + "dynamic-dylib": [ + "rsbegin.o" + ], + "dynamic-nopic-exe": [ + "rsbegin.o" + ], + "dynamic-pic-exe": [ + "rsbegin.o" + ], + "static-dylib": [ + "rsbegin.o" + ], + "static-nopic-exe": [ + "rsbegin.o" + ], + "static-pic-exe": [ + "rsbegin.o" + ] + }, + "pre-link-objects-fallback": { + "dynamic-dylib": [ + "dllcrt2.o", + "rsbegin.o" + ], + "dynamic-nopic-exe": [ + "crt2.o", + "rsbegin.o" + ], + "dynamic-pic-exe": [ + "crt2.o", + "rsbegin.o" + ], + "static-dylib": [ + "dllcrt2.o", + "rsbegin.o" + ], + "static-nopic-exe": [ + "crt2.o", + "rsbegin.o" + ], + "static-pic-exe": [ + "crt2.o", + "rsbegin.o" + ] + }, + "requires-uwtable": true, + "target-family": [ + "windows" + ], + "target-pointer-width": "64", + "vendor": "pc" +}