1use std::ffi::{OsStr, OsString};
2use std::fs::{self, File};
3use std::io::prelude::*;
4use std::path::{Path, PathBuf};
5use std::{env, iter, mem, str};
6
7use find_msvc_tools;
8use rustc_hir::attrs::WindowsSubsystemKind;
9use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
10use rustc_metadata::{
11 find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
12};
13use rustc_middle::bug;
14use rustc_middle::middle::dependency_format::Linkage;
15use rustc_middle::middle::exported_symbols::{
16 self, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
17};
18use rustc_middle::ty::TyCtxt;
19use rustc_session::Session;
20use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
21use rustc_target::spec::{Abi, Arch, Cc, LinkOutputKind, LinkerFlavor, Lld, Os};
22use tracing::{debug, warn};
23
24use super::command::Command;
25use super::symbol_export;
26use crate::back::symbol_export::allocator_shim_symbols;
27use crate::base::needs_allocator_shim_for_linking;
28use crate::errors;
29
30#[cfg(test)]
31mod tests;
32
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.abi == Abi::Uwp {
88 if let Some(ref tool) = msvc_tool {
89 let original_path = tool.path();
90 if let Some(root_lib_path) = original_path.ancestors().nth(4) {
91 let arch = match t.arch {
92 Arch::X86_64 => Some("x64"),
93 Arch::X86 => Some("x86"),
94 Arch::AArch64 => Some("arm64"),
95 Arch::Arm => Some("arm"),
96 _ => None,
97 };
98 if let Some(ref a) = arch {
99 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.abi == Abi::Uwp) {
::core::panicking::panic("assertion failed: cmd.get_args().is_empty() || sess.target.abi == Abi::Uwp")
};assert!(cmd.get_args().is_empty() || sess.target.abi == Abi::Uwp);
139 match flavor {
140 LinkerFlavor::Unix(Cc::No) if sess.target.os == Os::L4Re => {
141 Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>
142 }
143 LinkerFlavor::Unix(Cc::No) if sess.target.os == Os::Aix => {
144 Box::new(AixLinker::new(cmd, sess)) as Box<dyn Linker>
145 }
146 LinkerFlavor::WasmLld(Cc::No) => Box::new(WasmLd::new(cmd, sess)) as Box<dyn Linker>,
147 LinkerFlavor::Gnu(cc, _)
148 | LinkerFlavor::Darwin(cc, _)
149 | LinkerFlavor::WasmLld(cc)
150 | LinkerFlavor::Unix(cc) => Box::new(GccLinker {
151 cmd,
152 sess,
153 target_cpu,
154 hinted_static: None,
155 is_ld: cc == Cc::No,
156 is_gnu: flavor.is_gnu(),
157 uses_lld: flavor.uses_lld(),
158 codegen_backend,
159 }) as Box<dyn Linker>,
160 LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>,
161 LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
162 LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
163 LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box<dyn Linker>,
164 LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
165 }
166}
167
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}
356
357impl dyn Linker + '_ {
358 pub(crate) fn take_cmd(&mut self) -> Command {
359 mem::replace(self.cmd(), Command::new(""))
360 }
361}
362
363struct GccLinker<'a> {
364 cmd: Command,
365 sess: &'a Session,
366 target_cpu: &'a str,
367 hinted_static: Option<bool>, is_ld: bool,
370 is_gnu: bool,
371 uses_lld: bool,
372 codegen_backend: &'static str,
373}
374
375impl<'a> GccLinker<'a> {
376 fn takes_hints(&self) -> bool {
377 !self.sess.target.is_like_darwin && !self.sess.target.is_like_wasm
386 }
387
388 fn hint_static(&mut self) {
393 if !self.takes_hints() {
394 return;
395 }
396 if self.hinted_static != Some(true) {
397 self.link_arg("-Bstatic");
398 self.hinted_static = Some(true);
399 }
400 }
401
402 fn hint_dynamic(&mut self) {
403 if !self.takes_hints() {
404 return;
405 }
406 if self.hinted_static != Some(false) {
407 self.link_arg("-Bdynamic");
408 self.hinted_static = Some(false);
409 }
410 }
411
412 fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
413 if let Some(plugin_path) = plugin_path {
414 let mut arg = OsString::from("-plugin=");
415 arg.push(plugin_path);
416 self.link_arg(&arg);
417 }
418
419 let opt_level = match self.sess.opts.optimize {
420 config::OptLevel::No => "O0",
421 config::OptLevel::Less => "O1",
422 config::OptLevel::More | config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
423 config::OptLevel::Aggressive => "O3",
424 };
425
426 if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
427 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-plugin-opt=sample-profile={0}",
path.display()))
})format!("-plugin-opt=sample-profile={}", path.display()));
428 };
429 let prefix = if self.codegen_backend == "gcc" {
430 "-"
432 } else {
433 ""
434 };
435 self.link_args(&[
436 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-plugin-opt={0}{1}", prefix,
opt_level))
})format!("-plugin-opt={prefix}{opt_level}"),
437 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-plugin-opt={1}mcpu={0}",
self.target_cpu, prefix))
})format!("-plugin-opt={prefix}mcpu={}", self.target_cpu),
438 ]);
439 }
440
441 fn build_dylib(&mut self, crate_type: CrateType, out_filename: &Path) {
442 if self.sess.target.is_like_darwin {
444 if self.is_cc() {
445 self.cc_arg("-dynamiclib");
447 } else {
448 self.link_arg("-dylib");
449 }
451
452 if self.sess.opts.cg.rpath || self.sess.opts.unstable_opts.osx_rpath_install_name {
457 let mut rpath = OsString::from("@rpath/");
458 rpath.push(out_filename.file_name().unwrap());
459 self.link_arg("-install_name").link_arg(rpath);
460 }
461 } else {
462 self.link_or_cc_arg("-shared");
463 if let Some(name) = out_filename.file_name() {
464 if self.sess.target.is_like_windows {
465 let (prefix, suffix) = self.sess.staticlib_components(false);
469 let mut implib_name = OsString::from(prefix);
470 implib_name.push(name);
471 implib_name.push(suffix);
472 let mut out_implib = OsString::from("--out-implib=");
473 out_implib.push(out_filename.with_file_name(implib_name));
474 self.link_arg(out_implib);
475 } else if crate_type == CrateType::Dylib {
476 let mut soname = OsString::from("-soname=");
480 soname.push(name);
481 self.link_arg(soname);
482 }
483 }
484 }
485 }
486
487 fn with_as_needed(&mut self, as_needed: bool, f: impl FnOnce(&mut Self)) {
488 if !as_needed {
489 if self.sess.target.is_like_darwin {
490 self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
494 } else if self.is_gnu && !self.sess.target.is_like_windows {
495 self.link_arg("--no-as-needed");
496 } else {
497 self.sess.dcx().emit_warn(errors::LinkerUnsupportedModifier);
498 }
499 }
500
501 f(self);
502
503 if !as_needed {
504 if self.sess.target.is_like_darwin {
505 } else if self.is_gnu && !self.sess.target.is_like_windows {
507 self.link_arg("--as-needed");
508 }
509 }
510 }
511}
512
513impl<'a> Linker for GccLinker<'a> {
514 fn cmd(&mut self) -> &mut Command {
515 &mut self.cmd
516 }
517
518 fn is_cc(&self) -> bool {
519 !self.is_ld
520 }
521
522 fn set_output_kind(
523 &mut self,
524 output_kind: LinkOutputKind,
525 crate_type: CrateType,
526 out_filename: &Path,
527 ) {
528 match output_kind {
529 LinkOutputKind::DynamicNoPicExe => {
530 if !self.is_ld && self.is_gnu && !self.sess.target.is_like_windows {
532 self.cc_arg("-no-pie");
533 }
534 }
535 LinkOutputKind::DynamicPicExe => {
536 if !self.sess.target.is_like_windows {
538 self.link_or_cc_arg("-pie");
540 }
541 }
542 LinkOutputKind::StaticNoPicExe => {
543 self.link_or_cc_arg("-static");
545 if !self.is_ld && self.is_gnu {
546 self.cc_arg("-no-pie");
547 }
548 }
549 LinkOutputKind::StaticPicExe => {
550 if !self.is_ld {
551 self.cc_arg("-static-pie");
554 } else {
555 self.link_args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
561 }
562 }
563 LinkOutputKind::DynamicDylib => self.build_dylib(crate_type, out_filename),
564 LinkOutputKind::StaticDylib => {
565 self.link_or_cc_arg("-static");
566 self.build_dylib(crate_type, out_filename);
567 }
568 LinkOutputKind::WasiReactorExe => {
569 self.link_args(&["--entry", "_initialize"]);
570 }
571 }
572
573 if self.sess.target.os == Os::VxWorks
579 && #[allow(non_exhaustive_omitted_patterns)] match output_kind {
LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe |
LinkOutputKind::StaticDylib => true,
_ => false,
}matches!(
580 output_kind,
581 LinkOutputKind::StaticNoPicExe
582 | LinkOutputKind::StaticPicExe
583 | LinkOutputKind::StaticDylib
584 )
585 {
586 self.cc_arg("--static-crt");
587 }
588
589 if self.sess.target.arch == Arch::Avr && !self.uses_lld {
595 self.verbatim_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-mmcu={0}", self.target_cpu))
})format!("-mmcu={}", self.target_cpu));
596 }
597 }
598
599 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool) {
600 if self.sess.target.os == Os::Illumos && name == "c" {
601 return;
607 }
608 self.hint_dynamic();
609 self.with_as_needed(as_needed, |this| {
610 let colon = if verbatim && this.is_gnu { ":" } else { "" };
611 this.link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
})format!("-l{colon}{name}"));
612 });
613 }
614
615 fn link_dylib_by_path(&mut self, path: &Path, as_needed: bool) {
616 self.hint_dynamic();
617 self.with_as_needed(as_needed, |this| {
618 this.link_or_cc_arg(path);
619 })
620 }
621
622 fn link_framework_by_name(&mut self, name: &str, _verbatim: bool, as_needed: bool) {
623 self.hint_dynamic();
624 if !as_needed {
625 self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
629 }
630 self.link_or_cc_args(&["-framework", name]);
631 }
632
633 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
634 self.hint_static();
635 let colon = if verbatim && self.is_gnu { ":" } else { "" };
636 if !whole_archive {
637 self.link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
})format!("-l{colon}{name}"));
638 } else if self.sess.target.is_like_darwin {
639 self.link_arg("-force_load");
642 self.link_arg(find_native_static_library(name, verbatim, self.sess));
643 } else {
644 self.link_arg("--whole-archive")
645 .link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}{1}", colon, name))
})format!("-l{colon}{name}"))
646 .link_arg("--no-whole-archive");
647 }
648 }
649
650 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
651 self.hint_static();
652 if !whole_archive {
653 self.link_or_cc_arg(path);
654 } else if self.sess.target.is_like_darwin {
655 self.link_arg("-force_load").link_arg(path);
656 } else {
657 self.link_arg("--whole-archive").link_arg(path).link_arg("--no-whole-archive");
658 }
659 }
660
661 fn framework_path(&mut self, path: &Path) {
662 self.link_or_cc_arg("-F").link_or_cc_arg(path);
663 }
664 fn full_relro(&mut self) {
665 self.link_args(&["-z", "relro", "-z", "now"]);
666 }
667 fn partial_relro(&mut self) {
668 self.link_args(&["-z", "relro"]);
669 }
670 fn no_relro(&mut self) {
671 self.link_args(&["-z", "norelro"]);
672 }
673
674 fn gc_sections(&mut self, keep_metadata: bool) {
675 if self.sess.target.is_like_darwin {
690 self.link_arg("-dead_strip");
691
692 } else if (self.is_gnu || self.sess.target.is_like_wasm) && !keep_metadata {
698 self.link_arg("--gc-sections");
699 }
700 }
701
702 fn optimize(&mut self) {
703 if !self.is_gnu && !self.sess.target.is_like_wasm {
704 return;
705 }
706
707 if self.sess.opts.optimize == config::OptLevel::More
710 || self.sess.opts.optimize == config::OptLevel::Aggressive
711 {
712 self.link_arg("-O1");
713 }
714 }
715
716 fn pgo_gen(&mut self) {
717 if !self.is_gnu {
718 return;
719 }
720
721 self.link_or_cc_args(&["-u", "__llvm_profile_runtime"]);
733 }
734
735 fn control_flow_guard(&mut self) {}
736
737 fn ehcont_guard(&mut self) {}
738
739 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
740 if self.sess.target.is_like_darwin {
742 return;
743 }
744
745 match strip {
746 Strip::None => {}
747 Strip::Debuginfo => {
748 if !self.sess.target.is_like_solaris {
753 self.link_arg("--strip-debug");
754 }
755 }
756 Strip::Symbols => {
757 self.link_arg("--strip-all");
758 }
759 }
760 match self.sess.opts.unstable_opts.debuginfo_compression {
761 config::DebugInfoCompression::None => {}
762 config::DebugInfoCompression::Zlib => {
763 self.link_arg("--compress-debug-sections=zlib");
764 }
765 config::DebugInfoCompression::Zstd => {
766 self.link_arg("--compress-debug-sections=zstd");
767 }
768 }
769 }
770
771 fn no_crt_objects(&mut self) {
772 if !self.is_ld {
773 self.cc_arg("-nostartfiles");
774 }
775 }
776
777 fn no_default_libraries(&mut self) {
778 if !self.is_ld {
779 self.cc_arg("-nodefaultlibs");
780 }
781 }
782
783 fn export_symbols(
784 &mut self,
785 tmpdir: &Path,
786 crate_type: CrateType,
787 symbols: &[(String, SymbolExportKind)],
788 ) {
789 if crate_type == CrateType::Executable {
791 let should_export_executable_symbols =
792 self.sess.opts.unstable_opts.export_executable_symbols;
793 if self.sess.target.override_export_symbols.is_none()
794 && !should_export_executable_symbols
795 {
796 return;
797 }
798 }
799
800 if !self.sess.target.limit_rdylib_exports {
805 return;
806 }
807
808 let path = tmpdir.join(if self.sess.target.is_like_windows { "list.def" } else { "list" });
809 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:809",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(809u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("EXPORTED SYMBOLS:")
as &dyn Value))])
});
} else { ; }
};debug!("EXPORTED SYMBOLS:");
810
811 if self.sess.target.is_like_darwin {
812 let res = try {
814 let mut f = File::create_buffered(&path)?;
815 for (sym, _) in symbols {
816 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:816",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(816u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
sym) as &dyn Value))])
});
} else { ; }
};debug!(" _{sym}");
817 f.write_fmt(format_args!("_{0}\n", sym))writeln!(f, "_{sym}")?;
818 }
819 };
820 if let Err(error) = res {
821 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
822 }
823 self.link_arg("-exported_symbols_list").link_arg(path);
824 } else if self.sess.target.is_like_windows {
825 let res = try {
826 let mut f = File::create_buffered(&path)?;
827
828 f.write_fmt(format_args!("EXPORTS\n"))writeln!(f, "EXPORTS")?;
831 for (symbol, kind) in symbols {
832 let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
833 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:833",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(833u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
symbol) as &dyn Value))])
});
} else { ; }
};debug!(" _{symbol}");
834 f.write_fmt(format_args!(" \"{0}\"{1}\n", symbol, kind_marker))writeln!(f, " \"{symbol}\"{kind_marker}")?;
837 }
838 };
839 if let Err(error) = res {
840 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
841 }
842 self.link_arg(path);
843 } else if self.sess.target.is_like_wasm {
844 self.link_arg("--no-export-dynamic");
845 for (sym, _) in symbols {
846 self.link_arg("--export").link_arg(sym);
847 }
848 } else if crate_type == CrateType::Executable && !self.sess.target.is_like_solaris {
849 let res = try {
850 let mut f = File::create_buffered(&path)?;
851 f.write_fmt(format_args!("{{\n"))writeln!(f, "{{")?;
852 for (sym, _) in symbols {
853 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:853",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(853u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["sym"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&sym as
&dyn Value))])
});
} else { ; }
};debug!(sym);
854 f.write_fmt(format_args!(" {0};\n", sym))writeln!(f, " {sym};")?;
855 }
856 f.write_fmt(format_args!("}};\n"))writeln!(f, "}};")?;
857 };
858 if let Err(error) = res {
859 self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
860 }
861 self.link_arg("--dynamic-list").link_arg(path);
862 } else {
863 let res = try {
865 let mut f = File::create_buffered(&path)?;
866 f.write_fmt(format_args!("{{\n"))writeln!(f, "{{")?;
867 if !symbols.is_empty() {
868 f.write_fmt(format_args!(" global:\n"))writeln!(f, " global:")?;
869 for (sym, _) in symbols {
870 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:870",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(870u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" {0};",
sym) as &dyn Value))])
});
} else { ; }
};debug!(" {sym};");
871 f.write_fmt(format_args!(" {0};\n", sym))writeln!(f, " {sym};")?;
872 }
873 }
874 f.write_fmt(format_args!("\n local:\n *;\n}};\n"))writeln!(f, "\n local:\n *;\n}};")?;
875 };
876 if let Err(error) = res {
877 self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
878 }
879 if self.sess.target.is_like_solaris {
880 self.link_arg("-M").link_arg(path);
881 } else {
882 let mut arg = OsString::from("--version-script=");
883 arg.push(path);
884 self.link_arg(arg).link_arg("--no-undefined-version");
885 }
886 }
887 }
888
889 fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
890 self.link_args(&["--subsystem", subsystem.as_str()]);
891 }
892
893 fn reset_per_library_state(&mut self) {
894 self.hint_dynamic(); }
896
897 fn linker_plugin_lto(&mut self) {
898 match self.sess.opts.cg.linker_plugin_lto {
899 LinkerPluginLto::Disabled => {
900 }
902 LinkerPluginLto::LinkerPluginAuto => {
903 self.push_linker_plugin_lto_args(None);
904 }
905 LinkerPluginLto::LinkerPlugin(ref path) => {
906 self.push_linker_plugin_lto_args(Some(path.as_os_str()));
907 }
908 }
909 }
910
911 fn add_eh_frame_header(&mut self) {
915 self.link_arg("--eh-frame-hdr");
916 }
917
918 fn add_no_exec(&mut self) {
919 if self.sess.target.is_like_windows {
920 self.link_arg("--nxcompat");
921 } else if self.is_gnu {
922 self.link_args(&["-z", "noexecstack"]);
923 }
924 }
925
926 fn add_as_needed(&mut self) {
927 if self.is_gnu && !self.sess.target.is_like_windows {
928 self.link_arg("--as-needed");
929 } else if self.sess.target.is_like_solaris {
930 self.link_args(&["-z", "ignore"]);
932 }
933 }
934}
935
936struct MsvcLinker<'a> {
937 cmd: Command,
938 sess: &'a Session,
939}
940
941impl<'a> Linker for MsvcLinker<'a> {
942 fn cmd(&mut self) -> &mut Command {
943 &mut self.cmd
944 }
945
946 fn set_output_kind(
947 &mut self,
948 output_kind: LinkOutputKind,
949 _crate_type: CrateType,
950 out_filename: &Path,
951 ) {
952 match output_kind {
953 LinkOutputKind::DynamicNoPicExe
954 | LinkOutputKind::DynamicPicExe
955 | LinkOutputKind::StaticNoPicExe
956 | LinkOutputKind::StaticPicExe => {}
957 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
958 self.link_arg("/DLL");
959 let mut arg: OsString = "/IMPLIB:".into();
960 arg.push(out_filename.with_extension("dll.lib"));
961 self.link_arg(arg);
962 }
963 LinkOutputKind::WasiReactorExe => {
964 {
::core::panicking::panic_fmt(format_args!("can\'t link as reactor on non-wasi target"));
};panic!("can't link as reactor on non-wasi target");
965 }
966 }
967 }
968
969 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
970 if let Some(path) = try_find_native_dynamic_library(self.sess, name, verbatim) {
973 self.link_arg(path);
974 } else {
975 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", name,
if verbatim { "" } else { ".lib" }))
})format!("{}{}", name, if verbatim { "" } else { ".lib" }));
976 }
977 }
978
979 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
980 let implib_path = path.with_extension("dll.lib");
983 if implib_path.exists() {
984 self.link_or_cc_arg(implib_path);
985 }
986 }
987
988 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
989 if let Some(path) = try_find_native_static_library(self.sess, name, verbatim) {
992 self.link_staticlib_by_path(&path, whole_archive);
993 } else {
994 let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
995 let (prefix, suffix) = self.sess.staticlib_components(verbatim);
996 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}{2}{3}", opts, prefix, name,
suffix))
})format!("{opts}{prefix}{name}{suffix}"));
997 }
998 }
999
1000 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1001 if !whole_archive {
1002 self.link_arg(path);
1003 } else {
1004 let mut arg = OsString::from("/WHOLEARCHIVE:");
1005 arg.push(path);
1006 self.link_arg(arg);
1007 }
1008 }
1009
1010 fn gc_sections(&mut self, _keep_metadata: bool) {
1011 if self.sess.opts.optimize != config::OptLevel::No {
1015 self.link_arg("/OPT:REF,ICF");
1016 } else {
1017 self.link_arg("/OPT:REF,NOICF");
1020 }
1021 }
1022
1023 fn full_relro(&mut self) {
1024 }
1026
1027 fn partial_relro(&mut self) {
1028 }
1030
1031 fn no_relro(&mut self) {
1032 }
1034
1035 fn no_crt_objects(&mut self) {
1036 }
1038
1039 fn no_default_libraries(&mut self) {
1040 self.link_arg("/NODEFAULTLIB");
1041 }
1042
1043 fn include_path(&mut self, path: &Path) {
1044 let mut arg = OsString::from("/LIBPATH:");
1045 arg.push(path);
1046 self.link_arg(&arg);
1047 }
1048
1049 fn output_filename(&mut self, path: &Path) {
1050 let mut arg = OsString::from("/OUT:");
1051 arg.push(path);
1052 self.link_arg(&arg);
1053 }
1054
1055 fn optimize(&mut self) {
1056 }
1058
1059 fn pgo_gen(&mut self) {
1060 }
1062
1063 fn control_flow_guard(&mut self) {
1064 self.link_arg("/guard:cf");
1065 }
1066
1067 fn ehcont_guard(&mut self) {
1068 if self.sess.target.pointer_width == 64 {
1069 self.link_arg("/guard:ehcont");
1070 }
1071 }
1072
1073 fn debuginfo(&mut self, _strip: Strip, natvis_debugger_visualizers: &[PathBuf]) {
1074 self.link_arg("/DEBUG");
1077
1078 self.link_arg("/PDBALTPATH:%_PDB%");
1086
1087 let natvis_dir_path = self.sess.opts.sysroot.path().join("lib\\rustlib\\etc");
1089 if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
1090 for entry in natvis_dir {
1091 match entry {
1092 Ok(entry) => {
1093 let path = entry.path();
1094 if path.extension() == Some("natvis".as_ref()) {
1095 let mut arg = OsString::from("/NATVIS:");
1096 arg.push(path);
1097 self.link_arg(arg);
1098 }
1099 }
1100 Err(error) => {
1101 self.sess.dcx().emit_warn(errors::NoNatvisDirectory { error });
1102 }
1103 }
1104 }
1105 }
1106
1107 for path in natvis_debugger_visualizers {
1109 let mut arg = OsString::from("/NATVIS:");
1110 arg.push(path);
1111 self.link_arg(arg);
1112 }
1113 }
1114
1115 fn export_symbols(
1128 &mut self,
1129 tmpdir: &Path,
1130 crate_type: CrateType,
1131 symbols: &[(String, SymbolExportKind)],
1132 ) {
1133 if crate_type == CrateType::Executable {
1135 let should_export_executable_symbols =
1136 self.sess.opts.unstable_opts.export_executable_symbols;
1137 if !should_export_executable_symbols {
1138 return;
1139 }
1140 }
1141
1142 let path = tmpdir.join("lib.def");
1143 let res = try {
1144 let mut f = File::create_buffered(&path)?;
1145
1146 f.write_fmt(format_args!("LIBRARY\n"))writeln!(f, "LIBRARY")?;
1149 f.write_fmt(format_args!("EXPORTS\n"))writeln!(f, "EXPORTS")?;
1150 for (symbol, kind) in symbols {
1151 let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
1152 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1152",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1152u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
symbol) as &dyn Value))])
});
} else { ; }
};debug!(" _{symbol}");
1153 f.write_fmt(format_args!(" {0}{1}\n", symbol, kind_marker))writeln!(f, " {symbol}{kind_marker}")?;
1154 }
1155 };
1156 if let Err(error) = res {
1157 self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
1158 }
1159 let mut arg = OsString::from("/DEF:");
1160 arg.push(path);
1161 self.link_arg(&arg);
1162 }
1163
1164 fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1165 let subsystem = subsystem.as_str();
1166 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/SUBSYSTEM:{0}", subsystem))
})format!("/SUBSYSTEM:{subsystem}"));
1167
1168 if subsystem == "windows" {
1183 self.link_arg("/ENTRY:mainCRTStartup");
1184 }
1185 }
1186
1187 fn linker_plugin_lto(&mut self) {
1188 }
1190
1191 fn add_no_exec(&mut self) {
1192 self.link_arg("/NXCOMPAT");
1193 }
1194}
1195
1196struct EmLinker<'a> {
1197 cmd: Command,
1198 sess: &'a Session,
1199}
1200
1201impl<'a> Linker for EmLinker<'a> {
1202 fn cmd(&mut self) -> &mut Command {
1203 &mut self.cmd
1204 }
1205
1206 fn is_cc(&self) -> bool {
1207 true
1208 }
1209
1210 fn set_output_kind(
1211 &mut self,
1212 output_kind: LinkOutputKind,
1213 _crate_type: CrateType,
1214 _out_filename: &Path,
1215 ) {
1216 match output_kind {
1217 LinkOutputKind::DynamicNoPicExe | LinkOutputKind::DynamicPicExe => {
1218 self.cmd.arg("-sMAIN_MODULE=2");
1219 }
1220 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1221 self.cmd.arg("-sSIDE_MODULE=2");
1222 }
1223 LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {}
1225 LinkOutputKind::WasiReactorExe => {
1226 ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
1227 }
1228 }
1229 }
1230
1231 fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1232 self.link_or_cc_args(&["-l", name]);
1234 }
1235
1236 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1237 self.link_or_cc_arg(path);
1238 }
1239
1240 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
1241 self.link_or_cc_args(&["-l", name]);
1242 }
1243
1244 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1245 self.link_or_cc_arg(path);
1246 }
1247
1248 fn full_relro(&mut self) {
1249 }
1251
1252 fn partial_relro(&mut self) {
1253 }
1255
1256 fn no_relro(&mut self) {
1257 }
1259
1260 fn gc_sections(&mut self, _keep_metadata: bool) {
1261 }
1263
1264 fn optimize(&mut self) {
1265 self.cc_arg(match self.sess.opts.optimize {
1267 OptLevel::No => "-O0",
1268 OptLevel::Less => "-O1",
1269 OptLevel::More => "-O2",
1270 OptLevel::Aggressive => "-O3",
1271 OptLevel::Size => "-Os",
1272 OptLevel::SizeMin => "-Oz",
1273 });
1274 }
1275
1276 fn pgo_gen(&mut self) {
1277 }
1279
1280 fn control_flow_guard(&mut self) {}
1281
1282 fn ehcont_guard(&mut self) {}
1283
1284 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1285 self.cc_arg(match self.sess.opts.debuginfo {
1288 DebugInfo::None => "-g0",
1289 DebugInfo::Limited | DebugInfo::LineTablesOnly | DebugInfo::LineDirectivesOnly => {
1290 "--profiling-funcs"
1291 }
1292 DebugInfo::Full => "-g",
1293 });
1294 }
1295
1296 fn no_crt_objects(&mut self) {}
1297
1298 fn no_default_libraries(&mut self) {
1299 self.cc_arg("-nodefaultlibs");
1300 }
1301
1302 fn export_symbols(
1303 &mut self,
1304 _tmpdir: &Path,
1305 _crate_type: CrateType,
1306 symbols: &[(String, SymbolExportKind)],
1307 ) {
1308 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1308",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1308u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("EXPORTED SYMBOLS:")
as &dyn Value))])
});
} else { ; }
};debug!("EXPORTED SYMBOLS:");
1309
1310 self.cc_arg("-s");
1311
1312 let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
1313 let encoded = serde_json::to_string(
1314 &symbols.iter().map(|(sym, _)| "_".to_owned() + sym).collect::<Vec<_>>(),
1315 )
1316 .unwrap();
1317 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1317",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1317u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("{0}",
encoded) as &dyn Value))])
});
} else { ; }
};debug!("{encoded}");
1318
1319 arg.push(encoded);
1320
1321 self.cc_arg(arg);
1322 }
1323
1324 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {
1325 }
1327
1328 fn linker_plugin_lto(&mut self) {
1329 }
1331}
1332
1333struct WasmLd<'a> {
1334 cmd: Command,
1335 sess: &'a Session,
1336}
1337
1338impl<'a> WasmLd<'a> {
1339 fn new(cmd: Command, sess: &'a Session) -> WasmLd<'a> {
1340 WasmLd { cmd, sess }
1341 }
1342}
1343
1344impl<'a> Linker for WasmLd<'a> {
1345 fn cmd(&mut self) -> &mut Command {
1346 &mut self.cmd
1347 }
1348
1349 fn set_output_kind(
1350 &mut self,
1351 output_kind: LinkOutputKind,
1352 _crate_type: CrateType,
1353 _out_filename: &Path,
1354 ) {
1355 match output_kind {
1356 LinkOutputKind::DynamicNoPicExe
1357 | LinkOutputKind::DynamicPicExe
1358 | LinkOutputKind::StaticNoPicExe
1359 | LinkOutputKind::StaticPicExe => {}
1360 LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1361 self.link_arg("--no-entry");
1362 }
1363 LinkOutputKind::WasiReactorExe => {
1364 self.link_args(&["--entry", "_initialize"]);
1365 }
1366 }
1367 }
1368
1369 fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1370 self.link_or_cc_args(&["-l", name]);
1371 }
1372
1373 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1374 self.link_or_cc_arg(path);
1375 }
1376
1377 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1378 if !whole_archive {
1379 self.link_or_cc_args(&["-l", name]);
1380 } else {
1381 self.link_arg("--whole-archive")
1382 .link_or_cc_args(&["-l", name])
1383 .link_arg("--no-whole-archive");
1384 }
1385 }
1386
1387 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1388 if !whole_archive {
1389 self.link_or_cc_arg(path);
1390 } else {
1391 self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1392 }
1393 }
1394
1395 fn full_relro(&mut self) {}
1396
1397 fn partial_relro(&mut self) {}
1398
1399 fn no_relro(&mut self) {}
1400
1401 fn gc_sections(&mut self, _keep_metadata: bool) {
1402 self.link_arg("--gc-sections");
1403 }
1404
1405 fn optimize(&mut self) {
1406 self.link_arg(match self.sess.opts.optimize {
1409 OptLevel::No => "-O0",
1410 OptLevel::Less => "-O1",
1411 OptLevel::More => "-O2",
1412 OptLevel::Aggressive => "-O3",
1413 OptLevel::Size => "-O2",
1416 OptLevel::SizeMin => "-O2",
1417 });
1418 }
1419
1420 fn pgo_gen(&mut self) {}
1421
1422 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1423 match strip {
1424 Strip::None => {}
1425 Strip::Debuginfo => {
1426 self.link_arg("--strip-debug");
1427 }
1428 Strip::Symbols => {
1429 self.link_arg("--strip-all");
1430 }
1431 }
1432 }
1433
1434 fn control_flow_guard(&mut self) {}
1435
1436 fn ehcont_guard(&mut self) {}
1437
1438 fn no_crt_objects(&mut self) {}
1439
1440 fn no_default_libraries(&mut self) {}
1441
1442 fn export_symbols(
1443 &mut self,
1444 _tmpdir: &Path,
1445 _crate_type: CrateType,
1446 symbols: &[(String, SymbolExportKind)],
1447 ) {
1448 for (sym, _) in symbols {
1449 self.link_args(&["--export", sym]);
1450 }
1451
1452 if #[allow(non_exhaustive_omitted_patterns)] match self.sess.target.os {
Os::Unknown | Os::None => true,
_ => false,
}matches!(self.sess.target.os, Os::Unknown | Os::None) {
1457 self.link_args(&["--export=__heap_base", "--export=__data_end"]);
1458 }
1459 }
1460
1461 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1462
1463 fn linker_plugin_lto(&mut self) {
1464 match self.sess.opts.cg.linker_plugin_lto {
1465 LinkerPluginLto::Disabled => {
1466 }
1468 LinkerPluginLto::LinkerPluginAuto => {
1469 self.push_linker_plugin_lto_args();
1470 }
1471 LinkerPluginLto::LinkerPlugin(_) => {
1472 self.push_linker_plugin_lto_args();
1473 }
1474 }
1475 }
1476}
1477
1478impl<'a> WasmLd<'a> {
1479 fn push_linker_plugin_lto_args(&mut self) {
1480 let opt_level = match self.sess.opts.optimize {
1481 config::OptLevel::No => "O0",
1482 config::OptLevel::Less => "O1",
1483 config::OptLevel::More => "O2",
1484 config::OptLevel::Aggressive => "O3",
1485 config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
1487 };
1488 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("--lto-{0}", opt_level))
})format!("--lto-{opt_level}"));
1489 }
1490}
1491
1492struct L4Bender<'a> {
1494 cmd: Command,
1495 sess: &'a Session,
1496 hinted_static: bool,
1497}
1498
1499impl<'a> Linker for L4Bender<'a> {
1500 fn cmd(&mut self) -> &mut Command {
1501 &mut self.cmd
1502 }
1503
1504 fn set_output_kind(
1505 &mut self,
1506 _output_kind: LinkOutputKind,
1507 _crate_type: CrateType,
1508 _out_filename: &Path,
1509 ) {
1510 }
1511
1512 fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
1513 self.hint_static();
1514 if !whole_archive {
1515 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-PC{0}", name))
})format!("-PC{name}"));
1516 } else {
1517 self.link_arg("--whole-archive")
1518 .link_or_cc_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}", name))
})format!("-l{name}"))
1519 .link_arg("--no-whole-archive");
1520 }
1521 }
1522
1523 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1524 self.hint_static();
1525 if !whole_archive {
1526 self.link_or_cc_arg(path);
1527 } else {
1528 self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
1529 }
1530 }
1531
1532 fn full_relro(&mut self) {
1533 self.link_args(&["-z", "relro", "-z", "now"]);
1534 }
1535
1536 fn partial_relro(&mut self) {
1537 self.link_args(&["-z", "relro"]);
1538 }
1539
1540 fn no_relro(&mut self) {
1541 self.link_args(&["-z", "norelro"]);
1542 }
1543
1544 fn gc_sections(&mut self, keep_metadata: bool) {
1545 if !keep_metadata {
1546 self.link_arg("--gc-sections");
1547 }
1548 }
1549
1550 fn optimize(&mut self) {
1551 if self.sess.opts.optimize == config::OptLevel::More
1554 || self.sess.opts.optimize == config::OptLevel::Aggressive
1555 {
1556 self.link_arg("-O1");
1557 }
1558 }
1559
1560 fn pgo_gen(&mut self) {}
1561
1562 fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
1563 match strip {
1564 Strip::None => {}
1565 Strip::Debuginfo => {
1566 self.link_arg("--strip-debug");
1567 }
1568 Strip::Symbols => {
1569 self.link_arg("--strip-all");
1570 }
1571 }
1572 }
1573
1574 fn no_default_libraries(&mut self) {
1575 self.cc_arg("-nostdlib");
1576 }
1577
1578 fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[(String, SymbolExportKind)]) {
1579 self.sess.dcx().emit_warn(errors::L4BenderExportingSymbolsUnimplemented);
1581 }
1582
1583 fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1584 let subsystem = subsystem.as_str();
1585 self.link_arg(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("--subsystem {0}", subsystem))
})format!("--subsystem {subsystem}"));
1586 }
1587
1588 fn reset_per_library_state(&mut self) {
1589 self.hint_static(); }
1591
1592 fn linker_plugin_lto(&mut self) {}
1593
1594 fn control_flow_guard(&mut self) {}
1595
1596 fn ehcont_guard(&mut self) {}
1597
1598 fn no_crt_objects(&mut self) {}
1599}
1600
1601impl<'a> L4Bender<'a> {
1602 fn new(cmd: Command, sess: &'a Session) -> L4Bender<'a> {
1603 L4Bender { cmd, sess, hinted_static: false }
1604 }
1605
1606 fn hint_static(&mut self) {
1607 if !self.hinted_static {
1608 self.link_or_cc_arg("-static");
1609 self.hinted_static = true;
1610 }
1611 }
1612}
1613
1614struct AixLinker<'a> {
1616 cmd: Command,
1617 sess: &'a Session,
1618 hinted_static: Option<bool>,
1619}
1620
1621impl<'a> AixLinker<'a> {
1622 fn new(cmd: Command, sess: &'a Session) -> AixLinker<'a> {
1623 AixLinker { cmd, sess, hinted_static: None }
1624 }
1625
1626 fn hint_static(&mut self) {
1627 if self.hinted_static != Some(true) {
1628 self.link_arg("-bstatic");
1629 self.hinted_static = Some(true);
1630 }
1631 }
1632
1633 fn hint_dynamic(&mut self) {
1634 if self.hinted_static != Some(false) {
1635 self.link_arg("-bdynamic");
1636 self.hinted_static = Some(false);
1637 }
1638 }
1639
1640 fn build_dylib(&mut self, _out_filename: &Path) {
1641 self.link_args(&["-bM:SRE", "-bnoentry"]);
1642 self.link_arg("-bexpfull");
1645 }
1646}
1647
1648impl<'a> Linker for AixLinker<'a> {
1649 fn cmd(&mut self) -> &mut Command {
1650 &mut self.cmd
1651 }
1652
1653 fn set_output_kind(
1654 &mut self,
1655 output_kind: LinkOutputKind,
1656 _crate_type: CrateType,
1657 out_filename: &Path,
1658 ) {
1659 match output_kind {
1660 LinkOutputKind::DynamicDylib => {
1661 self.hint_dynamic();
1662 self.build_dylib(out_filename);
1663 }
1664 LinkOutputKind::StaticDylib => {
1665 self.hint_static();
1666 self.build_dylib(out_filename);
1667 }
1668 _ => {}
1669 }
1670 }
1671
1672 fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
1673 self.hint_dynamic();
1674 self.link_or_cc_arg(if verbatim { String::from(name) } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}", name))
})format!("-l{name}") });
1675 }
1676
1677 fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
1678 self.hint_dynamic();
1679 self.link_or_cc_arg(path);
1680 }
1681
1682 fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
1683 self.hint_static();
1684 if !whole_archive {
1685 self.link_or_cc_arg(if verbatim { String::from(name) } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-l{0}", name))
})format!("-l{name}") });
1686 } else {
1687 let mut arg = OsString::from("-bkeepfile:");
1688 arg.push(find_native_static_library(name, verbatim, self.sess));
1689 self.link_or_cc_arg(arg);
1690 }
1691 }
1692
1693 fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1694 self.hint_static();
1695 if !whole_archive {
1696 self.link_or_cc_arg(path);
1697 } else {
1698 let mut arg = OsString::from("-bkeepfile:");
1699 arg.push(path);
1700 self.link_arg(arg);
1701 }
1702 }
1703
1704 fn full_relro(&mut self) {}
1705
1706 fn partial_relro(&mut self) {}
1707
1708 fn no_relro(&mut self) {}
1709
1710 fn gc_sections(&mut self, _keep_metadata: bool) {
1711 self.link_arg("-bgc");
1712 }
1713
1714 fn optimize(&mut self) {}
1715
1716 fn pgo_gen(&mut self) {
1717 self.link_arg("-bdbg:namedsects:ss");
1718 self.link_arg("-u");
1719 self.link_arg("__llvm_profile_runtime");
1720 }
1721
1722 fn control_flow_guard(&mut self) {}
1723
1724 fn ehcont_guard(&mut self) {}
1725
1726 fn debuginfo(&mut self, _: Strip, _: &[PathBuf]) {}
1727
1728 fn no_crt_objects(&mut self) {}
1729
1730 fn no_default_libraries(&mut self) {}
1731
1732 fn export_symbols(
1733 &mut self,
1734 tmpdir: &Path,
1735 _crate_type: CrateType,
1736 symbols: &[(String, SymbolExportKind)],
1737 ) {
1738 let path = tmpdir.join("list.exp");
1739 let res = try {
1740 let mut f = File::create_buffered(&path)?;
1741 for (symbol, _) in symbols {
1743 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/linker.rs:1743",
"rustc_codegen_ssa::back::linker", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/linker.rs"),
::tracing_core::__macro_support::Option::Some(1743u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::linker"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" _{0}",
symbol) as &dyn Value))])
});
} else { ; }
};debug!(" _{symbol}");
1744 f.write_fmt(format_args!(" {0}\n", symbol))writeln!(f, " {symbol}")?;
1745 }
1746 };
1747 if let Err(e) = res {
1748 self.sess.dcx().fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to write export file: {0}",
e))
})format!("failed to write export file: {e}"));
1749 }
1750 self.link_arg(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("-bE:{0}", path.to_str().unwrap()))
})format!("-bE:{}", path.to_str().unwrap()));
1751 }
1752
1753 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1754
1755 fn reset_per_library_state(&mut self) {
1756 self.hint_dynamic();
1757 }
1758
1759 fn linker_plugin_lto(&mut self) {}
1760
1761 fn add_eh_frame_header(&mut self) {}
1762
1763 fn add_no_exec(&mut self) {}
1764
1765 fn add_as_needed(&mut self) {}
1766}
1767
1768fn for_each_exported_symbols_include_dep<'tcx>(
1769 tcx: TyCtxt<'tcx>,
1770 crate_type: CrateType,
1771 mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum),
1772) {
1773 let formats = tcx.dependency_formats(());
1774 let deps = &formats[&crate_type];
1775
1776 for (cnum, dep_format) in deps.iter_enumerated() {
1777 if *dep_format == Linkage::Static {
1779 for &(symbol, info) in tcx.exported_non_generic_symbols(cnum).iter() {
1780 callback(symbol, info, cnum);
1781 }
1782 for &(symbol, info) in tcx.exported_generic_symbols(cnum).iter() {
1783 callback(symbol, info, cnum);
1784 }
1785 }
1786 }
1787}
1788
1789pub(crate) fn exported_symbols(
1790 tcx: TyCtxt<'_>,
1791 crate_type: CrateType,
1792) -> Vec<(String, SymbolExportKind)> {
1793 if let Some(ref exports) = tcx.sess.target.override_export_symbols {
1794 return exports
1795 .iter()
1796 .map(|name| {
1797 (
1798 name.to_string(),
1799 SymbolExportKind::Text,
1803 )
1804 })
1805 .collect();
1806 }
1807
1808 let mut symbols = if let CrateType::ProcMacro = crate_type {
1809 exported_symbols_for_proc_macro_crate(tcx)
1810 } else {
1811 exported_symbols_for_non_proc_macro(tcx, crate_type)
1812 };
1813
1814 if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
1815 let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx);
1816 symbols.push((metadata_symbol_name, SymbolExportKind::Data));
1817 }
1818
1819 symbols
1820}
1821
1822fn exported_symbols_for_non_proc_macro(
1823 tcx: TyCtxt<'_>,
1824 crate_type: CrateType,
1825) -> Vec<(String, SymbolExportKind)> {
1826 let mut symbols = Vec::new();
1827 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1828 for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1829 if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum) {
1833 symbols.push((
1834 symbol_export::exporting_symbol_name_for_instance_in_crate(tcx, symbol, cnum),
1835 info.kind,
1836 ));
1837 symbol_export::extend_exported_symbols(&mut symbols, tcx, symbol, cnum);
1838 }
1839 });
1840
1841 if export_threshold == SymbolExportLevel::Rust
1843 && needs_allocator_shim_for_linking(tcx.dependency_formats(()), crate_type)
1844 && let Some(kind) = tcx.allocator_kind(())
1845 {
1846 symbols.extend(allocator_shim_symbols(tcx, kind));
1847 }
1848
1849 symbols
1850}
1851
1852fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<(String, SymbolExportKind)> {
1853 if !tcx.sess.opts.output_types.should_codegen() {
1855 return Vec::new();
1856 }
1857
1858 let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
1859 let proc_macro_decls_name = tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
1860
1861 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(proc_macro_decls_name, SymbolExportKind::Data)]))vec![(proc_macro_decls_name, SymbolExportKind::Data)]
1862}
1863
1864pub(crate) fn linked_symbols(
1865 tcx: TyCtxt<'_>,
1866 crate_type: CrateType,
1867) -> Vec<(String, SymbolExportKind)> {
1868 match crate_type {
1869 CrateType::Executable
1870 | CrateType::ProcMacro
1871 | CrateType::Cdylib
1872 | CrateType::Dylib
1873 | CrateType::Sdylib => (),
1874 CrateType::StaticLib | CrateType::Rlib => {
1875 return Vec::new();
1877 }
1878 }
1879
1880 match tcx.sess.lto() {
1881 Lto::No | Lto::ThinLocal => {}
1882 Lto::Thin | Lto::Fat => {
1883 return Vec::new();
1892 }
1893 }
1894
1895 let mut symbols = Vec::new();
1896
1897 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1898 for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1899 if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum)
1900 || info.used
1901 || info.rustc_std_internal_symbol
1902 {
1903 symbols.push((
1904 symbol_export::linking_symbol_name_for_instance_in_crate(
1905 tcx, symbol, info.kind, cnum,
1906 ),
1907 info.kind,
1908 ));
1909 }
1910 });
1911
1912 symbols
1913}
1914
1915struct PtxLinker<'a> {
1918 cmd: Command,
1919 sess: &'a Session,
1920}
1921
1922impl<'a> Linker for PtxLinker<'a> {
1923 fn cmd(&mut self) -> &mut Command {
1924 &mut self.cmd
1925 }
1926
1927 fn set_output_kind(
1928 &mut self,
1929 _output_kind: LinkOutputKind,
1930 _crate_type: CrateType,
1931 _out_filename: &Path,
1932 ) {
1933 }
1934
1935 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
1936 { ::core::panicking::panic_fmt(format_args!("staticlibs not supported")); }panic!("staticlibs not supported")
1937 }
1938
1939 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1940 self.link_arg("--rlib").link_arg(path);
1941 }
1942
1943 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
1944 self.link_arg("--debug");
1945 }
1946
1947 fn add_object(&mut self, path: &Path) {
1948 self.link_arg("--bitcode").link_arg(path);
1949 }
1950
1951 fn optimize(&mut self) {
1952 match self.sess.lto() {
1953 Lto::Thin | Lto::Fat | Lto::ThinLocal => {
1954 self.link_arg("-Olto");
1955 }
1956
1957 Lto::No => {}
1958 }
1959 }
1960
1961 fn full_relro(&mut self) {}
1962
1963 fn partial_relro(&mut self) {}
1964
1965 fn no_relro(&mut self) {}
1966
1967 fn gc_sections(&mut self, _keep_metadata: bool) {}
1968
1969 fn pgo_gen(&mut self) {}
1970
1971 fn no_crt_objects(&mut self) {}
1972
1973 fn no_default_libraries(&mut self) {}
1974
1975 fn control_flow_guard(&mut self) {}
1976
1977 fn ehcont_guard(&mut self) {}
1978
1979 fn export_symbols(
1980 &mut self,
1981 _tmpdir: &Path,
1982 _crate_type: CrateType,
1983 _symbols: &[(String, SymbolExportKind)],
1984 ) {
1985 }
1986
1987 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
1988
1989 fn linker_plugin_lto(&mut self) {}
1990}
1991
1992struct LlbcLinker<'a> {
1994 cmd: Command,
1995 sess: &'a Session,
1996}
1997
1998impl<'a> Linker for LlbcLinker<'a> {
1999 fn cmd(&mut self) -> &mut Command {
2000 &mut self.cmd
2001 }
2002
2003 fn set_output_kind(
2004 &mut self,
2005 _output_kind: LinkOutputKind,
2006 _crate_type: CrateType,
2007 _out_filename: &Path,
2008 ) {
2009 }
2010
2011 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
2012 { ::core::panicking::panic_fmt(format_args!("staticlibs not supported")); }panic!("staticlibs not supported")
2013 }
2014
2015 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2016 self.link_or_cc_arg(path);
2017 }
2018
2019 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2020 self.link_arg("--debug");
2021 }
2022
2023 fn optimize(&mut self) {
2024 self.link_arg(match self.sess.opts.optimize {
2025 OptLevel::No => "-O0",
2026 OptLevel::Less => "-O1",
2027 OptLevel::More => "-O2",
2028 OptLevel::Aggressive => "-O3",
2029 OptLevel::Size => "-Os",
2030 OptLevel::SizeMin => "-Oz",
2031 });
2032 }
2033
2034 fn full_relro(&mut self) {}
2035
2036 fn partial_relro(&mut self) {}
2037
2038 fn no_relro(&mut self) {}
2039
2040 fn gc_sections(&mut self, _keep_metadata: bool) {}
2041
2042 fn pgo_gen(&mut self) {}
2043
2044 fn no_crt_objects(&mut self) {}
2045
2046 fn no_default_libraries(&mut self) {}
2047
2048 fn control_flow_guard(&mut self) {}
2049
2050 fn ehcont_guard(&mut self) {}
2051
2052 fn export_symbols(
2053 &mut self,
2054 _tmpdir: &Path,
2055 _crate_type: CrateType,
2056 symbols: &[(String, SymbolExportKind)],
2057 ) {
2058 match _crate_type {
2059 CrateType::Cdylib => {
2060 for (sym, _) in symbols {
2061 self.link_args(&["--export-symbol", sym]);
2062 }
2063 }
2064 _ => (),
2065 }
2066 }
2067
2068 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
2069
2070 fn linker_plugin_lto(&mut self) {}
2071}
2072
2073struct BpfLinker<'a> {
2074 cmd: Command,
2075 sess: &'a Session,
2076}
2077
2078impl<'a> Linker for BpfLinker<'a> {
2079 fn cmd(&mut self) -> &mut Command {
2080 &mut self.cmd
2081 }
2082
2083 fn set_output_kind(
2084 &mut self,
2085 _output_kind: LinkOutputKind,
2086 _crate_type: CrateType,
2087 _out_filename: &Path,
2088 ) {
2089 }
2090
2091 fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
2092 self.sess.dcx().emit_fatal(errors::BpfStaticlibNotSupported)
2093 }
2094
2095 fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
2096 self.link_or_cc_arg(path);
2097 }
2098
2099 fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
2100 self.link_arg("--debug");
2101 }
2102
2103 fn optimize(&mut self) {
2104 self.link_arg(match self.sess.opts.optimize {
2105 OptLevel::No => "-O0",
2106 OptLevel::Less => "-O1",
2107 OptLevel::More => "-O2",
2108 OptLevel::Aggressive => "-O3",
2109 OptLevel::Size => "-Os",
2110 OptLevel::SizeMin => "-Oz",
2111 });
2112 }
2113
2114 fn full_relro(&mut self) {}
2115
2116 fn partial_relro(&mut self) {}
2117
2118 fn no_relro(&mut self) {}
2119
2120 fn gc_sections(&mut self, _keep_metadata: bool) {}
2121
2122 fn pgo_gen(&mut self) {}
2123
2124 fn no_crt_objects(&mut self) {}
2125
2126 fn no_default_libraries(&mut self) {}
2127
2128 fn control_flow_guard(&mut self) {}
2129
2130 fn ehcont_guard(&mut self) {}
2131
2132 fn export_symbols(
2133 &mut self,
2134 tmpdir: &Path,
2135 _crate_type: CrateType,
2136 symbols: &[(String, SymbolExportKind)],
2137 ) {
2138 let path = tmpdir.join("symbols");
2139 let res = try {
2140 let mut f = File::create_buffered(&path)?;
2141 for (sym, _) in symbols {
2142 f.write_fmt(format_args!("{0}\n", sym))writeln!(f, "{sym}")?;
2143 }
2144 };
2145 if let Err(error) = res {
2146 self.sess.dcx().emit_fatal(errors::SymbolFileWriteFailure { error });
2147 } else {
2148 self.link_arg("--export-symbols").link_arg(&path);
2149 }
2150 }
2151
2152 fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
2153
2154 fn linker_plugin_lto(&mut self) {}
2155}