Skip to main content

rustc_codegen_ssa/back/
linker.rs

1use std::ffi::{OsStr, OsString};
2use std::fs::{self, File};
3use std::io::prelude::*;
4use std::path::{Path, PathBuf};
5use std::{env, iter, mem, str};
6
7use find_msvc_tools;
8use rustc_hir::attrs::WindowsSubsystemKind;
9use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
10use rustc_metadata::{
11    find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
12};
13use rustc_middle::bug;
14use rustc_middle::middle::dependency_format::Linkage;
15use rustc_middle::middle::exported_symbols::{
16    self, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
17};
18use rustc_middle::ty::TyCtxt;
19use rustc_session::Session;
20use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
21use rustc_target::spec::{Abi, Arch, Cc, LinkOutputKind, LinkerFlavor, Lld, Os};
22use tracing::{debug, warn};
23
24use super::command::Command;
25use super::symbol_export;
26use crate::back::symbol_export::allocator_shim_symbols;
27use crate::base::needs_allocator_shim_for_linking;
28use crate::errors;
29
30#[cfg(test)]
31mod tests;
32
33/// Disables non-English messages from localized linkers.
34/// Such messages may cause issues with text encoding on Windows (#35785)
35/// and prevent inspection of linker output in case of errors, which we occasionally do.
36/// This should be acceptable because other messages from rustc are in English anyway,
37/// and may also be desirable to improve searchability of the linker diagnostics.
38pub(crate) fn disable_localization(linker: &mut Command) {
39    // No harm in setting both env vars simultaneously.
40    // Unix-style linkers.
41    linker.env("LC_ALL", "C");
42    // MSVC's `link.exe`.
43    linker.env("VSLANG", "1033");
44}
45
46/// The third parameter is for env vars, used on windows to set up the
47/// path for MSVC to find its DLLs, and gcc to find its bundled
48/// toolchain
49pub(crate) fn get_linker<'a>(
50    sess: &'a Session,
51    linker: &Path,
52    flavor: LinkerFlavor,
53    self_contained: bool,
54    target_cpu: &'a str,
55    codegen_backend: &'static str,
56) -> Box<dyn Linker + 'a> {
57    let msvc_tool = find_msvc_tools::find_tool(sess.target.arch.desc(), "link.exe");
58
59    // If our linker looks like a batch script on Windows then to execute this
60    // we'll need to spawn `cmd` explicitly. This is primarily done to handle
61    // emscripten where the linker is `emcc.bat` and needs to be spawned as
62    // `cmd /c emcc.bat ...`.
63    //
64    // This worked historically but is needed manually since #42436 (regression
65    // was tagged as #42791) and some more info can be found on #44443 for
66    // emscripten itself.
67    let mut cmd = match linker.to_str() {
68        Some(linker) if falsecfg!(windows) && linker.ends_with(".bat") => Command::bat_script(linker),
69        _ => match flavor {
70            LinkerFlavor::Gnu(Cc::No, Lld::Yes)
71            | LinkerFlavor::Darwin(Cc::No, Lld::Yes)
72            | LinkerFlavor::WasmLld(Cc::No)
73            | LinkerFlavor::Msvc(Lld::Yes) => Command::lld(linker, flavor.lld_flavor()),
74            LinkerFlavor::Msvc(Lld::No)
75                if sess.opts.cg.linker.is_none() && sess.target.linker.is_none() =>
76            {
77                Command::new(msvc_tool.as_ref().map_or(linker, |t| t.path()))
78            }
79            _ => Command::new(linker),
80        },
81    };
82
83    // UWP apps have API restrictions enforced during Store submissions.
84    // To comply with the Windows App Certification Kit,
85    // MSVC needs to link with the Store versions of the runtime libraries (vcruntime, msvcrt, etc).
86    let t = &sess.target;
87    if #[allow(non_exhaustive_omitted_patterns)] match flavor {
    LinkerFlavor::Msvc(..) => true,
    _ => false,
}matches!(flavor, LinkerFlavor::Msvc(..)) && t.abi == Abi::Uwp {
88        if let Some(ref tool) = msvc_tool {
89            let original_path = tool.path();
90            if let Some(root_lib_path) = original_path.ancestors().nth(4) {
91                let arch = match t.arch {
92                    Arch::X86_64 => Some("x64"),
93                    Arch::X86 => Some("x86"),
94                    Arch::AArch64 => Some("arm64"),
95                    Arch::Arm => Some("arm"),
96                    _ => None,
97                };
98                if let Some(ref a) = arch {
99                    // FIXME: Move this to `fn linker_with_args`.
100                    let mut arg = OsString::from("/LIBPATH:");
101                    arg.push(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}\\lib\\{1}\\store",
                root_lib_path.display(), a))
    })format!("{}\\lib\\{}\\store", root_lib_path.display(), a));
