]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/spec/crt_objects.rs
Auto merge of #77618 - fusion-engineering-forks:windows-parker, r=Amanieu
[rust.git] / compiler / rustc_target / src / spec / crt_objects.rs
1 //! Object files providing support for basic runtime facilities and added to the produced binaries
2 //! at the start and at the end of linking.
3 //!
4 //! Table of CRT objects for popular toolchains.
5 //! The `crtx` ones are generally distributed with libc and the `begin/end` ones with gcc.
6 //! See <https://dev.gentoo.org/~vapier/crt.txt> for some more details.
7 //!
8 //! | Pre-link CRT objects | glibc                  | musl                   | bionic           | mingw             | wasi |
9 //! |----------------------|------------------------|------------------------|------------------|-------------------|------|
10 //! | dynamic-nopic-exe    | crt1, crti, crtbegin   | crt1, crti, crtbegin   | crtbegin_dynamic | crt2, crtbegin    | crt1 |
11 //! | dynamic-pic-exe      | Scrt1, crti, crtbeginS | Scrt1, crti, crtbeginS | crtbegin_dynamic | crt2, crtbegin    | crt1 |
12 //! | static-nopic-exe     | crt1, crti, crtbeginT  | crt1, crti, crtbegin   | crtbegin_static  | crt2, crtbegin    | crt1 |
13 //! | static-pic-exe       | rcrt1, crti, crtbeginS | rcrt1, crti, crtbeginS | crtbegin_dynamic | crt2, crtbegin    | crt1 |
14 //! | dynamic-dylib        | crti, crtbeginS        | crti, crtbeginS        | crtbegin_so      | dllcrt2, crtbegin | -    |
15 //! | static-dylib (gcc)   | crti, crtbeginT        | crti, crtbeginS        | crtbegin_so      | dllcrt2, crtbegin | -    |
16 //! | static-dylib (clang) | crti, crtbeginT        | N/A                    | crtbegin_static  | dllcrt2, crtbegin | -    |
17 //!
18 //! | Post-link CRT objects | glibc         | musl          | bionic         | mingw  | wasi |
19 //! |-----------------------|---------------|---------------|----------------|--------|------|
20 //! | dynamic-nopic-exe     | crtend, crtn  | crtend, crtn  | crtend_android | crtend | -    |
21 //! | dynamic-pic-exe       | crtendS, crtn | crtendS, crtn | crtend_android | crtend | -    |
22 //! | static-nopic-exe      | crtend, crtn  | crtend, crtn  | crtend_android | crtend | -    |
23 //! | static-pic-exe        | crtendS, crtn | crtendS, crtn | crtend_android | crtend | -    |
24 //! | dynamic-dylib         | crtendS, crtn | crtendS, crtn | crtend_so      | crtend | -    |
25 //! | static-dylib (gcc)    | crtend, crtn  | crtendS, crtn | crtend_so      | crtend | -    |
26 //! | static-dylib (clang)  | crtendS, crtn | N/A           | crtend_so      | crtend | -    |
27 //!
28 //! Use cases for rustc linking the CRT objects explicitly:
29 //!     - rustc needs to add its own Rust-specific objects (mingw is the example)
30 //!     - gcc wrapper cannot be used for some reason and linker like ld or lld is used directly.
31 //!     - gcc wrapper pulls wrong CRT objects (e.g. from glibc when we are targeting musl).
32 //!
33 //! In general it is preferable to rely on the target's native toolchain to pull the objects.
34 //! However, for some targets (musl, mingw) rustc historically provides a more self-contained
35 //! installation not requiring users to install the native target's toolchain.
36 //! In that case rustc distributes the objects as a part of the target's Rust toolchain
37 //! and falls back to linking with them manually.
38 //! Unlike native toolchains, rustc only currently adds the libc's objects during linking,
39 //! but not gcc's. As a result rustc cannot link with C++ static libraries (#36710)
40 //! when linking in self-contained mode.
41
42 use crate::spec::LinkOutputKind;
43 use rustc_serialize::json::{Json, ToJson};
44 use std::collections::BTreeMap;
45 use std::str::FromStr;
46
47 pub type CrtObjects = BTreeMap<LinkOutputKind, Vec<String>>;
48
49 pub(super) fn new(obj_table: &[(LinkOutputKind, &[&str])]) -> CrtObjects {
50     obj_table.iter().map(|(z, k)| (*z, k.iter().map(|b| b.to_string()).collect())).collect()
51 }
52
53 pub(super) fn all(obj: &str) -> CrtObjects {
54     new(&[
55         (LinkOutputKind::DynamicNoPicExe, &[obj]),
56         (LinkOutputKind::DynamicPicExe, &[obj]),
57         (LinkOutputKind::StaticNoPicExe, &[obj]),
58         (LinkOutputKind::StaticPicExe, &[obj]),
59         (LinkOutputKind::DynamicDylib, &[obj]),
60         (LinkOutputKind::StaticDylib, &[obj]),
61     ])
62 }
63
64 pub(super) fn pre_musl_fallback() -> CrtObjects {
65     new(&[
66         (LinkOutputKind::DynamicNoPicExe, &["crt1.o", "crti.o"]),
67         (LinkOutputKind::DynamicPicExe, &["Scrt1.o", "crti.o"]),
68         (LinkOutputKind::StaticNoPicExe, &["crt1.o", "crti.o"]),
69         (LinkOutputKind::StaticPicExe, &["rcrt1.o", "crti.o"]),
70         (LinkOutputKind::DynamicDylib, &["crti.o"]),
71         (LinkOutputKind::StaticDylib, &["crti.o"]),
72     ])
73 }
74
75 pub(super) fn post_musl_fallback() -> CrtObjects {
76     all("crtn.o")
77 }
78
79 pub(super) fn pre_mingw_fallback() -> CrtObjects {
80     new(&[
81         (LinkOutputKind::DynamicNoPicExe, &["crt2.o", "rsbegin.o"]),
82         (LinkOutputKind::DynamicPicExe, &["crt2.o", "rsbegin.o"]),
83         (LinkOutputKind::StaticNoPicExe, &["crt2.o", "rsbegin.o"]),
84         (LinkOutputKind::StaticPicExe, &["crt2.o", "rsbegin.o"]),
85         (LinkOutputKind::DynamicDylib, &["dllcrt2.o", "rsbegin.o"]),
86         (LinkOutputKind::StaticDylib, &["dllcrt2.o", "rsbegin.o"]),
87     ])
88 }
89
90 pub(super) fn post_mingw_fallback() -> CrtObjects {
91     all("rsend.o")
92 }
93
94 pub(super) fn pre_mingw() -> CrtObjects {
95     all("rsbegin.o")
96 }
97
98 pub(super) fn post_mingw() -> CrtObjects {
99     all("rsend.o")
100 }
101
102 pub(super) fn pre_wasi_fallback() -> CrtObjects {
103     new(&[
104         (LinkOutputKind::DynamicNoPicExe, &["crt1.o"]),
105         (LinkOutputKind::DynamicPicExe, &["crt1.o"]),
106         (LinkOutputKind::StaticNoPicExe, &["crt1.o"]),
107         (LinkOutputKind::StaticPicExe, &["crt1.o"]),
108     ])
109 }
110
111 pub(super) fn post_wasi_fallback() -> CrtObjects {
112     new(&[])
113 }
114
115 /// Which logic to use to determine whether to fall back to the "self-contained" mode or not.
116 #[derive(Clone, Copy, PartialEq, Hash, Debug)]
117 pub enum CrtObjectsFallback {
118     Musl,
119     Mingw,
120     Wasm,
121 }
122
123 impl FromStr for CrtObjectsFallback {
124     type Err = ();
125
126     fn from_str(s: &str) -> Result<CrtObjectsFallback, ()> {
127         Ok(match s {
128             "musl" => CrtObjectsFallback::Musl,
129             "mingw" => CrtObjectsFallback::Mingw,
130             "wasm" => CrtObjectsFallback::Wasm,
131             _ => return Err(()),
132         })
133     }
134 }
135
136 impl ToJson for CrtObjectsFallback {
137     fn to_json(&self) -> Json {
138         match *self {
139             CrtObjectsFallback::Musl => "musl",
140             CrtObjectsFallback::Mingw => "mingw",
141             CrtObjectsFallback::Wasm => "wasm",
142         }
143         .to_json()
144     }
145 }