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.
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.
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 | - |
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 | - |
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).
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.
42 use crate::spec::LinkOutputKind;
43 use rustc_serialize::json::{Json, ToJson};
44 use std::collections::BTreeMap;
45 use std::str::FromStr;
47 pub type CrtObjects = BTreeMap<LinkOutputKind, Vec<String>>;
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()
53 pub(super) fn all(obj: &str) -> CrtObjects {
55 (LinkOutputKind::DynamicNoPicExe, &[obj]),
56 (LinkOutputKind::DynamicPicExe, &[obj]),
57 (LinkOutputKind::StaticNoPicExe, &[obj]),
58 (LinkOutputKind::StaticPicExe, &[obj]),
59 (LinkOutputKind::DynamicDylib, &[obj]),
60 (LinkOutputKind::StaticDylib, &[obj]),
64 pub(super) fn pre_musl_fallback() -> CrtObjects {
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"]),
75 pub(super) fn post_musl_fallback() -> CrtObjects {
79 pub(super) fn pre_mingw_fallback() -> CrtObjects {
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"]),
90 pub(super) fn post_mingw_fallback() -> CrtObjects {
94 pub(super) fn pre_mingw() -> CrtObjects {
98 pub(super) fn post_mingw() -> CrtObjects {
102 pub(super) fn pre_wasi_fallback() -> CrtObjects {
104 (LinkOutputKind::DynamicNoPicExe, &["crt1.o"]),
105 (LinkOutputKind::DynamicPicExe, &["crt1.o"]),
106 (LinkOutputKind::StaticNoPicExe, &["crt1.o"]),
107 (LinkOutputKind::StaticPicExe, &["crt1.o"]),
111 pub(super) fn post_wasi_fallback() -> CrtObjects {
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 {
123 impl FromStr for CrtObjectsFallback {
126 fn from_str(s: &str) -> Result<CrtObjectsFallback, ()> {
128 "musl" => CrtObjectsFallback::Musl,
129 "mingw" => CrtObjectsFallback::Mingw,
130 "wasm" => CrtObjectsFallback::Wasm,
136 impl ToJson for CrtObjectsFallback {
137 fn to_json(&self) -> Json {
139 CrtObjectsFallback::Musl => "musl",
140 CrtObjectsFallback::Mingw => "mingw",
141 CrtObjectsFallback::Wasm => "wasm",