102                    cmd.arg(&arg);
103                } else {
104                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:104",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::WARN,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(104u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::WARN <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("arch is not supported")
                                            as &dyn Value))])
            });
    } else { ; }
};warn!("arch is not supported");
105                }
106            } else {
107                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:107",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::WARN,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(107u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::WARN <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("MSVC root path lib location not found")
                                            as &dyn Value))])
            });
    } else { ; }
};warn!("MSVC root path lib location not found");
108            }
109        } else {
110            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:110",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::WARN,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(110u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::WARN <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("link.exe not found")
                                            as &dyn Value))])
            });
    } else { ; }
};warn!("link.exe not found");
111        }
112    }
113
114    // The compiler's sysroot often has some bundled tools, so add it to the
115    // PATH for the child.
116    let mut new_path = sess.get_tools_search_paths(self_contained);
117    let mut msvc_changed_path = false;
118    if sess.target.is_like_msvc
119        && let Some(ref tool) = msvc_tool
120    {
121        for (k, v) in tool.env() {
122            if k == "PATH" {
123                new_path.extend(env::split_paths(v));
124                msvc_changed_path = true;
125            } else {
126                cmd.env(k, v);
127            }
128        }
129    }
130
131    if !msvc_changed_path && let Some(path) = env::var_os("PATH") {
132        new_path.extend(env::split_paths(&path));
133    }
134    cmd.env("PATH", env::join_paths(new_path).unwrap());
135
136    // FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
137    // to the linker args construction.
138    if !(cmd.get_args().is_empty() || sess.target.abi == Abi::Uwp) {
    ::core::panicking::panic("assertion failed: cmd.get_args().is_empty() || sess.target.abi == Abi::Uwp")
};assert!(cmd.get_args().is_empty() || sess.target.abi == Abi::Uwp);
139    match flavor {
140        LinkerFlavor::Unix(Cc::No) if sess.target.os == Os::L4Re => {
141            Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>
142        }
143        LinkerFlavor::Unix(Cc::No) if sess.target.os == Os::Aix => {
144            Box::new(AixLinker::new(cmd, sess)) as Box<dyn Linker>
145        }
146        LinkerFlavor::WasmLld(Cc::No) => Box::new(WasmLd::new(cmd, sess)) as Box<dyn Linker>,
147        LinkerFlavor::Gnu(cc, _)
148        | LinkerFlavor::Darwin(cc, _)
149        | LinkerFlavor::WasmLld(cc)
150        | LinkerFlavor::Unix(cc) => Box::new(GccLinker {
151            cmd,
152            sess,
153            target_cpu,
154            hinted_static: None,
155            is_ld: cc == Cc::No,
156            is_gnu: flavor.is_gnu(),
157            uses_lld: flavor.uses_lld(),
158            codegen_backend,
159        }) as Box<dyn Linker>,
160        LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>,
161        LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
162        LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
163        LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box<dyn Linker>,
164        LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
165    }
166}
167
168// Note: Ideally neither these helper function, nor the macro-generated inherent methods below
169// would exist, and these functions would live in `trait Linker`.
170// Unfortunately, adding these functions to `trait Linker` make it `dyn`-incompatible.
171// If the methods are added to the trait with `where Self: Sized` bounds, then even a separate
172// implementation of them for `dyn Linker {}` wouldn't work due to a conflict with those
173// uncallable methods in the trait.
174
175/// Just pass the arguments to the linker as is.
176/// It is assumed that they are correctly prepared in advance.
177fn verbatim_args<L: Linker + ?Sized>(
178    l: &mut L,
179    args: impl IntoIterator<Item: AsRef<OsStr>>,
180) -> &mut L {
181    for arg in args {
182        l.cmd().arg(arg);
183    }
184    l
185}
186/// Add underlying linker arguments to C compiler command, by wrapping them in
187/// `-Wl` or `-Xlinker`.
188fn convert_link_args_to_cc_args(cmd: &mut Command, args: impl IntoIterator<Item: AsRef<OsStr>>) {
189    let mut combined_arg = OsString::from("-Wl");
190    for arg in args {
191        // If the argument itself contains a comma, we need to emit it
192        // as `-Xlinker`, otherwise we can use `-Wl`.
193        if arg.as_ref().as_encoded_bytes().contains(&b',') {
194            // Emit current `-Wl` argument, if any has been built.
195            if combined_arg != OsStr::new("-Wl") {
196                cmd.arg(combined_arg);
197                // Begin next `-Wl` argument.
198                combined_arg = OsString::from("-Wl");
199            }
200
201            // Emit `-Xlinker` argument.
202            cmd.arg("-Xlinker");
203            cmd.arg(arg);
204        } else {
205            // Append to `-Wl` argument.
206            combined_arg.push(",");
207            combined_arg.push(arg);
208        }
209    }
210    // Emit final `-Wl` argument.
211    if combined_arg != OsStr::new("-Wl") {
212        cmd.arg(combined_arg);
213    }
214}
215/// Arguments for the underlying linker.
216/// Add options to pass them through cc wrapper if `Linker` is a cc wrapper.
217fn link_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
218    if !l.is_cc() {
219        verbatim_args(l, args);
220    } else {
221        convert_link_args_to_cc_args(l.cmd(), args);
222    }
223    l
224}
225/// Arguments for the cc wrapper specifically.
226/// Check that it's indeed a cc wrapper and pass verbatim.
227fn cc_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
228    if !l.is_cc() { ::core::panicking::panic("assertion failed: l.is_cc()") };assert!(l.is_cc());
229    verbatim_args(l, args)
230}
231/// Arguments supported by both underlying linker and cc wrapper, pass verbatim.
232fn link_or_cc_args<L: Linker + ?Sized>(
233    l: &mut L,
234    args: impl IntoIterator<Item: AsRef<OsStr>>,
235) -> &mut L {
236    verbatim_args(l, args)
237}
238
239macro_rules! generate_arg_methods {
240    ($($ty:ty)*) => { $(
241        impl $ty {
242            #[allow(unused)]
243            pub(crate) fn verbatim_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
244                verbatim_args(self, args)
245            }
246            #[allow(unused)]
247            pub(crate) fn verbatim_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
248                verbatim_args(self, iter::once(arg))
249            }
250            #[allow(unused)]
251            pub(crate) fn link_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
252                link_args(self, args)
253            }
254            #[allow(unused)]
255            pub(crate) fn link_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
256                link_args(self, iter::once(arg))
257            }
258            #[allow(unused)]
259            pub(crate) fn cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
260                cc_args(self, args)
261            }
262            #[allow(unused)]
263            pub(crate) fn cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
264                cc_args(self, iter::once(arg))
265            }
266            #[allow(unused)]
267            pub(crate) fn link_or_cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
268                link_or_cc_args(self, args)
269            }
270            #[allow(unused)]
271            pub(crate) fn link_or_cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
272                link_or_cc_args(self, iter::once(arg))
273            }
274        }
275    )* }
276}
277
278impl dyn Linker + '_ {
    #[allow(unused)]
    pub(crate) fn verbatim_args(&mut self,
        args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
        verbatim_args(self, args)
    }
    #[allow(unused)]
    pub(crate) fn verbatim_arg(&mut self, arg: impl AsRef<OsStr>)
        -> &mut Self {
        verbatim_args(self, iter::once(arg))
    }
    #[allow(unused)]
    pub(crate) fn link_args(&mut self,
        args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
        link_args(self, args)
    }
    #[allow(unused)]
    pub(crate) fn link_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
        link_args(self, iter::once(arg))
    }
    #[allow(unused)]
    pub(crate) fn cc_args(&mut self,
        args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
        cc_args(self, args)
    }
    #[allow(unused)]
    pub(crate) fn cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
        cc_args(self, iter::once(arg))
    }
    #[allow(unused)]
    pub(crate) fn link_or_cc_args(&mut self,
        args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
        link_or_cc_args(self, args)
    }
    #[allow(unused)]
    pub(crate) fn link_or_cc_arg(&mut self, arg: impl AsRef<OsStr>)
        -> &mut Self {
        link_or_cc_args(self, iter::once(arg))
    }
}generate_arg_methods! {
279    GccLinker<'_>
280    MsvcLinker<'_>
281    EmLinker<'_>
282    WasmLd<'_>
283    L4Bender<'_>
284    AixLinker<'_>
285    LlbcLinker<'_>
286    PtxLinker<'_>
287    BpfLinker<'_>
288    dyn Linker + '_
289}
290
291/// Linker abstraction used by `back::link` to build up the command to invoke a
292/// linker.
293///
294/// This trait is the total list of requirements needed by `back::link` and
295/// represents the meaning of each option being passed down. This trait is then
296/// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
297/// MSVC linker (e.g., `link.exe`) is being used.
298pub(crate) trait Linker {
299    fn cmd(&mut self) -> &mut Command;
300    fn is_cc(&self) -> bool {
301        false
302    }
303    fn set_output_kind(
304        &mut self,
305        output_kind: LinkOutputKind,
306        crate_type: CrateType,
307        out_filename: &Path,
308    );
309    fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
310        ::rustc_middle::util::bug::bug_fmt(format_args!("dylib linked with unsupported linker"))bug!("dylib linked with unsupported linker")
311    }
312    fn link_dylib_by_path(&mut self, _path: &Path, _as_needed: bool) {
313        ::rustc_middle::util::bug::bug_fmt(format_args!("dylib linked with unsupported linker"))bug!("dylib linked with unsupported linker")
314    }
315    fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
316        ::rustc_middle::util::bug::bug_fmt(format_args!("framework linked with unsupported linker"))bug!("framework linked with unsupported linker")
317    }
318    fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool);
319    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
320    fn include_path(&mut self, path: &Path) {
321        link_or_cc_args(link_or_cc_args(self, &["-L"]), &[path]);
322    }
323    fn framework_path(&mut self, _path: &Path) {
324        ::rustc_middle::util::bug::bug_fmt(format_args!("framework path set with unsupported linker"))bug!("framework path set with unsupported linker")
325    }
326    fn output_filename(&mut self, path: &Path) {
327        link_or_cc_args(link_or_cc_args(self, &["-o"]), &[path]);
328    }
329    fn add_object(&mut self, path: &Path) {
330        link_or_cc_args(self, &[path]);
331    }
332    fn gc_sections(&mut self, keep_metadata: bool);
333    fn full_relro(&mut self);
334    fn partial_relro(&mut self);
335    fn no_relro(&mut self);
336    fn optimize(&mut self);
337    fn pgo_gen(&mut self);
338    fn control_flow_guard(&mut self);
339    fn ehcont_guard(&mut self);
340    fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]);
341    fn no_crt_objects(&mut self);
342    fn no_default_libraries(&mut self);
343    fn export_symbols(
344        &mut self,
345        tmpdir: &Path,
346        crate_type: CrateType,
347        symbols: &[(String, SymbolExportKind)],
348    );
349    fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind);
350    fn linker_plugin_lto(&mut self);
351    fn add_eh_frame_header(&mut self) {}
352    fn add_no_exec(&mut self) {}
353    fn add_as_needed(&mut self) {}
354    fn reset_per_library_state(&mut self) {}
355}
356
357impl dyn Linker + '_ {
358    pub(crate) fn take_cmd(&mut self) -> Command {
359        mem::replace(self.cmd(), Command::new(""))
360    }
361}
362
363struct GccLinker<'a> {
364    cmd: Command,
365    sess: &'a Session,
366    target_cpu: &'a str,
367    hinted_static: Option<bool>, // Keeps track of the current hinting mode.
368    // Link as ld
369    is_ld: bool,
370    is_gnu: bool,
371    uses_lld: bool,
372    codegen_backend: &'static str,
373}
374
375impl<'a> GccLinker<'a> {
376    fn takes_hints(&self) -> bool {
377        // Really this function only returns true if the underlying linker
378        // configured for a compiler is binutils `ld.bfd` and `ld.gold`. We
379        // don't really have a foolproof way to detect that, so rule out some
380        // platforms where currently this is guaranteed to *not* be the case:
381        //
382        // * On OSX they have their own linker, not binutils'
383        // * For WebAssembly the only functional linker is LLD, which doesn't
384        //   support hint flags
385        !self.sess.target.is_like_darwin && !self.sess.target.is_like_wasm
386    }
387
388    // Some platforms take hints about whether a library is static or dynamic.
389    // For those that support this, we ensure we pass the option if the library
390    // was flagged "static" (most defaults are dynamic) to ensure that if
391    // libfoo.a and libfoo.so both exist that the right one is chosen.
392    fn hint_static(&mut self) {
393        if !self.takes_hints() {
394            return;
395        }
396        if self.hinted_static != Some(true) {
397            self.link_arg("-Bstatic");
398            self.hinted_static = Some(true);
399        }
400    }
401
402    fn hint_dynamic(&mut self) {
403        if !self.takes_hints() {
404            return;
405        }
406        if self.hinted_static != Some(false) {
407            self.link_arg("-Bdynamic");
408            self.hinted_static = Some(false);
409        }
410    }
411
412    fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
413        if let Some(plugin_path) = plugin_path {
414            let mut arg = OsString::from("-plugin=");
415            arg.push(plugin_path);
416            self.link_arg(&arg);
417        }
418
419        let opt_level = match self.sess.opts.optimize {
420            config::OptLevel::No => "O0",
421            config::OptLevel::Less => "O1",
422            config::OptLevel::More | config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
423            config::OptLevel::Aggressive => "O3",
424        };
425
426        if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
427            self.link_arg(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-plugin-opt=sample-profile={0}",
                path.display()))
    })format!("-plugin-opt=sample-profile={}", path.display()));
428        };
429        let prefix = if self.codegen_backend == "gcc" {
430            // The GCC linker plugin requires a leading dash.
431            "-"
432        } else {
433            ""
434        };
435        self.link_args(&[
436            &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-plugin-opt={0}{1}", prefix,
                opt_level))
    })format!("-plugin-opt={prefix}{opt_level}"),
437            &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-plugin-opt={1}mcpu={0}",
                self.target_cpu, prefix))
    })format!("-plugin-opt={prefix}mcpu={}", self.target_cpu),
