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 find_msvc_tools;
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::{
15 self, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
16};
17use rustc_middle::ty::TyCtxt;
18use rustc_session::Session;
19use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
20use rustc_target::spec::{Cc, LinkOutputKind, LinkerFlavor, Lld};
21use tracing::{debug, warn};
22
23use super::command::Command;
24use super::symbol_export;
25use crate::back::symbol_export::allocator_shim_symbols;
26use crate::base::needs_allocator_shim_for_linking;
27use crate::errors;
28
29#[cfg(test)]
30mod tests;
31
32pub(crate) fn disable_localization(linker: &mut Command) {
38 linker.env("LC_ALL", "C");
41 linker.env("VSLANG", "1033");
43}
44
45pub(crate) fn get_linker<'a>(
49 sess: &'a Session,
50 linker: &Path,
51 flavor: LinkerFlavor,
52 self_contained: bool,
53 target_cpu: &'a str,
54 codegen_backend: &'static str,
55) -> Box<dyn Linker + 'a> {
56 let msvc_tool = find_msvc_tools::find_tool(&sess.target.arch, "link.exe");
57
58 let mut cmd = match linker.to_str() {
67 Some(linker) if cfg!(windows) && linker.ends_with(".bat") => Command::bat_script(linker),
68 _ => match flavor {
69 LinkerFlavor::Gnu(Cc::No, Lld::Yes)
70 | LinkerFlavor::Darwin(Cc::No, Lld::Yes)
71 | LinkerFlavor::WasmLld(Cc::No)
72 | LinkerFlavor::Msvc(Lld::Yes) => Command::lld(linker, flavor.lld_flavor()),
73 LinkerFlavor::Msvc(Lld::No)
74 if sess.opts.cg.linker.is_none() && sess.target.linker.is_none() =>
75 {
76 Command::new(msvc_tool.as_ref().map_or(linker, |t| t.path()))
77 }
78 _ => Command::new(linker),
79 },
80 };
81
82 let t = &sess.target;
86 if matches!(flavor, LinkerFlavor::Msvc(..)) && t.vendor == "uwp" {
87 if let Some(ref tool) = msvc_tool {
88 let original_path = tool.path();
89 if let Some(root_lib_path) = original_path.ancestors().nth(4) {
90 let arch = match t.arch.as_ref() {
91 "x86_64" => Some("x64"),
92 "x86" => Some("x86"),
93 "aarch64" => Some("arm64"),
94 "arm" => Some("arm"),
95 _ => None,
96 };
97 if let Some(ref a) = arch {
98 let mut arg = OsString::from("/LIBPATH:");
100 arg.push(format!("{}\\lib\\{}\\store", root_lib_path.display(), a));
101 cmd.arg(&arg);
102 } else {
103 warn!("arch is not supported");
104 }
105 } else {
106 warn!("MSVC root path lib location not found");
107 }
108 } else {
109 warn!("link.exe not found");
110 }
111 }
112
113 let mut new_path = sess.get_tools_search_paths(self_contained);
116 let mut msvc_changed_path = false;
117 if sess.target.is_like_msvc
118 && let Some(ref tool) = msvc_tool
119 {
120 for (k, v) in tool.env() {
121 if k == "PATH" {
122 new_path.extend(env::split_paths(v));
123 msvc_changed_path = true;
124 } else {
125 cmd.env(k, v);
126 }
127 }
128 }
129
130 if !msvc_changed_path && let Some(path) = env::var_os("PATH") {
131 new_path.extend(env::split_paths(&path));
132 }
133 cmd.env("PATH", env::join_paths(new_path).unwrap());
134
135 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 uses_lld: flavor.uses_lld(),
157 codegen_backend,
158 }) as Box<dyn Linker>,
159 LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>,
160 LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
161 LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
162 LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box<dyn Linker>,
163 LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
164 }
165}
166
167fn verbatim_args<L: Linker + ?Sized>(
177 l: &mut L,
178 args: impl IntoIterator<Item: AsRef<OsStr>>,
179) -> &mut L {
180 for arg in args {
181 l.cmd().arg(arg);
182 }
183 l
184}
185fn convert_link_args_to_cc_args(cmd: &mut Command, args: impl IntoIterator<Item: AsRef<OsStr>>) {
188 let mut combined_arg = OsString::from("-Wl");
189 for arg in args {
190 if arg.as_ref().as_encoded_bytes().contains(&b',') {
193 if combined_arg != OsStr::new("-Wl") {
195 cmd.arg(combined_arg);
196 combined_arg = OsString::from("-Wl");
198 }
199
200 cmd.arg("-Xlinker");
202 cmd.arg(arg);
203 } else {
204 combined_arg.push(",");
206 combined_arg.push(arg);
207 }
208 }
209 if combined_arg != OsStr::new("-Wl") {
211 cmd.arg(combined_arg);
212 }
213}
214fn link_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
217 if !l.is_cc() {
218 verbatim_args(l, args);
219 } else {
220 convert_link_args_to_cc_args(l.cmd(), args);
221 }
222 l
223}
224fn cc_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
227 assert!(l.is_cc());
228 verbatim_args(l, args)
229}
230fn link_or_cc_args<L: Linker + ?Sized>(
232 l: &mut L,
233 args: impl IntoIterator<Item: AsRef<OsStr>>,
234) -> &mut L {
235 verbatim_args(l, args)
236}
237
238macro_rules! generate_arg_methods {
239 ($($ty:ty)*) => { $(
240 impl $ty {
241 #[allow(unused)]
242 pub(crate) fn verbatim_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
243 verbatim_args(self, args)
244 }
245 #[allow(unused)]
246 pub(crate) fn verbatim_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
247 verbatim_args(self, iter::once(arg))
248 }
249 #[allow(unused)]
250 pub(crate) fn link_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
251 link_args(self, args)
252 }
253 #[allow(unused)]
254 pub(crate) fn link_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
255 link_args(self, iter::once(arg))
256 }
257 #[allow(unused)]
258 pub(crate) fn cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
259 cc_args(self, args)
260 }
261 #[allow(unused)]
262 pub(crate) fn cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
263 cc_args(self, iter::once(arg))
264 }
265 #[allow(unused)]
266 pub(crate) fn link_or_cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
267 link_or_cc_args(self, args)
268 }
269 #[allow(unused)]
270 pub(crate) fn link_or_cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
271 link_or_cc_args(self, iter::once(arg))
272 }
273 }
274 )* }
275}
276
277generate_arg_methods! {
278 GccLinker<'_>
279 MsvcLinker<'_>
280 EmLinker<'_>
281 WasmLd<'_>
282 L4Bender<'_>
283 AixLinker<'_>
284 LlbcLinker<'_>
285 PtxLinker<'_>
286 BpfLinker<'_>
287 dyn Linker + '_
288}
289
290pub(crate) trait Linker {
298 fn cmd(&mut self) -> &mut Command;
299 fn is_cc(&self) -> bool {
300 false
301 }
302 fn set_output_kind(
303 &mut self,
304 output_kind: LinkOutputKind,
305 crate_type: CrateType,
306 out_filename: &Path,
307 );
308 fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
309 bug!("dylib linked with unsupported linker")
310 }
311 fn link_dylib_by_path(&mut self, _path: &Path, _as_needed: bool) {
312 bug!("dylib linked with unsupported linker")
313 }
314 fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
315 bug!("framework linked with unsupported linker")
316 }
317 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool);
318 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
319 fn include_path(&mut self, path: &Path) {
320 link_or_cc_args(link_or_cc_args(self, &["-L"]), &[path]);
321 }
322 fn framework_path(&mut self, _path: &Path) {
323 bug!("framework path set with unsupported linker")
324 }
325 fn output_filename(&mut self, path: &Path) {
326 link_or_cc_args(link_or_cc_args(self, &["-o"]), &[path]);
327 }
328 fn add_object(&mut self, path: &Path) {
329 link_or_cc_args(self, &[path]);
330 }
331 fn gc_sections(&mut self, keep_metadata: bool);
332 fn full_relro(&mut self);
333 fn partial_relro(&mut self);
334 fn no_relro(&mut self);
335 fn optimize(&mut self);
336 fn pgo_gen(&mut self);
337 fn control_flow_guard(&mut self);
338 fn ehcont_guard(&mut self);
339 fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]);
340 fn no_crt_objects(&mut self);
341 fn no_default_libraries(&mut self);
342 fn export_symbols(
343 &mut self,
344 tmpdir: &Path,
345 crate_type: CrateType,
346 symbols: &[(String, SymbolExportKind)],
347 );
348 fn subsystem(&mut self, subsystem: &str);
349 fn linker_plugin_lto(&mut self);
350 fn add_eh_frame_header(&mut self) {}
351 fn add_no_exec(&mut self) {}
352 fn add_as_needed(&mut self) {}
353 fn reset_per_library_state(&mut self) {}
354}
355
356impl dyn Linker + '_ {
357 pub(crate) fn take_cmd(&mut self) -> Command {
358 mem::replace(self.cmd(), Command::new(""))
359 }
360}
361
362struct GccLinker<'a> {
363 cmd: Command,
364 sess: &'a Session,
365 target_cpu: &'a str,
366 hinted_static: Option<bool>, is_ld: bool,
369 is_gnu: bool,
370 uses_lld: bool,
371 codegen_backend: &'static str,
372}
373
374impl<'a> GccLinker<'a> {
375 fn takes_hints(&self) -> bool {
376 !self.sess.target.is_like_darwin && !self.sess.target.is_like_wasm
385 }
386
387 fn hint_static(&mut self) {
392 if !self.takes_hints() {
393 return;
394 }
395 if self.hinted_static != Some(true) {
396 self.link_arg("-Bstatic");
397 self.hinted_static = Some(true);
398 }
399 }
400
401 fn hint_dynamic(&mut self) {
402 if !self.takes_hints() {
403 return;
404 }
405 if self.hinted_static != Some(false) {
406 self.link_arg("-Bdynamic");
407 self.hinted_static = Some(false);
408 }
409 }
410
411 fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
412 if let Some(plugin_path) = plugin_path {
413 let mut arg = OsString::from("-plugin=");
414 arg.push(plugin_path);
415 self.link_arg(&arg);
416 }
417
418 let opt_level = match self.sess.opts.optimize {
419 config::OptLevel::No => "O0",
420 config::OptLevel::Less => "O1",
421 config::OptLevel::More | config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
422 config::OptLevel::Aggressive => "O3",
423 };
424
425 if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
426 self.link_arg(&format!("-plugin-opt=sample-profile={}", path.display()));
427 };
428 let prefix = if self.codegen_backend == "gcc" {
429 "-"
431 } else {
432 ""
433 };
434 self.link_args(&[
435 &format!("-plugin-opt={prefix}{opt_level}"),
436 &format!("-plugin-opt={prefix}mcpu={}", self.target_cpu),
437 ]);
438 }
439
440 fn build_dylib(&mut self, crate_type: CrateType, out_filename: &Path) {
441 if self.sess.target.is_like_darwin {
443 if self.is_cc() {
444 self.cc_arg("-dynamiclib");
446 } else {
447 self.link_arg("-dylib");
448 }
450
451 if self.sess.opts.cg.rpath || self.sess.opts.unstable_opts.osx_rpath_install_name {
456 let mut rpath = OsString::from("@rpath/");
457 rpath.push(out_filename.file_name().unwrap());
458 self.link_arg("-install_name").link_arg(rpath);
459 }
460 } else {
461 self.link_or_cc_arg("-shared");
462 if let Some(name) = out_filename.file_name() {
463 if self.sess.target.is_like_windows {
464 let (prefix, suffix) = self.sess.staticlib_components(false);
468 let mut implib_name = OsString::from(prefix);
469 implib_name.push(name);
470 implib_name.push(suffix);
471 let mut out_implib = OsString::from("--out-implib=");
472 out_implib.push(out_filename.with_file_name(implib_name));
473 self.link_arg(out_implib);
474 } else if crate_type == CrateType::Dylib {
475 let mut soname = OsString::from("-soname=");
479 soname.push(name);
480 self.link_arg(soname);
481 }
482 }
483 }
484 }
485
486 fn with_as_needed(&mut self, as_needed: bool, f: impl FnOnce(&mut Self)) {
487 if !as_needed {
488 if self.sess.target.is_like_darwin {
489 self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
493 } else if self.is_gnu && !self.sess.target.is_like_windows {
494 self.link_arg("--no-as-needed");
495 } else {
496 self.sess.dcx().emit_warn(errors::LinkerUnsupportedModifier);
497 }
498 }
499
500 f(self);
501
502 if !as_needed {
503 if self.sess.target.is_like_darwin {
504 } else if self.is_gnu && !self.sess.target.is_like_windows {
506 self.link_arg("--as-needed");
507 }
508 }
509 }
510}
511
512impl<'a> Linker for GccLinker<'a> {
513 fn cmd(&mut self) -> &mut Command {
514 &mut self.cmd
515 }
516
517 fn is_cc(&self) -> bool {
518 !self.is_ld
519 }
520
521 fn set_output_kind(
522 &mut self,
523 output_kind: LinkOutputKind,
524 crate_type: CrateType,
525 out_filename: &Path,
526 ) {
527 match output_kind {
528 LinkOutputKind::DynamicNoPicExe => {
529 if !self.is_ld && self.is_gnu {
530 self.cc_arg("-no-pie");
531 }
532 }
533 LinkOutputKind::DynamicPicExe => {
534 if !self.sess.target.is_like_windows {
536 self.link_or_cc_arg("-pie");
538 }
539 }
540 LinkOutputKind::StaticNoPicExe => {
541 self.link_or_cc_arg("-static");
543 if !self.is_ld && self.is_gnu {
544 self.cc_arg("-no-pie");
545 }
546 }
547 LinkOutputKind::StaticPicExe => {
548 if !self.is_ld {
549 self.cc_arg("-static-pie");
552 } else {
553 self.link_args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
559 }
560 }
561 LinkOutputKind::DynamicDylib => self.build_dylib(crate_type, out_filename),
562 LinkOutputKind::StaticDylib => {
563 self.link_or_cc_arg("-static");
564 self.build_dylib(crate_type, out_filename);
565 }
566 LinkOutputKind::WasiReactorExe => {
567 self.link_args(&["--entry", "_initialize"]);
568 }
569 }
570
571 if self.sess.target.os == "vxworks"
577 && matches!(
578 output_kind,
579 LinkOutputKind::StaticNoPicExe
580 | LinkOutputKind::StaticPicExe
581 | LinkOutputKind::StaticDylib
582 )
583 {
584 self.cc_arg("--static-crt");
585 }
586
587 if self.sess.target.arch == "avr" && !self.uses_lld {
593 self.verbatim_arg(format!("-mmcu={}", self.target_cpu));
594 }
595 }
596
597 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool) {
598 if self.sess.target.os == "illumos" && name == "c" {
599 return;
605 }
606 self.hint_dynamic();
607 self.with_as_needed(as_needed, |this| {
608 let colon = if verbatim && this.is_gnu { ":" } else { "" };
609 this.link_or_cc_arg(format!("-l{colon}{name}"));
610 });
611 }
612
613 fn link_dylib_by_path(&mut self, path: &Path, as_needed: bool) {
614 self.hint_dynamic();
615 self.with_as_needed(as_needed, |this| {
616 this.link_or_cc_arg(path);
617 })
618 }
619
620 fn link_framework_by_name(&mut self, name: &str, _verbatim: bool, as_needed: bool) {
621 self.hint_dynamic();
622 if !as_needed {
623 self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
627 }
628 self.link_or_cc_args(&["-framework", name]);
629 }
630
631 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
632 self.hint_static();
633 let colon = if verbatim && self.is_gnu { ":" } else { "" };
634 if !whole_archive {
635 self.link_or_cc_arg(format!("-l{colon}{name}"));
636 } else if self.sess.target.is_like_darwin {
637 self.link_arg("-force_load");
640 self.link_arg(find_native_static_library(name, verbatim, self.sess));
641 } else {
642 self.link_arg("--whole-archive")
643 .link_or_cc_arg(format!("-l{colon}{name}"))
644 .link_arg("--no-whole-archive");
645 }
646 }
647
648 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
649 self.hint_static();
650 if !whole_archive {
651 self.link_or_cc_arg(path);
652 } else if self.sess.target.is_like_darwin {
653 self.link_arg("-force_load").link_arg(path);
654 } else {
655 self.link_arg("--whole-archive").link_arg(path).link_arg("--no-whole-archive");
656 }
657 }
658
659 fn framework_path(&mut self, path: &Path) {
660 self.link_or_cc_arg("-F").link_or_cc_arg(path);
661 }
662 fn full_relro(&mut self) {
663 self.link_args(&["-z", "relro", "-z", "now"]);
664 }
665 fn partial_relro(&mut self) {
666 self.link_args(&["-z", "relro"]);
667 }
668 fn no_relro(&mut self) {
669 self.link_args(&["-z", "norelro"]);
670 }
671
672 fn gc_sections(&mut self, keep_metadata: bool) {
673 if self.sess.target.is_like_darwin {
688 self.link_arg("-dead_strip");
689
690 } else if (self.is_gnu || self.sess.target.is_like_wasm) && !keep_metadata {
696 self.link_arg("--gc-sections");
697 }
698 }
699
700 fn optimize(&mut self) {
701 if !self.is_gnu && !self.sess.target.is_like_wasm {
702 return;
703 }
704
705 if self.sess.opts.optimize == config::OptLevel::More
708 || self.sess.opts.optimize == config::OptLevel::Aggressive
709 {
710 self.link_arg("-O1");
711 }
712 }
713
714 fn pgo_gen(&mut self) {
715 if !self.is_gnu {
716 return;
717 }
718
719 self.link_or_cc_args(&["-u", "__llvm_profile_runtime"]);
731 }
732
733 fn control_flow_guard(&mut self) {}
734
735 fn ehcont_guard(&mut self) {}
736
737 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
738 if self.sess.target.is_like_darwin {
740 return;
741 }
742
743 match strip {
744 Strip::None => {}
745 Strip::Debuginfo => {
746 if !self.sess.target.is_like_solaris {
751 self.link_arg("--strip-debug");
752 }
753 }
754 Strip::Symbols => {
755 self.link_arg("--strip-all");
756 }
757 }
758 match self.sess.opts.unstable_opts.debuginfo_compression {
759 config::DebugInfoCompression::None => {}
760 config::DebugInfoCompression::Zlib => {
761 self.link_arg("--compress-debug-sections=zlib");
762 }
763 config::DebugInfoCompression::Zstd => {
764 self.link_arg("--compress-debug-sections=zstd");
765 }
766 }
767 }
768
769 fn no_crt_objects(&mut self) {
770 if !self.is_ld {
771 self.cc_arg("-nostartfiles");
772 }
773 }
774
775 fn no_default_libraries(&mut self) {
776 if !self.is_ld {
777 self.cc_arg("-nodefaultlibs");
778 }
779 }
780
781 fn export_symbols(
782 &mut self,
783 tmpdir: &Path,
784 crate_type: CrateType,
785 symbols: &[(String, SymbolExportKind)],
786 ) {
787 if crate_type == CrateType::Executable {
789 let should_export_executable_symbols =
790 self.sess.opts.unstable_opts.export_executable_symbols;
791 if self.sess.target.override_export_symbols.is_none()
792 && !should_export_executable_symbols
793 {
794 return;
795 }
796 }
797
798 if !self.sess.target.limit_rdylib_exports {
803 return;
804 }
805
806 let path = tmpdir.join(if self.sess.target.is_like_windows { "list.def" } else { "list" });
807 debug!("EXPORTED SYMBOLS:");
808
809 if self.sess.target.is_like_darwin {
810 let res: io::Result<()> = try {
812 let mut f = File::create_buffered(&path)?;
813 for (sym, _) in symbols {
814 debug!(" _{sym}");
815 writeln!(f, "_{sym}")?;
816 }
817 };
818 if let Err(error) = res {
819 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
820 }
821 self.link_arg("-exported_symbols_list").link_arg(path);
822 } else if self.sess.target.is_like_windows {
823 let res: io::Result<()> = try {
824 let mut f = File::create_buffered(&path)?;
825
826 writeln!(f, "EXPORTS")?;
829 for (symbol, kind) in symbols {
830 let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
831 debug!(" _{symbol}");
832 writeln!(f, " \"{symbol}\"{kind_marker}")?;
835 }
836 };
837 if let Err(error) = res {
838 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
839 }
840 self.link_arg(path);
841 } else if crate_type == CrateType::Executable && !self.sess.target.is_like_solaris {
842 let res: io::Result<()> = try {
843 let mut f = File::create_buffered(&path)?;
844 writeln!(f, "{{")?;
845 for (sym, _) in symbols {
846 debug!(sym);
847 writeln!(f, " {sym};")?;
848 }
849 writeln!(f, "}};")?;
850 };
851 if let Err(error) = res {
852 self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
853 }
854 self.link_arg("--dynamic-list").link_arg(path);
855 } else if self.sess.target.is_like_wasm {
856 self.link_arg("--no-export-dynamic");
857 for (sym, _) in symbols {
858 self.link_arg("--export").link_arg(sym);
859 }
860 } else {
861 let res: io::Result<()> = try {
863 let mut f = File::create_buffered(&path)?;
864 writeln!(f, "{{")?;
865 if !symbols.is_empty() {
866 writeln!(f, " global:")?;
867 for (sym, _) in symbols {
868 debug!(" {sym};");
869 writeln!(f, " {sym};")?;
870 }
871 }
872 writeln!(f, "\n local:\n *;\n}};")?;
873 };
874 if let Err(error) = res {
875 self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
876 }
877 if self.sess.target.is_like_solaris {
878 self.link_arg("-M").link_arg(path);
879 } else {
880 let mut arg = OsString::from("--version-script=");
881 arg.push(path);
882 self.link_arg(arg).link_arg("--no-undefined-version");
883 }
884 }
885 }
886
887 fn subsystem(&mut self, subsystem: &str) {
888 self.link_args(&["--subsystem", subsystem]);
889 }
890
891 fn reset_per_library_state(&mut self) {
892 self.hint_dynamic(); }
894
895 fn linker_plugin_lto(&mut self) {
896 match self.sess.opts.cg.linker_plugin_lto {
897 LinkerPluginLto::Disabled => {
898 }
900 LinkerPluginLto::LinkerPluginAuto => {
901 self.push_linker_plugin_lto_args(None);
902 }
903 LinkerPluginLto::LinkerPlugin(ref path) => {
904 self.push_linker_plugin_lto_args(Some(path.as_os_str()));
905 }
906 }
907 }
908
909 fn add_eh_frame_header(&mut self) {
913 self.link_arg("--eh-frame-hdr");
914 }
915
916 fn add_no_exec(&mut self) {
917 if self.sess.target.is_like_windows {
918 self.link_arg("--nxcompat");
919 } else if self.is_gnu {
920 self.link_args(&["-z", "noexecstack"]);
921 }
922 }
923
924 fn add_as_needed(&mut self) {
925 if self.is_gnu && !self.sess.target.is_like_windows {
926 self.link_arg("--as-needed");
927 } else if self.sess.target.is_like_solaris {
928 self.link_args(&["-z", "ignore"]);
930 }
931 }
932}
933
934struct MsvcLinker<'a> {
935 cmd: Command,
936 sess: &'a Session,
937}
938
939impl<'a> Linker for MsvcLinker<'a> {
940 fn cmd(&mut self) -> &mut Command {
941 &mut self.cmd
942 }
943
944 fn set_output_kind(
945 &mut self,
946 output_kind: LinkOutputKind,
947 _crate_type: CrateType,
948 out_filename: &Path,
949 ) {
950 match output_kind {
951 LinkOutputKind::DynamicNoPicExe
952 | LinkOutputKind::DynamicPicExe
953 | LinkOutputKind::StaticNoPicExe
954 | LinkOutputKind::StaticPicExe => {}
955 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
956 self.link_arg("/DLL");
957 let mut arg: OsString = "/IMPLIB:".into();
958 arg.push(out_filename.with_extension("dll.lib"));
959 self.link_arg(arg);
960 }
961 LinkOutputKind::WasiReactorExe => {
962 panic!("can't link as reactor on non-wasi target");
963 }
964 }
965 }
966
967 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
968 if let Some(path) = try_find_native_dynamic_library(self.sess, name, verbatim) {
971 self.link_arg(path);
972 } else {
973 self.link_arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
974 }
975 }
976
977 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
978 let implib_path = path.with_extension("dll.lib");
981 if implib_path.exists() {
982 self.link_or_cc_arg(implib_path);
983 }
984 }
985
986 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
987 if let Some(path) = try_find_native_static_library(self.sess, name, verbatim) {
990 self.link_staticlib_by_path(&path, whole_archive);
991 } else {
992 let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
993 let (prefix, suffix) = self.sess.staticlib_components(verbatim);
994 self.link_arg(format!("{opts}{prefix}{name}{suffix}"));
995 }
996 }
997
998 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
999 if !whole_archive {
1000 self.link_arg(path);
1001 } else {
1002 let mut arg = OsString::from("/WHOLEARCHIVE:");
1003 arg.push(path);
1004 self.link_arg(arg);
1005 }
1006 }
1007
1008 fn gc_sections(&mut self, _keep_metadata: bool) {
1009 if self.sess.opts.optimize != config::OptLevel::No {
1013 self.link_arg("/OPT:REF,ICF");
1014 } else {
1015 self.link_arg("/OPT:REF,NOICF");
1018 }
1019 }
1020
1021 fn full_relro(&mut self) {
1022 }
1024
1025 fn partial_relro(&mut self) {
1026 }
1028
1029 fn no_relro(&mut self) {
1030 }
1032
1033 fn no_crt_objects(&mut self) {
1034 }
1036
1037 fn no_default_libraries(&mut self) {
1038 self.link_arg("/NODEFAULTLIB");
1039 }
1040
1041 fn include_path(&mut self, path: &Path) {
1042 let mut arg = OsString::from("/LIBPATH:");
1043 arg.push(path);
1044 self.link_arg(&arg);
1045 }
1046
1047 fn output_filename(&mut self, path: &Path) {
1048 let mut arg = OsString::from("/OUT:");
1049 arg.push(path);
1050 self.link_arg(&arg);
1051 }
1052
1053 fn optimize(&mut self) {
1054 }
1056
1057 fn pgo_gen(&mut self) {
1058 }
1060
1061 fn control_flow_guard(&mut self) {
1062 self.link_arg("/guard:cf");
1063 }
1064
1065 fn ehcont_guard(&mut self) {
1066 if self.sess.target.pointer_width == 64 {
1067 self.link_arg("/guard:ehcont");
1068 }
1069 }
1070
1071 fn debuginfo(&mut self, _strip: Strip, natvis_debugger_visualizers: &[PathBuf]) {
1072 self.link_arg("/DEBUG");
1075
1076 self.link_arg("/PDBALTPATH:%_PDB%");
1084
1085 let natvis_dir_path = self.sess.opts.sysroot.path().join("lib\\rustlib\\etc");
1087 if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
1088 for entry in natvis_dir {
1089 match entry {
1090 Ok(entry) => {
1091 let path = entry.path();
1092 if path.extension() == Some("natvis".as_ref()) {
1093 let mut arg = OsString::from("/NATVIS:");
1094 arg.push(path);
1095 self.link_arg(arg);
1096 }
1097 }
1098 Err(error) => {
1099 self.sess.dcx().emit_warn(errors::NoNatvisDirectory { error });
1100 }
1101 }
1102 }
1103 }
1104
1105 for path in natvis_debugger_visualizers {
1107 let mut arg = OsString::from("/NATVIS:");
1108 arg.push(path);
1109 self.link_arg(arg);
1110 }
1111 }
1112
1113 fn export_symbols(
1126 &mut self,
1127 tmpdir: &Path,
1128 crate_type: CrateType,
1129 symbols: &[(String, SymbolExportKind)],
1130 ) {
1131 if crate_type == CrateType::Executable {
1133 let should_export_executable_symbols =
1134 self.sess.opts.unstable_opts.export_executable_symbols;
1135 if !should_export_executable_symbols {
1136 return;
1137 }
1138 }
1139
1140 let path = tmpdir.join("lib.def");
1141 let res: io::Result<()> = try {
1142 let mut f = File::create_buffered(&path)?;
1143
1144 writeln!(f, "LIBRARY")?;
1147 writeln!(f, "EXPORTS")?;
1148 for (symbol, kind) in symbols {
1149 let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
1150 debug!(" _{symbol}");
1151 writeln!(f, " {symbol}{kind_marker}")?;
1152 }
1153 };
1154 if let Err(error) = res {
1155 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
1156 }
1157 let mut arg = OsString::from("/DEF:");
1158 arg.push(path);
1159 self.link_arg(&arg);
1160 }
1161
1162 fn subsystem(&mut self, subsystem: &str) {
1163 self.link_arg(&format!("/SUBSYSTEM:{subsystem}"));
1166
1167 if subsystem == "windows" {
1182 self.link_arg("/ENTRY:mainCRTStartup");
1183 }
1184 }
1185
1186 fn linker_plugin_lto(&mut self) {
1187 }
1189
1190 fn add_no_exec(&mut self) {
1191 self.link_arg("/NXCOMPAT");
1192 }
1193}
1194
1195struct EmLinker<'a> {
1196 cmd: Command,
1197 sess: &'a Session,
1198}
1199
1200impl<'a> Linker for EmLinker<'a> {
1201 fn cmd(&mut self) -> &mut Command {
1202 &mut self.cmd
1203 }
1204
1205 fn is_cc(&self) -> bool {
1206 true
1207 }
1208
1209 fn set_output_kind(
1210 &mut self,
1211 _output_kind: LinkOutputKind,
1212 _crate_type: CrateType,
1213 _out_filename: &Path,
1214 ) {
1215 }
1216
1217 fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1218 self.link_or_cc_args(&["-l", name]);
1220 }
1221
1222 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1223 self.link_or_cc_arg(path);
1224 }
1225
1226 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
1227 self.link_or_cc_args(&["-l", name]);
1228 }
1229
1230 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1231 self.link_or_cc_arg(path);
1232 }
1233
1234 fn full_relro(&mut self) {
1235 }
1237
1238 fn partial_relro(&mut self) {
1239 }
1241
1242 fn no_relro(&mut self) {
1243 }
1245
1246 fn gc_sections(&mut self, _keep_metadata: bool) {
1247 }
1249
1250 fn optimize(&mut self) {
1251 self.cc_arg(match self.sess.opts.optimize {
1253 OptLevel::No => "-O0",
1254 OptLevel::Less => "-O1",
1255 OptLevel::More => "-O2",
1256 OptLevel::Aggressive => "-O3",
1257 OptLevel::Size => "-Os",
1258 OptLevel::SizeMin => "-Oz",
1259 });
1260 }
1261
1262 fn pgo_gen(&mut self) {
1263 }
1265
1266 fn control_flow_guard(&mut self) {}
1267
1268 fn ehcont_guard(&mut self) {}
1269
1270 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1271 self.cc_arg(match self.sess.opts.debuginfo {
1274 DebugInfo::None => "-g0",
1275 DebugInfo::Limited | DebugInfo::LineTablesOnly | DebugInfo::LineDirectivesOnly => {
1276 "--profiling-funcs"
1277 }
1278 DebugInfo::Full => "-g",
1279 });
1280 }
1281
1282 fn no_crt_objects(&mut self) {}
1283
1284 fn no_default_libraries(&mut self) {
1285 self.cc_arg("-nodefaultlibs");
1286 }
1287
1288 fn export_symbols(
1289 &mut self,
1290 _tmpdir: &Path,
1291 _crate_type: CrateType,
1292 symbols: &[(String, SymbolExportKind)],
1293 ) {
1294 debug!("EXPORTED SYMBOLS:");
1295
1296 self.cc_arg("-s");
1297
1298 let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
1299 let encoded = serde_json::to_string(
1300 &symbols.iter().map(|(sym, _)| "_".to_owned() + sym).collect::<Vec<_>>(),
1301 )
1302 .unwrap();
1303 debug!("{encoded}");
1304
1305 arg.push(encoded);
1306
1307 self.cc_arg(arg);
1308 }
1309
1310 fn subsystem(&mut self, _subsystem: &str) {
1311 }
1313
1314 fn linker_plugin_lto(&mut self) {
1315 }
1317}
1318
1319struct WasmLd<'a> {
1320 cmd: Command,
1321 sess: &'a Session,
1322}
1323
1324impl<'a> WasmLd<'a> {
1325 fn new(cmd: Command, sess: &'a Session) -> WasmLd<'a> {
1326 WasmLd { cmd, sess }
1327 }
1328}
1329
1330impl<'a> Linker for WasmLd<'a> {
1331 fn cmd(&mut self) -> &mut Command {
1332 &mut self.cmd
1333 }
1334
1335 fn set_output_kind(
1336 &mut self,
1337 output_kind: LinkOutputKind,
1338 _crate_type: CrateType,
1339 _out_filename: &Path,
1340 ) {
1341 match output_kind {
1342 LinkOutputKind::DynamicNoPicExe
1343 | LinkOutputKind::DynamicPicExe
1344 | LinkOutputKind::StaticNoPicExe
1345 | LinkOutputKind::StaticPicExe => {}
1346 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1347 self.link_arg("--no-entry");
1348 }
1349 LinkOutputKind::WasiReactorExe => {
1350 self.link_args(&["--entry", "_initialize"]);
1351 }
1352 }
1353 }
1354
1355 fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1356 self.link_or_cc_args(&["-l", name]);
1357 }
1358
1359 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1360 self.link_or_cc_arg(path);
1361 }
1362
1363 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1364 if !whole_archive {
1365 self.link_or_cc_args(&["-l", name]);
1366 } else {
1367 self.link_arg("--whole-archive")
1368 .link_or_cc_args(&["-l", name])
1369 .link_arg("--no-whole-archive");
1370 }
1371 }
1372
1373 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1374 if !whole_archive {
1375 self.link_or_cc_arg(path);
1376 } else {
1377 self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1378 }
1379 }
1380
1381 fn full_relro(&mut self) {}
1382
1383 fn partial_relro(&mut self) {}
1384
1385 fn no_relro(&mut self) {}
1386
1387 fn gc_sections(&mut self, _keep_metadata: bool) {
1388 self.link_arg("--gc-sections");
1389 }
1390
1391 fn optimize(&mut self) {
1392 self.link_arg(match self.sess.opts.optimize {
1395 OptLevel::No => "-O0",
1396 OptLevel::Less => "-O1",
1397 OptLevel::More => "-O2",
1398 OptLevel::Aggressive => "-O3",
1399 OptLevel::Size => "-O2",
1402 OptLevel::SizeMin => "-O2",
1403 });
1404 }
1405
1406 fn pgo_gen(&mut self) {}
1407
1408 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1409 match strip {
1410 Strip::None => {}
1411 Strip::Debuginfo => {
1412 self.link_arg("--strip-debug");
1413 }
1414 Strip::Symbols => {
1415 self.link_arg("--strip-all");
1416 }
1417 }
1418 }
1419
1420 fn control_flow_guard(&mut self) {}
1421
1422 fn ehcont_guard(&mut self) {}
1423
1424 fn no_crt_objects(&mut self) {}
1425
1426 fn no_default_libraries(&mut self) {}
1427
1428 fn export_symbols(
1429 &mut self,
1430 _tmpdir: &Path,
1431 _crate_type: CrateType,
1432 symbols: &[(String, SymbolExportKind)],
1433 ) {
1434 for (sym, _) in symbols {
1435 self.link_args(&["--export", sym]);
1436 }
1437
1438 if self.sess.target.os == "unknown" || self.sess.target.os == "none" {
1443 self.link_args(&["--export=__heap_base", "--export=__data_end"]);
1444 }
1445 }
1446
1447 fn subsystem(&mut self, _subsystem: &str) {}
1448
1449 fn linker_plugin_lto(&mut self) {
1450 match self.sess.opts.cg.linker_plugin_lto {
1451 LinkerPluginLto::Disabled => {
1452 }
1454 LinkerPluginLto::LinkerPluginAuto => {
1455 self.push_linker_plugin_lto_args();
1456 }
1457 LinkerPluginLto::LinkerPlugin(_) => {
1458 self.push_linker_plugin_lto_args();
1459 }
1460 }
1461 }
1462}
1463
1464impl<'a> WasmLd<'a> {
1465 fn push_linker_plugin_lto_args(&mut self) {
1466 let opt_level = match self.sess.opts.optimize {
1467 config::OptLevel::No => "O0",
1468 config::OptLevel::Less => "O1",
1469 config::OptLevel::More => "O2",
1470 config::OptLevel::Aggressive => "O3",
1471 config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
1473 };
1474 self.link_arg(&format!("--lto-{opt_level}"));
1475 }
1476}
1477
1478struct L4Bender<'a> {
1480 cmd: Command,
1481 sess: &'a Session,
1482 hinted_static: bool,
1483}
1484
1485impl<'a> Linker for L4Bender<'a> {
1486 fn cmd(&mut self) -> &mut Command {
1487 &mut self.cmd
1488 }
1489
1490 fn set_output_kind(
1491 &mut self,
1492 _output_kind: LinkOutputKind,
1493 _crate_type: CrateType,
1494 _out_filename: &Path,
1495 ) {
1496 }
1497
1498 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1499 self.hint_static();
1500 if !whole_archive {
1501 self.link_arg(format!("-PC{name}"));
1502 } else {
1503 self.link_arg("--whole-archive")
1504 .link_or_cc_arg(format!("-l{name}"))
1505 .link_arg("--no-whole-archive");
1506 }
1507 }
1508
1509 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1510 self.hint_static();
1511 if !whole_archive {
1512 self.link_or_cc_arg(path);
1513 } else {
1514 self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1515 }
1516 }
1517
1518 fn full_relro(&mut self) {
1519 self.link_args(&["-z", "relro", "-z", "now"]);
1520 }
1521
1522 fn partial_relro(&mut self) {
1523 self.link_args(&["-z", "relro"]);
1524 }
1525
1526 fn no_relro(&mut self) {
1527 self.link_args(&["-z", "norelro"]);
1528 }
1529
1530 fn gc_sections(&mut self, keep_metadata: bool) {
1531 if !keep_metadata {
1532 self.link_arg("--gc-sections");
1533 }
1534 }
1535
1536 fn optimize(&mut self) {
1537 if self.sess.opts.optimize == config::OptLevel::More
1540 || self.sess.opts.optimize == config::OptLevel::Aggressive
1541 {
1542 self.link_arg("-O1");
1543 }
1544 }
1545
1546 fn pgo_gen(&mut self) {}
1547
1548 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1549 match strip {
1550 Strip::None => {}
1551 Strip::Debuginfo => {
1552 self.link_arg("--strip-debug");
1553 }
1554 Strip::Symbols => {
1555 self.link_arg("--strip-all");
1556 }
1557 }
1558 }
1559
1560 fn no_default_libraries(&mut self) {
1561 self.cc_arg("-nostdlib");
1562 }
1563
1564 fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[(String, SymbolExportKind)]) {
1565 self.sess.dcx().emit_warn(errors::L4BenderExportingSymbolsUnimplemented);
1567 }
1568
1569 fn subsystem(&mut self, subsystem: &str) {
1570 self.link_arg(&format!("--subsystem {subsystem}"));
1571 }
1572
1573 fn reset_per_library_state(&mut self) {
1574 self.hint_static(); }
1576
1577 fn linker_plugin_lto(&mut self) {}
1578
1579 fn control_flow_guard(&mut self) {}
1580
1581 fn ehcont_guard(&mut self) {}
1582
1583 fn no_crt_objects(&mut self) {}
1584}
1585
1586impl<'a> L4Bender<'a> {
1587 fn new(cmd: Command, sess: &'a Session) -> L4Bender<'a> {
1588 L4Bender { cmd, sess, hinted_static: false }
1589 }
1590
1591 fn hint_static(&mut self) {
1592 if !self.hinted_static {
1593 self.link_or_cc_arg("-static");
1594 self.hinted_static = true;
1595 }
1596 }
1597}
1598
1599struct AixLinker<'a> {
1601 cmd: Command,
1602 sess: &'a Session,
1603 hinted_static: Option<bool>,
1604}
1605
1606impl<'a> AixLinker<'a> {
1607 fn new(cmd: Command, sess: &'a Session) -> AixLinker<'a> {
1608 AixLinker { cmd, sess, hinted_static: None }
1609 }
1610
1611 fn hint_static(&mut self) {
1612 if self.hinted_static != Some(true) {
1613 self.link_arg("-bstatic");
1614 self.hinted_static = Some(true);
1615 }
1616 }
1617
1618 fn hint_dynamic(&mut self) {
1619 if self.hinted_static != Some(false) {
1620 self.link_arg("-bdynamic");
1621 self.hinted_static = Some(false);
1622 }
1623 }
1624
1625 fn build_dylib(&mut self, _out_filename: &Path) {
1626 self.link_args(&["-bM:SRE", "-bnoentry"]);
1627 self.link_arg("-bexpfull");
1630 }
1631}
1632
1633impl<'a> Linker for AixLinker<'a> {
1634 fn cmd(&mut self) -> &mut Command {
1635 &mut self.cmd
1636 }
1637
1638 fn set_output_kind(
1639 &mut self,
1640 output_kind: LinkOutputKind,
1641 _crate_type: CrateType,
1642 out_filename: &Path,
1643 ) {
1644 match output_kind {
1645 LinkOutputKind::DynamicDylib => {
1646 self.hint_dynamic();
1647 self.build_dylib(out_filename);
1648 }
1649 LinkOutputKind::StaticDylib => {
1650 self.hint_static();
1651 self.build_dylib(out_filename);
1652 }
1653 _ => {}
1654 }
1655 }
1656
1657 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
1658 self.hint_dynamic();
1659 self.link_or_cc_arg(if verbatim { String::from(name) } else { format!("-l{name}") });
1660 }
1661
1662 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1663 self.hint_dynamic();
1664 self.link_or_cc_arg(path);
1665 }
1666
1667 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
1668 self.hint_static();
1669 if !whole_archive {
1670 self.link_or_cc_arg(if verbatim { String::from(name) } else { format!("-l{name}") });
1671 } else {
1672 let mut arg = OsString::from("-bkeepfile:");
1673 arg.push(find_native_static_library(name, verbatim, self.sess));
1674 self.link_or_cc_arg(arg);
1675 }
1676 }
1677
1678 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1679 self.hint_static();
1680 if !whole_archive {
1681 self.link_or_cc_arg(path);
1682 } else {
1683 let mut arg = OsString::from("-bkeepfile:");
1684 arg.push(path);
1685 self.link_arg(arg);
1686 }
1687 }
1688
1689 fn full_relro(&mut self) {}
1690
1691 fn partial_relro(&mut self) {}
1692
1693 fn no_relro(&mut self) {}
1694
1695 fn gc_sections(&mut self, _keep_metadata: bool) {
1696 self.link_arg("-bgc");
1697 }
1698
1699 fn optimize(&mut self) {}
1700
1701 fn pgo_gen(&mut self) {
1702 self.link_arg("-bdbg:namedsects:ss");
1703 self.link_arg("-u");
1704 self.link_arg("__llvm_profile_runtime");
1705 }
1706
1707 fn control_flow_guard(&mut self) {}
1708
1709 fn ehcont_guard(&mut self) {}
1710
1711 fn debuginfo(&mut self, _: Strip, _: &[PathBuf]) {}
1712
1713 fn no_crt_objects(&mut self) {}
1714
1715 fn no_default_libraries(&mut self) {}
1716
1717 fn export_symbols(
1718 &mut self,
1719 tmpdir: &Path,
1720 _crate_type: CrateType,
1721 symbols: &[(String, SymbolExportKind)],
1722 ) {
1723 let path = tmpdir.join("list.exp");
1724 let res: io::Result<()> = try {
1725 let mut f = File::create_buffered(&path)?;
1726 for (symbol, _) in symbols {
1728 debug!(" _{symbol}");
1729 writeln!(f, " {symbol}")?;
1730 }
1731 };
1732 if let Err(e) = res {
1733 self.sess.dcx().fatal(format!("failed to write export file: {e}"));
1734 }
1735 self.link_arg(format!("-bE:{}", path.to_str().unwrap()));
1736 }
1737
1738 fn subsystem(&mut self, _subsystem: &str) {}
1739
1740 fn reset_per_library_state(&mut self) {
1741 self.hint_dynamic();
1742 }
1743
1744 fn linker_plugin_lto(&mut self) {}
1745
1746 fn add_eh_frame_header(&mut self) {}
1747
1748 fn add_no_exec(&mut self) {}
1749
1750 fn add_as_needed(&mut self) {}
1751}
1752
1753fn for_each_exported_symbols_include_dep<'tcx>(
1754 tcx: TyCtxt<'tcx>,
1755 crate_type: CrateType,
1756 mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum),
1757) {
1758 let formats = tcx.dependency_formats(());
1759 let deps = &formats[&crate_type];
1760
1761 for (cnum, dep_format) in deps.iter_enumerated() {
1762 if *dep_format == Linkage::Static {
1764 for &(symbol, info) in tcx.exported_non_generic_symbols(cnum).iter() {
1765 callback(symbol, info, cnum);
1766 }
1767 for &(symbol, info) in tcx.exported_generic_symbols(cnum).iter() {
1768 callback(symbol, info, cnum);
1769 }
1770 }
1771 }
1772}
1773
1774pub(crate) fn exported_symbols(
1775 tcx: TyCtxt<'_>,
1776 crate_type: CrateType,
1777) -> Vec<(String, SymbolExportKind)> {
1778 if let Some(ref exports) = tcx.sess.target.override_export_symbols {
1779 return exports
1780 .iter()
1781 .map(|name| {
1782 (
1783 name.to_string(),
1784 SymbolExportKind::Text,
1788 )
1789 })
1790 .collect();
1791 }
1792
1793 let mut symbols = if let CrateType::ProcMacro = crate_type {
1794 exported_symbols_for_proc_macro_crate(tcx)
1795 } else {
1796 exported_symbols_for_non_proc_macro(tcx, crate_type)
1797 };
1798
1799 if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
1800 let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx);
1801 symbols.push((metadata_symbol_name, SymbolExportKind::Data));
1802 }
1803
1804 symbols
1805}
1806
1807fn exported_symbols_for_non_proc_macro(
1808 tcx: TyCtxt<'_>,
1809 crate_type: CrateType,
1810) -> Vec<(String, SymbolExportKind)> {
1811 let mut symbols = Vec::new();
1812 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1813 for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1814 if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum) {
1818 symbols.push((
1819 symbol_export::exporting_symbol_name_for_instance_in_crate(tcx, symbol, cnum),
1820 info.kind,
1821 ));
1822 symbol_export::extend_exported_symbols(&mut symbols, tcx, symbol, cnum);
1823 }
1824 });
1825
1826 if export_threshold == SymbolExportLevel::Rust
1828 && needs_allocator_shim_for_linking(tcx.dependency_formats(()), crate_type)
1829 && tcx.allocator_kind(()).is_some()
1830 {
1831 symbols.extend(allocator_shim_symbols(tcx));
1832 }
1833
1834 symbols
1835}
1836
1837fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<(String, SymbolExportKind)> {
1838 if !tcx.sess.opts.output_types.should_codegen() {
1840 return Vec::new();
1841 }
1842
1843 let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
1844 let proc_macro_decls_name = tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
1845
1846 vec![(proc_macro_decls_name, SymbolExportKind::Data)]
1847}
1848
1849pub(crate) fn linked_symbols(
1850 tcx: TyCtxt<'_>,
1851 crate_type: CrateType,
1852) -> Vec<(String, SymbolExportKind)> {
1853 match crate_type {
1854 CrateType::Executable
1855 | CrateType::ProcMacro
1856 | CrateType::Cdylib
1857 | CrateType::Dylib
1858 | CrateType::Sdylib => (),
1859 CrateType::Staticlib | CrateType::Rlib => {
1860 return Vec::new();
1862 }
1863 }
1864
1865 match tcx.sess.lto() {
1866 Lto::No | Lto::ThinLocal => {}
1867 Lto::Thin | Lto::Fat => {
1868 return Vec::new();
1877 }
1878 }
1879
1880 let mut symbols = Vec::new();
1881
1882 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1883 for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1884 if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum)
1885 || info.used
1886 || info.rustc_std_internal_symbol
1887 {
1888 symbols.push((
1889 symbol_export::linking_symbol_name_for_instance_in_crate(
1890 tcx, symbol, info.kind, cnum,
1891 ),
1892 info.kind,
1893 ));
1894 }
1895 });
1896
1897 symbols
1898}
1899
1900struct PtxLinker<'a> {
1903 cmd: Command,
1904 sess: &'a Session,
1905}
1906
1907impl<'a> Linker for PtxLinker<'a> {
1908 fn cmd(&mut self) -> &mut Command {
1909 &mut self.cmd
1910 }
1911
1912 fn set_output_kind(
1913 &mut self,
1914 _output_kind: LinkOutputKind,
1915 _crate_type: CrateType,
1916 _out_filename: &Path,
1917 ) {
1918 }
1919
1920 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
1921 panic!("staticlibs not supported")
1922 }
1923
1924 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1925 self.link_arg("--rlib").link_arg(path);
1926 }
1927
1928 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1929 self.link_arg("--debug");
1930 }
1931
1932 fn add_object(&mut self, path: &Path) {
1933 self.link_arg("--bitcode").link_arg(path);
1934 }
1935
1936 fn optimize(&mut self) {
1937 match self.sess.lto() {
1938 Lto::Thin | Lto::Fat | Lto::ThinLocal => {
1939 self.link_arg("-Olto");
1940 }
1941
1942 Lto::No => {}
1943 }
1944 }
1945
1946 fn full_relro(&mut self) {}
1947
1948 fn partial_relro(&mut self) {}
1949
1950 fn no_relro(&mut self) {}
1951
1952 fn gc_sections(&mut self, _keep_metadata: bool) {}
1953
1954 fn pgo_gen(&mut self) {}
1955
1956 fn no_crt_objects(&mut self) {}
1957
1958 fn no_default_libraries(&mut self) {}
1959
1960 fn control_flow_guard(&mut self) {}
1961
1962 fn ehcont_guard(&mut self) {}
1963
1964 fn export_symbols(
1965 &mut self,
1966 _tmpdir: &Path,
1967 _crate_type: CrateType,
1968 _symbols: &[(String, SymbolExportKind)],
1969 ) {
1970 }
1971
1972 fn subsystem(&mut self, _subsystem: &str) {}
1973
1974 fn linker_plugin_lto(&mut self) {}
1975}
1976
1977struct LlbcLinker<'a> {
1979 cmd: Command,
1980 sess: &'a Session,
1981}
1982
1983impl<'a> Linker for LlbcLinker<'a> {
1984 fn cmd(&mut self) -> &mut Command {
1985 &mut self.cmd
1986 }
1987
1988 fn set_output_kind(
1989 &mut self,
1990 _output_kind: LinkOutputKind,
1991 _crate_type: CrateType,
1992 _out_filename: &Path,
1993 ) {
1994 }
1995
1996 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
1997 panic!("staticlibs not supported")
1998 }
1999
2000 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2001 self.link_or_cc_arg(path);
2002 }
2003
2004 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2005 self.link_arg("--debug");
2006 }
2007
2008 fn optimize(&mut self) {
2009 self.link_arg(match self.sess.opts.optimize {
2010 OptLevel::No => "-O0",
2011 OptLevel::Less => "-O1",
2012 OptLevel::More => "-O2",
2013 OptLevel::Aggressive => "-O3",
2014 OptLevel::Size => "-Os",
2015 OptLevel::SizeMin => "-Oz",
2016 });
2017 }
2018
2019 fn full_relro(&mut self) {}
2020
2021 fn partial_relro(&mut self) {}
2022
2023 fn no_relro(&mut self) {}
2024
2025 fn gc_sections(&mut self, _keep_metadata: bool) {}
2026
2027 fn pgo_gen(&mut self) {}
2028
2029 fn no_crt_objects(&mut self) {}
2030
2031 fn no_default_libraries(&mut self) {}
2032
2033 fn control_flow_guard(&mut self) {}
2034
2035 fn ehcont_guard(&mut self) {}
2036
2037 fn export_symbols(
2038 &mut self,
2039 _tmpdir: &Path,
2040 _crate_type: CrateType,
2041 symbols: &[(String, SymbolExportKind)],
2042 ) {
2043 match _crate_type {
2044 CrateType::Cdylib => {
2045 for (sym, _) in symbols {
2046 self.link_args(&["--export-symbol", sym]);
2047 }
2048 }
2049 _ => (),
2050 }
2051 }
2052
2053 fn subsystem(&mut self, _subsystem: &str) {}
2054
2055 fn linker_plugin_lto(&mut self) {}
2056}
2057
2058struct BpfLinker<'a> {
2059 cmd: Command,
2060 sess: &'a Session,
2061}
2062
2063impl<'a> Linker for BpfLinker<'a> {
2064 fn cmd(&mut self) -> &mut Command {
2065 &mut self.cmd
2066 }
2067
2068 fn set_output_kind(
2069 &mut self,
2070 _output_kind: LinkOutputKind,
2071 _crate_type: CrateType,
2072 _out_filename: &Path,
2073 ) {
2074 }
2075
2076 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
2077 panic!("staticlibs not supported")
2078 }
2079
2080 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2081 self.link_or_cc_arg(path);
2082 }
2083
2084 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2085 self.link_arg("--debug");
2086 }
2087
2088 fn optimize(&mut self) {
2089 self.link_arg(match self.sess.opts.optimize {
2090 OptLevel::No => "-O0",
2091 OptLevel::Less => "-O1",
2092 OptLevel::More => "-O2",
2093 OptLevel::Aggressive => "-O3",
2094 OptLevel::Size => "-Os",
2095 OptLevel::SizeMin => "-Oz",
2096 });
2097 }
2098
2099 fn full_relro(&mut self) {}
2100
2101 fn partial_relro(&mut self) {}
2102
2103 fn no_relro(&mut self) {}
2104
2105 fn gc_sections(&mut self, _keep_metadata: bool) {}
2106
2107 fn pgo_gen(&mut self) {}
2108
2109 fn no_crt_objects(&mut self) {}
2110
2111 fn no_default_libraries(&mut self) {}
2112
2113 fn control_flow_guard(&mut self) {}
2114
2115 fn ehcont_guard(&mut self) {}
2116
2117 fn export_symbols(
2118 &mut self,
2119 tmpdir: &Path,
2120 _crate_type: CrateType,
2121 symbols: &[(String, SymbolExportKind)],
2122 ) {
2123 let path = tmpdir.join("symbols");
2124 let res: io::Result<()> = try {
2125 let mut f = File::create_buffered(&path)?;
2126 for (sym, _) in symbols {
2127 writeln!(f, "{sym}")?;
2128 }
2129 };
2130 if let Err(error) = res {
2131 self.sess.dcx().emit_fatal(errors::SymbolFileWriteFailure { error });
2132 } else {
2133 self.link_arg("--export-symbols").link_arg(&path);
2134 }
2135 }
2136
2137 fn subsystem(&mut self, _subsystem: &str) {}
2138
2139 fn linker_plugin_lto(&mut self) {}
2140}