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