438        ]);
439    }
440
441    fn build_dylib(&mut self, crate_type: CrateType, out_filename: &Path) {
442        // On mac we need to tell the linker to let this library be rpathed
443        if self.sess.target.is_like_darwin {
444            if self.is_cc() {
445                // `-dynamiclib` makes `cc` pass `-dylib` to the linker.
446                self.cc_arg("-dynamiclib");
447            } else {
448                self.link_arg("-dylib");
449                // Clang also sets `-dynamic`, but that's implied by `-dylib`, so unnecessary.
450            }
451
452            // Note that the `osx_rpath_install_name` option here is a hack
453            // purely to support bootstrap right now, we should get a more
454            // principled solution at some point to force the compiler to pass
455            // the right `-Wl,-install_name` with an `@rpath` in it.
456            if self.sess.opts.cg.rpath || self.sess.opts.unstable_opts.osx_rpath_install_name {
457                let mut rpath = OsString::from("@rpath/");
458                rpath.push(out_filename.file_name().unwrap());
459                self.link_arg("-install_name").link_arg(rpath);
460            }
461        } else {
462            self.link_or_cc_arg("-shared");
463            if let Some(name) = out_filename.file_name() {
464                if self.sess.target.is_like_windows {
465                    // The output filename already contains `dll_suffix` so
466                    // the resulting import library will have a name in the
467                    // form of libfoo.dll.a
468                    let (prefix, suffix) = self.sess.staticlib_components(false);
469                    let mut implib_name = OsString::from(prefix);
470                    implib_name.push(name);
471                    implib_name.push(suffix);
472                    let mut out_implib = OsString::from("--out-implib=");
473                    out_implib.push(out_filename.with_file_name(implib_name));
474                    self.link_arg(out_implib);
475                } else if crate_type == CrateType::Dylib {
476                    // When dylibs are linked by a full path this value will get into `DT_NEEDED`
477                    // instead of the full path, so the library can be later found in some other
478                    // location than that specific path.
479                    let mut soname = OsString::from("-soname=");
480                    soname.push(name);
481                    self.link_arg(soname);
482                }
483            }
484        }
485    }
486
487    fn with_as_needed(&mut self, as_needed: bool, f: impl FnOnce(&mut Self)) {
488        if !as_needed {
489            if self.sess.target.is_like_darwin {
490                // FIXME(81490): ld64 doesn't support these flags but macOS 11
491                // has -needed-l{} / -needed_library {}
492                // but we have no way to detect that here.
493                self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
494            } else if self.is_gnu && !self.sess.target.is_like_windows {
495                self.link_arg("--no-as-needed");
496            } else {
497                self.sess.dcx().emit_warn(errors::LinkerUnsupportedModifier);
498            }
499        }
500
501        f(self);
502
503        if !as_needed {
504            if self.sess.target.is_like_darwin {
505                // See above FIXME comment
506            } else if self.is_gnu && !self.sess.target.is_like_windows {
507                self.link_arg("--as-needed");
508            }
509        }
510    }
511}
512
513impl<'a> Linker for GccLinker<'a> {
514    fn cmd(&mut self) -> &mut Command {
515        &mut self.cmd
516    }
517
518    fn is_cc(&self) -> bool {
519        !self.is_ld
520    }
521
522    fn set_output_kind(
523        &mut self,
524        output_kind: LinkOutputKind,
525        crate_type: CrateType,
526        out_filename: &Path,
527    ) {
528        match output_kind {
529            LinkOutputKind::DynamicNoPicExe => {
530                // noop on windows w/ gcc, warning w/ clang
531                if !self.is_ld && self.is_gnu && !self.sess.target.is_like_windows {
532                    self.cc_arg("-no-pie");
533                }
534            }
535            LinkOutputKind::DynamicPicExe => {
536                // noop on windows w/ gcc & ld, error w/ lld
537                if !self.sess.target.is_like_windows {
538                    // `-pie` works for both gcc wrapper and ld.
539                    self.link_or_cc_arg("-pie");
540                }
541            }
542            LinkOutputKind::StaticNoPicExe => {
543                // `-static` works for both gcc wrapper and ld.
544                self.link_or_cc_arg("-static");
545                if !self.is_ld && self.is_gnu {
546                    self.cc_arg("-no-pie");
547                }
548            }
549            LinkOutputKind::StaticPicExe => {
550                if !self.is_ld {
551                    // Note that combination `-static -pie` doesn't work as expected
552                    // for the gcc wrapper, `-static` in that case suppresses `-pie`.
553                    self.cc_arg("-static-pie");
554                } else {
555                    // `--no-dynamic-linker` and `-z text` are not strictly necessary for producing
556                    // a static pie, but currently passed because gcc and clang pass them.
557                    // The former suppresses the `INTERP` ELF header specifying dynamic linker,
558                    // which is otherwise implicitly injected by ld (but not lld).
559                    // The latter doesn't change anything, only ensures that everything is pic.
560                    self.link_args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
561                }
562            }
563            LinkOutputKind::DynamicDylib => self.build_dylib(crate_type, out_filename),
564            LinkOutputKind::StaticDylib => {
565                self.link_or_cc_arg("-static");
566                self.build_dylib(crate_type, out_filename);
567            }
568            LinkOutputKind::WasiReactorExe => {
569                self.link_args(&["--entry", "_initialize"]);
570            }
571        }
572
573        // VxWorks compiler driver introduced `--static-crt` flag specifically for rustc,
574        // it switches linking for libc and similar system libraries to static without using
575        // any `#[link]` attributes in the `libc` crate, see #72782 for details.
576        // FIXME: Switch to using `#[link]` attributes in the `libc` crate
577        // similarly to other targets.
578        if self.sess.target.os == Os::VxWorks
579            && #[allow(non_exhaustive_omitted_patterns)] match output_kind {
    LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe |
        LinkOutputKind::StaticDylib => true,
    _ => false,
}matches!(
580                output_kind,
581                LinkOutputKind::StaticNoPicExe
582                    | LinkOutputKind::StaticPicExe
583                    | LinkOutputKind::StaticDylib
584            )
585        {
586            self.cc_arg("--static-crt");
587        }
588
589        // avr-none doesn't have default ISA, users must specify which specific
590        // CPU (well, microcontroller) they are targetting using `-Ctarget-cpu`.
591        //
592        // Currently this makes sense only when using avr-gcc as a linker, since
593        // it brings a couple of hand-written important intrinsics from libgcc.
594        if self.sess.target.arch == Arch::Avr && !self.uses_lld {
595            self.verbatim_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-mmcu={0}", self.target_cpu))
    })format!("-mmcu={}", self.target_cpu));
596        }
597    }
598
599    fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool) {
600        if self.sess.target.os == Os::Illumos && name == "c" {
601            // libc will be added via late_link_args on illumos so that it will
602            // appear last in the library search order.
603            // FIXME: This should be replaced by a more complete and generic
604            // mechanism for controlling the order of library arguments passed
605            // to the linker.
606            return;
607        }
608        self.hint_dynamic();
609        self.with_as_needed(as_needed, |this| {
610            let colon = if verbatim && this.is_gnu { ":" } else { "" };
611            this.link_or_cc_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
    })format!("-l{colon}{name}"));
612        });
613    }
614
615    fn link_dylib_by_path(&mut self, path: &Path, as_needed: bool) {
616        self.hint_dynamic();
617        self.with_as_needed(as_needed, |this| {
618            this.link_or_cc_arg(path);
619        })
620    }
621
622    fn link_framework_by_name(&mut self, name: &str, _verbatim: bool, as_needed: bool) {
623        self.hint_dynamic();
624        if !as_needed {
625            // FIXME(81490): ld64 as of macOS 11 supports the -needed_framework
626            // flag but we have no way to detect that here.
627            // self.link_or_cc_arg("-needed_framework").link_or_cc_arg(name);
628            self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
629        }
630        self.link_or_cc_args(&["-framework", name]);
631    }
632
633    fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
634        self.hint_static();
635        let colon = if verbatim && self.is_gnu { ":" } else { "" };
636        if !whole_archive {
637            self.link_or_cc_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
    })format!("-l{colon}{name}"));
638        } else if self.sess.target.is_like_darwin {
639            // -force_load is the macOS equivalent of --whole-archive, but it
640            // involves passing the full path to the library to link.
641            self.link_arg("-force_load");
642            self.link_arg(find_native_static_library(name, verbatim, self.sess));
643        } else {
644            self.link_arg("--whole-archive")
645                .link_or_cc_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
    })format!("-l{colon}{name}"))
