Skip to content

Commit 5addb13

Browse files
committed
Auto merge of #75483 - mati865:mingw-lld-flags, r=petrochenkov
Add LLD flags for MinGW Tested locally and this now works: - `RUSTFLAGS="-Zlink-self-contained=yes -Clinker=rust-lld" cargo b` - `RUSTFLAGS="-Zlink-self-contained=no -Clinker=rust-lld -Zpre-link-arg=-Ld:/msys64/mingw64/x86_64-w64-mingw32/lib -Zpre-link-arg=-Ld:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0 -Zpre-link-arg=crt2.o" cargo b` This is "harmless" part of the changes to make possible linking with bare LLD with windows-gnu target. More debatable changes should follow in next PRs soon.
2 parents b9db927 + c75f7f9 commit 5addb13

6 files changed

+71
-67
lines changed

src/librustc_target/spec/i686_pc_windows_gnu.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use crate::spec::{LinkerFlavor, Target, TargetResult};
1+
use crate::spec::{LinkerFlavor, LldFlavor, Target, TargetResult};
22

33
pub fn target() -> TargetResult {
44
let mut base = super::windows_gnu_base::opts();
55
base.cpu = "pentium4".to_string();
6+
base.pre_link_args
7+
.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".to_string(), "i386pe".to_string()]);
68
base.max_atomic_width = Some(64);
79
base.eliminate_frame_pointer = false; // Required for backtraces
810
base.linker = Some("i686-w64-mingw32-gcc".to_string());

src/librustc_target/spec/i686_uwp_windows_gnu.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use crate::spec::{LinkerFlavor, Target, TargetResult};
1+
use crate::spec::{LinkerFlavor, LldFlavor, Target, TargetResult};
22

33
pub fn target() -> TargetResult {
44
let mut base = super::windows_uwp_gnu_base::opts();
55
base.cpu = "pentium4".to_string();
6+
base.pre_link_args
7+
.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".to_string(), "i386pe".to_string()]);
68
base.max_atomic_width = Some(64);
79
base.eliminate_frame_pointer = false; // Required for backtraces
810

src/librustc_target/spec/windows_gnu_base.rs

+43-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::spec::crt_objects::{self, CrtObjectsFallback};
2-
use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions};
2+
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
33

44
pub fn opts() -> TargetOptions {
55
let mut pre_link_args = LinkArgs::new();
@@ -19,51 +19,48 @@ pub fn opts() -> TargetOptions {
1919
let mut late_link_args_static = LinkArgs::new();
2020
// Order of `late_link_args*` was found through trial and error to work with various
2121
// mingw-w64 versions (not tested on the CI). It's expected to change from time to time.
22-
late_link_args.insert(
23-
LinkerFlavor::Gcc,
24-
vec![
25-
"-lmsvcrt".to_string(),
26-
"-lmingwex".to_string(),
27-
"-lmingw32".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-
// The library is purposely listed twice to fix that.
33-
//
34-
// See https://github.com/rust-lang/rust/pull/47483 for some more details.
35-
"-lmsvcrt".to_string(),
36-
"-luser32".to_string(),
37-
"-lkernel32".to_string(),
38-
],
39-
);
40-
late_link_args_dynamic.insert(
41-
LinkerFlavor::Gcc,
42-
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-
"-lgcc".to_string(),
48-
"-lkernel32".to_string(),
49-
],
50-
);
51-
late_link_args_static.insert(
52-
LinkerFlavor::Gcc,
53-
vec![
54-
// If all of our crates are statically linked then we can get away
55-
// with statically linking the libgcc unwinding code. This allows
56-
// binaries to be redistributed without the libgcc_s-dw2-1.dll
57-
// dependency, but unfortunately break unwinding across DLL
58-
// boundaries when unwinding across FFI boundaries.
59-
"-lgcc_eh".to_string(),
60-
"-l:libpthread.a".to_string(),
61-
"-lgcc".to_string(),
62-
// libpthread depends on libmsvcrt, so we need to link it *again*.
63-
"-lmsvcrt".to_string(),
64-
"-lkernel32".to_string(),
65-
],
66-
);
22+
let mingw_libs = vec![
23+
"-lmsvcrt".to_string(),
24+
"-lmingwex".to_string(),
25+
"-lmingw32".to_string(),
26+
// mingw's msvcrt is a weird hybrid import library and static library.
27+
// And it seems that the linker fails to use import symbols from msvcrt
28+
// that are required from functions in msvcrt in certain cases. For example
29+
// `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
30+
// The library is purposely listed twice to fix that.
31+
//
32+
// See https://github.com/rust-lang/rust/pull/47483 for some more details.
33+
"-lmsvcrt".to_string(),
34+
"-luser32".to_string(),
35+
"-lkernel32".to_string(),
36+
];
37+
late_link_args.insert(LinkerFlavor::Gcc, mingw_libs.clone());
38+
late_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), mingw_libs);
39+
let dynamic_unwind_libs = vec![
40+
// If any of our crates are dynamically linked then we need to use
41+
// the shared libgcc_s-dw2-1.dll. This is required to support
42+
// unwinding across DLL boundaries.
43+
"-lgcc_s".to_string(),
44+
"-lgcc".to_string(),
45+
"-lkernel32".to_string(),
46+
];
47+
late_link_args_dynamic.insert(LinkerFlavor::Gcc, dynamic_unwind_libs.clone());
48+
late_link_args_dynamic.insert(LinkerFlavor::Lld(LldFlavor::Ld), dynamic_unwind_libs);
49+
let static_unwind_libs = vec![
50+
// If all of our crates are statically linked then we can get away
51+
// with statically linking the libgcc unwinding code. This allows
52+
// binaries to be redistributed without the libgcc_s-dw2-1.dll
53+
// dependency, but unfortunately break unwinding across DLL
54+
// boundaries when unwinding across FFI boundaries.
55+
"-lgcc_eh".to_string(),
56+
"-l:libpthread.a".to_string(),
57+
"-lgcc".to_string(),
58+
// libpthread depends on libmsvcrt, so we need to link it *again*.
59+
"-lmsvcrt".to_string(),
60+
"-lkernel32".to_string(),
61+
];
62+
late_link_args_static.insert(LinkerFlavor::Gcc, static_unwind_libs.clone());
63+
late_link_args_static.insert(LinkerFlavor::Lld(LldFlavor::Ld), static_unwind_libs);
6764

