|
| 1 | +use crate::spec::crt_objects; |
1 | 2 | use crate::spec::LinkSelfContainedDefault;
|
2 |
| -use crate::spec::{add_link_args, crt_objects}; |
3 | 3 | use crate::spec::{cvs, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
|
4 | 4 | use std::borrow::Cow;
|
5 | 5 |
|
6 | 6 | pub fn opts() -> TargetOptions {
|
7 |
| - let mut pre_link_args = TargetOptions::link_args( |
8 |
| - LinkerFlavor::Gnu(Cc::No, Lld::No), |
9 |
| - &[ |
10 |
| - // Enable ASLR |
11 |
| - "--dynamicbase", |
12 |
| - // ASLR will rebase it anyway so leaving that option enabled only leads to confusion |
13 |
| - "--disable-auto-image-base", |
14 |
| - ], |
15 |
| - ); |
16 |
| - add_link_args( |
17 |
| - &mut pre_link_args, |
18 |
| - LinkerFlavor::Gnu(Cc::Yes, Lld::No), |
19 |
| - &[ |
20 |
| - // Tell GCC to avoid linker plugins, because we are not bundling |
21 |
| - // them with Windows installer, and Rust does its own LTO anyways. |
22 |
| - "-fno-use-linker-plugin", |
23 |
| - "-Wl,--dynamicbase", |
24 |
| - "-Wl,--disable-auto-image-base", |
25 |
| - ], |
26 |
| - ); |
| 7 | + let pre_link_args = TargetOptions::link_args_list(&[ |
| 8 | + ( |
| 9 | + LinkerFlavor::Gnu(Cc::No, Lld::No), |
| 10 | + &[ |
| 11 | + // Enable ASLR |
| 12 | + "--dynamicbase", |
| 13 | + // ASLR will rebase it anyway so leaving that option enabled only leads to confusion |
| 14 | + "--disable-auto-image-base", |
| 15 | + ], |
| 16 | + ), |
| 17 | + ( |
| 18 | + LinkerFlavor::Gnu(Cc::Yes, Lld::No), |
| 19 | + &[ |
| 20 | + // Tell GCC to avoid linker plugins, because we are not bundling |
| 21 | + // them with Windows installer, and Rust does its own LTO anyways. |
| 22 | + "-fno-use-linker-plugin", |
| 23 | + "-Wl,--dynamicbase", |
| 24 | + "-Wl,--disable-auto-image-base", |
| 25 | + ], |
| 26 | + ), |
| 27 | + ]); |
27 | 28 |
|
28 |
| - // Order of `late_link_args*` was found through trial and error to work with various |
29 |
| - // mingw-w64 versions (not tested on the CI). It's expected to change from time to time. |
30 |
| - let mingw_libs = &[ |
31 |
| - "-lmsvcrt", |
32 |
| - "-lmingwex", |
33 |
| - "-lmingw32", |
34 |
| - "-lgcc", // alas, mingw* libraries above depend on libgcc |
35 |
| - // mingw's msvcrt is a weird hybrid import library and static library. |
36 |
| - // And it seems that the linker fails to use import symbols from msvcrt |
37 |
| - // that are required from functions in msvcrt in certain cases. For example |
38 |
| - // `_fmode` that is used by an implementation of `__p__fmode` in x86_64. |
39 |
| - // The library is purposely listed twice to fix that. |
40 |
| - // |
41 |
| - // See https://github.com/rust-lang/rust/pull/47483 for some more details. |
42 |
| - "-lmsvcrt", |
43 |
| - // Math functions missing in MSVCRT (they are present in UCRT) require |
44 |
| - // this dependency cycle: `libmingwex.a` -> `libmsvcrt.a` -> `libmingwex.a`. |
45 |
| - "-lmingwex", |
46 |
| - "-luser32", |
47 |
| - "-lkernel32", |
48 |
| - ]; |
49 |
| - let mut late_link_args = |
50 |
| - TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), mingw_libs); |
51 |
| - add_link_args(&mut late_link_args, LinkerFlavor::Gnu(Cc::Yes, Lld::No), mingw_libs); |
| 29 | + let late_link_args = { |
| 30 | + // Order of `late_link_args*` was found through trial and error to work with various |
| 31 | + // mingw-w64 versions (not tested on the CI). It's expected to change from time to time. |
| 32 | + const MINGW_LIBS: &[&str] = &[ |
| 33 | + "-lmsvcrt", |
| 34 | + "-lmingwex", |
| 35 | + "-lmingw32", |
| 36 | + "-lgcc", // alas, mingw* libraries above depend on libgcc |
| 37 | + // mingw's msvcrt is a weird hybrid import library and static library. |
| 38 | + // And it seems that the linker fails to use import symbols from msvcrt |
| 39 | + // that are required from functions in msvcrt in certain cases. For example |
| 40 | + // `_fmode` that is used by an implementation of `__p__fmode` in x86_64. |
| 41 | + // The library is purposely listed twice to fix that. |
| 42 | + // |
| 43 | + // See https://github.com/rust-lang/rust/pull/47483 for some more details. |
| 44 | + "-lmsvcrt", |
| 45 | + // Math functions missing in MSVCRT (they are present in UCRT) require |
| 46 | + // this dependency cycle: `libmingwex.a` -> `libmsvcrt.a` -> `libmingwex.a`. |
| 47 | + "-lmingwex", |
| 48 | + "-luser32", |
| 49 | + "-lkernel32", |
| 50 | + ]; |
| 51 | + TargetOptions::link_args_list(&[ |
| 52 | + (LinkerFlavor::Gnu(Cc::No, Lld::No), MINGW_LIBS), |
| 53 | + (LinkerFlavor::Gnu(Cc::Yes, Lld::No), MINGW_LIBS), |
| 54 | + ]) |
| 55 | + }; |
52 | 56 | // If any of our crates are dynamically linked then we need to use
|
53 | 57 | // the shared libgcc_s-dw2-1.dll. This is required to support
|
54 | 58 | // unwinding across DLL boundaries.
|
55 |
| - let dynamic_unwind_libs = &["-lgcc_s"]; |
56 |
| - let mut late_link_args_dynamic = |
57 |
| - TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), dynamic_unwind_libs); |
58 |
| - add_link_args( |
59 |
| - &mut late_link_args_dynamic, |
60 |
| - LinkerFlavor::Gnu(Cc::Yes, Lld::No), |
61 |
| - dynamic_unwind_libs, |
62 |
| - ); |
| 59 | + let late_link_args_dynamic = { |
| 60 | + const DYNAMIC_UNWIND_LIBS: &[&str] = &["-lgcc_s"]; |
| 61 | + TargetOptions::link_args_list(&[ |
| 62 | + (LinkerFlavor::Gnu(Cc::No, Lld::No), DYNAMIC_UNWIND_LIBS), |
| 63 | + (LinkerFlavor::Gnu(Cc::Yes, Lld::No), DYNAMIC_UNWIND_LIBS), |
| 64 | + ]) |
| 65 | + }; |
63 | 66 | // If all of our crates are statically linked then we can get away
|
64 | 67 | // with statically linking the libgcc unwinding code. This allows
|
65 | 68 | // binaries to be redistributed without the libgcc_s-dw2-1.dll
|
66 | 69 | // dependency, but unfortunately break unwinding across DLL
|
67 | 70 | // boundaries when unwinding across FFI boundaries.
|
68 |
| - let static_unwind_libs = &["-lgcc_eh", "-l:libpthread.a"]; |
69 |
| - let mut late_link_args_static = |
70 |
| - TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), static_unwind_libs); |
71 |
| - add_link_args( |
72 |
| - &mut late_link_args_static, |
73 |
| - LinkerFlavor::Gnu(Cc::Yes, Lld::No), |
74 |
| - static_unwind_libs, |
75 |
| - ); |
| 71 | + let late_link_args_static = { |
| 72 | + const STATIC_UNWIND_LIBS: &[&str] = &["-lgcc_eh", "-l:libpthread.a"]; |
| 73 | + TargetOptions::link_args_list(&[ |
| 74 | + (LinkerFlavor::Gnu(Cc::No, Lld::No), STATIC_UNWIND_LIBS), |
| 75 | + (LinkerFlavor::Gnu(Cc::Yes, Lld::No), STATIC_UNWIND_LIBS), |
| 76 | + ]) |
| 77 | + }; |
76 | 78 |
|
77 | 79 | TargetOptions {
|
78 | 80 | os: "windows".into(),
|
|
0 commit comments