646                .link_arg("--no-whole-archive");
647        }
648    }
649
650    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
651        self.hint_static();
652        if !whole_archive {
653            self.link_or_cc_arg(path);
654        } else if self.sess.target.is_like_darwin {
655            self.link_arg("-force_load").link_arg(path);
656        } else {
657            self.link_arg("--whole-archive").link_arg(path).link_arg("--no-whole-archive");
658        }
659    }
660
661    fn framework_path(&mut self, path: &Path) {
662        self.link_or_cc_arg("-F").link_or_cc_arg(path);
663    }
664    fn full_relro(&mut self) {
665        self.link_args(&["-z", "relro", "-z", "now"]);
666    }
667    fn partial_relro(&mut self) {
668        self.link_args(&["-z", "relro"]);
669    }
670    fn no_relro(&mut self) {
671        self.link_args(&["-z", "norelro"]);
672    }
673
674    fn gc_sections(&mut self, keep_metadata: bool) {
675        // The dead_strip option to the linker specifies that functions and data
676        // unreachable by the entry point will be removed. This is quite useful
677        // with Rust's compilation model of compiling libraries at a time into
678        // one object file. For example, this brings hello world from 1.7MB to
679        // 458K.
680        //
681        // Note that this is done for both executables and dynamic libraries. We
682        // won't get much benefit from dylibs because LLVM will have already
683        // stripped away as much as it could. This has not been seen to impact
684        // link times negatively.
685        //
686        // -dead_strip can't be part of the pre_link_args because it's also used
687        // for partial linking when using multiple codegen units (-r). So we
688        // insert it here.
689        if self.sess.target.is_like_darwin {
690            self.link_arg("-dead_strip");
691
692        // If we're building a dylib, we don't use --gc-sections because LLVM
693        // has already done the best it can do, and we also don't want to
694        // eliminate the metadata. If we're building an executable, however,
695        // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
696        // reduction.
697        } else if (self.is_gnu || self.sess.target.is_like_wasm) && !keep_metadata {
698            self.link_arg("--gc-sections");
699        }
700    }
701
702    fn optimize(&mut self) {
703        if !self.is_gnu && !self.sess.target.is_like_wasm {
704            return;
705        }
706
707        // GNU-style linkers support optimization with -O. GNU ld doesn't
708        // need a numeric argument, but other linkers do.
709        if self.sess.opts.optimize == config::OptLevel::More
710            || self.sess.opts.optimize == config::OptLevel::Aggressive
711        {
712            self.link_arg("-O1");
713        }
714    }
715
716    fn pgo_gen(&mut self) {
717        if !self.is_gnu {
718            return;
719        }
720
721        // If we're doing PGO generation stuff and on a GNU-like linker, use the
722        // "-u" flag to properly pull in the profiler runtime bits.
723        //
724        // This is because LLVM otherwise won't add the needed initialization
725        // for us on Linux (though the extra flag should be harmless if it
726        // does).
727        //
728        // See https://reviews.llvm.org/D14033 and https://reviews.llvm.org/D14030.
729        //
730        // Though it may be worth to try to revert those changes upstream, since
731        // the overhead of the initialization should be minor.
732        self.link_or_cc_args(&["-u", "__llvm_profile_runtime"]);
733    }
734
735    fn control_flow_guard(&mut self) {}
736
737    fn ehcont_guard(&mut self) {}
738
739    fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
740        // MacOS linker doesn't support stripping symbols directly anymore.
741        if self.sess.target.is_like_darwin {
742            return;
743        }
744
745        match strip {
746            Strip::None => {}
747            Strip::Debuginfo => {
748                // The illumos linker does not support --strip-debug although
749                // it does support --strip-all as a compatibility alias for -s.
750                // The --strip-debug case is handled by running an external
751                // `strip` utility as a separate step after linking.
752                if !self.sess.target.is_like_solaris {
753                    self.link_arg("--strip-debug");
754                }
755            }
756            Strip::Symbols => {
757                self.link_arg("--strip-all");
758            }
759        }
760        match self.sess.opts.unstable_opts.debuginfo_compression {
761            config::DebugInfoCompression::None => {}
762            config::DebugInfoCompression::Zlib => {
763                self.link_arg("--compress-debug-sections=zlib");
764            }
765            config::DebugInfoCompression::Zstd => {
766                self.link_arg("--compress-debug-sections=zstd");
767            }
768        }
769    }
770
771    fn no_crt_objects(&mut self) {
772        if !self.is_ld {
773            self.cc_arg("-nostartfiles");
774        }
775    }
776
777    fn no_default_libraries(&mut self) {
778        if !self.is_ld {
779            self.cc_arg("-nodefaultlibs");
780        }
781    }
782
783    fn export_symbols(
784        &mut self,
785        tmpdir: &Path,
786        crate_type: CrateType,
787        symbols: &[(String, SymbolExportKind)],
788    ) {
789        // Symbol visibility in object files typically takes care of this.
790        if crate_type == CrateType::Executable {
791            let should_export_executable_symbols =
792                self.sess.opts.unstable_opts.export_executable_symbols;
793            if self.sess.target.override_export_symbols.is_none()
794                && !should_export_executable_symbols
795            {
796                return;
797            }
798        }
799
800        // We manually create a list of exported symbols to ensure we don't expose any more.
801        // The object files have far more public symbols than we actually want to export,
802        // so we hide them all here.
803
804        if !self.sess.target.limit_rdylib_exports {
805            return;
806        }
807
808        let path = tmpdir.join(if self.sess.target.is_like_windows { "list.def" } else { "list" });
809        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:809",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(809u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("EXPORTED SYMBOLS:")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("EXPORTED SYMBOLS:");
810
811        if self.sess.target.is_like_darwin {
812            // Write a plain, newline-separated list of symbols
813            let res = try {
814                let mut f = File::create_buffered(&path)?;
815                for (sym, _) in symbols {
816                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:816",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(816u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("  _{0}",
                                                    sym) as &dyn Value))])
            });
    } else { ; }
};debug!("  _{sym}");
817                    f.write_fmt(format_args!("_{0}\n", sym))writeln!(f, "_{sym}")?;
818                }
819            };
820            if let Err(error) = res {
821                self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
822            }
823            self.link_arg("-exported_symbols_list").link_arg(path);
824        } else if self.sess.target.is_like_windows {
825            let res = try {
826                let mut f = File::create_buffered(&path)?;
827
828                // .def file similar to MSVC one but without LIBRARY section
829                // because LD doesn't like when it's empty
830                f.write_fmt(format_args!("EXPORTS\n"))writeln!(f, "EXPORTS")?;
831                for (symbol, kind) in symbols {
832                    let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
833                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:833",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(833u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("  _{0}",
                                                    symbol) as &dyn Value))])
            });
    } else { ; }
};debug!("  _{symbol}");
834                    // Quote the name in case it's reserved by linker in some way
835                    // (this accounts for names with dots in particular).
836                    f.write_fmt(format_args!("  \"{0}\"{1}\n", symbol, kind_marker))writeln!(f, "  \"{symbol}\"{kind_marker}")?;
837                }
838            };
839            if let Err(error) = res {
840                self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
841            }
842            self.link_arg(path);
843        } else if self.sess.target.is_like_wasm {
844            self.link_arg("--no-export-dynamic");
845            for (sym, _) in symbols {
846                self.link_arg("--export").link_arg(sym);
847            }
848        } else if crate_type == CrateType::Executable && !self.sess.target.is_like_solaris {
849            let res = try {
850                let mut f = File::create_buffered(&path)?;
851                f.write_fmt(format_args!("{{\n"))writeln!(f, "{{")?;
852                for (sym, _) in symbols {
853                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:853",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(853u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["sym"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&sym as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(sym);
854                    f.write_fmt(format_args!("  {0};\n", sym))writeln!(f, "  {sym};")?;
855                }
856                f.write_fmt(format_args!("}};\n"))writeln!(f, "}};")?;
857            };
858            if let Err(error) = res {
859                self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
860            }
861            self.link_arg("--dynamic-list").link_arg(path);
862        } else {
863            // Write an LD version script
864            let res = try {
865                let mut f = File::create_buffered(&path)?;
866                f.write_fmt(format_args!("{{\n"))writeln!(f, "{{")?;
867                if !symbols.is_empty() {
868                    f.write_fmt(format_args!("  global:\n"))writeln!(f, "  global:")?;
869                    for (sym, _) in symbols {
870                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:870",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(870u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("    {0};",
                                                    sym) as &dyn Value))])
            });
    } else { ; }
};debug!("    {sym};");
871                        f.write_fmt(format_args!("    {0};\n", sym))writeln!(f, "    {sym};")?;
872                    }
873                }
874                f.write_fmt(format_args!("\n  local:\n    *;\n}};\n"))writeln!(f, "\n  local:\n    *;\n}};")?;
875            };
876            if let Err(error) = res {
877                self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
878            }
879            if self.sess.target.is_like_solaris {
880                self.link_arg("-M").link_arg(path);
881            } else {
882                let mut arg = OsString::from("--version-script=");
883                arg.push(path);
884                self.link_arg(arg).link_arg("--no-undefined-version");
885            }
886        }
887    }
888
889    fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
890        self.link_args(&["--subsystem", subsystem.as_str()]);
891    }
892
893    fn reset_per_library_state(&mut self) {
894        self.hint_dynamic(); // Reset to default before returning the composed command line.
895    }
896
897    fn linker_plugin_lto(&mut self) {
898        match self.sess.opts.cg.linker_plugin_lto {
899            LinkerPluginLto::Disabled => {
900                // Nothing to do
901            }
902            LinkerPluginLto::LinkerPluginAuto => {
903                self.push_linker_plugin_lto_args(None);
904            }
905            LinkerPluginLto::LinkerPlugin(ref path) => {
906                self.push_linker_plugin_lto_args(Some(path.as_os_str()));
907            }
908        }
909    }
910
911    // Add the `GNU_EH_FRAME` program header which is required to locate unwinding information.
912    // Some versions of `gcc` add it implicitly, some (e.g. `musl-gcc`) don't,
913    // so we just always add it.
914    fn add_eh_frame_header(&mut self) {
915        self.link_arg("--eh-frame-hdr");
916    }
917
918    fn add_no_exec(&mut self) {
919        if self.sess.target.is_like_windows {
920            self.link_arg("--nxcompat");
921        } else if self.is_gnu {
922            self.link_args(&["-z", "noexecstack"]);
923        }
924    }
925
926    fn add_as_needed(&mut self) {
927        if self.is_gnu && !self.sess.target.is_like_windows {
928            self.link_arg("--as-needed");
929        } else if self.sess.target.is_like_solaris {
930            // -z ignore is the Solaris equivalent to the GNU ld --as-needed option
931            self.link_args(&["-z", "ignore"]);
932        }
933    }
934}
935
936struct MsvcLinker<'a> {
937    cmd: Command,
938    sess: &'a Session,
939}
940
941impl<'a> Linker for MsvcLinker<'a> {
942    fn cmd(&mut self) -> &mut Command {
943        &mut self.cmd
944    }
945
946    fn set_output_kind(
947        &mut self,
948        output_kind: LinkOutputKind,
949        _crate_type: CrateType,
950        out_filename: &Path,
951    ) {
952        match output_kind {
953            LinkOutputKind::DynamicNoPicExe
954            | LinkOutputKind::DynamicPicExe
955            | LinkOutputKind::StaticNoPicExe
956            | LinkOutputKind::StaticPicExe => {}
957            LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
958                self.link_arg("/DLL");
959                let mut arg: OsString = "/IMPLIB:".into();
960                arg.push(out_filename.with_extension("dll.lib"));
961                self.link_arg(arg);
962            }
963            LinkOutputKind::WasiReactorExe => {
964                {
    ::core::panicking::panic_fmt(format_args!("can\'t link as reactor on non-wasi target"));
};panic!("can't link as reactor on non-wasi target");
965            }
966        }
967    }
968
969    fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
970        // On MSVC-like targets rustc supports import libraries using alternative naming
971        // scheme (`libfoo.a`) unsupported by linker, search for such libraries manually.
972        if let Some(path) = try_find_native_dynamic_library(self.sess, name, verbatim) {
973            self.link_arg(path);
974        } else {
975            self.link_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}", name,
                if verbatim { "" } else { ".lib" }))
    })format!("{}{}", name, if verbatim { "" } else { ".lib" }));
