1use std::ffi::{OsStr, OsString};
2use std::fs::{self, File};
3use std::io::prelude::*;
4use std::path::{Path, PathBuf};
5use std::{env, iter, mem, str};
6
7use find_msvc_tools;
8use rustc_hir::attrs::WindowsSubsystemKind;
9use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
10use rustc_metadata::{
11 find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
12};
13use rustc_middle::bug;
14use rustc_middle::middle::dependency_format::Linkage;
15use rustc_middle::middle::exported_symbols::{
16 self, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
17};
18use rustc_middle::ty::TyCtxt;
19use rustc_session::Session;
20use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
21use rustc_target::spec::{Arch, Cc, CfgAbi, LinkOutputKind, LinkerFlavor, Lld, Os};
22use tracing::{debug, warn};
23
24use super::command::Command;
25use super::symbol_export;
26use crate::back::symbol_export::allocator_shim_symbols;
27use crate::base::needs_allocator_shim_for_linking;
28use crate::errors;
29
30#[cfg(test)]
31mod tests;
32
33pub(crate) fn disable_localization(linker: &mut Command) {
39 linker.env("LC_ALL", "C");
42 linker.env("VSLANG", "1033");
44}
45
46pub(crate) fn get_linker<'a>(
50 sess: &'a Session,
51 linker: &Path,
52 flavor: LinkerFlavor,
53 self_contained: bool,
54 target_cpu: &'a str,
55 codegen_backend: &'static str,
56) -> Box<dyn Linker + 'a> {
57 let msvc_tool = find_msvc_tools::find_tool(sess.target.arch.desc(), "link.exe");
58
59 let mut cmd = match linker.to_str() {
68 Some(linker) if falsecfg!(windows) && linker.ends_with(".bat") => Command::bat_script(linker),
69 _ => match flavor {
70 LinkerFlavor::Gnu(Cc::No, Lld::Yes)
71 | LinkerFlavor::Darwin(Cc::No, Lld::Yes)
72 | LinkerFlavor::WasmLld(Cc::No)
73 | LinkerFlavor::Msvc(Lld::Yes) => Command::lld(linker, flavor.lld_flavor()),
74 LinkerFlavor::Msvc(Lld::No)
75 if sess.opts.cg.linker.is_none() && sess.target.linker.is_none() =>
76 {
77 Command::new(msvc_tool.as_ref().map_or(linker, |t| t.path()))
78 }
79 _ => Command::new(linker),
80 },
81 };
82
83 let t = &sess.target;
87 if #[allow(non_exhaustive_omitted_patterns)] match flavor {
LinkerFlavor::Msvc(..) => true,
_ => false,
}matches!(flavor, LinkerFlavor::Msvc(..)) && t.cfg_abi == CfgAbi::Uwp {
88 if let Some(ref tool) = msvc_tool {
89 let original_path = tool.path();
90 if let Some(root_lib_path) = original_path.ancestors().nth(4) {
91 let arch = match t.arch {
92 Arch::X86_64 => Some("x64"),
93 Arch::X86 => Some("x86"),
94 Arch::AArch64 => Some("arm64"),
95 Arch::Arm => Some("arm"),
96 _ => None,
97 };
98 if let Some(ref a) = arch {
99 let mut arg = OsString::from("/LIBPATH:");
101 arg.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}\\lib\\{1}\\store",
root_lib_path.display(), a))
})format!("{}\\lib\\{}\\store", root_lib_path.display(), a));
102 cmd.arg(&arg);
103 } else {
104 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:104",
"rustc_codegen_ssa::back::linker", ::tracing::Level::WARN,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(104u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::WARN <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("arch is not supported")
as &dyn Value))])
});
} else { ; }
};warn!("arch is not supported");
105 }
106 } else {
107 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:107",
"rustc_codegen_ssa::back::linker", ::tracing::Level::WARN,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(107u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::WARN <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("MSVC root path lib location not found")
as &dyn Value))])
});
} else { ; }
};warn!("MSVC root path lib location not found");
108 }
109 } else {
110 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:110",
"rustc_codegen_ssa::back::linker", ::tracing::Level::WARN,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(110u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::WARN <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("link.exe not found")
as &dyn Value))])
});
} else { ; }
};warn!("link.exe not found");
111 }
112 }
113
114 let mut new_path = sess.get_tools_search_paths(self_contained);
117 let mut msvc_changed_path = false;
118 if sess.target.is_like_msvc
119 && let Some(ref tool) = msvc_tool
120 {
121 for (k, v) in tool.env() {
122 if k == "PATH" {
123 new_path.extend(env::split_paths(v));
124 msvc_changed_path = true;
125 } else {
126 cmd.env(k, v);
127 }
128 }
129 }
130
131 if !msvc_changed_path && let Some(path) = env::var_os("PATH") {
132 new_path.extend(env::split_paths(&path));
133 }
134 cmd.env("PATH", env::join_paths(new_path).unwrap());
135
136 if !(cmd.get_args().is_empty() || sess.target.cfg_abi == CfgAbi::Uwp) {
::core::panicking::panic("assertion failed: cmd.get_args().is_empty() || sess.target.cfg_abi == CfgAbi::Uwp")
};assert!(cmd.get_args().is_empty() || sess.target.cfg_abi == CfgAbi::Uwp);
139 match flavor {
140 LinkerFlavor::Unix(Cc::No) if sess.target.os == Os::L4Re => {
141 Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>
142 }
143 LinkerFlavor::Unix(Cc::No) if sess.target.os == Os::Aix => {
144 Box::new(AixLinker::new(cmd, sess)) as Box<dyn Linker>
145 }
146 LinkerFlavor::WasmLld(Cc::No) => Box::new(WasmLd::new(cmd, sess)) as Box<dyn Linker>,
147 LinkerFlavor::Gnu(cc, _)
148 | LinkerFlavor::Darwin(cc, _)
149 | LinkerFlavor::WasmLld(cc)
150 | LinkerFlavor::Unix(cc) => Box::new(GccLinker {
151 cmd,
152 sess,
153 target_cpu,
154 hinted_static: None,
155 is_ld: cc == Cc::No,
156 is_gnu: flavor.is_gnu(),
157 uses_lld: flavor.uses_lld(),
158 codegen_backend,
159 }) as Box<dyn Linker>,
160 LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>,
161 LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
162 LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
163 LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box<dyn Linker>,
164 LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
165 }
166}
167
168fn verbatim_args<L: Linker + ?Sized>(
178 l: &mut L,
179 args: impl IntoIterator<Item: AsRef<OsStr>>,
180) -> &mut L {
181 for arg in args {
182 l.cmd().arg(arg);
183 }
184 l
185}
186fn convert_link_args_to_cc_args(cmd: &mut Command, args: impl IntoIterator<Item: AsRef<OsStr>>) {
189 let mut combined_arg = OsString::from("-Wl");
190 for arg in args {
191 if arg.as_ref().as_encoded_bytes().contains(&b',') {
194 if combined_arg != OsStr::new("-Wl") {
196 cmd.arg(combined_arg);
197 combined_arg = OsString::from("-Wl");
199 }
200
201 cmd.arg("-Xlinker");
203 cmd.arg(arg);
204 } else {
205 combined_arg.push(",");
207 combined_arg.push(arg);
208 }
209 }
210 if combined_arg != OsStr::new("-Wl") {
212 cmd.arg(combined_arg);
213 }
214}
215fn link_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
218 if !l.is_cc() {
219 verbatim_args(l, args);
220 } else {
221 convert_link_args_to_cc_args(l.cmd(), args);
222 }
223 l
224}
225fn cc_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
228 if !l.is_cc() { ::core::panicking::panic("assertion failed: l.is_cc()") };assert!(l.is_cc());
229 verbatim_args(l, args)
230}
231fn link_or_cc_args<L: Linker + ?Sized>(
233 l: &mut L,
234 args: impl IntoIterator<Item: AsRef<OsStr>>,
235) -> &mut L {
236 verbatim_args(l, args)
237}
238
239macro_rules! generate_arg_methods {
240 ($($ty:ty)*) => { $(
241 impl $ty {
242 #[allow(unused)]
243 pub(crate) fn verbatim_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
244 verbatim_args(self, args)
245 }
246 #[allow(unused)]
247 pub(crate) fn verbatim_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
248 verbatim_args(self, iter::once(arg))
249 }
250 #[allow(unused)]
251 pub(crate) fn link_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
252 link_args(self, args)
253 }
254 #[allow(unused)]
255 pub(crate) fn link_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
256 link_args(self, iter::once(arg))
257 }
258 #[allow(unused)]
259 pub(crate) fn cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
260 cc_args(self, args)
261 }
262 #[allow(unused)]
263 pub(crate) fn cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
264 cc_args(self, iter::once(arg))
265 }
266 #[allow(unused)]
267 pub(crate) fn link_or_cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
268 link_or_cc_args(self, args)
269 }
270 #[allow(unused)]
271 pub(crate) fn link_or_cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
272 link_or_cc_args(self, iter::once(arg))
273 }
274 }
275 )* }
276}
277
278impl dyn Linker + '_ {
#[allow(unused)]
pub(crate) fn verbatim_args(&mut self,
args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
verbatim_args(self, args)
}
#[allow(unused)]
pub(crate) fn verbatim_arg(&mut self, arg: impl AsRef<OsStr>)
-> &mut Self {
verbatim_args(self, iter::once(arg))
}
#[allow(unused)]
pub(crate) fn link_args(&mut self,
args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
link_args(self, args)
}
#[allow(unused)]
pub(crate) fn link_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
link_args(self, iter::once(arg))
}
#[allow(unused)]
pub(crate) fn cc_args(&mut self,
args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
cc_args(self, args)
}
#[allow(unused)]
pub(crate) fn cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
cc_args(self, iter::once(arg))
}
#[allow(unused)]
pub(crate) fn link_or_cc_args(&mut self,
args: impl IntoIterator<Item : AsRef<OsStr>>) -> &mut Self {
link_or_cc_args(self, args)
}
#[allow(unused)]
pub(crate) fn link_or_cc_arg(&mut self, arg: impl AsRef<OsStr>)
-> &mut Self {
link_or_cc_args(self, iter::once(arg))
}
}generate_arg_methods! {
279 GccLinker<'_>
280 MsvcLinker<'_>
281 EmLinker<'_>
282 WasmLd<'_>
283 L4Bender<'_>
284 AixLinker<'_>
285 LlbcLinker<'_>
286 PtxLinker<'_>
287 BpfLinker<'_>
288 dyn Linker + '_
289}
290
291pub(crate) trait Linker {
299 fn cmd(&mut self) -> &mut Command;
300 fn is_cc(&self) -> bool {
301 false
302 }
303 fn set_output_kind(
304 &mut self,
305 output_kind: LinkOutputKind,
306 crate_type: CrateType,
307 out_filename: &Path,
308 );
309 fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
310 ::rustc_middle::util::bug::bug_fmt(format_args!("dylib linked with unsupported linker"))bug!("dylib linked with unsupported linker")
311 }
312 fn link_dylib_by_path(&mut self, _path: &Path, _as_needed: bool) {
313 ::rustc_middle::util::bug::bug_fmt(format_args!("dylib linked with unsupported linker"))bug!("dylib linked with unsupported linker")
314 }
315 fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
316 ::rustc_middle::util::bug::bug_fmt(format_args!("framework linked with unsupported linker"))bug!("framework linked with unsupported linker")
317 }
318 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool);
319 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
320 fn include_path(&mut self, path: &Path) {
321 link_or_cc_args(link_or_cc_args(self, &["-L"]), &[path]);
322 }
323 fn framework_path(&mut self, _path: &Path) {
324 ::rustc_middle::util::bug::bug_fmt(format_args!("framework path set with unsupported linker"))bug!("framework path set with unsupported linker")
325 }
326 fn output_filename(&mut self, path: &Path) {
327 link_or_cc_args(link_or_cc_args(self, &["-o"]), &[path]);
328 }
329 fn add_object(&mut self, path: &Path) {
330 link_or_cc_args(self, &[path]);
331 }
332 fn gc_sections(&mut self, keep_metadata: bool);
333 fn full_relro(&mut self);
334 fn partial_relro(&mut self);
335 fn no_relro(&mut self);
336 fn optimize(&mut self);
337 fn pgo_gen(&mut self);
338 fn control_flow_guard(&mut self);
339 fn ehcont_guard(&mut self);
340 fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]);
341 fn no_crt_objects(&mut self);
342 fn no_default_libraries(&mut self);
343 fn export_symbols(
344 &mut self,
345 tmpdir: &Path,
346 crate_type: CrateType,
347 symbols: &[(String, SymbolExportKind)],
348 );
349 fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind);
350 fn linker_plugin_lto(&mut self);
351 fn add_eh_frame_header(&mut self) {}
352 fn add_no_exec(&mut self) {}
353 fn add_as_needed(&mut self) {}
354 fn reset_per_library_state(&mut self) {}
355 fn enable_profiling(&mut self) {}
356}
357
358impl dyn Linker + '_ {
359 pub(crate) fn take_cmd(&mut self) -> Command {
360 mem::replace(self.cmd(), Command::new(""))
361 }
362}
363
364struct GccLinker<'a> {
365 cmd: Command,
366 sess: &'a Session,
367 target_cpu: &'a str,
368 hinted_static: Option<bool>, is_ld: bool,
371 is_gnu: bool,
372 uses_lld: bool,
373 codegen_backend: &'static str,
374}
375
376impl<'a> GccLinker<'a> {
377 fn takes_hints(&self) -> bool {
378 !self.sess.target.is_like_darwin && !self.sess.target.is_like_wasm
387 }
388
389 fn hint_static(&mut self) {
394 if !self.takes_hints() {
395 return;
396 }
397 if self.hinted_static != Some(true) {
398 self.link_arg("-Bstatic");
399 self.hinted_static = Some(true);
400 }
401 }
402
403 fn hint_dynamic(&mut self) {
404 if !self.takes_hints() {
405 return;
406 }
407 if self.hinted_static != Some(false) {
408 self.link_arg("-Bdynamic");
409 self.hinted_static = Some(false);
410 }
411 }
412
413 fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
414 if let Some(plugin_path) = plugin_path {
415 let mut arg = OsString::from("-plugin=");
416 arg.push(plugin_path);
417 self.link_arg(&arg);
418 }
419
420 let opt_level = match self.sess.opts.optimize {
421 config::OptLevel::No => "O0",
422 config::OptLevel::Less => "O1",
423 config::OptLevel::More | config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
424 config::OptLevel::Aggressive => "O3",
425 };
426
427 if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
428 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-plugin-opt=sample-profile={0}",
path.display()))
})format!("-plugin-opt=sample-profile={}", path.display()));
429 };
430 let prefix = if self.codegen_backend == "gcc" {
431 "-"
433 } else {
434 ""
435 };
436 self.link_args(&[
437 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-plugin-opt={0}{1}", prefix,
opt_level))
})format!("-plugin-opt={prefix}{opt_level}"),
438 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-plugin-opt={1}mcpu={0}",
self.target_cpu, prefix))
})format!("-plugin-opt={prefix}mcpu={}", self.target_cpu),
439 ]);
440 }
441
442 fn build_dylib(&mut self, crate_type: CrateType, out_filename: &Path) {
443 if self.sess.target.is_like_darwin {
445 if self.is_cc() {
446 self.cc_arg("-dynamiclib");
448 } else {
449 self.link_arg("-dylib");
450 }
452
453 if self.sess.opts.cg.rpath || self.sess.opts.unstable_opts.osx_rpath_install_name {
458 let mut rpath = OsString::from("@rpath/");
459 rpath.push(out_filename.file_name().unwrap());
460 self.link_arg("-install_name").link_arg(rpath);
461 }
462 } else {
463 self.link_or_cc_arg("-shared");
464 if let Some(name) = out_filename.file_name() {
465 if self.sess.target.is_like_windows {
466 let (prefix, suffix) = self.sess.staticlib_components(false);
470 let mut implib_name = OsString::from(prefix);
471 implib_name.push(name);
472 implib_name.push(suffix);
473 let mut out_implib = OsString::from("--out-implib=");
474 out_implib.push(out_filename.with_file_name(implib_name));
475 self.link_arg(out_implib);
476 } else if crate_type == CrateType::Dylib {
477 let mut soname = OsString::from("-soname=");
481 soname.push(name);
482 self.link_arg(soname);
483 }
484 }
485 }
486 }
487
488 fn with_as_needed(&mut self, as_needed: bool, f: impl FnOnce(&mut Self)) {
489 if !as_needed {
490 if self.sess.target.is_like_darwin {
491 self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
495 } else if self.is_gnu && !self.sess.target.is_like_windows {
496 self.link_arg("--no-as-needed");
497 } else {
498 self.sess.dcx().emit_warn(errors::LinkerUnsupportedModifier);
499 }
500 }
501
502 f(self);
503
504 if !as_needed {
505 if self.sess.target.is_like_darwin {
506 } else if self.is_gnu && !self.sess.target.is_like_windows {
508 self.link_arg("--as-needed");
509 }
510 }
511 }
512}
513
514impl<'a> Linker for GccLinker<'a> {
515 fn cmd(&mut self) -> &mut Command {
516 &mut self.cmd
517 }
518
519 fn is_cc(&self) -> bool {
520 !self.is_ld
521 }
522
523 fn set_output_kind(
524 &mut self,
525 output_kind: LinkOutputKind,
526 crate_type: CrateType,
527 out_filename: &Path,
528 ) {
529 match output_kind {
530 LinkOutputKind::DynamicNoPicExe => {
531 if !self.is_ld && self.is_gnu && !self.sess.target.is_like_windows {
533 self.cc_arg("-no-pie");
534 }
535 }
536 LinkOutputKind::DynamicPicExe => {
537 if !self.sess.target.is_like_windows {
539 self.link_or_cc_arg("-pie");
541 }
542 }
543 LinkOutputKind::StaticNoPicExe => {
544 self.link_or_cc_arg("-static");
546 if !self.is_ld && self.is_gnu {
547 self.cc_arg("-no-pie");
548 }
549 }
550 LinkOutputKind::StaticPicExe => {
551 if !self.is_ld {
552 self.cc_arg("-static-pie");
555 } else {
556 self.link_args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
562 }
563 }
564 LinkOutputKind::DynamicDylib => self.build_dylib(crate_type, out_filename),
565 LinkOutputKind::StaticDylib => {
566 self.link_or_cc_arg("-static");
567 self.build_dylib(crate_type, out_filename);
568 }
569 LinkOutputKind::WasiReactorExe => {
570 self.link_args(&["--entry", "_initialize"]);
571 }
572 }
573
574 if self.sess.target.os == Os::VxWorks
580 && #[allow(non_exhaustive_omitted_patterns)] match output_kind {
LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe |
LinkOutputKind::StaticDylib => true,
_ => false,
}matches!(
581 output_kind,
582 LinkOutputKind::StaticNoPicExe
583 | LinkOutputKind::StaticPicExe
584 | LinkOutputKind::StaticDylib
585 )
586 {
587 self.cc_arg("--static-crt");
588 }
589
590 if self.sess.target.arch == Arch::Avr && !self.uses_lld {
596 self.verbatim_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-mmcu={0}", self.target_cpu))
})format!("-mmcu={}", self.target_cpu));
597 }
598 }
599
600 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool) {
601 if self.sess.target.os == Os::Illumos && name == "c" {
602 return;
608 }
609 self.hint_dynamic();
610 self.with_as_needed(as_needed, |this| {
611 let colon = if verbatim && this.is_gnu { ":" } else { "" };
612 this.link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
})format!("-l{colon}{name}"));
613 });
614 }
615
616 fn link_dylib_by_path(&mut self, path: &Path, as_needed: bool) {
617 self.hint_dynamic();
618 self.with_as_needed(as_needed, |this| {
619 this.link_or_cc_arg(path);
620 })
621 }
622
623 fn link_framework_by_name(&mut self, name: &str, _verbatim: bool, as_needed: bool) {
624 self.hint_dynamic();
625 if !as_needed {
626 self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
630 }
631 self.link_or_cc_args(&["-framework", name]);
632 }
633
634 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
635 self.hint_static();
636 let colon = if verbatim && self.is_gnu { ":" } else { "" };
637 if !whole_archive {
638 self.link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
})format!("-l{colon}{name}"));
639 } else if self.sess.target.is_like_darwin {
640 self.link_arg("-force_load");
643 self.link_arg(find_native_static_library(name, verbatim, self.sess));
644 } else {
645 self.link_arg("--whole-archive")
646 .link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
})format!("-l{colon}{name}"))
647 .link_arg("--no-whole-archive");
648 }
649 }
650
651 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
652 self.hint_static();
653 if !whole_archive {
654 self.link_or_cc_arg(path);
655 } else if self.sess.target.is_like_darwin {
656 self.link_arg("-force_load").link_arg(path);
657 } else {
658 self.link_arg("--whole-archive").link_arg(path).link_arg("--no-whole-archive");
659 }
660 }
661
662 fn framework_path(&mut self, path: &Path) {
663 self.link_or_cc_arg("-F").link_or_cc_arg(path);
664 }
665 fn full_relro(&mut self) {
666 self.link_args(&["-z", "relro", "-z", "now"]);
667 }
668 fn partial_relro(&mut self) {
669 self.link_args(&["-z", "relro"]);
670 }
671 fn no_relro(&mut self) {
672 self.link_args(&["-z", "norelro"]);
673 }
674
675 fn gc_sections(&mut self, keep_metadata: bool) {
676 if self.sess.target.is_like_darwin {
691 self.link_arg("-dead_strip");
692
693 } else if (self.is_gnu || self.sess.target.is_like_wasm) && !keep_metadata {
699 self.link_arg("--gc-sections");
700 }
701 }
702
703 fn optimize(&mut self) {
704 if !self.is_gnu && !self.sess.target.is_like_wasm {
705 return;
706 }
707
708 if self.sess.opts.optimize == config::OptLevel::More
711 || self.sess.opts.optimize == config::OptLevel::Aggressive
712 {
713 self.link_arg("-O1");
714 }
715 }
716
717 fn pgo_gen(&mut self) {
718 if !self.is_gnu {
719 return;
720 }
721
722 self.link_or_cc_args(&["-u", "__llvm_profile_runtime"]);
734 }
735
736 fn enable_profiling(&mut self) {
737 if !self.is_ld {
740 self.cc_arg("-pg");
741 if self.sess.target.is_like_windows {
744 self.cc_arg("-lgmon");
745 self.cc_arg("-lkernel32");
746 self.cc_arg("-lmsvcrt");
747 }
748 }
749 }
750
751 fn control_flow_guard(&mut self) {}
752
753 fn ehcont_guard(&mut self) {}
754
755 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
756 if self.sess.target.is_like_darwin {
758 return;
759 }
760
761 match strip {
762 Strip::None => {}
763 Strip::Debuginfo => {
764 if !self.sess.target.is_like_solaris {
769 self.link_arg("--strip-debug");
770 }
771 }
772 Strip::Symbols => {
773 self.link_arg("--strip-all");
774 }
775 }
776 match self.sess.opts.unstable_opts.debuginfo_compression {
777 config::DebugInfoCompression::None => {}
778 config::DebugInfoCompression::Zlib => {
779 self.link_arg("--compress-debug-sections=zlib");
780 }
781 config::DebugInfoCompression::Zstd => {
782 self.link_arg("--compress-debug-sections=zstd");
783 }
784 }
785 }
786
787 fn no_crt_objects(&mut self) {
788 if !self.is_ld {
789 self.cc_arg("-nostartfiles");
790 }
791 }
792
793 fn no_default_libraries(&mut self) {
794 if !self.is_ld {
795 self.cc_arg("-nodefaultlibs");
796 }
797 }
798
799 fn export_symbols(
800 &mut self,
801 tmpdir: &Path,
802 crate_type: CrateType,
803 symbols: &[(String, SymbolExportKind)],
804 ) {
805 if crate_type == CrateType::Executable {
807 let should_export_executable_symbols =
808 self.sess.opts.unstable_opts.export_executable_symbols;
809 if self.sess.target.override_export_symbols.is_none()
810 && !should_export_executable_symbols
811 {
812 return;
813 }
814 }
815
816 if !self.sess.target.limit_rdylib_exports {
821 return;
822 }
823
824 let path = tmpdir.join(if self.sess.target.is_like_windows { "list.def" } else { "list" });
825 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:825",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(825u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("EXPORTED SYMBOLS:")
as &dyn Value))])
});
} else { ; }
};debug!("EXPORTED SYMBOLS:");
826
827 if self.sess.target.is_like_darwin {
828 let res = try {
830 let mut f = File::create_buffered(&path)?;
831 for (sym, _) in symbols {
832 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:832",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(832u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
sym) as &dyn Value))])
});
} else { ; }
};debug!(" _{sym}");
833 f.write_fmt(format_args!("_{0}\n", sym))writeln!(f, "_{sym}")?;
834 }
835 };
836 if let Err(error) = res {
837 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
838 }
839 self.link_arg("-exported_symbols_list").link_arg(path);
840 } else if self.sess.target.is_like_windows {
841 let res = try {
842 let mut f = File::create_buffered(&path)?;
843
844 f.write_fmt(format_args!("EXPORTS\n"))writeln!(f, "EXPORTS")?;
847 for (symbol, kind) in symbols {
848 let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
849 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:849",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(849u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
symbol) as &dyn Value))])
});
} else { ; }
};debug!(" _{symbol}");
850 f.write_fmt(format_args!(" \"{0}\"{1}\n", symbol, kind_marker))writeln!(f, " \"{symbol}\"{kind_marker}")?;
853 }
854 };
855 if let Err(error) = res {
856 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
857 }
858 self.link_arg(path);
859 } else if self.sess.target.is_like_wasm {
860 self.link_arg("--no-export-dynamic");
861 for (sym, _) in symbols {
862 self.link_arg("--export").link_arg(sym);
863 }
864 } else if crate_type == CrateType::Executable && !self.sess.target.is_like_solaris {
865 let res = try {
866 let mut f = File::create_buffered(&path)?;
867 f.write_fmt(format_args!("{{\n"))writeln!(f, "{{")?;
868 for (sym, _) in symbols {
869 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:869",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(869u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["sym"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&sym as
&dyn Value))])
});
} else { ; }
};debug!(sym);
870 f.write_fmt(format_args!(" {0};\n", sym))writeln!(f, " {sym};")?;
871 }
872 f.write_fmt(format_args!("}};\n"))writeln!(f, "}};")?;
873 };
874 if let Err(error) = res {
875 self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
876 }
877 self.link_arg("--dynamic-list").link_arg(path);
878 } else {
879 let res = try {
881 let mut f = File::create_buffered(&path)?;
882 f.write_fmt(format_args!("{{\n"))writeln!(f, "{{")?;
883 if !symbols.is_empty() {
884 f.write_fmt(format_args!(" global:\n"))writeln!(f, " global:")?;
885 for (sym, _) in symbols {
886 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:886",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(886u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" {0};",
sym) as &dyn Value))])
});
} else { ; }
};debug!(" {sym};");
887 f.write_fmt(format_args!(" {0};\n", sym))writeln!(f, " {sym};")?;
888 }
889 }
890 f.write_fmt(format_args!("\n local:\n *;\n}};\n"))writeln!(f, "\n local:\n *;\n}};")?;
891 };
892 if let Err(error) = res {
893 self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
894 }
895 if self.sess.target.is_like_solaris {
896 self.link_arg("-M").link_arg(path);
897 } else {
898 let mut arg = OsString::from("--version-script=");
899 arg.push(path);
900 self.link_arg(arg).link_arg("--no-undefined-version");
901 }
902 }
903 }
904
905 fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
906 self.link_args(&["--subsystem", subsystem.as_str()]);
907 }
908
909 fn reset_per_library_state(&mut self) {
910 self.hint_dynamic(); }
912
913 fn linker_plugin_lto(&mut self) {
914 match self.sess.opts.cg.linker_plugin_lto {
915 LinkerPluginLto::Disabled => {
916 }
918 LinkerPluginLto::LinkerPluginAuto => {
919 self.push_linker_plugin_lto_args(None);
920 }
921 LinkerPluginLto::LinkerPlugin(ref path) => {
922 self.push_linker_plugin_lto_args(Some(path.as_os_str()));
923 }
924 }
925 }
926
927 fn add_eh_frame_header(&mut self) {
931 self.link_arg("--eh-frame-hdr");
932 }
933
934 fn add_no_exec(&mut self) {
935 if self.sess.target.is_like_windows {
936 self.link_arg("--nxcompat");
937 } else if self.is_gnu {
938 self.link_args(&["-z", "noexecstack"]);
939 }
940 }
941
942 fn add_as_needed(&mut self) {
943 if self.is_gnu && !self.sess.target.is_like_windows {
944 self.link_arg("--as-needed");
945 } else if self.sess.target.is_like_solaris {
946 self.link_args(&["-z", "ignore"]);
948 }
949 }
950}
951
952struct MsvcLinker<'a> {
953 cmd: Command,
954 sess: &'a Session,
955}
956
957impl<'a> Linker for MsvcLinker<'a> {
958 fn cmd(&mut self) -> &mut Command {
959 &mut self.cmd
960 }
961
962 fn set_output_kind(
963 &mut self,
964 output_kind: LinkOutputKind,
965 _crate_type: CrateType,
966 out_filename: &Path,
967 ) {
968 match output_kind {
969 LinkOutputKind::DynamicNoPicExe
970 | LinkOutputKind::DynamicPicExe
971 | LinkOutputKind::StaticNoPicExe
972 | LinkOutputKind::StaticPicExe => {}
973 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
974 self.link_arg("/DLL");
975 let mut arg: OsString = "/IMPLIB:".into();
976 arg.push(out_filename.with_extension("dll.lib"));
977 self.link_arg(arg);
978 }
979 LinkOutputKind::WasiReactorExe => {
980 {
::core::panicking::panic_fmt(format_args!("can\'t link as reactor on non-wasi target"));
};panic!("can't link as reactor on non-wasi target");
981 }
982 }
983 }
984
985 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
986 if let Some(path) = try_find_native_dynamic_library(self.sess, name, verbatim) {
989 self.link_arg(path);
990 } else {
991 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", name,
if verbatim { "" } else { ".lib" }))
})format!("{}{}", name, if verbatim { "" } else { ".lib" }));
992 }
993 }
994
995 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
996 let implib_path = path.with_extension("dll.lib");
999 if implib_path.exists() {
1000 self.link_or_cc_arg(implib_path);
1001 }
1002 }
1003
1004 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
1005 if let Some(path) = try_find_native_static_library(self.sess, name, verbatim) {
1008 self.link_staticlib_by_path(&path, whole_archive);
1009 } else {
1010 let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
1011 let (prefix, suffix) = self.sess.staticlib_components(verbatim);
1012 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}{2}{3}", opts, prefix, name,
suffix))
})format!("{opts}{prefix}{name}{suffix}"));
1013 }
1014 }
1015
1016 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1017 if !whole_archive {
1018 self.link_arg(path);
1019 } else {
1020 let mut arg = OsString::from("/WHOLEARCHIVE:");
1021 arg.push(path);
1022 self.link_arg(arg);
1023 }
1024 }
1025
1026 fn gc_sections(&mut self, _keep_metadata: bool) {
1027 if self.sess.opts.optimize != config::OptLevel::No {
1031 self.link_arg("/OPT:REF,ICF");
1032 } else {
1033 self.link_arg("/OPT:REF,NOICF");
1036 }
1037 }
1038
1039 fn full_relro(&mut self) {
1040 }
1042
1043 fn partial_relro(&mut self) {
1044 }
1046
1047 fn no_relro(&mut self) {
1048 }
1050
1051 fn no_crt_objects(&mut self) {
1052 }
1054
1055 fn no_default_libraries(&mut self) {
1056 self.link_arg("/NODEFAULTLIB");
1057 }
1058
1059 fn include_path(&mut self, path: &Path) {
1060 let mut arg = OsString::from("/LIBPATH:");
1061 arg.push(path);
1062 self.link_arg(&arg);
1063 }
1064
1065 fn output_filename(&mut self, path: &Path) {
1066 let mut arg = OsString::from("/OUT:");
1067 arg.push(path);
1068 self.link_arg(&arg);
1069 }
1070
1071 fn optimize(&mut self) {
1072 }
1074
1075 fn pgo_gen(&mut self) {
1076 }
1078
1079 fn control_flow_guard(&mut self) {
1080 self.link_arg("/guard:cf");
1081 }
1082
1083 fn ehcont_guard(&mut self) {
1084 if self.sess.target.pointer_width == 64 {
1085 self.link_arg("/guard:ehcont");
1086 }
1087 }
1088
1089 fn debuginfo(&mut self, _strip: Strip, natvis_debugger_visualizers: &[PathBuf]) {
1090 self.link_arg("/DEBUG");
1093
1094 self.link_arg("/PDBALTPATH:%_PDB%");
1102
1103 let natvis_dir_path = self.sess.opts.sysroot.path().join("lib\\rustlib\\etc");
1105 if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
1106 for entry in natvis_dir {
1107 match entry {
1108 Ok(entry) => {
1109 let path = entry.path();
1110 if path.extension() == Some("natvis".as_ref()) {
1111 let mut arg = OsString::from("/NATVIS:");
1112 arg.push(path);
1113 self.link_arg(arg);
1114 }
1115 }
1116 Err(error) => {
1117 self.sess.dcx().emit_warn(errors::NoNatvisDirectory { error });
1118 }
1119 }
1120 }
1121 }
1122
1123 for path in natvis_debugger_visualizers {
1125 let mut arg = OsString::from("/NATVIS:");
1126 arg.push(path);
1127 self.link_arg(arg);
1128 }
1129 }
1130
1131 fn export_symbols(
1144 &mut self,
1145 tmpdir: &Path,
1146 crate_type: CrateType,
1147 symbols: &[(String, SymbolExportKind)],
1148 ) {
1149 if crate_type == CrateType::Executable {
1151 let should_export_executable_symbols =
1152 self.sess.opts.unstable_opts.export_executable_symbols;
1153 if !should_export_executable_symbols {
1154 return;
1155 }
1156 }
1157
1158 let path = tmpdir.join("lib.def");
1159 let res = try {
1160 let mut f = File::create_buffered(&path)?;
1161
1162 f.write_fmt(format_args!("LIBRARY\n"))writeln!(f, "LIBRARY")?;
1165 f.write_fmt(format_args!("EXPORTS\n"))writeln!(f, "EXPORTS")?;
1166 for (symbol, kind) in symbols {
1167 let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
1168 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1168",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1168u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
symbol) as &dyn Value))])
});
} else { ; }
};debug!(" _{symbol}");
1169 f.write_fmt(format_args!(" {0}{1}\n", symbol, kind_marker))writeln!(f, " {symbol}{kind_marker}")?;
1170 }
1171 };
1172 if let Err(error) = res {
1173 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
1174 }
1175 let mut arg = OsString::from("/DEF:");
1176 arg.push(path);
1177 self.link_arg(&arg);
1178 }
1179
1180 fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1181 let subsystem = subsystem.as_str();
1182 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/SUBSYSTEM:{0}", subsystem))
})format!("/SUBSYSTEM:{subsystem}"));
1183
1184 if subsystem == "windows" {
1199 self.link_arg("/ENTRY:mainCRTStartup");
1200 }
1201 }
1202
1203 fn linker_plugin_lto(&mut self) {
1204 }
1206
1207 fn add_no_exec(&mut self) {
1208 self.link_arg("/NXCOMPAT");
1209 }
1210}
1211
1212struct EmLinker<'a> {
1213 cmd: Command,
1214 sess: &'a Session,
1215}
1216
1217impl<'a> Linker for EmLinker<'a> {
1218 fn cmd(&mut self) -> &mut Command {
1219 &mut self.cmd
1220 }
1221
1222 fn is_cc(&self) -> bool {
1223 true
1224 }
1225
1226 fn set_output_kind(
1227 &mut self,
1228 output_kind: LinkOutputKind,
1229 _crate_type: CrateType,
1230 _out_filename: &Path,
1231 ) {
1232 match output_kind {
1233 LinkOutputKind::DynamicNoPicExe | LinkOutputKind::DynamicPicExe => {
1234 self.cmd.arg("-sMAIN_MODULE=2");
1235 }
1236 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1237 self.cmd.arg("-sSIDE_MODULE=2");
1238 }
1239 LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {}
1241 LinkOutputKind::WasiReactorExe => {
1242 ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
1243 }
1244 }
1245 }
1246
1247 fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1248 self.link_or_cc_args(&["-l", name]);
1250 }
1251
1252 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1253 self.link_or_cc_arg(path);
1254 }
1255
1256 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
1257 self.link_or_cc_args(&["-l", name]);
1258 }
1259
1260 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1261 self.link_or_cc_arg(path);
1262 }
1263
1264 fn full_relro(&mut self) {
1265 }
1267
1268 fn partial_relro(&mut self) {
1269 }
1271
1272 fn no_relro(&mut self) {
1273 }
1275
1276 fn gc_sections(&mut self, _keep_metadata: bool) {
1277 }
1279
1280 fn optimize(&mut self) {
1281 self.cc_arg(match self.sess.opts.optimize {
1283 OptLevel::No => "-O0",
1284 OptLevel::Less => "-O1",
1285 OptLevel::More => "-O2",
1286 OptLevel::Aggressive => "-O3",
1287 OptLevel::Size => "-Os",
1288 OptLevel::SizeMin => "-Oz",
1289 });
1290 }
1291
1292 fn pgo_gen(&mut self) {
1293 }
1295
1296 fn control_flow_guard(&mut self) {}
1297
1298 fn ehcont_guard(&mut self) {}
1299
1300 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1301 self.cc_arg(match self.sess.opts.debuginfo {
1304 DebugInfo::None => "-g0",
1305 DebugInfo::Limited | DebugInfo::LineTablesOnly | DebugInfo::LineDirectivesOnly => {
1306 "--profiling-funcs"
1307 }
1308 DebugInfo::Full => "-g",
1309 });
1310 }
1311
1312 fn no_crt_objects(&mut self) {}
1313
1314 fn no_default_libraries(&mut self) {
1315 self.cc_arg("-nodefaultlibs");
1316 }
1317
1318 fn export_symbols(
1319 &mut self,
1320 _tmpdir: &Path,
1321 _crate_type: CrateType,
1322 symbols: &[(String, SymbolExportKind)],
1323 ) {
1324 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1324",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1324u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("EXPORTED SYMBOLS:")
as &dyn Value))])
});
} else { ; }
};debug!("EXPORTED SYMBOLS:");
1325
1326 self.cc_arg("-s");
1327
1328 let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
1329 let encoded = serde_json::to_string(
1330 &symbols.iter().map(|(sym, _)| "_".to_owned() + sym).collect::<Vec<_>>(),
1331 )
1332 .unwrap();
1333 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1333",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1333u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("{0}",
encoded) as &dyn Value))])
});
} else { ; }
};debug!("{encoded}");
1334
1335 arg.push(encoded);
1336
1337 self.cc_arg(arg);
1338 }
1339
1340 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {
1341 }
1343
1344 fn linker_plugin_lto(&mut self) {
1345 }
1347}
1348
1349struct WasmLd<'a> {
1350 cmd: Command,
1351 sess: &'a Session,
1352}
1353
1354impl<'a> WasmLd<'a> {
1355 fn new(cmd: Command, sess: &'a Session) -> WasmLd<'a> {
1356 WasmLd { cmd, sess }
1357 }
1358}
1359
1360impl<'a> Linker for WasmLd<'a> {
1361 fn cmd(&mut self) -> &mut Command {
1362 &mut self.cmd
1363 }
1364
1365 fn set_output_kind(
1366 &mut self,
1367 output_kind: LinkOutputKind,
1368 _crate_type: CrateType,
1369 _out_filename: &Path,
1370 ) {
1371 match output_kind {
1372 LinkOutputKind::DynamicNoPicExe
1373 | LinkOutputKind::DynamicPicExe
1374 | LinkOutputKind::StaticNoPicExe
1375 | LinkOutputKind::StaticPicExe => {}
1376 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1377 self.link_arg("--no-entry");
1378 }
1379 LinkOutputKind::WasiReactorExe => {
1380 self.link_args(&["--entry", "_initialize"]);
1381 }
1382 }
1383 }
1384
1385 fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1386 self.link_or_cc_args(&["-l", name]);
1387 }
1388
1389 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1390 self.link_or_cc_arg(path);
1391 }
1392
1393 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1394 if !whole_archive {
1395 self.link_or_cc_args(&["-l", name]);
1396 } else {
1397 self.link_arg("--whole-archive")
1398 .link_or_cc_args(&["-l", name])
1399 .link_arg("--no-whole-archive");
1400 }
1401 }
1402
1403 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1404 if !whole_archive {
1405 self.link_or_cc_arg(path);
1406 } else {
1407 self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1408 }
1409 }
1410
1411 fn full_relro(&mut self) {}
1412
1413 fn partial_relro(&mut self) {}
1414
1415 fn no_relro(&mut self) {}
1416
1417 fn gc_sections(&mut self, _keep_metadata: bool) {
1418 self.link_arg("--gc-sections");
1419 }
1420
1421 fn optimize(&mut self) {
1422 self.link_arg(match self.sess.opts.optimize {
1425 OptLevel::No => "-O0",
1426 OptLevel::Less => "-O1",
1427 OptLevel::More => "-O2",
1428 OptLevel::Aggressive => "-O3",
1429 OptLevel::Size => "-O2",
1432 OptLevel::SizeMin => "-O2",
1433 });
1434 }
1435
1436 fn pgo_gen(&mut self) {}
1437
1438 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1439 match strip {
1440 Strip::None => {}
1441 Strip::Debuginfo => {
1442 self.link_arg("--strip-debug");
1443 }
1444 Strip::Symbols => {
1445 self.link_arg("--strip-all");
1446 }
1447 }
1448 }
1449
1450 fn control_flow_guard(&mut self) {}
1451
1452 fn ehcont_guard(&mut self) {}
1453
1454 fn no_crt_objects(&mut self) {}
1455
1456 fn no_default_libraries(&mut self) {}
1457
1458 fn export_symbols(
1459 &mut self,
1460 _tmpdir: &Path,
1461 _crate_type: CrateType,
1462 symbols: &[(String, SymbolExportKind)],
1463 ) {
1464 for (sym, _) in symbols {
1465 self.link_args(&["--export", sym]);
1466 }
1467
1468 if #[allow(non_exhaustive_omitted_patterns)] match self.sess.target.os {
Os::Unknown | Os::None => true,
_ => false,
}matches!(self.sess.target.os, Os::Unknown | Os::None) {
1473 self.link_args(&["--export=__heap_base", "--export=__data_end"]);
1474 }
1475 }
1476
1477 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1478
1479 fn linker_plugin_lto(&mut self) {
1480 match self.sess.opts.cg.linker_plugin_lto {
1481 LinkerPluginLto::Disabled => {
1482 }
1484 LinkerPluginLto::LinkerPluginAuto => {
1485 self.push_linker_plugin_lto_args();
1486 }
1487 LinkerPluginLto::LinkerPlugin(_) => {
1488 self.push_linker_plugin_lto_args();
1489 }
1490 }
1491 }
1492}
1493
1494impl<'a> WasmLd<'a> {
1495 fn push_linker_plugin_lto_args(&mut self) {
1496 let opt_level = match self.sess.opts.optimize {
1497 config::OptLevel::No => "O0",
1498 config::OptLevel::Less => "O1",
1499 config::OptLevel::More => "O2",
1500 config::OptLevel::Aggressive => "O3",
1501 config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
1503 };
1504 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("--lto-{0}", opt_level))
})format!("--lto-{opt_level}"));
1505 }
1506}
1507
1508struct L4Bender<'a> {
1510 cmd: Command,
1511 sess: &'a Session,
1512 hinted_static: bool,
1513}
1514
1515impl<'a> Linker for L4Bender<'a> {
1516 fn cmd(&mut self) -> &mut Command {
1517 &mut self.cmd
1518 }
1519
1520 fn set_output_kind(
1521 &mut self,
1522 _output_kind: LinkOutputKind,
1523 _crate_type: CrateType,
1524 _out_filename: &Path,
1525 ) {
1526 }
1527
1528 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1529 self.hint_static();
1530 if !whole_archive {
1531 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-PC{0}", name))
})format!("-PC{name}"));
1532 } else {
1533 self.link_arg("--whole-archive")
1534 .link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}", name))
})format!("-l{name}"))
1535 .link_arg("--no-whole-archive");
1536 }
1537 }
1538
1539 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1540 self.hint_static();
1541 if !whole_archive {
1542 self.link_or_cc_arg(path);
1543 } else {
1544 self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1545 }
1546 }
1547
1548 fn full_relro(&mut self) {
1549 self.link_args(&["-z", "relro", "-z", "now"]);
1550 }
1551
1552 fn partial_relro(&mut self) {
1553 self.link_args(&["-z", "relro"]);
1554 }
1555
1556 fn no_relro(&mut self) {
1557 self.link_args(&["-z", "norelro"]);
1558 }
1559
1560 fn gc_sections(&mut self, keep_metadata: bool) {
1561 if !keep_metadata {
1562 self.link_arg("--gc-sections");
1563 }
1564 }
1565
1566 fn optimize(&mut self) {
1567 if self.sess.opts.optimize == config::OptLevel::More
1570 || self.sess.opts.optimize == config::OptLevel::Aggressive
1571 {
1572 self.link_arg("-O1");
1573 }
1574 }
1575
1576 fn pgo_gen(&mut self) {}
1577
1578 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1579 match strip {
1580 Strip::None => {}
1581 Strip::Debuginfo => {
1582 self.link_arg("--strip-debug");
1583 }
1584 Strip::Symbols => {
1585 self.link_arg("--strip-all");
1586 }
1587 }
1588 }
1589
1590 fn no_default_libraries(&mut self) {
1591 self.cc_arg("-nostdlib");
1592 }
1593
1594 fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[(String, SymbolExportKind)]) {
1595 self.sess.dcx().emit_warn(errors::L4BenderExportingSymbolsUnimplemented);
1597 }
1598
1599 fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1600 let subsystem = subsystem.as_str();
1601 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("--subsystem {0}", subsystem))
})format!("--subsystem {subsystem}"));
1602 }
1603
1604 fn reset_per_library_state(&mut self) {
1605 self.hint_static(); }
1607
1608 fn linker_plugin_lto(&mut self) {}
1609
1610 fn control_flow_guard(&mut self) {}
1611
1612 fn ehcont_guard(&mut self) {}
1613
1614 fn no_crt_objects(&mut self) {}
1615}
1616
1617impl<'a> L4Bender<'a> {
1618 fn new(cmd: Command, sess: &'a Session) -> L4Bender<'a> {
1619 L4Bender { cmd, sess, hinted_static: false }
1620 }
1621
1622 fn hint_static(&mut self) {
1623 if !self.hinted_static {
1624 self.link_or_cc_arg("-static");
1625 self.hinted_static = true;
1626 }
1627 }
1628}
1629
1630struct AixLinker<'a> {
1632 cmd: Command,
1633 sess: &'a Session,
1634 hinted_static: Option<bool>,
1635}
1636
1637impl<'a> AixLinker<'a> {
1638 fn new(cmd: Command, sess: &'a Session) -> AixLinker<'a> {
1639 AixLinker { cmd, sess, hinted_static: None }
1640 }
1641
1642 fn hint_static(&mut self) {
1643 if self.hinted_static != Some(true) {
1644 self.link_arg("-bstatic");
1645 self.hinted_static = Some(true);
1646 }
1647 }
1648
1649 fn hint_dynamic(&mut self) {
1650 if self.hinted_static != Some(false) {
1651 self.link_arg("-bdynamic");
1652 self.hinted_static = Some(false);
1653 }
1654 }
1655
1656 fn build_dylib(&mut self, _out_filename: &Path) {
1657 self.link_args(&["-bM:SRE", "-bnoentry"]);
1658 self.link_arg("-bexpfull");
1661 }
1662}
1663
1664impl<'a> Linker for AixLinker<'a> {
1665 fn cmd(&mut self) -> &mut Command {
1666 &mut self.cmd
1667 }
1668
1669 fn set_output_kind(
1670 &mut self,
1671 output_kind: LinkOutputKind,
1672 _crate_type: CrateType,
1673 out_filename: &Path,
1674 ) {
1675 match output_kind {
1676 LinkOutputKind::DynamicDylib => {
1677 self.hint_dynamic();
1678 self.build_dylib(out_filename);
1679 }
1680 LinkOutputKind::StaticDylib => {
1681 self.hint_static();
1682 self.build_dylib(out_filename);
1683 }
1684 _ => {}
1685 }
1686 }
1687
1688 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
1689 self.hint_dynamic();
1690 self.link_or_cc_arg(if verbatim { String::from(name) } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}", name))
})format!("-l{name}") });
1691 }
1692
1693 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1694 self.hint_dynamic();
1695 self.link_or_cc_arg(path);
1696 }
1697
1698 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
1699 self.hint_static();
1700 if !whole_archive {
1701 self.link_or_cc_arg(if verbatim { String::from(name) } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}", name))
})format!("-l{name}") });
1702 } else {
1703 let mut arg = OsString::from("-bkeepfile:");
1704 arg.push(find_native_static_library(name, verbatim, self.sess));
1705 self.link_or_cc_arg(arg);
1706 }
1707 }
1708
1709 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1710 self.hint_static();
1711 if !whole_archive {
1712 self.link_or_cc_arg(path);
1713 } else {
1714 let mut arg = OsString::from("-bkeepfile:");
1715 arg.push(path);
1716 self.link_arg(arg);
1717 }
1718 }
1719
1720 fn full_relro(&mut self) {}
1721
1722 fn partial_relro(&mut self) {}
1723
1724 fn no_relro(&mut self) {}
1725
1726 fn gc_sections(&mut self, _keep_metadata: bool) {
1727 self.link_arg("-bgc");
1728 }
1729
1730 fn optimize(&mut self) {}
1731
1732 fn pgo_gen(&mut self) {
1733 self.link_arg("-bdbg:namedsects:ss");
1734 self.link_arg("-u");
1735 self.link_arg("__llvm_profile_runtime");
1736 }
1737
1738 fn control_flow_guard(&mut self) {}
1739
1740 fn ehcont_guard(&mut self) {}
1741
1742 fn debuginfo(&mut self, _: Strip, _: &[PathBuf]) {}
1743
1744 fn no_crt_objects(&mut self) {}
1745
1746 fn no_default_libraries(&mut self) {}
1747
1748 fn export_symbols(
1749 &mut self,
1750 tmpdir: &Path,
1751 _crate_type: CrateType,
1752 symbols: &[(String, SymbolExportKind)],
1753 ) {
1754 let path = tmpdir.join("list.exp");
1755 let res = try {
1756 let mut f = File::create_buffered(&path)?;
1757 for (symbol, _) in symbols {
1759 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1759",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1759u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
symbol) as &dyn Value))])
});
} else { ; }
};debug!(" _{symbol}");
1760 f.write_fmt(format_args!(" {0}\n", symbol))writeln!(f, " {symbol}")?;
1761 }
1762 };
1763 if let Err(e) = res {
1764 self.sess.dcx().fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to write export file: {0}",
e))
})format!("failed to write export file: {e}"));
1765 }
1766 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-bE:{0}", path.to_str().unwrap()))
})format!("-bE:{}", path.to_str().unwrap()));
1767 }
1768
1769 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1770
1771 fn reset_per_library_state(&mut self) {
1772 self.hint_dynamic();
1773 }
1774
1775 fn linker_plugin_lto(&mut self) {}
1776
1777 fn add_eh_frame_header(&mut self) {}
1778
1779 fn add_no_exec(&mut self) {}
1780
1781 fn add_as_needed(&mut self) {}
1782}
1783
1784fn for_each_exported_symbols_include_dep<'tcx>(
1785 tcx: TyCtxt<'tcx>,
1786 crate_type: CrateType,
1787 mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum),
1788) {
1789 let formats = tcx.dependency_formats(());
1790 let deps = &formats[&crate_type];
1791
1792 for (cnum, dep_format) in deps.iter_enumerated() {
1793 if *dep_format == Linkage::Static {
1795 for &(symbol, info) in tcx.exported_non_generic_symbols(cnum).iter() {
1796 callback(symbol, info, cnum);
1797 }
1798 for &(symbol, info) in tcx.exported_generic_symbols(cnum).iter() {
1799 callback(symbol, info, cnum);
1800 }
1801 }
1802 }
1803}
1804
1805pub(crate) fn exported_symbols(
1806 tcx: TyCtxt<'_>,
1807 crate_type: CrateType,
1808) -> Vec<(String, SymbolExportKind)> {
1809 if let Some(ref exports) = tcx.sess.target.override_export_symbols {
1810 return exports
1811 .iter()
1812 .map(|name| {
1813 (
1814 name.to_string(),
1815 SymbolExportKind::Text,
1819 )
1820 })
1821 .collect();
1822 }
1823
1824 let mut symbols = if let CrateType::ProcMacro = crate_type {
1825 exported_symbols_for_proc_macro_crate(tcx)
1826 } else {
1827 exported_symbols_for_non_proc_macro(tcx, crate_type)
1828 };
1829
1830 if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
1831 let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx);
1832 symbols.push((metadata_symbol_name, SymbolExportKind::Data));
1833 }
1834
1835 symbols
1836}
1837
1838fn exported_symbols_for_non_proc_macro(
1839 tcx: TyCtxt<'_>,
1840 crate_type: CrateType,
1841) -> Vec<(String, SymbolExportKind)> {
1842 let mut symbols = Vec::new();
1843 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1844 for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1845 if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum) {
1849 symbols.push((
1850 symbol_export::exporting_symbol_name_for_instance_in_crate(tcx, symbol, cnum),
1851 info.kind,
1852 ));
1853 symbol_export::extend_exported_symbols(&mut symbols, tcx, symbol, cnum);
1854 }
1855 });
1856
1857 if export_threshold == SymbolExportLevel::Rust
1859 && needs_allocator_shim_for_linking(tcx.dependency_formats(()), crate_type)
1860 && let Some(kind) = tcx.allocator_kind(())
1861 {
1862 symbols.extend(allocator_shim_symbols(tcx, kind));
1863 }
1864
1865 symbols
1866}
1867
1868fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<(String, SymbolExportKind)> {
1869 if !tcx.sess.opts.output_types.should_codegen() {
1871 return Vec::new();
1872 }
1873
1874 let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
1875 let proc_macro_decls_name = tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
1876
1877 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(proc_macro_decls_name, SymbolExportKind::Data)]))vec![(proc_macro_decls_name, SymbolExportKind::Data)]
1878}
1879
1880pub(crate) fn linked_symbols(
1881 tcx: TyCtxt<'_>,
1882 crate_type: CrateType,
1883) -> Vec<(String, SymbolExportKind)> {
1884 match crate_type {
1885 CrateType::Executable
1886 | CrateType::ProcMacro
1887 | CrateType::Cdylib
1888 | CrateType::Dylib
1889 | CrateType::Sdylib => (),
1890 CrateType::StaticLib | CrateType::Rlib => {
1891 return Vec::new();
1893 }
1894 }
1895
1896 match tcx.sess.lto() {
1897 Lto::No | Lto::ThinLocal => {}
1898 Lto::Thin | Lto::Fat => {
1899 return Vec::new();
1908 }
1909 }
1910
1911 let mut symbols = Vec::new();
1912
1913 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1914 for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1915 if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum)
1916 || info.used
1917 || info.rustc_std_internal_symbol
1918 {
1919 symbols.push((
1920 symbol_export::linking_symbol_name_for_instance_in_crate(
1921 tcx, symbol, info.kind, cnum,
1922 ),
1923 info.kind,
1924 ));
1925 }
1926 });
1927
1928 symbols
1929}
1930
1931struct PtxLinker<'a> {
1934 cmd: Command,
1935 sess: &'a Session,
1936}
1937
1938impl<'a> Linker for PtxLinker<'a> {
1939 fn cmd(&mut self) -> &mut Command {
1940 &mut self.cmd
1941 }
1942
1943 fn set_output_kind(
1944 &mut self,
1945 _output_kind: LinkOutputKind,
1946 _crate_type: CrateType,
1947 _out_filename: &Path,
1948 ) {
1949 }
1950
1951 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
1952 { ::core::panicking::panic_fmt(format_args!("staticlibs not supported")); }panic!("staticlibs not supported")
1953 }
1954
1955 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1956 self.link_arg("--rlib").link_arg(path);
1957 }
1958
1959 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1960 self.link_arg("--debug");
1961 }
1962
1963 fn add_object(&mut self, path: &Path) {
1964 self.link_arg("--bitcode").link_arg(path);
1965 }
1966
1967 fn optimize(&mut self) {
1968 match self.sess.lto() {
1969 Lto::Thin | Lto::Fat | Lto::ThinLocal => {
1970 self.link_arg("-Olto");
1971 }
1972
1973 Lto::No => {}
1974 }
1975 }
1976
1977 fn full_relro(&mut self) {}
1978
1979 fn partial_relro(&mut self) {}
1980
1981 fn no_relro(&mut self) {}
1982
1983 fn gc_sections(&mut self, _keep_metadata: bool) {}
1984
1985 fn pgo_gen(&mut self) {}
1986
1987 fn no_crt_objects(&mut self) {}
1988
1989 fn no_default_libraries(&mut self) {}
1990
1991 fn control_flow_guard(&mut self) {}
1992
1993 fn ehcont_guard(&mut self) {}
1994
1995 fn export_symbols(
1996 &mut self,
1997 _tmpdir: &Path,
1998 _crate_type: CrateType,
1999 _symbols: &[(String, SymbolExportKind)],
2000 ) {
2001 }
2002
2003 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
2004
2005 fn linker_plugin_lto(&mut self) {}
2006}
2007
2008struct LlbcLinker<'a> {
2010 cmd: Command,
2011 sess: &'a Session,
2012}
2013
2014impl<'a> Linker for LlbcLinker<'a> {
2015 fn cmd(&mut self) -> &mut Command {
2016 &mut self.cmd
2017 }
2018
2019 fn set_output_kind(
2020 &mut self,
2021 _output_kind: LinkOutputKind,
2022 _crate_type: CrateType,
2023 _out_filename: &Path,
2024 ) {
2025 }
2026
2027 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
2028 { ::core::panicking::panic_fmt(format_args!("staticlibs not supported")); }panic!("staticlibs not supported")
2029 }
2030
2031 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2032 self.link_or_cc_arg(path);
2033 }
2034
2035 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2036 self.link_arg("--debug");
2037 }
2038
2039 fn optimize(&mut self) {
2040 self.link_arg(match self.sess.opts.optimize {
2041 OptLevel::No => "-O0",
2042 OptLevel::Less => "-O1",
2043 OptLevel::More => "-O2",
2044 OptLevel::Aggressive => "-O3",
2045 OptLevel::Size => "-Os",
2046 OptLevel::SizeMin => "-Oz",
2047 });
2048 }
2049
2050 fn full_relro(&mut self) {}
2051
2052 fn partial_relro(&mut self) {}
2053
2054 fn no_relro(&mut self) {}
2055
2056 fn gc_sections(&mut self, _keep_metadata: bool) {}
2057
2058 fn pgo_gen(&mut self) {}
2059
2060 fn no_crt_objects(&mut self) {}
2061
2062 fn no_default_libraries(&mut self) {}
2063
2064 fn control_flow_guard(&mut self) {}
2065
2066 fn ehcont_guard(&mut self) {}
2067
2068 fn export_symbols(
2069 &mut self,
2070 _tmpdir: &Path,
2071 _crate_type: CrateType,
2072 symbols: &[(String, SymbolExportKind)],
2073 ) {
2074 match _crate_type {
2075 CrateType::Cdylib => {
2076 for (sym, _) in symbols {
2077 self.link_args(&["--export-symbol", sym]);
2078 }
2079 }
2080 _ => (),
2081 }
2082 }
2083
2084 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
2085
2086 fn linker_plugin_lto(&mut self) {}
2087}
2088
2089struct BpfLinker<'a> {
2090 cmd: Command,
2091 sess: &'a Session,
2092}
2093
2094impl<'a> Linker for BpfLinker<'a> {
2095 fn cmd(&mut self) -> &mut Command {
2096 &mut self.cmd
2097 }
2098
2099 fn set_output_kind(
2100 &mut self,
2101 _output_kind: LinkOutputKind,
2102 _crate_type: CrateType,
2103 _out_filename: &Path,
2104 ) {
2105 }
2106
2107 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
2108 self.sess.dcx().emit_fatal(errors::BpfStaticlibNotSupported)
2109 }
2110
2111 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2112 self.link_or_cc_arg(path);
2113 }
2114
2115 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2116 self.link_arg("--debug");
2117 }
2118
2119 fn optimize(&mut self) {
2120 self.link_arg(match self.sess.opts.optimize {
2121 OptLevel::No => "-O0",
2122 OptLevel::Less => "-O1",
2123 OptLevel::More => "-O2",
2124 OptLevel::Aggressive => "-O3",
2125 OptLevel::Size => "-Os",
2126 OptLevel::SizeMin => "-Oz",
2127 });
2128 }
2129
2130 fn full_relro(&mut self) {}
2131
2132 fn partial_relro(&mut self) {}
2133
2134 fn no_relro(&mut self) {}
2135
2136 fn gc_sections(&mut self, _keep_metadata: bool) {}
2137
2138 fn pgo_gen(&mut self) {}
2139
2140 fn no_crt_objects(&mut self) {}
2141
2142 fn no_default_libraries(&mut self) {}
2143
2144 fn control_flow_guard(&mut self) {}
2145
2146 fn ehcont_guard(&mut self) {}
2147
2148 fn export_symbols(
2149 &mut self,
2150 tmpdir: &Path,
2151 _crate_type: CrateType,
2152 symbols: &[(String, SymbolExportKind)],
2153 ) {
2154 let path = tmpdir.join("symbols");
2155 let res = try {
2156 let mut f = File::create_buffered(&path)?;
2157 for (sym, _) in symbols {
2158 f.write_fmt(format_args!("{0}\n", sym))writeln!(f, "{sym}")?;
2159 }
2160 };
2161 if let Err(error) = res {
2162 self.sess.dcx().emit_fatal(errors::SymbolFileWriteFailure { error });
2163 } else {
2164 self.link_arg("--export-symbols").link_arg(&path);
2165 }
2166 }
2167
2168 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
2169
2170 fn linker_plugin_lto(&mut self) {}
2171}