6865
TargetOptions {
6966
// FIXME(#13846) this should be enabled for windows

src/librustc_target/spec/windows_uwp_gnu_base.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions};
1+
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
22

33
pub fn opts() -> TargetOptions {
44
let base = super::windows_gnu_base::opts();
@@ -8,22 +8,21 @@ pub fn opts() -> TargetOptions {
88
let mut late_link_args = LinkArgs::new();
99
let late_link_args_dynamic = LinkArgs::new();
1010
let late_link_args_static = LinkArgs::new();
11-
late_link_args.insert(
12-
LinkerFlavor::Gcc,
13-
vec![
14-
//"-lwinstorecompat".to_string(),
15-
//"-lmingwex".to_string(),
16-
//"-lwinstorecompat".to_string(),
17-
"-lwinstorecompat".to_string(),
18-
"-lruntimeobject".to_string(),
19-
"-lsynchronization".to_string(),
20-
"-lvcruntime140_app".to_string(),
21-
"-lucrt".to_string(),
22-
"-lwindowsapp".to_string(),
23-
"-lmingwex".to_string(),
24-
"-lmingw32".to_string(),
25-
],
26-
);
11+
let mingw_libs = vec![
12+
//"-lwinstorecompat".to_string(),
13+
//"-lmingwex".to_string(),
14+
//"-lwinstorecompat".to_string(),
15+
"-lwinstorecompat".to_string(),
16+
"-lruntimeobject".to_string(),
17+
"-lsynchronization".to_string(),
18+
"-lvcruntime140_app".to_string(),
19+
"-lucrt".to_string(),
20+
"-lwindowsapp".to_string(),
21+
"-lmingwex".to_string(),
22+
"-lmingw32".to_string(),
23+
];
24+
late_link_args.insert(LinkerFlavor::Gcc, mingw_libs.clone());
25+
late_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), mingw_libs.clone());
2726

2827
TargetOptions {
2928
executables: false,

src/librustc_target/spec/x86_64_pc_windows_gnu.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use crate::spec::{LinkerFlavor, Target, TargetResult};
1+
use crate::spec::{LinkerFlavor, LldFlavor, Target, TargetResult};
22

33
pub fn target() -> TargetResult {
44
let mut base = super::windows_gnu_base::opts();
55
base.cpu = "x86-64".to_string();
66
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
7+
base.pre_link_args
8+
.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".to_string(), "i386pep".to_string()]);
79
base.max_atomic_width = Some(64);
810
base.linker = Some("x86_64-w64-mingw32-gcc".to_string());
911

src/librustc_target/spec/x86_64_uwp_windows_gnu.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use crate::spec::{LinkerFlavor, Target, TargetResult};
1+
use crate::spec::{LinkerFlavor, LldFlavor, Target, TargetResult};
22

33
pub fn target() -> TargetResult {
44
let mut base = super::windows_uwp_gnu_base::opts();
55
base.cpu = "x86-64".to_string();
66
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
7+
base.pre_link_args
8+
.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".to_string(), "i386pep".to_string()]);
79
base.max_atomic_width = Some(64);
810

911
Ok(Target {

0 commit comments

Comments
 (0)