976        }
977    }
978
979    fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
980        // When producing a dll, MSVC linker may not emit an implib file if the dll doesn't export
981        // any symbols, so we skip linking if the implib file is not present.
982        let implib_path = path.with_extension("dll.lib");
983        if implib_path.exists() {
984            self.link_or_cc_arg(implib_path);
985        }
986    }
987
988    fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
989        // On MSVC-like targets rustc supports static libraries using alternative naming
990        // scheme (`libfoo.a`) unsupported by linker, search for such libraries manually.
991        if let Some(path) = try_find_native_static_library(self.sess, name, verbatim) {
992            self.link_staticlib_by_path(&path, whole_archive);
993        } else {
994            let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
995            let (prefix, suffix) = self.sess.staticlib_components(verbatim);
996            self.link_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}{2}{3}", opts, prefix, name,
                suffix))
    })format!("{opts}{prefix}{name}{suffix}"));
997        }
998    }
999
1000    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1001        if !whole_archive {
1002            self.link_arg(path);
1003        } else {
1004            let mut arg = OsString::from("/WHOLEARCHIVE:");
1005            arg.push(path);
1006            self.link_arg(arg);
1007        }
1008    }
1009
1010    fn gc_sections(&mut self, _keep_metadata: bool) {
1011        // MSVC's ICF (Identical COMDAT Folding) link optimization is
1012        // slow for Rust and thus we disable it by default when not in
1013        // optimization build.
1014        if self.sess.opts.optimize != config::OptLevel::No {
1015            self.link_arg("/OPT:REF,ICF");
1016        } else {
1017            // It is necessary to specify NOICF here, because /OPT:REF
1018            // implies ICF by default.
1019            self.link_arg("/OPT:REF,NOICF");
1020        }
1021    }
1022
1023    fn full_relro(&mut self) {
1024        // noop
1025    }
1026
1027    fn partial_relro(&mut self) {
1028        // noop
1029    }
1030
1031    fn no_relro(&mut self) {
1032        // noop
1033    }
1034
1035    fn no_crt_objects(&mut self) {
1036        // noop
1037    }
1038
1039    fn no_default_libraries(&mut self) {
1040        self.link_arg("/NODEFAULTLIB");
1041    }
1042
1043    fn include_path(&mut self, path: &Path) {
1044        let mut arg = OsString::from("/LIBPATH:");
1045        arg.push(path);
1046        self.link_arg(&arg);
1047    }
1048
1049    fn output_filename(&mut self, path: &Path) {
1050        let mut arg = OsString::from("/OUT:");
1051        arg.push(path);
1052        self.link_arg(&arg);
1053    }
1054
1055    fn optimize(&mut self) {
1056        // Needs more investigation of `/OPT` arguments
1057    }
1058
1059    fn pgo_gen(&mut self) {
1060        // Nothing needed here.
1061    }
1062
1063    fn control_flow_guard(&mut self) {
1064        self.link_arg("/guard:cf");
1065    }
1066
1067    fn ehcont_guard(&mut self) {
1068        if self.sess.target.pointer_width == 64 {
1069            self.link_arg("/guard:ehcont");
1070        }
1071    }
1072
1073    fn debuginfo(&mut self, _strip: Strip, natvis_debugger_visualizers: &[PathBuf]) {
1074        // This will cause the Microsoft linker to generate a PDB file
1075        // from the CodeView line tables in the object files.
1076        self.link_arg("/DEBUG");
1077
1078        // Default to emitting only the file name of the PDB file into
1079        // the binary instead of the full path. Emitting the full path
1080        // may leak private information (such as user names).
1081        // See https://github.com/rust-lang/rust/issues/87825.
1082        //
1083        // This default behavior can be overridden by explicitly passing
1084        // `-Clink-arg=/PDBALTPATH:...` to rustc.
1085        self.link_arg("/PDBALTPATH:%_PDB%");
1086
1087        // This will cause the Microsoft linker to embed .natvis info into the PDB file
1088        let natvis_dir_path = self.sess.opts.sysroot.path().join("lib\\rustlib\\etc");
1089        if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
1090            for entry in natvis_dir {
1091                match entry {
1092                    Ok(entry) => {
1093                        let path = entry.path();
1094                        if path.extension() == Some("natvis".as_ref()) {
1095                            let mut arg = OsString::from("/NATVIS:");
1096                            arg.push(path);
1097                            self.link_arg(arg);
1098                        }
1099                    }
1100                    Err(error) => {
1101                        self.sess.dcx().emit_warn(errors::NoNatvisDirectory { error });
1102                    }
1103                }
1104            }
1105        }
1106
1107        // This will cause the Microsoft linker to embed .natvis info for all crates into the PDB file
1108        for path in natvis_debugger_visualizers {
1109            let mut arg = OsString::from("/NATVIS:");
1110            arg.push(path);
1111            self.link_arg(arg);
1112        }
1113    }
1114
1115    // Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
1116    // export symbols from a dynamic library. When building a dynamic library,
1117    // however, we're going to want some symbols exported, so this function
1118    // generates a DEF file which lists all the symbols.
1119    //
1120    // The linker will read this `*.def` file and export all the symbols from
1121    // the dynamic library. Note that this is not as simple as just exporting
1122    // all the symbols in the current crate (as specified by `codegen.reachable`)
1123    // but rather we also need to possibly export the symbols of upstream
1124    // crates. Upstream rlibs may be linked statically to this dynamic library,
1125    // in which case they may continue to transitively be used and hence need
1126    // their symbols exported.
1127    fn export_symbols(
1128        &mut self,
1129        tmpdir: &Path,
1130        crate_type: CrateType,
1131        symbols: &[(String, SymbolExportKind)],
1132    ) {
1133        // Symbol visibility takes care of this typically
1134        if crate_type == CrateType::Executable {
1135            let should_export_executable_symbols =
1136                self.sess.opts.unstable_opts.export_executable_symbols;
1137            if !should_export_executable_symbols {
1138                return;
1139            }
1140        }
1141
1142        let path = tmpdir.join("lib.def");
1143        let res = try {
1144            let mut f = File::create_buffered(&path)?;
1145
1146            // Start off with the standard module name header and then go
1147            // straight to exports.
1148            f.write_fmt(format_args!("LIBRARY\n"))writeln!(f, "LIBRARY")?;
1149            f.write_fmt(format_args!("EXPORTS\n"))writeln!(f, "EXPORTS")?;
1150            for (symbol, kind) in symbols {
1151                let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
1152                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1152",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(1152u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("  _{0}",
                                                    symbol) as &dyn Value))])
            });
    } else { ; }
};debug!("  _{symbol}");
1153                f.write_fmt(format_args!("  {0}{1}\n", symbol, kind_marker))writeln!(f, "  {symbol}{kind_marker}")?;
1154            }
1155        };
1156        if let Err(error) = res {
1157            self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
1158        }
1159        let mut arg = OsString::from("/DEF:");
1160        arg.push(path);
1161        self.link_arg(&arg);
1162    }
1163
1164    fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1165        let subsystem = subsystem.as_str();
1166        self.link_arg(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("/SUBSYSTEM:{0}", subsystem))
    })format!("/SUBSYSTEM:{subsystem}"));
