]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_ssa/src/back/link.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_codegen_ssa / src / back / link.rs
CommitLineData
a2a8927a 1use rustc_arena::TypedArena;
5e7ed085 2use rustc_ast::CRATE_NODE_ID;
f2b60f7d
FG
3use rustc_data_structures::fx::FxHashSet;
4use rustc_data_structures::fx::FxIndexMap;
a2a8927a 5use rustc_data_structures::memmap::Mmap;
3dfed10e 6use rustc_data_structures::temp_dir::MaybeTempDir;
5e7ed085 7use rustc_errors::{ErrorGuaranteed, Handler};
ba9703b0 8use rustc_fs_util::fix_windows_verbatim_for_gcc;
923072b8 9use rustc_hir::def_id::CrateNum;
f2b60f7d 10use rustc_metadata::find_native_static_library;
064997fb 11use rustc_metadata::fs::{emit_metadata, METADATA_FILENAME};
ba9703b0 12use rustc_middle::middle::dependency_format::Linkage;
04454e1e 13use rustc_middle::middle::exported_symbols::SymbolExportKind;
17df50a5 14use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip};
a2a8927a 15use rustc_session::config::{OutputFilenames, OutputType, PrintRequest, SplitDwarfKind};
c295e0f8 16use rustc_session::cstore::DllImport;
ba9703b0
XL
17use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
18use rustc_session::search_paths::PathKind;
f9f354fc 19use rustc_session::utils::NativeLibKind;
dfeec247
XL
20/// For all the linkers we support, and information they might
21/// need out of the shared crate context before we get rid of it.
ba9703b0 22use rustc_session::{filesearch, Session};
dfeec247 23use rustc_span::symbol::Symbol;
923072b8 24use rustc_span::DebuggerVisualizerFile;
f2b60f7d 25use rustc_target::spec::crt_objects::{CrtObjects, LinkSelfContainedDefault};
5869c6ff 26use rustc_target::spec::{LinkOutputKind, LinkerFlavor, LldFlavor, SplitDebuginfo};
cdc7bbd5 27use rustc_target::spec::{PanicStrategy, RelocModel, RelroLevel, SanitizerSet, Target};
a1dfa0c6 28
f2b60f7d 29use super::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
a1dfa0c6 30use super::command::Command;
f9f354fc 31use super::linker::{self, Linker};
5e7ed085 32use super::metadata::{create_rmeta_file, MetadataPosition};
48663c56 33use super::rpath::{self, RPathConfig};
064997fb 34use crate::{looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib};
a1dfa0c6
XL
35
36use cc::windows_registry;
94222f64 37use regex::Regex;
3dfed10e 38use tempfile::Builder as TempFileBuilder;
48663c56 39
a2a8927a 40use std::borrow::Borrow;
923072b8
FG
41use std::cell::OnceCell;
42use std::collections::BTreeSet;
dfeec247 43use std::ffi::OsString;
a2a8927a
XL
44use std::fs::{File, OpenOptions};
45use std::io::{BufWriter, Write};
5e7ed085 46use std::ops::Deref;
a1dfa0c6 47use std::path::{Path, PathBuf};
dfeec247 48use std::process::{ExitStatus, Output, Stdio};
f2b60f7d 49use std::{env, fmt, fs, io, mem, str};
a1dfa0c6 50
6a06907d 51pub fn ensure_removed(diag_handler: &Handler, path: &Path) {
a1dfa0c6 52 if let Err(e) = fs::remove_file(path) {
6a06907d
XL
53 if e.kind() != io::ErrorKind::NotFound {
54 diag_handler.err(&format!("failed to remove {}: {}", path.display(), e));
55 }
a1dfa0c6
XL
56 }
57}
58
48663c56
XL
59/// Performs the linkage portion of the compilation phase. This will generate all
60/// of the requested outputs for this compilation session.
064997fb 61pub fn link_binary<'a>(
dfeec247 62 sess: &'a Session,
064997fb 63 archive_builder_builder: &dyn ArchiveBuilderBuilder,
dfeec247
XL
64 codegen_results: &CodegenResults,
65 outputs: &OutputFilenames,
5e7ed085 66) -> Result<(), ErrorGuaranteed> {
dfeec247 67 let _timer = sess.timer("link_binary");
48663c56 68 let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
f9f354fc 69 for &crate_type in sess.crate_types().iter() {
48663c56 70 // Ignore executable crates if we have -Z no-codegen, as they will error.
064997fb 71 if (sess.opts.unstable_opts.no_codegen || !sess.opts.output_types.should_codegen())
dfeec247 72 && !output_metadata
f9f354fc 73 && crate_type == CrateType::Executable
dfeec247 74 {
48663c56
XL
75 continue;
76 }
77
78 if invalid_output_for_target(sess, crate_type) {
dfeec247
XL
79 bug!(
80 "invalid output type `{:?}` for target os `{}`",
81 crate_type,
82 sess.opts.target_triple
83 );
48663c56
XL
84 }
85
dfeec247
XL
86 sess.time("link_binary_check_files_are_writeable", || {
87 for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) {
88 check_file_is_writeable(obj, sess);
89 }
90 });
48663c56 91
5869c6ff 92 if outputs.outputs.should_link() {
3dfed10e
XL
93 let tmpdir = TempFileBuilder::new()
94 .prefix("rustc")
95 .tempdir()
96 .unwrap_or_else(|err| sess.fatal(&format!("couldn't create a temp dir: {}", err)));
97 let path = MaybeTempDir::new(tmpdir, sess.opts.cg.save_temps);
136023e0
XL
98 let out_filename = out_filename(
99 sess,
100 crate_type,
101 outputs,
a2a8927a 102 codegen_results.crate_info.local_crate_name.as_str(),
136023e0 103 );
48663c56 104 match crate_type {
f9f354fc 105 CrateType::Rlib => {
dfeec247 106 let _timer = sess.timer("link_rlib");
064997fb
FG
107 info!("preparing rlib to {:?}", out_filename);
108 link_rlib(
136023e0 109 sess,
064997fb 110 archive_builder_builder,
136023e0
XL
111 codegen_results,
112 RlibFlavor::Normal,
136023e0
XL
113 &path,
114 )?
064997fb 115 .build(&out_filename);
48663c56 116 }
f9f354fc 117 CrateType::Staticlib => {
064997fb
FG
118 link_staticlib(
119 sess,
120 archive_builder_builder,
121 codegen_results,
122 &out_filename,
123 &path,
124 )?;
48663c56
XL
125 }
126 _ => {
064997fb 127 link_natively(
48663c56 128 sess,
064997fb 129 archive_builder_builder,
48663c56
XL
130 crate_type,
131 &out_filename,
132 codegen_results,
3dfed10e 133 path.as_ref(),
064997fb 134 )?;
48663c56
XL
135 }
136 }
416331ca 137 if sess.opts.json_artifact_notifications {
dc9dc135 138 sess.parse_sess.span_diagnostic.emit_artifact_notification(&out_filename, "link");
48663c56 139 }
3c0e092e
XL
140
141 if sess.prof.enabled() {
142 if let Some(artifact_name) = out_filename.file_name() {
143 // Record size for self-profiling
144 let file_size = std::fs::metadata(&out_filename).map(|m| m.len()).unwrap_or(0);
145
146 sess.prof.artifact_size(
147 "linked_artifact",
148 artifact_name.to_string_lossy(),
149 file_size,
150 );
151 }
152 }
48663c56 153 }
48663c56
XL
154 }
155
a2a8927a 156 // Remove the temporary object file and metadata if we aren't saving temps.
dfeec247 157 sess.time("link_binary_remove_temps", || {
a2a8927a
XL
158 // If the user requests that temporaries are saved, don't delete any.
159 if sess.opts.cg.save_temps {
160 return;
161 }
fc512014 162
064997fb
FG
163 let maybe_remove_temps_from_module =
164 |preserve_objects: bool, preserve_dwarf_objects: bool, module: &CompiledModule| {
165 if !preserve_objects {
166 if let Some(ref obj) = module.object {
167 ensure_removed(sess.diagnostic(), obj);
168 }
169 }
170
171 if !preserve_dwarf_objects {
172 if let Some(ref dwo_obj) = module.dwarf_object {
173 ensure_removed(sess.diagnostic(), dwo_obj);
174 }
175 }
176 };
177
178 let remove_temps_from_module =
179 |module: &CompiledModule| maybe_remove_temps_from_module(false, false, module);
fc512014 180
a2a8927a
XL
181 // Otherwise, always remove the metadata and allocator module temporaries.
182 if let Some(ref metadata_module) = codegen_results.metadata_module {
183 remove_temps_from_module(metadata_module);
184 }
185
186 if let Some(ref allocator_module) = codegen_results.allocator_module {
187 remove_temps_from_module(allocator_module);
188 }
189
190 // If no requested outputs require linking, then the object temporaries should
191 // be kept.
192 if !sess.opts.output_types.should_link() {
193 return;
194 }
195
196 // Potentially keep objects for their debuginfo.
197 let (preserve_objects, preserve_dwarf_objects) = preserve_objects_for_their_debuginfo(sess);
198 debug!(?preserve_objects, ?preserve_dwarf_objects);
199
200 for module in &codegen_results.modules {
064997fb 201 maybe_remove_temps_from_module(preserve_objects, preserve_dwarf_objects, module);
48663c56 202 }
dfeec247 203 });
a1dfa0c6 204
136023e0 205 Ok(())
a1dfa0c6
XL
206}
207
e74abb32
XL
208pub fn each_linked_rlib(
209 info: &CrateInfo,
210 f: &mut dyn FnMut(CrateNum, &Path),
211) -> Result<(), String> {
136023e0 212 let crates = info.used_crates.iter();
e74abb32
XL
213 let mut fmts = None;
214 for (ty, list) in info.dependency_formats.iter() {
215 match ty {
f9f354fc
XL
216 CrateType::Executable
217 | CrateType::Staticlib
218 | CrateType::Cdylib
219 | CrateType::ProcMacro => {
e74abb32
XL
220 fmts = Some(list);
221 break;
222 }
223 _ => {}
224 }
225 }
3c0e092e
XL
226 let Some(fmts) = fmts else {
227 return Err("could not find formats for rlibs".to_string());
a1dfa0c6 228 };
136023e0 229 for &cnum in crates {
a1dfa0c6 230 match fmts.get(cnum.as_usize() - 1) {
ba9703b0 231 Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue,
a1dfa0c6 232 Some(_) => {}
dfeec247 233 None => return Err("could not find formats for rlibs".to_string()),
a1dfa0c6 234 }
04454e1e 235 let name = info.crate_name[&cnum];
136023e0 236 let used_crate_source = &info.used_crate_source[&cnum];
5099ac24
FG
237 if let Some((path, _)) = &used_crate_source.rlib {
238 f(cnum, &path);
136023e0 239 } else {
5099ac24
FG
240 if used_crate_source.rmeta.is_some() {
241 return Err(format!(
242 "could not find rlib for: `{}`, found rmeta (metadata) file",
243 name
244 ));
245 } else {
246 return Err(format!("could not find rlib for: `{}`", name));
247 }
248 }
a1dfa0c6
XL
249 }
250 Ok(())
251}
252
fc512014
XL
253/// Create an 'rlib'.
254///
255/// An rlib in its current incarnation is essentially a renamed .a file. The rlib primarily contains
256/// the object file of the crate, but it also contains all of the object files from native
257/// libraries. This is done by unzipping native libraries and inserting all of the contents into
258/// this archive.
064997fb 259fn link_rlib<'a>(
dfeec247 260 sess: &'a Session,
064997fb 261 archive_builder_builder: &dyn ArchiveBuilderBuilder,
dfeec247
XL
262 codegen_results: &CodegenResults,
263 flavor: RlibFlavor,
3dfed10e 264 tmpdir: &MaybeTempDir,
064997fb 265) -> Result<Box<dyn ArchiveBuilder<'a> + 'a>, ErrorGuaranteed> {
c295e0f8
XL
266 let lib_search_paths = archive_search_paths(sess);
267
064997fb 268 let mut ab = archive_builder_builder.new_archive_builder(sess);
48663c56 269
5e7ed085
FG
270 let trailing_metadata = match flavor {
271 RlibFlavor::Normal => {
272 let (metadata, metadata_position) =
273 create_rmeta_file(sess, codegen_results.metadata.raw_data());
274 let metadata = emit_metadata(sess, &metadata, tmpdir);
275 match metadata_position {
276 MetadataPosition::First => {
277 // Most of the time metadata in rlib files is wrapped in a "dummy" object
278 // file for the target platform so the rlib can be processed entirely by
279 // normal linkers for the platform. Sometimes this is not possible however.
280 // If it is possible however, placing the metadata object first improves
281 // performance of getting metadata from rlibs.
282 ab.add_file(&metadata);
283 None
284 }
285 MetadataPosition::Last => Some(metadata),
286 }
287 }
288
289 RlibFlavor::StaticlibBase => None,
290 };
291
a2a8927a
XL
292 for m in &codegen_results.modules {
293 if let Some(obj) = m.object.as_ref() {
294 ab.add_file(obj);
295 }
296
297 if let Some(dwarf_obj) = m.dwarf_object.as_ref() {
298 ab.add_file(dwarf_obj);
299 }
48663c56
XL
300 }
301
5e7ed085
FG
302 match flavor {
303 RlibFlavor::Normal => {}
304 RlibFlavor::StaticlibBase => {
305 let obj = codegen_results.allocator_module.as_ref().and_then(|m| m.object.as_ref());
306 if let Some(obj) = obj {
307 ab.add_file(obj);
308 }
309 }
310 }
311
f2b60f7d
FG
312 // Used if packed_bundled_libs flag enabled.
313 let mut packed_bundled_libs = Vec::new();
314
48663c56
XL
315 // Note that in this loop we are ignoring the value of `lib.cfg`. That is,
316 // we may not be configured to actually include a static library if we're
317 // adding it here. That's because later when we consume this rlib we'll
318 // decide whether we actually needed the static library or not.
319 //
320 // To do this "correctly" we'd need to keep track of which libraries added
321 // which object files to the archive. We don't do that here, however. The
322 // #[link(cfg(..))] feature is unstable, though, and only intended to get
323 // liblibc working. In that sense the check below just indicates that if
324 // there are any libraries we want to omit object files for at link time we
325 // just exclude all custom object files.
326 //
327 // Eventually if we want to stabilize or flesh out the #[link(cfg(..))]
328 // feature then we'll need to figure out how to record what objects were
329 // loaded from the libraries found here and then encode that into the
330 // metadata of the rlib we're generating somehow.
331 for lib in codegen_results.crate_info.used_libraries.iter() {
332 match lib.kind {
f2b60f7d
FG
333 NativeLibKind::Static { bundle: None | Some(true), whole_archive: Some(true) }
334 if flavor == RlibFlavor::Normal && sess.opts.unstable_opts.packed_bundled_libs => {}
c295e0f8
XL
335 NativeLibKind::Static { bundle: None | Some(true), whole_archive: Some(true) }
336 if flavor == RlibFlavor::Normal =>
337 {
338 // Don't allow mixing +bundle with +whole_archive since an rlib may contain
339 // multiple native libs, some of which are +whole-archive and some of which are
340 // -whole-archive and it isn't clear how we can currently handle such a
341 // situation correctly.
342 // See https://github.com/rust-lang/rust/issues/88085#issuecomment-901050897
343 sess.err(
344 "the linking modifiers `+bundle` and `+whole-archive` are not compatible \
345 with each other when generating rlibs",
346 );
347 }
17df50a5
XL
348 NativeLibKind::Static { bundle: None | Some(true), .. } => {}
349 NativeLibKind::Static { bundle: Some(false), .. }
350 | NativeLibKind::Dylib { .. }
351 | NativeLibKind::Framework { .. }
f9f354fc 352 | NativeLibKind::RawDylib
064997fb 353 | NativeLibKind::LinkArg
f9f354fc 354 | NativeLibKind::Unspecified => continue,
48663c56
XL
355 }
356 if let Some(name) = lib.name {
c295e0f8 357 let location =
f2b60f7d
FG
358 find_native_static_library(name.as_str(), lib.verbatim, &lib_search_paths, sess);
359 if sess.opts.unstable_opts.packed_bundled_libs && flavor == RlibFlavor::Normal {
360 packed_bundled_libs.push(find_native_static_library(
361 lib.filename.unwrap().as_str(),
362 Some(true),
363 &lib_search_paths,
364 sess,
365 ));
366 continue;
367 }
064997fb 368 ab.add_archive(&location, Box::new(|_| false)).unwrap_or_else(|e| {
c295e0f8
XL
369 sess.fatal(&format!(
370 "failed to add native library {}: {}",
371 location.to_string_lossy(),
372 e
373 ));
374 });
48663c56
XL
375 }
376 }
377
17df50a5 378 for (raw_dylib_name, raw_dylib_imports) in
f2b60f7d 379 collate_raw_dylibs(sess, codegen_results.crate_info.used_libraries.iter())?
17df50a5 380 {
064997fb
FG
381 let output_path = archive_builder_builder.create_dll_import_lib(
382 sess,
383 &raw_dylib_name,
384 &raw_dylib_imports,
385 tmpdir.as_ref(),
f2b60f7d 386 true,
064997fb
FG
387 );
388
389 ab.add_archive(&output_path, Box::new(|_| false)).unwrap_or_else(|e| {
390 sess.fatal(&format!("failed to add native library {}: {}", output_path.display(), e));
391 });
17df50a5
XL
392 }
393
5e7ed085
FG
394 if let Some(trailing_metadata) = trailing_metadata {
395 // Note that it is important that we add all of our non-object "magical
396 // files" *after* all of the object files in the archive. The reason for
397 // this is as follows:
398 //
399 // * When performing LTO, this archive will be modified to remove
400 // objects from above. The reason for this is described below.
401 //
402 // * When the system linker looks at an archive, it will attempt to
403 // determine the architecture of the archive in order to see whether its
404 // linkable.
405 //
406 // The algorithm for this detection is: iterate over the files in the
407 // archive. Skip magical SYMDEF names. Interpret the first file as an
408 // object file. Read architecture from the object file.
409 //
410 // * As one can probably see, if "metadata" and "foo.bc" were placed
411 // before all of the objects, then the architecture of this archive would
412 // not be correctly inferred once 'foo.o' is removed.
413 //
414 // * Most of the time metadata in rlib files is wrapped in a "dummy" object
415 // file for the target platform so the rlib can be processed entirely by
416 // normal linkers for the platform. Sometimes this is not possible however.
417 //
418 // Basically, all this means is that this code should not move above the
419 // code above.
420 ab.add_file(&trailing_metadata);
48663c56 421 }
5099ac24 422
f2b60f7d
FG
423 // Add all bundled static native library dependencies.
424 // Archives added to the end of .rlib archive, see comment above for the reason.
425 for lib in packed_bundled_libs {
426 ab.add_file(&lib)
427 }
428
136023e0 429 return Ok(ab);
17df50a5
XL
430}
431
432/// Extract all symbols defined in raw-dylib libraries, collated by library name.
433///
434/// If we have multiple extern blocks that specify symbols defined in the same raw-dylib library,
435/// then the CodegenResults value contains one NativeLib instance for each block. However, the
436/// linker appears to expect only a single import library for each library used, so we need to
437/// collate the symbols together by library name before generating the import libraries.
f2b60f7d
FG
438fn collate_raw_dylibs<'a, 'b>(
439 sess: &'a Session,
440 used_libraries: impl IntoIterator<Item = &'b NativeLib>,
5e7ed085 441) -> Result<Vec<(String, Vec<DllImport>)>, ErrorGuaranteed> {
136023e0
XL
442 // Use index maps to preserve original order of imports and libraries.
443 let mut dylib_table = FxIndexMap::<String, FxIndexMap<Symbol, &DllImport>>::default();
17df50a5
XL
444
445 for lib in used_libraries {
446 if lib.kind == NativeLibKind::RawDylib {
136023e0
XL
447 let ext = if matches!(lib.verbatim, Some(true)) { "" } else { ".dll" };
448 let name = format!("{}{}", lib.name.expect("unnamed raw-dylib library"), ext);
449 let imports = dylib_table.entry(name.clone()).or_default();
450 for import in &lib.dll_imports {
451 if let Some(old_import) = imports.insert(import.name, import) {
452 // FIXME: when we add support for ordinals, figure out if we need to do anything
453 // if we have two DllImport values with the same name but different ordinals.
454 if import.calling_convention != old_import.calling_convention {
455 sess.span_err(
456 import.span,
457 &format!(
458 "multiple declarations of external function `{}` from \
459 library `{}` have different calling conventions",
460 import.name, name,
461 ),
462 );
463 }
464 }
465 }
17df50a5
XL
466 }
467 }
136023e0
XL
468 sess.compile_status()?;
469 Ok(dylib_table
17df50a5 470 .into_iter()
136023e0
XL
471 .map(|(name, imports)| {
472 (name, imports.into_iter().map(|(_, import)| import.clone()).collect())
17df50a5 473 })
136023e0 474 .collect())
48663c56
XL
475}
476
fc512014
XL
477/// Create a static archive.
478///
479/// This is essentially the same thing as an rlib, but it also involves adding all of the upstream
480/// crates' objects into the archive. This will slurp in all of the native libraries of upstream
481/// dependencies as well.
482///
483/// Additionally, there's no way for us to link dynamic libraries, so we warn about all dynamic
484/// library dependencies that they're not linked in.
485///
486/// There's no need to include metadata in a static archive, so ensure to not link in the metadata
487/// object file (and also don't prepare the archive with a metadata file).
064997fb 488fn link_staticlib<'a>(
dfeec247 489 sess: &'a Session,
064997fb 490 archive_builder_builder: &dyn ArchiveBuilderBuilder,
dfeec247
XL
491 codegen_results: &CodegenResults,
492 out_filename: &Path,
3dfed10e 493 tempdir: &MaybeTempDir,
5e7ed085 494) -> Result<(), ErrorGuaranteed> {
064997fb
FG
495 info!("preparing staticlib to {:?}", out_filename);
496 let mut ab = link_rlib(
497 sess,
498 archive_builder_builder,
499 codegen_results,
500 RlibFlavor::StaticlibBase,
501 tempdir,
502 )?;
48663c56
XL
503 let mut all_native_libs = vec![];
504
e74abb32 505 let res = each_linked_rlib(&codegen_results.crate_info, &mut |cnum, path| {
04454e1e 506 let name = codegen_results.crate_info.crate_name[&cnum];
48663c56
XL
507 let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
508
509 // Here when we include the rlib into our staticlib we need to make a
510 // decision whether to include the extra object files along the way.
511 // These extra object files come from statically included native
512 // libraries, but they may be cfg'd away with #[link(cfg(..))].
513 //
514 // This unstable feature, though, only needs liblibc to work. The only
515 // use case there is where musl is statically included in liblibc.rlib,
516 // so if we don't want the included version we just need to skip it. As
517 // a result the logic here is that if *any* linked library is cfg'd away
518 // we just skip all object files.
519 //
520 // Clearly this is not sufficient for a general purpose feature, and
521 // we'd want to read from the library's metadata to determine which
522 // object files come from where and selectively skip them.
17df50a5
XL
523 let skip_object_files = native_libs.iter().any(|lib| {
524 matches!(lib.kind, NativeLibKind::Static { bundle: None | Some(true), .. })
525 && !relevant_lib(sess, lib)
526 });
c295e0f8
XL
527
528 let lto = are_upstream_rust_objects_already_included(sess)
529 && !ignored_for_lto(sess, &codegen_results.crate_info, cnum);
530
531 // Ignoring obj file starting with the crate name
532 // as simple comparison is not enough - there
533 // might be also an extra name suffix
534 let obj_start = name.as_str().to_owned();
535
064997fb
FG
536 ab.add_archive(
537 path,
538 Box::new(move |fname: &str| {
539 // Ignore metadata files, no matter the name.
540 if fname == METADATA_FILENAME {
541 return true;
542 }
c295e0f8 543
064997fb
FG
544 // Don't include Rust objects if LTO is enabled
545 if lto && looks_like_rust_object_file(fname) {
546 return true;
547 }
c295e0f8 548
064997fb
FG
549 // Otherwise if this is *not* a rust object and we're skipping
550 // objects then skip this file
551 if skip_object_files && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
552 return true;
553 }
c295e0f8 554
064997fb
FG
555 // ok, don't skip this
556 false
557 }),
558 )
dfeec247 559 .unwrap();
48663c56
XL
560
561 all_native_libs.extend(codegen_results.crate_info.native_libraries[&cnum].iter().cloned());
562 });
563 if let Err(e) = res {
564 sess.fatal(&e);
565 }
566
064997fb 567 ab.build(out_filename);
48663c56
XL
568
569 if !all_native_libs.is_empty() {
570 if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
571 print_native_static_libs(sess, &all_native_libs);
572 }
573 }
136023e0
XL
574
575 Ok(())
48663c56
XL
576}
577
a2a8927a
XL
578/// Use `thorin` (rust implementation of a dwarf packaging utility) to link DWARF objects into a
579/// DWARF package.
580fn link_dwarf_object<'a>(
581 sess: &'a Session,
582 cg_results: &CodegenResults,
583 executable_out_filename: &Path,
584) {
fc512014 585 let dwp_out_filename = executable_out_filename.with_extension("dwp");
a2a8927a 586 debug!(?dwp_out_filename, ?executable_out_filename);
fc512014 587
a2a8927a
XL
588 #[derive(Default)]
589 struct ThorinSession<Relocations> {
590 arena_data: TypedArena<Vec<u8>>,
591 arena_mmap: TypedArena<Mmap>,
592 arena_relocations: TypedArena<Relocations>,
fc512014 593 }
fc512014 594
a2a8927a
XL
595 impl<Relocations> ThorinSession<Relocations> {
596 fn alloc_mmap<'arena>(&'arena self, data: Mmap) -> &'arena Mmap {
597 (*self.arena_mmap.alloc(data)).borrow()
fc512014 598 }
a2a8927a 599 }
fc512014 600
a2a8927a
XL
601 impl<Relocations> thorin::Session<Relocations> for ThorinSession<Relocations> {
602 fn alloc_data<'arena>(&'arena self, data: Vec<u8>) -> &'arena [u8] {
603 (*self.arena_data.alloc(data)).borrow()
604 }
fc512014 605
a2a8927a
XL
606 fn alloc_relocation<'arena>(&'arena self, data: Relocations) -> &'arena Relocations {
607 (*self.arena_relocations.alloc(data)).borrow()
608 }
609
610 fn read_input<'arena>(&'arena self, path: &Path) -> std::io::Result<&'arena [u8]> {
611 let file = File::open(&path)?;
612 let mmap = (unsafe { Mmap::map(file) })?;
613 Ok(self.alloc_mmap(mmap))
614 }
615 }
616
617 match sess.time("run_thorin", || -> Result<(), thorin::Error> {
618 let thorin_sess = ThorinSession::default();
619 let mut package = thorin::DwarfPackage::new(&thorin_sess);
620
621 // Input objs contain .o/.dwo files from the current crate.
064997fb 622 match sess.opts.unstable_opts.split_dwarf_kind {
a2a8927a
XL
623 SplitDwarfKind::Single => {
624 for input_obj in cg_results.modules.iter().filter_map(|m| m.object.as_ref()) {
625 package.add_input_object(input_obj)?;
626 }
fc512014 627 }
a2a8927a
XL
628 SplitDwarfKind::Split => {
629 for input_obj in cg_results.modules.iter().filter_map(|m| m.dwarf_object.as_ref()) {
630 package.add_input_object(input_obj)?;
631 }
632 }
633 }
fc512014 634
a2a8927a
XL
635 // Input rlibs contain .o/.dwo files from dependencies.
636 let input_rlibs = cg_results
637 .crate_info
638 .used_crate_source
639 .values()
640 .filter_map(|csource| csource.rlib.as_ref())
641 .map(|(path, _)| path);
642 for input_rlib in input_rlibs {
643 debug!(?input_rlib);
644 package.add_input_object(input_rlib)?;
645 }
646
647 // Failing to read the referenced objects is expected for dependencies where the path in the
648 // executable will have been cleaned by Cargo, but the referenced objects will be contained
649 // within rlibs provided as inputs.
650 //
651 // If paths have been remapped, then .o/.dwo files from the current crate also won't be
652 // found, but are provided explicitly above.
653 //
654 // Adding an executable is primarily done to make `thorin` check that all the referenced
655 // dwarf objects are found in the end.
656 package.add_executable(
657 &executable_out_filename,
658 thorin::MissingReferencedObjectBehaviour::Skip,
659 )?;
660
661 let output = package.finish()?.write()?;
662 let mut output_stream = BufWriter::new(
663 OpenOptions::new()
664 .read(true)
665 .write(true)
666 .create(true)
667 .truncate(true)
668 .open(dwp_out_filename)?,
669 );
670 output_stream.write_all(&output)?;
671 output_stream.flush()?;
672
673 Ok(())
674 }) {
675 Ok(()) => {}
676 Err(e) => {
677 sess.struct_err("linking dwarf objects with thorin failed")
678 .note(&format!("{:?}", e))
679 .emit();
064997fb 680 sess.abort_if_errors();
fc512014
XL
681 }
682 }
683}
684
685/// Create a dynamic library or executable.
686///
687/// This will invoke the system linker/cc to create the resulting file. This links to all upstream
688/// files as well.
064997fb 689fn link_natively<'a>(
dfeec247 690 sess: &'a Session,
064997fb 691 archive_builder_builder: &dyn ArchiveBuilderBuilder,
f9f354fc 692 crate_type: CrateType,
dfeec247
XL
693 out_filename: &Path,
694 codegen_results: &CodegenResults,
695 tmpdir: &Path,
064997fb 696) -> Result<(), ErrorGuaranteed> {
48663c56 697 info!("preparing {:?} to {:?}", crate_type, out_filename);
ba9703b0 698 let (linker_path, flavor) = linker_and_flavor(sess);
064997fb 699 let mut cmd = linker_with_args(
ba9703b0
XL
700 &linker_path,
701 flavor,
702 sess,
064997fb 703 archive_builder_builder,
ba9703b0
XL
704 crate_type,
705 tmpdir,
706 out_filename,
707 codegen_results,
064997fb 708 )?;
48663c56 709
f9f354fc
XL
710 linker::disable_localization(&mut cmd);
711
5e7ed085
FG
712 for &(ref k, ref v) in sess.target.link_env.as_ref() {
713 cmd.env(k.as_ref(), v.as_ref());
48663c56 714 }
5e7ed085
FG
715 for k in sess.target.link_env_remove.as_ref() {
716 cmd.env_remove(k.as_ref());
e1599b0c 717 }
48663c56 718
5099ac24 719 if sess.opts.prints.contains(&PrintRequest::LinkArgs) {
48663c56
XL
720 println!("{:?}", &cmd);
721 }
722
723 // May have not found libraries in the right formats.
724 sess.abort_if_errors();
725
726 // Invoke the system linker
48663c56
XL
727 info!("{:?}", &cmd);
728 let retry_on_segfault = env::var("RUSTC_RETRY_LINKER_ON_SEGFAULT").is_ok();
94222f64
XL
729 let unknown_arg_regex =
730 Regex::new(r"(unknown|unrecognized) (command line )?(option|argument)").unwrap();
48663c56
XL
731 let mut prog;
732 let mut i = 0;
733 loop {
734 i += 1;
ba9703b0 735 prog = sess.time("run_linker", || exec_linker(sess, &cmd, out_filename, tmpdir));
5e7ed085
FG
736 let Ok(ref output) = prog else {
737 break;
48663c56
XL
738 };
739 if output.status.success() {
dfeec247 740 break;
48663c56
XL
741 }
742 let mut out = output.stderr.clone();
743 out.extend(&output.stdout);
744 let out = String::from_utf8_lossy(&out);
745
94222f64 746 // Check to see if the link failed with an error message that indicates it
5e7ed085 747 // doesn't recognize the -no-pie option. If so, re-perform the link step
94222f64
XL
748 // without it. This is safe because if the linker doesn't support -no-pie
749 // then it should not default to linking executables as pie. Different
750 // versions of gcc seem to use different quotes in the error message so
751 // don't check for them.
29967ef6 752 if sess.target.linker_is_gnu
dfeec247 753 && flavor != LinkerFlavor::Ld
94222f64 754 && unknown_arg_regex.is_match(&out)
dfeec247
XL
755 && out.contains("-no-pie")
756 && cmd.get_args().iter().any(|e| e.to_string_lossy() == "-no-pie")
757 {
48663c56
XL
758 info!("linker output: {:?}", out);
759 warn!("Linker does not support -no-pie command line option. Retrying without.");
760 for arg in cmd.take_args() {
761 if arg.to_string_lossy() != "-no-pie" {
762 cmd.arg(arg);
763 }
764 }
765 info!("{:?}", &cmd);
766 continue;
767 }
dc9dc135 768
f035d41b
XL
769 // Detect '-static-pie' used with an older version of gcc or clang not supporting it.
770 // Fallback from '-static-pie' to '-static' in that case.
29967ef6 771 if sess.target.linker_is_gnu
f035d41b 772 && flavor != LinkerFlavor::Ld
94222f64 773 && unknown_arg_regex.is_match(&out)
f035d41b
XL
774 && (out.contains("-static-pie") || out.contains("--no-dynamic-linker"))
775 && cmd.get_args().iter().any(|e| e.to_string_lossy() == "-static-pie")
776 {
777 info!("linker output: {:?}", out);
778 warn!(
779 "Linker does not support -static-pie command line option. Retrying with -static instead."
780 );
781 // Mirror `add_(pre,post)_link_objects` to replace CRT objects.
f2b60f7d 782 let self_contained = self_contained(sess, crate_type);
29967ef6 783 let opts = &sess.target;
f035d41b 784 let pre_objects = if self_contained {
f2b60f7d 785 &opts.pre_link_objects_self_contained
f035d41b
XL
786 } else {
787 &opts.pre_link_objects
788 };
789 let post_objects = if self_contained {
f2b60f7d 790 &opts.post_link_objects_self_contained
f035d41b
XL
791 } else {
792 &opts.post_link_objects
793 };
794 let get_objects = |objects: &CrtObjects, kind| {
795 objects
796 .get(&kind)
797 .iter()
798 .copied()
799 .flatten()
800 .map(|obj| get_object_file_path(sess, obj, self_contained).into_os_string())
801 .collect::<Vec<_>>()
802 };
803 let pre_objects_static_pie = get_objects(pre_objects, LinkOutputKind::StaticPicExe);
804 let post_objects_static_pie = get_objects(post_objects, LinkOutputKind::StaticPicExe);
805 let mut pre_objects_static = get_objects(pre_objects, LinkOutputKind::StaticNoPicExe);
806 let mut post_objects_static = get_objects(post_objects, LinkOutputKind::StaticNoPicExe);
807 // Assume that we know insertion positions for the replacement arguments from replaced
808 // arguments, which is true for all supported targets.
809 assert!(pre_objects_static.is_empty() || !pre_objects_static_pie.is_empty());
810 assert!(post_objects_static.is_empty() || !post_objects_static_pie.is_empty());
811 for arg in cmd.take_args() {
812 if arg.to_string_lossy() == "-static-pie" {
813 // Replace the output kind.
814 cmd.arg("-static");
815 } else if pre_objects_static_pie.contains(&arg) {
816 // Replace the pre-link objects (replace the first and remove the rest).
817 cmd.args(mem::take(&mut pre_objects_static));
818 } else if post_objects_static_pie.contains(&arg) {
819 // Replace the post-link objects (replace the first and remove the rest).
820 cmd.args(mem::take(&mut post_objects_static));
821 } else {
822 cmd.arg(arg);
823 }
824 }
825 info!("{:?}", &cmd);
826 continue;
827 }
828
dc9dc135
XL
829 // Here's a terribly awful hack that really shouldn't be present in any
830 // compiler. Here an environment variable is supported to automatically
831 // retry the linker invocation if the linker looks like it segfaulted.
832 //
833 // Gee that seems odd, normally segfaults are things we want to know
834 // about! Unfortunately though in rust-lang/rust#38878 we're
835 // experiencing the linker segfaulting on Travis quite a bit which is
836 // causing quite a bit of pain to land PRs when they spuriously fail
837 // due to a segfault.
838 //
839 // The issue #38878 has some more debugging information on it as well,
840 // but this unfortunately looks like it's just a race condition in
841 // macOS's linker with some thread pool working in the background. It
842 // seems that no one currently knows a fix for this so in the meantime
843 // we're left with this...
48663c56 844 if !retry_on_segfault || i > 3 {
dfeec247 845 break;
48663c56
XL
846 }
847 let msg_segv = "clang: error: unable to execute command: Segmentation fault: 11";
dfeec247 848 let msg_bus = "clang: error: unable to execute command: Bus error: 10";
dc9dc135
XL
849 if out.contains(msg_segv) || out.contains(msg_bus) {
850 warn!(
c295e0f8 851 ?cmd, %out,
dc9dc135 852 "looks like the linker segfaulted when we tried to call it, \
c295e0f8 853 automatically retrying again",
dc9dc135
XL
854 );
855 continue;
48663c56
XL
856 }
857
dc9dc135
XL
858 if is_illegal_instruction(&output.status) {
859 warn!(
c295e0f8 860 ?cmd, %out, status = %output.status,
dc9dc135 861 "looks like the linker hit an illegal instruction when we \
c295e0f8 862 tried to call it, automatically retrying again.",
dc9dc135
XL
863 );
864 continue;
865 }
866
867 #[cfg(unix)]
868 fn is_illegal_instruction(status: &ExitStatus) -> bool {
869 use std::os::unix::prelude::*;
870 status.signal() == Some(libc::SIGILL)
871 }
872
6a06907d 873 #[cfg(not(unix))]
dc9dc135
XL
874 fn is_illegal_instruction(_status: &ExitStatus) -> bool {
875 false
876 }
48663c56
XL
877 }
878
879 match prog {
880 Ok(prog) => {
48663c56
XL
881 if !prog.status.success() {
882 let mut output = prog.stderr.clone();
883 output.extend_from_slice(&prog.stdout);
f2b60f7d 884 let escaped_output = escape_string(&output);
136023e0 885 let mut err = sess.struct_err(&format!(
dfeec247 886 "linking with `{}` failed: {}",
ba9703b0 887 linker_path.display(),
dfeec247 888 prog.status
136023e0
XL
889 ));
890 err.note(&format!("{:?}", &cmd)).note(&escaped_output);
891 if escaped_output.contains("undefined reference to") {
892 err.help(
893 "some `extern` functions couldn't be found; some native libraries may \
894 need to be installed or have their path specified",
895 );
896 err.note("use the `-l` flag to specify native libraries to link");
897 err.note("use the `cargo:rustc-link-lib` directive to specify the native \
898 libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)");
899 }
900 err.emit();
f9f354fc
XL
901
902 // If MSVC's `link.exe` was expected but the return code
903 // is not a Microsoft LNK error then suggest a way to fix or
904 // install the Visual Studio build tools.
905 if let Some(code) = prog.status.code() {
29967ef6 906 if sess.target.is_like_msvc
f9f354fc
XL
907 && flavor == LinkerFlavor::Msvc
908 // Respect the command line override
909 && sess.opts.cg.linker.is_none()
910 // Match exactly "link.exe"
911 && linker_path.to_str() == Some("link.exe")
912 // All Microsoft `link.exe` linking error codes are
913 // four digit numbers in the range 1000 to 9999 inclusive
914 && (code < 1000 || code > 9999)
915 {
916 let is_vs_installed = windows_registry::find_vs_version().is_ok();
917 let has_linker = windows_registry::find_tool(
918 &sess.opts.target_triple.triple(),
919 "link.exe",
920 )
921 .is_some();
922
923 sess.note_without_error("`link.exe` returned an unexpected error");
924 if is_vs_installed && has_linker {
925 // the linker is broken
926 sess.note_without_error(
927 "the Visual Studio build tools may need to be repaired \
928 using the Visual Studio installer",
929 );
930 sess.note_without_error(
931 "or a necessary component may be missing from the \
932 \"C++ build tools\" workload",
933 );
934 } else if is_vs_installed {
935 // the linker is not installed
936 sess.note_without_error(
937 "in the Visual Studio installer, ensure the \
938 \"C++ build tools\" workload is selected",
939 );
940 } else {
941 // visual studio is not installed
942 sess.note_without_error(
943 "you may need to install Visual Studio build tools with the \
944 \"C++ build tools\" workload",
945 );
946 }
947 }
948 }
949
48663c56
XL
950 sess.abort_if_errors();
951 }
f2b60f7d
FG
952 info!("linker stderr:\n{}", escape_string(&prog.stderr));
953 info!("linker stdout:\n{}", escape_string(&prog.stdout));
dfeec247 954 }
48663c56
XL
955 Err(e) => {
956 let linker_not_found = e.kind() == io::ErrorKind::NotFound;
957
958 let mut linker_error = {
959 if linker_not_found {
ba9703b0 960 sess.struct_err(&format!("linker `{}` not found", linker_path.display()))
48663c56 961 } else {
ba9703b0
XL
962 sess.struct_err(&format!(
963 "could not exec the linker `{}`",
964 linker_path.display()
965 ))
48663c56
XL
966 }
967 };
968
969 linker_error.note(&e.to_string());
970
971 if !linker_not_found {
972 linker_error.note(&format!("{:?}", &cmd));
973 }
974
975 linker_error.emit();
976
29967ef6 977 if sess.target.is_like_msvc && linker_not_found {
416331ca
XL
978 sess.note_without_error(
979 "the msvc targets depend on the msvc linker \
980 but `link.exe` was not found",
981 );
982 sess.note_without_error(
5099ac24 983 "please ensure that VS 2013, VS 2015, VS 2017, VS 2019 or VS 2022 \
416331ca
XL
984 was installed with the Visual C++ option",
985 );
48663c56
XL
986 }
987 sess.abort_if_errors();
988 }
989 }
990
5869c6ff
XL
991 match sess.split_debuginfo() {
992 // If split debug information is disabled or located in individual files
993 // there's nothing to do here.
994 SplitDebuginfo::Off | SplitDebuginfo::Unpacked => {}
995
996 // If packed split-debuginfo is requested, but the final compilation
997 // doesn't actually have any debug information, then we skip this step.
998 SplitDebuginfo::Packed if sess.opts.debuginfo == DebugInfo::None => {}
999
1000 // On macOS the external `dsymutil` tool is used to create the packed
1001 // debug information. Note that this will read debug information from
1002 // the objects on the filesystem which we'll clean up later.
1003 SplitDebuginfo::Packed if sess.target.is_like_osx => {
1004 let prog = Command::new("dsymutil").arg(out_filename).output();
1005 match prog {
1006 Ok(prog) => {
1007 if !prog.status.success() {
1008 let mut output = prog.stderr.clone();
1009 output.extend_from_slice(&prog.stdout);
1010 sess.struct_warn(&format!(
1011 "processing debug info with `dsymutil` failed: {}",
1012 prog.status
1013 ))
1014 .note(&escape_string(&output))
1015 .emit();
1016 }
fc512014 1017 }
5869c6ff 1018 Err(e) => sess.fatal(&format!("unable to run `dsymutil`: {}", e)),
fc512014 1019 }
48663c56 1020 }
5869c6ff
XL
1021
1022 // On MSVC packed debug information is produced by the linker itself so
1023 // there's no need to do anything else here.
04454e1e 1024 SplitDebuginfo::Packed if sess.target.is_like_windows => {}
5869c6ff
XL
1025
1026 // ... and otherwise we're processing a `*.dwp` packed dwarf file.
a2a8927a 1027 //
5e7ed085 1028 // We cannot rely on the .o paths in the executable because they may have been
a2a8927a
XL
1029 // remapped by --remap-path-prefix and therefore invalid, so we need to provide
1030 // the .o/.dwo paths explicitly.
1031 SplitDebuginfo::Packed => link_dwarf_object(sess, codegen_results, out_filename),
48663c56 1032 }
17df50a5 1033
3c0e092e
XL
1034 let strip = strip_value(sess);
1035
17df50a5 1036 if sess.target.is_like_osx {
923072b8
FG
1037 match (strip, crate_type) {
1038 (Strip::Debuginfo, _) => strip_symbols_in_osx(sess, &out_filename, Some("-S")),
1039 // Per the manpage, `-x` is the maximum safe strip level for dynamic libraries. (#93988)
1040 (Strip::Symbols, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) => {
1041 strip_symbols_in_osx(sess, &out_filename, Some("-x"))
1042 }
1043 (Strip::Symbols, _) => strip_symbols_in_osx(sess, &out_filename, None),
1044 (Strip::None, _) => {}
17df50a5
XL
1045 }
1046 }
064997fb
FG
1047
1048 Ok(())
17df50a5
XL
1049}
1050
3c0e092e
XL
1051// Temporarily support both -Z strip and -C strip
1052fn strip_value(sess: &Session) -> Strip {
064997fb 1053 match (sess.opts.unstable_opts.strip, sess.opts.cg.strip) {
3c0e092e
XL
1054 (s, Strip::None) => s,
1055 (_, s) => s,
1056 }
1057}
1058
c295e0f8
XL
1059fn strip_symbols_in_osx<'a>(sess: &'a Session, out_filename: &Path, option: Option<&str>) {
1060 let mut cmd = Command::new("strip");
1061 if let Some(option) = option {
1062 cmd.arg(option);
1063 }
1064 let prog = cmd.arg(out_filename).output();
17df50a5
XL
1065 match prog {
1066 Ok(prog) => {
1067 if !prog.status.success() {
1068 let mut output = prog.stderr.clone();
1069 output.extend_from_slice(&prog.stdout);
1070 sess.struct_warn(&format!(
1071 "stripping debug info with `strip` failed: {}",
1072 prog.status
1073 ))
1074 .note(&escape_string(&output))
1075 .emit();
1076 }
1077 }
1078 Err(e) => sess.fatal(&format!("unable to run `strip`: {}", e)),
1079 }
1080}
1081
17df50a5 1082fn escape_string(s: &[u8]) -> String {
f2b60f7d
FG
1083 match str::from_utf8(s) {
1084 Ok(s) => s.to_owned(),
1085 Err(_) => format!("Non-UTF-8 output: {}", s.escape_ascii()),
1086 }
48663c56
XL
1087}
1088
17df50a5 1089fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) {
3dfed10e
XL
1090 // On macOS the runtimes are distributed as dylibs which should be linked to
1091 // both executables and dynamic shared objects. Everywhere else the runtimes
5e7ed085 1092 // are currently distributed as static libraries which should be linked to
3dfed10e
XL
1093 // executables only.
1094 let needs_runtime = match crate_type {
1095 CrateType::Executable => true,
29967ef6 1096 CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => sess.target.is_like_osx,
3dfed10e
XL
1097 CrateType::Rlib | CrateType::Staticlib => false,
1098 };
1099
1100 if !needs_runtime {
dfeec247
XL
1101 return;
1102 }
3dfed10e 1103
064997fb 1104 let sanitizer = sess.opts.unstable_opts.sanitizer;
f035d41b
XL
1105 if sanitizer.contains(SanitizerSet::ADDRESS) {
1106 link_sanitizer_runtime(sess, linker, "asan");
1107 }
1108 if sanitizer.contains(SanitizerSet::LEAK) {
1109 link_sanitizer_runtime(sess, linker, "lsan");
1110 }
1111 if sanitizer.contains(SanitizerSet::MEMORY) {
1112 link_sanitizer_runtime(sess, linker, "msan");
1113 }
1114 if sanitizer.contains(SanitizerSet::THREAD) {
1115 link_sanitizer_runtime(sess, linker, "tsan");
1116 }
6a06907d
XL
1117 if sanitizer.contains(SanitizerSet::HWADDRESS) {
1118 link_sanitizer_runtime(sess, linker, "hwasan");
1119 }
f035d41b 1120}
dfeec247 1121
f035d41b 1122fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
3c0e092e 1123 fn find_sanitizer_runtime(sess: &Session, filename: &str) -> PathBuf {
5869c6ff
XL
1124 let session_tlib =
1125 filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple());
3c0e092e 1126 let path = session_tlib.join(filename);
5869c6ff
XL
1127 if path.exists() {
1128 return session_tlib;
1129 } else {
1130 let default_sysroot = filesearch::get_or_default_sysroot();
1131 let default_tlib = filesearch::make_target_lib_path(
1132 &default_sysroot,
1133 sess.opts.target_triple.triple(),
1134 );
1135 return default_tlib;
1136 }
1137 }
1138
74b04a01
XL
1139 let channel = option_env!("CFG_RELEASE_CHANNEL")
1140 .map(|channel| format!("-{}", channel))
1141 .unwrap_or_default();
dfeec247 1142
cdc7bbd5
XL
1143 if sess.target.is_like_osx {
1144 // On Apple platforms, the sanitizer is always built as a dylib, and
1145 // LLVM will link to `@rpath/*.dylib`, so we need to specify an
1146 // rpath to the library as well (the rpath should be absolute, see
1147 // PR #41352 for details).
1148 let filename = format!("rustc{}_rt.{}", channel, name);
1149 let path = find_sanitizer_runtime(&sess, &filename);
1150 let rpath = path.to_str().expect("non-utf8 component in path");
1151 linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
064997fb 1152 linker.link_dylib(&filename, false, true);
cdc7bbd5
XL
1153 } else {
1154 let filename = format!("librustc{}_rt.{}.a", channel, name);
1155 let path = find_sanitizer_runtime(&sess, &filename).join(&filename);
1156 linker.link_whole_rlib(&path);
dfeec247
XL
1157 }
1158}
1159
a1dfa0c6
XL
1160/// Returns a boolean indicating whether the specified crate should be ignored
1161/// during LTO.
1162///
1163/// Crates ignored during LTO are not lumped together in the "massive object
1164/// file" that we create and are linked in their normal rlib states. See
1165/// comments below for what crates do not participate in LTO.
1166///
1167/// It's unusual for a crate to not participate in LTO. Typically only
1168/// compiler-specific and unstable crates have a reason to not participate in
1169/// LTO.
1170pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool {
1171 // If our target enables builtin function lowering in LLVM then the
1172 // crates providing these functions don't participate in LTO (e.g.
1173 // no_builtins or compiler builtins crates).
29967ef6 1174 !sess.target.no_builtins
dfeec247 1175 && (info.compiler_builtins == Some(cnum) || info.is_no_builtins.contains(&cnum))
a1dfa0c6
XL
1176}
1177
17df50a5
XL
1178// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use
1179pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
a1dfa0c6
XL
1180 fn infer_from(
1181 sess: &Session,
1182 linker: Option<PathBuf>,
1183 flavor: Option<LinkerFlavor>,
1184 ) -> Option<(PathBuf, LinkerFlavor)> {
1185 match (linker, flavor) {
1186 (Some(linker), Some(flavor)) => Some((linker, flavor)),
1187 // only the linker flavor is known; use the default linker for the selected flavor
dfeec247
XL
1188 (None, Some(flavor)) => Some((
1189 PathBuf::from(match flavor {
ba9703b0
XL
1190 LinkerFlavor::Gcc => {
1191 if cfg!(any(target_os = "solaris", target_os = "illumos")) {
1192 // On historical Solaris systems, "cc" may have
1193 // been Sun Studio, which is not flag-compatible
1194 // with "gcc". This history casts a long shadow,
1195 // and many modern illumos distributions today
1196 // ship GCC as "gcc" without also making it
1197 // available as "cc".
1198 "gcc"
1199 } else {
1200 "cc"
1201 }
1202 }
dfeec247 1203 LinkerFlavor::Ld => "ld",
dfeec247 1204 LinkerFlavor::Lld(_) => "lld",
f2b60f7d
FG
1205 LinkerFlavor::Msvc => "link.exe",
1206 LinkerFlavor::EmCc => {
1207 if cfg!(windows) {
1208 "emcc.bat"
1209 } else {
1210 "emcc"
1211 }
1212 }
1213 LinkerFlavor::Bpf => "bpf-linker",
1214 LinkerFlavor::Ptx => "rust-ptx-linker",
dfeec247
XL
1215 }),
1216 flavor,
1217 )),
a1dfa0c6 1218 (Some(linker), None) => {
dfeec247
XL
1219 let stem = linker.file_stem().and_then(|stem| stem.to_str()).unwrap_or_else(|| {
1220 sess.fatal("couldn't extract file stem from specified linker")
1221 });
a1dfa0c6
XL
1222
1223 let flavor = if stem == "emcc" {
f2b60f7d 1224 LinkerFlavor::EmCc
532ac7d7
XL
1225 } else if stem == "gcc"
1226 || stem.ends_with("-gcc")
1227 || stem == "clang"
1228 || stem.ends_with("-clang")
1229 {
a1dfa0c6 1230 LinkerFlavor::Gcc
17df50a5
XL
1231 } else if stem == "wasm-ld" || stem.ends_with("-wasm-ld") {
1232 LinkerFlavor::Lld(LldFlavor::Wasm)
a1dfa0c6
XL
1233 } else if stem == "ld" || stem == "ld.lld" || stem.ends_with("-ld") {
1234 LinkerFlavor::Ld
1235 } else if stem == "link" || stem == "lld-link" {
1236 LinkerFlavor::Msvc
1237 } else if stem == "lld" || stem == "rust-lld" {
29967ef6 1238 LinkerFlavor::Lld(sess.target.lld_flavor)
a1dfa0c6
XL
1239 } else {
1240 // fall back to the value in the target spec
29967ef6 1241 sess.target.linker_flavor
a1dfa0c6
XL
1242 };
1243
1244 Some((linker, flavor))
dfeec247 1245 }
a1dfa0c6
XL
1246 (None, None) => None,
1247 }
1248 }
1249
1250 // linker and linker flavor specified via command line have precedence over what the target
1251 // specification specifies
f2b60f7d
FG
1252 let linker_flavor = sess.opts.cg.linker_flavor.map(LinkerFlavor::from_cli);
1253 if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), linker_flavor) {
a1dfa0c6
XL
1254 return ret;
1255 }
1256
1257 if let Some(ret) = infer_from(
1258 sess,
5e7ed085 1259 sess.target.linker.as_deref().map(PathBuf::from),
29967ef6 1260 Some(sess.target.linker_flavor),
a1dfa0c6
XL
1261 ) {
1262 return ret;
1263 }
1264
1265 bug!("Not enough information provided to determine how to invoke the linker");
1266}
48663c56 1267
a2a8927a
XL
1268/// Returns a pair of boolean indicating whether we should preserve the object and
1269/// dwarf object files on the filesystem for their debug information. This is often
1270/// useful with split-dwarf like schemes.
1271fn preserve_objects_for_their_debuginfo(sess: &Session) -> (bool, bool) {
48663c56
XL
1272 // If the objects don't have debuginfo there's nothing to preserve.
1273 if sess.opts.debuginfo == config::DebugInfo::None {
a2a8927a 1274 return (false, false);
48663c56
XL
1275 }
1276
1277 // If we're only producing artifacts that are archives, no need to preserve
1278 // the objects as they're losslessly contained inside the archives.
a2a8927a
XL
1279 if sess.crate_types().iter().all(|&x| x.is_archive()) {
1280 return (false, false);
1281 }
1282
064997fb 1283 match (sess.split_debuginfo(), sess.opts.unstable_opts.split_dwarf_kind) {
a2a8927a
XL
1284 // If there is no split debuginfo then do not preserve objects.
1285 (SplitDebuginfo::Off, _) => (false, false),
1286 // If there is packed split debuginfo, then the debuginfo in the objects
1287 // has been packaged and the objects can be deleted.
1288 (SplitDebuginfo::Packed, _) => (false, false),
1289 // If there is unpacked split debuginfo and the current target can not use
1290 // split dwarf, then keep objects.
1291 (SplitDebuginfo::Unpacked, _) if !sess.target_can_use_split_dwarf() => (true, false),
1292 // If there is unpacked split debuginfo and the target can use split dwarf, then
1293 // keep the object containing that debuginfo (whether that is an object file or
1294 // dwarf object file depends on the split dwarf kind).
1295 (SplitDebuginfo::Unpacked, SplitDwarfKind::Single) => (true, false),
1296 (SplitDebuginfo::Unpacked, SplitDwarfKind::Split) => (false, true),
48663c56 1297 }
48663c56
XL
1298}
1299
c295e0f8 1300fn archive_search_paths(sess: &Session) -> Vec<PathBuf> {
48663c56
XL
1301 sess.target_filesearch(PathKind::Native).search_path_dirs()
1302}
1303
c295e0f8 1304#[derive(PartialEq)]
48663c56
XL
1305enum RlibFlavor {
1306 Normal,
1307 StaticlibBase,
1308}
1309
f9f354fc 1310fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
dfeec247
XL
1311 let lib_args: Vec<_> = all_native_libs
1312 .iter()
48663c56
XL
1313 .filter(|l| relevant_lib(sess, l))
1314 .filter_map(|lib| {
1315 let name = lib.name?;
1316 match lib.kind {
17df50a5
XL
1317 NativeLibKind::Static { bundle: Some(false), .. }
1318 | NativeLibKind::Dylib { .. }
f9f354fc 1319 | NativeLibKind::Unspecified => {
17df50a5 1320 let verbatim = lib.verbatim.unwrap_or(false);
29967ef6 1321 if sess.target.is_like_msvc {
17df50a5
XL
1322 Some(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
1323 } else if sess.target.linker_is_gnu {
1324 Some(format!("-l{}{}", if verbatim { ":" } else { "" }, name))
48663c56
XL
1325 } else {
1326 Some(format!("-l{}", name))
1327 }
dfeec247 1328 }
17df50a5 1329 NativeLibKind::Framework { .. } => {
48663c56
XL
1330 // ld-only syntax, since there are no frameworks in MSVC
1331 Some(format!("-framework {}", name))
dfeec247 1332 }
48663c56 1333 // These are included, no need to print them
17df50a5 1334 NativeLibKind::Static { bundle: None | Some(true), .. }
064997fb 1335 | NativeLibKind::LinkArg
17df50a5 1336 | NativeLibKind::RawDylib => None,
48663c56
XL
1337 }
1338 })
1339 .collect();
1340 if !lib_args.is_empty() {
dfeec247
XL
1341 sess.note_without_error(
1342 "Link against the following native artifacts when linking \
48663c56 1343 against this static library. The order and any duplication \
dfeec247
XL
1344 can be significant on some platforms.",
1345 );
48663c56
XL
1346 // Prefix for greppability
1347 sess.note_without_error(&format!("native-static-libs: {}", &lib_args.join(" ")));
1348 }
1349}
1350
f035d41b 1351fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> PathBuf {
48663c56
XL
1352 let fs = sess.target_filesearch(PathKind::Native);
1353 let file_path = fs.get_lib_path().join(name);
1354 if file_path.exists() {
dfeec247 1355 return file_path;
48663c56 1356 }
f035d41b
XL
1357 // Special directory with objects used only in self-contained linkage mode
1358 if self_contained {
1359 let file_path = fs.get_self_contained_lib_path().join(name);
1360 if file_path.exists() {
1361 return file_path;
1362 }
1363 }
48663c56
XL
1364 for search_path in fs.search_paths() {
1365 let file_path = search_path.dir.join(name);
1366 if file_path.exists() {
dfeec247 1367 return file_path;
48663c56
XL
1368 }
1369 }
1370 PathBuf::from(name)
1371}
1372
ba9703b0 1373fn exec_linker(
dfeec247 1374 sess: &Session,
ba9703b0 1375 cmd: &Command,
dfeec247
XL
1376 out_filename: &Path,
1377 tmpdir: &Path,
1378) -> io::Result<Output> {
48663c56
XL
1379 // When attempting to spawn the linker we run a risk of blowing out the
1380 // size limits for spawning a new process with respect to the arguments
1381 // we pass on the command line.
1382 //
1383 // Here we attempt to handle errors from the OS saying "your list of
1384 // arguments is too big" by reinvoking the linker again with an `@`-file
1385 // that contains all the arguments. The theory is that this is then
1386 // accepted on all linkers and the linker will read all its options out of
1387 // there instead of looking at the command line.
1388 if !cmd.very_likely_to_exceed_some_spawn_limit() {
1389 match cmd.command().stdout(Stdio::piped()).stderr(Stdio::piped()).spawn() {
1390 Ok(child) => {
1391 let output = child.wait_with_output();
1392 flush_linked_file(&output, out_filename)?;
1393 return output;
1394 }
1395 Err(ref e) if command_line_too_big(e) => {
1396 info!("command line to linker was too big: {}", e);
1397 }
dfeec247 1398 Err(e) => return Err(e),
48663c56
XL
1399 }
1400 }
1401
1402 info!("falling back to passing arguments to linker via an @-file");
1403 let mut cmd2 = cmd.clone();
1404 let mut args = String::new();
1405 for arg in cmd2.take_args() {
dfeec247 1406 args.push_str(
29967ef6
XL
1407 &Escape { arg: arg.to_str().unwrap(), is_like_msvc: sess.target.is_like_msvc }
1408 .to_string(),
dfeec247 1409 );
1b1a35ee 1410 args.push('\n');
48663c56
XL
1411 }
1412 let file = tmpdir.join("linker-arguments");
29967ef6 1413 let bytes = if sess.target.is_like_msvc {
48663c56
XL
1414 let mut out = Vec::with_capacity((1 + args.len()) * 2);
1415 // start the stream with a UTF-16 BOM
1416 for c in std::iter::once(0xFEFF).chain(args.encode_utf16()) {
1417 // encode in little endian
1418 out.push(c as u8);
1419 out.push((c >> 8) as u8);
1420 }
1421 out
1422 } else {
1423 args.into_bytes()
1424 };
1425 fs::write(&file, &bytes)?;
1426 cmd2.arg(format!("@{}", file.display()));
1427 info!("invoking linker {:?}", cmd2);
1428 let output = cmd2.output();
1429 flush_linked_file(&output, out_filename)?;
1430 return output;
1431
6a06907d 1432 #[cfg(not(windows))]
48663c56
XL
1433 fn flush_linked_file(_: &io::Result<Output>, _: &Path) -> io::Result<()> {
1434 Ok(())
1435 }
1436
1437 #[cfg(windows)]
dfeec247
XL
1438 fn flush_linked_file(
1439 command_output: &io::Result<Output>,
1440 out_filename: &Path,
1441 ) -> io::Result<()> {
48663c56
XL
1442 // On Windows, under high I/O load, output buffers are sometimes not flushed,
1443 // even long after process exit, causing nasty, non-reproducible output bugs.
1444 //
1445 // File::sync_all() calls FlushFileBuffers() down the line, which solves the problem.
1446 //
1447 // А full writeup of the original Chrome bug can be found at
1448 // randomascii.wordpress.com/2018/02/25/compiler-bug-linker-bug-windows-kernel-bug/amp
1449
1450 if let &Ok(ref out) = command_output {
1451 if out.status.success() {
1452 if let Ok(of) = fs::OpenOptions::new().write(true).open(out_filename) {
1453 of.sync_all()?;
1454 }
1455 }
1456 }
1457
1458 Ok(())
1459 }
1460
1461 #[cfg(unix)]
1462 fn command_line_too_big(err: &io::Error) -> bool {
1463 err.raw_os_error() == Some(::libc::E2BIG)
1464 }
1465
1466 #[cfg(windows)]
1467 fn command_line_too_big(err: &io::Error) -> bool {
1468 const ERROR_FILENAME_EXCED_RANGE: i32 = 206;
1469 err.raw_os_error() == Some(ERROR_FILENAME_EXCED_RANGE)
1470 }
1471
6a06907d
XL
1472 #[cfg(not(any(unix, windows)))]
1473 fn command_line_too_big(_: &io::Error) -> bool {
1474 false
1475 }
1476
48663c56
XL
1477 struct Escape<'a> {
1478 arg: &'a str,
1479 is_like_msvc: bool,
1480 }
1481
1482 impl<'a> fmt::Display for Escape<'a> {
1483 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1484 if self.is_like_msvc {
1485 // This is "documented" at
dfeec247 1486 // https://docs.microsoft.com/en-us/cpp/build/reference/at-specify-a-linker-response-file
48663c56
XL
1487 //
1488 // Unfortunately there's not a great specification of the
1489 // syntax I could find online (at least) but some local
1490 // testing showed that this seemed sufficient-ish to catch
1491 // at least a few edge cases.
1492 write!(f, "\"")?;
1493 for c in self.arg.chars() {
1494 match c {
1495 '"' => write!(f, "\\{}", c)?,
1496 c => write!(f, "{}", c)?,
1497 }
1498 }
1499 write!(f, "\"")?;
1500 } else {
1501 // This is documented at https://linux.die.net/man/1/ld, namely:
1502 //
1503 // > Options in file are separated by whitespace. A whitespace
1504 // > character may be included in an option by surrounding the
1505 // > entire option in either single or double quotes. Any
1506 // > character (including a backslash) may be included by
1507 // > prefixing the character to be included with a backslash.
1508 //
1509 // We put an argument on each line, so all we need to do is
1510 // ensure the line is interpreted as one whole argument.
1511 for c in self.arg.chars() {
1512 match c {
1513 '\\' | ' ' => write!(f, "\\{}", c)?,
1514 c => write!(f, "{}", c)?,
1515 }
1516 }
1517 }
1518 Ok(())
1519 }
1520 }
1521}
1522
f9f354fc
XL
1523fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
1524 let kind = match (crate_type, sess.crt_static(Some(crate_type)), sess.relocation_model()) {
5869c6ff 1525 (CrateType::Executable, _, _) if sess.is_wasi_reactor() => LinkOutputKind::WasiReactorExe,
c295e0f8
XL
1526 (CrateType::Executable, false, RelocModel::Pic | RelocModel::Pie) => {
1527 LinkOutputKind::DynamicPicExe
1528 }
f9f354fc 1529 (CrateType::Executable, false, _) => LinkOutputKind::DynamicNoPicExe,
c295e0f8
XL
1530 (CrateType::Executable, true, RelocModel::Pic | RelocModel::Pie) => {
1531 LinkOutputKind::StaticPicExe
1532 }
f9f354fc
XL
1533 (CrateType::Executable, true, _) => LinkOutputKind::StaticNoPicExe,
1534 (_, true, _) => LinkOutputKind::StaticDylib,
1535 (_, false, _) => LinkOutputKind::DynamicDylib,
ba9703b0 1536 };
f9f354fc
XL
1537
1538 // Adjust the output kind to target capabilities.
29967ef6 1539 let opts = &sess.target;
f9f354fc
XL
1540 let pic_exe_supported = opts.position_independent_executables;
1541 let static_pic_exe_supported = opts.static_position_independent_executables;
1542 let static_dylib_supported = opts.crt_static_allows_dylibs;
1543 match kind {
1544 LinkOutputKind::DynamicPicExe if !pic_exe_supported => LinkOutputKind::DynamicNoPicExe,
1545 LinkOutputKind::StaticPicExe if !static_pic_exe_supported => LinkOutputKind::StaticNoPicExe,
1546 LinkOutputKind::StaticDylib if !static_dylib_supported => LinkOutputKind::DynamicDylib,
1547 _ => kind,
ba9703b0 1548 }
f9f354fc 1549}
ba9703b0 1550
1b1a35ee
XL
1551// Returns true if linker is located within sysroot
1552fn detect_self_contained_mingw(sess: &Session) -> bool {
1553 let (linker, _) = linker_and_flavor(&sess);
1554 // Assume `-C linker=rust-lld` as self-contained mode
1555 if linker == Path::new("rust-lld") {
1556 return true;
1557 }
1558 let linker_with_extension = if cfg!(windows) && linker.extension().is_none() {
1559 linker.with_extension("exe")
1560 } else {
1561 linker
1562 };
1563 for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
1564 let full_path = dir.join(&linker_with_extension);
1565 // If linker comes from sysroot assume self-contained mode
1566 if full_path.is_file() && !full_path.starts_with(&sess.sysroot) {
1567 return false;
1568 }
1569 }
1570 true
1571}
1572
f2b60f7d
FG
1573/// Various toolchain components used during linking are used from rustc distribution
1574/// instead of being found somewhere on the host system.
f9f354fc 1575/// We only provide such support for a very limited number of targets.
f2b60f7d 1576fn self_contained(sess: &Session, crate_type: CrateType) -> bool {
1b1a35ee 1577 if let Some(self_contained) = sess.opts.cg.link_self_contained {
f035d41b
XL
1578 return self_contained;
1579 }
1580
f2b60f7d
FG
1581 match sess.target.link_self_contained {
1582 LinkSelfContainedDefault::False => false,
1583 LinkSelfContainedDefault::True => true,
f9f354fc
XL
1584 // FIXME: Find a better heuristic for "native musl toolchain is available",
1585 // based on host and linker path, for example.
1586 // (https://github.com/rust-lang/rust/pull/71769#issuecomment-626330237).
f2b60f7d
FG
1587 LinkSelfContainedDefault::Musl => sess.crt_static(Some(crate_type)),
1588 LinkSelfContainedDefault::Mingw => {
29967ef6
XL
1589 sess.host == sess.target
1590 && sess.target.vendor != "uwp"
1b1a35ee 1591 && detect_self_contained_mingw(&sess)
3dfed10e 1592 }
ba9703b0
XL
1593 }
1594}
1595
f9f354fc
XL
1596/// Add pre-link object files defined by the target spec.
1597fn add_pre_link_objects(
1598 cmd: &mut dyn Linker,
1599 sess: &Session,
f2b60f7d 1600 flavor: LinkerFlavor,
f9f354fc 1601 link_output_kind: LinkOutputKind,
f035d41b 1602 self_contained: bool,
f9f354fc 1603) {
f2b60f7d
FG
1604 // FIXME: we are currently missing some infra here (per-linker-flavor CRT objects),
1605 // so Fuchsia has to be special-cased.
29967ef6 1606 let opts = &sess.target;
f2b60f7d
FG
1607 let empty = Default::default();
1608 let objects = if self_contained {
1609 &opts.pre_link_objects_self_contained
1610 } else if !(sess.target.os == "fuchsia" && flavor == LinkerFlavor::Gcc) {
1611 &opts.pre_link_objects
1612 } else {
1613 &empty
1614 };
f9f354fc 1615 for obj in objects.get(&link_output_kind).iter().copied().flatten() {
f035d41b 1616 cmd.add_object(&get_object_file_path(sess, obj, self_contained));
ba9703b0 1617 }
f9f354fc
XL
1618}
1619
1620/// Add post-link object files defined by the target spec.
1621fn add_post_link_objects(
1622 cmd: &mut dyn Linker,
1623 sess: &Session,
1624 link_output_kind: LinkOutputKind,
f035d41b 1625 self_contained: bool,
f9f354fc 1626) {
f2b60f7d
FG
1627 let objects = if self_contained {
1628 &sess.target.post_link_objects_self_contained
1629 } else {
1630 &sess.target.post_link_objects
1631 };
f9f354fc 1632 for obj in objects.get(&link_output_kind).iter().copied().flatten() {
f035d41b 1633 cmd.add_object(&get_object_file_path(sess, obj, self_contained));
ba9703b0
XL
1634 }
1635}
1636
1637/// Add arbitrary "pre-link" args defined by the target spec or from command line.
1638/// FIXME: Determine where exactly these args need to be inserted.
f035d41b 1639fn add_pre_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
29967ef6 1640 if let Some(args) = sess.target.pre_link_args.get(&flavor) {
5e7ed085 1641 cmd.args(args.iter().map(Deref::deref));
ba9703b0 1642 }
064997fb 1643 cmd.args(&sess.opts.unstable_opts.pre_link_args);
ba9703b0 1644}
48663c56 1645
f9f354fc
XL
1646/// Add a link script embedded in the target, if applicable.
1647fn add_link_script(cmd: &mut dyn Linker, sess: &Session, tmpdir: &Path, crate_type: CrateType) {
29967ef6 1648 match (crate_type, &sess.target.link_script) {
f9f354fc 1649 (CrateType::Cdylib | CrateType::Executable, Some(script)) => {
29967ef6 1650 if !sess.target.linker_is_gnu {
f9f354fc
XL
1651 sess.fatal("can only use link script when linking with GNU-like linker");
1652 }
1653
29967ef6 1654 let file_name = ["rustc", &sess.target.llvm_target, "linkfile.ld"].join("-");
f9f354fc
XL
1655
1656 let path = tmpdir.join(file_name);
5e7ed085 1657 if let Err(e) = fs::write(&path, script.as_ref()) {
f9f354fc
XL
1658 sess.fatal(&format!("failed to write link script to {}: {}", path.display(), e));
1659 }
1660
1661 cmd.arg("--script");
1662 cmd.arg(path);
1663 }
1664 _ => {}
1665 }
1666}
1667
cdc7bbd5 1668/// Add arbitrary "user defined" args defined from command line.
ba9703b0 1669/// FIXME: Determine where exactly these args need to be inserted.
cdc7bbd5 1670fn add_user_defined_link_args(cmd: &mut dyn Linker, sess: &Session) {
ba9703b0 1671 cmd.args(&sess.opts.cg.link_args);
ba9703b0 1672}
48663c56 1673
ba9703b0
XL
1674/// Add arbitrary "late link" args defined by the target spec.
1675/// FIXME: Determine where exactly these args need to be inserted.
1676fn add_late_link_args(
1677 cmd: &mut dyn Linker,
1678 sess: &Session,
1679 flavor: LinkerFlavor,
f9f354fc 1680 crate_type: CrateType,
ba9703b0
XL
1681 codegen_results: &CodegenResults,
1682) {
f9f354fc 1683 let any_dynamic_crate = crate_type == CrateType::Dylib
ba9703b0
XL
1684 || codegen_results.crate_info.dependency_formats.iter().any(|(ty, list)| {
1685 *ty == crate_type && list.iter().any(|&linkage| linkage == Linkage::Dynamic)
1686 });
1687 if any_dynamic_crate {
29967ef6 1688 if let Some(args) = sess.target.late_link_args_dynamic.get(&flavor) {
5e7ed085 1689 cmd.args(args.iter().map(Deref::deref));
ba9703b0
XL
1690 }
1691 } else {
29967ef6 1692 if let Some(args) = sess.target.late_link_args_static.get(&flavor) {
5e7ed085 1693 cmd.args(args.iter().map(Deref::deref));
74b04a01
XL
1694 }
1695 }
29967ef6 1696 if let Some(args) = sess.target.late_link_args.get(&flavor) {
5e7ed085 1697 cmd.args(args.iter().map(Deref::deref));
1b1a35ee 1698 }
ba9703b0 1699}
74b04a01 1700
ba9703b0
XL
1701/// Add arbitrary "post-link" args defined by the target spec.
1702/// FIXME: Determine where exactly these args need to be inserted.
1703fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
29967ef6 1704 if let Some(args) = sess.target.post_link_args.get(&flavor) {
5e7ed085 1705 cmd.args(args.iter().map(Deref::deref));
ba9703b0
XL
1706 }
1707}
e1599b0c 1708
04454e1e
FG
1709/// Add a synthetic object file that contains reference to all symbols that we want to expose to
1710/// the linker.
1711///
1712/// Background: we implement rlibs as static library (archives). Linkers treat archives
1713/// differently from object files: all object files participate in linking, while archives will
1714/// only participate in linking if they can satisfy at least one undefined reference (version
1715/// scripts doesn't count). This causes `#[no_mangle]` or `#[used]` items to be ignored by the
1716/// linker, and since they never participate in the linking, using `KEEP` in the linker scripts
1717/// can't keep them either. This causes #47384.
1718///
1719/// To keep them around, we could use `--whole-archive` and equivalents to force rlib to
1720/// participate in linking like object files, but this proves to be expensive (#93791). Therefore
1721/// we instead just introduce an undefined reference to them. This could be done by `-u` command
1722/// line option to the linker or `EXTERN(...)` in linker scripts, however they does not only
1723/// introduce an undefined reference, but also make them the GC roots, preventing `--gc-sections`
1724/// from removing them, and this is especially problematic for embedded programming where every
1725/// byte counts.
1726///
1727/// This method creates a synthetic object file, which contains undefined references to all symbols
1728/// that are necessary for the linking. They are only present in symbol table but not actually
1729/// used in any sections, so the linker will therefore pick relevant rlibs for linking, but
1730/// unused `#[no_mangle]` or `#[used]` can still be discard by GC sections.
f2b60f7d
FG
1731///
1732/// There's a few internal crates in the standard library (aka libcore and
1733/// libstd) which actually have a circular dependence upon one another. This
1734/// currently arises through "weak lang items" where libcore requires things
1735/// like `rust_begin_unwind` but libstd ends up defining it. To get this
1736/// circular dependence to work correctly we declare some of these things
1737/// in this synthetic object.
04454e1e
FG
1738fn add_linked_symbol_object(
1739 cmd: &mut dyn Linker,
1740 sess: &Session,
1741 tmpdir: &Path,
1742 symbols: &[(String, SymbolExportKind)],
1743) {
1744 if symbols.is_empty() {
1745 return;
1746 }
1747
1748 let Some(mut file) = super::metadata::create_object_file(sess) else {
1749 return;
1750 };
1751
1752 // NOTE(nbdd0121): MSVC will hang if the input object file contains no sections,
1753 // so add an empty section.
1754 if file.format() == object::BinaryFormat::Coff {
1755 file.add_section(Vec::new(), ".text".into(), object::SectionKind::Text);
1756
1757 // We handle the name decoration of COFF targets in `symbol_export.rs`, so disable the
1758 // default mangler in `object` crate.
1759 file.set_mangling(object::write::Mangling::None);
1760
1761 // Add feature flags to the object file. On MSVC this is optional but LLD will complain if
1762 // not present.
1763 let mut feature = 0;
1764
1765 if file.architecture() == object::Architecture::I386 {
1766 // Indicate that all SEH handlers are registered in .sxdata section.
1767 // We don't have generate any code, so we don't need .sxdata section but LLD still
1768 // expects us to set this bit (see #96498).
1769 // Reference: https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
1770 feature |= 1;
1771 }
1772
1773 file.add_symbol(object::write::Symbol {
1774 name: "@feat.00".into(),
1775 value: feature,
1776 size: 0,
1777 kind: object::SymbolKind::Data,
1778 scope: object::SymbolScope::Compilation,
1779 weak: false,
1780 section: object::write::SymbolSection::Absolute,
1781 flags: object::SymbolFlags::None,
1782 });
1783 }
1784
1785 for (sym, kind) in symbols.iter() {
1786 file.add_symbol(object::write::Symbol {
1787 name: sym.clone().into(),
1788 value: 0,
1789 size: 0,
1790 kind: match kind {
1791 SymbolExportKind::Text => object::SymbolKind::Text,
1792 SymbolExportKind::Data => object::SymbolKind::Data,
1793 SymbolExportKind::Tls => object::SymbolKind::Tls,
1794 },
1795 scope: object::SymbolScope::Unknown,
1796 weak: false,
1797 section: object::write::SymbolSection::Undefined,
1798 flags: object::SymbolFlags::None,
1799 });
1800 }
1801
1802 let path = tmpdir.join("symbols.o");
1803 let result = std::fs::write(&path, file.write().unwrap());
1804 if let Err(e) = result {
1805 sess.fatal(&format!("failed to write {}: {}", path.display(), e));
1806 }
1807 cmd.add_object(&path);
1808}
1809
ba9703b0
XL
1810/// Add object files containing code from the current crate.
1811fn add_local_crate_regular_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) {
48663c56
XL
1812 for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) {
1813 cmd.add_object(obj);
1814 }
ba9703b0 1815}
48663c56 1816
ba9703b0
XL
1817/// Add object files for allocator code linked once for the whole crate tree.
1818fn add_local_crate_allocator_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) {
1819 if let Some(obj) = codegen_results.allocator_module.as_ref().and_then(|m| m.object.as_ref()) {
1820 cmd.add_object(obj);
48663c56 1821 }
ba9703b0 1822}
48663c56 1823
ba9703b0
XL
1824/// Add object files containing metadata for the current crate.
1825fn add_local_crate_metadata_objects(
1826 cmd: &mut dyn Linker,
f9f354fc 1827 crate_type: CrateType,
ba9703b0
XL
1828 codegen_results: &CodegenResults,
1829) {
48663c56
XL
1830 // When linking a dynamic library, we put the metadata into a section of the
1831 // executable. This metadata is in a separate object file from the main
1832 // object file, so we link that in here.
f9f354fc 1833 if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
ba9703b0
XL
1834 if let Some(obj) = codegen_results.metadata_module.as_ref().and_then(|m| m.object.as_ref())
1835 {
48663c56
XL
1836 cmd.add_object(obj);
1837 }
1838 }
ba9703b0 1839}
48663c56 1840
ba9703b0 1841/// Add sysroot and other globally set directories to the directory search list.
f035d41b 1842fn add_library_search_dirs(cmd: &mut dyn Linker, sess: &Session, self_contained: bool) {
ba9703b0
XL
1843 // The default library location, we need this to find the runtime.
1844 // The location of crates will be determined as needed.
1845 let lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
1846 cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
f035d41b
XL
1847
1848 // Special directory with libraries used only in self-contained linkage mode
1849 if self_contained {
1850 let lib_path = sess.target_filesearch(PathKind::All).get_self_contained_lib_path();
1851 cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
1852 }
ba9703b0
XL
1853}
1854
ba9703b0
XL
1855/// Add options making relocation sections in the produced ELF files read-only
1856/// and suppressing lazy binding.
1857fn add_relro_args(cmd: &mut dyn Linker, sess: &Session) {
064997fb 1858 match sess.opts.unstable_opts.relro_level.unwrap_or(sess.target.relro_level) {
ba9703b0
XL
1859 RelroLevel::Full => cmd.full_relro(),
1860 RelroLevel::Partial => cmd.partial_relro(),
1861 RelroLevel::Off => cmd.no_relro(),
1862 RelroLevel::None => {}
74b04a01 1863 }
ba9703b0 1864}
74b04a01 1865
ba9703b0
XL
1866/// Add library search paths used at runtime by dynamic linkers.
1867fn add_rpath_args(
1868 cmd: &mut dyn Linker,
1869 sess: &Session,
1870 codegen_results: &CodegenResults,
1871 out_filename: &Path,
1872) {
48663c56
XL
1873 // FIXME (#2397): At some point we want to rpath our guesses as to
1874 // where extern libraries might live, based on the
17df50a5 1875 // add_lib_search_paths
48663c56 1876 if sess.opts.cg.rpath {
136023e0
XL
1877 let libs = codegen_results
1878 .crate_info
1879 .used_crates
1880 .iter()
1881 .filter_map(|cnum| {
1882 codegen_results.crate_info.used_crate_source[cnum]
1883 .dylib
1884 .as_ref()
1885 .map(|(path, _)| &**path)
1886 })
1887 .collect::<Vec<_>>();
48663c56 1888 let mut rpath_config = RPathConfig {
136023e0 1889 libs: &*libs,
48663c56 1890 out_filename: out_filename.to_path_buf(),
29967ef6
XL
1891 has_rpath: sess.target.has_rpath,
1892 is_like_osx: sess.target.is_like_osx,
1893 linker_is_gnu: sess.target.linker_is_gnu,
48663c56
XL
1894 };
1895 cmd.args(&rpath::get_rpath_flags(&mut rpath_config));
1896 }
ba9703b0 1897}
48663c56 1898
ba9703b0 1899/// Produce the linker command line containing linker path and arguments.
17df50a5
XL
1900///
1901/// When comments in the function say "order-(in)dependent" they mean order-dependence between
1902/// options and libraries/object files. For example `--whole-archive` (order-dependent) applies
1903/// to specific libraries passed after it, and `-o` (output file, order-independent) applies
1904/// to the linking process as a whole.
1905/// Order-independent options may still override each other in order-dependent fashion,
1906/// e.g `--foo=yes --foo=no` may be equivalent to `--foo=no`.
064997fb 1907fn linker_with_args<'a>(
ba9703b0
XL
1908 path: &Path,
1909 flavor: LinkerFlavor,
1910 sess: &'a Session,
064997fb 1911 archive_builder_builder: &dyn ArchiveBuilderBuilder,
f9f354fc 1912 crate_type: CrateType,
ba9703b0
XL
1913 tmpdir: &Path,
1914 out_filename: &Path,
1915 codegen_results: &CodegenResults,
064997fb 1916) -> Result<Command, ErrorGuaranteed> {
f2b60f7d 1917 let self_contained = self_contained(sess, crate_type);
136023e0
XL
1918 let cmd = &mut *super::linker::get_linker(
1919 sess,
1920 path,
1921 flavor,
f2b60f7d 1922 self_contained,
136023e0
XL
1923 &codegen_results.crate_info.target_cpu,
1924 );
f9f354fc 1925 let link_output_kind = link_output_kind(sess, crate_type);
ba9703b0 1926
17df50a5
XL
1927 // ------------ Early order-dependent options ------------
1928
1929 // If we're building something like a dynamic library then some platforms
1930 // need to make sure that all symbols are exported correctly from the
1931 // dynamic library.
1932 // Must be passed before any libraries to prevent the symbols to export from being thrown away,
1933 // at least on some platforms (e.g. windows-gnu).
136023e0
XL
1934 cmd.export_symbols(
1935 tmpdir,
1936 crate_type,
1937 &codegen_results.crate_info.exported_symbols[&crate_type],
1938 );
17df50a5
XL
1939
1940 // Can be used for adding custom CRT objects or overriding order-dependent options above.
1941 // FIXME: In practice built-in target specs use this for arbitrary order-independent options,
1942 // introduce a target spec option for order-independent linker options and migrate built-in
1943 // specs to it.
f035d41b 1944 add_pre_link_args(cmd, sess, flavor);
ba9703b0 1945
17df50a5
XL
1946 // ------------ Object code and libraries, order-dependent ------------
1947
1948 // Pre-link CRT objects.
f2b60f7d 1949 add_pre_link_objects(cmd, sess, flavor, link_output_kind, self_contained);
17df50a5 1950
04454e1e
FG
1951 add_linked_symbol_object(
1952 cmd,
1953 sess,
1954 tmpdir,
1955 &codegen_results.crate_info.linked_symbols[&crate_type],
1956 );
1957
17df50a5
XL
1958 // Sanitizer libraries.
1959 add_sanitizer_libraries(sess, crate_type, cmd);
1960
1961 // Object code from the current crate.
1962 // Take careful note of the ordering of the arguments we pass to the linker
1963 // here. Linkers will assume that things on the left depend on things to the
1964 // right. Things on the right cannot depend on things on the left. This is
1965 // all formally implemented in terms of resolving symbols (libs on the right
1966 // resolve unknown symbols of libs on the left, but not vice versa).
1967 //
1968 // For this reason, we have organized the arguments we pass to the linker as
1969 // such:
1970 //
1971 // 1. The local object that LLVM just generated
1972 // 2. Local native libraries
1973 // 3. Upstream rust libraries
1974 // 4. Upstream native libraries
1975 //
1976 // The rationale behind this ordering is that those items lower down in the
1977 // list can't depend on items higher up in the list. For example nothing can
1978 // depend on what we just generated (e.g., that'd be a circular dependency).
1979 // Upstream rust libraries are not supposed to depend on our local native
1980 // libraries as that would violate the structure of the DAG, in that
1981 // scenario they are required to link to them as well in a shared fashion.
17df50a5
XL
1982 //
1983 // Note that upstream rust libraries may contain native dependencies as
1984 // well, but they also can't depend on what we just started to add to the
1985 // link line. And finally upstream native libraries can't depend on anything
1986 // in this DAG so far because they can only depend on other native libraries
1987 // and such dependencies are also required to be specified.
1988 add_local_crate_regular_objects(cmd, codegen_results);
1989 add_local_crate_metadata_objects(cmd, crate_type, codegen_results);
1990 add_local_crate_allocator_objects(cmd, codegen_results);
1991
1992 // Avoid linking to dynamic libraries unless they satisfy some undefined symbols
1993 // at the point at which they are specified on the command line.
1994 // Must be passed before any (dynamic) libraries to have effect on them.
1995 // On Solaris-like systems, `-z ignore` acts as both `--as-needed` and `--gc-sections`
1996 // so it will ignore unreferenced ELF sections from relocatable objects.
1997 // For that reason, we put this flag after metadata objects as they would otherwise be removed.
1998 // FIXME: Support more fine-grained dead code removal on Solaris/illumos
1999 // and move this option back to the top.
2000 cmd.add_as_needed();
2001
f2b60f7d
FG
2002 // Local native libraries of all kinds.
2003 //
2004 // If `-Zlink-native-libraries=false` is set, then the assumption is that an
2005 // external build system already has the native dependencies defined, and it
2006 // will provide them to the linker itself.
064997fb 2007 if sess.opts.unstable_opts.link_native_libraries {
04454e1e 2008 add_local_native_libraries(cmd, sess, codegen_results);
17df50a5
XL
2009 }
2010
f2b60f7d 2011 // Upstream rust libraries and their (possibly bundled) static native libraries.
064997fb
FG
2012 add_upstream_rust_crates(
2013 cmd,
2014 sess,
2015 archive_builder_builder,
2016 codegen_results,
2017 crate_type,
2018 tmpdir,
2019 );
17df50a5 2020
f2b60f7d
FG
2021 // Dynamic native libraries from upstream crates.
2022 //
2023 // FIXME: Merge this to `add_upstream_rust_crates` so that all native libraries are linked
2024 // together with their respective upstream crates, and in their originally specified order.
2025 // This may be slightly breaking due to our use of `--as-needed` and needs a crater run.
064997fb 2026 if sess.opts.unstable_opts.link_native_libraries {
94222f64 2027 add_upstream_native_libraries(cmd, sess, codegen_results);
17df50a5
XL
2028 }
2029
064997fb
FG
2030 // Link with the import library generated for any raw-dylib functions.
2031 for (raw_dylib_name, raw_dylib_imports) in
f2b60f7d 2032 collate_raw_dylibs(sess, codegen_results.crate_info.used_libraries.iter())?
064997fb
FG
2033 {
2034 cmd.add_object(&archive_builder_builder.create_dll_import_lib(
2035 sess,
2036 &raw_dylib_name,
2037 &raw_dylib_imports,
2038 tmpdir,
f2b60f7d
FG
2039 true,
2040 ));
2041 }
2042 // As with add_upstream_native_libraries, we need to add the upstream raw-dylib symbols in case
2043 // they are used within inlined functions or instantiated generic functions. We do this *after*
2044 // handling the raw-dylib symbols in the current crate to make sure that those are chosen first
2045 // by the linker.
2046 let (_, dependency_linkage) = codegen_results
2047 .crate_info
2048 .dependency_formats
2049 .iter()
2050 .find(|(ty, _)| *ty == crate_type)
2051 .expect("failed to find crate type in dependency format list");
2052 let native_libraries_from_nonstatics = codegen_results
2053 .crate_info
2054 .native_libraries
2055 .iter()
2056 .filter_map(|(cnum, libraries)| {
2057 (dependency_linkage[cnum.as_usize() - 1] != Linkage::Static).then(|| libraries)
2058 })
2059 .flatten();
2060 for (raw_dylib_name, raw_dylib_imports) in
2061 collate_raw_dylibs(sess, native_libraries_from_nonstatics)?
2062 {
2063 cmd.add_object(&archive_builder_builder.create_dll_import_lib(
2064 sess,
2065 &raw_dylib_name,
2066 &raw_dylib_imports,
2067 tmpdir,
2068 false,
064997fb
FG
2069 ));
2070 }
2071
17df50a5
XL
2072 // Library linking above uses some global state for things like `-Bstatic`/`-Bdynamic` to make
2073 // command line shorter, reset it to default here before adding more libraries.
2074 cmd.reset_per_library_state();
2075
2076 // FIXME: Built-in target specs occasionally use this for linking system libraries,
2077 // eliminate all such uses by migrating them to `#[link]` attributes in `lib(std,c,unwind)`
2078 // and remove the option.
2079 add_late_link_args(cmd, sess, flavor, crate_type, codegen_results);
2080
2081 // ------------ Arbitrary order-independent options ------------
2082
2083 // Add order-independent options determined by rustc from its compiler options,
2084 // target properties and source code.
2085 add_order_independent_options(
2086 cmd,
2087 sess,
2088 link_output_kind,
f2b60f7d 2089 self_contained,
17df50a5
XL
2090 flavor,
2091 crate_type,
2092 codegen_results,
2093 out_filename,
2094 tmpdir,
2095 );
2096
2097 // Can be used for arbitrary order-independent options.
2098 // In practice may also be occasionally used for linking native libraries.
2099 // Passed after compiler-generated options to support manual overriding when necessary.
2100 add_user_defined_link_args(cmd, sess);
2101
2102 // ------------ Object code and libraries, order-dependent ------------
2103
2104 // Post-link CRT objects.
f2b60f7d 2105 add_post_link_objects(cmd, sess, link_output_kind, self_contained);
17df50a5
XL
2106
2107 // ------------ Late order-dependent options ------------
2108
2109 // Doesn't really make sense.
2110 // FIXME: In practice built-in target specs use this for arbitrary order-independent options,
2111 // introduce a target spec option for order-independent linker options, migrate built-in specs
2112 // to it and remove the option.
2113 add_post_link_args(cmd, sess, flavor);
2114
064997fb 2115 Ok(cmd.take_cmd())
17df50a5
XL
2116}
2117
2118fn add_order_independent_options(
2119 cmd: &mut dyn Linker,
2120 sess: &Session,
2121 link_output_kind: LinkOutputKind,
f2b60f7d 2122 self_contained: bool,
17df50a5
XL
2123 flavor: LinkerFlavor,
2124 crate_type: CrateType,
2125 codegen_results: &CodegenResults,
2126 out_filename: &Path,
2127 tmpdir: &Path,
2128) {
2129 add_gcc_ld_path(cmd, sess, flavor);
2130
1b1a35ee
XL
2131 add_apple_sdk(cmd, sess, flavor);
2132
f9f354fc
XL
2133 add_link_script(cmd, sess, tmpdir, crate_type);
2134
f2b60f7d
FG
2135 if sess.target.os == "fuchsia"
2136 && crate_type == CrateType::Executable
2137 && flavor != LinkerFlavor::Gcc
2138 {
064997fb 2139 let prefix = if sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::ADDRESS) {
f035d41b
XL
2140 "asan/"
2141 } else {
2142 ""
ba9703b0
XL
2143 };
2144 cmd.arg(format!("--dynamic-linker={}ld.so.1", prefix));
2145 }
2146
29967ef6 2147 if sess.target.eh_frame_header {
f9652781
XL
2148 cmd.add_eh_frame_header();
2149 }
f035d41b 2150
cdc7bbd5
XL
2151 // Make the binary compatible with data execution prevention schemes.
2152 cmd.add_no_exec();
2153
f2b60f7d 2154 if self_contained {
f9f354fc
XL
2155 cmd.no_crt_objects();
2156 }
2157
923072b8 2158 if sess.target.os == "emscripten" {
ba9703b0
XL
2159 cmd.arg("-s");
2160 cmd.arg(if sess.panic_strategy() == PanicStrategy::Abort {
2161 "DISABLE_EXCEPTION_CATCHING=1"
2162 } else {
2163 "DISABLE_EXCEPTION_CATCHING=0"
2164 });
2165 }
2166
f2b60f7d 2167 if flavor == LinkerFlavor::Ptx {
17df50a5
XL
2168 // Provide the linker with fallback to internal `target-cpu`.
2169 cmd.arg("--fallback-arch");
136023e0 2170 cmd.arg(&codegen_results.crate_info.target_cpu);
f2b60f7d 2171 } else if flavor == LinkerFlavor::Bpf {
17df50a5 2172 cmd.arg("--cpu");
136023e0 2173 cmd.arg(&codegen_results.crate_info.target_cpu);
17df50a5
XL
2174 cmd.arg("--cpu-features");
2175 cmd.arg(match &sess.opts.cg.target_feature {
5e7ed085
FG
2176 feat if !feat.is_empty() => feat.as_ref(),
2177 _ => sess.target.options.features.as_ref(),
17df50a5
XL
2178 });
2179 }
ba9703b0 2180
ba9703b0
XL
2181 cmd.linker_plugin_lto();
2182
f2b60f7d 2183 add_library_search_dirs(cmd, sess, self_contained);
ba9703b0 2184
ba9703b0
XL
2185 cmd.output_filename(out_filename);
2186
29967ef6 2187 if crate_type == CrateType::Executable && sess.target.is_like_windows {
17df50a5 2188 if let Some(ref s) = codegen_results.crate_info.windows_subsystem {
ba9703b0
XL
2189 cmd.subsystem(s);
2190 }
2191 }
2192
ba9703b0
XL
2193 // Try to strip as much out of the generated object by removing unused
2194 // sections if possible. See more comments in linker.rs
1b1a35ee 2195 if !sess.link_dead_code() {
136023e0
XL
2196 // If PGO is enabled sometimes gc_sections will remove the profile data section
2197 // as it appears to be unused. This can then cause the PGO profile file to lose
2198 // some functions. If we are generating a profile we shouldn't strip those metadata
2199 // sections to ensure we have all the data for PGO.
2200 let keep_metadata =
2201 crate_type == CrateType::Dylib || sess.opts.cg.profile_generate.enabled();
064997fb
FG
2202 if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
2203 {
2204 cmd.gc_sections(keep_metadata);
2205 } else {
2206 cmd.no_gc_sections();
2207 }
ba9703b0
XL
2208 }
2209
f9f354fc 2210 cmd.set_output_kind(link_output_kind, out_filename);
ba9703b0 2211
ba9703b0
XL
2212 add_relro_args(cmd, sess);
2213
ba9703b0
XL
2214 // Pass optimization flags down to the linker.
2215 cmd.optimize();
2216
923072b8
FG
2217 // Gather the set of NatVis files, if any, and write them out to a temp directory.
2218 let natvis_visualizers = collect_natvis_visualizers(
2219 tmpdir,
2220 sess,
2221 &codegen_results.crate_info.local_crate_name,
2222 &codegen_results.crate_info.natvis_debugger_visualizers,
2223 );
04454e1e 2224
923072b8
FG
2225 // Pass debuginfo, NatVis debugger visualizers and strip flags down to the linker.
2226 cmd.debuginfo(strip_value(sess), &natvis_visualizers);
ba9703b0 2227
ba9703b0
XL
2228 // We want to prevent the compiler from accidentally leaking in any system libraries,
2229 // so by default we tell linkers not to link to any default libraries.
29967ef6 2230 if !sess.opts.cg.default_linker_libraries && sess.target.no_default_libraries {
ba9703b0
XL
2231 cmd.no_default_libraries();
2232 }
2233
cdc7bbd5 2234 if sess.opts.cg.profile_generate.enabled() || sess.instrument_coverage() {
ba9703b0
XL
2235 cmd.pgo_gen();
2236 }
2237
3dfed10e 2238 if sess.opts.cg.control_flow_guard != CFGuard::Disabled {
ba9703b0
XL
2239 cmd.control_flow_guard();
2240 }
2241
ba9703b0 2242 add_rpath_args(cmd, sess, codegen_results, out_filename);
48663c56
XL
2243}
2244
923072b8
FG
2245// Write the NatVis debugger visualizer files for each crate to the temp directory and gather the file paths.
2246fn collect_natvis_visualizers(
04454e1e
FG
2247 tmpdir: &Path,
2248 sess: &Session,
923072b8
FG
2249 crate_name: &Symbol,
2250 natvis_debugger_visualizers: &BTreeSet<DebuggerVisualizerFile>,
04454e1e 2251) -> Vec<PathBuf> {
923072b8 2252 let mut visualizer_paths = Vec::with_capacity(natvis_debugger_visualizers.len());
04454e1e 2253
923072b8
FG
2254 for (index, visualizer) in natvis_debugger_visualizers.iter().enumerate() {
2255 let visualizer_out_file = tmpdir.join(format!("{}-{}.natvis", crate_name.as_str(), index));
04454e1e 2256
923072b8
FG
2257 match fs::write(&visualizer_out_file, &visualizer.src) {
2258 Ok(()) => {
2259 visualizer_paths.push(visualizer_out_file);
2260 }
2261 Err(error) => {
2262 sess.warn(
2263 format!(
2264 "Unable to write debugger visualizer file `{}`: {} ",
2265 visualizer_out_file.display(),
2266 error
2267 )
2268 .as_str(),
2269 );
2270 }
2271 };
04454e1e
FG
2272 }
2273 visualizer_paths
5e7ed085
FG
2274}
2275
fc512014
XL
2276/// # Native library linking
2277///
2278/// User-supplied library search paths (-L on the command line). These are the same paths used to
2279/// find Rust crates, so some of them may have been added already by the previous crate linking
2280/// code. This only allows them to be found at compile time so it is still entirely up to outside
2281/// forces to make sure that library can be found at runtime.
2282///
2283/// Also note that the native libraries linked here are only the ones located in the current crate.
2284/// Upstream crates with native library dependencies may have their native library pulled in above.
ba9703b0 2285fn add_local_native_libraries(
dfeec247
XL
2286 cmd: &mut dyn Linker,
2287 sess: &Session,
2288 codegen_results: &CodegenResults,
2289) {
48663c56
XL
2290 let filesearch = sess.target_filesearch(PathKind::All);
2291 for search_path in filesearch.search_paths() {
2292 match search_path.kind {
dfeec247
XL
2293 PathKind::Framework => {
2294 cmd.framework_path(&search_path.dir);
2295 }
2296 _ => {
2297 cmd.include_path(&fix_windows_verbatim_for_gcc(&search_path.dir));
2298 }
48663c56
XL
2299 }
2300 }
2301
dfeec247
XL
2302 let relevant_libs =
2303 codegen_results.crate_info.used_libraries.iter().filter(|l| relevant_lib(sess, l));
48663c56 2304
c295e0f8 2305 let search_path = OnceCell::new();
5e7ed085 2306 let mut last = (None, NativeLibKind::Unspecified, None);
48663c56 2307 for lib in relevant_libs {
5e7ed085
FG
2308 let Some(name) = lib.name else {
2309 continue;
48663c56 2310 };
064997fb 2311 let name = name.as_str();
17df50a5
XL
2312
2313 // Skip if this library is the same as the last.
5e7ed085
FG
2314 last = if (lib.name, lib.kind, lib.verbatim) == last {
2315 continue;
2316 } else {
2317 (lib.name, lib.kind, lib.verbatim)
2318 };
17df50a5
XL
2319
2320 let verbatim = lib.verbatim.unwrap_or(false);
48663c56 2321 match lib.kind {
17df50a5
XL
2322 NativeLibKind::Dylib { as_needed } => {
2323 cmd.link_dylib(name, verbatim, as_needed.unwrap_or(true))
2324 }
2325 NativeLibKind::Unspecified => cmd.link_dylib(name, verbatim, true),
2326 NativeLibKind::Framework { as_needed } => {
2327 cmd.link_framework(name, as_needed.unwrap_or(true))
2328 }
5e7ed085
FG
2329 NativeLibKind::Static { whole_archive, bundle, .. } => {
2330 if whole_archive == Some(true)
5e7ed085
FG
2331 // Backward compatibility case: this can be a rlib (so `+whole-archive` cannot
2332 // be added explicitly if necessary, see the error in `fn link_rlib`) compiled
2333 // as an executable due to `--test`. Use whole-archive implicitly, like before
2334 // the introduction of native lib modifiers.
923072b8 2335 || (whole_archive == None && bundle != Some(false) && sess.opts.test)
5e7ed085
FG
2336 {
2337 cmd.link_whole_staticlib(
2338 name,
2339 verbatim,
2340 &search_path.get_or_init(|| archive_search_paths(sess)),
2341 );
2342 } else {
2343 cmd.link_staticlib(name, verbatim)
2344 }
17df50a5 2345 }
f9f354fc 2346 NativeLibKind::RawDylib => {
064997fb
FG
2347 // Ignore RawDylib here, they are handled separately in linker_with_args().
2348 }
2349 NativeLibKind::LinkArg => {
2350 cmd.arg(name);
dfeec247 2351 }
48663c56
XL
2352 }
2353 }
2354}
2355
923072b8 2356/// # Linking Rust crates and their non-bundled static libraries
fc512014
XL
2357///
2358/// Rust crates are not considered at all when creating an rlib output. All dependencies will be
2359/// linked when producing the final output (instead of the intermediate rlib version).
064997fb 2360fn add_upstream_rust_crates<'a>(
e74abb32
XL
2361 cmd: &mut dyn Linker,
2362 sess: &'a Session,
064997fb 2363 archive_builder_builder: &dyn ArchiveBuilderBuilder,
e74abb32 2364 codegen_results: &CodegenResults,
f9f354fc 2365 crate_type: CrateType,
e74abb32
XL
2366 tmpdir: &Path,
2367) {
48663c56
XL
2368 // All of the heavy lifting has previously been accomplished by the
2369 // dependency_format module of the compiler. This is just crawling the
2370 // output of that module, adding crates as necessary.
2371 //
2372 // Linking to a rlib involves just passing it to the linker (the linker
2373 // will slurp up the object files inside), and linking to a dynamic library
2374 // involves just passing the right -l flag.
2375
dfeec247
XL
2376 let (_, data) = codegen_results
2377 .crate_info
2378 .dependency_formats
e74abb32
XL
2379 .iter()
2380 .find(|(ty, _)| *ty == crate_type)
2381 .expect("failed to find crate type in dependency format list");
48663c56
XL
2382
2383 // Invoke get_used_crates to ensure that we get a topological sorting of
2384 // crates.
136023e0 2385 let deps = &codegen_results.crate_info.used_crates;
48663c56 2386
48663c56 2387 let mut compiler_builtins = None;
c295e0f8 2388 let search_path = OnceCell::new();
48663c56 2389
136023e0 2390 for &cnum in deps.iter() {
48663c56
XL
2391 // We may not pass all crates through to the linker. Some crates may
2392 // appear statically in an existing dylib, meaning we'll pick up all the
2393 // symbols from the dylib.
2394 let src = &codegen_results.crate_info.used_crate_source[&cnum];
2395 match data[cnum.as_usize() - 1] {
2396 _ if codegen_results.crate_info.profiler_runtime == Some(cnum) => {
f2b60f7d
FG
2397 add_static_crate(
2398 cmd,
2399 sess,
2400 archive_builder_builder,
2401 codegen_results,
2402 tmpdir,
2403 cnum,
2404 &Default::default(),
2405 );
48663c56 2406 }
48663c56
XL
2407 // compiler-builtins are always placed last to ensure that they're
2408 // linked correctly.
2409 _ if codegen_results.crate_info.compiler_builtins == Some(cnum) => {
2410 assert!(compiler_builtins.is_none());
2411 compiler_builtins = Some(cnum);
2412 }
dfeec247 2413 Linkage::NotLinked | Linkage::IncludedFromDylib => {}
48663c56 2414 Linkage::Static => {
f2b60f7d
FG
2415 let bundled_libs = if sess.opts.unstable_opts.packed_bundled_libs {
2416 codegen_results.crate_info.native_libraries[&cnum]
2417 .iter()
2418 .filter_map(|lib| lib.filename)
2419 .collect::<FxHashSet<_>>()
2420 } else {
2421 Default::default()
2422 };
2423 add_static_crate(
2424 cmd,
2425 sess,
2426 archive_builder_builder,
2427 codegen_results,
2428 tmpdir,
2429 cnum,
2430 &bundled_libs,
2431 );
94222f64
XL
2432
2433 // Link static native libs with "-bundle" modifier only if the crate they originate from
2434 // is being linked statically to the current crate. If it's linked dynamically
2435 // or is an rlib already included via some other dylib crate, the symbols from
2436 // native libs will have already been included in that dylib.
2437 //
f2b60f7d 2438 // If `-Zlink-native-libraries=false` is set, then the assumption is that an
94222f64
XL
2439 // external build system already has the native dependencies defined, and it
2440 // will provide them to the linker itself.
064997fb 2441 if sess.opts.unstable_opts.link_native_libraries {
f2b60f7d
FG
2442 if sess.opts.unstable_opts.packed_bundled_libs {
2443 // If rlib contains native libs as archives, unpack them to tmpdir.
2444 let rlib = &src.rlib.as_ref().unwrap().0;
2445 archive_builder_builder
2446 .extract_bundled_libs(rlib, tmpdir, &bundled_libs)
2447 .unwrap_or_else(|e| sess.fatal(e));
2448 }
2449
5e7ed085 2450 let mut last = (None, NativeLibKind::Unspecified, None);
94222f64 2451 for lib in &codegen_results.crate_info.native_libraries[&cnum] {
5e7ed085
FG
2452 let Some(name) = lib.name else {
2453 continue;
2454 };
064997fb 2455 let name = name.as_str();
c295e0f8 2456 if !relevant_lib(sess, lib) {
c295e0f8
XL
2457 continue;
2458 }
2459
2460 // Skip if this library is the same as the last.
5e7ed085 2461 last = if (lib.name, lib.kind, lib.verbatim) == last {
c295e0f8 2462 continue;
5e7ed085
FG
2463 } else {
2464 (lib.name, lib.kind, lib.verbatim)
2465 };
2466
064997fb
FG
2467 match lib.kind {
2468 NativeLibKind::Static {
2469 bundle: Some(false),
2470 whole_archive: Some(true),
2471 } => {
5e7ed085
FG
2472 cmd.link_whole_staticlib(
2473 name,
064997fb 2474 lib.verbatim.unwrap_or(false),
5e7ed085
FG
2475 search_path.get_or_init(|| archive_search_paths(sess)),
2476 );
c295e0f8 2477 }
064997fb
FG
2478 NativeLibKind::Static {
2479 bundle: Some(false),
2480 whole_archive: Some(false) | None,
2481 } => {
f2b60f7d
FG
2482 // HACK/FIXME: Fixup a circular dependency between libgcc and libc
2483 // with glibc. This logic should be moved to the libc crate.
2484 if sess.target.os == "linux"
2485 && sess.target.env == "gnu"
2486 && name == "c"
2487 {
2488 cmd.link_staticlib("gcc", false);
2489 }
064997fb
FG
2490 cmd.link_staticlib(name, lib.verbatim.unwrap_or(false));
2491 }
2492 NativeLibKind::LinkArg => {
2493 cmd.arg(name);
2494 }
2495 NativeLibKind::Dylib { .. }
2496 | NativeLibKind::Framework { .. }
2497 | NativeLibKind::Unspecified
2498 | NativeLibKind::RawDylib => {}
f2b60f7d
FG
2499 NativeLibKind::Static { bundle: Some(true) | None, whole_archive } => {
2500 if sess.opts.unstable_opts.packed_bundled_libs {
2501 // If rlib contains native libs as archives, they are unpacked to tmpdir.
2502 let path = tmpdir.join(lib.filename.unwrap().as_str());
2503 if whole_archive == Some(true) {
2504 cmd.link_whole_rlib(&path);
2505 } else {
2506 cmd.link_rlib(&path);
2507 }
2508 }
2509 }
94222f64
XL
2510 }
2511 }
2512 }
48663c56 2513 }
dfeec247 2514 Linkage::Dynamic => add_dynamic_crate(cmd, sess, &src.dylib.as_ref().unwrap().0),
48663c56 2515 }
48663c56
XL
2516 }
2517
2518 // compiler-builtins are always placed last to ensure that they're
2519 // linked correctly.
2520 // We must always link the `compiler_builtins` crate statically. Even if it
2521 // was already "included" in a dylib (e.g., `libstd` when `-C prefer-dynamic`
2522 // is used)
2523 if let Some(cnum) = compiler_builtins {
f2b60f7d
FG
2524 add_static_crate(
2525 cmd,
2526 sess,
2527 archive_builder_builder,
2528 codegen_results,
2529 tmpdir,
2530 cnum,
2531 &Default::default(),
2532 );
48663c56
XL
2533 }
2534
2535 // Converts a library file-stem into a cc -l argument
29967ef6
XL
2536 fn unlib<'a>(target: &Target, stem: &'a str) -> &'a str {
2537 if stem.starts_with("lib") && !target.is_like_windows { &stem[3..] } else { stem }
48663c56
XL
2538 }
2539
48663c56 2540 // Adds the static "rlib" versions of all crates to the command line.
17df50a5
XL
2541 // There's a bit of magic which happens here specifically related to LTO,
2542 // namely that we remove upstream object files.
48663c56
XL
2543 //
2544 // When performing LTO, almost(*) all of the bytecode from the upstream
2545 // libraries has already been included in our object file output. As a
2546 // result we need to remove the object files in the upstream libraries so
2547 // the linker doesn't try to include them twice (or whine about duplicate
2548 // symbols). We must continue to include the rest of the rlib, however, as
2549 // it may contain static native libraries which must be linked in.
2550 //
2551 // (*) Crates marked with `#![no_builtins]` don't participate in LTO and
2552 // their bytecode wasn't included. The object files in those libraries must
2553 // still be passed to the linker.
2554 //
17df50a5
XL
2555 // Note, however, that if we're not doing LTO we can just pass the rlib
2556 // blindly to the linker (fast) because it's fine if it's not actually
2557 // included as we're at the end of the dependency chain.
064997fb 2558 fn add_static_crate<'a>(
dfeec247
XL
2559 cmd: &mut dyn Linker,
2560 sess: &'a Session,
064997fb 2561 archive_builder_builder: &dyn ArchiveBuilderBuilder,
dfeec247
XL
2562 codegen_results: &CodegenResults,
2563 tmpdir: &Path,
dfeec247 2564 cnum: CrateNum,
f2b60f7d 2565 bundled_lib_file_names: &FxHashSet<Symbol>,
dfeec247 2566 ) {
48663c56
XL
2567 let src = &codegen_results.crate_info.used_crate_source[&cnum];
2568 let cratepath = &src.rlib.as_ref().unwrap().0;
2569
17df50a5 2570 let mut link_upstream = |path: &Path| {
04454e1e 2571 cmd.link_rlib(&fix_windows_verbatim_for_gcc(path));
17df50a5
XL
2572 };
2573
48663c56
XL
2574 // See the comment above in `link_staticlib` and `link_rlib` for why if
2575 // there's a static library that's not relevant we skip all object
2576 // files.
2577 let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
17df50a5
XL
2578 let skip_native = native_libs.iter().any(|lib| {
2579 matches!(lib.kind, NativeLibKind::Static { bundle: None | Some(true), .. })
2580 && !relevant_lib(sess, lib)
2581 });
dfeec247
XL
2582
2583 if (!are_upstream_rust_objects_already_included(sess)
2584 || ignored_for_lto(sess, &codegen_results.crate_info, cnum))
dfeec247
XL
2585 && !skip_native
2586 {
17df50a5 2587 link_upstream(cratepath);
dfeec247 2588 return;
48663c56
XL
2589 }
2590
2591 let dst = tmpdir.join(cratepath.file_name().unwrap());
2592 let name = cratepath.file_name().unwrap().to_str().unwrap();
2593 let name = &name[3..name.len() - 5]; // chop off lib/.rlib
f2b60f7d 2594 let bundled_lib_file_names = bundled_lib_file_names.clone();
48663c56 2595
74b04a01 2596 sess.prof.generic_activity_with_arg("link_altering_rlib", name).run(|| {
923072b8
FG
2597 let canonical_name = name.replace('-', "_");
2598 let upstream_rust_objects_already_included =
2599 are_upstream_rust_objects_already_included(sess);
2600 let is_builtins = sess.target.no_builtins
2601 || !codegen_results.crate_info.is_no_builtins.contains(&cnum);
2602
064997fb
FG
2603 let mut archive = archive_builder_builder.new_archive_builder(sess);
2604 if let Err(e) = archive.add_archive(
2605 cratepath,
2606 Box::new(move |f| {
2607 if f == METADATA_FILENAME {
2608 return true;
2609 }
48663c56 2610
064997fb 2611 let canonical = f.replace('-', "_");
48663c56 2612
064997fb
FG
2613 let is_rust_object =
2614 canonical.starts_with(&canonical_name) && looks_like_rust_object_file(&f);
48663c56 2615
064997fb
FG
2616 // If we've been requested to skip all native object files
2617 // (those not generated by the rust compiler) then we can skip
2618 // this file. See above for why we may want to do this.
2619 let skip_because_cfg_say_so = skip_native && !is_rust_object;
48663c56 2620
064997fb
FG
2621 // If we're performing LTO and this is a rust-generated object
2622 // file, then we don't need the object file as it's part of the
2623 // LTO module. Note that `#![no_builtins]` is excluded from LTO,
2624 // though, so we let that object file slide.
2625 let skip_because_lto =
2626 upstream_rust_objects_already_included && is_rust_object && is_builtins;
48663c56 2627
f2b60f7d
FG
2628 // We skip native libraries because:
2629 // 1. This native libraries won't be used from the generated rlib,
2630 // so we can throw them away to avoid the copying work.
2631 // 2. We can't allow it to be a single remaining entry in archive
2632 // as some linkers may complain on that.
2633 if bundled_lib_file_names.contains(&Symbol::intern(f)) {
2634 return true;
2635 }
2636
064997fb
FG
2637 if skip_because_cfg_say_so || skip_because_lto {
2638 return true;
2639 }
48663c56 2640
064997fb
FG
2641 false
2642 }),
2643 ) {
923072b8
FG
2644 sess.fatal(&format!("failed to build archive from rlib: {}", e));
2645 }
064997fb 2646 if archive.build(&dst) {
923072b8 2647 link_upstream(&dst);
48663c56 2648 }
48663c56
XL
2649 });
2650 }
2651
2652 // Same thing as above, but for dynamic crates instead of static crates.
2653 fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
2654 // Just need to tell the linker about where the library lives and
2655 // what its name is
2656 let parent = cratepath.parent();
2657 if let Some(dir) = parent {
2658 cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
2659 }
2660 let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
dfeec247 2661 cmd.link_rust_dylib(
064997fb 2662 &unlib(&sess.target, filestem),
6a06907d 2663 parent.unwrap_or_else(|| Path::new("")),
dfeec247 2664 );
48663c56
XL
2665 }
2666}
2667
fc512014
XL
2668/// Link in all of our upstream crates' native dependencies. Remember that all of these upstream
2669/// native dependencies are all non-static dependencies. We've got two cases then:
2670///
2671/// 1. The upstream crate is an rlib. In this case we *must* link in the native dependency because
2672/// the rlib is just an archive.
2673///
2674/// 2. The upstream crate is a dylib. In order to use the dylib, we have to have the dependency
2675/// present on the system somewhere. Thus, we don't gain a whole lot from not linking in the
2676/// dynamic dependency to this crate as well.
2677///
2678/// The use case for this is a little subtle. In theory the native dependencies of a crate are
2679/// purely an implementation detail of the crate itself, but the problem arises with generic and
2680/// inlined functions. If a generic function calls a native function, then the generic function
2681/// must be instantiated in the target crate, meaning that the native symbol must also be resolved
2682/// in the target crate.
ba9703b0 2683fn add_upstream_native_libraries(
e74abb32
XL
2684 cmd: &mut dyn Linker,
2685 sess: &Session,
2686 codegen_results: &CodegenResults,
e74abb32 2687) {
5e7ed085 2688 let mut last = (None, NativeLibKind::Unspecified, None);
94222f64 2689 for &cnum in &codegen_results.crate_info.used_crates {
48663c56 2690 for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
5e7ed085
FG
2691 let Some(name) = lib.name else {
2692 continue;
48663c56 2693 };
064997fb 2694 let name = name.as_str();
48663c56 2695 if !relevant_lib(sess, &lib) {
dfeec247 2696 continue;
48663c56 2697 }
17df50a5
XL
2698
2699 // Skip if this library is the same as the last.
5e7ed085
FG
2700 last = if (lib.name, lib.kind, lib.verbatim) == last {
2701 continue;
2702 } else {
2703 (lib.name, lib.kind, lib.verbatim)
2704 };
17df50a5
XL
2705
2706 let verbatim = lib.verbatim.unwrap_or(false);
48663c56 2707 match lib.kind {
17df50a5
XL
2708 NativeLibKind::Dylib { as_needed } => {
2709 cmd.link_dylib(name, verbatim, as_needed.unwrap_or(true))
2710 }
2711 NativeLibKind::Unspecified => cmd.link_dylib(name, verbatim, true),
2712 NativeLibKind::Framework { as_needed } => {
2713 cmd.link_framework(name, as_needed.unwrap_or(true))
2714 }
94222f64
XL
2715 // ignore static native libraries here as we've
2716 // already included them in add_local_native_libraries and
2717 // add_upstream_rust_crates
2718 NativeLibKind::Static { .. } => {}
064997fb 2719 NativeLibKind::RawDylib | NativeLibKind::LinkArg => {}
48663c56
XL
2720 }
2721 }
2722 }
2723}
2724
f9f354fc 2725fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
48663c56 2726 match lib.cfg {
5e7ed085 2727 Some(ref cfg) => rustc_attr::cfg_matches(cfg, &sess.parse_sess, CRATE_NODE_ID, None),
48663c56
XL
2728 None => true,
2729 }
2730}
2731
f2b60f7d 2732pub(crate) fn are_upstream_rust_objects_already_included(sess: &Session) -> bool {
48663c56
XL
2733 match sess.lto() {
2734 config::Lto::Fat => true,
2735 config::Lto::Thin => {
2736 // If we defer LTO to the linker, we haven't run LTO ourselves, so
2737 // any upstream object files have not been copied yet.
2738 !sess.opts.cg.linker_plugin_lto.enabled()
2739 }
dfeec247 2740 config::Lto::No | config::Lto::ThinLocal => false,
48663c56
XL
2741 }
2742}
1b1a35ee
XL
2743
2744fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
29967ef6
XL
2745 let arch = &sess.target.arch;
2746 let os = &sess.target.os;
2747 let llvm_target = &sess.target.llvm_target;
2748 if sess.target.vendor != "apple"
f2b60f7d 2749 || !matches!(os.as_ref(), "ios" | "tvos" | "watchos" | "macos")
064997fb 2750 || (flavor != LinkerFlavor::Gcc && flavor != LinkerFlavor::Lld(LldFlavor::Ld64))
1b1a35ee
XL
2751 {
2752 return;
2753 }
f2b60f7d
FG
2754
2755 if os == "macos" && flavor != LinkerFlavor::Lld(LldFlavor::Ld64) {
2756 return;
2757 }
2758
5e7ed085 2759 let sdk_name = match (arch.as_ref(), os.as_ref()) {
1b1a35ee
XL
2760 ("aarch64", "tvos") => "appletvos",
2761 ("x86_64", "tvos") => "appletvsimulator",
2762 ("arm", "ios") => "iphoneos",
fc512014 2763 ("aarch64", "ios") if llvm_target.contains("macabi") => "macosx",
923072b8 2764 ("aarch64", "ios") if llvm_target.ends_with("-simulator") => "iphonesimulator",
1b1a35ee
XL
2765 ("aarch64", "ios") => "iphoneos",
2766 ("x86", "ios") => "iphonesimulator",
fc512014 2767 ("x86_64", "ios") if llvm_target.contains("macabi") => "macosx",
1b1a35ee 2768 ("x86_64", "ios") => "iphonesimulator",
923072b8
FG
2769 ("x86_64", "watchos") => "watchsimulator",
2770 ("arm64_32", "watchos") => "watchos",
2771 ("aarch64", "watchos") if llvm_target.ends_with("-simulator") => "watchsimulator",
2772 ("aarch64", "watchos") => "watchos",
2773 ("arm", "watchos") => "watchos",
f2b60f7d 2774 (_, "macos") => "macosx",
1b1a35ee
XL
2775 _ => {
2776 sess.err(&format!("unsupported arch `{}` for os `{}`", arch, os));
2777 return;
2778 }
2779 };
2780 let sdk_root = match get_apple_sdk_root(sdk_name) {
2781 Ok(s) => s,
2782 Err(e) => {
2783 sess.err(&e);
2784 return;
2785 }
2786 };
064997fb
FG
2787
2788 match flavor {
2789 LinkerFlavor::Gcc => {
2790 cmd.args(&["-isysroot", &sdk_root, "-Wl,-syslibroot", &sdk_root]);
2791 }
2792 LinkerFlavor::Lld(LldFlavor::Ld64) => {
2793 cmd.args(&["-syslibroot", &sdk_root]);
2794 }
2795 _ => unreachable!(),
5869c6ff 2796 }
1b1a35ee
XL
2797}
2798
2799fn get_apple_sdk_root(sdk_name: &str) -> Result<String, String> {
2800 // Following what clang does
2801 // (https://github.com/llvm/llvm-project/blob/
2802 // 296a80102a9b72c3eda80558fb78a3ed8849b341/clang/lib/Driver/ToolChains/Darwin.cpp#L1661-L1678)
2803 // to allow the SDK path to be set. (For clang, xcrun sets
2804 // SDKROOT; for rustc, the user or build system can set it, or we
2805 // can fall back to checking for xcrun on PATH.)
2806 if let Ok(sdkroot) = env::var("SDKROOT") {
2807 let p = Path::new(&sdkroot);
2808 match sdk_name {
2809 // Ignore `SDKROOT` if it's clearly set for the wrong platform.
2810 "appletvos"
2811 if sdkroot.contains("TVSimulator.platform")
2812 || sdkroot.contains("MacOSX.platform") => {}
2813 "appletvsimulator"
2814 if sdkroot.contains("TVOS.platform") || sdkroot.contains("MacOSX.platform") => {}
2815 "iphoneos"
2816 if sdkroot.contains("iPhoneSimulator.platform")
2817 || sdkroot.contains("MacOSX.platform") => {}
2818 "iphonesimulator"
2819 if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("MacOSX.platform") => {
2820 }
2821 "macosx10.15"
2822 if sdkroot.contains("iPhoneOS.platform")
2823 || sdkroot.contains("iPhoneSimulator.platform") => {}
923072b8
FG
2824 "watchos"
2825 if sdkroot.contains("WatchSimulator.platform")
2826 || sdkroot.contains("MacOSX.platform") => {}
2827 "watchsimulator"
2828 if sdkroot.contains("WatchOS.platform") || sdkroot.contains("MacOSX.platform") => {}
1b1a35ee
XL
2829 // Ignore `SDKROOT` if it's not a valid path.
2830 _ if !p.is_absolute() || p == Path::new("/") || !p.exists() => {}
2831 _ => return Ok(sdkroot),
2832 }
2833 }
2834 let res =
2835 Command::new("xcrun").arg("--show-sdk-path").arg("-sdk").arg(sdk_name).output().and_then(
2836 |output| {
2837 if output.status.success() {
2838 Ok(String::from_utf8(output.stdout).unwrap())
2839 } else {
2840 let error = String::from_utf8(output.stderr);
2841 let error = format!("process exit with error: {}", error.unwrap());
2842 Err(io::Error::new(io::ErrorKind::Other, &error[..]))
2843 }
2844 },
2845 );
2846
2847 match res {
2848 Ok(output) => Ok(output.trim().to_string()),
2849 Err(e) => Err(format!("failed to get {} SDK path: {}", sdk_name, e)),
2850 }
2851}
17df50a5
XL
2852
2853fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
064997fb 2854 if let Some(ld_impl) = sess.opts.unstable_opts.gcc_ld {
17df50a5
XL
2855 if let LinkerFlavor::Gcc = flavor {
2856 match ld_impl {
2857 LdImpl::Lld => {
f2b60f7d
FG
2858 // Implement the "self-contained" part of -Zgcc-ld
2859 // by adding rustc distribution directories to the tool search path.
2860 for path in sess.get_tools_search_paths(false) {
2861 cmd.arg({
2862 let mut arg = OsString::from("-B");
2863 arg.push(path.join("gcc-ld"));
2864 arg
2865 });
2866 }
2867 // Implement the "linker flavor" part of -Zgcc-ld
2868 // by asking cc to use some kind of lld.
2869 cmd.arg("-fuse-ld=lld");
2870 if sess.target.lld_flavor != LldFlavor::Ld {
2871 // Tell clang to use a non-default LLD flavor.
2872 // Gcc doesn't understand the target option, but we currently assume
2873 // that gcc is not used for Apple and Wasm targets (#97402).
2874 cmd.arg(format!("--target={}", sess.target.llvm_target));
2875 }
17df50a5
XL
2876 }
2877 }
2878 } else {
2879 sess.fatal("option `-Z gcc-ld` is used even though linker flavor is not gcc");
2880 }
2881 }
2882}