]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_target/src/spec/crt_objects.rs
New upstream version 1.63.0+dfsg1
[rustc.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 //! | wasi-reactor-exe | N/A | N/A | N/A | N/A | crt1-reactor |
18 //!
19 //! | Post-link CRT objects | glibc | musl | bionic | mingw | wasi |
20 //! |-----------------------|---------------|---------------|----------------|--------|------|
21 //! | dynamic-nopic-exe | crtend, crtn | crtend, crtn | crtend_android | crtend | - |
22 //! | dynamic-pic-exe | crtendS, crtn | crtendS, crtn | crtend_android | crtend | - |
23 //! | static-nopic-exe | crtend, crtn | crtend, crtn | crtend_android | crtend | - |
24 //! | static-pic-exe | crtendS, crtn | crtendS, crtn | crtend_android | crtend | - |
25 //! | dynamic-dylib | crtendS, crtn | crtendS, crtn | crtend_so | crtend | - |
26 //! | static-dylib (gcc) | crtend, crtn | crtendS, crtn | crtend_so | crtend | - |
27 //! | static-dylib (clang) | crtendS, crtn | N/A | crtend_so | crtend | - |
28 //!
29 //! Use cases for rustc linking the CRT objects explicitly:
30 //! - rustc needs to add its own Rust-specific objects (mingw is the example)
31 //! - gcc wrapper cannot be used for some reason and linker like ld or lld is used directly.
32 //! - gcc wrapper pulls wrong CRT objects (e.g. from glibc when we are targeting musl).
33 //!
34 //! In general it is preferable to rely on the target's native toolchain to pull the objects.
35 //! However, for some targets (musl, mingw) rustc historically provides a more self-contained
36 //! installation not requiring users to install the native target's toolchain.
37 //! In that case rustc distributes the objects as a part of the target's Rust toolchain
38 //! and falls back to linking with them manually.
39 //! Unlike native toolchains, rustc only currently adds the libc's objects during linking,
40 //! but not gcc's. As a result rustc cannot link with C++ static libraries (#36710)
41 //! when linking in self-contained mode.
42
43 use crate::json::{Json, ToJson};
44 use crate::spec::LinkOutputKind;
45 use std::borrow::Cow;
46 use std::collections::BTreeMap;
47 use std::str::FromStr;
48
49 pub type CrtObjects = BTreeMap<LinkOutputKind, Vec<Cow<'static, str>>>;
50
51 pub(super) fn new(obj_table: &[(LinkOutputKind, &[&'static str])]) -> CrtObjects {
52 obj_table.iter().map(|(z, k)| (*z, k.iter().map(|b| (*b).into()).collect())).collect()
53 }
54
55 pub(super) fn all(obj: &'static str) -> CrtObjects {
56 new(&[
57 (LinkOutputKind::DynamicNoPicExe, &[obj]),
58 (LinkOutputKind::DynamicPicExe, &[obj]),
59 (LinkOutputKind::StaticNoPicExe, &[obj]),
60 (LinkOutputKind::StaticPicExe, &[obj]),
61 (LinkOutputKind::DynamicDylib, &[obj]),
62 (LinkOutputKind::StaticDylib, &[obj]),
63 ])
64 }
65
66 pub(super) fn pre_musl_fallback() -> CrtObjects {
67 new(&[
68 (LinkOutputKind::DynamicNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
69 (LinkOutputKind::DynamicPicExe, &["Scrt1.o", "crti.o", "crtbeginS.o"]),
70 (LinkOutputKind::StaticNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
71 (LinkOutputKind::StaticPicExe, &["rcrt1.o", "crti.o", "crtbeginS.o"]),
72 (LinkOutputKind::DynamicDylib, &["crti.o", "crtbeginS.o"]),
73 (LinkOutputKind::StaticDylib, &["crti.o", "crtbeginS.o"]),
74 ])
75 }
76
77 pub(super) fn post_musl_fallback() -> CrtObjects {
78 new(&[
79 (LinkOutputKind::DynamicNoPicExe, &["crtend.o", "crtn.o"]),
80 (LinkOutputKind::DynamicPicExe, &["crtendS.o", "crtn.o"]),
81 (LinkOutputKind::StaticNoPicExe, &["crtend.o", "crtn.o"]),
82 (LinkOutputKind::StaticPicExe, &["crtendS.o", "crtn.o"]),
83 (LinkOutputKind::DynamicDylib, &["crtendS.o", "crtn.o"]),
84 (LinkOutputKind::StaticDylib, &["crtendS.o", "crtn.o"]),
85 ])
86 }
87
88 pub(super) fn pre_mingw_fallback() -> CrtObjects {
89 new(&[
90 (LinkOutputKind::DynamicNoPicExe, &["crt2.o", "rsbegin.o"]),
91 (LinkOutputKind::DynamicPicExe, &["crt2.o", "rsbegin.o"]),
92 (LinkOutputKind::StaticNoPicExe, &["crt2.o", "rsbegin.o"]),
93 (LinkOutputKind::StaticPicExe, &["crt2.o", "rsbegin.o"]),
94 (LinkOutputKind::DynamicDylib, &["dllcrt2.o", "rsbegin.o"]),
95 (LinkOutputKind::StaticDylib, &["dllcrt2.o", "rsbegin.o"]),
96 ])
97 }
98
99 pub(super) fn post_mingw_fallback() -> CrtObjects {
100 all("rsend.o")
101 }
102
103 pub(super) fn pre_mingw() -> CrtObjects {
104 all("rsbegin.o")
105 }
106
107 pub(super) fn post_mingw() -> CrtObjects {
108 all("rsend.o")
109 }
110
111 pub(super) fn pre_wasi_fallback() -> CrtObjects {
112 // Use crt1-command.o instead of crt1.o to enable support for new-style
113 // commands. See https://reviews.llvm.org/D81689 for more info.
114 new(&[
115 (LinkOutputKind::DynamicNoPicExe, &["crt1-command.o"]),
116 (LinkOutputKind::DynamicPicExe, &["crt1-command.o"]),
117 (LinkOutputKind::StaticNoPicExe, &["crt1-command.o"]),
118 (LinkOutputKind::StaticPicExe, &["crt1-command.o"]),
119 (LinkOutputKind::WasiReactorExe, &["crt1-reactor.o"]),
120 ])
121 }
122
123 pub(super) fn post_wasi_fallback() -> CrtObjects {
124 new(&[])
125 }
126
127 /// Which logic to use to determine whether to fall back to the "self-contained" mode or not.
128 #[derive(Clone, Copy, PartialEq, Hash, Debug)]
129 pub enum CrtObjectsFallback {
130 Musl,
131 Mingw,
132 Wasm,
133 }
134
135 impl FromStr for CrtObjectsFallback {
136 type Err = ();
137
138 fn from_str(s: &str) -> Result<CrtObjectsFallback, ()> {
139 Ok(match s {
140 "musl" => CrtObjectsFallback::Musl,
141 "mingw" => CrtObjectsFallback::Mingw,
142 "wasm" => CrtObjectsFallback::Wasm,
143 _ => return Err(()),
144 })
145 }
146 }
147
148 impl ToJson for CrtObjectsFallback {
149 fn to_json(&self) -> Json {
150 match *self {
151 CrtObjectsFallback::Musl => "musl",
152 CrtObjectsFallback::Mingw => "mingw",
153 CrtObjectsFallback::Wasm => "wasm",
154 }
155 .to_json()
156 }
157 }