1167
1168        // Windows has two subsystems we're interested in right now, the console
1169        // and windows subsystems. These both implicitly have different entry
1170        // points (starting symbols). The console entry point starts with
1171        // `mainCRTStartup` and the windows entry point starts with
1172        // `WinMainCRTStartup`. These entry points, defined in system libraries,
1173        // will then later probe for either `main` or `WinMain`, respectively to
1174        // start the application.
1175        //
1176        // In Rust we just always generate a `main` function so we want control
1177        // to always start there, so we force the entry point on the windows
1178        // subsystem to be `mainCRTStartup` to get everything booted up
1179        // correctly.
1180        //
1181        // For more information see RFC #1665
1182        if subsystem == "windows" {
1183            self.link_arg("/ENTRY:mainCRTStartup");
1184        }
1185    }
1186
1187    fn linker_plugin_lto(&mut self) {
1188        // Do nothing
1189    }
1190
1191    fn add_no_exec(&mut self) {
1192        self.link_arg("/NXCOMPAT");
1193    }
1194}
1195
1196struct EmLinker<'a> {
1197    cmd: Command,
1198    sess: &'a Session,
1199}
1200
1201impl<'a> Linker for EmLinker<'a> {
1202    fn cmd(&mut self) -> &mut Command {
1203        &mut self.cmd
1204    }
1205
1206    fn is_cc(&self) -> bool {
1207        true
1208    }
1209
1210    fn set_output_kind(
1211        &mut self,
1212        output_kind: LinkOutputKind,
1213        _crate_type: CrateType,
1214        _out_filename: &Path,
1215    ) {
1216        match output_kind {
1217            LinkOutputKind::DynamicNoPicExe | LinkOutputKind::DynamicPicExe => {
1218                self.cmd.arg("-sMAIN_MODULE=2");
1219            }
1220            LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1221                self.cmd.arg("-sSIDE_MODULE=2");
1222            }
1223            // -fno-pie is the default on Emscripten.
1224            LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {}
1225            LinkOutputKind::WasiReactorExe => {
1226                ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
1227            }
1228        }
1229    }
1230
1231    fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1232        // Emscripten always links statically
1233        self.link_or_cc_args(&["-l", name]);
1234    }
1235
1236    fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1237        self.link_or_cc_arg(path);
1238    }
1239
1240    fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
1241        self.link_or_cc_args(&["-l", name]);
1242    }
1243
1244    fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1245        self.link_or_cc_arg(path);
1246    }
1247
1248    fn full_relro(&mut self) {
1249        // noop
1250    }
1251
1252    fn partial_relro(&mut self) {
1253        // noop
1254    }
1255
1256    fn no_relro(&mut self) {
1257        // noop
1258    }
1259
1260    fn gc_sections(&mut self, _keep_metadata: bool) {
1261        // noop
1262    }
1263
1264    fn optimize(&mut self) {
1265        // Emscripten performs own optimizations
1266        self.cc_arg(match self.sess.opts.optimize {
1267            OptLevel::No => "-O0",
1268            OptLevel::Less => "-O1",
1269            OptLevel::More => "-O2",
1270            OptLevel::Aggressive => "-O3",
1271            OptLevel::Size => "-Os",
1272            OptLevel::SizeMin => "-Oz",
1273        });
1274    }
1275
1276    fn pgo_gen(&mut self) {
1277        // noop, but maybe we need something like the gnu linker?
1278    }
1279
1280    fn control_flow_guard(&mut self) {}
1281
1282    fn ehcont_guard(&mut self) {}
1283
1284    fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1285        // Preserve names or generate source maps depending on debug info
1286        // For more information see https://emscripten.org/docs/tools_reference/emcc.html#emcc-g
1287        self.cc_arg(match self.sess.opts.debuginfo {
1288            DebugInfo::None => "-g0",
1289            DebugInfo::Limited | DebugInfo::LineTablesOnly | DebugInfo::LineDirectivesOnly => {
1290                "--profiling-funcs"
1291            }
1292            DebugInfo::Full => "-g",
1293        });
1294    }
1295
1296    fn no_crt_objects(&mut self) {}
1297
1298    fn no_default_libraries(&mut self) {
1299        self.cc_arg("-nodefaultlibs");
1300    }
1301
1302    fn export_symbols(
1303        &mut self,
1304        _tmpdir: &Path,
1305        _crate_type: CrateType,
1306        symbols: &[(String, SymbolExportKind)],
1307    ) {
1308        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1308",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(1308u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("EXPORTED SYMBOLS:")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("EXPORTED SYMBOLS:");
1309
1310        self.cc_arg("-s");
1311
1312        let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
1313        let encoded = serde_json::to_string(
1314            &symbols.iter().map(|(sym, _)| "_".to_owned() + sym).collect::<Vec<_>>(),
1315        )
1316        .unwrap();
1317        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1317",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(1317u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("{0}",
                                                    encoded) as &dyn Value))])
            });
    } else { ; }
};debug!("{encoded}");
1318
1319        arg.push(encoded);
1320
1321        self.cc_arg(arg);
1322    }
1323
1324    fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {
1325        // noop
1326    }
1327
1328    fn linker_plugin_lto(&mut self) {
1329        // Do nothing
1330    }
1331}
1332
1333struct WasmLd<'a> {
1334    cmd: Command,
1335    sess: &'a Session,
1336}
1337
1338impl<'a> WasmLd<'a> {
1339    fn new(cmd: Command, sess: &'a Session) -> WasmLd<'a> {
1340        WasmLd { cmd, sess }
1341    }
1342}
1343
1344impl<'a> Linker for WasmLd<'a> {
1345    fn cmd(&mut self) -> &mut Command {
1346        &mut self.cmd
1347    }
1348
1349    fn set_output_kind(
1350        &mut self,
1351        output_kind: LinkOutputKind,
1352        _crate_type: CrateType,
1353        _out_filename: &Path,
1354    ) {
1355        match output_kind {
1356            LinkOutputKind::DynamicNoPicExe
1357            | LinkOutputKind::DynamicPicExe
1358            | LinkOutputKind::StaticNoPicExe
1359            | LinkOutputKind::StaticPicExe => {}
1360            LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1361                self.link_arg("--no-entry");
1362            }
1363            LinkOutputKind::WasiReactorExe => {
1364                self.link_args(&["--entry", "_initialize"]);
1365            }
1366        }
1367    }
1368
1369    fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1370        self.link_or_cc_args(&["-l", name]);
1371    }
1372
1373    fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1374        self.link_or_cc_arg(path);
1375    }
1376
1377    fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1378        if !whole_archive {
1379            self.link_or_cc_args(&["-l", name]);
1380        } else {
1381            self.link_arg("--whole-archive")
1382                .link_or_cc_args(&["-l", name])
1383                .link_arg("--no-whole-archive");
1384        }
1385    }
1386
1387    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1388        if !whole_archive {
1389            self.link_or_cc_arg(path);
1390        } else {
1391            self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1392        }
1393    }
1394
1395    fn full_relro(&mut self) {}
1396
1397    fn partial_relro(&mut self) {}
1398
1399    fn no_relro(&mut self) {}
1400
1401    fn gc_sections(&mut self, _keep_metadata: bool) {
1402        self.link_arg("--gc-sections");
1403    }
1404
1405    fn optimize(&mut self) {
1406        // The -O flag is, as of late 2023, only used for merging of strings and debuginfo, and
1407        // only differentiates -O0 and -O1. It does not apply to LTO.
1408        self.link_arg(match self.sess.opts.optimize {
1409            OptLevel::No => "-O0",
1410            OptLevel::Less => "-O1",
1411            OptLevel::More => "-O2",
1412            OptLevel::Aggressive => "-O3",
1413            // Currently LLD doesn't support `Os` and `Oz`, so pass through `O2`
1414            // instead.
1415            OptLevel::Size => "-O2",
1416            OptLevel::SizeMin => "-O2",
1417        });
1418    }
1419
1420    fn pgo_gen(&mut self) {}
1421
1422    fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1423        match strip {
1424            Strip::None => {}
1425            Strip::Debuginfo => {
1426                self.link_arg("--strip-debug");
1427            }
1428            Strip::Symbols => {
1429                self.link_arg("--strip-all");
1430            }
1431        }
1432    }
1433
1434    fn control_flow_guard(&mut self) {}
1435
1436    fn ehcont_guard(&mut self) {}
1437
1438    fn no_crt_objects(&mut self) {}
1439
1440    fn no_default_libraries(&mut self) {}
1441
1442    fn export_symbols(
1443        &mut self,
1444        _tmpdir: &Path,
1445        _crate_type: CrateType,
1446        symbols: &[(String, SymbolExportKind)],
1447    ) {
1448        for (sym, _) in symbols {
1449            self.link_args(&["--export", sym]);
1450        }
1451
1452        // LLD will hide these otherwise-internal symbols since it only exports
1453        // symbols explicitly passed via the `--export` flags above and hides all
1454        // others. Various bits and pieces of wasm32-unknown-unknown tooling use
1455        // this, so be sure these symbols make their way out of the linker as well.
1456        if #[allow(non_exhaustive_omitted_patterns)] match self.sess.target.os {
    Os::Unknown | Os::None => true,
    _ => false,
}matches!(self.sess.target.os, Os::Unknown | Os::None) {
1457            self.link_args(&["--export=__heap_base", "--export=__data_end"]);
1458        }
1459    }
1460
1461    fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1462
1463    fn linker_plugin_lto(&mut self) {
1464        match self.sess.opts.cg.linker_plugin_lto {
1465            LinkerPluginLto::Disabled => {
1466                // Nothing to do
1467            }
1468            LinkerPluginLto::LinkerPluginAuto => {
1469                self.push_linker_plugin_lto_args();
1470            }
1471            LinkerPluginLto::LinkerPlugin(_) => {
1472                self.push_linker_plugin_lto_args();
1473            }
1474        }
1475    }
1476}
1477
1478impl<'a> WasmLd<'a> {
1479    fn push_linker_plugin_lto_args(&mut self) {
1480        let opt_level = match self.sess.opts.optimize {
1481            config::OptLevel::No => "O0",
1482            config::OptLevel::Less => "O1",
1483            config::OptLevel::More => "O2",
1484            config::OptLevel::Aggressive => "O3",
1485            // wasm-ld only handles integer LTO opt levels. Use O2
1486            config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
1487        };
1488        self.link_arg(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("--lto-{0}", opt_level))
    })format!("--lto-{opt_level}"));
1489    }
1490}
1491
1492/// Linker shepherd script for L4Re (Fiasco)
1493struct L4Bender<'a> {
1494    cmd: Command,
1495    sess: &'a Session,
1496    hinted_static: bool,
1497}
1498
1499impl<'a> Linker for L4Bender<'a> {
1500    fn cmd(&mut self) -> &mut Command {
1501        &mut self.cmd
1502    }
1503
1504    fn set_output_kind(
1505        &mut self,
1506        _output_kind: LinkOutputKind,
1507        _crate_type: CrateType,
1508        _out_filename: &Path,
1509    ) {
1510    }
1511
1512    fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1513        self.hint_static();
1514        if !whole_archive {
1515            self.link_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-PC{0}", name))
    })format!("-PC{name}"));
1516        } else {
1517            self.link_arg("--whole-archive")
1518                .link_or_cc_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-l{0}", name))
    })format!("-l{name}"))
