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