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