1519                .link_arg("--no-whole-archive");
1520        }
1521    }
1522
1523    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1524        self.hint_static();
1525        if !whole_archive {
1526            self.link_or_cc_arg(path);
1527        } else {
1528            self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1529        }
1530    }
1531
1532    fn full_relro(&mut self) {
1533        self.link_args(&["-z", "relro", "-z", "now"]);
1534    }
1535
1536    fn partial_relro(&mut self) {
1537        self.link_args(&["-z", "relro"]);
1538    }
1539
1540    fn no_relro(&mut self) {
1541        self.link_args(&["-z", "norelro"]);
1542    }
1543
1544    fn gc_sections(&mut self, keep_metadata: bool) {
1545        if !keep_metadata {
1546            self.link_arg("--gc-sections");
1547        }
1548    }
1549
1550    fn optimize(&mut self) {
1551        // GNU-style linkers support optimization with -O. GNU ld doesn't
1552        // need a numeric argument, but other linkers do.
1553        if self.sess.opts.optimize == config::OptLevel::More
1554            || self.sess.opts.optimize == config::OptLevel::Aggressive
1555        {
1556            self.link_arg("-O1");
1557        }
1558    }
1559
1560    fn pgo_gen(&mut self) {}
1561
1562    fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1563        match strip {
1564            Strip::None => {}
1565            Strip::Debuginfo => {
1566                self.link_arg("--strip-debug");
1567            }
1568            Strip::Symbols => {
1569                self.link_arg("--strip-all");
1570            }
1571        }
1572    }
1573
1574    fn no_default_libraries(&mut self) {
1575        self.cc_arg("-nostdlib");
1576    }
1577
1578    fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[(String, SymbolExportKind)]) {
1579        // ToDo, not implemented, copy from GCC
1580        self.sess.dcx().emit_warn(errors::L4BenderExportingSymbolsUnimplemented);
1581    }
1582
1583    fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1584        let subsystem = subsystem.as_str();
1585        self.link_arg(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("--subsystem {0}", subsystem))
    })format!("--subsystem {subsystem}"));
1586    }
1587
1588    fn reset_per_library_state(&mut self) {
1589        self.hint_static(); // Reset to default before returning the composed command line.
1590    }
1591
1592    fn linker_plugin_lto(&mut self) {}
1593
1594    fn control_flow_guard(&mut self) {}
1595
1596    fn ehcont_guard(&mut self) {}
1597
1598    fn no_crt_objects(&mut self) {}
1599}
1600
1601impl<'a> L4Bender<'a> {
1602    fn new(cmd: Command, sess: &'a Session) -> L4Bender<'a> {
1603        L4Bender { cmd, sess, hinted_static: false }
1604    }
1605
1606    fn hint_static(&mut self) {
1607        if !self.hinted_static {
1608            self.link_or_cc_arg("-static");
1609            self.hinted_static = true;
1610        }
1611    }
1612}
1613
1614/// Linker for AIX.
1615struct AixLinker<'a> {
1616    cmd: Command,
1617    sess: &'a Session,
1618    hinted_static: Option<bool>,
1619}
1620
1621impl<'a> AixLinker<'a> {
1622    fn new(cmd: Command, sess: &'a Session) -> AixLinker<'a> {
1623        AixLinker { cmd, sess, hinted_static: None }
1624    }
1625
1626    fn hint_static(&mut self) {
1627        if self.hinted_static != Some(true) {
1628            self.link_arg("-bstatic");
1629            self.hinted_static = Some(true);
1630        }
1631    }
1632
1633    fn hint_dynamic(&mut self) {
1634        if self.hinted_static != Some(false) {
1635            self.link_arg("-bdynamic");
1636            self.hinted_static = Some(false);
1637        }
1638    }
1639
1640    fn build_dylib(&mut self, _out_filename: &Path) {
1641        self.link_args(&["-bM:SRE", "-bnoentry"]);
1642        // FIXME: Use CreateExportList utility to create export list
1643        // and remove -bexpfull.
1644        self.link_arg("-bexpfull");
1645    }
1646}
1647
1648impl<'a> Linker for AixLinker<'a> {
1649    fn cmd(&mut self) -> &mut Command {
1650        &mut self.cmd
1651    }
1652
1653    fn set_output_kind(
1654        &mut self,
1655        output_kind: LinkOutputKind,
1656        _crate_type: CrateType,
1657        out_filename: &Path,
1658    ) {
1659        match output_kind {
1660            LinkOutputKind::DynamicDylib => {
1661                self.hint_dynamic();
1662                self.build_dylib(out_filename);
1663            }
1664            LinkOutputKind::StaticDylib => {
1665                self.hint_static();
1666                self.build_dylib(out_filename);
1667            }
1668            _ => {}
1669        }
1670    }
1671
1672    fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
1673        self.hint_dynamic();
1674        self.link_or_cc_arg(if verbatim { String::from(name) } else { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-l{0}", name))
    })format!("-l{name}") });
1675    }
1676
1677    fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1678        self.hint_dynamic();
1679        self.link_or_cc_arg(path);
1680    }
1681
1682    fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
1683        self.hint_static();
1684        if !whole_archive {
1685            self.link_or_cc_arg(if verbatim { String::from(name) } else { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-l{0}", name))
    })format!("-l{name}") });
1686        } else {
1687            let mut arg = OsString::from("-bkeepfile:");
1688            arg.push(find_native_static_library(name, verbatim, self.sess));
1689            self.link_or_cc_arg(arg);
1690        }
1691    }
1692
1693    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1694        self.hint_static();
1695        if !whole_archive {
1696            self.link_or_cc_arg(path);
1697        } else {
1698            let mut arg = OsString::from("-bkeepfile:");
1699            arg.push(path);
1700            self.link_arg(arg);
1701        }
1702    }
1703
1704    fn full_relro(&mut self) {}
1705
1706    fn partial_relro(&mut self) {}
1707
1708    fn no_relro(&mut self) {}
1709
1710    fn gc_sections(&mut self, _keep_metadata: bool) {
1711        self.link_arg("-bgc");
1712    }
1713
1714    fn optimize(&mut self) {}
1715
1716    fn pgo_gen(&mut self) {
1717        self.link_arg("-bdbg:namedsects:ss");
1718        self.link_arg("-u");
1719        self.link_arg("__llvm_profile_runtime");
1720    }
1721
1722    fn control_flow_guard(&mut self) {}
1723
1724    fn ehcont_guard(&mut self) {}
1725
1726    fn debuginfo(&mut self, _: Strip, _: &[PathBuf]) {}
1727
1728    fn no_crt_objects(&mut self) {}
1729
1730    fn no_default_libraries(&mut self) {}
1731
1732    fn export_symbols(
1733        &mut self,
1734        tmpdir: &Path,
1735        _crate_type: CrateType,
1736        symbols: &[(String, SymbolExportKind)],
1737    ) {
1738        let path = tmpdir.join("list.exp");
1739        let res = try {
1740            let mut f = File::create_buffered(&path)?;
1741            // FIXME: use llvm-nm to generate export list.
1742            for (symbol, _) in symbols {
1743                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1743",
                        "rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
                        ::tracing_core::__macro_support::Option::Some(1743u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("  _{0}",
                                                    symbol) as &dyn Value))])
            });
    } else { ; }
};debug!("  _{symbol}");
1744                f.write_fmt(format_args!("  {0}\n", symbol))writeln!(f, "  {symbol}")?;
1745            }
1746        };
1747        if let Err(e) = res {
1748            self.sess.dcx().fatal(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("failed to write export file: {0}",
                e))
    })format!("failed to write export file: {e}"));
1749        }
1750        self.link_arg(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-bE:{0}", path.to_str().unwrap()))
    })format!("-bE:{}", path.to_str().unwrap()));
