]> git.lizzy.rs Git - rust.git/blob - src/librustc_target/spec/windows_base.rs
Auto merge of #70800 - Dylan-DPC:rollup-9jjoptp, r=Dylan-DPC
[rust.git] / src / librustc_target / spec / windows_base.rs
1 use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions};
2 use std::default::Default;
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             // Do not use the standard system startup files or libraries when linking
15             "-nostdlib".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     late_link_args.insert(
23         LinkerFlavor::Gcc,
24         vec![
25             "-lmingwex".to_string(),
26             "-lmingw32".to_string(),
27             "-lmsvcrt".to_string(),
28             // mingw's msvcrt is a weird hybrid import library and static library.
29             // And it seems that the linker fails to use import symbols from msvcrt
30             // that are required from functions in msvcrt in certain cases. For example
31             // `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
32             // Listing the library twice seems to fix that, and seems to also be done
33             // by mingw's gcc (Though not sure if it's done on purpose, or by mistake).
34             //
35             // See https://github.com/rust-lang/rust/pull/47483
36             "-lmsvcrt".to_string(),
37             "-luser32".to_string(),
38             "-lkernel32".to_string(),
39         ],
40     );
41     late_link_args_dynamic.insert(
42         LinkerFlavor::Gcc,
43         vec![
44             // If any of our crates are dynamically linked then we need to use
45             // the shared libgcc_s-dw2-1.dll. This is required to support
46             // unwinding across DLL boundaries.
47             "-lgcc_s".to_string(),
48             "-lgcc".to_string(),
49             "-lkernel32".to_string(),
50         ],
51     );
52     late_link_args_static.insert(
53         LinkerFlavor::Gcc,
54         vec![
55             // If all of our crates are statically linked then we can get away
56             // with statically linking the libgcc unwinding code. This allows
57             // binaries to be redistributed without the libgcc_s-dw2-1.dll
58             // dependency, but unfortunately break unwinding across DLL
59             // boundaries when unwinding across FFI boundaries.
60             "-lgcc".to_string(),
61             "-lgcc_eh".to_string(),
62             "-lpthread".to_string(),
63             // libpthread depends on libmsvcrt, so we need to link it *again*.
64             "-lmsvcrt".to_string(),
65             "-lkernel32".to_string(),
66         ],
67     );
68
69     TargetOptions {
70         // FIXME(#13846) this should be enabled for windows
71         function_sections: false,
72         linker: Some("gcc".to_string()),
73         dynamic_linking: true,
74         executables: true,
75         dll_prefix: String::new(),
76         dll_suffix: ".dll".to_string(),
77         exe_suffix: ".exe".to_string(),
78         staticlib_prefix: String::new(),
79         staticlib_suffix: ".lib".to_string(),
80         target_family: Some("windows".to_string()),
81         is_like_windows: true,
82         allows_weak_linkage: false,
83         pre_link_args,
84         pre_link_objects_exe: vec![
85             "crt2.o".to_string(),    // mingw C runtime initialization for executables
86             "rsbegin.o".to_string(), // Rust compiler runtime initialization, see rsbegin.rs
87         ],
88         pre_link_objects_dll: vec![
89             "dllcrt2.o".to_string(), // mingw C runtime initialization for dlls
90             "rsbegin.o".to_string(),
91         ],
92         late_link_args,
93         late_link_args_dynamic,
94         late_link_args_static,
95         post_link_objects: vec!["rsend.o".to_string()],
96         abi_return_struct_as_int: true,
97         emit_debug_gdb_scripts: false,
98         requires_uwtable: true,
99
100         ..Default::default()
101     }
102 }