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