1751    }
1752
1753    fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1754
1755    fn reset_per_library_state(&mut self) {
1756        self.hint_dynamic();
1757    }
1758
1759    fn linker_plugin_lto(&mut self) {}
1760
1761    fn add_eh_frame_header(&mut self) {}
1762
1763    fn add_no_exec(&mut self) {}
1764
1765    fn add_as_needed(&mut self) {}
1766}
1767
1768fn for_each_exported_symbols_include_dep<'tcx>(
1769    tcx: TyCtxt<'tcx>,
1770    crate_type: CrateType,
1771    mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum),
1772) {
1773    let formats = tcx.dependency_formats(());
1774    let deps = &formats[&crate_type];
1775
1776    for (cnum, dep_format) in deps.iter_enumerated() {
1777        // For each dependency that we are linking to statically ...
1778        if *dep_format == Linkage::Static {
1779            for &(symbol, info) in tcx.exported_non_generic_symbols(cnum).iter() {
1780                callback(symbol, info, cnum);
1781            }
1782            for &(symbol, info) in tcx.exported_generic_symbols(cnum).iter() {
1783                callback(symbol, info, cnum);
1784            }
1785        }
1786    }
1787}
1788
1789pub(crate) fn exported_symbols(
1790    tcx: TyCtxt<'_>,
1791    crate_type: CrateType,
1792) -> Vec<(String, SymbolExportKind)> {
1793    if let Some(ref exports) = tcx.sess.target.override_export_symbols {
1794        return exports
1795            .iter()
1796            .map(|name| {
1797                (
1798                    name.to_string(),
1799                    // FIXME use the correct export kind for this symbol. override_export_symbols
1800                    // can't directly specify the SymbolExportKind as it is defined in rustc_middle
1801                    // which rustc_target can't depend on.
1802                    SymbolExportKind::Text,
1803                )
1804            })
1805            .collect();
1806    }
1807
1808    let mut symbols = if let CrateType::ProcMacro = crate_type {
1809        exported_symbols_for_proc_macro_crate(tcx)
1810    } else {
1811        exported_symbols_for_non_proc_macro(tcx, crate_type)
1812    };
1813
1814    if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
1815        let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx);
1816        symbols.push((metadata_symbol_name, SymbolExportKind::Data));
1817    }
1818
1819    symbols
1820}
1821
1822fn exported_symbols_for_non_proc_macro(
1823    tcx: TyCtxt<'_>,
1824    crate_type: CrateType,
1825) -> Vec<(String, SymbolExportKind)> {
1826    let mut symbols = Vec::new();
1827    let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1828    for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1829        // Do not export mangled symbols from cdylibs and don't attempt to export compiler-builtins
1830        // from any dylib. The latter doesn't work anyway as we use hidden visibility for
1831        // compiler-builtins. Most linkers silently ignore it, but ld64 gives a warning.
1832        if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum) {
1833            symbols.push((
1834                symbol_export::exporting_symbol_name_for_instance_in_crate(tcx, symbol, cnum),
1835                info.kind,
1836            ));
1837            symbol_export::extend_exported_symbols(&mut symbols, tcx, symbol, cnum);
1838        }
1839    });
1840
1841    // Mark allocator shim symbols as exported only if they were generated.
1842    if export_threshold == SymbolExportLevel::Rust
1843        && needs_allocator_shim_for_linking(tcx.dependency_formats(()), crate_type)
1844        && let Some(kind) = tcx.allocator_kind(())
1845    {
1846        symbols.extend(allocator_shim_symbols(tcx, kind));
1847    }
1848
1849    symbols
1850}
1851
1852fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<(String, SymbolExportKind)> {
1853    // `exported_symbols` will be empty when !should_codegen.
1854    if !tcx.sess.opts.output_types.should_codegen() {
1855        return Vec::new();
1856    }
1857
1858    let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
1859    let proc_macro_decls_name = tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
1860
1861    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(proc_macro_decls_name, SymbolExportKind::Data)]))vec![(proc_macro_decls_name, SymbolExportKind::Data)]
1862}
1863
1864pub(crate) fn linked_symbols(
1865    tcx: TyCtxt<'_>,
1866    crate_type: CrateType,
1867) -> Vec<(String, SymbolExportKind)> {
1868    match crate_type {
1869        CrateType::Executable
1870        | CrateType::ProcMacro
1871        | CrateType::Cdylib
1872        | CrateType::Dylib
1873        | CrateType::Sdylib => (),
1874        CrateType::StaticLib | CrateType::Rlib => {
1875            // These are not linked, so no need to generate symbols.o for them.
1876            return Vec::new();
1877        }
1878    }
1879
1880    match tcx.sess.lto() {
1881        Lto::No | Lto::ThinLocal => {}
1882        Lto::Thin | Lto::Fat => {
1883            // We really only need symbols from upstream rlibs to end up in the linked symbols list.
1884            // The rest are in separate object files which the linker will always link in and
1885            // doesn't have rules around the order in which they need to appear.
1886            // When doing LTO, some of the symbols in the linked symbols list happen to be
1887            // internalized by LTO, which then prevents referencing them from symbols.o. When doing
1888            // LTO, all object files that get linked in will be local object files rather than
1889            // pulled in from rlibs, so an empty linked symbols list works fine to avoid referencing
1890            // all those internalized symbols from symbols.o.
1891            return Vec::new();
1892        }
1893    }
1894
1895    let mut symbols = Vec::new();
1896
1897    let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1898    for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1899        if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum)
1900            || info.used
1901            || info.rustc_std_internal_symbol
1902        {
1903            symbols.push((
1904                symbol_export::linking_symbol_name_for_instance_in_crate(
1905                    tcx, symbol, info.kind, cnum,
1906                ),
1907                info.kind,
1908            ));
1909        }
1910    });
1911
1912    symbols
1913}
1914
1915/// Much simplified and explicit CLI for the NVPTX linker. The linker operates
1916/// with bitcode and uses LLVM backend to generate a PTX assembly.
1917struct PtxLinker<'a> {
1918    cmd: Command,
1919    sess: &'a Session,
1920}
1921
1922impl<'a> Linker for PtxLinker<'a> {
1923    fn cmd(&mut self) -> &mut Command {
1924        &mut self.cmd
1925    }
1926
1927    fn set_output_kind(
1928        &mut self,
1929        _output_kind: LinkOutputKind,
1930        _crate_type: CrateType,
1931        _out_filename: &Path,
1932    ) {
1933    }
1934
1935    fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
1936        { ::core::panicking::panic_fmt(format_args!("staticlibs not supported")); }panic!("staticlibs not supported")
1937    }
1938
1939    fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1940        self.link_arg("--rlib").link_arg(path);
1941    }
1942
1943    fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1944        self.link_arg("--debug");
1945    }
1946
1947    fn add_object(&mut self, path: &Path) {
1948        self.link_arg("--bitcode").link_arg(path);
1949    }
1950
1951    fn optimize(&mut self) {
1952        match self.sess.lto() {
1953            Lto::Thin | Lto::Fat | Lto::ThinLocal => {
1954                self.link_arg("-Olto");
1955            }
1956
1957            Lto::No => {}
1958        }
1959    }
1960
1961    fn full_relro(&mut self) {}
1962
1963    fn partial_relro(&mut self) {}
1964
1965    fn no_relro(&mut self) {}
1966
1967    fn gc_sections(&mut self, _keep_metadata: bool) {}
1968
1969    fn pgo_gen(&mut self) {}
1970
1971    fn no_crt_objects(&mut self) {}
1972
1973    fn no_default_libraries(&mut self) {}
1974
1975    fn control_flow_guard(&mut self) {}
1976
1977    fn ehcont_guard(&mut self) {}
1978
1979    fn export_symbols(
1980        &mut self,
1981        _tmpdir: &Path,
1982        _crate_type: CrateType,
1983        _symbols: &[(String, SymbolExportKind)],
1984    ) {
1985    }
1986
1987    fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1988
1989    fn linker_plugin_lto(&mut self) {}
1990}
1991
1992/// The `self-contained` LLVM bitcode linker
1993struct LlbcLinker<'a> {
1994    cmd: Command,
1995    sess: &'a Session,
1996}
1997
1998impl<'a> Linker for LlbcLinker<'a> {
1999    fn cmd(&mut self) -> &mut Command {
2000        &mut self.cmd
2001    }
2002
2003    fn set_output_kind(
2004        &mut self,
2005        _output_kind: LinkOutputKind,
2006        _crate_type: CrateType,
2007        _out_filename: &Path,
2008    ) {
2009    }
2010
2011    fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
2012        { ::core::panicking::panic_fmt(format_args!("staticlibs not supported")); }panic!("staticlibs not supported")
2013    }
2014
2015    fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2016        self.link_or_cc_arg(path);
2017    }
2018
2019    fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2020        self.link_arg("--debug");
2021    }
2022
2023    fn optimize(&mut self) {
2024        self.link_arg(match self.sess.opts.optimize {
2025            OptLevel::No => "-O0",
2026            OptLevel::Less => "-O1",
2027            OptLevel::More => "-O2",
2028            OptLevel::Aggressive => "-O3",
2029            OptLevel::Size => "-Os",
2030            OptLevel::SizeMin => "-Oz",
2031        });
2032    }
2033
2034    fn full_relro(&mut self) {}
2035
2036    fn partial_relro(&mut self) {}
2037
2038    fn no_relro(&mut self) {}
2039
2040    fn gc_sections(&mut self, _keep_metadata: bool) {}
2041
2042    fn pgo_gen(&mut self) {}
2043
2044    fn no_crt_objects(&mut self) {}
2045
2046    fn no_default_libraries(&mut self) {}
2047
2048    fn control_flow_guard(&mut self) {}
2049
2050    fn ehcont_guard(&mut self) {}
2051
2052    fn export_symbols(
2053        &mut self,
2054        _tmpdir: &Path,
2055        _crate_type: CrateType,
2056        symbols: &[(String, SymbolExportKind)],
2057    ) {
2058        match _crate_type {
2059            CrateType::Cdylib => {
2060                for (sym, _) in symbols {
2061                    self.link_args(&["--export-symbol", sym]);
2062                }
2063            }
2064            _ => (),
2065        }
2066    }
2067
2068    fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
2069
2070    fn linker_plugin_lto(&mut self) {}
2071}
2072
2073struct BpfLinker<'a> {
2074    cmd: Command,
2075    sess: &'a Session,
2076}
2077
2078impl<'a> Linker for BpfLinker<'a> {
2079    fn cmd(&mut self) -> &mut Command {
2080        &mut self.cmd
2081    }
2082
2083    fn set_output_kind(
2084        &mut self,
2085        _output_kind: LinkOutputKind,
2086        _crate_type: CrateType,
2087        _out_filename: &Path,
2088    ) {
2089    }
2090
2091    fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
2092        self.sess.dcx().emit_fatal(errors::BpfStaticlibNotSupported)
2093    }
2094
2095    fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2096        self.link_or_cc_arg(path);
2097    }
2098
2099    fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2100        self.link_arg("--debug");
2101    }
2102
2103    fn optimize(&mut self) {
2104        self.link_arg(match self.sess.opts.optimize {
2105            OptLevel::No => "-O0",
2106            OptLevel::Less => "-O1",
2107            OptLevel::More => "-O2",
2108            OptLevel::Aggressive => "-O3",
2109            OptLevel::Size => "-Os",
2110            OptLevel::SizeMin => "-Oz",
2111        });
2112    }
2113
2114    fn full_relro(&mut self) {}
2115
2116    fn partial_relro(&mut self) {}
2117
2118    fn no_relro(&mut self) {}
2119
2120    fn gc_sections(&mut self, _keep_metadata: bool) {}
2121
2122    fn pgo_gen(&mut self) {}
2123
2124    fn no_crt_objects(&mut self) {}
2125
2126    fn no_default_libraries(&mut self) {}
2127
2128    fn control_flow_guard(&mut self) {}
2129
2130    fn ehcont_guard(&mut self) {}
2131
2132    fn export_symbols(
2133        &mut self,
2134        tmpdir: &Path,
2135        _crate_type: CrateType,
2136        symbols: &[(String, SymbolExportKind)],
2137    ) {
2138        let path = tmpdir.join("symbols");
2139        let res = try {
2140            let mut f = File::create_buffered(&path)?;
2141            for (sym, _) in symbols {
2142                f.write_fmt(format_args!("{0}\n", sym))writeln!(f, "{sym}")?;
2143            }
2144        };
2145        if let Err(error) = res {
2146            self.sess.dcx().emit_fatal(errors::SymbolFileWriteFailure { error });
2147        } else {
2148            self.link_arg("--export-symbols").link_arg(&path);
2149        }
2150    }
2151
2152    fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
2153
2154    fn linker_plugin_lto(&mut self) {}
2155}