1 use crate::spec::crt_objects::{self, CrtObjectsFallback};
2 use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
3 
opts() -> TargetOptions4 pub fn opts() -> TargetOptions {
5     let mut pre_link_args = LinkArgs::new();
6     pre_link_args.insert(
7         LinkerFlavor::Gcc,
8         vec![
9             // Tell GCC to avoid linker plugins, because we are not bundling
10             // them with Windows installer, and Rust does its own LTO anyways.
11             "-fno-use-linker-plugin".to_string(),
12             // Enable ASLR
13             "-Wl,--dynamicbase".to_string(),
14             // ASLR will rebase it anyway so leaving that option enabled only leads to confusion
15             "-Wl,--disable-auto-image-base".to_string(),
16         ],
17     );
18 
19     let mut late_link_args = LinkArgs::new();
20     let mut late_link_args_dynamic = LinkArgs::new();
21     let mut late_link_args_static = LinkArgs::new();
22     // Order of `late_link_args*` was found through trial and error to work with various
23     // mingw-w64 versions (not tested on the CI). It's expected to change from time to time.
24     let mingw_libs = vec![
25         "-lmsvcrt".to_string(),
26         "-lmingwex".to_string(),
27         "-lmingw32".to_string(),
28         "-lgcc".to_string(), // alas, mingw* libraries above depend on libgcc
29         // mingw's msvcrt is a weird hybrid import library and static library.
30         // And it seems that the linker fails to use import symbols from msvcrt
31         // that are required from functions in msvcrt in certain cases. For example
32         // `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
33         // The library is purposely listed twice to fix that.
34         //
35         // See https://github.com/rust-lang/rust/pull/47483 for some more details.
36         "-lmsvcrt".to_string(),
37         "-luser32".to_string(),
38         "-lkernel32".to_string(),
39     ];
40     late_link_args.insert(LinkerFlavor::Gcc, mingw_libs.clone());
41     late_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), mingw_libs);
42     let dynamic_unwind_libs = vec![
43         // If any of our crates are dynamically linked then we need to use
44         // the shared libgcc_s-dw2-1.dll. This is required to support
45         // unwinding across DLL boundaries.
46         "-lgcc_s".to_string(),
47     ];
48     late_link_args_dynamic.insert(LinkerFlavor::Gcc, dynamic_unwind_libs.clone());
49     late_link_args_dynamic.insert(LinkerFlavor::Lld(LldFlavor::Ld), dynamic_unwind_libs);
50     let static_unwind_libs = vec![
51         // If all of our crates are statically linked then we can get away
52         // with statically linking the libgcc unwinding code. This allows
53         // binaries to be redistributed without the libgcc_s-dw2-1.dll
54         // dependency, but unfortunately break unwinding across DLL
55         // boundaries when unwinding across FFI boundaries.
56         "-lgcc_eh".to_string(),
57         "-l:libpthread.a".to_string(),
58     ];
59     late_link_args_static.insert(LinkerFlavor::Gcc, static_unwind_libs.clone());
60     late_link_args_static.insert(LinkerFlavor::Lld(LldFlavor::Ld), static_unwind_libs);
61 
62     TargetOptions {
63         os: "windows".to_string(),
64         env: "gnu".to_string(),
65         vendor: "pc".to_string(),
66         // FIXME(#13846) this should be enabled for windows
67         function_sections: false,
68         linker: Some("gcc".to_string()),
69         dynamic_linking: true,
70         executables: true,
71         dll_prefix: String::new(),
72         dll_suffix: ".dll".to_string(),
73         exe_suffix: ".exe".to_string(),
74         families: vec!["windows".to_string()],
75         is_like_windows: true,
76         allows_weak_linkage: false,
77         pre_link_args,
78         pre_link_objects: crt_objects::pre_mingw(),
79         post_link_objects: crt_objects::post_mingw(),
80         pre_link_objects_fallback: crt_objects::pre_mingw_fallback(),
81         post_link_objects_fallback: crt_objects::post_mingw_fallback(),
82         crt_objects_fallback: Some(CrtObjectsFallback::Mingw),
83         late_link_args,
84         late_link_args_dynamic,
85         late_link_args_static,
86         abi_return_struct_as_int: true,
87         emit_debug_gdb_scripts: false,
88         requires_uwtable: true,
89         eh_frame_header: false,
90 
91         ..Default::default()
92     }
93 }
94